s3_clnt.c 125 KB

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