s2_srvr.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /* ssl/s2_srvr.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. #include <stdio.h>
  59. #include "bio.h"
  60. #include "rand.h"
  61. #include "objects.h"
  62. #include "ssl_locl.h"
  63. #include "evp.h"
  64. #ifndef NOPROTO
  65. static int get_client_master_key(SSL *s);
  66. static int get_client_hello(SSL *s);
  67. static int server_hello(SSL *s);
  68. static int get_client_finished(SSL *s);
  69. static int server_verify(SSL *s);
  70. static int server_finish(SSL *s);
  71. static int request_certificate(SSL *s);
  72. static int ssl_rsa_private_decrypt(CERT *c, int len, unsigned char *from,
  73. unsigned char *to,int padding);
  74. #else
  75. static int get_client_master_key();
  76. static int get_client_hello();
  77. static int server_hello();
  78. static int get_client_finished();
  79. static int server_verify();
  80. static int server_finish();
  81. static int request_certificate();
  82. static int ssl_rsa_private_decrypt();
  83. #endif
  84. #define BREAK break
  85. static SSL_METHOD *ssl2_get_server_method(ver)
  86. int ver;
  87. {
  88. if (ver == SSL2_VERSION)
  89. return(SSLv2_server_method());
  90. else
  91. return(NULL);
  92. }
  93. SSL_METHOD *SSLv2_server_method()
  94. {
  95. static int init=1;
  96. static SSL_METHOD SSLv2_server_data;
  97. if (init)
  98. {
  99. init=0;
  100. memcpy((char *)&SSLv2_server_data,(char *)sslv2_base_method(),
  101. sizeof(SSL_METHOD));
  102. SSLv2_server_data.ssl_accept=ssl2_accept;
  103. SSLv2_server_data.get_ssl_method=ssl2_get_server_method;
  104. }
  105. return(&SSLv2_server_data);
  106. }
  107. int ssl2_accept(s)
  108. SSL *s;
  109. {
  110. unsigned long l=time(NULL);
  111. BUF_MEM *buf=NULL;
  112. int ret= -1;
  113. long num1;
  114. void (*cb)()=NULL;
  115. int new_state,state;
  116. RAND_seed((unsigned char *)&l,sizeof(l));
  117. ERR_clear_error();
  118. clear_sys_error();
  119. if (s->info_callback != NULL)
  120. cb=s->info_callback;
  121. else if (s->ctx->info_callback != NULL)
  122. cb=s->ctx->info_callback;
  123. /* init things to blank */
  124. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  125. s->in_handshake++;
  126. if (((s->session == NULL) || (s->session->cert == NULL)) &&
  127. (s->cert == NULL))
  128. {
  129. SSLerr(SSL_F_SSL2_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
  130. return(-1);
  131. }
  132. clear_sys_error();
  133. for (;;)
  134. {
  135. state=s->state;
  136. switch (s->state)
  137. {
  138. case SSL_ST_BEFORE:
  139. case SSL_ST_ACCEPT:
  140. case SSL_ST_BEFORE|SSL_ST_ACCEPT:
  141. case SSL_ST_OK|SSL_ST_ACCEPT:
  142. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  143. s->version=SSL2_VERSION;
  144. s->type=SSL_ST_ACCEPT;
  145. buf=s->init_buf;
  146. if ((buf == NULL) && ((buf=BUF_MEM_new()) == NULL))
  147. { ret= -1; goto end; }
  148. if (!BUF_MEM_grow(buf,(int)
  149. SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
  150. { ret= -1; goto end; }
  151. s->init_buf=buf;
  152. s->init_num=0;
  153. s->ctx->sess_accept++;
  154. s->handshake_func=ssl2_accept;
  155. s->state=SSL2_ST_GET_CLIENT_HELLO_A;
  156. BREAK;
  157. case SSL2_ST_GET_CLIENT_HELLO_A:
  158. case SSL2_ST_GET_CLIENT_HELLO_B:
  159. case SSL2_ST_GET_CLIENT_HELLO_C:
  160. s->shutdown=0;
  161. ret=get_client_hello(s);
  162. if (ret <= 0) goto end;
  163. s->init_num=0;
  164. s->state=SSL2_ST_SEND_SERVER_HELLO_A;
  165. BREAK;
  166. case SSL2_ST_SEND_SERVER_HELLO_A:
  167. case SSL2_ST_SEND_SERVER_HELLO_B:
  168. ret=server_hello(s);
  169. if (ret <= 0) goto end;
  170. s->init_num=0;
  171. if (!s->hit)
  172. {
  173. s->state=SSL2_ST_GET_CLIENT_MASTER_KEY_A;
  174. BREAK;
  175. }
  176. else
  177. {
  178. s->state=SSL2_ST_SERVER_START_ENCRYPTION;
  179. BREAK;
  180. }
  181. case SSL2_ST_GET_CLIENT_MASTER_KEY_A:
  182. case SSL2_ST_GET_CLIENT_MASTER_KEY_B:
  183. ret=get_client_master_key(s);
  184. if (ret <= 0) goto end;
  185. s->init_num=0;
  186. s->state=SSL2_ST_SERVER_START_ENCRYPTION;
  187. BREAK;
  188. case SSL2_ST_SERVER_START_ENCRYPTION:
  189. /* Ok we how have sent all the stuff needed to
  190. * start encrypting, the next packet back will
  191. * be encrypted. */
  192. if (!ssl2_enc_init(s,0))
  193. { ret= -1; goto end; }
  194. s->s2->clear_text=0;
  195. s->state=SSL2_ST_SEND_SERVER_VERIFY_A;
  196. BREAK;
  197. case SSL2_ST_SEND_SERVER_VERIFY_A:
  198. case SSL2_ST_SEND_SERVER_VERIFY_B:
  199. ret=server_verify(s);
  200. if (ret <= 0) goto end;
  201. s->init_num=0;
  202. if (s->hit)
  203. {
  204. /* If we are in here, we have been
  205. * buffering the output, so we need to
  206. * flush it and remove buffering from
  207. * future traffic */
  208. s->state=SSL2_ST_SEND_SERVER_VERIFY_C;
  209. BREAK;
  210. }
  211. else
  212. {
  213. s->state=SSL2_ST_GET_CLIENT_FINISHED_A;
  214. break;
  215. }
  216. case SSL2_ST_SEND_SERVER_VERIFY_C:
  217. /* get the number of bytes to write */
  218. num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
  219. if (num1 != 0)
  220. {
  221. s->rwstate=SSL_WRITING;
  222. num1=BIO_flush(s->wbio);
  223. if (num1 <= 0) { ret= -1; goto end; }
  224. s->rwstate=SSL_NOTHING;
  225. }
  226. /* flushed and now remove buffering */
  227. s->wbio=BIO_pop(s->wbio);
  228. s->state=SSL2_ST_GET_CLIENT_FINISHED_A;
  229. BREAK;
  230. case SSL2_ST_GET_CLIENT_FINISHED_A:
  231. case SSL2_ST_GET_CLIENT_FINISHED_B:
  232. ret=get_client_finished(s);
  233. if (ret <= 0)
  234. goto end;
  235. s->init_num=0;
  236. s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_A;
  237. BREAK;
  238. case SSL2_ST_SEND_REQUEST_CERTIFICATE_A:
  239. case SSL2_ST_SEND_REQUEST_CERTIFICATE_B:
  240. case SSL2_ST_SEND_REQUEST_CERTIFICATE_C:
  241. case SSL2_ST_SEND_REQUEST_CERTIFICATE_D:
  242. /* don't do a 'request certificate' if we
  243. * don't want to, or we already have one, and
  244. * we only want to do it once. */
  245. if (!(s->verify_mode & SSL_VERIFY_PEER) ||
  246. ((s->session->peer != NULL) &&
  247. (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)))
  248. {
  249. s->state=SSL2_ST_SEND_SERVER_FINISHED_A;
  250. break;
  251. }
  252. else
  253. {
  254. ret=request_certificate(s);
  255. if (ret <= 0) goto end;
  256. s->init_num=0;
  257. s->state=SSL2_ST_SEND_SERVER_FINISHED_A;
  258. }
  259. BREAK;
  260. case SSL2_ST_SEND_SERVER_FINISHED_A:
  261. case SSL2_ST_SEND_SERVER_FINISHED_B:
  262. ret=server_finish(s);
  263. if (ret <= 0) goto end;
  264. s->init_num=0;
  265. s->state=SSL_ST_OK;
  266. break;
  267. case SSL_ST_OK:
  268. BUF_MEM_free(s->init_buf);
  269. s->init_buf=NULL;
  270. s->init_num=0;
  271. /* ERR_clear_error();*/
  272. ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
  273. s->ctx->sess_accept_good++;
  274. /* s->server=1; */
  275. ret=1;
  276. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  277. goto end;
  278. /* BREAK; */
  279. default:
  280. SSLerr(SSL_F_SSL2_ACCEPT,SSL_R_UNKNOWN_STATE);
  281. ret= -1;
  282. goto end;
  283. /* BREAK; */
  284. }
  285. if ((cb != NULL) && (s->state != state))
  286. {
  287. new_state=s->state;
  288. s->state=state;
  289. cb(s,SSL_CB_ACCEPT_LOOP,1);
  290. s->state=new_state;
  291. }
  292. }
  293. end:
  294. s->in_handshake--;
  295. if (cb != NULL)
  296. cb(s,SSL_CB_ACCEPT_EXIT,ret);
  297. return(ret);
  298. }
  299. static int get_client_master_key(s)
  300. SSL *s;
  301. {
  302. int export,i,n,keya,error=0,ek;
  303. unsigned char *p;
  304. SSL_CIPHER *cp;
  305. EVP_CIPHER *c;
  306. EVP_MD *md;
  307. p=(unsigned char *)s->init_buf->data;
  308. if (s->state == SSL2_ST_GET_CLIENT_MASTER_KEY_A)
  309. {
  310. i=ssl2_read(s,(char *)&(p[s->init_num]),10-s->init_num);
  311. if (i < (10-s->init_num))
  312. return(ssl2_part_read(s,SSL_F_GET_CLIENT_MASTER_KEY,i));
  313. if (*(p++) != SSL2_MT_CLIENT_MASTER_KEY)
  314. {
  315. if (p[-1] != SSL2_MT_ERROR)
  316. {
  317. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  318. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_READ_WRONG_PACKET_TYPE);
  319. }
  320. else
  321. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,
  322. SSL_R_PEER_ERROR);
  323. return(-1);
  324. }
  325. cp=ssl2_get_cipher_by_char(p);
  326. if (cp == NULL)
  327. {
  328. ssl2_return_error(s,SSL2_PE_NO_CIPHER);
  329. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,
  330. SSL_R_NO_CIPHER_MATCH);
  331. return(-1);
  332. }
  333. s->session->cipher= cp;
  334. p+=3;
  335. n2s(p,i); s->s2->tmp.clear=i;
  336. n2s(p,i); s->s2->tmp.enc=i;
  337. n2s(p,i); s->session->key_arg_length=i;
  338. s->state=SSL2_ST_GET_CLIENT_MASTER_KEY_B;
  339. s->init_num=0;
  340. }
  341. /* SSL2_ST_GET_CLIENT_MASTER_KEY_B */
  342. p=(unsigned char *)s->init_buf->data;
  343. keya=s->session->key_arg_length;
  344. n=s->s2->tmp.clear+s->s2->tmp.enc+keya - s->init_num;
  345. i=ssl2_read(s,(char *)&(p[s->init_num]),n);
  346. if (i != n) return(ssl2_part_read(s,SSL_F_GET_CLIENT_MASTER_KEY,i));
  347. memcpy(s->session->key_arg,&(p[s->s2->tmp.clear+s->s2->tmp.enc]),
  348. (unsigned int)keya);
  349. if (s->session->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL)
  350. {
  351. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  352. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_NO_PRIVATEKEY);
  353. return(-1);
  354. }
  355. i=ssl_rsa_private_decrypt(s->cert,s->s2->tmp.enc,
  356. &(p[s->s2->tmp.clear]),&(p[s->s2->tmp.clear]),
  357. (s->s2->ssl2_rollback)?RSA_SSLV23_PADDING:RSA_PKCS1_PADDING);
  358. export=(s->session->cipher->algorithms & SSL_EXP)?1:0;
  359. if (!ssl_cipher_get_evp(s->session->cipher,&c,&md))
  360. {
  361. ssl2_return_error(s,SSL2_PE_NO_CIPHER);
  362. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
  363. return(0);
  364. }
  365. if (s->session->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC)
  366. {
  367. export=1;
  368. ek=8;
  369. }
  370. else
  371. ek=5;
  372. /* bad decrypt */
  373. #if 1
  374. /* If a bad decrypt, continue with protocol but with a
  375. * dud master secret */
  376. if ((i < 0) ||
  377. ((!export && (i != EVP_CIPHER_key_length(c)))
  378. || ( export && ((i != ek) || (s->s2->tmp.clear+i !=
  379. EVP_CIPHER_key_length(c))))))
  380. {
  381. if (export)
  382. i=ek;
  383. else
  384. i=EVP_CIPHER_key_length(c);
  385. RAND_bytes(p,i);
  386. }
  387. #else
  388. if (i < 0)
  389. {
  390. error=1;
  391. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_BAD_RSA_DECRYPT);
  392. }
  393. /* incorrect number of key bytes for non export cipher */
  394. else if ((!export && (i != EVP_CIPHER_key_length(c)))
  395. || ( export && ((i != ek) || (s->s2->tmp.clear+i !=
  396. EVP_CIPHER_key_length(c)))))
  397. {
  398. error=1;
  399. SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_WRONG_NUMBER_OF_KEY_BITS);
  400. }
  401. if (error)
  402. {
  403. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  404. return(-1);
  405. }
  406. #endif
  407. if (export) i+=s->s2->tmp.clear;
  408. s->session->master_key_length=i;
  409. memcpy(s->session->master_key,p,(unsigned int)i);
  410. return(1);
  411. }
  412. static int get_client_hello(s)
  413. SSL *s;
  414. {
  415. int i,n;
  416. unsigned char *p;
  417. STACK *cs; /* a stack of SSL_CIPHERS */
  418. STACK *cl; /* the ones we want to use */
  419. int z;
  420. /* This is a bit of a hack to check for the correct packet
  421. * type the first time round. */
  422. if (s->state == SSL2_ST_GET_CLIENT_HELLO_A)
  423. {
  424. s->first_packet=1;
  425. s->state=SSL2_ST_GET_CLIENT_HELLO_B;
  426. }
  427. p=(unsigned char *)s->init_buf->data;
  428. if (s->state == SSL2_ST_GET_CLIENT_HELLO_B)
  429. {
  430. i=ssl2_read(s,(char *)&(p[s->init_num]),9-s->init_num);
  431. if (i < (9-s->init_num))
  432. return(ssl2_part_read(s,SSL_F_GET_CLIENT_HELLO,i));
  433. if (*(p++) != SSL2_MT_CLIENT_HELLO)
  434. {
  435. if (p[-1] != SSL2_MT_ERROR)
  436. {
  437. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  438. SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_READ_WRONG_PACKET_TYPE);
  439. }
  440. else
  441. SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_PEER_ERROR);
  442. return(-1);
  443. }
  444. n2s(p,i);
  445. if (i < s->version) s->version=i;
  446. n2s(p,i); s->s2->tmp.cipher_spec_length=i;
  447. n2s(p,i); s->s2->tmp.session_id_length=i;
  448. n2s(p,i); s->s2->challenge_length=i;
  449. if ( (i < SSL2_MIN_CHALLENGE_LENGTH) ||
  450. (i > SSL2_MAX_CHALLENGE_LENGTH))
  451. {
  452. SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_INVALID_CHALLENGE_LENGTH);
  453. return(-1);
  454. }
  455. s->state=SSL2_ST_GET_CLIENT_HELLO_C;
  456. s->init_num=0;
  457. }
  458. /* SSL2_ST_GET_CLIENT_HELLO_C */
  459. p=(unsigned char *)s->init_buf->data;
  460. n=s->s2->tmp.cipher_spec_length+s->s2->challenge_length+
  461. s->s2->tmp.session_id_length-s->init_num;
  462. i=ssl2_read(s,(char *)&(p[s->init_num]),n);
  463. if (i != n) return(ssl2_part_read(s,SSL_F_GET_CLIENT_HELLO,i));
  464. /* get session-id before cipher stuff so we can get out session
  465. * structure if it is cached */
  466. /* session-id */
  467. if ((s->s2->tmp.session_id_length != 0) &&
  468. (s->s2->tmp.session_id_length != SSL2_SSL_SESSION_ID_LENGTH))
  469. {
  470. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  471. SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_BAD_SSL_SESSION_ID_LENGTH);
  472. return(-1);
  473. }
  474. if (s->s2->tmp.session_id_length == 0)
  475. {
  476. if (!ssl_get_new_session(s,1))
  477. {
  478. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  479. return(-1);
  480. }
  481. }
  482. else
  483. {
  484. i=ssl_get_prev_session(s,&(p[s->s2->tmp.cipher_spec_length]),
  485. s->s2->tmp.session_id_length);
  486. if (i == 1)
  487. { /* previous session */
  488. s->hit=1;
  489. }
  490. else if (i == -1)
  491. {
  492. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  493. return(-1);
  494. }
  495. else
  496. {
  497. if (s->cert == NULL)
  498. {
  499. ssl2_return_error(s,SSL2_PE_NO_CERTIFICATE);
  500. SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_NO_CERTIFICATE_SET);
  501. return(-1);
  502. }
  503. if (!ssl_get_new_session(s,1))
  504. {
  505. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  506. return(-1);
  507. }
  508. }
  509. }
  510. if (!s->hit)
  511. {
  512. cs=ssl_bytes_to_cipher_list(s,p,s->s2->tmp.cipher_spec_length,
  513. &s->session->ciphers);
  514. if (cs == NULL) goto mem_err;
  515. cl=ssl_get_ciphers_by_id(s);
  516. for (z=0; z<sk_num(cs); z++)
  517. {
  518. if (sk_find(cl,sk_value(cs,z)) < 0)
  519. {
  520. sk_delete(cs,z);
  521. z--;
  522. }
  523. }
  524. /* s->session->ciphers should now have a list of
  525. * ciphers that are on both the client and server.
  526. * This list is ordered by the order the client sent
  527. * the ciphers.
  528. */
  529. }
  530. p+=s->s2->tmp.cipher_spec_length;
  531. /* done cipher selection */
  532. /* session id extracted already */
  533. p+=s->s2->tmp.session_id_length;
  534. /* challenge */
  535. memcpy(s->s2->challenge,p,(unsigned int)s->s2->challenge_length);
  536. return(1);
  537. mem_err:
  538. SSLerr(SSL_F_GET_CLIENT_HELLO,ERR_R_MALLOC_FAILURE);
  539. return(0);
  540. }
  541. static int server_hello(s)
  542. SSL *s;
  543. {
  544. unsigned char *p,*d;
  545. int n,hit;
  546. STACK *sk;
  547. p=(unsigned char *)s->init_buf->data;
  548. if (s->state == SSL2_ST_SEND_SERVER_HELLO_A)
  549. {
  550. d=p+11;
  551. *(p++)=SSL2_MT_SERVER_HELLO; /* type */
  552. hit=s->hit;
  553. *(p++)=(unsigned char)hit;
  554. if (!hit)
  555. { /* else add cert to session */
  556. CRYPTO_add(&s->cert->references,1,CRYPTO_LOCK_SSL_CERT);
  557. if (s->session->cert != NULL)
  558. ssl_cert_free(s->session->cert);
  559. s->session->cert=s->cert;
  560. }
  561. else /* We have a session id-cache hit, if the
  562. * session-id has no certificate listed against
  563. * the 'cert' structure, grab the 'old' one
  564. * listed against the SSL connection */
  565. {
  566. if (s->session->cert == NULL)
  567. {
  568. CRYPTO_add(&s->cert->references,1,
  569. CRYPTO_LOCK_SSL_CERT);
  570. s->session->cert=s->cert;
  571. }
  572. }
  573. if (s->session->cert == NULL)
  574. {
  575. ssl2_return_error(s,SSL2_PE_NO_CERTIFICATE);
  576. SSLerr(SSL_F_SERVER_HELLO,SSL_R_NO_CERTIFICATE_SPECIFIED);
  577. return(-1);
  578. }
  579. if (hit)
  580. {
  581. *(p++)=0; /* no certificate type */
  582. s2n(s->version,p); /* version */
  583. s2n(0,p); /* cert len */
  584. s2n(0,p); /* ciphers len */
  585. }
  586. else
  587. {
  588. /* EAY EAY */
  589. /* put certificate type */
  590. *(p++)=SSL2_CT_X509_CERTIFICATE;
  591. s2n(s->version,p); /* version */
  592. n=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);
  593. s2n(n,p); /* certificate length */
  594. i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&d);
  595. n=0;
  596. /* lets send out the ciphers we like in the
  597. * prefered order */
  598. sk= s->session->ciphers;
  599. n=ssl_cipher_list_to_bytes(s,s->session->ciphers,d);
  600. d+=n;
  601. s2n(n,p); /* add cipher length */
  602. }
  603. /* make and send conn_id */
  604. s2n(SSL2_CONNECTION_ID_LENGTH,p); /* add conn_id length */
  605. s->s2->conn_id_length=SSL2_CONNECTION_ID_LENGTH;
  606. RAND_bytes(s->s2->conn_id,(int)s->s2->conn_id_length);
  607. memcpy(d,s->s2->conn_id,SSL2_CONNECTION_ID_LENGTH);
  608. d+=SSL2_CONNECTION_ID_LENGTH;
  609. s->state=SSL2_ST_SEND_SERVER_HELLO_B;
  610. s->init_num=d-(unsigned char *)s->init_buf->data;
  611. s->init_off=0;
  612. }
  613. /* SSL2_ST_SEND_SERVER_HELLO_B */
  614. /* If we are using TCP/IP, the performace is bad if we do 2
  615. * writes without a read between them. This occurs when
  616. * Session-id reuse is used, so I will put in a buffering module
  617. */
  618. if (s->hit)
  619. {
  620. if (!ssl_init_wbio_buffer(s,1)) return(-1);
  621. }
  622. return(ssl2_do_write(s));
  623. }
  624. static int get_client_finished(s)
  625. SSL *s;
  626. {
  627. unsigned char *p;
  628. int i;
  629. p=(unsigned char *)s->init_buf->data;
  630. if (s->state == SSL2_ST_GET_CLIENT_FINISHED_A)
  631. {
  632. i=ssl2_read(s,(char *)&(p[s->init_num]),1-s->init_num);
  633. if (i < 1-s->init_num)
  634. return(ssl2_part_read(s,SSL_F_GET_CLIENT_FINISHED,i));
  635. if (*p != SSL2_MT_CLIENT_FINISHED)
  636. {
  637. if (*p != SSL2_MT_ERROR)
  638. {
  639. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  640. SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_READ_WRONG_PACKET_TYPE);
  641. }
  642. else
  643. SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_PEER_ERROR);
  644. return(-1);
  645. }
  646. s->init_num=0;
  647. s->state=SSL2_ST_GET_CLIENT_FINISHED_B;
  648. }
  649. /* SSL2_ST_GET_CLIENT_FINISHED_B */
  650. i=ssl2_read(s,(char *)&(p[s->init_num]),s->s2->conn_id_length-s->init_num);
  651. if (i < (int)s->s2->conn_id_length-s->init_num)
  652. {
  653. return(ssl2_part_read(s,SSL_F_GET_CLIENT_FINISHED,i));
  654. }
  655. if (memcmp(p,s->s2->conn_id,(unsigned int)s->s2->conn_id_length) != 0)
  656. {
  657. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  658. SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_CONNECTION_ID_IS_DIFFERENT);
  659. return(-1);
  660. }
  661. return(1);
  662. }
  663. static int server_verify(s)
  664. SSL *s;
  665. {
  666. unsigned char *p;
  667. if (s->state == SSL2_ST_SEND_SERVER_VERIFY_A)
  668. {
  669. p=(unsigned char *)s->init_buf->data;
  670. *(p++)=SSL2_MT_SERVER_VERIFY;
  671. memcpy(p,s->s2->challenge,(unsigned int)s->s2->challenge_length);
  672. /* p+=s->s2->challenge_length; */
  673. s->state=SSL2_ST_SEND_SERVER_VERIFY_B;
  674. s->init_num=s->s2->challenge_length+1;
  675. s->init_off=0;
  676. }
  677. return(ssl2_do_write(s));
  678. }
  679. static int server_finish(s)
  680. SSL *s;
  681. {
  682. unsigned char *p;
  683. if (s->state == SSL2_ST_SEND_SERVER_FINISHED_A)
  684. {
  685. p=(unsigned char *)s->init_buf->data;
  686. *(p++)=SSL2_MT_SERVER_FINISHED;
  687. memcpy(p,s->session->session_id,
  688. (unsigned int)s->session->session_id_length);
  689. /* p+=s->session->session_id_length; */
  690. s->state=SSL2_ST_SEND_SERVER_FINISHED_B;
  691. s->init_num=s->session->session_id_length+1;
  692. s->init_off=0;
  693. }
  694. /* SSL2_ST_SEND_SERVER_FINISHED_B */
  695. return(ssl2_do_write(s));
  696. }
  697. /* send the request and check the response */
  698. static int request_certificate(s)
  699. SSL *s;
  700. {
  701. unsigned char *p,*p2,*buf2;
  702. unsigned char *ccd;
  703. int i,j,ctype,ret= -1;
  704. X509 *x509=NULL;
  705. STACK *sk=NULL;
  706. ccd=s->s2->tmp.ccl;
  707. if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_A)
  708. {
  709. p=(unsigned char *)s->init_buf->data;
  710. *(p++)=SSL2_MT_REQUEST_CERTIFICATE;
  711. *(p++)=SSL2_AT_MD5_WITH_RSA_ENCRYPTION;
  712. RAND_bytes(ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
  713. memcpy(p,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
  714. s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_B;
  715. s->init_num=SSL2_MIN_CERT_CHALLENGE_LENGTH+2;
  716. s->init_off=0;
  717. }
  718. if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_B)
  719. {
  720. i=ssl2_do_write(s);
  721. if (i <= 0)
  722. {
  723. ret=i;
  724. goto end;
  725. }
  726. s->init_num=0;
  727. s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_C;
  728. }
  729. if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_C)
  730. {
  731. p=(unsigned char *)s->init_buf->data;
  732. i=ssl2_read(s,(char *)&(p[s->init_num]),6-s->init_num);
  733. if (i < 3)
  734. {
  735. ret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);
  736. goto end;
  737. }
  738. if ((*p == SSL2_MT_ERROR) && (i >= 3))
  739. {
  740. n2s(p,i);
  741. if (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  742. {
  743. ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
  744. SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  745. goto end;
  746. }
  747. ret=1;
  748. goto end;
  749. }
  750. if ((*(p++) != SSL2_MT_CLIENT_CERTIFICATE) || (i < 6))
  751. {
  752. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  753. SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_SHORT_READ);
  754. goto end;
  755. }
  756. /* ok we have a response */
  757. /* certificate type, there is only one right now. */
  758. ctype= *(p++);
  759. if (ctype != SSL2_AT_MD5_WITH_RSA_ENCRYPTION)
  760. {
  761. ssl2_return_error(s,SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE);
  762. SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_RESPONSE_ARGUMENT);
  763. goto end;
  764. }
  765. n2s(p,i); s->s2->tmp.clen=i;
  766. n2s(p,i); s->s2->tmp.rlen=i;
  767. s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_D;
  768. s->init_num=0;
  769. }
  770. /* SSL2_ST_SEND_REQUEST_CERTIFICATE_D */
  771. p=(unsigned char *)s->init_buf->data;
  772. j=s->s2->tmp.clen+s->s2->tmp.rlen-s->init_num;
  773. i=ssl2_read(s,(char *)&(p[s->init_num]),j);
  774. if (i < j)
  775. {
  776. ret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);
  777. goto end;
  778. }
  779. x509=(X509 *)d2i_X509(NULL,&p,(long)s->s2->tmp.clen);
  780. if (x509 == NULL)
  781. {
  782. SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_X509_LIB);
  783. goto msg_end;
  784. }
  785. if (((sk=sk_new_null()) == NULL) || (!sk_push(sk,(char *)x509)))
  786. {
  787. SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  788. goto msg_end;
  789. }
  790. i=ssl_verify_cert_chain(s,sk);
  791. if (i) /* we like the packet, now check the chksum */
  792. {
  793. EVP_MD_CTX ctx;
  794. EVP_PKEY *pkey=NULL;
  795. EVP_VerifyInit(&ctx,s->ctx->rsa_md5);
  796. EVP_VerifyUpdate(&ctx,s->s2->key_material,
  797. (unsigned int)s->s2->key_material_length);
  798. EVP_VerifyUpdate(&ctx,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
  799. i=i2d_X509(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);
  800. buf2=(unsigned char *)Malloc((unsigned int)i);
  801. if (buf2 == NULL)
  802. {
  803. SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  804. goto msg_end;
  805. }
  806. p2=buf2;
  807. i=i2d_X509(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&p2);
  808. EVP_VerifyUpdate(&ctx,buf2,(unsigned int)i);
  809. Free(buf2);
  810. pkey=X509_get_pubkey(x509);
  811. if (pkey == NULL) goto end;
  812. i=EVP_VerifyFinal(&ctx,p,s->s2->tmp.rlen,pkey);
  813. memset(&ctx,0,sizeof(ctx));
  814. if (i)
  815. {
  816. if (s->session->peer != NULL)
  817. X509_free(s->session->peer);
  818. s->session->peer=x509;
  819. CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509);
  820. ret=1;
  821. goto end;
  822. }
  823. else
  824. {
  825. SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_CHECKSUM);
  826. goto msg_end;
  827. }
  828. }
  829. else
  830. {
  831. msg_end:
  832. ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
  833. }
  834. end:
  835. if (sk != NULL) sk_free(sk);
  836. if (x509 != NULL) X509_free(x509);
  837. return(ret);
  838. }
  839. static int ssl_rsa_private_decrypt(c, len, from, to,padding)
  840. CERT *c;
  841. int len;
  842. unsigned char *from;
  843. unsigned char *to;
  844. int padding;
  845. {
  846. RSA *rsa;
  847. int i;
  848. if ((c == NULL) || (c->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL))
  849. {
  850. SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,SSL_R_NO_PRIVATEKEY);
  851. return(-1);
  852. }
  853. if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey->type != EVP_PKEY_RSA)
  854. {
  855. SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,SSL_R_PUBLIC_KEY_IS_NOT_RSA);
  856. return(-1);
  857. }
  858. rsa=c->pkeys[SSL_PKEY_RSA_ENC].privatekey->pkey.rsa;
  859. /* we have the public key */
  860. i=RSA_private_decrypt(len,from,to,rsa,padding);
  861. if (i < 0)
  862. SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,ERR_R_RSA_LIB);
  863. return(i);
  864. }