blob: c719b9660af0315e1bf44404ceb5a06f918e3d07 [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"
Manuel Pégourié-Gonnardfeb03962020-08-20 09:59:33 +02009
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +020010#include "hash_info.h"
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020011#include "mbedtls/legacy_or_psa.h"
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +020012
Hanno Becker418a6222017-09-14 07:51:28 +010013#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010014int mbedtls_rsa_decrypt_func(void *ctx, size_t *olen,
15 const unsigned char *input, unsigned char *output,
16 size_t output_max_len)
Hanno Becker418a6222017-09-14 07:51:28 +010017{
Gilles Peskine449bd832023-01-11 14:50:10 +010018 return mbedtls_rsa_pkcs1_decrypt((mbedtls_rsa_context *) ctx, NULL, NULL,
19 olen, input, output, output_max_len);
Hanno Becker418a6222017-09-14 07:51:28 +010020}
Gilles Peskine449bd832023-01-11 14:50:10 +010021int mbedtls_rsa_sign_func(void *ctx,
22 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
23 mbedtls_md_type_t md_alg, unsigned int hashlen,
24 const unsigned char *hash, unsigned char *sig)
Hanno Becker418a6222017-09-14 07:51:28 +010025{
Gilles Peskine449bd832023-01-11 14:50:10 +010026 return mbedtls_rsa_pkcs1_sign((mbedtls_rsa_context *) ctx, f_rng, p_rng,
27 md_alg, hashlen, hash, sig);
Hanno Becker418a6222017-09-14 07:51:28 +010028}
Gilles Peskine449bd832023-01-11 14:50:10 +010029size_t mbedtls_rsa_key_len_func(void *ctx)
Hanno Becker418a6222017-09-14 07:51:28 +010030{
Gilles Peskine449bd832023-01-11 14:50:10 +010031 return ((const mbedtls_rsa_context *) ctx)->len;
Hanno Becker418a6222017-09-14 07:51:28 +010032}
33#endif /* MBEDTLS_RSA_C */
34
Gilles Peskinef4e672e2020-01-31 14:22:10 +010035#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
36 defined(MBEDTLS_PEM_WRITE_C) && defined(MBEDTLS_X509_CSR_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010037static int x509_crt_verifycsr(const unsigned char *buf, size_t buflen)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050038{
Przemek Stekiel54a54462022-08-01 13:59:12 +020039 unsigned char hash[PSA_HASH_MAX_SIZE];
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050040 mbedtls_x509_csr csr;
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010041 int ret = 0;
42
Gilles Peskine449bd832023-01-11 14:50:10 +010043 mbedtls_x509_csr_init(&csr);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050044
Gilles Peskine449bd832023-01-11 14:50:10 +010045 if (mbedtls_x509_csr_parse(&csr, buf, buflen) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010046 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
47 goto cleanup;
48 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050049
Gilles Peskine449bd832023-01-11 14:50:10 +010050 psa_algorithm_t psa_alg = mbedtls_hash_info_psa_from_md(csr.sig_md);
Przemek Stekiel54a54462022-08-01 13:59:12 +020051 size_t hash_size = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010052 psa_status_t status = psa_hash_compute(psa_alg, csr.cri.p, csr.cri.len,
53 hash, PSA_HASH_MAX_SIZE, &hash_size);
Przemek Stekiel54a54462022-08-01 13:59:12 +020054
Gilles Peskine449bd832023-01-11 14:50:10 +010055 if (status != PSA_SUCCESS) {
Andrzej Kurek4b114072018-11-19 18:04:01 -050056 /* Note: this can't happen except after an internal error */
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010057 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
58 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050059 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050060
Gilles Peskine449bd832023-01-11 14:50:10 +010061 if (mbedtls_pk_verify_ext(csr.sig_pk, csr.sig_opts, &csr.pk,
62 csr.sig_md, hash, mbedtls_hash_info_get_size(csr.sig_md),
63 csr.sig.p, csr.sig.len) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010064 ret = MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
65 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050066 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050067
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010068cleanup:
69
Gilles Peskine449bd832023-01-11 14:50:10 +010070 mbedtls_x509_csr_free(&csr);
71 return ret;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050072}
Gilles Peskinef4e672e2020-01-31 14:22:10 +010073#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_PEM_WRITE_C && MBEDTLS_X509_CSR_WRITE_C */
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050074
Valerio Setti48e8fc72022-10-19 15:14:29 +020075#if defined(MBEDTLS_X509_CSR_WRITE_C)
76
77/*
78 * The size of this temporary buffer is given by the sequence of functions
79 * called hereinafter:
80 * - mbedtls_asn1_write_oid()
81 * - 8 bytes for MBEDTLS_OID_EXTENDED_KEY_USAGE raw value
82 * - 1 byte for MBEDTLS_OID_EXTENDED_KEY_USAGE length
83 * - 1 byte for MBEDTLS_ASN1_OID tag
84 * - mbedtls_asn1_write_len()
85 * - 1 byte since we're dealing with sizes which are less than 0x80
86 * - mbedtls_asn1_write_tag()
87 * - 1 byte
88 *
89 * This length is fine as long as this function is called using the
90 * MBEDTLS_OID_SERVER_AUTH OID. If this is changed in the future, then this
91 * buffer's length should be adjusted accordingly.
92 * Unfortunately there's no predefined max size for OIDs which can be used
93 * to set an overall upper boundary which is always guaranteed.
94 */
95#define EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH 12
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097static int csr_set_extended_key_usage(mbedtls_x509write_csr *ctx,
98 const char *oid, size_t oid_len)
Valerio Setti48e8fc72022-10-19 15:14:29 +020099{
100 unsigned char buf[EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH] = { 0 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 unsigned char *p = buf + sizeof(buf);
Valerio Setti48e8fc72022-10-19 15:14:29 +0200102 int ret;
103 size_t len = 0;
104
105 /*
106 * Following functions fail anyway if the temporary buffer is not large,
107 * but we set an extra check here to emphasize a possible source of errors
108 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 if (oid_len > EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH) {
Valerio Setti48e8fc72022-10-19 15:14:29 +0200110 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
111 }
112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(&p, buf, oid, oid_len));
114 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, ret));
115 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, buf,
116 MBEDTLS_ASN1_CONSTRUCTED |
117 MBEDTLS_ASN1_SEQUENCE));
Valerio Setti48e8fc72022-10-19 15:14:29 +0200118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 ret = mbedtls_x509write_csr_set_extension(ctx,
120 MBEDTLS_OID_EXTENDED_KEY_USAGE,
121 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
122 0,
123 p,
124 len);
Valerio Setti48e8fc72022-10-19 15:14:29 +0200125
126 return ret;
127}
128#endif /* MBEDTLS_X509_CSR_WRITE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200129/* END_HEADER */
Paul Bakker6d620502012-02-16 14:09:13 +0000130
Paul Bakker33b43f12013-08-20 11:48:36 +0200131/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 * depends_on:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO:MBEDTLS_PK_PARSE_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200133 * END_DEPENDENCIES
134 */
Paul Bakker6d620502012-02-16 14:09:13 +0000135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100137void x509_csr_check(char *key_file, char *cert_req_check_file, int md_type,
138 int key_usage, int set_key_usage, int cert_type,
139 int set_cert_type, int set_extension)
Paul Bakker6d620502012-02-16 14:09:13 +0000140{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_pk_context key;
142 mbedtls_x509write_csr req;
Andres AGe0af9952016-09-07 11:09:44 +0100143 unsigned char buf[4096];
Paul Bakker6d620502012-02-16 14:09:13 +0000144 int ret;
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100145#if !defined(MBEDTLS_USE_PSA_CRYPTO)
146 unsigned char check_buf[4000];
Paul Bakker6d620502012-02-16 14:09:13 +0000147 FILE *f;
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100148 size_t olen = 0;
149#endif /* !MBEDTLS_USE_PSA_CRYPTO */
150 size_t pem_len = 0, buf_index;
151 int der_len = -1;
Paul Bakker3a8cb6f2013-12-30 20:41:54 +0100152 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200153 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker6d620502012-02-16 14:09:13 +0000154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 mbedtls_x509write_csr_init(&req);
Neil Armstronga97f1ac2022-07-20 15:49:49 +0200158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 USE_PSA_INIT();
Neil Armstrong5f8328b2022-02-22 17:32:00 +0100160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 mbedtls_pk_init(&key);
162 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL,
163 mbedtls_test_rnd_std_rand, NULL) == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 mbedtls_x509write_csr_set_md_alg(&req, md_type);
166 mbedtls_x509write_csr_set_key(&req, &key);
167 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
168 if (set_key_usage != 0) {
169 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
170 }
171 if (set_cert_type != 0) {
172 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
173 }
174 if (set_extension != 0) {
175 TEST_ASSERT(csr_set_extended_key_usage(&req, MBEDTLS_OID_SERVER_AUTH,
176 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) == 0);
177 }
Paul Bakker8eabfc12013-08-25 10:18:25 +0200178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf),
180 mbedtls_test_rnd_pseudo_rand, &rnd_info);
181 TEST_ASSERT(ret == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 pem_len = strlen((char *) buf);
Paul Bakker6d620502012-02-16 14:09:13 +0000184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
186 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000187 }
188
Neil Armstrong5b320382022-02-21 17:22:10 +0100189#if defined(MBEDTLS_USE_PSA_CRYPTO)
190 // When using PSA crypto, RNG isn't controllable, so cert_req_check_file can't be used
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 (void) cert_req_check_file;
Neil Armstrong5b320382022-02-21 17:22:10 +0100192 buf[pem_len] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0);
Neil Armstrong5b320382022-02-21 17:22:10 +0100194#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 f = fopen(cert_req_check_file, "r");
196 TEST_ASSERT(f != NULL);
197 olen = fread(check_buf, 1, sizeof(check_buf), f);
198 fclose(f);
Paul Bakker6d620502012-02-16 14:09:13 +0000199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 TEST_ASSERT(olen >= pem_len - 1);
201 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
Neil Armstrongc23d2e32022-03-18 15:31:59 +0100202#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 der_len = mbedtls_x509write_csr_der(&req, buf, sizeof(buf),
205 mbedtls_test_rnd_pseudo_rand,
206 &rnd_info);
207 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100210 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 }
Andres AGe0af9952016-09-07 11:09:44 +0100212
Neil Armstrong5b320382022-02-21 17:22:10 +0100213#if defined(MBEDTLS_USE_PSA_CRYPTO)
214 // When using PSA crypto, RNG isn't controllable, result length isn't
215 // deterministic over multiple runs, removing a single byte isn't enough to
216 // go into the MBEDTLS_ERR_ASN1_BUF_TOO_SMALL error case
217 der_len /= 2;
218#else
219 der_len -= 1;
220#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 ret = mbedtls_x509write_csr_der(&req, buf, (size_t) (der_len),
222 mbedtls_test_rnd_pseudo_rand, &rnd_info);
223 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100224
Paul Bakkerbd51b262014-07-10 15:26:12 +0200225exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 mbedtls_x509write_csr_free(&req);
227 mbedtls_pk_free(&key);
228 USE_PSA_DONE();
Paul Bakker6d620502012-02-16 14:09:13 +0000229}
Paul Bakker33b43f12013-08-20 11:48:36 +0200230/* END_CASE */
Paul Bakker2397cf32013-09-08 15:58:15 +0200231
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500232/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C:MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100233void x509_csr_check_opaque(char *key_file, int md_type, int key_usage,
234 int cert_type)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500235{
236 mbedtls_pk_context key;
Ronald Cron5425a212020-08-04 14:58:35 +0200237 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong95974972022-04-22 13:57:44 +0200238 psa_algorithm_t md_alg_psa, alg_psa;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500239 mbedtls_x509write_csr req;
240 unsigned char buf[4096];
241 int ret;
242 size_t pem_len = 0;
243 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200244 mbedtls_test_rnd_pseudo_info rnd_info;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 mbedtls_x509write_csr_init(&req);
Neil Armstronga97f1ac2022-07-20 15:49:49 +0200249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 USE_PSA_INIT();
Neil Armstronga97f1ac2022-07-20 15:49:49 +0200251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 md_alg_psa = mbedtls_hash_info_psa_from_md((mbedtls_md_type_t) md_type);
253 TEST_ASSERT(md_alg_psa != MBEDTLS_MD_NONE);
Andrzej Kurek967cfd12018-11-20 02:53:17 -0500254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 mbedtls_pk_init(&key);
256 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL,
257 mbedtls_test_rnd_std_rand, NULL) == 0);
Neil Armstrong95974972022-04-22 13:57:44 +0200258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
260 alg_psa = PSA_ALG_ECDSA(md_alg_psa);
261 } else if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
262 alg_psa = PSA_ALG_RSA_PKCS1V15_SIGN(md_alg_psa);
263 } else {
264 TEST_ASSUME(!"PK key type not supported in this configuration");
265 }
Neil Armstrong95974972022-04-22 13:57:44 +0200266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 TEST_ASSERT(mbedtls_pk_wrap_as_opaque(&key, &key_id, alg_psa,
268 PSA_KEY_USAGE_SIGN_HASH,
269 PSA_ALG_NONE) == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 mbedtls_x509write_csr_set_md_alg(&req, md_type);
272 mbedtls_x509write_csr_set_key(&req, &key);
273 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
274 if (key_usage != 0) {
275 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
276 }
277 if (cert_type != 0) {
278 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
279 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf) - 1,
282 mbedtls_test_rnd_pseudo_rand, &rnd_info);
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 TEST_ASSERT(ret == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 pem_len = strlen((char *) buf);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500287 buf[pem_len] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500289
Manuel Pégourié-Gonnardfeb03962020-08-20 09:59:33 +0200290
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500291exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 mbedtls_x509write_csr_free(&req);
293 mbedtls_pk_free(&key);
294 psa_destroy_key(key_id);
295 PSA_DONE();
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500296}
297/* END_CASE */
298
Ronald Cron81cd7ab2022-10-18 12:15:27 +0200299/* 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 +0100300void x509_crt_check(char *subject_key_file, char *subject_pwd,
301 char *subject_name, char *issuer_key_file,
302 char *issuer_pwd, char *issuer_name,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100303 data_t *serial_arg, char *not_before, char *not_after,
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 int md_type, int key_usage, int set_key_usage,
305 char *ext_key_usage,
306 int cert_type, int set_cert_type, int auth_ident,
307 int ver, char *cert_check_file, int pk_wrap, int is_ca,
308 char *cert_verify_file)
Paul Bakker2397cf32013-09-08 15:58:15 +0200309{
Hanno Becker418a6222017-09-14 07:51:28 +0100310 mbedtls_pk_context subject_key, issuer_key, issuer_key_alt;
311 mbedtls_pk_context *key = &issuer_key;
312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_x509write_cert crt;
Andres AGe0af9952016-09-07 11:09:44 +0100314 unsigned char buf[4096];
Paul Bakker2397cf32013-09-08 15:58:15 +0200315 unsigned char check_buf[5000];
Werner Lewisacd01e52022-05-10 12:23:13 +0100316 unsigned char *p, *end;
317 unsigned char tag, sz;
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100318#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
319 mbedtls_mpi serial_mpi;
320#endif
Werner Lewisacd01e52022-05-10 12:23:13 +0100321 int ret, before_tag, after_tag;
Paul Elliott557b8d62020-11-19 09:46:56 +0000322 size_t olen = 0, pem_len = 0, buf_index = 0;
Andres AGe0af9952016-09-07 11:09:44 +0100323 int der_len = -1;
Paul Bakker2397cf32013-09-08 15:58:15 +0200324 FILE *f;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200325 mbedtls_test_rnd_pseudo_info rnd_info;
Neil Armstrong98f899c2022-03-16 17:42:42 +0100326#if defined(MBEDTLS_USE_PSA_CRYPTO)
327 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
328#endif
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100329 mbedtls_pk_type_t issuer_key_type;
Paul Bakker2397cf32013-09-08 15:58:15 +0200330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100332#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
333 mbedtls_mpi_init(&serial_mpi);
334#endif
Hanno Becker418a6222017-09-14 07:51:28 +0100335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 USE_PSA_INIT();
Neil Armstrong5f8328b2022-02-22 17:32:00 +0100337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 mbedtls_pk_init(&subject_key);
339 mbedtls_pk_init(&issuer_key);
340 mbedtls_pk_init(&issuer_key_alt);
Hanno Becker418a6222017-09-14 07:51:28 +0100341
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 mbedtls_x509write_crt_init(&crt);
Paul Bakker2397cf32013-09-08 15:58:15 +0200343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 TEST_ASSERT(mbedtls_pk_parse_keyfile(&subject_key, subject_key_file,
345 subject_pwd, mbedtls_test_rnd_std_rand, NULL) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 TEST_ASSERT(mbedtls_pk_parse_keyfile(&issuer_key, issuer_key_file,
348 issuer_pwd, mbedtls_test_rnd_std_rand, NULL) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 issuer_key_type = mbedtls_pk_get_type(&issuer_key);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100351
Manuel Pégourié-Gonnard147b28e2018-03-12 15:26:59 +0100352#if defined(MBEDTLS_RSA_C)
Hanno Becker418a6222017-09-14 07:51:28 +0100353 /* For RSA PK contexts, create a copy as an alternative RSA context. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 if (pk_wrap == 1 && issuer_key_type == MBEDTLS_PK_RSA) {
355 TEST_ASSERT(mbedtls_pk_setup_rsa_alt(&issuer_key_alt,
356 mbedtls_pk_rsa(issuer_key),
357 mbedtls_rsa_decrypt_func,
358 mbedtls_rsa_sign_func,
359 mbedtls_rsa_key_len_func) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100360
361 key = &issuer_key_alt;
362 }
Manuel Pégourié-Gonnard147b28e2018-03-12 15:26:59 +0100363#endif
Hanno Becker418a6222017-09-14 07:51:28 +0100364
Neil Armstrong98f899c2022-03-16 17:42:42 +0100365#if defined(MBEDTLS_USE_PSA_CRYPTO)
366 /* For Opaque PK contexts, wrap key as an Opaque RSA context. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (pk_wrap == 2) {
Neil Armstrong95974972022-04-22 13:57:44 +0200368 psa_algorithm_t alg_psa, md_alg_psa;
Neil Armstrong98f899c2022-03-16 17:42:42 +0100369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 md_alg_psa = mbedtls_hash_info_psa_from_md((mbedtls_md_type_t) md_type);
371 TEST_ASSERT(md_alg_psa != MBEDTLS_MD_NONE);
Neil Armstrong95974972022-04-22 13:57:44 +0200372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if (mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_ECKEY) {
374 alg_psa = PSA_ALG_ECDSA(md_alg_psa);
375 } else if (mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_RSA) {
376 alg_psa = PSA_ALG_RSA_PKCS1V15_SIGN(md_alg_psa);
377 } else {
378 TEST_ASSUME(!"PK key type not supported in this configuration");
379 }
Neil Armstrong95974972022-04-22 13:57:44 +0200380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 TEST_ASSERT(mbedtls_pk_wrap_as_opaque(&issuer_key, &key_id, alg_psa,
382 PSA_KEY_USAGE_SIGN_HASH,
383 PSA_ALG_NONE) == 0);
Neil Armstrong98f899c2022-03-16 17:42:42 +0100384 }
385#endif /* MBEDTLS_USE_PSA_CRYPTO */
386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (pk_wrap == 2) {
388 TEST_ASSERT(mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_OPAQUE);
389 }
Neil Armstrong98f899c2022-03-16 17:42:42 +0100390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (ver != -1) {
392 mbedtls_x509write_crt_set_version(&crt, ver);
393 }
Hanno Becker418a6222017-09-14 07:51:28 +0100394
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100395#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100396 TEST_ASSERT(mbedtls_mpi_read_binary(&serial_mpi, serial_arg->x,
397 serial_arg->len) == 0);
398 TEST_ASSERT(mbedtls_x509write_crt_set_serial(&crt, &serial_mpi) == 0);
Valerio Settida0afcc2023-01-05 16:46:59 +0100399#else
Valerio Settiaf4815c2023-01-26 17:43:09 +0100400 TEST_ASSERT(mbedtls_x509write_crt_set_serial_raw(&crt, serial_arg->x,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100401 serial_arg->len) == 0);
Valerio Settida0afcc2023-01-05 16:46:59 +0100402#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 TEST_ASSERT(mbedtls_x509write_crt_set_validity(&crt, not_before,
404 not_after) == 0);
405 mbedtls_x509write_crt_set_md_alg(&crt, md_type);
406 TEST_ASSERT(mbedtls_x509write_crt_set_issuer_name(&crt, issuer_name) == 0);
407 TEST_ASSERT(mbedtls_x509write_crt_set_subject_name(&crt, subject_name) == 0);
408 mbedtls_x509write_crt_set_subject_key(&crt, &subject_key);
Hanno Becker418a6222017-09-14 07:51:28 +0100409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 mbedtls_x509write_crt_set_issuer_key(&crt, key);
Paul Bakker2397cf32013-09-08 15:58:15 +0200411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if (crt.version >= MBEDTLS_X509_CRT_VERSION_3) {
Darren Krahn9c134ce2021-01-13 22:04:45 -0800413 /* For the CA case, a path length of -1 means unlimited. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 TEST_ASSERT(mbedtls_x509write_crt_set_basic_constraints(&crt, is_ca,
415 (is_ca ? -1 : 0)) == 0);
416 TEST_ASSERT(mbedtls_x509write_crt_set_subject_key_identifier(&crt) == 0);
417 if (auth_ident) {
418 TEST_ASSERT(mbedtls_x509write_crt_set_authority_key_identifier(&crt) == 0);
419 }
420 if (set_key_usage != 0) {
421 TEST_ASSERT(mbedtls_x509write_crt_set_key_usage(&crt, key_usage) == 0);
422 }
423 if (set_cert_type != 0) {
424 TEST_ASSERT(mbedtls_x509write_crt_set_ns_cert_type(&crt, cert_type) == 0);
425 }
426 if (strcmp(ext_key_usage, "NULL") != 0) {
Dave Rodgmanec9f6b42022-07-27 14:34:58 +0100427 mbedtls_asn1_sequence exts[2];
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 memset(exts, 0, sizeof(exts));
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100429
430#define SET_OID(x, oid) \
431 do { \
432 x.len = MBEDTLS_OID_SIZE(oid); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 x.p = (unsigned char *) oid; \
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100434 x.tag = MBEDTLS_ASN1_OID; \
Dave Rodgmane2b772d2022-08-11 16:04:13 +0100435 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 while (0)
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 if (strcmp(ext_key_usage, "serverAuth") == 0) {
439 SET_OID(exts[0].buf, MBEDTLS_OID_SERVER_AUTH);
440 } else if (strcmp(ext_key_usage, "codeSigning,timeStamping") == 0) {
441 SET_OID(exts[0].buf, MBEDTLS_OID_CODE_SIGNING);
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100442 exts[0].next = &exts[1];
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 SET_OID(exts[1].buf, MBEDTLS_OID_TIME_STAMPING);
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000444 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 TEST_ASSERT(mbedtls_x509write_crt_set_ext_key_usage(&crt, exts) == 0);
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000446 }
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100447 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 ret = mbedtls_x509write_crt_pem(&crt, buf, sizeof(buf),
450 mbedtls_test_rnd_pseudo_rand, &rnd_info);
451 TEST_ASSERT(ret == 0);
Paul Bakker2397cf32013-09-08 15:58:15 +0200452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 pem_len = strlen((char *) buf);
Paul Bakker2397cf32013-09-08 15:58:15 +0200454
Paul Elliott557b8d62020-11-19 09:46:56 +0000455 // check that the rest of the buffer remains clear
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
457 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000458 }
459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if (issuer_key_type != MBEDTLS_PK_RSA) {
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100461 mbedtls_x509_crt crt_parse, trusted;
462 uint32_t flags;
Paul Bakker2397cf32013-09-08 15:58:15 +0200463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 mbedtls_x509_crt_init(&crt_parse);
465 mbedtls_x509_crt_init(&trusted);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted,
468 cert_verify_file) == 0);
469 TEST_ASSERT(mbedtls_x509_crt_parse(&crt_parse,
470 buf, sizeof(buf)) == 0);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 ret = mbedtls_x509_crt_verify(&crt_parse, &trusted, NULL, NULL, &flags,
473 NULL, NULL);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 mbedtls_x509_crt_free(&crt_parse);
476 mbedtls_x509_crt_free(&trusted);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 TEST_EQUAL(flags, 0);
479 TEST_EQUAL(ret, 0);
480 } else if (*cert_check_file != '\0') {
481 f = fopen(cert_check_file, "r");
482 TEST_ASSERT(f != NULL);
483 olen = fread(check_buf, 1, sizeof(check_buf), f);
484 fclose(f);
485 TEST_ASSERT(olen < sizeof(check_buf));
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 TEST_EQUAL(olen, pem_len);
488 TEST_ASSERT(olen >= pem_len - 1);
489 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100490 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 der_len = mbedtls_x509write_crt_der(&crt, buf, sizeof(buf),
493 mbedtls_test_rnd_pseudo_rand,
494 &rnd_info);
495 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100498 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 }
Andres AGe0af9952016-09-07 11:09:44 +0100500
Werner Lewisacd01e52022-05-10 12:23:13 +0100501 // Not testing against file, check date format
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 if (*cert_check_file == '\0') {
Werner Lewisacd01e52022-05-10 12:23:13 +0100503 // UTC tag if before 2050, 2 digits less for year
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (not_before[0] == '2' && (not_before[1] > '0' || not_before[2] > '4')) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100505 before_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 } else {
Werner Lewisacd01e52022-05-10 12:23:13 +0100507 before_tag = MBEDTLS_ASN1_UTC_TIME;
508 not_before += 2;
509 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if (not_after[0] == '2' && (not_after[1] > '0' || not_after[2] > '4')) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100511 after_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 } else {
Werner Lewisacd01e52022-05-10 12:23:13 +0100513 after_tag = MBEDTLS_ASN1_UTC_TIME;
514 not_after += 2;
515 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 end = buf + sizeof(buf);
517 for (p = end - der_len; p < end;) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100518 tag = *p++;
519 sz = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 if (tag == MBEDTLS_ASN1_UTC_TIME || tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100521 // Check correct tag and time written
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 TEST_ASSERT(before_tag == tag);
523 TEST_ASSERT(memcmp(p, not_before, sz - 1) == 0);
Werner Lewisacd01e52022-05-10 12:23:13 +0100524 p += sz;
525 tag = *p++;
526 sz = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 TEST_ASSERT(after_tag == tag);
528 TEST_ASSERT(memcmp(p, not_after, sz - 1) == 0);
Werner Lewisacd01e52022-05-10 12:23:13 +0100529 break;
530 }
531 // Increment if long form ASN1 length
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if (sz & 0x80) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100533 p += sz & 0x0F;
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 }
535 if (tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
Werner Lewisacd01e52022-05-10 12:23:13 +0100536 p += sz;
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 }
Werner Lewisacd01e52022-05-10 12:23:13 +0100538 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 TEST_ASSERT(p < end);
Werner Lewisacd01e52022-05-10 12:23:13 +0100540 }
541
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100542#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge6ed23c2022-04-22 09:44:04 +0200543 // When using PSA crypto, RNG isn't controllable, result length isn't
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100544 // deterministic over multiple runs, removing a single byte isn't enough to
545 // go into the MBEDTLS_ERR_ASN1_BUF_TOO_SMALL error case
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (issuer_key_type != MBEDTLS_PK_RSA) {
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100547 der_len /= 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 } else
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100549#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 der_len -= 1;
Neil Armstrong6ce6dd92022-03-17 09:38:50 +0100551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 ret = mbedtls_x509write_crt_der(&crt, buf, (size_t) (der_len),
553 mbedtls_test_rnd_pseudo_rand, &rnd_info);
554 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100555
Paul Bakkerbd51b262014-07-10 15:26:12 +0200556exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 mbedtls_x509write_crt_free(&crt);
558 mbedtls_pk_free(&issuer_key_alt);
559 mbedtls_pk_free(&subject_key);
560 mbedtls_pk_free(&issuer_key);
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100561#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
562 mbedtls_mpi_free(&serial_mpi);
563#endif
Neil Armstrong98f899c2022-03-16 17:42:42 +0100564#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 psa_destroy_key(key_id);
Neil Armstrong98f899c2022-03-16 17:42:42 +0100566#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 USE_PSA_DONE();
Paul Bakker2397cf32013-09-08 15:58:15 +0200568}
569/* END_CASE */
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200570
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100571/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_WRITE_C */
572void x509_set_serial_check()
573{
574 mbedtls_x509write_cert ctx;
575 uint8_t invalid_serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN + 1];
576
577 memset(invalid_serial, 0x01, sizeof(invalid_serial));
578
Valerio Settiea19d2d2023-01-09 17:21:17 +0100579#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100580 mbedtls_mpi serial_mpi;
581
582 mbedtls_mpi_init(&serial_mpi);
583 TEST_EQUAL(mbedtls_mpi_read_binary(&serial_mpi, invalid_serial,
584 sizeof(invalid_serial)), 0);
585 TEST_EQUAL(mbedtls_x509write_crt_set_serial(&ctx, &serial_mpi),
586 MBEDTLS_ERR_X509_BAD_INPUT_DATA);
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100587#endif
588
Valerio Settiaf4815c2023-01-26 17:43:09 +0100589 TEST_EQUAL(mbedtls_x509write_crt_set_serial_raw(&ctx, invalid_serial,
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100590 sizeof(invalid_serial)),
591 MBEDTLS_ERR_X509_BAD_INPUT_DATA);
592
Valerio Settia87f8392023-01-26 18:00:50 +0100593exit:
594#if defined(MBEDTLS_TEST_DEPRECATED) && defined(MBEDTLS_BIGNUM_C)
595 mbedtls_mpi_free(&serial_mpi);
596#else
597 ;
598#endif
Valerio Settiaad8dbd2023-01-09 17:20:25 +0100599}
600/* END_CASE */
601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100603void mbedtls_x509_string_to_names(char *name, char *parsed_name, int result
604 )
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200605{
606 int ret;
607 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_asn1_named_data *names = NULL;
609 mbedtls_x509_name parsed, *parsed_cur, *parsed_prv;
Manuel Pégourié-Gonnard4fd0b252015-06-26 14:15:48 +0200610 unsigned char buf[1024], out[1024], *c;
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 memset(&parsed, 0, sizeof(parsed));
613 memset(out, 0, sizeof(out));
614 memset(buf, 0, sizeof(buf));
615 c = buf + sizeof(buf);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200616
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 ret = mbedtls_x509_string_to_names(&names, name);
618 TEST_ASSERT(ret == result);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 if (ret != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200621 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 }
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 ret = mbedtls_x509_write_names(&c, buf, names);
625 TEST_ASSERT(ret > 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 TEST_ASSERT(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
628 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) == 0);
629 TEST_ASSERT(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed) == 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200630
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 ret = mbedtls_x509_dn_gets((char *) out, sizeof(out), &parsed);
632 TEST_ASSERT(ret > 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 TEST_ASSERT(strcmp((char *) out, parsed_name) == 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200635
636exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 mbedtls_asn1_free_named_data_list(&names);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200638
639 parsed_cur = parsed.next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 while (parsed_cur != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200641 parsed_prv = parsed_cur;
642 parsed_cur = parsed_cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 mbedtls_free(parsed_prv);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200644 }
645}
646/* END_CASE */