s3_clnt.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373
  1. /* ssl/s3_clnt.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson ([email protected]).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young ([email protected])"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson ([email protected])"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * [email protected].
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * ([email protected]). This product includes software written by Tim
  108. * Hudson ([email protected]).
  109. *
  110. */
  111. /* ====================================================================
  112. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  113. *
  114. * Portions of the attached software ("Contribution") are developed by
  115. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  116. *
  117. * The Contribution is licensed pursuant to the OpenSSL open source
  118. * license provided above.
  119. *
  120. * ECC cipher suite support in OpenSSL originally written by
  121. * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
  122. *
  123. */
  124. /* ====================================================================
  125. * Copyright 2005 Nokia. All rights reserved.
  126. *
  127. * The portions of the attached software ("Contribution") is developed by
  128. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  129. * license.
  130. *
  131. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  132. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  133. * support (see RFC 4279) to OpenSSL.
  134. *
  135. * No patent licenses or other rights except those expressly stated in
  136. * the OpenSSL open source license shall be deemed granted or received
  137. * expressly, by implication, estoppel, or otherwise.
  138. *
  139. * No assurances are provided by Nokia that the Contribution does not
  140. * infringe the patent or other intellectual property rights of any third
  141. * party or that the license provides you with all the necessary rights
  142. * to make use of the Contribution.
  143. *
  144. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  145. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  146. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  147. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  148. * OTHERWISE.
  149. */
  150. #include <stdio.h>
  151. #include "ssl_locl.h"
  152. #include "kssl_lcl.h"
  153. #include <openssl/buffer.h>
  154. #include <openssl/rand.h>
  155. #include <openssl/objects.h>
  156. #include <openssl/evp.h>
  157. #include <openssl/md5.h>
  158. #ifdef OPENSSL_FIPS
  159. #include <openssl/fips.h>
  160. #endif
  161. #ifndef OPENSSL_NO_DH
  162. #include <openssl/dh.h>
  163. #endif
  164. #include <openssl/bn.h>
  165. #ifndef OPENSSL_NO_ENGINE
  166. #include <openssl/engine.h>
  167. #endif
  168. static const SSL_METHOD *ssl3_get_client_method(int ver);
  169. static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b);
  170. static const SSL_METHOD *ssl3_get_client_method(int ver)
  171. {
  172. if (ver == SSL3_VERSION)
  173. return(SSLv3_client_method());
  174. else
  175. return(NULL);
  176. }
  177. IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
  178. ssl_undefined_function,
  179. ssl3_connect,
  180. ssl3_get_client_method)
  181. int ssl3_connect(SSL *s)
  182. {
  183. BUF_MEM *buf=NULL;
  184. unsigned long Time=(unsigned long)time(NULL);
  185. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  186. int ret= -1;
  187. int new_state,state,skip=0;
  188. RAND_add(&Time,sizeof(Time),0);
  189. ERR_clear_error();
  190. clear_sys_error();
  191. if (s->info_callback != NULL)
  192. cb=s->info_callback;
  193. else if (s->ctx->info_callback != NULL)
  194. cb=s->ctx->info_callback;
  195. s->in_handshake++;
  196. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  197. #ifndef OPENSSL_NO_HEARTBEATS
  198. /* If we're awaiting a HeartbeatResponse, pretend we
  199. * already got and don't await it anymore, because
  200. * Heartbeats don't make sense during handshakes anyway.
  201. */
  202. if (s->tlsext_hb_pending)
  203. {
  204. s->tlsext_hb_pending = 0;
  205. s->tlsext_hb_seq++;
  206. }
  207. #endif
  208. for (;;)
  209. {
  210. state=s->state;
  211. switch(s->state)
  212. {
  213. case SSL_ST_RENEGOTIATE:
  214. s->renegotiate=1;
  215. s->state=SSL_ST_CONNECT;
  216. s->ctx->stats.sess_connect_renegotiate++;
  217. /* break */
  218. case SSL_ST_BEFORE:
  219. case SSL_ST_CONNECT:
  220. case SSL_ST_BEFORE|SSL_ST_CONNECT:
  221. case SSL_ST_OK|SSL_ST_CONNECT:
  222. s->server=0;
  223. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  224. if ((s->version & 0xff00 ) != 0x0300)
  225. {
  226. SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
  227. ret = -1;
  228. goto end;
  229. }
  230. /* s->version=SSL3_VERSION; */
  231. s->type=SSL_ST_CONNECT;
  232. if (s->init_buf == NULL)
  233. {
  234. if ((buf=BUF_MEM_new()) == NULL)
  235. {
  236. ret= -1;
  237. goto end;
  238. }
  239. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  240. {
  241. ret= -1;
  242. goto end;
  243. }
  244. s->init_buf=buf;
  245. buf=NULL;
  246. }
  247. if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
  248. /* setup buffing BIO */
  249. if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
  250. /* don't push the buffering BIO quite yet */
  251. ssl3_init_finished_mac(s);
  252. s->state=SSL3_ST_CW_CLNT_HELLO_A;
  253. s->ctx->stats.sess_connect++;
  254. s->init_num=0;
  255. break;
  256. case SSL3_ST_CW_CLNT_HELLO_A:
  257. case SSL3_ST_CW_CLNT_HELLO_B:
  258. s->shutdown=0;
  259. ret=ssl3_client_hello(s);
  260. if (ret <= 0) goto end;
  261. s->state=SSL3_ST_CR_SRVR_HELLO_A;
  262. s->init_num=0;
  263. /* turn on buffering for the next lot of output */
  264. if (s->bbio != s->wbio)
  265. s->wbio=BIO_push(s->bbio,s->wbio);
  266. break;
  267. case SSL3_ST_CR_SRVR_HELLO_A:
  268. case SSL3_ST_CR_SRVR_HELLO_B:
  269. ret=ssl3_get_server_hello(s);
  270. if (ret <= 0) goto end;
  271. if (s->hit)
  272. {
  273. s->state=SSL3_ST_CR_FINISHED_A;
  274. #ifndef OPENSSL_NO_TLSEXT
  275. if (s->tlsext_ticket_expected)
  276. {
  277. /* receive renewed session ticket */
  278. s->state=SSL3_ST_CR_SESSION_TICKET_A;
  279. }
  280. #endif
  281. }
  282. else
  283. s->state=SSL3_ST_CR_CERT_A;
  284. s->init_num=0;
  285. break;
  286. case SSL3_ST_CR_CERT_A:
  287. case SSL3_ST_CR_CERT_B:
  288. #ifndef OPENSSL_NO_TLSEXT
  289. ret=ssl3_check_finished(s);
  290. if (ret <= 0) goto end;
  291. if (ret == 2)
  292. {
  293. s->hit = 1;
  294. if (s->tlsext_ticket_expected)
  295. s->state=SSL3_ST_CR_SESSION_TICKET_A;
  296. else
  297. s->state=SSL3_ST_CR_FINISHED_A;
  298. s->init_num=0;
  299. break;
  300. }
  301. #endif
  302. /* Check if it is anon DH/ECDH */
  303. /* or PSK */
  304. if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
  305. !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
  306. {
  307. ret=ssl3_get_server_certificate(s);
  308. if (ret <= 0) goto end;
  309. #ifndef OPENSSL_NO_TLSEXT
  310. if (s->tlsext_status_expected)
  311. s->state=SSL3_ST_CR_CERT_STATUS_A;
  312. else
  313. s->state=SSL3_ST_CR_KEY_EXCH_A;
  314. }
  315. else
  316. {
  317. skip = 1;
  318. s->state=SSL3_ST_CR_KEY_EXCH_A;
  319. }
  320. #else
  321. }
  322. else
  323. skip=1;
  324. s->state=SSL3_ST_CR_KEY_EXCH_A;
  325. #endif
  326. s->init_num=0;
  327. break;
  328. case SSL3_ST_CR_KEY_EXCH_A:
  329. case SSL3_ST_CR_KEY_EXCH_B:
  330. ret=ssl3_get_key_exchange(s);
  331. if (ret <= 0) goto end;
  332. s->state=SSL3_ST_CR_CERT_REQ_A;
  333. s->init_num=0;
  334. /* at this point we check that we have the
  335. * required stuff from the server */
  336. if (!ssl3_check_cert_and_algorithm(s))
  337. {
  338. ret= -1;
  339. goto end;
  340. }
  341. break;
  342. case SSL3_ST_CR_CERT_REQ_A:
  343. case SSL3_ST_CR_CERT_REQ_B:
  344. ret=ssl3_get_certificate_request(s);
  345. if (ret <= 0) goto end;
  346. s->state=SSL3_ST_CR_SRVR_DONE_A;
  347. s->init_num=0;
  348. break;
  349. case SSL3_ST_CR_SRVR_DONE_A:
  350. case SSL3_ST_CR_SRVR_DONE_B:
  351. ret=ssl3_get_server_done(s);
  352. if (ret <= 0) goto end;
  353. #ifndef OPENSSL_NO_SRP
  354. if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP)
  355. {
  356. if ((ret = SRP_Calc_A_param(s))<=0)
  357. {
  358. SSLerr(SSL_F_SSL3_CONNECT,SSL_R_SRP_A_CALC);
  359. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INTERNAL_ERROR);
  360. goto end;
  361. }
  362. }
  363. #endif
  364. if (s->s3->tmp.cert_req)
  365. s->state=SSL3_ST_CW_CERT_A;
  366. else
  367. s->state=SSL3_ST_CW_KEY_EXCH_A;
  368. s->init_num=0;
  369. break;
  370. case SSL3_ST_CW_CERT_A:
  371. case SSL3_ST_CW_CERT_B:
  372. case SSL3_ST_CW_CERT_C:
  373. case SSL3_ST_CW_CERT_D:
  374. ret=ssl3_send_client_certificate(s);
  375. if (ret <= 0) goto end;
  376. s->state=SSL3_ST_CW_KEY_EXCH_A;
  377. s->init_num=0;
  378. break;
  379. case SSL3_ST_CW_KEY_EXCH_A:
  380. case SSL3_ST_CW_KEY_EXCH_B:
  381. ret=ssl3_send_client_key_exchange(s);
  382. if (ret <= 0) goto end;
  383. /* EAY EAY EAY need to check for DH fix cert
  384. * sent back */
  385. /* For TLS, cert_req is set to 2, so a cert chain
  386. * of nothing is sent, but no verify packet is sent */
  387. /* XXX: For now, we do not support client
  388. * authentication in ECDH cipher suites with
  389. * ECDH (rather than ECDSA) certificates.
  390. * We need to skip the certificate verify
  391. * message when client's ECDH public key is sent
  392. * inside the client certificate.
  393. */
  394. if (s->s3->tmp.cert_req == 1)
  395. {
  396. s->state=SSL3_ST_CW_CERT_VRFY_A;
  397. }
  398. else
  399. {
  400. s->state=SSL3_ST_CW_CHANGE_A;
  401. s->s3->change_cipher_spec=0;
  402. }
  403. if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
  404. {
  405. s->state=SSL3_ST_CW_CHANGE_A;
  406. s->s3->change_cipher_spec=0;
  407. }
  408. s->init_num=0;
  409. break;
  410. case SSL3_ST_CW_CERT_VRFY_A:
  411. case SSL3_ST_CW_CERT_VRFY_B:
  412. ret=ssl3_send_client_verify(s);
  413. if (ret <= 0) goto end;
  414. s->state=SSL3_ST_CW_CHANGE_A;
  415. s->init_num=0;
  416. s->s3->change_cipher_spec=0;
  417. break;
  418. case SSL3_ST_CW_CHANGE_A:
  419. case SSL3_ST_CW_CHANGE_B:
  420. ret=ssl3_send_change_cipher_spec(s,
  421. SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
  422. if (ret <= 0) goto end;
  423. #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
  424. s->state=SSL3_ST_CW_FINISHED_A;
  425. #else
  426. if (s->s3->next_proto_neg_seen)
  427. s->state=SSL3_ST_CW_NEXT_PROTO_A;
  428. else
  429. s->state=SSL3_ST_CW_FINISHED_A;
  430. #endif
  431. s->init_num=0;
  432. s->session->cipher=s->s3->tmp.new_cipher;
  433. #ifdef OPENSSL_NO_COMP
  434. s->session->compress_meth=0;
  435. #else
  436. if (s->s3->tmp.new_compression == NULL)
  437. s->session->compress_meth=0;
  438. else
  439. s->session->compress_meth=
  440. s->s3->tmp.new_compression->id;
  441. #endif
  442. if (!s->method->ssl3_enc->setup_key_block(s))
  443. {
  444. ret= -1;
  445. goto end;
  446. }
  447. if (!s->method->ssl3_enc->change_cipher_state(s,
  448. SSL3_CHANGE_CIPHER_CLIENT_WRITE))
  449. {
  450. ret= -1;
  451. goto end;
  452. }
  453. break;
  454. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  455. case SSL3_ST_CW_NEXT_PROTO_A:
  456. case SSL3_ST_CW_NEXT_PROTO_B:
  457. ret=ssl3_send_next_proto(s);
  458. if (ret <= 0) goto end;
  459. s->state=SSL3_ST_CW_FINISHED_A;
  460. break;
  461. #endif
  462. case SSL3_ST_CW_FINISHED_A:
  463. case SSL3_ST_CW_FINISHED_B:
  464. ret=ssl3_send_finished(s,
  465. SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
  466. s->method->ssl3_enc->client_finished_label,
  467. s->method->ssl3_enc->client_finished_label_len);
  468. if (ret <= 0) goto end;
  469. s->state=SSL3_ST_CW_FLUSH;
  470. /* clear flags */
  471. s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
  472. if (s->hit)
  473. {
  474. s->s3->tmp.next_state=SSL_ST_OK;
  475. if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
  476. {
  477. s->state=SSL_ST_OK;
  478. s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
  479. s->s3->delay_buf_pop_ret=0;
  480. }
  481. }
  482. else
  483. {
  484. #ifndef OPENSSL_NO_TLSEXT
  485. /* Allow NewSessionTicket if ticket expected */
  486. if (s->tlsext_ticket_expected)
  487. s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
  488. else
  489. #endif
  490. s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
  491. }
  492. s->init_num=0;
  493. break;
  494. #ifndef OPENSSL_NO_TLSEXT
  495. case SSL3_ST_CR_SESSION_TICKET_A:
  496. case SSL3_ST_CR_SESSION_TICKET_B:
  497. ret=ssl3_get_new_session_ticket(s);
  498. if (ret <= 0) goto end;
  499. s->state=SSL3_ST_CR_FINISHED_A;
  500. s->init_num=0;
  501. break;
  502. case SSL3_ST_CR_CERT_STATUS_A:
  503. case SSL3_ST_CR_CERT_STATUS_B:
  504. ret=ssl3_get_cert_status(s);
  505. if (ret <= 0) goto end;
  506. s->state=SSL3_ST_CR_KEY_EXCH_A;
  507. s->init_num=0;
  508. break;
  509. #endif
  510. case SSL3_ST_CR_FINISHED_A:
  511. case SSL3_ST_CR_FINISHED_B:
  512. ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
  513. SSL3_ST_CR_FINISHED_B);
  514. if (ret <= 0) goto end;
  515. if (s->hit)
  516. s->state=SSL3_ST_CW_CHANGE_A;
  517. else
  518. s->state=SSL_ST_OK;
  519. s->init_num=0;
  520. break;
  521. case SSL3_ST_CW_FLUSH:
  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 SSL_ST_OK:
  532. /* clean a few things up */
  533. ssl3_cleanup_key_block(s);
  534. if (s->init_buf != NULL)
  535. {
  536. BUF_MEM_free(s->init_buf);
  537. s->init_buf=NULL;
  538. }
  539. /* If we are not 'joining' the last two packets,
  540. * remove the buffering now */
  541. if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
  542. ssl_free_wbio_buffer(s);
  543. /* else do it later in ssl3_write */
  544. s->init_num=0;
  545. s->renegotiate=0;
  546. s->new_session=0;
  547. ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
  548. if (s->hit) s->ctx->stats.sess_hit++;
  549. ret=1;
  550. /* s->server=0; */
  551. s->handshake_func=ssl3_connect;
  552. s->ctx->stats.sess_connect_good++;
  553. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  554. goto end;
  555. /* break; */
  556. default:
  557. SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
  558. ret= -1;
  559. goto end;
  560. /* break; */
  561. }
  562. /* did we do anything */
  563. if (!s->s3->tmp.reuse_message && !skip)
  564. {
  565. if (s->debug)
  566. {
  567. if ((ret=BIO_flush(s->wbio)) <= 0)
  568. goto end;
  569. }
  570. if ((cb != NULL) && (s->state != state))
  571. {
  572. new_state=s->state;
  573. s->state=state;
  574. cb(s,SSL_CB_CONNECT_LOOP,1);
  575. s->state=new_state;
  576. }
  577. }
  578. skip=0;
  579. }
  580. end:
  581. s->in_handshake--;
  582. if (buf != NULL)
  583. BUF_MEM_free(buf);
  584. if (cb != NULL)
  585. cb(s,SSL_CB_CONNECT_EXIT,ret);
  586. return(ret);
  587. }
  588. int ssl3_client_hello(SSL *s)
  589. {
  590. unsigned char *buf;
  591. unsigned char *p,*d;
  592. int i;
  593. unsigned long Time,l;
  594. #ifndef OPENSSL_NO_COMP
  595. int j;
  596. SSL_COMP *comp;
  597. #endif
  598. buf=(unsigned char *)s->init_buf->data;
  599. if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
  600. {
  601. SSL_SESSION *sess = s->session;
  602. if ((sess == NULL) ||
  603. (sess->ssl_version != s->version) ||
  604. #ifdef OPENSSL_NO_TLSEXT
  605. !sess->session_id_length ||
  606. #else
  607. (!sess->session_id_length && !sess->tlsext_tick) ||
  608. #endif
  609. (sess->not_resumable))
  610. {
  611. if (!ssl_get_new_session(s,0))
  612. goto err;
  613. }
  614. /* else use the pre-loaded session */
  615. p=s->s3->client_random;
  616. Time=(unsigned long)time(NULL); /* Time */
  617. l2n(Time,p);
  618. if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
  619. goto err;
  620. /* Do the message type and length last */
  621. d=p= &(buf[4]);
  622. /* version indicates the negotiated version: for example from
  623. * an SSLv2/v3 compatible client hello). The client_version
  624. * field is the maximum version we permit and it is also
  625. * used in RSA encrypted premaster secrets. Some servers can
  626. * choke if we initially report a higher version then
  627. * renegotiate to a lower one in the premaster secret. This
  628. * didn't happen with TLS 1.0 as most servers supported it
  629. * but it can with TLS 1.1 or later if the server only supports
  630. * 1.0.
  631. *
  632. * Possible scenario with previous logic:
  633. * 1. Client hello indicates TLS 1.2
  634. * 2. Server hello says TLS 1.0
  635. * 3. RSA encrypted premaster secret uses 1.2.
  636. * 4. Handhaked proceeds using TLS 1.0.
  637. * 5. Server sends hello request to renegotiate.
  638. * 6. Client hello indicates TLS v1.0 as we now
  639. * know that is maximum server supports.
  640. * 7. Server chokes on RSA encrypted premaster secret
  641. * containing version 1.0.
  642. *
  643. * For interoperability it should be OK to always use the
  644. * maximum version we support in client hello and then rely
  645. * on the checking of version to ensure the servers isn't
  646. * being inconsistent: for example initially negotiating with
  647. * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
  648. * client_version in client hello and not resetting it to
  649. * the negotiated version.
  650. */
  651. #if 0
  652. *(p++)=s->version>>8;
  653. *(p++)=s->version&0xff;
  654. s->client_version=s->version;
  655. #else
  656. *(p++)=s->client_version>>8;
  657. *(p++)=s->client_version&0xff;
  658. #endif
  659. /* Random stuff */
  660. memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
  661. p+=SSL3_RANDOM_SIZE;
  662. /* Session ID */
  663. if (s->new_session)
  664. i=0;
  665. else
  666. i=s->session->session_id_length;
  667. *(p++)=i;
  668. if (i != 0)
  669. {
  670. if (i > (int)sizeof(s->session->session_id))
  671. {
  672. SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  673. goto err;
  674. }
  675. memcpy(p,s->session->session_id,i);
  676. p+=i;
  677. }
  678. /* Ciphers supported */
  679. i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0);
  680. if (i == 0)
  681. {
  682. SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
  683. goto err;
  684. }
  685. #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
  686. /* Some servers hang if client hello > 256 bytes
  687. * as hack workaround chop number of supported ciphers
  688. * to keep it well below this if we use TLS v1.2
  689. */
  690. if (TLS1_get_version(s) >= TLS1_2_VERSION
  691. && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
  692. i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
  693. #endif
  694. s2n(i,p);
  695. p+=i;
  696. /* COMPRESSION */
  697. #ifdef OPENSSL_NO_COMP
  698. *(p++)=1;
  699. #else
  700. if ((s->options & SSL_OP_NO_COMPRESSION)
  701. || !s->ctx->comp_methods)
  702. j=0;
  703. else
  704. j=sk_SSL_COMP_num(s->ctx->comp_methods);
  705. *(p++)=1+j;
  706. for (i=0; i<j; i++)
  707. {
  708. comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
  709. *(p++)=comp->id;
  710. }
  711. #endif
  712. *(p++)=0; /* Add the NULL method */
  713. #ifndef OPENSSL_NO_TLSEXT
  714. /* TLS extensions*/
  715. if (ssl_prepare_clienthello_tlsext(s) <= 0)
  716. {
  717. SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
  718. goto err;
  719. }
  720. if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
  721. {
  722. SSLerr(SSL_F_SSL3_CLIENT_HELLO,ERR_R_INTERNAL_ERROR);
  723. goto err;
  724. }
  725. #endif
  726. l=(p-d);
  727. d=buf;
  728. *(d++)=SSL3_MT_CLIENT_HELLO;
  729. l2n3(l,d);
  730. s->state=SSL3_ST_CW_CLNT_HELLO_B;
  731. /* number of bytes to write */
  732. s->init_num=p-buf;
  733. s->init_off=0;
  734. }
  735. /* SSL3_ST_CW_CLNT_HELLO_B */
  736. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  737. err:
  738. return(-1);
  739. }
  740. int ssl3_get_server_hello(SSL *s)
  741. {
  742. STACK_OF(SSL_CIPHER) *sk;
  743. const SSL_CIPHER *c;
  744. unsigned char *p,*d;
  745. int i,al,ok;
  746. unsigned int j;
  747. long n;
  748. #ifndef OPENSSL_NO_COMP
  749. SSL_COMP *comp;
  750. #endif
  751. n=s->method->ssl_get_message(s,
  752. SSL3_ST_CR_SRVR_HELLO_A,
  753. SSL3_ST_CR_SRVR_HELLO_B,
  754. -1,
  755. 20000, /* ?? */
  756. &ok);
  757. if (!ok) return((int)n);
  758. if ( SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER)
  759. {
  760. if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST)
  761. {
  762. if ( s->d1->send_cookie == 0)
  763. {
  764. s->s3->tmp.reuse_message = 1;
  765. return 1;
  766. }
  767. else /* already sent a cookie */
  768. {
  769. al=SSL_AD_UNEXPECTED_MESSAGE;
  770. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
  771. goto f_err;
  772. }
  773. }
  774. }
  775. if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO)
  776. {
  777. al=SSL_AD_UNEXPECTED_MESSAGE;
  778. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
  779. goto f_err;
  780. }
  781. d=p=(unsigned char *)s->init_msg;
  782. if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
  783. {
  784. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
  785. s->version=(s->version&0xff00)|p[1];
  786. al=SSL_AD_PROTOCOL_VERSION;
  787. goto f_err;
  788. }
  789. p+=2;
  790. /* load the server hello data */
  791. /* load the server random */
  792. memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
  793. p+=SSL3_RANDOM_SIZE;
  794. /* get the session-id */
  795. j= *(p++);
  796. if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE))
  797. {
  798. al=SSL_AD_ILLEGAL_PARAMETER;
  799. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_LONG);
  800. goto f_err;
  801. }
  802. #ifndef OPENSSL_NO_TLSEXT
  803. /* check if we want to resume the session based on external pre-shared secret */
  804. if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
  805. {
  806. SSL_CIPHER *pref_cipher=NULL;
  807. s->session->master_key_length=sizeof(s->session->master_key);
  808. if (s->tls_session_secret_cb(s, s->session->master_key,
  809. &s->session->master_key_length,
  810. NULL, &pref_cipher,
  811. s->tls_session_secret_cb_arg))
  812. {
  813. s->session->cipher = pref_cipher ?
  814. pref_cipher : ssl_get_cipher_by_char(s, p+j);
  815. }
  816. }
  817. #endif /* OPENSSL_NO_TLSEXT */
  818. if (j != 0 && j == s->session->session_id_length
  819. && memcmp(p,s->session->session_id,j) == 0)
  820. {
  821. if(s->sid_ctx_length != s->session->sid_ctx_length
  822. || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
  823. {
  824. /* actually a client application bug */
  825. al=SSL_AD_ILLEGAL_PARAMETER;
  826. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  827. goto f_err;
  828. }
  829. s->hit=1;
  830. }
  831. else /* a miss or crap from the other end */
  832. {
  833. /* If we were trying for session-id reuse, make a new
  834. * SSL_SESSION so we don't stuff up other people */
  835. s->hit=0;
  836. if (s->session->session_id_length > 0)
  837. {
  838. if (!ssl_get_new_session(s,0))
  839. {
  840. al=SSL_AD_INTERNAL_ERROR;
  841. goto f_err;
  842. }
  843. }
  844. s->session->session_id_length=j;
  845. memcpy(s->session->session_id,p,j); /* j could be 0 */
  846. }
  847. p+=j;
  848. c=ssl_get_cipher_by_char(s,p);
  849. if (c == NULL)
  850. {
  851. /* unknown cipher */
  852. al=SSL_AD_ILLEGAL_PARAMETER;
  853. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
  854. goto f_err;
  855. }
  856. /* TLS v1.2 only ciphersuites require v1.2 or later */
  857. if ((c->algorithm_ssl & SSL_TLSV1_2) &&
  858. (TLS1_get_version(s) < TLS1_2_VERSION))
  859. {
  860. al=SSL_AD_ILLEGAL_PARAMETER;
  861. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
  862. goto f_err;
  863. }
  864. p+=ssl_put_cipher_by_char(s,NULL,NULL);
  865. sk=ssl_get_ciphers_by_id(s);
  866. i=sk_SSL_CIPHER_find(sk,c);
  867. if (i < 0)
  868. {
  869. /* we did not say we would use this cipher */
  870. al=SSL_AD_ILLEGAL_PARAMETER;
  871. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
  872. goto f_err;
  873. }
  874. /* Depending on the session caching (internal/external), the cipher
  875. and/or cipher_id values may not be set. Make sure that
  876. cipher_id is set and use it for comparison. */
  877. if (s->session->cipher)
  878. s->session->cipher_id = s->session->cipher->id;
  879. if (s->hit && (s->session->cipher_id != c->id))
  880. {
  881. /* Workaround is now obsolete */
  882. #if 0
  883. if (!(s->options &
  884. SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
  885. #endif
  886. {
  887. al=SSL_AD_ILLEGAL_PARAMETER;
  888. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  889. goto f_err;
  890. }
  891. }
  892. s->s3->tmp.new_cipher=c;
  893. /* Don't digest cached records if TLS v1.2: we may need them for
  894. * client authentication.
  895. */
  896. if (TLS1_get_version(s) < TLS1_2_VERSION && !ssl3_digest_cached_records(s))
  897. {
  898. al = SSL_AD_INTERNAL_ERROR;
  899. goto f_err;
  900. }
  901. /* lets get the compression algorithm */
  902. /* COMPRESSION */
  903. #ifdef OPENSSL_NO_COMP
  904. if (*(p++) != 0)
  905. {
  906. al=SSL_AD_ILLEGAL_PARAMETER;
  907. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  908. goto f_err;
  909. }
  910. /* If compression is disabled we'd better not try to resume a session
  911. * using compression.
  912. */
  913. if (s->session->compress_meth != 0)
  914. {
  915. al=SSL_AD_INTERNAL_ERROR;
  916. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_INCONSISTENT_COMPRESSION);
  917. goto f_err;
  918. }
  919. #else
  920. j= *(p++);
  921. if (s->hit && j != s->session->compress_meth)
  922. {
  923. al=SSL_AD_ILLEGAL_PARAMETER;
  924. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
  925. goto f_err;
  926. }
  927. if (j == 0)
  928. comp=NULL;
  929. else if (s->options & SSL_OP_NO_COMPRESSION)
  930. {
  931. al=SSL_AD_ILLEGAL_PARAMETER;
  932. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_COMPRESSION_DISABLED);
  933. goto f_err;
  934. }
  935. else
  936. comp=ssl3_comp_find(s->ctx->comp_methods,j);
  937. if ((j != 0) && (comp == NULL))
  938. {
  939. al=SSL_AD_ILLEGAL_PARAMETER;
  940. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  941. goto f_err;
  942. }
  943. else
  944. {
  945. s->s3->tmp.new_compression=comp;
  946. }
  947. #endif
  948. #ifndef OPENSSL_NO_TLSEXT
  949. /* TLS extensions*/
  950. if (s->version >= SSL3_VERSION)
  951. {
  952. if (!ssl_parse_serverhello_tlsext(s,&p,d,n, &al))
  953. {
  954. /* 'al' set by ssl_parse_serverhello_tlsext */
  955. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_PARSE_TLSEXT);
  956. goto f_err;
  957. }
  958. if (ssl_check_serverhello_tlsext(s) <= 0)
  959. {
  960. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SERVERHELLO_TLSEXT);
  961. goto err;
  962. }
  963. }
  964. #endif
  965. if (p != (d+n))
  966. {
  967. /* wrong packet length */
  968. al=SSL_AD_DECODE_ERROR;
  969. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
  970. goto f_err;
  971. }
  972. return(1);
  973. f_err:
  974. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  975. err:
  976. return(-1);
  977. }
  978. int ssl3_get_server_certificate(SSL *s)
  979. {
  980. int al,i,ok,ret= -1;
  981. unsigned long n,nc,llen,l;
  982. X509 *x=NULL;
  983. const unsigned char *q,*p;
  984. unsigned char *d;
  985. STACK_OF(X509) *sk=NULL;
  986. SESS_CERT *sc;
  987. EVP_PKEY *pkey=NULL;
  988. int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
  989. n=s->method->ssl_get_message(s,
  990. SSL3_ST_CR_CERT_A,
  991. SSL3_ST_CR_CERT_B,
  992. -1,
  993. s->max_cert_list,
  994. &ok);
  995. if (!ok) return((int)n);
  996. if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
  997. ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) &&
  998. (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)))
  999. {
  1000. s->s3->tmp.reuse_message=1;
  1001. return(1);
  1002. }
  1003. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
  1004. {
  1005. al=SSL_AD_UNEXPECTED_MESSAGE;
  1006. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
  1007. goto f_err;
  1008. }
  1009. p=d=(unsigned char *)s->init_msg;
  1010. if ((sk=sk_X509_new_null()) == NULL)
  1011. {
  1012. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  1013. goto err;
  1014. }
  1015. n2l3(p,llen);
  1016. if (llen+3 != n)
  1017. {
  1018. al=SSL_AD_DECODE_ERROR;
  1019. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
  1020. goto f_err;
  1021. }
  1022. for (nc=0; nc<llen; )
  1023. {
  1024. n2l3(p,l);
  1025. if ((l+nc+3) > llen)
  1026. {
  1027. al=SSL_AD_DECODE_ERROR;
  1028. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  1029. goto f_err;
  1030. }
  1031. q=p;
  1032. x=d2i_X509(NULL,&q,l);
  1033. if (x == NULL)
  1034. {
  1035. al=SSL_AD_BAD_CERTIFICATE;
  1036. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
  1037. goto f_err;
  1038. }
  1039. if (q != (p+l))
  1040. {
  1041. al=SSL_AD_DECODE_ERROR;
  1042. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  1043. goto f_err;
  1044. }
  1045. if (!sk_X509_push(sk,x))
  1046. {
  1047. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  1048. goto err;
  1049. }
  1050. x=NULL;
  1051. nc+=l+3;
  1052. p=q;
  1053. }
  1054. i=ssl_verify_cert_chain(s,sk);
  1055. if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
  1056. #ifndef OPENSSL_NO_KRB5
  1057. && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
  1058. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
  1059. #endif /* OPENSSL_NO_KRB5 */
  1060. )
  1061. {
  1062. al=ssl_verify_alarm_type(s->verify_result);
  1063. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
  1064. goto f_err;
  1065. }
  1066. ERR_clear_error(); /* but we keep s->verify_result */
  1067. sc=ssl_sess_cert_new();
  1068. if (sc == NULL) goto err;
  1069. if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
  1070. s->session->sess_cert=sc;
  1071. sc->cert_chain=sk;
  1072. /* Inconsistency alert: cert_chain does include the peer's
  1073. * certificate, which we don't include in s3_srvr.c */
  1074. x=sk_X509_value(sk,0);
  1075. sk=NULL;
  1076. /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
  1077. pkey=X509_get_pubkey(x);
  1078. /* VRS: allow null cert if auth == KRB5 */
  1079. need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
  1080. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
  1081. ? 0 : 1;
  1082. #ifdef KSSL_DEBUG
  1083. printf("pkey,x = %p, %p\n", pkey,x);
  1084. printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
  1085. printf("cipher, alg, nc = %s, %lx, %lx, %d\n", s->s3->tmp.new_cipher->name,
  1086. s->s3->tmp.new_cipher->algorithm_mkey, s->s3->tmp.new_cipher->algorithm_auth, need_cert);
  1087. #endif /* KSSL_DEBUG */
  1088. if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
  1089. {
  1090. x=NULL;
  1091. al=SSL3_AL_FATAL;
  1092. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  1093. SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
  1094. goto f_err;
  1095. }
  1096. i=ssl_cert_type(x,pkey);
  1097. if (need_cert && i < 0)
  1098. {
  1099. x=NULL;
  1100. al=SSL3_AL_FATAL;
  1101. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  1102. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  1103. goto f_err;
  1104. }
  1105. if (need_cert)
  1106. {
  1107. sc->peer_cert_type=i;
  1108. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  1109. /* Why would the following ever happen?
  1110. * We just created sc a couple of lines ago. */
  1111. if (sc->peer_pkeys[i].x509 != NULL)
  1112. X509_free(sc->peer_pkeys[i].x509);
  1113. sc->peer_pkeys[i].x509=x;
  1114. sc->peer_key= &(sc->peer_pkeys[i]);
  1115. if (s->session->peer != NULL)
  1116. X509_free(s->session->peer);
  1117. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  1118. s->session->peer=x;
  1119. }
  1120. else
  1121. {
  1122. sc->peer_cert_type=i;
  1123. sc->peer_key= NULL;
  1124. if (s->session->peer != NULL)
  1125. X509_free(s->session->peer);
  1126. s->session->peer=NULL;
  1127. }
  1128. s->session->verify_result = s->verify_result;
  1129. x=NULL;
  1130. ret=1;
  1131. if (0)
  1132. {
  1133. f_err:
  1134. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1135. }
  1136. err:
  1137. EVP_PKEY_free(pkey);
  1138. X509_free(x);
  1139. sk_X509_pop_free(sk,X509_free);
  1140. return(ret);
  1141. }
  1142. int ssl3_get_key_exchange(SSL *s)
  1143. {
  1144. #ifndef OPENSSL_NO_RSA
  1145. unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
  1146. #endif
  1147. EVP_MD_CTX md_ctx;
  1148. unsigned char *param,*p;
  1149. int al,i,j,param_len,ok;
  1150. long n,alg_k,alg_a;
  1151. EVP_PKEY *pkey=NULL;
  1152. const EVP_MD *md = NULL;
  1153. #ifndef OPENSSL_NO_RSA
  1154. RSA *rsa=NULL;
  1155. #endif
  1156. #ifndef OPENSSL_NO_DH
  1157. DH *dh=NULL;
  1158. #endif
  1159. #ifndef OPENSSL_NO_ECDH
  1160. EC_KEY *ecdh = NULL;
  1161. BN_CTX *bn_ctx = NULL;
  1162. EC_POINT *srvr_ecpoint = NULL;
  1163. int curve_nid = 0;
  1164. int encoded_pt_len = 0;
  1165. #endif
  1166. /* use same message size as in ssl3_get_certificate_request()
  1167. * as ServerKeyExchange message may be skipped */
  1168. n=s->method->ssl_get_message(s,
  1169. SSL3_ST_CR_KEY_EXCH_A,
  1170. SSL3_ST_CR_KEY_EXCH_B,
  1171. -1,
  1172. s->max_cert_list,
  1173. &ok);
  1174. if (!ok) return((int)n);
  1175. if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
  1176. {
  1177. #ifndef OPENSSL_NO_PSK
  1178. /* In plain PSK ciphersuite, ServerKeyExchange can be
  1179. omitted if no identity hint is sent. Set
  1180. session->sess_cert anyway to avoid problems
  1181. later.*/
  1182. if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)
  1183. {
  1184. s->session->sess_cert=ssl_sess_cert_new();
  1185. if (s->ctx->psk_identity_hint)
  1186. OPENSSL_free(s->ctx->psk_identity_hint);
  1187. s->ctx->psk_identity_hint = NULL;
  1188. }
  1189. #endif
  1190. s->s3->tmp.reuse_message=1;
  1191. return(1);
  1192. }
  1193. param=p=(unsigned char *)s->init_msg;
  1194. if (s->session->sess_cert != NULL)
  1195. {
  1196. #ifndef OPENSSL_NO_RSA
  1197. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  1198. {
  1199. RSA_free(s->session->sess_cert->peer_rsa_tmp);
  1200. s->session->sess_cert->peer_rsa_tmp=NULL;
  1201. }
  1202. #endif
  1203. #ifndef OPENSSL_NO_DH
  1204. if (s->session->sess_cert->peer_dh_tmp)
  1205. {
  1206. DH_free(s->session->sess_cert->peer_dh_tmp);
  1207. s->session->sess_cert->peer_dh_tmp=NULL;
  1208. }
  1209. #endif
  1210. #ifndef OPENSSL_NO_ECDH
  1211. if (s->session->sess_cert->peer_ecdh_tmp)
  1212. {
  1213. EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
  1214. s->session->sess_cert->peer_ecdh_tmp=NULL;
  1215. }
  1216. #endif
  1217. }
  1218. else
  1219. {
  1220. s->session->sess_cert=ssl_sess_cert_new();
  1221. }
  1222. param_len=0;
  1223. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  1224. alg_a=s->s3->tmp.new_cipher->algorithm_auth;
  1225. EVP_MD_CTX_init(&md_ctx);
  1226. #ifndef OPENSSL_NO_PSK
  1227. if (alg_k & SSL_kPSK)
  1228. {
  1229. char tmp_id_hint[PSK_MAX_IDENTITY_LEN+1];
  1230. al=SSL_AD_HANDSHAKE_FAILURE;
  1231. n2s(p,i);
  1232. param_len=i+2;
  1233. /* Store PSK identity hint for later use, hint is used
  1234. * in ssl3_send_client_key_exchange. Assume that the
  1235. * maximum length of a PSK identity hint can be as
  1236. * long as the maximum length of a PSK identity. */
  1237. if (i > PSK_MAX_IDENTITY_LEN)
  1238. {
  1239. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1240. SSL_R_DATA_LENGTH_TOO_LONG);
  1241. goto f_err;
  1242. }
  1243. if (param_len > n)
  1244. {
  1245. al=SSL_AD_DECODE_ERROR;
  1246. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1247. SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
  1248. goto f_err;
  1249. }
  1250. /* If received PSK identity hint contains NULL
  1251. * characters, the hint is truncated from the first
  1252. * NULL. p may not be ending with NULL, so create a
  1253. * NULL-terminated string. */
  1254. memcpy(tmp_id_hint, p, i);
  1255. memset(tmp_id_hint+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
  1256. if (s->ctx->psk_identity_hint != NULL)
  1257. OPENSSL_free(s->ctx->psk_identity_hint);
  1258. s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
  1259. if (s->ctx->psk_identity_hint == NULL)
  1260. {
  1261. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1262. goto f_err;
  1263. }
  1264. p+=i;
  1265. n-=param_len;
  1266. }
  1267. else
  1268. #endif /* !OPENSSL_NO_PSK */
  1269. #ifndef OPENSSL_NO_SRP
  1270. if (alg_k & SSL_kSRP)
  1271. {
  1272. n2s(p,i);
  1273. param_len=i+2;
  1274. if (param_len > n)
  1275. {
  1276. al=SSL_AD_DECODE_ERROR;
  1277. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SRP_N_LENGTH);
  1278. goto f_err;
  1279. }
  1280. if (!(s->srp_ctx.N=BN_bin2bn(p,i,NULL)))
  1281. {
  1282. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1283. goto err;
  1284. }
  1285. p+=i;
  1286. n2s(p,i);
  1287. param_len+=i+2;
  1288. if (param_len > n)
  1289. {
  1290. al=SSL_AD_DECODE_ERROR;
  1291. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SRP_G_LENGTH);
  1292. goto f_err;
  1293. }
  1294. if (!(s->srp_ctx.g=BN_bin2bn(p,i,NULL)))
  1295. {
  1296. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1297. goto err;
  1298. }
  1299. p+=i;
  1300. i = (unsigned int)(p[0]);
  1301. p++;
  1302. param_len+=i+1;
  1303. if (param_len > n)
  1304. {
  1305. al=SSL_AD_DECODE_ERROR;
  1306. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SRP_S_LENGTH);
  1307. goto f_err;
  1308. }
  1309. if (!(s->srp_ctx.s=BN_bin2bn(p,i,NULL)))
  1310. {
  1311. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1312. goto err;
  1313. }
  1314. p+=i;
  1315. n2s(p,i);
  1316. param_len+=i+2;
  1317. if (param_len > n)
  1318. {
  1319. al=SSL_AD_DECODE_ERROR;
  1320. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SRP_B_LENGTH);
  1321. goto f_err;
  1322. }
  1323. if (!(s->srp_ctx.B=BN_bin2bn(p,i,NULL)))
  1324. {
  1325. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1326. goto err;
  1327. }
  1328. p+=i;
  1329. n-=param_len;
  1330. /* We must check if there is a certificate */
  1331. #ifndef OPENSSL_NO_RSA
  1332. if (alg_a & SSL_aRSA)
  1333. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1334. #else
  1335. if (0)
  1336. ;
  1337. #endif
  1338. #ifndef OPENSSL_NO_DSA
  1339. else if (alg_a & SSL_aDSS)
  1340. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
  1341. #endif
  1342. }
  1343. else
  1344. #endif /* !OPENSSL_NO_SRP */
  1345. #ifndef OPENSSL_NO_RSA
  1346. if (alg_k & SSL_kRSA)
  1347. {
  1348. if ((rsa=RSA_new()) == NULL)
  1349. {
  1350. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1351. goto err;
  1352. }
  1353. n2s(p,i);
  1354. param_len=i+2;
  1355. if (param_len > n)
  1356. {
  1357. al=SSL_AD_DECODE_ERROR;
  1358. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
  1359. goto f_err;
  1360. }
  1361. if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
  1362. {
  1363. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1364. goto err;
  1365. }
  1366. p+=i;
  1367. n2s(p,i);
  1368. param_len+=i+2;
  1369. if (param_len > n)
  1370. {
  1371. al=SSL_AD_DECODE_ERROR;
  1372. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
  1373. goto f_err;
  1374. }
  1375. if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
  1376. {
  1377. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1378. goto err;
  1379. }
  1380. p+=i;
  1381. n-=param_len;
  1382. /* this should be because we are using an export cipher */
  1383. if (alg_a & SSL_aRSA)
  1384. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1385. else
  1386. {
  1387. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1388. goto err;
  1389. }
  1390. s->session->sess_cert->peer_rsa_tmp=rsa;
  1391. rsa=NULL;
  1392. }
  1393. #else /* OPENSSL_NO_RSA */
  1394. if (0)
  1395. ;
  1396. #endif
  1397. #ifndef OPENSSL_NO_DH
  1398. else if (alg_k & SSL_kEDH)
  1399. {
  1400. if ((dh=DH_new()) == NULL)
  1401. {
  1402. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
  1403. goto err;
  1404. }
  1405. n2s(p,i);
  1406. param_len=i+2;
  1407. if (param_len > n)
  1408. {
  1409. al=SSL_AD_DECODE_ERROR;
  1410. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
  1411. goto f_err;
  1412. }
  1413. if (!(dh->p=BN_bin2bn(p,i,NULL)))
  1414. {
  1415. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1416. goto err;
  1417. }
  1418. p+=i;
  1419. n2s(p,i);
  1420. param_len+=i+2;
  1421. if (param_len > n)
  1422. {
  1423. al=SSL_AD_DECODE_ERROR;
  1424. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
  1425. goto f_err;
  1426. }
  1427. if (!(dh->g=BN_bin2bn(p,i,NULL)))
  1428. {
  1429. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1430. goto err;
  1431. }
  1432. p+=i;
  1433. n2s(p,i);
  1434. param_len+=i+2;
  1435. if (param_len > n)
  1436. {
  1437. al=SSL_AD_DECODE_ERROR;
  1438. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
  1439. goto f_err;
  1440. }
  1441. if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
  1442. {
  1443. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1444. goto err;
  1445. }
  1446. p+=i;
  1447. n-=param_len;
  1448. #ifndef OPENSSL_NO_RSA
  1449. if (alg_a & SSL_aRSA)
  1450. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1451. #else
  1452. if (0)
  1453. ;
  1454. #endif
  1455. #ifndef OPENSSL_NO_DSA
  1456. else if (alg_a & SSL_aDSS)
  1457. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
  1458. #endif
  1459. /* else anonymous DH, so no certificate or pkey. */
  1460. s->session->sess_cert->peer_dh_tmp=dh;
  1461. dh=NULL;
  1462. }
  1463. else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd))
  1464. {
  1465. al=SSL_AD_ILLEGAL_PARAMETER;
  1466. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
  1467. goto f_err;
  1468. }
  1469. #endif /* !OPENSSL_NO_DH */
  1470. #ifndef OPENSSL_NO_ECDH
  1471. else if (alg_k & SSL_kEECDH)
  1472. {
  1473. EC_GROUP *ngroup;
  1474. const EC_GROUP *group;
  1475. if ((ecdh=EC_KEY_new()) == NULL)
  1476. {
  1477. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1478. goto err;
  1479. }
  1480. /* Extract elliptic curve parameters and the
  1481. * server's ephemeral ECDH public key.
  1482. * Keep accumulating lengths of various components in
  1483. * param_len and make sure it never exceeds n.
  1484. */
  1485. /* XXX: For now we only support named (not generic) curves
  1486. * and the ECParameters in this case is just three bytes.
  1487. */
  1488. param_len=3;
  1489. if ((param_len > n) ||
  1490. (*p != NAMED_CURVE_TYPE) ||
  1491. ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0))
  1492. {
  1493. al=SSL_AD_INTERNAL_ERROR;
  1494. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  1495. goto f_err;
  1496. }
  1497. ngroup = EC_GROUP_new_by_curve_name(curve_nid);
  1498. if (ngroup == NULL)
  1499. {
  1500. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
  1501. goto err;
  1502. }
  1503. if (EC_KEY_set_group(ecdh, ngroup) == 0)
  1504. {
  1505. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
  1506. goto err;
  1507. }
  1508. EC_GROUP_free(ngroup);
  1509. group = EC_KEY_get0_group(ecdh);
  1510. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  1511. (EC_GROUP_get_degree(group) > 163))
  1512. {
  1513. al=SSL_AD_EXPORT_RESTRICTION;
  1514. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
  1515. goto f_err;
  1516. }
  1517. p+=3;
  1518. /* Next, get the encoded ECPoint */
  1519. if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
  1520. ((bn_ctx = BN_CTX_new()) == NULL))
  1521. {
  1522. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1523. goto err;
  1524. }
  1525. encoded_pt_len = *p; /* length of encoded point */
  1526. p+=1;
  1527. param_len += (1 + encoded_pt_len);
  1528. if ((param_len > n) ||
  1529. (EC_POINT_oct2point(group, srvr_ecpoint,
  1530. p, encoded_pt_len, bn_ctx) == 0))
  1531. {
  1532. al=SSL_AD_DECODE_ERROR;
  1533. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_ECPOINT);
  1534. goto f_err;
  1535. }
  1536. n-=param_len;
  1537. p+=encoded_pt_len;
  1538. /* The ECC/TLS specification does not mention
  1539. * the use of DSA to sign ECParameters in the server
  1540. * key exchange message. We do support RSA and ECDSA.
  1541. */
  1542. if (0) ;
  1543. #ifndef OPENSSL_NO_RSA
  1544. else if (alg_a & SSL_aRSA)
  1545. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1546. #endif
  1547. #ifndef OPENSSL_NO_ECDSA
  1548. else if (alg_a & SSL_aECDSA)
  1549. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  1550. #endif
  1551. /* else anonymous ECDH, so no certificate or pkey. */
  1552. EC_KEY_set_public_key(ecdh, srvr_ecpoint);
  1553. s->session->sess_cert->peer_ecdh_tmp=ecdh;
  1554. ecdh=NULL;
  1555. BN_CTX_free(bn_ctx);
  1556. bn_ctx = NULL;
  1557. EC_POINT_free(srvr_ecpoint);
  1558. srvr_ecpoint = NULL;
  1559. }
  1560. else if (alg_k)
  1561. {
  1562. al=SSL_AD_UNEXPECTED_MESSAGE;
  1563. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
  1564. goto f_err;
  1565. }
  1566. #endif /* !OPENSSL_NO_ECDH */
  1567. /* p points to the next byte, there are 'n' bytes left */
  1568. /* if it was signed, check the signature */
  1569. if (pkey != NULL)
  1570. {
  1571. if (TLS1_get_version(s) >= TLS1_2_VERSION)
  1572. {
  1573. int sigalg = tls12_get_sigid(pkey);
  1574. /* Should never happen */
  1575. if (sigalg == -1)
  1576. {
  1577. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1578. goto err;
  1579. }
  1580. /* Check key type is consistent with signature */
  1581. if (sigalg != (int)p[1])
  1582. {
  1583. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_TYPE);
  1584. al=SSL_AD_DECODE_ERROR;
  1585. goto f_err;
  1586. }
  1587. md = tls12_get_hash(p[0]);
  1588. if (md == NULL)
  1589. {
  1590. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNKNOWN_DIGEST);
  1591. al=SSL_AD_DECODE_ERROR;
  1592. goto f_err;
  1593. }
  1594. #ifdef SSL_DEBUG
  1595. fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
  1596. #endif
  1597. p += 2;
  1598. n -= 2;
  1599. }
  1600. else
  1601. md = EVP_sha1();
  1602. n2s(p,i);
  1603. n-=2;
  1604. j=EVP_PKEY_size(pkey);
  1605. if ((i != n) || (n > j) || (n <= 0))
  1606. {
  1607. /* wrong packet length */
  1608. al=SSL_AD_DECODE_ERROR;
  1609. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
  1610. goto f_err;
  1611. }
  1612. #ifndef OPENSSL_NO_RSA
  1613. if (pkey->type == EVP_PKEY_RSA && TLS1_get_version(s) < TLS1_2_VERSION)
  1614. {
  1615. int num;
  1616. j=0;
  1617. q=md_buf;
  1618. for (num=2; num > 0; num--)
  1619. {
  1620. EVP_MD_CTX_set_flags(&md_ctx,
  1621. EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  1622. EVP_DigestInit_ex(&md_ctx,(num == 2)
  1623. ?s->ctx->md5:s->ctx->sha1, NULL);
  1624. EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1625. EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1626. EVP_DigestUpdate(&md_ctx,param,param_len);
  1627. EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i);
  1628. q+=i;
  1629. j+=i;
  1630. }
  1631. i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
  1632. pkey->pkey.rsa);
  1633. if (i < 0)
  1634. {
  1635. al=SSL_AD_DECRYPT_ERROR;
  1636. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
  1637. goto f_err;
  1638. }
  1639. if (i == 0)
  1640. {
  1641. /* bad signature */
  1642. al=SSL_AD_DECRYPT_ERROR;
  1643. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
  1644. goto f_err;
  1645. }
  1646. }
  1647. else
  1648. #endif
  1649. {
  1650. EVP_VerifyInit_ex(&md_ctx, md, NULL);
  1651. EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1652. EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1653. EVP_VerifyUpdate(&md_ctx,param,param_len);
  1654. if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0)
  1655. {
  1656. /* bad signature */
  1657. al=SSL_AD_DECRYPT_ERROR;
  1658. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
  1659. goto f_err;
  1660. }
  1661. }
  1662. }
  1663. else
  1664. {
  1665. if (!(alg_a & SSL_aNULL) && !(alg_k & SSL_kPSK))
  1666. /* aNULL or kPSK do not need public keys */
  1667. {
  1668. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1669. goto err;
  1670. }
  1671. /* still data left over */
  1672. if (n != 0)
  1673. {
  1674. al=SSL_AD_DECODE_ERROR;
  1675. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
  1676. goto f_err;
  1677. }
  1678. }
  1679. EVP_PKEY_free(pkey);
  1680. EVP_MD_CTX_cleanup(&md_ctx);
  1681. return(1);
  1682. f_err:
  1683. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1684. err:
  1685. EVP_PKEY_free(pkey);
  1686. #ifndef OPENSSL_NO_RSA
  1687. if (rsa != NULL)
  1688. RSA_free(rsa);
  1689. #endif
  1690. #ifndef OPENSSL_NO_DH
  1691. if (dh != NULL)
  1692. DH_free(dh);
  1693. #endif
  1694. #ifndef OPENSSL_NO_ECDH
  1695. BN_CTX_free(bn_ctx);
  1696. EC_POINT_free(srvr_ecpoint);
  1697. if (ecdh != NULL)
  1698. EC_KEY_free(ecdh);
  1699. #endif
  1700. EVP_MD_CTX_cleanup(&md_ctx);
  1701. return(-1);
  1702. }
  1703. int ssl3_get_certificate_request(SSL *s)
  1704. {
  1705. int ok,ret=0;
  1706. unsigned long n,nc,l;
  1707. unsigned int llen, ctype_num,i;
  1708. X509_NAME *xn=NULL;
  1709. const unsigned char *p,*q;
  1710. unsigned char *d;
  1711. STACK_OF(X509_NAME) *ca_sk=NULL;
  1712. n=s->method->ssl_get_message(s,
  1713. SSL3_ST_CR_CERT_REQ_A,
  1714. SSL3_ST_CR_CERT_REQ_B,
  1715. -1,
  1716. s->max_cert_list,
  1717. &ok);
  1718. if (!ok) return((int)n);
  1719. s->s3->tmp.cert_req=0;
  1720. if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
  1721. {
  1722. s->s3->tmp.reuse_message=1;
  1723. /* If we get here we don't need any cached handshake records
  1724. * as we wont be doing client auth.
  1725. */
  1726. if (s->s3->handshake_buffer)
  1727. {
  1728. if (!ssl3_digest_cached_records(s))
  1729. goto err;
  1730. }
  1731. return(1);
  1732. }
  1733. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
  1734. {
  1735. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  1736. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
  1737. goto err;
  1738. }
  1739. /* TLS does not like anon-DH with client cert */
  1740. if (s->version > SSL3_VERSION)
  1741. {
  1742. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
  1743. {
  1744. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  1745. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
  1746. goto err;
  1747. }
  1748. }
  1749. p=d=(unsigned char *)s->init_msg;
  1750. if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
  1751. {
  1752. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
  1753. goto err;
  1754. }
  1755. /* get the certificate types */
  1756. ctype_num= *(p++);
  1757. if (ctype_num > SSL3_CT_NUMBER)
  1758. ctype_num=SSL3_CT_NUMBER;
  1759. for (i=0; i<ctype_num; i++)
  1760. s->s3->tmp.ctype[i]= p[i];
  1761. p+=ctype_num;
  1762. if (TLS1_get_version(s) >= TLS1_2_VERSION)
  1763. {
  1764. n2s(p, llen);
  1765. /* Check we have enough room for signature algorithms and
  1766. * following length value.
  1767. */
  1768. if ((unsigned long)(p - d + llen + 2) > n)
  1769. {
  1770. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1771. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_DATA_LENGTH_TOO_LONG);
  1772. goto err;
  1773. }
  1774. if ((llen & 1) || !tls1_process_sigalgs(s, p, llen))
  1775. {
  1776. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1777. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_SIGNATURE_ALGORITHMS_ERROR);
  1778. goto err;
  1779. }
  1780. p += llen;
  1781. }
  1782. /* get the CA RDNs */
  1783. n2s(p,llen);
  1784. #if 0
  1785. {
  1786. FILE *out;
  1787. out=fopen("/tmp/vsign.der","w");
  1788. fwrite(p,1,llen,out);
  1789. fclose(out);
  1790. }
  1791. #endif
  1792. if ((unsigned long)(p - d + llen) != n)
  1793. {
  1794. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1795. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
  1796. goto err;
  1797. }
  1798. for (nc=0; nc<llen; )
  1799. {
  1800. n2s(p,l);
  1801. if ((l+nc+2) > llen)
  1802. {
  1803. if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  1804. goto cont; /* netscape bugs */
  1805. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1806. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
  1807. goto err;
  1808. }
  1809. q=p;
  1810. if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
  1811. {
  1812. /* If netscape tolerance is on, ignore errors */
  1813. if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
  1814. goto cont;
  1815. else
  1816. {
  1817. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1818. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
  1819. goto err;
  1820. }
  1821. }
  1822. if (q != (p+l))
  1823. {
  1824. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1825. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
  1826. goto err;
  1827. }
  1828. if (!sk_X509_NAME_push(ca_sk,xn))
  1829. {
  1830. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
  1831. goto err;
  1832. }
  1833. p+=l;
  1834. nc+=l+2;
  1835. }
  1836. if (0)
  1837. {
  1838. cont:
  1839. ERR_clear_error();
  1840. }
  1841. /* we should setup a certificate to return.... */
  1842. s->s3->tmp.cert_req=1;
  1843. s->s3->tmp.ctype_num=ctype_num;
  1844. if (s->s3->tmp.ca_names != NULL)
  1845. sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
  1846. s->s3->tmp.ca_names=ca_sk;
  1847. ca_sk=NULL;
  1848. ret=1;
  1849. err:
  1850. if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
  1851. return(ret);
  1852. }
  1853. static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
  1854. {
  1855. return(X509_NAME_cmp(*a,*b));
  1856. }
  1857. #ifndef OPENSSL_NO_TLSEXT
  1858. int ssl3_get_new_session_ticket(SSL *s)
  1859. {
  1860. int ok,al,ret=0, ticklen;
  1861. long n;
  1862. const unsigned char *p;
  1863. unsigned char *d;
  1864. n=s->method->ssl_get_message(s,
  1865. SSL3_ST_CR_SESSION_TICKET_A,
  1866. SSL3_ST_CR_SESSION_TICKET_B,
  1867. -1,
  1868. 16384,
  1869. &ok);
  1870. if (!ok)
  1871. return((int)n);
  1872. if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
  1873. {
  1874. s->s3->tmp.reuse_message=1;
  1875. return(1);
  1876. }
  1877. if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET)
  1878. {
  1879. al=SSL_AD_UNEXPECTED_MESSAGE;
  1880. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_BAD_MESSAGE_TYPE);
  1881. goto f_err;
  1882. }
  1883. if (n < 6)
  1884. {
  1885. /* need at least ticket_lifetime_hint + ticket length */
  1886. al = SSL_AD_DECODE_ERROR;
  1887. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH);
  1888. goto f_err;
  1889. }
  1890. p=d=(unsigned char *)s->init_msg;
  1891. n2l(p, s->session->tlsext_tick_lifetime_hint);
  1892. n2s(p, ticklen);
  1893. /* ticket_lifetime_hint + ticket_length + ticket */
  1894. if (ticklen + 6 != n)
  1895. {
  1896. al = SSL_AD_DECODE_ERROR;
  1897. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH);
  1898. goto f_err;
  1899. }
  1900. if (s->session->tlsext_tick)
  1901. {
  1902. OPENSSL_free(s->session->tlsext_tick);
  1903. s->session->tlsext_ticklen = 0;
  1904. }
  1905. s->session->tlsext_tick = OPENSSL_malloc(ticklen);
  1906. if (!s->session->tlsext_tick)
  1907. {
  1908. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,ERR_R_MALLOC_FAILURE);
  1909. goto err;
  1910. }
  1911. memcpy(s->session->tlsext_tick, p, ticklen);
  1912. s->session->tlsext_ticklen = ticklen;
  1913. /* There are two ways to detect a resumed ticket sesion.
  1914. * One is to set an appropriate session ID and then the server
  1915. * must return a match in ServerHello. This allows the normal
  1916. * client session ID matching to work and we know much
  1917. * earlier that the ticket has been accepted.
  1918. *
  1919. * The other way is to set zero length session ID when the
  1920. * ticket is presented and rely on the handshake to determine
  1921. * session resumption.
  1922. *
  1923. * We choose the former approach because this fits in with
  1924. * assumptions elsewhere in OpenSSL. The session ID is set
  1925. * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
  1926. * ticket.
  1927. */
  1928. EVP_Digest(p, ticklen,
  1929. s->session->session_id, &s->session->session_id_length,
  1930. #ifndef OPENSSL_NO_SHA256
  1931. EVP_sha256(), NULL);
  1932. #else
  1933. EVP_sha1(), NULL);
  1934. #endif
  1935. ret=1;
  1936. return(ret);
  1937. f_err:
  1938. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1939. err:
  1940. return(-1);
  1941. }
  1942. int ssl3_get_cert_status(SSL *s)
  1943. {
  1944. int ok, al;
  1945. unsigned long resplen,n;
  1946. const unsigned char *p;
  1947. n=s->method->ssl_get_message(s,
  1948. SSL3_ST_CR_CERT_STATUS_A,
  1949. SSL3_ST_CR_CERT_STATUS_B,
  1950. SSL3_MT_CERTIFICATE_STATUS,
  1951. 16384,
  1952. &ok);
  1953. if (!ok) return((int)n);
  1954. if (n < 4)
  1955. {
  1956. /* need at least status type + length */
  1957. al = SSL_AD_DECODE_ERROR;
  1958. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
  1959. goto f_err;
  1960. }
  1961. p = (unsigned char *)s->init_msg;
  1962. if (*p++ != TLSEXT_STATUSTYPE_ocsp)
  1963. {
  1964. al = SSL_AD_DECODE_ERROR;
  1965. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_UNSUPPORTED_STATUS_TYPE);
  1966. goto f_err;
  1967. }
  1968. n2l3(p, resplen);
  1969. if (resplen + 4 != n)
  1970. {
  1971. al = SSL_AD_DECODE_ERROR;
  1972. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
  1973. goto f_err;
  1974. }
  1975. if (s->tlsext_ocsp_resp)
  1976. OPENSSL_free(s->tlsext_ocsp_resp);
  1977. s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
  1978. if (!s->tlsext_ocsp_resp)
  1979. {
  1980. al = SSL_AD_INTERNAL_ERROR;
  1981. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE);
  1982. goto f_err;
  1983. }
  1984. s->tlsext_ocsp_resplen = resplen;
  1985. if (s->ctx->tlsext_status_cb)
  1986. {
  1987. int ret;
  1988. ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
  1989. if (ret == 0)
  1990. {
  1991. al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
  1992. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_INVALID_STATUS_RESPONSE);
  1993. goto f_err;
  1994. }
  1995. if (ret < 0)
  1996. {
  1997. al = SSL_AD_INTERNAL_ERROR;
  1998. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE);
  1999. goto f_err;
  2000. }
  2001. }
  2002. return 1;
  2003. f_err:
  2004. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  2005. return(-1);
  2006. }
  2007. #endif
  2008. int ssl3_get_server_done(SSL *s)
  2009. {
  2010. int ok,ret=0;
  2011. long n;
  2012. n=s->method->ssl_get_message(s,
  2013. SSL3_ST_CR_SRVR_DONE_A,
  2014. SSL3_ST_CR_SRVR_DONE_B,
  2015. SSL3_MT_SERVER_DONE,
  2016. 30, /* should be very small, like 0 :-) */
  2017. &ok);
  2018. if (!ok) return((int)n);
  2019. if (n > 0)
  2020. {
  2021. /* should contain no data */
  2022. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  2023. SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
  2024. return -1;
  2025. }
  2026. ret=1;
  2027. return(ret);
  2028. }
  2029. int ssl3_send_client_key_exchange(SSL *s)
  2030. {
  2031. unsigned char *p,*d;
  2032. int n;
  2033. unsigned long alg_k;
  2034. #ifndef OPENSSL_NO_RSA
  2035. unsigned char *q;
  2036. EVP_PKEY *pkey=NULL;
  2037. #endif
  2038. #ifndef OPENSSL_NO_KRB5
  2039. KSSL_ERR kssl_err;
  2040. #endif /* OPENSSL_NO_KRB5 */
  2041. #ifndef OPENSSL_NO_ECDH
  2042. EC_KEY *clnt_ecdh = NULL;
  2043. const EC_POINT *srvr_ecpoint = NULL;
  2044. EVP_PKEY *srvr_pub_pkey = NULL;
  2045. unsigned char *encodedPoint = NULL;
  2046. int encoded_pt_len = 0;
  2047. BN_CTX * bn_ctx = NULL;
  2048. #endif
  2049. if (s->state == SSL3_ST_CW_KEY_EXCH_A)
  2050. {
  2051. d=(unsigned char *)s->init_buf->data;
  2052. p= &(d[4]);
  2053. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  2054. /* Fool emacs indentation */
  2055. if (0) {}
  2056. #ifndef OPENSSL_NO_RSA
  2057. else if (alg_k & SSL_kRSA)
  2058. {
  2059. RSA *rsa;
  2060. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  2061. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  2062. rsa=s->session->sess_cert->peer_rsa_tmp;
  2063. else
  2064. {
  2065. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  2066. if ((pkey == NULL) ||
  2067. (pkey->type != EVP_PKEY_RSA) ||
  2068. (pkey->pkey.rsa == NULL))
  2069. {
  2070. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  2071. goto err;
  2072. }
  2073. rsa=pkey->pkey.rsa;
  2074. EVP_PKEY_free(pkey);
  2075. }
  2076. tmp_buf[0]=s->client_version>>8;
  2077. tmp_buf[1]=s->client_version&0xff;
  2078. if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
  2079. goto err;
  2080. s->session->master_key_length=sizeof tmp_buf;
  2081. q=p;
  2082. /* Fix buf for TLS and beyond */
  2083. if (s->version > SSL3_VERSION)
  2084. p+=2;
  2085. n=RSA_public_encrypt(sizeof tmp_buf,
  2086. tmp_buf,p,rsa,RSA_PKCS1_PADDING);
  2087. #ifdef PKCS1_CHECK
  2088. if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
  2089. if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
  2090. #endif
  2091. if (n <= 0)
  2092. {
  2093. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
  2094. goto err;
  2095. }
  2096. /* Fix buf for TLS and beyond */
  2097. if (s->version > SSL3_VERSION)
  2098. {
  2099. s2n(n,q);
  2100. n+=2;
  2101. }
  2102. s->session->master_key_length=
  2103. s->method->ssl3_enc->generate_master_secret(s,
  2104. s->session->master_key,
  2105. tmp_buf,sizeof tmp_buf);
  2106. OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
  2107. }
  2108. #endif
  2109. #ifndef OPENSSL_NO_KRB5
  2110. else if (alg_k & SSL_kKRB5)
  2111. {
  2112. krb5_error_code krb5rc;
  2113. KSSL_CTX *kssl_ctx = s->kssl_ctx;
  2114. /* krb5_data krb5_ap_req; */
  2115. krb5_data *enc_ticket;
  2116. krb5_data authenticator, *authp = NULL;
  2117. EVP_CIPHER_CTX ciph_ctx;
  2118. const EVP_CIPHER *enc = NULL;
  2119. unsigned char iv[EVP_MAX_IV_LENGTH];
  2120. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  2121. unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
  2122. + EVP_MAX_IV_LENGTH];
  2123. int padl, outl = sizeof(epms);
  2124. EVP_CIPHER_CTX_init(&ciph_ctx);
  2125. #ifdef KSSL_DEBUG
  2126. printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
  2127. alg_k, SSL_kKRB5);
  2128. #endif /* KSSL_DEBUG */
  2129. authp = NULL;
  2130. #ifdef KRB5SENDAUTH
  2131. if (KRB5SENDAUTH) authp = &authenticator;
  2132. #endif /* KRB5SENDAUTH */
  2133. krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
  2134. &kssl_err);
  2135. enc = kssl_map_enc(kssl_ctx->enctype);
  2136. if (enc == NULL)
  2137. goto err;
  2138. #ifdef KSSL_DEBUG
  2139. {
  2140. printf("kssl_cget_tkt rtn %d\n", krb5rc);
  2141. if (krb5rc && kssl_err.text)
  2142. printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
  2143. }
  2144. #endif /* KSSL_DEBUG */
  2145. if (krb5rc)
  2146. {
  2147. ssl3_send_alert(s,SSL3_AL_FATAL,
  2148. SSL_AD_HANDSHAKE_FAILURE);
  2149. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2150. kssl_err.reason);
  2151. goto err;
  2152. }
  2153. /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
  2154. ** in place of RFC 2712 KerberosWrapper, as in:
  2155. **
  2156. ** Send ticket (copy to *p, set n = length)
  2157. ** n = krb5_ap_req.length;
  2158. ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
  2159. ** if (krb5_ap_req.data)
  2160. ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
  2161. **
  2162. ** Now using real RFC 2712 KerberosWrapper
  2163. ** (Thanks to Simon Wilkinson <[email protected]>)
  2164. ** Note: 2712 "opaque" types are here replaced
  2165. ** with a 2-byte length followed by the value.
  2166. ** Example:
  2167. ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
  2168. ** Where "xx xx" = length bytes. Shown here with
  2169. ** optional authenticator omitted.
  2170. */
  2171. /* KerberosWrapper.Ticket */
  2172. s2n(enc_ticket->length,p);
  2173. memcpy(p, enc_ticket->data, enc_ticket->length);
  2174. p+= enc_ticket->length;
  2175. n = enc_ticket->length + 2;
  2176. /* KerberosWrapper.Authenticator */
  2177. if (authp && authp->length)
  2178. {
  2179. s2n(authp->length,p);
  2180. memcpy(p, authp->data, authp->length);
  2181. p+= authp->length;
  2182. n+= authp->length + 2;
  2183. free(authp->data);
  2184. authp->data = NULL;
  2185. authp->length = 0;
  2186. }
  2187. else
  2188. {
  2189. s2n(0,p);/* null authenticator length */
  2190. n+=2;
  2191. }
  2192. tmp_buf[0]=s->client_version>>8;
  2193. tmp_buf[1]=s->client_version&0xff;
  2194. if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
  2195. goto err;
  2196. /* 20010420 VRS. Tried it this way; failed.
  2197. ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
  2198. ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
  2199. ** kssl_ctx->length);
  2200. ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
  2201. */
  2202. memset(iv, 0, sizeof iv); /* per RFC 1510 */
  2203. EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
  2204. kssl_ctx->key,iv);
  2205. EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
  2206. sizeof tmp_buf);
  2207. EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
  2208. outl += padl;
  2209. if (outl > (int)sizeof epms)
  2210. {
  2211. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  2212. goto err;
  2213. }
  2214. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  2215. /* KerberosWrapper.EncryptedPreMasterSecret */
  2216. s2n(outl,p);
  2217. memcpy(p, epms, outl);
  2218. p+=outl;
  2219. n+=outl + 2;
  2220. s->session->master_key_length=
  2221. s->method->ssl3_enc->generate_master_secret(s,
  2222. s->session->master_key,
  2223. tmp_buf, sizeof tmp_buf);
  2224. OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
  2225. OPENSSL_cleanse(epms, outl);
  2226. }
  2227. #endif
  2228. #ifndef OPENSSL_NO_DH
  2229. else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  2230. {
  2231. DH *dh_srvr,*dh_clnt;
  2232. if (s->session->sess_cert == NULL)
  2233. {
  2234. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  2235. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
  2236. goto err;
  2237. }
  2238. if (s->session->sess_cert->peer_dh_tmp != NULL)
  2239. dh_srvr=s->session->sess_cert->peer_dh_tmp;
  2240. else
  2241. {
  2242. /* we get them from the cert */
  2243. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  2244. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
  2245. goto err;
  2246. }
  2247. /* generate a new random key */
  2248. if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
  2249. {
  2250. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  2251. goto err;
  2252. }
  2253. if (!DH_generate_key(dh_clnt))
  2254. {
  2255. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  2256. DH_free(dh_clnt);
  2257. goto err;
  2258. }
  2259. /* use the 'p' output buffer for the DH key, but
  2260. * make sure to clear it out afterwards */
  2261. n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
  2262. if (n <= 0)
  2263. {
  2264. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  2265. DH_free(dh_clnt);
  2266. goto err;
  2267. }
  2268. /* generate master key from the result */
  2269. s->session->master_key_length=
  2270. s->method->ssl3_enc->generate_master_secret(s,
  2271. s->session->master_key,p,n);
  2272. /* clean up */
  2273. memset(p,0,n);
  2274. /* send off the data */
  2275. n=BN_num_bytes(dh_clnt->pub_key);
  2276. s2n(n,p);
  2277. BN_bn2bin(dh_clnt->pub_key,p);
  2278. n+=2;
  2279. DH_free(dh_clnt);
  2280. /* perhaps clean things up a bit EAY EAY EAY EAY*/
  2281. }
  2282. #endif
  2283. #ifndef OPENSSL_NO_ECDH
  2284. else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
  2285. {
  2286. const EC_GROUP *srvr_group = NULL;
  2287. EC_KEY *tkey;
  2288. int ecdh_clnt_cert = 0;
  2289. int field_size = 0;
  2290. /* Did we send out the client's
  2291. * ECDH share for use in premaster
  2292. * computation as part of client certificate?
  2293. * If so, set ecdh_clnt_cert to 1.
  2294. */
  2295. if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
  2296. {
  2297. /* XXX: For now, we do not support client
  2298. * authentication using ECDH certificates.
  2299. * To add such support, one needs to add
  2300. * code that checks for appropriate
  2301. * conditions and sets ecdh_clnt_cert to 1.
  2302. * For example, the cert have an ECC
  2303. * key on the same curve as the server's
  2304. * and the key should be authorized for
  2305. * key agreement.
  2306. *
  2307. * One also needs to add code in ssl3_connect
  2308. * to skip sending the certificate verify
  2309. * message.
  2310. *
  2311. * if ((s->cert->key->privatekey != NULL) &&
  2312. * (s->cert->key->privatekey->type ==
  2313. * EVP_PKEY_EC) && ...)
  2314. * ecdh_clnt_cert = 1;
  2315. */
  2316. }
  2317. if (s->session->sess_cert->peer_ecdh_tmp != NULL)
  2318. {
  2319. tkey = s->session->sess_cert->peer_ecdh_tmp;
  2320. }
  2321. else
  2322. {
  2323. /* Get the Server Public Key from Cert */
  2324. srvr_pub_pkey = X509_get_pubkey(s->session-> \
  2325. sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  2326. if ((srvr_pub_pkey == NULL) ||
  2327. (srvr_pub_pkey->type != EVP_PKEY_EC) ||
  2328. (srvr_pub_pkey->pkey.ec == NULL))
  2329. {
  2330. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2331. ERR_R_INTERNAL_ERROR);
  2332. goto err;
  2333. }
  2334. tkey = srvr_pub_pkey->pkey.ec;
  2335. }
  2336. srvr_group = EC_KEY_get0_group(tkey);
  2337. srvr_ecpoint = EC_KEY_get0_public_key(tkey);
  2338. if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
  2339. {
  2340. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2341. ERR_R_INTERNAL_ERROR);
  2342. goto err;
  2343. }
  2344. if ((clnt_ecdh=EC_KEY_new()) == NULL)
  2345. {
  2346. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  2347. goto err;
  2348. }
  2349. if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
  2350. {
  2351. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
  2352. goto err;
  2353. }
  2354. if (ecdh_clnt_cert)
  2355. {
  2356. /* Reuse key info from our certificate
  2357. * We only need our private key to perform
  2358. * the ECDH computation.
  2359. */
  2360. const BIGNUM *priv_key;
  2361. tkey = s->cert->key->privatekey->pkey.ec;
  2362. priv_key = EC_KEY_get0_private_key(tkey);
  2363. if (priv_key == NULL)
  2364. {
  2365. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  2366. goto err;
  2367. }
  2368. if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
  2369. {
  2370. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
  2371. goto err;
  2372. }
  2373. }
  2374. else
  2375. {
  2376. /* Generate a new ECDH key pair */
  2377. if (!(EC_KEY_generate_key(clnt_ecdh)))
  2378. {
  2379. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  2380. goto err;
  2381. }
  2382. }
  2383. /* use the 'p' output buffer for the ECDH key, but
  2384. * make sure to clear it out afterwards
  2385. */
  2386. field_size = EC_GROUP_get_degree(srvr_group);
  2387. if (field_size <= 0)
  2388. {
  2389. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2390. ERR_R_ECDH_LIB);
  2391. goto err;
  2392. }
  2393. n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
  2394. if (n <= 0)
  2395. {
  2396. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2397. ERR_R_ECDH_LIB);
  2398. goto err;
  2399. }
  2400. /* generate master key from the result */
  2401. s->session->master_key_length = s->method->ssl3_enc \
  2402. -> generate_master_secret(s,
  2403. s->session->master_key,
  2404. p, n);
  2405. memset(p, 0, n); /* clean up */
  2406. if (ecdh_clnt_cert)
  2407. {
  2408. /* Send empty client key exch message */
  2409. n = 0;
  2410. }
  2411. else
  2412. {
  2413. /* First check the size of encoding and
  2414. * allocate memory accordingly.
  2415. */
  2416. encoded_pt_len =
  2417. EC_POINT_point2oct(srvr_group,
  2418. EC_KEY_get0_public_key(clnt_ecdh),
  2419. POINT_CONVERSION_UNCOMPRESSED,
  2420. NULL, 0, NULL);
  2421. encodedPoint = (unsigned char *)
  2422. OPENSSL_malloc(encoded_pt_len *
  2423. sizeof(unsigned char));
  2424. bn_ctx = BN_CTX_new();
  2425. if ((encodedPoint == NULL) ||
  2426. (bn_ctx == NULL))
  2427. {
  2428. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  2429. goto err;
  2430. }
  2431. /* Encode the public key */
  2432. n = EC_POINT_point2oct(srvr_group,
  2433. EC_KEY_get0_public_key(clnt_ecdh),
  2434. POINT_CONVERSION_UNCOMPRESSED,
  2435. encodedPoint, encoded_pt_len, bn_ctx);
  2436. *p = n; /* length of encoded point */
  2437. /* Encoded point will be copied here */
  2438. p += 1;
  2439. /* copy the point */
  2440. memcpy((unsigned char *)p, encodedPoint, n);
  2441. /* increment n to account for length field */
  2442. n += 1;
  2443. }
  2444. /* Free allocated memory */
  2445. BN_CTX_free(bn_ctx);
  2446. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  2447. if (clnt_ecdh != NULL)
  2448. EC_KEY_free(clnt_ecdh);
  2449. EVP_PKEY_free(srvr_pub_pkey);
  2450. }
  2451. #endif /* !OPENSSL_NO_ECDH */
  2452. else if (alg_k & SSL_kGOST)
  2453. {
  2454. /* GOST key exchange message creation */
  2455. EVP_PKEY_CTX *pkey_ctx;
  2456. X509 *peer_cert;
  2457. size_t msglen;
  2458. unsigned int md_len;
  2459. int keytype;
  2460. unsigned char premaster_secret[32],shared_ukm[32], tmp[256];
  2461. EVP_MD_CTX *ukm_hash;
  2462. EVP_PKEY *pub_key;
  2463. /* Get server sertificate PKEY and create ctx from it */
  2464. peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST01)].x509;
  2465. if (!peer_cert)
  2466. peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST94)].x509;
  2467. if (!peer_cert) {
  2468. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
  2469. goto err;
  2470. }
  2471. pkey_ctx=EVP_PKEY_CTX_new(pub_key=X509_get_pubkey(peer_cert),NULL);
  2472. /* If we have send a certificate, and certificate key
  2473. * parameters match those of server certificate, use
  2474. * certificate key for key exchange
  2475. */
  2476. /* Otherwise, generate ephemeral key pair */
  2477. EVP_PKEY_encrypt_init(pkey_ctx);
  2478. /* Generate session key */
  2479. RAND_bytes(premaster_secret,32);
  2480. /* If we have client certificate, use its secret as peer key */
  2481. if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
  2482. if (EVP_PKEY_derive_set_peer(pkey_ctx,s->cert->key->privatekey) <=0) {
  2483. /* If there was an error - just ignore it. Ephemeral key
  2484. * would be used
  2485. */
  2486. ERR_clear_error();
  2487. }
  2488. }
  2489. /* Compute shared IV and store it in algorithm-specific
  2490. * context data */
  2491. ukm_hash = EVP_MD_CTX_create();
  2492. EVP_DigestInit(ukm_hash,EVP_get_digestbynid(NID_id_GostR3411_94));
  2493. EVP_DigestUpdate(ukm_hash,s->s3->client_random,SSL3_RANDOM_SIZE);
  2494. EVP_DigestUpdate(ukm_hash,s->s3->server_random,SSL3_RANDOM_SIZE);
  2495. EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
  2496. EVP_MD_CTX_destroy(ukm_hash);
  2497. if (EVP_PKEY_CTX_ctrl(pkey_ctx,-1,EVP_PKEY_OP_ENCRYPT,EVP_PKEY_CTRL_SET_IV,
  2498. 8,shared_ukm)<0) {
  2499. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2500. SSL_R_LIBRARY_BUG);
  2501. goto err;
  2502. }
  2503. /* Make GOST keytransport blob message */
  2504. /*Encapsulate it into sequence */
  2505. *(p++)=V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
  2506. msglen=255;
  2507. if (EVP_PKEY_encrypt(pkey_ctx,tmp,&msglen,premaster_secret,32)<0) {
  2508. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2509. SSL_R_LIBRARY_BUG);
  2510. goto err;
  2511. }
  2512. if (msglen >= 0x80)
  2513. {
  2514. *(p++)=0x81;
  2515. *(p++)= msglen & 0xff;
  2516. n=msglen+3;
  2517. }
  2518. else
  2519. {
  2520. *(p++)= msglen & 0xff;
  2521. n=msglen+2;
  2522. }
  2523. memcpy(p, tmp, msglen);
  2524. /* Check if pubkey from client certificate was used */
  2525. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
  2526. {
  2527. /* Set flag "skip certificate verify" */
  2528. s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
  2529. }
  2530. EVP_PKEY_CTX_free(pkey_ctx);
  2531. s->session->master_key_length=
  2532. s->method->ssl3_enc->generate_master_secret(s,
  2533. s->session->master_key,premaster_secret,32);
  2534. EVP_PKEY_free(pub_key);
  2535. }
  2536. #ifndef OPENSSL_NO_SRP
  2537. else if (alg_k & SSL_kSRP)
  2538. {
  2539. if (s->srp_ctx.A != NULL)
  2540. {
  2541. /* send off the data */
  2542. n=BN_num_bytes(s->srp_ctx.A);
  2543. s2n(n,p);
  2544. BN_bn2bin(s->srp_ctx.A,p);
  2545. n+=2;
  2546. }
  2547. else
  2548. {
  2549. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  2550. goto err;
  2551. }
  2552. if (s->session->srp_username != NULL)
  2553. OPENSSL_free(s->session->srp_username);
  2554. s->session->srp_username = BUF_strdup(s->srp_ctx.login);
  2555. if (s->session->srp_username == NULL)
  2556. {
  2557. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2558. ERR_R_MALLOC_FAILURE);
  2559. goto err;
  2560. }
  2561. if ((s->session->master_key_length = SRP_generate_client_master_secret(s,s->session->master_key))<0)
  2562. {
  2563. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  2564. goto err;
  2565. }
  2566. }
  2567. #endif
  2568. #ifndef OPENSSL_NO_PSK
  2569. else if (alg_k & SSL_kPSK)
  2570. {
  2571. char identity[PSK_MAX_IDENTITY_LEN];
  2572. unsigned char *t = NULL;
  2573. unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
  2574. unsigned int pre_ms_len = 0, psk_len = 0;
  2575. int psk_err = 1;
  2576. n = 0;
  2577. if (s->psk_client_callback == NULL)
  2578. {
  2579. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2580. SSL_R_PSK_NO_CLIENT_CB);
  2581. goto err;
  2582. }
  2583. psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
  2584. identity, PSK_MAX_IDENTITY_LEN,
  2585. psk_or_pre_ms, sizeof(psk_or_pre_ms));
  2586. if (psk_len > PSK_MAX_PSK_LEN)
  2587. {
  2588. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2589. ERR_R_INTERNAL_ERROR);
  2590. goto psk_err;
  2591. }
  2592. else if (psk_len == 0)
  2593. {
  2594. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2595. SSL_R_PSK_IDENTITY_NOT_FOUND);
  2596. goto psk_err;
  2597. }
  2598. /* create PSK pre_master_secret */
  2599. pre_ms_len = 2+psk_len+2+psk_len;
  2600. t = psk_or_pre_ms;
  2601. memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
  2602. s2n(psk_len, t);
  2603. memset(t, 0, psk_len);
  2604. t+=psk_len;
  2605. s2n(psk_len, t);
  2606. if (s->session->psk_identity_hint != NULL)
  2607. OPENSSL_free(s->session->psk_identity_hint);
  2608. s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
  2609. if (s->ctx->psk_identity_hint != NULL &&
  2610. s->session->psk_identity_hint == NULL)
  2611. {
  2612. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2613. ERR_R_MALLOC_FAILURE);
  2614. goto psk_err;
  2615. }
  2616. if (s->session->psk_identity != NULL)
  2617. OPENSSL_free(s->session->psk_identity);
  2618. s->session->psk_identity = BUF_strdup(identity);
  2619. if (s->session->psk_identity == NULL)
  2620. {
  2621. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2622. ERR_R_MALLOC_FAILURE);
  2623. goto psk_err;
  2624. }
  2625. s->session->master_key_length =
  2626. s->method->ssl3_enc->generate_master_secret(s,
  2627. s->session->master_key,
  2628. psk_or_pre_ms, pre_ms_len);
  2629. n = strlen(identity);
  2630. s2n(n, p);
  2631. memcpy(p, identity, n);
  2632. n+=2;
  2633. psk_err = 0;
  2634. psk_err:
  2635. OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
  2636. OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
  2637. if (psk_err != 0)
  2638. {
  2639. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  2640. goto err;
  2641. }
  2642. }
  2643. #endif
  2644. else
  2645. {
  2646. ssl3_send_alert(s, SSL3_AL_FATAL,
  2647. SSL_AD_HANDSHAKE_FAILURE);
  2648. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2649. ERR_R_INTERNAL_ERROR);
  2650. goto err;
  2651. }
  2652. *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
  2653. l2n3(n,d);
  2654. s->state=SSL3_ST_CW_KEY_EXCH_B;
  2655. /* number of bytes to write */
  2656. s->init_num=n+4;
  2657. s->init_off=0;
  2658. }
  2659. /* SSL3_ST_CW_KEY_EXCH_B */
  2660. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2661. err:
  2662. #ifndef OPENSSL_NO_ECDH
  2663. BN_CTX_free(bn_ctx);
  2664. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  2665. if (clnt_ecdh != NULL)
  2666. EC_KEY_free(clnt_ecdh);
  2667. EVP_PKEY_free(srvr_pub_pkey);
  2668. #endif
  2669. return(-1);
  2670. }
  2671. int ssl3_send_client_verify(SSL *s)
  2672. {
  2673. unsigned char *p,*d;
  2674. unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  2675. EVP_PKEY *pkey;
  2676. EVP_PKEY_CTX *pctx=NULL;
  2677. EVP_MD_CTX mctx;
  2678. unsigned u=0;
  2679. unsigned long n;
  2680. int j;
  2681. EVP_MD_CTX_init(&mctx);
  2682. if (s->state == SSL3_ST_CW_CERT_VRFY_A)
  2683. {
  2684. d=(unsigned char *)s->init_buf->data;
  2685. p= &(d[4]);
  2686. pkey=s->cert->key->privatekey;
  2687. /* Create context from key and test if sha1 is allowed as digest */
  2688. pctx = EVP_PKEY_CTX_new(pkey,NULL);
  2689. EVP_PKEY_sign_init(pctx);
  2690. if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1())>0)
  2691. {
  2692. if (TLS1_get_version(s) < TLS1_2_VERSION)
  2693. s->method->ssl3_enc->cert_verify_mac(s,
  2694. NID_sha1,
  2695. &(data[MD5_DIGEST_LENGTH]));
  2696. }
  2697. else
  2698. {
  2699. ERR_clear_error();
  2700. }
  2701. /* For TLS v1.2 send signature algorithm and signature
  2702. * using agreed digest and cached handshake records.
  2703. */
  2704. if (TLS1_get_version(s) >= TLS1_2_VERSION)
  2705. {
  2706. long hdatalen = 0;
  2707. void *hdata;
  2708. const EVP_MD *md = s->cert->key->digest;
  2709. hdatalen = BIO_get_mem_data(s->s3->handshake_buffer,
  2710. &hdata);
  2711. if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md))
  2712. {
  2713. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
  2714. ERR_R_INTERNAL_ERROR);
  2715. goto err;
  2716. }
  2717. p += 2;
  2718. #ifdef SSL_DEBUG
  2719. fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
  2720. EVP_MD_name(md));
  2721. #endif
  2722. if (!EVP_SignInit_ex(&mctx, md, NULL)
  2723. || !EVP_SignUpdate(&mctx, hdata, hdatalen)
  2724. || !EVP_SignFinal(&mctx, p + 2, &u, pkey))
  2725. {
  2726. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
  2727. ERR_R_EVP_LIB);
  2728. goto err;
  2729. }
  2730. s2n(u,p);
  2731. n = u + 4;
  2732. if (!ssl3_digest_cached_records(s))
  2733. goto err;
  2734. }
  2735. else
  2736. #ifndef OPENSSL_NO_RSA
  2737. if (pkey->type == EVP_PKEY_RSA)
  2738. {
  2739. s->method->ssl3_enc->cert_verify_mac(s,
  2740. NID_md5,
  2741. &(data[0]));
  2742. if (RSA_sign(NID_md5_sha1, data,
  2743. MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
  2744. &(p[2]), &u, pkey->pkey.rsa) <= 0 )
  2745. {
  2746. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
  2747. goto err;
  2748. }
  2749. s2n(u,p);
  2750. n=u+2;
  2751. }
  2752. else
  2753. #endif
  2754. #ifndef OPENSSL_NO_DSA
  2755. if (pkey->type == EVP_PKEY_DSA)
  2756. {
  2757. if (!DSA_sign(pkey->save_type,
  2758. &(data[MD5_DIGEST_LENGTH]),
  2759. SHA_DIGEST_LENGTH,&(p[2]),
  2760. (unsigned int *)&j,pkey->pkey.dsa))
  2761. {
  2762. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
  2763. goto err;
  2764. }
  2765. s2n(j,p);
  2766. n=j+2;
  2767. }
  2768. else
  2769. #endif
  2770. #ifndef OPENSSL_NO_ECDSA
  2771. if (pkey->type == EVP_PKEY_EC)
  2772. {
  2773. if (!ECDSA_sign(pkey->save_type,
  2774. &(data[MD5_DIGEST_LENGTH]),
  2775. SHA_DIGEST_LENGTH,&(p[2]),
  2776. (unsigned int *)&j,pkey->pkey.ec))
  2777. {
  2778. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
  2779. ERR_R_ECDSA_LIB);
  2780. goto err;
  2781. }
  2782. s2n(j,p);
  2783. n=j+2;
  2784. }
  2785. else
  2786. #endif
  2787. if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_2001)
  2788. {
  2789. unsigned char signbuf[64];
  2790. int i;
  2791. size_t sigsize=64;
  2792. s->method->ssl3_enc->cert_verify_mac(s,
  2793. NID_id_GostR3411_94,
  2794. data);
  2795. if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
  2796. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
  2797. ERR_R_INTERNAL_ERROR);
  2798. goto err;
  2799. }
  2800. for (i=63,j=0; i>=0; j++, i--) {
  2801. p[2+j]=signbuf[i];
  2802. }
  2803. s2n(j,p);
  2804. n=j+2;
  2805. }
  2806. else
  2807. {
  2808. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
  2809. goto err;
  2810. }
  2811. *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
  2812. l2n3(n,d);
  2813. s->state=SSL3_ST_CW_CERT_VRFY_B;
  2814. s->init_num=(int)n+4;
  2815. s->init_off=0;
  2816. }
  2817. EVP_MD_CTX_cleanup(&mctx);
  2818. EVP_PKEY_CTX_free(pctx);
  2819. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2820. err:
  2821. EVP_MD_CTX_cleanup(&mctx);
  2822. EVP_PKEY_CTX_free(pctx);
  2823. return(-1);
  2824. }
  2825. int ssl3_send_client_certificate(SSL *s)
  2826. {
  2827. X509 *x509=NULL;
  2828. EVP_PKEY *pkey=NULL;
  2829. int i;
  2830. unsigned long l;
  2831. if (s->state == SSL3_ST_CW_CERT_A)
  2832. {
  2833. if ((s->cert == NULL) ||
  2834. (s->cert->key->x509 == NULL) ||
  2835. (s->cert->key->privatekey == NULL))
  2836. s->state=SSL3_ST_CW_CERT_B;
  2837. else
  2838. s->state=SSL3_ST_CW_CERT_C;
  2839. }
  2840. /* We need to get a client cert */
  2841. if (s->state == SSL3_ST_CW_CERT_B)
  2842. {
  2843. /* If we get an error, we need to
  2844. * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
  2845. * We then get retied later */
  2846. i=0;
  2847. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  2848. if (i < 0)
  2849. {
  2850. s->rwstate=SSL_X509_LOOKUP;
  2851. return(-1);
  2852. }
  2853. s->rwstate=SSL_NOTHING;
  2854. if ((i == 1) && (pkey != NULL) && (x509 != NULL))
  2855. {
  2856. s->state=SSL3_ST_CW_CERT_B;
  2857. if ( !SSL_use_certificate(s,x509) ||
  2858. !SSL_use_PrivateKey(s,pkey))
  2859. i=0;
  2860. }
  2861. else if (i == 1)
  2862. {
  2863. i=0;
  2864. SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  2865. }
  2866. if (x509 != NULL) X509_free(x509);
  2867. if (pkey != NULL) EVP_PKEY_free(pkey);
  2868. if (i == 0)
  2869. {
  2870. if (s->version == SSL3_VERSION)
  2871. {
  2872. s->s3->tmp.cert_req=0;
  2873. ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
  2874. return(1);
  2875. }
  2876. else
  2877. {
  2878. s->s3->tmp.cert_req=2;
  2879. }
  2880. }
  2881. /* Ok, we have a cert */
  2882. s->state=SSL3_ST_CW_CERT_C;
  2883. }
  2884. if (s->state == SSL3_ST_CW_CERT_C)
  2885. {
  2886. s->state=SSL3_ST_CW_CERT_D;
  2887. l=ssl3_output_cert_chain(s,
  2888. (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
  2889. s->init_num=(int)l;
  2890. s->init_off=0;
  2891. }
  2892. /* SSL3_ST_CW_CERT_D */
  2893. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2894. }
  2895. #define has_bits(i,m) (((i)&(m)) == (m))
  2896. int ssl3_check_cert_and_algorithm(SSL *s)
  2897. {
  2898. int i,idx;
  2899. long alg_k,alg_a;
  2900. EVP_PKEY *pkey=NULL;
  2901. SESS_CERT *sc;
  2902. #ifndef OPENSSL_NO_RSA
  2903. RSA *rsa;
  2904. #endif
  2905. #ifndef OPENSSL_NO_DH
  2906. DH *dh;
  2907. #endif
  2908. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  2909. alg_a=s->s3->tmp.new_cipher->algorithm_auth;
  2910. /* we don't have a certificate */
  2911. if ((alg_a & (SSL_aDH|SSL_aNULL|SSL_aKRB5)) || (alg_k & SSL_kPSK))
  2912. return(1);
  2913. sc=s->session->sess_cert;
  2914. if (sc == NULL)
  2915. {
  2916. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
  2917. goto err;
  2918. }
  2919. #ifndef OPENSSL_NO_RSA
  2920. rsa=s->session->sess_cert->peer_rsa_tmp;
  2921. #endif
  2922. #ifndef OPENSSL_NO_DH
  2923. dh=s->session->sess_cert->peer_dh_tmp;
  2924. #endif
  2925. /* This is the passed certificate */
  2926. idx=sc->peer_cert_type;
  2927. #ifndef OPENSSL_NO_ECDH
  2928. if (idx == SSL_PKEY_ECC)
  2929. {
  2930. if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
  2931. s) == 0)
  2932. { /* check failed */
  2933. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_BAD_ECC_CERT);
  2934. goto f_err;
  2935. }
  2936. else
  2937. {
  2938. return 1;
  2939. }
  2940. }
  2941. #endif
  2942. pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
  2943. i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
  2944. EVP_PKEY_free(pkey);
  2945. /* Check that we have a certificate if we require one */
  2946. if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
  2947. {
  2948. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
  2949. goto f_err;
  2950. }
  2951. #ifndef OPENSSL_NO_DSA
  2952. else if ((alg_a & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
  2953. {
  2954. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
  2955. goto f_err;
  2956. }
  2957. #endif
  2958. #ifndef OPENSSL_NO_RSA
  2959. if ((alg_k & SSL_kRSA) &&
  2960. !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
  2961. {
  2962. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  2963. goto f_err;
  2964. }
  2965. #endif
  2966. #ifndef OPENSSL_NO_DH
  2967. if ((alg_k & SSL_kEDH) &&
  2968. !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
  2969. {
  2970. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
  2971. goto f_err;
  2972. }
  2973. else if ((alg_k & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
  2974. {
  2975. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
  2976. goto f_err;
  2977. }
  2978. #ifndef OPENSSL_NO_DSA
  2979. else if ((alg_k & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
  2980. {
  2981. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
  2982. goto f_err;
  2983. }
  2984. #endif
  2985. #endif
  2986. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
  2987. {
  2988. #ifndef OPENSSL_NO_RSA
  2989. if (alg_k & SSL_kRSA)
  2990. {
  2991. if (rsa == NULL
  2992. || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
  2993. {
  2994. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
  2995. goto f_err;
  2996. }
  2997. }
  2998. else
  2999. #endif
  3000. #ifndef OPENSSL_NO_DH
  3001. if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  3002. {
  3003. if (dh == NULL
  3004. || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
  3005. {
  3006. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
  3007. goto f_err;
  3008. }
  3009. }
  3010. else
  3011. #endif
  3012. {
  3013. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  3014. goto f_err;
  3015. }
  3016. }
  3017. return(1);
  3018. f_err:
  3019. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  3020. err:
  3021. return(0);
  3022. }
  3023. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  3024. int ssl3_send_next_proto(SSL *s)
  3025. {
  3026. unsigned int len, padding_len;
  3027. unsigned char *d;
  3028. if (s->state == SSL3_ST_CW_NEXT_PROTO_A)
  3029. {
  3030. len = s->next_proto_negotiated_len;
  3031. padding_len = 32 - ((len + 2) % 32);
  3032. d = (unsigned char *)s->init_buf->data;
  3033. d[4] = len;
  3034. memcpy(d + 5, s->next_proto_negotiated, len);
  3035. d[5 + len] = padding_len;
  3036. memset(d + 6 + len, 0, padding_len);
  3037. *(d++)=SSL3_MT_NEXT_PROTO;
  3038. l2n3(2 + len + padding_len, d);
  3039. s->state = SSL3_ST_CW_NEXT_PROTO_B;
  3040. s->init_num = 4 + 2 + len + padding_len;
  3041. s->init_off = 0;
  3042. }
  3043. return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
  3044. }
  3045. #endif /* !OPENSSL_NO_TLSEXT && !OPENSSL_NO_NEXTPROTONEG */
  3046. /* Check to see if handshake is full or resumed. Usually this is just a
  3047. * case of checking to see if a cache hit has occurred. In the case of
  3048. * session tickets we have to check the next message to be sure.
  3049. */
  3050. #ifndef OPENSSL_NO_TLSEXT
  3051. int ssl3_check_finished(SSL *s)
  3052. {
  3053. int ok;
  3054. long n;
  3055. /* If we have no ticket it cannot be a resumed session. */
  3056. if (!s->session->tlsext_tick)
  3057. return 1;
  3058. /* this function is called when we really expect a Certificate
  3059. * message, so permit appropriate message length */
  3060. n=s->method->ssl_get_message(s,
  3061. SSL3_ST_CR_CERT_A,
  3062. SSL3_ST_CR_CERT_B,
  3063. -1,
  3064. s->max_cert_list,
  3065. &ok);
  3066. if (!ok) return((int)n);
  3067. s->s3->tmp.reuse_message = 1;
  3068. if ((s->s3->tmp.message_type == SSL3_MT_FINISHED)
  3069. || (s->s3->tmp.message_type == SSL3_MT_NEWSESSION_TICKET))
  3070. return 2;
  3071. return 1;
  3072. }
  3073. #endif
  3074. int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
  3075. {
  3076. int i = 0;
  3077. #ifndef OPENSSL_NO_ENGINE
  3078. if (s->ctx->client_cert_engine)
  3079. {
  3080. i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
  3081. SSL_get_client_CA_list(s),
  3082. px509, ppkey, NULL, NULL, NULL);
  3083. if (i != 0)
  3084. return i;
  3085. }
  3086. #endif
  3087. if (s->ctx->client_cert_cb)
  3088. i = s->ctx->client_cert_cb(s,px509,ppkey);
  3089. return i;
  3090. }