s3_srvr.c 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649
  1. /* ssl/s3_srvr.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. #define REUSE_CIPHER_BUG
  151. #define NETSCAPE_HANG_BUG
  152. #include <stdio.h>
  153. #include "ssl_locl.h"
  154. #include "kssl_lcl.h"
  155. #include "../crypto/constant_time_locl.h"
  156. #include <openssl/buffer.h>
  157. #include <openssl/rand.h>
  158. #include <openssl/objects.h>
  159. #include <openssl/evp.h>
  160. #include <openssl/hmac.h>
  161. #include <openssl/x509.h>
  162. #ifndef OPENSSL_NO_DH
  163. # include <openssl/dh.h>
  164. #endif
  165. #include <openssl/bn.h>
  166. #ifndef OPENSSL_NO_KRB5
  167. # include <openssl/krb5_asn.h>
  168. #endif
  169. #include <openssl/md5.h>
  170. #ifndef OPENSSL_NO_SSL3_METHOD
  171. static const SSL_METHOD *ssl3_get_server_method(int ver);
  172. static const SSL_METHOD *ssl3_get_server_method(int ver)
  173. {
  174. if (ver == SSL3_VERSION)
  175. return (SSLv3_server_method());
  176. else
  177. return (NULL);
  178. }
  179. IMPLEMENT_ssl3_meth_func(SSLv3_server_method,
  180. ssl3_accept,
  181. ssl_undefined_function, ssl3_get_server_method)
  182. #endif
  183. #ifndef OPENSSL_NO_SRP
  184. static int ssl_check_srp_ext_ClientHello(SSL *s, int *al)
  185. {
  186. int ret = SSL_ERROR_NONE;
  187. *al = SSL_AD_UNRECOGNIZED_NAME;
  188. if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
  189. (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
  190. if (s->srp_ctx.login == NULL) {
  191. /*
  192. * RFC 5054 says SHOULD reject, we do so if There is no srp
  193. * login name
  194. */
  195. ret = SSL3_AL_FATAL;
  196. *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
  197. } else {
  198. ret = SSL_srp_server_param_with_username(s, al);
  199. }
  200. }
  201. return ret;
  202. }
  203. #endif
  204. int ssl3_accept(SSL *s)
  205. {
  206. BUF_MEM *buf;
  207. unsigned long alg_k, Time = (unsigned long)time(NULL);
  208. void (*cb) (const SSL *ssl, int type, int val) = NULL;
  209. int ret = -1;
  210. int new_state, state, skip = 0;
  211. RAND_add(&Time, sizeof(Time), 0);
  212. ERR_clear_error();
  213. clear_sys_error();
  214. if (s->info_callback != NULL)
  215. cb = s->info_callback;
  216. else if (s->ctx->info_callback != NULL)
  217. cb = s->ctx->info_callback;
  218. /* init things to blank */
  219. s->in_handshake++;
  220. if (!SSL_in_init(s) || SSL_in_before(s))
  221. SSL_clear(s);
  222. if (s->cert == NULL) {
  223. SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_NO_CERTIFICATE_SET);
  224. return (-1);
  225. }
  226. #ifndef OPENSSL_NO_HEARTBEATS
  227. /*
  228. * If we're awaiting a HeartbeatResponse, pretend we already got and
  229. * don't await it anymore, because Heartbeats don't make sense during
  230. * handshakes anyway.
  231. */
  232. if (s->tlsext_hb_pending) {
  233. s->tlsext_hb_pending = 0;
  234. s->tlsext_hb_seq++;
  235. }
  236. #endif
  237. for (;;) {
  238. state = s->state;
  239. switch (s->state) {
  240. case SSL_ST_RENEGOTIATE:
  241. s->renegotiate = 1;
  242. /* s->state=SSL_ST_ACCEPT; */
  243. case SSL_ST_BEFORE:
  244. case SSL_ST_ACCEPT:
  245. case SSL_ST_BEFORE | SSL_ST_ACCEPT:
  246. case SSL_ST_OK | SSL_ST_ACCEPT:
  247. s->server = 1;
  248. if (cb != NULL)
  249. cb(s, SSL_CB_HANDSHAKE_START, 1);
  250. if ((s->version >> 8) != 3) {
  251. SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
  252. s->state = SSL_ST_ERR;
  253. return -1;
  254. }
  255. s->type = SSL_ST_ACCEPT;
  256. if (s->init_buf == NULL) {
  257. if ((buf = BUF_MEM_new()) == NULL) {
  258. ret = -1;
  259. s->state = SSL_ST_ERR;
  260. goto end;
  261. }
  262. if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
  263. BUF_MEM_free(buf);
  264. ret = -1;
  265. s->state = SSL_ST_ERR;
  266. goto end;
  267. }
  268. s->init_buf = buf;
  269. }
  270. if (!ssl3_setup_buffers(s)) {
  271. ret = -1;
  272. s->state = SSL_ST_ERR;
  273. goto end;
  274. }
  275. s->init_num = 0;
  276. s->s3->flags &= ~SSL3_FLAGS_SGC_RESTART_DONE;
  277. s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
  278. /*
  279. * Should have been reset by ssl3_get_finished, too.
  280. */
  281. s->s3->change_cipher_spec = 0;
  282. if (s->state != SSL_ST_RENEGOTIATE) {
  283. /*
  284. * Ok, we now need to push on a buffering BIO so that the
  285. * output is sent in a way that TCP likes :-)
  286. */
  287. if (!ssl_init_wbio_buffer(s, 1)) {
  288. ret = -1;
  289. s->state = SSL_ST_ERR;
  290. goto end;
  291. }
  292. ssl3_init_finished_mac(s);
  293. s->state = SSL3_ST_SR_CLNT_HELLO_A;
  294. s->ctx->stats.sess_accept++;
  295. } else if (!s->s3->send_connection_binding &&
  296. !(s->options &
  297. SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
  298. /*
  299. * Server attempting to renegotiate with client that doesn't
  300. * support secure renegotiation.
  301. */
  302. SSLerr(SSL_F_SSL3_ACCEPT,
  303. SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  304. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  305. ret = -1;
  306. s->state = SSL_ST_ERR;
  307. goto end;
  308. } else {
  309. /*
  310. * s->state == SSL_ST_RENEGOTIATE, we will just send a
  311. * HelloRequest
  312. */
  313. s->ctx->stats.sess_accept_renegotiate++;
  314. s->state = SSL3_ST_SW_HELLO_REQ_A;
  315. }
  316. break;
  317. case SSL3_ST_SW_HELLO_REQ_A:
  318. case SSL3_ST_SW_HELLO_REQ_B:
  319. s->shutdown = 0;
  320. ret = ssl3_send_hello_request(s);
  321. if (ret <= 0)
  322. goto end;
  323. s->s3->tmp.next_state = SSL3_ST_SW_HELLO_REQ_C;
  324. s->state = SSL3_ST_SW_FLUSH;
  325. s->init_num = 0;
  326. ssl3_init_finished_mac(s);
  327. break;
  328. case SSL3_ST_SW_HELLO_REQ_C:
  329. s->state = SSL_ST_OK;
  330. break;
  331. case SSL3_ST_SR_CLNT_HELLO_A:
  332. case SSL3_ST_SR_CLNT_HELLO_B:
  333. case SSL3_ST_SR_CLNT_HELLO_C:
  334. s->shutdown = 0;
  335. if (s->rwstate != SSL_X509_LOOKUP) {
  336. ret = ssl3_get_client_hello(s);
  337. if (ret <= 0)
  338. goto end;
  339. }
  340. #ifndef OPENSSL_NO_SRP
  341. {
  342. int al;
  343. if ((ret = ssl_check_srp_ext_ClientHello(s, &al)) < 0) {
  344. /*
  345. * callback indicates firther work to be done
  346. */
  347. s->rwstate = SSL_X509_LOOKUP;
  348. goto end;
  349. }
  350. if (ret != SSL_ERROR_NONE) {
  351. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  352. /*
  353. * This is not really an error but the only means to for
  354. * a client to detect whether srp is supported.
  355. */
  356. if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY)
  357. SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_CLIENTHELLO_TLSEXT);
  358. ret = -1;
  359. s->state = SSL_ST_ERR;
  360. goto end;
  361. }
  362. }
  363. #endif
  364. s->renegotiate = 2;
  365. s->state = SSL3_ST_SW_SRVR_HELLO_A;
  366. s->init_num = 0;
  367. break;
  368. case SSL3_ST_SW_SRVR_HELLO_A:
  369. case SSL3_ST_SW_SRVR_HELLO_B:
  370. ret = ssl3_send_server_hello(s);
  371. if (ret <= 0)
  372. goto end;
  373. #ifndef OPENSSL_NO_TLSEXT
  374. if (s->hit) {
  375. if (s->tlsext_ticket_expected)
  376. s->state = SSL3_ST_SW_SESSION_TICKET_A;
  377. else
  378. s->state = SSL3_ST_SW_CHANGE_A;
  379. }
  380. #else
  381. if (s->hit)
  382. s->state = SSL3_ST_SW_CHANGE_A;
  383. #endif
  384. else
  385. s->state = SSL3_ST_SW_CERT_A;
  386. s->init_num = 0;
  387. break;
  388. case SSL3_ST_SW_CERT_A:
  389. case SSL3_ST_SW_CERT_B:
  390. /* Check if it is anon DH or anon ECDH, */
  391. /* normal PSK or KRB5 or SRP */
  392. if (!
  393. (s->s3->tmp.
  394. new_cipher->algorithm_auth & (SSL_aNULL | SSL_aKRB5 |
  395. SSL_aSRP))
  396. && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
  397. ret = ssl3_send_server_certificate(s);
  398. if (ret <= 0)
  399. goto end;
  400. #ifndef OPENSSL_NO_TLSEXT
  401. if (s->tlsext_status_expected)
  402. s->state = SSL3_ST_SW_CERT_STATUS_A;
  403. else
  404. s->state = SSL3_ST_SW_KEY_EXCH_A;
  405. } else {
  406. skip = 1;
  407. s->state = SSL3_ST_SW_KEY_EXCH_A;
  408. }
  409. #else
  410. } else
  411. skip = 1;
  412. s->state = SSL3_ST_SW_KEY_EXCH_A;
  413. #endif
  414. s->init_num = 0;
  415. break;
  416. case SSL3_ST_SW_KEY_EXCH_A:
  417. case SSL3_ST_SW_KEY_EXCH_B:
  418. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  419. /*
  420. * clear this, it may get reset by
  421. * send_server_key_exchange
  422. */
  423. s->s3->tmp.use_rsa_tmp = 0;
  424. /*
  425. * only send if a DH key exchange, fortezza or RSA but we have a
  426. * sign only certificate PSK: may send PSK identity hints For
  427. * ECC ciphersuites, we send a serverKeyExchange message only if
  428. * the cipher suite is either ECDH-anon or ECDHE. In other cases,
  429. * the server certificate contains the server's public key for
  430. * key exchange.
  431. */
  432. if (0
  433. /*
  434. * PSK: send ServerKeyExchange if PSK identity hint if
  435. * provided
  436. */
  437. #ifndef OPENSSL_NO_PSK
  438. || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
  439. #endif
  440. #ifndef OPENSSL_NO_SRP
  441. /* SRP: send ServerKeyExchange */
  442. || (alg_k & SSL_kSRP)
  443. #endif
  444. || (alg_k & (SSL_kDHr | SSL_kDHd | SSL_kEDH))
  445. || (alg_k & SSL_kEECDH)
  446. || ((alg_k & SSL_kRSA)
  447. && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
  448. || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
  449. && EVP_PKEY_size(s->cert->pkeys
  450. [SSL_PKEY_RSA_ENC].privatekey) *
  451. 8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
  452. )
  453. )
  454. )
  455. ) {
  456. ret = ssl3_send_server_key_exchange(s);
  457. if (ret <= 0)
  458. goto end;
  459. } else
  460. skip = 1;
  461. s->state = SSL3_ST_SW_CERT_REQ_A;
  462. s->init_num = 0;
  463. break;
  464. case SSL3_ST_SW_CERT_REQ_A:
  465. case SSL3_ST_SW_CERT_REQ_B:
  466. if ( /* don't request cert unless asked for it: */
  467. !(s->verify_mode & SSL_VERIFY_PEER) ||
  468. /*
  469. * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
  470. * during re-negotiation:
  471. */
  472. ((s->session->peer != NULL) &&
  473. (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
  474. /*
  475. * never request cert in anonymous ciphersuites (see
  476. * section "Certificate request" in SSL 3 drafts and in
  477. * RFC 2246):
  478. */
  479. ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
  480. /*
  481. * ... except when the application insists on
  482. * verification (against the specs, but s3_clnt.c accepts
  483. * this for SSL 3)
  484. */
  485. !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
  486. /*
  487. * never request cert in Kerberos ciphersuites
  488. */
  489. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) ||
  490. /* don't request certificate for SRP auth */
  491. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
  492. /*
  493. * With normal PSK Certificates and Certificate Requests
  494. * are omitted
  495. */
  496. || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
  497. /* no cert request */
  498. skip = 1;
  499. s->s3->tmp.cert_request = 0;
  500. s->state = SSL3_ST_SW_SRVR_DONE_A;
  501. if (s->s3->handshake_buffer) {
  502. if (!ssl3_digest_cached_records(s)) {
  503. s->state = SSL_ST_ERR;
  504. return -1;
  505. }
  506. }
  507. } else {
  508. s->s3->tmp.cert_request = 1;
  509. ret = ssl3_send_certificate_request(s);
  510. if (ret <= 0)
  511. goto end;
  512. #ifndef NETSCAPE_HANG_BUG
  513. s->state = SSL3_ST_SW_SRVR_DONE_A;
  514. #else
  515. s->state = SSL3_ST_SW_FLUSH;
  516. s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
  517. #endif
  518. s->init_num = 0;
  519. }
  520. break;
  521. case SSL3_ST_SW_SRVR_DONE_A:
  522. case SSL3_ST_SW_SRVR_DONE_B:
  523. ret = ssl3_send_server_done(s);
  524. if (ret <= 0)
  525. goto end;
  526. s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
  527. s->state = SSL3_ST_SW_FLUSH;
  528. s->init_num = 0;
  529. break;
  530. case SSL3_ST_SW_FLUSH:
  531. /*
  532. * This code originally checked to see if any data was pending
  533. * using BIO_CTRL_INFO and then flushed. This caused problems as
  534. * documented in PR#1939. The proposed fix doesn't completely
  535. * resolve this issue as buggy implementations of
  536. * BIO_CTRL_PENDING still exist. So instead we just flush
  537. * unconditionally.
  538. */
  539. s->rwstate = SSL_WRITING;
  540. if (BIO_flush(s->wbio) <= 0) {
  541. ret = -1;
  542. goto end;
  543. }
  544. s->rwstate = SSL_NOTHING;
  545. s->state = s->s3->tmp.next_state;
  546. break;
  547. case SSL3_ST_SR_CERT_A:
  548. case SSL3_ST_SR_CERT_B:
  549. /* Check for second client hello (MS SGC) */
  550. ret = ssl3_check_client_hello(s);
  551. if (ret <= 0)
  552. goto end;
  553. if (ret == 2)
  554. s->state = SSL3_ST_SR_CLNT_HELLO_C;
  555. else {
  556. if (s->s3->tmp.cert_request) {
  557. ret = ssl3_get_client_certificate(s);
  558. if (ret <= 0)
  559. goto end;
  560. }
  561. s->init_num = 0;
  562. s->state = SSL3_ST_SR_KEY_EXCH_A;
  563. }
  564. break;
  565. case SSL3_ST_SR_KEY_EXCH_A:
  566. case SSL3_ST_SR_KEY_EXCH_B:
  567. ret = ssl3_get_client_key_exchange(s);
  568. if (ret <= 0)
  569. goto end;
  570. if (ret == 2) {
  571. /*
  572. * For the ECDH ciphersuites when the client sends its ECDH
  573. * pub key in a certificate, the CertificateVerify message is
  574. * not sent. Also for GOST ciphersuites when the client uses
  575. * its key from the certificate for key exchange.
  576. */
  577. #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
  578. s->state = SSL3_ST_SR_FINISHED_A;
  579. #else
  580. if (s->s3->next_proto_neg_seen)
  581. s->state = SSL3_ST_SR_NEXT_PROTO_A;
  582. else
  583. s->state = SSL3_ST_SR_FINISHED_A;
  584. #endif
  585. s->init_num = 0;
  586. } else if (TLS1_get_version(s) >= TLS1_2_VERSION) {
  587. s->state = SSL3_ST_SR_CERT_VRFY_A;
  588. s->init_num = 0;
  589. if (!s->session->peer)
  590. break;
  591. /*
  592. * For TLS v1.2 freeze the handshake buffer at this point and
  593. * digest cached records.
  594. */
  595. if (!s->s3->handshake_buffer) {
  596. SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
  597. s->state = SSL_ST_ERR;
  598. return -1;
  599. }
  600. s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE;
  601. if (!ssl3_digest_cached_records(s)) {
  602. s->state = SSL_ST_ERR;
  603. return -1;
  604. }
  605. } else {
  606. int offset = 0;
  607. int dgst_num;
  608. s->state = SSL3_ST_SR_CERT_VRFY_A;
  609. s->init_num = 0;
  610. /*
  611. * We need to get hashes here so if there is a client cert,
  612. * it can be verified FIXME - digest processing for
  613. * CertificateVerify should be generalized. But it is next
  614. * step
  615. */
  616. if (s->s3->handshake_buffer) {
  617. if (!ssl3_digest_cached_records(s)) {
  618. s->state = SSL_ST_ERR;
  619. return -1;
  620. }
  621. }
  622. for (dgst_num = 0; dgst_num < SSL_MAX_DIGEST; dgst_num++)
  623. if (s->s3->handshake_dgst[dgst_num]) {
  624. int dgst_size;
  625. s->method->ssl3_enc->cert_verify_mac(s,
  626. EVP_MD_CTX_type
  627. (s->
  628. s3->handshake_dgst
  629. [dgst_num]),
  630. &(s->s3->
  631. tmp.cert_verify_md
  632. [offset]));
  633. dgst_size =
  634. EVP_MD_CTX_size(s->s3->handshake_dgst[dgst_num]);
  635. if (dgst_size < 0) {
  636. s->state = SSL_ST_ERR;
  637. ret = -1;
  638. goto end;
  639. }
  640. offset += dgst_size;
  641. }
  642. }
  643. break;
  644. case SSL3_ST_SR_CERT_VRFY_A:
  645. case SSL3_ST_SR_CERT_VRFY_B:
  646. ret = ssl3_get_cert_verify(s);
  647. if (ret <= 0)
  648. goto end;
  649. #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
  650. s->state = SSL3_ST_SR_FINISHED_A;
  651. #else
  652. if (s->s3->next_proto_neg_seen)
  653. s->state = SSL3_ST_SR_NEXT_PROTO_A;
  654. else
  655. s->state = SSL3_ST_SR_FINISHED_A;
  656. #endif
  657. s->init_num = 0;
  658. break;
  659. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  660. case SSL3_ST_SR_NEXT_PROTO_A:
  661. case SSL3_ST_SR_NEXT_PROTO_B:
  662. /*
  663. * Enable CCS for NPN. Receiving a CCS clears the flag, so make
  664. * sure not to re-enable it to ban duplicates. This *should* be the
  665. * first time we have received one - but we check anyway to be
  666. * cautious.
  667. * s->s3->change_cipher_spec is set when a CCS is
  668. * processed in s3_pkt.c, and remains set until
  669. * the client's Finished message is read.
  670. */
  671. if (!s->s3->change_cipher_spec)
  672. s->s3->flags |= SSL3_FLAGS_CCS_OK;
  673. ret = ssl3_get_next_proto(s);
  674. if (ret <= 0)
  675. goto end;
  676. s->init_num = 0;
  677. s->state = SSL3_ST_SR_FINISHED_A;
  678. break;
  679. #endif
  680. case SSL3_ST_SR_FINISHED_A:
  681. case SSL3_ST_SR_FINISHED_B:
  682. /*
  683. * Enable CCS for handshakes without NPN. In NPN the CCS flag has
  684. * already been set. Receiving a CCS clears the flag, so make
  685. * sure not to re-enable it to ban duplicates.
  686. * s->s3->change_cipher_spec is set when a CCS is
  687. * processed in s3_pkt.c, and remains set until
  688. * the client's Finished message is read.
  689. */
  690. if (!s->s3->change_cipher_spec)
  691. s->s3->flags |= SSL3_FLAGS_CCS_OK;
  692. ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A,
  693. SSL3_ST_SR_FINISHED_B);
  694. if (ret <= 0)
  695. goto end;
  696. if (s->hit)
  697. s->state = SSL_ST_OK;
  698. #ifndef OPENSSL_NO_TLSEXT
  699. else if (s->tlsext_ticket_expected)
  700. s->state = SSL3_ST_SW_SESSION_TICKET_A;
  701. #endif
  702. else
  703. s->state = SSL3_ST_SW_CHANGE_A;
  704. s->init_num = 0;
  705. break;
  706. #ifndef OPENSSL_NO_TLSEXT
  707. case SSL3_ST_SW_SESSION_TICKET_A:
  708. case SSL3_ST_SW_SESSION_TICKET_B:
  709. ret = ssl3_send_newsession_ticket(s);
  710. if (ret <= 0)
  711. goto end;
  712. s->state = SSL3_ST_SW_CHANGE_A;
  713. s->init_num = 0;
  714. break;
  715. case SSL3_ST_SW_CERT_STATUS_A:
  716. case SSL3_ST_SW_CERT_STATUS_B:
  717. ret = ssl3_send_cert_status(s);
  718. if (ret <= 0)
  719. goto end;
  720. s->state = SSL3_ST_SW_KEY_EXCH_A;
  721. s->init_num = 0;
  722. break;
  723. #endif
  724. case SSL3_ST_SW_CHANGE_A:
  725. case SSL3_ST_SW_CHANGE_B:
  726. s->session->cipher = s->s3->tmp.new_cipher;
  727. if (!s->method->ssl3_enc->setup_key_block(s)) {
  728. ret = -1;
  729. s->state = SSL_ST_ERR;
  730. goto end;
  731. }
  732. ret = ssl3_send_change_cipher_spec(s,
  733. SSL3_ST_SW_CHANGE_A,
  734. SSL3_ST_SW_CHANGE_B);
  735. if (ret <= 0)
  736. goto end;
  737. s->state = SSL3_ST_SW_FINISHED_A;
  738. s->init_num = 0;
  739. if (!s->method->ssl3_enc->change_cipher_state(s,
  740. SSL3_CHANGE_CIPHER_SERVER_WRITE))
  741. {
  742. ret = -1;
  743. s->state = SSL_ST_ERR;
  744. goto end;
  745. }
  746. break;
  747. case SSL3_ST_SW_FINISHED_A:
  748. case SSL3_ST_SW_FINISHED_B:
  749. ret = ssl3_send_finished(s,
  750. SSL3_ST_SW_FINISHED_A,
  751. SSL3_ST_SW_FINISHED_B,
  752. s->method->
  753. ssl3_enc->server_finished_label,
  754. s->method->
  755. ssl3_enc->server_finished_label_len);
  756. if (ret <= 0)
  757. goto end;
  758. s->state = SSL3_ST_SW_FLUSH;
  759. if (s->hit) {
  760. #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
  761. s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
  762. #else
  763. if (s->s3->next_proto_neg_seen) {
  764. s->s3->tmp.next_state = SSL3_ST_SR_NEXT_PROTO_A;
  765. } else
  766. s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
  767. #endif
  768. } else
  769. s->s3->tmp.next_state = SSL_ST_OK;
  770. s->init_num = 0;
  771. break;
  772. case SSL_ST_OK:
  773. /* clean a few things up */
  774. ssl3_cleanup_key_block(s);
  775. BUF_MEM_free(s->init_buf);
  776. s->init_buf = NULL;
  777. /* remove buffering on output */
  778. ssl_free_wbio_buffer(s);
  779. s->init_num = 0;
  780. if (s->renegotiate == 2) { /* skipped if we just sent a
  781. * HelloRequest */
  782. s->renegotiate = 0;
  783. s->new_session = 0;
  784. ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
  785. s->ctx->stats.sess_accept_good++;
  786. /* s->server=1; */
  787. s->handshake_func = ssl3_accept;
  788. if (cb != NULL)
  789. cb(s, SSL_CB_HANDSHAKE_DONE, 1);
  790. }
  791. ret = 1;
  792. goto end;
  793. /* break; */
  794. case SSL_ST_ERR:
  795. default:
  796. SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_UNKNOWN_STATE);
  797. ret = -1;
  798. goto end;
  799. /* break; */
  800. }
  801. if (!s->s3->tmp.reuse_message && !skip) {
  802. if (s->debug) {
  803. if ((ret = BIO_flush(s->wbio)) <= 0)
  804. goto end;
  805. }
  806. if ((cb != NULL) && (s->state != state)) {
  807. new_state = s->state;
  808. s->state = state;
  809. cb(s, SSL_CB_ACCEPT_LOOP, 1);
  810. s->state = new_state;
  811. }
  812. }
  813. skip = 0;
  814. }
  815. end:
  816. /* BIO_flush(s->wbio); */
  817. s->in_handshake--;
  818. if (cb != NULL)
  819. cb(s, SSL_CB_ACCEPT_EXIT, ret);
  820. return (ret);
  821. }
  822. int ssl3_send_hello_request(SSL *s)
  823. {
  824. unsigned char *p;
  825. if (s->state == SSL3_ST_SW_HELLO_REQ_A) {
  826. p = (unsigned char *)s->init_buf->data;
  827. *(p++) = SSL3_MT_HELLO_REQUEST;
  828. *(p++) = 0;
  829. *(p++) = 0;
  830. *(p++) = 0;
  831. s->state = SSL3_ST_SW_HELLO_REQ_B;
  832. /* number of bytes to write */
  833. s->init_num = 4;
  834. s->init_off = 0;
  835. }
  836. /* SSL3_ST_SW_HELLO_REQ_B */
  837. return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
  838. }
  839. int ssl3_check_client_hello(SSL *s)
  840. {
  841. int ok;
  842. long n;
  843. /*
  844. * this function is called when we really expect a Certificate message,
  845. * so permit appropriate message length
  846. */
  847. n = s->method->ssl_get_message(s,
  848. SSL3_ST_SR_CERT_A,
  849. SSL3_ST_SR_CERT_B,
  850. -1, s->max_cert_list, &ok);
  851. if (!ok)
  852. return ((int)n);
  853. s->s3->tmp.reuse_message = 1;
  854. if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO) {
  855. /*
  856. * We only allow the client to restart the handshake once per
  857. * negotiation.
  858. */
  859. if (s->s3->flags & SSL3_FLAGS_SGC_RESTART_DONE) {
  860. SSLerr(SSL_F_SSL3_CHECK_CLIENT_HELLO,
  861. SSL_R_MULTIPLE_SGC_RESTARTS);
  862. return -1;
  863. }
  864. /*
  865. * Throw away what we have done so far in the current handshake,
  866. * which will now be aborted. (A full SSL_clear would be too much.)
  867. */
  868. #ifndef OPENSSL_NO_DH
  869. if (s->s3->tmp.dh != NULL) {
  870. DH_free(s->s3->tmp.dh);
  871. s->s3->tmp.dh = NULL;
  872. }
  873. #endif
  874. #ifndef OPENSSL_NO_ECDH
  875. if (s->s3->tmp.ecdh != NULL) {
  876. EC_KEY_free(s->s3->tmp.ecdh);
  877. s->s3->tmp.ecdh = NULL;
  878. }
  879. #endif
  880. s->s3->flags |= SSL3_FLAGS_SGC_RESTART_DONE;
  881. return 2;
  882. }
  883. return 1;
  884. }
  885. int ssl3_get_client_hello(SSL *s)
  886. {
  887. int i, j, ok, al, ret = -1, cookie_valid = 0;
  888. unsigned int cookie_len;
  889. long n;
  890. unsigned long id;
  891. unsigned char *p, *d, *q;
  892. SSL_CIPHER *c;
  893. #ifndef OPENSSL_NO_COMP
  894. SSL_COMP *comp = NULL;
  895. #endif
  896. STACK_OF(SSL_CIPHER) *ciphers = NULL;
  897. /*
  898. * We do this so that we will respond with our native type. If we are
  899. * TLSv1 and we get SSLv3, we will respond with TLSv1, This down
  900. * switching should be handled by a different method. If we are SSLv3, we
  901. * will respond with SSLv3, even if prompted with TLSv1.
  902. */
  903. if (s->state == SSL3_ST_SR_CLNT_HELLO_A) {
  904. s->state = SSL3_ST_SR_CLNT_HELLO_B;
  905. }
  906. s->first_packet = 1;
  907. n = s->method->ssl_get_message(s,
  908. SSL3_ST_SR_CLNT_HELLO_B,
  909. SSL3_ST_SR_CLNT_HELLO_C,
  910. SSL3_MT_CLIENT_HELLO,
  911. SSL3_RT_MAX_PLAIN_LENGTH, &ok);
  912. if (!ok)
  913. return ((int)n);
  914. s->first_packet = 0;
  915. d = p = (unsigned char *)s->init_msg;
  916. /*
  917. * 2 bytes for client version, SSL3_RANDOM_SIZE bytes for random, 1 byte
  918. * for session id length
  919. */
  920. if (n < 2 + SSL3_RANDOM_SIZE + 1) {
  921. al = SSL_AD_DECODE_ERROR;
  922. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
  923. goto f_err;
  924. }
  925. /*
  926. * use version from inside client hello, not from record header (may
  927. * differ: see RFC 2246, Appendix E, second paragraph)
  928. */
  929. s->client_version = (((int)p[0]) << 8) | (int)p[1];
  930. p += 2;
  931. if ((s->version == DTLS1_VERSION && s->client_version > s->version) ||
  932. (s->version != DTLS1_VERSION && s->client_version < s->version)) {
  933. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
  934. if ((s->client_version >> 8) == SSL3_VERSION_MAJOR &&
  935. !s->enc_write_ctx && !s->write_hash) {
  936. /*
  937. * similar to ssl3_get_record, send alert using remote version
  938. * number
  939. */
  940. s->version = s->client_version;
  941. }
  942. al = SSL_AD_PROTOCOL_VERSION;
  943. goto f_err;
  944. }
  945. /*
  946. * If we require cookies and this ClientHello doesn't contain one, just
  947. * return since we do not want to allocate any memory yet. So check
  948. * cookie length...
  949. */
  950. if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
  951. unsigned int session_length, cookie_length;
  952. session_length = *(p + SSL3_RANDOM_SIZE);
  953. if (p + SSL3_RANDOM_SIZE + session_length + 1 >= d + n) {
  954. al = SSL_AD_DECODE_ERROR;
  955. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
  956. goto f_err;
  957. }
  958. cookie_length = *(p + SSL3_RANDOM_SIZE + session_length + 1);
  959. if (cookie_length == 0)
  960. return 1;
  961. }
  962. /* load the client random */
  963. memcpy(s->s3->client_random, p, SSL3_RANDOM_SIZE);
  964. p += SSL3_RANDOM_SIZE;
  965. /* get the session-id */
  966. j = *(p++);
  967. if (p + j > d + n) {
  968. al = SSL_AD_DECODE_ERROR;
  969. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
  970. goto f_err;
  971. }
  972. if ((j < 0) || (j > SSL_MAX_SSL_SESSION_ID_LENGTH)) {
  973. al = SSL_AD_DECODE_ERROR;
  974. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
  975. goto f_err;
  976. }
  977. s->hit = 0;
  978. /*
  979. * Versions before 0.9.7 always allow clients to resume sessions in
  980. * renegotiation. 0.9.7 and later allow this by default, but optionally
  981. * ignore resumption requests with flag
  982. * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
  983. * than a change to default behavior so that applications relying on this
  984. * for security won't even compile against older library versions).
  985. * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
  986. * request renegotiation but not a new session (s->new_session remains
  987. * unset): for servers, this essentially just means that the
  988. * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be ignored.
  989. */
  990. if ((s->new_session
  991. && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
  992. if (!ssl_get_new_session(s, 1))
  993. goto err;
  994. } else {
  995. i = ssl_get_prev_session(s, p, j, d + n);
  996. /*
  997. * Only resume if the session's version matches the negotiated
  998. * version.
  999. * RFC 5246 does not provide much useful advice on resumption
  1000. * with a different protocol version. It doesn't forbid it but
  1001. * the sanity of such behaviour would be questionable.
  1002. * In practice, clients do not accept a version mismatch and
  1003. * will abort the handshake with an error.
  1004. */
  1005. if (i == 1 && s->version == s->session->ssl_version) { /* previous
  1006. * session */
  1007. s->hit = 1;
  1008. } else if (i == -1)
  1009. goto err;
  1010. else { /* i == 0 */
  1011. if (!ssl_get_new_session(s, 1))
  1012. goto err;
  1013. }
  1014. }
  1015. p += j;
  1016. if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) {
  1017. /* cookie stuff */
  1018. if (p + 1 > d + n) {
  1019. al = SSL_AD_DECODE_ERROR;
  1020. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
  1021. goto f_err;
  1022. }
  1023. cookie_len = *(p++);
  1024. if (p + cookie_len > d + n) {
  1025. al = SSL_AD_DECODE_ERROR;
  1026. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
  1027. goto f_err;
  1028. }
  1029. /*
  1030. * The ClientHello may contain a cookie even if the
  1031. * HelloVerify message has not been sent--make sure that it
  1032. * does not cause an overflow.
  1033. */
  1034. if (cookie_len > sizeof(s->d1->rcvd_cookie)) {
  1035. /* too much data */
  1036. al = SSL_AD_DECODE_ERROR;
  1037. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
  1038. goto f_err;
  1039. }
  1040. /* verify the cookie if appropriate option is set. */
  1041. if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && cookie_len > 0) {
  1042. memcpy(s->d1->rcvd_cookie, p, cookie_len);
  1043. if (s->ctx->app_verify_cookie_cb != NULL) {
  1044. if (s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
  1045. cookie_len) == 0) {
  1046. al = SSL_AD_HANDSHAKE_FAILURE;
  1047. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
  1048. SSL_R_COOKIE_MISMATCH);
  1049. goto f_err;
  1050. }
  1051. /* else cookie verification succeeded */
  1052. }
  1053. /* default verification */
  1054. else if (memcmp(s->d1->rcvd_cookie, s->d1->cookie,
  1055. s->d1->cookie_len) != 0) {
  1056. al = SSL_AD_HANDSHAKE_FAILURE;
  1057. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
  1058. goto f_err;
  1059. }
  1060. cookie_valid = 1;
  1061. }
  1062. p += cookie_len;
  1063. }
  1064. if (p + 2 > d + n) {
  1065. al = SSL_AD_DECODE_ERROR;
  1066. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
  1067. goto f_err;
  1068. }
  1069. n2s(p, i);
  1070. if (i == 0) {
  1071. al = SSL_AD_ILLEGAL_PARAMETER;
  1072. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_SPECIFIED);
  1073. goto f_err;
  1074. }
  1075. /* i bytes of cipher data + 1 byte for compression length later */
  1076. if ((p + i + 1) > (d + n)) {
  1077. /* not enough data */
  1078. al = SSL_AD_DECODE_ERROR;
  1079. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
  1080. goto f_err;
  1081. }
  1082. if (ssl_bytes_to_cipher_list(s, p, i, &(ciphers)) == NULL) {
  1083. goto err;
  1084. }
  1085. p += i;
  1086. /* If it is a hit, check that the cipher is in the list */
  1087. if (s->hit) {
  1088. j = 0;
  1089. id = s->session->cipher->id;
  1090. #ifdef CIPHER_DEBUG
  1091. fprintf(stderr, "client sent %d ciphers\n",
  1092. sk_SSL_CIPHER_num(ciphers));
  1093. #endif
  1094. for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  1095. c = sk_SSL_CIPHER_value(ciphers, i);
  1096. #ifdef CIPHER_DEBUG
  1097. fprintf(stderr, "client [%2d of %2d]:%s\n",
  1098. i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
  1099. #endif
  1100. if (c->id == id) {
  1101. j = 1;
  1102. break;
  1103. }
  1104. }
  1105. /*
  1106. * Disabled because it can be used in a ciphersuite downgrade attack:
  1107. * CVE-2010-4180.
  1108. */
  1109. #if 0
  1110. if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)
  1111. && (sk_SSL_CIPHER_num(ciphers) == 1)) {
  1112. /*
  1113. * Special case as client bug workaround: the previously used
  1114. * cipher may not be in the current list, the client instead
  1115. * might be trying to continue using a cipher that before wasn't
  1116. * chosen due to server preferences. We'll have to reject the
  1117. * connection if the cipher is not enabled, though.
  1118. */
  1119. c = sk_SSL_CIPHER_value(ciphers, 0);
  1120. if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0) {
  1121. s->session->cipher = c;
  1122. j = 1;
  1123. }
  1124. }
  1125. #endif
  1126. if (j == 0) {
  1127. /*
  1128. * we need to have the cipher in the cipher list if we are asked
  1129. * to reuse it
  1130. */
  1131. al = SSL_AD_ILLEGAL_PARAMETER;
  1132. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
  1133. SSL_R_REQUIRED_CIPHER_MISSING);
  1134. goto f_err;
  1135. }
  1136. }
  1137. /* compression */
  1138. i = *(p++);
  1139. if ((p + i) > (d + n)) {
  1140. /* not enough data */
  1141. al = SSL_AD_DECODE_ERROR;
  1142. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
  1143. goto f_err;
  1144. }
  1145. q = p;
  1146. for (j = 0; j < i; j++) {
  1147. if (p[j] == 0)
  1148. break;
  1149. }
  1150. p += i;
  1151. if (j >= i) {
  1152. /* no compress */
  1153. al = SSL_AD_DECODE_ERROR;
  1154. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
  1155. goto f_err;
  1156. }
  1157. #ifndef OPENSSL_NO_TLSEXT
  1158. /* TLS extensions */
  1159. if (s->version >= SSL3_VERSION) {
  1160. if (!ssl_parse_clienthello_tlsext(s, &p, d + n, &al)) {
  1161. /* 'al' set by ssl_parse_clienthello_tlsext */
  1162. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
  1163. goto f_err;
  1164. }
  1165. }
  1166. if (ssl_check_clienthello_tlsext_early(s) <= 0) {
  1167. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
  1168. goto err;
  1169. }
  1170. /*
  1171. * Check if we want to use external pre-shared secret for this handshake
  1172. * for not reused session only. We need to generate server_random before
  1173. * calling tls_session_secret_cb in order to allow SessionTicket
  1174. * processing to use it in key derivation.
  1175. */
  1176. {
  1177. unsigned char *pos;
  1178. pos = s->s3->server_random;
  1179. if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) {
  1180. al = SSL_AD_INTERNAL_ERROR;
  1181. goto f_err;
  1182. }
  1183. }
  1184. if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) {
  1185. SSL_CIPHER *pref_cipher = NULL;
  1186. s->session->master_key_length = sizeof(s->session->master_key);
  1187. if (s->tls_session_secret_cb(s, s->session->master_key,
  1188. &s->session->master_key_length, ciphers,
  1189. &pref_cipher,
  1190. s->tls_session_secret_cb_arg)) {
  1191. s->hit = 1;
  1192. s->session->ciphers = ciphers;
  1193. s->session->verify_result = X509_V_OK;
  1194. ciphers = NULL;
  1195. /* check if some cipher was preferred by call back */
  1196. pref_cipher =
  1197. pref_cipher ? pref_cipher : ssl3_choose_cipher(s,
  1198. s->
  1199. session->ciphers,
  1200. SSL_get_ciphers
  1201. (s));
  1202. if (pref_cipher == NULL) {
  1203. al = SSL_AD_HANDSHAKE_FAILURE;
  1204. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
  1205. goto f_err;
  1206. }
  1207. s->session->cipher = pref_cipher;
  1208. if (s->cipher_list)
  1209. sk_SSL_CIPHER_free(s->cipher_list);
  1210. if (s->cipher_list_by_id)
  1211. sk_SSL_CIPHER_free(s->cipher_list_by_id);
  1212. s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
  1213. s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
  1214. }
  1215. }
  1216. #endif
  1217. /*
  1218. * Worst case, we will use the NULL compression, but if we have other
  1219. * options, we will now look for them. We have i-1 compression
  1220. * algorithms from the client, starting at q.
  1221. */
  1222. s->s3->tmp.new_compression = NULL;
  1223. #ifndef OPENSSL_NO_COMP
  1224. /* This only happens if we have a cache hit */
  1225. if (s->session->compress_meth != 0) {
  1226. int m, comp_id = s->session->compress_meth;
  1227. /* Perform sanity checks on resumed compression algorithm */
  1228. /* Can't disable compression */
  1229. if (s->options & SSL_OP_NO_COMPRESSION) {
  1230. al = SSL_AD_INTERNAL_ERROR;
  1231. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
  1232. SSL_R_INCONSISTENT_COMPRESSION);
  1233. goto f_err;
  1234. }
  1235. /* Look for resumed compression method */
  1236. for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
  1237. comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
  1238. if (comp_id == comp->id) {
  1239. s->s3->tmp.new_compression = comp;
  1240. break;
  1241. }
  1242. }
  1243. if (s->s3->tmp.new_compression == NULL) {
  1244. al = SSL_AD_INTERNAL_ERROR;
  1245. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
  1246. SSL_R_INVALID_COMPRESSION_ALGORITHM);
  1247. goto f_err;
  1248. }
  1249. /* Look for resumed method in compression list */
  1250. for (m = 0; m < i; m++) {
  1251. if (q[m] == comp_id)
  1252. break;
  1253. }
  1254. if (m >= i) {
  1255. al = SSL_AD_ILLEGAL_PARAMETER;
  1256. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
  1257. SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING);
  1258. goto f_err;
  1259. }
  1260. } else if (s->hit)
  1261. comp = NULL;
  1262. else if (!(s->options & SSL_OP_NO_COMPRESSION) && s->ctx->comp_methods) {
  1263. /* See if we have a match */
  1264. int m, nn, o, v, done = 0;
  1265. nn = sk_SSL_COMP_num(s->ctx->comp_methods);
  1266. for (m = 0; m < nn; m++) {
  1267. comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
  1268. v = comp->id;
  1269. for (o = 0; o < i; o++) {
  1270. if (v == q[o]) {
  1271. done = 1;
  1272. break;
  1273. }
  1274. }
  1275. if (done)
  1276. break;
  1277. }
  1278. if (done)
  1279. s->s3->tmp.new_compression = comp;
  1280. else
  1281. comp = NULL;
  1282. }
  1283. #else
  1284. /*
  1285. * If compression is disabled we'd better not try to resume a session
  1286. * using compression.
  1287. */
  1288. if (s->session->compress_meth != 0) {
  1289. al = SSL_AD_INTERNAL_ERROR;
  1290. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
  1291. goto f_err;
  1292. }
  1293. #endif
  1294. /*
  1295. * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
  1296. */
  1297. if (!s->hit) {
  1298. #ifdef OPENSSL_NO_COMP
  1299. s->session->compress_meth = 0;
  1300. #else
  1301. s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
  1302. #endif
  1303. if (s->session->ciphers != NULL)
  1304. sk_SSL_CIPHER_free(s->session->ciphers);
  1305. s->session->ciphers = ciphers;
  1306. if (ciphers == NULL) {
  1307. al = SSL_AD_INTERNAL_ERROR;
  1308. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  1309. goto f_err;
  1310. }
  1311. ciphers = NULL;
  1312. c = ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
  1313. if (c == NULL) {
  1314. al = SSL_AD_HANDSHAKE_FAILURE;
  1315. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
  1316. goto f_err;
  1317. }
  1318. s->s3->tmp.new_cipher = c;
  1319. } else {
  1320. /* Session-id reuse */
  1321. #ifdef REUSE_CIPHER_BUG
  1322. STACK_OF(SSL_CIPHER) *sk;
  1323. SSL_CIPHER *nc = NULL;
  1324. SSL_CIPHER *ec = NULL;
  1325. if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG) {
  1326. sk = s->session->ciphers;
  1327. for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
  1328. c = sk_SSL_CIPHER_value(sk, i);
  1329. if (c->algorithm_enc & SSL_eNULL)
  1330. nc = c;
  1331. if (SSL_C_IS_EXPORT(c))
  1332. ec = c;
  1333. }
  1334. if (nc != NULL)
  1335. s->s3->tmp.new_cipher = nc;
  1336. else if (ec != NULL)
  1337. s->s3->tmp.new_cipher = ec;
  1338. else
  1339. s->s3->tmp.new_cipher = s->session->cipher;
  1340. } else
  1341. #endif
  1342. s->s3->tmp.new_cipher = s->session->cipher;
  1343. }
  1344. if (TLS1_get_version(s) < TLS1_2_VERSION
  1345. || !(s->verify_mode & SSL_VERIFY_PEER)) {
  1346. if (!ssl3_digest_cached_records(s)) {
  1347. al = SSL_AD_INTERNAL_ERROR;
  1348. goto f_err;
  1349. }
  1350. }
  1351. /*-
  1352. * we now have the following setup.
  1353. * client_random
  1354. * cipher_list - our prefered list of ciphers
  1355. * ciphers - the clients prefered list of ciphers
  1356. * compression - basically ignored right now
  1357. * ssl version is set - sslv3
  1358. * s->session - The ssl session has been setup.
  1359. * s->hit - session reuse flag
  1360. * s->tmp.new_cipher - the new cipher to use.
  1361. */
  1362. /* Handles TLS extensions that we couldn't check earlier */
  1363. if (s->version >= SSL3_VERSION) {
  1364. if (ssl_check_clienthello_tlsext_late(s) <= 0) {
  1365. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
  1366. goto err;
  1367. }
  1368. }
  1369. ret = cookie_valid ? 2 : 1;
  1370. if (0) {
  1371. f_err:
  1372. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1373. err:
  1374. s->state = SSL_ST_ERR;
  1375. }
  1376. if (ciphers != NULL)
  1377. sk_SSL_CIPHER_free(ciphers);
  1378. return ret;
  1379. }
  1380. int ssl3_send_server_hello(SSL *s)
  1381. {
  1382. unsigned char *buf;
  1383. unsigned char *p, *d;
  1384. int i, sl;
  1385. unsigned long l;
  1386. if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
  1387. buf = (unsigned char *)s->init_buf->data;
  1388. #ifdef OPENSSL_NO_TLSEXT
  1389. p = s->s3->server_random;
  1390. if (ssl_fill_hello_random(s, 1, p, SSL3_RANDOM_SIZE) <= 0) {
  1391. s->state = SSL_ST_ERR;
  1392. return -1;
  1393. }
  1394. #endif
  1395. /* Do the message type and length last */
  1396. d = p = &(buf[4]);
  1397. *(p++) = s->version >> 8;
  1398. *(p++) = s->version & 0xff;
  1399. /* Random stuff */
  1400. memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
  1401. p += SSL3_RANDOM_SIZE;
  1402. /*-
  1403. * There are several cases for the session ID to send
  1404. * back in the server hello:
  1405. * - For session reuse from the session cache,
  1406. * we send back the old session ID.
  1407. * - If stateless session reuse (using a session ticket)
  1408. * is successful, we send back the client's "session ID"
  1409. * (which doesn't actually identify the session).
  1410. * - If it is a new session, we send back the new
  1411. * session ID.
  1412. * - However, if we want the new session to be single-use,
  1413. * we send back a 0-length session ID.
  1414. * s->hit is non-zero in either case of session reuse,
  1415. * so the following won't overwrite an ID that we're supposed
  1416. * to send back.
  1417. */
  1418. if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
  1419. && !s->hit)
  1420. s->session->session_id_length = 0;
  1421. sl = s->session->session_id_length;
  1422. if (sl > (int)sizeof(s->session->session_id)) {
  1423. SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  1424. s->state = SSL_ST_ERR;
  1425. return -1;
  1426. }
  1427. *(p++) = sl;
  1428. memcpy(p, s->session->session_id, sl);
  1429. p += sl;
  1430. /* put the cipher */
  1431. i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
  1432. p += i;
  1433. /* put the compression method */
  1434. #ifdef OPENSSL_NO_COMP
  1435. *(p++) = 0;
  1436. #else
  1437. if (s->s3->tmp.new_compression == NULL)
  1438. *(p++) = 0;
  1439. else
  1440. *(p++) = s->s3->tmp.new_compression->id;
  1441. #endif
  1442. #ifndef OPENSSL_NO_TLSEXT
  1443. if (ssl_prepare_serverhello_tlsext(s) <= 0) {
  1444. SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
  1445. s->state = SSL_ST_ERR;
  1446. return -1;
  1447. }
  1448. if ((p =
  1449. ssl_add_serverhello_tlsext(s, p,
  1450. buf + SSL3_RT_MAX_PLAIN_LENGTH)) ==
  1451. NULL) {
  1452. SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  1453. s->state = SSL_ST_ERR;
  1454. return -1;
  1455. }
  1456. #endif
  1457. /* do the header */
  1458. l = (p - d);
  1459. d = buf;
  1460. *(d++) = SSL3_MT_SERVER_HELLO;
  1461. l2n3(l, d);
  1462. s->state = SSL3_ST_SW_SRVR_HELLO_B;
  1463. /* number of bytes to write */
  1464. s->init_num = p - buf;
  1465. s->init_off = 0;
  1466. }
  1467. /* SSL3_ST_SW_SRVR_HELLO_B */
  1468. return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
  1469. }
  1470. int ssl3_send_server_done(SSL *s)
  1471. {
  1472. unsigned char *p;
  1473. if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
  1474. p = (unsigned char *)s->init_buf->data;
  1475. /* do the header */
  1476. *(p++) = SSL3_MT_SERVER_DONE;
  1477. *(p++) = 0;
  1478. *(p++) = 0;
  1479. *(p++) = 0;
  1480. s->state = SSL3_ST_SW_SRVR_DONE_B;
  1481. /* number of bytes to write */
  1482. s->init_num = 4;
  1483. s->init_off = 0;
  1484. }
  1485. /* SSL3_ST_SW_SRVR_DONE_B */
  1486. return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
  1487. }
  1488. int ssl3_send_server_key_exchange(SSL *s)
  1489. {
  1490. #ifndef OPENSSL_NO_RSA
  1491. unsigned char *q;
  1492. int j, num;
  1493. RSA *rsa;
  1494. unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
  1495. unsigned int u;
  1496. #endif
  1497. #ifndef OPENSSL_NO_DH
  1498. DH *dh = NULL, *dhp;
  1499. #endif
  1500. #ifndef OPENSSL_NO_ECDH
  1501. EC_KEY *ecdh = NULL, *ecdhp;
  1502. unsigned char *encodedPoint = NULL;
  1503. int encodedlen = 0;
  1504. int curve_id = 0;
  1505. BN_CTX *bn_ctx = NULL;
  1506. #endif
  1507. EVP_PKEY *pkey;
  1508. const EVP_MD *md = NULL;
  1509. unsigned char *p, *d;
  1510. int al, i;
  1511. unsigned long type;
  1512. int n;
  1513. CERT *cert;
  1514. BIGNUM *r[4];
  1515. int nr[4], kn;
  1516. BUF_MEM *buf;
  1517. EVP_MD_CTX md_ctx;
  1518. EVP_MD_CTX_init(&md_ctx);
  1519. if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
  1520. type = s->s3->tmp.new_cipher->algorithm_mkey;
  1521. cert = s->cert;
  1522. buf = s->init_buf;
  1523. r[0] = r[1] = r[2] = r[3] = NULL;
  1524. n = 0;
  1525. #ifndef OPENSSL_NO_RSA
  1526. if (type & SSL_kRSA) {
  1527. rsa = cert->rsa_tmp;
  1528. if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) {
  1529. rsa = s->cert->rsa_tmp_cb(s,
  1530. SSL_C_IS_EXPORT(s->s3->
  1531. tmp.new_cipher),
  1532. SSL_C_EXPORT_PKEYLENGTH(s->s3->
  1533. tmp.new_cipher));
  1534. if (rsa == NULL) {
  1535. al = SSL_AD_HANDSHAKE_FAILURE;
  1536. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  1537. SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
  1538. goto f_err;
  1539. }
  1540. RSA_up_ref(rsa);
  1541. cert->rsa_tmp = rsa;
  1542. }
  1543. if (rsa == NULL) {
  1544. al = SSL_AD_HANDSHAKE_FAILURE;
  1545. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  1546. SSL_R_MISSING_TMP_RSA_KEY);
  1547. goto f_err;
  1548. }
  1549. r[0] = rsa->n;
  1550. r[1] = rsa->e;
  1551. s->s3->tmp.use_rsa_tmp = 1;
  1552. } else
  1553. #endif
  1554. #ifndef OPENSSL_NO_DH
  1555. if (type & SSL_kEDH) {
  1556. dhp = cert->dh_tmp;
  1557. if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
  1558. dhp = s->cert->dh_tmp_cb(s,
  1559. SSL_C_IS_EXPORT(s->s3->
  1560. tmp.new_cipher),
  1561. SSL_C_EXPORT_PKEYLENGTH(s->s3->
  1562. tmp.new_cipher));
  1563. if (dhp == NULL) {
  1564. al = SSL_AD_HANDSHAKE_FAILURE;
  1565. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  1566. SSL_R_MISSING_TMP_DH_KEY);
  1567. goto f_err;
  1568. }
  1569. if (s->s3->tmp.dh != NULL) {
  1570. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  1571. ERR_R_INTERNAL_ERROR);
  1572. goto err;
  1573. }
  1574. if ((dh = DHparams_dup(dhp)) == NULL) {
  1575. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
  1576. goto err;
  1577. }
  1578. s->s3->tmp.dh = dh;
  1579. if (!DH_generate_key(dh)) {
  1580. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
  1581. goto err;
  1582. }
  1583. r[0] = dh->p;
  1584. r[1] = dh->g;
  1585. r[2] = dh->pub_key;
  1586. } else
  1587. #endif
  1588. #ifndef OPENSSL_NO_ECDH
  1589. if (type & SSL_kEECDH) {
  1590. const EC_GROUP *group;
  1591. ecdhp = cert->ecdh_tmp;
  1592. if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL)) {
  1593. ecdhp = s->cert->ecdh_tmp_cb(s,
  1594. SSL_C_IS_EXPORT(s->s3->
  1595. tmp.new_cipher),
  1596. SSL_C_EXPORT_PKEYLENGTH(s->
  1597. s3->tmp.new_cipher));
  1598. }
  1599. if (ecdhp == NULL) {
  1600. al = SSL_AD_HANDSHAKE_FAILURE;
  1601. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  1602. SSL_R_MISSING_TMP_ECDH_KEY);
  1603. goto f_err;
  1604. }
  1605. if (s->s3->tmp.ecdh != NULL) {
  1606. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  1607. ERR_R_INTERNAL_ERROR);
  1608. goto err;
  1609. }
  1610. /* Duplicate the ECDH structure. */
  1611. if (ecdhp == NULL) {
  1612. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  1613. goto err;
  1614. }
  1615. if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
  1616. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  1617. goto err;
  1618. }
  1619. s->s3->tmp.ecdh = ecdh;
  1620. if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
  1621. (EC_KEY_get0_private_key(ecdh) == NULL) ||
  1622. (s->options & SSL_OP_SINGLE_ECDH_USE)) {
  1623. if (!EC_KEY_generate_key(ecdh)) {
  1624. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  1625. ERR_R_ECDH_LIB);
  1626. goto err;
  1627. }
  1628. }
  1629. if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
  1630. (EC_KEY_get0_public_key(ecdh) == NULL) ||
  1631. (EC_KEY_get0_private_key(ecdh) == NULL)) {
  1632. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  1633. goto err;
  1634. }
  1635. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  1636. (EC_GROUP_get_degree(group) > 163)) {
  1637. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  1638. SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
  1639. goto err;
  1640. }
  1641. /*
  1642. * XXX: For now, we only support ephemeral ECDH keys over named
  1643. * (not generic) curves. For supported named curves, curve_id is
  1644. * non-zero.
  1645. */
  1646. if ((curve_id =
  1647. tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
  1648. == 0) {
  1649. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  1650. SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
  1651. goto err;
  1652. }
  1653. /*
  1654. * Encode the public key. First check the size of encoding and
  1655. * allocate memory accordingly.
  1656. */
  1657. encodedlen = EC_POINT_point2oct(group,
  1658. EC_KEY_get0_public_key(ecdh),
  1659. POINT_CONVERSION_UNCOMPRESSED,
  1660. NULL, 0, NULL);
  1661. encodedPoint = (unsigned char *)
  1662. OPENSSL_malloc(encodedlen * sizeof(unsigned char));
  1663. bn_ctx = BN_CTX_new();
  1664. if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
  1665. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  1666. ERR_R_MALLOC_FAILURE);
  1667. goto err;
  1668. }
  1669. encodedlen = EC_POINT_point2oct(group,
  1670. EC_KEY_get0_public_key(ecdh),
  1671. POINT_CONVERSION_UNCOMPRESSED,
  1672. encodedPoint, encodedlen, bn_ctx);
  1673. if (encodedlen == 0) {
  1674. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  1675. goto err;
  1676. }
  1677. BN_CTX_free(bn_ctx);
  1678. bn_ctx = NULL;
  1679. /*
  1680. * XXX: For now, we only support named (not generic) curves in
  1681. * ECDH ephemeral key exchanges. In this situation, we need four
  1682. * additional bytes to encode the entire ServerECDHParams
  1683. * structure.
  1684. */
  1685. n = 4 + encodedlen;
  1686. /*
  1687. * We'll generate the serverKeyExchange message explicitly so we
  1688. * can set these to NULLs
  1689. */
  1690. r[0] = NULL;
  1691. r[1] = NULL;
  1692. r[2] = NULL;
  1693. r[3] = NULL;
  1694. } else
  1695. #endif /* !OPENSSL_NO_ECDH */
  1696. #ifndef OPENSSL_NO_PSK
  1697. if (type & SSL_kPSK) {
  1698. /*
  1699. * reserve size for record length and PSK identity hint
  1700. */
  1701. n += 2 + strlen(s->ctx->psk_identity_hint);
  1702. } else
  1703. #endif /* !OPENSSL_NO_PSK */
  1704. #ifndef OPENSSL_NO_SRP
  1705. if (type & SSL_kSRP) {
  1706. if ((s->srp_ctx.N == NULL) ||
  1707. (s->srp_ctx.g == NULL) ||
  1708. (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
  1709. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  1710. SSL_R_MISSING_SRP_PARAM);
  1711. goto err;
  1712. }
  1713. r[0] = s->srp_ctx.N;
  1714. r[1] = s->srp_ctx.g;
  1715. r[2] = s->srp_ctx.s;
  1716. r[3] = s->srp_ctx.B;
  1717. } else
  1718. #endif
  1719. {
  1720. al = SSL_AD_HANDSHAKE_FAILURE;
  1721. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  1722. SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  1723. goto f_err;
  1724. }
  1725. for (i = 0; i < 4 && r[i] != NULL; i++) {
  1726. nr[i] = BN_num_bytes(r[i]);
  1727. #ifndef OPENSSL_NO_SRP
  1728. if ((i == 2) && (type & SSL_kSRP))
  1729. n += 1 + nr[i];
  1730. else
  1731. #endif
  1732. n += 2 + nr[i];
  1733. }
  1734. if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
  1735. && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
  1736. if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, &md))
  1737. == NULL) {
  1738. al = SSL_AD_DECODE_ERROR;
  1739. goto f_err;
  1740. }
  1741. kn = EVP_PKEY_size(pkey);
  1742. } else {
  1743. pkey = NULL;
  1744. kn = 0;
  1745. }
  1746. if (!BUF_MEM_grow_clean(buf, n + 4 + kn)) {
  1747. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
  1748. goto err;
  1749. }
  1750. d = (unsigned char *)s->init_buf->data;
  1751. p = &(d[4]);
  1752. for (i = 0; i < 4 && r[i] != NULL; i++) {
  1753. #ifndef OPENSSL_NO_SRP
  1754. if ((i == 2) && (type & SSL_kSRP)) {
  1755. *p = nr[i];
  1756. p++;
  1757. } else
  1758. #endif
  1759. s2n(nr[i], p);
  1760. BN_bn2bin(r[i], p);
  1761. p += nr[i];
  1762. }
  1763. #ifndef OPENSSL_NO_ECDH
  1764. if (type & SSL_kEECDH) {
  1765. /*
  1766. * XXX: For now, we only support named (not generic) curves. In
  1767. * this situation, the serverKeyExchange message has: [1 byte
  1768. * CurveType], [2 byte CurveName] [1 byte length of encoded
  1769. * point], followed by the actual encoded point itself
  1770. */
  1771. *p = NAMED_CURVE_TYPE;
  1772. p += 1;
  1773. *p = 0;
  1774. p += 1;
  1775. *p = curve_id;
  1776. p += 1;
  1777. *p = encodedlen;
  1778. p += 1;
  1779. memcpy((unsigned char *)p,
  1780. (unsigned char *)encodedPoint, encodedlen);
  1781. OPENSSL_free(encodedPoint);
  1782. encodedPoint = NULL;
  1783. p += encodedlen;
  1784. }
  1785. #endif
  1786. #ifndef OPENSSL_NO_PSK
  1787. if (type & SSL_kPSK) {
  1788. /* copy PSK identity hint */
  1789. s2n(strlen(s->ctx->psk_identity_hint), p);
  1790. strncpy((char *)p, s->ctx->psk_identity_hint,
  1791. strlen(s->ctx->psk_identity_hint));
  1792. p += strlen(s->ctx->psk_identity_hint);
  1793. }
  1794. #endif
  1795. /* not anonymous */
  1796. if (pkey != NULL) {
  1797. /*
  1798. * n is the length of the params, they start at &(d[4]) and p
  1799. * points to the space at the end.
  1800. */
  1801. #ifndef OPENSSL_NO_RSA
  1802. if (pkey->type == EVP_PKEY_RSA
  1803. && TLS1_get_version(s) < TLS1_2_VERSION) {
  1804. q = md_buf;
  1805. j = 0;
  1806. for (num = 2; num > 0; num--) {
  1807. EVP_MD_CTX_set_flags(&md_ctx,
  1808. EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  1809. if (EVP_DigestInit_ex(&md_ctx,
  1810. (num == 2) ? s->ctx->md5
  1811. : s->ctx->sha1,
  1812. NULL) <= 0
  1813. || EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
  1814. SSL3_RANDOM_SIZE) <= 0
  1815. || EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
  1816. SSL3_RANDOM_SIZE) <= 0
  1817. || EVP_DigestUpdate(&md_ctx, &(d[4]), n) <= 0
  1818. || EVP_DigestFinal_ex(&md_ctx, q,
  1819. (unsigned int *)&i) <= 0) {
  1820. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  1821. ERR_LIB_EVP);
  1822. al = SSL_AD_INTERNAL_ERROR;
  1823. goto f_err;
  1824. }
  1825. q += i;
  1826. j += i;
  1827. }
  1828. if (RSA_sign(NID_md5_sha1, md_buf, j,
  1829. &(p[2]), &u, pkey->pkey.rsa) <= 0) {
  1830. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA);
  1831. goto err;
  1832. }
  1833. s2n(u, p);
  1834. n += u + 2;
  1835. } else
  1836. #endif
  1837. if (md) {
  1838. /*
  1839. * For TLS1.2 and later send signature algorithm
  1840. */
  1841. if (TLS1_get_version(s) >= TLS1_2_VERSION) {
  1842. if (!tls12_get_sigandhash(p, pkey, md)) {
  1843. /* Should never happen */
  1844. al = SSL_AD_INTERNAL_ERROR;
  1845. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  1846. ERR_R_INTERNAL_ERROR);
  1847. goto f_err;
  1848. }
  1849. p += 2;
  1850. }
  1851. #ifdef SSL_DEBUG
  1852. fprintf(stderr, "Using hash %s\n", EVP_MD_name(md));
  1853. #endif
  1854. if (EVP_SignInit_ex(&md_ctx, md, NULL) <= 0
  1855. || EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
  1856. SSL3_RANDOM_SIZE) <= 0
  1857. || EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
  1858. SSL3_RANDOM_SIZE) <= 0
  1859. || EVP_SignUpdate(&md_ctx, &(d[4]), n) <= 0
  1860. || EVP_SignFinal(&md_ctx, &(p[2]),
  1861. (unsigned int *)&i, pkey) <= 0) {
  1862. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_EVP);
  1863. al = SSL_AD_INTERNAL_ERROR;
  1864. goto f_err;
  1865. }
  1866. s2n(i, p);
  1867. n += i + 2;
  1868. if (TLS1_get_version(s) >= TLS1_2_VERSION)
  1869. n += 2;
  1870. } else {
  1871. /* Is this error check actually needed? */
  1872. al = SSL_AD_HANDSHAKE_FAILURE;
  1873. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  1874. SSL_R_UNKNOWN_PKEY_TYPE);
  1875. goto f_err;
  1876. }
  1877. }
  1878. *(d++) = SSL3_MT_SERVER_KEY_EXCHANGE;
  1879. l2n3(n, d);
  1880. /*
  1881. * we should now have things packed up, so lets send it off
  1882. */
  1883. s->init_num = n + 4;
  1884. s->init_off = 0;
  1885. }
  1886. s->state = SSL3_ST_SW_KEY_EXCH_B;
  1887. EVP_MD_CTX_cleanup(&md_ctx);
  1888. return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
  1889. f_err:
  1890. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1891. err:
  1892. #ifndef OPENSSL_NO_ECDH
  1893. if (encodedPoint != NULL)
  1894. OPENSSL_free(encodedPoint);
  1895. BN_CTX_free(bn_ctx);
  1896. #endif
  1897. EVP_MD_CTX_cleanup(&md_ctx);
  1898. s->state = SSL_ST_ERR;
  1899. return (-1);
  1900. }
  1901. int ssl3_send_certificate_request(SSL *s)
  1902. {
  1903. unsigned char *p, *d;
  1904. int i, j, nl, off, n;
  1905. STACK_OF(X509_NAME) *sk = NULL;
  1906. X509_NAME *name;
  1907. BUF_MEM *buf;
  1908. if (s->state == SSL3_ST_SW_CERT_REQ_A) {
  1909. buf = s->init_buf;
  1910. d = p = (unsigned char *)&(buf->data[4]);
  1911. /* get the list of acceptable cert types */
  1912. p++;
  1913. n = ssl3_get_req_cert_type(s, p);
  1914. d[0] = n;
  1915. p += n;
  1916. n++;
  1917. if (TLS1_get_version(s) >= TLS1_2_VERSION) {
  1918. nl = tls12_get_req_sig_algs(s, p + 2);
  1919. s2n(nl, p);
  1920. p += nl + 2;
  1921. n += nl + 2;
  1922. }
  1923. off = n;
  1924. p += 2;
  1925. n += 2;
  1926. sk = SSL_get_client_CA_list(s);
  1927. nl = 0;
  1928. if (sk != NULL) {
  1929. for (i = 0; i < sk_X509_NAME_num(sk); i++) {
  1930. name = sk_X509_NAME_value(sk, i);
  1931. j = i2d_X509_NAME(name, NULL);
  1932. if (!BUF_MEM_grow_clean(buf, 4 + n + j + 2)) {
  1933. SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,
  1934. ERR_R_BUF_LIB);
  1935. goto err;
  1936. }
  1937. p = (unsigned char *)&(buf->data[4 + n]);
  1938. if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) {
  1939. s2n(j, p);
  1940. i2d_X509_NAME(name, &p);
  1941. n += 2 + j;
  1942. nl += 2 + j;
  1943. } else {
  1944. d = p;
  1945. i2d_X509_NAME(name, &p);
  1946. j -= 2;
  1947. s2n(j, d);
  1948. j += 2;
  1949. n += j;
  1950. nl += j;
  1951. }
  1952. }
  1953. }
  1954. /* else no CA names */
  1955. p = (unsigned char *)&(buf->data[4 + off]);
  1956. s2n(nl, p);
  1957. d = (unsigned char *)buf->data;
  1958. *(d++) = SSL3_MT_CERTIFICATE_REQUEST;
  1959. l2n3(n, d);
  1960. /*
  1961. * we should now have things packed up, so lets send it off
  1962. */
  1963. s->init_num = n + 4;
  1964. s->init_off = 0;
  1965. #ifdef NETSCAPE_HANG_BUG
  1966. if (!BUF_MEM_grow_clean(buf, s->init_num + 4)) {
  1967. SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST, ERR_R_BUF_LIB);
  1968. goto err;
  1969. }
  1970. p = (unsigned char *)s->init_buf->data + s->init_num;
  1971. /* do the header */
  1972. *(p++) = SSL3_MT_SERVER_DONE;
  1973. *(p++) = 0;
  1974. *(p++) = 0;
  1975. *(p++) = 0;
  1976. s->init_num += 4;
  1977. #endif
  1978. s->state = SSL3_ST_SW_CERT_REQ_B;
  1979. }
  1980. /* SSL3_ST_SW_CERT_REQ_B */
  1981. return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
  1982. err:
  1983. s->state = SSL_ST_ERR;
  1984. return (-1);
  1985. }
  1986. int ssl3_get_client_key_exchange(SSL *s)
  1987. {
  1988. int i, al, ok;
  1989. long n;
  1990. unsigned long alg_k;
  1991. unsigned char *p;
  1992. #ifndef OPENSSL_NO_RSA
  1993. RSA *rsa = NULL;
  1994. EVP_PKEY *pkey = NULL;
  1995. #endif
  1996. #ifndef OPENSSL_NO_DH
  1997. BIGNUM *pub = NULL;
  1998. DH *dh_srvr;
  1999. #endif
  2000. #ifndef OPENSSL_NO_KRB5
  2001. KSSL_ERR kssl_err;
  2002. #endif /* OPENSSL_NO_KRB5 */
  2003. #ifndef OPENSSL_NO_ECDH
  2004. EC_KEY *srvr_ecdh = NULL;
  2005. EVP_PKEY *clnt_pub_pkey = NULL;
  2006. EC_POINT *clnt_ecpoint = NULL;
  2007. BN_CTX *bn_ctx = NULL;
  2008. #endif
  2009. n = s->method->ssl_get_message(s,
  2010. SSL3_ST_SR_KEY_EXCH_A,
  2011. SSL3_ST_SR_KEY_EXCH_B,
  2012. SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, &ok);
  2013. if (!ok)
  2014. return ((int)n);
  2015. p = (unsigned char *)s->init_msg;
  2016. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  2017. #ifndef OPENSSL_NO_RSA
  2018. if (alg_k & SSL_kRSA) {
  2019. unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
  2020. int decrypt_len;
  2021. unsigned char decrypt_good, version_good;
  2022. size_t j;
  2023. /* FIX THIS UP EAY EAY EAY EAY */
  2024. if (s->s3->tmp.use_rsa_tmp) {
  2025. if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
  2026. rsa = s->cert->rsa_tmp;
  2027. /*
  2028. * Don't do a callback because rsa_tmp should be sent already
  2029. */
  2030. if (rsa == NULL) {
  2031. al = SSL_AD_HANDSHAKE_FAILURE;
  2032. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2033. SSL_R_MISSING_TMP_RSA_PKEY);
  2034. goto f_err;
  2035. }
  2036. } else {
  2037. pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
  2038. if ((pkey == NULL) ||
  2039. (pkey->type != EVP_PKEY_RSA) || (pkey->pkey.rsa == NULL)) {
  2040. al = SSL_AD_HANDSHAKE_FAILURE;
  2041. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2042. SSL_R_MISSING_RSA_CERTIFICATE);
  2043. goto f_err;
  2044. }
  2045. rsa = pkey->pkey.rsa;
  2046. }
  2047. /* TLS and [incidentally] DTLS{0xFEFF} */
  2048. if (s->version > SSL3_VERSION && s->version != DTLS1_BAD_VER) {
  2049. n2s(p, i);
  2050. if (n != i + 2) {
  2051. if (!(s->options & SSL_OP_TLS_D5_BUG)) {
  2052. al = SSL_AD_DECODE_ERROR;
  2053. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2054. SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
  2055. goto f_err;
  2056. } else
  2057. p -= 2;
  2058. } else
  2059. n = i;
  2060. }
  2061. /*
  2062. * Reject overly short RSA ciphertext because we want to be sure
  2063. * that the buffer size makes it safe to iterate over the entire
  2064. * size of a premaster secret (SSL_MAX_MASTER_KEY_LENGTH). The
  2065. * actual expected size is larger due to RSA padding, but the
  2066. * bound is sufficient to be safe.
  2067. */
  2068. if (n < SSL_MAX_MASTER_KEY_LENGTH) {
  2069. al = SSL_AD_DECRYPT_ERROR;
  2070. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2071. SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
  2072. goto f_err;
  2073. }
  2074. /*
  2075. * We must not leak whether a decryption failure occurs because of
  2076. * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
  2077. * section 7.4.7.1). The code follows that advice of the TLS RFC and
  2078. * generates a random premaster secret for the case that the decrypt
  2079. * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
  2080. */
  2081. /*
  2082. * should be RAND_bytes, but we cannot work around a failure.
  2083. */
  2084. if (RAND_pseudo_bytes(rand_premaster_secret,
  2085. sizeof(rand_premaster_secret)) <= 0)
  2086. goto err;
  2087. decrypt_len =
  2088. RSA_private_decrypt((int)n, p, p, rsa, RSA_PKCS1_PADDING);
  2089. ERR_clear_error();
  2090. /*
  2091. * decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. decrypt_good will
  2092. * be 0xff if so and zero otherwise.
  2093. */
  2094. decrypt_good =
  2095. constant_time_eq_int_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
  2096. /*
  2097. * If the version in the decrypted pre-master secret is correct then
  2098. * version_good will be 0xff, otherwise it'll be zero. The
  2099. * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
  2100. * (http://eprint.iacr.org/2003/052/) exploits the version number
  2101. * check as a "bad version oracle". Thus version checks are done in
  2102. * constant time and are treated like any other decryption error.
  2103. */
  2104. version_good =
  2105. constant_time_eq_8(p[0], (unsigned)(s->client_version >> 8));
  2106. version_good &=
  2107. constant_time_eq_8(p[1], (unsigned)(s->client_version & 0xff));
  2108. /*
  2109. * The premaster secret must contain the same version number as the
  2110. * ClientHello to detect version rollback attacks (strangely, the
  2111. * protocol does not offer such protection for DH ciphersuites).
  2112. * However, buggy clients exist that send the negotiated protocol
  2113. * version instead if the server does not support the requested
  2114. * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
  2115. * clients.
  2116. */
  2117. if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
  2118. unsigned char workaround_good;
  2119. workaround_good =
  2120. constant_time_eq_8(p[0], (unsigned)(s->version >> 8));
  2121. workaround_good &=
  2122. constant_time_eq_8(p[1], (unsigned)(s->version & 0xff));
  2123. version_good |= workaround_good;
  2124. }
  2125. /*
  2126. * Both decryption and version must be good for decrypt_good to
  2127. * remain non-zero (0xff).
  2128. */
  2129. decrypt_good &= version_good;
  2130. /*
  2131. * Now copy rand_premaster_secret over from p using
  2132. * decrypt_good_mask. If decryption failed, then p does not
  2133. * contain valid plaintext, however, a check above guarantees
  2134. * it is still sufficiently large to read from.
  2135. */
  2136. for (j = 0; j < sizeof(rand_premaster_secret); j++) {
  2137. p[j] = constant_time_select_8(decrypt_good, p[j],
  2138. rand_premaster_secret[j]);
  2139. }
  2140. s->session->master_key_length =
  2141. s->method->ssl3_enc->generate_master_secret(s,
  2142. s->
  2143. session->master_key,
  2144. p,
  2145. sizeof
  2146. (rand_premaster_secret));
  2147. OPENSSL_cleanse(p, sizeof(rand_premaster_secret));
  2148. } else
  2149. #endif
  2150. #ifndef OPENSSL_NO_DH
  2151. if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
  2152. n2s(p, i);
  2153. if (n != i + 2) {
  2154. if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) {
  2155. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2156. SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
  2157. goto err;
  2158. } else {
  2159. p -= 2;
  2160. i = (int)n;
  2161. }
  2162. }
  2163. if (n == 0L) { /* the parameters are in the cert */
  2164. al = SSL_AD_HANDSHAKE_FAILURE;
  2165. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2166. SSL_R_UNABLE_TO_DECODE_DH_CERTS);
  2167. goto f_err;
  2168. } else {
  2169. if (s->s3->tmp.dh == NULL) {
  2170. al = SSL_AD_HANDSHAKE_FAILURE;
  2171. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2172. SSL_R_MISSING_TMP_DH_KEY);
  2173. goto f_err;
  2174. } else
  2175. dh_srvr = s->s3->tmp.dh;
  2176. }
  2177. pub = BN_bin2bn(p, i, NULL);
  2178. if (pub == NULL) {
  2179. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_BN_LIB);
  2180. goto err;
  2181. }
  2182. i = DH_compute_key(p, pub, dh_srvr);
  2183. if (i <= 0) {
  2184. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
  2185. BN_clear_free(pub);
  2186. goto err;
  2187. }
  2188. DH_free(s->s3->tmp.dh);
  2189. s->s3->tmp.dh = NULL;
  2190. BN_clear_free(pub);
  2191. pub = NULL;
  2192. s->session->master_key_length =
  2193. s->method->ssl3_enc->generate_master_secret(s,
  2194. s->
  2195. session->master_key,
  2196. p, i);
  2197. OPENSSL_cleanse(p, i);
  2198. } else
  2199. #endif
  2200. #ifndef OPENSSL_NO_KRB5
  2201. if (alg_k & SSL_kKRB5) {
  2202. krb5_error_code krb5rc;
  2203. krb5_data enc_ticket;
  2204. krb5_data authenticator;
  2205. krb5_data enc_pms;
  2206. KSSL_CTX *kssl_ctx = s->kssl_ctx;
  2207. EVP_CIPHER_CTX ciph_ctx;
  2208. const EVP_CIPHER *enc = NULL;
  2209. unsigned char iv[EVP_MAX_IV_LENGTH];
  2210. unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_BLOCK_LENGTH];
  2211. int padl, outl;
  2212. krb5_timestamp authtime = 0;
  2213. krb5_ticket_times ttimes;
  2214. int kerr = 0;
  2215. EVP_CIPHER_CTX_init(&ciph_ctx);
  2216. if (!kssl_ctx)
  2217. kssl_ctx = kssl_ctx_new();
  2218. n2s(p, i);
  2219. enc_ticket.length = i;
  2220. if (n < (long)(enc_ticket.length + 6)) {
  2221. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2222. SSL_R_DATA_LENGTH_TOO_LONG);
  2223. goto err;
  2224. }
  2225. enc_ticket.data = (char *)p;
  2226. p += enc_ticket.length;
  2227. n2s(p, i);
  2228. authenticator.length = i;
  2229. if (n < (long)(enc_ticket.length + authenticator.length + 6)) {
  2230. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2231. SSL_R_DATA_LENGTH_TOO_LONG);
  2232. goto err;
  2233. }
  2234. authenticator.data = (char *)p;
  2235. p += authenticator.length;
  2236. n2s(p, i);
  2237. enc_pms.length = i;
  2238. enc_pms.data = (char *)p;
  2239. p += enc_pms.length;
  2240. /*
  2241. * Note that the length is checked again below, ** after decryption
  2242. */
  2243. if (enc_pms.length > sizeof pms) {
  2244. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2245. SSL_R_DATA_LENGTH_TOO_LONG);
  2246. goto err;
  2247. }
  2248. if (n != (long)(enc_ticket.length + authenticator.length +
  2249. enc_pms.length + 6)) {
  2250. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2251. SSL_R_DATA_LENGTH_TOO_LONG);
  2252. goto err;
  2253. }
  2254. if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
  2255. &kssl_err)) != 0) {
  2256. # ifdef KSSL_DEBUG
  2257. fprintf(stderr, "kssl_sget_tkt rtn %d [%d]\n",
  2258. krb5rc, kssl_err.reason);
  2259. if (kssl_err.text)
  2260. fprintf(stderr, "kssl_err text= %s\n", kssl_err.text);
  2261. # endif /* KSSL_DEBUG */
  2262. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, kssl_err.reason);
  2263. goto err;
  2264. }
  2265. /*
  2266. * Note: no authenticator is not considered an error, ** but will
  2267. * return authtime == 0.
  2268. */
  2269. if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
  2270. &authtime, &kssl_err)) != 0) {
  2271. # ifdef KSSL_DEBUG
  2272. fprintf(stderr, "kssl_check_authent rtn %d [%d]\n",
  2273. krb5rc, kssl_err.reason);
  2274. if (kssl_err.text)
  2275. fprintf(stderr, "kssl_err text= %s\n", kssl_err.text);
  2276. # endif /* KSSL_DEBUG */
  2277. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, kssl_err.reason);
  2278. goto err;
  2279. }
  2280. if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0) {
  2281. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);
  2282. goto err;
  2283. }
  2284. # ifdef KSSL_DEBUG
  2285. kssl_ctx_show(kssl_ctx);
  2286. # endif /* KSSL_DEBUG */
  2287. enc = kssl_map_enc(kssl_ctx->enctype);
  2288. if (enc == NULL)
  2289. goto err;
  2290. memset(iv, 0, sizeof iv); /* per RFC 1510 */
  2291. if (!EVP_DecryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv)) {
  2292. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2293. SSL_R_DECRYPTION_FAILED);
  2294. goto err;
  2295. }
  2296. if (!EVP_DecryptUpdate(&ciph_ctx, pms, &outl,
  2297. (unsigned char *)enc_pms.data, enc_pms.length))
  2298. {
  2299. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2300. SSL_R_DECRYPTION_FAILED);
  2301. kerr = 1;
  2302. goto kclean;
  2303. }
  2304. if (outl > SSL_MAX_MASTER_KEY_LENGTH) {
  2305. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2306. SSL_R_DATA_LENGTH_TOO_LONG);
  2307. kerr = 1;
  2308. goto kclean;
  2309. }
  2310. if (!EVP_DecryptFinal_ex(&ciph_ctx, &(pms[outl]), &padl)) {
  2311. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2312. SSL_R_DECRYPTION_FAILED);
  2313. kerr = 1;
  2314. goto kclean;
  2315. }
  2316. outl += padl;
  2317. if (outl > SSL_MAX_MASTER_KEY_LENGTH) {
  2318. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2319. SSL_R_DATA_LENGTH_TOO_LONG);
  2320. kerr = 1;
  2321. goto kclean;
  2322. }
  2323. if (!((pms[0] == (s->client_version >> 8))
  2324. && (pms[1] == (s->client_version & 0xff)))) {
  2325. /*
  2326. * The premaster secret must contain the same version number as
  2327. * the ClientHello to detect version rollback attacks (strangely,
  2328. * the protocol does not offer such protection for DH
  2329. * ciphersuites). However, buggy clients exist that send random
  2330. * bytes instead of the protocol version. If
  2331. * SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients.
  2332. * (Perhaps we should have a separate BUG value for the Kerberos
  2333. * cipher)
  2334. */
  2335. if (!(s->options & SSL_OP_TLS_ROLLBACK_BUG)) {
  2336. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2337. SSL_AD_DECODE_ERROR);
  2338. kerr = 1;
  2339. goto kclean;
  2340. }
  2341. }
  2342. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  2343. s->session->master_key_length =
  2344. s->method->ssl3_enc->generate_master_secret(s,
  2345. s->
  2346. session->master_key,
  2347. pms, outl);
  2348. if (kssl_ctx->client_princ) {
  2349. size_t len = strlen(kssl_ctx->client_princ);
  2350. if (len < SSL_MAX_KRB5_PRINCIPAL_LENGTH) {
  2351. s->session->krb5_client_princ_len = len;
  2352. memcpy(s->session->krb5_client_princ, kssl_ctx->client_princ,
  2353. len);
  2354. }
  2355. }
  2356. /*- Was doing kssl_ctx_free() here,
  2357. * but it caused problems for apache.
  2358. * kssl_ctx = kssl_ctx_free(kssl_ctx);
  2359. * if (s->kssl_ctx) s->kssl_ctx = NULL;
  2360. */
  2361. kclean:
  2362. OPENSSL_cleanse(pms, sizeof(pms));
  2363. if (kerr)
  2364. goto err;
  2365. } else
  2366. #endif /* OPENSSL_NO_KRB5 */
  2367. #ifndef OPENSSL_NO_ECDH
  2368. if (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)) {
  2369. int ret = 1;
  2370. int field_size = 0;
  2371. const EC_KEY *tkey;
  2372. const EC_GROUP *group;
  2373. const BIGNUM *priv_key;
  2374. /* initialize structures for server's ECDH key pair */
  2375. if ((srvr_ecdh = EC_KEY_new()) == NULL) {
  2376. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  2377. goto err;
  2378. }
  2379. /* Let's get server private key and group information */
  2380. if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
  2381. /* use the certificate */
  2382. tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
  2383. } else {
  2384. /*
  2385. * use the ephermeral values we saved when generating the
  2386. * ServerKeyExchange msg.
  2387. */
  2388. tkey = s->s3->tmp.ecdh;
  2389. }
  2390. group = EC_KEY_get0_group(tkey);
  2391. priv_key = EC_KEY_get0_private_key(tkey);
  2392. if (!EC_KEY_set_group(srvr_ecdh, group) ||
  2393. !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
  2394. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
  2395. goto err;
  2396. }
  2397. /* Let's get client's public key */
  2398. if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) {
  2399. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  2400. goto err;
  2401. }
  2402. if (n == 0L) {
  2403. /* Client Publickey was in Client Certificate */
  2404. if (alg_k & SSL_kEECDH) {
  2405. al = SSL_AD_HANDSHAKE_FAILURE;
  2406. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2407. SSL_R_MISSING_TMP_ECDH_KEY);
  2408. goto f_err;
  2409. }
  2410. if (((clnt_pub_pkey = X509_get_pubkey(s->session->peer))
  2411. == NULL) || (clnt_pub_pkey->type != EVP_PKEY_EC)) {
  2412. /*
  2413. * XXX: For now, we do not support client authentication
  2414. * using ECDH certificates so this branch (n == 0L) of the
  2415. * code is never executed. When that support is added, we
  2416. * ought to ensure the key received in the certificate is
  2417. * authorized for key agreement. ECDH_compute_key implicitly
  2418. * checks that the two ECDH shares are for the same group.
  2419. */
  2420. al = SSL_AD_HANDSHAKE_FAILURE;
  2421. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2422. SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
  2423. goto f_err;
  2424. }
  2425. if (EC_POINT_copy(clnt_ecpoint,
  2426. EC_KEY_get0_public_key(clnt_pub_pkey->
  2427. pkey.ec)) == 0) {
  2428. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
  2429. goto err;
  2430. }
  2431. ret = 2; /* Skip certificate verify processing */
  2432. } else {
  2433. /*
  2434. * Get client's public key from encoded point in the
  2435. * ClientKeyExchange message.
  2436. */
  2437. if ((bn_ctx = BN_CTX_new()) == NULL) {
  2438. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2439. ERR_R_MALLOC_FAILURE);
  2440. goto err;
  2441. }
  2442. /* Get encoded point length */
  2443. i = *p;
  2444. p += 1;
  2445. if (n != 1 + i) {
  2446. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
  2447. goto err;
  2448. }
  2449. if (EC_POINT_oct2point(group, clnt_ecpoint, p, i, bn_ctx) == 0) {
  2450. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
  2451. goto err;
  2452. }
  2453. /*
  2454. * p is pointing to somewhere in the buffer currently, so set it
  2455. * to the start
  2456. */
  2457. p = (unsigned char *)s->init_buf->data;
  2458. }
  2459. /* Compute the shared pre-master secret */
  2460. field_size = EC_GROUP_get_degree(group);
  2461. if (field_size <= 0) {
  2462. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  2463. goto err;
  2464. }
  2465. i = ECDH_compute_key(p, (field_size + 7) / 8, clnt_ecpoint, srvr_ecdh,
  2466. NULL);
  2467. if (i <= 0) {
  2468. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  2469. goto err;
  2470. }
  2471. EVP_PKEY_free(clnt_pub_pkey);
  2472. EC_POINT_free(clnt_ecpoint);
  2473. EC_KEY_free(srvr_ecdh);
  2474. BN_CTX_free(bn_ctx);
  2475. EC_KEY_free(s->s3->tmp.ecdh);
  2476. s->s3->tmp.ecdh = NULL;
  2477. /* Compute the master secret */
  2478. s->session->master_key_length =
  2479. s->method->ssl3_enc->generate_master_secret(s,
  2480. s->
  2481. session->master_key,
  2482. p, i);
  2483. OPENSSL_cleanse(p, i);
  2484. return (ret);
  2485. } else
  2486. #endif
  2487. #ifndef OPENSSL_NO_PSK
  2488. if (alg_k & SSL_kPSK) {
  2489. unsigned char *t = NULL;
  2490. unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
  2491. unsigned int pre_ms_len = 0, psk_len = 0;
  2492. int psk_err = 1;
  2493. char tmp_id[PSK_MAX_IDENTITY_LEN + 1];
  2494. al = SSL_AD_HANDSHAKE_FAILURE;
  2495. n2s(p, i);
  2496. if (n != i + 2) {
  2497. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
  2498. goto psk_err;
  2499. }
  2500. if (i > PSK_MAX_IDENTITY_LEN) {
  2501. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2502. SSL_R_DATA_LENGTH_TOO_LONG);
  2503. goto psk_err;
  2504. }
  2505. if (s->psk_server_callback == NULL) {
  2506. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2507. SSL_R_PSK_NO_SERVER_CB);
  2508. goto psk_err;
  2509. }
  2510. /*
  2511. * Create guaranteed NULL-terminated identity string for the callback
  2512. */
  2513. memcpy(tmp_id, p, i);
  2514. memset(tmp_id + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i);
  2515. psk_len = s->psk_server_callback(s, tmp_id,
  2516. psk_or_pre_ms,
  2517. sizeof(psk_or_pre_ms));
  2518. OPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN + 1);
  2519. if (psk_len > PSK_MAX_PSK_LEN) {
  2520. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  2521. goto psk_err;
  2522. } else if (psk_len == 0) {
  2523. /*
  2524. * PSK related to the given identity not found
  2525. */
  2526. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2527. SSL_R_PSK_IDENTITY_NOT_FOUND);
  2528. al = SSL_AD_UNKNOWN_PSK_IDENTITY;
  2529. goto psk_err;
  2530. }
  2531. /* create PSK pre_master_secret */
  2532. pre_ms_len = 2 + psk_len + 2 + psk_len;
  2533. t = psk_or_pre_ms;
  2534. memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
  2535. s2n(psk_len, t);
  2536. memset(t, 0, psk_len);
  2537. t += psk_len;
  2538. s2n(psk_len, t);
  2539. if (s->session->psk_identity != NULL)
  2540. OPENSSL_free(s->session->psk_identity);
  2541. s->session->psk_identity = BUF_strndup((char *)p, i);
  2542. if (s->session->psk_identity == NULL) {
  2543. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  2544. goto psk_err;
  2545. }
  2546. if (s->session->psk_identity_hint != NULL)
  2547. OPENSSL_free(s->session->psk_identity_hint);
  2548. s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
  2549. if (s->ctx->psk_identity_hint != NULL &&
  2550. s->session->psk_identity_hint == NULL) {
  2551. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  2552. goto psk_err;
  2553. }
  2554. s->session->master_key_length =
  2555. s->method->ssl3_enc->generate_master_secret(s,
  2556. s->
  2557. session->master_key,
  2558. psk_or_pre_ms,
  2559. pre_ms_len);
  2560. psk_err = 0;
  2561. psk_err:
  2562. OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
  2563. if (psk_err != 0)
  2564. goto f_err;
  2565. } else
  2566. #endif
  2567. #ifndef OPENSSL_NO_SRP
  2568. if (alg_k & SSL_kSRP) {
  2569. int param_len;
  2570. n2s(p, i);
  2571. param_len = i + 2;
  2572. if (param_len > n) {
  2573. al = SSL_AD_DECODE_ERROR;
  2574. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2575. SSL_R_BAD_SRP_A_LENGTH);
  2576. goto f_err;
  2577. }
  2578. if (!(s->srp_ctx.A = BN_bin2bn(p, i, NULL))) {
  2579. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_BN_LIB);
  2580. goto err;
  2581. }
  2582. if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0
  2583. || BN_is_zero(s->srp_ctx.A)) {
  2584. al = SSL_AD_ILLEGAL_PARAMETER;
  2585. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2586. SSL_R_BAD_SRP_PARAMETERS);
  2587. goto f_err;
  2588. }
  2589. if (s->session->srp_username != NULL)
  2590. OPENSSL_free(s->session->srp_username);
  2591. s->session->srp_username = BUF_strdup(s->srp_ctx.login);
  2592. if (s->session->srp_username == NULL) {
  2593. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  2594. goto err;
  2595. }
  2596. if ((s->session->master_key_length =
  2597. SRP_generate_server_master_secret(s,
  2598. s->session->master_key)) < 0) {
  2599. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  2600. goto err;
  2601. }
  2602. p += i;
  2603. } else
  2604. #endif /* OPENSSL_NO_SRP */
  2605. if (alg_k & SSL_kGOST) {
  2606. int ret = 0;
  2607. EVP_PKEY_CTX *pkey_ctx;
  2608. EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
  2609. unsigned char premaster_secret[32], *start;
  2610. size_t outlen = 32, inlen;
  2611. unsigned long alg_a;
  2612. int Ttag, Tclass;
  2613. long Tlen;
  2614. /* Get our certificate private key */
  2615. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  2616. if (alg_a & SSL_aGOST94)
  2617. pk = s->cert->pkeys[SSL_PKEY_GOST94].privatekey;
  2618. else if (alg_a & SSL_aGOST01)
  2619. pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
  2620. pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
  2621. if (pkey_ctx == NULL) {
  2622. al = SSL_AD_INTERNAL_ERROR;
  2623. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  2624. goto f_err;
  2625. }
  2626. if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
  2627. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  2628. goto gerr;
  2629. }
  2630. /*
  2631. * If client certificate is present and is of the same type, maybe
  2632. * use it for key exchange. Don't mind errors from
  2633. * EVP_PKEY_derive_set_peer, because it is completely valid to use a
  2634. * client certificate for authorization only.
  2635. */
  2636. client_pub_pkey = X509_get_pubkey(s->session->peer);
  2637. if (client_pub_pkey) {
  2638. if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
  2639. ERR_clear_error();
  2640. }
  2641. /* Decrypt session key */
  2642. if (ASN1_get_object
  2643. ((const unsigned char **)&p, &Tlen, &Ttag, &Tclass,
  2644. n) != V_ASN1_CONSTRUCTED || Ttag != V_ASN1_SEQUENCE
  2645. || Tclass != V_ASN1_UNIVERSAL) {
  2646. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2647. SSL_R_DECRYPTION_FAILED);
  2648. goto gerr;
  2649. }
  2650. start = p;
  2651. inlen = Tlen;
  2652. if (EVP_PKEY_decrypt
  2653. (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
  2654. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2655. SSL_R_DECRYPTION_FAILED);
  2656. goto gerr;
  2657. }
  2658. /* Generate master secret */
  2659. s->session->master_key_length =
  2660. s->method->ssl3_enc->generate_master_secret(s,
  2661. s->
  2662. session->master_key,
  2663. premaster_secret, 32);
  2664. OPENSSL_cleanse(premaster_secret, sizeof(premaster_secret));
  2665. /* Check if pubkey from client certificate was used */
  2666. if (EVP_PKEY_CTX_ctrl
  2667. (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
  2668. ret = 2;
  2669. else
  2670. ret = 1;
  2671. gerr:
  2672. EVP_PKEY_free(client_pub_pkey);
  2673. EVP_PKEY_CTX_free(pkey_ctx);
  2674. if (ret)
  2675. return ret;
  2676. else
  2677. goto err;
  2678. } else {
  2679. al = SSL_AD_HANDSHAKE_FAILURE;
  2680. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_UNKNOWN_CIPHER_TYPE);
  2681. goto f_err;
  2682. }
  2683. return (1);
  2684. f_err:
  2685. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  2686. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH) || defined(OPENSSL_NO_SRP)
  2687. err:
  2688. #endif
  2689. #ifndef OPENSSL_NO_ECDH
  2690. EVP_PKEY_free(clnt_pub_pkey);
  2691. EC_POINT_free(clnt_ecpoint);
  2692. if (srvr_ecdh != NULL)
  2693. EC_KEY_free(srvr_ecdh);
  2694. BN_CTX_free(bn_ctx);
  2695. #endif
  2696. s->state = SSL_ST_ERR;
  2697. return (-1);
  2698. }
  2699. int ssl3_get_cert_verify(SSL *s)
  2700. {
  2701. EVP_PKEY *pkey = NULL;
  2702. unsigned char *p;
  2703. int al, ok, ret = 0;
  2704. long n;
  2705. int type = 0, i, j;
  2706. X509 *peer;
  2707. const EVP_MD *md = NULL;
  2708. EVP_MD_CTX mctx;
  2709. EVP_MD_CTX_init(&mctx);
  2710. /*
  2711. * We should only process a CertificateVerify message if we have received
  2712. * a Certificate from the client. If so then |s->session->peer| will be non
  2713. * NULL. In some instances a CertificateVerify message is not required even
  2714. * if the peer has sent a Certificate (e.g. such as in the case of static
  2715. * DH). In that case the ClientKeyExchange processing will skip the
  2716. * CertificateVerify state so we should not arrive here.
  2717. */
  2718. if (s->session->peer == NULL) {
  2719. ret = 1;
  2720. goto end;
  2721. }
  2722. n = s->method->ssl_get_message(s,
  2723. SSL3_ST_SR_CERT_VRFY_A,
  2724. SSL3_ST_SR_CERT_VRFY_B,
  2725. SSL3_MT_CERTIFICATE_VERIFY,
  2726. SSL3_RT_MAX_PLAIN_LENGTH, &ok);
  2727. if (!ok)
  2728. return ((int)n);
  2729. peer = s->session->peer;
  2730. pkey = X509_get_pubkey(peer);
  2731. type = X509_certificate_type(peer, pkey);
  2732. if (!(type & EVP_PKT_SIGN)) {
  2733. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
  2734. SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
  2735. al = SSL_AD_ILLEGAL_PARAMETER;
  2736. goto f_err;
  2737. }
  2738. /* we now have a signature that we need to verify */
  2739. p = (unsigned char *)s->init_msg;
  2740. /* Check for broken implementations of GOST ciphersuites */
  2741. /*
  2742. * If key is GOST and n is exactly 64, it is bare signature without
  2743. * length field
  2744. */
  2745. if (n == 64 && (pkey->type == NID_id_GostR3410_94 ||
  2746. pkey->type == NID_id_GostR3410_2001)) {
  2747. i = 64;
  2748. } else {
  2749. if (TLS1_get_version(s) >= TLS1_2_VERSION) {
  2750. int sigalg = tls12_get_sigid(pkey);
  2751. /* Should never happen */
  2752. if (sigalg == -1) {
  2753. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
  2754. al = SSL_AD_INTERNAL_ERROR;
  2755. goto f_err;
  2756. }
  2757. /* Check key type is consistent with signature */
  2758. if (sigalg != (int)p[1]) {
  2759. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
  2760. SSL_R_WRONG_SIGNATURE_TYPE);
  2761. al = SSL_AD_DECODE_ERROR;
  2762. goto f_err;
  2763. }
  2764. md = tls12_get_hash(p[0]);
  2765. if (md == NULL) {
  2766. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_UNKNOWN_DIGEST);
  2767. al = SSL_AD_DECODE_ERROR;
  2768. goto f_err;
  2769. }
  2770. #ifdef SSL_DEBUG
  2771. fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
  2772. #endif
  2773. p += 2;
  2774. n -= 2;
  2775. }
  2776. n2s(p, i);
  2777. n -= 2;
  2778. if (i > n) {
  2779. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
  2780. al = SSL_AD_DECODE_ERROR;
  2781. goto f_err;
  2782. }
  2783. }
  2784. j = EVP_PKEY_size(pkey);
  2785. if ((i > j) || (n > j) || (n <= 0)) {
  2786. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE);
  2787. al = SSL_AD_DECODE_ERROR;
  2788. goto f_err;
  2789. }
  2790. if (TLS1_get_version(s) >= TLS1_2_VERSION) {
  2791. long hdatalen = 0;
  2792. void *hdata;
  2793. hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
  2794. if (hdatalen <= 0) {
  2795. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
  2796. al = SSL_AD_INTERNAL_ERROR;
  2797. goto f_err;
  2798. }
  2799. #ifdef SSL_DEBUG
  2800. fprintf(stderr, "Using TLS 1.2 with client verify alg %s\n",
  2801. EVP_MD_name(md));
  2802. #endif
  2803. if (!EVP_VerifyInit_ex(&mctx, md, NULL)
  2804. || !EVP_VerifyUpdate(&mctx, hdata, hdatalen)) {
  2805. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_EVP_LIB);
  2806. al = SSL_AD_INTERNAL_ERROR;
  2807. goto f_err;
  2808. }
  2809. if (EVP_VerifyFinal(&mctx, p, i, pkey) <= 0) {
  2810. al = SSL_AD_DECRYPT_ERROR;
  2811. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_SIGNATURE);
  2812. goto f_err;
  2813. }
  2814. } else
  2815. #ifndef OPENSSL_NO_RSA
  2816. if (pkey->type == EVP_PKEY_RSA) {
  2817. i = RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
  2818. MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, p, i,
  2819. pkey->pkey.rsa);
  2820. if (i < 0) {
  2821. al = SSL_AD_DECRYPT_ERROR;
  2822. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_DECRYPT);
  2823. goto f_err;
  2824. }
  2825. if (i == 0) {
  2826. al = SSL_AD_DECRYPT_ERROR;
  2827. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_SIGNATURE);
  2828. goto f_err;
  2829. }
  2830. } else
  2831. #endif
  2832. #ifndef OPENSSL_NO_DSA
  2833. if (pkey->type == EVP_PKEY_DSA) {
  2834. j = DSA_verify(pkey->save_type,
  2835. &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
  2836. SHA_DIGEST_LENGTH, p, i, pkey->pkey.dsa);
  2837. if (j <= 0) {
  2838. /* bad signature */
  2839. al = SSL_AD_DECRYPT_ERROR;
  2840. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_DSA_SIGNATURE);
  2841. goto f_err;
  2842. }
  2843. } else
  2844. #endif
  2845. #ifndef OPENSSL_NO_ECDSA
  2846. if (pkey->type == EVP_PKEY_EC) {
  2847. j = ECDSA_verify(pkey->save_type,
  2848. &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
  2849. SHA_DIGEST_LENGTH, p, i, pkey->pkey.ec);
  2850. if (j <= 0) {
  2851. /* bad signature */
  2852. al = SSL_AD_DECRYPT_ERROR;
  2853. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
  2854. goto f_err;
  2855. }
  2856. } else
  2857. #endif
  2858. if (pkey->type == NID_id_GostR3410_94
  2859. || pkey->type == NID_id_GostR3410_2001) {
  2860. unsigned char signature[64];
  2861. int idx;
  2862. EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL);
  2863. if (pctx == NULL) {
  2864. al = SSL_AD_INTERNAL_ERROR;
  2865. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
  2866. goto f_err;
  2867. }
  2868. if (EVP_PKEY_verify_init(pctx) <= 0) {
  2869. EVP_PKEY_CTX_free(pctx);
  2870. al = SSL_AD_INTERNAL_ERROR;
  2871. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
  2872. goto f_err;
  2873. }
  2874. if (i != 64) {
  2875. fprintf(stderr, "GOST signature length is %d", i);
  2876. }
  2877. for (idx = 0; idx < 64; idx++) {
  2878. signature[63 - idx] = p[idx];
  2879. }
  2880. j = EVP_PKEY_verify(pctx, signature, 64, s->s3->tmp.cert_verify_md,
  2881. 32);
  2882. EVP_PKEY_CTX_free(pctx);
  2883. if (j <= 0) {
  2884. al = SSL_AD_DECRYPT_ERROR;
  2885. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
  2886. goto f_err;
  2887. }
  2888. } else {
  2889. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
  2890. al = SSL_AD_UNSUPPORTED_CERTIFICATE;
  2891. goto f_err;
  2892. }
  2893. ret = 1;
  2894. if (0) {
  2895. f_err:
  2896. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  2897. s->state = SSL_ST_ERR;
  2898. }
  2899. end:
  2900. if (s->s3->handshake_buffer) {
  2901. BIO_free(s->s3->handshake_buffer);
  2902. s->s3->handshake_buffer = NULL;
  2903. s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE;
  2904. }
  2905. EVP_MD_CTX_cleanup(&mctx);
  2906. EVP_PKEY_free(pkey);
  2907. return (ret);
  2908. }
  2909. int ssl3_get_client_certificate(SSL *s)
  2910. {
  2911. int i, ok, al, ret = -1;
  2912. X509 *x = NULL;
  2913. unsigned long l, nc, llen, n;
  2914. const unsigned char *p, *q;
  2915. unsigned char *d;
  2916. STACK_OF(X509) *sk = NULL;
  2917. n = s->method->ssl_get_message(s,
  2918. SSL3_ST_SR_CERT_A,
  2919. SSL3_ST_SR_CERT_B,
  2920. -1, s->max_cert_list, &ok);
  2921. if (!ok)
  2922. return ((int)n);
  2923. if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
  2924. if ((s->verify_mode & SSL_VERIFY_PEER) &&
  2925. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
  2926. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
  2927. SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  2928. al = SSL_AD_HANDSHAKE_FAILURE;
  2929. goto f_err;
  2930. }
  2931. /*
  2932. * If tls asked for a client cert, the client must return a 0 list
  2933. */
  2934. if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) {
  2935. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
  2936. SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
  2937. al = SSL_AD_UNEXPECTED_MESSAGE;
  2938. goto f_err;
  2939. }
  2940. s->s3->tmp.reuse_message = 1;
  2941. return (1);
  2942. }
  2943. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
  2944. al = SSL_AD_UNEXPECTED_MESSAGE;
  2945. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_WRONG_MESSAGE_TYPE);
  2946. goto f_err;
  2947. }
  2948. p = d = (unsigned char *)s->init_msg;
  2949. if ((sk = sk_X509_new_null()) == NULL) {
  2950. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  2951. goto err;
  2952. }
  2953. n2l3(p, llen);
  2954. if (llen + 3 != n) {
  2955. al = SSL_AD_DECODE_ERROR;
  2956. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
  2957. goto f_err;
  2958. }
  2959. for (nc = 0; nc < llen;) {
  2960. n2l3(p, l);
  2961. if ((l + nc + 3) > llen) {
  2962. al = SSL_AD_DECODE_ERROR;
  2963. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
  2964. SSL_R_CERT_LENGTH_MISMATCH);
  2965. goto f_err;
  2966. }
  2967. q = p;
  2968. x = d2i_X509(NULL, &p, l);
  2969. if (x == NULL) {
  2970. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
  2971. goto err;
  2972. }
  2973. if (p != (q + l)) {
  2974. al = SSL_AD_DECODE_ERROR;
  2975. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
  2976. SSL_R_CERT_LENGTH_MISMATCH);
  2977. goto f_err;
  2978. }
  2979. if (!sk_X509_push(sk, x)) {
  2980. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  2981. goto err;
  2982. }
  2983. x = NULL;
  2984. nc += l + 3;
  2985. }
  2986. if (sk_X509_num(sk) <= 0) {
  2987. /* TLS does not mind 0 certs returned */
  2988. if (s->version == SSL3_VERSION) {
  2989. al = SSL_AD_HANDSHAKE_FAILURE;
  2990. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
  2991. SSL_R_NO_CERTIFICATES_RETURNED);
  2992. goto f_err;
  2993. }
  2994. /* Fail for TLS only if we required a certificate */
  2995. else if ((s->verify_mode & SSL_VERIFY_PEER) &&
  2996. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
  2997. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
  2998. SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  2999. al = SSL_AD_HANDSHAKE_FAILURE;
  3000. goto f_err;
  3001. }
  3002. /* No client certificate so digest cached records */
  3003. if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s)) {
  3004. al = SSL_AD_INTERNAL_ERROR;
  3005. goto f_err;
  3006. }
  3007. } else {
  3008. i = ssl_verify_cert_chain(s, sk);
  3009. if (i <= 0) {
  3010. al = ssl_verify_alarm_type(s->verify_result);
  3011. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
  3012. SSL_R_NO_CERTIFICATE_RETURNED);
  3013. goto f_err;
  3014. }
  3015. }
  3016. if (s->session->peer != NULL) /* This should not be needed */
  3017. X509_free(s->session->peer);
  3018. s->session->peer = sk_X509_shift(sk);
  3019. s->session->verify_result = s->verify_result;
  3020. /*
  3021. * With the current implementation, sess_cert will always be NULL when we
  3022. * arrive here.
  3023. */
  3024. if (s->session->sess_cert == NULL) {
  3025. s->session->sess_cert = ssl_sess_cert_new();
  3026. if (s->session->sess_cert == NULL) {
  3027. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  3028. goto err;
  3029. }
  3030. }
  3031. if (s->session->sess_cert->cert_chain != NULL)
  3032. sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
  3033. s->session->sess_cert->cert_chain = sk;
  3034. /*
  3035. * Inconsistency alert: cert_chain does *not* include the peer's own
  3036. * certificate, while we do include it in s3_clnt.c
  3037. */
  3038. sk = NULL;
  3039. ret = 1;
  3040. if (0) {
  3041. f_err:
  3042. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  3043. err:
  3044. s->state = SSL_ST_ERR;
  3045. }
  3046. if (x != NULL)
  3047. X509_free(x);
  3048. if (sk != NULL)
  3049. sk_X509_pop_free(sk, X509_free);
  3050. return (ret);
  3051. }
  3052. int ssl3_send_server_certificate(SSL *s)
  3053. {
  3054. unsigned long l;
  3055. X509 *x;
  3056. if (s->state == SSL3_ST_SW_CERT_A) {
  3057. x = ssl_get_server_send_cert(s);
  3058. if (x == NULL) {
  3059. /* VRS: allow null cert if auth == KRB5 */
  3060. if ((s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5) ||
  3061. (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5)) {
  3062. SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,
  3063. ERR_R_INTERNAL_ERROR);
  3064. s->state = SSL_ST_ERR;
  3065. return (0);
  3066. }
  3067. }
  3068. l = ssl3_output_cert_chain(s, x);
  3069. if (!l) {
  3070. SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
  3071. s->state = SSL_ST_ERR;
  3072. return (0);
  3073. }
  3074. s->state = SSL3_ST_SW_CERT_B;
  3075. s->init_num = (int)l;
  3076. s->init_off = 0;
  3077. }
  3078. /* SSL3_ST_SW_CERT_B */
  3079. return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
  3080. }
  3081. #ifndef OPENSSL_NO_TLSEXT
  3082. /* send a new session ticket (not necessarily for a new session) */
  3083. int ssl3_send_newsession_ticket(SSL *s)
  3084. {
  3085. unsigned char *senc = NULL;
  3086. EVP_CIPHER_CTX ctx;
  3087. HMAC_CTX hctx;
  3088. if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
  3089. unsigned char *p, *macstart;
  3090. const unsigned char *const_p;
  3091. int len, slen_full, slen;
  3092. SSL_SESSION *sess;
  3093. unsigned int hlen;
  3094. SSL_CTX *tctx = s->initial_ctx;
  3095. unsigned char iv[EVP_MAX_IV_LENGTH];
  3096. unsigned char key_name[16];
  3097. /* get session encoding length */
  3098. slen_full = i2d_SSL_SESSION(s->session, NULL);
  3099. /*
  3100. * Some length values are 16 bits, so forget it if session is too
  3101. * long
  3102. */
  3103. if (slen_full == 0 || slen_full > 0xFF00) {
  3104. s->state = SSL_ST_ERR;
  3105. return -1;
  3106. }
  3107. senc = OPENSSL_malloc(slen_full);
  3108. if (!senc) {
  3109. s->state = SSL_ST_ERR;
  3110. return -1;
  3111. }
  3112. EVP_CIPHER_CTX_init(&ctx);
  3113. HMAC_CTX_init(&hctx);
  3114. p = senc;
  3115. if (!i2d_SSL_SESSION(s->session, &p))
  3116. goto err;
  3117. /*
  3118. * create a fresh copy (not shared with other threads) to clean up
  3119. */
  3120. const_p = senc;
  3121. sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
  3122. if (sess == NULL)
  3123. goto err;
  3124. sess->session_id_length = 0; /* ID is irrelevant for the ticket */
  3125. slen = i2d_SSL_SESSION(sess, NULL);
  3126. if (slen == 0 || slen > slen_full) { /* shouldn't ever happen */
  3127. SSL_SESSION_free(sess);
  3128. goto err;
  3129. }
  3130. p = senc;
  3131. if (!i2d_SSL_SESSION(sess, &p)) {
  3132. SSL_SESSION_free(sess);
  3133. goto err;
  3134. }
  3135. SSL_SESSION_free(sess);
  3136. /*-
  3137. * Grow buffer if need be: the length calculation is as
  3138. * follows 1 (size of message name) + 3 (message length
  3139. * bytes) + 4 (ticket lifetime hint) + 2 (ticket length) +
  3140. * 16 (key name) + max_iv_len (iv length) +
  3141. * session_length + max_enc_block_size (max encrypted session
  3142. * length) + max_md_size (HMAC).
  3143. */
  3144. if (!BUF_MEM_grow(s->init_buf,
  3145. 26 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH +
  3146. EVP_MAX_MD_SIZE + slen))
  3147. goto err;
  3148. p = (unsigned char *)s->init_buf->data;
  3149. /* do the header */
  3150. *(p++) = SSL3_MT_NEWSESSION_TICKET;
  3151. /* Skip message length for now */
  3152. p += 3;
  3153. /*
  3154. * Initialize HMAC and cipher contexts. If callback present it does
  3155. * all the work otherwise use generated values from parent ctx.
  3156. */
  3157. if (tctx->tlsext_ticket_key_cb) {
  3158. if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
  3159. &hctx, 1) < 0)
  3160. goto err;
  3161. } else {
  3162. if (RAND_bytes(iv, 16) <= 0)
  3163. goto err;
  3164. if (!EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
  3165. tctx->tlsext_tick_aes_key, iv))
  3166. goto err;
  3167. if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
  3168. tlsext_tick_md(), NULL))
  3169. goto err;
  3170. memcpy(key_name, tctx->tlsext_tick_key_name, 16);
  3171. }
  3172. /*
  3173. * Ticket lifetime hint (advisory only): We leave this unspecified
  3174. * for resumed session (for simplicity), and guess that tickets for
  3175. * new sessions will live as long as their sessions.
  3176. */
  3177. l2n(s->hit ? 0 : s->session->timeout, p);
  3178. /* Skip ticket length for now */
  3179. p += 2;
  3180. /* Output key name */
  3181. macstart = p;
  3182. memcpy(p, key_name, 16);
  3183. p += 16;
  3184. /* output IV */
  3185. memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
  3186. p += EVP_CIPHER_CTX_iv_length(&ctx);
  3187. /* Encrypt session data */
  3188. if (!EVP_EncryptUpdate(&ctx, p, &len, senc, slen))
  3189. goto err;
  3190. p += len;
  3191. if (!EVP_EncryptFinal(&ctx, p, &len))
  3192. goto err;
  3193. p += len;
  3194. if (!HMAC_Update(&hctx, macstart, p - macstart))
  3195. goto err;
  3196. if (!HMAC_Final(&hctx, p, &hlen))
  3197. goto err;
  3198. EVP_CIPHER_CTX_cleanup(&ctx);
  3199. HMAC_CTX_cleanup(&hctx);
  3200. p += hlen;
  3201. /* Now write out lengths: p points to end of data written */
  3202. /* Total length */
  3203. len = p - (unsigned char *)s->init_buf->data;
  3204. p = (unsigned char *)s->init_buf->data + 1;
  3205. l2n3(len - 4, p); /* Message length */
  3206. p += 4;
  3207. s2n(len - 10, p); /* Ticket length */
  3208. /* number of bytes to write */
  3209. s->init_num = len;
  3210. s->state = SSL3_ST_SW_SESSION_TICKET_B;
  3211. s->init_off = 0;
  3212. OPENSSL_free(senc);
  3213. }
  3214. /* SSL3_ST_SW_SESSION_TICKET_B */
  3215. return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
  3216. err:
  3217. if (senc)
  3218. OPENSSL_free(senc);
  3219. EVP_CIPHER_CTX_cleanup(&ctx);
  3220. HMAC_CTX_cleanup(&hctx);
  3221. s->state = SSL_ST_ERR;
  3222. return -1;
  3223. }
  3224. int ssl3_send_cert_status(SSL *s)
  3225. {
  3226. if (s->state == SSL3_ST_SW_CERT_STATUS_A) {
  3227. unsigned char *p;
  3228. /*-
  3229. * Grow buffer if need be: the length calculation is as
  3230. * follows 1 (message type) + 3 (message length) +
  3231. * 1 (ocsp response type) + 3 (ocsp response length)
  3232. * + (ocsp response)
  3233. */
  3234. if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen)) {
  3235. s->state = SSL_ST_ERR;
  3236. return -1;
  3237. }
  3238. p = (unsigned char *)s->init_buf->data;
  3239. /* do the header */
  3240. *(p++) = SSL3_MT_CERTIFICATE_STATUS;
  3241. /* message length */
  3242. l2n3(s->tlsext_ocsp_resplen + 4, p);
  3243. /* status type */
  3244. *(p++) = s->tlsext_status_type;
  3245. /* length of OCSP response */
  3246. l2n3(s->tlsext_ocsp_resplen, p);
  3247. /* actual response */
  3248. memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
  3249. /* number of bytes to write */
  3250. s->init_num = 8 + s->tlsext_ocsp_resplen;
  3251. s->state = SSL3_ST_SW_CERT_STATUS_B;
  3252. s->init_off = 0;
  3253. }
  3254. /* SSL3_ST_SW_CERT_STATUS_B */
  3255. return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
  3256. }
  3257. # ifndef OPENSSL_NO_NEXTPROTONEG
  3258. /*
  3259. * ssl3_get_next_proto reads a Next Protocol Negotiation handshake message.
  3260. * It sets the next_proto member in s if found
  3261. */
  3262. int ssl3_get_next_proto(SSL *s)
  3263. {
  3264. int ok;
  3265. int proto_len, padding_len;
  3266. long n;
  3267. const unsigned char *p;
  3268. /*
  3269. * Clients cannot send a NextProtocol message if we didn't see the
  3270. * extension in their ClientHello
  3271. */
  3272. if (!s->s3->next_proto_neg_seen) {
  3273. SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,
  3274. SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
  3275. s->state = SSL_ST_ERR;
  3276. return -1;
  3277. }
  3278. /* See the payload format below */
  3279. n = s->method->ssl_get_message(s,
  3280. SSL3_ST_SR_NEXT_PROTO_A,
  3281. SSL3_ST_SR_NEXT_PROTO_B,
  3282. SSL3_MT_NEXT_PROTO, 514, &ok);
  3283. if (!ok)
  3284. return ((int)n);
  3285. /*
  3286. * s->state doesn't reflect whether ChangeCipherSpec has been received in
  3287. * this handshake, but s->s3->change_cipher_spec does (will be reset by
  3288. * ssl3_get_finished).
  3289. */
  3290. if (!s->s3->change_cipher_spec) {
  3291. SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
  3292. s->state = SSL_ST_ERR;
  3293. return -1;
  3294. }
  3295. if (n < 2) {
  3296. s->state = SSL_ST_ERR;
  3297. return 0; /* The body must be > 1 bytes long */
  3298. }
  3299. p = (unsigned char *)s->init_msg;
  3300. /*-
  3301. * The payload looks like:
  3302. * uint8 proto_len;
  3303. * uint8 proto[proto_len];
  3304. * uint8 padding_len;
  3305. * uint8 padding[padding_len];
  3306. */
  3307. proto_len = p[0];
  3308. if (proto_len + 2 > s->init_num) {
  3309. s->state = SSL_ST_ERR;
  3310. return 0;
  3311. }
  3312. padding_len = p[proto_len + 1];
  3313. if (proto_len + padding_len + 2 != s->init_num) {
  3314. s->state = SSL_ST_ERR;
  3315. return 0;
  3316. }
  3317. s->next_proto_negotiated = OPENSSL_malloc(proto_len);
  3318. if (!s->next_proto_negotiated) {
  3319. SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, ERR_R_MALLOC_FAILURE);
  3320. s->state = SSL_ST_ERR;
  3321. return 0;
  3322. }
  3323. memcpy(s->next_proto_negotiated, p + 1, proto_len);
  3324. s->next_proto_negotiated_len = proto_len;
  3325. return 1;
  3326. }
  3327. # endif
  3328. #endif