s23_clnt.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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. /* ====================================================================
  59. * Copyright (c) 1998-2006 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 <stdio.h>
  112. #include "ssl_locl.h"
  113. #include <openssl/buffer.h>
  114. #include <openssl/rand.h>
  115. #include <openssl/objects.h>
  116. #include <openssl/evp.h>
  117. static const SSL_METHOD *ssl23_get_client_method(int ver);
  118. static int ssl23_client_hello(SSL *s);
  119. static int ssl23_get_server_hello(SSL *s);
  120. static const SSL_METHOD *ssl23_get_client_method(int ver)
  121. {
  122. #ifndef OPENSSL_NO_SSL2
  123. if (ver == SSL2_VERSION)
  124. return (SSLv2_client_method());
  125. #endif
  126. #ifndef OPENSSL_NO_SSL3
  127. if (ver == SSL3_VERSION)
  128. return (SSLv3_client_method());
  129. #endif
  130. if (ver == TLS1_VERSION)
  131. return (TLSv1_client_method());
  132. else
  133. return (NULL);
  134. }
  135. IMPLEMENT_ssl23_meth_func(SSLv23_client_method,
  136. ssl_undefined_function,
  137. ssl23_connect, ssl23_get_client_method)
  138. int ssl23_connect(SSL *s)
  139. {
  140. BUF_MEM *buf = NULL;
  141. unsigned long Time = (unsigned long)time(NULL);
  142. void (*cb) (const SSL *ssl, int type, int val) = NULL;
  143. int ret = -1;
  144. int new_state, state;
  145. RAND_add(&Time, sizeof(Time), 0);
  146. ERR_clear_error();
  147. clear_sys_error();
  148. if (s->info_callback != NULL)
  149. cb = s->info_callback;
  150. else if (s->ctx->info_callback != NULL)
  151. cb = s->ctx->info_callback;
  152. s->in_handshake++;
  153. if (!SSL_in_init(s) || SSL_in_before(s))
  154. SSL_clear(s);
  155. for (;;) {
  156. state = s->state;
  157. switch (s->state) {
  158. case SSL_ST_BEFORE:
  159. case SSL_ST_CONNECT:
  160. case SSL_ST_BEFORE | SSL_ST_CONNECT:
  161. case SSL_ST_OK | SSL_ST_CONNECT:
  162. if (s->session != NULL) {
  163. SSLerr(SSL_F_SSL23_CONNECT,
  164. SSL_R_SSL23_DOING_SESSION_ID_REUSE);
  165. ret = -1;
  166. goto end;
  167. }
  168. s->server = 0;
  169. if (cb != NULL)
  170. cb(s, SSL_CB_HANDSHAKE_START, 1);
  171. /* s->version=TLS1_VERSION; */
  172. s->type = SSL_ST_CONNECT;
  173. if (s->init_buf == NULL) {
  174. if ((buf = BUF_MEM_new()) == NULL) {
  175. ret = -1;
  176. goto end;
  177. }
  178. if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
  179. ret = -1;
  180. goto end;
  181. }
  182. s->init_buf = buf;
  183. buf = NULL;
  184. }
  185. if (!ssl3_setup_buffers(s)) {
  186. ret = -1;
  187. goto end;
  188. }
  189. ssl3_init_finished_mac(s);
  190. s->state = SSL23_ST_CW_CLNT_HELLO_A;
  191. s->ctx->stats.sess_connect++;
  192. s->init_num = 0;
  193. break;
  194. case SSL23_ST_CW_CLNT_HELLO_A:
  195. case SSL23_ST_CW_CLNT_HELLO_B:
  196. s->shutdown = 0;
  197. ret = ssl23_client_hello(s);
  198. if (ret <= 0)
  199. goto end;
  200. s->state = SSL23_ST_CR_SRVR_HELLO_A;
  201. s->init_num = 0;
  202. break;
  203. case SSL23_ST_CR_SRVR_HELLO_A:
  204. case SSL23_ST_CR_SRVR_HELLO_B:
  205. ret = ssl23_get_server_hello(s);
  206. if (ret >= 0)
  207. cb = NULL;
  208. goto end;
  209. /* break; */
  210. default:
  211. SSLerr(SSL_F_SSL23_CONNECT, SSL_R_UNKNOWN_STATE);
  212. ret = -1;
  213. goto end;
  214. /* break; */
  215. }
  216. if (s->debug) {
  217. (void)BIO_flush(s->wbio);
  218. }
  219. if ((cb != NULL) && (s->state != state)) {
  220. new_state = s->state;
  221. s->state = state;
  222. cb(s, SSL_CB_CONNECT_LOOP, 1);
  223. s->state = new_state;
  224. }
  225. }
  226. end:
  227. s->in_handshake--;
  228. if (buf != NULL)
  229. BUF_MEM_free(buf);
  230. if (cb != NULL)
  231. cb(s, SSL_CB_CONNECT_EXIT, ret);
  232. return (ret);
  233. }
  234. static int ssl23_no_ssl2_ciphers(SSL *s)
  235. {
  236. SSL_CIPHER *cipher;
  237. STACK_OF(SSL_CIPHER) *ciphers;
  238. int i;
  239. ciphers = SSL_get_ciphers(s);
  240. for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  241. cipher = sk_SSL_CIPHER_value(ciphers, i);
  242. if (cipher->algorithm_ssl == SSL_SSLV2)
  243. return 0;
  244. }
  245. return 1;
  246. }
  247. static int ssl23_client_hello(SSL *s)
  248. {
  249. unsigned char *buf;
  250. unsigned char *p, *d;
  251. int i, ch_len;
  252. unsigned long Time, l;
  253. int ssl2_compat;
  254. int version = 0, version_major, version_minor;
  255. #ifndef OPENSSL_NO_COMP
  256. int j;
  257. SSL_COMP *comp;
  258. #endif
  259. int ret;
  260. ssl2_compat = (s->options & SSL_OP_NO_SSLv2) ? 0 : 1;
  261. if (ssl2_compat && ssl23_no_ssl2_ciphers(s))
  262. ssl2_compat = 0;
  263. if (!(s->options & SSL_OP_NO_TLSv1)) {
  264. version = TLS1_VERSION;
  265. } else if (!(s->options & SSL_OP_NO_SSLv3)) {
  266. version = SSL3_VERSION;
  267. } else if (!(s->options & SSL_OP_NO_SSLv2)) {
  268. version = SSL2_VERSION;
  269. }
  270. #ifndef OPENSSL_NO_TLSEXT
  271. if (version != SSL2_VERSION) {
  272. /*
  273. * have to disable SSL 2.0 compatibility if we need TLS extensions
  274. */
  275. if (s->tlsext_hostname != NULL)
  276. ssl2_compat = 0;
  277. if (s->tlsext_status_type != -1)
  278. ssl2_compat = 0;
  279. # ifdef TLSEXT_TYPE_opaque_prf_input
  280. if (s->ctx->tlsext_opaque_prf_input_callback != 0
  281. || s->tlsext_opaque_prf_input != NULL)
  282. ssl2_compat = 0;
  283. # endif
  284. }
  285. #endif
  286. buf = (unsigned char *)s->init_buf->data;
  287. if (s->state == SSL23_ST_CW_CLNT_HELLO_A) {
  288. #if 0
  289. /* don't reuse session-id's */
  290. if (!ssl_get_new_session(s, 0)) {
  291. return (-1);
  292. }
  293. #endif
  294. p = s->s3->client_random;
  295. Time = (unsigned long)time(NULL); /* Time */
  296. l2n(Time, p);
  297. if (RAND_pseudo_bytes(p, SSL3_RANDOM_SIZE - 4) <= 0)
  298. return -1;
  299. if (version == TLS1_VERSION) {
  300. version_major = TLS1_VERSION_MAJOR;
  301. version_minor = TLS1_VERSION_MINOR;
  302. } else if (version == SSL3_VERSION) {
  303. version_major = SSL3_VERSION_MAJOR;
  304. version_minor = SSL3_VERSION_MINOR;
  305. } else if (version == SSL2_VERSION) {
  306. version_major = SSL2_VERSION_MAJOR;
  307. version_minor = SSL2_VERSION_MINOR;
  308. } else {
  309. SSLerr(SSL_F_SSL23_CLIENT_HELLO, SSL_R_NO_PROTOCOLS_AVAILABLE);
  310. return (-1);
  311. }
  312. s->client_version = version;
  313. if (ssl2_compat) {
  314. /* create SSL 2.0 compatible Client Hello */
  315. /* two byte record header will be written last */
  316. d = &(buf[2]);
  317. p = d + 9; /* leave space for message type, version,
  318. * individual length fields */
  319. *(d++) = SSL2_MT_CLIENT_HELLO;
  320. *(d++) = version_major;
  321. *(d++) = version_minor;
  322. /* Ciphers supported */
  323. i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), p, 0);
  324. if (i == 0) {
  325. /* no ciphers */
  326. SSLerr(SSL_F_SSL23_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
  327. return -1;
  328. }
  329. s2n(i, d);
  330. p += i;
  331. /*
  332. * put in the session-id length (zero since there is no reuse)
  333. */
  334. #if 0
  335. s->session->session_id_length = 0;
  336. #endif
  337. s2n(0, d);
  338. if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
  339. ch_len = SSL2_CHALLENGE_LENGTH;
  340. else
  341. ch_len = SSL2_MAX_CHALLENGE_LENGTH;
  342. /* write out sslv2 challenge */
  343. /*
  344. * Note that ch_len must be <= SSL3_RANDOM_SIZE (32), because it
  345. * is one of SSL2_MAX_CHALLENGE_LENGTH (32) or
  346. * SSL2_MAX_CHALLENGE_LENGTH (16), but leave the check in for
  347. * futurproofing
  348. */
  349. if (SSL3_RANDOM_SIZE < ch_len)
  350. i = SSL3_RANDOM_SIZE;
  351. else
  352. i = ch_len;
  353. s2n(i, d);
  354. memset(&(s->s3->client_random[0]), 0, SSL3_RANDOM_SIZE);
  355. if (RAND_pseudo_bytes
  356. (&(s->s3->client_random[SSL3_RANDOM_SIZE - i]), i) <= 0)
  357. return -1;
  358. memcpy(p, &(s->s3->client_random[SSL3_RANDOM_SIZE - i]), i);
  359. p += i;
  360. i = p - &(buf[2]);
  361. buf[0] = ((i >> 8) & 0xff) | 0x80;
  362. buf[1] = (i & 0xff);
  363. /* number of bytes to write */
  364. s->init_num = i + 2;
  365. s->init_off = 0;
  366. ssl3_finish_mac(s, &(buf[2]), i);
  367. } else {
  368. /* create Client Hello in SSL 3.0/TLS 1.0 format */
  369. /*
  370. * do the record header (5 bytes) and handshake message header (4
  371. * bytes) last
  372. */
  373. d = p = &(buf[9]);
  374. *(p++) = version_major;
  375. *(p++) = version_minor;
  376. /* Random stuff */
  377. memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
  378. p += SSL3_RANDOM_SIZE;
  379. /* Session ID (zero since there is no reuse) */
  380. *(p++) = 0;
  381. /* Ciphers supported (using SSL 3.0/TLS 1.0 format) */
  382. i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]),
  383. ssl3_put_cipher_by_char);
  384. if (i == 0) {
  385. SSLerr(SSL_F_SSL23_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
  386. return -1;
  387. }
  388. s2n(i, p);
  389. p += i;
  390. /* COMPRESSION */
  391. #ifdef OPENSSL_NO_COMP
  392. *(p++) = 1;
  393. #else
  394. if ((s->options & SSL_OP_NO_COMPRESSION)
  395. || !s->ctx->comp_methods)
  396. j = 0;
  397. else
  398. j = sk_SSL_COMP_num(s->ctx->comp_methods);
  399. *(p++) = 1 + j;
  400. for (i = 0; i < j; i++) {
  401. comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
  402. *(p++) = comp->id;
  403. }
  404. #endif
  405. *(p++) = 0; /* Add the NULL method */
  406. #ifndef OPENSSL_NO_TLSEXT
  407. /* TLS extensions */
  408. if (ssl_prepare_clienthello_tlsext(s) <= 0) {
  409. SSLerr(SSL_F_SSL23_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
  410. return -1;
  411. }
  412. if ((p =
  413. ssl_add_clienthello_tlsext(s, p,
  414. buf +
  415. SSL3_RT_MAX_PLAIN_LENGTH)) ==
  416. NULL) {
  417. SSLerr(SSL_F_SSL23_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  418. return -1;
  419. }
  420. #endif
  421. l = p - d;
  422. /* fill in 4-byte handshake header */
  423. d = &(buf[5]);
  424. *(d++) = SSL3_MT_CLIENT_HELLO;
  425. l2n3(l, d);
  426. l += 4;
  427. if (l > SSL3_RT_MAX_PLAIN_LENGTH) {
  428. SSLerr(SSL_F_SSL23_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  429. return -1;
  430. }
  431. /* fill in 5-byte record header */
  432. d = buf;
  433. *(d++) = SSL3_RT_HANDSHAKE;
  434. *(d++) = version_major;
  435. *(d++) = version_minor; /* arguably we should send the *lowest*
  436. * suported version here (indicating,
  437. * e.g., TLS 1.0 in "SSL 3.0 format") */
  438. s2n((int)l, d);
  439. /* number of bytes to write */
  440. s->init_num = p - buf;
  441. s->init_off = 0;
  442. ssl3_finish_mac(s, &(buf[5]), s->init_num - 5);
  443. }
  444. s->state = SSL23_ST_CW_CLNT_HELLO_B;
  445. s->init_off = 0;
  446. }
  447. /* SSL3_ST_CW_CLNT_HELLO_B */
  448. ret = ssl23_write_bytes(s);
  449. if ((ret >= 2) && s->msg_callback) {
  450. /* Client Hello has been sent; tell msg_callback */
  451. if (ssl2_compat)
  452. s->msg_callback(1, SSL2_VERSION, 0, s->init_buf->data + 2,
  453. ret - 2, s, s->msg_callback_arg);
  454. else
  455. s->msg_callback(1, version, SSL3_RT_HANDSHAKE,
  456. s->init_buf->data + 5, ret - 5, s,
  457. s->msg_callback_arg);
  458. }
  459. return ret;
  460. }
  461. static int ssl23_get_server_hello(SSL *s)
  462. {
  463. char buf[8];
  464. unsigned char *p;
  465. int i;
  466. int n;
  467. n = ssl23_read_bytes(s, 7);
  468. if (n != 7)
  469. return (n);
  470. p = s->packet;
  471. memcpy(buf, p, n);
  472. if ((p[0] & 0x80) && (p[2] == SSL2_MT_SERVER_HELLO) &&
  473. (p[5] == 0x00) && (p[6] == 0x02)) {
  474. #ifdef OPENSSL_NO_SSL2
  475. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
  476. goto err;
  477. #else
  478. /* we are talking sslv2 */
  479. /*
  480. * we need to clean up the SSLv3 setup and put in the sslv2 stuff.
  481. */
  482. int ch_len;
  483. if (s->options & SSL_OP_NO_SSLv2) {
  484. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
  485. goto err;
  486. }
  487. if (s->s2 == NULL) {
  488. if (!ssl2_new(s))
  489. goto err;
  490. } else
  491. ssl2_clear(s);
  492. if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
  493. ch_len = SSL2_CHALLENGE_LENGTH;
  494. else
  495. ch_len = SSL2_MAX_CHALLENGE_LENGTH;
  496. /* write out sslv2 challenge */
  497. /*
  498. * Note that ch_len must be <= SSL3_RANDOM_SIZE (32), because it is
  499. * one of SSL2_MAX_CHALLENGE_LENGTH (32) or SSL2_MAX_CHALLENGE_LENGTH
  500. * (16), but leave the check in for futurproofing
  501. */
  502. i = (SSL3_RANDOM_SIZE < ch_len)
  503. ? SSL3_RANDOM_SIZE : ch_len;
  504. s->s2->challenge_length = i;
  505. memcpy(s->s2->challenge,
  506. &(s->s3->client_random[SSL3_RANDOM_SIZE - i]), i);
  507. if (s->s3 != NULL)
  508. ssl3_free(s);
  509. if (!BUF_MEM_grow_clean(s->init_buf,
  510. SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)) {
  511. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO, ERR_R_BUF_LIB);
  512. goto err;
  513. }
  514. s->state = SSL2_ST_GET_SERVER_HELLO_A;
  515. if (!(s->client_version == SSL2_VERSION))
  516. /*
  517. * use special padding (SSL 3.0 draft/RFC 2246, App. E.2)
  518. */
  519. s->s2->ssl2_rollback = 1;
  520. /*
  521. * setup the 7 bytes we have read so we get them from the sslv2
  522. * buffer
  523. */
  524. s->rstate = SSL_ST_READ_HEADER;
  525. s->packet_length = n;
  526. s->packet = &(s->s2->rbuf[0]);
  527. memcpy(s->packet, buf, n);
  528. s->s2->rbuf_left = n;
  529. s->s2->rbuf_offs = 0;
  530. /* we have already written one */
  531. s->s2->write_sequence = 1;
  532. s->method = SSLv2_client_method();
  533. s->handshake_func = s->method->ssl_connect;
  534. #endif
  535. } else if (p[1] == SSL3_VERSION_MAJOR &&
  536. (p[2] == SSL3_VERSION_MINOR || p[2] == TLS1_VERSION_MINOR) &&
  537. ((p[0] == SSL3_RT_HANDSHAKE && p[5] == SSL3_MT_SERVER_HELLO) ||
  538. (p[0] == SSL3_RT_ALERT && p[3] == 0 && p[4] == 2))) {
  539. /* we have sslv3 or tls1 (server hello or alert) */
  540. #ifndef OPENSSL_NO_SSL3
  541. if ((p[2] == SSL3_VERSION_MINOR) && !(s->options & SSL_OP_NO_SSLv3)) {
  542. s->version = SSL3_VERSION;
  543. s->method = SSLv3_client_method();
  544. } else
  545. #endif
  546. if ((p[2] == TLS1_VERSION_MINOR) && !(s->options & SSL_OP_NO_TLSv1)) {
  547. s->version = TLS1_VERSION;
  548. s->method = TLSv1_client_method();
  549. } else {
  550. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
  551. goto err;
  552. }
  553. /* ensure that TLS_MAX_VERSION is up-to-date */
  554. OPENSSL_assert(s->version <= TLS_MAX_VERSION);
  555. if (p[0] == SSL3_RT_ALERT && p[5] != SSL3_AL_WARNING) {
  556. /* fatal alert */
  557. void (*cb) (const SSL *ssl, int type, int val) = NULL;
  558. int j;
  559. if (s->info_callback != NULL)
  560. cb = s->info_callback;
  561. else if (s->ctx->info_callback != NULL)
  562. cb = s->ctx->info_callback;
  563. i = p[5];
  564. if (cb != NULL) {
  565. j = (i << 8) | p[6];
  566. cb(s, SSL_CB_READ_ALERT, j);
  567. }
  568. if (s->msg_callback)
  569. s->msg_callback(0, s->version, SSL3_RT_ALERT, p + 5, 2, s,
  570. s->msg_callback_arg);
  571. s->rwstate = SSL_NOTHING;
  572. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO, SSL_AD_REASON_OFFSET + p[6]);
  573. goto err;
  574. }
  575. if (!ssl_init_wbio_buffer(s, 1))
  576. goto err;
  577. /* we are in this state */
  578. s->state = SSL3_ST_CR_SRVR_HELLO_A;
  579. /*
  580. * put the 7 bytes we have read into the input buffer for SSLv3
  581. */
  582. s->rstate = SSL_ST_READ_HEADER;
  583. s->packet_length = n;
  584. if (s->s3->rbuf.buf == NULL)
  585. if (!ssl3_setup_read_buffer(s))
  586. goto err;
  587. s->packet = &(s->s3->rbuf.buf[0]);
  588. memcpy(s->packet, buf, n);
  589. s->s3->rbuf.left = n;
  590. s->s3->rbuf.offset = 0;
  591. s->handshake_func = s->method->ssl_connect;
  592. } else {
  593. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO, SSL_R_UNKNOWN_PROTOCOL);
  594. goto err;
  595. }
  596. s->init_num = 0;
  597. /*
  598. * Since, if we are sending a ssl23 client hello, we are not reusing a
  599. * session-id
  600. */
  601. if (!ssl_get_new_session(s, 0))
  602. goto err;
  603. return (SSL_connect(s));
  604. err:
  605. return (-1);
  606. }