Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

582 lignes
17 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com). */
  108. #include <stdio.h>
  109. #include <openssl/buf.h>
  110. #include <openssl/err.h>
  111. #include <openssl/evp.h>
  112. #include <openssl/obj.h>
  113. #include <openssl/rand.h>
  114. #include "ssl_locl.h"
  115. static const SSL_METHOD *ssl23_get_client_method(int ver);
  116. static int ssl23_client_hello(SSL *s);
  117. static int ssl23_get_server_hello(SSL *s);
  118. static const SSL_METHOD *ssl23_get_client_method(int ver)
  119. {
  120. /* When SSL_set_session is called, do NOT switch to the version-specific
  121. * method table. The server may still negotiate a different version when
  122. * rejecting the session.
  123. *
  124. * TODO(davidben): Clean this up. This duplicates logic from the
  125. * version-specific tables. https://crbug.com/403378 */
  126. return SSLv23_client_method();
  127. }
  128. IMPLEMENT_ssl23_meth_func(SSLv23_client_method,
  129. ssl_undefined_function,
  130. ssl23_connect,
  131. ssl23_get_client_method)
  132. int ssl23_connect(SSL *s)
  133. {
  134. BUF_MEM *buf=NULL;
  135. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  136. int ret= -1;
  137. int new_state,state;
  138. ERR_clear_error();
  139. ERR_clear_system_error();
  140. if (s->info_callback != NULL)
  141. cb=s->info_callback;
  142. else if (s->ctx->info_callback != NULL)
  143. cb=s->ctx->info_callback;
  144. s->in_handshake++;
  145. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  146. for (;;)
  147. {
  148. state=s->state;
  149. switch(s->state)
  150. {
  151. case SSL_ST_BEFORE:
  152. case SSL_ST_CONNECT:
  153. case SSL_ST_BEFORE|SSL_ST_CONNECT:
  154. case SSL_ST_OK|SSL_ST_CONNECT:
  155. s->server=0;
  156. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  157. /* s->version=TLS1_VERSION; */
  158. s->type=SSL_ST_CONNECT;
  159. if (s->init_buf == NULL)
  160. {
  161. if ((buf=BUF_MEM_new()) == NULL)
  162. {
  163. ret= -1;
  164. goto end;
  165. }
  166. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  167. {
  168. ret= -1;
  169. goto end;
  170. }
  171. s->init_buf=buf;
  172. buf=NULL;
  173. }
  174. if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
  175. ssl3_init_finished_mac(s);
  176. s->state=SSL23_ST_CW_CLNT_HELLO_A;
  177. s->ctx->stats.sess_connect++;
  178. s->init_num=0;
  179. break;
  180. case SSL23_ST_CW_CLNT_HELLO_A:
  181. case SSL23_ST_CW_CLNT_HELLO_B:
  182. s->shutdown=0;
  183. ret=ssl23_client_hello(s);
  184. if (ret <= 0) goto end;
  185. s->state=SSL23_ST_CR_SRVR_HELLO_A;
  186. s->init_num=0;
  187. break;
  188. case SSL23_ST_CR_SRVR_HELLO_A:
  189. case SSL23_ST_CR_SRVR_HELLO_B:
  190. ret=ssl23_get_server_hello(s);
  191. if (ret >= 0) cb=NULL;
  192. goto end;
  193. /* break; */
  194. default:
  195. OPENSSL_PUT_ERROR(SSL, ssl23_connect, SSL_R_UNKNOWN_STATE);
  196. ret= -1;
  197. goto end;
  198. /* break; */
  199. }
  200. if (s->debug) { (void)BIO_flush(s->wbio); }
  201. if ((cb != NULL) && (s->state != state))
  202. {
  203. new_state=s->state;
  204. s->state=state;
  205. cb(s,SSL_CB_CONNECT_LOOP,1);
  206. s->state=new_state;
  207. }
  208. }
  209. end:
  210. s->in_handshake--;
  211. if (buf != NULL)
  212. BUF_MEM_free(buf);
  213. if (cb != NULL)
  214. cb(s,SSL_CB_CONNECT_EXIT,ret);
  215. return(ret);
  216. }
  217. /* Fill a ClientRandom or ServerRandom field of length len. Returns <= 0
  218. * on failure, 1 on success. */
  219. int ssl_fill_hello_random(SSL *s, int server, unsigned char *result, int len)
  220. {
  221. int send_time = 0;
  222. if (len < 4)
  223. return 0;
  224. if (server)
  225. send_time = (s->mode & SSL_MODE_SEND_SERVERHELLO_TIME) != 0;
  226. else
  227. send_time = (s->mode & SSL_MODE_SEND_CLIENTHELLO_TIME) != 0;
  228. if (send_time)
  229. {
  230. unsigned long Time = (unsigned long)time(NULL);
  231. unsigned char *p = result;
  232. l2n(Time, p);
  233. return RAND_pseudo_bytes(p, len-4);
  234. }
  235. else
  236. return RAND_pseudo_bytes(result, len);
  237. }
  238. static int ssl23_client_hello(SSL *s)
  239. {
  240. unsigned char *buf;
  241. unsigned char *p,*d;
  242. int i;
  243. unsigned long l;
  244. int version = 0, version_major, version_minor;
  245. int ret;
  246. unsigned long mask, options = s->options;
  247. /*
  248. * SSL_OP_NO_X disables all protocols above X *if* there are
  249. * some protocols below X enabled. This is required in order
  250. * to maintain "version capability" vector contiguous. So
  251. * that if application wants to disable TLS1.0 in favour of
  252. * TLS1>=1, it would be insufficient to pass SSL_NO_TLSv1, the
  253. * answer is SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3|SSL_OP_NO_SSLv2.
  254. */
  255. mask = SSL_OP_NO_TLSv1_1|SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3;
  256. version = TLS1_2_VERSION;
  257. if ((options & SSL_OP_NO_TLSv1_2) && (options & mask) != mask)
  258. version = TLS1_1_VERSION;
  259. mask &= ~SSL_OP_NO_TLSv1_1;
  260. if ((options & SSL_OP_NO_TLSv1_1) && (options & mask) != mask)
  261. version = TLS1_VERSION;
  262. mask &= ~SSL_OP_NO_TLSv1;
  263. if ((options & SSL_OP_NO_TLSv1) && (options & mask) != mask)
  264. version = SSL3_VERSION;
  265. mask &= ~SSL_OP_NO_SSLv3;
  266. buf=(unsigned char *)s->init_buf->data;
  267. if (s->state == SSL23_ST_CW_CLNT_HELLO_A)
  268. {
  269. /* Check if the session is resumable. If not, drop it. */
  270. if (s->session != NULL)
  271. {
  272. if (s->session->ssl_version > version ||
  273. s->session->session_id_length == 0 ||
  274. s->session->not_resumable)
  275. {
  276. SSL_SESSION_free(s->session);
  277. s->session = NULL;
  278. }
  279. }
  280. p=s->s3->client_random;
  281. if (ssl_fill_hello_random(s, 0, p, SSL3_RANDOM_SIZE) <= 0)
  282. return -1;
  283. if (version == TLS1_2_VERSION)
  284. {
  285. version_major = TLS1_2_VERSION_MAJOR;
  286. version_minor = TLS1_2_VERSION_MINOR;
  287. }
  288. else if (version == TLS1_1_VERSION)
  289. {
  290. version_major = TLS1_1_VERSION_MAJOR;
  291. version_minor = TLS1_1_VERSION_MINOR;
  292. }
  293. else if (version == TLS1_VERSION)
  294. {
  295. version_major = TLS1_VERSION_MAJOR;
  296. version_minor = TLS1_VERSION_MINOR;
  297. }
  298. else if (version == SSL3_VERSION)
  299. {
  300. version_major = SSL3_VERSION_MAJOR;
  301. version_minor = SSL3_VERSION_MINOR;
  302. }
  303. else if (version == SSL2_VERSION)
  304. {
  305. version_major = SSL2_VERSION_MAJOR;
  306. version_minor = SSL2_VERSION_MINOR;
  307. }
  308. else
  309. {
  310. OPENSSL_PUT_ERROR(SSL, ssl23_client_hello, SSL_R_NO_PROTOCOLS_AVAILABLE);
  311. return(-1);
  312. }
  313. s->client_version = version;
  314. /* create Client Hello in SSL 3.0/TLS 1.0 format */
  315. /* do the record header (5 bytes) and handshake message
  316. * header (4 bytes) last. Note: the final argument to
  317. * ssl_add_clienthello_tlsext below depends on the size
  318. * of this prefix. */
  319. d = p = &(buf[9]);
  320. *(p++) = version_major;
  321. *(p++) = version_minor;
  322. /* Random stuff */
  323. memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
  324. p += SSL3_RANDOM_SIZE;
  325. /* Session ID */
  326. if (s->new_session || s->session == NULL)
  327. i=0;
  328. else
  329. i=s->session->session_id_length;
  330. *(p++)=i;
  331. if (i != 0)
  332. {
  333. if (i > (int)sizeof(s->session->session_id))
  334. {
  335. OPENSSL_PUT_ERROR(SSL, ssl23_client_hello, ERR_R_INTERNAL_ERROR);
  336. return -1;
  337. }
  338. memcpy(p,s->session->session_id,i);
  339. p+=i;
  340. }
  341. /* Ciphers supported (using SSL 3.0/TLS 1.0 format) */
  342. i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
  343. if (i == 0)
  344. {
  345. OPENSSL_PUT_ERROR(SSL, ssl23_client_hello, SSL_R_NO_CIPHERS_AVAILABLE);
  346. return -1;
  347. }
  348. s2n(i,p);
  349. p+=i;
  350. /* COMPRESSION */
  351. *(p++)=1;
  352. *(p++)=0; /* Add the NULL method */
  353. /* TLS extensions*/
  354. if (ssl_prepare_clienthello_tlsext(s) <= 0)
  355. {
  356. OPENSSL_PUT_ERROR(SSL, ssl23_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
  357. return -1;
  358. }
  359. /* The buffer includes the 5 byte record header, so
  360. * subtract it to compute hlen for
  361. * ssl_add_clienthello_tlsext. */
  362. if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH, p-buf-5)) == NULL)
  363. {
  364. OPENSSL_PUT_ERROR(SSL, ssl23_client_hello, ERR_R_INTERNAL_ERROR);
  365. return -1;
  366. }
  367. l = p-d;
  368. /* fill in 4-byte handshake header */
  369. d=&(buf[5]);
  370. *(d++)=SSL3_MT_CLIENT_HELLO;
  371. l2n3(l,d);
  372. l += 4;
  373. if (l > SSL3_RT_MAX_PLAIN_LENGTH)
  374. {
  375. OPENSSL_PUT_ERROR(SSL, ssl23_client_hello, ERR_R_INTERNAL_ERROR);
  376. return -1;
  377. }
  378. /* fill in 5-byte record header */
  379. d=buf;
  380. *(d++) = SSL3_RT_HANDSHAKE;
  381. *(d++) = version_major;
  382. /* Some servers hang if we use long client hellos
  383. * and a record number > TLS 1.0.
  384. */
  385. if (TLS1_get_client_version(s) > TLS1_VERSION)
  386. *(d++) = 1;
  387. else
  388. *(d++) = version_minor;
  389. s2n((int)l,d);
  390. /* number of bytes to write */
  391. s->init_num=p-buf;
  392. s->init_off=0;
  393. ssl3_finish_mac(s,&(buf[5]), s->init_num - 5);
  394. s->state=SSL23_ST_CW_CLNT_HELLO_B;
  395. s->init_off=0;
  396. }
  397. /* SSL3_ST_CW_CLNT_HELLO_B */
  398. ret = ssl23_write_bytes(s);
  399. if ((ret >= 2) && s->msg_callback)
  400. {
  401. /* Client Hello has been sent; tell msg_callback */
  402. s->msg_callback(1, version, SSL3_RT_HEADER, s->init_buf->data, 5, s, s->msg_callback_arg);
  403. s->msg_callback(1, version, SSL3_RT_HANDSHAKE, s->init_buf->data+5, ret-5, s, s->msg_callback_arg);
  404. }
  405. return ret;
  406. }
  407. static int ssl23_get_server_hello(SSL *s)
  408. {
  409. char buf[8];
  410. unsigned char *p;
  411. int i;
  412. int n;
  413. n=ssl23_read_bytes(s,7);
  414. if (n != 7) return(n);
  415. p=s->packet;
  416. memcpy(buf,p,n);
  417. if ((p[0] & 0x80) && (p[2] == SSL2_MT_SERVER_HELLO) &&
  418. (p[5] == 0x00) && (p[6] == 0x02))
  419. {
  420. OPENSSL_PUT_ERROR(SSL, ssl23_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL);
  421. goto err;
  422. }
  423. else if (p[1] == SSL3_VERSION_MAJOR &&
  424. p[2] <= TLS1_2_VERSION_MINOR &&
  425. ((p[0] == SSL3_RT_HANDSHAKE && p[5] == SSL3_MT_SERVER_HELLO) ||
  426. (p[0] == SSL3_RT_ALERT && p[3] == 0 && p[4] == 2)))
  427. {
  428. /* we have sslv3 or tls1 (server hello or alert) */
  429. if ((p[2] == SSL3_VERSION_MINOR) &&
  430. !(s->options & SSL_OP_NO_SSLv3))
  431. {
  432. s->version=SSL3_VERSION;
  433. s->method=SSLv3_client_method();
  434. }
  435. else if ((p[2] == TLS1_VERSION_MINOR) &&
  436. !(s->options & SSL_OP_NO_TLSv1))
  437. {
  438. s->version=TLS1_VERSION;
  439. s->method=TLSv1_client_method();
  440. }
  441. else if ((p[2] == TLS1_1_VERSION_MINOR) &&
  442. !(s->options & SSL_OP_NO_TLSv1_1))
  443. {
  444. s->version=TLS1_1_VERSION;
  445. s->method=TLSv1_1_client_method();
  446. }
  447. else if ((p[2] == TLS1_2_VERSION_MINOR) &&
  448. !(s->options & SSL_OP_NO_TLSv1_2))
  449. {
  450. s->version=TLS1_2_VERSION;
  451. s->method=TLSv1_2_client_method();
  452. }
  453. else
  454. {
  455. OPENSSL_PUT_ERROR(SSL, ssl23_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL);
  456. goto err;
  457. }
  458. if (p[0] == SSL3_RT_ALERT && p[5] != SSL3_AL_WARNING)
  459. {
  460. /* fatal alert */
  461. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  462. int j;
  463. if (s->info_callback != NULL)
  464. cb=s->info_callback;
  465. else if (s->ctx->info_callback != NULL)
  466. cb=s->ctx->info_callback;
  467. i=p[5];
  468. if (cb != NULL)
  469. {
  470. j=(i<<8)|p[6];
  471. cb(s,SSL_CB_READ_ALERT,j);
  472. }
  473. if (s->msg_callback)
  474. {
  475. s->msg_callback(0, s->version, SSL3_RT_HEADER, p, 5, s, s->msg_callback_arg);
  476. s->msg_callback(0, s->version, SSL3_RT_ALERT, p+5, 2, s, s->msg_callback_arg);
  477. }
  478. s->rwstate=SSL_NOTHING;
  479. OPENSSL_PUT_ERROR(SSL, ssl23_get_server_hello, SSL_AD_REASON_OFFSET + p[6]);
  480. goto err;
  481. }
  482. if (!ssl_init_wbio_buffer(s,1)) goto err;
  483. /* we are in this state */
  484. s->state=SSL3_ST_CR_SRVR_HELLO_A;
  485. /* put the 7 bytes we have read into the input buffer
  486. * for SSLv3 */
  487. s->rstate=SSL_ST_READ_HEADER;
  488. s->packet_length=n;
  489. if (s->s3->rbuf.buf == NULL)
  490. if (!ssl3_setup_read_buffer(s))
  491. goto err;
  492. s->packet= &(s->s3->rbuf.buf[0]);
  493. memcpy(s->packet,buf,n);
  494. s->s3->rbuf.left=n;
  495. s->s3->rbuf.offset=0;
  496. s->handshake_func=s->method->ssl_connect;
  497. }
  498. else
  499. {
  500. OPENSSL_PUT_ERROR(SSL, ssl23_get_server_hello, SSL_R_UNKNOWN_PROTOCOL);
  501. goto err;
  502. }
  503. s->init_num=0;
  504. /* If there was no session to resume, now that the final version is
  505. * determined, insert a fresh one. */
  506. if (s->session == NULL && !ssl_get_new_session(s,0))
  507. goto err;
  508. return(SSL_connect(s));
  509. err:
  510. return(-1);
  511. }