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.
 
 
 
 
 
 

674 lines
23 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", ssl->session->master_key,
  200. ssl->session->master_key_length)) {
  201. return 0;
  202. }
  203. /* Copy the finished so we can use it for renegotiation checks */
  204. if (ssl->server) {
  205. assert(n <= EVP_MAX_MD_SIZE);
  206. memcpy(ssl->s3->previous_server_finished, ssl->s3->tmp.finish_md, n);
  207. ssl->s3->previous_server_finished_len = n;
  208. } else {
  209. assert(n <= EVP_MAX_MD_SIZE);
  210. memcpy(ssl->s3->previous_client_finished, ssl->s3->tmp.finish_md, n);
  211. ssl->s3->previous_client_finished_len = n;
  212. }
  213. CBB cbb, body;
  214. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_FINISHED) ||
  215. !CBB_add_bytes(&body, ssl->s3->tmp.finish_md,
  216. ssl->s3->tmp.finish_md_len) ||
  217. !ssl->method->finish_message(ssl, &cbb)) {
  218. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  219. CBB_cleanup(&cbb);
  220. return -1;
  221. }
  222. ssl->state = b;
  223. return ssl->method->write_message(ssl);
  224. }
  225. /* ssl3_take_mac calculates the Finished MAC for the handshakes messages seen
  226. * so far. */
  227. static void ssl3_take_mac(SSL *ssl) {
  228. /* If no new cipher setup then return immediately: other functions will set
  229. * the appropriate error. */
  230. if (ssl->s3->tmp.new_cipher == NULL) {
  231. return;
  232. }
  233. ssl->s3->tmp.peer_finish_md_len = ssl->s3->enc_method->final_finish_mac(
  234. ssl, !ssl->server, ssl->s3->tmp.peer_finish_md);
  235. }
  236. int ssl3_get_finished(SSL *ssl) {
  237. int al;
  238. int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_FINISHED,
  239. ssl_dont_hash_message);
  240. if (ret <= 0) {
  241. return ret;
  242. }
  243. /* Snapshot the finished hash before incorporating the new message. */
  244. ssl3_take_mac(ssl);
  245. if (!ssl->method->hash_current_message(ssl)) {
  246. goto err;
  247. }
  248. size_t finished_len = ssl->s3->tmp.peer_finish_md_len;
  249. if (finished_len != ssl->init_num) {
  250. al = SSL_AD_DECODE_ERROR;
  251. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DIGEST_LENGTH);
  252. goto f_err;
  253. }
  254. int finished_ret =
  255. CRYPTO_memcmp(ssl->init_msg, ssl->s3->tmp.peer_finish_md, finished_len);
  256. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  257. finished_ret = 0;
  258. #endif
  259. if (finished_ret != 0) {
  260. al = SSL_AD_DECRYPT_ERROR;
  261. OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED);
  262. goto f_err;
  263. }
  264. /* Copy the finished so we can use it for renegotiation checks */
  265. if (ssl->server) {
  266. assert(finished_len <= EVP_MAX_MD_SIZE);
  267. memcpy(ssl->s3->previous_client_finished, ssl->s3->tmp.peer_finish_md,
  268. finished_len);
  269. ssl->s3->previous_client_finished_len = finished_len;
  270. } else {
  271. assert(finished_len <= EVP_MAX_MD_SIZE);
  272. memcpy(ssl->s3->previous_server_finished, ssl->s3->tmp.peer_finish_md,
  273. finished_len);
  274. ssl->s3->previous_server_finished_len = finished_len;
  275. }
  276. return 1;
  277. f_err:
  278. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  279. err:
  280. return 0;
  281. }
  282. int ssl3_send_change_cipher_spec(SSL *ssl) {
  283. static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS};
  284. return ssl3_do_write(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec,
  285. sizeof(kChangeCipherSpec));
  286. }
  287. int ssl3_output_cert_chain(SSL *ssl) {
  288. CBB cbb, body;
  289. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CERTIFICATE) ||
  290. !ssl_add_cert_chain(ssl, &body) ||
  291. !ssl->method->finish_message(ssl, &cbb)) {
  292. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  293. CBB_cleanup(&cbb);
  294. return 0;
  295. }
  296. return 1;
  297. }
  298. size_t ssl_max_handshake_message_len(const SSL *ssl) {
  299. /* kMaxMessageLen is the default maximum message size for handshakes which do
  300. * not accept peer certificate chains. */
  301. static const size_t kMaxMessageLen = 16384;
  302. if ((!ssl->server || (ssl->verify_mode & SSL_VERIFY_PEER)) &&
  303. kMaxMessageLen < ssl->max_cert_list) {
  304. return ssl->max_cert_list;
  305. }
  306. return kMaxMessageLen;
  307. }
  308. static int extend_handshake_buffer(SSL *ssl, size_t length) {
  309. if (!BUF_MEM_reserve(ssl->init_buf, length)) {
  310. return -1;
  311. }
  312. while (ssl->init_buf->length < length) {
  313. int ret =
  314. ssl3_read_bytes(ssl, SSL3_RT_HANDSHAKE,
  315. (uint8_t *)ssl->init_buf->data + ssl->init_buf->length,
  316. length - ssl->init_buf->length, 0);
  317. if (ret <= 0) {
  318. return ret;
  319. }
  320. ssl->init_buf->length += (size_t)ret;
  321. }
  322. return 1;
  323. }
  324. static int read_v2_client_hello(SSL *ssl) {
  325. /* Read the first 5 bytes, the size of the TLS record header. This is
  326. * sufficient to detect a V2ClientHello and ensures that we never read beyond
  327. * the first record. */
  328. int ret = ssl_read_buffer_extend_to(ssl, SSL3_RT_HEADER_LENGTH);
  329. if (ret <= 0) {
  330. return ret;
  331. }
  332. const uint8_t *p = ssl_read_buffer(ssl);
  333. /* Some dedicated error codes for protocol mixups should the application wish
  334. * to interpret them differently. (These do not overlap with ClientHello or
  335. * V2ClientHello.) */
  336. if (strncmp("GET ", (const char *)p, 4) == 0 ||
  337. strncmp("POST ", (const char *)p, 5) == 0 ||
  338. strncmp("HEAD ", (const char *)p, 5) == 0 ||
  339. strncmp("PUT ", (const char *)p, 4) == 0) {
  340. OPENSSL_PUT_ERROR(SSL, SSL_R_HTTP_REQUEST);
  341. return -1;
  342. }
  343. if (strncmp("CONNE", (const char *)p, 5) == 0) {
  344. OPENSSL_PUT_ERROR(SSL, SSL_R_HTTPS_PROXY_REQUEST);
  345. return -1;
  346. }
  347. if ((p[0] & 0x80) == 0 || p[2] != SSL2_MT_CLIENT_HELLO ||
  348. p[3] != SSL3_VERSION_MAJOR) {
  349. /* Not a V2ClientHello. */
  350. return 1;
  351. }
  352. /* Determine the length of the V2ClientHello. */
  353. size_t msg_length = ((p[0] & 0x7f) << 8) | p[1];
  354. if (msg_length > (1024 * 4)) {
  355. OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
  356. return -1;
  357. }
  358. if (msg_length < SSL3_RT_HEADER_LENGTH - 2) {
  359. /* Reject lengths that are too short early. We have already read
  360. * |SSL3_RT_HEADER_LENGTH| bytes, so we should not attempt to process an
  361. * (invalid) V2ClientHello which would be shorter than that. */
  362. OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_LENGTH_MISMATCH);
  363. return -1;
  364. }
  365. /* Read the remainder of the V2ClientHello. */
  366. ret = ssl_read_buffer_extend_to(ssl, 2 + msg_length);
  367. if (ret <= 0) {
  368. return ret;
  369. }
  370. CBS v2_client_hello;
  371. CBS_init(&v2_client_hello, ssl_read_buffer(ssl) + 2, msg_length);
  372. /* The V2ClientHello without the length is incorporated into the handshake
  373. * hash. */
  374. if (!ssl3_update_handshake_hash(ssl, CBS_data(&v2_client_hello),
  375. CBS_len(&v2_client_hello))) {
  376. return -1;
  377. }
  378. ssl_do_msg_callback(ssl, 0 /* read */, SSL2_VERSION, 0,
  379. CBS_data(&v2_client_hello), CBS_len(&v2_client_hello));
  380. uint8_t msg_type;
  381. uint16_t version, cipher_spec_length, session_id_length, challenge_length;
  382. CBS cipher_specs, session_id, challenge;
  383. if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
  384. !CBS_get_u16(&v2_client_hello, &version) ||
  385. !CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
  386. !CBS_get_u16(&v2_client_hello, &session_id_length) ||
  387. !CBS_get_u16(&v2_client_hello, &challenge_length) ||
  388. !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
  389. !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
  390. !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
  391. CBS_len(&v2_client_hello) != 0) {
  392. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  393. return -1;
  394. }
  395. /* msg_type has already been checked. */
  396. assert(msg_type == SSL2_MT_CLIENT_HELLO);
  397. /* The client_random is the V2ClientHello challenge. Truncate or
  398. * left-pad with zeros as needed. */
  399. size_t rand_len = CBS_len(&challenge);
  400. if (rand_len > SSL3_RANDOM_SIZE) {
  401. rand_len = SSL3_RANDOM_SIZE;
  402. }
  403. uint8_t random[SSL3_RANDOM_SIZE];
  404. memset(random, 0, SSL3_RANDOM_SIZE);
  405. memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge),
  406. rand_len);
  407. /* Write out an equivalent SSLv3 ClientHello. */
  408. CBB client_hello, hello_body, cipher_suites;
  409. if (!CBB_init_fixed(&client_hello, (uint8_t *)ssl->init_buf->data,
  410. ssl->init_buf->max) ||
  411. !CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) ||
  412. !CBB_add_u24_length_prefixed(&client_hello, &hello_body) ||
  413. !CBB_add_u16(&hello_body, version) ||
  414. !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
  415. /* No session id. */
  416. !CBB_add_u8(&hello_body, 0) ||
  417. !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) {
  418. CBB_cleanup(&client_hello);
  419. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  420. return -1;
  421. }
  422. /* Copy the cipher suites. */
  423. while (CBS_len(&cipher_specs) > 0) {
  424. uint32_t cipher_spec;
  425. if (!CBS_get_u24(&cipher_specs, &cipher_spec)) {
  426. CBB_cleanup(&client_hello);
  427. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  428. return -1;
  429. }
  430. /* Skip SSLv2 ciphers. */
  431. if ((cipher_spec & 0xff0000) != 0) {
  432. continue;
  433. }
  434. if (!CBB_add_u16(&cipher_suites, cipher_spec)) {
  435. CBB_cleanup(&client_hello);
  436. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  437. return -1;
  438. }
  439. }
  440. /* Add the null compression scheme and finish. */
  441. if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) ||
  442. !CBB_finish(&client_hello, NULL, &ssl->init_buf->length)) {
  443. CBB_cleanup(&client_hello);
  444. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  445. return -1;
  446. }
  447. /* Mark the message for "re"-use. */
  448. ssl->s3->tmp.reuse_message = 1;
  449. ssl->s3->tmp.message_complete = 1;
  450. /* Consume and discard the V2ClientHello. */
  451. ssl_read_buffer_consume(ssl, 2 + msg_length);
  452. ssl_read_buffer_discard(ssl);
  453. return 1;
  454. }
  455. int ssl3_get_message(SSL *ssl, int msg_type,
  456. enum ssl_hash_message_t hash_message) {
  457. again:
  458. if (ssl->server && !ssl->s3->v2_hello_done) {
  459. /* Bypass the record layer for the first message to handle V2ClientHello. */
  460. assert(hash_message == ssl_hash_message);
  461. int ret = read_v2_client_hello(ssl);
  462. if (ret <= 0) {
  463. return ret;
  464. }
  465. ssl->s3->v2_hello_done = 1;
  466. }
  467. if (ssl->s3->tmp.reuse_message) {
  468. /* A ssl_dont_hash_message call cannot be combined with reuse_message; the
  469. * ssl_dont_hash_message would have to have been applied to the previous
  470. * call. */
  471. assert(hash_message == ssl_hash_message);
  472. assert(ssl->s3->tmp.message_complete);
  473. ssl->s3->tmp.reuse_message = 0;
  474. hash_message = ssl_dont_hash_message;
  475. } else if (ssl->s3->tmp.message_complete) {
  476. ssl->s3->tmp.message_complete = 0;
  477. ssl->init_buf->length = 0;
  478. }
  479. /* Read the message header, if we haven't yet. */
  480. int ret = extend_handshake_buffer(ssl, 4);
  481. if (ret <= 0) {
  482. return ret;
  483. }
  484. /* Parse out the length. Cap it so the peer cannot force us to buffer up to
  485. * 2^24 bytes. */
  486. const uint8_t *p = (uint8_t *)ssl->init_buf->data;
  487. size_t msg_len = (((uint32_t)p[1]) << 16) | (((uint32_t)p[2]) << 8) | p[3];
  488. if (msg_len > ssl_max_handshake_message_len(ssl)) {
  489. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  490. OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
  491. return -1;
  492. }
  493. /* Read the message body, if we haven't yet. */
  494. ret = extend_handshake_buffer(ssl, 4 + msg_len);
  495. if (ret <= 0) {
  496. return ret;
  497. }
  498. /* We have now received a complete message. */
  499. ssl->s3->tmp.message_complete = 1;
  500. ssl_do_msg_callback(ssl, 0 /* read */, ssl->version, SSL3_RT_HANDSHAKE,
  501. ssl->init_buf->data, ssl->init_buf->length);
  502. /* Ignore stray HelloRequest messages. Per RFC 5246, section 7.4.1.1, the
  503. * server may send HelloRequest at any time. */
  504. static const uint8_t kHelloRequest[4] = {SSL3_MT_HELLO_REQUEST, 0, 0, 0};
  505. if (!ssl->server &&
  506. (!ssl->s3->have_version ||
  507. ssl3_protocol_version(ssl) < TLS1_3_VERSION) &&
  508. ssl->init_buf->length == sizeof(kHelloRequest) &&
  509. memcmp(kHelloRequest, ssl->init_buf->data, sizeof(kHelloRequest)) == 0) {
  510. goto again;
  511. }
  512. uint8_t actual_type = ((const uint8_t *)ssl->init_buf->data)[0];
  513. if (msg_type >= 0 && actual_type != msg_type) {
  514. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  515. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  516. return -1;
  517. }
  518. ssl->s3->tmp.message_type = actual_type;
  519. ssl->init_msg = (uint8_t*)ssl->init_buf->data + 4;
  520. ssl->init_num = ssl->init_buf->length - 4;
  521. /* Feed this message into MAC computation. */
  522. if (hash_message == ssl_hash_message && !ssl3_hash_current_message(ssl)) {
  523. return -1;
  524. }
  525. return 1;
  526. }
  527. int ssl3_hash_current_message(SSL *ssl) {
  528. return ssl3_update_handshake_hash(ssl, (uint8_t *)ssl->init_buf->data,
  529. ssl->init_buf->length);
  530. }
  531. int ssl_verify_alarm_type(long type) {
  532. int al;
  533. switch (type) {
  534. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  535. case X509_V_ERR_UNABLE_TO_GET_CRL:
  536. case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
  537. al = SSL_AD_UNKNOWN_CA;
  538. break;
  539. case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
  540. case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
  541. case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
  542. case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  543. case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  544. case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
  545. case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
  546. case X509_V_ERR_CERT_NOT_YET_VALID:
  547. case X509_V_ERR_CRL_NOT_YET_VALID:
  548. case X509_V_ERR_CERT_UNTRUSTED:
  549. case X509_V_ERR_CERT_REJECTED:
  550. case X509_V_ERR_HOSTNAME_MISMATCH:
  551. case X509_V_ERR_EMAIL_MISMATCH:
  552. case X509_V_ERR_IP_ADDRESS_MISMATCH:
  553. al = SSL_AD_BAD_CERTIFICATE;
  554. break;
  555. case X509_V_ERR_CERT_SIGNATURE_FAILURE:
  556. case X509_V_ERR_CRL_SIGNATURE_FAILURE:
  557. al = SSL_AD_DECRYPT_ERROR;
  558. break;
  559. case X509_V_ERR_CERT_HAS_EXPIRED:
  560. case X509_V_ERR_CRL_HAS_EXPIRED:
  561. al = SSL_AD_CERTIFICATE_EXPIRED;
  562. break;
  563. case X509_V_ERR_CERT_REVOKED:
  564. al = SSL_AD_CERTIFICATE_REVOKED;
  565. break;
  566. case X509_V_ERR_UNSPECIFIED:
  567. case X509_V_ERR_OUT_OF_MEM:
  568. case X509_V_ERR_INVALID_CALL:
  569. case X509_V_ERR_STORE_LOOKUP:
  570. al = SSL_AD_INTERNAL_ERROR;
  571. break;
  572. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  573. case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
  574. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
  575. case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
  576. case X509_V_ERR_CERT_CHAIN_TOO_LONG:
  577. case X509_V_ERR_PATH_LENGTH_EXCEEDED:
  578. case X509_V_ERR_INVALID_CA:
  579. al = SSL_AD_UNKNOWN_CA;
  580. break;
  581. case X509_V_ERR_APPLICATION_VERIFICATION:
  582. al = SSL_AD_HANDSHAKE_FAILURE;
  583. break;
  584. case X509_V_ERR_INVALID_PURPOSE:
  585. al = SSL_AD_UNSUPPORTED_CERTIFICATE;
  586. break;
  587. default:
  588. al = SSL_AD_CERTIFICATE_UNKNOWN;
  589. break;
  590. }
  591. return al;
  592. }