blob: cd6baa420041410ce1f2f3424464598886cecd8c [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 Benjamin046bc1f2017-08-31 15:06:42 -040062static int resolve_ecdhe_secret(SSL_HANDSHAKE *hs, bool *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;
David Benjamin046bc1f2017-08-31 15:06:42 -040065 *out_need_retry = false;
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
David Benjamin74795b32017-08-31 15:13:12 -040076 bool found_key_share;
Steven Valdez5440fe02016-07-18 12:40:30 -040077 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) {
David Benjamin046bc1f2017-08-31 15:06:42 -040088 *out_need_retry = true;
Steven Valdez5440fe02016-07-18 12:40:30 -040089 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
David Benjamin046bc1f2017-08-31 15:06:42 -0400397 ssl->s3->session_reused = true;
David Benjamin707af292017-03-10 17:47:18 -0500398
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
David Benjaminc11ea9422017-08-29 16:33:21 -0400416 // Store the initial negotiated ALPN in the session.
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500417 if (ssl->s3->alpn_selected != NULL) {
David Benjamind304a2f2017-07-12 23:00:28 -0400418 hs->new_session->early_alpn = (uint8_t *)BUF_memdup(
419 ssl->s3->alpn_selected, ssl->s3->alpn_selected_len);
David Benjamin45738dd2017-02-09 20:01:26 -0500420 if (hs->new_session->early_alpn == NULL) {
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500421 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
422 return ssl_hs_error;
423 }
David Benjamin45738dd2017-02-09 20:01:26 -0500424 hs->new_session->early_alpn_len = ssl->s3->alpn_selected_len;
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500425 }
426
David Benjamin707af292017-03-10 17:47:18 -0500427 if (ssl->ctx->dos_protection_cb != NULL &&
428 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400429 // Connection rejected for DOS reasons.
David Benjamin707af292017-03-10 17:47:18 -0500430 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
431 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
432 return ssl_hs_error;
433 }
434
David Benjaminc11ea9422017-08-29 16:33:21 -0400435 // Incorporate the PSK into the running secret.
David Benjamin8f820b42016-11-30 11:24:40 -0500436 if (ssl->s3->session_reused) {
David Benjamin45738dd2017-02-09 20:01:26 -0500437 if (!tls13_advance_key_schedule(hs, hs->new_session->master_key,
438 hs->new_session->master_key_length)) {
David Benjamin8f820b42016-11-30 11:24:40 -0500439 return ssl_hs_error;
440 }
Steven Valdez908ac192017-01-12 13:17:07 -0500441 } else if (!tls13_advance_key_schedule(hs, kZeroes, hs->hash_len)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400442 return ssl_hs_error;
443 }
444
Steven Valdez2d850622017-01-11 11:34:52 -0500445 if (ssl->early_data_accepted) {
446 if (!tls13_derive_early_secrets(hs)) {
447 return ssl_hs_error;
448 }
449 } else if (hs->early_data_offered) {
David Benjamin046bc1f2017-08-31 15:06:42 -0400450 ssl->s3->skip_early_data = true;
Steven Valdez2d850622017-01-11 11:34:52 -0500451 }
452
David Benjaminc11ea9422017-08-29 16:33:21 -0400453 // Resolve ECDHE and incorporate it into the secret.
David Benjamin046bc1f2017-08-31 15:06:42 -0400454 bool need_retry;
David Benjamin6e4fc332016-11-17 16:43:08 +0900455 if (!resolve_ecdhe_secret(hs, &need_retry, &client_hello)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400456 if (need_retry) {
Steven Valdez2d850622017-01-11 11:34:52 -0500457 ssl->early_data_accepted = 0;
David Benjamin046bc1f2017-08-31 15:06:42 -0400458 ssl->s3->skip_early_data = true;
David Benjamin8f94c312017-08-01 17:35:55 -0400459 ssl->method->next_message(ssl);
David Benjamin3977f302016-12-11 13:30:41 -0500460 hs->tls13_state = state_send_hello_retry_request;
Steven Valdez5440fe02016-07-18 12:40:30 -0400461 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400462 }
Steven Valdez5440fe02016-07-18 12:40:30 -0400463 return ssl_hs_error;
464 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400465
David Benjamin8f94c312017-08-01 17:35:55 -0400466 ssl->method->next_message(ssl);
David Benjamin3977f302016-12-11 13:30:41 -0500467 hs->tls13_state = state_send_server_hello;
Steven Valdez5440fe02016-07-18 12:40:30 -0400468 return ssl_hs_ok;
469}
Steven Valdez143e8b32016-07-11 13:19:03 -0400470
David Benjaminc3c88822016-11-14 10:32:04 +0900471static enum ssl_hs_wait_t do_send_hello_retry_request(SSL_HANDSHAKE *hs) {
472 SSL *const ssl = hs->ssl;
David Benjamin1386aad2017-07-19 23:57:40 -0400473 ScopedCBB cbb;
474 CBB body, extensions;
Steven Valdez5440fe02016-07-18 12:40:30 -0400475 uint16_t group_id;
David Benjamin1386aad2017-07-19 23:57:40 -0400476 if (!ssl->method->init_message(ssl, cbb.get(), &body,
Steven Valdez5440fe02016-07-18 12:40:30 -0400477 SSL3_MT_HELLO_RETRY_REQUEST) ||
478 !CBB_add_u16(&body, ssl->version) ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900479 !tls1_get_shared_group(hs, &group_id) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400480 !CBB_add_u16_length_prefixed(&body, &extensions) ||
David Benjamin3baa6e12016-10-07 21:10:38 -0400481 !CBB_add_u16(&extensions, TLSEXT_TYPE_key_share) ||
482 !CBB_add_u16(&extensions, 2 /* length */) ||
483 !CBB_add_u16(&extensions, group_id) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400484 !ssl_add_message_cbb(ssl, cbb.get())) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400485 return ssl_hs_error;
486 }
487
David Benjamin7934f082017-08-01 16:32:25 -0400488 hs->tls13_state = state_read_second_client_hello;
489 return ssl_hs_flush;
Steven Valdez5440fe02016-07-18 12:40:30 -0400490}
491
David Benjamin7934f082017-08-01 16:32:25 -0400492static enum ssl_hs_wait_t do_read_second_client_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900493 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400494 SSLMessage msg;
495 if (!ssl->method->get_message(ssl, &msg)) {
496 return ssl_hs_read_message;
497 }
498 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400499 return ssl_hs_error;
500 }
David Benjamin731058e2016-12-03 23:15:13 -0500501 SSL_CLIENT_HELLO client_hello;
David Benjamin7934f082017-08-01 16:32:25 -0400502 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400503 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
504 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
505 return ssl_hs_error;
506 }
507
David Benjamin046bc1f2017-08-31 15:06:42 -0400508 bool need_retry;
David Benjamin6e4fc332016-11-17 16:43:08 +0900509 if (!resolve_ecdhe_secret(hs, &need_retry, &client_hello)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400510 if (need_retry) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400511 // Only send one HelloRetryRequest.
Steven Valdez5440fe02016-07-18 12:40:30 -0400512 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
513 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Steven Valdez143e8b32016-07-11 13:19:03 -0400514 }
Steven Valdez5440fe02016-07-18 12:40:30 -0400515 return ssl_hs_error;
516 }
517
David Benjamin7934f082017-08-01 16:32:25 -0400518 if (!ssl_hash_message(hs, msg)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400519 return ssl_hs_error;
520 }
521
David Benjamin8f94c312017-08-01 17:35:55 -0400522 ssl->method->next_message(ssl);
David Benjamin3977f302016-12-11 13:30:41 -0500523 hs->tls13_state = state_send_server_hello;
Steven Valdez143e8b32016-07-11 13:19:03 -0400524 return ssl_hs_ok;
525}
526
David Benjaminc3c88822016-11-14 10:32:04 +0900527static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
528 SSL *const ssl = hs->ssl;
David Benjamin81b7bc32017-01-12 19:44:57 -0500529
Steven Valdez038da9b2017-07-10 12:57:25 -0400530 uint16_t version = ssl->version;
531 if (ssl->version == TLS1_3_EXPERIMENT_VERSION) {
532 version = TLS1_2_VERSION;
533 }
534
David Benjaminc11ea9422017-08-29 16:33:21 -0400535 // Send a ServerHello.
David Benjamin1386aad2017-07-19 23:57:40 -0400536 ScopedCBB cbb;
537 CBB body, extensions, session_id;
538 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
Steven Valdez038da9b2017-07-10 12:57:25 -0400539 !CBB_add_u16(&body, version) ||
Steven Valdez143e8b32016-07-11 13:19:03 -0400540 !RAND_bytes(ssl->s3->server_random, sizeof(ssl->s3->server_random)) ||
541 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
Steven Valdez520e1222017-06-13 12:45:25 -0400542 (ssl->version == TLS1_3_EXPERIMENT_VERSION &&
543 (!CBB_add_u8_length_prefixed(&body, &session_id) ||
544 !CBB_add_bytes(&session_id, hs->session_id, hs->session_id_len))) ||
David Benjamin45738dd2017-02-09 20:01:26 -0500545 !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
Steven Valdez520e1222017-06-13 12:45:25 -0400546 (ssl->version == TLS1_3_EXPERIMENT_VERSION && !CBB_add_u8(&body, 0)) ||
Steven Valdez143e8b32016-07-11 13:19:03 -0400547 !CBB_add_u16_length_prefixed(&body, &extensions) ||
David Benjamin8baf9632016-11-17 17:11:16 +0900548 !ssl_ext_pre_shared_key_add_serverhello(hs, &extensions) ||
Steven Valdez924a3522017-03-02 16:05:03 -0500549 !ssl_ext_key_share_add_serverhello(hs, &extensions) ||
Steven Valdez038da9b2017-07-10 12:57:25 -0400550 (ssl->version == TLS1_3_EXPERIMENT_VERSION &&
551 !ssl_ext_supported_versions_add_serverhello(hs, &extensions)) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400552 !ssl_add_message_cbb(ssl, cbb.get())) {
553 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400554 }
555
Steven Valdez520e1222017-06-13 12:45:25 -0400556 if (ssl->version == TLS1_3_EXPERIMENT_VERSION &&
557 !ssl3_add_change_cipher_spec(ssl)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400558 return ssl_hs_error;
Steven Valdez520e1222017-06-13 12:45:25 -0400559 }
560
David Benjaminc11ea9422017-08-29 16:33:21 -0400561 // Derive and enable the handshake traffic secrets.
Steven Valdez4cb84942016-12-16 11:29:28 -0500562 if (!tls13_derive_handshake_secrets(hs) ||
Steven Valdez4cb84942016-12-16 11:29:28 -0500563 !tls13_set_traffic_key(ssl, evp_aead_seal, hs->server_handshake_secret,
564 hs->hash_len)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400565 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400566 }
567
David Benjaminc11ea9422017-08-29 16:33:21 -0400568 // Send EncryptedExtensions.
David Benjamin1386aad2017-07-19 23:57:40 -0400569 if (!ssl->method->init_message(ssl, cbb.get(), &body,
Steven Valdez143e8b32016-07-11 13:19:03 -0400570 SSL3_MT_ENCRYPTED_EXTENSIONS) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500571 !ssl_add_serverhello_tlsext(hs, &body) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400572 !ssl_add_message_cbb(ssl, cbb.get())) {
573 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400574 }
575
David Benjaminc3648fa2017-07-01 10:50:56 -0400576 if (!ssl->s3->session_reused) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400577 // Determine whether to request a client certificate.
David Benjaminc3648fa2017-07-01 10:50:56 -0400578 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
David Benjaminc11ea9422017-08-29 16:33:21 -0400579 // Only request a certificate if Channel ID isn't negotiated.
David Benjaminc3648fa2017-07-01 10:50:56 -0400580 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
581 ssl->s3->tlsext_channel_id_valid) {
David Benjaminfd45ee72017-08-31 14:49:09 -0400582 hs->cert_request = false;
David Benjaminc3648fa2017-07-01 10:50:56 -0400583 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400584 }
585
David Benjaminc11ea9422017-08-29 16:33:21 -0400586 // Send a CertificateRequest, if necessary.
David Benjamin81b7bc32017-01-12 19:44:57 -0500587 if (hs->cert_request) {
588 CBB sigalgs_cbb;
David Benjamin1386aad2017-07-19 23:57:40 -0400589 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin81b7bc32017-01-12 19:44:57 -0500590 SSL3_MT_CERTIFICATE_REQUEST) ||
David Benjamin69522112017-03-28 15:38:29 -0500591 !CBB_add_u8(&body, 0 /* no certificate_request_context. */) ||
592 !CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
593 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) ||
594 !ssl_add_client_CA_list(ssl, &body) ||
David Benjamin81b7bc32017-01-12 19:44:57 -0500595 !CBB_add_u16(&body, 0 /* empty certificate_extensions. */) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400596 !ssl_add_message_cbb(ssl, cbb.get())) {
597 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400598 }
599 }
600
David Benjaminc11ea9422017-08-29 16:33:21 -0400601 // Send the server Certificate message, if necessary.
David Benjamin81b7bc32017-01-12 19:44:57 -0500602 if (!ssl->s3->session_reused) {
603 if (!ssl_has_certificate(ssl)) {
604 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
David Benjamin1386aad2017-07-19 23:57:40 -0400605 return ssl_hs_error;
David Benjamin81b7bc32017-01-12 19:44:57 -0500606 }
607
David Benjamin0f24bed2017-01-12 19:46:50 -0500608 if (!tls13_add_certificate(hs)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400609 return ssl_hs_error;
David Benjamin81b7bc32017-01-12 19:44:57 -0500610 }
611
612 hs->tls13_state = state_send_server_certificate_verify;
613 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400614 }
615
David Benjamin81b7bc32017-01-12 19:44:57 -0500616 hs->tls13_state = state_send_server_finished;
David Benjamin25ac2512017-01-12 19:31:28 -0500617 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400618}
619
David Benjamin44148742017-06-17 13:20:59 -0400620static enum ssl_hs_wait_t do_send_server_certificate_verify(SSL_HANDSHAKE *hs) {
621 switch (tls13_add_certificate_verify(hs)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400622 case ssl_private_key_success:
David Benjamin3977f302016-12-11 13:30:41 -0500623 hs->tls13_state = state_send_server_finished;
David Benjamin25ac2512017-01-12 19:31:28 -0500624 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400625
626 case ssl_private_key_retry:
David Benjamin44148742017-06-17 13:20:59 -0400627 hs->tls13_state = state_send_server_certificate_verify;
Steven Valdez143e8b32016-07-11 13:19:03 -0400628 return ssl_hs_private_key_operation;
629
630 case ssl_private_key_failure:
631 return ssl_hs_error;
632 }
633
634 assert(0);
635 return ssl_hs_error;
636}
637
David Benjaminc3c88822016-11-14 10:32:04 +0900638static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900639 SSL *const ssl = hs->ssl;
David Benjamin0f24bed2017-01-12 19:46:50 -0500640 if (!tls13_add_finished(hs) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400641 // Update the secret to the master secret and derive traffic keys.
David Benjamin25ac2512017-01-12 19:31:28 -0500642 !tls13_advance_key_schedule(hs, kZeroes, hs->hash_len) ||
David Benjamin6e4fc332016-11-17 16:43:08 +0900643 !tls13_derive_application_secrets(hs) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400644 !tls13_set_traffic_key(ssl, evp_aead_seal, hs->server_traffic_secret_0,
645 hs->hash_len)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400646 return ssl_hs_error;
647 }
648
David Benjamin794cc592017-03-25 22:24:23 -0500649 if (ssl->early_data_accepted) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400650 // If accepting 0-RTT, we send tickets half-RTT. This gets the tickets on
651 // the wire sooner and also avoids triggering a write on |SSL_read| when
652 // processing the client Finished. This requires computing the client
653 // Finished early. See draft-ietf-tls-tls13-18, section 4.5.1.
David Benjamin794cc592017-03-25 22:24:23 -0500654 size_t finished_len;
655 if (!tls13_finished_mac(hs, hs->expected_client_finished, &finished_len,
656 0 /* client */)) {
657 return ssl_hs_error;
658 }
659
660 if (finished_len != hs->hash_len) {
661 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
662 return ssl_hs_error;
663 }
664
David Benjaminc11ea9422017-08-29 16:33:21 -0400665 // Feed the predicted Finished into the transcript. This allows us to derive
666 // the resumption secret early and send half-RTT tickets.
667 //
668 // TODO(davidben): This will need to be updated for DTLS 1.3.
David Benjamin794cc592017-03-25 22:24:23 -0500669 assert(!SSL_is_dtls(hs->ssl));
David Benjamind304a2f2017-07-12 23:00:28 -0400670 assert(hs->hash_len <= 0xff);
David Benjamin6dc8bf62017-07-19 16:38:21 -0400671 uint8_t header[4] = {SSL3_MT_FINISHED, 0, 0,
672 static_cast<uint8_t>(hs->hash_len)};
673 if (!hs->transcript.Update(header, sizeof(header)) ||
674 !hs->transcript.Update(hs->expected_client_finished, hs->hash_len) ||
David Benjamin8f94c312017-08-01 17:35:55 -0400675 !tls13_derive_resumption_secret(hs) ||
676 !add_new_session_tickets(hs)) {
David Benjamin794cc592017-03-25 22:24:23 -0500677 return ssl_hs_error;
678 }
679 }
680
Steven Valdez2d850622017-01-11 11:34:52 -0500681 hs->tls13_state = state_read_second_client_flight;
682 return ssl_hs_flush;
683}
684
685static enum ssl_hs_wait_t do_read_second_client_flight(SSL_HANDSHAKE *hs) {
686 SSL *const ssl = hs->ssl;
687 if (ssl->early_data_accepted) {
688 if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->early_traffic_secret,
689 hs->hash_len)) {
690 return ssl_hs_error;
691 }
David Benjaminfd45ee72017-08-31 14:49:09 -0400692 hs->can_early_write = true;
693 hs->can_early_read = true;
694 hs->in_early_data = true;
Steven Valdez2d850622017-01-11 11:34:52 -0500695 hs->tls13_state = state_process_end_of_early_data;
696 return ssl_hs_read_end_of_early_data;
697 }
Steven Valdez2d850622017-01-11 11:34:52 -0500698 hs->tls13_state = state_process_end_of_early_data;
699 return ssl_hs_ok;
700}
701
702static enum ssl_hs_wait_t do_process_end_of_early_data(SSL_HANDSHAKE *hs) {
Steven Valdez520e1222017-06-13 12:45:25 -0400703 hs->tls13_state = state_process_change_cipher_spec;
David Benjaminc11ea9422017-08-29 16:33:21 -0400704 // If early data was accepted, the ChangeCipherSpec message will be in the
705 // discarded early data.
Steven Valdez520e1222017-06-13 12:45:25 -0400706 if (hs->early_data_offered && !hs->ssl->early_data_accepted) {
707 return ssl_hs_ok;
708 }
709 return hs->ssl->version == TLS1_3_EXPERIMENT_VERSION
710 ? ssl_hs_read_change_cipher_spec
711 : ssl_hs_ok;
712}
713
714static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
Steven Valdez2d850622017-01-11 11:34:52 -0500715 SSL *const ssl = hs->ssl;
716 if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->client_handshake_secret,
717 hs->hash_len)) {
718 return ssl_hs_error;
719 }
David Benjamin7934f082017-08-01 16:32:25 -0400720 hs->tls13_state = ssl->early_data_accepted ? state_read_client_finished
721 : state_read_client_certificate;
722 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400723}
724
David Benjamin7934f082017-08-01 16:32:25 -0400725static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900726 SSL *const ssl = hs->ssl;
727 if (!hs->cert_request) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400728 // OpenSSL returns X509_V_OK when no certificates are requested. This is
729 // classed by them as a bug, but it's assumed by at least NGINX.
David Benjamin45738dd2017-02-09 20:01:26 -0500730 hs->new_session->verify_result = X509_V_OK;
Adam Langley37646832016-08-01 16:16:46 -0700731
David Benjaminc11ea9422017-08-29 16:33:21 -0400732 // Skip this state.
David Benjamin7934f082017-08-01 16:32:25 -0400733 hs->tls13_state = state_read_channel_id;
Steven Valdez143e8b32016-07-11 13:19:03 -0400734 return ssl_hs_ok;
735 }
736
David Benjamin4087df92016-08-01 20:16:31 -0400737 const int allow_anonymous =
738 (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) == 0;
David Benjamin7934f082017-08-01 16:32:25 -0400739 SSLMessage msg;
740 if (!ssl->method->get_message(ssl, &msg)) {
741 return ssl_hs_read_message;
742 }
743 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
744 !tls13_process_certificate(hs, msg, allow_anonymous) ||
745 !ssl_hash_message(hs, msg)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400746 return ssl_hs_error;
747 }
748
David Benjamin8f94c312017-08-01 17:35:55 -0400749 ssl->method->next_message(ssl);
David Benjamin7934f082017-08-01 16:32:25 -0400750 hs->tls13_state = state_read_client_certificate_verify;
751 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400752}
753
David Benjamin7934f082017-08-01 16:32:25 -0400754static enum ssl_hs_wait_t do_read_client_certificate_verify(
David Benjaminc3c88822016-11-14 10:32:04 +0900755 SSL_HANDSHAKE *hs) {
756 SSL *const ssl = hs->ssl;
David Benjamin45738dd2017-02-09 20:01:26 -0500757 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400758 // Skip this state.
David Benjamin7934f082017-08-01 16:32:25 -0400759 hs->tls13_state = state_read_channel_id;
Steven Valdez143e8b32016-07-11 13:19:03 -0400760 return ssl_hs_ok;
761 }
762
David Benjamin7934f082017-08-01 16:32:25 -0400763 SSLMessage msg;
764 if (!ssl->method->get_message(ssl, &msg)) {
765 return ssl_hs_read_message;
766 }
767
David Benjamin3a1dd462017-07-11 16:13:10 -0400768 switch (ssl_verify_peer_cert(hs)) {
769 case ssl_verify_ok:
770 break;
771 case ssl_verify_invalid:
772 return ssl_hs_error;
773 case ssl_verify_retry:
David Benjamin7934f082017-08-01 16:32:25 -0400774 hs->tls13_state = state_read_client_certificate_verify;
David Benjamin3a1dd462017-07-11 16:13:10 -0400775 return ssl_hs_certificate_verify;
776 }
777
David Benjamin7934f082017-08-01 16:32:25 -0400778 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY) ||
779 !tls13_process_certificate_verify(hs, msg) ||
780 !ssl_hash_message(hs, msg)) {
David Benjamin6929f272016-11-16 19:10:08 +0900781 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400782 }
783
David Benjamin8f94c312017-08-01 17:35:55 -0400784 ssl->method->next_message(ssl);
David Benjamin7934f082017-08-01 16:32:25 -0400785 hs->tls13_state = state_read_channel_id;
786 return ssl_hs_ok;
Nick Harper60a85cb2016-09-23 16:25:11 -0700787}
788
David Benjamin7934f082017-08-01 16:32:25 -0400789static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
David Benjamin8f94c312017-08-01 17:35:55 -0400790 SSL *const ssl = hs->ssl;
791 if (!ssl->s3->tlsext_channel_id_valid) {
David Benjamin7934f082017-08-01 16:32:25 -0400792 hs->tls13_state = state_read_client_finished;
Nick Harper60a85cb2016-09-23 16:25:11 -0700793 return ssl_hs_ok;
794 }
795
David Benjamin7934f082017-08-01 16:32:25 -0400796 SSLMessage msg;
797 if (!ssl->method->get_message(ssl, &msg)) {
798 return ssl_hs_read_message;
799 }
800 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
801 !tls1_verify_channel_id(hs, msg) ||
802 !ssl_hash_message(hs, msg)) {
Nick Harper60a85cb2016-09-23 16:25:11 -0700803 return ssl_hs_error;
804 }
805
David Benjamin8f94c312017-08-01 17:35:55 -0400806 ssl->method->next_message(ssl);
David Benjamin7934f082017-08-01 16:32:25 -0400807 hs->tls13_state = state_read_client_finished;
808 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400809}
810
David Benjamin7934f082017-08-01 16:32:25 -0400811static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900812 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400813 SSLMessage msg;
814 if (!ssl->method->get_message(ssl, &msg)) {
815 return ssl_hs_read_message;
816 }
817 if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400818 // If early data was accepted, we've already computed the client Finished
819 // and derived the resumption secret.
David Benjamin7934f082017-08-01 16:32:25 -0400820 !tls13_process_finished(hs, msg, ssl->early_data_accepted) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400821 // evp_aead_seal keys have already been switched.
Steven Valdeza833c352016-11-01 13:39:36 -0400822 !tls13_set_traffic_key(ssl, evp_aead_open, hs->client_traffic_secret_0,
David Benjamin794cc592017-03-25 22:24:23 -0500823 hs->hash_len)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400824 return ssl_hs_error;
825 }
826
David Benjamin794cc592017-03-25 22:24:23 -0500827 if (!ssl->early_data_accepted) {
David Benjamin7934f082017-08-01 16:32:25 -0400828 if (!ssl_hash_message(hs, msg) ||
David Benjamin794cc592017-03-25 22:24:23 -0500829 !tls13_derive_resumption_secret(hs)) {
830 return ssl_hs_error;
831 }
832
David Benjaminc11ea9422017-08-29 16:33:21 -0400833 // We send post-handshake tickets as part of the handshake in 1-RTT.
David Benjamin794cc592017-03-25 22:24:23 -0500834 hs->tls13_state = state_send_new_session_ticket;
David Benjamin8f94c312017-08-01 17:35:55 -0400835 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -0400836 // We already sent half-RTT tickets.
David Benjamin8f94c312017-08-01 17:35:55 -0400837 hs->tls13_state = state_done;
David Benjamin794cc592017-03-25 22:24:23 -0500838 }
839
David Benjamin8f94c312017-08-01 17:35:55 -0400840 ssl->method->next_message(ssl);
Steven Valdez143e8b32016-07-11 13:19:03 -0400841 return ssl_hs_ok;
842}
843
David Benjaminc3c88822016-11-14 10:32:04 +0900844static enum ssl_hs_wait_t do_send_new_session_ticket(SSL_HANDSHAKE *hs) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400845 // If the client doesn't accept resumption with PSK_DHE_KE, don't send a
846 // session ticket.
Steven Valdeza833c352016-11-01 13:39:36 -0400847 if (!hs->accept_psk_mode) {
David Benjamin3977f302016-12-11 13:30:41 -0500848 hs->tls13_state = state_done;
Steven Valdeza833c352016-11-01 13:39:36 -0400849 return ssl_hs_ok;
850 }
851
David Benjamin794cc592017-03-25 22:24:23 -0500852 if (!add_new_session_tickets(hs)) {
853 return ssl_hs_error;
Steven Valdez08b65f42016-12-07 15:29:45 -0500854 }
855
David Benjamin25ac2512017-01-12 19:31:28 -0500856 hs->tls13_state = state_done;
857 return ssl_hs_flush;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400858}
859
David Benjaminc3c88822016-11-14 10:32:04 +0900860enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs) {
David Benjamin3977f302016-12-11 13:30:41 -0500861 while (hs->tls13_state != state_done) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400862 enum ssl_hs_wait_t ret = ssl_hs_error;
David Benjamind304a2f2017-07-12 23:00:28 -0400863 enum server_hs_state_t state =
864 static_cast<enum server_hs_state_t>(hs->tls13_state);
Steven Valdez143e8b32016-07-11 13:19:03 -0400865 switch (state) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400866 case state_select_parameters:
David Benjaminc3c88822016-11-14 10:32:04 +0900867 ret = do_select_parameters(hs);
David Benjamin25fe85b2016-08-09 20:00:32 -0400868 break;
David Benjamin707af292017-03-10 17:47:18 -0500869 case state_select_session:
870 ret = do_select_session(hs);
871 break;
Steven Valdez5440fe02016-07-18 12:40:30 -0400872 case state_send_hello_retry_request:
David Benjaminc3c88822016-11-14 10:32:04 +0900873 ret = do_send_hello_retry_request(hs);
Steven Valdez5440fe02016-07-18 12:40:30 -0400874 break;
David Benjamin7934f082017-08-01 16:32:25 -0400875 case state_read_second_client_hello:
876 ret = do_read_second_client_hello(hs);
Steven Valdez5440fe02016-07-18 12:40:30 -0400877 break;
Steven Valdez143e8b32016-07-11 13:19:03 -0400878 case state_send_server_hello:
David Benjaminc3c88822016-11-14 10:32:04 +0900879 ret = do_send_server_hello(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400880 break;
Steven Valdez143e8b32016-07-11 13:19:03 -0400881 case state_send_server_certificate_verify:
David Benjamin44148742017-06-17 13:20:59 -0400882 ret = do_send_server_certificate_verify(hs);
Steven Valdez2d850622017-01-11 11:34:52 -0500883 break;
Steven Valdez143e8b32016-07-11 13:19:03 -0400884 case state_send_server_finished:
David Benjaminc3c88822016-11-14 10:32:04 +0900885 ret = do_send_server_finished(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400886 break;
Steven Valdez2d850622017-01-11 11:34:52 -0500887 case state_read_second_client_flight:
888 ret = do_read_second_client_flight(hs);
889 break;
890 case state_process_end_of_early_data:
891 ret = do_process_end_of_early_data(hs);
892 break;
Steven Valdez520e1222017-06-13 12:45:25 -0400893 case state_process_change_cipher_spec:
894 ret = do_process_change_cipher_spec(hs);
895 break;
David Benjamin7934f082017-08-01 16:32:25 -0400896 case state_read_client_certificate:
897 ret = do_read_client_certificate(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400898 break;
David Benjamin7934f082017-08-01 16:32:25 -0400899 case state_read_client_certificate_verify:
900 ret = do_read_client_certificate_verify(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400901 break;
David Benjamin7934f082017-08-01 16:32:25 -0400902 case state_read_channel_id:
903 ret = do_read_channel_id(hs);
Nick Harper60a85cb2016-09-23 16:25:11 -0700904 break;
David Benjamin7934f082017-08-01 16:32:25 -0400905 case state_read_client_finished:
906 ret = do_read_client_finished(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400907 break;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400908 case state_send_new_session_ticket:
David Benjaminc3c88822016-11-14 10:32:04 +0900909 ret = do_send_new_session_ticket(hs);
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400910 break;
Steven Valdez143e8b32016-07-11 13:19:03 -0400911 case state_done:
912 ret = ssl_hs_ok;
913 break;
914 }
915
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400916 if (hs->tls13_state != state) {
David Benjaminf60bcfb2017-08-18 15:23:44 -0400917 ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
918 }
919
Steven Valdez143e8b32016-07-11 13:19:03 -0400920 if (ret != ssl_hs_ok) {
921 return ret;
922 }
923 }
924
925 return ssl_hs_ok;
926}
David Benjamin86e95b82017-07-18 16:34:25 -0400927
David Benjaminf60bcfb2017-08-18 15:23:44 -0400928const char *tls13_server_handshake_state(SSL_HANDSHAKE *hs) {
929 enum server_hs_state_t state =
930 static_cast<enum server_hs_state_t>(hs->tls13_state);
931 switch (state) {
932 case state_select_parameters:
933 return "TLS 1.3 server select_parameters";
934 case state_select_session:
935 return "TLS 1.3 server select_session";
936 case state_send_hello_retry_request:
937 return "TLS 1.3 server send_hello_retry_request";
938 case state_read_second_client_hello:
939 return "TLS 1.3 server read_second_client_hello";
940 case state_send_server_hello:
941 return "TLS 1.3 server send_server_hello";
942 case state_send_server_certificate_verify:
943 return "TLS 1.3 server send_server_certificate_verify";
944 case state_send_server_finished:
945 return "TLS 1.3 server send_server_finished";
946 case state_read_second_client_flight:
947 return "TLS 1.3 server read_second_client_flight";
948 case state_process_change_cipher_spec:
949 return "TLS 1.3 server process_change_cipher_spec";
950 case state_process_end_of_early_data:
951 return "TLS 1.3 server process_end_of_early_data";
952 case state_read_client_certificate:
953 return "TLS 1.3 server read_client_certificate";
954 case state_read_client_certificate_verify:
955 return "TLS 1.3 server read_client_certificate_verify";
956 case state_read_channel_id:
957 return "TLS 1.3 server read_channel_id";
958 case state_read_client_finished:
959 return "TLS 1.3 server read_client_finished";
960 case state_send_new_session_ticket:
961 return "TLS 1.3 server send_new_session_ticket";
962 case state_done:
963 return "TLS 1.3 server done";
964 }
965
966 return "TLS 1.3 server unknown";
967}
968
David Benjamin86e95b82017-07-18 16:34:25 -0400969} // namespace bssl