blob: 61b317c625e6bcaab1182e3ca612203f0b228579 [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 Settid3f7df42022-10-19 15:14:29 +02008#include "mbedtls/asn1write.h"
Manuel Pégourié-Gonnardfeb03962020-08-20 09:59:33 +02009
Hanno Becker418a6222017-09-14 07:51:28 +010010#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010011int mbedtls_rsa_decrypt_func(void *ctx, int mode, size_t *olen,
12 const unsigned char *input, unsigned char *output,
13 size_t output_max_len)
Hanno Becker418a6222017-09-14 07:51:28 +010014{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010015 return mbedtls_rsa_pkcs1_decrypt((mbedtls_rsa_context *) ctx, NULL, NULL, mode, olen,
16 input, output, output_max_len);
Hanno Becker418a6222017-09-14 07:51:28 +010017}
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010018int mbedtls_rsa_sign_func(void *ctx,
19 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
20 int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
21 const unsigned char *hash, unsigned char *sig)
Hanno Becker418a6222017-09-14 07:51:28 +010022{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010023 return mbedtls_rsa_pkcs1_sign((mbedtls_rsa_context *) ctx, f_rng, p_rng, mode,
24 md_alg, hashlen, hash, sig);
Hanno Becker418a6222017-09-14 07:51:28 +010025}
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010026size_t mbedtls_rsa_key_len_func(void *ctx)
Hanno Becker418a6222017-09-14 07:51:28 +010027{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010028 return ((const mbedtls_rsa_context *) ctx)->len;
Hanno Becker418a6222017-09-14 07:51:28 +010029}
30#endif /* MBEDTLS_RSA_C */
31
Gilles Peskinef4e672e2020-01-31 14:22:10 +010032#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
33 defined(MBEDTLS_PEM_WRITE_C) && defined(MBEDTLS_X509_CSR_WRITE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010034static int x509_crt_verifycsr(const unsigned char *buf, size_t buflen)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050035{
36 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
37 const mbedtls_md_info_t *md_info;
38 mbedtls_x509_csr csr;
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010039 int ret = 0;
40
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041 mbedtls_x509_csr_init(&csr);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050042
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010048 md_info = mbedtls_md_info_from_type(csr.sig_md);
49 if (mbedtls_md(md_info, csr.cri.p, csr.cri.len, hash) != 0) {
Andrzej Kurek4b114072018-11-19 18:04:01 -050050 /* Note: this can't happen except after an internal error */
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010051 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
52 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050053 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010055 if (mbedtls_pk_verify_ext(csr.sig_pk, csr.sig_opts, &csr.pk,
56 csr.sig_md, hash, mbedtls_md_get_size(md_info),
57 csr.sig.p, csr.sig.len) != 0) {
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010058 ret = MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
59 goto cleanup;
Andrzej Kurek4b114072018-11-19 18:04:01 -050060 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050061
Hanno Beckerbf2dacb2019-06-03 16:28:24 +010062cleanup:
63
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064 mbedtls_x509_csr_free(&csr);
65 return ret;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050066}
Gilles Peskinef4e672e2020-01-31 14:22:10 +010067#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_PEM_WRITE_C && MBEDTLS_X509_CSR_WRITE_C */
Andrzej Kurek5f7bad32018-11-19 10:12:37 -050068
Valerio Settid3f7df42022-10-19 15:14:29 +020069#if defined(MBEDTLS_X509_CSR_WRITE_C)
70
71/*
72 * The size of this temporary buffer is given by the sequence of functions
73 * called hereinafter:
74 * - mbedtls_asn1_write_oid()
75 * - 8 bytes for MBEDTLS_OID_EXTENDED_KEY_USAGE raw value
76 * - 1 byte for MBEDTLS_OID_EXTENDED_KEY_USAGE length
77 * - 1 byte for MBEDTLS_ASN1_OID tag
78 * - mbedtls_asn1_write_len()
79 * - 1 byte since we're dealing with sizes which are less than 0x80
80 * - mbedtls_asn1_write_tag()
81 * - 1 byte
82 *
83 * This length is fine as long as this function is called using the
84 * MBEDTLS_OID_SERVER_AUTH OID. If this is changed in the future, then this
85 * buffer's length should be adjusted accordingly.
86 * Unfortunately there's no predefined max size for OIDs which can be used
87 * to set an overall upper boundary which is always guaranteed.
88 */
89#define EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH 12
90
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091static int csr_set_extended_key_usage(mbedtls_x509write_csr *ctx,
92 const char *oid, size_t oid_len)
Valerio Settid3f7df42022-10-19 15:14:29 +020093{
94 unsigned char buf[EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH] = { 0 };
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095 unsigned char *p = buf + sizeof(buf);
Valerio Settid3f7df42022-10-19 15:14:29 +020096 int ret;
97 size_t len = 0;
98
99 /*
100 * Following functions fail anyway if the temporary buffer is not large,
101 * but we set an extra check here to emphasize a possible source of errors
102 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 if (oid_len > EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH) {
Valerio Settid3f7df42022-10-19 15:14:29 +0200104 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
105 }
106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(&p, buf, oid, oid_len));
108 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, ret));
109 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, buf,
110 MBEDTLS_ASN1_CONSTRUCTED |
111 MBEDTLS_ASN1_SEQUENCE));
Valerio Settid3f7df42022-10-19 15:14:29 +0200112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 ret = mbedtls_x509write_csr_set_extension(ctx,
114 MBEDTLS_OID_EXTENDED_KEY_USAGE,
115 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
116 p,
117 len);
Valerio Settid3f7df42022-10-19 15:14:29 +0200118
119 return ret;
120}
121#endif /* MBEDTLS_X509_CSR_WRITE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200122/* END_HEADER */
Paul Bakker6d620502012-02-16 14:09:13 +0000123
Paul Bakker33b43f12013-08-20 11:48:36 +0200124/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 * depends_on:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO:MBEDTLS_PK_PARSE_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200126 * END_DEPENDENCIES
127 */
Paul Bakker6d620502012-02-16 14:09:13 +0000128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130void x509_csr_check(char *key_file, char *cert_req_check_file, int md_type,
131 int key_usage, int set_key_usage, int cert_type,
132 int set_cert_type, int set_extension)
Paul Bakker6d620502012-02-16 14:09:13 +0000133{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_pk_context key;
135 mbedtls_x509write_csr req;
Andres AGe0af9952016-09-07 11:09:44 +0100136 unsigned char buf[4096];
Paul Bakker6d620502012-02-16 14:09:13 +0000137 unsigned char check_buf[4000];
138 int ret;
Paul Elliott557b8d62020-11-19 09:46:56 +0000139 size_t olen = 0, pem_len = 0, buf_index;
Andres AGe0af9952016-09-07 11:09:44 +0100140 int der_len = -1;
Paul Bakker6d620502012-02-16 14:09:13 +0000141 FILE *f;
Paul Bakker3a8cb6f2013-12-30 20:41:54 +0100142 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200143 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker6d620502012-02-16 14:09:13 +0000144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 mbedtls_x509write_csr_init(&req);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 mbedtls_pk_init(&key);
Valerio Settie7373a82023-04-19 14:53:36 +0200149 USE_PSA_INIT();
150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL) == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153 mbedtls_x509write_csr_set_md_alg(&req, md_type);
154 mbedtls_x509write_csr_set_key(&req, &key);
155 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
156 if (set_key_usage != 0) {
157 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
158 }
159 if (set_cert_type != 0) {
160 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
161 }
162 if (set_extension != 0) {
163 TEST_ASSERT(csr_set_extended_key_usage(&req, MBEDTLS_OID_SERVER_AUTH,
164 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000165 }
166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf),
168 mbedtls_test_rnd_pseudo_rand, &rnd_info);
169 TEST_ASSERT(ret == 0);
Paul Bakker6d620502012-02-16 14:09:13 +0000170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171 pem_len = strlen((char *) buf);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100173 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
174 TEST_ASSERT(buf[buf_index] == 0);
175 }
Andres AGe0af9952016-09-07 11:09:44 +0100176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177 f = fopen(cert_req_check_file, "r");
178 TEST_ASSERT(f != NULL);
179 olen = fread(check_buf, 1, sizeof(check_buf), f);
180 fclose(f);
181
182 TEST_ASSERT(olen >= pem_len - 1);
183 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
184
185 der_len = mbedtls_x509write_csr_der(&req, buf, sizeof(buf),
186 mbedtls_test_rnd_pseudo_rand,
187 &rnd_info);
188 TEST_ASSERT(der_len >= 0);
189
190 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100191 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 }
Andres AGe0af9952016-09-07 11:09:44 +0100193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 ret = mbedtls_x509write_csr_der(&req, buf, (size_t) (der_len - 1),
195 mbedtls_test_rnd_pseudo_rand, &rnd_info);
196 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100197
Paul Bakkerbd51b262014-07-10 15:26:12 +0200198exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 mbedtls_x509write_csr_free(&req);
200 mbedtls_pk_free(&key);
Valerio Settie7373a82023-04-19 14:53:36 +0200201 USE_PSA_DONE();
Paul Bakker6d620502012-02-16 14:09:13 +0000202}
Paul Bakker33b43f12013-08-20 11:48:36 +0200203/* END_CASE */
Paul Bakker2397cf32013-09-08 15:58:15 +0200204
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500205/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C:MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206void x509_csr_check_opaque(char *key_file, int md_type, int key_usage,
207 int cert_type)
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500208{
209 mbedtls_pk_context key;
Paul Elliott417b8d92024-10-25 12:41:28 +0100210 mbedtls_pk_init(&key);
211
Ronald Cron5425a212020-08-04 14:58:35 +0200212 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500213 psa_algorithm_t md_alg_psa;
Paul Elliott417b8d92024-10-25 12:41:28 +0100214
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500215 mbedtls_x509write_csr req;
Paul Elliott417b8d92024-10-25 12:41:28 +0100216 mbedtls_x509write_csr_init(&req);
217
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500218 unsigned char buf[4096];
219 int ret;
220 size_t pem_len = 0;
221 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Ronald Cron351f0ee2020-06-10 12:12:18 +0200222 mbedtls_test_rnd_pseudo_info rnd_info;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500224
Neil Armstrong11048662022-07-20 15:49:49 +0200225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 USE_PSA_INIT();
Neil Armstrong11048662022-07-20 15:49:49 +0200227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 md_alg_psa = mbedtls_psa_translate_md((mbedtls_md_type_t) md_type);
229 TEST_ASSERT(md_alg_psa != MBEDTLS_MD_NONE);
Andrzej Kurek967cfd12018-11-20 02:53:17 -0500230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL) == 0);
232 TEST_ASSERT(mbedtls_pk_wrap_as_opaque(&key, &key_id, md_alg_psa) == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 mbedtls_x509write_csr_set_md_alg(&req, md_type);
235 mbedtls_x509write_csr_set_key(&req, &key);
236 TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0);
237 if (key_usage != 0) {
238 TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0);
239 }
240 if (cert_type != 0) {
241 TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0);
242 }
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf) - 1,
245 mbedtls_test_rnd_pseudo_rand, &rnd_info);
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 TEST_ASSERT(ret == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249 pem_len = strlen((char *) buf);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500250 buf[pem_len] = '\0';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0);
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500252
253exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 mbedtls_x509write_csr_free(&req);
255 mbedtls_pk_free(&key);
256 psa_destroy_key(key_id);
Valerio Settie7373a82023-04-19 14:53:36 +0200257 USE_PSA_DONE();
Andrzej Kurek5f7bad32018-11-19 10:12:37 -0500258}
259/* END_CASE */
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CRT_WRITE_C:MBEDTLS_SHA1_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262void x509_crt_check(char *subject_key_file, char *subject_pwd,
263 char *subject_name, char *issuer_key_file,
264 char *issuer_pwd, char *issuer_name,
265 char *serial_str, char *not_before, char *not_after,
266 int md_type, int key_usage, int set_key_usage,
267 int cert_type, int set_cert_type, int auth_ident,
268 int ver, char *cert_check_file, int rsa_alt, int is_ca)
Paul Bakker2397cf32013-09-08 15:58:15 +0200269{
Hanno Becker418a6222017-09-14 07:51:28 +0100270 mbedtls_pk_context subject_key, issuer_key, issuer_key_alt;
271 mbedtls_pk_context *key = &issuer_key;
272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_x509write_cert crt;
Andres AGe0af9952016-09-07 11:09:44 +0100274 unsigned char buf[4096];
Paul Bakker2397cf32013-09-08 15:58:15 +0200275 unsigned char check_buf[5000];
Werner Lewis1b54a052022-05-10 12:23:13 +0100276 unsigned char *p, *end;
277 unsigned char tag, sz;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_mpi serial;
Werner Lewis1b54a052022-05-10 12:23:13 +0100279 int ret, before_tag, after_tag;
Paul Elliott557b8d62020-11-19 09:46:56 +0000280 size_t olen = 0, pem_len = 0, buf_index = 0;
Andres AGe0af9952016-09-07 11:09:44 +0100281 int der_len = -1;
Paul Bakker2397cf32013-09-08 15:58:15 +0200282 FILE *f;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200283 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker2397cf32013-09-08 15:58:15 +0200284
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285 memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info));
286 mbedtls_mpi_init(&serial);
Hanno Becker418a6222017-09-14 07:51:28 +0100287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288 mbedtls_pk_init(&subject_key);
289 mbedtls_pk_init(&issuer_key);
290 mbedtls_pk_init(&issuer_key_alt);
Hanno Becker418a6222017-09-14 07:51:28 +0100291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292 mbedtls_x509write_crt_init(&crt);
Valerio Settie7373a82023-04-19 14:53:36 +0200293 USE_PSA_INIT();
Paul Bakker2397cf32013-09-08 15:58:15 +0200294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 TEST_ASSERT(mbedtls_pk_parse_keyfile(&subject_key, subject_key_file,
296 subject_pwd) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100297
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100298 TEST_ASSERT(mbedtls_pk_parse_keyfile(&issuer_key, issuer_key_file,
299 issuer_pwd) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100300
Dave Rodgmanbd2b8e42023-01-20 11:39:00 +0000301#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Hanno Becker418a6222017-09-14 07:51:28 +0100302 /* For RSA PK contexts, create a copy as an alternative RSA context. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 if (rsa_alt == 1 && mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_RSA) {
304 TEST_ASSERT(mbedtls_pk_setup_rsa_alt(&issuer_key_alt,
305 mbedtls_pk_rsa(issuer_key),
306 mbedtls_rsa_decrypt_func,
307 mbedtls_rsa_sign_func,
308 mbedtls_rsa_key_len_func) == 0);
Hanno Becker418a6222017-09-14 07:51:28 +0100309
310 key = &issuer_key_alt;
311 }
Manuel Pégourié-Gonnard147b28e2018-03-12 15:26:59 +0100312#else
313 (void) rsa_alt;
314#endif
Hanno Becker418a6222017-09-14 07:51:28 +0100315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 TEST_ASSERT(mbedtls_test_read_mpi(&serial, serial_str) == 0);
Paul Bakker2397cf32013-09-08 15:58:15 +0200317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 if (ver != -1) {
319 mbedtls_x509write_crt_set_version(&crt, ver);
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100320 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200321
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 TEST_ASSERT(mbedtls_x509write_crt_set_serial(&crt, &serial) == 0);
323 TEST_ASSERT(mbedtls_x509write_crt_set_validity(&crt, not_before,
324 not_after) == 0);
325 mbedtls_x509write_crt_set_md_alg(&crt, md_type);
326 TEST_ASSERT(mbedtls_x509write_crt_set_issuer_name(&crt, issuer_name) == 0);
327 TEST_ASSERT(mbedtls_x509write_crt_set_subject_name(&crt, subject_name) == 0);
328 mbedtls_x509write_crt_set_subject_key(&crt, &subject_key);
Paul Bakker2397cf32013-09-08 15:58:15 +0200329
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 mbedtls_x509write_crt_set_issuer_key(&crt, key);
331
332 if (crt.version >= MBEDTLS_X509_CRT_VERSION_3) {
333 /* For the CA case, a path length of -1 means unlimited. */
334 TEST_ASSERT(mbedtls_x509write_crt_set_basic_constraints(&crt, is_ca,
335 (is_ca ? -1 : 0)) == 0);
336 TEST_ASSERT(mbedtls_x509write_crt_set_subject_key_identifier(&crt) == 0);
337 if (auth_ident) {
338 TEST_ASSERT(mbedtls_x509write_crt_set_authority_key_identifier(&crt) == 0);
339 }
340 if (set_key_usage != 0) {
341 TEST_ASSERT(mbedtls_x509write_crt_set_key_usage(&crt, key_usage) == 0);
342 }
343 if (set_cert_type != 0) {
344 TEST_ASSERT(mbedtls_x509write_crt_set_ns_cert_type(&crt, cert_type) == 0);
345 }
346 }
347
348 ret = mbedtls_x509write_crt_pem(&crt, buf, sizeof(buf),
349 mbedtls_test_rnd_pseudo_rand, &rnd_info);
350 TEST_ASSERT(ret == 0);
351
352 pem_len = strlen((char *) buf);
Paul Bakker2397cf32013-09-08 15:58:15 +0200353
Paul Elliott557b8d62020-11-19 09:46:56 +0000354 // check that the rest of the buffer remains clear
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) {
356 TEST_ASSERT(buf[buf_index] == 0);
Paul Elliott557b8d62020-11-19 09:46:56 +0000357 }
358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 if (*cert_check_file != '\0') {
360 f = fopen(cert_check_file, "r");
361 TEST_ASSERT(f != NULL);
362 olen = fread(check_buf, 1, sizeof(check_buf), f);
363 fclose(f);
364 TEST_ASSERT(olen < sizeof(check_buf));
365 TEST_ASSERT(olen >= pem_len - 1);
366 TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0);
Werner Lewis1b54a052022-05-10 12:23:13 +0100367 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100369 der_len = mbedtls_x509write_crt_der(&crt, buf, sizeof(buf),
370 mbedtls_test_rnd_pseudo_rand,
371 &rnd_info);
372 TEST_ASSERT(der_len >= 0);
Andres AGe0af9952016-09-07 11:09:44 +0100373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 if (der_len == 0) {
Andres AGe0af9952016-09-07 11:09:44 +0100375 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 }
Andres AGe0af9952016-09-07 11:09:44 +0100377
Werner Lewis1b54a052022-05-10 12:23:13 +0100378 // Not testing against file, check date format
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100379 if (*cert_check_file == '\0') {
Werner Lewis1b54a052022-05-10 12:23:13 +0100380 // UTC tag if before 2050, 2 digits less for year
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100381 if (not_before[0] == '2' && (not_before[1] > '0' || not_before[2] > '4')) {
Werner Lewis1b54a052022-05-10 12:23:13 +0100382 before_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 } else {
Werner Lewis1b54a052022-05-10 12:23:13 +0100384 before_tag = MBEDTLS_ASN1_UTC_TIME;
385 not_before += 2;
386 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100387 if (not_after[0] == '2' && (not_after[1] > '0' || not_after[2] > '4')) {
Werner Lewis1b54a052022-05-10 12:23:13 +0100388 after_tag = MBEDTLS_ASN1_GENERALIZED_TIME;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 } else {
Werner Lewis1b54a052022-05-10 12:23:13 +0100390 after_tag = MBEDTLS_ASN1_UTC_TIME;
391 not_after += 2;
392 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 end = buf + sizeof(buf);
394 for (p = end - der_len; p < end;) {
Werner Lewis1b54a052022-05-10 12:23:13 +0100395 tag = *p++;
396 sz = *p++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100397 if (tag == MBEDTLS_ASN1_UTC_TIME || tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Werner Lewis1b54a052022-05-10 12:23:13 +0100398 // Check correct tag and time written
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100399 TEST_ASSERT(before_tag == tag);
400 TEST_ASSERT(memcmp(p, not_before, sz - 1) == 0);
Werner Lewis1b54a052022-05-10 12:23:13 +0100401 p += sz;
402 tag = *p++;
403 sz = *p++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 TEST_ASSERT(after_tag == tag);
405 TEST_ASSERT(memcmp(p, not_after, sz - 1) == 0);
Werner Lewis1b54a052022-05-10 12:23:13 +0100406 break;
407 }
408 // Increment if long form ASN1 length
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 if (sz & 0x80) {
Werner Lewis1b54a052022-05-10 12:23:13 +0100410 p += sz & 0x0F;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 }
412 if (tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
Werner Lewis1b54a052022-05-10 12:23:13 +0100413 p += sz;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414 }
Werner Lewis1b54a052022-05-10 12:23:13 +0100415 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416 TEST_ASSERT(p < end);
Werner Lewis1b54a052022-05-10 12:23:13 +0100417 }
418
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100419 ret = mbedtls_x509write_crt_der(&crt, buf, (size_t) (der_len - 1),
420 mbedtls_test_rnd_pseudo_rand, &rnd_info);
421 TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
Andres AGe0af9952016-09-07 11:09:44 +0100422
Paul Bakkerbd51b262014-07-10 15:26:12 +0200423exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100424 mbedtls_x509write_crt_free(&crt);
425 mbedtls_pk_free(&issuer_key_alt);
426 mbedtls_pk_free(&subject_key);
427 mbedtls_pk_free(&issuer_key);
428 mbedtls_mpi_free(&serial);
Valerio Settie7373a82023-04-19 14:53:36 +0200429 USE_PSA_DONE();
Paul Bakker2397cf32013-09-08 15:58:15 +0200430}
431/* END_CASE */
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200432
Valerio Setti5b787142023-01-13 08:40:26 +0100433/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_WRITE_C */
434void x509_set_serial_check()
435{
436 mbedtls_x509write_cert ctx;
437 mbedtls_mpi serial_mpi;
438 uint8_t invalid_serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN + 1];
439
David Horstmannec4c47f2023-12-08 18:27:48 +0000440 mbedtls_mpi_init(&serial_mpi);
441
Valerio Settie7373a82023-04-19 14:53:36 +0200442 USE_PSA_INIT();
Valerio Setti5b787142023-01-13 08:40:26 +0100443 memset(invalid_serial, 0x01, sizeof(invalid_serial));
444
Valerio Setti5b787142023-01-13 08:40:26 +0100445 TEST_EQUAL(mbedtls_mpi_read_binary(&serial_mpi, invalid_serial,
446 sizeof(invalid_serial)), 0);
447 TEST_EQUAL(mbedtls_x509write_crt_set_serial(&ctx, &serial_mpi),
448 MBEDTLS_ERR_X509_BAD_INPUT_DATA);
Valerio Setti7ba00372023-01-26 18:03:27 +0100449
450exit:
Valerio Setti5b787142023-01-13 08:40:26 +0100451 mbedtls_mpi_free(&serial_mpi);
Valerio Settie7373a82023-04-19 14:53:36 +0200452 USE_PSA_DONE();
Valerio Setti5b787142023-01-13 08:40:26 +0100453}
454/* END_CASE */
455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457void mbedtls_x509_string_to_names(char *name, char *parsed_name, int result
458 )
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200459{
460 int ret;
461 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_asn1_named_data *names = NULL;
Gilles Peskine11f41792023-10-17 16:35:20 +0200463 mbedtls_x509_name parsed;
464 memset(&parsed, 0, sizeof(parsed));
465 mbedtls_x509_name *parsed_cur = NULL;
466 mbedtls_x509_name *parsed_prv = NULL;
467 unsigned char buf[1024] = { 0 };
468 unsigned char out[1024] = { 0 };
469 unsigned char *c = buf + sizeof(buf);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200470
Valerio Settie7373a82023-04-19 14:53:36 +0200471 USE_PSA_INIT();
472
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100473 ret = mbedtls_x509_string_to_names(&names, name);
474 TEST_ASSERT(ret == result);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200475
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476 if (ret != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200477 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 }
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480 ret = mbedtls_x509_write_names(&c, buf, names);
481 TEST_ASSERT(ret > 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100483 TEST_ASSERT(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
484 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) == 0);
485 TEST_ASSERT(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed) == 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100487 ret = mbedtls_x509_dn_gets((char *) out, sizeof(out), &parsed);
488 TEST_ASSERT(ret > 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200489
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100490 TEST_ASSERT(strcmp((char *) out, parsed_name) == 0);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200491
492exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100493 mbedtls_asn1_free_named_data_list(&names);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200494
495 parsed_cur = parsed.next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100496 while (parsed_cur != 0) {
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200497 parsed_prv = parsed_cur;
498 parsed_cur = parsed_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100499 mbedtls_free(parsed_prv);
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200500 }
Valerio Settie7373a82023-04-19 14:53:36 +0200501 USE_PSA_DONE();
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200502}
503/* END_CASE */
Jonathan Winzig61f4fc22024-01-10 13:26:12 +0100504
505/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_WRITE_C */
506void x509_set_extension_length_check()
507{
508 int ret = 0;
509
510 mbedtls_x509write_csr ctx;
511 mbedtls_x509write_csr_init(&ctx);
512
513 unsigned char buf[EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH] = { 0 };
514 unsigned char *p = buf + sizeof(buf);
515
516 ret = mbedtls_x509_set_extension(&(ctx.extensions),
517 MBEDTLS_OID_EXTENDED_KEY_USAGE,
518 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
519 0,
520 p,
521 SIZE_MAX);
522 TEST_ASSERT(MBEDTLS_ERR_X509_BAD_INPUT_DATA == ret);
523}
524/* END_CASE */