blob: 4cc7e604d0c56d0b41079820ca6b8d5b3a122647 [file] [log] [blame]
David Benjaminc895d6b2016-08-11 13:26:41 -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>
Robert Sloan4d1ac502017-02-06 08:36:14 -080018#include <limits.h>
David Benjaminc895d6b2016-08-11 13:26:41 -040019#include <string.h>
20
Robert Sloanb6d070c2017-07-24 08:40:01 -070021#include <utility>
22
David Benjaminc895d6b2016-08-11 13:26:41 -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>
David Benjaminc895d6b2016-08-11 13:26:41 -040028
Steven Valdez909b19f2016-11-21 15:35:44 -050029#include "../crypto/internal.h"
David Benjaminc895d6b2016-08-11 13:26:41 -040030#include "internal.h"
31
32
Robert Sloanb6d070c2017-07-24 08:40:01 -070033namespace bssl {
34
David Benjaminc895d6b2016-08-11 13:26:41 -040035enum client_hs_state_t {
36 state_process_hello_retry_request = 0,
37 state_send_second_client_hello,
David Benjaminc895d6b2016-08-11 13:26:41 -040038 state_process_server_hello,
Robert Sloana12bf462017-07-17 07:08:26 -070039 state_process_change_cipher_spec,
David Benjaminc895d6b2016-08-11 13:26:41 -040040 state_process_encrypted_extensions,
Robert Sloane56da3e2017-06-26 08:26:42 -070041 state_continue_second_server_flight,
David Benjaminc895d6b2016-08-11 13:26:41 -040042 state_process_certificate_request,
43 state_process_server_certificate,
44 state_process_server_certificate_verify,
45 state_process_server_finished,
Robert Sloan6d0d00e2017-03-27 07:13:07 -070046 state_send_end_of_early_data,
David Benjaminc895d6b2016-08-11 13:26:41 -040047 state_send_client_certificate,
48 state_send_client_certificate_verify,
Robert Sloan4d1ac502017-02-06 08:36:14 -080049 state_complete_second_flight,
David Benjaminc895d6b2016-08-11 13:26:41 -040050 state_done,
51};
52
David Benjamin1b249672016-12-06 18:25:50 -050053static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0};
54
55static enum ssl_hs_wait_t do_process_hello_retry_request(SSL_HANDSHAKE *hs) {
56 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -040057 if (ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) {
Steven Valdeze7531f02016-12-14 13:29:57 -050058 hs->tls13_state = state_process_server_hello;
David Benjaminc895d6b2016-08-11 13:26:41 -040059 return ssl_hs_ok;
60 }
61
62 CBS cbs, extensions;
Robert Sloana12bf462017-07-17 07:08:26 -070063 uint16_t server_version;
David Benjaminc895d6b2016-08-11 13:26:41 -040064 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Robert Sloana12bf462017-07-17 07:08:26 -070065 if (!CBS_get_u16(&cbs, &server_version) ||
David Benjaminc895d6b2016-08-11 13:26:41 -040066 !CBS_get_u16_length_prefixed(&cbs, &extensions) ||
David Benjamin95add822016-10-19 01:09:12 -040067 /* HelloRetryRequest may not be empty. */
68 CBS_len(&extensions) == 0 ||
David Benjaminc895d6b2016-08-11 13:26:41 -040069 CBS_len(&cbs) != 0) {
David Benjamin95add822016-10-19 01:09:12 -040070 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -040071 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
72 return ssl_hs_error;
73 }
74
Steven Valdez909b19f2016-11-21 15:35:44 -050075 int have_cookie, have_key_share;
76 CBS cookie, key_share;
77 const SSL_EXTENSION_TYPE ext_types[] = {
78 {TLSEXT_TYPE_key_share, &have_key_share, &key_share},
79 {TLSEXT_TYPE_cookie, &have_cookie, &cookie},
80 };
81
Robert Sloan5d625782017-02-13 09:55:39 -080082 uint8_t alert = SSL_AD_DECODE_ERROR;
Steven Valdez909b19f2016-11-21 15:35:44 -050083 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
Robert Sloan69939df2017-01-09 10:53:07 -080084 OPENSSL_ARRAY_SIZE(ext_types),
85 0 /* reject unknown */)) {
Steven Valdez909b19f2016-11-21 15:35:44 -050086 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
87 return ssl_hs_error;
88 }
89
90 if (have_cookie) {
91 CBS cookie_value;
92 if (!CBS_get_u16_length_prefixed(&cookie, &cookie_value) ||
93 CBS_len(&cookie_value) == 0 ||
94 CBS_len(&cookie) != 0) {
David Benjamin95add822016-10-19 01:09:12 -040095 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
96 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
97 return ssl_hs_error;
98 }
David Benjaminc895d6b2016-08-11 13:26:41 -040099
Steven Valdez909b19f2016-11-21 15:35:44 -0500100 if (!CBS_stow(&cookie_value, &hs->cookie, &hs->cookie_len)) {
101 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400102 }
103 }
104
Steven Valdez909b19f2016-11-21 15:35:44 -0500105 if (have_key_share) {
106 uint16_t group_id;
107 if (!CBS_get_u16(&key_share, &group_id) || CBS_len(&key_share) != 0) {
108 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
109 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
110 return ssl_hs_error;
111 }
112
113 /* The group must be supported. */
114 const uint16_t *groups;
115 size_t groups_len;
116 tls1_get_grouplist(ssl, &groups, &groups_len);
117 int found = 0;
118 for (size_t i = 0; i < groups_len; i++) {
119 if (groups[i] == group_id) {
120 found = 1;
121 break;
122 }
123 }
124
125 if (!found) {
126 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
127 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
128 return ssl_hs_error;
129 }
130
131 /* Check that the HelloRetryRequest does not request the key share that
132 * was provided in the initial ClientHello. */
Robert Sloanb6d070c2017-07-24 08:40:01 -0700133 if (hs->key_share->GroupID() == group_id) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500134 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
135 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
136 return ssl_hs_error;
137 }
138
Robert Sloanb6d070c2017-07-24 08:40:01 -0700139 hs->key_share.reset();
Steven Valdez909b19f2016-11-21 15:35:44 -0500140 hs->retry_group = group_id;
141 }
142
Robert Sloan5d625782017-02-13 09:55:39 -0800143 if (!ssl_hash_current_message(hs)) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800144 return ssl_hs_error;
145 }
146
David Benjamin95add822016-10-19 01:09:12 -0400147 hs->received_hello_retry_request = 1;
Steven Valdeze7531f02016-12-14 13:29:57 -0500148 hs->tls13_state = state_send_second_client_hello;
Robert Sloane56da3e2017-06-26 08:26:42 -0700149 /* 0-RTT is rejected if we receive a HelloRetryRequest. */
150 if (hs->in_early_data) {
151 return ssl_hs_early_data_rejected;
152 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400153 return ssl_hs_ok;
154}
155
David Benjamin1b249672016-12-06 18:25:50 -0500156static enum ssl_hs_wait_t do_send_second_client_hello(SSL_HANDSHAKE *hs) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700157 SSL *const ssl = hs->ssl;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700158 /* Restore the null cipher. We may have switched due to 0-RTT. */
159 bssl::UniquePtr<SSLAEADContext> null_ctx = SSLAEADContext::CreateNullCipher();
160 if (!null_ctx ||
161 !ssl->method->set_write_state(ssl, std::move(null_ctx)) ||
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700162 !ssl_write_client_hello(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400163 return ssl_hs_error;
164 }
165
Steven Valdeze7531f02016-12-14 13:29:57 -0500166 hs->tls13_state = state_process_server_hello;
David Benjaminc895d6b2016-08-11 13:26:41 -0400167 return ssl_hs_flush_and_read_message;
168}
169
David Benjamin1b249672016-12-06 18:25:50 -0500170static enum ssl_hs_wait_t do_process_server_hello(SSL_HANDSHAKE *hs) {
171 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800172 if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400173 return ssl_hs_error;
174 }
175
Robert Sloana12bf462017-07-17 07:08:26 -0700176 CBS cbs, server_random, session_id, extensions;
177 uint16_t server_version;
David Benjaminc895d6b2016-08-11 13:26:41 -0400178 uint16_t cipher_suite;
Robert Sloana12bf462017-07-17 07:08:26 -0700179 uint8_t compression_method;
David Benjaminc895d6b2016-08-11 13:26:41 -0400180 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Robert Sloana12bf462017-07-17 07:08:26 -0700181 if (!CBS_get_u16(&cbs, &server_version) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400182 !CBS_get_bytes(&cbs, &server_random, SSL3_RANDOM_SIZE) ||
Robert Sloana12bf462017-07-17 07:08:26 -0700183 (ssl->version == TLS1_3_EXPERIMENT_VERSION &&
184 !CBS_get_u8_length_prefixed(&cbs, &session_id)) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400185 !CBS_get_u16(&cbs, &cipher_suite) ||
Robert Sloana12bf462017-07-17 07:08:26 -0700186 (ssl->version == TLS1_3_EXPERIMENT_VERSION &&
187 (!CBS_get_u8(&cbs, &compression_method) || compression_method != 0)) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400188 !CBS_get_u16_length_prefixed(&cbs, &extensions) ||
189 CBS_len(&cbs) != 0) {
190 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
191 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
192 return ssl_hs_error;
193 }
194
Robert Sloana12bf462017-07-17 07:08:26 -0700195 uint16_t expected_version =
196 ssl->version == TLS1_3_EXPERIMENT_VERSION ? TLS1_2_VERSION : ssl->version;
197 if (server_version != expected_version) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400198 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
199 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
200 return ssl_hs_error;
201 }
202
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400203 assert(ssl->s3->have_version);
Robert Sloan69939df2017-01-09 10:53:07 -0800204 OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
205 SSL3_RANDOM_SIZE);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400206
207 const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
208 if (cipher == NULL) {
209 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
210 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
211 return ssl_hs_error;
212 }
213
Steven Valdez909b19f2016-11-21 15:35:44 -0500214 /* Check if the cipher is a TLS 1.3 cipher. */
215 if (SSL_CIPHER_get_min_version(cipher) > ssl3_protocol_version(ssl) ||
216 SSL_CIPHER_get_max_version(cipher) < ssl3_protocol_version(ssl)) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400217 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
218 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
219 return ssl_hs_error;
220 }
221
David Benjaminc895d6b2016-08-11 13:26:41 -0400222 /* Parse out the extensions. */
Robert Sloana12bf462017-07-17 07:08:26 -0700223 int have_key_share = 0, have_pre_shared_key = 0, have_supported_versions = 0;
224 CBS key_share, pre_shared_key, supported_versions;
Steven Valdez909b19f2016-11-21 15:35:44 -0500225 const SSL_EXTENSION_TYPE ext_types[] = {
226 {TLSEXT_TYPE_key_share, &have_key_share, &key_share},
227 {TLSEXT_TYPE_pre_shared_key, &have_pre_shared_key, &pre_shared_key},
Robert Sloana12bf462017-07-17 07:08:26 -0700228 {TLSEXT_TYPE_supported_versions, &have_supported_versions,
229 &supported_versions},
Steven Valdez909b19f2016-11-21 15:35:44 -0500230 };
David Benjaminc895d6b2016-08-11 13:26:41 -0400231
Robert Sloan5d625782017-02-13 09:55:39 -0800232 uint8_t alert = SSL_AD_DECODE_ERROR;
Steven Valdez909b19f2016-11-21 15:35:44 -0500233 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
Robert Sloan69939df2017-01-09 10:53:07 -0800234 OPENSSL_ARRAY_SIZE(ext_types),
235 0 /* reject unknown */)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500236 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
237 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400238 }
239
Robert Sloana12bf462017-07-17 07:08:26 -0700240 /* supported_versions is parsed in handshake_client to select the experimental
241 * TLS 1.3 version. */
242 if (have_supported_versions && ssl->version != TLS1_3_EXPERIMENT_VERSION) {
243 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
244 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
245 return ssl_hs_error;
246 }
247
Steven Valdez909b19f2016-11-21 15:35:44 -0500248 alert = SSL_AD_DECODE_ERROR;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400249 if (have_pre_shared_key) {
250 if (ssl->session == NULL) {
251 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
252 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
253 return ssl_hs_error;
254 }
255
David Benjamin1b249672016-12-06 18:25:50 -0500256 if (!ssl_ext_pre_shared_key_parse_serverhello(hs, &alert,
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400257 &pre_shared_key)) {
258 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
259 return ssl_hs_error;
260 }
261
262 if (ssl->session->ssl_version != ssl->version) {
263 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
264 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
265 return ssl_hs_error;
266 }
267
Steven Valdez909b19f2016-11-21 15:35:44 -0500268 if (ssl->session->cipher->algorithm_prf != cipher->algorithm_prf) {
269 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_PRF_HASH_MISMATCH);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400270 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
271 return ssl_hs_error;
272 }
273
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400274 if (!ssl_session_is_context_valid(ssl, ssl->session)) {
275 /* This is actually a client application bug. */
276 OPENSSL_PUT_ERROR(SSL,
277 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
278 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
279 return ssl_hs_error;
280 }
281
282 ssl->s3->session_reused = 1;
283 /* Only authentication information carries over in TLS 1.3. */
Robert Sloana94fe052017-02-21 08:49:28 -0800284 hs->new_session = SSL_SESSION_dup(ssl->session, SSL_SESSION_DUP_AUTH_ONLY);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700285 if (!hs->new_session) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400286 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
287 return ssl_hs_error;
288 }
Robert Sloane56da3e2017-06-26 08:26:42 -0700289 ssl_set_session(ssl, NULL);
Robert Sloan4d1ac502017-02-06 08:36:14 -0800290
291 /* Resumption incorporates fresh key material, so refresh the timeout. */
Robert Sloanb6d070c2017-07-24 08:40:01 -0700292 ssl_session_renew_timeout(ssl, hs->new_session.get(),
Robert Sloan1c9db532017-03-13 08:03:59 -0700293 ssl->session_ctx->session_psk_dhe_timeout);
David Benjamin1b249672016-12-06 18:25:50 -0500294 } else if (!ssl_get_new_session(hs, 0)) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400295 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -0400296 return ssl_hs_error;
297 }
298
Robert Sloana94fe052017-02-21 08:49:28 -0800299 hs->new_session->cipher = cipher;
300 hs->new_cipher = cipher;
301
David Benjaminc895d6b2016-08-11 13:26:41 -0400302 /* The PRF hash is now known. Set up the key schedule. */
David Benjamin1b249672016-12-06 18:25:50 -0500303 if (!tls13_init_key_schedule(hs)) {
304 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400305 }
306
David Benjamin1b249672016-12-06 18:25:50 -0500307 /* Incorporate the PSK into the running secret. */
308 if (ssl->s3->session_reused) {
Robert Sloana94fe052017-02-21 08:49:28 -0800309 if (!tls13_advance_key_schedule(hs, hs->new_session->master_key,
310 hs->new_session->master_key_length)) {
David Benjamin1b249672016-12-06 18:25:50 -0500311 return ssl_hs_error;
312 }
Robert Sloan5d625782017-02-13 09:55:39 -0800313 } else if (!tls13_advance_key_schedule(hs, kZeroes, hs->hash_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400314 return ssl_hs_error;
315 }
316
Steven Valdeze7531f02016-12-14 13:29:57 -0500317 if (!have_key_share) {
318 /* We do not support psk_ke and thus always require a key share. */
319 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE);
320 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION);
321 return ssl_hs_error;
322 }
323
David Benjaminc895d6b2016-08-11 13:26:41 -0400324 /* Resolve ECDHE and incorporate it into the secret. */
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400325 uint8_t *dhe_secret;
326 size_t dhe_secret_len;
Robert Sloan5d625782017-02-13 09:55:39 -0800327 alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -0500328 if (!ssl_ext_key_share_parse_serverhello(hs, &dhe_secret, &dhe_secret_len,
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400329 &alert, &key_share)) {
330 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
331 return ssl_hs_error;
332 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400333
David Benjamin1b249672016-12-06 18:25:50 -0500334 if (!tls13_advance_key_schedule(hs, dhe_secret, dhe_secret_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400335 OPENSSL_free(dhe_secret);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400336 return ssl_hs_error;
337 }
338 OPENSSL_free(dhe_secret);
339
Robert Sloan5d625782017-02-13 09:55:39 -0800340 if (!ssl_hash_current_message(hs) ||
Robert Sloana12bf462017-07-17 07:08:26 -0700341 !tls13_derive_handshake_secrets(hs)) {
342 return ssl_hs_error;
343 }
344 hs->tls13_state = state_process_change_cipher_spec;
345 return ssl->version == TLS1_3_EXPERIMENT_VERSION
346 ? ssl_hs_read_change_cipher_spec
347 : ssl_hs_ok;
348}
349
350static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
351 SSL *const ssl = hs->ssl;
352 if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->server_handshake_secret,
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700353 hs->hash_len)) {
354 return ssl_hs_error;
355 }
356
Robert Sloana12bf462017-07-17 07:08:26 -0700357 if (!hs->early_data_offered) {
358 /* If not sending early data, set client traffic keys now so that alerts are
359 * encrypted. */
360 if ((ssl->version == TLS1_3_EXPERIMENT_VERSION &&
361 !ssl3_add_change_cipher_spec(ssl)) ||
362 !tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_handshake_secret,
363 hs->hash_len)) {
364 return ssl_hs_error;
365 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400366 }
367
Steven Valdeze7531f02016-12-14 13:29:57 -0500368 hs->tls13_state = state_process_encrypted_extensions;
David Benjaminc895d6b2016-08-11 13:26:41 -0400369 return ssl_hs_read_message;
370}
371
David Benjamin1b249672016-12-06 18:25:50 -0500372static enum ssl_hs_wait_t do_process_encrypted_extensions(SSL_HANDSHAKE *hs) {
373 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800374 if (!ssl_check_message_type(ssl, SSL3_MT_ENCRYPTED_EXTENSIONS)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400375 return ssl_hs_error;
376 }
377
378 CBS cbs;
379 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
David Benjamin1b249672016-12-06 18:25:50 -0500380 if (!ssl_parse_serverhello_tlsext(hs, &cbs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400381 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
382 return ssl_hs_error;
383 }
384 if (CBS_len(&cbs) != 0) {
385 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
386 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
387 return ssl_hs_error;
388 }
389
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700390 /* Store the negotiated ALPN in the session. */
391 if (ssl->s3->alpn_selected != NULL) {
Robert Sloana12bf462017-07-17 07:08:26 -0700392 hs->new_session->early_alpn = (uint8_t *)BUF_memdup(
393 ssl->s3->alpn_selected, ssl->s3->alpn_selected_len);
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700394 if (hs->new_session->early_alpn == NULL) {
395 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
396 return ssl_hs_error;
397 }
398 hs->new_session->early_alpn_len = ssl->s3->alpn_selected_len;
399 }
400
401 if (ssl->early_data_accepted) {
Robert Sloane56da3e2017-06-26 08:26:42 -0700402 if (hs->early_session->cipher != hs->new_session->cipher ||
403 hs->early_session->early_alpn_len != ssl->s3->alpn_selected_len ||
404 OPENSSL_memcmp(hs->early_session->early_alpn, ssl->s3->alpn_selected,
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700405 ssl->s3->alpn_selected_len) != 0) {
406 OPENSSL_PUT_ERROR(SSL, SSL_R_ALPN_MISMATCH_ON_EARLY_DATA);
407 return ssl_hs_error;
408 }
409 if (ssl->s3->tlsext_channel_id_valid) {
410 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_ON_EARLY_DATA);
411 return ssl_hs_error;
412 }
413 }
414
Robert Sloan5d625782017-02-13 09:55:39 -0800415 if (!ssl_hash_current_message(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400416 return ssl_hs_error;
417 }
418
Robert Sloane56da3e2017-06-26 08:26:42 -0700419 hs->tls13_state = state_continue_second_server_flight;
420 if (hs->in_early_data && !ssl->early_data_accepted) {
421 return ssl_hs_early_data_rejected;
422 }
423 return ssl_hs_ok;
424}
425
426static enum ssl_hs_wait_t do_continue_second_server_flight(SSL_HANDSHAKE *hs) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500427 hs->tls13_state = state_process_certificate_request;
David Benjaminc895d6b2016-08-11 13:26:41 -0400428 return ssl_hs_read_message;
429}
430
David Benjamin1b249672016-12-06 18:25:50 -0500431static enum ssl_hs_wait_t do_process_certificate_request(SSL_HANDSHAKE *hs) {
432 SSL *const ssl = hs->ssl;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400433 /* CertificateRequest may only be sent in non-resumption handshakes. */
434 if (ssl->s3->session_reused) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500435 hs->tls13_state = state_process_server_finished;
David Benjaminc895d6b2016-08-11 13:26:41 -0400436 return ssl_hs_ok;
437 }
438
439 /* CertificateRequest is optional. */
440 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500441 hs->tls13_state = state_process_server_certificate;
David Benjaminc895d6b2016-08-11 13:26:41 -0400442 return ssl_hs_ok;
443 }
444
445 CBS cbs, context, supported_signature_algorithms;
446 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
447 if (!CBS_get_u8_length_prefixed(&cbs, &context) ||
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400448 /* The request context is always empty during the handshake. */
449 CBS_len(&context) != 0 ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400450 !CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
451 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjamin1b249672016-12-06 18:25:50 -0500452 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400453 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
454 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
455 return ssl_hs_error;
456 }
457
Robert Sloan5d625782017-02-13 09:55:39 -0800458 uint8_t alert = SSL_AD_DECODE_ERROR;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700459 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names =
Robert Sloan7d422bc2017-03-06 10:04:29 -0800460 ssl_parse_client_CA_list(ssl, &alert, &cbs);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700461 if (!ca_names) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400462 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
463 return ssl_hs_error;
464 }
465
466 /* Ignore extensions. */
467 CBS extensions;
468 if (!CBS_get_u16_length_prefixed(&cbs, &extensions) ||
469 CBS_len(&cbs) != 0) {
470 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
471 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
472 return ssl_hs_error;
473 }
474
David Benjamin1b249672016-12-06 18:25:50 -0500475 hs->cert_request = 1;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700476 hs->ca_names = std::move(ca_names);
Robert Sloan7d422bc2017-03-06 10:04:29 -0800477 ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400478
Robert Sloan5d625782017-02-13 09:55:39 -0800479 if (!ssl_hash_current_message(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400480 return ssl_hs_error;
481 }
482
Steven Valdeze7531f02016-12-14 13:29:57 -0500483 hs->tls13_state = state_process_server_certificate;
David Benjaminc895d6b2016-08-11 13:26:41 -0400484 return ssl_hs_read_message;
485}
486
David Benjamin1b249672016-12-06 18:25:50 -0500487static enum ssl_hs_wait_t do_process_server_certificate(SSL_HANDSHAKE *hs) {
488 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800489 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE) ||
Steven Valdeze7531f02016-12-14 13:29:57 -0500490 !tls13_process_certificate(hs, 0 /* certificate required */) ||
Robert Sloan5d625782017-02-13 09:55:39 -0800491 !ssl_hash_current_message(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400492 return ssl_hs_error;
493 }
494
Steven Valdeze7531f02016-12-14 13:29:57 -0500495 hs->tls13_state = state_process_server_certificate_verify;
David Benjaminc895d6b2016-08-11 13:26:41 -0400496 return ssl_hs_read_message;
497}
498
499static enum ssl_hs_wait_t do_process_server_certificate_verify(
David Benjamin1b249672016-12-06 18:25:50 -0500500 SSL_HANDSHAKE *hs) {
501 SSL *const ssl = hs->ssl;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700502 switch (ssl_verify_peer_cert(hs)) {
503 case ssl_verify_ok:
504 break;
505 case ssl_verify_invalid:
506 return ssl_hs_error;
507 case ssl_verify_retry:
508 hs->tls13_state = state_process_server_certificate_verify;
509 return ssl_hs_certificate_verify;
510 }
511
Robert Sloan4d1ac502017-02-06 08:36:14 -0800512 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY) ||
Steven Valdeze7531f02016-12-14 13:29:57 -0500513 !tls13_process_certificate_verify(hs) ||
Robert Sloan5d625782017-02-13 09:55:39 -0800514 !ssl_hash_current_message(hs)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500515 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400516 }
517
Steven Valdeze7531f02016-12-14 13:29:57 -0500518 hs->tls13_state = state_process_server_finished;
David Benjaminc895d6b2016-08-11 13:26:41 -0400519 return ssl_hs_read_message;
520}
521
David Benjamin1b249672016-12-06 18:25:50 -0500522static enum ssl_hs_wait_t do_process_server_finished(SSL_HANDSHAKE *hs) {
523 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800524 if (!ssl_check_message_type(ssl, SSL3_MT_FINISHED) ||
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700525 !tls13_process_finished(hs, 0 /* don't use saved value */) ||
Robert Sloan5d625782017-02-13 09:55:39 -0800526 !ssl_hash_current_message(hs) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400527 /* Update the secret to the master secret and derive traffic keys. */
David Benjamin1b249672016-12-06 18:25:50 -0500528 !tls13_advance_key_schedule(hs, kZeroes, hs->hash_len) ||
529 !tls13_derive_application_secrets(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400530 return ssl_hs_error;
531 }
532
533 ssl->method->received_flight(ssl);
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700534 hs->tls13_state = state_send_end_of_early_data;
535 return ssl_hs_ok;
536}
537
538static enum ssl_hs_wait_t do_send_end_of_early_data(SSL_HANDSHAKE *hs) {
539 SSL *const ssl = hs->ssl;
Robert Sloane56da3e2017-06-26 08:26:42 -0700540
541 if (ssl->early_data_accepted) {
542 hs->can_early_write = 0;
543 if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING,
544 TLS1_AD_END_OF_EARLY_DATA)) {
545 return ssl_hs_error;
546 }
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700547 }
548
Robert Sloana12bf462017-07-17 07:08:26 -0700549 if (hs->early_data_offered) {
550 if ((ssl->version == TLS1_3_EXPERIMENT_VERSION &&
551 !ssl3_add_change_cipher_spec(ssl)) ||
552 !tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_handshake_secret,
553 hs->hash_len)) {
554 return ssl_hs_error;
555 }
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700556 }
557
Steven Valdeze7531f02016-12-14 13:29:57 -0500558 hs->tls13_state = state_send_client_certificate;
David Benjaminc895d6b2016-08-11 13:26:41 -0400559 return ssl_hs_ok;
560}
561
Steven Valdeze7531f02016-12-14 13:29:57 -0500562static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500563 SSL *const ssl = hs->ssl;
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700564
David Benjaminc895d6b2016-08-11 13:26:41 -0400565 /* The peer didn't request a certificate. */
David Benjamin1b249672016-12-06 18:25:50 -0500566 if (!hs->cert_request) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800567 hs->tls13_state = state_complete_second_flight;
David Benjaminc895d6b2016-08-11 13:26:41 -0400568 return ssl_hs_ok;
569 }
570
571 /* Call cert_cb to update the certificate. */
572 if (ssl->cert->cert_cb != NULL) {
573 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
574 if (rv == 0) {
575 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
576 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
577 return ssl_hs_error;
578 }
579 if (rv < 0) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500580 hs->tls13_state = state_send_client_certificate;
David Benjaminc895d6b2016-08-11 13:26:41 -0400581 return ssl_hs_x509_lookup;
582 }
583 }
584
Robert Sloan572a4e22017-04-17 10:52:19 -0700585 if (!ssl_on_certificate_selected(hs) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -0800586 !tls13_add_certificate(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400587 return ssl_hs_error;
588 }
589
Steven Valdeze7531f02016-12-14 13:29:57 -0500590 hs->tls13_state = state_send_client_certificate_verify;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800591 return ssl_hs_ok;
David Benjaminc895d6b2016-08-11 13:26:41 -0400592}
593
Robert Sloane56da3e2017-06-26 08:26:42 -0700594static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500595 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -0400596 /* Don't send CertificateVerify if there is no certificate. */
597 if (!ssl_has_certificate(ssl)) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800598 hs->tls13_state = state_complete_second_flight;
David Benjaminc895d6b2016-08-11 13:26:41 -0400599 return ssl_hs_ok;
600 }
601
Robert Sloane56da3e2017-06-26 08:26:42 -0700602 switch (tls13_add_certificate_verify(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400603 case ssl_private_key_success:
Robert Sloan4d1ac502017-02-06 08:36:14 -0800604 hs->tls13_state = state_complete_second_flight;
605 return ssl_hs_ok;
David Benjaminc895d6b2016-08-11 13:26:41 -0400606
607 case ssl_private_key_retry:
Robert Sloane56da3e2017-06-26 08:26:42 -0700608 hs->tls13_state = state_send_client_certificate_verify;
David Benjaminc895d6b2016-08-11 13:26:41 -0400609 return ssl_hs_private_key_operation;
610
611 case ssl_private_key_failure:
612 return ssl_hs_error;
613 }
614
615 assert(0);
616 return ssl_hs_error;
617}
618
Robert Sloan4d1ac502017-02-06 08:36:14 -0800619static enum ssl_hs_wait_t do_complete_second_flight(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500620 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800621
622 /* Send a Channel ID assertion if necessary. */
623 if (ssl->s3->tlsext_channel_id_valid) {
624 if (!ssl_do_channel_id_callback(ssl)) {
625 hs->tls13_state = state_complete_second_flight;
626 return ssl_hs_error;
627 }
628
629 if (ssl->tlsext_channel_id_private == NULL) {
630 return ssl_hs_channel_id_lookup;
631 }
632
Robert Sloanb6d070c2017-07-24 08:40:01 -0700633 ScopedCBB cbb;
634 CBB body;
635 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) ||
Robert Sloan5d625782017-02-13 09:55:39 -0800636 !tls1_write_channel_id(hs, &body) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -0700637 !ssl_add_message_cbb(ssl, cbb.get())) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800638 return ssl_hs_error;
639 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500640 }
641
Robert Sloan4d1ac502017-02-06 08:36:14 -0800642 /* Send a Finished message. */
643 if (!tls13_add_finished(hs)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500644 return ssl_hs_error;
645 }
646
Robert Sloan4d1ac502017-02-06 08:36:14 -0800647 /* Derive the final keys and enable them. */
Steven Valdez909b19f2016-11-21 15:35:44 -0500648 if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->server_traffic_secret_0,
649 hs->hash_len) ||
650 !tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_traffic_secret_0,
651 hs->hash_len) ||
David Benjamin1b249672016-12-06 18:25:50 -0500652 !tls13_derive_resumption_secret(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400653 return ssl_hs_error;
654 }
655
Steven Valdeze7531f02016-12-14 13:29:57 -0500656 hs->tls13_state = state_done;
David Benjaminc895d6b2016-08-11 13:26:41 -0400657 return ssl_hs_flush;
658}
659
David Benjamin1b249672016-12-06 18:25:50 -0500660enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500661 while (hs->tls13_state != state_done) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400662 enum ssl_hs_wait_t ret = ssl_hs_error;
Robert Sloana12bf462017-07-17 07:08:26 -0700663 enum client_hs_state_t state =
664 static_cast<enum client_hs_state_t>(hs->tls13_state);
David Benjaminc895d6b2016-08-11 13:26:41 -0400665 switch (state) {
666 case state_process_hello_retry_request:
David Benjamin1b249672016-12-06 18:25:50 -0500667 ret = do_process_hello_retry_request(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400668 break;
669 case state_send_second_client_hello:
David Benjamin1b249672016-12-06 18:25:50 -0500670 ret = do_send_second_client_hello(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400671 break;
David Benjaminc895d6b2016-08-11 13:26:41 -0400672 case state_process_server_hello:
David Benjamin1b249672016-12-06 18:25:50 -0500673 ret = do_process_server_hello(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400674 break;
Robert Sloana12bf462017-07-17 07:08:26 -0700675 case state_process_change_cipher_spec:
676 ret = do_process_change_cipher_spec(hs);
677 break;
David Benjaminc895d6b2016-08-11 13:26:41 -0400678 case state_process_encrypted_extensions:
David Benjamin1b249672016-12-06 18:25:50 -0500679 ret = do_process_encrypted_extensions(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400680 break;
Robert Sloane56da3e2017-06-26 08:26:42 -0700681 case state_continue_second_server_flight:
682 ret = do_continue_second_server_flight(hs);
683 break;
David Benjaminc895d6b2016-08-11 13:26:41 -0400684 case state_process_certificate_request:
David Benjamin1b249672016-12-06 18:25:50 -0500685 ret = do_process_certificate_request(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400686 break;
687 case state_process_server_certificate:
David Benjamin1b249672016-12-06 18:25:50 -0500688 ret = do_process_server_certificate(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400689 break;
690 case state_process_server_certificate_verify:
David Benjamin1b249672016-12-06 18:25:50 -0500691 ret = do_process_server_certificate_verify(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400692 break;
693 case state_process_server_finished:
David Benjamin1b249672016-12-06 18:25:50 -0500694 ret = do_process_server_finished(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400695 break;
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700696 case state_send_end_of_early_data:
697 ret = do_send_end_of_early_data(hs);
698 break;
David Benjaminc895d6b2016-08-11 13:26:41 -0400699 case state_send_client_certificate:
David Benjamin1b249672016-12-06 18:25:50 -0500700 ret = do_send_client_certificate(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400701 break;
702 case state_send_client_certificate_verify:
Robert Sloane56da3e2017-06-26 08:26:42 -0700703 ret = do_send_client_certificate_verify(hs);
Steven Valdez909b19f2016-11-21 15:35:44 -0500704 break;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800705 case state_complete_second_flight:
706 ret = do_complete_second_flight(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400707 break;
708 case state_done:
709 ret = ssl_hs_ok;
710 break;
711 }
712
713 if (ret != ssl_hs_ok) {
714 return ret;
715 }
716 }
717
718 return ssl_hs_ok;
719}
720
721int tls13_process_new_session_ticket(SSL *ssl) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700722 UniquePtr<SSL_SESSION> session(SSL_SESSION_dup(ssl->s3->established_session,
723 SSL_SESSION_INCLUDE_NONAUTH));
Robert Sloana12bf462017-07-17 07:08:26 -0700724 if (!session) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400725 return 0;
726 }
727
Robert Sloana12bf462017-07-17 07:08:26 -0700728 ssl_session_rebase_time(ssl, session.get());
Steven Valdez909b19f2016-11-21 15:35:44 -0500729
Robert Sloan4d1ac502017-02-06 08:36:14 -0800730 uint32_t server_timeout;
Steven Valdez909b19f2016-11-21 15:35:44 -0500731 CBS cbs, ticket, extensions;
David Benjaminc895d6b2016-08-11 13:26:41 -0400732 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Robert Sloan4d1ac502017-02-06 08:36:14 -0800733 if (!CBS_get_u32(&cbs, &server_timeout) ||
Steven Valdez909b19f2016-11-21 15:35:44 -0500734 !CBS_get_u32(&cbs, &session->ticket_age_add) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400735 !CBS_get_u16_length_prefixed(&cbs, &ticket) ||
736 !CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen) ||
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400737 !CBS_get_u16_length_prefixed(&cbs, &extensions) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400738 CBS_len(&cbs) != 0) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400739 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
740 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloana12bf462017-07-17 07:08:26 -0700741 return 0;
Robert Sloan69939df2017-01-09 10:53:07 -0800742 }
743
Robert Sloan4d1ac502017-02-06 08:36:14 -0800744 /* Cap the renewable lifetime by the server advertised value. This avoids
Robert Sloan7d422bc2017-03-06 10:04:29 -0800745 * wasting bandwidth on 0-RTT when we know the server will reject it. */
746 if (session->timeout > server_timeout) {
747 session->timeout = server_timeout;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800748 }
749
Robert Sloan69939df2017-01-09 10:53:07 -0800750 /* Parse out the extensions. */
751 int have_early_data_info = 0;
752 CBS early_data_info;
753 const SSL_EXTENSION_TYPE ext_types[] = {
754 {TLSEXT_TYPE_ticket_early_data_info, &have_early_data_info,
755 &early_data_info},
756 };
757
Robert Sloan5d625782017-02-13 09:55:39 -0800758 uint8_t alert = SSL_AD_DECODE_ERROR;
Robert Sloan69939df2017-01-09 10:53:07 -0800759 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
760 OPENSSL_ARRAY_SIZE(ext_types),
761 1 /* ignore unknown */)) {
762 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana12bf462017-07-17 07:08:26 -0700763 return 0;
Robert Sloan69939df2017-01-09 10:53:07 -0800764 }
765
Robert Sloan572a4e22017-04-17 10:52:19 -0700766 if (have_early_data_info && ssl->cert->enable_early_data) {
Robert Sloan69939df2017-01-09 10:53:07 -0800767 if (!CBS_get_u32(&early_data_info, &session->ticket_max_early_data) ||
768 CBS_len(&early_data_info) != 0) {
769 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
770 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloana12bf462017-07-17 07:08:26 -0700771 return 0;
Robert Sloan69939df2017-01-09 10:53:07 -0800772 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400773 }
774
Steven Valdez909b19f2016-11-21 15:35:44 -0500775 session->ticket_age_add_valid = 1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400776 session->not_resumable = 0;
777
Steven Valdez909b19f2016-11-21 15:35:44 -0500778 if (ssl->ctx->new_session_cb != NULL &&
Robert Sloana12bf462017-07-17 07:08:26 -0700779 ssl->ctx->new_session_cb(ssl, session.get())) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400780 /* |new_session_cb|'s return value signals that it took ownership. */
Robert Sloana12bf462017-07-17 07:08:26 -0700781 session.release();
David Benjaminc895d6b2016-08-11 13:26:41 -0400782 }
783
Robert Sloana12bf462017-07-17 07:08:26 -0700784 return 1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400785}
David Benjamin7c0d06c2016-08-11 13:26:41 -0400786
David Benjamin1b249672016-12-06 18:25:50 -0500787void ssl_clear_tls13_state(SSL_HANDSHAKE *hs) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700788 hs->key_share.reset();
David Benjamin7c0d06c2016-08-11 13:26:41 -0400789
David Benjamin1b249672016-12-06 18:25:50 -0500790 OPENSSL_free(hs->key_share_bytes);
791 hs->key_share_bytes = NULL;
792 hs->key_share_bytes_len = 0;
David Benjamin7c0d06c2016-08-11 13:26:41 -0400793}
Robert Sloanb6d070c2017-07-24 08:40:01 -0700794
795} // namespace bssl