Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/ssl/handshake_client.cc

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected])
2 // Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
3 // Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4 // Copyright 2005 Nokia. All rights reserved.
5 //
6 // ECC cipher suite support in OpenSSL originally written by
7 // Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
8 //
9 // The Contribution, originally written by Mika Kousa and Pasi Eronen of
10 // Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
11 // support (see RFC 4279) to OpenSSL.
12 //
13 // SPDX-License-Identifier: Apache-2.0
14
15 #include <openssl/ssl.h>
16
17 #include <assert.h>
18 #include <limits.h>
19 #include <string.h>
20
21 #include <utility>
22
23 #include <openssl/aead.h>
24 #include <openssl/bn.h>
25 #include <openssl/bytestring.h>
26 #include <openssl/ec_key.h>
27 #include <openssl/ecdsa.h>
28 #include <openssl/err.h>
29 #include <openssl/evp.h>
30 #include <openssl/hpke.h>
31 #include <openssl/md5.h>
32 #include <openssl/mem.h>
33 #include <openssl/rand.h>
34 #include <openssl/sha.h>
35
36 #include "../crypto/internal.h"
37 #include "internal.h"
38
39
40 BSSL_NAMESPACE_BEGIN
41
42 enum ssl_client_hs_state_t {
43 state_start_connect = 0,
44 state_enter_early_data,
45 state_early_reverify_server_certificate,
46 state_read_hello_verify_request,
47 state_read_server_hello,
48 state_tls13,
49 state_read_server_certificate,
50 state_read_certificate_status,
51 state_verify_server_certificate,
52 state_reverify_server_certificate,
53 state_read_server_key_exchange,
54 state_read_certificate_request,
55 state_read_server_hello_done,
56 state_send_client_certificate,
57 state_send_client_key_exchange,
58 state_send_client_certificate_verify,
59 state_send_client_finished,
60 state_finish_flight,
61 state_read_session_ticket,
62 state_process_change_cipher_spec,
63 state_read_server_finished,
64 state_finish_client_handshake,
65 state_done,
66 };
67
68 // ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
69 // disabled algorithms.
70 static void ssl_get_client_disabled(const SSL_HANDSHAKE *hs,
71 uint32_t *out_mask_a,
72 uint32_t *out_mask_k) {
73 *out_mask_a = 0;
74 *out_mask_k = 0;
75
76 // PSK requires a client callback.
77 if (hs->config->psk_client_callback == NULL) {
78 *out_mask_a |= SSL_aPSK;
79 *out_mask_k |= SSL_kPSK;
80 }
81 }
82
83 // collect_cipher_protocol_ids uses |cbb| to collect IANA-assigned numbers
84 // of |ciphers|. |mask_a|, |mask_k|, |max_version| and |min_version|
85 // are used to filter the |ciphers|. |any_enabled| will be true if not all
86 // ciphers are filtered out.
87 // It returns true when success. It returns false otherwise.
88 static bool collect_cipher_protocol_ids(STACK_OF(SSL_CIPHER) *ciphers,
89 CBB *cbb, uint32_t mask_k, uint32_t mask_a, uint16_t max_version,
90 uint16_t min_version, bool *any_enabled) {
91 *any_enabled = false;
92
93 for (const SSL_CIPHER *cipher : ciphers) {
94 // Skip disabled ciphers
95 if ((cipher->algorithm_mkey & mask_k) ||
96 (cipher->algorithm_auth & mask_a)) {
97 continue;
98 }
99 if (SSL_CIPHER_get_min_version(cipher) > max_version ||
100 SSL_CIPHER_get_max_version(cipher) < min_version) {
101 continue;
102 }
103 *any_enabled = true;
104 if (!CBB_add_u16(cbb, SSL_CIPHER_get_protocol_id(cipher))) {
105 return false;
106 }
107 }
108 return true;
109 }
110
111 static bool ssl_write_client_cipher_list(const SSL_HANDSHAKE *hs, CBB *out,
112 ssl_client_hello_type_t type) {
113 const SSL *const ssl = hs->ssl;
114 uint32_t mask_a, mask_k;
115 ssl_get_client_disabled(hs, &mask_a, &mask_k);
116
117 CBB child;
118 if (!CBB_add_u16_length_prefixed(out, &child)) {
119 return false;
120 }
121
122 // Add a fake cipher suite. See RFC 8701.
123 if (ssl->ctx->grease_enabled &&
124 !CBB_add_u16(&child, ssl_get_grease_value(hs, ssl_grease_cipher))) {
125 return false;
126 }
127
128 // Add all ciphers unless TLS 1.3 only connection
129 if (hs->min_version < TLS1_3_VERSION && type != ssl_client_hello_inner) {
130 bool any_enabled = false;
131 if (!collect_cipher_protocol_ids(SSL_get_ciphers(ssl), &child, mask_k,
132 mask_a, hs->max_version, hs->min_version, &any_enabled)) {
133 return false;
134 }
135
136 // If all ciphers were disabled, return the error to the caller.
137 if (!any_enabled) {
138 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
139 return false;
140 }
141 } else if (hs->max_version >= TLS1_3_VERSION) {
142 // Only TLS 1.3 ciphers
143 STACK_OF(SSL_CIPHER) *ciphers = (ssl->config && ssl->config->tls13_cipher_list) ?
144 ssl->config->tls13_cipher_list->ciphers.get() : ssl->ctx->tls13_cipher_list->ciphers.get();
145
146 bool any_enabled = false;
147
148 if (!collect_cipher_protocol_ids(ciphers, &child, mask_k,
149 mask_a, hs->max_version, hs->min_version, &any_enabled)) {
150 return false;
151 }
152
153 // If all ciphers were disabled, return the error to the caller.
154 if (!any_enabled) {
155 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
156 return false;
157 }
158 }
159
160 if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
161 if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
162 return false;
163 }
164 }
165
166 return CBB_flush(out);
167 }
168
169 bool ssl_write_client_hello_without_extensions(const SSL_HANDSHAKE *hs,
170 CBB *cbb,
171 ssl_client_hello_type_t type,
172 bool empty_session_id) {
173 const SSL *const ssl = hs->ssl;
174 CBB child;
175 if (!CBB_add_u16(cbb, hs->client_version) ||
176 !CBB_add_bytes(cbb,
177 type == ssl_client_hello_inner ? hs->inner_client_random
178 : ssl->s3->client_random,
179 SSL3_RANDOM_SIZE) ||
180 !CBB_add_u8_length_prefixed(cbb, &child)) {
181 return false;
182 }
183
184 // Do not send a session ID on renegotiation.
185 if (!ssl->s3->initial_handshake_complete &&
186 !empty_session_id &&
187 !CBB_add_bytes(&child, hs->session_id, hs->session_id_len)) {
188 return false;
189 }
190
191 if (SSL_is_dtls(ssl)) {
192 if (!CBB_add_u8_length_prefixed(cbb, &child) ||
193 !CBB_add_bytes(&child, hs->dtls_cookie.data(),
194 hs->dtls_cookie.size())) {
195 return false;
196 }
197 }
198
199 if (!ssl_write_client_cipher_list(hs, cbb, type) ||
200 !CBB_add_u8(cbb, 1 /* one compression method */) ||
201 !CBB_add_u8(cbb, 0 /* null compression */)) {
202 return false;
203 }
204 return true;
205 }
206
207 bool ssl_add_client_hello(SSL_HANDSHAKE *hs) {
208 SSL *const ssl = hs->ssl;
209 ScopedCBB cbb;
210 CBB body;
211 ssl_client_hello_type_t type = hs->selected_ech_config
212 ? ssl_client_hello_outer
213 : ssl_client_hello_unencrypted;
214 bool needs_psk_binder;
215 Array<uint8_t> msg;
216 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO) ||
217 !ssl_write_client_hello_without_extensions(hs, &body, type,
218 /*empty_session_id=*/false) ||
219 !ssl_add_clienthello_tlsext(hs, &body, /*out_encoded=*/nullptr,
220 &needs_psk_binder, type, CBB_len(&body)) ||
221 !ssl->method->finish_message(ssl, cbb.get(), &msg)) {
222 return false;
223 }
224
225 // Now that the length prefixes have been computed, fill in the placeholder
226 // PSK binder.
227 if (needs_psk_binder) {
228 // ClientHelloOuter cannot have a PSK binder. Otherwise the
229 // ClientHellOuterAAD computation would break.
230 assert(type != ssl_client_hello_outer);
231 if (!tls13_write_psk_binder(hs, hs->transcript, MakeSpan(msg),
232 /*out_binder_len=*/0)) {
233 return false;
234 }
235 }
236
237 return ssl->method->add_message(ssl, std::move(msg));
238 }
239
240 static bool parse_server_version(const SSL_HANDSHAKE *hs, uint16_t *out_version,
241 uint8_t *out_alert,
242 const ParsedServerHello &server_hello) {
243 // If the outer version is not TLS 1.2, use it.
244 // TODO(davidben): This function doesn't quite match the RFC8446 formulation.
245 if (server_hello.legacy_version != TLS1_2_VERSION) {
246 *out_version = server_hello.legacy_version;
247 return true;
248 }
249
250 SSLExtension supported_versions(TLSEXT_TYPE_supported_versions);
251 CBS extensions = server_hello.extensions;
252 if (!ssl_parse_extensions(&extensions, out_alert, {&supported_versions},
253 /*ignore_unknown=*/true)) {
254 return false;
255 }
256
257 if (!supported_versions.present) {
258 *out_version = server_hello.legacy_version;
259 return true;
260 }
261
262 if (!CBS_get_u16(&supported_versions.data, out_version) ||
263 CBS_len(&supported_versions.data) != 0) {
264 *out_alert = SSL_AD_DECODE_ERROR;
265 return false;
266 }
267
268 return true;
269 }
270
271 // should_offer_early_data returns |ssl_early_data_accepted| if |hs| should
272 // offer early data, and some other reason code otherwise.
273 static ssl_early_data_reason_t should_offer_early_data(
274 const SSL_HANDSHAKE *hs) {
275 const SSL *const ssl = hs->ssl;
276 assert(!ssl->server);
277 if (!ssl->enable_early_data) {
278 return ssl_early_data_disabled;
279 }
280
281 if (hs->max_version < TLS1_3_VERSION) {
282 // We discard inapplicable sessions, so this is redundant with the session
283 // checks below, but reporting that TLS 1.3 was disabled is more useful.
284 return ssl_early_data_protocol_version;
285 }
286
287 if (ssl->session == nullptr) {
288 return ssl_early_data_no_session_offered;
289 }
290
291 if (ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION ||
292 ssl->session->ticket_max_early_data == 0) {
293 return ssl_early_data_unsupported_for_session;
294 }
295
296 if (!ssl->session->early_alpn.empty()) {
297 if (!ssl_is_alpn_protocol_allowed(hs, ssl->session->early_alpn)) {
298 // Avoid reporting a confusing value in |SSL_get0_alpn_selected|.
299 return ssl_early_data_alpn_mismatch;
300 }
301
302 // If the previous connection negotiated ALPS, only offer 0-RTT when the
303 // local are settings are consistent with what we'd offer for this
304 // connection.
305 if (ssl->session->has_application_settings) {
306 Span<const uint8_t> settings;
307 if (!ssl_get_local_application_settings(hs, &settings,
308 ssl->session->early_alpn) ||
309 settings != ssl->session->local_application_settings) {
310 return ssl_early_data_alps_mismatch;
311 }
312 }
313 }
314
315 // Early data has not yet been accepted, but we use it as a success code.
316 return ssl_early_data_accepted;
317 }
318
319 void ssl_done_writing_client_hello(SSL_HANDSHAKE *hs) {
320 hs->ech_client_outer.Reset();
321 hs->cookie.Reset();
322 hs->key_share_bytes.Reset();
323 }
324
325 static enum ssl_hs_wait_t do_start_connect(SSL_HANDSHAKE *hs) {
326 SSL *const ssl = hs->ssl;
327
328 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
329 ssl_update_counter(ssl->session_ctx.get(),
330 ssl->session_ctx->stats.sess_connect, true);
331 // |session_reused| must be reset in case this is a renegotiation.
332 ssl->s3->session_reused = false;
333
334 // Freeze the version range.
335 if (!ssl_get_version_range(hs, &hs->min_version, &hs->max_version)) {
336 return ssl_hs_error;
337 }
338
339 uint8_t ech_enc[EVP_HPKE_MAX_ENC_LENGTH];
340 size_t ech_enc_len;
341 if (!ssl_select_ech_config(hs, ech_enc, &ech_enc_len)) {
342 return ssl_hs_error;
343 }
344
345 // Always advertise the ClientHello version from the original maximum version,
346 // even on renegotiation. The static RSA key exchange uses this field, and
347 // some servers fail when it changes across handshakes.
348 if (SSL_is_dtls(hs->ssl)) {
349 hs->client_version =
350 hs->max_version >= TLS1_2_VERSION ? DTLS1_2_VERSION : DTLS1_VERSION;
351 } else {
352 hs->client_version =
353 hs->max_version >= TLS1_2_VERSION ? TLS1_2_VERSION : hs->max_version;
354 }
355
356 // If the configured session has expired or is not usable, drop it. We also do
357 // not offer sessions on renegotiation.
358 if (ssl->session != nullptr) {
359 if (ssl->session->is_server ||
360 !ssl_supports_version(hs, ssl->session->ssl_version) ||
361 // Do not offer TLS 1.2 sessions with ECH. ClientHelloInner does not
362 // offer TLS 1.2, and the cleartext session ID may leak the server
363 // identity.
364 (hs->selected_ech_config &&
365 ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION) ||
366 !SSL_SESSION_is_resumable(ssl->session.get()) ||
367 !ssl_session_is_time_valid(ssl, ssl->session.get()) ||
368 (ssl->quic_method != nullptr) != ssl->session->is_quic ||
369 ssl->s3->initial_handshake_complete) {
370 ssl_set_session(ssl, nullptr);
371 }
372 }
373
374 if (!RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
375 return ssl_hs_error;
376 }
377 if (hs->selected_ech_config &&
378 !RAND_bytes(hs->inner_client_random, sizeof(hs->inner_client_random))) {
379 return ssl_hs_error;
380 }
381
382 // Never send a session ID in QUIC. QUIC uses TLS 1.3 at a minimum and
383 // disables TLS 1.3 middlebox compatibility mode.
384 if (ssl->quic_method == nullptr) {
385 const bool has_id_session = ssl->session != nullptr &&
386 ssl->session->session_id_length > 0 &&
387 ssl->session->ticket.empty();
388 const bool has_ticket_session =
389 ssl->session != nullptr && !ssl->session->ticket.empty();
390 if (has_id_session) {
391 hs->session_id_len = ssl->session->session_id_length;
392 OPENSSL_memcpy(hs->session_id, ssl->session->session_id,
393 hs->session_id_len);
394 } else if (has_ticket_session || hs->max_version >= TLS1_3_VERSION) {
395 // Send a random session ID. TLS 1.3 always sends one, and TLS 1.2 session
396 // tickets require a placeholder value to signal resumption.
397 hs->session_id_len = sizeof(hs->session_id);
398 if (!RAND_bytes(hs->session_id, hs->session_id_len)) {
399 return ssl_hs_error;
400 }
401 }
402 }
403
404 ssl_early_data_reason_t reason = should_offer_early_data(hs);
405 if (reason != ssl_early_data_accepted) {
406 ssl->s3->early_data_reason = reason;
407 } else {
408 hs->early_data_offered = true;
409 }
410
411 if (!ssl_setup_key_shares(hs, /*override_group_id=*/0) ||
412 !ssl_setup_extension_permutation(hs) ||
413 !ssl_encrypt_client_hello(hs, MakeConstSpan(ech_enc, ech_enc_len)) ||
414 !ssl_add_client_hello(hs)) {
415 return ssl_hs_error;
416 }
417
418 hs->state = state_enter_early_data;
419 return ssl_hs_flush;
420 }
421
422 static enum ssl_hs_wait_t do_enter_early_data(SSL_HANDSHAKE *hs) {
423 SSL *const ssl = hs->ssl;
424
425 if (SSL_is_dtls(ssl)) {
426 hs->state = state_read_hello_verify_request;
427 return ssl_hs_ok;
428 }
429
430 if (!hs->early_data_offered) {
431 hs->state = state_read_server_hello;
432 return ssl_hs_ok;
433 }
434
435 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->session->ssl_version);
436 if (!ssl->method->add_change_cipher_spec(ssl)) {
437 return ssl_hs_error;
438 }
439
440 if (!tls13_init_early_key_schedule(hs, ssl->session.get()) ||
441 !tls13_derive_early_secret(hs)) {
442 return ssl_hs_error;
443 }
444
445 // Stash the early data session, so connection properties may be queried out
446 // of it.
447 hs->early_session = UpRef(ssl->session);
448 hs->state = state_early_reverify_server_certificate;
449 return ssl_hs_ok;
450 }
451
452 static enum ssl_hs_wait_t do_early_reverify_server_certificate(SSL_HANDSHAKE *hs) {
453 if (hs->ssl->ctx->reverify_on_resume) {
454 // Don't send an alert on error. The alert be in early data, which the
455 // server may not accept anyway. It would also be a mismatch between QUIC
456 // and TCP because the QUIC early keys are deferred below.
457 //
458 // TODO(davidben): The client behavior should be to verify the certificate
459 // before deciding whether to offer the session and, if invalid, decline to
460 // send the session.
461 switch (ssl_reverify_peer_cert(hs, /*send_alert=*/false)) {
462 case ssl_verify_ok:
463 break;
464 case ssl_verify_invalid:
465 return ssl_hs_error;
466 case ssl_verify_retry:
467 hs->state = state_early_reverify_server_certificate;
468 return ssl_hs_certificate_verify;
469 }
470 }
471
472 // Defer releasing the 0-RTT key to after certificate reverification, so the
473 // QUIC implementation does not accidentally write data too early.
474 if (!tls13_set_traffic_key(hs->ssl, ssl_encryption_early_data, evp_aead_seal,
475 hs->early_session.get(),
476 hs->early_traffic_secret())) {
477 return ssl_hs_error;
478 }
479
480 hs->in_early_data = true;
481 hs->can_early_write = true;
482 hs->state = state_read_server_hello;
483 return ssl_hs_early_return;
484 }
485
486 static enum ssl_hs_wait_t do_read_hello_verify_request(SSL_HANDSHAKE *hs) {
487 SSL *const ssl = hs->ssl;
488
489 assert(SSL_is_dtls(ssl));
490
491 // When implementing DTLS 1.3, we need to handle the interactions between
492 // HelloVerifyRequest, DTLS 1.3's HelloVerifyRequest removal, and ECH.
493 assert(hs->max_version < TLS1_3_VERSION);
494
495 SSLMessage msg;
496 if (!ssl->method->get_message(ssl, &msg)) {
497 return ssl_hs_read_message;
498 }
499
500 if (msg.type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
501 hs->state = state_read_server_hello;
502 return ssl_hs_ok;
503 }
504
505 CBS hello_verify_request = msg.body, cookie;
506 uint16_t server_version;
507 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
508 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
509 CBS_len(&hello_verify_request) != 0) {
510 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
511 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
512 return ssl_hs_error;
513 }
514
515 if (!hs->dtls_cookie.CopyFrom(cookie)) {
516 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
517 return ssl_hs_error;
518 }
519
520 ssl->method->next_message(ssl);
521
522 // DTLS resets the handshake buffer after HelloVerifyRequest.
523 if (!hs->transcript.Init()) {
524 return ssl_hs_error;
525 }
526
527 if (!ssl_add_client_hello(hs)) {
528 return ssl_hs_error;
529 }
530
531 hs->state = state_read_server_hello;
532 return ssl_hs_flush;
533 }
534
535 bool ssl_parse_server_hello(ParsedServerHello *out, uint8_t *out_alert,
536 const SSLMessage &msg) {
537 if (msg.type != SSL3_MT_SERVER_HELLO) {
538 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
539 *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
540 return false;
541 }
542 out->raw = msg.raw;
543 CBS body = msg.body;
544 if (!CBS_get_u16(&body, &out->legacy_version) ||
545 !CBS_get_bytes(&body, &out->random, SSL3_RANDOM_SIZE) ||
546 !CBS_get_u8_length_prefixed(&body, &out->session_id) ||
547 CBS_len(&out->session_id) > SSL3_SESSION_ID_SIZE ||
548 !CBS_get_u16(&body, &out->cipher_suite) ||
549 !CBS_get_u8(&body, &out->compression_method)) {
550 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
551 *out_alert = SSL_AD_DECODE_ERROR;
552 return false;
553 }
554 // In TLS 1.2 and below, empty extensions blocks may be omitted. In TLS 1.3,
555 // ServerHellos always have extensions, so this can be applied generically.
556 CBS_init(&out->extensions, nullptr, 0);
557 if ((CBS_len(&body) != 0 &&
558 !CBS_get_u16_length_prefixed(&body, &out->extensions)) ||
559 CBS_len(&body) != 0) {
560 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
561 *out_alert = SSL_AD_DECODE_ERROR;
562 return false;
563 }
564 return true;
565 }
566
567 static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) {
568 SSL *const ssl = hs->ssl;
569 SSLMessage msg;
570 if (!ssl->method->get_message(ssl, &msg)) {
571 return ssl_hs_read_server_hello;
572 }
573
574 ParsedServerHello server_hello;
575 uint16_t server_version;
576 uint8_t alert = SSL_AD_DECODE_ERROR;
577 if (!ssl_parse_server_hello(&server_hello, &alert, msg) ||
578 !parse_server_version(hs, &server_version, &alert, server_hello)) {
579 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
580 return ssl_hs_error;
581 }
582
583 if (!ssl_supports_version(hs, server_version)) {
584 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
585 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
586 return ssl_hs_error;
587 }
588
589 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
590 if (!ssl->s3->have_version) {
591 ssl->version = server_version;
592 // At this point, the connection's version is known and ssl->version is
593 // fixed. Begin enforcing the record-layer version.
594 ssl->s3->have_version = true;
595 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
596 } else if (server_version != ssl->version) {
597 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
598 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
599 return ssl_hs_error;
600 }
601
602 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
603 hs->state = state_tls13;
604 return ssl_hs_ok;
605 }
606
607 // Clear some TLS 1.3 state that no longer needs to be retained.
608 hs->key_shares[0].reset();
609 hs->key_shares[1].reset();
610 ssl_done_writing_client_hello(hs);
611
612 // A TLS 1.2 server would not know to skip the early data we offered. Report
613 // an error code sooner. The caller may use this error code to implement the
614 // fallback described in RFC 8446 appendix D.3.
615 if (hs->early_data_offered) {
616 // Disconnect early writes. This ensures subsequent |SSL_write| calls query
617 // the handshake which, in turn, will replay the error code rather than fail
618 // at the |write_shutdown| check. See https://crbug.com/1078515.
619 // TODO(davidben): Should all handshake errors do this? What about record
620 // decryption failures?
621 hs->can_early_write = false;
622 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA);
623 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
624 return ssl_hs_error;
625 }
626
627 // TLS 1.2 handshakes cannot accept ECH.
628 if (hs->selected_ech_config) {
629 ssl->s3->ech_status = ssl_ech_rejected;
630 }
631
632 // Copy over the server random.
633 OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_hello.random),
634 SSL3_RANDOM_SIZE);
635
636 // Enforce the TLS 1.3 anti-downgrade feature.
637 if (!ssl->s3->initial_handshake_complete &&
638 ssl_supports_version(hs, TLS1_3_VERSION)) {
639 static_assert(
640 sizeof(kTLS12DowngradeRandom) == sizeof(kTLS13DowngradeRandom),
641 "downgrade signals have different size");
642 static_assert(
643 sizeof(kJDK11DowngradeRandom) == sizeof(kTLS13DowngradeRandom),
644 "downgrade signals have different size");
645 auto suffix =
646 MakeConstSpan(ssl->s3->server_random, sizeof(ssl->s3->server_random))
647 .subspan(SSL3_RANDOM_SIZE - sizeof(kTLS13DowngradeRandom));
648 if (suffix == kTLS12DowngradeRandom || suffix == kTLS13DowngradeRandom ||
649 suffix == kJDK11DowngradeRandom) {
650 OPENSSL_PUT_ERROR(SSL, SSL_R_TLS13_DOWNGRADE);
651 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
652 return ssl_hs_error;
653 }
654 }
655
656 // The cipher must be allowed in the selected version and enabled.
657 const SSL_CIPHER *cipher = SSL_get_cipher_by_value(server_hello.cipher_suite);
658 uint32_t mask_a, mask_k;
659 ssl_get_client_disabled(hs, &mask_a, &mask_k);
660 if (cipher == nullptr ||
661 (cipher->algorithm_mkey & mask_k) ||
662 (cipher->algorithm_auth & mask_a) ||
663 SSL_CIPHER_get_min_version(cipher) > ssl_protocol_version(ssl) ||
664 SSL_CIPHER_get_max_version(cipher) < ssl_protocol_version(ssl) ||
665 !sk_SSL_CIPHER_find_awslc(SSL_get_ciphers(ssl), nullptr, cipher)) {
666 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
667 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
668 return ssl_hs_error;
669 }
670
671 hs->new_cipher = cipher;
672
673 if (hs->session_id_len != 0 &&
674 CBS_mem_equal(&server_hello.session_id, hs->session_id,
675 hs->session_id_len)) {
676 // Echoing the ClientHello session ID in TLS 1.2, whether from the session
677 // or a synthetic one, indicates resumption. If there was no session (or if
678 // the session was only offered in ECH ClientHelloInner), this was the
679 // TLS 1.3 compatibility mode session ID. As we know this is not a session
680 // the server knows about, any server resuming it is in error. Reject the
681 // first connection deterministicly, rather than installing an invalid
682 // session into the session cache. https://crbug.com/796910
683 if (ssl->session == nullptr || ssl->s3->ech_status == ssl_ech_rejected) {
684 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_ECHOED_INVALID_SESSION_ID);
685 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
686 return ssl_hs_error;
687 }
688 if (ssl->session->ssl_version != ssl->version) {
689 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
690 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
691 return ssl_hs_error;
692 }
693 if (ssl->session->cipher != hs->new_cipher) {
694 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
695 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
696 return ssl_hs_error;
697 }
698 if (!ssl_session_is_context_valid(hs, ssl->session.get())) {
699 // This is actually a client application bug.
700 OPENSSL_PUT_ERROR(SSL,
701 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
702 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
703 return ssl_hs_error;
704 }
705 // We never offer sessions on renegotiation.
706 assert(!ssl->s3->initial_handshake_complete);
707 ssl->s3->session_reused = true;
708 } else {
709 // The session wasn't resumed. Create a fresh SSL_SESSION to fill out.
710 ssl_set_session(ssl, NULL);
711 if (!ssl_get_new_session(hs)) {
712 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
713 return ssl_hs_error;
714 }
715
716 // Save the session ID from the server. This may be empty if the session
717 // isn't resumable, or if we'll receive a session ticket later.
718 assert(CBS_len(&server_hello.session_id) <= SSL3_SESSION_ID_SIZE);
719 static_assert(SSL3_SESSION_ID_SIZE <= UINT8_MAX,
720 "max session ID is too large");
721 hs->new_session->session_id_length =
722 static_cast<uint8_t>(CBS_len(&server_hello.session_id));
723 OPENSSL_memcpy(hs->new_session->session_id,
724 CBS_data(&server_hello.session_id),
725 CBS_len(&server_hello.session_id));
726
727 hs->new_session->cipher = hs->new_cipher;
728 }
729
730 // Now that the cipher is known, initialize the handshake hash and hash the
731 // ServerHello.
732 if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
733 !ssl_hash_message(hs, msg)) {
734 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
735 return ssl_hs_error;
736 }
737
738 // If doing a full handshake, the server may request a client certificate
739 // which requires hashing the handshake transcript. Otherwise, the handshake
740 // buffer may be released.
741 if (ssl->session != NULL ||
742 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
743 hs->transcript.FreeBuffer();
744 }
745
746 // Only the NULL compression algorithm is supported.
747 if (server_hello.compression_method != 0) {
748 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
749 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
750 return ssl_hs_error;
751 }
752
753 if (!ssl_parse_serverhello_tlsext(hs, &server_hello.extensions)) {
754 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
755 return ssl_hs_error;
756 }
757
758 if (ssl->session != NULL &&
759 hs->extended_master_secret != ssl->session->extended_master_secret) {
760 if (ssl->session->extended_master_secret) {
761 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
762 } else {
763 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
764 }
765 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
766 return ssl_hs_error;
767 }
768
769 ssl->method->next_message(ssl);
770
771 if (ssl->session != NULL) {
772 if (ssl->ctx->reverify_on_resume &&
773 ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
774 hs->state = state_reverify_server_certificate;
775 } else {
776 hs->state = state_read_session_ticket;
777 }
778 return ssl_hs_ok;
779 }
780
781 hs->state = state_read_server_certificate;
782 return ssl_hs_ok;
783 }
784
785 static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
786 enum ssl_hs_wait_t wait = tls13_client_handshake(hs);
787 if (wait == ssl_hs_ok) {
788 hs->state = state_finish_client_handshake;
789 return ssl_hs_ok;
790 }
791
792 return wait;
793 }
794
795 static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) {
796 SSL *const ssl = hs->ssl;
797
798 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
799 hs->state = state_read_certificate_status;
800 return ssl_hs_ok;
801 }
802
803 SSLMessage msg;
804 if (!ssl->method->get_message(ssl, &msg)) {
805 return ssl_hs_read_message;
806 }
807
808 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
809 !ssl_hash_message(hs, msg)) {
810 return ssl_hs_error;
811 }
812
813 CBS body = msg.body;
814 uint8_t alert = SSL_AD_DECODE_ERROR;
815 if (!ssl_parse_cert_chain(&alert, &hs->new_session->certs, &hs->peer_pubkey,
816 NULL, &body, ssl->ctx->pool)) {
817 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
818 return ssl_hs_error;
819 }
820
821 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0 ||
822 CBS_len(&body) != 0 ||
823 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
824 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
825 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
826 return ssl_hs_error;
827 }
828
829 if (!ssl_check_leaf_certificate(
830 hs, hs->peer_pubkey.get(),
831 sk_CRYPTO_BUFFER_value(hs->new_session->certs.get(), 0))) {
832 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
833 return ssl_hs_error;
834 }
835
836 ssl->method->next_message(ssl);
837
838 hs->state = state_read_certificate_status;
839 return ssl_hs_ok;
840 }
841
842 static enum ssl_hs_wait_t do_read_certificate_status(SSL_HANDSHAKE *hs) {
843 SSL *const ssl = hs->ssl;
844
845 if (!hs->certificate_status_expected) {
846 hs->state = state_verify_server_certificate;
847 return ssl_hs_ok;
848 }
849
850 SSLMessage msg;
851 if (!ssl->method->get_message(ssl, &msg)) {
852 return ssl_hs_read_message;
853 }
854
855 if (msg.type != SSL3_MT_CERTIFICATE_STATUS) {
856 // A server may send status_request in ServerHello and then change its mind
857 // about sending CertificateStatus.
858 hs->state = state_verify_server_certificate;
859 return ssl_hs_ok;
860 }
861
862 if (!ssl_hash_message(hs, msg)) {
863 return ssl_hs_error;
864 }
865
866 CBS certificate_status = msg.body, ocsp_response;
867 uint8_t status_type;
868 if (!CBS_get_u8(&certificate_status, &status_type) ||
869 status_type != TLSEXT_STATUSTYPE_ocsp ||
870 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
871 CBS_len(&ocsp_response) == 0 ||
872 CBS_len(&certificate_status) != 0) {
873 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
874 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
875 return ssl_hs_error;
876 }
877
878 hs->new_session->ocsp_response.reset(
879 CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool));
880 if (hs->new_session->ocsp_response == nullptr) {
881 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
882 return ssl_hs_error;
883 }
884
885 ssl->method->next_message(ssl);
886
887 hs->state = state_verify_server_certificate;
888 return ssl_hs_ok;
889 }
890
891 static enum ssl_hs_wait_t do_verify_server_certificate(SSL_HANDSHAKE *hs) {
892 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
893 hs->state = state_read_server_key_exchange;
894 return ssl_hs_ok;
895 }
896
897 switch (ssl_verify_peer_cert(hs)) {
898 case ssl_verify_ok:
899 break;
900 case ssl_verify_invalid:
901 return ssl_hs_error;
902 case ssl_verify_retry:
903 hs->state = state_verify_server_certificate;
904 return ssl_hs_certificate_verify;
905 }
906
907 hs->state = state_read_server_key_exchange;
908 return ssl_hs_ok;
909 }
910
911 static enum ssl_hs_wait_t do_reverify_server_certificate(SSL_HANDSHAKE *hs) {
912 assert(hs->ssl->ctx->reverify_on_resume);
913
914 switch (ssl_reverify_peer_cert(hs, /*send_alert=*/true)) {
915 case ssl_verify_ok:
916 break;
917 case ssl_verify_invalid:
918 return ssl_hs_error;
919 case ssl_verify_retry:
920 hs->state = state_reverify_server_certificate;
921 return ssl_hs_certificate_verify;
922 }
923
924 hs->state = state_read_session_ticket;
925 return ssl_hs_ok;
926 }
927
928 static enum ssl_hs_wait_t do_read_server_key_exchange(SSL_HANDSHAKE *hs) {
929 SSL *const ssl = hs->ssl;
930 SSLMessage msg;
931 if (!ssl->method->get_message(ssl, &msg)) {
932 return ssl_hs_read_message;
933 }
934
935 if (msg.type != SSL3_MT_SERVER_KEY_EXCHANGE) {
936 // Some ciphers (pure PSK) have an optional ServerKeyExchange message.
937 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
938 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
939 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
940 return ssl_hs_error;
941 }
942
943 hs->state = state_read_certificate_request;
944 return ssl_hs_ok;
945 }
946
947 if (!ssl_hash_message(hs, msg)) {
948 return ssl_hs_error;
949 }
950
951 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
952 uint32_t alg_a = hs->new_cipher->algorithm_auth;
953 CBS server_key_exchange = msg.body;
954 if (alg_a & SSL_aPSK) {
955 CBS psk_identity_hint;
956
957 // Each of the PSK key exchanges begins with a psk_identity_hint.
958 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
959 &psk_identity_hint)) {
960 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
961 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
962 return ssl_hs_error;
963 }
964
965 // Store the PSK identity hint for the ClientKeyExchange. Assume that the
966 // maximum length of a PSK identity hint can be as long as the maximum
967 // length of a PSK identity. Also do not allow NULL characters; identities
968 // are saved as C strings.
969 //
970 // TODO(davidben): Should invalid hints be ignored? It's a hint rather than
971 // a specific identity.
972 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
973 CBS_contains_zero_byte(&psk_identity_hint)) {
974 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
975 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
976 return ssl_hs_error;
977 }
978
979 // Save non-empty identity hints as a C string. Empty identity hints we
980 // treat as missing. Plain PSK makes it possible to send either no hint
981 // (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
982 // empty hint. Having different capabilities is odd, so we interpret empty
983 // and missing as identical.
984 char *raw = nullptr;
985 if (CBS_len(&psk_identity_hint) != 0 &&
986 !CBS_strdup(&psk_identity_hint, &raw)) {
987 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
988 return ssl_hs_error;
989 }
990 hs->peer_psk_identity_hint.reset(raw);
991 }
992
993 if (alg_k & SSL_kECDHE) {
994 // Parse the server parameters.
995 uint8_t group_type;
996 uint16_t group_id;
997 CBS point;
998 if (!CBS_get_u8(&server_key_exchange, &group_type) ||
999 group_type != NAMED_CURVE_TYPE ||
1000 !CBS_get_u16(&server_key_exchange, &group_id) ||
1001 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
1002 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1003 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1004 return ssl_hs_error;
1005 }
1006
1007 // Ensure the group is consistent with preferences.
1008 if (!tls1_check_group_id(hs, group_id)) {
1009 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
1010 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1011 return ssl_hs_error;
1012 }
1013
1014 // Save the group and peer public key for later.
1015 hs->new_session->group_id = group_id;
1016 if (!ssl->s3->peer_key.CopyFrom(point)) {
1017 return ssl_hs_error;
1018 }
1019 } else if (!(alg_k & SSL_kPSK)) {
1020 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
1021 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1022 return ssl_hs_error;
1023 }
1024
1025 // At this point, |server_key_exchange| contains the signature, if any, while
1026 // |msg.body| contains the entire message. From that, derive a CBS containing
1027 // just the parameter.
1028 CBS parameter;
1029 CBS_init(&parameter, CBS_data(&msg.body),
1030 CBS_len(&msg.body) - CBS_len(&server_key_exchange));
1031
1032 // ServerKeyExchange should be signed by the server's public key.
1033 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
1034 uint16_t signature_algorithm = 0;
1035 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
1036 if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
1037 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1038 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1039 return ssl_hs_error;
1040 }
1041 uint8_t alert = SSL_AD_DECODE_ERROR;
1042 if (!tls12_check_peer_sigalg(hs, &alert, signature_algorithm)) {
1043 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
1044 return ssl_hs_error;
1045 }
1046 hs->new_session->peer_signature_algorithm = signature_algorithm;
1047 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
1048 hs->peer_pubkey.get())) {
1049 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1050 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
1051 return ssl_hs_error;
1052 }
1053
1054 // The last field in |server_key_exchange| is the signature.
1055 CBS signature;
1056 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1057 CBS_len(&server_key_exchange) != 0) {
1058 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1059 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1060 return ssl_hs_error;
1061 }
1062
1063 ScopedCBB transcript;
1064 Array<uint8_t> transcript_data;
1065 if (!CBB_init(transcript.get(),
1066 2 * SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
1067 !CBB_add_bytes(transcript.get(), ssl->s3->client_random,
1068 SSL3_RANDOM_SIZE) ||
1069 !CBB_add_bytes(transcript.get(), ssl->s3->server_random,
1070 SSL3_RANDOM_SIZE) ||
1071 !CBB_add_bytes(transcript.get(), CBS_data(&parameter),
1072 CBS_len(&parameter)) ||
1073 !CBBFinishArray(transcript.get(), &transcript_data)) {
1074 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1075 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1076 return ssl_hs_error;
1077 }
1078
1079 if (!ssl_public_key_verify(ssl, signature, signature_algorithm,
1080 hs->peer_pubkey.get(), transcript_data)) {
1081 // bad signature
1082 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
1083 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1084 return ssl_hs_error;
1085 }
1086 } else {
1087 // PSK ciphers are the only supported certificate-less ciphers.
1088 assert(alg_a == SSL_aPSK);
1089
1090 if (CBS_len(&server_key_exchange) > 0) {
1091 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
1092 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1093 return ssl_hs_error;
1094 }
1095 }
1096
1097 ssl->method->next_message(ssl);
1098 hs->state = state_read_certificate_request;
1099 return ssl_hs_ok;
1100 }
1101
1102 static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) {
1103 SSL *const ssl = hs->ssl;
1104
1105 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
1106 hs->state = state_read_server_hello_done;
1107 return ssl_hs_ok;
1108 }
1109
1110 SSLMessage msg;
1111 if (!ssl->method->get_message(ssl, &msg)) {
1112 return ssl_hs_read_message;
1113 }
1114
1115 if (msg.type == SSL3_MT_SERVER_HELLO_DONE) {
1116 // If we get here we don't need the handshake buffer as we won't be doing
1117 // client auth.
1118 hs->transcript.FreeBuffer();
1119 hs->state = state_read_server_hello_done;
1120 return ssl_hs_ok;
1121 }
1122
1123 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_REQUEST) ||
1124 !ssl_hash_message(hs, msg)) {
1125 return ssl_hs_error;
1126 }
1127
1128 // Get the certificate types.
1129 CBS body = msg.body, certificate_types;
1130 if (!CBS_get_u8_length_prefixed(&body, &certificate_types)) {
1131 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1132 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1133 return ssl_hs_error;
1134 }
1135
1136 if (!hs->certificate_types.CopyFrom(certificate_types)) {
1137 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1138 return ssl_hs_error;
1139 }
1140
1141 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
1142 CBS supported_signature_algorithms;
1143 if (!CBS_get_u16_length_prefixed(&body, &supported_signature_algorithms) ||
1144 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
1145 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1146 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1147 return ssl_hs_error;
1148 }
1149 }
1150
1151 uint8_t alert = SSL_AD_DECODE_ERROR;
1152 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names =
1153 ssl_parse_client_CA_list(ssl, &alert, &body);
1154 if (!ca_names) {
1155 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
1156 return ssl_hs_error;
1157 }
1158
1159 if (CBS_len(&body) != 0) {
1160 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1161 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1162 return ssl_hs_error;
1163 }
1164
1165 hs->cert_request = true;
1166 hs->ca_names = std::move(ca_names);
1167 ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
1168
1169 ssl->method->next_message(ssl);
1170 hs->state = state_read_server_hello_done;
1171 return ssl_hs_ok;
1172 }
1173
1174 static enum ssl_hs_wait_t do_read_server_hello_done(SSL_HANDSHAKE *hs) {
1175 SSL *const ssl = hs->ssl;
1176 SSLMessage msg;
1177 if (!ssl->method->get_message(ssl, &msg)) {
1178 return ssl_hs_read_message;
1179 }
1180
1181 if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO_DONE) ||
1182 !ssl_hash_message(hs, msg)) {
1183 return ssl_hs_error;
1184 }
1185
1186 // ServerHelloDone is empty.
1187 if (CBS_len(&msg.body) != 0) {
1188 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1189 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1190 return ssl_hs_error;
1191 }
1192
1193 // ServerHelloDone should be the end of the flight.
1194 if (ssl->method->has_unprocessed_handshake_data(ssl)) {
1195 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1196 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA);
1197 return ssl_hs_error;
1198 }
1199
1200 ssl->method->next_message(ssl);
1201 hs->state = state_send_client_certificate;
1202 return ssl_hs_ok;
1203 }
1204
1205 static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
1206 SSL *const ssl = hs->ssl;
1207
1208 // The peer didn't request a certificate.
1209 if (!hs->cert_request) {
1210 hs->state = state_send_client_key_exchange;
1211 return ssl_hs_ok;
1212 }
1213
1214 if (ssl->s3->ech_status == ssl_ech_rejected) {
1215 // Do not send client certificates on ECH reject. We have not authenticated
1216 // the server for the name that can learn the certificate.
1217 SSL_certs_clear(ssl);
1218 } else if (hs->config->cert->cert_cb != nullptr) {
1219 // Call cert_cb to update the certificate.
1220 int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg);
1221 if (rv == 0) {
1222 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1223 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
1224 return ssl_hs_error;
1225 }
1226 if (rv < 0) {
1227 hs->state = state_send_client_certificate;
1228 return ssl_hs_x509_lookup;
1229 }
1230 }
1231
1232 if (!ssl_on_certificate_selected(hs)) {
1233 return ssl_hs_error;
1234 }
1235
1236 if (ssl_has_certificate(hs)) {
1237 if (hs->config->check_client_certificate_type) {
1238 // Check the certificate types advertised by the peer.
1239 uint8_t cert_type;
1240 switch (EVP_PKEY_id(hs->local_pubkey.get())) {
1241 case EVP_PKEY_RSA:
1242 cert_type = SSL3_CT_RSA_SIGN;
1243 break;
1244 case EVP_PKEY_EC:
1245 case EVP_PKEY_ED25519:
1246 cert_type = TLS_CT_ECDSA_SIGN;
1247 break;
1248 default:
1249 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1250 return ssl_hs_error;
1251 }
1252 if (std::find(hs->certificate_types.begin(), hs->certificate_types.end(),
1253 cert_type) == hs->certificate_types.end()) {
1254 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1255 return ssl_hs_error;
1256 }
1257 }
1258 } else {
1259 // Without a client certificate, the handshake buffer may be released.
1260 hs->transcript.FreeBuffer();
1261 }
1262
1263 if (!ssl_output_cert_chain(hs)) {
1264 return ssl_hs_error;
1265 }
1266
1267 hs->state = state_send_client_key_exchange;
1268 return ssl_hs_ok;
1269 }
1270
1271 static_assert(sizeof(size_t) >= sizeof(unsigned),
1272 "size_t is smaller than unsigned");
1273
1274 static enum ssl_hs_wait_t do_send_client_key_exchange(SSL_HANDSHAKE *hs) {
1275 SSL *const ssl = hs->ssl;
1276 ScopedCBB cbb;
1277 CBB body;
1278 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1279 SSL3_MT_CLIENT_KEY_EXCHANGE)) {
1280 return ssl_hs_error;
1281 }
1282
1283 Array<uint8_t> pms;
1284 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1285 uint32_t alg_a = hs->new_cipher->algorithm_auth;
1286 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
1287 const CRYPTO_BUFFER *leaf =
1288 sk_CRYPTO_BUFFER_value(hs->new_session->certs.get(), 0);
1289 CBS leaf_cbs;
1290 CRYPTO_BUFFER_init_CBS(leaf, &leaf_cbs);
1291
1292 // Check the key usage matches the cipher suite. We do this unconditionally
1293 // for non-RSA certificates. In particular, it's needed to distinguish ECDH
1294 // certificates, which we do not support, from ECDSA certificates.
1295 // Historically, we have not checked RSA key usages, so it is controlled by
1296 // a flag for now. See https://crbug.com/795089.
1297 ssl_key_usage_t intended_use = (alg_k & SSL_kRSA)
1298 ? key_usage_encipherment
1299 : key_usage_digital_signature;
1300 if (!ssl_cert_check_key_usage(&leaf_cbs, intended_use)) {
1301 if (hs->config->enforce_rsa_key_usage ||
1302 EVP_PKEY_id(hs->peer_pubkey.get()) != EVP_PKEY_RSA) {
1303 return ssl_hs_error;
1304 }
1305 ERR_clear_error();
1306 ssl->s3->was_key_usage_invalid = true;
1307 }
1308 }
1309
1310 // If using a PSK key exchange, prepare the pre-shared key.
1311 unsigned psk_len = 0;
1312 uint8_t psk[PSK_MAX_PSK_LEN];
1313 if (alg_a & SSL_aPSK) {
1314 if (hs->config->psk_client_callback == NULL) {
1315 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
1316 return ssl_hs_error;
1317 }
1318
1319 char identity[PSK_MAX_IDENTITY_LEN + 1];
1320 OPENSSL_memset(identity, 0, sizeof(identity));
1321 psk_len = hs->config->psk_client_callback(
1322 ssl, hs->peer_psk_identity_hint.get(), identity, sizeof(identity), psk,
1323 sizeof(psk));
1324 if (psk_len == 0) {
1325 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1326 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1327 return ssl_hs_error;
1328 }
1329 assert(psk_len <= PSK_MAX_PSK_LEN);
1330
1331 hs->new_session->psk_identity.reset(OPENSSL_strdup(identity));
1332 if (hs->new_session->psk_identity == nullptr) {
1333 return ssl_hs_error;
1334 }
1335
1336 // Write out psk_identity.
1337 CBB child;
1338 if (!CBB_add_u16_length_prefixed(&body, &child) ||
1339 !CBB_add_bytes(&child, (const uint8_t *)identity,
1340 OPENSSL_strnlen(identity, sizeof(identity))) ||
1341 !CBB_flush(&body)) {
1342 return ssl_hs_error;
1343 }
1344 }
1345
1346 // Depending on the key exchange method, compute |pms|.
1347 if (alg_k & SSL_kRSA) {
1348 if (!pms.Init(SSL_MAX_MASTER_KEY_LENGTH)) {
1349 return ssl_hs_error;
1350 }
1351
1352 RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey.get());
1353 if (rsa == NULL) {
1354 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1355 return ssl_hs_error;
1356 }
1357
1358 pms[0] = hs->client_version >> 8;
1359 pms[1] = hs->client_version & 0xff;
1360 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1361 return ssl_hs_error;
1362 }
1363
1364 CBB enc_pms;
1365 uint8_t *ptr;
1366 size_t enc_pms_len;
1367 if (!CBB_add_u16_length_prefixed(&body, &enc_pms) ||
1368 !CBB_reserve(&enc_pms, &ptr, RSA_size(rsa)) ||
1369 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms.data(),
1370 pms.size(), RSA_PKCS1_PADDING) ||
1371 !CBB_did_write(&enc_pms, enc_pms_len) ||
1372 !CBB_flush(&body)) {
1373 return ssl_hs_error;
1374 }
1375 } else if (alg_k & SSL_kECDHE) {
1376 CBB child;
1377 if (!CBB_add_u8_length_prefixed(&body, &child)) {
1378 return ssl_hs_error;
1379 }
1380
1381 // Generate the premaster secret.
1382 bssl::UniquePtr<SSLKeyShare> key_share =
1383 SSLKeyShare::Create(hs->new_session->group_id);
1384 uint8_t alert = SSL_AD_DECODE_ERROR;
1385 if (!key_share ||
1386 !key_share->Accept(&child, &pms, &alert, ssl->s3->peer_key)) {
1387 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
1388 return ssl_hs_error;
1389 }
1390 if (!CBB_flush(&body)) {
1391 return ssl_hs_error;
1392 }
1393
1394 // The peer key could be discarded, but we preserve it since OpenSSL
1395 // allows the user to observe it with |SSL_get_peer_tmp_key|.
1396 } else if (alg_k & SSL_kPSK) {
1397 // For plain PSK, other_secret is a block of 0s with the same length as
1398 // the pre-shared key.
1399 if (!pms.Init(psk_len)) {
1400 return ssl_hs_error;
1401 }
1402 OPENSSL_memset(pms.data(), 0, pms.size());
1403 } else {
1404 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1405 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1406 return ssl_hs_error;
1407 }
1408
1409 // For a PSK cipher suite, other_secret is combined with the pre-shared
1410 // key.
1411 if (alg_a & SSL_aPSK) {
1412 ScopedCBB pms_cbb;
1413 CBB child;
1414 if (!CBB_init(pms_cbb.get(), 2 + psk_len + 2 + pms.size()) ||
1415 !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
1416 !CBB_add_bytes(&child, pms.data(), pms.size()) ||
1417 !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
1418 !CBB_add_bytes(&child, psk, psk_len) ||
1419 !CBBFinishArray(pms_cbb.get(), &pms)) {
1420 return ssl_hs_error;
1421 }
1422 }
1423
1424 // The message must be added to the finished hash before calculating the
1425 // master secret.
1426 if (!ssl_add_message_cbb(ssl, cbb.get())) {
1427 return ssl_hs_error;
1428 }
1429
1430 hs->new_session->secret_length =
1431 tls1_generate_master_secret(hs, hs->new_session->secret, pms);
1432 if (hs->new_session->secret_length == 0) {
1433 return ssl_hs_error;
1434 }
1435 hs->new_session->extended_master_secret = hs->extended_master_secret;
1436
1437 hs->state = state_send_client_certificate_verify;
1438 return ssl_hs_ok;
1439 }
1440
1441 static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) {
1442 SSL *const ssl = hs->ssl;
1443
1444 if (!hs->cert_request || !ssl_has_certificate(hs)) {
1445 hs->state = state_send_client_finished;
1446 return ssl_hs_ok;
1447 }
1448
1449 assert(ssl_has_private_key(hs));
1450 ScopedCBB cbb;
1451 CBB body, child;
1452 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1453 SSL3_MT_CERTIFICATE_VERIFY)) {
1454 return ssl_hs_error;
1455 }
1456
1457 if (!tls1_choose_signature_algorithm(hs, &hs->signature_algorithm)) {
1458 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1459 return ssl_hs_error;
1460 }
1461 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
1462 // Write out the digest type in TLS 1.2.
1463 if (!CBB_add_u16(&body, hs->signature_algorithm)) {
1464 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1465 return ssl_hs_error;
1466 }
1467 }
1468
1469 // Set aside space for the signature.
1470 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
1471 uint8_t *ptr;
1472 if (!CBB_add_u16_length_prefixed(&body, &child) ||
1473 !CBB_reserve(&child, &ptr, max_sig_len)) {
1474 return ssl_hs_error;
1475 }
1476
1477 size_t sig_len = max_sig_len;
1478 switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
1479 hs->signature_algorithm,
1480 hs->transcript.buffer())) {
1481 case ssl_private_key_success:
1482 break;
1483 case ssl_private_key_failure:
1484 return ssl_hs_error;
1485 case ssl_private_key_retry:
1486 hs->state = state_send_client_certificate_verify;
1487 return ssl_hs_private_key_operation;
1488 }
1489
1490 if (!CBB_did_write(&child, sig_len) ||
1491 !ssl_add_message_cbb(ssl, cbb.get())) {
1492 return ssl_hs_error;
1493 }
1494
1495 // The handshake buffer is no longer necessary.
1496 hs->transcript.FreeBuffer();
1497
1498 hs->state = state_send_client_finished;
1499 return ssl_hs_ok;
1500 }
1501
1502 static enum ssl_hs_wait_t do_send_client_finished(SSL_HANDSHAKE *hs) {
1503 SSL *const ssl = hs->ssl;
1504 hs->can_release_private_key = true;
1505 if (!ssl->method->add_change_cipher_spec(ssl) ||
1506 !tls1_change_cipher_state(hs, evp_aead_seal)) {
1507 return ssl_hs_error;
1508 }
1509
1510 if (hs->next_proto_neg_seen) {
1511 static const uint8_t kZero[32] = {0};
1512 size_t padding_len =
1513 32 - ((ssl->s3->next_proto_negotiated.size() + 2) % 32);
1514
1515 ScopedCBB cbb;
1516 CBB body, child;
1517 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_NEXT_PROTO) ||
1518 !CBB_add_u8_length_prefixed(&body, &child) ||
1519 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated.data(),
1520 ssl->s3->next_proto_negotiated.size()) ||
1521 !CBB_add_u8_length_prefixed(&body, &child) ||
1522 !CBB_add_bytes(&child, kZero, padding_len) ||
1523 !ssl_add_message_cbb(ssl, cbb.get())) {
1524 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1525 return ssl_hs_error;
1526 }
1527 }
1528
1529 if (hs->channel_id_negotiated) {
1530 ScopedCBB cbb;
1531 CBB body;
1532 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) ||
1533 !tls1_write_channel_id(hs, &body) ||
1534 !ssl_add_message_cbb(ssl, cbb.get())) {
1535 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1536 return ssl_hs_error;
1537 }
1538 }
1539
1540 if (!ssl_send_finished(hs)) {
1541 return ssl_hs_error;
1542 }
1543
1544 hs->state = state_finish_flight;
1545 return ssl_hs_flush;
1546 }
1547
1548 static bool can_false_start(const SSL_HANDSHAKE *hs) {
1549 const SSL *const ssl = hs->ssl;
1550
1551 // False Start bypasses the Finished check's downgrade protection. This can
1552 // enable attacks where we send data under weaker settings than supported
1553 // (e.g. the Logjam attack). Thus we require TLS 1.2 with an ECDHE+AEAD
1554 // cipher, our strongest settings before TLS 1.3.
1555 //
1556 // Now that TLS 1.3 exists, we would like to avoid similar attacks between
1557 // TLS 1.2 and TLS 1.3, but there are too many TLS 1.2 deployments to
1558 // sacrifice False Start on them. Instead, we rely on the ServerHello.random
1559 // downgrade signal, which we unconditionally enforce.
1560 if (SSL_is_dtls(ssl) ||
1561 SSL_version(ssl) != TLS1_2_VERSION ||
1562 hs->new_cipher->algorithm_mkey != SSL_kECDHE ||
1563 hs->new_cipher->algorithm_mac != SSL_AEAD) {
1564 return false;
1565 }
1566
1567 // If ECH was rejected, disable False Start. We run the handshake to
1568 // completion, including the Finished downgrade check, to authenticate the
1569 // recovery flow.
1570 if (ssl->s3->ech_status == ssl_ech_rejected) {
1571 return false;
1572 }
1573
1574 // Additionally require ALPN or NPN by default.
1575 //
1576 // TODO(davidben): Can this constraint be relaxed globally now that cipher
1577 // suite requirements have been tightened?
1578 if (!ssl->ctx->false_start_allowed_without_alpn &&
1579 ssl->s3->alpn_selected.empty() &&
1580 ssl->s3->next_proto_negotiated.empty()) {
1581 return false;
1582 }
1583
1584 return true;
1585 }
1586
1587 static enum ssl_hs_wait_t do_finish_flight(SSL_HANDSHAKE *hs) {
1588 SSL *const ssl = hs->ssl;
1589 if (ssl->session != NULL) {
1590 hs->state = state_finish_client_handshake;
1591 return ssl_hs_ok;
1592 }
1593
1594 // This is a full handshake. If it involves ChannelID, then record the
1595 // handshake hashes at this point in the session so that any resumption of
1596 // this session with ChannelID can sign those hashes.
1597 if (!tls1_record_handshake_hashes_for_channel_id(hs)) {
1598 return ssl_hs_error;
1599 }
1600
1601 hs->state = state_read_session_ticket;
1602
1603 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
1604 can_false_start(hs) &&
1605 // No False Start on renegotiation (would complicate the state machine).
1606 !ssl->s3->initial_handshake_complete) {
1607 hs->in_false_start = true;
1608 hs->can_early_write = true;
1609 return ssl_hs_early_return;
1610 }
1611
1612 return ssl_hs_ok;
1613 }
1614
1615 static enum ssl_hs_wait_t do_read_session_ticket(SSL_HANDSHAKE *hs) {
1616 SSL *const ssl = hs->ssl;
1617
1618 if (!hs->ticket_expected) {
1619 hs->state = state_process_change_cipher_spec;
1620 return ssl_hs_read_change_cipher_spec;
1621 }
1622
1623 SSLMessage msg;
1624 if (!ssl->method->get_message(ssl, &msg)) {
1625 return ssl_hs_read_message;
1626 }
1627
1628 if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEW_SESSION_TICKET) ||
1629 !ssl_hash_message(hs, msg)) {
1630 return ssl_hs_error;
1631 }
1632
1633 CBS new_session_ticket = msg.body, ticket;
1634 uint32_t ticket_lifetime_hint;
1635 if (!CBS_get_u32(&new_session_ticket, &ticket_lifetime_hint) ||
1636 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1637 CBS_len(&new_session_ticket) != 0) {
1638 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1639 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1640 return ssl_hs_error;
1641 }
1642
1643 if (CBS_len(&ticket) == 0) {
1644 // RFC 5077 allows a server to change its mind and send no ticket after
1645 // negotiating the extension. The value of |ticket_expected| is checked in
1646 // |ssl_update_cache| so is cleared here to avoid an unnecessary update.
1647 hs->ticket_expected = false;
1648 ssl->method->next_message(ssl);
1649 hs->state = state_process_change_cipher_spec;
1650 return ssl_hs_read_change_cipher_spec;
1651 }
1652
1653 if (ssl->session != nullptr) {
1654 // The server is sending a new ticket for an existing session. Sessions are
1655 // immutable once established, so duplicate all but the ticket of the
1656 // existing session.
1657 assert(!hs->new_session);
1658 hs->new_session =
1659 SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_INCLUDE_NONAUTH);
1660 if (!hs->new_session) {
1661 return ssl_hs_error;
1662 }
1663 }
1664
1665 ssl->verify_result = hs->new_session->verify_result;
1666
1667 // |ticket_lifetime_hint| is measured from when the ticket was issued.
1668 ssl_session_rebase_time(ssl, hs->new_session.get());
1669
1670 if (!hs->new_session->ticket.CopyFrom(ticket)) {
1671 return ssl_hs_error;
1672 }
1673 hs->new_session->ticket_lifetime_hint = ticket_lifetime_hint;
1674
1675 // Historically, OpenSSL filled in fake session IDs for ticket-based sessions.
1676 // TODO(davidben): Are external callers relying on this? Try removing this.
1677 SHA256(CBS_data(&ticket), CBS_len(&ticket), hs->new_session->session_id);
1678 hs->new_session->session_id_length = SHA256_DIGEST_LENGTH;
1679
1680 ssl->method->next_message(ssl);
1681 hs->state = state_process_change_cipher_spec;
1682 return ssl_hs_read_change_cipher_spec;
1683 }
1684
1685 static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
1686 if (!tls1_change_cipher_state(hs, evp_aead_open)) {
1687 return ssl_hs_error;
1688 }
1689
1690 hs->state = state_read_server_finished;
1691 return ssl_hs_ok;
1692 }
1693
1694 static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) {
1695 SSL *const ssl = hs->ssl;
1696 enum ssl_hs_wait_t wait = ssl_get_finished(hs);
1697 if (wait != ssl_hs_ok) {
1698 return wait;
1699 }
1700
1701 if (ssl->session != NULL) {
1702 hs->state = state_send_client_finished;
1703 return ssl_hs_ok;
1704 }
1705
1706 hs->state = state_finish_client_handshake;
1707 return ssl_hs_ok;
1708 }
1709
1710 static enum ssl_hs_wait_t do_finish_client_handshake(SSL_HANDSHAKE *hs) {
1711 SSL *const ssl = hs->ssl;
1712 if (ssl->s3->ech_status == ssl_ech_rejected) {
1713 // Release the retry configs.
1714 hs->ech_authenticated_reject = true;
1715 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ECH_REQUIRED);
1716 OPENSSL_PUT_ERROR(SSL, SSL_R_ECH_REJECTED);
1717 return ssl_hs_error;
1718 }
1719
1720 ssl->method->on_handshake_complete(ssl);
1721
1722 // Note TLS 1.2 resumptions with ticket renewal have both |ssl->session| (the
1723 // resumed session) and |hs->new_session| (the session with the new ticket).
1724 bool has_new_session = hs->new_session != nullptr;
1725 if (has_new_session) {
1726 // When False Start is enabled, the handshake reports completion early. The
1727 // caller may then have passed the (then unresuable) |hs->new_session| to
1728 // another thread via |SSL_get0_session| for resumption. To avoid potential
1729 // race conditions in such callers, we duplicate the session before
1730 // clearing |not_resumable|.
1731 ssl->s3->established_session =
1732 SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_DUP_ALL);
1733 if (!ssl->s3->established_session) {
1734 return ssl_hs_error;
1735 }
1736 // Renegotiations do not participate in session resumption.
1737 if (!ssl->s3->initial_handshake_complete) {
1738 ssl->s3->established_session->not_resumable = false;
1739 }
1740
1741 hs->new_session.reset();
1742 } else {
1743 assert(ssl->session != nullptr);
1744 ssl->s3->established_session = UpRef(ssl->session);
1745 }
1746
1747 hs->handshake_finalized = true;
1748 ssl->s3->initial_handshake_complete = true;
1749 if (has_new_session) {
1750 ssl_update_cache(ssl);
1751 }
1752
1753 if (ssl->s3->session_reused) {
1754 // Update client side session hit counter on successfully reused sessions.
1755 ssl_update_counter(ssl->session_ctx.get(), ssl->session_ctx->stats.sess_hit,
1756 true);
1757 }
1758
1759 hs->state = state_done;
1760 return ssl_hs_ok;
1761 }
1762
1763 enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs) {
1764 while (hs->state != state_done) {
1765 enum ssl_hs_wait_t ret = ssl_hs_error;
1766 enum ssl_client_hs_state_t state =
1767 static_cast<enum ssl_client_hs_state_t>(hs->state);
1768 switch (state) {
1769 case state_start_connect:
1770 ret = do_start_connect(hs);
1771 break;
1772 case state_enter_early_data:
1773 ret = do_enter_early_data(hs);
1774 break;
1775 case state_early_reverify_server_certificate:
1776 ret = do_early_reverify_server_certificate(hs);
1777 break;
1778 case state_read_hello_verify_request:
1779 ret = do_read_hello_verify_request(hs);
1780 break;
1781 case state_read_server_hello:
1782 ret = do_read_server_hello(hs);
1783 break;
1784 case state_tls13:
1785 ret = do_tls13(hs);
1786 break;
1787 case state_read_server_certificate:
1788 ret = do_read_server_certificate(hs);
1789 break;
1790 case state_read_certificate_status:
1791 ret = do_read_certificate_status(hs);
1792 break;
1793 case state_verify_server_certificate:
1794 ret = do_verify_server_certificate(hs);
1795 break;
1796 case state_reverify_server_certificate:
1797 ret = do_reverify_server_certificate(hs);
1798 break;
1799 case state_read_server_key_exchange:
1800 ret = do_read_server_key_exchange(hs);
1801 break;
1802 case state_read_certificate_request:
1803 ret = do_read_certificate_request(hs);
1804 break;
1805 case state_read_server_hello_done:
1806 ret = do_read_server_hello_done(hs);
1807 break;
1808 case state_send_client_certificate:
1809 ret = do_send_client_certificate(hs);
1810 break;
1811 case state_send_client_key_exchange:
1812 ret = do_send_client_key_exchange(hs);
1813 break;
1814 case state_send_client_certificate_verify:
1815 ret = do_send_client_certificate_verify(hs);
1816 break;
1817 case state_send_client_finished:
1818 ret = do_send_client_finished(hs);
1819 break;
1820 case state_finish_flight:
1821 ret = do_finish_flight(hs);
1822 break;
1823 case state_read_session_ticket:
1824 ret = do_read_session_ticket(hs);
1825 break;
1826 case state_process_change_cipher_spec:
1827 ret = do_process_change_cipher_spec(hs);
1828 break;
1829 case state_read_server_finished:
1830 ret = do_read_server_finished(hs);
1831 break;
1832 case state_finish_client_handshake:
1833 ret = do_finish_client_handshake(hs);
1834 break;
1835 case state_done:
1836 ret = ssl_hs_ok;
1837 break;
1838 }
1839
1840 if (hs->state != state) {
1841 ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1);
1842 }
1843
1844 if (ret != ssl_hs_ok) {
1845 return ret;
1846 }
1847 }
1848
1849 ssl_update_counter(hs->ssl->session_ctx.get(),
1850 hs->ssl->session_ctx->stats.sess_connect_good, true);
1851 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
1852 return ssl_hs_ok;
1853 }
1854
1855 const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs) {
1856 enum ssl_client_hs_state_t state =
1857 static_cast<enum ssl_client_hs_state_t>(hs->state);
1858 switch (state) {
1859 case state_start_connect:
1860 return "TLS client start_connect";
1861 case state_enter_early_data:
1862 return "TLS client enter_early_data";
1863 case state_early_reverify_server_certificate:
1864 return "TLS client early_reverify_server_certificate";
1865 case state_read_hello_verify_request:
1866 return "TLS client read_hello_verify_request";
1867 case state_read_server_hello:
1868 return "TLS client read_server_hello";
1869 case state_tls13:
1870 return tls13_client_handshake_state(hs);
1871 case state_read_server_certificate:
1872 return "TLS client read_server_certificate";
1873 case state_read_certificate_status:
1874 return "TLS client read_certificate_status";
1875 case state_verify_server_certificate:
1876 return "TLS client verify_server_certificate";
1877 case state_reverify_server_certificate:
1878 return "TLS client reverify_server_certificate";
1879 case state_read_server_key_exchange:
1880 return "TLS client read_server_key_exchange";
1881 case state_read_certificate_request:
1882 return "TLS client read_certificate_request";
1883 case state_read_server_hello_done:
1884 return "TLS client read_server_hello_done";
1885 case state_send_client_certificate:
1886 return "TLS client send_client_certificate";
1887 case state_send_client_key_exchange:
1888 return "TLS client send_client_key_exchange";
1889 case state_send_client_certificate_verify:
1890 return "TLS client send_client_certificate_verify";
1891 case state_send_client_finished:
1892 return "TLS client send_client_finished";
1893 case state_finish_flight:
1894 return "TLS client finish_flight";
1895 case state_read_session_ticket:
1896 return "TLS client read_session_ticket";
1897 case state_process_change_cipher_spec:
1898 return "TLS client process_change_cipher_spec";
1899 case state_read_server_finished:
1900 return "TLS client read_server_finished";
1901 case state_finish_client_handshake:
1902 return "TLS client finish_client_handshake";
1903 case state_done:
1904 return "TLS client done";
1905 }
1906
1907 return "TLS client unknown";
1908 }
1909
1910 BSSL_NAMESPACE_END