s2_clnt.c 30 KB

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