s2_clnt.c 30 KB

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