blob: 50f7e5a6f9dfdc508a5f0d25c654617d7b045528 [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
21#include <openssl/bytestring.h>
22#include <openssl/digest.h>
23#include <openssl/err.h>
24#include <openssl/mem.h>
25#include <openssl/stack.h>
26#include <openssl/x509.h>
27
Steven Valdez909b19f2016-11-21 15:35:44 -050028#include "../crypto/internal.h"
David Benjaminc895d6b2016-08-11 13:26:41 -040029#include "internal.h"
30
31
32enum client_hs_state_t {
33 state_process_hello_retry_request = 0,
34 state_send_second_client_hello,
David Benjaminc895d6b2016-08-11 13:26:41 -040035 state_process_server_hello,
36 state_process_encrypted_extensions,
37 state_process_certificate_request,
38 state_process_server_certificate,
39 state_process_server_certificate_verify,
40 state_process_server_finished,
David Benjaminc895d6b2016-08-11 13:26:41 -040041 state_send_client_certificate,
42 state_send_client_certificate_verify,
43 state_complete_client_certificate_verify,
Robert Sloan4d1ac502017-02-06 08:36:14 -080044 state_complete_second_flight,
David Benjaminc895d6b2016-08-11 13:26:41 -040045 state_done,
46};
47
David Benjamin1b249672016-12-06 18:25:50 -050048static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0};
49
50static enum ssl_hs_wait_t do_process_hello_retry_request(SSL_HANDSHAKE *hs) {
51 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -040052 if (ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) {
Steven Valdeze7531f02016-12-14 13:29:57 -050053 hs->tls13_state = state_process_server_hello;
David Benjaminc895d6b2016-08-11 13:26:41 -040054 return ssl_hs_ok;
55 }
56
57 CBS cbs, extensions;
David Benjamin95add822016-10-19 01:09:12 -040058 uint16_t server_wire_version;
David Benjaminc895d6b2016-08-11 13:26:41 -040059 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
60 if (!CBS_get_u16(&cbs, &server_wire_version) ||
David Benjaminc895d6b2016-08-11 13:26:41 -040061 !CBS_get_u16_length_prefixed(&cbs, &extensions) ||
David Benjamin95add822016-10-19 01:09:12 -040062 /* HelloRetryRequest may not be empty. */
63 CBS_len(&extensions) == 0 ||
David Benjaminc895d6b2016-08-11 13:26:41 -040064 CBS_len(&cbs) != 0) {
David Benjamin95add822016-10-19 01:09:12 -040065 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -040066 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
67 return ssl_hs_error;
68 }
69
Steven Valdez909b19f2016-11-21 15:35:44 -050070 int have_cookie, have_key_share;
71 CBS cookie, key_share;
72 const SSL_EXTENSION_TYPE ext_types[] = {
73 {TLSEXT_TYPE_key_share, &have_key_share, &key_share},
74 {TLSEXT_TYPE_cookie, &have_cookie, &cookie},
75 };
76
Robert Sloan5d625782017-02-13 09:55:39 -080077 uint8_t alert = SSL_AD_DECODE_ERROR;
Steven Valdez909b19f2016-11-21 15:35:44 -050078 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
Robert Sloan69939df2017-01-09 10:53:07 -080079 OPENSSL_ARRAY_SIZE(ext_types),
80 0 /* reject unknown */)) {
Steven Valdez909b19f2016-11-21 15:35:44 -050081 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
82 return ssl_hs_error;
83 }
84
85 if (have_cookie) {
86 CBS cookie_value;
87 if (!CBS_get_u16_length_prefixed(&cookie, &cookie_value) ||
88 CBS_len(&cookie_value) == 0 ||
89 CBS_len(&cookie) != 0) {
David Benjamin95add822016-10-19 01:09:12 -040090 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
91 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
92 return ssl_hs_error;
93 }
David Benjaminc895d6b2016-08-11 13:26:41 -040094
Steven Valdez909b19f2016-11-21 15:35:44 -050095 if (!CBS_stow(&cookie_value, &hs->cookie, &hs->cookie_len)) {
96 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -040097 }
98 }
99
Steven Valdez909b19f2016-11-21 15:35:44 -0500100 if (have_key_share) {
101 uint16_t group_id;
102 if (!CBS_get_u16(&key_share, &group_id) || CBS_len(&key_share) != 0) {
103 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
104 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
105 return ssl_hs_error;
106 }
107
108 /* The group must be supported. */
109 const uint16_t *groups;
110 size_t groups_len;
111 tls1_get_grouplist(ssl, &groups, &groups_len);
112 int found = 0;
113 for (size_t i = 0; i < groups_len; i++) {
114 if (groups[i] == group_id) {
115 found = 1;
116 break;
117 }
118 }
119
120 if (!found) {
121 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
122 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
123 return ssl_hs_error;
124 }
125
126 /* Check that the HelloRetryRequest does not request the key share that
127 * was provided in the initial ClientHello. */
128 if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) == group_id) {
129 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
130 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
131 return ssl_hs_error;
132 }
133
134 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
135 hs->retry_group = group_id;
136 }
137
Robert Sloan5d625782017-02-13 09:55:39 -0800138 if (!ssl_hash_current_message(hs)) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800139 return ssl_hs_error;
140 }
141
David Benjamin95add822016-10-19 01:09:12 -0400142 hs->received_hello_retry_request = 1;
Steven Valdeze7531f02016-12-14 13:29:57 -0500143 hs->tls13_state = state_send_second_client_hello;
David Benjaminc895d6b2016-08-11 13:26:41 -0400144 return ssl_hs_ok;
145}
146
David Benjamin1b249672016-12-06 18:25:50 -0500147static enum ssl_hs_wait_t do_send_second_client_hello(SSL_HANDSHAKE *hs) {
148 if (!ssl_write_client_hello(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400149 return ssl_hs_error;
150 }
151
Steven Valdeze7531f02016-12-14 13:29:57 -0500152 hs->tls13_state = state_process_server_hello;
David Benjaminc895d6b2016-08-11 13:26:41 -0400153 return ssl_hs_flush_and_read_message;
154}
155
David Benjamin1b249672016-12-06 18:25:50 -0500156static enum ssl_hs_wait_t do_process_server_hello(SSL_HANDSHAKE *hs) {
157 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800158 if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400159 return ssl_hs_error;
160 }
161
162 CBS cbs, server_random, extensions;
163 uint16_t server_wire_version;
164 uint16_t cipher_suite;
165 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
166 if (!CBS_get_u16(&cbs, &server_wire_version) ||
167 !CBS_get_bytes(&cbs, &server_random, SSL3_RANDOM_SIZE) ||
168 !CBS_get_u16(&cbs, &cipher_suite) ||
169 !CBS_get_u16_length_prefixed(&cbs, &extensions) ||
170 CBS_len(&cbs) != 0) {
171 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
172 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
173 return ssl_hs_error;
174 }
175
176 if (server_wire_version != ssl->version) {
177 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
178 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
179 return ssl_hs_error;
180 }
181
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400182 assert(ssl->s3->have_version);
Robert Sloan69939df2017-01-09 10:53:07 -0800183 OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
184 SSL3_RANDOM_SIZE);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400185
186 const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
187 if (cipher == NULL) {
188 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
189 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
190 return ssl_hs_error;
191 }
192
Steven Valdez909b19f2016-11-21 15:35:44 -0500193 /* Check if the cipher is a TLS 1.3 cipher. */
194 if (SSL_CIPHER_get_min_version(cipher) > ssl3_protocol_version(ssl) ||
195 SSL_CIPHER_get_max_version(cipher) < ssl3_protocol_version(ssl)) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400196 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
197 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
198 return ssl_hs_error;
199 }
200
David Benjaminc895d6b2016-08-11 13:26:41 -0400201 /* Parse out the extensions. */
Robert Sloan69939df2017-01-09 10:53:07 -0800202 int have_key_share = 0, have_pre_shared_key = 0, have_short_header = 0;
203 CBS key_share, pre_shared_key, short_header;
Steven Valdez909b19f2016-11-21 15:35:44 -0500204 const SSL_EXTENSION_TYPE ext_types[] = {
205 {TLSEXT_TYPE_key_share, &have_key_share, &key_share},
206 {TLSEXT_TYPE_pre_shared_key, &have_pre_shared_key, &pre_shared_key},
Robert Sloan69939df2017-01-09 10:53:07 -0800207 {TLSEXT_TYPE_short_header, &have_short_header, &short_header},
Steven Valdez909b19f2016-11-21 15:35:44 -0500208 };
David Benjaminc895d6b2016-08-11 13:26:41 -0400209
Robert Sloan5d625782017-02-13 09:55:39 -0800210 uint8_t alert = SSL_AD_DECODE_ERROR;
Steven Valdez909b19f2016-11-21 15:35:44 -0500211 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
Robert Sloan69939df2017-01-09 10:53:07 -0800212 OPENSSL_ARRAY_SIZE(ext_types),
213 0 /* reject unknown */)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500214 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
215 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400216 }
217
Steven Valdez909b19f2016-11-21 15:35:44 -0500218 alert = SSL_AD_DECODE_ERROR;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400219 if (have_pre_shared_key) {
220 if (ssl->session == NULL) {
221 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
222 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
223 return ssl_hs_error;
224 }
225
David Benjamin1b249672016-12-06 18:25:50 -0500226 if (!ssl_ext_pre_shared_key_parse_serverhello(hs, &alert,
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400227 &pre_shared_key)) {
228 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
229 return ssl_hs_error;
230 }
231
232 if (ssl->session->ssl_version != ssl->version) {
233 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
234 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
235 return ssl_hs_error;
236 }
237
Steven Valdez909b19f2016-11-21 15:35:44 -0500238 if (ssl->session->cipher->algorithm_prf != cipher->algorithm_prf) {
239 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_PRF_HASH_MISMATCH);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400240 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
241 return ssl_hs_error;
242 }
243
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400244 if (!ssl_session_is_context_valid(ssl, ssl->session)) {
245 /* This is actually a client application bug. */
246 OPENSSL_PUT_ERROR(SSL,
247 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
248 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
249 return ssl_hs_error;
250 }
251
252 ssl->s3->session_reused = 1;
253 /* Only authentication information carries over in TLS 1.3. */
254 ssl->s3->new_session =
255 SSL_SESSION_dup(ssl->session, SSL_SESSION_DUP_AUTH_ONLY);
256 if (ssl->s3->new_session == NULL) {
257 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
258 return ssl_hs_error;
259 }
David Benjamin7c0d06c2016-08-11 13:26:41 -0400260 ssl_set_session(ssl, NULL);
Robert Sloan4d1ac502017-02-06 08:36:14 -0800261
262 /* Resumption incorporates fresh key material, so refresh the timeout. */
263 ssl_session_renew_timeout(ssl, ssl->s3->new_session,
Robert Sloan5d625782017-02-13 09:55:39 -0800264 ssl->initial_ctx->session_psk_dhe_timeout);
David Benjamin1b249672016-12-06 18:25:50 -0500265 } else if (!ssl_get_new_session(hs, 0)) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400266 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -0400267 return ssl_hs_error;
268 }
269
David Benjaminc895d6b2016-08-11 13:26:41 -0400270 ssl->s3->new_session->cipher = cipher;
271 ssl->s3->tmp.new_cipher = cipher;
272
273 /* The PRF hash is now known. Set up the key schedule. */
David Benjamin1b249672016-12-06 18:25:50 -0500274 if (!tls13_init_key_schedule(hs)) {
275 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400276 }
277
David Benjamin1b249672016-12-06 18:25:50 -0500278 /* Incorporate the PSK into the running secret. */
279 if (ssl->s3->session_reused) {
280 if (!tls13_advance_key_schedule(hs, ssl->s3->new_session->master_key,
281 ssl->s3->new_session->master_key_length)) {
282 return ssl_hs_error;
283 }
Robert Sloan5d625782017-02-13 09:55:39 -0800284 } else if (!tls13_advance_key_schedule(hs, kZeroes, hs->hash_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400285 return ssl_hs_error;
286 }
287
Steven Valdeze7531f02016-12-14 13:29:57 -0500288 if (!have_key_share) {
289 /* We do not support psk_ke and thus always require a key share. */
290 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE);
291 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION);
292 return ssl_hs_error;
293 }
294
David Benjaminc895d6b2016-08-11 13:26:41 -0400295 /* Resolve ECDHE and incorporate it into the secret. */
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400296 uint8_t *dhe_secret;
297 size_t dhe_secret_len;
Robert Sloan5d625782017-02-13 09:55:39 -0800298 alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -0500299 if (!ssl_ext_key_share_parse_serverhello(hs, &dhe_secret, &dhe_secret_len,
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400300 &alert, &key_share)) {
301 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
302 return ssl_hs_error;
303 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400304
David Benjamin1b249672016-12-06 18:25:50 -0500305 if (!tls13_advance_key_schedule(hs, dhe_secret, dhe_secret_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400306 OPENSSL_free(dhe_secret);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400307 return ssl_hs_error;
308 }
309 OPENSSL_free(dhe_secret);
310
Robert Sloan69939df2017-01-09 10:53:07 -0800311 /* Negotiate short record headers. */
312 if (have_short_header) {
313 if (CBS_len(&short_header) != 0) {
314 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
315 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
316 return ssl_hs_error;
317 }
318
319 if (!ssl->ctx->short_header_enabled) {
320 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
321 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
322 return ssl_hs_error;
323 }
324
325 ssl->s3->short_header = 1;
326 }
327
Robert Sloan5d625782017-02-13 09:55:39 -0800328 if (!ssl_hash_current_message(hs) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -0800329 !tls13_derive_handshake_secrets(hs) ||
Robert Sloan69939df2017-01-09 10:53:07 -0800330 !tls13_set_traffic_key(ssl, evp_aead_open, hs->server_handshake_secret,
331 hs->hash_len) ||
332 !tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_handshake_secret,
333 hs->hash_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400334 return ssl_hs_error;
335 }
336
Steven Valdeze7531f02016-12-14 13:29:57 -0500337 hs->tls13_state = state_process_encrypted_extensions;
David Benjaminc895d6b2016-08-11 13:26:41 -0400338 return ssl_hs_read_message;
339}
340
David Benjamin1b249672016-12-06 18:25:50 -0500341static enum ssl_hs_wait_t do_process_encrypted_extensions(SSL_HANDSHAKE *hs) {
342 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800343 if (!ssl_check_message_type(ssl, SSL3_MT_ENCRYPTED_EXTENSIONS)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400344 return ssl_hs_error;
345 }
346
347 CBS cbs;
348 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
David Benjamin1b249672016-12-06 18:25:50 -0500349 if (!ssl_parse_serverhello_tlsext(hs, &cbs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400350 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
351 return ssl_hs_error;
352 }
353 if (CBS_len(&cbs) != 0) {
354 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
355 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
356 return ssl_hs_error;
357 }
358
Robert Sloan5d625782017-02-13 09:55:39 -0800359 if (!ssl_hash_current_message(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400360 return ssl_hs_error;
361 }
362
Steven Valdeze7531f02016-12-14 13:29:57 -0500363 hs->tls13_state = state_process_certificate_request;
David Benjaminc895d6b2016-08-11 13:26:41 -0400364 return ssl_hs_read_message;
365}
366
David Benjamin1b249672016-12-06 18:25:50 -0500367static enum ssl_hs_wait_t do_process_certificate_request(SSL_HANDSHAKE *hs) {
368 SSL *const ssl = hs->ssl;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400369 /* CertificateRequest may only be sent in non-resumption handshakes. */
370 if (ssl->s3->session_reused) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500371 hs->tls13_state = state_process_server_finished;
David Benjaminc895d6b2016-08-11 13:26:41 -0400372 return ssl_hs_ok;
373 }
374
375 /* CertificateRequest is optional. */
376 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500377 hs->tls13_state = state_process_server_certificate;
David Benjaminc895d6b2016-08-11 13:26:41 -0400378 return ssl_hs_ok;
379 }
380
381 CBS cbs, context, supported_signature_algorithms;
382 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
383 if (!CBS_get_u8_length_prefixed(&cbs, &context) ||
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400384 /* The request context is always empty during the handshake. */
385 CBS_len(&context) != 0 ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400386 !CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
387 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjamin1b249672016-12-06 18:25:50 -0500388 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400389 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
390 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
391 return ssl_hs_error;
392 }
393
Robert Sloan5d625782017-02-13 09:55:39 -0800394 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminc895d6b2016-08-11 13:26:41 -0400395 STACK_OF(X509_NAME) *ca_sk = ssl_parse_client_CA_list(ssl, &alert, &cbs);
396 if (ca_sk == NULL) {
397 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
398 return ssl_hs_error;
399 }
400
401 /* Ignore extensions. */
402 CBS extensions;
403 if (!CBS_get_u16_length_prefixed(&cbs, &extensions) ||
404 CBS_len(&cbs) != 0) {
405 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin7c0d06c2016-08-11 13:26:41 -0400406 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
David Benjaminc895d6b2016-08-11 13:26:41 -0400407 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
408 return ssl_hs_error;
409 }
410
David Benjamin1b249672016-12-06 18:25:50 -0500411 hs->cert_request = 1;
412 sk_X509_NAME_pop_free(hs->ca_names, X509_NAME_free);
413 hs->ca_names = ca_sk;
David Benjaminc895d6b2016-08-11 13:26:41 -0400414
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
Steven Valdeze7531f02016-12-14 13:29:57 -0500419 hs->tls13_state = state_process_server_certificate;
David Benjaminc895d6b2016-08-11 13:26:41 -0400420 return ssl_hs_read_message;
421}
422
David Benjamin1b249672016-12-06 18:25:50 -0500423static enum ssl_hs_wait_t do_process_server_certificate(SSL_HANDSHAKE *hs) {
424 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800425 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE) ||
Steven Valdeze7531f02016-12-14 13:29:57 -0500426 !tls13_process_certificate(hs, 0 /* certificate required */) ||
Robert Sloan5d625782017-02-13 09:55:39 -0800427 !ssl_hash_current_message(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400428 return ssl_hs_error;
429 }
430
Steven Valdeze7531f02016-12-14 13:29:57 -0500431 hs->tls13_state = state_process_server_certificate_verify;
David Benjaminc895d6b2016-08-11 13:26:41 -0400432 return ssl_hs_read_message;
433}
434
435static enum ssl_hs_wait_t do_process_server_certificate_verify(
David Benjamin1b249672016-12-06 18:25:50 -0500436 SSL_HANDSHAKE *hs) {
437 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800438 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY) ||
Steven Valdeze7531f02016-12-14 13:29:57 -0500439 !tls13_process_certificate_verify(hs) ||
Robert Sloan5d625782017-02-13 09:55:39 -0800440 !ssl_hash_current_message(hs)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500441 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400442 }
443
Steven Valdeze7531f02016-12-14 13:29:57 -0500444 hs->tls13_state = state_process_server_finished;
David Benjaminc895d6b2016-08-11 13:26:41 -0400445 return ssl_hs_read_message;
446}
447
David Benjamin1b249672016-12-06 18:25:50 -0500448static enum ssl_hs_wait_t do_process_server_finished(SSL_HANDSHAKE *hs) {
449 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800450 if (!ssl_check_message_type(ssl, SSL3_MT_FINISHED) ||
David Benjamin1b249672016-12-06 18:25:50 -0500451 !tls13_process_finished(hs) ||
Robert Sloan5d625782017-02-13 09:55:39 -0800452 !ssl_hash_current_message(hs) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400453 /* Update the secret to the master secret and derive traffic keys. */
David Benjamin1b249672016-12-06 18:25:50 -0500454 !tls13_advance_key_schedule(hs, kZeroes, hs->hash_len) ||
455 !tls13_derive_application_secrets(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400456 return ssl_hs_error;
457 }
458
459 ssl->method->received_flight(ssl);
Steven Valdeze7531f02016-12-14 13:29:57 -0500460 hs->tls13_state = state_send_client_certificate;
David Benjaminc895d6b2016-08-11 13:26:41 -0400461 return ssl_hs_ok;
462}
463
Steven Valdeze7531f02016-12-14 13:29:57 -0500464static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500465 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -0400466 /* The peer didn't request a certificate. */
David Benjamin1b249672016-12-06 18:25:50 -0500467 if (!hs->cert_request) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800468 hs->tls13_state = state_complete_second_flight;
David Benjaminc895d6b2016-08-11 13:26:41 -0400469 return ssl_hs_ok;
470 }
471
472 /* Call cert_cb to update the certificate. */
473 if (ssl->cert->cert_cb != NULL) {
474 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
475 if (rv == 0) {
476 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
477 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
478 return ssl_hs_error;
479 }
480 if (rv < 0) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500481 hs->tls13_state = state_send_client_certificate;
David Benjaminc895d6b2016-08-11 13:26:41 -0400482 return ssl_hs_x509_lookup;
483 }
484 }
485
Robert Sloan69939df2017-01-09 10:53:07 -0800486 if (!ssl_auto_chain_if_needed(ssl) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -0800487 !tls13_add_certificate(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400488 return ssl_hs_error;
489 }
490
Steven Valdeze7531f02016-12-14 13:29:57 -0500491 hs->tls13_state = state_send_client_certificate_verify;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800492 return ssl_hs_ok;
David Benjaminc895d6b2016-08-11 13:26:41 -0400493}
494
David Benjamin1b249672016-12-06 18:25:50 -0500495static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs,
David Benjaminc895d6b2016-08-11 13:26:41 -0400496 int is_first_run) {
David Benjamin1b249672016-12-06 18:25:50 -0500497 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -0400498 /* Don't send CertificateVerify if there is no certificate. */
499 if (!ssl_has_certificate(ssl)) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800500 hs->tls13_state = state_complete_second_flight;
David Benjaminc895d6b2016-08-11 13:26:41 -0400501 return ssl_hs_ok;
502 }
503
Robert Sloan4d1ac502017-02-06 08:36:14 -0800504 switch (tls13_add_certificate_verify(hs, is_first_run)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400505 case ssl_private_key_success:
Robert Sloan4d1ac502017-02-06 08:36:14 -0800506 hs->tls13_state = state_complete_second_flight;
507 return ssl_hs_ok;
David Benjaminc895d6b2016-08-11 13:26:41 -0400508
509 case ssl_private_key_retry:
Steven Valdeze7531f02016-12-14 13:29:57 -0500510 hs->tls13_state = state_complete_client_certificate_verify;
David Benjaminc895d6b2016-08-11 13:26:41 -0400511 return ssl_hs_private_key_operation;
512
513 case ssl_private_key_failure:
514 return ssl_hs_error;
515 }
516
517 assert(0);
518 return ssl_hs_error;
519}
520
Robert Sloan4d1ac502017-02-06 08:36:14 -0800521static enum ssl_hs_wait_t do_complete_second_flight(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500522 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800523
524 /* Send a Channel ID assertion if necessary. */
525 if (ssl->s3->tlsext_channel_id_valid) {
526 if (!ssl_do_channel_id_callback(ssl)) {
527 hs->tls13_state = state_complete_second_flight;
528 return ssl_hs_error;
529 }
530
531 if (ssl->tlsext_channel_id_private == NULL) {
532 return ssl_hs_channel_id_lookup;
533 }
534
535 CBB cbb, body;
536 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) ||
Robert Sloan5d625782017-02-13 09:55:39 -0800537 !tls1_write_channel_id(hs, &body) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -0800538 !ssl_add_message_cbb(ssl, &cbb)) {
539 CBB_cleanup(&cbb);
540 return ssl_hs_error;
541 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500542 }
543
Robert Sloan4d1ac502017-02-06 08:36:14 -0800544 /* Send a Finished message. */
545 if (!tls13_add_finished(hs)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500546 return ssl_hs_error;
547 }
548
Robert Sloan4d1ac502017-02-06 08:36:14 -0800549 /* Derive the final keys and enable them. */
Steven Valdez909b19f2016-11-21 15:35:44 -0500550 if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->server_traffic_secret_0,
551 hs->hash_len) ||
552 !tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_traffic_secret_0,
553 hs->hash_len) ||
David Benjamin1b249672016-12-06 18:25:50 -0500554 !tls13_derive_resumption_secret(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400555 return ssl_hs_error;
556 }
557
Steven Valdeze7531f02016-12-14 13:29:57 -0500558 hs->tls13_state = state_done;
David Benjaminc895d6b2016-08-11 13:26:41 -0400559 return ssl_hs_flush;
560}
561
David Benjamin1b249672016-12-06 18:25:50 -0500562enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500563 while (hs->tls13_state != state_done) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400564 enum ssl_hs_wait_t ret = ssl_hs_error;
Steven Valdeze7531f02016-12-14 13:29:57 -0500565 enum client_hs_state_t state = hs->tls13_state;
David Benjaminc895d6b2016-08-11 13:26:41 -0400566 switch (state) {
567 case state_process_hello_retry_request:
David Benjamin1b249672016-12-06 18:25:50 -0500568 ret = do_process_hello_retry_request(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400569 break;
570 case state_send_second_client_hello:
David Benjamin1b249672016-12-06 18:25:50 -0500571 ret = do_send_second_client_hello(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400572 break;
David Benjaminc895d6b2016-08-11 13:26:41 -0400573 case state_process_server_hello:
David Benjamin1b249672016-12-06 18:25:50 -0500574 ret = do_process_server_hello(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400575 break;
576 case state_process_encrypted_extensions:
David Benjamin1b249672016-12-06 18:25:50 -0500577 ret = do_process_encrypted_extensions(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400578 break;
579 case state_process_certificate_request:
David Benjamin1b249672016-12-06 18:25:50 -0500580 ret = do_process_certificate_request(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400581 break;
582 case state_process_server_certificate:
David Benjamin1b249672016-12-06 18:25:50 -0500583 ret = do_process_server_certificate(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400584 break;
585 case state_process_server_certificate_verify:
David Benjamin1b249672016-12-06 18:25:50 -0500586 ret = do_process_server_certificate_verify(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400587 break;
588 case state_process_server_finished:
David Benjamin1b249672016-12-06 18:25:50 -0500589 ret = do_process_server_finished(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400590 break;
David Benjaminc895d6b2016-08-11 13:26:41 -0400591 case state_send_client_certificate:
David Benjamin1b249672016-12-06 18:25:50 -0500592 ret = do_send_client_certificate(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400593 break;
594 case state_send_client_certificate_verify:
David Benjamin1b249672016-12-06 18:25:50 -0500595 ret = do_send_client_certificate_verify(hs, 1 /* first run */);
Steven Valdez909b19f2016-11-21 15:35:44 -0500596 break;
David Benjaminc895d6b2016-08-11 13:26:41 -0400597 case state_complete_client_certificate_verify:
David Benjamin1b249672016-12-06 18:25:50 -0500598 ret = do_send_client_certificate_verify(hs, 0 /* complete */);
Steven Valdez909b19f2016-11-21 15:35:44 -0500599 break;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800600 case state_complete_second_flight:
601 ret = do_complete_second_flight(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400602 break;
603 case state_done:
604 ret = ssl_hs_ok;
605 break;
606 }
607
608 if (ret != ssl_hs_ok) {
609 return ret;
610 }
611 }
612
613 return ssl_hs_ok;
614}
615
616int tls13_process_new_session_ticket(SSL *ssl) {
Robert Sloan69939df2017-01-09 10:53:07 -0800617 int ret = 0;
Robert Sloan5d625782017-02-13 09:55:39 -0800618 SSL_SESSION *session = SSL_SESSION_dup(ssl->s3->established_session,
619 SSL_SESSION_INCLUDE_NONAUTH);
David Benjaminc895d6b2016-08-11 13:26:41 -0400620 if (session == NULL) {
621 return 0;
622 }
623
Robert Sloan4d1ac502017-02-06 08:36:14 -0800624 ssl_session_rebase_time(ssl, session);
Steven Valdez909b19f2016-11-21 15:35:44 -0500625
Robert Sloan4d1ac502017-02-06 08:36:14 -0800626 uint32_t server_timeout;
Steven Valdez909b19f2016-11-21 15:35:44 -0500627 CBS cbs, ticket, extensions;
David Benjaminc895d6b2016-08-11 13:26:41 -0400628 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Robert Sloan4d1ac502017-02-06 08:36:14 -0800629 if (!CBS_get_u32(&cbs, &server_timeout) ||
Steven Valdez909b19f2016-11-21 15:35:44 -0500630 !CBS_get_u32(&cbs, &session->ticket_age_add) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400631 !CBS_get_u16_length_prefixed(&cbs, &ticket) ||
632 !CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen) ||
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400633 !CBS_get_u16_length_prefixed(&cbs, &extensions) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400634 CBS_len(&cbs) != 0) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400635 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
636 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan69939df2017-01-09 10:53:07 -0800637 goto err;
638 }
639
Robert Sloan4d1ac502017-02-06 08:36:14 -0800640 /* Cap the renewable lifetime by the server advertised value. This avoids
641 * wasting bandwidth on 0-RTT when we know the server will reject it.
642 *
643 * TODO(davidben): This dance where we're not sure if long or uint32_t is
644 * bigger is silly. session->timeout should not be a long to begin with.
645 * https://crbug.com/boringssl/155. */
646#if LONG_MAX < 0xffffffff
647 if (server_timeout > LONG_MAX) {
648 server_timeout = LONG_MAX;
649 }
650#endif
651 if (session->timeout > (long)server_timeout) {
652 session->timeout = (long)server_timeout;
653 }
654
Robert Sloan69939df2017-01-09 10:53:07 -0800655 /* Parse out the extensions. */
656 int have_early_data_info = 0;
657 CBS early_data_info;
658 const SSL_EXTENSION_TYPE ext_types[] = {
659 {TLSEXT_TYPE_ticket_early_data_info, &have_early_data_info,
660 &early_data_info},
661 };
662
Robert Sloan5d625782017-02-13 09:55:39 -0800663 uint8_t alert = SSL_AD_DECODE_ERROR;
Robert Sloan69939df2017-01-09 10:53:07 -0800664 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
665 OPENSSL_ARRAY_SIZE(ext_types),
666 1 /* ignore unknown */)) {
667 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
668 goto err;
669 }
670
Robert Sloan4d1ac502017-02-06 08:36:14 -0800671 if (have_early_data_info && ssl->ctx->enable_early_data) {
Robert Sloan69939df2017-01-09 10:53:07 -0800672 if (!CBS_get_u32(&early_data_info, &session->ticket_max_early_data) ||
673 CBS_len(&early_data_info) != 0) {
674 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
675 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
676 goto err;
677 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400678 }
679
Steven Valdez909b19f2016-11-21 15:35:44 -0500680 session->ticket_age_add_valid = 1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400681 session->not_resumable = 0;
682
Steven Valdez909b19f2016-11-21 15:35:44 -0500683 if (ssl->ctx->new_session_cb != NULL &&
David Benjaminc895d6b2016-08-11 13:26:41 -0400684 ssl->ctx->new_session_cb(ssl, session)) {
685 /* |new_session_cb|'s return value signals that it took ownership. */
Robert Sloan69939df2017-01-09 10:53:07 -0800686 session = NULL;
David Benjaminc895d6b2016-08-11 13:26:41 -0400687 }
688
Robert Sloan69939df2017-01-09 10:53:07 -0800689 ret = 1;
690
691err:
David Benjaminc895d6b2016-08-11 13:26:41 -0400692 SSL_SESSION_free(session);
Robert Sloan69939df2017-01-09 10:53:07 -0800693 return ret;
David Benjaminc895d6b2016-08-11 13:26:41 -0400694}
David Benjamin7c0d06c2016-08-11 13:26:41 -0400695
David Benjamin1b249672016-12-06 18:25:50 -0500696void ssl_clear_tls13_state(SSL_HANDSHAKE *hs) {
697 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
David Benjamin7c0d06c2016-08-11 13:26:41 -0400698
David Benjamin1b249672016-12-06 18:25:50 -0500699 OPENSSL_free(hs->key_share_bytes);
700 hs->key_share_bytes = NULL;
701 hs->key_share_bytes_len = 0;
David Benjamin7c0d06c2016-08-11 13:26:41 -0400702}