blob: a0b1dc13518d2e9f7f239c35dde94d940add8d1f [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"
5#include "mbedtls/pem.h"
6#include "mbedtls/oid.h"
Hanno Becker418a6222017-09-14 07:51:28 +01007#include "mbedtls/rsa.h"
Valerio Setti48e8fc72022-10-19 15:14:29 +02008#include "mbedtls/asn1write.h"
Valerio Setti178b5bd2023-02-13 10:04:28 +01009#include "mbedtls/pk.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020010#include "md_psa.h"
Manuel Pégourié-Gonnardfeb03962020-08-20 09:59:33 +020011
Hanno Becker418a6222017-09-14 07:51:28 +010012#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010013int mbedtls_rsa_decrypt_func(void *ctx, size_t *olen,
14 const unsigned char *input, unsigned char *output,
15 size_t output_max_len)
Hanno Becker418a6222017-09-14 07:51:28 +010016{
Gilles Peskine449bd832023-01-11 14:50:10 +010017 return mbedtls_rsa_pkcs1_decrypt((mbedtls_rsa_context *) ctx, NULL, NULL,
18 olen, input, output, output_max_len);
Hanno Becker418a6222017-09-14 07:51:28 +010019}
Gilles Peskine449bd832023-01-11 14:50:10 +010020int mbedtls_rsa_sign_func(void *ctx,
21 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
22 mbedtls_md_type_t md_alg, unsigned int hashlen,
23 const unsigned char *hash, unsigned char *sig)
Hanno Becker418a6222017-09-14 07:51:28 +010024{
Gilles Peskine449bd832023-01-11 14:50:10 +010025 return mbedtls_rsa_pkcs1_sign((mbedtls_rsa_context *) ctx, f_rng, p_rng,
26 md_alg, hashlen, hash, sig);
Hanno Becker418a6222017-09-14 07:51:28 +010027}
Gilles Peskine449bd832023-01-11 14:50:10 +010028size_t mbedtls_rsa_key_len_func(void *ctx)
Hanno Becker418a6222017-09-14 07:51:28 +010029{
Gilles Peskine449bd832023-01-11 14:50:10 +010030 return ((const mbedtls_rsa_context *) ctx)->len;
Hanno Becker418a6222017-09-14 07:51:28 +010031}
32#endif /* MBEDTLS_RSA_C */
33
Gilles Peskinef4e672e2020-01-31 14:22:10 +010034#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
35 defined(MBEDTLS_PEM_WRITE_C) && defined(MBEDTLS_X509_CSR_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010036static int x509_crt_verifycsr(const unsigned char *buf, size_t buflen)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050037{
Przemek Stekiel54a54462022-08-01 13:59:12 +020038 unsigned char hash[PSA_HASH_MAX_SIZE];
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050039 mbedtls_x509_csr csr;
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010040 int ret = 0;
41
Gilles Peskine449bd832023-01-11 14:50:10 +010042 mbedtls_x509_csr_init(&csr);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050043
Gilles Peskine449bd832023-01-11 14:50:10 +010044 if (mbedtls_x509_csr_parse(&csr, buf, buflen) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010045 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
46 goto cleanup;
47 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050048
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020049 psa_algorithm_t psa_alg = mbedtls_md_psa_alg_from_type(csr.sig_md);
Przemek Stekiel54a54462022-08-01 13:59:12 +020050 size_t hash_size = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010051 psa_status_t status = psa_hash_compute(psa_alg, csr.cri.p, csr.cri.len,
52 hash, PSA_HASH_MAX_SIZE, &hash_size);
Przemek Stekiel54a54462022-08-01 13:59:12 +020053
Gilles Peskine449bd832023-01-11 14:50:10 +010054 if (status != PSA_SUCCESS) {
Andrzej Kurek4b114072018-11-19 18:04:01 -050055 /* Note: this can't happen except after an internal error */
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010056 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
57 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050058 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050059
Gilles Peskine449bd832023-01-11 14:50:10 +010060 if (mbedtls_pk_verify_ext(csr.sig_pk, csr.sig_opts, &csr.pk,
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +020061 csr.sig_md, hash, mbedtls_md_get_size_from_type(csr.sig_md),
Gilles Peskine449bd832023-01-11 14:50:10 +010062 csr.sig.p, csr.sig.len) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010063 ret = MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
64 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050065 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050066
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010067cleanup:
68
Gilles Peskine449bd832023-01-11 14:50:10 +010069 mbedtls_x509_csr_free(&csr);
70 return ret;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050071}
Gilles Peskinef4e672e2020-01-31 14:22:10 +010072#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_PEM_WRITE_C && MBEDTLS_X509_CSR_WRITE_C */
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050073
Valerio Setti48e8fc72022-10-19 15:14:29 +020074#if defined(MBEDTLS_X509_CSR_WRITE_C)
75
76/*
77 * The size of this temporary buffer is given by the sequence of functions
78 * called hereinafter:
79 * - mbedtls_asn1_write_oid()
80 * - 8 bytes for MBEDTLS_OID_EXTENDED_KEY_USAGE raw value
81 * - 1 byte for MBEDTLS_OID_EXTENDED_KEY_USAGE length
82 * - 1 byte for MBEDTLS_ASN1_OID tag
83 * - mbedtls_asn1_write_len()
84 * - 1 byte since we're dealing with sizes which are less than 0x80
85 * - mbedtls_asn1_write_tag()
86 * - 1 byte
87 *
88 * This length is fine as long as this function is called using the
89 * MBEDTLS_OID_SERVER_AUTH OID. If this is changed in the future, then this
90 * buffer's length should be adjusted accordingly.
91 * Unfortunately there's no predefined max size for OIDs which can be used
92 * to set an overall upper boundary which is always guaranteed.
93 */
94#define EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH 12
95
Gilles Peskine449bd832023-01-11 14:50:10 +010096static int csr_set_extended_key_usage(mbedtls_x509write_csr *ctx,
97 const char *oid, size_t oid_len)
Valerio Setti48e8fc72022-10-19 15:14:29 +020098{
99 unsigned char buf[EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH] = { 0 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 unsigned char *p = buf + sizeof(buf);
Valerio Setti48e8fc72022-10-19 15:14:29 +0200101 int ret;
102 size_t len = 0;
103
104 /*
105 * Following functions fail anyway if the temporary buffer is not large,
106 * but we set an extra check here to emphasize a possible source of errors
107 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 if (oid_len > EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH) {
Valerio Setti48e8fc72022-10-19 15:14:29 +0200109 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
110 }
111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(&p, buf, oid, oid_len));
113 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, ret));
114 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, buf,
115 MBEDTLS_ASN1_CONSTRUCTED |
116 MBEDTLS_ASN1_SEQUENCE));
Valerio Setti48e8fc72022-10-19 15:14:29 +0200117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 ret = mbedtls_x509write_csr_set_extension(ctx,
119 MBEDTLS_OID_EXTENDED_KEY_USAGE,
120 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
121 0,
122 p,
123 len);
Valerio Setti48e8fc72022-10-19 15:14:29 +0200124
125 return ret;
126}
127#endif /* MBEDTLS_X509_CSR_WRITE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200128/* END_HEADER */
Paul Bakker6d620502012-02-16 14:09:13 +0000129
Paul Bakker33b43f12013-08-20 11:48:36 +0200130/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 * depends_on:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO:MBEDTLS_PK_PARSE_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200132 * END_DEPENDENCIES
133 */
Paul Bakker6d620502012-02-16 14:09:13 +0000134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136void x509_csr_check(char *key_file, char *cert_req_check_file, int md_type,
137 int key_usage, int set_key_usage, int cert_type,
138 int set_cert_type, int set_extension)
Paul Bakker6d620502012-02-16 14:09:13 +0000139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_pk_context key;
141 mbedtls_x509write_csr req;
Andres AGe0af9952016-09-07 11:09:44 +0100142 unsigned char buf[4096];
Paul Bakker6d620502012-02-16 14:09:13 +0000143 int ret;
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100144#if !defined(MBEDTLS_USE_PSA_CRYPTO)
145 unsigned char check_buf[4000];
Paul Bakker6d620502012-02-16 14:09:13 +0000146 FILE *f;
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100147 size_t olen = 0;
148#endif /* !MBEDTLS_USE_PSA_CRYPTO */
149 size_t pem_len = 0, buf_index;
150 int der_len = -1;
Paul Bakker3a8cb6f2013-12-30 20:41:54 +0100151 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200152 mbedtls_test_rnd_pseudo_info rnd_info;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100153 mbedtls_x509_san_list san_ip;
154 mbedtls_x509_san_list san_dns;
155 mbedtls_x509_san_list san_uri;
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400156 mbedtls_x509_san_list san_mail;
157 mbedtls_x509_san_list san_dn;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100158 mbedtls_x509_san_list *san_list = NULL;
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400159 mbedtls_asn1_named_data *ext_san_dirname = NULL;
160
161 const char san_ip_name[] = { 0x7f, 0x00, 0x00, 0x01 }; // 127.0.0.1
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100162 const char *san_dns_name = "example.com";
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400163 const char *san_dn_name = "C=UK,O=Mbed TLS,CN=Mbed TLS directoryName SAN";
164 const char *san_mail_name = "mail@example.com";
165 const char *san_uri_name = "http://pki.example.com";
166
167 san_mail.node.type = MBEDTLS_X509_SAN_RFC822_NAME;
168 san_mail.node.san.unstructured_name.p = (unsigned char *) san_mail_name;
169 san_mail.node.san.unstructured_name.len = strlen(san_mail_name);
170 san_mail.next = NULL;
171
172 san_dns.node.type = MBEDTLS_X509_SAN_DNS_NAME;
173 san_dns.node.san.unstructured_name.p = (unsigned char *) san_dns_name;
174 san_dns.node.san.unstructured_name.len = strlen(san_dns_name);
175 san_dns.next = &san_mail;
176
177 san_dn.node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
178 TEST_ASSERT(mbedtls_x509_string_to_names(&ext_san_dirname,
179 san_dn_name) == 0);
180 san_dn.node.san.directory_name = *ext_san_dirname;
181 san_dn.next = &san_dns;
182
183 san_ip.node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
184 san_ip.node.san.unstructured_name.p = (unsigned char *) san_ip_name;
185 san_ip.node.san.unstructured_name.len = sizeof(san_ip_name);
186 san_ip.next = &san_dn;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100187
188 san_uri.node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100189 san_uri.node.san.unstructured_name.p = (unsigned char *) san_uri_name;
190 san_uri.node.san.unstructured_name.len = strlen(san_uri_name);
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400191 san_uri.next = &san_ip;
192
193 san_list = &san_uri;
Paul Bakker6d620502012-02-16 14:09:13 +0000194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 mbedtls_x509write_csr_init(&req);
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 mbedtls_pk_init(&key);
valerio32f2ac92023-04-20 11:59:52 +0200199 MD_OR_USE_PSA_INIT();
200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL,
202 mbedtls_test_rnd_std_rand, NULL) == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 mbedtls_x509write_csr_set_md_alg(&req, md_type);
205 mbedtls_x509write_csr_set_key(&req, &key);
206 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
207 if (set_key_usage != 0) {
208 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
209 }
210 if (set_cert_type != 0) {
211 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
212 }
213 if (set_extension != 0) {
214 TEST_ASSERT(csr_set_extended_key_usage(&req, MBEDTLS_OID_SERVER_AUTH,
215 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) == 0);
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100216
217 TEST_ASSERT(mbedtls_x509write_csr_set_subject_alternative_name(&req, san_list) == 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 }
Paul Bakker8eabfc12013-08-25 10:18:25 +0200219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf),
221 mbedtls_test_rnd_pseudo_rand, &rnd_info);
222 TEST_ASSERT(ret == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 pem_len = strlen((char *) buf);
Paul Bakker6d620502012-02-16 14:09:13 +0000225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
227 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000228 }
229
Neil Armstrong5b320382022-02-21 17:22:10 +0100230#if defined(MBEDTLS_USE_PSA_CRYPTO)
231 // When using PSA crypto, RNG isn't controllable, so cert_req_check_file can't be used
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 (void) cert_req_check_file;
Neil Armstrong5b320382022-02-21 17:22:10 +0100233 buf[pem_len] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0);
Neil Armstrong5b320382022-02-21 17:22:10 +0100235#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 f = fopen(cert_req_check_file, "r");
237 TEST_ASSERT(f != NULL);
238 olen = fread(check_buf, 1, sizeof(check_buf), f);
239 fclose(f);
Paul Bakker6d620502012-02-16 14:09:13 +0000240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 TEST_ASSERT(olen >= pem_len - 1);
242 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100243#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 der_len = mbedtls_x509write_csr_der(&req, buf, sizeof(buf),
246 mbedtls_test_rnd_pseudo_rand,
247 &rnd_info);
248 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100251 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 }
Andres AGe0af9952016-09-07 11:09:44 +0100253
Neil Armstrong5b320382022-02-21 17:22:10 +0100254#if defined(MBEDTLS_USE_PSA_CRYPTO)
255 // When using PSA crypto, RNG isn't controllable, result length isn't
256 // deterministic over multiple runs, removing a single byte isn't enough to
257 // go into the MBEDTLS_ERR_ASN1_BUF_TOO_SMALL error case
258 der_len /= 2;
259#else
260 der_len -= 1;
261#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 ret = mbedtls_x509write_csr_der(&req, buf, (size_t) (der_len),
263 mbedtls_test_rnd_pseudo_rand, &rnd_info);
264 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100265
Paul Bakkerbd51b262014-07-10 15:26:12 +0200266exit:
Andrzej Kurekbdb41dd2023-07-10 08:09:50 -0400267 mbedtls_asn1_free_named_data_list(&ext_san_dirname);
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 mbedtls_x509write_csr_free(&req);
269 mbedtls_pk_free(&key);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100270 MD_OR_USE_PSA_DONE();
Paul Bakker6d620502012-02-16 14:09:13 +0000271}
Paul Bakker33b43f12013-08-20 11:48:36 +0200272/* END_CASE */
Paul Bakker2397cf32013-09-08 15:58:15 +0200273
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500274/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C:MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275void x509_csr_check_opaque(char *key_file, int md_type, int key_usage,
276 int cert_type)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500277{
278 mbedtls_pk_context key;
Ronald Cron5425a212020-08-04 14:58:35 +0200279 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong95974972022-04-22 13:57:44 +0200280 psa_algorithm_t md_alg_psa, alg_psa;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500281 mbedtls_x509write_csr req;
282 unsigned char buf[4096];
283 int ret;
284 size_t pem_len = 0;
285 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200286 mbedtls_test_rnd_pseudo_info rnd_info;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500287
valerio32f2ac92023-04-20 11:59:52 +0200288 mbedtls_x509write_csr_init(&req);
Valerio Setti569c1712023-04-19 14:53:36 +0200289 MD_OR_USE_PSA_INIT();
290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500292
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200293 md_alg_psa = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 TEST_ASSERT(md_alg_psa != MBEDTLS_MD_NONE);
Andrzej Kurek967cfd12018-11-20 02:53:17 -0500295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 mbedtls_pk_init(&key);
297 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL,
298 mbedtls_test_rnd_std_rand, NULL) == 0);
Neil Armstrong95974972022-04-22 13:57:44 +0200299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
301 alg_psa = PSA_ALG_ECDSA(md_alg_psa);
302 } else if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
303 alg_psa = PSA_ALG_RSA_PKCS1V15_SIGN(md_alg_psa);
304 } else {
305 TEST_ASSUME(!"PK key type not supported in this configuration");
306 }
Neil Armstrong95974972022-04-22 13:57:44 +0200307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 TEST_ASSERT(mbedtls_pk_wrap_as_opaque(&key, &key_id, alg_psa,
309 PSA_KEY_USAGE_SIGN_HASH,
310 PSA_ALG_NONE) == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 mbedtls_x509write_csr_set_md_alg(&req, md_type);
313 mbedtls_x509write_csr_set_key(&req, &key);
314 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
315 if (key_usage != 0) {
316 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
317 }
318 if (cert_type != 0) {
319 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
320 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf) - 1,
323 mbedtls_test_rnd_pseudo_rand, &rnd_info);
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 TEST_ASSERT(ret == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 pem_len = strlen((char *) buf);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500328 buf[pem_len] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500330
Manuel Pégourié-Gonnardfeb03962020-08-20 09:59:33 +0200331
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500332exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 mbedtls_x509write_csr_free(&req);
334 mbedtls_pk_free(&key);
335 psa_destroy_key(key_id);
Valerio Setti569c1712023-04-19 14:53:36 +0200336 MD_OR_USE_PSA_DONE();
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500337}
338/* END_CASE */
339
Manuel Pégourié-Gonnarda9464892023-03-17 12:08:50 +0100340/* 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 +0100341void x509_crt_check(char *subject_key_file, char *subject_pwd,
342 char *subject_name, char *issuer_key_file,
343 char *issuer_pwd, char *issuer_name,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100344 data_t *serial_arg, char *not_before, char *not_after,
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 int md_type, int key_usage, int set_key_usage,
346 char *ext_key_usage,
347 int cert_type, int set_cert_type, int auth_ident,
348 int ver, char *cert_check_file, int pk_wrap, int is_ca,
Andrzej Kurek76c96622023-04-04 06:57:08 -0400349 char *cert_verify_file, int set_subjectAltNames)
Paul Bakker2397cf32013-09-08 15:58:15 +0200350{
Hanno Becker418a6222017-09-14 07:51:28 +0100351 mbedtls_pk_context subject_key, issuer_key, issuer_key_alt;
352 mbedtls_pk_context *key = &issuer_key;
353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_x509write_cert crt;
Andres AGe0af9952016-09-07 11:09:44 +0100355 unsigned char buf[4096];
Paul Bakker2397cf32013-09-08 15:58:15 +0200356 unsigned char check_buf[5000];
Werner Lewisacd01e52022-05-10 12:23:13 +0100357 unsigned char *p, *end;
358 unsigned char tag, sz;
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100359#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
360 mbedtls_mpi serial_mpi;
361#endif
Werner Lewisacd01e52022-05-10 12:23:13 +0100362 int ret, before_tag, after_tag;
Paul Elliott557b8d62020-11-19 09:46:56 +0000363 size_t olen = 0, pem_len = 0, buf_index = 0;
Andres AGe0af9952016-09-07 11:09:44 +0100364 int der_len = -1;
Paul Bakker2397cf32013-09-08 15:58:15 +0200365 FILE *f;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200366 mbedtls_test_rnd_pseudo_info rnd_info;
Neil Armstrong98f899c2022-03-16 17:42:42 +0100367#if defined(MBEDTLS_USE_PSA_CRYPTO)
368 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
369#endif
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100370 mbedtls_pk_type_t issuer_key_type;
Andrzej Kurek76c96622023-04-04 06:57:08 -0400371 mbedtls_x509_san_list san_ip;
372 mbedtls_x509_san_list san_dns;
373 mbedtls_x509_san_list san_uri;
374 mbedtls_x509_san_list san_mail;
375 mbedtls_x509_san_list san_dn;
376 mbedtls_asn1_named_data *ext_san_dirname = NULL;
377 const char san_ip_name[] = { 0x01, 0x02, 0x03, 0x04 };
378 const char *san_dns_name = "example.com";
379 const char *san_dn_name = "C=UK,O=Mbed TLS,CN=SubjectAltName test";
380 const char *san_mail_name = "mail@example.com";
381 const char *san_uri_name = "http://pki.example.com";
382 mbedtls_x509_san_list *san_list = NULL;
383
384 if (set_subjectAltNames) {
385 san_mail.node.type = MBEDTLS_X509_SAN_RFC822_NAME;
386 san_mail.node.san.unstructured_name.p = (unsigned char *) san_mail_name;
Andrzej Kurek13c43f62023-04-04 10:43:38 -0400387 san_mail.node.san.unstructured_name.len = strlen(san_mail_name);
Andrzej Kurek76c96622023-04-04 06:57:08 -0400388 san_mail.next = NULL;
389
390 san_dns.node.type = MBEDTLS_X509_SAN_DNS_NAME;
391 san_dns.node.san.unstructured_name.p = (unsigned char *) san_dns_name;
392 san_dns.node.san.unstructured_name.len = strlen(san_dns_name);
393 san_dns.next = &san_mail;
394
395 san_dn.node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
396 TEST_ASSERT(mbedtls_x509_string_to_names(&ext_san_dirname,
397 san_dn_name) == 0);
398 san_dn.node.san.directory_name = *ext_san_dirname;
399 san_dn.next = &san_dns;
400
401 san_ip.node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
402 san_ip.node.san.unstructured_name.p = (unsigned char *) san_ip_name;
403 san_ip.node.san.unstructured_name.len = sizeof(san_ip_name);
404 san_ip.next = &san_dn;
405
406 san_uri.node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
407 san_uri.node.san.unstructured_name.p = (unsigned char *) san_uri_name;
408 san_uri.node.san.unstructured_name.len = strlen(san_uri_name);
409 san_uri.next = &san_ip;
410
411 san_list = &san_uri;
412 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100415#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
416 mbedtls_mpi_init(&serial_mpi);
417#endif
Hanno Becker418a6222017-09-14 07:51:28 +0100418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 mbedtls_pk_init(&subject_key);
420 mbedtls_pk_init(&issuer_key);
421 mbedtls_pk_init(&issuer_key_alt);
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 mbedtls_x509write_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200423 MD_OR_USE_PSA_INIT();
Paul Bakker2397cf32013-09-08 15:58:15 +0200424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 TEST_ASSERT(mbedtls_pk_parse_keyfile(&subject_key, subject_key_file,
426 subject_pwd, mbedtls_test_rnd_std_rand, NULL) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 TEST_ASSERT(mbedtls_pk_parse_keyfile(&issuer_key, issuer_key_file,
429 issuer_pwd, mbedtls_test_rnd_std_rand, NULL) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 issuer_key_type = mbedtls_pk_get_type(&issuer_key);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100432
Dave Rodgmandc3b1542023-01-20 11:39:00 +0000433#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Hanno Becker418a6222017-09-14 07:51:28 +0100434 /* For RSA PK contexts, create a copy as an alternative RSA context. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if (pk_wrap == 1 && issuer_key_type == MBEDTLS_PK_RSA) {
436 TEST_ASSERT(mbedtls_pk_setup_rsa_alt(&issuer_key_alt,
437 mbedtls_pk_rsa(issuer_key),
438 mbedtls_rsa_decrypt_func,
439 mbedtls_rsa_sign_func,
440 mbedtls_rsa_key_len_func) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100441
442 key = &issuer_key_alt;
443 }
Manuel Pégourié-Gonnard147b28e2018-03-12 15:26:59 +0100444#endif
Hanno Becker418a6222017-09-14 07:51:28 +0100445
Neil Armstrong98f899c2022-03-16 17:42:42 +0100446#if defined(MBEDTLS_USE_PSA_CRYPTO)
447 /* For Opaque PK contexts, wrap key as an Opaque RSA context. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 if (pk_wrap == 2) {
Neil Armstrong95974972022-04-22 13:57:44 +0200449 psa_algorithm_t alg_psa, md_alg_psa;
Neil Armstrong98f899c2022-03-16 17:42:42 +0100450
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200451 md_alg_psa = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 TEST_ASSERT(md_alg_psa != MBEDTLS_MD_NONE);
Neil Armstrong95974972022-04-22 13:57:44 +0200453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if (mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_ECKEY) {
455 alg_psa = PSA_ALG_ECDSA(md_alg_psa);
456 } else if (mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_RSA) {
457 alg_psa = PSA_ALG_RSA_PKCS1V15_SIGN(md_alg_psa);
458 } else {
459 TEST_ASSUME(!"PK key type not supported in this configuration");
460 }
Neil Armstrong95974972022-04-22 13:57:44 +0200461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 TEST_ASSERT(mbedtls_pk_wrap_as_opaque(&issuer_key, &key_id, alg_psa,
463 PSA_KEY_USAGE_SIGN_HASH,
464 PSA_ALG_NONE) == 0);
Neil Armstrong98f899c2022-03-16 17:42:42 +0100465 }
466#endif /* MBEDTLS_USE_PSA_CRYPTO */
467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (pk_wrap == 2) {
469 TEST_ASSERT(mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_OPAQUE);
470 }
Neil Armstrong98f899c2022-03-16 17:42:42 +0100471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if (ver != -1) {
473 mbedtls_x509write_crt_set_version(&crt, ver);
474 }
Hanno Becker418a6222017-09-14 07:51:28 +0100475
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100476#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100477 TEST_ASSERT(mbedtls_mpi_read_binary(&serial_mpi, serial_arg->x,
478 serial_arg->len) == 0);
479 TEST_ASSERT(mbedtls_x509write_crt_set_serial(&crt, &serial_mpi) == 0);
Valerio Settida0afcc2023-01-05 16:46:59 +0100480#else
Valerio Settiaf4815c2023-01-26 17:43:09 +0100481 TEST_ASSERT(mbedtls_x509write_crt_set_serial_raw(&crt, serial_arg->x,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100482 serial_arg->len) == 0);
Valerio Settida0afcc2023-01-05 16:46:59 +0100483#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 TEST_ASSERT(mbedtls_x509write_crt_set_validity(&crt, not_before,
485 not_after) == 0);
486 mbedtls_x509write_crt_set_md_alg(&crt, md_type);
487 TEST_ASSERT(mbedtls_x509write_crt_set_issuer_name(&crt, issuer_name) == 0);
488 TEST_ASSERT(mbedtls_x509write_crt_set_subject_name(&crt, subject_name) == 0);
489 mbedtls_x509write_crt_set_subject_key(&crt, &subject_key);
Hanno Becker418a6222017-09-14 07:51:28 +0100490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 mbedtls_x509write_crt_set_issuer_key(&crt, key);
Paul Bakker2397cf32013-09-08 15:58:15 +0200492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 if (crt.version >= MBEDTLS_X509_CRT_VERSION_3) {
Darren Krahn9c134ce2021-01-13 22:04:45 -0800494 /* For the CA case, a path length of -1 means unlimited. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 TEST_ASSERT(mbedtls_x509write_crt_set_basic_constraints(&crt, is_ca,
496 (is_ca ? -1 : 0)) == 0);
497 TEST_ASSERT(mbedtls_x509write_crt_set_subject_key_identifier(&crt) == 0);
498 if (auth_ident) {
499 TEST_ASSERT(mbedtls_x509write_crt_set_authority_key_identifier(&crt) == 0);
500 }
501 if (set_key_usage != 0) {
502 TEST_ASSERT(mbedtls_x509write_crt_set_key_usage(&crt, key_usage) == 0);
503 }
504 if (set_cert_type != 0) {
505 TEST_ASSERT(mbedtls_x509write_crt_set_ns_cert_type(&crt, cert_type) == 0);
506 }
507 if (strcmp(ext_key_usage, "NULL") != 0) {
Dave Rodgmanec9f6b42022-07-27 14:34:58 +0100508 mbedtls_asn1_sequence exts[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 memset(exts, 0, sizeof(exts));
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100510
511#define SET_OID(x, oid) \
512 do { \
513 x.len = MBEDTLS_OID_SIZE(oid); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 x.p = (unsigned char *) oid; \
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100515 x.tag = MBEDTLS_ASN1_OID; \
Dave Rodgmane2b772d2022-08-11 16:04:13 +0100516 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 while (0)
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if (strcmp(ext_key_usage, "serverAuth") == 0) {
520 SET_OID(exts[0].buf, MBEDTLS_OID_SERVER_AUTH);
521 } else if (strcmp(ext_key_usage, "codeSigning,timeStamping") == 0) {
522 SET_OID(exts[0].buf, MBEDTLS_OID_CODE_SIGNING);
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100523 exts[0].next = &exts[1];
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 SET_OID(exts[1].buf, MBEDTLS_OID_TIME_STAMPING);
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000525 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 TEST_ASSERT(mbedtls_x509write_crt_set_ext_key_usage(&crt, exts) == 0);
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000527 }
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100528 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200529
Andrzej Kurek76c96622023-04-04 06:57:08 -0400530 if (set_subjectAltNames) {
531 TEST_ASSERT(mbedtls_x509write_crt_set_subject_alternative_name(&crt, san_list) == 0);
532 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 ret = mbedtls_x509write_crt_pem(&crt, buf, sizeof(buf),
534 mbedtls_test_rnd_pseudo_rand, &rnd_info);
535 TEST_ASSERT(ret == 0);
Paul Bakker2397cf32013-09-08 15:58:15 +0200536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 pem_len = strlen((char *) buf);
Paul Bakker2397cf32013-09-08 15:58:15 +0200538
Paul Elliott557b8d62020-11-19 09:46:56 +0000539 // check that the rest of the buffer remains clear
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
541 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000542 }
543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 if (issuer_key_type != MBEDTLS_PK_RSA) {
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100545 mbedtls_x509_crt crt_parse, trusted;
546 uint32_t flags;
Paul Bakker2397cf32013-09-08 15:58:15 +0200547
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 mbedtls_x509_crt_init(&crt_parse);
549 mbedtls_x509_crt_init(&trusted);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted,
552 cert_verify_file) == 0);
553 TEST_ASSERT(mbedtls_x509_crt_parse(&crt_parse,
554 buf, sizeof(buf)) == 0);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 ret = mbedtls_x509_crt_verify(&crt_parse, &trusted, NULL, NULL, &flags,
557 NULL, NULL);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 mbedtls_x509_crt_free(&crt_parse);
560 mbedtls_x509_crt_free(&trusted);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 TEST_EQUAL(flags, 0);
563 TEST_EQUAL(ret, 0);
564 } else if (*cert_check_file != '\0') {
565 f = fopen(cert_check_file, "r");
566 TEST_ASSERT(f != NULL);
567 olen = fread(check_buf, 1, sizeof(check_buf), f);
568 fclose(f);
569 TEST_ASSERT(olen < sizeof(check_buf));
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 TEST_EQUAL(olen, pem_len);
572 TEST_ASSERT(olen >= pem_len - 1);
573 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100574 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 der_len = mbedtls_x509write_crt_der(&crt, buf, sizeof(buf),
577 mbedtls_test_rnd_pseudo_rand,
578 &rnd_info);
579 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100582 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 }
Andres AGe0af9952016-09-07 11:09:44 +0100584
Werner Lewisacd01e52022-05-10 12:23:13 +0100585 // Not testing against file, check date format
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (*cert_check_file == '\0') {
Werner Lewisacd01e52022-05-10 12:23:13 +0100587 // UTC tag if before 2050, 2 digits less for year
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 if (not_before[0] == '2' && (not_before[1] > '0' || not_before[2] > '4')) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100589 before_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 } else {
Werner Lewisacd01e52022-05-10 12:23:13 +0100591 before_tag = MBEDTLS_ASN1_UTC_TIME;
592 not_before += 2;
593 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if (not_after[0] == '2' && (not_after[1] > '0' || not_after[2] > '4')) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100595 after_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 } else {
Werner Lewisacd01e52022-05-10 12:23:13 +0100597 after_tag = MBEDTLS_ASN1_UTC_TIME;
598 not_after += 2;
599 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 end = buf + sizeof(buf);
601 for (p = end - der_len; p < end;) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100602 tag = *p++;
603 sz = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if (tag == MBEDTLS_ASN1_UTC_TIME || tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100605 // Check correct tag and time written
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 TEST_ASSERT(before_tag == tag);
607 TEST_ASSERT(memcmp(p, not_before, sz - 1) == 0);
Werner Lewisacd01e52022-05-10 12:23:13 +0100608 p += sz;
609 tag = *p++;
610 sz = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 TEST_ASSERT(after_tag == tag);
612 TEST_ASSERT(memcmp(p, not_after, sz - 1) == 0);
Werner Lewisacd01e52022-05-10 12:23:13 +0100613 break;
614 }
615 // Increment if long form ASN1 length
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 if (sz & 0x80) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100617 p += sz & 0x0F;
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 }
619 if (tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100620 p += sz;
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 }
Werner Lewisacd01e52022-05-10 12:23:13 +0100622 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 TEST_ASSERT(p < end);
Werner Lewisacd01e52022-05-10 12:23:13 +0100624 }
625
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100626#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge6ed23c2022-04-22 09:44:04 +0200627 // When using PSA crypto, RNG isn't controllable, result length isn't
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100628 // deterministic over multiple runs, removing a single byte isn't enough to
629 // go into the MBEDTLS_ERR_ASN1_BUF_TOO_SMALL error case
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 if (issuer_key_type != MBEDTLS_PK_RSA) {
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100631 der_len /= 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 } else
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100633#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 der_len -= 1;
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 ret = mbedtls_x509write_crt_der(&crt, buf, (size_t) (der_len),
637 mbedtls_test_rnd_pseudo_rand, &rnd_info);
638 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100639
Paul Bakkerbd51b262014-07-10 15:26:12 +0200640exit:
Andrzej Kurek5da1d752023-04-05 08:30:59 -0400641 mbedtls_asn1_free_named_data_list(&ext_san_dirname);
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 mbedtls_x509write_crt_free(&crt);
643 mbedtls_pk_free(&issuer_key_alt);
644 mbedtls_pk_free(&subject_key);
645 mbedtls_pk_free(&issuer_key);
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100646#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
647 mbedtls_mpi_free(&serial_mpi);
648#endif
Neil Armstrong98f899c2022-03-16 17:42:42 +0100649#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 psa_destroy_key(key_id);
Neil Armstrong98f899c2022-03-16 17:42:42 +0100651#endif
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100652 MD_OR_USE_PSA_DONE();
Paul Bakker2397cf32013-09-08 15:58:15 +0200653}
654/* END_CASE */
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200655
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100656/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_WRITE_C */
657void x509_set_serial_check()
658{
659 mbedtls_x509write_cert ctx;
660 uint8_t invalid_serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN + 1];
661
Valerio Setti569c1712023-04-19 14:53:36 +0200662 USE_PSA_INIT();
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100663 memset(invalid_serial, 0x01, sizeof(invalid_serial));
664
Valerio Settiea19d2d2023-01-09 17:21:17 +0100665#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100666 mbedtls_mpi serial_mpi;
667
668 mbedtls_mpi_init(&serial_mpi);
669 TEST_EQUAL(mbedtls_mpi_read_binary(&serial_mpi, invalid_serial,
670 sizeof(invalid_serial)), 0);
671 TEST_EQUAL(mbedtls_x509write_crt_set_serial(&ctx, &serial_mpi),
672 MBEDTLS_ERR_X509_BAD_INPUT_DATA);
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100673#endif
674
Valerio Settiaf4815c2023-01-26 17:43:09 +0100675 TEST_EQUAL(mbedtls_x509write_crt_set_serial_raw(&ctx, invalid_serial,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100676 sizeof(invalid_serial)),
677 MBEDTLS_ERR_X509_BAD_INPUT_DATA);
678
Valerio Settia87f8392023-01-26 18:00:50 +0100679exit:
680#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
681 mbedtls_mpi_free(&serial_mpi);
682#else
683 ;
684#endif
Valerio Setti569c1712023-04-19 14:53:36 +0200685 USE_PSA_DONE();
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100686}
687/* END_CASE */
688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100690void mbedtls_x509_string_to_names(char *name, char *parsed_name, int result
691 )
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200692{
693 int ret;
694 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 mbedtls_asn1_named_data *names = NULL;
696 mbedtls_x509_name parsed, *parsed_cur, *parsed_prv;
Manuel Pégourié-Gonnard4fd0b252015-06-26 14:15:48 +0200697 unsigned char buf[1024], out[1024], *c;
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200698
Valerio Setti569c1712023-04-19 14:53:36 +0200699 USE_PSA_INIT();
700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 memset(&parsed, 0, sizeof(parsed));
702 memset(out, 0, sizeof(out));
703 memset(buf, 0, sizeof(buf));
704 c = buf + sizeof(buf);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 ret = mbedtls_x509_string_to_names(&names, name);
707 TEST_ASSERT(ret == result);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 if (ret != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200710 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 }
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 ret = mbedtls_x509_write_names(&c, buf, names);
714 TEST_ASSERT(ret > 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 TEST_ASSERT(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
717 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) == 0);
718 TEST_ASSERT(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed) == 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 ret = mbedtls_x509_dn_gets((char *) out, sizeof(out), &parsed);
721 TEST_ASSERT(ret > 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 TEST_ASSERT(strcmp((char *) out, parsed_name) == 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200724
725exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 mbedtls_asn1_free_named_data_list(&names);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200727
728 parsed_cur = parsed.next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 while (parsed_cur != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200730 parsed_prv = parsed_cur;
731 parsed_cur = parsed_cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 mbedtls_free(parsed_prv);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200733 }
Valerio Setti569c1712023-04-19 14:53:36 +0200734 USE_PSA_DONE();
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200735}
736/* END_CASE */