blob: ba71c8f80c3d7aa627587397218f883d65eb9e92 [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 \
David Horstmann7d0e5d22024-07-01 16:39:39 +010018 adapt the script framework/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
Minos Galanakisa83ada42024-01-19 10:59:07 +00001086/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
1087void mbedtls_x509_get_ca_istrue(char *crt_file, int result)
1088{
1089 mbedtls_x509_crt crt;
1090 mbedtls_x509_crt_init(&crt);
1091 USE_PSA_INIT();
1092
1093 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
1094 TEST_EQUAL(mbedtls_x509_crt_get_ca_istrue(&crt), result);
1095exit:
1096 mbedtls_x509_crt_free(&crt);
1097 USE_PSA_DONE();
1098}
1099/* END_CASE */
1100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001102void x509parse_crt(data_t *buf, char *result_str, int result)
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001103{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 mbedtls_x509_crt crt;
Hanno Becker612a2f12020-10-09 09:19:39 +01001105#if !defined(MBEDTLS_X509_REMOVE_INFO)
Hanno Beckerfff2d572020-10-09 09:45:19 +01001106 unsigned char output[2000] = { 0 };
Azim Khanf1aaec92017-05-30 14:23:15 +01001107 int res;
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001108#else
1109 ((void) result_str);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001110#endif
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001113 USE_PSA_INIT();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001114
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001115 TEST_EQUAL(mbedtls_x509_crt_parse_der(&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 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1119 TEST_ASSERT(res != -1);
1120 TEST_ASSERT(res != -2);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001121
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001122 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001123 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001125#endif
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 mbedtls_x509_crt_free(&crt);
1128 mbedtls_x509_crt_init(&crt);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001129
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001130 TEST_EQUAL(mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len), result);
Hanno Becker612a2f12020-10-09 09:19:39 +01001131#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 if ((result) == 0) {
1133 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakker33b43f12013-08-20 11:48:36 +02001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 TEST_ASSERT(res != -1);
1138 TEST_ASSERT(res != -2);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001139
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001140 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001141 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001143#endif /* !MBEDTLS_X509_REMOVE_INFO */
Paul Bakkerb08e6842012-02-11 18:43:20 +00001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 mbedtls_x509_crt_free(&crt);
1146 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001147
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001148 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL, NULL),
1149 result);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001150#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 if ((result) == 0) {
1152 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 TEST_ASSERT(res != -1);
1155 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001156
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001157 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001158 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001160#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 mbedtls_x509_crt_free(&crt);
1163 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001164
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001165 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL, NULL),
1166 result);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001167#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 if ((result) == 0) {
1169 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 TEST_ASSERT(res != -1);
1172 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001173
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001174 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001175 }
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001176#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001177
1178exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001180 USE_PSA_DONE();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001181}
1182/* END_CASE */
1183
1184/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001185void x509parse_crt_cb(data_t *buf, char *result_str, int result)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001186{
1187 mbedtls_x509_crt crt;
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001188 mbedtls_x509_buf oid;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001189
1190#if !defined(MBEDTLS_X509_REMOVE_INFO)
1191 unsigned char output[2000] = { 0 };
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001192 int res;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001193#else
1194 ((void) result_str);
1195#endif
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001196
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001197 oid.tag = MBEDTLS_ASN1_OID;
1198 oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001202 USE_PSA_INIT();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001203
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001204 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
1205 &oid), result);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001206#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if ((result) == 0) {
1208 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 TEST_ASSERT(res != -1);
1211 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001212
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001213 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001214 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 memset(output, 0, 2000);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001216#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 mbedtls_x509_crt_free(&crt);
1219 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001220
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001221 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1222 &oid), (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001223#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if ((result) == 0) {
1225 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 TEST_ASSERT(res != -1);
1228 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001229
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001230 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001231 }
Hanno Beckerfff2d572020-10-09 09:45:19 +01001232#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001233
Paul Bakkerbd51b262014-07-10 15:26:12 +02001234exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001236 USE_PSA_DONE();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001237}
Paul Bakker33b43f12013-08-20 11:48:36 +02001238/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001239
Hanno Becker612a2f12020-10-09 09:19:39 +01001240/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001241void x509parse_crl(data_t *buf, char *result_str, int result)
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001242{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001244 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +01001245 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +02001248 USE_PSA_INIT();
1249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 memset(output, 0, 2000);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001251
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001252
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001253 TEST_EQUAL(mbedtls_x509_crl_parse(&crl, buf->x, buf->len), (result));
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 if ((result) == 0) {
1255 res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
Paul Bakker33b43f12013-08-20 11:48:36 +02001256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 TEST_ASSERT(res != -1);
1258 TEST_ASSERT(res != -2);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001259
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001260 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001261 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001262
Paul Bakkerbd51b262014-07-10 15:26:12 +02001263exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +02001265 USE_PSA_DONE();
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001266}
Paul Bakker33b43f12013-08-20 11:48:36 +02001267/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001268
Hanno Becker612a2f12020-10-09 09:19:39 +01001269/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001270void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001271{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001273 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001274 int my_ret;
1275
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001277 USE_PSA_INIT();
1278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 memset(my_out, 0, sizeof(my_out));
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001282 TEST_EQUAL(my_ret, ref_ret);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 if (ref_ret == 0) {
1285 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001286 TEST_EQUAL(my_out_len, strlen(ref_out));
1287 TEST_EQUAL(strcmp(my_out, ref_out), 0);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001288 }
1289
Paul Bakkerbd51b262014-07-10 15:26:12 +02001290exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001292 USE_PSA_DONE();
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001293}
1294/* END_CASE */
1295
Matthias Schulzab408222023-10-18 13:20:59 +02001296/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1297void mbedtls_x509_csr_parse_with_ext_cb(data_t *csr_der, char *ref_out, int ref_ret, int accept)
1298{
1299 mbedtls_x509_csr csr;
1300 char my_out[1000];
1301 int my_ret;
1302
1303 mbedtls_x509_csr_init(&csr);
1304 USE_PSA_INIT();
1305
1306 memset(my_out, 0, sizeof(my_out));
1307
1308 my_ret = mbedtls_x509_csr_parse_der_with_ext_cb(&csr, csr_der->x, csr_der->len,
1309 accept ? parse_csr_ext_accept_cb :
Matthias Schulzedc32ea2023-10-19 16:09:08 +02001310 parse_csr_ext_reject_cb,
Matthias Schulzab408222023-10-18 13:20:59 +02001311 NULL);
1312 TEST_EQUAL(my_ret, ref_ret);
1313
1314 if (ref_ret == 0) {
1315 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1316 TEST_EQUAL(my_out_len, strlen(ref_out));
1317 TEST_EQUAL(strcmp(my_out, ref_out), 0);
1318 }
1319
1320exit:
1321 mbedtls_x509_csr_free(&csr);
1322 USE_PSA_DONE();
1323}
1324/* END_CASE */
1325
Przemek Stekield7992df2023-01-25 16:19:50 +01001326/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1327void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
1328{
1329 mbedtls_x509_csr csr;
1330 char my_out[1000];
1331 int my_ret;
1332
1333 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001334 USE_PSA_INIT();
1335
Przemek Stekield7992df2023-01-25 16:19:50 +01001336 memset(my_out, 0, sizeof(my_out));
1337
1338 my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001339 TEST_EQUAL(my_ret, ref_ret);
Przemek Stekield7992df2023-01-25 16:19:50 +01001340
1341 if (ref_ret == 0) {
1342 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001343 TEST_EQUAL(my_out_len, strlen(ref_out));
1344 TEST_EQUAL(strcmp(my_out, ref_out), 0);
Przemek Stekield7992df2023-01-25 16:19:50 +01001345 }
1346
1347exit:
1348 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001349 USE_PSA_DONE();
Przemek Stekield7992df2023-01-25 16:19:50 +01001350}
1351/* END_CASE */
1352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1e5fec62023-04-13 18:13:48 +02001354void mbedtls_x509_crt_parse_file(char *crt_path, int ret, int nb_crt)
1355{
1356 mbedtls_x509_crt chain, *cur;
1357 int i;
1358
1359 mbedtls_x509_crt_init(&chain);
1360 USE_PSA_INIT();
1361
1362 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, crt_path), ret);
1363
1364 /* Check how many certs we got */
1365 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1366 if (cur->raw.p != NULL) {
1367 i++;
1368 }
1369 }
1370
1371 TEST_EQUAL(i, nb_crt);
1372
1373exit:
1374 mbedtls_x509_crt_free(&chain);
1375 USE_PSA_DONE();
1376}
1377/* END_CASE */
1378
1379/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001380void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001381{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001383 int i;
1384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001386 USE_PSA_INIT();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001387
Gilles Peskine55ad28a2023-04-13 18:14:45 +02001388 TEST_EQUAL(mbedtls_x509_crt_parse_path(&chain, crt_path), ret);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001389
1390 /* Check how many certs we got */
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1392 if (cur->raw.p != NULL) {
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001393 i++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 }
1395 }
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001396
Gilles Peskine55ad28a2023-04-13 18:14:45 +02001397 TEST_EQUAL(i, nb_crt);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001398
Paul Bakkerbd51b262014-07-10 15:26:12 +02001399exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 mbedtls_x509_crt_free(&chain);
Valerio Setti569c1712023-04-19 14:53:36 +02001401 USE_PSA_DONE();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001402}
1403/* END_CASE */
1404
Janos Follath822b2c32015-10-11 10:25:22 +02001405/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001406void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1407 int ret_chk, int flags_chk)
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001408{
1409 char file_buf[128];
1410 int ret;
1411 uint32_t flags;
1412 mbedtls_x509_crt trusted, chain;
1413
1414 /*
1415 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1416 * with NN.crt signed by NN-1.crt
1417 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 mbedtls_x509_crt_init(&trusted);
1419 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001420 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001421
1422 /* Load trusted root */
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001423 TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, ca_file), 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001424
1425 /* Load a chain with nb_int intermediates (from 01 to nb_int),
1426 * plus one "end-entity" cert (nb_int + 1) */
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001427 ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 nb_int + 1);
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001429 TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001430 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, file_buf), 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001431
1432 /* Try to verify that chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1434 NULL, NULL);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001435 TEST_EQUAL(ret, ret_chk);
1436 TEST_EQUAL(flags, (uint32_t) flags_chk);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001437
1438exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 mbedtls_x509_crt_free(&chain);
1440 mbedtls_x509_crt_free(&trusted);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001441 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001442}
1443/* END_CASE */
1444
1445/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001446void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1447 int flags_result, int result,
1448 char *profile_name, int vrfy_fatal_lvls)
Janos Follath822b2c32015-10-11 10:25:22 +02001449{
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 char *act;
Janos Follath822b2c32015-10-11 10:25:22 +02001451 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001452 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001453 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001454 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +02001455
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 mbedtls_x509_crt_init(&chain);
1457 mbedtls_x509_crt_init(&trusted);
valerio32f2ac92023-04-20 11:59:52 +02001458 MD_OR_USE_PSA_INIT();
Janos Follath822b2c32015-10-11 10:25:22 +02001459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 while ((act = mystrsep(&chain_paths, " ")) != NULL) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001461 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, act), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 }
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001463 TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, trusted_ca), 0);
Janos Follath822b2c32015-10-11 10:25:22 +02001464
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 if (strcmp(profile_name, "") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001466 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 } else if (strcmp(profile_name, "next") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001468 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 } else if (strcmp(profile_name, "suiteb") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001470 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 } else if (strcmp(profile_name, "rsa3072") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001472 profile = &profile_rsa3072;
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 } else if (strcmp(profile_name, "sha512") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001474 profile = &profile_sha512;
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 }
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1478 NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
Janos Follathef4f2582015-10-11 16:17:27 +02001479
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001480 TEST_EQUAL(res, (result));
1481 TEST_EQUAL(flags, (uint32_t) (flags_result));
Janos Follath822b2c32015-10-11 10:25:22 +02001482
1483exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 mbedtls_x509_crt_free(&trusted);
1485 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001486 MD_OR_USE_PSA_DONE();
Janos Follath822b2c32015-10-11 10:25:22 +02001487}
1488/* END_CASE */
1489
Hanno Becker612a2f12020-10-09 09:19:39 +01001490/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001491void x509_oid_desc(data_t *buf, char *ref_desc)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001492{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001494 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001495 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001496
Valerio Setti569c1712023-04-19 14:53:36 +02001497 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001500 oid.p = buf->x;
1501 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 ret = mbedtls_oid_get_extended_key_usage(&oid, &desc);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001504
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 if (strcmp(ref_desc, "notfound") == 0) {
1506 TEST_ASSERT(ret != 0);
1507 TEST_ASSERT(desc == NULL);
1508 } else {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001509 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 TEST_ASSERT(desc != NULL);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001511 TEST_EQUAL(strcmp(desc, ref_desc), 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001512 }
valerio32f2ac92023-04-20 11:59:52 +02001513
1514exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001515 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001516}
1517/* END_CASE */
1518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001520void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001521{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001523 char num_buf[100];
1524
Valerio Setti569c1712023-04-19 14:53:36 +02001525 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001526
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001527 memset(num_buf, 0x2a, sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001530 oid.p = oid_buf->x;
1531 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001532
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001533 TEST_ASSERT((size_t) blen <= sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001534
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001535 TEST_EQUAL(mbedtls_oid_get_numeric_string(num_buf, blen, &oid), ret);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001536
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 if (ret >= 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001538 TEST_EQUAL(num_buf[ret], 0);
1539 TEST_EQUAL(strcmp(num_buf, numstr), 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001540 }
valerio32f2ac92023-04-20 11:59:52 +02001541
1542exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001543 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001544}
1545/* END_CASE */
1546
TRodziewicz442fdc22021-06-07 13:52:23 +02001547/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001548void x509_check_key_usage(char *crt_file, int usage, int ret)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001549{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001553 USE_PSA_INIT();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001554
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001555 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001556
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001557 TEST_EQUAL(mbedtls_x509_crt_check_key_usage(&crt, usage), ret);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001558
Paul Bakkerbd51b262014-07-10 15:26:12 +02001559exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001561 USE_PSA_DONE();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001562}
1563/* END_CASE */
1564
TRodziewicz442fdc22021-06-07 13:52:23 +02001565/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001566void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1567 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001568{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001570
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001572 USE_PSA_INIT();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001573
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001574 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001575
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001576 TEST_EQUAL(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x, oid->len),
1577 ret);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001578
Paul Bakkerbd51b262014-07-10 15:26:12 +02001579exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001581 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001582}
1583/* END_CASE */
1584
Andres AG4b76aec2016-09-23 13:16:02 +01001585/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001586void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1587 int day, int hour, int min, int sec)
Andres AG4b76aec2016-09-23 13:16:02 +01001588{
1589 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001590 unsigned char buf[21];
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 unsigned char *start = buf;
1592 unsigned char *end = buf;
Andres AG4b76aec2016-09-23 13:16:02 +01001593
Valerio Setti569c1712023-04-19 14:53:36 +02001594 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 memset(&time, 0x00, sizeof(time));
1596 *end = (unsigned char) tag; end++;
1597 *end = strlen(time_str);
1598 TEST_ASSERT(*end < 20);
Andres AG4b76aec2016-09-23 13:16:02 +01001599 end++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 memcpy(end, time_str, (size_t) *(end - 1));
Andres AG4b76aec2016-09-23 13:16:02 +01001601 end += *(end - 1);
1602
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001603 TEST_EQUAL(mbedtls_x509_get_time(&start, end, &time), ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 if (ret == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001605 TEST_EQUAL(year, time.year);
1606 TEST_EQUAL(mon, time.mon);
1607 TEST_EQUAL(day, time.day);
1608 TEST_EQUAL(hour, time.hour);
1609 TEST_EQUAL(min, time.min);
1610 TEST_EQUAL(sec, time.sec);
Andres AG4b76aec2016-09-23 13:16:02 +01001611 }
valerio32f2ac92023-04-20 11:59:52 +02001612exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001613 USE_PSA_DONE();
Andres AG4b76aec2016-09-23 13:16:02 +01001614}
1615/* END_CASE */
1616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001618void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1619 int ref_msg_md, int ref_mgf_md,
1620 int ref_salt_len, int ref_ret)
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001621{
1622 int my_ret;
Ronald Cronac6ae352020-06-26 14:33:03 +02001623 mbedtls_x509_buf buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001625 int my_salt_len;
1626
Valerio Setti569c1712023-04-19 14:53:36 +02001627 USE_PSA_INIT();
1628
Ronald Cronac6ae352020-06-26 14:33:03 +02001629 buf.p = params->x;
1630 buf.len = params->len;
1631 buf.tag = params_tag;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1634 &my_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001635
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001636 TEST_EQUAL(my_ret, ref_ret);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 if (ref_ret == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001639 TEST_EQUAL(my_msg_md, (mbedtls_md_type_t) ref_msg_md);
1640 TEST_EQUAL(my_mgf_md, (mbedtls_md_type_t) ref_mgf_md);
1641 TEST_EQUAL(my_salt_len, ref_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001642 }
1643
Paul Bakkerbd51b262014-07-10 15:26:12 +02001644exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001645 USE_PSA_DONE();
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001646}
1647/* END_CASE */
toth92ga41954d2021-02-12 16:11:17 +01001648
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001649/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001650void x509_crt_parse_subjectkeyid(char *file, data_t *subjectKeyId, int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001651{
1652 mbedtls_x509_crt crt;
toth92ga41954d2021-02-12 16:11:17 +01001653
1654 mbedtls_x509_crt_init(&crt);
1655
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001656 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001657
toth92g357b2972021-05-04 15:41:35 +02001658 if (ref_ret == 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001659 TEST_EQUAL(crt.subject_key_id.tag, MBEDTLS_ASN1_OCTET_STRING);
1660 TEST_EQUAL(memcmp(crt.subject_key_id.p, subjectKeyId->x, subjectKeyId->len), 0);
1661 TEST_EQUAL(crt.subject_key_id.len, subjectKeyId->len);
toth92g357b2972021-05-04 15:41:35 +02001662 } else {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001663 TEST_EQUAL(crt.subject_key_id.tag, 0);
1664 TEST_EQUAL(crt.subject_key_id.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001665 }
1666
1667exit:
1668 mbedtls_x509_crt_free(&crt);
1669}
1670/* END_CASE */
1671
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001672/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Przemek Stekiel2568d472023-04-06 09:23:25 +02001673void x509_crt_parse_authoritykeyid(char *file,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001674 data_t *keyId,
toth92g5042b102021-05-06 08:22:17 +02001675 char *authorityKeyId_issuer,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001676 data_t *serial,
toth92g5042b102021-05-06 08:22:17 +02001677 int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001678{
1679 mbedtls_x509_crt crt;
Przemek Stekiel39dbe232023-04-03 10:19:22 +02001680 mbedtls_x509_subject_alternative_name san;
David Horstmann9a3a1a62023-06-22 16:59:09 +01001681 char name_buf[128];
toth92ga41954d2021-02-12 16:11:17 +01001682
1683 mbedtls_x509_crt_init(&crt);
1684
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001685 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001686
toth92g357b2972021-05-04 15:41:35 +02001687 if (ref_ret == 0) {
toth92ga41954d2021-02-12 16:11:17 +01001688 /* KeyId test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001689 if (keyId->len > 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001690 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, MBEDTLS_ASN1_OCTET_STRING);
1691 TEST_EQUAL(memcmp(crt.authority_key_id.keyIdentifier.p, keyId->x, keyId->len), 0);
1692 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, keyId->len);
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001693 } else {
1694 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1695 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001696 }
toth92ga41954d2021-02-12 16:11:17 +01001697
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001698
toth92ga41954d2021-02-12 16:11:17 +01001699 /* Issuer test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001700 if (strlen(authorityKeyId_issuer) > 0) {
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001701 mbedtls_x509_sequence *issuerPtr = &crt.authority_key_id.authorityCertIssuer;
Przemek Stekiel01984212023-02-09 09:29:34 +01001702
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001703 TEST_EQUAL(mbedtls_x509_parse_subject_alt_name(&issuerPtr->buf, &san), 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001704
David Horstmann9a3a1a62023-06-22 16:59:09 +01001705 TEST_ASSERT(mbedtls_x509_dn_gets(name_buf, sizeof(name_buf),
1706 &san.san.directory_name)
1707 > 0);
1708 TEST_EQUAL(strcmp(name_buf, authorityKeyId_issuer), 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001709
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001710 mbedtls_x509_free_subject_alt_name(&san);
toth92ga41954d2021-02-12 16:11:17 +01001711 }
1712
1713 /* Serial test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001714 if (serial->len > 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001715 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001716 MBEDTLS_ASN1_INTEGER);
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001717 TEST_EQUAL(memcmp(crt.authority_key_id.authorityCertSerialNumber.p,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001718 serial->x, serial->len), 0);
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001719 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, serial->len);
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001720 } else {
1721 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1722 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001723 }
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001724
toth92g357b2972021-05-04 15:41:35 +02001725 } else {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001726 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1727 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001728
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001729 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1730 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001731 }
1732
1733exit:
1734 mbedtls_x509_crt_free(&crt);
1735}
1736/* END_CASE */