You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

528 lines
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 <assert.h>
  109. #include <stdio.h>
  110. #include <openssl/buf.h>
  111. #include <openssl/err.h>
  112. #include <openssl/evp.h>
  113. #include <openssl/mem.h>
  114. #include <openssl/obj.h>
  115. #include <openssl/rand.h>
  116. #include "ssl_locl.h"
  117. static int ssl23_get_client_hello(SSL *s);
  118. static int ssl23_get_v2_client_hello(SSL *s);
  119. static const SSL_METHOD *ssl23_get_server_method(int ver)
  120. {
  121. if (ver == SSL3_VERSION)
  122. return(SSLv3_server_method());
  123. else if (ver == TLS1_VERSION)
  124. return(TLSv1_server_method());
  125. else if (ver == TLS1_1_VERSION)
  126. return(TLSv1_1_server_method());
  127. else if (ver == TLS1_2_VERSION)
  128. return(TLSv1_2_server_method());
  129. else
  130. return(NULL);
  131. }
  132. IMPLEMENT_ssl23_meth_func(SSLv23_server_method,
  133. ssl23_accept,
  134. ssl_undefined_function)
  135. int ssl23_accept(SSL *s)
  136. {
  137. BUF_MEM *buf;
  138. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  139. int ret= -1;
  140. int new_state,state;
  141. ERR_clear_error();
  142. ERR_clear_system_error();
  143. if (s->info_callback != NULL)
  144. cb=s->info_callback;
  145. else if (s->ctx->info_callback != NULL)
  146. cb=s->ctx->info_callback;
  147. s->in_handshake++;
  148. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  149. for (;;)
  150. {
  151. state=s->state;
  152. switch(s->state)
  153. {
  154. case SSL_ST_BEFORE:
  155. case SSL_ST_ACCEPT:
  156. case SSL_ST_BEFORE|SSL_ST_ACCEPT:
  157. case SSL_ST_OK|SSL_ST_ACCEPT:
  158. s->server=1;
  159. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  160. /* s->version=SSL3_VERSION; */
  161. s->type=SSL_ST_ACCEPT;
  162. if (s->init_buf == NULL)
  163. {
  164. if ((buf=BUF_MEM_new()) == NULL)
  165. {
  166. ret= -1;
  167. goto end;
  168. }
  169. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  170. {
  171. ret= -1;
  172. goto end;
  173. }
  174. s->init_buf=buf;
  175. }
  176. if (!ssl3_init_finished_mac(s))
  177. {
  178. OPENSSL_PUT_ERROR(SSL, ssl23_accept, ERR_R_INTERNAL_ERROR);
  179. ret = -1;
  180. goto end;
  181. }
  182. s->state=SSL23_ST_SR_CLNT_HELLO;
  183. s->ctx->stats.sess_accept++;
  184. s->init_num=0;
  185. break;
  186. case SSL23_ST_SR_CLNT_HELLO:
  187. s->shutdown = 0;
  188. ret = ssl23_get_client_hello(s);
  189. if (ret <= 0) goto end;
  190. break;
  191. case SSL23_ST_SR_V2_CLNT_HELLO:
  192. ret = ssl23_get_v2_client_hello(s);
  193. if (ret <= 0) goto end;
  194. break;
  195. case SSL23_ST_SR_SWITCH_VERSION:
  196. if (!ssl_init_wbio_buffer(s, 1))
  197. {
  198. ret = -1;
  199. goto end;
  200. }
  201. s->state = SSL3_ST_SR_CLNT_HELLO_A;
  202. s->method = ssl23_get_server_method(s->version);
  203. assert(s->method != NULL);
  204. s->handshake_func = s->method->ssl_accept;
  205. s->init_num = 0;
  206. /* NULL the callback; SSL_accept will call it instead. */
  207. cb = NULL;
  208. ret = SSL_accept(s);
  209. goto end;
  210. /* break; */
  211. default:
  212. OPENSSL_PUT_ERROR(SSL, ssl23_accept, SSL_R_UNKNOWN_STATE);
  213. ret= -1;
  214. goto end;
  215. /* break; */
  216. }
  217. if ((cb != NULL) && (s->state != state))
  218. {
  219. new_state=s->state;
  220. s->state=state;
  221. cb(s,SSL_CB_ACCEPT_LOOP,1);
  222. s->state=new_state;
  223. }
  224. }
  225. end:
  226. s->in_handshake--;
  227. if (cb != NULL)
  228. cb(s,SSL_CB_ACCEPT_EXIT,ret);
  229. return(ret);
  230. }
  231. /* ssl23_get_mutual_version determines the highest supported version for a
  232. * client which reports a highest version of |client_version|. On success, it
  233. * returns 1 and sets |*out_version| to the negotiated version. Otherwise, it
  234. * returns 0. */
  235. static int ssl23_get_mutual_version(SSL *s, int *out_version, uint16_t client_version)
  236. {
  237. if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2))
  238. {
  239. *out_version = TLS1_2_VERSION;
  240. return 1;
  241. }
  242. if (client_version >= TLS1_1_VERSION && !(s->options & SSL_OP_NO_TLSv1_1))
  243. {
  244. *out_version = TLS1_1_VERSION;
  245. return 1;
  246. }
  247. if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1))
  248. {
  249. *out_version = TLS1_VERSION;
  250. return 1;
  251. }
  252. if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3))
  253. {
  254. *out_version = SSL3_VERSION;
  255. return 1;
  256. }
  257. return 0;
  258. }
  259. static int ssl23_get_client_hello(SSL *s)
  260. {
  261. uint8_t *p;
  262. int n = 0;
  263. /* Sniff enough of the input to determine ClientHello type and the
  264. * client version. */
  265. if (!ssl3_setup_buffers(s)) goto err;
  266. /* Read the initial 11 bytes of the input. This is sufficient to
  267. * determine the client version for a ClientHello or a
  268. * V2ClientHello.
  269. *
  270. * ClientHello (assuming client_version is unfragmented):
  271. * Byte Content
  272. * 0 type \
  273. * 1-2 version > record header
  274. * 3-4 length /
  275. * 5 msg_type \
  276. * 6-8 length > Client Hello message
  277. * 9-10 client_version /
  278. *
  279. * V2ClientHello:
  280. * Byte Content
  281. * 0-1 msg_length
  282. * 2 msg_type
  283. * 3-4 version
  284. * 5-6 cipher_spec_length
  285. * 7-8 session_id_length
  286. * 9-10 challenge_length
  287. */
  288. n = ssl23_read_bytes(s, 11);
  289. if (n <= 0)
  290. return n;
  291. assert(n == 11);
  292. p = s->packet;
  293. /* Some dedicated error codes for protocol mixups should the application
  294. * wish to interpret them differently. (These do not overlap with
  295. * ClientHello or V2ClientHello.) */
  296. if ((strncmp("GET ", (char *)p, 4) == 0) ||
  297. (strncmp("POST ",(char *)p, 5) == 0) ||
  298. (strncmp("HEAD ",(char *)p, 5) == 0) ||
  299. (strncmp("PUT ", (char *)p, 4) == 0))
  300. {
  301. OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_HTTP_REQUEST);
  302. goto err;
  303. }
  304. if (strncmp("CONNECT",(char *)p, 7) == 0)
  305. {
  306. OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_HTTPS_PROXY_REQUEST);
  307. goto err;
  308. }
  309. /* Determine if this is a ClientHello or V2ClientHello. */
  310. if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO))
  311. {
  312. /* This is a V2ClientHello. Determine the version to
  313. * use. */
  314. uint16_t client_version = (p[3] << 8) | p[4];
  315. if (!ssl23_get_mutual_version(s, &s->version, client_version))
  316. {
  317. OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_UNSUPPORTED_PROTOCOL);
  318. goto err;
  319. }
  320. /* Parse the entire V2ClientHello. */
  321. s->state = SSL23_ST_SR_V2_CLNT_HELLO;
  322. }
  323. else if ((p[0] == SSL3_RT_HANDSHAKE) &&
  324. (p[1] >= SSL3_VERSION_MAJOR) &&
  325. (p[5] == SSL3_MT_CLIENT_HELLO))
  326. {
  327. /* This is a fragment of a ClientHello. We look at the
  328. * client_hello to negotiate the version. However, this
  329. * is difficult if we have only a pathologically small
  330. * fragment. No known client fragments ClientHello like
  331. * this, so we simply reject such connections to avoid
  332. * protocol version downgrade attacks. */
  333. uint16_t record_length = (p[3] << 8) | p[4];
  334. uint16_t client_version;
  335. if (record_length < 6)
  336. {
  337. OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_RECORD_TOO_SMALL);
  338. goto err;
  339. }
  340. client_version = (p[9] << 8) | p[10];
  341. if (!ssl23_get_mutual_version(s, &s->version, client_version))
  342. {
  343. OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_UNSUPPORTED_PROTOCOL);
  344. goto err;
  345. }
  346. /* Reset the record-layer state for SSL3. */
  347. assert(s->rstate == SSL_ST_READ_HEADER);
  348. s->s3->rbuf.left = s->packet_length;
  349. s->s3->rbuf.offset = 0;
  350. s->packet_length = 0;
  351. /* Ready to switch versions. */
  352. s->state = SSL23_ST_SR_SWITCH_VERSION;
  353. }
  354. return 1;
  355. err:
  356. return -1;
  357. }
  358. static int ssl23_get_v2_client_hello(SSL *s)
  359. {
  360. uint8_t *p;
  361. size_t i;
  362. int n = 0;
  363. CBS v2_client_hello, cipher_specs, session_id, challenge;
  364. size_t msg_length, len;
  365. uint8_t msg_type;
  366. uint16_t version, cipher_spec_length, session_id_length, challenge_length;
  367. CBB client_hello, hello_body, cipher_suites;
  368. uint8_t random[SSL3_RANDOM_SIZE];
  369. /* Read the remainder of the V2ClientHello. We have previously read 11
  370. * bytes in ssl23_get_client_hello. */
  371. p = s->packet;
  372. msg_length = ((p[0] & 0x7f) << 8) | p[1];
  373. if (msg_length > (1024 * 4))
  374. {
  375. OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, SSL_R_RECORD_TOO_LARGE);
  376. goto err;
  377. }
  378. if (msg_length < 11 - 2)
  379. {
  380. /* Reject lengths that are too short early. We have already read
  381. * 11 bytes, so we should not attempt to process an (invalid)
  382. * V2ClientHello which would be shorter than that. */
  383. OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, SSL_R_RECORD_LENGTH_MISMATCH);
  384. goto err;
  385. }
  386. n = ssl23_read_bytes(s, msg_length + 2);
  387. if (n <= 0)
  388. return n;
  389. assert(n == s->packet_length);
  390. /* The V2ClientHello without the length is incorporated into the
  391. * Finished hash. */
  392. ssl3_finish_mac(s, s->packet + 2, s->packet_length - 2);
  393. if (s->msg_callback)
  394. s->msg_callback(0, SSL2_VERSION, 0, s->packet+2, s->packet_length-2, s, s->msg_callback_arg); /* CLIENT-HELLO */
  395. CBS_init(&v2_client_hello, s->packet + 2, s->packet_length - 2);
  396. if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
  397. !CBS_get_u16(&v2_client_hello, &version) ||
  398. !CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
  399. !CBS_get_u16(&v2_client_hello, &session_id_length) ||
  400. !CBS_get_u16(&v2_client_hello, &challenge_length) ||
  401. !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
  402. !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
  403. !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
  404. CBS_len(&v2_client_hello) != 0)
  405. {
  406. OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, SSL_R_DECODE_ERROR);
  407. goto err;
  408. }
  409. /* msg_type has already been checked. */
  410. assert(msg_type == SSL2_MT_CLIENT_HELLO);
  411. /* The client_random is the V2ClientHello challenge. Truncate or
  412. * left-pad with zeros as needed. */
  413. memset(random, 0, SSL3_RANDOM_SIZE);
  414. i = (CBS_len(&challenge) > SSL3_RANDOM_SIZE) ? SSL3_RANDOM_SIZE : CBS_len(&challenge);
  415. memcpy(random, CBS_data(&challenge), i);
  416. /* Write out an equivalent SSLv3 ClientHello. */
  417. if (!CBB_init_fixed(&client_hello, (uint8_t *)s->init_buf->data, s->init_buf->max))
  418. {
  419. OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, ERR_R_MALLOC_FAILURE);
  420. goto err;
  421. }
  422. if (!CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) ||
  423. !CBB_add_u24_length_prefixed(&client_hello, &hello_body) ||
  424. !CBB_add_u16(&hello_body, version) ||
  425. !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
  426. /* No session id. */
  427. !CBB_add_u8(&hello_body, 0) ||
  428. !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites))
  429. {
  430. CBB_cleanup(&client_hello);
  431. OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
  432. goto err;
  433. }
  434. /* Copy the cipher suites. */
  435. while (CBS_len(&cipher_specs) > 0)
  436. {
  437. uint32_t cipher_spec;
  438. if (!CBS_get_u24(&cipher_specs, &cipher_spec))
  439. {
  440. CBB_cleanup(&client_hello);
  441. OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, SSL_R_DECODE_ERROR);
  442. goto err;
  443. }
  444. /* Skip SSLv2 ciphers. */
  445. if ((cipher_spec & 0xff0000) != 0)
  446. continue;
  447. if (!CBB_add_u16(&cipher_suites, cipher_spec))
  448. {
  449. CBB_cleanup(&client_hello);
  450. OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
  451. goto err;
  452. }
  453. }
  454. /* Add the null compression scheme and finish. */
  455. if (!CBB_add_u8(&hello_body, 1) ||
  456. !CBB_add_u8(&hello_body, 0) ||
  457. !CBB_finish(&client_hello, NULL, &len))
  458. {
  459. CBB_cleanup(&client_hello);
  460. OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
  461. goto err;
  462. }
  463. /* Mark the message for "re"-use by the version-specific
  464. * method. */
  465. s->s3->tmp.reuse_message = 1;
  466. s->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO;
  467. /* The handshake message header is 4 bytes. */
  468. s->s3->tmp.message_size = len - 4;
  469. /* Reset the record layer for SSL3. */
  470. assert(s->rstate == SSL_ST_READ_HEADER);
  471. s->packet_length = 0;
  472. s->s3->rbuf.left = 0;
  473. s->s3->rbuf.offset = 0;
  474. s->state = SSL23_ST_SR_SWITCH_VERSION;
  475. return 1;
  476. err:
  477. return -1;
  478. }