blob: 29c05745ad4cea2b64072f1cced93a8a9ba27e16 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Azim Khand30ca132017-06-09 04:32:58 +01002#include "mbedtls/bignum.h"
Andres AG4b76aec2016-09-23 13:16:02 +01003#include "mbedtls/x509.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00004#include "mbedtls/x509_crt.h"
5#include "mbedtls/x509_crl.h"
6#include "mbedtls/x509_csr.h"
7#include "mbedtls/pem.h"
8#include "mbedtls/oid.h"
9#include "mbedtls/base64.h"
Chris Jones9f7a6932021-04-14 12:12:09 +010010#include "mbedtls/error.h"
Valerio Setti178b5bd2023-02-13 10:04:28 +010011#include "mbedtls/pk.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +010012#include "string.h"
Paul Bakkerb63b0af2011-01-13 17:54:59 +000013
Simon Butcher9e24b512017-07-28 12:15:13 +010014#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
Hanno Becker3b1422e2017-07-26 13:38:02 +010015#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
Gilles Peskine449bd832023-01-11 14:50:10 +010016 than the current threshold 19. To test larger values, please \
17 adapt the script tests/data_files/dir-max/long.sh."
Hanno Becker3b1422e2017-07-26 13:38:02 +010018#endif
19
Hanno Becker20a4ade2019-06-03 14:27:03 +010020/* Test-only profile allowing all digests, PK algorithms, and curves. */
21const mbedtls_x509_crt_profile profile_all =
22{
23 0xFFFFFFFF, /* Any MD */
24 0xFFFFFFFF, /* Any PK alg */
25 0xFFFFFFFF, /* Any curve */
26 1024,
27};
28
Gilles Peskineef86ab22017-05-05 18:59:02 +020029/* Profile for backward compatibility. Allows SHA-1, unlike the default
30 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020031const mbedtls_x509_crt_profile compat_profile =
32{
Gilles Peskine449bd832023-01-11 14:50:10 +010033 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) |
34 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160) |
35 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
36 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
37 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
38 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubney28015e12022-04-21 08:12:59 +010039 0xFFFFFFFF, /* Any PK alg */
40 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020041 1024,
42};
43
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020044const mbedtls_x509_crt_profile profile_rsa3072 =
45{
Gilles Peskine449bd832023-01-11 14:50:10 +010046 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
47 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
48 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
49 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_RSA),
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020050 0,
51 3072,
52};
53
54const mbedtls_x509_crt_profile profile_sha512 =
55{
Gilles Peskine449bd832023-01-11 14:50:10 +010056 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubney28015e12022-04-21 08:12:59 +010057 0xFFFFFFFF, /* Any PK alg */
58 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020059 1024,
60};
61
Gilles Peskine449bd832023-01-11 14:50:10 +010062int verify_none(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000063{
Paul Bakker5a624082011-01-18 16:31:52 +000064 ((void) data);
65 ((void) crt);
66 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010067 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020068
Paul Bakker915275b2012-09-28 07:10:55 +000069 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000070}
71
Gilles Peskine449bd832023-01-11 14:50:10 +010072int verify_all(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000073{
Paul Bakker5a624082011-01-18 16:31:52 +000074 ((void) data);
75 ((void) crt);
76 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000077 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000078
Paul Bakkerb63b0af2011-01-13 17:54:59 +000079 return 0;
80}
81
Jarno Lamsa03cd1202019-03-27 15:45:04 +020082#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +010083int ca_callback_fail(void *data, mbedtls_x509_crt const *child, mbedtls_x509_crt **candidates)
Jarno Lamsa557426a2019-03-27 17:08:29 +020084{
85 ((void) data);
86 ((void) child);
87 ((void) candidates);
88
89 return -1;
90}
Przemek Stekielcd204992022-04-27 15:33:43 +020091#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010092int ca_callback(void *data, mbedtls_x509_crt const *child,
93 mbedtls_x509_crt **candidates)
Jarno Lamsa03cd1202019-03-27 15:45:04 +020094{
Hanno Beckercbb59032019-03-28 14:14:22 +000095 int ret = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +020096 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +000097 mbedtls_x509_crt *first;
98
99 /* This is a test-only implementation of the CA callback
100 * which always returns the entire list of trusted certificates.
101 * Production implementations managing a large number of CAs
102 * should use an efficient presentation and lookup for the
103 * set of trusted certificates (such as a hashtable) and only
104 * return those trusted certificates which satisfy basic
105 * parental checks, such as the matching of child `Issuer`
106 * and parent `Subject` field. */
107 ((void) child);
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
110 if (first == NULL) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000111 ret = -1;
112 goto exit;
113 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 mbedtls_x509_crt_init(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000117 ret = -1;
118 goto exit;
119 }
120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 while (ca->next != NULL) {
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200122 ca = ca->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000124 ret = -1;
125 goto exit;
126 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200127 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000128
129exit:
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 if (ret != 0) {
132 mbedtls_x509_crt_free(first);
133 mbedtls_free(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000134 first = NULL;
135 }
136
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200137 *candidates = first;
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 return ret;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200139}
Przemek Stekielcd204992022-04-27 15:33:43 +0200140#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200141#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143int verify_fatal(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200144{
145 int *levels = (int *) data;
146
147 ((void) crt);
148 ((void) certificate_depth);
149
150 /* Simulate a fatal error in the callback */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 if (*levels & (1 << certificate_depth)) {
152 *flags |= (1 << certificate_depth);
153 return -1 - certificate_depth;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200154 }
155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return 0;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200157}
158
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900159/* strsep() not available on Windows */
160char *mystrsep(char **stringp, const char *delim)
161{
162 const char *p;
163 char *ret = *stringp;
164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if (*stringp == NULL) {
166 return NULL;
167 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 for (;; (*stringp)++) {
170 if (**stringp == '\0') {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900171 *stringp = NULL;
172 goto done;
173 }
174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 for (p = delim; *p != '\0'; p++) {
176 if (**stringp == *p) {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900177 **stringp = '\0';
178 (*stringp)++;
179 goto done;
180 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900182 }
183
184done:
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return ret;
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900186}
187
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200188#if defined(MBEDTLS_X509_CRT_PARSE_C)
189typedef struct {
190 char buf[512];
191 char *p;
192} verify_print_context;
193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194void verify_print_init(verify_print_context *ctx)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200195{
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 memset(ctx, 0, sizeof(verify_print_context));
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200197 ctx->p = ctx->buf;
198}
199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200int verify_print(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200201{
202 int ret;
203 verify_print_context *ctx = (verify_print_context *) data;
204 char *p = ctx->p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 size_t n = ctx->buf + sizeof(ctx->buf) - ctx->p;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200206 ((void) flags);
207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 ret = mbedtls_snprintf(p, n, "depth %d - serial ", certificate_depth);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200209 MBEDTLS_X509_SAFE_SNPRINTF;
210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200212 MBEDTLS_X509_SAFE_SNPRINTF;
213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 ret = mbedtls_snprintf(p, n, " - subject ");
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200215 MBEDTLS_X509_SAFE_SNPRINTF;
216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200218 MBEDTLS_X509_SAFE_SNPRINTF;
219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 ret = mbedtls_snprintf(p, n, " - flags 0x%08x\n", *flags);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200221 MBEDTLS_X509_SAFE_SNPRINTF;
222
223 ctx->p = p;
224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return 0;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200226}
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228int verify_parse_san(mbedtls_x509_subject_alternative_name *san,
229 char **buf, size_t *size)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200230{
231 int ret;
232 size_t i;
233 char *p = *buf;
234 size_t n = *size;
235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 ret = mbedtls_snprintf(p, n, "type : %d", san->type);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200237 MBEDTLS_X509_SAFE_SNPRINTF;
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 switch (san->type) {
240 case (MBEDTLS_X509_SAN_OTHER_NAME):
241 ret = mbedtls_snprintf(p, n, "\notherName :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300242 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
245 &san->san.other_name.value.hardware_module_name.oid) != 0) {
246 ret = mbedtls_snprintf(p, n, " hardware module name :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300247 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 ret = mbedtls_snprintf(p, n, " hardware type : ");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300249 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 ret = mbedtls_oid_get_numeric_string(p,
252 n,
253 &san->san.other_name.value.hardware_module_name.oid);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300254 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 ret = mbedtls_snprintf(p, n, ", hardware serial number : ");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300257 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 for (i = 0; i < san->san.other_name.value.hardware_module_name.val.len; i++) {
260 ret = mbedtls_snprintf(p,
261 n,
262 "%02X",
263 san->san.other_name.value.hardware_module_name.val.p[i]);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300264 MBEDTLS_X509_SAFE_SNPRINTF;
265 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200266 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 break;/* MBEDTLS_OID_ON_HW_MODULE_NAME */
268 case (MBEDTLS_X509_SAN_DNS_NAME):
269 ret = mbedtls_snprintf(p, n, "\ndNSName : ");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200270 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 if (san->san.unstructured_name.len >= n) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200272 *p = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200274 }
275 n -= san->san.unstructured_name.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 for (i = 0; i < san->san.unstructured_name.len; i++) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200277 *p++ = san->san.unstructured_name.p[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 }
279 break;/* MBEDTLS_X509_SAN_DNS_NAME */
Przemek Stekiel608e3ef2023-02-09 14:47:50 +0100280 case (MBEDTLS_X509_SAN_RFC822_NAME):
281 ret = mbedtls_snprintf(p, n, "\nrfc822Name : ");
282 MBEDTLS_X509_SAFE_SNPRINTF;
283 if (san->san.unstructured_name.len >= n) {
284 *p = '\0';
285 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
286 }
287 n -= san->san.unstructured_name.len;
288 for (i = 0; i < san->san.unstructured_name.len; i++) {
289 *p++ = san->san.unstructured_name.p[i];
290 }
291 break;/* MBEDTLS_X509_SAN_RFC822_NAME */
Andrzej Kureke12b01d2023-01-10 06:47:38 -0500292 case (MBEDTLS_X509_SAN_DIRECTORY_NAME):
293 ret = mbedtls_snprintf(p, n, "\ndirectoryName : ");
294 MBEDTLS_X509_SAFE_SNPRINTF;
295 ret = mbedtls_x509_dn_gets(p, n, &san->san.directory_name);
296 if (ret < 0) {
297 return ret;
298 }
299
300 p += ret;
301 n -= ret;
302 break;/* MBEDTLS_X509_SAN_DIRECTORY_NAME */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200303 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 /*
305 * Should not happen.
306 */
307 return -1;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200308 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 ret = mbedtls_snprintf(p, n, "\n");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200310 MBEDTLS_X509_SAFE_SNPRINTF;
311
312 *size = n;
313 *buf = p;
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200316}
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318int parse_crt_ext_cb(void *p_ctx, mbedtls_x509_crt const *crt, mbedtls_x509_buf const *oid,
319 int critical, const unsigned char *cp, const unsigned char *end)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200320{
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 (void) crt;
322 (void) critical;
323 mbedtls_x509_buf *new_oid = (mbedtls_x509_buf *) p_ctx;
324 if (oid->tag == MBEDTLS_ASN1_OID &&
325 MBEDTLS_OID_CMP(MBEDTLS_OID_CERTIFICATE_POLICIES, oid) == 0) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200326 /* Handle unknown certificate policy */
327 int ret, parse_ret = 0;
328 size_t len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 unsigned char **p = (unsigned char **) &cp;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200330
331 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 ret = mbedtls_asn1_get_tag(p, end, &len,
333 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
334 if (ret != 0) {
335 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
336 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (*p + len != end) {
339 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
340 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
341 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200342
343 /*
344 * Cannot be an empty sequence.
345 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (len == 0) {
347 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
348 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
349 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 while (*p < end) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200352 const unsigned char *policy_end;
353
354 /*
355 * Get the policy sequence
356 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
358 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
359 0) {
360 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
361 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200362
363 policy_end = *p + len;
364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
366 MBEDTLS_ASN1_OID)) != 0) {
367 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
368 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200369
Nicola Di Lietob77fad82020-06-17 17:57:36 +0200370 /*
371 * Recognize exclusively the policy with OID 1
372 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if (len != 1 || *p[0] != 1) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200374 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200376
377 *p += len;
378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 /*
380 * If there is an optional qualifier, then *p < policy_end
381 * Check the Qualifier len to verify it doesn't exceed policy_end.
382 */
383 if (*p < policy_end) {
384 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
385 MBEDTLS_ASN1_CONSTRUCTED |
386 MBEDTLS_ASN1_SEQUENCE)) != 0) {
387 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
388 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200389 /*
390 * Skip the optional policy qualifiers.
391 */
392 *p += len;
393 }
394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if (*p != policy_end) {
396 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
397 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
398 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200399 }
400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (*p != end) {
402 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
403 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
404 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 return parse_ret;
407 } else if (new_oid != NULL && new_oid->tag == oid->tag && new_oid->len == oid->len &&
408 memcmp(new_oid->p, oid->p, oid->len) == 0) {
409 return 0;
410 } else {
411 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
412 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200413 }
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200414}
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200415#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200416/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000417
Paul Bakker33b43f12013-08-20 11:48:36 +0200418/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200420 * END_DEPENDENCIES
421 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000422
Thomas Daubney5c9c2ce2022-06-06 16:36:43 +0100423/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100424void x509_accessor_ext_types(int ext_type, int has_ext_type)
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100425{
426 mbedtls_x509_crt crt;
427 int expected_result = ext_type & has_ext_type;
428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 mbedtls_x509_crt_init(&crt);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100430
431 crt.ext_types = ext_type;
432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 TEST_ASSERT(mbedtls_x509_crt_has_ext_type(&crt, has_ext_type) == expected_result);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 mbedtls_x509_crt_free(&crt);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100436}
437/* END_CASE */
438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100440void x509_parse_san(char *crt_file, char *result_str)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200441{
Ron Eldor890819a2019-05-13 19:03:04 +0300442 int ret;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200443 mbedtls_x509_crt crt;
Ron Eldor890819a2019-05-13 19:03:04 +0300444 mbedtls_x509_subject_alternative_name san;
445 mbedtls_x509_sequence *cur = NULL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200446 char buf[2000];
447 char *p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 size_t n = sizeof(buf);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 mbedtls_x509_crt_init(&crt);
451 memset(buf, 0, 2000);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Ron Eldor890819a2019-05-13 19:03:04 +0300454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 if (crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Ron Eldor890819a2019-05-13 19:03:04 +0300456 cur = &crt.subject_alt_names;
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 while (cur != NULL) {
458 ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
459 TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
Ron Eldor890819a2019-05-13 19:03:04 +0300460 /*
461 * If san type not supported, ignore.
462 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if (ret == 0) {
Andrzej Kurekd40c2b62023-02-13 07:01:59 -0500464 ret = verify_parse_san(&san, &p, &n);
465 mbedtls_x509_free_subject_alt_name(&san);
466 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 }
Ron Eldor890819a2019-05-13 19:03:04 +0300468 cur = cur->next;
469 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200470 }
471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 TEST_ASSERT(strcmp(buf, result_str) == 0);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200473
474exit:
475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 mbedtls_x509_crt_free(&crt);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200477}
478/* END_CASE */
479
Hanno Becker612a2f12020-10-09 09:19:39 +0100480/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100481void x509_cert_info(char *crt_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000482{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000484 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000485 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 mbedtls_x509_crt_init(&crt);
488 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
491 res = mbedtls_x509_crt_info(buf, 2000, "", &crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 TEST_ASSERT(res != -1);
494 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200497
498exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000500}
Paul Bakker33b43f12013-08-20 11:48:36 +0200501/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000502
Hanno Becker612a2f12020-10-09 09:19:39 +0100503/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100504void mbedtls_x509_crl_info(char *crl_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000505{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000507 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000508 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 mbedtls_x509_crl_init(&crl);
511 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == 0);
514 res = mbedtls_x509_crl_info(buf, 2000, "", &crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 TEST_ASSERT(res != -1);
517 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200520
521exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 mbedtls_x509_crl_free(&crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000523}
Paul Bakker33b43f12013-08-20 11:48:36 +0200524/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000525
Andres AGa39db392016-12-08 17:10:38 +0000526/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100527void mbedtls_x509_crl_parse(char *crl_file, int result)
Andres AGa39db392016-12-08 17:10:38 +0000528{
529 mbedtls_x509_crl crl;
530 char buf[2000];
531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 mbedtls_x509_crl_init(&crl);
533 memset(buf, 0, 2000);
Andres AGa39db392016-12-08 17:10:38 +0000534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == result);
Andres AGa39db392016-12-08 17:10:38 +0000536
537exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 mbedtls_x509_crl_free(&crl);
Andres AGa39db392016-12-08 17:10:38 +0000539}
540/* END_CASE */
541
Hanno Becker612a2f12020-10-09 09:19:39 +0100542/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100543void mbedtls_x509_csr_info(char *csr_file, char *result_str)
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100544{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100546 char buf[2000];
547 int res;
548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 mbedtls_x509_csr_init(&csr);
550 memset(buf, 0, 2000);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 TEST_ASSERT(mbedtls_x509_csr_parse_file(&csr, csr_file) == 0);
553 res = mbedtls_x509_csr_info(buf, 2000, "", &csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 TEST_ASSERT(res != -1);
556 TEST_ASSERT(res != -2);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200559
560exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 mbedtls_x509_csr_free(&csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100562}
563/* END_CASE */
564
Hanno Becker612a2f12020-10-09 09:19:39 +0100565/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100566void x509_verify_info(int flags, char *prefix, char *result_str)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100567{
568 char buf[2000];
569 int res;
570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 memset(buf, 0, sizeof(buf));
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 res = mbedtls_x509_crt_verify_info(buf, sizeof(buf), prefix, flags);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 TEST_ASSERT(res >= 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 TEST_ASSERT(strcmp(buf, result_str) == 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100578}
579/* END_CASE */
580
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200581/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100582void x509_verify_restart(char *crt_file, char *ca_file,
583 int result, int flags_result,
584 int max_ops, int min_restart, int max_restart)
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200585{
586 int ret, cnt_restart;
587 mbedtls_x509_crt_restart_ctx rs_ctx;
588 mbedtls_x509_crt crt;
589 mbedtls_x509_crt ca;
590 uint32_t flags = 0;
591
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200592 /*
593 * See comments on ecp_test_vect_restart() for op count precision.
594 *
595 * For reference, with mbed TLS 2.6 and default settings:
596 * - ecdsa_verify() for P-256: ~ 6700
597 * - ecdsa_verify() for P-384: ~ 18800
598 * - x509_verify() for server5 -> test-ca2: ~ 18800
599 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
600 */
601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 mbedtls_x509_crt_restart_init(&rs_ctx);
603 mbedtls_x509_crt_init(&crt);
604 mbedtls_x509_crt_init(&ca);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200605
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100606 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnardcc6e0a62022-12-05 12:55:05 +0100607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
609 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 mbedtls_ecp_set_max_ops(max_ops);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200612
613 cnt_restart = 0;
614 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
616 &mbedtls_x509_crt_profile_default, NULL, &flags,
617 NULL, NULL, &rs_ctx);
618 } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 TEST_ASSERT(ret == result);
621 TEST_ASSERT(flags == (uint32_t) flags_result);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200622
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 TEST_ASSERT(cnt_restart >= min_restart);
624 TEST_ASSERT(cnt_restart <= max_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200625
626 /* Do we leak memory when aborting? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
628 &mbedtls_x509_crt_profile_default, NULL, &flags,
629 NULL, NULL, &rs_ctx);
630 TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200631
632exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 mbedtls_x509_crt_restart_free(&rs_ctx);
634 mbedtls_x509_crt_free(&crt);
635 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100636 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200637}
638/* END_CASE */
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100641void x509_verify(char *crt_file, char *ca_file, char *crl_file,
642 char *cn_name_str, int result, int flags_result,
643 char *profile_str,
644 char *verify_callback)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000645{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_x509_crt crt;
647 mbedtls_x509_crt ca;
648 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200649 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000650 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200651 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 char *cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200653 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 mbedtls_x509_crt_init(&crt);
656 mbedtls_x509_crt_init(&ca);
657 mbedtls_x509_crl_init(&crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000658
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100659 MD_OR_USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +0100660
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 if (strcmp(cn_name_str, "NULL") != 0) {
Paul Bakker33b43f12013-08-20 11:48:36 +0200662 cn_name = cn_name_str;
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200664
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (strcmp(profile_str, "") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200666 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 } else if (strcmp(profile_str, "next") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200668 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 } else if (strcmp(profile_str, "suite_b") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200670 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 } else if (strcmp(profile_str, "compat") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200672 profile = &compat_profile;
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 } else if (strcmp(profile_str, "all") == 0) {
Hanno Becker20a4ade2019-06-03 14:27:03 +0100674 profile = &profile_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 } else {
676 TEST_ASSERT("Unknown algorithm profile" == 0);
677 }
Gilles Peskineef86ab22017-05-05 18:59:02 +0200678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (strcmp(verify_callback, "NULL") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200680 f_vrfy = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 } else if (strcmp(verify_callback, "verify_none") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200682 f_vrfy = verify_none;
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 } else if (strcmp(verify_callback, "verify_all") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200684 f_vrfy = verify_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 } else {
686 TEST_ASSERT("No known verify callback selected" == 0);
687 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
690 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
691 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == 0);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 res = mbedtls_x509_crt_verify_with_profile(&crt,
694 &ca,
695 &crl,
696 profile,
697 cn_name,
698 &flags,
699 f_vrfy,
700 NULL);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 TEST_EQUAL(res, result);
703 TEST_EQUAL(flags, (uint32_t) flags_result);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200704
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200705#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker0350d562019-03-28 14:23:36 +0000706 /* CRLs aren't supported with CA callbacks, so skip the CA callback
Jarno Lamsadfd22c42019-04-01 15:18:53 +0300707 * version of the test if CRLs are in use. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (crl_file == NULL || strcmp(crl_file, "") == 0) {
Hanno Becker0350d562019-03-28 14:23:36 +0000709 flags = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 res = mbedtls_x509_crt_verify_with_ca_cb(&crt,
712 ca_callback,
713 &ca,
714 profile,
715 cn_name,
716 &flags,
717 f_vrfy,
718 NULL);
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 TEST_ASSERT(res == (result));
721 TEST_ASSERT(flags == (uint32_t) (flags_result));
Hanno Becker0350d562019-03-28 14:23:36 +0000722 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200723#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200724exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 mbedtls_x509_crt_free(&crt);
726 mbedtls_x509_crt_free(&ca);
727 mbedtls_x509_crl_free(&crl);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100728 MD_OR_USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000729}
Paul Bakker33b43f12013-08-20 11:48:36 +0200730/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000731
Jarno Lamsa557426a2019-03-27 17:08:29 +0200732/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Gilles Peskine449bd832023-01-11 14:50:10 +0100733void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
734 int exp_ret)
Jarno Lamsa557426a2019-03-27 17:08:29 +0200735{
736 int ret;
737 mbedtls_x509_crt crt;
738 mbedtls_x509_crt ca;
739 uint32_t flags = 0;
Hanno Becker3f932bb2019-03-28 17:06:47 +0000740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 mbedtls_x509_crt_init(&crt);
742 mbedtls_x509_crt_init(&ca);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
745 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 if (strcmp(name, "NULL") == 0) {
Jarno Lamsa557426a2019-03-27 17:08:29 +0200748 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca,
752 &compat_profile, name, &flags,
753 NULL, NULL);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 TEST_ASSERT(ret == exp_ret);
756 TEST_ASSERT(flags == (uint32_t) (-1));
Jarno Lamsa557426a2019-03-27 17:08:29 +0200757exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 mbedtls_x509_crt_free(&crt);
759 mbedtls_x509_crt_free(&ca);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200760}
761/* END_CASE */
762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100764void x509_verify_callback(char *crt_file, char *ca_file, char *name,
765 int exp_ret, char *exp_vrfy_out)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200766{
767 int ret;
768 mbedtls_x509_crt crt;
769 mbedtls_x509_crt ca;
770 uint32_t flags = 0;
771 verify_print_context vrfy_ctx;
772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 mbedtls_x509_crt_init(&crt);
774 mbedtls_x509_crt_init(&ca);
775 verify_print_init(&vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200776
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100777 MD_OR_USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +0100778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
780 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (strcmp(name, "NULL") == 0) {
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200783 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 }
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL,
787 &compat_profile,
788 name, &flags,
789 verify_print, &vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 TEST_ASSERT(ret == exp_ret);
792 TEST_ASSERT(strcmp(vrfy_ctx.buf, exp_vrfy_out) == 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200793
794exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 mbedtls_x509_crt_free(&crt);
796 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100797 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200798}
799/* END_CASE */
800
Hanno Becker612a2f12020-10-09 09:19:39 +0100801/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100802void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
803 char *new_subject_ou,
804 char *result_str,
805 int ret)
Werner Lewis31ecb962022-06-17 15:51:55 +0100806{
807 mbedtls_x509_crt crt;
808 char buf[2000];
809 int res = 0;
810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 mbedtls_x509_crt_init(&crt);
812 memset(buf, 0, 2000);
Werner Lewis31ecb962022-06-17 15:51:55 +0100813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100815 crt.subject.next->val.p = (unsigned char *) new_subject_ou;
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 crt.subject.next->val.len = strlen(new_subject_ou);
Werner Lewis31ecb962022-06-17 15:51:55 +0100817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
Werner Lewis31ecb962022-06-17 15:51:55 +0100819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (ret != 0) {
821 TEST_ASSERT(res == ret);
822 } else {
823 TEST_ASSERT(res != -1);
824 TEST_ASSERT(res != -2);
825 TEST_ASSERT(strcmp(buf, result_str) == 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100826 }
827exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 mbedtls_x509_crt_free(&crt);
Werner Lewis31ecb962022-06-17 15:51:55 +0100829}
830/* END_CASE */
831
832/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000834{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000836 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200837 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 mbedtls_x509_crt_init(&crt);
840 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
843 if (strcmp(entity, "subject") == 0) {
844 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
845 } else if (strcmp(entity, "issuer") == 0) {
846 res = mbedtls_x509_dn_gets(buf, 2000, &crt.issuer);
847 } else {
848 TEST_ASSERT("Unknown entity" == 0);
849 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 TEST_ASSERT(res != -1);
852 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200855
856exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000858}
Paul Bakker33b43f12013-08-20 11:48:36 +0200859/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000860
David Horstmanndb73d3b2022-10-04 16:49:16 +0100861/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
David Horstmanndb73d3b2022-10-04 16:49:16 +0100863{
864 unsigned char *name;
865 unsigned char *p;
866 size_t name_len;
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100867 mbedtls_x509_name head;
David Horstmann3cd67582022-10-17 17:59:10 +0100868 int ret;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 memset(&head, 0, sizeof(head));
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100873 p = name;
874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 ret = mbedtls_x509_get_name(&p, (name + name_len), &head);
876 if (ret == 0) {
877 mbedtls_asn1_free_named_data_list_shallow(head.next);
878 }
David Horstmanndb73d3b2022-10-04 16:49:16 +0100879
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 TEST_EQUAL(ret, exp_ret);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 mbedtls_free(name);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100883}
884/* END_CASE */
885
Werner Lewisb3acb052022-06-17 15:59:58 +0100886/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100887void mbedtls_x509_dn_get_next(char *name_str,
888 int next_merged,
889 char *expected_oids,
890 int exp_count,
891 char *exp_dn_gets)
Werner Lewisb3acb052022-06-17 15:59:58 +0100892{
893 int ret = 0, i;
Werner Lewisac80a662022-06-23 11:58:02 +0100894 size_t len = 0, out_size;
Werner Lewisb3acb052022-06-17 15:59:58 +0100895 mbedtls_asn1_named_data *names = NULL;
Glenn Straussa4b40412022-06-26 19:32:09 -0400896 mbedtls_x509_name parsed, *parsed_cur;
Werner Lewisac80a662022-06-23 11:58:02 +0100897 // Size of buf is maximum required for test cases
898 unsigned char buf[80], *out = NULL, *c;
Werner Lewisb3acb052022-06-17 15:59:58 +0100899 const char *short_name;
900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 memset(&parsed, 0, sizeof(parsed));
902 memset(buf, 0, sizeof(buf));
903 c = buf + sizeof(buf);
Werner Lewisac80a662022-06-23 11:58:02 +0100904 // Additional size required for trailing space
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 out_size = strlen(expected_oids) + 2;
906 ASSERT_ALLOC(out, out_size);
Werner Lewisb3acb052022-06-17 15:59:58 +0100907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 TEST_EQUAL(mbedtls_x509_string_to_names(&names, name_str), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100909
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 ret = mbedtls_x509_write_names(&c, buf, names);
911 TEST_LE_S(0, ret);
Werner Lewisb3acb052022-06-17 15:59:58 +0100912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
914 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
915 TEST_EQUAL(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100916
917 // Iterate over names and set next_merged nodes
918 parsed_cur = &parsed;
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 for (; next_merged != 0 && parsed_cur != NULL; next_merged = next_merged >> 1) {
Werner Lewis12657cd2022-06-20 11:47:57 +0100920 parsed_cur->next_merged = next_merged & 0x01;
Werner Lewisb3acb052022-06-17 15:59:58 +0100921 parsed_cur = parsed_cur->next;
922 }
923
924 // Iterate over RDN nodes and print OID of first element to buffer
925 parsed_cur = &parsed;
926 len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 for (i = 0; parsed_cur != NULL; i++) {
928 TEST_EQUAL(mbedtls_oid_get_attr_short_name(&parsed_cur->oid,
929 &short_name), 0);
930 len += mbedtls_snprintf((char *) out + len, out_size - len, "%s ", short_name);
931 parsed_cur = mbedtls_x509_dn_get_next(parsed_cur);
Werner Lewisb3acb052022-06-17 15:59:58 +0100932 }
933 out[len-1] = 0;
934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 TEST_EQUAL(exp_count, i);
936 TEST_EQUAL(strcmp((char *) out, expected_oids), 0);
937 mbedtls_free(out);
Werner Lewisac80a662022-06-23 11:58:02 +0100938 out = NULL;
Werner Lewisb3acb052022-06-17 15:59:58 +0100939
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 out_size = strlen(exp_dn_gets) + 1;
941 ASSERT_ALLOC(out, out_size);
Werner Lewisac80a662022-06-23 11:58:02 +0100942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 TEST_LE_S(0, mbedtls_x509_dn_gets((char *) out, out_size, &parsed));
944 TEST_EQUAL(strcmp((char *) out, exp_dn_gets), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100945exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 mbedtls_free(out);
947 mbedtls_asn1_free_named_data_list(&names);
948 mbedtls_asn1_free_named_data_list_shallow(parsed.next);
Werner Lewisb3acb052022-06-17 15:59:58 +0100949}
Werner Lewisb3acb052022-06-17 15:59:58 +0100950/* END_CASE */
951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100953void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000954{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 mbedtls_x509_crt_init(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 if (strcmp(entity, "valid_from") == 0) {
962 TEST_ASSERT(mbedtls_x509_time_is_past(&crt.valid_from) == result);
963 } else if (strcmp(entity, "valid_to") == 0) {
964 TEST_ASSERT(mbedtls_x509_time_is_past(&crt.valid_to) == result);
965 } else {
966 TEST_ASSERT("Unknown entity" == 0);
967 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000968
Paul Bakkerbd51b262014-07-10 15:26:12 +0200969exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000971}
Paul Bakker33b43f12013-08-20 11:48:36 +0200972/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100975void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100976{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100982
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 if (strcmp(entity, "valid_from") == 0) {
984 TEST_ASSERT(mbedtls_x509_time_is_future(&crt.valid_from) == result);
985 } else if (strcmp(entity, "valid_to") == 0) {
986 TEST_ASSERT(mbedtls_x509_time_is_future(&crt.valid_to) == result);
987 } else {
988 TEST_ASSERT("Unknown entity" == 0);
989 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100990
Paul Bakkerbd51b262014-07-10 15:26:12 +0200991exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100993}
994/* END_CASE */
995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100997void x509parse_crt_file(char *crt_file, int result)
Paul Bakker5a5fa922014-09-26 14:53:04 +0200998{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +02001000
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 mbedtls_x509_crt_init(&crt);
Paul Bakker5a5fa922014-09-26 14:53:04 +02001002
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == result);
Paul Bakker5a5fa922014-09-26 14:53:04 +02001004
1005exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 mbedtls_x509_crt_free(&crt);
Paul Bakker5a5fa922014-09-26 14:53:04 +02001007}
1008/* END_CASE */
1009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001011void x509parse_crt(data_t *buf, char *result_str, int result)
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001012{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 mbedtls_x509_crt crt;
Hanno Becker612a2f12020-10-09 09:19:39 +01001014#if !defined(MBEDTLS_X509_REMOVE_INFO)
Hanno Beckerfff2d572020-10-09 09:45:19 +01001015 unsigned char output[2000] = { 0 };
Azim Khanf1aaec92017-05-30 14:23:15 +01001016 int res;
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001017#else
1018 ((void) result_str);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001019#endif
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 mbedtls_x509_crt_init(&crt);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 TEST_ASSERT(mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len) == (result));
Hanno Becker612a2f12020-10-09 09:19:39 +01001024#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 if ((result) == 0) {
1026 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1027 TEST_ASSERT(res != -1);
1028 TEST_ASSERT(res != -2);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001031 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001033#endif
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001034
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 mbedtls_x509_crt_free(&crt);
1036 mbedtls_x509_crt_init(&crt);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001037
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 TEST_ASSERT(mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len) == (result));
Hanno Becker612a2f12020-10-09 09:19:39 +01001039#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 if ((result) == 0) {
1041 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001042
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakker33b43f12013-08-20 11:48:36 +02001044
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 TEST_ASSERT(res != -1);
1046 TEST_ASSERT(res != -2);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001049 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001051#endif /* !MBEDTLS_X509_REMOVE_INFO */
Paul Bakkerb08e6842012-02-11 18:43:20 +00001052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 mbedtls_x509_crt_free(&crt);
1054 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL,
1057 NULL) == (result));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001058#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if ((result) == 0) {
1060 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 TEST_ASSERT(res != -1);
1063 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001064
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001066 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001068#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 mbedtls_x509_crt_free(&crt);
1071 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL,
1074 NULL) == (result));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001075#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 if ((result) == 0) {
1077 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 TEST_ASSERT(res != -1);
1080 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001083 }
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001084#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001085
1086exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 mbedtls_x509_crt_free(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001088}
1089/* END_CASE */
1090
1091/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001092void x509parse_crt_cb(data_t *buf, char *result_str, int result)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001093{
1094 mbedtls_x509_crt crt;
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001095 mbedtls_x509_buf oid;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001096
1097#if !defined(MBEDTLS_X509_REMOVE_INFO)
1098 unsigned char output[2000] = { 0 };
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001099 int res;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001100#else
1101 ((void) result_str);
1102#endif
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001103
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001104 oid.tag = MBEDTLS_ASN1_OID;
1105 oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001107
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
1111 &oid) == (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001112#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 if ((result) == 0) {
1114 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 TEST_ASSERT(res != -1);
1117 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001120 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 memset(output, 0, 2000);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001122#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001123
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 mbedtls_x509_crt_free(&crt);
1125 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1128 &oid) == (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001129#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 if ((result) == 0) {
1131 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 TEST_ASSERT(res != -1);
1134 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001137 }
Hanno Beckerfff2d572020-10-09 09:45:19 +01001138#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001139
Paul Bakkerbd51b262014-07-10 15:26:12 +02001140exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 mbedtls_x509_crt_free(&crt);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001142}
Paul Bakker33b43f12013-08-20 11:48:36 +02001143/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001144
Hanno Becker612a2f12020-10-09 09:19:39 +01001145/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001146void x509parse_crl(data_t *buf, char *result_str, int result)
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001147{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001149 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +01001150 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 mbedtls_x509_crl_init(&crl);
1153 memset(output, 0, 2000);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001154
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 TEST_ASSERT(mbedtls_x509_crl_parse(&crl, buf->x, buf->len) == (result));
1157 if ((result) == 0) {
1158 res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
Paul Bakker33b43f12013-08-20 11:48:36 +02001159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 TEST_ASSERT(res != -1);
1161 TEST_ASSERT(res != -2);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001164 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001165
Paul Bakkerbd51b262014-07-10 15:26:12 +02001166exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 mbedtls_x509_crl_free(&crl);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001168}
Paul Bakker33b43f12013-08-20 11:48:36 +02001169/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001170
Hanno Becker612a2f12020-10-09 09:19:39 +01001171/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001172void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001173{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001175 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001176 int my_ret;
1177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 mbedtls_x509_csr_init(&csr);
1179 memset(my_out, 0, sizeof(my_out));
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
1182 TEST_ASSERT(my_ret == ref_ret);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 if (ref_ret == 0) {
1185 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1186 TEST_ASSERT(my_out_len == strlen(ref_out));
1187 TEST_ASSERT(strcmp(my_out, ref_out) == 0);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001188 }
1189
Paul Bakkerbd51b262014-07-10 15:26:12 +02001190exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 mbedtls_x509_csr_free(&csr);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001192}
1193/* END_CASE */
1194
Przemek Stekield7992df2023-01-25 16:19:50 +01001195/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1196void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
1197{
1198 mbedtls_x509_csr csr;
1199 char my_out[1000];
1200 int my_ret;
1201
1202 mbedtls_x509_csr_init(&csr);
1203 memset(my_out, 0, sizeof(my_out));
1204
1205 my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file);
1206 TEST_ASSERT(my_ret == ref_ret);
1207
1208 if (ref_ret == 0) {
1209 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1210 TEST_ASSERT(my_out_len == strlen(ref_out));
1211 TEST_ASSERT(strcmp(my_out, ref_out) == 0);
1212 }
1213
1214exit:
1215 mbedtls_x509_csr_free(&csr);
1216}
1217/* END_CASE */
1218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001220void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001221{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001223 int i;
1224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 mbedtls_x509_crt_init(&chain);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 TEST_ASSERT(mbedtls_x509_crt_parse_path(&chain, crt_path) == ret);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001228
1229 /* Check how many certs we got */
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1231 if (cur->raw.p != NULL) {
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001232 i++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 }
1234 }
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 TEST_ASSERT(i == nb_crt);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001237
Paul Bakkerbd51b262014-07-10 15:26:12 +02001238exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001240}
1241/* END_CASE */
1242
Janos Follath822b2c32015-10-11 10:25:22 +02001243/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001244void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1245 int ret_chk, int flags_chk)
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001246{
1247 char file_buf[128];
1248 int ret;
1249 uint32_t flags;
1250 mbedtls_x509_crt trusted, chain;
1251
1252 /*
1253 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1254 * with NN.crt signed by NN-1.crt
1255 */
1256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 mbedtls_x509_crt_init(&trusted);
1258 mbedtls_x509_crt_init(&chain);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001259
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001260 MD_OR_USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +01001261
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001262 /* Load trusted root */
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, ca_file) == 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001264
1265 /* Load a chain with nb_int intermediates (from 01 to nb_int),
1266 * plus one "end-entity" cert (nb_int + 1) */
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001267 ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 nb_int + 1);
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001269 TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, file_buf) == 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001271
1272 /* Try to verify that chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1274 NULL, NULL);
1275 TEST_ASSERT(ret == ret_chk);
1276 TEST_ASSERT(flags == (uint32_t) flags_chk);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001277
1278exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 mbedtls_x509_crt_free(&chain);
1280 mbedtls_x509_crt_free(&trusted);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001281 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001282}
1283/* END_CASE */
1284
1285/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001286void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1287 int flags_result, int result,
1288 char *profile_name, int vrfy_fatal_lvls)
Janos Follath822b2c32015-10-11 10:25:22 +02001289{
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 char *act;
Janos Follath822b2c32015-10-11 10:25:22 +02001291 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001292 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001293 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001294 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +02001295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 mbedtls_x509_crt_init(&chain);
1297 mbedtls_x509_crt_init(&trusted);
Janos Follath822b2c32015-10-11 10:25:22 +02001298
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001299 MD_OR_USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +01001300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 while ((act = mystrsep(&chain_paths, " ")) != NULL) {
1302 TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, act) == 0);
1303 }
1304 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, trusted_ca) == 0);
Janos Follath822b2c32015-10-11 10:25:22 +02001305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 if (strcmp(profile_name, "") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001307 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 } else if (strcmp(profile_name, "next") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001309 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 } else if (strcmp(profile_name, "suiteb") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001311 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 } else if (strcmp(profile_name, "rsa3072") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001313 profile = &profile_rsa3072;
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 } else if (strcmp(profile_name, "sha512") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001315 profile = &profile_sha512;
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 }
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1319 NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
Janos Follathef4f2582015-10-11 16:17:27 +02001320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 TEST_ASSERT(res == (result));
1322 TEST_ASSERT(flags == (uint32_t) (flags_result));
Janos Follath822b2c32015-10-11 10:25:22 +02001323
1324exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 mbedtls_x509_crt_free(&trusted);
1326 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001327 MD_OR_USE_PSA_DONE();
Janos Follath822b2c32015-10-11 10:25:22 +02001328}
1329/* END_CASE */
1330
Hanno Becker612a2f12020-10-09 09:19:39 +01001331/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001332void x509_oid_desc(data_t *buf, char *ref_desc)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001333{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001335 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001336 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001337
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001340 oid.p = buf->x;
1341 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001342
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 ret = mbedtls_oid_get_extended_key_usage(&oid, &desc);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 if (strcmp(ref_desc, "notfound") == 0) {
1346 TEST_ASSERT(ret != 0);
1347 TEST_ASSERT(desc == NULL);
1348 } else {
1349 TEST_ASSERT(ret == 0);
1350 TEST_ASSERT(desc != NULL);
1351 TEST_ASSERT(strcmp(desc, ref_desc) == 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001352 }
1353}
1354/* END_CASE */
1355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001357void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001358{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001360 char num_buf[100];
1361
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001362 memset(num_buf, 0x2a, sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001365 oid.p = oid_buf->x;
1366 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001367
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001368 TEST_ASSERT((size_t) blen <= sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 TEST_ASSERT(mbedtls_oid_get_numeric_string(num_buf, blen, &oid) == ret);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 if (ret >= 0) {
1373 TEST_ASSERT(num_buf[ret] == 0);
1374 TEST_ASSERT(strcmp(num_buf, numstr) == 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001375 }
1376}
1377/* END_CASE */
1378
TRodziewicz442fdc22021-06-07 13:52:23 +02001379/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001380void x509_check_key_usage(char *crt_file, int usage, int ret)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001381{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001385
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 TEST_ASSERT(mbedtls_x509_crt_check_key_usage(&crt, usage) == ret);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001389
Paul Bakkerbd51b262014-07-10 15:26:12 +02001390exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001392}
1393/* END_CASE */
1394
TRodziewicz442fdc22021-06-07 13:52:23 +02001395/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001396void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1397 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001398{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001400
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001402
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001403
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 TEST_ASSERT(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x,
1407 oid->len) == ret);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001408
Paul Bakkerbd51b262014-07-10 15:26:12 +02001409exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001411}
1412/* END_CASE */
1413
Andres AG4b76aec2016-09-23 13:16:02 +01001414/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001415void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1416 int day, int hour, int min, int sec)
Andres AG4b76aec2016-09-23 13:16:02 +01001417{
1418 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001419 unsigned char buf[21];
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 unsigned char *start = buf;
1421 unsigned char *end = buf;
Andres AG4b76aec2016-09-23 13:16:02 +01001422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 memset(&time, 0x00, sizeof(time));
1424 *end = (unsigned char) tag; end++;
1425 *end = strlen(time_str);
1426 TEST_ASSERT(*end < 20);
Andres AG4b76aec2016-09-23 13:16:02 +01001427 end++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 memcpy(end, time_str, (size_t) *(end - 1));
Andres AG4b76aec2016-09-23 13:16:02 +01001429 end += *(end - 1);
1430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 TEST_ASSERT(mbedtls_x509_get_time(&start, end, &time) == ret);
1432 if (ret == 0) {
1433 TEST_ASSERT(year == time.year);
1434 TEST_ASSERT(mon == time.mon);
1435 TEST_ASSERT(day == time.day);
1436 TEST_ASSERT(hour == time.hour);
1437 TEST_ASSERT(min == time.min);
1438 TEST_ASSERT(sec == time.sec);
Andres AG4b76aec2016-09-23 13:16:02 +01001439 }
1440}
1441/* END_CASE */
1442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001444void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1445 int ref_msg_md, int ref_mgf_md,
1446 int ref_salt_len, int ref_ret)
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001447{
1448 int my_ret;
Ronald Cronac6ae352020-06-26 14:33:03 +02001449 mbedtls_x509_buf buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001451 int my_salt_len;
1452
Ronald Cronac6ae352020-06-26 14:33:03 +02001453 buf.p = params->x;
1454 buf.len = params->len;
1455 buf.tag = params_tag;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1458 &my_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 TEST_ASSERT(my_ret == ref_ret);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 if (ref_ret == 0) {
1463 TEST_ASSERT(my_msg_md == (mbedtls_md_type_t) ref_msg_md);
1464 TEST_ASSERT(my_mgf_md == (mbedtls_md_type_t) ref_mgf_md);
1465 TEST_ASSERT(my_salt_len == ref_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001466 }
1467
Paul Bakkerbd51b262014-07-10 15:26:12 +02001468exit:
Azim Khand30ca132017-06-09 04:32:58 +01001469 ;;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001470}
1471/* END_CASE */