blob: 66477e0d1601a0f7af6f7f90309dabae9ff72877 [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"
Valerio Setti25b282e2024-01-17 10:55:32 +01007#include "x509_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00008#include "mbedtls/pem.h"
9#include "mbedtls/oid.h"
10#include "mbedtls/base64.h"
Chris Jones9f7a6932021-04-14 12:12:09 +010011#include "mbedtls/error.h"
Valerio Setti178b5bd2023-02-13 10:04:28 +010012#include "mbedtls/pk.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +010013#include "string.h"
Paul Bakkerb63b0af2011-01-13 17:54:59 +000014
Simon Butcher9e24b512017-07-28 12:15:13 +010015#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
Hanno Becker3b1422e2017-07-26 13:38:02 +010016#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
Gilles Peskine449bd832023-01-11 14:50:10 +010017 than the current threshold 19. To test larger values, please \
18 adapt the script tests/data_files/dir-max/long.sh."
Hanno Becker3b1422e2017-07-26 13:38:02 +010019#endif
20
Hanno Becker20a4ade2019-06-03 14:27:03 +010021/* Test-only profile allowing all digests, PK algorithms, and curves. */
22const mbedtls_x509_crt_profile profile_all =
23{
24 0xFFFFFFFF, /* Any MD */
25 0xFFFFFFFF, /* Any PK alg */
26 0xFFFFFFFF, /* Any curve */
27 1024,
28};
29
Gilles Peskineef86ab22017-05-05 18:59:02 +020030/* Profile for backward compatibility. Allows SHA-1, unlike the default
31 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020032const mbedtls_x509_crt_profile compat_profile =
33{
Gilles Peskine449bd832023-01-11 14:50:10 +010034 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) |
35 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160) |
36 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
37 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
38 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
39 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubney28015e12022-04-21 08:12:59 +010040 0xFFFFFFFF, /* Any PK alg */
41 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020042 1024,
43};
44
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020045const mbedtls_x509_crt_profile profile_rsa3072 =
46{
Gilles Peskine449bd832023-01-11 14:50:10 +010047 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
48 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
49 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
50 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_RSA),
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020051 0,
52 3072,
53};
54
55const mbedtls_x509_crt_profile profile_sha512 =
56{
Gilles Peskine449bd832023-01-11 14:50:10 +010057 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubney28015e12022-04-21 08:12:59 +010058 0xFFFFFFFF, /* Any PK alg */
59 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020060 1024,
61};
62
Gilles Peskine449bd832023-01-11 14:50:10 +010063int verify_none(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000064{
Paul Bakker5a624082011-01-18 16:31:52 +000065 ((void) data);
66 ((void) crt);
67 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010068 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020069
Paul Bakker915275b2012-09-28 07:10:55 +000070 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000071}
72
Gilles Peskine449bd832023-01-11 14:50:10 +010073int verify_all(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000074{
Paul Bakker5a624082011-01-18 16:31:52 +000075 ((void) data);
76 ((void) crt);
77 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000078 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000079
Paul Bakkerb63b0af2011-01-13 17:54:59 +000080 return 0;
81}
82
Jarno Lamsa03cd1202019-03-27 15:45:04 +020083#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +010084int ca_callback_fail(void *data, mbedtls_x509_crt const *child, mbedtls_x509_crt **candidates)
Jarno Lamsa557426a2019-03-27 17:08:29 +020085{
86 ((void) data);
87 ((void) child);
88 ((void) candidates);
89
90 return -1;
91}
Przemek Stekielcd204992022-04-27 15:33:43 +020092#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010093int ca_callback(void *data, mbedtls_x509_crt const *child,
94 mbedtls_x509_crt **candidates)
Jarno Lamsa03cd1202019-03-27 15:45:04 +020095{
Hanno Beckercbb59032019-03-28 14:14:22 +000096 int ret = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +020097 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +000098 mbedtls_x509_crt *first;
99
100 /* This is a test-only implementation of the CA callback
101 * which always returns the entire list of trusted certificates.
102 * Production implementations managing a large number of CAs
103 * should use an efficient presentation and lookup for the
104 * set of trusted certificates (such as a hashtable) and only
105 * return those trusted certificates which satisfy basic
106 * parental checks, such as the matching of child `Issuer`
107 * and parent `Subject` field. */
108 ((void) child);
109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
111 if (first == NULL) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000112 ret = -1;
113 goto exit;
114 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 mbedtls_x509_crt_init(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000118 ret = -1;
119 goto exit;
120 }
121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 while (ca->next != NULL) {
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200123 ca = ca->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000125 ret = -1;
126 goto exit;
127 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200128 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000129
130exit:
131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 if (ret != 0) {
133 mbedtls_x509_crt_free(first);
134 mbedtls_free(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000135 first = NULL;
136 }
137
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200138 *candidates = first;
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 return ret;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200140}
Przemek Stekielcd204992022-04-27 15:33:43 +0200141#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200142#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144int verify_fatal(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200145{
146 int *levels = (int *) data;
147
148 ((void) crt);
149 ((void) certificate_depth);
150
151 /* Simulate a fatal error in the callback */
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 if (*levels & (1 << certificate_depth)) {
153 *flags |= (1 << certificate_depth);
154 return -1 - certificate_depth;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200155 }
156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return 0;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200158}
159
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900160/* strsep() not available on Windows */
161char *mystrsep(char **stringp, const char *delim)
162{
163 const char *p;
164 char *ret = *stringp;
165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 if (*stringp == NULL) {
167 return NULL;
168 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 for (;; (*stringp)++) {
171 if (**stringp == '\0') {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900172 *stringp = NULL;
173 goto done;
174 }
175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 for (p = delim; *p != '\0'; p++) {
177 if (**stringp == *p) {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900178 **stringp = '\0';
179 (*stringp)++;
180 goto done;
181 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900183 }
184
185done:
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 return ret;
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900187}
188
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200189#if defined(MBEDTLS_X509_CRT_PARSE_C)
190typedef struct {
191 char buf[512];
192 char *p;
193} verify_print_context;
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195void verify_print_init(verify_print_context *ctx)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200196{
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 memset(ctx, 0, sizeof(verify_print_context));
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200198 ctx->p = ctx->buf;
199}
200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201int verify_print(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200202{
203 int ret;
204 verify_print_context *ctx = (verify_print_context *) data;
205 char *p = ctx->p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 size_t n = ctx->buf + sizeof(ctx->buf) - ctx->p;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200207 ((void) flags);
208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 ret = mbedtls_snprintf(p, n, "depth %d - serial ", certificate_depth);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200210 MBEDTLS_X509_SAFE_SNPRINTF;
211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200213 MBEDTLS_X509_SAFE_SNPRINTF;
214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 ret = mbedtls_snprintf(p, n, " - subject ");
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200216 MBEDTLS_X509_SAFE_SNPRINTF;
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200219 MBEDTLS_X509_SAFE_SNPRINTF;
220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 ret = mbedtls_snprintf(p, n, " - flags 0x%08x\n", *flags);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200222 MBEDTLS_X509_SAFE_SNPRINTF;
223
224 ctx->p = p;
225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return 0;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200227}
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229int verify_parse_san(mbedtls_x509_subject_alternative_name *san,
230 char **buf, size_t *size)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200231{
232 int ret;
233 size_t i;
234 char *p = *buf;
235 size_t n = *size;
236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 ret = mbedtls_snprintf(p, n, "type : %d", san->type);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200238 MBEDTLS_X509_SAFE_SNPRINTF;
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 switch (san->type) {
241 case (MBEDTLS_X509_SAN_OTHER_NAME):
242 ret = mbedtls_snprintf(p, n, "\notherName :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300243 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
David Horstmanncfae6a12023-08-18 19:12:59 +0100246 &san->san.other_name.type_id) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 ret = mbedtls_snprintf(p, n, " hardware module name :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300248 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 ret = mbedtls_snprintf(p, n, " hardware type : ");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300250 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 ret = mbedtls_oid_get_numeric_string(p,
253 n,
Matthias Schulzedc32ea2023-10-19 16:09:08 +0200254 &san->san.other_name.value.hardware_module_name
255 .oid);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300256 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 ret = mbedtls_snprintf(p, n, ", hardware serial number : ");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300259 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 for (i = 0; i < san->san.other_name.value.hardware_module_name.val.len; i++) {
262 ret = mbedtls_snprintf(p,
263 n,
264 "%02X",
265 san->san.other_name.value.hardware_module_name.val.p[i]);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300266 MBEDTLS_X509_SAFE_SNPRINTF;
267 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200268 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 break;/* MBEDTLS_OID_ON_HW_MODULE_NAME */
270 case (MBEDTLS_X509_SAN_DNS_NAME):
271 ret = mbedtls_snprintf(p, n, "\ndNSName : ");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200272 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (san->san.unstructured_name.len >= n) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200274 *p = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200276 }
277 n -= san->san.unstructured_name.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 for (i = 0; i < san->san.unstructured_name.len; i++) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200279 *p++ = san->san.unstructured_name.p[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 }
281 break;/* MBEDTLS_X509_SAN_DNS_NAME */
Przemek Stekiel608e3ef2023-02-09 14:47:50 +0100282 case (MBEDTLS_X509_SAN_RFC822_NAME):
283 ret = mbedtls_snprintf(p, n, "\nrfc822Name : ");
284 MBEDTLS_X509_SAFE_SNPRINTF;
285 if (san->san.unstructured_name.len >= n) {
286 *p = '\0';
287 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
288 }
289 n -= san->san.unstructured_name.len;
290 for (i = 0; i < san->san.unstructured_name.len; i++) {
291 *p++ = san->san.unstructured_name.p[i];
292 }
293 break;/* MBEDTLS_X509_SAN_RFC822_NAME */
Andrzej Kureke12b01d2023-01-10 06:47:38 -0500294 case (MBEDTLS_X509_SAN_DIRECTORY_NAME):
295 ret = mbedtls_snprintf(p, n, "\ndirectoryName : ");
296 MBEDTLS_X509_SAFE_SNPRINTF;
297 ret = mbedtls_x509_dn_gets(p, n, &san->san.directory_name);
298 if (ret < 0) {
299 return ret;
300 }
301
302 p += ret;
303 n -= ret;
304 break;/* MBEDTLS_X509_SAN_DIRECTORY_NAME */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200305 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 /*
307 * Should not happen.
308 */
309 return -1;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200310 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 ret = mbedtls_snprintf(p, n, "\n");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200312 MBEDTLS_X509_SAFE_SNPRINTF;
313
314 *size = n;
315 *buf = p;
316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200318}
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320int parse_crt_ext_cb(void *p_ctx, mbedtls_x509_crt const *crt, mbedtls_x509_buf const *oid,
321 int critical, const unsigned char *cp, const unsigned char *end)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200322{
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 (void) crt;
324 (void) critical;
325 mbedtls_x509_buf *new_oid = (mbedtls_x509_buf *) p_ctx;
326 if (oid->tag == MBEDTLS_ASN1_OID &&
327 MBEDTLS_OID_CMP(MBEDTLS_OID_CERTIFICATE_POLICIES, oid) == 0) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200328 /* Handle unknown certificate policy */
329 int ret, parse_ret = 0;
330 size_t len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 unsigned char **p = (unsigned char **) &cp;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200332
333 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 ret = mbedtls_asn1_get_tag(p, end, &len,
335 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
336 if (ret != 0) {
337 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
338 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (*p + len != end) {
341 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
342 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
343 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200344
345 /*
346 * Cannot be an empty sequence.
347 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (len == 0) {
349 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
350 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
351 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 while (*p < end) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200354 const unsigned char *policy_end;
355
356 /*
357 * Get the policy sequence
358 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
360 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
361 0) {
362 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
363 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200364
365 policy_end = *p + len;
366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
368 MBEDTLS_ASN1_OID)) != 0) {
369 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
370 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200371
Nicola Di Lietob77fad82020-06-17 17:57:36 +0200372 /*
373 * Recognize exclusively the policy with OID 1
374 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (len != 1 || *p[0] != 1) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200376 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200378
379 *p += len;
380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 /*
382 * If there is an optional qualifier, then *p < policy_end
383 * Check the Qualifier len to verify it doesn't exceed policy_end.
384 */
385 if (*p < policy_end) {
386 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
387 MBEDTLS_ASN1_CONSTRUCTED |
388 MBEDTLS_ASN1_SEQUENCE)) != 0) {
389 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
390 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200391 /*
392 * Skip the optional policy qualifiers.
393 */
394 *p += len;
395 }
396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 if (*p != policy_end) {
398 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
399 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
400 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200401 }
402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (*p != end) {
404 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
405 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
406 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 return parse_ret;
409 } else if (new_oid != NULL && new_oid->tag == oid->tag && new_oid->len == oid->len &&
410 memcmp(new_oid->p, oid->p, oid->len) == 0) {
411 return 0;
412 } else {
413 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
414 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200415 }
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200416}
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200417#endif /* MBEDTLS_X509_CRT_PARSE_C */
Matthias Schulzab408222023-10-18 13:20:59 +0200418
Matthias Schulz03bd0952023-10-19 09:52:59 +0200419#if defined(MBEDTLS_X509_CSR_PARSE_C)
Matthias Schulzab408222023-10-18 13:20:59 +0200420int parse_csr_ext_accept_cb(void *p_ctx, mbedtls_x509_csr const *csr, mbedtls_x509_buf const *oid,
Matthias Schulzedc32ea2023-10-19 16:09:08 +0200421 int critical, const unsigned char *cp, const unsigned char *end)
Matthias Schulzab408222023-10-18 13:20:59 +0200422{
423 (void) p_ctx;
424 (void) csr;
425 (void) oid;
426 (void) critical;
427 (void) cp;
428 (void) end;
429
430 return 0;
431}
432
433int parse_csr_ext_reject_cb(void *p_ctx, mbedtls_x509_csr const *csr, mbedtls_x509_buf const *oid,
Matthias Schulzedc32ea2023-10-19 16:09:08 +0200434 int critical, const unsigned char *cp, const unsigned char *end)
Matthias Schulzab408222023-10-18 13:20:59 +0200435{
436 (void) p_ctx;
437 (void) csr;
438 (void) oid;
439 (void) critical;
440 (void) cp;
441 (void) end;
442
443 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
444 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
445}
Matthias Schulz03bd0952023-10-19 09:52:59 +0200446#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200447/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000448
Thomas Daubney5c9c2ce2022-06-06 16:36:43 +0100449/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100450void x509_accessor_ext_types(int ext_type, int has_ext_type)
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100451{
452 mbedtls_x509_crt crt;
453 int expected_result = ext_type & has_ext_type;
454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200456 USE_PSA_INIT();
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100457
458 crt.ext_types = ext_type;
459
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500460 TEST_EQUAL(mbedtls_x509_crt_has_ext_type(&crt, has_ext_type), expected_result);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100461
valerio32f2ac92023-04-20 11:59:52 +0200462exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200464 USE_PSA_DONE();
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100465}
466/* END_CASE */
467
Glenn Strauss6f545ac2022-10-25 15:02:14 -0400468/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_TEST_HOOKS */
469void x509_crt_parse_cn_inet_pton(const char *cn, data_t *exp, int ref_ret)
470{
471 uint32_t addr[4];
472 size_t addrlen = mbedtls_x509_crt_parse_cn_inet_pton(cn, addr);
473 TEST_EQUAL(addrlen, (size_t) ref_ret);
474
475 if (addrlen) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100476 TEST_MEMORY_COMPARE(exp->x, exp->len, addr, addrlen);
Glenn Strauss6f545ac2022-10-25 15:02:14 -0400477 }
478}
479/* END_CASE */
480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500482void x509_parse_san(char *crt_file, char *result_str, int parse_result)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200483{
Ron Eldor890819a2019-05-13 19:03:04 +0300484 int ret;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200485 mbedtls_x509_crt crt;
Ron Eldor890819a2019-05-13 19:03:04 +0300486 mbedtls_x509_subject_alternative_name san;
487 mbedtls_x509_sequence *cur = NULL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200488 char buf[2000];
489 char *p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 size_t n = sizeof(buf);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200493 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 memset(buf, 0, 2000);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200495
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500496 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), parse_result);
Ron Eldor890819a2019-05-13 19:03:04 +0300497
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500498 if (parse_result != 0) {
499 goto exit;
500 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 if (crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Ron Eldor890819a2019-05-13 19:03:04 +0300502 cur = &crt.subject_alt_names;
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 while (cur != NULL) {
504 ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
505 TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
Ron Eldor890819a2019-05-13 19:03:04 +0300506 /*
507 * If san type not supported, ignore.
508 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if (ret == 0) {
Andrzej Kurekd40c2b62023-02-13 07:01:59 -0500510 ret = verify_parse_san(&san, &p, &n);
511 mbedtls_x509_free_subject_alt_name(&san);
512 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 }
Ron Eldor890819a2019-05-13 19:03:04 +0300514 cur = cur->next;
515 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200516 }
517
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500518 TEST_EQUAL(strcmp(buf, result_str), 0);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200519
520exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200522 USE_PSA_DONE();
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200523}
524/* END_CASE */
525
Hanno Becker612a2f12020-10-09 09:19:39 +0100526/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100527void x509_cert_info(char *crt_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000528{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000530 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000531 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200534 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000536
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500537 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 res = mbedtls_x509_crt_info(buf, 2000, "", &crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 TEST_ASSERT(res != -1);
541 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000542
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500543 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200544
545exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200547 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000548}
Paul Bakker33b43f12013-08-20 11:48:36 +0200549/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000550
Hanno Becker612a2f12020-10-09 09:19:39 +0100551/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100552void mbedtls_x509_crl_info(char *crl_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000553{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000555 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000556 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200559 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000561
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500562 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 res = mbedtls_x509_crl_info(buf, 2000, "", &crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 TEST_ASSERT(res != -1);
566 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000567
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500568 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200569
570exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +0200572 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000573}
Paul Bakker33b43f12013-08-20 11:48:36 +0200574/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000575
Andres AGa39db392016-12-08 17:10:38 +0000576/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100577void mbedtls_x509_crl_parse(char *crl_file, int result)
Andres AGa39db392016-12-08 17:10:38 +0000578{
579 mbedtls_x509_crl crl;
580 char buf[2000];
581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200583 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 memset(buf, 0, 2000);
Andres AGa39db392016-12-08 17:10:38 +0000585
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500586 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), result);
Andres AGa39db392016-12-08 17:10:38 +0000587
588exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +0200590 USE_PSA_DONE();
Andres AGa39db392016-12-08 17:10:38 +0000591}
592/* END_CASE */
593
Hanno Becker612a2f12020-10-09 09:19:39 +0100594/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100595void mbedtls_x509_csr_info(char *csr_file, char *result_str)
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100596{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100598 char buf[2000];
599 int res;
600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +0200602 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 memset(buf, 0, 2000);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100604
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500605 TEST_EQUAL(mbedtls_x509_csr_parse_file(&csr, csr_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 res = mbedtls_x509_csr_info(buf, 2000, "", &csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 TEST_ASSERT(res != -1);
609 TEST_ASSERT(res != -2);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100610
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500611 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200612
613exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +0200615 USE_PSA_DONE();
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100616}
617/* END_CASE */
618
Hanno Becker612a2f12020-10-09 09:19:39 +0100619/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100620void x509_verify_info(int flags, char *prefix, char *result_str)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100621{
622 char buf[2000];
623 int res;
624
Valerio Setti569c1712023-04-19 14:53:36 +0200625 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 memset(buf, 0, sizeof(buf));
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 res = mbedtls_x509_crt_verify_info(buf, sizeof(buf), prefix, flags);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 TEST_ASSERT(res >= 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100631
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500632 TEST_EQUAL(strcmp(buf, result_str), 0);
valerio32f2ac92023-04-20 11:59:52 +0200633
634exit:
Valerio Setti569c1712023-04-19 14:53:36 +0200635 USE_PSA_DONE();
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100636}
637/* END_CASE */
638
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200639/* 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 +0100640void x509_verify_restart(char *crt_file, char *ca_file,
641 int result, int flags_result,
642 int max_ops, int min_restart, int max_restart)
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200643{
644 int ret, cnt_restart;
645 mbedtls_x509_crt_restart_ctx rs_ctx;
646 mbedtls_x509_crt crt;
647 mbedtls_x509_crt ca;
648 uint32_t flags = 0;
649
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200650 /*
651 * See comments on ecp_test_vect_restart() for op count precision.
652 *
Gilles Peskinee820c0a2023-08-03 17:45:20 +0200653 * For reference, with Mbed TLS 2.6 and default settings:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200654 * - ecdsa_verify() for P-256: ~ 6700
655 * - ecdsa_verify() for P-384: ~ 18800
656 * - x509_verify() for server5 -> test-ca2: ~ 18800
657 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
658 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 mbedtls_x509_crt_restart_init(&rs_ctx);
660 mbedtls_x509_crt_init(&crt);
661 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200662 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200663
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500664 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
665 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 mbedtls_ecp_set_max_ops(max_ops);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200668
669 cnt_restart = 0;
670 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
672 &mbedtls_x509_crt_profile_default, NULL, &flags,
673 NULL, NULL, &rs_ctx);
674 } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200675
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500676 TEST_EQUAL(ret, result);
677 TEST_EQUAL(flags, (uint32_t) flags_result);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 TEST_ASSERT(cnt_restart >= min_restart);
680 TEST_ASSERT(cnt_restart <= max_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200681
682 /* Do we leak memory when aborting? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
684 &mbedtls_x509_crt_profile_default, NULL, &flags,
685 NULL, NULL, &rs_ctx);
686 TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200687
688exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 mbedtls_x509_crt_restart_free(&rs_ctx);
690 mbedtls_x509_crt_free(&crt);
691 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100692 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200693}
694/* END_CASE */
695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100697void x509_verify(char *crt_file, char *ca_file, char *crl_file,
698 char *cn_name_str, int result, int flags_result,
699 char *profile_str,
700 char *verify_callback)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000701{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_x509_crt crt;
703 mbedtls_x509_crt ca;
704 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200705 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000706 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200707 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 char *cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200709 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 mbedtls_x509_crt_init(&crt);
712 mbedtls_x509_crt_init(&ca);
713 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200714 MD_OR_USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if (strcmp(cn_name_str, "NULL") != 0) {
Paul Bakker33b43f12013-08-20 11:48:36 +0200717 cn_name = cn_name_str;
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (strcmp(profile_str, "") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200721 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 } else if (strcmp(profile_str, "next") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200723 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 } else if (strcmp(profile_str, "suite_b") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200725 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 } else if (strcmp(profile_str, "compat") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200727 profile = &compat_profile;
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 } else if (strcmp(profile_str, "all") == 0) {
Hanno Becker20a4ade2019-06-03 14:27:03 +0100729 profile = &profile_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100731 TEST_FAIL("Unknown algorithm profile");
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 }
Gilles Peskineef86ab22017-05-05 18:59:02 +0200733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if (strcmp(verify_callback, "NULL") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200735 f_vrfy = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 } else if (strcmp(verify_callback, "verify_none") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200737 f_vrfy = verify_none;
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 } else if (strcmp(verify_callback, "verify_all") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200739 f_vrfy = verify_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100741 TEST_FAIL("No known verify callback selected");
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200743
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500744 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
745 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
746 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 res = mbedtls_x509_crt_verify_with_profile(&crt,
749 &ca,
750 &crl,
751 profile,
752 cn_name,
753 &flags,
754 f_vrfy,
755 NULL);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 TEST_EQUAL(res, result);
758 TEST_EQUAL(flags, (uint32_t) flags_result);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200759
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200760#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker0350d562019-03-28 14:23:36 +0000761 /* CRLs aren't supported with CA callbacks, so skip the CA callback
Jarno Lamsadfd22c42019-04-01 15:18:53 +0300762 * version of the test if CRLs are in use. */
Gilles Peskinebb7d92c2023-10-17 17:26:44 +0200763 if (strcmp(crl_file, "") == 0) {
Hanno Becker0350d562019-03-28 14:23:36 +0000764 flags = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 res = mbedtls_x509_crt_verify_with_ca_cb(&crt,
767 ca_callback,
768 &ca,
769 profile,
770 cn_name,
771 &flags,
772 f_vrfy,
773 NULL);
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200774
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500775 TEST_EQUAL(res, result);
776 TEST_EQUAL(flags, (uint32_t) (flags_result));
Hanno Becker0350d562019-03-28 14:23:36 +0000777 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200778#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200779exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 mbedtls_x509_crt_free(&crt);
781 mbedtls_x509_crt_free(&ca);
782 mbedtls_x509_crl_free(&crl);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100783 MD_OR_USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000784}
Paul Bakker33b43f12013-08-20 11:48:36 +0200785/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000786
Jarno Lamsa557426a2019-03-27 17:08:29 +0200787/* 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 +0100788void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
789 int exp_ret)
Jarno Lamsa557426a2019-03-27 17:08:29 +0200790{
791 int ret;
792 mbedtls_x509_crt crt;
793 mbedtls_x509_crt ca;
794 uint32_t flags = 0;
Hanno Becker3f932bb2019-03-28 17:06:47 +0000795
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 mbedtls_x509_crt_init(&crt);
797 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200798 USE_PSA_INIT();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200799
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500800 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
801 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if (strcmp(name, "NULL") == 0) {
Jarno Lamsa557426a2019-03-27 17:08:29 +0200804 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca,
808 &compat_profile, name, &flags,
809 NULL, NULL);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200810
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500811 TEST_EQUAL(ret, exp_ret);
812 TEST_EQUAL(flags, (uint32_t) (-1));
Jarno Lamsa557426a2019-03-27 17:08:29 +0200813exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 mbedtls_x509_crt_free(&crt);
815 mbedtls_x509_crt_free(&ca);
Valerio Setti569c1712023-04-19 14:53:36 +0200816 USE_PSA_DONE();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200817}
818/* END_CASE */
819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821void x509_verify_callback(char *crt_file, char *ca_file, char *name,
822 int exp_ret, char *exp_vrfy_out)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200823{
824 int ret;
825 mbedtls_x509_crt crt;
826 mbedtls_x509_crt ca;
827 uint32_t flags = 0;
828 verify_print_context vrfy_ctx;
829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 mbedtls_x509_crt_init(&crt);
831 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200832 MD_OR_USE_PSA_INIT();
833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 verify_print_init(&vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200835
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500836 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
837 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if (strcmp(name, "NULL") == 0) {
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200840 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 }
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL,
844 &compat_profile,
845 name, &flags,
846 verify_print, &vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200847
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500848 TEST_EQUAL(ret, exp_ret);
849 TEST_EQUAL(strcmp(vrfy_ctx.buf, exp_vrfy_out), 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200850
851exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 mbedtls_x509_crt_free(&crt);
853 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100854 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200855}
856/* END_CASE */
857
Hanno Becker612a2f12020-10-09 09:19:39 +0100858/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100859void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
860 char *new_subject_ou,
861 char *result_str,
862 int ret)
Werner Lewis31ecb962022-06-17 15:51:55 +0100863{
864 mbedtls_x509_crt crt;
865 char buf[2000];
866 int res = 0;
867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200869 USE_PSA_INIT();
870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 memset(buf, 0, 2000);
Werner Lewis31ecb962022-06-17 15:51:55 +0100872
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500873 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100874 crt.subject.next->val.p = (unsigned char *) new_subject_ou;
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 crt.subject.next->val.len = strlen(new_subject_ou);
Werner Lewis31ecb962022-06-17 15:51:55 +0100876
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
Werner Lewis31ecb962022-06-17 15:51:55 +0100878
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 if (ret != 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500880 TEST_EQUAL(res, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 } else {
882 TEST_ASSERT(res != -1);
883 TEST_ASSERT(res != -2);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500884 TEST_EQUAL(strcmp(buf, result_str), 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100885 }
886exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200888 USE_PSA_DONE();
Werner Lewis31ecb962022-06-17 15:51:55 +0100889}
890/* END_CASE */
891
892/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100893void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000894{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000896 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200897 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200900 USE_PSA_INIT();
901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000903
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500904 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if (strcmp(entity, "subject") == 0) {
906 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
907 } else if (strcmp(entity, "issuer") == 0) {
908 res = mbedtls_x509_dn_gets(buf, 2000, &crt.issuer);
909 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100910 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 TEST_ASSERT(res != -1);
914 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000915
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500916 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200917
918exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200920 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000921}
Paul Bakker33b43f12013-08-20 11:48:36 +0200922/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000923
David Horstmanndb73d3b2022-10-04 16:49:16 +0100924/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100925void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
David Horstmanndb73d3b2022-10-04 16:49:16 +0100926{
valerioe50831c2023-04-20 14:48:19 +0200927 unsigned char *name = NULL;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100928 unsigned char *p;
929 size_t name_len;
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100930 mbedtls_x509_name head;
David Horstmann3cd67582022-10-17 17:59:10 +0100931 int ret;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100932
Valerio Setti569c1712023-04-19 14:53:36 +0200933 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 memset(&head, 0, sizeof(head));
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100935
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100937 p = name;
938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 ret = mbedtls_x509_get_name(&p, (name + name_len), &head);
940 if (ret == 0) {
941 mbedtls_asn1_free_named_data_list_shallow(head.next);
942 }
David Horstmanndb73d3b2022-10-04 16:49:16 +0100943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 TEST_EQUAL(ret, exp_ret);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100945
valerio32f2ac92023-04-20 11:59:52 +0200946exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 mbedtls_free(name);
Valerio Setti569c1712023-04-19 14:53:36 +0200948 USE_PSA_DONE();
David Horstmanndb73d3b2022-10-04 16:49:16 +0100949}
950/* END_CASE */
951
Werner Lewisb3acb052022-06-17 15:59:58 +0100952/* 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 +0100953void mbedtls_x509_dn_get_next(char *name_str,
954 int next_merged,
955 char *expected_oids,
956 int exp_count,
957 char *exp_dn_gets)
Werner Lewisb3acb052022-06-17 15:59:58 +0100958{
959 int ret = 0, i;
Werner Lewisac80a662022-06-23 11:58:02 +0100960 size_t len = 0, out_size;
Werner Lewisb3acb052022-06-17 15:59:58 +0100961 mbedtls_asn1_named_data *names = NULL;
Gilles Peskine21e46b32023-10-17 16:35:20 +0200962 mbedtls_x509_name parsed;
963 memset(&parsed, 0, sizeof(parsed));
964 mbedtls_x509_name *parsed_cur;
Werner Lewisac80a662022-06-23 11:58:02 +0100965 // Size of buf is maximum required for test cases
Gilles Peskinef2574202023-10-18 17:39:48 +0200966 unsigned char buf[80] = { 0 };
Gilles Peskine21e46b32023-10-17 16:35:20 +0200967 unsigned char *out = NULL;
968 unsigned char *c = buf + sizeof(buf);
Werner Lewisb3acb052022-06-17 15:59:58 +0100969 const char *short_name;
970
Valerio Setti569c1712023-04-19 14:53:36 +0200971 USE_PSA_INIT();
Gilles Peskine21e46b32023-10-17 16:35:20 +0200972
Werner Lewisac80a662022-06-23 11:58:02 +0100973 // Additional size required for trailing space
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 out_size = strlen(expected_oids) + 2;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100975 TEST_CALLOC(out, out_size);
Werner Lewisb3acb052022-06-17 15:59:58 +0100976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 TEST_EQUAL(mbedtls_x509_string_to_names(&names, name_str), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 ret = mbedtls_x509_write_names(&c, buf, names);
980 TEST_LE_S(0, ret);
Werner Lewisb3acb052022-06-17 15:59:58 +0100981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
983 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
984 TEST_EQUAL(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100985
986 // Iterate over names and set next_merged nodes
987 parsed_cur = &parsed;
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 for (; next_merged != 0 && parsed_cur != NULL; next_merged = next_merged >> 1) {
Werner Lewis12657cd2022-06-20 11:47:57 +0100989 parsed_cur->next_merged = next_merged & 0x01;
Werner Lewisb3acb052022-06-17 15:59:58 +0100990 parsed_cur = parsed_cur->next;
991 }
992
993 // Iterate over RDN nodes and print OID of first element to buffer
994 parsed_cur = &parsed;
995 len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 for (i = 0; parsed_cur != NULL; i++) {
997 TEST_EQUAL(mbedtls_oid_get_attr_short_name(&parsed_cur->oid,
998 &short_name), 0);
999 len += mbedtls_snprintf((char *) out + len, out_size - len, "%s ", short_name);
1000 parsed_cur = mbedtls_x509_dn_get_next(parsed_cur);
Werner Lewisb3acb052022-06-17 15:59:58 +01001001 }
1002 out[len-1] = 0;
1003
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 TEST_EQUAL(exp_count, i);
1005 TEST_EQUAL(strcmp((char *) out, expected_oids), 0);
1006 mbedtls_free(out);
Werner Lewisac80a662022-06-23 11:58:02 +01001007 out = NULL;
Werner Lewisb3acb052022-06-17 15:59:58 +01001008
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 out_size = strlen(exp_dn_gets) + 1;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001010 TEST_CALLOC(out, out_size);
Werner Lewisac80a662022-06-23 11:58:02 +01001011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 TEST_LE_S(0, mbedtls_x509_dn_gets((char *) out, out_size, &parsed));
1013 TEST_EQUAL(strcmp((char *) out, exp_dn_gets), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +01001014exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 mbedtls_free(out);
1016 mbedtls_asn1_free_named_data_list(&names);
1017 mbedtls_asn1_free_named_data_list_shallow(parsed.next);
Valerio Setti569c1712023-04-19 14:53:36 +02001018 USE_PSA_DONE();
Werner Lewisb3acb052022-06-17 15:59:58 +01001019}
Werner Lewisb3acb052022-06-17 15:59:58 +01001020/* END_CASE */
1021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001023void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
Paul Bakker37940d9f2009-07-10 22:38:58 +00001024{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +00001026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001028 USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +00001029
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001030 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001031
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 if (strcmp(entity, "valid_from") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001033 TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_from), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 } else if (strcmp(entity, "valid_to") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001035 TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_to), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +01001037 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001039
Paul Bakkerbd51b262014-07-10 15:26:12 +02001040exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001042 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +00001043}
Paul Bakker33b43f12013-08-20 11:48:36 +02001044/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +00001045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001047void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001048{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001052 USE_PSA_INIT();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001053
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001054 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if (strcmp(entity, "valid_from") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001057 TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_from), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 } else if (strcmp(entity, "valid_to") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001059 TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_to), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +01001061 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001063
Paul Bakkerbd51b262014-07-10 15:26:12 +02001064exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001066 USE_PSA_DONE();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001067}
1068/* END_CASE */
1069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001071void x509parse_crt_file(char *crt_file, int result)
Paul Bakker5a5fa922014-09-26 14:53:04 +02001072{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +02001074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001076 USE_PSA_INIT();
Paul Bakker5a5fa922014-09-26 14:53:04 +02001077
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001078 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), result);
Paul Bakker5a5fa922014-09-26 14:53:04 +02001079
1080exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001082 USE_PSA_DONE();
Paul Bakker5a5fa922014-09-26 14:53:04 +02001083}
1084/* END_CASE */
1085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001087void x509parse_crt(data_t *buf, char *result_str, int result)
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 mbedtls_x509_crt crt;
Hanno Becker612a2f12020-10-09 09:19:39 +01001090#if !defined(MBEDTLS_X509_REMOVE_INFO)
Hanno Beckerfff2d572020-10-09 09:45:19 +01001091 unsigned char output[2000] = { 0 };
Azim Khanf1aaec92017-05-30 14:23:15 +01001092 int res;
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001093#else
1094 ((void) result_str);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001095#endif
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001098 USE_PSA_INIT();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001099
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001100 TEST_EQUAL(mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len), result);
Hanno Becker612a2f12020-10-09 09:19:39 +01001101#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 if ((result) == 0) {
1103 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1104 TEST_ASSERT(res != -1);
1105 TEST_ASSERT(res != -2);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001106
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001107 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001108 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001110#endif
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 mbedtls_x509_crt_free(&crt);
1113 mbedtls_x509_crt_init(&crt);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001114
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001115 TEST_EQUAL(mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len), result);
Hanno Becker612a2f12020-10-09 09:19:39 +01001116#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 if ((result) == 0) {
1118 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakker33b43f12013-08-20 11:48:36 +02001121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 TEST_ASSERT(res != -1);
1123 TEST_ASSERT(res != -2);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001124
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001125 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001126 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001128#endif /* !MBEDTLS_X509_REMOVE_INFO */
Paul Bakkerb08e6842012-02-11 18:43:20 +00001129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 mbedtls_x509_crt_free(&crt);
1131 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001132
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001133 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL, NULL),
1134 result);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001135#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 if ((result) == 0) {
1137 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 TEST_ASSERT(res != -1);
1140 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001141
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001142 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001143 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001145#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 mbedtls_x509_crt_free(&crt);
1148 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001149
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001150 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL, NULL),
1151 result);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001152#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 if ((result) == 0) {
1154 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 TEST_ASSERT(res != -1);
1157 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001158
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001159 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001160 }
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001161#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001162
1163exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001165 USE_PSA_DONE();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001166}
1167/* END_CASE */
1168
1169/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001170void x509parse_crt_cb(data_t *buf, char *result_str, int result)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001171{
1172 mbedtls_x509_crt crt;
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001173 mbedtls_x509_buf oid;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001174
1175#if !defined(MBEDTLS_X509_REMOVE_INFO)
1176 unsigned char output[2000] = { 0 };
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001177 int res;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001178#else
1179 ((void) result_str);
1180#endif
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001181
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001182 oid.tag = MBEDTLS_ASN1_OID;
1183 oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001187 USE_PSA_INIT();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001188
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001189 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
1190 &oid), result);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001191#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 if ((result) == 0) {
1193 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 TEST_ASSERT(res != -1);
1196 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001197
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001198 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001199 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 memset(output, 0, 2000);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001201#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 mbedtls_x509_crt_free(&crt);
1204 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001205
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001206 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1207 &oid), (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001208#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 if ((result) == 0) {
1210 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 TEST_ASSERT(res != -1);
1213 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001214
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001215 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001216 }
Hanno Beckerfff2d572020-10-09 09:45:19 +01001217#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001218
Paul Bakkerbd51b262014-07-10 15:26:12 +02001219exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001221 USE_PSA_DONE();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001222}
Paul Bakker33b43f12013-08-20 11:48:36 +02001223/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001224
Hanno Becker612a2f12020-10-09 09:19:39 +01001225/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001226void x509parse_crl(data_t *buf, char *result_str, int result)
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001229 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +01001230 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +02001233 USE_PSA_INIT();
1234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 memset(output, 0, 2000);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001236
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001237
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001238 TEST_EQUAL(mbedtls_x509_crl_parse(&crl, buf->x, buf->len), (result));
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 if ((result) == 0) {
1240 res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
Paul Bakker33b43f12013-08-20 11:48:36 +02001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 TEST_ASSERT(res != -1);
1243 TEST_ASSERT(res != -2);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001244
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001245 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001246 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001247
Paul Bakkerbd51b262014-07-10 15:26:12 +02001248exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +02001250 USE_PSA_DONE();
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001251}
Paul Bakker33b43f12013-08-20 11:48:36 +02001252/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001253
Hanno Becker612a2f12020-10-09 09:19:39 +01001254/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001255void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001256{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001258 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001259 int my_ret;
1260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001262 USE_PSA_INIT();
1263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 memset(my_out, 0, sizeof(my_out));
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001265
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001267 TEST_EQUAL(my_ret, ref_ret);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 if (ref_ret == 0) {
1270 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001271 TEST_EQUAL(my_out_len, strlen(ref_out));
1272 TEST_EQUAL(strcmp(my_out, ref_out), 0);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001273 }
1274
Paul Bakkerbd51b262014-07-10 15:26:12 +02001275exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001277 USE_PSA_DONE();
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001278}
1279/* END_CASE */
1280
Matthias Schulzab408222023-10-18 13:20:59 +02001281/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1282void mbedtls_x509_csr_parse_with_ext_cb(data_t *csr_der, char *ref_out, int ref_ret, int accept)
1283{
1284 mbedtls_x509_csr csr;
1285 char my_out[1000];
1286 int my_ret;
1287
1288 mbedtls_x509_csr_init(&csr);
1289 USE_PSA_INIT();
1290
1291 memset(my_out, 0, sizeof(my_out));
1292
1293 my_ret = mbedtls_x509_csr_parse_der_with_ext_cb(&csr, csr_der->x, csr_der->len,
1294 accept ? parse_csr_ext_accept_cb :
Matthias Schulzedc32ea2023-10-19 16:09:08 +02001295 parse_csr_ext_reject_cb,
Matthias Schulzab408222023-10-18 13:20:59 +02001296 NULL);
1297 TEST_EQUAL(my_ret, ref_ret);
1298
1299 if (ref_ret == 0) {
1300 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1301 TEST_EQUAL(my_out_len, strlen(ref_out));
1302 TEST_EQUAL(strcmp(my_out, ref_out), 0);
1303 }
1304
1305exit:
1306 mbedtls_x509_csr_free(&csr);
1307 USE_PSA_DONE();
1308}
1309/* END_CASE */
1310
Przemek Stekield7992df2023-01-25 16:19:50 +01001311/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1312void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
1313{
1314 mbedtls_x509_csr csr;
1315 char my_out[1000];
1316 int my_ret;
1317
1318 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001319 USE_PSA_INIT();
1320
Przemek Stekield7992df2023-01-25 16:19:50 +01001321 memset(my_out, 0, sizeof(my_out));
1322
1323 my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001324 TEST_EQUAL(my_ret, ref_ret);
Przemek Stekield7992df2023-01-25 16:19:50 +01001325
1326 if (ref_ret == 0) {
1327 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001328 TEST_EQUAL(my_out_len, strlen(ref_out));
1329 TEST_EQUAL(strcmp(my_out, ref_out), 0);
Przemek Stekield7992df2023-01-25 16:19:50 +01001330 }
1331
1332exit:
1333 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001334 USE_PSA_DONE();
Przemek Stekield7992df2023-01-25 16:19:50 +01001335}
1336/* END_CASE */
1337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1e5fec62023-04-13 18:13:48 +02001339void mbedtls_x509_crt_parse_file(char *crt_path, int ret, int nb_crt)
1340{
1341 mbedtls_x509_crt chain, *cur;
1342 int i;
1343
1344 mbedtls_x509_crt_init(&chain);
1345 USE_PSA_INIT();
1346
1347 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, crt_path), ret);
1348
1349 /* Check how many certs we got */
1350 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1351 if (cur->raw.p != NULL) {
1352 i++;
1353 }
1354 }
1355
1356 TEST_EQUAL(i, nb_crt);
1357
1358exit:
1359 mbedtls_x509_crt_free(&chain);
1360 USE_PSA_DONE();
1361}
1362/* END_CASE */
1363
1364/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001365void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001366{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001368 int i;
1369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001371 USE_PSA_INIT();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001372
Gilles Peskine55ad28a2023-04-13 18:14:45 +02001373 TEST_EQUAL(mbedtls_x509_crt_parse_path(&chain, crt_path), ret);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001374
1375 /* Check how many certs we got */
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1377 if (cur->raw.p != NULL) {
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001378 i++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 }
1380 }
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001381
Gilles Peskine55ad28a2023-04-13 18:14:45 +02001382 TEST_EQUAL(i, nb_crt);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001383
Paul Bakkerbd51b262014-07-10 15:26:12 +02001384exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 mbedtls_x509_crt_free(&chain);
Valerio Setti569c1712023-04-19 14:53:36 +02001386 USE_PSA_DONE();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001387}
1388/* END_CASE */
1389
Janos Follath822b2c32015-10-11 10:25:22 +02001390/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001391void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1392 int ret_chk, int flags_chk)
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001393{
1394 char file_buf[128];
1395 int ret;
1396 uint32_t flags;
1397 mbedtls_x509_crt trusted, chain;
1398
1399 /*
1400 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1401 * with NN.crt signed by NN-1.crt
1402 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 mbedtls_x509_crt_init(&trusted);
1404 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001405 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001406
1407 /* Load trusted root */
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001408 TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, ca_file), 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001409
1410 /* Load a chain with nb_int intermediates (from 01 to nb_int),
1411 * plus one "end-entity" cert (nb_int + 1) */
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001412 ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 nb_int + 1);
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001414 TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001415 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, file_buf), 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001416
1417 /* Try to verify that chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1419 NULL, NULL);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001420 TEST_EQUAL(ret, ret_chk);
1421 TEST_EQUAL(flags, (uint32_t) flags_chk);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001422
1423exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 mbedtls_x509_crt_free(&chain);
1425 mbedtls_x509_crt_free(&trusted);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001426 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001427}
1428/* END_CASE */
1429
1430/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001431void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1432 int flags_result, int result,
1433 char *profile_name, int vrfy_fatal_lvls)
Janos Follath822b2c32015-10-11 10:25:22 +02001434{
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 char *act;
Janos Follath822b2c32015-10-11 10:25:22 +02001436 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001437 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001438 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001439 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +02001440
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 mbedtls_x509_crt_init(&chain);
1442 mbedtls_x509_crt_init(&trusted);
valerio32f2ac92023-04-20 11:59:52 +02001443 MD_OR_USE_PSA_INIT();
Janos Follath822b2c32015-10-11 10:25:22 +02001444
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 while ((act = mystrsep(&chain_paths, " ")) != NULL) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001446 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, act), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 }
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001448 TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, trusted_ca), 0);
Janos Follath822b2c32015-10-11 10:25:22 +02001449
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 if (strcmp(profile_name, "") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001451 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 } else if (strcmp(profile_name, "next") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001453 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 } else if (strcmp(profile_name, "suiteb") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001455 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 } else if (strcmp(profile_name, "rsa3072") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001457 profile = &profile_rsa3072;
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 } else if (strcmp(profile_name, "sha512") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001459 profile = &profile_sha512;
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 }
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1463 NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
Janos Follathef4f2582015-10-11 16:17:27 +02001464
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001465 TEST_EQUAL(res, (result));
1466 TEST_EQUAL(flags, (uint32_t) (flags_result));
Janos Follath822b2c32015-10-11 10:25:22 +02001467
1468exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 mbedtls_x509_crt_free(&trusted);
1470 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001471 MD_OR_USE_PSA_DONE();
Janos Follath822b2c32015-10-11 10:25:22 +02001472}
1473/* END_CASE */
1474
Hanno Becker612a2f12020-10-09 09:19:39 +01001475/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001476void x509_oid_desc(data_t *buf, char *ref_desc)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001477{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001479 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001480 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001481
Valerio Setti569c1712023-04-19 14:53:36 +02001482 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001485 oid.p = buf->x;
1486 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 ret = mbedtls_oid_get_extended_key_usage(&oid, &desc);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 if (strcmp(ref_desc, "notfound") == 0) {
1491 TEST_ASSERT(ret != 0);
1492 TEST_ASSERT(desc == NULL);
1493 } else {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001494 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 TEST_ASSERT(desc != NULL);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001496 TEST_EQUAL(strcmp(desc, ref_desc), 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001497 }
valerio32f2ac92023-04-20 11:59:52 +02001498
1499exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001500 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001501}
1502/* END_CASE */
1503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001505void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001506{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001508 char num_buf[100];
1509
Valerio Setti569c1712023-04-19 14:53:36 +02001510 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001511
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001512 memset(num_buf, 0x2a, sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001515 oid.p = oid_buf->x;
1516 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001517
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001518 TEST_ASSERT((size_t) blen <= sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001519
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001520 TEST_EQUAL(mbedtls_oid_get_numeric_string(num_buf, blen, &oid), ret);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 if (ret >= 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001523 TEST_EQUAL(num_buf[ret], 0);
1524 TEST_EQUAL(strcmp(num_buf, numstr), 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001525 }
valerio32f2ac92023-04-20 11:59:52 +02001526
1527exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001528 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001529}
1530/* END_CASE */
1531
TRodziewicz442fdc22021-06-07 13:52:23 +02001532/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001533void x509_check_key_usage(char *crt_file, int usage, int ret)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001534{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001536
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001538 USE_PSA_INIT();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001539
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001540 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001541
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001542 TEST_EQUAL(mbedtls_x509_crt_check_key_usage(&crt, usage), ret);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001543
Paul Bakkerbd51b262014-07-10 15:26:12 +02001544exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001546 USE_PSA_DONE();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001547}
1548/* END_CASE */
1549
TRodziewicz442fdc22021-06-07 13:52:23 +02001550/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001551void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1552 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001553{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001557 USE_PSA_INIT();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001558
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001559 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001560
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001561 TEST_EQUAL(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x, oid->len),
1562 ret);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001563
Paul Bakkerbd51b262014-07-10 15:26:12 +02001564exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001566 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001567}
1568/* END_CASE */
1569
Andres AG4b76aec2016-09-23 13:16:02 +01001570/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001571void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1572 int day, int hour, int min, int sec)
Andres AG4b76aec2016-09-23 13:16:02 +01001573{
1574 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001575 unsigned char buf[21];
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 unsigned char *start = buf;
1577 unsigned char *end = buf;
Andres AG4b76aec2016-09-23 13:16:02 +01001578
Valerio Setti569c1712023-04-19 14:53:36 +02001579 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 memset(&time, 0x00, sizeof(time));
1581 *end = (unsigned char) tag; end++;
1582 *end = strlen(time_str);
1583 TEST_ASSERT(*end < 20);
Andres AG4b76aec2016-09-23 13:16:02 +01001584 end++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 memcpy(end, time_str, (size_t) *(end - 1));
Andres AG4b76aec2016-09-23 13:16:02 +01001586 end += *(end - 1);
1587
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001588 TEST_EQUAL(mbedtls_x509_get_time(&start, end, &time), ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 if (ret == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001590 TEST_EQUAL(year, time.year);
1591 TEST_EQUAL(mon, time.mon);
1592 TEST_EQUAL(day, time.day);
1593 TEST_EQUAL(hour, time.hour);
1594 TEST_EQUAL(min, time.min);
1595 TEST_EQUAL(sec, time.sec);
Andres AG4b76aec2016-09-23 13:16:02 +01001596 }
valerio32f2ac92023-04-20 11:59:52 +02001597exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001598 USE_PSA_DONE();
Andres AG4b76aec2016-09-23 13:16:02 +01001599}
1600/* END_CASE */
1601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001603void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1604 int ref_msg_md, int ref_mgf_md,
1605 int ref_salt_len, int ref_ret)
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001606{
1607 int my_ret;
Ronald Cronac6ae352020-06-26 14:33:03 +02001608 mbedtls_x509_buf buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001610 int my_salt_len;
1611
Valerio Setti569c1712023-04-19 14:53:36 +02001612 USE_PSA_INIT();
1613
Ronald Cronac6ae352020-06-26 14:33:03 +02001614 buf.p = params->x;
1615 buf.len = params->len;
1616 buf.tag = params_tag;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001617
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1619 &my_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001620
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001621 TEST_EQUAL(my_ret, ref_ret);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 if (ref_ret == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001624 TEST_EQUAL(my_msg_md, (mbedtls_md_type_t) ref_msg_md);
1625 TEST_EQUAL(my_mgf_md, (mbedtls_md_type_t) ref_mgf_md);
1626 TEST_EQUAL(my_salt_len, ref_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001627 }
1628
Paul Bakkerbd51b262014-07-10 15:26:12 +02001629exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001630 USE_PSA_DONE();
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001631}
1632/* END_CASE */
toth92ga41954d2021-02-12 16:11:17 +01001633
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001634/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001635void x509_crt_parse_subjectkeyid(char *file, data_t *subjectKeyId, int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001636{
1637 mbedtls_x509_crt crt;
toth92ga41954d2021-02-12 16:11:17 +01001638
1639 mbedtls_x509_crt_init(&crt);
1640
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001641 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001642
toth92g357b2972021-05-04 15:41:35 +02001643 if (ref_ret == 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001644 TEST_EQUAL(crt.subject_key_id.tag, MBEDTLS_ASN1_OCTET_STRING);
1645 TEST_EQUAL(memcmp(crt.subject_key_id.p, subjectKeyId->x, subjectKeyId->len), 0);
1646 TEST_EQUAL(crt.subject_key_id.len, subjectKeyId->len);
toth92g357b2972021-05-04 15:41:35 +02001647 } else {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001648 TEST_EQUAL(crt.subject_key_id.tag, 0);
1649 TEST_EQUAL(crt.subject_key_id.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001650 }
1651
1652exit:
1653 mbedtls_x509_crt_free(&crt);
1654}
1655/* END_CASE */
1656
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001657/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Przemek Stekiel2568d472023-04-06 09:23:25 +02001658void x509_crt_parse_authoritykeyid(char *file,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001659 data_t *keyId,
toth92g5042b102021-05-06 08:22:17 +02001660 char *authorityKeyId_issuer,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001661 data_t *serial,
toth92g5042b102021-05-06 08:22:17 +02001662 int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001663{
1664 mbedtls_x509_crt crt;
Przemek Stekiel39dbe232023-04-03 10:19:22 +02001665 mbedtls_x509_subject_alternative_name san;
David Horstmann9a3a1a62023-06-22 16:59:09 +01001666 char name_buf[128];
toth92ga41954d2021-02-12 16:11:17 +01001667
1668 mbedtls_x509_crt_init(&crt);
1669
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001670 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001671
toth92g357b2972021-05-04 15:41:35 +02001672 if (ref_ret == 0) {
toth92ga41954d2021-02-12 16:11:17 +01001673 /* KeyId test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001674 if (keyId->len > 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001675 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, MBEDTLS_ASN1_OCTET_STRING);
1676 TEST_EQUAL(memcmp(crt.authority_key_id.keyIdentifier.p, keyId->x, keyId->len), 0);
1677 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, keyId->len);
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001678 } else {
1679 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1680 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001681 }
toth92ga41954d2021-02-12 16:11:17 +01001682
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001683
toth92ga41954d2021-02-12 16:11:17 +01001684 /* Issuer test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001685 if (strlen(authorityKeyId_issuer) > 0) {
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001686 mbedtls_x509_sequence *issuerPtr = &crt.authority_key_id.authorityCertIssuer;
Przemek Stekiel01984212023-02-09 09:29:34 +01001687
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001688 TEST_EQUAL(mbedtls_x509_parse_subject_alt_name(&issuerPtr->buf, &san), 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001689
David Horstmann9a3a1a62023-06-22 16:59:09 +01001690 TEST_ASSERT(mbedtls_x509_dn_gets(name_buf, sizeof(name_buf),
1691 &san.san.directory_name)
1692 > 0);
1693 TEST_EQUAL(strcmp(name_buf, authorityKeyId_issuer), 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001694
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001695 mbedtls_x509_free_subject_alt_name(&san);
toth92ga41954d2021-02-12 16:11:17 +01001696 }
1697
1698 /* Serial test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001699 if (serial->len > 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001700 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001701 MBEDTLS_ASN1_INTEGER);
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001702 TEST_EQUAL(memcmp(crt.authority_key_id.authorityCertSerialNumber.p,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001703 serial->x, serial->len), 0);
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001704 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, serial->len);
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001705 } else {
1706 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1707 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001708 }
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001709
toth92g357b2972021-05-04 15:41:35 +02001710 } else {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001711 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1712 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001713
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001714 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1715 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001716 }
1717
1718exit:
1719 mbedtls_x509_crt_free(&crt);
1720}
1721/* END_CASE */