blob: 3d84c729fea45414dba864ee3ca9bdf8f4fb627f [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"
7#include "mbedtls/oid.h"
Hanno Becker418a6222017-09-14 07:51:28 +01008#include "mbedtls/rsa.h"
Valerio Setti48e8fc72022-10-19 15:14:29 +02009#include "mbedtls/asn1write.h"
Valerio Setti178b5bd2023-02-13 10:04:28 +010010#include "mbedtls/pk.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010011#include "mbedtls/psa_util.h"
Manuel Pégourié-Gonnardfeb03962020-08-20 09:59:33 +020012
Hanno Becker418a6222017-09-14 07:51:28 +010013#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010014int mbedtls_rsa_decrypt_func(void *ctx, size_t *olen,
15 const unsigned char *input, unsigned char *output,
16 size_t output_max_len)
Hanno Becker418a6222017-09-14 07:51:28 +010017{
Gilles Peskine449bd832023-01-11 14:50:10 +010018 return mbedtls_rsa_pkcs1_decrypt((mbedtls_rsa_context *) ctx, NULL, NULL,
19 olen, input, output, output_max_len);
Hanno Becker418a6222017-09-14 07:51:28 +010020}
Gilles Peskine449bd832023-01-11 14:50:10 +010021int mbedtls_rsa_sign_func(void *ctx,
22 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
23 mbedtls_md_type_t md_alg, unsigned int hashlen,
24 const unsigned char *hash, unsigned char *sig)
Hanno Becker418a6222017-09-14 07:51:28 +010025{
Gilles Peskine449bd832023-01-11 14:50:10 +010026 return mbedtls_rsa_pkcs1_sign((mbedtls_rsa_context *) ctx, f_rng, p_rng,
27 md_alg, hashlen, hash, sig);
Hanno Becker418a6222017-09-14 07:51:28 +010028}
Gilles Peskine449bd832023-01-11 14:50:10 +010029size_t mbedtls_rsa_key_len_func(void *ctx)
Hanno Becker418a6222017-09-14 07:51:28 +010030{
Gilles Peskine449bd832023-01-11 14:50:10 +010031 return ((const mbedtls_rsa_context *) ctx)->len;
Hanno Becker418a6222017-09-14 07:51:28 +010032}
33#endif /* MBEDTLS_RSA_C */
34
Gilles Peskinef4e672e2020-01-31 14:22:10 +010035#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
36 defined(MBEDTLS_PEM_WRITE_C) && defined(MBEDTLS_X509_CSR_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010037static int x509_crt_verifycsr(const unsigned char *buf, size_t buflen)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050038{
Przemek Stekiel54a54462022-08-01 13:59:12 +020039 unsigned char hash[PSA_HASH_MAX_SIZE];
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050040 mbedtls_x509_csr csr;
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010041 int ret = 0;
42
Gilles Peskine449bd832023-01-11 14:50:10 +010043 mbedtls_x509_csr_init(&csr);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050044
Gilles Peskine449bd832023-01-11 14:50:10 +010045 if (mbedtls_x509_csr_parse(&csr, buf, buflen) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010046 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
47 goto cleanup;
48 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050049
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020050 psa_algorithm_t psa_alg = mbedtls_md_psa_alg_from_type(csr.sig_md);
Przemek Stekiel54a54462022-08-01 13:59:12 +020051 size_t hash_size = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010052 psa_status_t status = psa_hash_compute(psa_alg, csr.cri.p, csr.cri.len,
53 hash, PSA_HASH_MAX_SIZE, &hash_size);
Przemek Stekiel54a54462022-08-01 13:59:12 +020054
Gilles Peskine449bd832023-01-11 14:50:10 +010055 if (status != PSA_SUCCESS) {
Andrzej Kurek4b114072018-11-19 18:04:01 -050056 /* Note: this can't happen except after an internal error */
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010057 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
58 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050059 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050060
Gilles Peskine449bd832023-01-11 14:50:10 +010061 if (mbedtls_pk_verify_ext(csr.sig_pk, csr.sig_opts, &csr.pk,
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +020062 csr.sig_md, hash, mbedtls_md_get_size_from_type(csr.sig_md),
Gilles Peskine449bd832023-01-11 14:50:10 +010063 csr.sig.p, csr.sig.len) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010064 ret = MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
65 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050066 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050067
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010068cleanup:
69
Gilles Peskine449bd832023-01-11 14:50:10 +010070 mbedtls_x509_csr_free(&csr);
71 return ret;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050072}
Gilles Peskinef4e672e2020-01-31 14:22:10 +010073#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_PEM_WRITE_C && MBEDTLS_X509_CSR_WRITE_C */
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050074
Valerio Setti48e8fc72022-10-19 15:14:29 +020075#if defined(MBEDTLS_X509_CSR_WRITE_C)
76
77/*
78 * The size of this temporary buffer is given by the sequence of functions
79 * called hereinafter:
80 * - mbedtls_asn1_write_oid()
81 * - 8 bytes for MBEDTLS_OID_EXTENDED_KEY_USAGE raw value
82 * - 1 byte for MBEDTLS_OID_EXTENDED_KEY_USAGE length
83 * - 1 byte for MBEDTLS_ASN1_OID tag
84 * - mbedtls_asn1_write_len()
85 * - 1 byte since we're dealing with sizes which are less than 0x80
86 * - mbedtls_asn1_write_tag()
87 * - 1 byte
88 *
89 * This length is fine as long as this function is called using the
90 * MBEDTLS_OID_SERVER_AUTH OID. If this is changed in the future, then this
91 * buffer's length should be adjusted accordingly.
92 * Unfortunately there's no predefined max size for OIDs which can be used
93 * to set an overall upper boundary which is always guaranteed.
94 */
95#define EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH 12
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097static int csr_set_extended_key_usage(mbedtls_x509write_csr *ctx,
98 const char *oid, size_t oid_len)
Valerio Setti48e8fc72022-10-19 15:14:29 +020099{
100 unsigned char buf[EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH] = { 0 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 unsigned char *p = buf + sizeof(buf);
Valerio Setti48e8fc72022-10-19 15:14:29 +0200102 int ret;
103 size_t len = 0;
104
105 /*
106 * Following functions fail anyway if the temporary buffer is not large,
107 * but we set an extra check here to emphasize a possible source of errors
108 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 if (oid_len > EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH) {
Valerio Setti48e8fc72022-10-19 15:14:29 +0200110 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
111 }
112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(&p, buf, oid, oid_len));
114 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, ret));
115 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, buf,
116 MBEDTLS_ASN1_CONSTRUCTED |
117 MBEDTLS_ASN1_SEQUENCE));
Valerio Setti48e8fc72022-10-19 15:14:29 +0200118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 ret = mbedtls_x509write_csr_set_extension(ctx,
120 MBEDTLS_OID_EXTENDED_KEY_USAGE,
121 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
122 0,
123 p,
124 len);
Valerio Setti48e8fc72022-10-19 15:14:29 +0200125
126 return ret;
127}
128#endif /* MBEDTLS_X509_CSR_WRITE_C */
Gilles Peskinec94500b2023-09-21 18:01:05 +0200129
130/* Due to inconsistencies in the input size limits applied by different
131 * library functions, some write-parse tests may fail. */
132#define MAY_FAIL_GET_NAME 0x0001
133#define MAY_FAIL_DN_GETS 0x0002
134
Paul Bakker33b43f12013-08-20 11:48:36 +0200135/* END_HEADER */
Paul Bakker6d620502012-02-16 14:09:13 +0000136
Paul Bakker33b43f12013-08-20 11:48:36 +0200137/* BEGIN_DEPENDENCIES
Valerio Setti3580f442023-07-27 10:19:53 +0200138 * depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_PARSE_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200139 * END_DEPENDENCIES
140 */
Paul Bakker6d620502012-02-16 14:09:13 +0000141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100143void x509_csr_check(char *key_file, char *cert_req_check_file, int md_type,
144 int key_usage, int set_key_usage, int cert_type,
145 int set_cert_type, int set_extension)
Paul Bakker6d620502012-02-16 14:09:13 +0000146{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_pk_context key;
148 mbedtls_x509write_csr req;
Andres AGe0af9952016-09-07 11:09:44 +0100149 unsigned char buf[4096];
Paul Bakker6d620502012-02-16 14:09:13 +0000150 int ret;
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100151#if !defined(MBEDTLS_USE_PSA_CRYPTO)
152 unsigned char check_buf[4000];
Paul Bakker6d620502012-02-16 14:09:13 +0000153 FILE *f;
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100154 size_t olen = 0;
155#endif /* !MBEDTLS_USE_PSA_CRYPTO */
156 size_t pem_len = 0, buf_index;
157 int der_len = -1;
Paul Bakker3a8cb6f2013-12-30 20:41:54 +0100158 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200159 mbedtls_test_rnd_pseudo_info rnd_info;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100160 mbedtls_x509_san_list san_ip;
161 mbedtls_x509_san_list san_dns;
162 mbedtls_x509_san_list san_uri;
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400163 mbedtls_x509_san_list san_mail;
164 mbedtls_x509_san_list san_dn;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100165 mbedtls_x509_san_list *san_list = NULL;
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400166 mbedtls_asn1_named_data *ext_san_dirname = NULL;
167
168 const char san_ip_name[] = { 0x7f, 0x00, 0x00, 0x01 }; // 127.0.0.1
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100169 const char *san_dns_name = "example.com";
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400170 const char *san_dn_name = "C=UK,O=Mbed TLS,CN=Mbed TLS directoryName SAN";
171 const char *san_mail_name = "mail@example.com";
172 const char *san_uri_name = "http://pki.example.com";
173
174 san_mail.node.type = MBEDTLS_X509_SAN_RFC822_NAME;
175 san_mail.node.san.unstructured_name.p = (unsigned char *) san_mail_name;
176 san_mail.node.san.unstructured_name.len = strlen(san_mail_name);
177 san_mail.next = NULL;
178
179 san_dns.node.type = MBEDTLS_X509_SAN_DNS_NAME;
180 san_dns.node.san.unstructured_name.p = (unsigned char *) san_dns_name;
181 san_dns.node.san.unstructured_name.len = strlen(san_dns_name);
182 san_dns.next = &san_mail;
183
184 san_dn.node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
185 TEST_ASSERT(mbedtls_x509_string_to_names(&ext_san_dirname,
186 san_dn_name) == 0);
187 san_dn.node.san.directory_name = *ext_san_dirname;
188 san_dn.next = &san_dns;
189
190 san_ip.node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
191 san_ip.node.san.unstructured_name.p = (unsigned char *) san_ip_name;
192 san_ip.node.san.unstructured_name.len = sizeof(san_ip_name);
193 san_ip.next = &san_dn;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100194
195 san_uri.node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100196 san_uri.node.san.unstructured_name.p = (unsigned char *) san_uri_name;
197 san_uri.node.san.unstructured_name.len = strlen(san_uri_name);
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400198 san_uri.next = &san_ip;
199
200 san_list = &san_uri;
Paul Bakker6d620502012-02-16 14:09:13 +0000201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 mbedtls_x509write_csr_init(&req);
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 mbedtls_pk_init(&key);
valerio32f2ac92023-04-20 11:59:52 +0200206 MD_OR_USE_PSA_INIT();
207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL,
209 mbedtls_test_rnd_std_rand, NULL) == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 mbedtls_x509write_csr_set_md_alg(&req, md_type);
212 mbedtls_x509write_csr_set_key(&req, &key);
213 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
214 if (set_key_usage != 0) {
215 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
216 }
217 if (set_cert_type != 0) {
218 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
219 }
220 if (set_extension != 0) {
221 TEST_ASSERT(csr_set_extended_key_usage(&req, MBEDTLS_OID_SERVER_AUTH,
222 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) == 0);
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100223
224 TEST_ASSERT(mbedtls_x509write_csr_set_subject_alternative_name(&req, san_list) == 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 }
Paul Bakker8eabfc12013-08-25 10:18:25 +0200226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf),
228 mbedtls_test_rnd_pseudo_rand, &rnd_info);
229 TEST_ASSERT(ret == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 pem_len = strlen((char *) buf);
Paul Bakker6d620502012-02-16 14:09:13 +0000232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
234 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000235 }
236
Neil Armstrong5b320382022-02-21 17:22:10 +0100237#if defined(MBEDTLS_USE_PSA_CRYPTO)
238 // When using PSA crypto, RNG isn't controllable, so cert_req_check_file can't be used
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 (void) cert_req_check_file;
Neil Armstrong5b320382022-02-21 17:22:10 +0100240 buf[pem_len] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0);
Neil Armstrong5b320382022-02-21 17:22:10 +0100242#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 f = fopen(cert_req_check_file, "r");
244 TEST_ASSERT(f != NULL);
245 olen = fread(check_buf, 1, sizeof(check_buf), f);
246 fclose(f);
Paul Bakker6d620502012-02-16 14:09:13 +0000247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 TEST_ASSERT(olen >= pem_len - 1);
249 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100250#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 der_len = mbedtls_x509write_csr_der(&req, buf, sizeof(buf),
253 mbedtls_test_rnd_pseudo_rand,
254 &rnd_info);
255 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100258 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 }
Andres AGe0af9952016-09-07 11:09:44 +0100260
Neil Armstrong5b320382022-02-21 17:22:10 +0100261#if defined(MBEDTLS_USE_PSA_CRYPTO)
262 // When using PSA crypto, RNG isn't controllable, result length isn't
263 // deterministic over multiple runs, removing a single byte isn't enough to
264 // go into the MBEDTLS_ERR_ASN1_BUF_TOO_SMALL error case
265 der_len /= 2;
266#else
267 der_len -= 1;
268#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 ret = mbedtls_x509write_csr_der(&req, buf, (size_t) (der_len),
270 mbedtls_test_rnd_pseudo_rand, &rnd_info);
271 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100272
Paul Bakkerbd51b262014-07-10 15:26:12 +0200273exit:
Andrzej Kurekbdb41dd2023-07-10 08:09:50 -0400274 mbedtls_asn1_free_named_data_list(&ext_san_dirname);
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 mbedtls_x509write_csr_free(&req);
276 mbedtls_pk_free(&key);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100277 MD_OR_USE_PSA_DONE();
Paul Bakker6d620502012-02-16 14:09:13 +0000278}
Paul Bakker33b43f12013-08-20 11:48:36 +0200279/* END_CASE */
Paul Bakker2397cf32013-09-08 15:58:15 +0200280
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500281/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C:MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100282void x509_csr_check_opaque(char *key_file, int md_type, int key_usage,
283 int cert_type)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500284{
285 mbedtls_pk_context key;
Ronald Cron5425a212020-08-04 14:58:35 +0200286 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong95974972022-04-22 13:57:44 +0200287 psa_algorithm_t md_alg_psa, alg_psa;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500288 mbedtls_x509write_csr req;
289 unsigned char buf[4096];
290 int ret;
291 size_t pem_len = 0;
292 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200293 mbedtls_test_rnd_pseudo_info rnd_info;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500294
valerio32f2ac92023-04-20 11:59:52 +0200295 mbedtls_x509write_csr_init(&req);
Valerio Setti569c1712023-04-19 14:53:36 +0200296 MD_OR_USE_PSA_INIT();
297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500299
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200300 md_alg_psa = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 TEST_ASSERT(md_alg_psa != MBEDTLS_MD_NONE);
Andrzej Kurek967cfd12018-11-20 02:53:17 -0500302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 mbedtls_pk_init(&key);
304 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL,
305 mbedtls_test_rnd_std_rand, NULL) == 0);
Neil Armstrong95974972022-04-22 13:57:44 +0200306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
308 alg_psa = PSA_ALG_ECDSA(md_alg_psa);
309 } else if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
310 alg_psa = PSA_ALG_RSA_PKCS1V15_SIGN(md_alg_psa);
311 } else {
312 TEST_ASSUME(!"PK key type not supported in this configuration");
313 }
Neil Armstrong95974972022-04-22 13:57:44 +0200314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 TEST_ASSERT(mbedtls_pk_wrap_as_opaque(&key, &key_id, alg_psa,
316 PSA_KEY_USAGE_SIGN_HASH,
317 PSA_ALG_NONE) == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 mbedtls_x509write_csr_set_md_alg(&req, md_type);
320 mbedtls_x509write_csr_set_key(&req, &key);
321 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
322 if (key_usage != 0) {
323 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
324 }
325 if (cert_type != 0) {
326 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
327 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf) - 1,
330 mbedtls_test_rnd_pseudo_rand, &rnd_info);
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 TEST_ASSERT(ret == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 pem_len = strlen((char *) buf);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500335 buf[pem_len] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500337
Manuel Pégourié-Gonnardfeb03962020-08-20 09:59:33 +0200338
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500339exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 mbedtls_x509write_csr_free(&req);
341 mbedtls_pk_free(&key);
342 psa_destroy_key(key_id);
Valerio Setti569c1712023-04-19 14:53:36 +0200343 MD_OR_USE_PSA_DONE();
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500344}
345/* END_CASE */
346
Manuel Pégourié-Gonnarda9464892023-03-17 12:08:50 +0100347/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CRT_WRITE_C:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_MD_CAN_SHA1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348void x509_crt_check(char *subject_key_file, char *subject_pwd,
349 char *subject_name, char *issuer_key_file,
350 char *issuer_pwd, char *issuer_name,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100351 data_t *serial_arg, char *not_before, char *not_after,
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 int md_type, int key_usage, int set_key_usage,
353 char *ext_key_usage,
354 int cert_type, int set_cert_type, int auth_ident,
355 int ver, char *cert_check_file, int pk_wrap, int is_ca,
Andrzej Kurek76c96622023-04-04 06:57:08 -0400356 char *cert_verify_file, int set_subjectAltNames)
Paul Bakker2397cf32013-09-08 15:58:15 +0200357{
Hanno Becker418a6222017-09-14 07:51:28 +0100358 mbedtls_pk_context subject_key, issuer_key, issuer_key_alt;
359 mbedtls_pk_context *key = &issuer_key;
360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 mbedtls_x509write_cert crt;
Andres AGe0af9952016-09-07 11:09:44 +0100362 unsigned char buf[4096];
Paul Bakker2397cf32013-09-08 15:58:15 +0200363 unsigned char check_buf[5000];
Werner Lewisacd01e52022-05-10 12:23:13 +0100364 unsigned char *p, *end;
365 unsigned char tag, sz;
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100366#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
367 mbedtls_mpi serial_mpi;
368#endif
Werner Lewisacd01e52022-05-10 12:23:13 +0100369 int ret, before_tag, after_tag;
Paul Elliott557b8d62020-11-19 09:46:56 +0000370 size_t olen = 0, pem_len = 0, buf_index = 0;
Andres AGe0af9952016-09-07 11:09:44 +0100371 int der_len = -1;
Paul Bakker2397cf32013-09-08 15:58:15 +0200372 FILE *f;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200373 mbedtls_test_rnd_pseudo_info rnd_info;
Neil Armstrong98f899c2022-03-16 17:42:42 +0100374#if defined(MBEDTLS_USE_PSA_CRYPTO)
375 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
376#endif
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100377 mbedtls_pk_type_t issuer_key_type;
Andrzej Kurek76c96622023-04-04 06:57:08 -0400378 mbedtls_x509_san_list san_ip;
379 mbedtls_x509_san_list san_dns;
380 mbedtls_x509_san_list san_uri;
381 mbedtls_x509_san_list san_mail;
382 mbedtls_x509_san_list san_dn;
383 mbedtls_asn1_named_data *ext_san_dirname = NULL;
384 const char san_ip_name[] = { 0x01, 0x02, 0x03, 0x04 };
385 const char *san_dns_name = "example.com";
386 const char *san_dn_name = "C=UK,O=Mbed TLS,CN=SubjectAltName test";
387 const char *san_mail_name = "mail@example.com";
388 const char *san_uri_name = "http://pki.example.com";
389 mbedtls_x509_san_list *san_list = NULL;
390
391 if (set_subjectAltNames) {
392 san_mail.node.type = MBEDTLS_X509_SAN_RFC822_NAME;
393 san_mail.node.san.unstructured_name.p = (unsigned char *) san_mail_name;
Andrzej Kurek13c43f62023-04-04 10:43:38 -0400394 san_mail.node.san.unstructured_name.len = strlen(san_mail_name);
Andrzej Kurek76c96622023-04-04 06:57:08 -0400395 san_mail.next = NULL;
396
397 san_dns.node.type = MBEDTLS_X509_SAN_DNS_NAME;
398 san_dns.node.san.unstructured_name.p = (unsigned char *) san_dns_name;
399 san_dns.node.san.unstructured_name.len = strlen(san_dns_name);
400 san_dns.next = &san_mail;
401
402 san_dn.node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
403 TEST_ASSERT(mbedtls_x509_string_to_names(&ext_san_dirname,
404 san_dn_name) == 0);
405 san_dn.node.san.directory_name = *ext_san_dirname;
406 san_dn.next = &san_dns;
407
408 san_ip.node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
409 san_ip.node.san.unstructured_name.p = (unsigned char *) san_ip_name;
410 san_ip.node.san.unstructured_name.len = sizeof(san_ip_name);
411 san_ip.next = &san_dn;
412
413 san_uri.node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
414 san_uri.node.san.unstructured_name.p = (unsigned char *) san_uri_name;
415 san_uri.node.san.unstructured_name.len = strlen(san_uri_name);
416 san_uri.next = &san_ip;
417
418 san_list = &san_uri;
419 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100422#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
423 mbedtls_mpi_init(&serial_mpi);
424#endif
Hanno Becker418a6222017-09-14 07:51:28 +0100425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 mbedtls_pk_init(&subject_key);
427 mbedtls_pk_init(&issuer_key);
428 mbedtls_pk_init(&issuer_key_alt);
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 mbedtls_x509write_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200430 MD_OR_USE_PSA_INIT();
Paul Bakker2397cf32013-09-08 15:58:15 +0200431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 TEST_ASSERT(mbedtls_pk_parse_keyfile(&subject_key, subject_key_file,
433 subject_pwd, mbedtls_test_rnd_std_rand, NULL) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 TEST_ASSERT(mbedtls_pk_parse_keyfile(&issuer_key, issuer_key_file,
436 issuer_pwd, mbedtls_test_rnd_std_rand, NULL) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 issuer_key_type = mbedtls_pk_get_type(&issuer_key);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100439
Dave Rodgmandc3b1542023-01-20 11:39:00 +0000440#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Hanno Becker418a6222017-09-14 07:51:28 +0100441 /* For RSA PK contexts, create a copy as an alternative RSA context. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if (pk_wrap == 1 && issuer_key_type == MBEDTLS_PK_RSA) {
443 TEST_ASSERT(mbedtls_pk_setup_rsa_alt(&issuer_key_alt,
444 mbedtls_pk_rsa(issuer_key),
445 mbedtls_rsa_decrypt_func,
446 mbedtls_rsa_sign_func,
447 mbedtls_rsa_key_len_func) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100448
449 key = &issuer_key_alt;
450 }
Manuel Pégourié-Gonnard147b28e2018-03-12 15:26:59 +0100451#endif
Hanno Becker418a6222017-09-14 07:51:28 +0100452
Neil Armstrong98f899c2022-03-16 17:42:42 +0100453#if defined(MBEDTLS_USE_PSA_CRYPTO)
454 /* For Opaque PK contexts, wrap key as an Opaque RSA context. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 if (pk_wrap == 2) {
Neil Armstrong95974972022-04-22 13:57:44 +0200456 psa_algorithm_t alg_psa, md_alg_psa;
Neil Armstrong98f899c2022-03-16 17:42:42 +0100457
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200458 md_alg_psa = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 TEST_ASSERT(md_alg_psa != MBEDTLS_MD_NONE);
Neil Armstrong95974972022-04-22 13:57:44 +0200460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_ECKEY) {
462 alg_psa = PSA_ALG_ECDSA(md_alg_psa);
463 } else if (mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_RSA) {
464 alg_psa = PSA_ALG_RSA_PKCS1V15_SIGN(md_alg_psa);
465 } else {
466 TEST_ASSUME(!"PK key type not supported in this configuration");
467 }
Neil Armstrong95974972022-04-22 13:57:44 +0200468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 TEST_ASSERT(mbedtls_pk_wrap_as_opaque(&issuer_key, &key_id, alg_psa,
470 PSA_KEY_USAGE_SIGN_HASH,
471 PSA_ALG_NONE) == 0);
Neil Armstrong98f899c2022-03-16 17:42:42 +0100472 }
473#endif /* MBEDTLS_USE_PSA_CRYPTO */
474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 if (pk_wrap == 2) {
476 TEST_ASSERT(mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_OPAQUE);
477 }
Neil Armstrong98f899c2022-03-16 17:42:42 +0100478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 if (ver != -1) {
480 mbedtls_x509write_crt_set_version(&crt, ver);
481 }
Hanno Becker418a6222017-09-14 07:51:28 +0100482
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100483#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100484 TEST_ASSERT(mbedtls_mpi_read_binary(&serial_mpi, serial_arg->x,
485 serial_arg->len) == 0);
486 TEST_ASSERT(mbedtls_x509write_crt_set_serial(&crt, &serial_mpi) == 0);
Valerio Settida0afcc2023-01-05 16:46:59 +0100487#else
Valerio Settiaf4815c2023-01-26 17:43:09 +0100488 TEST_ASSERT(mbedtls_x509write_crt_set_serial_raw(&crt, serial_arg->x,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100489 serial_arg->len) == 0);
Valerio Settida0afcc2023-01-05 16:46:59 +0100490#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 TEST_ASSERT(mbedtls_x509write_crt_set_validity(&crt, not_before,
492 not_after) == 0);
493 mbedtls_x509write_crt_set_md_alg(&crt, md_type);
494 TEST_ASSERT(mbedtls_x509write_crt_set_issuer_name(&crt, issuer_name) == 0);
495 TEST_ASSERT(mbedtls_x509write_crt_set_subject_name(&crt, subject_name) == 0);
496 mbedtls_x509write_crt_set_subject_key(&crt, &subject_key);
Hanno Becker418a6222017-09-14 07:51:28 +0100497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 mbedtls_x509write_crt_set_issuer_key(&crt, key);
Paul Bakker2397cf32013-09-08 15:58:15 +0200499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (crt.version >= MBEDTLS_X509_CRT_VERSION_3) {
Darren Krahn9c134ce2021-01-13 22:04:45 -0800501 /* For the CA case, a path length of -1 means unlimited. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 TEST_ASSERT(mbedtls_x509write_crt_set_basic_constraints(&crt, is_ca,
503 (is_ca ? -1 : 0)) == 0);
504 TEST_ASSERT(mbedtls_x509write_crt_set_subject_key_identifier(&crt) == 0);
505 if (auth_ident) {
506 TEST_ASSERT(mbedtls_x509write_crt_set_authority_key_identifier(&crt) == 0);
507 }
508 if (set_key_usage != 0) {
509 TEST_ASSERT(mbedtls_x509write_crt_set_key_usage(&crt, key_usage) == 0);
510 }
511 if (set_cert_type != 0) {
512 TEST_ASSERT(mbedtls_x509write_crt_set_ns_cert_type(&crt, cert_type) == 0);
513 }
514 if (strcmp(ext_key_usage, "NULL") != 0) {
Dave Rodgmanec9f6b42022-07-27 14:34:58 +0100515 mbedtls_asn1_sequence exts[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 memset(exts, 0, sizeof(exts));
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100517
518#define SET_OID(x, oid) \
519 do { \
520 x.len = MBEDTLS_OID_SIZE(oid); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 x.p = (unsigned char *) oid; \
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100522 x.tag = MBEDTLS_ASN1_OID; \
Dave Rodgmane2b772d2022-08-11 16:04:13 +0100523 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 while (0)
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if (strcmp(ext_key_usage, "serverAuth") == 0) {
527 SET_OID(exts[0].buf, MBEDTLS_OID_SERVER_AUTH);
528 } else if (strcmp(ext_key_usage, "codeSigning,timeStamping") == 0) {
529 SET_OID(exts[0].buf, MBEDTLS_OID_CODE_SIGNING);
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100530 exts[0].next = &exts[1];
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 SET_OID(exts[1].buf, MBEDTLS_OID_TIME_STAMPING);
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000532 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 TEST_ASSERT(mbedtls_x509write_crt_set_ext_key_usage(&crt, exts) == 0);
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000534 }
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100535 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200536
Andrzej Kurek76c96622023-04-04 06:57:08 -0400537 if (set_subjectAltNames) {
538 TEST_ASSERT(mbedtls_x509write_crt_set_subject_alternative_name(&crt, san_list) == 0);
539 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 ret = mbedtls_x509write_crt_pem(&crt, buf, sizeof(buf),
541 mbedtls_test_rnd_pseudo_rand, &rnd_info);
542 TEST_ASSERT(ret == 0);
Paul Bakker2397cf32013-09-08 15:58:15 +0200543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 pem_len = strlen((char *) buf);
Paul Bakker2397cf32013-09-08 15:58:15 +0200545
Paul Elliott557b8d62020-11-19 09:46:56 +0000546 // check that the rest of the buffer remains clear
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
548 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000549 }
550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if (issuer_key_type != MBEDTLS_PK_RSA) {
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100552 mbedtls_x509_crt crt_parse, trusted;
553 uint32_t flags;
Paul Bakker2397cf32013-09-08 15:58:15 +0200554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 mbedtls_x509_crt_init(&crt_parse);
556 mbedtls_x509_crt_init(&trusted);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted,
559 cert_verify_file) == 0);
560 TEST_ASSERT(mbedtls_x509_crt_parse(&crt_parse,
561 buf, sizeof(buf)) == 0);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 ret = mbedtls_x509_crt_verify(&crt_parse, &trusted, NULL, NULL, &flags,
564 NULL, NULL);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 mbedtls_x509_crt_free(&crt_parse);
567 mbedtls_x509_crt_free(&trusted);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 TEST_EQUAL(flags, 0);
570 TEST_EQUAL(ret, 0);
571 } else if (*cert_check_file != '\0') {
572 f = fopen(cert_check_file, "r");
573 TEST_ASSERT(f != NULL);
574 olen = fread(check_buf, 1, sizeof(check_buf), f);
575 fclose(f);
576 TEST_ASSERT(olen < sizeof(check_buf));
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 TEST_EQUAL(olen, pem_len);
579 TEST_ASSERT(olen >= pem_len - 1);
580 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100581 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 der_len = mbedtls_x509write_crt_der(&crt, buf, sizeof(buf),
584 mbedtls_test_rnd_pseudo_rand,
585 &rnd_info);
586 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100589 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 }
Andres AGe0af9952016-09-07 11:09:44 +0100591
Werner Lewisacd01e52022-05-10 12:23:13 +0100592 // Not testing against file, check date format
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 if (*cert_check_file == '\0') {
Werner Lewisacd01e52022-05-10 12:23:13 +0100594 // UTC tag if before 2050, 2 digits less for year
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 if (not_before[0] == '2' && (not_before[1] > '0' || not_before[2] > '4')) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100596 before_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 } else {
Werner Lewisacd01e52022-05-10 12:23:13 +0100598 before_tag = MBEDTLS_ASN1_UTC_TIME;
599 not_before += 2;
600 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 if (not_after[0] == '2' && (not_after[1] > '0' || not_after[2] > '4')) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100602 after_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 } else {
Werner Lewisacd01e52022-05-10 12:23:13 +0100604 after_tag = MBEDTLS_ASN1_UTC_TIME;
605 not_after += 2;
606 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 end = buf + sizeof(buf);
608 for (p = end - der_len; p < end;) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100609 tag = *p++;
610 sz = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 if (tag == MBEDTLS_ASN1_UTC_TIME || tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100612 // Check correct tag and time written
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 TEST_ASSERT(before_tag == tag);
614 TEST_ASSERT(memcmp(p, not_before, sz - 1) == 0);
Werner Lewisacd01e52022-05-10 12:23:13 +0100615 p += sz;
616 tag = *p++;
617 sz = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 TEST_ASSERT(after_tag == tag);
619 TEST_ASSERT(memcmp(p, not_after, sz - 1) == 0);
Werner Lewisacd01e52022-05-10 12:23:13 +0100620 break;
621 }
622 // Increment if long form ASN1 length
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 if (sz & 0x80) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100624 p += sz & 0x0F;
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 }
626 if (tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100627 p += sz;
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 }
Werner Lewisacd01e52022-05-10 12:23:13 +0100629 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 TEST_ASSERT(p < end);
Werner Lewisacd01e52022-05-10 12:23:13 +0100631 }
632
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100633#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge6ed23c2022-04-22 09:44:04 +0200634 // When using PSA crypto, RNG isn't controllable, result length isn't
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100635 // deterministic over multiple runs, removing a single byte isn't enough to
636 // go into the MBEDTLS_ERR_ASN1_BUF_TOO_SMALL error case
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 if (issuer_key_type != MBEDTLS_PK_RSA) {
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100638 der_len /= 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 } else
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100640#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 der_len -= 1;
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 ret = mbedtls_x509write_crt_der(&crt, buf, (size_t) (der_len),
644 mbedtls_test_rnd_pseudo_rand, &rnd_info);
645 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100646
Paul Bakkerbd51b262014-07-10 15:26:12 +0200647exit:
Andrzej Kurek5da1d752023-04-05 08:30:59 -0400648 mbedtls_asn1_free_named_data_list(&ext_san_dirname);
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 mbedtls_x509write_crt_free(&crt);
650 mbedtls_pk_free(&issuer_key_alt);
651 mbedtls_pk_free(&subject_key);
652 mbedtls_pk_free(&issuer_key);
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100653#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
654 mbedtls_mpi_free(&serial_mpi);
655#endif
Neil Armstrong98f899c2022-03-16 17:42:42 +0100656#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 psa_destroy_key(key_id);
Neil Armstrong98f899c2022-03-16 17:42:42 +0100658#endif
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100659 MD_OR_USE_PSA_DONE();
Paul Bakker2397cf32013-09-08 15:58:15 +0200660}
661/* END_CASE */
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200662
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100663/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_WRITE_C */
664void x509_set_serial_check()
665{
666 mbedtls_x509write_cert ctx;
667 uint8_t invalid_serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN + 1];
668
David Horstmanne04a97a2023-12-08 18:27:48 +0000669#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
670 mbedtls_mpi serial_mpi;
671 mbedtls_mpi_init(&serial_mpi);
672#endif
673
Valerio Setti569c1712023-04-19 14:53:36 +0200674 USE_PSA_INIT();
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100675 memset(invalid_serial, 0x01, sizeof(invalid_serial));
676
Valerio Settiea19d2d2023-01-09 17:21:17 +0100677#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100678 TEST_EQUAL(mbedtls_mpi_read_binary(&serial_mpi, invalid_serial,
679 sizeof(invalid_serial)), 0);
680 TEST_EQUAL(mbedtls_x509write_crt_set_serial(&ctx, &serial_mpi),
681 MBEDTLS_ERR_X509_BAD_INPUT_DATA);
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100682#endif
683
Valerio Settiaf4815c2023-01-26 17:43:09 +0100684 TEST_EQUAL(mbedtls_x509write_crt_set_serial_raw(&ctx, invalid_serial,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100685 sizeof(invalid_serial)),
686 MBEDTLS_ERR_X509_BAD_INPUT_DATA);
687
Valerio Settia87f8392023-01-26 18:00:50 +0100688exit:
689#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
690 mbedtls_mpi_free(&serial_mpi);
691#else
692 ;
693#endif
Valerio Setti569c1712023-04-19 14:53:36 +0200694 USE_PSA_DONE();
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100695}
696/* END_CASE */
697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C */
Gilles Peskinec94500b2023-09-21 18:01:05 +0200699void mbedtls_x509_string_to_names(char *name, char *parsed_name,
700 int result, int may_fail)
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200701{
702 int ret;
703 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_asn1_named_data *names = NULL;
Gilles Peskine21e46b32023-10-17 16:35:20 +0200705 mbedtls_x509_name parsed;
706 memset(&parsed, 0, sizeof(parsed));
707 mbedtls_x509_name *parsed_cur = NULL;
708 mbedtls_x509_name *parsed_prv = NULL;
Gilles Peskinef2574202023-10-18 17:39:48 +0200709 unsigned char buf[1024] = { 0 };
710 unsigned char out[1024] = { 0 };
Gilles Peskine21e46b32023-10-17 16:35:20 +0200711 unsigned char *c = buf + sizeof(buf);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200712
Valerio Setti569c1712023-04-19 14:53:36 +0200713 USE_PSA_INIT();
714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 ret = mbedtls_x509_string_to_names(&names, name);
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200716 TEST_EQUAL(ret, result);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if (ret != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200719 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 }
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 ret = mbedtls_x509_write_names(&c, buf, names);
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200723 TEST_LE_S(1, ret);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200724
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200725 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
Gilles Peskineaa01a032023-09-21 15:57:30 +0200726 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
Gilles Peskinec94500b2023-09-21 18:01:05 +0200727 ret = mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed);
728 if ((may_fail & MAY_FAIL_GET_NAME) && ret < 0) {
729 /* Validation inconsistency between mbedtls_x509_string_to_names() and
730 * mbedtls_x509_get_name(). Accept it for now. */
731 goto exit;
732 }
733 TEST_EQUAL(ret, 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 ret = mbedtls_x509_dn_gets((char *) out, sizeof(out), &parsed);
Gilles Peskinec94500b2023-09-21 18:01:05 +0200736 if ((may_fail & MAY_FAIL_DN_GETS) && ret < 0) {
737 /* Validation inconsistency between mbedtls_x509_string_to_names() and
738 * mbedtls_x509_dn_gets(). Accept it for now. */
739 goto exit;
740 }
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200741 TEST_LE_S(1, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 TEST_ASSERT(strcmp((char *) out, parsed_name) == 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200743
744exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 mbedtls_asn1_free_named_data_list(&names);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200746
747 parsed_cur = parsed.next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 while (parsed_cur != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200749 parsed_prv = parsed_cur;
750 parsed_cur = parsed_cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 mbedtls_free(parsed_prv);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200752 }
Valerio Setti569c1712023-04-19 14:53:36 +0200753 USE_PSA_DONE();
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200754}
755/* END_CASE */
Jonathan Winzig2bd2b782024-01-09 15:19:42 +0100756
Jonathan Winzig315c3ca2024-01-09 18:31:11 +0100757/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_WRITE_C */
Jonathan Winzig6c9779f2024-01-09 17:47:10 +0100758void x509_set_extension_length_check()
Jonathan Winzig2bd2b782024-01-09 15:19:42 +0100759{
760 int ret = 0;
761
762 mbedtls_x509write_csr ctx;
763 mbedtls_x509write_csr_init(&ctx);
764
765 unsigned char buf[EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH] = { 0 };
766 unsigned char *p = buf + sizeof(buf);
767
768 ret = mbedtls_x509_set_extension(&(ctx.MBEDTLS_PRIVATE(extensions)),
769 MBEDTLS_OID_EXTENDED_KEY_USAGE,
770 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
771 0,
772 p,
Jonathan Winzig6c9779f2024-01-09 17:47:10 +0100773 SIZE_MAX);
774 TEST_ASSERT(MBEDTLS_ERR_X509_BAD_INPUT_DATA == ret);
Jonathan Winzig2bd2b782024-01-09 15:19:42 +0100775}
776/* END_CASE */