blob: 9ee693e6654101dc844a053700b02958d4f850da [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);
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 mbedtls_ecp_set_max_ops(max_ops);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200685
686 cnt_restart = 0;
687 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
689 &mbedtls_x509_crt_profile_default, NULL, &flags,
690 NULL, NULL, &rs_ctx);
691 } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200692
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500693 TEST_EQUAL(ret, result);
694 TEST_EQUAL(flags, (uint32_t) flags_result);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 TEST_ASSERT(cnt_restart >= min_restart);
697 TEST_ASSERT(cnt_restart <= max_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200698
699 /* Do we leak memory when aborting? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
701 &mbedtls_x509_crt_profile_default, NULL, &flags,
702 NULL, NULL, &rs_ctx);
703 TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200704
705exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 mbedtls_x509_crt_restart_free(&rs_ctx);
707 mbedtls_x509_crt_free(&crt);
708 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100709 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200710}
711/* END_CASE */
712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714void x509_verify(char *crt_file, char *ca_file, char *crl_file,
715 char *cn_name_str, int result, int flags_result,
716 char *profile_str,
717 char *verify_callback)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000718{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_x509_crt crt;
720 mbedtls_x509_crt ca;
721 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200722 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000723 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200724 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 char *cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200726 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 mbedtls_x509_crt_init(&crt);
729 mbedtls_x509_crt_init(&ca);
730 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200731 MD_OR_USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 if (strcmp(cn_name_str, "NULL") != 0) {
Paul Bakker33b43f12013-08-20 11:48:36 +0200734 cn_name = cn_name_str;
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 if (strcmp(profile_str, "") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200738 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 } else if (strcmp(profile_str, "next") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200740 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 } else if (strcmp(profile_str, "suite_b") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200742 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 } else if (strcmp(profile_str, "compat") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200744 profile = &compat_profile;
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 } else if (strcmp(profile_str, "all") == 0) {
Hanno Becker20a4ade2019-06-03 14:27:03 +0100746 profile = &profile_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100748 TEST_FAIL("Unknown algorithm profile");
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 }
Gilles Peskineef86ab22017-05-05 18:59:02 +0200750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 if (strcmp(verify_callback, "NULL") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200752 f_vrfy = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 } else if (strcmp(verify_callback, "verify_none") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200754 f_vrfy = verify_none;
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 } else if (strcmp(verify_callback, "verify_all") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200756 f_vrfy = verify_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100758 TEST_FAIL("No known verify callback selected");
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200760
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500761 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
762 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
763 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 res = mbedtls_x509_crt_verify_with_profile(&crt,
766 &ca,
767 &crl,
768 profile,
769 cn_name,
770 &flags,
771 f_vrfy,
772 NULL);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 TEST_EQUAL(res, result);
775 TEST_EQUAL(flags, (uint32_t) flags_result);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200776
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200777#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker0350d562019-03-28 14:23:36 +0000778 /* CRLs aren't supported with CA callbacks, so skip the CA callback
Jarno Lamsadfd22c42019-04-01 15:18:53 +0300779 * version of the test if CRLs are in use. */
Gilles Peskinebb7d92c2023-10-17 17:26:44 +0200780 if (strcmp(crl_file, "") == 0) {
Hanno Becker0350d562019-03-28 14:23:36 +0000781 flags = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 res = mbedtls_x509_crt_verify_with_ca_cb(&crt,
784 ca_callback,
785 &ca,
786 profile,
787 cn_name,
788 &flags,
789 f_vrfy,
790 NULL);
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200791
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500792 TEST_EQUAL(res, result);
793 TEST_EQUAL(flags, (uint32_t) (flags_result));
Hanno Becker0350d562019-03-28 14:23:36 +0000794 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200795#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200796exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 mbedtls_x509_crt_free(&crt);
798 mbedtls_x509_crt_free(&ca);
799 mbedtls_x509_crl_free(&crl);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100800 MD_OR_USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000801}
Paul Bakker33b43f12013-08-20 11:48:36 +0200802/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000803
Jarno Lamsa557426a2019-03-27 17:08:29 +0200804/* 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 +0100805void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
806 int exp_ret)
Jarno Lamsa557426a2019-03-27 17:08:29 +0200807{
808 int ret;
809 mbedtls_x509_crt crt;
810 mbedtls_x509_crt ca;
811 uint32_t flags = 0;
Hanno Becker3f932bb2019-03-28 17:06:47 +0000812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 mbedtls_x509_crt_init(&crt);
814 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200815 USE_PSA_INIT();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200816
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500817 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
818 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (strcmp(name, "NULL") == 0) {
Jarno Lamsa557426a2019-03-27 17:08:29 +0200821 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca,
825 &compat_profile, name, &flags,
826 NULL, NULL);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200827
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500828 TEST_EQUAL(ret, exp_ret);
829 TEST_EQUAL(flags, (uint32_t) (-1));
Jarno Lamsa557426a2019-03-27 17:08:29 +0200830exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 mbedtls_x509_crt_free(&crt);
832 mbedtls_x509_crt_free(&ca);
Valerio Setti569c1712023-04-19 14:53:36 +0200833 USE_PSA_DONE();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200834}
835/* END_CASE */
836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100838void x509_verify_callback(char *crt_file, char *ca_file, char *name,
839 int exp_ret, char *exp_vrfy_out)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200840{
841 int ret;
842 mbedtls_x509_crt crt;
843 mbedtls_x509_crt ca;
844 uint32_t flags = 0;
845 verify_print_context vrfy_ctx;
846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 mbedtls_x509_crt_init(&crt);
848 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200849 MD_OR_USE_PSA_INIT();
850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 verify_print_init(&vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200852
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500853 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
854 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200855
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 if (strcmp(name, "NULL") == 0) {
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200857 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 }
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL,
861 &compat_profile,
862 name, &flags,
863 verify_print, &vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200864
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500865 TEST_EQUAL(ret, exp_ret);
866 TEST_EQUAL(strcmp(vrfy_ctx.buf, exp_vrfy_out), 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200867
868exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 mbedtls_x509_crt_free(&crt);
870 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100871 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200872}
873/* END_CASE */
874
Hanno Becker612a2f12020-10-09 09:19:39 +0100875/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100876void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
877 char *new_subject_ou,
878 char *result_str,
879 int ret)
Werner Lewis31ecb962022-06-17 15:51:55 +0100880{
881 mbedtls_x509_crt crt;
882 char buf[2000];
883 int res = 0;
884
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200886 USE_PSA_INIT();
887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 memset(buf, 0, 2000);
Werner Lewis31ecb962022-06-17 15:51:55 +0100889
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500890 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100891 crt.subject.next->val.p = (unsigned char *) new_subject_ou;
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 crt.subject.next->val.len = strlen(new_subject_ou);
Werner Lewis31ecb962022-06-17 15:51:55 +0100893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
Werner Lewis31ecb962022-06-17 15:51:55 +0100895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 if (ret != 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500897 TEST_EQUAL(res, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 } else {
899 TEST_ASSERT(res != -1);
900 TEST_ASSERT(res != -2);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500901 TEST_EQUAL(strcmp(buf, result_str), 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100902 }
903exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200905 USE_PSA_DONE();
Werner Lewis31ecb962022-06-17 15:51:55 +0100906}
907/* END_CASE */
908
909/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100910void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000911{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000913 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200914 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000915
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200917 USE_PSA_INIT();
918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000920
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500921 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 if (strcmp(entity, "subject") == 0) {
923 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
924 } else if (strcmp(entity, "issuer") == 0) {
925 res = mbedtls_x509_dn_gets(buf, 2000, &crt.issuer);
926 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100927 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000929
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 TEST_ASSERT(res != -1);
931 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000932
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500933 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200934
935exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200937 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000938}
Paul Bakker33b43f12013-08-20 11:48:36 +0200939/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000940
David Horstmanndb73d3b2022-10-04 16:49:16 +0100941/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100942void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
David Horstmanndb73d3b2022-10-04 16:49:16 +0100943{
valerioe50831c2023-04-20 14:48:19 +0200944 unsigned char *name = NULL;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100945 unsigned char *p;
946 size_t name_len;
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100947 mbedtls_x509_name head;
David Horstmann3cd67582022-10-17 17:59:10 +0100948 int ret;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100949
Valerio Setti569c1712023-04-19 14:53:36 +0200950 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 memset(&head, 0, sizeof(head));
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100954 p = name;
955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 ret = mbedtls_x509_get_name(&p, (name + name_len), &head);
957 if (ret == 0) {
958 mbedtls_asn1_free_named_data_list_shallow(head.next);
959 }
David Horstmanndb73d3b2022-10-04 16:49:16 +0100960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 TEST_EQUAL(ret, exp_ret);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100962
valerio32f2ac92023-04-20 11:59:52 +0200963exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 mbedtls_free(name);
Valerio Setti569c1712023-04-19 14:53:36 +0200965 USE_PSA_DONE();
David Horstmanndb73d3b2022-10-04 16:49:16 +0100966}
967/* END_CASE */
968
Werner Lewisb3acb052022-06-17 15:59:58 +0100969/* 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 +0100970void mbedtls_x509_dn_get_next(char *name_str,
971 int next_merged,
972 char *expected_oids,
973 int exp_count,
974 char *exp_dn_gets)
Werner Lewisb3acb052022-06-17 15:59:58 +0100975{
976 int ret = 0, i;
Werner Lewisac80a662022-06-23 11:58:02 +0100977 size_t len = 0, out_size;
Werner Lewisb3acb052022-06-17 15:59:58 +0100978 mbedtls_asn1_named_data *names = NULL;
Gilles Peskine21e46b32023-10-17 16:35:20 +0200979 mbedtls_x509_name parsed;
980 memset(&parsed, 0, sizeof(parsed));
981 mbedtls_x509_name *parsed_cur;
Werner Lewisac80a662022-06-23 11:58:02 +0100982 // Size of buf is maximum required for test cases
Gilles Peskinef2574202023-10-18 17:39:48 +0200983 unsigned char buf[80] = { 0 };
Gilles Peskine21e46b32023-10-17 16:35:20 +0200984 unsigned char *out = NULL;
985 unsigned char *c = buf + sizeof(buf);
Werner Lewisb3acb052022-06-17 15:59:58 +0100986 const char *short_name;
987
Valerio Setti569c1712023-04-19 14:53:36 +0200988 USE_PSA_INIT();
Gilles Peskine21e46b32023-10-17 16:35:20 +0200989
Werner Lewisac80a662022-06-23 11:58:02 +0100990 // Additional size required for trailing space
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 out_size = strlen(expected_oids) + 2;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100992 TEST_CALLOC(out, out_size);
Werner Lewisb3acb052022-06-17 15:59:58 +0100993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 TEST_EQUAL(mbedtls_x509_string_to_names(&names, name_str), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100995
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 ret = mbedtls_x509_write_names(&c, buf, names);
997 TEST_LE_S(0, ret);
Werner Lewisb3acb052022-06-17 15:59:58 +0100998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
1000 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
1001 TEST_EQUAL(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +01001002
1003 // Iterate over names and set next_merged nodes
1004 parsed_cur = &parsed;
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 for (; next_merged != 0 && parsed_cur != NULL; next_merged = next_merged >> 1) {
Werner Lewis12657cd2022-06-20 11:47:57 +01001006 parsed_cur->next_merged = next_merged & 0x01;
Werner Lewisb3acb052022-06-17 15:59:58 +01001007 parsed_cur = parsed_cur->next;
1008 }
1009
1010 // Iterate over RDN nodes and print OID of first element to buffer
1011 parsed_cur = &parsed;
1012 len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 for (i = 0; parsed_cur != NULL; i++) {
Gilles Peskine532e3ee2025-05-07 20:37:15 +02001014 TEST_EQUAL(mbedtls_x509_oid_get_attr_short_name(&parsed_cur->oid,
1015 &short_name), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 len += mbedtls_snprintf((char *) out + len, out_size - len, "%s ", short_name);
1017 parsed_cur = mbedtls_x509_dn_get_next(parsed_cur);
Werner Lewisb3acb052022-06-17 15:59:58 +01001018 }
1019 out[len-1] = 0;
1020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 TEST_EQUAL(exp_count, i);
1022 TEST_EQUAL(strcmp((char *) out, expected_oids), 0);
1023 mbedtls_free(out);
Werner Lewisac80a662022-06-23 11:58:02 +01001024 out = NULL;
Werner Lewisb3acb052022-06-17 15:59:58 +01001025
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 out_size = strlen(exp_dn_gets) + 1;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001027 TEST_CALLOC(out, out_size);
Werner Lewisac80a662022-06-23 11:58:02 +01001028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 TEST_LE_S(0, mbedtls_x509_dn_gets((char *) out, out_size, &parsed));
1030 TEST_EQUAL(strcmp((char *) out, exp_dn_gets), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +01001031exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 mbedtls_free(out);
1033 mbedtls_asn1_free_named_data_list(&names);
1034 mbedtls_asn1_free_named_data_list_shallow(parsed.next);
Valerio Setti569c1712023-04-19 14:53:36 +02001035 USE_PSA_DONE();
Werner Lewisb3acb052022-06-17 15:59:58 +01001036}
Werner Lewisb3acb052022-06-17 15:59:58 +01001037/* END_CASE */
1038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001040void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
Paul Bakker37940d9f2009-07-10 22:38:58 +00001041{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +00001043
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001045 USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +00001046
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001047 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if (strcmp(entity, "valid_from") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001050 TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_from), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 } else if (strcmp(entity, "valid_to") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001052 TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_to), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +01001054 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001056
Paul Bakkerbd51b262014-07-10 15:26:12 +02001057exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001059 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +00001060}
Paul Bakker33b43f12013-08-20 11:48:36 +02001061/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +00001062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001064void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001065{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001067
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001069 USE_PSA_INIT();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001070
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001071 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 if (strcmp(entity, "valid_from") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001074 TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_from), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 } else if (strcmp(entity, "valid_to") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001076 TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_to), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +01001078 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001080
Paul Bakkerbd51b262014-07-10 15:26:12 +02001081exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001083 USE_PSA_DONE();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001084}
1085/* END_CASE */
1086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001088void x509parse_crt_file(char *crt_file, int result)
Paul Bakker5a5fa922014-09-26 14:53:04 +02001089{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +02001091
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001093 USE_PSA_INIT();
Paul Bakker5a5fa922014-09-26 14:53:04 +02001094
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001095 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), result);
Paul Bakker5a5fa922014-09-26 14:53:04 +02001096
1097exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001099 USE_PSA_DONE();
Paul Bakker5a5fa922014-09-26 14:53:04 +02001100}
1101/* END_CASE */
1102
Minos Galanakisa83ada42024-01-19 10:59:07 +00001103/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
1104void mbedtls_x509_get_ca_istrue(char *crt_file, int result)
1105{
1106 mbedtls_x509_crt crt;
1107 mbedtls_x509_crt_init(&crt);
1108 USE_PSA_INIT();
1109
1110 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
1111 TEST_EQUAL(mbedtls_x509_crt_get_ca_istrue(&crt), result);
1112exit:
1113 mbedtls_x509_crt_free(&crt);
1114 USE_PSA_DONE();
1115}
1116/* END_CASE */
1117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001119void x509parse_crt(data_t *buf, char *result_str, int result)
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001120{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 mbedtls_x509_crt crt;
Hanno Becker612a2f12020-10-09 09:19:39 +01001122#if !defined(MBEDTLS_X509_REMOVE_INFO)
Hanno Beckerfff2d572020-10-09 09:45:19 +01001123 unsigned char output[2000] = { 0 };
Azim Khanf1aaec92017-05-30 14:23:15 +01001124 int res;
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001125#else
1126 ((void) result_str);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001127#endif
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001130 USE_PSA_INIT();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001131
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001132 TEST_EQUAL(mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len), result);
Hanno Becker612a2f12020-10-09 09:19:39 +01001133#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 if ((result) == 0) {
1135 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1136 TEST_ASSERT(res != -1);
1137 TEST_ASSERT(res != -2);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001138
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001139 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001140 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001142#endif
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 mbedtls_x509_crt_free(&crt);
1145 mbedtls_x509_crt_init(&crt);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001146
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001147 TEST_EQUAL(mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len), result);
Hanno Becker612a2f12020-10-09 09:19:39 +01001148#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 if ((result) == 0) {
1150 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakker33b43f12013-08-20 11:48:36 +02001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 TEST_ASSERT(res != -1);
1155 TEST_ASSERT(res != -2);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001156
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001157 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001158 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001160#endif /* !MBEDTLS_X509_REMOVE_INFO */
Paul Bakkerb08e6842012-02-11 18:43:20 +00001161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 mbedtls_x509_crt_free(&crt);
1163 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001164
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001165 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL, NULL),
1166 result);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001167#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 if ((result) == 0) {
1169 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 TEST_ASSERT(res != -1);
1172 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001173
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001174 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001175 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001177#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 mbedtls_x509_crt_free(&crt);
1180 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001181
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001182 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL, NULL),
1183 result);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001184#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 if ((result) == 0) {
1186 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001187
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 TEST_ASSERT(res != -1);
1189 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001190
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001191 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001192 }
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001193#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001194
1195exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001197 USE_PSA_DONE();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001198}
1199/* END_CASE */
1200
1201/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001202void x509parse_crt_cb(data_t *buf, char *result_str, int result)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001203{
1204 mbedtls_x509_crt crt;
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001205 mbedtls_x509_buf oid;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001206
1207#if !defined(MBEDTLS_X509_REMOVE_INFO)
1208 unsigned char output[2000] = { 0 };
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001209 int res;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001210#else
1211 ((void) result_str);
1212#endif
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001213
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001214 oid.tag = MBEDTLS_ASN1_OID;
1215 oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001219 USE_PSA_INIT();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001220
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001221 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
1222 &oid), result);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001223#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if ((result) == 0) {
1225 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 TEST_ASSERT(res != -1);
1228 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001229
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001230 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001231 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 memset(output, 0, 2000);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001233#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 mbedtls_x509_crt_free(&crt);
1236 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001237
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001238 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1239 &oid), (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001240#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 if ((result) == 0) {
1242 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001243
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 TEST_ASSERT(res != -1);
1245 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001246
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001247 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001248 }
Hanno Beckerfff2d572020-10-09 09:45:19 +01001249#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001250
Paul Bakkerbd51b262014-07-10 15:26:12 +02001251exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001253 USE_PSA_DONE();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001254}
Paul Bakker33b43f12013-08-20 11:48:36 +02001255/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001256
Hanno Becker612a2f12020-10-09 09:19:39 +01001257/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001258void x509parse_crl(data_t *buf, char *result_str, int result)
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001259{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001261 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +01001262 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +02001265 USE_PSA_INIT();
1266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 memset(output, 0, 2000);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001268
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001269
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001270 TEST_EQUAL(mbedtls_x509_crl_parse(&crl, buf->x, buf->len), (result));
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 if ((result) == 0) {
1272 res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
Paul Bakker33b43f12013-08-20 11:48:36 +02001273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 TEST_ASSERT(res != -1);
1275 TEST_ASSERT(res != -2);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001276
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001277 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001278 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001279
Paul Bakkerbd51b262014-07-10 15:26:12 +02001280exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +02001282 USE_PSA_DONE();
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001283}
Paul Bakker33b43f12013-08-20 11:48:36 +02001284/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001285
Hanno Becker612a2f12020-10-09 09:19:39 +01001286/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001287void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001288{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001290 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001291 int my_ret;
1292
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001294 USE_PSA_INIT();
1295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 memset(my_out, 0, sizeof(my_out));
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001299 TEST_EQUAL(my_ret, ref_ret);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 if (ref_ret == 0) {
1302 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001303 TEST_EQUAL(my_out_len, strlen(ref_out));
1304 TEST_EQUAL(strcmp(my_out, ref_out), 0);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001305 }
1306
Paul Bakkerbd51b262014-07-10 15:26:12 +02001307exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001309 USE_PSA_DONE();
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001310}
1311/* END_CASE */
1312
Matthias Schulzab408222023-10-18 13:20:59 +02001313/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1314void mbedtls_x509_csr_parse_with_ext_cb(data_t *csr_der, char *ref_out, int ref_ret, int accept)
1315{
1316 mbedtls_x509_csr csr;
1317 char my_out[1000];
1318 int my_ret;
1319
1320 mbedtls_x509_csr_init(&csr);
1321 USE_PSA_INIT();
1322
1323 memset(my_out, 0, sizeof(my_out));
1324
1325 my_ret = mbedtls_x509_csr_parse_der_with_ext_cb(&csr, csr_der->x, csr_der->len,
1326 accept ? parse_csr_ext_accept_cb :
Matthias Schulzedc32ea2023-10-19 16:09:08 +02001327 parse_csr_ext_reject_cb,
Matthias Schulzab408222023-10-18 13:20:59 +02001328 NULL);
1329 TEST_EQUAL(my_ret, ref_ret);
1330
1331 if (ref_ret == 0) {
1332 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1333 TEST_EQUAL(my_out_len, strlen(ref_out));
1334 TEST_EQUAL(strcmp(my_out, ref_out), 0);
1335 }
1336
1337exit:
1338 mbedtls_x509_csr_free(&csr);
1339 USE_PSA_DONE();
1340}
1341/* END_CASE */
1342
Przemek Stekield7992df2023-01-25 16:19:50 +01001343/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1344void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
1345{
1346 mbedtls_x509_csr csr;
1347 char my_out[1000];
1348 int my_ret;
1349
1350 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001351 USE_PSA_INIT();
1352
Przemek Stekield7992df2023-01-25 16:19:50 +01001353 memset(my_out, 0, sizeof(my_out));
1354
1355 my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001356 TEST_EQUAL(my_ret, ref_ret);
Przemek Stekield7992df2023-01-25 16:19:50 +01001357
1358 if (ref_ret == 0) {
1359 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001360 TEST_EQUAL(my_out_len, strlen(ref_out));
1361 TEST_EQUAL(strcmp(my_out, ref_out), 0);
Przemek Stekield7992df2023-01-25 16:19:50 +01001362 }
1363
1364exit:
1365 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001366 USE_PSA_DONE();
Przemek Stekield7992df2023-01-25 16:19:50 +01001367}
1368/* END_CASE */
1369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1e5fec62023-04-13 18:13:48 +02001371void mbedtls_x509_crt_parse_file(char *crt_path, int ret, int nb_crt)
1372{
1373 mbedtls_x509_crt chain, *cur;
1374 int i;
1375
1376 mbedtls_x509_crt_init(&chain);
1377 USE_PSA_INIT();
1378
1379 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, crt_path), ret);
1380
1381 /* Check how many certs we got */
1382 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1383 if (cur->raw.p != NULL) {
1384 i++;
1385 }
1386 }
1387
1388 TEST_EQUAL(i, nb_crt);
1389
1390exit:
1391 mbedtls_x509_crt_free(&chain);
1392 USE_PSA_DONE();
1393}
1394/* END_CASE */
1395
1396/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001397void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001398{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001400 int i;
1401
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001403 USE_PSA_INIT();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001404
Gilles Peskine55ad28a2023-04-13 18:14:45 +02001405 TEST_EQUAL(mbedtls_x509_crt_parse_path(&chain, crt_path), ret);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001406
1407 /* Check how many certs we got */
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1409 if (cur->raw.p != NULL) {
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001410 i++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 }
1412 }
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001413
Gilles Peskine55ad28a2023-04-13 18:14:45 +02001414 TEST_EQUAL(i, nb_crt);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001415
Paul Bakkerbd51b262014-07-10 15:26:12 +02001416exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 mbedtls_x509_crt_free(&chain);
Valerio Setti569c1712023-04-19 14:53:36 +02001418 USE_PSA_DONE();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001419}
1420/* END_CASE */
1421
Janos Follath822b2c32015-10-11 10:25:22 +02001422/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001423void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1424 int ret_chk, int flags_chk)
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001425{
1426 char file_buf[128];
1427 int ret;
1428 uint32_t flags;
1429 mbedtls_x509_crt trusted, chain;
1430
1431 /*
1432 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1433 * with NN.crt signed by NN-1.crt
1434 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 mbedtls_x509_crt_init(&trusted);
1436 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001437 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001438
1439 /* Load trusted root */
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001440 TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, ca_file), 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001441
1442 /* Load a chain with nb_int intermediates (from 01 to nb_int),
1443 * plus one "end-entity" cert (nb_int + 1) */
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001444 ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 nb_int + 1);
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001446 TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001447 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, file_buf), 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001448
1449 /* Try to verify that chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1451 NULL, NULL);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001452 TEST_EQUAL(ret, ret_chk);
1453 TEST_EQUAL(flags, (uint32_t) flags_chk);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001454
1455exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 mbedtls_x509_crt_free(&chain);
1457 mbedtls_x509_crt_free(&trusted);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001458 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001459}
1460/* END_CASE */
1461
1462/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001463void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1464 int flags_result, int result,
1465 char *profile_name, int vrfy_fatal_lvls)
Janos Follath822b2c32015-10-11 10:25:22 +02001466{
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 char *act;
Janos Follath822b2c32015-10-11 10:25:22 +02001468 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001469 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001470 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001471 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +02001472
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 mbedtls_x509_crt_init(&chain);
1474 mbedtls_x509_crt_init(&trusted);
valerio32f2ac92023-04-20 11:59:52 +02001475 MD_OR_USE_PSA_INIT();
Janos Follath822b2c32015-10-11 10:25:22 +02001476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 while ((act = mystrsep(&chain_paths, " ")) != NULL) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001478 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, act), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 }
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001480 TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, trusted_ca), 0);
Janos Follath822b2c32015-10-11 10:25:22 +02001481
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 if (strcmp(profile_name, "") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001483 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 } else if (strcmp(profile_name, "next") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001485 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 } else if (strcmp(profile_name, "suiteb") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001487 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 } else if (strcmp(profile_name, "rsa3072") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001489 profile = &profile_rsa3072;
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 } else if (strcmp(profile_name, "sha512") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001491 profile = &profile_sha512;
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 }
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1495 NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
Janos Follathef4f2582015-10-11 16:17:27 +02001496
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001497 TEST_EQUAL(res, (result));
1498 TEST_EQUAL(flags, (uint32_t) (flags_result));
Janos Follath822b2c32015-10-11 10:25:22 +02001499
1500exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 mbedtls_x509_crt_free(&trusted);
1502 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001503 MD_OR_USE_PSA_DONE();
Janos Follath822b2c32015-10-11 10:25:22 +02001504}
1505/* END_CASE */
1506
Gilles Peskine02ec5852025-05-12 20:52:07 +02001507/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001508void x509_oid_desc(data_t *buf, char *ref_desc)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001509{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001511 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001512 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001513
Valerio Setti569c1712023-04-19 14:53:36 +02001514 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001517 oid.p = buf->x;
1518 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001519
Gilles Peskine532e3ee2025-05-07 20:37:15 +02001520 ret = mbedtls_x509_oid_get_extended_key_usage(&oid, &desc);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 if (strcmp(ref_desc, "notfound") == 0) {
1523 TEST_ASSERT(ret != 0);
1524 TEST_ASSERT(desc == NULL);
1525 } else {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001526 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 TEST_ASSERT(desc != NULL);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001528 TEST_EQUAL(strcmp(desc, ref_desc), 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001529 }
valerio32f2ac92023-04-20 11:59:52 +02001530
1531exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001532 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001533}
1534/* END_CASE */
1535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001537void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001538{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001540 char num_buf[100];
1541
Valerio Setti569c1712023-04-19 14:53:36 +02001542 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001543
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001544 memset(num_buf, 0x2a, sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001547 oid.p = oid_buf->x;
1548 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001549
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001550 TEST_ASSERT((size_t) blen <= sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001551
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001552 TEST_EQUAL(mbedtls_oid_get_numeric_string(num_buf, blen, &oid), ret);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001553
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 if (ret >= 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001555 TEST_EQUAL(num_buf[ret], 0);
1556 TEST_EQUAL(strcmp(num_buf, numstr), 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001557 }
valerio32f2ac92023-04-20 11:59:52 +02001558
1559exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001560 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001561}
1562/* END_CASE */
1563
TRodziewicz442fdc22021-06-07 13:52:23 +02001564/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001565void x509_check_key_usage(char *crt_file, int usage, int ret)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001566{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001568
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001570 USE_PSA_INIT();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001571
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001572 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001573
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001574 TEST_EQUAL(mbedtls_x509_crt_check_key_usage(&crt, usage), ret);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001575
Paul Bakkerbd51b262014-07-10 15:26:12 +02001576exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001578 USE_PSA_DONE();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001579}
1580/* END_CASE */
1581
TRodziewicz442fdc22021-06-07 13:52:23 +02001582/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001583void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1584 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001585{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001589 USE_PSA_INIT();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001590
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001591 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001592
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001593 TEST_EQUAL(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x, oid->len),
1594 ret);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001595
Paul Bakkerbd51b262014-07-10 15:26:12 +02001596exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001598 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001599}
1600/* END_CASE */
1601
Andres AG4b76aec2016-09-23 13:16:02 +01001602/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001603void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1604 int day, int hour, int min, int sec)
Andres AG4b76aec2016-09-23 13:16:02 +01001605{
1606 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001607 unsigned char buf[21];
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 unsigned char *start = buf;
1609 unsigned char *end = buf;
Andres AG4b76aec2016-09-23 13:16:02 +01001610
Valerio Setti569c1712023-04-19 14:53:36 +02001611 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 memset(&time, 0x00, sizeof(time));
1613 *end = (unsigned char) tag; end++;
1614 *end = strlen(time_str);
1615 TEST_ASSERT(*end < 20);
Andres AG4b76aec2016-09-23 13:16:02 +01001616 end++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 memcpy(end, time_str, (size_t) *(end - 1));
Andres AG4b76aec2016-09-23 13:16:02 +01001618 end += *(end - 1);
1619
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001620 TEST_EQUAL(mbedtls_x509_get_time(&start, end, &time), ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 if (ret == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001622 TEST_EQUAL(year, time.year);
1623 TEST_EQUAL(mon, time.mon);
1624 TEST_EQUAL(day, time.day);
1625 TEST_EQUAL(hour, time.hour);
1626 TEST_EQUAL(min, time.min);
1627 TEST_EQUAL(sec, time.sec);
Andres AG4b76aec2016-09-23 13:16:02 +01001628 }
valerio32f2ac92023-04-20 11:59:52 +02001629exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001630 USE_PSA_DONE();
Andres AG4b76aec2016-09-23 13:16:02 +01001631}
1632/* END_CASE */
1633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001635void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1636 int ref_msg_md, int ref_mgf_md,
1637 int ref_salt_len, int ref_ret)
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001638{
1639 int my_ret;
Ronald Cronac6ae352020-06-26 14:33:03 +02001640 mbedtls_x509_buf buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001642 int my_salt_len;
1643
Valerio Setti569c1712023-04-19 14:53:36 +02001644 USE_PSA_INIT();
1645
Ronald Cronac6ae352020-06-26 14:33:03 +02001646 buf.p = params->x;
1647 buf.len = params->len;
1648 buf.tag = params_tag;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001649
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1651 &my_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001652
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001653 TEST_EQUAL(my_ret, ref_ret);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001654
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 if (ref_ret == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001656 TEST_EQUAL(my_msg_md, (mbedtls_md_type_t) ref_msg_md);
1657 TEST_EQUAL(my_mgf_md, (mbedtls_md_type_t) ref_mgf_md);
1658 TEST_EQUAL(my_salt_len, ref_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001659 }
1660
Paul Bakkerbd51b262014-07-10 15:26:12 +02001661exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001662 USE_PSA_DONE();
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001663}
1664/* END_CASE */
toth92ga41954d2021-02-12 16:11:17 +01001665
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001666/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001667void x509_crt_parse_subjectkeyid(char *file, data_t *subjectKeyId, int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001668{
1669 mbedtls_x509_crt crt;
toth92ga41954d2021-02-12 16:11:17 +01001670
1671 mbedtls_x509_crt_init(&crt);
1672
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001673 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001674
toth92g357b2972021-05-04 15:41:35 +02001675 if (ref_ret == 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001676 TEST_EQUAL(crt.subject_key_id.tag, MBEDTLS_ASN1_OCTET_STRING);
1677 TEST_EQUAL(memcmp(crt.subject_key_id.p, subjectKeyId->x, subjectKeyId->len), 0);
1678 TEST_EQUAL(crt.subject_key_id.len, subjectKeyId->len);
toth92g357b2972021-05-04 15:41:35 +02001679 } else {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001680 TEST_EQUAL(crt.subject_key_id.tag, 0);
1681 TEST_EQUAL(crt.subject_key_id.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001682 }
1683
1684exit:
1685 mbedtls_x509_crt_free(&crt);
1686}
1687/* END_CASE */
1688
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001689/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Przemek Stekiel2568d472023-04-06 09:23:25 +02001690void x509_crt_parse_authoritykeyid(char *file,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001691 data_t *keyId,
toth92g5042b102021-05-06 08:22:17 +02001692 char *authorityKeyId_issuer,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001693 data_t *serial,
toth92g5042b102021-05-06 08:22:17 +02001694 int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001695{
1696 mbedtls_x509_crt crt;
Przemek Stekiel39dbe232023-04-03 10:19:22 +02001697 mbedtls_x509_subject_alternative_name san;
David Horstmann9a3a1a62023-06-22 16:59:09 +01001698 char name_buf[128];
toth92ga41954d2021-02-12 16:11:17 +01001699
1700 mbedtls_x509_crt_init(&crt);
1701
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001702 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001703
toth92g357b2972021-05-04 15:41:35 +02001704 if (ref_ret == 0) {
toth92ga41954d2021-02-12 16:11:17 +01001705 /* KeyId test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001706 if (keyId->len > 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001707 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, MBEDTLS_ASN1_OCTET_STRING);
1708 TEST_EQUAL(memcmp(crt.authority_key_id.keyIdentifier.p, keyId->x, keyId->len), 0);
1709 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, keyId->len);
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001710 } else {
1711 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1712 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001713 }
toth92ga41954d2021-02-12 16:11:17 +01001714
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001715
toth92ga41954d2021-02-12 16:11:17 +01001716 /* Issuer test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001717 if (strlen(authorityKeyId_issuer) > 0) {
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001718 mbedtls_x509_sequence *issuerPtr = &crt.authority_key_id.authorityCertIssuer;
Przemek Stekiel01984212023-02-09 09:29:34 +01001719
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001720 TEST_EQUAL(mbedtls_x509_parse_subject_alt_name(&issuerPtr->buf, &san), 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001721
David Horstmann9a3a1a62023-06-22 16:59:09 +01001722 TEST_ASSERT(mbedtls_x509_dn_gets(name_buf, sizeof(name_buf),
1723 &san.san.directory_name)
1724 > 0);
1725 TEST_EQUAL(strcmp(name_buf, authorityKeyId_issuer), 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001726
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001727 mbedtls_x509_free_subject_alt_name(&san);
toth92ga41954d2021-02-12 16:11:17 +01001728 }
1729
1730 /* Serial test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001731 if (serial->len > 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001732 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001733 MBEDTLS_ASN1_INTEGER);
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001734 TEST_EQUAL(memcmp(crt.authority_key_id.authorityCertSerialNumber.p,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001735 serial->x, serial->len), 0);
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001736 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, serial->len);
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001737 } else {
1738 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1739 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001740 }
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001741
toth92g357b2972021-05-04 15:41:35 +02001742 } else {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001743 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1744 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001745
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001746 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1747 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001748 }
1749
1750exit:
1751 mbedtls_x509_crt_free(&crt);
1752}
1753/* END_CASE */
Sam Berry4aee6a22024-07-19 15:00:41 +01001754
Harry Ramsey94c30652024-09-19 16:13:32 +01001755/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Sam Berry4aee6a22024-07-19 15:00:41 +01001756void oid_get_numeric_string(data_t *oid, int error_ret, char *result_str)
1757{
1758 char buf[256];
1759 mbedtls_asn1_buf input_oid = { 0, 0, NULL };
1760 int ret;
1761
1762 input_oid.tag = MBEDTLS_ASN1_OID;
1763 /* Test that an empty OID is not dereferenced */
1764 input_oid.p = oid->len ? oid->x : (void *) 1;
1765 input_oid.len = oid->len;
1766
1767 ret = mbedtls_oid_get_numeric_string(buf, sizeof(buf), &input_oid);
1768
1769 if (error_ret == 0) {
1770 TEST_EQUAL(ret, strlen(result_str));
1771 TEST_ASSERT(ret >= 3);
1772 TEST_EQUAL(strcmp(buf, result_str), 0);
1773 } else {
1774 TEST_EQUAL(ret, error_ret);
1775 }
1776}
1777/* END_CASE */