blob: f42349cb5b21fdc4a4405b8fea7a7b4c3982ffd6 [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
Ben Taylor90204262025-06-09 11:51:28 +010018#if defined(MBEDTLS_PEM_WRITE_C) && defined(MBEDTLS_X509_CSR_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010019static int x509_crt_verifycsr(const unsigned char *buf, size_t buflen)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050020{
Przemek Stekiel54a54462022-08-01 13:59:12 +020021 unsigned char hash[PSA_HASH_MAX_SIZE];
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050022 mbedtls_x509_csr csr;
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010023 int ret = 0;
24
Gilles Peskine449bd832023-01-11 14:50:10 +010025 mbedtls_x509_csr_init(&csr);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050026
Gilles Peskine449bd832023-01-11 14:50:10 +010027 if (mbedtls_x509_csr_parse(&csr, buf, buflen) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010028 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
29 goto cleanup;
30 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050031
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020032 psa_algorithm_t psa_alg = mbedtls_md_psa_alg_from_type(csr.sig_md);
Przemek Stekiel54a54462022-08-01 13:59:12 +020033 size_t hash_size = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010034 psa_status_t status = psa_hash_compute(psa_alg, csr.cri.p, csr.cri.len,
35 hash, PSA_HASH_MAX_SIZE, &hash_size);
Przemek Stekiel54a54462022-08-01 13:59:12 +020036
Gilles Peskine449bd832023-01-11 14:50:10 +010037 if (status != PSA_SUCCESS) {
Andrzej Kurek4b114072018-11-19 18:04:01 -050038 /* Note: this can't happen except after an internal error */
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010039 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
40 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050041 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050042
Ben Taylor5be85112025-07-17 10:05:23 +010043 if (mbedtls_pk_verify_new(csr.sig_pk, &csr.pk,
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +020044 csr.sig_md, hash, mbedtls_md_get_size_from_type(csr.sig_md),
Gilles Peskine449bd832023-01-11 14:50:10 +010045 csr.sig.p, csr.sig.len) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010046 ret = MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
47 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050048 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050049
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010050cleanup:
51
Gilles Peskine449bd832023-01-11 14:50:10 +010052 mbedtls_x509_csr_free(&csr);
53 return ret;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050054}
Ben Taylor90204262025-06-09 11:51:28 +010055#endif /* MBEDTLS_PEM_WRITE_C && MBEDTLS_X509_CSR_WRITE_C */
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050056
Valerio Setti48e8fc72022-10-19 15:14:29 +020057#if defined(MBEDTLS_X509_CSR_WRITE_C)
58
59/*
60 * The size of this temporary buffer is given by the sequence of functions
61 * called hereinafter:
62 * - mbedtls_asn1_write_oid()
63 * - 8 bytes for MBEDTLS_OID_EXTENDED_KEY_USAGE raw value
64 * - 1 byte for MBEDTLS_OID_EXTENDED_KEY_USAGE length
65 * - 1 byte for MBEDTLS_ASN1_OID tag
66 * - mbedtls_asn1_write_len()
67 * - 1 byte since we're dealing with sizes which are less than 0x80
68 * - mbedtls_asn1_write_tag()
69 * - 1 byte
70 *
71 * This length is fine as long as this function is called using the
72 * MBEDTLS_OID_SERVER_AUTH OID. If this is changed in the future, then this
73 * buffer's length should be adjusted accordingly.
74 * Unfortunately there's no predefined max size for OIDs which can be used
75 * to set an overall upper boundary which is always guaranteed.
76 */
77#define EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH 12
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079static int csr_set_extended_key_usage(mbedtls_x509write_csr *ctx,
80 const char *oid, size_t oid_len)
Valerio Setti48e8fc72022-10-19 15:14:29 +020081{
82 unsigned char buf[EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH] = { 0 };
Gilles Peskine449bd832023-01-11 14:50:10 +010083 unsigned char *p = buf + sizeof(buf);
Valerio Setti48e8fc72022-10-19 15:14:29 +020084 int ret;
85 size_t len = 0;
86
87 /*
88 * Following functions fail anyway if the temporary buffer is not large,
89 * but we set an extra check here to emphasize a possible source of errors
90 */
Gilles Peskine449bd832023-01-11 14:50:10 +010091 if (oid_len > EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH) {
Valerio Setti48e8fc72022-10-19 15:14:29 +020092 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
93 }
94
Gilles Peskine449bd832023-01-11 14:50:10 +010095 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(&p, buf, oid, oid_len));
96 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, ret));
97 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, buf,
98 MBEDTLS_ASN1_CONSTRUCTED |
99 MBEDTLS_ASN1_SEQUENCE));
Valerio Setti48e8fc72022-10-19 15:14:29 +0200100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 ret = mbedtls_x509write_csr_set_extension(ctx,
102 MBEDTLS_OID_EXTENDED_KEY_USAGE,
103 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
104 0,
105 p,
106 len);
Valerio Setti48e8fc72022-10-19 15:14:29 +0200107
108 return ret;
109}
110#endif /* MBEDTLS_X509_CSR_WRITE_C */
Gilles Peskinec94500b2023-09-21 18:01:05 +0200111
112/* Due to inconsistencies in the input size limits applied by different
113 * library functions, some write-parse tests may fail. */
114#define MAY_FAIL_GET_NAME 0x0001
115#define MAY_FAIL_DN_GETS 0x0002
116
Paul Bakker33b43f12013-08-20 11:48:36 +0200117/* END_HEADER */
Paul Bakker6d620502012-02-16 14:09:13 +0000118
Paul Bakker33b43f12013-08-20 11:48:36 +0200119/* BEGIN_DEPENDENCIES
Valerio Setti3580f442023-07-27 10:19:53 +0200120 * depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_PARSE_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200121 * END_DEPENDENCIES
122 */
Paul Bakker6d620502012-02-16 14:09:13 +0000123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125void x509_csr_check(char *key_file, char *cert_req_check_file, int md_type,
126 int key_usage, int set_key_usage, int cert_type,
127 int set_cert_type, int set_extension)
Paul Bakker6d620502012-02-16 14:09:13 +0000128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_pk_context key;
130 mbedtls_x509write_csr req;
Andres AGe0af9952016-09-07 11:09:44 +0100131 unsigned char buf[4096];
Paul Bakker6d620502012-02-16 14:09:13 +0000132 int ret;
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100133 size_t pem_len = 0, buf_index;
134 int der_len = -1;
Paul Bakker3a8cb6f2013-12-30 20:41:54 +0100135 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200136 mbedtls_test_rnd_pseudo_info rnd_info;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100137 mbedtls_x509_san_list san_ip;
138 mbedtls_x509_san_list san_dns;
139 mbedtls_x509_san_list san_uri;
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400140 mbedtls_x509_san_list san_mail;
141 mbedtls_x509_san_list san_dn;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100142 mbedtls_x509_san_list *san_list = NULL;
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400143 mbedtls_asn1_named_data *ext_san_dirname = NULL;
144
145 const char san_ip_name[] = { 0x7f, 0x00, 0x00, 0x01 }; // 127.0.0.1
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100146 const char *san_dns_name = "example.com";
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400147 const char *san_dn_name = "C=UK,O=Mbed TLS,CN=Mbed TLS directoryName SAN";
148 const char *san_mail_name = "mail@example.com";
149 const char *san_uri_name = "http://pki.example.com";
150
151 san_mail.node.type = MBEDTLS_X509_SAN_RFC822_NAME;
152 san_mail.node.san.unstructured_name.p = (unsigned char *) san_mail_name;
153 san_mail.node.san.unstructured_name.len = strlen(san_mail_name);
154 san_mail.next = NULL;
155
156 san_dns.node.type = MBEDTLS_X509_SAN_DNS_NAME;
157 san_dns.node.san.unstructured_name.p = (unsigned char *) san_dns_name;
158 san_dns.node.san.unstructured_name.len = strlen(san_dns_name);
159 san_dns.next = &san_mail;
160
161 san_dn.node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
162 TEST_ASSERT(mbedtls_x509_string_to_names(&ext_san_dirname,
163 san_dn_name) == 0);
164 san_dn.node.san.directory_name = *ext_san_dirname;
165 san_dn.next = &san_dns;
166
167 san_ip.node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
168 san_ip.node.san.unstructured_name.p = (unsigned char *) san_ip_name;
169 san_ip.node.san.unstructured_name.len = sizeof(san_ip_name);
170 san_ip.next = &san_dn;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100171
172 san_uri.node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100173 san_uri.node.san.unstructured_name.p = (unsigned char *) san_uri_name;
174 san_uri.node.san.unstructured_name.len = strlen(san_uri_name);
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400175 san_uri.next = &san_ip;
176
177 san_list = &san_uri;
Paul Bakker6d620502012-02-16 14:09:13 +0000178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 mbedtls_x509write_csr_init(&req);
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 mbedtls_pk_init(&key);
valerio32f2ac92023-04-20 11:59:52 +0200183 MD_OR_USE_PSA_INIT();
184
Ben Taylor440cb2a2025-03-05 09:40:08 +0000185 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL) == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 mbedtls_x509write_csr_set_md_alg(&req, md_type);
188 mbedtls_x509write_csr_set_key(&req, &key);
189 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
190 if (set_key_usage != 0) {
191 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
192 }
193 if (set_cert_type != 0) {
194 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
195 }
196 if (set_extension != 0) {
197 TEST_ASSERT(csr_set_extended_key_usage(&req, MBEDTLS_OID_SERVER_AUTH,
198 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) == 0);
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100199
200 TEST_ASSERT(mbedtls_x509write_csr_set_subject_alternative_name(&req, san_list) == 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 }
Paul Bakker8eabfc12013-08-25 10:18:25 +0200202
Ben Taylor440cb2a2025-03-05 09:40:08 +0000203 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf));
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 TEST_ASSERT(ret == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 pem_len = strlen((char *) buf);
Paul Bakker6d620502012-02-16 14:09:13 +0000207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
209 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000210 }
211
Neil Armstrong5b320382022-02-21 17:22:10 +0100212 // When using PSA crypto, RNG isn't controllable, so cert_req_check_file can't be used
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 (void) cert_req_check_file;
Neil Armstrong5b320382022-02-21 17:22:10 +0100214 buf[pem_len] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100216
Ben Taylor440cb2a2025-03-05 09:40:08 +0000217 der_len = mbedtls_x509write_csr_der(&req, buf, sizeof(buf));
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100221 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 }
Andres AGe0af9952016-09-07 11:09:44 +0100223
Neil Armstrong5b320382022-02-21 17:22:10 +0100224 // When using PSA crypto, RNG isn't controllable, result length isn't
225 // deterministic over multiple runs, removing a single byte isn't enough to
226 // go into the MBEDTLS_ERR_ASN1_BUF_TOO_SMALL error case
227 der_len /= 2;
Ben Taylor440cb2a2025-03-05 09:40:08 +0000228 ret = mbedtls_x509write_csr_der(&req, buf, (size_t) (der_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100230
Paul Bakkerbd51b262014-07-10 15:26:12 +0200231exit:
Andrzej Kurekbdb41dd2023-07-10 08:09:50 -0400232 mbedtls_asn1_free_named_data_list(&ext_san_dirname);
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 mbedtls_x509write_csr_free(&req);
234 mbedtls_pk_free(&key);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100235 MD_OR_USE_PSA_DONE();
Paul Bakker6d620502012-02-16 14:09:13 +0000236}
Paul Bakker33b43f12013-08-20 11:48:36 +0200237/* END_CASE */
Paul Bakker2397cf32013-09-08 15:58:15 +0200238
Ben Taylor90204262025-06-09 11:51:28 +0100239/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100240void x509_csr_check_opaque(char *key_file, int md_type, int key_usage,
241 int cert_type)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500242{
243 mbedtls_pk_context key;
Paul Elliott9a209b82024-10-25 12:41:28 +0100244 mbedtls_pk_init(&key);
245
Ronald Cron5425a212020-08-04 14:58:35 +0200246 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100247 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliott9a209b82024-10-25 12:41:28 +0100248
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500249 mbedtls_x509write_csr req;
Paul Elliott9a209b82024-10-25 12:41:28 +0100250 mbedtls_x509write_csr_init(&req);
251
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500252 unsigned char buf[4096];
253 int ret;
254 size_t pem_len = 0;
255 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200256 mbedtls_test_rnd_pseudo_info rnd_info;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500257
Valerio Setti569c1712023-04-19 14:53:36 +0200258 MD_OR_USE_PSA_INIT();
259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500261
Ben Taylor440cb2a2025-03-05 09:40:08 +0000262 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL) == 0);
Neil Armstrong95974972022-04-22 13:57:44 +0200263
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100264 /* Turn the PK context into an opaque one. */
265 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&key, PSA_KEY_USAGE_SIGN_HASH, &key_attr), 0);
266 TEST_EQUAL(mbedtls_pk_import_into_psa(&key, &key_attr, &key_id), 0);
267 mbedtls_pk_free(&key);
268 mbedtls_pk_init(&key);
Ben Taylor361ce2b2025-07-04 10:36:53 +0100269 TEST_EQUAL(mbedtls_pk_wrap_psa(&key, key_id), 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 mbedtls_x509write_csr_set_md_alg(&req, md_type);
272 mbedtls_x509write_csr_set_key(&req, &key);
273 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
274 if (key_usage != 0) {
275 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
276 }
277 if (cert_type != 0) {
278 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
279 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500280
Ben Taylor440cb2a2025-03-05 09:40:08 +0000281 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf) - 1);
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 TEST_ASSERT(ret == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 pem_len = strlen((char *) buf);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500286 buf[pem_len] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500288
Manuel Pégourié-Gonnardfeb03962020-08-20 09:59:33 +0200289
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500290exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 mbedtls_x509write_csr_free(&req);
292 mbedtls_pk_free(&key);
293 psa_destroy_key(key_id);
Valerio Setti569c1712023-04-19 14:53:36 +0200294 MD_OR_USE_PSA_DONE();
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500295}
296/* END_CASE */
297
Elena Uziunaite9fc5be02024-09-04 18:12:59 +0100298/* 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 +0100299void x509_crt_check(char *subject_key_file, char *subject_pwd,
300 char *subject_name, char *issuer_key_file,
301 char *issuer_pwd, char *issuer_name,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100302 data_t *serial_arg, char *not_before, char *not_after,
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 int md_type, int key_usage, int set_key_usage,
304 char *ext_key_usage,
305 int cert_type, int set_cert_type, int auth_ident,
306 int ver, char *cert_check_file, int pk_wrap, int is_ca,
Andrzej Kurek76c96622023-04-04 06:57:08 -0400307 char *cert_verify_file, int set_subjectAltNames)
Paul Bakker2397cf32013-09-08 15:58:15 +0200308{
Hanno Becker418a6222017-09-14 07:51:28 +0100309 mbedtls_pk_context subject_key, issuer_key, issuer_key_alt;
310 mbedtls_pk_context *key = &issuer_key;
311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 mbedtls_x509write_cert crt;
Andres AGe0af9952016-09-07 11:09:44 +0100313 unsigned char buf[4096];
Paul Bakker2397cf32013-09-08 15:58:15 +0200314 unsigned char check_buf[5000];
Werner Lewisacd01e52022-05-10 12:23:13 +0100315 unsigned char *p, *end;
316 unsigned char tag, sz;
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100317#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
318 mbedtls_mpi serial_mpi;
319#endif
Werner Lewisacd01e52022-05-10 12:23:13 +0100320 int ret, before_tag, after_tag;
Paul Elliott557b8d62020-11-19 09:46:56 +0000321 size_t olen = 0, pem_len = 0, buf_index = 0;
Andres AGe0af9952016-09-07 11:09:44 +0100322 int der_len = -1;
Paul Bakker2397cf32013-09-08 15:58:15 +0200323 FILE *f;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200324 mbedtls_test_rnd_pseudo_info rnd_info;
Neil Armstrong98f899c2022-03-16 17:42:42 +0100325 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100326 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100327 mbedtls_pk_type_t issuer_key_type;
Andrzej Kurek76c96622023-04-04 06:57:08 -0400328 mbedtls_x509_san_list san_ip;
329 mbedtls_x509_san_list san_dns;
330 mbedtls_x509_san_list san_uri;
331 mbedtls_x509_san_list san_mail;
332 mbedtls_x509_san_list san_dn;
333 mbedtls_asn1_named_data *ext_san_dirname = NULL;
334 const char san_ip_name[] = { 0x01, 0x02, 0x03, 0x04 };
335 const char *san_dns_name = "example.com";
336 const char *san_dn_name = "C=UK,O=Mbed TLS,CN=SubjectAltName test";
337 const char *san_mail_name = "mail@example.com";
338 const char *san_uri_name = "http://pki.example.com";
339 mbedtls_x509_san_list *san_list = NULL;
340
341 if (set_subjectAltNames) {
342 san_mail.node.type = MBEDTLS_X509_SAN_RFC822_NAME;
343 san_mail.node.san.unstructured_name.p = (unsigned char *) san_mail_name;
Andrzej Kurek13c43f62023-04-04 10:43:38 -0400344 san_mail.node.san.unstructured_name.len = strlen(san_mail_name);
Andrzej Kurek76c96622023-04-04 06:57:08 -0400345 san_mail.next = NULL;
346
347 san_dns.node.type = MBEDTLS_X509_SAN_DNS_NAME;
348 san_dns.node.san.unstructured_name.p = (unsigned char *) san_dns_name;
349 san_dns.node.san.unstructured_name.len = strlen(san_dns_name);
350 san_dns.next = &san_mail;
351
352 san_dn.node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
353 TEST_ASSERT(mbedtls_x509_string_to_names(&ext_san_dirname,
354 san_dn_name) == 0);
355 san_dn.node.san.directory_name = *ext_san_dirname;
356 san_dn.next = &san_dns;
357
358 san_ip.node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
359 san_ip.node.san.unstructured_name.p = (unsigned char *) san_ip_name;
360 san_ip.node.san.unstructured_name.len = sizeof(san_ip_name);
361 san_ip.next = &san_dn;
362
363 san_uri.node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
364 san_uri.node.san.unstructured_name.p = (unsigned char *) san_uri_name;
365 san_uri.node.san.unstructured_name.len = strlen(san_uri_name);
366 san_uri.next = &san_ip;
367
368 san_list = &san_uri;
369 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100372#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
373 mbedtls_mpi_init(&serial_mpi);
374#endif
Hanno Becker418a6222017-09-14 07:51:28 +0100375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 mbedtls_pk_init(&subject_key);
377 mbedtls_pk_init(&issuer_key);
378 mbedtls_pk_init(&issuer_key_alt);
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 mbedtls_x509write_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200380 MD_OR_USE_PSA_INIT();
Paul Bakker2397cf32013-09-08 15:58:15 +0200381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 TEST_ASSERT(mbedtls_pk_parse_keyfile(&subject_key, subject_key_file,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000383 subject_pwd) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 TEST_ASSERT(mbedtls_pk_parse_keyfile(&issuer_key, issuer_key_file,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000386 issuer_pwd) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 issuer_key_type = mbedtls_pk_get_type(&issuer_key);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100389
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100390 /* Turn the issuer PK context into an opaque one. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (pk_wrap == 2) {
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100392 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&issuer_key, PSA_KEY_USAGE_SIGN_HASH,
393 &key_attr), 0);
394 TEST_EQUAL(mbedtls_pk_import_into_psa(&issuer_key, &key_attr, &key_id), 0);
395 mbedtls_pk_free(&issuer_key);
396 mbedtls_pk_init(&issuer_key);
Ben Taylor361ce2b2025-07-04 10:36:53 +0100397 TEST_EQUAL(mbedtls_pk_wrap_psa(&issuer_key, key_id), 0);
Neil Armstrong98f899c2022-03-16 17:42:42 +0100398 }
Neil Armstrong98f899c2022-03-16 17:42:42 +0100399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (pk_wrap == 2) {
401 TEST_ASSERT(mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_OPAQUE);
402 }
Neil Armstrong98f899c2022-03-16 17:42:42 +0100403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if (ver != -1) {
405 mbedtls_x509write_crt_set_version(&crt, ver);
406 }
Hanno Becker418a6222017-09-14 07:51:28 +0100407
Valerio Settiaf4815c2023-01-26 17:43:09 +0100408 TEST_ASSERT(mbedtls_x509write_crt_set_serial_raw(&crt, serial_arg->x,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100409 serial_arg->len) == 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 TEST_ASSERT(mbedtls_x509write_crt_set_validity(&crt, not_before,
411 not_after) == 0);
412 mbedtls_x509write_crt_set_md_alg(&crt, md_type);
413 TEST_ASSERT(mbedtls_x509write_crt_set_issuer_name(&crt, issuer_name) == 0);
414 TEST_ASSERT(mbedtls_x509write_crt_set_subject_name(&crt, subject_name) == 0);
415 mbedtls_x509write_crt_set_subject_key(&crt, &subject_key);
Hanno Becker418a6222017-09-14 07:51:28 +0100416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 mbedtls_x509write_crt_set_issuer_key(&crt, key);
Paul Bakker2397cf32013-09-08 15:58:15 +0200418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (crt.version >= MBEDTLS_X509_CRT_VERSION_3) {
Darren Krahn9c134ce2021-01-13 22:04:45 -0800420 /* For the CA case, a path length of -1 means unlimited. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 TEST_ASSERT(mbedtls_x509write_crt_set_basic_constraints(&crt, is_ca,
422 (is_ca ? -1 : 0)) == 0);
423 TEST_ASSERT(mbedtls_x509write_crt_set_subject_key_identifier(&crt) == 0);
424 if (auth_ident) {
425 TEST_ASSERT(mbedtls_x509write_crt_set_authority_key_identifier(&crt) == 0);
426 }
427 if (set_key_usage != 0) {
428 TEST_ASSERT(mbedtls_x509write_crt_set_key_usage(&crt, key_usage) == 0);
429 }
430 if (set_cert_type != 0) {
431 TEST_ASSERT(mbedtls_x509write_crt_set_ns_cert_type(&crt, cert_type) == 0);
432 }
433 if (strcmp(ext_key_usage, "NULL") != 0) {
Dave Rodgmanec9f6b42022-07-27 14:34:58 +0100434 mbedtls_asn1_sequence exts[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 memset(exts, 0, sizeof(exts));
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100436
437#define SET_OID(x, oid) \
438 do { \
439 x.len = MBEDTLS_OID_SIZE(oid); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 x.p = (unsigned char *) oid; \
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100441 x.tag = MBEDTLS_ASN1_OID; \
Dave Rodgmane2b772d2022-08-11 16:04:13 +0100442 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 while (0)
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if (strcmp(ext_key_usage, "serverAuth") == 0) {
446 SET_OID(exts[0].buf, MBEDTLS_OID_SERVER_AUTH);
447 } else if (strcmp(ext_key_usage, "codeSigning,timeStamping") == 0) {
448 SET_OID(exts[0].buf, MBEDTLS_OID_CODE_SIGNING);
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100449 exts[0].next = &exts[1];
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 SET_OID(exts[1].buf, MBEDTLS_OID_TIME_STAMPING);
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000451 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 TEST_ASSERT(mbedtls_x509write_crt_set_ext_key_usage(&crt, exts) == 0);
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000453 }
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100454 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200455
Andrzej Kurek76c96622023-04-04 06:57:08 -0400456 if (set_subjectAltNames) {
457 TEST_ASSERT(mbedtls_x509write_crt_set_subject_alternative_name(&crt, san_list) == 0);
458 }
Ben Taylor440cb2a2025-03-05 09:40:08 +0000459 ret = mbedtls_x509write_crt_pem(&crt, buf, sizeof(buf));
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 TEST_ASSERT(ret == 0);
Paul Bakker2397cf32013-09-08 15:58:15 +0200461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 pem_len = strlen((char *) buf);
Paul Bakker2397cf32013-09-08 15:58:15 +0200463
Paul Elliott557b8d62020-11-19 09:46:56 +0000464 // check that the rest of the buffer remains clear
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
466 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000467 }
468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (issuer_key_type != MBEDTLS_PK_RSA) {
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100470 mbedtls_x509_crt crt_parse, trusted;
471 uint32_t flags;
Paul Bakker2397cf32013-09-08 15:58:15 +0200472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 mbedtls_x509_crt_init(&crt_parse);
474 mbedtls_x509_crt_init(&trusted);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted,
477 cert_verify_file) == 0);
478 TEST_ASSERT(mbedtls_x509_crt_parse(&crt_parse,
479 buf, sizeof(buf)) == 0);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 ret = mbedtls_x509_crt_verify(&crt_parse, &trusted, NULL, NULL, &flags,
482 NULL, NULL);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 mbedtls_x509_crt_free(&crt_parse);
485 mbedtls_x509_crt_free(&trusted);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 TEST_EQUAL(flags, 0);
488 TEST_EQUAL(ret, 0);
489 } else if (*cert_check_file != '\0') {
490 f = fopen(cert_check_file, "r");
491 TEST_ASSERT(f != NULL);
492 olen = fread(check_buf, 1, sizeof(check_buf), f);
493 fclose(f);
494 TEST_ASSERT(olen < sizeof(check_buf));
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 TEST_EQUAL(olen, pem_len);
497 TEST_ASSERT(olen >= pem_len - 1);
498 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100499 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200500
Ben Taylor440cb2a2025-03-05 09:40:08 +0000501 der_len = mbedtls_x509write_crt_der(&crt, buf, sizeof(buf));
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100505 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 }
Andres AGe0af9952016-09-07 11:09:44 +0100507
Werner Lewisacd01e52022-05-10 12:23:13 +0100508 // Not testing against file, check date format
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if (*cert_check_file == '\0') {
Werner Lewisacd01e52022-05-10 12:23:13 +0100510 // UTC tag if before 2050, 2 digits less for year
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 if (not_before[0] == '2' && (not_before[1] > '0' || not_before[2] > '4')) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100512 before_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 } else {
Werner Lewisacd01e52022-05-10 12:23:13 +0100514 before_tag = MBEDTLS_ASN1_UTC_TIME;
515 not_before += 2;
516 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if (not_after[0] == '2' && (not_after[1] > '0' || not_after[2] > '4')) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100518 after_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 } else {
Werner Lewisacd01e52022-05-10 12:23:13 +0100520 after_tag = MBEDTLS_ASN1_UTC_TIME;
521 not_after += 2;
522 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 end = buf + sizeof(buf);
524 for (p = end - der_len; p < end;) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100525 tag = *p++;
526 sz = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 if (tag == MBEDTLS_ASN1_UTC_TIME || tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100528 // Check correct tag and time written
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 TEST_ASSERT(before_tag == tag);
530 TEST_ASSERT(memcmp(p, not_before, sz - 1) == 0);
Werner Lewisacd01e52022-05-10 12:23:13 +0100531 p += sz;
532 tag = *p++;
533 sz = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 TEST_ASSERT(after_tag == tag);
535 TEST_ASSERT(memcmp(p, not_after, sz - 1) == 0);
Werner Lewisacd01e52022-05-10 12:23:13 +0100536 break;
537 }
538 // Increment if long form ASN1 length
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 if (sz & 0x80) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100540 p += sz & 0x0F;
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 }
542 if (tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100543 p += sz;
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 }
Werner Lewisacd01e52022-05-10 12:23:13 +0100545 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 TEST_ASSERT(p < end);
Werner Lewisacd01e52022-05-10 12:23:13 +0100547 }
548
Neil Armstronge6ed23c2022-04-22 09:44:04 +0200549 // When using PSA crypto, RNG isn't controllable, result length isn't
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100550 // deterministic over multiple runs, removing a single byte isn't enough to
551 // go into the MBEDTLS_ERR_ASN1_BUF_TOO_SMALL error case
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 if (issuer_key_type != MBEDTLS_PK_RSA) {
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100553 der_len /= 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 } else
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 der_len -= 1;
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100556
Ben Taylor440cb2a2025-03-05 09:40:08 +0000557 ret = mbedtls_x509write_crt_der(&crt, buf, (size_t) (der_len));
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100559
Paul Bakkerbd51b262014-07-10 15:26:12 +0200560exit:
Andrzej Kurek5da1d752023-04-05 08:30:59 -0400561 mbedtls_asn1_free_named_data_list(&ext_san_dirname);
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 mbedtls_x509write_crt_free(&crt);
563 mbedtls_pk_free(&issuer_key_alt);
564 mbedtls_pk_free(&subject_key);
565 mbedtls_pk_free(&issuer_key);
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100566#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
567 mbedtls_mpi_free(&serial_mpi);
568#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 psa_destroy_key(key_id);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100570 MD_OR_USE_PSA_DONE();
Paul Bakker2397cf32013-09-08 15:58:15 +0200571}
572/* END_CASE */
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200573
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100574/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_WRITE_C */
575void x509_set_serial_check()
576{
577 mbedtls_x509write_cert ctx;
578 uint8_t invalid_serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN + 1];
579
David Horstmanne04a97a2023-12-08 18:27:48 +0000580#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
581 mbedtls_mpi serial_mpi;
582 mbedtls_mpi_init(&serial_mpi);
583#endif
584
Valerio Setti569c1712023-04-19 14:53:36 +0200585 USE_PSA_INIT();
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100586 memset(invalid_serial, 0x01, sizeof(invalid_serial));
587
Valerio Settiaf4815c2023-01-26 17:43:09 +0100588 TEST_EQUAL(mbedtls_x509write_crt_set_serial_raw(&ctx, invalid_serial,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100589 sizeof(invalid_serial)),
590 MBEDTLS_ERR_X509_BAD_INPUT_DATA);
591
Valerio Settia87f8392023-01-26 18:00:50 +0100592exit:
593#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
594 mbedtls_mpi_free(&serial_mpi);
595#else
596 ;
597#endif
Valerio Setti569c1712023-04-19 14:53:36 +0200598 USE_PSA_DONE();
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100599}
600/* END_CASE */
601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C */
Gilles Peskinec94500b2023-09-21 18:01:05 +0200603void mbedtls_x509_string_to_names(char *name, char *parsed_name,
604 int result, int may_fail)
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200605{
606 int ret;
607 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_asn1_named_data *names = NULL;
Gilles Peskine21e46b32023-10-17 16:35:20 +0200609 mbedtls_x509_name parsed;
610 memset(&parsed, 0, sizeof(parsed));
611 mbedtls_x509_name *parsed_cur = NULL;
612 mbedtls_x509_name *parsed_prv = NULL;
Gilles Peskinef2574202023-10-18 17:39:48 +0200613 unsigned char buf[1024] = { 0 };
614 unsigned char out[1024] = { 0 };
Gilles Peskine21e46b32023-10-17 16:35:20 +0200615 unsigned char *c = buf + sizeof(buf);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200616
Valerio Setti569c1712023-04-19 14:53:36 +0200617 USE_PSA_INIT();
618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 ret = mbedtls_x509_string_to_names(&names, name);
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200620 TEST_EQUAL(ret, result);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 if (ret != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200623 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 }
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 ret = mbedtls_x509_write_names(&c, buf, names);
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200627 TEST_LE_S(1, ret);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200628
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200629 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
Gilles Peskineaa01a032023-09-21 15:57:30 +0200630 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
Gilles Peskinec94500b2023-09-21 18:01:05 +0200631 ret = mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed);
632 if ((may_fail & MAY_FAIL_GET_NAME) && ret < 0) {
633 /* Validation inconsistency between mbedtls_x509_string_to_names() and
634 * mbedtls_x509_get_name(). Accept it for now. */
635 goto exit;
636 }
637 TEST_EQUAL(ret, 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 ret = mbedtls_x509_dn_gets((char *) out, sizeof(out), &parsed);
Gilles Peskinec94500b2023-09-21 18:01:05 +0200640 if ((may_fail & MAY_FAIL_DN_GETS) && ret < 0) {
641 /* Validation inconsistency between mbedtls_x509_string_to_names() and
642 * mbedtls_x509_dn_gets(). Accept it for now. */
643 goto exit;
644 }
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200645 TEST_LE_S(1, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 TEST_ASSERT(strcmp((char *) out, parsed_name) == 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200647
Manuel Pégourié-Gonnardbda3ab92025-05-05 18:25:26 +0200648 /* Check that calling a 2nd time with the same param (now non-NULL)
649 * returns an error as expected. */
650 ret = mbedtls_x509_string_to_names(&names, name);
651 TEST_EQUAL(ret, MBEDTLS_ERR_X509_BAD_INPUT_DATA);
652
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200653exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 mbedtls_asn1_free_named_data_list(&names);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200655
656 parsed_cur = parsed.next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 while (parsed_cur != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200658 parsed_prv = parsed_cur;
659 parsed_cur = parsed_cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 mbedtls_free(parsed_prv);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200661 }
Valerio Setti569c1712023-04-19 14:53:36 +0200662 USE_PSA_DONE();
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200663}
664/* END_CASE */
Jonathan Winzig2bd2b782024-01-09 15:19:42 +0100665
Jonathan Winzig315c3ca2024-01-09 18:31:11 +0100666/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_WRITE_C */
Jonathan Winzig6c9779f2024-01-09 17:47:10 +0100667void x509_set_extension_length_check()
Jonathan Winzig2bd2b782024-01-09 15:19:42 +0100668{
669 int ret = 0;
670
671 mbedtls_x509write_csr ctx;
672 mbedtls_x509write_csr_init(&ctx);
673
674 unsigned char buf[EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH] = { 0 };
675 unsigned char *p = buf + sizeof(buf);
676
677 ret = mbedtls_x509_set_extension(&(ctx.MBEDTLS_PRIVATE(extensions)),
678 MBEDTLS_OID_EXTENDED_KEY_USAGE,
679 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
680 0,
681 p,
Jonathan Winzig6c9779f2024-01-09 17:47:10 +0100682 SIZE_MAX);
683 TEST_ASSERT(MBEDTLS_ERR_X509_BAD_INPUT_DATA == ret);
Jonathan Winzig2bd2b782024-01-09 15:19:42 +0100684}
685/* END_CASE */
Sam Berry2bb3f4d2024-07-19 15:14:27 +0100686
Gilles Peskineb8288202025-05-12 21:07:47 +0200687/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C */
Sam Berry2bb3f4d2024-07-19 15:14:27 +0100688void oid_from_numeric_string(char *oid_str, int error_ret,
689 data_t *exp_oid_buf)
690{
691 mbedtls_asn1_buf oid = { 0, 0, NULL };
692 mbedtls_asn1_buf exp_oid = { 0, 0, NULL };
693 int ret;
694
695 exp_oid.tag = MBEDTLS_ASN1_OID;
696 exp_oid.p = exp_oid_buf->x;
697 exp_oid.len = exp_oid_buf->len;
698
699 ret = mbedtls_oid_from_numeric_string(&oid, oid_str, strlen(oid_str));
700
701 if (error_ret == 0) {
702 TEST_EQUAL(oid.len, exp_oid.len);
703 TEST_ASSERT(memcmp(oid.p, exp_oid.p, oid.len) == 0);
704 mbedtls_free(oid.p);
705 oid.p = NULL;
706 oid.len = 0;
707 } else {
708 TEST_EQUAL(ret, error_ret);
709 }
710}
711/* END_CASE */