blob: c3b67684db4846a2dd7d38edb3280d8044f68019 [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é-Gonnardfeb03962020-08-20 09:59:33 +020010
Hanno Becker418a6222017-09-14 07:51:28 +010011#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010012int mbedtls_rsa_decrypt_func(void *ctx, size_t *olen,
13 const unsigned char *input, unsigned char *output,
14 size_t output_max_len)
Hanno Becker418a6222017-09-14 07:51:28 +010015{
Gilles Peskine449bd832023-01-11 14:50:10 +010016 return mbedtls_rsa_pkcs1_decrypt((mbedtls_rsa_context *) ctx, NULL, NULL,
17 olen, input, output, output_max_len);
Hanno Becker418a6222017-09-14 07:51:28 +010018}
Gilles Peskine449bd832023-01-11 14:50:10 +010019int mbedtls_rsa_sign_func(void *ctx,
20 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
21 mbedtls_md_type_t md_alg, unsigned int hashlen,
22 const unsigned char *hash, unsigned char *sig)
Hanno Becker418a6222017-09-14 07:51:28 +010023{
Gilles Peskine449bd832023-01-11 14:50:10 +010024 return mbedtls_rsa_pkcs1_sign((mbedtls_rsa_context *) ctx, f_rng, p_rng,
25 md_alg, hashlen, hash, sig);
Hanno Becker418a6222017-09-14 07:51:28 +010026}
Gilles Peskine449bd832023-01-11 14:50:10 +010027size_t mbedtls_rsa_key_len_func(void *ctx)
Hanno Becker418a6222017-09-14 07:51:28 +010028{
Gilles Peskine449bd832023-01-11 14:50:10 +010029 return ((const mbedtls_rsa_context *) ctx)->len;
Hanno Becker418a6222017-09-14 07:51:28 +010030}
31#endif /* MBEDTLS_RSA_C */
32
Gilles Peskinef4e672e2020-01-31 14:22:10 +010033#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
34 defined(MBEDTLS_PEM_WRITE_C) && defined(MBEDTLS_X509_CSR_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010035static int x509_crt_verifycsr(const unsigned char *buf, size_t buflen)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050036{
Przemek Stekiel54a54462022-08-01 13:59:12 +020037 unsigned char hash[PSA_HASH_MAX_SIZE];
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050038 mbedtls_x509_csr csr;
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010039 int ret = 0;
40
Gilles Peskine449bd832023-01-11 14:50:10 +010041 mbedtls_x509_csr_init(&csr);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050042
Gilles Peskine449bd832023-01-11 14:50:10 +010043 if (mbedtls_x509_csr_parse(&csr, buf, buflen) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010044 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
45 goto cleanup;
46 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050047
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020048 psa_algorithm_t psa_alg = mbedtls_md_psa_alg_from_type(csr.sig_md);
Przemek Stekiel54a54462022-08-01 13:59:12 +020049 size_t hash_size = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010050 psa_status_t status = psa_hash_compute(psa_alg, csr.cri.p, csr.cri.len,
51 hash, PSA_HASH_MAX_SIZE, &hash_size);
Przemek Stekiel54a54462022-08-01 13:59:12 +020052
Gilles Peskine449bd832023-01-11 14:50:10 +010053 if (status != PSA_SUCCESS) {
Andrzej Kurek4b114072018-11-19 18:04:01 -050054 /* Note: this can't happen except after an internal error */
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010055 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
56 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050057 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050058
Gilles Peskine449bd832023-01-11 14:50:10 +010059 if (mbedtls_pk_verify_ext(csr.sig_pk, csr.sig_opts, &csr.pk,
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +020060 csr.sig_md, hash, mbedtls_md_get_size_from_type(csr.sig_md),
Gilles Peskine449bd832023-01-11 14:50:10 +010061 csr.sig.p, csr.sig.len) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010062 ret = MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
63 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050064 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050065
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010066cleanup:
67
Gilles Peskine449bd832023-01-11 14:50:10 +010068 mbedtls_x509_csr_free(&csr);
69 return ret;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050070}
Gilles Peskinef4e672e2020-01-31 14:22:10 +010071#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_PEM_WRITE_C && MBEDTLS_X509_CSR_WRITE_C */
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050072
Valerio Setti48e8fc72022-10-19 15:14:29 +020073#if defined(MBEDTLS_X509_CSR_WRITE_C)
74
75/*
76 * The size of this temporary buffer is given by the sequence of functions
77 * called hereinafter:
78 * - mbedtls_asn1_write_oid()
79 * - 8 bytes for MBEDTLS_OID_EXTENDED_KEY_USAGE raw value
80 * - 1 byte for MBEDTLS_OID_EXTENDED_KEY_USAGE length
81 * - 1 byte for MBEDTLS_ASN1_OID tag
82 * - mbedtls_asn1_write_len()
83 * - 1 byte since we're dealing with sizes which are less than 0x80
84 * - mbedtls_asn1_write_tag()
85 * - 1 byte
86 *
87 * This length is fine as long as this function is called using the
88 * MBEDTLS_OID_SERVER_AUTH OID. If this is changed in the future, then this
89 * buffer's length should be adjusted accordingly.
90 * Unfortunately there's no predefined max size for OIDs which can be used
91 * to set an overall upper boundary which is always guaranteed.
92 */
93#define EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH 12
94
Gilles Peskine449bd832023-01-11 14:50:10 +010095static int csr_set_extended_key_usage(mbedtls_x509write_csr *ctx,
96 const char *oid, size_t oid_len)
Valerio Setti48e8fc72022-10-19 15:14:29 +020097{
98 unsigned char buf[EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH] = { 0 };
Gilles Peskine449bd832023-01-11 14:50:10 +010099 unsigned char *p = buf + sizeof(buf);
Valerio Setti48e8fc72022-10-19 15:14:29 +0200100 int ret;
101 size_t len = 0;
102
103 /*
104 * Following functions fail anyway if the temporary buffer is not large,
105 * but we set an extra check here to emphasize a possible source of errors
106 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (oid_len > EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH) {
Valerio Setti48e8fc72022-10-19 15:14:29 +0200108 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
109 }
110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(&p, buf, oid, oid_len));
112 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, ret));
113 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, buf,
114 MBEDTLS_ASN1_CONSTRUCTED |
115 MBEDTLS_ASN1_SEQUENCE));
Valerio Setti48e8fc72022-10-19 15:14:29 +0200116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 ret = mbedtls_x509write_csr_set_extension(ctx,
118 MBEDTLS_OID_EXTENDED_KEY_USAGE,
119 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
120 0,
121 p,
122 len);
Valerio Setti48e8fc72022-10-19 15:14:29 +0200123
124 return ret;
125}
126#endif /* MBEDTLS_X509_CSR_WRITE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200127/* END_HEADER */
Paul Bakker6d620502012-02-16 14:09:13 +0000128
Paul Bakker33b43f12013-08-20 11:48:36 +0200129/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 * depends_on:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO:MBEDTLS_PK_PARSE_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200131 * END_DEPENDENCIES
132 */
Paul Bakker6d620502012-02-16 14:09:13 +0000133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100135void x509_csr_check(char *key_file, char *cert_req_check_file, int md_type,
136 int key_usage, int set_key_usage, int cert_type,
137 int set_cert_type, int set_extension)
Paul Bakker6d620502012-02-16 14:09:13 +0000138{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_pk_context key;
140 mbedtls_x509write_csr req;
Andres AGe0af9952016-09-07 11:09:44 +0100141 unsigned char buf[4096];
Paul Bakker6d620502012-02-16 14:09:13 +0000142 int ret;
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100143#if !defined(MBEDTLS_USE_PSA_CRYPTO)
144 unsigned char check_buf[4000];
Paul Bakker6d620502012-02-16 14:09:13 +0000145 FILE *f;
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100146 size_t olen = 0;
147#endif /* !MBEDTLS_USE_PSA_CRYPTO */
148 size_t pem_len = 0, buf_index;
149 int der_len = -1;
Paul Bakker3a8cb6f2013-12-30 20:41:54 +0100150 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200151 mbedtls_test_rnd_pseudo_info rnd_info;
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100152 mbedtls_x509_san_list san_ip;
153 mbedtls_x509_san_list san_dns;
154 mbedtls_x509_san_list san_uri;
155 mbedtls_x509_san_list *san_list = NULL;
156 const char san_ip_name[] = { 0x7f, 0x01, 0x01, 0x00 }; // 127.1.1.0
157 const char *san_dns_name = "example.com";
158 const char *san_uri_name = "http://pki.example.com/";
159
160 san_uri.node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100161 san_uri.node.san.unstructured_name.p = (unsigned char *) san_uri_name;
162 san_uri.node.san.unstructured_name.len = strlen(san_uri_name);
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100163 san_uri.next = NULL;
164 san_ip.node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100165 san_ip.node.san.unstructured_name.p = (unsigned char *) san_ip_name;
166 san_ip.node.san.unstructured_name.len = sizeof(san_ip_name);
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100167 san_ip.next = &san_uri;
168 san_dns.node.type = MBEDTLS_X509_SAN_DNS_NAME;
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100169 san_dns.node.san.unstructured_name.p = (unsigned char *) san_dns_name;
170 san_dns.node.san.unstructured_name.len = strlen(san_dns_name);
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100171 san_dns.next = &san_ip;
172 san_list = &san_dns;
Paul Bakker6d620502012-02-16 14:09:13 +0000173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 mbedtls_x509write_csr_init(&req);
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 mbedtls_pk_init(&key);
valerio32f2ac92023-04-20 11:59:52 +0200178 MD_OR_USE_PSA_INIT();
179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL,
181 mbedtls_test_rnd_std_rand, NULL) == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 mbedtls_x509write_csr_set_md_alg(&req, md_type);
184 mbedtls_x509write_csr_set_key(&req, &key);
185 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
186 if (set_key_usage != 0) {
187 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
188 }
189 if (set_cert_type != 0) {
190 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
191 }
192 if (set_extension != 0) {
193 TEST_ASSERT(csr_set_extended_key_usage(&req, MBEDTLS_OID_SERVER_AUTH,
194 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) == 0);
Przemek Stekiel8e83d3a2023-02-14 12:01:16 +0100195
196 TEST_ASSERT(mbedtls_x509write_csr_set_subject_alternative_name(&req, san_list) == 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 }
Paul Bakker8eabfc12013-08-25 10:18:25 +0200198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf),
200 mbedtls_test_rnd_pseudo_rand, &rnd_info);
201 TEST_ASSERT(ret == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 pem_len = strlen((char *) buf);
Paul Bakker6d620502012-02-16 14:09:13 +0000204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
206 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000207 }
208
Neil Armstrong5b320382022-02-21 17:22:10 +0100209#if defined(MBEDTLS_USE_PSA_CRYPTO)
210 // When using PSA crypto, RNG isn't controllable, so cert_req_check_file can't be used
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 (void) cert_req_check_file;
Neil Armstrong5b320382022-02-21 17:22:10 +0100212 buf[pem_len] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0);
Neil Armstrong5b320382022-02-21 17:22:10 +0100214#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 f = fopen(cert_req_check_file, "r");
216 TEST_ASSERT(f != NULL);
217 olen = fread(check_buf, 1, sizeof(check_buf), f);
218 fclose(f);
Paul Bakker6d620502012-02-16 14:09:13 +0000219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 TEST_ASSERT(olen >= pem_len - 1);
221 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100222#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 der_len = mbedtls_x509write_csr_der(&req, buf, sizeof(buf),
225 mbedtls_test_rnd_pseudo_rand,
226 &rnd_info);
227 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100230 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 }
Andres AGe0af9952016-09-07 11:09:44 +0100232
Neil Armstrong5b320382022-02-21 17:22:10 +0100233#if defined(MBEDTLS_USE_PSA_CRYPTO)
234 // When using PSA crypto, RNG isn't controllable, result length isn't
235 // deterministic over multiple runs, removing a single byte isn't enough to
236 // go into the MBEDTLS_ERR_ASN1_BUF_TOO_SMALL error case
237 der_len /= 2;
238#else
239 der_len -= 1;
240#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 ret = mbedtls_x509write_csr_der(&req, buf, (size_t) (der_len),
242 mbedtls_test_rnd_pseudo_rand, &rnd_info);
243 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100244
Paul Bakkerbd51b262014-07-10 15:26:12 +0200245exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 mbedtls_x509write_csr_free(&req);
247 mbedtls_pk_free(&key);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100248 MD_OR_USE_PSA_DONE();
Paul Bakker6d620502012-02-16 14:09:13 +0000249}
Paul Bakker33b43f12013-08-20 11:48:36 +0200250/* END_CASE */
Paul Bakker2397cf32013-09-08 15:58:15 +0200251
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500252/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C:MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100253void x509_csr_check_opaque(char *key_file, int md_type, int key_usage,
254 int cert_type)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500255{
256 mbedtls_pk_context key;
Ronald Cron5425a212020-08-04 14:58:35 +0200257 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong95974972022-04-22 13:57:44 +0200258 psa_algorithm_t md_alg_psa, alg_psa;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500259 mbedtls_x509write_csr req;
260 unsigned char buf[4096];
261 int ret;
262 size_t pem_len = 0;
263 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200264 mbedtls_test_rnd_pseudo_info rnd_info;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500265
valerio32f2ac92023-04-20 11:59:52 +0200266 mbedtls_x509write_csr_init(&req);
Valerio Setti569c1712023-04-19 14:53:36 +0200267 MD_OR_USE_PSA_INIT();
268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500270
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200271 md_alg_psa = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 TEST_ASSERT(md_alg_psa != MBEDTLS_MD_NONE);
Andrzej Kurek967cfd12018-11-20 02:53:17 -0500273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 mbedtls_pk_init(&key);
275 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL,
276 mbedtls_test_rnd_std_rand, NULL) == 0);
Neil Armstrong95974972022-04-22 13:57:44 +0200277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
279 alg_psa = PSA_ALG_ECDSA(md_alg_psa);
280 } else if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
281 alg_psa = PSA_ALG_RSA_PKCS1V15_SIGN(md_alg_psa);
282 } else {
283 TEST_ASSUME(!"PK key type not supported in this configuration");
284 }
Neil Armstrong95974972022-04-22 13:57:44 +0200285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 TEST_ASSERT(mbedtls_pk_wrap_as_opaque(&key, &key_id, alg_psa,
287 PSA_KEY_USAGE_SIGN_HASH,
288 PSA_ALG_NONE) == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 mbedtls_x509write_csr_set_md_alg(&req, md_type);
291 mbedtls_x509write_csr_set_key(&req, &key);
292 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
293 if (key_usage != 0) {
294 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
295 }
296 if (cert_type != 0) {
297 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
298 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf) - 1,
301 mbedtls_test_rnd_pseudo_rand, &rnd_info);
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 TEST_ASSERT(ret == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 pem_len = strlen((char *) buf);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500306 buf[pem_len] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500308
Manuel Pégourié-Gonnardfeb03962020-08-20 09:59:33 +0200309
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500310exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 mbedtls_x509write_csr_free(&req);
312 mbedtls_pk_free(&key);
313 psa_destroy_key(key_id);
Valerio Setti569c1712023-04-19 14:53:36 +0200314 MD_OR_USE_PSA_DONE();
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500315}
316/* END_CASE */
317
Manuel Pégourié-Gonnarda9464892023-03-17 12:08:50 +0100318/* 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 +0100319void x509_crt_check(char *subject_key_file, char *subject_pwd,
320 char *subject_name, char *issuer_key_file,
321 char *issuer_pwd, char *issuer_name,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100322 data_t *serial_arg, char *not_before, char *not_after,
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 int md_type, int key_usage, int set_key_usage,
324 char *ext_key_usage,
325 int cert_type, int set_cert_type, int auth_ident,
326 int ver, char *cert_check_file, int pk_wrap, int is_ca,
Andrzej Kurek76c96622023-04-04 06:57:08 -0400327 char *cert_verify_file, int set_subjectAltNames)
Paul Bakker2397cf32013-09-08 15:58:15 +0200328{
Hanno Becker418a6222017-09-14 07:51:28 +0100329 mbedtls_pk_context subject_key, issuer_key, issuer_key_alt;
330 mbedtls_pk_context *key = &issuer_key;
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 mbedtls_x509write_cert crt;
Andres AGe0af9952016-09-07 11:09:44 +0100333 unsigned char buf[4096];
Paul Bakker2397cf32013-09-08 15:58:15 +0200334 unsigned char check_buf[5000];
Werner Lewisacd01e52022-05-10 12:23:13 +0100335 unsigned char *p, *end;
336 unsigned char tag, sz;
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100337#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
338 mbedtls_mpi serial_mpi;
339#endif
Werner Lewisacd01e52022-05-10 12:23:13 +0100340 int ret, before_tag, after_tag;
Paul Elliott557b8d62020-11-19 09:46:56 +0000341 size_t olen = 0, pem_len = 0, buf_index = 0;
Andres AGe0af9952016-09-07 11:09:44 +0100342 int der_len = -1;
Paul Bakker2397cf32013-09-08 15:58:15 +0200343 FILE *f;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200344 mbedtls_test_rnd_pseudo_info rnd_info;
Neil Armstrong98f899c2022-03-16 17:42:42 +0100345#if defined(MBEDTLS_USE_PSA_CRYPTO)
346 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
347#endif
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100348 mbedtls_pk_type_t issuer_key_type;
Andrzej Kurek76c96622023-04-04 06:57:08 -0400349 mbedtls_x509_san_list san_ip;
350 mbedtls_x509_san_list san_dns;
351 mbedtls_x509_san_list san_uri;
352 mbedtls_x509_san_list san_mail;
353 mbedtls_x509_san_list san_dn;
354 mbedtls_asn1_named_data *ext_san_dirname = NULL;
355 const char san_ip_name[] = { 0x01, 0x02, 0x03, 0x04 };
356 const char *san_dns_name = "example.com";
357 const char *san_dn_name = "C=UK,O=Mbed TLS,CN=SubjectAltName test";
358 const char *san_mail_name = "mail@example.com";
359 const char *san_uri_name = "http://pki.example.com";
360 mbedtls_x509_san_list *san_list = NULL;
361
362 if (set_subjectAltNames) {
363 san_mail.node.type = MBEDTLS_X509_SAN_RFC822_NAME;
364 san_mail.node.san.unstructured_name.p = (unsigned char *) san_mail_name;
Andrzej Kurek13c43f62023-04-04 10:43:38 -0400365 san_mail.node.san.unstructured_name.len = strlen(san_mail_name);
Andrzej Kurek76c96622023-04-04 06:57:08 -0400366 san_mail.next = NULL;
367
368 san_dns.node.type = MBEDTLS_X509_SAN_DNS_NAME;
369 san_dns.node.san.unstructured_name.p = (unsigned char *) san_dns_name;
370 san_dns.node.san.unstructured_name.len = strlen(san_dns_name);
371 san_dns.next = &san_mail;
372
373 san_dn.node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
374 TEST_ASSERT(mbedtls_x509_string_to_names(&ext_san_dirname,
375 san_dn_name) == 0);
376 san_dn.node.san.directory_name = *ext_san_dirname;
377 san_dn.next = &san_dns;
378
379 san_ip.node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
380 san_ip.node.san.unstructured_name.p = (unsigned char *) san_ip_name;
381 san_ip.node.san.unstructured_name.len = sizeof(san_ip_name);
382 san_ip.next = &san_dn;
383
384 san_uri.node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
385 san_uri.node.san.unstructured_name.p = (unsigned char *) san_uri_name;
386 san_uri.node.san.unstructured_name.len = strlen(san_uri_name);
387 san_uri.next = &san_ip;
388
389 san_list = &san_uri;
390 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100393#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
394 mbedtls_mpi_init(&serial_mpi);
395#endif
Hanno Becker418a6222017-09-14 07:51:28 +0100396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 mbedtls_pk_init(&subject_key);
398 mbedtls_pk_init(&issuer_key);
399 mbedtls_pk_init(&issuer_key_alt);
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 mbedtls_x509write_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200401 MD_OR_USE_PSA_INIT();
Paul Bakker2397cf32013-09-08 15:58:15 +0200402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 TEST_ASSERT(mbedtls_pk_parse_keyfile(&subject_key, subject_key_file,
404 subject_pwd, mbedtls_test_rnd_std_rand, NULL) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 TEST_ASSERT(mbedtls_pk_parse_keyfile(&issuer_key, issuer_key_file,
407 issuer_pwd, mbedtls_test_rnd_std_rand, NULL) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 issuer_key_type = mbedtls_pk_get_type(&issuer_key);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100410
Dave Rodgmandc3b1542023-01-20 11:39:00 +0000411#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Hanno Becker418a6222017-09-14 07:51:28 +0100412 /* For RSA PK contexts, create a copy as an alternative RSA context. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 if (pk_wrap == 1 && issuer_key_type == MBEDTLS_PK_RSA) {
414 TEST_ASSERT(mbedtls_pk_setup_rsa_alt(&issuer_key_alt,
415 mbedtls_pk_rsa(issuer_key),
416 mbedtls_rsa_decrypt_func,
417 mbedtls_rsa_sign_func,
418 mbedtls_rsa_key_len_func) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100419
420 key = &issuer_key_alt;
421 }
Manuel Pégourié-Gonnard147b28e2018-03-12 15:26:59 +0100422#endif
Hanno Becker418a6222017-09-14 07:51:28 +0100423
Neil Armstrong98f899c2022-03-16 17:42:42 +0100424#if defined(MBEDTLS_USE_PSA_CRYPTO)
425 /* For Opaque PK contexts, wrap key as an Opaque RSA context. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 if (pk_wrap == 2) {
Neil Armstrong95974972022-04-22 13:57:44 +0200427 psa_algorithm_t alg_psa, md_alg_psa;
Neil Armstrong98f899c2022-03-16 17:42:42 +0100428
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200429 md_alg_psa = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) md_type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 TEST_ASSERT(md_alg_psa != MBEDTLS_MD_NONE);
Neil Armstrong95974972022-04-22 13:57:44 +0200431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_ECKEY) {
433 alg_psa = PSA_ALG_ECDSA(md_alg_psa);
434 } else if (mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_RSA) {
435 alg_psa = PSA_ALG_RSA_PKCS1V15_SIGN(md_alg_psa);
436 } else {
437 TEST_ASSUME(!"PK key type not supported in this configuration");
438 }
Neil Armstrong95974972022-04-22 13:57:44 +0200439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 TEST_ASSERT(mbedtls_pk_wrap_as_opaque(&issuer_key, &key_id, alg_psa,
441 PSA_KEY_USAGE_SIGN_HASH,
442 PSA_ALG_NONE) == 0);
Neil Armstrong98f899c2022-03-16 17:42:42 +0100443 }
444#endif /* MBEDTLS_USE_PSA_CRYPTO */
445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if (pk_wrap == 2) {
447 TEST_ASSERT(mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_OPAQUE);
448 }
Neil Armstrong98f899c2022-03-16 17:42:42 +0100449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if (ver != -1) {
451 mbedtls_x509write_crt_set_version(&crt, ver);
452 }
Hanno Becker418a6222017-09-14 07:51:28 +0100453
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100454#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100455 TEST_ASSERT(mbedtls_mpi_read_binary(&serial_mpi, serial_arg->x,
456 serial_arg->len) == 0);
457 TEST_ASSERT(mbedtls_x509write_crt_set_serial(&crt, &serial_mpi) == 0);
Valerio Settida0afcc2023-01-05 16:46:59 +0100458#else
Valerio Settiaf4815c2023-01-26 17:43:09 +0100459 TEST_ASSERT(mbedtls_x509write_crt_set_serial_raw(&crt, serial_arg->x,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100460 serial_arg->len) == 0);
Valerio Settida0afcc2023-01-05 16:46:59 +0100461#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 TEST_ASSERT(mbedtls_x509write_crt_set_validity(&crt, not_before,
463 not_after) == 0);
464 mbedtls_x509write_crt_set_md_alg(&crt, md_type);
465 TEST_ASSERT(mbedtls_x509write_crt_set_issuer_name(&crt, issuer_name) == 0);
466 TEST_ASSERT(mbedtls_x509write_crt_set_subject_name(&crt, subject_name) == 0);
467 mbedtls_x509write_crt_set_subject_key(&crt, &subject_key);
Hanno Becker418a6222017-09-14 07:51:28 +0100468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 mbedtls_x509write_crt_set_issuer_key(&crt, key);
Paul Bakker2397cf32013-09-08 15:58:15 +0200470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 if (crt.version >= MBEDTLS_X509_CRT_VERSION_3) {
Darren Krahn9c134ce2021-01-13 22:04:45 -0800472 /* For the CA case, a path length of -1 means unlimited. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 TEST_ASSERT(mbedtls_x509write_crt_set_basic_constraints(&crt, is_ca,
474 (is_ca ? -1 : 0)) == 0);
475 TEST_ASSERT(mbedtls_x509write_crt_set_subject_key_identifier(&crt) == 0);
476 if (auth_ident) {
477 TEST_ASSERT(mbedtls_x509write_crt_set_authority_key_identifier(&crt) == 0);
478 }
479 if (set_key_usage != 0) {
480 TEST_ASSERT(mbedtls_x509write_crt_set_key_usage(&crt, key_usage) == 0);
481 }
482 if (set_cert_type != 0) {
483 TEST_ASSERT(mbedtls_x509write_crt_set_ns_cert_type(&crt, cert_type) == 0);
484 }
485 if (strcmp(ext_key_usage, "NULL") != 0) {
Dave Rodgmanec9f6b42022-07-27 14:34:58 +0100486 mbedtls_asn1_sequence exts[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 memset(exts, 0, sizeof(exts));
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100488
489#define SET_OID(x, oid) \
490 do { \
491 x.len = MBEDTLS_OID_SIZE(oid); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 x.p = (unsigned char *) oid; \
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100493 x.tag = MBEDTLS_ASN1_OID; \
Dave Rodgmane2b772d2022-08-11 16:04:13 +0100494 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 while (0)
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if (strcmp(ext_key_usage, "serverAuth") == 0) {
498 SET_OID(exts[0].buf, MBEDTLS_OID_SERVER_AUTH);
499 } else if (strcmp(ext_key_usage, "codeSigning,timeStamping") == 0) {
500 SET_OID(exts[0].buf, MBEDTLS_OID_CODE_SIGNING);
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100501 exts[0].next = &exts[1];
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 SET_OID(exts[1].buf, MBEDTLS_OID_TIME_STAMPING);
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000503 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 TEST_ASSERT(mbedtls_x509write_crt_set_ext_key_usage(&crt, exts) == 0);
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000505 }
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100506 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200507
Andrzej Kurek76c96622023-04-04 06:57:08 -0400508 if (set_subjectAltNames) {
509 TEST_ASSERT(mbedtls_x509write_crt_set_subject_alternative_name(&crt, san_list) == 0);
510 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 ret = mbedtls_x509write_crt_pem(&crt, buf, sizeof(buf),
512 mbedtls_test_rnd_pseudo_rand, &rnd_info);
513 TEST_ASSERT(ret == 0);
Paul Bakker2397cf32013-09-08 15:58:15 +0200514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 pem_len = strlen((char *) buf);
Paul Bakker2397cf32013-09-08 15:58:15 +0200516
Paul Elliott557b8d62020-11-19 09:46:56 +0000517 // check that the rest of the buffer remains clear
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
519 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000520 }
521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if (issuer_key_type != MBEDTLS_PK_RSA) {
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100523 mbedtls_x509_crt crt_parse, trusted;
524 uint32_t flags;
Paul Bakker2397cf32013-09-08 15:58:15 +0200525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 mbedtls_x509_crt_init(&crt_parse);
527 mbedtls_x509_crt_init(&trusted);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted,
530 cert_verify_file) == 0);
531 TEST_ASSERT(mbedtls_x509_crt_parse(&crt_parse,
532 buf, sizeof(buf)) == 0);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 ret = mbedtls_x509_crt_verify(&crt_parse, &trusted, NULL, NULL, &flags,
535 NULL, NULL);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 mbedtls_x509_crt_free(&crt_parse);
538 mbedtls_x509_crt_free(&trusted);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 TEST_EQUAL(flags, 0);
541 TEST_EQUAL(ret, 0);
542 } else if (*cert_check_file != '\0') {
543 f = fopen(cert_check_file, "r");
544 TEST_ASSERT(f != NULL);
545 olen = fread(check_buf, 1, sizeof(check_buf), f);
546 fclose(f);
547 TEST_ASSERT(olen < sizeof(check_buf));
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 TEST_EQUAL(olen, pem_len);
550 TEST_ASSERT(olen >= pem_len - 1);
551 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100552 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 der_len = mbedtls_x509write_crt_der(&crt, buf, sizeof(buf),
555 mbedtls_test_rnd_pseudo_rand,
556 &rnd_info);
557 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100560 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 }
Andres AGe0af9952016-09-07 11:09:44 +0100562
Werner Lewisacd01e52022-05-10 12:23:13 +0100563 // Not testing against file, check date format
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if (*cert_check_file == '\0') {
Werner Lewisacd01e52022-05-10 12:23:13 +0100565 // UTC tag if before 2050, 2 digits less for year
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 if (not_before[0] == '2' && (not_before[1] > '0' || not_before[2] > '4')) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100567 before_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 } else {
Werner Lewisacd01e52022-05-10 12:23:13 +0100569 before_tag = MBEDTLS_ASN1_UTC_TIME;
570 not_before += 2;
571 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 if (not_after[0] == '2' && (not_after[1] > '0' || not_after[2] > '4')) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100573 after_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 } else {
Werner Lewisacd01e52022-05-10 12:23:13 +0100575 after_tag = MBEDTLS_ASN1_UTC_TIME;
576 not_after += 2;
577 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 end = buf + sizeof(buf);
579 for (p = end - der_len; p < end;) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100580 tag = *p++;
581 sz = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if (tag == MBEDTLS_ASN1_UTC_TIME || tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100583 // Check correct tag and time written
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 TEST_ASSERT(before_tag == tag);
585 TEST_ASSERT(memcmp(p, not_before, sz - 1) == 0);
Werner Lewisacd01e52022-05-10 12:23:13 +0100586 p += sz;
587 tag = *p++;
588 sz = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 TEST_ASSERT(after_tag == tag);
590 TEST_ASSERT(memcmp(p, not_after, sz - 1) == 0);
Werner Lewisacd01e52022-05-10 12:23:13 +0100591 break;
592 }
593 // Increment if long form ASN1 length
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if (sz & 0x80) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100595 p += sz & 0x0F;
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 }
597 if (tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100598 p += sz;
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 }
Werner Lewisacd01e52022-05-10 12:23:13 +0100600 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 TEST_ASSERT(p < end);
Werner Lewisacd01e52022-05-10 12:23:13 +0100602 }
603
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100604#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge6ed23c2022-04-22 09:44:04 +0200605 // When using PSA crypto, RNG isn't controllable, result length isn't
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100606 // deterministic over multiple runs, removing a single byte isn't enough to
607 // go into the MBEDTLS_ERR_ASN1_BUF_TOO_SMALL error case
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (issuer_key_type != MBEDTLS_PK_RSA) {
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100609 der_len /= 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 } else
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100611#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 der_len -= 1;
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 ret = mbedtls_x509write_crt_der(&crt, buf, (size_t) (der_len),
615 mbedtls_test_rnd_pseudo_rand, &rnd_info);
616 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100617
Paul Bakkerbd51b262014-07-10 15:26:12 +0200618exit:
Andrzej Kurek5da1d752023-04-05 08:30:59 -0400619 mbedtls_asn1_free_named_data_list(&ext_san_dirname);
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 mbedtls_x509write_crt_free(&crt);
621 mbedtls_pk_free(&issuer_key_alt);
622 mbedtls_pk_free(&subject_key);
623 mbedtls_pk_free(&issuer_key);
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100624#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
625 mbedtls_mpi_free(&serial_mpi);
626#endif
Neil Armstrong98f899c2022-03-16 17:42:42 +0100627#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 psa_destroy_key(key_id);
Neil Armstrong98f899c2022-03-16 17:42:42 +0100629#endif
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100630 MD_OR_USE_PSA_DONE();
Paul Bakker2397cf32013-09-08 15:58:15 +0200631}
632/* END_CASE */
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200633
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100634/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_WRITE_C */
635void x509_set_serial_check()
636{
637 mbedtls_x509write_cert ctx;
638 uint8_t invalid_serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN + 1];
639
Valerio Setti569c1712023-04-19 14:53:36 +0200640 USE_PSA_INIT();
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100641 memset(invalid_serial, 0x01, sizeof(invalid_serial));
642
Valerio Settiea19d2d2023-01-09 17:21:17 +0100643#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100644 mbedtls_mpi serial_mpi;
645
646 mbedtls_mpi_init(&serial_mpi);
647 TEST_EQUAL(mbedtls_mpi_read_binary(&serial_mpi, invalid_serial,
648 sizeof(invalid_serial)), 0);
649 TEST_EQUAL(mbedtls_x509write_crt_set_serial(&ctx, &serial_mpi),
650 MBEDTLS_ERR_X509_BAD_INPUT_DATA);
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100651#endif
652
Valerio Settiaf4815c2023-01-26 17:43:09 +0100653 TEST_EQUAL(mbedtls_x509write_crt_set_serial_raw(&ctx, invalid_serial,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100654 sizeof(invalid_serial)),
655 MBEDTLS_ERR_X509_BAD_INPUT_DATA);
656
Valerio Settia87f8392023-01-26 18:00:50 +0100657exit:
658#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
659 mbedtls_mpi_free(&serial_mpi);
660#else
661 ;
662#endif
Valerio Setti569c1712023-04-19 14:53:36 +0200663 USE_PSA_DONE();
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100664}
665/* END_CASE */
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668void mbedtls_x509_string_to_names(char *name, char *parsed_name, int result
669 )
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200670{
671 int ret;
672 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_asn1_named_data *names = NULL;
674 mbedtls_x509_name parsed, *parsed_cur, *parsed_prv;
Manuel Pégourié-Gonnard4fd0b252015-06-26 14:15:48 +0200675 unsigned char buf[1024], out[1024], *c;
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200676
Valerio Setti569c1712023-04-19 14:53:36 +0200677 USE_PSA_INIT();
678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 memset(&parsed, 0, sizeof(parsed));
680 memset(out, 0, sizeof(out));
681 memset(buf, 0, sizeof(buf));
682 c = buf + sizeof(buf);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 ret = mbedtls_x509_string_to_names(&names, name);
685 TEST_ASSERT(ret == result);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 if (ret != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200688 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 }
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 ret = mbedtls_x509_write_names(&c, buf, names);
692 TEST_ASSERT(ret > 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 TEST_ASSERT(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
695 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) == 0);
696 TEST_ASSERT(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed) == 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 ret = mbedtls_x509_dn_gets((char *) out, sizeof(out), &parsed);
699 TEST_ASSERT(ret > 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 TEST_ASSERT(strcmp((char *) out, parsed_name) == 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200702
703exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 mbedtls_asn1_free_named_data_list(&names);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200705
706 parsed_cur = parsed.next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 while (parsed_cur != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200708 parsed_prv = parsed_cur;
709 parsed_cur = parsed_cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 mbedtls_free(parsed_prv);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200711 }
Valerio Setti569c1712023-04-19 14:53:36 +0200712 USE_PSA_DONE();
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200713}
714/* END_CASE */