blob: d1df9e391293be8f66a1ff0a6fa453bdfb9f0805 [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"
Sam Berry2bb3f4d2024-07-19 15:14:27 +01009#include "mbedtls/asn1.h"
Valerio Setti48e8fc72022-10-19 15:14:29 +020010#include "mbedtls/asn1write.h"
Valerio Setti178b5bd2023-02-13 10:04:28 +010011#include "mbedtls/pk.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010012#include "mbedtls/psa_util.h"
Manuel Pégourié-Gonnardfeb03962020-08-20 09:59:33 +020013
Michael Schustera3cc4632024-06-07 01:51:54 +020014#if defined(MBEDTLS_PEM_WRITE_C) && \
15 defined(MBEDTLS_X509_CRT_WRITE_C) && \
16 defined(MBEDTLS_X509_CRT_PARSE_C) && \
Elena Uziunaite9fc5be02024-09-04 18:12:59 +010017 defined(PSA_WANT_ALG_SHA_1) && \
Michael Schustera3cc4632024-06-07 01:51:54 +020018 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Michael Schuster54300d42024-06-04 02:30:22 +020019static int mbedtls_rsa_decrypt_func(void *ctx, size_t *olen,
Michael Schusterbd89b792024-06-04 02:41:10 +020020 const unsigned char *input, unsigned char *output,
21 size_t output_max_len)
Hanno Becker418a6222017-09-14 07:51:28 +010022{
Gilles Peskine449bd832023-01-11 14:50:10 +010023 return mbedtls_rsa_pkcs1_decrypt((mbedtls_rsa_context *) ctx, NULL, NULL,
24 olen, input, output, output_max_len);
Hanno Becker418a6222017-09-14 07:51:28 +010025}
Michael Schuster54300d42024-06-04 02:30:22 +020026static int mbedtls_rsa_sign_func(void *ctx,
Michael Schusterbd89b792024-06-04 02:41:10 +020027 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
28 mbedtls_md_type_t md_alg, unsigned int hashlen,
29 const unsigned char *hash, unsigned char *sig)
Hanno Becker418a6222017-09-14 07:51:28 +010030{
Gilles Peskine449bd832023-01-11 14:50:10 +010031 return mbedtls_rsa_pkcs1_sign((mbedtls_rsa_context *) ctx, f_rng, p_rng,
32 md_alg, hashlen, hash, sig);
Hanno Becker418a6222017-09-14 07:51:28 +010033}
Michael Schuster54300d42024-06-04 02:30:22 +020034static size_t mbedtls_rsa_key_len_func(void *ctx)
Hanno Becker418a6222017-09-14 07:51:28 +010035{
Gilles Peskine449bd832023-01-11 14:50:10 +010036 return ((const mbedtls_rsa_context *) ctx)->len;
Hanno Becker418a6222017-09-14 07:51:28 +010037}
Michael Schustera3cc4632024-06-07 01:51:54 +020038#endif
Hanno Becker418a6222017-09-14 07:51:28 +010039
Gilles Peskinef4e672e2020-01-31 14:22:10 +010040#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
41 defined(MBEDTLS_PEM_WRITE_C) && defined(MBEDTLS_X509_CSR_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010042static int x509_crt_verifycsr(const unsigned char *buf, size_t buflen)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050043{
Przemek Stekiel54a54462022-08-01 13:59:12 +020044 unsigned char hash[PSA_HASH_MAX_SIZE];
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050045 mbedtls_x509_csr csr;
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010046 int ret = 0;
47
Gilles Peskine449bd832023-01-11 14:50:10 +010048 mbedtls_x509_csr_init(&csr);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050049
Gilles Peskine449bd832023-01-11 14:50:10 +010050 if (mbedtls_x509_csr_parse(&csr, buf, buflen) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010051 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
52 goto cleanup;
53 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050054
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020055 psa_algorithm_t psa_alg = mbedtls_md_psa_alg_from_type(csr.sig_md);
Przemek Stekiel54a54462022-08-01 13:59:12 +020056 size_t hash_size = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010057 psa_status_t status = psa_hash_compute(psa_alg, csr.cri.p, csr.cri.len,
58 hash, PSA_HASH_MAX_SIZE, &hash_size);
Przemek Stekiel54a54462022-08-01 13:59:12 +020059
Gilles Peskine449bd832023-01-11 14:50:10 +010060 if (status != PSA_SUCCESS) {
Andrzej Kurek4b114072018-11-19 18:04:01 -050061 /* Note: this can't happen except after an internal error */
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010062 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
63 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050064 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050065
Gilles Peskine449bd832023-01-11 14:50:10 +010066 if (mbedtls_pk_verify_ext(csr.sig_pk, csr.sig_opts, &csr.pk,
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +020067 csr.sig_md, hash, mbedtls_md_get_size_from_type(csr.sig_md),
Gilles Peskine449bd832023-01-11 14:50:10 +010068 csr.sig.p, csr.sig.len) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010069 ret = MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
70 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050071 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050072
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010073cleanup:
74
Gilles Peskine449bd832023-01-11 14:50:10 +010075 mbedtls_x509_csr_free(&csr);
76 return ret;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050077}
Gilles Peskinef4e672e2020-01-31 14:22:10 +010078#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_PEM_WRITE_C && MBEDTLS_X509_CSR_WRITE_C */
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050079
Valerio Setti48e8fc72022-10-19 15:14:29 +020080#if defined(MBEDTLS_X509_CSR_WRITE_C)
81
82/*
83 * The size of this temporary buffer is given by the sequence of functions
84 * called hereinafter:
85 * - mbedtls_asn1_write_oid()
86 * - 8 bytes for MBEDTLS_OID_EXTENDED_KEY_USAGE raw value
87 * - 1 byte for MBEDTLS_OID_EXTENDED_KEY_USAGE length
88 * - 1 byte for MBEDTLS_ASN1_OID tag
89 * - mbedtls_asn1_write_len()
90 * - 1 byte since we're dealing with sizes which are less than 0x80
91 * - mbedtls_asn1_write_tag()
92 * - 1 byte
93 *
94 * This length is fine as long as this function is called using the
95 * MBEDTLS_OID_SERVER_AUTH OID. If this is changed in the future, then this
96 * buffer's length should be adjusted accordingly.
97 * Unfortunately there's no predefined max size for OIDs which can be used
98 * to set an overall upper boundary which is always guaranteed.
99 */
100#define EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH 12
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102static int csr_set_extended_key_usage(mbedtls_x509write_csr *ctx,
103 const char *oid, size_t oid_len)
Valerio Setti48e8fc72022-10-19 15:14:29 +0200104{
105 unsigned char buf[EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH] = { 0 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 unsigned char *p = buf + sizeof(buf);
Valerio Setti48e8fc72022-10-19 15:14:29 +0200107 int ret;
108 size_t len = 0;
109
110 /*
111 * Following functions fail anyway if the temporary buffer is not large,
112 * but we set an extra check here to emphasize a possible source of errors
113 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 if (oid_len > EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH) {
Valerio Setti48e8fc72022-10-19 15:14:29 +0200115 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
116 }
117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(&p, buf, oid, oid_len));
119 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, ret));
120 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, buf,
121 MBEDTLS_ASN1_CONSTRUCTED |
122 MBEDTLS_ASN1_SEQUENCE));
Valerio Setti48e8fc72022-10-19 15:14:29 +0200123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 ret = mbedtls_x509write_csr_set_extension(ctx,
125 MBEDTLS_OID_EXTENDED_KEY_USAGE,
126 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
127 0,
128 p,
129 len);
Valerio Setti48e8fc72022-10-19 15:14:29 +0200130
131 return ret;
132}
133#endif /* MBEDTLS_X509_CSR_WRITE_C */
Gilles Peskinec94500b2023-09-21 18:01:05 +0200134
135/* Due to inconsistencies in the input size limits applied by different
136 * library functions, some write-parse tests may fail. */
137#define MAY_FAIL_GET_NAME 0x0001
138#define MAY_FAIL_DN_GETS 0x0002
139
Paul Bakker33b43f12013-08-20 11:48:36 +0200140/* END_HEADER */
Paul Bakker6d620502012-02-16 14:09:13 +0000141
Paul Bakker33b43f12013-08-20 11:48:36 +0200142/* BEGIN_DEPENDENCIES
Valerio Setti3580f442023-07-27 10:19:53 +0200143 * depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_PARSE_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200144 * END_DEPENDENCIES
145 */
Paul Bakker6d620502012-02-16 14:09:13 +0000146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148void x509_csr_check(char *key_file, char *cert_req_check_file, int md_type,
149 int key_usage, int set_key_usage, int cert_type,
150 int set_cert_type, int set_extension)
Paul Bakker6d620502012-02-16 14:09:13 +0000151{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_pk_context key;
153 mbedtls_x509write_csr req;
Andres AGe0af9952016-09-07 11:09:44 +0100154 unsigned char buf[4096];
Paul Bakker6d620502012-02-16 14:09:13 +0000155 int ret;
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100156#if !defined(MBEDTLS_USE_PSA_CRYPTO)
157 unsigned char check_buf[4000];
Paul Bakker6d620502012-02-16 14:09:13 +0000158 FILE *f;
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100159 size_t olen = 0;
160#endif /* !MBEDTLS_USE_PSA_CRYPTO */
161 size_t pem_len = 0, buf_index;
162 int der_len = -1;
Paul Bakker3a8cb6f2013-12-30 20:41:54 +0100163 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200164 mbedtls_test_rnd_pseudo_info rnd_info;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100165 mbedtls_x509_san_list san_ip;
166 mbedtls_x509_san_list san_dns;
167 mbedtls_x509_san_list san_uri;
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400168 mbedtls_x509_san_list san_mail;
169 mbedtls_x509_san_list san_dn;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100170 mbedtls_x509_san_list *san_list = NULL;
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400171 mbedtls_asn1_named_data *ext_san_dirname = NULL;
172
173 const char san_ip_name[] = { 0x7f, 0x00, 0x00, 0x01 }; // 127.0.0.1
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100174 const char *san_dns_name = "example.com";
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400175 const char *san_dn_name = "C=UK,O=Mbed TLS,CN=Mbed TLS directoryName SAN";
176 const char *san_mail_name = "mail@example.com";
177 const char *san_uri_name = "http://pki.example.com";
178
179 san_mail.node.type = MBEDTLS_X509_SAN_RFC822_NAME;
180 san_mail.node.san.unstructured_name.p = (unsigned char *) san_mail_name;
181 san_mail.node.san.unstructured_name.len = strlen(san_mail_name);
182 san_mail.next = NULL;
183
184 san_dns.node.type = MBEDTLS_X509_SAN_DNS_NAME;
185 san_dns.node.san.unstructured_name.p = (unsigned char *) san_dns_name;
186 san_dns.node.san.unstructured_name.len = strlen(san_dns_name);
187 san_dns.next = &san_mail;
188
189 san_dn.node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
190 TEST_ASSERT(mbedtls_x509_string_to_names(&ext_san_dirname,
191 san_dn_name) == 0);
192 san_dn.node.san.directory_name = *ext_san_dirname;
193 san_dn.next = &san_dns;
194
195 san_ip.node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
196 san_ip.node.san.unstructured_name.p = (unsigned char *) san_ip_name;
197 san_ip.node.san.unstructured_name.len = sizeof(san_ip_name);
198 san_ip.next = &san_dn;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100199
200 san_uri.node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100201 san_uri.node.san.unstructured_name.p = (unsigned char *) san_uri_name;
202 san_uri.node.san.unstructured_name.len = strlen(san_uri_name);
Andrzej Kurek34ccd8d2023-07-07 06:32:17 -0400203 san_uri.next = &san_ip;
204
205 san_list = &san_uri;
Paul Bakker6d620502012-02-16 14:09:13 +0000206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 mbedtls_x509write_csr_init(&req);
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 mbedtls_pk_init(&key);
valerio32f2ac92023-04-20 11:59:52 +0200211 MD_OR_USE_PSA_INIT();
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL,
214 mbedtls_test_rnd_std_rand, NULL) == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 mbedtls_x509write_csr_set_md_alg(&req, md_type);
217 mbedtls_x509write_csr_set_key(&req, &key);
218 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
219 if (set_key_usage != 0) {
220 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
221 }
222 if (set_cert_type != 0) {
223 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
224 }
225 if (set_extension != 0) {
226 TEST_ASSERT(csr_set_extended_key_usage(&req, MBEDTLS_OID_SERVER_AUTH,
227 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) == 0);
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100228
229 TEST_ASSERT(mbedtls_x509write_csr_set_subject_alternative_name(&req, san_list) == 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 }
Paul Bakker8eabfc12013-08-25 10:18:25 +0200231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf),
233 mbedtls_test_rnd_pseudo_rand, &rnd_info);
234 TEST_ASSERT(ret == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 pem_len = strlen((char *) buf);
Paul Bakker6d620502012-02-16 14:09:13 +0000237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
239 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000240 }
241
Neil Armstrong5b320382022-02-21 17:22:10 +0100242#if defined(MBEDTLS_USE_PSA_CRYPTO)
243 // When using PSA crypto, RNG isn't controllable, so cert_req_check_file can't be used
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 (void) cert_req_check_file;
Neil Armstrong5b320382022-02-21 17:22:10 +0100245 buf[pem_len] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0);
Neil Armstrong5b320382022-02-21 17:22:10 +0100247#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 f = fopen(cert_req_check_file, "r");
249 TEST_ASSERT(f != NULL);
250 olen = fread(check_buf, 1, sizeof(check_buf), f);
251 fclose(f);
Paul Bakker6d620502012-02-16 14:09:13 +0000252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 TEST_ASSERT(olen >= pem_len - 1);
254 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100255#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 der_len = mbedtls_x509write_csr_der(&req, buf, sizeof(buf),
258 mbedtls_test_rnd_pseudo_rand,
259 &rnd_info);
260 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100263 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 }
Andres AGe0af9952016-09-07 11:09:44 +0100265
Neil Armstrong5b320382022-02-21 17:22:10 +0100266#if defined(MBEDTLS_USE_PSA_CRYPTO)
267 // When using PSA crypto, RNG isn't controllable, result length isn't
268 // deterministic over multiple runs, removing a single byte isn't enough to
269 // go into the MBEDTLS_ERR_ASN1_BUF_TOO_SMALL error case
270 der_len /= 2;
271#else
272 der_len -= 1;
273#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 ret = mbedtls_x509write_csr_der(&req, buf, (size_t) (der_len),
275 mbedtls_test_rnd_pseudo_rand, &rnd_info);
276 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100277
Paul Bakkerbd51b262014-07-10 15:26:12 +0200278exit:
Andrzej Kurekbdb41dd2023-07-10 08:09:50 -0400279 mbedtls_asn1_free_named_data_list(&ext_san_dirname);
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 mbedtls_x509write_csr_free(&req);
281 mbedtls_pk_free(&key);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100282 MD_OR_USE_PSA_DONE();
Paul Bakker6d620502012-02-16 14:09:13 +0000283}
Paul Bakker33b43f12013-08-20 11:48:36 +0200284/* END_CASE */
Paul Bakker2397cf32013-09-08 15:58:15 +0200285
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500286/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C:MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100287void x509_csr_check_opaque(char *key_file, int md_type, int key_usage,
288 int cert_type)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500289{
290 mbedtls_pk_context key;
Paul Elliott9a209b82024-10-25 12:41:28 +0100291 mbedtls_pk_init(&key);
292
Ronald Cron5425a212020-08-04 14:58:35 +0200293 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100294 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliott9a209b82024-10-25 12:41:28 +0100295
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500296 mbedtls_x509write_csr req;
Paul Elliott9a209b82024-10-25 12:41:28 +0100297 mbedtls_x509write_csr_init(&req);
298
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500299 unsigned char buf[4096];
300 int ret;
301 size_t pem_len = 0;
302 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200303 mbedtls_test_rnd_pseudo_info rnd_info;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500304
Valerio Setti569c1712023-04-19 14:53:36 +0200305 MD_OR_USE_PSA_INIT();
306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL,
310 mbedtls_test_rnd_std_rand, NULL) == 0);
Neil Armstrong95974972022-04-22 13:57:44 +0200311
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100312 /* Turn the PK context into an opaque one. */
313 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&key, PSA_KEY_USAGE_SIGN_HASH, &key_attr), 0);
314 TEST_EQUAL(mbedtls_pk_import_into_psa(&key, &key_attr, &key_id), 0);
315 mbedtls_pk_free(&key);
316 mbedtls_pk_init(&key);
317 TEST_EQUAL(mbedtls_pk_setup_opaque(&key, key_id), 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
Elena Uziunaite9fc5be02024-09-04 18:12:59 +0100347/* 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 +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;
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100376 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong98f899c2022-03-16 17:42:42 +0100377#endif
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100378 mbedtls_pk_type_t issuer_key_type;
Andrzej Kurek76c96622023-04-04 06:57:08 -0400379 mbedtls_x509_san_list san_ip;
380 mbedtls_x509_san_list san_dns;
381 mbedtls_x509_san_list san_uri;
382 mbedtls_x509_san_list san_mail;
383 mbedtls_x509_san_list san_dn;
384 mbedtls_asn1_named_data *ext_san_dirname = NULL;
385 const char san_ip_name[] = { 0x01, 0x02, 0x03, 0x04 };
386 const char *san_dns_name = "example.com";
387 const char *san_dn_name = "C=UK,O=Mbed TLS,CN=SubjectAltName test";
388 const char *san_mail_name = "mail@example.com";
389 const char *san_uri_name = "http://pki.example.com";
390 mbedtls_x509_san_list *san_list = NULL;
391
392 if (set_subjectAltNames) {
393 san_mail.node.type = MBEDTLS_X509_SAN_RFC822_NAME;
394 san_mail.node.san.unstructured_name.p = (unsigned char *) san_mail_name;
Andrzej Kurek13c43f62023-04-04 10:43:38 -0400395 san_mail.node.san.unstructured_name.len = strlen(san_mail_name);
Andrzej Kurek76c96622023-04-04 06:57:08 -0400396 san_mail.next = NULL;
397
398 san_dns.node.type = MBEDTLS_X509_SAN_DNS_NAME;
399 san_dns.node.san.unstructured_name.p = (unsigned char *) san_dns_name;
400 san_dns.node.san.unstructured_name.len = strlen(san_dns_name);
401 san_dns.next = &san_mail;
402
403 san_dn.node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
404 TEST_ASSERT(mbedtls_x509_string_to_names(&ext_san_dirname,
405 san_dn_name) == 0);
406 san_dn.node.san.directory_name = *ext_san_dirname;
407 san_dn.next = &san_dns;
408
409 san_ip.node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
410 san_ip.node.san.unstructured_name.p = (unsigned char *) san_ip_name;
411 san_ip.node.san.unstructured_name.len = sizeof(san_ip_name);
412 san_ip.next = &san_dn;
413
414 san_uri.node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
415 san_uri.node.san.unstructured_name.p = (unsigned char *) san_uri_name;
416 san_uri.node.san.unstructured_name.len = strlen(san_uri_name);
417 san_uri.next = &san_ip;
418
419 san_list = &san_uri;
420 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100423#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
424 mbedtls_mpi_init(&serial_mpi);
425#endif
Hanno Becker418a6222017-09-14 07:51:28 +0100426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 mbedtls_pk_init(&subject_key);
428 mbedtls_pk_init(&issuer_key);
429 mbedtls_pk_init(&issuer_key_alt);
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 mbedtls_x509write_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200431 MD_OR_USE_PSA_INIT();
Paul Bakker2397cf32013-09-08 15:58:15 +0200432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 TEST_ASSERT(mbedtls_pk_parse_keyfile(&subject_key, subject_key_file,
434 subject_pwd, mbedtls_test_rnd_std_rand, NULL) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 TEST_ASSERT(mbedtls_pk_parse_keyfile(&issuer_key, issuer_key_file,
437 issuer_pwd, mbedtls_test_rnd_std_rand, NULL) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 issuer_key_type = mbedtls_pk_get_type(&issuer_key);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100440
Dave Rodgmandc3b1542023-01-20 11:39:00 +0000441#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Hanno Becker418a6222017-09-14 07:51:28 +0100442 /* For RSA PK contexts, create a copy as an alternative RSA context. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (pk_wrap == 1 && issuer_key_type == MBEDTLS_PK_RSA) {
444 TEST_ASSERT(mbedtls_pk_setup_rsa_alt(&issuer_key_alt,
445 mbedtls_pk_rsa(issuer_key),
446 mbedtls_rsa_decrypt_func,
447 mbedtls_rsa_sign_func,
448 mbedtls_rsa_key_len_func) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100449
450 key = &issuer_key_alt;
451 }
Manuel Pégourié-Gonnard147b28e2018-03-12 15:26:59 +0100452#endif
Hanno Becker418a6222017-09-14 07:51:28 +0100453
Neil Armstrong98f899c2022-03-16 17:42:42 +0100454#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100455 /* Turn the issuer PK context into an opaque one. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 if (pk_wrap == 2) {
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100457 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&issuer_key, PSA_KEY_USAGE_SIGN_HASH,
458 &key_attr), 0);
459 TEST_EQUAL(mbedtls_pk_import_into_psa(&issuer_key, &key_attr, &key_id), 0);
460 mbedtls_pk_free(&issuer_key);
461 mbedtls_pk_init(&issuer_key);
462 TEST_EQUAL(mbedtls_pk_setup_opaque(&issuer_key, key_id), 0);
Neil Armstrong98f899c2022-03-16 17:42:42 +0100463 }
464#endif /* MBEDTLS_USE_PSA_CRYPTO */
465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 if (pk_wrap == 2) {
467 TEST_ASSERT(mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_OPAQUE);
468 }
Neil Armstrong98f899c2022-03-16 17:42:42 +0100469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (ver != -1) {
471 mbedtls_x509write_crt_set_version(&crt, ver);
472 }
Hanno Becker418a6222017-09-14 07:51:28 +0100473
Valerio Settiaf4815c2023-01-26 17:43:09 +0100474 TEST_ASSERT(mbedtls_x509write_crt_set_serial_raw(&crt, serial_arg->x,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100475 serial_arg->len) == 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 TEST_ASSERT(mbedtls_x509write_crt_set_validity(&crt, not_before,
477 not_after) == 0);
478 mbedtls_x509write_crt_set_md_alg(&crt, md_type);
479 TEST_ASSERT(mbedtls_x509write_crt_set_issuer_name(&crt, issuer_name) == 0);
480 TEST_ASSERT(mbedtls_x509write_crt_set_subject_name(&crt, subject_name) == 0);
481 mbedtls_x509write_crt_set_subject_key(&crt, &subject_key);
Hanno Becker418a6222017-09-14 07:51:28 +0100482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 mbedtls_x509write_crt_set_issuer_key(&crt, key);
Paul Bakker2397cf32013-09-08 15:58:15 +0200484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (crt.version >= MBEDTLS_X509_CRT_VERSION_3) {
Darren Krahn9c134ce2021-01-13 22:04:45 -0800486 /* For the CA case, a path length of -1 means unlimited. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 TEST_ASSERT(mbedtls_x509write_crt_set_basic_constraints(&crt, is_ca,
488 (is_ca ? -1 : 0)) == 0);
489 TEST_ASSERT(mbedtls_x509write_crt_set_subject_key_identifier(&crt) == 0);
490 if (auth_ident) {
491 TEST_ASSERT(mbedtls_x509write_crt_set_authority_key_identifier(&crt) == 0);
492 }
493 if (set_key_usage != 0) {
494 TEST_ASSERT(mbedtls_x509write_crt_set_key_usage(&crt, key_usage) == 0);
495 }
496 if (set_cert_type != 0) {
497 TEST_ASSERT(mbedtls_x509write_crt_set_ns_cert_type(&crt, cert_type) == 0);
498 }
499 if (strcmp(ext_key_usage, "NULL") != 0) {
Dave Rodgmanec9f6b42022-07-27 14:34:58 +0100500 mbedtls_asn1_sequence exts[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 memset(exts, 0, sizeof(exts));
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100502
503#define SET_OID(x, oid) \
504 do { \
505 x.len = MBEDTLS_OID_SIZE(oid); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 x.p = (unsigned char *) oid; \
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100507 x.tag = MBEDTLS_ASN1_OID; \
Dave Rodgmane2b772d2022-08-11 16:04:13 +0100508 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 while (0)
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 if (strcmp(ext_key_usage, "serverAuth") == 0) {
512 SET_OID(exts[0].buf, MBEDTLS_OID_SERVER_AUTH);
513 } else if (strcmp(ext_key_usage, "codeSigning,timeStamping") == 0) {
514 SET_OID(exts[0].buf, MBEDTLS_OID_CODE_SIGNING);
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100515 exts[0].next = &exts[1];
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 SET_OID(exts[1].buf, MBEDTLS_OID_TIME_STAMPING);
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000517 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 TEST_ASSERT(mbedtls_x509write_crt_set_ext_key_usage(&crt, exts) == 0);
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000519 }
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100520 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200521
Andrzej Kurek76c96622023-04-04 06:57:08 -0400522 if (set_subjectAltNames) {
523 TEST_ASSERT(mbedtls_x509write_crt_set_subject_alternative_name(&crt, san_list) == 0);
524 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 ret = mbedtls_x509write_crt_pem(&crt, buf, sizeof(buf),
526 mbedtls_test_rnd_pseudo_rand, &rnd_info);
527 TEST_ASSERT(ret == 0);
Paul Bakker2397cf32013-09-08 15:58:15 +0200528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 pem_len = strlen((char *) buf);
Paul Bakker2397cf32013-09-08 15:58:15 +0200530
Paul Elliott557b8d62020-11-19 09:46:56 +0000531 // check that the rest of the buffer remains clear
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
533 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000534 }
535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if (issuer_key_type != MBEDTLS_PK_RSA) {
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100537 mbedtls_x509_crt crt_parse, trusted;
538 uint32_t flags;
Paul Bakker2397cf32013-09-08 15:58:15 +0200539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 mbedtls_x509_crt_init(&crt_parse);
541 mbedtls_x509_crt_init(&trusted);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted,
544 cert_verify_file) == 0);
545 TEST_ASSERT(mbedtls_x509_crt_parse(&crt_parse,
546 buf, sizeof(buf)) == 0);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100547
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 ret = mbedtls_x509_crt_verify(&crt_parse, &trusted, NULL, NULL, &flags,
549 NULL, NULL);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 mbedtls_x509_crt_free(&crt_parse);
552 mbedtls_x509_crt_free(&trusted);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 TEST_EQUAL(flags, 0);
555 TEST_EQUAL(ret, 0);
556 } else if (*cert_check_file != '\0') {
557 f = fopen(cert_check_file, "r");
558 TEST_ASSERT(f != NULL);
559 olen = fread(check_buf, 1, sizeof(check_buf), f);
560 fclose(f);
561 TEST_ASSERT(olen < sizeof(check_buf));
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 TEST_EQUAL(olen, pem_len);
564 TEST_ASSERT(olen >= pem_len - 1);
565 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100566 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 der_len = mbedtls_x509write_crt_der(&crt, buf, sizeof(buf),
569 mbedtls_test_rnd_pseudo_rand,
570 &rnd_info);
571 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100574 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 }
Andres AGe0af9952016-09-07 11:09:44 +0100576
Werner Lewisacd01e52022-05-10 12:23:13 +0100577 // Not testing against file, check date format
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if (*cert_check_file == '\0') {
Werner Lewisacd01e52022-05-10 12:23:13 +0100579 // UTC tag if before 2050, 2 digits less for year
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 if (not_before[0] == '2' && (not_before[1] > '0' || not_before[2] > '4')) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100581 before_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 } else {
Werner Lewisacd01e52022-05-10 12:23:13 +0100583 before_tag = MBEDTLS_ASN1_UTC_TIME;
584 not_before += 2;
585 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (not_after[0] == '2' && (not_after[1] > '0' || not_after[2] > '4')) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100587 after_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 } else {
Werner Lewisacd01e52022-05-10 12:23:13 +0100589 after_tag = MBEDTLS_ASN1_UTC_TIME;
590 not_after += 2;
591 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 end = buf + sizeof(buf);
593 for (p = end - der_len; p < end;) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100594 tag = *p++;
595 sz = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (tag == MBEDTLS_ASN1_UTC_TIME || tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100597 // Check correct tag and time written
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 TEST_ASSERT(before_tag == tag);
599 TEST_ASSERT(memcmp(p, not_before, sz - 1) == 0);
Werner Lewisacd01e52022-05-10 12:23:13 +0100600 p += sz;
601 tag = *p++;
602 sz = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 TEST_ASSERT(after_tag == tag);
604 TEST_ASSERT(memcmp(p, not_after, sz - 1) == 0);
Werner Lewisacd01e52022-05-10 12:23:13 +0100605 break;
606 }
607 // Increment if long form ASN1 length
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (sz & 0x80) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100609 p += sz & 0x0F;
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 }
611 if (tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100612 p += sz;
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 }
Werner Lewisacd01e52022-05-10 12:23:13 +0100614 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 TEST_ASSERT(p < end);
Werner Lewisacd01e52022-05-10 12:23:13 +0100616 }
617
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100618#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge6ed23c2022-04-22 09:44:04 +0200619 // When using PSA crypto, RNG isn't controllable, result length isn't
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100620 // deterministic over multiple runs, removing a single byte isn't enough to
621 // go into the MBEDTLS_ERR_ASN1_BUF_TOO_SMALL error case
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 if (issuer_key_type != MBEDTLS_PK_RSA) {
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100623 der_len /= 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 } else
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100625#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 der_len -= 1;
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 ret = mbedtls_x509write_crt_der(&crt, buf, (size_t) (der_len),
629 mbedtls_test_rnd_pseudo_rand, &rnd_info);
630 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100631
Paul Bakkerbd51b262014-07-10 15:26:12 +0200632exit:
Andrzej Kurek5da1d752023-04-05 08:30:59 -0400633 mbedtls_asn1_free_named_data_list(&ext_san_dirname);
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 mbedtls_x509write_crt_free(&crt);
635 mbedtls_pk_free(&issuer_key_alt);
636 mbedtls_pk_free(&subject_key);
637 mbedtls_pk_free(&issuer_key);
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100638#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
639 mbedtls_mpi_free(&serial_mpi);
640#endif
Neil Armstrong98f899c2022-03-16 17:42:42 +0100641#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 psa_destroy_key(key_id);
Neil Armstrong98f899c2022-03-16 17:42:42 +0100643#endif
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100644 MD_OR_USE_PSA_DONE();
Paul Bakker2397cf32013-09-08 15:58:15 +0200645}
646/* END_CASE */
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200647
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100648/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_WRITE_C */
649void x509_set_serial_check()
650{
651 mbedtls_x509write_cert ctx;
652 uint8_t invalid_serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN + 1];
653
David Horstmanne04a97a2023-12-08 18:27:48 +0000654#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
655 mbedtls_mpi serial_mpi;
656 mbedtls_mpi_init(&serial_mpi);
657#endif
658
Valerio Setti569c1712023-04-19 14:53:36 +0200659 USE_PSA_INIT();
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100660 memset(invalid_serial, 0x01, sizeof(invalid_serial));
661
Valerio Settiaf4815c2023-01-26 17:43:09 +0100662 TEST_EQUAL(mbedtls_x509write_crt_set_serial_raw(&ctx, invalid_serial,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100663 sizeof(invalid_serial)),
664 MBEDTLS_ERR_X509_BAD_INPUT_DATA);
665
Valerio Settia87f8392023-01-26 18:00:50 +0100666exit:
667#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
668 mbedtls_mpi_free(&serial_mpi);
669#else
670 ;
671#endif
Valerio Setti569c1712023-04-19 14:53:36 +0200672 USE_PSA_DONE();
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100673}
674/* END_CASE */
675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C */
Gilles Peskinec94500b2023-09-21 18:01:05 +0200677void mbedtls_x509_string_to_names(char *name, char *parsed_name,
678 int result, int may_fail)
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200679{
680 int ret;
681 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 mbedtls_asn1_named_data *names = NULL;
Gilles Peskine21e46b32023-10-17 16:35:20 +0200683 mbedtls_x509_name parsed;
684 memset(&parsed, 0, sizeof(parsed));
685 mbedtls_x509_name *parsed_cur = NULL;
686 mbedtls_x509_name *parsed_prv = NULL;
Gilles Peskinef2574202023-10-18 17:39:48 +0200687 unsigned char buf[1024] = { 0 };
688 unsigned char out[1024] = { 0 };
Gilles Peskine21e46b32023-10-17 16:35:20 +0200689 unsigned char *c = buf + sizeof(buf);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200690
Valerio Setti569c1712023-04-19 14:53:36 +0200691 USE_PSA_INIT();
692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 ret = mbedtls_x509_string_to_names(&names, name);
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200694 TEST_EQUAL(ret, result);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (ret != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200697 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 }
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 ret = mbedtls_x509_write_names(&c, buf, names);
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200701 TEST_LE_S(1, ret);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200702
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200703 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
Gilles Peskineaa01a032023-09-21 15:57:30 +0200704 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
Gilles Peskinec94500b2023-09-21 18:01:05 +0200705 ret = mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed);
706 if ((may_fail & MAY_FAIL_GET_NAME) && ret < 0) {
707 /* Validation inconsistency between mbedtls_x509_string_to_names() and
708 * mbedtls_x509_get_name(). Accept it for now. */
709 goto exit;
710 }
711 TEST_EQUAL(ret, 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 ret = mbedtls_x509_dn_gets((char *) out, sizeof(out), &parsed);
Gilles Peskinec94500b2023-09-21 18:01:05 +0200714 if ((may_fail & MAY_FAIL_DN_GETS) && ret < 0) {
715 /* Validation inconsistency between mbedtls_x509_string_to_names() and
716 * mbedtls_x509_dn_gets(). Accept it for now. */
717 goto exit;
718 }
Gilles Peskine1c7223b2023-09-21 14:02:05 +0200719 TEST_LE_S(1, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 TEST_ASSERT(strcmp((char *) out, parsed_name) == 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200721
722exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 mbedtls_asn1_free_named_data_list(&names);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200724
725 parsed_cur = parsed.next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 while (parsed_cur != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200727 parsed_prv = parsed_cur;
728 parsed_cur = parsed_cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 mbedtls_free(parsed_prv);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200730 }
Valerio Setti569c1712023-04-19 14:53:36 +0200731 USE_PSA_DONE();
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200732}
733/* END_CASE */
Jonathan Winzig2bd2b782024-01-09 15:19:42 +0100734
Jonathan Winzig315c3ca2024-01-09 18:31:11 +0100735/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_WRITE_C */
Jonathan Winzig6c9779f2024-01-09 17:47:10 +0100736void x509_set_extension_length_check()
Jonathan Winzig2bd2b782024-01-09 15:19:42 +0100737{
738 int ret = 0;
739
740 mbedtls_x509write_csr ctx;
741 mbedtls_x509write_csr_init(&ctx);
742
743 unsigned char buf[EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH] = { 0 };
744 unsigned char *p = buf + sizeof(buf);
745
746 ret = mbedtls_x509_set_extension(&(ctx.MBEDTLS_PRIVATE(extensions)),
747 MBEDTLS_OID_EXTENDED_KEY_USAGE,
748 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
749 0,
750 p,
Jonathan Winzig6c9779f2024-01-09 17:47:10 +0100751 SIZE_MAX);
752 TEST_ASSERT(MBEDTLS_ERR_X509_BAD_INPUT_DATA == ret);
Jonathan Winzig2bd2b782024-01-09 15:19:42 +0100753}
754/* END_CASE */
Sam Berry2bb3f4d2024-07-19 15:14:27 +0100755
756/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
757void oid_from_numeric_string(char *oid_str, int error_ret,
758 data_t *exp_oid_buf)
759{
760 mbedtls_asn1_buf oid = { 0, 0, NULL };
761 mbedtls_asn1_buf exp_oid = { 0, 0, NULL };
762 int ret;
763
764 exp_oid.tag = MBEDTLS_ASN1_OID;
765 exp_oid.p = exp_oid_buf->x;
766 exp_oid.len = exp_oid_buf->len;
767
768 ret = mbedtls_oid_from_numeric_string(&oid, oid_str, strlen(oid_str));
769
770 if (error_ret == 0) {
771 TEST_EQUAL(oid.len, exp_oid.len);
772 TEST_ASSERT(memcmp(oid.p, exp_oid.p, oid.len) == 0);
773 mbedtls_free(oid.p);
774 oid.p = NULL;
775 oid.len = 0;
776 } else {
777 TEST_EQUAL(ret, error_ret);
778 }
779}
780/* END_CASE */