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.
 
 
 
 
 
 

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