s23_clnt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /* ssl/s23_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. #include <stdio.h>
  59. #include "ssl_locl.h"
  60. #include <openssl/buffer.h>
  61. #include <openssl/rand.h>
  62. #include <openssl/objects.h>
  63. #include <openssl/evp.h>
  64. static SSL_METHOD *ssl23_get_client_method(int ver);
  65. static int ssl23_client_hello(SSL *s);
  66. static int ssl23_get_server_hello(SSL *s);
  67. static SSL_METHOD *ssl23_get_client_method(int ver)
  68. {
  69. #ifndef OPENSSL_NO_SSL2
  70. if (ver == SSL2_VERSION)
  71. return(SSLv2_client_method());
  72. #endif
  73. if (ver == SSL3_VERSION)
  74. return(SSLv3_client_method());
  75. else if (ver == TLS1_VERSION)
  76. return(TLSv1_client_method());
  77. else
  78. return(NULL);
  79. }
  80. IMPLEMENT_ssl23_meth_func(SSLv23_client_method,
  81. ssl_undefined_function,
  82. ssl23_connect,
  83. ssl23_get_client_method)
  84. int ssl23_connect(SSL *s)
  85. {
  86. BUF_MEM *buf=NULL;
  87. unsigned long Time=(unsigned long)time(NULL);
  88. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  89. int ret= -1;
  90. int new_state,state;
  91. RAND_add(&Time,sizeof(Time),0);
  92. ERR_clear_error();
  93. clear_sys_error();
  94. if (s->info_callback != NULL)
  95. cb=s->info_callback;
  96. else if (s->ctx->info_callback != NULL)
  97. cb=s->ctx->info_callback;
  98. s->in_handshake++;
  99. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  100. for (;;)
  101. {
  102. state=s->state;
  103. switch(s->state)
  104. {
  105. case SSL_ST_BEFORE:
  106. case SSL_ST_CONNECT:
  107. case SSL_ST_BEFORE|SSL_ST_CONNECT:
  108. case SSL_ST_OK|SSL_ST_CONNECT:
  109. if (s->session != NULL)
  110. {
  111. SSLerr(SSL_F_SSL23_CONNECT,SSL_R_SSL23_DOING_SESSION_ID_REUSE);
  112. ret= -1;
  113. goto end;
  114. }
  115. s->server=0;
  116. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  117. /* s->version=TLS1_VERSION; */
  118. s->type=SSL_ST_CONNECT;
  119. if (s->init_buf == NULL)
  120. {
  121. if ((buf=BUF_MEM_new()) == NULL)
  122. {
  123. ret= -1;
  124. goto end;
  125. }
  126. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  127. {
  128. ret= -1;
  129. goto end;
  130. }
  131. s->init_buf=buf;
  132. buf=NULL;
  133. }
  134. if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
  135. ssl3_init_finished_mac(s);
  136. s->state=SSL23_ST_CW_CLNT_HELLO_A;
  137. s->ctx->stats.sess_connect++;
  138. s->init_num=0;
  139. break;
  140. case SSL23_ST_CW_CLNT_HELLO_A:
  141. case SSL23_ST_CW_CLNT_HELLO_B:
  142. s->shutdown=0;
  143. ret=ssl23_client_hello(s);
  144. if (ret <= 0) goto end;
  145. s->state=SSL23_ST_CR_SRVR_HELLO_A;
  146. s->init_num=0;
  147. break;
  148. case SSL23_ST_CR_SRVR_HELLO_A:
  149. case SSL23_ST_CR_SRVR_HELLO_B:
  150. ret=ssl23_get_server_hello(s);
  151. if (ret >= 0) cb=NULL;
  152. goto end;
  153. /* break; */
  154. default:
  155. SSLerr(SSL_F_SSL23_CONNECT,SSL_R_UNKNOWN_STATE);
  156. ret= -1;
  157. goto end;
  158. /* break; */
  159. }
  160. if (s->debug) { (void)BIO_flush(s->wbio); }
  161. if ((cb != NULL) && (s->state != state))
  162. {
  163. new_state=s->state;
  164. s->state=state;
  165. cb(s,SSL_CB_CONNECT_LOOP,1);
  166. s->state=new_state;
  167. }
  168. }
  169. end:
  170. s->in_handshake--;
  171. if (buf != NULL)
  172. BUF_MEM_free(buf);
  173. if (cb != NULL)
  174. cb(s,SSL_CB_CONNECT_EXIT,ret);
  175. return(ret);
  176. }
  177. static int ssl23_client_hello(SSL *s)
  178. {
  179. unsigned char *buf;
  180. unsigned char *p,*d;
  181. int i,j,ch_len;
  182. unsigned long Time,l;
  183. int ssl2_compat;
  184. int version = 0, version_major, version_minor;
  185. SSL_COMP *comp;
  186. int ret;
  187. ssl2_compat = (s->options & SSL_OP_NO_SSLv2) ? 0 : 1;
  188. if (!(s->options & SSL_OP_NO_TLSv1))
  189. {
  190. version = TLS1_VERSION;
  191. }
  192. else if (!(s->options & SSL_OP_NO_SSLv3))
  193. {
  194. version = SSL3_VERSION;
  195. }
  196. else if (!(s->options & SSL_OP_NO_SSLv2))
  197. {
  198. version = SSL2_VERSION;
  199. }
  200. buf=(unsigned char *)s->init_buf->data;
  201. if (s->state == SSL23_ST_CW_CLNT_HELLO_A)
  202. {
  203. #if 0
  204. /* don't reuse session-id's */
  205. if (!ssl_get_new_session(s,0))
  206. {
  207. return(-1);
  208. }
  209. #endif
  210. p=s->s3->client_random;
  211. Time=(unsigned long)time(NULL); /* Time */
  212. l2n(Time,p);
  213. if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
  214. return -1;
  215. if (version == TLS1_VERSION)
  216. {
  217. version_major = TLS1_VERSION_MAJOR;
  218. version_minor = TLS1_VERSION_MINOR;
  219. }
  220. #ifdef OPENSSL_FIPS
  221. else if(FIPS_mode())
  222. {
  223. SSLerr(SSL_F_SSL23_CLIENT_HELLO,
  224. SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
  225. return -1;
  226. }
  227. #endif
  228. else if (version == SSL3_VERSION)
  229. {
  230. version_major = SSL3_VERSION_MAJOR;
  231. version_minor = SSL3_VERSION_MINOR;
  232. }
  233. else if (version == SSL2_VERSION)
  234. {
  235. version_major = SSL2_VERSION_MAJOR;
  236. version_minor = SSL2_VERSION_MINOR;
  237. }
  238. else
  239. {
  240. SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_PROTOCOLS_AVAILABLE);
  241. return(-1);
  242. }
  243. s->client_version = version;
  244. if (ssl2_compat)
  245. {
  246. /* create SSL 2.0 compatible Client Hello */
  247. /* two byte record header will be written last */
  248. d = &(buf[2]);
  249. p = d + 9; /* leave space for message type, version, individual length fields */
  250. *(d++) = SSL2_MT_CLIENT_HELLO;
  251. *(d++) = version_major;
  252. *(d++) = version_minor;
  253. /* Ciphers supported */
  254. i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),p,0);
  255. if (i == 0)
  256. {
  257. /* no ciphers */
  258. SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
  259. return -1;
  260. }
  261. s2n(i,d);
  262. p+=i;
  263. /* put in the session-id length (zero since there is no reuse) */
  264. #if 0
  265. s->session->session_id_length=0;
  266. #endif
  267. s2n(0,d);
  268. if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
  269. ch_len=SSL2_CHALLENGE_LENGTH;
  270. else
  271. ch_len=SSL2_MAX_CHALLENGE_LENGTH;
  272. /* write out sslv2 challenge */
  273. if (SSL3_RANDOM_SIZE < ch_len)
  274. i=SSL3_RANDOM_SIZE;
  275. else
  276. i=ch_len;
  277. s2n(i,d);
  278. memset(&(s->s3->client_random[0]),0,SSL3_RANDOM_SIZE);
  279. if (RAND_pseudo_bytes(&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i) <= 0)
  280. return -1;
  281. memcpy(p,&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
  282. p+=i;
  283. i= p- &(buf[2]);
  284. buf[0]=((i>>8)&0xff)|0x80;
  285. buf[1]=(i&0xff);
  286. /* number of bytes to write */
  287. s->init_num=i+2;
  288. s->init_off=0;
  289. ssl3_finish_mac(s,&(buf[2]),i);
  290. }
  291. else
  292. {
  293. /* create Client Hello in SSL 3.0/TLS 1.0 format */
  294. /* do the record header (5 bytes) and handshake message header (4 bytes) last */
  295. d = p = &(buf[9]);
  296. *(p++) = version_major;
  297. *(p++) = version_minor;
  298. /* Random stuff */
  299. memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
  300. p += SSL3_RANDOM_SIZE;
  301. /* Session ID (zero since there is no reuse) */
  302. *(p++) = 0;
  303. /* Ciphers supported (using SSL 3.0/TLS 1.0 format) */
  304. i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),ssl3_put_cipher_by_char);
  305. if (i == 0)
  306. {
  307. SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
  308. return -1;
  309. }
  310. s2n(i,p);
  311. p+=i;
  312. /* COMPRESSION */
  313. if (s->ctx->comp_methods == NULL)
  314. j=0;
  315. else
  316. j=sk_SSL_COMP_num(s->ctx->comp_methods);
  317. *(p++)=1+j;
  318. for (i=0; i<j; i++)
  319. {
  320. comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
  321. *(p++)=comp->id;
  322. }
  323. *(p++)=0; /* Add the NULL method */
  324. l = p-d;
  325. *p = 42;
  326. /* fill in 4-byte handshake header */
  327. d=&(buf[5]);
  328. *(d++)=SSL3_MT_CLIENT_HELLO;
  329. l2n3(l,d);
  330. l += 4;
  331. if (l > SSL3_RT_MAX_PLAIN_LENGTH)
  332. {
  333. SSLerr(SSL_F_SSL23_CLIENT_HELLO,ERR_R_INTERNAL_ERROR);
  334. return -1;
  335. }
  336. /* fill in 5-byte record header */
  337. d=buf;
  338. *(d++) = SSL3_RT_HANDSHAKE;
  339. *(d++) = version_major;
  340. *(d++) = version_minor; /* arguably we should send the *lowest* suported version here
  341. * (indicating, e.g., TLS 1.0 in "SSL 3.0 format") */
  342. s2n((int)l,d);
  343. /* number of bytes to write */
  344. s->init_num=p-buf;
  345. s->init_off=0;
  346. ssl3_finish_mac(s,&(buf[5]), s->init_num - 5);
  347. }
  348. s->state=SSL23_ST_CW_CLNT_HELLO_B;
  349. s->init_off=0;
  350. }
  351. /* SSL3_ST_CW_CLNT_HELLO_B */
  352. ret = ssl23_write_bytes(s);
  353. if ((ret >= 2) && s->msg_callback)
  354. {
  355. /* Client Hello has been sent; tell msg_callback */
  356. if (ssl2_compat)
  357. s->msg_callback(1, SSL2_VERSION, 0, s->init_buf->data+2, ret-2, s, s->msg_callback_arg);
  358. else
  359. s->msg_callback(1, version, SSL3_RT_HANDSHAKE, s->init_buf->data+5, ret-5, s, s->msg_callback_arg);
  360. }
  361. return ret;
  362. }
  363. static int ssl23_get_server_hello(SSL *s)
  364. {
  365. char buf[8];
  366. unsigned char *p;
  367. int i;
  368. int n;
  369. n=ssl23_read_bytes(s,7);
  370. if (n != 7) return(n);
  371. p=s->packet;
  372. memcpy(buf,p,n);
  373. if ((p[0] & 0x80) && (p[2] == SSL2_MT_SERVER_HELLO) &&
  374. (p[5] == 0x00) && (p[6] == 0x02))
  375. {
  376. #ifdef OPENSSL_NO_SSL2
  377. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
  378. goto err;
  379. #else
  380. /* we are talking sslv2 */
  381. /* we need to clean up the SSLv3 setup and put in the
  382. * sslv2 stuff. */
  383. int ch_len;
  384. if (s->options & SSL_OP_NO_SSLv2)
  385. {
  386. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
  387. goto err;
  388. }
  389. if (s->s2 == NULL)
  390. {
  391. if (!ssl2_new(s))
  392. goto err;
  393. }
  394. else
  395. ssl2_clear(s);
  396. if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
  397. ch_len=SSL2_CHALLENGE_LENGTH;
  398. else
  399. ch_len=SSL2_MAX_CHALLENGE_LENGTH;
  400. /* write out sslv2 challenge */
  401. i=(SSL3_RANDOM_SIZE < ch_len)
  402. ?SSL3_RANDOM_SIZE:ch_len;
  403. s->s2->challenge_length=i;
  404. memcpy(s->s2->challenge,
  405. &(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
  406. if (s->s3 != NULL) ssl3_free(s);
  407. if (!BUF_MEM_grow_clean(s->init_buf,
  408. SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
  409. {
  410. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,ERR_R_BUF_LIB);
  411. goto err;
  412. }
  413. s->state=SSL2_ST_GET_SERVER_HELLO_A;
  414. if (!(s->client_version == SSL2_VERSION))
  415. /* use special padding (SSL 3.0 draft/RFC 2246, App. E.2) */
  416. s->s2->ssl2_rollback=1;
  417. /* setup the 5 bytes we have read so we get them from
  418. * the sslv2 buffer */
  419. s->rstate=SSL_ST_READ_HEADER;
  420. s->packet_length=n;
  421. s->packet= &(s->s2->rbuf[0]);
  422. memcpy(s->packet,buf,n);
  423. s->s2->rbuf_left=n;
  424. s->s2->rbuf_offs=0;
  425. /* we have already written one */
  426. s->s2->write_sequence=1;
  427. s->method=SSLv2_client_method();
  428. s->handshake_func=s->method->ssl_connect;
  429. #endif
  430. }
  431. else if ((p[0] == SSL3_RT_HANDSHAKE) &&
  432. (p[1] == SSL3_VERSION_MAJOR) &&
  433. ((p[2] == SSL3_VERSION_MINOR) ||
  434. (p[2] == TLS1_VERSION_MINOR)) &&
  435. (p[5] == SSL3_MT_SERVER_HELLO))
  436. {
  437. /* we have sslv3 or tls1 */
  438. if (!ssl_init_wbio_buffer(s,1)) goto err;
  439. /* we are in this state */
  440. s->state=SSL3_ST_CR_SRVR_HELLO_A;
  441. /* put the 5 bytes we have read into the input buffer
  442. * for SSLv3 */
  443. s->rstate=SSL_ST_READ_HEADER;
  444. s->packet_length=n;
  445. s->packet= &(s->s3->rbuf.buf[0]);
  446. memcpy(s->packet,buf,n);
  447. s->s3->rbuf.left=n;
  448. s->s3->rbuf.offset=0;
  449. if ((p[2] == SSL3_VERSION_MINOR) &&
  450. !(s->options & SSL_OP_NO_SSLv3))
  451. {
  452. #ifdef OPENSSL_FIPS
  453. if(FIPS_mode())
  454. {
  455. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,
  456. SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
  457. goto err;
  458. }
  459. #endif
  460. s->version=SSL3_VERSION;
  461. s->method=SSLv3_client_method();
  462. }
  463. else if ((p[2] == TLS1_VERSION_MINOR) &&
  464. !(s->options & SSL_OP_NO_TLSv1))
  465. {
  466. s->version=TLS1_VERSION;
  467. s->method=TLSv1_client_method();
  468. }
  469. else
  470. {
  471. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
  472. goto err;
  473. }
  474. s->handshake_func=s->method->ssl_connect;
  475. }
  476. else if ((p[0] == SSL3_RT_ALERT) &&
  477. (p[1] == SSL3_VERSION_MAJOR) &&
  478. ((p[2] == SSL3_VERSION_MINOR) ||
  479. (p[2] == TLS1_VERSION_MINOR)) &&
  480. (p[3] == 0) &&
  481. (p[4] == 2))
  482. {
  483. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  484. int j;
  485. /* An alert */
  486. if (s->info_callback != NULL)
  487. cb=s->info_callback;
  488. else if (s->ctx->info_callback != NULL)
  489. cb=s->ctx->info_callback;
  490. i=p[5];
  491. if (cb != NULL)
  492. {
  493. j=(i<<8)|p[6];
  494. cb(s,SSL_CB_READ_ALERT,j);
  495. }
  496. s->rwstate=SSL_NOTHING;
  497. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_AD_REASON_OFFSET+p[6]);
  498. goto err;
  499. }
  500. else
  501. {
  502. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNKNOWN_PROTOCOL);
  503. goto err;
  504. }
  505. s->init_num=0;
  506. /* Since, if we are sending a ssl23 client hello, we are not
  507. * reusing a session-id */
  508. if (!ssl_get_new_session(s,0))
  509. goto err;
  510. return(SSL_connect(s));
  511. err:
  512. return(-1);
  513. }