s3_clnt.c 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523
  1. /* ssl/s3_clnt.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson ([email protected]).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young ([email protected])"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson ([email protected])"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * [email protected].
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * ([email protected]). This product includes software written by Tim
  108. * Hudson ([email protected]).
  109. *
  110. */
  111. /* ====================================================================
  112. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  113. *
  114. * Portions of the attached software ("Contribution") are developed by
  115. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  116. *
  117. * The Contribution is licensed pursuant to the OpenSSL open source
  118. * license provided above.
  119. *
  120. * ECC cipher suite support in OpenSSL originally written by
  121. * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
  122. *
  123. */
  124. /* ====================================================================
  125. * Copyright 2005 Nokia. All rights reserved.
  126. *
  127. * The portions of the attached software ("Contribution") is developed by
  128. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  129. * license.
  130. *
  131. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  132. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  133. * support (see RFC 4279) to OpenSSL.
  134. *
  135. * No patent licenses or other rights except those expressly stated in
  136. * the OpenSSL open source license shall be deemed granted or received
  137. * expressly, by implication, estoppel, or otherwise.
  138. *
  139. * No assurances are provided by Nokia that the Contribution does not
  140. * infringe the patent or other intellectual property rights of any third
  141. * party or that the license provides you with all the necessary rights
  142. * to make use of the Contribution.
  143. *
  144. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  145. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  146. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  147. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  148. * OTHERWISE.
  149. */
  150. #include <stdio.h>
  151. #include "ssl_locl.h"
  152. #include "kssl_lcl.h"
  153. #include <openssl/buffer.h>
  154. #include <openssl/rand.h>
  155. #include <openssl/objects.h>
  156. #include <openssl/evp.h>
  157. #include <openssl/md5.h>
  158. #ifdef OPENSSL_FIPS
  159. # include <openssl/fips.h>
  160. #endif
  161. #ifndef OPENSSL_NO_DH
  162. # include <openssl/dh.h>
  163. #endif
  164. #include <openssl/bn.h>
  165. #ifndef OPENSSL_NO_ENGINE
  166. # include <openssl/engine.h>
  167. #endif
  168. static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
  169. #ifndef OPENSSL_NO_TLSEXT
  170. static int ssl3_check_finished(SSL *s);
  171. #endif
  172. #ifndef OPENSSL_NO_SSL3_METHOD
  173. static const SSL_METHOD *ssl3_get_client_method(int ver)
  174. {
  175. if (ver == SSL3_VERSION)
  176. return (SSLv3_client_method());
  177. else
  178. return (NULL);
  179. }
  180. IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
  181. ssl_undefined_function,
  182. ssl3_connect, ssl3_get_client_method)
  183. #endif
  184. int ssl3_connect(SSL *s)
  185. {
  186. BUF_MEM *buf = NULL;
  187. unsigned long Time = (unsigned long)time(NULL);
  188. void (*cb) (const SSL *ssl, int type, int val) = NULL;
  189. int ret = -1;
  190. int new_state, state, skip = 0;
  191. RAND_add(&Time, sizeof(Time), 0);
  192. ERR_clear_error();
  193. clear_sys_error();
  194. if (s->info_callback != NULL)
  195. cb = s->info_callback;
  196. else if (s->ctx->info_callback != NULL)
  197. cb = s->ctx->info_callback;
  198. s->in_handshake++;
  199. if (!SSL_in_init(s) || SSL_in_before(s))
  200. SSL_clear(s);
  201. #ifndef OPENSSL_NO_HEARTBEATS
  202. /*
  203. * If we're awaiting a HeartbeatResponse, pretend we already got and
  204. * don't await it anymore, because Heartbeats don't make sense during
  205. * handshakes anyway.
  206. */
  207. if (s->tlsext_hb_pending) {
  208. s->tlsext_hb_pending = 0;
  209. s->tlsext_hb_seq++;
  210. }
  211. #endif
  212. for (;;) {
  213. state = s->state;
  214. switch (s->state) {
  215. case SSL_ST_RENEGOTIATE:
  216. s->renegotiate = 1;
  217. s->state = SSL_ST_CONNECT;
  218. s->ctx->stats.sess_connect_renegotiate++;
  219. /* break */
  220. case SSL_ST_BEFORE:
  221. case SSL_ST_CONNECT:
  222. case SSL_ST_BEFORE | SSL_ST_CONNECT:
  223. case SSL_ST_OK | SSL_ST_CONNECT:
  224. s->server = 0;
  225. if (cb != NULL)
  226. cb(s, SSL_CB_HANDSHAKE_START, 1);
  227. if ((s->version & 0xff00) != 0x0300) {
  228. SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
  229. s->state = SSL_ST_ERR;
  230. ret = -1;
  231. goto end;
  232. }
  233. /* s->version=SSL3_VERSION; */
  234. s->type = SSL_ST_CONNECT;
  235. if (s->init_buf == NULL) {
  236. if ((buf = BUF_MEM_new()) == NULL) {
  237. ret = -1;
  238. s->state = SSL_ST_ERR;
  239. goto end;
  240. }
  241. if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
  242. ret = -1;
  243. s->state = SSL_ST_ERR;
  244. goto end;
  245. }
  246. s->init_buf = buf;
  247. buf = NULL;
  248. }
  249. if (!ssl3_setup_buffers(s)) {
  250. ret = -1;
  251. goto end;
  252. }
  253. /* setup buffing BIO */
  254. if (!ssl_init_wbio_buffer(s, 0)) {
  255. ret = -1;
  256. s->state = SSL_ST_ERR;
  257. goto end;
  258. }
  259. /* don't push the buffering BIO quite yet */
  260. ssl3_init_finished_mac(s);
  261. s->state = SSL3_ST_CW_CLNT_HELLO_A;
  262. s->ctx->stats.sess_connect++;
  263. s->init_num = 0;
  264. s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
  265. /*
  266. * Should have been reset by ssl3_get_finished, too.
  267. */
  268. s->s3->change_cipher_spec = 0;
  269. break;
  270. case SSL3_ST_CW_CLNT_HELLO_A:
  271. case SSL3_ST_CW_CLNT_HELLO_B:
  272. s->shutdown = 0;
  273. ret = ssl3_client_hello(s);
  274. if (ret <= 0)
  275. goto end;
  276. s->state = SSL3_ST_CR_SRVR_HELLO_A;
  277. s->init_num = 0;
  278. /* turn on buffering for the next lot of output */
  279. if (s->bbio != s->wbio)
  280. s->wbio = BIO_push(s->bbio, s->wbio);
  281. break;
  282. case SSL3_ST_CR_SRVR_HELLO_A:
  283. case SSL3_ST_CR_SRVR_HELLO_B:
  284. ret = ssl3_get_server_hello(s);
  285. if (ret <= 0)
  286. goto end;
  287. if (s->hit) {
  288. s->state = SSL3_ST_CR_FINISHED_A;
  289. #ifndef OPENSSL_NO_TLSEXT
  290. if (s->tlsext_ticket_expected) {
  291. /* receive renewed session ticket */
  292. s->state = SSL3_ST_CR_SESSION_TICKET_A;
  293. }
  294. #endif
  295. } else
  296. s->state = SSL3_ST_CR_CERT_A;
  297. s->init_num = 0;
  298. break;
  299. case SSL3_ST_CR_CERT_A:
  300. case SSL3_ST_CR_CERT_B:
  301. #ifndef OPENSSL_NO_TLSEXT
  302. /* Noop (ret = 0) for everything but EAP-FAST. */
  303. ret = ssl3_check_finished(s);
  304. if (ret < 0)
  305. goto end;
  306. if (ret == 1) {
  307. s->hit = 1;
  308. s->state = SSL3_ST_CR_FINISHED_A;
  309. s->init_num = 0;
  310. break;
  311. }
  312. #endif
  313. /* Check if it is anon DH/ECDH, SRP auth */
  314. /* or PSK */
  315. if (!
  316. (s->s3->tmp.
  317. new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
  318. && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
  319. ret = ssl3_get_server_certificate(s);
  320. if (ret <= 0)
  321. goto end;
  322. #ifndef OPENSSL_NO_TLSEXT
  323. if (s->tlsext_status_expected)
  324. s->state = SSL3_ST_CR_CERT_STATUS_A;
  325. else
  326. s->state = SSL3_ST_CR_KEY_EXCH_A;
  327. } else {
  328. skip = 1;
  329. s->state = SSL3_ST_CR_KEY_EXCH_A;
  330. }
  331. #else
  332. } else
  333. skip = 1;
  334. s->state = SSL3_ST_CR_KEY_EXCH_A;
  335. #endif
  336. s->init_num = 0;
  337. break;
  338. case SSL3_ST_CR_KEY_EXCH_A:
  339. case SSL3_ST_CR_KEY_EXCH_B:
  340. ret = ssl3_get_key_exchange(s);
  341. if (ret <= 0)
  342. goto end;
  343. s->state = SSL3_ST_CR_CERT_REQ_A;
  344. s->init_num = 0;
  345. /*
  346. * at this point we check that we have the required stuff from
  347. * the server
  348. */
  349. if (!ssl3_check_cert_and_algorithm(s)) {
  350. ret = -1;
  351. s->state = SSL_ST_ERR;
  352. goto end;
  353. }
  354. break;
  355. case SSL3_ST_CR_CERT_REQ_A:
  356. case SSL3_ST_CR_CERT_REQ_B:
  357. ret = ssl3_get_certificate_request(s);
  358. if (ret <= 0)
  359. goto end;
  360. s->state = SSL3_ST_CR_SRVR_DONE_A;
  361. s->init_num = 0;
  362. break;
  363. case SSL3_ST_CR_SRVR_DONE_A:
  364. case SSL3_ST_CR_SRVR_DONE_B:
  365. ret = ssl3_get_server_done(s);
  366. if (ret <= 0)
  367. goto end;
  368. #ifndef OPENSSL_NO_SRP
  369. if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
  370. if ((ret = SRP_Calc_A_param(s)) <= 0) {
  371. SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC);
  372. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  373. s->state = SSL_ST_ERR;
  374. goto end;
  375. }
  376. }
  377. #endif
  378. if (s->s3->tmp.cert_req)
  379. s->state = SSL3_ST_CW_CERT_A;
  380. else
  381. s->state = SSL3_ST_CW_KEY_EXCH_A;
  382. s->init_num = 0;
  383. break;
  384. case SSL3_ST_CW_CERT_A:
  385. case SSL3_ST_CW_CERT_B:
  386. case SSL3_ST_CW_CERT_C:
  387. case SSL3_ST_CW_CERT_D:
  388. ret = ssl3_send_client_certificate(s);
  389. if (ret <= 0)
  390. goto end;
  391. s->state = SSL3_ST_CW_KEY_EXCH_A;
  392. s->init_num = 0;
  393. break;
  394. case SSL3_ST_CW_KEY_EXCH_A:
  395. case SSL3_ST_CW_KEY_EXCH_B:
  396. ret = ssl3_send_client_key_exchange(s);
  397. if (ret <= 0)
  398. goto end;
  399. /*
  400. * EAY EAY EAY need to check for DH fix cert sent back
  401. */
  402. /*
  403. * For TLS, cert_req is set to 2, so a cert chain of nothing is
  404. * sent, but no verify packet is sent
  405. */
  406. /*
  407. * XXX: For now, we do not support client authentication in ECDH
  408. * cipher suites with ECDH (rather than ECDSA) certificates. We
  409. * need to skip the certificate verify message when client's
  410. * ECDH public key is sent inside the client certificate.
  411. */
  412. if (s->s3->tmp.cert_req == 1) {
  413. s->state = SSL3_ST_CW_CERT_VRFY_A;
  414. } else {
  415. s->state = SSL3_ST_CW_CHANGE_A;
  416. }
  417. if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
  418. s->state = SSL3_ST_CW_CHANGE_A;
  419. }
  420. s->init_num = 0;
  421. break;
  422. case SSL3_ST_CW_CERT_VRFY_A:
  423. case SSL3_ST_CW_CERT_VRFY_B:
  424. ret = ssl3_send_client_verify(s);
  425. if (ret <= 0)
  426. goto end;
  427. s->state = SSL3_ST_CW_CHANGE_A;
  428. s->init_num = 0;
  429. break;
  430. case SSL3_ST_CW_CHANGE_A:
  431. case SSL3_ST_CW_CHANGE_B:
  432. ret = ssl3_send_change_cipher_spec(s,
  433. SSL3_ST_CW_CHANGE_A,
  434. SSL3_ST_CW_CHANGE_B);
  435. if (ret <= 0)
  436. goto end;
  437. #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
  438. s->state = SSL3_ST_CW_FINISHED_A;
  439. #else
  440. if (s->s3->next_proto_neg_seen)
  441. s->state = SSL3_ST_CW_NEXT_PROTO_A;
  442. else
  443. s->state = SSL3_ST_CW_FINISHED_A;
  444. #endif
  445. s->init_num = 0;
  446. s->session->cipher = s->s3->tmp.new_cipher;
  447. #ifdef OPENSSL_NO_COMP
  448. s->session->compress_meth = 0;
  449. #else
  450. if (s->s3->tmp.new_compression == NULL)
  451. s->session->compress_meth = 0;
  452. else
  453. s->session->compress_meth = s->s3->tmp.new_compression->id;
  454. #endif
  455. if (!s->method->ssl3_enc->setup_key_block(s)) {
  456. ret = -1;
  457. s->state = SSL_ST_ERR;
  458. goto end;
  459. }
  460. if (!s->method->ssl3_enc->change_cipher_state(s,
  461. SSL3_CHANGE_CIPHER_CLIENT_WRITE))
  462. {
  463. ret = -1;
  464. s->state = SSL_ST_ERR;
  465. goto end;
  466. }
  467. break;
  468. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  469. case SSL3_ST_CW_NEXT_PROTO_A:
  470. case SSL3_ST_CW_NEXT_PROTO_B:
  471. ret = ssl3_send_next_proto(s);
  472. if (ret <= 0)
  473. goto end;
  474. s->state = SSL3_ST_CW_FINISHED_A;
  475. break;
  476. #endif
  477. case SSL3_ST_CW_FINISHED_A:
  478. case SSL3_ST_CW_FINISHED_B:
  479. ret = ssl3_send_finished(s,
  480. SSL3_ST_CW_FINISHED_A,
  481. SSL3_ST_CW_FINISHED_B,
  482. s->method->
  483. ssl3_enc->client_finished_label,
  484. s->method->
  485. ssl3_enc->client_finished_label_len);
  486. if (ret <= 0)
  487. goto end;
  488. s->state = SSL3_ST_CW_FLUSH;
  489. /* clear flags */
  490. s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
  491. if (s->hit) {
  492. s->s3->tmp.next_state = SSL_ST_OK;
  493. if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
  494. s->state = SSL_ST_OK;
  495. s->s3->flags |= SSL3_FLAGS_POP_BUFFER;
  496. s->s3->delay_buf_pop_ret = 0;
  497. }
  498. } else {
  499. #ifndef OPENSSL_NO_TLSEXT
  500. /*
  501. * Allow NewSessionTicket if ticket expected
  502. */
  503. if (s->tlsext_ticket_expected)
  504. s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
  505. else
  506. #endif
  507. s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
  508. }
  509. s->init_num = 0;
  510. break;
  511. #ifndef OPENSSL_NO_TLSEXT
  512. case SSL3_ST_CR_SESSION_TICKET_A:
  513. case SSL3_ST_CR_SESSION_TICKET_B:
  514. ret = ssl3_get_new_session_ticket(s);
  515. if (ret <= 0)
  516. goto end;
  517. s->state = SSL3_ST_CR_FINISHED_A;
  518. s->init_num = 0;
  519. break;
  520. case SSL3_ST_CR_CERT_STATUS_A:
  521. case SSL3_ST_CR_CERT_STATUS_B:
  522. ret = ssl3_get_cert_status(s);
  523. if (ret <= 0)
  524. goto end;
  525. s->state = SSL3_ST_CR_KEY_EXCH_A;
  526. s->init_num = 0;
  527. break;
  528. #endif
  529. case SSL3_ST_CR_FINISHED_A:
  530. case SSL3_ST_CR_FINISHED_B:
  531. if (!s->s3->change_cipher_spec)
  532. s->s3->flags |= SSL3_FLAGS_CCS_OK;
  533. ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
  534. SSL3_ST_CR_FINISHED_B);
  535. if (ret <= 0)
  536. goto end;
  537. if (s->hit)
  538. s->state = SSL3_ST_CW_CHANGE_A;
  539. else
  540. s->state = SSL_ST_OK;
  541. s->init_num = 0;
  542. break;
  543. case SSL3_ST_CW_FLUSH:
  544. s->rwstate = SSL_WRITING;
  545. if (BIO_flush(s->wbio) <= 0) {
  546. ret = -1;
  547. goto end;
  548. }
  549. s->rwstate = SSL_NOTHING;
  550. s->state = s->s3->tmp.next_state;
  551. break;
  552. case SSL_ST_OK:
  553. /* clean a few things up */
  554. ssl3_cleanup_key_block(s);
  555. if (s->init_buf != NULL) {
  556. BUF_MEM_free(s->init_buf);
  557. s->init_buf = NULL;
  558. }
  559. /*
  560. * If we are not 'joining' the last two packets, remove the
  561. * buffering now
  562. */
  563. if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
  564. ssl_free_wbio_buffer(s);
  565. /* else do it later in ssl3_write */
  566. s->init_num = 0;
  567. s->renegotiate = 0;
  568. s->new_session = 0;
  569. ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
  570. if (s->hit)
  571. s->ctx->stats.sess_hit++;
  572. ret = 1;
  573. /* s->server=0; */
  574. s->handshake_func = ssl3_connect;
  575. s->ctx->stats.sess_connect_good++;
  576. if (cb != NULL)
  577. cb(s, SSL_CB_HANDSHAKE_DONE, 1);
  578. goto end;
  579. /* break; */
  580. case SSL_ST_ERR:
  581. default:
  582. SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE);
  583. ret = -1;
  584. goto end;
  585. /* break; */
  586. }
  587. /* did we do anything */
  588. if (!s->s3->tmp.reuse_message && !skip) {
  589. if (s->debug) {
  590. if ((ret = BIO_flush(s->wbio)) <= 0)
  591. goto end;
  592. }
  593. if ((cb != NULL) && (s->state != state)) {
  594. new_state = s->state;
  595. s->state = state;
  596. cb(s, SSL_CB_CONNECT_LOOP, 1);
  597. s->state = new_state;
  598. }
  599. }
  600. skip = 0;
  601. }
  602. end:
  603. s->in_handshake--;
  604. if (buf != NULL)
  605. BUF_MEM_free(buf);
  606. if (cb != NULL)
  607. cb(s, SSL_CB_CONNECT_EXIT, ret);
  608. return (ret);
  609. }
  610. int ssl3_client_hello(SSL *s)
  611. {
  612. unsigned char *buf;
  613. unsigned char *p, *d;
  614. int i;
  615. unsigned long l;
  616. #ifndef OPENSSL_NO_COMP
  617. int j;
  618. SSL_COMP *comp;
  619. #endif
  620. buf = (unsigned char *)s->init_buf->data;
  621. if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
  622. SSL_SESSION *sess = s->session;
  623. if ((sess == NULL) || (sess->ssl_version != s->version) ||
  624. #ifdef OPENSSL_NO_TLSEXT
  625. !sess->session_id_length ||
  626. #else
  627. /*
  628. * In the case of EAP-FAST, we can have a pre-shared
  629. * "ticket" without a session ID.
  630. */
  631. (!sess->session_id_length && !sess->tlsext_tick) ||
  632. #endif
  633. (sess->not_resumable)) {
  634. if (!ssl_get_new_session(s, 0))
  635. goto err;
  636. }
  637. /* else use the pre-loaded session */
  638. p = s->s3->client_random;
  639. if (ssl_fill_hello_random(s, 0, p, SSL3_RANDOM_SIZE) <= 0)
  640. goto err;
  641. /* Do the message type and length last */
  642. d = p = &(buf[4]);
  643. /*-
  644. * version indicates the negotiated version: for example from
  645. * an SSLv2/v3 compatible client hello). The client_version
  646. * field is the maximum version we permit and it is also
  647. * used in RSA encrypted premaster secrets. Some servers can
  648. * choke if we initially report a higher version then
  649. * renegotiate to a lower one in the premaster secret. This
  650. * didn't happen with TLS 1.0 as most servers supported it
  651. * but it can with TLS 1.1 or later if the server only supports
  652. * 1.0.
  653. *
  654. * Possible scenario with previous logic:
  655. * 1. Client hello indicates TLS 1.2
  656. * 2. Server hello says TLS 1.0
  657. * 3. RSA encrypted premaster secret uses 1.2.
  658. * 4. Handhaked proceeds using TLS 1.0.
  659. * 5. Server sends hello request to renegotiate.
  660. * 6. Client hello indicates TLS v1.0 as we now
  661. * know that is maximum server supports.
  662. * 7. Server chokes on RSA encrypted premaster secret
  663. * containing version 1.0.
  664. *
  665. * For interoperability it should be OK to always use the
  666. * maximum version we support in client hello and then rely
  667. * on the checking of version to ensure the servers isn't
  668. * being inconsistent: for example initially negotiating with
  669. * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
  670. * client_version in client hello and not resetting it to
  671. * the negotiated version.
  672. */
  673. #if 0
  674. *(p++) = s->version >> 8;
  675. *(p++) = s->version & 0xff;
  676. s->client_version = s->version;
  677. #else
  678. *(p++) = s->client_version >> 8;
  679. *(p++) = s->client_version & 0xff;
  680. #endif
  681. /* Random stuff */
  682. memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
  683. p += SSL3_RANDOM_SIZE;
  684. /* Session ID */
  685. if (s->new_session)
  686. i = 0;
  687. else
  688. i = s->session->session_id_length;
  689. *(p++) = i;
  690. if (i != 0) {
  691. if (i > (int)sizeof(s->session->session_id)) {
  692. SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  693. goto err;
  694. }
  695. memcpy(p, s->session->session_id, i);
  696. p += i;
  697. }
  698. /* Ciphers supported */
  699. i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
  700. if (i == 0) {
  701. SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
  702. goto err;
  703. }
  704. #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
  705. /*
  706. * Some servers hang if client hello > 256 bytes as hack workaround
  707. * chop number of supported ciphers to keep it well below this if we
  708. * use TLS v1.2
  709. */
  710. if (TLS1_get_version(s) >= TLS1_2_VERSION
  711. && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
  712. i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
  713. #endif
  714. s2n(i, p);
  715. p += i;
  716. /* COMPRESSION */
  717. #ifdef OPENSSL_NO_COMP
  718. *(p++) = 1;
  719. #else
  720. if ((s->options & SSL_OP_NO_COMPRESSION)
  721. || !s->ctx->comp_methods)
  722. j = 0;
  723. else
  724. j = sk_SSL_COMP_num(s->ctx->comp_methods);
  725. *(p++) = 1 + j;
  726. for (i = 0; i < j; i++) {
  727. comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
  728. *(p++) = comp->id;
  729. }
  730. #endif
  731. *(p++) = 0; /* Add the NULL method */
  732. #ifndef OPENSSL_NO_TLSEXT
  733. /* TLS extensions */
  734. if (ssl_prepare_clienthello_tlsext(s) <= 0) {
  735. SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
  736. goto err;
  737. }
  738. if ((p =
  739. ssl_add_clienthello_tlsext(s, p,
  740. buf + SSL3_RT_MAX_PLAIN_LENGTH)) ==
  741. NULL) {
  742. SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  743. goto err;
  744. }
  745. #endif
  746. l = (p - d);
  747. d = buf;
  748. *(d++) = SSL3_MT_CLIENT_HELLO;
  749. l2n3(l, d);
  750. s->state = SSL3_ST_CW_CLNT_HELLO_B;
  751. /* number of bytes to write */
  752. s->init_num = p - buf;
  753. s->init_off = 0;
  754. }
  755. /* SSL3_ST_CW_CLNT_HELLO_B */
  756. return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
  757. err:
  758. s->state = SSL_ST_ERR;
  759. return (-1);
  760. }
  761. int ssl3_get_server_hello(SSL *s)
  762. {
  763. STACK_OF(SSL_CIPHER) *sk;
  764. const SSL_CIPHER *c;
  765. unsigned char *p, *d;
  766. int i, al, ok;
  767. unsigned int j;
  768. long n;
  769. #ifndef OPENSSL_NO_COMP
  770. SSL_COMP *comp;
  771. #endif
  772. n = s->method->ssl_get_message(s,
  773. SSL3_ST_CR_SRVR_HELLO_A,
  774. SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
  775. if (!ok)
  776. return ((int)n);
  777. if (SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER) {
  778. if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
  779. if (s->d1->send_cookie == 0) {
  780. s->s3->tmp.reuse_message = 1;
  781. return 1;
  782. } else { /* already sent a cookie */
  783. al = SSL_AD_UNEXPECTED_MESSAGE;
  784. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
  785. goto f_err;
  786. }
  787. }
  788. }
  789. if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
  790. al = SSL_AD_UNEXPECTED_MESSAGE;
  791. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
  792. goto f_err;
  793. }
  794. d = p = (unsigned char *)s->init_msg;
  795. if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) {
  796. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
  797. s->version = (s->version & 0xff00) | p[1];
  798. al = SSL_AD_PROTOCOL_VERSION;
  799. goto f_err;
  800. }
  801. p += 2;
  802. /* load the server hello data */
  803. /* load the server random */
  804. memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE);
  805. p += SSL3_RANDOM_SIZE;
  806. s->hit = 0;
  807. /* get the session-id */
  808. j = *(p++);
  809. if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) {
  810. al = SSL_AD_ILLEGAL_PARAMETER;
  811. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
  812. goto f_err;
  813. }
  814. #ifndef OPENSSL_NO_TLSEXT
  815. /*
  816. * Check if we can resume the session based on external pre-shared secret.
  817. * EAP-FAST (RFC 4851) supports two types of session resumption.
  818. * Resumption based on server-side state works with session IDs.
  819. * Resumption based on pre-shared Protected Access Credentials (PACs)
  820. * works by overriding the SessionTicket extension at the application
  821. * layer, and does not send a session ID. (We do not know whether EAP-FAST
  822. * servers would honour the session ID.) Therefore, the session ID alone
  823. * is not a reliable indicator of session resumption, so we first check if
  824. * we can resume, and later peek at the next handshake message to see if the
  825. * server wants to resume.
  826. */
  827. if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
  828. s->session->tlsext_tick) {
  829. SSL_CIPHER *pref_cipher = NULL;
  830. s->session->master_key_length = sizeof(s->session->master_key);
  831. if (s->tls_session_secret_cb(s, s->session->master_key,
  832. &s->session->master_key_length,
  833. NULL, &pref_cipher,
  834. s->tls_session_secret_cb_arg)) {
  835. s->session->cipher = pref_cipher ?
  836. pref_cipher : ssl_get_cipher_by_char(s, p + j);
  837. } else {
  838. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  839. al = SSL_AD_INTERNAL_ERROR;
  840. goto f_err;
  841. }
  842. }
  843. #endif /* OPENSSL_NO_TLSEXT */
  844. if (j != 0 && j == s->session->session_id_length
  845. && memcmp(p, s->session->session_id, j) == 0) {
  846. if (s->sid_ctx_length != s->session->sid_ctx_length
  847. || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
  848. /* actually a client application bug */
  849. al = SSL_AD_ILLEGAL_PARAMETER;
  850. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
  851. SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  852. goto f_err;
  853. }
  854. s->hit = 1;
  855. } else {
  856. /*
  857. * If we were trying for session-id reuse but the server
  858. * didn't echo the ID, make a new SSL_SESSION.
  859. * In the case of EAP-FAST and PAC, we do not send a session ID,
  860. * so the PAC-based session secret is always preserved. It'll be
  861. * overwritten if the server refuses resumption.
  862. */
  863. if (s->session->session_id_length > 0) {
  864. if (!ssl_get_new_session(s, 0)) {
  865. al = SSL_AD_INTERNAL_ERROR;
  866. goto f_err;
  867. }
  868. }
  869. s->session->session_id_length = j;
  870. memcpy(s->session->session_id, p, j); /* j could be 0 */
  871. }
  872. p += j;
  873. c = ssl_get_cipher_by_char(s, p);
  874. if (c == NULL) {
  875. /* unknown cipher */
  876. al = SSL_AD_ILLEGAL_PARAMETER;
  877. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
  878. goto f_err;
  879. }
  880. /* TLS v1.2 only ciphersuites require v1.2 or later */
  881. if ((c->algorithm_ssl & SSL_TLSV1_2) &&
  882. (TLS1_get_version(s) < TLS1_2_VERSION)) {
  883. al = SSL_AD_ILLEGAL_PARAMETER;
  884. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
  885. goto f_err;
  886. }
  887. #ifndef OPENSSL_NO_SRP
  888. if (((c->algorithm_mkey & SSL_kSRP) || (c->algorithm_auth & SSL_aSRP)) &&
  889. !(s->srp_ctx.srp_Mask & SSL_kSRP)) {
  890. al = SSL_AD_ILLEGAL_PARAMETER;
  891. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
  892. goto f_err;
  893. }
  894. #endif /* OPENSSL_NO_SRP */
  895. p += ssl_put_cipher_by_char(s, NULL, NULL);
  896. sk = ssl_get_ciphers_by_id(s);
  897. i = sk_SSL_CIPHER_find(sk, c);
  898. if (i < 0) {
  899. /* we did not say we would use this cipher */
  900. al = SSL_AD_ILLEGAL_PARAMETER;
  901. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
  902. goto f_err;
  903. }
  904. /*
  905. * Depending on the session caching (internal/external), the cipher
  906. * and/or cipher_id values may not be set. Make sure that cipher_id is
  907. * set and use it for comparison.
  908. */
  909. if (s->session->cipher)
  910. s->session->cipher_id = s->session->cipher->id;
  911. if (s->hit && (s->session->cipher_id != c->id)) {
  912. /* Workaround is now obsolete */
  913. #if 0
  914. if (!(s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
  915. #endif
  916. {
  917. al = SSL_AD_ILLEGAL_PARAMETER;
  918. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
  919. SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  920. goto f_err;
  921. }
  922. }
  923. s->s3->tmp.new_cipher = c;
  924. /*
  925. * Don't digest cached records if TLS v1.2: we may need them for client
  926. * authentication.
  927. */
  928. if (TLS1_get_version(s) < TLS1_2_VERSION
  929. && !ssl3_digest_cached_records(s)) {
  930. al = SSL_AD_INTERNAL_ERROR;
  931. goto f_err;
  932. }
  933. /* lets get the compression algorithm */
  934. /* COMPRESSION */
  935. #ifdef OPENSSL_NO_COMP
  936. if (*(p++) != 0) {
  937. al = SSL_AD_ILLEGAL_PARAMETER;
  938. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
  939. SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  940. goto f_err;
  941. }
  942. /*
  943. * If compression is disabled we'd better not try to resume a session
  944. * using compression.
  945. */
  946. if (s->session->compress_meth != 0) {
  947. al = SSL_AD_INTERNAL_ERROR;
  948. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
  949. goto f_err;
  950. }
  951. #else
  952. j = *(p++);
  953. if (s->hit && j != s->session->compress_meth) {
  954. al = SSL_AD_ILLEGAL_PARAMETER;
  955. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
  956. SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
  957. goto f_err;
  958. }
  959. if (j == 0)
  960. comp = NULL;
  961. else if (s->options & SSL_OP_NO_COMPRESSION) {
  962. al = SSL_AD_ILLEGAL_PARAMETER;
  963. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
  964. goto f_err;
  965. } else
  966. comp = ssl3_comp_find(s->ctx->comp_methods, j);
  967. if ((j != 0) && (comp == NULL)) {
  968. al = SSL_AD_ILLEGAL_PARAMETER;
  969. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
  970. SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  971. goto f_err;
  972. } else {
  973. s->s3->tmp.new_compression = comp;
  974. }
  975. #endif
  976. #ifndef OPENSSL_NO_TLSEXT
  977. /* TLS extensions */
  978. if (s->version >= SSL3_VERSION) {
  979. if (!ssl_parse_serverhello_tlsext(s, &p, d, n, &al)) {
  980. /* 'al' set by ssl_parse_serverhello_tlsext */
  981. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
  982. goto f_err;
  983. }
  984. if (ssl_check_serverhello_tlsext(s) <= 0) {
  985. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
  986. goto err;
  987. }
  988. }
  989. #endif
  990. if (p != (d + n)) {
  991. /* wrong packet length */
  992. al = SSL_AD_DECODE_ERROR;
  993. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
  994. goto f_err;
  995. }
  996. return (1);
  997. f_err:
  998. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  999. err:
  1000. s->state = SSL_ST_ERR;
  1001. return (-1);
  1002. }
  1003. int ssl3_get_server_certificate(SSL *s)
  1004. {
  1005. int al, i, ok, ret = -1;
  1006. unsigned long n, nc, llen, l;
  1007. X509 *x = NULL;
  1008. const unsigned char *q, *p;
  1009. unsigned char *d;
  1010. STACK_OF(X509) *sk = NULL;
  1011. SESS_CERT *sc;
  1012. EVP_PKEY *pkey = NULL;
  1013. int need_cert = 1; /* VRS: 0=> will allow null cert if auth ==
  1014. * KRB5 */
  1015. n = s->method->ssl_get_message(s,
  1016. SSL3_ST_CR_CERT_A,
  1017. SSL3_ST_CR_CERT_B,
  1018. -1, s->max_cert_list, &ok);
  1019. if (!ok)
  1020. return ((int)n);
  1021. if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
  1022. ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) &&
  1023. (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE))) {
  1024. s->s3->tmp.reuse_message = 1;
  1025. return (1);
  1026. }
  1027. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
  1028. al = SSL_AD_UNEXPECTED_MESSAGE;
  1029. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
  1030. goto f_err;
  1031. }
  1032. p = d = (unsigned char *)s->init_msg;
  1033. if ((sk = sk_X509_new_null()) == NULL) {
  1034. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  1035. goto err;
  1036. }
  1037. n2l3(p, llen);
  1038. if (llen + 3 != n) {
  1039. al = SSL_AD_DECODE_ERROR;
  1040. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
  1041. goto f_err;
  1042. }
  1043. for (nc = 0; nc < llen;) {
  1044. n2l3(p, l);
  1045. if ((l + nc + 3) > llen) {
  1046. al = SSL_AD_DECODE_ERROR;
  1047. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  1048. SSL_R_CERT_LENGTH_MISMATCH);
  1049. goto f_err;
  1050. }
  1051. q = p;
  1052. x = d2i_X509(NULL, &q, l);
  1053. if (x == NULL) {
  1054. al = SSL_AD_BAD_CERTIFICATE;
  1055. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
  1056. goto f_err;
  1057. }
  1058. if (q != (p + l)) {
  1059. al = SSL_AD_DECODE_ERROR;
  1060. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  1061. SSL_R_CERT_LENGTH_MISMATCH);
  1062. goto f_err;
  1063. }
  1064. if (!sk_X509_push(sk, x)) {
  1065. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  1066. goto err;
  1067. }
  1068. x = NULL;
  1069. nc += l + 3;
  1070. p = q;
  1071. }
  1072. i = ssl_verify_cert_chain(s, sk);
  1073. if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
  1074. #ifndef OPENSSL_NO_KRB5
  1075. && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
  1076. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
  1077. #endif /* OPENSSL_NO_KRB5 */
  1078. ) {
  1079. al = ssl_verify_alarm_type(s->verify_result);
  1080. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  1081. SSL_R_CERTIFICATE_VERIFY_FAILED);
  1082. goto f_err;
  1083. }
  1084. ERR_clear_error(); /* but we keep s->verify_result */
  1085. sc = ssl_sess_cert_new();
  1086. if (sc == NULL)
  1087. goto err;
  1088. if (s->session->sess_cert)
  1089. ssl_sess_cert_free(s->session->sess_cert);
  1090. s->session->sess_cert = sc;
  1091. sc->cert_chain = sk;
  1092. /*
  1093. * Inconsistency alert: cert_chain does include the peer's certificate,
  1094. * which we don't include in s3_srvr.c
  1095. */
  1096. x = sk_X509_value(sk, 0);
  1097. sk = NULL;
  1098. /*
  1099. * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
  1100. */
  1101. pkey = X509_get_pubkey(x);
  1102. /* VRS: allow null cert if auth == KRB5 */
  1103. need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
  1104. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
  1105. ? 0 : 1;
  1106. #ifdef KSSL_DEBUG
  1107. fprintf(stderr, "pkey,x = %p, %p\n", pkey, x);
  1108. fprintf(stderr, "ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x, pkey));
  1109. fprintf(stderr, "cipher, alg, nc = %s, %lx, %lx, %d\n",
  1110. s->s3->tmp.new_cipher->name,
  1111. s->s3->tmp.new_cipher->algorithm_mkey,
  1112. s->s3->tmp.new_cipher->algorithm_auth, need_cert);
  1113. #endif /* KSSL_DEBUG */
  1114. if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) {
  1115. x = NULL;
  1116. al = SSL3_AL_FATAL;
  1117. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  1118. SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
  1119. goto f_err;
  1120. }
  1121. i = ssl_cert_type(x, pkey);
  1122. if (need_cert && i < 0) {
  1123. x = NULL;
  1124. al = SSL3_AL_FATAL;
  1125. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  1126. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  1127. goto f_err;
  1128. }
  1129. if (need_cert) {
  1130. sc->peer_cert_type = i;
  1131. CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
  1132. /*
  1133. * Why would the following ever happen? We just created sc a couple
  1134. * of lines ago.
  1135. */
  1136. if (sc->peer_pkeys[i].x509 != NULL)
  1137. X509_free(sc->peer_pkeys[i].x509);
  1138. sc->peer_pkeys[i].x509 = x;
  1139. sc->peer_key = &(sc->peer_pkeys[i]);
  1140. if (s->session->peer != NULL)
  1141. X509_free(s->session->peer);
  1142. CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
  1143. s->session->peer = x;
  1144. } else {
  1145. sc->peer_cert_type = i;
  1146. sc->peer_key = NULL;
  1147. if (s->session->peer != NULL)
  1148. X509_free(s->session->peer);
  1149. s->session->peer = NULL;
  1150. }
  1151. s->session->verify_result = s->verify_result;
  1152. x = NULL;
  1153. ret = 1;
  1154. if (0) {
  1155. f_err:
  1156. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1157. err:
  1158. s->state = SSL_ST_ERR;
  1159. }
  1160. EVP_PKEY_free(pkey);
  1161. X509_free(x);
  1162. sk_X509_pop_free(sk, X509_free);
  1163. return (ret);
  1164. }
  1165. int ssl3_get_key_exchange(SSL *s)
  1166. {
  1167. #ifndef OPENSSL_NO_RSA
  1168. unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
  1169. #endif
  1170. EVP_MD_CTX md_ctx;
  1171. unsigned char *param, *p;
  1172. int al, j, ok;
  1173. long i, param_len, n, alg_k, alg_a;
  1174. EVP_PKEY *pkey = NULL;
  1175. const EVP_MD *md = NULL;
  1176. #ifndef OPENSSL_NO_RSA
  1177. RSA *rsa = NULL;
  1178. #endif
  1179. #ifndef OPENSSL_NO_DH
  1180. DH *dh = NULL;
  1181. #endif
  1182. #ifndef OPENSSL_NO_ECDH
  1183. EC_KEY *ecdh = NULL;
  1184. BN_CTX *bn_ctx = NULL;
  1185. EC_POINT *srvr_ecpoint = NULL;
  1186. int curve_nid = 0;
  1187. int encoded_pt_len = 0;
  1188. #endif
  1189. EVP_MD_CTX_init(&md_ctx);
  1190. /*
  1191. * use same message size as in ssl3_get_certificate_request() as
  1192. * ServerKeyExchange message may be skipped
  1193. */
  1194. n = s->method->ssl_get_message(s,
  1195. SSL3_ST_CR_KEY_EXCH_A,
  1196. SSL3_ST_CR_KEY_EXCH_B,
  1197. -1, s->max_cert_list, &ok);
  1198. if (!ok)
  1199. return ((int)n);
  1200. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1201. if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
  1202. /*
  1203. * Can't skip server key exchange if this is an ephemeral
  1204. * ciphersuite.
  1205. */
  1206. if (alg_k & (SSL_kEDH | SSL_kEECDH)) {
  1207. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
  1208. al = SSL_AD_UNEXPECTED_MESSAGE;
  1209. goto f_err;
  1210. }
  1211. #ifndef OPENSSL_NO_PSK
  1212. /*
  1213. * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no
  1214. * identity hint is sent. Set session->sess_cert anyway to avoid
  1215. * problems later.
  1216. */
  1217. if (alg_k & SSL_kPSK) {
  1218. s->session->sess_cert = ssl_sess_cert_new();
  1219. if (s->ctx->psk_identity_hint)
  1220. OPENSSL_free(s->ctx->psk_identity_hint);
  1221. s->ctx->psk_identity_hint = NULL;
  1222. }
  1223. #endif
  1224. s->s3->tmp.reuse_message = 1;
  1225. return (1);
  1226. }
  1227. param = p = (unsigned char *)s->init_msg;
  1228. if (s->session->sess_cert != NULL) {
  1229. #ifndef OPENSSL_NO_RSA
  1230. if (s->session->sess_cert->peer_rsa_tmp != NULL) {
  1231. RSA_free(s->session->sess_cert->peer_rsa_tmp);
  1232. s->session->sess_cert->peer_rsa_tmp = NULL;
  1233. }
  1234. #endif
  1235. #ifndef OPENSSL_NO_DH
  1236. if (s->session->sess_cert->peer_dh_tmp) {
  1237. DH_free(s->session->sess_cert->peer_dh_tmp);
  1238. s->session->sess_cert->peer_dh_tmp = NULL;
  1239. }
  1240. #endif
  1241. #ifndef OPENSSL_NO_ECDH
  1242. if (s->session->sess_cert->peer_ecdh_tmp) {
  1243. EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
  1244. s->session->sess_cert->peer_ecdh_tmp = NULL;
  1245. }
  1246. #endif
  1247. } else {
  1248. s->session->sess_cert = ssl_sess_cert_new();
  1249. }
  1250. /* Total length of the parameters including the length prefix */
  1251. param_len = 0;
  1252. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1253. al = SSL_AD_DECODE_ERROR;
  1254. #ifndef OPENSSL_NO_PSK
  1255. if (alg_k & SSL_kPSK) {
  1256. char tmp_id_hint[PSK_MAX_IDENTITY_LEN + 1];
  1257. param_len = 2;
  1258. if (param_len > n) {
  1259. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1260. goto f_err;
  1261. }
  1262. n2s(p, i);
  1263. /*
  1264. * Store PSK identity hint for later use, hint is used in
  1265. * ssl3_send_client_key_exchange. Assume that the maximum length of
  1266. * a PSK identity hint can be as long as the maximum length of a PSK
  1267. * identity.
  1268. */
  1269. if (i > PSK_MAX_IDENTITY_LEN) {
  1270. al = SSL_AD_HANDSHAKE_FAILURE;
  1271. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
  1272. goto f_err;
  1273. }
  1274. if (i > n - param_len) {
  1275. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1276. SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
  1277. goto f_err;
  1278. }
  1279. param_len += i;
  1280. /*
  1281. * If received PSK identity hint contains NULL characters, the hint
  1282. * is truncated from the first NULL. p may not be ending with NULL,
  1283. * so create a NULL-terminated string.
  1284. */
  1285. memcpy(tmp_id_hint, p, i);
  1286. memset(tmp_id_hint + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i);
  1287. if (s->ctx->psk_identity_hint != NULL)
  1288. OPENSSL_free(s->ctx->psk_identity_hint);
  1289. s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
  1290. if (s->ctx->psk_identity_hint == NULL) {
  1291. al = SSL_AD_HANDSHAKE_FAILURE;
  1292. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1293. goto f_err;
  1294. }
  1295. p += i;
  1296. n -= param_len;
  1297. } else
  1298. #endif /* !OPENSSL_NO_PSK */
  1299. #ifndef OPENSSL_NO_SRP
  1300. if (alg_k & SSL_kSRP) {
  1301. param_len = 2;
  1302. if (param_len > n) {
  1303. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1304. goto f_err;
  1305. }
  1306. n2s(p, i);
  1307. if (i > n - param_len) {
  1308. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
  1309. goto f_err;
  1310. }
  1311. param_len += i;
  1312. if (!(s->srp_ctx.N = BN_bin2bn(p, i, NULL))) {
  1313. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1314. goto err;
  1315. }
  1316. p += i;
  1317. if (2 > n - param_len) {
  1318. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1319. goto f_err;
  1320. }
  1321. param_len += 2;
  1322. n2s(p, i);
  1323. if (i > n - param_len) {
  1324. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
  1325. goto f_err;
  1326. }
  1327. param_len += i;
  1328. if (!(s->srp_ctx.g = BN_bin2bn(p, i, NULL))) {
  1329. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1330. goto err;
  1331. }
  1332. p += i;
  1333. if (1 > n - param_len) {
  1334. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1335. goto f_err;
  1336. }
  1337. param_len += 1;
  1338. i = (unsigned int)(p[0]);
  1339. p++;
  1340. if (i > n - param_len) {
  1341. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
  1342. goto f_err;
  1343. }
  1344. param_len += i;
  1345. if (!(s->srp_ctx.s = BN_bin2bn(p, i, NULL))) {
  1346. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1347. goto err;
  1348. }
  1349. p += i;
  1350. if (2 > n - param_len) {
  1351. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1352. goto f_err;
  1353. }
  1354. param_len += 2;
  1355. n2s(p, i);
  1356. if (i > n - param_len) {
  1357. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);
  1358. goto f_err;
  1359. }
  1360. param_len += i;
  1361. if (!(s->srp_ctx.B = BN_bin2bn(p, i, NULL))) {
  1362. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1363. goto err;
  1364. }
  1365. p += i;
  1366. n -= param_len;
  1367. if (!srp_verify_server_param(s, &al)) {
  1368. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
  1369. goto f_err;
  1370. }
  1371. /* We must check if there is a certificate */
  1372. # ifndef OPENSSL_NO_RSA
  1373. if (alg_a & SSL_aRSA)
  1374. pkey =
  1375. X509_get_pubkey(s->session->
  1376. sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1377. # else
  1378. if (0) ;
  1379. # endif
  1380. # ifndef OPENSSL_NO_DSA
  1381. else if (alg_a & SSL_aDSS)
  1382. pkey =
  1383. X509_get_pubkey(s->session->
  1384. sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
  1385. x509);
  1386. # endif
  1387. } else
  1388. #endif /* !OPENSSL_NO_SRP */
  1389. #ifndef OPENSSL_NO_RSA
  1390. if (alg_k & SSL_kRSA) {
  1391. /* Temporary RSA keys only allowed in export ciphersuites */
  1392. if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
  1393. al = SSL_AD_UNEXPECTED_MESSAGE;
  1394. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
  1395. goto f_err;
  1396. }
  1397. if ((rsa = RSA_new()) == NULL) {
  1398. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1399. goto err;
  1400. }
  1401. param_len = 2;
  1402. if (param_len > n) {
  1403. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1404. goto f_err;
  1405. }
  1406. n2s(p, i);
  1407. if (i > n - param_len) {
  1408. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
  1409. goto f_err;
  1410. }
  1411. param_len += i;
  1412. if (!(rsa->n = BN_bin2bn(p, i, rsa->n))) {
  1413. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1414. goto err;
  1415. }
  1416. p += i;
  1417. if (2 > n - param_len) {
  1418. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1419. goto f_err;
  1420. }
  1421. param_len += 2;
  1422. n2s(p, i);
  1423. if (i > n - param_len) {
  1424. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
  1425. goto f_err;
  1426. }
  1427. param_len += i;
  1428. if (!(rsa->e = BN_bin2bn(p, i, rsa->e))) {
  1429. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1430. goto err;
  1431. }
  1432. p += i;
  1433. n -= param_len;
  1434. /* this should be because we are using an export cipher */
  1435. if (alg_a & SSL_aRSA)
  1436. pkey =
  1437. X509_get_pubkey(s->session->
  1438. sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1439. else {
  1440. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1441. goto err;
  1442. }
  1443. if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
  1444. al = SSL_AD_UNEXPECTED_MESSAGE;
  1445. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
  1446. goto f_err;
  1447. }
  1448. s->session->sess_cert->peer_rsa_tmp = rsa;
  1449. rsa = NULL;
  1450. }
  1451. #else /* OPENSSL_NO_RSA */
  1452. if (0) ;
  1453. #endif
  1454. #ifndef OPENSSL_NO_DH
  1455. else if (alg_k & SSL_kEDH) {
  1456. if ((dh = DH_new()) == NULL) {
  1457. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
  1458. goto err;
  1459. }
  1460. param_len = 2;
  1461. if (param_len > n) {
  1462. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1463. goto f_err;
  1464. }
  1465. n2s(p, i);
  1466. if (i > n - param_len) {
  1467. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
  1468. goto f_err;
  1469. }
  1470. param_len += i;
  1471. if (!(dh->p = BN_bin2bn(p, i, NULL))) {
  1472. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1473. goto err;
  1474. }
  1475. p += i;
  1476. if (2 > n - param_len) {
  1477. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1478. goto f_err;
  1479. }
  1480. param_len += 2;
  1481. n2s(p, i);
  1482. if (i > n - param_len) {
  1483. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
  1484. goto f_err;
  1485. }
  1486. param_len += i;
  1487. if (!(dh->g = BN_bin2bn(p, i, NULL))) {
  1488. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1489. goto err;
  1490. }
  1491. p += i;
  1492. if (2 > n - param_len) {
  1493. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1494. goto f_err;
  1495. }
  1496. param_len += 2;
  1497. n2s(p, i);
  1498. if (i > n - param_len) {
  1499. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
  1500. goto f_err;
  1501. }
  1502. param_len += i;
  1503. if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) {
  1504. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1505. goto err;
  1506. }
  1507. p += i;
  1508. n -= param_len;
  1509. # ifndef OPENSSL_NO_RSA
  1510. if (alg_a & SSL_aRSA)
  1511. pkey =
  1512. X509_get_pubkey(s->session->
  1513. sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1514. # else
  1515. if (0) ;
  1516. # endif
  1517. # ifndef OPENSSL_NO_DSA
  1518. else if (alg_a & SSL_aDSS)
  1519. pkey =
  1520. X509_get_pubkey(s->session->
  1521. sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
  1522. x509);
  1523. # endif
  1524. /* else anonymous DH, so no certificate or pkey. */
  1525. s->session->sess_cert->peer_dh_tmp = dh;
  1526. dh = NULL;
  1527. } else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd)) {
  1528. al = SSL_AD_ILLEGAL_PARAMETER;
  1529. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1530. SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
  1531. goto f_err;
  1532. }
  1533. #endif /* !OPENSSL_NO_DH */
  1534. #ifndef OPENSSL_NO_ECDH
  1535. else if (alg_k & SSL_kEECDH) {
  1536. EC_GROUP *ngroup;
  1537. const EC_GROUP *group;
  1538. if ((ecdh = EC_KEY_new()) == NULL) {
  1539. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1540. goto err;
  1541. }
  1542. /*
  1543. * Extract elliptic curve parameters and the server's ephemeral ECDH
  1544. * public key. Keep accumulating lengths of various components in
  1545. * param_len and make sure it never exceeds n.
  1546. */
  1547. /*
  1548. * XXX: For now we only support named (not generic) curves and the
  1549. * ECParameters in this case is just three bytes. We also need one
  1550. * byte for the length of the encoded point
  1551. */
  1552. param_len = 4;
  1553. if (param_len > n) {
  1554. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1555. goto f_err;
  1556. }
  1557. if ((*p != NAMED_CURVE_TYPE) ||
  1558. ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0)) {
  1559. al = SSL_AD_INTERNAL_ERROR;
  1560. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1561. SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  1562. goto f_err;
  1563. }
  1564. ngroup = EC_GROUP_new_by_curve_name(curve_nid);
  1565. if (ngroup == NULL) {
  1566. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
  1567. goto err;
  1568. }
  1569. if (EC_KEY_set_group(ecdh, ngroup) == 0) {
  1570. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
  1571. goto err;
  1572. }
  1573. EC_GROUP_free(ngroup);
  1574. group = EC_KEY_get0_group(ecdh);
  1575. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  1576. (EC_GROUP_get_degree(group) > 163)) {
  1577. al = SSL_AD_EXPORT_RESTRICTION;
  1578. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1579. SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
  1580. goto f_err;
  1581. }
  1582. p += 3;
  1583. /* Next, get the encoded ECPoint */
  1584. if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
  1585. ((bn_ctx = BN_CTX_new()) == NULL)) {
  1586. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1587. goto err;
  1588. }
  1589. encoded_pt_len = *p; /* length of encoded point */
  1590. p += 1;
  1591. if ((encoded_pt_len > n - param_len) ||
  1592. (EC_POINT_oct2point(group, srvr_ecpoint,
  1593. p, encoded_pt_len, bn_ctx) == 0)) {
  1594. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
  1595. goto f_err;
  1596. }
  1597. param_len += encoded_pt_len;
  1598. n -= param_len;
  1599. p += encoded_pt_len;
  1600. /*
  1601. * The ECC/TLS specification does not mention the use of DSA to sign
  1602. * ECParameters in the server key exchange message. We do support RSA
  1603. * and ECDSA.
  1604. */
  1605. if (0) ;
  1606. # ifndef OPENSSL_NO_RSA
  1607. else if (alg_a & SSL_aRSA)
  1608. pkey =
  1609. X509_get_pubkey(s->session->
  1610. sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1611. # endif
  1612. # ifndef OPENSSL_NO_ECDSA
  1613. else if (alg_a & SSL_aECDSA)
  1614. pkey =
  1615. X509_get_pubkey(s->session->
  1616. sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  1617. # endif
  1618. /* else anonymous ECDH, so no certificate or pkey. */
  1619. EC_KEY_set_public_key(ecdh, srvr_ecpoint);
  1620. s->session->sess_cert->peer_ecdh_tmp = ecdh;
  1621. ecdh = NULL;
  1622. BN_CTX_free(bn_ctx);
  1623. bn_ctx = NULL;
  1624. EC_POINT_free(srvr_ecpoint);
  1625. srvr_ecpoint = NULL;
  1626. } else if (alg_k) {
  1627. al = SSL_AD_UNEXPECTED_MESSAGE;
  1628. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
  1629. goto f_err;
  1630. }
  1631. #endif /* !OPENSSL_NO_ECDH */
  1632. /* p points to the next byte, there are 'n' bytes left */
  1633. /* if it was signed, check the signature */
  1634. if (pkey != NULL) {
  1635. if (TLS1_get_version(s) >= TLS1_2_VERSION) {
  1636. int sigalg;
  1637. if (2 > n) {
  1638. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1639. goto f_err;
  1640. }
  1641. sigalg = tls12_get_sigid(pkey);
  1642. /* Should never happen */
  1643. if (sigalg == -1) {
  1644. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1645. goto err;
  1646. }
  1647. /* Check key type is consistent with signature */
  1648. if (sigalg != (int)p[1]) {
  1649. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1650. SSL_R_WRONG_SIGNATURE_TYPE);
  1651. al = SSL_AD_DECODE_ERROR;
  1652. goto f_err;
  1653. }
  1654. md = tls12_get_hash(p[0]);
  1655. if (md == NULL) {
  1656. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNKNOWN_DIGEST);
  1657. goto f_err;
  1658. }
  1659. #ifdef SSL_DEBUG
  1660. fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
  1661. #endif
  1662. p += 2;
  1663. n -= 2;
  1664. } else
  1665. md = EVP_sha1();
  1666. if (2 > n) {
  1667. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1668. goto f_err;
  1669. }
  1670. n2s(p, i);
  1671. n -= 2;
  1672. j = EVP_PKEY_size(pkey);
  1673. /*
  1674. * Check signature length. If n is 0 then signature is empty
  1675. */
  1676. if ((i != n) || (n > j) || (n <= 0)) {
  1677. /* wrong packet length */
  1678. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
  1679. goto f_err;
  1680. }
  1681. #ifndef OPENSSL_NO_RSA
  1682. if (pkey->type == EVP_PKEY_RSA
  1683. && TLS1_get_version(s) < TLS1_2_VERSION) {
  1684. int num;
  1685. unsigned int size;
  1686. j = 0;
  1687. q = md_buf;
  1688. for (num = 2; num > 0; num--) {
  1689. EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  1690. EVP_DigestInit_ex(&md_ctx, (num == 2)
  1691. ? s->ctx->md5 : s->ctx->sha1, NULL);
  1692. EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
  1693. SSL3_RANDOM_SIZE);
  1694. EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
  1695. SSL3_RANDOM_SIZE);
  1696. EVP_DigestUpdate(&md_ctx, param, param_len);
  1697. EVP_DigestFinal_ex(&md_ctx, q, &size);
  1698. q += size;
  1699. j += size;
  1700. }
  1701. i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa);
  1702. if (i < 0) {
  1703. al = SSL_AD_DECRYPT_ERROR;
  1704. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
  1705. goto f_err;
  1706. }
  1707. if (i == 0) {
  1708. /* bad signature */
  1709. al = SSL_AD_DECRYPT_ERROR;
  1710. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
  1711. goto f_err;
  1712. }
  1713. } else
  1714. #endif
  1715. {
  1716. EVP_VerifyInit_ex(&md_ctx, md, NULL);
  1717. EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
  1718. SSL3_RANDOM_SIZE);
  1719. EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
  1720. SSL3_RANDOM_SIZE);
  1721. EVP_VerifyUpdate(&md_ctx, param, param_len);
  1722. if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) {
  1723. /* bad signature */
  1724. al = SSL_AD_DECRYPT_ERROR;
  1725. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
  1726. goto f_err;
  1727. }
  1728. }
  1729. } else {
  1730. /* aNULL, aSRP or kPSK do not need public keys */
  1731. if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) {
  1732. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1733. goto err;
  1734. }
  1735. /* still data left over */
  1736. if (n != 0) {
  1737. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
  1738. goto f_err;
  1739. }
  1740. }
  1741. EVP_PKEY_free(pkey);
  1742. EVP_MD_CTX_cleanup(&md_ctx);
  1743. return (1);
  1744. f_err:
  1745. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1746. err:
  1747. EVP_PKEY_free(pkey);
  1748. #ifndef OPENSSL_NO_RSA
  1749. if (rsa != NULL)
  1750. RSA_free(rsa);
  1751. #endif
  1752. #ifndef OPENSSL_NO_DH
  1753. if (dh != NULL)
  1754. DH_free(dh);
  1755. #endif
  1756. #ifndef OPENSSL_NO_ECDH
  1757. BN_CTX_free(bn_ctx);
  1758. EC_POINT_free(srvr_ecpoint);
  1759. if (ecdh != NULL)
  1760. EC_KEY_free(ecdh);
  1761. #endif
  1762. EVP_MD_CTX_cleanup(&md_ctx);
  1763. s->state = SSL_ST_ERR;
  1764. return (-1);
  1765. }
  1766. int ssl3_get_certificate_request(SSL *s)
  1767. {
  1768. int ok, ret = 0;
  1769. unsigned long n, nc, l;
  1770. unsigned int llen, ctype_num, i;
  1771. X509_NAME *xn = NULL;
  1772. const unsigned char *p, *q;
  1773. unsigned char *d;
  1774. STACK_OF(X509_NAME) *ca_sk = NULL;
  1775. n = s->method->ssl_get_message(s,
  1776. SSL3_ST_CR_CERT_REQ_A,
  1777. SSL3_ST_CR_CERT_REQ_B,
  1778. -1, s->max_cert_list, &ok);
  1779. if (!ok)
  1780. return ((int)n);
  1781. s->s3->tmp.cert_req = 0;
  1782. if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
  1783. s->s3->tmp.reuse_message = 1;
  1784. /*
  1785. * If we get here we don't need any cached handshake records as we
  1786. * wont be doing client auth.
  1787. */
  1788. if (s->s3->handshake_buffer) {
  1789. if (!ssl3_digest_cached_records(s))
  1790. goto err;
  1791. }
  1792. return (1);
  1793. }
  1794. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
  1795. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1796. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
  1797. goto err;
  1798. }
  1799. /* TLS does not like anon-DH with client cert */
  1800. if (s->version > SSL3_VERSION) {
  1801. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
  1802. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1803. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
  1804. SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
  1805. goto err;
  1806. }
  1807. }
  1808. p = d = (unsigned char *)s->init_msg;
  1809. if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
  1810. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
  1811. goto err;
  1812. }
  1813. /* get the certificate types */
  1814. ctype_num = *(p++);
  1815. if (ctype_num > SSL3_CT_NUMBER)
  1816. ctype_num = SSL3_CT_NUMBER;
  1817. for (i = 0; i < ctype_num; i++)
  1818. s->s3->tmp.ctype[i] = p[i];
  1819. p += ctype_num;
  1820. if (TLS1_get_version(s) >= TLS1_2_VERSION) {
  1821. n2s(p, llen);
  1822. /*
  1823. * Check we have enough room for signature algorithms and following
  1824. * length value.
  1825. */
  1826. if ((unsigned long)(p - d + llen + 2) > n) {
  1827. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1828. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
  1829. SSL_R_DATA_LENGTH_TOO_LONG);
  1830. goto err;
  1831. }
  1832. if ((llen & 1) || !tls1_process_sigalgs(s, p, llen)) {
  1833. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1834. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
  1835. SSL_R_SIGNATURE_ALGORITHMS_ERROR);
  1836. goto err;
  1837. }
  1838. p += llen;
  1839. }
  1840. /* get the CA RDNs */
  1841. n2s(p, llen);
  1842. #if 0
  1843. {
  1844. FILE *out;
  1845. out = fopen("/tmp/vsign.der", "w");
  1846. fwrite(p, 1, llen, out);
  1847. fclose(out);
  1848. }
  1849. #endif
  1850. if ((unsigned long)(p - d + llen) != n) {
  1851. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1852. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
  1853. goto err;
  1854. }
  1855. for (nc = 0; nc < llen;) {
  1856. n2s(p, l);
  1857. if ((l + nc + 2) > llen) {
  1858. if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  1859. goto cont; /* netscape bugs */
  1860. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1861. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
  1862. goto err;
  1863. }
  1864. q = p;
  1865. if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) {
  1866. /* If netscape tolerance is on, ignore errors */
  1867. if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
  1868. goto cont;
  1869. else {
  1870. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1871. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
  1872. goto err;
  1873. }
  1874. }
  1875. if (q != (p + l)) {
  1876. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1877. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
  1878. SSL_R_CA_DN_LENGTH_MISMATCH);
  1879. goto err;
  1880. }
  1881. if (!sk_X509_NAME_push(ca_sk, xn)) {
  1882. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
  1883. goto err;
  1884. }
  1885. p += l;
  1886. nc += l + 2;
  1887. }
  1888. if (0) {
  1889. cont:
  1890. ERR_clear_error();
  1891. }
  1892. /* we should setup a certificate to return.... */
  1893. s->s3->tmp.cert_req = 1;
  1894. s->s3->tmp.ctype_num = ctype_num;
  1895. if (s->s3->tmp.ca_names != NULL)
  1896. sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
  1897. s->s3->tmp.ca_names = ca_sk;
  1898. ca_sk = NULL;
  1899. ret = 1;
  1900. goto done;
  1901. err:
  1902. s->state = SSL_ST_ERR;
  1903. done:
  1904. if (ca_sk != NULL)
  1905. sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
  1906. return (ret);
  1907. }
  1908. static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
  1909. {
  1910. return (X509_NAME_cmp(*a, *b));
  1911. }
  1912. #ifndef OPENSSL_NO_TLSEXT
  1913. int ssl3_get_new_session_ticket(SSL *s)
  1914. {
  1915. int ok, al, ret = 0, ticklen;
  1916. long n;
  1917. const unsigned char *p;
  1918. unsigned char *d;
  1919. n = s->method->ssl_get_message(s,
  1920. SSL3_ST_CR_SESSION_TICKET_A,
  1921. SSL3_ST_CR_SESSION_TICKET_B,
  1922. SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
  1923. if (!ok)
  1924. return ((int)n);
  1925. if (n < 6) {
  1926. /* need at least ticket_lifetime_hint + ticket length */
  1927. al = SSL_AD_DECODE_ERROR;
  1928. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
  1929. goto f_err;
  1930. }
  1931. p = d = (unsigned char *)s->init_msg;
  1932. if (s->session->session_id_length > 0) {
  1933. int i = s->session_ctx->session_cache_mode;
  1934. SSL_SESSION *new_sess;
  1935. /*
  1936. * We reused an existing session, so we need to replace it with a new
  1937. * one
  1938. */
  1939. if (i & SSL_SESS_CACHE_CLIENT) {
  1940. /*
  1941. * Remove the old session from the cache
  1942. */
  1943. if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
  1944. if (s->session_ctx->remove_session_cb != NULL)
  1945. s->session_ctx->remove_session_cb(s->session_ctx,
  1946. s->session);
  1947. } else {
  1948. /* We carry on if this fails */
  1949. SSL_CTX_remove_session(s->session_ctx, s->session);
  1950. }
  1951. }
  1952. if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
  1953. al = SSL_AD_INTERNAL_ERROR;
  1954. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
  1955. goto f_err;
  1956. }
  1957. SSL_SESSION_free(s->session);
  1958. s->session = new_sess;
  1959. }
  1960. n2l(p, s->session->tlsext_tick_lifetime_hint);
  1961. n2s(p, ticklen);
  1962. /* ticket_lifetime_hint + ticket_length + ticket */
  1963. if (ticklen + 6 != n) {
  1964. al = SSL_AD_DECODE_ERROR;
  1965. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
  1966. goto f_err;
  1967. }
  1968. if (s->session->tlsext_tick) {
  1969. OPENSSL_free(s->session->tlsext_tick);
  1970. s->session->tlsext_ticklen = 0;
  1971. }
  1972. s->session->tlsext_tick = OPENSSL_malloc(ticklen);
  1973. if (!s->session->tlsext_tick) {
  1974. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
  1975. goto err;
  1976. }
  1977. memcpy(s->session->tlsext_tick, p, ticklen);
  1978. s->session->tlsext_ticklen = ticklen;
  1979. /*
  1980. * There are two ways to detect a resumed ticket session. One is to set
  1981. * an appropriate session ID and then the server must return a match in
  1982. * ServerHello. This allows the normal client session ID matching to work
  1983. * and we know much earlier that the ticket has been accepted. The
  1984. * other way is to set zero length session ID when the ticket is
  1985. * presented and rely on the handshake to determine session resumption.
  1986. * We choose the former approach because this fits in with assumptions
  1987. * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
  1988. * SHA256 is disabled) hash of the ticket.
  1989. */
  1990. EVP_Digest(p, ticklen,
  1991. s->session->session_id, &s->session->session_id_length,
  1992. # ifndef OPENSSL_NO_SHA256
  1993. EVP_sha256(), NULL);
  1994. # else
  1995. EVP_sha1(), NULL);
  1996. # endif
  1997. ret = 1;
  1998. return (ret);
  1999. f_err:
  2000. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  2001. err:
  2002. s->state = SSL_ST_ERR;
  2003. return (-1);
  2004. }
  2005. int ssl3_get_cert_status(SSL *s)
  2006. {
  2007. int ok, al;
  2008. unsigned long resplen, n;
  2009. const unsigned char *p;
  2010. n = s->method->ssl_get_message(s,
  2011. SSL3_ST_CR_CERT_STATUS_A,
  2012. SSL3_ST_CR_CERT_STATUS_B,
  2013. SSL3_MT_CERTIFICATE_STATUS, 16384, &ok);
  2014. if (!ok)
  2015. return ((int)n);
  2016. if (n < 4) {
  2017. /* need at least status type + length */
  2018. al = SSL_AD_DECODE_ERROR;
  2019. SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
  2020. goto f_err;
  2021. }
  2022. p = (unsigned char *)s->init_msg;
  2023. if (*p++ != TLSEXT_STATUSTYPE_ocsp) {
  2024. al = SSL_AD_DECODE_ERROR;
  2025. SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
  2026. goto f_err;
  2027. }
  2028. n2l3(p, resplen);
  2029. if (resplen + 4 != n) {
  2030. al = SSL_AD_DECODE_ERROR;
  2031. SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
  2032. goto f_err;
  2033. }
  2034. if (s->tlsext_ocsp_resp)
  2035. OPENSSL_free(s->tlsext_ocsp_resp);
  2036. s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
  2037. if (!s->tlsext_ocsp_resp) {
  2038. al = SSL_AD_INTERNAL_ERROR;
  2039. SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
  2040. goto f_err;
  2041. }
  2042. s->tlsext_ocsp_resplen = resplen;
  2043. if (s->ctx->tlsext_status_cb) {
  2044. int ret;
  2045. ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
  2046. if (ret == 0) {
  2047. al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
  2048. SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
  2049. goto f_err;
  2050. }
  2051. if (ret < 0) {
  2052. al = SSL_AD_INTERNAL_ERROR;
  2053. SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
  2054. goto f_err;
  2055. }
  2056. }
  2057. return 1;
  2058. f_err:
  2059. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  2060. s->state = SSL_ST_ERR;
  2061. return (-1);
  2062. }
  2063. #endif
  2064. int ssl3_get_server_done(SSL *s)
  2065. {
  2066. int ok, ret = 0;
  2067. long n;
  2068. /* Second to last param should be very small, like 0 :-) */
  2069. n = s->method->ssl_get_message(s,
  2070. SSL3_ST_CR_SRVR_DONE_A,
  2071. SSL3_ST_CR_SRVR_DONE_B,
  2072. SSL3_MT_SERVER_DONE, 30, &ok);
  2073. if (!ok)
  2074. return ((int)n);
  2075. if (n > 0) {
  2076. /* should contain no data */
  2077. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  2078. SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
  2079. s->state = SSL_ST_ERR;
  2080. return -1;
  2081. }
  2082. ret = 1;
  2083. return (ret);
  2084. }
  2085. int ssl3_send_client_key_exchange(SSL *s)
  2086. {
  2087. unsigned char *p, *d;
  2088. int n;
  2089. unsigned long alg_k;
  2090. #ifndef OPENSSL_NO_RSA
  2091. unsigned char *q;
  2092. EVP_PKEY *pkey = NULL;
  2093. #endif
  2094. #ifndef OPENSSL_NO_KRB5
  2095. KSSL_ERR kssl_err;
  2096. #endif /* OPENSSL_NO_KRB5 */
  2097. #ifndef OPENSSL_NO_ECDH
  2098. EC_KEY *clnt_ecdh = NULL;
  2099. const EC_POINT *srvr_ecpoint = NULL;
  2100. EVP_PKEY *srvr_pub_pkey = NULL;
  2101. unsigned char *encodedPoint = NULL;
  2102. int encoded_pt_len = 0;
  2103. BN_CTX *bn_ctx = NULL;
  2104. #endif
  2105. if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
  2106. d = (unsigned char *)s->init_buf->data;
  2107. p = &(d[4]);
  2108. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  2109. /* Fool emacs indentation */
  2110. if (0) {
  2111. }
  2112. #ifndef OPENSSL_NO_RSA
  2113. else if (alg_k & SSL_kRSA) {
  2114. RSA *rsa;
  2115. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  2116. if (s->session->sess_cert == NULL) {
  2117. /*
  2118. * We should always have a server certificate with SSL_kRSA.
  2119. */
  2120. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2121. ERR_R_INTERNAL_ERROR);
  2122. goto err;
  2123. }
  2124. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  2125. rsa = s->session->sess_cert->peer_rsa_tmp;
  2126. else {
  2127. pkey =
  2128. X509_get_pubkey(s->session->
  2129. sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].
  2130. x509);
  2131. if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
  2132. || (pkey->pkey.rsa == NULL)) {
  2133. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2134. ERR_R_INTERNAL_ERROR);
  2135. goto err;
  2136. }
  2137. rsa = pkey->pkey.rsa;
  2138. EVP_PKEY_free(pkey);
  2139. }
  2140. tmp_buf[0] = s->client_version >> 8;
  2141. tmp_buf[1] = s->client_version & 0xff;
  2142. if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
  2143. goto err;
  2144. s->session->master_key_length = sizeof tmp_buf;
  2145. q = p;
  2146. /* Fix buf for TLS and beyond */
  2147. if (s->version > SSL3_VERSION)
  2148. p += 2;
  2149. n = RSA_public_encrypt(sizeof tmp_buf,
  2150. tmp_buf, p, rsa, RSA_PKCS1_PADDING);
  2151. # ifdef PKCS1_CHECK
  2152. if (s->options & SSL_OP_PKCS1_CHECK_1)
  2153. p[1]++;
  2154. if (s->options & SSL_OP_PKCS1_CHECK_2)
  2155. tmp_buf[0] = 0x70;
  2156. # endif
  2157. if (n <= 0) {
  2158. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2159. SSL_R_BAD_RSA_ENCRYPT);
  2160. goto err;
  2161. }
  2162. /* Fix buf for TLS and beyond */
  2163. if (s->version > SSL3_VERSION) {
  2164. s2n(n, q);
  2165. n += 2;
  2166. }
  2167. s->session->master_key_length =
  2168. s->method->ssl3_enc->generate_master_secret(s,
  2169. s->
  2170. session->master_key,
  2171. tmp_buf,
  2172. sizeof tmp_buf);
  2173. OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
  2174. }
  2175. #endif
  2176. #ifndef OPENSSL_NO_KRB5
  2177. else if (alg_k & SSL_kKRB5) {
  2178. krb5_error_code krb5rc;
  2179. KSSL_CTX *kssl_ctx = s->kssl_ctx;
  2180. /* krb5_data krb5_ap_req; */
  2181. krb5_data *enc_ticket;
  2182. krb5_data authenticator, *authp = NULL;
  2183. EVP_CIPHER_CTX ciph_ctx;
  2184. const EVP_CIPHER *enc = NULL;
  2185. unsigned char iv[EVP_MAX_IV_LENGTH];
  2186. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  2187. unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH];
  2188. int padl, outl = sizeof(epms);
  2189. EVP_CIPHER_CTX_init(&ciph_ctx);
  2190. # ifdef KSSL_DEBUG
  2191. fprintf(stderr, "ssl3_send_client_key_exchange(%lx & %lx)\n",
  2192. alg_k, SSL_kKRB5);
  2193. # endif /* KSSL_DEBUG */
  2194. authp = NULL;
  2195. # ifdef KRB5SENDAUTH
  2196. if (KRB5SENDAUTH)
  2197. authp = &authenticator;
  2198. # endif /* KRB5SENDAUTH */
  2199. krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err);
  2200. enc = kssl_map_enc(kssl_ctx->enctype);
  2201. if (enc == NULL)
  2202. goto err;
  2203. # ifdef KSSL_DEBUG
  2204. {
  2205. fprintf(stderr, "kssl_cget_tkt rtn %d\n", krb5rc);
  2206. if (krb5rc && kssl_err.text)
  2207. fprintf(stderr, "kssl_cget_tkt kssl_err=%s\n",
  2208. kssl_err.text);
  2209. }
  2210. # endif /* KSSL_DEBUG */
  2211. if (krb5rc) {
  2212. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  2213. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason);
  2214. goto err;
  2215. }
  2216. /*-
  2217. * 20010406 VRS - Earlier versions used KRB5 AP_REQ
  2218. * in place of RFC 2712 KerberosWrapper, as in:
  2219. *
  2220. * Send ticket (copy to *p, set n = length)
  2221. * n = krb5_ap_req.length;
  2222. * memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
  2223. * if (krb5_ap_req.data)
  2224. * kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
  2225. *
  2226. * Now using real RFC 2712 KerberosWrapper
  2227. * (Thanks to Simon Wilkinson <[email protected]>)
  2228. * Note: 2712 "opaque" types are here replaced
  2229. * with a 2-byte length followed by the value.
  2230. * Example:
  2231. * KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
  2232. * Where "xx xx" = length bytes. Shown here with
  2233. * optional authenticator omitted.
  2234. */
  2235. /* KerberosWrapper.Ticket */
  2236. s2n(enc_ticket->length, p);
  2237. memcpy(p, enc_ticket->data, enc_ticket->length);
  2238. p += enc_ticket->length;
  2239. n = enc_ticket->length + 2;
  2240. /* KerberosWrapper.Authenticator */
  2241. if (authp && authp->length) {
  2242. s2n(authp->length, p);
  2243. memcpy(p, authp->data, authp->length);
  2244. p += authp->length;
  2245. n += authp->length + 2;
  2246. free(authp->data);
  2247. authp->data = NULL;
  2248. authp->length = 0;
  2249. } else {
  2250. s2n(0, p); /* null authenticator length */
  2251. n += 2;
  2252. }
  2253. tmp_buf[0] = s->client_version >> 8;
  2254. tmp_buf[1] = s->client_version & 0xff;
  2255. if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
  2256. goto err;
  2257. /*-
  2258. * 20010420 VRS. Tried it this way; failed.
  2259. * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
  2260. * EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
  2261. * kssl_ctx->length);
  2262. * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
  2263. */
  2264. memset(iv, 0, sizeof iv); /* per RFC 1510 */
  2265. EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv);
  2266. EVP_EncryptUpdate(&ciph_ctx, epms, &outl, tmp_buf,
  2267. sizeof tmp_buf);
  2268. EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl);
  2269. outl += padl;
  2270. if (outl > (int)sizeof epms) {
  2271. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2272. ERR_R_INTERNAL_ERROR);
  2273. goto err;
  2274. }
  2275. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  2276. /* KerberosWrapper.EncryptedPreMasterSecret */
  2277. s2n(outl, p);
  2278. memcpy(p, epms, outl);
  2279. p += outl;
  2280. n += outl + 2;
  2281. s->session->master_key_length =
  2282. s->method->ssl3_enc->generate_master_secret(s,
  2283. s->
  2284. session->master_key,
  2285. tmp_buf,
  2286. sizeof tmp_buf);
  2287. OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
  2288. OPENSSL_cleanse(epms, outl);
  2289. }
  2290. #endif
  2291. #ifndef OPENSSL_NO_DH
  2292. else if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
  2293. DH *dh_srvr, *dh_clnt;
  2294. if (s->session->sess_cert == NULL) {
  2295. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  2296. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2297. SSL_R_UNEXPECTED_MESSAGE);
  2298. goto err;
  2299. }
  2300. if (s->session->sess_cert->peer_dh_tmp != NULL)
  2301. dh_srvr = s->session->sess_cert->peer_dh_tmp;
  2302. else {
  2303. /* we get them from the cert */
  2304. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  2305. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2306. SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
  2307. goto err;
  2308. }
  2309. /* generate a new random key */
  2310. if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
  2311. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
  2312. goto err;
  2313. }
  2314. if (!DH_generate_key(dh_clnt)) {
  2315. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
  2316. DH_free(dh_clnt);
  2317. goto err;
  2318. }
  2319. /*
  2320. * use the 'p' output buffer for the DH key, but make sure to
  2321. * clear it out afterwards
  2322. */
  2323. n = DH_compute_key(p, dh_srvr->pub_key, dh_clnt);
  2324. if (n <= 0) {
  2325. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
  2326. DH_free(dh_clnt);
  2327. goto err;
  2328. }
  2329. /* generate master key from the result */
  2330. s->session->master_key_length =
  2331. s->method->ssl3_enc->generate_master_secret(s,
  2332. s->
  2333. session->master_key,
  2334. p, n);
  2335. /* clean up */
  2336. memset(p, 0, n);
  2337. /* send off the data */
  2338. n = BN_num_bytes(dh_clnt->pub_key);
  2339. s2n(n, p);
  2340. BN_bn2bin(dh_clnt->pub_key, p);
  2341. n += 2;
  2342. DH_free(dh_clnt);
  2343. }
  2344. #endif
  2345. #ifndef OPENSSL_NO_ECDH
  2346. else if (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)) {
  2347. const EC_GROUP *srvr_group = NULL;
  2348. EC_KEY *tkey;
  2349. int ecdh_clnt_cert = 0;
  2350. int field_size = 0;
  2351. if (s->session->sess_cert == NULL) {
  2352. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  2353. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2354. SSL_R_UNEXPECTED_MESSAGE);
  2355. goto err;
  2356. }
  2357. /*
  2358. * Did we send out the client's ECDH share for use in premaster
  2359. * computation as part of client certificate? If so, set
  2360. * ecdh_clnt_cert to 1.
  2361. */
  2362. if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
  2363. /*-
  2364. * XXX: For now, we do not support client
  2365. * authentication using ECDH certificates.
  2366. * To add such support, one needs to add
  2367. * code that checks for appropriate
  2368. * conditions and sets ecdh_clnt_cert to 1.
  2369. * For example, the cert have an ECC
  2370. * key on the same curve as the server's
  2371. * and the key should be authorized for
  2372. * key agreement.
  2373. *
  2374. * One also needs to add code in ssl3_connect
  2375. * to skip sending the certificate verify
  2376. * message.
  2377. *
  2378. * if ((s->cert->key->privatekey != NULL) &&
  2379. * (s->cert->key->privatekey->type ==
  2380. * EVP_PKEY_EC) && ...)
  2381. * ecdh_clnt_cert = 1;
  2382. */
  2383. }
  2384. if (s->session->sess_cert->peer_ecdh_tmp != NULL) {
  2385. tkey = s->session->sess_cert->peer_ecdh_tmp;
  2386. } else {
  2387. /* Get the Server Public Key from Cert */
  2388. srvr_pub_pkey =
  2389. X509_get_pubkey(s->session->
  2390. sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  2391. if ((srvr_pub_pkey == NULL)
  2392. || (srvr_pub_pkey->type != EVP_PKEY_EC)
  2393. || (srvr_pub_pkey->pkey.ec == NULL)) {
  2394. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2395. ERR_R_INTERNAL_ERROR);
  2396. goto err;
  2397. }
  2398. tkey = srvr_pub_pkey->pkey.ec;
  2399. }
  2400. srvr_group = EC_KEY_get0_group(tkey);
  2401. srvr_ecpoint = EC_KEY_get0_public_key(tkey);
  2402. if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
  2403. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2404. ERR_R_INTERNAL_ERROR);
  2405. goto err;
  2406. }
  2407. if ((clnt_ecdh = EC_KEY_new()) == NULL) {
  2408. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2409. ERR_R_MALLOC_FAILURE);
  2410. goto err;
  2411. }
  2412. if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
  2413. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
  2414. goto err;
  2415. }
  2416. if (ecdh_clnt_cert) {
  2417. /*
  2418. * Reuse key info from our certificate We only need our
  2419. * private key to perform the ECDH computation.
  2420. */
  2421. const BIGNUM *priv_key;
  2422. tkey = s->cert->key->privatekey->pkey.ec;
  2423. priv_key = EC_KEY_get0_private_key(tkey);
  2424. if (priv_key == NULL) {
  2425. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2426. ERR_R_MALLOC_FAILURE);
  2427. goto err;
  2428. }
  2429. if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
  2430. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
  2431. goto err;
  2432. }
  2433. } else {
  2434. /* Generate a new ECDH key pair */
  2435. if (!(EC_KEY_generate_key(clnt_ecdh))) {
  2436. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2437. ERR_R_ECDH_LIB);
  2438. goto err;
  2439. }
  2440. }
  2441. /*
  2442. * use the 'p' output buffer for the ECDH key, but make sure to
  2443. * clear it out afterwards
  2444. */
  2445. field_size = EC_GROUP_get_degree(srvr_group);
  2446. if (field_size <= 0) {
  2447. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  2448. goto err;
  2449. }
  2450. n = ECDH_compute_key(p, (field_size + 7) / 8, srvr_ecpoint,
  2451. clnt_ecdh, NULL);
  2452. if (n <= 0) {
  2453. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  2454. goto err;
  2455. }
  2456. /* generate master key from the result */
  2457. s->session->master_key_length =
  2458. s->method->ssl3_enc->generate_master_secret(s,
  2459. s->
  2460. session->master_key,
  2461. p, n);
  2462. memset(p, 0, n); /* clean up */
  2463. if (ecdh_clnt_cert) {
  2464. /* Send empty client key exch message */
  2465. n = 0;
  2466. } else {
  2467. /*
  2468. * First check the size of encoding and allocate memory
  2469. * accordingly.
  2470. */
  2471. encoded_pt_len =
  2472. EC_POINT_point2oct(srvr_group,
  2473. EC_KEY_get0_public_key(clnt_ecdh),
  2474. POINT_CONVERSION_UNCOMPRESSED,
  2475. NULL, 0, NULL);
  2476. encodedPoint = (unsigned char *)
  2477. OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
  2478. bn_ctx = BN_CTX_new();
  2479. if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
  2480. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2481. ERR_R_MALLOC_FAILURE);
  2482. goto err;
  2483. }
  2484. /* Encode the public key */
  2485. n = EC_POINT_point2oct(srvr_group,
  2486. EC_KEY_get0_public_key(clnt_ecdh),
  2487. POINT_CONVERSION_UNCOMPRESSED,
  2488. encodedPoint, encoded_pt_len, bn_ctx);
  2489. *p = n; /* length of encoded point */
  2490. /* Encoded point will be copied here */
  2491. p += 1;
  2492. /* copy the point */
  2493. memcpy((unsigned char *)p, encodedPoint, n);
  2494. /* increment n to account for length field */
  2495. n += 1;
  2496. }
  2497. /* Free allocated memory */
  2498. BN_CTX_free(bn_ctx);
  2499. if (encodedPoint != NULL)
  2500. OPENSSL_free(encodedPoint);
  2501. if (clnt_ecdh != NULL)
  2502. EC_KEY_free(clnt_ecdh);
  2503. EVP_PKEY_free(srvr_pub_pkey);
  2504. }
  2505. #endif /* !OPENSSL_NO_ECDH */
  2506. else if (alg_k & SSL_kGOST) {
  2507. /* GOST key exchange message creation */
  2508. EVP_PKEY_CTX *pkey_ctx;
  2509. X509 *peer_cert;
  2510. size_t msglen;
  2511. unsigned int md_len;
  2512. int keytype;
  2513. unsigned char premaster_secret[32], shared_ukm[32], tmp[256];
  2514. EVP_MD_CTX *ukm_hash;
  2515. EVP_PKEY *pub_key;
  2516. /*
  2517. * Get server sertificate PKEY and create ctx from it
  2518. */
  2519. peer_cert =
  2520. s->session->
  2521. sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST01)].x509;
  2522. if (!peer_cert)
  2523. peer_cert =
  2524. s->session->
  2525. sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST94)].x509;
  2526. if (!peer_cert) {
  2527. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2528. SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
  2529. goto err;
  2530. }
  2531. pkey_ctx = EVP_PKEY_CTX_new(pub_key =
  2532. X509_get_pubkey(peer_cert), NULL);
  2533. /*
  2534. * If we have send a certificate, and certificate key
  2535. *
  2536. * * parameters match those of server certificate, use
  2537. * certificate key for key exchange
  2538. */
  2539. /* Otherwise, generate ephemeral key pair */
  2540. EVP_PKEY_encrypt_init(pkey_ctx);
  2541. /* Generate session key */
  2542. if (RAND_bytes(premaster_secret, 32) <= 0) {
  2543. EVP_PKEY_CTX_free(pkey_ctx);
  2544. goto err;
  2545. }
  2546. /*
  2547. * If we have client certificate, use its secret as peer key
  2548. */
  2549. if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
  2550. if (EVP_PKEY_derive_set_peer
  2551. (pkey_ctx, s->cert->key->privatekey) <= 0) {
  2552. /*
  2553. * If there was an error - just ignore it. Ephemeral key
  2554. * * would be used
  2555. */
  2556. ERR_clear_error();
  2557. }
  2558. }
  2559. /*
  2560. * Compute shared IV and store it in algorithm-specific context
  2561. * data
  2562. */
  2563. ukm_hash = EVP_MD_CTX_create();
  2564. EVP_DigestInit(ukm_hash,
  2565. EVP_get_digestbynid(NID_id_GostR3411_94));
  2566. EVP_DigestUpdate(ukm_hash, s->s3->client_random,
  2567. SSL3_RANDOM_SIZE);
  2568. EVP_DigestUpdate(ukm_hash, s->s3->server_random,
  2569. SSL3_RANDOM_SIZE);
  2570. EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
  2571. EVP_MD_CTX_destroy(ukm_hash);
  2572. if (EVP_PKEY_CTX_ctrl
  2573. (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
  2574. shared_ukm) < 0) {
  2575. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2576. SSL_R_LIBRARY_BUG);
  2577. goto err;
  2578. }
  2579. /* Make GOST keytransport blob message */
  2580. /*
  2581. * Encapsulate it into sequence
  2582. */
  2583. *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
  2584. msglen = 255;
  2585. if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, premaster_secret, 32)
  2586. < 0) {
  2587. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2588. SSL_R_LIBRARY_BUG);
  2589. goto err;
  2590. }
  2591. if (msglen >= 0x80) {
  2592. *(p++) = 0x81;
  2593. *(p++) = msglen & 0xff;
  2594. n = msglen + 3;
  2595. } else {
  2596. *(p++) = msglen & 0xff;
  2597. n = msglen + 2;
  2598. }
  2599. memcpy(p, tmp, msglen);
  2600. /* Check if pubkey from client certificate was used */
  2601. if (EVP_PKEY_CTX_ctrl
  2602. (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
  2603. /* Set flag "skip certificate verify" */
  2604. s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
  2605. }
  2606. EVP_PKEY_CTX_free(pkey_ctx);
  2607. s->session->master_key_length =
  2608. s->method->ssl3_enc->generate_master_secret(s,
  2609. s->
  2610. session->master_key,
  2611. premaster_secret,
  2612. 32);
  2613. EVP_PKEY_free(pub_key);
  2614. }
  2615. #ifndef OPENSSL_NO_SRP
  2616. else if (alg_k & SSL_kSRP) {
  2617. if (s->srp_ctx.A != NULL) {
  2618. /* send off the data */
  2619. n = BN_num_bytes(s->srp_ctx.A);
  2620. s2n(n, p);
  2621. BN_bn2bin(s->srp_ctx.A, p);
  2622. n += 2;
  2623. } else {
  2624. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2625. ERR_R_INTERNAL_ERROR);
  2626. goto err;
  2627. }
  2628. if (s->session->srp_username != NULL)
  2629. OPENSSL_free(s->session->srp_username);
  2630. s->session->srp_username = BUF_strdup(s->srp_ctx.login);
  2631. if (s->session->srp_username == NULL) {
  2632. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2633. ERR_R_MALLOC_FAILURE);
  2634. goto err;
  2635. }
  2636. if ((s->session->master_key_length =
  2637. SRP_generate_client_master_secret(s,
  2638. s->session->master_key)) <
  2639. 0) {
  2640. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2641. ERR_R_INTERNAL_ERROR);
  2642. goto err;
  2643. }
  2644. }
  2645. #endif
  2646. #ifndef OPENSSL_NO_PSK
  2647. else if (alg_k & SSL_kPSK) {
  2648. /*
  2649. * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
  2650. * \0-terminated identity. The last byte is for us for simulating
  2651. * strnlen.
  2652. */
  2653. char identity[PSK_MAX_IDENTITY_LEN + 2];
  2654. size_t identity_len;
  2655. unsigned char *t = NULL;
  2656. unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
  2657. unsigned int pre_ms_len = 0, psk_len = 0;
  2658. int psk_err = 1;
  2659. n = 0;
  2660. if (s->psk_client_callback == NULL) {
  2661. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2662. SSL_R_PSK_NO_CLIENT_CB);
  2663. goto err;
  2664. }
  2665. memset(identity, 0, sizeof(identity));
  2666. psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
  2667. identity, sizeof(identity) - 1,
  2668. psk_or_pre_ms,
  2669. sizeof(psk_or_pre_ms));
  2670. if (psk_len > PSK_MAX_PSK_LEN) {
  2671. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2672. ERR_R_INTERNAL_ERROR);
  2673. goto psk_err;
  2674. } else if (psk_len == 0) {
  2675. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2676. SSL_R_PSK_IDENTITY_NOT_FOUND);
  2677. goto psk_err;
  2678. }
  2679. identity[PSK_MAX_IDENTITY_LEN + 1] = '\0';
  2680. identity_len = strlen(identity);
  2681. if (identity_len > PSK_MAX_IDENTITY_LEN) {
  2682. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2683. ERR_R_INTERNAL_ERROR);
  2684. goto psk_err;
  2685. }
  2686. /* create PSK pre_master_secret */
  2687. pre_ms_len = 2 + psk_len + 2 + psk_len;
  2688. t = psk_or_pre_ms;
  2689. memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
  2690. s2n(psk_len, t);
  2691. memset(t, 0, psk_len);
  2692. t += psk_len;
  2693. s2n(psk_len, t);
  2694. if (s->session->psk_identity_hint != NULL)
  2695. OPENSSL_free(s->session->psk_identity_hint);
  2696. s->session->psk_identity_hint =
  2697. BUF_strdup(s->ctx->psk_identity_hint);
  2698. if (s->ctx->psk_identity_hint != NULL
  2699. && s->session->psk_identity_hint == NULL) {
  2700. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2701. ERR_R_MALLOC_FAILURE);
  2702. goto psk_err;
  2703. }
  2704. if (s->session->psk_identity != NULL)
  2705. OPENSSL_free(s->session->psk_identity);
  2706. s->session->psk_identity = BUF_strdup(identity);
  2707. if (s->session->psk_identity == NULL) {
  2708. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2709. ERR_R_MALLOC_FAILURE);
  2710. goto psk_err;
  2711. }
  2712. s->session->master_key_length =
  2713. s->method->ssl3_enc->generate_master_secret(s,
  2714. s->
  2715. session->master_key,
  2716. psk_or_pre_ms,
  2717. pre_ms_len);
  2718. s2n(identity_len, p);
  2719. memcpy(p, identity, identity_len);
  2720. n = 2 + identity_len;
  2721. psk_err = 0;
  2722. psk_err:
  2723. OPENSSL_cleanse(identity, sizeof(identity));
  2724. OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
  2725. if (psk_err != 0) {
  2726. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  2727. goto err;
  2728. }
  2729. }
  2730. #endif
  2731. else {
  2732. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  2733. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  2734. goto err;
  2735. }
  2736. *(d++) = SSL3_MT_CLIENT_KEY_EXCHANGE;
  2737. l2n3(n, d);
  2738. s->state = SSL3_ST_CW_KEY_EXCH_B;
  2739. /* number of bytes to write */
  2740. s->init_num = n + 4;
  2741. s->init_off = 0;
  2742. }
  2743. /* SSL3_ST_CW_KEY_EXCH_B */
  2744. return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
  2745. err:
  2746. #ifndef OPENSSL_NO_ECDH
  2747. BN_CTX_free(bn_ctx);
  2748. if (encodedPoint != NULL)
  2749. OPENSSL_free(encodedPoint);
  2750. if (clnt_ecdh != NULL)
  2751. EC_KEY_free(clnt_ecdh);
  2752. EVP_PKEY_free(srvr_pub_pkey);
  2753. #endif
  2754. s->state = SSL_ST_ERR;
  2755. return (-1);
  2756. }
  2757. int ssl3_send_client_verify(SSL *s)
  2758. {
  2759. unsigned char *p, *d;
  2760. unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
  2761. EVP_PKEY *pkey;
  2762. EVP_PKEY_CTX *pctx = NULL;
  2763. EVP_MD_CTX mctx;
  2764. unsigned u = 0;
  2765. unsigned long n;
  2766. int j;
  2767. EVP_MD_CTX_init(&mctx);
  2768. if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
  2769. d = (unsigned char *)s->init_buf->data;
  2770. p = &(d[4]);
  2771. pkey = s->cert->key->privatekey;
  2772. /* Create context from key and test if sha1 is allowed as digest */
  2773. pctx = EVP_PKEY_CTX_new(pkey, NULL);
  2774. EVP_PKEY_sign_init(pctx);
  2775. if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
  2776. if (TLS1_get_version(s) < TLS1_2_VERSION)
  2777. s->method->ssl3_enc->cert_verify_mac(s,
  2778. NID_sha1,
  2779. &(data
  2780. [MD5_DIGEST_LENGTH]));
  2781. } else {
  2782. ERR_clear_error();
  2783. }
  2784. /*
  2785. * For TLS v1.2 send signature algorithm and signature using agreed
  2786. * digest and cached handshake records.
  2787. */
  2788. if (TLS1_get_version(s) >= TLS1_2_VERSION) {
  2789. long hdatalen = 0;
  2790. void *hdata;
  2791. const EVP_MD *md = s->cert->key->digest;
  2792. hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
  2793. if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
  2794. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
  2795. goto err;
  2796. }
  2797. p += 2;
  2798. #ifdef SSL_DEBUG
  2799. fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
  2800. EVP_MD_name(md));
  2801. #endif
  2802. if (!EVP_SignInit_ex(&mctx, md, NULL)
  2803. || !EVP_SignUpdate(&mctx, hdata, hdatalen)
  2804. || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
  2805. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
  2806. goto err;
  2807. }
  2808. s2n(u, p);
  2809. n = u + 4;
  2810. if (!ssl3_digest_cached_records(s))
  2811. goto err;
  2812. } else
  2813. #ifndef OPENSSL_NO_RSA
  2814. if (pkey->type == EVP_PKEY_RSA) {
  2815. s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
  2816. if (RSA_sign(NID_md5_sha1, data,
  2817. MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
  2818. &(p[2]), &u, pkey->pkey.rsa) <= 0) {
  2819. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
  2820. goto err;
  2821. }
  2822. s2n(u, p);
  2823. n = u + 2;
  2824. } else
  2825. #endif
  2826. #ifndef OPENSSL_NO_DSA
  2827. if (pkey->type == EVP_PKEY_DSA) {
  2828. if (!DSA_sign(pkey->save_type,
  2829. &(data[MD5_DIGEST_LENGTH]),
  2830. SHA_DIGEST_LENGTH, &(p[2]),
  2831. (unsigned int *)&j, pkey->pkey.dsa)) {
  2832. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
  2833. goto err;
  2834. }
  2835. s2n(j, p);
  2836. n = j + 2;
  2837. } else
  2838. #endif
  2839. #ifndef OPENSSL_NO_ECDSA
  2840. if (pkey->type == EVP_PKEY_EC) {
  2841. if (!ECDSA_sign(pkey->save_type,
  2842. &(data[MD5_DIGEST_LENGTH]),
  2843. SHA_DIGEST_LENGTH, &(p[2]),
  2844. (unsigned int *)&j, pkey->pkey.ec)) {
  2845. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
  2846. goto err;
  2847. }
  2848. s2n(j, p);
  2849. n = j + 2;
  2850. } else
  2851. #endif
  2852. if (pkey->type == NID_id_GostR3410_94
  2853. || pkey->type == NID_id_GostR3410_2001) {
  2854. unsigned char signbuf[64];
  2855. int i;
  2856. size_t sigsize = 64;
  2857. s->method->ssl3_enc->cert_verify_mac(s,
  2858. NID_id_GostR3411_94, data);
  2859. if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
  2860. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
  2861. goto err;
  2862. }
  2863. for (i = 63, j = 0; i >= 0; j++, i--) {
  2864. p[2 + j] = signbuf[i];
  2865. }
  2866. s2n(j, p);
  2867. n = j + 2;
  2868. } else {
  2869. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
  2870. goto err;
  2871. }
  2872. *(d++) = SSL3_MT_CERTIFICATE_VERIFY;
  2873. l2n3(n, d);
  2874. s->state = SSL3_ST_CW_CERT_VRFY_B;
  2875. s->init_num = (int)n + 4;
  2876. s->init_off = 0;
  2877. }
  2878. EVP_MD_CTX_cleanup(&mctx);
  2879. EVP_PKEY_CTX_free(pctx);
  2880. return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
  2881. err:
  2882. EVP_MD_CTX_cleanup(&mctx);
  2883. EVP_PKEY_CTX_free(pctx);
  2884. s->state = SSL_ST_ERR;
  2885. return (-1);
  2886. }
  2887. int ssl3_send_client_certificate(SSL *s)
  2888. {
  2889. X509 *x509 = NULL;
  2890. EVP_PKEY *pkey = NULL;
  2891. int i;
  2892. unsigned long l;
  2893. if (s->state == SSL3_ST_CW_CERT_A) {
  2894. if ((s->cert == NULL) ||
  2895. (s->cert->key->x509 == NULL) ||
  2896. (s->cert->key->privatekey == NULL))
  2897. s->state = SSL3_ST_CW_CERT_B;
  2898. else
  2899. s->state = SSL3_ST_CW_CERT_C;
  2900. }
  2901. /* We need to get a client cert */
  2902. if (s->state == SSL3_ST_CW_CERT_B) {
  2903. /*
  2904. * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
  2905. * return(-1); We then get retied later
  2906. */
  2907. i = 0;
  2908. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  2909. if (i < 0) {
  2910. s->rwstate = SSL_X509_LOOKUP;
  2911. return (-1);
  2912. }
  2913. s->rwstate = SSL_NOTHING;
  2914. if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
  2915. s->state = SSL3_ST_CW_CERT_B;
  2916. if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
  2917. i = 0;
  2918. } else if (i == 1) {
  2919. i = 0;
  2920. SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
  2921. SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  2922. }
  2923. if (x509 != NULL)
  2924. X509_free(x509);
  2925. if (pkey != NULL)
  2926. EVP_PKEY_free(pkey);
  2927. if (i == 0) {
  2928. if (s->version == SSL3_VERSION) {
  2929. s->s3->tmp.cert_req = 0;
  2930. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
  2931. return (1);
  2932. } else {
  2933. s->s3->tmp.cert_req = 2;
  2934. }
  2935. }
  2936. /* Ok, we have a cert */
  2937. s->state = SSL3_ST_CW_CERT_C;
  2938. }
  2939. if (s->state == SSL3_ST_CW_CERT_C) {
  2940. s->state = SSL3_ST_CW_CERT_D;
  2941. l = ssl3_output_cert_chain(s,
  2942. (s->s3->tmp.cert_req ==
  2943. 2) ? NULL : s->cert->key->x509);
  2944. if (!l) {
  2945. SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
  2946. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  2947. s->state = SSL_ST_ERR;
  2948. return 0;
  2949. }
  2950. s->init_num = (int)l;
  2951. s->init_off = 0;
  2952. }
  2953. /* SSL3_ST_CW_CERT_D */
  2954. return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
  2955. }
  2956. #define has_bits(i,m) (((i)&(m)) == (m))
  2957. int ssl3_check_cert_and_algorithm(SSL *s)
  2958. {
  2959. int i, idx;
  2960. long alg_k, alg_a;
  2961. EVP_PKEY *pkey = NULL;
  2962. int pkey_bits;
  2963. SESS_CERT *sc;
  2964. #ifndef OPENSSL_NO_RSA
  2965. RSA *rsa;
  2966. #endif
  2967. #ifndef OPENSSL_NO_DH
  2968. DH *dh;
  2969. #endif
  2970. int al = SSL_AD_HANDSHAKE_FAILURE;
  2971. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  2972. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  2973. /* we don't have a certificate */
  2974. if ((alg_a & (SSL_aDH | SSL_aNULL | SSL_aKRB5)) || (alg_k & SSL_kPSK))
  2975. return (1);
  2976. sc = s->session->sess_cert;
  2977. if (sc == NULL) {
  2978. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
  2979. goto err;
  2980. }
  2981. #ifndef OPENSSL_NO_RSA
  2982. rsa = s->session->sess_cert->peer_rsa_tmp;
  2983. #endif
  2984. #ifndef OPENSSL_NO_DH
  2985. dh = s->session->sess_cert->peer_dh_tmp;
  2986. #endif
  2987. /* This is the passed certificate */
  2988. idx = sc->peer_cert_type;
  2989. #ifndef OPENSSL_NO_ECDH
  2990. if (idx == SSL_PKEY_ECC) {
  2991. if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
  2992. /* check failed */
  2993. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
  2994. goto f_err;
  2995. } else {
  2996. return 1;
  2997. }
  2998. }
  2999. #endif
  3000. pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
  3001. pkey_bits = EVP_PKEY_bits(pkey);
  3002. i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
  3003. EVP_PKEY_free(pkey);
  3004. /* Check that we have a certificate if we require one */
  3005. if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
  3006. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3007. SSL_R_MISSING_RSA_SIGNING_CERT);
  3008. goto f_err;
  3009. }
  3010. #ifndef OPENSSL_NO_DSA
  3011. else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
  3012. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3013. SSL_R_MISSING_DSA_SIGNING_CERT);
  3014. goto f_err;
  3015. }
  3016. #endif
  3017. #ifndef OPENSSL_NO_RSA
  3018. if (alg_k & SSL_kRSA) {
  3019. if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  3020. !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
  3021. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3022. SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  3023. goto f_err;
  3024. } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
  3025. if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
  3026. if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
  3027. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3028. SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  3029. goto f_err;
  3030. }
  3031. if (rsa != NULL) {
  3032. /* server key exchange is not allowed. */
  3033. al = SSL_AD_INTERNAL_ERROR;
  3034. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
  3035. goto f_err;
  3036. }
  3037. }
  3038. }
  3039. }
  3040. #endif
  3041. #ifndef OPENSSL_NO_DH
  3042. if ((alg_k & SSL_kEDH) && dh == NULL) {
  3043. al = SSL_AD_INTERNAL_ERROR;
  3044. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
  3045. goto f_err;
  3046. }
  3047. if ((alg_k & SSL_kDHr) && !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
  3048. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3049. SSL_R_MISSING_DH_RSA_CERT);
  3050. goto f_err;
  3051. }
  3052. # ifndef OPENSSL_NO_DSA
  3053. if ((alg_k & SSL_kDHd) && !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
  3054. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3055. SSL_R_MISSING_DH_DSA_CERT);
  3056. goto f_err;
  3057. }
  3058. # endif
  3059. /* Check DHE only: static DH not implemented. */
  3060. if (alg_k & SSL_kEDH) {
  3061. int dh_size = BN_num_bits(dh->p);
  3062. if ((!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && dh_size < 768)
  3063. || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && dh_size < 512)) {
  3064. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_DH_KEY_TOO_SMALL);
  3065. goto f_err;
  3066. }
  3067. }
  3068. #endif /* !OPENSSL_NO_DH */
  3069. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  3070. pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
  3071. #ifndef OPENSSL_NO_RSA
  3072. if (alg_k & SSL_kRSA) {
  3073. if (rsa == NULL) {
  3074. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3075. SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
  3076. goto f_err;
  3077. } else if (BN_num_bits(rsa->n) >
  3078. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
  3079. /* We have a temporary RSA key but it's too large. */
  3080. al = SSL_AD_EXPORT_RESTRICTION;
  3081. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3082. SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
  3083. goto f_err;
  3084. }
  3085. } else
  3086. #endif
  3087. #ifndef OPENSSL_NO_DH
  3088. if (alg_k & SSL_kEDH) {
  3089. if (BN_num_bits(dh->p) >
  3090. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
  3091. /* We have a temporary DH key but it's too large. */
  3092. al = SSL_AD_EXPORT_RESTRICTION;
  3093. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3094. SSL_R_MISSING_EXPORT_TMP_DH_KEY);
  3095. goto f_err;
  3096. }
  3097. } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
  3098. /* The cert should have had an export DH key. */
  3099. al = SSL_AD_EXPORT_RESTRICTION;
  3100. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3101. SSL_R_MISSING_EXPORT_TMP_DH_KEY);
  3102. goto f_err;
  3103. } else
  3104. #endif
  3105. {
  3106. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3107. SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  3108. goto f_err;
  3109. }
  3110. }
  3111. return (1);
  3112. f_err:
  3113. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  3114. err:
  3115. return (0);
  3116. }
  3117. #ifndef OPENSSL_NO_TLSEXT
  3118. /*
  3119. * Normally, we can tell if the server is resuming the session from
  3120. * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
  3121. * message after the ServerHello to determine if the server is resuming.
  3122. * Therefore, we allow EAP-FAST to peek ahead.
  3123. * ssl3_check_finished returns 1 if we are resuming from an external
  3124. * pre-shared secret, we have a "ticket" and the next server handshake message
  3125. * is Finished; and 0 otherwise. It returns -1 upon an error.
  3126. */
  3127. static int ssl3_check_finished(SSL *s)
  3128. {
  3129. int ok = 0;
  3130. if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
  3131. !s->session->tlsext_tick)
  3132. return 0;
  3133. /* Need to permit this temporarily, in case the next message is Finished. */
  3134. s->s3->flags |= SSL3_FLAGS_CCS_OK;
  3135. /*
  3136. * This function is called when we might get a Certificate message instead,
  3137. * so permit appropriate message length.
  3138. * We ignore the return value as we're only interested in the message type
  3139. * and not its length.
  3140. */
  3141. s->method->ssl_get_message(s,
  3142. SSL3_ST_CR_CERT_A,
  3143. SSL3_ST_CR_CERT_B,
  3144. -1, s->max_cert_list, &ok);
  3145. s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
  3146. if (!ok)
  3147. return -1;
  3148. s->s3->tmp.reuse_message = 1;
  3149. if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
  3150. return 1;
  3151. /* If we're not done, then the CCS arrived early and we should bail. */
  3152. if (s->s3->change_cipher_spec) {
  3153. SSLerr(SSL_F_SSL3_CHECK_FINISHED, SSL_R_CCS_RECEIVED_EARLY);
  3154. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  3155. return -1;
  3156. }
  3157. return 0;
  3158. }
  3159. # ifndef OPENSSL_NO_NEXTPROTONEG
  3160. int ssl3_send_next_proto(SSL *s)
  3161. {
  3162. unsigned int len, padding_len;
  3163. unsigned char *d;
  3164. if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
  3165. len = s->next_proto_negotiated_len;
  3166. padding_len = 32 - ((len + 2) % 32);
  3167. d = (unsigned char *)s->init_buf->data;
  3168. d[4] = len;
  3169. memcpy(d + 5, s->next_proto_negotiated, len);
  3170. d[5 + len] = padding_len;
  3171. memset(d + 6 + len, 0, padding_len);
  3172. *(d++) = SSL3_MT_NEXT_PROTO;
  3173. l2n3(2 + len + padding_len, d);
  3174. s->state = SSL3_ST_CW_NEXT_PROTO_B;
  3175. s->init_num = 4 + 2 + len + padding_len;
  3176. s->init_off = 0;
  3177. }
  3178. return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
  3179. }
  3180. #endif /* !OPENSSL_NO_NEXTPROTONEG */
  3181. #endif /* !OPENSSL_NO_TLSEXT */
  3182. int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
  3183. {
  3184. int i = 0;
  3185. #ifndef OPENSSL_NO_ENGINE
  3186. if (s->ctx->client_cert_engine) {
  3187. i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
  3188. SSL_get_client_CA_list(s),
  3189. px509, ppkey, NULL, NULL, NULL);
  3190. if (i != 0)
  3191. return i;
  3192. }
  3193. #endif
  3194. if (s->ctx->client_cert_cb)
  3195. i = s->ctx->client_cert_cb(s, px509, ppkey);
  3196. return i;
  3197. }