s3_srvr.c 89 KB

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