s2_clnt.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. /* ssl/s2_clnt.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/rand.h>
  115. # include <openssl/buffer.h>
  116. # include <openssl/objects.h>
  117. # include <openssl/evp.h>
  118. static const SSL_METHOD *ssl2_get_client_method(int ver);
  119. static int get_server_finished(SSL *s);
  120. static int get_server_verify(SSL *s);
  121. static int get_server_hello(SSL *s);
  122. static int client_hello(SSL *s);
  123. static int client_master_key(SSL *s);
  124. static int client_finished(SSL *s);
  125. static int client_certificate(SSL *s);
  126. static int ssl_rsa_public_encrypt(SESS_CERT *sc, int len, unsigned char *from,
  127. unsigned char *to, int padding);
  128. # define BREAK break
  129. static const SSL_METHOD *ssl2_get_client_method(int ver)
  130. {
  131. if (ver == SSL2_VERSION)
  132. return (SSLv2_client_method());
  133. else
  134. return (NULL);
  135. }
  136. IMPLEMENT_ssl2_meth_func(SSLv2_client_method,
  137. ssl_undefined_function,
  138. ssl2_connect, ssl2_get_client_method)
  139. int ssl2_connect(SSL *s)
  140. {
  141. unsigned long l = (unsigned long)time(NULL);
  142. BUF_MEM *buf = NULL;
  143. int ret = -1;
  144. void (*cb) (const SSL *ssl, int type, int val) = NULL;
  145. int new_state, state;
  146. RAND_add(&l, sizeof(l), 0);
  147. ERR_clear_error();
  148. clear_sys_error();
  149. if (s->info_callback != NULL)
  150. cb = s->info_callback;
  151. else if (s->ctx->info_callback != NULL)
  152. cb = s->ctx->info_callback;
  153. /* init things to blank */
  154. s->in_handshake++;
  155. if (!SSL_in_init(s) || SSL_in_before(s))
  156. SSL_clear(s);
  157. for (;;) {
  158. state = s->state;
  159. switch (s->state) {
  160. case SSL_ST_BEFORE:
  161. case SSL_ST_CONNECT:
  162. case SSL_ST_BEFORE | SSL_ST_CONNECT:
  163. case SSL_ST_OK | SSL_ST_CONNECT:
  164. s->server = 0;
  165. if (cb != NULL)
  166. cb(s, SSL_CB_HANDSHAKE_START, 1);
  167. s->version = SSL2_VERSION;
  168. s->type = SSL_ST_CONNECT;
  169. buf = s->init_buf;
  170. if ((buf == NULL) && ((buf = BUF_MEM_new()) == NULL)) {
  171. ret = -1;
  172. goto end;
  173. }
  174. if (!BUF_MEM_grow(buf, SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)) {
  175. if (buf == s->init_buf)
  176. buf = NULL;
  177. ret = -1;
  178. goto end;
  179. }
  180. s->init_buf = buf;
  181. buf = NULL;
  182. s->init_num = 0;
  183. s->state = SSL2_ST_SEND_CLIENT_HELLO_A;
  184. s->ctx->stats.sess_connect++;
  185. s->handshake_func = ssl2_connect;
  186. BREAK;
  187. case SSL2_ST_SEND_CLIENT_HELLO_A:
  188. case SSL2_ST_SEND_CLIENT_HELLO_B:
  189. s->shutdown = 0;
  190. ret = client_hello(s);
  191. if (ret <= 0)
  192. goto end;
  193. s->init_num = 0;
  194. s->state = SSL2_ST_GET_SERVER_HELLO_A;
  195. BREAK;
  196. case SSL2_ST_GET_SERVER_HELLO_A:
  197. case SSL2_ST_GET_SERVER_HELLO_B:
  198. ret = get_server_hello(s);
  199. if (ret <= 0)
  200. goto end;
  201. s->init_num = 0;
  202. if (!s->hit) { /* new session */
  203. s->state = SSL2_ST_SEND_CLIENT_MASTER_KEY_A;
  204. BREAK;
  205. } else {
  206. s->state = SSL2_ST_CLIENT_START_ENCRYPTION;
  207. break;
  208. }
  209. case SSL2_ST_SEND_CLIENT_MASTER_KEY_A:
  210. case SSL2_ST_SEND_CLIENT_MASTER_KEY_B:
  211. ret = client_master_key(s);
  212. if (ret <= 0)
  213. goto end;
  214. s->init_num = 0;
  215. s->state = SSL2_ST_CLIENT_START_ENCRYPTION;
  216. break;
  217. case SSL2_ST_CLIENT_START_ENCRYPTION:
  218. /*
  219. * Ok, we now have all the stuff needed to start encrypting, so
  220. * lets fire it up :-)
  221. */
  222. if (!ssl2_enc_init(s, 1)) {
  223. ret = -1;
  224. goto end;
  225. }
  226. s->s2->clear_text = 0;
  227. s->state = SSL2_ST_SEND_CLIENT_FINISHED_A;
  228. break;
  229. case SSL2_ST_SEND_CLIENT_FINISHED_A:
  230. case SSL2_ST_SEND_CLIENT_FINISHED_B:
  231. ret = client_finished(s);
  232. if (ret <= 0)
  233. goto end;
  234. s->init_num = 0;
  235. s->state = SSL2_ST_GET_SERVER_VERIFY_A;
  236. break;
  237. case SSL2_ST_GET_SERVER_VERIFY_A:
  238. case SSL2_ST_GET_SERVER_VERIFY_B:
  239. ret = get_server_verify(s);
  240. if (ret <= 0)
  241. goto end;
  242. s->init_num = 0;
  243. s->state = SSL2_ST_GET_SERVER_FINISHED_A;
  244. break;
  245. case SSL2_ST_GET_SERVER_FINISHED_A:
  246. case SSL2_ST_GET_SERVER_FINISHED_B:
  247. ret = get_server_finished(s);
  248. if (ret <= 0)
  249. goto end;
  250. break;
  251. case SSL2_ST_SEND_CLIENT_CERTIFICATE_A:
  252. case SSL2_ST_SEND_CLIENT_CERTIFICATE_B:
  253. case SSL2_ST_SEND_CLIENT_CERTIFICATE_C:
  254. case SSL2_ST_SEND_CLIENT_CERTIFICATE_D:
  255. case SSL2_ST_X509_GET_CLIENT_CERTIFICATE:
  256. ret = client_certificate(s);
  257. if (ret <= 0)
  258. goto end;
  259. s->init_num = 0;
  260. s->state = SSL2_ST_GET_SERVER_FINISHED_A;
  261. break;
  262. case SSL_ST_OK:
  263. if (s->init_buf != NULL) {
  264. BUF_MEM_free(s->init_buf);
  265. s->init_buf = NULL;
  266. }
  267. s->init_num = 0;
  268. /* ERR_clear_error(); */
  269. /*
  270. * If we want to cache session-ids in the client and we
  271. * successfully add the session-id to the cache, and there is a
  272. * callback, then pass it out. 26/11/96 - eay - only add if not a
  273. * re-used session.
  274. */
  275. ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
  276. if (s->hit)
  277. s->ctx->stats.sess_hit++;
  278. ret = 1;
  279. /* s->server=0; */
  280. s->ctx->stats.sess_connect_good++;
  281. if (cb != NULL)
  282. cb(s, SSL_CB_HANDSHAKE_DONE, 1);
  283. goto end;
  284. /* break; */
  285. default:
  286. SSLerr(SSL_F_SSL2_CONNECT, SSL_R_UNKNOWN_STATE);
  287. return (-1);
  288. /* break; */
  289. }
  290. if ((cb != NULL) && (s->state != state)) {
  291. new_state = s->state;
  292. s->state = state;
  293. cb(s, SSL_CB_CONNECT_LOOP, 1);
  294. s->state = new_state;
  295. }
  296. }
  297. end:
  298. s->in_handshake--;
  299. if (buf != NULL)
  300. BUF_MEM_free(buf);
  301. if (cb != NULL)
  302. cb(s, SSL_CB_CONNECT_EXIT, ret);
  303. return (ret);
  304. }
  305. static int get_server_hello(SSL *s)
  306. {
  307. unsigned char *buf;
  308. unsigned char *p;
  309. int i, j;
  310. unsigned long len;
  311. STACK_OF(SSL_CIPHER) *sk = NULL, *cl, *prio, *allow;
  312. buf = (unsigned char *)s->init_buf->data;
  313. p = buf;
  314. if (s->state == SSL2_ST_GET_SERVER_HELLO_A) {
  315. i = ssl2_read(s, (char *)&(buf[s->init_num]), 11 - s->init_num);
  316. if (i < (11 - s->init_num))
  317. return (ssl2_part_read(s, SSL_F_GET_SERVER_HELLO, i));
  318. s->init_num = 11;
  319. if (*(p++) != SSL2_MT_SERVER_HELLO) {
  320. if (p[-1] != SSL2_MT_ERROR) {
  321. ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  322. SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_READ_WRONG_PACKET_TYPE);
  323. } else
  324. SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_PEER_ERROR);
  325. return (-1);
  326. }
  327. # if 0
  328. s->hit = (*(p++)) ? 1 : 0;
  329. /*
  330. * Some [PPC?] compilers fail to increment p in above statement, e.g.
  331. * one provided with Rhapsody 5.5, but most recent example XL C 11.1
  332. * for AIX, even without optimization flag...
  333. */
  334. # else
  335. s->hit = (*p) ? 1 : 0;
  336. p++;
  337. # endif
  338. s->s2->tmp.cert_type = *(p++);
  339. n2s(p, i);
  340. if (i < s->version)
  341. s->version = i;
  342. n2s(p, i);
  343. s->s2->tmp.cert_length = i;
  344. n2s(p, i);
  345. s->s2->tmp.csl = i;
  346. n2s(p, i);
  347. s->s2->tmp.conn_id_length = i;
  348. s->state = SSL2_ST_GET_SERVER_HELLO_B;
  349. }
  350. /* SSL2_ST_GET_SERVER_HELLO_B */
  351. len =
  352. 11 + (unsigned long)s->s2->tmp.cert_length +
  353. (unsigned long)s->s2->tmp.csl +
  354. (unsigned long)s->s2->tmp.conn_id_length;
  355. if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) {
  356. SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_MESSAGE_TOO_LONG);
  357. return -1;
  358. }
  359. j = (int)len - s->init_num;
  360. i = ssl2_read(s, (char *)&(buf[s->init_num]), j);
  361. if (i != j)
  362. return (ssl2_part_read(s, SSL_F_GET_SERVER_HELLO, i));
  363. if (s->msg_callback) {
  364. /* SERVER-HELLO */
  365. s->msg_callback(0, s->version, 0, buf, (size_t)len, s,
  366. s->msg_callback_arg);
  367. }
  368. /* things are looking good */
  369. p = buf + 11;
  370. if (s->hit) {
  371. if (s->s2->tmp.cert_length != 0) {
  372. SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_REUSE_CERT_LENGTH_NOT_ZERO);
  373. return (-1);
  374. }
  375. if (s->s2->tmp.cert_type != 0) {
  376. if (!(s->options & SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG)) {
  377. SSLerr(SSL_F_GET_SERVER_HELLO,
  378. SSL_R_REUSE_CERT_TYPE_NOT_ZERO);
  379. return (-1);
  380. }
  381. }
  382. if (s->s2->tmp.csl != 0) {
  383. SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_REUSE_CIPHER_LIST_NOT_ZERO);
  384. return (-1);
  385. }
  386. } else {
  387. # if 0
  388. /* very bad */
  389. memset(s->session->session_id, 0,
  390. SSL_MAX_SSL_SESSION_ID_LENGTH_IN_BYTES);
  391. s->session->session_id_length = 0;
  392. # endif
  393. /*
  394. * we need to do this in case we were trying to reuse a client
  395. * session but others are already reusing it. If this was a new
  396. * 'blank' session ID, the session-id length will still be 0
  397. */
  398. if (s->session->session_id_length > 0) {
  399. if (!ssl_get_new_session(s, 0)) {
  400. ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  401. return (-1);
  402. }
  403. }
  404. if (ssl2_set_certificate(s, s->s2->tmp.cert_type,
  405. s->s2->tmp.cert_length, p) <= 0) {
  406. ssl2_return_error(s, SSL2_PE_BAD_CERTIFICATE);
  407. return (-1);
  408. }
  409. p += s->s2->tmp.cert_length;
  410. if (s->s2->tmp.csl == 0) {
  411. ssl2_return_error(s, SSL2_PE_NO_CIPHER);
  412. SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_NO_CIPHER_LIST);
  413. return (-1);
  414. }
  415. /*
  416. * We have just received a list of ciphers back from the server. We
  417. * need to get the ones that match, then select the one we want the
  418. * most :-).
  419. */
  420. /* load the ciphers */
  421. sk = ssl_bytes_to_cipher_list(s, p, s->s2->tmp.csl,
  422. &s->session->ciphers);
  423. p += s->s2->tmp.csl;
  424. if (sk == NULL) {
  425. ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  426. SSLerr(SSL_F_GET_SERVER_HELLO, ERR_R_MALLOC_FAILURE);
  427. return (-1);
  428. }
  429. (void)sk_SSL_CIPHER_set_cmp_func(sk, ssl_cipher_ptr_id_cmp);
  430. /* get the array of ciphers we will accept */
  431. cl = SSL_get_ciphers(s);
  432. (void)sk_SSL_CIPHER_set_cmp_func(cl, ssl_cipher_ptr_id_cmp);
  433. /*
  434. * If server preference flag set, choose the first
  435. * (highest priority) cipher the server sends, otherwise
  436. * client preference has priority.
  437. */
  438. if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
  439. prio = sk;
  440. allow = cl;
  441. } else {
  442. prio = cl;
  443. allow = sk;
  444. }
  445. /*
  446. * In theory we could have ciphers sent back that we don't want to
  447. * use but that does not matter since we will check against the list
  448. * we originally sent and for performance reasons we should not
  449. * bother to match the two lists up just to check.
  450. */
  451. for (i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
  452. if (sk_SSL_CIPHER_find(allow, sk_SSL_CIPHER_value(prio, i)) >= 0)
  453. break;
  454. }
  455. if (i >= sk_SSL_CIPHER_num(prio)) {
  456. ssl2_return_error(s, SSL2_PE_NO_CIPHER);
  457. SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_NO_CIPHER_MATCH);
  458. return (-1);
  459. }
  460. s->session->cipher = sk_SSL_CIPHER_value(prio, i);
  461. if (s->session->peer != NULL) { /* can't happen */
  462. ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  463. SSLerr(SSL_F_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  464. return (-1);
  465. }
  466. s->session->peer = s->session->sess_cert->peer_key->x509;
  467. /* peer_key->x509 has been set by ssl2_set_certificate. */
  468. CRYPTO_add(&s->session->peer->references, 1, CRYPTO_LOCK_X509);
  469. }
  470. if (s->session->sess_cert == NULL
  471. || s->session->peer != s->session->sess_cert->peer_key->x509)
  472. /* can't happen */
  473. {
  474. ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  475. SSLerr(SSL_F_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  476. return (-1);
  477. }
  478. s->s2->conn_id_length = s->s2->tmp.conn_id_length;
  479. if (s->s2->conn_id_length > sizeof s->s2->conn_id) {
  480. ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  481. SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_SSL2_CONNECTION_ID_TOO_LONG);
  482. return -1;
  483. }
  484. memcpy(s->s2->conn_id, p, s->s2->tmp.conn_id_length);
  485. return (1);
  486. }
  487. static int client_hello(SSL *s)
  488. {
  489. unsigned char *buf;
  490. unsigned char *p, *d;
  491. /* CIPHER **cipher;*/
  492. int i, n, j;
  493. buf = (unsigned char *)s->init_buf->data;
  494. if (s->state == SSL2_ST_SEND_CLIENT_HELLO_A) {
  495. if ((s->session == NULL) || (s->session->ssl_version != s->version)) {
  496. if (!ssl_get_new_session(s, 0)) {
  497. ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  498. return (-1);
  499. }
  500. }
  501. /* else use the pre-loaded session */
  502. p = buf; /* header */
  503. d = p + 9; /* data section */
  504. *(p++) = SSL2_MT_CLIENT_HELLO; /* type */
  505. s2n(SSL2_VERSION, p); /* version */
  506. n = j = 0;
  507. n = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), d, 0);
  508. d += n;
  509. if (n == 0) {
  510. SSLerr(SSL_F_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
  511. return (-1);
  512. }
  513. s2n(n, p); /* cipher spec num bytes */
  514. if ((s->session->session_id_length > 0) &&
  515. (s->session->session_id_length <=
  516. SSL2_MAX_SSL_SESSION_ID_LENGTH)) {
  517. i = s->session->session_id_length;
  518. s2n(i, p); /* session id length */
  519. memcpy(d, s->session->session_id, (unsigned int)i);
  520. d += i;
  521. } else {
  522. s2n(0, p);
  523. }
  524. s->s2->challenge_length = SSL2_CHALLENGE_LENGTH;
  525. s2n(SSL2_CHALLENGE_LENGTH, p); /* challenge length */
  526. /*
  527. * challenge id data
  528. */
  529. if (RAND_bytes(s->s2->challenge, SSL2_CHALLENGE_LENGTH) <= 0)
  530. return -1;
  531. memcpy(d, s->s2->challenge, SSL2_CHALLENGE_LENGTH);
  532. d += SSL2_CHALLENGE_LENGTH;
  533. s->state = SSL2_ST_SEND_CLIENT_HELLO_B;
  534. s->init_num = d - buf;
  535. s->init_off = 0;
  536. }
  537. /* SSL2_ST_SEND_CLIENT_HELLO_B */
  538. return (ssl2_do_write(s));
  539. }
  540. static int client_master_key(SSL *s)
  541. {
  542. unsigned char *buf;
  543. unsigned char *p, *d;
  544. int clear, enc, karg, i;
  545. SSL_SESSION *sess;
  546. const EVP_CIPHER *c;
  547. const EVP_MD *md;
  548. buf = (unsigned char *)s->init_buf->data;
  549. if (s->state == SSL2_ST_SEND_CLIENT_MASTER_KEY_A) {
  550. if (!ssl_cipher_get_evp(s->session, &c, &md, NULL, NULL, NULL)) {
  551. ssl2_return_error(s, SSL2_PE_NO_CIPHER);
  552. SSLerr(SSL_F_CLIENT_MASTER_KEY,
  553. SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
  554. return (-1);
  555. }
  556. sess = s->session;
  557. p = buf;
  558. d = p + 10;
  559. *(p++) = SSL2_MT_CLIENT_MASTER_KEY; /* type */
  560. i = ssl_put_cipher_by_char(s, sess->cipher, p);
  561. p += i;
  562. /* make key_arg data */
  563. i = EVP_CIPHER_iv_length(c);
  564. sess->key_arg_length = i;
  565. if (i > SSL_MAX_KEY_ARG_LENGTH) {
  566. ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  567. SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
  568. return -1;
  569. }
  570. if (i > 0)
  571. if (RAND_bytes(sess->key_arg, i) <= 0)
  572. return -1;
  573. /* make a master key */
  574. i = EVP_CIPHER_key_length(c);
  575. sess->master_key_length = i;
  576. if (i > 0) {
  577. if (i > (int)sizeof(sess->master_key)) {
  578. ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  579. SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
  580. return -1;
  581. }
  582. if (RAND_bytes(sess->master_key, i) <= 0) {
  583. ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  584. return (-1);
  585. }
  586. }
  587. if (sess->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC)
  588. enc = 8;
  589. else if (SSL_C_IS_EXPORT(sess->cipher))
  590. enc = 5;
  591. else
  592. enc = i;
  593. if ((int)i < enc) {
  594. ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  595. SSLerr(SSL_F_CLIENT_MASTER_KEY, SSL_R_CIPHER_TABLE_SRC_ERROR);
  596. return (-1);
  597. }
  598. clear = i - enc;
  599. s2n(clear, p);
  600. memcpy(d, sess->master_key, (unsigned int)clear);
  601. d += clear;
  602. enc = ssl_rsa_public_encrypt(sess->sess_cert, enc,
  603. &(sess->master_key[clear]), d,
  604. (s->
  605. s2->ssl2_rollback) ? RSA_SSLV23_PADDING
  606. : RSA_PKCS1_PADDING);
  607. if (enc <= 0) {
  608. ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  609. SSLerr(SSL_F_CLIENT_MASTER_KEY, SSL_R_PUBLIC_KEY_ENCRYPT_ERROR);
  610. return (-1);
  611. }
  612. # ifdef PKCS1_CHECK
  613. if (s->options & SSL_OP_PKCS1_CHECK_1)
  614. d[1]++;
  615. if (s->options & SSL_OP_PKCS1_CHECK_2)
  616. sess->master_key[clear]++;
  617. # endif
  618. s2n(enc, p);
  619. d += enc;
  620. karg = sess->key_arg_length;
  621. s2n(karg, p); /* key arg size */
  622. if (karg > (int)sizeof(sess->key_arg)) {
  623. ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  624. SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
  625. return -1;
  626. }
  627. memcpy(d, sess->key_arg, (unsigned int)karg);
  628. d += karg;
  629. s->state = SSL2_ST_SEND_CLIENT_MASTER_KEY_B;
  630. s->init_num = d - buf;
  631. s->init_off = 0;
  632. }
  633. /* SSL2_ST_SEND_CLIENT_MASTER_KEY_B */
  634. return (ssl2_do_write(s));
  635. }
  636. static int client_finished(SSL *s)
  637. {
  638. unsigned char *p;
  639. if (s->state == SSL2_ST_SEND_CLIENT_FINISHED_A) {
  640. p = (unsigned char *)s->init_buf->data;
  641. *(p++) = SSL2_MT_CLIENT_FINISHED;
  642. if (s->s2->conn_id_length > sizeof s->s2->conn_id) {
  643. SSLerr(SSL_F_CLIENT_FINISHED, ERR_R_INTERNAL_ERROR);
  644. return -1;
  645. }
  646. memcpy(p, s->s2->conn_id, (unsigned int)s->s2->conn_id_length);
  647. s->state = SSL2_ST_SEND_CLIENT_FINISHED_B;
  648. s->init_num = s->s2->conn_id_length + 1;
  649. s->init_off = 0;
  650. }
  651. return (ssl2_do_write(s));
  652. }
  653. /* read the data and then respond */
  654. static int client_certificate(SSL *s)
  655. {
  656. unsigned char *buf;
  657. unsigned char *p, *d;
  658. int i;
  659. unsigned int n;
  660. int cert_ch_len;
  661. unsigned char *cert_ch;
  662. buf = (unsigned char *)s->init_buf->data;
  663. /*
  664. * We have a cert associated with the SSL, so attach it to the session if
  665. * it does not have one
  666. */
  667. if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_A) {
  668. i = ssl2_read(s, (char *)&(buf[s->init_num]),
  669. SSL2_MAX_CERT_CHALLENGE_LENGTH + 2 - s->init_num);
  670. if (i < (SSL2_MIN_CERT_CHALLENGE_LENGTH + 2 - s->init_num))
  671. return (ssl2_part_read(s, SSL_F_CLIENT_CERTIFICATE, i));
  672. s->init_num += i;
  673. if (s->msg_callback) {
  674. /* REQUEST-CERTIFICATE */
  675. s->msg_callback(0, s->version, 0, buf, (size_t)s->init_num, s,
  676. s->msg_callback_arg);
  677. }
  678. /* type=buf[0]; */
  679. /* type eq x509 */
  680. if (buf[1] != SSL2_AT_MD5_WITH_RSA_ENCRYPTION) {
  681. ssl2_return_error(s, SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE);
  682. SSLerr(SSL_F_CLIENT_CERTIFICATE, SSL_R_BAD_AUTHENTICATION_TYPE);
  683. return (-1);
  684. }
  685. if ((s->cert == NULL) ||
  686. (s->cert->key->x509 == NULL) ||
  687. (s->cert->key->privatekey == NULL)) {
  688. s->state = SSL2_ST_X509_GET_CLIENT_CERTIFICATE;
  689. } else
  690. s->state = SSL2_ST_SEND_CLIENT_CERTIFICATE_C;
  691. }
  692. cert_ch = buf + 2;
  693. cert_ch_len = s->init_num - 2;
  694. if (s->state == SSL2_ST_X509_GET_CLIENT_CERTIFICATE) {
  695. X509 *x509 = NULL;
  696. EVP_PKEY *pkey = NULL;
  697. /*
  698. * If we get an error we need to ssl->rwstate=SSL_X509_LOOKUP;
  699. * return(error); We should then be retried when things are ok and we
  700. * can get a cert or not
  701. */
  702. i = 0;
  703. if (s->ctx->client_cert_cb != NULL) {
  704. i = s->ctx->client_cert_cb(s, &(x509), &(pkey));
  705. }
  706. if (i < 0) {
  707. s->rwstate = SSL_X509_LOOKUP;
  708. return (-1);
  709. }
  710. s->rwstate = SSL_NOTHING;
  711. if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
  712. s->state = SSL2_ST_SEND_CLIENT_CERTIFICATE_C;
  713. if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
  714. i = 0;
  715. }
  716. X509_free(x509);
  717. EVP_PKEY_free(pkey);
  718. } else if (i == 1) {
  719. if (x509 != NULL)
  720. X509_free(x509);
  721. if (pkey != NULL)
  722. EVP_PKEY_free(pkey);
  723. SSLerr(SSL_F_CLIENT_CERTIFICATE,
  724. SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  725. i = 0;
  726. }
  727. if (i == 0) {
  728. /*
  729. * We have no client certificate to respond with so send the
  730. * correct error message back
  731. */
  732. s->state = SSL2_ST_SEND_CLIENT_CERTIFICATE_B;
  733. p = buf;
  734. *(p++) = SSL2_MT_ERROR;
  735. s2n(SSL2_PE_NO_CERTIFICATE, p);
  736. s->init_off = 0;
  737. s->init_num = 3;
  738. /* Write is done at the end */
  739. }
  740. }
  741. if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_B) {
  742. return (ssl2_do_write(s));
  743. }
  744. if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_C) {
  745. EVP_MD_CTX ctx;
  746. /*
  747. * ok, now we calculate the checksum do it first so we can reuse buf
  748. * :-)
  749. */
  750. p = buf;
  751. EVP_MD_CTX_init(&ctx);
  752. EVP_SignInit_ex(&ctx, s->ctx->rsa_md5, NULL);
  753. EVP_SignUpdate(&ctx, s->s2->key_material, s->s2->key_material_length);
  754. EVP_SignUpdate(&ctx, cert_ch, (unsigned int)cert_ch_len);
  755. i = i2d_X509(s->session->sess_cert->peer_key->x509, &p);
  756. /*
  757. * Don't update the signature if it fails - FIXME: probably should
  758. * handle this better
  759. */
  760. if (i > 0)
  761. EVP_SignUpdate(&ctx, buf, (unsigned int)i);
  762. p = buf;
  763. d = p + 6;
  764. *(p++) = SSL2_MT_CLIENT_CERTIFICATE;
  765. *(p++) = SSL2_CT_X509_CERTIFICATE;
  766. n = i2d_X509(s->cert->key->x509, &d);
  767. s2n(n, p);
  768. if (!EVP_SignFinal(&ctx, d, &n, s->cert->key->privatekey)) {
  769. /*
  770. * this is not good. If things have failed it means there so
  771. * something wrong with the key. We will continue with a 0 length
  772. * signature
  773. */
  774. }
  775. EVP_MD_CTX_cleanup(&ctx);
  776. s2n(n, p);
  777. d += n;
  778. s->state = SSL2_ST_SEND_CLIENT_CERTIFICATE_D;
  779. s->init_num = d - buf;
  780. s->init_off = 0;
  781. }
  782. /* if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_D) */
  783. return (ssl2_do_write(s));
  784. }
  785. static int get_server_verify(SSL *s)
  786. {
  787. unsigned char *p;
  788. int i, n, len;
  789. p = (unsigned char *)s->init_buf->data;
  790. if (s->state == SSL2_ST_GET_SERVER_VERIFY_A) {
  791. i = ssl2_read(s, (char *)&(p[s->init_num]), 1 - s->init_num);
  792. if (i < (1 - s->init_num))
  793. return (ssl2_part_read(s, SSL_F_GET_SERVER_VERIFY, i));
  794. s->init_num += i;
  795. s->state = SSL2_ST_GET_SERVER_VERIFY_B;
  796. if (*p != SSL2_MT_SERVER_VERIFY) {
  797. if (p[0] != SSL2_MT_ERROR) {
  798. ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  799. SSLerr(SSL_F_GET_SERVER_VERIFY, SSL_R_READ_WRONG_PACKET_TYPE);
  800. } else {
  801. SSLerr(SSL_F_GET_SERVER_VERIFY, SSL_R_PEER_ERROR);
  802. /* try to read the error message */
  803. i = ssl2_read(s, (char *)&(p[s->init_num]), 3 - s->init_num);
  804. return ssl2_part_read(s, SSL_F_GET_SERVER_VERIFY, i);
  805. }
  806. return (-1);
  807. }
  808. }
  809. p = (unsigned char *)s->init_buf->data;
  810. len = 1 + s->s2->challenge_length;
  811. n = len - s->init_num;
  812. i = ssl2_read(s, (char *)&(p[s->init_num]), n);
  813. if (i < n)
  814. return (ssl2_part_read(s, SSL_F_GET_SERVER_VERIFY, i));
  815. if (s->msg_callback) {
  816. /* SERVER-VERIFY */
  817. s->msg_callback(0, s->version, 0, p, len, s, s->msg_callback_arg);
  818. }
  819. p += 1;
  820. if (CRYPTO_memcmp(p, s->s2->challenge, s->s2->challenge_length) != 0) {
  821. ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  822. SSLerr(SSL_F_GET_SERVER_VERIFY, SSL_R_CHALLENGE_IS_DIFFERENT);
  823. return (-1);
  824. }
  825. return (1);
  826. }
  827. static int get_server_finished(SSL *s)
  828. {
  829. unsigned char *buf;
  830. unsigned char *p;
  831. int i, n, len;
  832. buf = (unsigned char *)s->init_buf->data;
  833. p = buf;
  834. if (s->state == SSL2_ST_GET_SERVER_FINISHED_A) {
  835. i = ssl2_read(s, (char *)&(buf[s->init_num]), 1 - s->init_num);
  836. if (i < (1 - s->init_num))
  837. return (ssl2_part_read(s, SSL_F_GET_SERVER_FINISHED, i));
  838. s->init_num += i;
  839. if (*p == SSL2_MT_REQUEST_CERTIFICATE) {
  840. s->state = SSL2_ST_SEND_CLIENT_CERTIFICATE_A;
  841. return (1);
  842. } else if (*p != SSL2_MT_SERVER_FINISHED) {
  843. if (p[0] != SSL2_MT_ERROR) {
  844. ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  845. SSLerr(SSL_F_GET_SERVER_FINISHED,
  846. SSL_R_READ_WRONG_PACKET_TYPE);
  847. } else {
  848. SSLerr(SSL_F_GET_SERVER_FINISHED, SSL_R_PEER_ERROR);
  849. /* try to read the error message */
  850. i = ssl2_read(s, (char *)&(p[s->init_num]), 3 - s->init_num);
  851. return ssl2_part_read(s, SSL_F_GET_SERVER_VERIFY, i);
  852. }
  853. return (-1);
  854. }
  855. s->state = SSL2_ST_GET_SERVER_FINISHED_B;
  856. }
  857. len = 1 + SSL2_SSL_SESSION_ID_LENGTH;
  858. n = len - s->init_num;
  859. i = ssl2_read(s, (char *)&(buf[s->init_num]), n);
  860. if (i < n) {
  861. /*
  862. * XXX could be shorter than SSL2_SSL_SESSION_ID_LENGTH,
  863. * that's the maximum
  864. */
  865. return (ssl2_part_read(s, SSL_F_GET_SERVER_FINISHED, i));
  866. }
  867. s->init_num += i;
  868. if (s->msg_callback) {
  869. /* SERVER-FINISHED */
  870. s->msg_callback(0, s->version, 0, buf, (size_t)s->init_num, s,
  871. s->msg_callback_arg);
  872. }
  873. if (!s->hit) { /* new session */
  874. /* new session-id */
  875. /*
  876. * Make sure we were not trying to re-use an old SSL_SESSION or bad
  877. * things can happen
  878. */
  879. /* ZZZZZZZZZZZZZ */
  880. s->session->session_id_length = SSL2_SSL_SESSION_ID_LENGTH;
  881. memcpy(s->session->session_id, p + 1, SSL2_SSL_SESSION_ID_LENGTH);
  882. } else {
  883. if (!(s->options & SSL_OP_MICROSOFT_SESS_ID_BUG)) {
  884. if ((s->session->session_id_length >
  885. sizeof s->session->session_id)
  886. || (0 !=
  887. memcmp(buf + 1, s->session->session_id,
  888. (unsigned int)s->session->session_id_length))) {
  889. ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  890. SSLerr(SSL_F_GET_SERVER_FINISHED,
  891. SSL_R_SSL_SESSION_ID_IS_DIFFERENT);
  892. return (-1);
  893. }
  894. }
  895. }
  896. s->state = SSL_ST_OK;
  897. return (1);
  898. }
  899. /* loads in the certificate from the server */
  900. int ssl2_set_certificate(SSL *s, int type, int len, const unsigned char *data)
  901. {
  902. STACK_OF(X509) *sk = NULL;
  903. EVP_PKEY *pkey = NULL;
  904. SESS_CERT *sc = NULL;
  905. int i;
  906. X509 *x509 = NULL;
  907. int ret = 0;
  908. x509 = d2i_X509(NULL, &data, (long)len);
  909. if (x509 == NULL) {
  910. SSLerr(SSL_F_SSL2_SET_CERTIFICATE, ERR_R_X509_LIB);
  911. goto err;
  912. }
  913. if ((sk = sk_X509_new_null()) == NULL || !sk_X509_push(sk, x509)) {
  914. SSLerr(SSL_F_SSL2_SET_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  915. goto err;
  916. }
  917. i = ssl_verify_cert_chain(s, sk);
  918. if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)) {
  919. SSLerr(SSL_F_SSL2_SET_CERTIFICATE, SSL_R_CERTIFICATE_VERIFY_FAILED);
  920. goto err;
  921. }
  922. ERR_clear_error(); /* but we keep s->verify_result */
  923. s->session->verify_result = s->verify_result;
  924. /* server's cert for this session */
  925. sc = ssl_sess_cert_new();
  926. if (sc == NULL) {
  927. ret = -1;
  928. goto err;
  929. }
  930. if (s->session->sess_cert)
  931. ssl_sess_cert_free(s->session->sess_cert);
  932. s->session->sess_cert = sc;
  933. sc->peer_pkeys[SSL_PKEY_RSA_ENC].x509 = x509;
  934. sc->peer_key = &(sc->peer_pkeys[SSL_PKEY_RSA_ENC]);
  935. pkey = X509_get_pubkey(x509);
  936. x509 = NULL;
  937. if (pkey == NULL) {
  938. SSLerr(SSL_F_SSL2_SET_CERTIFICATE,
  939. SSL_R_UNABLE_TO_EXTRACT_PUBLIC_KEY);
  940. goto err;
  941. }
  942. if (pkey->type != EVP_PKEY_RSA) {
  943. SSLerr(SSL_F_SSL2_SET_CERTIFICATE, SSL_R_PUBLIC_KEY_NOT_RSA);
  944. goto err;
  945. }
  946. if (!ssl_set_peer_cert_type(sc, SSL2_CT_X509_CERTIFICATE))
  947. goto err;
  948. ret = 1;
  949. err:
  950. sk_X509_free(sk);
  951. X509_free(x509);
  952. EVP_PKEY_free(pkey);
  953. return (ret);
  954. }
  955. static int ssl_rsa_public_encrypt(SESS_CERT *sc, int len, unsigned char *from,
  956. unsigned char *to, int padding)
  957. {
  958. EVP_PKEY *pkey = NULL;
  959. int i = -1;
  960. if ((sc == NULL) || (sc->peer_key->x509 == NULL) ||
  961. ((pkey = X509_get_pubkey(sc->peer_key->x509)) == NULL)) {
  962. SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT, SSL_R_NO_PUBLICKEY);
  963. return (-1);
  964. }
  965. if (pkey->type != EVP_PKEY_RSA) {
  966. SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT, SSL_R_PUBLIC_KEY_IS_NOT_RSA);
  967. goto end;
  968. }
  969. /* we have the public key */
  970. i = RSA_public_encrypt(len, from, to, pkey->pkey.rsa, padding);
  971. if (i < 0)
  972. SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT, ERR_R_RSA_LIB);
  973. end:
  974. EVP_PKEY_free(pkey);
  975. return (i);
  976. }
  977. #else /* !OPENSSL_NO_SSL2 */
  978. # if PEDANTIC
  979. static void *dummy = &dummy;
  980. # endif
  981. #endif