Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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