Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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