s23_clnt.c 26 KB

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