blob: 4f0605cd1c105efdb7bbae26fb9d1b83754420f2 [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"
Gilles Peskinecd4c0d72025-05-07 23:45:12 +02009#include "mbedtls/oid.h"
Gilles Peskine86a47f82025-05-07 20:20:12 +020010#include "x509_oid.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000011#include "mbedtls/base64.h"
Chris Jones9f7a6932021-04-14 12:12:09 +010012#include "mbedtls/error.h"
Valerio Setti178b5bd2023-02-13 10:04:28 +010013#include "mbedtls/pk.h"
Sam Berry4aee6a22024-07-19 15:00:41 +010014#include "mbedtls/asn1.h"
15#include "mbedtls/asn1write.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +010016#include "string.h"
Paul Bakkerb63b0af2011-01-13 17:54:59 +000017
Simon Butcher9e24b512017-07-28 12:15:13 +010018#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
Hanno Becker3b1422e2017-07-26 13:38:02 +010019#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
Gilles Peskine449bd832023-01-11 14:50:10 +010020 than the current threshold 19. To test larger values, please \
David Horstmann441b66c2024-07-01 16:39:39 +010021 adapt the script framework/data_files/dir-max/long.sh."
Hanno Becker3b1422e2017-07-26 13:38:02 +010022#endif
23
Hanno Becker20a4ade2019-06-03 14:27:03 +010024/* Test-only profile allowing all digests, PK algorithms, and curves. */
25const mbedtls_x509_crt_profile profile_all =
26{
27 0xFFFFFFFF, /* Any MD */
28 0xFFFFFFFF, /* Any PK alg */
29 0xFFFFFFFF, /* Any curve */
30 1024,
31};
32
Gilles Peskineef86ab22017-05-05 18:59:02 +020033/* Profile for backward compatibility. Allows SHA-1, unlike the default
34 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020035const mbedtls_x509_crt_profile compat_profile =
36{
Gilles Peskine449bd832023-01-11 14:50:10 +010037 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) |
38 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160) |
39 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
40 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
41 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
42 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubney28015e12022-04-21 08:12:59 +010043 0xFFFFFFFF, /* Any PK alg */
44 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020045 1024,
46};
47
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020048const mbedtls_x509_crt_profile profile_rsa3072 =
49{
Gilles Peskine449bd832023-01-11 14:50:10 +010050 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
51 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
52 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
53 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_RSA),
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020054 0,
55 3072,
56};
57
58const mbedtls_x509_crt_profile profile_sha512 =
59{
Gilles Peskine449bd832023-01-11 14:50:10 +010060 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubney28015e12022-04-21 08:12:59 +010061 0xFFFFFFFF, /* Any PK alg */
62 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020063 1024,
64};
65
Michael Schustera3cc4632024-06-07 01:51:54 +020066#if defined(MBEDTLS_X509_CRT_PARSE_C)
67
68#if defined(MBEDTLS_FS_IO)
Michael Schuster54300d42024-06-04 02:30:22 +020069static int verify_none(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000070{
Paul Bakker5a624082011-01-18 16:31:52 +000071 ((void) data);
72 ((void) crt);
73 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010074 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020075
Paul Bakker915275b2012-09-28 07:10:55 +000076 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000077}
78
Michael Schuster54300d42024-06-04 02:30:22 +020079static int verify_all(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000080{
Paul Bakker5a624082011-01-18 16:31:52 +000081 ((void) data);
82 ((void) crt);
83 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000084 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000085
Paul Bakkerb63b0af2011-01-13 17:54:59 +000086 return 0;
87}
88
Michael Schustera3cc4632024-06-07 01:51:54 +020089#if defined(MBEDTLS_X509_CRL_PARSE_C) && \
90 defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Michael Schusterf828f042024-06-07 06:47:31 +020091static int ca_callback_fail(void *data, mbedtls_x509_crt const *child,
92 mbedtls_x509_crt **candidates)
Jarno Lamsa557426a2019-03-27 17:08:29 +020093{
94 ((void) data);
95 ((void) child);
96 ((void) candidates);
97
98 return -1;
99}
Michael Schustera3cc4632024-06-07 01:51:54 +0200100
Michael Schuster54300d42024-06-04 02:30:22 +0200101static int ca_callback(void *data, mbedtls_x509_crt const *child,
Michael Schusterbd89b792024-06-04 02:41:10 +0200102 mbedtls_x509_crt **candidates)
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200103{
Hanno Beckercbb59032019-03-28 14:14:22 +0000104 int ret = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200105 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +0000106 mbedtls_x509_crt *first;
107
108 /* This is a test-only implementation of the CA callback
109 * which always returns the entire list of trusted certificates.
110 * Production implementations managing a large number of CAs
111 * should use an efficient presentation and lookup for the
112 * set of trusted certificates (such as a hashtable) and only
113 * return those trusted certificates which satisfy basic
114 * parental checks, such as the matching of child `Issuer`
115 * and parent `Subject` field. */
116 ((void) child);
117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
119 if (first == NULL) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000120 ret = -1;
121 goto exit;
122 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 mbedtls_x509_crt_init(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000126 ret = -1;
127 goto exit;
128 }
129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 while (ca->next != NULL) {
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200131 ca = ca->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000133 ret = -1;
134 goto exit;
135 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200136 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000137
138exit:
139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 if (ret != 0) {
141 mbedtls_x509_crt_free(first);
142 mbedtls_free(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000143 first = NULL;
144 }
145
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200146 *candidates = first;
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return ret;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200148}
Michael Schustera3cc4632024-06-07 01:51:54 +0200149#endif /* MBEDTLS_X509_CRL_PARSE_C && MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200150
Michael Schuster54300d42024-06-04 02:30:22 +0200151static int verify_fatal(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200152{
153 int *levels = (int *) data;
154
155 ((void) crt);
156 ((void) certificate_depth);
157
158 /* Simulate a fatal error in the callback */
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 if (*levels & (1 << certificate_depth)) {
160 *flags |= (1 << certificate_depth);
161 return -1 - certificate_depth;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200162 }
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return 0;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200165}
166
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900167/* strsep() not available on Windows */
Michael Schuster54300d42024-06-04 02:30:22 +0200168static char *mystrsep(char **stringp, const char *delim)
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900169{
170 const char *p;
171 char *ret = *stringp;
172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 if (*stringp == NULL) {
174 return NULL;
175 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 for (;; (*stringp)++) {
178 if (**stringp == '\0') {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900179 *stringp = NULL;
180 goto done;
181 }
182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 for (p = delim; *p != '\0'; p++) {
184 if (**stringp == *p) {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900185 **stringp = '\0';
186 (*stringp)++;
187 goto done;
188 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900190 }
191
192done:
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 return ret;
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900194}
195
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200196typedef struct {
197 char buf[512];
198 char *p;
199} verify_print_context;
200
Michael Schuster54300d42024-06-04 02:30:22 +0200201static void verify_print_init(verify_print_context *ctx)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200202{
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 memset(ctx, 0, sizeof(verify_print_context));
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200204 ctx->p = ctx->buf;
205}
206
Michael Schuster54300d42024-06-04 02:30:22 +0200207static int verify_print(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200208{
209 int ret;
210 verify_print_context *ctx = (verify_print_context *) data;
211 char *p = ctx->p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 size_t n = ctx->buf + sizeof(ctx->buf) - ctx->p;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200213 ((void) flags);
214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 ret = mbedtls_snprintf(p, n, "depth %d - serial ", certificate_depth);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200216 MBEDTLS_X509_SAFE_SNPRINTF;
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200219 MBEDTLS_X509_SAFE_SNPRINTF;
220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 ret = mbedtls_snprintf(p, n, " - subject ");
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200222 MBEDTLS_X509_SAFE_SNPRINTF;
223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200225 MBEDTLS_X509_SAFE_SNPRINTF;
226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 ret = mbedtls_snprintf(p, n, " - flags 0x%08x\n", *flags);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200228 MBEDTLS_X509_SAFE_SNPRINTF;
229
230 ctx->p = p;
231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 return 0;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200233}
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200234
Michael Schuster54300d42024-06-04 02:30:22 +0200235static int verify_parse_san(mbedtls_x509_subject_alternative_name *san,
Michael Schusterbd89b792024-06-04 02:41:10 +0200236 char **buf, size_t *size)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200237{
238 int ret;
239 size_t i;
240 char *p = *buf;
241 size_t n = *size;
242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 ret = mbedtls_snprintf(p, n, "type : %d", san->type);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200244 MBEDTLS_X509_SAFE_SNPRINTF;
245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 switch (san->type) {
247 case (MBEDTLS_X509_SAN_OTHER_NAME):
248 ret = mbedtls_snprintf(p, n, "\notherName :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300249 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
David Horstmanncfae6a12023-08-18 19:12:59 +0100252 &san->san.other_name.type_id) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 ret = mbedtls_snprintf(p, n, " hardware module name :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300254 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 ret = mbedtls_snprintf(p, n, " hardware type : ");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300256 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 ret = mbedtls_oid_get_numeric_string(p,
259 n,
Matthias Schulzedc32ea2023-10-19 16:09:08 +0200260 &san->san.other_name.value.hardware_module_name
261 .oid);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300262 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 ret = mbedtls_snprintf(p, n, ", hardware serial number : ");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300265 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 for (i = 0; i < san->san.other_name.value.hardware_module_name.val.len; i++) {
268 ret = mbedtls_snprintf(p,
269 n,
270 "%02X",
271 san->san.other_name.value.hardware_module_name.val.p[i]);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300272 MBEDTLS_X509_SAFE_SNPRINTF;
273 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200274 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 break;/* MBEDTLS_OID_ON_HW_MODULE_NAME */
276 case (MBEDTLS_X509_SAN_DNS_NAME):
277 ret = mbedtls_snprintf(p, n, "\ndNSName : ");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200278 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if (san->san.unstructured_name.len >= n) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200280 *p = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200282 }
283 n -= san->san.unstructured_name.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 for (i = 0; i < san->san.unstructured_name.len; i++) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200285 *p++ = san->san.unstructured_name.p[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 }
287 break;/* MBEDTLS_X509_SAN_DNS_NAME */
Przemek Stekiel608e3ef2023-02-09 14:47:50 +0100288 case (MBEDTLS_X509_SAN_RFC822_NAME):
289 ret = mbedtls_snprintf(p, n, "\nrfc822Name : ");
290 MBEDTLS_X509_SAFE_SNPRINTF;
291 if (san->san.unstructured_name.len >= n) {
292 *p = '\0';
293 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
294 }
295 n -= san->san.unstructured_name.len;
296 for (i = 0; i < san->san.unstructured_name.len; i++) {
297 *p++ = san->san.unstructured_name.p[i];
298 }
299 break;/* MBEDTLS_X509_SAN_RFC822_NAME */
Andrzej Kureke12b01d2023-01-10 06:47:38 -0500300 case (MBEDTLS_X509_SAN_DIRECTORY_NAME):
301 ret = mbedtls_snprintf(p, n, "\ndirectoryName : ");
302 MBEDTLS_X509_SAFE_SNPRINTF;
303 ret = mbedtls_x509_dn_gets(p, n, &san->san.directory_name);
304 if (ret < 0) {
305 return ret;
306 }
307
308 p += ret;
309 n -= ret;
310 break;/* MBEDTLS_X509_SAN_DIRECTORY_NAME */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200311 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 /*
313 * Should not happen.
314 */
315 return -1;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200316 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 ret = mbedtls_snprintf(p, n, "\n");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200318 MBEDTLS_X509_SAFE_SNPRINTF;
319
320 *size = n;
321 *buf = p;
322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200324}
Michael Schustera3cc4632024-06-07 01:51:54 +0200325#endif /* MBEDTLS_FS_IO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200326
Michael Schuster54300d42024-06-04 02:30:22 +0200327static 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 +0200328 int critical, const unsigned char *cp, const unsigned char *end)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200329{
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 (void) crt;
331 (void) critical;
332 mbedtls_x509_buf *new_oid = (mbedtls_x509_buf *) p_ctx;
333 if (oid->tag == MBEDTLS_ASN1_OID &&
334 MBEDTLS_OID_CMP(MBEDTLS_OID_CERTIFICATE_POLICIES, oid) == 0) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200335 /* Handle unknown certificate policy */
336 int ret, parse_ret = 0;
337 size_t len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 unsigned char **p = (unsigned char **) &cp;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200339
340 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 ret = mbedtls_asn1_get_tag(p, end, &len,
342 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
343 if (ret != 0) {
344 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
345 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (*p + len != end) {
348 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
349 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
350 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200351
352 /*
353 * Cannot be an empty sequence.
354 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (len == 0) {
356 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
357 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
358 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 while (*p < end) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200361 const unsigned char *policy_end;
362
363 /*
364 * Get the policy sequence
365 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
367 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
368 0) {
369 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
370 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200371
372 policy_end = *p + len;
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
375 MBEDTLS_ASN1_OID)) != 0) {
376 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
377 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200378
Nicola Di Lietob77fad82020-06-17 17:57:36 +0200379 /*
380 * Recognize exclusively the policy with OID 1
381 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 if (len != 1 || *p[0] != 1) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200383 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200385
386 *p += len;
387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 /*
389 * If there is an optional qualifier, then *p < policy_end
390 * Check the Qualifier len to verify it doesn't exceed policy_end.
391 */
392 if (*p < policy_end) {
393 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
394 MBEDTLS_ASN1_CONSTRUCTED |
395 MBEDTLS_ASN1_SEQUENCE)) != 0) {
396 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
397 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200398 /*
399 * Skip the optional policy qualifiers.
400 */
401 *p += len;
402 }
403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if (*p != policy_end) {
405 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
406 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
407 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200408 }
409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 if (*p != end) {
411 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
412 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
413 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 return parse_ret;
416 } else if (new_oid != NULL && new_oid->tag == oid->tag && new_oid->len == oid->len &&
417 memcmp(new_oid->p, oid->p, oid->len) == 0) {
418 return 0;
419 } else {
420 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
421 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200422 }
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200423}
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200424#endif /* MBEDTLS_X509_CRT_PARSE_C */
Matthias Schulzab408222023-10-18 13:20:59 +0200425
Michael Schustera3cc4632024-06-07 01:51:54 +0200426#if defined(MBEDTLS_X509_CSR_PARSE_C) && \
427 !defined(MBEDTLS_X509_REMOVE_INFO)
Michael Schusterf828f042024-06-07 06:47:31 +0200428static int parse_csr_ext_accept_cb(void *p_ctx,
429 mbedtls_x509_csr const *csr,
430 mbedtls_x509_buf const *oid,
431 int critical,
432 const unsigned char *cp,
433 const unsigned char *end)
Matthias Schulzab408222023-10-18 13:20:59 +0200434{
435 (void) p_ctx;
436 (void) csr;
437 (void) oid;
438 (void) critical;
439 (void) cp;
440 (void) end;
441
442 return 0;
443}
444
Michael Schusterf828f042024-06-07 06:47:31 +0200445static int parse_csr_ext_reject_cb(void *p_ctx,
446 mbedtls_x509_csr const *csr,
447 mbedtls_x509_buf const *oid,
448 int critical,
449 const unsigned char *cp,
450 const unsigned char *end)
Matthias Schulzab408222023-10-18 13:20:59 +0200451{
452 (void) p_ctx;
453 (void) csr;
454 (void) oid;
455 (void) critical;
456 (void) cp;
457 (void) end;
458
459 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
460 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
461}
Michael Schustera3cc4632024-06-07 01:51:54 +0200462#endif /* MBEDTLS_X509_CSR_PARSE_C && !MBEDTLS_X509_REMOVE_INFO */
Paul Bakker33b43f12013-08-20 11:48:36 +0200463/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000464
Thomas Daubney5c9c2ce2022-06-06 16:36:43 +0100465/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100466void x509_accessor_ext_types(int ext_type, int has_ext_type)
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100467{
468 mbedtls_x509_crt crt;
469 int expected_result = ext_type & has_ext_type;
470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200472 USE_PSA_INIT();
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100473
474 crt.ext_types = ext_type;
475
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500476 TEST_EQUAL(mbedtls_x509_crt_has_ext_type(&crt, has_ext_type), expected_result);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100477
valerio32f2ac92023-04-20 11:59:52 +0200478exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200480 USE_PSA_DONE();
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100481}
482/* END_CASE */
483
Glenn Strauss6f545ac2022-10-25 15:02:14 -0400484/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_TEST_HOOKS */
485void x509_crt_parse_cn_inet_pton(const char *cn, data_t *exp, int ref_ret)
486{
487 uint32_t addr[4];
488 size_t addrlen = mbedtls_x509_crt_parse_cn_inet_pton(cn, addr);
489 TEST_EQUAL(addrlen, (size_t) ref_ret);
490
491 if (addrlen) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100492 TEST_MEMORY_COMPARE(exp->x, exp->len, addr, addrlen);
Glenn Strauss6f545ac2022-10-25 15:02:14 -0400493 }
494}
495/* END_CASE */
496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500498void x509_parse_san(char *crt_file, char *result_str, int parse_result)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200499{
Ron Eldor890819a2019-05-13 19:03:04 +0300500 int ret;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200501 mbedtls_x509_crt crt;
Ron Eldor890819a2019-05-13 19:03:04 +0300502 mbedtls_x509_subject_alternative_name san;
503 mbedtls_x509_sequence *cur = NULL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200504 char buf[2000];
505 char *p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 size_t n = sizeof(buf);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200509 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 memset(buf, 0, 2000);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200511
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500512 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), parse_result);
Ron Eldor890819a2019-05-13 19:03:04 +0300513
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500514 if (parse_result != 0) {
515 goto exit;
516 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if (crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Ron Eldor890819a2019-05-13 19:03:04 +0300518 cur = &crt.subject_alt_names;
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 while (cur != NULL) {
520 ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
521 TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
Ron Eldor890819a2019-05-13 19:03:04 +0300522 /*
523 * If san type not supported, ignore.
524 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if (ret == 0) {
Andrzej Kurekd40c2b62023-02-13 07:01:59 -0500526 ret = verify_parse_san(&san, &p, &n);
527 mbedtls_x509_free_subject_alt_name(&san);
528 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 }
Ron Eldor890819a2019-05-13 19:03:04 +0300530 cur = cur->next;
531 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200532 }
533
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500534 TEST_EQUAL(strcmp(buf, result_str), 0);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200535
536exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200538 USE_PSA_DONE();
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200539}
540/* END_CASE */
541
Hanno Becker612a2f12020-10-09 09:19:39 +0100542/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100543void x509_cert_info(char *crt_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000544{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000546 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000547 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200550 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000552
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500553 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 res = mbedtls_x509_crt_info(buf, 2000, "", &crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 TEST_ASSERT(res != -1);
557 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000558
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500559 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200560
561exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200563 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000564}
Paul Bakker33b43f12013-08-20 11:48:36 +0200565/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000566
Hanno Becker612a2f12020-10-09 09:19:39 +0100567/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100568void mbedtls_x509_crl_info(char *crl_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000569{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000571 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000572 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200575 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000577
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500578 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 res = mbedtls_x509_crl_info(buf, 2000, "", &crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 TEST_ASSERT(res != -1);
582 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000583
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500584 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200585
586exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +0200588 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000589}
Paul Bakker33b43f12013-08-20 11:48:36 +0200590/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000591
Andres AGa39db392016-12-08 17:10:38 +0000592/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100593void mbedtls_x509_crl_parse(char *crl_file, int result)
Andres AGa39db392016-12-08 17:10:38 +0000594{
595 mbedtls_x509_crl crl;
596 char buf[2000];
597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200599 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 memset(buf, 0, 2000);
Andres AGa39db392016-12-08 17:10:38 +0000601
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500602 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), result);
Andres AGa39db392016-12-08 17:10:38 +0000603
604exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +0200606 USE_PSA_DONE();
Andres AGa39db392016-12-08 17:10:38 +0000607}
608/* END_CASE */
609
Hanno Becker612a2f12020-10-09 09:19:39 +0100610/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100611void mbedtls_x509_csr_info(char *csr_file, char *result_str)
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100612{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100614 char buf[2000];
615 int res;
616
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +0200618 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 memset(buf, 0, 2000);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100620
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500621 TEST_EQUAL(mbedtls_x509_csr_parse_file(&csr, csr_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 res = mbedtls_x509_csr_info(buf, 2000, "", &csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 TEST_ASSERT(res != -1);
625 TEST_ASSERT(res != -2);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100626
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500627 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200628
629exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +0200631 USE_PSA_DONE();
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100632}
633/* END_CASE */
634
Hanno Becker612a2f12020-10-09 09:19:39 +0100635/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100636void x509_verify_info(int flags, char *prefix, char *result_str)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100637{
638 char buf[2000];
639 int res;
640
Valerio Setti569c1712023-04-19 14:53:36 +0200641 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 memset(buf, 0, sizeof(buf));
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 res = mbedtls_x509_crt_verify_info(buf, sizeof(buf), prefix, flags);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 TEST_ASSERT(res >= 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100647
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500648 TEST_EQUAL(strcmp(buf, result_str), 0);
valerio32f2ac92023-04-20 11:59:52 +0200649
650exit:
Valerio Setti569c1712023-04-19 14:53:36 +0200651 USE_PSA_DONE();
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100652}
653/* END_CASE */
654
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200655/* 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 +0100656void x509_verify_restart(char *crt_file, char *ca_file,
657 int result, int flags_result,
658 int max_ops, int min_restart, int max_restart)
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200659{
660 int ret, cnt_restart;
661 mbedtls_x509_crt_restart_ctx rs_ctx;
662 mbedtls_x509_crt crt;
663 mbedtls_x509_crt ca;
664 uint32_t flags = 0;
665
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200666 /*
667 * See comments on ecp_test_vect_restart() for op count precision.
668 *
Gilles Peskinee820c0a2023-08-03 17:45:20 +0200669 * For reference, with Mbed TLS 2.6 and default settings:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200670 * - ecdsa_verify() for P-256: ~ 6700
671 * - ecdsa_verify() for P-384: ~ 18800
672 * - x509_verify() for server5 -> test-ca2: ~ 18800
673 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
674 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 mbedtls_x509_crt_restart_init(&rs_ctx);
676 mbedtls_x509_crt_init(&crt);
677 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200678 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200679
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500680 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
681 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200682
Valerio Setti4a2e7b92025-05-23 15:15:22 +0200683 psa_interruptible_set_max_ops(max_ops);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200684
685 cnt_restart = 0;
686 do {
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 } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200691
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500692 TEST_EQUAL(ret, result);
693 TEST_EQUAL(flags, (uint32_t) flags_result);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200694
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 TEST_ASSERT(cnt_restart >= min_restart);
696 TEST_ASSERT(cnt_restart <= max_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200697
698 /* Do we leak memory when aborting? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
700 &mbedtls_x509_crt_profile_default, NULL, &flags,
701 NULL, NULL, &rs_ctx);
702 TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200703
704exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 mbedtls_x509_crt_restart_free(&rs_ctx);
706 mbedtls_x509_crt_free(&crt);
707 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100708 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200709}
710/* END_CASE */
711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100713void x509_verify(char *crt_file, char *ca_file, char *crl_file,
714 char *cn_name_str, int result, int flags_result,
715 char *profile_str,
716 char *verify_callback)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000717{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_x509_crt crt;
719 mbedtls_x509_crt ca;
720 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200721 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000722 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200723 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 char *cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200725 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 mbedtls_x509_crt_init(&crt);
728 mbedtls_x509_crt_init(&ca);
729 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200730 MD_OR_USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (strcmp(cn_name_str, "NULL") != 0) {
Paul Bakker33b43f12013-08-20 11:48:36 +0200733 cn_name = cn_name_str;
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 if (strcmp(profile_str, "") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200737 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 } else if (strcmp(profile_str, "next") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200739 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 } else if (strcmp(profile_str, "suite_b") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200741 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 } else if (strcmp(profile_str, "compat") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200743 profile = &compat_profile;
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 } else if (strcmp(profile_str, "all") == 0) {
Hanno Becker20a4ade2019-06-03 14:27:03 +0100745 profile = &profile_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100747 TEST_FAIL("Unknown algorithm profile");
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 }
Gilles Peskineef86ab22017-05-05 18:59:02 +0200749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 if (strcmp(verify_callback, "NULL") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200751 f_vrfy = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 } else if (strcmp(verify_callback, "verify_none") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200753 f_vrfy = verify_none;
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 } else if (strcmp(verify_callback, "verify_all") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200755 f_vrfy = verify_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100757 TEST_FAIL("No known verify callback selected");
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200759
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500760 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
761 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
762 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 res = mbedtls_x509_crt_verify_with_profile(&crt,
765 &ca,
766 &crl,
767 profile,
768 cn_name,
769 &flags,
770 f_vrfy,
771 NULL);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 TEST_EQUAL(res, result);
774 TEST_EQUAL(flags, (uint32_t) flags_result);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200775
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200776#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker0350d562019-03-28 14:23:36 +0000777 /* CRLs aren't supported with CA callbacks, so skip the CA callback
Jarno Lamsadfd22c42019-04-01 15:18:53 +0300778 * version of the test if CRLs are in use. */
Gilles Peskinebb7d92c2023-10-17 17:26:44 +0200779 if (strcmp(crl_file, "") == 0) {
Hanno Becker0350d562019-03-28 14:23:36 +0000780 flags = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 res = mbedtls_x509_crt_verify_with_ca_cb(&crt,
783 ca_callback,
784 &ca,
785 profile,
786 cn_name,
787 &flags,
788 f_vrfy,
789 NULL);
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200790
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500791 TEST_EQUAL(res, result);
792 TEST_EQUAL(flags, (uint32_t) (flags_result));
Hanno Becker0350d562019-03-28 14:23:36 +0000793 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200794#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200795exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 mbedtls_x509_crt_free(&crt);
797 mbedtls_x509_crt_free(&ca);
798 mbedtls_x509_crl_free(&crl);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100799 MD_OR_USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000800}
Paul Bakker33b43f12013-08-20 11:48:36 +0200801/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000802
Jarno Lamsa557426a2019-03-27 17:08:29 +0200803/* 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 +0100804void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
805 int exp_ret)
Jarno Lamsa557426a2019-03-27 17:08:29 +0200806{
807 int ret;
808 mbedtls_x509_crt crt;
809 mbedtls_x509_crt ca;
810 uint32_t flags = 0;
Hanno Becker3f932bb2019-03-28 17:06:47 +0000811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 mbedtls_x509_crt_init(&crt);
813 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200814 USE_PSA_INIT();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200815
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500816 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
817 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (strcmp(name, "NULL") == 0) {
Jarno Lamsa557426a2019-03-27 17:08:29 +0200820 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca,
824 &compat_profile, name, &flags,
825 NULL, NULL);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200826
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500827 TEST_EQUAL(ret, exp_ret);
828 TEST_EQUAL(flags, (uint32_t) (-1));
Jarno Lamsa557426a2019-03-27 17:08:29 +0200829exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 mbedtls_x509_crt_free(&crt);
831 mbedtls_x509_crt_free(&ca);
Valerio Setti569c1712023-04-19 14:53:36 +0200832 USE_PSA_DONE();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200833}
834/* END_CASE */
835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100837void x509_verify_callback(char *crt_file, char *ca_file, char *name,
838 int exp_ret, char *exp_vrfy_out)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200839{
840 int ret;
841 mbedtls_x509_crt crt;
842 mbedtls_x509_crt ca;
843 uint32_t flags = 0;
844 verify_print_context vrfy_ctx;
845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 mbedtls_x509_crt_init(&crt);
847 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200848 MD_OR_USE_PSA_INIT();
849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 verify_print_init(&vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200851
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500852 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
853 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (strcmp(name, "NULL") == 0) {
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200856 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 }
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200858
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL,
860 &compat_profile,
861 name, &flags,
862 verify_print, &vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200863
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500864 TEST_EQUAL(ret, exp_ret);
865 TEST_EQUAL(strcmp(vrfy_ctx.buf, exp_vrfy_out), 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200866
867exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 mbedtls_x509_crt_free(&crt);
869 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100870 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200871}
872/* END_CASE */
873
Hanno Becker612a2f12020-10-09 09:19:39 +0100874/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100875void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
876 char *new_subject_ou,
877 char *result_str,
878 int ret)
Werner Lewis31ecb962022-06-17 15:51:55 +0100879{
880 mbedtls_x509_crt crt;
881 char buf[2000];
882 int res = 0;
883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200885 USE_PSA_INIT();
886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 memset(buf, 0, 2000);
Werner Lewis31ecb962022-06-17 15:51:55 +0100888
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500889 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100890 crt.subject.next->val.p = (unsigned char *) new_subject_ou;
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 crt.subject.next->val.len = strlen(new_subject_ou);
Werner Lewis31ecb962022-06-17 15:51:55 +0100892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
Werner Lewis31ecb962022-06-17 15:51:55 +0100894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 if (ret != 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500896 TEST_EQUAL(res, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 } else {
898 TEST_ASSERT(res != -1);
899 TEST_ASSERT(res != -2);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500900 TEST_EQUAL(strcmp(buf, result_str), 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100901 }
902exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200904 USE_PSA_DONE();
Werner Lewis31ecb962022-06-17 15:51:55 +0100905}
906/* END_CASE */
907
908/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100909void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000910{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000912 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200913 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200916 USE_PSA_INIT();
917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000919
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500920 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if (strcmp(entity, "subject") == 0) {
922 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
923 } else if (strcmp(entity, "issuer") == 0) {
924 res = mbedtls_x509_dn_gets(buf, 2000, &crt.issuer);
925 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100926 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 TEST_ASSERT(res != -1);
930 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000931
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500932 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200933
934exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200936 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000937}
Paul Bakker33b43f12013-08-20 11:48:36 +0200938/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000939
David Horstmanndb73d3b2022-10-04 16:49:16 +0100940/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100941void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
David Horstmanndb73d3b2022-10-04 16:49:16 +0100942{
valerioe50831c2023-04-20 14:48:19 +0200943 unsigned char *name = NULL;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100944 unsigned char *p;
945 size_t name_len;
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100946 mbedtls_x509_name head;
David Horstmann3cd67582022-10-17 17:59:10 +0100947 int ret;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100948
Valerio Setti569c1712023-04-19 14:53:36 +0200949 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 memset(&head, 0, sizeof(head));
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100953 p = name;
954
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 ret = mbedtls_x509_get_name(&p, (name + name_len), &head);
956 if (ret == 0) {
957 mbedtls_asn1_free_named_data_list_shallow(head.next);
958 }
David Horstmanndb73d3b2022-10-04 16:49:16 +0100959
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 TEST_EQUAL(ret, exp_ret);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100961
valerio32f2ac92023-04-20 11:59:52 +0200962exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 mbedtls_free(name);
Valerio Setti569c1712023-04-19 14:53:36 +0200964 USE_PSA_DONE();
David Horstmanndb73d3b2022-10-04 16:49:16 +0100965}
966/* END_CASE */
967
Werner Lewisb3acb052022-06-17 15:59:58 +0100968/* 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 +0100969void mbedtls_x509_dn_get_next(char *name_str,
970 int next_merged,
971 char *expected_oids,
972 int exp_count,
973 char *exp_dn_gets)
Werner Lewisb3acb052022-06-17 15:59:58 +0100974{
975 int ret = 0, i;
Werner Lewisac80a662022-06-23 11:58:02 +0100976 size_t len = 0, out_size;
Werner Lewisb3acb052022-06-17 15:59:58 +0100977 mbedtls_asn1_named_data *names = NULL;
Gilles Peskine21e46b32023-10-17 16:35:20 +0200978 mbedtls_x509_name parsed;
979 memset(&parsed, 0, sizeof(parsed));
980 mbedtls_x509_name *parsed_cur;
Werner Lewisac80a662022-06-23 11:58:02 +0100981 // Size of buf is maximum required for test cases
Gilles Peskinef2574202023-10-18 17:39:48 +0200982 unsigned char buf[80] = { 0 };
Gilles Peskine21e46b32023-10-17 16:35:20 +0200983 unsigned char *out = NULL;
984 unsigned char *c = buf + sizeof(buf);
Werner Lewisb3acb052022-06-17 15:59:58 +0100985 const char *short_name;
986
Valerio Setti569c1712023-04-19 14:53:36 +0200987 USE_PSA_INIT();
Gilles Peskine21e46b32023-10-17 16:35:20 +0200988
Werner Lewisac80a662022-06-23 11:58:02 +0100989 // Additional size required for trailing space
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 out_size = strlen(expected_oids) + 2;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100991 TEST_CALLOC(out, out_size);
Werner Lewisb3acb052022-06-17 15:59:58 +0100992
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 TEST_EQUAL(mbedtls_x509_string_to_names(&names, name_str), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100994
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 ret = mbedtls_x509_write_names(&c, buf, names);
996 TEST_LE_S(0, ret);
Werner Lewisb3acb052022-06-17 15:59:58 +0100997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
999 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
1000 TEST_EQUAL(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +01001001
1002 // Iterate over names and set next_merged nodes
1003 parsed_cur = &parsed;
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 for (; next_merged != 0 && parsed_cur != NULL; next_merged = next_merged >> 1) {
Werner Lewis12657cd2022-06-20 11:47:57 +01001005 parsed_cur->next_merged = next_merged & 0x01;
Werner Lewisb3acb052022-06-17 15:59:58 +01001006 parsed_cur = parsed_cur->next;
1007 }
1008
1009 // Iterate over RDN nodes and print OID of first element to buffer
1010 parsed_cur = &parsed;
1011 len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 for (i = 0; parsed_cur != NULL; i++) {
Gilles Peskine532e3ee2025-05-07 20:37:15 +02001013 TEST_EQUAL(mbedtls_x509_oid_get_attr_short_name(&parsed_cur->oid,
1014 &short_name), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 len += mbedtls_snprintf((char *) out + len, out_size - len, "%s ", short_name);
1016 parsed_cur = mbedtls_x509_dn_get_next(parsed_cur);
Werner Lewisb3acb052022-06-17 15:59:58 +01001017 }
1018 out[len-1] = 0;
1019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 TEST_EQUAL(exp_count, i);
1021 TEST_EQUAL(strcmp((char *) out, expected_oids), 0);
1022 mbedtls_free(out);
Werner Lewisac80a662022-06-23 11:58:02 +01001023 out = NULL;
Werner Lewisb3acb052022-06-17 15:59:58 +01001024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 out_size = strlen(exp_dn_gets) + 1;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001026 TEST_CALLOC(out, out_size);
Werner Lewisac80a662022-06-23 11:58:02 +01001027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 TEST_LE_S(0, mbedtls_x509_dn_gets((char *) out, out_size, &parsed));
1029 TEST_EQUAL(strcmp((char *) out, exp_dn_gets), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +01001030exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 mbedtls_free(out);
1032 mbedtls_asn1_free_named_data_list(&names);
1033 mbedtls_asn1_free_named_data_list_shallow(parsed.next);
Valerio Setti569c1712023-04-19 14:53:36 +02001034 USE_PSA_DONE();
Werner Lewisb3acb052022-06-17 15:59:58 +01001035}
Werner Lewisb3acb052022-06-17 15:59:58 +01001036/* END_CASE */
1037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001039void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
Paul Bakker37940d9f2009-07-10 22:38:58 +00001040{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +00001042
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001044 USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +00001045
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001046 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if (strcmp(entity, "valid_from") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001049 TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_from), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 } else if (strcmp(entity, "valid_to") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001051 TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_to), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +01001053 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001055
Paul Bakkerbd51b262014-07-10 15:26:12 +02001056exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001058 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +00001059}
Paul Bakker33b43f12013-08-20 11:48:36 +02001060/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +00001061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001063void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001064{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001068 USE_PSA_INIT();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001069
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001070 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if (strcmp(entity, "valid_from") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001073 TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_from), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 } else if (strcmp(entity, "valid_to") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001075 TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_to), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +01001077 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001079
Paul Bakkerbd51b262014-07-10 15:26:12 +02001080exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001082 USE_PSA_DONE();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001083}
1084/* END_CASE */
1085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001087void x509parse_crt_file(char *crt_file, int result)
Paul Bakker5a5fa922014-09-26 14:53:04 +02001088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +02001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001092 USE_PSA_INIT();
Paul Bakker5a5fa922014-09-26 14:53:04 +02001093
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001094 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), result);
Paul Bakker5a5fa922014-09-26 14:53:04 +02001095
1096exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001098 USE_PSA_DONE();
Paul Bakker5a5fa922014-09-26 14:53:04 +02001099}
1100/* END_CASE */
1101
Minos Galanakisa83ada42024-01-19 10:59:07 +00001102/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
1103void mbedtls_x509_get_ca_istrue(char *crt_file, int result)
1104{
1105 mbedtls_x509_crt crt;
1106 mbedtls_x509_crt_init(&crt);
1107 USE_PSA_INIT();
1108
1109 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
1110 TEST_EQUAL(mbedtls_x509_crt_get_ca_istrue(&crt), result);
1111exit:
1112 mbedtls_x509_crt_free(&crt);
1113 USE_PSA_DONE();
1114}
1115/* END_CASE */
1116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001118void x509parse_crt(data_t *buf, char *result_str, int result)
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001119{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 mbedtls_x509_crt crt;
Valerio Setti210b6112025-06-19 23:19:05 +02001121#if !defined(MBEDTLS_X509_REMOVE_INFO)
Hanno Beckerfff2d572020-10-09 09:45:19 +01001122 unsigned char output[2000] = { 0 };
Valerio Setti210b6112025-06-19 23:19:05 +02001123#else
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001124 ((void) result_str);
Valerio Setti210b6112025-06-19 23:19:05 +02001125#endif
Valerio Setti27eb0142025-06-19 23:40:18 +02001126 /* Tests whose result is MBEDTLS_ERR_PK_INVALID_PUBKEY might return
1127 * MBEDTLS_ERR_ASN1_UNEXPECTED_TAG until psa#308 is merged. This variable
1128 * is therefore used for backward compatiblity and will be removed in
1129 * mbedtls#10229. */
1130 int result_back_comp = result;
Valerio Setti11345e92025-06-10 13:39:44 +02001131 int res;
1132
1133#if !defined(MBEDTLS_PK_USE_PSA_RSA_DATA)
1134 /* Support for mbedtls#10213 before psa#308. Once psa#308 will be
1135 * merged this dirty fix can be removed. */
1136 if (result == MBEDTLS_ERR_PK_INVALID_PUBKEY) {
Valerio Setti27eb0142025-06-19 23:40:18 +02001137 result_back_comp = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
Valerio Setti11345e92025-06-10 13:39:44 +02001138 }
1139#endif /* MBEDTLS_PK_USE_PSA_RSA_DATA */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001142 USE_PSA_INIT();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001143
Valerio Setti11345e92025-06-10 13:39:44 +02001144 res = mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len);
Valerio Setti27eb0142025-06-19 23:40:18 +02001145 TEST_ASSERT((res == result) || (res == result_back_comp));
Hanno Becker612a2f12020-10-09 09:19:39 +01001146#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 if ((result) == 0) {
1148 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1149 TEST_ASSERT(res != -1);
1150 TEST_ASSERT(res != -2);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001151
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001152 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001153 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001155#endif
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001156
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 mbedtls_x509_crt_free(&crt);
1158 mbedtls_x509_crt_init(&crt);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001159
Valerio Setti11345e92025-06-10 13:39:44 +02001160 res = mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len);
Valerio Setti27eb0142025-06-19 23:40:18 +02001161 TEST_ASSERT((res == result) || (res == result_back_comp));
Hanno Becker612a2f12020-10-09 09:19:39 +01001162#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 if ((result) == 0) {
1164 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakker33b43f12013-08-20 11:48:36 +02001167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 TEST_ASSERT(res != -1);
1169 TEST_ASSERT(res != -2);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001170
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001171 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001172 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001174#endif /* !MBEDTLS_X509_REMOVE_INFO */
Paul Bakkerb08e6842012-02-11 18:43:20 +00001175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 mbedtls_x509_crt_free(&crt);
1177 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001178
Valerio Setti11345e92025-06-10 13:39:44 +02001179 res = mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL, NULL);
Valerio Setti27eb0142025-06-19 23:40:18 +02001180 TEST_ASSERT((res == result) || (res == result_back_comp));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001181#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if ((result) == 0) {
1183 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001184
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 TEST_ASSERT(res != -1);
1186 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001187
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001188 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001189 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001191#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 mbedtls_x509_crt_free(&crt);
1194 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001195
Valerio Setti11345e92025-06-10 13:39:44 +02001196 res = mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL, NULL);
Valerio Setti27eb0142025-06-19 23:40:18 +02001197 TEST_ASSERT((res == result) || (res == result_back_comp));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001198#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 if ((result) == 0) {
1200 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 TEST_ASSERT(res != -1);
1203 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001204
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001205 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001206 }
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001207#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001208
1209exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001211 USE_PSA_DONE();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001212}
1213/* END_CASE */
1214
1215/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001216void x509parse_crt_cb(data_t *buf, char *result_str, int result)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001217{
1218 mbedtls_x509_crt crt;
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001219 mbedtls_x509_buf oid;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001220
1221#if !defined(MBEDTLS_X509_REMOVE_INFO)
1222 unsigned char output[2000] = { 0 };
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001223 int res;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001224#else
1225 ((void) result_str);
1226#endif
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001227
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001228 oid.tag = MBEDTLS_ASN1_OID;
1229 oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001233 USE_PSA_INIT();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001234
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001235 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
1236 &oid), result);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001237#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 if ((result) == 0) {
1239 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001240
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 TEST_ASSERT(res != -1);
1242 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001243
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001244 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001245 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 memset(output, 0, 2000);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001247#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001248
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 mbedtls_x509_crt_free(&crt);
1250 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001251
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001252 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1253 &oid), (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001254#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 if ((result) == 0) {
1256 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 TEST_ASSERT(res != -1);
1259 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001260
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001261 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001262 }
Hanno Beckerfff2d572020-10-09 09:45:19 +01001263#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001264
Paul Bakkerbd51b262014-07-10 15:26:12 +02001265exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001267 USE_PSA_DONE();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001268}
Paul Bakker33b43f12013-08-20 11:48:36 +02001269/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001270
Hanno Becker612a2f12020-10-09 09:19:39 +01001271/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001272void x509parse_crl(data_t *buf, char *result_str, int result)
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001273{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001275 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +01001276 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001277
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +02001279 USE_PSA_INIT();
1280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 memset(output, 0, 2000);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001282
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001283
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001284 TEST_EQUAL(mbedtls_x509_crl_parse(&crl, buf->x, buf->len), (result));
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 if ((result) == 0) {
1286 res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
Paul Bakker33b43f12013-08-20 11:48:36 +02001287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 TEST_ASSERT(res != -1);
1289 TEST_ASSERT(res != -2);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001290
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001291 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001292 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001293
Paul Bakkerbd51b262014-07-10 15:26:12 +02001294exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +02001296 USE_PSA_DONE();
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001297}
Paul Bakker33b43f12013-08-20 11:48:36 +02001298/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001299
Hanno Becker612a2f12020-10-09 09:19:39 +01001300/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001301void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001302{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001304 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001305 int my_ret;
1306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001308 USE_PSA_INIT();
1309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 memset(my_out, 0, sizeof(my_out));
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001313 TEST_EQUAL(my_ret, ref_ret);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 if (ref_ret == 0) {
1316 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001317 TEST_EQUAL(my_out_len, strlen(ref_out));
1318 TEST_EQUAL(strcmp(my_out, ref_out), 0);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001319 }
1320
Paul Bakkerbd51b262014-07-10 15:26:12 +02001321exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001323 USE_PSA_DONE();
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001324}
1325/* END_CASE */
1326
Matthias Schulzab408222023-10-18 13:20:59 +02001327/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1328void mbedtls_x509_csr_parse_with_ext_cb(data_t *csr_der, char *ref_out, int ref_ret, int accept)
1329{
1330 mbedtls_x509_csr csr;
1331 char my_out[1000];
1332 int my_ret;
1333
1334 mbedtls_x509_csr_init(&csr);
1335 USE_PSA_INIT();
1336
1337 memset(my_out, 0, sizeof(my_out));
1338
1339 my_ret = mbedtls_x509_csr_parse_der_with_ext_cb(&csr, csr_der->x, csr_der->len,
1340 accept ? parse_csr_ext_accept_cb :
Matthias Schulzedc32ea2023-10-19 16:09:08 +02001341 parse_csr_ext_reject_cb,
Matthias Schulzab408222023-10-18 13:20:59 +02001342 NULL);
1343 TEST_EQUAL(my_ret, ref_ret);
1344
1345 if (ref_ret == 0) {
1346 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1347 TEST_EQUAL(my_out_len, strlen(ref_out));
1348 TEST_EQUAL(strcmp(my_out, ref_out), 0);
1349 }
1350
1351exit:
1352 mbedtls_x509_csr_free(&csr);
1353 USE_PSA_DONE();
1354}
1355/* END_CASE */
1356
Przemek Stekield7992df2023-01-25 16:19:50 +01001357/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1358void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
1359{
1360 mbedtls_x509_csr csr;
1361 char my_out[1000];
1362 int my_ret;
1363
1364 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001365 USE_PSA_INIT();
1366
Przemek Stekield7992df2023-01-25 16:19:50 +01001367 memset(my_out, 0, sizeof(my_out));
1368
1369 my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001370 TEST_EQUAL(my_ret, ref_ret);
Przemek Stekield7992df2023-01-25 16:19:50 +01001371
1372 if (ref_ret == 0) {
1373 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001374 TEST_EQUAL(my_out_len, strlen(ref_out));
1375 TEST_EQUAL(strcmp(my_out, ref_out), 0);
Przemek Stekield7992df2023-01-25 16:19:50 +01001376 }
1377
1378exit:
1379 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001380 USE_PSA_DONE();
Przemek Stekield7992df2023-01-25 16:19:50 +01001381}
1382/* END_CASE */
1383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1e5fec62023-04-13 18:13:48 +02001385void mbedtls_x509_crt_parse_file(char *crt_path, int ret, int nb_crt)
1386{
1387 mbedtls_x509_crt chain, *cur;
1388 int i;
1389
1390 mbedtls_x509_crt_init(&chain);
1391 USE_PSA_INIT();
1392
1393 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, crt_path), ret);
1394
1395 /* Check how many certs we got */
1396 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1397 if (cur->raw.p != NULL) {
1398 i++;
1399 }
1400 }
1401
1402 TEST_EQUAL(i, nb_crt);
1403
1404exit:
1405 mbedtls_x509_crt_free(&chain);
1406 USE_PSA_DONE();
1407}
1408/* END_CASE */
1409
1410/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001411void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001412{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001414 int i;
1415
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001417 USE_PSA_INIT();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001418
Gilles Peskine55ad28a2023-04-13 18:14:45 +02001419 TEST_EQUAL(mbedtls_x509_crt_parse_path(&chain, crt_path), ret);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001420
1421 /* Check how many certs we got */
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1423 if (cur->raw.p != NULL) {
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001424 i++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 }
1426 }
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001427
Gilles Peskine55ad28a2023-04-13 18:14:45 +02001428 TEST_EQUAL(i, nb_crt);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001429
Paul Bakkerbd51b262014-07-10 15:26:12 +02001430exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 mbedtls_x509_crt_free(&chain);
Valerio Setti569c1712023-04-19 14:53:36 +02001432 USE_PSA_DONE();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001433}
1434/* END_CASE */
1435
Janos Follath822b2c32015-10-11 10:25:22 +02001436/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001437void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1438 int ret_chk, int flags_chk)
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001439{
1440 char file_buf[128];
1441 int ret;
1442 uint32_t flags;
1443 mbedtls_x509_crt trusted, chain;
1444
1445 /*
1446 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1447 * with NN.crt signed by NN-1.crt
1448 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 mbedtls_x509_crt_init(&trusted);
1450 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001451 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001452
1453 /* Load trusted root */
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001454 TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, ca_file), 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001455
1456 /* Load a chain with nb_int intermediates (from 01 to nb_int),
1457 * plus one "end-entity" cert (nb_int + 1) */
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001458 ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 nb_int + 1);
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001460 TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001461 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, file_buf), 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001462
1463 /* Try to verify that chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1465 NULL, NULL);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001466 TEST_EQUAL(ret, ret_chk);
1467 TEST_EQUAL(flags, (uint32_t) flags_chk);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001468
1469exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 mbedtls_x509_crt_free(&chain);
1471 mbedtls_x509_crt_free(&trusted);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001472 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001473}
1474/* END_CASE */
1475
1476/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001477void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1478 int flags_result, int result,
1479 char *profile_name, int vrfy_fatal_lvls)
Janos Follath822b2c32015-10-11 10:25:22 +02001480{
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 char *act;
Janos Follath822b2c32015-10-11 10:25:22 +02001482 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001483 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001484 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001485 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +02001486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 mbedtls_x509_crt_init(&chain);
1488 mbedtls_x509_crt_init(&trusted);
valerio32f2ac92023-04-20 11:59:52 +02001489 MD_OR_USE_PSA_INIT();
Janos Follath822b2c32015-10-11 10:25:22 +02001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 while ((act = mystrsep(&chain_paths, " ")) != NULL) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001492 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, act), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 }
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001494 TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, trusted_ca), 0);
Janos Follath822b2c32015-10-11 10:25:22 +02001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (strcmp(profile_name, "") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001497 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 } else if (strcmp(profile_name, "next") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001499 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 } else if (strcmp(profile_name, "suiteb") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001501 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 } else if (strcmp(profile_name, "rsa3072") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001503 profile = &profile_rsa3072;
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 } else if (strcmp(profile_name, "sha512") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001505 profile = &profile_sha512;
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 }
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001507
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1509 NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
Janos Follathef4f2582015-10-11 16:17:27 +02001510
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001511 TEST_EQUAL(res, (result));
1512 TEST_EQUAL(flags, (uint32_t) (flags_result));
Janos Follath822b2c32015-10-11 10:25:22 +02001513
1514exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 mbedtls_x509_crt_free(&trusted);
1516 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001517 MD_OR_USE_PSA_DONE();
Janos Follath822b2c32015-10-11 10:25:22 +02001518}
1519/* END_CASE */
1520
Gilles Peskine02ec5852025-05-12 20:52:07 +02001521/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001522void x509_oid_desc(data_t *buf, char *ref_desc)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001523{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001525 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001526 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001527
Valerio Setti569c1712023-04-19 14:53:36 +02001528 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001531 oid.p = buf->x;
1532 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001533
Gilles Peskine532e3ee2025-05-07 20:37:15 +02001534 ret = mbedtls_x509_oid_get_extended_key_usage(&oid, &desc);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001535
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 if (strcmp(ref_desc, "notfound") == 0) {
1537 TEST_ASSERT(ret != 0);
1538 TEST_ASSERT(desc == NULL);
1539 } else {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001540 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 TEST_ASSERT(desc != NULL);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001542 TEST_EQUAL(strcmp(desc, ref_desc), 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001543 }
valerio32f2ac92023-04-20 11:59:52 +02001544
1545exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001546 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001547}
1548/* END_CASE */
1549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001551void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001552{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001554 char num_buf[100];
1555
Valerio Setti569c1712023-04-19 14:53:36 +02001556 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001557
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001558 memset(num_buf, 0x2a, sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001561 oid.p = oid_buf->x;
1562 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001563
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001564 TEST_ASSERT((size_t) blen <= sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001565
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001566 TEST_EQUAL(mbedtls_oid_get_numeric_string(num_buf, blen, &oid), ret);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001567
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 if (ret >= 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001569 TEST_EQUAL(num_buf[ret], 0);
1570 TEST_EQUAL(strcmp(num_buf, numstr), 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001571 }
valerio32f2ac92023-04-20 11:59:52 +02001572
1573exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001574 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001575}
1576/* END_CASE */
1577
TRodziewicz442fdc22021-06-07 13:52:23 +02001578/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001579void x509_check_key_usage(char *crt_file, int usage, int ret)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001580{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001582
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001584 USE_PSA_INIT();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001585
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001586 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001587
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001588 TEST_EQUAL(mbedtls_x509_crt_check_key_usage(&crt, usage), ret);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001589
Paul Bakkerbd51b262014-07-10 15:26:12 +02001590exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001592 USE_PSA_DONE();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001593}
1594/* END_CASE */
1595
TRodziewicz442fdc22021-06-07 13:52:23 +02001596/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001597void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1598 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001599{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001603 USE_PSA_INIT();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001604
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001605 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001606
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001607 TEST_EQUAL(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x, oid->len),
1608 ret);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001609
Paul Bakkerbd51b262014-07-10 15:26:12 +02001610exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001612 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001613}
1614/* END_CASE */
1615
Andres AG4b76aec2016-09-23 13:16:02 +01001616/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001617void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1618 int day, int hour, int min, int sec)
Andres AG4b76aec2016-09-23 13:16:02 +01001619{
1620 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001621 unsigned char buf[21];
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 unsigned char *start = buf;
1623 unsigned char *end = buf;
Andres AG4b76aec2016-09-23 13:16:02 +01001624
Valerio Setti569c1712023-04-19 14:53:36 +02001625 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 memset(&time, 0x00, sizeof(time));
1627 *end = (unsigned char) tag; end++;
1628 *end = strlen(time_str);
1629 TEST_ASSERT(*end < 20);
Andres AG4b76aec2016-09-23 13:16:02 +01001630 end++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 memcpy(end, time_str, (size_t) *(end - 1));
Andres AG4b76aec2016-09-23 13:16:02 +01001632 end += *(end - 1);
1633
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001634 TEST_EQUAL(mbedtls_x509_get_time(&start, end, &time), ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 if (ret == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001636 TEST_EQUAL(year, time.year);
1637 TEST_EQUAL(mon, time.mon);
1638 TEST_EQUAL(day, time.day);
1639 TEST_EQUAL(hour, time.hour);
1640 TEST_EQUAL(min, time.min);
1641 TEST_EQUAL(sec, time.sec);
Andres AG4b76aec2016-09-23 13:16:02 +01001642 }
valerio32f2ac92023-04-20 11:59:52 +02001643exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001644 USE_PSA_DONE();
Andres AG4b76aec2016-09-23 13:16:02 +01001645}
1646/* END_CASE */
1647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001649void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1650 int ref_msg_md, int ref_mgf_md,
1651 int ref_salt_len, int ref_ret)
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001652{
1653 int my_ret;
Ronald Cronac6ae352020-06-26 14:33:03 +02001654 mbedtls_x509_buf buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001656 int my_salt_len;
1657
Valerio Setti569c1712023-04-19 14:53:36 +02001658 USE_PSA_INIT();
1659
Ronald Cronac6ae352020-06-26 14:33:03 +02001660 buf.p = params->x;
1661 buf.len = params->len;
1662 buf.tag = params_tag;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001663
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1665 &my_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001666
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001667 TEST_EQUAL(my_ret, ref_ret);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001668
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 if (ref_ret == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001670 TEST_EQUAL(my_msg_md, (mbedtls_md_type_t) ref_msg_md);
1671 TEST_EQUAL(my_mgf_md, (mbedtls_md_type_t) ref_mgf_md);
1672 TEST_EQUAL(my_salt_len, ref_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001673 }
1674
Paul Bakkerbd51b262014-07-10 15:26:12 +02001675exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001676 USE_PSA_DONE();
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001677}
1678/* END_CASE */
toth92ga41954d2021-02-12 16:11:17 +01001679
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001680/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001681void x509_crt_parse_subjectkeyid(char *file, data_t *subjectKeyId, int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001682{
1683 mbedtls_x509_crt crt;
toth92ga41954d2021-02-12 16:11:17 +01001684
1685 mbedtls_x509_crt_init(&crt);
Valerio Setti03a86e72025-05-28 12:01:14 +02001686 /* X509 relies on PK under the hood and the latter can use PSA to store keys
1687 * and perform operations so psa_crypto_init() must be called before. */
1688 USE_PSA_INIT();
toth92ga41954d2021-02-12 16:11:17 +01001689
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001690 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001691
toth92g357b2972021-05-04 15:41:35 +02001692 if (ref_ret == 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001693 TEST_EQUAL(crt.subject_key_id.tag, MBEDTLS_ASN1_OCTET_STRING);
1694 TEST_EQUAL(memcmp(crt.subject_key_id.p, subjectKeyId->x, subjectKeyId->len), 0);
1695 TEST_EQUAL(crt.subject_key_id.len, subjectKeyId->len);
toth92g357b2972021-05-04 15:41:35 +02001696 } else {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001697 TEST_EQUAL(crt.subject_key_id.tag, 0);
1698 TEST_EQUAL(crt.subject_key_id.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001699 }
1700
1701exit:
1702 mbedtls_x509_crt_free(&crt);
Valerio Setti03a86e72025-05-28 12:01:14 +02001703 USE_PSA_DONE();
toth92ga41954d2021-02-12 16:11:17 +01001704}
1705/* END_CASE */
1706
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001707/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Przemek Stekiel2568d472023-04-06 09:23:25 +02001708void x509_crt_parse_authoritykeyid(char *file,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001709 data_t *keyId,
toth92g5042b102021-05-06 08:22:17 +02001710 char *authorityKeyId_issuer,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001711 data_t *serial,
toth92g5042b102021-05-06 08:22:17 +02001712 int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001713{
1714 mbedtls_x509_crt crt;
Przemek Stekiel39dbe232023-04-03 10:19:22 +02001715 mbedtls_x509_subject_alternative_name san;
David Horstmann9a3a1a62023-06-22 16:59:09 +01001716 char name_buf[128];
toth92ga41954d2021-02-12 16:11:17 +01001717
1718 mbedtls_x509_crt_init(&crt);
Valerio Setti03a86e72025-05-28 12:01:14 +02001719 /* X509 relies on PK under the hood and the latter can use PSA to store keys
1720 * and perform operations so psa_crypto_init() must be called before. */
1721 USE_PSA_INIT();
toth92ga41954d2021-02-12 16:11:17 +01001722
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001723 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001724
toth92g357b2972021-05-04 15:41:35 +02001725 if (ref_ret == 0) {
toth92ga41954d2021-02-12 16:11:17 +01001726 /* KeyId test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001727 if (keyId->len > 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001728 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, MBEDTLS_ASN1_OCTET_STRING);
1729 TEST_EQUAL(memcmp(crt.authority_key_id.keyIdentifier.p, keyId->x, keyId->len), 0);
1730 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, keyId->len);
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001731 } else {
1732 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1733 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001734 }
toth92ga41954d2021-02-12 16:11:17 +01001735
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001736
toth92ga41954d2021-02-12 16:11:17 +01001737 /* Issuer test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001738 if (strlen(authorityKeyId_issuer) > 0) {
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001739 mbedtls_x509_sequence *issuerPtr = &crt.authority_key_id.authorityCertIssuer;
Przemek Stekiel01984212023-02-09 09:29:34 +01001740
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001741 TEST_EQUAL(mbedtls_x509_parse_subject_alt_name(&issuerPtr->buf, &san), 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001742
David Horstmann9a3a1a62023-06-22 16:59:09 +01001743 TEST_ASSERT(mbedtls_x509_dn_gets(name_buf, sizeof(name_buf),
1744 &san.san.directory_name)
1745 > 0);
1746 TEST_EQUAL(strcmp(name_buf, authorityKeyId_issuer), 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001747
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001748 mbedtls_x509_free_subject_alt_name(&san);
toth92ga41954d2021-02-12 16:11:17 +01001749 }
1750
1751 /* Serial test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001752 if (serial->len > 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001753 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001754 MBEDTLS_ASN1_INTEGER);
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001755 TEST_EQUAL(memcmp(crt.authority_key_id.authorityCertSerialNumber.p,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001756 serial->x, serial->len), 0);
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001757 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, serial->len);
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001758 } else {
1759 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1760 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001761 }
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001762
toth92g357b2972021-05-04 15:41:35 +02001763 } else {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001764 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1765 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001766
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001767 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1768 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001769 }
1770
1771exit:
1772 mbedtls_x509_crt_free(&crt);
Valerio Setti03a86e72025-05-28 12:01:14 +02001773 USE_PSA_DONE();
toth92ga41954d2021-02-12 16:11:17 +01001774}
1775/* END_CASE */
Sam Berry4aee6a22024-07-19 15:00:41 +01001776
Harry Ramsey94c30652024-09-19 16:13:32 +01001777/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Sam Berry4aee6a22024-07-19 15:00:41 +01001778void oid_get_numeric_string(data_t *oid, int error_ret, char *result_str)
1779{
1780 char buf[256];
1781 mbedtls_asn1_buf input_oid = { 0, 0, NULL };
1782 int ret;
1783
1784 input_oid.tag = MBEDTLS_ASN1_OID;
1785 /* Test that an empty OID is not dereferenced */
1786 input_oid.p = oid->len ? oid->x : (void *) 1;
1787 input_oid.len = oid->len;
1788
1789 ret = mbedtls_oid_get_numeric_string(buf, sizeof(buf), &input_oid);
1790
1791 if (error_ret == 0) {
1792 TEST_EQUAL(ret, strlen(result_str));
1793 TEST_ASSERT(ret >= 3);
1794 TEST_EQUAL(strcmp(buf, result_str), 0);
1795 } else {
1796 TEST_EQUAL(ret, error_ret);
1797 }
1798}
1799/* END_CASE */