s2_srvr.c 31 KB

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