Skip to content

Commit b2b9068

Browse files
Merge pull request Mbed-TLS#8942 from valeriosetti/fix-null-dereference
[Bugfix] Fix null dereference in `mbedtls_pk_verify_ext()`
2 parents af14b89 + da47518 commit b2b9068

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Bugfix
2+
* Fix NULL pointer dereference in mbedtls_pk_verify_ext() when called using
3+
an opaque RSA context and specifying MBEDTLS_PK_RSASSA_PSS as key type.

library/pk.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,12 @@ int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
11261126
return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
11271127
}
11281128

1129+
/* Ensure the PK context is of the right type otherwise mbedtls_pk_rsa()
1130+
* below would return a NULL pointer. */
1131+
if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_RSA) {
1132+
return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1133+
}
1134+
11291135
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
11301136
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
11311137
const mbedtls_pk_rsassa_pss_options *pss_opts;

tests/suites/test_suite_pk.function

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,6 +2060,21 @@ void pk_psa_wrap_sign_ext(int pk_type, int key_bits, int key_pk_type, int md_alg
20602060
sig, sizeof(sig), &sig_len,
20612061
mbedtls_test_rnd_std_rand, NULL), 0);
20622062

2063+
/* verify_ext() is not supported when using an opaque context. */
2064+
if (key_pk_type == MBEDTLS_PK_RSASSA_PSS) {
2065+
mbedtls_pk_rsassa_pss_options pss_opts = {
2066+
.mgf1_hash_id = md_alg,
2067+
.expected_salt_len = MBEDTLS_RSA_SALT_LEN_ANY,
2068+
};
2069+
TEST_EQUAL(mbedtls_pk_verify_ext(key_pk_type, &pss_opts, &pk, md_alg,
2070+
hash, hash_len, sig, sig_len),
2071+
MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE);
2072+
} else {
2073+
TEST_EQUAL(mbedtls_pk_verify_ext(key_pk_type, NULL, &pk, md_alg,
2074+
hash, hash_len, sig, sig_len),
2075+
MBEDTLS_ERR_PK_TYPE_MISMATCH);
2076+
}
2077+
20632078
mbedtls_pk_free(&pk);
20642079
TEST_EQUAL(PSA_SUCCESS, psa_destroy_key(key_id));
20652080

0 commit comments

Comments
 (0)