tests: x509parse: fix return values for invalid RSA keys
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
diff --git a/tests/suites/test_suite_x509parse.data b/tests/suites/test_suite_x509parse.data
index c7c465b..c0850b6 100644
--- a/tests/suites/test_suite_x509parse.data
+++ b/tests/suites/test_suite_x509parse.data
@@ -1770,11 +1770,11 @@
X509 CRT ASN1 (TBS, inv SubPubKeyInfo, inv internal bitstring tag)
depends_on:MBEDTLS_RSA_C:PSA_WANT_ALG_SHA_256
-x509parse_crt:"308180306ba0030201008204deadbeef300d06092a864886f70d01010b0500300c310a30080600130454657374301c170c303930313031303030303030170c303931323331323335393539300c310a300806001304546573743015300d06092A864886F70D0101010500030400310000300d06092a864886f70d01010b0500030200ff":"":MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)
+x509parse_crt:"308180306ba0030201008204deadbeef300d06092a864886f70d01010b0500300c310a30080600130454657374301c170c303930313031303030303030170c303931323331323335393539300c310a300806001304546573743015300d06092A864886F70D0101010500030400310000300d06092a864886f70d01010b0500030200ff":"":MBEDTLS_ERR_PK_INVALID_PUBKEY
X509 CRT ASN1 (TBS, inv SubPubKeyInfo, inv RSA modulus)
depends_on:MBEDTLS_RSA_C:PSA_WANT_ALG_SHA_256
-x509parse_crt:"3081873072a0030201008204deadbeef300d06092a864886f70d01010b0500300c310a30080600130454657374301c170c303930313031303030303030170c303931323331323335393539300c310a30080600130454657374301c300d06092A864886F70D0101010500030b0030080202ffff0302ffff300d06092a864886f70d01010b0500030200ff":"":MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)
+x509parse_crt:"3081873072a0030201008204deadbeef300d06092a864886f70d01010b0500300c310a30080600130454657374301c170c303930313031303030303030170c303931323331323335393539300c310a30080600130454657374301c300d06092A864886F70D0101010500030b0030080202ffff0302ffff300d06092a864886f70d01010b0500030200ff":"":MBEDTLS_ERR_PK_INVALID_PUBKEY
X509 CRT ASN1 (TBS, inv SubPubKeyInfo, total length mismatch)
depends_on:MBEDTLS_RSA_C:PSA_WANT_ALG_SHA_256
diff --git a/tests/suites/test_suite_x509parse.function b/tests/suites/test_suite_x509parse.function
index 09b248e..8f0da5a 100644
--- a/tests/suites/test_suite_x509parse.function
+++ b/tests/suites/test_suite_x509parse.function
@@ -1118,17 +1118,29 @@
void x509parse_crt(data_t *buf, char *result_str, int result)
{
mbedtls_x509_crt crt;
-#if !defined(MBEDTLS_X509_REMOVE_INFO)
+ #if !defined(MBEDTLS_X509_REMOVE_INFO)
unsigned char output[2000] = { 0 };
- int res;
-#else
+ #else
((void) result_str);
-#endif
+ #endif
+ /* Pick an error which is not used in the test_suite_x509parse.data file. */
+ int result_ext = MBEDTLS_ERR_ERROR_GENERIC_ERROR;
+ int res;
+
+#if !defined(MBEDTLS_PK_USE_PSA_RSA_DATA)
+ /* Support for mbedtls#10213 before psa#308. Once psa#308 will be
+ * merged this dirty fix can be removed. */
+ if (result == MBEDTLS_ERR_PK_INVALID_PUBKEY) {
+ result_ext = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
+ }
+#endif /* MBEDTLS_PK_USE_PSA_RSA_DATA */
mbedtls_x509_crt_init(&crt);
USE_PSA_INIT();
- TEST_EQUAL(mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len), result);
+ res = mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len);
+ fprintf(stderr, "\n res=%d, result=%d, result_ext=%d \n", res, result, result_ext);
+ TEST_ASSERT((res == result) || (res == result_ext));
#if !defined(MBEDTLS_X509_REMOVE_INFO)
if ((result) == 0) {
res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
@@ -1143,7 +1155,8 @@
mbedtls_x509_crt_free(&crt);
mbedtls_x509_crt_init(&crt);
- TEST_EQUAL(mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len), result);
+ res = mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len);
+ TEST_ASSERT((res == result) || (res == result_ext));
#if !defined(MBEDTLS_X509_REMOVE_INFO)
if ((result) == 0) {
memset(output, 0, 2000);
@@ -1161,8 +1174,8 @@
mbedtls_x509_crt_free(&crt);
mbedtls_x509_crt_init(&crt);
- TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL, NULL),
- result);
+ res = mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL, NULL);
+ TEST_ASSERT((res == result) || (res == result_ext));
#if !defined(MBEDTLS_X509_REMOVE_INFO)
if ((result) == 0) {
res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
@@ -1178,8 +1191,8 @@
mbedtls_x509_crt_free(&crt);
mbedtls_x509_crt_init(&crt);
- TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL, NULL),
- result);
+ res = mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL, NULL);
+ TEST_ASSERT((res == result) || (res == result_ext));
#if !defined(MBEDTLS_X509_REMOVE_INFO)
if ((result) == 0) {
res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);