blob: 376e0ac3d0515006f3b1403d65d7d8140b04be4e [file] [log] [blame]
Steven Valdez143e8b32016-07-11 13:19:03 -04001/* Copyright (c) 2016, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include <openssl/ssl.h>
16
17#include <assert.h>
18#include <string.h>
19
20#include <openssl/bytestring.h>
21#include <openssl/digest.h>
22#include <openssl/err.h>
23#include <openssl/mem.h>
24#include <openssl/stack.h>
25#include <openssl/x509.h>
26
27#include "internal.h"
28
29
30enum client_hs_state_t {
Steven Valdez5440fe02016-07-18 12:40:30 -040031 state_process_hello_retry_request = 0,
32 state_send_second_client_hello,
33 state_flush_second_client_hello,
34 state_process_server_hello,
Steven Valdez143e8b32016-07-11 13:19:03 -040035 state_process_encrypted_extensions,
36 state_process_certificate_request,
37 state_process_server_certificate,
38 state_process_server_certificate_verify,
39 state_process_server_finished,
40 state_certificate_callback,
41 state_send_client_certificate,
42 state_send_client_certificate_verify,
43 state_complete_client_certificate_verify,
44 state_send_client_finished,
45 state_flush,
46 state_done,
47};
48
Steven Valdez5440fe02016-07-18 12:40:30 -040049static enum ssl_hs_wait_t do_process_hello_retry_request(SSL *ssl,
50 SSL_HANDSHAKE *hs) {
51 if (ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) {
52 hs->state = state_process_server_hello;
53 return ssl_hs_ok;
54 }
55
56 CBS cbs, extensions;
57 uint16_t server_wire_version, cipher_suite, group_id;
58 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
59 if (!CBS_get_u16(&cbs, &server_wire_version) ||
60 !CBS_get_u16(&cbs, &cipher_suite) ||
61 !CBS_get_u16(&cbs, &group_id) ||
62 /* We do not currently parse any HelloRetryRequest extensions. */
63 !CBS_get_u16_length_prefixed(&cbs, &extensions) ||
64 CBS_len(&cbs) != 0) {
65 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
66 return ssl_hs_error;
67 }
68
69 /* TODO(svaldez): Don't do early_data on HelloRetryRequest. */
70
71 const uint16_t *groups;
72 size_t groups_len;
73 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
74 int found = 0;
75 for (size_t i = 0; i < groups_len; i++) {
76 if (groups[i] == group_id) {
77 found = 1;
78 break;
79 }
80 }
81
82 if (!found) {
83 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
84 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
85 return ssl_hs_error;
86 }
87
88 for (size_t i = 0; i < ssl->s3->hs->groups_len; i++) {
89 /* Check that the HelloRetryRequest does not request a key share that was
90 * provided in the initial ClientHello.
91 *
92 * TODO(svaldez): Don't enforce this check when the HelloRetryRequest is due
93 * to a cookie. */
94 if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->groups[i]) == group_id) {
95 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
96 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
97 return ssl_hs_error;
98 }
99 }
100
101 ssl_handshake_clear_groups(ssl->s3->hs);
102 ssl->s3->hs->retry_group = group_id;
103
104 hs->state = state_send_second_client_hello;
105 return ssl_hs_ok;
106}
107
108static enum ssl_hs_wait_t do_send_second_client_hello(SSL *ssl,
109 SSL_HANDSHAKE *hs) {
110 CBB cbb, body;
111 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CLIENT_HELLO) ||
112 !ssl_add_client_hello_body(ssl, &body) ||
113 !ssl->method->finish_message(ssl, &cbb)) {
114 CBB_cleanup(&cbb);
115 return ssl_hs_error;
116 }
117
118 hs->state = state_flush_second_client_hello;
119 return ssl_hs_write_message;
120}
121
122static enum ssl_hs_wait_t do_flush_second_client_hello(SSL *ssl,
123 SSL_HANDSHAKE *hs) {
124 hs->state = state_process_server_hello;
125 return ssl_hs_flush_and_read_message;
126}
127
Steven Valdez143e8b32016-07-11 13:19:03 -0400128static enum ssl_hs_wait_t do_process_server_hello(SSL *ssl, SSL_HANDSHAKE *hs) {
129 if (!tls13_check_message_type(ssl, SSL3_MT_SERVER_HELLO)) {
130 return ssl_hs_error;
131 }
132
133 CBS cbs, server_random, extensions;
134 uint16_t server_wire_version;
135 uint16_t cipher_suite;
136 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
137 if (!CBS_get_u16(&cbs, &server_wire_version) ||
138 !CBS_get_bytes(&cbs, &server_random, SSL3_RANDOM_SIZE) ||
139 !CBS_get_u16(&cbs, &cipher_suite) ||
140 !CBS_get_u16_length_prefixed(&cbs, &extensions) ||
141 CBS_len(&cbs) != 0) {
142 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez5440fe02016-07-18 12:40:30 -0400143 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
144 return ssl_hs_error;
145 }
146
147 if (server_wire_version != ssl->version) {
148 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
149 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
Steven Valdez143e8b32016-07-11 13:19:03 -0400150 return ssl_hs_error;
151 }
152
153 /* Parse out the extensions. */
154 int have_key_share = 0;
155 CBS key_share;
156 while (CBS_len(&extensions) != 0) {
157 uint16_t type;
158 CBS extension;
159 if (!CBS_get_u16(&extensions, &type) ||
160 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
161 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
162 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
163 return ssl_hs_error;
164 }
165
166 switch (type) {
167 case TLSEXT_TYPE_key_share:
168 if (have_key_share) {
169 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_EXTENSION);
170 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
171 return ssl_hs_error;
172 }
173 key_share = extension;
174 have_key_share = 1;
175 break;
176 default:
177 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
178 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
179 return ssl_hs_error;
180 }
181 }
182
183 assert(ssl->s3->have_version);
184 memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
185
Steven Valdez87eab492016-06-27 16:34:59 -0400186 SSL_set_session(ssl, NULL);
Steven Valdez143e8b32016-07-11 13:19:03 -0400187 if (!ssl_get_new_session(ssl, 0)) {
188 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
189 return ssl_hs_error;
190 }
191
192 const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
193 if (cipher == NULL) {
194 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
195 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
196 return ssl_hs_error;
197 }
198
199 /* Check if the cipher is disabled. */
200 if ((cipher->algorithm_mkey & ssl->cert->mask_k) ||
201 (cipher->algorithm_auth & ssl->cert->mask_a) ||
202 SSL_CIPHER_get_min_version(cipher) > ssl3_protocol_version(ssl) ||
203 SSL_CIPHER_get_max_version(cipher) < ssl3_protocol_version(ssl) ||
204 !sk_SSL_CIPHER_find(ssl_get_ciphers_by_id(ssl), NULL, cipher)) {
205 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
206 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
207 return ssl_hs_error;
208 }
209
Steven Valdez87eab492016-06-27 16:34:59 -0400210 ssl->s3->new_session->cipher = cipher;
Steven Valdez143e8b32016-07-11 13:19:03 -0400211 ssl->s3->tmp.new_cipher = cipher;
212
213 /* The PRF hash is now known. Set up the key schedule. */
214 static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0};
215 size_t hash_len =
216 EVP_MD_size(ssl_get_handshake_digest(ssl_get_algorithm_prf(ssl)));
217 if (!tls13_init_key_schedule(ssl, kZeroes, hash_len)) {
218 return ssl_hs_error;
219 }
220
221 /* Resolve PSK and incorporate it into the secret. */
222 if (cipher->algorithm_auth == SSL_aPSK) {
223 /* TODO(davidben): Support PSK. */
224 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
225 return ssl_hs_error;
226 } else if (!tls13_advance_key_schedule(ssl, kZeroes, hash_len)) {
227 return ssl_hs_error;
228 }
229
230 /* Resolve ECDHE and incorporate it into the secret. */
231 if (cipher->algorithm_mkey == SSL_kECDHE) {
232 if (!have_key_share) {
233 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE);
234 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION);
235 return ssl_hs_error;
236 }
237
238 uint8_t *dhe_secret;
239 size_t dhe_secret_len;
240 uint8_t alert = SSL_AD_DECODE_ERROR;
Steven Valdez7259f2f2016-08-02 16:55:05 -0400241 if (!ssl_ext_key_share_parse_serverhello(ssl, &dhe_secret, &dhe_secret_len,
242 &alert, &key_share)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400243 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
244 return ssl_hs_error;
245 }
246
247 int ok = tls13_advance_key_schedule(ssl, dhe_secret, dhe_secret_len);
248 OPENSSL_free(dhe_secret);
249 if (!ok) {
250 return ssl_hs_error;
251 }
252 } else {
253 if (have_key_share) {
254 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
255 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
256 return ssl_hs_error;
257 }
258 if (!tls13_advance_key_schedule(ssl, kZeroes, hash_len)) {
259 return ssl_hs_error;
260 }
Steven Valdez5440fe02016-07-18 12:40:30 -0400261 }
262
263 /* If there was no HelloRetryRequest, the version negotiation logic has
264 * already hashed the message. */
265 if (ssl->s3->hs->retry_group != 0 &&
266 !ssl->method->hash_current_message(ssl)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400267 return ssl_hs_error;
268 }
269
270 if (!tls13_set_handshake_traffic(ssl)) {
271 return ssl_hs_error;
272 }
273
274 hs->state = state_process_encrypted_extensions;
275 return ssl_hs_read_message;
276}
277
278static enum ssl_hs_wait_t do_process_encrypted_extensions(SSL *ssl,
279 SSL_HANDSHAKE *hs) {
280 if (!tls13_check_message_type(ssl, SSL3_MT_ENCRYPTED_EXTENSIONS)) {
281 return ssl_hs_error;
282 }
283
284 CBS cbs;
285 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
286 if (!ssl_parse_serverhello_tlsext(ssl, &cbs)) {
287 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
288 return ssl_hs_error;
289 }
290 if (CBS_len(&cbs) != 0) {
291 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
292 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
293 return ssl_hs_error;
294 }
295
296 if (!ssl->method->hash_current_message(ssl)) {
297 return ssl_hs_error;
298 }
299
300 hs->state = state_process_certificate_request;
301 return ssl_hs_read_message;
302}
303
304static enum ssl_hs_wait_t do_process_certificate_request(SSL *ssl,
305 SSL_HANDSHAKE *hs) {
306 ssl->s3->tmp.cert_request = 0;
307
308 /* CertificateRequest may only be sent in certificate-based ciphers. */
309 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
310 hs->state = state_process_server_finished;
311 return ssl_hs_ok;
312 }
313
314 /* CertificateRequest is optional. */
315 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
316 hs->state = state_process_server_certificate;
317 return ssl_hs_ok;
318 }
319
320 CBS cbs, context, supported_signature_algorithms;
321 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
322 if (!CBS_get_u8_length_prefixed(&cbs, &context) ||
323 !CBS_stow(&context, &ssl->s3->hs->cert_context,
324 &ssl->s3->hs->cert_context_len) ||
325 !CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
David Benjamin48901652016-08-01 12:12:47 -0400326 CBS_len(&supported_signature_algorithms) == 0 ||
Steven Valdez143e8b32016-07-11 13:19:03 -0400327 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
328 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
329 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
330 return ssl_hs_error;
331 }
332
333 uint8_t alert;
334 STACK_OF(X509_NAME) *ca_sk = ssl_parse_client_CA_list(ssl, &alert, &cbs);
335 if (ca_sk == NULL) {
336 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
337 return ssl_hs_error;
338 }
339
340 /* Ignore extensions. */
341 CBS extensions;
342 if (!CBS_get_u16_length_prefixed(&cbs, &extensions) ||
343 CBS_len(&cbs) != 0) {
344 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
345 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
346 return ssl_hs_error;
347 }
348
349 ssl->s3->tmp.cert_request = 1;
350 sk_X509_NAME_pop_free(ssl->s3->tmp.ca_names, X509_NAME_free);
351 ssl->s3->tmp.ca_names = ca_sk;
352
353 if (!ssl->method->hash_current_message(ssl)) {
354 return ssl_hs_error;
355 }
356
357 hs->state = state_process_server_certificate;
358 return ssl_hs_read_message;
359}
360
361static enum ssl_hs_wait_t do_process_server_certificate(SSL *ssl,
362 SSL_HANDSHAKE *hs) {
363 if (!tls13_check_message_type(ssl, SSL3_MT_CERTIFICATE) ||
364 !tls13_process_certificate(ssl) ||
365 !ssl->method->hash_current_message(ssl)) {
366 return ssl_hs_error;
367 }
368
David Benjamin3ce43892016-08-01 19:41:34 -0400369 /* Check the certificate matches the cipher suite.
370 *
371 * TODO(davidben): Remove this check when switching to the new TLS 1.3 cipher
372 * suite negotiation. */
373 if (!ssl_check_leaf_certificate(ssl, ssl->s3->new_session->peer)) {
374 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
375 return ssl_hs_error;
376 }
377
Steven Valdez143e8b32016-07-11 13:19:03 -0400378 hs->state = state_process_server_certificate_verify;
379 return ssl_hs_read_message;
380}
381
382static enum ssl_hs_wait_t do_process_server_certificate_verify(
383 SSL *ssl, SSL_HANDSHAKE *hs) {
384 if (!tls13_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY) ||
385 !tls13_process_certificate_verify(ssl) ||
386 !ssl->method->hash_current_message(ssl)) {
387 return 0;
388 }
389
390 hs->state = state_process_server_finished;
391 return ssl_hs_read_message;
392}
393
394static enum ssl_hs_wait_t do_process_server_finished(SSL *ssl,
395 SSL_HANDSHAKE *hs) {
396 static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0};
397
398 if (!tls13_check_message_type(ssl, SSL3_MT_FINISHED) ||
399 !tls13_process_finished(ssl) ||
400 !ssl->method->hash_current_message(ssl) ||
401 /* Update the secret to the master secret and derive traffic keys. */
402 !tls13_advance_key_schedule(ssl, kZeroes, hs->hash_len) ||
403 !tls13_derive_traffic_secret_0(ssl)) {
404 return ssl_hs_error;
405 }
406
David Benjamin613fe3b2016-07-22 17:39:29 +0200407 ssl->method->received_flight(ssl);
Steven Valdez143e8b32016-07-11 13:19:03 -0400408 hs->state = state_certificate_callback;
409 return ssl_hs_ok;
410}
411
412static enum ssl_hs_wait_t do_certificate_callback(SSL *ssl, SSL_HANDSHAKE *hs) {
413 /* The peer didn't request a certificate. */
414 if (!ssl->s3->tmp.cert_request) {
415 hs->state = state_send_client_finished;
416 return ssl_hs_ok;
417 }
418
419 /* Call cert_cb to update the certificate. */
420 if (ssl->cert->cert_cb != NULL) {
421 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
422 if (rv == 0) {
423 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
424 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
425 return ssl_hs_error;
426 }
427 if (rv < 0) {
428 hs->state = state_certificate_callback;
429 return ssl_hs_x509_lookup;
430 }
431 }
432
433 hs->state = state_send_client_certificate;
434 return ssl_hs_ok;
435}
436
437static enum ssl_hs_wait_t do_send_client_certificate(SSL *ssl,
438 SSL_HANDSHAKE *hs) {
David Benjamin13f1ebe2016-07-20 10:11:04 +0200439 /* Call client_cert_cb to update the certificate. */
440 int should_retry;
441 if (!ssl_do_client_cert_cb(ssl, &should_retry)) {
442 if (should_retry) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400443 hs->state = state_send_client_certificate;
444 return ssl_hs_x509_lookup;
445 }
David Benjamin13f1ebe2016-07-20 10:11:04 +0200446 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400447 }
448
449 if (!tls13_prepare_certificate(ssl)) {
450 return ssl_hs_error;
451 }
452
453 hs->state = state_send_client_certificate_verify;
454 return ssl_hs_write_message;
455}
456
457static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL *ssl,
458 SSL_HANDSHAKE *hs,
459 int is_first_run) {
460 /* Don't send CertificateVerify if there is no certificate. */
461 if (!ssl_has_certificate(ssl)) {
462 hs->state = state_send_client_finished;
463 return ssl_hs_ok;
464 }
465
466 switch (tls13_prepare_certificate_verify(ssl, is_first_run)) {
467 case ssl_private_key_success:
468 hs->state = state_send_client_finished;
469 return ssl_hs_write_message;
470
471 case ssl_private_key_retry:
472 hs->state = state_complete_client_certificate_verify;
473 return ssl_hs_private_key_operation;
474
475 case ssl_private_key_failure:
476 return ssl_hs_error;
477 }
478
479 assert(0);
480 return ssl_hs_error;
481}
482
483static enum ssl_hs_wait_t do_send_client_finished(SSL *ssl, SSL_HANDSHAKE *hs) {
484 if (!tls13_prepare_finished(ssl)) {
485 return ssl_hs_error;
486 }
487
488 hs->state = state_flush;
489 return ssl_hs_write_message;
490}
491
492static enum ssl_hs_wait_t do_flush(SSL *ssl, SSL_HANDSHAKE *hs) {
493 if (!tls13_set_traffic_key(ssl, type_data, evp_aead_open,
494 hs->traffic_secret_0, hs->hash_len) ||
495 !tls13_set_traffic_key(ssl, type_data, evp_aead_seal,
496 hs->traffic_secret_0, hs->hash_len) ||
497 !tls13_finalize_keys(ssl)) {
498 return ssl_hs_error;
499 }
500
501 hs->state = state_done;
502 return ssl_hs_flush;
503}
504
505enum ssl_hs_wait_t tls13_client_handshake(SSL *ssl) {
506 SSL_HANDSHAKE *hs = ssl->s3->hs;
507
508 while (hs->state != state_done) {
509 enum ssl_hs_wait_t ret = ssl_hs_error;
510 enum client_hs_state_t state = hs->state;
511 switch (state) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400512 case state_process_hello_retry_request:
513 ret = do_process_hello_retry_request(ssl, hs);
514 break;
515 case state_send_second_client_hello:
516 ret = do_send_second_client_hello(ssl, hs);
517 break;
518 case state_flush_second_client_hello:
519 ret = do_flush_second_client_hello(ssl, hs);
520 break;
Steven Valdez143e8b32016-07-11 13:19:03 -0400521 case state_process_server_hello:
522 ret = do_process_server_hello(ssl, hs);
523 break;
524 case state_process_encrypted_extensions:
525 ret = do_process_encrypted_extensions(ssl, hs);
526 break;
527 case state_process_certificate_request:
528 ret = do_process_certificate_request(ssl, hs);
529 break;
530 case state_process_server_certificate:
531 ret = do_process_server_certificate(ssl, hs);
532 break;
533 case state_process_server_certificate_verify:
534 ret = do_process_server_certificate_verify(ssl, hs);
535 break;
536 case state_process_server_finished:
537 ret = do_process_server_finished(ssl, hs);
538 break;
539 case state_certificate_callback:
540 ret = do_certificate_callback(ssl, hs);
541 break;
542 case state_send_client_certificate:
543 ret = do_send_client_certificate(ssl, hs);
544 break;
545 case state_send_client_certificate_verify:
546 ret = do_send_client_certificate_verify(ssl, hs, 1 /* first run */);
547 break;
548 case state_complete_client_certificate_verify:
549 ret = do_send_client_certificate_verify(ssl, hs, 0 /* complete */);
550 break;
551 case state_send_client_finished:
552 ret = do_send_client_finished(ssl, hs);
553 break;
554 case state_flush:
555 ret = do_flush(ssl, hs);
556 break;
557 case state_done:
558 ret = ssl_hs_ok;
559 break;
560 }
561
562 if (ret != ssl_hs_ok) {
563 return ret;
564 }
565 }
566
567 return ssl_hs_ok;
568}
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400569
570int tls13_process_new_session_ticket(SSL *ssl) {
571 SSL_SESSION *session = SSL_SESSION_dup(ssl->s3->established_session,
572 0 /* don't include ticket */);
573 if (session == NULL) {
574 return 0;
575 }
576
577 CBS cbs, extensions, ticket;
578 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
579 if (!CBS_get_u32(&cbs, &session->ticket_lifetime_hint) ||
580 !CBS_get_u32(&cbs, &session->ticket_flags) ||
581 !CBS_get_u32(&cbs, &session->ticket_age_add) ||
582 !CBS_get_u16_length_prefixed(&cbs, &extensions) ||
583 !CBS_get_u16_length_prefixed(&cbs, &ticket) ||
584 !CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen) ||
585 CBS_len(&cbs) != 0) {
586 SSL_SESSION_free(session);
587 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
588 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
589 return 0;
590 }
591
592 session->ticket_age_add_valid = 1;
593 session->not_resumable = 0;
594
595 if (ssl->ctx->new_session_cb != NULL &&
596 ssl->ctx->new_session_cb(ssl, session)) {
597 /* |new_session_cb|'s return value signals that it took ownership. */
598 return 1;
599 }
600
601 SSL_SESSION_free(session);
602 return 1;
603}