s3_srvr.c 82 KB

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