blob: 40281a07f90023c1e97ac6bbfc267bf1e92482d4 [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 {
Robert Sloan84377092017-08-14 09:33:19 -070036 state_read_hello_retry_request = 0,
David Benjaminc895d6b2016-08-11 13:26:41 -040037 state_send_second_client_hello,
Robert Sloan84377092017-08-14 09:33:19 -070038 state_read_server_hello,
Robert Sloan84377092017-08-14 09:33:19 -070039 state_read_encrypted_extensions,
40 state_read_certificate_request,
41 state_read_server_certificate,
42 state_read_server_certificate_verify,
43 state_read_server_finished,
Robert Sloan6d0d00e2017-03-27 07:13:07 -070044 state_send_end_of_early_data,
David Benjaminc895d6b2016-08-11 13:26:41 -040045 state_send_client_certificate,
46 state_send_client_certificate_verify,
Robert Sloan4d1ac502017-02-06 08:36:14 -080047 state_complete_second_flight,
David Benjaminc895d6b2016-08-11 13:26:41 -040048 state_done,
49};
50
David Benjamin1b249672016-12-06 18:25:50 -050051static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0};
52
Robert Sloan84377092017-08-14 09:33:19 -070053static enum ssl_hs_wait_t do_read_hello_retry_request(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -050054 SSL *const ssl = hs->ssl;
Robert Sloan99319a12017-11-27 10:32:46 -080055 assert(ssl->s3->have_version);
Robert Sloan84377092017-08-14 09:33:19 -070056 SSLMessage msg;
57 if (!ssl->method->get_message(ssl, &msg)) {
58 return ssl_hs_read_message;
59 }
David Benjaminc895d6b2016-08-11 13:26:41 -040060
Robert Sloan8542c082018-02-05 09:07:34 -080061 // Queue up a ChangeCipherSpec for whenever we next send something. This
62 // will be before the second ClientHello. If we offered early data, this was
63 // already done.
64 if (!hs->early_data_offered &&
65 !ssl->method->add_change_cipher_spec(ssl)) {
66 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -040067 }
68
Robert Sloan8542c082018-02-05 09:07:34 -080069 if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) {
70 return ssl_hs_error;
71 }
Robert Sloanb1b54b82017-11-06 13:50:02 -080072
Robert Sloan8542c082018-02-05 09:07:34 -080073 CBS body = msg.body, extensions, server_random, session_id;
74 uint16_t server_version, cipher_suite;
75 uint8_t compression_method;
76 if (!CBS_get_u16(&body, &server_version) ||
77 !CBS_get_bytes(&body, &server_random, SSL3_RANDOM_SIZE) ||
78 !CBS_get_u8_length_prefixed(&body, &session_id) ||
79 !CBS_mem_equal(&session_id, hs->session_id, hs->session_id_len) ||
80 !CBS_get_u16(&body, &cipher_suite) ||
81 !CBS_get_u8(&body, &compression_method) ||
82 compression_method != 0 ||
83 !CBS_get_u16_length_prefixed(&body, &extensions) ||
84 CBS_len(&extensions) == 0 ||
85 CBS_len(&body) != 0) {
86 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
87 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
88 return ssl_hs_error;
89 }
Robert Sloanb1b54b82017-11-06 13:50:02 -080090
Robert Sloan8542c082018-02-05 09:07:34 -080091 if (!CBS_mem_equal(&server_random, kHelloRetryRequest, SSL3_RANDOM_SIZE)) {
92 hs->tls13_state = state_read_server_hello;
93 return ssl_hs_ok;
94 }
95
96 const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
97 // Check if the cipher is a TLS 1.3 cipher.
98 if (cipher == NULL ||
99 SSL_CIPHER_get_min_version(cipher) > ssl_protocol_version(ssl) ||
100 SSL_CIPHER_get_max_version(cipher) < ssl_protocol_version(ssl)) {
101 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
102 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
103 return ssl_hs_error;
104 }
105
106 hs->new_cipher = cipher;
107
108 if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
109 !hs->transcript.UpdateForHelloRetryRequest()) {
110 return ssl_hs_error;
Robert Sloanb1b54b82017-11-06 13:50:02 -0800111 }
112
113
Robert Sloand5c22152017-11-13 09:22:12 -0800114 bool have_cookie, have_key_share, have_supported_versions;
115 CBS cookie, key_share, supported_versions;
Robert Sloan0db7f542018-01-16 15:48:33 -0800116 SSL_EXTENSION_TYPE ext_types[] = {
Robert Sloan8542c082018-02-05 09:07:34 -0800117 {TLSEXT_TYPE_key_share, &have_key_share, &key_share},
Steven Valdez909b19f2016-11-21 15:35:44 -0500118 {TLSEXT_TYPE_cookie, &have_cookie, &cookie},
Robert Sloand5c22152017-11-13 09:22:12 -0800119 {TLSEXT_TYPE_supported_versions, &have_supported_versions,
120 &supported_versions},
Steven Valdez909b19f2016-11-21 15:35:44 -0500121 };
122
Robert Sloan5d625782017-02-13 09:55:39 -0800123 uint8_t alert = SSL_AD_DECODE_ERROR;
Steven Valdez909b19f2016-11-21 15:35:44 -0500124 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
Robert Sloan69939df2017-01-09 10:53:07 -0800125 OPENSSL_ARRAY_SIZE(ext_types),
126 0 /* reject unknown */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700127 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez909b19f2016-11-21 15:35:44 -0500128 return ssl_hs_error;
129 }
130
Robert Sloan99319a12017-11-27 10:32:46 -0800131 if (!have_cookie && !have_key_share) {
132 OPENSSL_PUT_ERROR(SSL, SSL_R_EMPTY_HELLO_RETRY_REQUEST);
133 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
134 return ssl_hs_error;
135 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500136 if (have_cookie) {
137 CBS cookie_value;
138 if (!CBS_get_u16_length_prefixed(&cookie, &cookie_value) ||
139 CBS_len(&cookie_value) == 0 ||
140 CBS_len(&cookie) != 0) {
David Benjamin95add822016-10-19 01:09:12 -0400141 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700142 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin95add822016-10-19 01:09:12 -0400143 return ssl_hs_error;
144 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400145
Robert Sloan4562e9d2017-10-02 10:26:51 -0700146 if (!hs->cookie.CopyFrom(cookie_value)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500147 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400148 }
149 }
150
Steven Valdez909b19f2016-11-21 15:35:44 -0500151 if (have_key_share) {
152 uint16_t group_id;
153 if (!CBS_get_u16(&key_share, &group_id) || CBS_len(&key_share) != 0) {
154 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700155 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez909b19f2016-11-21 15:35:44 -0500156 return ssl_hs_error;
157 }
158
Robert Sloana27a6a42017-09-05 08:39:28 -0700159 // The group must be supported.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100160 if (!tls1_check_group_id(hs, group_id)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700161 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez909b19f2016-11-21 15:35:44 -0500162 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
163 return ssl_hs_error;
164 }
165
Robert Sloana27a6a42017-09-05 08:39:28 -0700166 // Check that the HelloRetryRequest does not request the key share that
167 // was provided in the initial ClientHello.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700168 if (hs->key_share->GroupID() == group_id) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700169 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez909b19f2016-11-21 15:35:44 -0500170 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
171 return ssl_hs_error;
172 }
173
Robert Sloanb6d070c2017-07-24 08:40:01 -0700174 hs->key_share.reset();
Steven Valdez909b19f2016-11-21 15:35:44 -0500175 hs->retry_group = group_id;
176 }
177
Robert Sloan84377092017-08-14 09:33:19 -0700178 if (!ssl_hash_message(hs, msg)) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800179 return ssl_hs_error;
180 }
181
Robert Sloan84377092017-08-14 09:33:19 -0700182 ssl->method->next_message(ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -0700183 hs->received_hello_retry_request = true;
Steven Valdeze7531f02016-12-14 13:29:57 -0500184 hs->tls13_state = state_send_second_client_hello;
Robert Sloana27a6a42017-09-05 08:39:28 -0700185 // 0-RTT is rejected if we receive a HelloRetryRequest.
Robert Sloane56da3e2017-06-26 08:26:42 -0700186 if (hs->in_early_data) {
187 return ssl_hs_early_data_rejected;
188 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400189 return ssl_hs_ok;
190}
191
David Benjamin1b249672016-12-06 18:25:50 -0500192static enum ssl_hs_wait_t do_send_second_client_hello(SSL_HANDSHAKE *hs) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700193 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700194 // Restore the null cipher. We may have switched due to 0-RTT.
Robert Sloandb4251a2017-09-18 09:38:15 -0700195 bssl::UniquePtr<SSLAEADContext> null_ctx =
196 SSLAEADContext::CreateNullCipher(SSL_is_dtls(ssl));
Robert Sloanb6d070c2017-07-24 08:40:01 -0700197 if (!null_ctx ||
Robert Sloandb4251a2017-09-18 09:38:15 -0700198 !ssl->method->set_write_state(ssl, std::move(null_ctx))) {
199 return ssl_hs_error;
200 }
201
202 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
203
204 if (!ssl_write_client_hello(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400205 return ssl_hs_error;
206 }
207
Robert Sloan84377092017-08-14 09:33:19 -0700208 hs->tls13_state = state_read_server_hello;
209 return ssl_hs_flush;
David Benjaminc895d6b2016-08-11 13:26:41 -0400210}
211
Robert Sloan84377092017-08-14 09:33:19 -0700212static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500213 SSL *const ssl = hs->ssl;
Robert Sloan84377092017-08-14 09:33:19 -0700214 SSLMessage msg;
215 if (!ssl->method->get_message(ssl, &msg)) {
216 return ssl_hs_read_message;
217 }
218 if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400219 return ssl_hs_error;
220 }
221
Robert Sloan84377092017-08-14 09:33:19 -0700222 CBS body = msg.body, server_random, session_id, extensions;
Robert Sloana12bf462017-07-17 07:08:26 -0700223 uint16_t server_version;
David Benjaminc895d6b2016-08-11 13:26:41 -0400224 uint16_t cipher_suite;
Robert Sloana12bf462017-07-17 07:08:26 -0700225 uint8_t compression_method;
Robert Sloan84377092017-08-14 09:33:19 -0700226 if (!CBS_get_u16(&body, &server_version) ||
227 !CBS_get_bytes(&body, &server_random, SSL3_RANDOM_SIZE) ||
Robert Sloan0da43952018-01-03 15:13:14 -0800228 !CBS_get_u8_length_prefixed(&body, &session_id) ||
229 !CBS_mem_equal(&session_id, hs->session_id, hs->session_id_len) ||
Robert Sloan84377092017-08-14 09:33:19 -0700230 !CBS_get_u16(&body, &cipher_suite) ||
Robert Sloan0da43952018-01-03 15:13:14 -0800231 !CBS_get_u8(&body, &compression_method) ||
232 compression_method != 0 ||
Robert Sloan84377092017-08-14 09:33:19 -0700233 !CBS_get_u16_length_prefixed(&body, &extensions) ||
234 CBS_len(&body) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700235 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -0400236 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
237 return ssl_hs_error;
238 }
239
Robert Sloan0da43952018-01-03 15:13:14 -0800240 if (server_version != TLS1_2_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700241 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -0400242 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
243 return ssl_hs_error;
244 }
245
Robert Sloan99319a12017-11-27 10:32:46 -0800246 // Forbid a second HelloRetryRequest.
Robert Sloan8542c082018-02-05 09:07:34 -0800247 if (CBS_mem_equal(&server_random, kHelloRetryRequest, SSL3_RANDOM_SIZE)) {
Robert Sloan99319a12017-11-27 10:32:46 -0800248 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
249 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
250 return ssl_hs_error;
251 }
252
Robert Sloan69939df2017-01-09 10:53:07 -0800253 OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
254 SSL3_RANDOM_SIZE);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400255
Robert Sloanb1b54b82017-11-06 13:50:02 -0800256 // Check if the cipher is a TLS 1.3 cipher.
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400257 const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
Robert Sloanb1b54b82017-11-06 13:50:02 -0800258 if (cipher == nullptr ||
259 SSL_CIPHER_get_min_version(cipher) > ssl_protocol_version(ssl) ||
260 SSL_CIPHER_get_max_version(cipher) < ssl_protocol_version(ssl)) {
261 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700262 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400263 return ssl_hs_error;
264 }
265
Robert Sloanb1b54b82017-11-06 13:50:02 -0800266 // Check that the cipher matches the one in the HelloRetryRequest.
Robert Sloan8542c082018-02-05 09:07:34 -0800267 if (hs->received_hello_retry_request &&
Robert Sloanb1b54b82017-11-06 13:50:02 -0800268 hs->new_cipher != cipher) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400269 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700270 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400271 return ssl_hs_error;
272 }
273
Robert Sloana27a6a42017-09-05 08:39:28 -0700274 // Parse out the extensions.
275 bool have_key_share = false, have_pre_shared_key = false,
276 have_supported_versions = false;
Robert Sloana12bf462017-07-17 07:08:26 -0700277 CBS key_share, pre_shared_key, supported_versions;
Robert Sloan0db7f542018-01-16 15:48:33 -0800278 SSL_EXTENSION_TYPE ext_types[] = {
Robert Sloan8542c082018-02-05 09:07:34 -0800279 {TLSEXT_TYPE_key_share, &have_key_share, &key_share},
Steven Valdez909b19f2016-11-21 15:35:44 -0500280 {TLSEXT_TYPE_pre_shared_key, &have_pre_shared_key, &pre_shared_key},
Robert Sloana12bf462017-07-17 07:08:26 -0700281 {TLSEXT_TYPE_supported_versions, &have_supported_versions,
282 &supported_versions},
Steven Valdez909b19f2016-11-21 15:35:44 -0500283 };
David Benjaminc895d6b2016-08-11 13:26:41 -0400284
Robert Sloan5d625782017-02-13 09:55:39 -0800285 uint8_t alert = SSL_AD_DECODE_ERROR;
Steven Valdez909b19f2016-11-21 15:35:44 -0500286 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
Robert Sloan69939df2017-01-09 10:53:07 -0800287 OPENSSL_ARRAY_SIZE(ext_types),
288 0 /* reject unknown */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700289 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez909b19f2016-11-21 15:35:44 -0500290 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400291 }
292
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100293 if (ssl_is_draft28(ssl->version)) {
294 // Recheck supported_versions, in case this is the second ServerHello.
295 uint16_t version;
296 if (!have_supported_versions ||
297 !CBS_get_u16(&supported_versions, &version) ||
298 version != ssl->version) {
299 OPENSSL_PUT_ERROR(SSL, SSL_R_SECOND_SERVERHELLO_VERSION_MISMATCH);
300 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
301 return ssl_hs_error;
302 }
303 }
304
Steven Valdez909b19f2016-11-21 15:35:44 -0500305 alert = SSL_AD_DECODE_ERROR;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400306 if (have_pre_shared_key) {
307 if (ssl->session == NULL) {
308 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700309 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400310 return ssl_hs_error;
311 }
312
David Benjamin1b249672016-12-06 18:25:50 -0500313 if (!ssl_ext_pre_shared_key_parse_serverhello(hs, &alert,
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400314 &pre_shared_key)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700315 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400316 return ssl_hs_error;
317 }
318
319 if (ssl->session->ssl_version != ssl->version) {
320 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700321 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400322 return ssl_hs_error;
323 }
324
Steven Valdez909b19f2016-11-21 15:35:44 -0500325 if (ssl->session->cipher->algorithm_prf != cipher->algorithm_prf) {
326 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_PRF_HASH_MISMATCH);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700327 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400328 return ssl_hs_error;
329 }
330
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100331 if (!ssl_session_is_context_valid(hs, ssl->session.get())) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700332 // This is actually a client application bug.
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400333 OPENSSL_PUT_ERROR(SSL,
334 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700335 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400336 return ssl_hs_error;
337 }
338
Robert Sloana27a6a42017-09-05 08:39:28 -0700339 ssl->s3->session_reused = true;
340 // Only authentication information carries over in TLS 1.3.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100341 hs->new_session =
342 SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_DUP_AUTH_ONLY);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700343 if (!hs->new_session) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700344 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400345 return ssl_hs_error;
346 }
Robert Sloane56da3e2017-06-26 08:26:42 -0700347 ssl_set_session(ssl, NULL);
Robert Sloan4d1ac502017-02-06 08:36:14 -0800348
Robert Sloana27a6a42017-09-05 08:39:28 -0700349 // Resumption incorporates fresh key material, so refresh the timeout.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700350 ssl_session_renew_timeout(ssl, hs->new_session.get(),
Robert Sloan1c9db532017-03-13 08:03:59 -0700351 ssl->session_ctx->session_psk_dhe_timeout);
David Benjamin1b249672016-12-06 18:25:50 -0500352 } else if (!ssl_get_new_session(hs, 0)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700353 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -0400354 return ssl_hs_error;
355 }
356
Robert Sloana94fe052017-02-21 08:49:28 -0800357 hs->new_session->cipher = cipher;
358 hs->new_cipher = cipher;
359
Robert Sloanb1b54b82017-11-06 13:50:02 -0800360 size_t hash_len =
361 EVP_MD_size(ssl_get_handshake_digest(ssl_protocol_version(ssl), cipher));
David Benjaminc895d6b2016-08-11 13:26:41 -0400362
Robert Sloanb1b54b82017-11-06 13:50:02 -0800363 // Set up the key schedule and incorporate the PSK into the running secret.
David Benjamin1b249672016-12-06 18:25:50 -0500364 if (ssl->s3->session_reused) {
Robert Sloanb1b54b82017-11-06 13:50:02 -0800365 if (!tls13_init_key_schedule(hs, hs->new_session->master_key,
366 hs->new_session->master_key_length)) {
David Benjamin1b249672016-12-06 18:25:50 -0500367 return ssl_hs_error;
368 }
Robert Sloanb1b54b82017-11-06 13:50:02 -0800369 } else if (!tls13_init_key_schedule(hs, kZeroes, hash_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400370 return ssl_hs_error;
371 }
372
Steven Valdeze7531f02016-12-14 13:29:57 -0500373 if (!have_key_share) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700374 // We do not support psk_ke and thus always require a key share.
Steven Valdeze7531f02016-12-14 13:29:57 -0500375 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700376 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION);
Steven Valdeze7531f02016-12-14 13:29:57 -0500377 return ssl_hs_error;
378 }
379
Robert Sloana27a6a42017-09-05 08:39:28 -0700380 // Resolve ECDHE and incorporate it into the secret.
Robert Sloan4562e9d2017-10-02 10:26:51 -0700381 Array<uint8_t> dhe_secret;
Robert Sloan5d625782017-02-13 09:55:39 -0800382 alert = SSL_AD_DECODE_ERROR;
Robert Sloan4562e9d2017-10-02 10:26:51 -0700383 if (!ssl_ext_key_share_parse_serverhello(hs, &dhe_secret, &alert,
384 &key_share)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700385 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400386 return ssl_hs_error;
387 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400388
Robert Sloan4562e9d2017-10-02 10:26:51 -0700389 if (!tls13_advance_key_schedule(hs, dhe_secret.data(), dhe_secret.size()) ||
390 !ssl_hash_message(hs, msg) ||
Robert Sloand5c22152017-11-13 09:22:12 -0800391 !tls13_derive_handshake_secrets(hs) ||
392 !tls13_set_traffic_key(ssl, evp_aead_open, hs->server_handshake_secret,
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700393 hs->hash_len)) {
394 return ssl_hs_error;
395 }
396
Robert Sloana12bf462017-07-17 07:08:26 -0700397 if (!hs->early_data_offered) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700398 // If not sending early data, set client traffic keys now so that alerts are
399 // encrypted.
Robert Sloan99319a12017-11-27 10:32:46 -0800400 if (!tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_handshake_secret,
Robert Sloana12bf462017-07-17 07:08:26 -0700401 hs->hash_len)) {
402 return ssl_hs_error;
403 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400404 }
405
Robert Sloand5c22152017-11-13 09:22:12 -0800406 ssl->method->next_message(ssl);
Robert Sloan84377092017-08-14 09:33:19 -0700407 hs->tls13_state = state_read_encrypted_extensions;
408 return ssl_hs_ok;
David Benjaminc895d6b2016-08-11 13:26:41 -0400409}
410
Robert Sloan84377092017-08-14 09:33:19 -0700411static enum ssl_hs_wait_t do_read_encrypted_extensions(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500412 SSL *const ssl = hs->ssl;
Robert Sloan84377092017-08-14 09:33:19 -0700413 SSLMessage msg;
414 if (!ssl->method->get_message(ssl, &msg)) {
415 return ssl_hs_read_message;
416 }
417 if (!ssl_check_message_type(ssl, msg, SSL3_MT_ENCRYPTED_EXTENSIONS)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400418 return ssl_hs_error;
419 }
420
Robert Sloan84377092017-08-14 09:33:19 -0700421 CBS body = msg.body;
422 if (!ssl_parse_serverhello_tlsext(hs, &body)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400423 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
424 return ssl_hs_error;
425 }
Robert Sloan84377092017-08-14 09:33:19 -0700426 if (CBS_len(&body) != 0) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400427 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700428 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -0400429 return ssl_hs_error;
430 }
431
Robert Sloana27a6a42017-09-05 08:39:28 -0700432 // Store the negotiated ALPN in the session.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100433 if (!hs->new_session->early_alpn.CopyFrom(ssl->s3->alpn_selected)) {
434 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
435 return ssl_hs_error;
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700436 }
437
Robert Sloan0da43952018-01-03 15:13:14 -0800438 if (ssl->s3->early_data_accepted) {
Robert Sloane56da3e2017-06-26 08:26:42 -0700439 if (hs->early_session->cipher != hs->new_session->cipher ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100440 MakeConstSpan(hs->early_session->early_alpn) !=
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700441 ssl->s3->alpn_selected) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700442 OPENSSL_PUT_ERROR(SSL, SSL_R_ALPN_MISMATCH_ON_EARLY_DATA);
443 return ssl_hs_error;
444 }
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100445 if (ssl->s3->channel_id_valid || ssl->s3->token_binding_negotiated) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700446 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION_ON_EARLY_DATA);
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700447 return ssl_hs_error;
448 }
449 }
450
Robert Sloan84377092017-08-14 09:33:19 -0700451 if (!ssl_hash_message(hs, msg)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400452 return ssl_hs_error;
453 }
454
Robert Sloan84377092017-08-14 09:33:19 -0700455 ssl->method->next_message(ssl);
456 hs->tls13_state = state_read_certificate_request;
Robert Sloan0da43952018-01-03 15:13:14 -0800457 if (hs->in_early_data && !ssl->s3->early_data_accepted) {
Robert Sloane56da3e2017-06-26 08:26:42 -0700458 return ssl_hs_early_data_rejected;
459 }
460 return ssl_hs_ok;
461}
462
Robert Sloan84377092017-08-14 09:33:19 -0700463static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500464 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700465 // CertificateRequest may only be sent in non-resumption handshakes.
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400466 if (ssl->s3->session_reused) {
Robert Sloan84377092017-08-14 09:33:19 -0700467 hs->tls13_state = state_read_server_finished;
David Benjaminc895d6b2016-08-11 13:26:41 -0400468 return ssl_hs_ok;
469 }
470
Robert Sloan84377092017-08-14 09:33:19 -0700471 SSLMessage msg;
472 if (!ssl->method->get_message(ssl, &msg)) {
473 return ssl_hs_read_message;
474 }
475
Robert Sloana27a6a42017-09-05 08:39:28 -0700476 // CertificateRequest is optional.
Robert Sloan84377092017-08-14 09:33:19 -0700477 if (msg.type != SSL3_MT_CERTIFICATE_REQUEST) {
478 hs->tls13_state = state_read_server_certificate;
David Benjaminc895d6b2016-08-11 13:26:41 -0400479 return ssl_hs_ok;
480 }
481
David Benjaminc895d6b2016-08-11 13:26:41 -0400482
Robert Sloan8542c082018-02-05 09:07:34 -0800483 bool have_sigalgs = false, have_ca = false;
484 CBS sigalgs, ca;
485 const SSL_EXTENSION_TYPE ext_types[] = {
486 {TLSEXT_TYPE_signature_algorithms, &have_sigalgs, &sigalgs},
487 {TLSEXT_TYPE_certificate_authorities, &have_ca, &ca},
488 };
David Benjaminc895d6b2016-08-11 13:26:41 -0400489
Robert Sloan8542c082018-02-05 09:07:34 -0800490 CBS body = msg.body, context, extensions, supported_signature_algorithms;
491 uint8_t alert = SSL_AD_DECODE_ERROR;
492 if (!CBS_get_u8_length_prefixed(&body, &context) ||
493 // The request context is always empty during the handshake.
494 CBS_len(&context) != 0 ||
495 !CBS_get_u16_length_prefixed(&body, &extensions) ||
496 CBS_len(&body) != 0 ||
497 !ssl_parse_extensions(&extensions, &alert, ext_types,
498 OPENSSL_ARRAY_SIZE(ext_types),
499 1 /* accept unknown */) ||
500 (have_ca && CBS_len(&ca) == 0) ||
501 !have_sigalgs ||
502 !CBS_get_u16_length_prefixed(&sigalgs,
503 &supported_signature_algorithms) ||
504 CBS_len(&supported_signature_algorithms) == 0 ||
505 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
506 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
507 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
508 return ssl_hs_error;
509 }
Robert Sloanb1b54b82017-11-06 13:50:02 -0800510
Robert Sloan8542c082018-02-05 09:07:34 -0800511 if (have_ca) {
512 hs->ca_names = ssl_parse_client_CA_list(ssl, &alert, &ca);
Robert Sloanb1b54b82017-11-06 13:50:02 -0800513 if (!hs->ca_names) {
514 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
515 return ssl_hs_error;
516 }
Robert Sloan8542c082018-02-05 09:07:34 -0800517 } else {
518 hs->ca_names.reset(sk_CRYPTO_BUFFER_new_null());
519 if (!hs->ca_names) {
520 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
521 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloanb1b54b82017-11-06 13:50:02 -0800522 return ssl_hs_error;
523 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400524 }
525
Robert Sloana27a6a42017-09-05 08:39:28 -0700526 hs->cert_request = true;
Robert Sloan7d422bc2017-03-06 10:04:29 -0800527 ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400528
Robert Sloan84377092017-08-14 09:33:19 -0700529 if (!ssl_hash_message(hs, msg)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400530 return ssl_hs_error;
531 }
532
Robert Sloan84377092017-08-14 09:33:19 -0700533 ssl->method->next_message(ssl);
534 hs->tls13_state = state_read_server_certificate;
535 return ssl_hs_ok;
David Benjaminc895d6b2016-08-11 13:26:41 -0400536}
537
Robert Sloan84377092017-08-14 09:33:19 -0700538static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500539 SSL *const ssl = hs->ssl;
Robert Sloan84377092017-08-14 09:33:19 -0700540 SSLMessage msg;
541 if (!ssl->method->get_message(ssl, &msg)) {
542 return ssl_hs_read_message;
543 }
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100544
545 if (msg.type != SSL3_MT_COMPRESSED_CERTIFICATE &&
546 !ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE)) {
547 return ssl_hs_error;
548 }
549
550 if (!tls13_process_certificate(hs, msg, 0 /* certificate required */) ||
Robert Sloan84377092017-08-14 09:33:19 -0700551 !ssl_hash_message(hs, msg)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400552 return ssl_hs_error;
553 }
554
Robert Sloan84377092017-08-14 09:33:19 -0700555 ssl->method->next_message(ssl);
556 hs->tls13_state = state_read_server_certificate_verify;
557 return ssl_hs_ok;
David Benjaminc895d6b2016-08-11 13:26:41 -0400558}
559
Robert Sloan84377092017-08-14 09:33:19 -0700560static enum ssl_hs_wait_t do_read_server_certificate_verify(
David Benjamin1b249672016-12-06 18:25:50 -0500561 SSL_HANDSHAKE *hs) {
562 SSL *const ssl = hs->ssl;
Robert Sloan84377092017-08-14 09:33:19 -0700563 SSLMessage msg;
564 if (!ssl->method->get_message(ssl, &msg)) {
565 return ssl_hs_read_message;
566 }
Robert Sloanb6d070c2017-07-24 08:40:01 -0700567 switch (ssl_verify_peer_cert(hs)) {
568 case ssl_verify_ok:
569 break;
570 case ssl_verify_invalid:
571 return ssl_hs_error;
572 case ssl_verify_retry:
Robert Sloan84377092017-08-14 09:33:19 -0700573 hs->tls13_state = state_read_server_certificate_verify;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700574 return ssl_hs_certificate_verify;
575 }
576
Robert Sloan84377092017-08-14 09:33:19 -0700577 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY) ||
578 !tls13_process_certificate_verify(hs, msg) ||
579 !ssl_hash_message(hs, msg)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500580 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400581 }
582
Robert Sloan84377092017-08-14 09:33:19 -0700583 ssl->method->next_message(ssl);
584 hs->tls13_state = state_read_server_finished;
585 return ssl_hs_ok;
David Benjaminc895d6b2016-08-11 13:26:41 -0400586}
587
Robert Sloan84377092017-08-14 09:33:19 -0700588static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500589 SSL *const ssl = hs->ssl;
Robert Sloan84377092017-08-14 09:33:19 -0700590 SSLMessage msg;
591 if (!ssl->method->get_message(ssl, &msg)) {
592 return ssl_hs_read_message;
593 }
594 if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED) ||
595 !tls13_process_finished(hs, msg, 0 /* don't use saved value */) ||
596 !ssl_hash_message(hs, msg) ||
Robert Sloana27a6a42017-09-05 08:39:28 -0700597 // Update the secret to the master secret and derive traffic keys.
David Benjamin1b249672016-12-06 18:25:50 -0500598 !tls13_advance_key_schedule(hs, kZeroes, hs->hash_len) ||
599 !tls13_derive_application_secrets(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400600 return ssl_hs_error;
601 }
602
Robert Sloan84377092017-08-14 09:33:19 -0700603 ssl->method->next_message(ssl);
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700604 hs->tls13_state = state_send_end_of_early_data;
605 return ssl_hs_ok;
606}
607
608static enum ssl_hs_wait_t do_send_end_of_early_data(SSL_HANDSHAKE *hs) {
609 SSL *const ssl = hs->ssl;
Robert Sloane56da3e2017-06-26 08:26:42 -0700610
Robert Sloan0da43952018-01-03 15:13:14 -0800611 if (ssl->s3->early_data_accepted) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700612 hs->can_early_write = false;
Robert Sloan8542c082018-02-05 09:07:34 -0800613 ScopedCBB cbb;
614 CBB body;
615 if (!ssl->method->init_message(ssl, cbb.get(), &body,
616 SSL3_MT_END_OF_EARLY_DATA) ||
617 !ssl_add_message_cbb(ssl, cbb.get())) {
618 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700619 }
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700620 }
621
Robert Sloana12bf462017-07-17 07:08:26 -0700622 if (hs->early_data_offered) {
Robert Sloand5c22152017-11-13 09:22:12 -0800623 if (!tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_handshake_secret,
Robert Sloana12bf462017-07-17 07:08:26 -0700624 hs->hash_len)) {
625 return ssl_hs_error;
626 }
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700627 }
628
Steven Valdeze7531f02016-12-14 13:29:57 -0500629 hs->tls13_state = state_send_client_certificate;
David Benjaminc895d6b2016-08-11 13:26:41 -0400630 return ssl_hs_ok;
631}
632
Steven Valdeze7531f02016-12-14 13:29:57 -0500633static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500634 SSL *const ssl = hs->ssl;
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700635
Robert Sloana27a6a42017-09-05 08:39:28 -0700636 // The peer didn't request a certificate.
David Benjamin1b249672016-12-06 18:25:50 -0500637 if (!hs->cert_request) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800638 hs->tls13_state = state_complete_second_flight;
David Benjaminc895d6b2016-08-11 13:26:41 -0400639 return ssl_hs_ok;
640 }
641
Robert Sloana27a6a42017-09-05 08:39:28 -0700642 // Call cert_cb to update the certificate.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100643 if (hs->config->cert->cert_cb != NULL) {
644 int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg);
David Benjaminc895d6b2016-08-11 13:26:41 -0400645 if (rv == 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700646 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -0400647 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
648 return ssl_hs_error;
649 }
650 if (rv < 0) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500651 hs->tls13_state = state_send_client_certificate;
David Benjaminc895d6b2016-08-11 13:26:41 -0400652 return ssl_hs_x509_lookup;
653 }
654 }
655
Robert Sloan572a4e22017-04-17 10:52:19 -0700656 if (!ssl_on_certificate_selected(hs) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -0800657 !tls13_add_certificate(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400658 return ssl_hs_error;
659 }
660
Steven Valdeze7531f02016-12-14 13:29:57 -0500661 hs->tls13_state = state_send_client_certificate_verify;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800662 return ssl_hs_ok;
David Benjaminc895d6b2016-08-11 13:26:41 -0400663}
664
Robert Sloane56da3e2017-06-26 08:26:42 -0700665static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700666 // Don't send CertificateVerify if there is no certificate.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100667 if (!ssl_has_certificate(hs->config)) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800668 hs->tls13_state = state_complete_second_flight;
David Benjaminc895d6b2016-08-11 13:26:41 -0400669 return ssl_hs_ok;
670 }
671
Robert Sloane56da3e2017-06-26 08:26:42 -0700672 switch (tls13_add_certificate_verify(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400673 case ssl_private_key_success:
Robert Sloan4d1ac502017-02-06 08:36:14 -0800674 hs->tls13_state = state_complete_second_flight;
675 return ssl_hs_ok;
David Benjaminc895d6b2016-08-11 13:26:41 -0400676
677 case ssl_private_key_retry:
Robert Sloane56da3e2017-06-26 08:26:42 -0700678 hs->tls13_state = state_send_client_certificate_verify;
David Benjaminc895d6b2016-08-11 13:26:41 -0400679 return ssl_hs_private_key_operation;
680
681 case ssl_private_key_failure:
682 return ssl_hs_error;
683 }
684
685 assert(0);
686 return ssl_hs_error;
687}
688
Robert Sloan4d1ac502017-02-06 08:36:14 -0800689static enum ssl_hs_wait_t do_complete_second_flight(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500690 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800691
Robert Sloana27a6a42017-09-05 08:39:28 -0700692 // Send a Channel ID assertion if necessary.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100693 if (ssl->s3->channel_id_valid) {
694 if (!ssl_do_channel_id_callback(hs)) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800695 hs->tls13_state = state_complete_second_flight;
696 return ssl_hs_error;
697 }
698
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100699 if (hs->config->channel_id_private == NULL) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800700 return ssl_hs_channel_id_lookup;
701 }
702
Robert Sloanb6d070c2017-07-24 08:40:01 -0700703 ScopedCBB cbb;
704 CBB body;
705 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) ||
Robert Sloan5d625782017-02-13 09:55:39 -0800706 !tls1_write_channel_id(hs, &body) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -0700707 !ssl_add_message_cbb(ssl, cbb.get())) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800708 return ssl_hs_error;
709 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500710 }
711
Robert Sloana27a6a42017-09-05 08:39:28 -0700712 // Send a Finished message.
Robert Sloan4d1ac502017-02-06 08:36:14 -0800713 if (!tls13_add_finished(hs)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500714 return ssl_hs_error;
715 }
716
Robert Sloana27a6a42017-09-05 08:39:28 -0700717 // Derive the final keys and enable them.
Steven Valdez909b19f2016-11-21 15:35:44 -0500718 if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->server_traffic_secret_0,
719 hs->hash_len) ||
720 !tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_traffic_secret_0,
721 hs->hash_len) ||
David Benjamin1b249672016-12-06 18:25:50 -0500722 !tls13_derive_resumption_secret(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400723 return ssl_hs_error;
724 }
725
Steven Valdeze7531f02016-12-14 13:29:57 -0500726 hs->tls13_state = state_done;
David Benjaminc895d6b2016-08-11 13:26:41 -0400727 return ssl_hs_flush;
728}
729
David Benjamin1b249672016-12-06 18:25:50 -0500730enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500731 while (hs->tls13_state != state_done) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400732 enum ssl_hs_wait_t ret = ssl_hs_error;
Robert Sloana12bf462017-07-17 07:08:26 -0700733 enum client_hs_state_t state =
734 static_cast<enum client_hs_state_t>(hs->tls13_state);
David Benjaminc895d6b2016-08-11 13:26:41 -0400735 switch (state) {
Robert Sloan84377092017-08-14 09:33:19 -0700736 case state_read_hello_retry_request:
737 ret = do_read_hello_retry_request(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400738 break;
739 case state_send_second_client_hello:
David Benjamin1b249672016-12-06 18:25:50 -0500740 ret = do_send_second_client_hello(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400741 break;
Robert Sloan84377092017-08-14 09:33:19 -0700742 case state_read_server_hello:
743 ret = do_read_server_hello(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400744 break;
Robert Sloan84377092017-08-14 09:33:19 -0700745 case state_read_encrypted_extensions:
746 ret = do_read_encrypted_extensions(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400747 break;
Robert Sloan84377092017-08-14 09:33:19 -0700748 case state_read_certificate_request:
749 ret = do_read_certificate_request(hs);
Robert Sloane56da3e2017-06-26 08:26:42 -0700750 break;
Robert Sloan84377092017-08-14 09:33:19 -0700751 case state_read_server_certificate:
752 ret = do_read_server_certificate(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400753 break;
Robert Sloan84377092017-08-14 09:33:19 -0700754 case state_read_server_certificate_verify:
755 ret = do_read_server_certificate_verify(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400756 break;
Robert Sloan84377092017-08-14 09:33:19 -0700757 case state_read_server_finished:
758 ret = do_read_server_finished(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400759 break;
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700760 case state_send_end_of_early_data:
761 ret = do_send_end_of_early_data(hs);
762 break;
David Benjaminc895d6b2016-08-11 13:26:41 -0400763 case state_send_client_certificate:
David Benjamin1b249672016-12-06 18:25:50 -0500764 ret = do_send_client_certificate(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400765 break;
766 case state_send_client_certificate_verify:
Robert Sloane56da3e2017-06-26 08:26:42 -0700767 ret = do_send_client_certificate_verify(hs);
Steven Valdez909b19f2016-11-21 15:35:44 -0500768 break;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800769 case state_complete_second_flight:
770 ret = do_complete_second_flight(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400771 break;
772 case state_done:
773 ret = ssl_hs_ok;
774 break;
775 }
776
Robert Sloana27a6a42017-09-05 08:39:28 -0700777 if (hs->tls13_state != state) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700778 ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1);
779 }
780
David Benjaminc895d6b2016-08-11 13:26:41 -0400781 if (ret != ssl_hs_ok) {
782 return ret;
783 }
784 }
785
786 return ssl_hs_ok;
787}
788
Robert Sloan8f860b12017-08-28 07:37:06 -0700789const char *tls13_client_handshake_state(SSL_HANDSHAKE *hs) {
790 enum client_hs_state_t state =
791 static_cast<enum client_hs_state_t>(hs->tls13_state);
792 switch (state) {
793 case state_read_hello_retry_request:
794 return "TLS 1.3 client read_hello_retry_request";
795 case state_send_second_client_hello:
796 return "TLS 1.3 client send_second_client_hello";
797 case state_read_server_hello:
798 return "TLS 1.3 client read_server_hello";
Robert Sloan8f860b12017-08-28 07:37:06 -0700799 case state_read_encrypted_extensions:
800 return "TLS 1.3 client read_encrypted_extensions";
801 case state_read_certificate_request:
802 return "TLS 1.3 client read_certificate_request";
803 case state_read_server_certificate:
804 return "TLS 1.3 client read_server_certificate";
805 case state_read_server_certificate_verify:
806 return "TLS 1.3 client read_server_certificate_verify";
807 case state_read_server_finished:
808 return "TLS 1.3 client read_server_finished";
809 case state_send_end_of_early_data:
810 return "TLS 1.3 client send_end_of_early_data";
811 case state_send_client_certificate:
812 return "TLS 1.3 client send_client_certificate";
813 case state_send_client_certificate_verify:
814 return "TLS 1.3 client send_client_certificate_verify";
815 case state_complete_second_flight:
816 return "TLS 1.3 client complete_second_flight";
817 case state_done:
818 return "TLS 1.3 client done";
819 }
820
821 return "TLS 1.3 client unknown";
822}
823
Robert Sloan84377092017-08-14 09:33:19 -0700824int tls13_process_new_session_ticket(SSL *ssl, const SSLMessage &msg) {
Robert Sloan36272962017-10-23 10:28:39 -0700825 if (ssl->s3->write_shutdown != ssl_shutdown_none) {
826 // Ignore tickets on shutdown. Callers tend to indiscriminately call
827 // |SSL_shutdown| before destroying an |SSL|, at which point calling the new
828 // session callback may be confusing.
829 return 1;
830 }
831
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700832 UniquePtr<SSL_SESSION> session = SSL_SESSION_dup(
833 ssl->s3->established_session.get(), SSL_SESSION_INCLUDE_NONAUTH);
Robert Sloana12bf462017-07-17 07:08:26 -0700834 if (!session) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400835 return 0;
836 }
837
Robert Sloana12bf462017-07-17 07:08:26 -0700838 ssl_session_rebase_time(ssl, session.get());
Steven Valdez909b19f2016-11-21 15:35:44 -0500839
Robert Sloan4d1ac502017-02-06 08:36:14 -0800840 uint32_t server_timeout;
Robert Sloanb1b54b82017-11-06 13:50:02 -0800841 CBS body = msg.body, ticket_nonce, ticket, extensions;
Robert Sloan84377092017-08-14 09:33:19 -0700842 if (!CBS_get_u32(&body, &server_timeout) ||
843 !CBS_get_u32(&body, &session->ticket_age_add) ||
Robert Sloan8542c082018-02-05 09:07:34 -0800844 !CBS_get_u8_length_prefixed(&body, &ticket_nonce) ||
Robert Sloan84377092017-08-14 09:33:19 -0700845 !CBS_get_u16_length_prefixed(&body, &ticket) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100846 !session->ticket.CopyFrom(ticket) ||
Robert Sloan84377092017-08-14 09:33:19 -0700847 !CBS_get_u16_length_prefixed(&body, &extensions) ||
848 CBS_len(&body) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700849 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -0400850 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloana12bf462017-07-17 07:08:26 -0700851 return 0;
Robert Sloan69939df2017-01-09 10:53:07 -0800852 }
853
Robert Sloana27a6a42017-09-05 08:39:28 -0700854 // Cap the renewable lifetime by the server advertised value. This avoids
855 // wasting bandwidth on 0-RTT when we know the server will reject it.
Robert Sloan7d422bc2017-03-06 10:04:29 -0800856 if (session->timeout > server_timeout) {
857 session->timeout = server_timeout;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800858 }
859
Robert Sloanb1b54b82017-11-06 13:50:02 -0800860 if (!tls13_derive_session_psk(session.get(), ticket_nonce)) {
861 return 0;
862 }
863
Robert Sloana27a6a42017-09-05 08:39:28 -0700864 // Parse out the extensions.
865 bool have_early_data_info = false;
Robert Sloan69939df2017-01-09 10:53:07 -0800866 CBS early_data_info;
867 const SSL_EXTENSION_TYPE ext_types[] = {
Robert Sloan8542c082018-02-05 09:07:34 -0800868 {TLSEXT_TYPE_early_data, &have_early_data_info, &early_data_info},
Robert Sloan69939df2017-01-09 10:53:07 -0800869 };
870
Robert Sloan5d625782017-02-13 09:55:39 -0800871 uint8_t alert = SSL_AD_DECODE_ERROR;
Robert Sloan69939df2017-01-09 10:53:07 -0800872 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
873 OPENSSL_ARRAY_SIZE(ext_types),
874 1 /* ignore unknown */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700875 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana12bf462017-07-17 07:08:26 -0700876 return 0;
Robert Sloan69939df2017-01-09 10:53:07 -0800877 }
878
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100879 if (have_early_data_info && ssl->enable_early_data) {
Robert Sloan69939df2017-01-09 10:53:07 -0800880 if (!CBS_get_u32(&early_data_info, &session->ticket_max_early_data) ||
881 CBS_len(&early_data_info) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700882 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloan69939df2017-01-09 10:53:07 -0800883 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloana12bf462017-07-17 07:08:26 -0700884 return 0;
Robert Sloan69939df2017-01-09 10:53:07 -0800885 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400886 }
887
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100888 session->ticket_age_add_valid = true;
889 session->not_resumable = false;
David Benjaminc895d6b2016-08-11 13:26:41 -0400890
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100891 if ((ssl->session_ctx->session_cache_mode & SSL_SESS_CACHE_CLIENT) &&
892 ssl->session_ctx->new_session_cb != NULL &&
893 ssl->session_ctx->new_session_cb(ssl, session.get())) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700894 // |new_session_cb|'s return value signals that it took ownership.
Robert Sloana12bf462017-07-17 07:08:26 -0700895 session.release();
David Benjaminc895d6b2016-08-11 13:26:41 -0400896 }
897
Robert Sloana12bf462017-07-17 07:08:26 -0700898 return 1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400899}
David Benjamin7c0d06c2016-08-11 13:26:41 -0400900
Robert Sloanb6d070c2017-07-24 08:40:01 -0700901} // namespace bssl