mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-07-29 11:41:15 +03:00
Merge pull request #8727 from ronald-cron-arm/tls13-ignore-early-data-when-rejected
TLS 1.3: SRV: Ignore early data when rejected
This commit is contained in:
@ -3985,6 +3985,31 @@ static int ssl_prepare_record_content(mbedtls_ssl_context *ssl,
|
||||
rec)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "ssl_decrypt_buf", ret);
|
||||
|
||||
#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_SRV_C)
|
||||
/*
|
||||
* Although the server rejected early data, it might receive early
|
||||
* data as long as it has not received the client Finished message.
|
||||
* It is encrypted with early keys and should be ignored as stated
|
||||
* in section 4.2.10 of RFC 8446:
|
||||
*
|
||||
* "Ignore the extension and return a regular 1-RTT response. The
|
||||
* server then skips past early data by attempting to deprotect
|
||||
* received records using the handshake traffic key, discarding
|
||||
* records which fail deprotection (up to the configured
|
||||
* max_early_data_size). Once a record is deprotected successfully,
|
||||
* it is treated as the start of the client's second flight and the
|
||||
* server proceeds as with an ordinary 1-RTT handshake."
|
||||
*/
|
||||
if ((old_msg_type == MBEDTLS_SSL_MSG_APPLICATION_DATA) &&
|
||||
(ssl->discard_early_data_record ==
|
||||
MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD)) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(
|
||||
3, ("EarlyData: deprotect and discard app data records."));
|
||||
/* TODO: Add max_early_data_size check here, see issue 6347 */
|
||||
ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_SRV_C */
|
||||
|
||||
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
||||
if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
|
||||
ssl->conf->ignore_unexpected_cid
|
||||
@ -3994,9 +4019,27 @@ static int ssl_prepare_record_content(mbedtls_ssl_context *ssl,
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
||||
|
||||
/*
|
||||
* The decryption of the record failed, no reason to ignore it,
|
||||
* return in error with the decryption error code.
|
||||
*/
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_SRV_C)
|
||||
/*
|
||||
* If the server were discarding protected records that it fails to
|
||||
* deprotect because it has rejected early data, as we have just
|
||||
* deprotected successfully a record, the server has to resume normal
|
||||
* operation and fail the connection if the deprotection of a record
|
||||
* fails.
|
||||
*/
|
||||
if (ssl->discard_early_data_record ==
|
||||
MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD) {
|
||||
ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD;
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_SRV_C */
|
||||
|
||||
if (old_msg_type != rec->type) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(4, ("record type after decrypt (before %d): %d",
|
||||
old_msg_type, rec->type));
|
||||
@ -4070,6 +4113,32 @@ static int ssl_prepare_record_content(mbedtls_ssl_context *ssl,
|
||||
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_SRV_C)
|
||||
/*
|
||||
* Although the server rejected early data because it needed to send an
|
||||
* HelloRetryRequest message, it might receive early data as long as it has
|
||||
* not received the client Finished message.
|
||||
* The early data is encrypted with early keys and should be ignored as
|
||||
* stated in section 4.2.10 of RFC 8446 (second case):
|
||||
*
|
||||
* "The server then ignores early data by skipping all records with an
|
||||
* external content type of "application_data" (indicating that they are
|
||||
* encrypted), up to the configured max_early_data_size. Ignore application
|
||||
* data message before 2nd ClientHello when early_data was received in 1st
|
||||
* ClientHello."
|
||||
*/
|
||||
if (ssl->discard_early_data_record == MBEDTLS_SSL_EARLY_DATA_DISCARD) {
|
||||
if (rec->type == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(
|
||||
3, ("EarlyData: Ignore application message before 2nd ClientHello"));
|
||||
/* TODO: Add max_early_data_size check here, see issue 6347 */
|
||||
return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
|
||||
} else if (rec->type == MBEDTLS_SSL_MSG_HANDSHAKE) {
|
||||
ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD;
|
||||
}
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_SRV_C */
|
||||
|
||||
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
|
||||
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
||||
mbedtls_ssl_dtls_replay_update(ssl);
|
||||
|
@ -1098,9 +1098,14 @@ static int ssl_handshake_init(mbedtls_ssl_context *ssl)
|
||||
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_CLI_C)
|
||||
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
||||
#if defined(MBEDTLS_SSL_CLI_C)
|
||||
ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_SENT;
|
||||
#endif
|
||||
#if defined(MBEDTLS_SSL_SRV_C)
|
||||
ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD;
|
||||
#endif
|
||||
#endif /* MBEDTLS_SSL_EARLY_DATA */
|
||||
|
||||
/* Initialize structures */
|
||||
mbedtls_ssl_session_init(ssl->session_negotiate);
|
||||
|
@ -1182,7 +1182,8 @@ int mbedtls_ssl_tls13_write_client_hello_exts(mbedtls_ssl_context *ssl,
|
||||
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
||||
if (mbedtls_ssl_conf_tls13_is_some_psk_enabled(ssl) &&
|
||||
ssl_tls13_early_data_has_valid_ticket(ssl) &&
|
||||
ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED) {
|
||||
ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
|
||||
ssl->handshake->hello_retry_request_count == 0) {
|
||||
|
||||
ret = mbedtls_ssl_tls13_write_early_data_ext(
|
||||
ssl, 0, p, end, &ext_len);
|
||||
|
@ -1533,6 +1533,12 @@ static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
|
||||
unsigned int extension_type;
|
||||
size_t extension_data_len;
|
||||
const unsigned char *extension_data_end;
|
||||
uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
|
||||
|
||||
if (ssl->handshake->hello_retry_request_count > 0) {
|
||||
/* Do not accept early data extension in 2nd ClientHello */
|
||||
allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
|
||||
}
|
||||
|
||||
/* RFC 8446, section 4.2.11
|
||||
*
|
||||
@ -1560,7 +1566,7 @@ static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
|
||||
|
||||
ret = mbedtls_ssl_tls13_check_received_extension(
|
||||
ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
|
||||
MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH);
|
||||
allowed_exts);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
@ -1780,28 +1786,15 @@ static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
||||
static int ssl_tls13_is_early_data_accepted(mbedtls_ssl_context *ssl,
|
||||
int hrr_required)
|
||||
static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
|
||||
{
|
||||
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
||||
|
||||
if ((handshake->received_extensions &
|
||||
MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) == 0) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(
|
||||
1, ("EarlyData: no early data extension received."));
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(
|
||||
1,
|
||||
("EarlyData: rejected, feature disabled in server configuration."));
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (hrr_required) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(1, ("EarlyData: rejected, HRR required."));
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!handshake->resume) {
|
||||
@ -1810,7 +1803,7 @@ static int ssl_tls13_is_early_data_accepted(mbedtls_ssl_context *ssl,
|
||||
resumption. */
|
||||
MBEDTLS_SSL_DEBUG_MSG(
|
||||
1, ("EarlyData: rejected, not a session resumption."));
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* RFC 8446 4.2.10
|
||||
@ -1833,7 +1826,7 @@ static int ssl_tls13_is_early_data_accepted(mbedtls_ssl_context *ssl,
|
||||
MBEDTLS_SSL_DEBUG_MSG(
|
||||
1, ("EarlyData: rejected, the selected key in "
|
||||
"`pre_shared_key` is not the first one."));
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (handshake->ciphersuite_info->id !=
|
||||
@ -1841,7 +1834,7 @@ static int ssl_tls13_is_early_data_accepted(mbedtls_ssl_context *ssl,
|
||||
MBEDTLS_SSL_DEBUG_MSG(
|
||||
1, ("EarlyData: rejected, the selected ciphersuite is not the one "
|
||||
"of the selected pre-shared key."));
|
||||
return 0;
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
@ -1850,10 +1843,10 @@ static int ssl_tls13_is_early_data_accepted(mbedtls_ssl_context *ssl,
|
||||
1,
|
||||
("EarlyData: rejected, early_data not allowed in ticket "
|
||||
"permission bits."));
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_EARLY_DATA */
|
||||
|
||||
@ -1885,15 +1878,22 @@ static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
||||
ssl->handshake->early_data_accepted =
|
||||
ssl_tls13_is_early_data_accepted(ssl, hrr_required);
|
||||
if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
|
||||
ssl->handshake->early_data_accepted =
|
||||
(!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
|
||||
|
||||
if (ssl->handshake->early_data_accepted) {
|
||||
ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(
|
||||
1, "mbedtls_ssl_tls13_compute_early_transform", ret);
|
||||
return ret;
|
||||
if (ssl->handshake->early_data_accepted) {
|
||||
ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(
|
||||
1, "mbedtls_ssl_tls13_compute_early_transform", ret);
|
||||
return ret;
|
||||
}
|
||||
} else {
|
||||
ssl->discard_early_data_record =
|
||||
hrr_required ?
|
||||
MBEDTLS_SSL_EARLY_DATA_DISCARD :
|
||||
MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
|
||||
}
|
||||
}
|
||||
#else
|
||||
|
Reference in New Issue
Block a user