s3_clnt.c 125 KB

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