blob: 6730f8310670c8c6d345426057ad44c0ea966c10 [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>
26#include <openssl/mem.h>
27#include <openssl/rand.h>
28#include <openssl/stack.h>
29
David Benjamin17cf2cb2016-12-13 01:07:13 -050030#include "../crypto/internal.h"
Steven Valdez143e8b32016-07-11 13:19:03 -040031#include "internal.h"
32
33
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070034BSSL_NAMESPACE_BEGIN
David Benjamin86e95b82017-07-18 16:34:25 -040035
Steven Valdez5440fe02016-07-18 12:40:30 -040036static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0};
37
David Benjamin6433a912019-05-04 14:17:08 -050038// Allow a minute of ticket age skew in either direction. This covers
39// transmission delays in ClientHello and NewSessionTicket, as well as
40// drift between client and server clock rate since the ticket was issued.
41// See RFC 8446, section 8.3.
42static const int32_t kMaxTicketAgeSkewSeconds = 60;
43
David Benjamin046bc1f2017-08-31 15:06:42 -040044static int resolve_ecdhe_secret(SSL_HANDSHAKE *hs, bool *out_need_retry,
David Benjamin731058e2016-12-03 23:15:13 -050045 SSL_CLIENT_HELLO *client_hello) {
David Benjamin6e4fc332016-11-17 16:43:08 +090046 SSL *const ssl = hs->ssl;
David Benjamin046bc1f2017-08-31 15:06:42 -040047 *out_need_retry = false;
Steven Valdez5440fe02016-07-18 12:40:30 -040048
David Benjaminc11ea9422017-08-29 16:33:21 -040049 // We only support connections that include an ECDHE key exchange.
Steven Valdez5440fe02016-07-18 12:40:30 -040050 CBS key_share;
David Benjamin731058e2016-12-03 23:15:13 -050051 if (!ssl_client_hello_get_extension(client_hello, &key_share,
Steven Valdez7e5dd252018-01-22 15:20:31 -050052 TLSEXT_TYPE_key_share)) {
Steven Valdez5440fe02016-07-18 12:40:30 -040053 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE);
David Benjamind1e3ce12017-10-06 18:31:15 -040054 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION);
David Benjamin6929f272016-11-16 19:10:08 +090055 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -040056 }
57
David Benjamin74795b32017-08-31 15:13:12 -040058 bool found_key_share;
David Benjamin499742c2017-07-22 12:45:38 -040059 Array<uint8_t> dhe_secret;
David Benjamin7e1f9842016-09-20 19:24:40 -040060 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8baf9632016-11-17 17:11:16 +090061 if (!ssl_ext_key_share_parse_clienthello(hs, &found_key_share, &dhe_secret,
David Benjamin499742c2017-07-22 12:45:38 -040062 &alert, &key_share)) {
David Benjamind1e3ce12017-10-06 18:31:15 -040063 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez5440fe02016-07-18 12:40:30 -040064 return 0;
65 }
66
67 if (!found_key_share) {
David Benjamin046bc1f2017-08-31 15:06:42 -040068 *out_need_retry = true;
Steven Valdez5440fe02016-07-18 12:40:30 -040069 return 0;
70 }
71
David Benjamin79b8b3a2019-08-16 18:58:13 -040072 return tls13_advance_key_schedule(hs, dhe_secret);
Steven Valdez5440fe02016-07-18 12:40:30 -040073}
74
Steven Valdez038da9b2017-07-10 12:57:25 -040075static int ssl_ext_supported_versions_add_serverhello(SSL_HANDSHAKE *hs,
76 CBB *out) {
77 CBB contents;
78 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
79 !CBB_add_u16_length_prefixed(out, &contents) ||
80 !CBB_add_u16(&contents, hs->ssl->version) ||
81 !CBB_flush(out)) {
82 return 0;
83 }
84
85 return 1;
86}
87
David Benjamin34202b92016-11-16 19:07:53 +090088static const SSL_CIPHER *choose_tls13_cipher(
David Benjamin43cc9c62018-12-12 13:06:46 -060089 const SSL *ssl, const SSL_CLIENT_HELLO *client_hello, uint16_t group_id) {
David Benjamin34202b92016-11-16 19:07:53 +090090 CBS cipher_suites;
91 CBS_init(&cipher_suites, client_hello->cipher_suites,
92 client_hello->cipher_suites_len);
93
David Benjamind1e3ce12017-10-06 18:31:15 -040094 const uint16_t version = ssl_protocol_version(ssl);
David Benjamin34202b92016-11-16 19:07:53 +090095
Steven Valdezd6f9c352019-06-25 10:13:18 -040096 return ssl_choose_tls13_cipher(cipher_suites, version, group_id);
David Benjamin34202b92016-11-16 19:07:53 +090097}
98
David Benjamin0cbb1af2018-07-17 21:26:05 -040099static bool add_new_session_tickets(SSL_HANDSHAKE *hs, bool *out_sent_tickets) {
David Benjamin794cc592017-03-25 22:24:23 -0500100 SSL *const ssl = hs->ssl;
David Benjamin0cbb1af2018-07-17 21:26:05 -0400101 if (// If the client doesn't accept resumption with PSK_DHE_KE, don't send a
102 // session ticket.
103 !hs->accept_psk_mode ||
104 // We only implement stateless resumption in TLS 1.3, so skip sending
105 // tickets if disabled.
106 (SSL_get_options(ssl) & SSL_OP_NO_TICKET)) {
107 *out_sent_tickets = false;
108 return true;
109 }
110
David Benjaminc11ea9422017-08-29 16:33:21 -0400111 // TLS 1.3 recommends single-use tickets, so issue multiple tickets in case
112 // the client makes several connections before getting a renewal.
David Benjamin794cc592017-03-25 22:24:23 -0500113 static const int kNumTickets = 2;
114
David Benjaminc11ea9422017-08-29 16:33:21 -0400115 // Rebase the session timestamp so that it is measured from ticket
116 // issuance.
David Benjamin31b0c9b2017-07-20 14:49:15 -0400117 ssl_session_rebase_time(ssl, hs->new_session.get());
David Benjamin794cc592017-03-25 22:24:23 -0500118
119 for (int i = 0; i < kNumTickets; i++) {
Steven Valdezcd8470f2017-10-11 12:29:36 -0400120 UniquePtr<SSL_SESSION> session(
121 SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_INCLUDE_NONAUTH));
122 if (!session) {
David Benjamin0cbb1af2018-07-17 21:26:05 -0400123 return false;
David Benjamin794cc592017-03-25 22:24:23 -0500124 }
David Benjamin794cc592017-03-25 22:24:23 -0500125
Steven Valdezcd8470f2017-10-11 12:29:36 -0400126 if (!RAND_bytes((uint8_t *)&session->ticket_age_add, 4)) {
David Benjamin0cbb1af2018-07-17 21:26:05 -0400127 return false;
Steven Valdezcd8470f2017-10-11 12:29:36 -0400128 }
David Benjamina3a71e92018-06-29 13:24:45 -0400129 session->ticket_age_add_valid = true;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700130 if (ssl->enable_early_data) {
David Benjamind6343572019-08-15 17:29:02 -0400131 // QUIC does not use the max_early_data_size parameter and always sets it
132 // to a fixed value. See draft-ietf-quic-tls-22, section 4.5.
133 session->ticket_max_early_data =
134 ssl->quic_method != nullptr ? 0xffffffff : kMaxEarlyDataAccepted;
Steven Valdezbe165a22017-10-10 11:45:01 -0400135 }
136
Steven Valdezcd8470f2017-10-11 12:29:36 -0400137 static_assert(kNumTickets < 256, "Too many tickets");
138 uint8_t nonce[] = {static_cast<uint8_t>(i)};
139
David Benjamin1386aad2017-07-19 23:57:40 -0400140 ScopedCBB cbb;
Steven Valdezcd8470f2017-10-11 12:29:36 -0400141 CBB body, nonce_cbb, ticket, extensions;
David Benjamin1386aad2017-07-19 23:57:40 -0400142 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin794cc592017-03-25 22:24:23 -0500143 SSL3_MT_NEW_SESSION_TICKET) ||
Steven Valdezcd8470f2017-10-11 12:29:36 -0400144 !CBB_add_u32(&body, session->timeout) ||
145 !CBB_add_u32(&body, session->ticket_age_add) ||
Steven Valdez7e5dd252018-01-22 15:20:31 -0500146 !CBB_add_u8_length_prefixed(&body, &nonce_cbb) ||
147 !CBB_add_bytes(&nonce_cbb, nonce, sizeof(nonce)) ||
David Benjamin794cc592017-03-25 22:24:23 -0500148 !CBB_add_u16_length_prefixed(&body, &ticket) ||
Alessandro Ghedini2cc6f442018-12-11 11:35:17 +0000149 !tls13_derive_session_psk(session.get(), nonce) ||
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700150 !ssl_encrypt_ticket(hs, &ticket, session.get()) ||
David Benjamin794cc592017-03-25 22:24:23 -0500151 !CBB_add_u16_length_prefixed(&body, &extensions)) {
David Benjamin0cbb1af2018-07-17 21:26:05 -0400152 return false;
David Benjamin794cc592017-03-25 22:24:23 -0500153 }
154
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700155 if (ssl->enable_early_data) {
David Benjamina93beba2019-10-15 16:58:21 -0400156 CBB early_data;
Steven Valdez7e5dd252018-01-22 15:20:31 -0500157 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_early_data) ||
David Benjamina93beba2019-10-15 16:58:21 -0400158 !CBB_add_u16_length_prefixed(&extensions, &early_data) ||
159 !CBB_add_u32(&early_data, session->ticket_max_early_data) ||
David Benjamin794cc592017-03-25 22:24:23 -0500160 !CBB_flush(&extensions)) {
David Benjamin0cbb1af2018-07-17 21:26:05 -0400161 return false;
David Benjamin794cc592017-03-25 22:24:23 -0500162 }
163 }
164
David Benjaminc11ea9422017-08-29 16:33:21 -0400165 // Add a fake extension. See draft-davidben-tls-grease-01.
David Benjamin794cc592017-03-25 22:24:23 -0500166 if (!CBB_add_u16(&extensions,
David Benjamina7bc9442018-01-18 10:08:53 -0500167 ssl_get_grease_value(hs, ssl_grease_ticket_extension)) ||
David Benjamin794cc592017-03-25 22:24:23 -0500168 !CBB_add_u16(&extensions, 0 /* empty */)) {
David Benjamin0cbb1af2018-07-17 21:26:05 -0400169 return false;
David Benjamin794cc592017-03-25 22:24:23 -0500170 }
171
David Benjamin1386aad2017-07-19 23:57:40 -0400172 if (!ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin0cbb1af2018-07-17 21:26:05 -0400173 return false;
David Benjamin794cc592017-03-25 22:24:23 -0500174 }
175 }
176
David Benjamin0cbb1af2018-07-17 21:26:05 -0400177 *out_sent_tickets = true;
178 return true;
David Benjamin794cc592017-03-25 22:24:23 -0500179}
180
David Benjaminc3c88822016-11-14 10:32:04 +0900181static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400182 // At this point, most ClientHello extensions have already been processed by
183 // the common handshake logic. Resolve the remaining non-PSK parameters.
David Benjaminc3c88822016-11-14 10:32:04 +0900184 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400185 SSLMessage msg;
186 if (!ssl->method->get_message(ssl, &msg)) {
187 return ssl_hs_read_message;
188 }
David Benjamin731058e2016-12-03 23:15:13 -0500189 SSL_CLIENT_HELLO client_hello;
David Benjamin7934f082017-08-01 16:32:25 -0400190 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
David Benjamin34202b92016-11-16 19:07:53 +0900191 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
David Benjamind1e3ce12017-10-06 18:31:15 -0400192 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin34202b92016-11-16 19:07:53 +0900193 return ssl_hs_error;
194 }
195
Steven Valdez520e1222017-06-13 12:45:25 -0400196 OPENSSL_memcpy(hs->session_id, client_hello.session_id,
197 client_hello.session_id_len);
198 hs->session_id_len = client_hello.session_id_len;
199
David Benjamin43cc9c62018-12-12 13:06:46 -0600200 uint16_t group_id;
201 if (!tls1_get_shared_group(hs, &group_id)) {
202 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
203 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
204 return ssl_hs_error;
205 }
206
David Benjaminc11ea9422017-08-29 16:33:21 -0400207 // Negotiate the cipher suite.
David Benjamin43cc9c62018-12-12 13:06:46 -0600208 hs->new_cipher = choose_tls13_cipher(ssl, &client_hello, group_id);
David Benjamin45738dd2017-02-09 20:01:26 -0500209 if (hs->new_cipher == NULL) {
David Benjaminf01f42a2016-11-16 19:05:33 +0900210 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
David Benjamind1e3ce12017-10-06 18:31:15 -0400211 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
David Benjaminf01f42a2016-11-16 19:05:33 +0900212 return ssl_hs_error;
213 }
214
David Benjaminc11ea9422017-08-29 16:33:21 -0400215 // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
216 // deferred. Complete it now.
David Benjamin707af292017-03-10 17:47:18 -0500217 uint8_t alert = SSL_AD_DECODE_ERROR;
218 if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400219 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin707af292017-03-10 17:47:18 -0500220 return ssl_hs_error;
221 }
222
David Benjaminc11ea9422017-08-29 16:33:21 -0400223 // The PRF hash is now known. Set up the key schedule and hash the
224 // ClientHello.
Steven Valdezcd8470f2017-10-11 12:29:36 -0400225 if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher)) {
226 return ssl_hs_error;
227 }
228
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800229 hs->tls13_state = state13_select_session;
David Benjamin707af292017-03-10 17:47:18 -0500230 return ssl_hs_ok;
231}
Steven Valdez908ac192017-01-12 13:17:07 -0500232
David Benjamin707af292017-03-10 17:47:18 -0500233static enum ssl_ticket_aead_result_t select_session(
David Benjamin37af90f2017-07-29 01:42:16 -0400234 SSL_HANDSHAKE *hs, uint8_t *out_alert, UniquePtr<SSL_SESSION> *out_session,
David Benjamin64770122019-05-04 11:00:04 -0500235 int32_t *out_ticket_age_skew, bool *out_offered_ticket,
236 const SSLMessage &msg, const SSL_CLIENT_HELLO *client_hello) {
David Benjamin707af292017-03-10 17:47:18 -0500237 SSL *const ssl = hs->ssl;
David Benjamin64770122019-05-04 11:00:04 -0500238 *out_session = nullptr;
David Benjamin707af292017-03-10 17:47:18 -0500239
David Benjamin707af292017-03-10 17:47:18 -0500240 CBS pre_shared_key;
David Benjamin64770122019-05-04 11:00:04 -0500241 *out_offered_ticket = ssl_client_hello_get_extension(
242 client_hello, &pre_shared_key, TLSEXT_TYPE_pre_shared_key);
243 if (!*out_offered_ticket) {
David Benjamin707af292017-03-10 17:47:18 -0500244 return ssl_ticket_aead_ignore_ticket;
245 }
246
David Benjamin707af292017-03-10 17:47:18 -0500247 CBS ticket, binders;
248 uint32_t client_ticket_age;
David Benjamin9806ae02019-08-16 15:32:03 -0400249 if (!ssl_ext_pre_shared_key_parse_clienthello(
250 hs, &ticket, &binders, &client_ticket_age, out_alert, client_hello,
251 &pre_shared_key)) {
David Benjamin707af292017-03-10 17:47:18 -0500252 return ssl_ticket_aead_error;
253 }
254
David Benjamin64770122019-05-04 11:00:04 -0500255 // If the peer did not offer psk_dhe, ignore the resumption.
256 if (!hs->accept_psk_mode) {
257 return ssl_ticket_aead_ignore_ticket;
258 }
259
David Benjaminc11ea9422017-08-29 16:33:21 -0400260 // TLS 1.3 session tickets are renewed separately as part of the
261 // NewSessionTicket.
David Benjaminfd45ee72017-08-31 14:49:09 -0400262 bool unused_renew;
David Benjamin37af90f2017-07-29 01:42:16 -0400263 UniquePtr<SSL_SESSION> session;
David Benjamin707af292017-03-10 17:47:18 -0500264 enum ssl_ticket_aead_result_t ret =
David Benjamin28655672018-07-18 23:23:25 -0400265 ssl_process_ticket(hs, &session, &unused_renew, ticket, {});
David Benjamin707af292017-03-10 17:47:18 -0500266 switch (ret) {
267 case ssl_ticket_aead_success:
268 break;
269 case ssl_ticket_aead_error:
270 *out_alert = SSL_AD_INTERNAL_ERROR;
271 return ret;
272 default:
273 return ret;
274 }
275
David Benjamin37af90f2017-07-29 01:42:16 -0400276 if (!ssl_session_is_resumable(hs, session.get()) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400277 // Historically, some TLS 1.3 tickets were missing ticket_age_add.
David Benjamin707af292017-03-10 17:47:18 -0500278 !session->ticket_age_add_valid) {
David Benjamin707af292017-03-10 17:47:18 -0500279 return ssl_ticket_aead_ignore_ticket;
280 }
281
David Benjaminc11ea9422017-08-29 16:33:21 -0400282 // Recover the client ticket age and convert to seconds.
David Benjamin707af292017-03-10 17:47:18 -0500283 client_ticket_age -= session->ticket_age_add;
284 client_ticket_age /= 1000;
285
286 struct OPENSSL_timeval now;
287 ssl_get_current_time(ssl, &now);
288
David Benjaminc11ea9422017-08-29 16:33:21 -0400289 // Compute the server ticket age in seconds.
David Benjamin707af292017-03-10 17:47:18 -0500290 assert(now.tv_sec >= session->time);
291 uint64_t server_ticket_age = now.tv_sec - session->time;
292
David Benjaminc11ea9422017-08-29 16:33:21 -0400293 // To avoid overflowing |hs->ticket_age_skew|, we will not resume
294 // 68-year-old sessions.
David Benjamin707af292017-03-10 17:47:18 -0500295 if (server_ticket_age > INT32_MAX) {
David Benjamin707af292017-03-10 17:47:18 -0500296 return ssl_ticket_aead_ignore_ticket;
297 }
298
David Benjamin8c98bac2019-08-15 20:40:01 -0400299 *out_ticket_age_skew = static_cast<int32_t>(client_ticket_age) -
300 static_cast<int32_t>(server_ticket_age);
David Benjamin707af292017-03-10 17:47:18 -0500301
David Benjaminc11ea9422017-08-29 16:33:21 -0400302 // Check the PSK binder.
David Benjamin7934f082017-08-01 16:32:25 -0400303 if (!tls13_verify_psk_binder(hs, session.get(), msg, &binders)) {
David Benjamin707af292017-03-10 17:47:18 -0500304 *out_alert = SSL_AD_DECRYPT_ERROR;
305 return ssl_ticket_aead_error;
306 }
307
David Benjamin37af90f2017-07-29 01:42:16 -0400308 *out_session = std::move(session);
David Benjamin707af292017-03-10 17:47:18 -0500309 return ssl_ticket_aead_success;
310}
311
312static enum ssl_hs_wait_t do_select_session(SSL_HANDSHAKE *hs) {
313 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400314 SSLMessage msg;
315 if (!ssl->method->get_message(ssl, &msg)) {
316 return ssl_hs_read_message;
317 }
David Benjamin707af292017-03-10 17:47:18 -0500318 SSL_CLIENT_HELLO client_hello;
David Benjamin7934f082017-08-01 16:32:25 -0400319 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
David Benjamin707af292017-03-10 17:47:18 -0500320 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
David Benjamind1e3ce12017-10-06 18:31:15 -0400321 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin707af292017-03-10 17:47:18 -0500322 return ssl_hs_error;
323 }
324
David Benjamin4eb95cc2016-11-16 17:08:23 +0900325 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin37af90f2017-07-29 01:42:16 -0400326 UniquePtr<SSL_SESSION> session;
David Benjamin64770122019-05-04 11:00:04 -0500327 bool offered_ticket = false;
328 switch (select_session(hs, &alert, &session, &ssl->s3->ticket_age_skew,
329 &offered_ticket, msg, &client_hello)) {
David Benjamin707af292017-03-10 17:47:18 -0500330 case ssl_ticket_aead_ignore_ticket:
David Benjamin37af90f2017-07-29 01:42:16 -0400331 assert(!session);
David Benjamin64770122019-05-04 11:00:04 -0500332 if (!ssl->enable_early_data) {
333 ssl->s3->early_data_reason = ssl_early_data_disabled;
334 } else if (!offered_ticket) {
335 ssl->s3->early_data_reason = ssl_early_data_no_session_offered;
336 } else {
337 ssl->s3->early_data_reason = ssl_early_data_session_not_resumed;
338 }
David Benjamin707af292017-03-10 17:47:18 -0500339 if (!ssl_get_new_session(hs, 1 /* server */)) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400340 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjaminf01f42a2016-11-16 19:05:33 +0900341 return ssl_hs_error;
342 }
David Benjamin707af292017-03-10 17:47:18 -0500343 break;
Steven Valdeza833c352016-11-01 13:39:36 -0400344
David Benjamin707af292017-03-10 17:47:18 -0500345 case ssl_ticket_aead_success:
David Benjaminc11ea9422017-08-29 16:33:21 -0400346 // Carry over authentication information from the previous handshake into
347 // a fresh session.
David Benjamin37af90f2017-07-29 01:42:16 -0400348 hs->new_session =
349 SSL_SESSION_dup(session.get(), SSL_SESSION_DUP_AUTH_ONLY);
David Benjamin6433a912019-05-04 14:17:08 -0500350 if (hs->new_session == nullptr) {
351 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
352 return ssl_hs_error;
353 }
Steven Valdez2d850622017-01-11 11:34:52 -0500354
David Benjamin754d4c92020-02-11 16:27:21 -0500355 // |ssl_session_is_resumable| forbids cross-cipher resumptions even if the
356 // PRF hashes match.
357 assert(hs->new_cipher == session->cipher);
358
David Benjamin64770122019-05-04 11:00:04 -0500359 if (!ssl->enable_early_data) {
360 ssl->s3->early_data_reason = ssl_early_data_disabled;
361 } else if (session->ticket_max_early_data == 0) {
362 ssl->s3->early_data_reason = ssl_early_data_unsupported_for_session;
363 } else if (!hs->early_data_offered) {
364 ssl->s3->early_data_reason = ssl_early_data_peer_declined;
365 } else if (ssl->s3->channel_id_valid) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400366 // Channel ID is incompatible with 0-RTT.
David Benjamin64770122019-05-04 11:00:04 -0500367 ssl->s3->early_data_reason = ssl_early_data_channel_id;
368 } else if (ssl->s3->token_binding_negotiated) {
369 // Token Binding is incompatible with 0-RTT.
370 ssl->s3->early_data_reason = ssl_early_data_token_binding;
371 } else if (MakeConstSpan(ssl->s3->alpn_selected) != session->early_alpn) {
372 // The negotiated ALPN must match the one in the ticket.
373 ssl->s3->early_data_reason = ssl_early_data_alpn_mismatch;
David Benjamin6433a912019-05-04 14:17:08 -0500374 } else if (ssl->s3->ticket_age_skew < -kMaxTicketAgeSkewSeconds ||
375 kMaxTicketAgeSkewSeconds < ssl->s3->ticket_age_skew) {
376 ssl->s3->early_data_reason = ssl_early_data_ticket_age_skew;
David Benjamin64770122019-05-04 11:00:04 -0500377 } else {
378 ssl->s3->early_data_reason = ssl_early_data_accepted;
David Benjamin02e62562017-12-18 18:04:01 -0500379 ssl->s3->early_data_accepted = true;
Steven Valdez2d850622017-01-11 11:34:52 -0500380 }
381
David Benjamin046bc1f2017-08-31 15:06:42 -0400382 ssl->s3->session_reused = true;
David Benjamin707af292017-03-10 17:47:18 -0500383
David Benjaminc11ea9422017-08-29 16:33:21 -0400384 // Resumption incorporates fresh key material, so refresh the timeout.
David Benjamin98472cb2018-05-02 16:05:36 -0400385 ssl_session_renew_timeout(ssl, hs->new_session.get(),
386 ssl->session_ctx->session_psk_dhe_timeout);
David Benjamin707af292017-03-10 17:47:18 -0500387 break;
388
389 case ssl_ticket_aead_error:
David Benjamind1e3ce12017-10-06 18:31:15 -0400390 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin707af292017-03-10 17:47:18 -0500391 return ssl_hs_error;
392
393 case ssl_ticket_aead_retry:
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800394 hs->tls13_state = state13_select_session;
David Benjamin707af292017-03-10 17:47:18 -0500395 return ssl_hs_pending_ticket;
396 }
397
David Benjaminc11ea9422017-08-29 16:33:21 -0400398 // Record connection properties in the new session.
David Benjamin707af292017-03-10 17:47:18 -0500399 hs->new_session->cipher = hs->new_cipher;
400
David Benjaminc11ea9422017-08-29 16:33:21 -0400401 // Store the initial negotiated ALPN in the session.
David Benjaminbfdd1a92018-06-29 16:26:38 -0400402 if (!hs->new_session->early_alpn.CopyFrom(ssl->s3->alpn_selected)) {
403 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
404 return ssl_hs_error;
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500405 }
406
David Benjamin707af292017-03-10 17:47:18 -0500407 if (ssl->ctx->dos_protection_cb != NULL &&
408 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400409 // Connection rejected for DOS reasons.
David Benjamin707af292017-03-10 17:47:18 -0500410 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
David Benjamind1e3ce12017-10-06 18:31:15 -0400411 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin707af292017-03-10 17:47:18 -0500412 return ssl_hs_error;
413 }
414
Steven Valdezcd8470f2017-10-11 12:29:36 -0400415 size_t hash_len = EVP_MD_size(
416 ssl_get_handshake_digest(ssl_protocol_version(ssl), hs->new_cipher));
417
418 // Set up the key schedule and incorporate the PSK into the running secret.
David Benjamin8f820b42016-11-30 11:24:40 -0500419 if (ssl->s3->session_reused) {
David Benjamin79b8b3a2019-08-16 18:58:13 -0400420 if (!tls13_init_key_schedule(
421 hs, MakeConstSpan(hs->new_session->master_key,
422 hs->new_session->master_key_length))) {
David Benjamin8f820b42016-11-30 11:24:40 -0500423 return ssl_hs_error;
424 }
David Benjamin79b8b3a2019-08-16 18:58:13 -0400425 } else if (!tls13_init_key_schedule(hs, MakeConstSpan(kZeroes, hash_len))) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400426 return ssl_hs_error;
427 }
428
David Benjamin9806ae02019-08-16 15:32:03 -0400429 if (!ssl_hash_message(hs, msg)) {
430 return ssl_hs_error;
431 }
432
David Benjamin02e62562017-12-18 18:04:01 -0500433 if (ssl->s3->early_data_accepted) {
David Benjamind6343572019-08-15 17:29:02 -0400434 if (!tls13_derive_early_secret(hs)) {
Steven Valdez2d850622017-01-11 11:34:52 -0500435 return ssl_hs_error;
436 }
437 } else if (hs->early_data_offered) {
David Benjamin046bc1f2017-08-31 15:06:42 -0400438 ssl->s3->skip_early_data = true;
Steven Valdez2d850622017-01-11 11:34:52 -0500439 }
440
David Benjaminc11ea9422017-08-29 16:33:21 -0400441 // Resolve ECDHE and incorporate it into the secret.
David Benjamin046bc1f2017-08-31 15:06:42 -0400442 bool need_retry;
David Benjamin6e4fc332016-11-17 16:43:08 +0900443 if (!resolve_ecdhe_secret(hs, &need_retry, &client_hello)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400444 if (need_retry) {
David Benjamin64770122019-05-04 11:00:04 -0500445 if (ssl->s3->early_data_accepted) {
446 ssl->s3->early_data_reason = ssl_early_data_hello_retry_request;
447 ssl->s3->early_data_accepted = false;
448 }
David Benjamin046bc1f2017-08-31 15:06:42 -0400449 ssl->s3->skip_early_data = true;
David Benjamin8f94c312017-08-01 17:35:55 -0400450 ssl->method->next_message(ssl);
Steven Valdez7e5dd252018-01-22 15:20:31 -0500451 if (!hs->transcript.UpdateForHelloRetryRequest()) {
Steven Valdezcd8470f2017-10-11 12:29:36 -0400452 return ssl_hs_error;
453 }
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800454 hs->tls13_state = state13_send_hello_retry_request;
Steven Valdez5440fe02016-07-18 12:40:30 -0400455 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400456 }
Steven Valdez5440fe02016-07-18 12:40:30 -0400457 return ssl_hs_error;
458 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400459
David Benjamin8f94c312017-08-01 17:35:55 -0400460 ssl->method->next_message(ssl);
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800461 hs->tls13_state = state13_send_server_hello;
Steven Valdez5440fe02016-07-18 12:40:30 -0400462 return ssl_hs_ok;
463}
Steven Valdez143e8b32016-07-11 13:19:03 -0400464
David Benjaminc3c88822016-11-14 10:32:04 +0900465static enum ssl_hs_wait_t do_send_hello_retry_request(SSL_HANDSHAKE *hs) {
466 SSL *const ssl = hs->ssl;
Steven Valdez964b2372017-11-07 17:09:52 -0500467
468
Steven Valdez7e5dd252018-01-22 15:20:31 -0500469 ScopedCBB cbb;
470 CBB body, session_id, extensions;
471 uint16_t group_id;
472 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
473 !CBB_add_u16(&body, TLS1_2_VERSION) ||
474 !CBB_add_bytes(&body, kHelloRetryRequest, SSL3_RANDOM_SIZE) ||
475 !CBB_add_u8_length_prefixed(&body, &session_id) ||
476 !CBB_add_bytes(&session_id, hs->session_id, hs->session_id_len) ||
477 !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
478 !CBB_add_u8(&body, 0 /* no compression */) ||
479 !tls1_get_shared_group(hs, &group_id) ||
480 !CBB_add_u16_length_prefixed(&body, &extensions) ||
481 !CBB_add_u16(&extensions, TLSEXT_TYPE_supported_versions) ||
482 !CBB_add_u16(&extensions, 2 /* length */) ||
483 !CBB_add_u16(&extensions, ssl->version) ||
484 !CBB_add_u16(&extensions, TLSEXT_TYPE_key_share) ||
485 !CBB_add_u16(&extensions, 2 /* length */) ||
486 !CBB_add_u16(&extensions, group_id) ||
487 !ssl_add_message_cbb(ssl, cbb.get())) {
488 return ssl_hs_error;
489 }
Steven Valdez964b2372017-11-07 17:09:52 -0500490
Steven Valdez7e5dd252018-01-22 15:20:31 -0500491 if (!ssl->method->add_change_cipher_spec(ssl)) {
492 return ssl_hs_error;
Steven Valdez5440fe02016-07-18 12:40:30 -0400493 }
494
Kris Kwiatkowskib11902a2019-08-24 11:01:04 +0100495 ssl->s3->used_hello_retry_request = true;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800496 hs->tls13_state = state13_read_second_client_hello;
David Benjamin7934f082017-08-01 16:32:25 -0400497 return ssl_hs_flush;
Steven Valdez5440fe02016-07-18 12:40:30 -0400498}
499
David Benjamin7934f082017-08-01 16:32:25 -0400500static enum ssl_hs_wait_t do_read_second_client_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900501 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400502 SSLMessage msg;
503 if (!ssl->method->get_message(ssl, &msg)) {
504 return ssl_hs_read_message;
505 }
506 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400507 return ssl_hs_error;
508 }
David Benjamin731058e2016-12-03 23:15:13 -0500509 SSL_CLIENT_HELLO client_hello;
David Benjamin7934f082017-08-01 16:32:25 -0400510 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400511 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
David Benjamind1e3ce12017-10-06 18:31:15 -0400512 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez5440fe02016-07-18 12:40:30 -0400513 return ssl_hs_error;
514 }
515
David Benjamin9806ae02019-08-16 15:32:03 -0400516 // We perform all our negotiation based on the first ClientHello (for
517 // consistency with what |select_certificate_cb| observed), which is in the
518 // transcript, so we can ignore most of this second one.
519 //
520 // We do, however, check the second PSK binder. This covers the client key
521 // share, in case we ever send half-RTT data (we currently do not). It is also
522 // a tricky computation, so we enforce the peer handled it correctly.
523 if (ssl->s3->session_reused) {
524 CBS pre_shared_key;
525 if (!ssl_client_hello_get_extension(&client_hello, &pre_shared_key,
526 TLSEXT_TYPE_pre_shared_key)) {
527 OPENSSL_PUT_ERROR(SSL, SSL_R_INCONSISTENT_CLIENT_HELLO);
528 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
529 return ssl_hs_error;
530 }
531
532 CBS ticket, binders;
533 uint32_t client_ticket_age;
534 uint8_t alert = SSL_AD_DECODE_ERROR;
535 if (!ssl_ext_pre_shared_key_parse_clienthello(
536 hs, &ticket, &binders, &client_ticket_age, &alert, &client_hello,
537 &pre_shared_key)) {
538 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
539 return ssl_hs_error;
540 }
541
542 // Note it is important that we do not obtain a new |SSL_SESSION| from
543 // |ticket|. We have already selected parameters based on the first
544 // ClientHello (in the transcript) and must not switch partway through.
545 if (!tls13_verify_psk_binder(hs, hs->new_session.get(), msg, &binders)) {
546 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
547 return ssl_hs_error;
548 }
549 }
550
David Benjamin046bc1f2017-08-31 15:06:42 -0400551 bool need_retry;
David Benjamin6e4fc332016-11-17 16:43:08 +0900552 if (!resolve_ecdhe_secret(hs, &need_retry, &client_hello)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400553 if (need_retry) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400554 // Only send one HelloRetryRequest.
David Benjamind1e3ce12017-10-06 18:31:15 -0400555 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez5440fe02016-07-18 12:40:30 -0400556 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Steven Valdez143e8b32016-07-11 13:19:03 -0400557 }
Steven Valdez5440fe02016-07-18 12:40:30 -0400558 return ssl_hs_error;
559 }
560
David Benjamin7934f082017-08-01 16:32:25 -0400561 if (!ssl_hash_message(hs, msg)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400562 return ssl_hs_error;
563 }
564
David Benjaminf9cc26f2020-02-09 16:49:31 -0500565 // ClientHello should be the end of the flight.
566 if (ssl->method->has_unprocessed_handshake_data(ssl)) {
567 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
568 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA);
569 return ssl_hs_error;
570 }
571
David Benjamin8f94c312017-08-01 17:35:55 -0400572 ssl->method->next_message(ssl);
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800573 hs->tls13_state = state13_send_server_hello;
Steven Valdez143e8b32016-07-11 13:19:03 -0400574 return ssl_hs_ok;
575}
576
David Benjaminc3c88822016-11-14 10:32:04 +0900577static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
578 SSL *const ssl = hs->ssl;
David Benjamin81b7bc32017-01-12 19:44:57 -0500579
David Benjaminc11ea9422017-08-29 16:33:21 -0400580 // Send a ServerHello.
David Benjamin1386aad2017-07-19 23:57:40 -0400581 ScopedCBB cbb;
582 CBB body, extensions, session_id;
583 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
Steven Valdez64cc1212017-12-04 11:15:37 -0500584 !CBB_add_u16(&body, TLS1_2_VERSION) ||
Steven Valdez143e8b32016-07-11 13:19:03 -0400585 !RAND_bytes(ssl->s3->server_random, sizeof(ssl->s3->server_random)) ||
586 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
Steven Valdez64cc1212017-12-04 11:15:37 -0500587 !CBB_add_u8_length_prefixed(&body, &session_id) ||
588 !CBB_add_bytes(&session_id, hs->session_id, hs->session_id_len) ||
David Benjamin45738dd2017-02-09 20:01:26 -0500589 !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
Steven Valdez64cc1212017-12-04 11:15:37 -0500590 !CBB_add_u8(&body, 0) ||
Steven Valdez143e8b32016-07-11 13:19:03 -0400591 !CBB_add_u16_length_prefixed(&body, &extensions) ||
David Benjamin8baf9632016-11-17 17:11:16 +0900592 !ssl_ext_pre_shared_key_add_serverhello(hs, &extensions) ||
Steven Valdez924a3522017-03-02 16:05:03 -0500593 !ssl_ext_key_share_add_serverhello(hs, &extensions) ||
Steven Valdez64cc1212017-12-04 11:15:37 -0500594 !ssl_ext_supported_versions_add_serverhello(hs, &extensions) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400595 !ssl_add_message_cbb(ssl, cbb.get())) {
596 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400597 }
598
Kris Kwiatkowskib11902a2019-08-24 11:01:04 +0100599 if (!ssl->s3->used_hello_retry_request &&
David Benjamin666d16e2017-10-06 18:45:16 -0400600 !ssl->method->add_change_cipher_spec(ssl)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400601 return ssl_hs_error;
Steven Valdez520e1222017-06-13 12:45:25 -0400602 }
603
David Benjaminc11ea9422017-08-29 16:33:21 -0400604 // Derive and enable the handshake traffic secrets.
Steven Valdez4cb84942016-12-16 11:29:28 -0500605 if (!tls13_derive_handshake_secrets(hs) ||
David Benjamine530ea32019-08-16 19:28:00 -0400606 !tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_seal,
David Benjamin754d4c92020-02-11 16:27:21 -0500607 hs->new_session.get(),
David Benjamine530ea32019-08-16 19:28:00 -0400608 hs->server_handshake_secret())) {
David Benjamin1386aad2017-07-19 23:57:40 -0400609 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400610 }
611
David Benjaminc11ea9422017-08-29 16:33:21 -0400612 // Send EncryptedExtensions.
David Benjamin1386aad2017-07-19 23:57:40 -0400613 if (!ssl->method->init_message(ssl, cbb.get(), &body,
Steven Valdez143e8b32016-07-11 13:19:03 -0400614 SSL3_MT_ENCRYPTED_EXTENSIONS) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500615 !ssl_add_serverhello_tlsext(hs, &body) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400616 !ssl_add_message_cbb(ssl, cbb.get())) {
617 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400618 }
619
David Benjaminc3648fa2017-07-01 10:50:56 -0400620 if (!ssl->s3->session_reused) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400621 // Determine whether to request a client certificate.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700622 hs->cert_request = !!(hs->config->verify_mode & SSL_VERIFY_PEER);
David Benjaminc11ea9422017-08-29 16:33:21 -0400623 // Only request a certificate if Channel ID isn't negotiated.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700624 if ((hs->config->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
David Benjamin46853762018-07-03 14:01:26 -0400625 ssl->s3->channel_id_valid) {
David Benjaminfd45ee72017-08-31 14:49:09 -0400626 hs->cert_request = false;
David Benjaminc3648fa2017-07-01 10:50:56 -0400627 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400628 }
629
David Benjaminc11ea9422017-08-29 16:33:21 -0400630 // Send a CertificateRequest, if necessary.
David Benjamin81b7bc32017-01-12 19:44:57 -0500631 if (hs->cert_request) {
Steven Valdez7e5dd252018-01-22 15:20:31 -0500632 CBB cert_request_extensions, sigalg_contents, sigalgs_cbb;
633 if (!ssl->method->init_message(ssl, cbb.get(), &body,
634 SSL3_MT_CERTIFICATE_REQUEST) ||
635 !CBB_add_u8(&body, 0 /* no certificate_request_context. */) ||
636 !CBB_add_u16_length_prefixed(&body, &cert_request_extensions) ||
637 !CBB_add_u16(&cert_request_extensions,
638 TLSEXT_TYPE_signature_algorithms) ||
639 !CBB_add_u16_length_prefixed(&cert_request_extensions,
640 &sigalg_contents) ||
641 !CBB_add_u16_length_prefixed(&sigalg_contents, &sigalgs_cbb) ||
David Benjaminebad5082020-02-03 19:32:19 -0500642 !tls12_add_verify_sigalgs(hs, &sigalgs_cbb)) {
Steven Valdez7e5dd252018-01-22 15:20:31 -0500643 return ssl_hs_error;
644 }
645
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700646 if (ssl_has_client_CAs(hs->config)) {
Steven Valdez7e5dd252018-01-22 15:20:31 -0500647 CBB ca_contents;
648 if (!CBB_add_u16(&cert_request_extensions,
649 TLSEXT_TYPE_certificate_authorities) ||
Steven Valdezcd8470f2017-10-11 12:29:36 -0400650 !CBB_add_u16_length_prefixed(&cert_request_extensions,
Steven Valdez7e5dd252018-01-22 15:20:31 -0500651 &ca_contents) ||
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700652 !ssl_add_client_CA_list(hs, &ca_contents) ||
Steven Valdez7e5dd252018-01-22 15:20:31 -0500653 !CBB_flush(&cert_request_extensions)) {
Steven Valdezcd8470f2017-10-11 12:29:36 -0400654 return ssl_hs_error;
655 }
Steven Valdez7e5dd252018-01-22 15:20:31 -0500656 }
Steven Valdezcd8470f2017-10-11 12:29:36 -0400657
Steven Valdez7e5dd252018-01-22 15:20:31 -0500658 if (!ssl_add_message_cbb(ssl, cbb.get())) {
659 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400660 }
661 }
662
David Benjaminc11ea9422017-08-29 16:33:21 -0400663 // Send the server Certificate message, if necessary.
David Benjamin81b7bc32017-01-12 19:44:57 -0500664 if (!ssl->s3->session_reused) {
Christopher Patton9cde8482018-07-17 11:36:36 -0700665 if (!ssl_has_certificate(hs)) {
David Benjamin81b7bc32017-01-12 19:44:57 -0500666 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
David Benjamin1386aad2017-07-19 23:57:40 -0400667 return ssl_hs_error;
David Benjamin81b7bc32017-01-12 19:44:57 -0500668 }
669
David Benjamin0f24bed2017-01-12 19:46:50 -0500670 if (!tls13_add_certificate(hs)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400671 return ssl_hs_error;
David Benjamin81b7bc32017-01-12 19:44:57 -0500672 }
673
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800674 hs->tls13_state = state13_send_server_certificate_verify;
David Benjamin81b7bc32017-01-12 19:44:57 -0500675 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400676 }
677
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800678 hs->tls13_state = state13_send_server_finished;
David Benjamin25ac2512017-01-12 19:31:28 -0500679 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400680}
681
David Benjamin44148742017-06-17 13:20:59 -0400682static enum ssl_hs_wait_t do_send_server_certificate_verify(SSL_HANDSHAKE *hs) {
683 switch (tls13_add_certificate_verify(hs)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400684 case ssl_private_key_success:
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800685 hs->tls13_state = state13_send_server_finished;
David Benjamin25ac2512017-01-12 19:31:28 -0500686 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400687
688 case ssl_private_key_retry:
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800689 hs->tls13_state = state13_send_server_certificate_verify;
Steven Valdez143e8b32016-07-11 13:19:03 -0400690 return ssl_hs_private_key_operation;
691
692 case ssl_private_key_failure:
693 return ssl_hs_error;
694 }
695
696 assert(0);
697 return ssl_hs_error;
698}
699
David Benjaminc3c88822016-11-14 10:32:04 +0900700static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900701 SSL *const ssl = hs->ssl;
David Benjamin0f24bed2017-01-12 19:46:50 -0500702 if (!tls13_add_finished(hs) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400703 // Update the secret to the master secret and derive traffic keys.
David Benjamine530ea32019-08-16 19:28:00 -0400704 !tls13_advance_key_schedule(
705 hs, MakeConstSpan(kZeroes, hs->transcript.DigestLen())) ||
David Benjamin6e4fc332016-11-17 16:43:08 +0900706 !tls13_derive_application_secrets(hs) ||
David Benjamine530ea32019-08-16 19:28:00 -0400707 !tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_seal,
David Benjamin754d4c92020-02-11 16:27:21 -0500708 hs->new_session.get(),
David Benjamine530ea32019-08-16 19:28:00 -0400709 hs->server_traffic_secret_0())) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400710 return ssl_hs_error;
711 }
712
Matthew Braithwaite093a8232020-01-28 14:06:55 -0800713 hs->tls13_state = state13_send_half_rtt_ticket;
David Benjamin0c306492020-02-09 16:28:52 -0500714 return hs->handback ? ssl_hs_handback : ssl_hs_ok;
Matthew Braithwaite093a8232020-01-28 14:06:55 -0800715}
716
717static enum ssl_hs_wait_t do_send_half_rtt_ticket(SSL_HANDSHAKE *hs) {
718 SSL *const ssl = hs->ssl;
719
David Benjamin02e62562017-12-18 18:04:01 -0500720 if (ssl->s3->early_data_accepted) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400721 // If accepting 0-RTT, we send tickets half-RTT. This gets the tickets on
722 // the wire sooner and also avoids triggering a write on |SSL_read| when
723 // processing the client Finished. This requires computing the client
David Benjamina130ce02018-08-14 22:26:39 -0500724 // Finished early. See RFC 8446, section 4.6.1.
Steven Valdez7e5dd252018-01-22 15:20:31 -0500725 static const uint8_t kEndOfEarlyData[4] = {SSL3_MT_END_OF_EARLY_DATA, 0,
726 0, 0};
David Benjamind6343572019-08-15 17:29:02 -0400727 if (ssl->quic_method == nullptr &&
728 !hs->transcript.Update(kEndOfEarlyData)) {
Steven Valdez7e5dd252018-01-22 15:20:31 -0500729 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
730 return ssl_hs_error;
Steven Valdezcd8470f2017-10-11 12:29:36 -0400731 }
732
David Benjamin794cc592017-03-25 22:24:23 -0500733 size_t finished_len;
David Benjamine530ea32019-08-16 19:28:00 -0400734 if (!tls13_finished_mac(hs, hs->expected_client_finished().data(),
735 &finished_len, false /* client */)) {
David Benjamin794cc592017-03-25 22:24:23 -0500736 return ssl_hs_error;
737 }
738
David Benjamine530ea32019-08-16 19:28:00 -0400739 if (finished_len != hs->expected_client_finished().size()) {
David Benjamin794cc592017-03-25 22:24:23 -0500740 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
741 return ssl_hs_error;
742 }
743
David Benjaminc11ea9422017-08-29 16:33:21 -0400744 // Feed the predicted Finished into the transcript. This allows us to derive
745 // the resumption secret early and send half-RTT tickets.
746 //
747 // TODO(davidben): This will need to be updated for DTLS 1.3.
David Benjamin794cc592017-03-25 22:24:23 -0500748 assert(!SSL_is_dtls(hs->ssl));
David Benjamine530ea32019-08-16 19:28:00 -0400749 assert(hs->expected_client_finished().size() <= 0xff);
750 uint8_t header[4] = {
751 SSL3_MT_FINISHED, 0, 0,
752 static_cast<uint8_t>(hs->expected_client_finished().size())};
David Benjamin0cbb1af2018-07-17 21:26:05 -0400753 bool unused_sent_tickets;
David Benjamin75a1f232017-10-11 17:19:19 -0400754 if (!hs->transcript.Update(header) ||
David Benjamine530ea32019-08-16 19:28:00 -0400755 !hs->transcript.Update(hs->expected_client_finished()) ||
David Benjamin8f94c312017-08-01 17:35:55 -0400756 !tls13_derive_resumption_secret(hs) ||
David Benjamin0cbb1af2018-07-17 21:26:05 -0400757 !add_new_session_tickets(hs, &unused_sent_tickets)) {
David Benjamin794cc592017-03-25 22:24:23 -0500758 return ssl_hs_error;
759 }
760 }
761
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800762 hs->tls13_state = state13_read_second_client_flight;
Steven Valdez2d850622017-01-11 11:34:52 -0500763 return ssl_hs_flush;
764}
765
766static enum ssl_hs_wait_t do_read_second_client_flight(SSL_HANDSHAKE *hs) {
767 SSL *const ssl = hs->ssl;
David Benjamin02e62562017-12-18 18:04:01 -0500768 if (ssl->s3->early_data_accepted) {
David Benjamin1e859052020-02-09 16:04:58 -0500769 if (!tls13_set_traffic_key(ssl, ssl_encryption_early_data, evp_aead_open,
David Benjamin754d4c92020-02-11 16:27:21 -0500770 hs->new_session.get(),
David Benjamine530ea32019-08-16 19:28:00 -0400771 hs->early_traffic_secret())) {
Steven Valdez2d850622017-01-11 11:34:52 -0500772 return ssl_hs_error;
773 }
David Benjaminfd45ee72017-08-31 14:49:09 -0400774 hs->can_early_write = true;
775 hs->can_early_read = true;
776 hs->in_early_data = true;
Steven Valdez2d850622017-01-11 11:34:52 -0500777 }
David Benjamind6343572019-08-15 17:29:02 -0400778
779 // QUIC doesn't use an EndOfEarlyData message (draft-ietf-quic-tls-22,
780 // section 8.3), so we switch to client_handshake_secret before the early
781 // return.
782 if (ssl->quic_method != nullptr) {
783 if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_open,
David Benjamin754d4c92020-02-11 16:27:21 -0500784 hs->new_session.get(),
David Benjamind6343572019-08-15 17:29:02 -0400785 hs->client_handshake_secret())) {
786 return ssl_hs_error;
787 }
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800788 hs->tls13_state = state13_read_client_certificate;
David Benjamind6343572019-08-15 17:29:02 -0400789 return ssl->s3->early_data_accepted ? ssl_hs_early_return : ssl_hs_ok;
790 }
791
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800792 hs->tls13_state = state13_process_end_of_early_data;
David Benjamin02e62562017-12-18 18:04:01 -0500793 return ssl->s3->early_data_accepted ? ssl_hs_read_end_of_early_data
794 : ssl_hs_ok;
Steven Valdez2d850622017-01-11 11:34:52 -0500795}
796
797static enum ssl_hs_wait_t do_process_end_of_early_data(SSL_HANDSHAKE *hs) {
Steven Valdezcd8470f2017-10-11 12:29:36 -0400798 SSL *const ssl = hs->ssl;
David Benjaminf3503512019-08-20 15:55:24 -0400799 // If early data was not accepted, the EndOfEarlyData will be in the discarded
800 // early data.
801 if (hs->ssl->s3->early_data_accepted) {
802 SSLMessage msg;
803 if (!ssl->method->get_message(ssl, &msg)) {
804 return ssl_hs_read_message;
Steven Valdezcd8470f2017-10-11 12:29:36 -0400805 }
David Benjaminf3503512019-08-20 15:55:24 -0400806 if (!ssl_check_message_type(ssl, msg, SSL3_MT_END_OF_EARLY_DATA)) {
807 return ssl_hs_error;
808 }
809 if (CBS_len(&msg.body) != 0) {
810 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
811 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
812 return ssl_hs_error;
813 }
814 ssl->method->next_message(ssl);
Steven Valdez520e1222017-06-13 12:45:25 -0400815 }
David Benjamine530ea32019-08-16 19:28:00 -0400816 if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_open,
David Benjamin754d4c92020-02-11 16:27:21 -0500817 hs->new_session.get(),
David Benjamine530ea32019-08-16 19:28:00 -0400818 hs->client_handshake_secret())) {
Steven Valdez2d850622017-01-11 11:34:52 -0500819 return ssl_hs_error;
820 }
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800821 hs->tls13_state = state13_read_client_certificate;
David Benjamin7934f082017-08-01 16:32:25 -0400822 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400823}
824
David Benjamin7934f082017-08-01 16:32:25 -0400825static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900826 SSL *const ssl = hs->ssl;
827 if (!hs->cert_request) {
David Benjaminf3503512019-08-20 15:55:24 -0400828 if (!ssl->s3->session_reused) {
829 // OpenSSL returns X509_V_OK when no certificates are requested. This is
830 // classed by them as a bug, but it's assumed by at least NGINX. (Only do
831 // this in full handshakes as resumptions should carry over the previous
832 // |verify_result|, though this is a no-op because servers do not
833 // implement the client's odd soft-fail mode.)
834 hs->new_session->verify_result = X509_V_OK;
835 }
Adam Langley37646832016-08-01 16:16:46 -0700836
David Benjaminc11ea9422017-08-29 16:33:21 -0400837 // Skip this state.
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800838 hs->tls13_state = state13_read_channel_id;
Steven Valdez143e8b32016-07-11 13:19:03 -0400839 return ssl_hs_ok;
840 }
841
David Benjamin12f58782018-08-28 17:06:31 -0500842 const bool allow_anonymous =
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700843 (hs->config->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) == 0;
David Benjamin7934f082017-08-01 16:32:25 -0400844 SSLMessage msg;
845 if (!ssl->method->get_message(ssl, &msg)) {
846 return ssl_hs_read_message;
847 }
848 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
849 !tls13_process_certificate(hs, msg, allow_anonymous) ||
850 !ssl_hash_message(hs, msg)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400851 return ssl_hs_error;
852 }
853
David Benjamin8f94c312017-08-01 17:35:55 -0400854 ssl->method->next_message(ssl);
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800855 hs->tls13_state = state13_read_client_certificate_verify;
David Benjamin7934f082017-08-01 16:32:25 -0400856 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400857}
858
David Benjamin7934f082017-08-01 16:32:25 -0400859static enum ssl_hs_wait_t do_read_client_certificate_verify(
David Benjaminc3c88822016-11-14 10:32:04 +0900860 SSL_HANDSHAKE *hs) {
861 SSL *const ssl = hs->ssl;
David Benjaminbfdd1a92018-06-29 16:26:38 -0400862 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400863 // Skip this state.
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800864 hs->tls13_state = state13_read_channel_id;
Steven Valdez143e8b32016-07-11 13:19:03 -0400865 return ssl_hs_ok;
866 }
867
David Benjamin7934f082017-08-01 16:32:25 -0400868 SSLMessage msg;
869 if (!ssl->method->get_message(ssl, &msg)) {
870 return ssl_hs_read_message;
871 }
872
David Benjamin3a1dd462017-07-11 16:13:10 -0400873 switch (ssl_verify_peer_cert(hs)) {
874 case ssl_verify_ok:
875 break;
876 case ssl_verify_invalid:
877 return ssl_hs_error;
878 case ssl_verify_retry:
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800879 hs->tls13_state = state13_read_client_certificate_verify;
David Benjamin3a1dd462017-07-11 16:13:10 -0400880 return ssl_hs_certificate_verify;
881 }
882
David Benjamin7934f082017-08-01 16:32:25 -0400883 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY) ||
884 !tls13_process_certificate_verify(hs, msg) ||
885 !ssl_hash_message(hs, msg)) {
David Benjamin6929f272016-11-16 19:10:08 +0900886 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400887 }
888
David Benjamin8f94c312017-08-01 17:35:55 -0400889 ssl->method->next_message(ssl);
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800890 hs->tls13_state = state13_read_channel_id;
David Benjamin7934f082017-08-01 16:32:25 -0400891 return ssl_hs_ok;
Nick Harper60a85cb2016-09-23 16:25:11 -0700892}
893
David Benjamin7934f082017-08-01 16:32:25 -0400894static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
David Benjamin8f94c312017-08-01 17:35:55 -0400895 SSL *const ssl = hs->ssl;
David Benjamin46853762018-07-03 14:01:26 -0400896 if (!ssl->s3->channel_id_valid) {
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800897 hs->tls13_state = state13_read_client_finished;
Nick Harper60a85cb2016-09-23 16:25:11 -0700898 return ssl_hs_ok;
899 }
900
David Benjamin7934f082017-08-01 16:32:25 -0400901 SSLMessage msg;
902 if (!ssl->method->get_message(ssl, &msg)) {
903 return ssl_hs_read_message;
904 }
905 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
906 !tls1_verify_channel_id(hs, msg) ||
907 !ssl_hash_message(hs, msg)) {
Nick Harper60a85cb2016-09-23 16:25:11 -0700908 return ssl_hs_error;
909 }
910
David Benjamin8f94c312017-08-01 17:35:55 -0400911 ssl->method->next_message(ssl);
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800912 hs->tls13_state = state13_read_client_finished;
David Benjamin7934f082017-08-01 16:32:25 -0400913 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400914}
915
David Benjamin7934f082017-08-01 16:32:25 -0400916static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900917 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400918 SSLMessage msg;
919 if (!ssl->method->get_message(ssl, &msg)) {
920 return ssl_hs_read_message;
921 }
922 if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400923 // If early data was accepted, we've already computed the client Finished
924 // and derived the resumption secret.
David Benjamin02e62562017-12-18 18:04:01 -0500925 !tls13_process_finished(hs, msg, ssl->s3->early_data_accepted) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400926 // evp_aead_seal keys have already been switched.
David Benjamine530ea32019-08-16 19:28:00 -0400927 !tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_open,
David Benjamin754d4c92020-02-11 16:27:21 -0500928 hs->new_session.get(),
David Benjamine530ea32019-08-16 19:28:00 -0400929 hs->client_traffic_secret_0())) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400930 return ssl_hs_error;
931 }
932
David Benjamin02e62562017-12-18 18:04:01 -0500933 if (!ssl->s3->early_data_accepted) {
David Benjamin7934f082017-08-01 16:32:25 -0400934 if (!ssl_hash_message(hs, msg) ||
David Benjamin794cc592017-03-25 22:24:23 -0500935 !tls13_derive_resumption_secret(hs)) {
936 return ssl_hs_error;
937 }
938
David Benjaminc11ea9422017-08-29 16:33:21 -0400939 // We send post-handshake tickets as part of the handshake in 1-RTT.
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800940 hs->tls13_state = state13_send_new_session_ticket;
David Benjamin8f94c312017-08-01 17:35:55 -0400941 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -0400942 // We already sent half-RTT tickets.
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800943 hs->tls13_state = state13_done;
David Benjamin794cc592017-03-25 22:24:23 -0500944 }
945
David Benjamin8f94c312017-08-01 17:35:55 -0400946 ssl->method->next_message(ssl);
Steven Valdez143e8b32016-07-11 13:19:03 -0400947 return ssl_hs_ok;
948}
949
David Benjaminc3c88822016-11-14 10:32:04 +0900950static enum ssl_hs_wait_t do_send_new_session_ticket(SSL_HANDSHAKE *hs) {
David Benjamin0cbb1af2018-07-17 21:26:05 -0400951 bool sent_tickets;
952 if (!add_new_session_tickets(hs, &sent_tickets)) {
David Benjamin794cc592017-03-25 22:24:23 -0500953 return ssl_hs_error;
Steven Valdez08b65f42016-12-07 15:29:45 -0500954 }
955
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800956 hs->tls13_state = state13_done;
Steven Valdez777a2392019-02-21 11:30:47 -0500957 // In TLS 1.3, the NewSessionTicket isn't flushed until the server performs a
958 // write, to prevent a non-reading client from causing the server to hang in
959 // the case of a small server write buffer. Consumers which don't write data
960 // to the client will need to do a zero-byte write if they wish to flush the
961 // tickets.
Goutam Tamvada49de1fc2019-10-04 16:58:29 -0400962 if (hs->ssl->quic_method != nullptr && sent_tickets) {
Steven Valdez777a2392019-02-21 11:30:47 -0500963 return ssl_hs_flush;
964 }
965 return ssl_hs_ok;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400966}
967
David Benjaminc3c88822016-11-14 10:32:04 +0900968enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs) {
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800969 while (hs->tls13_state != state13_done) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400970 enum ssl_hs_wait_t ret = ssl_hs_error;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800971 enum tls13_server_hs_state_t state =
972 static_cast<enum tls13_server_hs_state_t>(hs->tls13_state);
Steven Valdez143e8b32016-07-11 13:19:03 -0400973 switch (state) {
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800974 case state13_select_parameters:
David Benjaminc3c88822016-11-14 10:32:04 +0900975 ret = do_select_parameters(hs);
David Benjamin25fe85b2016-08-09 20:00:32 -0400976 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800977 case state13_select_session:
David Benjamin707af292017-03-10 17:47:18 -0500978 ret = do_select_session(hs);
979 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800980 case state13_send_hello_retry_request:
David Benjaminc3c88822016-11-14 10:32:04 +0900981 ret = do_send_hello_retry_request(hs);
Steven Valdez5440fe02016-07-18 12:40:30 -0400982 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800983 case state13_read_second_client_hello:
David Benjamin7934f082017-08-01 16:32:25 -0400984 ret = do_read_second_client_hello(hs);
Steven Valdez5440fe02016-07-18 12:40:30 -0400985 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800986 case state13_send_server_hello:
David Benjaminc3c88822016-11-14 10:32:04 +0900987 ret = do_send_server_hello(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400988 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800989 case state13_send_server_certificate_verify:
David Benjamin44148742017-06-17 13:20:59 -0400990 ret = do_send_server_certificate_verify(hs);
Steven Valdez2d850622017-01-11 11:34:52 -0500991 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800992 case state13_send_server_finished:
David Benjaminc3c88822016-11-14 10:32:04 +0900993 ret = do_send_server_finished(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400994 break;
Matthew Braithwaite093a8232020-01-28 14:06:55 -0800995 case state13_send_half_rtt_ticket:
996 ret = do_send_half_rtt_ticket(hs);
997 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -0800998 case state13_read_second_client_flight:
Steven Valdez2d850622017-01-11 11:34:52 -0500999 ret = do_read_second_client_flight(hs);
1000 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001001 case state13_process_end_of_early_data:
Steven Valdez2d850622017-01-11 11:34:52 -05001002 ret = do_process_end_of_early_data(hs);
1003 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001004 case state13_read_client_certificate:
David Benjamin7934f082017-08-01 16:32:25 -04001005 ret = do_read_client_certificate(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001006 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001007 case state13_read_client_certificate_verify:
David Benjamin7934f082017-08-01 16:32:25 -04001008 ret = do_read_client_certificate_verify(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001009 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001010 case state13_read_channel_id:
David Benjamin7934f082017-08-01 16:32:25 -04001011 ret = do_read_channel_id(hs);
Nick Harper60a85cb2016-09-23 16:25:11 -07001012 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001013 case state13_read_client_finished:
David Benjamin7934f082017-08-01 16:32:25 -04001014 ret = do_read_client_finished(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001015 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001016 case state13_send_new_session_ticket:
David Benjaminc3c88822016-11-14 10:32:04 +09001017 ret = do_send_new_session_ticket(hs);
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001018 break;
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001019 case state13_done:
Steven Valdez143e8b32016-07-11 13:19:03 -04001020 ret = ssl_hs_ok;
1021 break;
1022 }
1023
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001024 if (hs->tls13_state != state) {
David Benjaminf60bcfb2017-08-18 15:23:44 -04001025 ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
1026 }
1027
Steven Valdez143e8b32016-07-11 13:19:03 -04001028 if (ret != ssl_hs_ok) {
1029 return ret;
1030 }
1031 }
1032
1033 return ssl_hs_ok;
1034}
David Benjamin86e95b82017-07-18 16:34:25 -04001035
David Benjaminf60bcfb2017-08-18 15:23:44 -04001036const char *tls13_server_handshake_state(SSL_HANDSHAKE *hs) {
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001037 enum tls13_server_hs_state_t state =
1038 static_cast<enum tls13_server_hs_state_t>(hs->tls13_state);
David Benjaminf60bcfb2017-08-18 15:23:44 -04001039 switch (state) {
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001040 case state13_select_parameters:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001041 return "TLS 1.3 server select_parameters";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001042 case state13_select_session:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001043 return "TLS 1.3 server select_session";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001044 case state13_send_hello_retry_request:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001045 return "TLS 1.3 server send_hello_retry_request";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001046 case state13_read_second_client_hello:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001047 return "TLS 1.3 server read_second_client_hello";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001048 case state13_send_server_hello:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001049 return "TLS 1.3 server send_server_hello";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001050 case state13_send_server_certificate_verify:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001051 return "TLS 1.3 server send_server_certificate_verify";
Matthew Braithwaite093a8232020-01-28 14:06:55 -08001052 case state13_send_half_rtt_ticket:
1053 return "TLS 1.3 server send_half_rtt_ticket";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001054 case state13_send_server_finished:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001055 return "TLS 1.3 server send_server_finished";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001056 case state13_read_second_client_flight:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001057 return "TLS 1.3 server read_second_client_flight";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001058 case state13_process_end_of_early_data:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001059 return "TLS 1.3 server process_end_of_early_data";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001060 case state13_read_client_certificate:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001061 return "TLS 1.3 server read_client_certificate";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001062 case state13_read_client_certificate_verify:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001063 return "TLS 1.3 server read_client_certificate_verify";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001064 case state13_read_channel_id:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001065 return "TLS 1.3 server read_channel_id";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001066 case state13_read_client_finished:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001067 return "TLS 1.3 server read_client_finished";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001068 case state13_send_new_session_ticket:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001069 return "TLS 1.3 server send_new_session_ticket";
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001070 case state13_done:
David Benjaminf60bcfb2017-08-18 15:23:44 -04001071 return "TLS 1.3 server done";
1072 }
1073
1074 return "TLS 1.3 server unknown";
1075}
1076
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -07001077BSSL_NAMESPACE_END