blob: f50b07776a59f00e3feeace5123efb0e74506d93 [file] [log] [blame]
Steven Valdez143e8b32016-07-11 13:19:03 -04001/* Copyright (c) 2016, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include <openssl/ssl.h>
16
17#include <assert.h>
David Benjamin17b30832017-01-28 14:00:32 -050018#include <limits.h>
Steven Valdez143e8b32016-07-11 13:19:03 -040019#include <string.h>
20
David Benjamin31b0c9b2017-07-20 14:49:15 -040021#include <utility>
22
Steven Valdez143e8b32016-07-11 13:19:03 -040023#include <openssl/bytestring.h>
24#include <openssl/digest.h>
25#include <openssl/err.h>
26#include <openssl/mem.h>
27#include <openssl/stack.h>
Steven Valdez143e8b32016-07-11 13:19:03 -040028
David Benjaminffb11072016-11-13 10:32:10 +090029#include "../crypto/internal.h"
Steven Valdez143e8b32016-07-11 13:19:03 -040030#include "internal.h"
31
32
David Benjamin86e95b82017-07-18 16:34:25 -040033namespace bssl {
34
Steven Valdez143e8b32016-07-11 13:19:03 -040035enum client_hs_state_t {
David Benjamin7934f082017-08-01 16:32:25 -040036 state_read_hello_retry_request = 0,
Steven Valdez5440fe02016-07-18 12:40:30 -040037 state_send_second_client_hello,
David Benjamin7934f082017-08-01 16:32:25 -040038 state_read_server_hello,
Steven Valdez520e1222017-06-13 12:45:25 -040039 state_process_change_cipher_spec,
David Benjamin7934f082017-08-01 16:32:25 -040040 state_read_encrypted_extensions,
41 state_read_certificate_request,
42 state_read_server_certificate,
43 state_read_server_certificate_verify,
44 state_read_server_finished,
Steven Valdez2d850622017-01-11 11:34:52 -050045 state_send_end_of_early_data,
Steven Valdez143e8b32016-07-11 13:19:03 -040046 state_send_client_certificate,
47 state_send_client_certificate_verify,
David Benjamin81b7bc32017-01-12 19:44:57 -050048 state_complete_second_flight,
Steven Valdez143e8b32016-07-11 13:19:03 -040049 state_done,
50};
51
David Benjamin8f820b42016-11-30 11:24:40 -050052static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0};
53
David Benjamin7934f082017-08-01 16:32:25 -040054static enum ssl_hs_wait_t do_read_hello_retry_request(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +090055 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -040056 SSLMessage msg;
57 if (!ssl->method->get_message(ssl, &msg)) {
58 return ssl_hs_read_message;
59 }
60 if (msg.type != SSL3_MT_HELLO_RETRY_REQUEST) {
61 hs->tls13_state = state_read_server_hello;
Steven Valdez5440fe02016-07-18 12:40:30 -040062 return ssl_hs_ok;
63 }
64
David Benjamin7934f082017-08-01 16:32:25 -040065 CBS body = msg.body, extensions;
Steven Valdez038da9b2017-07-10 12:57:25 -040066 uint16_t server_version;
David Benjamin7934f082017-08-01 16:32:25 -040067 if (!CBS_get_u16(&body, &server_version) ||
68 !CBS_get_u16_length_prefixed(&body, &extensions) ||
David Benjaminc11ea9422017-08-29 16:33:21 -040069 // HelloRetryRequest may not be empty.
David Benjamin3baa6e12016-10-07 21:10:38 -040070 CBS_len(&extensions) == 0 ||
David Benjamin7934f082017-08-01 16:32:25 -040071 CBS_len(&body) != 0) {
David Benjamin3baa6e12016-10-07 21:10:38 -040072 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez5440fe02016-07-18 12:40:30 -040073 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
74 return ssl_hs_error;
75 }
76
David Benjamin74795b32017-08-31 15:13:12 -040077 bool have_cookie, have_key_share;
David Benjaminffb11072016-11-13 10:32:10 +090078 CBS cookie, key_share;
79 const SSL_EXTENSION_TYPE ext_types[] = {
80 {TLSEXT_TYPE_key_share, &have_key_share, &key_share},
81 {TLSEXT_TYPE_cookie, &have_cookie, &cookie},
82 };
83
Adam Langleyc68e5b92017-02-08 13:33:15 -080084 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminffb11072016-11-13 10:32:10 +090085 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
Steven Valdez08b65f42016-12-07 15:29:45 -050086 OPENSSL_ARRAY_SIZE(ext_types),
87 0 /* reject unknown */)) {
David Benjaminffb11072016-11-13 10:32:10 +090088 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
89 return ssl_hs_error;
90 }
91
92 if (have_cookie) {
93 CBS cookie_value;
94 if (!CBS_get_u16_length_prefixed(&cookie, &cookie_value) ||
95 CBS_len(&cookie_value) == 0 ||
96 CBS_len(&cookie) != 0) {
David Benjamin3baa6e12016-10-07 21:10:38 -040097 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
98 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
99 return ssl_hs_error;
100 }
Steven Valdez5440fe02016-07-18 12:40:30 -0400101
David Benjaminffb11072016-11-13 10:32:10 +0900102 if (!CBS_stow(&cookie_value, &hs->cookie, &hs->cookie_len)) {
103 return ssl_hs_error;
Steven Valdez5440fe02016-07-18 12:40:30 -0400104 }
105 }
106
David Benjaminffb11072016-11-13 10:32:10 +0900107 if (have_key_share) {
108 uint16_t group_id;
109 if (!CBS_get_u16(&key_share, &group_id) || CBS_len(&key_share) != 0) {
110 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
111 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
112 return ssl_hs_error;
113 }
114
David Benjaminc11ea9422017-08-29 16:33:21 -0400115 // The group must be supported.
David Benjaminffb11072016-11-13 10:32:10 +0900116 const uint16_t *groups;
117 size_t groups_len;
118 tls1_get_grouplist(ssl, &groups, &groups_len);
119 int found = 0;
120 for (size_t i = 0; i < groups_len; i++) {
121 if (groups[i] == group_id) {
122 found = 1;
123 break;
124 }
125 }
126
127 if (!found) {
128 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
129 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
130 return ssl_hs_error;
131 }
132
David Benjaminc11ea9422017-08-29 16:33:21 -0400133 // Check that the HelloRetryRequest does not request the key share that
134 // was provided in the initial ClientHello.
David Benjaminc642aca2017-07-19 23:28:43 -0400135 if (hs->key_share->GroupID() == group_id) {
David Benjaminffb11072016-11-13 10:32:10 +0900136 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
137 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
138 return ssl_hs_error;
139 }
140
David Benjaminc642aca2017-07-19 23:28:43 -0400141 hs->key_share.reset();
David Benjaminffb11072016-11-13 10:32:10 +0900142 hs->retry_group = group_id;
143 }
144
David Benjamin7934f082017-08-01 16:32:25 -0400145 if (!ssl_hash_message(hs, msg)) {
David Benjaminf71036e2017-01-21 14:49:39 -0500146 return ssl_hs_error;
147 }
148
David Benjamin8f94c312017-08-01 17:35:55 -0400149 ssl->method->next_message(ssl);
David Benjaminfd45ee72017-08-31 14:49:09 -0400150 hs->received_hello_retry_request = true;
David Benjamin3977f302016-12-11 13:30:41 -0500151 hs->tls13_state = state_send_second_client_hello;
David Benjaminc11ea9422017-08-29 16:33:21 -0400152 // 0-RTT is rejected if we receive a HelloRetryRequest.
Steven Valdeze831a812017-03-09 14:56:07 -0500153 if (hs->in_early_data) {
154 return ssl_hs_early_data_rejected;
155 }
Steven Valdez5440fe02016-07-18 12:40:30 -0400156 return ssl_hs_ok;
157}
158
David Benjaminc3c88822016-11-14 10:32:04 +0900159static enum ssl_hs_wait_t do_send_second_client_hello(SSL_HANDSHAKE *hs) {
Steven Valdez2d850622017-01-11 11:34:52 -0500160 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400161 // Restore the null cipher. We may have switched due to 0-RTT.
Steven Valdezc7d4d212017-09-11 13:53:08 -0400162 bssl::UniquePtr<SSLAEADContext> null_ctx =
163 SSLAEADContext::CreateNullCipher(SSL_is_dtls(ssl));
David Benjamincfc11c22017-07-18 22:45:18 -0400164 if (!null_ctx ||
Steven Valdezc7d4d212017-09-11 13:53:08 -0400165 !ssl->method->set_write_state(ssl, std::move(null_ctx))) {
166 return ssl_hs_error;
167 }
168
169 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
170
171 if (!ssl_write_client_hello(hs)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400172 return ssl_hs_error;
173 }
174
David Benjamin7934f082017-08-01 16:32:25 -0400175 hs->tls13_state = state_read_server_hello;
176 return ssl_hs_flush;
Steven Valdez5440fe02016-07-18 12:40:30 -0400177}
178
David Benjamin7934f082017-08-01 16:32:25 -0400179static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900180 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400181 SSLMessage msg;
182 if (!ssl->method->get_message(ssl, &msg)) {
183 return ssl_hs_read_message;
184 }
185 if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400186 return ssl_hs_error;
187 }
188
David Benjamin7934f082017-08-01 16:32:25 -0400189 CBS body = msg.body, server_random, session_id, extensions;
Steven Valdez038da9b2017-07-10 12:57:25 -0400190 uint16_t server_version;
Steven Valdez143e8b32016-07-11 13:19:03 -0400191 uint16_t cipher_suite;
Steven Valdez520e1222017-06-13 12:45:25 -0400192 uint8_t compression_method;
David Benjamin7934f082017-08-01 16:32:25 -0400193 if (!CBS_get_u16(&body, &server_version) ||
194 !CBS_get_bytes(&body, &server_random, SSL3_RANDOM_SIZE) ||
Steven Valdez16821262017-09-08 17:03:42 -0400195 (ssl_is_resumption_experiment(ssl->version) &&
David Benjamin7934f082017-08-01 16:32:25 -0400196 !CBS_get_u8_length_prefixed(&body, &session_id)) ||
197 !CBS_get_u16(&body, &cipher_suite) ||
Steven Valdez16821262017-09-08 17:03:42 -0400198 (ssl_is_resumption_experiment(ssl->version) &&
David Benjamin7934f082017-08-01 16:32:25 -0400199 (!CBS_get_u8(&body, &compression_method) || compression_method != 0)) ||
200 !CBS_get_u16_length_prefixed(&body, &extensions) ||
201 CBS_len(&body) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400202 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez5440fe02016-07-18 12:40:30 -0400203 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
204 return ssl_hs_error;
205 }
206
Steven Valdez16821262017-09-08 17:03:42 -0400207 uint16_t expected_version = ssl_is_resumption_experiment(ssl->version)
208 ? TLS1_2_VERSION
209 : ssl->version;
Steven Valdez038da9b2017-07-10 12:57:25 -0400210 if (server_version != expected_version) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400211 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
212 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
Steven Valdez143e8b32016-07-11 13:19:03 -0400213 return ssl_hs_error;
214 }
215
Steven Valdez803c77a2016-09-06 14:13:43 -0400216 assert(ssl->s3->have_version);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500217 OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
218 SSL3_RANDOM_SIZE);
Steven Valdez803c77a2016-09-06 14:13:43 -0400219
220 const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
221 if (cipher == NULL) {
222 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
223 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
224 return ssl_hs_error;
225 }
226
David Benjaminc11ea9422017-08-29 16:33:21 -0400227 // Check if the cipher is a TLS 1.3 cipher.
David Benjaminabbbee12016-10-31 19:20:42 -0400228 if (SSL_CIPHER_get_min_version(cipher) > ssl3_protocol_version(ssl) ||
229 SSL_CIPHER_get_max_version(cipher) < ssl3_protocol_version(ssl)) {
Steven Valdez803c77a2016-09-06 14:13:43 -0400230 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
231 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
232 return ssl_hs_error;
233 }
234
David Benjaminc11ea9422017-08-29 16:33:21 -0400235 // Parse out the extensions.
David Benjamin74795b32017-08-31 15:13:12 -0400236 bool have_key_share = false, have_pre_shared_key = false,
237 have_supported_versions = false;
Steven Valdez038da9b2017-07-10 12:57:25 -0400238 CBS key_share, pre_shared_key, supported_versions;
David Benjaminffb11072016-11-13 10:32:10 +0900239 const SSL_EXTENSION_TYPE ext_types[] = {
240 {TLSEXT_TYPE_key_share, &have_key_share, &key_share},
241 {TLSEXT_TYPE_pre_shared_key, &have_pre_shared_key, &pre_shared_key},
Steven Valdez038da9b2017-07-10 12:57:25 -0400242 {TLSEXT_TYPE_supported_versions, &have_supported_versions,
243 &supported_versions},
David Benjaminffb11072016-11-13 10:32:10 +0900244 };
Steven Valdez143e8b32016-07-11 13:19:03 -0400245
Adam Langleyc68e5b92017-02-08 13:33:15 -0800246 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminffb11072016-11-13 10:32:10 +0900247 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
Steven Valdez08b65f42016-12-07 15:29:45 -0500248 OPENSSL_ARRAY_SIZE(ext_types),
249 0 /* reject unknown */)) {
David Benjaminffb11072016-11-13 10:32:10 +0900250 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
251 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400252 }
253
David Benjaminc11ea9422017-08-29 16:33:21 -0400254 // supported_versions is parsed in handshake_client to select the experimental
255 // TLS 1.3 version.
Steven Valdez16821262017-09-08 17:03:42 -0400256 if (have_supported_versions && !ssl_is_resumption_experiment(ssl->version)) {
Steven Valdez038da9b2017-07-10 12:57:25 -0400257 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
258 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
259 return ssl_hs_error;
260 }
261
David Benjaminffb11072016-11-13 10:32:10 +0900262 alert = SSL_AD_DECODE_ERROR;
Steven Valdez4aa154e2016-07-29 14:32:55 -0400263 if (have_pre_shared_key) {
264 if (ssl->session == NULL) {
265 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
266 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
267 return ssl_hs_error;
268 }
269
David Benjamin8baf9632016-11-17 17:11:16 +0900270 if (!ssl_ext_pre_shared_key_parse_serverhello(hs, &alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -0400271 &pre_shared_key)) {
272 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
273 return ssl_hs_error;
274 }
275
276 if (ssl->session->ssl_version != ssl->version) {
277 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
278 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
279 return ssl_hs_error;
280 }
281
David Benjamine1cc35e2016-11-16 16:25:58 +0900282 if (ssl->session->cipher->algorithm_prf != cipher->algorithm_prf) {
283 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_PRF_HASH_MISMATCH);
Steven Valdez803c77a2016-09-06 14:13:43 -0400284 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
285 return ssl_hs_error;
286 }
287
Steven Valdez4aa154e2016-07-29 14:32:55 -0400288 if (!ssl_session_is_context_valid(ssl, ssl->session)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400289 // This is actually a client application bug.
Steven Valdez4aa154e2016-07-29 14:32:55 -0400290 OPENSSL_PUT_ERROR(SSL,
291 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
292 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
293 return ssl_hs_error;
294 }
295
David Benjamin046bc1f2017-08-31 15:06:42 -0400296 ssl->s3->session_reused = true;
David Benjaminc11ea9422017-08-29 16:33:21 -0400297 // Only authentication information carries over in TLS 1.3.
David Benjamin45738dd2017-02-09 20:01:26 -0500298 hs->new_session = SSL_SESSION_dup(ssl->session, SSL_SESSION_DUP_AUTH_ONLY);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400299 if (!hs->new_session) {
Steven Valdez4aa154e2016-07-29 14:32:55 -0400300 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
301 return ssl_hs_error;
302 }
Steven Valdeze831a812017-03-09 14:56:07 -0500303 ssl_set_session(ssl, NULL);
David Benjamin17b30832017-01-28 14:00:32 -0500304
David Benjaminc11ea9422017-08-29 16:33:21 -0400305 // Resumption incorporates fresh key material, so refresh the timeout.
David Benjamin31b0c9b2017-07-20 14:49:15 -0400306 ssl_session_renew_timeout(ssl, hs->new_session.get(),
David Benjaminbe497062017-03-10 16:08:36 -0500307 ssl->session_ctx->session_psk_dhe_timeout);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900308 } else if (!ssl_get_new_session(hs, 0)) {
Steven Valdez803c77a2016-09-06 14:13:43 -0400309 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -0400310 return ssl_hs_error;
311 }
312
David Benjamin45738dd2017-02-09 20:01:26 -0500313 hs->new_session->cipher = cipher;
314 hs->new_cipher = cipher;
Steven Valdez143e8b32016-07-11 13:19:03 -0400315
David Benjaminc11ea9422017-08-29 16:33:21 -0400316 // The PRF hash is now known. Set up the key schedule.
David Benjamin6e4fc332016-11-17 16:43:08 +0900317 if (!tls13_init_key_schedule(hs)) {
David Benjamin8f820b42016-11-30 11:24:40 -0500318 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400319 }
320
David Benjaminc11ea9422017-08-29 16:33:21 -0400321 // Incorporate the PSK into the running secret.
David Benjamin8f820b42016-11-30 11:24:40 -0500322 if (ssl->s3->session_reused) {
David Benjamin45738dd2017-02-09 20:01:26 -0500323 if (!tls13_advance_key_schedule(hs, hs->new_session->master_key,
324 hs->new_session->master_key_length)) {
David Benjamin8f820b42016-11-30 11:24:40 -0500325 return ssl_hs_error;
326 }
Steven Valdez908ac192017-01-12 13:17:07 -0500327 } else if (!tls13_advance_key_schedule(hs, kZeroes, hs->hash_len)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400328 return ssl_hs_error;
329 }
330
David Benjamindb5bd722016-12-08 18:21:27 -0500331 if (!have_key_share) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400332 // We do not support psk_ke and thus always require a key share.
David Benjamindb5bd722016-12-08 18:21:27 -0500333 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE);
334 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION);
335 return ssl_hs_error;
336 }
337
David Benjaminc11ea9422017-08-29 16:33:21 -0400338 // Resolve ECDHE and incorporate it into the secret.
Steven Valdez803c77a2016-09-06 14:13:43 -0400339 uint8_t *dhe_secret;
340 size_t dhe_secret_len;
Adam Langleyc68e5b92017-02-08 13:33:15 -0800341 alert = SSL_AD_DECODE_ERROR;
David Benjamin8baf9632016-11-17 17:11:16 +0900342 if (!ssl_ext_key_share_parse_serverhello(hs, &dhe_secret, &dhe_secret_len,
Steven Valdez803c77a2016-09-06 14:13:43 -0400343 &alert, &key_share)) {
344 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
345 return ssl_hs_error;
346 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400347
David Benjamin6e4fc332016-11-17 16:43:08 +0900348 if (!tls13_advance_key_schedule(hs, dhe_secret, dhe_secret_len)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400349 OPENSSL_free(dhe_secret);
Steven Valdez803c77a2016-09-06 14:13:43 -0400350 return ssl_hs_error;
351 }
352 OPENSSL_free(dhe_secret);
353
David Benjamin7934f082017-08-01 16:32:25 -0400354 if (!ssl_hash_message(hs, msg) ||
Steven Valdez520e1222017-06-13 12:45:25 -0400355 !tls13_derive_handshake_secrets(hs)) {
356 return ssl_hs_error;
357 }
David Benjamin8f94c312017-08-01 17:35:55 -0400358
359 ssl->method->next_message(ssl);
Steven Valdez520e1222017-06-13 12:45:25 -0400360 hs->tls13_state = state_process_change_cipher_spec;
Steven Valdez16821262017-09-08 17:03:42 -0400361 return ssl_is_resumption_experiment(ssl->version)
Steven Valdez520e1222017-06-13 12:45:25 -0400362 ? ssl_hs_read_change_cipher_spec
363 : ssl_hs_ok;
364}
365
366static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
367 SSL *const ssl = hs->ssl;
368 if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->server_handshake_secret,
Steven Valdez2d850622017-01-11 11:34:52 -0500369 hs->hash_len)) {
370 return ssl_hs_error;
371 }
372
Steven Valdez520e1222017-06-13 12:45:25 -0400373 if (!hs->early_data_offered) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400374 // If not sending early data, set client traffic keys now so that alerts are
375 // encrypted.
Steven Valdezc7d4d212017-09-11 13:53:08 -0400376 if ((ssl_is_resumption_client_ccs_experiment(ssl->version) &&
Steven Valdez520e1222017-06-13 12:45:25 -0400377 !ssl3_add_change_cipher_spec(ssl)) ||
378 !tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_handshake_secret,
379 hs->hash_len)) {
380 return ssl_hs_error;
381 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400382 }
383
David Benjamin7934f082017-08-01 16:32:25 -0400384 hs->tls13_state = state_read_encrypted_extensions;
385 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400386}
387
David Benjamin7934f082017-08-01 16:32:25 -0400388static enum ssl_hs_wait_t do_read_encrypted_extensions(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900389 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400390 SSLMessage msg;
391 if (!ssl->method->get_message(ssl, &msg)) {
392 return ssl_hs_read_message;
393 }
394 if (!ssl_check_message_type(ssl, msg, SSL3_MT_ENCRYPTED_EXTENSIONS)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400395 return ssl_hs_error;
396 }
397
David Benjamin7934f082017-08-01 16:32:25 -0400398 CBS body = msg.body;
399 if (!ssl_parse_serverhello_tlsext(hs, &body)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400400 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
401 return ssl_hs_error;
402 }
David Benjamin7934f082017-08-01 16:32:25 -0400403 if (CBS_len(&body) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400404 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
405 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
406 return ssl_hs_error;
407 }
408
David Benjaminc11ea9422017-08-29 16:33:21 -0400409 // Store the negotiated ALPN in the session.
Steven Valdez2d850622017-01-11 11:34:52 -0500410 if (ssl->s3->alpn_selected != NULL) {
David Benjamind304a2f2017-07-12 23:00:28 -0400411 hs->new_session->early_alpn = (uint8_t *)BUF_memdup(
412 ssl->s3->alpn_selected, ssl->s3->alpn_selected_len);
Steven Valdez2d850622017-01-11 11:34:52 -0500413 if (hs->new_session->early_alpn == NULL) {
414 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
415 return ssl_hs_error;
416 }
417 hs->new_session->early_alpn_len = ssl->s3->alpn_selected_len;
418 }
419
420 if (ssl->early_data_accepted) {
Steven Valdeze831a812017-03-09 14:56:07 -0500421 if (hs->early_session->cipher != hs->new_session->cipher ||
422 hs->early_session->early_alpn_len != ssl->s3->alpn_selected_len ||
423 OPENSSL_memcmp(hs->early_session->early_alpn, ssl->s3->alpn_selected,
Steven Valdez2d850622017-01-11 11:34:52 -0500424 ssl->s3->alpn_selected_len) != 0) {
425 OPENSSL_PUT_ERROR(SSL, SSL_R_ALPN_MISMATCH_ON_EARLY_DATA);
426 return ssl_hs_error;
427 }
Steven Valdezf4ecc842017-08-10 14:02:56 -0400428 if (ssl->s3->tlsext_channel_id_valid || hs->received_custom_extension) {
429 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION_ON_EARLY_DATA);
Steven Valdez2a070722017-03-25 20:54:16 -0500430 return ssl_hs_error;
431 }
Steven Valdez2d850622017-01-11 11:34:52 -0500432 }
433
David Benjamin7934f082017-08-01 16:32:25 -0400434 if (!ssl_hash_message(hs, msg)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400435 return ssl_hs_error;
436 }
437
David Benjamin8f94c312017-08-01 17:35:55 -0400438 ssl->method->next_message(ssl);
David Benjamin7934f082017-08-01 16:32:25 -0400439 hs->tls13_state = state_read_certificate_request;
Steven Valdeze831a812017-03-09 14:56:07 -0500440 if (hs->in_early_data && !ssl->early_data_accepted) {
441 return ssl_hs_early_data_rejected;
442 }
443 return ssl_hs_ok;
444}
445
David Benjamin7934f082017-08-01 16:32:25 -0400446static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900447 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400448 // CertificateRequest may only be sent in non-resumption handshakes.
Steven Valdez803c77a2016-09-06 14:13:43 -0400449 if (ssl->s3->session_reused) {
David Benjamin7934f082017-08-01 16:32:25 -0400450 hs->tls13_state = state_read_server_finished;
Steven Valdez143e8b32016-07-11 13:19:03 -0400451 return ssl_hs_ok;
452 }
453
David Benjamin7934f082017-08-01 16:32:25 -0400454 SSLMessage msg;
455 if (!ssl->method->get_message(ssl, &msg)) {
456 return ssl_hs_read_message;
457 }
458
David Benjaminc11ea9422017-08-29 16:33:21 -0400459 // CertificateRequest is optional.
David Benjamin7934f082017-08-01 16:32:25 -0400460 if (msg.type != SSL3_MT_CERTIFICATE_REQUEST) {
461 hs->tls13_state = state_read_server_certificate;
Steven Valdez143e8b32016-07-11 13:19:03 -0400462 return ssl_hs_ok;
463 }
464
David Benjamin7934f082017-08-01 16:32:25 -0400465 CBS body = msg.body, context, supported_signature_algorithms;
466 if (!CBS_get_u8_length_prefixed(&body, &context) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400467 // The request context is always empty during the handshake.
David Benjamin8a8349b2016-08-18 02:32:23 -0400468 CBS_len(&context) != 0 ||
David Benjamin7934f082017-08-01 16:32:25 -0400469 !CBS_get_u16_length_prefixed(&body, &supported_signature_algorithms) ||
David Benjamin48901652016-08-01 12:12:47 -0400470 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900471 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400472 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
473 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
474 return ssl_hs_error;
475 }
476
Adam Langleyc68e5b92017-02-08 13:33:15 -0800477 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin31b0c9b2017-07-20 14:49:15 -0400478 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names =
David Benjamin7934f082017-08-01 16:32:25 -0400479 ssl_parse_client_CA_list(ssl, &alert, &body);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400480 if (!ca_names) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400481 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
482 return ssl_hs_error;
483 }
484
David Benjaminc11ea9422017-08-29 16:33:21 -0400485 // Ignore extensions.
Steven Valdez143e8b32016-07-11 13:19:03 -0400486 CBS extensions;
David Benjamin7934f082017-08-01 16:32:25 -0400487 if (!CBS_get_u16_length_prefixed(&body, &extensions) ||
488 CBS_len(&body) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400489 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
490 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
491 return ssl_hs_error;
492 }
493
David Benjaminfd45ee72017-08-31 14:49:09 -0400494 hs->cert_request = true;
David Benjamin31b0c9b2017-07-20 14:49:15 -0400495 hs->ca_names = std::move(ca_names);
Adam Langley34b4c822017-02-02 10:57:17 -0800496 ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400497
David Benjamin7934f082017-08-01 16:32:25 -0400498 if (!ssl_hash_message(hs, msg)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400499 return ssl_hs_error;
500 }
501
David Benjamin8f94c312017-08-01 17:35:55 -0400502 ssl->method->next_message(ssl);
David Benjamin7934f082017-08-01 16:32:25 -0400503 hs->tls13_state = state_read_server_certificate;
504 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400505}
506
David Benjamin7934f082017-08-01 16:32:25 -0400507static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900508 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400509 SSLMessage msg;
510 if (!ssl->method->get_message(ssl, &msg)) {
511 return ssl_hs_read_message;
512 }
513 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
514 !tls13_process_certificate(hs, msg, 0 /* certificate required */) ||
515 !ssl_hash_message(hs, msg)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400516 return ssl_hs_error;
517 }
518
David Benjamin8f94c312017-08-01 17:35:55 -0400519 ssl->method->next_message(ssl);
David Benjamin7934f082017-08-01 16:32:25 -0400520 hs->tls13_state = state_read_server_certificate_verify;
521 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400522}
523
David Benjamin7934f082017-08-01 16:32:25 -0400524static enum ssl_hs_wait_t do_read_server_certificate_verify(
David Benjaminc3c88822016-11-14 10:32:04 +0900525 SSL_HANDSHAKE *hs) {
526 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400527 SSLMessage msg;
528 if (!ssl->method->get_message(ssl, &msg)) {
529 return ssl_hs_read_message;
530 }
David Benjamin3a1dd462017-07-11 16:13:10 -0400531 switch (ssl_verify_peer_cert(hs)) {
532 case ssl_verify_ok:
533 break;
534 case ssl_verify_invalid:
535 return ssl_hs_error;
536 case ssl_verify_retry:
David Benjamin7934f082017-08-01 16:32:25 -0400537 hs->tls13_state = state_read_server_certificate_verify;
David Benjamin3a1dd462017-07-11 16:13:10 -0400538 return ssl_hs_certificate_verify;
539 }
540
David Benjamin7934f082017-08-01 16:32:25 -0400541 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY) ||
542 !tls13_process_certificate_verify(hs, msg) ||
543 !ssl_hash_message(hs, msg)) {
David Benjamin6929f272016-11-16 19:10:08 +0900544 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400545 }
546
David Benjamin8f94c312017-08-01 17:35:55 -0400547 ssl->method->next_message(ssl);
David Benjamin7934f082017-08-01 16:32:25 -0400548 hs->tls13_state = state_read_server_finished;
549 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400550}
551
David Benjamin7934f082017-08-01 16:32:25 -0400552static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900553 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400554 SSLMessage msg;
555 if (!ssl->method->get_message(ssl, &msg)) {
556 return ssl_hs_read_message;
557 }
558 if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED) ||
559 !tls13_process_finished(hs, msg, 0 /* don't use saved value */) ||
560 !ssl_hash_message(hs, msg) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400561 // Update the secret to the master secret and derive traffic keys.
David Benjamin6e4fc332016-11-17 16:43:08 +0900562 !tls13_advance_key_schedule(hs, kZeroes, hs->hash_len) ||
563 !tls13_derive_application_secrets(hs)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400564 return ssl_hs_error;
565 }
566
David Benjamin8f94c312017-08-01 17:35:55 -0400567 ssl->method->next_message(ssl);
Steven Valdez2d850622017-01-11 11:34:52 -0500568 hs->tls13_state = state_send_end_of_early_data;
569 return ssl_hs_ok;
570}
571
572static enum ssl_hs_wait_t do_send_end_of_early_data(SSL_HANDSHAKE *hs) {
573 SSL *const ssl = hs->ssl;
Steven Valdeze831a812017-03-09 14:56:07 -0500574
575 if (ssl->early_data_accepted) {
David Benjaminfd45ee72017-08-31 14:49:09 -0400576 hs->can_early_write = false;
Steven Valdeze831a812017-03-09 14:56:07 -0500577 if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING,
578 TLS1_AD_END_OF_EARLY_DATA)) {
579 return ssl_hs_error;
580 }
Steven Valdez2d850622017-01-11 11:34:52 -0500581 }
582
Steven Valdez520e1222017-06-13 12:45:25 -0400583 if (hs->early_data_offered) {
Steven Valdezc7d4d212017-09-11 13:53:08 -0400584 if ((ssl_is_resumption_client_ccs_experiment(ssl->version) &&
Steven Valdez520e1222017-06-13 12:45:25 -0400585 !ssl3_add_change_cipher_spec(ssl)) ||
586 !tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_handshake_secret,
587 hs->hash_len)) {
588 return ssl_hs_error;
589 }
Steven Valdez2d850622017-01-11 11:34:52 -0500590 }
591
David Benjamin5edfc8c2016-12-10 15:46:58 -0500592 hs->tls13_state = state_send_client_certificate;
Steven Valdez143e8b32016-07-11 13:19:03 -0400593 return ssl_hs_ok;
594}
595
David Benjamin5edfc8c2016-12-10 15:46:58 -0500596static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900597 SSL *const ssl = hs->ssl;
Steven Valdez2d850622017-01-11 11:34:52 -0500598
David Benjaminc11ea9422017-08-29 16:33:21 -0400599 // The peer didn't request a certificate.
David Benjaminc3c88822016-11-14 10:32:04 +0900600 if (!hs->cert_request) {
David Benjamin81b7bc32017-01-12 19:44:57 -0500601 hs->tls13_state = state_complete_second_flight;
Steven Valdez143e8b32016-07-11 13:19:03 -0400602 return ssl_hs_ok;
603 }
604
David Benjaminc11ea9422017-08-29 16:33:21 -0400605 // Call cert_cb to update the certificate.
Steven Valdez143e8b32016-07-11 13:19:03 -0400606 if (ssl->cert->cert_cb != NULL) {
607 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
608 if (rv == 0) {
609 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
610 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
611 return ssl_hs_error;
612 }
613 if (rv < 0) {
David Benjamin3977f302016-12-11 13:30:41 -0500614 hs->tls13_state = state_send_client_certificate;
Steven Valdez143e8b32016-07-11 13:19:03 -0400615 return ssl_hs_x509_lookup;
616 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400617 }
618
David Benjamina232a712017-03-30 15:51:53 -0500619 if (!ssl_on_certificate_selected(hs) ||
David Benjamin0f24bed2017-01-12 19:46:50 -0500620 !tls13_add_certificate(hs)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400621 return ssl_hs_error;
622 }
623
David Benjamin3977f302016-12-11 13:30:41 -0500624 hs->tls13_state = state_send_client_certificate_verify;
David Benjamin25ac2512017-01-12 19:31:28 -0500625 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400626}
627
David Benjamin44148742017-06-17 13:20:59 -0400628static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900629 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400630 // Don't send CertificateVerify if there is no certificate.
Steven Valdez143e8b32016-07-11 13:19:03 -0400631 if (!ssl_has_certificate(ssl)) {
David Benjamin81b7bc32017-01-12 19:44:57 -0500632 hs->tls13_state = state_complete_second_flight;
Steven Valdez143e8b32016-07-11 13:19:03 -0400633 return ssl_hs_ok;
634 }
635
David Benjamin44148742017-06-17 13:20:59 -0400636 switch (tls13_add_certificate_verify(hs)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400637 case ssl_private_key_success:
David Benjamin81b7bc32017-01-12 19:44:57 -0500638 hs->tls13_state = state_complete_second_flight;
David Benjamin25ac2512017-01-12 19:31:28 -0500639 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400640
641 case ssl_private_key_retry:
David Benjamin44148742017-06-17 13:20:59 -0400642 hs->tls13_state = state_send_client_certificate_verify;
Steven Valdez143e8b32016-07-11 13:19:03 -0400643 return ssl_hs_private_key_operation;
644
645 case ssl_private_key_failure:
646 return ssl_hs_error;
647 }
648
649 assert(0);
650 return ssl_hs_error;
651}
652
David Benjamin81b7bc32017-01-12 19:44:57 -0500653static enum ssl_hs_wait_t do_complete_second_flight(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900654 SSL *const ssl = hs->ssl;
David Benjamin81b7bc32017-01-12 19:44:57 -0500655
David Benjaminc11ea9422017-08-29 16:33:21 -0400656 // Send a Channel ID assertion if necessary.
David Benjamin81b7bc32017-01-12 19:44:57 -0500657 if (ssl->s3->tlsext_channel_id_valid) {
658 if (!ssl_do_channel_id_callback(ssl)) {
659 hs->tls13_state = state_complete_second_flight;
660 return ssl_hs_error;
661 }
662
663 if (ssl->tlsext_channel_id_private == NULL) {
664 return ssl_hs_channel_id_lookup;
665 }
666
David Benjamin1386aad2017-07-19 23:57:40 -0400667 ScopedCBB cbb;
668 CBB body;
669 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) ||
Steven Valdez908ac192017-01-12 13:17:07 -0500670 !tls1_write_channel_id(hs, &body) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400671 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin81b7bc32017-01-12 19:44:57 -0500672 return ssl_hs_error;
673 }
Nick Harper60a85cb2016-09-23 16:25:11 -0700674 }
675
David Benjaminc11ea9422017-08-29 16:33:21 -0400676 // Send a Finished message.
David Benjamin0f24bed2017-01-12 19:46:50 -0500677 if (!tls13_add_finished(hs)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400678 return ssl_hs_error;
679 }
680
David Benjaminc11ea9422017-08-29 16:33:21 -0400681 // Derive the final keys and enable them.
Steven Valdeza833c352016-11-01 13:39:36 -0400682 if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->server_traffic_secret_0,
683 hs->hash_len) ||
684 !tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_traffic_secret_0,
685 hs->hash_len) ||
David Benjamin6e4fc332016-11-17 16:43:08 +0900686 !tls13_derive_resumption_secret(hs)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400687 return ssl_hs_error;
688 }
689
David Benjamin3977f302016-12-11 13:30:41 -0500690 hs->tls13_state = state_done;
Steven Valdez143e8b32016-07-11 13:19:03 -0400691 return ssl_hs_flush;
692}
693
David Benjaminc3c88822016-11-14 10:32:04 +0900694enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs) {
David Benjamin3977f302016-12-11 13:30:41 -0500695 while (hs->tls13_state != state_done) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400696 enum ssl_hs_wait_t ret = ssl_hs_error;
David Benjamind304a2f2017-07-12 23:00:28 -0400697 enum client_hs_state_t state =
698 static_cast<enum client_hs_state_t>(hs->tls13_state);
Steven Valdez143e8b32016-07-11 13:19:03 -0400699 switch (state) {
David Benjamin7934f082017-08-01 16:32:25 -0400700 case state_read_hello_retry_request:
701 ret = do_read_hello_retry_request(hs);
Steven Valdez5440fe02016-07-18 12:40:30 -0400702 break;
703 case state_send_second_client_hello:
David Benjaminc3c88822016-11-14 10:32:04 +0900704 ret = do_send_second_client_hello(hs);
Steven Valdez5440fe02016-07-18 12:40:30 -0400705 break;
David Benjamin7934f082017-08-01 16:32:25 -0400706 case state_read_server_hello:
707 ret = do_read_server_hello(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400708 break;
Steven Valdez520e1222017-06-13 12:45:25 -0400709 case state_process_change_cipher_spec:
710 ret = do_process_change_cipher_spec(hs);
711 break;
David Benjamin7934f082017-08-01 16:32:25 -0400712 case state_read_encrypted_extensions:
713 ret = do_read_encrypted_extensions(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400714 break;
David Benjamin7934f082017-08-01 16:32:25 -0400715 case state_read_certificate_request:
716 ret = do_read_certificate_request(hs);
Steven Valdeze831a812017-03-09 14:56:07 -0500717 break;
David Benjamin7934f082017-08-01 16:32:25 -0400718 case state_read_server_certificate:
719 ret = do_read_server_certificate(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400720 break;
David Benjamin7934f082017-08-01 16:32:25 -0400721 case state_read_server_certificate_verify:
722 ret = do_read_server_certificate_verify(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400723 break;
David Benjamin7934f082017-08-01 16:32:25 -0400724 case state_read_server_finished:
725 ret = do_read_server_finished(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400726 break;
Steven Valdez2d850622017-01-11 11:34:52 -0500727 case state_send_end_of_early_data:
728 ret = do_send_end_of_early_data(hs);
729 break;
Steven Valdez143e8b32016-07-11 13:19:03 -0400730 case state_send_client_certificate:
David Benjaminc3c88822016-11-14 10:32:04 +0900731 ret = do_send_client_certificate(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400732 break;
733 case state_send_client_certificate_verify:
David Benjamin44148742017-06-17 13:20:59 -0400734 ret = do_send_client_certificate_verify(hs);
Nick Harper60a85cb2016-09-23 16:25:11 -0700735 break;
David Benjamin81b7bc32017-01-12 19:44:57 -0500736 case state_complete_second_flight:
737 ret = do_complete_second_flight(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400738 break;
Steven Valdez143e8b32016-07-11 13:19:03 -0400739 case state_done:
740 ret = ssl_hs_ok;
741 break;
742 }
743
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400744 if (hs->tls13_state != state) {
David Benjaminf60bcfb2017-08-18 15:23:44 -0400745 ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1);
746 }
747
Steven Valdez143e8b32016-07-11 13:19:03 -0400748 if (ret != ssl_hs_ok) {
749 return ret;
750 }
751 }
752
753 return ssl_hs_ok;
754}
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400755
David Benjaminf60bcfb2017-08-18 15:23:44 -0400756const char *tls13_client_handshake_state(SSL_HANDSHAKE *hs) {
757 enum client_hs_state_t state =
758 static_cast<enum client_hs_state_t>(hs->tls13_state);
759 switch (state) {
760 case state_read_hello_retry_request:
761 return "TLS 1.3 client read_hello_retry_request";
762 case state_send_second_client_hello:
763 return "TLS 1.3 client send_second_client_hello";
764 case state_read_server_hello:
765 return "TLS 1.3 client read_server_hello";
766 case state_process_change_cipher_spec:
767 return "TLS 1.3 client process_change_cipher_spec";
768 case state_read_encrypted_extensions:
769 return "TLS 1.3 client read_encrypted_extensions";
770 case state_read_certificate_request:
771 return "TLS 1.3 client read_certificate_request";
772 case state_read_server_certificate:
773 return "TLS 1.3 client read_server_certificate";
774 case state_read_server_certificate_verify:
775 return "TLS 1.3 client read_server_certificate_verify";
776 case state_read_server_finished:
777 return "TLS 1.3 client read_server_finished";
778 case state_send_end_of_early_data:
779 return "TLS 1.3 client send_end_of_early_data";
780 case state_send_client_certificate:
781 return "TLS 1.3 client send_client_certificate";
782 case state_send_client_certificate_verify:
783 return "TLS 1.3 client send_client_certificate_verify";
784 case state_complete_second_flight:
785 return "TLS 1.3 client complete_second_flight";
786 case state_done:
787 return "TLS 1.3 client done";
788 }
789
790 return "TLS 1.3 client unknown";
791}
792
David Benjamin7934f082017-08-01 16:32:25 -0400793int tls13_process_new_session_ticket(SSL *ssl, const SSLMessage &msg) {
David Benjamin86e95b82017-07-18 16:34:25 -0400794 UniquePtr<SSL_SESSION> session(SSL_SESSION_dup(ssl->s3->established_session,
795 SSL_SESSION_INCLUDE_NONAUTH));
David Benjamind304a2f2017-07-12 23:00:28 -0400796 if (!session) {
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400797 return 0;
798 }
799
David Benjamind304a2f2017-07-12 23:00:28 -0400800 ssl_session_rebase_time(ssl, session.get());
David Benjamin123db572016-11-03 16:59:25 -0400801
David Benjamin17b30832017-01-28 14:00:32 -0500802 uint32_t server_timeout;
David Benjamin7934f082017-08-01 16:32:25 -0400803 CBS body = msg.body, ticket, extensions;
804 if (!CBS_get_u32(&body, &server_timeout) ||
805 !CBS_get_u32(&body, &session->ticket_age_add) ||
806 !CBS_get_u16_length_prefixed(&body, &ticket) ||
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400807 !CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen) ||
David Benjamin7934f082017-08-01 16:32:25 -0400808 !CBS_get_u16_length_prefixed(&body, &extensions) ||
809 CBS_len(&body) != 0) {
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400810 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
811 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind304a2f2017-07-12 23:00:28 -0400812 return 0;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400813 }
814
David Benjaminc11ea9422017-08-29 16:33:21 -0400815 // Cap the renewable lifetime by the server advertised value. This avoids
816 // wasting bandwidth on 0-RTT when we know the server will reject it.
David Benjaminad8f5e12017-02-20 17:00:20 -0500817 if (session->timeout > server_timeout) {
818 session->timeout = server_timeout;
David Benjamin17b30832017-01-28 14:00:32 -0500819 }
820
David Benjaminc11ea9422017-08-29 16:33:21 -0400821 // Parse out the extensions.
David Benjamin74795b32017-08-31 15:13:12 -0400822 bool have_early_data_info = false;
Steven Valdez08b65f42016-12-07 15:29:45 -0500823 CBS early_data_info;
824 const SSL_EXTENSION_TYPE ext_types[] = {
825 {TLSEXT_TYPE_ticket_early_data_info, &have_early_data_info,
826 &early_data_info},
827 };
828
Adam Langleyc68e5b92017-02-08 13:33:15 -0800829 uint8_t alert = SSL_AD_DECODE_ERROR;
Steven Valdez08b65f42016-12-07 15:29:45 -0500830 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
831 OPENSSL_ARRAY_SIZE(ext_types),
832 1 /* ignore unknown */)) {
833 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamind304a2f2017-07-12 23:00:28 -0400834 return 0;
Steven Valdez08b65f42016-12-07 15:29:45 -0500835 }
836
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -0500837 if (have_early_data_info && ssl->cert->enable_early_data) {
Steven Valdez08b65f42016-12-07 15:29:45 -0500838 if (!CBS_get_u32(&early_data_info, &session->ticket_max_early_data) ||
839 CBS_len(&early_data_info) != 0) {
840 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin9c33ae82017-01-08 06:04:43 -0500841 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind304a2f2017-07-12 23:00:28 -0400842 return 0;
Steven Valdez08b65f42016-12-07 15:29:45 -0500843 }
844 }
845
Steven Valdeza833c352016-11-01 13:39:36 -0400846 session->ticket_age_add_valid = 1;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400847 session->not_resumable = 0;
848
David Benjamin3d8f0802017-09-06 16:12:52 -0400849 if ((ssl->ctx->session_cache_mode & SSL_SESS_CACHE_CLIENT) &&
850 ssl->ctx->new_session_cb != NULL &&
David Benjamind304a2f2017-07-12 23:00:28 -0400851 ssl->ctx->new_session_cb(ssl, session.get())) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400852 // |new_session_cb|'s return value signals that it took ownership.
David Benjamind304a2f2017-07-12 23:00:28 -0400853 session.release();
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400854 }
855
David Benjamind304a2f2017-07-12 23:00:28 -0400856 return 1;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400857}
David Benjamin4fe3c902016-08-16 02:17:03 -0400858
David Benjamin6e4fc332016-11-17 16:43:08 +0900859void ssl_clear_tls13_state(SSL_HANDSHAKE *hs) {
David Benjaminc642aca2017-07-19 23:28:43 -0400860 hs->key_share.reset();
David Benjamin4fe3c902016-08-16 02:17:03 -0400861
David Benjamin6e4fc332016-11-17 16:43:08 +0900862 OPENSSL_free(hs->key_share_bytes);
863 hs->key_share_bytes = NULL;
864 hs->key_share_bytes_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -0400865}
David Benjamin86e95b82017-07-18 16:34:25 -0400866
867} // namespace bssl