s2_srvr.c 40 KB

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