s3_clnt.c 126 KB

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