s23_clnt.c 17 KB

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