s2_srvr.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /* ssl/s2_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-2001 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. #include "ssl_locl.h"
  112. #ifndef OPENSSL_NO_SSL2
  113. #include <stdio.h>
  114. #include <openssl/bio.h>
  115. #include <openssl/rand.h>
  116. #include <openssl/objects.h>
  117. #include <openssl/evp.h>
  118. static SSL_METHOD *ssl2_get_server_method(int ver);
  119. static int get_client_master_key(SSL *s);
  120. static int get_client_hello(SSL *s);
  121. static int server_hello(SSL *s);
  122. static int get_client_finished(SSL *s);
  123. static int server_verify(SSL *s);
  124. static int server_finish(SSL *s);
  125. static int request_certificate(SSL *s);
  126. static int ssl_rsa_private_decrypt(CERT *c, int len, unsigned char *from,
  127. unsigned char *to,int padding);
  128. #define BREAK break
  129. static SSL_METHOD *ssl2_get_server_method(int ver)
  130. {
  131. if (ver == SSL2_VERSION)
  132. return(SSLv2_server_method());
  133. else
  134. return(NULL);
  135. }
  136. SSL_METHOD *SSLv2_server_method(void)
  137. {
  138. static int init=1;
  139. static SSL_METHOD SSLv2_server_data;
  140. if (init)
  141. {
  142. CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD);
  143. if (init)
  144. {
  145. memcpy((char *)&SSLv2_server_data,(char *)sslv2_base_method(),
  146. sizeof(SSL_METHOD));
  147. SSLv2_server_data.ssl_accept=ssl2_accept;
  148. SSLv2_server_data.get_ssl_method=ssl2_get_server_method;
  149. init=0;
  150. }
  151. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD);
  152. }
  153. return(&SSLv2_server_data);
  154. }
  155. int ssl2_accept(SSL *s)
  156. {
  157. unsigned long l=time(NULL);
  158. BUF_MEM *buf=NULL;
  159. int ret= -1;
  160. long num1;
  161. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  162. int new_state,state;
  163. RAND_add(&l,sizeof(l),0);
  164. ERR_clear_error();
  165. clear_sys_error();
  166. if (s->info_callback != NULL)
  167. cb=s->info_callback;
  168. else if (s->ctx->info_callback != NULL)
  169. cb=s->ctx->info_callback;
  170. /* init things to blank */
  171. s->in_handshake++;
  172. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  173. if (s->cert == NULL)
  174. {
  175. SSLerr(SSL_F_SSL2_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
  176. return(-1);
  177. }
  178. clear_sys_error();
  179. for (;;)
  180. {
  181. state=s->state;
  182. switch (s->state)
  183. {
  184. case SSL_ST_BEFORE:
  185. case SSL_ST_ACCEPT:
  186. case SSL_ST_BEFORE|SSL_ST_ACCEPT:
  187. case SSL_ST_OK|SSL_ST_ACCEPT:
  188. s->server=1;
  189. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  190. s->version=SSL2_VERSION;
  191. s->type=SSL_ST_ACCEPT;
  192. buf=s->init_buf;
  193. if ((buf == NULL) && ((buf=BUF_MEM_new()) == NULL))
  194. { ret= -1; goto end; }
  195. if (!BUF_MEM_grow(buf,(int)
  196. SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
  197. { ret= -1; goto end; }
  198. s->init_buf=buf;
  199. s->init_num=0;
  200. s->ctx->stats.sess_accept++;
  201. s->handshake_func=ssl2_accept;
  202. s->state=SSL2_ST_GET_CLIENT_HELLO_A;
  203. BREAK;
  204. case SSL2_ST_GET_CLIENT_HELLO_A:
  205. case SSL2_ST_GET_CLIENT_HELLO_B:
  206. case SSL2_ST_GET_CLIENT_HELLO_C:
  207. s->shutdown=0;
  208. ret=get_client_hello(s);
  209. if (ret <= 0) goto end;
  210. s->init_num=0;
  211. s->state=SSL2_ST_SEND_SERVER_HELLO_A;
  212. BREAK;
  213. case SSL2_ST_SEND_SERVER_HELLO_A:
  214. case SSL2_ST_SEND_SERVER_HELLO_B:
  215. ret=server_hello(s);
  216. if (ret <= 0) goto end;
  217. s->init_num=0;
  218. if (!s->hit)
  219. {
  220. s->state=SSL2_ST_GET_CLIENT_MASTER_KEY_A;
  221. BREAK;
  222. }
  223. else
  224. {
  225. s->state=SSL2_ST_SERVER_START_ENCRYPTION;
  226. BREAK;
  227. }
  228. case SSL2_ST_GET_CLIENT_MASTER_KEY_A:
  229. case SSL2_ST_GET_CLIENT_MASTER_KEY_B:
  230. ret=get_client_master_key(s);
  231. if (ret <= 0) goto end;
  232. s->init_num=0;
  233. s->state=SSL2_ST_SERVER_START_ENCRYPTION;
  234. BREAK;
  235. case SSL2_ST_SERVER_START_ENCRYPTION:
  236. /* Ok we how have sent all the stuff needed to
  237. * start encrypting, the next packet back will
  238. * be encrypted. */
  239. if (!ssl2_enc_init(s,0))
  240. { ret= -1; goto end; }
  241. s->s2->clear_text=0;
  242. s->state=SSL2_ST_SEND_SERVER_VERIFY_A;
  243. BREAK;
  244. case SSL2_ST_SEND_SERVER_VERIFY_A:
  245. case SSL2_ST_SEND_SERVER_VERIFY_B:
  246. ret=server_verify(s);
  247. if (ret <= 0) goto end;
  248. s->init_num=0;
  249. if (s->hit)
  250. {
  251. /* If we are in here, we have been
  252. * buffering the output, so we need to
  253. * flush it and remove buffering from
  254. * future traffic */
  255. s->state=SSL2_ST_SEND_SERVER_VERIFY_C;
  256. BREAK;
  257. }
  258. else
  259. {
  260. s->state=SSL2_ST_GET_CLIENT_FINISHED_A;
  261. break;
  262. }
  263. case SSL2_ST_SEND_SERVER_VERIFY_C:
  264. /* get the number of bytes to write */
  265. num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
  266. if (num1 != 0)
  267. {
  268. s->rwstate=SSL_WRITING;
  269. num1=BIO_flush(s->wbio);
  270. if (num1 <= 0) { ret= -1; goto end; }
  271. s->rwstate=SSL_NOTHING;
  272. }
  273. /* flushed and now remove buffering */
  274. s->wbio=BIO_pop(s->wbio);
  275. s->state=SSL2_ST_GET_CLIENT_FINISHED_A;
  276. BREAK;
  277. case SSL2_ST_GET_CLIENT_FINISHED_A:
  278. case SSL2_ST_GET_CLIENT_FINISHED_B:
  279. ret=get_client_finished(s);
  280. if (ret <= 0)
  281. goto end;
  282. s->init_num=0;
  283. s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_A;
  284. BREAK;
  285. case SSL2_ST_SEND_REQUEST_CERTIFICATE_A:
  286. case SSL2_ST_SEND_REQUEST_CERTIFICATE_B:
  287. case SSL2_ST_SEND_REQUEST_CERTIFICATE_C:
  288. case SSL2_ST_SEND_REQUEST_CERTIFICATE_D:
  289. /* don't do a 'request certificate' if we
  290. * don't want to, or we already have one, and
  291. * we only want to do it once. */
  292. if (!(s->verify_mode & SSL_VERIFY_PEER) ||
  293. ((s->session->peer != NULL) &&
  294. (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)))
  295. {
  296. s->state=SSL2_ST_SEND_SERVER_FINISHED_A;
  297. break;
  298. }
  299. else
  300. {
  301. ret=request_certificate(s);
  302. if (ret <= 0) goto end;
  303. s->init_num=0;
  304. s->state=SSL2_ST_SEND_SERVER_FINISHED_A;
  305. }
  306. BREAK;
  307. case SSL2_ST_SEND_SERVER_FINISHED_A:
  308. case SSL2_ST_SEND_SERVER_FINISHED_B:
  309. ret=server_finish(s);
  310. if (ret <= 0) goto end;
  311. s->init_num=0;
  312. s->state=SSL_ST_OK;
  313. break;
  314. case SSL_ST_OK:
  315. BUF_MEM_free(s->init_buf);
  316. ssl_free_wbio_buffer(s);
  317. s->init_buf=NULL;
  318. s->init_num=0;
  319. /* ERR_clear_error();*/
  320. ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
  321. s->ctx->stats.sess_accept_good++;
  322. /* s->server=1; */
  323. ret=1;
  324. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  325. goto end;
  326. /* BREAK; */
  327. default:
  328. SSLerr(SSL_F_SSL2_ACCEPT,SSL_R_UNKNOWN_STATE);
  329. ret= -1;
  330. goto end;
  331. /* BREAK; */
  332. }
  333. if ((cb != NULL) && (s->state != state))
  334. {
  335. new_state=s->state;
  336. s->state=state;
  337. cb(s,SSL_CB_ACCEPT_LOOP,1);
  338. s->state=new_state;
  339. }
  340. }
  341. end:
  342. s->in_handshake--;
  343. if (cb != NULL)
  344. cb(s,SSL_CB_ACCEPT_EXIT,ret);
  345. return(ret);
  346. }
  347. static int get_client_master_key(SSL *s)
  348. {
  349. int is_export,i,n,keya,ek;
  350. unsigned long len;
  351. unsigned char *p;
  352. SSL_CIPHER *cp;
  353. const EVP_CIPHER *c;
  354. const EVP_MD *md;
  355. p=(unsigned char *)s->init_buf->data;
  356. if (s->state == SSL2_ST_GET_CLIENT_MASTER_KEY_A)
  357. {
  358. i=ssl2_read(s,(char *)&(p[s->init_num]),10-s->init_num);
  359. if (i < (10-s->init_num))
  360. return(ssl2_part_read(s,SSL_F_GET_CLIENT_MASTER_KEY,i));
  361. s->init_num = 10;
  362. if (*(p++) != SSL2_MT_CLIENT_MASTER_KEY)
  363. {
  364. if (p[-1] != SSL2_MT_ERROR)
  365. {
  366. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  367. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_READ_WRONG_PACKET_TYPE);
  368. }
  369. else
  370. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, SSL_R_PEER_ERROR);
  371. return(-1);
  372. }
  373. cp=ssl2_get_cipher_by_char(p);
  374. if (cp == NULL)
  375. {
  376. ssl2_return_error(s,SSL2_PE_NO_CIPHER);
  377. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, SSL_R_NO_CIPHER_MATCH);
  378. return(-1);
  379. }
  380. s->session->cipher= cp;
  381. p+=3;
  382. n2s(p,i); s->s2->tmp.clear=i;
  383. n2s(p,i); s->s2->tmp.enc=i;
  384. n2s(p,i); s->session->key_arg_length=i;
  385. if(s->session->key_arg_length > SSL_MAX_KEY_ARG_LENGTH)
  386. {
  387. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  388. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, SSL_R_KEY_ARG_TOO_LONG);
  389. return -1;
  390. }
  391. s->state=SSL2_ST_GET_CLIENT_MASTER_KEY_B;
  392. }
  393. /* SSL2_ST_GET_CLIENT_MASTER_KEY_B */
  394. p=(unsigned char *)s->init_buf->data;
  395. if (s->init_buf->length < SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
  396. {
  397. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  398. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
  399. return -1;
  400. }
  401. keya=s->session->key_arg_length;
  402. len = 10 + (unsigned long)s->s2->tmp.clear + (unsigned long)s->s2->tmp.enc + (unsigned long)keya;
  403. if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
  404. {
  405. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  406. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_MESSAGE_TOO_LONG);
  407. return -1;
  408. }
  409. n = (int)len - s->init_num;
  410. i = ssl2_read(s,(char *)&(p[s->init_num]),n);
  411. if (i != n) return(ssl2_part_read(s,SSL_F_GET_CLIENT_MASTER_KEY,i));
  412. if (s->msg_callback)
  413. s->msg_callback(0, s->version, 0, p, (size_t)len, s, s->msg_callback_arg); /* CLIENT-MASTER-KEY */
  414. p += 10;
  415. memcpy(s->session->key_arg,&(p[s->s2->tmp.clear+s->s2->tmp.enc]),
  416. (unsigned int)keya);
  417. if (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL)
  418. {
  419. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  420. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_NO_PRIVATEKEY);
  421. return(-1);
  422. }
  423. i=ssl_rsa_private_decrypt(s->cert,s->s2->tmp.enc,
  424. &(p[s->s2->tmp.clear]),&(p[s->s2->tmp.clear]),
  425. (s->s2->ssl2_rollback)?RSA_SSLV23_PADDING:RSA_PKCS1_PADDING);
  426. is_export=SSL_C_IS_EXPORT(s->session->cipher);
  427. if (!ssl_cipher_get_evp(s->session,&c,&md,NULL))
  428. {
  429. ssl2_return_error(s,SSL2_PE_NO_CIPHER);
  430. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
  431. return(0);
  432. }
  433. if (s->session->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC)
  434. {
  435. is_export=1;
  436. ek=8;
  437. }
  438. else
  439. ek=5;
  440. /* bad decrypt */
  441. #if 1
  442. /* If a bad decrypt, continue with protocol but with a
  443. * random master secret (Bleichenbacher attack) */
  444. if ((i < 0) ||
  445. ((!is_export && (i != EVP_CIPHER_key_length(c)))
  446. || (is_export && ((i != ek) || (s->s2->tmp.clear+(unsigned int)i !=
  447. (unsigned int)EVP_CIPHER_key_length(c))))))
  448. {
  449. ERR_clear_error();
  450. if (is_export)
  451. i=ek;
  452. else
  453. i=EVP_CIPHER_key_length(c);
  454. RAND_pseudo_bytes(p,i);
  455. }
  456. #else
  457. if (i < 0)
  458. {
  459. error=1;
  460. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_BAD_RSA_DECRYPT);
  461. }
  462. /* incorrect number of key bytes for non export cipher */
  463. else if ((!is_export && (i != EVP_CIPHER_key_length(c)))
  464. || (is_export && ((i != ek) || (s->s2->tmp.clear+i !=
  465. EVP_CIPHER_key_length(c)))))
  466. {
  467. error=1;
  468. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_WRONG_NUMBER_OF_KEY_BITS);
  469. }
  470. if (error)
  471. {
  472. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  473. return(-1);
  474. }
  475. #endif
  476. if (is_export) i+=s->s2->tmp.clear;
  477. if (i > SSL_MAX_MASTER_KEY_LENGTH)
  478. {
  479. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  480. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
  481. return -1;
  482. }
  483. s->session->master_key_length=i;
  484. memcpy(s->session->master_key,p,(unsigned int)i);
  485. return(1);
  486. }
  487. static int get_client_hello(SSL *s)
  488. {
  489. int i,n;
  490. unsigned long len;
  491. unsigned char *p;
  492. STACK_OF(SSL_CIPHER) *cs; /* a stack of SSL_CIPHERS */
  493. STACK_OF(SSL_CIPHER) *cl; /* the ones we want to use */
  494. STACK_OF(SSL_CIPHER) *prio, *allow;
  495. int z;
  496. /* This is a bit of a hack to check for the correct packet
  497. * type the first time round. */
  498. if (s->state == SSL2_ST_GET_CLIENT_HELLO_A)
  499. {
  500. s->first_packet=1;
  501. s->state=SSL2_ST_GET_CLIENT_HELLO_B;
  502. }
  503. p=(unsigned char *)s->init_buf->data;
  504. if (s->state == SSL2_ST_GET_CLIENT_HELLO_B)
  505. {
  506. i=ssl2_read(s,(char *)&(p[s->init_num]),9-s->init_num);
  507. if (i < (9-s->init_num))
  508. return(ssl2_part_read(s,SSL_F_GET_CLIENT_HELLO,i));
  509. s->init_num = 9;
  510. if (*(p++) != SSL2_MT_CLIENT_HELLO)
  511. {
  512. if (p[-1] != SSL2_MT_ERROR)
  513. {
  514. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  515. SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_READ_WRONG_PACKET_TYPE);
  516. }
  517. else
  518. SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_PEER_ERROR);
  519. return(-1);
  520. }
  521. n2s(p,i);
  522. if (i < s->version) s->version=i;
  523. n2s(p,i); s->s2->tmp.cipher_spec_length=i;
  524. n2s(p,i); s->s2->tmp.session_id_length=i;
  525. n2s(p,i); s->s2->challenge_length=i;
  526. if ( (i < SSL2_MIN_CHALLENGE_LENGTH) ||
  527. (i > SSL2_MAX_CHALLENGE_LENGTH))
  528. {
  529. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  530. SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_INVALID_CHALLENGE_LENGTH);
  531. return(-1);
  532. }
  533. s->state=SSL2_ST_GET_CLIENT_HELLO_C;
  534. }
  535. /* SSL2_ST_GET_CLIENT_HELLO_C */
  536. p=(unsigned char *)s->init_buf->data;
  537. len = 9 + (unsigned long)s->s2->tmp.cipher_spec_length + (unsigned long)s->s2->challenge_length + (unsigned long)s->s2->tmp.session_id_length;
  538. if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
  539. {
  540. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  541. SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_MESSAGE_TOO_LONG);
  542. return -1;
  543. }
  544. n = (int)len - s->init_num;
  545. i = ssl2_read(s,(char *)&(p[s->init_num]),n);
  546. if (i != n) return(ssl2_part_read(s,SSL_F_GET_CLIENT_HELLO,i));
  547. if (s->msg_callback)
  548. s->msg_callback(0, s->version, 0, p, (size_t)len, s, s->msg_callback_arg); /* CLIENT-HELLO */
  549. p += 9;
  550. /* get session-id before cipher stuff so we can get out session
  551. * structure if it is cached */
  552. /* session-id */
  553. if ((s->s2->tmp.session_id_length != 0) &&
  554. (s->s2->tmp.session_id_length != SSL2_SSL_SESSION_ID_LENGTH))
  555. {
  556. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  557. SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_BAD_SSL_SESSION_ID_LENGTH);
  558. return(-1);
  559. }
  560. if (s->s2->tmp.session_id_length == 0)
  561. {
  562. if (!ssl_get_new_session(s,1))
  563. {
  564. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  565. return(-1);
  566. }
  567. }
  568. else
  569. {
  570. i=ssl_get_prev_session(s,&(p[s->s2->tmp.cipher_spec_length]),
  571. s->s2->tmp.session_id_length);
  572. if (i == 1)
  573. { /* previous session */
  574. s->hit=1;
  575. }
  576. else if (i == -1)
  577. {
  578. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  579. return(-1);
  580. }
  581. else
  582. {
  583. if (s->cert == NULL)
  584. {
  585. ssl2_return_error(s,SSL2_PE_NO_CERTIFICATE);
  586. SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_NO_CERTIFICATE_SET);
  587. return(-1);
  588. }
  589. if (!ssl_get_new_session(s,1))
  590. {
  591. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  592. return(-1);
  593. }
  594. }
  595. }
  596. if (!s->hit)
  597. {
  598. cs=ssl_bytes_to_cipher_list(s,p,s->s2->tmp.cipher_spec_length,
  599. &s->session->ciphers);
  600. if (cs == NULL) goto mem_err;
  601. cl=SSL_get_ciphers(s);
  602. if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
  603. {
  604. prio=sk_SSL_CIPHER_dup(cl);
  605. if (prio == NULL) goto mem_err;
  606. allow = cs;
  607. }
  608. else
  609. {
  610. prio = cs;
  611. allow = cl;
  612. }
  613. for (z=0; z<sk_SSL_CIPHER_num(prio); z++)
  614. {
  615. if (sk_SSL_CIPHER_find(allow,sk_SSL_CIPHER_value(prio,z)) < 0)
  616. {
  617. sk_SSL_CIPHER_delete(prio,z);
  618. z--;
  619. }
  620. }
  621. if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
  622. {
  623. sk_SSL_CIPHER_free(s->session->ciphers);
  624. s->session->ciphers = prio;
  625. }
  626. /* s->session->ciphers should now have a list of
  627. * ciphers that are on both the client and server.
  628. * This list is ordered by the order the client sent
  629. * the ciphers or in the order of the server's preference
  630. * if SSL_OP_CIPHER_SERVER_PREFERENCE was set.
  631. */
  632. }
  633. p+=s->s2->tmp.cipher_spec_length;
  634. /* done cipher selection */
  635. /* session id extracted already */
  636. p+=s->s2->tmp.session_id_length;
  637. /* challenge */
  638. if (s->s2->challenge_length > sizeof s->s2->challenge)
  639. {
  640. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  641. SSLerr(SSL_F_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  642. return -1;
  643. }
  644. memcpy(s->s2->challenge,p,(unsigned int)s->s2->challenge_length);
  645. return(1);
  646. mem_err:
  647. SSLerr(SSL_F_GET_CLIENT_HELLO,ERR_R_MALLOC_FAILURE);
  648. return(0);
  649. }
  650. static int server_hello(SSL *s)
  651. {
  652. unsigned char *p,*d;
  653. int n,hit;
  654. STACK_OF(SSL_CIPHER) *sk;
  655. p=(unsigned char *)s->init_buf->data;
  656. if (s->state == SSL2_ST_SEND_SERVER_HELLO_A)
  657. {
  658. d=p+11;
  659. *(p++)=SSL2_MT_SERVER_HELLO; /* type */
  660. hit=s->hit;
  661. *(p++)=(unsigned char)hit;
  662. #if 1
  663. if (!hit)
  664. {
  665. if (s->session->sess_cert != NULL)
  666. /* This can't really happen because get_client_hello
  667. * has called ssl_get_new_session, which does not set
  668. * sess_cert. */
  669. ssl_sess_cert_free(s->session->sess_cert);
  670. s->session->sess_cert = ssl_sess_cert_new();
  671. if (s->session->sess_cert == NULL)
  672. {
  673. SSLerr(SSL_F_SERVER_HELLO, ERR_R_MALLOC_FAILURE);
  674. return(-1);
  675. }
  676. }
  677. /* If 'hit' is set, then s->sess_cert may be non-NULL or NULL,
  678. * depending on whether it survived in the internal cache
  679. * or was retrieved from an external cache.
  680. * If it is NULL, we cannot put any useful data in it anyway,
  681. * so we don't touch it.
  682. */
  683. #else /* That's what used to be done when cert_st and sess_cert_st were
  684. * the same. */
  685. if (!hit)
  686. { /* else add cert to session */
  687. CRYPTO_add(&s->cert->references,1,CRYPTO_LOCK_SSL_CERT);
  688. if (s->session->sess_cert != NULL)
  689. ssl_cert_free(s->session->sess_cert);
  690. s->session->sess_cert=s->cert;
  691. }
  692. else /* We have a session id-cache hit, if the
  693. * session-id has no certificate listed against
  694. * the 'cert' structure, grab the 'old' one
  695. * listed against the SSL connection */
  696. {
  697. if (s->session->sess_cert == NULL)
  698. {
  699. CRYPTO_add(&s->cert->references,1,
  700. CRYPTO_LOCK_SSL_CERT);
  701. s->session->sess_cert=s->cert;
  702. }
  703. }
  704. #endif
  705. if (s->cert == NULL)
  706. {
  707. ssl2_return_error(s,SSL2_PE_NO_CERTIFICATE);
  708. SSLerr(SSL_F_SERVER_HELLO,SSL_R_NO_CERTIFICATE_SPECIFIED);
  709. return(-1);
  710. }
  711. if (hit)
  712. {
  713. *(p++)=0; /* no certificate type */
  714. s2n(s->version,p); /* version */
  715. s2n(0,p); /* cert len */
  716. s2n(0,p); /* ciphers len */
  717. }
  718. else
  719. {
  720. /* EAY EAY */
  721. /* put certificate type */
  722. *(p++)=SSL2_CT_X509_CERTIFICATE;
  723. s2n(s->version,p); /* version */
  724. n=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);
  725. s2n(n,p); /* certificate length */
  726. i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&d);
  727. n=0;
  728. /* lets send out the ciphers we like in the
  729. * prefered order */
  730. sk= s->session->ciphers;
  731. n=ssl_cipher_list_to_bytes(s,s->session->ciphers,d);
  732. d+=n;
  733. s2n(n,p); /* add cipher length */
  734. }
  735. /* make and send conn_id */
  736. s2n(SSL2_CONNECTION_ID_LENGTH,p); /* add conn_id length */
  737. s->s2->conn_id_length=SSL2_CONNECTION_ID_LENGTH;
  738. RAND_pseudo_bytes(s->s2->conn_id,(int)s->s2->conn_id_length);
  739. memcpy(d,s->s2->conn_id,SSL2_CONNECTION_ID_LENGTH);
  740. d+=SSL2_CONNECTION_ID_LENGTH;
  741. s->state=SSL2_ST_SEND_SERVER_HELLO_B;
  742. s->init_num=d-(unsigned char *)s->init_buf->data;
  743. s->init_off=0;
  744. }
  745. /* SSL2_ST_SEND_SERVER_HELLO_B */
  746. /* If we are using TCP/IP, the performance is bad if we do 2
  747. * writes without a read between them. This occurs when
  748. * Session-id reuse is used, so I will put in a buffering module
  749. */
  750. if (s->hit)
  751. {
  752. if (!ssl_init_wbio_buffer(s,1)) return(-1);
  753. }
  754. return(ssl2_do_write(s));
  755. }
  756. static int get_client_finished(SSL *s)
  757. {
  758. unsigned char *p;
  759. int i, n;
  760. unsigned long len;
  761. p=(unsigned char *)s->init_buf->data;
  762. if (s->state == SSL2_ST_GET_CLIENT_FINISHED_A)
  763. {
  764. i=ssl2_read(s,(char *)&(p[s->init_num]),1-s->init_num);
  765. if (i < 1-s->init_num)
  766. return(ssl2_part_read(s,SSL_F_GET_CLIENT_FINISHED,i));
  767. s->init_num += i;
  768. if (*p != SSL2_MT_CLIENT_FINISHED)
  769. {
  770. if (*p != SSL2_MT_ERROR)
  771. {
  772. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  773. SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_READ_WRONG_PACKET_TYPE);
  774. }
  775. else
  776. {
  777. SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_PEER_ERROR);
  778. /* try to read the error message */
  779. i=ssl2_read(s,(char *)&(p[s->init_num]),3-s->init_num);
  780. return ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i);
  781. }
  782. return(-1);
  783. }
  784. s->state=SSL2_ST_GET_CLIENT_FINISHED_B;
  785. }
  786. /* SSL2_ST_GET_CLIENT_FINISHED_B */
  787. if (s->s2->conn_id_length > sizeof s->s2->conn_id)
  788. {
  789. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  790. SSLerr(SSL_F_GET_CLIENT_FINISHED, ERR_R_INTERNAL_ERROR);
  791. return -1;
  792. }
  793. len = 1 + (unsigned long)s->s2->conn_id_length;
  794. n = (int)len - s->init_num;
  795. i = ssl2_read(s,(char *)&(p[s->init_num]),n);
  796. if (i < n)
  797. {
  798. return(ssl2_part_read(s,SSL_F_GET_CLIENT_FINISHED,i));
  799. }
  800. if (s->msg_callback)
  801. s->msg_callback(0, s->version, 0, p, len, s, s->msg_callback_arg); /* CLIENT-FINISHED */
  802. p += 1;
  803. if (memcmp(p,s->s2->conn_id,s->s2->conn_id_length) != 0)
  804. {
  805. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  806. SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_CONNECTION_ID_IS_DIFFERENT);
  807. return(-1);
  808. }
  809. return(1);
  810. }
  811. static int server_verify(SSL *s)
  812. {
  813. unsigned char *p;
  814. if (s->state == SSL2_ST_SEND_SERVER_VERIFY_A)
  815. {
  816. p=(unsigned char *)s->init_buf->data;
  817. *(p++)=SSL2_MT_SERVER_VERIFY;
  818. if (s->s2->challenge_length > sizeof s->s2->challenge)
  819. {
  820. SSLerr(SSL_F_SERVER_VERIFY, ERR_R_INTERNAL_ERROR);
  821. return -1;
  822. }
  823. memcpy(p,s->s2->challenge,(unsigned int)s->s2->challenge_length);
  824. /* p+=s->s2->challenge_length; */
  825. s->state=SSL2_ST_SEND_SERVER_VERIFY_B;
  826. s->init_num=s->s2->challenge_length+1;
  827. s->init_off=0;
  828. }
  829. return(ssl2_do_write(s));
  830. }
  831. static int server_finish(SSL *s)
  832. {
  833. unsigned char *p;
  834. if (s->state == SSL2_ST_SEND_SERVER_FINISHED_A)
  835. {
  836. p=(unsigned char *)s->init_buf->data;
  837. *(p++)=SSL2_MT_SERVER_FINISHED;
  838. if (s->session->session_id_length > sizeof s->session->session_id)
  839. {
  840. SSLerr(SSL_F_SERVER_FINISH, ERR_R_INTERNAL_ERROR);
  841. return -1;
  842. }
  843. memcpy(p,s->session->session_id, (unsigned int)s->session->session_id_length);
  844. /* p+=s->session->session_id_length; */
  845. s->state=SSL2_ST_SEND_SERVER_FINISHED_B;
  846. s->init_num=s->session->session_id_length+1;
  847. s->init_off=0;
  848. }
  849. /* SSL2_ST_SEND_SERVER_FINISHED_B */
  850. return(ssl2_do_write(s));
  851. }
  852. /* send the request and check the response */
  853. static int request_certificate(SSL *s)
  854. {
  855. unsigned char *p,*p2,*buf2;
  856. unsigned char *ccd;
  857. int i,j,ctype,ret= -1;
  858. unsigned long len;
  859. X509 *x509=NULL;
  860. STACK_OF(X509) *sk=NULL;
  861. ccd=s->s2->tmp.ccl;
  862. if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_A)
  863. {
  864. p=(unsigned char *)s->init_buf->data;
  865. *(p++)=SSL2_MT_REQUEST_CERTIFICATE;
  866. *(p++)=SSL2_AT_MD5_WITH_RSA_ENCRYPTION;
  867. RAND_pseudo_bytes(ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
  868. memcpy(p,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
  869. s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_B;
  870. s->init_num=SSL2_MIN_CERT_CHALLENGE_LENGTH+2;
  871. s->init_off=0;
  872. }
  873. if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_B)
  874. {
  875. i=ssl2_do_write(s);
  876. if (i <= 0)
  877. {
  878. ret=i;
  879. goto end;
  880. }
  881. s->init_num=0;
  882. s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_C;
  883. }
  884. if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_C)
  885. {
  886. p=(unsigned char *)s->init_buf->data;
  887. i=ssl2_read(s,(char *)&(p[s->init_num]),6-s->init_num); /* try to read 6 octets ... */
  888. if (i < 3-s->init_num) /* ... but don't call ssl2_part_read now if we got at least 3
  889. * (probably NO-CERTIFICATE-ERROR) */
  890. {
  891. ret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);
  892. goto end;
  893. }
  894. s->init_num += i;
  895. if ((s->init_num >= 3) && (p[0] == SSL2_MT_ERROR))
  896. {
  897. n2s(p,i);
  898. if (i != SSL2_PE_NO_CERTIFICATE)
  899. {
  900. /* not the error message we expected -- let ssl2_part_read handle it */
  901. s->init_num -= 3;
  902. ret = ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE, 3);
  903. goto end;
  904. }
  905. if (s->msg_callback)
  906. s->msg_callback(0, s->version, 0, p, 3, s, s->msg_callback_arg); /* ERROR */
  907. /* this is the one place where we can recover from an SSL 2.0 error */
  908. if (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  909. {
  910. ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
  911. SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  912. goto end;
  913. }
  914. ret=1;
  915. goto end;
  916. }
  917. if ((*(p++) != SSL2_MT_CLIENT_CERTIFICATE) || (s->init_num < 6))
  918. {
  919. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  920. SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_SHORT_READ);
  921. goto end;
  922. }
  923. if (s->init_num != 6)
  924. {
  925. SSLerr(SSL_F_REQUEST_CERTIFICATE, ERR_R_INTERNAL_ERROR);
  926. goto end;
  927. }
  928. /* ok we have a response */
  929. /* certificate type, there is only one right now. */
  930. ctype= *(p++);
  931. if (ctype != SSL2_AT_MD5_WITH_RSA_ENCRYPTION)
  932. {
  933. ssl2_return_error(s,SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE);
  934. SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_RESPONSE_ARGUMENT);
  935. goto end;
  936. }
  937. n2s(p,i); s->s2->tmp.clen=i;
  938. n2s(p,i); s->s2->tmp.rlen=i;
  939. s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_D;
  940. }
  941. /* SSL2_ST_SEND_REQUEST_CERTIFICATE_D */
  942. p=(unsigned char *)s->init_buf->data;
  943. len = 6 + (unsigned long)s->s2->tmp.clen + (unsigned long)s->s2->tmp.rlen;
  944. if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
  945. {
  946. SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_MESSAGE_TOO_LONG);
  947. goto end;
  948. }
  949. j = (int)len - s->init_num;
  950. i = ssl2_read(s,(char *)&(p[s->init_num]),j);
  951. if (i < j)
  952. {
  953. ret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);
  954. goto end;
  955. }
  956. if (s->msg_callback)
  957. s->msg_callback(0, s->version, 0, p, len, s, s->msg_callback_arg); /* CLIENT-CERTIFICATE */
  958. p += 6;
  959. x509=(X509 *)d2i_X509(NULL,&p,(long)s->s2->tmp.clen);
  960. if (x509 == NULL)
  961. {
  962. SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_X509_LIB);
  963. goto msg_end;
  964. }
  965. if (((sk=sk_X509_new_null()) == NULL) || (!sk_X509_push(sk,x509)))
  966. {
  967. SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  968. goto msg_end;
  969. }
  970. i=ssl_verify_cert_chain(s,sk);
  971. if (i) /* we like the packet, now check the chksum */
  972. {
  973. EVP_MD_CTX ctx;
  974. EVP_PKEY *pkey=NULL;
  975. EVP_MD_CTX_init(&ctx);
  976. EVP_VerifyInit_ex(&ctx,s->ctx->rsa_md5, NULL);
  977. EVP_VerifyUpdate(&ctx,s->s2->key_material,
  978. s->s2->key_material_length);
  979. EVP_VerifyUpdate(&ctx,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
  980. i=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);
  981. buf2=OPENSSL_malloc((unsigned int)i);
  982. if (buf2 == NULL)
  983. {
  984. SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  985. goto msg_end;
  986. }
  987. p2=buf2;
  988. i=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&p2);
  989. EVP_VerifyUpdate(&ctx,buf2,(unsigned int)i);
  990. OPENSSL_free(buf2);
  991. pkey=X509_get_pubkey(x509);
  992. if (pkey == NULL) goto end;
  993. i=EVP_VerifyFinal(&ctx,p,s->s2->tmp.rlen,pkey);
  994. EVP_PKEY_free(pkey);
  995. EVP_MD_CTX_cleanup(&ctx);
  996. if (i)
  997. {
  998. if (s->session->peer != NULL)
  999. X509_free(s->session->peer);
  1000. s->session->peer=x509;
  1001. CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509);
  1002. s->session->verify_result = s->verify_result;
  1003. ret=1;
  1004. goto end;
  1005. }
  1006. else
  1007. {
  1008. SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_CHECKSUM);
  1009. goto msg_end;
  1010. }
  1011. }
  1012. else
  1013. {
  1014. msg_end:
  1015. ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
  1016. }
  1017. end:
  1018. sk_X509_free(sk);
  1019. X509_free(x509);
  1020. return(ret);
  1021. }
  1022. static int ssl_rsa_private_decrypt(CERT *c, int len, unsigned char *from,
  1023. unsigned char *to, int padding)
  1024. {
  1025. RSA *rsa;
  1026. int i;
  1027. if ((c == NULL) || (c->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL))
  1028. {
  1029. SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,SSL_R_NO_PRIVATEKEY);
  1030. return(-1);
  1031. }
  1032. if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey->type != EVP_PKEY_RSA)
  1033. {
  1034. SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,SSL_R_PUBLIC_KEY_IS_NOT_RSA);
  1035. return(-1);
  1036. }
  1037. rsa=c->pkeys[SSL_PKEY_RSA_ENC].privatekey->pkey.rsa;
  1038. /* we have the public key */
  1039. i=RSA_private_decrypt(len,from,to,rsa,padding);
  1040. if (i < 0)
  1041. SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,ERR_R_RSA_LIB);
  1042. return(i);
  1043. }
  1044. #else /* !OPENSSL_NO_SSL2 */
  1045. # if PEDANTIC
  1046. static void *dummy=&dummy;
  1047. # endif
  1048. #endif