s2_srvr.c 39 KB

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