s2_clnt.c 29 KB

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