s23_clnt.c 21 KB

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