s23_clnt.c 26 KB

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