blob: b7e531e653742d2cc1416a857be173db8617d1c8 [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"
Ben Taylorc801d322025-07-03 15:01:39 +010013#if defined(MBEDTLS_PK_HAVE_PRIVATE_HEADER)
14#include <mbedtls/private/pk_private.h>
15#endif /* MBEDTLS_PK_HAVE_PRIVATE_HEADER */
Valerio Setti384fbde2024-01-02 13:26:40 +010016#include "mbedtls/psa_util.h"
Manuel Pégourié-Gonnardfeb03962020-08-20 09:59:33 +020017
Gilles Peskinef4e672e2020-01-31 14:22:10 +010018#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
19 defined(MBEDTLS_PEM_WRITE_C) && defined(MBEDTLS_X509_CSR_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010020static int x509_crt_verifycsr(const unsigned char *buf, size_t buflen)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050021{
Przemek Stekiel54a54462022-08-01 13:59:12 +020022 unsigned char hash[PSA_HASH_MAX_SIZE];
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050023 mbedtls_x509_csr csr;
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010024 int ret = 0;
25
Gilles Peskine449bd832023-01-11 14:50:10 +010026 mbedtls_x509_csr_init(&csr);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050027
Gilles Peskine449bd832023-01-11 14:50:10 +010028 if (mbedtls_x509_csr_parse(&csr, buf, buflen) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010029 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
30 goto cleanup;
31 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050032
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020033 psa_algorithm_t psa_alg = mbedtls_md_psa_alg_from_type(csr.sig_md);
Przemek Stekiel54a54462022-08-01 13:59:12 +020034 size_t hash_size = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010035 psa_status_t status = psa_hash_compute(psa_alg, csr.cri.p, csr.cri.len,
36 hash, PSA_HASH_MAX_SIZE, &hash_size);
Przemek Stekiel54a54462022-08-01 13:59:12 +020037
Gilles Peskine449bd832023-01-11 14:50:10 +010038 if (status != PSA_SUCCESS) {
Andrzej Kurek4b114072018-11-19 18:04:01 -050039 /* Note: this can't happen except after an internal error */
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010040 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
41 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050042 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050043
Ben Taylor0c787e32025-07-14 08:33:24 +010044 if (mbedtls_pk_verify_new(csr.sig_pk, NULL, &csr.pk,
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +020045 csr.sig_md, hash, mbedtls_md_get_size_from_type(csr.sig_md),
Gilles Peskine449bd832023-01-11 14:50:10 +010046 csr.sig.p, csr.sig.len) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010047 ret = MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
48 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050049 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050050
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010051cleanup:
52
Gilles Peskine449bd832023-01-11 14:50:10 +010053 mbedtls_x509_csr_free(&csr);
54 return ret;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050055}
Gilles Peskinef4e672e2020-01-31 14:22:10 +010056#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_PEM_WRITE_C && MBEDTLS_X509_CSR_WRITE_C */
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050057
Valerio Setti48e8fc72022-10-19 15:14:29 +020058#if defined(MBEDTLS_X509_CSR_WRITE_C)
59
60/*
61 * The size of this temporary buffer is given by the sequence of functions
62 * called hereinafter:
63 * - mbedtls_asn1_write_oid()
64 * - 8 bytes for MBEDTLS_OID_EXTENDED_KEY_USAGE raw value
65 * - 1 byte for MBEDTLS_OID_EXTENDED_KEY_USAGE length
66 * - 1 byte for MBEDTLS_ASN1_OID tag
67 * - mbedtls_asn1_write_len()
68 * - 1 byte since we're dealing with sizes which are less than 0x80
69 * - mbedtls_asn1_write_tag()
70 * - 1 byte
71 *
72 * This length is fine as long as this function is called using the
73 * MBEDTLS_OID_SERVER_AUTH OID. If this is changed in the future, then this
74 * buffer's length should be adjusted accordingly.
75 * Unfortunately there's no predefined max size for OIDs which can be used
76 * to set an overall upper boundary which is always guaranteed.
77 */
78#define EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH 12
79
Gilles Peskine449bd832023-01-11 14:50:10 +010080static int csr_set_extended_key_usage(mbedtls_x509write_csr *ctx,
81 const char *oid, size_t oid_len)
Valerio Setti48e8fc72022-10-19 15:14:29 +020082{
83 unsigned char buf[EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH] = { 0 };
Gilles Peskine449bd832023-01-11 14:50:10 +010084 unsigned char *p = buf + sizeof(buf);
Valerio Setti48e8fc72022-10-19 15:14:29 +020085 int ret;
86 size_t len = 0;
87
88 /*
89 * Following functions fail anyway if the temporary buffer is not large,
90 * but we set an extra check here to emphasize a possible source of errors
91 */
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (oid_len > EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH) {
Valerio Setti48e8fc72022-10-19 15:14:29 +020093 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
94 }
95
Gilles Peskine449bd832023-01-11 14:50:10 +010096 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(&p, buf, oid, oid_len));
97 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, ret));
98 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, buf,
99 MBEDTLS_ASN1_CONSTRUCTED |
100 MBEDTLS_ASN1_SEQUENCE));
Valerio Setti48e8fc72022-10-19 15:14:29 +0200101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 ret = mbedtls_x509write_csr_set_extension(ctx,
103 MBEDTLS_OID_EXTENDED_KEY_USAGE,
104 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
105 0,
106 p,
107 len);
Valerio Setti48e8fc72022-10-19 15:14:29 +0200108
109 return ret;
110}
111#endif /* MBEDTLS_X509_CSR_WRITE_C */
Gilles Peskinec94500b2023-09-21 18:01:05 +0200112
113/* Due to inconsistencies in the input size limits applied by different
114 * library functions, some write-parse tests may fail. */
115#define MAY_FAIL_GET_NAME 0x0001
116#define MAY_FAIL_DN_GETS 0x0002
117
Paul Bakker33b43f12013-08-20 11:48:36 +0200118/* END_HEADER */
Paul Bakker6d620502012-02-16 14:09:13 +0000119
Paul Bakker33b43f12013-08-20 11:48:36 +0200120/* BEGIN_DEPENDENCIES
Valerio Setti3580f442023-07-27 10:19:53 +0200121 * depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_PARSE_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200122 * END_DEPENDENCIES
123 */
Paul Bakker6d620502012-02-16 14:09:13 +0000124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100126void x509_csr_check(char *key_file, char *cert_req_check_file, int md_type,
127 int key_usage, int set_key_usage, int cert_type,
128 int set_cert_type, int set_extension)
Paul Bakker6d620502012-02-16 14:09:13 +0000129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_pk_context key;
131 mbedtls_x509write_csr req;
Andres AGe0af9952016-09-07 11:09:44 +0100132 unsigned char buf[4096];
Paul Bakker6d620502012-02-16 14:09:13 +0000133 int ret;
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100134#if !defined(MBEDTLS_USE_PSA_CRYPTO)
135 unsigned char check_buf[4000];
Paul Bakker6d620502012-02-16 14:09:13 +0000136 FILE *f;
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100137 size_t olen = 0;
138#endif /* !MBEDTLS_USE_PSA_CRYPTO */
139 size_t pem_len = 0, buf_index;
140 int der_len = -1;
Paul Bakker3a8cb6f2013-12-30 20:41:54 +0100141 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200142 mbedtls_test_rnd_pseudo_info rnd_info;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100143 mbedtls_x509_san_list san_ip;
144 mbedtls_x509_san_list san_dns;
145 mbedtls_x509_san_list san_uri;
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400146 mbedtls_x509_san_list san_mail;
147 mbedtls_x509_san_list san_dn;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100148 mbedtls_x509_san_list *san_list = NULL;
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400149 mbedtls_asn1_named_data *ext_san_dirname = NULL;
150
151 const char san_ip_name[] = { 0x7f, 0x00, 0x00, 0x01 }; // 127.0.0.1
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100152 const char *san_dns_name = "example.com";
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400153 const char *san_dn_name = "C=UK,O=Mbed TLS,CN=Mbed TLS directoryName SAN";
154 const char *san_mail_name = "mail@example.com";
155 const char *san_uri_name = "http://pki.example.com";
156
157 san_mail.node.type = MBEDTLS_X509_SAN_RFC822_NAME;
158 san_mail.node.san.unstructured_name.p = (unsigned char *) san_mail_name;
159 san_mail.node.san.unstructured_name.len = strlen(san_mail_name);
160 san_mail.next = NULL;
161
162 san_dns.node.type = MBEDTLS_X509_SAN_DNS_NAME;
163 san_dns.node.san.unstructured_name.p = (unsigned char *) san_dns_name;
164 san_dns.node.san.unstructured_name.len = strlen(san_dns_name);
165 san_dns.next = &san_mail;
166
167 san_dn.node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
168 TEST_ASSERT(mbedtls_x509_string_to_names(&ext_san_dirname,
169 san_dn_name) == 0);
170 san_dn.node.san.directory_name = *ext_san_dirname;
171 san_dn.next = &san_dns;
172
173 san_ip.node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
174 san_ip.node.san.unstructured_name.p = (unsigned char *) san_ip_name;
175 san_ip.node.san.unstructured_name.len = sizeof(san_ip_name);
176 san_ip.next = &san_dn;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100177
178 san_uri.node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100179 san_uri.node.san.unstructured_name.p = (unsigned char *) san_uri_name;
180 san_uri.node.san.unstructured_name.len = strlen(san_uri_name);
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400181 san_uri.next = &san_ip;
182
183 san_list = &san_uri;
Paul Bakker6d620502012-02-16 14:09:13 +0000184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 mbedtls_x509write_csr_init(&req);
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 mbedtls_pk_init(&key);
valerio32f2ac92023-04-20 11:59:52 +0200189 MD_OR_USE_PSA_INIT();
190
Ben Taylor440cb2a2025-03-05 09:40:08 +0000191 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL) == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 mbedtls_x509write_csr_set_md_alg(&req, md_type);
194 mbedtls_x509write_csr_set_key(&req, &key);
195 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
196 if (set_key_usage != 0) {
197 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
198 }
199 if (set_cert_type != 0) {
200 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
201 }
202 if (set_extension != 0) {
203 TEST_ASSERT(csr_set_extended_key_usage(&req, MBEDTLS_OID_SERVER_AUTH,
204 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) == 0);
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100205
206 TEST_ASSERT(mbedtls_x509write_csr_set_subject_alternative_name(&req, san_list) == 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 }
Paul Bakker8eabfc12013-08-25 10:18:25 +0200208
Ben Taylor440cb2a2025-03-05 09:40:08 +0000209 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf));
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 TEST_ASSERT(ret == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 pem_len = strlen((char *) buf);
Paul Bakker6d620502012-02-16 14:09:13 +0000213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
215 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000216 }
217
Neil Armstrong5b320382022-02-21 17:22:10 +0100218#if defined(MBEDTLS_USE_PSA_CRYPTO)
219 // When using PSA crypto, RNG isn't controllable, so cert_req_check_file can't be used
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 (void) cert_req_check_file;
Neil Armstrong5b320382022-02-21 17:22:10 +0100221 buf[pem_len] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0);
Neil Armstrong5b320382022-02-21 17:22:10 +0100223#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 f = fopen(cert_req_check_file, "r");
225 TEST_ASSERT(f != NULL);
226 olen = fread(check_buf, 1, sizeof(check_buf), f);
227 fclose(f);
Paul Bakker6d620502012-02-16 14:09:13 +0000228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 TEST_ASSERT(olen >= pem_len - 1);
230 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100231#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100232
Ben Taylor440cb2a2025-03-05 09:40:08 +0000233 der_len = mbedtls_x509write_csr_der(&req, buf, sizeof(buf));
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100237 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 }
Andres AGe0af9952016-09-07 11:09:44 +0100239
Neil Armstrong5b320382022-02-21 17:22:10 +0100240#if defined(MBEDTLS_USE_PSA_CRYPTO)
241 // When using PSA crypto, RNG isn't controllable, result length isn't
242 // deterministic over multiple runs, removing a single byte isn't enough to
243 // go into the MBEDTLS_ERR_ASN1_BUF_TOO_SMALL error case
244 der_len /= 2;
245#else
246 der_len -= 1;
247#endif
Ben Taylor440cb2a2025-03-05 09:40:08 +0000248 ret = mbedtls_x509write_csr_der(&req, buf, (size_t) (der_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100250
Paul Bakkerbd51b262014-07-10 15:26:12 +0200251exit:
Andrzej Kurekbdb41dd2023-07-10 08:09:50 -0400252 mbedtls_asn1_free_named_data_list(&ext_san_dirname);
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 mbedtls_x509write_csr_free(&req);
254 mbedtls_pk_free(&key);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100255 MD_OR_USE_PSA_DONE();
Paul Bakker6d620502012-02-16 14:09:13 +0000256}
Paul Bakker33b43f12013-08-20 11:48:36 +0200257/* END_CASE */
Paul Bakker2397cf32013-09-08 15:58:15 +0200258
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500259/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C:MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100260void x509_csr_check_opaque(char *key_file, int md_type, int key_usage,
261 int cert_type)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500262{
263 mbedtls_pk_context key;
Paul Elliott9a209b82024-10-25 12:41:28 +0100264 mbedtls_pk_init(&key);
265
Ronald Cron5425a212020-08-04 14:58:35 +0200266 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100267 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliott9a209b82024-10-25 12:41:28 +0100268
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500269 mbedtls_x509write_csr req;
Paul Elliott9a209b82024-10-25 12:41:28 +0100270 mbedtls_x509write_csr_init(&req);
271
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500272 unsigned char buf[4096];
273 int ret;
274 size_t pem_len = 0;
275 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200276 mbedtls_test_rnd_pseudo_info rnd_info;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500277
Valerio Setti569c1712023-04-19 14:53:36 +0200278 MD_OR_USE_PSA_INIT();
279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500281
Ben Taylor440cb2a2025-03-05 09:40:08 +0000282 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL) == 0);
Neil Armstrong95974972022-04-22 13:57:44 +0200283
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100284 /* Turn the PK context into an opaque one. */
285 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&key, PSA_KEY_USAGE_SIGN_HASH, &key_attr), 0);
286 TEST_EQUAL(mbedtls_pk_import_into_psa(&key, &key_attr, &key_id), 0);
287 mbedtls_pk_free(&key);
288 mbedtls_pk_init(&key);
289 TEST_EQUAL(mbedtls_pk_setup_opaque(&key, key_id), 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 mbedtls_x509write_csr_set_md_alg(&req, md_type);
292 mbedtls_x509write_csr_set_key(&req, &key);
293 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
294 if (key_usage != 0) {
295 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
296 }
297 if (cert_type != 0) {
298 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
299 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500300
Ben Taylor440cb2a2025-03-05 09:40:08 +0000301 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf) - 1);
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 TEST_ASSERT(ret == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 pem_len = strlen((char *) buf);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500306 buf[pem_len] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500308
Manuel Pégourié-Gonnardfeb03962020-08-20 09:59:33 +0200309
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500310exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 mbedtls_x509write_csr_free(&req);
312 mbedtls_pk_free(&key);
313 psa_destroy_key(key_id);
Valerio Setti569c1712023-04-19 14:53:36 +0200314 MD_OR_USE_PSA_DONE();
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500315}
316/* END_CASE */
317
Elena Uziunaite9fc5be02024-09-04 18:12:59 +0100318/* 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 +0100319void x509_crt_check(char *subject_key_file, char *subject_pwd,
320 char *subject_name, char *issuer_key_file,
321 char *issuer_pwd, char *issuer_name,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100322 data_t *serial_arg, char *not_before, char *not_after,
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 int md_type, int key_usage, int set_key_usage,
324 char *ext_key_usage,
325 int cert_type, int set_cert_type, int auth_ident,
326 int ver, char *cert_check_file, int pk_wrap, int is_ca,
Andrzej Kurek76c96622023-04-04 06:57:08 -0400327 char *cert_verify_file, int set_subjectAltNames)
Paul Bakker2397cf32013-09-08 15:58:15 +0200328{
Hanno Becker418a6222017-09-14 07:51:28 +0100329 mbedtls_pk_context subject_key, issuer_key, issuer_key_alt;
330 mbedtls_pk_context *key = &issuer_key;
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 mbedtls_x509write_cert crt;
Andres AGe0af9952016-09-07 11:09:44 +0100333 unsigned char buf[4096];
Paul Bakker2397cf32013-09-08 15:58:15 +0200334 unsigned char check_buf[5000];
Werner Lewisacd01e52022-05-10 12:23:13 +0100335 unsigned char *p, *end;
336 unsigned char tag, sz;
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100337#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
338 mbedtls_mpi serial_mpi;
339#endif
Werner Lewisacd01e52022-05-10 12:23:13 +0100340 int ret, before_tag, after_tag;
Paul Elliott557b8d62020-11-19 09:46:56 +0000341 size_t olen = 0, pem_len = 0, buf_index = 0;
Andres AGe0af9952016-09-07 11:09:44 +0100342 int der_len = -1;
Paul Bakker2397cf32013-09-08 15:58:15 +0200343 FILE *f;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200344 mbedtls_test_rnd_pseudo_info rnd_info;
Neil Armstrong98f899c2022-03-16 17:42:42 +0100345#if defined(MBEDTLS_USE_PSA_CRYPTO)
346 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100347 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong98f899c2022-03-16 17:42:42 +0100348#endif
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100349 mbedtls_pk_type_t issuer_key_type;
Andrzej Kurek76c96622023-04-04 06:57:08 -0400350 mbedtls_x509_san_list san_ip;
351 mbedtls_x509_san_list san_dns;
352 mbedtls_x509_san_list san_uri;
353 mbedtls_x509_san_list san_mail;
354 mbedtls_x509_san_list san_dn;
355 mbedtls_asn1_named_data *ext_san_dirname = NULL;
356 const char san_ip_name[] = { 0x01, 0x02, 0x03, 0x04 };
357 const char *san_dns_name = "example.com";
358 const char *san_dn_name = "C=UK,O=Mbed TLS,CN=SubjectAltName test";
359 const char *san_mail_name = "mail@example.com";
360 const char *san_uri_name = "http://pki.example.com";
361 mbedtls_x509_san_list *san_list = NULL;
362
363 if (set_subjectAltNames) {
364 san_mail.node.type = MBEDTLS_X509_SAN_RFC822_NAME;
365 san_mail.node.san.unstructured_name.p = (unsigned char *) san_mail_name;
Andrzej Kurek13c43f62023-04-04 10:43:38 -0400366 san_mail.node.san.unstructured_name.len = strlen(san_mail_name);
Andrzej Kurek76c96622023-04-04 06:57:08 -0400367 san_mail.next = NULL;
368
369 san_dns.node.type = MBEDTLS_X509_SAN_DNS_NAME;
370 san_dns.node.san.unstructured_name.p = (unsigned char *) san_dns_name;
371 san_dns.node.san.unstructured_name.len = strlen(san_dns_name);
372 san_dns.next = &san_mail;
373
374 san_dn.node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
375 TEST_ASSERT(mbedtls_x509_string_to_names(&ext_san_dirname,
376 san_dn_name) == 0);
377 san_dn.node.san.directory_name = *ext_san_dirname;
378 san_dn.next = &san_dns;
379
380 san_ip.node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
381 san_ip.node.san.unstructured_name.p = (unsigned char *) san_ip_name;
382 san_ip.node.san.unstructured_name.len = sizeof(san_ip_name);
383 san_ip.next = &san_dn;
384
385 san_uri.node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
386 san_uri.node.san.unstructured_name.p = (unsigned char *) san_uri_name;
387 san_uri.node.san.unstructured_name.len = strlen(san_uri_name);
388 san_uri.next = &san_ip;
389
390 san_list = &san_uri;
391 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100394#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
395 mbedtls_mpi_init(&serial_mpi);
396#endif
Hanno Becker418a6222017-09-14 07:51:28 +0100397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 mbedtls_pk_init(&subject_key);
399 mbedtls_pk_init(&issuer_key);
400 mbedtls_pk_init(&issuer_key_alt);
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 mbedtls_x509write_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200402 MD_OR_USE_PSA_INIT();
Paul Bakker2397cf32013-09-08 15:58:15 +0200403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 TEST_ASSERT(mbedtls_pk_parse_keyfile(&subject_key, subject_key_file,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000405 subject_pwd) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 TEST_ASSERT(mbedtls_pk_parse_keyfile(&issuer_key, issuer_key_file,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000408 issuer_pwd) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 issuer_key_type = mbedtls_pk_get_type(&issuer_key);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100411
Neil Armstrong98f899c2022-03-16 17:42:42 +0100412#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100413 /* Turn the issuer PK context into an opaque one. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 if (pk_wrap == 2) {
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100415 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&issuer_key, PSA_KEY_USAGE_SIGN_HASH,
416 &key_attr), 0);
417 TEST_EQUAL(mbedtls_pk_import_into_psa(&issuer_key, &key_attr, &key_id), 0);
418 mbedtls_pk_free(&issuer_key);
419 mbedtls_pk_init(&issuer_key);
420 TEST_EQUAL(mbedtls_pk_setup_opaque(&issuer_key, key_id), 0);
Neil Armstrong98f899c2022-03-16 17:42:42 +0100421 }
422#endif /* MBEDTLS_USE_PSA_CRYPTO */
423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if (pk_wrap == 2) {
425 TEST_ASSERT(mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_OPAQUE);
426 }
Neil Armstrong98f899c2022-03-16 17:42:42 +0100427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 if (ver != -1) {
429 mbedtls_x509write_crt_set_version(&crt, ver);
430 }
Hanno Becker418a6222017-09-14 07:51:28 +0100431
Valerio Settiaf4815c2023-01-26 17:43:09 +0100432 TEST_ASSERT(mbedtls_x509write_crt_set_serial_raw(&crt, serial_arg->x,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100433 serial_arg->len) == 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 TEST_ASSERT(mbedtls_x509write_crt_set_validity(&crt, not_before,
435 not_after) == 0);
436 mbedtls_x509write_crt_set_md_alg(&crt, md_type);
437 TEST_ASSERT(mbedtls_x509write_crt_set_issuer_name(&crt, issuer_name) == 0);
438 TEST_ASSERT(mbedtls_x509write_crt_set_subject_name(&crt, subject_name) == 0);
439 mbedtls_x509write_crt_set_subject_key(&crt, &subject_key);
Hanno Becker418a6222017-09-14 07:51:28 +0100440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 mbedtls_x509write_crt_set_issuer_key(&crt, key);
Paul Bakker2397cf32013-09-08 15:58:15 +0200442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (crt.version >= MBEDTLS_X509_CRT_VERSION_3) {
Darren Krahn9c134ce2021-01-13 22:04:45 -0800444 /* For the CA case, a path length of -1 means unlimited. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 TEST_ASSERT(mbedtls_x509write_crt_set_basic_constraints(&crt, is_ca,
446 (is_ca ? -1 : 0)) == 0);
447 TEST_ASSERT(mbedtls_x509write_crt_set_subject_key_identifier(&crt) == 0);
448 if (auth_ident) {
449 TEST_ASSERT(mbedtls_x509write_crt_set_authority_key_identifier(&crt) == 0);
450 }
451 if (set_key_usage != 0) {
452 TEST_ASSERT(mbedtls_x509write_crt_set_key_usage(&crt, key_usage) == 0);
453 }
454 if (set_cert_type != 0) {
455 TEST_ASSERT(mbedtls_x509write_crt_set_ns_cert_type(&crt, cert_type) == 0);
456 }
457 if (strcmp(ext_key_usage, "NULL") != 0) {
Dave Rodgmanec9f6b42022-07-27 14:34:58 +0100458 mbedtls_asn1_sequence exts[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 memset(exts, 0, sizeof(exts));
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100460
461#define SET_OID(x, oid) \
462 do { \
463 x.len = MBEDTLS_OID_SIZE(oid); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 x.p = (unsigned char *) oid; \
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100465 x.tag = MBEDTLS_ASN1_OID; \
Dave Rodgmane2b772d2022-08-11 16:04:13 +0100466 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 while (0)
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (strcmp(ext_key_usage, "serverAuth") == 0) {
470 SET_OID(exts[0].buf, MBEDTLS_OID_SERVER_AUTH);
471 } else if (strcmp(ext_key_usage, "codeSigning,timeStamping") == 0) {
472 SET_OID(exts[0].buf, MBEDTLS_OID_CODE_SIGNING);
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100473 exts[0].next = &exts[1];
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 SET_OID(exts[1].buf, MBEDTLS_OID_TIME_STAMPING);
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000475 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 TEST_ASSERT(mbedtls_x509write_crt_set_ext_key_usage(&crt, exts) == 0);
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000477 }
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100478 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200479
Andrzej Kurek76c96622023-04-04 06:57:08 -0400480 if (set_subjectAltNames) {
481 TEST_ASSERT(mbedtls_x509write_crt_set_subject_alternative_name(&crt, san_list) == 0);
482 }
Ben Taylor440cb2a2025-03-05 09:40:08 +0000483 ret = mbedtls_x509write_crt_pem(&crt, buf, sizeof(buf));
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 TEST_ASSERT(ret == 0);
Paul Bakker2397cf32013-09-08 15:58:15 +0200485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 pem_len = strlen((char *) buf);
Paul Bakker2397cf32013-09-08 15:58:15 +0200487
Paul Elliott557b8d62020-11-19 09:46:56 +0000488 // check that the rest of the buffer remains clear
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
490 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000491 }
492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 if (issuer_key_type != MBEDTLS_PK_RSA) {
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100494 mbedtls_x509_crt crt_parse, trusted;
495 uint32_t flags;
Paul Bakker2397cf32013-09-08 15:58:15 +0200496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 mbedtls_x509_crt_init(&crt_parse);
498 mbedtls_x509_crt_init(&trusted);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted,
501 cert_verify_file) == 0);
502 TEST_ASSERT(mbedtls_x509_crt_parse(&crt_parse,
503 buf, sizeof(buf)) == 0);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 ret = mbedtls_x509_crt_verify(&crt_parse, &trusted, NULL, NULL, &flags,
506 NULL, NULL);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 mbedtls_x509_crt_free(&crt_parse);
509 mbedtls_x509_crt_free(&trusted);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 TEST_EQUAL(flags, 0);
512 TEST_EQUAL(ret, 0);
513 } else if (*cert_check_file != '\0') {
514 f = fopen(cert_check_file, "r");
515 TEST_ASSERT(f != NULL);
516 olen = fread(check_buf, 1, sizeof(check_buf), f);
517 fclose(f);
518 TEST_ASSERT(olen < sizeof(check_buf));
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 TEST_EQUAL(olen, pem_len);
521 TEST_ASSERT(olen >= pem_len - 1);
522 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100523 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200524
Ben Taylor440cb2a2025-03-05 09:40:08 +0000525 der_len = mbedtls_x509write_crt_der(&crt, buf, sizeof(buf));
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100529 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 }
Andres AGe0af9952016-09-07 11:09:44 +0100531
Werner Lewisacd01e52022-05-10 12:23:13 +0100532 // Not testing against file, check date format
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 if (*cert_check_file == '\0') {
Werner Lewisacd01e52022-05-10 12:23:13 +0100534 // UTC tag if before 2050, 2 digits less for year
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 if (not_before[0] == '2' && (not_before[1] > '0' || not_before[2] > '4')) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100536 before_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 } else {
Werner Lewisacd01e52022-05-10 12:23:13 +0100538 before_tag = MBEDTLS_ASN1_UTC_TIME;
539 not_before += 2;
540 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 if (not_after[0] == '2' && (not_after[1] > '0' || not_after[2] > '4')) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100542 after_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 } else {
Werner Lewisacd01e52022-05-10 12:23:13 +0100544 after_tag = MBEDTLS_ASN1_UTC_TIME;
545 not_after += 2;
546 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 end = buf + sizeof(buf);
548 for (p = end - der_len; p < end;) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100549 tag = *p++;
550 sz = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if (tag == MBEDTLS_ASN1_UTC_TIME || tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100552 // Check correct tag and time written
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 TEST_ASSERT(before_tag == tag);
554 TEST_ASSERT(memcmp(p, not_before, sz - 1) == 0);
Werner Lewisacd01e52022-05-10 12:23:13 +0100555 p += sz;
556 tag = *p++;
557 sz = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 TEST_ASSERT(after_tag == tag);
559 TEST_ASSERT(memcmp(p, not_after, sz - 1) == 0);
Werner Lewisacd01e52022-05-10 12:23:13 +0100560 break;
561 }
562 // Increment if long form ASN1 length
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 if (sz & 0x80) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100564 p += sz & 0x0F;
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 }
566 if (tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100567 p += sz;
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 }
Werner Lewisacd01e52022-05-10 12:23:13 +0100569 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 TEST_ASSERT(p < end);
Werner Lewisacd01e52022-05-10 12:23:13 +0100571 }
572
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100573#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge6ed23c2022-04-22 09:44:04 +0200574 // When using PSA crypto, RNG isn't controllable, result length isn't
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100575 // deterministic over multiple runs, removing a single byte isn't enough to
576 // go into the MBEDTLS_ERR_ASN1_BUF_TOO_SMALL error case
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 if (issuer_key_type != MBEDTLS_PK_RSA) {
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100578 der_len /= 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 } else
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100580#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 der_len -= 1;
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100582
Ben Taylor440cb2a2025-03-05 09:40:08 +0000583 ret = mbedtls_x509write_crt_der(&crt, buf, (size_t) (der_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100585
Paul Bakkerbd51b262014-07-10 15:26:12 +0200586exit:
Andrzej Kurek5da1d752023-04-05 08:30:59 -0400587 mbedtls_asn1_free_named_data_list(&ext_san_dirname);
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 mbedtls_x509write_crt_free(&crt);
589 mbedtls_pk_free(&issuer_key_alt);
590 mbedtls_pk_free(&subject_key);
591 mbedtls_pk_free(&issuer_key);
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100592#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
593 mbedtls_mpi_free(&serial_mpi);
594#endif
Neil Armstrong98f899c2022-03-16 17:42:42 +0100595#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 psa_destroy_key(key_id);
Neil Armstrong98f899c2022-03-16 17:42:42 +0100597#endif
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100598 MD_OR_USE_PSA_DONE();
Paul Bakker2397cf32013-09-08 15:58:15 +0200599}
600/* END_CASE */
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200601
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100602/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_WRITE_C */
603void x509_set_serial_check()
604{
605 mbedtls_x509write_cert ctx;
606 uint8_t invalid_serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN + 1];
607
David Horstmanne04a97a2023-12-08 18:27:48 +0000608#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
609 mbedtls_mpi serial_mpi;
610 mbedtls_mpi_init(&serial_mpi);
611#endif
612
Valerio Setti569c1712023-04-19 14:53:36 +0200613 USE_PSA_INIT();
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100614 memset(invalid_serial, 0x01, sizeof(invalid_serial));
615
Valerio Settiaf4815c2023-01-26 17:43:09 +0100616 TEST_EQUAL(mbedtls_x509write_crt_set_serial_raw(&ctx, invalid_serial,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100617 sizeof(invalid_serial)),
618 MBEDTLS_ERR_X509_BAD_INPUT_DATA);
619
Valerio Settia87f8392023-01-26 18:00:50 +0100620exit:
621#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
622 mbedtls_mpi_free(&serial_mpi);
623#else
624 ;
625#endif
Valerio Setti569c1712023-04-19 14:53:36 +0200626 USE_PSA_DONE();
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100627}
628/* END_CASE */
629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C */
Gilles Peskinec94500b2023-09-21 18:01:05 +0200631void mbedtls_x509_string_to_names(char *name, char *parsed_name,
632 int result, int may_fail)
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200633{
634 int ret;
635 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_asn1_named_data *names = NULL;
Gilles Peskine21e46b32023-10-17 16:35:20 +0200637 mbedtls_x509_name parsed;
638 memset(&parsed, 0, sizeof(parsed));
639 mbedtls_x509_name *parsed_cur = NULL;
640 mbedtls_x509_name *parsed_prv = NULL;
Gilles Peskinef2574202023-10-18 17:39:48 +0200641 unsigned char buf[1024] = { 0 };
642 unsigned char out[1024] = { 0 };
Gilles Peskine21e46b32023-10-17 16:35:20 +0200643 unsigned char *c = buf + sizeof(buf);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200644
Valerio Setti569c1712023-04-19 14:53:36 +0200645 USE_PSA_INIT();
646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 ret = mbedtls_x509_string_to_names(&names, name);
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200648 TEST_EQUAL(ret, result);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (ret != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200651 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 }
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 ret = mbedtls_x509_write_names(&c, buf, names);
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200655 TEST_LE_S(1, ret);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200656
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200657 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
Gilles Peskineaa01a032023-09-21 15:57:30 +0200658 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
Gilles Peskinec94500b2023-09-21 18:01:05 +0200659 ret = mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed);
660 if ((may_fail & MAY_FAIL_GET_NAME) && ret < 0) {
661 /* Validation inconsistency between mbedtls_x509_string_to_names() and
662 * mbedtls_x509_get_name(). Accept it for now. */
663 goto exit;
664 }
665 TEST_EQUAL(ret, 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 ret = mbedtls_x509_dn_gets((char *) out, sizeof(out), &parsed);
Gilles Peskinec94500b2023-09-21 18:01:05 +0200668 if ((may_fail & MAY_FAIL_DN_GETS) && ret < 0) {
669 /* Validation inconsistency between mbedtls_x509_string_to_names() and
670 * mbedtls_x509_dn_gets(). Accept it for now. */
671 goto exit;
672 }
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200673 TEST_LE_S(1, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 TEST_ASSERT(strcmp((char *) out, parsed_name) == 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200675
Manuel Pégourié-Gonnardbda3ab92025-05-05 18:25:26 +0200676 /* Check that calling a 2nd time with the same param (now non-NULL)
677 * returns an error as expected. */
678 ret = mbedtls_x509_string_to_names(&names, name);
679 TEST_EQUAL(ret, MBEDTLS_ERR_X509_BAD_INPUT_DATA);
680
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200681exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 mbedtls_asn1_free_named_data_list(&names);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200683
684 parsed_cur = parsed.next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 while (parsed_cur != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200686 parsed_prv = parsed_cur;
687 parsed_cur = parsed_cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 mbedtls_free(parsed_prv);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200689 }
Valerio Setti569c1712023-04-19 14:53:36 +0200690 USE_PSA_DONE();
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200691}
692/* END_CASE */
Jonathan Winzig2bd2b782024-01-09 15:19:42 +0100693
Jonathan Winzig315c3ca2024-01-09 18:31:11 +0100694/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_WRITE_C */
Jonathan Winzig6c9779f2024-01-09 17:47:10 +0100695void x509_set_extension_length_check()
Jonathan Winzig2bd2b782024-01-09 15:19:42 +0100696{
697 int ret = 0;
698
699 mbedtls_x509write_csr ctx;
700 mbedtls_x509write_csr_init(&ctx);
701
702 unsigned char buf[EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH] = { 0 };
703 unsigned char *p = buf + sizeof(buf);
704
705 ret = mbedtls_x509_set_extension(&(ctx.MBEDTLS_PRIVATE(extensions)),
706 MBEDTLS_OID_EXTENDED_KEY_USAGE,
707 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
708 0,
709 p,
Jonathan Winzig6c9779f2024-01-09 17:47:10 +0100710 SIZE_MAX);
711 TEST_ASSERT(MBEDTLS_ERR_X509_BAD_INPUT_DATA == ret);
Jonathan Winzig2bd2b782024-01-09 15:19:42 +0100712}
713/* END_CASE */
Sam Berry2bb3f4d2024-07-19 15:14:27 +0100714
Gilles Peskineb8288202025-05-12 21:07:47 +0200715/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C */
Sam Berry2bb3f4d2024-07-19 15:14:27 +0100716void oid_from_numeric_string(char *oid_str, int error_ret,
717 data_t *exp_oid_buf)
718{
719 mbedtls_asn1_buf oid = { 0, 0, NULL };
720 mbedtls_asn1_buf exp_oid = { 0, 0, NULL };
721 int ret;
722
723 exp_oid.tag = MBEDTLS_ASN1_OID;
724 exp_oid.p = exp_oid_buf->x;
725 exp_oid.len = exp_oid_buf->len;
726
727 ret = mbedtls_oid_from_numeric_string(&oid, oid_str, strlen(oid_str));
728
729 if (error_ret == 0) {
730 TEST_EQUAL(oid.len, exp_oid.len);
731 TEST_ASSERT(memcmp(oid.p, exp_oid.p, oid.len) == 0);
732 mbedtls_free(oid.p);
733 oid.p = NULL;
734 oid.len = 0;
735 } else {
736 TEST_EQUAL(ret, error_ret);
737 }
738}
739/* END_CASE */