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.
 
 
 
 
 
 

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