blob: 74d3646e1560eb5b1666a84e20383524f3ffffeb [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
77 uint8_t alert;
78 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 Sloan4d1ac502017-02-06 08:36:14 -0800138 if (!ssl_hash_current_message(ssl)) {
139 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
Steven Valdez909b19f2016-11-21 15:35:44 -0500210 uint8_t alert;
211 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,
264 ssl->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. */
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400274 size_t hash_len =
David Benjaminc895d6b2016-08-11 13:26:41 -0400275 EVP_MD_size(ssl_get_handshake_digest(ssl_get_algorithm_prf(ssl)));
David Benjamin1b249672016-12-06 18:25:50 -0500276 if (!tls13_init_key_schedule(hs)) {
277 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400278 }
279
David Benjamin1b249672016-12-06 18:25:50 -0500280 /* Incorporate the PSK into the running secret. */
281 if (ssl->s3->session_reused) {
282 if (!tls13_advance_key_schedule(hs, ssl->s3->new_session->master_key,
283 ssl->s3->new_session->master_key_length)) {
284 return ssl_hs_error;
285 }
286 } else if (!tls13_advance_key_schedule(hs, kZeroes, hash_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400287 return ssl_hs_error;
288 }
289
Steven Valdeze7531f02016-12-14 13:29:57 -0500290 if (!have_key_share) {
291 /* We do not support psk_ke and thus always require a key share. */
292 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE);
293 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION);
294 return ssl_hs_error;
295 }
296
David Benjaminc895d6b2016-08-11 13:26:41 -0400297 /* Resolve ECDHE and incorporate it into the secret. */
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400298 uint8_t *dhe_secret;
299 size_t dhe_secret_len;
David Benjamin1b249672016-12-06 18:25:50 -0500300 if (!ssl_ext_key_share_parse_serverhello(hs, &dhe_secret, &dhe_secret_len,
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400301 &alert, &key_share)) {
302 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
303 return ssl_hs_error;
304 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400305
David Benjamin1b249672016-12-06 18:25:50 -0500306 if (!tls13_advance_key_schedule(hs, dhe_secret, dhe_secret_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400307 OPENSSL_free(dhe_secret);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400308 return ssl_hs_error;
309 }
310 OPENSSL_free(dhe_secret);
311
Robert Sloan69939df2017-01-09 10:53:07 -0800312 /* Negotiate short record headers. */
313 if (have_short_header) {
314 if (CBS_len(&short_header) != 0) {
315 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
316 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
317 return ssl_hs_error;
318 }
319
320 if (!ssl->ctx->short_header_enabled) {
321 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
322 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
323 return ssl_hs_error;
324 }
325
326 ssl->s3->short_header = 1;
327 }
328
Robert Sloan4d1ac502017-02-06 08:36:14 -0800329 if (!ssl_hash_current_message(ssl) ||
330 !tls13_derive_handshake_secrets(hs) ||
Robert Sloan69939df2017-01-09 10:53:07 -0800331 !tls13_set_traffic_key(ssl, evp_aead_open, hs->server_handshake_secret,
332 hs->hash_len) ||
333 !tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_handshake_secret,
334 hs->hash_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400335 return ssl_hs_error;
336 }
337
Steven Valdeze7531f02016-12-14 13:29:57 -0500338 hs->tls13_state = state_process_encrypted_extensions;
David Benjaminc895d6b2016-08-11 13:26:41 -0400339 return ssl_hs_read_message;
340}
341
David Benjamin1b249672016-12-06 18:25:50 -0500342static enum ssl_hs_wait_t do_process_encrypted_extensions(SSL_HANDSHAKE *hs) {
343 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800344 if (!ssl_check_message_type(ssl, SSL3_MT_ENCRYPTED_EXTENSIONS)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400345 return ssl_hs_error;
346 }
347
348 CBS cbs;
349 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
David Benjamin1b249672016-12-06 18:25:50 -0500350 if (!ssl_parse_serverhello_tlsext(hs, &cbs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400351 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
352 return ssl_hs_error;
353 }
354 if (CBS_len(&cbs) != 0) {
355 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
356 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
357 return ssl_hs_error;
358 }
359
Steven Valdez909b19f2016-11-21 15:35:44 -0500360 if (!ssl_hash_current_message(ssl)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400361 return ssl_hs_error;
362 }
363
Steven Valdeze7531f02016-12-14 13:29:57 -0500364 hs->tls13_state = state_process_certificate_request;
David Benjaminc895d6b2016-08-11 13:26:41 -0400365 return ssl_hs_read_message;
366}
367
David Benjamin1b249672016-12-06 18:25:50 -0500368static enum ssl_hs_wait_t do_process_certificate_request(SSL_HANDSHAKE *hs) {
369 SSL *const ssl = hs->ssl;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400370 /* CertificateRequest may only be sent in non-resumption handshakes. */
371 if (ssl->s3->session_reused) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500372 hs->tls13_state = state_process_server_finished;
David Benjaminc895d6b2016-08-11 13:26:41 -0400373 return ssl_hs_ok;
374 }
375
376 /* CertificateRequest is optional. */
377 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500378 hs->tls13_state = state_process_server_certificate;
David Benjaminc895d6b2016-08-11 13:26:41 -0400379 return ssl_hs_ok;
380 }
381
382 CBS cbs, context, supported_signature_algorithms;
383 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
384 if (!CBS_get_u8_length_prefixed(&cbs, &context) ||
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400385 /* The request context is always empty during the handshake. */
386 CBS_len(&context) != 0 ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400387 !CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
388 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjamin1b249672016-12-06 18:25:50 -0500389 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400390 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
391 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
392 return ssl_hs_error;
393 }
394
395 uint8_t alert;
396 STACK_OF(X509_NAME) *ca_sk = ssl_parse_client_CA_list(ssl, &alert, &cbs);
397 if (ca_sk == NULL) {
398 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
399 return ssl_hs_error;
400 }
401
402 /* Ignore extensions. */
403 CBS extensions;
404 if (!CBS_get_u16_length_prefixed(&cbs, &extensions) ||
405 CBS_len(&cbs) != 0) {
406 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin7c0d06c2016-08-11 13:26:41 -0400407 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
David Benjaminc895d6b2016-08-11 13:26:41 -0400408 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
409 return ssl_hs_error;
410 }
411
David Benjamin1b249672016-12-06 18:25:50 -0500412 hs->cert_request = 1;
413 sk_X509_NAME_pop_free(hs->ca_names, X509_NAME_free);
414 hs->ca_names = ca_sk;
David Benjaminc895d6b2016-08-11 13:26:41 -0400415
Steven Valdez909b19f2016-11-21 15:35:44 -0500416 if (!ssl_hash_current_message(ssl)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400417 return ssl_hs_error;
418 }
419
Steven Valdeze7531f02016-12-14 13:29:57 -0500420 hs->tls13_state = state_process_server_certificate;
David Benjaminc895d6b2016-08-11 13:26:41 -0400421 return ssl_hs_read_message;
422}
423
David Benjamin1b249672016-12-06 18:25:50 -0500424static enum ssl_hs_wait_t do_process_server_certificate(SSL_HANDSHAKE *hs) {
425 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800426 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE) ||
Steven Valdeze7531f02016-12-14 13:29:57 -0500427 !tls13_process_certificate(hs, 0 /* certificate required */) ||
Steven Valdez909b19f2016-11-21 15:35:44 -0500428 !ssl_hash_current_message(ssl)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400429 return ssl_hs_error;
430 }
431
Steven Valdeze7531f02016-12-14 13:29:57 -0500432 hs->tls13_state = state_process_server_certificate_verify;
David Benjaminc895d6b2016-08-11 13:26:41 -0400433 return ssl_hs_read_message;
434}
435
436static enum ssl_hs_wait_t do_process_server_certificate_verify(
David Benjamin1b249672016-12-06 18:25:50 -0500437 SSL_HANDSHAKE *hs) {
438 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800439 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY) ||
Steven Valdeze7531f02016-12-14 13:29:57 -0500440 !tls13_process_certificate_verify(hs) ||
Steven Valdez909b19f2016-11-21 15:35:44 -0500441 !ssl_hash_current_message(ssl)) {
442 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400443 }
444
Steven Valdeze7531f02016-12-14 13:29:57 -0500445 hs->tls13_state = state_process_server_finished;
David Benjaminc895d6b2016-08-11 13:26:41 -0400446 return ssl_hs_read_message;
447}
448
David Benjamin1b249672016-12-06 18:25:50 -0500449static enum ssl_hs_wait_t do_process_server_finished(SSL_HANDSHAKE *hs) {
450 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800451 if (!ssl_check_message_type(ssl, SSL3_MT_FINISHED) ||
David Benjamin1b249672016-12-06 18:25:50 -0500452 !tls13_process_finished(hs) ||
Steven Valdez909b19f2016-11-21 15:35:44 -0500453 !ssl_hash_current_message(ssl) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400454 /* Update the secret to the master secret and derive traffic keys. */
David Benjamin1b249672016-12-06 18:25:50 -0500455 !tls13_advance_key_schedule(hs, kZeroes, hs->hash_len) ||
456 !tls13_derive_application_secrets(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400457 return ssl_hs_error;
458 }
459
460 ssl->method->received_flight(ssl);
Steven Valdeze7531f02016-12-14 13:29:57 -0500461 hs->tls13_state = state_send_client_certificate;
David Benjaminc895d6b2016-08-11 13:26:41 -0400462 return ssl_hs_ok;
463}
464
Steven Valdeze7531f02016-12-14 13:29:57 -0500465static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500466 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -0400467 /* The peer didn't request a certificate. */
David Benjamin1b249672016-12-06 18:25:50 -0500468 if (!hs->cert_request) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800469 hs->tls13_state = state_complete_second_flight;
David Benjaminc895d6b2016-08-11 13:26:41 -0400470 return ssl_hs_ok;
471 }
472
473 /* Call cert_cb to update the certificate. */
474 if (ssl->cert->cert_cb != NULL) {
475 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
476 if (rv == 0) {
477 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
478 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
479 return ssl_hs_error;
480 }
481 if (rv < 0) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500482 hs->tls13_state = state_send_client_certificate;
David Benjaminc895d6b2016-08-11 13:26:41 -0400483 return ssl_hs_x509_lookup;
484 }
485 }
486
Robert Sloan69939df2017-01-09 10:53:07 -0800487 if (!ssl_auto_chain_if_needed(ssl) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -0800488 !tls13_add_certificate(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400489 return ssl_hs_error;
490 }
491
Steven Valdeze7531f02016-12-14 13:29:57 -0500492 hs->tls13_state = state_send_client_certificate_verify;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800493 return ssl_hs_ok;
David Benjaminc895d6b2016-08-11 13:26:41 -0400494}
495
David Benjamin1b249672016-12-06 18:25:50 -0500496static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs,
David Benjaminc895d6b2016-08-11 13:26:41 -0400497 int is_first_run) {
David Benjamin1b249672016-12-06 18:25:50 -0500498 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -0400499 /* Don't send CertificateVerify if there is no certificate. */
500 if (!ssl_has_certificate(ssl)) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800501 hs->tls13_state = state_complete_second_flight;
David Benjaminc895d6b2016-08-11 13:26:41 -0400502 return ssl_hs_ok;
503 }
504
Robert Sloan4d1ac502017-02-06 08:36:14 -0800505 switch (tls13_add_certificate_verify(hs, is_first_run)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400506 case ssl_private_key_success:
Robert Sloan4d1ac502017-02-06 08:36:14 -0800507 hs->tls13_state = state_complete_second_flight;
508 return ssl_hs_ok;
David Benjaminc895d6b2016-08-11 13:26:41 -0400509
510 case ssl_private_key_retry:
Steven Valdeze7531f02016-12-14 13:29:57 -0500511 hs->tls13_state = state_complete_client_certificate_verify;
David Benjaminc895d6b2016-08-11 13:26:41 -0400512 return ssl_hs_private_key_operation;
513
514 case ssl_private_key_failure:
515 return ssl_hs_error;
516 }
517
518 assert(0);
519 return ssl_hs_error;
520}
521
Robert Sloan4d1ac502017-02-06 08:36:14 -0800522static enum ssl_hs_wait_t do_complete_second_flight(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500523 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800524
525 /* Send a Channel ID assertion if necessary. */
526 if (ssl->s3->tlsext_channel_id_valid) {
527 if (!ssl_do_channel_id_callback(ssl)) {
528 hs->tls13_state = state_complete_second_flight;
529 return ssl_hs_error;
530 }
531
532 if (ssl->tlsext_channel_id_private == NULL) {
533 return ssl_hs_channel_id_lookup;
534 }
535
536 CBB cbb, body;
537 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) ||
538 !tls1_write_channel_id(ssl, &body) ||
539 !ssl_add_message_cbb(ssl, &cbb)) {
540 CBB_cleanup(&cbb);
541 return ssl_hs_error;
542 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500543 }
544
Robert Sloan4d1ac502017-02-06 08:36:14 -0800545 /* Send a Finished message. */
546 if (!tls13_add_finished(hs)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500547 return ssl_hs_error;
548 }
549
Robert Sloan4d1ac502017-02-06 08:36:14 -0800550 /* Derive the final keys and enable them. */
Steven Valdez909b19f2016-11-21 15:35:44 -0500551 if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->server_traffic_secret_0,
552 hs->hash_len) ||
553 !tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_traffic_secret_0,
554 hs->hash_len) ||
David Benjamin1b249672016-12-06 18:25:50 -0500555 !tls13_derive_resumption_secret(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400556 return ssl_hs_error;
557 }
558
Steven Valdeze7531f02016-12-14 13:29:57 -0500559 hs->tls13_state = state_done;
David Benjaminc895d6b2016-08-11 13:26:41 -0400560 return ssl_hs_flush;
561}
562
David Benjamin1b249672016-12-06 18:25:50 -0500563enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500564 while (hs->tls13_state != state_done) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400565 enum ssl_hs_wait_t ret = ssl_hs_error;
Steven Valdeze7531f02016-12-14 13:29:57 -0500566 enum client_hs_state_t state = hs->tls13_state;
David Benjaminc895d6b2016-08-11 13:26:41 -0400567 switch (state) {
568 case state_process_hello_retry_request:
David Benjamin1b249672016-12-06 18:25:50 -0500569 ret = do_process_hello_retry_request(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400570 break;
571 case state_send_second_client_hello:
David Benjamin1b249672016-12-06 18:25:50 -0500572 ret = do_send_second_client_hello(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400573 break;
David Benjaminc895d6b2016-08-11 13:26:41 -0400574 case state_process_server_hello:
David Benjamin1b249672016-12-06 18:25:50 -0500575 ret = do_process_server_hello(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400576 break;
577 case state_process_encrypted_extensions:
David Benjamin1b249672016-12-06 18:25:50 -0500578 ret = do_process_encrypted_extensions(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400579 break;
580 case state_process_certificate_request:
David Benjamin1b249672016-12-06 18:25:50 -0500581 ret = do_process_certificate_request(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400582 break;
583 case state_process_server_certificate:
David Benjamin1b249672016-12-06 18:25:50 -0500584 ret = do_process_server_certificate(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400585 break;
586 case state_process_server_certificate_verify:
David Benjamin1b249672016-12-06 18:25:50 -0500587 ret = do_process_server_certificate_verify(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400588 break;
589 case state_process_server_finished:
David Benjamin1b249672016-12-06 18:25:50 -0500590 ret = do_process_server_finished(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400591 break;
David Benjaminc895d6b2016-08-11 13:26:41 -0400592 case state_send_client_certificate:
David Benjamin1b249672016-12-06 18:25:50 -0500593 ret = do_send_client_certificate(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400594 break;
595 case state_send_client_certificate_verify:
David Benjamin1b249672016-12-06 18:25:50 -0500596 ret = do_send_client_certificate_verify(hs, 1 /* first run */);
Steven Valdez909b19f2016-11-21 15:35:44 -0500597 break;
David Benjaminc895d6b2016-08-11 13:26:41 -0400598 case state_complete_client_certificate_verify:
David Benjamin1b249672016-12-06 18:25:50 -0500599 ret = do_send_client_certificate_verify(hs, 0 /* complete */);
Steven Valdez909b19f2016-11-21 15:35:44 -0500600 break;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800601 case state_complete_second_flight:
602 ret = do_complete_second_flight(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400603 break;
604 case state_done:
605 ret = ssl_hs_ok;
606 break;
607 }
608
609 if (ret != ssl_hs_ok) {
610 return ret;
611 }
612 }
613
614 return ssl_hs_ok;
615}
616
617int tls13_process_new_session_ticket(SSL *ssl) {
Robert Sloan69939df2017-01-09 10:53:07 -0800618 int ret = 0;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400619 SSL_SESSION *session =
620 SSL_SESSION_dup(ssl->s3->established_session,
621 SSL_SESSION_INCLUDE_NONAUTH);
David Benjaminc895d6b2016-08-11 13:26:41 -0400622 if (session == NULL) {
623 return 0;
624 }
625
Robert Sloan4d1ac502017-02-06 08:36:14 -0800626 ssl_session_rebase_time(ssl, session);
Steven Valdez909b19f2016-11-21 15:35:44 -0500627
Robert Sloan4d1ac502017-02-06 08:36:14 -0800628 uint32_t server_timeout;
Steven Valdez909b19f2016-11-21 15:35:44 -0500629 CBS cbs, ticket, extensions;
David Benjaminc895d6b2016-08-11 13:26:41 -0400630 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Robert Sloan4d1ac502017-02-06 08:36:14 -0800631 if (!CBS_get_u32(&cbs, &server_timeout) ||
Steven Valdez909b19f2016-11-21 15:35:44 -0500632 !CBS_get_u32(&cbs, &session->ticket_age_add) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400633 !CBS_get_u16_length_prefixed(&cbs, &ticket) ||
634 !CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen) ||
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400635 !CBS_get_u16_length_prefixed(&cbs, &extensions) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400636 CBS_len(&cbs) != 0) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400637 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
638 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan69939df2017-01-09 10:53:07 -0800639 goto err;
640 }
641
Robert Sloan4d1ac502017-02-06 08:36:14 -0800642 /* Cap the renewable lifetime by the server advertised value. This avoids
643 * wasting bandwidth on 0-RTT when we know the server will reject it.
644 *
645 * TODO(davidben): This dance where we're not sure if long or uint32_t is
646 * bigger is silly. session->timeout should not be a long to begin with.
647 * https://crbug.com/boringssl/155. */
648#if LONG_MAX < 0xffffffff
649 if (server_timeout > LONG_MAX) {
650 server_timeout = LONG_MAX;
651 }
652#endif
653 if (session->timeout > (long)server_timeout) {
654 session->timeout = (long)server_timeout;
655 }
656
Robert Sloan69939df2017-01-09 10:53:07 -0800657 /* Parse out the extensions. */
658 int have_early_data_info = 0;
659 CBS early_data_info;
660 const SSL_EXTENSION_TYPE ext_types[] = {
661 {TLSEXT_TYPE_ticket_early_data_info, &have_early_data_info,
662 &early_data_info},
663 };
664
665 uint8_t alert;
666 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
667 OPENSSL_ARRAY_SIZE(ext_types),
668 1 /* ignore unknown */)) {
669 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
670 goto err;
671 }
672
Robert Sloan4d1ac502017-02-06 08:36:14 -0800673 if (have_early_data_info && ssl->ctx->enable_early_data) {
Robert Sloan69939df2017-01-09 10:53:07 -0800674 if (!CBS_get_u32(&early_data_info, &session->ticket_max_early_data) ||
675 CBS_len(&early_data_info) != 0) {
676 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
677 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
678 goto err;
679 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400680 }
681
Steven Valdez909b19f2016-11-21 15:35:44 -0500682 session->ticket_age_add_valid = 1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400683 session->not_resumable = 0;
684
Steven Valdez909b19f2016-11-21 15:35:44 -0500685 if (ssl->ctx->new_session_cb != NULL &&
David Benjaminc895d6b2016-08-11 13:26:41 -0400686 ssl->ctx->new_session_cb(ssl, session)) {
687 /* |new_session_cb|'s return value signals that it took ownership. */
Robert Sloan69939df2017-01-09 10:53:07 -0800688 session = NULL;
David Benjaminc895d6b2016-08-11 13:26:41 -0400689 }
690
Robert Sloan69939df2017-01-09 10:53:07 -0800691 ret = 1;
692
693err:
David Benjaminc895d6b2016-08-11 13:26:41 -0400694 SSL_SESSION_free(session);
Robert Sloan69939df2017-01-09 10:53:07 -0800695 return ret;
David Benjaminc895d6b2016-08-11 13:26:41 -0400696}
David Benjamin7c0d06c2016-08-11 13:26:41 -0400697
David Benjamin1b249672016-12-06 18:25:50 -0500698void ssl_clear_tls13_state(SSL_HANDSHAKE *hs) {
699 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
David Benjamin7c0d06c2016-08-11 13:26:41 -0400700
David Benjamin1b249672016-12-06 18:25:50 -0500701 OPENSSL_free(hs->key_share_bytes);
702 hs->key_share_bytes = NULL;
703 hs->key_share_bytes_len = 0;
David Benjamin7c0d06c2016-08-11 13:26:41 -0400704}