2
0

s23_clnt.c 19 KB

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