2016-07-11 18:19:03 +01:00
|
|
|
/* Copyright (c) 2016, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
2017-01-28 19:00:32 +00:00
|
|
|
#include <limits.h>
|
2016-07-11 18:19:03 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <openssl/bytestring.h>
|
|
|
|
#include <openssl/digest.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/mem.h>
|
|
|
|
#include <openssl/stack.h>
|
|
|
|
|
2016-11-13 01:32:10 +00:00
|
|
|
#include "../crypto/internal.h"
|
2016-07-11 18:19:03 +01:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
|
|
|
|
enum client_hs_state_t {
|
2016-07-18 17:40:30 +01:00
|
|
|
state_process_hello_retry_request = 0,
|
|
|
|
state_send_second_client_hello,
|
|
|
|
state_process_server_hello,
|
2016-07-11 18:19:03 +01:00
|
|
|
state_process_encrypted_extensions,
|
|
|
|
state_process_certificate_request,
|
|
|
|
state_process_server_certificate,
|
|
|
|
state_process_server_certificate_verify,
|
|
|
|
state_process_server_finished,
|
2017-01-11 16:34:52 +00:00
|
|
|
state_send_end_of_early_data,
|
2016-07-11 18:19:03 +01:00
|
|
|
state_send_client_certificate,
|
|
|
|
state_send_client_certificate_verify,
|
|
|
|
state_complete_client_certificate_verify,
|
2017-01-13 00:44:57 +00:00
|
|
|
state_complete_second_flight,
|
2016-07-11 18:19:03 +01:00
|
|
|
state_done,
|
|
|
|
};
|
|
|
|
|
2016-11-30 16:24:40 +00:00
|
|
|
static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0};
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static enum ssl_hs_wait_t do_process_hello_retry_request(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2016-07-18 17:40:30 +01:00
|
|
|
if (ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) {
|
2016-12-11 18:30:41 +00:00
|
|
|
hs->tls13_state = state_process_server_hello;
|
2016-07-18 17:40:30 +01:00
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
CBS cbs, extensions;
|
2016-10-08 02:10:38 +01:00
|
|
|
uint16_t server_wire_version;
|
2016-07-18 17:40:30 +01:00
|
|
|
CBS_init(&cbs, ssl->init_msg, ssl->init_num);
|
|
|
|
if (!CBS_get_u16(&cbs, &server_wire_version) ||
|
|
|
|
!CBS_get_u16_length_prefixed(&cbs, &extensions) ||
|
2016-10-08 02:10:38 +01:00
|
|
|
/* HelloRetryRequest may not be empty. */
|
|
|
|
CBS_len(&extensions) == 0 ||
|
2016-07-18 17:40:30 +01:00
|
|
|
CBS_len(&cbs) != 0) {
|
2016-10-08 02:10:38 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2016-07-18 17:40:30 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2016-11-13 01:32:10 +00:00
|
|
|
int have_cookie, have_key_share;
|
|
|
|
CBS cookie, key_share;
|
|
|
|
const SSL_EXTENSION_TYPE ext_types[] = {
|
|
|
|
{TLSEXT_TYPE_key_share, &have_key_share, &key_share},
|
|
|
|
{TLSEXT_TYPE_cookie, &have_cookie, &cookie},
|
|
|
|
};
|
|
|
|
|
2017-02-08 21:33:15 +00:00
|
|
|
uint8_t alert = SSL_AD_DECODE_ERROR;
|
2016-11-13 01:32:10 +00:00
|
|
|
if (!ssl_parse_extensions(&extensions, &alert, ext_types,
|
2016-12-07 20:29:45 +00:00
|
|
|
OPENSSL_ARRAY_SIZE(ext_types),
|
|
|
|
0 /* reject unknown */)) {
|
2016-11-13 01:32:10 +00:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_cookie) {
|
|
|
|
CBS cookie_value;
|
|
|
|
if (!CBS_get_u16_length_prefixed(&cookie, &cookie_value) ||
|
|
|
|
CBS_len(&cookie_value) == 0 ||
|
|
|
|
CBS_len(&cookie) != 0) {
|
2016-10-08 02:10:38 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
return ssl_hs_error;
|
2016-07-18 17:40:30 +01:00
|
|
|
}
|
|
|
|
|
2016-11-13 01:32:10 +00:00
|
|
|
if (!CBS_stow(&cookie_value, &hs->cookie, &hs->cookie_len)) {
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
}
|
2016-10-08 02:10:38 +01:00
|
|
|
|
2016-11-13 01:32:10 +00:00
|
|
|
if (have_key_share) {
|
|
|
|
uint16_t group_id;
|
|
|
|
if (!CBS_get_u16(&key_share, &group_id) || CBS_len(&key_share) != 0) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
2016-10-08 02:10:38 +01:00
|
|
|
|
2016-11-13 01:32:10 +00:00
|
|
|
/* The group must be supported. */
|
|
|
|
const uint16_t *groups;
|
|
|
|
size_t groups_len;
|
|
|
|
tls1_get_grouplist(ssl, &groups, &groups_len);
|
|
|
|
int found = 0;
|
|
|
|
for (size_t i = 0; i < groups_len; i++) {
|
|
|
|
if (groups[i] == group_id) {
|
|
|
|
found = 1;
|
2016-10-08 02:10:38 +01:00
|
|
|
break;
|
|
|
|
}
|
2016-11-13 01:32:10 +00:00
|
|
|
}
|
2016-10-08 02:10:38 +01:00
|
|
|
|
2016-11-13 01:32:10 +00:00
|
|
|
if (!found) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
2016-10-08 02:10:38 +01:00
|
|
|
|
2016-11-13 01:32:10 +00:00
|
|
|
/* Check that the HelloRetryRequest does not request the key share that
|
|
|
|
* was provided in the initial ClientHello. */
|
|
|
|
if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) == group_id) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
|
|
|
|
return ssl_hs_error;
|
2016-10-08 02:10:38 +01:00
|
|
|
}
|
2016-11-13 01:32:10 +00:00
|
|
|
|
|
|
|
SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
|
|
|
|
hs->retry_group = group_id;
|
2016-10-08 02:10:38 +01:00
|
|
|
}
|
|
|
|
|
2017-01-12 18:17:07 +00:00
|
|
|
if (!ssl_hash_current_message(hs)) {
|
2017-01-21 19:49:39 +00:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2016-10-08 02:10:38 +01:00
|
|
|
hs->received_hello_retry_request = 1;
|
2016-12-11 18:30:41 +00:00
|
|
|
hs->tls13_state = state_send_second_client_hello;
|
2016-07-18 17:40:30 +01:00
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static enum ssl_hs_wait_t do_send_second_client_hello(SSL_HANDSHAKE *hs) {
|
2017-01-11 16:34:52 +00:00
|
|
|
SSL *const ssl = hs->ssl;
|
|
|
|
/* TODO(svaldez): Ensure that we set can_early_write to false since 0-RTT is
|
|
|
|
* rejected if we receive a HelloRetryRequest. */
|
|
|
|
if (!ssl->method->set_write_state(ssl, NULL) ||
|
|
|
|
!ssl_write_client_hello(hs)) {
|
2016-07-18 17:40:30 +01:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2016-12-11 18:30:41 +00:00
|
|
|
hs->tls13_state = state_process_server_hello;
|
2016-07-18 17:40:30 +01:00
|
|
|
return ssl_hs_flush_and_read_message;
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static enum ssl_hs_wait_t do_process_server_hello(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2017-01-21 19:13:39 +00:00
|
|
|
if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO)) {
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
CBS cbs, server_random, extensions;
|
|
|
|
uint16_t server_wire_version;
|
|
|
|
uint16_t cipher_suite;
|
|
|
|
CBS_init(&cbs, ssl->init_msg, ssl->init_num);
|
|
|
|
if (!CBS_get_u16(&cbs, &server_wire_version) ||
|
|
|
|
!CBS_get_bytes(&cbs, &server_random, SSL3_RANDOM_SIZE) ||
|
|
|
|
!CBS_get_u16(&cbs, &cipher_suite) ||
|
|
|
|
!CBS_get_u16_length_prefixed(&cbs, &extensions) ||
|
|
|
|
CBS_len(&cbs) != 0) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2016-07-18 17:40:30 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (server_wire_version != ssl->version) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2016-09-06 19:13:43 +01:00
|
|
|
assert(ssl->s3->have_version);
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
|
|
|
|
SSL3_RANDOM_SIZE);
|
2016-09-06 19:13:43 +01:00
|
|
|
|
|
|
|
const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
|
|
|
|
if (cipher == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2016-10-31 23:20:42 +00:00
|
|
|
/* Check if the cipher is a TLS 1.3 cipher. */
|
|
|
|
if (SSL_CIPHER_get_min_version(cipher) > ssl3_protocol_version(ssl) ||
|
|
|
|
SSL_CIPHER_get_max_version(cipher) < ssl3_protocol_version(ssl)) {
|
2016-09-06 19:13:43 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2016-07-11 18:19:03 +01:00
|
|
|
/* Parse out the extensions. */
|
2017-03-02 21:05:03 +00:00
|
|
|
int have_key_share = 0, have_pre_shared_key = 0;
|
|
|
|
CBS key_share, pre_shared_key;
|
2016-11-13 01:32:10 +00:00
|
|
|
const SSL_EXTENSION_TYPE ext_types[] = {
|
|
|
|
{TLSEXT_TYPE_key_share, &have_key_share, &key_share},
|
|
|
|
{TLSEXT_TYPE_pre_shared_key, &have_pre_shared_key, &pre_shared_key},
|
|
|
|
};
|
|
|
|
|
2017-02-08 21:33:15 +00:00
|
|
|
uint8_t alert = SSL_AD_DECODE_ERROR;
|
2016-11-13 01:32:10 +00:00
|
|
|
if (!ssl_parse_extensions(&extensions, &alert, ext_types,
|
2016-12-07 20:29:45 +00:00
|
|
|
OPENSSL_ARRAY_SIZE(ext_types),
|
|
|
|
0 /* reject unknown */)) {
|
2016-11-13 01:32:10 +00:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
|
|
|
|
return ssl_hs_error;
|
2016-07-11 18:19:03 +01:00
|
|
|
}
|
|
|
|
|
2016-11-13 01:32:10 +00:00
|
|
|
alert = SSL_AD_DECODE_ERROR;
|
2016-07-29 19:32:55 +01:00
|
|
|
if (have_pre_shared_key) {
|
|
|
|
if (ssl->session == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2016-11-17 08:11:16 +00:00
|
|
|
if (!ssl_ext_pre_shared_key_parse_serverhello(hs, &alert,
|
2016-07-29 19:32:55 +01:00
|
|
|
&pre_shared_key)) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl->session->ssl_version != ssl->version) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2016-11-16 07:25:58 +00:00
|
|
|
if (ssl->session->cipher->algorithm_prf != cipher->algorithm_prf) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_PRF_HASH_MISMATCH);
|
2016-09-06 19:13:43 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2016-07-29 19:32:55 +01:00
|
|
|
if (!ssl_session_is_context_valid(ssl, ssl->session)) {
|
|
|
|
/* This is actually a client application bug. */
|
|
|
|
OPENSSL_PUT_ERROR(SSL,
|
|
|
|
SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl->s3->session_reused = 1;
|
|
|
|
/* Only authentication information carries over in TLS 1.3. */
|
2017-02-10 01:01:26 +00:00
|
|
|
hs->new_session = SSL_SESSION_dup(ssl->session, SSL_SESSION_DUP_AUTH_ONLY);
|
|
|
|
if (hs->new_session == NULL) {
|
2016-07-29 19:32:55 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
2017-01-28 19:00:32 +00:00
|
|
|
|
|
|
|
/* Resumption incorporates fresh key material, so refresh the timeout. */
|
2017-02-10 01:01:26 +00:00
|
|
|
ssl_session_renew_timeout(ssl, hs->new_session,
|
2017-03-10 21:08:36 +00:00
|
|
|
ssl->session_ctx->session_psk_dhe_timeout);
|
2016-11-17 08:20:47 +00:00
|
|
|
} else if (!ssl_get_new_session(hs, 0)) {
|
2016-09-06 19:13:43 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2017-02-10 01:01:26 +00:00
|
|
|
hs->new_session->cipher = cipher;
|
|
|
|
hs->new_cipher = cipher;
|
2016-07-11 18:19:03 +01:00
|
|
|
|
|
|
|
/* The PRF hash is now known. Set up the key schedule. */
|
2016-11-17 07:43:08 +00:00
|
|
|
if (!tls13_init_key_schedule(hs)) {
|
2016-11-30 16:24:40 +00:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
2016-09-06 19:13:43 +01:00
|
|
|
|
2016-11-30 16:24:40 +00:00
|
|
|
/* Incorporate the PSK into the running secret. */
|
2016-07-29 19:32:55 +01:00
|
|
|
if (ssl->s3->session_reused) {
|
2017-02-10 01:01:26 +00:00
|
|
|
if (!tls13_advance_key_schedule(hs, hs->new_session->master_key,
|
|
|
|
hs->new_session->master_key_length)) {
|
2016-07-29 19:32:55 +01:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
2017-01-12 18:17:07 +00:00
|
|
|
} else if (!tls13_advance_key_schedule(hs, kZeroes, hs->hash_len)) {
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2016-12-08 23:21:27 +00:00
|
|
|
if (!have_key_share) {
|
|
|
|
/* We do not support psk_ke and thus always require a key share. */
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2016-07-11 18:19:03 +01:00
|
|
|
/* Resolve ECDHE and incorporate it into the secret. */
|
2016-09-06 19:13:43 +01:00
|
|
|
uint8_t *dhe_secret;
|
|
|
|
size_t dhe_secret_len;
|
2017-02-08 21:33:15 +00:00
|
|
|
alert = SSL_AD_DECODE_ERROR;
|
2016-11-17 08:11:16 +00:00
|
|
|
if (!ssl_ext_key_share_parse_serverhello(hs, &dhe_secret, &dhe_secret_len,
|
2016-09-06 19:13:43 +01:00
|
|
|
&alert, &key_share)) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
2016-07-11 18:19:03 +01:00
|
|
|
|
2016-11-17 07:43:08 +00:00
|
|
|
if (!tls13_advance_key_schedule(hs, dhe_secret, dhe_secret_len)) {
|
2016-07-11 18:19:03 +01:00
|
|
|
OPENSSL_free(dhe_secret);
|
2016-09-06 19:13:43 +01:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
OPENSSL_free(dhe_secret);
|
|
|
|
|
2017-01-12 18:17:07 +00:00
|
|
|
if (!ssl_hash_current_message(hs) ||
|
2017-01-21 19:49:39 +00:00
|
|
|
!tls13_derive_handshake_secrets(hs) ||
|
2016-12-16 16:29:28 +00:00
|
|
|
!tls13_set_traffic_key(ssl, evp_aead_open, hs->server_handshake_secret,
|
2017-01-11 16:34:52 +00:00
|
|
|
hs->hash_len)) {
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If not sending early data, set client traffic keys now so that alerts are
|
|
|
|
* encrypted. */
|
|
|
|
if (!hs->early_data_offered &&
|
2016-12-16 16:29:28 +00:00
|
|
|
!tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_handshake_secret,
|
|
|
|
hs->hash_len)) {
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2016-12-11 18:30:41 +00:00
|
|
|
hs->tls13_state = state_process_encrypted_extensions;
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_read_message;
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static enum ssl_hs_wait_t do_process_encrypted_extensions(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2017-01-21 19:13:39 +00:00
|
|
|
if (!ssl_check_message_type(ssl, SSL3_MT_ENCRYPTED_EXTENSIONS)) {
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
CBS cbs;
|
|
|
|
CBS_init(&cbs, ssl->init_msg, ssl->init_num);
|
2016-12-03 07:20:34 +00:00
|
|
|
if (!ssl_parse_serverhello_tlsext(hs, &cbs)) {
|
2016-07-11 18:19:03 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
if (CBS_len(&cbs) != 0) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2017-01-11 16:34:52 +00:00
|
|
|
/* Store the negotiated ALPN in the session. */
|
|
|
|
if (ssl->s3->alpn_selected != NULL) {
|
|
|
|
hs->new_session->early_alpn =
|
|
|
|
BUF_memdup(ssl->s3->alpn_selected, ssl->s3->alpn_selected_len);
|
|
|
|
if (hs->new_session->early_alpn == NULL) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
hs->new_session->early_alpn_len = ssl->s3->alpn_selected_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl->early_data_accepted) {
|
|
|
|
if (ssl->session->cipher != hs->new_session->cipher ||
|
|
|
|
ssl->session->early_alpn_len != ssl->s3->alpn_selected_len ||
|
|
|
|
OPENSSL_memcmp(ssl->session->early_alpn, ssl->s3->alpn_selected,
|
|
|
|
ssl->s3->alpn_selected_len) != 0) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_ALPN_MISMATCH_ON_EARLY_DATA);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
2017-03-26 02:54:16 +01:00
|
|
|
if (ssl->s3->tlsext_channel_id_valid) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_ON_EARLY_DATA);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
2017-01-11 16:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Release offered session now that it is no longer needed. */
|
|
|
|
if (ssl->s3->session_reused) {
|
|
|
|
ssl_set_session(ssl, NULL);
|
|
|
|
}
|
|
|
|
|
2017-01-12 18:17:07 +00:00
|
|
|
if (!ssl_hash_current_message(hs)) {
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2016-12-11 18:30:41 +00:00
|
|
|
hs->tls13_state = state_process_certificate_request;
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_read_message;
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static enum ssl_hs_wait_t do_process_certificate_request(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2016-09-06 19:13:43 +01:00
|
|
|
/* CertificateRequest may only be sent in non-resumption handshakes. */
|
|
|
|
if (ssl->s3->session_reused) {
|
2016-12-11 18:30:41 +00:00
|
|
|
hs->tls13_state = state_process_server_finished;
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CertificateRequest is optional. */
|
|
|
|
if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
|
2016-12-11 18:30:41 +00:00
|
|
|
hs->tls13_state = state_process_server_certificate;
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
CBS cbs, context, supported_signature_algorithms;
|
|
|
|
CBS_init(&cbs, ssl->init_msg, ssl->init_num);
|
|
|
|
if (!CBS_get_u8_length_prefixed(&cbs, &context) ||
|
2016-08-18 07:32:23 +01:00
|
|
|
/* The request context is always empty during the handshake. */
|
|
|
|
CBS_len(&context) != 0 ||
|
2016-07-11 18:19:03 +01:00
|
|
|
!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
|
2016-08-01 17:12:47 +01:00
|
|
|
CBS_len(&supported_signature_algorithms) == 0 ||
|
2016-11-17 08:20:47 +00:00
|
|
|
!tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
|
2016-07-11 18:19:03 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2017-02-08 21:33:15 +00:00
|
|
|
uint8_t alert = SSL_AD_DECODE_ERROR;
|
2017-02-02 18:57:17 +00:00
|
|
|
STACK_OF(CRYPTO_BUFFER) *ca_names =
|
|
|
|
ssl_parse_client_CA_list(ssl, &alert, &cbs);
|
|
|
|
if (ca_names == NULL) {
|
2016-07-11 18:19:03 +01:00
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ignore extensions. */
|
|
|
|
CBS extensions;
|
|
|
|
if (!CBS_get_u16_length_prefixed(&cbs, &extensions) ||
|
|
|
|
CBS_len(&cbs) != 0) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2017-02-02 18:57:17 +00:00
|
|
|
sk_CRYPTO_BUFFER_pop_free(ca_names, CRYPTO_BUFFER_free);
|
2016-07-11 18:19:03 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
hs->cert_request = 1;
|
2017-02-02 18:57:17 +00:00
|
|
|
sk_CRYPTO_BUFFER_pop_free(hs->ca_names, CRYPTO_BUFFER_free);
|
|
|
|
hs->ca_names = ca_names;
|
|
|
|
ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
|
2016-07-11 18:19:03 +01:00
|
|
|
|
2017-01-12 18:17:07 +00:00
|
|
|
if (!ssl_hash_current_message(hs)) {
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2016-12-11 18:30:41 +00:00
|
|
|
hs->tls13_state = state_process_server_certificate;
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_read_message;
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static enum ssl_hs_wait_t do_process_server_certificate(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2017-01-21 19:13:39 +00:00
|
|
|
if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE) ||
|
2016-12-12 19:46:09 +00:00
|
|
|
!tls13_process_certificate(hs, 0 /* certificate required */) ||
|
2017-01-12 18:17:07 +00:00
|
|
|
!ssl_hash_current_message(hs)) {
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2016-12-11 18:30:41 +00:00
|
|
|
hs->tls13_state = state_process_server_certificate_verify;
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_read_message;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum ssl_hs_wait_t do_process_server_certificate_verify(
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2017-01-21 19:13:39 +00:00
|
|
|
if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY) ||
|
2016-12-12 19:46:09 +00:00
|
|
|
!tls13_process_certificate_verify(hs) ||
|
2017-01-12 18:17:07 +00:00
|
|
|
!ssl_hash_current_message(hs)) {
|
2016-11-16 10:10:08 +00:00
|
|
|
return ssl_hs_error;
|
2016-07-11 18:19:03 +01:00
|
|
|
}
|
|
|
|
|
2016-12-11 18:30:41 +00:00
|
|
|
hs->tls13_state = state_process_server_finished;
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_read_message;
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static enum ssl_hs_wait_t do_process_server_finished(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
2017-01-21 19:13:39 +00:00
|
|
|
if (!ssl_check_message_type(ssl, SSL3_MT_FINISHED) ||
|
2017-03-26 04:24:23 +01:00
|
|
|
!tls13_process_finished(hs, 0 /* don't use saved value */) ||
|
2017-01-12 18:17:07 +00:00
|
|
|
!ssl_hash_current_message(hs) ||
|
2016-07-11 18:19:03 +01:00
|
|
|
/* Update the secret to the master secret and derive traffic keys. */
|
2016-11-17 07:43:08 +00:00
|
|
|
!tls13_advance_key_schedule(hs, kZeroes, hs->hash_len) ||
|
|
|
|
!tls13_derive_application_secrets(hs)) {
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2016-07-22 16:39:29 +01:00
|
|
|
ssl->method->received_flight(ssl);
|
2017-01-11 16:34:52 +00:00
|
|
|
hs->tls13_state = state_send_end_of_early_data;
|
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum ssl_hs_wait_t do_send_end_of_early_data(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL *const ssl = hs->ssl;
|
|
|
|
/* TODO(svaldez): Stop sending early data. */
|
|
|
|
if (ssl->early_data_accepted &&
|
|
|
|
!ssl->method->add_alert(ssl, SSL3_AL_WARNING,
|
|
|
|
TLS1_AD_END_OF_EARLY_DATA)) {
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hs->early_data_offered &&
|
|
|
|
!tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_handshake_secret,
|
|
|
|
hs->hash_len)) {
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2016-12-10 20:46:58 +00:00
|
|
|
hs->tls13_state = state_send_client_certificate;
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
2016-12-10 20:46:58 +00:00
|
|
|
static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL *const ssl = hs->ssl;
|
2017-01-11 16:34:52 +00:00
|
|
|
|
2016-07-11 18:19:03 +01:00
|
|
|
/* The peer didn't request a certificate. */
|
2016-11-14 01:32:04 +00:00
|
|
|
if (!hs->cert_request) {
|
2017-01-13 00:44:57 +00:00
|
|
|
hs->tls13_state = state_complete_second_flight;
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call cert_cb to update the certificate. */
|
|
|
|
if (ssl->cert->cert_cb != NULL) {
|
|
|
|
int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
|
|
|
|
if (rv == 0) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
if (rv < 0) {
|
2016-12-11 18:30:41 +00:00
|
|
|
hs->tls13_state = state_send_client_certificate;
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_x509_lookup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 21:51:53 +01:00
|
|
|
if (!ssl_on_certificate_selected(hs) ||
|
2017-01-13 00:46:50 +00:00
|
|
|
!tls13_add_certificate(hs)) {
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2016-12-11 18:30:41 +00:00
|
|
|
hs->tls13_state = state_send_client_certificate_verify;
|
2017-01-13 00:31:28 +00:00
|
|
|
return ssl_hs_ok;
|
2016-07-11 18:19:03 +01:00
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs,
|
2016-07-11 18:19:03 +01:00
|
|
|
int is_first_run) {
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL *const ssl = hs->ssl;
|
2016-07-11 18:19:03 +01:00
|
|
|
/* Don't send CertificateVerify if there is no certificate. */
|
|
|
|
if (!ssl_has_certificate(ssl)) {
|
2017-01-13 00:44:57 +00:00
|
|
|
hs->tls13_state = state_complete_second_flight;
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
|
|
|
|
2017-01-13 00:46:50 +00:00
|
|
|
switch (tls13_add_certificate_verify(hs, is_first_run)) {
|
2016-07-11 18:19:03 +01:00
|
|
|
case ssl_private_key_success:
|
2017-01-13 00:44:57 +00:00
|
|
|
hs->tls13_state = state_complete_second_flight;
|
2017-01-13 00:31:28 +00:00
|
|
|
return ssl_hs_ok;
|
2016-07-11 18:19:03 +01:00
|
|
|
|
|
|
|
case ssl_private_key_retry:
|
2016-12-11 18:30:41 +00:00
|
|
|
hs->tls13_state = state_complete_client_certificate_verify;
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_private_key_operation;
|
|
|
|
|
|
|
|
case ssl_private_key_failure:
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(0);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2017-01-13 00:44:57 +00:00
|
|
|
static enum ssl_hs_wait_t do_complete_second_flight(SSL_HANDSHAKE *hs) {
|
2016-11-14 01:32:04 +00:00
|
|
|
SSL *const ssl = hs->ssl;
|
2016-09-24 00:25:11 +01:00
|
|
|
|
2017-01-13 00:44:57 +00:00
|
|
|
/* Send a Channel ID assertion if necessary. */
|
|
|
|
if (ssl->s3->tlsext_channel_id_valid) {
|
|
|
|
if (!ssl_do_channel_id_callback(ssl)) {
|
|
|
|
hs->tls13_state = state_complete_second_flight;
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
2016-09-24 00:25:11 +01:00
|
|
|
|
2017-01-13 00:44:57 +00:00
|
|
|
if (ssl->tlsext_channel_id_private == NULL) {
|
|
|
|
return ssl_hs_channel_id_lookup;
|
|
|
|
}
|
2016-09-24 00:25:11 +01:00
|
|
|
|
2017-01-13 00:44:57 +00:00
|
|
|
CBB cbb, body;
|
|
|
|
if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) ||
|
2017-01-12 18:17:07 +00:00
|
|
|
!tls1_write_channel_id(hs, &body) ||
|
2017-01-13 00:44:57 +00:00
|
|
|
!ssl_add_message_cbb(ssl, &cbb)) {
|
|
|
|
CBB_cleanup(&cbb);
|
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
2016-09-24 00:25:11 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 00:44:57 +00:00
|
|
|
/* Send a Finished message. */
|
2017-01-13 00:46:50 +00:00
|
|
|
if (!tls13_add_finished(hs)) {
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2017-01-13 00:44:57 +00:00
|
|
|
/* Derive the final keys and enable them. */
|
2016-11-01 17:39:36 +00:00
|
|
|
if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->server_traffic_secret_0,
|
|
|
|
hs->hash_len) ||
|
|
|
|
!tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_traffic_secret_0,
|
|
|
|
hs->hash_len) ||
|
2016-11-17 07:43:08 +00:00
|
|
|
!tls13_derive_resumption_secret(hs)) {
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_error;
|
|
|
|
}
|
|
|
|
|
2016-12-11 18:30:41 +00:00
|
|
|
hs->tls13_state = state_done;
|
2016-07-11 18:19:03 +01:00
|
|
|
return ssl_hs_flush;
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:32:04 +00:00
|
|
|
enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs) {
|
2016-12-11 18:30:41 +00:00
|
|
|
while (hs->tls13_state != state_done) {
|
2016-07-11 18:19:03 +01:00
|
|
|
enum ssl_hs_wait_t ret = ssl_hs_error;
|
2016-12-11 18:30:41 +00:00
|
|
|
enum client_hs_state_t state = hs->tls13_state;
|
2016-07-11 18:19:03 +01:00
|
|
|
switch (state) {
|
2016-07-18 17:40:30 +01:00
|
|
|
case state_process_hello_retry_request:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = do_process_hello_retry_request(hs);
|
2016-07-18 17:40:30 +01:00
|
|
|
break;
|
|
|
|
case state_send_second_client_hello:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = do_send_second_client_hello(hs);
|
2016-07-18 17:40:30 +01:00
|
|
|
break;
|
2016-07-11 18:19:03 +01:00
|
|
|
case state_process_server_hello:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = do_process_server_hello(hs);
|
2016-07-11 18:19:03 +01:00
|
|
|
break;
|
|
|
|
case state_process_encrypted_extensions:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = do_process_encrypted_extensions(hs);
|
2016-07-11 18:19:03 +01:00
|
|
|
break;
|
|
|
|
case state_process_certificate_request:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = do_process_certificate_request(hs);
|
2016-07-11 18:19:03 +01:00
|
|
|
break;
|
|
|
|
case state_process_server_certificate:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = do_process_server_certificate(hs);
|
2016-07-11 18:19:03 +01:00
|
|
|
break;
|
|
|
|
case state_process_server_certificate_verify:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = do_process_server_certificate_verify(hs);
|
2016-07-11 18:19:03 +01:00
|
|
|
break;
|
|
|
|
case state_process_server_finished:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = do_process_server_finished(hs);
|
2016-07-11 18:19:03 +01:00
|
|
|
break;
|
2017-01-11 16:34:52 +00:00
|
|
|
case state_send_end_of_early_data:
|
|
|
|
ret = do_send_end_of_early_data(hs);
|
|
|
|
break;
|
2016-07-11 18:19:03 +01:00
|
|
|
case state_send_client_certificate:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = do_send_client_certificate(hs);
|
2016-07-11 18:19:03 +01:00
|
|
|
break;
|
|
|
|
case state_send_client_certificate_verify:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = do_send_client_certificate_verify(hs, 1 /* first run */);
|
2016-09-24 00:25:11 +01:00
|
|
|
break;
|
2016-07-11 18:19:03 +01:00
|
|
|
case state_complete_client_certificate_verify:
|
2016-11-14 01:32:04 +00:00
|
|
|
ret = do_send_client_certificate_verify(hs, 0 /* complete */);
|
2016-09-24 00:25:11 +01:00
|
|
|
break;
|
2017-01-13 00:44:57 +00:00
|
|
|
case state_complete_second_flight:
|
|
|
|
ret = do_complete_second_flight(hs);
|
2016-07-11 18:19:03 +01:00
|
|
|
break;
|
|
|
|
case state_done:
|
|
|
|
ret = ssl_hs_ok;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != ssl_hs_ok) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ssl_hs_ok;
|
|
|
|
}
|
2016-07-27 16:10:52 +01:00
|
|
|
|
|
|
|
int tls13_process_new_session_ticket(SSL *ssl) {
|
2017-01-08 11:04:43 +00:00
|
|
|
int ret = 0;
|
2017-02-01 23:49:37 +00:00
|
|
|
SSL_SESSION *session = SSL_SESSION_dup(ssl->s3->established_session,
|
|
|
|
SSL_SESSION_INCLUDE_NONAUTH);
|
2016-07-27 16:10:52 +01:00
|
|
|
if (session == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-28 19:00:32 +00:00
|
|
|
ssl_session_rebase_time(ssl, session);
|
2016-11-03 20:59:25 +00:00
|
|
|
|
2017-01-28 19:00:32 +00:00
|
|
|
uint32_t server_timeout;
|
2016-11-01 17:39:36 +00:00
|
|
|
CBS cbs, ticket, extensions;
|
2016-07-27 16:10:52 +01:00
|
|
|
CBS_init(&cbs, ssl->init_msg, ssl->init_num);
|
2017-01-28 19:00:32 +00:00
|
|
|
if (!CBS_get_u32(&cbs, &server_timeout) ||
|
2016-11-01 17:39:36 +00:00
|
|
|
!CBS_get_u32(&cbs, &session->ticket_age_add) ||
|
2016-07-27 16:10:52 +01:00
|
|
|
!CBS_get_u16_length_prefixed(&cbs, &ticket) ||
|
|
|
|
!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen) ||
|
2016-09-01 17:29:49 +01:00
|
|
|
!CBS_get_u16_length_prefixed(&cbs, &extensions) ||
|
2016-07-27 16:10:52 +01:00
|
|
|
CBS_len(&cbs) != 0) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
2017-01-08 11:04:43 +00:00
|
|
|
goto err;
|
2016-07-27 16:10:52 +01:00
|
|
|
}
|
|
|
|
|
2017-01-28 19:00:32 +00:00
|
|
|
/* Cap the renewable lifetime by the server advertised value. This avoids
|
2017-02-20 22:00:20 +00:00
|
|
|
* wasting bandwidth on 0-RTT when we know the server will reject it. */
|
|
|
|
if (session->timeout > server_timeout) {
|
|
|
|
session->timeout = server_timeout;
|
2017-01-28 19:00:32 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 20:29:45 +00:00
|
|
|
/* Parse out the extensions. */
|
|
|
|
int have_early_data_info = 0;
|
|
|
|
CBS early_data_info;
|
|
|
|
const SSL_EXTENSION_TYPE ext_types[] = {
|
|
|
|
{TLSEXT_TYPE_ticket_early_data_info, &have_early_data_info,
|
|
|
|
&early_data_info},
|
|
|
|
};
|
|
|
|
|
2017-02-08 21:33:15 +00:00
|
|
|
uint8_t alert = SSL_AD_DECODE_ERROR;
|
2016-12-07 20:29:45 +00:00
|
|
|
if (!ssl_parse_extensions(&extensions, &alert, ext_types,
|
|
|
|
OPENSSL_ARRAY_SIZE(ext_types),
|
|
|
|
1 /* ignore unknown */)) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
|
2017-01-08 11:04:43 +00:00
|
|
|
goto err;
|
2016-12-07 20:29:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 22:33:24 +01:00
|
|
|
if (have_early_data_info && ssl->cert->enable_early_data) {
|
2016-12-07 20:29:45 +00:00
|
|
|
if (!CBS_get_u32(&early_data_info, &session->ticket_max_early_data) ||
|
|
|
|
CBS_len(&early_data_info) != 0) {
|
|
|
|
ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
|
2017-01-08 11:04:43 +00:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
|
|
|
|
goto err;
|
2016-12-07 20:29:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-01 17:39:36 +00:00
|
|
|
session->ticket_age_add_valid = 1;
|
2016-07-27 16:10:52 +01:00
|
|
|
session->not_resumable = 0;
|
|
|
|
|
2016-11-01 17:39:36 +00:00
|
|
|
if (ssl->ctx->new_session_cb != NULL &&
|
2016-07-27 16:10:52 +01:00
|
|
|
ssl->ctx->new_session_cb(ssl, session)) {
|
|
|
|
/* |new_session_cb|'s return value signals that it took ownership. */
|
2017-01-08 11:04:43 +00:00
|
|
|
session = NULL;
|
2016-07-27 16:10:52 +01:00
|
|
|
}
|
|
|
|
|
2017-01-08 11:04:43 +00:00
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
err:
|
2016-07-27 16:10:52 +01:00
|
|
|
SSL_SESSION_free(session);
|
2017-01-08 11:04:43 +00:00
|
|
|
return ret;
|
2016-07-27 16:10:52 +01:00
|
|
|
}
|
2016-08-16 07:17:03 +01:00
|
|
|
|
2016-11-17 07:43:08 +00:00
|
|
|
void ssl_clear_tls13_state(SSL_HANDSHAKE *hs) {
|
|
|
|
SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
|
2016-08-16 07:17:03 +01:00
|
|
|
|
2016-11-17 07:43:08 +00:00
|
|
|
OPENSSL_free(hs->key_share_bytes);
|
|
|
|
hs->key_share_bytes = NULL;
|
|
|
|
hs->key_share_bytes_len = 0;
|
2016-08-16 07:17:03 +01:00
|
|
|
}
|