blob: b017e6854ef6b1e5a078b32ceaa8b1abc5f3a997 [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 Horstmann441b66c2024-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
Michael Schustera3cc4632024-06-07 01:51:54 +020063#if defined(MBEDTLS_X509_CRT_PARSE_C)
64
65#if defined(MBEDTLS_FS_IO)
Michael Schuster54300d42024-06-04 02:30:22 +020066static int verify_none(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000067{
Paul Bakker5a624082011-01-18 16:31:52 +000068 ((void) data);
69 ((void) crt);
70 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010071 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020072
Paul Bakker915275b2012-09-28 07:10:55 +000073 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000074}
75
Michael Schuster54300d42024-06-04 02:30:22 +020076static int verify_all(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000077{
Paul Bakker5a624082011-01-18 16:31:52 +000078 ((void) data);
79 ((void) crt);
80 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000081 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000082
Paul Bakkerb63b0af2011-01-13 17:54:59 +000083 return 0;
84}
85
Michael Schustera3cc4632024-06-07 01:51:54 +020086#if defined(MBEDTLS_X509_CRL_PARSE_C) && \
87 defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Michael Schuster54300d42024-06-04 02:30:22 +020088static int ca_callback_fail(void *data, mbedtls_x509_crt const *child, mbedtls_x509_crt **candidates)
Jarno Lamsa557426a2019-03-27 17:08:29 +020089{
90 ((void) data);
91 ((void) child);
92 ((void) candidates);
93
94 return -1;
95}
Michael Schustera3cc4632024-06-07 01:51:54 +020096
Michael Schuster54300d42024-06-04 02:30:22 +020097static int ca_callback(void *data, mbedtls_x509_crt const *child,
Michael Schusterbd89b792024-06-04 02:41:10 +020098 mbedtls_x509_crt **candidates)
Jarno Lamsa03cd1202019-03-27 15:45:04 +020099{
Hanno Beckercbb59032019-03-28 14:14:22 +0000100 int ret = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200101 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +0000102 mbedtls_x509_crt *first;
103
104 /* This is a test-only implementation of the CA callback
105 * which always returns the entire list of trusted certificates.
106 * Production implementations managing a large number of CAs
107 * should use an efficient presentation and lookup for the
108 * set of trusted certificates (such as a hashtable) and only
109 * return those trusted certificates which satisfy basic
110 * parental checks, such as the matching of child `Issuer`
111 * and parent `Subject` field. */
112 ((void) child);
113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
115 if (first == NULL) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000116 ret = -1;
117 goto exit;
118 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 mbedtls_x509_crt_init(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000122 ret = -1;
123 goto exit;
124 }
125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 while (ca->next != NULL) {
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200127 ca = ca->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000129 ret = -1;
130 goto exit;
131 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200132 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000133
134exit:
135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 if (ret != 0) {
137 mbedtls_x509_crt_free(first);
138 mbedtls_free(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000139 first = NULL;
140 }
141
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200142 *candidates = first;
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 return ret;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200144}
Michael Schustera3cc4632024-06-07 01:51:54 +0200145#endif /* MBEDTLS_X509_CRL_PARSE_C && MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200146
Michael Schuster54300d42024-06-04 02:30:22 +0200147static int verify_fatal(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200148{
149 int *levels = (int *) data;
150
151 ((void) crt);
152 ((void) certificate_depth);
153
154 /* Simulate a fatal error in the callback */
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 if (*levels & (1 << certificate_depth)) {
156 *flags |= (1 << certificate_depth);
157 return -1 - certificate_depth;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200158 }
159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return 0;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200161}
162
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900163/* strsep() not available on Windows */
Michael Schuster54300d42024-06-04 02:30:22 +0200164static char *mystrsep(char **stringp, const char *delim)
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900165{
166 const char *p;
167 char *ret = *stringp;
168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 if (*stringp == NULL) {
170 return NULL;
171 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 for (;; (*stringp)++) {
174 if (**stringp == '\0') {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900175 *stringp = NULL;
176 goto done;
177 }
178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 for (p = delim; *p != '\0'; p++) {
180 if (**stringp == *p) {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900181 **stringp = '\0';
182 (*stringp)++;
183 goto done;
184 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900186 }
187
188done:
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return ret;
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900190}
191
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200192typedef struct {
193 char buf[512];
194 char *p;
195} verify_print_context;
196
Michael Schuster54300d42024-06-04 02:30:22 +0200197static void verify_print_init(verify_print_context *ctx)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200198{
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 memset(ctx, 0, sizeof(verify_print_context));
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200200 ctx->p = ctx->buf;
201}
202
Michael Schuster54300d42024-06-04 02:30:22 +0200203static int verify_print(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200204{
205 int ret;
206 verify_print_context *ctx = (verify_print_context *) data;
207 char *p = ctx->p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 size_t n = ctx->buf + sizeof(ctx->buf) - ctx->p;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200209 ((void) flags);
210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 ret = mbedtls_snprintf(p, n, "depth %d - serial ", certificate_depth);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200212 MBEDTLS_X509_SAFE_SNPRINTF;
213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200215 MBEDTLS_X509_SAFE_SNPRINTF;
216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 ret = mbedtls_snprintf(p, n, " - subject ");
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200218 MBEDTLS_X509_SAFE_SNPRINTF;
219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200221 MBEDTLS_X509_SAFE_SNPRINTF;
222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 ret = mbedtls_snprintf(p, n, " - flags 0x%08x\n", *flags);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200224 MBEDTLS_X509_SAFE_SNPRINTF;
225
226 ctx->p = p;
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return 0;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200229}
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200230
Michael Schuster54300d42024-06-04 02:30:22 +0200231static int verify_parse_san(mbedtls_x509_subject_alternative_name *san,
Michael Schusterbd89b792024-06-04 02:41:10 +0200232 char **buf, size_t *size)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200233{
234 int ret;
235 size_t i;
236 char *p = *buf;
237 size_t n = *size;
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 ret = mbedtls_snprintf(p, n, "type : %d", san->type);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200240 MBEDTLS_X509_SAFE_SNPRINTF;
241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 switch (san->type) {
243 case (MBEDTLS_X509_SAN_OTHER_NAME):
244 ret = mbedtls_snprintf(p, n, "\notherName :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300245 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
David Horstmanncfae6a12023-08-18 19:12:59 +0100248 &san->san.other_name.type_id) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 ret = mbedtls_snprintf(p, n, " hardware module name :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300250 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 ret = mbedtls_snprintf(p, n, " hardware type : ");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300252 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 ret = mbedtls_oid_get_numeric_string(p,
255 n,
Matthias Schulzedc32ea2023-10-19 16:09:08 +0200256 &san->san.other_name.value.hardware_module_name
257 .oid);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300258 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 ret = mbedtls_snprintf(p, n, ", hardware serial number : ");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300261 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 for (i = 0; i < san->san.other_name.value.hardware_module_name.val.len; i++) {
264 ret = mbedtls_snprintf(p,
265 n,
266 "%02X",
267 san->san.other_name.value.hardware_module_name.val.p[i]);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300268 MBEDTLS_X509_SAFE_SNPRINTF;
269 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200270 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 break;/* MBEDTLS_OID_ON_HW_MODULE_NAME */
272 case (MBEDTLS_X509_SAN_DNS_NAME):
273 ret = mbedtls_snprintf(p, n, "\ndNSName : ");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200274 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if (san->san.unstructured_name.len >= n) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200276 *p = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200278 }
279 n -= san->san.unstructured_name.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 for (i = 0; i < san->san.unstructured_name.len; i++) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200281 *p++ = san->san.unstructured_name.p[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 }
283 break;/* MBEDTLS_X509_SAN_DNS_NAME */
Przemek Stekiel608e3ef2023-02-09 14:47:50 +0100284 case (MBEDTLS_X509_SAN_RFC822_NAME):
285 ret = mbedtls_snprintf(p, n, "\nrfc822Name : ");
286 MBEDTLS_X509_SAFE_SNPRINTF;
287 if (san->san.unstructured_name.len >= n) {
288 *p = '\0';
289 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
290 }
291 n -= san->san.unstructured_name.len;
292 for (i = 0; i < san->san.unstructured_name.len; i++) {
293 *p++ = san->san.unstructured_name.p[i];
294 }
295 break;/* MBEDTLS_X509_SAN_RFC822_NAME */
Andrzej Kureke12b01d2023-01-10 06:47:38 -0500296 case (MBEDTLS_X509_SAN_DIRECTORY_NAME):
297 ret = mbedtls_snprintf(p, n, "\ndirectoryName : ");
298 MBEDTLS_X509_SAFE_SNPRINTF;
299 ret = mbedtls_x509_dn_gets(p, n, &san->san.directory_name);
300 if (ret < 0) {
301 return ret;
302 }
303
304 p += ret;
305 n -= ret;
306 break;/* MBEDTLS_X509_SAN_DIRECTORY_NAME */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200307 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 /*
309 * Should not happen.
310 */
311 return -1;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200312 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 ret = mbedtls_snprintf(p, n, "\n");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200314 MBEDTLS_X509_SAFE_SNPRINTF;
315
316 *size = n;
317 *buf = p;
318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200320}
Michael Schustera3cc4632024-06-07 01:51:54 +0200321#endif /* MBEDTLS_FS_IO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200322
Michael Schuster54300d42024-06-04 02:30:22 +0200323static int parse_crt_ext_cb(void *p_ctx, mbedtls_x509_crt const *crt, mbedtls_x509_buf const *oid,
Michael Schusterbd89b792024-06-04 02:41:10 +0200324 int critical, const unsigned char *cp, const unsigned char *end)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200325{
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 (void) crt;
327 (void) critical;
328 mbedtls_x509_buf *new_oid = (mbedtls_x509_buf *) p_ctx;
329 if (oid->tag == MBEDTLS_ASN1_OID &&
330 MBEDTLS_OID_CMP(MBEDTLS_OID_CERTIFICATE_POLICIES, oid) == 0) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200331 /* Handle unknown certificate policy */
332 int ret, parse_ret = 0;
333 size_t len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 unsigned char **p = (unsigned char **) &cp;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200335
336 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 ret = mbedtls_asn1_get_tag(p, end, &len,
338 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
339 if (ret != 0) {
340 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
341 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 if (*p + len != end) {
344 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
345 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
346 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200347
348 /*
349 * Cannot be an empty sequence.
350 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if (len == 0) {
352 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
353 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
354 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 while (*p < end) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200357 const unsigned char *policy_end;
358
359 /*
360 * Get the policy sequence
361 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
363 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
364 0) {
365 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
366 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200367
368 policy_end = *p + len;
369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
371 MBEDTLS_ASN1_OID)) != 0) {
372 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
373 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200374
Nicola Di Lietob77fad82020-06-17 17:57:36 +0200375 /*
376 * Recognize exclusively the policy with OID 1
377 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if (len != 1 || *p[0] != 1) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200379 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200381
382 *p += len;
383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 /*
385 * If there is an optional qualifier, then *p < policy_end
386 * Check the Qualifier len to verify it doesn't exceed policy_end.
387 */
388 if (*p < policy_end) {
389 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
390 MBEDTLS_ASN1_CONSTRUCTED |
391 MBEDTLS_ASN1_SEQUENCE)) != 0) {
392 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
393 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200394 /*
395 * Skip the optional policy qualifiers.
396 */
397 *p += len;
398 }
399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (*p != policy_end) {
401 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
402 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
403 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200404 }
405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 if (*p != end) {
407 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
408 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
409 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 return parse_ret;
412 } else if (new_oid != NULL && new_oid->tag == oid->tag && new_oid->len == oid->len &&
413 memcmp(new_oid->p, oid->p, oid->len) == 0) {
414 return 0;
415 } else {
416 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
417 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200418 }
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200419}
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200420#endif /* MBEDTLS_X509_CRT_PARSE_C */
Matthias Schulzab408222023-10-18 13:20:59 +0200421
Michael Schustera3cc4632024-06-07 01:51:54 +0200422#if defined(MBEDTLS_X509_CSR_PARSE_C) && \
423 !defined(MBEDTLS_X509_REMOVE_INFO)
Michael Schuster54300d42024-06-04 02:30:22 +0200424static int parse_csr_ext_accept_cb(void *p_ctx, mbedtls_x509_csr const *csr, mbedtls_x509_buf const *oid,
Michael Schusterbd89b792024-06-04 02:41:10 +0200425 int critical, const unsigned char *cp, const unsigned char *end)
Matthias Schulzab408222023-10-18 13:20:59 +0200426{
427 (void) p_ctx;
428 (void) csr;
429 (void) oid;
430 (void) critical;
431 (void) cp;
432 (void) end;
433
434 return 0;
435}
436
Michael Schuster54300d42024-06-04 02:30:22 +0200437static int parse_csr_ext_reject_cb(void *p_ctx, mbedtls_x509_csr const *csr, mbedtls_x509_buf const *oid,
Michael Schusterbd89b792024-06-04 02:41:10 +0200438 int critical, const unsigned char *cp, const unsigned char *end)
Matthias Schulzab408222023-10-18 13:20:59 +0200439{
440 (void) p_ctx;
441 (void) csr;
442 (void) oid;
443 (void) critical;
444 (void) cp;
445 (void) end;
446
447 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
448 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
449}
Michael Schustera3cc4632024-06-07 01:51:54 +0200450#endif /* MBEDTLS_X509_CSR_PARSE_C && !MBEDTLS_X509_REMOVE_INFO */
Paul Bakker33b43f12013-08-20 11:48:36 +0200451/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000452
Thomas Daubney5c9c2ce2022-06-06 16:36:43 +0100453/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100454void x509_accessor_ext_types(int ext_type, int has_ext_type)
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100455{
456 mbedtls_x509_crt crt;
457 int expected_result = ext_type & has_ext_type;
458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200460 USE_PSA_INIT();
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100461
462 crt.ext_types = ext_type;
463
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500464 TEST_EQUAL(mbedtls_x509_crt_has_ext_type(&crt, has_ext_type), expected_result);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100465
valerio32f2ac92023-04-20 11:59:52 +0200466exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200468 USE_PSA_DONE();
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100469}
470/* END_CASE */
471
Glenn Strauss6f545ac2022-10-25 15:02:14 -0400472/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_TEST_HOOKS */
473void x509_crt_parse_cn_inet_pton(const char *cn, data_t *exp, int ref_ret)
474{
475 uint32_t addr[4];
476 size_t addrlen = mbedtls_x509_crt_parse_cn_inet_pton(cn, addr);
477 TEST_EQUAL(addrlen, (size_t) ref_ret);
478
479 if (addrlen) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100480 TEST_MEMORY_COMPARE(exp->x, exp->len, addr, addrlen);
Glenn Strauss6f545ac2022-10-25 15:02:14 -0400481 }
482}
483/* END_CASE */
484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500486void x509_parse_san(char *crt_file, char *result_str, int parse_result)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200487{
Ron Eldor890819a2019-05-13 19:03:04 +0300488 int ret;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200489 mbedtls_x509_crt crt;
Ron Eldor890819a2019-05-13 19:03:04 +0300490 mbedtls_x509_subject_alternative_name san;
491 mbedtls_x509_sequence *cur = NULL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200492 char buf[2000];
493 char *p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 size_t n = sizeof(buf);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200497 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 memset(buf, 0, 2000);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200499
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500500 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), parse_result);
Ron Eldor890819a2019-05-13 19:03:04 +0300501
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500502 if (parse_result != 0) {
503 goto exit;
504 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if (crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Ron Eldor890819a2019-05-13 19:03:04 +0300506 cur = &crt.subject_alt_names;
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 while (cur != NULL) {
508 ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
509 TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
Ron Eldor890819a2019-05-13 19:03:04 +0300510 /*
511 * If san type not supported, ignore.
512 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 if (ret == 0) {
Andrzej Kurekd40c2b62023-02-13 07:01:59 -0500514 ret = verify_parse_san(&san, &p, &n);
515 mbedtls_x509_free_subject_alt_name(&san);
516 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 }
Ron Eldor890819a2019-05-13 19:03:04 +0300518 cur = cur->next;
519 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200520 }
521
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500522 TEST_EQUAL(strcmp(buf, result_str), 0);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200523
524exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200526 USE_PSA_DONE();
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200527}
528/* END_CASE */
529
Hanno Becker612a2f12020-10-09 09:19:39 +0100530/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100531void x509_cert_info(char *crt_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000532{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000534 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000535 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200538 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000540
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500541 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 res = mbedtls_x509_crt_info(buf, 2000, "", &crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 TEST_ASSERT(res != -1);
545 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000546
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500547 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200548
549exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200551 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000552}
Paul Bakker33b43f12013-08-20 11:48:36 +0200553/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000554
Hanno Becker612a2f12020-10-09 09:19:39 +0100555/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100556void mbedtls_x509_crl_info(char *crl_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000557{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000559 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000560 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200563 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000565
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500566 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 res = mbedtls_x509_crl_info(buf, 2000, "", &crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 TEST_ASSERT(res != -1);
570 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000571
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500572 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200573
574exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +0200576 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000577}
Paul Bakker33b43f12013-08-20 11:48:36 +0200578/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000579
Andres AGa39db392016-12-08 17:10:38 +0000580/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100581void mbedtls_x509_crl_parse(char *crl_file, int result)
Andres AGa39db392016-12-08 17:10:38 +0000582{
583 mbedtls_x509_crl crl;
584 char buf[2000];
585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200587 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 memset(buf, 0, 2000);
Andres AGa39db392016-12-08 17:10:38 +0000589
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500590 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), result);
Andres AGa39db392016-12-08 17:10:38 +0000591
592exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +0200594 USE_PSA_DONE();
Andres AGa39db392016-12-08 17:10:38 +0000595}
596/* END_CASE */
597
Hanno Becker612a2f12020-10-09 09:19:39 +0100598/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100599void mbedtls_x509_csr_info(char *csr_file, char *result_str)
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100600{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100602 char buf[2000];
603 int res;
604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +0200606 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 memset(buf, 0, 2000);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100608
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500609 TEST_EQUAL(mbedtls_x509_csr_parse_file(&csr, csr_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 res = mbedtls_x509_csr_info(buf, 2000, "", &csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 TEST_ASSERT(res != -1);
613 TEST_ASSERT(res != -2);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100614
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500615 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200616
617exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +0200619 USE_PSA_DONE();
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100620}
621/* END_CASE */
622
Hanno Becker612a2f12020-10-09 09:19:39 +0100623/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100624void x509_verify_info(int flags, char *prefix, char *result_str)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100625{
626 char buf[2000];
627 int res;
628
Valerio Setti569c1712023-04-19 14:53:36 +0200629 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 memset(buf, 0, sizeof(buf));
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 res = mbedtls_x509_crt_verify_info(buf, sizeof(buf), prefix, flags);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 TEST_ASSERT(res >= 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100635
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500636 TEST_EQUAL(strcmp(buf, result_str), 0);
valerio32f2ac92023-04-20 11:59:52 +0200637
638exit:
Valerio Setti569c1712023-04-19 14:53:36 +0200639 USE_PSA_DONE();
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100640}
641/* END_CASE */
642
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200643/* 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 +0100644void x509_verify_restart(char *crt_file, char *ca_file,
645 int result, int flags_result,
646 int max_ops, int min_restart, int max_restart)
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200647{
648 int ret, cnt_restart;
649 mbedtls_x509_crt_restart_ctx rs_ctx;
650 mbedtls_x509_crt crt;
651 mbedtls_x509_crt ca;
652 uint32_t flags = 0;
653
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200654 /*
655 * See comments on ecp_test_vect_restart() for op count precision.
656 *
Gilles Peskinee820c0a2023-08-03 17:45:20 +0200657 * For reference, with Mbed TLS 2.6 and default settings:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200658 * - ecdsa_verify() for P-256: ~ 6700
659 * - ecdsa_verify() for P-384: ~ 18800
660 * - x509_verify() for server5 -> test-ca2: ~ 18800
661 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
662 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 mbedtls_x509_crt_restart_init(&rs_ctx);
664 mbedtls_x509_crt_init(&crt);
665 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200666 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200667
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500668 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
669 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200670
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 mbedtls_ecp_set_max_ops(max_ops);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200672
673 cnt_restart = 0;
674 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
676 &mbedtls_x509_crt_profile_default, NULL, &flags,
677 NULL, NULL, &rs_ctx);
678 } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200679
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500680 TEST_EQUAL(ret, result);
681 TEST_EQUAL(flags, (uint32_t) flags_result);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 TEST_ASSERT(cnt_restart >= min_restart);
684 TEST_ASSERT(cnt_restart <= max_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200685
686 /* Do we leak memory when aborting? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
688 &mbedtls_x509_crt_profile_default, NULL, &flags,
689 NULL, NULL, &rs_ctx);
690 TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200691
692exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 mbedtls_x509_crt_restart_free(&rs_ctx);
694 mbedtls_x509_crt_free(&crt);
695 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100696 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200697}
698/* END_CASE */
699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100701void x509_verify(char *crt_file, char *ca_file, char *crl_file,
702 char *cn_name_str, int result, int flags_result,
703 char *profile_str,
704 char *verify_callback)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000705{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_x509_crt crt;
707 mbedtls_x509_crt ca;
708 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200709 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000710 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200711 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 char *cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200713 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 mbedtls_x509_crt_init(&crt);
716 mbedtls_x509_crt_init(&ca);
717 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200718 MD_OR_USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (strcmp(cn_name_str, "NULL") != 0) {
Paul Bakker33b43f12013-08-20 11:48:36 +0200721 cn_name = cn_name_str;
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (strcmp(profile_str, "") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200725 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 } else if (strcmp(profile_str, "next") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200727 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 } else if (strcmp(profile_str, "suite_b") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200729 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 } else if (strcmp(profile_str, "compat") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200731 profile = &compat_profile;
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 } else if (strcmp(profile_str, "all") == 0) {
Hanno Becker20a4ade2019-06-03 14:27:03 +0100733 profile = &profile_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100735 TEST_FAIL("Unknown algorithm profile");
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 }
Gilles Peskineef86ab22017-05-05 18:59:02 +0200737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 if (strcmp(verify_callback, "NULL") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200739 f_vrfy = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 } else if (strcmp(verify_callback, "verify_none") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200741 f_vrfy = verify_none;
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 } else if (strcmp(verify_callback, "verify_all") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200743 f_vrfy = verify_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100745 TEST_FAIL("No known verify callback selected");
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200747
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500748 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
749 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
750 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000751
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 res = mbedtls_x509_crt_verify_with_profile(&crt,
753 &ca,
754 &crl,
755 profile,
756 cn_name,
757 &flags,
758 f_vrfy,
759 NULL);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 TEST_EQUAL(res, result);
762 TEST_EQUAL(flags, (uint32_t) flags_result);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200763
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200764#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker0350d562019-03-28 14:23:36 +0000765 /* CRLs aren't supported with CA callbacks, so skip the CA callback
Jarno Lamsadfd22c42019-04-01 15:18:53 +0300766 * version of the test if CRLs are in use. */
Gilles Peskinebb7d92c2023-10-17 17:26:44 +0200767 if (strcmp(crl_file, "") == 0) {
Hanno Becker0350d562019-03-28 14:23:36 +0000768 flags = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 res = mbedtls_x509_crt_verify_with_ca_cb(&crt,
771 ca_callback,
772 &ca,
773 profile,
774 cn_name,
775 &flags,
776 f_vrfy,
777 NULL);
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200778
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500779 TEST_EQUAL(res, result);
780 TEST_EQUAL(flags, (uint32_t) (flags_result));
Hanno Becker0350d562019-03-28 14:23:36 +0000781 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200782#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200783exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 mbedtls_x509_crt_free(&crt);
785 mbedtls_x509_crt_free(&ca);
786 mbedtls_x509_crl_free(&crl);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100787 MD_OR_USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000788}
Paul Bakker33b43f12013-08-20 11:48:36 +0200789/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000790
Jarno Lamsa557426a2019-03-27 17:08:29 +0200791/* 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 +0100792void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
793 int exp_ret)
Jarno Lamsa557426a2019-03-27 17:08:29 +0200794{
795 int ret;
796 mbedtls_x509_crt crt;
797 mbedtls_x509_crt ca;
798 uint32_t flags = 0;
Hanno Becker3f932bb2019-03-28 17:06:47 +0000799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 mbedtls_x509_crt_init(&crt);
801 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200802 USE_PSA_INIT();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200803
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500804 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
805 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if (strcmp(name, "NULL") == 0) {
Jarno Lamsa557426a2019-03-27 17:08:29 +0200808 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca,
812 &compat_profile, name, &flags,
813 NULL, NULL);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200814
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500815 TEST_EQUAL(ret, exp_ret);
816 TEST_EQUAL(flags, (uint32_t) (-1));
Jarno Lamsa557426a2019-03-27 17:08:29 +0200817exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 mbedtls_x509_crt_free(&crt);
819 mbedtls_x509_crt_free(&ca);
Valerio Setti569c1712023-04-19 14:53:36 +0200820 USE_PSA_DONE();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200821}
822/* END_CASE */
823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825void x509_verify_callback(char *crt_file, char *ca_file, char *name,
826 int exp_ret, char *exp_vrfy_out)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200827{
828 int ret;
829 mbedtls_x509_crt crt;
830 mbedtls_x509_crt ca;
831 uint32_t flags = 0;
832 verify_print_context vrfy_ctx;
833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 mbedtls_x509_crt_init(&crt);
835 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200836 MD_OR_USE_PSA_INIT();
837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 verify_print_init(&vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200839
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500840 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
841 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 if (strcmp(name, "NULL") == 0) {
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200844 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 }
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL,
848 &compat_profile,
849 name, &flags,
850 verify_print, &vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200851
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500852 TEST_EQUAL(ret, exp_ret);
853 TEST_EQUAL(strcmp(vrfy_ctx.buf, exp_vrfy_out), 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200854
855exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 mbedtls_x509_crt_free(&crt);
857 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100858 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200859}
860/* END_CASE */
861
Hanno Becker612a2f12020-10-09 09:19:39 +0100862/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100863void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
864 char *new_subject_ou,
865 char *result_str,
866 int ret)
Werner Lewis31ecb962022-06-17 15:51:55 +0100867{
868 mbedtls_x509_crt crt;
869 char buf[2000];
870 int res = 0;
871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200873 USE_PSA_INIT();
874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 memset(buf, 0, 2000);
Werner Lewis31ecb962022-06-17 15:51:55 +0100876
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500877 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100878 crt.subject.next->val.p = (unsigned char *) new_subject_ou;
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 crt.subject.next->val.len = strlen(new_subject_ou);
Werner Lewis31ecb962022-06-17 15:51:55 +0100880
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
Werner Lewis31ecb962022-06-17 15:51:55 +0100882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 if (ret != 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500884 TEST_EQUAL(res, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 } else {
886 TEST_ASSERT(res != -1);
887 TEST_ASSERT(res != -2);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500888 TEST_EQUAL(strcmp(buf, result_str), 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100889 }
890exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200892 USE_PSA_DONE();
Werner Lewis31ecb962022-06-17 15:51:55 +0100893}
894/* END_CASE */
895
896/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100897void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000898{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000900 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200901 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200904 USE_PSA_INIT();
905
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000907
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500908 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 if (strcmp(entity, "subject") == 0) {
910 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
911 } else if (strcmp(entity, "issuer") == 0) {
912 res = mbedtls_x509_dn_gets(buf, 2000, &crt.issuer);
913 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100914 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 TEST_ASSERT(res != -1);
918 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000919
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500920 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200921
922exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200924 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000925}
Paul Bakker33b43f12013-08-20 11:48:36 +0200926/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000927
David Horstmanndb73d3b2022-10-04 16:49:16 +0100928/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100929void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
David Horstmanndb73d3b2022-10-04 16:49:16 +0100930{
valerioe50831c2023-04-20 14:48:19 +0200931 unsigned char *name = NULL;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100932 unsigned char *p;
933 size_t name_len;
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100934 mbedtls_x509_name head;
David Horstmann3cd67582022-10-17 17:59:10 +0100935 int ret;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100936
Valerio Setti569c1712023-04-19 14:53:36 +0200937 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 memset(&head, 0, sizeof(head));
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100939
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100941 p = name;
942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 ret = mbedtls_x509_get_name(&p, (name + name_len), &head);
944 if (ret == 0) {
945 mbedtls_asn1_free_named_data_list_shallow(head.next);
946 }
David Horstmanndb73d3b2022-10-04 16:49:16 +0100947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 TEST_EQUAL(ret, exp_ret);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100949
valerio32f2ac92023-04-20 11:59:52 +0200950exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 mbedtls_free(name);
Valerio Setti569c1712023-04-19 14:53:36 +0200952 USE_PSA_DONE();
David Horstmanndb73d3b2022-10-04 16:49:16 +0100953}
954/* END_CASE */
955
Werner Lewisb3acb052022-06-17 15:59:58 +0100956/* 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 +0100957void mbedtls_x509_dn_get_next(char *name_str,
958 int next_merged,
959 char *expected_oids,
960 int exp_count,
961 char *exp_dn_gets)
Werner Lewisb3acb052022-06-17 15:59:58 +0100962{
963 int ret = 0, i;
Werner Lewisac80a662022-06-23 11:58:02 +0100964 size_t len = 0, out_size;
Werner Lewisb3acb052022-06-17 15:59:58 +0100965 mbedtls_asn1_named_data *names = NULL;
Gilles Peskine21e46b32023-10-17 16:35:20 +0200966 mbedtls_x509_name parsed;
967 memset(&parsed, 0, sizeof(parsed));
968 mbedtls_x509_name *parsed_cur;
Werner Lewisac80a662022-06-23 11:58:02 +0100969 // Size of buf is maximum required for test cases
Gilles Peskinef2574202023-10-18 17:39:48 +0200970 unsigned char buf[80] = { 0 };
Gilles Peskine21e46b32023-10-17 16:35:20 +0200971 unsigned char *out = NULL;
972 unsigned char *c = buf + sizeof(buf);
Werner Lewisb3acb052022-06-17 15:59:58 +0100973 const char *short_name;
974
Valerio Setti569c1712023-04-19 14:53:36 +0200975 USE_PSA_INIT();
Gilles Peskine21e46b32023-10-17 16:35:20 +0200976
Werner Lewisac80a662022-06-23 11:58:02 +0100977 // Additional size required for trailing space
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 out_size = strlen(expected_oids) + 2;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100979 TEST_CALLOC(out, out_size);
Werner Lewisb3acb052022-06-17 15:59:58 +0100980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 TEST_EQUAL(mbedtls_x509_string_to_names(&names, name_str), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100982
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 ret = mbedtls_x509_write_names(&c, buf, names);
984 TEST_LE_S(0, ret);
Werner Lewisb3acb052022-06-17 15:59:58 +0100985
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
987 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
988 TEST_EQUAL(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100989
990 // Iterate over names and set next_merged nodes
991 parsed_cur = &parsed;
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 for (; next_merged != 0 && parsed_cur != NULL; next_merged = next_merged >> 1) {
Werner Lewis12657cd2022-06-20 11:47:57 +0100993 parsed_cur->next_merged = next_merged & 0x01;
Werner Lewisb3acb052022-06-17 15:59:58 +0100994 parsed_cur = parsed_cur->next;
995 }
996
997 // Iterate over RDN nodes and print OID of first element to buffer
998 parsed_cur = &parsed;
999 len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 for (i = 0; parsed_cur != NULL; i++) {
1001 TEST_EQUAL(mbedtls_oid_get_attr_short_name(&parsed_cur->oid,
1002 &short_name), 0);
1003 len += mbedtls_snprintf((char *) out + len, out_size - len, "%s ", short_name);
1004 parsed_cur = mbedtls_x509_dn_get_next(parsed_cur);
Werner Lewisb3acb052022-06-17 15:59:58 +01001005 }
1006 out[len-1] = 0;
1007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 TEST_EQUAL(exp_count, i);
1009 TEST_EQUAL(strcmp((char *) out, expected_oids), 0);
1010 mbedtls_free(out);
Werner Lewisac80a662022-06-23 11:58:02 +01001011 out = NULL;
Werner Lewisb3acb052022-06-17 15:59:58 +01001012
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 out_size = strlen(exp_dn_gets) + 1;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001014 TEST_CALLOC(out, out_size);
Werner Lewisac80a662022-06-23 11:58:02 +01001015
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 TEST_LE_S(0, mbedtls_x509_dn_gets((char *) out, out_size, &parsed));
1017 TEST_EQUAL(strcmp((char *) out, exp_dn_gets), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +01001018exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 mbedtls_free(out);
1020 mbedtls_asn1_free_named_data_list(&names);
1021 mbedtls_asn1_free_named_data_list_shallow(parsed.next);
Valerio Setti569c1712023-04-19 14:53:36 +02001022 USE_PSA_DONE();
Werner Lewisb3acb052022-06-17 15:59:58 +01001023}
Werner Lewisb3acb052022-06-17 15:59:58 +01001024/* END_CASE */
1025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001027void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
Paul Bakker37940d9f2009-07-10 22:38:58 +00001028{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +00001030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001032 USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +00001033
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001034 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 if (strcmp(entity, "valid_from") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001037 TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_from), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 } else if (strcmp(entity, "valid_to") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001039 TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_to), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +01001041 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001043
Paul Bakkerbd51b262014-07-10 15:26:12 +02001044exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001046 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +00001047}
Paul Bakker33b43f12013-08-20 11:48:36 +02001048/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +00001049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001051void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001052{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001056 USE_PSA_INIT();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001057
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001058 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 if (strcmp(entity, "valid_from") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001061 TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_from), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 } else if (strcmp(entity, "valid_to") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001063 TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_to), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +01001065 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001067
Paul Bakkerbd51b262014-07-10 15:26:12 +02001068exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001070 USE_PSA_DONE();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001071}
1072/* END_CASE */
1073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001075void x509parse_crt_file(char *crt_file, int result)
Paul Bakker5a5fa922014-09-26 14:53:04 +02001076{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +02001078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001080 USE_PSA_INIT();
Paul Bakker5a5fa922014-09-26 14:53:04 +02001081
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001082 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), result);
Paul Bakker5a5fa922014-09-26 14:53:04 +02001083
1084exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001086 USE_PSA_DONE();
Paul Bakker5a5fa922014-09-26 14:53:04 +02001087}
1088/* END_CASE */
1089
Minos Galanakisa83ada42024-01-19 10:59:07 +00001090/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
1091void mbedtls_x509_get_ca_istrue(char *crt_file, int result)
1092{
1093 mbedtls_x509_crt crt;
1094 mbedtls_x509_crt_init(&crt);
1095 USE_PSA_INIT();
1096
1097 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
1098 TEST_EQUAL(mbedtls_x509_crt_get_ca_istrue(&crt), result);
1099exit:
1100 mbedtls_x509_crt_free(&crt);
1101 USE_PSA_DONE();
1102}
1103/* END_CASE */
1104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001106void x509parse_crt(data_t *buf, char *result_str, int result)
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001107{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 mbedtls_x509_crt crt;
Hanno Becker612a2f12020-10-09 09:19:39 +01001109#if !defined(MBEDTLS_X509_REMOVE_INFO)
Hanno Beckerfff2d572020-10-09 09:45:19 +01001110 unsigned char output[2000] = { 0 };
Azim Khanf1aaec92017-05-30 14:23:15 +01001111 int res;
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001112#else
1113 ((void) result_str);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001114#endif
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001117 USE_PSA_INIT();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001118
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001119 TEST_EQUAL(mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len), result);
Hanno Becker612a2f12020-10-09 09:19:39 +01001120#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 if ((result) == 0) {
1122 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1123 TEST_ASSERT(res != -1);
1124 TEST_ASSERT(res != -2);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001125
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001126 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001127 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001129#endif
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 mbedtls_x509_crt_free(&crt);
1132 mbedtls_x509_crt_init(&crt);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001133
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001134 TEST_EQUAL(mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len), result);
Hanno Becker612a2f12020-10-09 09:19:39 +01001135#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 if ((result) == 0) {
1137 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakker33b43f12013-08-20 11:48:36 +02001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 TEST_ASSERT(res != -1);
1142 TEST_ASSERT(res != -2);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001143
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001144 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001145 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001147#endif /* !MBEDTLS_X509_REMOVE_INFO */
Paul Bakkerb08e6842012-02-11 18:43:20 +00001148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 mbedtls_x509_crt_free(&crt);
1150 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001151
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001152 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL, NULL),
1153 result);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001154#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 if ((result) == 0) {
1156 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 TEST_ASSERT(res != -1);
1159 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001160
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001161 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001162 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001164#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 mbedtls_x509_crt_free(&crt);
1167 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001168
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001169 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL, NULL),
1170 result);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001171#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 if ((result) == 0) {
1173 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 TEST_ASSERT(res != -1);
1176 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001177
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001178 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001179 }
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001180#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001181
1182exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001184 USE_PSA_DONE();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001185}
1186/* END_CASE */
1187
1188/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001189void x509parse_crt_cb(data_t *buf, char *result_str, int result)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001190{
1191 mbedtls_x509_crt crt;
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001192 mbedtls_x509_buf oid;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001193
1194#if !defined(MBEDTLS_X509_REMOVE_INFO)
1195 unsigned char output[2000] = { 0 };
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001196 int res;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001197#else
1198 ((void) result_str);
1199#endif
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001200
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001201 oid.tag = MBEDTLS_ASN1_OID;
1202 oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001206 USE_PSA_INIT();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001207
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001208 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
1209 &oid), result);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001210#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 if ((result) == 0) {
1212 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001213
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 TEST_ASSERT(res != -1);
1215 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001216
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001217 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001218 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 memset(output, 0, 2000);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001220#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 mbedtls_x509_crt_free(&crt);
1223 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001224
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001225 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1226 &oid), (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001227#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 if ((result) == 0) {
1229 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 TEST_ASSERT(res != -1);
1232 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001233
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001234 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001235 }
Hanno Beckerfff2d572020-10-09 09:45:19 +01001236#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001237
Paul Bakkerbd51b262014-07-10 15:26:12 +02001238exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001240 USE_PSA_DONE();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001241}
Paul Bakker33b43f12013-08-20 11:48:36 +02001242/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001243
Hanno Becker612a2f12020-10-09 09:19:39 +01001244/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001245void x509parse_crl(data_t *buf, char *result_str, int result)
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001246{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001248 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +01001249 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +02001252 USE_PSA_INIT();
1253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 memset(output, 0, 2000);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001255
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001256
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001257 TEST_EQUAL(mbedtls_x509_crl_parse(&crl, buf->x, buf->len), (result));
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 if ((result) == 0) {
1259 res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
Paul Bakker33b43f12013-08-20 11:48:36 +02001260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 TEST_ASSERT(res != -1);
1262 TEST_ASSERT(res != -2);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001263
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001264 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001265 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001266
Paul Bakkerbd51b262014-07-10 15:26:12 +02001267exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +02001269 USE_PSA_DONE();
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001270}
Paul Bakker33b43f12013-08-20 11:48:36 +02001271/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001272
Hanno Becker612a2f12020-10-09 09:19:39 +01001273/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001274void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001275{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001277 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001278 int my_ret;
1279
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001281 USE_PSA_INIT();
1282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 memset(my_out, 0, sizeof(my_out));
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001286 TEST_EQUAL(my_ret, ref_ret);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 if (ref_ret == 0) {
1289 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001290 TEST_EQUAL(my_out_len, strlen(ref_out));
1291 TEST_EQUAL(strcmp(my_out, ref_out), 0);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001292 }
1293
Paul Bakkerbd51b262014-07-10 15:26:12 +02001294exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001296 USE_PSA_DONE();
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001297}
1298/* END_CASE */
1299
Matthias Schulzab408222023-10-18 13:20:59 +02001300/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1301void mbedtls_x509_csr_parse_with_ext_cb(data_t *csr_der, char *ref_out, int ref_ret, int accept)
1302{
1303 mbedtls_x509_csr csr;
1304 char my_out[1000];
1305 int my_ret;
1306
1307 mbedtls_x509_csr_init(&csr);
1308 USE_PSA_INIT();
1309
1310 memset(my_out, 0, sizeof(my_out));
1311
1312 my_ret = mbedtls_x509_csr_parse_der_with_ext_cb(&csr, csr_der->x, csr_der->len,
1313 accept ? parse_csr_ext_accept_cb :
Matthias Schulzedc32ea2023-10-19 16:09:08 +02001314 parse_csr_ext_reject_cb,
Matthias Schulzab408222023-10-18 13:20:59 +02001315 NULL);
1316 TEST_EQUAL(my_ret, ref_ret);
1317
1318 if (ref_ret == 0) {
1319 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1320 TEST_EQUAL(my_out_len, strlen(ref_out));
1321 TEST_EQUAL(strcmp(my_out, ref_out), 0);
1322 }
1323
1324exit:
1325 mbedtls_x509_csr_free(&csr);
1326 USE_PSA_DONE();
1327}
1328/* END_CASE */
1329
Przemek Stekield7992df2023-01-25 16:19:50 +01001330/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1331void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
1332{
1333 mbedtls_x509_csr csr;
1334 char my_out[1000];
1335 int my_ret;
1336
1337 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001338 USE_PSA_INIT();
1339
Przemek Stekield7992df2023-01-25 16:19:50 +01001340 memset(my_out, 0, sizeof(my_out));
1341
1342 my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001343 TEST_EQUAL(my_ret, ref_ret);
Przemek Stekield7992df2023-01-25 16:19:50 +01001344
1345 if (ref_ret == 0) {
1346 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001347 TEST_EQUAL(my_out_len, strlen(ref_out));
1348 TEST_EQUAL(strcmp(my_out, ref_out), 0);
Przemek Stekield7992df2023-01-25 16:19:50 +01001349 }
1350
1351exit:
1352 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001353 USE_PSA_DONE();
Przemek Stekield7992df2023-01-25 16:19:50 +01001354}
1355/* END_CASE */
1356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1e5fec62023-04-13 18:13:48 +02001358void mbedtls_x509_crt_parse_file(char *crt_path, int ret, int nb_crt)
1359{
1360 mbedtls_x509_crt chain, *cur;
1361 int i;
1362
1363 mbedtls_x509_crt_init(&chain);
1364 USE_PSA_INIT();
1365
1366 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, crt_path), ret);
1367
1368 /* Check how many certs we got */
1369 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1370 if (cur->raw.p != NULL) {
1371 i++;
1372 }
1373 }
1374
1375 TEST_EQUAL(i, nb_crt);
1376
1377exit:
1378 mbedtls_x509_crt_free(&chain);
1379 USE_PSA_DONE();
1380}
1381/* END_CASE */
1382
1383/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001384void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001385{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001387 int i;
1388
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001390 USE_PSA_INIT();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001391
Gilles Peskine55ad28a2023-04-13 18:14:45 +02001392 TEST_EQUAL(mbedtls_x509_crt_parse_path(&chain, crt_path), ret);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001393
1394 /* Check how many certs we got */
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1396 if (cur->raw.p != NULL) {
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001397 i++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 }
1399 }
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001400
Gilles Peskine55ad28a2023-04-13 18:14:45 +02001401 TEST_EQUAL(i, nb_crt);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001402
Paul Bakkerbd51b262014-07-10 15:26:12 +02001403exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 mbedtls_x509_crt_free(&chain);
Valerio Setti569c1712023-04-19 14:53:36 +02001405 USE_PSA_DONE();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001406}
1407/* END_CASE */
1408
Janos Follath822b2c32015-10-11 10:25:22 +02001409/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001410void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1411 int ret_chk, int flags_chk)
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001412{
1413 char file_buf[128];
1414 int ret;
1415 uint32_t flags;
1416 mbedtls_x509_crt trusted, chain;
1417
1418 /*
1419 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1420 * with NN.crt signed by NN-1.crt
1421 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 mbedtls_x509_crt_init(&trusted);
1423 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001424 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001425
1426 /* Load trusted root */
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001427 TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, ca_file), 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001428
1429 /* Load a chain with nb_int intermediates (from 01 to nb_int),
1430 * plus one "end-entity" cert (nb_int + 1) */
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001431 ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 nb_int + 1);
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001433 TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001434 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, file_buf), 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001435
1436 /* Try to verify that chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1438 NULL, NULL);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001439 TEST_EQUAL(ret, ret_chk);
1440 TEST_EQUAL(flags, (uint32_t) flags_chk);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001441
1442exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 mbedtls_x509_crt_free(&chain);
1444 mbedtls_x509_crt_free(&trusted);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001445 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001446}
1447/* END_CASE */
1448
1449/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001450void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1451 int flags_result, int result,
1452 char *profile_name, int vrfy_fatal_lvls)
Janos Follath822b2c32015-10-11 10:25:22 +02001453{
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 char *act;
Janos Follath822b2c32015-10-11 10:25:22 +02001455 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001456 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001457 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001458 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +02001459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 mbedtls_x509_crt_init(&chain);
1461 mbedtls_x509_crt_init(&trusted);
valerio32f2ac92023-04-20 11:59:52 +02001462 MD_OR_USE_PSA_INIT();
Janos Follath822b2c32015-10-11 10:25:22 +02001463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 while ((act = mystrsep(&chain_paths, " ")) != NULL) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001465 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, act), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 }
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001467 TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, trusted_ca), 0);
Janos Follath822b2c32015-10-11 10:25:22 +02001468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 if (strcmp(profile_name, "") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001470 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 } else if (strcmp(profile_name, "next") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001472 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 } else if (strcmp(profile_name, "suiteb") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001474 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 } else if (strcmp(profile_name, "rsa3072") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001476 profile = &profile_rsa3072;
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 } else if (strcmp(profile_name, "sha512") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001478 profile = &profile_sha512;
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 }
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001480
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1482 NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
Janos Follathef4f2582015-10-11 16:17:27 +02001483
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001484 TEST_EQUAL(res, (result));
1485 TEST_EQUAL(flags, (uint32_t) (flags_result));
Janos Follath822b2c32015-10-11 10:25:22 +02001486
1487exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 mbedtls_x509_crt_free(&trusted);
1489 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001490 MD_OR_USE_PSA_DONE();
Janos Follath822b2c32015-10-11 10:25:22 +02001491}
1492/* END_CASE */
1493
Hanno Becker612a2f12020-10-09 09:19:39 +01001494/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001495void x509_oid_desc(data_t *buf, char *ref_desc)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001496{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001498 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001499 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001500
Valerio Setti569c1712023-04-19 14:53:36 +02001501 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001504 oid.p = buf->x;
1505 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001506
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 ret = mbedtls_oid_get_extended_key_usage(&oid, &desc);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 if (strcmp(ref_desc, "notfound") == 0) {
1510 TEST_ASSERT(ret != 0);
1511 TEST_ASSERT(desc == NULL);
1512 } else {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001513 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 TEST_ASSERT(desc != NULL);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001515 TEST_EQUAL(strcmp(desc, ref_desc), 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001516 }
valerio32f2ac92023-04-20 11:59:52 +02001517
1518exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001519 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001520}
1521/* END_CASE */
1522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001524void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001525{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001527 char num_buf[100];
1528
Valerio Setti569c1712023-04-19 14:53:36 +02001529 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001530
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001531 memset(num_buf, 0x2a, sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001534 oid.p = oid_buf->x;
1535 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001536
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001537 TEST_ASSERT((size_t) blen <= sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001538
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001539 TEST_EQUAL(mbedtls_oid_get_numeric_string(num_buf, blen, &oid), ret);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 if (ret >= 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001542 TEST_EQUAL(num_buf[ret], 0);
1543 TEST_EQUAL(strcmp(num_buf, numstr), 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001544 }
valerio32f2ac92023-04-20 11:59:52 +02001545
1546exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001547 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001548}
1549/* END_CASE */
1550
TRodziewicz442fdc22021-06-07 13:52:23 +02001551/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001552void x509_check_key_usage(char *crt_file, int usage, int ret)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001553{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001557 USE_PSA_INIT();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001558
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001559 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001560
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001561 TEST_EQUAL(mbedtls_x509_crt_check_key_usage(&crt, usage), ret);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001562
Paul Bakkerbd51b262014-07-10 15:26:12 +02001563exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001565 USE_PSA_DONE();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001566}
1567/* END_CASE */
1568
TRodziewicz442fdc22021-06-07 13:52:23 +02001569/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001570void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1571 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001572{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001574
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001576 USE_PSA_INIT();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001577
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001578 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001579
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001580 TEST_EQUAL(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x, oid->len),
1581 ret);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001582
Paul Bakkerbd51b262014-07-10 15:26:12 +02001583exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001585 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001586}
1587/* END_CASE */
1588
Andres AG4b76aec2016-09-23 13:16:02 +01001589/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001590void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1591 int day, int hour, int min, int sec)
Andres AG4b76aec2016-09-23 13:16:02 +01001592{
1593 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001594 unsigned char buf[21];
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 unsigned char *start = buf;
1596 unsigned char *end = buf;
Andres AG4b76aec2016-09-23 13:16:02 +01001597
Valerio Setti569c1712023-04-19 14:53:36 +02001598 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 memset(&time, 0x00, sizeof(time));
1600 *end = (unsigned char) tag; end++;
1601 *end = strlen(time_str);
1602 TEST_ASSERT(*end < 20);
Andres AG4b76aec2016-09-23 13:16:02 +01001603 end++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 memcpy(end, time_str, (size_t) *(end - 1));
Andres AG4b76aec2016-09-23 13:16:02 +01001605 end += *(end - 1);
1606
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001607 TEST_EQUAL(mbedtls_x509_get_time(&start, end, &time), ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 if (ret == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001609 TEST_EQUAL(year, time.year);
1610 TEST_EQUAL(mon, time.mon);
1611 TEST_EQUAL(day, time.day);
1612 TEST_EQUAL(hour, time.hour);
1613 TEST_EQUAL(min, time.min);
1614 TEST_EQUAL(sec, time.sec);
Andres AG4b76aec2016-09-23 13:16:02 +01001615 }
valerio32f2ac92023-04-20 11:59:52 +02001616exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001617 USE_PSA_DONE();
Andres AG4b76aec2016-09-23 13:16:02 +01001618}
1619/* END_CASE */
1620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001622void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1623 int ref_msg_md, int ref_mgf_md,
1624 int ref_salt_len, int ref_ret)
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001625{
1626 int my_ret;
Ronald Cronac6ae352020-06-26 14:33:03 +02001627 mbedtls_x509_buf buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001629 int my_salt_len;
1630
Valerio Setti569c1712023-04-19 14:53:36 +02001631 USE_PSA_INIT();
1632
Ronald Cronac6ae352020-06-26 14:33:03 +02001633 buf.p = params->x;
1634 buf.len = params->len;
1635 buf.tag = params_tag;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001636
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1638 &my_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001639
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001640 TEST_EQUAL(my_ret, ref_ret);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 if (ref_ret == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001643 TEST_EQUAL(my_msg_md, (mbedtls_md_type_t) ref_msg_md);
1644 TEST_EQUAL(my_mgf_md, (mbedtls_md_type_t) ref_mgf_md);
1645 TEST_EQUAL(my_salt_len, ref_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001646 }
1647
Paul Bakkerbd51b262014-07-10 15:26:12 +02001648exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001649 USE_PSA_DONE();
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001650}
1651/* END_CASE */
toth92ga41954d2021-02-12 16:11:17 +01001652
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001653/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001654void x509_crt_parse_subjectkeyid(char *file, data_t *subjectKeyId, int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001655{
1656 mbedtls_x509_crt crt;
toth92ga41954d2021-02-12 16:11:17 +01001657
1658 mbedtls_x509_crt_init(&crt);
1659
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001660 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001661
toth92g357b2972021-05-04 15:41:35 +02001662 if (ref_ret == 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001663 TEST_EQUAL(crt.subject_key_id.tag, MBEDTLS_ASN1_OCTET_STRING);
1664 TEST_EQUAL(memcmp(crt.subject_key_id.p, subjectKeyId->x, subjectKeyId->len), 0);
1665 TEST_EQUAL(crt.subject_key_id.len, subjectKeyId->len);
toth92g357b2972021-05-04 15:41:35 +02001666 } else {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001667 TEST_EQUAL(crt.subject_key_id.tag, 0);
1668 TEST_EQUAL(crt.subject_key_id.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001669 }
1670
1671exit:
1672 mbedtls_x509_crt_free(&crt);
1673}
1674/* END_CASE */
1675
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001676/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Przemek Stekiel2568d472023-04-06 09:23:25 +02001677void x509_crt_parse_authoritykeyid(char *file,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001678 data_t *keyId,
toth92g5042b102021-05-06 08:22:17 +02001679 char *authorityKeyId_issuer,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001680 data_t *serial,
toth92g5042b102021-05-06 08:22:17 +02001681 int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001682{
1683 mbedtls_x509_crt crt;
Przemek Stekiel39dbe232023-04-03 10:19:22 +02001684 mbedtls_x509_subject_alternative_name san;
David Horstmann9a3a1a62023-06-22 16:59:09 +01001685 char name_buf[128];
toth92ga41954d2021-02-12 16:11:17 +01001686
1687 mbedtls_x509_crt_init(&crt);
1688
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001689 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001690
toth92g357b2972021-05-04 15:41:35 +02001691 if (ref_ret == 0) {
toth92ga41954d2021-02-12 16:11:17 +01001692 /* KeyId test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001693 if (keyId->len > 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001694 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, MBEDTLS_ASN1_OCTET_STRING);
1695 TEST_EQUAL(memcmp(crt.authority_key_id.keyIdentifier.p, keyId->x, keyId->len), 0);
1696 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, keyId->len);
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001697 } else {
1698 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1699 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001700 }
toth92ga41954d2021-02-12 16:11:17 +01001701
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001702
toth92ga41954d2021-02-12 16:11:17 +01001703 /* Issuer test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001704 if (strlen(authorityKeyId_issuer) > 0) {
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001705 mbedtls_x509_sequence *issuerPtr = &crt.authority_key_id.authorityCertIssuer;
Przemek Stekiel01984212023-02-09 09:29:34 +01001706
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001707 TEST_EQUAL(mbedtls_x509_parse_subject_alt_name(&issuerPtr->buf, &san), 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001708
David Horstmann9a3a1a62023-06-22 16:59:09 +01001709 TEST_ASSERT(mbedtls_x509_dn_gets(name_buf, sizeof(name_buf),
1710 &san.san.directory_name)
1711 > 0);
1712 TEST_EQUAL(strcmp(name_buf, authorityKeyId_issuer), 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001713
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001714 mbedtls_x509_free_subject_alt_name(&san);
toth92ga41954d2021-02-12 16:11:17 +01001715 }
1716
1717 /* Serial test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001718 if (serial->len > 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001719 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001720 MBEDTLS_ASN1_INTEGER);
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001721 TEST_EQUAL(memcmp(crt.authority_key_id.authorityCertSerialNumber.p,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001722 serial->x, serial->len), 0);
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001723 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, serial->len);
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001724 } else {
1725 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1726 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001727 }
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001728
toth92g357b2972021-05-04 15:41:35 +02001729 } else {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001730 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1731 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001732
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001733 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1734 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001735 }
1736
1737exit:
1738 mbedtls_x509_crt_free(&crt);
1739}
1740/* END_CASE */