blob: f43e01ea9ed4f9a78d0e57b336ddab8bdfe2ea53 [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 Peskinecd4c0d72025-05-07 23:45:12 +02007#include "mbedtls/oid.h"
Gilles Peskine86a47f82025-05-07 20:20:12 +02008#include "x509_oid.h"
Hanno Becker418a6222017-09-14 07:51:28 +01009#include "mbedtls/rsa.h"
Sam Berry2bb3f4d2024-07-19 15:14:27 +010010#include "mbedtls/asn1.h"
Valerio Setti48e8fc72022-10-19 15:14:29 +020011#include "mbedtls/asn1write.h"
Valerio Setti178b5bd2023-02-13 10:04:28 +010012#include "mbedtls/pk.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010013#include "mbedtls/psa_util.h"
Manuel Pégourié-Gonnardfeb03962020-08-20 09:59:33 +020014
Gilles Peskinef4e672e2020-01-31 14:22:10 +010015#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
16 defined(MBEDTLS_PEM_WRITE_C) && defined(MBEDTLS_X509_CSR_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010017static int x509_crt_verifycsr(const unsigned char *buf, size_t buflen)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050018{
Przemek Stekiel54a54462022-08-01 13:59:12 +020019 unsigned char hash[PSA_HASH_MAX_SIZE];
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050020 mbedtls_x509_csr csr;
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010021 int ret = 0;
22
Gilles Peskine449bd832023-01-11 14:50:10 +010023 mbedtls_x509_csr_init(&csr);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050024
Gilles Peskine449bd832023-01-11 14:50:10 +010025 if (mbedtls_x509_csr_parse(&csr, buf, buflen) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010026 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
27 goto cleanup;
28 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050029
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020030 psa_algorithm_t psa_alg = mbedtls_md_psa_alg_from_type(csr.sig_md);
Przemek Stekiel54a54462022-08-01 13:59:12 +020031 size_t hash_size = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010032 psa_status_t status = psa_hash_compute(psa_alg, csr.cri.p, csr.cri.len,
33 hash, PSA_HASH_MAX_SIZE, &hash_size);
Przemek Stekiel54a54462022-08-01 13:59:12 +020034
Gilles Peskine449bd832023-01-11 14:50:10 +010035 if (status != PSA_SUCCESS) {
Andrzej Kurek4b114072018-11-19 18:04:01 -050036 /* Note: this can't happen except after an internal error */
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010037 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
38 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050039 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050040
Valerio Setti7f6f4e62025-04-23 11:29:51 +020041 if (mbedtls_pk_verify_ext(csr.sig_pk, NULL, &csr.pk,
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +020042 csr.sig_md, hash, mbedtls_md_get_size_from_type(csr.sig_md),
Gilles Peskine449bd832023-01-11 14:50:10 +010043 csr.sig.p, csr.sig.len) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010044 ret = MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
45 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050046 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050047
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010048cleanup:
49
Gilles Peskine449bd832023-01-11 14:50:10 +010050 mbedtls_x509_csr_free(&csr);
51 return ret;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050052}
Gilles Peskinef4e672e2020-01-31 14:22:10 +010053#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_PEM_WRITE_C && MBEDTLS_X509_CSR_WRITE_C */
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050054
Valerio Setti48e8fc72022-10-19 15:14:29 +020055#if defined(MBEDTLS_X509_CSR_WRITE_C)
56
57/*
58 * The size of this temporary buffer is given by the sequence of functions
59 * called hereinafter:
60 * - mbedtls_asn1_write_oid()
61 * - 8 bytes for MBEDTLS_OID_EXTENDED_KEY_USAGE raw value
62 * - 1 byte for MBEDTLS_OID_EXTENDED_KEY_USAGE length
63 * - 1 byte for MBEDTLS_ASN1_OID tag
64 * - mbedtls_asn1_write_len()
65 * - 1 byte since we're dealing with sizes which are less than 0x80
66 * - mbedtls_asn1_write_tag()
67 * - 1 byte
68 *
69 * This length is fine as long as this function is called using the
70 * MBEDTLS_OID_SERVER_AUTH OID. If this is changed in the future, then this
71 * buffer's length should be adjusted accordingly.
72 * Unfortunately there's no predefined max size for OIDs which can be used
73 * to set an overall upper boundary which is always guaranteed.
74 */
75#define EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH 12
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077static int csr_set_extended_key_usage(mbedtls_x509write_csr *ctx,
78 const char *oid, size_t oid_len)
Valerio Setti48e8fc72022-10-19 15:14:29 +020079{
80 unsigned char buf[EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH] = { 0 };
Gilles Peskine449bd832023-01-11 14:50:10 +010081 unsigned char *p = buf + sizeof(buf);
Valerio Setti48e8fc72022-10-19 15:14:29 +020082 int ret;
83 size_t len = 0;
84
85 /*
86 * Following functions fail anyway if the temporary buffer is not large,
87 * but we set an extra check here to emphasize a possible source of errors
88 */
Gilles Peskine449bd832023-01-11 14:50:10 +010089 if (oid_len > EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH) {
Valerio Setti48e8fc72022-10-19 15:14:29 +020090 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
91 }
92
Gilles Peskine449bd832023-01-11 14:50:10 +010093 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(&p, buf, oid, oid_len));
94 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, ret));
95 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, buf,
96 MBEDTLS_ASN1_CONSTRUCTED |
97 MBEDTLS_ASN1_SEQUENCE));
Valerio Setti48e8fc72022-10-19 15:14:29 +020098
Gilles Peskine449bd832023-01-11 14:50:10 +010099 ret = mbedtls_x509write_csr_set_extension(ctx,
100 MBEDTLS_OID_EXTENDED_KEY_USAGE,
101 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
102 0,
103 p,
104 len);
Valerio Setti48e8fc72022-10-19 15:14:29 +0200105
106 return ret;
107}
108#endif /* MBEDTLS_X509_CSR_WRITE_C */
Gilles Peskinec94500b2023-09-21 18:01:05 +0200109
110/* Due to inconsistencies in the input size limits applied by different
111 * library functions, some write-parse tests may fail. */
112#define MAY_FAIL_GET_NAME 0x0001
113#define MAY_FAIL_DN_GETS 0x0002
114
Paul Bakker33b43f12013-08-20 11:48:36 +0200115/* END_HEADER */
Paul Bakker6d620502012-02-16 14:09:13 +0000116
Paul Bakker33b43f12013-08-20 11:48:36 +0200117/* BEGIN_DEPENDENCIES
Valerio Setti3580f442023-07-27 10:19:53 +0200118 * depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_PARSE_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200119 * END_DEPENDENCIES
120 */
Paul Bakker6d620502012-02-16 14:09:13 +0000121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100123void x509_csr_check(char *key_file, char *cert_req_check_file, int md_type,
124 int key_usage, int set_key_usage, int cert_type,
125 int set_cert_type, int set_extension)
Paul Bakker6d620502012-02-16 14:09:13 +0000126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_pk_context key;
128 mbedtls_x509write_csr req;
Andres AGe0af9952016-09-07 11:09:44 +0100129 unsigned char buf[4096];
Paul Bakker6d620502012-02-16 14:09:13 +0000130 int ret;
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100131#if !defined(MBEDTLS_USE_PSA_CRYPTO)
132 unsigned char check_buf[4000];
Paul Bakker6d620502012-02-16 14:09:13 +0000133 FILE *f;
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100134 size_t olen = 0;
135#endif /* !MBEDTLS_USE_PSA_CRYPTO */
136 size_t pem_len = 0, buf_index;
137 int der_len = -1;
Paul Bakker3a8cb6f2013-12-30 20:41:54 +0100138 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200139 mbedtls_test_rnd_pseudo_info rnd_info;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100140 mbedtls_x509_san_list san_ip;
141 mbedtls_x509_san_list san_dns;
142 mbedtls_x509_san_list san_uri;
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400143 mbedtls_x509_san_list san_mail;
144 mbedtls_x509_san_list san_dn;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100145 mbedtls_x509_san_list *san_list = NULL;
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400146 mbedtls_asn1_named_data *ext_san_dirname = NULL;
147
148 const char san_ip_name[] = { 0x7f, 0x00, 0x00, 0x01 }; // 127.0.0.1
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100149 const char *san_dns_name = "example.com";
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400150 const char *san_dn_name = "C=UK,O=Mbed TLS,CN=Mbed TLS directoryName SAN";
151 const char *san_mail_name = "mail@example.com";
152 const char *san_uri_name = "http://pki.example.com";
153
154 san_mail.node.type = MBEDTLS_X509_SAN_RFC822_NAME;
155 san_mail.node.san.unstructured_name.p = (unsigned char *) san_mail_name;
156 san_mail.node.san.unstructured_name.len = strlen(san_mail_name);
157 san_mail.next = NULL;
158
159 san_dns.node.type = MBEDTLS_X509_SAN_DNS_NAME;
160 san_dns.node.san.unstructured_name.p = (unsigned char *) san_dns_name;
161 san_dns.node.san.unstructured_name.len = strlen(san_dns_name);
162 san_dns.next = &san_mail;
163
164 san_dn.node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
165 TEST_ASSERT(mbedtls_x509_string_to_names(&ext_san_dirname,
166 san_dn_name) == 0);
167 san_dn.node.san.directory_name = *ext_san_dirname;
168 san_dn.next = &san_dns;
169
170 san_ip.node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
171 san_ip.node.san.unstructured_name.p = (unsigned char *) san_ip_name;
172 san_ip.node.san.unstructured_name.len = sizeof(san_ip_name);
173 san_ip.next = &san_dn;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100174
175 san_uri.node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100176 san_uri.node.san.unstructured_name.p = (unsigned char *) san_uri_name;
177 san_uri.node.san.unstructured_name.len = strlen(san_uri_name);
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400178 san_uri.next = &san_ip;
179
180 san_list = &san_uri;
Paul Bakker6d620502012-02-16 14:09:13 +0000181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 mbedtls_x509write_csr_init(&req);
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 mbedtls_pk_init(&key);
valerio32f2ac92023-04-20 11:59:52 +0200186 MD_OR_USE_PSA_INIT();
187
Ben Taylor440cb2a2025-03-05 09:40:08 +0000188 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL) == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 mbedtls_x509write_csr_set_md_alg(&req, md_type);
191 mbedtls_x509write_csr_set_key(&req, &key);
192 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
193 if (set_key_usage != 0) {
194 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
195 }
196 if (set_cert_type != 0) {
197 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
198 }
199 if (set_extension != 0) {
200 TEST_ASSERT(csr_set_extended_key_usage(&req, MBEDTLS_OID_SERVER_AUTH,
201 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) == 0);
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100202
203 TEST_ASSERT(mbedtls_x509write_csr_set_subject_alternative_name(&req, san_list) == 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 }
Paul Bakker8eabfc12013-08-25 10:18:25 +0200205
Ben Taylor440cb2a2025-03-05 09:40:08 +0000206 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf));
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 TEST_ASSERT(ret == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 pem_len = strlen((char *) buf);
Paul Bakker6d620502012-02-16 14:09:13 +0000210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
212 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000213 }
214
Neil Armstrong5b320382022-02-21 17:22:10 +0100215#if defined(MBEDTLS_USE_PSA_CRYPTO)
216 // When using PSA crypto, RNG isn't controllable, so cert_req_check_file can't be used
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 (void) cert_req_check_file;
Neil Armstrong5b320382022-02-21 17:22:10 +0100218 buf[pem_len] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0);
Neil Armstrong5b320382022-02-21 17:22:10 +0100220#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 f = fopen(cert_req_check_file, "r");
222 TEST_ASSERT(f != NULL);
223 olen = fread(check_buf, 1, sizeof(check_buf), f);
224 fclose(f);
Paul Bakker6d620502012-02-16 14:09:13 +0000225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 TEST_ASSERT(olen >= pem_len - 1);
227 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100228#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100229
Ben Taylor440cb2a2025-03-05 09:40:08 +0000230 der_len = mbedtls_x509write_csr_der(&req, buf, sizeof(buf));
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100234 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 }
Andres AGe0af9952016-09-07 11:09:44 +0100236
Neil Armstrong5b320382022-02-21 17:22:10 +0100237#if defined(MBEDTLS_USE_PSA_CRYPTO)
238 // When using PSA crypto, RNG isn't controllable, result length isn't
239 // deterministic over multiple runs, removing a single byte isn't enough to
240 // go into the MBEDTLS_ERR_ASN1_BUF_TOO_SMALL error case
241 der_len /= 2;
242#else
243 der_len -= 1;
244#endif
Ben Taylor440cb2a2025-03-05 09:40:08 +0000245 ret = mbedtls_x509write_csr_der(&req, buf, (size_t) (der_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100247
Paul Bakkerbd51b262014-07-10 15:26:12 +0200248exit:
Andrzej Kurekbdb41dd2023-07-10 08:09:50 -0400249 mbedtls_asn1_free_named_data_list(&ext_san_dirname);
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 mbedtls_x509write_csr_free(&req);
251 mbedtls_pk_free(&key);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100252 MD_OR_USE_PSA_DONE();
Paul Bakker6d620502012-02-16 14:09:13 +0000253}
Paul Bakker33b43f12013-08-20 11:48:36 +0200254/* END_CASE */
Paul Bakker2397cf32013-09-08 15:58:15 +0200255
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500256/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C:MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100257void x509_csr_check_opaque(char *key_file, int md_type, int key_usage,
258 int cert_type)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500259{
260 mbedtls_pk_context key;
Paul Elliott9a209b82024-10-25 12:41:28 +0100261 mbedtls_pk_init(&key);
262
Ronald Cron5425a212020-08-04 14:58:35 +0200263 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100264 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliott9a209b82024-10-25 12:41:28 +0100265
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500266 mbedtls_x509write_csr req;
Paul Elliott9a209b82024-10-25 12:41:28 +0100267 mbedtls_x509write_csr_init(&req);
268
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500269 unsigned char buf[4096];
270 int ret;
271 size_t pem_len = 0;
272 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200273 mbedtls_test_rnd_pseudo_info rnd_info;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500274
Valerio Setti569c1712023-04-19 14:53:36 +0200275 MD_OR_USE_PSA_INIT();
276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500278
Ben Taylor440cb2a2025-03-05 09:40:08 +0000279 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL) == 0);
Neil Armstrong95974972022-04-22 13:57:44 +0200280
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100281 /* Turn the PK context into an opaque one. */
282 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&key, PSA_KEY_USAGE_SIGN_HASH, &key_attr), 0);
283 TEST_EQUAL(mbedtls_pk_import_into_psa(&key, &key_attr, &key_id), 0);
284 mbedtls_pk_free(&key);
285 mbedtls_pk_init(&key);
286 TEST_EQUAL(mbedtls_pk_setup_opaque(&key, key_id), 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 mbedtls_x509write_csr_set_md_alg(&req, md_type);
289 mbedtls_x509write_csr_set_key(&req, &key);
290 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
291 if (key_usage != 0) {
292 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
293 }
294 if (cert_type != 0) {
295 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
296 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500297
Ben Taylor440cb2a2025-03-05 09:40:08 +0000298 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf) - 1);
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 TEST_ASSERT(ret == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 pem_len = strlen((char *) buf);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500303 buf[pem_len] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500305
Manuel Pégourié-Gonnardfeb03962020-08-20 09:59:33 +0200306
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500307exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 mbedtls_x509write_csr_free(&req);
309 mbedtls_pk_free(&key);
310 psa_destroy_key(key_id);
Valerio Setti569c1712023-04-19 14:53:36 +0200311 MD_OR_USE_PSA_DONE();
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500312}
313/* END_CASE */
314
Elena Uziunaite9fc5be02024-09-04 18:12:59 +0100315/* 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 +0100316void x509_crt_check(char *subject_key_file, char *subject_pwd,
317 char *subject_name, char *issuer_key_file,
318 char *issuer_pwd, char *issuer_name,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100319 data_t *serial_arg, char *not_before, char *not_after,
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 int md_type, int key_usage, int set_key_usage,
321 char *ext_key_usage,
322 int cert_type, int set_cert_type, int auth_ident,
323 int ver, char *cert_check_file, int pk_wrap, int is_ca,
Andrzej Kurek76c96622023-04-04 06:57:08 -0400324 char *cert_verify_file, int set_subjectAltNames)
Paul Bakker2397cf32013-09-08 15:58:15 +0200325{
Hanno Becker418a6222017-09-14 07:51:28 +0100326 mbedtls_pk_context subject_key, issuer_key, issuer_key_alt;
327 mbedtls_pk_context *key = &issuer_key;
328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 mbedtls_x509write_cert crt;
Andres AGe0af9952016-09-07 11:09:44 +0100330 unsigned char buf[4096];
Paul Bakker2397cf32013-09-08 15:58:15 +0200331 unsigned char check_buf[5000];
Werner Lewisacd01e52022-05-10 12:23:13 +0100332 unsigned char *p, *end;
333 unsigned char tag, sz;
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100334#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
335 mbedtls_mpi serial_mpi;
336#endif
Werner Lewisacd01e52022-05-10 12:23:13 +0100337 int ret, before_tag, after_tag;
Paul Elliott557b8d62020-11-19 09:46:56 +0000338 size_t olen = 0, pem_len = 0, buf_index = 0;
Andres AGe0af9952016-09-07 11:09:44 +0100339 int der_len = -1;
Paul Bakker2397cf32013-09-08 15:58:15 +0200340 FILE *f;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200341 mbedtls_test_rnd_pseudo_info rnd_info;
Neil Armstrong98f899c2022-03-16 17:42:42 +0100342#if defined(MBEDTLS_USE_PSA_CRYPTO)
343 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100344 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong98f899c2022-03-16 17:42:42 +0100345#endif
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100346 mbedtls_pk_type_t issuer_key_type;
Andrzej Kurek76c96622023-04-04 06:57:08 -0400347 mbedtls_x509_san_list san_ip;
348 mbedtls_x509_san_list san_dns;
349 mbedtls_x509_san_list san_uri;
350 mbedtls_x509_san_list san_mail;
351 mbedtls_x509_san_list san_dn;
352 mbedtls_asn1_named_data *ext_san_dirname = NULL;
353 const char san_ip_name[] = { 0x01, 0x02, 0x03, 0x04 };
354 const char *san_dns_name = "example.com";
355 const char *san_dn_name = "C=UK,O=Mbed TLS,CN=SubjectAltName test";
356 const char *san_mail_name = "mail@example.com";
357 const char *san_uri_name = "http://pki.example.com";
358 mbedtls_x509_san_list *san_list = NULL;
359
360 if (set_subjectAltNames) {
361 san_mail.node.type = MBEDTLS_X509_SAN_RFC822_NAME;
362 san_mail.node.san.unstructured_name.p = (unsigned char *) san_mail_name;
Andrzej Kurek13c43f62023-04-04 10:43:38 -0400363 san_mail.node.san.unstructured_name.len = strlen(san_mail_name);
Andrzej Kurek76c96622023-04-04 06:57:08 -0400364 san_mail.next = NULL;
365
366 san_dns.node.type = MBEDTLS_X509_SAN_DNS_NAME;
367 san_dns.node.san.unstructured_name.p = (unsigned char *) san_dns_name;
368 san_dns.node.san.unstructured_name.len = strlen(san_dns_name);
369 san_dns.next = &san_mail;
370
371 san_dn.node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
372 TEST_ASSERT(mbedtls_x509_string_to_names(&ext_san_dirname,
373 san_dn_name) == 0);
374 san_dn.node.san.directory_name = *ext_san_dirname;
375 san_dn.next = &san_dns;
376
377 san_ip.node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
378 san_ip.node.san.unstructured_name.p = (unsigned char *) san_ip_name;
379 san_ip.node.san.unstructured_name.len = sizeof(san_ip_name);
380 san_ip.next = &san_dn;
381
382 san_uri.node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
383 san_uri.node.san.unstructured_name.p = (unsigned char *) san_uri_name;
384 san_uri.node.san.unstructured_name.len = strlen(san_uri_name);
385 san_uri.next = &san_ip;
386
387 san_list = &san_uri;
388 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100391#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
392 mbedtls_mpi_init(&serial_mpi);
393#endif
Hanno Becker418a6222017-09-14 07:51:28 +0100394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 mbedtls_pk_init(&subject_key);
396 mbedtls_pk_init(&issuer_key);
397 mbedtls_pk_init(&issuer_key_alt);
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 mbedtls_x509write_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200399 MD_OR_USE_PSA_INIT();
Paul Bakker2397cf32013-09-08 15:58:15 +0200400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 TEST_ASSERT(mbedtls_pk_parse_keyfile(&subject_key, subject_key_file,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000402 subject_pwd) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 TEST_ASSERT(mbedtls_pk_parse_keyfile(&issuer_key, issuer_key_file,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000405 issuer_pwd) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 issuer_key_type = mbedtls_pk_get_type(&issuer_key);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100408
Neil Armstrong98f899c2022-03-16 17:42:42 +0100409#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100410 /* Turn the issuer PK context into an opaque one. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 if (pk_wrap == 2) {
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100412 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&issuer_key, PSA_KEY_USAGE_SIGN_HASH,
413 &key_attr), 0);
414 TEST_EQUAL(mbedtls_pk_import_into_psa(&issuer_key, &key_attr, &key_id), 0);
415 mbedtls_pk_free(&issuer_key);
416 mbedtls_pk_init(&issuer_key);
417 TEST_EQUAL(mbedtls_pk_setup_opaque(&issuer_key, key_id), 0);
Neil Armstrong98f899c2022-03-16 17:42:42 +0100418 }
419#endif /* MBEDTLS_USE_PSA_CRYPTO */
420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 if (pk_wrap == 2) {
422 TEST_ASSERT(mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_OPAQUE);
423 }
Neil Armstrong98f899c2022-03-16 17:42:42 +0100424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (ver != -1) {
426 mbedtls_x509write_crt_set_version(&crt, ver);
427 }
Hanno Becker418a6222017-09-14 07:51:28 +0100428
Valerio Settiaf4815c2023-01-26 17:43:09 +0100429 TEST_ASSERT(mbedtls_x509write_crt_set_serial_raw(&crt, serial_arg->x,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100430 serial_arg->len) == 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 TEST_ASSERT(mbedtls_x509write_crt_set_validity(&crt, not_before,
432 not_after) == 0);
433 mbedtls_x509write_crt_set_md_alg(&crt, md_type);
434 TEST_ASSERT(mbedtls_x509write_crt_set_issuer_name(&crt, issuer_name) == 0);
435 TEST_ASSERT(mbedtls_x509write_crt_set_subject_name(&crt, subject_name) == 0);
436 mbedtls_x509write_crt_set_subject_key(&crt, &subject_key);
Hanno Becker418a6222017-09-14 07:51:28 +0100437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 mbedtls_x509write_crt_set_issuer_key(&crt, key);
Paul Bakker2397cf32013-09-08 15:58:15 +0200439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if (crt.version >= MBEDTLS_X509_CRT_VERSION_3) {
Darren Krahn9c134ce2021-01-13 22:04:45 -0800441 /* For the CA case, a path length of -1 means unlimited. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 TEST_ASSERT(mbedtls_x509write_crt_set_basic_constraints(&crt, is_ca,
443 (is_ca ? -1 : 0)) == 0);
444 TEST_ASSERT(mbedtls_x509write_crt_set_subject_key_identifier(&crt) == 0);
445 if (auth_ident) {
446 TEST_ASSERT(mbedtls_x509write_crt_set_authority_key_identifier(&crt) == 0);
447 }
448 if (set_key_usage != 0) {
449 TEST_ASSERT(mbedtls_x509write_crt_set_key_usage(&crt, key_usage) == 0);
450 }
451 if (set_cert_type != 0) {
452 TEST_ASSERT(mbedtls_x509write_crt_set_ns_cert_type(&crt, cert_type) == 0);
453 }
454 if (strcmp(ext_key_usage, "NULL") != 0) {
Dave Rodgmanec9f6b42022-07-27 14:34:58 +0100455 mbedtls_asn1_sequence exts[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 memset(exts, 0, sizeof(exts));
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100457
458#define SET_OID(x, oid) \
459 do { \
460 x.len = MBEDTLS_OID_SIZE(oid); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 x.p = (unsigned char *) oid; \
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100462 x.tag = MBEDTLS_ASN1_OID; \
Dave Rodgmane2b772d2022-08-11 16:04:13 +0100463 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 while (0)
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 if (strcmp(ext_key_usage, "serverAuth") == 0) {
467 SET_OID(exts[0].buf, MBEDTLS_OID_SERVER_AUTH);
468 } else if (strcmp(ext_key_usage, "codeSigning,timeStamping") == 0) {
469 SET_OID(exts[0].buf, MBEDTLS_OID_CODE_SIGNING);
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100470 exts[0].next = &exts[1];
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 SET_OID(exts[1].buf, MBEDTLS_OID_TIME_STAMPING);
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000472 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 TEST_ASSERT(mbedtls_x509write_crt_set_ext_key_usage(&crt, exts) == 0);
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000474 }
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100475 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200476
Andrzej Kurek76c96622023-04-04 06:57:08 -0400477 if (set_subjectAltNames) {
478 TEST_ASSERT(mbedtls_x509write_crt_set_subject_alternative_name(&crt, san_list) == 0);
479 }
Ben Taylor440cb2a2025-03-05 09:40:08 +0000480 ret = mbedtls_x509write_crt_pem(&crt, buf, sizeof(buf));
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 TEST_ASSERT(ret == 0);
Paul Bakker2397cf32013-09-08 15:58:15 +0200482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 pem_len = strlen((char *) buf);
Paul Bakker2397cf32013-09-08 15:58:15 +0200484
Paul Elliott557b8d62020-11-19 09:46:56 +0000485 // check that the rest of the buffer remains clear
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
487 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000488 }
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if (issuer_key_type != MBEDTLS_PK_RSA) {
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100491 mbedtls_x509_crt crt_parse, trusted;
492 uint32_t flags;
Paul Bakker2397cf32013-09-08 15:58:15 +0200493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 mbedtls_x509_crt_init(&crt_parse);
495 mbedtls_x509_crt_init(&trusted);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted,
498 cert_verify_file) == 0);
499 TEST_ASSERT(mbedtls_x509_crt_parse(&crt_parse,
500 buf, sizeof(buf)) == 0);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 ret = mbedtls_x509_crt_verify(&crt_parse, &trusted, NULL, NULL, &flags,
503 NULL, NULL);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 mbedtls_x509_crt_free(&crt_parse);
506 mbedtls_x509_crt_free(&trusted);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 TEST_EQUAL(flags, 0);
509 TEST_EQUAL(ret, 0);
510 } else if (*cert_check_file != '\0') {
511 f = fopen(cert_check_file, "r");
512 TEST_ASSERT(f != NULL);
513 olen = fread(check_buf, 1, sizeof(check_buf), f);
514 fclose(f);
515 TEST_ASSERT(olen < sizeof(check_buf));
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 TEST_EQUAL(olen, pem_len);
518 TEST_ASSERT(olen >= pem_len - 1);
519 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100520 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200521
Ben Taylor440cb2a2025-03-05 09:40:08 +0000522 der_len = mbedtls_x509write_crt_der(&crt, buf, sizeof(buf));
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100526 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 }
Andres AGe0af9952016-09-07 11:09:44 +0100528
Werner Lewisacd01e52022-05-10 12:23:13 +0100529 // Not testing against file, check date format
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 if (*cert_check_file == '\0') {
Werner Lewisacd01e52022-05-10 12:23:13 +0100531 // UTC tag if before 2050, 2 digits less for year
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if (not_before[0] == '2' && (not_before[1] > '0' || not_before[2] > '4')) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100533 before_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 } else {
Werner Lewisacd01e52022-05-10 12:23:13 +0100535 before_tag = MBEDTLS_ASN1_UTC_TIME;
536 not_before += 2;
537 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 if (not_after[0] == '2' && (not_after[1] > '0' || not_after[2] > '4')) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100539 after_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 } else {
Werner Lewisacd01e52022-05-10 12:23:13 +0100541 after_tag = MBEDTLS_ASN1_UTC_TIME;
542 not_after += 2;
543 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 end = buf + sizeof(buf);
545 for (p = end - der_len; p < end;) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100546 tag = *p++;
547 sz = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 if (tag == MBEDTLS_ASN1_UTC_TIME || tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100549 // Check correct tag and time written
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 TEST_ASSERT(before_tag == tag);
551 TEST_ASSERT(memcmp(p, not_before, sz - 1) == 0);
Werner Lewisacd01e52022-05-10 12:23:13 +0100552 p += sz;
553 tag = *p++;
554 sz = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 TEST_ASSERT(after_tag == tag);
556 TEST_ASSERT(memcmp(p, not_after, sz - 1) == 0);
Werner Lewisacd01e52022-05-10 12:23:13 +0100557 break;
558 }
559 // Increment if long form ASN1 length
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (sz & 0x80) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100561 p += sz & 0x0F;
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 }
563 if (tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100564 p += sz;
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 }
Werner Lewisacd01e52022-05-10 12:23:13 +0100566 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 TEST_ASSERT(p < end);
Werner Lewisacd01e52022-05-10 12:23:13 +0100568 }
569
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100570#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge6ed23c2022-04-22 09:44:04 +0200571 // When using PSA crypto, RNG isn't controllable, result length isn't
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100572 // deterministic over multiple runs, removing a single byte isn't enough to
573 // go into the MBEDTLS_ERR_ASN1_BUF_TOO_SMALL error case
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if (issuer_key_type != MBEDTLS_PK_RSA) {
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100575 der_len /= 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 } else
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100577#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 der_len -= 1;
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100579
Ben Taylor440cb2a2025-03-05 09:40:08 +0000580 ret = mbedtls_x509write_crt_der(&crt, buf, (size_t) (der_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100582
Paul Bakkerbd51b262014-07-10 15:26:12 +0200583exit:
Andrzej Kurek5da1d752023-04-05 08:30:59 -0400584 mbedtls_asn1_free_named_data_list(&ext_san_dirname);
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 mbedtls_x509write_crt_free(&crt);
586 mbedtls_pk_free(&issuer_key_alt);
587 mbedtls_pk_free(&subject_key);
588 mbedtls_pk_free(&issuer_key);
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100589#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
590 mbedtls_mpi_free(&serial_mpi);
591#endif
Neil Armstrong98f899c2022-03-16 17:42:42 +0100592#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 psa_destroy_key(key_id);
Neil Armstrong98f899c2022-03-16 17:42:42 +0100594#endif
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100595 MD_OR_USE_PSA_DONE();
Paul Bakker2397cf32013-09-08 15:58:15 +0200596}
597/* END_CASE */
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200598
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100599/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_WRITE_C */
600void x509_set_serial_check()
601{
602 mbedtls_x509write_cert ctx;
603 uint8_t invalid_serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN + 1];
604
David Horstmanne04a97a2023-12-08 18:27:48 +0000605#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
606 mbedtls_mpi serial_mpi;
607 mbedtls_mpi_init(&serial_mpi);
608#endif
609
Valerio Setti569c1712023-04-19 14:53:36 +0200610 USE_PSA_INIT();
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100611 memset(invalid_serial, 0x01, sizeof(invalid_serial));
612
Valerio Settiaf4815c2023-01-26 17:43:09 +0100613 TEST_EQUAL(mbedtls_x509write_crt_set_serial_raw(&ctx, invalid_serial,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100614 sizeof(invalid_serial)),
615 MBEDTLS_ERR_X509_BAD_INPUT_DATA);
616
Valerio Settia87f8392023-01-26 18:00:50 +0100617exit:
618#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
619 mbedtls_mpi_free(&serial_mpi);
620#else
621 ;
622#endif
Valerio Setti569c1712023-04-19 14:53:36 +0200623 USE_PSA_DONE();
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100624}
625/* END_CASE */
626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C */
Gilles Peskinec94500b2023-09-21 18:01:05 +0200628void mbedtls_x509_string_to_names(char *name, char *parsed_name,
629 int result, int may_fail)
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200630{
631 int ret;
632 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_asn1_named_data *names = NULL;
Gilles Peskine21e46b32023-10-17 16:35:20 +0200634 mbedtls_x509_name parsed;
635 memset(&parsed, 0, sizeof(parsed));
636 mbedtls_x509_name *parsed_cur = NULL;
637 mbedtls_x509_name *parsed_prv = NULL;
Gilles Peskinef2574202023-10-18 17:39:48 +0200638 unsigned char buf[1024] = { 0 };
639 unsigned char out[1024] = { 0 };
Gilles Peskine21e46b32023-10-17 16:35:20 +0200640 unsigned char *c = buf + sizeof(buf);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200641
Valerio Setti569c1712023-04-19 14:53:36 +0200642 USE_PSA_INIT();
643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 ret = mbedtls_x509_string_to_names(&names, name);
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200645 TEST_EQUAL(ret, result);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (ret != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200648 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 }
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 ret = mbedtls_x509_write_names(&c, buf, names);
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200652 TEST_LE_S(1, ret);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200653
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200654 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
Gilles Peskineaa01a032023-09-21 15:57:30 +0200655 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
Gilles Peskinec94500b2023-09-21 18:01:05 +0200656 ret = mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed);
657 if ((may_fail & MAY_FAIL_GET_NAME) && ret < 0) {
658 /* Validation inconsistency between mbedtls_x509_string_to_names() and
659 * mbedtls_x509_get_name(). Accept it for now. */
660 goto exit;
661 }
662 TEST_EQUAL(ret, 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200663
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 ret = mbedtls_x509_dn_gets((char *) out, sizeof(out), &parsed);
Gilles Peskinec94500b2023-09-21 18:01:05 +0200665 if ((may_fail & MAY_FAIL_DN_GETS) && ret < 0) {
666 /* Validation inconsistency between mbedtls_x509_string_to_names() and
667 * mbedtls_x509_dn_gets(). Accept it for now. */
668 goto exit;
669 }
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200670 TEST_LE_S(1, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 TEST_ASSERT(strcmp((char *) out, parsed_name) == 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200672
673exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 mbedtls_asn1_free_named_data_list(&names);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200675
676 parsed_cur = parsed.next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 while (parsed_cur != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200678 parsed_prv = parsed_cur;
679 parsed_cur = parsed_cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 mbedtls_free(parsed_prv);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200681 }
Valerio Setti569c1712023-04-19 14:53:36 +0200682 USE_PSA_DONE();
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200683}
684/* END_CASE */
Jonathan Winzig2bd2b782024-01-09 15:19:42 +0100685
Jonathan Winzig315c3ca2024-01-09 18:31:11 +0100686/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_WRITE_C */
Jonathan Winzig6c9779f2024-01-09 17:47:10 +0100687void x509_set_extension_length_check()
Jonathan Winzig2bd2b782024-01-09 15:19:42 +0100688{
689 int ret = 0;
690
691 mbedtls_x509write_csr ctx;
692 mbedtls_x509write_csr_init(&ctx);
693
694 unsigned char buf[EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH] = { 0 };
695 unsigned char *p = buf + sizeof(buf);
696
697 ret = mbedtls_x509_set_extension(&(ctx.MBEDTLS_PRIVATE(extensions)),
698 MBEDTLS_OID_EXTENDED_KEY_USAGE,
699 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
700 0,
701 p,
Jonathan Winzig6c9779f2024-01-09 17:47:10 +0100702 SIZE_MAX);
703 TEST_ASSERT(MBEDTLS_ERR_X509_BAD_INPUT_DATA == ret);
Jonathan Winzig2bd2b782024-01-09 15:19:42 +0100704}
705/* END_CASE */
Sam Berry2bb3f4d2024-07-19 15:14:27 +0100706
707/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
708void oid_from_numeric_string(char *oid_str, int error_ret,
709 data_t *exp_oid_buf)
710{
711 mbedtls_asn1_buf oid = { 0, 0, NULL };
712 mbedtls_asn1_buf exp_oid = { 0, 0, NULL };
713 int ret;
714
715 exp_oid.tag = MBEDTLS_ASN1_OID;
716 exp_oid.p = exp_oid_buf->x;
717 exp_oid.len = exp_oid_buf->len;
718
719 ret = mbedtls_oid_from_numeric_string(&oid, oid_str, strlen(oid_str));
720
721 if (error_ret == 0) {
722 TEST_EQUAL(oid.len, exp_oid.len);
723 TEST_ASSERT(memcmp(oid.p, exp_oid.p, oid.len) == 0);
724 mbedtls_free(oid.p);
725 oid.p = NULL;
726 oid.len = 0;
727 } else {
728 TEST_EQUAL(ret, error_ret);
729 }
730}
731/* END_CASE */