From a38ea44d97fbf613f1730b395eafac1cdadbad0a Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Fri, 29 May 2026 18:22:51 +0200 Subject: [PATCH] pkcs7: fail verification if MD alg or sig_alg are not secure Add a check on mbedtls_pkcs7_data_or_hash_verify() so that the verification fails if the MD alg specified in PKCS7 structure or the signature algorithm specified in the X.509 certificate are not in the list of secure algorithms (i.e. mbedtls_x509_crt_profile_default). Signed-off-by: Valerio Setti --- library/pkcs7.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/library/pkcs7.c b/library/pkcs7.c index 3aac662ba6..cd56b4a5b7 100644 --- a/library/pkcs7.c +++ b/library/pkcs7.c @@ -666,6 +666,14 @@ static int mbedtls_pkcs7_data_or_hash_verify(mbedtls_pkcs7 *pkcs7, return ret; } + /* Ensure the MD alg from the PKCS#7 context and signature algorithm from + * the certificate belong to the list of secure algorithms + * (i.e. mbedtls_x509_crt_profile_default). */ + if (mbedtls_x509_profile_check_md_alg(&mbedtls_x509_crt_profile_default, md_alg) || + mbedtls_x509_profile_check_pk_alg(&mbedtls_x509_crt_profile_default, cert->sig_pk)) { + return MBEDTLS_ERR_PKCS7_VERIFY_FAIL; + } + md_info = mbedtls_md_info_from_type(md_alg); if (md_info == NULL) { return MBEDTLS_ERR_PKCS7_VERIFY_FAIL;