s3_srvr.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619
  1. /* ssl/s3_srvr.c */
  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-2005 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. #define REUSE_CIPHER_BUG
  125. #define NETSCAPE_HANG_BUG
  126. #include <stdio.h>
  127. #include "ssl_locl.h"
  128. #include "kssl_lcl.h"
  129. #include <openssl/buffer.h>
  130. #include <openssl/rand.h>
  131. #include <openssl/objects.h>
  132. #include <openssl/evp.h>
  133. #include <openssl/x509.h>
  134. #ifndef OPENSSL_NO_DH
  135. #include <openssl/dh.h>
  136. #endif
  137. #include <openssl/bn.h>
  138. #ifndef OPENSSL_NO_KRB5
  139. #include <openssl/krb5_asn.h>
  140. #endif
  141. #include <openssl/md5.h>
  142. static SSL_METHOD *ssl3_get_server_method(int ver);
  143. #ifndef OPENSSL_NO_ECDH
  144. static int nid2curve_id(int nid);
  145. #endif
  146. static SSL_METHOD *ssl3_get_server_method(int ver)
  147. {
  148. if (ver == SSL3_VERSION)
  149. return(SSLv3_server_method());
  150. else
  151. return(NULL);
  152. }
  153. SSL_METHOD *SSLv3_server_method(void)
  154. {
  155. static int init=1;
  156. static SSL_METHOD SSLv3_server_data;
  157. if (init)
  158. {
  159. CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD);
  160. if (init)
  161. {
  162. memcpy((char *)&SSLv3_server_data,(char *)sslv3_base_method(),
  163. sizeof(SSL_METHOD));
  164. SSLv3_server_data.ssl_accept=ssl3_accept;
  165. SSLv3_server_data.get_ssl_method=ssl3_get_server_method;
  166. init=0;
  167. }
  168. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD);
  169. }
  170. return(&SSLv3_server_data);
  171. }
  172. int ssl3_accept(SSL *s)
  173. {
  174. BUF_MEM *buf;
  175. unsigned long l,Time=time(NULL);
  176. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  177. long num1;
  178. int ret= -1;
  179. int new_state,state,skip=0;
  180. RAND_add(&Time,sizeof(Time),0);
  181. ERR_clear_error();
  182. clear_sys_error();
  183. if (s->info_callback != NULL)
  184. cb=s->info_callback;
  185. else if (s->ctx->info_callback != NULL)
  186. cb=s->ctx->info_callback;
  187. /* init things to blank */
  188. s->in_handshake++;
  189. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  190. if (s->cert == NULL)
  191. {
  192. SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
  193. return(-1);
  194. }
  195. for (;;)
  196. {
  197. state=s->state;
  198. switch (s->state)
  199. {
  200. case SSL_ST_RENEGOTIATE:
  201. s->new_session=1;
  202. /* s->state=SSL_ST_ACCEPT; */
  203. case SSL_ST_BEFORE:
  204. case SSL_ST_ACCEPT:
  205. case SSL_ST_BEFORE|SSL_ST_ACCEPT:
  206. case SSL_ST_OK|SSL_ST_ACCEPT:
  207. s->server=1;
  208. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  209. if ((s->version>>8) != 3)
  210. {
  211. SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
  212. return -1;
  213. }
  214. s->type=SSL_ST_ACCEPT;
  215. if (s->init_buf == NULL)
  216. {
  217. if ((buf=BUF_MEM_new()) == NULL)
  218. {
  219. ret= -1;
  220. goto end;
  221. }
  222. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  223. {
  224. ret= -1;
  225. goto end;
  226. }
  227. s->init_buf=buf;
  228. }
  229. if (!ssl3_setup_buffers(s))
  230. {
  231. ret= -1;
  232. goto end;
  233. }
  234. s->init_num=0;
  235. if (s->state != SSL_ST_RENEGOTIATE)
  236. {
  237. /* Ok, we now need to push on a buffering BIO so that
  238. * the output is sent in a way that TCP likes :-)
  239. */
  240. if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
  241. ssl3_init_finished_mac(s);
  242. s->state=SSL3_ST_SR_CLNT_HELLO_A;
  243. s->ctx->stats.sess_accept++;
  244. }
  245. else
  246. {
  247. /* s->state == SSL_ST_RENEGOTIATE,
  248. * we will just send a HelloRequest */
  249. s->ctx->stats.sess_accept_renegotiate++;
  250. s->state=SSL3_ST_SW_HELLO_REQ_A;
  251. }
  252. break;
  253. case SSL3_ST_SW_HELLO_REQ_A:
  254. case SSL3_ST_SW_HELLO_REQ_B:
  255. s->shutdown=0;
  256. ret=ssl3_send_hello_request(s);
  257. if (ret <= 0) goto end;
  258. s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
  259. s->state=SSL3_ST_SW_FLUSH;
  260. s->init_num=0;
  261. ssl3_init_finished_mac(s);
  262. break;
  263. case SSL3_ST_SW_HELLO_REQ_C:
  264. s->state=SSL_ST_OK;
  265. break;
  266. case SSL3_ST_SR_CLNT_HELLO_A:
  267. case SSL3_ST_SR_CLNT_HELLO_B:
  268. case SSL3_ST_SR_CLNT_HELLO_C:
  269. s->shutdown=0;
  270. ret=ssl3_get_client_hello(s);
  271. if (ret <= 0) goto end;
  272. s->new_session = 2;
  273. s->state=SSL3_ST_SW_SRVR_HELLO_A;
  274. s->init_num=0;
  275. break;
  276. case SSL3_ST_SW_SRVR_HELLO_A:
  277. case SSL3_ST_SW_SRVR_HELLO_B:
  278. ret=ssl3_send_server_hello(s);
  279. if (ret <= 0) goto end;
  280. if (s->hit)
  281. s->state=SSL3_ST_SW_CHANGE_A;
  282. else
  283. s->state=SSL3_ST_SW_CERT_A;
  284. s->init_num=0;
  285. break;
  286. case SSL3_ST_SW_CERT_A:
  287. case SSL3_ST_SW_CERT_B:
  288. /* Check if it is anon DH or anon ECDH */
  289. if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
  290. {
  291. ret=ssl3_send_server_certificate(s);
  292. if (ret <= 0) goto end;
  293. }
  294. else
  295. skip=1;
  296. s->state=SSL3_ST_SW_KEY_EXCH_A;
  297. s->init_num=0;
  298. break;
  299. case SSL3_ST_SW_KEY_EXCH_A:
  300. case SSL3_ST_SW_KEY_EXCH_B:
  301. l=s->s3->tmp.new_cipher->algorithms;
  302. /* clear this, it may get reset by
  303. * send_server_key_exchange */
  304. if ((s->options & SSL_OP_EPHEMERAL_RSA)
  305. #ifndef OPENSSL_NO_KRB5
  306. && !(l & SSL_KRB5)
  307. #endif /* OPENSSL_NO_KRB5 */
  308. )
  309. /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
  310. * even when forbidden by protocol specs
  311. * (handshake may fail as clients are not required to
  312. * be able to handle this) */
  313. s->s3->tmp.use_rsa_tmp=1;
  314. else
  315. s->s3->tmp.use_rsa_tmp=0;
  316. /* only send if a DH key exchange, fortezza or
  317. * RSA but we have a sign only certificate
  318. *
  319. * For ECC ciphersuites, we send a serverKeyExchange
  320. * message only if the cipher suite is either
  321. * ECDH-anon or ECDHE. In other cases, the
  322. * server certificate contains the server's
  323. * public key for key exchange.
  324. */
  325. if (s->s3->tmp.use_rsa_tmp
  326. || (l & SSL_kECDHE)
  327. || (l & (SSL_DH|SSL_kFZA))
  328. || ((l & SSL_kRSA)
  329. && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
  330. || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
  331. && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
  332. )
  333. )
  334. )
  335. )
  336. {
  337. ret=ssl3_send_server_key_exchange(s);
  338. if (ret <= 0) goto end;
  339. }
  340. else
  341. skip=1;
  342. s->state=SSL3_ST_SW_CERT_REQ_A;
  343. s->init_num=0;
  344. break;
  345. case SSL3_ST_SW_CERT_REQ_A:
  346. case SSL3_ST_SW_CERT_REQ_B:
  347. if (/* don't request cert unless asked for it: */
  348. !(s->verify_mode & SSL_VERIFY_PEER) ||
  349. /* if SSL_VERIFY_CLIENT_ONCE is set,
  350. * don't request cert during re-negotiation: */
  351. ((s->session->peer != NULL) &&
  352. (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
  353. /* never request cert in anonymous ciphersuites
  354. * (see section "Certificate request" in SSL 3 drafts
  355. * and in RFC 2246): */
  356. ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
  357. /* ... except when the application insists on verification
  358. * (against the specs, but s3_clnt.c accepts this for SSL 3) */
  359. !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
  360. /* never request cert in Kerberos ciphersuites */
  361. (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5))
  362. {
  363. /* no cert request */
  364. skip=1;
  365. s->s3->tmp.cert_request=0;
  366. s->state=SSL3_ST_SW_SRVR_DONE_A;
  367. }
  368. else
  369. {
  370. s->s3->tmp.cert_request=1;
  371. ret=ssl3_send_certificate_request(s);
  372. if (ret <= 0) goto end;
  373. #ifndef NETSCAPE_HANG_BUG
  374. s->state=SSL3_ST_SW_SRVR_DONE_A;
  375. #else
  376. s->state=SSL3_ST_SW_FLUSH;
  377. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  378. #endif
  379. s->init_num=0;
  380. }
  381. break;
  382. case SSL3_ST_SW_SRVR_DONE_A:
  383. case SSL3_ST_SW_SRVR_DONE_B:
  384. ret=ssl3_send_server_done(s);
  385. if (ret <= 0) goto end;
  386. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  387. s->state=SSL3_ST_SW_FLUSH;
  388. s->init_num=0;
  389. break;
  390. case SSL3_ST_SW_FLUSH:
  391. /* number of bytes to be flushed */
  392. num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
  393. if (num1 > 0)
  394. {
  395. s->rwstate=SSL_WRITING;
  396. num1=BIO_flush(s->wbio);
  397. if (num1 <= 0) { ret= -1; goto end; }
  398. s->rwstate=SSL_NOTHING;
  399. }
  400. s->state=s->s3->tmp.next_state;
  401. break;
  402. case SSL3_ST_SR_CERT_A:
  403. case SSL3_ST_SR_CERT_B:
  404. /* Check for second client hello (MS SGC) */
  405. ret = ssl3_check_client_hello(s);
  406. if (ret <= 0)
  407. goto end;
  408. if (ret == 2)
  409. s->state = SSL3_ST_SR_CLNT_HELLO_C;
  410. else {
  411. if (s->s3->tmp.cert_request)
  412. {
  413. ret=ssl3_get_client_certificate(s);
  414. if (ret <= 0) goto end;
  415. }
  416. s->init_num=0;
  417. s->state=SSL3_ST_SR_KEY_EXCH_A;
  418. }
  419. break;
  420. case SSL3_ST_SR_KEY_EXCH_A:
  421. case SSL3_ST_SR_KEY_EXCH_B:
  422. ret=ssl3_get_client_key_exchange(s);
  423. if (ret <= 0)
  424. goto end;
  425. if (ret == 2)
  426. {
  427. /* For the ECDH ciphersuites when
  428. * the client sends its ECDH pub key in
  429. * a certificate, the CertificateVerify
  430. * message is not sent.
  431. */
  432. s->state=SSL3_ST_SR_FINISHED_A;
  433. s->init_num = 0;
  434. }
  435. else
  436. {
  437. s->state=SSL3_ST_SR_CERT_VRFY_A;
  438. s->init_num=0;
  439. /* We need to get hashes here so if there is
  440. * a client cert, it can be verified
  441. */
  442. s->method->ssl3_enc->cert_verify_mac(s,
  443. &(s->s3->finish_dgst1),
  444. &(s->s3->tmp.cert_verify_md[0]));
  445. s->method->ssl3_enc->cert_verify_mac(s,
  446. &(s->s3->finish_dgst2),
  447. &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
  448. }
  449. break;
  450. case SSL3_ST_SR_CERT_VRFY_A:
  451. case SSL3_ST_SR_CERT_VRFY_B:
  452. /* we should decide if we expected this one */
  453. ret=ssl3_get_cert_verify(s);
  454. if (ret <= 0) goto end;
  455. s->state=SSL3_ST_SR_FINISHED_A;
  456. s->init_num=0;
  457. break;
  458. case SSL3_ST_SR_FINISHED_A:
  459. case SSL3_ST_SR_FINISHED_B:
  460. ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
  461. SSL3_ST_SR_FINISHED_B);
  462. if (ret <= 0) goto end;
  463. if (s->hit)
  464. s->state=SSL_ST_OK;
  465. else
  466. s->state=SSL3_ST_SW_CHANGE_A;
  467. s->init_num=0;
  468. break;
  469. case SSL3_ST_SW_CHANGE_A:
  470. case SSL3_ST_SW_CHANGE_B:
  471. s->session->cipher=s->s3->tmp.new_cipher;
  472. if (!s->method->ssl3_enc->setup_key_block(s))
  473. { ret= -1; goto end; }
  474. ret=ssl3_send_change_cipher_spec(s,
  475. SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
  476. if (ret <= 0) goto end;
  477. s->state=SSL3_ST_SW_FINISHED_A;
  478. s->init_num=0;
  479. if (!s->method->ssl3_enc->change_cipher_state(s,
  480. SSL3_CHANGE_CIPHER_SERVER_WRITE))
  481. {
  482. ret= -1;
  483. goto end;
  484. }
  485. break;
  486. case SSL3_ST_SW_FINISHED_A:
  487. case SSL3_ST_SW_FINISHED_B:
  488. ret=ssl3_send_finished(s,
  489. SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
  490. s->method->ssl3_enc->server_finished_label,
  491. s->method->ssl3_enc->server_finished_label_len);
  492. if (ret <= 0) goto end;
  493. s->state=SSL3_ST_SW_FLUSH;
  494. if (s->hit)
  495. s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
  496. else
  497. s->s3->tmp.next_state=SSL_ST_OK;
  498. s->init_num=0;
  499. break;
  500. case SSL_ST_OK:
  501. /* clean a few things up */
  502. ssl3_cleanup_key_block(s);
  503. BUF_MEM_free(s->init_buf);
  504. s->init_buf=NULL;
  505. /* remove buffering on output */
  506. ssl_free_wbio_buffer(s);
  507. s->init_num=0;
  508. if (s->new_session == 2) /* skipped if we just sent a HelloRequest */
  509. {
  510. /* actually not necessarily a 'new' session unless
  511. * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
  512. s->new_session=0;
  513. ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
  514. s->ctx->stats.sess_accept_good++;
  515. /* s->server=1; */
  516. s->handshake_func=ssl3_accept;
  517. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  518. }
  519. ret = 1;
  520. goto end;
  521. /* break; */
  522. default:
  523. SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
  524. ret= -1;
  525. goto end;
  526. /* break; */
  527. }
  528. if (!s->s3->tmp.reuse_message && !skip)
  529. {
  530. if (s->debug)
  531. {
  532. if ((ret=BIO_flush(s->wbio)) <= 0)
  533. goto end;
  534. }
  535. if ((cb != NULL) && (s->state != state))
  536. {
  537. new_state=s->state;
  538. s->state=state;
  539. cb(s,SSL_CB_ACCEPT_LOOP,1);
  540. s->state=new_state;
  541. }
  542. }
  543. skip=0;
  544. }
  545. end:
  546. /* BIO_flush(s->wbio); */
  547. s->in_handshake--;
  548. if (cb != NULL)
  549. cb(s,SSL_CB_ACCEPT_EXIT,ret);
  550. return(ret);
  551. }
  552. int ssl3_send_hello_request(SSL *s)
  553. {
  554. unsigned char *p;
  555. if (s->state == SSL3_ST_SW_HELLO_REQ_A)
  556. {
  557. p=(unsigned char *)s->init_buf->data;
  558. *(p++)=SSL3_MT_HELLO_REQUEST;
  559. *(p++)=0;
  560. *(p++)=0;
  561. *(p++)=0;
  562. s->state=SSL3_ST_SW_HELLO_REQ_B;
  563. /* number of bytes to write */
  564. s->init_num=4;
  565. s->init_off=0;
  566. }
  567. /* SSL3_ST_SW_HELLO_REQ_B */
  568. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  569. }
  570. int ssl3_check_client_hello(SSL *s)
  571. {
  572. int ok;
  573. long n;
  574. /* this function is called when we really expect a Certificate message,
  575. * so permit appropriate message length */
  576. n=s->method->ssl_get_message(s,
  577. SSL3_ST_SR_CERT_A,
  578. SSL3_ST_SR_CERT_B,
  579. -1,
  580. s->max_cert_list,
  581. &ok);
  582. if (!ok) return((int)n);
  583. s->s3->tmp.reuse_message = 1;
  584. if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
  585. {
  586. /* Throw away what we have done so far in the current handshake,
  587. * which will now be aborted. (A full SSL_clear would be too much.)
  588. * I hope that tmp.dh is the only thing that may need to be cleared
  589. * when a handshake is not completed ... */
  590. #ifndef OPENSSL_NO_DH
  591. if (s->s3->tmp.dh != NULL)
  592. {
  593. DH_free(s->s3->tmp.dh);
  594. s->s3->tmp.dh = NULL;
  595. }
  596. #endif
  597. return 2;
  598. }
  599. return 1;
  600. }
  601. int ssl3_get_client_hello(SSL *s)
  602. {
  603. int i,j,ok,al,ret= -1;
  604. unsigned int cookie_len;
  605. long n;
  606. unsigned long id;
  607. unsigned char *p,*d,*q;
  608. SSL_CIPHER *c;
  609. SSL_COMP *comp=NULL;
  610. STACK_OF(SSL_CIPHER) *ciphers=NULL;
  611. /* We do this so that we will respond with our native type.
  612. * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
  613. * This down switching should be handled by a different method.
  614. * If we are SSLv3, we will respond with SSLv3, even if prompted with
  615. * TLSv1.
  616. */
  617. if (s->state == SSL3_ST_SR_CLNT_HELLO_A)
  618. {
  619. s->first_packet=1;
  620. s->state=SSL3_ST_SR_CLNT_HELLO_B;
  621. }
  622. n=s->method->ssl_get_message(s,
  623. SSL3_ST_SR_CLNT_HELLO_B,
  624. SSL3_ST_SR_CLNT_HELLO_C,
  625. SSL3_MT_CLIENT_HELLO,
  626. SSL3_RT_MAX_PLAIN_LENGTH,
  627. &ok);
  628. if (!ok) return((int)n);
  629. d=p=(unsigned char *)s->init_msg;
  630. /* use version from inside client hello, not from record header
  631. * (may differ: see RFC 2246, Appendix E, second paragraph) */
  632. s->client_version=(((int)p[0])<<8)|(int)p[1];
  633. p+=2;
  634. if (s->client_version < s->version)
  635. {
  636. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
  637. if ((s->client_version>>8) == SSL3_VERSION_MAJOR)
  638. {
  639. /* similar to ssl3_get_record, send alert using remote version number */
  640. s->version = s->client_version;
  641. }
  642. al = SSL_AD_PROTOCOL_VERSION;
  643. goto f_err;
  644. }
  645. /* load the client random */
  646. memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
  647. p+=SSL3_RANDOM_SIZE;
  648. /* get the session-id */
  649. j= *(p++);
  650. s->hit=0;
  651. /* Versions before 0.9.7 always allow session reuse during renegotiation
  652. * (i.e. when s->new_session is true), option
  653. * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7.
  654. * Maybe this optional behaviour should always have been the default,
  655. * but we cannot safely change the default behaviour (or new applications
  656. * might be written that become totally unsecure when compiled with
  657. * an earlier library version)
  658. */
  659. if (j == 0 || (s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)))
  660. {
  661. if (!ssl_get_new_session(s,1))
  662. goto err;
  663. }
  664. else
  665. {
  666. i=ssl_get_prev_session(s,p,j);
  667. if (i == 1)
  668. { /* previous session */
  669. s->hit=1;
  670. }
  671. else if (i == -1)
  672. goto err;
  673. else /* i == 0 */
  674. {
  675. if (!ssl_get_new_session(s,1))
  676. goto err;
  677. }
  678. }
  679. p+=j;
  680. if (SSL_version(s) == DTLS1_VERSION)
  681. {
  682. /* cookie stuff */
  683. cookie_len = *(p++);
  684. if ( (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) &&
  685. s->d1->send_cookie == 0)
  686. {
  687. /* HelloVerifyMessage has already been sent */
  688. if ( cookie_len != s->d1->cookie_len)
  689. {
  690. al = SSL_AD_HANDSHAKE_FAILURE;
  691. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
  692. goto f_err;
  693. }
  694. }
  695. /*
  696. * The ClientHello may contain a cookie even if the
  697. * HelloVerify message has not been sent--make sure that it
  698. * does not cause an overflow.
  699. */
  700. if ( cookie_len > sizeof(s->d1->rcvd_cookie))
  701. {
  702. /* too much data */
  703. al = SSL_AD_DECODE_ERROR;
  704. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
  705. goto f_err;
  706. }
  707. /* verify the cookie if appropriate option is set. */
  708. if ( (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) &&
  709. cookie_len > 0)
  710. {
  711. memcpy(s->d1->rcvd_cookie, p, cookie_len);
  712. if ( s->ctx->app_verify_cookie_cb != NULL)
  713. {
  714. if ( s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
  715. cookie_len) == 0)
  716. {
  717. al=SSL_AD_HANDSHAKE_FAILURE;
  718. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
  719. SSL_R_COOKIE_MISMATCH);
  720. goto f_err;
  721. }
  722. /* else cookie verification succeeded */
  723. }
  724. else if ( memcmp(s->d1->rcvd_cookie, s->d1->cookie,
  725. s->d1->cookie_len) != 0) /* default verification */
  726. {
  727. al=SSL_AD_HANDSHAKE_FAILURE;
  728. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
  729. SSL_R_COOKIE_MISMATCH);
  730. goto f_err;
  731. }
  732. }
  733. p += cookie_len;
  734. }
  735. n2s(p,i);
  736. if ((i == 0) && (j != 0))
  737. {
  738. /* we need a cipher if we are not resuming a session */
  739. al=SSL_AD_ILLEGAL_PARAMETER;
  740. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
  741. goto f_err;
  742. }
  743. if ((p+i) >= (d+n))
  744. {
  745. /* not enough data */
  746. al=SSL_AD_DECODE_ERROR;
  747. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
  748. goto f_err;
  749. }
  750. if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
  751. == NULL))
  752. {
  753. goto err;
  754. }
  755. p+=i;
  756. /* If it is a hit, check that the cipher is in the list */
  757. if ((s->hit) && (i > 0))
  758. {
  759. j=0;
  760. id=s->session->cipher->id;
  761. #ifdef CIPHER_DEBUG
  762. printf("client sent %d ciphers\n",sk_num(ciphers));
  763. #endif
  764. for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
  765. {
  766. c=sk_SSL_CIPHER_value(ciphers,i);
  767. #ifdef CIPHER_DEBUG
  768. printf("client [%2d of %2d]:%s\n",
  769. i,sk_num(ciphers),SSL_CIPHER_get_name(c));
  770. #endif
  771. if (c->id == id)
  772. {
  773. j=1;
  774. break;
  775. }
  776. }
  777. if (j == 0)
  778. {
  779. if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
  780. {
  781. /* Very bad for multi-threading.... */
  782. s->session->cipher=sk_SSL_CIPHER_value(ciphers, 0);
  783. }
  784. else
  785. {
  786. /* we need to have the cipher in the cipher
  787. * list if we are asked to reuse it */
  788. al=SSL_AD_ILLEGAL_PARAMETER;
  789. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
  790. goto f_err;
  791. }
  792. }
  793. }
  794. /* compression */
  795. i= *(p++);
  796. if ((p+i) > (d+n))
  797. {
  798. /* not enough data */
  799. al=SSL_AD_DECODE_ERROR;
  800. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
  801. goto f_err;
  802. }
  803. q=p;
  804. for (j=0; j<i; j++)
  805. {
  806. if (p[j] == 0) break;
  807. }
  808. p+=i;
  809. if (j >= i)
  810. {
  811. /* no compress */
  812. al=SSL_AD_DECODE_ERROR;
  813. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
  814. goto f_err;
  815. }
  816. /* Worst case, we will use the NULL compression, but if we have other
  817. * options, we will now look for them. We have i-1 compression
  818. * algorithms from the client, starting at q. */
  819. s->s3->tmp.new_compression=NULL;
  820. if (s->ctx->comp_methods != NULL)
  821. { /* See if we have a match */
  822. int m,nn,o,v,done=0;
  823. nn=sk_SSL_COMP_num(s->ctx->comp_methods);
  824. for (m=0; m<nn; m++)
  825. {
  826. comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
  827. v=comp->id;
  828. for (o=0; o<i; o++)
  829. {
  830. if (v == q[o])
  831. {
  832. done=1;
  833. break;
  834. }
  835. }
  836. if (done) break;
  837. }
  838. if (done)
  839. s->s3->tmp.new_compression=comp;
  840. else
  841. comp=NULL;
  842. }
  843. /* TLS does not mind if there is extra stuff */
  844. #if 0 /* SSL 3.0 does not mind either, so we should disable this test
  845. * (was enabled in 0.9.6d through 0.9.6j and 0.9.7 through 0.9.7b,
  846. * in earlier SSLeay/OpenSSL releases this test existed but was buggy) */
  847. if (s->version == SSL3_VERSION)
  848. {
  849. if (p < (d+n))
  850. {
  851. /* wrong number of bytes,
  852. * there could be more to follow */
  853. al=SSL_AD_DECODE_ERROR;
  854. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
  855. goto f_err;
  856. }
  857. }
  858. #endif
  859. /* Given s->session->ciphers and SSL_get_ciphers, we must
  860. * pick a cipher */
  861. if (!s->hit)
  862. {
  863. s->session->compress_meth=(comp == NULL)?0:comp->id;
  864. if (s->session->ciphers != NULL)
  865. sk_SSL_CIPHER_free(s->session->ciphers);
  866. s->session->ciphers=ciphers;
  867. if (ciphers == NULL)
  868. {
  869. al=SSL_AD_ILLEGAL_PARAMETER;
  870. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
  871. goto f_err;
  872. }
  873. ciphers=NULL;
  874. c=ssl3_choose_cipher(s,s->session->ciphers,
  875. SSL_get_ciphers(s));
  876. if (c == NULL)
  877. {
  878. al=SSL_AD_HANDSHAKE_FAILURE;
  879. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
  880. goto f_err;
  881. }
  882. s->s3->tmp.new_cipher=c;
  883. }
  884. else
  885. {
  886. /* Session-id reuse */
  887. #ifdef REUSE_CIPHER_BUG
  888. STACK_OF(SSL_CIPHER) *sk;
  889. SSL_CIPHER *nc=NULL;
  890. SSL_CIPHER *ec=NULL;
  891. if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
  892. {
  893. sk=s->session->ciphers;
  894. for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
  895. {
  896. c=sk_SSL_CIPHER_value(sk,i);
  897. if (c->algorithms & SSL_eNULL)
  898. nc=c;
  899. if (SSL_C_IS_EXPORT(c))
  900. ec=c;
  901. }
  902. if (nc != NULL)
  903. s->s3->tmp.new_cipher=nc;
  904. else if (ec != NULL)
  905. s->s3->tmp.new_cipher=ec;
  906. else
  907. s->s3->tmp.new_cipher=s->session->cipher;
  908. }
  909. else
  910. #endif
  911. s->s3->tmp.new_cipher=s->session->cipher;
  912. }
  913. /* we now have the following setup.
  914. * client_random
  915. * cipher_list - our prefered list of ciphers
  916. * ciphers - the clients prefered list of ciphers
  917. * compression - basically ignored right now
  918. * ssl version is set - sslv3
  919. * s->session - The ssl session has been setup.
  920. * s->hit - session reuse flag
  921. * s->tmp.new_cipher - the new cipher to use.
  922. */
  923. ret=1;
  924. if (0)
  925. {
  926. f_err:
  927. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  928. }
  929. err:
  930. if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
  931. return(ret);
  932. }
  933. int ssl3_send_server_hello(SSL *s)
  934. {
  935. unsigned char *buf;
  936. unsigned char *p,*d;
  937. int i,sl;
  938. unsigned long l,Time;
  939. if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
  940. {
  941. buf=(unsigned char *)s->init_buf->data;
  942. p=s->s3->server_random;
  943. Time=time(NULL); /* Time */
  944. l2n(Time,p);
  945. if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
  946. return -1;
  947. /* Do the message type and length last */
  948. d=p= &(buf[4]);
  949. *(p++)=s->version>>8;
  950. *(p++)=s->version&0xff;
  951. /* Random stuff */
  952. memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
  953. p+=SSL3_RANDOM_SIZE;
  954. /* now in theory we have 3 options to sending back the
  955. * session id. If it is a re-use, we send back the
  956. * old session-id, if it is a new session, we send
  957. * back the new session-id or we send back a 0 length
  958. * session-id if we want it to be single use.
  959. * Currently I will not implement the '0' length session-id
  960. * 12-Jan-98 - I'll now support the '0' length stuff.
  961. */
  962. if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
  963. s->session->session_id_length=0;
  964. sl=s->session->session_id_length;
  965. if (sl > (int)sizeof(s->session->session_id))
  966. {
  967. SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  968. return -1;
  969. }
  970. *(p++)=sl;
  971. memcpy(p,s->session->session_id,sl);
  972. p+=sl;
  973. /* put the cipher */
  974. i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
  975. p+=i;
  976. /* put the compression method */
  977. if (s->s3->tmp.new_compression == NULL)
  978. *(p++)=0;
  979. else
  980. *(p++)=s->s3->tmp.new_compression->id;
  981. /* do the header */
  982. l=(p-d);
  983. d=buf;
  984. *(d++)=SSL3_MT_SERVER_HELLO;
  985. l2n3(l,d);
  986. s->state=SSL3_ST_CW_CLNT_HELLO_B;
  987. /* number of bytes to write */
  988. s->init_num=p-buf;
  989. s->init_off=0;
  990. }
  991. /* SSL3_ST_CW_CLNT_HELLO_B */
  992. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  993. }
  994. int ssl3_send_server_done(SSL *s)
  995. {
  996. unsigned char *p;
  997. if (s->state == SSL3_ST_SW_SRVR_DONE_A)
  998. {
  999. p=(unsigned char *)s->init_buf->data;
  1000. /* do the header */
  1001. *(p++)=SSL3_MT_SERVER_DONE;
  1002. *(p++)=0;
  1003. *(p++)=0;
  1004. *(p++)=0;
  1005. s->state=SSL3_ST_SW_SRVR_DONE_B;
  1006. /* number of bytes to write */
  1007. s->init_num=4;
  1008. s->init_off=0;
  1009. }
  1010. /* SSL3_ST_CW_CLNT_HELLO_B */
  1011. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1012. }
  1013. int ssl3_send_server_key_exchange(SSL *s)
  1014. {
  1015. #ifndef OPENSSL_NO_RSA
  1016. unsigned char *q;
  1017. int j,num;
  1018. RSA *rsa;
  1019. unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  1020. unsigned int u;
  1021. #endif
  1022. #ifndef OPENSSL_NO_DH
  1023. DH *dh=NULL,*dhp;
  1024. #endif
  1025. #ifndef OPENSSL_NO_ECDH
  1026. EC_KEY *ecdh=NULL, *ecdhp;
  1027. unsigned char *encodedPoint = NULL;
  1028. int encodedlen = 0;
  1029. int curve_id = 0;
  1030. BN_CTX *bn_ctx = NULL;
  1031. #endif
  1032. EVP_PKEY *pkey;
  1033. unsigned char *p,*d;
  1034. int al,i;
  1035. unsigned long type;
  1036. int n;
  1037. CERT *cert;
  1038. BIGNUM *r[4];
  1039. int nr[4],kn;
  1040. BUF_MEM *buf;
  1041. EVP_MD_CTX md_ctx;
  1042. EVP_MD_CTX_init(&md_ctx);
  1043. if (s->state == SSL3_ST_SW_KEY_EXCH_A)
  1044. {
  1045. type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
  1046. cert=s->cert;
  1047. buf=s->init_buf;
  1048. r[0]=r[1]=r[2]=r[3]=NULL;
  1049. n=0;
  1050. #ifndef OPENSSL_NO_RSA
  1051. if (type & SSL_kRSA)
  1052. {
  1053. rsa=cert->rsa_tmp;
  1054. if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
  1055. {
  1056. rsa=s->cert->rsa_tmp_cb(s,
  1057. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  1058. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  1059. if(rsa == NULL)
  1060. {
  1061. al=SSL_AD_HANDSHAKE_FAILURE;
  1062. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
  1063. goto f_err;
  1064. }
  1065. RSA_up_ref(rsa);
  1066. cert->rsa_tmp=rsa;
  1067. }
  1068. if (rsa == NULL)
  1069. {
  1070. al=SSL_AD_HANDSHAKE_FAILURE;
  1071. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
  1072. goto f_err;
  1073. }
  1074. r[0]=rsa->n;
  1075. r[1]=rsa->e;
  1076. s->s3->tmp.use_rsa_tmp=1;
  1077. }
  1078. else
  1079. #endif
  1080. #ifndef OPENSSL_NO_DH
  1081. if (type & SSL_kEDH)
  1082. {
  1083. dhp=cert->dh_tmp;
  1084. if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
  1085. dhp=s->cert->dh_tmp_cb(s,
  1086. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  1087. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  1088. if (dhp == NULL)
  1089. {
  1090. al=SSL_AD_HANDSHAKE_FAILURE;
  1091. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
  1092. goto f_err;
  1093. }
  1094. if (s->s3->tmp.dh != NULL)
  1095. {
  1096. DH_free(dh);
  1097. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1098. goto err;
  1099. }
  1100. if ((dh=DHparams_dup(dhp)) == NULL)
  1101. {
  1102. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
  1103. goto err;
  1104. }
  1105. s->s3->tmp.dh=dh;
  1106. if ((dhp->pub_key == NULL ||
  1107. dhp->priv_key == NULL ||
  1108. (s->options & SSL_OP_SINGLE_DH_USE)))
  1109. {
  1110. if(!DH_generate_key(dh))
  1111. {
  1112. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  1113. ERR_R_DH_LIB);
  1114. goto err;
  1115. }
  1116. }
  1117. else
  1118. {
  1119. dh->pub_key=BN_dup(dhp->pub_key);
  1120. dh->priv_key=BN_dup(dhp->priv_key);
  1121. if ((dh->pub_key == NULL) ||
  1122. (dh->priv_key == NULL))
  1123. {
  1124. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
  1125. goto err;
  1126. }
  1127. }
  1128. r[0]=dh->p;
  1129. r[1]=dh->g;
  1130. r[2]=dh->pub_key;
  1131. }
  1132. else
  1133. #endif
  1134. #ifndef OPENSSL_NO_ECDH
  1135. if (type & SSL_kECDHE)
  1136. {
  1137. const EC_GROUP *group;
  1138. ecdhp=cert->ecdh_tmp;
  1139. if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL))
  1140. {
  1141. ecdhp=s->cert->ecdh_tmp_cb(s,
  1142. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  1143. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  1144. }
  1145. if (ecdhp == NULL)
  1146. {
  1147. al=SSL_AD_HANDSHAKE_FAILURE;
  1148. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
  1149. goto f_err;
  1150. }
  1151. if (s->s3->tmp.ecdh != NULL)
  1152. {
  1153. EC_KEY_free(s->s3->tmp.ecdh);
  1154. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1155. goto err;
  1156. }
  1157. /* Duplicate the ECDH structure. */
  1158. if (ecdhp == NULL)
  1159. {
  1160. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1161. goto err;
  1162. }
  1163. if (!EC_KEY_up_ref(ecdhp))
  1164. {
  1165. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1166. goto err;
  1167. }
  1168. ecdh = ecdhp;
  1169. s->s3->tmp.ecdh=ecdh;
  1170. if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
  1171. (EC_KEY_get0_private_key(ecdh) == NULL) ||
  1172. (s->options & SSL_OP_SINGLE_ECDH_USE))
  1173. {
  1174. if(!EC_KEY_generate_key(ecdh))
  1175. {
  1176. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1177. goto err;
  1178. }
  1179. }
  1180. if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
  1181. (EC_KEY_get0_public_key(ecdh) == NULL) ||
  1182. (EC_KEY_get0_private_key(ecdh) == NULL))
  1183. {
  1184. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1185. goto err;
  1186. }
  1187. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  1188. (EC_GROUP_get_degree(group) > 163))
  1189. {
  1190. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
  1191. goto err;
  1192. }
  1193. /* XXX: For now, we only support ephemeral ECDH
  1194. * keys over named (not generic) curves. For
  1195. * supported named curves, curve_id is non-zero.
  1196. */
  1197. if ((curve_id =
  1198. nid2curve_id(EC_GROUP_get_curve_name(group)))
  1199. == 0)
  1200. {
  1201. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
  1202. goto err;
  1203. }
  1204. /* Encode the public key.
  1205. * First check the size of encoding and
  1206. * allocate memory accordingly.
  1207. */
  1208. encodedlen = EC_POINT_point2oct(group,
  1209. EC_KEY_get0_public_key(ecdh),
  1210. POINT_CONVERSION_UNCOMPRESSED,
  1211. NULL, 0, NULL);
  1212. encodedPoint = (unsigned char *)
  1213. OPENSSL_malloc(encodedlen*sizeof(unsigned char));
  1214. bn_ctx = BN_CTX_new();
  1215. if ((encodedPoint == NULL) || (bn_ctx == NULL))
  1216. {
  1217. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1218. goto err;
  1219. }
  1220. encodedlen = EC_POINT_point2oct(group,
  1221. EC_KEY_get0_public_key(ecdh),
  1222. POINT_CONVERSION_UNCOMPRESSED,
  1223. encodedPoint, encodedlen, bn_ctx);
  1224. if (encodedlen == 0)
  1225. {
  1226. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1227. goto err;
  1228. }
  1229. BN_CTX_free(bn_ctx); bn_ctx=NULL;
  1230. /* XXX: For now, we only support named (not
  1231. * generic) curves in ECDH ephemeral key exchanges.
  1232. * In this situation, we need three additional bytes
  1233. * to encode the entire ServerECDHParams
  1234. * structure.
  1235. */
  1236. n = 3 + encodedlen;
  1237. /* We'll generate the serverKeyExchange message
  1238. * explicitly so we can set these to NULLs
  1239. */
  1240. r[0]=NULL;
  1241. r[1]=NULL;
  1242. r[2]=NULL;
  1243. }
  1244. else
  1245. #endif /* !OPENSSL_NO_ECDH */
  1246. {
  1247. al=SSL_AD_HANDSHAKE_FAILURE;
  1248. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  1249. goto f_err;
  1250. }
  1251. for (i=0; r[i] != NULL; i++)
  1252. {
  1253. nr[i]=BN_num_bytes(r[i]);
  1254. n+=2+nr[i];
  1255. }
  1256. if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
  1257. {
  1258. if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
  1259. == NULL)
  1260. {
  1261. al=SSL_AD_DECODE_ERROR;
  1262. goto f_err;
  1263. }
  1264. kn=EVP_PKEY_size(pkey);
  1265. }
  1266. else
  1267. {
  1268. pkey=NULL;
  1269. kn=0;
  1270. }
  1271. if (!BUF_MEM_grow_clean(buf,n+4+kn))
  1272. {
  1273. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
  1274. goto err;
  1275. }
  1276. d=(unsigned char *)s->init_buf->data;
  1277. p= &(d[4]);
  1278. for (i=0; r[i] != NULL; i++)
  1279. {
  1280. s2n(nr[i],p);
  1281. BN_bn2bin(r[i],p);
  1282. p+=nr[i];
  1283. }
  1284. #ifndef OPENSSL_NO_ECDH
  1285. if (type & SSL_kECDHE)
  1286. {
  1287. /* XXX: For now, we only support named (not generic) curves.
  1288. * In this situation, the serverKeyExchange message has:
  1289. * [1 byte CurveType], [1 byte CurveName]
  1290. * [1 byte length of encoded point], followed by
  1291. * the actual encoded point itself
  1292. */
  1293. *p = NAMED_CURVE_TYPE;
  1294. p += 1;
  1295. *p = curve_id;
  1296. p += 1;
  1297. *p = encodedlen;
  1298. p += 1;
  1299. memcpy((unsigned char*)p,
  1300. (unsigned char *)encodedPoint,
  1301. encodedlen);
  1302. OPENSSL_free(encodedPoint);
  1303. p += encodedlen;
  1304. }
  1305. #endif
  1306. /* not anonymous */
  1307. if (pkey != NULL)
  1308. {
  1309. /* n is the length of the params, they start at &(d[4])
  1310. * and p points to the space at the end. */
  1311. #ifndef OPENSSL_NO_RSA
  1312. if (pkey->type == EVP_PKEY_RSA)
  1313. {
  1314. q=md_buf;
  1315. j=0;
  1316. for (num=2; num > 0; num--)
  1317. {
  1318. EVP_DigestInit_ex(&md_ctx,(num == 2)
  1319. ?s->ctx->md5:s->ctx->sha1, NULL);
  1320. EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1321. EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1322. EVP_DigestUpdate(&md_ctx,&(d[4]),n);
  1323. EVP_DigestFinal_ex(&md_ctx,q,
  1324. (unsigned int *)&i);
  1325. q+=i;
  1326. j+=i;
  1327. }
  1328. if (RSA_sign(NID_md5_sha1, md_buf, j,
  1329. &(p[2]), &u, pkey->pkey.rsa) <= 0)
  1330. {
  1331. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
  1332. goto err;
  1333. }
  1334. s2n(u,p);
  1335. n+=u+2;
  1336. }
  1337. else
  1338. #endif
  1339. #if !defined(OPENSSL_NO_DSA)
  1340. if (pkey->type == EVP_PKEY_DSA)
  1341. {
  1342. /* lets do DSS */
  1343. EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL);
  1344. EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1345. EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1346. EVP_SignUpdate(&md_ctx,&(d[4]),n);
  1347. if (!EVP_SignFinal(&md_ctx,&(p[2]),
  1348. (unsigned int *)&i,pkey))
  1349. {
  1350. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
  1351. goto err;
  1352. }
  1353. s2n(i,p);
  1354. n+=i+2;
  1355. }
  1356. else
  1357. #endif
  1358. #if !defined(OPENSSL_NO_ECDSA)
  1359. if (pkey->type == EVP_PKEY_EC)
  1360. {
  1361. /* let's do ECDSA */
  1362. EVP_SignInit_ex(&md_ctx,EVP_ecdsa(), NULL);
  1363. EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1364. EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1365. EVP_SignUpdate(&md_ctx,&(d[4]),n);
  1366. if (!EVP_SignFinal(&md_ctx,&(p[2]),
  1367. (unsigned int *)&i,pkey))
  1368. {
  1369. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_ECDSA);
  1370. goto err;
  1371. }
  1372. s2n(i,p);
  1373. n+=i+2;
  1374. }
  1375. else
  1376. #endif
  1377. {
  1378. /* Is this error check actually needed? */
  1379. al=SSL_AD_HANDSHAKE_FAILURE;
  1380. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
  1381. goto f_err;
  1382. }
  1383. }
  1384. *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE;
  1385. l2n3(n,d);
  1386. /* we should now have things packed up, so lets send
  1387. * it off */
  1388. s->init_num=n+4;
  1389. s->init_off=0;
  1390. }
  1391. s->state = SSL3_ST_SW_KEY_EXCH_B;
  1392. EVP_MD_CTX_cleanup(&md_ctx);
  1393. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1394. f_err:
  1395. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1396. err:
  1397. #ifndef OPENSSL_NO_ECDH
  1398. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  1399. BN_CTX_free(bn_ctx);
  1400. #endif
  1401. EVP_MD_CTX_cleanup(&md_ctx);
  1402. return(-1);
  1403. }
  1404. int ssl3_send_certificate_request(SSL *s)
  1405. {
  1406. unsigned char *p,*d;
  1407. int i,j,nl,off,n;
  1408. STACK_OF(X509_NAME) *sk=NULL;
  1409. X509_NAME *name;
  1410. BUF_MEM *buf;
  1411. if (s->state == SSL3_ST_SW_CERT_REQ_A)
  1412. {
  1413. buf=s->init_buf;
  1414. d=p=(unsigned char *)&(buf->data[4]);
  1415. /* get the list of acceptable cert types */
  1416. p++;
  1417. n=ssl3_get_req_cert_type(s,p);
  1418. d[0]=n;
  1419. p+=n;
  1420. n++;
  1421. off=n;
  1422. p+=2;
  1423. n+=2;
  1424. sk=SSL_get_client_CA_list(s);
  1425. nl=0;
  1426. if (sk != NULL)
  1427. {
  1428. for (i=0; i<sk_X509_NAME_num(sk); i++)
  1429. {
  1430. name=sk_X509_NAME_value(sk,i);
  1431. j=i2d_X509_NAME(name,NULL);
  1432. if (!BUF_MEM_grow_clean(buf,4+n+j+2))
  1433. {
  1434. SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
  1435. goto err;
  1436. }
  1437. p=(unsigned char *)&(buf->data[4+n]);
  1438. if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  1439. {
  1440. s2n(j,p);
  1441. i2d_X509_NAME(name,&p);
  1442. n+=2+j;
  1443. nl+=2+j;
  1444. }
  1445. else
  1446. {
  1447. d=p;
  1448. i2d_X509_NAME(name,&p);
  1449. j-=2; s2n(j,d); j+=2;
  1450. n+=j;
  1451. nl+=j;
  1452. }
  1453. }
  1454. }
  1455. /* else no CA names */
  1456. p=(unsigned char *)&(buf->data[4+off]);
  1457. s2n(nl,p);
  1458. d=(unsigned char *)buf->data;
  1459. *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
  1460. l2n3(n,d);
  1461. /* we should now have things packed up, so lets send
  1462. * it off */
  1463. s->init_num=n+4;
  1464. s->init_off=0;
  1465. #ifdef NETSCAPE_HANG_BUG
  1466. p=(unsigned char *)s->init_buf->data + s->init_num;
  1467. /* do the header */
  1468. *(p++)=SSL3_MT_SERVER_DONE;
  1469. *(p++)=0;
  1470. *(p++)=0;
  1471. *(p++)=0;
  1472. s->init_num += 4;
  1473. #endif
  1474. s->state = SSL3_ST_SW_CERT_REQ_B;
  1475. }
  1476. /* SSL3_ST_SW_CERT_REQ_B */
  1477. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1478. err:
  1479. return(-1);
  1480. }
  1481. #ifndef OPENSSL_NO_ECDH
  1482. static const int KDF1_SHA1_len = 20;
  1483. static void *KDF1_SHA1(const void *in, size_t inlen, void *out, size_t *outlen)
  1484. {
  1485. #ifndef OPENSSL_NO_SHA
  1486. if (*outlen < SHA_DIGEST_LENGTH)
  1487. return NULL;
  1488. else
  1489. *outlen = SHA_DIGEST_LENGTH;
  1490. return SHA1(in, inlen, out);
  1491. #else
  1492. return NULL;
  1493. #endif /* OPENSSL_NO_SHA */
  1494. }
  1495. #endif /* OPENSSL_NO_ECDH */
  1496. int ssl3_get_client_key_exchange(SSL *s)
  1497. {
  1498. int i,al,ok;
  1499. long n;
  1500. unsigned long l;
  1501. unsigned char *p;
  1502. #ifndef OPENSSL_NO_RSA
  1503. RSA *rsa=NULL;
  1504. EVP_PKEY *pkey=NULL;
  1505. #endif
  1506. #ifndef OPENSSL_NO_DH
  1507. BIGNUM *pub=NULL;
  1508. DH *dh_srvr;
  1509. #endif
  1510. #ifndef OPENSSL_NO_KRB5
  1511. KSSL_ERR kssl_err;
  1512. #endif /* OPENSSL_NO_KRB5 */
  1513. #ifndef OPENSSL_NO_ECDH
  1514. EC_KEY *srvr_ecdh = NULL;
  1515. EVP_PKEY *clnt_pub_pkey = NULL;
  1516. EC_POINT *clnt_ecpoint = NULL;
  1517. BN_CTX *bn_ctx = NULL;
  1518. #endif
  1519. n=s->method->ssl_get_message(s,
  1520. SSL3_ST_SR_KEY_EXCH_A,
  1521. SSL3_ST_SR_KEY_EXCH_B,
  1522. SSL3_MT_CLIENT_KEY_EXCHANGE,
  1523. 2048, /* ??? */
  1524. &ok);
  1525. if (!ok) return((int)n);
  1526. p=(unsigned char *)s->init_msg;
  1527. l=s->s3->tmp.new_cipher->algorithms;
  1528. #ifndef OPENSSL_NO_RSA
  1529. if (l & SSL_kRSA)
  1530. {
  1531. /* FIX THIS UP EAY EAY EAY EAY */
  1532. if (s->s3->tmp.use_rsa_tmp)
  1533. {
  1534. if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
  1535. rsa=s->cert->rsa_tmp;
  1536. /* Don't do a callback because rsa_tmp should
  1537. * be sent already */
  1538. if (rsa == NULL)
  1539. {
  1540. al=SSL_AD_HANDSHAKE_FAILURE;
  1541. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
  1542. goto f_err;
  1543. }
  1544. }
  1545. else
  1546. {
  1547. pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
  1548. if ( (pkey == NULL) ||
  1549. (pkey->type != EVP_PKEY_RSA) ||
  1550. (pkey->pkey.rsa == NULL))
  1551. {
  1552. al=SSL_AD_HANDSHAKE_FAILURE;
  1553. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
  1554. goto f_err;
  1555. }
  1556. rsa=pkey->pkey.rsa;
  1557. }
  1558. /* TLS */
  1559. if (s->version > SSL3_VERSION)
  1560. {
  1561. n2s(p,i);
  1562. if (n != i+2)
  1563. {
  1564. if (!(s->options & SSL_OP_TLS_D5_BUG))
  1565. {
  1566. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
  1567. goto err;
  1568. }
  1569. else
  1570. p-=2;
  1571. }
  1572. else
  1573. n=i;
  1574. }
  1575. i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
  1576. al = -1;
  1577. if (i != SSL_MAX_MASTER_KEY_LENGTH)
  1578. {
  1579. al=SSL_AD_DECODE_ERROR;
  1580. /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); */
  1581. }
  1582. if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
  1583. {
  1584. /* The premaster secret must contain the same version number as the
  1585. * ClientHello to detect version rollback attacks (strangely, the
  1586. * protocol does not offer such protection for DH ciphersuites).
  1587. * However, buggy clients exist that send the negotiated protocol
  1588. * version instead if the server does not support the requested
  1589. * protocol version.
  1590. * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
  1591. if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
  1592. (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
  1593. {
  1594. al=SSL_AD_DECODE_ERROR;
  1595. /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); */
  1596. /* The Klima-Pokorny-Rosa extension of Bleichenbacher's attack
  1597. * (http://eprint.iacr.org/2003/052/) exploits the version
  1598. * number check as a "bad version oracle" -- an alert would
  1599. * reveal that the plaintext corresponding to some ciphertext
  1600. * made up by the adversary is properly formatted except
  1601. * that the version number is wrong. To avoid such attacks,
  1602. * we should treat this just like any other decryption error. */
  1603. }
  1604. }
  1605. if (al != -1)
  1606. {
  1607. /* Some decryption failure -- use random value instead as countermeasure
  1608. * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding
  1609. * (see RFC 2246, section 7.4.7.1). */
  1610. ERR_clear_error();
  1611. i = SSL_MAX_MASTER_KEY_LENGTH;
  1612. p[0] = s->client_version >> 8;
  1613. p[1] = s->client_version & 0xff;
  1614. if (RAND_pseudo_bytes(p+2, i-2) <= 0) /* should be RAND_bytes, but we cannot work around a failure */
  1615. goto err;
  1616. }
  1617. s->session->master_key_length=
  1618. s->method->ssl3_enc->generate_master_secret(s,
  1619. s->session->master_key,
  1620. p,i);
  1621. OPENSSL_cleanse(p,i);
  1622. }
  1623. else
  1624. #endif
  1625. #ifndef OPENSSL_NO_DH
  1626. if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  1627. {
  1628. n2s(p,i);
  1629. if (n != i+2)
  1630. {
  1631. if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
  1632. {
  1633. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
  1634. goto err;
  1635. }
  1636. else
  1637. {
  1638. p-=2;
  1639. i=(int)n;
  1640. }
  1641. }
  1642. if (n == 0L) /* the parameters are in the cert */
  1643. {
  1644. al=SSL_AD_HANDSHAKE_FAILURE;
  1645. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
  1646. goto f_err;
  1647. }
  1648. else
  1649. {
  1650. if (s->s3->tmp.dh == NULL)
  1651. {
  1652. al=SSL_AD_HANDSHAKE_FAILURE;
  1653. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
  1654. goto f_err;
  1655. }
  1656. else
  1657. dh_srvr=s->s3->tmp.dh;
  1658. }
  1659. pub=BN_bin2bn(p,i,NULL);
  1660. if (pub == NULL)
  1661. {
  1662. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
  1663. goto err;
  1664. }
  1665. i=DH_compute_key(p,pub,dh_srvr);
  1666. if (i <= 0)
  1667. {
  1668. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  1669. goto err;
  1670. }
  1671. DH_free(s->s3->tmp.dh);
  1672. s->s3->tmp.dh=NULL;
  1673. BN_clear_free(pub);
  1674. pub=NULL;
  1675. s->session->master_key_length=
  1676. s->method->ssl3_enc->generate_master_secret(s,
  1677. s->session->master_key,p,i);
  1678. OPENSSL_cleanse(p,i);
  1679. }
  1680. else
  1681. #endif
  1682. #ifndef OPENSSL_NO_KRB5
  1683. if (l & SSL_kKRB5)
  1684. {
  1685. krb5_error_code krb5rc;
  1686. krb5_data enc_ticket;
  1687. krb5_data authenticator;
  1688. krb5_data enc_pms;
  1689. KSSL_CTX *kssl_ctx = s->kssl_ctx;
  1690. EVP_CIPHER_CTX ciph_ctx;
  1691. EVP_CIPHER *enc = NULL;
  1692. unsigned char iv[EVP_MAX_IV_LENGTH];
  1693. unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH
  1694. + EVP_MAX_BLOCK_LENGTH];
  1695. int padl, outl;
  1696. krb5_timestamp authtime = 0;
  1697. krb5_ticket_times ttimes;
  1698. EVP_CIPHER_CTX_init(&ciph_ctx);
  1699. if (!kssl_ctx) kssl_ctx = kssl_ctx_new();
  1700. n2s(p,i);
  1701. enc_ticket.length = i;
  1702. if (n < enc_ticket.length + 6)
  1703. {
  1704. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1705. SSL_R_DATA_LENGTH_TOO_LONG);
  1706. goto err;
  1707. }
  1708. enc_ticket.data = (char *)p;
  1709. p+=enc_ticket.length;
  1710. n2s(p,i);
  1711. authenticator.length = i;
  1712. if (n < enc_ticket.length + authenticator.length + 6)
  1713. {
  1714. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1715. SSL_R_DATA_LENGTH_TOO_LONG);
  1716. goto err;
  1717. }
  1718. authenticator.data = (char *)p;
  1719. p+=authenticator.length;
  1720. n2s(p,i);
  1721. enc_pms.length = i;
  1722. enc_pms.data = (char *)p;
  1723. p+=enc_pms.length;
  1724. /* Note that the length is checked again below,
  1725. ** after decryption
  1726. */
  1727. if(enc_pms.length > sizeof pms)
  1728. {
  1729. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1730. SSL_R_DATA_LENGTH_TOO_LONG);
  1731. goto err;
  1732. }
  1733. if (n != (long)(enc_ticket.length + authenticator.length +
  1734. enc_pms.length + 6))
  1735. {
  1736. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1737. SSL_R_DATA_LENGTH_TOO_LONG);
  1738. goto err;
  1739. }
  1740. if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
  1741. &kssl_err)) != 0)
  1742. {
  1743. #ifdef KSSL_DEBUG
  1744. printf("kssl_sget_tkt rtn %d [%d]\n",
  1745. krb5rc, kssl_err.reason);
  1746. if (kssl_err.text)
  1747. printf("kssl_err text= %s\n", kssl_err.text);
  1748. #endif /* KSSL_DEBUG */
  1749. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1750. kssl_err.reason);
  1751. goto err;
  1752. }
  1753. /* Note: no authenticator is not considered an error,
  1754. ** but will return authtime == 0.
  1755. */
  1756. if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
  1757. &authtime, &kssl_err)) != 0)
  1758. {
  1759. #ifdef KSSL_DEBUG
  1760. printf("kssl_check_authent rtn %d [%d]\n",
  1761. krb5rc, kssl_err.reason);
  1762. if (kssl_err.text)
  1763. printf("kssl_err text= %s\n", kssl_err.text);
  1764. #endif /* KSSL_DEBUG */
  1765. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1766. kssl_err.reason);
  1767. goto err;
  1768. }
  1769. if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)
  1770. {
  1771. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);
  1772. goto err;
  1773. }
  1774. #ifdef KSSL_DEBUG
  1775. kssl_ctx_show(kssl_ctx);
  1776. #endif /* KSSL_DEBUG */
  1777. enc = kssl_map_enc(kssl_ctx->enctype);
  1778. if (enc == NULL)
  1779. goto err;
  1780. memset(iv, 0, sizeof iv); /* per RFC 1510 */
  1781. if (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv))
  1782. {
  1783. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1784. SSL_R_DECRYPTION_FAILED);
  1785. goto err;
  1786. }
  1787. if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl,
  1788. (unsigned char *)enc_pms.data, enc_pms.length))
  1789. {
  1790. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1791. SSL_R_DECRYPTION_FAILED);
  1792. goto err;
  1793. }
  1794. if (outl > SSL_MAX_MASTER_KEY_LENGTH)
  1795. {
  1796. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1797. SSL_R_DATA_LENGTH_TOO_LONG);
  1798. goto err;
  1799. }
  1800. if (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl))
  1801. {
  1802. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1803. SSL_R_DECRYPTION_FAILED);
  1804. goto err;
  1805. }
  1806. outl += padl;
  1807. if (outl > SSL_MAX_MASTER_KEY_LENGTH)
  1808. {
  1809. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1810. SSL_R_DATA_LENGTH_TOO_LONG);
  1811. goto err;
  1812. }
  1813. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  1814. s->session->master_key_length=
  1815. s->method->ssl3_enc->generate_master_secret(s,
  1816. s->session->master_key, pms, outl);
  1817. if (kssl_ctx->client_princ)
  1818. {
  1819. int len = strlen(kssl_ctx->client_princ);
  1820. if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH )
  1821. {
  1822. s->session->krb5_client_princ_len = len;
  1823. memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);
  1824. }
  1825. }
  1826. /* Was doing kssl_ctx_free() here,
  1827. ** but it caused problems for apache.
  1828. ** kssl_ctx = kssl_ctx_free(kssl_ctx);
  1829. ** if (s->kssl_ctx) s->kssl_ctx = NULL;
  1830. */
  1831. }
  1832. else
  1833. #endif /* OPENSSL_NO_KRB5 */
  1834. #ifndef OPENSSL_NO_ECDH
  1835. if ((l & SSL_kECDH) || (l & SSL_kECDHE))
  1836. {
  1837. int ret = 1;
  1838. int field_size = 0;
  1839. const EC_KEY *tkey;
  1840. const EC_GROUP *group;
  1841. const BIGNUM *priv_key;
  1842. /* initialize structures for server's ECDH key pair */
  1843. if ((srvr_ecdh = EC_KEY_new()) == NULL)
  1844. {
  1845. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1846. ERR_R_MALLOC_FAILURE);
  1847. goto err;
  1848. }
  1849. /* Let's get server private key and group information */
  1850. if (l & SSL_kECDH)
  1851. {
  1852. /* use the certificate */
  1853. tkey = s->cert->key->privatekey->pkey.ec;
  1854. }
  1855. else
  1856. {
  1857. /* use the ephermeral values we saved when
  1858. * generating the ServerKeyExchange msg.
  1859. */
  1860. tkey = s->s3->tmp.ecdh;
  1861. }
  1862. group = EC_KEY_get0_group(tkey);
  1863. priv_key = EC_KEY_get0_private_key(tkey);
  1864. if (!EC_KEY_set_group(srvr_ecdh, group) ||
  1865. !EC_KEY_set_private_key(srvr_ecdh, priv_key))
  1866. {
  1867. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1868. ERR_R_EC_LIB);
  1869. goto err;
  1870. }
  1871. /* Let's get client's public key */
  1872. if ((clnt_ecpoint = EC_POINT_new(group)) == NULL)
  1873. {
  1874. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1875. ERR_R_MALLOC_FAILURE);
  1876. goto err;
  1877. }
  1878. if (n == 0L)
  1879. {
  1880. /* Client Publickey was in Client Certificate */
  1881. if (l & SSL_kECDHE)
  1882. {
  1883. al=SSL_AD_HANDSHAKE_FAILURE;
  1884. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
  1885. goto f_err;
  1886. }
  1887. if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer))
  1888. == NULL) ||
  1889. (clnt_pub_pkey->type != EVP_PKEY_EC))
  1890. {
  1891. /* XXX: For now, we do not support client
  1892. * authentication using ECDH certificates
  1893. * so this branch (n == 0L) of the code is
  1894. * never executed. When that support is
  1895. * added, we ought to ensure the key
  1896. * received in the certificate is
  1897. * authorized for key agreement.
  1898. * ECDH_compute_key implicitly checks that
  1899. * the two ECDH shares are for the same
  1900. * group.
  1901. */
  1902. al=SSL_AD_HANDSHAKE_FAILURE;
  1903. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1904. SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
  1905. goto f_err;
  1906. }
  1907. EC_POINT_copy(clnt_ecpoint,
  1908. EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec));
  1909. ret = 2; /* Skip certificate verify processing */
  1910. }
  1911. else
  1912. {
  1913. /* Get client's public key from encoded point
  1914. * in the ClientKeyExchange message.
  1915. */
  1916. if ((bn_ctx = BN_CTX_new()) == NULL)
  1917. {
  1918. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1919. ERR_R_MALLOC_FAILURE);
  1920. goto err;
  1921. }
  1922. /* Get encoded point length */
  1923. i = *p;
  1924. p += 1;
  1925. if (EC_POINT_oct2point(group,
  1926. clnt_ecpoint, p, i, bn_ctx) == 0)
  1927. {
  1928. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1929. ERR_R_EC_LIB);
  1930. goto err;
  1931. }
  1932. /* p is pointing to somewhere in the buffer
  1933. * currently, so set it to the start
  1934. */
  1935. p=(unsigned char *)s->init_buf->data;
  1936. }
  1937. /* Compute the shared pre-master secret */
  1938. field_size = EC_GROUP_get_degree(group);
  1939. if (field_size <= 0)
  1940. {
  1941. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1942. ERR_R_ECDH_LIB);
  1943. goto err;
  1944. }
  1945. /* If field size is not more than 24 octets, then use SHA-1 hash of result;
  1946. * otherwise, use result (see section 4.8 of draft-ietf-tls-ecc-03.txt;
  1947. * this is new with this version of the Internet Draft).
  1948. */
  1949. if (field_size <= 24 * 8)
  1950. i = ECDH_compute_key(p, KDF1_SHA1_len, clnt_ecpoint, srvr_ecdh, KDF1_SHA1);
  1951. else
  1952. i = ECDH_compute_key(p, (field_size+7)/8, clnt_ecpoint, srvr_ecdh, NULL);
  1953. if (i <= 0)
  1954. {
  1955. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1956. ERR_R_ECDH_LIB);
  1957. goto err;
  1958. }
  1959. EVP_PKEY_free(clnt_pub_pkey);
  1960. EC_POINT_free(clnt_ecpoint);
  1961. if (srvr_ecdh != NULL)
  1962. EC_KEY_free(srvr_ecdh);
  1963. BN_CTX_free(bn_ctx);
  1964. /* Compute the master secret */
  1965. s->session->master_key_length = s->method->ssl3_enc-> \
  1966. generate_master_secret(s, s->session->master_key, p, i);
  1967. OPENSSL_cleanse(p, i);
  1968. return (ret);
  1969. }
  1970. else
  1971. #endif
  1972. {
  1973. al=SSL_AD_HANDSHAKE_FAILURE;
  1974. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1975. SSL_R_UNKNOWN_CIPHER_TYPE);
  1976. goto f_err;
  1977. }
  1978. return(1);
  1979. f_err:
  1980. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1981. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH)
  1982. err:
  1983. #endif
  1984. #ifndef OPENSSL_NO_ECDH
  1985. EVP_PKEY_free(clnt_pub_pkey);
  1986. EC_POINT_free(clnt_ecpoint);
  1987. if (srvr_ecdh != NULL)
  1988. EC_KEY_free(srvr_ecdh);
  1989. BN_CTX_free(bn_ctx);
  1990. #endif
  1991. return(-1);
  1992. }
  1993. int ssl3_get_cert_verify(SSL *s)
  1994. {
  1995. EVP_PKEY *pkey=NULL;
  1996. unsigned char *p;
  1997. int al,ok,ret=0;
  1998. long n;
  1999. int type=0,i,j;
  2000. X509 *peer;
  2001. n=s->method->ssl_get_message(s,
  2002. SSL3_ST_SR_CERT_VRFY_A,
  2003. SSL3_ST_SR_CERT_VRFY_B,
  2004. -1,
  2005. 514, /* 514? */
  2006. &ok);
  2007. if (!ok) return((int)n);
  2008. if (s->session->peer != NULL)
  2009. {
  2010. peer=s->session->peer;
  2011. pkey=X509_get_pubkey(peer);
  2012. type=X509_certificate_type(peer,pkey);
  2013. }
  2014. else
  2015. {
  2016. peer=NULL;
  2017. pkey=NULL;
  2018. }
  2019. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
  2020. {
  2021. s->s3->tmp.reuse_message=1;
  2022. if ((peer != NULL) && (type | EVP_PKT_SIGN))
  2023. {
  2024. al=SSL_AD_UNEXPECTED_MESSAGE;
  2025. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
  2026. goto f_err;
  2027. }
  2028. ret=1;
  2029. goto end;
  2030. }
  2031. if (peer == NULL)
  2032. {
  2033. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
  2034. al=SSL_AD_UNEXPECTED_MESSAGE;
  2035. goto f_err;
  2036. }
  2037. if (!(type & EVP_PKT_SIGN))
  2038. {
  2039. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
  2040. al=SSL_AD_ILLEGAL_PARAMETER;
  2041. goto f_err;
  2042. }
  2043. if (s->s3->change_cipher_spec)
  2044. {
  2045. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
  2046. al=SSL_AD_UNEXPECTED_MESSAGE;
  2047. goto f_err;
  2048. }
  2049. /* we now have a signature that we need to verify */
  2050. p=(unsigned char *)s->init_msg;
  2051. n2s(p,i);
  2052. n-=2;
  2053. if (i > n)
  2054. {
  2055. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
  2056. al=SSL_AD_DECODE_ERROR;
  2057. goto f_err;
  2058. }
  2059. j=EVP_PKEY_size(pkey);
  2060. if ((i > j) || (n > j) || (n <= 0))
  2061. {
  2062. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
  2063. al=SSL_AD_DECODE_ERROR;
  2064. goto f_err;
  2065. }
  2066. #ifndef OPENSSL_NO_RSA
  2067. if (pkey->type == EVP_PKEY_RSA)
  2068. {
  2069. i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
  2070. MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i,
  2071. pkey->pkey.rsa);
  2072. if (i < 0)
  2073. {
  2074. al=SSL_AD_DECRYPT_ERROR;
  2075. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
  2076. goto f_err;
  2077. }
  2078. if (i == 0)
  2079. {
  2080. al=SSL_AD_DECRYPT_ERROR;
  2081. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
  2082. goto f_err;
  2083. }
  2084. }
  2085. else
  2086. #endif
  2087. #ifndef OPENSSL_NO_DSA
  2088. if (pkey->type == EVP_PKEY_DSA)
  2089. {
  2090. j=DSA_verify(pkey->save_type,
  2091. &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
  2092. SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
  2093. if (j <= 0)
  2094. {
  2095. /* bad signature */
  2096. al=SSL_AD_DECRYPT_ERROR;
  2097. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
  2098. goto f_err;
  2099. }
  2100. }
  2101. else
  2102. #endif
  2103. #ifndef OPENSSL_NO_ECDSA
  2104. if (pkey->type == EVP_PKEY_EC)
  2105. {
  2106. j=ECDSA_verify(pkey->save_type,
  2107. &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
  2108. SHA_DIGEST_LENGTH,p,i,pkey->pkey.ec);
  2109. if (j <= 0)
  2110. {
  2111. /* bad signature */
  2112. al=SSL_AD_DECRYPT_ERROR;
  2113. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
  2114. SSL_R_BAD_ECDSA_SIGNATURE);
  2115. goto f_err;
  2116. }
  2117. }
  2118. else
  2119. #endif
  2120. {
  2121. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR);
  2122. al=SSL_AD_UNSUPPORTED_CERTIFICATE;
  2123. goto f_err;
  2124. }
  2125. ret=1;
  2126. if (0)
  2127. {
  2128. f_err:
  2129. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  2130. }
  2131. end:
  2132. EVP_PKEY_free(pkey);
  2133. return(ret);
  2134. }
  2135. int ssl3_get_client_certificate(SSL *s)
  2136. {
  2137. int i,ok,al,ret= -1;
  2138. X509 *x=NULL;
  2139. unsigned long l,nc,llen,n;
  2140. const unsigned char *p,*q;
  2141. unsigned char *d;
  2142. STACK_OF(X509) *sk=NULL;
  2143. n=s->method->ssl_get_message(s,
  2144. SSL3_ST_SR_CERT_A,
  2145. SSL3_ST_SR_CERT_B,
  2146. -1,
  2147. s->max_cert_list,
  2148. &ok);
  2149. if (!ok) return((int)n);
  2150. if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
  2151. {
  2152. if ( (s->verify_mode & SSL_VERIFY_PEER) &&
  2153. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  2154. {
  2155. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  2156. al=SSL_AD_HANDSHAKE_FAILURE;
  2157. goto f_err;
  2158. }
  2159. /* If tls asked for a client cert, the client must return a 0 list */
  2160. if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
  2161. {
  2162. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
  2163. al=SSL_AD_UNEXPECTED_MESSAGE;
  2164. goto f_err;
  2165. }
  2166. s->s3->tmp.reuse_message=1;
  2167. return(1);
  2168. }
  2169. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
  2170. {
  2171. al=SSL_AD_UNEXPECTED_MESSAGE;
  2172. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
  2173. goto f_err;
  2174. }
  2175. p=d=(unsigned char *)s->init_msg;
  2176. if ((sk=sk_X509_new_null()) == NULL)
  2177. {
  2178. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  2179. goto err;
  2180. }
  2181. n2l3(p,llen);
  2182. if (llen+3 != n)
  2183. {
  2184. al=SSL_AD_DECODE_ERROR;
  2185. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
  2186. goto f_err;
  2187. }
  2188. for (nc=0; nc<llen; )
  2189. {
  2190. n2l3(p,l);
  2191. if ((l+nc+3) > llen)
  2192. {
  2193. al=SSL_AD_DECODE_ERROR;
  2194. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  2195. goto f_err;
  2196. }
  2197. q=p;
  2198. x=d2i_X509(NULL,&p,l);
  2199. if (x == NULL)
  2200. {
  2201. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
  2202. goto err;
  2203. }
  2204. if (p != (q+l))
  2205. {
  2206. al=SSL_AD_DECODE_ERROR;
  2207. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  2208. goto f_err;
  2209. }
  2210. if (!sk_X509_push(sk,x))
  2211. {
  2212. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  2213. goto err;
  2214. }
  2215. x=NULL;
  2216. nc+=l+3;
  2217. }
  2218. if (sk_X509_num(sk) <= 0)
  2219. {
  2220. /* TLS does not mind 0 certs returned */
  2221. if (s->version == SSL3_VERSION)
  2222. {
  2223. al=SSL_AD_HANDSHAKE_FAILURE;
  2224. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
  2225. goto f_err;
  2226. }
  2227. /* Fail for TLS only if we required a certificate */
  2228. else if ((s->verify_mode & SSL_VERIFY_PEER) &&
  2229. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  2230. {
  2231. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  2232. al=SSL_AD_HANDSHAKE_FAILURE;
  2233. goto f_err;
  2234. }
  2235. }
  2236. else
  2237. {
  2238. i=ssl_verify_cert_chain(s,sk);
  2239. if (!i)
  2240. {
  2241. al=ssl_verify_alarm_type(s->verify_result);
  2242. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
  2243. goto f_err;
  2244. }
  2245. }
  2246. if (s->session->peer != NULL) /* This should not be needed */
  2247. X509_free(s->session->peer);
  2248. s->session->peer=sk_X509_shift(sk);
  2249. s->session->verify_result = s->verify_result;
  2250. /* With the current implementation, sess_cert will always be NULL
  2251. * when we arrive here. */
  2252. if (s->session->sess_cert == NULL)
  2253. {
  2254. s->session->sess_cert = ssl_sess_cert_new();
  2255. if (s->session->sess_cert == NULL)
  2256. {
  2257. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  2258. goto err;
  2259. }
  2260. }
  2261. if (s->session->sess_cert->cert_chain != NULL)
  2262. sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
  2263. s->session->sess_cert->cert_chain=sk;
  2264. /* Inconsistency alert: cert_chain does *not* include the
  2265. * peer's own certificate, while we do include it in s3_clnt.c */
  2266. sk=NULL;
  2267. ret=1;
  2268. if (0)
  2269. {
  2270. f_err:
  2271. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  2272. }
  2273. err:
  2274. if (x != NULL) X509_free(x);
  2275. if (sk != NULL) sk_X509_pop_free(sk,X509_free);
  2276. return(ret);
  2277. }
  2278. int ssl3_send_server_certificate(SSL *s)
  2279. {
  2280. unsigned long l;
  2281. X509 *x;
  2282. if (s->state == SSL3_ST_SW_CERT_A)
  2283. {
  2284. x=ssl_get_server_send_cert(s);
  2285. if (x == NULL &&
  2286. /* VRS: allow null cert if auth == KRB5 */
  2287. (s->s3->tmp.new_cipher->algorithms
  2288. & (SSL_MKEY_MASK|SSL_AUTH_MASK))
  2289. != (SSL_aKRB5|SSL_kKRB5))
  2290. {
  2291. SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
  2292. return(0);
  2293. }
  2294. l=ssl3_output_cert_chain(s,x);
  2295. s->state=SSL3_ST_SW_CERT_B;
  2296. s->init_num=(int)l;
  2297. s->init_off=0;
  2298. }
  2299. /* SSL3_ST_SW_CERT_B */
  2300. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2301. }
  2302. #ifndef OPENSSL_NO_ECDH
  2303. /* This is the complement of curve_id2nid in s3_clnt.c. */
  2304. static int nid2curve_id(int nid)
  2305. {
  2306. /* ECC curves from draft-ietf-tls-ecc-01.txt (Mar 15, 2001)
  2307. * (no changes in draft-ietf-tls-ecc-03.txt [June 2003]) */
  2308. switch (nid) {
  2309. case NID_sect163k1: /* sect163k1 (1) */
  2310. return 1;
  2311. case NID_sect163r1: /* sect163r1 (2) */
  2312. return 2;
  2313. case NID_sect163r2: /* sect163r2 (3) */
  2314. return 3;
  2315. case NID_sect193r1: /* sect193r1 (4) */
  2316. return 4;
  2317. case NID_sect193r2: /* sect193r2 (5) */
  2318. return 5;
  2319. case NID_sect233k1: /* sect233k1 (6) */
  2320. return 6;
  2321. case NID_sect233r1: /* sect233r1 (7) */
  2322. return 7;
  2323. case NID_sect239k1: /* sect239k1 (8) */
  2324. return 8;
  2325. case NID_sect283k1: /* sect283k1 (9) */
  2326. return 9;
  2327. case NID_sect283r1: /* sect283r1 (10) */
  2328. return 10;
  2329. case NID_sect409k1: /* sect409k1 (11) */
  2330. return 11;
  2331. case NID_sect409r1: /* sect409r1 (12) */
  2332. return 12;
  2333. case NID_sect571k1: /* sect571k1 (13) */
  2334. return 13;
  2335. case NID_sect571r1: /* sect571r1 (14) */
  2336. return 14;
  2337. case NID_secp160k1: /* secp160k1 (15) */
  2338. return 15;
  2339. case NID_secp160r1: /* secp160r1 (16) */
  2340. return 16;
  2341. case NID_secp160r2: /* secp160r2 (17) */
  2342. return 17;
  2343. case NID_secp192k1: /* secp192k1 (18) */
  2344. return 18;
  2345. case NID_X9_62_prime192v1: /* secp192r1 (19) */
  2346. return 19;
  2347. case NID_secp224k1: /* secp224k1 (20) */
  2348. return 20;
  2349. case NID_secp224r1: /* secp224r1 (21) */
  2350. return 21;
  2351. case NID_secp256k1: /* secp256k1 (22) */
  2352. return 22;
  2353. case NID_X9_62_prime256v1: /* secp256r1 (23) */
  2354. return 23;
  2355. case NID_secp384r1: /* secp384r1 (24) */
  2356. return 24;
  2357. case NID_secp521r1: /* secp521r1 (25) */
  2358. return 25;
  2359. default:
  2360. return 0;
  2361. }
  2362. }
  2363. #endif