s3_clnt.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397
  1. /* ssl/s3_clnt.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-2002 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. #include <stdio.h>
  125. #include "ssl_locl.h"
  126. #include "kssl_lcl.h"
  127. #include <openssl/buffer.h>
  128. #include <openssl/rand.h>
  129. #include <openssl/objects.h>
  130. #include <openssl/evp.h>
  131. #include <openssl/md5.h>
  132. #include "cryptlib.h"
  133. static SSL_METHOD *ssl3_get_client_method(int ver);
  134. static int ssl3_client_hello(SSL *s);
  135. static int ssl3_get_server_hello(SSL *s);
  136. static int ssl3_get_certificate_request(SSL *s);
  137. static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b);
  138. static int ssl3_get_server_done(SSL *s);
  139. static int ssl3_send_client_verify(SSL *s);
  140. static int ssl3_send_client_certificate(SSL *s);
  141. static int ssl3_send_client_key_exchange(SSL *s);
  142. static int ssl3_get_key_exchange(SSL *s);
  143. static int ssl3_get_server_certificate(SSL *s);
  144. static int ssl3_check_cert_and_algorithm(SSL *s);
  145. #ifndef OPENSSL_NO_ECDH
  146. static int curve_id2nid(int curve_id);
  147. int check_srvr_ecc_cert_and_alg(X509 *x, SSL_CIPHER *cs);
  148. #endif
  149. static SSL_METHOD *ssl3_get_client_method(int ver)
  150. {
  151. if (ver == SSL3_VERSION)
  152. return(SSLv3_client_method());
  153. else
  154. return(NULL);
  155. }
  156. SSL_METHOD *SSLv3_client_method(void)
  157. {
  158. static int init=1;
  159. static SSL_METHOD SSLv3_client_data;
  160. if (init)
  161. {
  162. CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD);
  163. if (init)
  164. {
  165. memcpy((char *)&SSLv3_client_data,(char *)sslv3_base_method(),
  166. sizeof(SSL_METHOD));
  167. SSLv3_client_data.ssl_connect=ssl3_connect;
  168. SSLv3_client_data.get_ssl_method=ssl3_get_client_method;
  169. init=0;
  170. }
  171. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD);
  172. }
  173. return(&SSLv3_client_data);
  174. }
  175. int ssl3_connect(SSL *s)
  176. {
  177. BUF_MEM *buf;
  178. unsigned long Time=time(NULL),l;
  179. long num1;
  180. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  181. int ret= -1;
  182. int new_state,state,skip=0;;
  183. RAND_add(&Time,sizeof(Time),0);
  184. ERR_clear_error();
  185. clear_sys_error();
  186. if (s->info_callback != NULL)
  187. cb=s->info_callback;
  188. else if (s->ctx->info_callback != NULL)
  189. cb=s->ctx->info_callback;
  190. s->in_handshake++;
  191. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  192. for (;;)
  193. {
  194. state=s->state;
  195. switch(s->state)
  196. {
  197. case SSL_ST_RENEGOTIATE:
  198. s->new_session=1;
  199. s->state=SSL_ST_CONNECT;
  200. s->ctx->stats.sess_connect_renegotiate++;
  201. /* break */
  202. case SSL_ST_BEFORE:
  203. case SSL_ST_CONNECT:
  204. case SSL_ST_BEFORE|SSL_ST_CONNECT:
  205. case SSL_ST_OK|SSL_ST_CONNECT:
  206. s->server=0;
  207. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  208. if ((s->version & 0xff00 ) != 0x0300)
  209. {
  210. SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
  211. ret = -1;
  212. goto end;
  213. }
  214. /* s->version=SSL3_VERSION; */
  215. s->type=SSL_ST_CONNECT;
  216. if (s->init_buf == NULL)
  217. {
  218. if ((buf=BUF_MEM_new()) == NULL)
  219. {
  220. ret= -1;
  221. goto end;
  222. }
  223. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  224. {
  225. ret= -1;
  226. goto end;
  227. }
  228. s->init_buf=buf;
  229. }
  230. if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
  231. /* setup buffing BIO */
  232. if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
  233. /* don't push the buffering BIO quite yet */
  234. ssl3_init_finished_mac(s);
  235. s->state=SSL3_ST_CW_CLNT_HELLO_A;
  236. s->ctx->stats.sess_connect++;
  237. s->init_num=0;
  238. break;
  239. case SSL3_ST_CW_CLNT_HELLO_A:
  240. case SSL3_ST_CW_CLNT_HELLO_B:
  241. s->shutdown=0;
  242. ret=ssl3_client_hello(s);
  243. if (ret <= 0) goto end;
  244. s->state=SSL3_ST_CR_SRVR_HELLO_A;
  245. s->init_num=0;
  246. /* turn on buffering for the next lot of output */
  247. if (s->bbio != s->wbio)
  248. s->wbio=BIO_push(s->bbio,s->wbio);
  249. break;
  250. case SSL3_ST_CR_SRVR_HELLO_A:
  251. case SSL3_ST_CR_SRVR_HELLO_B:
  252. ret=ssl3_get_server_hello(s);
  253. if (ret <= 0) goto end;
  254. if (s->hit)
  255. s->state=SSL3_ST_CR_FINISHED_A;
  256. else
  257. s->state=SSL3_ST_CR_CERT_A;
  258. s->init_num=0;
  259. break;
  260. case SSL3_ST_CR_CERT_A:
  261. case SSL3_ST_CR_CERT_B:
  262. /* Check if it is anon DH/ECDH */
  263. if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
  264. {
  265. ret=ssl3_get_server_certificate(s);
  266. if (ret <= 0) goto end;
  267. }
  268. else
  269. skip=1;
  270. s->state=SSL3_ST_CR_KEY_EXCH_A;
  271. s->init_num=0;
  272. break;
  273. case SSL3_ST_CR_KEY_EXCH_A:
  274. case SSL3_ST_CR_KEY_EXCH_B:
  275. ret=ssl3_get_key_exchange(s);
  276. if (ret <= 0) goto end;
  277. s->state=SSL3_ST_CR_CERT_REQ_A;
  278. s->init_num=0;
  279. /* at this point we check that we have the
  280. * required stuff from the server */
  281. if (!ssl3_check_cert_and_algorithm(s))
  282. {
  283. ret= -1;
  284. goto end;
  285. }
  286. break;
  287. case SSL3_ST_CR_CERT_REQ_A:
  288. case SSL3_ST_CR_CERT_REQ_B:
  289. ret=ssl3_get_certificate_request(s);
  290. if (ret <= 0) goto end;
  291. s->state=SSL3_ST_CR_SRVR_DONE_A;
  292. s->init_num=0;
  293. break;
  294. case SSL3_ST_CR_SRVR_DONE_A:
  295. case SSL3_ST_CR_SRVR_DONE_B:
  296. ret=ssl3_get_server_done(s);
  297. if (ret <= 0) goto end;
  298. if (s->s3->tmp.cert_req)
  299. s->state=SSL3_ST_CW_CERT_A;
  300. else
  301. s->state=SSL3_ST_CW_KEY_EXCH_A;
  302. s->init_num=0;
  303. break;
  304. case SSL3_ST_CW_CERT_A:
  305. case SSL3_ST_CW_CERT_B:
  306. case SSL3_ST_CW_CERT_C:
  307. case SSL3_ST_CW_CERT_D:
  308. ret=ssl3_send_client_certificate(s);
  309. if (ret <= 0) goto end;
  310. s->state=SSL3_ST_CW_KEY_EXCH_A;
  311. s->init_num=0;
  312. break;
  313. case SSL3_ST_CW_KEY_EXCH_A:
  314. case SSL3_ST_CW_KEY_EXCH_B:
  315. ret=ssl3_send_client_key_exchange(s);
  316. if (ret <= 0) goto end;
  317. l=s->s3->tmp.new_cipher->algorithms;
  318. /* EAY EAY EAY need to check for DH fix cert
  319. * sent back */
  320. /* For TLS, cert_req is set to 2, so a cert chain
  321. * of nothing is sent, but no verify packet is sent */
  322. /* XXX: For now, we do not support client
  323. * authentication in ECDH cipher suites with
  324. * ECDH (rather than ECDSA) certificates.
  325. * We need to skip the certificate verify
  326. * message when client's ECDH public key is sent
  327. * inside the client certificate.
  328. */
  329. if (s->s3->tmp.cert_req == 1)
  330. {
  331. s->state=SSL3_ST_CW_CERT_VRFY_A;
  332. }
  333. else
  334. {
  335. s->state=SSL3_ST_CW_CHANGE_A;
  336. s->s3->change_cipher_spec=0;
  337. }
  338. s->init_num=0;
  339. break;
  340. case SSL3_ST_CW_CERT_VRFY_A:
  341. case SSL3_ST_CW_CERT_VRFY_B:
  342. ret=ssl3_send_client_verify(s);
  343. if (ret <= 0) goto end;
  344. s->state=SSL3_ST_CW_CHANGE_A;
  345. s->init_num=0;
  346. s->s3->change_cipher_spec=0;
  347. break;
  348. case SSL3_ST_CW_CHANGE_A:
  349. case SSL3_ST_CW_CHANGE_B:
  350. ret=ssl3_send_change_cipher_spec(s,
  351. SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
  352. if (ret <= 0) goto end;
  353. s->state=SSL3_ST_CW_FINISHED_A;
  354. s->init_num=0;
  355. s->session->cipher=s->s3->tmp.new_cipher;
  356. if (s->s3->tmp.new_compression == NULL)
  357. s->session->compress_meth=0;
  358. else
  359. s->session->compress_meth=
  360. s->s3->tmp.new_compression->id;
  361. if (!s->method->ssl3_enc->setup_key_block(s))
  362. {
  363. ret= -1;
  364. goto end;
  365. }
  366. if (!s->method->ssl3_enc->change_cipher_state(s,
  367. SSL3_CHANGE_CIPHER_CLIENT_WRITE))
  368. {
  369. ret= -1;
  370. goto end;
  371. }
  372. break;
  373. case SSL3_ST_CW_FINISHED_A:
  374. case SSL3_ST_CW_FINISHED_B:
  375. ret=ssl3_send_finished(s,
  376. SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
  377. s->method->ssl3_enc->client_finished_label,
  378. s->method->ssl3_enc->client_finished_label_len);
  379. if (ret <= 0) goto end;
  380. s->state=SSL3_ST_CW_FLUSH;
  381. /* clear flags */
  382. s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
  383. if (s->hit)
  384. {
  385. s->s3->tmp.next_state=SSL_ST_OK;
  386. if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
  387. {
  388. s->state=SSL_ST_OK;
  389. s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
  390. s->s3->delay_buf_pop_ret=0;
  391. }
  392. }
  393. else
  394. {
  395. s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
  396. }
  397. s->init_num=0;
  398. break;
  399. case SSL3_ST_CR_FINISHED_A:
  400. case SSL3_ST_CR_FINISHED_B:
  401. ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
  402. SSL3_ST_CR_FINISHED_B);
  403. if (ret <= 0) goto end;
  404. if (s->hit)
  405. s->state=SSL3_ST_CW_CHANGE_A;
  406. else
  407. s->state=SSL_ST_OK;
  408. s->init_num=0;
  409. break;
  410. case SSL3_ST_CW_FLUSH:
  411. /* number of bytes to be flushed */
  412. num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
  413. if (num1 > 0)
  414. {
  415. s->rwstate=SSL_WRITING;
  416. num1=BIO_flush(s->wbio);
  417. if (num1 <= 0) { ret= -1; goto end; }
  418. s->rwstate=SSL_NOTHING;
  419. }
  420. s->state=s->s3->tmp.next_state;
  421. break;
  422. case SSL_ST_OK:
  423. /* clean a few things up */
  424. ssl3_cleanup_key_block(s);
  425. if (s->init_buf != NULL)
  426. {
  427. BUF_MEM_free(s->init_buf);
  428. s->init_buf=NULL;
  429. }
  430. /* If we are not 'joining' the last two packets,
  431. * remove the buffering now */
  432. if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
  433. ssl_free_wbio_buffer(s);
  434. /* else do it later in ssl3_write */
  435. s->init_num=0;
  436. s->new_session=0;
  437. ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
  438. if (s->hit) s->ctx->stats.sess_hit++;
  439. ret=1;
  440. /* s->server=0; */
  441. s->handshake_func=ssl3_connect;
  442. s->ctx->stats.sess_connect_good++;
  443. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  444. goto end;
  445. /* break; */
  446. default:
  447. SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
  448. ret= -1;
  449. goto end;
  450. /* break; */
  451. }
  452. /* did we do anything */
  453. if (!s->s3->tmp.reuse_message && !skip)
  454. {
  455. if (s->debug)
  456. {
  457. if ((ret=BIO_flush(s->wbio)) <= 0)
  458. goto end;
  459. }
  460. if ((cb != NULL) && (s->state != state))
  461. {
  462. new_state=s->state;
  463. s->state=state;
  464. cb(s,SSL_CB_CONNECT_LOOP,1);
  465. s->state=new_state;
  466. }
  467. }
  468. skip=0;
  469. }
  470. end:
  471. s->in_handshake--;
  472. if (cb != NULL)
  473. cb(s,SSL_CB_CONNECT_EXIT,ret);
  474. return(ret);
  475. }
  476. static int ssl3_client_hello(SSL *s)
  477. {
  478. unsigned char *buf;
  479. unsigned char *p,*d;
  480. int i,j;
  481. unsigned long Time,l;
  482. SSL_COMP *comp;
  483. buf=(unsigned char *)s->init_buf->data;
  484. if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
  485. {
  486. if ((s->session == NULL) ||
  487. (s->session->ssl_version != s->version) ||
  488. (s->session->not_resumable))
  489. {
  490. if (!ssl_get_new_session(s,0))
  491. goto err;
  492. }
  493. /* else use the pre-loaded session */
  494. p=s->s3->client_random;
  495. Time=time(NULL); /* Time */
  496. l2n(Time,p);
  497. RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
  498. /* Do the message type and length last */
  499. d=p= &(buf[4]);
  500. *(p++)=s->version>>8;
  501. *(p++)=s->version&0xff;
  502. s->client_version=s->version;
  503. /* Random stuff */
  504. memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
  505. p+=SSL3_RANDOM_SIZE;
  506. /* Session ID */
  507. if (s->new_session)
  508. i=0;
  509. else
  510. i=s->session->session_id_length;
  511. *(p++)=i;
  512. if (i != 0)
  513. {
  514. if (i > sizeof s->session->session_id)
  515. {
  516. SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  517. goto err;
  518. }
  519. memcpy(p,s->session->session_id,i);
  520. p+=i;
  521. }
  522. /* Ciphers supported */
  523. i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]));
  524. if (i == 0)
  525. {
  526. SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
  527. goto err;
  528. }
  529. s2n(i,p);
  530. p+=i;
  531. /* COMPRESSION */
  532. if (s->ctx->comp_methods == NULL)
  533. j=0;
  534. else
  535. j=sk_SSL_COMP_num(s->ctx->comp_methods);
  536. *(p++)=1+j;
  537. for (i=0; i<j; i++)
  538. {
  539. comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
  540. *(p++)=comp->id;
  541. }
  542. *(p++)=0; /* Add the NULL method */
  543. l=(p-d);
  544. d=buf;
  545. *(d++)=SSL3_MT_CLIENT_HELLO;
  546. l2n3(l,d);
  547. s->state=SSL3_ST_CW_CLNT_HELLO_B;
  548. /* number of bytes to write */
  549. s->init_num=p-buf;
  550. s->init_off=0;
  551. }
  552. /* SSL3_ST_CW_CLNT_HELLO_B */
  553. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  554. err:
  555. return(-1);
  556. }
  557. static int ssl3_get_server_hello(SSL *s)
  558. {
  559. STACK_OF(SSL_CIPHER) *sk;
  560. SSL_CIPHER *c;
  561. unsigned char *p,*d;
  562. int i,al,ok;
  563. unsigned int j;
  564. long n;
  565. SSL_COMP *comp;
  566. n=ssl3_get_message(s,
  567. SSL3_ST_CR_SRVR_HELLO_A,
  568. SSL3_ST_CR_SRVR_HELLO_B,
  569. SSL3_MT_SERVER_HELLO,
  570. 300, /* ?? */
  571. &ok);
  572. if (!ok) return((int)n);
  573. d=p=(unsigned char *)s->init_msg;
  574. if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
  575. {
  576. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
  577. s->version=(s->version&0xff00)|p[1];
  578. al=SSL_AD_PROTOCOL_VERSION;
  579. goto f_err;
  580. }
  581. p+=2;
  582. /* load the server hello data */
  583. /* load the server random */
  584. memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
  585. p+=SSL3_RANDOM_SIZE;
  586. /* get the session-id */
  587. j= *(p++);
  588. if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE))
  589. {
  590. al=SSL_AD_ILLEGAL_PARAMETER;
  591. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_LONG);
  592. goto f_err;
  593. }
  594. if (j != 0 && j == s->session->session_id_length
  595. && memcmp(p,s->session->session_id,j) == 0)
  596. {
  597. if(s->sid_ctx_length != s->session->sid_ctx_length
  598. || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
  599. {
  600. /* actually a client application bug */
  601. al=SSL_AD_ILLEGAL_PARAMETER;
  602. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  603. goto f_err;
  604. }
  605. s->hit=1;
  606. }
  607. else /* a miss or crap from the other end */
  608. {
  609. /* If we were trying for session-id reuse, make a new
  610. * SSL_SESSION so we don't stuff up other people */
  611. s->hit=0;
  612. if (s->session->session_id_length > 0)
  613. {
  614. if (!ssl_get_new_session(s,0))
  615. {
  616. al=SSL_AD_INTERNAL_ERROR;
  617. goto f_err;
  618. }
  619. }
  620. s->session->session_id_length=j;
  621. memcpy(s->session->session_id,p,j); /* j could be 0 */
  622. }
  623. p+=j;
  624. c=ssl_get_cipher_by_char(s,p);
  625. if (c == NULL)
  626. {
  627. /* unknown cipher */
  628. al=SSL_AD_ILLEGAL_PARAMETER;
  629. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
  630. goto f_err;
  631. }
  632. p+=ssl_put_cipher_by_char(s,NULL,NULL);
  633. sk=ssl_get_ciphers_by_id(s);
  634. i=sk_SSL_CIPHER_find(sk,c);
  635. if (i < 0)
  636. {
  637. /* we did not say we would use this cipher */
  638. al=SSL_AD_ILLEGAL_PARAMETER;
  639. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
  640. goto f_err;
  641. }
  642. /* Depending on the session caching (internal/external), the cipher
  643. and/or cipher_id values may not be set. Make sure that
  644. cipher_id is set and use it for comparison. */
  645. if (s->session->cipher)
  646. s->session->cipher_id = s->session->cipher->id;
  647. if (s->hit && (s->session->cipher_id != c->id))
  648. {
  649. if (!(s->options &
  650. SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
  651. {
  652. al=SSL_AD_ILLEGAL_PARAMETER;
  653. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  654. goto f_err;
  655. }
  656. }
  657. s->s3->tmp.new_cipher=c;
  658. /* lets get the compression algorithm */
  659. /* COMPRESSION */
  660. j= *(p++);
  661. if (j == 0)
  662. comp=NULL;
  663. else
  664. comp=ssl3_comp_find(s->ctx->comp_methods,j);
  665. if ((j != 0) && (comp == NULL))
  666. {
  667. al=SSL_AD_ILLEGAL_PARAMETER;
  668. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  669. goto f_err;
  670. }
  671. else
  672. {
  673. s->s3->tmp.new_compression=comp;
  674. }
  675. if (p != (d+n))
  676. {
  677. /* wrong packet length */
  678. al=SSL_AD_DECODE_ERROR;
  679. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
  680. goto err;
  681. }
  682. return(1);
  683. f_err:
  684. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  685. err:
  686. return(-1);
  687. }
  688. static int ssl3_get_server_certificate(SSL *s)
  689. {
  690. int al,i,ok,ret= -1;
  691. unsigned long n,nc,llen,l;
  692. X509 *x=NULL;
  693. unsigned char *p,*d,*q;
  694. STACK_OF(X509) *sk=NULL;
  695. SESS_CERT *sc;
  696. EVP_PKEY *pkey=NULL;
  697. int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
  698. n=ssl3_get_message(s,
  699. SSL3_ST_CR_CERT_A,
  700. SSL3_ST_CR_CERT_B,
  701. -1,
  702. s->max_cert_list,
  703. &ok);
  704. if (!ok) return((int)n);
  705. if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE)
  706. {
  707. s->s3->tmp.reuse_message=1;
  708. return(1);
  709. }
  710. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
  711. {
  712. al=SSL_AD_UNEXPECTED_MESSAGE;
  713. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
  714. goto f_err;
  715. }
  716. d=p=(unsigned char *)s->init_msg;
  717. if ((sk=sk_X509_new_null()) == NULL)
  718. {
  719. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  720. goto err;
  721. }
  722. n2l3(p,llen);
  723. if (llen+3 != n)
  724. {
  725. al=SSL_AD_DECODE_ERROR;
  726. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
  727. goto f_err;
  728. }
  729. for (nc=0; nc<llen; )
  730. {
  731. n2l3(p,l);
  732. if ((l+nc+3) > llen)
  733. {
  734. al=SSL_AD_DECODE_ERROR;
  735. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  736. goto f_err;
  737. }
  738. q=p;
  739. x=d2i_X509(NULL,&q,l);
  740. if (x == NULL)
  741. {
  742. al=SSL_AD_BAD_CERTIFICATE;
  743. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
  744. goto f_err;
  745. }
  746. if (q != (p+l))
  747. {
  748. al=SSL_AD_DECODE_ERROR;
  749. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  750. goto f_err;
  751. }
  752. if (!sk_X509_push(sk,x))
  753. {
  754. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  755. goto err;
  756. }
  757. x=NULL;
  758. nc+=l+3;
  759. p=q;
  760. }
  761. i=ssl_verify_cert_chain(s,sk);
  762. if ((s->verify_mode != SSL_VERIFY_NONE) && (!i)
  763. #ifndef OPENSSL_NO_KRB5
  764. && (s->s3->tmp.new_cipher->algorithms & (SSL_MKEY_MASK|SSL_AUTH_MASK))
  765. != (SSL_aKRB5|SSL_kKRB5)
  766. #endif /* OPENSSL_NO_KRB5 */
  767. )
  768. {
  769. al=ssl_verify_alarm_type(s->verify_result);
  770. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
  771. goto f_err;
  772. }
  773. ERR_clear_error(); /* but we keep s->verify_result */
  774. sc=ssl_sess_cert_new();
  775. if (sc == NULL) goto err;
  776. if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
  777. s->session->sess_cert=sc;
  778. sc->cert_chain=sk;
  779. /* Inconsistency alert: cert_chain does include the peer's
  780. * certificate, which we don't include in s3_srvr.c */
  781. x=sk_X509_value(sk,0);
  782. sk=NULL;
  783. /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
  784. pkey=X509_get_pubkey(x);
  785. /* VRS: allow null cert if auth == KRB5 */
  786. need_cert = ((s->s3->tmp.new_cipher->algorithms
  787. & (SSL_MKEY_MASK|SSL_AUTH_MASK))
  788. == (SSL_aKRB5|SSL_kKRB5))? 0: 1;
  789. #ifdef KSSL_DEBUG
  790. printf("pkey,x = %p, %p\n", pkey,x);
  791. printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
  792. printf("cipher, alg, nc = %s, %lx, %d\n", s->s3->tmp.new_cipher->name,
  793. s->s3->tmp.new_cipher->algorithms, need_cert);
  794. #endif /* KSSL_DEBUG */
  795. if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
  796. {
  797. x=NULL;
  798. al=SSL3_AL_FATAL;
  799. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  800. SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
  801. goto f_err;
  802. }
  803. i=ssl_cert_type(x,pkey);
  804. if (need_cert && i < 0)
  805. {
  806. x=NULL;
  807. al=SSL3_AL_FATAL;
  808. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  809. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  810. goto f_err;
  811. }
  812. if (need_cert)
  813. {
  814. sc->peer_cert_type=i;
  815. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  816. /* Why would the following ever happen?
  817. * We just created sc a couple of lines ago. */
  818. if (sc->peer_pkeys[i].x509 != NULL)
  819. X509_free(sc->peer_pkeys[i].x509);
  820. sc->peer_pkeys[i].x509=x;
  821. sc->peer_key= &(sc->peer_pkeys[i]);
  822. if (s->session->peer != NULL)
  823. X509_free(s->session->peer);
  824. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  825. s->session->peer=x;
  826. }
  827. else
  828. {
  829. sc->peer_cert_type=i;
  830. sc->peer_key= NULL;
  831. if (s->session->peer != NULL)
  832. X509_free(s->session->peer);
  833. s->session->peer=NULL;
  834. }
  835. s->session->verify_result = s->verify_result;
  836. x=NULL;
  837. ret=1;
  838. if (0)
  839. {
  840. f_err:
  841. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  842. }
  843. err:
  844. EVP_PKEY_free(pkey);
  845. X509_free(x);
  846. sk_X509_pop_free(sk,X509_free);
  847. return(ret);
  848. }
  849. static int ssl3_get_key_exchange(SSL *s)
  850. {
  851. #ifndef OPENSSL_NO_RSA
  852. unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
  853. #endif
  854. EVP_MD_CTX md_ctx;
  855. unsigned char *param,*p;
  856. int al,i,j,param_len,ok;
  857. long n,alg;
  858. EVP_PKEY *pkey=NULL;
  859. #ifndef OPENSSL_NO_RSA
  860. RSA *rsa=NULL;
  861. #endif
  862. #ifndef OPENSSL_NO_DH
  863. DH *dh=NULL;
  864. #endif
  865. #ifndef OPENSSL_NO_ECDH
  866. EC_KEY *ecdh = NULL;
  867. BN_CTX *bn_ctx = NULL;
  868. EC_POINT *srvr_ecpoint = NULL;
  869. int curve_nid = 0;
  870. int encoded_pt_len = 0;
  871. #endif
  872. /* use same message size as in ssl3_get_certificate_request()
  873. * as ServerKeyExchange message may be skipped */
  874. n=ssl3_get_message(s,
  875. SSL3_ST_CR_KEY_EXCH_A,
  876. SSL3_ST_CR_KEY_EXCH_B,
  877. -1,
  878. s->max_cert_list,
  879. &ok);
  880. if (!ok) return((int)n);
  881. if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
  882. {
  883. s->s3->tmp.reuse_message=1;
  884. return(1);
  885. }
  886. param=p=(unsigned char *)s->init_msg;
  887. if (s->session->sess_cert != NULL)
  888. {
  889. #ifndef OPENSSL_NO_RSA
  890. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  891. {
  892. RSA_free(s->session->sess_cert->peer_rsa_tmp);
  893. s->session->sess_cert->peer_rsa_tmp=NULL;
  894. }
  895. #endif
  896. #ifndef OPENSSL_NO_DH
  897. if (s->session->sess_cert->peer_dh_tmp)
  898. {
  899. DH_free(s->session->sess_cert->peer_dh_tmp);
  900. s->session->sess_cert->peer_dh_tmp=NULL;
  901. }
  902. #endif
  903. #ifndef OPENSSL_NO_ECDH
  904. if (s->session->sess_cert->peer_ecdh_tmp)
  905. {
  906. EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
  907. s->session->sess_cert->peer_ecdh_tmp=NULL;
  908. }
  909. #endif
  910. }
  911. else
  912. {
  913. s->session->sess_cert=ssl_sess_cert_new();
  914. }
  915. param_len=0;
  916. alg=s->s3->tmp.new_cipher->algorithms;
  917. EVP_MD_CTX_init(&md_ctx);
  918. #ifndef OPENSSL_NO_RSA
  919. if (alg & SSL_kRSA)
  920. {
  921. if ((rsa=RSA_new()) == NULL)
  922. {
  923. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  924. goto err;
  925. }
  926. n2s(p,i);
  927. param_len=i+2;
  928. if (param_len > n)
  929. {
  930. al=SSL_AD_DECODE_ERROR;
  931. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
  932. goto f_err;
  933. }
  934. if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
  935. {
  936. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  937. goto err;
  938. }
  939. p+=i;
  940. n2s(p,i);
  941. param_len+=i+2;
  942. if (param_len > n)
  943. {
  944. al=SSL_AD_DECODE_ERROR;
  945. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
  946. goto f_err;
  947. }
  948. if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
  949. {
  950. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  951. goto err;
  952. }
  953. p+=i;
  954. n-=param_len;
  955. /* this should be because we are using an export cipher */
  956. if (alg & SSL_aRSA)
  957. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  958. else
  959. {
  960. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  961. goto err;
  962. }
  963. s->session->sess_cert->peer_rsa_tmp=rsa;
  964. rsa=NULL;
  965. }
  966. #else /* OPENSSL_NO_RSA */
  967. if (0)
  968. ;
  969. #endif
  970. #ifndef OPENSSL_NO_DH
  971. else if (alg & SSL_kEDH)
  972. {
  973. if ((dh=DH_new()) == NULL)
  974. {
  975. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
  976. goto err;
  977. }
  978. n2s(p,i);
  979. param_len=i+2;
  980. if (param_len > n)
  981. {
  982. al=SSL_AD_DECODE_ERROR;
  983. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
  984. goto f_err;
  985. }
  986. if (!(dh->p=BN_bin2bn(p,i,NULL)))
  987. {
  988. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  989. goto err;
  990. }
  991. p+=i;
  992. n2s(p,i);
  993. param_len+=i+2;
  994. if (param_len > n)
  995. {
  996. al=SSL_AD_DECODE_ERROR;
  997. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
  998. goto f_err;
  999. }
  1000. if (!(dh->g=BN_bin2bn(p,i,NULL)))
  1001. {
  1002. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1003. goto err;
  1004. }
  1005. p+=i;
  1006. n2s(p,i);
  1007. param_len+=i+2;
  1008. if (param_len > n)
  1009. {
  1010. al=SSL_AD_DECODE_ERROR;
  1011. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
  1012. goto f_err;
  1013. }
  1014. if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
  1015. {
  1016. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1017. goto err;
  1018. }
  1019. p+=i;
  1020. n-=param_len;
  1021. #ifndef OPENSSL_NO_RSA
  1022. if (alg & SSL_aRSA)
  1023. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1024. #else
  1025. if (0)
  1026. ;
  1027. #endif
  1028. #ifndef OPENSSL_NO_DSA
  1029. else if (alg & SSL_aDSS)
  1030. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
  1031. #endif
  1032. /* else anonymous DH, so no certificate or pkey. */
  1033. s->session->sess_cert->peer_dh_tmp=dh;
  1034. dh=NULL;
  1035. }
  1036. else if ((alg & SSL_kDHr) || (alg & SSL_kDHd))
  1037. {
  1038. al=SSL_AD_ILLEGAL_PARAMETER;
  1039. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
  1040. goto f_err;
  1041. }
  1042. #endif /* !OPENSSL_NO_DH */
  1043. #ifndef OPENSSL_NO_ECDH
  1044. else if (alg & SSL_kECDHE)
  1045. {
  1046. if ((ecdh=EC_KEY_new()) == NULL)
  1047. {
  1048. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1049. goto err;
  1050. }
  1051. /* Extract elliptic curve parameters and the
  1052. * server's ephemeral ECDH public key.
  1053. * Keep accumulating lengths of various components in
  1054. * param_len and make sure it never exceeds n.
  1055. */
  1056. /* XXX: For now we only support named (not generic) curves
  1057. * and the ECParameters in this case is just two bytes.
  1058. */
  1059. param_len=2;
  1060. if ((param_len > n) ||
  1061. (*p != NAMED_CURVE_TYPE) ||
  1062. ((curve_nid = curve_id2nid(*(p + 1))) == 0))
  1063. {
  1064. al=SSL_AD_INTERNAL_ERROR;
  1065. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  1066. goto f_err;
  1067. }
  1068. if (!(ecdh->group=EC_GROUP_new_by_nid(curve_nid)))
  1069. {
  1070. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
  1071. goto err;
  1072. }
  1073. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  1074. (EC_GROUP_get_degree(ecdh->group) > 163))
  1075. {
  1076. al=SSL_AD_EXPORT_RESTRICTION;
  1077. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
  1078. goto f_err;
  1079. }
  1080. p+=2;
  1081. /* Next, get the encoded ECPoint */
  1082. if (((srvr_ecpoint = EC_POINT_new(ecdh->group)) == NULL) ||
  1083. ((bn_ctx = BN_CTX_new()) == NULL))
  1084. {
  1085. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1086. goto err;
  1087. }
  1088. encoded_pt_len = *p; /* length of encoded point */
  1089. p+=1;
  1090. param_len += (1 + encoded_pt_len);
  1091. if ((param_len > n) ||
  1092. (EC_POINT_oct2point(ecdh->group, srvr_ecpoint,
  1093. p, encoded_pt_len, bn_ctx) == 0))
  1094. {
  1095. al=SSL_AD_DECODE_ERROR;
  1096. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_ECPOINT);
  1097. goto f_err;
  1098. }
  1099. n-=param_len;
  1100. p+=encoded_pt_len;
  1101. /* The ECC/TLS specification does not mention
  1102. * the use of DSA to sign ECParameters in the server
  1103. * key exchange message. We do support RSA and ECDSA.
  1104. */
  1105. if (0) ;
  1106. #ifndef OPENSSL_NO_RSA
  1107. else if (alg & SSL_aRSA)
  1108. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1109. #endif
  1110. #ifndef OPENSSL_NO_ECDSA
  1111. else if (alg & SSL_aECDSA)
  1112. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  1113. #endif
  1114. /* else anonymous ECDH, so no certificate or pkey. */
  1115. ecdh->pub_key = srvr_ecpoint;
  1116. s->session->sess_cert->peer_ecdh_tmp=ecdh;
  1117. ecdh=NULL;
  1118. BN_CTX_free(bn_ctx);
  1119. srvr_ecpoint = NULL;
  1120. }
  1121. else if (alg & SSL_kECDH)
  1122. {
  1123. al=SSL_AD_UNEXPECTED_MESSAGE;
  1124. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
  1125. goto f_err;
  1126. }
  1127. #endif /* !OPENSSL_NO_ECDH */
  1128. if (alg & SSL_aFZA)
  1129. {
  1130. al=SSL_AD_HANDSHAKE_FAILURE;
  1131. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
  1132. goto f_err;
  1133. }
  1134. /* p points to the next byte, there are 'n' bytes left */
  1135. /* if it was signed, check the signature */
  1136. if (pkey != NULL)
  1137. {
  1138. n2s(p,i);
  1139. n-=2;
  1140. j=EVP_PKEY_size(pkey);
  1141. if ((i != n) || (n > j) || (n <= 0))
  1142. {
  1143. /* wrong packet length */
  1144. al=SSL_AD_DECODE_ERROR;
  1145. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
  1146. goto f_err;
  1147. }
  1148. #ifndef OPENSSL_NO_RSA
  1149. if (pkey->type == EVP_PKEY_RSA)
  1150. {
  1151. int num;
  1152. j=0;
  1153. q=md_buf;
  1154. for (num=2; num > 0; num--)
  1155. {
  1156. EVP_DigestInit_ex(&md_ctx,(num == 2)
  1157. ?s->ctx->md5:s->ctx->sha1, NULL);
  1158. EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1159. EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1160. EVP_DigestUpdate(&md_ctx,param,param_len);
  1161. EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i);
  1162. q+=i;
  1163. j+=i;
  1164. }
  1165. i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
  1166. pkey->pkey.rsa);
  1167. if (i < 0)
  1168. {
  1169. al=SSL_AD_DECRYPT_ERROR;
  1170. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
  1171. goto f_err;
  1172. }
  1173. if (i == 0)
  1174. {
  1175. /* bad signature */
  1176. al=SSL_AD_DECRYPT_ERROR;
  1177. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
  1178. goto f_err;
  1179. }
  1180. }
  1181. else
  1182. #endif
  1183. #ifndef OPENSSL_NO_DSA
  1184. if (pkey->type == EVP_PKEY_DSA)
  1185. {
  1186. /* lets do DSS */
  1187. EVP_VerifyInit_ex(&md_ctx,EVP_dss1(), NULL);
  1188. EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1189. EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1190. EVP_VerifyUpdate(&md_ctx,param,param_len);
  1191. if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
  1192. {
  1193. /* bad signature */
  1194. al=SSL_AD_DECRYPT_ERROR;
  1195. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
  1196. goto f_err;
  1197. }
  1198. }
  1199. else
  1200. #endif
  1201. #ifndef OPENSSL_NO_ECDSA
  1202. if (pkey->type == EVP_PKEY_EC)
  1203. {
  1204. /* let's do ECDSA */
  1205. EVP_VerifyInit_ex(&md_ctx,EVP_ecdsa(), NULL);
  1206. EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1207. EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1208. EVP_VerifyUpdate(&md_ctx,param,param_len);
  1209. if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
  1210. {
  1211. /* bad signature */
  1212. al=SSL_AD_DECRYPT_ERROR;
  1213. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
  1214. goto f_err;
  1215. }
  1216. }
  1217. else
  1218. #endif
  1219. {
  1220. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1221. goto err;
  1222. }
  1223. }
  1224. else
  1225. {
  1226. /* still data left over */
  1227. if (!(alg & SSL_aNULL))
  1228. {
  1229. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1230. goto err;
  1231. }
  1232. if (n != 0)
  1233. {
  1234. al=SSL_AD_DECODE_ERROR;
  1235. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
  1236. goto f_err;
  1237. }
  1238. }
  1239. EVP_PKEY_free(pkey);
  1240. EVP_MD_CTX_cleanup(&md_ctx);
  1241. return(1);
  1242. f_err:
  1243. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1244. err:
  1245. EVP_PKEY_free(pkey);
  1246. #ifndef OPENSSL_NO_RSA
  1247. if (rsa != NULL)
  1248. RSA_free(rsa);
  1249. #endif
  1250. #ifndef OPENSSL_NO_DH
  1251. if (dh != NULL)
  1252. DH_free(dh);
  1253. #endif
  1254. #ifndef OPENSSL_NO_ECDH
  1255. BN_CTX_free(bn_ctx);
  1256. EC_POINT_free(srvr_ecpoint);
  1257. if (ecdh != NULL)
  1258. EC_KEY_free(ecdh);
  1259. #endif
  1260. EVP_MD_CTX_cleanup(&md_ctx);
  1261. return(-1);
  1262. }
  1263. static int ssl3_get_certificate_request(SSL *s)
  1264. {
  1265. int ok,ret=0;
  1266. unsigned long n,nc,l;
  1267. unsigned int llen,ctype_num,i;
  1268. X509_NAME *xn=NULL;
  1269. unsigned char *p,*d,*q;
  1270. STACK_OF(X509_NAME) *ca_sk=NULL;
  1271. n=ssl3_get_message(s,
  1272. SSL3_ST_CR_CERT_REQ_A,
  1273. SSL3_ST_CR_CERT_REQ_B,
  1274. -1,
  1275. s->max_cert_list,
  1276. &ok);
  1277. if (!ok) return((int)n);
  1278. s->s3->tmp.cert_req=0;
  1279. if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
  1280. {
  1281. s->s3->tmp.reuse_message=1;
  1282. return(1);
  1283. }
  1284. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
  1285. {
  1286. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  1287. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
  1288. goto err;
  1289. }
  1290. /* TLS does not like anon-DH with client cert */
  1291. if (s->version > SSL3_VERSION)
  1292. {
  1293. l=s->s3->tmp.new_cipher->algorithms;
  1294. if (l & SSL_aNULL)
  1295. {
  1296. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  1297. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
  1298. goto err;
  1299. }
  1300. }
  1301. d=p=(unsigned char *)s->init_msg;
  1302. if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
  1303. {
  1304. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
  1305. goto err;
  1306. }
  1307. /* get the certificate types */
  1308. ctype_num= *(p++);
  1309. if (ctype_num > SSL3_CT_NUMBER)
  1310. ctype_num=SSL3_CT_NUMBER;
  1311. for (i=0; i<ctype_num; i++)
  1312. s->s3->tmp.ctype[i]= p[i];
  1313. p+=ctype_num;
  1314. /* get the CA RDNs */
  1315. n2s(p,llen);
  1316. #if 0
  1317. {
  1318. FILE *out;
  1319. out=fopen("/tmp/vsign.der","w");
  1320. fwrite(p,1,llen,out);
  1321. fclose(out);
  1322. }
  1323. #endif
  1324. if ((llen+ctype_num+2+1) != n)
  1325. {
  1326. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1327. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
  1328. goto err;
  1329. }
  1330. for (nc=0; nc<llen; )
  1331. {
  1332. n2s(p,l);
  1333. if ((l+nc+2) > llen)
  1334. {
  1335. if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  1336. goto cont; /* netscape bugs */
  1337. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1338. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
  1339. goto err;
  1340. }
  1341. q=p;
  1342. if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
  1343. {
  1344. /* If netscape tolerance is on, ignore errors */
  1345. if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
  1346. goto cont;
  1347. else
  1348. {
  1349. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1350. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
  1351. goto err;
  1352. }
  1353. }
  1354. if (q != (p+l))
  1355. {
  1356. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1357. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
  1358. goto err;
  1359. }
  1360. if (!sk_X509_NAME_push(ca_sk,xn))
  1361. {
  1362. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
  1363. goto err;
  1364. }
  1365. p+=l;
  1366. nc+=l+2;
  1367. }
  1368. if (0)
  1369. {
  1370. cont:
  1371. ERR_clear_error();
  1372. }
  1373. /* we should setup a certificate to return.... */
  1374. s->s3->tmp.cert_req=1;
  1375. s->s3->tmp.ctype_num=ctype_num;
  1376. if (s->s3->tmp.ca_names != NULL)
  1377. sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
  1378. s->s3->tmp.ca_names=ca_sk;
  1379. ca_sk=NULL;
  1380. ret=1;
  1381. err:
  1382. if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
  1383. return(ret);
  1384. }
  1385. static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
  1386. {
  1387. return(X509_NAME_cmp(*a,*b));
  1388. }
  1389. static int ssl3_get_server_done(SSL *s)
  1390. {
  1391. int ok,ret=0;
  1392. long n;
  1393. n=ssl3_get_message(s,
  1394. SSL3_ST_CR_SRVR_DONE_A,
  1395. SSL3_ST_CR_SRVR_DONE_B,
  1396. SSL3_MT_SERVER_DONE,
  1397. 30, /* should be very small, like 0 :-) */
  1398. &ok);
  1399. if (!ok) return((int)n);
  1400. if (n > 0)
  1401. {
  1402. /* should contain no data */
  1403. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1404. SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
  1405. return -1;
  1406. }
  1407. ret=1;
  1408. return(ret);
  1409. }
  1410. static int ssl3_send_client_key_exchange(SSL *s)
  1411. {
  1412. unsigned char *p,*d;
  1413. int n;
  1414. unsigned long l;
  1415. #ifndef OPENSSL_NO_RSA
  1416. unsigned char *q;
  1417. EVP_PKEY *pkey=NULL;
  1418. #endif
  1419. #ifndef OPENSSL_NO_KRB5
  1420. KSSL_ERR kssl_err;
  1421. #endif /* OPENSSL_NO_KRB5 */
  1422. #ifndef OPENSSL_NO_ECDH
  1423. EC_KEY *clnt_ecdh = NULL;
  1424. EC_POINT *srvr_ecpoint = NULL;
  1425. EVP_PKEY *srvr_pub_pkey = NULL;
  1426. unsigned char *encodedPoint = NULL;
  1427. int encoded_pt_len = 0;
  1428. BN_CTX * bn_ctx = NULL;
  1429. #endif
  1430. if (s->state == SSL3_ST_CW_KEY_EXCH_A)
  1431. {
  1432. d=(unsigned char *)s->init_buf->data;
  1433. p= &(d[4]);
  1434. l=s->s3->tmp.new_cipher->algorithms;
  1435. /* Fool emacs indentation */
  1436. if (0) {}
  1437. #ifndef OPENSSL_NO_RSA
  1438. else if (l & SSL_kRSA)
  1439. {
  1440. RSA *rsa;
  1441. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  1442. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  1443. rsa=s->session->sess_cert->peer_rsa_tmp;
  1444. else
  1445. {
  1446. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1447. if ((pkey == NULL) ||
  1448. (pkey->type != EVP_PKEY_RSA) ||
  1449. (pkey->pkey.rsa == NULL))
  1450. {
  1451. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1452. goto err;
  1453. }
  1454. rsa=pkey->pkey.rsa;
  1455. EVP_PKEY_free(pkey);
  1456. }
  1457. tmp_buf[0]=s->client_version>>8;
  1458. tmp_buf[1]=s->client_version&0xff;
  1459. if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
  1460. goto err;
  1461. s->session->master_key_length=sizeof tmp_buf;
  1462. q=p;
  1463. /* Fix buf for TLS and beyond */
  1464. if (s->version > SSL3_VERSION)
  1465. p+=2;
  1466. n=RSA_public_encrypt(sizeof tmp_buf,
  1467. tmp_buf,p,rsa,RSA_PKCS1_PADDING);
  1468. #ifdef PKCS1_CHECK
  1469. if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
  1470. if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
  1471. #endif
  1472. if (n <= 0)
  1473. {
  1474. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
  1475. goto err;
  1476. }
  1477. /* Fix buf for TLS and beyond */
  1478. if (s->version > SSL3_VERSION)
  1479. {
  1480. s2n(n,q);
  1481. n+=2;
  1482. }
  1483. s->session->master_key_length=
  1484. s->method->ssl3_enc->generate_master_secret(s,
  1485. s->session->master_key,
  1486. tmp_buf,sizeof tmp_buf);
  1487. OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
  1488. }
  1489. #endif
  1490. #ifndef OPENSSL_NO_KRB5
  1491. else if (l & SSL_kKRB5)
  1492. {
  1493. krb5_error_code krb5rc;
  1494. KSSL_CTX *kssl_ctx = s->kssl_ctx;
  1495. /* krb5_data krb5_ap_req; */
  1496. krb5_data *enc_ticket;
  1497. krb5_data authenticator, *authp = NULL;
  1498. EVP_CIPHER_CTX ciph_ctx;
  1499. EVP_CIPHER *enc = NULL;
  1500. unsigned char iv[EVP_MAX_IV_LENGTH];
  1501. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  1502. unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
  1503. + EVP_MAX_IV_LENGTH];
  1504. int padl, outl = sizeof(epms);
  1505. EVP_CIPHER_CTX_init(&ciph_ctx);
  1506. #ifdef KSSL_DEBUG
  1507. printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
  1508. l, SSL_kKRB5);
  1509. #endif /* KSSL_DEBUG */
  1510. authp = NULL;
  1511. #ifdef KRB5SENDAUTH
  1512. if (KRB5SENDAUTH) authp = &authenticator;
  1513. #endif /* KRB5SENDAUTH */
  1514. krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
  1515. &kssl_err);
  1516. enc = kssl_map_enc(kssl_ctx->enctype);
  1517. if (enc == NULL)
  1518. goto err;
  1519. #ifdef KSSL_DEBUG
  1520. {
  1521. printf("kssl_cget_tkt rtn %d\n", krb5rc);
  1522. if (krb5rc && kssl_err.text)
  1523. printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
  1524. }
  1525. #endif /* KSSL_DEBUG */
  1526. if (krb5rc)
  1527. {
  1528. ssl3_send_alert(s,SSL3_AL_FATAL,
  1529. SSL_AD_HANDSHAKE_FAILURE);
  1530. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  1531. kssl_err.reason);
  1532. goto err;
  1533. }
  1534. /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
  1535. ** in place of RFC 2712 KerberosWrapper, as in:
  1536. **
  1537. ** Send ticket (copy to *p, set n = length)
  1538. ** n = krb5_ap_req.length;
  1539. ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
  1540. ** if (krb5_ap_req.data)
  1541. ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
  1542. **
  1543. ** Now using real RFC 2712 KerberosWrapper
  1544. ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
  1545. ** Note: 2712 "opaque" types are here replaced
  1546. ** with a 2-byte length followed by the value.
  1547. ** Example:
  1548. ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
  1549. ** Where "xx xx" = length bytes. Shown here with
  1550. ** optional authenticator omitted.
  1551. */
  1552. /* KerberosWrapper.Ticket */
  1553. s2n(enc_ticket->length,p);
  1554. memcpy(p, enc_ticket->data, enc_ticket->length);
  1555. p+= enc_ticket->length;
  1556. n = enc_ticket->length + 2;
  1557. /* KerberosWrapper.Authenticator */
  1558. if (authp && authp->length)
  1559. {
  1560. s2n(authp->length,p);
  1561. memcpy(p, authp->data, authp->length);
  1562. p+= authp->length;
  1563. n+= authp->length + 2;
  1564. free(authp->data);
  1565. authp->data = NULL;
  1566. authp->length = 0;
  1567. }
  1568. else
  1569. {
  1570. s2n(0,p);/* null authenticator length */
  1571. n+=2;
  1572. }
  1573. if (RAND_bytes(tmp_buf,sizeof tmp_buf) <= 0)
  1574. goto err;
  1575. /* 20010420 VRS. Tried it this way; failed.
  1576. ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
  1577. ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
  1578. ** kssl_ctx->length);
  1579. ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
  1580. */
  1581. memset(iv, 0, sizeof iv); /* per RFC 1510 */
  1582. EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
  1583. kssl_ctx->key,iv);
  1584. EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
  1585. sizeof tmp_buf);
  1586. EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
  1587. outl += padl;
  1588. if (outl > sizeof epms)
  1589. {
  1590. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1591. goto err;
  1592. }
  1593. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  1594. /* KerberosWrapper.EncryptedPreMasterSecret */
  1595. s2n(outl,p);
  1596. memcpy(p, epms, outl);
  1597. p+=outl;
  1598. n+=outl + 2;
  1599. s->session->master_key_length=
  1600. s->method->ssl3_enc->generate_master_secret(s,
  1601. s->session->master_key,
  1602. tmp_buf, sizeof tmp_buf);
  1603. OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
  1604. OPENSSL_cleanse(epms, outl);
  1605. }
  1606. #endif
  1607. #ifndef OPENSSL_NO_DH
  1608. else if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  1609. {
  1610. DH *dh_srvr,*dh_clnt;
  1611. if (s->session->sess_cert->peer_dh_tmp != NULL)
  1612. dh_srvr=s->session->sess_cert->peer_dh_tmp;
  1613. else
  1614. {
  1615. /* we get them from the cert */
  1616. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  1617. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
  1618. goto err;
  1619. }
  1620. /* generate a new random key */
  1621. if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
  1622. {
  1623. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  1624. goto err;
  1625. }
  1626. if (!DH_generate_key(dh_clnt))
  1627. {
  1628. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  1629. goto err;
  1630. }
  1631. /* use the 'p' output buffer for the DH key, but
  1632. * make sure to clear it out afterwards */
  1633. n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
  1634. if (n <= 0)
  1635. {
  1636. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  1637. goto err;
  1638. }
  1639. /* generate master key from the result */
  1640. s->session->master_key_length=
  1641. s->method->ssl3_enc->generate_master_secret(s,
  1642. s->session->master_key,p,n);
  1643. /* clean up */
  1644. memset(p,0,n);
  1645. /* send off the data */
  1646. n=BN_num_bytes(dh_clnt->pub_key);
  1647. s2n(n,p);
  1648. BN_bn2bin(dh_clnt->pub_key,p);
  1649. n+=2;
  1650. DH_free(dh_clnt);
  1651. /* perhaps clean things up a bit EAY EAY EAY EAY*/
  1652. }
  1653. #endif
  1654. #ifndef OPENSSL_NO_ECDH
  1655. else if ((l & SSL_kECDH) || (l & SSL_kECDHE))
  1656. {
  1657. EC_GROUP *srvr_group = NULL;
  1658. int ecdh_clnt_cert = 0;
  1659. /* Did we send out the client's
  1660. * ECDH share for use in premaster
  1661. * computation as part of client certificate?
  1662. * If so, set ecdh_clnt_cert to 1.
  1663. */
  1664. if ((l & SSL_kECDH) && (s->cert != NULL))
  1665. {
  1666. /* XXX: For now, we do not support client
  1667. * authentication using ECDH certificates.
  1668. * To add such support, one needs to add
  1669. * code that checks for appropriate
  1670. * conditions and sets ecdh_clnt_cert to 1.
  1671. * For example, the cert have an ECC
  1672. * key on the same curve as the server's
  1673. * and the key should be authorized for
  1674. * key agreement.
  1675. *
  1676. * One also needs to add code in ssl3_connect
  1677. * to skip sending the certificate verify
  1678. * message.
  1679. *
  1680. * if ((s->cert->key->privatekey != NULL) &&
  1681. * (s->cert->key->privatekey->type ==
  1682. * EVP_PKEY_EC) && ...)
  1683. * ecdh_clnt_cert = 1;
  1684. */
  1685. }
  1686. if (s->session->sess_cert->peer_ecdh_tmp != NULL)
  1687. {
  1688. srvr_group = s->session->sess_cert-> \
  1689. peer_ecdh_tmp->group;
  1690. srvr_ecpoint = s->session->sess_cert-> \
  1691. peer_ecdh_tmp->pub_key;
  1692. }
  1693. else
  1694. {
  1695. /* Get the Server Public Key from Cert */
  1696. srvr_pub_pkey = X509_get_pubkey(s->session-> \
  1697. sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  1698. if ((srvr_pub_pkey == NULL) ||
  1699. (srvr_pub_pkey->type != EVP_PKEY_EC) ||
  1700. (srvr_pub_pkey->pkey.eckey == NULL))
  1701. {
  1702. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  1703. ERR_R_INTERNAL_ERROR);
  1704. goto err;
  1705. }
  1706. srvr_group = srvr_pub_pkey->pkey.eckey->group;
  1707. srvr_ecpoint =
  1708. srvr_pub_pkey->pkey.eckey->pub_key;
  1709. }
  1710. if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
  1711. {
  1712. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  1713. ERR_R_INTERNAL_ERROR);
  1714. goto err;
  1715. }
  1716. if ((clnt_ecdh=EC_KEY_new()) == NULL)
  1717. {
  1718. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1719. goto err;
  1720. }
  1721. clnt_ecdh->group = srvr_group;
  1722. if (ecdh_clnt_cert)
  1723. {
  1724. /* Reuse key info from our certificate
  1725. * We only need our private key to perform
  1726. * the ECDH computation.
  1727. */
  1728. clnt_ecdh->priv_key = BN_dup(s->cert->key-> \
  1729. privatekey->pkey.eckey->priv_key);
  1730. }
  1731. else
  1732. {
  1733. /* Generate a new ECDH key pair */
  1734. if (!(EC_KEY_generate_key(clnt_ecdh)))
  1735. {
  1736. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  1737. goto err;
  1738. }
  1739. }
  1740. /* use the 'p' output buffer for the ECDH key, but
  1741. * make sure to clear it out afterwards
  1742. */
  1743. n=ECDH_compute_key(p, srvr_ecpoint, clnt_ecdh);
  1744. if (n <= 0)
  1745. {
  1746. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  1747. ERR_R_ECDH_LIB);
  1748. goto err;
  1749. }
  1750. /* generate master key from the result */
  1751. s->session->master_key_length = s->method->ssl3_enc \
  1752. -> generate_master_secret(s,
  1753. s->session->master_key,
  1754. p, n);
  1755. memset(p, 0, n); /* clean up */
  1756. if (ecdh_clnt_cert)
  1757. {
  1758. /* Send empty client key exch message */
  1759. n = 0;
  1760. }
  1761. else
  1762. {
  1763. /* First check the size of encoding and
  1764. * allocate memory accordingly.
  1765. */
  1766. encoded_pt_len =
  1767. EC_POINT_point2oct(clnt_ecdh->group,
  1768. clnt_ecdh->pub_key,
  1769. POINT_CONVERSION_UNCOMPRESSED,
  1770. NULL, 0, NULL);
  1771. encodedPoint = (unsigned char *)
  1772. OPENSSL_malloc(encoded_pt_len *
  1773. sizeof(unsigned char));
  1774. bn_ctx = BN_CTX_new();
  1775. if ((encodedPoint == NULL) ||
  1776. (bn_ctx == NULL))
  1777. {
  1778. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1779. goto err;
  1780. }
  1781. /* Encode the public key */
  1782. n = EC_POINT_point2oct(clnt_ecdh->group,
  1783. clnt_ecdh->pub_key,
  1784. POINT_CONVERSION_UNCOMPRESSED,
  1785. encodedPoint, encoded_pt_len, bn_ctx);
  1786. *p = n; /* length of encoded point */
  1787. /* Encoded point will be copied here */
  1788. p += 1;
  1789. /* copy the point */
  1790. memcpy((unsigned char *)p, encodedPoint, n);
  1791. /* increment n to account for length field */
  1792. n += 1;
  1793. }
  1794. /* Free allocated memory */
  1795. BN_CTX_free(bn_ctx);
  1796. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  1797. if (clnt_ecdh != NULL)
  1798. {
  1799. /* group is shared */
  1800. clnt_ecdh->group = NULL;
  1801. EC_KEY_free(clnt_ecdh);
  1802. }
  1803. EVP_PKEY_free(srvr_pub_pkey);
  1804. }
  1805. #endif /* !OPENSSL_NO_ECDH */
  1806. else
  1807. {
  1808. ssl3_send_alert(s, SSL3_AL_FATAL,
  1809. SSL_AD_HANDSHAKE_FAILURE);
  1810. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  1811. ERR_R_INTERNAL_ERROR);
  1812. goto err;
  1813. }
  1814. *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
  1815. l2n3(n,d);
  1816. s->state=SSL3_ST_CW_KEY_EXCH_B;
  1817. /* number of bytes to write */
  1818. s->init_num=n+4;
  1819. s->init_off=0;
  1820. }
  1821. /* SSL3_ST_CW_KEY_EXCH_B */
  1822. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1823. err:
  1824. #ifndef OPENSSL_NO_ECDH
  1825. BN_CTX_free(bn_ctx);
  1826. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  1827. if (clnt_ecdh != NULL)
  1828. {
  1829. /* group is shared */
  1830. clnt_ecdh->group = NULL;
  1831. EC_KEY_free(clnt_ecdh);
  1832. }
  1833. EVP_PKEY_free(srvr_pub_pkey);
  1834. #endif
  1835. return(-1);
  1836. }
  1837. static int ssl3_send_client_verify(SSL *s)
  1838. {
  1839. unsigned char *p,*d;
  1840. unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  1841. EVP_PKEY *pkey;
  1842. #ifndef OPENSSL_NO_RSA
  1843. unsigned u=0;
  1844. #endif
  1845. unsigned long n;
  1846. #ifndef OPENSSL_NO_DSA
  1847. int j;
  1848. #endif
  1849. if (s->state == SSL3_ST_CW_CERT_VRFY_A)
  1850. {
  1851. d=(unsigned char *)s->init_buf->data;
  1852. p= &(d[4]);
  1853. pkey=s->cert->key->privatekey;
  1854. s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2),
  1855. &(data[MD5_DIGEST_LENGTH]));
  1856. #ifndef OPENSSL_NO_RSA
  1857. if (pkey->type == EVP_PKEY_RSA)
  1858. {
  1859. s->method->ssl3_enc->cert_verify_mac(s,
  1860. &(s->s3->finish_dgst1),&(data[0]));
  1861. if (RSA_sign(NID_md5_sha1, data,
  1862. MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
  1863. &(p[2]), &u, pkey->pkey.rsa) <= 0 )
  1864. {
  1865. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
  1866. goto err;
  1867. }
  1868. s2n(u,p);
  1869. n=u+2;
  1870. }
  1871. else
  1872. #endif
  1873. #ifndef OPENSSL_NO_DSA
  1874. if (pkey->type == EVP_PKEY_DSA)
  1875. {
  1876. if (!DSA_sign(pkey->save_type,
  1877. &(data[MD5_DIGEST_LENGTH]),
  1878. SHA_DIGEST_LENGTH,&(p[2]),
  1879. (unsigned int *)&j,pkey->pkey.dsa))
  1880. {
  1881. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
  1882. goto err;
  1883. }
  1884. s2n(j,p);
  1885. n=j+2;
  1886. }
  1887. else
  1888. #endif
  1889. #ifndef OPENSSL_NO_ECDSA
  1890. if (pkey->type == EVP_PKEY_EC)
  1891. {
  1892. if (!ECDSA_sign(pkey->save_type,
  1893. &(data[MD5_DIGEST_LENGTH]),
  1894. SHA_DIGEST_LENGTH,&(p[2]),
  1895. (unsigned int *)&j,pkey->pkey.eckey))
  1896. {
  1897. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
  1898. ERR_R_ECDSA_LIB);
  1899. goto err;
  1900. }
  1901. s2n(j,p);
  1902. n=j+2;
  1903. }
  1904. else
  1905. #endif
  1906. {
  1907. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
  1908. goto err;
  1909. }
  1910. *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
  1911. l2n3(n,d);
  1912. s->init_num=(int)n+4;
  1913. s->init_off=0;
  1914. }
  1915. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1916. err:
  1917. return(-1);
  1918. }
  1919. static int ssl3_send_client_certificate(SSL *s)
  1920. {
  1921. X509 *x509=NULL;
  1922. EVP_PKEY *pkey=NULL;
  1923. int i;
  1924. unsigned long l;
  1925. if (s->state == SSL3_ST_CW_CERT_A)
  1926. {
  1927. if ((s->cert == NULL) ||
  1928. (s->cert->key->x509 == NULL) ||
  1929. (s->cert->key->privatekey == NULL))
  1930. s->state=SSL3_ST_CW_CERT_B;
  1931. else
  1932. s->state=SSL3_ST_CW_CERT_C;
  1933. }
  1934. /* We need to get a client cert */
  1935. if (s->state == SSL3_ST_CW_CERT_B)
  1936. {
  1937. /* If we get an error, we need to
  1938. * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
  1939. * We then get retied later */
  1940. i=0;
  1941. if (s->ctx->client_cert_cb != NULL)
  1942. i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
  1943. if (i < 0)
  1944. {
  1945. s->rwstate=SSL_X509_LOOKUP;
  1946. return(-1);
  1947. }
  1948. s->rwstate=SSL_NOTHING;
  1949. if ((i == 1) && (pkey != NULL) && (x509 != NULL))
  1950. {
  1951. s->state=SSL3_ST_CW_CERT_B;
  1952. if ( !SSL_use_certificate(s,x509) ||
  1953. !SSL_use_PrivateKey(s,pkey))
  1954. i=0;
  1955. }
  1956. else if (i == 1)
  1957. {
  1958. i=0;
  1959. SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  1960. }
  1961. if (x509 != NULL) X509_free(x509);
  1962. if (pkey != NULL) EVP_PKEY_free(pkey);
  1963. if (i == 0)
  1964. {
  1965. if (s->version == SSL3_VERSION)
  1966. {
  1967. s->s3->tmp.cert_req=0;
  1968. ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
  1969. return(1);
  1970. }
  1971. else
  1972. {
  1973. s->s3->tmp.cert_req=2;
  1974. }
  1975. }
  1976. /* Ok, we have a cert */
  1977. s->state=SSL3_ST_CW_CERT_C;
  1978. }
  1979. if (s->state == SSL3_ST_CW_CERT_C)
  1980. {
  1981. s->state=SSL3_ST_CW_CERT_D;
  1982. l=ssl3_output_cert_chain(s,
  1983. (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
  1984. s->init_num=(int)l;
  1985. s->init_off=0;
  1986. }
  1987. /* SSL3_ST_CW_CERT_D */
  1988. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1989. }
  1990. #define has_bits(i,m) (((i)&(m)) == (m))
  1991. static int ssl3_check_cert_and_algorithm(SSL *s)
  1992. {
  1993. int i,idx;
  1994. long algs;
  1995. EVP_PKEY *pkey=NULL;
  1996. SESS_CERT *sc;
  1997. #ifndef OPENSSL_NO_RSA
  1998. RSA *rsa;
  1999. #endif
  2000. #ifndef OPENSSL_NO_DH
  2001. DH *dh;
  2002. #endif
  2003. sc=s->session->sess_cert;
  2004. if (sc == NULL)
  2005. {
  2006. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
  2007. goto err;
  2008. }
  2009. algs=s->s3->tmp.new_cipher->algorithms;
  2010. /* we don't have a certificate */
  2011. if (algs & (SSL_aDH|SSL_aNULL|SSL_aKRB5))
  2012. return(1);
  2013. #ifndef OPENSSL_NO_RSA
  2014. rsa=s->session->sess_cert->peer_rsa_tmp;
  2015. #endif
  2016. #ifndef OPENSSL_NO_DH
  2017. dh=s->session->sess_cert->peer_dh_tmp;
  2018. #endif
  2019. /* This is the passed certificate */
  2020. idx=sc->peer_cert_type;
  2021. #ifndef OPENSSL_NO_ECDH
  2022. if (idx == SSL_PKEY_ECC)
  2023. {
  2024. if (check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
  2025. s->s3->tmp.new_cipher) == 0)
  2026. { /* check failed */
  2027. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_BAD_ECC_CERT);
  2028. goto f_err;
  2029. }
  2030. else
  2031. {
  2032. return 1;
  2033. }
  2034. }
  2035. #endif
  2036. pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
  2037. i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
  2038. EVP_PKEY_free(pkey);
  2039. /* Check that we have a certificate if we require one */
  2040. if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
  2041. {
  2042. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
  2043. goto f_err;
  2044. }
  2045. #ifndef OPENSSL_NO_DSA
  2046. else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
  2047. {
  2048. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
  2049. goto f_err;
  2050. }
  2051. #endif
  2052. #ifndef OPENSSL_NO_RSA
  2053. if ((algs & SSL_kRSA) &&
  2054. !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
  2055. {
  2056. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  2057. goto f_err;
  2058. }
  2059. #endif
  2060. #ifndef OPENSSL_NO_DH
  2061. if ((algs & SSL_kEDH) &&
  2062. !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
  2063. {
  2064. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
  2065. goto f_err;
  2066. }
  2067. else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
  2068. {
  2069. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
  2070. goto f_err;
  2071. }
  2072. #ifndef OPENSSL_NO_DSA
  2073. else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
  2074. {
  2075. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
  2076. goto f_err;
  2077. }
  2078. #endif
  2079. #endif
  2080. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
  2081. {
  2082. #ifndef OPENSSL_NO_RSA
  2083. if (algs & SSL_kRSA)
  2084. {
  2085. if (rsa == NULL
  2086. || RSA_size(rsa) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
  2087. {
  2088. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
  2089. goto f_err;
  2090. }
  2091. }
  2092. else
  2093. #endif
  2094. #ifndef OPENSSL_NO_DH
  2095. if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  2096. {
  2097. if (dh == NULL
  2098. || DH_size(dh) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
  2099. {
  2100. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
  2101. goto f_err;
  2102. }
  2103. }
  2104. else
  2105. #endif
  2106. {
  2107. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  2108. goto f_err;
  2109. }
  2110. }
  2111. return(1);
  2112. f_err:
  2113. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  2114. err:
  2115. return(0);
  2116. }
  2117. #ifndef OPENSSL_NO_ECDH
  2118. /* This is the complement of nid2curve_id in s3_srvr.c. */
  2119. static int curve_id2nid(int curve_id)
  2120. {
  2121. /* ECC curves from draft-ietf-tls-ecc-01.txt (Mar 15, 2001) */
  2122. static int nid_list[26] =
  2123. {
  2124. 0,
  2125. NID_sect163k1, /* sect163k1 (1) */
  2126. NID_sect163r1, /* sect163r1 (2) */
  2127. NID_sect163r2, /* sect163r2 (3) */
  2128. NID_sect193r1, /* sect193r1 (4) */
  2129. NID_sect193r2, /* sect193r2 (5) */
  2130. NID_sect233k1, /* sect233k1 (6) */
  2131. NID_sect233r1, /* sect233r1 (7) */
  2132. NID_sect239k1, /* sect239k1 (8) */
  2133. NID_sect283k1, /* sect283k1 (9) */
  2134. NID_sect283r1, /* sect283r1 (10) */
  2135. NID_sect409k1, /* sect409k1 (11) */
  2136. NID_sect409r1, /* sect409r1 (12) */
  2137. NID_sect571k1, /* sect571k1 (13) */
  2138. NID_sect571r1, /* sect571r1 (14) */
  2139. NID_secp160k1, /* secp160k1 (15) */
  2140. NID_secp160r1, /* secp160r1 (16) */
  2141. NID_secp160r2, /* secp160r2 (17) */
  2142. NID_secp192k1, /* secp192k1 (18) */
  2143. NID_X9_62_prime192v1, /* secp192r1 (19) */
  2144. NID_secp224k1, /* secp224k1 (20) */
  2145. NID_secp224r1, /* secp224r1 (21) */
  2146. NID_secp256k1, /* secp256k1 (22) */
  2147. NID_X9_62_prime256v1, /* secp256r1 (23) */
  2148. NID_secp384r1, /* secp384r1 (24) */
  2149. NID_secp521r1 /* secp521r1 (25) */
  2150. };
  2151. if ((curve_id < 1) || (curve_id > 25)) return 0;
  2152. return nid_list[curve_id];
  2153. }
  2154. #endif