Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

723 righe
24 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-2002 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. /* ====================================================================
  109. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  110. * ECC cipher suite support in OpenSSL originally developed by
  111. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */
  112. #include <openssl/ssl.h>
  113. #include <assert.h>
  114. #include <limits.h>
  115. #include <string.h>
  116. #include <openssl/buf.h>
  117. #include <openssl/bytestring.h>
  118. #include <openssl/err.h>
  119. #include <openssl/evp.h>
  120. #include <openssl/mem.h>
  121. #include <openssl/md5.h>
  122. #include <openssl/nid.h>
  123. #include <openssl/rand.h>
  124. #include <openssl/sha.h>
  125. #include <openssl/x509.h>
  126. #include "internal.h"
  127. /* ssl3_do_write sends |ssl->init_buf| in records of type 'type'
  128. * (SSL3_RT_HANDSHAKE or SSL3_RT_CHANGE_CIPHER_SPEC). It returns 1 on success
  129. * and <= 0 on error. */
  130. static int ssl3_do_write(SSL *ssl, int type, const uint8_t *data, size_t len) {
  131. int ret = ssl3_write_bytes(ssl, type, data, len);
  132. if (ret <= 0) {
  133. return ret;
  134. }
  135. /* ssl3_write_bytes writes the data in its entirety. */
  136. assert((size_t)ret == len);
  137. ssl_do_msg_callback(ssl, 1 /* write */, ssl->version, type, data, len);
  138. return 1;
  139. }
  140. int ssl3_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type) {
  141. CBB_zero(cbb);
  142. if (ssl->s3->pending_message != NULL) {
  143. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  144. return 0;
  145. }
  146. /* Pick a modest size hint to save most of the |realloc| calls. */
  147. if (!CBB_init(cbb, 64) ||
  148. !CBB_add_u8(cbb, type) ||
  149. !CBB_add_u24_length_prefixed(cbb, body)) {
  150. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  151. return 0;
  152. }
  153. return 1;
  154. }
  155. int ssl3_finish_message(SSL *ssl, CBB *cbb) {
  156. if (ssl->s3->pending_message != NULL) {
  157. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  158. return 0;
  159. }
  160. uint8_t *msg = NULL;
  161. size_t len;
  162. if (!CBB_finish(cbb, &msg, &len) ||
  163. len > 0xffffffffu) {
  164. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  165. OPENSSL_free(msg);
  166. return 0;
  167. }
  168. ssl3_update_handshake_hash(ssl, msg, len);
  169. ssl->s3->pending_message = msg;
  170. ssl->s3->pending_message_len = (uint32_t)len;
  171. return 1;
  172. }
  173. int ssl3_write_message(SSL *ssl) {
  174. if (ssl->s3->pending_message == NULL) {
  175. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  176. return 0;
  177. }
  178. int ret = ssl3_do_write(ssl, SSL3_RT_HANDSHAKE, ssl->s3->pending_message,
  179. ssl->s3->pending_message_len);
  180. if (ret <= 0) {
  181. return ret;
  182. }
  183. OPENSSL_free(ssl->s3->pending_message);
  184. ssl->s3->pending_message = NULL;
  185. ssl->s3->pending_message_len = 0;
  186. return 1;
  187. }
  188. int ssl3_send_finished(SSL *ssl, int a, int b) {
  189. if (ssl->state == b) {
  190. return ssl->method->write_message(ssl);
  191. }
  192. int n = ssl->s3->enc_method->final_finish_mac(ssl, ssl->server,
  193. ssl->s3->tmp.finish_md);
  194. if (n == 0) {
  195. return 0;
  196. }
  197. ssl->s3->tmp.finish_md_len = n;
  198. /* Log the master secret, if logging is enabled. */
  199. if (!ssl_log_secret(ssl, "CLIENT_RANDOM",
  200. SSL_get_session(ssl)->master_key,
  201. SSL_get_session(ssl)->master_key_length)) {
  202. return 0;
  203. }
  204. /* Copy the finished so we can use it for renegotiation checks */
  205. if (ssl->server) {
  206. assert(n <= EVP_MAX_MD_SIZE);
  207. memcpy(ssl->s3->previous_server_finished, ssl->s3->tmp.finish_md, n);
  208. ssl->s3->previous_server_finished_len = n;
  209. } else {
  210. assert(n <= EVP_MAX_MD_SIZE);
  211. memcpy(ssl->s3->previous_client_finished, ssl->s3->tmp.finish_md, n);
  212. ssl->s3->previous_client_finished_len = n;
  213. }
  214. CBB cbb, body;
  215. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_FINISHED) ||
  216. !CBB_add_bytes(&body, ssl->s3->tmp.finish_md,
  217. ssl->s3->tmp.finish_md_len) ||
  218. !ssl->method->finish_message(ssl, &cbb)) {
  219. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  220. CBB_cleanup(&cbb);
  221. return -1;
  222. }
  223. ssl->state = b;
  224. return ssl->method->write_message(ssl);
  225. }
  226. /* ssl3_take_mac calculates the Finished MAC for the handshakes messages seen
  227. * so far. */
  228. static void ssl3_take_mac(SSL *ssl) {
  229. /* If no new cipher setup then return immediately: other functions will set
  230. * the appropriate error. */
  231. if (ssl->s3->tmp.new_cipher == NULL) {
  232. return;
  233. }
  234. ssl->s3->tmp.peer_finish_md_len = ssl->s3->enc_method->final_finish_mac(
  235. ssl, !ssl->server, ssl->s3->tmp.peer_finish_md);
  236. }
  237. int ssl3_get_finished(SSL *ssl) {
  238. int al;
  239. int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_FINISHED,
  240. ssl_dont_hash_message);
  241. if (ret <= 0) {
  242. return ret;
  243. }
  244. /* Snapshot the finished hash before incorporating the new message. */
  245. ssl3_take_mac(ssl);
  246. if (!ssl->method->hash_current_message(ssl)) {
  247. goto err;
  248. }
  249. size_t finished_len = ssl->s3->tmp.peer_finish_md_len;
  250. if (finished_len != ssl->init_num) {
  251. al = SSL_AD_DECODE_ERROR;
  252. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DIGEST_LENGTH);
  253. goto f_err;
  254. }
  255. int finished_ret =
  256. CRYPTO_memcmp(ssl->init_msg, ssl->s3->tmp.peer_finish_md, finished_len);
  257. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  258. finished_ret = 0;
  259. #endif
  260. if (finished_ret != 0) {
  261. al = SSL_AD_DECRYPT_ERROR;
  262. OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED);
  263. goto f_err;
  264. }
  265. /* Copy the finished so we can use it for renegotiation checks */
  266. if (ssl->server) {
  267. assert(finished_len <= EVP_MAX_MD_SIZE);
  268. memcpy(ssl->s3->previous_client_finished, ssl->s3->tmp.peer_finish_md,
  269. finished_len);
  270. ssl->s3->previous_client_finished_len = finished_len;
  271. } else {
  272. assert(finished_len <= EVP_MAX_MD_SIZE);
  273. memcpy(ssl->s3->previous_server_finished, ssl->s3->tmp.peer_finish_md,
  274. finished_len);
  275. ssl->s3->previous_server_finished_len = finished_len;
  276. }
  277. return 1;
  278. f_err:
  279. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  280. err:
  281. return 0;
  282. }
  283. int ssl3_send_change_cipher_spec(SSL *ssl) {
  284. static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS};
  285. return ssl3_do_write(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec,
  286. sizeof(kChangeCipherSpec));
  287. }
  288. int ssl3_output_cert_chain(SSL *ssl) {
  289. CBB cbb, body;
  290. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CERTIFICATE) ||
  291. !ssl_add_cert_chain(ssl, &body) ||
  292. !ssl->method->finish_message(ssl, &cbb)) {
  293. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  294. CBB_cleanup(&cbb);
  295. return 0;
  296. }
  297. return 1;
  298. }
  299. size_t ssl_max_handshake_message_len(const SSL *ssl) {
  300. /* kMaxMessageLen is the default maximum message size for handshakes which do
  301. * not accept peer certificate chains. */
  302. static const size_t kMaxMessageLen = 16384;
  303. if (SSL_in_init(ssl)) {
  304. if ((!ssl->server || (ssl->verify_mode & SSL_VERIFY_PEER)) &&
  305. kMaxMessageLen < ssl->max_cert_list) {
  306. return ssl->max_cert_list;
  307. }
  308. return kMaxMessageLen;
  309. }
  310. if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  311. /* In TLS 1.2 and below, the largest acceptable post-handshake message is
  312. * a HelloRequest. */
  313. return 0;
  314. }
  315. if (ssl->server) {
  316. /* The largest acceptable post-handshake message for a server is a
  317. * KeyUpdate. We will never initiate post-handshake auth. */
  318. return 0;
  319. }
  320. /* Clients must accept NewSessionTicket and CertificateRequest, so allow the
  321. * default size. */
  322. return kMaxMessageLen;
  323. }
  324. static int extend_handshake_buffer(SSL *ssl, size_t length) {
  325. if (!BUF_MEM_reserve(ssl->init_buf, length)) {
  326. return -1;
  327. }
  328. while (ssl->init_buf->length < length) {
  329. int ret = ssl3_read_handshake_bytes(
  330. ssl, (uint8_t *)ssl->init_buf->data + ssl->init_buf->length,
  331. length - ssl->init_buf->length);
  332. if (ret <= 0) {
  333. return ret;
  334. }
  335. ssl->init_buf->length += (size_t)ret;
  336. }
  337. return 1;
  338. }
  339. static int read_v2_client_hello(SSL *ssl, int *out_is_v2_client_hello) {
  340. /* Read the first 5 bytes, the size of the TLS record header. This is
  341. * sufficient to detect a V2ClientHello and ensures that we never read beyond
  342. * the first record. */
  343. int ret = ssl_read_buffer_extend_to(ssl, SSL3_RT_HEADER_LENGTH);
  344. if (ret <= 0) {
  345. return ret;
  346. }
  347. const uint8_t *p = ssl_read_buffer(ssl);
  348. /* Some dedicated error codes for protocol mixups should the application wish
  349. * to interpret them differently. (These do not overlap with ClientHello or
  350. * V2ClientHello.) */
  351. if (strncmp("GET ", (const char *)p, 4) == 0 ||
  352. strncmp("POST ", (const char *)p, 5) == 0 ||
  353. strncmp("HEAD ", (const char *)p, 5) == 0 ||
  354. strncmp("PUT ", (const char *)p, 4) == 0) {
  355. OPENSSL_PUT_ERROR(SSL, SSL_R_HTTP_REQUEST);
  356. return -1;
  357. }
  358. if (strncmp("CONNE", (const char *)p, 5) == 0) {
  359. OPENSSL_PUT_ERROR(SSL, SSL_R_HTTPS_PROXY_REQUEST);
  360. return -1;
  361. }
  362. if ((p[0] & 0x80) == 0 || p[2] != SSL2_MT_CLIENT_HELLO ||
  363. p[3] != SSL3_VERSION_MAJOR) {
  364. /* Not a V2ClientHello. */
  365. *out_is_v2_client_hello = 0;
  366. return 1;
  367. }
  368. /* Determine the length of the V2ClientHello. */
  369. size_t msg_length = ((p[0] & 0x7f) << 8) | p[1];
  370. if (msg_length > (1024 * 4)) {
  371. OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
  372. return -1;
  373. }
  374. if (msg_length < SSL3_RT_HEADER_LENGTH - 2) {
  375. /* Reject lengths that are too short early. We have already read
  376. * |SSL3_RT_HEADER_LENGTH| bytes, so we should not attempt to process an
  377. * (invalid) V2ClientHello which would be shorter than that. */
  378. OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_LENGTH_MISMATCH);
  379. return -1;
  380. }
  381. /* Read the remainder of the V2ClientHello. */
  382. ret = ssl_read_buffer_extend_to(ssl, 2 + msg_length);
  383. if (ret <= 0) {
  384. return ret;
  385. }
  386. CBS v2_client_hello;
  387. CBS_init(&v2_client_hello, ssl_read_buffer(ssl) + 2, msg_length);
  388. /* The V2ClientHello without the length is incorporated into the handshake
  389. * hash. */
  390. if (!ssl3_update_handshake_hash(ssl, CBS_data(&v2_client_hello),
  391. CBS_len(&v2_client_hello))) {
  392. return -1;
  393. }
  394. ssl_do_msg_callback(ssl, 0 /* read */, SSL2_VERSION, 0,
  395. CBS_data(&v2_client_hello), CBS_len(&v2_client_hello));
  396. uint8_t msg_type;
  397. uint16_t version, cipher_spec_length, session_id_length, challenge_length;
  398. CBS cipher_specs, session_id, challenge;
  399. if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
  400. !CBS_get_u16(&v2_client_hello, &version) ||
  401. !CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
  402. !CBS_get_u16(&v2_client_hello, &session_id_length) ||
  403. !CBS_get_u16(&v2_client_hello, &challenge_length) ||
  404. !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
  405. !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
  406. !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
  407. CBS_len(&v2_client_hello) != 0) {
  408. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  409. return -1;
  410. }
  411. /* msg_type has already been checked. */
  412. assert(msg_type == SSL2_MT_CLIENT_HELLO);
  413. /* The client_random is the V2ClientHello challenge. Truncate or
  414. * left-pad with zeros as needed. */
  415. size_t rand_len = CBS_len(&challenge);
  416. if (rand_len > SSL3_RANDOM_SIZE) {
  417. rand_len = SSL3_RANDOM_SIZE;
  418. }
  419. uint8_t random[SSL3_RANDOM_SIZE];
  420. memset(random, 0, SSL3_RANDOM_SIZE);
  421. memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge),
  422. rand_len);
  423. /* Write out an equivalent SSLv3 ClientHello. */
  424. size_t max_v3_client_hello = SSL3_HM_HEADER_LENGTH + 2 /* version */ +
  425. SSL3_RANDOM_SIZE + 1 /* session ID length */ +
  426. 2 /* cipher list length */ +
  427. CBS_len(&cipher_specs) / 3 * 2 +
  428. 1 /* compression length */ + 1 /* compression */;
  429. CBB client_hello, hello_body, cipher_suites;
  430. CBB_zero(&client_hello);
  431. if (!BUF_MEM_reserve(ssl->init_buf, max_v3_client_hello) ||
  432. !CBB_init_fixed(&client_hello, (uint8_t *)ssl->init_buf->data,
  433. ssl->init_buf->max) ||
  434. !CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) ||
  435. !CBB_add_u24_length_prefixed(&client_hello, &hello_body) ||
  436. !CBB_add_u16(&hello_body, version) ||
  437. !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
  438. /* No session id. */
  439. !CBB_add_u8(&hello_body, 0) ||
  440. !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) {
  441. CBB_cleanup(&client_hello);
  442. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  443. return -1;
  444. }
  445. /* Copy the cipher suites. */
  446. while (CBS_len(&cipher_specs) > 0) {
  447. uint32_t cipher_spec;
  448. if (!CBS_get_u24(&cipher_specs, &cipher_spec)) {
  449. CBB_cleanup(&client_hello);
  450. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  451. return -1;
  452. }
  453. /* Skip SSLv2 ciphers. */
  454. if ((cipher_spec & 0xff0000) != 0) {
  455. continue;
  456. }
  457. if (!CBB_add_u16(&cipher_suites, cipher_spec)) {
  458. CBB_cleanup(&client_hello);
  459. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  460. return -1;
  461. }
  462. }
  463. /* Add the null compression scheme and finish. */
  464. if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) ||
  465. !CBB_finish(&client_hello, NULL, &ssl->init_buf->length)) {
  466. CBB_cleanup(&client_hello);
  467. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  468. return -1;
  469. }
  470. /* Consume and discard the V2ClientHello. */
  471. ssl_read_buffer_consume(ssl, 2 + msg_length);
  472. ssl_read_buffer_discard(ssl);
  473. *out_is_v2_client_hello = 1;
  474. return 1;
  475. }
  476. int ssl3_get_message(SSL *ssl, int msg_type,
  477. enum ssl_hash_message_t hash_message) {
  478. again:
  479. /* Re-create the handshake buffer if needed. */
  480. if (ssl->init_buf == NULL) {
  481. ssl->init_buf = BUF_MEM_new();
  482. if (ssl->init_buf == NULL) {
  483. return -1;
  484. }
  485. }
  486. if (ssl->server && !ssl->s3->v2_hello_done) {
  487. /* Bypass the record layer for the first message to handle V2ClientHello. */
  488. assert(hash_message == ssl_hash_message);
  489. int is_v2_client_hello = 0;
  490. int ret = read_v2_client_hello(ssl, &is_v2_client_hello);
  491. if (ret <= 0) {
  492. return ret;
  493. }
  494. if (is_v2_client_hello) {
  495. /* V2ClientHello is hashed separately. */
  496. hash_message = ssl_dont_hash_message;
  497. }
  498. ssl->s3->v2_hello_done = 1;
  499. }
  500. if (ssl->s3->tmp.reuse_message) {
  501. /* A ssl_dont_hash_message call cannot be combined with reuse_message; the
  502. * ssl_dont_hash_message would have to have been applied to the previous
  503. * call. */
  504. assert(hash_message == ssl_hash_message);
  505. assert(ssl->init_msg != NULL);
  506. ssl->s3->tmp.reuse_message = 0;
  507. hash_message = ssl_dont_hash_message;
  508. } else {
  509. ssl3_release_current_message(ssl, 0 /* don't free buffer */);
  510. }
  511. /* Read the message header, if we haven't yet. */
  512. int ret = extend_handshake_buffer(ssl, SSL3_HM_HEADER_LENGTH);
  513. if (ret <= 0) {
  514. return ret;
  515. }
  516. /* Parse out the length. Cap it so the peer cannot force us to buffer up to
  517. * 2^24 bytes. */
  518. const uint8_t *p = (uint8_t *)ssl->init_buf->data;
  519. size_t msg_len = (((uint32_t)p[1]) << 16) | (((uint32_t)p[2]) << 8) | p[3];
  520. if (msg_len > ssl_max_handshake_message_len(ssl)) {
  521. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  522. OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
  523. return -1;
  524. }
  525. /* Read the message body, if we haven't yet. */
  526. ret = extend_handshake_buffer(ssl, SSL3_HM_HEADER_LENGTH + msg_len);
  527. if (ret <= 0) {
  528. return ret;
  529. }
  530. /* We have now received a complete message. */
  531. ssl_do_msg_callback(ssl, 0 /* read */, ssl->version, SSL3_RT_HANDSHAKE,
  532. ssl->init_buf->data, ssl->init_buf->length);
  533. ssl->s3->tmp.message_type = ((const uint8_t *)ssl->init_buf->data)[0];
  534. ssl->init_msg = (uint8_t*)ssl->init_buf->data + SSL3_HM_HEADER_LENGTH;
  535. ssl->init_num = ssl->init_buf->length - SSL3_HM_HEADER_LENGTH;
  536. /* Ignore stray HelloRequest messages in the handshake before TLS 1.3. Per RFC
  537. * 5246, section 7.4.1.1, the server may send HelloRequest at any time. */
  538. if (!ssl->server && SSL_in_init(ssl) &&
  539. (!ssl->s3->have_version || ssl3_protocol_version(ssl) < TLS1_3_VERSION) &&
  540. ssl->s3->tmp.message_type == SSL3_MT_HELLO_REQUEST &&
  541. ssl->init_num == 0) {
  542. goto again;
  543. }
  544. if (msg_type >= 0 && ssl->s3->tmp.message_type != msg_type) {
  545. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  546. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  547. return -1;
  548. }
  549. /* Feed this message into MAC computation. */
  550. if (hash_message == ssl_hash_message && !ssl3_hash_current_message(ssl)) {
  551. return -1;
  552. }
  553. return 1;
  554. }
  555. int ssl3_hash_current_message(SSL *ssl) {
  556. return ssl3_update_handshake_hash(ssl, (uint8_t *)ssl->init_buf->data,
  557. ssl->init_buf->length);
  558. }
  559. void ssl3_release_current_message(SSL *ssl, int free_buffer) {
  560. if (ssl->init_msg != NULL) {
  561. /* |init_buf| never contains data beyond the current message. */
  562. assert(SSL3_HM_HEADER_LENGTH + ssl->init_num == ssl->init_buf->length);
  563. /* Clear the current message. */
  564. ssl->init_msg = NULL;
  565. ssl->init_num = 0;
  566. ssl->init_buf->length = 0;
  567. }
  568. if (free_buffer) {
  569. BUF_MEM_free(ssl->init_buf);
  570. ssl->init_buf = NULL;
  571. }
  572. }
  573. int ssl_verify_alarm_type(long type) {
  574. int al;
  575. switch (type) {
  576. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  577. case X509_V_ERR_UNABLE_TO_GET_CRL:
  578. case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
  579. al = SSL_AD_UNKNOWN_CA;
  580. break;
  581. case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
  582. case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
  583. case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
  584. case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  585. case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  586. case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
  587. case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
  588. case X509_V_ERR_CERT_NOT_YET_VALID:
  589. case X509_V_ERR_CRL_NOT_YET_VALID:
  590. case X509_V_ERR_CERT_UNTRUSTED:
  591. case X509_V_ERR_CERT_REJECTED:
  592. case X509_V_ERR_HOSTNAME_MISMATCH:
  593. case X509_V_ERR_EMAIL_MISMATCH:
  594. case X509_V_ERR_IP_ADDRESS_MISMATCH:
  595. al = SSL_AD_BAD_CERTIFICATE;
  596. break;
  597. case X509_V_ERR_CERT_SIGNATURE_FAILURE:
  598. case X509_V_ERR_CRL_SIGNATURE_FAILURE:
  599. al = SSL_AD_DECRYPT_ERROR;
  600. break;
  601. case X509_V_ERR_CERT_HAS_EXPIRED:
  602. case X509_V_ERR_CRL_HAS_EXPIRED:
  603. al = SSL_AD_CERTIFICATE_EXPIRED;
  604. break;
  605. case X509_V_ERR_CERT_REVOKED:
  606. al = SSL_AD_CERTIFICATE_REVOKED;
  607. break;
  608. case X509_V_ERR_UNSPECIFIED:
  609. case X509_V_ERR_OUT_OF_MEM:
  610. case X509_V_ERR_INVALID_CALL:
  611. case X509_V_ERR_STORE_LOOKUP:
  612. al = SSL_AD_INTERNAL_ERROR;
  613. break;
  614. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  615. case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
  616. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
  617. case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
  618. case X509_V_ERR_CERT_CHAIN_TOO_LONG:
  619. case X509_V_ERR_PATH_LENGTH_EXCEEDED:
  620. case X509_V_ERR_INVALID_CA:
  621. al = SSL_AD_UNKNOWN_CA;
  622. break;
  623. case X509_V_ERR_APPLICATION_VERIFICATION:
  624. al = SSL_AD_HANDSHAKE_FAILURE;
  625. break;
  626. case X509_V_ERR_INVALID_PURPOSE:
  627. al = SSL_AD_UNSUPPORTED_CERTIFICATE;
  628. break;
  629. default:
  630. al = SSL_AD_CERTIFICATE_UNKNOWN;
  631. break;
  632. }
  633. return al;
  634. }