blob: 3220a6eb9eb47e03c889d2ada13b6db63a52fd73 [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 Setti11345e92025-06-10 13:39:44 +02001126 /* Pick an error which is not used in the test_suite_x509parse.data file. */
1127 int result_ext = MBEDTLS_ERR_ERROR_GENERIC_ERROR;
1128 int res;
1129
1130#if !defined(MBEDTLS_PK_USE_PSA_RSA_DATA)
1131 /* Support for mbedtls#10213 before psa#308. Once psa#308 will be
1132 * merged this dirty fix can be removed. */
1133 if (result == MBEDTLS_ERR_PK_INVALID_PUBKEY) {
1134 result_ext = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
1135 }
1136#endif /* MBEDTLS_PK_USE_PSA_RSA_DATA */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001139 USE_PSA_INIT();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001140
Valerio Setti11345e92025-06-10 13:39:44 +02001141 res = mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len);
1142 fprintf(stderr, "\n res=%d, result=%d, result_ext=%d \n", res, result, result_ext);
1143 TEST_ASSERT((res == result) || (res == result_ext));
Hanno Becker612a2f12020-10-09 09:19:39 +01001144#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if ((result) == 0) {
1146 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1147 TEST_ASSERT(res != -1);
1148 TEST_ASSERT(res != -2);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001149
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001150 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001151 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001153#endif
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 mbedtls_x509_crt_free(&crt);
1156 mbedtls_x509_crt_init(&crt);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001157
Valerio Setti11345e92025-06-10 13:39:44 +02001158 res = mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len);
1159 TEST_ASSERT((res == result) || (res == result_ext));
Hanno Becker612a2f12020-10-09 09:19:39 +01001160#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 if ((result) == 0) {
1162 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakker33b43f12013-08-20 11:48:36 +02001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 TEST_ASSERT(res != -1);
1167 TEST_ASSERT(res != -2);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001168
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001169 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001170 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001172#endif /* !MBEDTLS_X509_REMOVE_INFO */
Paul Bakkerb08e6842012-02-11 18:43:20 +00001173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 mbedtls_x509_crt_free(&crt);
1175 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001176
Valerio Setti11345e92025-06-10 13:39:44 +02001177 res = mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL, NULL);
1178 TEST_ASSERT((res == result) || (res == result_ext));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001179#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 if ((result) == 0) {
1181 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 TEST_ASSERT(res != -1);
1184 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001185
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001186 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001187 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001189#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 mbedtls_x509_crt_free(&crt);
1192 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001193
Valerio Setti11345e92025-06-10 13:39:44 +02001194 res = mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL, NULL);
1195 TEST_ASSERT((res == result) || (res == result_ext));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001196#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 if ((result) == 0) {
1198 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001199
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 TEST_ASSERT(res != -1);
1201 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001202
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001203 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001204 }
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001205#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001206
1207exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001209 USE_PSA_DONE();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001210}
1211/* END_CASE */
1212
1213/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001214void x509parse_crt_cb(data_t *buf, char *result_str, int result)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001215{
1216 mbedtls_x509_crt crt;
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001217 mbedtls_x509_buf oid;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001218
1219#if !defined(MBEDTLS_X509_REMOVE_INFO)
1220 unsigned char output[2000] = { 0 };
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001221 int res;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001222#else
1223 ((void) result_str);
1224#endif
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001225
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001226 oid.tag = MBEDTLS_ASN1_OID;
1227 oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001229
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001231 USE_PSA_INIT();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001232
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001233 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
1234 &oid), result);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001235#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 if ((result) == 0) {
1237 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001238
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 TEST_ASSERT(res != -1);
1240 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001241
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001242 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001243 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 memset(output, 0, 2000);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001245#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 mbedtls_x509_crt_free(&crt);
1248 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001249
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001250 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1251 &oid), (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001252#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 if ((result) == 0) {
1254 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 TEST_ASSERT(res != -1);
1257 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001258
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001259 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001260 }
Hanno Beckerfff2d572020-10-09 09:45:19 +01001261#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001262
Paul Bakkerbd51b262014-07-10 15:26:12 +02001263exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001265 USE_PSA_DONE();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001266}
Paul Bakker33b43f12013-08-20 11:48:36 +02001267/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001268
Hanno Becker612a2f12020-10-09 09:19:39 +01001269/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001270void x509parse_crl(data_t *buf, char *result_str, int result)
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001271{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001273 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +01001274 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001275
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +02001277 USE_PSA_INIT();
1278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 memset(output, 0, 2000);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001280
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001281
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001282 TEST_EQUAL(mbedtls_x509_crl_parse(&crl, buf->x, buf->len), (result));
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 if ((result) == 0) {
1284 res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
Paul Bakker33b43f12013-08-20 11:48:36 +02001285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 TEST_ASSERT(res != -1);
1287 TEST_ASSERT(res != -2);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001288
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001289 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001290 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001291
Paul Bakkerbd51b262014-07-10 15:26:12 +02001292exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +02001294 USE_PSA_DONE();
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001295}
Paul Bakker33b43f12013-08-20 11:48:36 +02001296/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001297
Hanno Becker612a2f12020-10-09 09:19:39 +01001298/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001299void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001300{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001302 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001303 int my_ret;
1304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001306 USE_PSA_INIT();
1307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 memset(my_out, 0, sizeof(my_out));
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001311 TEST_EQUAL(my_ret, ref_ret);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 if (ref_ret == 0) {
1314 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001315 TEST_EQUAL(my_out_len, strlen(ref_out));
1316 TEST_EQUAL(strcmp(my_out, ref_out), 0);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001317 }
1318
Paul Bakkerbd51b262014-07-10 15:26:12 +02001319exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001321 USE_PSA_DONE();
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001322}
1323/* END_CASE */
1324
Matthias Schulzab408222023-10-18 13:20:59 +02001325/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1326void mbedtls_x509_csr_parse_with_ext_cb(data_t *csr_der, char *ref_out, int ref_ret, int accept)
1327{
1328 mbedtls_x509_csr csr;
1329 char my_out[1000];
1330 int my_ret;
1331
1332 mbedtls_x509_csr_init(&csr);
1333 USE_PSA_INIT();
1334
1335 memset(my_out, 0, sizeof(my_out));
1336
1337 my_ret = mbedtls_x509_csr_parse_der_with_ext_cb(&csr, csr_der->x, csr_der->len,
1338 accept ? parse_csr_ext_accept_cb :
Matthias Schulzedc32ea2023-10-19 16:09:08 +02001339 parse_csr_ext_reject_cb,
Matthias Schulzab408222023-10-18 13:20:59 +02001340 NULL);
1341 TEST_EQUAL(my_ret, ref_ret);
1342
1343 if (ref_ret == 0) {
1344 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1345 TEST_EQUAL(my_out_len, strlen(ref_out));
1346 TEST_EQUAL(strcmp(my_out, ref_out), 0);
1347 }
1348
1349exit:
1350 mbedtls_x509_csr_free(&csr);
1351 USE_PSA_DONE();
1352}
1353/* END_CASE */
1354
Przemek Stekield7992df2023-01-25 16:19:50 +01001355/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1356void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
1357{
1358 mbedtls_x509_csr csr;
1359 char my_out[1000];
1360 int my_ret;
1361
1362 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001363 USE_PSA_INIT();
1364
Przemek Stekield7992df2023-01-25 16:19:50 +01001365 memset(my_out, 0, sizeof(my_out));
1366
1367 my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001368 TEST_EQUAL(my_ret, ref_ret);
Przemek Stekield7992df2023-01-25 16:19:50 +01001369
1370 if (ref_ret == 0) {
1371 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001372 TEST_EQUAL(my_out_len, strlen(ref_out));
1373 TEST_EQUAL(strcmp(my_out, ref_out), 0);
Przemek Stekield7992df2023-01-25 16:19:50 +01001374 }
1375
1376exit:
1377 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001378 USE_PSA_DONE();
Przemek Stekield7992df2023-01-25 16:19:50 +01001379}
1380/* END_CASE */
1381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1e5fec62023-04-13 18:13:48 +02001383void mbedtls_x509_crt_parse_file(char *crt_path, int ret, int nb_crt)
1384{
1385 mbedtls_x509_crt chain, *cur;
1386 int i;
1387
1388 mbedtls_x509_crt_init(&chain);
1389 USE_PSA_INIT();
1390
1391 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, crt_path), ret);
1392
1393 /* Check how many certs we got */
1394 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1395 if (cur->raw.p != NULL) {
1396 i++;
1397 }
1398 }
1399
1400 TEST_EQUAL(i, nb_crt);
1401
1402exit:
1403 mbedtls_x509_crt_free(&chain);
1404 USE_PSA_DONE();
1405}
1406/* END_CASE */
1407
1408/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001409void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001410{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001412 int i;
1413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001415 USE_PSA_INIT();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001416
Gilles Peskine55ad28a2023-04-13 18:14:45 +02001417 TEST_EQUAL(mbedtls_x509_crt_parse_path(&chain, crt_path), ret);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001418
1419 /* Check how many certs we got */
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1421 if (cur->raw.p != NULL) {
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001422 i++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 }
1424 }
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001425
Gilles Peskine55ad28a2023-04-13 18:14:45 +02001426 TEST_EQUAL(i, nb_crt);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001427
Paul Bakkerbd51b262014-07-10 15:26:12 +02001428exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 mbedtls_x509_crt_free(&chain);
Valerio Setti569c1712023-04-19 14:53:36 +02001430 USE_PSA_DONE();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001431}
1432/* END_CASE */
1433
Janos Follath822b2c32015-10-11 10:25:22 +02001434/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001435void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1436 int ret_chk, int flags_chk)
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001437{
1438 char file_buf[128];
1439 int ret;
1440 uint32_t flags;
1441 mbedtls_x509_crt trusted, chain;
1442
1443 /*
1444 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1445 * with NN.crt signed by NN-1.crt
1446 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 mbedtls_x509_crt_init(&trusted);
1448 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001449 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001450
1451 /* Load trusted root */
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001452 TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, ca_file), 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001453
1454 /* Load a chain with nb_int intermediates (from 01 to nb_int),
1455 * plus one "end-entity" cert (nb_int + 1) */
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001456 ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 nb_int + 1);
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001458 TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001459 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, file_buf), 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001460
1461 /* Try to verify that chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1463 NULL, NULL);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001464 TEST_EQUAL(ret, ret_chk);
1465 TEST_EQUAL(flags, (uint32_t) flags_chk);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001466
1467exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 mbedtls_x509_crt_free(&chain);
1469 mbedtls_x509_crt_free(&trusted);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001470 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001471}
1472/* END_CASE */
1473
1474/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001475void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1476 int flags_result, int result,
1477 char *profile_name, int vrfy_fatal_lvls)
Janos Follath822b2c32015-10-11 10:25:22 +02001478{
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 char *act;
Janos Follath822b2c32015-10-11 10:25:22 +02001480 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001481 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001482 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001483 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +02001484
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 mbedtls_x509_crt_init(&chain);
1486 mbedtls_x509_crt_init(&trusted);
valerio32f2ac92023-04-20 11:59:52 +02001487 MD_OR_USE_PSA_INIT();
Janos Follath822b2c32015-10-11 10:25:22 +02001488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 while ((act = mystrsep(&chain_paths, " ")) != NULL) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001490 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, act), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 }
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001492 TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, trusted_ca), 0);
Janos Follath822b2c32015-10-11 10:25:22 +02001493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 if (strcmp(profile_name, "") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001495 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 } else if (strcmp(profile_name, "next") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001497 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 } else if (strcmp(profile_name, "suiteb") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001499 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 } else if (strcmp(profile_name, "rsa3072") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001501 profile = &profile_rsa3072;
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 } else if (strcmp(profile_name, "sha512") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001503 profile = &profile_sha512;
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 }
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1507 NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
Janos Follathef4f2582015-10-11 16:17:27 +02001508
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001509 TEST_EQUAL(res, (result));
1510 TEST_EQUAL(flags, (uint32_t) (flags_result));
Janos Follath822b2c32015-10-11 10:25:22 +02001511
1512exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 mbedtls_x509_crt_free(&trusted);
1514 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001515 MD_OR_USE_PSA_DONE();
Janos Follath822b2c32015-10-11 10:25:22 +02001516}
1517/* END_CASE */
1518
Gilles Peskine02ec5852025-05-12 20:52:07 +02001519/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001520void x509_oid_desc(data_t *buf, char *ref_desc)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001521{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001523 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001524 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001525
Valerio Setti569c1712023-04-19 14:53:36 +02001526 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001529 oid.p = buf->x;
1530 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001531
Gilles Peskine532e3ee2025-05-07 20:37:15 +02001532 ret = mbedtls_x509_oid_get_extended_key_usage(&oid, &desc);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 if (strcmp(ref_desc, "notfound") == 0) {
1535 TEST_ASSERT(ret != 0);
1536 TEST_ASSERT(desc == NULL);
1537 } else {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001538 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 TEST_ASSERT(desc != NULL);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001540 TEST_EQUAL(strcmp(desc, ref_desc), 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001541 }
valerio32f2ac92023-04-20 11:59:52 +02001542
1543exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001544 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001545}
1546/* END_CASE */
1547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001549void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001550{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001552 char num_buf[100];
1553
Valerio Setti569c1712023-04-19 14:53:36 +02001554 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001555
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001556 memset(num_buf, 0x2a, sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001559 oid.p = oid_buf->x;
1560 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001561
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001562 TEST_ASSERT((size_t) blen <= sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001563
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001564 TEST_EQUAL(mbedtls_oid_get_numeric_string(num_buf, blen, &oid), ret);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 if (ret >= 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001567 TEST_EQUAL(num_buf[ret], 0);
1568 TEST_EQUAL(strcmp(num_buf, numstr), 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001569 }
valerio32f2ac92023-04-20 11:59:52 +02001570
1571exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001572 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001573}
1574/* END_CASE */
1575
TRodziewicz442fdc22021-06-07 13:52:23 +02001576/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001577void x509_check_key_usage(char *crt_file, int usage, int ret)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001578{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001582 USE_PSA_INIT();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001583
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001584 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001585
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001586 TEST_EQUAL(mbedtls_x509_crt_check_key_usage(&crt, usage), ret);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001587
Paul Bakkerbd51b262014-07-10 15:26:12 +02001588exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001590 USE_PSA_DONE();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001591}
1592/* END_CASE */
1593
TRodziewicz442fdc22021-06-07 13:52:23 +02001594/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001595void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1596 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001597{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001601 USE_PSA_INIT();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001602
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001603 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001604
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001605 TEST_EQUAL(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x, oid->len),
1606 ret);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001607
Paul Bakkerbd51b262014-07-10 15:26:12 +02001608exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001610 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001611}
1612/* END_CASE */
1613
Andres AG4b76aec2016-09-23 13:16:02 +01001614/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001615void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1616 int day, int hour, int min, int sec)
Andres AG4b76aec2016-09-23 13:16:02 +01001617{
1618 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001619 unsigned char buf[21];
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 unsigned char *start = buf;
1621 unsigned char *end = buf;
Andres AG4b76aec2016-09-23 13:16:02 +01001622
Valerio Setti569c1712023-04-19 14:53:36 +02001623 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 memset(&time, 0x00, sizeof(time));
1625 *end = (unsigned char) tag; end++;
1626 *end = strlen(time_str);
1627 TEST_ASSERT(*end < 20);
Andres AG4b76aec2016-09-23 13:16:02 +01001628 end++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 memcpy(end, time_str, (size_t) *(end - 1));
Andres AG4b76aec2016-09-23 13:16:02 +01001630 end += *(end - 1);
1631
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001632 TEST_EQUAL(mbedtls_x509_get_time(&start, end, &time), ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 if (ret == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001634 TEST_EQUAL(year, time.year);
1635 TEST_EQUAL(mon, time.mon);
1636 TEST_EQUAL(day, time.day);
1637 TEST_EQUAL(hour, time.hour);
1638 TEST_EQUAL(min, time.min);
1639 TEST_EQUAL(sec, time.sec);
Andres AG4b76aec2016-09-23 13:16:02 +01001640 }
valerio32f2ac92023-04-20 11:59:52 +02001641exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001642 USE_PSA_DONE();
Andres AG4b76aec2016-09-23 13:16:02 +01001643}
1644/* END_CASE */
1645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001647void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1648 int ref_msg_md, int ref_mgf_md,
1649 int ref_salt_len, int ref_ret)
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001650{
1651 int my_ret;
Ronald Cronac6ae352020-06-26 14:33:03 +02001652 mbedtls_x509_buf buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001654 int my_salt_len;
1655
Valerio Setti569c1712023-04-19 14:53:36 +02001656 USE_PSA_INIT();
1657
Ronald Cronac6ae352020-06-26 14:33:03 +02001658 buf.p = params->x;
1659 buf.len = params->len;
1660 buf.tag = params_tag;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1663 &my_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001664
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001665 TEST_EQUAL(my_ret, ref_ret);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001666
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 if (ref_ret == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001668 TEST_EQUAL(my_msg_md, (mbedtls_md_type_t) ref_msg_md);
1669 TEST_EQUAL(my_mgf_md, (mbedtls_md_type_t) ref_mgf_md);
1670 TEST_EQUAL(my_salt_len, ref_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001671 }
1672
Paul Bakkerbd51b262014-07-10 15:26:12 +02001673exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001674 USE_PSA_DONE();
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001675}
1676/* END_CASE */
toth92ga41954d2021-02-12 16:11:17 +01001677
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001678/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001679void x509_crt_parse_subjectkeyid(char *file, data_t *subjectKeyId, int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001680{
1681 mbedtls_x509_crt crt;
toth92ga41954d2021-02-12 16:11:17 +01001682
1683 mbedtls_x509_crt_init(&crt);
Valerio Setti03a86e72025-05-28 12:01:14 +02001684 /* X509 relies on PK under the hood and the latter can use PSA to store keys
1685 * and perform operations so psa_crypto_init() must be called before. */
1686 USE_PSA_INIT();
toth92ga41954d2021-02-12 16:11:17 +01001687
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001688 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001689
toth92g357b2972021-05-04 15:41:35 +02001690 if (ref_ret == 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001691 TEST_EQUAL(crt.subject_key_id.tag, MBEDTLS_ASN1_OCTET_STRING);
1692 TEST_EQUAL(memcmp(crt.subject_key_id.p, subjectKeyId->x, subjectKeyId->len), 0);
1693 TEST_EQUAL(crt.subject_key_id.len, subjectKeyId->len);
toth92g357b2972021-05-04 15:41:35 +02001694 } else {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001695 TEST_EQUAL(crt.subject_key_id.tag, 0);
1696 TEST_EQUAL(crt.subject_key_id.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001697 }
1698
1699exit:
1700 mbedtls_x509_crt_free(&crt);
Valerio Setti03a86e72025-05-28 12:01:14 +02001701 USE_PSA_DONE();
toth92ga41954d2021-02-12 16:11:17 +01001702}
1703/* END_CASE */
1704
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001705/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Przemek Stekiel2568d472023-04-06 09:23:25 +02001706void x509_crt_parse_authoritykeyid(char *file,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001707 data_t *keyId,
toth92g5042b102021-05-06 08:22:17 +02001708 char *authorityKeyId_issuer,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001709 data_t *serial,
toth92g5042b102021-05-06 08:22:17 +02001710 int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001711{
1712 mbedtls_x509_crt crt;
Przemek Stekiel39dbe232023-04-03 10:19:22 +02001713 mbedtls_x509_subject_alternative_name san;
David Horstmann9a3a1a62023-06-22 16:59:09 +01001714 char name_buf[128];
toth92ga41954d2021-02-12 16:11:17 +01001715
1716 mbedtls_x509_crt_init(&crt);
Valerio Setti03a86e72025-05-28 12:01:14 +02001717 /* X509 relies on PK under the hood and the latter can use PSA to store keys
1718 * and perform operations so psa_crypto_init() must be called before. */
1719 USE_PSA_INIT();
toth92ga41954d2021-02-12 16:11:17 +01001720
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001721 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001722
toth92g357b2972021-05-04 15:41:35 +02001723 if (ref_ret == 0) {
toth92ga41954d2021-02-12 16:11:17 +01001724 /* KeyId test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001725 if (keyId->len > 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001726 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, MBEDTLS_ASN1_OCTET_STRING);
1727 TEST_EQUAL(memcmp(crt.authority_key_id.keyIdentifier.p, keyId->x, keyId->len), 0);
1728 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, keyId->len);
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001729 } else {
1730 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1731 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001732 }
toth92ga41954d2021-02-12 16:11:17 +01001733
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001734
toth92ga41954d2021-02-12 16:11:17 +01001735 /* Issuer test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001736 if (strlen(authorityKeyId_issuer) > 0) {
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001737 mbedtls_x509_sequence *issuerPtr = &crt.authority_key_id.authorityCertIssuer;
Przemek Stekiel01984212023-02-09 09:29:34 +01001738
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001739 TEST_EQUAL(mbedtls_x509_parse_subject_alt_name(&issuerPtr->buf, &san), 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001740
David Horstmann9a3a1a62023-06-22 16:59:09 +01001741 TEST_ASSERT(mbedtls_x509_dn_gets(name_buf, sizeof(name_buf),
1742 &san.san.directory_name)
1743 > 0);
1744 TEST_EQUAL(strcmp(name_buf, authorityKeyId_issuer), 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001745
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001746 mbedtls_x509_free_subject_alt_name(&san);
toth92ga41954d2021-02-12 16:11:17 +01001747 }
1748
1749 /* Serial test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001750 if (serial->len > 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001751 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001752 MBEDTLS_ASN1_INTEGER);
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001753 TEST_EQUAL(memcmp(crt.authority_key_id.authorityCertSerialNumber.p,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001754 serial->x, serial->len), 0);
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001755 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, serial->len);
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001756 } else {
1757 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1758 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001759 }
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001760
toth92g357b2972021-05-04 15:41:35 +02001761 } else {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001762 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1763 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001764
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001765 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1766 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001767 }
1768
1769exit:
1770 mbedtls_x509_crt_free(&crt);
Valerio Setti03a86e72025-05-28 12:01:14 +02001771 USE_PSA_DONE();
toth92ga41954d2021-02-12 16:11:17 +01001772}
1773/* END_CASE */
Sam Berry4aee6a22024-07-19 15:00:41 +01001774
Harry Ramsey94c30652024-09-19 16:13:32 +01001775/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Sam Berry4aee6a22024-07-19 15:00:41 +01001776void oid_get_numeric_string(data_t *oid, int error_ret, char *result_str)
1777{
1778 char buf[256];
1779 mbedtls_asn1_buf input_oid = { 0, 0, NULL };
1780 int ret;
1781
1782 input_oid.tag = MBEDTLS_ASN1_OID;
1783 /* Test that an empty OID is not dereferenced */
1784 input_oid.p = oid->len ? oid->x : (void *) 1;
1785 input_oid.len = oid->len;
1786
1787 ret = mbedtls_oid_get_numeric_string(buf, sizeof(buf), &input_oid);
1788
1789 if (error_ret == 0) {
1790 TEST_EQUAL(ret, strlen(result_str));
1791 TEST_ASSERT(ret >= 3);
1792 TEST_EQUAL(strcmp(buf, result_str), 0);
1793 } else {
1794 TEST_EQUAL(ret, error_ret);
1795 }
1796}
1797/* END_CASE */