s3_srvr.c 122 KB

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