blob: 905d62f5008bb30adebc5fa46b0b8c2c0fdb3161 [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"
7#include "mbedtls/pem.h"
8#include "mbedtls/oid.h"
9#include "mbedtls/base64.h"
Chris Jones9f7a6932021-04-14 12:12:09 +010010#include "mbedtls/error.h"
Valerio Setti178b5bd2023-02-13 10:04:28 +010011#include "mbedtls/pk.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +010012#include "string.h"
Paul Bakkerb63b0af2011-01-13 17:54:59 +000013
Glenn Strauss6f545ac2022-10-25 15:02:14 -040014#include "x509_invasive.h"
15
Simon Butcher9e24b512017-07-28 12:15:13 +010016#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
Hanno Becker3b1422e2017-07-26 13:38:02 +010017#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
Gilles Peskine449bd832023-01-11 14:50:10 +010018 than the current threshold 19. To test larger values, please \
19 adapt the script tests/data_files/dir-max/long.sh."
Hanno Becker3b1422e2017-07-26 13:38:02 +010020#endif
21
Hanno Becker20a4ade2019-06-03 14:27:03 +010022/* Test-only profile allowing all digests, PK algorithms, and curves. */
23const mbedtls_x509_crt_profile profile_all =
24{
25 0xFFFFFFFF, /* Any MD */
26 0xFFFFFFFF, /* Any PK alg */
27 0xFFFFFFFF, /* Any curve */
28 1024,
29};
30
Gilles Peskineef86ab22017-05-05 18:59:02 +020031/* Profile for backward compatibility. Allows SHA-1, unlike the default
32 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020033const mbedtls_x509_crt_profile compat_profile =
34{
Gilles Peskine449bd832023-01-11 14:50:10 +010035 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) |
36 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160) |
37 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
38 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
39 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
40 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubney28015e12022-04-21 08:12:59 +010041 0xFFFFFFFF, /* Any PK alg */
42 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020043 1024,
44};
45
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020046const mbedtls_x509_crt_profile profile_rsa3072 =
47{
Gilles Peskine449bd832023-01-11 14:50:10 +010048 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
49 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
50 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
51 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_RSA),
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020052 0,
53 3072,
54};
55
56const mbedtls_x509_crt_profile profile_sha512 =
57{
Gilles Peskine449bd832023-01-11 14:50:10 +010058 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubney28015e12022-04-21 08:12:59 +010059 0xFFFFFFFF, /* Any PK alg */
60 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020061 1024,
62};
63
Gilles Peskine449bd832023-01-11 14:50:10 +010064int verify_none(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000065{
Paul Bakker5a624082011-01-18 16:31:52 +000066 ((void) data);
67 ((void) crt);
68 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010069 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020070
Paul Bakker915275b2012-09-28 07:10:55 +000071 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000072}
73
Gilles Peskine449bd832023-01-11 14:50:10 +010074int verify_all(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000075{
Paul Bakker5a624082011-01-18 16:31:52 +000076 ((void) data);
77 ((void) crt);
78 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000079 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000080
Paul Bakkerb63b0af2011-01-13 17:54:59 +000081 return 0;
82}
83
Jarno Lamsa03cd1202019-03-27 15:45:04 +020084#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +010085int ca_callback_fail(void *data, mbedtls_x509_crt const *child, mbedtls_x509_crt **candidates)
Jarno Lamsa557426a2019-03-27 17:08:29 +020086{
87 ((void) data);
88 ((void) child);
89 ((void) candidates);
90
91 return -1;
92}
Przemek Stekielcd204992022-04-27 15:33:43 +020093#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010094int ca_callback(void *data, mbedtls_x509_crt const *child,
95 mbedtls_x509_crt **candidates)
Jarno Lamsa03cd1202019-03-27 15:45:04 +020096{
Hanno Beckercbb59032019-03-28 14:14:22 +000097 int ret = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +020098 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +000099 mbedtls_x509_crt *first;
100
101 /* This is a test-only implementation of the CA callback
102 * which always returns the entire list of trusted certificates.
103 * Production implementations managing a large number of CAs
104 * should use an efficient presentation and lookup for the
105 * set of trusted certificates (such as a hashtable) and only
106 * return those trusted certificates which satisfy basic
107 * parental checks, such as the matching of child `Issuer`
108 * and parent `Subject` field. */
109 ((void) child);
110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
112 if (first == NULL) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000113 ret = -1;
114 goto exit;
115 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_x509_crt_init(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000119 ret = -1;
120 goto exit;
121 }
122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 while (ca->next != NULL) {
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200124 ca = ca->next;
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 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200129 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000130
131exit:
132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 if (ret != 0) {
134 mbedtls_x509_crt_free(first);
135 mbedtls_free(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000136 first = NULL;
137 }
138
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200139 *candidates = first;
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 return ret;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200141}
Przemek Stekielcd204992022-04-27 15:33:43 +0200142#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200143#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145int verify_fatal(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200146{
147 int *levels = (int *) data;
148
149 ((void) crt);
150 ((void) certificate_depth);
151
152 /* Simulate a fatal error in the callback */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if (*levels & (1 << certificate_depth)) {
154 *flags |= (1 << certificate_depth);
155 return -1 - certificate_depth;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200156 }
157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return 0;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200159}
160
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900161/* strsep() not available on Windows */
162char *mystrsep(char **stringp, const char *delim)
163{
164 const char *p;
165 char *ret = *stringp;
166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 if (*stringp == NULL) {
168 return NULL;
169 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 for (;; (*stringp)++) {
172 if (**stringp == '\0') {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900173 *stringp = NULL;
174 goto done;
175 }
176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 for (p = delim; *p != '\0'; p++) {
178 if (**stringp == *p) {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900179 **stringp = '\0';
180 (*stringp)++;
181 goto done;
182 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900184 }
185
186done:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return ret;
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900188}
189
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200190#if defined(MBEDTLS_X509_CRT_PARSE_C)
191typedef struct {
192 char buf[512];
193 char *p;
194} verify_print_context;
195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196void verify_print_init(verify_print_context *ctx)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200197{
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 memset(ctx, 0, sizeof(verify_print_context));
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200199 ctx->p = ctx->buf;
200}
201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202int verify_print(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200203{
204 int ret;
205 verify_print_context *ctx = (verify_print_context *) data;
206 char *p = ctx->p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 size_t n = ctx->buf + sizeof(ctx->buf) - ctx->p;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200208 ((void) flags);
209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 ret = mbedtls_snprintf(p, n, "depth %d - serial ", certificate_depth);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200211 MBEDTLS_X509_SAFE_SNPRINTF;
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200214 MBEDTLS_X509_SAFE_SNPRINTF;
215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 ret = mbedtls_snprintf(p, n, " - subject ");
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200217 MBEDTLS_X509_SAFE_SNPRINTF;
218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200220 MBEDTLS_X509_SAFE_SNPRINTF;
221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 ret = mbedtls_snprintf(p, n, " - flags 0x%08x\n", *flags);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200223 MBEDTLS_X509_SAFE_SNPRINTF;
224
225 ctx->p = p;
226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 return 0;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200228}
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230int verify_parse_san(mbedtls_x509_subject_alternative_name *san,
231 char **buf, size_t *size)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200232{
233 int ret;
234 size_t i;
235 char *p = *buf;
236 size_t n = *size;
237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 ret = mbedtls_snprintf(p, n, "type : %d", san->type);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200239 MBEDTLS_X509_SAFE_SNPRINTF;
240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 switch (san->type) {
242 case (MBEDTLS_X509_SAN_OTHER_NAME):
243 ret = mbedtls_snprintf(p, n, "\notherName :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300244 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
247 &san->san.other_name.value.hardware_module_name.oid) != 0) {
248 ret = mbedtls_snprintf(p, n, " hardware module name :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300249 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 ret = mbedtls_snprintf(p, n, " hardware type : ");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300251 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 ret = mbedtls_oid_get_numeric_string(p,
254 n,
255 &san->san.other_name.value.hardware_module_name.oid);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300256 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 ret = mbedtls_snprintf(p, n, ", hardware serial number : ");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300259 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 for (i = 0; i < san->san.other_name.value.hardware_module_name.val.len; i++) {
262 ret = mbedtls_snprintf(p,
263 n,
264 "%02X",
265 san->san.other_name.value.hardware_module_name.val.p[i]);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300266 MBEDTLS_X509_SAFE_SNPRINTF;
267 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200268 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 break;/* MBEDTLS_OID_ON_HW_MODULE_NAME */
270 case (MBEDTLS_X509_SAN_DNS_NAME):
271 ret = mbedtls_snprintf(p, n, "\ndNSName : ");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200272 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (san->san.unstructured_name.len >= n) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200274 *p = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200276 }
277 n -= san->san.unstructured_name.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 for (i = 0; i < san->san.unstructured_name.len; i++) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200279 *p++ = san->san.unstructured_name.p[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 }
281 break;/* MBEDTLS_X509_SAN_DNS_NAME */
Przemek Stekiel608e3ef2023-02-09 14:47:50 +0100282 case (MBEDTLS_X509_SAN_RFC822_NAME):
283 ret = mbedtls_snprintf(p, n, "\nrfc822Name : ");
284 MBEDTLS_X509_SAFE_SNPRINTF;
285 if (san->san.unstructured_name.len >= n) {
286 *p = '\0';
287 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
288 }
289 n -= san->san.unstructured_name.len;
290 for (i = 0; i < san->san.unstructured_name.len; i++) {
291 *p++ = san->san.unstructured_name.p[i];
292 }
293 break;/* MBEDTLS_X509_SAN_RFC822_NAME */
Andrzej Kureke12b01d2023-01-10 06:47:38 -0500294 case (MBEDTLS_X509_SAN_DIRECTORY_NAME):
295 ret = mbedtls_snprintf(p, n, "\ndirectoryName : ");
296 MBEDTLS_X509_SAFE_SNPRINTF;
297 ret = mbedtls_x509_dn_gets(p, n, &san->san.directory_name);
298 if (ret < 0) {
299 return ret;
300 }
301
302 p += ret;
303 n -= ret;
304 break;/* MBEDTLS_X509_SAN_DIRECTORY_NAME */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200305 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 /*
307 * Should not happen.
308 */
309 return -1;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200310 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 ret = mbedtls_snprintf(p, n, "\n");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200312 MBEDTLS_X509_SAFE_SNPRINTF;
313
314 *size = n;
315 *buf = p;
316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200318}
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320int parse_crt_ext_cb(void *p_ctx, mbedtls_x509_crt const *crt, mbedtls_x509_buf const *oid,
321 int critical, const unsigned char *cp, const unsigned char *end)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200322{
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 (void) crt;
324 (void) critical;
325 mbedtls_x509_buf *new_oid = (mbedtls_x509_buf *) p_ctx;
326 if (oid->tag == MBEDTLS_ASN1_OID &&
327 MBEDTLS_OID_CMP(MBEDTLS_OID_CERTIFICATE_POLICIES, oid) == 0) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200328 /* Handle unknown certificate policy */
329 int ret, parse_ret = 0;
330 size_t len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 unsigned char **p = (unsigned char **) &cp;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200332
333 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 ret = mbedtls_asn1_get_tag(p, end, &len,
335 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
336 if (ret != 0) {
337 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
338 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (*p + len != end) {
341 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
342 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
343 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200344
345 /*
346 * Cannot be an empty sequence.
347 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (len == 0) {
349 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
350 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
351 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 while (*p < end) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200354 const unsigned char *policy_end;
355
356 /*
357 * Get the policy sequence
358 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
360 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
361 0) {
362 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
363 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200364
365 policy_end = *p + len;
366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
368 MBEDTLS_ASN1_OID)) != 0) {
369 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
370 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200371
Nicola Di Lietob77fad82020-06-17 17:57:36 +0200372 /*
373 * Recognize exclusively the policy with OID 1
374 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (len != 1 || *p[0] != 1) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200376 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200378
379 *p += len;
380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 /*
382 * If there is an optional qualifier, then *p < policy_end
383 * Check the Qualifier len to verify it doesn't exceed policy_end.
384 */
385 if (*p < policy_end) {
386 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
387 MBEDTLS_ASN1_CONSTRUCTED |
388 MBEDTLS_ASN1_SEQUENCE)) != 0) {
389 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
390 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200391 /*
392 * Skip the optional policy qualifiers.
393 */
394 *p += len;
395 }
396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 if (*p != policy_end) {
398 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
399 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
400 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200401 }
402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (*p != end) {
404 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
405 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
406 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 return parse_ret;
409 } else if (new_oid != NULL && new_oid->tag == oid->tag && new_oid->len == oid->len &&
410 memcmp(new_oid->p, oid->p, oid->len) == 0) {
411 return 0;
412 } else {
413 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
414 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200415 }
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200416}
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200417#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200418/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000419
Paul Bakker33b43f12013-08-20 11:48:36 +0200420/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200422 * END_DEPENDENCIES
423 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000424
Thomas Daubney5c9c2ce2022-06-06 16:36:43 +0100425/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100426void x509_accessor_ext_types(int ext_type, int has_ext_type)
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100427{
428 mbedtls_x509_crt crt;
429 int expected_result = ext_type & has_ext_type;
430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 mbedtls_x509_crt_init(&crt);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100432
433 crt.ext_types = ext_type;
434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 TEST_ASSERT(mbedtls_x509_crt_has_ext_type(&crt, has_ext_type) == expected_result);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 mbedtls_x509_crt_free(&crt);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100438}
439/* END_CASE */
440
Glenn Strauss6f545ac2022-10-25 15:02:14 -0400441/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_TEST_HOOKS */
442void x509_crt_parse_cn_inet_pton(const char *cn, data_t *exp, int ref_ret)
443{
444 uint32_t addr[4];
445 size_t addrlen = mbedtls_x509_crt_parse_cn_inet_pton(cn, addr);
446 TEST_EQUAL(addrlen, (size_t) ref_ret);
447
448 if (addrlen) {
449 ASSERT_COMPARE(exp->x, exp->len, addr, addrlen);
450 }
451}
452/* END_CASE */
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500455void x509_parse_san(char *crt_file, char *result_str, int parse_result)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200456{
Ron Eldor890819a2019-05-13 19:03:04 +0300457 int ret;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200458 mbedtls_x509_crt crt;
Ron Eldor890819a2019-05-13 19:03:04 +0300459 mbedtls_x509_subject_alternative_name san;
460 mbedtls_x509_sequence *cur = NULL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200461 char buf[2000];
462 char *p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 size_t n = sizeof(buf);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 mbedtls_x509_crt_init(&crt);
466 memset(buf, 0, 2000);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200467
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500468 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), parse_result);
Ron Eldor890819a2019-05-13 19:03:04 +0300469
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500470 if (parse_result != 0) {
471 goto exit;
472 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if (crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Ron Eldor890819a2019-05-13 19:03:04 +0300474 cur = &crt.subject_alt_names;
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 while (cur != NULL) {
476 ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
477 TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
Ron Eldor890819a2019-05-13 19:03:04 +0300478 /*
479 * If san type not supported, ignore.
480 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 if (ret == 0) {
Andrzej Kurekd40c2b62023-02-13 07:01:59 -0500482 ret = verify_parse_san(&san, &p, &n);
483 mbedtls_x509_free_subject_alt_name(&san);
484 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 }
Ron Eldor890819a2019-05-13 19:03:04 +0300486 cur = cur->next;
487 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200488 }
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 TEST_ASSERT(strcmp(buf, result_str) == 0);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200491
492exit:
493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 mbedtls_x509_crt_free(&crt);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200495}
496/* END_CASE */
497
Hanno Becker612a2f12020-10-09 09:19:39 +0100498/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100499void x509_cert_info(char *crt_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000500{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000502 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000503 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 mbedtls_x509_crt_init(&crt);
506 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
509 res = mbedtls_x509_crt_info(buf, 2000, "", &crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 TEST_ASSERT(res != -1);
512 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200515
516exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000518}
Paul Bakker33b43f12013-08-20 11:48:36 +0200519/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000520
Hanno Becker612a2f12020-10-09 09:19:39 +0100521/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100522void mbedtls_x509_crl_info(char *crl_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000523{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000525 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000526 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 mbedtls_x509_crl_init(&crl);
529 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == 0);
532 res = mbedtls_x509_crl_info(buf, 2000, "", &crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 TEST_ASSERT(res != -1);
535 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200538
539exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 mbedtls_x509_crl_free(&crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000541}
Paul Bakker33b43f12013-08-20 11:48:36 +0200542/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000543
Andres AGa39db392016-12-08 17:10:38 +0000544/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100545void mbedtls_x509_crl_parse(char *crl_file, int result)
Andres AGa39db392016-12-08 17:10:38 +0000546{
547 mbedtls_x509_crl crl;
548 char buf[2000];
549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 mbedtls_x509_crl_init(&crl);
551 memset(buf, 0, 2000);
Andres AGa39db392016-12-08 17:10:38 +0000552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == result);
Andres AGa39db392016-12-08 17:10:38 +0000554
555exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 mbedtls_x509_crl_free(&crl);
Andres AGa39db392016-12-08 17:10:38 +0000557}
558/* END_CASE */
559
Hanno Becker612a2f12020-10-09 09:19:39 +0100560/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100561void mbedtls_x509_csr_info(char *csr_file, char *result_str)
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100562{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100564 char buf[2000];
565 int res;
566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 mbedtls_x509_csr_init(&csr);
568 memset(buf, 0, 2000);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 TEST_ASSERT(mbedtls_x509_csr_parse_file(&csr, csr_file) == 0);
571 res = mbedtls_x509_csr_info(buf, 2000, "", &csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 TEST_ASSERT(res != -1);
574 TEST_ASSERT(res != -2);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200577
578exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 mbedtls_x509_csr_free(&csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100580}
581/* END_CASE */
582
Hanno Becker612a2f12020-10-09 09:19:39 +0100583/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100584void x509_verify_info(int flags, char *prefix, char *result_str)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100585{
586 char buf[2000];
587 int res;
588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 memset(buf, 0, sizeof(buf));
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 res = mbedtls_x509_crt_verify_info(buf, sizeof(buf), prefix, flags);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 TEST_ASSERT(res >= 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 TEST_ASSERT(strcmp(buf, result_str) == 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100596}
597/* END_CASE */
598
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200599/* 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 +0100600void x509_verify_restart(char *crt_file, char *ca_file,
601 int result, int flags_result,
602 int max_ops, int min_restart, int max_restart)
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200603{
604 int ret, cnt_restart;
605 mbedtls_x509_crt_restart_ctx rs_ctx;
606 mbedtls_x509_crt crt;
607 mbedtls_x509_crt ca;
608 uint32_t flags = 0;
609
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200610 /*
611 * See comments on ecp_test_vect_restart() for op count precision.
612 *
613 * For reference, with mbed TLS 2.6 and default settings:
614 * - ecdsa_verify() for P-256: ~ 6700
615 * - ecdsa_verify() for P-384: ~ 18800
616 * - x509_verify() for server5 -> test-ca2: ~ 18800
617 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
618 */
619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 mbedtls_x509_crt_restart_init(&rs_ctx);
621 mbedtls_x509_crt_init(&crt);
622 mbedtls_x509_crt_init(&ca);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200623
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100624 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnardcc6e0a62022-12-05 12:55:05 +0100625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
627 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 mbedtls_ecp_set_max_ops(max_ops);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200630
631 cnt_restart = 0;
632 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
634 &mbedtls_x509_crt_profile_default, NULL, &flags,
635 NULL, NULL, &rs_ctx);
636 } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 TEST_ASSERT(ret == result);
639 TEST_ASSERT(flags == (uint32_t) flags_result);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 TEST_ASSERT(cnt_restart >= min_restart);
642 TEST_ASSERT(cnt_restart <= max_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200643
644 /* Do we leak memory when aborting? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
646 &mbedtls_x509_crt_profile_default, NULL, &flags,
647 NULL, NULL, &rs_ctx);
648 TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200649
650exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 mbedtls_x509_crt_restart_free(&rs_ctx);
652 mbedtls_x509_crt_free(&crt);
653 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100654 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200655}
656/* END_CASE */
657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100659void x509_verify(char *crt_file, char *ca_file, char *crl_file,
660 char *cn_name_str, int result, int flags_result,
661 char *profile_str,
662 char *verify_callback)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000663{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_x509_crt crt;
665 mbedtls_x509_crt ca;
666 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200667 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000668 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200669 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 char *cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200671 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000672
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 mbedtls_x509_crt_init(&crt);
674 mbedtls_x509_crt_init(&ca);
675 mbedtls_x509_crl_init(&crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000676
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100677 MD_OR_USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +0100678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (strcmp(cn_name_str, "NULL") != 0) {
Paul Bakker33b43f12013-08-20 11:48:36 +0200680 cn_name = cn_name_str;
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 if (strcmp(profile_str, "") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200684 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 } else if (strcmp(profile_str, "next") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200686 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 } else if (strcmp(profile_str, "suite_b") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200688 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 } else if (strcmp(profile_str, "compat") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200690 profile = &compat_profile;
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 } else if (strcmp(profile_str, "all") == 0) {
Hanno Becker20a4ade2019-06-03 14:27:03 +0100692 profile = &profile_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 } else {
694 TEST_ASSERT("Unknown algorithm profile" == 0);
695 }
Gilles Peskineef86ab22017-05-05 18:59:02 +0200696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (strcmp(verify_callback, "NULL") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200698 f_vrfy = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 } else if (strcmp(verify_callback, "verify_none") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200700 f_vrfy = verify_none;
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 } else if (strcmp(verify_callback, "verify_all") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200702 f_vrfy = verify_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 } else {
704 TEST_ASSERT("No known verify callback selected" == 0);
705 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
708 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
709 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == 0);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 res = mbedtls_x509_crt_verify_with_profile(&crt,
712 &ca,
713 &crl,
714 profile,
715 cn_name,
716 &flags,
717 f_vrfy,
718 NULL);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 TEST_EQUAL(res, result);
721 TEST_EQUAL(flags, (uint32_t) flags_result);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200722
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200723#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker0350d562019-03-28 14:23:36 +0000724 /* CRLs aren't supported with CA callbacks, so skip the CA callback
Jarno Lamsadfd22c42019-04-01 15:18:53 +0300725 * version of the test if CRLs are in use. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 if (crl_file == NULL || strcmp(crl_file, "") == 0) {
Hanno Becker0350d562019-03-28 14:23:36 +0000727 flags = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 res = mbedtls_x509_crt_verify_with_ca_cb(&crt,
730 ca_callback,
731 &ca,
732 profile,
733 cn_name,
734 &flags,
735 f_vrfy,
736 NULL);
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 TEST_ASSERT(res == (result));
739 TEST_ASSERT(flags == (uint32_t) (flags_result));
Hanno Becker0350d562019-03-28 14:23:36 +0000740 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200741#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200742exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 mbedtls_x509_crt_free(&crt);
744 mbedtls_x509_crt_free(&ca);
745 mbedtls_x509_crl_free(&crl);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100746 MD_OR_USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000747}
Paul Bakker33b43f12013-08-20 11:48:36 +0200748/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000749
Jarno Lamsa557426a2019-03-27 17:08:29 +0200750/* 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 +0100751void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
752 int exp_ret)
Jarno Lamsa557426a2019-03-27 17:08:29 +0200753{
754 int ret;
755 mbedtls_x509_crt crt;
756 mbedtls_x509_crt ca;
757 uint32_t flags = 0;
Hanno Becker3f932bb2019-03-28 17:06:47 +0000758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 mbedtls_x509_crt_init(&crt);
760 mbedtls_x509_crt_init(&ca);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
763 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 if (strcmp(name, "NULL") == 0) {
Jarno Lamsa557426a2019-03-27 17:08:29 +0200766 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca,
770 &compat_profile, name, &flags,
771 NULL, NULL);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 TEST_ASSERT(ret == exp_ret);
774 TEST_ASSERT(flags == (uint32_t) (-1));
Jarno Lamsa557426a2019-03-27 17:08:29 +0200775exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 mbedtls_x509_crt_free(&crt);
777 mbedtls_x509_crt_free(&ca);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200778}
779/* END_CASE */
780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100782void x509_verify_callback(char *crt_file, char *ca_file, char *name,
783 int exp_ret, char *exp_vrfy_out)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200784{
785 int ret;
786 mbedtls_x509_crt crt;
787 mbedtls_x509_crt ca;
788 uint32_t flags = 0;
789 verify_print_context vrfy_ctx;
790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 mbedtls_x509_crt_init(&crt);
792 mbedtls_x509_crt_init(&ca);
793 verify_print_init(&vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200794
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100795 MD_OR_USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +0100796
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
798 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if (strcmp(name, "NULL") == 0) {
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200801 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 }
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL,
805 &compat_profile,
806 name, &flags,
807 verify_print, &vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 TEST_ASSERT(ret == exp_ret);
810 TEST_ASSERT(strcmp(vrfy_ctx.buf, exp_vrfy_out) == 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200811
812exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 mbedtls_x509_crt_free(&crt);
814 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100815 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200816}
817/* END_CASE */
818
Hanno Becker612a2f12020-10-09 09:19:39 +0100819/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
821 char *new_subject_ou,
822 char *result_str,
823 int ret)
Werner Lewis31ecb962022-06-17 15:51:55 +0100824{
825 mbedtls_x509_crt crt;
826 char buf[2000];
827 int res = 0;
828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 mbedtls_x509_crt_init(&crt);
830 memset(buf, 0, 2000);
Werner Lewis31ecb962022-06-17 15:51:55 +0100831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100833 crt.subject.next->val.p = (unsigned char *) new_subject_ou;
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 crt.subject.next->val.len = strlen(new_subject_ou);
Werner Lewis31ecb962022-06-17 15:51:55 +0100835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
Werner Lewis31ecb962022-06-17 15:51:55 +0100837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 if (ret != 0) {
839 TEST_ASSERT(res == ret);
840 } else {
841 TEST_ASSERT(res != -1);
842 TEST_ASSERT(res != -2);
843 TEST_ASSERT(strcmp(buf, result_str) == 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100844 }
845exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 mbedtls_x509_crt_free(&crt);
Werner Lewis31ecb962022-06-17 15:51:55 +0100847}
848/* END_CASE */
849
850/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100851void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000852{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000854 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200855 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000856
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 mbedtls_x509_crt_init(&crt);
858 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
861 if (strcmp(entity, "subject") == 0) {
862 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
863 } else if (strcmp(entity, "issuer") == 0) {
864 res = mbedtls_x509_dn_gets(buf, 2000, &crt.issuer);
865 } else {
866 TEST_ASSERT("Unknown entity" == 0);
867 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 TEST_ASSERT(res != -1);
870 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200873
874exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000876}
Paul Bakker33b43f12013-08-20 11:48:36 +0200877/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000878
David Horstmanndb73d3b2022-10-04 16:49:16 +0100879/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100880void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
David Horstmanndb73d3b2022-10-04 16:49:16 +0100881{
882 unsigned char *name;
883 unsigned char *p;
884 size_t name_len;
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100885 mbedtls_x509_name head;
David Horstmann3cd67582022-10-17 17:59:10 +0100886 int ret;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 memset(&head, 0, sizeof(head));
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100889
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100891 p = name;
892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 ret = mbedtls_x509_get_name(&p, (name + name_len), &head);
894 if (ret == 0) {
895 mbedtls_asn1_free_named_data_list_shallow(head.next);
896 }
David Horstmanndb73d3b2022-10-04 16:49:16 +0100897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 TEST_EQUAL(ret, exp_ret);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 mbedtls_free(name);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100901}
902/* END_CASE */
903
Werner Lewisb3acb052022-06-17 15:59:58 +0100904/* 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 +0100905void mbedtls_x509_dn_get_next(char *name_str,
906 int next_merged,
907 char *expected_oids,
908 int exp_count,
909 char *exp_dn_gets)
Werner Lewisb3acb052022-06-17 15:59:58 +0100910{
911 int ret = 0, i;
Werner Lewisac80a662022-06-23 11:58:02 +0100912 size_t len = 0, out_size;
Werner Lewisb3acb052022-06-17 15:59:58 +0100913 mbedtls_asn1_named_data *names = NULL;
Glenn Straussa4b40412022-06-26 19:32:09 -0400914 mbedtls_x509_name parsed, *parsed_cur;
Werner Lewisac80a662022-06-23 11:58:02 +0100915 // Size of buf is maximum required for test cases
916 unsigned char buf[80], *out = NULL, *c;
Werner Lewisb3acb052022-06-17 15:59:58 +0100917 const char *short_name;
918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 memset(&parsed, 0, sizeof(parsed));
920 memset(buf, 0, sizeof(buf));
921 c = buf + sizeof(buf);
Werner Lewisac80a662022-06-23 11:58:02 +0100922 // Additional size required for trailing space
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 out_size = strlen(expected_oids) + 2;
924 ASSERT_ALLOC(out, out_size);
Werner Lewisb3acb052022-06-17 15:59:58 +0100925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 TEST_EQUAL(mbedtls_x509_string_to_names(&names, name_str), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 ret = mbedtls_x509_write_names(&c, buf, names);
929 TEST_LE_S(0, ret);
Werner Lewisb3acb052022-06-17 15:59:58 +0100930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
932 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
933 TEST_EQUAL(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100934
935 // Iterate over names and set next_merged nodes
936 parsed_cur = &parsed;
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 for (; next_merged != 0 && parsed_cur != NULL; next_merged = next_merged >> 1) {
Werner Lewis12657cd2022-06-20 11:47:57 +0100938 parsed_cur->next_merged = next_merged & 0x01;
Werner Lewisb3acb052022-06-17 15:59:58 +0100939 parsed_cur = parsed_cur->next;
940 }
941
942 // Iterate over RDN nodes and print OID of first element to buffer
943 parsed_cur = &parsed;
944 len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 for (i = 0; parsed_cur != NULL; i++) {
946 TEST_EQUAL(mbedtls_oid_get_attr_short_name(&parsed_cur->oid,
947 &short_name), 0);
948 len += mbedtls_snprintf((char *) out + len, out_size - len, "%s ", short_name);
949 parsed_cur = mbedtls_x509_dn_get_next(parsed_cur);
Werner Lewisb3acb052022-06-17 15:59:58 +0100950 }
951 out[len-1] = 0;
952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 TEST_EQUAL(exp_count, i);
954 TEST_EQUAL(strcmp((char *) out, expected_oids), 0);
955 mbedtls_free(out);
Werner Lewisac80a662022-06-23 11:58:02 +0100956 out = NULL;
Werner Lewisb3acb052022-06-17 15:59:58 +0100957
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 out_size = strlen(exp_dn_gets) + 1;
959 ASSERT_ALLOC(out, out_size);
Werner Lewisac80a662022-06-23 11:58:02 +0100960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 TEST_LE_S(0, mbedtls_x509_dn_gets((char *) out, out_size, &parsed));
962 TEST_EQUAL(strcmp((char *) out, exp_dn_gets), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100963exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 mbedtls_free(out);
965 mbedtls_asn1_free_named_data_list(&names);
966 mbedtls_asn1_free_named_data_list_shallow(parsed.next);
Werner Lewisb3acb052022-06-17 15:59:58 +0100967}
Werner Lewisb3acb052022-06-17 15:59:58 +0100968/* END_CASE */
969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100971void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000972{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000974
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 mbedtls_x509_crt_init(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 if (strcmp(entity, "valid_from") == 0) {
980 TEST_ASSERT(mbedtls_x509_time_is_past(&crt.valid_from) == result);
981 } else if (strcmp(entity, "valid_to") == 0) {
982 TEST_ASSERT(mbedtls_x509_time_is_past(&crt.valid_to) == result);
983 } else {
984 TEST_ASSERT("Unknown entity" == 0);
985 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000986
Paul Bakkerbd51b262014-07-10 15:26:12 +0200987exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000989}
Paul Bakker33b43f12013-08-20 11:48:36 +0200990/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100993void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100994{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001000
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 if (strcmp(entity, "valid_from") == 0) {
1002 TEST_ASSERT(mbedtls_x509_time_is_future(&crt.valid_from) == result);
1003 } else if (strcmp(entity, "valid_to") == 0) {
1004 TEST_ASSERT(mbedtls_x509_time_is_future(&crt.valid_to) == result);
1005 } else {
1006 TEST_ASSERT("Unknown entity" == 0);
1007 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001008
Paul Bakkerbd51b262014-07-10 15:26:12 +02001009exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001011}
1012/* END_CASE */
1013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001015void x509parse_crt_file(char *crt_file, int result)
Paul Bakker5a5fa922014-09-26 14:53:04 +02001016{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +02001018
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 mbedtls_x509_crt_init(&crt);
Paul Bakker5a5fa922014-09-26 14:53:04 +02001020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == result);
Paul Bakker5a5fa922014-09-26 14:53:04 +02001022
1023exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 mbedtls_x509_crt_free(&crt);
Paul Bakker5a5fa922014-09-26 14:53:04 +02001025}
1026/* END_CASE */
1027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001029void x509parse_crt(data_t *buf, char *result_str, int result)
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001030{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 mbedtls_x509_crt crt;
Hanno Becker612a2f12020-10-09 09:19:39 +01001032#if !defined(MBEDTLS_X509_REMOVE_INFO)
Hanno Beckerfff2d572020-10-09 09:45:19 +01001033 unsigned char output[2000] = { 0 };
Azim Khanf1aaec92017-05-30 14:23:15 +01001034 int res;
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001035#else
1036 ((void) result_str);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001037#endif
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001038
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 mbedtls_x509_crt_init(&crt);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001040
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 TEST_ASSERT(mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len) == (result));
Hanno Becker612a2f12020-10-09 09:19:39 +01001042#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 if ((result) == 0) {
1044 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1045 TEST_ASSERT(res != -1);
1046 TEST_ASSERT(res != -2);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001049 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001051#endif
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 mbedtls_x509_crt_free(&crt);
1054 mbedtls_x509_crt_init(&crt);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 TEST_ASSERT(mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len) == (result));
Hanno Becker612a2f12020-10-09 09:19:39 +01001057#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if ((result) == 0) {
1059 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakker33b43f12013-08-20 11:48:36 +02001062
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 TEST_ASSERT(res != -1);
1064 TEST_ASSERT(res != -2);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001067 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001069#endif /* !MBEDTLS_X509_REMOVE_INFO */
Paul Bakkerb08e6842012-02-11 18:43:20 +00001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 mbedtls_x509_crt_free(&crt);
1072 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL,
1075 NULL) == (result));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001076#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 if ((result) == 0) {
1078 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 TEST_ASSERT(res != -1);
1081 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001084 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001086#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 mbedtls_x509_crt_free(&crt);
1089 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL,
1092 NULL) == (result));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001093#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 if ((result) == 0) {
1095 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 TEST_ASSERT(res != -1);
1098 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001101 }
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001102#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001103
1104exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 mbedtls_x509_crt_free(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001106}
1107/* END_CASE */
1108
1109/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001110void x509parse_crt_cb(data_t *buf, char *result_str, int result)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001111{
1112 mbedtls_x509_crt crt;
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001113 mbedtls_x509_buf oid;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001114
1115#if !defined(MBEDTLS_X509_REMOVE_INFO)
1116 unsigned char output[2000] = { 0 };
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001117 int res;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001118#else
1119 ((void) result_str);
1120#endif
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001121
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001122 oid.tag = MBEDTLS_ASN1_OID;
1123 oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
1129 &oid) == (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001130#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 if ((result) == 0) {
1132 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 TEST_ASSERT(res != -1);
1135 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001138 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 memset(output, 0, 2000);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001140#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 mbedtls_x509_crt_free(&crt);
1143 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1146 &oid) == (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001147#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 if ((result) == 0) {
1149 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 TEST_ASSERT(res != -1);
1152 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001155 }
Hanno Beckerfff2d572020-10-09 09:45:19 +01001156#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001157
Paul Bakkerbd51b262014-07-10 15:26:12 +02001158exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 mbedtls_x509_crt_free(&crt);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001160}
Paul Bakker33b43f12013-08-20 11:48:36 +02001161/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001162
Hanno Becker612a2f12020-10-09 09:19:39 +01001163/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001164void x509parse_crl(data_t *buf, char *result_str, int result)
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001165{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001167 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +01001168 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 mbedtls_x509_crl_init(&crl);
1171 memset(output, 0, 2000);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001172
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 TEST_ASSERT(mbedtls_x509_crl_parse(&crl, buf->x, buf->len) == (result));
1175 if ((result) == 0) {
1176 res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
Paul Bakker33b43f12013-08-20 11:48:36 +02001177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 TEST_ASSERT(res != -1);
1179 TEST_ASSERT(res != -2);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001182 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001183
Paul Bakkerbd51b262014-07-10 15:26:12 +02001184exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 mbedtls_x509_crl_free(&crl);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001186}
Paul Bakker33b43f12013-08-20 11:48:36 +02001187/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001188
Hanno Becker612a2f12020-10-09 09:19:39 +01001189/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001190void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001191{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001193 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001194 int my_ret;
1195
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 mbedtls_x509_csr_init(&csr);
1197 memset(my_out, 0, sizeof(my_out));
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
1200 TEST_ASSERT(my_ret == ref_ret);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 if (ref_ret == 0) {
1203 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1204 TEST_ASSERT(my_out_len == strlen(ref_out));
1205 TEST_ASSERT(strcmp(my_out, ref_out) == 0);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001206 }
1207
Paul Bakkerbd51b262014-07-10 15:26:12 +02001208exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 mbedtls_x509_csr_free(&csr);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001210}
1211/* END_CASE */
1212
Przemek Stekield7992df2023-01-25 16:19:50 +01001213/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1214void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
1215{
1216 mbedtls_x509_csr csr;
1217 char my_out[1000];
1218 int my_ret;
1219
1220 mbedtls_x509_csr_init(&csr);
1221 memset(my_out, 0, sizeof(my_out));
1222
1223 my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file);
1224 TEST_ASSERT(my_ret == ref_ret);
1225
1226 if (ref_ret == 0) {
1227 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1228 TEST_ASSERT(my_out_len == strlen(ref_out));
1229 TEST_ASSERT(strcmp(my_out, ref_out) == 0);
1230 }
1231
1232exit:
1233 mbedtls_x509_csr_free(&csr);
1234}
1235/* END_CASE */
1236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001238void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001239{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001241 int i;
1242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 mbedtls_x509_crt_init(&chain);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 TEST_ASSERT(mbedtls_x509_crt_parse_path(&chain, crt_path) == ret);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001246
1247 /* Check how many certs we got */
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1249 if (cur->raw.p != NULL) {
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001250 i++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 }
1252 }
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 TEST_ASSERT(i == nb_crt);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001255
Paul Bakkerbd51b262014-07-10 15:26:12 +02001256exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001258}
1259/* END_CASE */
1260
Janos Follath822b2c32015-10-11 10:25:22 +02001261/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001262void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1263 int ret_chk, int flags_chk)
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001264{
1265 char file_buf[128];
1266 int ret;
1267 uint32_t flags;
1268 mbedtls_x509_crt trusted, chain;
1269
1270 /*
1271 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1272 * with NN.crt signed by NN-1.crt
1273 */
1274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 mbedtls_x509_crt_init(&trusted);
1276 mbedtls_x509_crt_init(&chain);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001277
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001278 MD_OR_USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +01001279
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001280 /* Load trusted root */
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, ca_file) == 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001282
1283 /* Load a chain with nb_int intermediates (from 01 to nb_int),
1284 * plus one "end-entity" cert (nb_int + 1) */
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001285 ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 nb_int + 1);
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001287 TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, file_buf) == 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001289
1290 /* Try to verify that chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1292 NULL, NULL);
1293 TEST_ASSERT(ret == ret_chk);
1294 TEST_ASSERT(flags == (uint32_t) flags_chk);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001295
1296exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 mbedtls_x509_crt_free(&chain);
1298 mbedtls_x509_crt_free(&trusted);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001299 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001300}
1301/* END_CASE */
1302
1303/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001304void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1305 int flags_result, int result,
1306 char *profile_name, int vrfy_fatal_lvls)
Janos Follath822b2c32015-10-11 10:25:22 +02001307{
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 char *act;
Janos Follath822b2c32015-10-11 10:25:22 +02001309 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001310 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001311 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001312 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +02001313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 mbedtls_x509_crt_init(&chain);
1315 mbedtls_x509_crt_init(&trusted);
Janos Follath822b2c32015-10-11 10:25:22 +02001316
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001317 MD_OR_USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +01001318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 while ((act = mystrsep(&chain_paths, " ")) != NULL) {
1320 TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, act) == 0);
1321 }
1322 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, trusted_ca) == 0);
Janos Follath822b2c32015-10-11 10:25:22 +02001323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 if (strcmp(profile_name, "") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001325 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 } else if (strcmp(profile_name, "next") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001327 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 } else if (strcmp(profile_name, "suiteb") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001329 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 } else if (strcmp(profile_name, "rsa3072") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001331 profile = &profile_rsa3072;
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 } else if (strcmp(profile_name, "sha512") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001333 profile = &profile_sha512;
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 }
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1337 NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
Janos Follathef4f2582015-10-11 16:17:27 +02001338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 TEST_ASSERT(res == (result));
1340 TEST_ASSERT(flags == (uint32_t) (flags_result));
Janos Follath822b2c32015-10-11 10:25:22 +02001341
1342exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 mbedtls_x509_crt_free(&trusted);
1344 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001345 MD_OR_USE_PSA_DONE();
Janos Follath822b2c32015-10-11 10:25:22 +02001346}
1347/* END_CASE */
1348
Hanno Becker612a2f12020-10-09 09:19:39 +01001349/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001350void x509_oid_desc(data_t *buf, char *ref_desc)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001351{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001353 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001354 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001355
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001358 oid.p = buf->x;
1359 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 ret = mbedtls_oid_get_extended_key_usage(&oid, &desc);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 if (strcmp(ref_desc, "notfound") == 0) {
1364 TEST_ASSERT(ret != 0);
1365 TEST_ASSERT(desc == NULL);
1366 } else {
1367 TEST_ASSERT(ret == 0);
1368 TEST_ASSERT(desc != NULL);
1369 TEST_ASSERT(strcmp(desc, ref_desc) == 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001370 }
1371}
1372/* END_CASE */
1373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001375void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001376{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001378 char num_buf[100];
1379
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001380 memset(num_buf, 0x2a, sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001383 oid.p = oid_buf->x;
1384 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001385
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001386 TEST_ASSERT((size_t) blen <= sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 TEST_ASSERT(mbedtls_oid_get_numeric_string(num_buf, blen, &oid) == ret);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 if (ret >= 0) {
1391 TEST_ASSERT(num_buf[ret] == 0);
1392 TEST_ASSERT(strcmp(num_buf, numstr) == 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001393 }
1394}
1395/* END_CASE */
1396
TRodziewicz442fdc22021-06-07 13:52:23 +02001397/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001398void x509_check_key_usage(char *crt_file, int usage, int ret)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001399{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001401
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001403
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 TEST_ASSERT(mbedtls_x509_crt_check_key_usage(&crt, usage) == ret);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001407
Paul Bakkerbd51b262014-07-10 15:26:12 +02001408exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001410}
1411/* END_CASE */
1412
TRodziewicz442fdc22021-06-07 13:52:23 +02001413/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001414void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1415 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001416{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001418
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001420
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001421
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 TEST_ASSERT(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x,
1425 oid->len) == ret);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001426
Paul Bakkerbd51b262014-07-10 15:26:12 +02001427exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001429}
1430/* END_CASE */
1431
Andres AG4b76aec2016-09-23 13:16:02 +01001432/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001433void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1434 int day, int hour, int min, int sec)
Andres AG4b76aec2016-09-23 13:16:02 +01001435{
1436 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001437 unsigned char buf[21];
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 unsigned char *start = buf;
1439 unsigned char *end = buf;
Andres AG4b76aec2016-09-23 13:16:02 +01001440
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 memset(&time, 0x00, sizeof(time));
1442 *end = (unsigned char) tag; end++;
1443 *end = strlen(time_str);
1444 TEST_ASSERT(*end < 20);
Andres AG4b76aec2016-09-23 13:16:02 +01001445 end++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 memcpy(end, time_str, (size_t) *(end - 1));
Andres AG4b76aec2016-09-23 13:16:02 +01001447 end += *(end - 1);
1448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 TEST_ASSERT(mbedtls_x509_get_time(&start, end, &time) == ret);
1450 if (ret == 0) {
1451 TEST_ASSERT(year == time.year);
1452 TEST_ASSERT(mon == time.mon);
1453 TEST_ASSERT(day == time.day);
1454 TEST_ASSERT(hour == time.hour);
1455 TEST_ASSERT(min == time.min);
1456 TEST_ASSERT(sec == time.sec);
Andres AG4b76aec2016-09-23 13:16:02 +01001457 }
1458}
1459/* END_CASE */
1460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001462void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1463 int ref_msg_md, int ref_mgf_md,
1464 int ref_salt_len, int ref_ret)
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001465{
1466 int my_ret;
Ronald Cronac6ae352020-06-26 14:33:03 +02001467 mbedtls_x509_buf buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001469 int my_salt_len;
1470
Ronald Cronac6ae352020-06-26 14:33:03 +02001471 buf.p = params->x;
1472 buf.len = params->len;
1473 buf.tag = params_tag;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1476 &my_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001477
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 TEST_ASSERT(my_ret == ref_ret);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 if (ref_ret == 0) {
1481 TEST_ASSERT(my_msg_md == (mbedtls_md_type_t) ref_msg_md);
1482 TEST_ASSERT(my_mgf_md == (mbedtls_md_type_t) ref_mgf_md);
1483 TEST_ASSERT(my_salt_len == ref_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001484 }
1485
Paul Bakkerbd51b262014-07-10 15:26:12 +02001486exit:
Azim Khand30ca132017-06-09 04:32:58 +01001487 ;;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001488}
1489/* END_CASE */