s3_clnt.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372
  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 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. if (ssl_fill_hello_random(s, 0, p, SSL3_RANDOM_SIZE) <= 0)
  617. goto err;
  618. /* Do the message type and length last */
  619. d=p= &(buf[4]);
  620. /* version indicates the negotiated version: for example from
  621. * an SSLv2/v3 compatible client hello). The client_version
  622. * field is the maximum version we permit and it is also
  623. * used in RSA encrypted premaster secrets. Some servers can
  624. * choke if we initially report a higher version then
  625. * renegotiate to a lower one in the premaster secret. This
  626. * didn't happen with TLS 1.0 as most servers supported it
  627. * but it can with TLS 1.1 or later if the server only supports
  628. * 1.0.
  629. *
  630. * Possible scenario with previous logic:
  631. * 1. Client hello indicates TLS 1.2
  632. * 2. Server hello says TLS 1.0
  633. * 3. RSA encrypted premaster secret uses 1.2.
  634. * 4. Handhaked proceeds using TLS 1.0.
  635. * 5. Server sends hello request to renegotiate.
  636. * 6. Client hello indicates TLS v1.0 as we now
  637. * know that is maximum server supports.
  638. * 7. Server chokes on RSA encrypted premaster secret
  639. * containing version 1.0.
  640. *
  641. * For interoperability it should be OK to always use the
  642. * maximum version we support in client hello and then rely
  643. * on the checking of version to ensure the servers isn't
  644. * being inconsistent: for example initially negotiating with
  645. * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
  646. * client_version in client hello and not resetting it to
  647. * the negotiated version.
  648. */
  649. #if 0
  650. *(p++)=s->version>>8;
  651. *(p++)=s->version&0xff;
  652. s->client_version=s->version;
  653. #else
  654. *(p++)=s->client_version>>8;
  655. *(p++)=s->client_version&0xff;
  656. #endif
  657. /* Random stuff */
  658. memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
  659. p+=SSL3_RANDOM_SIZE;
  660. /* Session ID */
  661. if (s->new_session)
  662. i=0;
  663. else
  664. i=s->session->session_id_length;
  665. *(p++)=i;
  666. if (i != 0)
  667. {
  668. if (i > (int)sizeof(s->session->session_id))
  669. {
  670. SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  671. goto err;
  672. }
  673. memcpy(p,s->session->session_id,i);
  674. p+=i;
  675. }
  676. /* Ciphers supported */
  677. i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0);
  678. if (i == 0)
  679. {
  680. SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
  681. goto err;
  682. }
  683. #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
  684. /* Some servers hang if client hello > 256 bytes
  685. * as hack workaround chop number of supported ciphers
  686. * to keep it well below this if we use TLS v1.2
  687. */
  688. if (TLS1_get_version(s) >= TLS1_2_VERSION
  689. && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
  690. i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
  691. #endif
  692. s2n(i,p);
  693. p+=i;
  694. /* COMPRESSION */
  695. #ifdef OPENSSL_NO_COMP
  696. *(p++)=1;
  697. #else
  698. if ((s->options & SSL_OP_NO_COMPRESSION)
  699. || !s->ctx->comp_methods)
  700. j=0;
  701. else
  702. j=sk_SSL_COMP_num(s->ctx->comp_methods);
  703. *(p++)=1+j;
  704. for (i=0; i<j; i++)
  705. {
  706. comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
  707. *(p++)=comp->id;
  708. }
  709. #endif
  710. *(p++)=0; /* Add the NULL method */
  711. #ifndef OPENSSL_NO_TLSEXT
  712. /* TLS extensions*/
  713. if (ssl_prepare_clienthello_tlsext(s) <= 0)
  714. {
  715. SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
  716. goto err;
  717. }
  718. if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
  719. {
  720. SSLerr(SSL_F_SSL3_CLIENT_HELLO,ERR_R_INTERNAL_ERROR);
  721. goto err;
  722. }
  723. #endif
  724. l=(p-d);
  725. d=buf;
  726. *(d++)=SSL3_MT_CLIENT_HELLO;
  727. l2n3(l,d);
  728. s->state=SSL3_ST_CW_CLNT_HELLO_B;
  729. /* number of bytes to write */
  730. s->init_num=p-buf;
  731. s->init_off=0;
  732. }
  733. /* SSL3_ST_CW_CLNT_HELLO_B */
  734. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  735. err:
  736. return(-1);
  737. }
  738. int ssl3_get_server_hello(SSL *s)
  739. {
  740. STACK_OF(SSL_CIPHER) *sk;
  741. const SSL_CIPHER *c;
  742. unsigned char *p,*d;
  743. int i,al,ok;
  744. unsigned int j;
  745. long n;
  746. #ifndef OPENSSL_NO_COMP
  747. SSL_COMP *comp;
  748. #endif
  749. n=s->method->ssl_get_message(s,
  750. SSL3_ST_CR_SRVR_HELLO_A,
  751. SSL3_ST_CR_SRVR_HELLO_B,
  752. -1,
  753. 20000, /* ?? */
  754. &ok);
  755. if (!ok) return((int)n);
  756. if ( SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER)
  757. {
  758. if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST)
  759. {
  760. if ( s->d1->send_cookie == 0)
  761. {
  762. s->s3->tmp.reuse_message = 1;
  763. return 1;
  764. }
  765. else /* already sent a cookie */
  766. {
  767. al=SSL_AD_UNEXPECTED_MESSAGE;
  768. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
  769. goto f_err;
  770. }
  771. }
  772. }
  773. if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO)
  774. {
  775. al=SSL_AD_UNEXPECTED_MESSAGE;
  776. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
  777. goto f_err;
  778. }
  779. d=p=(unsigned char *)s->init_msg;
  780. if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
  781. {
  782. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
  783. s->version=(s->version&0xff00)|p[1];
  784. al=SSL_AD_PROTOCOL_VERSION;
  785. goto f_err;
  786. }
  787. p+=2;
  788. /* load the server hello data */
  789. /* load the server random */
  790. memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
  791. p+=SSL3_RANDOM_SIZE;
  792. /* get the session-id */
  793. j= *(p++);
  794. if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE))
  795. {
  796. al=SSL_AD_ILLEGAL_PARAMETER;
  797. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_LONG);
  798. goto f_err;
  799. }
  800. #ifndef OPENSSL_NO_TLSEXT
  801. /* check if we want to resume the session based on external pre-shared secret */
  802. if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
  803. {
  804. SSL_CIPHER *pref_cipher=NULL;
  805. s->session->master_key_length=sizeof(s->session->master_key);
  806. if (s->tls_session_secret_cb(s, s->session->master_key,
  807. &s->session->master_key_length,
  808. NULL, &pref_cipher,
  809. s->tls_session_secret_cb_arg))
  810. {
  811. s->session->cipher = pref_cipher ?
  812. pref_cipher : ssl_get_cipher_by_char(s, p+j);
  813. }
  814. }
  815. #endif /* OPENSSL_NO_TLSEXT */
  816. if (j != 0 && j == s->session->session_id_length
  817. && memcmp(p,s->session->session_id,j) == 0)
  818. {
  819. if(s->sid_ctx_length != s->session->sid_ctx_length
  820. || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
  821. {
  822. /* actually a client application bug */
  823. al=SSL_AD_ILLEGAL_PARAMETER;
  824. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  825. goto f_err;
  826. }
  827. s->hit=1;
  828. }
  829. else /* a miss or crap from the other end */
  830. {
  831. /* If we were trying for session-id reuse, make a new
  832. * SSL_SESSION so we don't stuff up other people */
  833. s->hit=0;
  834. if (s->session->session_id_length > 0)
  835. {
  836. if (!ssl_get_new_session(s,0))
  837. {
  838. al=SSL_AD_INTERNAL_ERROR;
  839. goto f_err;
  840. }
  841. }
  842. s->session->session_id_length=j;
  843. memcpy(s->session->session_id,p,j); /* j could be 0 */
  844. }
  845. p+=j;
  846. c=ssl_get_cipher_by_char(s,p);
  847. if (c == NULL)
  848. {
  849. /* unknown cipher */
  850. al=SSL_AD_ILLEGAL_PARAMETER;
  851. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
  852. goto f_err;
  853. }
  854. /* TLS v1.2 only ciphersuites require v1.2 or later */
  855. if ((c->algorithm_ssl & SSL_TLSV1_2) &&
  856. (TLS1_get_version(s) < TLS1_2_VERSION))
  857. {
  858. al=SSL_AD_ILLEGAL_PARAMETER;
  859. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
  860. goto f_err;
  861. }
  862. p+=ssl_put_cipher_by_char(s,NULL,NULL);
  863. sk=ssl_get_ciphers_by_id(s);
  864. i=sk_SSL_CIPHER_find(sk,c);
  865. if (i < 0)
  866. {
  867. /* we did not say we would use this cipher */
  868. al=SSL_AD_ILLEGAL_PARAMETER;
  869. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
  870. goto f_err;
  871. }
  872. /* Depending on the session caching (internal/external), the cipher
  873. and/or cipher_id values may not be set. Make sure that
  874. cipher_id is set and use it for comparison. */
  875. if (s->session->cipher)
  876. s->session->cipher_id = s->session->cipher->id;
  877. if (s->hit && (s->session->cipher_id != c->id))
  878. {
  879. /* Workaround is now obsolete */
  880. #if 0
  881. if (!(s->options &
  882. SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
  883. #endif
  884. {
  885. al=SSL_AD_ILLEGAL_PARAMETER;
  886. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  887. goto f_err;
  888. }
  889. }
  890. s->s3->tmp.new_cipher=c;
  891. /* Don't digest cached records if TLS v1.2: we may need them for
  892. * client authentication.
  893. */
  894. if (TLS1_get_version(s) < TLS1_2_VERSION && !ssl3_digest_cached_records(s))
  895. {
  896. al = SSL_AD_INTERNAL_ERROR;
  897. goto f_err;
  898. }
  899. /* lets get the compression algorithm */
  900. /* COMPRESSION */
  901. #ifdef OPENSSL_NO_COMP
  902. if (*(p++) != 0)
  903. {
  904. al=SSL_AD_ILLEGAL_PARAMETER;
  905. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  906. goto f_err;
  907. }
  908. /* If compression is disabled we'd better not try to resume a session
  909. * using compression.
  910. */
  911. if (s->session->compress_meth != 0)
  912. {
  913. al=SSL_AD_INTERNAL_ERROR;
  914. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_INCONSISTENT_COMPRESSION);
  915. goto f_err;
  916. }
  917. #else
  918. j= *(p++);
  919. if (s->hit && j != s->session->compress_meth)
  920. {
  921. al=SSL_AD_ILLEGAL_PARAMETER;
  922. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
  923. goto f_err;
  924. }
  925. if (j == 0)
  926. comp=NULL;
  927. else if (s->options & SSL_OP_NO_COMPRESSION)
  928. {
  929. al=SSL_AD_ILLEGAL_PARAMETER;
  930. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_COMPRESSION_DISABLED);
  931. goto f_err;
  932. }
  933. else
  934. comp=ssl3_comp_find(s->ctx->comp_methods,j);
  935. if ((j != 0) && (comp == NULL))
  936. {
  937. al=SSL_AD_ILLEGAL_PARAMETER;
  938. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  939. goto f_err;
  940. }
  941. else
  942. {
  943. s->s3->tmp.new_compression=comp;
  944. }
  945. #endif
  946. #ifndef OPENSSL_NO_TLSEXT
  947. /* TLS extensions*/
  948. if (s->version >= SSL3_VERSION)
  949. {
  950. if (!ssl_parse_serverhello_tlsext(s,&p,d,n, &al))
  951. {
  952. /* 'al' set by ssl_parse_serverhello_tlsext */
  953. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_PARSE_TLSEXT);
  954. goto f_err;
  955. }
  956. if (ssl_check_serverhello_tlsext(s) <= 0)
  957. {
  958. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SERVERHELLO_TLSEXT);
  959. goto err;
  960. }
  961. }
  962. #endif
  963. if (p != (d+n))
  964. {
  965. /* wrong packet length */
  966. al=SSL_AD_DECODE_ERROR;
  967. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
  968. goto f_err;
  969. }
  970. return(1);
  971. f_err:
  972. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  973. err:
  974. return(-1);
  975. }
  976. int ssl3_get_server_certificate(SSL *s)
  977. {
  978. int al,i,ok,ret= -1;
  979. unsigned long n,nc,llen,l;
  980. X509 *x=NULL;
  981. const unsigned char *q,*p;
  982. unsigned char *d;
  983. STACK_OF(X509) *sk=NULL;
  984. SESS_CERT *sc;
  985. EVP_PKEY *pkey=NULL;
  986. int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
  987. n=s->method->ssl_get_message(s,
  988. SSL3_ST_CR_CERT_A,
  989. SSL3_ST_CR_CERT_B,
  990. -1,
  991. s->max_cert_list,
  992. &ok);
  993. if (!ok) return((int)n);
  994. if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
  995. ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) &&
  996. (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)))
  997. {
  998. s->s3->tmp.reuse_message=1;
  999. return(1);
  1000. }
  1001. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
  1002. {
  1003. al=SSL_AD_UNEXPECTED_MESSAGE;
  1004. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
  1005. goto f_err;
  1006. }
  1007. p=d=(unsigned char *)s->init_msg;
  1008. if ((sk=sk_X509_new_null()) == NULL)
  1009. {
  1010. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  1011. goto err;
  1012. }
  1013. n2l3(p,llen);
  1014. if (llen+3 != n)
  1015. {
  1016. al=SSL_AD_DECODE_ERROR;
  1017. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
  1018. goto f_err;
  1019. }
  1020. for (nc=0; nc<llen; )
  1021. {
  1022. n2l3(p,l);
  1023. if ((l+nc+3) > llen)
  1024. {
  1025. al=SSL_AD_DECODE_ERROR;
  1026. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  1027. goto f_err;
  1028. }
  1029. q=p;
  1030. x=d2i_X509(NULL,&q,l);
  1031. if (x == NULL)
  1032. {
  1033. al=SSL_AD_BAD_CERTIFICATE;
  1034. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
  1035. goto f_err;
  1036. }
  1037. if (q != (p+l))
  1038. {
  1039. al=SSL_AD_DECODE_ERROR;
  1040. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  1041. goto f_err;
  1042. }
  1043. if (!sk_X509_push(sk,x))
  1044. {
  1045. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  1046. goto err;
  1047. }
  1048. x=NULL;
  1049. nc+=l+3;
  1050. p=q;
  1051. }
  1052. i=ssl_verify_cert_chain(s,sk);
  1053. if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
  1054. #ifndef OPENSSL_NO_KRB5
  1055. && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
  1056. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
  1057. #endif /* OPENSSL_NO_KRB5 */
  1058. )
  1059. {
  1060. al=ssl_verify_alarm_type(s->verify_result);
  1061. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
  1062. goto f_err;
  1063. }
  1064. ERR_clear_error(); /* but we keep s->verify_result */
  1065. sc=ssl_sess_cert_new();
  1066. if (sc == NULL) goto err;
  1067. if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
  1068. s->session->sess_cert=sc;
  1069. sc->cert_chain=sk;
  1070. /* Inconsistency alert: cert_chain does include the peer's
  1071. * certificate, which we don't include in s3_srvr.c */
  1072. x=sk_X509_value(sk,0);
  1073. sk=NULL;
  1074. /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
  1075. pkey=X509_get_pubkey(x);
  1076. /* VRS: allow null cert if auth == KRB5 */
  1077. need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
  1078. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
  1079. ? 0 : 1;
  1080. #ifdef KSSL_DEBUG
  1081. printf("pkey,x = %p, %p\n", pkey,x);
  1082. printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
  1083. printf("cipher, alg, nc = %s, %lx, %lx, %d\n", s->s3->tmp.new_cipher->name,
  1084. s->s3->tmp.new_cipher->algorithm_mkey, s->s3->tmp.new_cipher->algorithm_auth, need_cert);
  1085. #endif /* KSSL_DEBUG */
  1086. if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
  1087. {
  1088. x=NULL;
  1089. al=SSL3_AL_FATAL;
  1090. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  1091. SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
  1092. goto f_err;
  1093. }
  1094. i=ssl_cert_type(x,pkey);
  1095. if (need_cert && i < 0)
  1096. {
  1097. x=NULL;
  1098. al=SSL3_AL_FATAL;
  1099. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  1100. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  1101. goto f_err;
  1102. }
  1103. if (need_cert)
  1104. {
  1105. sc->peer_cert_type=i;
  1106. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  1107. /* Why would the following ever happen?
  1108. * We just created sc a couple of lines ago. */
  1109. if (sc->peer_pkeys[i].x509 != NULL)
  1110. X509_free(sc->peer_pkeys[i].x509);
  1111. sc->peer_pkeys[i].x509=x;
  1112. sc->peer_key= &(sc->peer_pkeys[i]);
  1113. if (s->session->peer != NULL)
  1114. X509_free(s->session->peer);
  1115. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  1116. s->session->peer=x;
  1117. }
  1118. else
  1119. {
  1120. sc->peer_cert_type=i;
  1121. sc->peer_key= NULL;
  1122. if (s->session->peer != NULL)
  1123. X509_free(s->session->peer);
  1124. s->session->peer=NULL;
  1125. }
  1126. s->session->verify_result = s->verify_result;
  1127. x=NULL;
  1128. ret=1;
  1129. if (0)
  1130. {
  1131. f_err:
  1132. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1133. }
  1134. err:
  1135. EVP_PKEY_free(pkey);
  1136. X509_free(x);
  1137. sk_X509_pop_free(sk,X509_free);
  1138. return(ret);
  1139. }
  1140. int ssl3_get_key_exchange(SSL *s)
  1141. {
  1142. #ifndef OPENSSL_NO_RSA
  1143. unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
  1144. #endif
  1145. EVP_MD_CTX md_ctx;
  1146. unsigned char *param,*p;
  1147. int al,i,j,param_len,ok;
  1148. long n,alg_k,alg_a;
  1149. EVP_PKEY *pkey=NULL;
  1150. const EVP_MD *md = NULL;
  1151. #ifndef OPENSSL_NO_RSA
  1152. RSA *rsa=NULL;
  1153. #endif
  1154. #ifndef OPENSSL_NO_DH
  1155. DH *dh=NULL;
  1156. #endif
  1157. #ifndef OPENSSL_NO_ECDH
  1158. EC_KEY *ecdh = NULL;
  1159. BN_CTX *bn_ctx = NULL;
  1160. EC_POINT *srvr_ecpoint = NULL;
  1161. int curve_nid = 0;
  1162. int encoded_pt_len = 0;
  1163. #endif
  1164. /* use same message size as in ssl3_get_certificate_request()
  1165. * as ServerKeyExchange message may be skipped */
  1166. n=s->method->ssl_get_message(s,
  1167. SSL3_ST_CR_KEY_EXCH_A,
  1168. SSL3_ST_CR_KEY_EXCH_B,
  1169. -1,
  1170. s->max_cert_list,
  1171. &ok);
  1172. if (!ok) return((int)n);
  1173. if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
  1174. {
  1175. #ifndef OPENSSL_NO_PSK
  1176. /* In plain PSK ciphersuite, ServerKeyExchange can be
  1177. omitted if no identity hint is sent. Set
  1178. session->sess_cert anyway to avoid problems
  1179. later.*/
  1180. if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)
  1181. {
  1182. s->session->sess_cert=ssl_sess_cert_new();
  1183. if (s->ctx->psk_identity_hint)
  1184. OPENSSL_free(s->ctx->psk_identity_hint);
  1185. s->ctx->psk_identity_hint = NULL;
  1186. }
  1187. #endif
  1188. s->s3->tmp.reuse_message=1;
  1189. return(1);
  1190. }
  1191. param=p=(unsigned char *)s->init_msg;
  1192. if (s->session->sess_cert != NULL)
  1193. {
  1194. #ifndef OPENSSL_NO_RSA
  1195. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  1196. {
  1197. RSA_free(s->session->sess_cert->peer_rsa_tmp);
  1198. s->session->sess_cert->peer_rsa_tmp=NULL;
  1199. }
  1200. #endif
  1201. #ifndef OPENSSL_NO_DH
  1202. if (s->session->sess_cert->peer_dh_tmp)
  1203. {
  1204. DH_free(s->session->sess_cert->peer_dh_tmp);
  1205. s->session->sess_cert->peer_dh_tmp=NULL;
  1206. }
  1207. #endif
  1208. #ifndef OPENSSL_NO_ECDH
  1209. if (s->session->sess_cert->peer_ecdh_tmp)
  1210. {
  1211. EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
  1212. s->session->sess_cert->peer_ecdh_tmp=NULL;
  1213. }
  1214. #endif
  1215. }
  1216. else
  1217. {
  1218. s->session->sess_cert=ssl_sess_cert_new();
  1219. }
  1220. param_len=0;
  1221. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  1222. alg_a=s->s3->tmp.new_cipher->algorithm_auth;
  1223. EVP_MD_CTX_init(&md_ctx);
  1224. #ifndef OPENSSL_NO_PSK
  1225. if (alg_k & SSL_kPSK)
  1226. {
  1227. char tmp_id_hint[PSK_MAX_IDENTITY_LEN+1];
  1228. al=SSL_AD_HANDSHAKE_FAILURE;
  1229. n2s(p,i);
  1230. param_len=i+2;
  1231. /* Store PSK identity hint for later use, hint is used
  1232. * in ssl3_send_client_key_exchange. Assume that the
  1233. * maximum length of a PSK identity hint can be as
  1234. * long as the maximum length of a PSK identity. */
  1235. if (i > PSK_MAX_IDENTITY_LEN)
  1236. {
  1237. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1238. SSL_R_DATA_LENGTH_TOO_LONG);
  1239. goto f_err;
  1240. }
  1241. if (param_len > n)
  1242. {
  1243. al=SSL_AD_DECODE_ERROR;
  1244. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1245. SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
  1246. goto f_err;
  1247. }
  1248. /* If received PSK identity hint contains NULL
  1249. * characters, the hint is truncated from the first
  1250. * NULL. p may not be ending with NULL, so create a
  1251. * NULL-terminated string. */
  1252. memcpy(tmp_id_hint, p, i);
  1253. memset(tmp_id_hint+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
  1254. if (s->ctx->psk_identity_hint != NULL)
  1255. OPENSSL_free(s->ctx->psk_identity_hint);
  1256. s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
  1257. if (s->ctx->psk_identity_hint == NULL)
  1258. {
  1259. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1260. goto f_err;
  1261. }
  1262. p+=i;
  1263. n-=param_len;
  1264. }
  1265. else
  1266. #endif /* !OPENSSL_NO_PSK */
  1267. #ifndef OPENSSL_NO_SRP
  1268. if (alg_k & SSL_kSRP)
  1269. {
  1270. n2s(p,i);
  1271. param_len=i+2;
  1272. if (param_len > n)
  1273. {
  1274. al=SSL_AD_DECODE_ERROR;
  1275. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SRP_N_LENGTH);
  1276. goto f_err;
  1277. }
  1278. if (!(s->srp_ctx.N=BN_bin2bn(p,i,NULL)))
  1279. {
  1280. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1281. goto err;
  1282. }
  1283. p+=i;
  1284. n2s(p,i);
  1285. param_len+=i+2;
  1286. if (param_len > n)
  1287. {
  1288. al=SSL_AD_DECODE_ERROR;
  1289. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SRP_G_LENGTH);
  1290. goto f_err;
  1291. }
  1292. if (!(s->srp_ctx.g=BN_bin2bn(p,i,NULL)))
  1293. {
  1294. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1295. goto err;
  1296. }
  1297. p+=i;
  1298. i = (unsigned int)(p[0]);
  1299. p++;
  1300. param_len+=i+1;
  1301. if (param_len > n)
  1302. {
  1303. al=SSL_AD_DECODE_ERROR;
  1304. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SRP_S_LENGTH);
  1305. goto f_err;
  1306. }
  1307. if (!(s->srp_ctx.s=BN_bin2bn(p,i,NULL)))
  1308. {
  1309. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1310. goto err;
  1311. }
  1312. p+=i;
  1313. n2s(p,i);
  1314. param_len+=i+2;
  1315. if (param_len > n)
  1316. {
  1317. al=SSL_AD_DECODE_ERROR;
  1318. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SRP_B_LENGTH);
  1319. goto f_err;
  1320. }
  1321. if (!(s->srp_ctx.B=BN_bin2bn(p,i,NULL)))
  1322. {
  1323. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1324. goto err;
  1325. }
  1326. p+=i;
  1327. n-=param_len;
  1328. /* We must check if there is a certificate */
  1329. #ifndef OPENSSL_NO_RSA
  1330. if (alg_a & SSL_aRSA)
  1331. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1332. #else
  1333. if (0)
  1334. ;
  1335. #endif
  1336. #ifndef OPENSSL_NO_DSA
  1337. else if (alg_a & SSL_aDSS)
  1338. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
  1339. #endif
  1340. }
  1341. else
  1342. #endif /* !OPENSSL_NO_SRP */
  1343. #ifndef OPENSSL_NO_RSA
  1344. if (alg_k & SSL_kRSA)
  1345. {
  1346. if ((rsa=RSA_new()) == NULL)
  1347. {
  1348. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1349. goto err;
  1350. }
  1351. n2s(p,i);
  1352. param_len=i+2;
  1353. if (param_len > n)
  1354. {
  1355. al=SSL_AD_DECODE_ERROR;
  1356. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
  1357. goto f_err;
  1358. }
  1359. if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
  1360. {
  1361. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1362. goto err;
  1363. }
  1364. p+=i;
  1365. n2s(p,i);
  1366. param_len+=i+2;
  1367. if (param_len > n)
  1368. {
  1369. al=SSL_AD_DECODE_ERROR;
  1370. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
  1371. goto f_err;
  1372. }
  1373. if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
  1374. {
  1375. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1376. goto err;
  1377. }
  1378. p+=i;
  1379. n-=param_len;
  1380. /* this should be because we are using an export cipher */
  1381. if (alg_a & SSL_aRSA)
  1382. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1383. else
  1384. {
  1385. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1386. goto err;
  1387. }
  1388. s->session->sess_cert->peer_rsa_tmp=rsa;
  1389. rsa=NULL;
  1390. }
  1391. #else /* OPENSSL_NO_RSA */
  1392. if (0)
  1393. ;
  1394. #endif
  1395. #ifndef OPENSSL_NO_DH
  1396. else if (alg_k & SSL_kEDH)
  1397. {
  1398. if ((dh=DH_new()) == NULL)
  1399. {
  1400. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
  1401. goto err;
  1402. }
  1403. n2s(p,i);
  1404. param_len=i+2;
  1405. if (param_len > n)
  1406. {
  1407. al=SSL_AD_DECODE_ERROR;
  1408. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
  1409. goto f_err;
  1410. }
  1411. if (!(dh->p=BN_bin2bn(p,i,NULL)))
  1412. {
  1413. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1414. goto err;
  1415. }
  1416. p+=i;
  1417. n2s(p,i);
  1418. param_len+=i+2;
  1419. if (param_len > n)
  1420. {
  1421. al=SSL_AD_DECODE_ERROR;
  1422. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
  1423. goto f_err;
  1424. }
  1425. if (!(dh->g=BN_bin2bn(p,i,NULL)))
  1426. {
  1427. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1428. goto err;
  1429. }
  1430. p+=i;
  1431. n2s(p,i);
  1432. param_len+=i+2;
  1433. if (param_len > n)
  1434. {
  1435. al=SSL_AD_DECODE_ERROR;
  1436. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
  1437. goto f_err;
  1438. }
  1439. if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
  1440. {
  1441. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1442. goto err;
  1443. }
  1444. p+=i;
  1445. n-=param_len;
  1446. #ifndef OPENSSL_NO_RSA
  1447. if (alg_a & SSL_aRSA)
  1448. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1449. #else
  1450. if (0)
  1451. ;
  1452. #endif
  1453. #ifndef OPENSSL_NO_DSA
  1454. else if (alg_a & SSL_aDSS)
  1455. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
  1456. #endif
  1457. /* else anonymous DH, so no certificate or pkey. */
  1458. s->session->sess_cert->peer_dh_tmp=dh;
  1459. dh=NULL;
  1460. }
  1461. else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd))
  1462. {
  1463. al=SSL_AD_ILLEGAL_PARAMETER;
  1464. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
  1465. goto f_err;
  1466. }
  1467. #endif /* !OPENSSL_NO_DH */
  1468. #ifndef OPENSSL_NO_ECDH
  1469. else if (alg_k & SSL_kEECDH)
  1470. {
  1471. EC_GROUP *ngroup;
  1472. const EC_GROUP *group;
  1473. if ((ecdh=EC_KEY_new()) == NULL)
  1474. {
  1475. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1476. goto err;
  1477. }
  1478. /* Extract elliptic curve parameters and the
  1479. * server's ephemeral ECDH public key.
  1480. * Keep accumulating lengths of various components in
  1481. * param_len and make sure it never exceeds n.
  1482. */
  1483. /* XXX: For now we only support named (not generic) curves
  1484. * and the ECParameters in this case is just three bytes.
  1485. */
  1486. param_len=3;
  1487. if ((param_len > n) ||
  1488. (*p != NAMED_CURVE_TYPE) ||
  1489. ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0))
  1490. {
  1491. al=SSL_AD_INTERNAL_ERROR;
  1492. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  1493. goto f_err;
  1494. }
  1495. ngroup = EC_GROUP_new_by_curve_name(curve_nid);
  1496. if (ngroup == NULL)
  1497. {
  1498. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
  1499. goto err;
  1500. }
  1501. if (EC_KEY_set_group(ecdh, ngroup) == 0)
  1502. {
  1503. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
  1504. goto err;
  1505. }
  1506. EC_GROUP_free(ngroup);
  1507. group = EC_KEY_get0_group(ecdh);
  1508. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  1509. (EC_GROUP_get_degree(group) > 163))
  1510. {
  1511. al=SSL_AD_EXPORT_RESTRICTION;
  1512. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
  1513. goto f_err;
  1514. }
  1515. p+=3;
  1516. /* Next, get the encoded ECPoint */
  1517. if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
  1518. ((bn_ctx = BN_CTX_new()) == NULL))
  1519. {
  1520. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1521. goto err;
  1522. }
  1523. encoded_pt_len = *p; /* length of encoded point */
  1524. p+=1;
  1525. param_len += (1 + encoded_pt_len);
  1526. if ((param_len > n) ||
  1527. (EC_POINT_oct2point(group, srvr_ecpoint,
  1528. p, encoded_pt_len, bn_ctx) == 0))
  1529. {
  1530. al=SSL_AD_DECODE_ERROR;
  1531. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_ECPOINT);
  1532. goto f_err;
  1533. }
  1534. n-=param_len;
  1535. p+=encoded_pt_len;
  1536. /* The ECC/TLS specification does not mention
  1537. * the use of DSA to sign ECParameters in the server
  1538. * key exchange message. We do support RSA and ECDSA.
  1539. */
  1540. if (0) ;
  1541. #ifndef OPENSSL_NO_RSA
  1542. else if (alg_a & SSL_aRSA)
  1543. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1544. #endif
  1545. #ifndef OPENSSL_NO_ECDSA
  1546. else if (alg_a & SSL_aECDSA)
  1547. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  1548. #endif
  1549. /* else anonymous ECDH, so no certificate or pkey. */
  1550. EC_KEY_set_public_key(ecdh, srvr_ecpoint);
  1551. s->session->sess_cert->peer_ecdh_tmp=ecdh;
  1552. ecdh=NULL;
  1553. BN_CTX_free(bn_ctx);
  1554. bn_ctx = NULL;
  1555. EC_POINT_free(srvr_ecpoint);
  1556. srvr_ecpoint = NULL;
  1557. }
  1558. else if (alg_k)
  1559. {
  1560. al=SSL_AD_UNEXPECTED_MESSAGE;
  1561. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
  1562. goto f_err;
  1563. }
  1564. #endif /* !OPENSSL_NO_ECDH */
  1565. /* p points to the next byte, there are 'n' bytes left */
  1566. /* if it was signed, check the signature */
  1567. if (pkey != NULL)
  1568. {
  1569. if (TLS1_get_version(s) >= TLS1_2_VERSION)
  1570. {
  1571. int sigalg = tls12_get_sigid(pkey);
  1572. /* Should never happen */
  1573. if (sigalg == -1)
  1574. {
  1575. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1576. goto err;
  1577. }
  1578. /* Check key type is consistent with signature */
  1579. if (sigalg != (int)p[1])
  1580. {
  1581. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_TYPE);
  1582. al=SSL_AD_DECODE_ERROR;
  1583. goto f_err;
  1584. }
  1585. md = tls12_get_hash(p[0]);
  1586. if (md == NULL)
  1587. {
  1588. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNKNOWN_DIGEST);
  1589. al=SSL_AD_DECODE_ERROR;
  1590. goto f_err;
  1591. }
  1592. #ifdef SSL_DEBUG
  1593. fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
  1594. #endif
  1595. p += 2;
  1596. n -= 2;
  1597. }
  1598. else
  1599. md = EVP_sha1();
  1600. n2s(p,i);
  1601. n-=2;
  1602. j=EVP_PKEY_size(pkey);
  1603. if ((i != n) || (n > j) || (n <= 0))
  1604. {
  1605. /* wrong packet length */
  1606. al=SSL_AD_DECODE_ERROR;
  1607. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
  1608. goto f_err;
  1609. }
  1610. #ifndef OPENSSL_NO_RSA
  1611. if (pkey->type == EVP_PKEY_RSA && TLS1_get_version(s) < TLS1_2_VERSION)
  1612. {
  1613. int num;
  1614. j=0;
  1615. q=md_buf;
  1616. for (num=2; num > 0; num--)
  1617. {
  1618. EVP_MD_CTX_set_flags(&md_ctx,
  1619. EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  1620. EVP_DigestInit_ex(&md_ctx,(num == 2)
  1621. ?s->ctx->md5:s->ctx->sha1, NULL);
  1622. EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1623. EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1624. EVP_DigestUpdate(&md_ctx,param,param_len);
  1625. EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i);
  1626. q+=i;
  1627. j+=i;
  1628. }
  1629. i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
  1630. pkey->pkey.rsa);
  1631. if (i < 0)
  1632. {
  1633. al=SSL_AD_DECRYPT_ERROR;
  1634. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
  1635. goto f_err;
  1636. }
  1637. if (i == 0)
  1638. {
  1639. /* bad signature */
  1640. al=SSL_AD_DECRYPT_ERROR;
  1641. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
  1642. goto f_err;
  1643. }
  1644. }
  1645. else
  1646. #endif
  1647. {
  1648. EVP_VerifyInit_ex(&md_ctx, md, NULL);
  1649. EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1650. EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1651. EVP_VerifyUpdate(&md_ctx,param,param_len);
  1652. if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0)
  1653. {
  1654. /* bad signature */
  1655. al=SSL_AD_DECRYPT_ERROR;
  1656. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
  1657. goto f_err;
  1658. }
  1659. }
  1660. }
  1661. else
  1662. {
  1663. if (!(alg_a & SSL_aNULL) && !(alg_k & SSL_kPSK))
  1664. /* aNULL or kPSK do not need public keys */
  1665. {
  1666. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1667. goto err;
  1668. }
  1669. /* still data left over */
  1670. if (n != 0)
  1671. {
  1672. al=SSL_AD_DECODE_ERROR;
  1673. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
  1674. goto f_err;
  1675. }
  1676. }
  1677. EVP_PKEY_free(pkey);
  1678. EVP_MD_CTX_cleanup(&md_ctx);
  1679. return(1);
  1680. f_err:
  1681. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1682. err:
  1683. EVP_PKEY_free(pkey);
  1684. #ifndef OPENSSL_NO_RSA
  1685. if (rsa != NULL)
  1686. RSA_free(rsa);
  1687. #endif
  1688. #ifndef OPENSSL_NO_DH
  1689. if (dh != NULL)
  1690. DH_free(dh);
  1691. #endif
  1692. #ifndef OPENSSL_NO_ECDH
  1693. BN_CTX_free(bn_ctx);
  1694. EC_POINT_free(srvr_ecpoint);
  1695. if (ecdh != NULL)
  1696. EC_KEY_free(ecdh);
  1697. #endif
  1698. EVP_MD_CTX_cleanup(&md_ctx);
  1699. return(-1);
  1700. }
  1701. int ssl3_get_certificate_request(SSL *s)
  1702. {
  1703. int ok,ret=0;
  1704. unsigned long n,nc,l;
  1705. unsigned int llen, ctype_num,i;
  1706. X509_NAME *xn=NULL;
  1707. const unsigned char *p,*q;
  1708. unsigned char *d;
  1709. STACK_OF(X509_NAME) *ca_sk=NULL;
  1710. n=s->method->ssl_get_message(s,
  1711. SSL3_ST_CR_CERT_REQ_A,
  1712. SSL3_ST_CR_CERT_REQ_B,
  1713. -1,
  1714. s->max_cert_list,
  1715. &ok);
  1716. if (!ok) return((int)n);
  1717. s->s3->tmp.cert_req=0;
  1718. if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
  1719. {
  1720. s->s3->tmp.reuse_message=1;
  1721. /* If we get here we don't need any cached handshake records
  1722. * as we wont be doing client auth.
  1723. */
  1724. if (s->s3->handshake_buffer)
  1725. {
  1726. if (!ssl3_digest_cached_records(s))
  1727. goto err;
  1728. }
  1729. return(1);
  1730. }
  1731. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
  1732. {
  1733. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  1734. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
  1735. goto err;
  1736. }
  1737. /* TLS does not like anon-DH with client cert */
  1738. if (s->version > SSL3_VERSION)
  1739. {
  1740. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
  1741. {
  1742. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  1743. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
  1744. goto err;
  1745. }
  1746. }
  1747. p=d=(unsigned char *)s->init_msg;
  1748. if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
  1749. {
  1750. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
  1751. goto err;
  1752. }
  1753. /* get the certificate types */
  1754. ctype_num= *(p++);
  1755. if (ctype_num > SSL3_CT_NUMBER)
  1756. ctype_num=SSL3_CT_NUMBER;
  1757. for (i=0; i<ctype_num; i++)
  1758. s->s3->tmp.ctype[i]= p[i];
  1759. p+=ctype_num;
  1760. if (TLS1_get_version(s) >= TLS1_2_VERSION)
  1761. {
  1762. n2s(p, llen);
  1763. /* Check we have enough room for signature algorithms and
  1764. * following length value.
  1765. */
  1766. if ((unsigned long)(p - d + llen + 2) > n)
  1767. {
  1768. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1769. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_DATA_LENGTH_TOO_LONG);
  1770. goto err;
  1771. }
  1772. if ((llen & 1) || !tls1_process_sigalgs(s, p, llen))
  1773. {
  1774. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1775. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_SIGNATURE_ALGORITHMS_ERROR);
  1776. goto err;
  1777. }
  1778. p += llen;
  1779. }
  1780. /* get the CA RDNs */
  1781. n2s(p,llen);
  1782. #if 0
  1783. {
  1784. FILE *out;
  1785. out=fopen("/tmp/vsign.der","w");
  1786. fwrite(p,1,llen,out);
  1787. fclose(out);
  1788. }
  1789. #endif
  1790. if ((unsigned long)(p - d + llen) != n)
  1791. {
  1792. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1793. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
  1794. goto err;
  1795. }
  1796. for (nc=0; nc<llen; )
  1797. {
  1798. n2s(p,l);
  1799. if ((l+nc+2) > llen)
  1800. {
  1801. if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  1802. goto cont; /* netscape bugs */
  1803. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1804. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
  1805. goto err;
  1806. }
  1807. q=p;
  1808. if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
  1809. {
  1810. /* If netscape tolerance is on, ignore errors */
  1811. if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
  1812. goto cont;
  1813. else
  1814. {
  1815. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1816. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
  1817. goto err;
  1818. }
  1819. }
  1820. if (q != (p+l))
  1821. {
  1822. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1823. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
  1824. goto err;
  1825. }
  1826. if (!sk_X509_NAME_push(ca_sk,xn))
  1827. {
  1828. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
  1829. goto err;
  1830. }
  1831. p+=l;
  1832. nc+=l+2;
  1833. }
  1834. if (0)
  1835. {
  1836. cont:
  1837. ERR_clear_error();
  1838. }
  1839. /* we should setup a certificate to return.... */
  1840. s->s3->tmp.cert_req=1;
  1841. s->s3->tmp.ctype_num=ctype_num;
  1842. if (s->s3->tmp.ca_names != NULL)
  1843. sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
  1844. s->s3->tmp.ca_names=ca_sk;
  1845. ca_sk=NULL;
  1846. ret=1;
  1847. err:
  1848. if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
  1849. return(ret);
  1850. }
  1851. static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
  1852. {
  1853. return(X509_NAME_cmp(*a,*b));
  1854. }
  1855. #ifndef OPENSSL_NO_TLSEXT
  1856. int ssl3_get_new_session_ticket(SSL *s)
  1857. {
  1858. int ok,al,ret=0, ticklen;
  1859. long n;
  1860. const unsigned char *p;
  1861. unsigned char *d;
  1862. n=s->method->ssl_get_message(s,
  1863. SSL3_ST_CR_SESSION_TICKET_A,
  1864. SSL3_ST_CR_SESSION_TICKET_B,
  1865. -1,
  1866. 16384,
  1867. &ok);
  1868. if (!ok)
  1869. return((int)n);
  1870. if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
  1871. {
  1872. s->s3->tmp.reuse_message=1;
  1873. return(1);
  1874. }
  1875. if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET)
  1876. {
  1877. al=SSL_AD_UNEXPECTED_MESSAGE;
  1878. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_BAD_MESSAGE_TYPE);
  1879. goto f_err;
  1880. }
  1881. if (n < 6)
  1882. {
  1883. /* need at least ticket_lifetime_hint + ticket length */
  1884. al = SSL_AD_DECODE_ERROR;
  1885. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH);
  1886. goto f_err;
  1887. }
  1888. p=d=(unsigned char *)s->init_msg;
  1889. n2l(p, s->session->tlsext_tick_lifetime_hint);
  1890. n2s(p, ticklen);
  1891. /* ticket_lifetime_hint + ticket_length + ticket */
  1892. if (ticklen + 6 != n)
  1893. {
  1894. al = SSL_AD_DECODE_ERROR;
  1895. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH);
  1896. goto f_err;
  1897. }
  1898. if (s->session->tlsext_tick)
  1899. {
  1900. OPENSSL_free(s->session->tlsext_tick);
  1901. s->session->tlsext_ticklen = 0;
  1902. }
  1903. s->session->tlsext_tick = OPENSSL_malloc(ticklen);
  1904. if (!s->session->tlsext_tick)
  1905. {
  1906. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,ERR_R_MALLOC_FAILURE);
  1907. goto err;
  1908. }
  1909. memcpy(s->session->tlsext_tick, p, ticklen);
  1910. s->session->tlsext_ticklen = ticklen;
  1911. /* There are two ways to detect a resumed ticket sesion.
  1912. * One is to set an appropriate session ID and then the server
  1913. * must return a match in ServerHello. This allows the normal
  1914. * client session ID matching to work and we know much
  1915. * earlier that the ticket has been accepted.
  1916. *
  1917. * The other way is to set zero length session ID when the
  1918. * ticket is presented and rely on the handshake to determine
  1919. * session resumption.
  1920. *
  1921. * We choose the former approach because this fits in with
  1922. * assumptions elsewhere in OpenSSL. The session ID is set
  1923. * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
  1924. * ticket.
  1925. */
  1926. EVP_Digest(p, ticklen,
  1927. s->session->session_id, &s->session->session_id_length,
  1928. #ifndef OPENSSL_NO_SHA256
  1929. EVP_sha256(), NULL);
  1930. #else
  1931. EVP_sha1(), NULL);
  1932. #endif
  1933. ret=1;
  1934. return(ret);
  1935. f_err:
  1936. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1937. err:
  1938. return(-1);
  1939. }
  1940. int ssl3_get_cert_status(SSL *s)
  1941. {
  1942. int ok, al;
  1943. unsigned long resplen,n;
  1944. const unsigned char *p;
  1945. n=s->method->ssl_get_message(s,
  1946. SSL3_ST_CR_CERT_STATUS_A,
  1947. SSL3_ST_CR_CERT_STATUS_B,
  1948. SSL3_MT_CERTIFICATE_STATUS,
  1949. 16384,
  1950. &ok);
  1951. if (!ok) return((int)n);
  1952. if (n < 4)
  1953. {
  1954. /* need at least status type + length */
  1955. al = SSL_AD_DECODE_ERROR;
  1956. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
  1957. goto f_err;
  1958. }
  1959. p = (unsigned char *)s->init_msg;
  1960. if (*p++ != TLSEXT_STATUSTYPE_ocsp)
  1961. {
  1962. al = SSL_AD_DECODE_ERROR;
  1963. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_UNSUPPORTED_STATUS_TYPE);
  1964. goto f_err;
  1965. }
  1966. n2l3(p, resplen);
  1967. if (resplen + 4 != n)
  1968. {
  1969. al = SSL_AD_DECODE_ERROR;
  1970. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
  1971. goto f_err;
  1972. }
  1973. if (s->tlsext_ocsp_resp)
  1974. OPENSSL_free(s->tlsext_ocsp_resp);
  1975. s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
  1976. if (!s->tlsext_ocsp_resp)
  1977. {
  1978. al = SSL_AD_INTERNAL_ERROR;
  1979. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE);
  1980. goto f_err;
  1981. }
  1982. s->tlsext_ocsp_resplen = resplen;
  1983. if (s->ctx->tlsext_status_cb)
  1984. {
  1985. int ret;
  1986. ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
  1987. if (ret == 0)
  1988. {
  1989. al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
  1990. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_INVALID_STATUS_RESPONSE);
  1991. goto f_err;
  1992. }
  1993. if (ret < 0)
  1994. {
  1995. al = SSL_AD_INTERNAL_ERROR;
  1996. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE);
  1997. goto f_err;
  1998. }
  1999. }
  2000. return 1;
  2001. f_err:
  2002. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  2003. return(-1);
  2004. }
  2005. #endif
  2006. int ssl3_get_server_done(SSL *s)
  2007. {
  2008. int ok,ret=0;
  2009. long n;
  2010. n=s->method->ssl_get_message(s,
  2011. SSL3_ST_CR_SRVR_DONE_A,
  2012. SSL3_ST_CR_SRVR_DONE_B,
  2013. SSL3_MT_SERVER_DONE,
  2014. 30, /* should be very small, like 0 :-) */
  2015. &ok);
  2016. if (!ok) return((int)n);
  2017. if (n > 0)
  2018. {
  2019. /* should contain no data */
  2020. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  2021. SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
  2022. return -1;
  2023. }
  2024. ret=1;
  2025. return(ret);
  2026. }
  2027. int ssl3_send_client_key_exchange(SSL *s)
  2028. {
  2029. unsigned char *p,*d;
  2030. int n;
  2031. unsigned long alg_k;
  2032. #ifndef OPENSSL_NO_RSA
  2033. unsigned char *q;
  2034. EVP_PKEY *pkey=NULL;
  2035. #endif
  2036. #ifndef OPENSSL_NO_KRB5
  2037. KSSL_ERR kssl_err;
  2038. #endif /* OPENSSL_NO_KRB5 */
  2039. #ifndef OPENSSL_NO_ECDH
  2040. EC_KEY *clnt_ecdh = NULL;
  2041. const EC_POINT *srvr_ecpoint = NULL;
  2042. EVP_PKEY *srvr_pub_pkey = NULL;
  2043. unsigned char *encodedPoint = NULL;
  2044. int encoded_pt_len = 0;
  2045. BN_CTX * bn_ctx = NULL;
  2046. #endif
  2047. if (s->state == SSL3_ST_CW_KEY_EXCH_A)
  2048. {
  2049. d=(unsigned char *)s->init_buf->data;
  2050. p= &(d[4]);
  2051. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  2052. /* Fool emacs indentation */
  2053. if (0) {}
  2054. #ifndef OPENSSL_NO_RSA
  2055. else if (alg_k & SSL_kRSA)
  2056. {
  2057. RSA *rsa;
  2058. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  2059. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  2060. rsa=s->session->sess_cert->peer_rsa_tmp;
  2061. else
  2062. {
  2063. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  2064. if ((pkey == NULL) ||
  2065. (pkey->type != EVP_PKEY_RSA) ||
  2066. (pkey->pkey.rsa == NULL))
  2067. {
  2068. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  2069. goto err;
  2070. }
  2071. rsa=pkey->pkey.rsa;
  2072. EVP_PKEY_free(pkey);
  2073. }
  2074. tmp_buf[0]=s->client_version>>8;
  2075. tmp_buf[1]=s->client_version&0xff;
  2076. if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
  2077. goto err;
  2078. s->session->master_key_length=sizeof tmp_buf;
  2079. q=p;
  2080. /* Fix buf for TLS and beyond */
  2081. if (s->version > SSL3_VERSION)
  2082. p+=2;
  2083. n=RSA_public_encrypt(sizeof tmp_buf,
  2084. tmp_buf,p,rsa,RSA_PKCS1_PADDING);
  2085. #ifdef PKCS1_CHECK
  2086. if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
  2087. if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
  2088. #endif
  2089. if (n <= 0)
  2090. {
  2091. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
  2092. goto err;
  2093. }
  2094. /* Fix buf for TLS and beyond */
  2095. if (s->version > SSL3_VERSION)
  2096. {
  2097. s2n(n,q);
  2098. n+=2;
  2099. }
  2100. s->session->master_key_length=
  2101. s->method->ssl3_enc->generate_master_secret(s,
  2102. s->session->master_key,
  2103. tmp_buf,sizeof tmp_buf);
  2104. OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
  2105. }
  2106. #endif
  2107. #ifndef OPENSSL_NO_KRB5
  2108. else if (alg_k & SSL_kKRB5)
  2109. {
  2110. krb5_error_code krb5rc;
  2111. KSSL_CTX *kssl_ctx = s->kssl_ctx;
  2112. /* krb5_data krb5_ap_req; */
  2113. krb5_data *enc_ticket;
  2114. krb5_data authenticator, *authp = NULL;
  2115. EVP_CIPHER_CTX ciph_ctx;
  2116. const EVP_CIPHER *enc = NULL;
  2117. unsigned char iv[EVP_MAX_IV_LENGTH];
  2118. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  2119. unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
  2120. + EVP_MAX_IV_LENGTH];
  2121. int padl, outl = sizeof(epms);
  2122. EVP_CIPHER_CTX_init(&ciph_ctx);
  2123. #ifdef KSSL_DEBUG
  2124. printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
  2125. alg_k, SSL_kKRB5);
  2126. #endif /* KSSL_DEBUG */
  2127. authp = NULL;
  2128. #ifdef KRB5SENDAUTH
  2129. if (KRB5SENDAUTH) authp = &authenticator;
  2130. #endif /* KRB5SENDAUTH */
  2131. krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
  2132. &kssl_err);
  2133. enc = kssl_map_enc(kssl_ctx->enctype);
  2134. if (enc == NULL)
  2135. goto err;
  2136. #ifdef KSSL_DEBUG
  2137. {
  2138. printf("kssl_cget_tkt rtn %d\n", krb5rc);
  2139. if (krb5rc && kssl_err.text)
  2140. printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
  2141. }
  2142. #endif /* KSSL_DEBUG */
  2143. if (krb5rc)
  2144. {
  2145. ssl3_send_alert(s,SSL3_AL_FATAL,
  2146. SSL_AD_HANDSHAKE_FAILURE);
  2147. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2148. kssl_err.reason);
  2149. goto err;
  2150. }
  2151. /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
  2152. ** in place of RFC 2712 KerberosWrapper, as in:
  2153. **
  2154. ** Send ticket (copy to *p, set n = length)
  2155. ** n = krb5_ap_req.length;
  2156. ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
  2157. ** if (krb5_ap_req.data)
  2158. ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
  2159. **
  2160. ** Now using real RFC 2712 KerberosWrapper
  2161. ** (Thanks to Simon Wilkinson <[email protected]>)
  2162. ** Note: 2712 "opaque" types are here replaced
  2163. ** with a 2-byte length followed by the value.
  2164. ** Example:
  2165. ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
  2166. ** Where "xx xx" = length bytes. Shown here with
  2167. ** optional authenticator omitted.
  2168. */
  2169. /* KerberosWrapper.Ticket */
  2170. s2n(enc_ticket->length,p);
  2171. memcpy(p, enc_ticket->data, enc_ticket->length);
  2172. p+= enc_ticket->length;
  2173. n = enc_ticket->length + 2;
  2174. /* KerberosWrapper.Authenticator */
  2175. if (authp && authp->length)
  2176. {
  2177. s2n(authp->length,p);
  2178. memcpy(p, authp->data, authp->length);
  2179. p+= authp->length;
  2180. n+= authp->length + 2;
  2181. free(authp->data);
  2182. authp->data = NULL;
  2183. authp->length = 0;
  2184. }
  2185. else
  2186. {
  2187. s2n(0,p);/* null authenticator length */
  2188. n+=2;
  2189. }
  2190. tmp_buf[0]=s->client_version>>8;
  2191. tmp_buf[1]=s->client_version&0xff;
  2192. if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
  2193. goto err;
  2194. /* 20010420 VRS. Tried it this way; failed.
  2195. ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
  2196. ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
  2197. ** kssl_ctx->length);
  2198. ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
  2199. */
  2200. memset(iv, 0, sizeof iv); /* per RFC 1510 */
  2201. EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
  2202. kssl_ctx->key,iv);
  2203. EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
  2204. sizeof tmp_buf);
  2205. EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
  2206. outl += padl;
  2207. if (outl > (int)sizeof epms)
  2208. {
  2209. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  2210. goto err;
  2211. }
  2212. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  2213. /* KerberosWrapper.EncryptedPreMasterSecret */
  2214. s2n(outl,p);
  2215. memcpy(p, epms, outl);
  2216. p+=outl;
  2217. n+=outl + 2;
  2218. s->session->master_key_length=
  2219. s->method->ssl3_enc->generate_master_secret(s,
  2220. s->session->master_key,
  2221. tmp_buf, sizeof tmp_buf);
  2222. OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
  2223. OPENSSL_cleanse(epms, outl);
  2224. }
  2225. #endif
  2226. #ifndef OPENSSL_NO_DH
  2227. else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  2228. {
  2229. DH *dh_srvr,*dh_clnt;
  2230. if (s->session->sess_cert == NULL)
  2231. {
  2232. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  2233. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
  2234. goto err;
  2235. }
  2236. if (s->session->sess_cert->peer_dh_tmp != NULL)
  2237. dh_srvr=s->session->sess_cert->peer_dh_tmp;
  2238. else
  2239. {
  2240. /* we get them from the cert */
  2241. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  2242. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
  2243. goto err;
  2244. }
  2245. /* generate a new random key */
  2246. if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
  2247. {
  2248. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  2249. goto err;
  2250. }
  2251. if (!DH_generate_key(dh_clnt))
  2252. {
  2253. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  2254. DH_free(dh_clnt);
  2255. goto err;
  2256. }
  2257. /* use the 'p' output buffer for the DH key, but
  2258. * make sure to clear it out afterwards */
  2259. n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
  2260. if (n <= 0)
  2261. {
  2262. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  2263. DH_free(dh_clnt);
  2264. goto err;
  2265. }
  2266. /* generate master key from the result */
  2267. s->session->master_key_length=
  2268. s->method->ssl3_enc->generate_master_secret(s,
  2269. s->session->master_key,p,n);
  2270. /* clean up */
  2271. memset(p,0,n);
  2272. /* send off the data */
  2273. n=BN_num_bytes(dh_clnt->pub_key);
  2274. s2n(n,p);
  2275. BN_bn2bin(dh_clnt->pub_key,p);
  2276. n+=2;
  2277. DH_free(dh_clnt);
  2278. /* perhaps clean things up a bit EAY EAY EAY EAY*/
  2279. }
  2280. #endif
  2281. #ifndef OPENSSL_NO_ECDH
  2282. else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
  2283. {
  2284. const EC_GROUP *srvr_group = NULL;
  2285. EC_KEY *tkey;
  2286. int ecdh_clnt_cert = 0;
  2287. int field_size = 0;
  2288. /* Did we send out the client's
  2289. * ECDH share for use in premaster
  2290. * computation as part of client certificate?
  2291. * If so, set ecdh_clnt_cert to 1.
  2292. */
  2293. if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
  2294. {
  2295. /* XXX: For now, we do not support client
  2296. * authentication using ECDH certificates.
  2297. * To add such support, one needs to add
  2298. * code that checks for appropriate
  2299. * conditions and sets ecdh_clnt_cert to 1.
  2300. * For example, the cert have an ECC
  2301. * key on the same curve as the server's
  2302. * and the key should be authorized for
  2303. * key agreement.
  2304. *
  2305. * One also needs to add code in ssl3_connect
  2306. * to skip sending the certificate verify
  2307. * message.
  2308. *
  2309. * if ((s->cert->key->privatekey != NULL) &&
  2310. * (s->cert->key->privatekey->type ==
  2311. * EVP_PKEY_EC) && ...)
  2312. * ecdh_clnt_cert = 1;
  2313. */
  2314. }
  2315. if (s->session->sess_cert->peer_ecdh_tmp != NULL)
  2316. {
  2317. tkey = s->session->sess_cert->peer_ecdh_tmp;
  2318. }
  2319. else
  2320. {
  2321. /* Get the Server Public Key from Cert */
  2322. srvr_pub_pkey = X509_get_pubkey(s->session-> \
  2323. sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  2324. if ((srvr_pub_pkey == NULL) ||
  2325. (srvr_pub_pkey->type != EVP_PKEY_EC) ||
  2326. (srvr_pub_pkey->pkey.ec == NULL))
  2327. {
  2328. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2329. ERR_R_INTERNAL_ERROR);
  2330. goto err;
  2331. }
  2332. tkey = srvr_pub_pkey->pkey.ec;
  2333. }
  2334. srvr_group = EC_KEY_get0_group(tkey);
  2335. srvr_ecpoint = EC_KEY_get0_public_key(tkey);
  2336. if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
  2337. {
  2338. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2339. ERR_R_INTERNAL_ERROR);
  2340. goto err;
  2341. }
  2342. if ((clnt_ecdh=EC_KEY_new()) == NULL)
  2343. {
  2344. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  2345. goto err;
  2346. }
  2347. if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
  2348. {
  2349. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
  2350. goto err;
  2351. }
  2352. if (ecdh_clnt_cert)
  2353. {
  2354. /* Reuse key info from our certificate
  2355. * We only need our private key to perform
  2356. * the ECDH computation.
  2357. */
  2358. const BIGNUM *priv_key;
  2359. tkey = s->cert->key->privatekey->pkey.ec;
  2360. priv_key = EC_KEY_get0_private_key(tkey);
  2361. if (priv_key == NULL)
  2362. {
  2363. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  2364. goto err;
  2365. }
  2366. if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
  2367. {
  2368. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
  2369. goto err;
  2370. }
  2371. }
  2372. else
  2373. {
  2374. /* Generate a new ECDH key pair */
  2375. if (!(EC_KEY_generate_key(clnt_ecdh)))
  2376. {
  2377. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  2378. goto err;
  2379. }
  2380. }
  2381. /* use the 'p' output buffer for the ECDH key, but
  2382. * make sure to clear it out afterwards
  2383. */
  2384. field_size = EC_GROUP_get_degree(srvr_group);
  2385. if (field_size <= 0)
  2386. {
  2387. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2388. ERR_R_ECDH_LIB);
  2389. goto err;
  2390. }
  2391. n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
  2392. if (n <= 0)
  2393. {
  2394. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2395. ERR_R_ECDH_LIB);
  2396. goto err;
  2397. }
  2398. /* generate master key from the result */
  2399. s->session->master_key_length = s->method->ssl3_enc \
  2400. -> generate_master_secret(s,
  2401. s->session->master_key,
  2402. p, n);
  2403. memset(p, 0, n); /* clean up */
  2404. if (ecdh_clnt_cert)
  2405. {
  2406. /* Send empty client key exch message */
  2407. n = 0;
  2408. }
  2409. else
  2410. {
  2411. /* First check the size of encoding and
  2412. * allocate memory accordingly.
  2413. */
  2414. encoded_pt_len =
  2415. EC_POINT_point2oct(srvr_group,
  2416. EC_KEY_get0_public_key(clnt_ecdh),
  2417. POINT_CONVERSION_UNCOMPRESSED,
  2418. NULL, 0, NULL);
  2419. encodedPoint = (unsigned char *)
  2420. OPENSSL_malloc(encoded_pt_len *
  2421. sizeof(unsigned char));
  2422. bn_ctx = BN_CTX_new();
  2423. if ((encodedPoint == NULL) ||
  2424. (bn_ctx == NULL))
  2425. {
  2426. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  2427. goto err;
  2428. }
  2429. /* Encode the public key */
  2430. n = EC_POINT_point2oct(srvr_group,
  2431. EC_KEY_get0_public_key(clnt_ecdh),
  2432. POINT_CONVERSION_UNCOMPRESSED,
  2433. encodedPoint, encoded_pt_len, bn_ctx);
  2434. *p = n; /* length of encoded point */
  2435. /* Encoded point will be copied here */
  2436. p += 1;
  2437. /* copy the point */
  2438. memcpy((unsigned char *)p, encodedPoint, n);
  2439. /* increment n to account for length field */
  2440. n += 1;
  2441. }
  2442. /* Free allocated memory */
  2443. BN_CTX_free(bn_ctx);
  2444. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  2445. if (clnt_ecdh != NULL)
  2446. EC_KEY_free(clnt_ecdh);
  2447. EVP_PKEY_free(srvr_pub_pkey);
  2448. }
  2449. #endif /* !OPENSSL_NO_ECDH */
  2450. else if (alg_k & SSL_kGOST)
  2451. {
  2452. /* GOST key exchange message creation */
  2453. EVP_PKEY_CTX *pkey_ctx;
  2454. X509 *peer_cert;
  2455. size_t msglen;
  2456. unsigned int md_len;
  2457. int keytype;
  2458. unsigned char premaster_secret[32],shared_ukm[32], tmp[256];
  2459. EVP_MD_CTX *ukm_hash;
  2460. EVP_PKEY *pub_key;
  2461. /* Get server sertificate PKEY and create ctx from it */
  2462. peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST01)].x509;
  2463. if (!peer_cert)
  2464. peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST94)].x509;
  2465. if (!peer_cert) {
  2466. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
  2467. goto err;
  2468. }
  2469. pkey_ctx=EVP_PKEY_CTX_new(pub_key=X509_get_pubkey(peer_cert),NULL);
  2470. /* If we have send a certificate, and certificate key
  2471. * parameters match those of server certificate, use
  2472. * certificate key for key exchange
  2473. */
  2474. /* Otherwise, generate ephemeral key pair */
  2475. EVP_PKEY_encrypt_init(pkey_ctx);
  2476. /* Generate session key */
  2477. RAND_bytes(premaster_secret,32);
  2478. /* If we have client certificate, use its secret as peer key */
  2479. if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
  2480. if (EVP_PKEY_derive_set_peer(pkey_ctx,s->cert->key->privatekey) <=0) {
  2481. /* If there was an error - just ignore it. Ephemeral key
  2482. * would be used
  2483. */
  2484. ERR_clear_error();
  2485. }
  2486. }
  2487. /* Compute shared IV and store it in algorithm-specific
  2488. * context data */
  2489. ukm_hash = EVP_MD_CTX_create();
  2490. EVP_DigestInit(ukm_hash,EVP_get_digestbynid(NID_id_GostR3411_94));
  2491. EVP_DigestUpdate(ukm_hash,s->s3->client_random,SSL3_RANDOM_SIZE);
  2492. EVP_DigestUpdate(ukm_hash,s->s3->server_random,SSL3_RANDOM_SIZE);
  2493. EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
  2494. EVP_MD_CTX_destroy(ukm_hash);
  2495. if (EVP_PKEY_CTX_ctrl(pkey_ctx,-1,EVP_PKEY_OP_ENCRYPT,EVP_PKEY_CTRL_SET_IV,
  2496. 8,shared_ukm)<0) {
  2497. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2498. SSL_R_LIBRARY_BUG);
  2499. goto err;
  2500. }
  2501. /* Make GOST keytransport blob message */
  2502. /*Encapsulate it into sequence */
  2503. *(p++)=V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
  2504. msglen=255;
  2505. if (EVP_PKEY_encrypt(pkey_ctx,tmp,&msglen,premaster_secret,32)<0) {
  2506. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2507. SSL_R_LIBRARY_BUG);
  2508. goto err;
  2509. }
  2510. if (msglen >= 0x80)
  2511. {
  2512. *(p++)=0x81;
  2513. *(p++)= msglen & 0xff;
  2514. n=msglen+3;
  2515. }
  2516. else
  2517. {
  2518. *(p++)= msglen & 0xff;
  2519. n=msglen+2;
  2520. }
  2521. memcpy(p, tmp, msglen);
  2522. /* Check if pubkey from client certificate was used */
  2523. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
  2524. {
  2525. /* Set flag "skip certificate verify" */
  2526. s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
  2527. }
  2528. EVP_PKEY_CTX_free(pkey_ctx);
  2529. s->session->master_key_length=
  2530. s->method->ssl3_enc->generate_master_secret(s,
  2531. s->session->master_key,premaster_secret,32);
  2532. EVP_PKEY_free(pub_key);
  2533. }
  2534. #ifndef OPENSSL_NO_SRP
  2535. else if (alg_k & SSL_kSRP)
  2536. {
  2537. if (s->srp_ctx.A != NULL)
  2538. {
  2539. /* send off the data */
  2540. n=BN_num_bytes(s->srp_ctx.A);
  2541. s2n(n,p);
  2542. BN_bn2bin(s->srp_ctx.A,p);
  2543. n+=2;
  2544. }
  2545. else
  2546. {
  2547. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  2548. goto err;
  2549. }
  2550. if (s->session->srp_username != NULL)
  2551. OPENSSL_free(s->session->srp_username);
  2552. s->session->srp_username = BUF_strdup(s->srp_ctx.login);
  2553. if (s->session->srp_username == NULL)
  2554. {
  2555. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2556. ERR_R_MALLOC_FAILURE);
  2557. goto err;
  2558. }
  2559. if ((s->session->master_key_length = SRP_generate_client_master_secret(s,s->session->master_key))<0)
  2560. {
  2561. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  2562. goto err;
  2563. }
  2564. }
  2565. #endif
  2566. #ifndef OPENSSL_NO_PSK
  2567. else if (alg_k & SSL_kPSK)
  2568. {
  2569. char identity[PSK_MAX_IDENTITY_LEN];
  2570. unsigned char *t = NULL;
  2571. unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
  2572. unsigned int pre_ms_len = 0, psk_len = 0;
  2573. int psk_err = 1;
  2574. n = 0;
  2575. if (s->psk_client_callback == NULL)
  2576. {
  2577. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2578. SSL_R_PSK_NO_CLIENT_CB);
  2579. goto err;
  2580. }
  2581. psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
  2582. identity, PSK_MAX_IDENTITY_LEN,
  2583. psk_or_pre_ms, sizeof(psk_or_pre_ms));
  2584. if (psk_len > PSK_MAX_PSK_LEN)
  2585. {
  2586. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2587. ERR_R_INTERNAL_ERROR);
  2588. goto psk_err;
  2589. }
  2590. else if (psk_len == 0)
  2591. {
  2592. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2593. SSL_R_PSK_IDENTITY_NOT_FOUND);
  2594. goto psk_err;
  2595. }
  2596. /* create PSK pre_master_secret */
  2597. pre_ms_len = 2+psk_len+2+psk_len;
  2598. t = psk_or_pre_ms;
  2599. memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
  2600. s2n(psk_len, t);
  2601. memset(t, 0, psk_len);
  2602. t+=psk_len;
  2603. s2n(psk_len, t);
  2604. if (s->session->psk_identity_hint != NULL)
  2605. OPENSSL_free(s->session->psk_identity_hint);
  2606. s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
  2607. if (s->ctx->psk_identity_hint != NULL &&
  2608. s->session->psk_identity_hint == NULL)
  2609. {
  2610. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2611. ERR_R_MALLOC_FAILURE);
  2612. goto psk_err;
  2613. }
  2614. if (s->session->psk_identity != NULL)
  2615. OPENSSL_free(s->session->psk_identity);
  2616. s->session->psk_identity = BUF_strdup(identity);
  2617. if (s->session->psk_identity == NULL)
  2618. {
  2619. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2620. ERR_R_MALLOC_FAILURE);
  2621. goto psk_err;
  2622. }
  2623. s->session->master_key_length =
  2624. s->method->ssl3_enc->generate_master_secret(s,
  2625. s->session->master_key,
  2626. psk_or_pre_ms, pre_ms_len);
  2627. n = strlen(identity);
  2628. s2n(n, p);
  2629. memcpy(p, identity, n);
  2630. n+=2;
  2631. psk_err = 0;
  2632. psk_err:
  2633. OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
  2634. OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
  2635. if (psk_err != 0)
  2636. {
  2637. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  2638. goto err;
  2639. }
  2640. }
  2641. #endif
  2642. else
  2643. {
  2644. ssl3_send_alert(s, SSL3_AL_FATAL,
  2645. SSL_AD_HANDSHAKE_FAILURE);
  2646. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2647. ERR_R_INTERNAL_ERROR);
  2648. goto err;
  2649. }
  2650. *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
  2651. l2n3(n,d);
  2652. s->state=SSL3_ST_CW_KEY_EXCH_B;
  2653. /* number of bytes to write */
  2654. s->init_num=n+4;
  2655. s->init_off=0;
  2656. }
  2657. /* SSL3_ST_CW_KEY_EXCH_B */
  2658. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2659. err:
  2660. #ifndef OPENSSL_NO_ECDH
  2661. BN_CTX_free(bn_ctx);
  2662. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  2663. if (clnt_ecdh != NULL)
  2664. EC_KEY_free(clnt_ecdh);
  2665. EVP_PKEY_free(srvr_pub_pkey);
  2666. #endif
  2667. return(-1);
  2668. }
  2669. int ssl3_send_client_verify(SSL *s)
  2670. {
  2671. unsigned char *p,*d;
  2672. unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  2673. EVP_PKEY *pkey;
  2674. EVP_PKEY_CTX *pctx=NULL;
  2675. EVP_MD_CTX mctx;
  2676. unsigned u=0;
  2677. unsigned long n;
  2678. int j;
  2679. EVP_MD_CTX_init(&mctx);
  2680. if (s->state == SSL3_ST_CW_CERT_VRFY_A)
  2681. {
  2682. d=(unsigned char *)s->init_buf->data;
  2683. p= &(d[4]);
  2684. pkey=s->cert->key->privatekey;
  2685. /* Create context from key and test if sha1 is allowed as digest */
  2686. pctx = EVP_PKEY_CTX_new(pkey,NULL);
  2687. EVP_PKEY_sign_init(pctx);
  2688. if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1())>0)
  2689. {
  2690. if (TLS1_get_version(s) < TLS1_2_VERSION)
  2691. s->method->ssl3_enc->cert_verify_mac(s,
  2692. NID_sha1,
  2693. &(data[MD5_DIGEST_LENGTH]));
  2694. }
  2695. else
  2696. {
  2697. ERR_clear_error();
  2698. }
  2699. /* For TLS v1.2 send signature algorithm and signature
  2700. * using agreed digest and cached handshake records.
  2701. */
  2702. if (TLS1_get_version(s) >= TLS1_2_VERSION)
  2703. {
  2704. long hdatalen = 0;
  2705. void *hdata;
  2706. const EVP_MD *md = s->cert->key->digest;
  2707. hdatalen = BIO_get_mem_data(s->s3->handshake_buffer,
  2708. &hdata);
  2709. if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md))
  2710. {
  2711. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
  2712. ERR_R_INTERNAL_ERROR);
  2713. goto err;
  2714. }
  2715. p += 2;
  2716. #ifdef SSL_DEBUG
  2717. fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
  2718. EVP_MD_name(md));
  2719. #endif
  2720. if (!EVP_SignInit_ex(&mctx, md, NULL)
  2721. || !EVP_SignUpdate(&mctx, hdata, hdatalen)
  2722. || !EVP_SignFinal(&mctx, p + 2, &u, pkey))
  2723. {
  2724. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
  2725. ERR_R_EVP_LIB);
  2726. goto err;
  2727. }
  2728. s2n(u,p);
  2729. n = u + 4;
  2730. if (!ssl3_digest_cached_records(s))
  2731. goto err;
  2732. }
  2733. else
  2734. #ifndef OPENSSL_NO_RSA
  2735. if (pkey->type == EVP_PKEY_RSA)
  2736. {
  2737. s->method->ssl3_enc->cert_verify_mac(s,
  2738. NID_md5,
  2739. &(data[0]));
  2740. if (RSA_sign(NID_md5_sha1, data,
  2741. MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
  2742. &(p[2]), &u, pkey->pkey.rsa) <= 0 )
  2743. {
  2744. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
  2745. goto err;
  2746. }
  2747. s2n(u,p);
  2748. n=u+2;
  2749. }
  2750. else
  2751. #endif
  2752. #ifndef OPENSSL_NO_DSA
  2753. if (pkey->type == EVP_PKEY_DSA)
  2754. {
  2755. if (!DSA_sign(pkey->save_type,
  2756. &(data[MD5_DIGEST_LENGTH]),
  2757. SHA_DIGEST_LENGTH,&(p[2]),
  2758. (unsigned int *)&j,pkey->pkey.dsa))
  2759. {
  2760. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
  2761. goto err;
  2762. }
  2763. s2n(j,p);
  2764. n=j+2;
  2765. }
  2766. else
  2767. #endif
  2768. #ifndef OPENSSL_NO_ECDSA
  2769. if (pkey->type == EVP_PKEY_EC)
  2770. {
  2771. if (!ECDSA_sign(pkey->save_type,
  2772. &(data[MD5_DIGEST_LENGTH]),
  2773. SHA_DIGEST_LENGTH,&(p[2]),
  2774. (unsigned int *)&j,pkey->pkey.ec))
  2775. {
  2776. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
  2777. ERR_R_ECDSA_LIB);
  2778. goto err;
  2779. }
  2780. s2n(j,p);
  2781. n=j+2;
  2782. }
  2783. else
  2784. #endif
  2785. if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_2001)
  2786. {
  2787. unsigned char signbuf[64];
  2788. int i;
  2789. size_t sigsize=64;
  2790. s->method->ssl3_enc->cert_verify_mac(s,
  2791. NID_id_GostR3411_94,
  2792. data);
  2793. if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
  2794. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
  2795. ERR_R_INTERNAL_ERROR);
  2796. goto err;
  2797. }
  2798. for (i=63,j=0; i>=0; j++, i--) {
  2799. p[2+j]=signbuf[i];
  2800. }
  2801. s2n(j,p);
  2802. n=j+2;
  2803. }
  2804. else
  2805. {
  2806. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
  2807. goto err;
  2808. }
  2809. *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
  2810. l2n3(n,d);
  2811. s->state=SSL3_ST_CW_CERT_VRFY_B;
  2812. s->init_num=(int)n+4;
  2813. s->init_off=0;
  2814. }
  2815. EVP_MD_CTX_cleanup(&mctx);
  2816. EVP_PKEY_CTX_free(pctx);
  2817. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2818. err:
  2819. EVP_MD_CTX_cleanup(&mctx);
  2820. EVP_PKEY_CTX_free(pctx);
  2821. return(-1);
  2822. }
  2823. int ssl3_send_client_certificate(SSL *s)
  2824. {
  2825. X509 *x509=NULL;
  2826. EVP_PKEY *pkey=NULL;
  2827. int i;
  2828. unsigned long l;
  2829. if (s->state == SSL3_ST_CW_CERT_A)
  2830. {
  2831. if ((s->cert == NULL) ||
  2832. (s->cert->key->x509 == NULL) ||
  2833. (s->cert->key->privatekey == NULL))
  2834. s->state=SSL3_ST_CW_CERT_B;
  2835. else
  2836. s->state=SSL3_ST_CW_CERT_C;
  2837. }
  2838. /* We need to get a client cert */
  2839. if (s->state == SSL3_ST_CW_CERT_B)
  2840. {
  2841. /* If we get an error, we need to
  2842. * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
  2843. * We then get retied later */
  2844. i=0;
  2845. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  2846. if (i < 0)
  2847. {
  2848. s->rwstate=SSL_X509_LOOKUP;
  2849. return(-1);
  2850. }
  2851. s->rwstate=SSL_NOTHING;
  2852. if ((i == 1) && (pkey != NULL) && (x509 != NULL))
  2853. {
  2854. s->state=SSL3_ST_CW_CERT_B;
  2855. if ( !SSL_use_certificate(s,x509) ||
  2856. !SSL_use_PrivateKey(s,pkey))
  2857. i=0;
  2858. }
  2859. else if (i == 1)
  2860. {
  2861. i=0;
  2862. SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  2863. }
  2864. if (x509 != NULL) X509_free(x509);
  2865. if (pkey != NULL) EVP_PKEY_free(pkey);
  2866. if (i == 0)
  2867. {
  2868. if (s->version == SSL3_VERSION)
  2869. {
  2870. s->s3->tmp.cert_req=0;
  2871. ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
  2872. return(1);
  2873. }
  2874. else
  2875. {
  2876. s->s3->tmp.cert_req=2;
  2877. }
  2878. }
  2879. /* Ok, we have a cert */
  2880. s->state=SSL3_ST_CW_CERT_C;
  2881. }
  2882. if (s->state == SSL3_ST_CW_CERT_C)
  2883. {
  2884. s->state=SSL3_ST_CW_CERT_D;
  2885. l=ssl3_output_cert_chain(s,
  2886. (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
  2887. s->init_num=(int)l;
  2888. s->init_off=0;
  2889. }
  2890. /* SSL3_ST_CW_CERT_D */
  2891. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2892. }
  2893. #define has_bits(i,m) (((i)&(m)) == (m))
  2894. int ssl3_check_cert_and_algorithm(SSL *s)
  2895. {
  2896. int i,idx;
  2897. long alg_k,alg_a;
  2898. EVP_PKEY *pkey=NULL;
  2899. SESS_CERT *sc;
  2900. #ifndef OPENSSL_NO_RSA
  2901. RSA *rsa;
  2902. #endif
  2903. #ifndef OPENSSL_NO_DH
  2904. DH *dh;
  2905. #endif
  2906. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  2907. alg_a=s->s3->tmp.new_cipher->algorithm_auth;
  2908. /* we don't have a certificate */
  2909. if ((alg_a & (SSL_aDH|SSL_aNULL|SSL_aKRB5)) || (alg_k & SSL_kPSK))
  2910. return(1);
  2911. sc=s->session->sess_cert;
  2912. if (sc == NULL)
  2913. {
  2914. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
  2915. goto err;
  2916. }
  2917. #ifndef OPENSSL_NO_RSA
  2918. rsa=s->session->sess_cert->peer_rsa_tmp;
  2919. #endif
  2920. #ifndef OPENSSL_NO_DH
  2921. dh=s->session->sess_cert->peer_dh_tmp;
  2922. #endif
  2923. /* This is the passed certificate */
  2924. idx=sc->peer_cert_type;
  2925. #ifndef OPENSSL_NO_ECDH
  2926. if (idx == SSL_PKEY_ECC)
  2927. {
  2928. if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
  2929. s) == 0)
  2930. { /* check failed */
  2931. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_BAD_ECC_CERT);
  2932. goto f_err;
  2933. }
  2934. else
  2935. {
  2936. return 1;
  2937. }
  2938. }
  2939. #endif
  2940. pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
  2941. i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
  2942. EVP_PKEY_free(pkey);
  2943. /* Check that we have a certificate if we require one */
  2944. if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
  2945. {
  2946. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
  2947. goto f_err;
  2948. }
  2949. #ifndef OPENSSL_NO_DSA
  2950. else if ((alg_a & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
  2951. {
  2952. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
  2953. goto f_err;
  2954. }
  2955. #endif
  2956. #ifndef OPENSSL_NO_RSA
  2957. if ((alg_k & SSL_kRSA) &&
  2958. !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
  2959. {
  2960. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  2961. goto f_err;
  2962. }
  2963. #endif
  2964. #ifndef OPENSSL_NO_DH
  2965. if ((alg_k & SSL_kEDH) &&
  2966. !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
  2967. {
  2968. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
  2969. goto f_err;
  2970. }
  2971. else if ((alg_k & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
  2972. {
  2973. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
  2974. goto f_err;
  2975. }
  2976. #ifndef OPENSSL_NO_DSA
  2977. else if ((alg_k & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
  2978. {
  2979. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
  2980. goto f_err;
  2981. }
  2982. #endif
  2983. #endif
  2984. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
  2985. {
  2986. #ifndef OPENSSL_NO_RSA
  2987. if (alg_k & SSL_kRSA)
  2988. {
  2989. if (rsa == NULL
  2990. || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
  2991. {
  2992. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
  2993. goto f_err;
  2994. }
  2995. }
  2996. else
  2997. #endif
  2998. #ifndef OPENSSL_NO_DH
  2999. if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  3000. {
  3001. if (dh == NULL
  3002. || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
  3003. {
  3004. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
  3005. goto f_err;
  3006. }
  3007. }
  3008. else
  3009. #endif
  3010. {
  3011. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  3012. goto f_err;
  3013. }
  3014. }
  3015. return(1);
  3016. f_err:
  3017. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  3018. err:
  3019. return(0);
  3020. }
  3021. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  3022. int ssl3_send_next_proto(SSL *s)
  3023. {
  3024. unsigned int len, padding_len;
  3025. unsigned char *d;
  3026. if (s->state == SSL3_ST_CW_NEXT_PROTO_A)
  3027. {
  3028. len = s->next_proto_negotiated_len;
  3029. padding_len = 32 - ((len + 2) % 32);
  3030. d = (unsigned char *)s->init_buf->data;
  3031. d[4] = len;
  3032. memcpy(d + 5, s->next_proto_negotiated, len);
  3033. d[5 + len] = padding_len;
  3034. memset(d + 6 + len, 0, padding_len);
  3035. *(d++)=SSL3_MT_NEXT_PROTO;
  3036. l2n3(2 + len + padding_len, d);
  3037. s->state = SSL3_ST_CW_NEXT_PROTO_B;
  3038. s->init_num = 4 + 2 + len + padding_len;
  3039. s->init_off = 0;
  3040. }
  3041. return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
  3042. }
  3043. #endif /* !OPENSSL_NO_TLSEXT && !OPENSSL_NO_NEXTPROTONEG */
  3044. /* Check to see if handshake is full or resumed. Usually this is just a
  3045. * case of checking to see if a cache hit has occurred. In the case of
  3046. * session tickets we have to check the next message to be sure.
  3047. */
  3048. #ifndef OPENSSL_NO_TLSEXT
  3049. int ssl3_check_finished(SSL *s)
  3050. {
  3051. int ok;
  3052. long n;
  3053. /* If we have no ticket it cannot be a resumed session. */
  3054. if (!s->session->tlsext_tick)
  3055. return 1;
  3056. /* this function is called when we really expect a Certificate
  3057. * message, so permit appropriate message length */
  3058. n=s->method->ssl_get_message(s,
  3059. SSL3_ST_CR_CERT_A,
  3060. SSL3_ST_CR_CERT_B,
  3061. -1,
  3062. s->max_cert_list,
  3063. &ok);
  3064. if (!ok) return((int)n);
  3065. s->s3->tmp.reuse_message = 1;
  3066. if ((s->s3->tmp.message_type == SSL3_MT_FINISHED)
  3067. || (s->s3->tmp.message_type == SSL3_MT_NEWSESSION_TICKET))
  3068. return 2;
  3069. return 1;
  3070. }
  3071. #endif
  3072. int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
  3073. {
  3074. int i = 0;
  3075. #ifndef OPENSSL_NO_ENGINE
  3076. if (s->ctx->client_cert_engine)
  3077. {
  3078. i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
  3079. SSL_get_client_CA_list(s),
  3080. px509, ppkey, NULL, NULL, NULL);
  3081. if (i != 0)
  3082. return i;
  3083. }
  3084. #endif
  3085. if (s->ctx->client_cert_cb)
  3086. i = s->ctx->client_cert_cb(s,px509,ppkey);
  3087. return i;
  3088. }