2
0

s3_srvr.c 89 KB

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