blob: e30eed949d92b70a241c02836de53f5925292f0f [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Mohammad Azim Khancf32c452017-06-13 14:55:58 +01002#include "mbedtls/bignum.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00003#include "mbedtls/x509_crt.h"
4#include "mbedtls/x509_csr.h"
Valerio Setti25b282e2024-01-17 10:55:32 +01005#include "x509_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00006#include "mbedtls/pem.h"
Gilles Peskine86a47f82025-05-07 20:20:12 +02007#include "x509_oid.h"
Hanno Becker418a6222017-09-14 07:51:28 +01008#include "mbedtls/rsa.h"
Sam Berry2bb3f4d2024-07-19 15:14:27 +01009#include "mbedtls/asn1.h"
Valerio Setti48e8fc72022-10-19 15:14:29 +020010#include "mbedtls/asn1write.h"
Valerio Setti178b5bd2023-02-13 10:04:28 +010011#include "mbedtls/pk.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010012#include "mbedtls/psa_util.h"
Manuel Pégourié-Gonnardfeb03962020-08-20 09:59:33 +020013
Gilles Peskinef4e672e2020-01-31 14:22:10 +010014#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
15 defined(MBEDTLS_PEM_WRITE_C) && defined(MBEDTLS_X509_CSR_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010016static int x509_crt_verifycsr(const unsigned char *buf, size_t buflen)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050017{
Przemek Stekiel54a54462022-08-01 13:59:12 +020018 unsigned char hash[PSA_HASH_MAX_SIZE];
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050019 mbedtls_x509_csr csr;
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010020 int ret = 0;
21
Gilles Peskine449bd832023-01-11 14:50:10 +010022 mbedtls_x509_csr_init(&csr);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050023
Gilles Peskine449bd832023-01-11 14:50:10 +010024 if (mbedtls_x509_csr_parse(&csr, buf, buflen) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010025 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
26 goto cleanup;
27 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050028
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020029 psa_algorithm_t psa_alg = mbedtls_md_psa_alg_from_type(csr.sig_md);
Przemek Stekiel54a54462022-08-01 13:59:12 +020030 size_t hash_size = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010031 psa_status_t status = psa_hash_compute(psa_alg, csr.cri.p, csr.cri.len,
32 hash, PSA_HASH_MAX_SIZE, &hash_size);
Przemek Stekiel54a54462022-08-01 13:59:12 +020033
Gilles Peskine449bd832023-01-11 14:50:10 +010034 if (status != PSA_SUCCESS) {
Andrzej Kurek4b114072018-11-19 18:04:01 -050035 /* Note: this can't happen except after an internal error */
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010036 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
37 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050038 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050039
Valerio Setti7f6f4e62025-04-23 11:29:51 +020040 if (mbedtls_pk_verify_ext(csr.sig_pk, NULL, &csr.pk,
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +020041 csr.sig_md, hash, mbedtls_md_get_size_from_type(csr.sig_md),
Gilles Peskine449bd832023-01-11 14:50:10 +010042 csr.sig.p, csr.sig.len) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010043 ret = MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
44 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050045 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050046
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010047cleanup:
48
Gilles Peskine449bd832023-01-11 14:50:10 +010049 mbedtls_x509_csr_free(&csr);
50 return ret;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050051}
Gilles Peskinef4e672e2020-01-31 14:22:10 +010052#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_PEM_WRITE_C && MBEDTLS_X509_CSR_WRITE_C */
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050053
Valerio Setti48e8fc72022-10-19 15:14:29 +020054#if defined(MBEDTLS_X509_CSR_WRITE_C)
55
56/*
57 * The size of this temporary buffer is given by the sequence of functions
58 * called hereinafter:
59 * - mbedtls_asn1_write_oid()
60 * - 8 bytes for MBEDTLS_OID_EXTENDED_KEY_USAGE raw value
61 * - 1 byte for MBEDTLS_OID_EXTENDED_KEY_USAGE length
62 * - 1 byte for MBEDTLS_ASN1_OID tag
63 * - mbedtls_asn1_write_len()
64 * - 1 byte since we're dealing with sizes which are less than 0x80
65 * - mbedtls_asn1_write_tag()
66 * - 1 byte
67 *
68 * This length is fine as long as this function is called using the
69 * MBEDTLS_OID_SERVER_AUTH OID. If this is changed in the future, then this
70 * buffer's length should be adjusted accordingly.
71 * Unfortunately there's no predefined max size for OIDs which can be used
72 * to set an overall upper boundary which is always guaranteed.
73 */
74#define EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH 12
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076static int csr_set_extended_key_usage(mbedtls_x509write_csr *ctx,
77 const char *oid, size_t oid_len)
Valerio Setti48e8fc72022-10-19 15:14:29 +020078{
79 unsigned char buf[EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH] = { 0 };
Gilles Peskine449bd832023-01-11 14:50:10 +010080 unsigned char *p = buf + sizeof(buf);
Valerio Setti48e8fc72022-10-19 15:14:29 +020081 int ret;
82 size_t len = 0;
83
84 /*
85 * Following functions fail anyway if the temporary buffer is not large,
86 * but we set an extra check here to emphasize a possible source of errors
87 */
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if (oid_len > EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH) {
Valerio Setti48e8fc72022-10-19 15:14:29 +020089 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
90 }
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(&p, buf, oid, oid_len));
93 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, ret));
94 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, buf,
95 MBEDTLS_ASN1_CONSTRUCTED |
96 MBEDTLS_ASN1_SEQUENCE));
Valerio Setti48e8fc72022-10-19 15:14:29 +020097
Gilles Peskine449bd832023-01-11 14:50:10 +010098 ret = mbedtls_x509write_csr_set_extension(ctx,
99 MBEDTLS_OID_EXTENDED_KEY_USAGE,
100 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
101 0,
102 p,
103 len);
Valerio Setti48e8fc72022-10-19 15:14:29 +0200104
105 return ret;
106}
107#endif /* MBEDTLS_X509_CSR_WRITE_C */
Gilles Peskinec94500b2023-09-21 18:01:05 +0200108
109/* Due to inconsistencies in the input size limits applied by different
110 * library functions, some write-parse tests may fail. */
111#define MAY_FAIL_GET_NAME 0x0001
112#define MAY_FAIL_DN_GETS 0x0002
113
Paul Bakker33b43f12013-08-20 11:48:36 +0200114/* END_HEADER */
Paul Bakker6d620502012-02-16 14:09:13 +0000115
Paul Bakker33b43f12013-08-20 11:48:36 +0200116/* BEGIN_DEPENDENCIES
Valerio Setti3580f442023-07-27 10:19:53 +0200117 * depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_PARSE_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200118 * END_DEPENDENCIES
119 */
Paul Bakker6d620502012-02-16 14:09:13 +0000120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122void x509_csr_check(char *key_file, char *cert_req_check_file, int md_type,
123 int key_usage, int set_key_usage, int cert_type,
124 int set_cert_type, int set_extension)
Paul Bakker6d620502012-02-16 14:09:13 +0000125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_pk_context key;
127 mbedtls_x509write_csr req;
Andres AGe0af9952016-09-07 11:09:44 +0100128 unsigned char buf[4096];
Paul Bakker6d620502012-02-16 14:09:13 +0000129 int ret;
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100130#if !defined(MBEDTLS_USE_PSA_CRYPTO)
131 unsigned char check_buf[4000];
Paul Bakker6d620502012-02-16 14:09:13 +0000132 FILE *f;
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100133 size_t olen = 0;
134#endif /* !MBEDTLS_USE_PSA_CRYPTO */
135 size_t pem_len = 0, buf_index;
136 int der_len = -1;
Paul Bakker3a8cb6f2013-12-30 20:41:54 +0100137 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200138 mbedtls_test_rnd_pseudo_info rnd_info;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100139 mbedtls_x509_san_list san_ip;
140 mbedtls_x509_san_list san_dns;
141 mbedtls_x509_san_list san_uri;
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400142 mbedtls_x509_san_list san_mail;
143 mbedtls_x509_san_list san_dn;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100144 mbedtls_x509_san_list *san_list = NULL;
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400145 mbedtls_asn1_named_data *ext_san_dirname = NULL;
146
147 const char san_ip_name[] = { 0x7f, 0x00, 0x00, 0x01 }; // 127.0.0.1
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100148 const char *san_dns_name = "example.com";
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400149 const char *san_dn_name = "C=UK,O=Mbed TLS,CN=Mbed TLS directoryName SAN";
150 const char *san_mail_name = "mail@example.com";
151 const char *san_uri_name = "http://pki.example.com";
152
153 san_mail.node.type = MBEDTLS_X509_SAN_RFC822_NAME;
154 san_mail.node.san.unstructured_name.p = (unsigned char *) san_mail_name;
155 san_mail.node.san.unstructured_name.len = strlen(san_mail_name);
156 san_mail.next = NULL;
157
158 san_dns.node.type = MBEDTLS_X509_SAN_DNS_NAME;
159 san_dns.node.san.unstructured_name.p = (unsigned char *) san_dns_name;
160 san_dns.node.san.unstructured_name.len = strlen(san_dns_name);
161 san_dns.next = &san_mail;
162
163 san_dn.node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
164 TEST_ASSERT(mbedtls_x509_string_to_names(&ext_san_dirname,
165 san_dn_name) == 0);
166 san_dn.node.san.directory_name = *ext_san_dirname;
167 san_dn.next = &san_dns;
168
169 san_ip.node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
170 san_ip.node.san.unstructured_name.p = (unsigned char *) san_ip_name;
171 san_ip.node.san.unstructured_name.len = sizeof(san_ip_name);
172 san_ip.next = &san_dn;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100173
174 san_uri.node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100175 san_uri.node.san.unstructured_name.p = (unsigned char *) san_uri_name;
176 san_uri.node.san.unstructured_name.len = strlen(san_uri_name);
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400177 san_uri.next = &san_ip;
178
179 san_list = &san_uri;
Paul Bakker6d620502012-02-16 14:09:13 +0000180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 mbedtls_x509write_csr_init(&req);
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 mbedtls_pk_init(&key);
valerio32f2ac92023-04-20 11:59:52 +0200185 MD_OR_USE_PSA_INIT();
186
Ben Taylor440cb2a2025-03-05 09:40:08 +0000187 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL) == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 mbedtls_x509write_csr_set_md_alg(&req, md_type);
190 mbedtls_x509write_csr_set_key(&req, &key);
191 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
192 if (set_key_usage != 0) {
193 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
194 }
195 if (set_cert_type != 0) {
196 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
197 }
198 if (set_extension != 0) {
199 TEST_ASSERT(csr_set_extended_key_usage(&req, MBEDTLS_OID_SERVER_AUTH,
200 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) == 0);
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100201
202 TEST_ASSERT(mbedtls_x509write_csr_set_subject_alternative_name(&req, san_list) == 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 }
Paul Bakker8eabfc12013-08-25 10:18:25 +0200204
Ben Taylor440cb2a2025-03-05 09:40:08 +0000205 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf));
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 TEST_ASSERT(ret == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 pem_len = strlen((char *) buf);
Paul Bakker6d620502012-02-16 14:09:13 +0000209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
211 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000212 }
213
Neil Armstrong5b320382022-02-21 17:22:10 +0100214#if defined(MBEDTLS_USE_PSA_CRYPTO)
215 // When using PSA crypto, RNG isn't controllable, so cert_req_check_file can't be used
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 (void) cert_req_check_file;
Neil Armstrong5b320382022-02-21 17:22:10 +0100217 buf[pem_len] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0);
Neil Armstrong5b320382022-02-21 17:22:10 +0100219#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 f = fopen(cert_req_check_file, "r");
221 TEST_ASSERT(f != NULL);
222 olen = fread(check_buf, 1, sizeof(check_buf), f);
223 fclose(f);
Paul Bakker6d620502012-02-16 14:09:13 +0000224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 TEST_ASSERT(olen >= pem_len - 1);
226 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100227#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100228
Ben Taylor440cb2a2025-03-05 09:40:08 +0000229 der_len = mbedtls_x509write_csr_der(&req, buf, sizeof(buf));
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100233 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 }
Andres AGe0af9952016-09-07 11:09:44 +0100235
Neil Armstrong5b320382022-02-21 17:22:10 +0100236#if defined(MBEDTLS_USE_PSA_CRYPTO)
237 // When using PSA crypto, RNG isn't controllable, result length isn't
238 // deterministic over multiple runs, removing a single byte isn't enough to
239 // go into the MBEDTLS_ERR_ASN1_BUF_TOO_SMALL error case
240 der_len /= 2;
241#else
242 der_len -= 1;
243#endif
Ben Taylor440cb2a2025-03-05 09:40:08 +0000244 ret = mbedtls_x509write_csr_der(&req, buf, (size_t) (der_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100246
Paul Bakkerbd51b262014-07-10 15:26:12 +0200247exit:
Andrzej Kurekbdb41dd2023-07-10 08:09:50 -0400248 mbedtls_asn1_free_named_data_list(&ext_san_dirname);
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 mbedtls_x509write_csr_free(&req);
250 mbedtls_pk_free(&key);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100251 MD_OR_USE_PSA_DONE();
Paul Bakker6d620502012-02-16 14:09:13 +0000252}
Paul Bakker33b43f12013-08-20 11:48:36 +0200253/* END_CASE */
Paul Bakker2397cf32013-09-08 15:58:15 +0200254
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500255/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C:MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100256void x509_csr_check_opaque(char *key_file, int md_type, int key_usage,
257 int cert_type)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500258{
259 mbedtls_pk_context key;
Paul Elliott9a209b82024-10-25 12:41:28 +0100260 mbedtls_pk_init(&key);
261
Ronald Cron5425a212020-08-04 14:58:35 +0200262 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100263 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliott9a209b82024-10-25 12:41:28 +0100264
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500265 mbedtls_x509write_csr req;
Paul Elliott9a209b82024-10-25 12:41:28 +0100266 mbedtls_x509write_csr_init(&req);
267
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500268 unsigned char buf[4096];
269 int ret;
270 size_t pem_len = 0;
271 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200272 mbedtls_test_rnd_pseudo_info rnd_info;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500273
Valerio Setti569c1712023-04-19 14:53:36 +0200274 MD_OR_USE_PSA_INIT();
275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500277
Ben Taylor440cb2a2025-03-05 09:40:08 +0000278 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL) == 0);
Neil Armstrong95974972022-04-22 13:57:44 +0200279
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100280 /* Turn the PK context into an opaque one. */
281 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&key, PSA_KEY_USAGE_SIGN_HASH, &key_attr), 0);
282 TEST_EQUAL(mbedtls_pk_import_into_psa(&key, &key_attr, &key_id), 0);
283 mbedtls_pk_free(&key);
284 mbedtls_pk_init(&key);
285 TEST_EQUAL(mbedtls_pk_setup_opaque(&key, key_id), 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 mbedtls_x509write_csr_set_md_alg(&req, md_type);
288 mbedtls_x509write_csr_set_key(&req, &key);
289 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
290 if (key_usage != 0) {
291 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
292 }
293 if (cert_type != 0) {
294 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
295 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500296
Ben Taylor440cb2a2025-03-05 09:40:08 +0000297 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf) - 1);
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 TEST_ASSERT(ret == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 pem_len = strlen((char *) buf);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500302 buf[pem_len] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500304
Manuel Pégourié-Gonnardfeb03962020-08-20 09:59:33 +0200305
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500306exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 mbedtls_x509write_csr_free(&req);
308 mbedtls_pk_free(&key);
309 psa_destroy_key(key_id);
Valerio Setti569c1712023-04-19 14:53:36 +0200310 MD_OR_USE_PSA_DONE();
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500311}
312/* END_CASE */
313
Elena Uziunaite9fc5be02024-09-04 18:12:59 +0100314/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CRT_WRITE_C:MBEDTLS_X509_CRT_PARSE_C:PSA_WANT_ALG_SHA_1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100315void x509_crt_check(char *subject_key_file, char *subject_pwd,
316 char *subject_name, char *issuer_key_file,
317 char *issuer_pwd, char *issuer_name,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100318 data_t *serial_arg, char *not_before, char *not_after,
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 int md_type, int key_usage, int set_key_usage,
320 char *ext_key_usage,
321 int cert_type, int set_cert_type, int auth_ident,
322 int ver, char *cert_check_file, int pk_wrap, int is_ca,
Andrzej Kurek76c96622023-04-04 06:57:08 -0400323 char *cert_verify_file, int set_subjectAltNames)
Paul Bakker2397cf32013-09-08 15:58:15 +0200324{
Hanno Becker418a6222017-09-14 07:51:28 +0100325 mbedtls_pk_context subject_key, issuer_key, issuer_key_alt;
326 mbedtls_pk_context *key = &issuer_key;
327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_x509write_cert crt;
Andres AGe0af9952016-09-07 11:09:44 +0100329 unsigned char buf[4096];
Paul Bakker2397cf32013-09-08 15:58:15 +0200330 unsigned char check_buf[5000];
Werner Lewisacd01e52022-05-10 12:23:13 +0100331 unsigned char *p, *end;
332 unsigned char tag, sz;
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100333#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
334 mbedtls_mpi serial_mpi;
335#endif
Werner Lewisacd01e52022-05-10 12:23:13 +0100336 int ret, before_tag, after_tag;
Paul Elliott557b8d62020-11-19 09:46:56 +0000337 size_t olen = 0, pem_len = 0, buf_index = 0;
Andres AGe0af9952016-09-07 11:09:44 +0100338 int der_len = -1;
Paul Bakker2397cf32013-09-08 15:58:15 +0200339 FILE *f;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200340 mbedtls_test_rnd_pseudo_info rnd_info;
Neil Armstrong98f899c2022-03-16 17:42:42 +0100341#if defined(MBEDTLS_USE_PSA_CRYPTO)
342 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100343 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong98f899c2022-03-16 17:42:42 +0100344#endif
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100345 mbedtls_pk_type_t issuer_key_type;
Andrzej Kurek76c96622023-04-04 06:57:08 -0400346 mbedtls_x509_san_list san_ip;
347 mbedtls_x509_san_list san_dns;
348 mbedtls_x509_san_list san_uri;
349 mbedtls_x509_san_list san_mail;
350 mbedtls_x509_san_list san_dn;
351 mbedtls_asn1_named_data *ext_san_dirname = NULL;
352 const char san_ip_name[] = { 0x01, 0x02, 0x03, 0x04 };
353 const char *san_dns_name = "example.com";
354 const char *san_dn_name = "C=UK,O=Mbed TLS,CN=SubjectAltName test";
355 const char *san_mail_name = "mail@example.com";
356 const char *san_uri_name = "http://pki.example.com";
357 mbedtls_x509_san_list *san_list = NULL;
358
359 if (set_subjectAltNames) {
360 san_mail.node.type = MBEDTLS_X509_SAN_RFC822_NAME;
361 san_mail.node.san.unstructured_name.p = (unsigned char *) san_mail_name;
Andrzej Kurek13c43f62023-04-04 10:43:38 -0400362 san_mail.node.san.unstructured_name.len = strlen(san_mail_name);
Andrzej Kurek76c96622023-04-04 06:57:08 -0400363 san_mail.next = NULL;
364
365 san_dns.node.type = MBEDTLS_X509_SAN_DNS_NAME;
366 san_dns.node.san.unstructured_name.p = (unsigned char *) san_dns_name;
367 san_dns.node.san.unstructured_name.len = strlen(san_dns_name);
368 san_dns.next = &san_mail;
369
370 san_dn.node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
371 TEST_ASSERT(mbedtls_x509_string_to_names(&ext_san_dirname,
372 san_dn_name) == 0);
373 san_dn.node.san.directory_name = *ext_san_dirname;
374 san_dn.next = &san_dns;
375
376 san_ip.node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
377 san_ip.node.san.unstructured_name.p = (unsigned char *) san_ip_name;
378 san_ip.node.san.unstructured_name.len = sizeof(san_ip_name);
379 san_ip.next = &san_dn;
380
381 san_uri.node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
382 san_uri.node.san.unstructured_name.p = (unsigned char *) san_uri_name;
383 san_uri.node.san.unstructured_name.len = strlen(san_uri_name);
384 san_uri.next = &san_ip;
385
386 san_list = &san_uri;
387 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100390#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
391 mbedtls_mpi_init(&serial_mpi);
392#endif
Hanno Becker418a6222017-09-14 07:51:28 +0100393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 mbedtls_pk_init(&subject_key);
395 mbedtls_pk_init(&issuer_key);
396 mbedtls_pk_init(&issuer_key_alt);
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 mbedtls_x509write_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200398 MD_OR_USE_PSA_INIT();
Paul Bakker2397cf32013-09-08 15:58:15 +0200399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 TEST_ASSERT(mbedtls_pk_parse_keyfile(&subject_key, subject_key_file,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000401 subject_pwd) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 TEST_ASSERT(mbedtls_pk_parse_keyfile(&issuer_key, issuer_key_file,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000404 issuer_pwd) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 issuer_key_type = mbedtls_pk_get_type(&issuer_key);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100407
Neil Armstrong98f899c2022-03-16 17:42:42 +0100408#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100409 /* Turn the issuer PK context into an opaque one. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 if (pk_wrap == 2) {
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100411 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&issuer_key, PSA_KEY_USAGE_SIGN_HASH,
412 &key_attr), 0);
413 TEST_EQUAL(mbedtls_pk_import_into_psa(&issuer_key, &key_attr, &key_id), 0);
414 mbedtls_pk_free(&issuer_key);
415 mbedtls_pk_init(&issuer_key);
416 TEST_EQUAL(mbedtls_pk_setup_opaque(&issuer_key, key_id), 0);
Neil Armstrong98f899c2022-03-16 17:42:42 +0100417 }
418#endif /* MBEDTLS_USE_PSA_CRYPTO */
419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 if (pk_wrap == 2) {
421 TEST_ASSERT(mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_OPAQUE);
422 }
Neil Armstrong98f899c2022-03-16 17:42:42 +0100423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if (ver != -1) {
425 mbedtls_x509write_crt_set_version(&crt, ver);
426 }
Hanno Becker418a6222017-09-14 07:51:28 +0100427
Valerio Settiaf4815c2023-01-26 17:43:09 +0100428 TEST_ASSERT(mbedtls_x509write_crt_set_serial_raw(&crt, serial_arg->x,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100429 serial_arg->len) == 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 TEST_ASSERT(mbedtls_x509write_crt_set_validity(&crt, not_before,
431 not_after) == 0);
432 mbedtls_x509write_crt_set_md_alg(&crt, md_type);
433 TEST_ASSERT(mbedtls_x509write_crt_set_issuer_name(&crt, issuer_name) == 0);
434 TEST_ASSERT(mbedtls_x509write_crt_set_subject_name(&crt, subject_name) == 0);
435 mbedtls_x509write_crt_set_subject_key(&crt, &subject_key);
Hanno Becker418a6222017-09-14 07:51:28 +0100436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 mbedtls_x509write_crt_set_issuer_key(&crt, key);
Paul Bakker2397cf32013-09-08 15:58:15 +0200438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (crt.version >= MBEDTLS_X509_CRT_VERSION_3) {
Darren Krahn9c134ce2021-01-13 22:04:45 -0800440 /* For the CA case, a path length of -1 means unlimited. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 TEST_ASSERT(mbedtls_x509write_crt_set_basic_constraints(&crt, is_ca,
442 (is_ca ? -1 : 0)) == 0);
443 TEST_ASSERT(mbedtls_x509write_crt_set_subject_key_identifier(&crt) == 0);
444 if (auth_ident) {
445 TEST_ASSERT(mbedtls_x509write_crt_set_authority_key_identifier(&crt) == 0);
446 }
447 if (set_key_usage != 0) {
448 TEST_ASSERT(mbedtls_x509write_crt_set_key_usage(&crt, key_usage) == 0);
449 }
450 if (set_cert_type != 0) {
451 TEST_ASSERT(mbedtls_x509write_crt_set_ns_cert_type(&crt, cert_type) == 0);
452 }
453 if (strcmp(ext_key_usage, "NULL") != 0) {
Dave Rodgmanec9f6b42022-07-27 14:34:58 +0100454 mbedtls_asn1_sequence exts[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 memset(exts, 0, sizeof(exts));
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100456
457#define SET_OID(x, oid) \
458 do { \
459 x.len = MBEDTLS_OID_SIZE(oid); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 x.p = (unsigned char *) oid; \
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100461 x.tag = MBEDTLS_ASN1_OID; \
Dave Rodgmane2b772d2022-08-11 16:04:13 +0100462 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 while (0)
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 if (strcmp(ext_key_usage, "serverAuth") == 0) {
466 SET_OID(exts[0].buf, MBEDTLS_OID_SERVER_AUTH);
467 } else if (strcmp(ext_key_usage, "codeSigning,timeStamping") == 0) {
468 SET_OID(exts[0].buf, MBEDTLS_OID_CODE_SIGNING);
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100469 exts[0].next = &exts[1];
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 SET_OID(exts[1].buf, MBEDTLS_OID_TIME_STAMPING);
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000471 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 TEST_ASSERT(mbedtls_x509write_crt_set_ext_key_usage(&crt, exts) == 0);
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000473 }
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100474 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200475
Andrzej Kurek76c96622023-04-04 06:57:08 -0400476 if (set_subjectAltNames) {
477 TEST_ASSERT(mbedtls_x509write_crt_set_subject_alternative_name(&crt, san_list) == 0);
478 }
Ben Taylor440cb2a2025-03-05 09:40:08 +0000479 ret = mbedtls_x509write_crt_pem(&crt, buf, sizeof(buf));
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 TEST_ASSERT(ret == 0);
Paul Bakker2397cf32013-09-08 15:58:15 +0200481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 pem_len = strlen((char *) buf);
Paul Bakker2397cf32013-09-08 15:58:15 +0200483
Paul Elliott557b8d62020-11-19 09:46:56 +0000484 // check that the rest of the buffer remains clear
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
486 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000487 }
488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (issuer_key_type != MBEDTLS_PK_RSA) {
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100490 mbedtls_x509_crt crt_parse, trusted;
491 uint32_t flags;
Paul Bakker2397cf32013-09-08 15:58:15 +0200492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 mbedtls_x509_crt_init(&crt_parse);
494 mbedtls_x509_crt_init(&trusted);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted,
497 cert_verify_file) == 0);
498 TEST_ASSERT(mbedtls_x509_crt_parse(&crt_parse,
499 buf, sizeof(buf)) == 0);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 ret = mbedtls_x509_crt_verify(&crt_parse, &trusted, NULL, NULL, &flags,
502 NULL, NULL);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 mbedtls_x509_crt_free(&crt_parse);
505 mbedtls_x509_crt_free(&trusted);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 TEST_EQUAL(flags, 0);
508 TEST_EQUAL(ret, 0);
509 } else if (*cert_check_file != '\0') {
510 f = fopen(cert_check_file, "r");
511 TEST_ASSERT(f != NULL);
512 olen = fread(check_buf, 1, sizeof(check_buf), f);
513 fclose(f);
514 TEST_ASSERT(olen < sizeof(check_buf));
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 TEST_EQUAL(olen, pem_len);
517 TEST_ASSERT(olen >= pem_len - 1);
518 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100519 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200520
Ben Taylor440cb2a2025-03-05 09:40:08 +0000521 der_len = mbedtls_x509write_crt_der(&crt, buf, sizeof(buf));
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100525 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 }
Andres AGe0af9952016-09-07 11:09:44 +0100527
Werner Lewisacd01e52022-05-10 12:23:13 +0100528 // Not testing against file, check date format
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 if (*cert_check_file == '\0') {
Werner Lewisacd01e52022-05-10 12:23:13 +0100530 // UTC tag if before 2050, 2 digits less for year
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if (not_before[0] == '2' && (not_before[1] > '0' || not_before[2] > '4')) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100532 before_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 } else {
Werner Lewisacd01e52022-05-10 12:23:13 +0100534 before_tag = MBEDTLS_ASN1_UTC_TIME;
535 not_before += 2;
536 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if (not_after[0] == '2' && (not_after[1] > '0' || not_after[2] > '4')) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100538 after_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 } else {
Werner Lewisacd01e52022-05-10 12:23:13 +0100540 after_tag = MBEDTLS_ASN1_UTC_TIME;
541 not_after += 2;
542 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 end = buf + sizeof(buf);
544 for (p = end - der_len; p < end;) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100545 tag = *p++;
546 sz = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if (tag == MBEDTLS_ASN1_UTC_TIME || tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100548 // Check correct tag and time written
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 TEST_ASSERT(before_tag == tag);
550 TEST_ASSERT(memcmp(p, not_before, sz - 1) == 0);
Werner Lewisacd01e52022-05-10 12:23:13 +0100551 p += sz;
552 tag = *p++;
553 sz = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 TEST_ASSERT(after_tag == tag);
555 TEST_ASSERT(memcmp(p, not_after, sz - 1) == 0);
Werner Lewisacd01e52022-05-10 12:23:13 +0100556 break;
557 }
558 // Increment if long form ASN1 length
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if (sz & 0x80) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100560 p += sz & 0x0F;
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 }
562 if (tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100563 p += sz;
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 }
Werner Lewisacd01e52022-05-10 12:23:13 +0100565 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 TEST_ASSERT(p < end);
Werner Lewisacd01e52022-05-10 12:23:13 +0100567 }
568
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100569#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge6ed23c2022-04-22 09:44:04 +0200570 // When using PSA crypto, RNG isn't controllable, result length isn't
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100571 // deterministic over multiple runs, removing a single byte isn't enough to
572 // go into the MBEDTLS_ERR_ASN1_BUF_TOO_SMALL error case
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 if (issuer_key_type != MBEDTLS_PK_RSA) {
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100574 der_len /= 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 } else
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100576#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 der_len -= 1;
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100578
Ben Taylor440cb2a2025-03-05 09:40:08 +0000579 ret = mbedtls_x509write_crt_der(&crt, buf, (size_t) (der_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100581
Paul Bakkerbd51b262014-07-10 15:26:12 +0200582exit:
Andrzej Kurek5da1d752023-04-05 08:30:59 -0400583 mbedtls_asn1_free_named_data_list(&ext_san_dirname);
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 mbedtls_x509write_crt_free(&crt);
585 mbedtls_pk_free(&issuer_key_alt);
586 mbedtls_pk_free(&subject_key);
587 mbedtls_pk_free(&issuer_key);
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100588#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
589 mbedtls_mpi_free(&serial_mpi);
590#endif
Neil Armstrong98f899c2022-03-16 17:42:42 +0100591#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 psa_destroy_key(key_id);
Neil Armstrong98f899c2022-03-16 17:42:42 +0100593#endif
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100594 MD_OR_USE_PSA_DONE();
Paul Bakker2397cf32013-09-08 15:58:15 +0200595}
596/* END_CASE */
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200597
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100598/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_WRITE_C */
599void x509_set_serial_check()
600{
601 mbedtls_x509write_cert ctx;
602 uint8_t invalid_serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN + 1];
603
David Horstmanne04a97a2023-12-08 18:27:48 +0000604#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
605 mbedtls_mpi serial_mpi;
606 mbedtls_mpi_init(&serial_mpi);
607#endif
608
Valerio Setti569c1712023-04-19 14:53:36 +0200609 USE_PSA_INIT();
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100610 memset(invalid_serial, 0x01, sizeof(invalid_serial));
611
Valerio Settiaf4815c2023-01-26 17:43:09 +0100612 TEST_EQUAL(mbedtls_x509write_crt_set_serial_raw(&ctx, invalid_serial,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100613 sizeof(invalid_serial)),
614 MBEDTLS_ERR_X509_BAD_INPUT_DATA);
615
Valerio Settia87f8392023-01-26 18:00:50 +0100616exit:
617#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
618 mbedtls_mpi_free(&serial_mpi);
619#else
620 ;
621#endif
Valerio Setti569c1712023-04-19 14:53:36 +0200622 USE_PSA_DONE();
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100623}
624/* END_CASE */
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C */
Gilles Peskinec94500b2023-09-21 18:01:05 +0200627void mbedtls_x509_string_to_names(char *name, char *parsed_name,
628 int result, int may_fail)
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200629{
630 int ret;
631 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_asn1_named_data *names = NULL;
Gilles Peskine21e46b32023-10-17 16:35:20 +0200633 mbedtls_x509_name parsed;
634 memset(&parsed, 0, sizeof(parsed));
635 mbedtls_x509_name *parsed_cur = NULL;
636 mbedtls_x509_name *parsed_prv = NULL;
Gilles Peskinef2574202023-10-18 17:39:48 +0200637 unsigned char buf[1024] = { 0 };
638 unsigned char out[1024] = { 0 };
Gilles Peskine21e46b32023-10-17 16:35:20 +0200639 unsigned char *c = buf + sizeof(buf);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200640
Valerio Setti569c1712023-04-19 14:53:36 +0200641 USE_PSA_INIT();
642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 ret = mbedtls_x509_string_to_names(&names, name);
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200644 TEST_EQUAL(ret, result);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (ret != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200647 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 }
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 ret = mbedtls_x509_write_names(&c, buf, names);
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200651 TEST_LE_S(1, ret);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200652
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200653 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
Gilles Peskineaa01a032023-09-21 15:57:30 +0200654 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
Gilles Peskinec94500b2023-09-21 18:01:05 +0200655 ret = mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed);
656 if ((may_fail & MAY_FAIL_GET_NAME) && ret < 0) {
657 /* Validation inconsistency between mbedtls_x509_string_to_names() and
658 * mbedtls_x509_get_name(). Accept it for now. */
659 goto exit;
660 }
661 TEST_EQUAL(ret, 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 ret = mbedtls_x509_dn_gets((char *) out, sizeof(out), &parsed);
Gilles Peskinec94500b2023-09-21 18:01:05 +0200664 if ((may_fail & MAY_FAIL_DN_GETS) && ret < 0) {
665 /* Validation inconsistency between mbedtls_x509_string_to_names() and
666 * mbedtls_x509_dn_gets(). Accept it for now. */
667 goto exit;
668 }
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200669 TEST_LE_S(1, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 TEST_ASSERT(strcmp((char *) out, parsed_name) == 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200671
672exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 mbedtls_asn1_free_named_data_list(&names);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200674
675 parsed_cur = parsed.next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 while (parsed_cur != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200677 parsed_prv = parsed_cur;
678 parsed_cur = parsed_cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 mbedtls_free(parsed_prv);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200680 }
Valerio Setti569c1712023-04-19 14:53:36 +0200681 USE_PSA_DONE();
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200682}
683/* END_CASE */
Jonathan Winzig2bd2b782024-01-09 15:19:42 +0100684
Jonathan Winzig315c3ca2024-01-09 18:31:11 +0100685/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_WRITE_C */
Jonathan Winzig6c9779f2024-01-09 17:47:10 +0100686void x509_set_extension_length_check()
Jonathan Winzig2bd2b782024-01-09 15:19:42 +0100687{
688 int ret = 0;
689
690 mbedtls_x509write_csr ctx;
691 mbedtls_x509write_csr_init(&ctx);
692
693 unsigned char buf[EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH] = { 0 };
694 unsigned char *p = buf + sizeof(buf);
695
696 ret = mbedtls_x509_set_extension(&(ctx.MBEDTLS_PRIVATE(extensions)),
697 MBEDTLS_OID_EXTENDED_KEY_USAGE,
698 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
699 0,
700 p,
Jonathan Winzig6c9779f2024-01-09 17:47:10 +0100701 SIZE_MAX);
702 TEST_ASSERT(MBEDTLS_ERR_X509_BAD_INPUT_DATA == ret);
Jonathan Winzig2bd2b782024-01-09 15:19:42 +0100703}
704/* END_CASE */
Sam Berry2bb3f4d2024-07-19 15:14:27 +0100705
706/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
707void oid_from_numeric_string(char *oid_str, int error_ret,
708 data_t *exp_oid_buf)
709{
710 mbedtls_asn1_buf oid = { 0, 0, NULL };
711 mbedtls_asn1_buf exp_oid = { 0, 0, NULL };
712 int ret;
713
714 exp_oid.tag = MBEDTLS_ASN1_OID;
715 exp_oid.p = exp_oid_buf->x;
716 exp_oid.len = exp_oid_buf->len;
717
718 ret = mbedtls_oid_from_numeric_string(&oid, oid_str, strlen(oid_str));
719
720 if (error_ret == 0) {
721 TEST_EQUAL(oid.len, exp_oid.len);
722 TEST_ASSERT(memcmp(oid.p, exp_oid.p, oid.len) == 0);
723 mbedtls_free(oid.p);
724 oid.p = NULL;
725 oid.len = 0;
726 } else {
727 TEST_EQUAL(ret, error_ret);
728 }
729}
730/* END_CASE */