blob: 707cf846ba4f3e4114585644f6275fc73fe057ad [file] [log] [blame]
Steven Valdez143e8b32016-07-11 13:19:03 -04001/* Copyright (c) 2016, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include <openssl/ssl.h>
16
17#include <assert.h>
18#include <string.h>
19
Adam Langley4bfab5d2019-01-23 13:52:17 -080020#include <tuple>
21
David Benjaminabbbee12016-10-31 19:20:42 -040022#include <openssl/aead.h>
Steven Valdez143e8b32016-07-11 13:19:03 -040023#include <openssl/bytestring.h>
24#include <openssl/digest.h>
25#include <openssl/err.h>
David Benjamin070a6c32021-05-05 15:39:27 -040026#include <openssl/hpke.h>
Steven Valdez143e8b32016-07-11 13:19:03 -040027#include <openssl/mem.h>
28#include <openssl/rand.h>
29#include <openssl/stack.h>
30
David Benjamin17cf2cb2016-12-13 01:07:13 -050031#include "../crypto/internal.h"
Steven Valdez143e8b32016-07-11 13:19:03 -040032#include "internal.h"
33
34
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070035BSSL_NAMESPACE_BEGIN
David Benjamin86e95b82017-07-18 16:34:25 -040036
Steven Valdez5440fe02016-07-18 12:40:30 -040037static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0};
38
David Benjamin6433a912019-05-04 14:17:08 -050039// Allow a minute of ticket age skew in either direction. This covers
40// transmission delays in ClientHello and NewSessionTicket, as well as
41// drift between client and server clock rate since the ticket was issued.
42// See RFC 8446, section 8.3.
43static const int32_t kMaxTicketAgeSkewSeconds = 60;
44
David Benjamin3b8c5ec2021-04-12 17:43:23 -040045static bool resolve_ecdhe_secret(SSL_HANDSHAKE *hs,
46 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin6e4fc332016-11-17 16:43:08 +090047 SSL *const ssl = hs->ssl;
David Benjamin3b8c5ec2021-04-12 17:43:23 -040048 const uint16_t group_id = hs->new_session->group_id;
Steven Valdez5440fe02016-07-18 12:40:30 -040049
David Benjamin74795b32017-08-31 15:13:12 -040050 bool found_key_share;
David Benjamin3b8c5ec2021-04-12 17:43:23 -040051 Span<const uint8_t> peer_key;
David Benjamin7e1f9842016-09-20 19:24:40 -040052 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin3b8c5ec2021-04-12 17:43:23 -040053 if (!ssl_ext_key_share_parse_clienthello(hs, &found_key_share, &peer_key,
54 &alert, client_hello)) {
David Benjamind1e3ce12017-10-06 18:31:15 -040055 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin3b8c5ec2021-04-12 17:43:23 -040056 return false;
Steven Valdez5440fe02016-07-18 12:40:30 -040057 }
58
59 if (!found_key_share) {
David Benjamin3b8c5ec2021-04-12 17:43:23 -040060 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
61 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
62 return false;
Steven Valdez5440fe02016-07-18 12:40:30 -040063 }
64
David Benjamin3b8c5ec2021-04-12 17:43:23 -040065 Array<uint8_t> secret;
David Benjaminb571e772021-03-25 19:42:16 -040066 SSL_HANDSHAKE_HINTS *const hints = hs->hints.get();
67 if (hints && !hs->hints_requested && hints->key_share_group_id == group_id &&
68 !hints->key_share_secret.empty()) {
David Benjamin08b1f382023-02-28 17:22:23 -050069 // Copy the key_share secret from hints.
70 if (!hs->key_share_ciphertext.CopyFrom(hints->key_share_ciphertext) ||
David Benjaminb571e772021-03-25 19:42:16 -040071 !secret.CopyFrom(hints->key_share_secret)) {
72 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
73 return false;
74 }
75 } else {
David Benjamin08b1f382023-02-28 17:22:23 -050076 ScopedCBB ciphertext;
David Benjaminb571e772021-03-25 19:42:16 -040077 UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id);
78 if (!key_share || //
David Benjamin08b1f382023-02-28 17:22:23 -050079 !CBB_init(ciphertext.get(), 32) ||
80 !key_share->Encap(ciphertext.get(), &secret, &alert, peer_key) ||
81 !CBBFinishArray(ciphertext.get(), &hs->key_share_ciphertext)) {
David Benjaminb571e772021-03-25 19:42:16 -040082 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
83 return false;
84 }
85 if (hints && hs->hints_requested) {
86 hints->key_share_group_id = group_id;
David Benjamin08b1f382023-02-28 17:22:23 -050087 if (!hints->key_share_ciphertext.CopyFrom(hs->key_share_ciphertext) ||
David Benjaminb571e772021-03-25 19:42:16 -040088 !hints->key_share_secret.CopyFrom(secret)) {
89 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
90 return false;
91 }
92 }
David Benjamin3b8c5ec2021-04-12 17:43:23 -040093 }
94
95 return tls13_advance_key_schedule(hs, secret);
Steven Valdez5440fe02016-07-18 12:40:30 -040096}
97
Steven Valdez038da9b2017-07-10 12:57:25 -040098static int ssl_ext_supported_versions_add_serverhello(SSL_HANDSHAKE *hs,
99 CBB *out) {
100 CBB contents;
101 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
102 !CBB_add_u16_length_prefixed(out, &contents) ||
103 !CBB_add_u16(&contents, hs->ssl->version) ||
104 !CBB_flush(out)) {
105 return 0;
106 }
107
108 return 1;
109}
110
David Benjamin34202b92016-11-16 19:07:53 +0900111static const SSL_CIPHER *choose_tls13_cipher(
David Benjamin43cc9c62018-12-12 13:06:46 -0600112 const SSL *ssl, const SSL_CLIENT_HELLO *client_hello, uint16_t group_id) {
David Benjamin34202b92016-11-16 19:07:53 +0900113 CBS cipher_suites;
114 CBS_init(&cipher_suites, client_hello->cipher_suites,
115 client_hello->cipher_suites_len);
116
David Benjamind1e3ce12017-10-06 18:31:15 -0400117 const uint16_t version = ssl_protocol_version(ssl);
David Benjamin34202b92016-11-16 19:07:53 +0900118
Bob Beck8cacbd92023-04-05 14:38:38 -0600119 return ssl_choose_tls13_cipher(
120 cipher_suites,
121 ssl->config->aes_hw_override ? ssl->config->aes_hw_override_value
122 : EVP_has_aes_hardware(),
Adam Langley2f6409e2023-04-10 21:09:11 +0000123 version, group_id, ssl->config->tls13_cipher_policy);
David Benjamin34202b92016-11-16 19:07:53 +0900124}
125
David Benjamin0cbb1af2018-07-17 21:26:05 -0400126static bool add_new_session_tickets(SSL_HANDSHAKE *hs, bool *out_sent_tickets) {
David Benjamin794cc592017-03-25 22:24:23 -0500127 SSL *const ssl = hs->ssl;
David Benjamin0cbb1af2018-07-17 21:26:05 -0400128 if (// If the client doesn't accept resumption with PSK_DHE_KE, don't send a
129 // session ticket.
130 !hs->accept_psk_mode ||
131 // We only implement stateless resumption in TLS 1.3, so skip sending
132 // tickets if disabled.
133 (SSL_get_options(ssl) & SSL_OP_NO_TICKET)) {
134 *out_sent_tickets = false;
135 return true;
136 }
137
David Benjaminc11ea9422017-08-29 16:33:21 -0400138 // Rebase the session timestamp so that it is measured from ticket
139 // issuance.
David Benjamin31b0c9b2017-07-20 14:49:15 -0400140 ssl_session_rebase_time(ssl, hs->new_session.get());
David Benjamin794cc592017-03-25 22:24:23 -0500141
David Benjamin3f180b82022-05-09 17:45:18 -0400142 assert(ssl->session_ctx->num_tickets <= kMaxTickets);
143 for (size_t i = 0; i < ssl->session_ctx->num_tickets; i++) {
Steven Valdezcd8470f2017-10-11 12:29:36 -0400144 UniquePtr<SSL_SESSION> session(
145 SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_INCLUDE_NONAUTH));
146 if (!session) {
David Benjamin0cbb1af2018-07-17 21:26:05 -0400147 return false;
David Benjamin794cc592017-03-25 22:24:23 -0500148 }
David Benjamin794cc592017-03-25 22:24:23 -0500149
Steven Valdezcd8470f2017-10-11 12:29:36 -0400150 if (!RAND_bytes((uint8_t *)&session->ticket_age_add, 4)) {
David Benjamin0cbb1af2018-07-17 21:26:05 -0400151 return false;
Steven Valdezcd8470f2017-10-11 12:29:36 -0400152 }
David Benjamina3a71e92018-06-29 13:24:45 -0400153 session->ticket_age_add_valid = true;
Nick Harper85194322020-05-20 16:59:29 -0700154 bool enable_early_data =
155 ssl->enable_early_data &&
156 (!ssl->quic_method || !ssl->config->quic_early_data_context.empty());
157 if (enable_early_data) {
David Benjamind6343572019-08-15 17:29:02 -0400158 // QUIC does not use the max_early_data_size parameter and always sets it
David Benjamina1d3bfb2021-06-01 12:12:44 -0400159 // to a fixed value. See RFC 9001, section 4.6.1.
David Benjamind6343572019-08-15 17:29:02 -0400160 session->ticket_max_early_data =
161 ssl->quic_method != nullptr ? 0xffffffff : kMaxEarlyDataAccepted;
Steven Valdezbe165a22017-10-10 11:45:01 -0400162 }
163
David Benjamin3f180b82022-05-09 17:45:18 -0400164 static_assert(kMaxTickets < 256, "Too many tickets");
165 assert(i < 256);
Steven Valdezcd8470f2017-10-11 12:29:36 -0400166 uint8_t nonce[] = {static_cast<uint8_t>(i)};
167
David Benjamin1386aad2017-07-19 23:57:40 -0400168 ScopedCBB cbb;
Steven Valdezcd8470f2017-10-11 12:29:36 -0400169 CBB body, nonce_cbb, ticket, extensions;
David Benjamin1386aad2017-07-19 23:57:40 -0400170 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin794cc592017-03-25 22:24:23 -0500171 SSL3_MT_NEW_SESSION_TICKET) ||
Steven Valdezcd8470f2017-10-11 12:29:36 -0400172 !CBB_add_u32(&body, session->timeout) ||
173 !CBB_add_u32(&body, session->ticket_age_add) ||
Steven Valdez7e5dd252018-01-22 15:20:31 -0500174 !CBB_add_u8_length_prefixed(&body, &nonce_cbb) ||
175 !CBB_add_bytes(&nonce_cbb, nonce, sizeof(nonce)) ||
David Benjamin794cc592017-03-25 22:24:23 -0500176 !CBB_add_u16_length_prefixed(&body, &ticket) ||
Alessandro Ghedini2cc6f442018-12-11 11:35:17 +0000177 !tls13_derive_session_psk(session.get(), nonce) ||
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700178 !ssl_encrypt_ticket(hs, &ticket, session.get()) ||
David Benjamin794cc592017-03-25 22:24:23 -0500179 !CBB_add_u16_length_prefixed(&body, &extensions)) {
David Benjamin0cbb1af2018-07-17 21:26:05 -0400180 return false;
David Benjamin794cc592017-03-25 22:24:23 -0500181 }
182
Nick Harper85194322020-05-20 16:59:29 -0700183 if (enable_early_data) {
David Benjamina93beba2019-10-15 16:58:21 -0400184 CBB early_data;
Steven Valdez7e5dd252018-01-22 15:20:31 -0500185 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_early_data) ||
David Benjamina93beba2019-10-15 16:58:21 -0400186 !CBB_add_u16_length_prefixed(&extensions, &early_data) ||
187 !CBB_add_u32(&early_data, session->ticket_max_early_data) ||
David Benjamin794cc592017-03-25 22:24:23 -0500188 !CBB_flush(&extensions)) {
David Benjamin0cbb1af2018-07-17 21:26:05 -0400189 return false;
David Benjamin794cc592017-03-25 22:24:23 -0500190 }
191 }
192
David Benjamin3675eb32021-05-18 14:01:07 -0400193 // Add a fake extension. See RFC 8701.
David Benjamin794cc592017-03-25 22:24:23 -0500194 if (!CBB_add_u16(&extensions,
David Benjamina7bc9442018-01-18 10:08:53 -0500195 ssl_get_grease_value(hs, ssl_grease_ticket_extension)) ||
David Benjamin794cc592017-03-25 22:24:23 -0500196 !CBB_add_u16(&extensions, 0 /* empty */)) {
David Benjamin0cbb1af2018-07-17 21:26:05 -0400197 return false;
David Benjamin794cc592017-03-25 22:24:23 -0500198 }
199
David Benjamin1386aad2017-07-19 23:57:40 -0400200 if (!ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin0cbb1af2018-07-17 21:26:05 -0400201 return false;
David Benjamin794cc592017-03-25 22:24:23 -0500202 }
203 }
204
David Benjamin0cbb1af2018-07-17 21:26:05 -0400205 *out_sent_tickets = true;
206 return true;
David Benjamin794cc592017-03-25 22:24:23 -0500207}
208
David Benjaminc3c88822016-11-14 10:32:04 +0900209static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400210 // At this point, most ClientHello extensions have already been processed by
211 // the common handshake logic. Resolve the remaining non-PSK parameters.
David Benjaminc3c88822016-11-14 10:32:04 +0900212 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400213 SSLMessage msg;
David Benjamin731058e2016-12-03 23:15:13 -0500214 SSL_CLIENT_HELLO client_hello;
Daniel McArdle00e434d2021-02-18 11:47:18 -0500215 if (!hs->GetClientHello(&msg, &client_hello)) {
David Benjamin34202b92016-11-16 19:07:53 +0900216 return ssl_hs_error;
217 }
218
Nick Harpercac93922020-05-07 13:53:23 -0700219 if (ssl->quic_method != nullptr && client_hello.session_id_len > 0) {
220 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_COMPATIBILITY_MODE);
221 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
222 return ssl_hs_error;
223 }
Steven Valdez520e1222017-06-13 12:45:25 -0400224 OPENSSL_memcpy(hs->session_id, client_hello.session_id,
225 client_hello.session_id_len);
226 hs->session_id_len = client_hello.session_id_len;
227
David Benjamin43cc9c62018-12-12 13:06:46 -0600228 uint16_t group_id;
229 if (!tls1_get_shared_group(hs, &group_id)) {
230 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
231 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
232 return ssl_hs_error;
233 }
234
David Benjaminc11ea9422017-08-29 16:33:21 -0400235 // Negotiate the cipher suite.
David Benjamin43cc9c62018-12-12 13:06:46 -0600236 hs->new_cipher = choose_tls13_cipher(ssl, &client_hello, group_id);
David Benjamin45738dd2017-02-09 20:01:26 -0500237 if (hs->new_cipher == NULL) {
David Benjaminf01f42a2016-11-16 19:05:33 +0900238 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
David Benjamind1e3ce12017-10-06 18:31:15 -0400239 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
David Benjaminf01f42a2016-11-16 19:05:33 +0900240 return ssl_hs_error;
241 }
242
David Benjaminc11ea9422017-08-29 16:33:21 -0400243 // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
244 // deferred. Complete it now.
David Benjamin707af292017-03-10 17:47:18 -0500245 uint8_t alert = SSL_AD_DECODE_ERROR;
246 if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400247 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin707af292017-03-10 17:47:18 -0500248 return ssl_hs_error;
249 }
250
David Benjamind55f4502021-08-11 13:19:19 -0400251 // The PRF hash is now known.
Steven Valdezcd8470f2017-10-11 12:29:36 -0400252 if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher)) {
253 return ssl_hs_error;
254 }
255
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800256 hs->tls13_state = state13_select_session;
David Benjamin707af292017-03-10 17:47:18 -0500257 return ssl_hs_ok;
258}
Steven Valdez908ac192017-01-12 13:17:07 -0500259
David Benjamin707af292017-03-10 17:47:18 -0500260static enum ssl_ticket_aead_result_t select_session(
David Benjamin37af90f2017-07-29 01:42:16 -0400261 SSL_HANDSHAKE *hs, uint8_t *out_alert, UniquePtr<SSL_SESSION> *out_session,
David Benjamin64770122019-05-04 11:00:04 -0500262 int32_t *out_ticket_age_skew, bool *out_offered_ticket,
263 const SSLMessage &msg, const SSL_CLIENT_HELLO *client_hello) {
David Benjamin707af292017-03-10 17:47:18 -0500264 SSL *const ssl = hs->ssl;
David Benjamin64770122019-05-04 11:00:04 -0500265 *out_session = nullptr;
David Benjamin707af292017-03-10 17:47:18 -0500266
David Benjamin707af292017-03-10 17:47:18 -0500267 CBS pre_shared_key;
David Benjamin64770122019-05-04 11:00:04 -0500268 *out_offered_ticket = ssl_client_hello_get_extension(
269 client_hello, &pre_shared_key, TLSEXT_TYPE_pre_shared_key);
270 if (!*out_offered_ticket) {
David Benjamin707af292017-03-10 17:47:18 -0500271 return ssl_ticket_aead_ignore_ticket;
272 }
273
David Benjamin8648c532021-08-19 18:02:37 -0400274 // Per RFC 8446, section 4.2.9, servers MUST abort the handshake if the client
David Benjamin3af62262021-03-30 13:37:53 -0400275 // sends pre_shared_key without psk_key_exchange_modes.
276 CBS unused;
277 if (!ssl_client_hello_get_extension(client_hello, &unused,
278 TLSEXT_TYPE_psk_key_exchange_modes)) {
279 *out_alert = SSL_AD_MISSING_EXTENSION;
280 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
281 return ssl_ticket_aead_error;
282 }
283
David Benjamin707af292017-03-10 17:47:18 -0500284 CBS ticket, binders;
285 uint32_t client_ticket_age;
David Benjamin9806ae02019-08-16 15:32:03 -0400286 if (!ssl_ext_pre_shared_key_parse_clienthello(
287 hs, &ticket, &binders, &client_ticket_age, out_alert, client_hello,
288 &pre_shared_key)) {
David Benjamin707af292017-03-10 17:47:18 -0500289 return ssl_ticket_aead_error;
290 }
291
David Benjamin64770122019-05-04 11:00:04 -0500292 // If the peer did not offer psk_dhe, ignore the resumption.
293 if (!hs->accept_psk_mode) {
294 return ssl_ticket_aead_ignore_ticket;
295 }
296
David Benjaminc11ea9422017-08-29 16:33:21 -0400297 // TLS 1.3 session tickets are renewed separately as part of the
298 // NewSessionTicket.
David Benjaminfd45ee72017-08-31 14:49:09 -0400299 bool unused_renew;
David Benjamin37af90f2017-07-29 01:42:16 -0400300 UniquePtr<SSL_SESSION> session;
David Benjamin707af292017-03-10 17:47:18 -0500301 enum ssl_ticket_aead_result_t ret =
David Benjamin28655672018-07-18 23:23:25 -0400302 ssl_process_ticket(hs, &session, &unused_renew, ticket, {});
David Benjamin707af292017-03-10 17:47:18 -0500303 switch (ret) {
304 case ssl_ticket_aead_success:
305 break;
306 case ssl_ticket_aead_error:
307 *out_alert = SSL_AD_INTERNAL_ERROR;
308 return ret;
309 default:
310 return ret;
311 }
312
David Benjamin37af90f2017-07-29 01:42:16 -0400313 if (!ssl_session_is_resumable(hs, session.get()) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400314 // Historically, some TLS 1.3 tickets were missing ticket_age_add.
David Benjamin707af292017-03-10 17:47:18 -0500315 !session->ticket_age_add_valid) {
David Benjamin707af292017-03-10 17:47:18 -0500316 return ssl_ticket_aead_ignore_ticket;
317 }
318
David Benjaminc11ea9422017-08-29 16:33:21 -0400319 // Recover the client ticket age and convert to seconds.
David Benjamin707af292017-03-10 17:47:18 -0500320 client_ticket_age -= session->ticket_age_add;
321 client_ticket_age /= 1000;
322
323 struct OPENSSL_timeval now;
324 ssl_get_current_time(ssl, &now);
325
David Benjaminc11ea9422017-08-29 16:33:21 -0400326 // Compute the server ticket age in seconds.
David Benjamin707af292017-03-10 17:47:18 -0500327 assert(now.tv_sec >= session->time);
328 uint64_t server_ticket_age = now.tv_sec - session->time;
329
David Benjaminc11ea9422017-08-29 16:33:21 -0400330 // To avoid overflowing |hs->ticket_age_skew|, we will not resume
331 // 68-year-old sessions.
David Benjamin707af292017-03-10 17:47:18 -0500332 if (server_ticket_age > INT32_MAX) {
David Benjamin707af292017-03-10 17:47:18 -0500333 return ssl_ticket_aead_ignore_ticket;
334 }
335
David Benjamin8c98bac2019-08-15 20:40:01 -0400336 *out_ticket_age_skew = static_cast<int32_t>(client_ticket_age) -
337 static_cast<int32_t>(server_ticket_age);
David Benjamin707af292017-03-10 17:47:18 -0500338
David Benjaminc11ea9422017-08-29 16:33:21 -0400339 // Check the PSK binder.
David Benjamin7934f082017-08-01 16:32:25 -0400340 if (!tls13_verify_psk_binder(hs, session.get(), msg, &binders)) {
David Benjamin707af292017-03-10 17:47:18 -0500341 *out_alert = SSL_AD_DECRYPT_ERROR;
342 return ssl_ticket_aead_error;
343 }
344
David Benjamin37af90f2017-07-29 01:42:16 -0400345 *out_session = std::move(session);
David Benjamin707af292017-03-10 17:47:18 -0500346 return ssl_ticket_aead_success;
347}
348
Nick Harper7c522992020-04-30 14:15:49 -0700349static bool quic_ticket_compatible(const SSL_SESSION *session,
350 const SSL_CONFIG *config) {
351 if (!session->is_quic) {
352 return true;
353 }
Nick Harper85194322020-05-20 16:59:29 -0700354
355 if (session->quic_early_data_context.empty() ||
356 config->quic_early_data_context.size() !=
357 session->quic_early_data_context.size() ||
358 CRYPTO_memcmp(config->quic_early_data_context.data(),
359 session->quic_early_data_context.data(),
360 session->quic_early_data_context.size()) != 0) {
Nick Harper7c522992020-04-30 14:15:49 -0700361 return false;
362 }
363 return true;
364}
365
David Benjamin707af292017-03-10 17:47:18 -0500366static enum ssl_hs_wait_t do_select_session(SSL_HANDSHAKE *hs) {
367 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400368 SSLMessage msg;
David Benjamin707af292017-03-10 17:47:18 -0500369 SSL_CLIENT_HELLO client_hello;
Daniel McArdle00e434d2021-02-18 11:47:18 -0500370 if (!hs->GetClientHello(&msg, &client_hello)) {
David Benjamin707af292017-03-10 17:47:18 -0500371 return ssl_hs_error;
372 }
373
David Benjamin4eb95cc2016-11-16 17:08:23 +0900374 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin37af90f2017-07-29 01:42:16 -0400375 UniquePtr<SSL_SESSION> session;
David Benjamin64770122019-05-04 11:00:04 -0500376 bool offered_ticket = false;
377 switch (select_session(hs, &alert, &session, &ssl->s3->ticket_age_skew,
378 &offered_ticket, msg, &client_hello)) {
David Benjamin707af292017-03-10 17:47:18 -0500379 case ssl_ticket_aead_ignore_ticket:
David Benjamin37af90f2017-07-29 01:42:16 -0400380 assert(!session);
David Benjamin962b3752021-05-10 15:17:18 -0400381 if (!ssl_get_new_session(hs)) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400382 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjaminf01f42a2016-11-16 19:05:33 +0900383 return ssl_hs_error;
384 }
David Benjamin707af292017-03-10 17:47:18 -0500385 break;
Steven Valdeza833c352016-11-01 13:39:36 -0400386
David Benjamin707af292017-03-10 17:47:18 -0500387 case ssl_ticket_aead_success:
David Benjaminc11ea9422017-08-29 16:33:21 -0400388 // Carry over authentication information from the previous handshake into
389 // a fresh session.
David Benjamin37af90f2017-07-29 01:42:16 -0400390 hs->new_session =
391 SSL_SESSION_dup(session.get(), SSL_SESSION_DUP_AUTH_ONLY);
David Benjamin6433a912019-05-04 14:17:08 -0500392 if (hs->new_session == nullptr) {
393 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
394 return ssl_hs_error;
395 }
Steven Valdez2d850622017-01-11 11:34:52 -0500396
David Benjamin046bc1f2017-08-31 15:06:42 -0400397 ssl->s3->session_reused = true;
David Benjamin9b2cdb72021-04-01 23:21:53 -0400398 hs->can_release_private_key = true;
David Benjamin707af292017-03-10 17:47:18 -0500399
David Benjaminc11ea9422017-08-29 16:33:21 -0400400 // Resumption incorporates fresh key material, so refresh the timeout.
David Benjamin98472cb2018-05-02 16:05:36 -0400401 ssl_session_renew_timeout(ssl, hs->new_session.get(),
402 ssl->session_ctx->session_psk_dhe_timeout);
David Benjamin707af292017-03-10 17:47:18 -0500403 break;
404
405 case ssl_ticket_aead_error:
David Benjamind1e3ce12017-10-06 18:31:15 -0400406 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin707af292017-03-10 17:47:18 -0500407 return ssl_hs_error;
408
409 case ssl_ticket_aead_retry:
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800410 hs->tls13_state = state13_select_session;
David Benjamin707af292017-03-10 17:47:18 -0500411 return ssl_hs_pending_ticket;
412 }
413
Steven Valdez51607f12020-08-05 10:46:05 -0400414 // Negotiate ALPS now, after ALPN is negotiated and |hs->new_session| is
415 // initialized.
416 if (!ssl_negotiate_alps(hs, &alert, &client_hello)) {
417 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
418 return ssl_hs_error;
419 }
420
David Benjamin3b8c5ec2021-04-12 17:43:23 -0400421 // Record connection properties in the new session.
422 hs->new_session->cipher = hs->new_cipher;
423 if (!tls1_get_shared_group(hs, &hs->new_session->group_id)) {
424 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
425 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
426 return ssl_hs_error;
427 }
428
429 // Determine if we need HelloRetryRequest.
430 bool found_key_share;
431 if (!ssl_ext_key_share_parse_clienthello(hs, &found_key_share,
432 /*out_key_share=*/nullptr, &alert,
433 &client_hello)) {
434 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
435 return ssl_hs_error;
436 }
437
Steven Valdez51607f12020-08-05 10:46:05 -0400438 // Determine if we're negotiating 0-RTT.
439 if (!ssl->enable_early_data) {
440 ssl->s3->early_data_reason = ssl_early_data_disabled;
441 } else if (!offered_ticket) {
442 ssl->s3->early_data_reason = ssl_early_data_no_session_offered;
443 } else if (!session) {
444 ssl->s3->early_data_reason = ssl_early_data_session_not_resumed;
445 } else if (session->ticket_max_early_data == 0) {
446 ssl->s3->early_data_reason = ssl_early_data_unsupported_for_session;
447 } else if (!hs->early_data_offered) {
448 ssl->s3->early_data_reason = ssl_early_data_peer_declined;
David Benjamin8acec002021-05-19 13:03:34 -0400449 } else if (hs->channel_id_negotiated) {
Steven Valdez51607f12020-08-05 10:46:05 -0400450 // Channel ID is incompatible with 0-RTT.
451 ssl->s3->early_data_reason = ssl_early_data_channel_id;
Steven Valdez51607f12020-08-05 10:46:05 -0400452 } else if (MakeConstSpan(ssl->s3->alpn_selected) != session->early_alpn) {
453 // The negotiated ALPN must match the one in the ticket.
454 ssl->s3->early_data_reason = ssl_early_data_alpn_mismatch;
455 } else if (hs->new_session->has_application_settings !=
456 session->has_application_settings ||
457 MakeConstSpan(hs->new_session->local_application_settings) !=
458 session->local_application_settings) {
459 ssl->s3->early_data_reason = ssl_early_data_alps_mismatch;
460 } else if (ssl->s3->ticket_age_skew < -kMaxTicketAgeSkewSeconds ||
461 kMaxTicketAgeSkewSeconds < ssl->s3->ticket_age_skew) {
462 ssl->s3->early_data_reason = ssl_early_data_ticket_age_skew;
463 } else if (!quic_ticket_compatible(session.get(), hs->config)) {
464 ssl->s3->early_data_reason = ssl_early_data_quic_parameter_mismatch;
David Benjamin3b8c5ec2021-04-12 17:43:23 -0400465 } else if (!found_key_share) {
466 ssl->s3->early_data_reason = ssl_early_data_hello_retry_request;
Steven Valdez51607f12020-08-05 10:46:05 -0400467 } else {
468 // |ssl_session_is_resumable| forbids cross-cipher resumptions even if the
469 // PRF hashes match.
470 assert(hs->new_cipher == session->cipher);
471
472 ssl->s3->early_data_reason = ssl_early_data_accepted;
473 ssl->s3->early_data_accepted = true;
474 }
475
Steven Valdez51607f12020-08-05 10:46:05 -0400476 // Store the ALPN and ALPS values in the session for 0-RTT. Note the peer
477 // applications settings are not generally known until client
478 // EncryptedExtensions.
David Benjaminbfdd1a92018-06-29 16:26:38 -0400479 if (!hs->new_session->early_alpn.CopyFrom(ssl->s3->alpn_selected)) {
480 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
481 return ssl_hs_error;
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500482 }
483
Steven Valdez51607f12020-08-05 10:46:05 -0400484 // The peer applications settings are usually received later, in
485 // EncryptedExtensions. But, in 0-RTT handshakes, we carry over the
486 // values from |session|. Do this now, before |session| is discarded.
487 if (ssl->s3->early_data_accepted &&
488 hs->new_session->has_application_settings &&
489 !hs->new_session->peer_application_settings.CopyFrom(
490 session->peer_application_settings)) {
491 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
492 return ssl_hs_error;
493 }
494
Nick Harper5e086952020-09-30 13:59:14 -0700495 // Copy the QUIC early data context to the session.
496 if (ssl->enable_early_data && ssl->quic_method) {
497 if (!hs->new_session->quic_early_data_context.CopyFrom(
498 hs->config->quic_early_data_context)) {
499 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
500 return ssl_hs_error;
501 }
502 }
503
David Benjamin707af292017-03-10 17:47:18 -0500504 if (ssl->ctx->dos_protection_cb != NULL &&
505 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400506 // Connection rejected for DOS reasons.
David Benjamin707af292017-03-10 17:47:18 -0500507 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
David Benjamind1e3ce12017-10-06 18:31:15 -0400508 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin707af292017-03-10 17:47:18 -0500509 return ssl_hs_error;
510 }
511
Steven Valdezcd8470f2017-10-11 12:29:36 -0400512 size_t hash_len = EVP_MD_size(
513 ssl_get_handshake_digest(ssl_protocol_version(ssl), hs->new_cipher));
514
515 // Set up the key schedule and incorporate the PSK into the running secret.
David Benjamin83a49932021-05-20 15:57:09 -0400516 if (!tls13_init_key_schedule(
517 hs, ssl->s3->session_reused
518 ? MakeConstSpan(hs->new_session->secret,
519 hs->new_session->secret_length)
520 : MakeConstSpan(kZeroes, hash_len)) ||
521 !ssl_hash_message(hs, msg)) {
David Benjamin9806ae02019-08-16 15:32:03 -0400522 return ssl_hs_error;
523 }
524
David Benjamin02e62562017-12-18 18:04:01 -0500525 if (ssl->s3->early_data_accepted) {
David Benjamind6343572019-08-15 17:29:02 -0400526 if (!tls13_derive_early_secret(hs)) {
Steven Valdez2d850622017-01-11 11:34:52 -0500527 return ssl_hs_error;
528 }
529 } else if (hs->early_data_offered) {
David Benjamin046bc1f2017-08-31 15:06:42 -0400530 ssl->s3->skip_early_data = true;
Steven Valdez2d850622017-01-11 11:34:52 -0500531 }
532
David Benjamin3b8c5ec2021-04-12 17:43:23 -0400533 if (!found_key_share) {
534 ssl->method->next_message(ssl);
535 if (!hs->transcript.UpdateForHelloRetryRequest()) {
536 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400537 }
David Benjamin3b8c5ec2021-04-12 17:43:23 -0400538 hs->tls13_state = state13_send_hello_retry_request;
539 return ssl_hs_ok;
540 }
541
542 if (!resolve_ecdhe_secret(hs, &client_hello)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400543 return ssl_hs_error;
544 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400545
David Benjamin8f94c312017-08-01 17:35:55 -0400546 ssl->method->next_message(ssl);
Daniel McArdle00e434d2021-02-18 11:47:18 -0500547 hs->ech_client_hello_buf.Reset();
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800548 hs->tls13_state = state13_send_server_hello;
Steven Valdez5440fe02016-07-18 12:40:30 -0400549 return ssl_hs_ok;
550}
Steven Valdez143e8b32016-07-11 13:19:03 -0400551
David Benjaminc3c88822016-11-14 10:32:04 +0900552static enum ssl_hs_wait_t do_send_hello_retry_request(SSL_HANDSHAKE *hs) {
553 SSL *const ssl = hs->ssl;
David Benjaminb571e772021-03-25 19:42:16 -0400554 if (hs->hints_requested) {
555 return ssl_hs_hints_ready;
556 }
Steven Valdez964b2372017-11-07 17:09:52 -0500557
Steven Valdez7e5dd252018-01-22 15:20:31 -0500558 ScopedCBB cbb;
559 CBB body, session_id, extensions;
560 uint16_t group_id;
561 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
562 !CBB_add_u16(&body, TLS1_2_VERSION) ||
563 !CBB_add_bytes(&body, kHelloRetryRequest, SSL3_RANDOM_SIZE) ||
564 !CBB_add_u8_length_prefixed(&body, &session_id) ||
565 !CBB_add_bytes(&session_id, hs->session_id, hs->session_id_len) ||
David Benjamin3743aaf2020-09-21 13:55:16 -0400566 !CBB_add_u16(&body, SSL_CIPHER_get_protocol_id(hs->new_cipher)) ||
Steven Valdez7e5dd252018-01-22 15:20:31 -0500567 !CBB_add_u8(&body, 0 /* no compression */) ||
568 !tls1_get_shared_group(hs, &group_id) ||
569 !CBB_add_u16_length_prefixed(&body, &extensions) ||
570 !CBB_add_u16(&extensions, TLSEXT_TYPE_supported_versions) ||
571 !CBB_add_u16(&extensions, 2 /* length */) ||
572 !CBB_add_u16(&extensions, ssl->version) ||
573 !CBB_add_u16(&extensions, TLSEXT_TYPE_key_share) ||
574 !CBB_add_u16(&extensions, 2 /* length */) ||
David Benjamin18b68362021-06-18 23:13:46 -0400575 !CBB_add_u16(&extensions, group_id)) {
Steven Valdez7e5dd252018-01-22 15:20:31 -0500576 return ssl_hs_error;
577 }
David Benjamin18b68362021-06-18 23:13:46 -0400578 if (hs->ech_is_inner) {
579 // Fill a placeholder for the ECH confirmation value.
580 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_encrypted_client_hello) ||
581 !CBB_add_u16(&extensions, ECH_CONFIRMATION_SIGNAL_LEN) ||
582 !CBB_add_zeros(&extensions, ECH_CONFIRMATION_SIGNAL_LEN)) {
583 return ssl_hs_error;
584 }
585 }
586 Array<uint8_t> hrr;
587 if (!ssl->method->finish_message(ssl, cbb.get(), &hrr)) {
588 return ssl_hs_error;
589 }
590 if (hs->ech_is_inner) {
591 // Now that the message is encoded, fill in the whole value.
592 size_t offset = hrr.size() - ECH_CONFIRMATION_SIGNAL_LEN;
593 if (!ssl_ech_accept_confirmation(
594 hs, MakeSpan(hrr).last(ECH_CONFIRMATION_SIGNAL_LEN),
595 ssl->s3->client_random, hs->transcript, /*is_hrr=*/true, hrr,
596 offset)) {
597 return ssl_hs_error;
598 }
599 }
Steven Valdez964b2372017-11-07 17:09:52 -0500600
David Benjamin18b68362021-06-18 23:13:46 -0400601 if (!ssl->method->add_message(ssl, std::move(hrr)) ||
602 !ssl->method->add_change_cipher_spec(ssl)) {
Steven Valdez7e5dd252018-01-22 15:20:31 -0500603 return ssl_hs_error;
Steven Valdez5440fe02016-07-18 12:40:30 -0400604 }
605
Kris Kwiatkowskib11902a2019-08-24 11:01:04 +0100606 ssl->s3->used_hello_retry_request = true;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800607 hs->tls13_state = state13_read_second_client_hello;
David Benjamin7934f082017-08-01 16:32:25 -0400608 return ssl_hs_flush;
Steven Valdez5440fe02016-07-18 12:40:30 -0400609}
610
David Benjamin7934f082017-08-01 16:32:25 -0400611static enum ssl_hs_wait_t do_read_second_client_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900612 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400613 SSLMessage msg;
614 if (!ssl->method->get_message(ssl, &msg)) {
615 return ssl_hs_read_message;
616 }
617 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400618 return ssl_hs_error;
619 }
David Benjamin731058e2016-12-03 23:15:13 -0500620 SSL_CLIENT_HELLO client_hello;
Daniel McArdle00e434d2021-02-18 11:47:18 -0500621 if (!ssl_client_hello_init(ssl, &client_hello, msg.body)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400622 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
David Benjamind1e3ce12017-10-06 18:31:15 -0400623 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez5440fe02016-07-18 12:40:30 -0400624 return ssl_hs_error;
625 }
626
David Benjaminba423c92021-06-15 16:26:58 -0400627 if (ssl->s3->ech_status == ssl_ech_accepted) {
David Benjamin18b68362021-06-18 23:13:46 -0400628 // If we previously accepted the ClientHelloInner, the second ClientHello
629 // must contain an outer encrypted_client_hello extension.
Daniel McArdle00e434d2021-02-18 11:47:18 -0500630 CBS ech_body;
631 if (!ssl_client_hello_get_extension(&client_hello, &ech_body,
632 TLSEXT_TYPE_encrypted_client_hello)) {
633 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
634 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION);
635 return ssl_hs_error;
636 }
Daniel McArdle00e434d2021-02-18 11:47:18 -0500637 uint16_t kdf_id, aead_id;
David Benjamin18b68362021-06-18 23:13:46 -0400638 uint8_t type, config_id;
Steven Valdez94a63a52021-04-29 10:52:42 -0400639 CBS enc, payload;
David Benjamin18b68362021-06-18 23:13:46 -0400640 if (!CBS_get_u8(&ech_body, &type) || //
641 type != ECH_CLIENT_OUTER || //
642 !CBS_get_u16(&ech_body, &kdf_id) || //
Daniel McArdle00e434d2021-02-18 11:47:18 -0500643 !CBS_get_u16(&ech_body, &aead_id) ||
Steven Valdez94a63a52021-04-29 10:52:42 -0400644 !CBS_get_u8(&ech_body, &config_id) ||
Daniel McArdle00e434d2021-02-18 11:47:18 -0500645 !CBS_get_u16_length_prefixed(&ech_body, &enc) ||
646 !CBS_get_u16_length_prefixed(&ech_body, &payload) ||
647 CBS_len(&ech_body) != 0) {
648 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
649 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
650 return ssl_hs_error;
651 }
652
David Benjaminf39c81d2021-05-03 18:39:46 -0400653 if (kdf_id != EVP_HPKE_KDF_id(EVP_HPKE_CTX_kdf(hs->ech_hpke_ctx.get())) ||
654 aead_id !=
655 EVP_HPKE_AEAD_id(EVP_HPKE_CTX_aead(hs->ech_hpke_ctx.get())) ||
656 config_id != hs->ech_config_id || CBS_len(&enc) > 0) {
Daniel McArdle00e434d2021-02-18 11:47:18 -0500657 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
658 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
659 return ssl_hs_error;
660 }
661
662 // Decrypt the payload with the HPKE context from the first ClientHello.
David Benjamin44425dd2022-01-27 12:22:42 -0500663 uint8_t alert = SSL_AD_DECODE_ERROR;
Daniel McArdle00e434d2021-02-18 11:47:18 -0500664 bool unused;
David Benjamin44425dd2022-01-27 12:22:42 -0500665 if (!ssl_client_hello_decrypt(hs, &alert, &unused,
666 &hs->ech_client_hello_buf, &client_hello,
667 payload)) {
Daniel McArdle00e434d2021-02-18 11:47:18 -0500668 // Decryption failure is fatal in the second ClientHello.
669 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
Daniel McArdle00e434d2021-02-18 11:47:18 -0500670 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
671 return ssl_hs_error;
672 }
Daniel McArdle00e434d2021-02-18 11:47:18 -0500673
674 // Reparse |client_hello| from the buffer owned by |hs|.
675 if (!hs->GetClientHello(&msg, &client_hello)) {
676 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
677 return ssl_hs_error;
678 }
679 }
680
David Benjamin9806ae02019-08-16 15:32:03 -0400681 // We perform all our negotiation based on the first ClientHello (for
682 // consistency with what |select_certificate_cb| observed), which is in the
683 // transcript, so we can ignore most of this second one.
684 //
685 // We do, however, check the second PSK binder. This covers the client key
686 // share, in case we ever send half-RTT data (we currently do not). It is also
687 // a tricky computation, so we enforce the peer handled it correctly.
688 if (ssl->s3->session_reused) {
689 CBS pre_shared_key;
690 if (!ssl_client_hello_get_extension(&client_hello, &pre_shared_key,
691 TLSEXT_TYPE_pre_shared_key)) {
692 OPENSSL_PUT_ERROR(SSL, SSL_R_INCONSISTENT_CLIENT_HELLO);
693 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
694 return ssl_hs_error;
695 }
696
697 CBS ticket, binders;
698 uint32_t client_ticket_age;
699 uint8_t alert = SSL_AD_DECODE_ERROR;
700 if (!ssl_ext_pre_shared_key_parse_clienthello(
701 hs, &ticket, &binders, &client_ticket_age, &alert, &client_hello,
702 &pre_shared_key)) {
703 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
704 return ssl_hs_error;
705 }
706
707 // Note it is important that we do not obtain a new |SSL_SESSION| from
708 // |ticket|. We have already selected parameters based on the first
709 // ClientHello (in the transcript) and must not switch partway through.
710 if (!tls13_verify_psk_binder(hs, hs->new_session.get(), msg, &binders)) {
711 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
712 return ssl_hs_error;
713 }
714 }
715
David Benjamin3b8c5ec2021-04-12 17:43:23 -0400716 if (!resolve_ecdhe_secret(hs, &client_hello)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400717 return ssl_hs_error;
718 }
719
David Benjamin7934f082017-08-01 16:32:25 -0400720 if (!ssl_hash_message(hs, msg)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400721 return ssl_hs_error;
722 }
723
David Benjaminf9cc26f2020-02-09 16:49:31 -0500724 // ClientHello should be the end of the flight.
725 if (ssl->method->has_unprocessed_handshake_data(ssl)) {
726 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
727 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA);
728 return ssl_hs_error;
729 }
730
David Benjamin8f94c312017-08-01 17:35:55 -0400731 ssl->method->next_message(ssl);
Daniel McArdle00e434d2021-02-18 11:47:18 -0500732 hs->ech_client_hello_buf.Reset();
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800733 hs->tls13_state = state13_send_server_hello;
Steven Valdez143e8b32016-07-11 13:19:03 -0400734 return ssl_hs_ok;
735}
736
David Benjaminc3c88822016-11-14 10:32:04 +0900737static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
738 SSL *const ssl = hs->ssl;
David Benjamin81b7bc32017-01-12 19:44:57 -0500739
Dan McArdlec2959352020-10-29 14:31:31 -0400740 Span<uint8_t> random(ssl->s3->server_random);
David Benjaminb571e772021-03-25 19:42:16 -0400741
742 SSL_HANDSHAKE_HINTS *const hints = hs->hints.get();
743 if (hints && !hs->hints_requested &&
David Benjamin4a6c8fd2022-07-21 14:05:41 -0700744 hints->server_random_tls13.size() == random.size()) {
745 OPENSSL_memcpy(random.data(), hints->server_random_tls13.data(),
746 random.size());
David Benjaminb571e772021-03-25 19:42:16 -0400747 } else {
748 RAND_bytes(random.data(), random.size());
749 if (hints && hs->hints_requested &&
David Benjamin4a6c8fd2022-07-21 14:05:41 -0700750 !hints->server_random_tls13.CopyFrom(random)) {
David Benjaminb571e772021-03-25 19:42:16 -0400751 return ssl_hs_error;
752 }
753 }
Dan McArdlec2959352020-10-29 14:31:31 -0400754
David Benjamin83a49932021-05-20 15:57:09 -0400755 Array<uint8_t> server_hello;
756 ScopedCBB cbb;
757 CBB body, extensions, session_id;
758 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
759 !CBB_add_u16(&body, TLS1_2_VERSION) ||
760 !CBB_add_bytes(&body, ssl->s3->server_random,
761 sizeof(ssl->s3->server_random)) ||
762 !CBB_add_u8_length_prefixed(&body, &session_id) ||
763 !CBB_add_bytes(&session_id, hs->session_id, hs->session_id_len) ||
764 !CBB_add_u16(&body, SSL_CIPHER_get_protocol_id(hs->new_cipher)) ||
765 !CBB_add_u8(&body, 0) ||
766 !CBB_add_u16_length_prefixed(&body, &extensions) ||
767 !ssl_ext_pre_shared_key_add_serverhello(hs, &extensions) ||
768 !ssl_ext_key_share_add_serverhello(hs, &extensions) ||
769 !ssl_ext_supported_versions_add_serverhello(hs, &extensions) ||
770 !ssl->method->finish_message(ssl, cbb.get(), &server_hello)) {
771 return ssl_hs_error;
Dan McArdlec2959352020-10-29 14:31:31 -0400772 }
773
David Benjamin18b68362021-06-18 23:13:46 -0400774 assert(ssl->s3->ech_status != ssl_ech_accepted || hs->ech_is_inner);
775 if (hs->ech_is_inner) {
David Benjamin83a49932021-05-20 15:57:09 -0400776 // Fill in the ECH confirmation signal.
David Benjamin18b68362021-06-18 23:13:46 -0400777 const size_t offset = ssl_ech_confirmation_signal_hello_offset(ssl);
David Benjamin006f20a2021-06-22 23:00:49 -0400778 Span<uint8_t> random_suffix = random.last(ECH_CONFIRMATION_SIGNAL_LEN);
David Benjamin18b68362021-06-18 23:13:46 -0400779 if (!ssl_ech_accept_confirmation(hs, random_suffix, ssl->s3->client_random,
780 hs->transcript,
781 /*is_hrr=*/false, server_hello, offset)) {
David Benjamin83a49932021-05-20 15:57:09 -0400782 return ssl_hs_error;
783 }
784
785 // Update |server_hello|.
David Benjamin83a49932021-05-20 15:57:09 -0400786 Span<uint8_t> server_hello_out =
787 MakeSpan(server_hello).subspan(offset, ECH_CONFIRMATION_SIGNAL_LEN);
788 OPENSSL_memcpy(server_hello_out.data(), random_suffix.data(),
789 ECH_CONFIRMATION_SIGNAL_LEN);
790 }
791
792 if (!ssl->method->add_message(ssl, std::move(server_hello))) {
David Benjamin1386aad2017-07-19 23:57:40 -0400793 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400794 }
795
David Benjamin08b1f382023-02-28 17:22:23 -0500796 hs->key_share_ciphertext.Reset(); // No longer needed.
Kris Kwiatkowskib11902a2019-08-24 11:01:04 +0100797 if (!ssl->s3->used_hello_retry_request &&
David Benjamin666d16e2017-10-06 18:45:16 -0400798 !ssl->method->add_change_cipher_spec(ssl)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400799 return ssl_hs_error;
Steven Valdez520e1222017-06-13 12:45:25 -0400800 }
801
David Benjaminc11ea9422017-08-29 16:33:21 -0400802 // Derive and enable the handshake traffic secrets.
Steven Valdez4cb84942016-12-16 11:29:28 -0500803 if (!tls13_derive_handshake_secrets(hs) ||
David Benjamine530ea32019-08-16 19:28:00 -0400804 !tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_seal,
David Benjamin754d4c92020-02-11 16:27:21 -0500805 hs->new_session.get(),
David Benjamine530ea32019-08-16 19:28:00 -0400806 hs->server_handshake_secret())) {
David Benjamin1386aad2017-07-19 23:57:40 -0400807 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400808 }
809
David Benjaminc11ea9422017-08-29 16:33:21 -0400810 // Send EncryptedExtensions.
David Benjamin1386aad2017-07-19 23:57:40 -0400811 if (!ssl->method->init_message(ssl, cbb.get(), &body,
Steven Valdez143e8b32016-07-11 13:19:03 -0400812 SSL3_MT_ENCRYPTED_EXTENSIONS) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500813 !ssl_add_serverhello_tlsext(hs, &body) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400814 !ssl_add_message_cbb(ssl, cbb.get())) {
815 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400816 }
817
David Benjaminc3648fa2017-07-01 10:50:56 -0400818 if (!ssl->s3->session_reused) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400819 // Determine whether to request a client certificate.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700820 hs->cert_request = !!(hs->config->verify_mode & SSL_VERIFY_PEER);
David Benjaminc11ea9422017-08-29 16:33:21 -0400821 // Only request a certificate if Channel ID isn't negotiated.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700822 if ((hs->config->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
David Benjamin8acec002021-05-19 13:03:34 -0400823 hs->channel_id_negotiated) {
David Benjaminfd45ee72017-08-31 14:49:09 -0400824 hs->cert_request = false;
David Benjaminc3648fa2017-07-01 10:50:56 -0400825 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400826 }
827
David Benjaminc11ea9422017-08-29 16:33:21 -0400828 // Send a CertificateRequest, if necessary.
David Benjamin81b7bc32017-01-12 19:44:57 -0500829 if (hs->cert_request) {
Steven Valdez7e5dd252018-01-22 15:20:31 -0500830 CBB cert_request_extensions, sigalg_contents, sigalgs_cbb;
831 if (!ssl->method->init_message(ssl, cbb.get(), &body,
832 SSL3_MT_CERTIFICATE_REQUEST) ||
833 !CBB_add_u8(&body, 0 /* no certificate_request_context. */) ||
834 !CBB_add_u16_length_prefixed(&body, &cert_request_extensions) ||
835 !CBB_add_u16(&cert_request_extensions,
836 TLSEXT_TYPE_signature_algorithms) ||
837 !CBB_add_u16_length_prefixed(&cert_request_extensions,
838 &sigalg_contents) ||
839 !CBB_add_u16_length_prefixed(&sigalg_contents, &sigalgs_cbb) ||
David Benjaminebad5082020-02-03 19:32:19 -0500840 !tls12_add_verify_sigalgs(hs, &sigalgs_cbb)) {
Steven Valdez7e5dd252018-01-22 15:20:31 -0500841 return ssl_hs_error;
842 }
843
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700844 if (ssl_has_client_CAs(hs->config)) {
Steven Valdez7e5dd252018-01-22 15:20:31 -0500845 CBB ca_contents;
846 if (!CBB_add_u16(&cert_request_extensions,
847 TLSEXT_TYPE_certificate_authorities) ||
Steven Valdezcd8470f2017-10-11 12:29:36 -0400848 !CBB_add_u16_length_prefixed(&cert_request_extensions,
Steven Valdez7e5dd252018-01-22 15:20:31 -0500849 &ca_contents) ||
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700850 !ssl_add_client_CA_list(hs, &ca_contents) ||
Steven Valdez7e5dd252018-01-22 15:20:31 -0500851 !CBB_flush(&cert_request_extensions)) {
Steven Valdezcd8470f2017-10-11 12:29:36 -0400852 return ssl_hs_error;
853 }
Steven Valdez7e5dd252018-01-22 15:20:31 -0500854 }
Steven Valdezcd8470f2017-10-11 12:29:36 -0400855
Steven Valdez7e5dd252018-01-22 15:20:31 -0500856 if (!ssl_add_message_cbb(ssl, cbb.get())) {
857 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400858 }
859 }
860
David Benjaminc11ea9422017-08-29 16:33:21 -0400861 // Send the server Certificate message, if necessary.
David Benjamin81b7bc32017-01-12 19:44:57 -0500862 if (!ssl->s3->session_reused) {
Christopher Patton9cde8482018-07-17 11:36:36 -0700863 if (!ssl_has_certificate(hs)) {
David Benjamin81b7bc32017-01-12 19:44:57 -0500864 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
David Benjamin1386aad2017-07-19 23:57:40 -0400865 return ssl_hs_error;
David Benjamin81b7bc32017-01-12 19:44:57 -0500866 }
867
David Benjamin0f24bed2017-01-12 19:46:50 -0500868 if (!tls13_add_certificate(hs)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400869 return ssl_hs_error;
David Benjamin81b7bc32017-01-12 19:44:57 -0500870 }
871
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800872 hs->tls13_state = state13_send_server_certificate_verify;
David Benjamin81b7bc32017-01-12 19:44:57 -0500873 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400874 }
875
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800876 hs->tls13_state = state13_send_server_finished;
David Benjamin25ac2512017-01-12 19:31:28 -0500877 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400878}
879
David Benjamin44148742017-06-17 13:20:59 -0400880static enum ssl_hs_wait_t do_send_server_certificate_verify(SSL_HANDSHAKE *hs) {
881 switch (tls13_add_certificate_verify(hs)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400882 case ssl_private_key_success:
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800883 hs->tls13_state = state13_send_server_finished;
David Benjamin25ac2512017-01-12 19:31:28 -0500884 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400885
886 case ssl_private_key_retry:
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800887 hs->tls13_state = state13_send_server_certificate_verify;
Steven Valdez143e8b32016-07-11 13:19:03 -0400888 return ssl_hs_private_key_operation;
889
890 case ssl_private_key_failure:
891 return ssl_hs_error;
892 }
893
894 assert(0);
895 return ssl_hs_error;
896}
897
David Benjaminc3c88822016-11-14 10:32:04 +0900898static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900899 SSL *const ssl = hs->ssl;
David Benjaminb571e772021-03-25 19:42:16 -0400900 if (hs->hints_requested) {
901 return ssl_hs_hints_ready;
902 }
903
David Benjamin9b2cdb72021-04-01 23:21:53 -0400904 hs->can_release_private_key = true;
David Benjamin0f24bed2017-01-12 19:46:50 -0500905 if (!tls13_add_finished(hs) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400906 // Update the secret to the master secret and derive traffic keys.
David Benjamine530ea32019-08-16 19:28:00 -0400907 !tls13_advance_key_schedule(
908 hs, MakeConstSpan(kZeroes, hs->transcript.DigestLen())) ||
David Benjamin6e4fc332016-11-17 16:43:08 +0900909 !tls13_derive_application_secrets(hs) ||
David Benjamine530ea32019-08-16 19:28:00 -0400910 !tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_seal,
David Benjamin754d4c92020-02-11 16:27:21 -0500911 hs->new_session.get(),
David Benjamine530ea32019-08-16 19:28:00 -0400912 hs->server_traffic_secret_0())) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400913 return ssl_hs_error;
914 }
915
Matthew Braithwaite093a8232020-01-28 14:06:55 -0800916 hs->tls13_state = state13_send_half_rtt_ticket;
David Benjamin0c306492020-02-09 16:28:52 -0500917 return hs->handback ? ssl_hs_handback : ssl_hs_ok;
Matthew Braithwaite093a8232020-01-28 14:06:55 -0800918}
919
920static enum ssl_hs_wait_t do_send_half_rtt_ticket(SSL_HANDSHAKE *hs) {
921 SSL *const ssl = hs->ssl;
922
David Benjamin02e62562017-12-18 18:04:01 -0500923 if (ssl->s3->early_data_accepted) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400924 // If accepting 0-RTT, we send tickets half-RTT. This gets the tickets on
925 // the wire sooner and also avoids triggering a write on |SSL_read| when
926 // processing the client Finished. This requires computing the client
David Benjamina130ce02018-08-14 22:26:39 -0500927 // Finished early. See RFC 8446, section 4.6.1.
Steven Valdez7e5dd252018-01-22 15:20:31 -0500928 static const uint8_t kEndOfEarlyData[4] = {SSL3_MT_END_OF_EARLY_DATA, 0,
929 0, 0};
David Benjamind6343572019-08-15 17:29:02 -0400930 if (ssl->quic_method == nullptr &&
931 !hs->transcript.Update(kEndOfEarlyData)) {
Steven Valdez7e5dd252018-01-22 15:20:31 -0500932 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
933 return ssl_hs_error;
Steven Valdezcd8470f2017-10-11 12:29:36 -0400934 }
935
David Benjamin794cc592017-03-25 22:24:23 -0500936 size_t finished_len;
David Benjamine530ea32019-08-16 19:28:00 -0400937 if (!tls13_finished_mac(hs, hs->expected_client_finished().data(),
938 &finished_len, false /* client */)) {
David Benjamin794cc592017-03-25 22:24:23 -0500939 return ssl_hs_error;
940 }
941
David Benjamine530ea32019-08-16 19:28:00 -0400942 if (finished_len != hs->expected_client_finished().size()) {
David Benjamin794cc592017-03-25 22:24:23 -0500943 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
944 return ssl_hs_error;
945 }
946
David Benjaminc11ea9422017-08-29 16:33:21 -0400947 // Feed the predicted Finished into the transcript. This allows us to derive
948 // the resumption secret early and send half-RTT tickets.
949 //
950 // TODO(davidben): This will need to be updated for DTLS 1.3.
David Benjamin794cc592017-03-25 22:24:23 -0500951 assert(!SSL_is_dtls(hs->ssl));
David Benjamine530ea32019-08-16 19:28:00 -0400952 assert(hs->expected_client_finished().size() <= 0xff);
953 uint8_t header[4] = {
954 SSL3_MT_FINISHED, 0, 0,
955 static_cast<uint8_t>(hs->expected_client_finished().size())};
David Benjamin0cbb1af2018-07-17 21:26:05 -0400956 bool unused_sent_tickets;
David Benjamin75a1f232017-10-11 17:19:19 -0400957 if (!hs->transcript.Update(header) ||
David Benjamine530ea32019-08-16 19:28:00 -0400958 !hs->transcript.Update(hs->expected_client_finished()) ||
David Benjamin8f94c312017-08-01 17:35:55 -0400959 !tls13_derive_resumption_secret(hs) ||
David Benjamin0cbb1af2018-07-17 21:26:05 -0400960 !add_new_session_tickets(hs, &unused_sent_tickets)) {
David Benjamin794cc592017-03-25 22:24:23 -0500961 return ssl_hs_error;
962 }
963 }
964
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800965 hs->tls13_state = state13_read_second_client_flight;
Steven Valdez2d850622017-01-11 11:34:52 -0500966 return ssl_hs_flush;
967}
968
969static enum ssl_hs_wait_t do_read_second_client_flight(SSL_HANDSHAKE *hs) {
970 SSL *const ssl = hs->ssl;
David Benjamin02e62562017-12-18 18:04:01 -0500971 if (ssl->s3->early_data_accepted) {
David Benjamin1e859052020-02-09 16:04:58 -0500972 if (!tls13_set_traffic_key(ssl, ssl_encryption_early_data, evp_aead_open,
David Benjamin754d4c92020-02-11 16:27:21 -0500973 hs->new_session.get(),
David Benjamine530ea32019-08-16 19:28:00 -0400974 hs->early_traffic_secret())) {
Steven Valdez2d850622017-01-11 11:34:52 -0500975 return ssl_hs_error;
976 }
David Benjaminfd45ee72017-08-31 14:49:09 -0400977 hs->can_early_write = true;
978 hs->can_early_read = true;
979 hs->in_early_data = true;
Steven Valdez2d850622017-01-11 11:34:52 -0500980 }
David Benjamind6343572019-08-15 17:29:02 -0400981
David Benjamina1d3bfb2021-06-01 12:12:44 -0400982 // QUIC doesn't use an EndOfEarlyData message (RFC 9001, section 8.3), so we
983 // switch to client_handshake_secret before the early return.
David Benjamind6343572019-08-15 17:29:02 -0400984 if (ssl->quic_method != nullptr) {
985 if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_open,
David Benjamin754d4c92020-02-11 16:27:21 -0500986 hs->new_session.get(),
David Benjamind6343572019-08-15 17:29:02 -0400987 hs->client_handshake_secret())) {
988 return ssl_hs_error;
989 }
David Benjamin71ed9d72021-01-12 18:18:46 -0500990 hs->tls13_state = state13_process_end_of_early_data;
David Benjamind6343572019-08-15 17:29:02 -0400991 return ssl->s3->early_data_accepted ? ssl_hs_early_return : ssl_hs_ok;
992 }
993
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800994 hs->tls13_state = state13_process_end_of_early_data;
David Benjamin02e62562017-12-18 18:04:01 -0500995 return ssl->s3->early_data_accepted ? ssl_hs_read_end_of_early_data
996 : ssl_hs_ok;
Steven Valdez2d850622017-01-11 11:34:52 -0500997}
998
999static enum ssl_hs_wait_t do_process_end_of_early_data(SSL_HANDSHAKE *hs) {
Steven Valdezcd8470f2017-10-11 12:29:36 -04001000 SSL *const ssl = hs->ssl;
David Benjamin71ed9d72021-01-12 18:18:46 -05001001 // In protocols that use EndOfEarlyData, we must consume the extra message and
1002 // switch to client_handshake_secret after the early return.
1003 if (ssl->quic_method == nullptr) {
1004 // If early data was not accepted, the EndOfEarlyData will be in the
1005 // discarded early data.
1006 if (hs->ssl->s3->early_data_accepted) {
1007 SSLMessage msg;
1008 if (!ssl->method->get_message(ssl, &msg)) {
1009 return ssl_hs_read_message;
1010 }
1011 if (!ssl_check_message_type(ssl, msg, SSL3_MT_END_OF_EARLY_DATA)) {
1012 return ssl_hs_error;
1013 }
1014 if (CBS_len(&msg.body) != 0) {
1015 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1016 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1017 return ssl_hs_error;
1018 }
1019 ssl->method->next_message(ssl);
Steven Valdezcd8470f2017-10-11 12:29:36 -04001020 }
David Benjamin71ed9d72021-01-12 18:18:46 -05001021 if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_open,
1022 hs->new_session.get(),
1023 hs->client_handshake_secret())) {
David Benjaminf3503512019-08-20 15:55:24 -04001024 return ssl_hs_error;
1025 }
Steven Valdez2d850622017-01-11 11:34:52 -05001026 }
Steven Valdez51607f12020-08-05 10:46:05 -04001027 hs->tls13_state = state13_read_client_encrypted_extensions;
1028 return ssl_hs_ok;
1029}
1030
1031static enum ssl_hs_wait_t do_read_client_encrypted_extensions(
1032 SSL_HANDSHAKE *hs) {
1033 SSL *const ssl = hs->ssl;
1034 // For now, only one extension uses client EncryptedExtensions. This function
1035 // may be generalized if others use it in the future.
1036 if (hs->new_session->has_application_settings &&
1037 !ssl->s3->early_data_accepted) {
1038 SSLMessage msg;
1039 if (!ssl->method->get_message(ssl, &msg)) {
1040 return ssl_hs_read_message;
1041 }
1042 if (!ssl_check_message_type(ssl, msg, SSL3_MT_ENCRYPTED_EXTENSIONS)) {
1043 return ssl_hs_error;
1044 }
1045
1046 CBS body = msg.body, extensions;
1047 if (!CBS_get_u16_length_prefixed(&body, &extensions) ||
1048 CBS_len(&body) != 0) {
1049 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1050 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1051 return ssl_hs_error;
1052 }
1053
Victor Tan558960d2023-06-23 15:04:33 +00001054 uint16_t extension_type = TLSEXT_TYPE_application_settings_old;
1055 if (hs->config->alps_use_new_codepoint) {
1056 extension_type = TLSEXT_TYPE_application_settings;
1057 }
1058 SSLExtension application_settings(extension_type);
Steven Valdez51607f12020-08-05 10:46:05 -04001059 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamina75027b2021-07-20 15:07:22 -04001060 if (!ssl_parse_extensions(&extensions, &alert, {&application_settings},
Steven Valdez51607f12020-08-05 10:46:05 -04001061 /*ignore_unknown=*/false)) {
1062 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
1063 return ssl_hs_error;
1064 }
1065
David Benjamina75027b2021-07-20 15:07:22 -04001066 if (!application_settings.present) {
Steven Valdez51607f12020-08-05 10:46:05 -04001067 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
1068 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION);
1069 return ssl_hs_error;
1070 }
1071
1072 // Note that, if 0-RTT was accepted, these values will already have been
1073 // initialized earlier.
1074 if (!hs->new_session->peer_application_settings.CopyFrom(
David Benjamina75027b2021-07-20 15:07:22 -04001075 application_settings.data) ||
Steven Valdez51607f12020-08-05 10:46:05 -04001076 !ssl_hash_message(hs, msg)) {
1077 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1078 return ssl_hs_error;
1079 }
1080
1081 ssl->method->next_message(ssl);
1082 }
1083
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001084 hs->tls13_state = state13_read_client_certificate;
David Benjamin7934f082017-08-01 16:32:25 -04001085 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -04001086}
1087
David Benjamin7934f082017-08-01 16:32:25 -04001088static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001089 SSL *const ssl = hs->ssl;
1090 if (!hs->cert_request) {
David Benjaminf3503512019-08-20 15:55:24 -04001091 if (!ssl->s3->session_reused) {
1092 // OpenSSL returns X509_V_OK when no certificates are requested. This is
1093 // classed by them as a bug, but it's assumed by at least NGINX. (Only do
1094 // this in full handshakes as resumptions should carry over the previous
1095 // |verify_result|, though this is a no-op because servers do not
1096 // implement the client's odd soft-fail mode.)
1097 hs->new_session->verify_result = X509_V_OK;
1098 }
Adam Langley37646832016-08-01 16:16:46 -07001099
David Benjaminc11ea9422017-08-29 16:33:21 -04001100 // Skip this state.
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001101 hs->tls13_state = state13_read_channel_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04001102 return ssl_hs_ok;
1103 }
1104
David Benjamin12f58782018-08-28 17:06:31 -05001105 const bool allow_anonymous =
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001106 (hs->config->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) == 0;
David Benjamin7934f082017-08-01 16:32:25 -04001107 SSLMessage msg;
1108 if (!ssl->method->get_message(ssl, &msg)) {
1109 return ssl_hs_read_message;
1110 }
1111 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
1112 !tls13_process_certificate(hs, msg, allow_anonymous) ||
1113 !ssl_hash_message(hs, msg)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001114 return ssl_hs_error;
1115 }
1116
David Benjamin8f94c312017-08-01 17:35:55 -04001117 ssl->method->next_message(ssl);
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001118 hs->tls13_state = state13_read_client_certificate_verify;
David Benjamin7934f082017-08-01 16:32:25 -04001119 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -04001120}
1121
Steven Valdez51607f12020-08-05 10:46:05 -04001122static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001123 SSL *const ssl = hs->ssl;
David Benjaminbfdd1a92018-06-29 16:26:38 -04001124 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001125 // Skip this state.
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001126 hs->tls13_state = state13_read_channel_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04001127 return ssl_hs_ok;
1128 }
1129
David Benjamin7934f082017-08-01 16:32:25 -04001130 SSLMessage msg;
1131 if (!ssl->method->get_message(ssl, &msg)) {
1132 return ssl_hs_read_message;
1133 }
1134
David Benjamin3a1dd462017-07-11 16:13:10 -04001135 switch (ssl_verify_peer_cert(hs)) {
1136 case ssl_verify_ok:
1137 break;
1138 case ssl_verify_invalid:
1139 return ssl_hs_error;
1140 case ssl_verify_retry:
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001141 hs->tls13_state = state13_read_client_certificate_verify;
David Benjamin3a1dd462017-07-11 16:13:10 -04001142 return ssl_hs_certificate_verify;
1143 }
1144
David Benjamin7934f082017-08-01 16:32:25 -04001145 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY) ||
1146 !tls13_process_certificate_verify(hs, msg) ||
1147 !ssl_hash_message(hs, msg)) {
David Benjamin6929f272016-11-16 19:10:08 +09001148 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -04001149 }
1150
David Benjamin8f94c312017-08-01 17:35:55 -04001151 ssl->method->next_message(ssl);
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001152 hs->tls13_state = state13_read_channel_id;
David Benjamin7934f082017-08-01 16:32:25 -04001153 return ssl_hs_ok;
Nick Harper60a85cb2016-09-23 16:25:11 -07001154}
1155
David Benjamin7934f082017-08-01 16:32:25 -04001156static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
David Benjamin8f94c312017-08-01 17:35:55 -04001157 SSL *const ssl = hs->ssl;
David Benjamin8acec002021-05-19 13:03:34 -04001158 if (!hs->channel_id_negotiated) {
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001159 hs->tls13_state = state13_read_client_finished;
Nick Harper60a85cb2016-09-23 16:25:11 -07001160 return ssl_hs_ok;
1161 }
1162
David Benjamin7934f082017-08-01 16:32:25 -04001163 SSLMessage msg;
1164 if (!ssl->method->get_message(ssl, &msg)) {
1165 return ssl_hs_read_message;
1166 }
1167 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
1168 !tls1_verify_channel_id(hs, msg) ||
1169 !ssl_hash_message(hs, msg)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07001170 return ssl_hs_error;
1171 }
1172
David Benjamin8f94c312017-08-01 17:35:55 -04001173 ssl->method->next_message(ssl);
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001174 hs->tls13_state = state13_read_client_finished;
David Benjamin7934f082017-08-01 16:32:25 -04001175 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -04001176}
1177
David Benjamin7934f082017-08-01 16:32:25 -04001178static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001179 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -04001180 SSLMessage msg;
1181 if (!ssl->method->get_message(ssl, &msg)) {
1182 return ssl_hs_read_message;
1183 }
1184 if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001185 // If early data was accepted, we've already computed the client Finished
1186 // and derived the resumption secret.
David Benjamin02e62562017-12-18 18:04:01 -05001187 !tls13_process_finished(hs, msg, ssl->s3->early_data_accepted) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001188 // evp_aead_seal keys have already been switched.
David Benjamine530ea32019-08-16 19:28:00 -04001189 !tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_open,
David Benjamin754d4c92020-02-11 16:27:21 -05001190 hs->new_session.get(),
David Benjamine530ea32019-08-16 19:28:00 -04001191 hs->client_traffic_secret_0())) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001192 return ssl_hs_error;
1193 }
1194
David Benjamin02e62562017-12-18 18:04:01 -05001195 if (!ssl->s3->early_data_accepted) {
David Benjamin7934f082017-08-01 16:32:25 -04001196 if (!ssl_hash_message(hs, msg) ||
David Benjamin794cc592017-03-25 22:24:23 -05001197 !tls13_derive_resumption_secret(hs)) {
1198 return ssl_hs_error;
1199 }
1200
David Benjaminc11ea9422017-08-29 16:33:21 -04001201 // We send post-handshake tickets as part of the handshake in 1-RTT.
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001202 hs->tls13_state = state13_send_new_session_ticket;
David Benjamin8f94c312017-08-01 17:35:55 -04001203 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001204 // We already sent half-RTT tickets.
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001205 hs->tls13_state = state13_done;
David Benjamin794cc592017-03-25 22:24:23 -05001206 }
1207
David Benjamin8f94c312017-08-01 17:35:55 -04001208 ssl->method->next_message(ssl);
Steven Valdez143e8b32016-07-11 13:19:03 -04001209 return ssl_hs_ok;
1210}
1211
David Benjaminc3c88822016-11-14 10:32:04 +09001212static enum ssl_hs_wait_t do_send_new_session_ticket(SSL_HANDSHAKE *hs) {
David Benjamin0cbb1af2018-07-17 21:26:05 -04001213 bool sent_tickets;
1214 if (!add_new_session_tickets(hs, &sent_tickets)) {
David Benjamin794cc592017-03-25 22:24:23 -05001215 return ssl_hs_error;
Steven Valdez08b65f42016-12-07 15:29:45 -05001216 }
1217
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001218 hs->tls13_state = state13_done;
Steven Valdez777a2392019-02-21 11:30:47 -05001219 // In TLS 1.3, the NewSessionTicket isn't flushed until the server performs a
1220 // write, to prevent a non-reading client from causing the server to hang in
1221 // the case of a small server write buffer. Consumers which don't write data
1222 // to the client will need to do a zero-byte write if they wish to flush the
1223 // tickets.
Goutam Tamvada49de1fc2019-10-04 16:58:29 -04001224 if (hs->ssl->quic_method != nullptr && sent_tickets) {
Steven Valdez777a2392019-02-21 11:30:47 -05001225 return ssl_hs_flush;
1226 }
1227 return ssl_hs_ok;
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001228}
1229
David Benjaminc3c88822016-11-14 10:32:04 +09001230enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs) {
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001231 while (hs->tls13_state != state13_done) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001232 enum ssl_hs_wait_t ret = ssl_hs_error;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001233 enum tls13_server_hs_state_t state =
1234 static_cast<enum tls13_server_hs_state_t>(hs->tls13_state);
Steven Valdez143e8b32016-07-11 13:19:03 -04001235 switch (state) {
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001236 case state13_select_parameters:
David Benjaminc3c88822016-11-14 10:32:04 +09001237 ret = do_select_parameters(hs);
David Benjamin25fe85b2016-08-09 20:00:32 -04001238 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001239 case state13_select_session:
David Benjamin707af292017-03-10 17:47:18 -05001240 ret = do_select_session(hs);
1241 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001242 case state13_send_hello_retry_request:
David Benjaminc3c88822016-11-14 10:32:04 +09001243 ret = do_send_hello_retry_request(hs);
Steven Valdez5440fe02016-07-18 12:40:30 -04001244 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001245 case state13_read_second_client_hello:
David Benjamin7934f082017-08-01 16:32:25 -04001246 ret = do_read_second_client_hello(hs);
Steven Valdez5440fe02016-07-18 12:40:30 -04001247 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001248 case state13_send_server_hello:
David Benjaminc3c88822016-11-14 10:32:04 +09001249 ret = do_send_server_hello(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001250 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001251 case state13_send_server_certificate_verify:
David Benjamin44148742017-06-17 13:20:59 -04001252 ret = do_send_server_certificate_verify(hs);
Steven Valdez2d850622017-01-11 11:34:52 -05001253 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001254 case state13_send_server_finished:
David Benjaminc3c88822016-11-14 10:32:04 +09001255 ret = do_send_server_finished(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001256 break;
Matthew Braithwaite093a8232020-01-28 14:06:55 -08001257 case state13_send_half_rtt_ticket:
1258 ret = do_send_half_rtt_ticket(hs);
1259 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001260 case state13_read_second_client_flight:
Steven Valdez2d850622017-01-11 11:34:52 -05001261 ret = do_read_second_client_flight(hs);
1262 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001263 case state13_process_end_of_early_data:
Steven Valdez2d850622017-01-11 11:34:52 -05001264 ret = do_process_end_of_early_data(hs);
1265 break;
Steven Valdez51607f12020-08-05 10:46:05 -04001266 case state13_read_client_encrypted_extensions:
1267 ret = do_read_client_encrypted_extensions(hs);
1268 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001269 case state13_read_client_certificate:
David Benjamin7934f082017-08-01 16:32:25 -04001270 ret = do_read_client_certificate(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001271 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001272 case state13_read_client_certificate_verify:
David Benjamin7934f082017-08-01 16:32:25 -04001273 ret = do_read_client_certificate_verify(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001274 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001275 case state13_read_channel_id:
David Benjamin7934f082017-08-01 16:32:25 -04001276 ret = do_read_channel_id(hs);
Nick Harper60a85cb2016-09-23 16:25:11 -07001277 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001278 case state13_read_client_finished:
David Benjamin7934f082017-08-01 16:32:25 -04001279 ret = do_read_client_finished(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001280 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001281 case state13_send_new_session_ticket:
David Benjaminc3c88822016-11-14 10:32:04 +09001282 ret = do_send_new_session_ticket(hs);
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001283 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001284 case state13_done:
Steven Valdez143e8b32016-07-11 13:19:03 -04001285 ret = ssl_hs_ok;
1286 break;
1287 }
1288
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001289 if (hs->tls13_state != state) {
David Benjaminf60bcfb2017-08-18 15:23:44 -04001290 ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
1291 }
1292
Steven Valdez143e8b32016-07-11 13:19:03 -04001293 if (ret != ssl_hs_ok) {
1294 return ret;
1295 }
1296 }
1297
1298 return ssl_hs_ok;
1299}
David Benjamin86e95b82017-07-18 16:34:25 -04001300
David Benjaminf60bcfb2017-08-18 15:23:44 -04001301const char *tls13_server_handshake_state(SSL_HANDSHAKE *hs) {
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001302 enum tls13_server_hs_state_t state =
1303 static_cast<enum tls13_server_hs_state_t>(hs->tls13_state);
David Benjaminf60bcfb2017-08-18 15:23:44 -04001304 switch (state) {
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001305 case state13_select_parameters:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001306 return "TLS 1.3 server select_parameters";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001307 case state13_select_session:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001308 return "TLS 1.3 server select_session";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001309 case state13_send_hello_retry_request:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001310 return "TLS 1.3 server send_hello_retry_request";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001311 case state13_read_second_client_hello:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001312 return "TLS 1.3 server read_second_client_hello";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001313 case state13_send_server_hello:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001314 return "TLS 1.3 server send_server_hello";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001315 case state13_send_server_certificate_verify:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001316 return "TLS 1.3 server send_server_certificate_verify";
Matthew Braithwaite093a8232020-01-28 14:06:55 -08001317 case state13_send_half_rtt_ticket:
1318 return "TLS 1.3 server send_half_rtt_ticket";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001319 case state13_send_server_finished:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001320 return "TLS 1.3 server send_server_finished";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001321 case state13_read_second_client_flight:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001322 return "TLS 1.3 server read_second_client_flight";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001323 case state13_process_end_of_early_data:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001324 return "TLS 1.3 server process_end_of_early_data";
Steven Valdez51607f12020-08-05 10:46:05 -04001325 case state13_read_client_encrypted_extensions:
1326 return "TLS 1.3 server read_client_encrypted_extensions";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001327 case state13_read_client_certificate:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001328 return "TLS 1.3 server read_client_certificate";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001329 case state13_read_client_certificate_verify:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001330 return "TLS 1.3 server read_client_certificate_verify";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001331 case state13_read_channel_id:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001332 return "TLS 1.3 server read_channel_id";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001333 case state13_read_client_finished:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001334 return "TLS 1.3 server read_client_finished";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001335 case state13_send_new_session_ticket:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001336 return "TLS 1.3 server send_new_session_ticket";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001337 case state13_done:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001338 return "TLS 1.3 server done";
1339 }
1340
1341 return "TLS 1.3 server unknown";
1342}
1343
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -07001344BSSL_NAMESPACE_END