blob: 969f2eca234927e3498b3b04a1f6a63566e5bc2d [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
David Benjaminc11ea9422017-08-29 16:33:21 -040015// Per C99, various stdint.h macros are unavailable in C++ unless some macros
16// are defined. C++11 overruled this decision, but older Android NDKs still
17// require it.
David Benjamind304a2f2017-07-12 23:00:28 -040018#if !defined(__STDC_LIMIT_MACROS)
19#define __STDC_LIMIT_MACROS
20#endif
21
Steven Valdez143e8b32016-07-11 13:19:03 -040022#include <openssl/ssl.h>
23
24#include <assert.h>
25#include <string.h>
26
David Benjaminabbbee12016-10-31 19:20:42 -040027#include <openssl/aead.h>
Steven Valdez143e8b32016-07-11 13:19:03 -040028#include <openssl/bytestring.h>
29#include <openssl/digest.h>
30#include <openssl/err.h>
31#include <openssl/mem.h>
32#include <openssl/rand.h>
33#include <openssl/stack.h>
34
David Benjamin17cf2cb2016-12-13 01:07:13 -050035#include "../crypto/internal.h"
Steven Valdez143e8b32016-07-11 13:19:03 -040036#include "internal.h"
37
38
David Benjamin86e95b82017-07-18 16:34:25 -040039namespace bssl {
40
Steven Valdez143e8b32016-07-11 13:19:03 -040041enum server_hs_state_t {
David Benjamindaa05392017-02-02 23:33:21 -050042 state_select_parameters = 0,
David Benjamin707af292017-03-10 17:47:18 -050043 state_select_session,
Steven Valdez5440fe02016-07-18 12:40:30 -040044 state_send_hello_retry_request,
David Benjamin7934f082017-08-01 16:32:25 -040045 state_read_second_client_hello,
Steven Valdez143e8b32016-07-11 13:19:03 -040046 state_send_server_hello,
Steven Valdez143e8b32016-07-11 13:19:03 -040047 state_send_server_certificate_verify,
Steven Valdez143e8b32016-07-11 13:19:03 -040048 state_send_server_finished,
Steven Valdez2d850622017-01-11 11:34:52 -050049 state_read_second_client_flight,
Steven Valdez520e1222017-06-13 12:45:25 -040050 state_process_change_cipher_spec,
Steven Valdez2d850622017-01-11 11:34:52 -050051 state_process_end_of_early_data,
David Benjamin7934f082017-08-01 16:32:25 -040052 state_read_client_certificate,
53 state_read_client_certificate_verify,
54 state_read_channel_id,
55 state_read_client_finished,
Steven Valdez1e6f11a2016-07-27 11:10:52 -040056 state_send_new_session_ticket,
Steven Valdez143e8b32016-07-11 13:19:03 -040057 state_done,
58};
59
Steven Valdez5440fe02016-07-18 12:40:30 -040060static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0};
61
David Benjamin6e4fc332016-11-17 16:43:08 +090062static int resolve_ecdhe_secret(SSL_HANDSHAKE *hs, int *out_need_retry,
David Benjamin731058e2016-12-03 23:15:13 -050063 SSL_CLIENT_HELLO *client_hello) {
David Benjamin6e4fc332016-11-17 16:43:08 +090064 SSL *const ssl = hs->ssl;
Steven Valdez5440fe02016-07-18 12:40:30 -040065 *out_need_retry = 0;
Steven Valdez5440fe02016-07-18 12:40:30 -040066
David Benjaminc11ea9422017-08-29 16:33:21 -040067 // We only support connections that include an ECDHE key exchange.
Steven Valdez5440fe02016-07-18 12:40:30 -040068 CBS key_share;
David Benjamin731058e2016-12-03 23:15:13 -050069 if (!ssl_client_hello_get_extension(client_hello, &key_share,
70 TLSEXT_TYPE_key_share)) {
Steven Valdez5440fe02016-07-18 12:40:30 -040071 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE);
72 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION);
David Benjamin6929f272016-11-16 19:10:08 +090073 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -040074 }
75
Steven Valdez5440fe02016-07-18 12:40:30 -040076 int found_key_share;
77 uint8_t *dhe_secret;
78 size_t dhe_secret_len;
David Benjamin7e1f9842016-09-20 19:24:40 -040079 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8baf9632016-11-17 17:11:16 +090080 if (!ssl_ext_key_share_parse_clienthello(hs, &found_key_share, &dhe_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -040081 &dhe_secret_len, &alert,
82 &key_share)) {
Steven Valdez5440fe02016-07-18 12:40:30 -040083 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
84 return 0;
85 }
86
87 if (!found_key_share) {
88 *out_need_retry = 1;
89 return 0;
90 }
91
David Benjamin6e4fc332016-11-17 16:43:08 +090092 int ok = tls13_advance_key_schedule(hs, dhe_secret, dhe_secret_len);
Steven Valdez5440fe02016-07-18 12:40:30 -040093 OPENSSL_free(dhe_secret);
94 return ok;
95}
96
Steven Valdez038da9b2017-07-10 12:57:25 -040097static int ssl_ext_supported_versions_add_serverhello(SSL_HANDSHAKE *hs,
98 CBB *out) {
99 CBB contents;
100 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
101 !CBB_add_u16_length_prefixed(out, &contents) ||
102 !CBB_add_u16(&contents, hs->ssl->version) ||
103 !CBB_flush(out)) {
104 return 0;
105 }
106
107 return 1;
108}
109
David Benjamin34202b92016-11-16 19:07:53 +0900110static const SSL_CIPHER *choose_tls13_cipher(
David Benjamin731058e2016-12-03 23:15:13 -0500111 const SSL *ssl, const SSL_CLIENT_HELLO *client_hello) {
David Benjamin34202b92016-11-16 19:07:53 +0900112 if (client_hello->cipher_suites_len % 2 != 0) {
113 return NULL;
114 }
115
116 CBS cipher_suites;
117 CBS_init(&cipher_suites, client_hello->cipher_suites,
118 client_hello->cipher_suites_len);
119
120 const int aes_is_fine = EVP_has_aes_hardware();
David Benjaminf01f42a2016-11-16 19:05:33 +0900121 const uint16_t version = ssl3_protocol_version(ssl);
David Benjamin34202b92016-11-16 19:07:53 +0900122
123 const SSL_CIPHER *best = NULL;
124 while (CBS_len(&cipher_suites) > 0) {
125 uint16_t cipher_suite;
126 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
127 return NULL;
128 }
129
David Benjaminc11ea9422017-08-29 16:33:21 -0400130 // Limit to TLS 1.3 ciphers we know about.
David Benjamin34202b92016-11-16 19:07:53 +0900131 const SSL_CIPHER *candidate = SSL_get_cipher_by_value(cipher_suite);
David Benjaminf01f42a2016-11-16 19:05:33 +0900132 if (candidate == NULL ||
133 SSL_CIPHER_get_min_version(candidate) > version ||
134 SSL_CIPHER_get_max_version(candidate) < version) {
David Benjamin34202b92016-11-16 19:07:53 +0900135 continue;
136 }
137
David Benjaminc11ea9422017-08-29 16:33:21 -0400138 // TLS 1.3 removes legacy ciphers, so honor the client order, but prefer
139 // ChaCha20 if we do not have AES hardware.
David Benjamin34202b92016-11-16 19:07:53 +0900140 if (aes_is_fine) {
141 return candidate;
142 }
143
144 if (candidate->algorithm_enc == SSL_CHACHA20POLY1305) {
145 return candidate;
146 }
147
148 if (best == NULL) {
149 best = candidate;
150 }
151 }
152
153 return best;
154}
155
David Benjamin794cc592017-03-25 22:24:23 -0500156static int add_new_session_tickets(SSL_HANDSHAKE *hs) {
157 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400158 // TLS 1.3 recommends single-use tickets, so issue multiple tickets in case
159 // the client makes several connections before getting a renewal.
David Benjamin794cc592017-03-25 22:24:23 -0500160 static const int kNumTickets = 2;
161
David Benjaminc11ea9422017-08-29 16:33:21 -0400162 // Rebase the session timestamp so that it is measured from ticket
163 // issuance.
David Benjamin31b0c9b2017-07-20 14:49:15 -0400164 ssl_session_rebase_time(ssl, hs->new_session.get());
David Benjamin794cc592017-03-25 22:24:23 -0500165
166 for (int i = 0; i < kNumTickets; i++) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400167 if (!RAND_bytes((uint8_t *)&hs->new_session->ticket_age_add, 4)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400168 return 0;
David Benjamin794cc592017-03-25 22:24:23 -0500169 }
David Benjamin31b0c9b2017-07-20 14:49:15 -0400170 hs->new_session->ticket_age_add_valid = 1;
David Benjamin794cc592017-03-25 22:24:23 -0500171
David Benjamin1386aad2017-07-19 23:57:40 -0400172 ScopedCBB cbb;
David Benjamin794cc592017-03-25 22:24:23 -0500173 CBB body, ticket, extensions;
David Benjamin1386aad2017-07-19 23:57:40 -0400174 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin794cc592017-03-25 22:24:23 -0500175 SSL3_MT_NEW_SESSION_TICKET) ||
David Benjamin31b0c9b2017-07-20 14:49:15 -0400176 !CBB_add_u32(&body, hs->new_session->timeout) ||
177 !CBB_add_u32(&body, hs->new_session->ticket_age_add) ||
David Benjamin794cc592017-03-25 22:24:23 -0500178 !CBB_add_u16_length_prefixed(&body, &ticket) ||
David Benjamin31b0c9b2017-07-20 14:49:15 -0400179 !ssl_encrypt_ticket(ssl, &ticket, hs->new_session.get()) ||
David Benjamin794cc592017-03-25 22:24:23 -0500180 !CBB_add_u16_length_prefixed(&body, &extensions)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400181 return 0;
David Benjamin794cc592017-03-25 22:24:23 -0500182 }
183
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -0500184 if (ssl->cert->enable_early_data) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400185 hs->new_session->ticket_max_early_data = kMaxEarlyDataAccepted;
David Benjamin794cc592017-03-25 22:24:23 -0500186
187 CBB early_data_info;
188 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_ticket_early_data_info) ||
189 !CBB_add_u16_length_prefixed(&extensions, &early_data_info) ||
David Benjamin31b0c9b2017-07-20 14:49:15 -0400190 !CBB_add_u32(&early_data_info,
191 hs->new_session->ticket_max_early_data) ||
David Benjamin794cc592017-03-25 22:24:23 -0500192 !CBB_flush(&extensions)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400193 return 0;
David Benjamin794cc592017-03-25 22:24:23 -0500194 }
195 }
196
David Benjaminc11ea9422017-08-29 16:33:21 -0400197 // Add a fake extension. See draft-davidben-tls-grease-01.
David Benjamin794cc592017-03-25 22:24:23 -0500198 if (!CBB_add_u16(&extensions,
199 ssl_get_grease_value(ssl, ssl_grease_ticket_extension)) ||
200 !CBB_add_u16(&extensions, 0 /* empty */)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400201 return 0;
David Benjamin794cc592017-03-25 22:24:23 -0500202 }
203
David Benjamin1386aad2017-07-19 23:57:40 -0400204 if (!ssl_add_message_cbb(ssl, cbb.get())) {
205 return 0;
David Benjamin794cc592017-03-25 22:24:23 -0500206 }
207 }
208
209 return 1;
David Benjamin794cc592017-03-25 22:24:23 -0500210}
211
David Benjaminc3c88822016-11-14 10:32:04 +0900212static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400213 // At this point, most ClientHello extensions have already been processed by
214 // the common handshake logic. Resolve the remaining non-PSK parameters.
David Benjaminc3c88822016-11-14 10:32:04 +0900215 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400216 SSLMessage msg;
217 if (!ssl->method->get_message(ssl, &msg)) {
218 return ssl_hs_read_message;
219 }
David Benjamin731058e2016-12-03 23:15:13 -0500220 SSL_CLIENT_HELLO client_hello;
David Benjamin7934f082017-08-01 16:32:25 -0400221 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
David Benjamin34202b92016-11-16 19:07:53 +0900222 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
223 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
224 return ssl_hs_error;
225 }
226
Steven Valdez520e1222017-06-13 12:45:25 -0400227 OPENSSL_memcpy(hs->session_id, client_hello.session_id,
228 client_hello.session_id_len);
229 hs->session_id_len = client_hello.session_id_len;
230
David Benjaminc11ea9422017-08-29 16:33:21 -0400231 // Negotiate the cipher suite.
David Benjamin45738dd2017-02-09 20:01:26 -0500232 hs->new_cipher = choose_tls13_cipher(ssl, &client_hello);
233 if (hs->new_cipher == NULL) {
David Benjaminf01f42a2016-11-16 19:05:33 +0900234 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
235 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
236 return ssl_hs_error;
237 }
238
David Benjaminc11ea9422017-08-29 16:33:21 -0400239 // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
240 // deferred. Complete it now.
David Benjamin707af292017-03-10 17:47:18 -0500241 uint8_t alert = SSL_AD_DECODE_ERROR;
242 if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
243 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
244 return ssl_hs_error;
245 }
246
David Benjaminc11ea9422017-08-29 16:33:21 -0400247 // The PRF hash is now known. Set up the key schedule and hash the
248 // ClientHello.
Steven Valdez908ac192017-01-12 13:17:07 -0500249 if (!tls13_init_key_schedule(hs) ||
David Benjamin7934f082017-08-01 16:32:25 -0400250 !ssl_hash_message(hs, msg)) {
Steven Valdez908ac192017-01-12 13:17:07 -0500251 return ssl_hs_error;
252 }
253
David Benjamin707af292017-03-10 17:47:18 -0500254 hs->tls13_state = state_select_session;
255 return ssl_hs_ok;
256}
Steven Valdez908ac192017-01-12 13:17:07 -0500257
David Benjamin707af292017-03-10 17:47:18 -0500258static enum ssl_ticket_aead_result_t select_session(
David Benjamin37af90f2017-07-29 01:42:16 -0400259 SSL_HANDSHAKE *hs, uint8_t *out_alert, UniquePtr<SSL_SESSION> *out_session,
David Benjamin7934f082017-08-01 16:32:25 -0400260 int32_t *out_ticket_age_skew, const SSLMessage &msg,
261 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin707af292017-03-10 17:47:18 -0500262 SSL *const ssl = hs->ssl;
263 *out_session = NULL;
264
David Benjaminc11ea9422017-08-29 16:33:21 -0400265 // Decode the ticket if we agreed on a PSK key exchange mode.
David Benjamin707af292017-03-10 17:47:18 -0500266 CBS pre_shared_key;
267 if (!hs->accept_psk_mode ||
268 !ssl_client_hello_get_extension(client_hello, &pre_shared_key,
269 TLSEXT_TYPE_pre_shared_key)) {
270 return ssl_ticket_aead_ignore_ticket;
271 }
272
David Benjaminc11ea9422017-08-29 16:33:21 -0400273 // Verify that the pre_shared_key extension is the last extension in
274 // ClientHello.
David Benjamin707af292017-03-10 17:47:18 -0500275 if (CBS_data(&pre_shared_key) + CBS_len(&pre_shared_key) !=
276 client_hello->extensions + client_hello->extensions_len) {
277 OPENSSL_PUT_ERROR(SSL, SSL_R_PRE_SHARED_KEY_MUST_BE_LAST);
278 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
279 return ssl_ticket_aead_error;
280 }
281
282 CBS ticket, binders;
283 uint32_t client_ticket_age;
284 if (!ssl_ext_pre_shared_key_parse_clienthello(hs, &ticket, &binders,
285 &client_ticket_age, out_alert,
286 &pre_shared_key)) {
287 return ssl_ticket_aead_error;
288 }
289
David Benjaminc11ea9422017-08-29 16:33:21 -0400290 // TLS 1.3 session tickets are renewed separately as part of the
291 // NewSessionTicket.
David Benjaminfd45ee72017-08-31 14:49:09 -0400292 bool unused_renew;
David Benjamin37af90f2017-07-29 01:42:16 -0400293 UniquePtr<SSL_SESSION> session;
David Benjamin707af292017-03-10 17:47:18 -0500294 enum ssl_ticket_aead_result_t ret =
295 ssl_process_ticket(ssl, &session, &unused_renew, CBS_data(&ticket),
296 CBS_len(&ticket), NULL, 0);
297 switch (ret) {
298 case ssl_ticket_aead_success:
299 break;
300 case ssl_ticket_aead_error:
301 *out_alert = SSL_AD_INTERNAL_ERROR;
302 return ret;
303 default:
304 return ret;
305 }
306
David Benjamin37af90f2017-07-29 01:42:16 -0400307 if (!ssl_session_is_resumable(hs, session.get()) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400308 // Historically, some TLS 1.3 tickets were missing ticket_age_add.
David Benjamin707af292017-03-10 17:47:18 -0500309 !session->ticket_age_add_valid) {
David Benjamin707af292017-03-10 17:47:18 -0500310 return ssl_ticket_aead_ignore_ticket;
311 }
312
David Benjaminc11ea9422017-08-29 16:33:21 -0400313 // Recover the client ticket age and convert to seconds.
David Benjamin707af292017-03-10 17:47:18 -0500314 client_ticket_age -= session->ticket_age_add;
315 client_ticket_age /= 1000;
316
317 struct OPENSSL_timeval now;
318 ssl_get_current_time(ssl, &now);
319
David Benjaminc11ea9422017-08-29 16:33:21 -0400320 // Compute the server ticket age in seconds.
David Benjamin707af292017-03-10 17:47:18 -0500321 assert(now.tv_sec >= session->time);
322 uint64_t server_ticket_age = now.tv_sec - session->time;
323
David Benjaminc11ea9422017-08-29 16:33:21 -0400324 // To avoid overflowing |hs->ticket_age_skew|, we will not resume
325 // 68-year-old sessions.
David Benjamin707af292017-03-10 17:47:18 -0500326 if (server_ticket_age > INT32_MAX) {
David Benjamin707af292017-03-10 17:47:18 -0500327 return ssl_ticket_aead_ignore_ticket;
328 }
329
David Benjaminc11ea9422017-08-29 16:33:21 -0400330 // TODO(davidben,svaldez): Measure this value to decide on tolerance. For
331 // now, accept all values. https://crbug.com/boringssl/113.
David Benjamin707af292017-03-10 17:47:18 -0500332 *out_ticket_age_skew =
333 (int32_t)client_ticket_age - (int32_t)server_ticket_age;
334
David Benjaminc11ea9422017-08-29 16:33:21 -0400335 // Check the PSK binder.
David Benjamin7934f082017-08-01 16:32:25 -0400336 if (!tls13_verify_psk_binder(hs, session.get(), msg, &binders)) {
David Benjamin707af292017-03-10 17:47:18 -0500337 *out_alert = SSL_AD_DECRYPT_ERROR;
338 return ssl_ticket_aead_error;
339 }
340
David Benjamin37af90f2017-07-29 01:42:16 -0400341 *out_session = std::move(session);
David Benjamin707af292017-03-10 17:47:18 -0500342 return ssl_ticket_aead_success;
343}
344
345static enum ssl_hs_wait_t do_select_session(SSL_HANDSHAKE *hs) {
346 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400347 SSLMessage msg;
348 if (!ssl->method->get_message(ssl, &msg)) {
349 return ssl_hs_read_message;
350 }
David Benjamin707af292017-03-10 17:47:18 -0500351 SSL_CLIENT_HELLO client_hello;
David Benjamin7934f082017-08-01 16:32:25 -0400352 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
David Benjamin707af292017-03-10 17:47:18 -0500353 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
354 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
355 return ssl_hs_error;
356 }
357
David Benjamin4eb95cc2016-11-16 17:08:23 +0900358 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin37af90f2017-07-29 01:42:16 -0400359 UniquePtr<SSL_SESSION> session;
David Benjamin7934f082017-08-01 16:32:25 -0400360 switch (select_session(hs, &alert, &session, &ssl->s3->ticket_age_skew, msg,
David Benjamin707af292017-03-10 17:47:18 -0500361 &client_hello)) {
362 case ssl_ticket_aead_ignore_ticket:
David Benjamin37af90f2017-07-29 01:42:16 -0400363 assert(!session);
David Benjamin707af292017-03-10 17:47:18 -0500364 if (!ssl_get_new_session(hs, 1 /* server */)) {
David Benjaminf01f42a2016-11-16 19:05:33 +0900365 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
366 return ssl_hs_error;
367 }
David Benjamin707af292017-03-10 17:47:18 -0500368 break;
Steven Valdeza833c352016-11-01 13:39:36 -0400369
David Benjamin707af292017-03-10 17:47:18 -0500370 case ssl_ticket_aead_success:
David Benjaminc11ea9422017-08-29 16:33:21 -0400371 // Carry over authentication information from the previous handshake into
372 // a fresh session.
David Benjamin37af90f2017-07-29 01:42:16 -0400373 hs->new_session =
374 SSL_SESSION_dup(session.get(), SSL_SESSION_DUP_AUTH_ONLY);
Steven Valdez2d850622017-01-11 11:34:52 -0500375
David Benjaminc11ea9422017-08-29 16:33:21 -0400376 if (// Early data must be acceptable for this ticket.
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -0500377 ssl->cert->enable_early_data &&
Steven Valdez2d850622017-01-11 11:34:52 -0500378 session->ticket_max_early_data != 0 &&
David Benjaminc11ea9422017-08-29 16:33:21 -0400379 // The client must have offered early data.
Steven Valdez2d850622017-01-11 11:34:52 -0500380 hs->early_data_offered &&
David Benjaminc11ea9422017-08-29 16:33:21 -0400381 // Channel ID is incompatible with 0-RTT.
Steven Valdez2a070722017-03-25 20:54:16 -0500382 !ssl->s3->tlsext_channel_id_valid &&
David Benjaminc11ea9422017-08-29 16:33:21 -0400383 // Custom extensions is incompatible with 0-RTT.
Steven Valdezf4ecc842017-08-10 14:02:56 -0400384 hs->custom_extensions.received == 0 &&
David Benjaminc11ea9422017-08-29 16:33:21 -0400385 // The negotiated ALPN must match the one in the ticket.
Steven Valdez2d850622017-01-11 11:34:52 -0500386 ssl->s3->alpn_selected_len == session->early_alpn_len &&
387 OPENSSL_memcmp(ssl->s3->alpn_selected, session->early_alpn,
388 ssl->s3->alpn_selected_len) == 0) {
389 ssl->early_data_accepted = 1;
390 }
391
David Benjamin707af292017-03-10 17:47:18 -0500392 if (hs->new_session == NULL) {
393 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
394 return ssl_hs_error;
395 }
396
397 ssl->s3->session_reused = 1;
398
David Benjaminc11ea9422017-08-29 16:33:21 -0400399 // Resumption incorporates fresh key material, so refresh the timeout.
David Benjamin31b0c9b2017-07-20 14:49:15 -0400400 ssl_session_renew_timeout(ssl, hs->new_session.get(),
David Benjamin707af292017-03-10 17:47:18 -0500401 ssl->session_ctx->session_psk_dhe_timeout);
402 break;
403
404 case ssl_ticket_aead_error:
405 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
406 return ssl_hs_error;
407
408 case ssl_ticket_aead_retry:
409 hs->tls13_state = state_select_session;
410 return ssl_hs_pending_ticket;
411 }
412
David Benjaminc11ea9422017-08-29 16:33:21 -0400413 // Record connection properties in the new session.
David Benjamin707af292017-03-10 17:47:18 -0500414 hs->new_session->cipher = hs->new_cipher;
415
416 if (hs->hostname != NULL) {
417 OPENSSL_free(hs->new_session->tlsext_hostname);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400418 hs->new_session->tlsext_hostname = BUF_strdup(hs->hostname.get());
David Benjamin707af292017-03-10 17:47:18 -0500419 if (hs->new_session->tlsext_hostname == NULL) {
Steven Valdez4aa154e2016-07-29 14:32:55 -0400420 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
421 return ssl_hs_error;
422 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400423 }
424
David Benjaminc11ea9422017-08-29 16:33:21 -0400425 // Store the initial negotiated ALPN in the session.
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500426 if (ssl->s3->alpn_selected != NULL) {
David Benjamind304a2f2017-07-12 23:00:28 -0400427 hs->new_session->early_alpn = (uint8_t *)BUF_memdup(
428 ssl->s3->alpn_selected, ssl->s3->alpn_selected_len);
David Benjamin45738dd2017-02-09 20:01:26 -0500429 if (hs->new_session->early_alpn == NULL) {
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500430 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
431 return ssl_hs_error;
432 }
David Benjamin45738dd2017-02-09 20:01:26 -0500433 hs->new_session->early_alpn_len = ssl->s3->alpn_selected_len;
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500434 }
435
David Benjamin707af292017-03-10 17:47:18 -0500436 if (ssl->ctx->dos_protection_cb != NULL &&
437 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400438 // Connection rejected for DOS reasons.
David Benjamin707af292017-03-10 17:47:18 -0500439 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
440 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
441 return ssl_hs_error;
442 }
443
David Benjaminc11ea9422017-08-29 16:33:21 -0400444 // Incorporate the PSK into the running secret.
David Benjamin8f820b42016-11-30 11:24:40 -0500445 if (ssl->s3->session_reused) {
David Benjamin45738dd2017-02-09 20:01:26 -0500446 if (!tls13_advance_key_schedule(hs, hs->new_session->master_key,
447 hs->new_session->master_key_length)) {
David Benjamin8f820b42016-11-30 11:24:40 -0500448 return ssl_hs_error;
449 }
Steven Valdez908ac192017-01-12 13:17:07 -0500450 } else if (!tls13_advance_key_schedule(hs, kZeroes, hs->hash_len)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400451 return ssl_hs_error;
452 }
453
Steven Valdez2d850622017-01-11 11:34:52 -0500454 if (ssl->early_data_accepted) {
455 if (!tls13_derive_early_secrets(hs)) {
456 return ssl_hs_error;
457 }
458 } else if (hs->early_data_offered) {
459 ssl->s3->skip_early_data = 1;
460 }
461
David Benjaminc11ea9422017-08-29 16:33:21 -0400462 // Resolve ECDHE and incorporate it into the secret.
Steven Valdez5440fe02016-07-18 12:40:30 -0400463 int need_retry;
David Benjamin6e4fc332016-11-17 16:43:08 +0900464 if (!resolve_ecdhe_secret(hs, &need_retry, &client_hello)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400465 if (need_retry) {
Steven Valdez2d850622017-01-11 11:34:52 -0500466 ssl->early_data_accepted = 0;
467 ssl->s3->skip_early_data = 1;
David Benjamin8f94c312017-08-01 17:35:55 -0400468 ssl->method->next_message(ssl);
David Benjamin3977f302016-12-11 13:30:41 -0500469 hs->tls13_state = state_send_hello_retry_request;
Steven Valdez5440fe02016-07-18 12:40:30 -0400470 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400471 }
Steven Valdez5440fe02016-07-18 12:40:30 -0400472 return ssl_hs_error;
473 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400474
David Benjamin8f94c312017-08-01 17:35:55 -0400475 ssl->method->next_message(ssl);
David Benjamin3977f302016-12-11 13:30:41 -0500476 hs->tls13_state = state_send_server_hello;
Steven Valdez5440fe02016-07-18 12:40:30 -0400477 return ssl_hs_ok;
478}
Steven Valdez143e8b32016-07-11 13:19:03 -0400479
David Benjaminc3c88822016-11-14 10:32:04 +0900480static enum ssl_hs_wait_t do_send_hello_retry_request(SSL_HANDSHAKE *hs) {
481 SSL *const ssl = hs->ssl;
David Benjamin1386aad2017-07-19 23:57:40 -0400482 ScopedCBB cbb;
483 CBB body, extensions;
Steven Valdez5440fe02016-07-18 12:40:30 -0400484 uint16_t group_id;
David Benjamin1386aad2017-07-19 23:57:40 -0400485 if (!ssl->method->init_message(ssl, cbb.get(), &body,
Steven Valdez5440fe02016-07-18 12:40:30 -0400486 SSL3_MT_HELLO_RETRY_REQUEST) ||
487 !CBB_add_u16(&body, ssl->version) ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900488 !tls1_get_shared_group(hs, &group_id) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400489 !CBB_add_u16_length_prefixed(&body, &extensions) ||
David Benjamin3baa6e12016-10-07 21:10:38 -0400490 !CBB_add_u16(&extensions, TLSEXT_TYPE_key_share) ||
491 !CBB_add_u16(&extensions, 2 /* length */) ||
492 !CBB_add_u16(&extensions, group_id) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400493 !ssl_add_message_cbb(ssl, cbb.get())) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400494 return ssl_hs_error;
495 }
496
David Benjamin7934f082017-08-01 16:32:25 -0400497 hs->tls13_state = state_read_second_client_hello;
498 return ssl_hs_flush;
Steven Valdez5440fe02016-07-18 12:40:30 -0400499}
500
David Benjamin7934f082017-08-01 16:32:25 -0400501static enum ssl_hs_wait_t do_read_second_client_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900502 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400503 SSLMessage msg;
504 if (!ssl->method->get_message(ssl, &msg)) {
505 return ssl_hs_read_message;
506 }
507 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400508 return ssl_hs_error;
509 }
David Benjamin731058e2016-12-03 23:15:13 -0500510 SSL_CLIENT_HELLO client_hello;
David Benjamin7934f082017-08-01 16:32:25 -0400511 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400512 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
513 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
514 return ssl_hs_error;
515 }
516
517 int need_retry;
David Benjamin6e4fc332016-11-17 16:43:08 +0900518 if (!resolve_ecdhe_secret(hs, &need_retry, &client_hello)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400519 if (need_retry) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400520 // Only send one HelloRetryRequest.
Steven Valdez5440fe02016-07-18 12:40:30 -0400521 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
522 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Steven Valdez143e8b32016-07-11 13:19:03 -0400523 }
Steven Valdez5440fe02016-07-18 12:40:30 -0400524 return ssl_hs_error;
525 }
526
David Benjamin7934f082017-08-01 16:32:25 -0400527 if (!ssl_hash_message(hs, msg)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400528 return ssl_hs_error;
529 }
530
David Benjamin8f94c312017-08-01 17:35:55 -0400531 ssl->method->next_message(ssl);
David Benjamin3977f302016-12-11 13:30:41 -0500532 hs->tls13_state = state_send_server_hello;
Steven Valdez143e8b32016-07-11 13:19:03 -0400533 return ssl_hs_ok;
534}
535
David Benjaminc3c88822016-11-14 10:32:04 +0900536static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
537 SSL *const ssl = hs->ssl;
David Benjamin81b7bc32017-01-12 19:44:57 -0500538
Steven Valdez038da9b2017-07-10 12:57:25 -0400539 uint16_t version = ssl->version;
540 if (ssl->version == TLS1_3_EXPERIMENT_VERSION) {
541 version = TLS1_2_VERSION;
542 }
543
David Benjaminc11ea9422017-08-29 16:33:21 -0400544 // Send a ServerHello.
David Benjamin1386aad2017-07-19 23:57:40 -0400545 ScopedCBB cbb;
546 CBB body, extensions, session_id;
547 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
Steven Valdez038da9b2017-07-10 12:57:25 -0400548 !CBB_add_u16(&body, version) ||
Steven Valdez143e8b32016-07-11 13:19:03 -0400549 !RAND_bytes(ssl->s3->server_random, sizeof(ssl->s3->server_random)) ||
550 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
Steven Valdez520e1222017-06-13 12:45:25 -0400551 (ssl->version == TLS1_3_EXPERIMENT_VERSION &&
552 (!CBB_add_u8_length_prefixed(&body, &session_id) ||
553 !CBB_add_bytes(&session_id, hs->session_id, hs->session_id_len))) ||
David Benjamin45738dd2017-02-09 20:01:26 -0500554 !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
Steven Valdez520e1222017-06-13 12:45:25 -0400555 (ssl->version == TLS1_3_EXPERIMENT_VERSION && !CBB_add_u8(&body, 0)) ||
Steven Valdez143e8b32016-07-11 13:19:03 -0400556 !CBB_add_u16_length_prefixed(&body, &extensions) ||
David Benjamin8baf9632016-11-17 17:11:16 +0900557 !ssl_ext_pre_shared_key_add_serverhello(hs, &extensions) ||
Steven Valdez924a3522017-03-02 16:05:03 -0500558 !ssl_ext_key_share_add_serverhello(hs, &extensions) ||
Steven Valdez038da9b2017-07-10 12:57:25 -0400559 (ssl->version == TLS1_3_EXPERIMENT_VERSION &&
560 !ssl_ext_supported_versions_add_serverhello(hs, &extensions)) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400561 !ssl_add_message_cbb(ssl, cbb.get())) {
562 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400563 }
564
Steven Valdez520e1222017-06-13 12:45:25 -0400565 if (ssl->version == TLS1_3_EXPERIMENT_VERSION &&
566 !ssl3_add_change_cipher_spec(ssl)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400567 return ssl_hs_error;
Steven Valdez520e1222017-06-13 12:45:25 -0400568 }
569
David Benjaminc11ea9422017-08-29 16:33:21 -0400570 // Derive and enable the handshake traffic secrets.
Steven Valdez4cb84942016-12-16 11:29:28 -0500571 if (!tls13_derive_handshake_secrets(hs) ||
Steven Valdez4cb84942016-12-16 11:29:28 -0500572 !tls13_set_traffic_key(ssl, evp_aead_seal, hs->server_handshake_secret,
573 hs->hash_len)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400574 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400575 }
576
David Benjaminc11ea9422017-08-29 16:33:21 -0400577 // Send EncryptedExtensions.
David Benjamin1386aad2017-07-19 23:57:40 -0400578 if (!ssl->method->init_message(ssl, cbb.get(), &body,
Steven Valdez143e8b32016-07-11 13:19:03 -0400579 SSL3_MT_ENCRYPTED_EXTENSIONS) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500580 !ssl_add_serverhello_tlsext(hs, &body) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400581 !ssl_add_message_cbb(ssl, cbb.get())) {
582 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400583 }
584
David Benjaminc3648fa2017-07-01 10:50:56 -0400585 if (!ssl->s3->session_reused) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400586 // Determine whether to request a client certificate.
David Benjaminc3648fa2017-07-01 10:50:56 -0400587 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
David Benjaminc11ea9422017-08-29 16:33:21 -0400588 // Only request a certificate if Channel ID isn't negotiated.
David Benjaminc3648fa2017-07-01 10:50:56 -0400589 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
590 ssl->s3->tlsext_channel_id_valid) {
David Benjaminfd45ee72017-08-31 14:49:09 -0400591 hs->cert_request = false;
David Benjaminc3648fa2017-07-01 10:50:56 -0400592 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400593 }
594
David Benjaminc11ea9422017-08-29 16:33:21 -0400595 // Send a CertificateRequest, if necessary.
David Benjamin81b7bc32017-01-12 19:44:57 -0500596 if (hs->cert_request) {
597 CBB sigalgs_cbb;
David Benjamin1386aad2017-07-19 23:57:40 -0400598 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin81b7bc32017-01-12 19:44:57 -0500599 SSL3_MT_CERTIFICATE_REQUEST) ||
David Benjamin69522112017-03-28 15:38:29 -0500600 !CBB_add_u8(&body, 0 /* no certificate_request_context. */) ||
601 !CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
602 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) ||
603 !ssl_add_client_CA_list(ssl, &body) ||
David Benjamin81b7bc32017-01-12 19:44:57 -0500604 !CBB_add_u16(&body, 0 /* empty certificate_extensions. */) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400605 !ssl_add_message_cbb(ssl, cbb.get())) {
606 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400607 }
608 }
609
David Benjaminc11ea9422017-08-29 16:33:21 -0400610 // Send the server Certificate message, if necessary.
David Benjamin81b7bc32017-01-12 19:44:57 -0500611 if (!ssl->s3->session_reused) {
612 if (!ssl_has_certificate(ssl)) {
613 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
David Benjamin1386aad2017-07-19 23:57:40 -0400614 return ssl_hs_error;
David Benjamin81b7bc32017-01-12 19:44:57 -0500615 }
616
David Benjamin0f24bed2017-01-12 19:46:50 -0500617 if (!tls13_add_certificate(hs)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400618 return ssl_hs_error;
David Benjamin81b7bc32017-01-12 19:44:57 -0500619 }
620
621 hs->tls13_state = state_send_server_certificate_verify;
622 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400623 }
624
David Benjamin81b7bc32017-01-12 19:44:57 -0500625 hs->tls13_state = state_send_server_finished;
David Benjamin25ac2512017-01-12 19:31:28 -0500626 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400627}
628
David Benjamin44148742017-06-17 13:20:59 -0400629static enum ssl_hs_wait_t do_send_server_certificate_verify(SSL_HANDSHAKE *hs) {
630 switch (tls13_add_certificate_verify(hs)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400631 case ssl_private_key_success:
David Benjamin3977f302016-12-11 13:30:41 -0500632 hs->tls13_state = state_send_server_finished;
David Benjamin25ac2512017-01-12 19:31:28 -0500633 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400634
635 case ssl_private_key_retry:
David Benjamin44148742017-06-17 13:20:59 -0400636 hs->tls13_state = state_send_server_certificate_verify;
Steven Valdez143e8b32016-07-11 13:19:03 -0400637 return ssl_hs_private_key_operation;
638
639 case ssl_private_key_failure:
640 return ssl_hs_error;
641 }
642
643 assert(0);
644 return ssl_hs_error;
645}
646
David Benjaminc3c88822016-11-14 10:32:04 +0900647static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900648 SSL *const ssl = hs->ssl;
David Benjamin0f24bed2017-01-12 19:46:50 -0500649 if (!tls13_add_finished(hs) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400650 // Update the secret to the master secret and derive traffic keys.
David Benjamin25ac2512017-01-12 19:31:28 -0500651 !tls13_advance_key_schedule(hs, kZeroes, hs->hash_len) ||
David Benjamin6e4fc332016-11-17 16:43:08 +0900652 !tls13_derive_application_secrets(hs) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400653 !tls13_set_traffic_key(ssl, evp_aead_seal, hs->server_traffic_secret_0,
654 hs->hash_len)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400655 return ssl_hs_error;
656 }
657
David Benjamin794cc592017-03-25 22:24:23 -0500658 if (ssl->early_data_accepted) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400659 // If accepting 0-RTT, we send tickets half-RTT. This gets the tickets on
660 // the wire sooner and also avoids triggering a write on |SSL_read| when
661 // processing the client Finished. This requires computing the client
662 // Finished early. See draft-ietf-tls-tls13-18, section 4.5.1.
David Benjamin794cc592017-03-25 22:24:23 -0500663 size_t finished_len;
664 if (!tls13_finished_mac(hs, hs->expected_client_finished, &finished_len,
665 0 /* client */)) {
666 return ssl_hs_error;
667 }
668
669 if (finished_len != hs->hash_len) {
670 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
671 return ssl_hs_error;
672 }
673
David Benjaminc11ea9422017-08-29 16:33:21 -0400674 // Feed the predicted Finished into the transcript. This allows us to derive
675 // the resumption secret early and send half-RTT tickets.
676 //
677 // TODO(davidben): This will need to be updated for DTLS 1.3.
David Benjamin794cc592017-03-25 22:24:23 -0500678 assert(!SSL_is_dtls(hs->ssl));
David Benjamind304a2f2017-07-12 23:00:28 -0400679 assert(hs->hash_len <= 0xff);
David Benjamin6dc8bf62017-07-19 16:38:21 -0400680 uint8_t header[4] = {SSL3_MT_FINISHED, 0, 0,
681 static_cast<uint8_t>(hs->hash_len)};
682 if (!hs->transcript.Update(header, sizeof(header)) ||
683 !hs->transcript.Update(hs->expected_client_finished, hs->hash_len) ||
David Benjamin8f94c312017-08-01 17:35:55 -0400684 !tls13_derive_resumption_secret(hs) ||
685 !add_new_session_tickets(hs)) {
David Benjamin794cc592017-03-25 22:24:23 -0500686 return ssl_hs_error;
687 }
688 }
689
Steven Valdez2d850622017-01-11 11:34:52 -0500690 hs->tls13_state = state_read_second_client_flight;
691 return ssl_hs_flush;
692}
693
694static enum ssl_hs_wait_t do_read_second_client_flight(SSL_HANDSHAKE *hs) {
695 SSL *const ssl = hs->ssl;
696 if (ssl->early_data_accepted) {
697 if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->early_traffic_secret,
698 hs->hash_len)) {
699 return ssl_hs_error;
700 }
David Benjaminfd45ee72017-08-31 14:49:09 -0400701 hs->can_early_write = true;
702 hs->can_early_read = true;
703 hs->in_early_data = true;
Steven Valdez2d850622017-01-11 11:34:52 -0500704 hs->tls13_state = state_process_end_of_early_data;
705 return ssl_hs_read_end_of_early_data;
706 }
Steven Valdez2d850622017-01-11 11:34:52 -0500707 hs->tls13_state = state_process_end_of_early_data;
708 return ssl_hs_ok;
709}
710
711static enum ssl_hs_wait_t do_process_end_of_early_data(SSL_HANDSHAKE *hs) {
Steven Valdez520e1222017-06-13 12:45:25 -0400712 hs->tls13_state = state_process_change_cipher_spec;
David Benjaminc11ea9422017-08-29 16:33:21 -0400713 // If early data was accepted, the ChangeCipherSpec message will be in the
714 // discarded early data.
Steven Valdez520e1222017-06-13 12:45:25 -0400715 if (hs->early_data_offered && !hs->ssl->early_data_accepted) {
716 return ssl_hs_ok;
717 }
718 return hs->ssl->version == TLS1_3_EXPERIMENT_VERSION
719 ? ssl_hs_read_change_cipher_spec
720 : ssl_hs_ok;
721}
722
723static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
Steven Valdez2d850622017-01-11 11:34:52 -0500724 SSL *const ssl = hs->ssl;
725 if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->client_handshake_secret,
726 hs->hash_len)) {
727 return ssl_hs_error;
728 }
David Benjamin7934f082017-08-01 16:32:25 -0400729 hs->tls13_state = ssl->early_data_accepted ? state_read_client_finished
730 : state_read_client_certificate;
731 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400732}
733
David Benjamin7934f082017-08-01 16:32:25 -0400734static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900735 SSL *const ssl = hs->ssl;
736 if (!hs->cert_request) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400737 // OpenSSL returns X509_V_OK when no certificates are requested. This is
738 // classed by them as a bug, but it's assumed by at least NGINX.
David Benjamin45738dd2017-02-09 20:01:26 -0500739 hs->new_session->verify_result = X509_V_OK;
Adam Langley37646832016-08-01 16:16:46 -0700740
David Benjaminc11ea9422017-08-29 16:33:21 -0400741 // Skip this state.
David Benjamin7934f082017-08-01 16:32:25 -0400742 hs->tls13_state = state_read_channel_id;
Steven Valdez143e8b32016-07-11 13:19:03 -0400743 return ssl_hs_ok;
744 }
745
David Benjamin4087df92016-08-01 20:16:31 -0400746 const int allow_anonymous =
747 (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) == 0;
David Benjamin7934f082017-08-01 16:32:25 -0400748 SSLMessage msg;
749 if (!ssl->method->get_message(ssl, &msg)) {
750 return ssl_hs_read_message;
751 }
752 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
753 !tls13_process_certificate(hs, msg, allow_anonymous) ||
754 !ssl_hash_message(hs, msg)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400755 return ssl_hs_error;
756 }
757
David Benjamin8f94c312017-08-01 17:35:55 -0400758 ssl->method->next_message(ssl);
David Benjamin7934f082017-08-01 16:32:25 -0400759 hs->tls13_state = state_read_client_certificate_verify;
760 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400761}
762
David Benjamin7934f082017-08-01 16:32:25 -0400763static enum ssl_hs_wait_t do_read_client_certificate_verify(
David Benjaminc3c88822016-11-14 10:32:04 +0900764 SSL_HANDSHAKE *hs) {
765 SSL *const ssl = hs->ssl;
David Benjamin45738dd2017-02-09 20:01:26 -0500766 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400767 // Skip this state.
David Benjamin7934f082017-08-01 16:32:25 -0400768 hs->tls13_state = state_read_channel_id;
Steven Valdez143e8b32016-07-11 13:19:03 -0400769 return ssl_hs_ok;
770 }
771
David Benjamin7934f082017-08-01 16:32:25 -0400772 SSLMessage msg;
773 if (!ssl->method->get_message(ssl, &msg)) {
774 return ssl_hs_read_message;
775 }
776
David Benjamin3a1dd462017-07-11 16:13:10 -0400777 switch (ssl_verify_peer_cert(hs)) {
778 case ssl_verify_ok:
779 break;
780 case ssl_verify_invalid:
781 return ssl_hs_error;
782 case ssl_verify_retry:
David Benjamin7934f082017-08-01 16:32:25 -0400783 hs->tls13_state = state_read_client_certificate_verify;
David Benjamin3a1dd462017-07-11 16:13:10 -0400784 return ssl_hs_certificate_verify;
785 }
786
David Benjamin7934f082017-08-01 16:32:25 -0400787 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY) ||
788 !tls13_process_certificate_verify(hs, msg) ||
789 !ssl_hash_message(hs, msg)) {
David Benjamin6929f272016-11-16 19:10:08 +0900790 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400791 }
792
David Benjamin8f94c312017-08-01 17:35:55 -0400793 ssl->method->next_message(ssl);
David Benjamin7934f082017-08-01 16:32:25 -0400794 hs->tls13_state = state_read_channel_id;
795 return ssl_hs_ok;
Nick Harper60a85cb2016-09-23 16:25:11 -0700796}
797
David Benjamin7934f082017-08-01 16:32:25 -0400798static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
David Benjamin8f94c312017-08-01 17:35:55 -0400799 SSL *const ssl = hs->ssl;
800 if (!ssl->s3->tlsext_channel_id_valid) {
David Benjamin7934f082017-08-01 16:32:25 -0400801 hs->tls13_state = state_read_client_finished;
Nick Harper60a85cb2016-09-23 16:25:11 -0700802 return ssl_hs_ok;
803 }
804
David Benjamin7934f082017-08-01 16:32:25 -0400805 SSLMessage msg;
806 if (!ssl->method->get_message(ssl, &msg)) {
807 return ssl_hs_read_message;
808 }
809 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
810 !tls1_verify_channel_id(hs, msg) ||
811 !ssl_hash_message(hs, msg)) {
Nick Harper60a85cb2016-09-23 16:25:11 -0700812 return ssl_hs_error;
813 }
814
David Benjamin8f94c312017-08-01 17:35:55 -0400815 ssl->method->next_message(ssl);
David Benjamin7934f082017-08-01 16:32:25 -0400816 hs->tls13_state = state_read_client_finished;
817 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400818}
819
David Benjamin7934f082017-08-01 16:32:25 -0400820static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900821 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400822 SSLMessage msg;
823 if (!ssl->method->get_message(ssl, &msg)) {
824 return ssl_hs_read_message;
825 }
826 if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400827 // If early data was accepted, we've already computed the client Finished
828 // and derived the resumption secret.
David Benjamin7934f082017-08-01 16:32:25 -0400829 !tls13_process_finished(hs, msg, ssl->early_data_accepted) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400830 // evp_aead_seal keys have already been switched.
Steven Valdeza833c352016-11-01 13:39:36 -0400831 !tls13_set_traffic_key(ssl, evp_aead_open, hs->client_traffic_secret_0,
David Benjamin794cc592017-03-25 22:24:23 -0500832 hs->hash_len)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400833 return ssl_hs_error;
834 }
835
David Benjamin794cc592017-03-25 22:24:23 -0500836 if (!ssl->early_data_accepted) {
David Benjamin7934f082017-08-01 16:32:25 -0400837 if (!ssl_hash_message(hs, msg) ||
David Benjamin794cc592017-03-25 22:24:23 -0500838 !tls13_derive_resumption_secret(hs)) {
839 return ssl_hs_error;
840 }
841
David Benjaminc11ea9422017-08-29 16:33:21 -0400842 // We send post-handshake tickets as part of the handshake in 1-RTT.
David Benjamin794cc592017-03-25 22:24:23 -0500843 hs->tls13_state = state_send_new_session_ticket;
David Benjamin8f94c312017-08-01 17:35:55 -0400844 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -0400845 // We already sent half-RTT tickets.
David Benjamin8f94c312017-08-01 17:35:55 -0400846 hs->tls13_state = state_done;
David Benjamin794cc592017-03-25 22:24:23 -0500847 }
848
David Benjamin8f94c312017-08-01 17:35:55 -0400849 ssl->method->next_message(ssl);
Steven Valdez143e8b32016-07-11 13:19:03 -0400850 return ssl_hs_ok;
851}
852
David Benjaminc3c88822016-11-14 10:32:04 +0900853static enum ssl_hs_wait_t do_send_new_session_ticket(SSL_HANDSHAKE *hs) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400854 // If the client doesn't accept resumption with PSK_DHE_KE, don't send a
855 // session ticket.
Steven Valdeza833c352016-11-01 13:39:36 -0400856 if (!hs->accept_psk_mode) {
David Benjamin3977f302016-12-11 13:30:41 -0500857 hs->tls13_state = state_done;
Steven Valdeza833c352016-11-01 13:39:36 -0400858 return ssl_hs_ok;
859 }
860
David Benjamin794cc592017-03-25 22:24:23 -0500861 if (!add_new_session_tickets(hs)) {
862 return ssl_hs_error;
Steven Valdez08b65f42016-12-07 15:29:45 -0500863 }
864
David Benjamin25ac2512017-01-12 19:31:28 -0500865 hs->tls13_state = state_done;
866 return ssl_hs_flush;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400867}
868
David Benjaminc3c88822016-11-14 10:32:04 +0900869enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs) {
David Benjamin3977f302016-12-11 13:30:41 -0500870 while (hs->tls13_state != state_done) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400871 enum ssl_hs_wait_t ret = ssl_hs_error;
David Benjamind304a2f2017-07-12 23:00:28 -0400872 enum server_hs_state_t state =
873 static_cast<enum server_hs_state_t>(hs->tls13_state);
Steven Valdez143e8b32016-07-11 13:19:03 -0400874 switch (state) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400875 case state_select_parameters:
David Benjaminc3c88822016-11-14 10:32:04 +0900876 ret = do_select_parameters(hs);
David Benjamin25fe85b2016-08-09 20:00:32 -0400877 break;
David Benjamin707af292017-03-10 17:47:18 -0500878 case state_select_session:
879 ret = do_select_session(hs);
880 break;
Steven Valdez5440fe02016-07-18 12:40:30 -0400881 case state_send_hello_retry_request:
David Benjaminc3c88822016-11-14 10:32:04 +0900882 ret = do_send_hello_retry_request(hs);
Steven Valdez5440fe02016-07-18 12:40:30 -0400883 break;
David Benjamin7934f082017-08-01 16:32:25 -0400884 case state_read_second_client_hello:
885 ret = do_read_second_client_hello(hs);
Steven Valdez5440fe02016-07-18 12:40:30 -0400886 break;
Steven Valdez143e8b32016-07-11 13:19:03 -0400887 case state_send_server_hello:
David Benjaminc3c88822016-11-14 10:32:04 +0900888 ret = do_send_server_hello(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400889 break;
Steven Valdez143e8b32016-07-11 13:19:03 -0400890 case state_send_server_certificate_verify:
David Benjamin44148742017-06-17 13:20:59 -0400891 ret = do_send_server_certificate_verify(hs);
Steven Valdez2d850622017-01-11 11:34:52 -0500892 break;
Steven Valdez143e8b32016-07-11 13:19:03 -0400893 case state_send_server_finished:
David Benjaminc3c88822016-11-14 10:32:04 +0900894 ret = do_send_server_finished(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400895 break;
Steven Valdez2d850622017-01-11 11:34:52 -0500896 case state_read_second_client_flight:
897 ret = do_read_second_client_flight(hs);
898 break;
899 case state_process_end_of_early_data:
900 ret = do_process_end_of_early_data(hs);
901 break;
Steven Valdez520e1222017-06-13 12:45:25 -0400902 case state_process_change_cipher_spec:
903 ret = do_process_change_cipher_spec(hs);
904 break;
David Benjamin7934f082017-08-01 16:32:25 -0400905 case state_read_client_certificate:
906 ret = do_read_client_certificate(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400907 break;
David Benjamin7934f082017-08-01 16:32:25 -0400908 case state_read_client_certificate_verify:
909 ret = do_read_client_certificate_verify(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400910 break;
David Benjamin7934f082017-08-01 16:32:25 -0400911 case state_read_channel_id:
912 ret = do_read_channel_id(hs);
Nick Harper60a85cb2016-09-23 16:25:11 -0700913 break;
David Benjamin7934f082017-08-01 16:32:25 -0400914 case state_read_client_finished:
915 ret = do_read_client_finished(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400916 break;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400917 case state_send_new_session_ticket:
David Benjaminc3c88822016-11-14 10:32:04 +0900918 ret = do_send_new_session_ticket(hs);
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400919 break;
Steven Valdez143e8b32016-07-11 13:19:03 -0400920 case state_done:
921 ret = ssl_hs_ok;
922 break;
923 }
924
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400925 if (hs->tls13_state != state) {
David Benjaminf60bcfb2017-08-18 15:23:44 -0400926 ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
927 }
928
Steven Valdez143e8b32016-07-11 13:19:03 -0400929 if (ret != ssl_hs_ok) {
930 return ret;
931 }
932 }
933
934 return ssl_hs_ok;
935}
David Benjamin86e95b82017-07-18 16:34:25 -0400936
David Benjaminf60bcfb2017-08-18 15:23:44 -0400937const char *tls13_server_handshake_state(SSL_HANDSHAKE *hs) {
938 enum server_hs_state_t state =
939 static_cast<enum server_hs_state_t>(hs->tls13_state);
940 switch (state) {
941 case state_select_parameters:
942 return "TLS 1.3 server select_parameters";
943 case state_select_session:
944 return "TLS 1.3 server select_session";
945 case state_send_hello_retry_request:
946 return "TLS 1.3 server send_hello_retry_request";
947 case state_read_second_client_hello:
948 return "TLS 1.3 server read_second_client_hello";
949 case state_send_server_hello:
950 return "TLS 1.3 server send_server_hello";
951 case state_send_server_certificate_verify:
952 return "TLS 1.3 server send_server_certificate_verify";
953 case state_send_server_finished:
954 return "TLS 1.3 server send_server_finished";
955 case state_read_second_client_flight:
956 return "TLS 1.3 server read_second_client_flight";
957 case state_process_change_cipher_spec:
958 return "TLS 1.3 server process_change_cipher_spec";
959 case state_process_end_of_early_data:
960 return "TLS 1.3 server process_end_of_early_data";
961 case state_read_client_certificate:
962 return "TLS 1.3 server read_client_certificate";
963 case state_read_client_certificate_verify:
964 return "TLS 1.3 server read_client_certificate_verify";
965 case state_read_channel_id:
966 return "TLS 1.3 server read_channel_id";
967 case state_read_client_finished:
968 return "TLS 1.3 server read_client_finished";
969 case state_send_new_session_ticket:
970 return "TLS 1.3 server send_new_session_ticket";
971 case state_done:
972 return "TLS 1.3 server done";
973 }
974
975 return "TLS 1.3 server unknown";
976}
977
David Benjamin86e95b82017-07-18 16:34:25 -0400978} // namespace bssl