s23_clnt.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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 if (ver == TLS1_1_VERSION)
  133. return (TLSv1_1_client_method());
  134. else if (ver == TLS1_2_VERSION)
  135. return (TLSv1_2_client_method());
  136. else
  137. return (NULL);
  138. }
  139. IMPLEMENT_ssl23_meth_func(SSLv23_client_method,
  140. ssl_undefined_function,
  141. ssl23_connect, ssl23_get_client_method)
  142. int ssl23_connect(SSL *s)
  143. {
  144. BUF_MEM *buf = NULL;
  145. unsigned long Time = (unsigned long)time(NULL);
  146. void (*cb) (const SSL *ssl, int type, int val) = NULL;
  147. int ret = -1;
  148. int new_state, state;
  149. RAND_add(&Time, sizeof(Time), 0);
  150. ERR_clear_error();
  151. clear_sys_error();
  152. if (s->info_callback != NULL)
  153. cb = s->info_callback;
  154. else if (s->ctx->info_callback != NULL)
  155. cb = s->ctx->info_callback;
  156. s->in_handshake++;
  157. if (!SSL_in_init(s) || SSL_in_before(s))
  158. SSL_clear(s);
  159. for (;;) {
  160. state = s->state;
  161. switch (s->state) {
  162. case SSL_ST_BEFORE:
  163. case SSL_ST_CONNECT:
  164. case SSL_ST_BEFORE | SSL_ST_CONNECT:
  165. case SSL_ST_OK | SSL_ST_CONNECT:
  166. if (s->session != NULL) {
  167. SSLerr(SSL_F_SSL23_CONNECT,
  168. SSL_R_SSL23_DOING_SESSION_ID_REUSE);
  169. ret = -1;
  170. goto end;
  171. }
  172. s->server = 0;
  173. if (cb != NULL)
  174. cb(s, SSL_CB_HANDSHAKE_START, 1);
  175. /* s->version=TLS1_VERSION; */
  176. s->type = SSL_ST_CONNECT;
  177. if (s->init_buf == NULL) {
  178. if ((buf = BUF_MEM_new()) == NULL) {
  179. ret = -1;
  180. goto end;
  181. }
  182. if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
  183. ret = -1;
  184. goto end;
  185. }
  186. s->init_buf = buf;
  187. buf = NULL;
  188. }
  189. if (!ssl3_setup_buffers(s)) {
  190. ret = -1;
  191. goto end;
  192. }
  193. if (!ssl3_init_finished_mac(s)) {
  194. ret = -1;
  195. goto end;
  196. }
  197. s->state = SSL23_ST_CW_CLNT_HELLO_A;
  198. s->ctx->stats.sess_connect++;
  199. s->init_num = 0;
  200. break;
  201. case SSL23_ST_CW_CLNT_HELLO_A:
  202. case SSL23_ST_CW_CLNT_HELLO_B:
  203. s->shutdown = 0;
  204. ret = ssl23_client_hello(s);
  205. if (ret <= 0)
  206. goto end;
  207. s->state = SSL23_ST_CR_SRVR_HELLO_A;
  208. s->init_num = 0;
  209. break;
  210. case SSL23_ST_CR_SRVR_HELLO_A:
  211. case SSL23_ST_CR_SRVR_HELLO_B:
  212. ret = ssl23_get_server_hello(s);
  213. if (ret >= 0)
  214. cb = NULL;
  215. goto end;
  216. /* break; */
  217. default:
  218. SSLerr(SSL_F_SSL23_CONNECT, SSL_R_UNKNOWN_STATE);
  219. ret = -1;
  220. goto end;
  221. /* break; */
  222. }
  223. if (s->debug) {
  224. (void)BIO_flush(s->wbio);
  225. }
  226. if ((cb != NULL) && (s->state != state)) {
  227. new_state = s->state;
  228. s->state = state;
  229. cb(s, SSL_CB_CONNECT_LOOP, 1);
  230. s->state = new_state;
  231. }
  232. }
  233. end:
  234. s->in_handshake--;
  235. if (buf != NULL)
  236. BUF_MEM_free(buf);
  237. if (cb != NULL)
  238. cb(s, SSL_CB_CONNECT_EXIT, ret);
  239. return (ret);
  240. }
  241. static int ssl23_no_ssl2_ciphers(SSL *s)
  242. {
  243. SSL_CIPHER *cipher;
  244. STACK_OF(SSL_CIPHER) *ciphers;
  245. int i;
  246. ciphers = SSL_get_ciphers(s);
  247. for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  248. cipher = sk_SSL_CIPHER_value(ciphers, i);
  249. if (cipher->algorithm_ssl == SSL_SSLV2)
  250. return 0;
  251. }
  252. return 1;
  253. }
  254. /*
  255. * Fill a ClientRandom or ServerRandom field of length len. Returns <= 0 on
  256. * failure, 1 on success.
  257. */
  258. int ssl_fill_hello_random(SSL *s, int server, unsigned char *result, int len)
  259. {
  260. int send_time = 0;
  261. if (len < 4)
  262. return 0;
  263. if (server)
  264. send_time = (s->mode & SSL_MODE_SEND_SERVERHELLO_TIME) != 0;
  265. else
  266. send_time = (s->mode & SSL_MODE_SEND_CLIENTHELLO_TIME) != 0;
  267. if (send_time) {
  268. unsigned long Time = (unsigned long)time(NULL);
  269. unsigned char *p = result;
  270. l2n(Time, p);
  271. return RAND_bytes(p, len - 4);
  272. } else
  273. return RAND_bytes(result, len);
  274. }
  275. static int ssl23_client_hello(SSL *s)
  276. {
  277. unsigned char *buf;
  278. unsigned char *p, *d;
  279. int i, ch_len;
  280. unsigned long l;
  281. int ssl2_compat;
  282. int version = 0, version_major, version_minor;
  283. int al = 0;
  284. #ifndef OPENSSL_NO_COMP
  285. int j;
  286. SSL_COMP *comp;
  287. #endif
  288. int ret;
  289. unsigned long mask, options = s->options;
  290. ssl2_compat = (options & SSL_OP_NO_SSLv2) ? 0 : 1;
  291. if (ssl2_compat && ssl23_no_ssl2_ciphers(s))
  292. ssl2_compat = 0;
  293. /*
  294. * SSL_OP_NO_X disables all protocols above X *if* there are
  295. * some protocols below X enabled. This is required in order
  296. * to maintain "version capability" vector contiguous. So
  297. * that if application wants to disable TLS1.0 in favour of
  298. * TLS1>=1, it would be insufficient to pass SSL_NO_TLSv1, the
  299. * answer is SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3|SSL_OP_NO_SSLv2.
  300. */
  301. mask = SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1
  302. #if !defined(OPENSSL_NO_SSL3)
  303. | SSL_OP_NO_SSLv3
  304. #endif
  305. #if !defined(OPENSSL_NO_SSL2)
  306. | (ssl2_compat ? SSL_OP_NO_SSLv2 : 0)
  307. #endif
  308. ;
  309. #if !defined(OPENSSL_NO_TLS1_2_CLIENT)
  310. version = TLS1_2_VERSION;
  311. if ((options & SSL_OP_NO_TLSv1_2) && (options & mask) != mask)
  312. version = TLS1_1_VERSION;
  313. #else
  314. version = TLS1_1_VERSION;
  315. #endif
  316. mask &= ~SSL_OP_NO_TLSv1_1;
  317. if ((options & SSL_OP_NO_TLSv1_1) && (options & mask) != mask)
  318. version = TLS1_VERSION;
  319. mask &= ~SSL_OP_NO_TLSv1;
  320. #if !defined(OPENSSL_NO_SSL3)
  321. if ((options & SSL_OP_NO_TLSv1) && (options & mask) != mask)
  322. version = SSL3_VERSION;
  323. mask &= ~SSL_OP_NO_SSLv3;
  324. #endif
  325. #if !defined(OPENSSL_NO_SSL2)
  326. if ((options & SSL_OP_NO_SSLv3) && (options & mask) != mask)
  327. version = SSL2_VERSION;
  328. #endif
  329. #ifndef OPENSSL_NO_TLSEXT
  330. if (version != SSL2_VERSION) {
  331. /*
  332. * have to disable SSL 2.0 compatibility if we need TLS extensions
  333. */
  334. if (s->tlsext_hostname != NULL)
  335. ssl2_compat = 0;
  336. if (s->tlsext_status_type != -1)
  337. ssl2_compat = 0;
  338. # ifdef TLSEXT_TYPE_opaque_prf_input
  339. if (s->ctx->tlsext_opaque_prf_input_callback != 0
  340. || s->tlsext_opaque_prf_input != NULL)
  341. ssl2_compat = 0;
  342. # endif
  343. if (s->cert->cli_ext.meths_count != 0)
  344. ssl2_compat = 0;
  345. }
  346. #endif
  347. buf = (unsigned char *)s->init_buf->data;
  348. if (s->state == SSL23_ST_CW_CLNT_HELLO_A) {
  349. /*
  350. * Since we're sending s23 client hello, we're not reusing a session, as
  351. * we'd be using the method from the saved session instead
  352. */
  353. if (!ssl_get_new_session(s, 0)) {
  354. return -1;
  355. }
  356. p = s->s3->client_random;
  357. if (ssl_fill_hello_random(s, 0, p, SSL3_RANDOM_SIZE) <= 0)
  358. return -1;
  359. if (version == TLS1_2_VERSION) {
  360. version_major = TLS1_2_VERSION_MAJOR;
  361. version_minor = TLS1_2_VERSION_MINOR;
  362. } else if (tls1_suiteb(s)) {
  363. SSLerr(SSL_F_SSL23_CLIENT_HELLO,
  364. SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
  365. return -1;
  366. } else if (version == TLS1_1_VERSION) {
  367. version_major = TLS1_1_VERSION_MAJOR;
  368. version_minor = TLS1_1_VERSION_MINOR;
  369. } else if (version == TLS1_VERSION) {
  370. version_major = TLS1_VERSION_MAJOR;
  371. version_minor = TLS1_VERSION_MINOR;
  372. }
  373. #ifdef OPENSSL_FIPS
  374. else if (FIPS_mode()) {
  375. SSLerr(SSL_F_SSL23_CLIENT_HELLO,
  376. SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
  377. return -1;
  378. }
  379. #endif
  380. else if (version == SSL3_VERSION) {
  381. version_major = SSL3_VERSION_MAJOR;
  382. version_minor = SSL3_VERSION_MINOR;
  383. } else if (version == SSL2_VERSION) {
  384. version_major = SSL2_VERSION_MAJOR;
  385. version_minor = SSL2_VERSION_MINOR;
  386. } else {
  387. SSLerr(SSL_F_SSL23_CLIENT_HELLO, SSL_R_NO_PROTOCOLS_AVAILABLE);
  388. return (-1);
  389. }
  390. s->client_version = version;
  391. if (ssl2_compat) {
  392. /* create SSL 2.0 compatible Client Hello */
  393. /* two byte record header will be written last */
  394. d = &(buf[2]);
  395. p = d + 9; /* leave space for message type, version,
  396. * individual length fields */
  397. *(d++) = SSL2_MT_CLIENT_HELLO;
  398. *(d++) = version_major;
  399. *(d++) = version_minor;
  400. /* Ciphers supported */
  401. i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), p, 0);
  402. if (i == 0) {
  403. /* no ciphers */
  404. SSLerr(SSL_F_SSL23_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
  405. return -1;
  406. }
  407. s2n(i, d);
  408. p += i;
  409. /*
  410. * put in the session-id length (zero since there is no reuse)
  411. */
  412. s2n(0, d);
  413. if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
  414. ch_len = SSL2_CHALLENGE_LENGTH;
  415. else
  416. ch_len = SSL2_MAX_CHALLENGE_LENGTH;
  417. /* write out sslv2 challenge */
  418. /*
  419. * Note that ch_len must be <= SSL3_RANDOM_SIZE (32), because it
  420. * is one of SSL2_MAX_CHALLENGE_LENGTH (32) or
  421. * SSL2_MAX_CHALLENGE_LENGTH (16), but leave the check in for
  422. * futurproofing
  423. */
  424. if (SSL3_RANDOM_SIZE < ch_len)
  425. i = SSL3_RANDOM_SIZE;
  426. else
  427. i = ch_len;
  428. s2n(i, d);
  429. memset(&(s->s3->client_random[0]), 0, SSL3_RANDOM_SIZE);
  430. if (RAND_bytes (&(s->s3->client_random[SSL3_RANDOM_SIZE - i]), i)
  431. <= 0)
  432. return -1;
  433. memcpy(p, &(s->s3->client_random[SSL3_RANDOM_SIZE - i]), i);
  434. p += i;
  435. i = p - &(buf[2]);
  436. buf[0] = ((i >> 8) & 0xff) | 0x80;
  437. buf[1] = (i & 0xff);
  438. /* number of bytes to write */
  439. s->init_num = i + 2;
  440. s->init_off = 0;
  441. ssl3_finish_mac(s, &(buf[2]), i);
  442. } else {
  443. /* create Client Hello in SSL 3.0/TLS 1.0 format */
  444. /*
  445. * do the record header (5 bytes) and handshake message header (4
  446. * bytes) last
  447. */
  448. d = p = &(buf[9]);
  449. *(p++) = version_major;
  450. *(p++) = version_minor;
  451. /* Random stuff */
  452. memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
  453. p += SSL3_RANDOM_SIZE;
  454. /* Session ID (zero since there is no reuse) */
  455. *(p++) = 0;
  456. /* Ciphers supported (using SSL 3.0/TLS 1.0 format) */
  457. i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]),
  458. ssl3_put_cipher_by_char);
  459. if (i == 0) {
  460. SSLerr(SSL_F_SSL23_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
  461. return -1;
  462. }
  463. #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
  464. /*
  465. * Some servers hang if client hello > 256 bytes as hack
  466. * workaround chop number of supported ciphers to keep it well
  467. * below this if we use TLS v1.2
  468. */
  469. if (TLS1_get_version(s) >= TLS1_2_VERSION
  470. && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
  471. i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
  472. #endif
  473. s2n(i, p);
  474. p += i;
  475. /* COMPRESSION */
  476. #ifdef OPENSSL_NO_COMP
  477. *(p++) = 1;
  478. #else
  479. if ((s->options & SSL_OP_NO_COMPRESSION)
  480. || !s->ctx->comp_methods)
  481. j = 0;
  482. else
  483. j = sk_SSL_COMP_num(s->ctx->comp_methods);
  484. *(p++) = 1 + j;
  485. for (i = 0; i < j; i++) {
  486. comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
  487. *(p++) = comp->id;
  488. }
  489. #endif
  490. *(p++) = 0; /* Add the NULL method */
  491. #ifndef OPENSSL_NO_TLSEXT
  492. /* TLS extensions */
  493. if (ssl_prepare_clienthello_tlsext(s) <= 0) {
  494. SSLerr(SSL_F_SSL23_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
  495. return -1;
  496. }
  497. if ((p =
  498. ssl_add_clienthello_tlsext(s, p,
  499. buf + SSL3_RT_MAX_PLAIN_LENGTH,
  500. &al)) == NULL) {
  501. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  502. SSLerr(SSL_F_SSL23_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  503. return -1;
  504. }
  505. #endif
  506. l = p - d;
  507. /* fill in 4-byte handshake header */
  508. d = &(buf[5]);
  509. *(d++) = SSL3_MT_CLIENT_HELLO;
  510. l2n3(l, d);
  511. l += 4;
  512. if (l > SSL3_RT_MAX_PLAIN_LENGTH) {
  513. SSLerr(SSL_F_SSL23_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  514. return -1;
  515. }
  516. /* fill in 5-byte record header */
  517. d = buf;
  518. *(d++) = SSL3_RT_HANDSHAKE;
  519. *(d++) = version_major;
  520. /*
  521. * Some servers hang if we use long client hellos and a record
  522. * number > TLS 1.0.
  523. */
  524. if (TLS1_get_client_version(s) > TLS1_VERSION)
  525. *(d++) = 1;
  526. else
  527. *(d++) = version_minor;
  528. s2n((int)l, d);
  529. /* number of bytes to write */
  530. s->init_num = p - buf;
  531. s->init_off = 0;
  532. ssl3_finish_mac(s, &(buf[5]), s->init_num - 5);
  533. }
  534. s->state = SSL23_ST_CW_CLNT_HELLO_B;
  535. s->init_off = 0;
  536. }
  537. /* SSL3_ST_CW_CLNT_HELLO_B */
  538. ret = ssl23_write_bytes(s);
  539. if ((ret >= 2) && s->msg_callback) {
  540. /* Client Hello has been sent; tell msg_callback */
  541. if (ssl2_compat)
  542. s->msg_callback(1, SSL2_VERSION, 0, s->init_buf->data + 2,
  543. ret - 2, s, s->msg_callback_arg);
  544. else {
  545. s->msg_callback(1, version, SSL3_RT_HEADER, s->init_buf->data, 5,
  546. s, s->msg_callback_arg);
  547. s->msg_callback(1, version, SSL3_RT_HANDSHAKE,
  548. s->init_buf->data + 5, ret - 5, s,
  549. s->msg_callback_arg);
  550. }
  551. }
  552. return ret;
  553. }
  554. static int ssl23_get_server_hello(SSL *s)
  555. {
  556. char buf[8];
  557. unsigned char *p;
  558. int i;
  559. int n;
  560. n = ssl23_read_bytes(s, 7);
  561. if (n != 7)
  562. return (n);
  563. p = s->packet;
  564. memcpy(buf, p, n);
  565. if ((p[0] & 0x80) && (p[2] == SSL2_MT_SERVER_HELLO) &&
  566. (p[5] == 0x00) && (p[6] == 0x02)) {
  567. #ifdef OPENSSL_NO_SSL2
  568. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
  569. goto err;
  570. #else
  571. /* we are talking sslv2 */
  572. /*
  573. * we need to clean up the SSLv3 setup and put in the sslv2 stuff.
  574. */
  575. int ch_len;
  576. if (s->options & SSL_OP_NO_SSLv2) {
  577. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
  578. goto err;
  579. }
  580. if (s->s2 == NULL) {
  581. if (!ssl2_new(s))
  582. goto err;
  583. } else
  584. ssl2_clear(s);
  585. if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
  586. ch_len = SSL2_CHALLENGE_LENGTH;
  587. else
  588. ch_len = SSL2_MAX_CHALLENGE_LENGTH;
  589. /* write out sslv2 challenge */
  590. /*
  591. * Note that ch_len must be <= SSL3_RANDOM_SIZE (32), because it is
  592. * one of SSL2_MAX_CHALLENGE_LENGTH (32) or SSL2_MAX_CHALLENGE_LENGTH
  593. * (16), but leave the check in for futurproofing
  594. */
  595. i = (SSL3_RANDOM_SIZE < ch_len)
  596. ? SSL3_RANDOM_SIZE : ch_len;
  597. s->s2->challenge_length = i;
  598. memcpy(s->s2->challenge,
  599. &(s->s3->client_random[SSL3_RANDOM_SIZE - i]), i);
  600. if (s->s3 != NULL)
  601. ssl3_free(s);
  602. if (!BUF_MEM_grow_clean(s->init_buf,
  603. SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)) {
  604. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO, ERR_R_BUF_LIB);
  605. goto err;
  606. }
  607. s->state = SSL2_ST_GET_SERVER_HELLO_A;
  608. if (!(s->client_version == SSL2_VERSION))
  609. /*
  610. * use special padding (SSL 3.0 draft/RFC 2246, App. E.2)
  611. */
  612. s->s2->ssl2_rollback = 1;
  613. /*
  614. * setup the 7 bytes we have read so we get them from the sslv2
  615. * buffer
  616. */
  617. s->rstate = SSL_ST_READ_HEADER;
  618. s->packet_length = n;
  619. s->packet = &(s->s2->rbuf[0]);
  620. memcpy(s->packet, buf, n);
  621. s->s2->rbuf_left = n;
  622. s->s2->rbuf_offs = 0;
  623. /* we have already written one */
  624. s->s2->write_sequence = 1;
  625. s->method = SSLv2_client_method();
  626. s->handshake_func = s->method->ssl_connect;
  627. #endif
  628. } else if (p[1] == SSL3_VERSION_MAJOR &&
  629. p[2] <= TLS1_2_VERSION_MINOR &&
  630. ((p[0] == SSL3_RT_HANDSHAKE && p[5] == SSL3_MT_SERVER_HELLO) ||
  631. (p[0] == SSL3_RT_ALERT && p[3] == 0 && p[4] == 2))) {
  632. /* we have sslv3 or tls1 (server hello or alert) */
  633. #ifndef OPENSSL_NO_SSL3
  634. if ((p[2] == SSL3_VERSION_MINOR) && !(s->options & SSL_OP_NO_SSLv3)) {
  635. # ifdef OPENSSL_FIPS
  636. if (FIPS_mode()) {
  637. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,
  638. SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
  639. goto err;
  640. }
  641. # endif
  642. s->version = SSL3_VERSION;
  643. s->method = SSLv3_client_method();
  644. } else
  645. #endif
  646. if ((p[2] == TLS1_VERSION_MINOR) && !(s->options & SSL_OP_NO_TLSv1)) {
  647. s->version = TLS1_VERSION;
  648. s->method = TLSv1_client_method();
  649. } else if ((p[2] == TLS1_1_VERSION_MINOR) &&
  650. !(s->options & SSL_OP_NO_TLSv1_1)) {
  651. s->version = TLS1_1_VERSION;
  652. s->method = TLSv1_1_client_method();
  653. } else if ((p[2] == TLS1_2_VERSION_MINOR) &&
  654. !(s->options & SSL_OP_NO_TLSv1_2)) {
  655. s->version = TLS1_2_VERSION;
  656. s->method = TLSv1_2_client_method();
  657. } else {
  658. /*
  659. * Unrecognised version, we'll send a protocol version alert using
  660. * our preferred version.
  661. */
  662. switch(s->client_version) {
  663. default:
  664. /*
  665. * Shouldn't happen
  666. * Fall through
  667. */
  668. case TLS1_2_VERSION:
  669. s->version = TLS1_2_VERSION;
  670. s->method = TLSv1_2_client_method();
  671. break;
  672. case TLS1_1_VERSION:
  673. s->version = TLS1_1_VERSION;
  674. s->method = TLSv1_1_client_method();
  675. break;
  676. case TLS1_VERSION:
  677. s->version = TLS1_VERSION;
  678. s->method = TLSv1_client_method();
  679. break;
  680. case SSL3_VERSION:
  681. s->version = SSL3_VERSION;
  682. s->method = SSLv3_client_method();
  683. break;
  684. }
  685. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
  686. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
  687. goto err;
  688. }
  689. s->session->ssl_version = s->version;
  690. /* ensure that TLS_MAX_VERSION is up-to-date */
  691. OPENSSL_assert(s->version <= TLS_MAX_VERSION);
  692. if (p[0] == SSL3_RT_ALERT && p[5] != SSL3_AL_WARNING) {
  693. /* fatal alert */
  694. void (*cb) (const SSL *ssl, int type, int val) = NULL;
  695. int j;
  696. if (s->info_callback != NULL)
  697. cb = s->info_callback;
  698. else if (s->ctx->info_callback != NULL)
  699. cb = s->ctx->info_callback;
  700. i = p[5];
  701. if (cb != NULL) {
  702. j = (i << 8) | p[6];
  703. cb(s, SSL_CB_READ_ALERT, j);
  704. }
  705. if (s->msg_callback) {
  706. s->msg_callback(0, s->version, SSL3_RT_HEADER, p, 5, s,
  707. s->msg_callback_arg);
  708. s->msg_callback(0, s->version, SSL3_RT_ALERT, p + 5, 2, s,
  709. s->msg_callback_arg);
  710. }
  711. s->rwstate = SSL_NOTHING;
  712. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO, SSL_AD_REASON_OFFSET + p[6]);
  713. goto err;
  714. }
  715. if (!ssl_init_wbio_buffer(s, 1))
  716. goto err;
  717. /* we are in this state */
  718. s->state = SSL3_ST_CR_SRVR_HELLO_A;
  719. /*
  720. * put the 7 bytes we have read into the input buffer for SSLv3
  721. */
  722. s->rstate = SSL_ST_READ_HEADER;
  723. s->packet_length = n;
  724. if (s->s3->rbuf.buf == NULL)
  725. if (!ssl3_setup_read_buffer(s))
  726. goto err;
  727. s->packet = &(s->s3->rbuf.buf[0]);
  728. memcpy(s->packet, buf, n);
  729. s->s3->rbuf.left = n;
  730. s->s3->rbuf.offset = 0;
  731. s->handshake_func = s->method->ssl_connect;
  732. } else {
  733. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO, SSL_R_UNKNOWN_PROTOCOL);
  734. goto err;
  735. }
  736. s->init_num = 0;
  737. return (SSL_connect(s));
  738. err:
  739. return (-1);
  740. }