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

1826 lignes
61 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-2007 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. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. *
  113. * Portions of the attached software ("Contribution") are developed by
  114. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  115. *
  116. * The Contribution is licensed pursuant to the OpenSSL open source
  117. * license provided above.
  118. *
  119. * ECC cipher suite support in OpenSSL originally written by
  120. * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
  121. *
  122. */
  123. /* ====================================================================
  124. * Copyright 2005 Nokia. All rights reserved.
  125. *
  126. * The portions of the attached software ("Contribution") is developed by
  127. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  128. * license.
  129. *
  130. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  131. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  132. * support (see RFC 4279) to OpenSSL.
  133. *
  134. * No patent licenses or other rights except those expressly stated in
  135. * the OpenSSL open source license shall be deemed granted or received
  136. * expressly, by implication, estoppel, or otherwise.
  137. *
  138. * No assurances are provided by Nokia that the Contribution does not
  139. * infringe the patent or other intellectual property rights of any third
  140. * party or that the license provides you with all the necessary rights
  141. * to make use of the Contribution.
  142. *
  143. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  144. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  145. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  146. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  147. * OTHERWISE.
  148. */
  149. #include <openssl/ssl.h>
  150. #include <assert.h>
  151. #include <limits.h>
  152. #include <string.h>
  153. #include <utility>
  154. #include <openssl/aead.h>
  155. #include <openssl/bn.h>
  156. #include <openssl/buf.h>
  157. #include <openssl/bytestring.h>
  158. #include <openssl/ec_key.h>
  159. #include <openssl/ecdsa.h>
  160. #include <openssl/err.h>
  161. #include <openssl/evp.h>
  162. #include <openssl/md5.h>
  163. #include <openssl/mem.h>
  164. #include <openssl/rand.h>
  165. #include "../crypto/internal.h"
  166. #include "internal.h"
  167. namespace bssl {
  168. enum ssl_client_hs_state_t {
  169. state_start_connect = 0,
  170. state_enter_early_data,
  171. state_read_hello_verify_request,
  172. state_read_server_hello,
  173. state_tls13,
  174. state_read_server_certificate,
  175. state_read_certificate_status,
  176. state_verify_server_certificate,
  177. state_reverify_server_certificate,
  178. state_read_server_key_exchange,
  179. state_read_certificate_request,
  180. state_read_server_hello_done,
  181. state_send_client_certificate,
  182. state_send_client_key_exchange,
  183. state_send_client_certificate_verify,
  184. state_send_client_finished,
  185. state_finish_flight,
  186. state_read_session_ticket,
  187. state_process_change_cipher_spec,
  188. state_read_server_finished,
  189. state_finish_client_handshake,
  190. state_done,
  191. };
  192. // ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
  193. // disabled algorithms.
  194. static void ssl_get_client_disabled(SSL_HANDSHAKE *hs, uint32_t *out_mask_a,
  195. uint32_t *out_mask_k) {
  196. *out_mask_a = 0;
  197. *out_mask_k = 0;
  198. // PSK requires a client callback.
  199. if (hs->config->psk_client_callback == NULL) {
  200. *out_mask_a |= SSL_aPSK;
  201. *out_mask_k |= SSL_kPSK;
  202. }
  203. }
  204. static int ssl_write_client_cipher_list(SSL_HANDSHAKE *hs, CBB *out) {
  205. SSL *const ssl = hs->ssl;
  206. uint32_t mask_a, mask_k;
  207. ssl_get_client_disabled(hs, &mask_a, &mask_k);
  208. CBB child;
  209. if (!CBB_add_u16_length_prefixed(out, &child)) {
  210. return 0;
  211. }
  212. // Add a fake cipher suite. See draft-davidben-tls-grease-01.
  213. if (ssl->ctx->grease_enabled &&
  214. !CBB_add_u16(&child, ssl_get_grease_value(hs, ssl_grease_cipher))) {
  215. return 0;
  216. }
  217. // Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
  218. // hardware support.
  219. if (hs->max_version >= TLS1_3_VERSION) {
  220. if (!EVP_has_aes_hardware() &&
  221. !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
  222. return 0;
  223. }
  224. if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
  225. !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
  226. return 0;
  227. }
  228. if (EVP_has_aes_hardware() &&
  229. !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
  230. return 0;
  231. }
  232. }
  233. if (hs->min_version < TLS1_3_VERSION) {
  234. int any_enabled = 0;
  235. for (const SSL_CIPHER *cipher : SSL_get_ciphers(ssl)) {
  236. // Skip disabled ciphers
  237. if ((cipher->algorithm_mkey & mask_k) ||
  238. (cipher->algorithm_auth & mask_a)) {
  239. continue;
  240. }
  241. if (SSL_CIPHER_get_min_version(cipher) > hs->max_version ||
  242. SSL_CIPHER_get_max_version(cipher) < hs->min_version) {
  243. continue;
  244. }
  245. any_enabled = 1;
  246. if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
  247. return 0;
  248. }
  249. }
  250. // If all ciphers were disabled, return the error to the caller.
  251. if (!any_enabled && hs->max_version < TLS1_3_VERSION) {
  252. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
  253. return 0;
  254. }
  255. }
  256. if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
  257. if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
  258. return 0;
  259. }
  260. }
  261. return CBB_flush(out);
  262. }
  263. int ssl_write_client_hello(SSL_HANDSHAKE *hs) {
  264. SSL *const ssl = hs->ssl;
  265. ScopedCBB cbb;
  266. CBB body;
  267. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO)) {
  268. return 0;
  269. }
  270. CBB child;
  271. if (!CBB_add_u16(&body, hs->client_version) ||
  272. !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  273. !CBB_add_u8_length_prefixed(&body, &child)) {
  274. return 0;
  275. }
  276. // Do not send a session ID on renegotiation.
  277. if (!ssl->s3->initial_handshake_complete &&
  278. !CBB_add_bytes(&child, hs->session_id, hs->session_id_len)) {
  279. return 0;
  280. }
  281. if (SSL_is_dtls(ssl)) {
  282. if (!CBB_add_u8_length_prefixed(&body, &child) ||
  283. !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
  284. return 0;
  285. }
  286. }
  287. size_t header_len =
  288. SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
  289. if (!ssl_write_client_cipher_list(hs, &body) ||
  290. !CBB_add_u8(&body, 1 /* one compression method */) ||
  291. !CBB_add_u8(&body, 0 /* null compression */) ||
  292. !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
  293. return 0;
  294. }
  295. Array<uint8_t> msg;
  296. if (!ssl->method->finish_message(ssl, cbb.get(), &msg)) {
  297. return 0;
  298. }
  299. // Now that the length prefixes have been computed, fill in the placeholder
  300. // PSK binder.
  301. if (hs->needs_psk_binder &&
  302. !tls13_write_psk_binder(hs, msg.data(), msg.size())) {
  303. return 0;
  304. }
  305. return ssl->method->add_message(ssl, std::move(msg));
  306. }
  307. static bool parse_supported_versions(SSL_HANDSHAKE *hs, uint16_t *version,
  308. const CBS *in) {
  309. // If the outer version is not TLS 1.2, or there is no extensions block, use
  310. // the outer version.
  311. if (*version != TLS1_2_VERSION || CBS_len(in) == 0) {
  312. return true;
  313. }
  314. SSL *const ssl = hs->ssl;
  315. CBS copy = *in, extensions;
  316. if (!CBS_get_u16_length_prefixed(&copy, &extensions) ||
  317. CBS_len(&copy) != 0) {
  318. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  319. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  320. return false;
  321. }
  322. bool have_supported_versions;
  323. CBS supported_versions;
  324. const SSL_EXTENSION_TYPE ext_types[] = {
  325. {TLSEXT_TYPE_supported_versions, &have_supported_versions,
  326. &supported_versions},
  327. };
  328. uint8_t alert = SSL_AD_DECODE_ERROR;
  329. if (!ssl_parse_extensions(&extensions, &alert, ext_types,
  330. OPENSSL_ARRAY_SIZE(ext_types),
  331. 1 /* ignore unknown */)) {
  332. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  333. return false;
  334. }
  335. // Override the outer version with the extension, if present.
  336. if (have_supported_versions &&
  337. (!CBS_get_u16(&supported_versions, version) ||
  338. CBS_len(&supported_versions) != 0)) {
  339. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  340. return false;
  341. }
  342. return true;
  343. }
  344. static enum ssl_hs_wait_t do_start_connect(SSL_HANDSHAKE *hs) {
  345. SSL *const ssl = hs->ssl;
  346. ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
  347. // |session_reused| must be reset in case this is a renegotiation.
  348. ssl->s3->session_reused = false;
  349. // Freeze the version range.
  350. if (!ssl_get_version_range(hs, &hs->min_version, &hs->max_version)) {
  351. return ssl_hs_error;
  352. }
  353. // Always advertise the ClientHello version from the original maximum version,
  354. // even on renegotiation. The static RSA key exchange uses this field, and
  355. // some servers fail when it changes across handshakes.
  356. if (SSL_is_dtls(hs->ssl)) {
  357. hs->client_version =
  358. hs->max_version >= TLS1_2_VERSION ? DTLS1_2_VERSION : DTLS1_VERSION;
  359. } else {
  360. hs->client_version =
  361. hs->max_version >= TLS1_2_VERSION ? TLS1_2_VERSION : hs->max_version;
  362. }
  363. // If the configured session has expired or was created at a disabled
  364. // version, drop it.
  365. if (ssl->session != NULL) {
  366. if (ssl->session->is_server ||
  367. !ssl_supports_version(hs, ssl->session->ssl_version) ||
  368. (ssl->session->session_id_length == 0 &&
  369. ssl->session->ticket.empty()) ||
  370. ssl->session->not_resumable ||
  371. !ssl_session_is_time_valid(ssl, ssl->session.get())) {
  372. ssl_set_session(ssl, NULL);
  373. }
  374. }
  375. if (!RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
  376. return ssl_hs_error;
  377. }
  378. // Initialize a random session ID for the experimental TLS 1.3 variant
  379. // requiring a session id.
  380. if (ssl->session != nullptr &&
  381. !ssl->s3->initial_handshake_complete &&
  382. ssl->session->session_id_length > 0) {
  383. hs->session_id_len = ssl->session->session_id_length;
  384. OPENSSL_memcpy(hs->session_id, ssl->session->session_id,
  385. hs->session_id_len);
  386. } else if (hs->max_version >= TLS1_3_VERSION) {
  387. hs->session_id_len = sizeof(hs->session_id);
  388. if (!RAND_bytes(hs->session_id, hs->session_id_len)) {
  389. return ssl_hs_error;
  390. }
  391. }
  392. if (!ssl_write_client_hello(hs)) {
  393. return ssl_hs_error;
  394. }
  395. hs->state = state_enter_early_data;
  396. return ssl_hs_flush;
  397. }
  398. static enum ssl_hs_wait_t do_enter_early_data(SSL_HANDSHAKE *hs) {
  399. SSL *const ssl = hs->ssl;
  400. if (SSL_is_dtls(ssl)) {
  401. hs->state = state_read_hello_verify_request;
  402. return ssl_hs_ok;
  403. }
  404. if (!hs->early_data_offered) {
  405. hs->state = state_read_server_hello;
  406. return ssl_hs_ok;
  407. }
  408. ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->session->ssl_version);
  409. if (!ssl->method->add_change_cipher_spec(ssl)) {
  410. return ssl_hs_error;
  411. }
  412. if (!tls13_init_early_key_schedule(hs, ssl->session->master_key,
  413. ssl->session->master_key_length) ||
  414. !tls13_derive_early_secrets(hs) ||
  415. !tls13_set_traffic_key(ssl, evp_aead_seal, hs->early_traffic_secret,
  416. hs->hash_len)) {
  417. return ssl_hs_error;
  418. }
  419. // Stash the early data session, so connection properties may be queried out
  420. // of it.
  421. hs->in_early_data = true;
  422. hs->early_session = UpRef(ssl->session);
  423. hs->can_early_write = true;
  424. hs->state = state_read_server_hello;
  425. return ssl_hs_early_return;
  426. }
  427. static enum ssl_hs_wait_t do_read_hello_verify_request(SSL_HANDSHAKE *hs) {
  428. SSL *const ssl = hs->ssl;
  429. assert(SSL_is_dtls(ssl));
  430. SSLMessage msg;
  431. if (!ssl->method->get_message(ssl, &msg)) {
  432. return ssl_hs_read_message;
  433. }
  434. if (msg.type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
  435. hs->state = state_read_server_hello;
  436. return ssl_hs_ok;
  437. }
  438. CBS hello_verify_request = msg.body, cookie;
  439. uint16_t server_version;
  440. if (!CBS_get_u16(&hello_verify_request, &server_version) ||
  441. !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
  442. CBS_len(&cookie) > sizeof(ssl->d1->cookie) ||
  443. CBS_len(&hello_verify_request) != 0) {
  444. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  445. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  446. return ssl_hs_error;
  447. }
  448. OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
  449. ssl->d1->cookie_len = CBS_len(&cookie);
  450. ssl->method->next_message(ssl);
  451. // DTLS resets the handshake buffer after HelloVerifyRequest.
  452. if (!hs->transcript.Init()) {
  453. return ssl_hs_error;
  454. }
  455. if (!ssl_write_client_hello(hs)) {
  456. return ssl_hs_error;
  457. }
  458. hs->state = state_read_server_hello;
  459. return ssl_hs_flush;
  460. }
  461. static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) {
  462. SSL *const ssl = hs->ssl;
  463. SSLMessage msg;
  464. if (!ssl->method->get_message(ssl, &msg)) {
  465. return ssl_hs_read_server_hello;
  466. }
  467. if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) {
  468. return ssl_hs_error;
  469. }
  470. CBS server_hello = msg.body, server_random, session_id;
  471. uint16_t server_version, cipher_suite;
  472. uint8_t compression_method;
  473. if (!CBS_get_u16(&server_hello, &server_version) ||
  474. !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
  475. !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
  476. CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
  477. !CBS_get_u16(&server_hello, &cipher_suite) ||
  478. !CBS_get_u8(&server_hello, &compression_method)) {
  479. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  480. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  481. return ssl_hs_error;
  482. }
  483. // Use the supported_versions extension if applicable.
  484. if (!parse_supported_versions(hs, &server_version, &server_hello)) {
  485. return ssl_hs_error;
  486. }
  487. if (!ssl_supports_version(hs, server_version)) {
  488. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
  489. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
  490. return ssl_hs_error;
  491. }
  492. assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
  493. if (!ssl->s3->have_version) {
  494. ssl->version = server_version;
  495. // At this point, the connection's version is known and ssl->version is
  496. // fixed. Begin enforcing the record-layer version.
  497. ssl->s3->have_version = true;
  498. ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
  499. } else if (server_version != ssl->version) {
  500. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
  501. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
  502. return ssl_hs_error;
  503. }
  504. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  505. hs->state = state_tls13;
  506. return ssl_hs_ok;
  507. }
  508. // Clear some TLS 1.3 state that no longer needs to be retained.
  509. hs->key_share.reset();
  510. hs->key_share_bytes.Reset();
  511. // A TLS 1.2 server would not know to skip the early data we offered. Report
  512. // an error code sooner. The caller may use this error code to implement the
  513. // fallback described in RFC 8446 appendix D.3.
  514. if (hs->early_data_offered) {
  515. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA);
  516. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
  517. return ssl_hs_error;
  518. }
  519. // Copy over the server random.
  520. OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
  521. SSL3_RANDOM_SIZE);
  522. // Enforce the TLS 1.3 anti-downgrade feature.
  523. if (!ssl->s3->initial_handshake_complete &&
  524. ssl_supports_version(hs, TLS1_3_VERSION)) {
  525. static_assert(
  526. sizeof(kTLS12DowngradeRandom) == sizeof(kTLS13DowngradeRandom),
  527. "downgrade signals have different size");
  528. auto suffix =
  529. MakeConstSpan(ssl->s3->server_random, sizeof(ssl->s3->server_random))
  530. .subspan(SSL3_RANDOM_SIZE - sizeof(kTLS13DowngradeRandom));
  531. if (suffix == kTLS12DowngradeRandom || suffix == kTLS13DowngradeRandom) {
  532. ssl->s3->tls13_downgrade = true;
  533. if (!ssl->ctx->ignore_tls13_downgrade) {
  534. OPENSSL_PUT_ERROR(SSL, SSL_R_TLS13_DOWNGRADE);
  535. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  536. return ssl_hs_error;
  537. }
  538. }
  539. }
  540. if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
  541. ssl->session->session_id_length != 0 &&
  542. CBS_mem_equal(&session_id, ssl->session->session_id,
  543. ssl->session->session_id_length)) {
  544. ssl->s3->session_reused = true;
  545. } else {
  546. // The server may also have echoed back the TLS 1.3 compatibility mode
  547. // session ID. As we know this is not a session the server knows about, any
  548. // server resuming it is in error. Reject the first connection
  549. // deterministicly, rather than installing an invalid session into the
  550. // session cache. https://crbug.com/796910
  551. if (hs->session_id_len != 0 &&
  552. CBS_mem_equal(&session_id, hs->session_id, hs->session_id_len)) {
  553. OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_ECHOED_INVALID_SESSION_ID);
  554. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  555. return ssl_hs_error;
  556. }
  557. // The session wasn't resumed. Create a fresh SSL_SESSION to
  558. // fill out.
  559. ssl_set_session(ssl, NULL);
  560. if (!ssl_get_new_session(hs, 0 /* client */)) {
  561. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  562. return ssl_hs_error;
  563. }
  564. // Note: session_id could be empty.
  565. hs->new_session->session_id_length = CBS_len(&session_id);
  566. OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id),
  567. CBS_len(&session_id));
  568. }
  569. const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
  570. if (cipher == NULL) {
  571. // unknown cipher
  572. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
  573. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  574. return ssl_hs_error;
  575. }
  576. // The cipher must be allowed in the selected version and enabled.
  577. uint32_t mask_a, mask_k;
  578. ssl_get_client_disabled(hs, &mask_a, &mask_k);
  579. if ((cipher->algorithm_mkey & mask_k) || (cipher->algorithm_auth & mask_a) ||
  580. SSL_CIPHER_get_min_version(cipher) > ssl_protocol_version(ssl) ||
  581. SSL_CIPHER_get_max_version(cipher) < ssl_protocol_version(ssl) ||
  582. !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, cipher)) {
  583. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
  584. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  585. return ssl_hs_error;
  586. }
  587. if (ssl->session != NULL) {
  588. if (ssl->session->ssl_version != ssl->version) {
  589. OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
  590. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  591. return ssl_hs_error;
  592. }
  593. if (ssl->session->cipher != cipher) {
  594. OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  595. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  596. return ssl_hs_error;
  597. }
  598. if (!ssl_session_is_context_valid(hs, ssl->session.get())) {
  599. // This is actually a client application bug.
  600. OPENSSL_PUT_ERROR(SSL,
  601. SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  602. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  603. return ssl_hs_error;
  604. }
  605. } else {
  606. hs->new_session->cipher = cipher;
  607. }
  608. hs->new_cipher = cipher;
  609. // Now that the cipher is known, initialize the handshake hash and hash the
  610. // ServerHello.
  611. if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
  612. !ssl_hash_message(hs, msg)) {
  613. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  614. return ssl_hs_error;
  615. }
  616. // If doing a full handshake, the server may request a client certificate
  617. // which requires hashing the handshake transcript. Otherwise, the handshake
  618. // buffer may be released.
  619. if (ssl->session != NULL ||
  620. !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  621. hs->transcript.FreeBuffer();
  622. }
  623. // Only the NULL compression algorithm is supported.
  624. if (compression_method != 0) {
  625. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  626. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  627. return ssl_hs_error;
  628. }
  629. // TLS extensions
  630. if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
  631. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  632. return ssl_hs_error;
  633. }
  634. // There should be nothing left over in the record.
  635. if (CBS_len(&server_hello) != 0) {
  636. // wrong packet length
  637. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  638. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  639. return ssl_hs_error;
  640. }
  641. if (ssl->session != NULL &&
  642. hs->extended_master_secret != ssl->session->extended_master_secret) {
  643. if (ssl->session->extended_master_secret) {
  644. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
  645. } else {
  646. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
  647. }
  648. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  649. return ssl_hs_error;
  650. }
  651. if (ssl->s3->token_binding_negotiated &&
  652. (!hs->extended_master_secret || !ssl->s3->send_connection_binding)) {
  653. OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_TB_WITHOUT_EMS_OR_RI);
  654. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
  655. return ssl_hs_error;
  656. }
  657. ssl->method->next_message(ssl);
  658. if (ssl->session != NULL) {
  659. if (ssl->ctx->reverify_on_resume &&
  660. ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  661. hs->state = state_reverify_server_certificate;
  662. } else {
  663. hs->state = state_read_session_ticket;
  664. }
  665. return ssl_hs_ok;
  666. }
  667. hs->state = state_read_server_certificate;
  668. return ssl_hs_ok;
  669. }
  670. static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
  671. enum ssl_hs_wait_t wait = tls13_client_handshake(hs);
  672. if (wait == ssl_hs_ok) {
  673. hs->state = state_finish_client_handshake;
  674. return ssl_hs_ok;
  675. }
  676. return wait;
  677. }
  678. static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) {
  679. SSL *const ssl = hs->ssl;
  680. if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  681. hs->state = state_read_certificate_status;
  682. return ssl_hs_ok;
  683. }
  684. SSLMessage msg;
  685. if (!ssl->method->get_message(ssl, &msg)) {
  686. return ssl_hs_read_message;
  687. }
  688. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
  689. !ssl_hash_message(hs, msg)) {
  690. return ssl_hs_error;
  691. }
  692. CBS body = msg.body;
  693. uint8_t alert = SSL_AD_DECODE_ERROR;
  694. if (!ssl_parse_cert_chain(&alert, &hs->new_session->certs, &hs->peer_pubkey,
  695. NULL, &body, ssl->ctx->pool)) {
  696. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  697. return ssl_hs_error;
  698. }
  699. if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0 ||
  700. CBS_len(&body) != 0 ||
  701. !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
  702. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  703. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  704. return ssl_hs_error;
  705. }
  706. if (!ssl_check_leaf_certificate(
  707. hs, hs->peer_pubkey.get(),
  708. sk_CRYPTO_BUFFER_value(hs->new_session->certs.get(), 0))) {
  709. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  710. return ssl_hs_error;
  711. }
  712. ssl->method->next_message(ssl);
  713. hs->state = state_read_certificate_status;
  714. return ssl_hs_ok;
  715. }
  716. static enum ssl_hs_wait_t do_read_certificate_status(SSL_HANDSHAKE *hs) {
  717. SSL *const ssl = hs->ssl;
  718. if (!hs->certificate_status_expected) {
  719. hs->state = state_verify_server_certificate;
  720. return ssl_hs_ok;
  721. }
  722. SSLMessage msg;
  723. if (!ssl->method->get_message(ssl, &msg)) {
  724. return ssl_hs_read_message;
  725. }
  726. if (msg.type != SSL3_MT_CERTIFICATE_STATUS) {
  727. // A server may send status_request in ServerHello and then change its mind
  728. // about sending CertificateStatus.
  729. hs->state = state_verify_server_certificate;
  730. return ssl_hs_ok;
  731. }
  732. if (!ssl_hash_message(hs, msg)) {
  733. return ssl_hs_error;
  734. }
  735. CBS certificate_status = msg.body, ocsp_response;
  736. uint8_t status_type;
  737. if (!CBS_get_u8(&certificate_status, &status_type) ||
  738. status_type != TLSEXT_STATUSTYPE_ocsp ||
  739. !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
  740. CBS_len(&ocsp_response) == 0 ||
  741. CBS_len(&certificate_status) != 0) {
  742. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  743. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  744. return ssl_hs_error;
  745. }
  746. hs->new_session->ocsp_response.reset(
  747. CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool));
  748. if (hs->new_session->ocsp_response == nullptr) {
  749. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  750. return ssl_hs_error;
  751. }
  752. ssl->method->next_message(ssl);
  753. hs->state = state_verify_server_certificate;
  754. return ssl_hs_ok;
  755. }
  756. static enum ssl_hs_wait_t do_verify_server_certificate(SSL_HANDSHAKE *hs) {
  757. if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  758. hs->state = state_read_server_key_exchange;
  759. return ssl_hs_ok;
  760. }
  761. switch (ssl_verify_peer_cert(hs)) {
  762. case ssl_verify_ok:
  763. break;
  764. case ssl_verify_invalid:
  765. return ssl_hs_error;
  766. case ssl_verify_retry:
  767. hs->state = state_verify_server_certificate;
  768. return ssl_hs_certificate_verify;
  769. }
  770. hs->state = state_read_server_key_exchange;
  771. return ssl_hs_ok;
  772. }
  773. static enum ssl_hs_wait_t do_reverify_server_certificate(SSL_HANDSHAKE *hs) {
  774. assert(hs->ssl->ctx->reverify_on_resume);
  775. switch (ssl_reverify_peer_cert(hs)) {
  776. case ssl_verify_ok:
  777. break;
  778. case ssl_verify_invalid:
  779. return ssl_hs_error;
  780. case ssl_verify_retry:
  781. hs->state = state_reverify_server_certificate;
  782. return ssl_hs_certificate_verify;
  783. }
  784. hs->state = state_read_session_ticket;
  785. return ssl_hs_ok;
  786. }
  787. static enum ssl_hs_wait_t do_read_server_key_exchange(SSL_HANDSHAKE *hs) {
  788. SSL *const ssl = hs->ssl;
  789. SSLMessage msg;
  790. if (!ssl->method->get_message(ssl, &msg)) {
  791. return ssl_hs_read_message;
  792. }
  793. if (msg.type != SSL3_MT_SERVER_KEY_EXCHANGE) {
  794. // Some ciphers (pure PSK) have an optional ServerKeyExchange message.
  795. if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
  796. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  797. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  798. return ssl_hs_error;
  799. }
  800. hs->state = state_read_certificate_request;
  801. return ssl_hs_ok;
  802. }
  803. if (!ssl_hash_message(hs, msg)) {
  804. return ssl_hs_error;
  805. }
  806. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  807. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  808. CBS server_key_exchange = msg.body;
  809. if (alg_a & SSL_aPSK) {
  810. CBS psk_identity_hint;
  811. // Each of the PSK key exchanges begins with a psk_identity_hint.
  812. if (!CBS_get_u16_length_prefixed(&server_key_exchange,
  813. &psk_identity_hint)) {
  814. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  815. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  816. return ssl_hs_error;
  817. }
  818. // Store the PSK identity hint for the ClientKeyExchange. Assume that the
  819. // maximum length of a PSK identity hint can be as long as the maximum
  820. // length of a PSK identity. Also do not allow NULL characters; identities
  821. // are saved as C strings.
  822. //
  823. // TODO(davidben): Should invalid hints be ignored? It's a hint rather than
  824. // a specific identity.
  825. if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
  826. CBS_contains_zero_byte(&psk_identity_hint)) {
  827. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  828. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  829. return ssl_hs_error;
  830. }
  831. // Save non-empty identity hints as a C string. Empty identity hints we
  832. // treat as missing. Plain PSK makes it possible to send either no hint
  833. // (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
  834. // empty hint. Having different capabilities is odd, so we interpret empty
  835. // and missing as identical.
  836. char *raw = nullptr;
  837. if (CBS_len(&psk_identity_hint) != 0 &&
  838. !CBS_strdup(&psk_identity_hint, &raw)) {
  839. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  840. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  841. return ssl_hs_error;
  842. }
  843. hs->peer_psk_identity_hint.reset(raw);
  844. }
  845. if (alg_k & SSL_kECDHE) {
  846. // Parse the server parameters.
  847. uint8_t group_type;
  848. uint16_t group_id;
  849. CBS point;
  850. if (!CBS_get_u8(&server_key_exchange, &group_type) ||
  851. group_type != NAMED_CURVE_TYPE ||
  852. !CBS_get_u16(&server_key_exchange, &group_id) ||
  853. !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
  854. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  855. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  856. return ssl_hs_error;
  857. }
  858. hs->new_session->group_id = group_id;
  859. // Ensure the group is consistent with preferences.
  860. if (!tls1_check_group_id(hs, group_id)) {
  861. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
  862. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  863. return ssl_hs_error;
  864. }
  865. // Initialize ECDH and save the peer public key for later.
  866. hs->key_share = SSLKeyShare::Create(group_id);
  867. if (!hs->key_share ||
  868. !hs->peer_key.CopyFrom(point)) {
  869. return ssl_hs_error;
  870. }
  871. } else if (!(alg_k & SSL_kPSK)) {
  872. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  873. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  874. return ssl_hs_error;
  875. }
  876. // At this point, |server_key_exchange| contains the signature, if any, while
  877. // |msg.body| contains the entire message. From that, derive a CBS containing
  878. // just the parameter.
  879. CBS parameter;
  880. CBS_init(&parameter, CBS_data(&msg.body),
  881. CBS_len(&msg.body) - CBS_len(&server_key_exchange));
  882. // ServerKeyExchange should be signed by the server's public key.
  883. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  884. uint16_t signature_algorithm = 0;
  885. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  886. if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
  887. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  888. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  889. return ssl_hs_error;
  890. }
  891. uint8_t alert = SSL_AD_DECODE_ERROR;
  892. if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
  893. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  894. return ssl_hs_error;
  895. }
  896. hs->new_session->peer_signature_algorithm = signature_algorithm;
  897. } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
  898. hs->peer_pubkey.get())) {
  899. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
  900. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
  901. return ssl_hs_error;
  902. }
  903. // The last field in |server_key_exchange| is the signature.
  904. CBS signature;
  905. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
  906. CBS_len(&server_key_exchange) != 0) {
  907. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  908. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  909. return ssl_hs_error;
  910. }
  911. ScopedCBB transcript;
  912. Array<uint8_t> transcript_data;
  913. if (!CBB_init(transcript.get(),
  914. 2 * SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
  915. !CBB_add_bytes(transcript.get(), ssl->s3->client_random,
  916. SSL3_RANDOM_SIZE) ||
  917. !CBB_add_bytes(transcript.get(), ssl->s3->server_random,
  918. SSL3_RANDOM_SIZE) ||
  919. !CBB_add_bytes(transcript.get(), CBS_data(&parameter),
  920. CBS_len(&parameter)) ||
  921. !CBBFinishArray(transcript.get(), &transcript_data)) {
  922. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  923. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  924. return ssl_hs_error;
  925. }
  926. bool sig_ok = ssl_public_key_verify(ssl, signature, signature_algorithm,
  927. hs->peer_pubkey.get(), transcript_data);
  928. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  929. sig_ok = true;
  930. ERR_clear_error();
  931. #endif
  932. if (!sig_ok) {
  933. // bad signature
  934. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
  935. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  936. return ssl_hs_error;
  937. }
  938. } else {
  939. // PSK ciphers are the only supported certificate-less ciphers.
  940. assert(alg_a == SSL_aPSK);
  941. if (CBS_len(&server_key_exchange) > 0) {
  942. OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
  943. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  944. return ssl_hs_error;
  945. }
  946. }
  947. ssl->method->next_message(ssl);
  948. hs->state = state_read_certificate_request;
  949. return ssl_hs_ok;
  950. }
  951. static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) {
  952. SSL *const ssl = hs->ssl;
  953. if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  954. hs->state = state_read_server_hello_done;
  955. return ssl_hs_ok;
  956. }
  957. SSLMessage msg;
  958. if (!ssl->method->get_message(ssl, &msg)) {
  959. return ssl_hs_read_message;
  960. }
  961. if (msg.type == SSL3_MT_SERVER_HELLO_DONE) {
  962. // If we get here we don't need the handshake buffer as we won't be doing
  963. // client auth.
  964. hs->transcript.FreeBuffer();
  965. hs->state = state_read_server_hello_done;
  966. return ssl_hs_ok;
  967. }
  968. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_REQUEST) ||
  969. !ssl_hash_message(hs, msg)) {
  970. return ssl_hs_error;
  971. }
  972. // Get the certificate types.
  973. CBS body = msg.body, certificate_types;
  974. if (!CBS_get_u8_length_prefixed(&body, &certificate_types)) {
  975. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  976. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  977. return ssl_hs_error;
  978. }
  979. if (!hs->certificate_types.CopyFrom(certificate_types)) {
  980. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  981. return ssl_hs_error;
  982. }
  983. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  984. CBS supported_signature_algorithms;
  985. if (!CBS_get_u16_length_prefixed(&body, &supported_signature_algorithms) ||
  986. !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
  987. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  988. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  989. return ssl_hs_error;
  990. }
  991. }
  992. uint8_t alert = SSL_AD_DECODE_ERROR;
  993. UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names =
  994. ssl_parse_client_CA_list(ssl, &alert, &body);
  995. if (!ca_names) {
  996. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  997. return ssl_hs_error;
  998. }
  999. if (CBS_len(&body) != 0) {
  1000. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1001. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1002. return ssl_hs_error;
  1003. }
  1004. hs->cert_request = true;
  1005. hs->ca_names = std::move(ca_names);
  1006. ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
  1007. ssl->method->next_message(ssl);
  1008. hs->state = state_read_server_hello_done;
  1009. return ssl_hs_ok;
  1010. }
  1011. static enum ssl_hs_wait_t do_read_server_hello_done(SSL_HANDSHAKE *hs) {
  1012. SSL *const ssl = hs->ssl;
  1013. SSLMessage msg;
  1014. if (!ssl->method->get_message(ssl, &msg)) {
  1015. return ssl_hs_read_message;
  1016. }
  1017. if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO_DONE) ||
  1018. !ssl_hash_message(hs, msg)) {
  1019. return ssl_hs_error;
  1020. }
  1021. // ServerHelloDone is empty.
  1022. if (CBS_len(&msg.body) != 0) {
  1023. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1024. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1025. return ssl_hs_error;
  1026. }
  1027. ssl->method->next_message(ssl);
  1028. hs->state = state_send_client_certificate;
  1029. return ssl_hs_ok;
  1030. }
  1031. static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
  1032. SSL *const ssl = hs->ssl;
  1033. // The peer didn't request a certificate.
  1034. if (!hs->cert_request) {
  1035. hs->state = state_send_client_key_exchange;
  1036. return ssl_hs_ok;
  1037. }
  1038. // Call cert_cb to update the certificate.
  1039. if (hs->config->cert->cert_cb != NULL) {
  1040. int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg);
  1041. if (rv == 0) {
  1042. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1043. OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
  1044. return ssl_hs_error;
  1045. }
  1046. if (rv < 0) {
  1047. hs->state = state_send_client_certificate;
  1048. return ssl_hs_x509_lookup;
  1049. }
  1050. }
  1051. if (!ssl_has_certificate(hs->config)) {
  1052. // Without a client certificate, the handshake buffer may be released.
  1053. hs->transcript.FreeBuffer();
  1054. }
  1055. if (!ssl_on_certificate_selected(hs) ||
  1056. !ssl_output_cert_chain(hs)) {
  1057. return ssl_hs_error;
  1058. }
  1059. hs->state = state_send_client_key_exchange;
  1060. return ssl_hs_ok;
  1061. }
  1062. static_assert(sizeof(size_t) >= sizeof(unsigned),
  1063. "size_t is smaller than unsigned");
  1064. static enum ssl_hs_wait_t do_send_client_key_exchange(SSL_HANDSHAKE *hs) {
  1065. SSL *const ssl = hs->ssl;
  1066. ScopedCBB cbb;
  1067. CBB body;
  1068. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  1069. SSL3_MT_CLIENT_KEY_EXCHANGE)) {
  1070. return ssl_hs_error;
  1071. }
  1072. Array<uint8_t> pms;
  1073. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  1074. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  1075. // If using a PSK key exchange, prepare the pre-shared key.
  1076. unsigned psk_len = 0;
  1077. uint8_t psk[PSK_MAX_PSK_LEN];
  1078. if (alg_a & SSL_aPSK) {
  1079. if (hs->config->psk_client_callback == NULL) {
  1080. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
  1081. return ssl_hs_error;
  1082. }
  1083. char identity[PSK_MAX_IDENTITY_LEN + 1];
  1084. OPENSSL_memset(identity, 0, sizeof(identity));
  1085. psk_len = hs->config->psk_client_callback(
  1086. ssl, hs->peer_psk_identity_hint.get(), identity, sizeof(identity), psk,
  1087. sizeof(psk));
  1088. if (psk_len == 0) {
  1089. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1090. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1091. return ssl_hs_error;
  1092. }
  1093. assert(psk_len <= PSK_MAX_PSK_LEN);
  1094. hs->new_session->psk_identity.reset(BUF_strdup(identity));
  1095. if (hs->new_session->psk_identity == nullptr) {
  1096. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1097. return ssl_hs_error;
  1098. }
  1099. // Write out psk_identity.
  1100. CBB child;
  1101. if (!CBB_add_u16_length_prefixed(&body, &child) ||
  1102. !CBB_add_bytes(&child, (const uint8_t *)identity,
  1103. OPENSSL_strnlen(identity, sizeof(identity))) ||
  1104. !CBB_flush(&body)) {
  1105. return ssl_hs_error;
  1106. }
  1107. }
  1108. // Depending on the key exchange method, compute |pms|.
  1109. if (alg_k & SSL_kRSA) {
  1110. if (!pms.Init(SSL_MAX_MASTER_KEY_LENGTH)) {
  1111. return ssl_hs_error;
  1112. }
  1113. RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey.get());
  1114. if (rsa == NULL) {
  1115. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1116. return ssl_hs_error;
  1117. }
  1118. pms[0] = hs->client_version >> 8;
  1119. pms[1] = hs->client_version & 0xff;
  1120. if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
  1121. return ssl_hs_error;
  1122. }
  1123. CBB enc_pms;
  1124. uint8_t *ptr;
  1125. size_t enc_pms_len;
  1126. if (!CBB_add_u16_length_prefixed(&body, &enc_pms) ||
  1127. !CBB_reserve(&enc_pms, &ptr, RSA_size(rsa)) ||
  1128. !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms.data(),
  1129. pms.size(), RSA_PKCS1_PADDING) ||
  1130. !CBB_did_write(&enc_pms, enc_pms_len) ||
  1131. !CBB_flush(&body)) {
  1132. return ssl_hs_error;
  1133. }
  1134. } else if (alg_k & SSL_kECDHE) {
  1135. // Generate a keypair and serialize the public half.
  1136. CBB child;
  1137. if (!CBB_add_u8_length_prefixed(&body, &child)) {
  1138. return ssl_hs_error;
  1139. }
  1140. // Compute the premaster.
  1141. uint8_t alert = SSL_AD_DECODE_ERROR;
  1142. if (!hs->key_share->Accept(&child, &pms, &alert, hs->peer_key)) {
  1143. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  1144. return ssl_hs_error;
  1145. }
  1146. if (!CBB_flush(&body)) {
  1147. return ssl_hs_error;
  1148. }
  1149. // The key exchange state may now be discarded.
  1150. hs->key_share.reset();
  1151. hs->peer_key.Reset();
  1152. } else if (alg_k & SSL_kPSK) {
  1153. // For plain PSK, other_secret is a block of 0s with the same length as
  1154. // the pre-shared key.
  1155. if (!pms.Init(psk_len)) {
  1156. return ssl_hs_error;
  1157. }
  1158. OPENSSL_memset(pms.data(), 0, pms.size());
  1159. } else {
  1160. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1161. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1162. return ssl_hs_error;
  1163. }
  1164. // For a PSK cipher suite, other_secret is combined with the pre-shared
  1165. // key.
  1166. if (alg_a & SSL_aPSK) {
  1167. ScopedCBB pms_cbb;
  1168. CBB child;
  1169. if (!CBB_init(pms_cbb.get(), 2 + psk_len + 2 + pms.size()) ||
  1170. !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
  1171. !CBB_add_bytes(&child, pms.data(), pms.size()) ||
  1172. !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
  1173. !CBB_add_bytes(&child, psk, psk_len) ||
  1174. !CBBFinishArray(pms_cbb.get(), &pms)) {
  1175. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1176. return ssl_hs_error;
  1177. }
  1178. }
  1179. // The message must be added to the finished hash before calculating the
  1180. // master secret.
  1181. if (!ssl_add_message_cbb(ssl, cbb.get())) {
  1182. return ssl_hs_error;
  1183. }
  1184. hs->new_session->master_key_length =
  1185. tls1_generate_master_secret(hs, hs->new_session->master_key, pms);
  1186. if (hs->new_session->master_key_length == 0) {
  1187. return ssl_hs_error;
  1188. }
  1189. hs->new_session->extended_master_secret = hs->extended_master_secret;
  1190. hs->state = state_send_client_certificate_verify;
  1191. return ssl_hs_ok;
  1192. }
  1193. static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) {
  1194. SSL *const ssl = hs->ssl;
  1195. if (!hs->cert_request || !ssl_has_certificate(hs->config)) {
  1196. hs->state = state_send_client_finished;
  1197. return ssl_hs_ok;
  1198. }
  1199. assert(ssl_has_private_key(hs->config));
  1200. ScopedCBB cbb;
  1201. CBB body, child;
  1202. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  1203. SSL3_MT_CERTIFICATE_VERIFY)) {
  1204. return ssl_hs_error;
  1205. }
  1206. uint16_t signature_algorithm;
  1207. if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
  1208. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1209. return ssl_hs_error;
  1210. }
  1211. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  1212. // Write out the digest type in TLS 1.2.
  1213. if (!CBB_add_u16(&body, signature_algorithm)) {
  1214. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1215. return ssl_hs_error;
  1216. }
  1217. }
  1218. // Set aside space for the signature.
  1219. const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
  1220. uint8_t *ptr;
  1221. if (!CBB_add_u16_length_prefixed(&body, &child) ||
  1222. !CBB_reserve(&child, &ptr, max_sig_len)) {
  1223. return ssl_hs_error;
  1224. }
  1225. size_t sig_len = max_sig_len;
  1226. switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
  1227. signature_algorithm,
  1228. hs->transcript.buffer())) {
  1229. case ssl_private_key_success:
  1230. break;
  1231. case ssl_private_key_failure:
  1232. return ssl_hs_error;
  1233. case ssl_private_key_retry:
  1234. hs->state = state_send_client_certificate_verify;
  1235. return ssl_hs_private_key_operation;
  1236. }
  1237. if (!CBB_did_write(&child, sig_len) ||
  1238. !ssl_add_message_cbb(ssl, cbb.get())) {
  1239. return ssl_hs_error;
  1240. }
  1241. // The handshake buffer is no longer necessary.
  1242. hs->transcript.FreeBuffer();
  1243. hs->state = state_send_client_finished;
  1244. return ssl_hs_ok;
  1245. }
  1246. static enum ssl_hs_wait_t do_send_client_finished(SSL_HANDSHAKE *hs) {
  1247. SSL *const ssl = hs->ssl;
  1248. // Resolve Channel ID first, before any non-idempotent operations.
  1249. if (ssl->s3->channel_id_valid) {
  1250. if (!ssl_do_channel_id_callback(hs)) {
  1251. return ssl_hs_error;
  1252. }
  1253. if (hs->config->channel_id_private == NULL) {
  1254. hs->state = state_send_client_finished;
  1255. return ssl_hs_channel_id_lookup;
  1256. }
  1257. }
  1258. if (!ssl->method->add_change_cipher_spec(ssl) ||
  1259. !tls1_change_cipher_state(hs, evp_aead_seal)) {
  1260. return ssl_hs_error;
  1261. }
  1262. if (hs->next_proto_neg_seen) {
  1263. static const uint8_t kZero[32] = {0};
  1264. size_t padding_len =
  1265. 32 - ((ssl->s3->next_proto_negotiated.size() + 2) % 32);
  1266. ScopedCBB cbb;
  1267. CBB body, child;
  1268. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_NEXT_PROTO) ||
  1269. !CBB_add_u8_length_prefixed(&body, &child) ||
  1270. !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated.data(),
  1271. ssl->s3->next_proto_negotiated.size()) ||
  1272. !CBB_add_u8_length_prefixed(&body, &child) ||
  1273. !CBB_add_bytes(&child, kZero, padding_len) ||
  1274. !ssl_add_message_cbb(ssl, cbb.get())) {
  1275. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1276. return ssl_hs_error;
  1277. }
  1278. }
  1279. if (ssl->s3->channel_id_valid) {
  1280. ScopedCBB cbb;
  1281. CBB body;
  1282. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) ||
  1283. !tls1_write_channel_id(hs, &body) ||
  1284. !ssl_add_message_cbb(ssl, cbb.get())) {
  1285. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1286. return ssl_hs_error;
  1287. }
  1288. }
  1289. if (!ssl_send_finished(hs)) {
  1290. return ssl_hs_error;
  1291. }
  1292. hs->state = state_finish_flight;
  1293. return ssl_hs_flush;
  1294. }
  1295. static bool can_false_start(const SSL_HANDSHAKE *hs) {
  1296. SSL *const ssl = hs->ssl;
  1297. // False Start bypasses the Finished check's downgrade protection. This can
  1298. // enable attacks where we send data under weaker settings than supported
  1299. // (e.g. the Logjam attack). Thus we require TLS 1.2 with an ECDHE+AEAD
  1300. // cipher, our strongest settings before TLS 1.3.
  1301. //
  1302. // Now that TLS 1.3 exists, we would like to avoid similar attacks between
  1303. // TLS 1.2 and TLS 1.3, but there are too many TLS 1.2 deployments to
  1304. // sacrifice False Start on them. TLS 1.3's downgrade signal fixes this, but
  1305. // |SSL_CTX_set_ignore_tls13_downgrade| can disable it due to compatibility
  1306. // issues.
  1307. //
  1308. // |SSL_CTX_set_ignore_tls13_downgrade| normally still retains Finished-based
  1309. // downgrade protection, but False Start bypasses that. Thus, we disable False
  1310. // Start based on the TLS 1.3 downgrade signal, even if otherwise unenforced.
  1311. if (SSL_is_dtls(ssl) ||
  1312. SSL_version(ssl) != TLS1_2_VERSION ||
  1313. hs->new_cipher->algorithm_mkey != SSL_kECDHE ||
  1314. hs->new_cipher->algorithm_mac != SSL_AEAD ||
  1315. ssl->s3->tls13_downgrade) {
  1316. return false;
  1317. }
  1318. // Additionally require ALPN or NPN by default.
  1319. //
  1320. // TODO(davidben): Can this constraint be relaxed globally now that cipher
  1321. // suite requirements have been tightened?
  1322. if (!ssl->ctx->false_start_allowed_without_alpn &&
  1323. ssl->s3->alpn_selected.empty() &&
  1324. ssl->s3->next_proto_negotiated.empty()) {
  1325. return false;
  1326. }
  1327. return true;
  1328. }
  1329. static enum ssl_hs_wait_t do_finish_flight(SSL_HANDSHAKE *hs) {
  1330. SSL *const ssl = hs->ssl;
  1331. if (ssl->session != NULL) {
  1332. hs->state = state_finish_client_handshake;
  1333. return ssl_hs_ok;
  1334. }
  1335. // This is a full handshake. If it involves ChannelID, then record the
  1336. // handshake hashes at this point in the session so that any resumption of
  1337. // this session with ChannelID can sign those hashes.
  1338. if (!tls1_record_handshake_hashes_for_channel_id(hs)) {
  1339. return ssl_hs_error;
  1340. }
  1341. hs->state = state_read_session_ticket;
  1342. if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
  1343. can_false_start(hs) &&
  1344. // No False Start on renegotiation (would complicate the state machine).
  1345. !ssl->s3->initial_handshake_complete) {
  1346. hs->in_false_start = true;
  1347. hs->can_early_write = true;
  1348. return ssl_hs_early_return;
  1349. }
  1350. return ssl_hs_ok;
  1351. }
  1352. static enum ssl_hs_wait_t do_read_session_ticket(SSL_HANDSHAKE *hs) {
  1353. SSL *const ssl = hs->ssl;
  1354. if (!hs->ticket_expected) {
  1355. hs->state = state_process_change_cipher_spec;
  1356. return ssl_hs_read_change_cipher_spec;
  1357. }
  1358. SSLMessage msg;
  1359. if (!ssl->method->get_message(ssl, &msg)) {
  1360. return ssl_hs_read_message;
  1361. }
  1362. if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEW_SESSION_TICKET) ||
  1363. !ssl_hash_message(hs, msg)) {
  1364. return ssl_hs_error;
  1365. }
  1366. CBS new_session_ticket = msg.body, ticket;
  1367. uint32_t ticket_lifetime_hint;
  1368. if (!CBS_get_u32(&new_session_ticket, &ticket_lifetime_hint) ||
  1369. !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
  1370. CBS_len(&new_session_ticket) != 0) {
  1371. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1372. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1373. return ssl_hs_error;
  1374. }
  1375. if (CBS_len(&ticket) == 0) {
  1376. // RFC 5077 allows a server to change its mind and send no ticket after
  1377. // negotiating the extension. The value of |ticket_expected| is checked in
  1378. // |ssl_update_cache| so is cleared here to avoid an unnecessary update.
  1379. hs->ticket_expected = false;
  1380. ssl->method->next_message(ssl);
  1381. hs->state = state_process_change_cipher_spec;
  1382. return ssl_hs_read_change_cipher_spec;
  1383. }
  1384. SSL_SESSION *session = hs->new_session.get();
  1385. UniquePtr<SSL_SESSION> renewed_session;
  1386. if (ssl->session != NULL) {
  1387. // The server is sending a new ticket for an existing session. Sessions are
  1388. // immutable once established, so duplicate all but the ticket of the
  1389. // existing session.
  1390. renewed_session =
  1391. SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_INCLUDE_NONAUTH);
  1392. if (!renewed_session) {
  1393. // This should never happen.
  1394. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1395. return ssl_hs_error;
  1396. }
  1397. session = renewed_session.get();
  1398. }
  1399. // |ticket_lifetime_hint| is measured from when the ticket was issued.
  1400. ssl_session_rebase_time(ssl, session);
  1401. if (!session->ticket.CopyFrom(ticket)) {
  1402. return ssl_hs_error;
  1403. }
  1404. session->ticket_lifetime_hint = ticket_lifetime_hint;
  1405. // Generate a session ID for this session based on the session ticket. We use
  1406. // the session ID mechanism for detecting ticket resumption. This also fits in
  1407. // with assumptions elsewhere in OpenSSL.
  1408. if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
  1409. session->session_id, &session->session_id_length,
  1410. EVP_sha256(), NULL)) {
  1411. return ssl_hs_error;
  1412. }
  1413. if (renewed_session) {
  1414. session->not_resumable = false;
  1415. ssl->session = std::move(renewed_session);
  1416. }
  1417. ssl->method->next_message(ssl);
  1418. hs->state = state_process_change_cipher_spec;
  1419. return ssl_hs_read_change_cipher_spec;
  1420. }
  1421. static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
  1422. if (!tls1_change_cipher_state(hs, evp_aead_open)) {
  1423. return ssl_hs_error;
  1424. }
  1425. hs->state = state_read_server_finished;
  1426. return ssl_hs_ok;
  1427. }
  1428. static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) {
  1429. SSL *const ssl = hs->ssl;
  1430. enum ssl_hs_wait_t wait = ssl_get_finished(hs);
  1431. if (wait != ssl_hs_ok) {
  1432. return wait;
  1433. }
  1434. if (ssl->session != NULL) {
  1435. hs->state = state_send_client_finished;
  1436. return ssl_hs_ok;
  1437. }
  1438. hs->state = state_finish_client_handshake;
  1439. return ssl_hs_ok;
  1440. }
  1441. static enum ssl_hs_wait_t do_finish_client_handshake(SSL_HANDSHAKE *hs) {
  1442. SSL *const ssl = hs->ssl;
  1443. ssl->method->on_handshake_complete(ssl);
  1444. if (ssl->session != NULL) {
  1445. ssl->s3->established_session = UpRef(ssl->session);
  1446. } else {
  1447. // We make a copy of the session in order to maintain the immutability
  1448. // of the new established_session due to False Start. The caller may
  1449. // have taken a reference to the temporary session.
  1450. ssl->s3->established_session =
  1451. SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_DUP_ALL);
  1452. if (!ssl->s3->established_session) {
  1453. return ssl_hs_error;
  1454. }
  1455. // Renegotiations do not participate in session resumption.
  1456. if (!ssl->s3->initial_handshake_complete) {
  1457. ssl->s3->established_session->not_resumable = false;
  1458. }
  1459. hs->new_session.reset();
  1460. }
  1461. hs->handshake_finalized = true;
  1462. ssl->s3->initial_handshake_complete = true;
  1463. ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
  1464. hs->state = state_done;
  1465. return ssl_hs_ok;
  1466. }
  1467. enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs) {
  1468. while (hs->state != state_done) {
  1469. enum ssl_hs_wait_t ret = ssl_hs_error;
  1470. enum ssl_client_hs_state_t state =
  1471. static_cast<enum ssl_client_hs_state_t>(hs->state);
  1472. switch (state) {
  1473. case state_start_connect:
  1474. ret = do_start_connect(hs);
  1475. break;
  1476. case state_enter_early_data:
  1477. ret = do_enter_early_data(hs);
  1478. break;
  1479. case state_read_hello_verify_request:
  1480. ret = do_read_hello_verify_request(hs);
  1481. break;
  1482. case state_read_server_hello:
  1483. ret = do_read_server_hello(hs);
  1484. break;
  1485. case state_tls13:
  1486. ret = do_tls13(hs);
  1487. break;
  1488. case state_read_server_certificate:
  1489. ret = do_read_server_certificate(hs);
  1490. break;
  1491. case state_read_certificate_status:
  1492. ret = do_read_certificate_status(hs);
  1493. break;
  1494. case state_verify_server_certificate:
  1495. ret = do_verify_server_certificate(hs);
  1496. break;
  1497. case state_reverify_server_certificate:
  1498. ret = do_reverify_server_certificate(hs);
  1499. break;
  1500. case state_read_server_key_exchange:
  1501. ret = do_read_server_key_exchange(hs);
  1502. break;
  1503. case state_read_certificate_request:
  1504. ret = do_read_certificate_request(hs);
  1505. break;
  1506. case state_read_server_hello_done:
  1507. ret = do_read_server_hello_done(hs);
  1508. break;
  1509. case state_send_client_certificate:
  1510. ret = do_send_client_certificate(hs);
  1511. break;
  1512. case state_send_client_key_exchange:
  1513. ret = do_send_client_key_exchange(hs);
  1514. break;
  1515. case state_send_client_certificate_verify:
  1516. ret = do_send_client_certificate_verify(hs);
  1517. break;
  1518. case state_send_client_finished:
  1519. ret = do_send_client_finished(hs);
  1520. break;
  1521. case state_finish_flight:
  1522. ret = do_finish_flight(hs);
  1523. break;
  1524. case state_read_session_ticket:
  1525. ret = do_read_session_ticket(hs);
  1526. break;
  1527. case state_process_change_cipher_spec:
  1528. ret = do_process_change_cipher_spec(hs);
  1529. break;
  1530. case state_read_server_finished:
  1531. ret = do_read_server_finished(hs);
  1532. break;
  1533. case state_finish_client_handshake:
  1534. ret = do_finish_client_handshake(hs);
  1535. break;
  1536. case state_done:
  1537. ret = ssl_hs_ok;
  1538. break;
  1539. }
  1540. if (hs->state != state) {
  1541. ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1);
  1542. }
  1543. if (ret != ssl_hs_ok) {
  1544. return ret;
  1545. }
  1546. }
  1547. ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
  1548. return ssl_hs_ok;
  1549. }
  1550. const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs) {
  1551. enum ssl_client_hs_state_t state =
  1552. static_cast<enum ssl_client_hs_state_t>(hs->state);
  1553. switch (state) {
  1554. case state_start_connect:
  1555. return "TLS client start_connect";
  1556. case state_enter_early_data:
  1557. return "TLS client enter_early_data";
  1558. case state_read_hello_verify_request:
  1559. return "TLS client read_hello_verify_request";
  1560. case state_read_server_hello:
  1561. return "TLS client read_server_hello";
  1562. case state_tls13:
  1563. return tls13_client_handshake_state(hs);
  1564. case state_read_server_certificate:
  1565. return "TLS client read_server_certificate";
  1566. case state_read_certificate_status:
  1567. return "TLS client read_certificate_status";
  1568. case state_verify_server_certificate:
  1569. return "TLS client verify_server_certificate";
  1570. case state_reverify_server_certificate:
  1571. return "TLS client reverify_server_certificate";
  1572. case state_read_server_key_exchange:
  1573. return "TLS client read_server_key_exchange";
  1574. case state_read_certificate_request:
  1575. return "TLS client read_certificate_request";
  1576. case state_read_server_hello_done:
  1577. return "TLS client read_server_hello_done";
  1578. case state_send_client_certificate:
  1579. return "TLS client send_client_certificate";
  1580. case state_send_client_key_exchange:
  1581. return "TLS client send_client_key_exchange";
  1582. case state_send_client_certificate_verify:
  1583. return "TLS client send_client_certificate_verify";
  1584. case state_send_client_finished:
  1585. return "TLS client send_client_finished";
  1586. case state_finish_flight:
  1587. return "TLS client finish_flight";
  1588. case state_read_session_ticket:
  1589. return "TLS client read_session_ticket";
  1590. case state_process_change_cipher_spec:
  1591. return "TLS client process_change_cipher_spec";
  1592. case state_read_server_finished:
  1593. return "TLS client read_server_finished";
  1594. case state_finish_client_handshake:
  1595. return "TLS client finish_client_handshake";
  1596. case state_done:
  1597. return "TLS client done";
  1598. }
  1599. return "TLS client unknown";
  1600. }
  1601. }