From 03a86e783b6bb2a64229e07545b430f2e1239332 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Wed, 28 May 2025 12:01:14 +0200 Subject: [PATCH] test: suites: pkcs7/x509parse: add missing PSA_INIT and PSA_DONE Both PKCS7 and X509 rely on PK module under the hood and the latter can use PSA to store keys and perform operations. Therefore psa_crypto_init() must be called before any operation can be done with PKCS7 and X509. Signed-off-by: Valerio Setti --- tests/suites/test_suite_pkcs7.function | 18 ++++++++++++++++-- tests/suites/test_suite_x509parse.function | 8 ++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/tests/suites/test_suite_pkcs7.function b/tests/suites/test_suite_pkcs7.function index e5dc4bd19..0c4a00b9e 100644 --- a/tests/suites/test_suite_pkcs7.function +++ b/tests/suites/test_suite_pkcs7.function @@ -33,9 +33,17 @@ static int pkcs7_parse_buffer(unsigned char *pkcs7_buf, int buflen) void pkcs7_asn1_fail(data_t *pkcs7_buf) { int res; + + /* PKCS7 uses X509 which itself relies on PK under the hood and the latter + * can use PSA to store keys and perform operations so psa_crypto_init() + * must be called before. */ + USE_PSA_INIT(); + res = pkcs7_parse_buffer(pkcs7_buf->x, pkcs7_buf->len); TEST_ASSERT(res != MBEDTLS_PKCS7_SIGNED_DATA); +exit: + USE_PSA_DONE(); } /* END_CASE */ @@ -46,6 +54,11 @@ void pkcs7_parse(char *pkcs7_file, int res_expect) size_t buflen; int res; + /* PKCS7 uses X509 which itself relies on PK under the hood and the latter + * can use PSA to store keys and perform operations so psa_crypto_init() + * must be called before. */ + USE_PSA_INIT(); + res = mbedtls_pk_load_file(pkcs7_file, &pkcs7_buf, &buflen); TEST_EQUAL(res, 0); @@ -54,6 +67,7 @@ void pkcs7_parse(char *pkcs7_file, int res_expect) exit: mbedtls_free(pkcs7_buf); + USE_PSA_DONE(); } /* END_CASE */ @@ -77,7 +91,7 @@ void pkcs7_verify(char *pkcs7_file, mbedtls_pkcs7 pkcs7; mbedtls_x509_crt **crts = NULL; - MD_OR_USE_PSA_INIT(); + USE_PSA_INIT(); mbedtls_pkcs7_init(&pkcs7); @@ -166,6 +180,6 @@ exit: mbedtls_free(crts); mbedtls_free(data); mbedtls_free(pkcs7_buf); - MD_OR_USE_PSA_DONE(); + USE_PSA_DONE(); } /* END_CASE */ diff --git a/tests/suites/test_suite_x509parse.function b/tests/suites/test_suite_x509parse.function index 7bcac865e..8225adb27 100644 --- a/tests/suites/test_suite_x509parse.function +++ b/tests/suites/test_suite_x509parse.function @@ -1668,6 +1668,9 @@ void x509_crt_parse_subjectkeyid(char *file, data_t *subjectKeyId, int ref_ret) mbedtls_x509_crt crt; mbedtls_x509_crt_init(&crt); + /* X509 relies on PK under the hood and the latter can use PSA to store keys + * and perform operations so psa_crypto_init() must be called before. */ + USE_PSA_INIT(); TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret); @@ -1682,6 +1685,7 @@ void x509_crt_parse_subjectkeyid(char *file, data_t *subjectKeyId, int ref_ret) exit: mbedtls_x509_crt_free(&crt); + USE_PSA_DONE(); } /* END_CASE */ @@ -1697,6 +1701,9 @@ void x509_crt_parse_authoritykeyid(char *file, char name_buf[128]; mbedtls_x509_crt_init(&crt); + /* X509 relies on PK under the hood and the latter can use PSA to store keys + * and perform operations so psa_crypto_init() must be called before. */ + USE_PSA_INIT(); TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret); @@ -1748,6 +1755,7 @@ void x509_crt_parse_authoritykeyid(char *file, exit: mbedtls_x509_crt_free(&crt); + USE_PSA_DONE(); } /* END_CASE */