s2_srvr.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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);
  370. if(i > 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->session->key_arg_length=i;
  377. s->state=SSL2_ST_GET_CLIENT_MASTER_KEY_B;
  378. }
  379. /* SSL2_ST_GET_CLIENT_MASTER_KEY_B */
  380. p=(unsigned char *)s->init_buf->data;
  381. if (s->init_buf->length < SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
  382. {
  383. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  384. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
  385. return -1;
  386. }
  387. keya=s->session->key_arg_length;
  388. len = 10 + (unsigned long)s->s2->tmp.clear + (unsigned long)s->s2->tmp.enc + (unsigned long)keya;
  389. if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
  390. {
  391. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  392. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_MESSAGE_TOO_LONG);
  393. return -1;
  394. }
  395. n = (int)len - s->init_num;
  396. i = ssl2_read(s,(char *)&(p[s->init_num]),n);
  397. if (i != n) return(ssl2_part_read(s,SSL_F_GET_CLIENT_MASTER_KEY,i));
  398. if (s->msg_callback)
  399. s->msg_callback(0, s->version, 0, p, (size_t)len, s, s->msg_callback_arg); /* CLIENT-MASTER-KEY */
  400. p += 10;
  401. memcpy(s->session->key_arg,&(p[s->s2->tmp.clear+s->s2->tmp.enc]),
  402. (unsigned int)keya);
  403. if (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL)
  404. {
  405. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  406. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_NO_PRIVATEKEY);
  407. return(-1);
  408. }
  409. i=ssl_rsa_private_decrypt(s->cert,s->s2->tmp.enc,
  410. &(p[s->s2->tmp.clear]),&(p[s->s2->tmp.clear]),
  411. (s->s2->ssl2_rollback)?RSA_SSLV23_PADDING:RSA_PKCS1_PADDING);
  412. is_export=SSL_C_IS_EXPORT(s->session->cipher);
  413. if (!ssl_cipher_get_evp(s->session,&c,&md,NULL,NULL,NULL))
  414. {
  415. ssl2_return_error(s,SSL2_PE_NO_CIPHER);
  416. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
  417. return(0);
  418. }
  419. if (s->session->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC)
  420. {
  421. is_export=1;
  422. ek=8;
  423. }
  424. else
  425. ek=5;
  426. /* bad decrypt */
  427. #if 1
  428. /* If a bad decrypt, continue with protocol but with a
  429. * random master secret (Bleichenbacher attack) */
  430. if ((i < 0) ||
  431. ((!is_export && (i != EVP_CIPHER_key_length(c)))
  432. || (is_export && ((i != ek) || (s->s2->tmp.clear+(unsigned int)i !=
  433. (unsigned int)EVP_CIPHER_key_length(c))))))
  434. {
  435. ERR_clear_error();
  436. if (is_export)
  437. i=ek;
  438. else
  439. i=EVP_CIPHER_key_length(c);
  440. if (RAND_pseudo_bytes(p,i) <= 0)
  441. return 0;
  442. }
  443. #else
  444. if (i < 0)
  445. {
  446. error=1;
  447. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_BAD_RSA_DECRYPT);
  448. }
  449. /* incorrect number of key bytes for non export cipher */
  450. else if ((!is_export && (i != EVP_CIPHER_key_length(c)))
  451. || (is_export && ((i != ek) || (s->s2->tmp.clear+i !=
  452. EVP_CIPHER_key_length(c)))))
  453. {
  454. error=1;
  455. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_WRONG_NUMBER_OF_KEY_BITS);
  456. }
  457. if (error)
  458. {
  459. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  460. return(-1);
  461. }
  462. #endif
  463. if (is_export) i+=s->s2->tmp.clear;
  464. if (i > SSL_MAX_MASTER_KEY_LENGTH)
  465. {
  466. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  467. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
  468. return -1;
  469. }
  470. s->session->master_key_length=i;
  471. memcpy(s->session->master_key,p,(unsigned int)i);
  472. return(1);
  473. }
  474. static int get_client_hello(SSL *s)
  475. {
  476. int i,n;
  477. unsigned long len;
  478. unsigned char *p;
  479. STACK_OF(SSL_CIPHER) *cs; /* a stack of SSL_CIPHERS */
  480. STACK_OF(SSL_CIPHER) *cl; /* the ones we want to use */
  481. STACK_OF(SSL_CIPHER) *prio, *allow;
  482. int z;
  483. /* This is a bit of a hack to check for the correct packet
  484. * type the first time round. */
  485. if (s->state == SSL2_ST_GET_CLIENT_HELLO_A)
  486. {
  487. s->first_packet=1;
  488. s->state=SSL2_ST_GET_CLIENT_HELLO_B;
  489. }
  490. p=(unsigned char *)s->init_buf->data;
  491. if (s->state == SSL2_ST_GET_CLIENT_HELLO_B)
  492. {
  493. i=ssl2_read(s,(char *)&(p[s->init_num]),9-s->init_num);
  494. if (i < (9-s->init_num))
  495. return(ssl2_part_read(s,SSL_F_GET_CLIENT_HELLO,i));
  496. s->init_num = 9;
  497. if (*(p++) != SSL2_MT_CLIENT_HELLO)
  498. {
  499. if (p[-1] != SSL2_MT_ERROR)
  500. {
  501. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  502. SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_READ_WRONG_PACKET_TYPE);
  503. }
  504. else
  505. SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_PEER_ERROR);
  506. return(-1);
  507. }
  508. n2s(p,i);
  509. if (i < s->version) s->version=i;
  510. n2s(p,i); s->s2->tmp.cipher_spec_length=i;
  511. n2s(p,i); s->s2->tmp.session_id_length=i;
  512. n2s(p,i); s->s2->challenge_length=i;
  513. if ( (i < SSL2_MIN_CHALLENGE_LENGTH) ||
  514. (i > SSL2_MAX_CHALLENGE_LENGTH))
  515. {
  516. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  517. SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_INVALID_CHALLENGE_LENGTH);
  518. return(-1);
  519. }
  520. s->state=SSL2_ST_GET_CLIENT_HELLO_C;
  521. }
  522. /* SSL2_ST_GET_CLIENT_HELLO_C */
  523. p=(unsigned char *)s->init_buf->data;
  524. len = 9 + (unsigned long)s->s2->tmp.cipher_spec_length + (unsigned long)s->s2->challenge_length + (unsigned long)s->s2->tmp.session_id_length;
  525. if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
  526. {
  527. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  528. SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_MESSAGE_TOO_LONG);
  529. return -1;
  530. }
  531. n = (int)len - s->init_num;
  532. i = ssl2_read(s,(char *)&(p[s->init_num]),n);
  533. if (i != n) return(ssl2_part_read(s,SSL_F_GET_CLIENT_HELLO,i));
  534. if (s->msg_callback)
  535. s->msg_callback(0, s->version, 0, p, (size_t)len, s, s->msg_callback_arg); /* CLIENT-HELLO */
  536. p += 9;
  537. /* get session-id before cipher stuff so we can get out session
  538. * structure if it is cached */
  539. /* session-id */
  540. if ((s->s2->tmp.session_id_length != 0) &&
  541. (s->s2->tmp.session_id_length != SSL2_SSL_SESSION_ID_LENGTH))
  542. {
  543. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  544. SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_BAD_SSL_SESSION_ID_LENGTH);
  545. return(-1);
  546. }
  547. if (s->s2->tmp.session_id_length == 0)
  548. {
  549. if (!ssl_get_new_session(s,1))
  550. {
  551. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  552. return(-1);
  553. }
  554. }
  555. else
  556. {
  557. i=ssl_get_prev_session(s,&(p[s->s2->tmp.cipher_spec_length]),
  558. s->s2->tmp.session_id_length, NULL);
  559. if (i == 1)
  560. { /* previous session */
  561. s->hit=1;
  562. }
  563. else if (i == -1)
  564. {
  565. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  566. return(-1);
  567. }
  568. else
  569. {
  570. if (s->cert == NULL)
  571. {
  572. ssl2_return_error(s,SSL2_PE_NO_CERTIFICATE);
  573. SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_NO_CERTIFICATE_SET);
  574. return(-1);
  575. }
  576. if (!ssl_get_new_session(s,1))
  577. {
  578. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  579. return(-1);
  580. }
  581. }
  582. }
  583. if (!s->hit)
  584. {
  585. cs=ssl_bytes_to_cipher_list(s,p,s->s2->tmp.cipher_spec_length,
  586. &s->session->ciphers);
  587. if (cs == NULL) goto mem_err;
  588. cl=SSL_get_ciphers(s);
  589. if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
  590. {
  591. prio=sk_SSL_CIPHER_dup(cl);
  592. if (prio == NULL) goto mem_err;
  593. allow = cs;
  594. }
  595. else
  596. {
  597. prio = cs;
  598. allow = cl;
  599. }
  600. for (z=0; z<sk_SSL_CIPHER_num(prio); z++)
  601. {
  602. if (sk_SSL_CIPHER_find(allow,sk_SSL_CIPHER_value(prio,z)) < 0)
  603. {
  604. (void)sk_SSL_CIPHER_delete(prio,z);
  605. z--;
  606. }
  607. }
  608. if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
  609. {
  610. sk_SSL_CIPHER_free(s->session->ciphers);
  611. s->session->ciphers = prio;
  612. }
  613. /* s->session->ciphers should now have a list of
  614. * ciphers that are on both the client and server.
  615. * This list is ordered by the order the client sent
  616. * the ciphers or in the order of the server's preference
  617. * if SSL_OP_CIPHER_SERVER_PREFERENCE was set.
  618. */
  619. }
  620. p+=s->s2->tmp.cipher_spec_length;
  621. /* done cipher selection */
  622. /* session id extracted already */
  623. p+=s->s2->tmp.session_id_length;
  624. /* challenge */
  625. if (s->s2->challenge_length > sizeof s->s2->challenge)
  626. {
  627. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  628. SSLerr(SSL_F_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  629. return -1;
  630. }
  631. memcpy(s->s2->challenge,p,(unsigned int)s->s2->challenge_length);
  632. return(1);
  633. mem_err:
  634. SSLerr(SSL_F_GET_CLIENT_HELLO,ERR_R_MALLOC_FAILURE);
  635. return(0);
  636. }
  637. static int server_hello(SSL *s)
  638. {
  639. unsigned char *p,*d;
  640. int n,hit;
  641. p=(unsigned char *)s->init_buf->data;
  642. if (s->state == SSL2_ST_SEND_SERVER_HELLO_A)
  643. {
  644. d=p+11;
  645. *(p++)=SSL2_MT_SERVER_HELLO; /* type */
  646. hit=s->hit;
  647. *(p++)=(unsigned char)hit;
  648. #if 1
  649. if (!hit)
  650. {
  651. if (s->session->sess_cert != NULL)
  652. /* This can't really happen because get_client_hello
  653. * has called ssl_get_new_session, which does not set
  654. * sess_cert. */
  655. ssl_sess_cert_free(s->session->sess_cert);
  656. s->session->sess_cert = ssl_sess_cert_new();
  657. if (s->session->sess_cert == NULL)
  658. {
  659. SSLerr(SSL_F_SERVER_HELLO, ERR_R_MALLOC_FAILURE);
  660. return(-1);
  661. }
  662. }
  663. /* If 'hit' is set, then s->sess_cert may be non-NULL or NULL,
  664. * depending on whether it survived in the internal cache
  665. * or was retrieved from an external cache.
  666. * If it is NULL, we cannot put any useful data in it anyway,
  667. * so we don't touch it.
  668. */
  669. #else /* That's what used to be done when cert_st and sess_cert_st were
  670. * the same. */
  671. if (!hit)
  672. { /* else add cert to session */
  673. CRYPTO_add(&s->cert->references,1,CRYPTO_LOCK_SSL_CERT);
  674. if (s->session->sess_cert != NULL)
  675. ssl_cert_free(s->session->sess_cert);
  676. s->session->sess_cert=s->cert;
  677. }
  678. else /* We have a session id-cache hit, if the
  679. * session-id has no certificate listed against
  680. * the 'cert' structure, grab the 'old' one
  681. * listed against the SSL connection */
  682. {
  683. if (s->session->sess_cert == NULL)
  684. {
  685. CRYPTO_add(&s->cert->references,1,
  686. CRYPTO_LOCK_SSL_CERT);
  687. s->session->sess_cert=s->cert;
  688. }
  689. }
  690. #endif
  691. if (s->cert == NULL)
  692. {
  693. ssl2_return_error(s,SSL2_PE_NO_CERTIFICATE);
  694. SSLerr(SSL_F_SERVER_HELLO,SSL_R_NO_CERTIFICATE_SPECIFIED);
  695. return(-1);
  696. }
  697. if (hit)
  698. {
  699. *(p++)=0; /* no certificate type */
  700. s2n(s->version,p); /* version */
  701. s2n(0,p); /* cert len */
  702. s2n(0,p); /* ciphers len */
  703. }
  704. else
  705. {
  706. /* EAY EAY */
  707. /* put certificate type */
  708. *(p++)=SSL2_CT_X509_CERTIFICATE;
  709. s2n(s->version,p); /* version */
  710. n=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);
  711. s2n(n,p); /* certificate length */
  712. i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&d);
  713. n=0;
  714. /* lets send out the ciphers we like in the
  715. * prefered order */
  716. n=ssl_cipher_list_to_bytes(s,s->session->ciphers,d,0);
  717. d+=n;
  718. s2n(n,p); /* add cipher length */
  719. }
  720. /* make and send conn_id */
  721. s2n(SSL2_CONNECTION_ID_LENGTH,p); /* add conn_id length */
  722. s->s2->conn_id_length=SSL2_CONNECTION_ID_LENGTH;
  723. if (RAND_pseudo_bytes(s->s2->conn_id,(int)s->s2->conn_id_length) <= 0)
  724. return -1;
  725. memcpy(d,s->s2->conn_id,SSL2_CONNECTION_ID_LENGTH);
  726. d+=SSL2_CONNECTION_ID_LENGTH;
  727. s->state=SSL2_ST_SEND_SERVER_HELLO_B;
  728. s->init_num=d-(unsigned char *)s->init_buf->data;
  729. s->init_off=0;
  730. }
  731. /* SSL2_ST_SEND_SERVER_HELLO_B */
  732. /* If we are using TCP/IP, the performance is bad if we do 2
  733. * writes without a read between them. This occurs when
  734. * Session-id reuse is used, so I will put in a buffering module
  735. */
  736. if (s->hit)
  737. {
  738. if (!ssl_init_wbio_buffer(s,1)) return(-1);
  739. }
  740. return(ssl2_do_write(s));
  741. }
  742. static int get_client_finished(SSL *s)
  743. {
  744. unsigned char *p;
  745. int i, n;
  746. unsigned long len;
  747. p=(unsigned char *)s->init_buf->data;
  748. if (s->state == SSL2_ST_GET_CLIENT_FINISHED_A)
  749. {
  750. i=ssl2_read(s,(char *)&(p[s->init_num]),1-s->init_num);
  751. if (i < 1-s->init_num)
  752. return(ssl2_part_read(s,SSL_F_GET_CLIENT_FINISHED,i));
  753. s->init_num += i;
  754. if (*p != SSL2_MT_CLIENT_FINISHED)
  755. {
  756. if (*p != SSL2_MT_ERROR)
  757. {
  758. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  759. SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_READ_WRONG_PACKET_TYPE);
  760. }
  761. else
  762. {
  763. SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_PEER_ERROR);
  764. /* try to read the error message */
  765. i=ssl2_read(s,(char *)&(p[s->init_num]),3-s->init_num);
  766. return ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i);
  767. }
  768. return(-1);
  769. }
  770. s->state=SSL2_ST_GET_CLIENT_FINISHED_B;
  771. }
  772. /* SSL2_ST_GET_CLIENT_FINISHED_B */
  773. if (s->s2->conn_id_length > sizeof s->s2->conn_id)
  774. {
  775. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  776. SSLerr(SSL_F_GET_CLIENT_FINISHED, ERR_R_INTERNAL_ERROR);
  777. return -1;
  778. }
  779. len = 1 + (unsigned long)s->s2->conn_id_length;
  780. n = (int)len - s->init_num;
  781. i = ssl2_read(s,(char *)&(p[s->init_num]),n);
  782. if (i < n)
  783. {
  784. return(ssl2_part_read(s,SSL_F_GET_CLIENT_FINISHED,i));
  785. }
  786. if (s->msg_callback)
  787. s->msg_callback(0, s->version, 0, p, len, s, s->msg_callback_arg); /* CLIENT-FINISHED */
  788. p += 1;
  789. if (memcmp(p,s->s2->conn_id,s->s2->conn_id_length) != 0)
  790. {
  791. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  792. SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_CONNECTION_ID_IS_DIFFERENT);
  793. return(-1);
  794. }
  795. return(1);
  796. }
  797. static int server_verify(SSL *s)
  798. {
  799. unsigned char *p;
  800. if (s->state == SSL2_ST_SEND_SERVER_VERIFY_A)
  801. {
  802. p=(unsigned char *)s->init_buf->data;
  803. *(p++)=SSL2_MT_SERVER_VERIFY;
  804. if (s->s2->challenge_length > sizeof s->s2->challenge)
  805. {
  806. SSLerr(SSL_F_SERVER_VERIFY, ERR_R_INTERNAL_ERROR);
  807. return -1;
  808. }
  809. memcpy(p,s->s2->challenge,(unsigned int)s->s2->challenge_length);
  810. /* p+=s->s2->challenge_length; */
  811. s->state=SSL2_ST_SEND_SERVER_VERIFY_B;
  812. s->init_num=s->s2->challenge_length+1;
  813. s->init_off=0;
  814. }
  815. return(ssl2_do_write(s));
  816. }
  817. static int server_finish(SSL *s)
  818. {
  819. unsigned char *p;
  820. if (s->state == SSL2_ST_SEND_SERVER_FINISHED_A)
  821. {
  822. p=(unsigned char *)s->init_buf->data;
  823. *(p++)=SSL2_MT_SERVER_FINISHED;
  824. if (s->session->session_id_length > sizeof s->session->session_id)
  825. {
  826. SSLerr(SSL_F_SERVER_FINISH, ERR_R_INTERNAL_ERROR);
  827. return -1;
  828. }
  829. memcpy(p,s->session->session_id, (unsigned int)s->session->session_id_length);
  830. /* p+=s->session->session_id_length; */
  831. s->state=SSL2_ST_SEND_SERVER_FINISHED_B;
  832. s->init_num=s->session->session_id_length+1;
  833. s->init_off=0;
  834. }
  835. /* SSL2_ST_SEND_SERVER_FINISHED_B */
  836. return(ssl2_do_write(s));
  837. }
  838. /* send the request and check the response */
  839. static int request_certificate(SSL *s)
  840. {
  841. const unsigned char *cp;
  842. unsigned char *p,*p2,*buf2;
  843. unsigned char *ccd;
  844. int i,j,ctype,ret= -1;
  845. unsigned long len;
  846. X509 *x509=NULL;
  847. STACK_OF(X509) *sk=NULL;
  848. ccd=s->s2->tmp.ccl;
  849. if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_A)
  850. {
  851. p=(unsigned char *)s->init_buf->data;
  852. *(p++)=SSL2_MT_REQUEST_CERTIFICATE;
  853. *(p++)=SSL2_AT_MD5_WITH_RSA_ENCRYPTION;
  854. if (RAND_pseudo_bytes(ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH) <= 0)
  855. return -1;
  856. memcpy(p,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
  857. s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_B;
  858. s->init_num=SSL2_MIN_CERT_CHALLENGE_LENGTH+2;
  859. s->init_off=0;
  860. }
  861. if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_B)
  862. {
  863. i=ssl2_do_write(s);
  864. if (i <= 0)
  865. {
  866. ret=i;
  867. goto end;
  868. }
  869. s->init_num=0;
  870. s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_C;
  871. }
  872. if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_C)
  873. {
  874. p=(unsigned char *)s->init_buf->data;
  875. i=ssl2_read(s,(char *)&(p[s->init_num]),6-s->init_num); /* try to read 6 octets ... */
  876. if (i < 3-s->init_num) /* ... but don't call ssl2_part_read now if we got at least 3
  877. * (probably NO-CERTIFICATE-ERROR) */
  878. {
  879. ret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);
  880. goto end;
  881. }
  882. s->init_num += i;
  883. if ((s->init_num >= 3) && (p[0] == SSL2_MT_ERROR))
  884. {
  885. n2s(p,i);
  886. if (i != SSL2_PE_NO_CERTIFICATE)
  887. {
  888. /* not the error message we expected -- let ssl2_part_read handle it */
  889. s->init_num -= 3;
  890. ret = ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE, 3);
  891. goto end;
  892. }
  893. if (s->msg_callback)
  894. s->msg_callback(0, s->version, 0, p, 3, s, s->msg_callback_arg); /* ERROR */
  895. /* this is the one place where we can recover from an SSL 2.0 error */
  896. if (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  897. {
  898. ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
  899. SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  900. goto end;
  901. }
  902. ret=1;
  903. goto end;
  904. }
  905. if ((*(p++) != SSL2_MT_CLIENT_CERTIFICATE) || (s->init_num < 6))
  906. {
  907. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  908. SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_SHORT_READ);
  909. goto end;
  910. }
  911. if (s->init_num != 6)
  912. {
  913. SSLerr(SSL_F_REQUEST_CERTIFICATE, ERR_R_INTERNAL_ERROR);
  914. goto end;
  915. }
  916. /* ok we have a response */
  917. /* certificate type, there is only one right now. */
  918. ctype= *(p++);
  919. if (ctype != SSL2_AT_MD5_WITH_RSA_ENCRYPTION)
  920. {
  921. ssl2_return_error(s,SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE);
  922. SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_RESPONSE_ARGUMENT);
  923. goto end;
  924. }
  925. n2s(p,i); s->s2->tmp.clen=i;
  926. n2s(p,i); s->s2->tmp.rlen=i;
  927. s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_D;
  928. }
  929. /* SSL2_ST_SEND_REQUEST_CERTIFICATE_D */
  930. p=(unsigned char *)s->init_buf->data;
  931. len = 6 + (unsigned long)s->s2->tmp.clen + (unsigned long)s->s2->tmp.rlen;
  932. if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
  933. {
  934. SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_MESSAGE_TOO_LONG);
  935. goto end;
  936. }
  937. j = (int)len - s->init_num;
  938. i = ssl2_read(s,(char *)&(p[s->init_num]),j);
  939. if (i < j)
  940. {
  941. ret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);
  942. goto end;
  943. }
  944. if (s->msg_callback)
  945. s->msg_callback(0, s->version, 0, p, len, s, s->msg_callback_arg); /* CLIENT-CERTIFICATE */
  946. p += 6;
  947. cp = p;
  948. x509=(X509 *)d2i_X509(NULL,&cp,(long)s->s2->tmp.clen);
  949. if (x509 == NULL)
  950. {
  951. SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_X509_LIB);
  952. goto msg_end;
  953. }
  954. if (((sk=sk_X509_new_null()) == NULL) || (!sk_X509_push(sk,x509)))
  955. {
  956. SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  957. goto msg_end;
  958. }
  959. i=ssl_verify_cert_chain(s,sk);
  960. if (i > 0) /* we like the packet, now check the chksum */
  961. {
  962. EVP_MD_CTX ctx;
  963. EVP_PKEY *pkey=NULL;
  964. EVP_MD_CTX_init(&ctx);
  965. EVP_VerifyInit_ex(&ctx,s->ctx->rsa_md5, NULL);
  966. EVP_VerifyUpdate(&ctx,s->s2->key_material,
  967. s->s2->key_material_length);
  968. EVP_VerifyUpdate(&ctx,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
  969. i=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);
  970. buf2=OPENSSL_malloc((unsigned int)i);
  971. if (buf2 == NULL)
  972. {
  973. SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  974. goto msg_end;
  975. }
  976. p2=buf2;
  977. i=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&p2);
  978. EVP_VerifyUpdate(&ctx,buf2,(unsigned int)i);
  979. OPENSSL_free(buf2);
  980. pkey=X509_get_pubkey(x509);
  981. if (pkey == NULL) goto end;
  982. i=EVP_VerifyFinal(&ctx,cp,s->s2->tmp.rlen,pkey);
  983. EVP_PKEY_free(pkey);
  984. EVP_MD_CTX_cleanup(&ctx);
  985. if (i > 0)
  986. {
  987. if (s->session->peer != NULL)
  988. X509_free(s->session->peer);
  989. s->session->peer=x509;
  990. CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509);
  991. s->session->verify_result = s->verify_result;
  992. ret=1;
  993. goto end;
  994. }
  995. else
  996. {
  997. SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_CHECKSUM);
  998. goto msg_end;
  999. }
  1000. }
  1001. else
  1002. {
  1003. msg_end:
  1004. ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
  1005. }
  1006. end:
  1007. sk_X509_free(sk);
  1008. X509_free(x509);
  1009. return(ret);
  1010. }
  1011. static int ssl_rsa_private_decrypt(CERT *c, int len, unsigned char *from,
  1012. unsigned char *to, int padding)
  1013. {
  1014. RSA *rsa;
  1015. int i;
  1016. if ((c == NULL) || (c->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL))
  1017. {
  1018. SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,SSL_R_NO_PRIVATEKEY);
  1019. return(-1);
  1020. }
  1021. if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey->type != EVP_PKEY_RSA)
  1022. {
  1023. SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,SSL_R_PUBLIC_KEY_IS_NOT_RSA);
  1024. return(-1);
  1025. }
  1026. rsa=c->pkeys[SSL_PKEY_RSA_ENC].privatekey->pkey.rsa;
  1027. /* we have the public key */
  1028. i=RSA_private_decrypt(len,from,to,rsa,padding);
  1029. if (i < 0)
  1030. SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,ERR_R_RSA_LIB);
  1031. return(i);
  1032. }
  1033. #else /* !OPENSSL_NO_SSL2 */
  1034. # if PEDANTIC
  1035. static void *dummy=&dummy;
  1036. # endif
  1037. #endif