2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2020-02-05 17:14:29 +01:00
|
|
|
* Generic SSL/TLS messaging layer functions
|
|
|
|
* (record layer + retransmission state machine)
|
2009-01-03 22:22:43 +01:00
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* http://www.ietf.org/rfc/rfc2246.txt
|
|
|
|
* http://www.ietf.org/rfc/rfc4346.txt
|
|
|
|
*/
|
|
|
|
|
2020-06-03 01:43:33 +02:00
|
|
|
#include "common.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS_C)
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2016-04-26 08:43:27 +02:00
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/ssl.h"
|
2021-03-05 19:38:47 +01:00
|
|
|
#include "ssl_misc.h"
|
2019-12-18 16:07:04 +01:00
|
|
|
#include "mbedtls/debug.h"
|
|
|
|
#include "mbedtls/error.h"
|
2018-04-17 16:51:09 +02:00
|
|
|
#include "mbedtls/platform_util.h"
|
2019-05-16 13:39:07 +02:00
|
|
|
#include "mbedtls/version.h"
|
2021-10-20 12:09:35 +02:00
|
|
|
#include "constant_time_internal.h"
|
2021-10-19 12:22:25 +02:00
|
|
|
#include "mbedtls/constant_time.h"
|
2013-08-27 21:55:01 +02:00
|
|
|
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2019-01-10 11:27:10 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-06-07 13:07:21 +02:00
|
|
|
#include "psa_util_internal.h"
|
2019-01-10 11:27:10 +01:00
|
|
|
#include "psa/crypto.h"
|
|
|
|
#endif
|
|
|
|
|
2016-10-07 15:47:14 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/oid.h"
|
2014-04-11 11:06:22 +02:00
|
|
|
#endif
|
|
|
|
|
2022-12-23 17:00:06 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-05-30 11:45:00 +02:00
|
|
|
/* Define a local translating function to save code size by not using too many
|
|
|
|
* arguments in each translating place. */
|
|
|
|
static int local_err_translation(psa_status_t status)
|
|
|
|
{
|
|
|
|
return psa_status_to_mbedtls(status, psa_to_ssl_errors,
|
2023-05-30 15:45:17 +02:00
|
|
|
ARRAY_LENGTH(psa_to_ssl_errors),
|
2023-05-30 11:45:00 +02:00
|
|
|
psa_generic_status_to_mbedtls);
|
|
|
|
}
|
|
|
|
#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
|
2022-12-23 17:00:06 +01:00
|
|
|
#endif
|
|
|
|
|
2023-05-09 12:00:07 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
|
|
|
|
#if defined(PSA_WANT_ALG_SHA_384)
|
|
|
|
#define MAX_HASH_BLOCK_LENGTH PSA_HASH_BLOCK_LENGTH(PSA_ALG_SHA_384)
|
|
|
|
#elif defined(PSA_WANT_ALG_SHA_256)
|
|
|
|
#define MAX_HASH_BLOCK_LENGTH PSA_HASH_BLOCK_LENGTH(PSA_ALG_SHA_256)
|
|
|
|
#else /* See check_config.h */
|
|
|
|
#define MAX_HASH_BLOCK_LENGTH PSA_HASH_BLOCK_LENGTH(PSA_ALG_SHA_1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
MBEDTLS_STATIC_TESTABLE
|
|
|
|
int mbedtls_ct_hmac(mbedtls_svc_key_id_t key,
|
|
|
|
psa_algorithm_t mac_alg,
|
|
|
|
const unsigned char *add_data,
|
|
|
|
size_t add_data_len,
|
|
|
|
const unsigned char *data,
|
|
|
|
size_t data_len_secret,
|
|
|
|
size_t min_data_len,
|
|
|
|
size_t max_data_len,
|
|
|
|
unsigned char *output)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This function breaks the HMAC abstraction and uses psa_hash_clone()
|
|
|
|
* extension in order to get constant-flow behaviour.
|
|
|
|
*
|
|
|
|
* HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
|
|
|
|
* concatenation, and okey/ikey are the XOR of the key with some fixed bit
|
|
|
|
* patterns (see RFC 2104, sec. 2).
|
|
|
|
*
|
|
|
|
* We'll first compute ikey/okey, then inner_hash = HASH(ikey + msg) by
|
|
|
|
* hashing up to minlen, then cloning the context, and for each byte up
|
|
|
|
* to maxlen finishing up the hash computation, keeping only the
|
|
|
|
* correct result.
|
|
|
|
*
|
|
|
|
* Then we only need to compute HASH(okey + inner_hash) and we're done.
|
|
|
|
*/
|
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH(mac_alg);
|
|
|
|
const size_t block_size = PSA_HASH_BLOCK_LENGTH(hash_alg);
|
|
|
|
unsigned char key_buf[MAX_HASH_BLOCK_LENGTH];
|
|
|
|
const size_t hash_size = PSA_HASH_LENGTH(hash_alg);
|
|
|
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
|
|
|
size_t hash_length;
|
|
|
|
|
|
|
|
unsigned char aux_out[PSA_HASH_MAX_SIZE];
|
|
|
|
psa_hash_operation_t aux_operation = PSA_HASH_OPERATION_INIT;
|
|
|
|
size_t offset;
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
size_t mac_key_length;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
#define PSA_CHK(func_call) \
|
|
|
|
do { \
|
|
|
|
status = (func_call); \
|
|
|
|
if (status != PSA_SUCCESS) \
|
|
|
|
goto cleanup; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* Export MAC key
|
|
|
|
* We assume key length is always exactly the output size
|
|
|
|
* which is never more than the block size, thus we use block_size
|
|
|
|
* as the key buffer size.
|
|
|
|
*/
|
|
|
|
PSA_CHK(psa_export_key(key, key_buf, block_size, &mac_key_length));
|
|
|
|
|
|
|
|
/* Calculate ikey */
|
|
|
|
for (i = 0; i < mac_key_length; i++) {
|
|
|
|
key_buf[i] = (unsigned char) (key_buf[i] ^ 0x36);
|
|
|
|
}
|
|
|
|
for (; i < block_size; ++i) {
|
|
|
|
key_buf[i] = 0x36;
|
|
|
|
}
|
|
|
|
|
|
|
|
PSA_CHK(psa_hash_setup(&operation, hash_alg));
|
|
|
|
|
|
|
|
/* Now compute inner_hash = HASH(ikey + msg) */
|
|
|
|
PSA_CHK(psa_hash_update(&operation, key_buf, block_size));
|
|
|
|
PSA_CHK(psa_hash_update(&operation, add_data, add_data_len));
|
|
|
|
PSA_CHK(psa_hash_update(&operation, data, min_data_len));
|
|
|
|
|
|
|
|
/* Fill the hash buffer in advance with something that is
|
|
|
|
* not a valid hash (barring an attack on the hash and
|
|
|
|
* deliberately-crafted input), in case the caller doesn't
|
|
|
|
* check the return status properly. */
|
|
|
|
memset(output, '!', hash_size);
|
|
|
|
|
|
|
|
/* For each possible length, compute the hash up to that point */
|
|
|
|
for (offset = min_data_len; offset <= max_data_len; offset++) {
|
|
|
|
PSA_CHK(psa_hash_clone(&operation, &aux_operation));
|
|
|
|
PSA_CHK(psa_hash_finish(&aux_operation, aux_out,
|
|
|
|
PSA_HASH_MAX_SIZE, &hash_length));
|
|
|
|
/* Keep only the correct inner_hash in the output buffer */
|
2023-08-10 15:01:51 +02:00
|
|
|
mbedtls_ct_memcpy_if(mbedtls_ct_uint_eq(offset, data_len_secret),
|
2023-05-17 13:36:01 +02:00
|
|
|
output, aux_out, NULL, hash_size);
|
2023-05-09 12:00:07 +02:00
|
|
|
|
|
|
|
if (offset < max_data_len) {
|
|
|
|
PSA_CHK(psa_hash_update(&operation, data + offset, 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Abort current operation to prepare for final operation */
|
|
|
|
PSA_CHK(psa_hash_abort(&operation));
|
|
|
|
|
|
|
|
/* Calculate okey */
|
|
|
|
for (i = 0; i < mac_key_length; i++) {
|
|
|
|
key_buf[i] = (unsigned char) ((key_buf[i] ^ 0x36) ^ 0x5C);
|
|
|
|
}
|
|
|
|
for (; i < block_size; ++i) {
|
|
|
|
key_buf[i] = 0x5C;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now compute HASH(okey + inner_hash) */
|
|
|
|
PSA_CHK(psa_hash_setup(&operation, hash_alg));
|
|
|
|
PSA_CHK(psa_hash_update(&operation, key_buf, block_size));
|
|
|
|
PSA_CHK(psa_hash_update(&operation, output, hash_size));
|
|
|
|
PSA_CHK(psa_hash_finish(&operation, output, hash_size, &hash_length));
|
|
|
|
|
|
|
|
#undef PSA_CHK
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
mbedtls_platform_zeroize(key_buf, MAX_HASH_BLOCK_LENGTH);
|
|
|
|
mbedtls_platform_zeroize(aux_out, PSA_HASH_MAX_SIZE);
|
|
|
|
|
|
|
|
psa_hash_abort(&operation);
|
|
|
|
psa_hash_abort(&aux_operation);
|
|
|
|
return PSA_TO_MBEDTLS_ERR(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef MAX_HASH_BLOCK_LENGTH
|
|
|
|
|
|
|
|
#else
|
|
|
|
MBEDTLS_STATIC_TESTABLE
|
|
|
|
int mbedtls_ct_hmac(mbedtls_md_context_t *ctx,
|
|
|
|
const unsigned char *add_data,
|
|
|
|
size_t add_data_len,
|
|
|
|
const unsigned char *data,
|
|
|
|
size_t data_len_secret,
|
|
|
|
size_t min_data_len,
|
|
|
|
size_t max_data_len,
|
|
|
|
unsigned char *output)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This function breaks the HMAC abstraction and uses the md_clone()
|
|
|
|
* extension to the MD API in order to get constant-flow behaviour.
|
|
|
|
*
|
|
|
|
* HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
|
|
|
|
* concatenation, and okey/ikey are the XOR of the key with some fixed bit
|
|
|
|
* patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx.
|
|
|
|
*
|
|
|
|
* We'll first compute inner_hash = HASH(ikey + msg) by hashing up to
|
|
|
|
* minlen, then cloning the context, and for each byte up to maxlen
|
|
|
|
* finishing up the hash computation, keeping only the correct result.
|
|
|
|
*
|
|
|
|
* Then we only need to compute HASH(okey + inner_hash) and we're done.
|
|
|
|
*/
|
|
|
|
const mbedtls_md_type_t md_alg = mbedtls_md_get_type(ctx->md_info);
|
|
|
|
/* TLS 1.2 only supports SHA-384, SHA-256, SHA-1, MD-5,
|
|
|
|
* all of which have the same block size except SHA-384. */
|
|
|
|
const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64;
|
|
|
|
const unsigned char * const ikey = ctx->hmac_ctx;
|
|
|
|
const unsigned char * const okey = ikey + block_size;
|
|
|
|
const size_t hash_size = mbedtls_md_get_size(ctx->md_info);
|
|
|
|
|
|
|
|
unsigned char aux_out[MBEDTLS_MD_MAX_SIZE];
|
|
|
|
mbedtls_md_context_t aux;
|
|
|
|
size_t offset;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
mbedtls_md_init(&aux);
|
|
|
|
|
|
|
|
#define MD_CHK(func_call) \
|
|
|
|
do { \
|
|
|
|
ret = (func_call); \
|
|
|
|
if (ret != 0) \
|
|
|
|
goto cleanup; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
MD_CHK(mbedtls_md_setup(&aux, ctx->md_info, 0));
|
|
|
|
|
|
|
|
/* After hmac_start() of hmac_reset(), ikey has already been hashed,
|
|
|
|
* so we can start directly with the message */
|
|
|
|
MD_CHK(mbedtls_md_update(ctx, add_data, add_data_len));
|
|
|
|
MD_CHK(mbedtls_md_update(ctx, data, min_data_len));
|
|
|
|
|
|
|
|
/* Fill the hash buffer in advance with something that is
|
|
|
|
* not a valid hash (barring an attack on the hash and
|
|
|
|
* deliberately-crafted input), in case the caller doesn't
|
|
|
|
* check the return status properly. */
|
|
|
|
memset(output, '!', hash_size);
|
|
|
|
|
|
|
|
/* For each possible length, compute the hash up to that point */
|
|
|
|
for (offset = min_data_len; offset <= max_data_len; offset++) {
|
|
|
|
MD_CHK(mbedtls_md_clone(&aux, ctx));
|
|
|
|
MD_CHK(mbedtls_md_finish(&aux, aux_out));
|
|
|
|
/* Keep only the correct inner_hash in the output buffer */
|
2023-08-10 12:58:18 +02:00
|
|
|
mbedtls_ct_memcpy_if(mbedtls_ct_uint_eq(offset, data_len_secret),
|
2023-05-17 13:36:01 +02:00
|
|
|
output, aux_out, NULL, hash_size);
|
2023-05-09 12:00:07 +02:00
|
|
|
|
|
|
|
if (offset < max_data_len) {
|
|
|
|
MD_CHK(mbedtls_md_update(ctx, data + offset, 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The context needs to finish() before it starts() again */
|
|
|
|
MD_CHK(mbedtls_md_finish(ctx, aux_out));
|
|
|
|
|
|
|
|
/* Now compute HASH(okey + inner_hash) */
|
|
|
|
MD_CHK(mbedtls_md_starts(ctx));
|
|
|
|
MD_CHK(mbedtls_md_update(ctx, okey, block_size));
|
|
|
|
MD_CHK(mbedtls_md_update(ctx, output, hash_size));
|
|
|
|
MD_CHK(mbedtls_md_finish(ctx, output));
|
|
|
|
|
|
|
|
/* Done, get ready for next time */
|
|
|
|
MD_CHK(mbedtls_md_hmac_reset(ctx));
|
|
|
|
|
|
|
|
#undef MD_CHK
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
mbedtls_md_free(&aux);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl);
|
2018-08-10 12:12:52 +02:00
|
|
|
|
2014-09-29 14:04:42 +02:00
|
|
|
/*
|
|
|
|
* Start a timer.
|
|
|
|
* Passing millisecs = 0 cancels a running timer.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_set_timer(mbedtls_ssl_context *ssl, uint32_t millisecs)
|
2014-09-29 14:04:42 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->f_set_timer == NULL) {
|
2015-05-12 20:55:41 +02:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2015-05-12 20:55:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("set_timer to %d ms", (int) millisecs));
|
|
|
|
ssl->f_set_timer(ssl->p_timer, millisecs / 4, millisecs);
|
2014-09-29 14:04:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return -1 is timer is expired, 0 if it isn't.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_check_timer(mbedtls_ssl_context *ssl)
|
2014-09-29 14:04:42 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->f_get_timer == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-12 20:55:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->f_get_timer(ssl->p_timer) == 2) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("timer expired"));
|
|
|
|
return -1;
|
2015-05-13 16:22:05 +02:00
|
|
|
}
|
2014-09-29 14:04:42 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2014-09-29 14:04:42 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_parse_record_header(mbedtls_ssl_context const *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
size_t len,
|
|
|
|
mbedtls_record *rec);
|
|
|
|
|
|
|
|
int mbedtls_ssl_check_record(mbedtls_ssl_context const *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
size_t buflen)
|
2021-05-20 14:46:20 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("=> mbedtls_ssl_check_record"));
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "record buffer", buf, buflen);
|
2021-05-20 14:46:20 +02:00
|
|
|
|
|
|
|
/* We don't support record checking in TLS because
|
2021-06-25 14:40:09 +02:00
|
|
|
* there doesn't seem to be a usecase for it.
|
2021-05-20 14:46:20 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM) {
|
2021-05-20 14:46:20 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
else {
|
2021-05-20 14:46:20 +02:00
|
|
|
mbedtls_record rec;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_parse_record_header(ssl, buf, buflen, &rec);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(3, "ssl_parse_record_header", ret);
|
2021-05-20 14:46:20 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->transform_in != NULL) {
|
|
|
|
ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in, &rec);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(3, "mbedtls_ssl_decrypt_buf", ret);
|
2021-05-20 14:46:20 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
|
|
|
|
|
|
|
exit:
|
|
|
|
/* On success, we have decrypted the buffer in-place, so make
|
|
|
|
* sure we don't leak any plaintext data. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(buf, buflen);
|
2021-05-20 14:46:20 +02:00
|
|
|
|
|
|
|
/* For the purpose of this API, treat messages with unexpected CID
|
|
|
|
* as well as such from future epochs as unexpected. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
|
|
|
|
ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) {
|
2021-05-20 14:46:20 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("<= mbedtls_ssl_check_record"));
|
|
|
|
return ret;
|
2021-05-20 14:46:20 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 12:33:50 +02:00
|
|
|
#define SSL_DONT_FORCE_FLUSH 0
|
|
|
|
#define SSL_FORCE_FLUSH 1
|
|
|
|
|
2015-05-13 16:22:05 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2018-08-06 12:19:13 +02:00
|
|
|
|
2018-08-28 11:09:23 +02:00
|
|
|
/* Forward declarations for functions related to message buffering. */
|
2023-01-11 14:50:10 +01:00
|
|
|
static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl,
|
|
|
|
uint8_t slot);
|
|
|
|
static void ssl_free_buffered_record(mbedtls_ssl_context *ssl);
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_load_buffered_message(mbedtls_ssl_context *ssl);
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_load_buffered_record(mbedtls_ssl_context *ssl);
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_buffer_message(mbedtls_ssl_context *ssl);
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_buffer_future_record(mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_record const *rec);
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl);
|
2018-08-28 11:09:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static size_t ssl_get_maximum_datagram_size(mbedtls_ssl_context const *ssl)
|
2018-08-06 12:19:13 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
|
2019-11-28 15:29:44 +01:00
|
|
|
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
|
|
|
|
size_t out_buf_len = ssl->out_buf_len;
|
|
|
|
#else
|
|
|
|
size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
|
|
|
|
#endif
|
2018-08-06 12:19:13 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mtu != 0 && mtu < out_buf_len) {
|
|
|
|
return mtu;
|
|
|
|
}
|
2018-08-06 12:19:13 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return out_buf_len;
|
2018-08-06 12:19:13 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_get_remaining_space_in_datagram(mbedtls_ssl_context const *ssl)
|
2018-08-06 12:33:50 +02:00
|
|
|
{
|
2018-08-22 15:41:02 +02:00
|
|
|
size_t const bytes_written = ssl->out_left;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t const mtu = ssl_get_maximum_datagram_size(ssl);
|
2018-08-06 12:33:50 +02:00
|
|
|
|
|
|
|
/* Double-check that the write-index hasn't gone
|
|
|
|
* past what we can transmit in a single datagram. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (bytes_written > mtu) {
|
2018-08-06 12:33:50 +02:00
|
|
|
/* Should never happen... */
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2018-08-06 12:33:50 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return (int) (mtu - bytes_written);
|
2018-08-06 12:33:50 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_get_remaining_payload_in_datagram(mbedtls_ssl_context const *ssl)
|
2018-08-06 12:33:50 +02:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-08-06 12:33:50 +02:00
|
|
|
size_t remaining, expansion;
|
2018-10-11 13:20:19 +02:00
|
|
|
size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
|
2018-08-06 12:33:50 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
|
2018-08-06 12:33:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (max_len > mfl) {
|
2018-08-06 12:33:50 +02:00
|
|
|
max_len = mfl;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-08-24 11:47:29 +02:00
|
|
|
|
|
|
|
/* By the standard (RFC 6066 Sect. 4), the MFL extension
|
|
|
|
* only limits the maximum record payload size, so in theory
|
|
|
|
* we would be allowed to pack multiple records of payload size
|
|
|
|
* MFL into a single datagram. However, this would mean that there's
|
|
|
|
* no way to explicitly communicate MTU restrictions to the peer.
|
|
|
|
*
|
|
|
|
* The following reduction of max_len makes sure that we never
|
|
|
|
* write datagrams larger than MFL + Record Expansion Overhead.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (max_len <= ssl->out_left) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-08-24 11:47:29 +02:00
|
|
|
|
|
|
|
max_len -= ssl->out_left;
|
2018-08-06 12:33:50 +02:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_get_remaining_space_in_datagram(ssl);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2018-08-06 12:33:50 +02:00
|
|
|
remaining = (size_t) ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_get_record_expansion(ssl);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2018-08-06 12:33:50 +02:00
|
|
|
expansion = (size_t) ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (remaining <= expansion) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-08-06 12:33:50 +02:00
|
|
|
|
|
|
|
remaining -= expansion;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (remaining >= max_len) {
|
2018-08-06 12:33:50 +02:00
|
|
|
remaining = max_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-08-06 12:33:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return (int) remaining;
|
2018-08-06 12:33:50 +02:00
|
|
|
}
|
|
|
|
|
2014-09-30 22:21:31 +02:00
|
|
|
/*
|
|
|
|
* Double the retransmit timeout value, within the allowed range,
|
|
|
|
* returning -1 if the maximum value has already been reached.
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_double_retransmit_timeout(mbedtls_ssl_context *ssl)
|
2014-09-30 22:21:31 +02:00
|
|
|
{
|
|
|
|
uint32_t new_timeout;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-09-30 22:21:31 +02:00
|
|
|
|
2018-08-20 09:34:02 +02:00
|
|
|
/* Implement the final paragraph of RFC 6347 section 4.1.1.1
|
|
|
|
* in the following way: after the initial transmission and a first
|
|
|
|
* retransmission, back off to a temporary estimated MTU of 508 bytes.
|
|
|
|
* This value is guaranteed to be deliverable (if not guaranteed to be
|
|
|
|
* delivered) of any compliant IPv4 (and IPv6) network, and should work
|
|
|
|
* on most non-IP stacks too. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min) {
|
2018-08-20 09:34:02 +02:00
|
|
|
ssl->handshake->mtu = 508;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("mtu autoreduction to %d bytes", ssl->handshake->mtu));
|
2018-10-05 14:06:01 +02:00
|
|
|
}
|
2018-08-20 09:34:02 +02:00
|
|
|
|
2014-09-30 22:21:31 +02:00
|
|
|
new_timeout = 2 * ssl->handshake->retransmit_timeout;
|
|
|
|
|
|
|
|
/* Avoid arithmetic overflow and range overflow */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (new_timeout < ssl->handshake->retransmit_timeout ||
|
|
|
|
new_timeout > ssl->conf->hs_timeout_max) {
|
2015-05-04 10:55:58 +02:00
|
|
|
new_timeout = ssl->conf->hs_timeout_max;
|
2014-09-30 22:21:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ssl->handshake->retransmit_timeout = new_timeout;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("update timeout value to %lu millisecs",
|
|
|
|
(unsigned long) ssl->handshake->retransmit_timeout));
|
2014-09-30 22:21:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2014-09-30 22:21:31 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void ssl_reset_retransmit_timeout(mbedtls_ssl_context *ssl)
|
2014-09-30 22:21:31 +02:00
|
|
|
{
|
2015-05-04 10:55:58 +02:00
|
|
|
ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("update timeout value to %lu millisecs",
|
|
|
|
(unsigned long) ssl->handshake->retransmit_timeout));
|
2014-09-30 22:21:31 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2014-09-30 22:21:31 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Encryption/decryption functions
|
|
|
|
*/
|
2017-06-29 18:09:42 +02:00
|
|
|
|
2021-12-08 16:57:54 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2020-05-28 17:15:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static size_t ssl_compute_padding_length(size_t len,
|
|
|
|
size_t granularity)
|
2020-05-28 17:15:19 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return (granularity - (len + 1) % granularity) % granularity;
|
2020-05-28 17:15:19 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 13:20:03 +02:00
|
|
|
/* This functions transforms a (D)TLS plaintext fragment and a record content
|
|
|
|
* type into an instance of the (D)TLSInnerPlaintext structure. This is used
|
|
|
|
* in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect
|
|
|
|
* a record's content type.
|
2020-02-05 15:44:10 +01:00
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* opaque content[DTLSPlaintext.length];
|
|
|
|
* ContentType real_type;
|
|
|
|
* uint8 zeros[length_of_padding];
|
2020-05-04 13:20:03 +02:00
|
|
|
* } (D)TLSInnerPlaintext;
|
2020-02-05 15:44:10 +01:00
|
|
|
*
|
|
|
|
* Input:
|
|
|
|
* - `content`: The beginning of the buffer holding the
|
|
|
|
* plaintext to be wrapped.
|
|
|
|
* - `*content_size`: The length of the plaintext in Bytes.
|
|
|
|
* - `max_len`: The number of Bytes available starting from
|
|
|
|
* `content`. This must be `>= *content_size`.
|
|
|
|
* - `rec_type`: The desired record content type.
|
|
|
|
*
|
|
|
|
* Output:
|
2020-05-04 13:20:03 +02:00
|
|
|
* - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure.
|
|
|
|
* - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure.
|
2020-02-05 15:44:10 +01:00
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* - `0` on success.
|
|
|
|
* - A negative error code if `max_len` didn't offer enough space
|
|
|
|
* for the expansion.
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_build_inner_plaintext(unsigned char *content,
|
|
|
|
size_t *content_size,
|
|
|
|
size_t remaining,
|
|
|
|
uint8_t rec_type,
|
|
|
|
size_t pad)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
|
|
|
size_t len = *content_size;
|
2017-06-29 18:09:42 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Write real content type */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (remaining == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
content[len] = rec_type;
|
2020-02-05 15:44:10 +01:00
|
|
|
len++;
|
|
|
|
remaining--;
|
2012-09-13 16:23:06 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (remaining < pad) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memset(content + len, 0, pad);
|
2020-02-05 15:44:10 +01:00
|
|
|
len += pad;
|
|
|
|
remaining -= pad;
|
2012-09-13 16:23:06 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
*content_size = len;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2012-09-13 16:23:06 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 13:20:03 +02:00
|
|
|
/* This function parses a (D)TLSInnerPlaintext structure.
|
|
|
|
* See ssl_build_inner_plaintext() for details. */
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_parse_inner_plaintext(unsigned char const *content,
|
|
|
|
size_t *content_size,
|
|
|
|
uint8_t *rec_type)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
size_t remaining = *content_size;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Determine length of padding by skipping zeroes from the back. */
|
2023-01-11 14:50:10 +01:00
|
|
|
do {
|
|
|
|
if (remaining == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
remaining--;
|
2023-01-11 14:50:10 +01:00
|
|
|
} while (content[remaining] == 0);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
*content_size = remaining;
|
2023-01-11 14:50:10 +01:00
|
|
|
*rec_type = content[remaining];
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2021-12-08 16:57:54 +01:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID || MBEDTLS_SSL_PROTO_TLS1_3 */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2021-10-12 09:22:33 +02:00
|
|
|
/* The size of the `add_data` structure depends on various
|
|
|
|
* factors, namely
|
|
|
|
*
|
|
|
|
* 1) CID functionality disabled
|
|
|
|
*
|
|
|
|
* additional_data =
|
|
|
|
* 8: seq_num +
|
|
|
|
* 1: type +
|
|
|
|
* 2: version +
|
|
|
|
* 2: length of inner plaintext +
|
|
|
|
*
|
|
|
|
* size = 13 bytes
|
|
|
|
*
|
|
|
|
* 2) CID functionality based on RFC 9146 enabled
|
|
|
|
*
|
|
|
|
* size = 8 + 1 + 1 + 1 + 2 + 2 + 6 + 2 + CID-length
|
|
|
|
* = 23 + CID-length
|
|
|
|
*
|
|
|
|
* 3) CID functionality based on legacy CID version
|
|
|
|
according to draft-ietf-tls-dtls-connection-id-05
|
|
|
|
* https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
|
|
|
|
*
|
|
|
|
* size = 13 + 1 + CID-length
|
|
|
|
*
|
|
|
|
* More information about the CID usage:
|
|
|
|
*
|
|
|
|
* Per Section 5.3 of draft-ietf-tls-dtls-connection-id-05 the
|
|
|
|
* size of the additional data structure is calculated as:
|
|
|
|
*
|
|
|
|
* additional_data =
|
|
|
|
* 8: seq_num +
|
|
|
|
* 1: tls12_cid +
|
|
|
|
* 2: DTLSCipherText.version +
|
|
|
|
* n: cid +
|
|
|
|
* 1: cid_length +
|
|
|
|
* 2: length_of_DTLSInnerPlaintext
|
|
|
|
*
|
|
|
|
* Per RFC 9146 the size of the add_data structure is calculated as:
|
|
|
|
*
|
|
|
|
* additional_data =
|
|
|
|
* 8: seq_num_placeholder +
|
|
|
|
* 1: tls12_cid +
|
|
|
|
* 1: cid_length +
|
|
|
|
* 1: tls12_cid +
|
|
|
|
* 2: DTLSCiphertext.version +
|
|
|
|
* 2: epoch +
|
|
|
|
* 6: sequence_number +
|
|
|
|
* n: cid +
|
|
|
|
* 2: length_of_DTLSInnerPlaintext
|
|
|
|
*
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static void ssl_extract_add_data_from_record(unsigned char *add_data,
|
|
|
|
size_t *add_data_len,
|
|
|
|
mbedtls_record *rec,
|
|
|
|
mbedtls_ssl_protocol_version
|
|
|
|
tls_version,
|
|
|
|
size_t taglen)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2021-10-12 09:22:33 +02:00
|
|
|
/* Several types of ciphers have been defined for use with TLS and DTLS,
|
|
|
|
* and the MAC calculations for those ciphers differ slightly. Further
|
|
|
|
* variants were added when the CID functionality was added with RFC 9146.
|
|
|
|
* This implementations also considers the use of a legacy version of the
|
|
|
|
* CID specification published in draft-ietf-tls-dtls-connection-id-05,
|
|
|
|
* which is used in deployments.
|
2020-02-05 15:44:10 +01:00
|
|
|
*
|
2021-10-12 09:22:33 +02:00
|
|
|
* We will distinguish between the non-CID and the CID cases below.
|
2020-02-05 15:44:10 +01:00
|
|
|
*
|
2021-10-12 09:22:33 +02:00
|
|
|
* --- Non-CID cases ---
|
2020-02-05 15:44:10 +01:00
|
|
|
*
|
2021-10-12 09:22:33 +02:00
|
|
|
* Quoting RFC 5246 (TLS 1.2):
|
2020-02-05 15:44:10 +01:00
|
|
|
*
|
2021-10-12 09:22:33 +02:00
|
|
|
* additional_data = seq_num + TLSCompressed.type +
|
|
|
|
* TLSCompressed.version + TLSCompressed.length;
|
2020-05-21 16:25:21 +02:00
|
|
|
*
|
|
|
|
* For TLS 1.3, the record sequence number is dropped from the AAD
|
|
|
|
* and encoded within the nonce of the AEAD operation instead.
|
2021-03-22 12:42:19 +01:00
|
|
|
* Moreover, the additional data involves the length of the TLS
|
|
|
|
* ciphertext, not the TLS plaintext as in earlier versions.
|
|
|
|
* Quoting RFC 8446 (TLS 1.3):
|
|
|
|
*
|
|
|
|
* additional_data = TLSCiphertext.opaque_type ||
|
|
|
|
* TLSCiphertext.legacy_record_version ||
|
|
|
|
* TLSCiphertext.length
|
|
|
|
*
|
|
|
|
* We pass the tag length to this function in order to compute the
|
|
|
|
* ciphertext length from the inner plaintext length rec->data_len via
|
|
|
|
*
|
|
|
|
* TLSCiphertext.length = TLSInnerPlaintext.length + taglen.
|
|
|
|
*
|
2021-10-12 09:22:33 +02:00
|
|
|
* --- CID cases ---
|
|
|
|
*
|
|
|
|
* RFC 9146 uses a common pattern when constructing the data
|
|
|
|
* passed into a MAC / AEAD cipher.
|
|
|
|
*
|
|
|
|
* Data concatenation for MACs used with block ciphers with
|
|
|
|
* Encrypt-then-MAC Processing (with CID):
|
|
|
|
*
|
|
|
|
* data = seq_num_placeholder +
|
|
|
|
* tls12_cid +
|
|
|
|
* cid_length +
|
|
|
|
* tls12_cid +
|
|
|
|
* DTLSCiphertext.version +
|
|
|
|
* epoch +
|
|
|
|
* sequence_number +
|
|
|
|
* cid +
|
|
|
|
* DTLSCiphertext.length +
|
|
|
|
* IV +
|
|
|
|
* ENC(content + padding + padding_length)
|
|
|
|
*
|
|
|
|
* Data concatenation for MACs used with block ciphers (with CID):
|
|
|
|
*
|
|
|
|
* data = seq_num_placeholder +
|
|
|
|
* tls12_cid +
|
|
|
|
* cid_length +
|
|
|
|
* tls12_cid +
|
|
|
|
* DTLSCiphertext.version +
|
|
|
|
* epoch +
|
|
|
|
* sequence_number +
|
|
|
|
* cid +
|
|
|
|
* length_of_DTLSInnerPlaintext +
|
|
|
|
* DTLSInnerPlaintext.content +
|
|
|
|
* DTLSInnerPlaintext.real_type +
|
|
|
|
* DTLSInnerPlaintext.zeros
|
|
|
|
*
|
|
|
|
* AEAD ciphers use the following additional data calculation (with CIDs):
|
|
|
|
*
|
|
|
|
* additional_data = seq_num_placeholder +
|
|
|
|
* tls12_cid +
|
|
|
|
* cid_length +
|
|
|
|
* tls12_cid +
|
|
|
|
* DTLSCiphertext.version +
|
|
|
|
* epoch +
|
|
|
|
* sequence_number +
|
|
|
|
* cid +
|
|
|
|
* length_of_DTLSInnerPlaintext
|
|
|
|
*
|
|
|
|
* Section 5.3 of draft-ietf-tls-dtls-connection-id-05 (for legacy CID use)
|
|
|
|
* defines the additional data calculation as follows:
|
|
|
|
*
|
|
|
|
* additional_data = seq_num +
|
|
|
|
* tls12_cid +
|
|
|
|
* DTLSCipherText.version +
|
|
|
|
* cid +
|
|
|
|
* cid_length +
|
|
|
|
* length_of_DTLSInnerPlaintext
|
2023-01-11 14:50:10 +01:00
|
|
|
*/
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-05-21 16:25:21 +02:00
|
|
|
unsigned char *cur = add_data;
|
2021-03-22 12:42:19 +01:00
|
|
|
size_t ad_len_field = rec->data_len;
|
2020-05-21 16:25:21 +02:00
|
|
|
|
2021-10-12 09:22:33 +02:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
|
|
|
|
MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 0
|
|
|
|
const unsigned char seq_num_placeholder[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
|
|
|
|
#endif
|
|
|
|
|
2021-12-08 16:57:54 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
|
2021-03-22 12:42:19 +01:00
|
|
|
/* In TLS 1.3, the AAD contains the length of the TLSCiphertext,
|
|
|
|
* which differs from the length of the TLSInnerPlaintext
|
|
|
|
* by the length of the authentication tag. */
|
|
|
|
ad_len_field += taglen;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2021-12-08 16:57:54 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
2020-05-21 16:25:21 +02:00
|
|
|
{
|
2022-03-14 17:34:51 +01:00
|
|
|
((void) tls_version);
|
2021-03-22 12:42:19 +01:00
|
|
|
((void) taglen);
|
2021-10-12 09:22:33 +02:00
|
|
|
|
2022-11-25 11:12:38 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 0
|
|
|
|
if (rec->cid_len != 0) {
|
2021-10-12 09:22:33 +02:00
|
|
|
// seq_num_placeholder
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(cur, seq_num_placeholder, sizeof(seq_num_placeholder));
|
|
|
|
cur += sizeof(seq_num_placeholder);
|
2021-10-12 09:22:33 +02:00
|
|
|
|
|
|
|
// tls12_cid type
|
|
|
|
*cur = rec->type;
|
|
|
|
cur++;
|
|
|
|
|
|
|
|
// cid_length
|
|
|
|
*cur = rec->cid_len;
|
|
|
|
cur++;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2022-11-25 11:12:38 +01:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
2021-10-12 09:22:33 +02:00
|
|
|
{
|
|
|
|
// epoch + sequence number
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(cur, rec->ctr, sizeof(rec->ctr));
|
|
|
|
cur += sizeof(rec->ctr);
|
2021-10-12 09:22:33 +02:00
|
|
|
}
|
2020-05-21 16:25:21 +02:00
|
|
|
}
|
|
|
|
|
2021-10-12 09:22:33 +02:00
|
|
|
// type
|
2020-05-21 16:25:21 +02:00
|
|
|
*cur = rec->type;
|
|
|
|
cur++;
|
|
|
|
|
2021-10-12 09:22:33 +02:00
|
|
|
// version
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(cur, rec->ver, sizeof(rec->ver));
|
|
|
|
cur += sizeof(rec->ver);
|
2015-03-24 18:08:19 +01:00
|
|
|
|
2021-10-12 09:22:33 +02:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
|
|
|
|
MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 1
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec->cid_len != 0) {
|
2021-10-12 09:22:33 +02:00
|
|
|
// CID
|
|
|
|
memcpy(cur, rec->cid, rec->cid_len);
|
2020-05-21 16:25:21 +02:00
|
|
|
cur += rec->cid_len;
|
|
|
|
|
2021-10-12 09:22:33 +02:00
|
|
|
// cid_length
|
2020-05-21 16:25:21 +02:00
|
|
|
*cur = rec->cid_len;
|
|
|
|
cur++;
|
|
|
|
|
2021-10-12 09:22:33 +02:00
|
|
|
// length of inner plaintext
|
|
|
|
MBEDTLS_PUT_UINT16_BE(ad_len_field, cur, 0);
|
|
|
|
cur += 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2021-10-12 09:22:33 +02:00
|
|
|
#elif defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
|
|
|
|
MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 0
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec->cid_len != 0) {
|
2021-10-12 09:22:33 +02:00
|
|
|
// epoch + sequence number
|
|
|
|
memcpy(cur, rec->ctr, sizeof(rec->ctr));
|
|
|
|
cur += sizeof(rec->ctr);
|
|
|
|
|
|
|
|
// CID
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(cur, rec->cid, rec->cid_len);
|
2021-10-12 09:22:33 +02:00
|
|
|
cur += rec->cid_len;
|
|
|
|
|
|
|
|
// length of inner plaintext
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(ad_len_field, cur, 0);
|
2020-05-21 16:25:21 +02:00
|
|
|
cur += 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(ad_len_field, cur, 0);
|
2020-05-21 16:25:21 +02:00
|
|
|
cur += 2;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2020-05-21 16:25:21 +02:00
|
|
|
|
|
|
|
*add_data_len = cur - add_data;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-10-11 11:54:42 +02:00
|
|
|
#if defined(MBEDTLS_SSL_HAVE_AEAD)
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2020-05-28 08:05:48 +02:00
|
|
|
static int ssl_transform_aead_dynamic_iv_is_explicit(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_transform const *transform)
|
2020-05-21 16:30:57 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return transform->ivlen != transform->fixed_ivlen;
|
2020-05-21 16:30:57 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 08:05:48 +02:00
|
|
|
/* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV )
|
|
|
|
*
|
|
|
|
* Concretely, this occurs in two variants:
|
|
|
|
*
|
|
|
|
* a) Fixed and dynamic IV lengths add up to total IV length, giving
|
|
|
|
* IV = fixed_iv || dynamic_iv
|
|
|
|
*
|
2020-06-04 14:31:46 +02:00
|
|
|
* This variant is used in TLS 1.2 when used with GCM or CCM.
|
|
|
|
*
|
2020-05-28 08:05:48 +02:00
|
|
|
* b) Fixed IV lengths matches total IV length, giving
|
|
|
|
* IV = fixed_iv XOR ( 0 || dynamic_iv )
|
2020-06-04 14:31:46 +02:00
|
|
|
*
|
|
|
|
* This variant occurs in TLS 1.3 and for TLS 1.2 when using ChaChaPoly.
|
|
|
|
*
|
|
|
|
* See also the documentation of mbedtls_ssl_transform.
|
2020-06-04 14:33:08 +02:00
|
|
|
*
|
|
|
|
* This function has the precondition that
|
|
|
|
*
|
|
|
|
* dst_iv_len >= max( fixed_iv_len, dynamic_iv_len )
|
|
|
|
*
|
|
|
|
* which has to be ensured by the caller. If this precondition
|
|
|
|
* violated, the behavior of this function is undefined.
|
2020-05-21 16:30:57 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static void ssl_build_record_nonce(unsigned char *dst_iv,
|
|
|
|
size_t dst_iv_len,
|
|
|
|
unsigned char const *fixed_iv,
|
|
|
|
size_t fixed_iv_len,
|
|
|
|
unsigned char const *dynamic_iv,
|
|
|
|
size_t dynamic_iv_len)
|
2020-05-21 16:30:57 +02:00
|
|
|
{
|
|
|
|
/* Start with Fixed IV || 0 */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(dst_iv, 0, dst_iv_len);
|
|
|
|
memcpy(dst_iv, fixed_iv, fixed_iv_len);
|
2020-05-21 16:30:57 +02:00
|
|
|
|
2020-05-28 08:05:48 +02:00
|
|
|
dst_iv += dst_iv_len - dynamic_iv_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_xor(dst_iv, dst_iv, dynamic_iv, dynamic_iv_len);
|
2020-05-21 16:30:57 +02:00
|
|
|
}
|
2023-10-11 11:54:42 +02:00
|
|
|
#endif /* MBEDTLS_SSL_HAVE_AEAD */
|
2020-05-21 16:30:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_encrypt_buf(mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_ssl_transform *transform,
|
|
|
|
mbedtls_record *rec,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng)
|
2019-08-17 10:30:28 +02:00
|
|
|
{
|
2022-03-30 10:58:01 +02:00
|
|
|
mbedtls_ssl_mode_t ssl_mode;
|
2020-02-05 15:44:10 +01:00
|
|
|
int auth_done = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char *data;
|
2021-10-12 09:22:33 +02:00
|
|
|
/* For an explanation of the additional data length see
|
2023-01-11 14:50:10 +01:00
|
|
|
* the description of ssl_extract_add_data_from_record().
|
|
|
|
*/
|
2021-10-12 09:22:33 +02:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
|
|
|
unsigned char add_data[23 + MBEDTLS_SSL_CID_OUT_LEN_MAX];
|
|
|
|
#else
|
|
|
|
unsigned char add_data[13];
|
|
|
|
#endif
|
2020-02-05 15:44:10 +01:00
|
|
|
size_t add_data_len;
|
|
|
|
size_t post_avail;
|
2019-08-17 10:30:28 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* The SSL context is only used for debugging purposes! */
|
|
|
|
#if !defined(MBEDTLS_DEBUG_C)
|
|
|
|
ssl = NULL; /* make sure we don't use it except for debug */
|
|
|
|
((void) ssl);
|
|
|
|
#endif
|
2019-08-17 10:30:28 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* The PRNG is used for dynamic IV generation that's used
|
2021-05-12 17:49:18 +02:00
|
|
|
* for CBC transformations in TLS 1.2. */
|
2023-01-11 14:50:10 +01:00
|
|
|
#if !(defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
|
|
|
|
defined(MBEDTLS_SSL_PROTO_TLS1_2))
|
2020-02-05 15:44:10 +01:00
|
|
|
((void) f_rng);
|
|
|
|
((void) p_rng);
|
|
|
|
#endif
|
2019-01-14 09:51:11 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> encrypt buf"));
|
2019-01-14 09:51:11 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (transform == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("no transform provided to encrypt_buf"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2019-01-14 09:51:11 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec == NULL
|
2020-02-05 15:44:10 +01:00
|
|
|
|| rec->buf == NULL
|
|
|
|
|| rec->buf_len < rec->data_offset
|
|
|
|
|| rec->buf_len - rec->data_offset < rec->data_len
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
|
|
|
|| rec->cid_len != 0
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("bad record structure provided to encrypt_buf"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2019-01-14 11:37:13 +01:00
|
|
|
}
|
2019-01-14 09:51:11 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_mode = mbedtls_ssl_get_mode_from_transform(transform);
|
2022-03-30 10:58:01 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
data = rec->buf + rec->data_offset;
|
2023-01-11 14:50:10 +01:00
|
|
|
post_avail = rec->buf_len - (rec->data_len + rec->data_offset);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "before encrypt: output payload",
|
|
|
|
data, rec->data_len);
|
2015-03-26 11:11:49 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Record content %" MBEDTLS_PRINTF_SIZET
|
|
|
|
" too large, maximum %" MBEDTLS_PRINTF_SIZET,
|
|
|
|
rec->data_len,
|
|
|
|
(size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2019-05-07 17:31:49 +02:00
|
|
|
}
|
2012-04-11 14:09:53 +02:00
|
|
|
|
2020-05-20 14:58:58 +02:00
|
|
|
/* The following two code paths implement the (D)TLSInnerPlaintext
|
|
|
|
* structure present in TLS 1.3 and DTLS 1.2 + CID.
|
|
|
|
*
|
|
|
|
* See ssl_build_inner_plaintext() for more information.
|
|
|
|
*
|
|
|
|
* Note that this changes `rec->data_len`, and hence
|
|
|
|
* `post_avail` needs to be recalculated afterwards.
|
|
|
|
*
|
|
|
|
* Note also that the two code paths cannot occur simultaneously
|
|
|
|
* since they apply to different versions of the protocol. There
|
|
|
|
* is hence no risk of double-addition of the inner plaintext.
|
|
|
|
*/
|
2021-12-08 16:57:54 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (transform->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
|
2020-05-28 17:15:19 +02:00
|
|
|
size_t padding =
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_compute_padding_length(rec->data_len,
|
|
|
|
MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY);
|
|
|
|
if (ssl_build_inner_plaintext(data,
|
|
|
|
&rec->data_len,
|
|
|
|
post_avail,
|
|
|
|
rec->type,
|
|
|
|
padding) != 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
|
2020-05-04 13:30:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
|
|
|
|
}
|
2021-12-08 16:57:54 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
2020-05-04 13:30:04 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
2012-04-11 14:09:53 +02:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Add CID information
|
2012-04-11 14:09:53 +02:00
|
|
|
*/
|
2020-02-05 15:44:10 +01:00
|
|
|
rec->cid_len = transform->out_cid_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(rec->cid, transform->out_cid, transform->out_cid_len);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "CID", rec->cid, rec->cid_len);
|
2012-04-11 14:09:53 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec->cid_len != 0) {
|
2020-05-28 17:15:19 +02:00
|
|
|
size_t padding =
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_compute_padding_length(rec->data_len,
|
|
|
|
MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY);
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Wrap plaintext into DTLSInnerPlaintext structure.
|
2020-05-04 13:20:03 +02:00
|
|
|
* See ssl_build_inner_plaintext() for more information.
|
2020-02-05 15:44:10 +01:00
|
|
|
*
|
|
|
|
* Note that this changes `rec->data_len`, and hence
|
|
|
|
* `post_avail` needs to be recalculated afterwards.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_build_inner_plaintext(data,
|
|
|
|
&rec->data_len,
|
|
|
|
post_avail,
|
|
|
|
rec->type,
|
|
|
|
padding) != 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2012-04-11 14:09:53 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
rec->type = MBEDTLS_SSL_MSG_CID;
|
2012-04-11 14:09:53 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
2012-04-11 14:09:53 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
post_avail = rec->buf_len - (rec->data_len + rec->data_offset);
|
2013-08-27 21:19:20 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Add MAC before if needed
|
|
|
|
*/
|
2020-11-30 09:54:23 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
|
|
|
|
ssl_mode == MBEDTLS_SSL_MODE_CBC) {
|
|
|
|
if (post_avail < transform->maclen) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
|
|
|
|
return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2021-05-12 17:49:18 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2021-07-06 13:42:11 +02:00
|
|
|
unsigned char mac[MBEDTLS_SSL_MAC_ADD];
|
2021-12-10 21:35:10 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-02-23 09:30:33 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t sign_mac_length = 0;
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2013-08-27 21:19:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
|
|
|
|
transform->tls_version,
|
|
|
|
transform->taglen);
|
2013-08-27 21:19:20 +02:00
|
|
|
|
2022-02-23 09:30:33 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_sign_setup(&operation, transform->psa_mac_enc,
|
|
|
|
transform->psa_mac_alg);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-02-23 09:30:33 +01:00
|
|
|
goto hmac_failed_etm_disabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-23 09:30:33 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_update(&operation, add_data, add_data_len);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-02-23 09:30:33 +01:00
|
|
|
goto hmac_failed_etm_disabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-23 09:30:33 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_update(&operation, data, rec->data_len);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-02-23 09:30:33 +01:00
|
|
|
goto hmac_failed_etm_disabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-23 09:30:33 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_sign_finish(&operation, mac, MBEDTLS_SSL_MAC_ADD,
|
|
|
|
&sign_mac_length);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-02-23 09:30:33 +01:00
|
|
|
goto hmac_failed_etm_disabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-23 09:30:33 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, add_data,
|
|
|
|
add_data_len);
|
|
|
|
if (ret != 0) {
|
2021-12-10 21:35:10 +01:00
|
|
|
goto hmac_failed_etm_disabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, data, rec->data_len);
|
|
|
|
if (ret != 0) {
|
2021-12-10 21:35:10 +01:00
|
|
|
goto hmac_failed_etm_disabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
ret = mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac);
|
|
|
|
if (ret != 0) {
|
2021-12-10 21:35:10 +01:00
|
|
|
goto hmac_failed_etm_disabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
ret = mbedtls_md_hmac_reset(&transform->md_ctx_enc);
|
|
|
|
if (ret != 0) {
|
2021-12-10 21:35:10 +01:00
|
|
|
goto hmac_failed_etm_disabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-23 09:30:33 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2012-11-24 11:26:46 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(data + rec->data_len, mac, transform->maclen);
|
2012-11-24 11:26:46 +01:00
|
|
|
#endif
|
2012-04-11 14:09:53 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "computed mac", data + rec->data_len,
|
|
|
|
transform->maclen);
|
2018-10-23 16:26:22 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
rec->data_len += transform->maclen;
|
|
|
|
post_avail -= transform->maclen;
|
|
|
|
auth_done++;
|
2021-12-10 21:35:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
hmac_failed_etm_disabled:
|
|
|
|
mbedtls_platform_zeroize(mac, transform->maclen);
|
2022-02-23 09:30:33 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_abort(&operation);
|
|
|
|
if (ret == 0 && status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-03-02 15:14:07 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_hmac_xxx", ret);
|
|
|
|
return ret;
|
2021-12-10 21:35:10 +01:00
|
|
|
}
|
2018-10-23 16:26:22 +02:00
|
|
|
}
|
2020-11-30 09:54:23 +01:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
|
2018-10-23 16:26:22 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Encrypt
|
|
|
|
*/
|
2021-03-22 14:01:27 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
|
|
|
|
"including %d bytes of padding",
|
|
|
|
rec->data_len, 0));
|
2018-10-23 16:26:22 +02:00
|
|
|
|
2022-02-07 10:52:47 +01:00
|
|
|
/* The only supported stream cipher is "NULL",
|
|
|
|
* so there's nothing to do here.*/
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2021-03-22 14:01:27 +01:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
|
2019-05-12 13:54:30 +02:00
|
|
|
|
2023-10-11 11:54:42 +02:00
|
|
|
#if defined(MBEDTLS_SSL_HAVE_AEAD)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
|
2020-02-05 15:44:10 +01:00
|
|
|
unsigned char iv[12];
|
2020-05-21 16:30:57 +02:00
|
|
|
unsigned char *dynamic_iv;
|
|
|
|
size_t dynamic_iv_len;
|
2020-05-28 08:05:48 +02:00
|
|
|
int dynamic_iv_is_explicit =
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_transform_aead_dynamic_iv_is_explicit(transform);
|
2022-01-13 14:28:44 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2022-02-03 08:55:33 +01:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-01-13 14:28:44 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-02-03 09:54:34 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-05-15 13:54:22 +02:00
|
|
|
|
2020-05-21 16:26:39 +02:00
|
|
|
/* Check that there's space for the authentication tag. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (post_avail < transform->taglen) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
|
|
|
|
return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2019-05-12 13:54:30 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
2020-05-21 16:30:57 +02:00
|
|
|
* Build nonce for AEAD encryption.
|
|
|
|
*
|
|
|
|
* Note: In the case of CCM and GCM in TLS 1.2, the dynamic
|
|
|
|
* part of the IV is prepended to the ciphertext and
|
|
|
|
* can be chosen freely - in particular, it need not
|
|
|
|
* agree with the record sequence number.
|
|
|
|
* However, since ChaChaPoly as well as all AEAD modes
|
|
|
|
* in TLS 1.3 use the record sequence number as the
|
|
|
|
* dynamic part of the nonce, we uniformly use the
|
|
|
|
* record sequence number here in all cases.
|
2020-02-05 15:44:10 +01:00
|
|
|
*/
|
2020-05-21 16:30:57 +02:00
|
|
|
dynamic_iv = rec->ctr;
|
2023-01-11 14:50:10 +01:00
|
|
|
dynamic_iv_len = sizeof(rec->ctr);
|
2020-05-21 16:30:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_build_record_nonce(iv, sizeof(iv),
|
|
|
|
transform->iv_enc,
|
|
|
|
transform->fixed_ivlen,
|
|
|
|
dynamic_iv,
|
|
|
|
dynamic_iv_len);
|
2019-05-06 13:48:22 +02:00
|
|
|
|
2020-05-21 16:25:21 +02:00
|
|
|
/*
|
|
|
|
* Build additional data for AEAD encryption.
|
|
|
|
* This depends on the TLS version.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
|
|
|
|
transform->tls_version,
|
|
|
|
transform->taglen);
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "IV used (internal)",
|
|
|
|
iv, transform->ivlen);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "IV used (transmitted)",
|
|
|
|
dynamic_iv,
|
|
|
|
dynamic_iv_is_explicit ? dynamic_iv_len : 0);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "additional data used for AEAD",
|
|
|
|
add_data, add_data_len);
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
|
|
|
|
"including 0 bytes of padding",
|
|
|
|
rec->data_len));
|
2019-07-09 12:54:17 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Encrypt and authenticate
|
|
|
|
*/
|
2022-01-13 14:28:44 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_aead_encrypt(transform->psa_key_enc,
|
|
|
|
transform->psa_alg,
|
|
|
|
iv, transform->ivlen,
|
|
|
|
add_data, add_data_len,
|
|
|
|
data, rec->data_len,
|
|
|
|
data, rec->buf_len - (data - rec->buf),
|
|
|
|
&rec->data_len);
|
|
|
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_encrypt_buf", ret);
|
|
|
|
return ret;
|
2022-02-03 09:54:34 +01:00
|
|
|
}
|
2022-01-13 14:28:44 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_cipher_auth_encrypt_ext(&transform->cipher_ctx_enc,
|
|
|
|
iv, transform->ivlen,
|
|
|
|
add_data, add_data_len,
|
|
|
|
data, rec->data_len, /* src */
|
|
|
|
data, rec->buf_len - (data - rec->buf), /* dst */
|
|
|
|
&rec->data_len,
|
|
|
|
transform->taglen)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_encrypt_ext", ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-01-13 14:28:44 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "after encrypt: tag",
|
|
|
|
data + rec->data_len - transform->taglen,
|
|
|
|
transform->taglen);
|
2020-05-21 16:30:57 +02:00
|
|
|
/* Account for authentication tag. */
|
|
|
|
post_avail -= transform->taglen;
|
2013-01-07 18:20:04 +01:00
|
|
|
|
2020-05-21 16:26:39 +02:00
|
|
|
/*
|
2020-05-21 16:30:57 +02:00
|
|
|
* Prefix record content with dynamic IV in case it is explicit.
|
2020-05-21 16:26:39 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (dynamic_iv_is_explicit != 0) {
|
|
|
|
if (rec->data_offset < dynamic_iv_len) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
|
|
|
|
return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
|
2020-05-21 16:30:57 +02:00
|
|
|
}
|
2020-05-21 16:26:39 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(data - dynamic_iv_len, dynamic_iv, dynamic_iv_len);
|
2020-05-21 16:30:57 +02:00
|
|
|
rec->data_offset -= dynamic_iv_len;
|
|
|
|
rec->data_len += dynamic_iv_len;
|
|
|
|
}
|
2013-01-07 18:20:04 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
auth_done++;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2023-10-11 11:54:42 +02:00
|
|
|
#endif /* MBEDTLS_SSL_HAVE_AEAD */
|
2020-07-09 12:11:39 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_mode == MBEDTLS_SSL_MODE_CBC ||
|
|
|
|
ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t padlen, i;
|
|
|
|
size_t olen;
|
2022-01-13 14:28:44 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2022-02-03 08:55:33 +01:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-01-13 14:28:44 +01:00
|
|
|
size_t part_len;
|
|
|
|
psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2019-04-30 14:52:29 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Currently we're always using minimal padding
|
|
|
|
* (up to 255 bytes would be allowed). */
|
2023-01-11 14:50:10 +01:00
|
|
|
padlen = transform->ivlen - (rec->data_len + 1) % transform->ivlen;
|
|
|
|
if (padlen == transform->ivlen) {
|
2020-02-05 15:44:10 +01:00
|
|
|
padlen = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-05-15 11:21:55 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Check there's enough space in the buffer for the padding. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (post_avail < padlen + 1) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
|
|
|
|
return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2019-04-26 17:22:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i <= padlen; i++) {
|
2020-02-05 15:44:10 +01:00
|
|
|
data[rec->data_len + i] = (unsigned char) padlen;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
rec->data_len += padlen + 1;
|
|
|
|
post_avail -= padlen + 1;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2021-05-12 17:49:18 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
2021-05-13 19:14:19 +02:00
|
|
|
* Prepend per-record IV for block cipher in TLS v1.2 as per
|
2020-02-05 15:44:10 +01:00
|
|
|
* Method 1 (6.2.3.2. in RFC4346 and RFC5246)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (f_rng == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("No PRNG provided to encrypt_record routine"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2021-07-06 13:42:11 +02:00
|
|
|
}
|
2014-06-18 15:34:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec->data_offset < transform->ivlen) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
|
|
|
|
return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
|
2021-07-06 13:42:11 +02:00
|
|
|
}
|
2018-06-18 11:16:43 +02:00
|
|
|
|
2021-07-06 13:42:11 +02:00
|
|
|
/*
|
|
|
|
* Generate IV
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = f_rng(p_rng, transform->iv_enc, transform->ivlen);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2014-06-18 15:34:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(data - transform->ivlen, transform->iv_enc, transform->ivlen);
|
2021-05-12 17:49:18 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2018-06-18 11:16:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
|
|
|
|
"including %"
|
|
|
|
MBEDTLS_PRINTF_SIZET
|
|
|
|
" bytes of IV and %" MBEDTLS_PRINTF_SIZET " bytes of padding",
|
|
|
|
rec->data_len, transform->ivlen,
|
|
|
|
padlen + 1));
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2022-01-13 14:28:44 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_encrypt_setup(&cipher_op,
|
|
|
|
transform->psa_key_enc, transform->psa_alg);
|
2022-01-13 14:28:44 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_encrypt_setup", ret);
|
|
|
|
return ret;
|
2022-02-03 09:54:34 +01:00
|
|
|
}
|
2022-01-13 14:28:44 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_set_iv(&cipher_op, transform->iv_enc, transform->ivlen);
|
2022-01-13 14:28:44 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_set_iv", ret);
|
|
|
|
return ret;
|
2022-02-03 09:54:34 +01:00
|
|
|
|
|
|
|
}
|
2022-01-13 14:28:44 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_update(&cipher_op,
|
|
|
|
data, rec->data_len,
|
|
|
|
data, rec->data_len, &olen);
|
2022-01-13 14:28:44 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_update", ret);
|
|
|
|
return ret;
|
2022-02-03 09:54:34 +01:00
|
|
|
|
|
|
|
}
|
2022-01-13 14:28:44 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_finish(&cipher_op,
|
|
|
|
data + olen, rec->data_len - olen,
|
|
|
|
&part_len);
|
2022-01-13 14:28:44 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_finish", ret);
|
|
|
|
return ret;
|
2022-02-03 09:54:34 +01:00
|
|
|
|
|
|
|
}
|
2022-01-13 14:28:44 +01:00
|
|
|
|
|
|
|
olen += part_len;
|
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_enc,
|
|
|
|
transform->iv_enc,
|
|
|
|
transform->ivlen,
|
|
|
|
data, rec->data_len,
|
|
|
|
data, &olen)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret);
|
|
|
|
return ret;
|
2014-06-18 15:34:40 +02:00
|
|
|
}
|
2022-01-13 14:28:44 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2012-02-06 17:45:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec->data_len != olen) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2017-11-20 17:36:41 +01:00
|
|
|
|
2021-05-12 17:49:18 +02:00
|
|
|
data -= transform->ivlen;
|
|
|
|
rec->data_offset -= transform->ivlen;
|
|
|
|
rec->data_len += transform->ivlen;
|
2012-02-06 17:45:10 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (auth_done == 0) {
|
2020-02-05 15:44:10 +01:00
|
|
|
unsigned char mac[MBEDTLS_SSL_MAC_ADD];
|
2022-02-23 09:30:33 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
|
|
|
size_t sign_mac_length = 0;
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2021-10-12 09:22:33 +02:00
|
|
|
/* MAC(MAC_write_key, add_data, IV, ENC(content + padding + padding_length))
|
2014-06-18 16:06:02 +02:00
|
|
|
*/
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (post_avail < transform->maclen) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
|
|
|
|
return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
|
2014-06-18 16:06:02 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_extract_add_data_from_record(add_data, &add_data_len,
|
|
|
|
rec, transform->tls_version,
|
|
|
|
transform->taglen);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac"));
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data,
|
|
|
|
add_data_len);
|
2022-02-23 09:30:33 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_sign_setup(&operation, transform->psa_mac_enc,
|
|
|
|
transform->psa_mac_alg);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-02-23 09:30:33 +01:00
|
|
|
goto hmac_failed_etm_enabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-23 09:30:33 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_update(&operation, add_data, add_data_len);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-02-23 09:30:33 +01:00
|
|
|
goto hmac_failed_etm_enabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-23 09:30:33 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_update(&operation, data, rec->data_len);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-02-23 09:30:33 +01:00
|
|
|
goto hmac_failed_etm_enabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-23 09:30:33 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_sign_finish(&operation, mac, MBEDTLS_SSL_MAC_ADD,
|
|
|
|
&sign_mac_length);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-02-23 09:30:33 +01:00
|
|
|
goto hmac_failed_etm_enabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-23 09:30:33 +01:00
|
|
|
#else
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, add_data,
|
|
|
|
add_data_len);
|
|
|
|
if (ret != 0) {
|
2021-12-10 21:35:10 +01:00
|
|
|
goto hmac_failed_etm_enabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
ret = mbedtls_md_hmac_update(&transform->md_ctx_enc,
|
|
|
|
data, rec->data_len);
|
|
|
|
if (ret != 0) {
|
2021-12-10 21:35:10 +01:00
|
|
|
goto hmac_failed_etm_enabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
ret = mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac);
|
|
|
|
if (ret != 0) {
|
2021-12-10 21:35:10 +01:00
|
|
|
goto hmac_failed_etm_enabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
ret = mbedtls_md_hmac_reset(&transform->md_ctx_enc);
|
|
|
|
if (ret != 0) {
|
2021-12-10 21:35:10 +01:00
|
|
|
goto hmac_failed_etm_enabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-23 09:30:33 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(data + rec->data_len, mac, transform->maclen);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
rec->data_len += transform->maclen;
|
|
|
|
post_avail -= transform->maclen;
|
|
|
|
auth_done++;
|
2021-12-10 21:35:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
hmac_failed_etm_enabled:
|
|
|
|
mbedtls_platform_zeroize(mac, transform->maclen);
|
2022-02-23 09:30:33 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_abort(&operation);
|
|
|
|
if (ret == 0 && status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-03-02 15:14:07 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "HMAC calculation failed", ret);
|
|
|
|
return ret;
|
2021-12-10 21:35:10 +01:00
|
|
|
}
|
2013-01-07 18:20:04 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2020-07-09 12:11:39 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
|
2018-01-03 16:32:31 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2018-01-03 16:32:31 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Make extra sure authentication was performed, exactly once */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (auth_done != 1) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= encrypt buf"));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl,
|
|
|
|
mbedtls_ssl_transform *transform,
|
|
|
|
mbedtls_record *rec)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2023-10-11 11:54:42 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) || defined(MBEDTLS_SSL_HAVE_AEAD)
|
2020-02-05 15:44:10 +01:00
|
|
|
size_t olen;
|
2023-10-11 11:54:42 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC || MBEDTLS_SSL_HAVE_AEAD */
|
2022-03-30 10:58:01 +02:00
|
|
|
mbedtls_ssl_mode_t ssl_mode;
|
2022-02-01 14:52:19 +01:00
|
|
|
int ret;
|
2022-02-03 09:54:34 +01:00
|
|
|
|
2022-02-01 14:52:19 +01:00
|
|
|
int auth_done = 0;
|
2020-11-30 09:54:23 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
|
2023-09-12 17:29:39 +02:00
|
|
|
size_t padlen = 0;
|
|
|
|
mbedtls_ct_condition_t correct = MBEDTLS_CT_TRUE;
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char *data;
|
2021-10-12 09:22:33 +02:00
|
|
|
/* For an explanation of the additional data length see
|
2023-01-11 14:50:10 +01:00
|
|
|
* the description of ssl_extract_add_data_from_record().
|
|
|
|
*/
|
2021-10-12 09:22:33 +02:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
|
|
|
unsigned char add_data[23 + MBEDTLS_SSL_CID_IN_LEN_MAX];
|
|
|
|
#else
|
|
|
|
unsigned char add_data[13];
|
|
|
|
#endif
|
2020-02-05 15:44:10 +01:00
|
|
|
size_t add_data_len;
|
2014-01-18 18:22:55 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if !defined(MBEDTLS_DEBUG_C)
|
|
|
|
ssl = NULL; /* make sure we don't use it except for debug */
|
|
|
|
((void) ssl);
|
2013-08-27 21:19:20 +02:00
|
|
|
#endif
|
2013-01-07 18:20:04 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> decrypt buf"));
|
|
|
|
if (rec == NULL ||
|
2020-02-05 15:44:10 +01:00
|
|
|
rec->buf == NULL ||
|
|
|
|
rec->buf_len < rec->data_offset ||
|
2023-01-11 14:50:10 +01:00
|
|
|
rec->buf_len - rec->data_offset < rec->data_len) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("bad record structure provided to decrypt_buf"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2012-05-08 11:17:57 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
data = rec->buf + rec->data_offset;
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_mode = mbedtls_ssl_get_mode_from_transform(transform);
|
2019-05-07 17:33:40 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
|
|
|
/*
|
|
|
|
* Match record's CID with incoming CID.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec->cid_len != transform->in_cid_len ||
|
|
|
|
memcmp(rec->cid, transform->in_cid, rec->cid_len) != 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_UNEXPECTED_CID;
|
2019-05-07 17:33:40 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
2018-11-17 23:27:38 +01:00
|
|
|
|
2021-03-22 14:01:27 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
|
2023-09-18 14:08:11 +02:00
|
|
|
if (rec->data_len < transform->maclen) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1,
|
|
|
|
("Record too short for MAC:"
|
|
|
|
" %" MBEDTLS_PRINTF_SIZET " < %" MBEDTLS_PRINTF_SIZET,
|
|
|
|
rec->data_len, transform->maclen));
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_MAC;
|
|
|
|
}
|
|
|
|
|
2022-02-07 10:52:47 +01:00
|
|
|
/* The only supported stream cipher is "NULL",
|
2023-09-18 14:08:11 +02:00
|
|
|
* so there's no encryption to do here.*/
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2021-03-22 14:01:27 +01:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
|
2023-10-11 11:54:42 +02:00
|
|
|
#if defined(MBEDTLS_SSL_HAVE_AEAD)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
|
2020-02-05 15:44:10 +01:00
|
|
|
unsigned char iv[12];
|
2020-05-21 16:30:57 +02:00
|
|
|
unsigned char *dynamic_iv;
|
|
|
|
size_t dynamic_iv_len;
|
2022-01-13 14:50:15 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2022-02-03 08:55:33 +01:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-01-13 14:50:15 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2013-08-31 17:25:14 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
2020-05-21 16:30:57 +02:00
|
|
|
* Extract dynamic part of nonce for AEAD decryption.
|
|
|
|
*
|
|
|
|
* Note: In the case of CCM and GCM in TLS 1.2, the dynamic
|
|
|
|
* part of the IV is prepended to the ciphertext and
|
|
|
|
* can be chosen freely - in particular, it need not
|
|
|
|
* agree with the record sequence number.
|
2020-02-05 15:44:10 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
dynamic_iv_len = sizeof(rec->ctr);
|
|
|
|
if (ssl_transform_aead_dynamic_iv_is_explicit(transform) == 1) {
|
|
|
|
if (rec->data_len < dynamic_iv_len) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
|
|
|
|
" ) < explicit_iv_len (%" MBEDTLS_PRINTF_SIZET ") ",
|
|
|
|
rec->data_len,
|
|
|
|
dynamic_iv_len));
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_MAC;
|
2020-05-21 16:30:57 +02:00
|
|
|
}
|
|
|
|
dynamic_iv = data;
|
2013-10-25 18:42:44 +02:00
|
|
|
|
2020-05-21 16:30:57 +02:00
|
|
|
data += dynamic_iv_len;
|
|
|
|
rec->data_offset += dynamic_iv_len;
|
|
|
|
rec->data_len -= dynamic_iv_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2020-05-28 08:05:48 +02:00
|
|
|
dynamic_iv = rec->ctr;
|
|
|
|
}
|
2020-05-21 16:30:57 +02:00
|
|
|
|
|
|
|
/* Check that there's space for the authentication tag. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec->data_len < transform->taglen) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
|
|
|
|
") < taglen (%" MBEDTLS_PRINTF_SIZET ") ",
|
|
|
|
rec->data_len,
|
|
|
|
transform->taglen));
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_MAC;
|
2013-10-25 18:33:32 +02:00
|
|
|
}
|
2020-05-21 16:30:57 +02:00
|
|
|
rec->data_len -= transform->taglen;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-05-21 16:30:57 +02:00
|
|
|
/*
|
|
|
|
* Prepare nonce from dynamic and static parts.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_build_record_nonce(iv, sizeof(iv),
|
|
|
|
transform->iv_dec,
|
|
|
|
transform->fixed_ivlen,
|
|
|
|
dynamic_iv,
|
|
|
|
dynamic_iv_len);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-05-21 16:30:57 +02:00
|
|
|
/*
|
|
|
|
* Build additional data for AEAD encryption.
|
|
|
|
* This depends on the TLS version.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
|
|
|
|
transform->tls_version,
|
|
|
|
transform->taglen);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "additional data used for AEAD",
|
|
|
|
add_data, add_data_len);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
/* Because of the check above, we know that there are
|
2021-12-21 06:14:10 +01:00
|
|
|
* explicit_iv_len Bytes preceding data, and taglen
|
2020-02-05 15:44:10 +01:00
|
|
|
* bytes following data + data_len. This justifies
|
|
|
|
* the debug message and the invocation of
|
2021-04-29 23:12:19 +02:00
|
|
|
* mbedtls_cipher_auth_decrypt_ext() below. */
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "IV used", iv, transform->ivlen);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "TAG used", data + rec->data_len,
|
|
|
|
transform->taglen);
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Decrypt and authenticate
|
|
|
|
*/
|
2022-01-13 14:50:15 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_aead_decrypt(transform->psa_key_dec,
|
|
|
|
transform->psa_alg,
|
|
|
|
iv, transform->ivlen,
|
|
|
|
add_data, add_data_len,
|
|
|
|
data, rec->data_len + transform->taglen,
|
|
|
|
data, rec->buf_len - (data - rec->buf),
|
|
|
|
&olen);
|
|
|
|
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "psa_aead_decrypt", ret);
|
|
|
|
return ret;
|
2022-02-03 09:54:34 +01:00
|
|
|
}
|
2022-01-13 14:50:15 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_cipher_auth_decrypt_ext(&transform->cipher_ctx_dec,
|
|
|
|
iv, transform->ivlen,
|
|
|
|
add_data, add_data_len,
|
|
|
|
data, rec->data_len + transform->taglen, /* src */
|
|
|
|
data, rec->buf_len - (data - rec->buf), &olen, /* dst */
|
|
|
|
transform->taglen)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_decrypt_ext", ret);
|
|
|
|
|
|
|
|
if (ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED) {
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_MAC;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-01-13 14:50:15 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
auth_done++;
|
2019-04-30 11:54:22 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Double-check that AEAD decryption doesn't change content length. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (olen != rec->data_len) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2023-10-11 11:54:42 +02:00
|
|
|
#endif /* MBEDTLS_SSL_HAVE_AEAD */
|
2020-07-09 12:11:39 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_mode == MBEDTLS_SSL_MODE_CBC ||
|
|
|
|
ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
|
2020-02-05 15:44:10 +01:00
|
|
|
size_t minlen = 0;
|
2022-01-13 14:50:15 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2022-02-03 08:55:33 +01:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-01-13 14:50:15 +01:00
|
|
|
size_t part_len;
|
|
|
|
psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check immediate ciphertext sanity
|
|
|
|
*/
|
2021-05-12 17:49:18 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2021-07-06 13:42:11 +02:00
|
|
|
/* The ciphertext is prefixed with the CBC IV. */
|
|
|
|
minlen += transform->ivlen;
|
2019-04-30 11:54:22 +02:00
|
|
|
#endif
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Size considerations:
|
|
|
|
*
|
|
|
|
* - The CBC cipher text must not be empty and hence
|
|
|
|
* at least of size transform->ivlen.
|
|
|
|
*
|
|
|
|
* Together with the potential IV-prefix, this explains
|
|
|
|
* the first of the two checks below.
|
|
|
|
*
|
|
|
|
* - The record must contain a MAC, either in plain or
|
|
|
|
* encrypted, depending on whether Encrypt-then-MAC
|
|
|
|
* is used or not.
|
|
|
|
* - If it is, the message contains the IV-prefix,
|
|
|
|
* the CBC ciphertext, and the MAC.
|
|
|
|
* - If it is not, the padded plaintext, and hence
|
|
|
|
* the CBC ciphertext, has at least length maclen + 1
|
|
|
|
* because there is at least the padding length byte.
|
|
|
|
*
|
|
|
|
* As the CBC ciphertext is not empty, both cases give the
|
|
|
|
* lower bound minlen + maclen + 1 on the record size, which
|
|
|
|
* we test for in the second check below.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec->data_len < minlen + transform->ivlen ||
|
|
|
|
rec->data_len < minlen + transform->maclen + 1) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
|
|
|
|
") < max( ivlen(%" MBEDTLS_PRINTF_SIZET
|
|
|
|
"), maclen (%" MBEDTLS_PRINTF_SIZET ") "
|
|
|
|
"+ 1 ) ( + expl IV )",
|
|
|
|
rec->data_len,
|
|
|
|
transform->ivlen,
|
|
|
|
transform->maclen));
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_MAC;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2019-05-03 09:46:14 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Authenticate before decrypt if enabled
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
|
2022-02-23 09:30:33 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
|
|
|
#else
|
2020-02-05 15:44:10 +01:00
|
|
|
unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
|
2022-02-23 09:30:33 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2019-04-30 12:08:59 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac"));
|
2019-04-30 12:08:59 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Update data_len in tandem with add_data.
|
|
|
|
*
|
|
|
|
* The subtraction is safe because of the previous check
|
|
|
|
* data_len >= minlen + maclen + 1.
|
|
|
|
*
|
|
|
|
* Afterwards, we know that data + data_len is followed by at
|
|
|
|
* least maclen Bytes, which justifies the call to
|
2021-10-20 11:59:27 +02:00
|
|
|
* mbedtls_ct_memcmp() below.
|
2020-02-05 15:44:10 +01:00
|
|
|
*
|
|
|
|
* Further, we still know that data_len > minlen */
|
|
|
|
rec->data_len -= transform->maclen;
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
|
|
|
|
transform->tls_version,
|
|
|
|
transform->taglen);
|
2019-04-30 12:08:59 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Calculate expected MAC. */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data,
|
|
|
|
add_data_len);
|
2022-02-23 09:30:33 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_verify_setup(&operation, transform->psa_mac_dec,
|
|
|
|
transform->psa_mac_alg);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-02-23 09:30:33 +01:00
|
|
|
goto hmac_failed_etm_enabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-23 09:30:33 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_update(&operation, add_data, add_data_len);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-02-23 09:30:33 +01:00
|
|
|
goto hmac_failed_etm_enabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-23 09:30:33 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_update(&operation, data, rec->data_len);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-02-23 09:30:33 +01:00
|
|
|
goto hmac_failed_etm_enabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-23 09:30:33 +01:00
|
|
|
|
|
|
|
/* Compare expected MAC with MAC at the end of the record. */
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_verify_finish(&operation, data + rec->data_len,
|
|
|
|
transform->maclen);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-02-23 09:30:33 +01:00
|
|
|
goto hmac_failed_etm_enabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-23 09:30:33 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_md_hmac_update(&transform->md_ctx_dec, add_data,
|
|
|
|
add_data_len);
|
|
|
|
if (ret != 0) {
|
2021-12-10 21:35:10 +01:00
|
|
|
goto hmac_failed_etm_enabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
ret = mbedtls_md_hmac_update(&transform->md_ctx_dec,
|
|
|
|
data, rec->data_len);
|
|
|
|
if (ret != 0) {
|
2021-12-10 21:35:10 +01:00
|
|
|
goto hmac_failed_etm_enabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
ret = mbedtls_md_hmac_finish(&transform->md_ctx_dec, mac_expect);
|
|
|
|
if (ret != 0) {
|
2021-12-10 21:35:10 +01:00
|
|
|
goto hmac_failed_etm_enabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
ret = mbedtls_md_hmac_reset(&transform->md_ctx_dec);
|
|
|
|
if (ret != 0) {
|
2021-12-10 21:35:10 +01:00
|
|
|
goto hmac_failed_etm_enabled;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-04-30 12:08:59 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "message mac", data + rec->data_len,
|
|
|
|
transform->maclen);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect,
|
|
|
|
transform->maclen);
|
2019-04-30 12:08:59 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Compare expected MAC with MAC at the end of the record. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ct_memcmp(data + rec->data_len, mac_expect,
|
|
|
|
transform->maclen) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match"));
|
2021-12-10 21:33:21 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_INVALID_MAC;
|
|
|
|
goto hmac_failed_etm_enabled;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-02-23 09:30:33 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2020-02-05 15:44:10 +01:00
|
|
|
auth_done++;
|
2021-12-10 21:33:21 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
hmac_failed_etm_enabled:
|
2022-02-23 09:30:33 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_abort(&operation);
|
|
|
|
if (ret == 0 && status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-23 09:30:33 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(mac_expect, transform->maclen);
|
2022-03-02 15:14:07 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
if (ret != MBEDTLS_ERR_SSL_INVALID_MAC) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_hmac_xxx", ret);
|
|
|
|
}
|
|
|
|
return ret;
|
2021-12-10 21:35:10 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
|
2019-04-30 12:08:59 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Check length sanity
|
|
|
|
*/
|
2019-05-03 09:16:16 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* We know from above that data_len > minlen >= 0,
|
|
|
|
* so the following check in particular implies that
|
|
|
|
* data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec->data_len % transform->ivlen != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
|
|
|
|
") %% ivlen (%" MBEDTLS_PRINTF_SIZET ") != 0",
|
|
|
|
rec->data_len, transform->ivlen));
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_MAC;
|
2019-05-03 09:16:16 +02:00
|
|
|
}
|
2019-04-30 12:08:59 +02:00
|
|
|
|
2021-05-12 17:49:18 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
2021-05-12 17:49:18 +02:00
|
|
|
* Initialize for prepended IV for block cipher in TLS v1.2
|
2020-02-05 15:44:10 +01:00
|
|
|
*/
|
2021-07-06 13:42:11 +02:00
|
|
|
/* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(transform->iv_dec, data, transform->ivlen);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2021-07-06 13:42:11 +02:00
|
|
|
data += transform->ivlen;
|
|
|
|
rec->data_offset += transform->ivlen;
|
|
|
|
rec->data_len -= transform->ivlen;
|
2021-05-12 17:49:18 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2019-05-03 09:16:16 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
|
|
|
|
|
2022-01-13 14:50:15 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_decrypt_setup(&cipher_op,
|
|
|
|
transform->psa_key_dec, transform->psa_alg);
|
2022-01-13 14:50:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_decrypt_setup", ret);
|
|
|
|
return ret;
|
2022-02-03 09:54:34 +01:00
|
|
|
}
|
2022-01-13 14:50:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_set_iv(&cipher_op, transform->iv_dec, transform->ivlen);
|
2022-01-13 14:50:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_set_iv", ret);
|
|
|
|
return ret;
|
2022-02-03 09:54:34 +01:00
|
|
|
}
|
2022-01-13 14:50:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_update(&cipher_op,
|
|
|
|
data, rec->data_len,
|
|
|
|
data, rec->data_len, &olen);
|
2022-01-13 14:50:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_update", ret);
|
|
|
|
return ret;
|
2022-02-03 09:54:34 +01:00
|
|
|
}
|
2022-01-13 14:50:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_finish(&cipher_op,
|
|
|
|
data + olen, rec->data_len - olen,
|
|
|
|
&part_len);
|
2022-01-13 14:50:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_finish", ret);
|
|
|
|
return ret;
|
2022-02-03 09:54:34 +01:00
|
|
|
}
|
2022-01-13 14:50:15 +01:00
|
|
|
|
|
|
|
olen += part_len;
|
|
|
|
#else
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_dec,
|
|
|
|
transform->iv_dec, transform->ivlen,
|
|
|
|
data, rec->data_len, data, &olen)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret);
|
|
|
|
return ret;
|
2019-05-03 09:16:16 +02:00
|
|
|
}
|
2022-01-13 14:50:15 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2019-04-30 12:08:59 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Double-check that length hasn't changed during decryption. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec->data_len != olen) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2019-05-06 12:05:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Safe since data_len >= minlen + maclen + 1, so after having
|
|
|
|
* subtracted at most minlen and maclen up to this point,
|
|
|
|
* data_len > 0 (because of data_len % ivlen == 0, it's actually
|
|
|
|
* >= ivlen ). */
|
|
|
|
padlen = data[rec->data_len - 1];
|
2019-05-06 12:05:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (auth_done == 1) {
|
2023-08-10 12:58:18 +02:00
|
|
|
const mbedtls_ct_condition_t ge = mbedtls_ct_uint_ge(
|
2023-01-11 14:50:10 +01:00
|
|
|
rec->data_len,
|
|
|
|
padlen + 1);
|
2023-09-12 17:29:39 +02:00
|
|
|
correct = mbedtls_ct_bool_and(ge, correct);
|
2023-08-10 13:11:31 +02:00
|
|
|
padlen = mbedtls_ct_size_if_else_0(ge, padlen);
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DEBUG_ALL)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec->data_len < transform->maclen + padlen + 1) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
|
|
|
|
") < maclen (%" MBEDTLS_PRINTF_SIZET
|
|
|
|
") + padlen (%" MBEDTLS_PRINTF_SIZET ")",
|
|
|
|
rec->data_len,
|
|
|
|
transform->maclen,
|
|
|
|
padlen + 1));
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2019-05-06 12:44:24 +02:00
|
|
|
#endif
|
2023-08-10 12:58:18 +02:00
|
|
|
const mbedtls_ct_condition_t ge = mbedtls_ct_uint_ge(
|
2023-01-11 14:50:10 +01:00
|
|
|
rec->data_len,
|
|
|
|
transform->maclen + padlen + 1);
|
2023-09-12 17:29:39 +02:00
|
|
|
correct = mbedtls_ct_bool_and(ge, correct);
|
2023-08-10 13:11:31 +02:00
|
|
|
padlen = mbedtls_ct_size_if_else_0(ge, padlen);
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2019-05-03 11:43:28 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
padlen++;
|
2012-04-18 18:10:25 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Regardless of the validity of the padding,
|
|
|
|
* we have data_len >= padlen here. */
|
2014-06-26 12:09:34 +02:00
|
|
|
|
2021-05-12 17:49:18 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2021-02-18 13:55:21 +01:00
|
|
|
/* The padding check involves a series of up to 256
|
2023-01-11 14:50:10 +01:00
|
|
|
* consecutive memory reads at the end of the record
|
|
|
|
* plaintext buffer. In order to hide the length and
|
|
|
|
* validity of the padding, always perform exactly
|
|
|
|
* `min(256,plaintext_len)` reads (but take into account
|
|
|
|
* only the last `padlen` bytes for the padding check). */
|
2021-02-18 13:55:21 +01:00
|
|
|
size_t pad_count = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
volatile unsigned char * const check = data;
|
2021-02-18 13:55:21 +01:00
|
|
|
|
|
|
|
/* Index of first padding byte; it has been ensured above
|
2023-01-11 14:50:10 +01:00
|
|
|
* that the subtraction is safe. */
|
2021-02-18 13:55:21 +01:00
|
|
|
size_t const padding_idx = rec->data_len - padlen;
|
|
|
|
size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
|
|
|
|
size_t const start_idx = rec->data_len - num_checks;
|
|
|
|
size_t idx;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (idx = start_idx; idx < rec->data_len; idx++) {
|
2021-02-18 13:55:21 +01:00
|
|
|
/* pad_count += (idx >= padding_idx) &&
|
2023-01-11 14:50:10 +01:00
|
|
|
* (check[idx] == padlen - 1);
|
|
|
|
*/
|
2023-08-10 12:58:18 +02:00
|
|
|
const mbedtls_ct_condition_t a = mbedtls_ct_uint_ge(idx, padding_idx);
|
2023-08-10 13:11:31 +02:00
|
|
|
size_t increment = mbedtls_ct_size_if_else_0(a, 1);
|
2023-08-10 12:58:18 +02:00
|
|
|
const mbedtls_ct_condition_t b = mbedtls_ct_uint_eq(check[idx], padlen - 1);
|
2023-08-10 13:11:31 +02:00
|
|
|
increment = mbedtls_ct_size_if_else_0(b, increment);
|
2023-05-17 13:36:01 +02:00
|
|
|
pad_count += increment;
|
2021-02-18 13:55:21 +01:00
|
|
|
}
|
2023-09-12 17:29:39 +02:00
|
|
|
correct = mbedtls_ct_bool_and(mbedtls_ct_uint_eq(pad_count, padlen), correct);
|
2012-04-18 18:10:25 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DEBUG_ALL)
|
2023-09-12 17:29:39 +02:00
|
|
|
if (padlen > 0 && correct == MBEDTLS_CT_FALSE) {
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("bad padding byte detected"));
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2023-09-12 17:29:39 +02:00
|
|
|
padlen = mbedtls_ct_size_if_else_0(correct, padlen);
|
2021-02-18 13:55:21 +01:00
|
|
|
|
2021-05-12 17:49:18 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2019-01-29 15:14:33 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* If the padding was found to be invalid, padlen == 0
|
|
|
|
* and the subtraction is safe. If the padding was found valid,
|
|
|
|
* padlen hasn't been changed and the previous assertion
|
|
|
|
* data_len >= padlen still holds. */
|
|
|
|
rec->data_len -= padlen;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2020-07-09 12:11:39 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
|
2019-01-29 15:14:33 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2019-01-29 15:14:33 +01:00
|
|
|
}
|
2019-05-03 11:43:28 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DEBUG_ALL)
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "raw buffer after decryption",
|
|
|
|
data, rec->data_len);
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2019-01-29 15:14:33 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Authenticate if not done yet.
|
|
|
|
* Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
|
|
|
|
*/
|
2020-11-30 09:54:23 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (auth_done == 0) {
|
2022-05-09 19:15:54 +02:00
|
|
|
unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD] = { 0 };
|
|
|
|
unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD] = { 0 };
|
2019-05-03 11:43:28 +02:00
|
|
|
|
2023-09-18 14:08:11 +02:00
|
|
|
/* For CBC+MAC, If the initial value of padlen was such that
|
2020-02-05 15:44:10 +01:00
|
|
|
* data_len < maclen + padlen + 1, then padlen
|
|
|
|
* got reset to 1, and the initial check
|
|
|
|
* data_len >= minlen + maclen + 1
|
|
|
|
* guarantees that at this point we still
|
|
|
|
* have at least data_len >= maclen.
|
|
|
|
*
|
|
|
|
* If the initial value of padlen was such that
|
|
|
|
* data_len >= maclen + padlen + 1, then we have
|
|
|
|
* subtracted either padlen + 1 (if the padding was correct)
|
|
|
|
* or 0 (if the padding was incorrect) since then,
|
|
|
|
* hence data_len >= maclen in any case.
|
2023-09-18 14:08:11 +02:00
|
|
|
*
|
|
|
|
* For stream ciphers, we checked above that
|
|
|
|
* data_len >= maclen.
|
2020-02-05 15:44:10 +01:00
|
|
|
*/
|
|
|
|
rec->data_len -= transform->maclen;
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
|
|
|
|
transform->tls_version,
|
|
|
|
transform->taglen);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2021-05-12 17:49:18 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2021-02-18 13:55:21 +01:00
|
|
|
/*
|
2023-01-11 14:50:10 +01:00
|
|
|
* The next two sizes are the minimum and maximum values of
|
|
|
|
* data_len over all padlen values.
|
|
|
|
*
|
|
|
|
* They're independent of padlen, since we previously did
|
|
|
|
* data_len -= padlen.
|
|
|
|
*
|
|
|
|
* Note that max_len + maclen is never more than the buffer
|
|
|
|
* length, as we previously did in_msglen -= maclen too.
|
|
|
|
*/
|
2021-02-18 13:55:21 +01:00
|
|
|
const size_t max_len = rec->data_len + padlen;
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t min_len = (max_len > 256) ? max_len - 256 : 0;
|
2021-02-18 13:55:21 +01:00
|
|
|
|
2022-02-25 15:14:29 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ct_hmac(transform->psa_mac_dec,
|
|
|
|
transform->psa_mac_alg,
|
|
|
|
add_data, add_data_len,
|
|
|
|
data, rec->data_len, min_len, max_len,
|
|
|
|
mac_expect);
|
2022-02-25 15:14:29 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ct_hmac(&transform->md_ctx_dec,
|
|
|
|
add_data, add_data_len,
|
|
|
|
data, rec->data_len, min_len, max_len,
|
|
|
|
mac_expect);
|
2022-02-25 15:14:29 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ct_hmac", ret);
|
2021-12-10 21:33:21 +01:00
|
|
|
goto hmac_failed_etm_disabled;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2021-02-18 13:55:21 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ct_memcpy_offset(mac_peer, data,
|
|
|
|
rec->data_len,
|
|
|
|
min_len, max_len,
|
|
|
|
transform->maclen);
|
2021-05-12 17:49:18 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2013-10-14 13:09:25 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DEBUG_ALL)
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect, transform->maclen);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "message mac", mac_peer, transform->maclen);
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2013-10-14 13:09:25 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ct_memcmp(mac_peer, mac_expect,
|
|
|
|
transform->maclen) != 0) {
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DEBUG_ALL)
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match"));
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2023-09-12 17:29:39 +02:00
|
|
|
correct = MBEDTLS_CT_FALSE;
|
2013-10-14 13:09:25 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
auth_done++;
|
2021-12-10 21:33:21 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
hmac_failed_etm_disabled:
|
|
|
|
mbedtls_platform_zeroize(mac_peer, transform->maclen);
|
|
|
|
mbedtls_platform_zeroize(mac_expect, transform->maclen);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2013-10-14 13:09:25 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Finally check the correct flag
|
|
|
|
*/
|
2023-09-12 17:29:39 +02:00
|
|
|
if (correct == MBEDTLS_CT_FALSE) {
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INVALID_MAC;
|
|
|
|
}
|
2020-11-30 09:54:23 +01:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
|
2013-10-14 13:09:25 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Make extra sure authentication was performed, exactly once */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (auth_done != 1) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2013-10-14 13:09:25 +02:00
|
|
|
}
|
|
|
|
|
2021-12-08 16:57:54 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (transform->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
|
2020-05-04 13:30:04 +02:00
|
|
|
/* Remove inner padding and infer true content type. */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_parse_inner_plaintext(data, &rec->data_len,
|
|
|
|
&rec->type);
|
2020-05-04 13:30:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_RECORD;
|
|
|
|
}
|
2020-05-04 13:30:04 +02:00
|
|
|
}
|
2021-12-08 16:57:54 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
2020-05-04 13:30:04 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec->cid_len != 0) {
|
|
|
|
ret = ssl_parse_inner_plaintext(data, &rec->data_len,
|
|
|
|
&rec->type);
|
|
|
|
if (ret != 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_RECORD;
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
2014-03-25 16:28:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= decrypt buf"));
|
2013-10-14 13:09:25 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2013-10-14 13:09:25 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#undef MAC_NONE
|
|
|
|
#undef MAC_PLAINTEXT
|
|
|
|
#undef MAC_CIPHERTEXT
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill the input message buffer by appending data to it.
|
|
|
|
* The amount of data already fetched is in ssl->in_left.
|
|
|
|
*
|
|
|
|
* If we return 0, is it guaranteed that (at least) nb_want bytes are
|
|
|
|
* available (from this read and/or a previous one). Otherwise, an error code
|
|
|
|
* is returned (possibly EOF or WANT_READ).
|
|
|
|
*
|
|
|
|
* With stream transport (TLS) on success ssl->in_left == nb_want, but
|
|
|
|
* with datagram transport (DTLS) on success ssl->in_left >= nb_want,
|
|
|
|
* since we always read a whole datagram at once.
|
|
|
|
*
|
|
|
|
* For DTLS, it is up to the caller to set ssl->next_record_offset when
|
|
|
|
* they're done reading a record.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_fetch_input(mbedtls_ssl_context *ssl, size_t nb_want)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t len;
|
2019-11-28 15:29:44 +01:00
|
|
|
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
|
|
|
|
size_t in_buf_len = ssl->in_buf_len;
|
|
|
|
#else
|
|
|
|
size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
|
|
|
|
#endif
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> fetch input"));
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->f_recv == NULL && ssl->f_recv_timeout == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Bad usage of mbedtls_ssl_set_bio() "));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2019-04-29 18:31:37 +02:00
|
|
|
}
|
2019-04-29 14:52:53 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (nb_want > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("requesting more data than fits"));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2019-04-29 18:31:37 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
2020-02-05 15:44:10 +01:00
|
|
|
uint32_t timeout;
|
2017-12-27 22:37:21 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* The point is, we need to always read a full datagram at once, so we
|
|
|
|
* sometimes read more then requested, and handle the additional data.
|
|
|
|
* It could be the rest of the current record (while fetching the
|
|
|
|
* header) and/or some other records in the same datagram.
|
|
|
|
*/
|
2017-11-09 19:57:39 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Move to the next record in the already read datagram if applicable
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->next_record_offset != 0) {
|
|
|
|
if (ssl->in_left < ssl->next_record_offset) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_left -= ssl->next_record_offset;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_left != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("next record in same datagram, offset: %"
|
|
|
|
MBEDTLS_PRINTF_SIZET,
|
|
|
|
ssl->next_record_offset));
|
|
|
|
memmove(ssl->in_hdr,
|
|
|
|
ssl->in_hdr + ssl->next_record_offset,
|
|
|
|
ssl->in_left);
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->next_record_offset = 0;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET
|
|
|
|
", nb_want: %" MBEDTLS_PRINTF_SIZET,
|
|
|
|
ssl->in_left, nb_want));
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Done if we already have enough data.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (nb_want <= ssl->in_left) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input"));
|
|
|
|
return 0;
|
2013-09-02 14:57:01 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* A record can't be split across datagrams. If we need to read but
|
|
|
|
* are not at the beginning of a new record, the caller did something
|
|
|
|
* wrong.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_left != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2013-09-02 14:57:01 +02:00
|
|
|
}
|
2012-04-18 16:23:57 +02:00
|
|
|
|
2013-01-07 18:20:04 +01:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Don't even try to read if time's out already.
|
|
|
|
* This avoids by-passing the timer when repeatedly receiving messages
|
|
|
|
* that will end up being dropped.
|
2013-01-07 18:20:04 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_check_timer(ssl) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("timer has expired"));
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_TIMEOUT;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
len = in_buf_len - (ssl->in_hdr - ssl->in_buf);
|
2014-10-29 22:29:20 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
|
2020-02-05 15:44:10 +01:00
|
|
|
timeout = ssl->handshake->retransmit_timeout;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2020-02-05 15:44:10 +01:00
|
|
|
timeout = ssl->conf->read_timeout;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-04-26 14:34:37 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("f_recv_timeout: %lu ms", (unsigned long) timeout));
|
2012-04-18 16:23:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->f_recv_timeout != NULL) {
|
|
|
|
ret = ssl->f_recv_timeout(ssl->p_bio, ssl->in_hdr, len,
|
|
|
|
timeout);
|
|
|
|
} else {
|
|
|
|
ret = ssl->f_recv(ssl->p_bio, ssl->in_hdr, len);
|
|
|
|
}
|
2017-12-27 22:37:21 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_CONN_EOF;
|
|
|
|
}
|
2017-12-27 22:37:21 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == MBEDTLS_ERR_SSL_TIMEOUT) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("timeout"));
|
|
|
|
mbedtls_ssl_set_timer(ssl, 0);
|
|
|
|
|
|
|
|
if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
|
|
|
|
if (ssl_double_retransmit_timeout(ssl) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("handshake timeout"));
|
|
|
|
return MBEDTLS_ERR_SSL_TIMEOUT;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2017-12-27 22:37:21 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_resend(ssl)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2010-07-25 16:24:53 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_WANT_READ;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
else if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
|
|
|
|
ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
|
|
|
|
if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request",
|
|
|
|
ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2010-07-25 16:24:53 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_WANT_READ;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
|
2010-07-25 16:24:53 +02:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_left = ret;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2013-08-31 17:40:26 +02:00
|
|
|
#endif
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET
|
|
|
|
", nb_want: %" MBEDTLS_PRINTF_SIZET,
|
|
|
|
ssl->in_left, nb_want));
|
2014-10-29 12:07:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (ssl->in_left < nb_want) {
|
2020-02-05 15:44:10 +01:00
|
|
|
len = nb_want - ssl->in_left;
|
2014-10-29 12:07:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_check_timer(ssl) != 0) {
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_TIMEOUT;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
if (ssl->f_recv_timeout != NULL) {
|
|
|
|
ret = ssl->f_recv_timeout(ssl->p_bio,
|
|
|
|
ssl->in_hdr + ssl->in_left, len,
|
|
|
|
ssl->conf->read_timeout);
|
|
|
|
} else {
|
|
|
|
ret = ssl->f_recv(ssl->p_bio,
|
|
|
|
ssl->in_hdr + ssl->in_left, len);
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2017-12-27 22:37:21 +01:00
|
|
|
}
|
2015-01-13 10:59:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET
|
|
|
|
", nb_want: %" MBEDTLS_PRINTF_SIZET,
|
|
|
|
ssl->in_left, nb_want));
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret);
|
2019-04-26 14:34:37 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_CONN_EOF;
|
|
|
|
}
|
2014-11-04 14:40:21 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2014-10-29 12:07:57 +01:00
|
|
|
|
2023-02-10 17:03:44 +01:00
|
|
|
if ((size_t) ret > len) {
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1,
|
|
|
|
("f_recv returned %d bytes but only %" MBEDTLS_PRINTF_SIZET
|
|
|
|
" were requested",
|
|
|
|
ret, len));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2018-01-05 17:24:22 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_left += ret;
|
2014-10-29 12:07:57 +01:00
|
|
|
}
|
2015-01-13 10:59:51 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input"));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Flush any data not yet written
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_flush_output(mbedtls_ssl_context *ssl)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned char *buf;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> flush output"));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->f_send == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Bad usage of mbedtls_ssl_set_bio() "));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Avoid incrementing counter if data is flushed */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->out_left == 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output"));
|
|
|
|
return 0;
|
2019-05-08 14:02:22 +02:00
|
|
|
}
|
2019-04-29 14:52:53 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (ssl->out_left > 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("message length: %" MBEDTLS_PRINTF_SIZET
|
|
|
|
", out_left: %" MBEDTLS_PRINTF_SIZET,
|
|
|
|
mbedtls_ssl_out_hdr_len(ssl) + ssl->out_msglen, ssl->out_left));
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
buf = ssl->out_hdr - ssl->out_left;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl->f_send(ssl->p_bio, buf, ssl->out_left);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", ret);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2013-09-02 14:57:01 +02:00
|
|
|
|
2023-02-10 17:03:44 +01:00
|
|
|
if ((size_t) ret > ssl->out_left) {
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1,
|
|
|
|
("f_send returned %d bytes but only %" MBEDTLS_PRINTF_SIZET
|
|
|
|
" bytes were sent",
|
|
|
|
ret, ssl->out_left));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2013-09-02 14:57:01 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
ssl->out_left -= ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_hdr = ssl->out_buf;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2012-04-18 16:23:57 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_hdr = ssl->out_buf + 8;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
|
2014-06-17 10:54:17 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output"));
|
2019-07-10 14:55:25 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2018-06-18 11:16:43 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Functions to handle the DTLS retransmission state machine
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
/*
|
|
|
|
* Append current handshake message to current outgoing flight
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_flight_append(mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
|
|
|
mbedtls_ssl_flight_item *msg;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_flight_append"));
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "message appended to flight",
|
|
|
|
ssl->out_msg, ssl->out_msglen);
|
2019-07-10 14:55:25 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Allocate space for current message */
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((msg = mbedtls_calloc(1, sizeof(mbedtls_ssl_flight_item))) == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
|
|
|
|
sizeof(mbedtls_ssl_flight_item)));
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2018-06-18 11:16:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((msg->p = mbedtls_calloc(1, ssl->out_msglen)) == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
|
|
|
|
ssl->out_msglen));
|
|
|
|
mbedtls_free(msg);
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2018-06-18 11:16:43 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Copy current handshake message with headers */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(msg->p, ssl->out_msg, ssl->out_msglen);
|
2020-02-05 15:44:10 +01:00
|
|
|
msg->len = ssl->out_msglen;
|
|
|
|
msg->type = ssl->out_msgtype;
|
|
|
|
msg->next = NULL;
|
2013-01-07 18:20:04 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Append to the current flight */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->flight == NULL) {
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake->flight = msg;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
|
2023-01-11 14:50:10 +01:00
|
|
|
while (cur->next != NULL) {
|
2020-02-05 15:44:10 +01:00
|
|
|
cur = cur->next;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
cur->next = msg;
|
|
|
|
}
|
2017-12-27 22:28:58 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_flight_append"));
|
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2017-12-27 22:28:58 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Free the current flight of handshake messages
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_flight_free(mbedtls_ssl_flight_item *flight)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
|
|
|
mbedtls_ssl_flight_item *cur = flight;
|
|
|
|
mbedtls_ssl_flight_item *next;
|
2017-12-27 22:28:58 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (cur != NULL) {
|
2020-02-05 15:44:10 +01:00
|
|
|
next = cur->next;
|
2013-01-07 18:20:04 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(cur->p);
|
|
|
|
mbedtls_free(cur);
|
2013-01-07 18:20:04 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
cur = next;
|
|
|
|
}
|
|
|
|
}
|
2013-09-05 16:10:41 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Swap transform_out and out_ctr with the alternative ones
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_swap_epochs(mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
|
|
|
mbedtls_ssl_transform *tmp_transform;
|
2021-10-08 09:21:19 +02:00
|
|
|
unsigned char tmp_out_ctr[MBEDTLS_SSL_SEQUENCE_NUMBER_LEN];
|
2012-04-18 16:23:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->transform_out == ssl->handshake->alt_transform_out) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("skip swap epochs"));
|
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2010-07-25 16:24:53 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("swap epochs"));
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
/* Swap transforms */
|
|
|
|
tmp_transform = ssl->transform_out;
|
|
|
|
ssl->transform_out = ssl->handshake->alt_transform_out;
|
|
|
|
ssl->handshake->alt_transform_out = tmp_transform;
|
|
|
|
|
|
|
|
/* Swap epoch + sequence_number */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(tmp_out_ctr, ssl->cur_out_ctr, sizeof(tmp_out_ctr));
|
|
|
|
memcpy(ssl->cur_out_ctr, ssl->handshake->alt_out_ctr,
|
|
|
|
sizeof(ssl->cur_out_ctr));
|
|
|
|
memcpy(ssl->handshake->alt_out_ctr, tmp_out_ctr,
|
|
|
|
sizeof(ssl->handshake->alt_out_ctr));
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
/* Adjust to the newly activated transform */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2013-01-03 14:52:21 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Retransmit the current flight of messages.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_resend(mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2014-09-29 15:29:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_resend"));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_flight_transmit(ssl);
|
2017-09-13 12:45:21 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_resend"));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Transmit or retransmit the current flight of messages.
|
|
|
|
*
|
|
|
|
* Need to remember the current message in case flush_output returns
|
|
|
|
* WANT_WRITE, causing us to exit this function and come back later.
|
|
|
|
* This function must be called until state is no longer SENDING.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_flight_transmit(mbedtls_ssl_context *ssl)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_flight_transmit"));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("initialise flight transmission"));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake->cur_msg = ssl->handshake->flight;
|
|
|
|
ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_swap_epochs(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (ssl->handshake->cur_msg != NULL) {
|
2020-02-05 15:44:10 +01:00
|
|
|
size_t max_frag_len;
|
|
|
|
const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
|
2014-09-19 15:09:21 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
int const is_finished =
|
2023-01-11 14:50:10 +01:00
|
|
|
(cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
|
|
|
|
cur->p[0] == MBEDTLS_SSL_HS_FINISHED);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-03-08 15:57:12 +01:00
|
|
|
int const force_flush = ssl->disable_datagram_packing == 1 ?
|
2023-01-11 14:50:10 +01:00
|
|
|
SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Swap epochs before sending Finished: we can't do it after
|
|
|
|
* sending ChangeCipherSpec, in case write returns WANT_READ.
|
|
|
|
* Must be done before copying, may change out_msg pointer */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (is_finished && ssl->handshake->cur_msg_p == (cur->p + 12)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("swap epochs to send finished message"));
|
|
|
|
ret = ssl_swap_epochs(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2014-06-26 13:37:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_get_remaining_payload_in_datagram(ssl);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
max_frag_len = (size_t) ret;
|
2017-04-28 18:15:26 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* CCS is copied as is, while HS messages may need fragmentation */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
|
|
|
|
if (max_frag_len == 0) {
|
|
|
|
if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2014-06-26 13:37:14 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-06-19 12:16:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(ssl->out_msg, cur->p, cur->len);
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_msglen = cur->len;
|
|
|
|
ssl->out_msgtype = cur->type;
|
2017-05-17 11:56:15 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Update position inside current message */
|
|
|
|
ssl->handshake->cur_msg_p += cur->len;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2020-02-05 15:44:10 +01:00
|
|
|
const unsigned char * const p = ssl->handshake->cur_msg_p;
|
|
|
|
const size_t hs_len = cur->len - 12;
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t frag_off = p - (cur->p + 12);
|
2020-02-05 15:44:10 +01:00
|
|
|
const size_t rem_len = hs_len - frag_off;
|
|
|
|
size_t cur_hs_frag_len, max_hs_frag_len;
|
2019-02-06 17:18:31 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((max_frag_len < 12) || (max_frag_len == 12 && hs_len != 0)) {
|
|
|
|
if (is_finished) {
|
|
|
|
ret = ssl_swap_epochs(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2020-02-26 09:53:42 +01:00
|
|
|
}
|
2014-06-26 13:37:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2014-07-01 14:53:22 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
max_hs_frag_len = max_frag_len - 12;
|
2014-07-01 14:53:22 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
cur_hs_frag_len = rem_len > max_hs_frag_len ?
|
2023-01-11 14:50:10 +01:00
|
|
|
max_hs_frag_len : rem_len;
|
2014-06-26 13:37:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (frag_off == 0 && cur_hs_frag_len != hs_len) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("fragmenting handshake message (%u > %u)",
|
|
|
|
(unsigned) cur_hs_frag_len,
|
|
|
|
(unsigned) max_hs_frag_len));
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2014-06-26 13:37:14 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Messages are stored with handshake headers as if not fragmented,
|
|
|
|
* copy beginning of headers then fill fragmentation fields.
|
|
|
|
* Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(ssl->out_msg, cur->p, 6);
|
2014-06-26 13:37:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->out_msg[6] = MBEDTLS_BYTE_2(frag_off);
|
|
|
|
ssl->out_msg[7] = MBEDTLS_BYTE_1(frag_off);
|
|
|
|
ssl->out_msg[8] = MBEDTLS_BYTE_0(frag_off);
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->out_msg[9] = MBEDTLS_BYTE_2(cur_hs_frag_len);
|
|
|
|
ssl->out_msg[10] = MBEDTLS_BYTE_1(cur_hs_frag_len);
|
|
|
|
ssl->out_msg[11] = MBEDTLS_BYTE_0(cur_hs_frag_len);
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "handshake header", ssl->out_msg, 12);
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Copy the handshake message content and set records fields */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(ssl->out_msg + 12, p, cur_hs_frag_len);
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_msglen = cur_hs_frag_len + 12;
|
|
|
|
ssl->out_msgtype = cur->type;
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Update position inside current message */
|
|
|
|
ssl->handshake->cur_msg_p += cur_hs_frag_len;
|
|
|
|
}
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* If done with the current message move to the next one if any */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->cur_msg_p >= cur->p + cur->len) {
|
|
|
|
if (cur->next != NULL) {
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake->cur_msg = cur->next;
|
|
|
|
ssl->handshake->cur_msg_p = cur->next->p + 12;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake->cur_msg = NULL;
|
|
|
|
ssl->handshake->cur_msg_p = NULL;
|
|
|
|
}
|
|
|
|
}
|
2013-04-16 13:15:56 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Actually send the message out */
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_write_record(ssl, force_flush)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2014-06-26 13:37:14 +02:00
|
|
|
}
|
2013-07-04 11:51:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2014-07-09 11:09:24 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Update state and set timer */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_is_handshake_over(ssl) == 1) {
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout);
|
2015-05-11 11:25:46 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_flight_transmit"));
|
2014-09-19 15:09:21 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2012-09-16 21:57:18 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* To be called when the last message of an incoming flight is received.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_recv_flight_completed(mbedtls_ssl_context *ssl)
|
2014-07-23 17:52:09 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
/* We won't need to resend that one any more */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_flight_free(ssl->handshake->flight);
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake->flight = NULL;
|
|
|
|
ssl->handshake->cur_msg = NULL;
|
2014-07-23 17:52:09 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* The next incoming flight will start with this msg_seq */
|
|
|
|
ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
|
2014-07-23 17:52:09 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* We don't want to remember CCS's across flight boundaries. */
|
|
|
|
ssl->handshake->buffering.seen_ccs = 0;
|
2014-07-23 17:52:09 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Clear future message buffering structure. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_buffering_free(ssl);
|
2014-07-23 17:52:09 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Cancel timer */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_set_timer(ssl, 0);
|
2018-08-06 10:26:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
|
|
|
|
ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) {
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2018-08-06 10:26:08 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* To be called when the last message of an outgoing flight is send.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_send_flight_completed(mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_reset_retransmit_timeout(ssl);
|
|
|
|
mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
|
|
|
|
ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) {
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-08-06 10:26:08 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2018-08-06 10:26:08 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Handshake layer functions
|
2018-08-06 10:26:08 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_start_handshake_msg(mbedtls_ssl_context *ssl, unsigned hs_type,
|
|
|
|
unsigned char **buf, size_t *buf_len)
|
2022-03-10 18:56:50 +01:00
|
|
|
{
|
|
|
|
/*
|
2021-12-21 06:14:10 +01:00
|
|
|
* Reserve 4 bytes for handshake header. ( Section 4,RFC 8446 )
|
2022-03-10 18:56:50 +01:00
|
|
|
* ...
|
|
|
|
* HandshakeType msg_type;
|
|
|
|
* uint24 length;
|
|
|
|
* ...
|
|
|
|
*/
|
|
|
|
*buf = ssl->out_msg + 4;
|
|
|
|
*buf_len = MBEDTLS_SSL_OUT_CONTENT_LEN - 4;
|
|
|
|
|
|
|
|
ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
|
|
|
|
ssl->out_msg[0] = hs_type;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-03-10 18:56:50 +01:00
|
|
|
}
|
2018-08-06 10:26:08 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Write (DTLS: or queue) current handshake (including CCS) message.
|
|
|
|
*
|
|
|
|
* - fill in handshake headers
|
|
|
|
* - update handshake checksum
|
|
|
|
* - DTLS: save message for resending
|
|
|
|
* - then pass to the record layer
|
|
|
|
*
|
|
|
|
* DTLS: except for HelloRequest, messages are only queued, and will only be
|
|
|
|
* actually sent when calling flight_transmit() or resend().
|
|
|
|
*
|
|
|
|
* Inputs:
|
|
|
|
* - ssl->out_msglen: 4 + actual handshake message len
|
|
|
|
* (4 is the size of handshake headers for TLS)
|
|
|
|
* - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
|
|
|
|
* - ssl->out_msg + 4: the handshake message body
|
|
|
|
*
|
|
|
|
* Outputs, ie state before passing to flight_append() or write_record():
|
|
|
|
* - ssl->out_msglen: the length of the record contents
|
|
|
|
* (including handshake headers but excluding record headers)
|
|
|
|
* - ssl->out_msg: the record contents (handshake headers + content)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_write_handshake_msg_ext(mbedtls_ssl_context *ssl,
|
|
|
|
int update_checksum,
|
|
|
|
int force_flush)
|
2018-08-06 10:26:08 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
const size_t hs_len = ssl->out_msglen - 4;
|
|
|
|
const unsigned char hs_type = ssl->out_msg[0];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> write handshake message"));
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Sanity checks
|
2019-05-08 10:38:41 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
|
|
|
|
ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Whenever we send anything different from a
|
|
|
|
* HelloRequest we should be in a handshake - double check. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
|
|
|
|
hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST) &&
|
|
|
|
ssl->handshake == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2019-05-08 10:38:41 +02:00
|
|
|
|
2018-08-06 10:26:08 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake != NULL &&
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2018-08-06 10:26:08 +02:00
|
|
|
}
|
|
|
|
#endif
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
/* Double-check that we did not exceed the bounds
|
|
|
|
* of the outgoing record buffer.
|
|
|
|
* This should never fail as the various message
|
|
|
|
* writing functions must obey the bounds of the
|
|
|
|
* outgoing record buffer, but better be safe.
|
|
|
|
*
|
|
|
|
* Note: We deliberately do not check for the MTU or MFL here.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Record too large: "
|
|
|
|
"size %" MBEDTLS_PRINTF_SIZET
|
|
|
|
", maximum %" MBEDTLS_PRINTF_SIZET,
|
|
|
|
ssl->out_msglen,
|
|
|
|
(size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2018-08-06 10:26:08 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Fill handshake headers
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) {
|
|
|
|
ssl->out_msg[1] = MBEDTLS_BYTE_2(hs_len);
|
|
|
|
ssl->out_msg[2] = MBEDTLS_BYTE_1(hs_len);
|
|
|
|
ssl->out_msg[3] = MBEDTLS_BYTE_0(hs_len);
|
2018-08-06 10:26:08 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* DTLS has additional fields in the Handshake layer,
|
|
|
|
* between the length field and the actual payload:
|
|
|
|
* uint16 message_seq;
|
|
|
|
* uint24 fragment_offset;
|
|
|
|
* uint24 fragment_length;
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Make room for the additional DTLS fields */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS handshake message too large: "
|
|
|
|
"size %" MBEDTLS_PRINTF_SIZET ", maximum %"
|
|
|
|
MBEDTLS_PRINTF_SIZET,
|
|
|
|
hs_len,
|
|
|
|
(size_t) (MBEDTLS_SSL_OUT_CONTENT_LEN - 12)));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2015-04-29 00:48:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memmove(ssl->out_msg + 12, ssl->out_msg + 4, hs_len);
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_msglen += 8;
|
2018-08-10 12:12:52 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Write message_seq and update it, except for HelloRequest */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST) {
|
|
|
|
MBEDTLS_PUT_UINT16_BE(ssl->handshake->out_msg_seq, ssl->out_msg, 4);
|
|
|
|
++(ssl->handshake->out_msg_seq);
|
|
|
|
} else {
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_msg[4] = 0;
|
|
|
|
ssl->out_msg[5] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handshake hashes are computed without fragmentation,
|
|
|
|
* so set frag_offset = 0 and frag_len = hs_len for now */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(ssl->out_msg + 6, 0x00, 3);
|
|
|
|
memcpy(ssl->out_msg + 9, ssl->out_msg + 1, 3);
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
|
|
|
|
|
|
|
/* Update running hashes of handshake messages seen */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST && update_checksum != 0) {
|
2023-02-06 00:34:21 +01:00
|
|
|
ret = ssl->handshake->update_checksum(ssl, ssl->out_msg,
|
|
|
|
ssl->out_msglen);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Either send now, or just save to be sent (and resent) later */
|
2018-08-10 12:12:52 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
|
|
|
|
!(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
|
|
|
|
hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST)) {
|
|
|
|
if ((ret = ssl_flight_append(ssl)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_flight_append", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2018-08-10 12:12:52 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_write_record(ssl, force_flush)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_record", ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2018-08-10 12:12:52 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= write handshake message"));
|
2018-08-10 12:12:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_finish_handshake_msg(mbedtls_ssl_context *ssl,
|
|
|
|
size_t buf_len, size_t msg_len)
|
2022-03-10 18:56:50 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t msg_with_header_len;
|
|
|
|
((void) buf_len);
|
|
|
|
|
|
|
|
/* Add reserved 4 bytes for handshake header */
|
|
|
|
msg_with_header_len = msg_len + 4;
|
|
|
|
ssl->out_msglen = msg_with_header_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_write_handshake_msg_ext(ssl, 0, 0));
|
2022-03-10 18:56:50 +01:00
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-03-10 18:56:50 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Record layer functions
|
|
|
|
*/
|
2012-09-28 09:31:51 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Write current record.
|
|
|
|
*
|
|
|
|
* Uses:
|
|
|
|
* - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
|
|
|
|
* - ssl->out_msglen: length of the record content (excl headers)
|
|
|
|
* - ssl->out_msg: record content
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_write_record(mbedtls_ssl_context *ssl, int force_flush)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
|
|
|
int ret, done = 0;
|
|
|
|
size_t len = ssl->out_msglen;
|
2022-03-08 15:57:12 +01:00
|
|
|
int flush = force_flush;
|
2018-07-24 13:53:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> write record"));
|
2018-07-04 17:41:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!done) {
|
2020-02-05 15:44:10 +01:00
|
|
|
unsigned i;
|
|
|
|
size_t protected_record_size;
|
2019-11-28 15:29:44 +01:00
|
|
|
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
|
|
|
|
size_t out_buf_len = ssl->out_buf_len;
|
|
|
|
#else
|
|
|
|
size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
|
|
|
|
#endif
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Skip writing the record content type to after the encryption,
|
|
|
|
* as it may change when using the CID extension. */
|
2022-03-15 00:04:24 +01:00
|
|
|
mbedtls_ssl_protocol_version tls_ver = ssl->tls_version;
|
2021-12-08 16:57:54 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2021-11-08 03:30:54 +01:00
|
|
|
/* TLS 1.3 still uses the TLS 1.2 version identifier
|
|
|
|
* for backwards compatibility. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (tls_ver == MBEDTLS_SSL_VERSION_TLS1_3) {
|
2022-03-15 00:04:24 +01:00
|
|
|
tls_ver = MBEDTLS_SSL_VERSION_TLS1_2;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-12-08 16:57:54 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_write_version(ssl->out_hdr + 1, ssl->conf->transport,
|
|
|
|
tls_ver);
|
2018-07-04 17:41:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(ssl->out_ctr, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
|
|
|
|
MBEDTLS_PUT_UINT16_BE(len, ssl->out_len, 0);
|
2018-07-04 17:41:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->transform_out != NULL) {
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_record rec;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
rec.buf = ssl->out_iv;
|
2023-01-11 14:50:10 +01:00
|
|
|
rec.buf_len = out_buf_len - (ssl->out_iv - ssl->out_buf);
|
2020-02-05 15:44:10 +01:00
|
|
|
rec.data_len = ssl->out_msglen;
|
|
|
|
rec.data_offset = ssl->out_msg - rec.buf;
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(&rec.ctr[0], ssl->out_ctr, sizeof(rec.ctr));
|
|
|
|
mbedtls_ssl_write_version(rec.ver, ssl->conf->transport, tls_ver);
|
2020-02-05 15:44:10 +01:00
|
|
|
rec.type = ssl->out_msgtype;
|
2018-08-10 13:38:21 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
|
|
|
/* The CID is set by mbedtls_ssl_encrypt_buf(). */
|
|
|
|
rec.cid_len = 0;
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
2014-11-03 08:23:14 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_encrypt_buf(ssl, ssl->transform_out, &rec,
|
|
|
|
ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_encrypt_buf", ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2015-05-13 16:22:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec.data_offset != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Update the record content type and CID. */
|
|
|
|
ssl->out_msgtype = rec.type;
|
2023-01-11 14:50:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
|
|
|
memcpy(ssl->out_cid, rec.cid, rec.cid_len);
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
|
|
|
ssl->out_msglen = len = rec.data_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->out_len, 0);
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
protected_record_size = len + mbedtls_ssl_out_hdr_len(ssl);
|
2011-10-06 14:37:39 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2020-02-05 15:44:10 +01:00
|
|
|
/* In case of DTLS, double-check that we don't exceed
|
|
|
|
* the remaining space in the datagram. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
|
|
|
ret = ssl_get_remaining_space_in_datagram(ssl);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2011-10-06 14:37:39 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (protected_record_size > (size_t) ret) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Should never happen */
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2011-10-06 14:37:39 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Now write the potentially updated record content type. */
|
|
|
|
ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
|
2018-08-06 10:40:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("output record: msgtype = %u, "
|
|
|
|
"version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET,
|
|
|
|
ssl->out_hdr[0], ssl->out_hdr[1],
|
|
|
|
ssl->out_hdr[2], len));
|
2011-10-06 14:37:39 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network",
|
|
|
|
ssl->out_hdr, protected_record_size);
|
2018-08-13 17:35:15 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_left += protected_record_size;
|
|
|
|
ssl->out_hdr += protected_record_size;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
|
2012-05-08 11:17:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 8; i > mbedtls_ssl_ep_len(ssl); i--) {
|
|
|
|
if (++ssl->cur_out_ctr[i - 1] != 0) {
|
2022-06-28 11:55:35 +02:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
}
|
2022-06-22 13:17:28 +02:00
|
|
|
|
2022-06-28 11:45:18 +02:00
|
|
|
/* The loop goes to its end if the counter is wrapping */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (i == mbedtls_ssl_ep_len(ssl)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("outgoing message counter would wrap"));
|
|
|
|
return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
|
2012-07-03 15:30:23 +02:00
|
|
|
}
|
2012-05-08 11:17:57 +02:00
|
|
|
}
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
|
|
|
|
flush == SSL_DONT_FORCE_FLUSH) {
|
2020-02-05 15:44:10 +01:00
|
|
|
size_t remaining;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_get_remaining_payload_in_datagram(ssl);
|
|
|
|
if (ret < 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_remaining_payload_in_datagram",
|
|
|
|
ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
remaining = (size_t) ret;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (remaining == 0) {
|
2020-02-05 15:44:10 +01:00
|
|
|
flush = SSL_FORCE_FLUSH;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2,
|
|
|
|
("Still %u bytes available in current datagram",
|
|
|
|
(unsigned) remaining));
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2013-02-14 11:19:38 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2013-02-14 11:19:38 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((flush == SSL_FORCE_FLUSH) &&
|
|
|
|
(ret = mbedtls_ssl_flush_output(ssl)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret);
|
|
|
|
return ret;
|
2015-09-08 11:58:14 +02:00
|
|
|
}
|
2014-07-22 17:32:01 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= write record"));
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2011-10-06 14:37:39 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2018-08-14 14:22:10 +02:00
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_hs_is_proper_fragment(mbedtls_ssl_context *ssl)
|
2011-01-13 18:54:59 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msglen < ssl->in_hslen ||
|
|
|
|
memcmp(ssl->in_msg + 6, "\0\0\0", 3) != 0 ||
|
|
|
|
memcmp(ssl->in_msg + 9, ssl->in_msg + 1, 3) != 0) {
|
|
|
|
return 1;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2011-01-13 18:54:59 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static uint32_t ssl_get_hs_frag_len(mbedtls_ssl_context const *ssl)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return (ssl->in_msg[9] << 16) |
|
|
|
|
(ssl->in_msg[10] << 8) |
|
|
|
|
ssl->in_msg[11];
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static uint32_t ssl_get_hs_frag_off(mbedtls_ssl_context const *ssl)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return (ssl->in_msg[6] << 16) |
|
|
|
|
(ssl->in_msg[7] << 8) |
|
|
|
|
ssl->in_msg[8];
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_check_hs_header(mbedtls_ssl_context const *ssl)
|
2014-09-17 10:47:43 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
uint32_t msg_len, frag_off, frag_len;
|
2015-05-06 16:38:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
msg_len = ssl_get_hs_total_len(ssl);
|
|
|
|
frag_off = ssl_get_hs_frag_off(ssl);
|
|
|
|
frag_len = ssl_get_hs_frag_len(ssl);
|
2018-08-20 10:37:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (frag_off > msg_len) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-09-17 10:47:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (frag_len > msg_len - frag_off) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-05-13 16:22:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (frag_len + 12 > ssl->in_msglen) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-05-12 20:55:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Mark bits in bitmask (used for DTLS HS reassembly)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static void ssl_bitmask_set(unsigned char *mask, size_t offset, size_t len)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
unsigned int start_bits, end_bits;
|
2013-08-02 15:34:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
start_bits = 8 - (offset % 8);
|
|
|
|
if (start_bits != 8) {
|
2020-02-05 15:44:10 +01:00
|
|
|
size_t first_byte_idx = offset / 8;
|
2013-08-02 15:34:52 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Special case */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (len <= start_bits) {
|
|
|
|
for (; len != 0; len--) {
|
|
|
|
mask[first_byte_idx] |= 1 << (start_bits - len);
|
|
|
|
}
|
2013-08-02 15:34:52 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Avoid potential issues with offset or len becoming invalid */
|
|
|
|
return;
|
|
|
|
}
|
2013-08-02 15:34:52 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
offset += start_bits; /* Now offset % 8 == 0 */
|
|
|
|
len -= start_bits;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (; start_bits != 0; start_bits--) {
|
|
|
|
mask[first_byte_idx] |= 1 << (start_bits - 1);
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2013-04-15 15:09:54 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
end_bits = len % 8;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (end_bits != 0) {
|
|
|
|
size_t last_byte_idx = (offset + len) / 8;
|
2013-04-15 15:09:54 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
len -= end_bits; /* Now len % 8 == 0 */
|
2013-04-15 15:09:54 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (; end_bits != 0; end_bits--) {
|
|
|
|
mask[last_byte_idx] |= 1 << (8 - end_bits);
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(mask + offset / 8, 0xFF, len / 8);
|
2015-06-17 10:58:20 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Check that bitmask is full
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_bitmask_check(unsigned char *mask, size_t len)
|
2013-09-23 14:46:13 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
size_t i;
|
2013-09-23 14:46:13 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < len / 8; i++) {
|
|
|
|
if (mask[i] != 0xFF) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2013-09-23 14:46:13 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < len % 8; i++) {
|
|
|
|
if ((mask[len / 8] & (1 << (7 - i))) == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2013-09-23 14:46:13 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2015-05-10 21:13:36 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* msg_len does not include the handshake header */
|
2023-01-11 14:50:10 +01:00
|
|
|
static size_t ssl_get_reassembly_buffer_size(size_t msg_len,
|
|
|
|
unsigned add_bitmap)
|
2015-05-10 21:13:36 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
size_t alloc_len;
|
2013-09-23 14:46:13 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
alloc_len = 12; /* Handshake header */
|
|
|
|
alloc_len += msg_len; /* Content buffer */
|
2019-03-27 17:54:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (add_bitmap) {
|
|
|
|
alloc_len += msg_len / 8 + (msg_len % 8 != 0); /* Bitmap */
|
2019-03-27 17:54:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
return alloc_len;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2012-09-27 21:15:01 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2015-05-11 08:46:37 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl)
|
2015-05-11 08:46:37 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return (ssl->in_msg[1] << 16) |
|
|
|
|
(ssl->in_msg[2] << 8) |
|
|
|
|
ssl->in_msg[3];
|
2015-05-11 08:46:37 +02:00
|
|
|
}
|
2015-06-19 12:16:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
|
2015-06-19 12:16:31 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msglen < mbedtls_ssl_hs_hdr_len(ssl)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("handshake message too short: %" MBEDTLS_PRINTF_SIZET,
|
|
|
|
ssl->in_msglen));
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_RECORD;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2015-05-10 23:10:37 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->in_hslen = mbedtls_ssl_hs_hdr_len(ssl) + ssl_get_hs_total_len(ssl);
|
2019-04-03 13:52:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("handshake message: msglen ="
|
|
|
|
" %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %"
|
|
|
|
MBEDTLS_PRINTF_SIZET,
|
|
|
|
ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen));
|
2015-09-15 12:43:43 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned int recv_msg_seq = (ssl->in_msg[4] << 8) | ssl->in_msg[5];
|
|
|
|
|
|
|
|
if (ssl_check_hs_header(ssl) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("invalid handshake header"));
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_RECORD;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl->handshake != NULL &&
|
|
|
|
((mbedtls_ssl_is_handshake_over(ssl) == 0 &&
|
|
|
|
recv_msg_seq != ssl->handshake->in_msg_seq) ||
|
|
|
|
(mbedtls_ssl_is_handshake_over(ssl) == 1 &&
|
|
|
|
ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO))) {
|
|
|
|
if (recv_msg_seq > ssl->handshake->in_msg_seq) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2,
|
|
|
|
(
|
|
|
|
"received future handshake message of sequence number %u (next %u)",
|
|
|
|
recv_msg_seq,
|
|
|
|
ssl->handshake->in_msg_seq));
|
|
|
|
return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Retransmit only on last message from previous flight, to avoid
|
|
|
|
* too many retransmissions.
|
|
|
|
* Besides, No sane server ever retransmits HelloVerifyRequest */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
|
|
|
|
ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("received message from last flight, "
|
|
|
|
"message_seq = %u, start_of_flight = %u",
|
|
|
|
recv_msg_seq,
|
|
|
|
ssl->handshake->in_flight_start_seq));
|
|
|
|
|
|
|
|
if ((ret = mbedtls_ssl_resend(ssl)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("dropping out-of-sequence message: "
|
|
|
|
"message_seq = %u, expected = %u",
|
|
|
|
recv_msg_seq,
|
|
|
|
ssl->handshake->in_msg_seq));
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
/* Wait until message completion to increment in_msg_seq */
|
2015-09-15 12:43:43 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Message reassembly is handled alongside buffering of future
|
|
|
|
* messages; the commonality is that both handshake fragments and
|
|
|
|
* future messages cannot be forwarded immediately to the
|
|
|
|
* handshake logic layer. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_hs_is_proper_fragment(ssl) == 1) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("found fragmented DTLS handshake message"));
|
|
|
|
return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
|
|
|
/* With TLS we don't handle fragmentation (for now) */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msglen < ssl->in_hslen) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("TLS handshake fragmentation not supported"));
|
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2015-09-15 12:43:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2015-09-15 12:43:43 +02:00
|
|
|
}
|
|
|
|
|
2023-02-06 00:34:21 +01:00
|
|
|
int mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl)
|
2018-10-22 16:31:26 +02:00
|
|
|
{
|
2023-02-06 00:34:21 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_ssl_handshake_params * const hs = ssl->handshake;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_is_handshake_over(ssl) == 0 && hs != NULL) {
|
2023-02-06 00:34:21 +01:00
|
|
|
ret = ssl->handshake->update_checksum(ssl, ssl->in_msg, ssl->in_hslen);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2015-08-27 16:37:35 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
/* Handshake message is complete, increment counter */
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
|
|
|
|
ssl->handshake != NULL) {
|
2020-02-05 15:44:10 +01:00
|
|
|
unsigned offset;
|
|
|
|
mbedtls_ssl_hs_buffer *hs_buf;
|
2017-07-05 15:25:21 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Increment handshake sequence number */
|
|
|
|
hs->in_msg_seq++;
|
2018-10-22 16:31:26 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Clear up handshake buffering and reassembly structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Free first entry */
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_buffering_free_slot(ssl, 0);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
/* Shift all other entries */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (offset = 0, hs_buf = &hs->buffering.hs[0];
|
2020-02-05 15:44:10 +01:00
|
|
|
offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
|
2023-01-11 14:50:10 +01:00
|
|
|
offset++, hs_buf++) {
|
2020-02-05 15:44:10 +01:00
|
|
|
*hs_buf = *(hs_buf + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a fresh last entry */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer));
|
2013-09-18 17:29:31 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2023-02-06 00:34:21 +01:00
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DTLS anti-replay: RFC 6347 4.1.2.6
|
|
|
|
*
|
|
|
|
* in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
|
|
|
|
* Bit n is set iff record number in_window_top - n has been seen.
|
|
|
|
*
|
|
|
|
* Usually, in_window_top is the last record number seen and the lsb of
|
|
|
|
* in_window is set. The only exception is the initial state (record number 0
|
|
|
|
* not seen yet).
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_dtls_replay_reset(mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
|
|
|
ssl->in_window_top = 0;
|
|
|
|
ssl->in_window = 0;
|
2018-10-22 16:31:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static inline uint64_t ssl_load_six_bytes(unsigned char *buf)
|
2018-10-22 16:31:26 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return ((uint64_t) buf[0] << 40) |
|
|
|
|
((uint64_t) buf[1] << 32) |
|
|
|
|
((uint64_t) buf[2] << 24) |
|
|
|
|
((uint64_t) buf[3] << 16) |
|
|
|
|
((uint64_t) buf[4] << 8) |
|
|
|
|
((uint64_t) buf[5]);
|
2013-09-18 17:29:31 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int mbedtls_ssl_dtls_record_replay_check(mbedtls_ssl_context *ssl, uint8_t *record_in_ctr)
|
2018-11-15 14:33:04 +01:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-02-05 15:44:10 +01:00
|
|
|
unsigned char *original_in_ctr;
|
2018-11-15 14:33:04 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
// save original in_ctr
|
|
|
|
original_in_ctr = ssl->in_ctr;
|
2019-11-20 14:54:36 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
// use counter from record
|
|
|
|
ssl->in_ctr = record_in_ctr;
|
2018-11-15 14:33:04 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_dtls_replay_check((mbedtls_ssl_context const *) ssl);
|
2018-11-15 14:33:04 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
// restore the counter
|
|
|
|
ssl->in_ctr = original_in_ctr;
|
2018-11-15 14:33:04 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return ret;
|
2018-10-22 16:31:26 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Return 0 if sequence number is acceptable, -1 otherwise
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_dtls_replay_check(mbedtls_ssl_context const *ssl)
|
2015-05-07 16:59:54 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
uint64_t rec_seqnum = ssl_load_six_bytes(ssl->in_ctr + 2);
|
2020-02-05 15:44:10 +01:00
|
|
|
uint64_t bit;
|
2015-05-07 16:59:54 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec_seqnum > ssl->in_window_top) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-07 16:59:54 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
bit = ssl->in_window_top - rec_seqnum;
|
2015-05-07 16:59:54 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (bit >= 64) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-05-07 16:59:54 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ssl->in_window & ((uint64_t) 1 << bit)) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-05-07 16:59:54 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2015-05-07 16:59:54 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Update replay window on new validated record
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_dtls_replay_update(mbedtls_ssl_context *ssl)
|
2018-10-22 16:31:26 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
uint64_t rec_seqnum = ssl_load_six_bytes(ssl->in_ctr + 2);
|
2018-10-22 16:31:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) {
|
2020-02-05 15:44:10 +01:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-10-22 16:31:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec_seqnum > ssl->in_window_top) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Update window_top and the contents of the window */
|
|
|
|
uint64_t shift = rec_seqnum - ssl->in_window_top;
|
2018-10-22 16:31:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (shift >= 64) {
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_window = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_window <<= shift;
|
|
|
|
ssl->in_window |= 1;
|
|
|
|
}
|
2018-10-22 16:31:26 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_window_top = rec_seqnum;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Mark that number as seen in the current window */
|
|
|
|
uint64_t bit = ssl->in_window_top - rec_seqnum;
|
2018-10-22 16:31:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (bit < 64) { /* Always true, but be extra sure */
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_window |= (uint64_t) 1 << bit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2013-04-16 18:05:29 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
|
2017-10-04 16:28:46 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
/*
|
2022-02-15 23:53:36 +01:00
|
|
|
* Check if a datagram looks like a ClientHello with a valid cookie,
|
|
|
|
* and if it doesn't, generate a HelloVerifyRequest message.
|
2020-02-05 15:44:10 +01:00
|
|
|
* Both input and output include full DTLS headers.
|
|
|
|
*
|
|
|
|
* - if cookie is valid, return 0
|
|
|
|
* - if ClientHello looks superficially valid but cookie is not,
|
|
|
|
* fill obuf and set olen, then
|
|
|
|
* return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
|
|
|
|
* - otherwise return a specific error code
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2022-06-08 17:47:33 +02:00
|
|
|
MBEDTLS_STATIC_TESTABLE
|
|
|
|
int mbedtls_ssl_check_dtls_clihlo_cookie(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *cli_id, size_t cli_id_len,
|
|
|
|
const unsigned char *in, size_t in_len,
|
|
|
|
unsigned char *obuf, size_t buf_len, size_t *olen)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2022-06-15 13:17:28 +02:00
|
|
|
size_t sid_len, cookie_len, epoch, fragment_offset;
|
2020-02-05 15:44:10 +01:00
|
|
|
unsigned char *p;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Structure of ClientHello with record and handshake headers,
|
|
|
|
* and expected values. We don't need to check a lot, more checks will be
|
|
|
|
* done when actually parsing the ClientHello - skipping those checks
|
|
|
|
* avoids code duplication and does not make cookie forging any easier.
|
|
|
|
*
|
|
|
|
* 0-0 ContentType type; copied, must be handshake
|
|
|
|
* 1-2 ProtocolVersion version; copied
|
|
|
|
* 3-4 uint16 epoch; copied, must be 0
|
|
|
|
* 5-10 uint48 sequence_number; copied
|
|
|
|
* 11-12 uint16 length; (ignored)
|
|
|
|
*
|
|
|
|
* 13-13 HandshakeType msg_type; (ignored)
|
|
|
|
* 14-16 uint24 length; (ignored)
|
|
|
|
* 17-18 uint16 message_seq; copied
|
|
|
|
* 19-21 uint24 fragment_offset; copied, must be 0
|
|
|
|
* 22-24 uint24 fragment_length; (ignored)
|
|
|
|
*
|
|
|
|
* 25-26 ProtocolVersion client_version; (ignored)
|
|
|
|
* 27-58 Random random; (ignored)
|
|
|
|
* 59-xx SessionID session_id; 1 byte len + sid_len content
|
|
|
|
* 60+ opaque cookie<0..2^8-1>; 1 byte len + content
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* Minimum length is 61 bytes.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: in_len=%u",
|
|
|
|
(unsigned) in_len));
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "cli_id", cli_id, cli_id_len);
|
|
|
|
if (in_len < 61) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: record too short"));
|
|
|
|
return MBEDTLS_ERR_SSL_DECODE_ERROR;
|
2022-02-15 23:53:36 +01:00
|
|
|
}
|
2022-06-15 13:17:28 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
epoch = MBEDTLS_GET_UINT16_BE(in, 3);
|
|
|
|
fragment_offset = MBEDTLS_GET_UINT24_BE(in, 19);
|
2022-06-15 13:17:28 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || epoch != 0 ||
|
|
|
|
fragment_offset != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: not a good ClientHello"));
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, (" type=%u epoch=%u fragment_offset=%u",
|
|
|
|
in[0], (unsigned) epoch,
|
|
|
|
(unsigned) fragment_offset));
|
|
|
|
return MBEDTLS_ERR_SSL_DECODE_ERROR;
|
2015-05-06 18:33:07 +02:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
sid_len = in[59];
|
2023-01-11 14:50:10 +01:00
|
|
|
if (59 + 1 + sid_len + 1 > in_len) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: sid_len=%u > %u",
|
|
|
|
(unsigned) sid_len,
|
|
|
|
(unsigned) in_len - 61));
|
|
|
|
return MBEDTLS_ERR_SSL_DECODE_ERROR;
|
2022-02-15 23:53:36 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "sid received from network",
|
|
|
|
in + 60, sid_len);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
cookie_len = in[60 + sid_len];
|
2023-01-11 14:50:10 +01:00
|
|
|
if (59 + 1 + sid_len + 1 + cookie_len > in_len) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: cookie_len=%u > %u",
|
|
|
|
(unsigned) cookie_len,
|
|
|
|
(unsigned) (in_len - sid_len - 61)));
|
|
|
|
return MBEDTLS_ERR_SSL_DECODE_ERROR;
|
2022-02-15 23:53:36 +01:00
|
|
|
}
|
2017-10-04 16:29:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "cookie received from network",
|
|
|
|
in + sid_len + 61, cookie_len);
|
|
|
|
if (ssl->conf->f_cookie_check(ssl->conf->p_cookie,
|
|
|
|
in + sid_len + 61, cookie_len,
|
|
|
|
cli_id, cli_id_len) == 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: valid"));
|
|
|
|
return 0;
|
2017-10-04 16:29:08 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* If we get here, we've got an invalid cookie, let's prepare HVR.
|
|
|
|
*
|
|
|
|
* 0-0 ContentType type; copied
|
|
|
|
* 1-2 ProtocolVersion version; copied
|
|
|
|
* 3-4 uint16 epoch; copied
|
|
|
|
* 5-10 uint48 sequence_number; copied
|
|
|
|
* 11-12 uint16 length; olen - 13
|
|
|
|
*
|
|
|
|
* 13-13 HandshakeType msg_type; hello_verify_request
|
|
|
|
* 14-16 uint24 length; olen - 25
|
|
|
|
* 17-18 uint16 message_seq; copied
|
|
|
|
* 19-21 uint24 fragment_offset; copied
|
|
|
|
* 22-24 uint24 fragment_length; olen - 25
|
|
|
|
*
|
|
|
|
* 25-26 ProtocolVersion server_version; 0xfe 0xff
|
|
|
|
* 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
|
|
|
|
*
|
|
|
|
* Minimum length is 28.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (buf_len < 28) {
|
|
|
|
return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Copy most fields and adapt others */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(obuf, in, 25);
|
2020-02-05 15:44:10 +01:00
|
|
|
obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
|
|
|
|
obuf[25] = 0xfe;
|
|
|
|
obuf[26] = 0xff;
|
2011-01-06 16:48:19 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Generate and write actual cookie */
|
|
|
|
p = obuf + 28;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->f_cookie_write(ssl->conf->p_cookie,
|
|
|
|
&p, obuf + buf_len,
|
|
|
|
cli_id, cli_id_len) != 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2015-05-06 18:33:07 +02:00
|
|
|
}
|
2011-01-06 16:48:19 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
*olen = p - obuf;
|
2011-01-06 16:48:19 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Go back and fill length fields */
|
2023-01-11 14:50:10 +01:00
|
|
|
obuf[27] = (unsigned char) (*olen - 28);
|
2015-06-11 14:49:42 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
obuf[14] = obuf[22] = MBEDTLS_BYTE_2(*olen - 25);
|
|
|
|
obuf[15] = obuf[23] = MBEDTLS_BYTE_1(*olen - 25);
|
|
|
|
obuf[16] = obuf[24] = MBEDTLS_BYTE_0(*olen - 25);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(*olen - 13, obuf, 11);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED;
|
2015-06-17 12:43:26 +02:00
|
|
|
}
|
|
|
|
|
2014-02-04 15:52:33 +01:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Handle possible client reconnect with the same UDP quadruplet
|
|
|
|
* (RFC 6347 Section 4.2.8).
|
|
|
|
*
|
|
|
|
* Called by ssl_parse_record_header() in case we receive an epoch 0 record
|
|
|
|
* that looks like a ClientHello.
|
|
|
|
*
|
|
|
|
* - if the input looks like a ClientHello without cookies,
|
Fix lack of cookie check on hard reconnect
Section 4.2.8 of RFC 6347 describes how to handle the case of a DTLS client
establishing a new connection using the same UDP quartet as an already active
connection, which we implement under the compile option
MBEDTLS_SSL_DLTS_CLIENT_PORT_REUSE. Relevant excerpts:
[the server] MUST NOT destroy the existing
association until the client has demonstrated reachability either by
completing a cookie exchange or by completing a complete handshake
including delivering a verifiable Finished message.
[...]
The reachability requirement prevents
off-path/blind attackers from destroying associations merely by
sending forged ClientHellos.
Our code chooses to use a cookie exchange for establishing reachability, but
unfortunately that check was effectively removed in a recent refactoring,
which changed what value ssl_handle_possible_reconnect() needs to return in
order for ssl_get_next_record() (introduced in that refactoring) to take the
proper action. Unfortunately, in addition to changing the value, the
refactoring also changed a return statement to an assignment to the ret
variable, causing the function to reach the code for a valid cookie, which
immediately destroys the existing association, effectively bypassing the
cookie verification.
This commit fixes that by immediately returning after sending a
HelloVerifyRequest when a ClientHello without a valid cookie is found. It also
updates the description of the function to reflect the new return value
convention (the refactoring updated the code but not the documentation).
The commit that changed the return value convention (and introduced the bug)
is 2fddd3765ea998bb9f40b52dc1baaf843b9889bf, whose commit message explains the
change.
Note: this bug also indirectly caused the ssl-opt.sh test case "DTLS client
reconnect from same port: reconnect" to occasionally fail due to a race
condition between the reception of the ClientHello carrying a valid cookie and
the closure of the connection by the server after noticing the ClientHello
didn't carry a valid cookie after it incorrectly destroyed the previous
connection, that could cause that ClientHello to be invisible to the server
(if that message reaches the server just before it does `net_close()`). A
welcome side effect of this commit is to remove that race condition, as the
new connection will immediately start with a ClientHello carrying a valid
cookie in the SSL input buffer, so the server will not call `net_close()` and
not risk discarding a better ClientHello that arrived in the meantime.
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2020-03-11 12:51:42 +01:00
|
|
|
* send back HelloVerifyRequest, then return 0
|
2020-02-05 15:44:10 +01:00
|
|
|
* - if the input looks like a ClientHello with a valid cookie,
|
|
|
|
* reset the session of the current context, and
|
|
|
|
* return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
|
|
|
|
* - if anything goes wrong, return a specific error code
|
|
|
|
*
|
Fix lack of cookie check on hard reconnect
Section 4.2.8 of RFC 6347 describes how to handle the case of a DTLS client
establishing a new connection using the same UDP quartet as an already active
connection, which we implement under the compile option
MBEDTLS_SSL_DLTS_CLIENT_PORT_REUSE. Relevant excerpts:
[the server] MUST NOT destroy the existing
association until the client has demonstrated reachability either by
completing a cookie exchange or by completing a complete handshake
including delivering a verifiable Finished message.
[...]
The reachability requirement prevents
off-path/blind attackers from destroying associations merely by
sending forged ClientHellos.
Our code chooses to use a cookie exchange for establishing reachability, but
unfortunately that check was effectively removed in a recent refactoring,
which changed what value ssl_handle_possible_reconnect() needs to return in
order for ssl_get_next_record() (introduced in that refactoring) to take the
proper action. Unfortunately, in addition to changing the value, the
refactoring also changed a return statement to an assignment to the ret
variable, causing the function to reach the code for a valid cookie, which
immediately destroys the existing association, effectively bypassing the
cookie verification.
This commit fixes that by immediately returning after sending a
HelloVerifyRequest when a ClientHello without a valid cookie is found. It also
updates the description of the function to reflect the new return value
convention (the refactoring updated the code but not the documentation).
The commit that changed the return value convention (and introduced the bug)
is 2fddd3765ea998bb9f40b52dc1baaf843b9889bf, whose commit message explains the
change.
Note: this bug also indirectly caused the ssl-opt.sh test case "DTLS client
reconnect from same port: reconnect" to occasionally fail due to a race
condition between the reception of the ClientHello carrying a valid cookie and
the closure of the connection by the server after noticing the ClientHello
didn't carry a valid cookie after it incorrectly destroyed the previous
connection, that could cause that ClientHello to be invisible to the server
(if that message reaches the server just before it does `net_close()`). A
welcome side effect of this commit is to remove that race condition, as the
new connection will immediately start with a ClientHello carrying a valid
cookie in the SSL input buffer, so the server will not call `net_close()` and
not risk discarding a better ClientHello that arrived in the meantime.
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2020-03-11 12:51:42 +01:00
|
|
|
* This function is called (through ssl_check_client_reconnect()) when an
|
|
|
|
* unexpected record is found in ssl_get_next_record(), which will discard the
|
|
|
|
* record if we return 0, and bubble up the return value otherwise (this
|
|
|
|
* includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
|
|
|
|
* errors, and is the right thing to do in both cases).
|
2014-02-04 15:52:33 +01:00
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_handle_possible_reconnect(mbedtls_ssl_context *ssl)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t len;
|
2015-05-06 11:47:06 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->f_cookie_write == NULL ||
|
|
|
|
ssl->conf->f_cookie_check == NULL) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* If we can't use cookies to verify reachability of the peer,
|
|
|
|
* drop the record. */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("no cookie callbacks, "
|
|
|
|
"can't check reconnect validity"));
|
|
|
|
return 0;
|
2017-04-07 14:25:49 +02:00
|
|
|
}
|
2013-08-19 14:25:29 +02:00
|
|
|
|
2022-06-08 17:47:33 +02:00
|
|
|
ret = mbedtls_ssl_check_dtls_clihlo_cookie(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl,
|
|
|
|
ssl->cli_id, ssl->cli_id_len,
|
|
|
|
ssl->in_buf, ssl->in_left,
|
|
|
|
ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len);
|
2015-09-27 23:50:49 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_ssl_check_dtls_clihlo_cookie", ret);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) {
|
2020-03-31 12:07:47 +02:00
|
|
|
int send_ret;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("sending HelloVerifyRequest"));
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network",
|
|
|
|
ssl->out_buf, len);
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Don't check write errors as we can't do anything here.
|
|
|
|
* If the error is permanent we'll catch it later,
|
|
|
|
* if it's not, then hopefully it'll work next time. */
|
2023-01-11 14:50:10 +01:00
|
|
|
send_ret = ssl->f_send(ssl->p_bio, ssl->out_buf, len);
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", send_ret);
|
2020-03-31 12:07:47 +02:00
|
|
|
(void) send_ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2017-04-07 14:25:49 +02:00
|
|
|
}
|
2013-04-16 13:15:56 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("cookie is valid, resetting context"));
|
|
|
|
if ((ret = mbedtls_ssl_session_reset_int(ssl, 1)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "reset", ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2017-04-07 14:25:49 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_CLIENT_RECONNECT;
|
2017-04-07 14:25:49 +02:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2012-09-27 23:49:42 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
|
2012-09-27 23:49:42 +02:00
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_check_record_type(uint8_t record_type)
|
2014-04-04 16:08:41 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
|
2020-02-05 15:44:10 +01:00
|
|
|
record_type != MBEDTLS_SSL_MSG_ALERT &&
|
|
|
|
record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
|
2023-01-11 14:50:10 +01:00
|
|
|
record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_RECORD;
|
2014-04-07 10:57:45 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2014-04-04 16:08:41 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* ContentType type;
|
|
|
|
* ProtocolVersion version;
|
|
|
|
* uint16 epoch; // DTLS only
|
|
|
|
* uint48 sequence_number; // DTLS only
|
|
|
|
* uint16 length;
|
|
|
|
*
|
|
|
|
* Return 0 if header looks sane (and, for DTLS, the record is expected)
|
|
|
|
* MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
|
|
|
|
* MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
|
|
|
|
*
|
|
|
|
* With DTLS, mbedtls_ssl_read_record() will:
|
|
|
|
* 1. proceed with the record if this function returns 0
|
|
|
|
* 2. drop only the current record if this function returns UNEXPECTED_RECORD
|
|
|
|
* 3. return CLIENT_RECONNECT if this function return that value
|
|
|
|
* 4. drop the whole datagram if this function returns anything else.
|
|
|
|
* Point 2 is needed when the peer is resending, and we have already received
|
|
|
|
* the first record from a datagram but are still waiting for the others.
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_parse_record_header(mbedtls_ssl_context const *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
size_t len,
|
|
|
|
mbedtls_record *rec)
|
2014-02-10 14:25:10 +01:00
|
|
|
{
|
2022-03-15 08:23:42 +01:00
|
|
|
mbedtls_ssl_protocol_version tls_version;
|
2011-10-06 15:04:09 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
size_t const rec_hdr_type_offset = 0;
|
|
|
|
size_t const rec_hdr_type_len = 1;
|
2012-09-28 15:28:45 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
size_t const rec_hdr_version_offset = rec_hdr_type_offset +
|
|
|
|
rec_hdr_type_len;
|
|
|
|
size_t const rec_hdr_version_len = 2;
|
2014-10-20 13:34:59 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
size_t const rec_hdr_ctr_len = 8;
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
uint32_t rec_epoch;
|
|
|
|
size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
|
|
|
|
rec_hdr_version_len;
|
2017-04-10 13:42:31 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
|
|
|
size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
|
|
|
|
rec_hdr_ctr_len;
|
|
|
|
size_t rec_hdr_cid_len = 0;
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2014-10-27 13:57:03 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
size_t rec_hdr_len_offset; /* To be determined */
|
|
|
|
size_t const rec_hdr_len_len = 2;
|
2014-10-20 18:40:56 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Check minimum lengths for record header.
|
|
|
|
*/
|
2015-01-12 13:43:29 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
2020-02-05 15:44:10 +01:00
|
|
|
rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
|
|
|
{
|
|
|
|
rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
|
2013-07-18 12:32:27 +02:00
|
|
|
}
|
2013-07-17 14:33:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (len < rec_hdr_len_offset + rec_hdr_len_len) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1,
|
|
|
|
(
|
|
|
|
"datagram of length %u too small to hold DTLS record header of length %u",
|
|
|
|
(unsigned) len,
|
|
|
|
(unsigned) (rec_hdr_len_len + rec_hdr_len_len)));
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_RECORD;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2014-07-03 19:29:16 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Parse and validate record content type
|
|
|
|
*/
|
2013-08-14 16:52:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
rec->type = buf[rec_hdr_type_offset];
|
2013-08-03 13:02:31 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Check record content type */
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
|
|
|
rec->cid_len = 0;
|
2019-05-07 17:33:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->conf->cid_len != 0 &&
|
2023-01-11 14:50:10 +01:00
|
|
|
rec->type == MBEDTLS_SSL_MSG_CID) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Shift pointers to account for record header including CID
|
|
|
|
* struct {
|
2021-10-12 09:22:33 +02:00
|
|
|
* ContentType outer_type = tls12_cid;
|
2020-02-05 15:44:10 +01:00
|
|
|
* ProtocolVersion version;
|
|
|
|
* uint16 epoch;
|
|
|
|
* uint48 sequence_number;
|
|
|
|
* opaque cid[cid_length]; // Additional field compared to
|
|
|
|
* // default DTLS record format
|
|
|
|
* uint16 length;
|
|
|
|
* opaque enc_content[DTLSCiphertext.length];
|
|
|
|
* } DTLSCiphertext;
|
|
|
|
*/
|
2015-10-02 14:33:37 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* So far, we only support static CID lengths
|
|
|
|
* fixed in the configuration. */
|
|
|
|
rec_hdr_cid_len = ssl->conf->cid_len;
|
|
|
|
rec_hdr_len_offset += rec_hdr_cid_len;
|
2018-04-25 20:39:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (len < rec_hdr_len_offset + rec_hdr_len_len) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1,
|
|
|
|
(
|
|
|
|
"datagram of length %u too small to hold DTLS record header including CID, length %u",
|
|
|
|
(unsigned) len,
|
|
|
|
(unsigned) (rec_hdr_len_offset + rec_hdr_len_len)));
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_RECORD;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2018-04-26 11:46:10 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* configured CID len is guaranteed at most 255, see
|
|
|
|
* MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
|
|
|
|
rec->cid_len = (uint8_t) rec_hdr_cid_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len);
|
|
|
|
} else
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_check_record_type(rec->type)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type %u",
|
|
|
|
(unsigned) rec->type));
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_RECORD;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2017-10-10 12:51:19 +02:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Parse and validate record version
|
2017-10-10 12:51:19 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
rec->ver[0] = buf[rec_hdr_version_offset + 0];
|
|
|
|
rec->ver[1] = buf[rec_hdr_version_offset + 1];
|
2023-07-13 14:12:40 +02:00
|
|
|
tls_version = (mbedtls_ssl_protocol_version) mbedtls_ssl_read_version(
|
|
|
|
buf + rec_hdr_version_offset,
|
|
|
|
ssl->conf->transport);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (tls_version > ssl->conf->max_tls_version) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("TLS version mismatch: got %u, expected max %u",
|
|
|
|
(unsigned) tls_version,
|
|
|
|
(unsigned) ssl->conf->max_tls_version));
|
2022-02-15 23:53:36 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INVALID_RECORD;
|
2017-10-10 12:51:19 +02:00
|
|
|
}
|
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Parse/Copy record sequence number.
|
2017-10-10 12:51:19 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Copy explicit record sequence number from input buffer. */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(&rec->ctr[0], buf + rec_hdr_ctr_offset,
|
|
|
|
rec_hdr_ctr_len);
|
|
|
|
} else
|
2017-10-10 12:51:19 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
|
|
|
/* Copy implicit record sequence number from SSL context structure. */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(&rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len);
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2017-10-10 12:51:19 +02:00
|
|
|
|
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Parse record length.
|
2017-10-10 12:51:19 +02:00
|
|
|
*/
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
rec->data_len = ((size_t) buf[rec_hdr_len_offset + 0] << 8) |
|
|
|
|
((size_t) buf[rec_hdr_len_offset + 1] << 0);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "input record header", buf, rec->data_offset);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("input record: msgtype = %u, "
|
|
|
|
"version = [0x%x], msglen = %" MBEDTLS_PRINTF_SIZET,
|
|
|
|
rec->type, (unsigned) tls_version, rec->data_len));
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
rec->buf = buf;
|
|
|
|
rec->buf_len = rec->data_offset + rec->data_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec->data_len == 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_RECORD;
|
|
|
|
}
|
2017-10-10 12:51:19 +02:00
|
|
|
|
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* DTLS-related tests.
|
|
|
|
* Check epoch before checking length constraint because
|
|
|
|
* the latter varies with the epoch. E.g., if a ChangeCipherSpec
|
|
|
|
* message gets duplicated before the corresponding Finished message,
|
|
|
|
* the second ChangeCipherSpec should be discarded because it belongs
|
|
|
|
* to an old epoch, but not because its length is shorter than
|
|
|
|
* the minimum record length for packets using the new record transform.
|
|
|
|
* Note that these two kinds of failures are handled differently,
|
|
|
|
* as an unexpected record is silently skipped but an invalid
|
|
|
|
* record leads to the entire datagram being dropped.
|
2017-10-10 12:51:19 +02:00
|
|
|
*/
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
|
|
|
rec_epoch = (rec->ctr[0] << 8) | rec->ctr[1];
|
2017-10-10 12:51:19 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Check that the datagram is large enough to contain a record
|
|
|
|
* of the advertised length. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (len < rec->data_offset + rec->data_len) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1,
|
|
|
|
(
|
|
|
|
"Datagram of length %u too small to contain record of advertised length %u.",
|
|
|
|
(unsigned) len,
|
|
|
|
(unsigned) (rec->data_offset + rec->data_len)));
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_RECORD;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Records from other, non-matching epochs are silently discarded.
|
|
|
|
* (The case of same-port Client reconnects must be considered in
|
|
|
|
* the caller). */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec_epoch != ssl->in_epoch) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("record from another epoch: "
|
|
|
|
"expected %u, received %lu",
|
|
|
|
ssl->in_epoch, (unsigned long) rec_epoch));
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
/* Records from the next epoch are considered for buffering
|
|
|
|
* (concretely: early Finished messages). */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec_epoch == (unsigned) ssl->in_epoch + 1) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("Consider record for buffering"));
|
|
|
|
return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
|
|
|
|
/* For records from the correct epoch, check whether their
|
|
|
|
* sequence number has been seen before. */
|
2023-01-11 14:50:10 +01:00
|
|
|
else if (mbedtls_ssl_dtls_record_replay_check((mbedtls_ssl_context *) ssl,
|
|
|
|
&rec->ctr[0]) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("replayed record"));
|
|
|
|
return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2017-10-10 12:51:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2017-10-10 12:51:19 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_check_client_reconnect(mbedtls_ssl_context *ssl)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned int rec_epoch = (ssl->in_ctr[0] << 8) | ssl->in_ctr[1];
|
2015-01-23 15:30:57 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Check for an epoch 0 ClientHello. We can't use in_msg here to
|
|
|
|
* access the first byte of record content (handshake type), as we
|
|
|
|
* have an active transform (possibly iv_len != 0), so use the
|
|
|
|
* fact that the record header len is 13 instead.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec_epoch == 0 &&
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_is_handshake_over(ssl) == 1 &&
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
|
|
|
|
ssl->in_left > 13 &&
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("possible client reconnect "
|
|
|
|
"from the same port"));
|
|
|
|
return ssl_handle_possible_reconnect(ssl);
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2015-01-23 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* If applicable, decrypt record content
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_prepare_record_content(mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_record *rec)
|
2011-01-16 22:27:44 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret, done = 0;
|
2013-03-06 10:23:34 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "input record from network",
|
|
|
|
rec->buf, rec->buf_len);
|
2011-01-16 22:27:44 +01:00
|
|
|
|
2021-11-24 12:43:39 +01:00
|
|
|
/*
|
|
|
|
* In TLS 1.3, always treat ChangeCipherSpec records
|
|
|
|
* as unencrypted. The only thing we do with them is
|
|
|
|
* check the length and content and ignore them.
|
|
|
|
*/
|
2021-12-08 16:57:54 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->transform_in != NULL &&
|
|
|
|
ssl->transform_in->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
|
|
|
|
if (rec->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
|
2021-11-24 12:43:39 +01:00
|
|
|
done = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-11-24 12:43:39 +01:00
|
|
|
}
|
2021-12-08 16:57:54 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
2021-11-24 12:43:39 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!done && ssl->transform_in != NULL) {
|
2020-02-05 15:44:10 +01:00
|
|
|
unsigned char const old_msg_type = rec->type;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in,
|
|
|
|
rec)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_decrypt_buf", ret);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->conf->ignore_unexpected_cid
|
2023-01-11 14:50:10 +01:00
|
|
|
== MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("ignoring unexpected CID"));
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (old_msg_type != rec->type) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("record type after decrypt (before %d): %d",
|
|
|
|
old_msg_type, rec->type));
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "input payload after decrypt",
|
|
|
|
rec->buf + rec->data_offset, rec->data_len);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
|
|
|
/* We have already checked the record content type
|
|
|
|
* in ssl_parse_record_header(), failing or silently
|
|
|
|
* dropping the record in the case of an unknown type.
|
|
|
|
*
|
|
|
|
* Since with the use of CIDs, the record content type
|
|
|
|
* might change during decryption, re-check the record
|
|
|
|
* content type, but treat a failure as fatal this time. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_check_record_type(rec->type)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type"));
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_RECORD;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec->data_len == 0) {
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2
|
|
|
|
&& rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* TLS v1.2 explicitly disallows zero-length messages which are not application data */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("invalid zero-length message type: %d", ssl->in_msgtype));
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_RECORD;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
|
|
|
|
|
|
|
ssl->nb_zero++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Three or more empty messages may be a DoS attack
|
|
|
|
* (excessive CPU consumption).
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->nb_zero > 3) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("received four consecutive empty "
|
|
|
|
"messages, possible DoS attack"));
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Treat the records as if they were not properly authenticated,
|
|
|
|
* thereby failing the connection if we see more than allowed
|
|
|
|
* by the configured bad MAC threshold. */
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INVALID_MAC;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->nb_zero = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2014-02-10 13:43:33 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
2020-02-05 15:44:10 +01:00
|
|
|
; /* in_ctr read from peer, not maintained internally */
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
unsigned i;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
|
|
|
|
i > mbedtls_ssl_ep_len(ssl); i--) {
|
|
|
|
if (++ssl->in_ctr[i - 1] != 0) {
|
2020-02-05 15:44:10 +01:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-10-08 09:21:19 +02:00
|
|
|
}
|
2014-02-10 13:43:33 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* The loop goes to its end iff the counter is wrapping */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (i == mbedtls_ssl_ep_len(ssl)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("incoming message counter would wrap"));
|
|
|
|
return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2014-02-10 13:43:33 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2014-02-10 13:43:33 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
|
|
|
mbedtls_ssl_dtls_replay_update(ssl);
|
2011-01-15 18:35:19 +01:00
|
|
|
}
|
2014-10-14 17:47:31 +02:00
|
|
|
#endif
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Check actual (decrypted) record content length against
|
|
|
|
* configured maximum. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec->data_len > MBEDTLS_SSL_IN_CONTENT_LEN) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("bad message length"));
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_RECORD;
|
2014-10-14 17:47:31 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2014-10-14 17:47:31 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Read a record.
|
|
|
|
*
|
|
|
|
* Silently ignore non-fatal alert (and for DTLS, invalid records as well,
|
|
|
|
* RFC 6347 4.1.2.7) and continue reading until a valid record is found.
|
|
|
|
*
|
|
|
|
*/
|
2015-08-31 18:30:52 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Helper functions for mbedtls_ssl_read_record(). */
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_consume_current_message(mbedtls_ssl_context *ssl);
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_get_next_record(mbedtls_ssl_context *ssl);
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl);
|
2015-08-31 18:30:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_read_record(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned update_hs_digest)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-08-31 18:30:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> read record"));
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->keep_current_message == 0) {
|
2020-02-05 15:44:10 +01:00
|
|
|
do {
|
2017-09-19 13:00:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_consume_current_message(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2015-08-31 18:30:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_record_is_in_progress(ssl) == 0) {
|
2022-10-06 19:31:25 +02:00
|
|
|
int dtls_have_buffered = 0;
|
2018-08-20 09:34:02 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* We only check for buffered messages if the
|
|
|
|
* current datagram is fully consumed. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
|
|
|
|
ssl_next_record_is_in_datagram(ssl) == 0) {
|
|
|
|
if (ssl_load_buffered_message(ssl) == 0) {
|
2022-10-06 19:31:25 +02:00
|
|
|
dtls_have_buffered = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2018-08-20 09:34:02 +02:00
|
|
|
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (dtls_have_buffered == 0) {
|
|
|
|
ret = ssl_get_next_record(ssl);
|
|
|
|
if (ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING) {
|
2020-02-05 15:44:10 +01:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-08-20 09:34:02 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, ("ssl_get_next_record"), ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-21 13:49:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_handle_message_type(ssl);
|
2018-08-21 11:20:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Buffer future message */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_buffer_message(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2017-09-21 13:49:50 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2017-09-21 13:49:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
} while (MBEDTLS_ERR_SSL_NON_FATAL == ret ||
|
|
|
|
MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret);
|
2017-09-21 13:49:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (0 != ret) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_handle_message_type"), ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2017-09-21 13:49:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
|
|
|
|
update_hs_digest == 1) {
|
2023-02-06 00:34:21 +01:00
|
|
|
ret = mbedtls_ssl_update_handshake_status(ssl);
|
|
|
|
if (0 != ret) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_update_handshake_status"), ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-09-21 13:49:50 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("reuse previously read message"));
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->keep_current_message = 0;
|
2013-07-30 12:41:56 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= read record"));
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2013-07-30 12:41:56 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl)
|
2019-05-20 11:12:28 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_left > ssl->next_record_offset) {
|
|
|
|
return 1;
|
|
|
|
}
|
2019-05-20 11:12:28 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2019-05-20 11:12:28 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_load_buffered_message(mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
|
|
|
mbedtls_ssl_handshake_params * const hs = ssl->handshake;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_hs_buffer *hs_buf;
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret = 0;
|
2019-05-16 13:50:45 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hs == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2019-05-16 13:50:45 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_message"));
|
2019-05-16 13:50:45 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
|
|
|
|
ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Check if we have seen a ChangeCipherSpec before.
|
|
|
|
* If yes, synthesize a CCS record. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!hs->buffering.seen_ccs) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("CCS not seen in the current flight"));
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = -1;
|
|
|
|
goto exit;
|
|
|
|
}
|
2019-05-16 13:50:45 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("Injecting buffered CCS message"));
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
|
|
|
|
ssl->in_msglen = 1;
|
|
|
|
ssl->in_msg[0] = 1;
|
2019-05-16 13:50:45 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* As long as they are equal, the exact value doesn't matter. */
|
|
|
|
ssl->in_left = 0;
|
|
|
|
ssl->next_record_offset = 0;
|
2019-05-16 13:39:07 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
hs->buffering.seen_ccs = 0;
|
|
|
|
goto exit;
|
|
|
|
}
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_DEBUG_C)
|
|
|
|
/* Debug only */
|
|
|
|
{
|
|
|
|
unsigned offset;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) {
|
2020-02-05 15:44:10 +01:00
|
|
|
hs_buf = &hs->buffering.hs[offset];
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hs_buf->is_valid == 1) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("Future message with sequence number %u %s buffered.",
|
|
|
|
hs->in_msg_seq + offset,
|
|
|
|
hs_buf->is_complete ? "fully" : "partially"));
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_DEBUG_C */
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Check if we have buffered and/or fully reassembled the
|
|
|
|
* next handshake message. */
|
|
|
|
hs_buf = &hs->buffering.hs[0];
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((hs_buf->is_valid == 1) && (hs_buf->is_complete == 1)) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Synthesize a record containing the buffered HS message. */
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t msg_len = (hs_buf->data[1] << 16) |
|
|
|
|
(hs_buf->data[2] << 8) |
|
|
|
|
hs_buf->data[3];
|
2019-05-16 13:39:07 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Double-check that we haven't accidentally buffered
|
|
|
|
* a message that doesn't fit into the input buffer. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2019-07-23 16:52:45 +02:00
|
|
|
}
|
2019-05-16 13:39:07 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("Next handshake message has been buffered - load"));
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "Buffered handshake message (incl. header)",
|
|
|
|
hs_buf->data, msg_len + 12);
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
|
|
|
|
ssl->in_hslen = msg_len + 12;
|
|
|
|
ssl->in_msglen = msg_len + 12;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(ssl->in_msg, hs_buf->data, ssl->in_hslen);
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = 0;
|
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("Next handshake message %u not or only partially bufffered",
|
|
|
|
hs->in_msg_seq));
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = -1;
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
exit:
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_message"));
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_buffer_make_space(mbedtls_ssl_context *ssl,
|
|
|
|
size_t desired)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
mbedtls_ssl_handshake_params * const hs = ssl->handshake;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("Attempt to free buffered messages to have %u bytes available",
|
|
|
|
(unsigned) desired));
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Get rid of future records epoch first, if such exist. */
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_free_buffered_record(ssl);
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Check if we have enough space available now. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
|
|
|
|
hs->buffering.total_bytes_buffered)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("Enough space available after freeing future epoch record"));
|
|
|
|
return 0;
|
2019-05-24 12:06:29 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
/* We don't have enough space to buffer the next expected handshake
|
|
|
|
* message. Remove buffers used for future messages to gain space,
|
|
|
|
* starting with the most distant one. */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
|
|
|
|
offset >= 0; offset--) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2,
|
|
|
|
(
|
|
|
|
"Free buffering slot %d to make space for reassembly of next handshake message",
|
|
|
|
offset));
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_buffering_free_slot(ssl, (uint8_t) offset);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
/* Check if we have enough space available now. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
|
|
|
|
hs->buffering.total_bytes_buffered)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("Enough space available after freeing buffered HS messages"));
|
|
|
|
return 0;
|
2019-05-24 12:06:29 +02:00
|
|
|
}
|
2019-05-21 11:01:32 +02:00
|
|
|
}
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return -1;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_buffer_message(mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
mbedtls_ssl_handshake_params * const hs = ssl->handshake;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hs == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_buffer_message"));
|
2019-05-16 11:11:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (ssl->in_msgtype) {
|
2020-02-05 15:44:10 +01:00
|
|
|
case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("Remember CCS message"));
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
hs->buffering.seen_ccs = 1;
|
|
|
|
break;
|
2019-05-21 11:01:32 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
case MBEDTLS_SSL_MSG_HANDSHAKE:
|
2019-05-21 11:01:32 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
unsigned recv_msg_seq_offset;
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned recv_msg_seq = (ssl->in_msg[4] << 8) | ssl->in_msg[5];
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_ssl_hs_buffer *hs_buf;
|
|
|
|
size_t msg_len = ssl->in_hslen - 12;
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* We should never receive an old handshake
|
|
|
|
* message - double-check nonetheless. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (recv_msg_seq < ssl->handshake->in_msg_seq) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2019-05-16 11:11:08 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Silently ignore -- message too far in the future */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2,
|
|
|
|
("Ignore future HS message with sequence number %u, "
|
|
|
|
"buffering window %u - %u",
|
|
|
|
recv_msg_seq, ssl->handshake->in_msg_seq,
|
|
|
|
ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS -
|
|
|
|
1));
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("Buffering HS message with sequence number %u, offset %u ",
|
|
|
|
recv_msg_seq, recv_msg_seq_offset));
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
hs_buf = &hs->buffering.hs[recv_msg_seq_offset];
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Check if the buffering for this seq nr has already commenced. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!hs_buf->is_valid) {
|
2020-02-05 15:44:10 +01:00
|
|
|
size_t reassembly_buf_sz;
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
hs_buf->is_fragmented =
|
2023-01-11 14:50:10 +01:00
|
|
|
(ssl_hs_is_proper_fragment(ssl) == 1);
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* We copy the message back into the input buffer
|
|
|
|
* after reassembly, so check that it's not too large.
|
|
|
|
* This is an implementation-specific limitation
|
|
|
|
* and not one from the standard, hence it is not
|
|
|
|
* checked in ssl_check_hs_header(). */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Ignore message */
|
|
|
|
goto exit;
|
|
|
|
}
|
2019-05-21 11:01:32 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Check if we have enough space to buffer the message. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hs->buffering.total_bytes_buffered >
|
|
|
|
MBEDTLS_SSL_DTLS_MAX_BUFFERING) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
reassembly_buf_sz = ssl_get_reassembly_buffer_size(msg_len,
|
|
|
|
hs_buf->is_fragmented);
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (reassembly_buf_sz > (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
|
|
|
|
hs->buffering.total_bytes_buffered)) {
|
|
|
|
if (recv_msg_seq_offset > 0) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* If we can't buffer a future message because
|
|
|
|
* of space limitations -- ignore. */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2,
|
|
|
|
("Buffering of future message of size %"
|
|
|
|
MBEDTLS_PRINTF_SIZET
|
|
|
|
" would exceed the compile-time limit %"
|
|
|
|
MBEDTLS_PRINTF_SIZET
|
|
|
|
" (already %" MBEDTLS_PRINTF_SIZET
|
|
|
|
" bytes buffered) -- ignore\n",
|
|
|
|
msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
|
|
|
|
hs->buffering.total_bytes_buffered));
|
2020-02-05 15:44:10 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2,
|
|
|
|
("Buffering of future message of size %"
|
|
|
|
MBEDTLS_PRINTF_SIZET
|
|
|
|
" would exceed the compile-time limit %"
|
|
|
|
MBEDTLS_PRINTF_SIZET
|
|
|
|
" (already %" MBEDTLS_PRINTF_SIZET
|
|
|
|
" bytes buffered) -- attempt to make space by freeing buffered future messages\n",
|
|
|
|
msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
|
|
|
|
hs->buffering.total_bytes_buffered));
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2019-07-23 16:52:45 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_buffer_make_space(ssl, reassembly_buf_sz) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2,
|
|
|
|
("Reassembly of next message of size %"
|
|
|
|
MBEDTLS_PRINTF_SIZET
|
|
|
|
" (%" MBEDTLS_PRINTF_SIZET
|
|
|
|
" with bitmap) would exceed"
|
|
|
|
" the compile-time limit %"
|
|
|
|
MBEDTLS_PRINTF_SIZET
|
|
|
|
" (already %" MBEDTLS_PRINTF_SIZET
|
|
|
|
" bytes buffered) -- fail\n",
|
|
|
|
msg_len,
|
|
|
|
reassembly_buf_sz,
|
|
|
|
(size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
|
|
|
|
hs->buffering.total_bytes_buffered));
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2,
|
|
|
|
("initialize reassembly, total length = %"
|
|
|
|
MBEDTLS_PRINTF_SIZET,
|
|
|
|
msg_len));
|
2019-05-16 13:39:07 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
hs_buf->data = mbedtls_calloc(1, reassembly_buf_sz);
|
|
|
|
if (hs_buf->data == NULL) {
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
hs_buf->data_len = reassembly_buf_sz;
|
2019-05-16 13:39:07 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Prepare final header: copy msg_type, length and message_seq,
|
|
|
|
* then add standardised fragment_offset and fragment_length */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(hs_buf->data, ssl->in_msg, 6);
|
|
|
|
memset(hs_buf->data + 6, 0, 3);
|
|
|
|
memcpy(hs_buf->data + 9, hs_buf->data + 1, 3);
|
2019-05-16 13:39:07 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
hs_buf->is_valid = 1;
|
|
|
|
|
|
|
|
hs->buffering.total_bytes_buffered += reassembly_buf_sz;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Make sure msg_type and length are consistent */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (memcmp(hs_buf->data, ssl->in_msg, 4) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Fragment header mismatch - ignore"));
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Ignore */
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!hs_buf->is_complete) {
|
2020-02-05 15:44:10 +01:00
|
|
|
size_t frag_len, frag_off;
|
|
|
|
unsigned char * const msg = hs_buf->data + 12;
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Check and copy current fragment
|
|
|
|
*/
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Validation of header fields already done in
|
|
|
|
* mbedtls_ssl_prepare_handshake_record(). */
|
2023-01-11 14:50:10 +01:00
|
|
|
frag_off = ssl_get_hs_frag_off(ssl);
|
|
|
|
frag_len = ssl_get_hs_frag_len(ssl);
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("adding fragment, offset = %" MBEDTLS_PRINTF_SIZET
|
|
|
|
", length = %" MBEDTLS_PRINTF_SIZET,
|
|
|
|
frag_off, frag_len));
|
|
|
|
memcpy(msg + frag_off, ssl->in_msg + 12, frag_len);
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hs_buf->is_fragmented) {
|
2020-02-05 15:44:10 +01:00
|
|
|
unsigned char * const bitmask = msg + msg_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_bitmask_set(bitmask, frag_off, frag_len);
|
|
|
|
hs_buf->is_complete = (ssl_bitmask_check(bitmask,
|
|
|
|
msg_len) == 0);
|
|
|
|
} else {
|
2020-02-05 15:44:10 +01:00
|
|
|
hs_buf->is_complete = 1;
|
|
|
|
}
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("message %scomplete",
|
|
|
|
hs_buf->is_complete ? "" : "not yet "));
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
break;
|
|
|
|
}
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
default:
|
|
|
|
/* We don't buffer other types of messages. */
|
|
|
|
break;
|
|
|
|
}
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
exit:
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_buffer_message"));
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_consume_current_message(mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2019-05-16 11:11:08 +02:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Consume last content-layer message and potentially
|
|
|
|
* update in_msglen which keeps track of the contents'
|
|
|
|
* consumption state.
|
|
|
|
*
|
|
|
|
* (1) Handshake messages:
|
|
|
|
* Remove last handshake message, move content
|
|
|
|
* and adapt in_msglen.
|
|
|
|
*
|
|
|
|
* (2) Alert messages:
|
|
|
|
* Consume whole record content, in_msglen = 0.
|
|
|
|
*
|
|
|
|
* (3) Change cipher spec:
|
|
|
|
* Consume whole record content, in_msglen = 0.
|
|
|
|
*
|
|
|
|
* (4) Application data:
|
|
|
|
* Don't do anything - the record layer provides
|
|
|
|
* the application data as a stream transport
|
|
|
|
* and consumes through mbedtls_ssl_read only.
|
|
|
|
*
|
2019-05-16 11:11:08 +02:00
|
|
|
*/
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Case (1): Handshake messages */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_hslen != 0) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Hard assertion to be sure that no application data
|
|
|
|
* is in flight, as corrupting ssl->in_msglen during
|
|
|
|
* ssl->in_offt != NULL is fatal. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_offt != NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Get next Handshake message in the current record
|
|
|
|
*/
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Notes:
|
|
|
|
* (1) in_hslen is not necessarily the size of the
|
|
|
|
* current handshake content: If DTLS handshake
|
|
|
|
* fragmentation is used, that's the fragment
|
|
|
|
* size instead. Using the total handshake message
|
|
|
|
* size here is faulty and should be changed at
|
|
|
|
* some point.
|
|
|
|
* (2) While it doesn't seem to cause problems, one
|
|
|
|
* has to be very careful not to assume that in_hslen
|
|
|
|
* is always <= in_msglen in a sensible communication.
|
|
|
|
* Again, it's wrong for DTLS handshake fragmentation.
|
|
|
|
* The following check is therefore mandatory, and
|
|
|
|
* should not be treated as a silently corrected assertion.
|
|
|
|
* Additionally, ssl->in_hslen might be arbitrarily out of
|
|
|
|
* bounds after handling a DTLS message with an unexpected
|
|
|
|
* sequence number, see mbedtls_ssl_prepare_handshake_record.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_hslen < ssl->in_msglen) {
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_msglen -= ssl->in_hslen;
|
2023-01-11 14:50:10 +01:00
|
|
|
memmove(ssl->in_msg, ssl->in_msg + ssl->in_hslen,
|
|
|
|
ssl->in_msglen);
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "remaining content in record",
|
|
|
|
ssl->in_msg, ssl->in_msglen);
|
|
|
|
} else {
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_msglen = 0;
|
2019-05-16 10:08:35 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_hslen = 0;
|
2019-05-16 10:08:35 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Case (4): Application data */
|
2023-01-11 14:50:10 +01:00
|
|
|
else if (ssl->in_offt != NULL) {
|
|
|
|
return 0;
|
2019-05-16 10:08:35 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Everything else (CCS & Alerts) */
|
2023-01-11 14:50:10 +01:00
|
|
|
else {
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_msglen = 0;
|
2019-05-16 11:11:08 +02:00
|
|
|
}
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2019-05-16 10:08:35 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl)
|
2019-05-23 12:28:45 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msglen > 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
2019-05-23 12:28:45 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2019-05-23 12:28:45 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2013-01-25 14:49:24 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void ssl_free_buffered_record(mbedtls_ssl_context *ssl)
|
2013-01-25 14:49:24 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_ssl_handshake_params * const hs = ssl->handshake;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hs == NULL) {
|
2020-02-05 15:44:10 +01:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-01-25 14:49:24 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hs->buffering.future_record.data != NULL) {
|
2020-02-05 15:44:10 +01:00
|
|
|
hs->buffering.total_bytes_buffered -=
|
|
|
|
hs->buffering.future_record.len;
|
2013-01-25 14:49:24 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(hs->buffering.future_record.data);
|
2020-02-05 15:44:10 +01:00
|
|
|
hs->buffering.future_record.data = NULL;
|
2013-01-25 14:49:24 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_load_buffered_record(mbedtls_ssl_context *ssl)
|
2012-09-16 21:57:18 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_ssl_handshake_params * const hs = ssl->handshake;
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char *rec;
|
2020-02-05 15:44:10 +01:00
|
|
|
size_t rec_len;
|
|
|
|
unsigned rec_epoch;
|
2019-11-28 15:29:44 +01:00
|
|
|
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
|
|
|
|
size_t in_buf_len = ssl->in_buf_len;
|
|
|
|
#else
|
|
|
|
size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-10-30 13:06:54 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hs == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-10-30 13:06:54 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
rec = hs->buffering.future_record.data;
|
|
|
|
rec_len = hs->buffering.future_record.len;
|
|
|
|
rec_epoch = hs->buffering.future_record.epoch;
|
2013-10-30 13:06:54 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Only consider loading future records if the
|
|
|
|
* input buffer is empty. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_next_record_is_in_datagram(ssl) == 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_record"));
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec_epoch != ssl->in_epoch) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("Buffered record not from current epoch."));
|
2020-02-05 15:44:10 +01:00
|
|
|
goto exit;
|
2014-08-19 19:18:39 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("Found buffered record from current epoch - load"));
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Double-check that the record is not too large */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec_len > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2012-09-16 21:57:18 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(ssl->in_hdr, rec, rec_len);
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_left = rec_len;
|
|
|
|
ssl->next_record_offset = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_free_buffered_record(ssl);
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_record"));
|
|
|
|
return 0;
|
2012-09-16 21:57:18 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_buffer_future_record(mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_record const *rec)
|
2013-10-30 13:06:54 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_ssl_handshake_params * const hs = ssl->handshake;
|
2014-08-15 19:04:47 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Don't buffer future records outside handshakes. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hs == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-08-15 19:04:47 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Only buffer handshake records (we are only interested
|
|
|
|
* in Finished messages). */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec->type != MBEDTLS_SSL_MSG_HANDSHAKE) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-10-30 16:41:21 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Don't buffer more than one future epoch record. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hs->buffering.future_record.data != NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-10-30 16:41:21 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Don't buffer record if there's not enough buffering space remaining. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rec->buf_len > (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
|
|
|
|
hs->buffering.total_bytes_buffered)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET
|
|
|
|
" would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
|
|
|
|
" (already %" MBEDTLS_PRINTF_SIZET
|
|
|
|
" bytes buffered) -- ignore\n",
|
|
|
|
rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
|
|
|
|
hs->buffering.total_bytes_buffered));
|
|
|
|
return 0;
|
2013-10-30 16:41:21 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Buffer record */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("Buffer record from epoch %u",
|
|
|
|
ssl->in_epoch + 1U));
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "Buffered record", rec->buf, rec->buf_len);
|
2013-10-30 13:06:54 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* ssl_parse_record_header() only considers records
|
|
|
|
* of the next epoch as candidates for buffering. */
|
|
|
|
hs->buffering.future_record.epoch = ssl->in_epoch + 1;
|
|
|
|
hs->buffering.future_record.len = rec->buf_len;
|
2016-12-15 18:01:16 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
hs->buffering.future_record.data =
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_calloc(1, hs->buffering.future_record.len);
|
|
|
|
if (hs->buffering.future_record.data == NULL) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* If we run out of RAM trying to buffer a
|
|
|
|
* record from the next epoch, just ignore. */
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2014-11-04 21:04:22 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(hs->buffering.future_record.data, rec->buf, rec->buf_len);
|
2014-11-04 21:04:22 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
hs->buffering.total_bytes_buffered += rec->buf_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2014-11-04 21:04:22 +01:00
|
|
|
}
|
2013-10-30 13:06:54 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_get_next_record(mbedtls_ssl_context *ssl)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_record rec;
|
2014-09-23 09:42:16 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2020-02-05 15:44:10 +01:00
|
|
|
/* We might have buffered a future record; if so,
|
|
|
|
* and if the epoch matches now, load it.
|
|
|
|
* On success, this call will set ssl->in_left to
|
|
|
|
* the length of the buffered record, so that
|
|
|
|
* the calls to ssl_fetch_input() below will
|
|
|
|
* essentially be no-ops. */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_load_buffered_record(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2014-11-04 21:04:22 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Ensure that we have enough space available for the default form
|
|
|
|
* of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
|
|
|
|
* with no space for CIDs counted in). */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_fetch_input(ssl, mbedtls_ssl_in_hdr_len(ssl));
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
|
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_parse_record_header(ssl, ssl->in_hdr, ssl->in_left, &rec);
|
|
|
|
if (ret != 0) {
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
|
|
|
if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) {
|
|
|
|
ret = ssl_buffer_future_record(ssl, &rec);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2011-05-18 15:32:51 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Fall through to handling of unexpected records */
|
|
|
|
ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2017-05-24 17:27:30 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD) {
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
/* Reset in pointers to default state for TLS/DTLS records,
|
|
|
|
* assuming no CID and no offset between record content and
|
|
|
|
* record plaintext. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_update_in_pointers(ssl);
|
2014-09-06 12:27:02 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Setup internal message pointers from record structure. */
|
|
|
|
ssl->in_msgtype = rec.type;
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
|
|
|
ssl->in_len = ssl->in_cid + rec.cid_len;
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
|
|
|
ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
|
|
|
|
ssl->in_msglen = rec.data_len;
|
2014-09-06 12:27:02 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_check_client_reconnect(ssl);
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(2, "ssl_check_client_reconnect", ret);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2014-09-06 12:27:02 +02:00
|
|
|
#endif
|
2017-05-24 17:27:30 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Skip unexpected record (but not whole datagram) */
|
|
|
|
ssl->next_record_offset = rec.buf_len;
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("discarding unexpected record "
|
|
|
|
"(header)"));
|
|
|
|
} else {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Skip invalid record and the rest of the datagram */
|
|
|
|
ssl->next_record_offset = 0;
|
|
|
|
ssl->in_left = 0;
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record "
|
|
|
|
"(header)"));
|
2012-09-16 21:57:18 +02:00
|
|
|
}
|
2014-08-19 12:28:50 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Get next record */
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
|
|
|
|
} else
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2013-10-30 16:41:45 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2013-10-30 16:41:45 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2014-08-19 12:28:50 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Remember offset of next record within datagram. */
|
|
|
|
ssl->next_record_offset = rec.buf_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->next_record_offset < ssl->in_left) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("more than one record within datagram"));
|
2014-08-19 12:28:50 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Fetch record contents from underlying transport.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_fetch_input(ssl, rec.buf_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
|
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_left = 0;
|
|
|
|
}
|
2014-10-01 18:29:03 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Decrypt record contents.
|
|
|
|
*/
|
2014-10-15 13:52:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = ssl_prepare_record_content(ssl, &rec)) != 0) {
|
2015-05-13 16:22:05 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Silently discard invalid records */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Except when waiting for Finished as a bad mac here
|
|
|
|
* probably means something went wrong in the handshake
|
|
|
|
* (eg wrong psk used, mitm downgrade attempt, etc.) */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
|
|
|
|
ssl->state == MBEDTLS_SSL_SERVER_FINISHED) {
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
|
|
|
|
mbedtls_ssl_send_alert_message(ssl,
|
|
|
|
MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC);
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->badmac_limit != 0 &&
|
|
|
|
++ssl->badmac_seen >= ssl->conf->badmac_limit) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("too many records with bad MAC"));
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_MAC;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* As above, invalid records cause
|
|
|
|
* dismissal of the whole datagram. */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->next_record_offset = 0;
|
|
|
|
ssl->in_left = 0;
|
2017-09-21 13:49:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record (mac)"));
|
|
|
|
return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2017-09-21 13:49:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
|
|
|
} else
|
2014-10-13 17:55:52 +02:00
|
|
|
#endif
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
|
|
|
/* Error out (and send alert) on invalid records */
|
|
|
|
#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
|
|
|
|
mbedtls_ssl_send_alert_message(ssl,
|
|
|
|
MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC);
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2014-10-13 17:55:52 +02:00
|
|
|
}
|
2011-06-08 15:10:54 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
/* Reset in pointers to default state for TLS/DTLS records,
|
|
|
|
* assuming no CID and no offset between record content and
|
|
|
|
* record plaintext. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_update_in_pointers(ssl);
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
|
|
|
ssl->in_len = ssl->in_cid + rec.cid_len;
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
|
|
|
ssl->in_iv = ssl->in_len + 2;
|
|
|
|
|
|
|
|
/* The record content type may change during decryption,
|
|
|
|
* so re-read it. */
|
|
|
|
ssl->in_msgtype = rec.type;
|
|
|
|
/* Also update the input buffer, because unfortunately
|
|
|
|
* the server-side ssl_parse_client_hello() reparses the
|
|
|
|
* record header when receiving a ClientHello initiating
|
|
|
|
* a renegotiation. */
|
|
|
|
ssl->in_hdr[0] = rec.type;
|
|
|
|
ssl->in_msg = rec.buf + rec.data_offset;
|
|
|
|
ssl->in_msglen = rec.data_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->in_len, 0);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_handle_message_type(mbedtls_ssl_context *ssl)
|
2015-01-07 12:39:44 +01:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-01-07 12:39:44 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Handle particular types of records
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) {
|
|
|
|
if ((ret = mbedtls_ssl_prepare_handshake_record(ssl)) != 0) {
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2015-01-07 12:39:44 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
|
|
|
|
if (ssl->in_msglen != 1) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET,
|
|
|
|
ssl->in_msglen));
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_RECORD;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2015-04-15 19:09:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msg[0] != 1) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, content: %02x",
|
|
|
|
ssl->in_msg[0]));
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_RECORD;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2015-04-15 19:09:03 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) {
|
|
|
|
if (ssl->handshake == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("dropping ChangeCipherSpec outside handshake"));
|
|
|
|
return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2015-09-01 17:43:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("received out-of-order ChangeCipherSpec - remember"));
|
|
|
|
return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2015-04-15 19:09:03 +02:00
|
|
|
#endif
|
2021-11-24 12:43:39 +01:00
|
|
|
|
2021-12-08 16:57:54 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
|
2021-11-24 12:43:39 +01:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1,
|
|
|
|
("Ignore ChangeCipherSpec in TLS 1.3 compatibility mode"));
|
|
|
|
return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
|
2021-11-24 12:43:39 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1,
|
|
|
|
("ChangeCipherSpec invalid in TLS 1.3 without compatibility mode"));
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_RECORD;
|
2021-11-24 12:43:39 +01:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
|
|
|
|
}
|
2021-12-08 16:57:54 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2015-04-15 19:09:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) {
|
|
|
|
if (ssl->in_msglen != 2) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Note: Standard allows for more than one 2 byte alert
|
|
|
|
to be packed in a single message, but Mbed TLS doesn't
|
|
|
|
currently support this. */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("invalid alert message, len: %" MBEDTLS_PRINTF_SIZET,
|
|
|
|
ssl->in_msglen));
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_RECORD;
|
2015-04-15 19:09:03 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("got an alert message, type: [%u:%u]",
|
|
|
|
ssl->in_msg[0], ssl->in_msg[1]));
|
2015-04-15 19:09:03 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Ignore non-fatal alerts, except close_notify and no_renegotiation
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("is a fatal alert message (msg %d)",
|
|
|
|
ssl->in_msg[1]));
|
|
|
|
return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2015-04-15 19:09:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
|
|
|
|
ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("is a close notify message"));
|
|
|
|
return MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
|
|
|
|
ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("is a no renegotiation alert"));
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Will be handled when trying to parse ServerHello */
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* Silently ignore: fetch new message */
|
|
|
|
return MBEDTLS_ERR_SSL_NON_FATAL;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Drop unexpected ApplicationData records,
|
|
|
|
* except at the beginning of renegotiations */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
|
|
|
|
mbedtls_ssl_is_handshake_over(ssl) == 0
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
&& !(ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
|
|
|
|
ssl->state == MBEDTLS_SSL_SERVER_HELLO)
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("dropping unexpected ApplicationData"));
|
|
|
|
return MBEDTLS_ERR_SSL_NON_FATAL;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake != NULL &&
|
|
|
|
mbedtls_ssl_is_handshake_over(ssl) == 1) {
|
|
|
|
mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_send_fatal_handshake_failure(mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_ssl_send_alert_message(ssl,
|
|
|
|
MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_send_alert_message(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char level,
|
|
|
|
unsigned char message)
|
2012-09-16 21:57:18 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-09-23 17:12:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl == NULL || ssl->conf == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2013-07-04 11:51:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->out_left != 0) {
|
|
|
|
return mbedtls_ssl_flush_output(ssl);
|
|
|
|
}
|
2018-08-06 12:35:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> send alert message"));
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("send alert level=%u message=%u", level, message));
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
|
|
|
|
ssl->out_msglen = 2;
|
|
|
|
ssl->out_msg[0] = level;
|
|
|
|
ssl->out_msg[1] = message;
|
2013-09-23 20:04:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
|
|
|
|
return ret;
|
2013-09-23 20:04:20 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= send alert message"));
|
2013-09-23 20:04:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2018-08-16 14:23:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_write_change_cipher_spec(mbedtls_ssl_context *ssl)
|
2018-08-16 14:23:47 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-08-24 10:34:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
|
2018-08-21 16:59:07 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
|
|
|
|
ssl->out_msglen = 1;
|
|
|
|
ssl->out_msg[0] = 1;
|
2018-08-23 14:18:05 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->state++;
|
2018-08-23 14:18:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
|
|
|
|
return ret;
|
2018-08-16 14:23:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2018-08-16 14:23:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_parse_change_cipher_spec(mbedtls_ssl_context *ssl)
|
2012-09-16 21:57:18 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-04-25 20:32:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse change cipher spec"));
|
2014-06-26 13:37:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
|
|
|
|
return ret;
|
2018-04-25 20:39:48 +02:00
|
|
|
}
|
2013-07-04 11:51:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("bad change cipher spec message"));
|
|
|
|
mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
|
|
|
|
return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
|
2015-05-07 16:59:54 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* CCS records are only accepted if they have length 1 and content '1',
|
|
|
|
* so we don't need to check this here. */
|
|
|
|
|
2013-09-24 22:30:56 +02:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Switch to our negotiated transform and session parameters for inbound
|
|
|
|
* data.
|
2013-09-24 22:30:56 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for inbound data"));
|
2022-12-01 11:57:19 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->transform_in = ssl->transform_negotiate;
|
2022-12-01 11:57:19 +01:00
|
|
|
#endif
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->session_in = ssl->session_negotiate;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_dtls_replay_reset(ssl);
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2013-09-24 22:30:56 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Increment epoch */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (++ssl->in_epoch == 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
|
2020-02-05 15:44:10 +01:00
|
|
|
/* This is highly unlikely to happen for legitimate reasons, so
|
|
|
|
treat it as an attack and don't send an alert. */
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
|
2013-09-24 22:30:56 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(ssl->in_ctr, 0, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_update_in_pointers(ssl);
|
2013-09-23 20:04:20 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->state++;
|
2012-09-25 23:55:46 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse change cipher spec"));
|
2013-08-02 14:44:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2012-09-16 21:57:18 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Once ssl->out_hdr as the address of the beginning of the
|
|
|
|
* next outgoing record is set, deduce the other pointers.
|
2019-07-10 14:58:45 +02:00
|
|
|
*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Note: For TLS, we save the implicit record sequence number
|
|
|
|
* (entering MAC computation) in the 8 bytes before ssl->out_hdr,
|
|
|
|
* and the caller has to make sure there's space for this.
|
2019-05-28 13:02:16 +02:00
|
|
|
*/
|
2019-07-11 12:50:53 +02:00
|
|
|
|
2020-05-28 08:17:36 +02:00
|
|
|
static size_t ssl_transform_get_explicit_iv_len(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_transform const *transform)
|
2020-05-28 08:17:36 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return transform->ivlen - transform->fixed_ivlen;
|
2020-05-28 08:17:36 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_update_out_pointers(mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_ssl_transform *transform)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_ctr = ssl->out_hdr + 3;
|
2019-07-15 09:04:11 +02:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
2021-10-08 09:21:19 +02:00
|
|
|
ssl->out_cid = ssl->out_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_len = ssl->out_cid;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (transform != NULL) {
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_len += transform->out_cid_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
2021-10-08 09:21:19 +02:00
|
|
|
ssl->out_len = ssl->out_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
|
|
|
ssl->out_iv = ssl->out_len + 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2019-07-15 09:04:11 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_len = ssl->out_hdr + 3;
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
|
|
|
ssl->out_cid = ssl->out_len;
|
|
|
|
#endif
|
|
|
|
ssl->out_iv = ssl->out_hdr + 5;
|
2019-07-15 09:04:11 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 08:17:36 +02:00
|
|
|
ssl->out_msg = ssl->out_iv;
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Adjust out_msg to make space for explicit IV, if used. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (transform != NULL) {
|
|
|
|
ssl->out_msg += ssl_transform_get_explicit_iv_len(transform);
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2019-07-15 11:23:03 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Once ssl->in_hdr as the address of the beginning of the
|
|
|
|
* next incoming record is set, deduce the other pointers.
|
|
|
|
*
|
|
|
|
* Note: For TLS, we save the implicit record sequence number
|
|
|
|
* (entering MAC computation) in the 8 bytes before ssl->in_hdr,
|
|
|
|
* and the caller has to make sure there's space for this.
|
|
|
|
*/
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
|
|
|
/* This function sets the pointers to match the case
|
|
|
|
* of unprotected TLS/DTLS records, with both ssl->in_iv
|
|
|
|
* and ssl->in_msg pointing to the beginning of the record
|
|
|
|
* content.
|
|
|
|
*
|
|
|
|
* When decrypting a protected record, ssl->in_msg
|
|
|
|
* will be shifted to point to the beginning of the
|
|
|
|
* record plaintext.
|
|
|
|
*/
|
2019-07-15 11:23:03 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* This sets the header pointers to match records
|
|
|
|
* without CID. When we receive a record containing
|
|
|
|
* a CID, the fields are shifted accordingly in
|
|
|
|
* ssl_parse_record_header(). */
|
|
|
|
ssl->in_ctr = ssl->in_hdr + 3;
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
2021-10-08 09:21:19 +02:00
|
|
|
ssl->in_cid = ssl->in_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_len = ssl->in_cid; /* Default: no CID */
|
|
|
|
#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
2021-10-08 09:21:19 +02:00
|
|
|
ssl->in_len = ssl->in_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
|
|
|
ssl->in_iv = ssl->in_len + 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2019-07-15 11:23:03 +02:00
|
|
|
{
|
2021-10-08 09:21:19 +02:00
|
|
|
ssl->in_ctr = ssl->in_hdr - MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_len = ssl->in_hdr + 3;
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
|
|
|
ssl->in_cid = ssl->in_len;
|
|
|
|
#endif
|
|
|
|
ssl->in_iv = ssl->in_hdr + 5;
|
2019-07-15 11:23:03 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* This will be adjusted at record decryption time. */
|
|
|
|
ssl->in_msg = ssl->in_iv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup an SSL context
|
|
|
|
*/
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_reset_in_out_pointers(mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
|
|
|
/* Set the incoming and outgoing record pointers. */
|
2019-07-15 11:23:03 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_hdr = ssl->out_buf;
|
|
|
|
ssl->in_hdr = ssl->in_buf;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2019-07-15 11:23:03 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
|
|
|
{
|
Keep pointer to TLS record sequence number static
The field `cur_out_ctr` in the SSL context keeps track of the
record sequence number for the next outgoing record.
For TLS, this sequence number is implicit and not transmitted
on the wire, while for DTLS, it's part of of the record header.
For DTLS, the position of the record sequence number of the next
outgoing record in that record's header is tracked in the pointer
`out_ctr`. This pointer moves forward along with other pointers
such as `out_hdr` or `out_msg` within the outgoing data buffer
`out_buf` as multiple records are written in the same datagram.
For TLS, the `out_ctr` pointer is logically superfluous, but
for some reason, we're still maintaining it by having it point
to the 8 Bytes prior to the header of the next outgoing record,
and always copying `cur_out_ctr` to this position prior to
encrypting an outgoing record.
After a record has been prepared for writing in `ssl_write_record()`,
the `out_xxx` pointers (except for `out_buf`, which is static),
are shifted forward so that they point to the header and content
of the next outgoing record. This is used only in DTLS in order
to stack multiple records into a single datagram, but the shifting
is happening for TLS as well. However, it has little effect in TLS
because we're always flushing immediately after writing, and afterwards
reset the `out_xxx` pointers.
While the present code works as-is, it is wrong to shift `out_ctr`
in the case of TLS, because it makes `out_ctr` point to the last 8
Bytes of the ciphertext of the last outgoing record. Should we ever
aim to prepare more than one protected record in `out_buf` before
dispatching it to the underlying transport, the superfluous copying
of `cur_out_ctr` to `out_buf` will corrupt the last 8 bytes of the
last record.
This commit aims to fix this problem in the minimal possible way,
by simply not shifting `out_ctr` after a record has been written.
It does deliberately not attempt to remove `out_ctr` for TLS altogether,
because any change in the messaging layer is hard to review, and
we're going to replace it soon anyhow.
The shifting happens in the helper routine mbedtls_ssl_update_out_pointers,
which assumed correctness of `out_hdr` for the beginning of the record header
of the next outgoing record, and derives the other `out_xxx` variables.
We remove the update of `out_ctr` from this function in the case of TLS,
and instead move the proper initialization of `out_ctr` to
`out_buf == initial_out_hdr - 8` to the function
mbedtls_ssl_reset_in_out_pointers().
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-03-02 16:28:41 +01:00
|
|
|
ssl->out_ctr = ssl->out_buf;
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_hdr = ssl->out_buf + 8;
|
|
|
|
ssl->in_hdr = ssl->in_buf + 8;
|
2019-07-15 11:23:03 +02:00
|
|
|
}
|
2019-07-11 12:50:53 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Derive other internal pointers. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_update_out_pointers(ssl, NULL /* no transform enabled */);
|
|
|
|
mbedtls_ssl_update_in_pointers(ssl);
|
2019-05-28 13:02:16 +02:00
|
|
|
}
|
|
|
|
|
2019-07-15 09:04:11 +02:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* SSL get accessors
|
2019-07-15 09:04:11 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t mbedtls_ssl_get_bytes_avail(const mbedtls_ssl_context *ssl)
|
2019-07-15 09:04:11 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return ssl->in_offt == NULL ? 0 : ssl->in_msglen;
|
2019-07-15 09:04:11 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_check_pending(const mbedtls_ssl_context *ssl)
|
2019-05-28 13:02:16 +02:00
|
|
|
{
|
2019-07-11 09:56:30 +02:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Case A: We're currently holding back
|
|
|
|
* a message for further processing.
|
2019-07-11 09:56:30 +02:00
|
|
|
*/
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->keep_current_message == 1) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: record held back for processing"));
|
|
|
|
return 1;
|
2019-07-11 09:56:30 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 10:58:10 +02:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Case B: Further records are pending in the current datagram.
|
2019-07-11 10:58:10 +02:00
|
|
|
*/
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
|
|
|
|
ssl->in_left > ssl->next_record_offset) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: more records within current datagram"));
|
|
|
|
return 1;
|
2019-07-11 10:58:10 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2019-07-11 10:58:10 +02:00
|
|
|
|
2019-07-11 12:50:53 +02:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Case C: A handshake message is being processed.
|
2019-07-11 12:50:53 +02:00
|
|
|
*/
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3,
|
|
|
|
("ssl_check_pending: more handshake messages within current record"));
|
|
|
|
return 1;
|
2019-07-23 16:52:45 +02:00
|
|
|
}
|
2019-07-11 12:50:53 +02:00
|
|
|
|
2019-07-15 09:04:11 +02:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Case D: An application data message is being processed
|
2019-07-15 09:04:11 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_offt != NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: application data record is being processed"));
|
|
|
|
return 1;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2019-07-15 09:04:11 +02:00
|
|
|
|
2019-07-15 11:23:03 +02:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* In all other cases, the rest of the message can be dropped.
|
|
|
|
* As in ssl_get_next_record, this needs to be adapted if
|
|
|
|
* we implement support for multiple alerts in single records.
|
2019-07-15 11:23:03 +02:00
|
|
|
*/
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: nothing pending"));
|
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2019-07-15 11:23:03 +02:00
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_get_record_expansion(const mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
|
|
|
size_t transform_expansion = 0;
|
|
|
|
const mbedtls_ssl_transform *transform = ssl->transform_out;
|
|
|
|
unsigned block_size;
|
2022-01-19 16:00:22 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_type_t key_type;
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2019-07-15 11:23:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t out_hdr_len = mbedtls_ssl_out_hdr_len(ssl);
|
2019-07-15 11:23:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (transform == NULL) {
|
|
|
|
return (int) out_hdr_len;
|
|
|
|
}
|
2019-07-15 11:23:03 +02:00
|
|
|
|
2022-01-19 16:00:22 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (transform->psa_alg == PSA_ALG_GCM ||
|
|
|
|
transform->psa_alg == PSA_ALG_CCM ||
|
|
|
|
transform->psa_alg == PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 8) ||
|
|
|
|
transform->psa_alg == PSA_ALG_CHACHA20_POLY1305 ||
|
|
|
|
transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) {
|
2022-01-24 23:46:50 +01:00
|
|
|
transform_expansion = transform->minlen;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (transform->psa_alg == PSA_ALG_CBC_NO_PADDING) {
|
|
|
|
(void) psa_get_key_attributes(transform->psa_key_enc, &attr);
|
|
|
|
key_type = psa_get_key_type(&attr);
|
2022-01-19 16:00:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
|
2022-01-19 16:00:22 +01:00
|
|
|
|
2022-01-24 23:46:50 +01:00
|
|
|
/* Expansion due to the addition of the MAC. */
|
|
|
|
transform_expansion += transform->maclen;
|
2022-01-19 16:00:22 +01:00
|
|
|
|
2022-01-24 23:46:50 +01:00
|
|
|
/* Expansion due to the addition of CBC padding;
|
2022-02-03 10:44:02 +01:00
|
|
|
* Theoretically up to 256 bytes, but we never use
|
|
|
|
* more than the block size of the underlying cipher. */
|
2022-01-24 23:46:50 +01:00
|
|
|
transform_expansion += block_size;
|
2022-01-19 16:00:22 +01:00
|
|
|
|
2022-01-24 23:46:50 +01:00
|
|
|
/* For TLS 1.2 or higher, an explicit IV is added
|
2022-02-03 10:44:02 +01:00
|
|
|
* after the record header. */
|
2022-01-19 16:00:22 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2022-01-24 23:46:50 +01:00
|
|
|
transform_expansion += block_size;
|
2022-01-19 16:00:22 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1,
|
|
|
|
("Unsupported psa_alg spotted in mbedtls_ssl_get_record_expansion()"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-01-19 16:00:22 +01:00
|
|
|
}
|
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)) {
|
2020-02-05 15:44:10 +01:00
|
|
|
case MBEDTLS_MODE_GCM:
|
|
|
|
case MBEDTLS_MODE_CCM:
|
|
|
|
case MBEDTLS_MODE_CHACHAPOLY:
|
|
|
|
case MBEDTLS_MODE_STREAM:
|
|
|
|
transform_expansion = transform->minlen;
|
|
|
|
break;
|
2019-07-15 11:23:03 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
case MBEDTLS_MODE_CBC:
|
2019-07-15 11:23:03 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
block_size = mbedtls_cipher_get_block_size(
|
2023-01-11 14:50:10 +01:00
|
|
|
&transform->cipher_ctx_enc);
|
2019-07-15 11:23:03 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Expansion due to the addition of the MAC. */
|
|
|
|
transform_expansion += transform->maclen;
|
2019-07-15 11:53:51 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Expansion due to the addition of CBC padding;
|
|
|
|
* Theoretically up to 256 bytes, but we never use
|
|
|
|
* more than the block size of the underlying cipher. */
|
|
|
|
transform_expansion += block_size;
|
2019-07-15 11:53:51 +02:00
|
|
|
|
2021-05-20 14:46:20 +02:00
|
|
|
/* For TLS 1.2 or higher, an explicit IV is added
|
2020-02-05 15:44:10 +01:00
|
|
|
* after the record header. */
|
2021-05-12 17:49:18 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2021-07-06 13:42:11 +02:00
|
|
|
transform_expansion += block_size;
|
2021-05-12 17:49:18 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
Fix SSL context deserialization
The SSL context maintains a set of 'out pointers' indicating the
address at which to write the header fields of the next outgoing
record. Some of these addresses have a static offset from the
beginning of the record header, while other offsets can vary
depending on the active record encryption mechanism: For example,
if an explicit IV is in use, there's an offset between the end
of the record header and the beginning of the encrypted data to
allow the explicit IV to be placed in between; also, if the DTLS
Connection ID (CID) feature is in use, the CID is part of the
record header, shifting all subsequent information (length, IV, data)
to the back.
When setting up an SSL context, the out pointers are initialized
according to the identity transform + no CID, and it is important
to keep them up to date whenever the record encryption mechanism
changes, which is done by the helper function ssl_update_out_pointers().
During context deserialization, updating the out pointers according
to the deserialized record transform went missing, leaving the out
pointers the initial state. When attemping to encrypt a record in
this state, this lead to failure if either a CID or an explicit IV
was in use. This wasn't caught in the tests by the bad luck that
they didn't use CID, _and_ used the default ciphersuite based on
ChaChaPoly, which doesn't have an explicit IV. Changing either of
this would have made the existing tests fail.
This commit fixes the bug by adding a call to ssl_update_out_pointers()
to ssl_context_load() implementing context deserialization.
Extending test coverage is left for a separate commit.
2019-08-30 11:42:49 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
break;
|
2019-07-15 11:53:51 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2019-07-15 11:53:51 +02:00
|
|
|
}
|
2022-01-19 16:00:22 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2019-07-15 11:53:51 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (transform->out_cid_len != 0) {
|
2020-02-05 15:44:10 +01:00
|
|
|
transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
2019-05-28 13:02:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return (int) (out_hdr_len + transform_expansion);
|
2019-05-28 13:02:16 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
2019-07-11 12:50:53 +02:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Check record counters and renegotiate if they're above the limit.
|
2019-07-11 12:50:53 +02:00
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_check_ctr_renegotiate(mbedtls_ssl_context *ssl)
|
2019-07-11 12:50:53 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t ep_len = mbedtls_ssl_ep_len(ssl);
|
2020-02-05 15:44:10 +01:00
|
|
|
int in_ctr_cmp;
|
|
|
|
int out_ctr_cmp;
|
2019-07-11 12:50:53 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_is_handshake_over(ssl) == 0 ||
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
|
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2019-07-11 12:50:53 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
in_ctr_cmp = memcmp(ssl->in_ctr + ep_len,
|
|
|
|
&ssl->conf->renego_period[ep_len],
|
|
|
|
MBEDTLS_SSL_SEQUENCE_NUMBER_LEN - ep_len);
|
|
|
|
out_ctr_cmp = memcmp(&ssl->cur_out_ctr[ep_len],
|
2021-09-28 12:58:59 +02:00
|
|
|
&ssl->conf->renego_period[ep_len],
|
2023-01-11 14:50:10 +01:00
|
|
|
sizeof(ssl->cur_out_ctr) - ep_len);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (in_ctr_cmp <= 0 && out_ctr_cmp <= 0) {
|
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("record counter limit reached: renegotiate"));
|
|
|
|
return mbedtls_ssl_renegotiate(ssl);
|
2019-07-11 12:50:53 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_RENEGOTIATION */
|
2019-07-11 12:50:53 +02:00
|
|
|
|
2022-07-07 11:42:26 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
|
2022-07-13 05:22:55 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_check_new_session_ticket(mbedtls_ssl_context *ssl)
|
2022-07-07 11:42:26 +02:00
|
|
|
{
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl)) ||
|
|
|
|
(ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET)) {
|
|
|
|
return 0;
|
2022-07-07 11:42:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ssl->keep_current_message = 1;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("NewSessionTicket received"));
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl,
|
|
|
|
MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
|
2022-07-07 11:42:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_WANT_READ;
|
2022-07-07 11:42:26 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
|
|
|
|
|
2022-07-13 05:22:55 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_handle_hs_message_post_handshake(mbedtls_ssl_context *ssl)
|
2022-07-07 11:42:26 +02:00
|
|
|
{
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("received post-handshake message"));
|
2022-07-07 11:42:26 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
|
|
|
|
int ret = ssl_tls13_check_new_session_ticket(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-07-07 11:42:26 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
|
|
|
|
|
|
|
|
/* Fail in all other cases. */
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
|
2022-07-07 11:42:26 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2020-11-24 07:41:37 +01:00
|
|
|
/* This function is called from mbedtls_ssl_read() when a handshake message is
|
2021-04-21 08:30:13 +02:00
|
|
|
* received after the initial handshake. In this context, handshake messages
|
2020-11-24 07:41:37 +01:00
|
|
|
* may only be sent for the purpose of initiating renegotiations.
|
|
|
|
*
|
|
|
|
* This function is introduced as a separate helper since the handling
|
|
|
|
* of post-handshake handshake messages changes significantly in TLS 1.3,
|
|
|
|
* and having a helper function allows to distinguish between TLS <= 1.2 and
|
|
|
|
* TLS 1.3 in the future without bloating the logic of mbedtls_ssl_read().
|
|
|
|
*/
|
2022-07-13 05:22:55 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls12_handle_hs_message_post_handshake(mbedtls_ssl_context *ssl)
|
2020-11-24 07:41:37 +01:00
|
|
|
{
|
2021-04-21 08:20:20 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-11-24 07:41:37 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* - For client-side, expect SERVER_HELLO_REQUEST.
|
|
|
|
* - For server-side, expect CLIENT_HELLO.
|
|
|
|
* - Fail (TLS) or silently drop record (DTLS) in other cases.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
|
|
|
|
(ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
|
|
|
|
ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl))) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not HelloRequest)"));
|
2020-11-24 07:41:37 +01:00
|
|
|
|
|
|
|
/* With DTLS, drop the packet (probably from last handshake) */
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
|
|
|
return 0;
|
2020-11-24 07:41:37 +01:00
|
|
|
}
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
|
2020-11-24 07:41:37 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
|
|
|
|
ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not ClientHello)"));
|
2020-11-24 07:41:37 +01:00
|
|
|
|
|
|
|
/* With DTLS, drop the packet (probably from last handshake) */
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
|
|
|
return 0;
|
2020-11-24 07:41:37 +01:00
|
|
|
}
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
|
2020-11-24 07:41:37 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SRV_C */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
|
|
|
/* Determine whether renegotiation attempt should be accepted */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!(ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
|
|
|
|
(ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
|
|
|
|
ssl->conf->allow_legacy_renegotiation ==
|
|
|
|
MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION))) {
|
2020-11-24 07:41:37 +01:00
|
|
|
/*
|
|
|
|
* Accept renegotiation request
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* DTLS clients need to know renego is server-initiated */
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
|
|
|
|
ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
|
2020-11-24 07:41:37 +01:00
|
|
|
ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
|
|
|
|
}
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_start_renegotiation(ssl);
|
|
|
|
if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
|
|
|
|
ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation",
|
|
|
|
ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else
|
2020-11-24 07:41:37 +01:00
|
|
|
#endif /* MBEDTLS_SSL_RENEGOTIATION */
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Refuse renegotiation
|
|
|
|
*/
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("refusing renegotiation, sending alert"));
|
2020-11-24 07:41:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_send_alert_message(ssl,
|
|
|
|
MBEDTLS_SSL_ALERT_LEVEL_WARNING,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION)) != 0) {
|
|
|
|
return ret;
|
2020-11-24 07:41:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-11-24 07:41:37 +01:00
|
|
|
}
|
2022-07-07 11:42:26 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
|
|
|
|
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_handle_hs_message_post_handshake(mbedtls_ssl_context *ssl)
|
2022-07-07 11:42:26 +02:00
|
|
|
{
|
|
|
|
/* Check protocol version and dispatch accordingly. */
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
|
|
|
|
return ssl_tls13_handle_hs_message_post_handshake(ssl);
|
2022-07-07 11:42:26 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->tls_version <= MBEDTLS_SSL_VERSION_TLS1_2) {
|
|
|
|
return ssl_tls12_handle_hs_message_post_handshake(ssl);
|
2022-07-07 11:42:26 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
|
|
|
|
|
|
|
/* Should never happen */
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-07-07 11:42:26 +02:00
|
|
|
}
|
2020-11-24 07:41:37 +01:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Receive application data decrypted from the SSL layer
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, size_t len)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t n;
|
2014-06-26 13:37:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl == NULL || ssl->conf == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> read"));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
|
|
|
if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake != NULL &&
|
|
|
|
ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
|
|
|
|
if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2013-10-11 09:59:44 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Check if renegotiation is necessary and/or handshake is
|
|
|
|
* in process. If yes, perform/continue, and fall through
|
|
|
|
* if an unexpected packet is received while the client
|
|
|
|
* is waiting for the ServerHello.
|
|
|
|
*
|
|
|
|
* (There is no equivalent to the last condition on
|
|
|
|
* the server-side as it is not treated as within
|
|
|
|
* a handshake while waiting for the ClientHello
|
|
|
|
* after a renegotiation request.)
|
|
|
|
*/
|
2013-02-14 11:19:38 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_check_ctr_renegotiate(ssl);
|
|
|
|
if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
|
|
|
|
ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret);
|
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2013-08-27 21:55:01 +02:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
|
|
|
|
ret = mbedtls_ssl_handshake(ssl);
|
|
|
|
if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
|
|
|
|
ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2012-05-08 11:17:57 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Loop as long as no application data record is available */
|
2023-01-11 14:50:10 +01:00
|
|
|
while (ssl->in_offt == NULL) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Start timer if not already running */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->f_get_timer != NULL &&
|
|
|
|
ssl->f_get_timer(ssl->p_timer) == -1) {
|
|
|
|
mbedtls_ssl_set_timer(ssl, ssl->conf->read_timeout);
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
|
|
|
|
if (ret == MBEDTLS_ERR_SSL_CONN_EOF) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-04 13:35:39 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2015-12-04 15:02:56 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msglen == 0 &&
|
|
|
|
ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* OpenSSL sends empty messages to randomize the IV
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
|
|
|
|
if (ret == MBEDTLS_ERR_SSL_CONN_EOF) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-06-17 13:53:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
}
|
2015-06-17 13:53:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) {
|
|
|
|
ret = ssl_handle_hs_message_post_handshake(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_handle_hs_message_post_handshake",
|
|
|
|
ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2015-05-04 13:35:39 +02:00
|
|
|
|
2021-04-21 08:30:13 +02:00
|
|
|
/* At this point, we don't know whether the renegotiation triggered
|
|
|
|
* by the post-handshake message has been completed or not. The cases
|
|
|
|
* to consider are the following:
|
2020-02-05 15:44:10 +01:00
|
|
|
* 1) The renegotiation is complete. In this case, no new record
|
|
|
|
* has been read yet.
|
|
|
|
* 2) The renegotiation is incomplete because the client received
|
|
|
|
* an application data record while awaiting the ServerHello.
|
|
|
|
* 3) The renegotiation is incomplete because the client received
|
|
|
|
* a non-handshake, non-application data message while awaiting
|
|
|
|
* the ServerHello.
|
2021-04-21 08:30:13 +02:00
|
|
|
*
|
|
|
|
* In each of these cases, looping will be the proper action:
|
2020-02-05 15:44:10 +01:00
|
|
|
* - For 1), the next iteration will read a new record and check
|
|
|
|
* if it's application data.
|
|
|
|
* - For 2), the loop condition isn't satisfied as application data
|
|
|
|
* is present, hence continue is the same as break
|
|
|
|
* - For 3), the loop condition is satisfied and read_record
|
|
|
|
* will re-deliver the message that was held back by the client
|
|
|
|
* when expecting the ServerHello.
|
|
|
|
*/
|
2021-04-21 08:30:13 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-05-04 13:35:39 +02:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
|
|
|
|
if (ssl->conf->renego_max_records >= 0) {
|
|
|
|
if (++ssl->renego_records_seen > ssl->conf->renego_max_records) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation requested, "
|
|
|
|
"but not honored by client"));
|
|
|
|
return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
|
2015-06-17 13:53:47 +02:00
|
|
|
}
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_RENEGOTIATION */
|
2015-06-17 13:53:47 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("ignoring non-fatal non-closure alert"));
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_READ;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2015-06-17 13:53:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("bad application data message"));
|
|
|
|
return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2015-06-17 13:53:47 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_offt = ssl->in_msg;
|
2015-06-17 13:53:47 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* We're going to return something now, cancel timer,
|
|
|
|
* except if handshake (renegotiation) is in progress */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_is_handshake_over(ssl) == 1) {
|
|
|
|
mbedtls_ssl_set_timer(ssl, 0);
|
|
|
|
}
|
2015-06-17 13:53:47 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2020-02-05 15:44:10 +01:00
|
|
|
/* If we requested renego but received AppData, resend HelloRequest.
|
|
|
|
* Do it now, after setting in_offt, to avoid taking this branch
|
|
|
|
* again if ssl_write_hello_request() returns WANT_WRITE */
|
|
|
|
#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
|
|
|
|
ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
|
|
|
|
if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request",
|
|
|
|
ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2015-06-17 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
n = (len < ssl->in_msglen)
|
2020-02-05 15:44:10 +01:00
|
|
|
? len : ssl->in_msglen;
|
2015-05-04 13:35:39 +02:00
|
|
|
|
2022-02-16 23:08:27 +01:00
|
|
|
if (len != 0) {
|
2022-02-10 21:57:18 +01:00
|
|
|
memcpy(buf, ssl->in_offt, n);
|
|
|
|
ssl->in_msglen -= n;
|
|
|
|
}
|
2015-05-04 13:35:39 +02:00
|
|
|
|
2020-07-15 10:55:00 +02:00
|
|
|
/* Zeroising the plaintext buffer to erase unused application data
|
|
|
|
from the memory. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(ssl->in_offt, n);
|
2020-07-15 10:55:00 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msglen == 0) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* all bytes consumed */
|
|
|
|
ssl->in_offt = NULL;
|
|
|
|
ssl->keep_current_message = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2020-02-05 15:44:10 +01:00
|
|
|
/* more data available */
|
|
|
|
ssl->in_offt += n;
|
2015-05-04 13:35:39 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= read"));
|
2015-05-04 13:35:39 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return (int) n;
|
2015-05-04 13:35:39 +02:00
|
|
|
}
|
|
|
|
|
2013-08-21 16:14:26 +02:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Send application data to be encrypted by the SSL layer, taking care of max
|
|
|
|
* fragment length and buffer size.
|
|
|
|
*
|
|
|
|
* According to RFC 5246 Section 6.2.1:
|
|
|
|
*
|
|
|
|
* Zero-length fragments of Application data MAY be sent as they are
|
|
|
|
* potentially useful as a traffic analysis countermeasure.
|
|
|
|
*
|
|
|
|
* Therefore, it is possible that the input message length is 0 and the
|
|
|
|
* corresponding return code is 0 on success.
|
2013-08-21 16:14:26 +02:00
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_write_real(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf, size_t len)
|
2013-08-21 16:14:26 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
int ret = mbedtls_ssl_get_max_out_record_payload(ssl);
|
2020-02-05 15:44:10 +01:00
|
|
|
const size_t max_len = (size_t) ret;
|
2013-08-21 16:14:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_max_out_record_payload", ret);
|
|
|
|
return ret;
|
2017-04-28 18:15:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (len > max_len) {
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("fragment larger than the (negotiated) "
|
|
|
|
"maximum fragment length: %" MBEDTLS_PRINTF_SIZET
|
|
|
|
" > %" MBEDTLS_PRINTF_SIZET,
|
|
|
|
len, max_len));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
} else
|
2013-08-22 13:52:48 +02:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
len = max_len;
|
2013-08-22 13:52:48 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->out_left != 0) {
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* The user has previously tried to send the data and
|
|
|
|
* MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
|
|
|
|
* written. In this case, we expect the high-level write function
|
|
|
|
* (e.g. mbedtls_ssl_write()) to be called with the same parameters
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* The user is trying to send a message the first time, so we need to
|
|
|
|
* copy the data into the internal buffers and setup the data structure
|
|
|
|
* to keep track of partial writes
|
|
|
|
*/
|
|
|
|
ssl->out_msglen = len;
|
|
|
|
ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
|
2023-02-24 16:41:34 +01:00
|
|
|
if (len > 0) {
|
|
|
|
memcpy(ssl->out_msg, buf, len);
|
|
|
|
}
|
2017-04-28 18:15:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2017-04-28 18:15:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return (int) len;
|
2017-04-28 18:15:26 +02:00
|
|
|
}
|
|
|
|
|
2014-02-04 16:18:07 +01:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Write application data (public-facing wrapper)
|
2014-02-04 16:18:07 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len)
|
2014-02-04 16:18:07 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2014-02-04 16:18:07 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> write"));
|
2015-06-17 12:10:46 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl == NULL || ssl->conf == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2014-02-04 16:18:07 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = ssl_check_ctr_renegotiate(ssl)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
|
|
|
|
if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_write_real(ssl, buf, len);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= write"));
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2014-02-04 16:18:07 +01:00
|
|
|
}
|
2014-04-09 09:50:57 +02:00
|
|
|
|
2015-06-17 14:34:48 +02:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Notify the peer that the connection is being closed
|
2015-06-17 14:34:48 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_close_notify(mbedtls_ssl_context *ssl)
|
2015-06-17 14:34:48 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-06-17 14:34:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl == NULL || ssl->conf == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2015-06-17 14:34:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> write close notify"));
|
2015-06-17 14:34:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_is_handshake_over(ssl) == 1) {
|
|
|
|
if ((ret = mbedtls_ssl_send_alert_message(ssl,
|
|
|
|
MBEDTLS_SSL_ALERT_LEVEL_WARNING,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_send_alert_message", ret);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= write close notify"));
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2015-06-17 14:34:48 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_transform_free(mbedtls_ssl_transform *transform)
|
2014-04-09 09:50:57 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (transform == NULL) {
|
2020-02-05 15:44:10 +01:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2022-01-11 08:28:13 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_destroy_key(transform->psa_key_enc);
|
|
|
|
psa_destroy_key(transform->psa_key_dec);
|
2022-01-19 16:00:22 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_cipher_free(&transform->cipher_ctx_enc);
|
|
|
|
mbedtls_cipher_free(&transform->cipher_ctx_dec);
|
2022-01-19 16:00:22 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-01-11 08:28:13 +01:00
|
|
|
|
2020-11-30 09:54:23 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
|
2022-02-23 09:24:45 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_destroy_key(transform->psa_mac_enc);
|
|
|
|
psa_destroy_key(transform->psa_mac_dec);
|
2022-02-24 11:17:45 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_free(&transform->md_ctx_enc);
|
|
|
|
mbedtls_md_free(&transform->md_ctx_dec);
|
2022-02-24 11:17:45 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2014-04-11 11:06:22 +02:00
|
|
|
#endif
|
2014-04-09 09:50:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(transform, sizeof(mbedtls_ssl_transform));
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2014-04-09 09:50:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_set_inbound_transform(mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_ssl_transform *transform)
|
2021-09-05 15:05:50 +02:00
|
|
|
{
|
|
|
|
ssl->transform_in = transform;
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(ssl->in_ctr, 0, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
|
2021-09-05 15:05:50 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_set_outbound_transform(mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_ssl_transform *transform)
|
2021-09-05 15:05:50 +02:00
|
|
|
{
|
|
|
|
ssl->transform_out = transform;
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
|
2021-09-05 15:05:50 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2014-04-09 09:50:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_buffering_free(mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
|
|
|
unsigned offset;
|
|
|
|
mbedtls_ssl_handshake_params * const hs = ssl->handshake;
|
2014-04-09 09:50:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hs == NULL) {
|
2020-02-05 15:44:10 +01:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2014-04-09 09:50:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_free_buffered_record(ssl);
|
2014-04-09 09:50:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) {
|
|
|
|
ssl_buffering_free_slot(ssl, offset);
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl,
|
|
|
|
uint8_t slot)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
|
|
|
mbedtls_ssl_handshake_params * const hs = ssl->handshake;
|
|
|
|
mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (slot >= MBEDTLS_SSL_MAX_BUFFERED_HS) {
|
2020-02-05 15:44:10 +01:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2014-04-11 11:06:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hs_buf->is_valid == 1) {
|
2020-02-05 15:44:10 +01:00
|
|
|
hs->buffering.total_bytes_buffered -= hs_buf->data_len;
|
2023-07-17 16:17:40 +02:00
|
|
|
mbedtls_zeroize_and_free(hs_buf->data, hs_buf->data_len);
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer));
|
2015-04-20 12:01:48 +02:00
|
|
|
}
|
2014-04-09 09:50:57 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2014-04-29 15:11:17 +02:00
|
|
|
|
2014-02-11 18:15:03 +01:00
|
|
|
/*
|
|
|
|
* Convert version numbers to/from wire format
|
|
|
|
* and, for DTLS, to/from TLS equivalent.
|
|
|
|
*
|
|
|
|
* For TLS this is the identity.
|
2022-03-15 08:23:42 +01:00
|
|
|
* For DTLS, map as follows, then use 1's complement (v -> ~v):
|
2014-02-11 18:15:03 +01:00
|
|
|
* 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
|
2022-03-15 08:23:42 +01:00
|
|
|
* DTLS 1.0 is stored as TLS 1.1 internally
|
2014-02-11 18:15:03 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_write_version(unsigned char version[2], int transport,
|
|
|
|
mbedtls_ssl_protocol_version tls_version)
|
2014-02-11 18:15:03 +01:00
|
|
|
{
|
2023-07-13 14:12:40 +02:00
|
|
|
uint16_t tls_version_formatted;
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
2023-07-13 14:12:40 +02:00
|
|
|
tls_version_formatted =
|
2023-01-11 14:50:10 +01:00
|
|
|
~(tls_version - (tls_version == 0x0302 ? 0x0202 : 0x0201));
|
2023-07-13 14:12:40 +02:00
|
|
|
} else
|
2014-03-25 13:36:22 +01:00
|
|
|
#else
|
|
|
|
((void) transport);
|
2014-02-11 18:15:03 +01:00
|
|
|
#endif
|
2023-07-13 14:12:40 +02:00
|
|
|
{
|
|
|
|
tls_version_formatted = (uint16_t) tls_version;
|
|
|
|
}
|
|
|
|
MBEDTLS_PUT_UINT16_BE(tls_version_formatted, version, 0);
|
2014-02-11 18:15:03 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
uint16_t mbedtls_ssl_read_version(const unsigned char version[2],
|
|
|
|
int transport)
|
2014-02-11 18:15:03 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
uint16_t tls_version = MBEDTLS_GET_UINT16_BE(version, 0);
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
2022-03-15 08:23:42 +01:00
|
|
|
tls_version =
|
2023-01-11 14:50:10 +01:00
|
|
|
~(tls_version - (tls_version == 0xfeff ? 0x0202 : 0x0201));
|
|
|
|
}
|
2014-03-25 13:36:22 +01:00
|
|
|
#else
|
|
|
|
((void) transport);
|
2014-02-11 18:15:03 +01:00
|
|
|
#endif
|
2022-03-15 08:23:42 +01:00
|
|
|
return tls_version;
|
2014-02-11 18:15:03 +01:00
|
|
|
}
|
|
|
|
|
2021-09-13 13:26:39 +02:00
|
|
|
/*
|
2021-09-22 15:37:18 +02:00
|
|
|
* Send pending fatal alert.
|
|
|
|
* 0, No alert message.
|
|
|
|
* !0, if mbedtls_ssl_send_alert_message() returned in error, the error code it
|
|
|
|
* returned, ssl->alert_reason otherwise.
|
2021-09-13 13:26:39 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_handle_pending_alert(mbedtls_ssl_context *ssl)
|
2021-09-13 13:26:39 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2021-09-18 14:50:22 +02:00
|
|
|
/* No pending alert, return success*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->send_alert == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-09-18 14:50:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_send_alert_message(ssl,
|
|
|
|
MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
ssl->alert_type);
|
2021-09-18 14:50:22 +02:00
|
|
|
|
2021-09-22 15:37:18 +02:00
|
|
|
/* If mbedtls_ssl_send_alert_message() returned with MBEDTLS_ERR_SSL_WANT_WRITE,
|
|
|
|
* do not clear the alert to be able to send it later.
|
2021-09-18 14:50:22 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
|
2021-09-14 16:17:21 +02:00
|
|
|
ssl->send_alert = 0;
|
2021-09-18 14:50:22 +02:00
|
|
|
}
|
2021-09-14 16:17:21 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2021-09-18 14:50:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ssl->alert_reason;
|
2021-09-13 13:26:39 +02:00
|
|
|
}
|
|
|
|
|
2021-09-14 16:17:21 +02:00
|
|
|
/*
|
|
|
|
* Set pending fatal alert flag.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_pend_fatal_alert(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char alert_type,
|
|
|
|
int alert_reason)
|
2021-09-14 16:17:21 +02:00
|
|
|
{
|
|
|
|
ssl->send_alert = 1;
|
|
|
|
ssl->alert_type = alert_type;
|
|
|
|
ssl->alert_reason = alert_reason;
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS_C */
|