Compare commits

...

16 Commits

Author SHA1 Message Date
Etienne Carriere
f7020940ce
Merge df620dae4304e6e8a6226624969e8ea1cc7d33a3 into 4fdb917aae2fb5876e04f88621630712a906a067 2026-10-27 14:20:54 +00:00
Gilles Peskine
4fdb917aae
Merge pull request #10966 from bjwtaylor/tl13-transform-resource-leak-3.6
Backport 3.6: Fix resource leak in populate transform
2026-10-27 12:34:56 +00:00
Ben Taylor
afa648aa44 Fix non-PSA TLS 1.3 transform cleanup
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-10-22 11:34:25 +01:00
Valerio Setti
a96c1dffcd
Merge pull request #10655 from bjwtaylor/no-program-failure-3.6
Backport 3.6: Add fix for -DENABLE_PROGRAMS=OFF build error
2026-10-21 15:41:20 +00:00
Gilles Peskine
2a96043825
Merge pull request #10984 from MechatronikMonkey/fix-empty-authority-key-id-3.6
Backport 3.6: x509_crt: accept empty AuthorityKeyIdentifier sequence
2026-10-21 15:12:17 +00:00
Ben Taylor
23370efdea Add comment explaining the derivation of the number of keys
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
(cherry picked from commit e0766f1b4a2133ea043e10cef86cc1c76f69b49b)
2026-10-21 08:24:45 +01:00
Ben Taylor
ad282114a1 Add ChangeLog for transform resource leak
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
(cherry picked from commit 5b4be7edaffe86553b4875f80c78c3befe4ae618)
2026-10-21 08:24:45 +01:00
Ben Taylor
3626aa2d34 Add fix for transform setup resource leak
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
(cherry picked from commit 0ad86e77b2e73100aa65f5148ea865a12caa87f0)
2026-10-21 08:24:45 +01:00
Ben Taylor
86ba516295 Add test for tansform setup resource leak
This is a manual backport, not a direct cherry-pick, because 3.6 can build TLS without PSA-backed transforms. Gate the regression on MBEDTLS_USE_PSA_CRYPTO.

Signed-off-by: Ben Taylor <ben.taylor@linaro.org>

(cherry picked from commit 95e95268a4c26608612f34f2bb76ff1dfa9ded0a)
2026-10-21 08:24:45 +01:00
Christopher Müller
7701d43f47 Add ChangeLog entry for empty AuthorityKeyIdentifier
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christopher Müller <chris@mechatronikmonkey.de>
2026-10-03 14:28:29 +02:00
Christopher Müller
a8c0f87bfb tests: x509parse: add empty and truncated AuthorityKeyIdentifier tests
An empty AuthorityKeyIdentifier SEQUENCE must parse. A [0] tag without
a length must still be rejected; that case already passed before the
fix and guards against loosening the check too much.

Backport to 3.6: the dependencies use the legacy configuration macros
(MBEDTLS_PK_CAN_ECDSA_SOME, MBEDTLS_ECP_HAVE_SECP256R1,
MBEDTLS_MD_CAN_SHA256) instead of PSA_WANT_*, and the expected error is
written as a sum, like the neighbouring AuthorityKeyIdentifier tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christopher Müller <chris@mechatronikmonkey.de>
2026-10-03 14:28:29 +02:00
Christopher Müller
955de69ed5 x509_crt: accept empty AuthorityKeyIdentifier sequence
All fields of AuthorityKeyIdentifier are OPTIONAL, so an empty SEQUENCE
(30 00) is syntactically valid. It was rejected with
MBEDTLS_ERR_X509_INVALID_EXTENSIONS + MBEDTLS_ERR_ASN1_OUT_OF_DATA,
because mbedtls_asn1_get_tag() reports OUT_OF_DATA rather than
UNEXPECTED_TAG when there is nothing left to parse.

Check for the empty sequence explicitly instead of treating OUT_OF_DATA
as an absent keyIdentifier: OUT_OF_DATA is also returned for a [0] tag
with a missing or truncated length, which must still be rejected.

Fixes #10253.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christopher Müller <chris@mechatronikmonkey.de>
2026-10-03 14:28:29 +02:00
Ben Taylor
b21300b7c2 Update Changelog
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-07-02 13:25:38 +01:00
Ben Taylor
f4dbfc21e6 Add changelog
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-07-02 13:25:38 +01:00
Ben Taylor
c606923ce5 Add fix for -DENABLE_PROGRAMS=OFF build error
Signed-off-by: Ben Taylor <ben.taylor@linaro.org>
2026-07-02 13:25:38 +01:00
Etienne Carriere
df620dae43 Fix build warning related to DTLS connect ID
Between mbedtls version 3.6.2 and 3.6.3, some header files inclusion
scheme have changed (P-R [1] and specifically [2]) leading to the build
warning shown below when building libmbedtls as a user TA library
in OP-TEE OS package [3]:

In file included from lib/libmbedtls/mbedtls/library/ssl_misc.h:18,
                 from lib/libmbedtls/mbedtls/library/ssl_debug_helpers_generated.c:14:
lib/libmbedtls/mbedtls/include/mbedtls/ssl.h:659:5: warning: "MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT" is not defined, evaluates to 0 [-Wundef]
  659 | #if MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 0
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix this by considering MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT only
when MBEDTLS_SSL_DTLS_CONNECTION_ID is defined. The build warning is
related to the definition of MBEDTLS_TLS_EXT_CID that is meaningful only
when MBEDTLS_SSL_DTLS_CONNECTION_ID is defined which is relevant only
when MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT is defined.

Fixes #10207.

Link: https://github.com/Mbed-TLS/mbedtls/pull/9671 [1]
Link: b81cd1af64 [2]
Link: https://github.com/OP-TEE/optee_os/pull/7337 [3]
Signed-off-by: Etienne Carriere <etienne.carriere@foss.st.com>
2025-06-05 10:42:36 +02:00
11 changed files with 180 additions and 0 deletions

View File

@ -0,0 +1,2 @@
Bugfix
* Fix failure of cmake -DENABLE_PROGRAMS=OFF. Fixes #10587.

View File

@ -0,0 +1,3 @@
Bugfix
* Fix a compilation warning in ssl.h when MBEDTLS_SSL_DTLS_CONNECTION_ID
and MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT are not defined. Fixes #10207.

View File

@ -0,0 +1,4 @@
Bugfix
* Fix resource leaks in TLS 1.3 transform setup when a setup step fails
after allocating cipher contexts or importing the encryption traffic key.
Fixes #10917.

View File

@ -0,0 +1,7 @@
Bugfix
* Accept X.509 certificates whose AuthorityKeyIdentifier extension is an
empty SEQUENCE. All of its fields are optional, so such certificates
are syntactically valid, but they were rejected with
MBEDTLS_ERR_X509_INVALID_EXTENSIONS. This affected, for example, the
root certificate that some antivirus products install for HTTPS
scanning. Reported by CodingMarkus. Fixes #10253.

View File

@ -656,11 +656,17 @@
#define MBEDTLS_TLS_EXT_SIG_ALG_CERT 50 /* RFC 8446 TLS 1.3 */
#define MBEDTLS_TLS_EXT_KEY_SHARE 51 /* RFC 8446 TLS 1.3 */
/*
* MBEDTLS_TLS_EXT_CID is required only when
* MBEDTLS_SSL_DTLS_CONNECTION_ID is defined.
*/
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
#if MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 0
#define MBEDTLS_TLS_EXT_CID 54 /* RFC 9146 DTLS 1.2 CID */
#else
#define MBEDTLS_TLS_EXT_CID 254 /* Pre-RFC 9146 DTLS 1.2 CID */
#endif
#endif
#define MBEDTLS_TLS_EXT_ECJPAKE_KKPP 256 /* experimental */

View File

@ -977,6 +977,7 @@ int mbedtls_ssl_tls13_populate_transform(
if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
cipher_info)) != 0) {
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
mbedtls_ssl_transform_free(transform);
return ret;
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */
@ -999,6 +1000,9 @@ int mbedtls_ssl_tls13_populate_transform(
#endif /* MBEDTLS_SSL_CLI_C */
{
/* should not happen */
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_ssl_transform_free(transform);
#endif
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
}
@ -1010,6 +1014,7 @@ int mbedtls_ssl_tls13_populate_transform(
key_enc, (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
MBEDTLS_ENCRYPT)) != 0) {
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
mbedtls_ssl_transform_free(transform);
return ret;
}
@ -1017,6 +1022,7 @@ int mbedtls_ssl_tls13_populate_transform(
key_dec, (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
MBEDTLS_DECRYPT)) != 0) {
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
mbedtls_ssl_transform_free(transform);
return ret;
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */
@ -1081,6 +1087,8 @@ int mbedtls_ssl_tls13_populate_transform(
&transform->psa_key_dec)) != PSA_SUCCESS) {
MBEDTLS_SSL_DEBUG_RET(
1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
psa_destroy_key(transform->psa_key_enc);
transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
return PSA_TO_MBEDTLS_ERR(status);
}
}

View File

@ -641,6 +641,11 @@ static int x509_get_authority_key_id(unsigned char **p,
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
}
/* All fields are OPTIONAL, so an empty SEQUENCE is valid. */
if (*p == end) {
return 0;
}
ret = mbedtls_asn1_get_tag(p, end, &len,
MBEDTLS_ASN1_CONTEXT_SPECIFIC);

View File

@ -140,7 +140,9 @@ if(GEN_FILES)
add_custom_target(handshake-generated.sh
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/opt-testcases/handshake-generated.sh)
set_target_properties(handshake-generated.sh PROPERTIES EXCLUDE_FROM_ALL NO)
if(ENABLE_PROGRAMS)
add_dependencies(${ssl_opt_target} handshake-generated.sh)
endif()
add_custom_command(
OUTPUT
@ -195,8 +197,11 @@ if(GEN_FILES)
)
add_custom_target(tls13-compat.sh
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/opt-testcases/tls13-compat.sh)
if(ENABLE_PROGRAMS)
set_target_properties(tls13-compat.sh PROPERTIES EXCLUDE_FROM_ALL NO)
add_dependencies(${ssl_opt_target} tls13-compat.sh)
endif()
else()
foreach(file ${all_generated_data_files})

View File

@ -3003,6 +3003,9 @@ SSL TLS 1.3 Record Encryption RFC 8448 Example #2
depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_GCM:PSA_WANT_ALG_SHA_256
ssl_tls13_record_protection:MBEDTLS_TLS1_3_AES_128_GCM_SHA256:MBEDTLS_SSL_IS_SERVER:1:1:"9f02283b6c9c07efc26bb9f2ac92e356":"cf782b88dd83549aadf1e984":"17422dda596ed5d9acd890e3c63f5051":"5b78923dee08579033e523d9":"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031":"2e937e11ef4ac740e538ad36005fc4a46932fc3225d05f82aa1b36e30efaf97d90e6dffc602dcb501a59a8fcc49c4bf2e5f0a21c0047c2abf332540dd032e167c2955d"
TLS 1.3 transform frees encryption key if decryption key import fails
ssl_tls13_populate_transform_second_key_import_failure
SSL TLS 1.3 Key schedule: Application secrets derivation helper
# Vector from RFC 8448
depends_on:PSA_WANT_ALG_SHA_256

View File

@ -5,6 +5,7 @@
#include <mbedtls/pk.h>
#include <ssl_tls13_keys.h>
#include <ssl_tls13_invasive.h>
#include <psa_crypto_slot_management.h>
#include <test/ssl_helpers.h>
#include <test/ssl_helpers_internal.h>
@ -34,6 +35,39 @@
#define TEST_SSL_CONTEXT_LOAD_OOB_OUT_CID 1
#define TEST_SSL_CONTEXT_LOAD_OOB_ALPN 2
#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_SSL_CLI_C) && \
defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
defined(MBEDTLS_USE_PSA_CRYPTO)
#define SSL_TEST_KEY_SLICE_1_LENGTH 4
#define SSL_TEST_KEY_SLICE_2_LENGTH 10
static size_t ssl_test_tiny_key_slice_length(size_t slice_idx)
{
switch (slice_idx) {
case 1: return SSL_TEST_KEY_SLICE_1_LENGTH;
case 2: return SSL_TEST_KEY_SLICE_2_LENGTH;
default: return 1;
}
}
/* In the below definition SSL_TEST_KEY_SLICE_1_LENGTH has 4 slots and
* SSL_TEST_KEY_SLICE_2_LENGTH has 10 all other slices have 1.
* psa_key_slot_volatile_slice_count() returns the total number of slices.
* However we have already countes slices 1 and 2, so we mustr subtract 2 from
* this number to avoid double counting.
*/
#define SSL_TEST_MAX_VOLATILE_KEYS \
(SSL_TEST_KEY_SLICE_1_LENGTH + \
SSL_TEST_KEY_SLICE_2_LENGTH + \
psa_key_slot_volatile_slice_count() - 2)
#endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_SSL_CLI_C &&
MBEDTLS_PSA_CRYPTO_C && MBEDTLS_SSL_PROTO_TLS1_3 &&
MBEDTLS_USE_PSA_CRYPTO */
#else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
#define SSL_TEST_MAX_VOLATILE_KEYS MBEDTLS_PSA_KEY_SLOT_COUNT
#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
defined(MBEDTLS_SSL_SESSION_TICKETS) && \
defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SRV_C) && \
@ -2461,6 +2495,85 @@ exit:
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_SSL_CLI_C:SSL_TEST_MAX_VOLATILE_KEYS:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_USE_PSA_CRYPTO */
void ssl_tls13_populate_transform_second_key_import_failure(void)
{
mbedtls_ssl_key_set traffic_keys;
mbedtls_ssl_transform transform;
mbedtls_svc_key_id_t *keys = NULL;
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mbedtls_psa_stats_t stats_before, stats_after;
psa_status_t status = PSA_SUCCESS;
size_t i = 0;
memset(&traffic_keys, 0, sizeof(traffic_keys));
mbedtls_ssl_transform_init(&transform);
#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC) && defined(MBEDTLS_TEST_HOOKS)
mbedtls_test_hook_psa_volatile_key_slice_length =
ssl_test_tiny_key_slice_length;
#endif
PSA_INIT();
TEST_CALLOC(keys, SSL_TEST_MAX_VOLATILE_KEYS + 1);
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
psa_set_key_type(&attributes, PSA_KEY_TYPE_RAW_DATA);
/* Allow one more attempt than the maximum number of volatile keys so
* that the loop ends because the key store is full. */
while (i <= SSL_TEST_MAX_VOLATILE_KEYS) {
status = psa_import_key(&attributes,
(const uint8_t *) &i, sizeof(i),
&keys[i]);
if (status == PSA_ERROR_INSUFFICIENT_MEMORY) {
break;
}
PSA_ASSERT(status);
i++;
}
TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_MEMORY);
TEST_ASSERT(i > 0);
/* Leave exactly one slot free: the encryption key import consumes it,
* then the decryption key import fails. */
PSA_ASSERT(psa_destroy_key(keys[--i]));
keys[i] = MBEDTLS_SVC_KEY_ID_INIT;
mbedtls_psa_get_stats(&stats_before);
traffic_keys.key_len = 16;
traffic_keys.iv_len = 12;
TEST_ASSERT(mbedtls_ssl_tls13_populate_transform(
&transform, MBEDTLS_SSL_IS_CLIENT,
MBEDTLS_TLS1_3_AES_128_GCM_SHA256,
&traffic_keys, NULL) != 0);
/* The first import must be rolled back when the second one fails. */
TEST_ASSERT(mbedtls_svc_key_id_is_null(transform.psa_key_enc));
/* Also check the key store itself: the failed operation must neither
* consume a slot nor leave the only free slot unusable. */
mbedtls_psa_get_stats(&stats_after);
TEST_EQUAL(stats_after.volatile_slots, stats_before.volatile_slots);
TEST_EQUAL(stats_after.empty_slots, stats_before.empty_slots);
PSA_ASSERT(psa_import_key(&attributes,
(const uint8_t *) &i, sizeof(i),
&keys[i]));
i++;
exit:
mbedtls_ssl_transform_free(&transform);
while (i > 0) {
psa_destroy_key(keys[--i]);
}
mbedtls_free(keys);
PSA_DONE();
#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC) && defined(MBEDTLS_TEST_HOOKS)
mbedtls_test_hook_psa_volatile_key_slice_length = NULL;
#endif
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
void ssl_tls13_key_evolution(int hash_alg,
data_t *secret,

View File

@ -3478,3 +3478,27 @@ x509_crt_parse_authoritykeyid:"../framework/data_files/authorityKeyId_subjectKey
X509 CRT parse Authority Key Id - Wrong Issuer sequence
depends_on:MBEDTLS_MD_CAN_MD5:MBEDTLS_RSA_C
x509_crt_parse_authoritykeyid:"../framework/data_files/clusterfuzz-testcase-minimized-fuzz_x509crt-6666050834661376.crt.der":"":"":"":MBEDTLS_ERR_X509_INVALID_EXTENSIONS+MBEDTLS_ERR_ASN1_OUT_OF_DATA
# Self-signed certificate whose AuthorityKeyIdentifier is an empty SEQUENCE (30 00).
# All fields of AuthorityKeyIdentifier are OPTIONAL in the ASN.1 module (RFC 5280,
# Appendix A.2), so this is syntactically valid; some antivirus products install
# such a root for HTTPS scanning. Generated with Python cryptography:
# key = ec.generate_private_key(ec.SECP256R1())
# name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Test")])
# cert = (x509.CertificateBuilder().subject_name(name).issuer_name(name)
# .public_key(key.public_key()).serial_number(1)
# .not_valid_before(datetime.datetime(2025, 1, 1))
# .not_valid_after(datetime.datetime(2035, 1, 1))
# .add_extension(x509.BasicConstraints(ca=True, path_length=None), critical=True)
# .add_extension(x509.UnrecognizedExtension(ObjectIdentifier("2.5.29.35"),
# b"\x30\x00"), critical=False)
# .sign(key, hashes.SHA256()))
X509 CRT parse Authority Key Id - Empty sequence
depends_on:MBEDTLS_PK_CAN_ECDSA_SOME:MBEDTLS_ECP_HAVE_SECP256R1:MBEDTLS_MD_CAN_SHA256
x509parse_crt:"3082012b3081d1a003020102020101300a06082a8648ce3d040302300f310d300b06035504030c0454657374301e170d3235303130313030303030305a170d3335303130313030303030305a300f310d300b06035504030c04546573743059301306072a8648ce3d020106082a8648ce3d03010703420004f8cf6bd9eb57f5fdc8771103e9f75becb6a865b1a5de8492b3233170f49e1ff388d25d24376d130c86a8acb5b0478d7c49b83f07ffbd0684c26a0da42c752256a31e301c300f0603551d130101ff040530030101ff30090603551d2304023000300a06082a8648ce3d0403020349003046022100cffb50bdb73287649de96d4c48fcbb5d6e1eb468c72c5016776a9fdf0dc3d295022100e1b4b7ecba9ca1b43882a688c6fd93ffc99112247cf3ebb852b07f09d7c76be2":"cert. version \: 3\nserial number \: 01\nissuer name \: CN=Test\nsubject name \: CN=Test\nissued on \: 2025-01-01 00\:00\:00\nexpires on \: 2035-01-01 00\:00\:00\nsigned using \: ECDSA with SHA256\nEC key size \: 256 bits\nbasic constraints \: CA=true\n":0
# Generated the same way as above, with extension value b"\x30\x01\x80":
# a [0] tag without a length. This must still be rejected.
X509 CRT parse Authority Key Id - Truncated keyIdentifier
depends_on:MBEDTLS_PK_CAN_ECDSA_SOME:MBEDTLS_ECP_HAVE_SECP256R1:MBEDTLS_MD_CAN_SHA256
x509parse_crt:"3082012b3081d2a003020102020101300a06082a8648ce3d040302300f310d300b06035504030c0454657374301e170d3235303130313030303030305a170d3335303130313030303030305a300f310d300b06035504030c04546573743059301306072a8648ce3d020106082a8648ce3d03010703420004eba1e439f91038837190c4da3703001dff24411b90bf6998f56cba76c753c19ca9158f3c243ccb2118f8b6a78b6eb507962e855bf7c13dbafc314dc56d5d27d8a31f301d300f0603551d130101ff040530030101ff300a0603551d230403300180300a06082a8648ce3d040302034800304502205443c61342f55d3d4cec26df92b7614a5e67177f057efd4f6fb9d7a1a47f5d110221008f05c4c193e203d78a322455132f1b4428388904874dd329fc60a68a37a559b4":"":MBEDTLS_ERR_X509_INVALID_EXTENSIONS+MBEDTLS_ERR_ASN1_OUT_OF_DATA