Debian Codegraph @

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

raw
1 // Copyright (c) 2016, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include <openssl/ssl.h>
5
6 #include <assert.h>
7 #include <limits.h>
8 #include <string.h>
9
10 #include <utility>
11
12 #include <openssl/bytestring.h>
13 #include <openssl/digest.h>
14 #include <openssl/err.h>
15 #include <openssl/mem.h>
16 #include <openssl/sha.h>
17 #include <openssl/stack.h>
18
19 #include "../crypto/internal.h"
20 #include "internal.h"
21
22
23 BSSL_NAMESPACE_BEGIN
24
25 enum client_hs_state_t {
26 state_read_hello_retry_request = 0,
27 state_send_second_client_hello,
28 state_read_server_hello,
29 state_read_encrypted_extensions,
30 state_read_certificate_request,
31 state_read_server_certificate,
32 state_read_server_certificate_verify,
33 state_server_certificate_reverify,
34 state_read_server_finished,
35 state_send_end_of_early_data,
36 state_send_client_encrypted_extensions,
37 state_send_client_certificate,
38 state_send_client_certificate_verify,
39 state_complete_second_flight,
40 state_done,
41 };
42
43 static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0};
44
45 // end_of_early_data closes the early data stream for |hs| and switches the
46 // encryption level to |level|. It returns true on success and false on error.
47 static bool close_early_data(SSL_HANDSHAKE *hs, ssl_encryption_level_t level) {
48 SSL *const ssl = hs->ssl;
49 assert(hs->in_early_data);
50
51 // Note |can_early_write| may already be false if |SSL_write| exceeded the
52 // early data write limit.
53 hs->can_early_write = false;
54
55 // 0-RTT write states on the client differ between TLS 1.3, DTLS 1.3, and
56 // QUIC. TLS 1.3 has one write encryption level at a time. 0-RTT write keys
57 // overwrite the null cipher and defer handshake write keys. While a
58 // HelloRetryRequest can cause us to rewind back to the null cipher, sequence
59 // numbers have no effect, so we can install a "new" null cipher.
60 //
61 // In QUIC and DTLS 1.3, 0-RTT write state cannot override or defer the normal
62 // write state. The two ClientHello sequence numbers must align, and handshake
63 // write keys must be installed early to ACK the EncryptedExtensions.
64 //
65 // We do not currently implement DTLS 1.3 and, in QUIC, the caller handles
66 // 0-RTT data, so we can skip installing 0-RTT keys and act as if there is one
67 // write level. If we implement DTLS 1.3, we'll need to model this better.
68 if (ssl->quic_method == nullptr) {
69 if (level == ssl_encryption_initial) {
70 bssl::UniquePtr<SSLAEADContext> null_ctx =
71 SSLAEADContext::CreateNullCipher(SSL_is_dtls(ssl));
72 if (!null_ctx ||
73 !ssl->method->set_write_state(ssl, ssl_encryption_initial,
74 std::move(null_ctx),
75 /*secret_for_quic=*/{})) {
76 return false;
77 }
78 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
79 } else {
80 assert(level == ssl_encryption_handshake);
81 if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_seal,
82 hs->new_session.get(),
83 hs->client_handshake_secret())) {
84 return false;
85 }
86 }
87 }
88
89 assert(ssl->s3->write_level == level);
90 return true;
91 }
92
93 static bool parse_server_hello_tls13(const SSL_HANDSHAKE *hs,
94 ParsedServerHello *out, uint8_t *out_alert,
95 const SSLMessage &msg) {
96 if (!ssl_parse_server_hello(out, out_alert, msg)) {
97 return false;
98 }
99 // The RFC8446 version of the structure fixes some legacy values.
100 // Additionally, the session ID must echo the original one.
101 if (out->legacy_version != TLS1_2_VERSION ||
102 out->compression_method != 0 ||
103 !CBS_mem_equal(&out->session_id, hs->session_id, hs->session_id_len) ||
104 CBS_len(&out->extensions) == 0) {
105 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
106 *out_alert = SSL_AD_DECODE_ERROR;
107 return false;
108 }
109 return true;
110 }
111
112 static bool is_hello_retry_request(const ParsedServerHello &server_hello) {
113 return Span<const uint8_t>(server_hello.random) == kHelloRetryRequest;
114 }
115
116 static bool check_ech_confirmation(const SSL_HANDSHAKE *hs, bool *out_accepted,
117 uint8_t *out_alert,
118 const ParsedServerHello &server_hello) {
119 const bool is_hrr = is_hello_retry_request(server_hello);
120 size_t offset;
121 if (is_hrr) {
122 // We check for an unsolicited extension when parsing all of them.
123 SSLExtension ech(TLSEXT_TYPE_encrypted_client_hello);
124 if (!ssl_parse_extensions(&server_hello.extensions, out_alert, {&ech},
125 /*ignore_unknown=*/true)) {
126 return false;
127 }
128 if (!ech.present) {
129 *out_accepted = false;
130 return true;
131 }
132 if (CBS_len(&ech.data) != ECH_CONFIRMATION_SIGNAL_LEN) {
133 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
134 *out_alert = SSL_AD_DECODE_ERROR;
135 return false;
136 }
137 offset = CBS_data(&ech.data) - CBS_data(&server_hello.raw);
138 } else {
139 offset = ssl_ech_confirmation_signal_hello_offset(hs->ssl);
140 }
141
142 if (!hs->selected_ech_config) {
143 *out_accepted = false;
144 return true;
145 }
146
147 uint8_t expected[ECH_CONFIRMATION_SIGNAL_LEN];
148 if (!ssl_ech_accept_confirmation(hs, expected, hs->inner_client_random,
149 hs->inner_transcript, is_hrr,
150 server_hello.raw, offset)) {
151 *out_alert = SSL_AD_INTERNAL_ERROR;
152 return false;
153 }
154
155 *out_accepted = CRYPTO_memcmp(CBS_data(&server_hello.raw) + offset, expected,
156 sizeof(expected)) == 0;
157 return true;
158 }
159
160 static enum ssl_hs_wait_t do_read_hello_retry_request(SSL_HANDSHAKE *hs) {
161 SSL *const ssl = hs->ssl;
162 assert(ssl->s3->have_version);
163 SSLMessage msg;
164 if (!ssl->method->get_message(ssl, &msg)) {
165 return ssl_hs_read_message;
166 }
167
168 // Queue up a ChangeCipherSpec for whenever we next send something. This
169 // will be before the second ClientHello. If we offered early data, this was
170 // already done.
171 if (!hs->early_data_offered &&
172 !ssl->method->add_change_cipher_spec(ssl)) {
173 return ssl_hs_error;
174 }
175
176 ParsedServerHello server_hello;
177 uint8_t alert = SSL_AD_DECODE_ERROR;
178 if (!parse_server_hello_tls13(hs, &server_hello, &alert, msg)) {
179 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
180 return ssl_hs_error;
181 }
182
183 // The cipher suite must be one we offered. We currently offer all supported
184 // TLS 1.3 ciphers, so check the version.
185 const SSL_CIPHER *cipher = SSL_get_cipher_by_value(server_hello.cipher_suite);
186 if (cipher == nullptr ||
187 SSL_CIPHER_get_min_version(cipher) > ssl_protocol_version(ssl) ||
188 SSL_CIPHER_get_max_version(cipher) < ssl_protocol_version(ssl)) {
189 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
190 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
191 return ssl_hs_error;
192 }
193
194 hs->new_cipher = cipher;
195
196 const bool is_hrr = is_hello_retry_request(server_hello);
197 if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
198 (is_hrr && !hs->transcript.UpdateForHelloRetryRequest())) {
199 return ssl_hs_error;
200 }
201 if (hs->selected_ech_config) {
202 if (!hs->inner_transcript.InitHash(ssl_protocol_version(ssl),
203 hs->new_cipher) ||
204 (is_hrr && !hs->inner_transcript.UpdateForHelloRetryRequest())) {
205 return ssl_hs_error;
206 }
207 }
208
209 // Determine which ClientHello the server is responding to. Run
210 // |check_ech_confirmation| unconditionally, so we validate the extension
211 // contents.
212 bool ech_accepted;
213 if (!check_ech_confirmation(hs, &ech_accepted, &alert, server_hello)) {
214 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
215 return ssl_hs_error;
216 }
217 if (hs->selected_ech_config) {
218 ssl->s3->ech_status = ech_accepted ? ssl_ech_accepted : ssl_ech_rejected;
219 }
220
221 if (!is_hrr) {
222 hs->tls13_state = state_read_server_hello;
223 return ssl_hs_ok;
224 }
225
226 // The ECH extension, if present, was already parsed by
227 // |check_ech_confirmation|.
228 SSLExtension cookie(TLSEXT_TYPE_cookie), key_share(TLSEXT_TYPE_key_share),
229 supported_versions(TLSEXT_TYPE_supported_versions),
230 ech_unused(TLSEXT_TYPE_encrypted_client_hello,
231 hs->selected_ech_config || hs->config->ech_grease_enabled);
232 if (!ssl_parse_extensions(
233 &server_hello.extensions, &alert,
234 {&cookie, &key_share, &supported_versions, &ech_unused},
235 /*ignore_unknown=*/false)) {
236 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
237 return ssl_hs_error;
238 }
239
240 if (!cookie.present && !key_share.present) {
241 OPENSSL_PUT_ERROR(SSL, SSL_R_EMPTY_HELLO_RETRY_REQUEST);
242 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
243 return ssl_hs_error;
244 }
245 if (cookie.present) {
246 CBS cookie_value;
247 if (!CBS_get_u16_length_prefixed(&cookie.data, &cookie_value) ||
248 CBS_len(&cookie_value) == 0 ||
249 CBS_len(&cookie.data) != 0) {
250 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
251 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
252 return ssl_hs_error;
253 }
254
255 if (!hs->cookie.CopyFrom(cookie_value)) {
256 return ssl_hs_error;
257 }
258 }
259
260 if (key_share.present) {
261 uint16_t group_id;
262 if (!CBS_get_u16(&key_share.data, &group_id) ||
263 CBS_len(&key_share.data) != 0) {
264 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
265 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
266 return ssl_hs_error;
267 }
268
269 // The group must be supported.
270 if (!tls1_check_group_id(hs, group_id)) {
271 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
272 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
273 return ssl_hs_error;
274 }
275
276 // Check that the HelloRetryRequest does not request a key share that was
277 // provided in the initial ClientHello.
278 if (hs->key_shares[0]->GroupID() == group_id ||
279 (hs->key_shares[1] && hs->key_shares[1]->GroupID() == group_id)) {
280 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
281 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
282 return ssl_hs_error;
283 }
284
285 if (!ssl_setup_key_shares(hs, group_id)) {
286 return ssl_hs_error;
287 }
288 }
289
290 // Although we now know whether ClientHelloInner was used, we currently
291 // maintain both transcripts up to ServerHello. We could swap transcripts
292 // early, but then ClientHello construction and |check_ech_confirmation|
293 // become more complex.
294 if (!ssl_hash_message(hs, msg)) {
295 return ssl_hs_error;
296 }
297 if (ssl->s3->ech_status == ssl_ech_accepted &&
298 !hs->inner_transcript.Update(msg.raw)) {
299 return ssl_hs_error;
300 }
301
302 // HelloRetryRequest should be the end of the flight.
303 if (ssl->method->has_unprocessed_handshake_data(ssl)) {
304 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
305 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA);
306 return ssl_hs_error;
307 }
308
309 ssl->method->next_message(ssl);
310 ssl->s3->used_hello_retry_request = true;
311 hs->tls13_state = state_send_second_client_hello;
312 // 0-RTT is rejected if we receive a HelloRetryRequest.
313 if (hs->in_early_data) {
314 ssl->s3->early_data_reason = ssl_early_data_hello_retry_request;
315 if (!close_early_data(hs, ssl_encryption_initial)) {
316 return ssl_hs_error;
317 }
318 return ssl_hs_early_data_rejected;
319 }
320 return ssl_hs_ok;
321 }
322
323 static enum ssl_hs_wait_t do_send_second_client_hello(SSL_HANDSHAKE *hs) {
324 // Any 0-RTT keys must have been discarded.
325 assert(hs->ssl->s3->write_level == ssl_encryption_initial);
326
327 // Build the second ClientHelloInner, if applicable. The second ClientHello
328 // uses an empty string for |enc|.
329 if (hs->ssl->s3->ech_status == ssl_ech_accepted &&
330 !ssl_encrypt_client_hello(hs, {})) {
331 return ssl_hs_error;
332 }
333
334 if (!ssl_add_client_hello(hs)) {
335 return ssl_hs_error;
336 }
337
338 ssl_done_writing_client_hello(hs);
339 hs->tls13_state = state_read_server_hello;
340 return ssl_hs_flush;
341 }
342
343 static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) {
344 SSL *const ssl = hs->ssl;
345 SSLMessage msg;
346 if (!ssl->method->get_message(ssl, &msg)) {
347 return ssl_hs_read_message;
348 }
349 ParsedServerHello server_hello;
350 uint8_t alert = SSL_AD_DECODE_ERROR;
351 if (!parse_server_hello_tls13(hs, &server_hello, &alert, msg)) {
352 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
353 return ssl_hs_error;
354 }
355
356 // Forbid a second HelloRetryRequest.
357 if (is_hello_retry_request(server_hello)) {
358 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
359 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
360 return ssl_hs_error;
361 }
362
363 // Check the cipher suite, in case this is after HelloRetryRequest.
364 if (SSL_CIPHER_get_protocol_id(hs->new_cipher) != server_hello.cipher_suite) {
365 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
366 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
367 return ssl_hs_error;
368 }
369
370 if (ssl->s3->ech_status == ssl_ech_accepted) {
371 if (ssl->s3->used_hello_retry_request) {
372 // HelloRetryRequest and ServerHello must accept ECH consistently.
373 bool ech_accepted;
374 if (!check_ech_confirmation(hs, &ech_accepted, &alert, server_hello)) {
375 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
376 return ssl_hs_error;
377 }
378 if (!ech_accepted) {
379 OPENSSL_PUT_ERROR(SSL, SSL_R_INCONSISTENT_ECH_NEGOTIATION);
380 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
381 return ssl_hs_error;
382 }
383 }
384
385 hs->transcript = std::move(hs->inner_transcript);
386 hs->extensions.sent = hs->inner_extensions_sent;
387 // Report the inner random value through |SSL_get_client_random|.
388 OPENSSL_memcpy(ssl->s3->client_random, hs->inner_client_random,
389 SSL3_RANDOM_SIZE);
390 }
391
392 OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_hello.random),
393 SSL3_RANDOM_SIZE);
394
395 // When offering ECH, |ssl->session| is only offered in ClientHelloInner.
396 const bool pre_shared_key_allowed =
397 ssl->session != nullptr && ssl->s3->ech_status != ssl_ech_rejected;
398 SSLExtension key_share(TLSEXT_TYPE_key_share),
399 pre_shared_key(TLSEXT_TYPE_pre_shared_key, pre_shared_key_allowed),
400 supported_versions(TLSEXT_TYPE_supported_versions);
401 if (!ssl_parse_extensions(&server_hello.extensions, &alert,
402 {&key_share, &pre_shared_key, &supported_versions},
403 /*ignore_unknown=*/false)) {
404 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
405 return ssl_hs_error;
406 }
407
408 // Recheck supported_versions, in case this is after HelloRetryRequest.
409 uint16_t version;
410 if (!supported_versions.present ||
411 !CBS_get_u16(&supported_versions.data, &version) ||
412 CBS_len(&supported_versions.data) != 0 || version != ssl->version) {
413 OPENSSL_PUT_ERROR(SSL, SSL_R_SECOND_SERVERHELLO_VERSION_MISMATCH);
414 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
415 return ssl_hs_error;
416 }
417
418 alert = SSL_AD_DECODE_ERROR;
419 if (pre_shared_key.present) {
420 if (!ssl_ext_pre_shared_key_parse_serverhello(hs, &alert,
421 &pre_shared_key.data)) {
422 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
423 return ssl_hs_error;
424 }
425
426 if (ssl->session->ssl_version != ssl->version) {
427 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
428 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
429 return ssl_hs_error;
430 }
431
432 if (ssl->session->cipher->algorithm_prf != hs->new_cipher->algorithm_prf) {
433 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_PRF_HASH_MISMATCH);
434 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
435 return ssl_hs_error;
436 }
437
438 if (!ssl_session_is_context_valid(hs, ssl->session.get())) {
439 // This is actually a client application bug.
440 OPENSSL_PUT_ERROR(SSL,
441 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
442 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
443 return ssl_hs_error;
444 }
445
446 ssl->s3->session_reused = true;
447 hs->can_release_private_key = true;
448 // Only authentication information carries over in TLS 1.3.
449 hs->new_session =
450 SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_DUP_AUTH_ONLY);
451 if (!hs->new_session) {
452 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
453 return ssl_hs_error;
454 }
455 ssl_set_session(ssl, NULL);
456
457 // Resumption incorporates fresh key material, so refresh the timeout.
458 ssl_session_renew_timeout(ssl, hs->new_session.get(),
459 ssl->session_ctx->session_psk_dhe_timeout);
460 } else if (!ssl_get_new_session(hs)) {
461 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
462 return ssl_hs_error;
463 }
464
465 hs->new_session->cipher = hs->new_cipher;
466
467 // Set up the key schedule and incorporate the PSK into the running secret.
468 size_t hash_len = EVP_MD_size(
469 ssl_get_handshake_digest(ssl_protocol_version(ssl), hs->new_cipher));
470 if (!tls13_init_key_schedule(
471 hs, ssl->s3->session_reused
472 ? MakeConstSpan(hs->new_session->secret,
473 hs->new_session->secret_length)
474 : MakeConstSpan(kZeroes, hash_len))) {
475 return ssl_hs_error;
476 }
477
478 if (!key_share.present) {
479 // We do not support psk_ke and thus always require a key share.
480 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE);
481 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION);
482 return ssl_hs_error;
483 }
484
485 // Resolve ECDHE and incorporate it into the secret.
486 Array<uint8_t> dhe_secret;
487 alert = SSL_AD_DECODE_ERROR;
488 if (!ssl_ext_key_share_parse_serverhello(hs, &dhe_secret, &ssl->s3->peer_key,
489 &alert, &key_share.data)) {
490 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
491 return ssl_hs_error;
492 }
493
494 if (!tls13_advance_key_schedule(hs, dhe_secret) ||
495 !ssl_hash_message(hs, msg) || !tls13_derive_handshake_secrets(hs)) {
496 return ssl_hs_error;
497 }
498
499 // If currently sending early data over TCP, we defer installing client
500 // traffic keys to when the early data stream is closed. See
501 // |close_early_data|. Note if the server has already rejected 0-RTT via
502 // HelloRetryRequest, |in_early_data| is already false.
503 if (!hs->in_early_data || ssl->quic_method != nullptr) {
504 if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_seal,
505 hs->new_session.get(),
506 hs->client_handshake_secret())) {
507 return ssl_hs_error;
508 }
509 }
510
511 if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_open,
512 hs->new_session.get(),
513 hs->server_handshake_secret())) {
514 return ssl_hs_error;
515 }
516
517 ssl->method->next_message(ssl);
518 hs->tls13_state = state_read_encrypted_extensions;
519 return ssl_hs_ok;
520 }
521
522 static enum ssl_hs_wait_t do_read_encrypted_extensions(SSL_HANDSHAKE *hs) {
523 SSL *const ssl = hs->ssl;
524 SSLMessage msg;
525 if (!ssl->method->get_message(ssl, &msg)) {
526 return ssl_hs_read_message;
527 }
528 if (!ssl_check_message_type(ssl, msg, SSL3_MT_ENCRYPTED_EXTENSIONS)) {
529 return ssl_hs_error;
530 }
531
532 CBS body = msg.body, extensions;
533 if (!CBS_get_u16_length_prefixed(&body, &extensions) ||
534 CBS_len(&body) != 0) {
535 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
536 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
537 return ssl_hs_error;
538 }
539
540 if (!ssl_parse_serverhello_tlsext(hs, &extensions)) {
541 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
542 return ssl_hs_error;
543 }
544
545 if (ssl->s3->early_data_accepted) {
546 // The extension parser checks the server resumed the session.
547 assert(ssl->s3->session_reused);
548 // If offering ECH, the server may not accept early data with
549 // ClientHelloOuter. We do not offer sessions with ClientHelloOuter, so this
550 // this should be implied by checking |session_reused|.
551 assert(ssl->s3->ech_status != ssl_ech_rejected);
552
553 if (hs->early_session->cipher != hs->new_session->cipher) {
554 OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_MISMATCH_ON_EARLY_DATA);
555 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
556 return ssl_hs_error;
557 }
558 if (MakeConstSpan(hs->early_session->early_alpn) !=
559 ssl->s3->alpn_selected) {
560 OPENSSL_PUT_ERROR(SSL, SSL_R_ALPN_MISMATCH_ON_EARLY_DATA);
561 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
562 return ssl_hs_error;
563 }
564 // Channel ID is incompatible with 0-RTT. The ALPS extension should be
565 // negotiated implicitly.
566 if (hs->channel_id_negotiated ||
567 hs->new_session->has_application_settings) {
568 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION_ON_EARLY_DATA);
569 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
570 return ssl_hs_error;
571 }
572 hs->new_session->has_application_settings =
573 hs->early_session->has_application_settings;
574 if (!hs->new_session->local_application_settings.CopyFrom(
575 hs->early_session->local_application_settings) ||
576 !hs->new_session->peer_application_settings.CopyFrom(
577 hs->early_session->peer_application_settings)) {
578 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
579 return ssl_hs_error;
580 }
581 if (hs->received_custom_extension) {
582 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION_ON_EARLY_DATA);
583 return ssl_hs_error;
584 }
585 }
586
587 // Store the negotiated ALPN in the session.
588 if (!hs->new_session->early_alpn.CopyFrom(ssl->s3->alpn_selected)) {
589 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
590 return ssl_hs_error;
591 }
592
593 if (!ssl_hash_message(hs, msg)) {
594 return ssl_hs_error;
595 }
596
597 ssl->method->next_message(ssl);
598 hs->tls13_state = state_read_certificate_request;
599 if (hs->in_early_data && !ssl->s3->early_data_accepted) {
600 if (!close_early_data(hs, ssl_encryption_handshake)) {
601 return ssl_hs_error;
602 }
603 return ssl_hs_early_data_rejected;
604 }
605 return ssl_hs_ok;
606 }
607
608 static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) {
609 SSL *const ssl = hs->ssl;
610 // CertificateRequest may only be sent in non-resumption handshakes.
611 if (ssl->s3->session_reused) {
612 if (ssl->ctx->reverify_on_resume && !ssl->s3->early_data_accepted) {
613 hs->tls13_state = state_server_certificate_reverify;
614 return ssl_hs_ok;
615 }
616 hs->tls13_state = state_read_server_finished;
617 return ssl_hs_ok;
618 }
619
620 SSLMessage msg;
621 if (!ssl->method->get_message(ssl, &msg)) {
622 return ssl_hs_read_message;
623 }
624
625 // CertificateRequest is optional.
626 if (msg.type != SSL3_MT_CERTIFICATE_REQUEST) {
627 hs->tls13_state = state_read_server_certificate;
628 return ssl_hs_ok;
629 }
630
631
632 SSLExtension sigalgs(TLSEXT_TYPE_signature_algorithms),
633 ca(TLSEXT_TYPE_certificate_authorities);
634 CBS body = msg.body, context, extensions, supported_signature_algorithms;
635 uint8_t alert = SSL_AD_DECODE_ERROR;
636 if (!CBS_get_u8_length_prefixed(&body, &context) ||
637 // The request context is always empty during the handshake.
638 CBS_len(&context) != 0 ||
639 !CBS_get_u16_length_prefixed(&body, &extensions) || //
640 CBS_len(&body) != 0 ||
641 !ssl_parse_extensions(&extensions, &alert, {&sigalgs, &ca},
642 /*ignore_unknown=*/true) ||
643 !sigalgs.present ||
644 !CBS_get_u16_length_prefixed(&sigalgs.data,
645 &supported_signature_algorithms) ||
646 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
647 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
648 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
649 return ssl_hs_error;
650 }
651
652 if (ca.present) {
653 hs->ca_names = ssl_parse_client_CA_list(ssl, &alert, &ca.data);
654 if (!hs->ca_names) {
655 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
656 return ssl_hs_error;
657 }
658 } else {
659 hs->ca_names.reset(sk_CRYPTO_BUFFER_new_null());
660 if (!hs->ca_names) {
661 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
662 return ssl_hs_error;
663 }
664 }
665
666 hs->cert_request = true;
667 ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
668
669 if (!ssl_hash_message(hs, msg)) {
670 return ssl_hs_error;
671 }
672
673 ssl->method->next_message(ssl);
674 hs->tls13_state = state_read_server_certificate;
675 return ssl_hs_ok;
676 }
677
678 static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) {
679 SSL *const ssl = hs->ssl;
680 SSLMessage msg;
681 if (!ssl->method->get_message(ssl, &msg)) {
682 return ssl_hs_read_message;
683 }
684
685 if (msg.type != SSL3_MT_COMPRESSED_CERTIFICATE &&
686 !ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE)) {
687 return ssl_hs_error;
688 }
689
690 if (!tls13_process_certificate(hs, msg, false /* certificate required */) ||
691 !ssl_hash_message(hs, msg)) {
692 return ssl_hs_error;
693 }
694
695 ssl->method->next_message(ssl);
696 hs->tls13_state = state_read_server_certificate_verify;
697 return ssl_hs_ok;
698 }
699
700 static enum ssl_hs_wait_t do_read_server_certificate_verify(SSL_HANDSHAKE *hs) {
701 SSL *const ssl = hs->ssl;
702 SSLMessage msg;
703 if (!ssl->method->get_message(ssl, &msg)) {
704 return ssl_hs_read_message;
705 }
706 switch (ssl_verify_peer_cert(hs)) {
707 case ssl_verify_ok:
708 break;
709 case ssl_verify_invalid:
710 return ssl_hs_error;
711 case ssl_verify_retry:
712 hs->tls13_state = state_read_server_certificate_verify;
713 return ssl_hs_certificate_verify;
714 }
715
716 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY) ||
717 !tls13_process_certificate_verify(hs, msg) ||
718 !ssl_hash_message(hs, msg)) {
719 return ssl_hs_error;
720 }
721
722 ssl->method->next_message(ssl);
723 hs->tls13_state = state_read_server_finished;
724 return ssl_hs_ok;
725 }
726
727 static enum ssl_hs_wait_t do_server_certificate_reverify(SSL_HANDSHAKE *hs) {
728 switch (ssl_reverify_peer_cert(hs, /*send_alert=*/true)) {
729 case ssl_verify_ok:
730 break;
731 case ssl_verify_invalid:
732 return ssl_hs_error;
733 case ssl_verify_retry:
734 hs->tls13_state = state_server_certificate_reverify;
735 return ssl_hs_certificate_verify;
736 }
737 hs->tls13_state = state_read_server_finished;
738 return ssl_hs_ok;
739 }
740
741 static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) {
742 SSL *const ssl = hs->ssl;
743 SSLMessage msg;
744 if (!ssl->method->get_message(ssl, &msg)) {
745 return ssl_hs_read_message;
746 }
747 if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED) ||
748 !tls13_process_finished(hs, msg, false /* don't use saved value */) ||
749 !ssl_hash_message(hs, msg) ||
750 // Update the secret to the master secret and derive traffic keys.
751 !tls13_advance_key_schedule(
752 hs, MakeConstSpan(kZeroes, hs->transcript.DigestLen())) ||
753 !tls13_derive_application_secrets(hs)) {
754 return ssl_hs_error;
755 }
756
757 // Finished should be the end of the flight.
758 if (ssl->method->has_unprocessed_handshake_data(ssl)) {
759 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
760 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA);
761 return ssl_hs_error;
762 }
763
764 ssl->method->next_message(ssl);
765 hs->tls13_state = state_send_end_of_early_data;
766 return ssl_hs_ok;
767 }
768
769 static enum ssl_hs_wait_t do_send_end_of_early_data(SSL_HANDSHAKE *hs) {
770 SSL *const ssl = hs->ssl;
771
772 if (ssl->s3->early_data_accepted) {
773 // QUIC omits the EndOfEarlyData message. See RFC 9001, section 8.3.
774 if (ssl->quic_method == nullptr) {
775 ScopedCBB cbb;
776 CBB body;
777 if (!ssl->method->init_message(ssl, cbb.get(), &body,
778 SSL3_MT_END_OF_EARLY_DATA) ||
779 !ssl_add_message_cbb(ssl, cbb.get())) {
780 return ssl_hs_error;
781 }
782 }
783
784 if (!close_early_data(hs, ssl_encryption_handshake)) {
785 return ssl_hs_error;
786 }
787 }
788
789 hs->tls13_state = state_send_client_encrypted_extensions;
790 return ssl_hs_ok;
791 }
792
793 static enum ssl_hs_wait_t do_send_client_encrypted_extensions(
794 SSL_HANDSHAKE *hs) {
795 SSL *const ssl = hs->ssl;
796 // For now, only one extension uses client EncryptedExtensions. This function
797 // may be generalized if others use it in the future.
798 if (hs->new_session->has_application_settings &&
799 !ssl->s3->early_data_accepted) {
800 ScopedCBB cbb;
801 CBB body, extensions, extension;
802 uint16_t extension_type = TLSEXT_TYPE_application_settings_old;
803 if (hs->config->alps_use_new_codepoint) {
804 extension_type = TLSEXT_TYPE_application_settings;
805 }
806 if (!ssl->method->init_message(ssl, cbb.get(), &body,
807 SSL3_MT_ENCRYPTED_EXTENSIONS) ||
808 !CBB_add_u16_length_prefixed(&body, &extensions) ||
809 !CBB_add_u16(&extensions, extension_type) ||
810 !CBB_add_u16_length_prefixed(&extensions, &extension) ||
811 !CBB_add_bytes(&extension,
812 hs->new_session->local_application_settings.data(),
813 hs->new_session->local_application_settings.size()) ||
814 !ssl_add_message_cbb(ssl, cbb.get())) {
815 return ssl_hs_error;
816 }
817 }
818
819 hs->tls13_state = state_send_client_certificate;
820 return ssl_hs_ok;
821 }
822
823 static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
824 SSL *const ssl = hs->ssl;
825
826 // The peer didn't request a certificate.
827 if (!hs->cert_request) {
828 hs->tls13_state = state_complete_second_flight;
829 return ssl_hs_ok;
830 }
831
832 if (ssl->s3->ech_status == ssl_ech_rejected) {
833 // Do not send client certificates on ECH reject. We have not authenticated
834 // the server for the name that can learn the certificate.
835 SSL_certs_clear(ssl);
836 } else if (hs->config->cert->cert_cb != nullptr) {
837 // Call cert_cb to update the certificate.
838 int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg);
839 if (rv == 0) {
840 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
841 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
842 return ssl_hs_error;
843 }
844 if (rv < 0) {
845 hs->tls13_state = state_send_client_certificate;
846 return ssl_hs_x509_lookup;
847 }
848 }
849
850 if (!ssl_on_certificate_selected(hs) ||
851 !tls13_add_certificate(hs)) {
852 return ssl_hs_error;
853 }
854
855 hs->tls13_state = state_send_client_certificate_verify;
856 return ssl_hs_ok;
857 }
858
859 static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) {
860 // Don't send CertificateVerify if there is no certificate.
861 if (!ssl_has_certificate(hs)) {
862 hs->tls13_state = state_complete_second_flight;
863 return ssl_hs_ok;
864 }
865
866 if (!tls1_choose_signature_algorithm(hs, &hs->signature_algorithm)) {
867 ssl_send_alert(hs->ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
868 return ssl_hs_error;
869 }
870
871 switch (tls13_add_certificate_verify(hs)) {
872 case ssl_private_key_success:
873 hs->tls13_state = state_complete_second_flight;
874 return ssl_hs_ok;
875
876 case ssl_private_key_retry:
877 hs->tls13_state = state_send_client_certificate_verify;
878 return ssl_hs_private_key_operation;
879
880 case ssl_private_key_failure:
881 return ssl_hs_error;
882 }
883
884 assert(0);
885 return ssl_hs_error;
886 }
887
888 static enum ssl_hs_wait_t do_complete_second_flight(SSL_HANDSHAKE *hs) {
889 SSL *const ssl = hs->ssl;
890 hs->can_release_private_key = true;
891
892 // Send a Channel ID assertion if necessary.
893 if (hs->channel_id_negotiated) {
894 ScopedCBB cbb;
895 CBB body;
896 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) ||
897 !tls1_write_channel_id(hs, &body) ||
898 !ssl_add_message_cbb(ssl, cbb.get())) {
899 return ssl_hs_error;
900 }
901 }
902
903 // Send a Finished message.
904 if (!tls13_add_finished(hs)) {
905 return ssl_hs_error;
906 }
907
908 // Derive the final keys and enable them.
909 if (!tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_seal,
910 hs->new_session.get(),
911 hs->client_traffic_secret_0()) ||
912 !tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_open,
913 hs->new_session.get(),
914 hs->server_traffic_secret_0()) ||
915 !tls13_derive_resumption_secret(hs)) {
916 return ssl_hs_error;
917 }
918
919 hs->tls13_state = state_done;
920 return ssl_hs_flush;
921 }
922
923 enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs) {
924 while (hs->tls13_state != state_done) {
925 enum ssl_hs_wait_t ret = ssl_hs_error;
926 enum client_hs_state_t state =
927 static_cast<enum client_hs_state_t>(hs->tls13_state);
928 switch (state) {
929 case state_read_hello_retry_request:
930 ret = do_read_hello_retry_request(hs);
931 break;
932 case state_send_second_client_hello:
933 ret = do_send_second_client_hello(hs);
934 break;
935 case state_read_server_hello:
936 ret = do_read_server_hello(hs);
937 break;
938 case state_read_encrypted_extensions:
939 ret = do_read_encrypted_extensions(hs);
940 break;
941 case state_read_certificate_request:
942 ret = do_read_certificate_request(hs);
943 break;
944 case state_read_server_certificate:
945 ret = do_read_server_certificate(hs);
946 break;
947 case state_read_server_certificate_verify:
948 ret = do_read_server_certificate_verify(hs);
949 break;
950 case state_server_certificate_reverify:
951 ret = do_server_certificate_reverify(hs);
952 break;
953 case state_read_server_finished:
954 ret = do_read_server_finished(hs);
955 break;
956 case state_send_end_of_early_data:
957 ret = do_send_end_of_early_data(hs);
958 break;
959 case state_send_client_certificate:
960 ret = do_send_client_certificate(hs);
961 break;
962 case state_send_client_encrypted_extensions:
963 ret = do_send_client_encrypted_extensions(hs);
964 break;
965 case state_send_client_certificate_verify:
966 ret = do_send_client_certificate_verify(hs);
967 break;
968 case state_complete_second_flight:
969 ret = do_complete_second_flight(hs);
970 break;
971 case state_done:
972 ret = ssl_hs_ok;
973 break;
974 }
975
976 if (hs->tls13_state != state) {
977 ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1);
978 }
979
980 if (ret != ssl_hs_ok) {
981 return ret;
982 }
983 }
984
985 return ssl_hs_ok;
986 }
987
988 const char *tls13_client_handshake_state(SSL_HANDSHAKE *hs) {
989 enum client_hs_state_t state =
990 static_cast<enum client_hs_state_t>(hs->tls13_state);
991 switch (state) {
992 case state_read_hello_retry_request:
993 return "TLS 1.3 client read_hello_retry_request";
994 case state_send_second_client_hello:
995 return "TLS 1.3 client send_second_client_hello";
996 case state_read_server_hello:
997 return "TLS 1.3 client read_server_hello";
998 case state_read_encrypted_extensions:
999 return "TLS 1.3 client read_encrypted_extensions";
1000 case state_read_certificate_request:
1001 return "TLS 1.3 client read_certificate_request";
1002 case state_read_server_certificate:
1003 return "TLS 1.3 client read_server_certificate";
1004 case state_read_server_certificate_verify:
1005 return "TLS 1.3 client read_server_certificate_verify";
1006 case state_server_certificate_reverify:
1007 return "TLS 1.3 client server_certificate_reverify";
1008 case state_read_server_finished:
1009 return "TLS 1.3 client read_server_finished";
1010 case state_send_end_of_early_data:
1011 return "TLS 1.3 client send_end_of_early_data";
1012 case state_send_client_encrypted_extensions:
1013 return "TLS 1.3 client send_client_encrypted_extensions";
1014 case state_send_client_certificate:
1015 return "TLS 1.3 client send_client_certificate";
1016 case state_send_client_certificate_verify:
1017 return "TLS 1.3 client send_client_certificate_verify";
1018 case state_complete_second_flight:
1019 return "TLS 1.3 client complete_second_flight";
1020 case state_done:
1021 return "TLS 1.3 client done";
1022 }
1023
1024 return "TLS 1.3 client unknown";
1025 }
1026
1027 bool tls13_process_new_session_ticket(SSL *ssl, const SSLMessage &msg) {
1028 if (ssl->s3->write_shutdown != ssl_shutdown_none) {
1029 // Ignore tickets on shutdown. Callers tend to indiscriminately call
1030 // |SSL_shutdown| before destroying an |SSL|, at which point calling the new
1031 // session callback may be confusing.
1032 return true;
1033 }
1034
1035 CBS body = msg.body;
1036 UniquePtr<SSL_SESSION> session = tls13_create_session_with_ticket(ssl, &body);
1037 if (!session) {
1038 return false;
1039 }
1040
1041 if ((ssl->session_ctx->session_cache_mode & SSL_SESS_CACHE_CLIENT) &&
1042 ssl->session_ctx->new_session_cb != NULL &&
1043 ssl->session_ctx->new_session_cb(ssl, session.get())) {
1044 // |new_session_cb|'s return value signals that it took ownership.
1045 session.release();
1046 }
1047
1048 return true;
1049 }
1050
1051 UniquePtr<SSL_SESSION> tls13_create_session_with_ticket(SSL *ssl, CBS *body) {
1052 UniquePtr<SSL_SESSION> session = SSL_SESSION_dup(
1053 ssl->s3->established_session.get(), SSL_SESSION_INCLUDE_NONAUTH);
1054 if (!session) {
1055 return nullptr;
1056 }
1057
1058 ssl_session_rebase_time(ssl, session.get());
1059
1060 uint32_t server_timeout;
1061 CBS ticket_nonce, ticket, extensions;
1062 if (!CBS_get_u32(body, &server_timeout) ||
1063 !CBS_get_u32(body, &session->ticket_age_add) ||
1064 !CBS_get_u8_length_prefixed(body, &ticket_nonce) ||
1065 !CBS_get_u16_length_prefixed(body, &ticket) ||
1066 CBS_len(&ticket) == 0 || //
1067 !session->ticket.CopyFrom(ticket) ||
1068 !CBS_get_u16_length_prefixed(body, &extensions) || //
1069 CBS_len(body) != 0) {
1070 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1071 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1072 return nullptr;
1073 }
1074
1075 // Cap the renewable lifetime by the server advertised value. This avoids
1076 // wasting bandwidth on 0-RTT when we know the server will reject it.
1077 if (session->timeout > server_timeout) {
1078 session->timeout = server_timeout;
1079 }
1080
1081 if (!tls13_derive_session_psk(session.get(), ticket_nonce)) {
1082 return nullptr;
1083 }
1084
1085 SSLExtension early_data(TLSEXT_TYPE_early_data);
1086 uint8_t alert = SSL_AD_DECODE_ERROR;
1087 if (!ssl_parse_extensions(&extensions, &alert, {&early_data},
1088 /*ignore_unknown=*/true)) {
1089 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
1090 return nullptr;
1091 }
1092
1093 if (early_data.present) {
1094 if (!CBS_get_u32(&early_data.data, &session->ticket_max_early_data) ||
1095 CBS_len(&early_data.data) != 0) {
1096 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1097 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1098 return nullptr;
1099 }
1100
1101 // QUIC does not use the max_early_data_size parameter and always sets it to
1102 // a fixed value. See RFC 9001, section 4.6.1.
1103 if (ssl->quic_method != nullptr &&
1104 session->ticket_max_early_data != 0xffffffff) {
1105 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1106 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1107 return nullptr;
1108 }
1109 }
1110
1111 // Historically, OpenSSL filled in fake session IDs for ticket-based sessions.
1112 // Envoy's tests depend on this, although perhaps they shouldn't.
1113 SHA256(CBS_data(&ticket), CBS_len(&ticket), session->session_id);
1114 session->session_id_length = SHA256_DIGEST_LENGTH;
1115
1116 session->ticket_age_add_valid = true;
1117 session->not_resumable = false;
1118
1119 return session;
1120 }
1121
1122 BSSL_NAMESPACE_END