test: fix USE_PSA_INIT/DONE for x509 test suite

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
This commit is contained in:
Valerio Setti 2023-04-19 14:53:36 +02:00
parent b79f7db9b0
commit 569c171015
2 changed files with 72 additions and 22 deletions

View file

@ -428,6 +428,7 @@ void x509_accessor_ext_types(int ext_type, int has_ext_type)
mbedtls_x509_crt crt;
int expected_result = ext_type & has_ext_type;
USE_PSA_INIT();
mbedtls_x509_crt_init(&crt);
crt.ext_types = ext_type;
@ -435,6 +436,7 @@ void x509_accessor_ext_types(int ext_type, int has_ext_type)
TEST_ASSERT(mbedtls_x509_crt_has_ext_type(&crt, has_ext_type) == expected_result);
mbedtls_x509_crt_free(&crt);
USE_PSA_DONE();
}
/* END_CASE */
@ -462,6 +464,7 @@ void x509_parse_san(char *crt_file, char *result_str, int parse_result)
char *p = buf;
size_t n = sizeof(buf);
USE_PSA_INIT();
mbedtls_x509_crt_init(&crt);
memset(buf, 0, 2000);
@ -492,6 +495,7 @@ void x509_parse_san(char *crt_file, char *result_str, int parse_result)
exit:
mbedtls_x509_crt_free(&crt);
USE_PSA_DONE();
}
/* END_CASE */
@ -502,6 +506,7 @@ void x509_cert_info(char *crt_file, char *result_str)
char buf[2000];
int res;
USE_PSA_INIT();
mbedtls_x509_crt_init(&crt);
memset(buf, 0, 2000);
@ -515,6 +520,7 @@ void x509_cert_info(char *crt_file, char *result_str)
exit:
mbedtls_x509_crt_free(&crt);
USE_PSA_DONE();
}
/* END_CASE */
@ -525,6 +531,7 @@ void mbedtls_x509_crl_info(char *crl_file, char *result_str)
char buf[2000];
int res;
USE_PSA_INIT();
mbedtls_x509_crl_init(&crl);
memset(buf, 0, 2000);
@ -538,6 +545,7 @@ void mbedtls_x509_crl_info(char *crl_file, char *result_str)
exit:
mbedtls_x509_crl_free(&crl);
USE_PSA_DONE();
}
/* END_CASE */
@ -547,6 +555,7 @@ void mbedtls_x509_crl_parse(char *crl_file, int result)
mbedtls_x509_crl crl;
char buf[2000];
USE_PSA_INIT();
mbedtls_x509_crl_init(&crl);
memset(buf, 0, 2000);
@ -554,6 +563,7 @@ void mbedtls_x509_crl_parse(char *crl_file, int result)
exit:
mbedtls_x509_crl_free(&crl);
USE_PSA_DONE();
}
/* END_CASE */
@ -564,6 +574,7 @@ void mbedtls_x509_csr_info(char *csr_file, char *result_str)
char buf[2000];
int res;
USE_PSA_INIT();
mbedtls_x509_csr_init(&csr);
memset(buf, 0, 2000);
@ -577,6 +588,7 @@ void mbedtls_x509_csr_info(char *csr_file, char *result_str)
exit:
mbedtls_x509_csr_free(&csr);
USE_PSA_DONE();
}
/* END_CASE */
@ -586,6 +598,7 @@ void x509_verify_info(int flags, char *prefix, char *result_str)
char buf[2000];
int res;
USE_PSA_INIT();
memset(buf, 0, sizeof(buf));
res = mbedtls_x509_crt_verify_info(buf, sizeof(buf), prefix, flags);
@ -593,6 +606,7 @@ void x509_verify_info(int flags, char *prefix, char *result_str)
TEST_ASSERT(res >= 0);
TEST_ASSERT(strcmp(buf, result_str) == 0);
USE_PSA_DONE();
}
/* END_CASE */
@ -616,13 +630,11 @@ void x509_verify_restart(char *crt_file, char *ca_file,
* - x509_verify() for server5 -> test-ca2: ~ 18800
* - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
*/
MD_OR_USE_PSA_INIT();
mbedtls_x509_crt_restart_init(&rs_ctx);
mbedtls_x509_crt_init(&crt);
mbedtls_x509_crt_init(&ca);
MD_OR_USE_PSA_INIT();
TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
@ -670,12 +682,11 @@ void x509_verify(char *crt_file, char *ca_file, char *crl_file,
char *cn_name = NULL;
const mbedtls_x509_crt_profile *profile;
MD_OR_USE_PSA_INIT();
mbedtls_x509_crt_init(&crt);
mbedtls_x509_crt_init(&ca);
mbedtls_x509_crl_init(&crl);
MD_OR_USE_PSA_INIT();
if (strcmp(cn_name_str, "NULL") != 0) {
cn_name = cn_name_str;
}
@ -756,6 +767,7 @@ void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
mbedtls_x509_crt ca;
uint32_t flags = 0;
USE_PSA_INIT();
mbedtls_x509_crt_init(&crt);
mbedtls_x509_crt_init(&ca);
@ -775,6 +787,7 @@ void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
exit:
mbedtls_x509_crt_free(&crt);
mbedtls_x509_crt_free(&ca);
USE_PSA_DONE();
}
/* END_CASE */
@ -788,12 +801,11 @@ void x509_verify_callback(char *crt_file, char *ca_file, char *name,
uint32_t flags = 0;
verify_print_context vrfy_ctx;
MD_OR_USE_PSA_INIT();
mbedtls_x509_crt_init(&crt);
mbedtls_x509_crt_init(&ca);
verify_print_init(&vrfy_ctx);
MD_OR_USE_PSA_INIT();
TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
@ -826,6 +838,7 @@ void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
char buf[2000];
int res = 0;
USE_PSA_INIT();
mbedtls_x509_crt_init(&crt);
memset(buf, 0, 2000);
@ -844,6 +857,7 @@ void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
}
exit:
mbedtls_x509_crt_free(&crt);
USE_PSA_DONE();
}
/* END_CASE */
@ -854,6 +868,7 @@ void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
char buf[2000];
int res = 0;
USE_PSA_INIT();
mbedtls_x509_crt_init(&crt);
memset(buf, 0, 2000);
@ -873,6 +888,7 @@ void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
exit:
mbedtls_x509_crt_free(&crt);
USE_PSA_DONE();
}
/* END_CASE */
@ -885,6 +901,7 @@ void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
mbedtls_x509_name head;
int ret;
USE_PSA_INIT();
memset(&head, 0, sizeof(head));
name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
@ -898,6 +915,7 @@ void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
TEST_EQUAL(ret, exp_ret);
mbedtls_free(name);
USE_PSA_DONE();
}
/* END_CASE */
@ -916,6 +934,7 @@ void mbedtls_x509_dn_get_next(char *name_str,
unsigned char buf[80], *out = NULL, *c;
const char *short_name;
USE_PSA_INIT();
memset(&parsed, 0, sizeof(parsed));
memset(buf, 0, sizeof(buf));
c = buf + sizeof(buf);
@ -964,6 +983,7 @@ exit:
mbedtls_free(out);
mbedtls_asn1_free_named_data_list(&names);
mbedtls_asn1_free_named_data_list_shallow(parsed.next);
USE_PSA_DONE();
}
/* END_CASE */
@ -972,6 +992,7 @@ void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
{
mbedtls_x509_crt crt;
USE_PSA_INIT();
mbedtls_x509_crt_init(&crt);
TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
@ -986,6 +1007,7 @@ void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
exit:
mbedtls_x509_crt_free(&crt);
USE_PSA_DONE();
}
/* END_CASE */
@ -994,6 +1016,7 @@ void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
{
mbedtls_x509_crt crt;
USE_PSA_INIT();
mbedtls_x509_crt_init(&crt);
TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
@ -1008,6 +1031,7 @@ void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
exit:
mbedtls_x509_crt_free(&crt);
USE_PSA_DONE();
}
/* END_CASE */
@ -1016,12 +1040,14 @@ void x509parse_crt_file(char *crt_file, int result)
{
mbedtls_x509_crt crt;
USE_PSA_INIT();
mbedtls_x509_crt_init(&crt);
TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == result);
exit:
mbedtls_x509_crt_free(&crt);
USE_PSA_DONE();
}
/* END_CASE */
@ -1036,6 +1062,7 @@ void x509parse_crt(data_t *buf, char *result_str, int result)
((void) result_str);
#endif
USE_PSA_INIT();
mbedtls_x509_crt_init(&crt);
TEST_ASSERT(mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len) == (result));
@ -1103,6 +1130,7 @@ void x509parse_crt(data_t *buf, char *result_str, int result)
exit:
mbedtls_x509_crt_free(&crt);
USE_PSA_DONE();
}
/* END_CASE */
@ -1119,6 +1147,7 @@ void x509parse_crt_cb(data_t *buf, char *result_str, int result)
((void) result_str);
#endif
USE_PSA_INIT();
oid.tag = MBEDTLS_ASN1_OID;
oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
@ -1157,6 +1186,7 @@ void x509parse_crt_cb(data_t *buf, char *result_str, int result)
exit:
mbedtls_x509_crt_free(&crt);
USE_PSA_DONE();
}
/* END_CASE */
@ -1167,6 +1197,7 @@ void x509parse_crl(data_t *buf, char *result_str, int result)
unsigned char output[2000];
int res;
USE_PSA_INIT();
mbedtls_x509_crl_init(&crl);
memset(output, 0, 2000);
@ -1183,6 +1214,7 @@ void x509parse_crl(data_t *buf, char *result_str, int result)
exit:
mbedtls_x509_crl_free(&crl);
USE_PSA_DONE();
}
/* END_CASE */
@ -1193,6 +1225,7 @@ void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
char my_out[1000];
int my_ret;
USE_PSA_INIT();
mbedtls_x509_csr_init(&csr);
memset(my_out, 0, sizeof(my_out));
@ -1207,6 +1240,7 @@ void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
exit:
mbedtls_x509_csr_free(&csr);
USE_PSA_DONE();
}
/* END_CASE */
@ -1217,6 +1251,7 @@ void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
char my_out[1000];
int my_ret;
USE_PSA_INIT();
mbedtls_x509_csr_init(&csr);
memset(my_out, 0, sizeof(my_out));
@ -1231,6 +1266,7 @@ void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
exit:
mbedtls_x509_csr_free(&csr);
USE_PSA_DONE();
}
/* END_CASE */
@ -1240,6 +1276,7 @@ void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
mbedtls_x509_crt chain, *cur;
int i;
USE_PSA_INIT();
mbedtls_x509_crt_init(&chain);
TEST_ASSERT(mbedtls_x509_crt_parse_path(&chain, crt_path) == ret);
@ -1255,6 +1292,7 @@ void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
exit:
mbedtls_x509_crt_free(&chain);
USE_PSA_DONE();
}
/* END_CASE */
@ -1271,12 +1309,10 @@ void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
* We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
* with NN.crt signed by NN-1.crt
*/
MD_OR_USE_PSA_INIT();
mbedtls_x509_crt_init(&trusted);
mbedtls_x509_crt_init(&chain);
MD_OR_USE_PSA_INIT();
/* Load trusted root */
TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, ca_file) == 0);
@ -1311,11 +1347,10 @@ void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
mbedtls_x509_crt trusted, chain;
const mbedtls_x509_crt_profile *profile = NULL;
MD_OR_USE_PSA_INIT();
mbedtls_x509_crt_init(&chain);
mbedtls_x509_crt_init(&trusted);
MD_OR_USE_PSA_INIT();
while ((act = mystrsep(&chain_paths, " ")) != NULL) {
TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, act) == 0);
}
@ -1353,7 +1388,7 @@ void x509_oid_desc(data_t *buf, char *ref_desc)
const char *desc = NULL;
int ret;
USE_PSA_INIT();
oid.tag = MBEDTLS_ASN1_OID;
oid.p = buf->x;
oid.len = buf->len;
@ -1368,6 +1403,7 @@ void x509_oid_desc(data_t *buf, char *ref_desc)
TEST_ASSERT(desc != NULL);
TEST_ASSERT(strcmp(desc, ref_desc) == 0);
}
USE_PSA_DONE();
}
/* END_CASE */
@ -1377,6 +1413,7 @@ void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
mbedtls_x509_buf oid;
char num_buf[100];
USE_PSA_INIT();
memset(num_buf, 0x2a, sizeof(num_buf));
oid.tag = MBEDTLS_ASN1_OID;
@ -1391,6 +1428,7 @@ void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
TEST_ASSERT(num_buf[ret] == 0);
TEST_ASSERT(strcmp(num_buf, numstr) == 0);
}
USE_PSA_DONE();
}
/* END_CASE */
@ -1399,6 +1437,7 @@ void x509_check_key_usage(char *crt_file, int usage, int ret)
{
mbedtls_x509_crt crt;
USE_PSA_INIT();
mbedtls_x509_crt_init(&crt);
TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
@ -1407,6 +1446,7 @@ void x509_check_key_usage(char *crt_file, int usage, int ret)
exit:
mbedtls_x509_crt_free(&crt);
USE_PSA_DONE();
}
/* END_CASE */
@ -1416,9 +1456,9 @@ void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
{
mbedtls_x509_crt crt;
USE_PSA_INIT();
mbedtls_x509_crt_init(&crt);
TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
TEST_ASSERT(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x,
@ -1426,6 +1466,7 @@ void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
exit:
mbedtls_x509_crt_free(&crt);
USE_PSA_DONE();
}
/* END_CASE */
@ -1438,6 +1479,7 @@ void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
unsigned char *start = buf;
unsigned char *end = buf;
USE_PSA_INIT();
memset(&time, 0x00, sizeof(time));
*end = (unsigned char) tag; end++;
*end = strlen(time_str);
@ -1455,6 +1497,7 @@ void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
TEST_ASSERT(min == time.min);
TEST_ASSERT(sec == time.sec);
}
USE_PSA_DONE();
}
/* END_CASE */
@ -1468,6 +1511,8 @@ void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
mbedtls_md_type_t my_msg_md, my_mgf_md;
int my_salt_len;
USE_PSA_INIT();
buf.p = params->x;
buf.len = params->len;
buf.tag = params_tag;
@ -1484,6 +1529,6 @@ void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
}
exit:
;;
USE_PSA_DONE();
}
/* END_CASE */

View file

@ -159,6 +159,8 @@ void x509_csr_check(char *key_file, char *cert_req_check_file, int md_type,
const char *san_dns_name = "example.com";
const char *san_uri_name = "http://pki.example.com/";
MD_OR_USE_PSA_INIT();
san_uri.node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
san_uri.node.san.unstructured_name.p = (unsigned char *) san_uri_name;
san_uri.node.san.unstructured_name.len = strlen(san_uri_name);
@ -177,8 +179,6 @@ void x509_csr_check(char *key_file, char *cert_req_check_file, int md_type,
mbedtls_x509write_csr_init(&req);
MD_OR_USE_PSA_INIT();
mbedtls_pk_init(&key);
TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL,
mbedtls_test_rnd_std_rand, NULL) == 0);
@ -266,12 +266,12 @@ void x509_csr_check_opaque(char *key_file, int md_type, int key_usage,
const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
mbedtls_test_rnd_pseudo_info rnd_info;
MD_OR_USE_PSA_INIT();
memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
mbedtls_x509write_csr_init(&req);
MD_OR_USE_PSA_INIT();
md_alg_psa = mbedtls_hash_info_psa_from_md((mbedtls_md_type_t) md_type);
TEST_ASSERT(md_alg_psa != MBEDTLS_MD_NONE);
@ -315,7 +315,7 @@ exit:
mbedtls_x509write_csr_free(&req);
mbedtls_pk_free(&key);
psa_destroy_key(key_id);
PSA_DONE();
MD_OR_USE_PSA_DONE();
}
/* END_CASE */
@ -351,13 +351,13 @@ void x509_crt_check(char *subject_key_file, char *subject_pwd,
#endif
mbedtls_pk_type_t issuer_key_type;
MD_OR_USE_PSA_INIT();
memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
mbedtls_mpi_init(&serial_mpi);
#endif
MD_OR_USE_PSA_INIT();
mbedtls_pk_init(&subject_key);
mbedtls_pk_init(&issuer_key);
mbedtls_pk_init(&issuer_key_alt);
@ -597,6 +597,7 @@ void x509_set_serial_check()
mbedtls_x509write_cert ctx;
uint8_t invalid_serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN + 1];
USE_PSA_INIT();
memset(invalid_serial, 0x01, sizeof(invalid_serial));
#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
@ -619,6 +620,7 @@ exit:
#else
;
#endif
USE_PSA_DONE();
}
/* END_CASE */
@ -632,6 +634,8 @@ void mbedtls_x509_string_to_names(char *name, char *parsed_name, int result
mbedtls_x509_name parsed, *parsed_cur, *parsed_prv;
unsigned char buf[1024], out[1024], *c;
USE_PSA_INIT();
memset(&parsed, 0, sizeof(parsed));
memset(out, 0, sizeof(out));
memset(buf, 0, sizeof(buf));
@ -665,5 +669,6 @@ exit:
parsed_cur = parsed_cur->next;
mbedtls_free(parsed_prv);
}
USE_PSA_DONE();
}
/* END_CASE */