s3_clnt.c 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568
  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. param_len = 2;
  1257. if (param_len > n) {
  1258. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1259. goto f_err;
  1260. }
  1261. n2s(p, i);
  1262. /*
  1263. * Store PSK identity hint for later use, hint is used in
  1264. * ssl3_send_client_key_exchange. Assume that the maximum length of
  1265. * a PSK identity hint can be as long as the maximum length of a PSK
  1266. * identity.
  1267. */
  1268. if (i > PSK_MAX_IDENTITY_LEN) {
  1269. al = SSL_AD_HANDSHAKE_FAILURE;
  1270. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
  1271. goto f_err;
  1272. }
  1273. if (i > n - param_len) {
  1274. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1275. SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
  1276. goto f_err;
  1277. }
  1278. param_len += i;
  1279. s->session->psk_identity_hint = BUF_strndup((char *)p, i);
  1280. if (s->session->psk_identity_hint == NULL) {
  1281. al = SSL_AD_HANDSHAKE_FAILURE;
  1282. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1283. goto f_err;
  1284. }
  1285. p += i;
  1286. n -= param_len;
  1287. } else
  1288. #endif /* !OPENSSL_NO_PSK */
  1289. #ifndef OPENSSL_NO_SRP
  1290. if (alg_k & SSL_kSRP) {
  1291. param_len = 2;
  1292. if (param_len > n) {
  1293. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1294. goto f_err;
  1295. }
  1296. n2s(p, i);
  1297. if (i > n - param_len) {
  1298. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
  1299. goto f_err;
  1300. }
  1301. param_len += i;
  1302. if (!(s->srp_ctx.N = BN_bin2bn(p, i, NULL))) {
  1303. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1304. goto err;
  1305. }
  1306. p += i;
  1307. if (2 > n - param_len) {
  1308. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1309. goto f_err;
  1310. }
  1311. param_len += 2;
  1312. n2s(p, i);
  1313. if (i > n - param_len) {
  1314. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
  1315. goto f_err;
  1316. }
  1317. param_len += i;
  1318. if (!(s->srp_ctx.g = BN_bin2bn(p, i, NULL))) {
  1319. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1320. goto err;
  1321. }
  1322. p += i;
  1323. if (1 > n - param_len) {
  1324. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1325. goto f_err;
  1326. }
  1327. param_len += 1;
  1328. i = (unsigned int)(p[0]);
  1329. p++;
  1330. if (i > n - param_len) {
  1331. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
  1332. goto f_err;
  1333. }
  1334. param_len += i;
  1335. if (!(s->srp_ctx.s = BN_bin2bn(p, i, NULL))) {
  1336. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1337. goto err;
  1338. }
  1339. p += i;
  1340. if (2 > n - param_len) {
  1341. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1342. goto f_err;
  1343. }
  1344. param_len += 2;
  1345. n2s(p, i);
  1346. if (i > n - param_len) {
  1347. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);
  1348. goto f_err;
  1349. }
  1350. param_len += i;
  1351. if (!(s->srp_ctx.B = BN_bin2bn(p, i, NULL))) {
  1352. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1353. goto err;
  1354. }
  1355. p += i;
  1356. n -= param_len;
  1357. if (!srp_verify_server_param(s, &al)) {
  1358. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
  1359. goto f_err;
  1360. }
  1361. /* We must check if there is a certificate */
  1362. # ifndef OPENSSL_NO_RSA
  1363. if (alg_a & SSL_aRSA)
  1364. pkey =
  1365. X509_get_pubkey(s->session->
  1366. sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1367. # else
  1368. if (0) ;
  1369. # endif
  1370. # ifndef OPENSSL_NO_DSA
  1371. else if (alg_a & SSL_aDSS)
  1372. pkey =
  1373. X509_get_pubkey(s->session->
  1374. sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
  1375. x509);
  1376. # endif
  1377. } else
  1378. #endif /* !OPENSSL_NO_SRP */
  1379. #ifndef OPENSSL_NO_RSA
  1380. if (alg_k & SSL_kRSA) {
  1381. /* Temporary RSA keys only allowed in export ciphersuites */
  1382. if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
  1383. al = SSL_AD_UNEXPECTED_MESSAGE;
  1384. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
  1385. goto f_err;
  1386. }
  1387. if ((rsa = RSA_new()) == NULL) {
  1388. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1389. goto err;
  1390. }
  1391. param_len = 2;
  1392. if (param_len > n) {
  1393. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1394. goto f_err;
  1395. }
  1396. n2s(p, i);
  1397. if (i > n - param_len) {
  1398. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
  1399. goto f_err;
  1400. }
  1401. param_len += i;
  1402. if (!(rsa->n = BN_bin2bn(p, i, rsa->n))) {
  1403. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1404. goto err;
  1405. }
  1406. p += i;
  1407. if (2 > n - param_len) {
  1408. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1409. goto f_err;
  1410. }
  1411. param_len += 2;
  1412. n2s(p, i);
  1413. if (i > n - param_len) {
  1414. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
  1415. goto f_err;
  1416. }
  1417. param_len += i;
  1418. if (!(rsa->e = BN_bin2bn(p, i, rsa->e))) {
  1419. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1420. goto err;
  1421. }
  1422. p += i;
  1423. n -= param_len;
  1424. /* this should be because we are using an export cipher */
  1425. if (alg_a & SSL_aRSA)
  1426. pkey =
  1427. X509_get_pubkey(s->session->
  1428. sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1429. else {
  1430. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1431. goto err;
  1432. }
  1433. if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
  1434. al = SSL_AD_UNEXPECTED_MESSAGE;
  1435. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
  1436. goto f_err;
  1437. }
  1438. s->session->sess_cert->peer_rsa_tmp = rsa;
  1439. rsa = NULL;
  1440. }
  1441. #else /* OPENSSL_NO_RSA */
  1442. if (0) ;
  1443. #endif
  1444. #ifndef OPENSSL_NO_DH
  1445. else if (alg_k & SSL_kEDH) {
  1446. if ((dh = DH_new()) == NULL) {
  1447. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
  1448. goto err;
  1449. }
  1450. param_len = 2;
  1451. if (param_len > n) {
  1452. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1453. goto f_err;
  1454. }
  1455. n2s(p, i);
  1456. if (i > n - param_len) {
  1457. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
  1458. goto f_err;
  1459. }
  1460. param_len += i;
  1461. if (!(dh->p = BN_bin2bn(p, i, NULL))) {
  1462. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1463. goto err;
  1464. }
  1465. p += i;
  1466. if (BN_is_zero(dh->p)) {
  1467. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_VALUE);
  1468. goto f_err;
  1469. }
  1470. if (2 > n - param_len) {
  1471. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1472. goto f_err;
  1473. }
  1474. param_len += 2;
  1475. n2s(p, i);
  1476. if (i > n - param_len) {
  1477. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
  1478. goto f_err;
  1479. }
  1480. param_len += i;
  1481. if (!(dh->g = BN_bin2bn(p, i, NULL))) {
  1482. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1483. goto err;
  1484. }
  1485. p += i;
  1486. if (BN_is_zero(dh->g)) {
  1487. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_VALUE);
  1488. goto f_err;
  1489. }
  1490. if (2 > n - param_len) {
  1491. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1492. goto f_err;
  1493. }
  1494. param_len += 2;
  1495. n2s(p, i);
  1496. if (i > n - param_len) {
  1497. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
  1498. goto f_err;
  1499. }
  1500. param_len += i;
  1501. if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) {
  1502. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
  1503. goto err;
  1504. }
  1505. p += i;
  1506. n -= param_len;
  1507. if (BN_is_zero(dh->pub_key)) {
  1508. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_VALUE);
  1509. goto f_err;
  1510. }
  1511. # ifndef OPENSSL_NO_RSA
  1512. if (alg_a & SSL_aRSA)
  1513. pkey =
  1514. X509_get_pubkey(s->session->
  1515. sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1516. # else
  1517. if (0) ;
  1518. # endif
  1519. # ifndef OPENSSL_NO_DSA
  1520. else if (alg_a & SSL_aDSS)
  1521. pkey =
  1522. X509_get_pubkey(s->session->
  1523. sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
  1524. x509);
  1525. # endif
  1526. /* else anonymous DH, so no certificate or pkey. */
  1527. s->session->sess_cert->peer_dh_tmp = dh;
  1528. dh = NULL;
  1529. } else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd)) {
  1530. al = SSL_AD_ILLEGAL_PARAMETER;
  1531. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1532. SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
  1533. goto f_err;
  1534. }
  1535. #endif /* !OPENSSL_NO_DH */
  1536. #ifndef OPENSSL_NO_ECDH
  1537. else if (alg_k & SSL_kEECDH) {
  1538. EC_GROUP *ngroup;
  1539. const EC_GROUP *group;
  1540. if ((ecdh = EC_KEY_new()) == NULL) {
  1541. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1542. goto err;
  1543. }
  1544. /*
  1545. * Extract elliptic curve parameters and the server's ephemeral ECDH
  1546. * public key. Keep accumulating lengths of various components in
  1547. * param_len and make sure it never exceeds n.
  1548. */
  1549. /*
  1550. * XXX: For now we only support named (not generic) curves and the
  1551. * ECParameters in this case is just three bytes. We also need one
  1552. * byte for the length of the encoded point
  1553. */
  1554. param_len = 4;
  1555. if (param_len > n) {
  1556. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1557. goto f_err;
  1558. }
  1559. if ((*p != NAMED_CURVE_TYPE) ||
  1560. ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0)) {
  1561. al = SSL_AD_INTERNAL_ERROR;
  1562. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1563. SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  1564. goto f_err;
  1565. }
  1566. ngroup = EC_GROUP_new_by_curve_name(curve_nid);
  1567. if (ngroup == NULL) {
  1568. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
  1569. goto err;
  1570. }
  1571. if (EC_KEY_set_group(ecdh, ngroup) == 0) {
  1572. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
  1573. goto err;
  1574. }
  1575. EC_GROUP_free(ngroup);
  1576. group = EC_KEY_get0_group(ecdh);
  1577. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  1578. (EC_GROUP_get_degree(group) > 163)) {
  1579. al = SSL_AD_EXPORT_RESTRICTION;
  1580. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1581. SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
  1582. goto f_err;
  1583. }
  1584. p += 3;
  1585. /* Next, get the encoded ECPoint */
  1586. if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
  1587. ((bn_ctx = BN_CTX_new()) == NULL)) {
  1588. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1589. goto err;
  1590. }
  1591. encoded_pt_len = *p; /* length of encoded point */
  1592. p += 1;
  1593. if ((encoded_pt_len > n - param_len) ||
  1594. (EC_POINT_oct2point(group, srvr_ecpoint,
  1595. p, encoded_pt_len, bn_ctx) == 0)) {
  1596. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
  1597. goto f_err;
  1598. }
  1599. param_len += encoded_pt_len;
  1600. n -= param_len;
  1601. p += encoded_pt_len;
  1602. /*
  1603. * The ECC/TLS specification does not mention the use of DSA to sign
  1604. * ECParameters in the server key exchange message. We do support RSA
  1605. * and ECDSA.
  1606. */
  1607. if (0) ;
  1608. # ifndef OPENSSL_NO_RSA
  1609. else if (alg_a & SSL_aRSA)
  1610. pkey =
  1611. X509_get_pubkey(s->session->
  1612. sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1613. # endif
  1614. # ifndef OPENSSL_NO_ECDSA
  1615. else if (alg_a & SSL_aECDSA)
  1616. pkey =
  1617. X509_get_pubkey(s->session->
  1618. sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  1619. # endif
  1620. /* else anonymous ECDH, so no certificate or pkey. */
  1621. EC_KEY_set_public_key(ecdh, srvr_ecpoint);
  1622. s->session->sess_cert->peer_ecdh_tmp = ecdh;
  1623. ecdh = NULL;
  1624. BN_CTX_free(bn_ctx);
  1625. bn_ctx = NULL;
  1626. EC_POINT_free(srvr_ecpoint);
  1627. srvr_ecpoint = NULL;
  1628. } else if (alg_k) {
  1629. al = SSL_AD_UNEXPECTED_MESSAGE;
  1630. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
  1631. goto f_err;
  1632. }
  1633. #endif /* !OPENSSL_NO_ECDH */
  1634. /* p points to the next byte, there are 'n' bytes left */
  1635. /* if it was signed, check the signature */
  1636. if (pkey != NULL) {
  1637. if (TLS1_get_version(s) >= TLS1_2_VERSION) {
  1638. int sigalg;
  1639. if (2 > n) {
  1640. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1641. goto f_err;
  1642. }
  1643. sigalg = tls12_get_sigid(pkey);
  1644. /* Should never happen */
  1645. if (sigalg == -1) {
  1646. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1647. goto err;
  1648. }
  1649. /* Check key type is consistent with signature */
  1650. if (sigalg != (int)p[1]) {
  1651. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1652. SSL_R_WRONG_SIGNATURE_TYPE);
  1653. al = SSL_AD_DECODE_ERROR;
  1654. goto f_err;
  1655. }
  1656. md = tls12_get_hash(p[0]);
  1657. if (md == NULL) {
  1658. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNKNOWN_DIGEST);
  1659. goto f_err;
  1660. }
  1661. #ifdef SSL_DEBUG
  1662. fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
  1663. #endif
  1664. p += 2;
  1665. n -= 2;
  1666. } else
  1667. md = EVP_sha1();
  1668. if (2 > n) {
  1669. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1670. goto f_err;
  1671. }
  1672. n2s(p, i);
  1673. n -= 2;
  1674. j = EVP_PKEY_size(pkey);
  1675. /*
  1676. * Check signature length. If n is 0 then signature is empty
  1677. */
  1678. if ((i != n) || (n > j) || (n <= 0)) {
  1679. /* wrong packet length */
  1680. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
  1681. goto f_err;
  1682. }
  1683. #ifndef OPENSSL_NO_RSA
  1684. if (pkey->type == EVP_PKEY_RSA
  1685. && TLS1_get_version(s) < TLS1_2_VERSION) {
  1686. int num;
  1687. unsigned int size;
  1688. j = 0;
  1689. q = md_buf;
  1690. for (num = 2; num > 0; num--) {
  1691. EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  1692. if (EVP_DigestInit_ex(&md_ctx,
  1693. (num == 2) ? s->ctx->md5 : s->ctx->sha1,
  1694. NULL) <= 0
  1695. || EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
  1696. SSL3_RANDOM_SIZE) <= 0
  1697. || EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
  1698. SSL3_RANDOM_SIZE) <= 0
  1699. || EVP_DigestUpdate(&md_ctx, param, param_len) <= 0
  1700. || EVP_DigestFinal_ex(&md_ctx, q, &size) <= 0) {
  1701. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1702. ERR_R_INTERNAL_ERROR);
  1703. al = SSL_AD_INTERNAL_ERROR;
  1704. goto f_err;
  1705. }
  1706. q += size;
  1707. j += size;
  1708. }
  1709. i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa);
  1710. if (i < 0) {
  1711. al = SSL_AD_DECRYPT_ERROR;
  1712. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
  1713. goto f_err;
  1714. }
  1715. if (i == 0) {
  1716. /* bad signature */
  1717. al = SSL_AD_DECRYPT_ERROR;
  1718. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
  1719. goto f_err;
  1720. }
  1721. } else
  1722. #endif
  1723. {
  1724. if (EVP_VerifyInit_ex(&md_ctx, md, NULL) <= 0
  1725. || EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
  1726. SSL3_RANDOM_SIZE) <= 0
  1727. || EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
  1728. SSL3_RANDOM_SIZE) <= 0
  1729. || EVP_VerifyUpdate(&md_ctx, param, param_len) <= 0) {
  1730. al = SSL_AD_INTERNAL_ERROR;
  1731. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EVP_LIB);
  1732. goto f_err;
  1733. }
  1734. if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) {
  1735. /* bad signature */
  1736. al = SSL_AD_DECRYPT_ERROR;
  1737. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
  1738. goto f_err;
  1739. }
  1740. }
  1741. } else {
  1742. /* aNULL, aSRP or kPSK do not need public keys */
  1743. if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) {
  1744. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1745. goto err;
  1746. }
  1747. /* still data left over */
  1748. if (n != 0) {
  1749. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
  1750. goto f_err;
  1751. }
  1752. }
  1753. EVP_PKEY_free(pkey);
  1754. EVP_MD_CTX_cleanup(&md_ctx);
  1755. return (1);
  1756. f_err:
  1757. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1758. err:
  1759. EVP_PKEY_free(pkey);
  1760. #ifndef OPENSSL_NO_RSA
  1761. if (rsa != NULL)
  1762. RSA_free(rsa);
  1763. #endif
  1764. #ifndef OPENSSL_NO_DH
  1765. if (dh != NULL)
  1766. DH_free(dh);
  1767. #endif
  1768. #ifndef OPENSSL_NO_ECDH
  1769. BN_CTX_free(bn_ctx);
  1770. EC_POINT_free(srvr_ecpoint);
  1771. if (ecdh != NULL)
  1772. EC_KEY_free(ecdh);
  1773. #endif
  1774. EVP_MD_CTX_cleanup(&md_ctx);
  1775. s->state = SSL_ST_ERR;
  1776. return (-1);
  1777. }
  1778. int ssl3_get_certificate_request(SSL *s)
  1779. {
  1780. int ok, ret = 0;
  1781. unsigned long n, nc, l;
  1782. unsigned int llen, ctype_num, i;
  1783. X509_NAME *xn = NULL;
  1784. const unsigned char *p, *q;
  1785. unsigned char *d;
  1786. STACK_OF(X509_NAME) *ca_sk = NULL;
  1787. n = s->method->ssl_get_message(s,
  1788. SSL3_ST_CR_CERT_REQ_A,
  1789. SSL3_ST_CR_CERT_REQ_B,
  1790. -1, s->max_cert_list, &ok);
  1791. if (!ok)
  1792. return ((int)n);
  1793. s->s3->tmp.cert_req = 0;
  1794. if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
  1795. s->s3->tmp.reuse_message = 1;
  1796. /*
  1797. * If we get here we don't need any cached handshake records as we
  1798. * wont be doing client auth.
  1799. */
  1800. if (s->s3->handshake_buffer) {
  1801. if (!ssl3_digest_cached_records(s))
  1802. goto err;
  1803. }
  1804. return (1);
  1805. }
  1806. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
  1807. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1808. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
  1809. goto err;
  1810. }
  1811. /* TLS does not like anon-DH with client cert */
  1812. if (s->version > SSL3_VERSION) {
  1813. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
  1814. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1815. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
  1816. SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
  1817. goto err;
  1818. }
  1819. }
  1820. p = d = (unsigned char *)s->init_msg;
  1821. if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
  1822. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
  1823. goto err;
  1824. }
  1825. /* get the certificate types */
  1826. ctype_num = *(p++);
  1827. if (ctype_num > SSL3_CT_NUMBER)
  1828. ctype_num = SSL3_CT_NUMBER;
  1829. for (i = 0; i < ctype_num; i++)
  1830. s->s3->tmp.ctype[i] = p[i];
  1831. p += ctype_num;
  1832. if (TLS1_get_version(s) >= TLS1_2_VERSION) {
  1833. n2s(p, llen);
  1834. /*
  1835. * Check we have enough room for signature algorithms and following
  1836. * length value.
  1837. */
  1838. if ((unsigned long)(p - d + llen + 2) > n) {
  1839. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1840. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
  1841. SSL_R_DATA_LENGTH_TOO_LONG);
  1842. goto err;
  1843. }
  1844. if ((llen & 1) || !tls1_process_sigalgs(s, p, llen)) {
  1845. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1846. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
  1847. SSL_R_SIGNATURE_ALGORITHMS_ERROR);
  1848. goto err;
  1849. }
  1850. p += llen;
  1851. }
  1852. /* get the CA RDNs */
  1853. n2s(p, llen);
  1854. #if 0
  1855. {
  1856. FILE *out;
  1857. out = fopen("/tmp/vsign.der", "w");
  1858. fwrite(p, 1, llen, out);
  1859. fclose(out);
  1860. }
  1861. #endif
  1862. if ((unsigned long)(p - d + llen) != n) {
  1863. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1864. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
  1865. goto err;
  1866. }
  1867. for (nc = 0; nc < llen;) {
  1868. n2s(p, l);
  1869. if ((l + nc + 2) > llen) {
  1870. if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  1871. goto cont; /* netscape bugs */
  1872. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1873. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
  1874. goto err;
  1875. }
  1876. q = p;
  1877. if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) {
  1878. /* If netscape tolerance is on, ignore errors */
  1879. if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
  1880. goto cont;
  1881. else {
  1882. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1883. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
  1884. goto err;
  1885. }
  1886. }
  1887. if (q != (p + l)) {
  1888. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1889. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
  1890. SSL_R_CA_DN_LENGTH_MISMATCH);
  1891. goto err;
  1892. }
  1893. if (!sk_X509_NAME_push(ca_sk, xn)) {
  1894. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
  1895. goto err;
  1896. }
  1897. p += l;
  1898. nc += l + 2;
  1899. }
  1900. if (0) {
  1901. cont:
  1902. ERR_clear_error();
  1903. }
  1904. /* we should setup a certificate to return.... */
  1905. s->s3->tmp.cert_req = 1;
  1906. s->s3->tmp.ctype_num = ctype_num;
  1907. if (s->s3->tmp.ca_names != NULL)
  1908. sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
  1909. s->s3->tmp.ca_names = ca_sk;
  1910. ca_sk = NULL;
  1911. ret = 1;
  1912. goto done;
  1913. err:
  1914. s->state = SSL_ST_ERR;
  1915. done:
  1916. if (ca_sk != NULL)
  1917. sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
  1918. return (ret);
  1919. }
  1920. static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
  1921. {
  1922. return (X509_NAME_cmp(*a, *b));
  1923. }
  1924. #ifndef OPENSSL_NO_TLSEXT
  1925. int ssl3_get_new_session_ticket(SSL *s)
  1926. {
  1927. int ok, al, ret = 0, ticklen;
  1928. long n;
  1929. const unsigned char *p;
  1930. unsigned char *d;
  1931. unsigned long ticket_lifetime_hint;
  1932. n = s->method->ssl_get_message(s,
  1933. SSL3_ST_CR_SESSION_TICKET_A,
  1934. SSL3_ST_CR_SESSION_TICKET_B,
  1935. SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
  1936. if (!ok)
  1937. return ((int)n);
  1938. if (n < 6) {
  1939. /* need at least ticket_lifetime_hint + ticket length */
  1940. al = SSL_AD_DECODE_ERROR;
  1941. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
  1942. goto f_err;
  1943. }
  1944. p = d = (unsigned char *)s->init_msg;
  1945. n2l(p, ticket_lifetime_hint);
  1946. n2s(p, ticklen);
  1947. /* ticket_lifetime_hint + ticket_length + ticket */
  1948. if (ticklen + 6 != n) {
  1949. al = SSL_AD_DECODE_ERROR;
  1950. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
  1951. goto f_err;
  1952. }
  1953. /* Server is allowed to change its mind and send an empty ticket. */
  1954. if (ticklen == 0)
  1955. return 1;
  1956. if (s->session->session_id_length > 0) {
  1957. int i = s->session_ctx->session_cache_mode;
  1958. SSL_SESSION *new_sess;
  1959. /*
  1960. * We reused an existing session, so we need to replace it with a new
  1961. * one
  1962. */
  1963. if (i & SSL_SESS_CACHE_CLIENT) {
  1964. /*
  1965. * Remove the old session from the cache
  1966. */
  1967. if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
  1968. if (s->session_ctx->remove_session_cb != NULL)
  1969. s->session_ctx->remove_session_cb(s->session_ctx,
  1970. s->session);
  1971. } else {
  1972. /* We carry on if this fails */
  1973. SSL_CTX_remove_session(s->session_ctx, s->session);
  1974. }
  1975. }
  1976. if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
  1977. al = SSL_AD_INTERNAL_ERROR;
  1978. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
  1979. goto f_err;
  1980. }
  1981. SSL_SESSION_free(s->session);
  1982. s->session = new_sess;
  1983. }
  1984. if (s->session->tlsext_tick) {
  1985. OPENSSL_free(s->session->tlsext_tick);
  1986. s->session->tlsext_ticklen = 0;
  1987. }
  1988. s->session->tlsext_tick = OPENSSL_malloc(ticklen);
  1989. if (!s->session->tlsext_tick) {
  1990. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
  1991. goto err;
  1992. }
  1993. memcpy(s->session->tlsext_tick, p, ticklen);
  1994. s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
  1995. s->session->tlsext_ticklen = ticklen;
  1996. /*
  1997. * There are two ways to detect a resumed ticket session. One is to set
  1998. * an appropriate session ID and then the server must return a match in
  1999. * ServerHello. This allows the normal client session ID matching to work
  2000. * and we know much earlier that the ticket has been accepted. The
  2001. * other way is to set zero length session ID when the ticket is
  2002. * presented and rely on the handshake to determine session resumption.
  2003. * We choose the former approach because this fits in with assumptions
  2004. * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
  2005. * SHA256 is disabled) hash of the ticket.
  2006. */
  2007. EVP_Digest(p, ticklen,
  2008. s->session->session_id, &s->session->session_id_length,
  2009. # ifndef OPENSSL_NO_SHA256
  2010. EVP_sha256(), NULL);
  2011. # else
  2012. EVP_sha1(), NULL);
  2013. # endif
  2014. ret = 1;
  2015. return (ret);
  2016. f_err:
  2017. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  2018. err:
  2019. s->state = SSL_ST_ERR;
  2020. return (-1);
  2021. }
  2022. int ssl3_get_cert_status(SSL *s)
  2023. {
  2024. int ok, al;
  2025. unsigned long resplen, n;
  2026. const unsigned char *p;
  2027. n = s->method->ssl_get_message(s,
  2028. SSL3_ST_CR_CERT_STATUS_A,
  2029. SSL3_ST_CR_CERT_STATUS_B,
  2030. -1, 16384, &ok);
  2031. if (!ok)
  2032. return ((int)n);
  2033. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
  2034. /*
  2035. * The CertificateStatus message is optional even if
  2036. * tlsext_status_expected is set
  2037. */
  2038. s->s3->tmp.reuse_message = 1;
  2039. } else {
  2040. if (n < 4) {
  2041. /* need at least status type + length */
  2042. al = SSL_AD_DECODE_ERROR;
  2043. SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
  2044. goto f_err;
  2045. }
  2046. p = (unsigned char *)s->init_msg;
  2047. if (*p++ != TLSEXT_STATUSTYPE_ocsp) {
  2048. al = SSL_AD_DECODE_ERROR;
  2049. SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
  2050. goto f_err;
  2051. }
  2052. n2l3(p, resplen);
  2053. if (resplen + 4 != n) {
  2054. al = SSL_AD_DECODE_ERROR;
  2055. SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
  2056. goto f_err;
  2057. }
  2058. s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
  2059. if (s->tlsext_ocsp_resp == NULL) {
  2060. al = SSL_AD_INTERNAL_ERROR;
  2061. SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
  2062. goto f_err;
  2063. }
  2064. s->tlsext_ocsp_resplen = resplen;
  2065. }
  2066. if (s->ctx->tlsext_status_cb) {
  2067. int ret;
  2068. ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
  2069. if (ret == 0) {
  2070. al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
  2071. SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
  2072. goto f_err;
  2073. }
  2074. if (ret < 0) {
  2075. al = SSL_AD_INTERNAL_ERROR;
  2076. SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
  2077. goto f_err;
  2078. }
  2079. }
  2080. return 1;
  2081. f_err:
  2082. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  2083. s->state = SSL_ST_ERR;
  2084. return (-1);
  2085. }
  2086. #endif
  2087. int ssl3_get_server_done(SSL *s)
  2088. {
  2089. int ok, ret = 0;
  2090. long n;
  2091. /* Second to last param should be very small, like 0 :-) */
  2092. n = s->method->ssl_get_message(s,
  2093. SSL3_ST_CR_SRVR_DONE_A,
  2094. SSL3_ST_CR_SRVR_DONE_B,
  2095. SSL3_MT_SERVER_DONE, 30, &ok);
  2096. if (!ok)
  2097. return ((int)n);
  2098. if (n > 0) {
  2099. /* should contain no data */
  2100. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  2101. SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
  2102. s->state = SSL_ST_ERR;
  2103. return -1;
  2104. }
  2105. ret = 1;
  2106. return (ret);
  2107. }
  2108. int ssl3_send_client_key_exchange(SSL *s)
  2109. {
  2110. unsigned char *p, *d;
  2111. int n;
  2112. unsigned long alg_k;
  2113. #ifndef OPENSSL_NO_RSA
  2114. unsigned char *q;
  2115. EVP_PKEY *pkey = NULL;
  2116. #endif
  2117. #ifndef OPENSSL_NO_KRB5
  2118. KSSL_ERR kssl_err;
  2119. #endif /* OPENSSL_NO_KRB5 */
  2120. #ifndef OPENSSL_NO_ECDH
  2121. EC_KEY *clnt_ecdh = NULL;
  2122. const EC_POINT *srvr_ecpoint = NULL;
  2123. EVP_PKEY *srvr_pub_pkey = NULL;
  2124. unsigned char *encodedPoint = NULL;
  2125. int encoded_pt_len = 0;
  2126. BN_CTX *bn_ctx = NULL;
  2127. #endif
  2128. if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
  2129. d = (unsigned char *)s->init_buf->data;
  2130. p = &(d[4]);
  2131. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  2132. /* Fool emacs indentation */
  2133. if (0) {
  2134. }
  2135. #ifndef OPENSSL_NO_RSA
  2136. else if (alg_k & SSL_kRSA) {
  2137. RSA *rsa;
  2138. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  2139. if (s->session->sess_cert == NULL) {
  2140. /*
  2141. * We should always have a server certificate with SSL_kRSA.
  2142. */
  2143. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2144. ERR_R_INTERNAL_ERROR);
  2145. goto err;
  2146. }
  2147. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  2148. rsa = s->session->sess_cert->peer_rsa_tmp;
  2149. else {
  2150. pkey =
  2151. X509_get_pubkey(s->session->
  2152. sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].
  2153. x509);
  2154. if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
  2155. || (pkey->pkey.rsa == NULL)) {
  2156. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2157. ERR_R_INTERNAL_ERROR);
  2158. EVP_PKEY_free(pkey);
  2159. goto err;
  2160. }
  2161. rsa = pkey->pkey.rsa;
  2162. EVP_PKEY_free(pkey);
  2163. }
  2164. tmp_buf[0] = s->client_version >> 8;
  2165. tmp_buf[1] = s->client_version & 0xff;
  2166. if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
  2167. goto err;
  2168. s->session->master_key_length = sizeof tmp_buf;
  2169. q = p;
  2170. /* Fix buf for TLS and beyond */
  2171. if (s->version > SSL3_VERSION)
  2172. p += 2;
  2173. n = RSA_public_encrypt(sizeof tmp_buf,
  2174. tmp_buf, p, rsa, RSA_PKCS1_PADDING);
  2175. # ifdef PKCS1_CHECK
  2176. if (s->options & SSL_OP_PKCS1_CHECK_1)
  2177. p[1]++;
  2178. if (s->options & SSL_OP_PKCS1_CHECK_2)
  2179. tmp_buf[0] = 0x70;
  2180. # endif
  2181. if (n <= 0) {
  2182. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2183. SSL_R_BAD_RSA_ENCRYPT);
  2184. goto err;
  2185. }
  2186. /* Fix buf for TLS and beyond */
  2187. if (s->version > SSL3_VERSION) {
  2188. s2n(n, q);
  2189. n += 2;
  2190. }
  2191. s->session->master_key_length =
  2192. s->method->ssl3_enc->generate_master_secret(s,
  2193. s->
  2194. session->master_key,
  2195. tmp_buf,
  2196. sizeof tmp_buf);
  2197. OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
  2198. }
  2199. #endif
  2200. #ifndef OPENSSL_NO_KRB5
  2201. else if (alg_k & SSL_kKRB5) {
  2202. krb5_error_code krb5rc;
  2203. KSSL_CTX *kssl_ctx = s->kssl_ctx;
  2204. /* krb5_data krb5_ap_req; */
  2205. krb5_data *enc_ticket;
  2206. krb5_data authenticator, *authp = NULL;
  2207. EVP_CIPHER_CTX ciph_ctx;
  2208. const EVP_CIPHER *enc = NULL;
  2209. unsigned char iv[EVP_MAX_IV_LENGTH];
  2210. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  2211. unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH];
  2212. int padl, outl = sizeof(epms);
  2213. EVP_CIPHER_CTX_init(&ciph_ctx);
  2214. # ifdef KSSL_DEBUG
  2215. fprintf(stderr, "ssl3_send_client_key_exchange(%lx & %lx)\n",
  2216. alg_k, SSL_kKRB5);
  2217. # endif /* KSSL_DEBUG */
  2218. authp = NULL;
  2219. # ifdef KRB5SENDAUTH
  2220. if (KRB5SENDAUTH)
  2221. authp = &authenticator;
  2222. # endif /* KRB5SENDAUTH */
  2223. krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err);
  2224. enc = kssl_map_enc(kssl_ctx->enctype);
  2225. if (enc == NULL)
  2226. goto err;
  2227. # ifdef KSSL_DEBUG
  2228. {
  2229. fprintf(stderr, "kssl_cget_tkt rtn %d\n", krb5rc);
  2230. if (krb5rc && kssl_err.text)
  2231. fprintf(stderr, "kssl_cget_tkt kssl_err=%s\n",
  2232. kssl_err.text);
  2233. }
  2234. # endif /* KSSL_DEBUG */
  2235. if (krb5rc) {
  2236. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  2237. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason);
  2238. goto err;
  2239. }
  2240. /*-
  2241. * 20010406 VRS - Earlier versions used KRB5 AP_REQ
  2242. * in place of RFC 2712 KerberosWrapper, as in:
  2243. *
  2244. * Send ticket (copy to *p, set n = length)
  2245. * n = krb5_ap_req.length;
  2246. * memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
  2247. * if (krb5_ap_req.data)
  2248. * kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
  2249. *
  2250. * Now using real RFC 2712 KerberosWrapper
  2251. * (Thanks to Simon Wilkinson <[email protected]>)
  2252. * Note: 2712 "opaque" types are here replaced
  2253. * with a 2-byte length followed by the value.
  2254. * Example:
  2255. * KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
  2256. * Where "xx xx" = length bytes. Shown here with
  2257. * optional authenticator omitted.
  2258. */
  2259. /* KerberosWrapper.Ticket */
  2260. s2n(enc_ticket->length, p);
  2261. memcpy(p, enc_ticket->data, enc_ticket->length);
  2262. p += enc_ticket->length;
  2263. n = enc_ticket->length + 2;
  2264. /* KerberosWrapper.Authenticator */
  2265. if (authp && authp->length) {
  2266. s2n(authp->length, p);
  2267. memcpy(p, authp->data, authp->length);
  2268. p += authp->length;
  2269. n += authp->length + 2;
  2270. free(authp->data);
  2271. authp->data = NULL;
  2272. authp->length = 0;
  2273. } else {
  2274. s2n(0, p); /* null authenticator length */
  2275. n += 2;
  2276. }
  2277. tmp_buf[0] = s->client_version >> 8;
  2278. tmp_buf[1] = s->client_version & 0xff;
  2279. if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
  2280. goto err;
  2281. /*-
  2282. * 20010420 VRS. Tried it this way; failed.
  2283. * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
  2284. * EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
  2285. * kssl_ctx->length);
  2286. * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
  2287. */
  2288. memset(iv, 0, sizeof iv); /* per RFC 1510 */
  2289. EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv);
  2290. EVP_EncryptUpdate(&ciph_ctx, epms, &outl, tmp_buf,
  2291. sizeof tmp_buf);
  2292. EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl);
  2293. outl += padl;
  2294. if (outl > (int)sizeof epms) {
  2295. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2296. ERR_R_INTERNAL_ERROR);
  2297. goto err;
  2298. }
  2299. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  2300. /* KerberosWrapper.EncryptedPreMasterSecret */
  2301. s2n(outl, p);
  2302. memcpy(p, epms, outl);
  2303. p += outl;
  2304. n += outl + 2;
  2305. s->session->master_key_length =
  2306. s->method->ssl3_enc->generate_master_secret(s,
  2307. s->
  2308. session->master_key,
  2309. tmp_buf,
  2310. sizeof tmp_buf);
  2311. OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
  2312. OPENSSL_cleanse(epms, outl);
  2313. }
  2314. #endif
  2315. #ifndef OPENSSL_NO_DH
  2316. else if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
  2317. DH *dh_srvr, *dh_clnt;
  2318. if (s->session->sess_cert == NULL) {
  2319. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  2320. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2321. SSL_R_UNEXPECTED_MESSAGE);
  2322. goto err;
  2323. }
  2324. if (s->session->sess_cert->peer_dh_tmp != NULL)
  2325. dh_srvr = s->session->sess_cert->peer_dh_tmp;
  2326. else {
  2327. /* we get them from the cert */
  2328. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  2329. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2330. SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
  2331. goto err;
  2332. }
  2333. /* generate a new random key */
  2334. if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
  2335. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
  2336. goto err;
  2337. }
  2338. if (!DH_generate_key(dh_clnt)) {
  2339. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
  2340. DH_free(dh_clnt);
  2341. goto err;
  2342. }
  2343. /*
  2344. * use the 'p' output buffer for the DH key, but make sure to
  2345. * clear it out afterwards
  2346. */
  2347. n = DH_compute_key(p, dh_srvr->pub_key, dh_clnt);
  2348. if (n <= 0) {
  2349. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
  2350. DH_free(dh_clnt);
  2351. goto err;
  2352. }
  2353. /* generate master key from the result */
  2354. s->session->master_key_length =
  2355. s->method->ssl3_enc->generate_master_secret(s,
  2356. s->
  2357. session->master_key,
  2358. p, n);
  2359. /* clean up */
  2360. memset(p, 0, n);
  2361. /* send off the data */
  2362. n = BN_num_bytes(dh_clnt->pub_key);
  2363. s2n(n, p);
  2364. BN_bn2bin(dh_clnt->pub_key, p);
  2365. n += 2;
  2366. DH_free(dh_clnt);
  2367. }
  2368. #endif
  2369. #ifndef OPENSSL_NO_ECDH
  2370. else if (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)) {
  2371. const EC_GROUP *srvr_group = NULL;
  2372. EC_KEY *tkey;
  2373. int ecdh_clnt_cert = 0;
  2374. int field_size = 0;
  2375. if (s->session->sess_cert == NULL) {
  2376. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  2377. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2378. SSL_R_UNEXPECTED_MESSAGE);
  2379. goto err;
  2380. }
  2381. /*
  2382. * Did we send out the client's ECDH share for use in premaster
  2383. * computation as part of client certificate? If so, set
  2384. * ecdh_clnt_cert to 1.
  2385. */
  2386. if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
  2387. /*-
  2388. * XXX: For now, we do not support client
  2389. * authentication using ECDH certificates.
  2390. * To add such support, one needs to add
  2391. * code that checks for appropriate
  2392. * conditions and sets ecdh_clnt_cert to 1.
  2393. * For example, the cert have an ECC
  2394. * key on the same curve as the server's
  2395. * and the key should be authorized for
  2396. * key agreement.
  2397. *
  2398. * One also needs to add code in ssl3_connect
  2399. * to skip sending the certificate verify
  2400. * message.
  2401. *
  2402. * if ((s->cert->key->privatekey != NULL) &&
  2403. * (s->cert->key->privatekey->type ==
  2404. * EVP_PKEY_EC) && ...)
  2405. * ecdh_clnt_cert = 1;
  2406. */
  2407. }
  2408. if (s->session->sess_cert->peer_ecdh_tmp != NULL) {
  2409. tkey = s->session->sess_cert->peer_ecdh_tmp;
  2410. } else {
  2411. /* Get the Server Public Key from Cert */
  2412. srvr_pub_pkey =
  2413. X509_get_pubkey(s->session->
  2414. sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  2415. if ((srvr_pub_pkey == NULL)
  2416. || (srvr_pub_pkey->type != EVP_PKEY_EC)
  2417. || (srvr_pub_pkey->pkey.ec == NULL)) {
  2418. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2419. ERR_R_INTERNAL_ERROR);
  2420. goto err;
  2421. }
  2422. tkey = srvr_pub_pkey->pkey.ec;
  2423. }
  2424. srvr_group = EC_KEY_get0_group(tkey);
  2425. srvr_ecpoint = EC_KEY_get0_public_key(tkey);
  2426. if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
  2427. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2428. ERR_R_INTERNAL_ERROR);
  2429. goto err;
  2430. }
  2431. if ((clnt_ecdh = EC_KEY_new()) == NULL) {
  2432. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2433. ERR_R_MALLOC_FAILURE);
  2434. goto err;
  2435. }
  2436. if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
  2437. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
  2438. goto err;
  2439. }
  2440. if (ecdh_clnt_cert) {
  2441. /*
  2442. * Reuse key info from our certificate We only need our
  2443. * private key to perform the ECDH computation.
  2444. */
  2445. const BIGNUM *priv_key;
  2446. tkey = s->cert->key->privatekey->pkey.ec;
  2447. priv_key = EC_KEY_get0_private_key(tkey);
  2448. if (priv_key == NULL) {
  2449. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2450. ERR_R_MALLOC_FAILURE);
  2451. goto err;
  2452. }
  2453. if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
  2454. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
  2455. goto err;
  2456. }
  2457. } else {
  2458. /* Generate a new ECDH key pair */
  2459. if (!(EC_KEY_generate_key(clnt_ecdh))) {
  2460. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2461. ERR_R_ECDH_LIB);
  2462. goto err;
  2463. }
  2464. }
  2465. /*
  2466. * use the 'p' output buffer for the ECDH key, but make sure to
  2467. * clear it out afterwards
  2468. */
  2469. field_size = EC_GROUP_get_degree(srvr_group);
  2470. if (field_size <= 0) {
  2471. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  2472. goto err;
  2473. }
  2474. n = ECDH_compute_key(p, (field_size + 7) / 8, srvr_ecpoint,
  2475. clnt_ecdh, NULL);
  2476. if (n <= 0) {
  2477. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  2478. goto err;
  2479. }
  2480. /* generate master key from the result */
  2481. s->session->master_key_length =
  2482. s->method->ssl3_enc->generate_master_secret(s,
  2483. s->
  2484. session->master_key,
  2485. p, n);
  2486. memset(p, 0, n); /* clean up */
  2487. if (ecdh_clnt_cert) {
  2488. /* Send empty client key exch message */
  2489. n = 0;
  2490. } else {
  2491. /*
  2492. * First check the size of encoding and allocate memory
  2493. * accordingly.
  2494. */
  2495. encoded_pt_len =
  2496. EC_POINT_point2oct(srvr_group,
  2497. EC_KEY_get0_public_key(clnt_ecdh),
  2498. POINT_CONVERSION_UNCOMPRESSED,
  2499. NULL, 0, NULL);
  2500. encodedPoint = (unsigned char *)
  2501. OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
  2502. bn_ctx = BN_CTX_new();
  2503. if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
  2504. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2505. ERR_R_MALLOC_FAILURE);
  2506. goto err;
  2507. }
  2508. /* Encode the public key */
  2509. n = EC_POINT_point2oct(srvr_group,
  2510. EC_KEY_get0_public_key(clnt_ecdh),
  2511. POINT_CONVERSION_UNCOMPRESSED,
  2512. encodedPoint, encoded_pt_len, bn_ctx);
  2513. *p = n; /* length of encoded point */
  2514. /* Encoded point will be copied here */
  2515. p += 1;
  2516. /* copy the point */
  2517. memcpy((unsigned char *)p, encodedPoint, n);
  2518. /* increment n to account for length field */
  2519. n += 1;
  2520. }
  2521. /* Free allocated memory */
  2522. BN_CTX_free(bn_ctx);
  2523. if (encodedPoint != NULL)
  2524. OPENSSL_free(encodedPoint);
  2525. if (clnt_ecdh != NULL)
  2526. EC_KEY_free(clnt_ecdh);
  2527. EVP_PKEY_free(srvr_pub_pkey);
  2528. }
  2529. #endif /* !OPENSSL_NO_ECDH */
  2530. else if (alg_k & SSL_kGOST) {
  2531. /* GOST key exchange message creation */
  2532. EVP_PKEY_CTX *pkey_ctx;
  2533. X509 *peer_cert;
  2534. size_t msglen;
  2535. unsigned int md_len;
  2536. int keytype;
  2537. unsigned char premaster_secret[32], shared_ukm[32], tmp[256];
  2538. EVP_MD_CTX *ukm_hash;
  2539. EVP_PKEY *pub_key;
  2540. /*
  2541. * Get server sertificate PKEY and create ctx from it
  2542. */
  2543. peer_cert =
  2544. s->session->
  2545. sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST01)].x509;
  2546. if (!peer_cert)
  2547. peer_cert =
  2548. s->session->
  2549. sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST94)].x509;
  2550. if (!peer_cert) {
  2551. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2552. SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
  2553. goto err;
  2554. }
  2555. pkey_ctx = EVP_PKEY_CTX_new(pub_key =
  2556. X509_get_pubkey(peer_cert), NULL);
  2557. if (pkey_ctx == NULL) {
  2558. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2559. ERR_R_MALLOC_FAILURE);
  2560. goto err;
  2561. }
  2562. /*
  2563. * If we have send a certificate, and certificate key
  2564. *
  2565. * * parameters match those of server certificate, use
  2566. * certificate key for key exchange
  2567. */
  2568. /* Otherwise, generate ephemeral key pair */
  2569. if (pkey_ctx == NULL
  2570. || EVP_PKEY_encrypt_init(pkey_ctx) <= 0
  2571. /* Generate session key */
  2572. || RAND_bytes(premaster_secret, 32) <= 0) {
  2573. EVP_PKEY_CTX_free(pkey_ctx);
  2574. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2575. ERR_R_INTERNAL_ERROR);
  2576. goto err;
  2577. }
  2578. /*
  2579. * If we have client certificate, use its secret as peer key
  2580. */
  2581. if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
  2582. if (EVP_PKEY_derive_set_peer
  2583. (pkey_ctx, s->cert->key->privatekey) <= 0) {
  2584. /*
  2585. * If there was an error - just ignore it. Ephemeral key
  2586. * * would be used
  2587. */
  2588. ERR_clear_error();
  2589. }
  2590. }
  2591. /*
  2592. * Compute shared IV and store it in algorithm-specific context
  2593. * data
  2594. */
  2595. ukm_hash = EVP_MD_CTX_create();
  2596. if (EVP_DigestInit(ukm_hash,
  2597. EVP_get_digestbynid(NID_id_GostR3411_94)) <= 0
  2598. || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
  2599. SSL3_RANDOM_SIZE) <= 0
  2600. || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
  2601. SSL3_RANDOM_SIZE) <= 0
  2602. || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
  2603. EVP_MD_CTX_destroy(ukm_hash);
  2604. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2605. ERR_R_INTERNAL_ERROR);
  2606. goto err;
  2607. }
  2608. EVP_MD_CTX_destroy(ukm_hash);
  2609. if (EVP_PKEY_CTX_ctrl
  2610. (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
  2611. shared_ukm) < 0) {
  2612. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2613. SSL_R_LIBRARY_BUG);
  2614. goto err;
  2615. }
  2616. /* Make GOST keytransport blob message */
  2617. /*
  2618. * Encapsulate it into sequence
  2619. */
  2620. *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
  2621. msglen = 255;
  2622. if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, premaster_secret, 32)
  2623. <= 0) {
  2624. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2625. SSL_R_LIBRARY_BUG);
  2626. goto err;
  2627. }
  2628. if (msglen >= 0x80) {
  2629. *(p++) = 0x81;
  2630. *(p++) = msglen & 0xff;
  2631. n = msglen + 3;
  2632. } else {
  2633. *(p++) = msglen & 0xff;
  2634. n = msglen + 2;
  2635. }
  2636. memcpy(p, tmp, msglen);
  2637. /* Check if pubkey from client certificate was used */
  2638. if (EVP_PKEY_CTX_ctrl
  2639. (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
  2640. /* Set flag "skip certificate verify" */
  2641. s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
  2642. }
  2643. EVP_PKEY_CTX_free(pkey_ctx);
  2644. s->session->master_key_length =
  2645. s->method->ssl3_enc->generate_master_secret(s,
  2646. s->
  2647. session->master_key,
  2648. premaster_secret,
  2649. 32);
  2650. EVP_PKEY_free(pub_key);
  2651. }
  2652. #ifndef OPENSSL_NO_SRP
  2653. else if (alg_k & SSL_kSRP) {
  2654. if (s->srp_ctx.A != NULL) {
  2655. /* send off the data */
  2656. n = BN_num_bytes(s->srp_ctx.A);
  2657. s2n(n, p);
  2658. BN_bn2bin(s->srp_ctx.A, p);
  2659. n += 2;
  2660. } else {
  2661. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2662. ERR_R_INTERNAL_ERROR);
  2663. goto err;
  2664. }
  2665. if (s->session->srp_username != NULL)
  2666. OPENSSL_free(s->session->srp_username);
  2667. s->session->srp_username = BUF_strdup(s->srp_ctx.login);
  2668. if (s->session->srp_username == NULL) {
  2669. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2670. ERR_R_MALLOC_FAILURE);
  2671. goto err;
  2672. }
  2673. if ((s->session->master_key_length =
  2674. SRP_generate_client_master_secret(s,
  2675. s->session->master_key)) <
  2676. 0) {
  2677. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2678. ERR_R_INTERNAL_ERROR);
  2679. goto err;
  2680. }
  2681. }
  2682. #endif
  2683. #ifndef OPENSSL_NO_PSK
  2684. else if (alg_k & SSL_kPSK) {
  2685. /*
  2686. * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
  2687. * \0-terminated identity. The last byte is for us for simulating
  2688. * strnlen.
  2689. */
  2690. char identity[PSK_MAX_IDENTITY_LEN + 2];
  2691. size_t identity_len;
  2692. unsigned char *t = NULL;
  2693. unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
  2694. unsigned int pre_ms_len = 0, psk_len = 0;
  2695. int psk_err = 1;
  2696. n = 0;
  2697. if (s->psk_client_callback == NULL) {
  2698. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2699. SSL_R_PSK_NO_CLIENT_CB);
  2700. goto err;
  2701. }
  2702. memset(identity, 0, sizeof(identity));
  2703. psk_len = s->psk_client_callback(s, s->session->psk_identity_hint,
  2704. identity, sizeof(identity) - 1,
  2705. psk_or_pre_ms,
  2706. sizeof(psk_or_pre_ms));
  2707. if (psk_len > PSK_MAX_PSK_LEN) {
  2708. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2709. ERR_R_INTERNAL_ERROR);
  2710. goto psk_err;
  2711. } else if (psk_len == 0) {
  2712. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2713. SSL_R_PSK_IDENTITY_NOT_FOUND);
  2714. goto psk_err;
  2715. }
  2716. identity[PSK_MAX_IDENTITY_LEN + 1] = '\0';
  2717. identity_len = strlen(identity);
  2718. if (identity_len > PSK_MAX_IDENTITY_LEN) {
  2719. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2720. ERR_R_INTERNAL_ERROR);
  2721. goto psk_err;
  2722. }
  2723. /* create PSK pre_master_secret */
  2724. pre_ms_len = 2 + psk_len + 2 + psk_len;
  2725. t = psk_or_pre_ms;
  2726. memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
  2727. s2n(psk_len, t);
  2728. memset(t, 0, psk_len);
  2729. t += psk_len;
  2730. s2n(psk_len, t);
  2731. if (s->session->psk_identity_hint != NULL)
  2732. OPENSSL_free(s->session->psk_identity_hint);
  2733. s->session->psk_identity_hint =
  2734. BUF_strdup(s->ctx->psk_identity_hint);
  2735. if (s->ctx->psk_identity_hint != NULL
  2736. && s->session->psk_identity_hint == NULL) {
  2737. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2738. ERR_R_MALLOC_FAILURE);
  2739. goto psk_err;
  2740. }
  2741. if (s->session->psk_identity != NULL)
  2742. OPENSSL_free(s->session->psk_identity);
  2743. s->session->psk_identity = BUF_strdup(identity);
  2744. if (s->session->psk_identity == NULL) {
  2745. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2746. ERR_R_MALLOC_FAILURE);
  2747. goto psk_err;
  2748. }
  2749. s->session->master_key_length =
  2750. s->method->ssl3_enc->generate_master_secret(s,
  2751. s->
  2752. session->master_key,
  2753. psk_or_pre_ms,
  2754. pre_ms_len);
  2755. s2n(identity_len, p);
  2756. memcpy(p, identity, identity_len);
  2757. n = 2 + identity_len;
  2758. psk_err = 0;
  2759. psk_err:
  2760. OPENSSL_cleanse(identity, sizeof(identity));
  2761. OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
  2762. if (psk_err != 0) {
  2763. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  2764. goto err;
  2765. }
  2766. }
  2767. #endif
  2768. else {
  2769. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  2770. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  2771. goto err;
  2772. }
  2773. *(d++) = SSL3_MT_CLIENT_KEY_EXCHANGE;
  2774. l2n3(n, d);
  2775. s->state = SSL3_ST_CW_KEY_EXCH_B;
  2776. /* number of bytes to write */
  2777. s->init_num = n + 4;
  2778. s->init_off = 0;
  2779. }
  2780. /* SSL3_ST_CW_KEY_EXCH_B */
  2781. return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
  2782. err:
  2783. #ifndef OPENSSL_NO_ECDH
  2784. BN_CTX_free(bn_ctx);
  2785. if (encodedPoint != NULL)
  2786. OPENSSL_free(encodedPoint);
  2787. if (clnt_ecdh != NULL)
  2788. EC_KEY_free(clnt_ecdh);
  2789. EVP_PKEY_free(srvr_pub_pkey);
  2790. #endif
  2791. s->state = SSL_ST_ERR;
  2792. return (-1);
  2793. }
  2794. int ssl3_send_client_verify(SSL *s)
  2795. {
  2796. unsigned char *p, *d;
  2797. unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
  2798. EVP_PKEY *pkey;
  2799. EVP_PKEY_CTX *pctx = NULL;
  2800. EVP_MD_CTX mctx;
  2801. unsigned u = 0;
  2802. unsigned long n;
  2803. int j;
  2804. EVP_MD_CTX_init(&mctx);
  2805. if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
  2806. d = (unsigned char *)s->init_buf->data;
  2807. p = &(d[4]);
  2808. pkey = s->cert->key->privatekey;
  2809. /* Create context from key and test if sha1 is allowed as digest */
  2810. pctx = EVP_PKEY_CTX_new(pkey, NULL);
  2811. if (pctx == NULL || EVP_PKEY_sign_init(pctx) <= 0) {
  2812. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
  2813. goto err;
  2814. }
  2815. if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
  2816. if (TLS1_get_version(s) < TLS1_2_VERSION)
  2817. s->method->ssl3_enc->cert_verify_mac(s,
  2818. NID_sha1,
  2819. &(data
  2820. [MD5_DIGEST_LENGTH]));
  2821. } else {
  2822. ERR_clear_error();
  2823. }
  2824. /*
  2825. * For TLS v1.2 send signature algorithm and signature using agreed
  2826. * digest and cached handshake records.
  2827. */
  2828. if (TLS1_get_version(s) >= TLS1_2_VERSION) {
  2829. long hdatalen = 0;
  2830. void *hdata;
  2831. const EVP_MD *md = s->cert->key->digest;
  2832. hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
  2833. if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
  2834. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
  2835. goto err;
  2836. }
  2837. p += 2;
  2838. #ifdef SSL_DEBUG
  2839. fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
  2840. EVP_MD_name(md));
  2841. #endif
  2842. if (!EVP_SignInit_ex(&mctx, md, NULL)
  2843. || !EVP_SignUpdate(&mctx, hdata, hdatalen)
  2844. || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
  2845. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
  2846. goto err;
  2847. }
  2848. s2n(u, p);
  2849. n = u + 4;
  2850. if (!ssl3_digest_cached_records(s))
  2851. goto err;
  2852. } else
  2853. #ifndef OPENSSL_NO_RSA
  2854. if (pkey->type == EVP_PKEY_RSA) {
  2855. s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
  2856. if (RSA_sign(NID_md5_sha1, data,
  2857. MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
  2858. &(p[2]), &u, pkey->pkey.rsa) <= 0) {
  2859. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
  2860. goto err;
  2861. }
  2862. s2n(u, p);
  2863. n = u + 2;
  2864. } else
  2865. #endif
  2866. #ifndef OPENSSL_NO_DSA
  2867. if (pkey->type == EVP_PKEY_DSA) {
  2868. if (!DSA_sign(pkey->save_type,
  2869. &(data[MD5_DIGEST_LENGTH]),
  2870. SHA_DIGEST_LENGTH, &(p[2]),
  2871. (unsigned int *)&j, pkey->pkey.dsa)) {
  2872. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
  2873. goto err;
  2874. }
  2875. s2n(j, p);
  2876. n = j + 2;
  2877. } else
  2878. #endif
  2879. #ifndef OPENSSL_NO_ECDSA
  2880. if (pkey->type == EVP_PKEY_EC) {
  2881. if (!ECDSA_sign(pkey->save_type,
  2882. &(data[MD5_DIGEST_LENGTH]),
  2883. SHA_DIGEST_LENGTH, &(p[2]),
  2884. (unsigned int *)&j, pkey->pkey.ec)) {
  2885. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
  2886. goto err;
  2887. }
  2888. s2n(j, p);
  2889. n = j + 2;
  2890. } else
  2891. #endif
  2892. if (pkey->type == NID_id_GostR3410_94
  2893. || pkey->type == NID_id_GostR3410_2001) {
  2894. unsigned char signbuf[64];
  2895. int i;
  2896. size_t sigsize = 64;
  2897. s->method->ssl3_enc->cert_verify_mac(s,
  2898. NID_id_GostR3411_94, data);
  2899. if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
  2900. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
  2901. goto err;
  2902. }
  2903. for (i = 63, j = 0; i >= 0; j++, i--) {
  2904. p[2 + j] = signbuf[i];
  2905. }
  2906. s2n(j, p);
  2907. n = j + 2;
  2908. } else {
  2909. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
  2910. goto err;
  2911. }
  2912. *(d++) = SSL3_MT_CERTIFICATE_VERIFY;
  2913. l2n3(n, d);
  2914. s->state = SSL3_ST_CW_CERT_VRFY_B;
  2915. s->init_num = (int)n + 4;
  2916. s->init_off = 0;
  2917. }
  2918. EVP_MD_CTX_cleanup(&mctx);
  2919. EVP_PKEY_CTX_free(pctx);
  2920. return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
  2921. err:
  2922. EVP_MD_CTX_cleanup(&mctx);
  2923. EVP_PKEY_CTX_free(pctx);
  2924. s->state = SSL_ST_ERR;
  2925. return (-1);
  2926. }
  2927. int ssl3_send_client_certificate(SSL *s)
  2928. {
  2929. X509 *x509 = NULL;
  2930. EVP_PKEY *pkey = NULL;
  2931. int i;
  2932. unsigned long l;
  2933. if (s->state == SSL3_ST_CW_CERT_A) {
  2934. if ((s->cert == NULL) ||
  2935. (s->cert->key->x509 == NULL) ||
  2936. (s->cert->key->privatekey == NULL))
  2937. s->state = SSL3_ST_CW_CERT_B;
  2938. else
  2939. s->state = SSL3_ST_CW_CERT_C;
  2940. }
  2941. /* We need to get a client cert */
  2942. if (s->state == SSL3_ST_CW_CERT_B) {
  2943. /*
  2944. * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
  2945. * return(-1); We then get retied later
  2946. */
  2947. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  2948. if (i < 0) {
  2949. s->rwstate = SSL_X509_LOOKUP;
  2950. return (-1);
  2951. }
  2952. s->rwstate = SSL_NOTHING;
  2953. if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
  2954. s->state = SSL3_ST_CW_CERT_B;
  2955. if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
  2956. i = 0;
  2957. } else if (i == 1) {
  2958. i = 0;
  2959. SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
  2960. SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  2961. }
  2962. if (x509 != NULL)
  2963. X509_free(x509);
  2964. if (pkey != NULL)
  2965. EVP_PKEY_free(pkey);
  2966. if (i == 0) {
  2967. if (s->version == SSL3_VERSION) {
  2968. s->s3->tmp.cert_req = 0;
  2969. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
  2970. return (1);
  2971. } else {
  2972. s->s3->tmp.cert_req = 2;
  2973. }
  2974. }
  2975. /* Ok, we have a cert */
  2976. s->state = SSL3_ST_CW_CERT_C;
  2977. }
  2978. if (s->state == SSL3_ST_CW_CERT_C) {
  2979. s->state = SSL3_ST_CW_CERT_D;
  2980. l = ssl3_output_cert_chain(s,
  2981. (s->s3->tmp.cert_req ==
  2982. 2) ? NULL : s->cert->key->x509);
  2983. if (!l) {
  2984. SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
  2985. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  2986. s->state = SSL_ST_ERR;
  2987. return 0;
  2988. }
  2989. s->init_num = (int)l;
  2990. s->init_off = 0;
  2991. }
  2992. /* SSL3_ST_CW_CERT_D */
  2993. return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
  2994. }
  2995. #define has_bits(i,m) (((i)&(m)) == (m))
  2996. int ssl3_check_cert_and_algorithm(SSL *s)
  2997. {
  2998. int i, idx;
  2999. long alg_k, alg_a;
  3000. EVP_PKEY *pkey = NULL;
  3001. int pkey_bits;
  3002. SESS_CERT *sc;
  3003. #ifndef OPENSSL_NO_RSA
  3004. RSA *rsa;
  3005. #endif
  3006. #ifndef OPENSSL_NO_DH
  3007. DH *dh;
  3008. #endif
  3009. int al = SSL_AD_HANDSHAKE_FAILURE;
  3010. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  3011. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  3012. /* we don't have a certificate */
  3013. if ((alg_a & (SSL_aDH | SSL_aNULL | SSL_aKRB5)) || (alg_k & SSL_kPSK))
  3014. return (1);
  3015. sc = s->session->sess_cert;
  3016. if (sc == NULL) {
  3017. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
  3018. goto err;
  3019. }
  3020. #ifndef OPENSSL_NO_RSA
  3021. rsa = s->session->sess_cert->peer_rsa_tmp;
  3022. #endif
  3023. #ifndef OPENSSL_NO_DH
  3024. dh = s->session->sess_cert->peer_dh_tmp;
  3025. #endif
  3026. /* This is the passed certificate */
  3027. idx = sc->peer_cert_type;
  3028. #ifndef OPENSSL_NO_ECDH
  3029. if (idx == SSL_PKEY_ECC) {
  3030. if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
  3031. /* check failed */
  3032. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
  3033. goto f_err;
  3034. } else {
  3035. return 1;
  3036. }
  3037. }
  3038. #endif
  3039. pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
  3040. pkey_bits = EVP_PKEY_bits(pkey);
  3041. i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
  3042. EVP_PKEY_free(pkey);
  3043. /* Check that we have a certificate if we require one */
  3044. if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
  3045. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3046. SSL_R_MISSING_RSA_SIGNING_CERT);
  3047. goto f_err;
  3048. }
  3049. #ifndef OPENSSL_NO_DSA
  3050. else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
  3051. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3052. SSL_R_MISSING_DSA_SIGNING_CERT);
  3053. goto f_err;
  3054. }
  3055. #endif
  3056. #ifndef OPENSSL_NO_RSA
  3057. if (alg_k & SSL_kRSA) {
  3058. if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  3059. !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
  3060. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3061. SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  3062. goto f_err;
  3063. } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
  3064. if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
  3065. if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
  3066. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3067. SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  3068. goto f_err;
  3069. }
  3070. if (rsa != NULL) {
  3071. /* server key exchange is not allowed. */
  3072. al = SSL_AD_INTERNAL_ERROR;
  3073. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
  3074. goto f_err;
  3075. }
  3076. }
  3077. }
  3078. }
  3079. #endif
  3080. #ifndef OPENSSL_NO_DH
  3081. if ((alg_k & SSL_kEDH) && dh == NULL) {
  3082. al = SSL_AD_INTERNAL_ERROR;
  3083. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
  3084. goto f_err;
  3085. }
  3086. if ((alg_k & SSL_kDHr) && !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
  3087. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3088. SSL_R_MISSING_DH_RSA_CERT);
  3089. goto f_err;
  3090. }
  3091. # ifndef OPENSSL_NO_DSA
  3092. if ((alg_k & SSL_kDHd) && !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
  3093. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3094. SSL_R_MISSING_DH_DSA_CERT);
  3095. goto f_err;
  3096. }
  3097. # endif
  3098. /* Check DHE only: static DH not implemented. */
  3099. if (alg_k & SSL_kEDH) {
  3100. int dh_size = BN_num_bits(dh->p);
  3101. if ((!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && dh_size < 1024)
  3102. || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && dh_size < 512)) {
  3103. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_DH_KEY_TOO_SMALL);
  3104. goto f_err;
  3105. }
  3106. }
  3107. #endif /* !OPENSSL_NO_DH */
  3108. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  3109. pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
  3110. #ifndef OPENSSL_NO_RSA
  3111. if (alg_k & SSL_kRSA) {
  3112. if (rsa == NULL) {
  3113. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3114. SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
  3115. goto f_err;
  3116. } else if (BN_num_bits(rsa->n) >
  3117. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
  3118. /* We have a temporary RSA key but it's too large. */
  3119. al = SSL_AD_EXPORT_RESTRICTION;
  3120. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3121. SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
  3122. goto f_err;
  3123. }
  3124. } else
  3125. #endif
  3126. #ifndef OPENSSL_NO_DH
  3127. if (alg_k & SSL_kEDH) {
  3128. if (BN_num_bits(dh->p) >
  3129. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
  3130. /* We have a temporary DH key but it's too large. */
  3131. al = SSL_AD_EXPORT_RESTRICTION;
  3132. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3133. SSL_R_MISSING_EXPORT_TMP_DH_KEY);
  3134. goto f_err;
  3135. }
  3136. } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
  3137. /* The cert should have had an export DH key. */
  3138. al = SSL_AD_EXPORT_RESTRICTION;
  3139. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3140. SSL_R_MISSING_EXPORT_TMP_DH_KEY);
  3141. goto f_err;
  3142. } else
  3143. #endif
  3144. {
  3145. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  3146. SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  3147. goto f_err;
  3148. }
  3149. }
  3150. return (1);
  3151. f_err:
  3152. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  3153. err:
  3154. return (0);
  3155. }
  3156. #ifndef OPENSSL_NO_TLSEXT
  3157. /*
  3158. * Normally, we can tell if the server is resuming the session from
  3159. * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
  3160. * message after the ServerHello to determine if the server is resuming.
  3161. * Therefore, we allow EAP-FAST to peek ahead.
  3162. * ssl3_check_finished returns 1 if we are resuming from an external
  3163. * pre-shared secret, we have a "ticket" and the next server handshake message
  3164. * is Finished; and 0 otherwise. It returns -1 upon an error.
  3165. */
  3166. static int ssl3_check_finished(SSL *s)
  3167. {
  3168. int ok = 0;
  3169. if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
  3170. !s->session->tlsext_tick)
  3171. return 0;
  3172. /* Need to permit this temporarily, in case the next message is Finished. */
  3173. s->s3->flags |= SSL3_FLAGS_CCS_OK;
  3174. /*
  3175. * This function is called when we might get a Certificate message instead,
  3176. * so permit appropriate message length.
  3177. * We ignore the return value as we're only interested in the message type
  3178. * and not its length.
  3179. */
  3180. s->method->ssl_get_message(s,
  3181. SSL3_ST_CR_CERT_A,
  3182. SSL3_ST_CR_CERT_B,
  3183. -1, s->max_cert_list, &ok);
  3184. s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
  3185. if (!ok)
  3186. return -1;
  3187. s->s3->tmp.reuse_message = 1;
  3188. if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
  3189. return 1;
  3190. /* If we're not done, then the CCS arrived early and we should bail. */
  3191. if (s->s3->change_cipher_spec) {
  3192. SSLerr(SSL_F_SSL3_CHECK_FINISHED, SSL_R_CCS_RECEIVED_EARLY);
  3193. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  3194. return -1;
  3195. }
  3196. return 0;
  3197. }
  3198. # ifndef OPENSSL_NO_NEXTPROTONEG
  3199. int ssl3_send_next_proto(SSL *s)
  3200. {
  3201. unsigned int len, padding_len;
  3202. unsigned char *d;
  3203. if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
  3204. len = s->next_proto_negotiated_len;
  3205. padding_len = 32 - ((len + 2) % 32);
  3206. d = (unsigned char *)s->init_buf->data;
  3207. d[4] = len;
  3208. memcpy(d + 5, s->next_proto_negotiated, len);
  3209. d[5 + len] = padding_len;
  3210. memset(d + 6 + len, 0, padding_len);
  3211. *(d++) = SSL3_MT_NEXT_PROTO;
  3212. l2n3(2 + len + padding_len, d);
  3213. s->state = SSL3_ST_CW_NEXT_PROTO_B;
  3214. s->init_num = 4 + 2 + len + padding_len;
  3215. s->init_off = 0;
  3216. }
  3217. return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
  3218. }
  3219. #endif /* !OPENSSL_NO_NEXTPROTONEG */
  3220. #endif /* !OPENSSL_NO_TLSEXT */
  3221. int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
  3222. {
  3223. int i = 0;
  3224. #ifndef OPENSSL_NO_ENGINE
  3225. if (s->ctx->client_cert_engine) {
  3226. i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
  3227. SSL_get_client_CA_list(s),
  3228. px509, ppkey, NULL, NULL, NULL);
  3229. if (i != 0)
  3230. return i;
  3231. }
  3232. #endif
  3233. if (s->ctx->client_cert_cb)
  3234. i = s->ctx->client_cert_cb(s, px509, ppkey);
  3235. return i;
  3236. }