Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/ssl/tls13_server.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 <string.h>
8
9 #include <tuple>
10
11 #include <openssl/aead.h>
12 #include <openssl/bytestring.h>
13 #include <openssl/digest.h>
14 #include <openssl/err.h>
15 #include <openssl/hpke.h>
16 #include <openssl/mem.h>
17 #include <openssl/rand.h>
18 #include <openssl/stack.h>
19
20 #include "../crypto/internal.h"
21 #include "internal.h"
22
23
24 BSSL_NAMESPACE_BEGIN
25
26 static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0};
27
28 // Allow a minute of ticket age skew in either direction. This covers
29 // transmission delays in ClientHello and NewSessionTicket, as well as
30 // drift between client and server clock rate since the ticket was issued.
31 // See RFC 8446, section 8.3.
32 static const int32_t kMaxTicketAgeSkewSeconds = 60;
33
34 static bool resolve_ecdhe_secret(SSL_HANDSHAKE *hs,
35 const SSL_CLIENT_HELLO *client_hello) {
36 SSL *const ssl = hs->ssl;
37 const uint16_t group_id = hs->new_session->group_id;
38
39 bool found_key_share;
40 Span<const uint8_t> peer_key;
41 uint8_t alert = SSL_AD_DECODE_ERROR;
42 if (!ssl_ext_key_share_parse_clienthello(hs, &found_key_share, &peer_key,
43 &alert, client_hello)) {
44 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
45 return false;
46 }
47
48 if (!found_key_share) {
49 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
50 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
51 return false;
52 }
53
54 Array<uint8_t> secret;
55 SSL_HANDSHAKE_HINTS *const hints = hs->hints.get();
56 if (hints && !hs->hints_requested && hints->key_share_group_id == group_id &&
57 !hints->key_share_secret.empty()) {
58 // Copy DH secret from hints.
59 if (!hs->ecdh_public_key.CopyFrom(hints->key_share_public_key) ||
60 !secret.CopyFrom(hints->key_share_secret)) {
61 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
62 return false;
63 }
64 } else {
65 ScopedCBB public_key;
66 UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id);
67 if (!key_share || //
68 !CBB_init(public_key.get(), 32) ||
69 !key_share->Accept(public_key.get(), &secret, &alert, peer_key) ||
70 !CBBFinishArray(public_key.get(), &hs->ecdh_public_key) ||
71 // Save peer's public key for observation with |SSL_get_peer_tmp_key|.
72 !ssl->s3->peer_key.CopyFrom(peer_key)) {
73 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
74 return false;
75 }
76 if (hints && hs->hints_requested) {
77 hints->key_share_group_id = group_id;
78 if (!hints->key_share_public_key.CopyFrom(hs->ecdh_public_key) ||
79 !hints->key_share_secret.CopyFrom(secret)) {
80 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
81 return false;
82 }
83 }
84 }
85
86 return tls13_advance_key_schedule(hs, secret);
87 }
88
89 static int ssl_ext_supported_versions_add_serverhello(SSL_HANDSHAKE *hs,
90 CBB *out) {
91 CBB contents;
92 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
93 !CBB_add_u16_length_prefixed(out, &contents) ||
94 !CBB_add_u16(&contents, hs->ssl->version) ||
95 !CBB_flush(out)) {
96 return 0;
97 }
98
99 return 1;
100 }
101
102 static const SSL_CIPHER *choose_tls13_cipher(const SSL *ssl) {
103 STACK_OF(SSL_CIPHER) *tls13_ciphers = nullptr;
104 // First check config, otherwise fallback to ctx preferences.
105 if (ssl->config && ssl->config->tls13_cipher_list &&
106 ssl->config->tls13_cipher_list.get()->ciphers &&
107 sk_SSL_CIPHER_num(ssl->config->tls13_cipher_list.get()->ciphers.get()) > 0) {
108 tls13_ciphers = ssl->config->tls13_cipher_list.get()->ciphers.get();
109 } else if (ssl->ctx->tls13_cipher_list &&
110 ssl->ctx->tls13_cipher_list.get()->ciphers &&
111 sk_SSL_CIPHER_num(ssl->ctx->tls13_cipher_list.get()->ciphers.get()) > 0) {
112 tls13_ciphers = ssl->ctx->tls13_cipher_list.get()->ciphers.get();
113 }
114
115 return ssl_choose_tls13_cipher(ssl->client_cipher_suites.get(),
116 ssl->config->aes_hw_override
117 ? ssl->config->aes_hw_override_value
118 : EVP_has_aes_hardware(),
119 ssl_protocol_version(ssl), tls13_ciphers);
120 }
121
122 static bool add_new_session_tickets(SSL_HANDSHAKE *hs, bool *out_sent_tickets) {
123 SSL *const ssl = hs->ssl;
124 if (// If the client doesn't accept resumption with PSK_DHE_KE, don't send a
125 // session ticket.
126 !hs->accept_psk_mode ||
127 // We only implement stateless resumption in TLS 1.3, so skip sending
128 // tickets if disabled.
129 (SSL_get_options(ssl) & SSL_OP_NO_TICKET)) {
130 *out_sent_tickets = false;
131 return true;
132 }
133
134 // Rebase the session timestamp so that it is measured from ticket
135 // issuance.
136 ssl_session_rebase_time(ssl, hs->new_session.get());
137
138 assert(ssl->session_ctx->num_tickets <= kMaxTickets);
139 for (size_t i = 0; i < ssl->session_ctx->num_tickets; i++) {
140 UniquePtr<SSL_SESSION> session(
141 SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_INCLUDE_NONAUTH));
142 if (!session) {
143 return false;
144 }
145
146 if (!RAND_bytes((uint8_t *)&session->ticket_age_add, 4)) {
147 return false;
148 }
149 session->ticket_age_add_valid = true;
150 bool enable_early_data =
151 ssl->enable_early_data &&
152 (!ssl->quic_method || !ssl->config->quic_early_data_context.empty());
153 if (enable_early_data) {
154 // QUIC does not use the max_early_data_size parameter and always sets it
155 // to a fixed value. See RFC 9001, section 4.6.1.
156 session->ticket_max_early_data =
157 ssl->quic_method != nullptr ? 0xffffffff : kMaxEarlyDataAccepted;
158 }
159
160 static_assert(kMaxTickets < 256, "Too many tickets");
161 assert(i < 256);
162 uint8_t nonce[] = {static_cast<uint8_t>(i)};
163
164 ScopedCBB cbb;
165 CBB body, nonce_cbb, ticket, extensions;
166 if (!ssl->method->init_message(ssl, cbb.get(), &body,
167 SSL3_MT_NEW_SESSION_TICKET) ||
168 !CBB_add_u32(&body, session->timeout) ||
169 !CBB_add_u32(&body, session->ticket_age_add) ||
170 !CBB_add_u8_length_prefixed(&body, &nonce_cbb) ||
171 !CBB_add_bytes(&nonce_cbb, nonce, sizeof(nonce)) ||
172 !CBB_add_u16_length_prefixed(&body, &ticket) ||
173 !tls13_derive_session_psk(session.get(), nonce) ||
174 !ssl_encrypt_ticket(hs, &ticket, session.get()) ||
175 !CBB_add_u16_length_prefixed(&body, &extensions)) {
176 return false;
177 }
178
179 if (enable_early_data) {
180 CBB early_data;
181 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_early_data) ||
182 !CBB_add_u16_length_prefixed(&extensions, &early_data) ||
183 !CBB_add_u32(&early_data, session->ticket_max_early_data) ||
184 !CBB_flush(&extensions)) {
185 return false;
186 }
187 }
188
189 // Add a fake extension. See RFC 8701.
190 if (!CBB_add_u16(&extensions,
191 ssl_get_grease_value(hs, ssl_grease_ticket_extension)) ||
192 !CBB_add_u16(&extensions, 0 /* empty */)) {
193 return false;
194 }
195
196 if (!ssl_add_message_cbb(ssl, cbb.get())) {
197 return false;
198 }
199 }
200
201 *out_sent_tickets = true;
202 return true;
203 }
204
205 static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) {
206 // At this point, most ClientHello extensions have already been processed by
207 // the common handshake logic. Resolve the remaining non-PSK parameters.
208 SSL *const ssl = hs->ssl;
209 SSLMessage msg;
210 SSL_CLIENT_HELLO client_hello;
211 if (!hs->GetClientHello(&msg, &client_hello)) {
212 return ssl_hs_error;
213 }
214
215 if (ssl->quic_method != nullptr && client_hello.session_id_len > 0) {
216 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_COMPATIBILITY_MODE);
217 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
218 return ssl_hs_error;
219 }
220 OPENSSL_memcpy(hs->session_id, client_hello.session_id,
221 client_hello.session_id_len);
222 hs->session_id_len = client_hello.session_id_len;
223
224 if (!ssl_parse_client_cipher_list(ssl, &client_hello, &ssl->client_cipher_suites)) {
225 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
226 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
227 return ssl_hs_error;
228 }
229
230 // Negotiate the cipher suite.
231 hs->new_cipher = choose_tls13_cipher(ssl);
232 if (hs->new_cipher == NULL) {
233 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
234 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
235 return ssl_hs_error;
236 }
237
238 // Negotiate the signature algorithms.
239 if (!tls1_choose_signature_algorithm(hs, &hs->signature_algorithm)) {
240 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
241 return ssl_hs_error;
242 }
243
244 if (!ssl_on_certificate_selected(hs)) {
245 return ssl_hs_error;
246 }
247
248 if (!tls1_call_ocsp_stapling_callback(hs)) {
249 return ssl_hs_error;
250 }
251
252 // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
253 // deferred. Complete it now.
254 uint8_t alert = SSL_AD_DECODE_ERROR;
255 if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
256 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
257 return ssl_hs_error;
258 }
259
260 // The PRF hash is now known.
261 if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher)) {
262 return ssl_hs_error;
263 }
264
265 hs->tls13_state = state13_select_session;
266 return ssl_hs_ok;
267 }
268
269 static enum ssl_ticket_aead_result_t select_session(
270 SSL_HANDSHAKE *hs, uint8_t *out_alert, UniquePtr<SSL_SESSION> *out_session,
271 int32_t *out_ticket_age_skew, bool *out_offered_ticket,
272 const SSLMessage &msg, const SSL_CLIENT_HELLO *client_hello) {
273 SSL *const ssl = hs->ssl;
274 *out_session = nullptr;
275
276 CBS pre_shared_key;
277 *out_offered_ticket = ssl_client_hello_get_extension(
278 client_hello, &pre_shared_key, TLSEXT_TYPE_pre_shared_key);
279 if (!*out_offered_ticket) {
280 return ssl_ticket_aead_ignore_ticket;
281 }
282
283 // Per RFC 8446, section 4.2.9, servers MUST abort the handshake if the client
284 // sends pre_shared_key without psk_key_exchange_modes.
285 CBS unused;
286 if (!ssl_client_hello_get_extension(client_hello, &unused,
287 TLSEXT_TYPE_psk_key_exchange_modes)) {
288 *out_alert = SSL_AD_MISSING_EXTENSION;
289 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
290 return ssl_ticket_aead_error;
291 }
292
293 CBS ticket, binders;
294 uint32_t client_ticket_age;
295 if (!ssl_ext_pre_shared_key_parse_clienthello(
296 hs, &ticket, &binders, &client_ticket_age, out_alert, client_hello,
297 &pre_shared_key)) {
298 return ssl_ticket_aead_error;
299 }
300
301 // If the peer did not offer psk_dhe, ignore the resumption.
302 if (!hs->accept_psk_mode) {
303 return ssl_ticket_aead_ignore_ticket;
304 }
305
306 // TLS 1.3 session tickets are renewed separately as part of the
307 // NewSessionTicket.
308 bool unused_renew;
309 UniquePtr<SSL_SESSION> session;
310 enum ssl_ticket_aead_result_t ret =
311 ssl_process_ticket(hs, &session, &unused_renew, ticket, {});
312 switch (ret) {
313 case ssl_ticket_aead_success:
314 break;
315 case ssl_ticket_aead_error:
316 *out_alert = SSL_AD_INTERNAL_ERROR;
317 return ret;
318 default:
319 return ret;
320 }
321
322 if (!ssl_session_is_resumable(hs, session.get()) ||
323 // Historically, some TLS 1.3 tickets were missing ticket_age_add.
324 !session->ticket_age_add_valid) {
325 return ssl_ticket_aead_ignore_ticket;
326 }
327
328 // Recover the client ticket age and convert to seconds.
329 client_ticket_age -= session->ticket_age_add;
330 client_ticket_age /= 1000;
331
332 struct OPENSSL_timeval now;
333 ssl_get_current_time(ssl, &now);
334
335 // Compute the server ticket age in seconds.
336 assert(now.tv_sec >= session->time);
337 uint64_t server_ticket_age = now.tv_sec - session->time;
338
339 // To avoid overflowing |hs->ticket_age_skew|, we will not resume
340 // 68-year-old sessions.
341 if (server_ticket_age > INT32_MAX) {
342 return ssl_ticket_aead_ignore_ticket;
343 }
344
345 *out_ticket_age_skew = static_cast<int32_t>(client_ticket_age) -
346 static_cast<int32_t>(server_ticket_age);
347
348 // Check the PSK binder.
349 if (!tls13_verify_psk_binder(hs, session.get(), msg, &binders)) {
350 *out_alert = SSL_AD_DECRYPT_ERROR;
351 return ssl_ticket_aead_error;
352 }
353
354 *out_session = std::move(session);
355 return ssl_ticket_aead_success;
356 }
357
358 static bool quic_ticket_compatible(const SSL_SESSION *session,
359 const SSL_CONFIG *config) {
360 if (!session->is_quic) {
361 return true;
362 }
363
364 if (session->quic_early_data_context.empty() ||
365 config->quic_early_data_context.size() !=
366 session->quic_early_data_context.size() ||
367 CRYPTO_memcmp(config->quic_early_data_context.data(),
368 session->quic_early_data_context.data(),
369 session->quic_early_data_context.size()) != 0) {
370 return false;
371 }
372 return true;
373 }
374
375 static enum ssl_hs_wait_t do_select_session(SSL_HANDSHAKE *hs) {
376 SSL *const ssl = hs->ssl;
377 SSLMessage msg;
378 SSL_CLIENT_HELLO client_hello;
379 if (!hs->GetClientHello(&msg, &client_hello)) {
380 return ssl_hs_error;
381 }
382
383 uint8_t alert = SSL_AD_DECODE_ERROR;
384 UniquePtr<SSL_SESSION> session;
385 bool offered_ticket = false;
386 switch (select_session(hs, &alert, &session, &ssl->s3->ticket_age_skew,
387 &offered_ticket, msg, &client_hello)) {
388 case ssl_ticket_aead_ignore_ticket:
389 assert(!session);
390 if (!ssl_get_new_session(hs)) {
391 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
392 return ssl_hs_error;
393 }
394 break;
395
396 case ssl_ticket_aead_success:
397 // Carry over authentication information from the previous handshake into
398 // a fresh session.
399 hs->new_session =
400 SSL_SESSION_dup(session.get(), SSL_SESSION_DUP_AUTH_ONLY);
401 if (hs->new_session == nullptr) {
402 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
403 return ssl_hs_error;
404 }
405
406 ssl->s3->session_reused = true;
407 hs->can_release_private_key = true;
408 ssl->verify_result = hs->new_session->verify_result;
409
410 // Resumption incorporates fresh key material, so refresh the timeout.
411 ssl_session_renew_timeout(ssl, hs->new_session.get(),
412 ssl->session_ctx->session_psk_dhe_timeout);
413 break;
414
415 case ssl_ticket_aead_error:
416 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
417 return ssl_hs_error;
418
419 case ssl_ticket_aead_retry:
420 hs->tls13_state = state13_select_session;
421 return ssl_hs_pending_ticket;
422 }
423
424 // Negotiate ALPS now, after ALPN is negotiated and |hs->new_session| is
425 // initialized.
426 if (!ssl_negotiate_alps(hs, &alert, &client_hello)) {
427 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
428 return ssl_hs_error;
429 }
430
431 // Record connection properties in the new session.
432 hs->new_session->cipher = hs->new_cipher;
433 if (!tls1_get_shared_group(hs, &hs->new_session->group_id)) {
434 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
435 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
436 return ssl_hs_error;
437 }
438
439 // Determine if we need HelloRetryRequest.
440 bool found_key_share;
441 if (!ssl_ext_key_share_parse_clienthello(hs, &found_key_share,
442 /*out_key_share=*/nullptr, &alert,
443 &client_hello)) {
444 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
445 return ssl_hs_error;
446 }
447
448 // Determine if we're negotiating 0-RTT.
449 if (!ssl->enable_early_data) {
450 ssl->s3->early_data_reason = ssl_early_data_disabled;
451 } else if (!offered_ticket) {
452 ssl->s3->early_data_reason = ssl_early_data_no_session_offered;
453 } else if (!session) {
454 ssl->s3->early_data_reason = ssl_early_data_session_not_resumed;
455 } else if (session->ticket_max_early_data == 0) {
456 ssl->s3->early_data_reason = ssl_early_data_unsupported_for_session;
457 } else if (!hs->early_data_offered) {
458 ssl->s3->early_data_reason = ssl_early_data_peer_declined;
459 } else if (hs->channel_id_negotiated) {
460 // Channel ID is incompatible with 0-RTT.
461 ssl->s3->early_data_reason = ssl_early_data_channel_id;
462 } else if (MakeConstSpan(ssl->s3->alpn_selected) != session->early_alpn) {
463 // The negotiated ALPN must match the one in the ticket.
464 ssl->s3->early_data_reason = ssl_early_data_alpn_mismatch;
465 } else if (hs->new_session->has_application_settings !=
466 session->has_application_settings ||
467 MakeConstSpan(hs->new_session->local_application_settings) !=
468 session->local_application_settings) {
469 ssl->s3->early_data_reason = ssl_early_data_alps_mismatch;
470 } else if (ssl->s3->ticket_age_skew < -kMaxTicketAgeSkewSeconds ||
471 kMaxTicketAgeSkewSeconds < ssl->s3->ticket_age_skew) {
472 ssl->s3->early_data_reason = ssl_early_data_ticket_age_skew;
473 } else if (!quic_ticket_compatible(session.get(), hs->config)) {
474 ssl->s3->early_data_reason = ssl_early_data_quic_parameter_mismatch;
475 } else if (!found_key_share) {
476 ssl->s3->early_data_reason = ssl_early_data_hello_retry_request;
477 } else if (hs->custom_extensions.received) {
478 ssl->s3->early_data_reason = ssl_early_data_unsupported_with_custom_extension;
479 } else {
480 // |ssl_session_is_resumable| forbids cross-cipher resumptions even if the
481 // PRF hashes match.
482 assert(hs->new_cipher == session->cipher);
483
484 ssl->s3->early_data_reason = ssl_early_data_accepted;
485 ssl->s3->early_data_accepted = true;
486 }
487
488 // Store the ALPN and ALPS values in the session for 0-RTT. Note the peer
489 // applications settings are not generally known until client
490 // EncryptedExtensions.
491 if (!hs->new_session->early_alpn.CopyFrom(ssl->s3->alpn_selected)) {
492 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
493 return ssl_hs_error;
494 }
495
496 // The peer applications settings are usually received later, in
497 // EncryptedExtensions. But, in 0-RTT handshakes, we carry over the
498 // values from |session|. Do this now, before |session| is discarded.
499 if (ssl->s3->early_data_accepted &&
500 hs->new_session->has_application_settings &&
501 !hs->new_session->peer_application_settings.CopyFrom(
502 session->peer_application_settings)) {
503 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
504 return ssl_hs_error;
505 }
506
507 // Copy the QUIC early data context to the session.
508 if (ssl->enable_early_data && ssl->quic_method) {
509 if (!hs->new_session->quic_early_data_context.CopyFrom(
510 hs->config->quic_early_data_context)) {
511 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
512 return ssl_hs_error;
513 }
514 }
515
516 if (ssl->ctx->dos_protection_cb != NULL &&
517 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
518 // Connection rejected for DOS reasons.
519 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
520 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
521 return ssl_hs_error;
522 }
523
524 size_t hash_len = EVP_MD_size(
525 ssl_get_handshake_digest(ssl_protocol_version(ssl), hs->new_cipher));
526
527 // Set up the key schedule and incorporate the PSK into the running secret.
528 if (!tls13_init_key_schedule(
529 hs, ssl->s3->session_reused
530 ? MakeConstSpan(hs->new_session->secret,
531 hs->new_session->secret_length)
532 : MakeConstSpan(kZeroes, hash_len)) ||
533 !ssl_hash_message(hs, msg)) {
534 return ssl_hs_error;
535 }
536
537 if (ssl->s3->early_data_accepted) {
538 if (!tls13_derive_early_secret(hs)) {
539 return ssl_hs_error;
540 }
541 } else if (hs->early_data_offered) {
542 ssl->s3->skip_early_data = true;
543 }
544
545 if (!found_key_share) {
546 ssl->method->next_message(ssl);
547 if (!hs->transcript.UpdateForHelloRetryRequest()) {
548 return ssl_hs_error;
549 }
550 hs->tls13_state = state13_send_hello_retry_request;
551 return ssl_hs_ok;
552 }
553
554 if (!resolve_ecdhe_secret(hs, &client_hello)) {
555 return ssl_hs_error;
556 }
557
558 ssl->method->next_message(ssl);
559 hs->ech_client_hello_buf.Reset();
560 hs->tls13_state = state13_send_server_hello;
561 return ssl_hs_ok;
562 }
563
564 static enum ssl_hs_wait_t do_send_hello_retry_request(SSL_HANDSHAKE *hs) {
565 SSL *const ssl = hs->ssl;
566 if (hs->hints_requested) {
567 return ssl_hs_hints_ready;
568 }
569
570 ScopedCBB cbb;
571 CBB body, session_id, extensions;
572 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
573 !CBB_add_u16(&body, TLS1_2_VERSION) ||
574 !CBB_add_bytes(&body, kHelloRetryRequest, SSL3_RANDOM_SIZE) ||
575 !CBB_add_u8_length_prefixed(&body, &session_id) ||
576 !CBB_add_bytes(&session_id, hs->session_id, hs->session_id_len) ||
577 !CBB_add_u16(&body, SSL_CIPHER_get_protocol_id(hs->new_cipher)) ||
578 !CBB_add_u8(&body, 0 /* no compression */) ||
579 !CBB_add_u16_length_prefixed(&body, &extensions) ||
580 !CBB_add_u16(&extensions, TLSEXT_TYPE_supported_versions) ||
581 !CBB_add_u16(&extensions, 2 /* length */) ||
582 !CBB_add_u16(&extensions, ssl->version) ||
583 !CBB_add_u16(&extensions, TLSEXT_TYPE_key_share) ||
584 !CBB_add_u16(&extensions, 2 /* length */) ||
585 !CBB_add_u16(&extensions, hs->new_session->group_id)) {
586 return ssl_hs_error;
587 }
588 if (hs->ech_is_inner) {
589 // Fill a placeholder for the ECH confirmation value.
590 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_encrypted_client_hello) ||
591 !CBB_add_u16(&extensions, ECH_CONFIRMATION_SIGNAL_LEN) ||
592 !CBB_add_zeros(&extensions, ECH_CONFIRMATION_SIGNAL_LEN)) {
593 return ssl_hs_error;
594 }
595 }
596 Array<uint8_t> hrr;
597 if (!ssl->method->finish_message(ssl, cbb.get(), &hrr)) {
598 return ssl_hs_error;
599 }
600 if (hs->ech_is_inner) {
601 // Now that the message is encoded, fill in the whole value.
602 size_t offset = hrr.size() - ECH_CONFIRMATION_SIGNAL_LEN;
603 if (!ssl_ech_accept_confirmation(
604 hs, MakeSpan(hrr).last(ECH_CONFIRMATION_SIGNAL_LEN),
605 ssl->s3->client_random, hs->transcript, /*is_hrr=*/true, hrr,
606 offset)) {
607 return ssl_hs_error;
608 }
609 }
610
611 if (!ssl->method->add_message(ssl, std::move(hrr)) ||
612 !ssl->method->add_change_cipher_spec(ssl)) {
613 return ssl_hs_error;
614 }
615
616 ssl->s3->used_hello_retry_request = true;
617 hs->tls13_state = state13_read_second_client_hello;
618 return ssl_hs_flush;
619 }
620
621 static enum ssl_hs_wait_t do_read_second_client_hello(SSL_HANDSHAKE *hs) {
622 SSL *const ssl = hs->ssl;
623 SSLMessage msg;
624 if (!ssl->method->get_message(ssl, &msg)) {
625 return ssl_hs_read_message;
626 }
627 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) {
628 return ssl_hs_error;
629 }
630 SSL_CLIENT_HELLO client_hello;
631 if (!ssl_client_hello_init(ssl, &client_hello, msg.body)) {
632 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
633 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
634 return ssl_hs_error;
635 }
636
637 if (ssl->s3->ech_status == ssl_ech_accepted) {
638 // If we previously accepted the ClientHelloInner, the second ClientHello
639 // must contain an outer encrypted_client_hello extension.
640 CBS ech_body;
641 if (!ssl_client_hello_get_extension(&client_hello, &ech_body,
642 TLSEXT_TYPE_encrypted_client_hello)) {
643 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
644 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION);
645 return ssl_hs_error;
646 }
647 uint16_t kdf_id, aead_id;
648 uint8_t type, config_id;
649 CBS enc, payload;
650 if (!CBS_get_u8(&ech_body, &type) || //
651 type != ECH_CLIENT_OUTER || //
652 !CBS_get_u16(&ech_body, &kdf_id) || //
653 !CBS_get_u16(&ech_body, &aead_id) ||
654 !CBS_get_u8(&ech_body, &config_id) ||
655 !CBS_get_u16_length_prefixed(&ech_body, &enc) ||
656 !CBS_get_u16_length_prefixed(&ech_body, &payload) ||
657 CBS_len(&ech_body) != 0) {
658 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
659 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
660 return ssl_hs_error;
661 }
662
663 if (kdf_id != EVP_HPKE_KDF_id(EVP_HPKE_CTX_kdf(hs->ech_hpke_ctx.get())) ||
664 aead_id !=
665 EVP_HPKE_AEAD_id(EVP_HPKE_CTX_aead(hs->ech_hpke_ctx.get())) ||
666 config_id != hs->ech_config_id || CBS_len(&enc) > 0) {
667 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
668 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
669 return ssl_hs_error;
670 }
671
672 // Decrypt the payload with the HPKE context from the first ClientHello.
673 uint8_t alert = SSL_AD_DECODE_ERROR;
674 bool unused;
675 if (!ssl_client_hello_decrypt(hs, &alert, &unused,
676 &hs->ech_client_hello_buf, &client_hello,
677 payload)) {
678 // Decryption failure is fatal in the second ClientHello.
679 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
680 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
681 return ssl_hs_error;
682 }
683
684 // Reparse |client_hello| from the buffer owned by |hs|.
685 if (!hs->GetClientHello(&msg, &client_hello)) {
686 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
687 return ssl_hs_error;
688 }
689 }
690
691 // We perform all our negotiation based on the first ClientHello (for
692 // consistency with what |select_certificate_cb| observed), which is in the
693 // transcript, so we can ignore most of this second one.
694 //
695 // We do, however, check the second PSK binder. This covers the client key
696 // share, in case we ever send half-RTT data (we currently do not). It is also
697 // a tricky computation, so we enforce the peer handled it correctly.
698 if (ssl->s3->session_reused) {
699 CBS pre_shared_key;
700 if (!ssl_client_hello_get_extension(&client_hello, &pre_shared_key,
701 TLSEXT_TYPE_pre_shared_key)) {
702 OPENSSL_PUT_ERROR(SSL, SSL_R_INCONSISTENT_CLIENT_HELLO);
703 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
704 return ssl_hs_error;
705 }
706
707 CBS ticket, binders;
708 uint32_t client_ticket_age;
709 uint8_t alert = SSL_AD_DECODE_ERROR;
710 if (!ssl_ext_pre_shared_key_parse_clienthello(
711 hs, &ticket, &binders, &client_ticket_age, &alert, &client_hello,
712 &pre_shared_key)) {
713 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
714 return ssl_hs_error;
715 }
716
717 // Note it is important that we do not obtain a new |SSL_SESSION| from
718 // |ticket|. We have already selected parameters based on the first
719 // ClientHello (in the transcript) and must not switch partway through.
720 if (!tls13_verify_psk_binder(hs, hs->new_session.get(), msg, &binders)) {
721 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
722 return ssl_hs_error;
723 }
724 }
725
726 if (!resolve_ecdhe_secret(hs, &client_hello)) {
727 return ssl_hs_error;
728 }
729
730 if (!ssl_hash_message(hs, msg)) {
731 return ssl_hs_error;
732 }
733
734 // ClientHello should be the end of the flight.
735 if (ssl->method->has_unprocessed_handshake_data(ssl)) {
736 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
737 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA);
738 return ssl_hs_error;
739 }
740
741 ssl->method->next_message(ssl);
742 hs->ech_client_hello_buf.Reset();
743 hs->tls13_state = state13_send_server_hello;
744 return ssl_hs_ok;
745 }
746
747 static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
748 SSL *const ssl = hs->ssl;
749
750 Span<uint8_t> random(ssl->s3->server_random);
751
752 SSL_HANDSHAKE_HINTS *const hints = hs->hints.get();
753 if (hints && !hs->hints_requested &&
754 hints->server_random_tls13.size() == random.size()) {
755 OPENSSL_memcpy(random.data(), hints->server_random_tls13.data(),
756 random.size());
757 } else {
758 RAND_bytes(random.data(), random.size());
759 if (hints && hs->hints_requested &&
760 !hints->server_random_tls13.CopyFrom(random)) {
761 return ssl_hs_error;
762 }
763 }
764
765 Array<uint8_t> server_hello;
766 ScopedCBB cbb;
767 CBB body, extensions, session_id;
768 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
769 !CBB_add_u16(&body, TLS1_2_VERSION) ||
770 !CBB_add_bytes(&body, ssl->s3->server_random,
771 sizeof(ssl->s3->server_random)) ||
772 !CBB_add_u8_length_prefixed(&body, &session_id) ||
773 !CBB_add_bytes(&session_id, hs->session_id, hs->session_id_len) ||
774 !CBB_add_u16(&body, SSL_CIPHER_get_protocol_id(hs->new_cipher)) ||
775 !CBB_add_u8(&body, 0) ||
776 !CBB_add_u16_length_prefixed(&body, &extensions) ||
777 !ssl_ext_pre_shared_key_add_serverhello(hs, &extensions) ||
778 !ssl_ext_key_share_add_serverhello(hs, &extensions) ||
779 !ssl_ext_supported_versions_add_serverhello(hs, &extensions) ||
780 !ssl->method->finish_message(ssl, cbb.get(), &server_hello)) {
781 return ssl_hs_error;
782 }
783
784 assert(ssl->s3->ech_status != ssl_ech_accepted || hs->ech_is_inner);
785 if (hs->ech_is_inner) {
786 // Fill in the ECH confirmation signal.
787 const size_t offset = ssl_ech_confirmation_signal_hello_offset(ssl);
788 Span<uint8_t> random_suffix = random.last(ECH_CONFIRMATION_SIGNAL_LEN);
789 if (!ssl_ech_accept_confirmation(hs, random_suffix, ssl->s3->client_random,
790 hs->transcript,
791 /*is_hrr=*/false, server_hello, offset)) {
792 return ssl_hs_error;
793 }
794
795 // Update |server_hello|.
796 Span<uint8_t> server_hello_out =
797 MakeSpan(server_hello).subspan(offset, ECH_CONFIRMATION_SIGNAL_LEN);
798 OPENSSL_memcpy(server_hello_out.data(), random_suffix.data(),
799 ECH_CONFIRMATION_SIGNAL_LEN);
800 }
801
802 if (!ssl->method->add_message(ssl, std::move(server_hello))) {
803 return ssl_hs_error;
804 }
805
806 hs->ecdh_public_key.Reset(); // No longer needed.
807 if (!ssl->s3->used_hello_retry_request &&
808 !ssl->method->add_change_cipher_spec(ssl)) {
809 return ssl_hs_error;
810 }
811
812 // Derive and enable the handshake traffic secrets.
813 if (!tls13_derive_handshake_secrets(hs) ||
814 !tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_seal,
815 hs->new_session.get(),
816 hs->server_handshake_secret())) {
817 return ssl_hs_error;
818 }
819
820 // Send EncryptedExtensions.
821 if (!ssl->method->init_message(ssl, cbb.get(), &body,
822 SSL3_MT_ENCRYPTED_EXTENSIONS) ||
823 !ssl_add_serverhello_tlsext(hs, &body) ||
824 !ssl_add_message_cbb(ssl, cbb.get())) {
825 return ssl_hs_error;
826 }
827
828 if (!ssl->s3->session_reused) {
829 // Determine whether to request a client certificate.
830 hs->cert_request = !!(hs->config->verify_mode & SSL_VERIFY_PEER);
831 // Only request a certificate if Channel ID isn't negotiated.
832 if ((hs->config->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
833 hs->channel_id_negotiated) {
834 hs->cert_request = false;
835 }
836 }
837
838 // Send a CertificateRequest, if necessary.
839 if (hs->cert_request) {
840 CBB cert_request_extensions, sigalg_contents, sigalgs_cbb;
841 if (!ssl->method->init_message(ssl, cbb.get(), &body,
842 SSL3_MT_CERTIFICATE_REQUEST) ||
843 !CBB_add_u8(&body, 0 /* no certificate_request_context. */) ||
844 !CBB_add_u16_length_prefixed(&body, &cert_request_extensions) ||
845 !CBB_add_u16(&cert_request_extensions,
846 TLSEXT_TYPE_signature_algorithms) ||
847 !CBB_add_u16_length_prefixed(&cert_request_extensions,
848 &sigalg_contents) ||
849 !CBB_add_u16_length_prefixed(&sigalg_contents, &sigalgs_cbb) ||
850 !tls12_add_verify_sigalgs(hs, &sigalgs_cbb)) {
851 return ssl_hs_error;
852 }
853
854 if (ssl_has_client_CAs(hs->config)) {
855 CBB ca_contents;
856 if (!CBB_add_u16(&cert_request_extensions,
857 TLSEXT_TYPE_certificate_authorities) ||
858 !CBB_add_u16_length_prefixed(&cert_request_extensions,
859 &ca_contents) ||
860 !ssl_add_client_CA_list(hs, &ca_contents) ||
861 !CBB_flush(&cert_request_extensions)) {
862 return ssl_hs_error;
863 }
864 }
865
866 if (!ssl_add_message_cbb(ssl, cbb.get())) {
867 return ssl_hs_error;
868 }
869 }
870
871 // Send the server Certificate message, if necessary.
872 if (!ssl->s3->session_reused) {
873 if (!ssl_has_certificate(hs)) {
874 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
875 return ssl_hs_error;
876 }
877
878 if (!tls13_add_certificate(hs)) {
879 return ssl_hs_error;
880 }
881
882 hs->tls13_state = state13_send_server_certificate_verify;
883 return ssl_hs_ok;
884 }
885
886 hs->tls13_state = state13_send_server_finished;
887 return ssl_hs_ok;
888 }
889
890 static enum ssl_hs_wait_t do_send_server_certificate_verify(SSL_HANDSHAKE *hs) {
891 switch (tls13_add_certificate_verify(hs)) {
892 case ssl_private_key_success:
893 hs->tls13_state = state13_send_server_finished;
894 return ssl_hs_ok;
895
896 case ssl_private_key_retry:
897 hs->tls13_state = state13_send_server_certificate_verify;
898 return ssl_hs_private_key_operation;
899
900 case ssl_private_key_failure:
901 return ssl_hs_error;
902 }
903
904 assert(0);
905 return ssl_hs_error;
906 }
907
908 static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
909 SSL *const ssl = hs->ssl;
910 if (hs->hints_requested) {
911 return ssl_hs_hints_ready;
912 }
913
914 hs->can_release_private_key = true;
915 if (!tls13_add_finished(hs) ||
916 // Update the secret to the master secret and derive traffic keys.
917 !tls13_advance_key_schedule(
918 hs, MakeConstSpan(kZeroes, hs->transcript.DigestLen())) ||
919 !tls13_derive_application_secrets(hs) ||
920 !tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_seal,
921 hs->new_session.get(),
922 hs->server_traffic_secret_0())) {
923 return ssl_hs_error;
924 }
925
926 hs->tls13_state = state13_send_half_rtt_ticket;
927 return hs->handback ? ssl_hs_handback : ssl_hs_ok;
928 }
929
930 static enum ssl_hs_wait_t do_send_half_rtt_ticket(SSL_HANDSHAKE *hs) {
931 SSL *const ssl = hs->ssl;
932
933 if (ssl->s3->early_data_accepted) {
934 // If accepting 0-RTT, we send tickets half-RTT. This gets the tickets on
935 // the wire sooner and also avoids triggering a write on |SSL_read| when
936 // processing the client Finished. This requires computing the client
937 // Finished early. See RFC 8446, section 4.6.1.
938 static const uint8_t kEndOfEarlyData[4] = {SSL3_MT_END_OF_EARLY_DATA, 0,
939 0, 0};
940 if (ssl->quic_method == nullptr &&
941 !hs->transcript.Update(kEndOfEarlyData)) {
942 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
943 return ssl_hs_error;
944 }
945
946 size_t finished_len;
947 if (!tls13_finished_mac(hs, hs->expected_client_finished().data(),
948 &finished_len, false /* client */)) {
949 return ssl_hs_error;
950 }
951
952 if (finished_len != hs->expected_client_finished().size()) {
953 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
954 return ssl_hs_error;
955 }
956
957 // Feed the predicted Finished into the transcript. This allows us to derive
958 // the resumption secret early and send half-RTT tickets.
959 //
960 // TODO(davidben): This will need to be updated for DTLS 1.3.
961 assert(!SSL_is_dtls(hs->ssl));
962 assert(hs->expected_client_finished().size() <= 0xff);
963 uint8_t header[4] = {
964 SSL3_MT_FINISHED, 0, 0,
965 static_cast<uint8_t>(hs->expected_client_finished().size())};
966 bool unused_sent_tickets;
967 if (!hs->transcript.Update(header) ||
968 !hs->transcript.Update(hs->expected_client_finished()) ||
969 !tls13_derive_resumption_secret(hs) ||
970 !add_new_session_tickets(hs, &unused_sent_tickets)) {
971 return ssl_hs_error;
972 }
973 }
974
975 hs->tls13_state = state13_read_second_client_flight;
976 return ssl_hs_flush;
977 }
978
979 static enum ssl_hs_wait_t do_read_second_client_flight(SSL_HANDSHAKE *hs) {
980 SSL *const ssl = hs->ssl;
981 if (ssl->s3->early_data_accepted) {
982 if (!tls13_set_traffic_key(ssl, ssl_encryption_early_data, evp_aead_open,
983 hs->new_session.get(),
984 hs->early_traffic_secret())) {
985 return ssl_hs_error;
986 }
987 hs->can_early_write = true;
988 hs->can_early_read = true;
989 hs->in_early_data = true;
990 }
991
992 // QUIC doesn't use an EndOfEarlyData message (RFC 9001, section 8.3), so we
993 // switch to client_handshake_secret before the early return.
994 if (ssl->quic_method != nullptr) {
995 if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_open,
996 hs->new_session.get(),
997 hs->client_handshake_secret())) {
998 return ssl_hs_error;
999 }
1000 hs->tls13_state = state13_process_end_of_early_data;
1001 return ssl->s3->early_data_accepted ? ssl_hs_early_return : ssl_hs_ok;
1002 }
1003
1004 hs->tls13_state = state13_process_end_of_early_data;
1005 return ssl->s3->early_data_accepted ? ssl_hs_read_end_of_early_data
1006 : ssl_hs_ok;
1007 }
1008
1009 static enum ssl_hs_wait_t do_process_end_of_early_data(SSL_HANDSHAKE *hs) {
1010 SSL *const ssl = hs->ssl;
1011 // In protocols that use EndOfEarlyData, we must consume the extra message and
1012 // switch to client_handshake_secret after the early return.
1013 if (ssl->quic_method == nullptr) {
1014 // If early data was not accepted, the EndOfEarlyData will be in the
1015 // discarded early data.
1016 if (hs->ssl->s3->early_data_accepted) {
1017 SSLMessage msg;
1018 if (!ssl->method->get_message(ssl, &msg)) {
1019 return ssl_hs_read_message;
1020 }
1021 if (!ssl_check_message_type(ssl, msg, SSL3_MT_END_OF_EARLY_DATA)) {
1022 return ssl_hs_error;
1023 }
1024 if (CBS_len(&msg.body) != 0) {
1025 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1026 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1027 return ssl_hs_error;
1028 }
1029 ssl->method->next_message(ssl);
1030 }
1031 if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_open,
1032 hs->new_session.get(),
1033 hs->client_handshake_secret())) {
1034 return ssl_hs_error;
1035 }
1036 }
1037 hs->tls13_state = state13_read_client_encrypted_extensions;
1038 return ssl_hs_ok;
1039 }
1040
1041 static enum ssl_hs_wait_t do_read_client_encrypted_extensions(
1042 SSL_HANDSHAKE *hs) {
1043 SSL *const ssl = hs->ssl;
1044 // For now, only one extension uses client EncryptedExtensions. This function
1045 // may be generalized if others use it in the future.
1046 if (hs->new_session->has_application_settings &&
1047 !ssl->s3->early_data_accepted) {
1048 SSLMessage msg;
1049 if (!ssl->method->get_message(ssl, &msg)) {
1050 return ssl_hs_read_message;
1051 }
1052 if (!ssl_check_message_type(ssl, msg, SSL3_MT_ENCRYPTED_EXTENSIONS)) {
1053 return ssl_hs_error;
1054 }
1055
1056 CBS body = msg.body, extensions;
1057 if (!CBS_get_u16_length_prefixed(&body, &extensions) ||
1058 CBS_len(&body) != 0) {
1059 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1060 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1061 return ssl_hs_error;
1062 }
1063
1064 uint16_t extension_type = TLSEXT_TYPE_application_settings_old;
1065 if (hs->config->alps_use_new_codepoint) {
1066 extension_type = TLSEXT_TYPE_application_settings;
1067 }
1068 SSLExtension application_settings(extension_type);
1069 uint8_t alert = SSL_AD_DECODE_ERROR;
1070 if (!ssl_parse_extensions(&extensions, &alert, {&application_settings},
1071 /*ignore_unknown=*/false)) {
1072 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
1073 return ssl_hs_error;
1074 }
1075
1076 if (!application_settings.present) {
1077 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
1078 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION);
1079 return ssl_hs_error;
1080 }
1081
1082 // Note that, if 0-RTT was accepted, these values will already have been
1083 // initialized earlier.
1084 if (!hs->new_session->peer_application_settings.CopyFrom(
1085 application_settings.data) ||
1086 !ssl_hash_message(hs, msg)) {
1087 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1088 return ssl_hs_error;
1089 }
1090
1091 ssl->method->next_message(ssl);
1092 }
1093
1094 hs->tls13_state = state13_read_client_certificate;
1095 return ssl_hs_ok;
1096 }
1097
1098 static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
1099 SSL *const ssl = hs->ssl;
1100 if (!hs->cert_request) {
1101 if (!ssl->s3->session_reused) {
1102 // OpenSSL returns X509_V_OK when no certificates are requested. This is
1103 // classed by them as a bug, but it's assumed by at least NGINX. (Only do
1104 // this in full handshakes as resumptions should carry over the previous
1105 // |verify_result|, though this is a no-op because servers do not
1106 // implement the client's odd soft-fail mode.)
1107 hs->new_session->verify_result = X509_V_OK;
1108 ssl->verify_result = hs->new_session->verify_result;
1109 }
1110
1111 // Skip this state.
1112 hs->tls13_state = state13_read_channel_id;
1113 return ssl_hs_ok;
1114 }
1115
1116 const bool allow_anonymous =
1117 (hs->config->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) == 0;
1118 SSLMessage msg;
1119 if (!ssl->method->get_message(ssl, &msg)) {
1120 return ssl_hs_read_message;
1121 }
1122 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
1123 !tls13_process_certificate(hs, msg, allow_anonymous) ||
1124 !ssl_hash_message(hs, msg)) {
1125 return ssl_hs_error;
1126 }
1127
1128 ssl->method->next_message(ssl);
1129 hs->tls13_state = state13_read_client_certificate_verify;
1130 return ssl_hs_ok;
1131 }
1132
1133 static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) {
1134 SSL *const ssl = hs->ssl;
1135 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) {
1136 // Skip this state.
1137 hs->tls13_state = state13_read_channel_id;
1138 return ssl_hs_ok;
1139 }
1140
1141 SSLMessage msg;
1142 if (!ssl->method->get_message(ssl, &msg)) {
1143 return ssl_hs_read_message;
1144 }
1145
1146 switch (ssl_verify_peer_cert(hs)) {
1147 case ssl_verify_ok:
1148 break;
1149 case ssl_verify_invalid:
1150 return ssl_hs_error;
1151 case ssl_verify_retry:
1152 hs->tls13_state = state13_read_client_certificate_verify;
1153 return ssl_hs_certificate_verify;
1154 }
1155
1156 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY) ||
1157 !tls13_process_certificate_verify(hs, msg) ||
1158 !ssl_hash_message(hs, msg)) {
1159 return ssl_hs_error;
1160 }
1161
1162 ssl->method->next_message(ssl);
1163 hs->tls13_state = state13_read_channel_id;
1164 return ssl_hs_ok;
1165 }
1166
1167 static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
1168 SSL *const ssl = hs->ssl;
1169 if (!hs->channel_id_negotiated) {
1170 hs->tls13_state = state13_read_client_finished;
1171 return ssl_hs_ok;
1172 }
1173
1174 SSLMessage msg;
1175 if (!ssl->method->get_message(ssl, &msg)) {
1176 return ssl_hs_read_message;
1177 }
1178 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
1179 !tls1_verify_channel_id(hs, msg) ||
1180 !ssl_hash_message(hs, msg)) {
1181 return ssl_hs_error;
1182 }
1183
1184 ssl->method->next_message(ssl);
1185 hs->tls13_state = state13_read_client_finished;
1186 return ssl_hs_ok;
1187 }
1188
1189 static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
1190 SSL *const ssl = hs->ssl;
1191 SSLMessage msg;
1192 if (!ssl->method->get_message(ssl, &msg)) {
1193 return ssl_hs_read_message;
1194 }
1195 if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED) ||
1196 // If early data was accepted, we've already computed the client Finished
1197 // and derived the resumption secret.
1198 !tls13_process_finished(hs, msg, ssl->s3->early_data_accepted) ||
1199 // evp_aead_seal keys have already been switched.
1200 !tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_open,
1201 hs->new_session.get(),
1202 hs->client_traffic_secret_0())) {
1203 return ssl_hs_error;
1204 }
1205
1206 if (!ssl->s3->early_data_accepted) {
1207 if (!ssl_hash_message(hs, msg) ||
1208 !tls13_derive_resumption_secret(hs)) {
1209 return ssl_hs_error;
1210 }
1211
1212 // We send post-handshake tickets as part of the handshake in 1-RTT.
1213 hs->tls13_state = state13_send_new_session_ticket;
1214 } else {
1215 // We already sent half-RTT tickets.
1216 hs->tls13_state = state13_done;
1217 }
1218
1219 ssl->method->next_message(ssl);
1220 return ssl_hs_ok;
1221 }
1222
1223 static enum ssl_hs_wait_t do_send_new_session_ticket(SSL_HANDSHAKE *hs) {
1224 bool sent_tickets;
1225 if (!add_new_session_tickets(hs, &sent_tickets)) {
1226 return ssl_hs_error;
1227 }
1228
1229 hs->tls13_state = state13_done;
1230 // In TLS 1.3, the NewSessionTicket isn't flushed until the server performs a
1231 // write, to prevent a non-reading client from causing the server to hang in
1232 // the case of a small server write buffer. Consumers which don't write data
1233 // to the client will need to do a zero-byte write if they wish to flush the
1234 // tickets.
1235 if (hs->ssl->quic_method != nullptr && sent_tickets) {
1236 return ssl_hs_flush;
1237 }
1238 return ssl_hs_ok;
1239 }
1240
1241 enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs) {
1242 while (hs->tls13_state != state13_done) {
1243 enum ssl_hs_wait_t ret = ssl_hs_error;
1244 enum tls13_server_hs_state_t state =
1245 static_cast<enum tls13_server_hs_state_t>(hs->tls13_state);
1246 switch (state) {
1247 case state13_select_parameters:
1248 ret = do_select_parameters(hs);
1249 break;
1250 case state13_select_session:
1251 ret = do_select_session(hs);
1252 break;
1253 case state13_send_hello_retry_request:
1254 ret = do_send_hello_retry_request(hs);
1255 break;
1256 case state13_read_second_client_hello:
1257 ret = do_read_second_client_hello(hs);
1258 break;
1259 case state13_send_server_hello:
1260 ret = do_send_server_hello(hs);
1261 break;
1262 case state13_send_server_certificate_verify:
1263 ret = do_send_server_certificate_verify(hs);
1264 break;
1265 case state13_send_server_finished:
1266 ret = do_send_server_finished(hs);
1267 break;
1268 case state13_send_half_rtt_ticket:
1269 ret = do_send_half_rtt_ticket(hs);
1270 break;
1271 case state13_read_second_client_flight:
1272 ret = do_read_second_client_flight(hs);
1273 break;
1274 case state13_process_end_of_early_data:
1275 ret = do_process_end_of_early_data(hs);
1276 break;
1277 case state13_read_client_encrypted_extensions:
1278 ret = do_read_client_encrypted_extensions(hs);
1279 break;
1280 case state13_read_client_certificate:
1281 ret = do_read_client_certificate(hs);
1282 break;
1283 case state13_read_client_certificate_verify:
1284 ret = do_read_client_certificate_verify(hs);
1285 break;
1286 case state13_read_channel_id:
1287 ret = do_read_channel_id(hs);
1288 break;
1289 case state13_read_client_finished:
1290 ret = do_read_client_finished(hs);
1291 break;
1292 case state13_send_new_session_ticket:
1293 ret = do_send_new_session_ticket(hs);
1294 break;
1295 case state13_done:
1296 ret = ssl_hs_ok;
1297 break;
1298 }
1299
1300 if (hs->tls13_state != state) {
1301 ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
1302 }
1303
1304 if (ret != ssl_hs_ok) {
1305 return ret;
1306 }
1307 }
1308
1309 return ssl_hs_ok;
1310 }
1311
1312 const char *tls13_server_handshake_state(SSL_HANDSHAKE *hs) {
1313 enum tls13_server_hs_state_t state =
1314 static_cast<enum tls13_server_hs_state_t>(hs->tls13_state);
1315 switch (state) {
1316 case state13_select_parameters:
1317 return "TLS 1.3 server select_parameters";
1318 case state13_select_session:
1319 return "TLS 1.3 server select_session";
1320 case state13_send_hello_retry_request:
1321 return "TLS 1.3 server send_hello_retry_request";
1322 case state13_read_second_client_hello:
1323 return "TLS 1.3 server read_second_client_hello";
1324 case state13_send_server_hello:
1325 return "TLS 1.3 server send_server_hello";
1326 case state13_send_server_certificate_verify:
1327 return "TLS 1.3 server send_server_certificate_verify";
1328 case state13_send_half_rtt_ticket:
1329 return "TLS 1.3 server send_half_rtt_ticket";
1330 case state13_send_server_finished:
1331 return "TLS 1.3 server send_server_finished";
1332 case state13_read_second_client_flight:
1333 return "TLS 1.3 server read_second_client_flight";
1334 case state13_process_end_of_early_data:
1335 return "TLS 1.3 server process_end_of_early_data";
1336 case state13_read_client_encrypted_extensions:
1337 return "TLS 1.3 server read_client_encrypted_extensions";
1338 case state13_read_client_certificate:
1339 return "TLS 1.3 server read_client_certificate";
1340 case state13_read_client_certificate_verify:
1341 return "TLS 1.3 server read_client_certificate_verify";
1342 case state13_read_channel_id:
1343 return "TLS 1.3 server read_channel_id";
1344 case state13_read_client_finished:
1345 return "TLS 1.3 server read_client_finished";
1346 case state13_send_new_session_ticket:
1347 return "TLS 1.3 server send_new_session_ticket";
1348 case state13_done:
1349 return "TLS 1.3 server done";
1350 }
1351
1352 return "TLS 1.3 server unknown";
1353 }
1354
1355 BSSL_NAMESPACE_END