blob: cd1f203eaf2f58197e8268d10f51c23ee8a28da9 [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
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +020011#include "hash_info.h"
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020012#include "mbedtls/legacy_or_psa.h"
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +020013
Hanno Becker418a6222017-09-14 07:51:28 +010014#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010015int mbedtls_rsa_decrypt_func(void *ctx, size_t *olen,
16 const unsigned char *input, unsigned char *output,
17 size_t output_max_len)
Hanno Becker418a6222017-09-14 07:51:28 +010018{
Gilles Peskine449bd832023-01-11 14:50:10 +010019 return mbedtls_rsa_pkcs1_decrypt((mbedtls_rsa_context *) ctx, NULL, NULL,
20 olen, input, output, output_max_len);
Hanno Becker418a6222017-09-14 07:51:28 +010021}
Gilles Peskine449bd832023-01-11 14:50:10 +010022int mbedtls_rsa_sign_func(void *ctx,
23 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
24 mbedtls_md_type_t md_alg, unsigned int hashlen,
25 const unsigned char *hash, unsigned char *sig)
Hanno Becker418a6222017-09-14 07:51:28 +010026{
Gilles Peskine449bd832023-01-11 14:50:10 +010027 return mbedtls_rsa_pkcs1_sign((mbedtls_rsa_context *) ctx, f_rng, p_rng,
28 md_alg, hashlen, hash, sig);
Hanno Becker418a6222017-09-14 07:51:28 +010029}
Gilles Peskine449bd832023-01-11 14:50:10 +010030size_t mbedtls_rsa_key_len_func(void *ctx)
Hanno Becker418a6222017-09-14 07:51:28 +010031{
Gilles Peskine449bd832023-01-11 14:50:10 +010032 return ((const mbedtls_rsa_context *) ctx)->len;
Hanno Becker418a6222017-09-14 07:51:28 +010033}
34#endif /* MBEDTLS_RSA_C */
35
Gilles Peskinef4e672e2020-01-31 14:22:10 +010036#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
37 defined(MBEDTLS_PEM_WRITE_C) && defined(MBEDTLS_X509_CSR_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010038static int x509_crt_verifycsr(const unsigned char *buf, size_t buflen)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050039{
Przemek Stekiel54a54462022-08-01 13:59:12 +020040 unsigned char hash[PSA_HASH_MAX_SIZE];
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050041 mbedtls_x509_csr csr;
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010042 int ret = 0;
43
Gilles Peskine449bd832023-01-11 14:50:10 +010044 mbedtls_x509_csr_init(&csr);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050045
Gilles Peskine449bd832023-01-11 14:50:10 +010046 if (mbedtls_x509_csr_parse(&csr, buf, buflen) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010047 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
48 goto cleanup;
49 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050050
Gilles Peskine449bd832023-01-11 14:50:10 +010051 psa_algorithm_t psa_alg = mbedtls_hash_info_psa_from_md(csr.sig_md);
Przemek Stekiel54a54462022-08-01 13:59:12 +020052 size_t hash_size = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010053 psa_status_t status = psa_hash_compute(psa_alg, csr.cri.p, csr.cri.len,
54 hash, PSA_HASH_MAX_SIZE, &hash_size);
Przemek Stekiel54a54462022-08-01 13:59:12 +020055
Gilles Peskine449bd832023-01-11 14:50:10 +010056 if (status != PSA_SUCCESS) {
Andrzej Kurek4b114072018-11-19 18:04:01 -050057 /* Note: this can't happen except after an internal error */
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010058 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
59 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050060 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050061
Gilles Peskine449bd832023-01-11 14:50:10 +010062 if (mbedtls_pk_verify_ext(csr.sig_pk, csr.sig_opts, &csr.pk,
63 csr.sig_md, hash, mbedtls_hash_info_get_size(csr.sig_md),
64 csr.sig.p, csr.sig.len) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010065 ret = MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
66 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050067 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050068
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010069cleanup:
70
Gilles Peskine449bd832023-01-11 14:50:10 +010071 mbedtls_x509_csr_free(&csr);
72 return ret;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050073}
Gilles Peskinef4e672e2020-01-31 14:22:10 +010074#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_PEM_WRITE_C && MBEDTLS_X509_CSR_WRITE_C */
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050075
Valerio Setti48e8fc72022-10-19 15:14:29 +020076#if defined(MBEDTLS_X509_CSR_WRITE_C)
77
78/*
79 * The size of this temporary buffer is given by the sequence of functions
80 * called hereinafter:
81 * - mbedtls_asn1_write_oid()
82 * - 8 bytes for MBEDTLS_OID_EXTENDED_KEY_USAGE raw value
83 * - 1 byte for MBEDTLS_OID_EXTENDED_KEY_USAGE length
84 * - 1 byte for MBEDTLS_ASN1_OID tag
85 * - mbedtls_asn1_write_len()
86 * - 1 byte since we're dealing with sizes which are less than 0x80
87 * - mbedtls_asn1_write_tag()
88 * - 1 byte
89 *
90 * This length is fine as long as this function is called using the
91 * MBEDTLS_OID_SERVER_AUTH OID. If this is changed in the future, then this
92 * buffer's length should be adjusted accordingly.
93 * Unfortunately there's no predefined max size for OIDs which can be used
94 * to set an overall upper boundary which is always guaranteed.
95 */
96#define EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH 12
97
Gilles Peskine449bd832023-01-11 14:50:10 +010098static int csr_set_extended_key_usage(mbedtls_x509write_csr *ctx,
99 const char *oid, size_t oid_len)
Valerio Setti48e8fc72022-10-19 15:14:29 +0200100{
101 unsigned char buf[EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH] = { 0 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 unsigned char *p = buf + sizeof(buf);
Valerio Setti48e8fc72022-10-19 15:14:29 +0200103 int ret;
104 size_t len = 0;
105
106 /*
107 * Following functions fail anyway if the temporary buffer is not large,
108 * but we set an extra check here to emphasize a possible source of errors
109 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if (oid_len > EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH) {
Valerio Setti48e8fc72022-10-19 15:14:29 +0200111 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
112 }
113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(&p, buf, oid, oid_len));
115 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, ret));
116 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, buf,
117 MBEDTLS_ASN1_CONSTRUCTED |
118 MBEDTLS_ASN1_SEQUENCE));
Valerio Setti48e8fc72022-10-19 15:14:29 +0200119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 ret = mbedtls_x509write_csr_set_extension(ctx,
121 MBEDTLS_OID_EXTENDED_KEY_USAGE,
122 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
123 0,
124 p,
125 len);
Valerio Setti48e8fc72022-10-19 15:14:29 +0200126
127 return ret;
128}
129#endif /* MBEDTLS_X509_CSR_WRITE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200130/* END_HEADER */
Paul Bakker6d620502012-02-16 14:09:13 +0000131
Paul Bakker33b43f12013-08-20 11:48:36 +0200132/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 * depends_on:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO:MBEDTLS_PK_PARSE_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200134 * END_DEPENDENCIES
135 */
Paul Bakker6d620502012-02-16 14:09:13 +0000136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100138void x509_csr_check(char *key_file, char *cert_req_check_file, int md_type,
139 int key_usage, int set_key_usage, int cert_type,
140 int set_cert_type, int set_extension)
Paul Bakker6d620502012-02-16 14:09:13 +0000141{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_pk_context key;
143 mbedtls_x509write_csr req;
Andres AGe0af9952016-09-07 11:09:44 +0100144 unsigned char buf[4096];
Paul Bakker6d620502012-02-16 14:09:13 +0000145 int ret;
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100146#if !defined(MBEDTLS_USE_PSA_CRYPTO)
147 unsigned char check_buf[4000];
Paul Bakker6d620502012-02-16 14:09:13 +0000148 FILE *f;
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100149 size_t olen = 0;
150#endif /* !MBEDTLS_USE_PSA_CRYPTO */
151 size_t pem_len = 0, buf_index;
152 int der_len = -1;
Paul Bakker3a8cb6f2013-12-30 20:41:54 +0100153 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200154 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker6d620502012-02-16 14:09:13 +0000155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 mbedtls_x509write_csr_init(&req);
Neil Armstronga97f1ac2022-07-20 15:49:49 +0200159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 USE_PSA_INIT();
Neil Armstrong5f8328b2022-02-22 17:32:00 +0100161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 mbedtls_pk_init(&key);
163 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL,
164 mbedtls_test_rnd_std_rand, NULL) == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 mbedtls_x509write_csr_set_md_alg(&req, md_type);
167 mbedtls_x509write_csr_set_key(&req, &key);
168 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
169 if (set_key_usage != 0) {
170 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
171 }
172 if (set_cert_type != 0) {
173 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
174 }
175 if (set_extension != 0) {
176 TEST_ASSERT(csr_set_extended_key_usage(&req, MBEDTLS_OID_SERVER_AUTH,
177 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) == 0);
178 }
Paul Bakker8eabfc12013-08-25 10:18:25 +0200179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf),
181 mbedtls_test_rnd_pseudo_rand, &rnd_info);
182 TEST_ASSERT(ret == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 pem_len = strlen((char *) buf);
Paul Bakker6d620502012-02-16 14:09:13 +0000185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
187 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000188 }
189
Neil Armstrong5b320382022-02-21 17:22:10 +0100190#if defined(MBEDTLS_USE_PSA_CRYPTO)
191 // When using PSA crypto, RNG isn't controllable, so cert_req_check_file can't be used
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 (void) cert_req_check_file;
Neil Armstrong5b320382022-02-21 17:22:10 +0100193 buf[pem_len] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0);
Neil Armstrong5b320382022-02-21 17:22:10 +0100195#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 f = fopen(cert_req_check_file, "r");
197 TEST_ASSERT(f != NULL);
198 olen = fread(check_buf, 1, sizeof(check_buf), f);
199 fclose(f);
Paul Bakker6d620502012-02-16 14:09:13 +0000200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 TEST_ASSERT(olen >= pem_len - 1);
202 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100203#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 der_len = mbedtls_x509write_csr_der(&req, buf, sizeof(buf),
206 mbedtls_test_rnd_pseudo_rand,
207 &rnd_info);
208 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100211 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 }
Andres AGe0af9952016-09-07 11:09:44 +0100213
Neil Armstrong5b320382022-02-21 17:22:10 +0100214#if defined(MBEDTLS_USE_PSA_CRYPTO)
215 // When using PSA crypto, RNG isn't controllable, result length isn't
216 // deterministic over multiple runs, removing a single byte isn't enough to
217 // go into the MBEDTLS_ERR_ASN1_BUF_TOO_SMALL error case
218 der_len /= 2;
219#else
220 der_len -= 1;
221#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 ret = mbedtls_x509write_csr_der(&req, buf, (size_t) (der_len),
223 mbedtls_test_rnd_pseudo_rand, &rnd_info);
224 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100225
Paul Bakkerbd51b262014-07-10 15:26:12 +0200226exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 mbedtls_x509write_csr_free(&req);
228 mbedtls_pk_free(&key);
229 USE_PSA_DONE();
Paul Bakker6d620502012-02-16 14:09:13 +0000230}
Paul Bakker33b43f12013-08-20 11:48:36 +0200231/* END_CASE */
Paul Bakker2397cf32013-09-08 15:58:15 +0200232
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500233/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C:MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100234void x509_csr_check_opaque(char *key_file, int md_type, int key_usage,
235 int cert_type)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500236{
237 mbedtls_pk_context key;
Ronald Cron5425a212020-08-04 14:58:35 +0200238 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong95974972022-04-22 13:57:44 +0200239 psa_algorithm_t md_alg_psa, alg_psa;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500240 mbedtls_x509write_csr req;
241 unsigned char buf[4096];
242 int ret;
243 size_t pem_len = 0;
244 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200245 mbedtls_test_rnd_pseudo_info rnd_info;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 mbedtls_x509write_csr_init(&req);
Neil Armstronga97f1ac2022-07-20 15:49:49 +0200250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 USE_PSA_INIT();
Neil Armstronga97f1ac2022-07-20 15:49:49 +0200252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 md_alg_psa = mbedtls_hash_info_psa_from_md((mbedtls_md_type_t) md_type);
254 TEST_ASSERT(md_alg_psa != MBEDTLS_MD_NONE);
Andrzej Kurek967cfd12018-11-20 02:53:17 -0500255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 mbedtls_pk_init(&key);
257 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL,
258 mbedtls_test_rnd_std_rand, NULL) == 0);
Neil Armstrong95974972022-04-22 13:57:44 +0200259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
261 alg_psa = PSA_ALG_ECDSA(md_alg_psa);
262 } else if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
263 alg_psa = PSA_ALG_RSA_PKCS1V15_SIGN(md_alg_psa);
264 } else {
265 TEST_ASSUME(!"PK key type not supported in this configuration");
266 }
Neil Armstrong95974972022-04-22 13:57:44 +0200267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 TEST_ASSERT(mbedtls_pk_wrap_as_opaque(&key, &key_id, alg_psa,
269 PSA_KEY_USAGE_SIGN_HASH,
270 PSA_ALG_NONE) == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 mbedtls_x509write_csr_set_md_alg(&req, md_type);
273 mbedtls_x509write_csr_set_key(&req, &key);
274 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
275 if (key_usage != 0) {
276 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
277 }
278 if (cert_type != 0) {
279 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
280 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf) - 1,
283 mbedtls_test_rnd_pseudo_rand, &rnd_info);
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 TEST_ASSERT(ret == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 pem_len = strlen((char *) buf);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500288 buf[pem_len] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500290
Manuel Pégourié-Gonnardfeb03962020-08-20 09:59:33 +0200291
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500292exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 mbedtls_x509write_csr_free(&req);
294 mbedtls_pk_free(&key);
295 psa_destroy_key(key_id);
296 PSA_DONE();
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500297}
298/* END_CASE */
299
Ronald Cron81cd7ab2022-10-18 12:15:27 +0200300/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CRT_WRITE_C:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100301void x509_crt_check(char *subject_key_file, char *subject_pwd,
302 char *subject_name, char *issuer_key_file,
303 char *issuer_pwd, char *issuer_name,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100304 data_t *serial_arg, char *not_before, char *not_after,
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 int md_type, int key_usage, int set_key_usage,
306 char *ext_key_usage,
307 int cert_type, int set_cert_type, int auth_ident,
308 int ver, char *cert_check_file, int pk_wrap, int is_ca,
309 char *cert_verify_file)
Paul Bakker2397cf32013-09-08 15:58:15 +0200310{
Hanno Becker418a6222017-09-14 07:51:28 +0100311 mbedtls_pk_context subject_key, issuer_key, issuer_key_alt;
312 mbedtls_pk_context *key = &issuer_key;
313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_x509write_cert crt;
Andres AGe0af9952016-09-07 11:09:44 +0100315 unsigned char buf[4096];
Paul Bakker2397cf32013-09-08 15:58:15 +0200316 unsigned char check_buf[5000];
Werner Lewisacd01e52022-05-10 12:23:13 +0100317 unsigned char *p, *end;
318 unsigned char tag, sz;
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100319#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
320 mbedtls_mpi serial_mpi;
321#endif
Werner Lewisacd01e52022-05-10 12:23:13 +0100322 int ret, before_tag, after_tag;
Paul Elliott557b8d62020-11-19 09:46:56 +0000323 size_t olen = 0, pem_len = 0, buf_index = 0;
Andres AGe0af9952016-09-07 11:09:44 +0100324 int der_len = -1;
Paul Bakker2397cf32013-09-08 15:58:15 +0200325 FILE *f;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200326 mbedtls_test_rnd_pseudo_info rnd_info;
Neil Armstrong98f899c2022-03-16 17:42:42 +0100327#if defined(MBEDTLS_USE_PSA_CRYPTO)
328 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
329#endif
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100330 mbedtls_pk_type_t issuer_key_type;
Paul Bakker2397cf32013-09-08 15:58:15 +0200331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100333#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
334 mbedtls_mpi_init(&serial_mpi);
335#endif
Hanno Becker418a6222017-09-14 07:51:28 +0100336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 USE_PSA_INIT();
Neil Armstrong5f8328b2022-02-22 17:32:00 +0100338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 mbedtls_pk_init(&subject_key);
340 mbedtls_pk_init(&issuer_key);
341 mbedtls_pk_init(&issuer_key_alt);
Hanno Becker418a6222017-09-14 07:51:28 +0100342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 mbedtls_x509write_crt_init(&crt);
Paul Bakker2397cf32013-09-08 15:58:15 +0200344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 TEST_ASSERT(mbedtls_pk_parse_keyfile(&subject_key, subject_key_file,
346 subject_pwd, mbedtls_test_rnd_std_rand, NULL) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 TEST_ASSERT(mbedtls_pk_parse_keyfile(&issuer_key, issuer_key_file,
349 issuer_pwd, mbedtls_test_rnd_std_rand, NULL) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 issuer_key_type = mbedtls_pk_get_type(&issuer_key);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100352
Dave Rodgmandc3b1542023-01-20 11:39:00 +0000353#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Hanno Becker418a6222017-09-14 07:51:28 +0100354 /* For RSA PK contexts, create a copy as an alternative RSA context. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (pk_wrap == 1 && issuer_key_type == MBEDTLS_PK_RSA) {
356 TEST_ASSERT(mbedtls_pk_setup_rsa_alt(&issuer_key_alt,
357 mbedtls_pk_rsa(issuer_key),
358 mbedtls_rsa_decrypt_func,
359 mbedtls_rsa_sign_func,
360 mbedtls_rsa_key_len_func) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100361
362 key = &issuer_key_alt;
363 }
Manuel Pégourié-Gonnard147b28e2018-03-12 15:26:59 +0100364#endif
Hanno Becker418a6222017-09-14 07:51:28 +0100365
Neil Armstrong98f899c2022-03-16 17:42:42 +0100366#if defined(MBEDTLS_USE_PSA_CRYPTO)
367 /* For Opaque PK contexts, wrap key as an Opaque RSA context. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 if (pk_wrap == 2) {
Neil Armstrong95974972022-04-22 13:57:44 +0200369 psa_algorithm_t alg_psa, md_alg_psa;
Neil Armstrong98f899c2022-03-16 17:42:42 +0100370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 md_alg_psa = mbedtls_hash_info_psa_from_md((mbedtls_md_type_t) md_type);
372 TEST_ASSERT(md_alg_psa != MBEDTLS_MD_NONE);
Neil Armstrong95974972022-04-22 13:57:44 +0200373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if (mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_ECKEY) {
375 alg_psa = PSA_ALG_ECDSA(md_alg_psa);
376 } else if (mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_RSA) {
377 alg_psa = PSA_ALG_RSA_PKCS1V15_SIGN(md_alg_psa);
378 } else {
379 TEST_ASSUME(!"PK key type not supported in this configuration");
380 }
Neil Armstrong95974972022-04-22 13:57:44 +0200381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 TEST_ASSERT(mbedtls_pk_wrap_as_opaque(&issuer_key, &key_id, alg_psa,
383 PSA_KEY_USAGE_SIGN_HASH,
384 PSA_ALG_NONE) == 0);
Neil Armstrong98f899c2022-03-16 17:42:42 +0100385 }
386#endif /* MBEDTLS_USE_PSA_CRYPTO */
387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 if (pk_wrap == 2) {
389 TEST_ASSERT(mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_OPAQUE);
390 }
Neil Armstrong98f899c2022-03-16 17:42:42 +0100391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if (ver != -1) {
393 mbedtls_x509write_crt_set_version(&crt, ver);
394 }
Hanno Becker418a6222017-09-14 07:51:28 +0100395
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100396#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100397 TEST_ASSERT(mbedtls_mpi_read_binary(&serial_mpi, serial_arg->x,
398 serial_arg->len) == 0);
399 TEST_ASSERT(mbedtls_x509write_crt_set_serial(&crt, &serial_mpi) == 0);
Valerio Settida0afcc2023-01-05 16:46:59 +0100400#else
Valerio Settiaf4815c2023-01-26 17:43:09 +0100401 TEST_ASSERT(mbedtls_x509write_crt_set_serial_raw(&crt, serial_arg->x,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100402 serial_arg->len) == 0);
Valerio Settida0afcc2023-01-05 16:46:59 +0100403#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 TEST_ASSERT(mbedtls_x509write_crt_set_validity(&crt, not_before,
405 not_after) == 0);
406 mbedtls_x509write_crt_set_md_alg(&crt, md_type);
407 TEST_ASSERT(mbedtls_x509write_crt_set_issuer_name(&crt, issuer_name) == 0);
408 TEST_ASSERT(mbedtls_x509write_crt_set_subject_name(&crt, subject_name) == 0);
409 mbedtls_x509write_crt_set_subject_key(&crt, &subject_key);
Hanno Becker418a6222017-09-14 07:51:28 +0100410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 mbedtls_x509write_crt_set_issuer_key(&crt, key);
Paul Bakker2397cf32013-09-08 15:58:15 +0200412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 if (crt.version >= MBEDTLS_X509_CRT_VERSION_3) {
Darren Krahn9c134ce2021-01-13 22:04:45 -0800414 /* For the CA case, a path length of -1 means unlimited. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 TEST_ASSERT(mbedtls_x509write_crt_set_basic_constraints(&crt, is_ca,
416 (is_ca ? -1 : 0)) == 0);
417 TEST_ASSERT(mbedtls_x509write_crt_set_subject_key_identifier(&crt) == 0);
418 if (auth_ident) {
419 TEST_ASSERT(mbedtls_x509write_crt_set_authority_key_identifier(&crt) == 0);
420 }
421 if (set_key_usage != 0) {
422 TEST_ASSERT(mbedtls_x509write_crt_set_key_usage(&crt, key_usage) == 0);
423 }
424 if (set_cert_type != 0) {
425 TEST_ASSERT(mbedtls_x509write_crt_set_ns_cert_type(&crt, cert_type) == 0);
426 }
427 if (strcmp(ext_key_usage, "NULL") != 0) {
Dave Rodgmanec9f6b42022-07-27 14:34:58 +0100428 mbedtls_asn1_sequence exts[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 memset(exts, 0, sizeof(exts));
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100430
431#define SET_OID(x, oid) \
432 do { \
433 x.len = MBEDTLS_OID_SIZE(oid); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 x.p = (unsigned char *) oid; \
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100435 x.tag = MBEDTLS_ASN1_OID; \
Dave Rodgmane2b772d2022-08-11 16:04:13 +0100436 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 while (0)
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (strcmp(ext_key_usage, "serverAuth") == 0) {
440 SET_OID(exts[0].buf, MBEDTLS_OID_SERVER_AUTH);
441 } else if (strcmp(ext_key_usage, "codeSigning,timeStamping") == 0) {
442 SET_OID(exts[0].buf, MBEDTLS_OID_CODE_SIGNING);
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100443 exts[0].next = &exts[1];
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 SET_OID(exts[1].buf, MBEDTLS_OID_TIME_STAMPING);
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000445 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 TEST_ASSERT(mbedtls_x509write_crt_set_ext_key_usage(&crt, exts) == 0);
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000447 }
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100448 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 ret = mbedtls_x509write_crt_pem(&crt, buf, sizeof(buf),
451 mbedtls_test_rnd_pseudo_rand, &rnd_info);
452 TEST_ASSERT(ret == 0);
Paul Bakker2397cf32013-09-08 15:58:15 +0200453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 pem_len = strlen((char *) buf);
Paul Bakker2397cf32013-09-08 15:58:15 +0200455
Paul Elliott557b8d62020-11-19 09:46:56 +0000456 // check that the rest of the buffer remains clear
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
458 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000459 }
460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (issuer_key_type != MBEDTLS_PK_RSA) {
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100462 mbedtls_x509_crt crt_parse, trusted;
463 uint32_t flags;
Paul Bakker2397cf32013-09-08 15:58:15 +0200464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 mbedtls_x509_crt_init(&crt_parse);
466 mbedtls_x509_crt_init(&trusted);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted,
469 cert_verify_file) == 0);
470 TEST_ASSERT(mbedtls_x509_crt_parse(&crt_parse,
471 buf, sizeof(buf)) == 0);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 ret = mbedtls_x509_crt_verify(&crt_parse, &trusted, NULL, NULL, &flags,
474 NULL, NULL);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 mbedtls_x509_crt_free(&crt_parse);
477 mbedtls_x509_crt_free(&trusted);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 TEST_EQUAL(flags, 0);
480 TEST_EQUAL(ret, 0);
481 } else if (*cert_check_file != '\0') {
482 f = fopen(cert_check_file, "r");
483 TEST_ASSERT(f != NULL);
484 olen = fread(check_buf, 1, sizeof(check_buf), f);
485 fclose(f);
486 TEST_ASSERT(olen < sizeof(check_buf));
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 TEST_EQUAL(olen, pem_len);
489 TEST_ASSERT(olen >= pem_len - 1);
490 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100491 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 der_len = mbedtls_x509write_crt_der(&crt, buf, sizeof(buf),
494 mbedtls_test_rnd_pseudo_rand,
495 &rnd_info);
496 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100499 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 }
Andres AGe0af9952016-09-07 11:09:44 +0100501
Werner Lewisacd01e52022-05-10 12:23:13 +0100502 // Not testing against file, check date format
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (*cert_check_file == '\0') {
Werner Lewisacd01e52022-05-10 12:23:13 +0100504 // UTC tag if before 2050, 2 digits less for year
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if (not_before[0] == '2' && (not_before[1] > '0' || not_before[2] > '4')) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100506 before_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 } else {
Werner Lewisacd01e52022-05-10 12:23:13 +0100508 before_tag = MBEDTLS_ASN1_UTC_TIME;
509 not_before += 2;
510 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 if (not_after[0] == '2' && (not_after[1] > '0' || not_after[2] > '4')) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100512 after_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 } else {
Werner Lewisacd01e52022-05-10 12:23:13 +0100514 after_tag = MBEDTLS_ASN1_UTC_TIME;
515 not_after += 2;
516 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 end = buf + sizeof(buf);
518 for (p = end - der_len; p < end;) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100519 tag = *p++;
520 sz = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if (tag == MBEDTLS_ASN1_UTC_TIME || tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100522 // Check correct tag and time written
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 TEST_ASSERT(before_tag == tag);
524 TEST_ASSERT(memcmp(p, not_before, sz - 1) == 0);
Werner Lewisacd01e52022-05-10 12:23:13 +0100525 p += sz;
526 tag = *p++;
527 sz = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 TEST_ASSERT(after_tag == tag);
529 TEST_ASSERT(memcmp(p, not_after, sz - 1) == 0);
Werner Lewisacd01e52022-05-10 12:23:13 +0100530 break;
531 }
532 // Increment if long form ASN1 length
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 if (sz & 0x80) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100534 p += sz & 0x0F;
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 }
536 if (tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100537 p += sz;
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 }
Werner Lewisacd01e52022-05-10 12:23:13 +0100539 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 TEST_ASSERT(p < end);
Werner Lewisacd01e52022-05-10 12:23:13 +0100541 }
542
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100543#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge6ed23c2022-04-22 09:44:04 +0200544 // When using PSA crypto, RNG isn't controllable, result length isn't
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100545 // deterministic over multiple runs, removing a single byte isn't enough to
546 // go into the MBEDTLS_ERR_ASN1_BUF_TOO_SMALL error case
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if (issuer_key_type != MBEDTLS_PK_RSA) {
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100548 der_len /= 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 } else
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100550#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 der_len -= 1;
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 ret = mbedtls_x509write_crt_der(&crt, buf, (size_t) (der_len),
554 mbedtls_test_rnd_pseudo_rand, &rnd_info);
555 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100556
Paul Bakkerbd51b262014-07-10 15:26:12 +0200557exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 mbedtls_x509write_crt_free(&crt);
559 mbedtls_pk_free(&issuer_key_alt);
560 mbedtls_pk_free(&subject_key);
561 mbedtls_pk_free(&issuer_key);
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100562#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
563 mbedtls_mpi_free(&serial_mpi);
564#endif
Neil Armstrong98f899c2022-03-16 17:42:42 +0100565#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 psa_destroy_key(key_id);
Neil Armstrong98f899c2022-03-16 17:42:42 +0100567#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 USE_PSA_DONE();
Paul Bakker2397cf32013-09-08 15:58:15 +0200569}
570/* END_CASE */
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200571
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100572/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_WRITE_C */
573void x509_set_serial_check()
574{
575 mbedtls_x509write_cert ctx;
576 uint8_t invalid_serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN + 1];
577
578 memset(invalid_serial, 0x01, sizeof(invalid_serial));
579
Valerio Settiea19d2d2023-01-09 17:21:17 +0100580#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100581 mbedtls_mpi serial_mpi;
582
583 mbedtls_mpi_init(&serial_mpi);
584 TEST_EQUAL(mbedtls_mpi_read_binary(&serial_mpi, invalid_serial,
585 sizeof(invalid_serial)), 0);
586 TEST_EQUAL(mbedtls_x509write_crt_set_serial(&ctx, &serial_mpi),
587 MBEDTLS_ERR_X509_BAD_INPUT_DATA);
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100588#endif
589
Valerio Settiaf4815c2023-01-26 17:43:09 +0100590 TEST_EQUAL(mbedtls_x509write_crt_set_serial_raw(&ctx, invalid_serial,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100591 sizeof(invalid_serial)),
592 MBEDTLS_ERR_X509_BAD_INPUT_DATA);
593
Valerio Settia87f8392023-01-26 18:00:50 +0100594exit:
595#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
596 mbedtls_mpi_free(&serial_mpi);
597#else
598 ;
599#endif
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100600}
601/* END_CASE */
602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100604void mbedtls_x509_string_to_names(char *name, char *parsed_name, int result
605 )
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200606{
607 int ret;
608 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_asn1_named_data *names = NULL;
610 mbedtls_x509_name parsed, *parsed_cur, *parsed_prv;
Manuel Pégourié-Gonnard4fd0b252015-06-26 14:15:48 +0200611 unsigned char buf[1024], out[1024], *c;
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 memset(&parsed, 0, sizeof(parsed));
614 memset(out, 0, sizeof(out));
615 memset(buf, 0, sizeof(buf));
616 c = buf + sizeof(buf);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 ret = mbedtls_x509_string_to_names(&names, name);
619 TEST_ASSERT(ret == result);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200620
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 if (ret != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200622 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 }
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 ret = mbedtls_x509_write_names(&c, buf, names);
626 TEST_ASSERT(ret > 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 TEST_ASSERT(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
629 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) == 0);
630 TEST_ASSERT(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed) == 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 ret = mbedtls_x509_dn_gets((char *) out, sizeof(out), &parsed);
633 TEST_ASSERT(ret > 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 TEST_ASSERT(strcmp((char *) out, parsed_name) == 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200636
637exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 mbedtls_asn1_free_named_data_list(&names);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200639
640 parsed_cur = parsed.next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 while (parsed_cur != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200642 parsed_prv = parsed_cur;
643 parsed_cur = parsed_cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 mbedtls_free(parsed_prv);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200645 }
646}
647/* END_CASE */