blob: f347374e4b6228963d16fe4abf0782af16fda283 [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
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020014#include "mbedtls/legacy_or_psa.h"
Przemek Stekield34f8c32022-08-02 09:09:29 +020015
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 */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200294 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 /*
296 * Should not happen.
297 */
298 return -1;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200299 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 ret = mbedtls_snprintf(p, n, "\n");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200301 MBEDTLS_X509_SAFE_SNPRINTF;
302
303 *size = n;
304 *buf = p;
305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200307}
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309int parse_crt_ext_cb(void *p_ctx, mbedtls_x509_crt const *crt, mbedtls_x509_buf const *oid,
310 int critical, const unsigned char *cp, const unsigned char *end)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200311{
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 (void) crt;
313 (void) critical;
314 mbedtls_x509_buf *new_oid = (mbedtls_x509_buf *) p_ctx;
315 if (oid->tag == MBEDTLS_ASN1_OID &&
316 MBEDTLS_OID_CMP(MBEDTLS_OID_CERTIFICATE_POLICIES, oid) == 0) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200317 /* Handle unknown certificate policy */
318 int ret, parse_ret = 0;
319 size_t len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 unsigned char **p = (unsigned char **) &cp;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200321
322 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 ret = mbedtls_asn1_get_tag(p, end, &len,
324 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
325 if (ret != 0) {
326 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
327 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 if (*p + len != end) {
330 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
331 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
332 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200333
334 /*
335 * Cannot be an empty sequence.
336 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if (len == 0) {
338 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
339 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
340 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200341
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 while (*p < end) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200343 const unsigned char *policy_end;
344
345 /*
346 * Get the policy sequence
347 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
349 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
350 0) {
351 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
352 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200353
354 policy_end = *p + len;
355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
357 MBEDTLS_ASN1_OID)) != 0) {
358 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
359 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200360
Nicola Di Lietob77fad82020-06-17 17:57:36 +0200361 /*
362 * Recognize exclusively the policy with OID 1
363 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 if (len != 1 || *p[0] != 1) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200365 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200367
368 *p += len;
369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 /*
371 * If there is an optional qualifier, then *p < policy_end
372 * Check the Qualifier len to verify it doesn't exceed policy_end.
373 */
374 if (*p < policy_end) {
375 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
376 MBEDTLS_ASN1_CONSTRUCTED |
377 MBEDTLS_ASN1_SEQUENCE)) != 0) {
378 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
379 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200380 /*
381 * Skip the optional policy qualifiers.
382 */
383 *p += len;
384 }
385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if (*p != policy_end) {
387 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
388 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
389 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200390 }
391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if (*p != end) {
393 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
394 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
395 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 return parse_ret;
398 } else if (new_oid != NULL && new_oid->tag == oid->tag && new_oid->len == oid->len &&
399 memcmp(new_oid->p, oid->p, oid->len) == 0) {
400 return 0;
401 } else {
402 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
403 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200404 }
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200405}
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200406#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200407/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000408
Paul Bakker33b43f12013-08-20 11:48:36 +0200409/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200411 * END_DEPENDENCIES
412 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000413
Thomas Daubney5c9c2ce2022-06-06 16:36:43 +0100414/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415void x509_accessor_ext_types(int ext_type, int has_ext_type)
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100416{
417 mbedtls_x509_crt crt;
418 int expected_result = ext_type & has_ext_type;
419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 mbedtls_x509_crt_init(&crt);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100421
422 crt.ext_types = ext_type;
423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 TEST_ASSERT(mbedtls_x509_crt_has_ext_type(&crt, has_ext_type) == expected_result);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 mbedtls_x509_crt_free(&crt);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100427}
428/* END_CASE */
429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100431void x509_parse_san(char *crt_file, char *result_str)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200432{
Ron Eldor890819a2019-05-13 19:03:04 +0300433 int ret;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200434 mbedtls_x509_crt crt;
Ron Eldor890819a2019-05-13 19:03:04 +0300435 mbedtls_x509_subject_alternative_name san;
436 mbedtls_x509_sequence *cur = NULL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200437 char buf[2000];
438 char *p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 size_t n = sizeof(buf);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 mbedtls_x509_crt_init(&crt);
442 memset(buf, 0, 2000);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Ron Eldor890819a2019-05-13 19:03:04 +0300445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if (crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Ron Eldor890819a2019-05-13 19:03:04 +0300447 cur = &crt.subject_alt_names;
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 while (cur != NULL) {
449 ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
450 TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
Ron Eldor890819a2019-05-13 19:03:04 +0300451 /*
452 * If san type not supported, ignore.
453 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if (ret == 0) {
455 TEST_ASSERT(verify_parse_san(&san, &p, &n) == 0);
456 }
Ron Eldor890819a2019-05-13 19:03:04 +0300457 cur = cur->next;
458 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200459 }
460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 TEST_ASSERT(strcmp(buf, result_str) == 0);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200462
463exit:
464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 mbedtls_x509_crt_free(&crt);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200466}
467/* END_CASE */
468
Hanno Becker612a2f12020-10-09 09:19:39 +0100469/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100470void x509_cert_info(char *crt_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000471{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000473 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000474 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 mbedtls_x509_crt_init(&crt);
477 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
480 res = mbedtls_x509_crt_info(buf, 2000, "", &crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 TEST_ASSERT(res != -1);
483 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200486
487exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000489}
Paul Bakker33b43f12013-08-20 11:48:36 +0200490/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000491
Hanno Becker612a2f12020-10-09 09:19:39 +0100492/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100493void mbedtls_x509_crl_info(char *crl_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000494{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000496 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000497 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 mbedtls_x509_crl_init(&crl);
500 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == 0);
503 res = mbedtls_x509_crl_info(buf, 2000, "", &crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 TEST_ASSERT(res != -1);
506 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200509
510exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 mbedtls_x509_crl_free(&crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000512}
Paul Bakker33b43f12013-08-20 11:48:36 +0200513/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000514
Andres AGa39db392016-12-08 17:10:38 +0000515/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100516void mbedtls_x509_crl_parse(char *crl_file, int result)
Andres AGa39db392016-12-08 17:10:38 +0000517{
518 mbedtls_x509_crl crl;
519 char buf[2000];
520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 mbedtls_x509_crl_init(&crl);
522 memset(buf, 0, 2000);
Andres AGa39db392016-12-08 17:10:38 +0000523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == result);
Andres AGa39db392016-12-08 17:10:38 +0000525
526exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 mbedtls_x509_crl_free(&crl);
Andres AGa39db392016-12-08 17:10:38 +0000528}
529/* END_CASE */
530
Hanno Becker612a2f12020-10-09 09:19:39 +0100531/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100532void mbedtls_x509_csr_info(char *csr_file, char *result_str)
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100533{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100535 char buf[2000];
536 int res;
537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 mbedtls_x509_csr_init(&csr);
539 memset(buf, 0, 2000);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 TEST_ASSERT(mbedtls_x509_csr_parse_file(&csr, csr_file) == 0);
542 res = mbedtls_x509_csr_info(buf, 2000, "", &csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 TEST_ASSERT(res != -1);
545 TEST_ASSERT(res != -2);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200548
549exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 mbedtls_x509_csr_free(&csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100551}
552/* END_CASE */
553
Hanno Becker612a2f12020-10-09 09:19:39 +0100554/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100555void x509_verify_info(int flags, char *prefix, char *result_str)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100556{
557 char buf[2000];
558 int res;
559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 memset(buf, 0, sizeof(buf));
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 res = mbedtls_x509_crt_verify_info(buf, sizeof(buf), prefix, flags);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 TEST_ASSERT(res >= 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 TEST_ASSERT(strcmp(buf, result_str) == 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100567}
568/* END_CASE */
569
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200570/* 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 +0100571void x509_verify_restart(char *crt_file, char *ca_file,
572 int result, int flags_result,
573 int max_ops, int min_restart, int max_restart)
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200574{
575 int ret, cnt_restart;
576 mbedtls_x509_crt_restart_ctx rs_ctx;
577 mbedtls_x509_crt crt;
578 mbedtls_x509_crt ca;
579 uint32_t flags = 0;
580
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200581 /*
582 * See comments on ecp_test_vect_restart() for op count precision.
583 *
584 * For reference, with mbed TLS 2.6 and default settings:
585 * - ecdsa_verify() for P-256: ~ 6700
586 * - ecdsa_verify() for P-384: ~ 18800
587 * - x509_verify() for server5 -> test-ca2: ~ 18800
588 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
589 */
590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 mbedtls_x509_crt_restart_init(&rs_ctx);
592 mbedtls_x509_crt_init(&crt);
593 mbedtls_x509_crt_init(&ca);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200594
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100595 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnardcc6e0a62022-12-05 12:55:05 +0100596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
598 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 mbedtls_ecp_set_max_ops(max_ops);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200601
602 cnt_restart = 0;
603 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
605 &mbedtls_x509_crt_profile_default, NULL, &flags,
606 NULL, NULL, &rs_ctx);
607 } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 TEST_ASSERT(ret == result);
610 TEST_ASSERT(flags == (uint32_t) flags_result);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 TEST_ASSERT(cnt_restart >= min_restart);
613 TEST_ASSERT(cnt_restart <= max_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200614
615 /* Do we leak memory when aborting? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
617 &mbedtls_x509_crt_profile_default, NULL, &flags,
618 NULL, NULL, &rs_ctx);
619 TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200620
621exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 mbedtls_x509_crt_restart_free(&rs_ctx);
623 mbedtls_x509_crt_free(&crt);
624 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100625 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200626}
627/* END_CASE */
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100630void x509_verify(char *crt_file, char *ca_file, char *crl_file,
631 char *cn_name_str, int result, int flags_result,
632 char *profile_str,
633 char *verify_callback)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000634{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_x509_crt crt;
636 mbedtls_x509_crt ca;
637 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200638 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000639 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200640 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 char *cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200642 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 mbedtls_x509_crt_init(&crt);
645 mbedtls_x509_crt_init(&ca);
646 mbedtls_x509_crl_init(&crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000647
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100648 MD_OR_USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +0100649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (strcmp(cn_name_str, "NULL") != 0) {
Paul Bakker33b43f12013-08-20 11:48:36 +0200651 cn_name = cn_name_str;
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (strcmp(profile_str, "") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200655 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 } else if (strcmp(profile_str, "next") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200657 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 } else if (strcmp(profile_str, "suite_b") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200659 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 } else if (strcmp(profile_str, "compat") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200661 profile = &compat_profile;
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 } else if (strcmp(profile_str, "all") == 0) {
Hanno Becker20a4ade2019-06-03 14:27:03 +0100663 profile = &profile_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 } else {
665 TEST_ASSERT("Unknown algorithm profile" == 0);
666 }
Gilles Peskineef86ab22017-05-05 18:59:02 +0200667
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if (strcmp(verify_callback, "NULL") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200669 f_vrfy = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 } else if (strcmp(verify_callback, "verify_none") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200671 f_vrfy = verify_none;
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 } else if (strcmp(verify_callback, "verify_all") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200673 f_vrfy = verify_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 } else {
675 TEST_ASSERT("No known verify callback selected" == 0);
676 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
679 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
680 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == 0);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 res = mbedtls_x509_crt_verify_with_profile(&crt,
683 &ca,
684 &crl,
685 profile,
686 cn_name,
687 &flags,
688 f_vrfy,
689 NULL);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 TEST_EQUAL(res, result);
692 TEST_EQUAL(flags, (uint32_t) flags_result);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200693
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200694#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker0350d562019-03-28 14:23:36 +0000695 /* CRLs aren't supported with CA callbacks, so skip the CA callback
Jarno Lamsadfd22c42019-04-01 15:18:53 +0300696 * version of the test if CRLs are in use. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (crl_file == NULL || strcmp(crl_file, "") == 0) {
Hanno Becker0350d562019-03-28 14:23:36 +0000698 flags = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 res = mbedtls_x509_crt_verify_with_ca_cb(&crt,
701 ca_callback,
702 &ca,
703 profile,
704 cn_name,
705 &flags,
706 f_vrfy,
707 NULL);
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 TEST_ASSERT(res == (result));
710 TEST_ASSERT(flags == (uint32_t) (flags_result));
Hanno Becker0350d562019-03-28 14:23:36 +0000711 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200712#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200713exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 mbedtls_x509_crt_free(&crt);
715 mbedtls_x509_crt_free(&ca);
716 mbedtls_x509_crl_free(&crl);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100717 MD_OR_USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000718}
Paul Bakker33b43f12013-08-20 11:48:36 +0200719/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000720
Jarno Lamsa557426a2019-03-27 17:08:29 +0200721/* 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 +0100722void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
723 int exp_ret)
Jarno Lamsa557426a2019-03-27 17:08:29 +0200724{
725 int ret;
726 mbedtls_x509_crt crt;
727 mbedtls_x509_crt ca;
728 uint32_t flags = 0;
Hanno Becker3f932bb2019-03-28 17:06:47 +0000729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 mbedtls_x509_crt_init(&crt);
731 mbedtls_x509_crt_init(&ca);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
734 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 if (strcmp(name, "NULL") == 0) {
Jarno Lamsa557426a2019-03-27 17:08:29 +0200737 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca,
741 &compat_profile, name, &flags,
742 NULL, NULL);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 TEST_ASSERT(ret == exp_ret);
745 TEST_ASSERT(flags == (uint32_t) (-1));
Jarno Lamsa557426a2019-03-27 17:08:29 +0200746exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 mbedtls_x509_crt_free(&crt);
748 mbedtls_x509_crt_free(&ca);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200749}
750/* END_CASE */
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100753void x509_verify_callback(char *crt_file, char *ca_file, char *name,
754 int exp_ret, char *exp_vrfy_out)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200755{
756 int ret;
757 mbedtls_x509_crt crt;
758 mbedtls_x509_crt ca;
759 uint32_t flags = 0;
760 verify_print_context vrfy_ctx;
761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 mbedtls_x509_crt_init(&crt);
763 mbedtls_x509_crt_init(&ca);
764 verify_print_init(&vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200765
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100766 MD_OR_USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +0100767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
769 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 if (strcmp(name, "NULL") == 0) {
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200772 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 }
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL,
776 &compat_profile,
777 name, &flags,
778 verify_print, &vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200779
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 TEST_ASSERT(ret == exp_ret);
781 TEST_ASSERT(strcmp(vrfy_ctx.buf, exp_vrfy_out) == 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200782
783exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 mbedtls_x509_crt_free(&crt);
785 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100786 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200787}
788/* END_CASE */
789
Hanno Becker612a2f12020-10-09 09:19:39 +0100790/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100791void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
792 char *new_subject_ou,
793 char *result_str,
794 int ret)
Werner Lewis31ecb962022-06-17 15:51:55 +0100795{
796 mbedtls_x509_crt crt;
797 char buf[2000];
798 int res = 0;
799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 mbedtls_x509_crt_init(&crt);
801 memset(buf, 0, 2000);
Werner Lewis31ecb962022-06-17 15:51:55 +0100802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100804 crt.subject.next->val.p = (unsigned char *) new_subject_ou;
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 crt.subject.next->val.len = strlen(new_subject_ou);
Werner Lewis31ecb962022-06-17 15:51:55 +0100806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
Werner Lewis31ecb962022-06-17 15:51:55 +0100808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (ret != 0) {
810 TEST_ASSERT(res == ret);
811 } else {
812 TEST_ASSERT(res != -1);
813 TEST_ASSERT(res != -2);
814 TEST_ASSERT(strcmp(buf, result_str) == 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100815 }
816exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 mbedtls_x509_crt_free(&crt);
Werner Lewis31ecb962022-06-17 15:51:55 +0100818}
819/* END_CASE */
820
821/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100822void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000823{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000825 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200826 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 mbedtls_x509_crt_init(&crt);
829 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
832 if (strcmp(entity, "subject") == 0) {
833 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
834 } else if (strcmp(entity, "issuer") == 0) {
835 res = mbedtls_x509_dn_gets(buf, 2000, &crt.issuer);
836 } else {
837 TEST_ASSERT("Unknown entity" == 0);
838 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 TEST_ASSERT(res != -1);
841 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200844
845exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000847}
Paul Bakker33b43f12013-08-20 11:48:36 +0200848/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000849
David Horstmanndb73d3b2022-10-04 16:49:16 +0100850/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100851void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
David Horstmanndb73d3b2022-10-04 16:49:16 +0100852{
853 unsigned char *name;
854 unsigned char *p;
855 size_t name_len;
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100856 mbedtls_x509_name head;
David Horstmann3cd67582022-10-17 17:59:10 +0100857 int ret;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100858
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 memset(&head, 0, sizeof(head));
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100862 p = name;
863
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 ret = mbedtls_x509_get_name(&p, (name + name_len), &head);
865 if (ret == 0) {
866 mbedtls_asn1_free_named_data_list_shallow(head.next);
867 }
David Horstmanndb73d3b2022-10-04 16:49:16 +0100868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 TEST_EQUAL(ret, exp_ret);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 mbedtls_free(name);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100872}
873/* END_CASE */
874
Werner Lewisb3acb052022-06-17 15:59:58 +0100875/* 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 +0100876void mbedtls_x509_dn_get_next(char *name_str,
877 int next_merged,
878 char *expected_oids,
879 int exp_count,
880 char *exp_dn_gets)
Werner Lewisb3acb052022-06-17 15:59:58 +0100881{
882 int ret = 0, i;
Werner Lewisac80a662022-06-23 11:58:02 +0100883 size_t len = 0, out_size;
Werner Lewisb3acb052022-06-17 15:59:58 +0100884 mbedtls_asn1_named_data *names = NULL;
Glenn Straussa4b40412022-06-26 19:32:09 -0400885 mbedtls_x509_name parsed, *parsed_cur;
Werner Lewisac80a662022-06-23 11:58:02 +0100886 // Size of buf is maximum required for test cases
887 unsigned char buf[80], *out = NULL, *c;
Werner Lewisb3acb052022-06-17 15:59:58 +0100888 const char *short_name;
889
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 memset(&parsed, 0, sizeof(parsed));
891 memset(buf, 0, sizeof(buf));
892 c = buf + sizeof(buf);
Werner Lewisac80a662022-06-23 11:58:02 +0100893 // Additional size required for trailing space
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 out_size = strlen(expected_oids) + 2;
895 ASSERT_ALLOC(out, out_size);
Werner Lewisb3acb052022-06-17 15:59:58 +0100896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 TEST_EQUAL(mbedtls_x509_string_to_names(&names, name_str), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 ret = mbedtls_x509_write_names(&c, buf, names);
900 TEST_LE_S(0, ret);
Werner Lewisb3acb052022-06-17 15:59:58 +0100901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
903 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
904 TEST_EQUAL(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100905
906 // Iterate over names and set next_merged nodes
907 parsed_cur = &parsed;
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 for (; next_merged != 0 && parsed_cur != NULL; next_merged = next_merged >> 1) {
Werner Lewis12657cd2022-06-20 11:47:57 +0100909 parsed_cur->next_merged = next_merged & 0x01;
Werner Lewisb3acb052022-06-17 15:59:58 +0100910 parsed_cur = parsed_cur->next;
911 }
912
913 // Iterate over RDN nodes and print OID of first element to buffer
914 parsed_cur = &parsed;
915 len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 for (i = 0; parsed_cur != NULL; i++) {
917 TEST_EQUAL(mbedtls_oid_get_attr_short_name(&parsed_cur->oid,
918 &short_name), 0);
919 len += mbedtls_snprintf((char *) out + len, out_size - len, "%s ", short_name);
920 parsed_cur = mbedtls_x509_dn_get_next(parsed_cur);
Werner Lewisb3acb052022-06-17 15:59:58 +0100921 }
922 out[len-1] = 0;
923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 TEST_EQUAL(exp_count, i);
925 TEST_EQUAL(strcmp((char *) out, expected_oids), 0);
926 mbedtls_free(out);
Werner Lewisac80a662022-06-23 11:58:02 +0100927 out = NULL;
Werner Lewisb3acb052022-06-17 15:59:58 +0100928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 out_size = strlen(exp_dn_gets) + 1;
930 ASSERT_ALLOC(out, out_size);
Werner Lewisac80a662022-06-23 11:58:02 +0100931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 TEST_LE_S(0, mbedtls_x509_dn_gets((char *) out, out_size, &parsed));
933 TEST_EQUAL(strcmp((char *) out, exp_dn_gets), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100934exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 mbedtls_free(out);
936 mbedtls_asn1_free_named_data_list(&names);
937 mbedtls_asn1_free_named_data_list_shallow(parsed.next);
Werner Lewisb3acb052022-06-17 15:59:58 +0100938}
Werner Lewisb3acb052022-06-17 15:59:58 +0100939/* END_CASE */
940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100942void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000943{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 mbedtls_x509_crt_init(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 if (strcmp(entity, "valid_from") == 0) {
951 TEST_ASSERT(mbedtls_x509_time_is_past(&crt.valid_from) == result);
952 } else if (strcmp(entity, "valid_to") == 0) {
953 TEST_ASSERT(mbedtls_x509_time_is_past(&crt.valid_to) == result);
954 } else {
955 TEST_ASSERT("Unknown entity" == 0);
956 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000957
Paul Bakkerbd51b262014-07-10 15:26:12 +0200958exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000960}
Paul Bakker33b43f12013-08-20 11:48:36 +0200961/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100965{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 if (strcmp(entity, "valid_from") == 0) {
973 TEST_ASSERT(mbedtls_x509_time_is_future(&crt.valid_from) == result);
974 } else if (strcmp(entity, "valid_to") == 0) {
975 TEST_ASSERT(mbedtls_x509_time_is_future(&crt.valid_to) == result);
976 } else {
977 TEST_ASSERT("Unknown entity" == 0);
978 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100979
Paul Bakkerbd51b262014-07-10 15:26:12 +0200980exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100982}
983/* END_CASE */
984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100986void x509parse_crt_file(char *crt_file, int result)
Paul Bakker5a5fa922014-09-26 14:53:04 +0200987{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +0200989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 mbedtls_x509_crt_init(&crt);
Paul Bakker5a5fa922014-09-26 14:53:04 +0200991
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == result);
Paul Bakker5a5fa922014-09-26 14:53:04 +0200993
994exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 mbedtls_x509_crt_free(&crt);
Paul Bakker5a5fa922014-09-26 14:53:04 +0200996}
997/* END_CASE */
998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001000void x509parse_crt(data_t *buf, char *result_str, int result)
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001001{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 mbedtls_x509_crt crt;
Hanno Becker612a2f12020-10-09 09:19:39 +01001003#if !defined(MBEDTLS_X509_REMOVE_INFO)
Hanno Beckerfff2d572020-10-09 09:45:19 +01001004 unsigned char output[2000] = { 0 };
Azim Khanf1aaec92017-05-30 14:23:15 +01001005 int res;
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001006#else
1007 ((void) result_str);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001008#endif
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001009
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 mbedtls_x509_crt_init(&crt);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 TEST_ASSERT(mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len) == (result));
Hanno Becker612a2f12020-10-09 09:19:39 +01001013#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 if ((result) == 0) {
1015 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1016 TEST_ASSERT(res != -1);
1017 TEST_ASSERT(res != -2);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001018
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001020 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001022#endif
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001023
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 mbedtls_x509_crt_free(&crt);
1025 mbedtls_x509_crt_init(&crt);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 TEST_ASSERT(mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len) == (result));
Hanno Becker612a2f12020-10-09 09:19:39 +01001028#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if ((result) == 0) {
1030 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001031
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakker33b43f12013-08-20 11:48:36 +02001033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 TEST_ASSERT(res != -1);
1035 TEST_ASSERT(res != -2);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001036
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001038 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001040#endif /* !MBEDTLS_X509_REMOVE_INFO */
Paul Bakkerb08e6842012-02-11 18:43:20 +00001041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 mbedtls_x509_crt_free(&crt);
1043 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001044
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL,
1046 NULL) == (result));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001047#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if ((result) == 0) {
1049 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 TEST_ASSERT(res != -1);
1052 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001055 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001057#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 mbedtls_x509_crt_free(&crt);
1060 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL,
1063 NULL) == (result));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001064#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if ((result) == 0) {
1066 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001067
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 TEST_ASSERT(res != -1);
1069 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001072 }
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001073#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001074
1075exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 mbedtls_x509_crt_free(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001077}
1078/* END_CASE */
1079
1080/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001081void x509parse_crt_cb(data_t *buf, char *result_str, int result)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001082{
1083 mbedtls_x509_crt crt;
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001084 mbedtls_x509_buf oid;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001085
1086#if !defined(MBEDTLS_X509_REMOVE_INFO)
1087 unsigned char output[2000] = { 0 };
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001088 int res;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001089#else
1090 ((void) result_str);
1091#endif
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001092
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001093 oid.tag = MBEDTLS_ASN1_OID;
1094 oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001098
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
1100 &oid) == (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001101#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 if ((result) == 0) {
1103 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 TEST_ASSERT(res != -1);
1106 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001107
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001109 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 memset(output, 0, 2000);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001111#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001112
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 mbedtls_x509_crt_free(&crt);
1114 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1117 &oid) == (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001118#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 if ((result) == 0) {
1120 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 TEST_ASSERT(res != -1);
1123 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001126 }
Hanno Beckerfff2d572020-10-09 09:45:19 +01001127#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001128
Paul Bakkerbd51b262014-07-10 15:26:12 +02001129exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 mbedtls_x509_crt_free(&crt);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001131}
Paul Bakker33b43f12013-08-20 11:48:36 +02001132/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001133
Hanno Becker612a2f12020-10-09 09:19:39 +01001134/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001135void x509parse_crl(data_t *buf, char *result_str, int result)
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001136{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001138 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +01001139 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 mbedtls_x509_crl_init(&crl);
1142 memset(output, 0, 2000);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001143
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 TEST_ASSERT(mbedtls_x509_crl_parse(&crl, buf->x, buf->len) == (result));
1146 if ((result) == 0) {
1147 res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
Paul Bakker33b43f12013-08-20 11:48:36 +02001148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 TEST_ASSERT(res != -1);
1150 TEST_ASSERT(res != -2);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001153 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001154
Paul Bakkerbd51b262014-07-10 15:26:12 +02001155exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 mbedtls_x509_crl_free(&crl);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001157}
Paul Bakker33b43f12013-08-20 11:48:36 +02001158/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001159
Hanno Becker612a2f12020-10-09 09:19:39 +01001160/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001161void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001162{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001164 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001165 int my_ret;
1166
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 mbedtls_x509_csr_init(&csr);
1168 memset(my_out, 0, sizeof(my_out));
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
1171 TEST_ASSERT(my_ret == ref_ret);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if (ref_ret == 0) {
1174 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1175 TEST_ASSERT(my_out_len == strlen(ref_out));
1176 TEST_ASSERT(strcmp(my_out, ref_out) == 0);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001177 }
1178
Paul Bakkerbd51b262014-07-10 15:26:12 +02001179exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 mbedtls_x509_csr_free(&csr);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001181}
1182/* END_CASE */
1183
Przemek Stekield7992df2023-01-25 16:19:50 +01001184/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1185void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
1186{
1187 mbedtls_x509_csr csr;
1188 char my_out[1000];
1189 int my_ret;
1190
1191 mbedtls_x509_csr_init(&csr);
1192 memset(my_out, 0, sizeof(my_out));
1193
1194 my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file);
1195 TEST_ASSERT(my_ret == ref_ret);
1196
1197 if (ref_ret == 0) {
1198 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1199 TEST_ASSERT(my_out_len == strlen(ref_out));
1200 TEST_ASSERT(strcmp(my_out, ref_out) == 0);
1201 }
1202
1203exit:
1204 mbedtls_x509_csr_free(&csr);
1205}
1206/* END_CASE */
1207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001209void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001210{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001212 int i;
1213
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 mbedtls_x509_crt_init(&chain);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 TEST_ASSERT(mbedtls_x509_crt_parse_path(&chain, crt_path) == ret);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001217
1218 /* Check how many certs we got */
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1220 if (cur->raw.p != NULL) {
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001221 i++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 }
1223 }
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 TEST_ASSERT(i == nb_crt);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001226
Paul Bakkerbd51b262014-07-10 15:26:12 +02001227exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001229}
1230/* END_CASE */
1231
Janos Follath822b2c32015-10-11 10:25:22 +02001232/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001233void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1234 int ret_chk, int flags_chk)
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001235{
1236 char file_buf[128];
1237 int ret;
1238 uint32_t flags;
1239 mbedtls_x509_crt trusted, chain;
1240
1241 /*
1242 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1243 * with NN.crt signed by NN-1.crt
1244 */
1245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 mbedtls_x509_crt_init(&trusted);
1247 mbedtls_x509_crt_init(&chain);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001248
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001249 MD_OR_USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +01001250
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001251 /* Load trusted root */
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, ca_file) == 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001253
1254 /* Load a chain with nb_int intermediates (from 01 to nb_int),
1255 * plus one "end-entity" cert (nb_int + 1) */
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001256 ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 nb_int + 1);
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001258 TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, file_buf) == 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001260
1261 /* Try to verify that chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1263 NULL, NULL);
1264 TEST_ASSERT(ret == ret_chk);
1265 TEST_ASSERT(flags == (uint32_t) flags_chk);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001266
1267exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 mbedtls_x509_crt_free(&chain);
1269 mbedtls_x509_crt_free(&trusted);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001270 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001271}
1272/* END_CASE */
1273
1274/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001275void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1276 int flags_result, int result,
1277 char *profile_name, int vrfy_fatal_lvls)
Janos Follath822b2c32015-10-11 10:25:22 +02001278{
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 char *act;
Janos Follath822b2c32015-10-11 10:25:22 +02001280 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001281 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001282 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001283 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +02001284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 mbedtls_x509_crt_init(&chain);
1286 mbedtls_x509_crt_init(&trusted);
Janos Follath822b2c32015-10-11 10:25:22 +02001287
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001288 MD_OR_USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +01001289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 while ((act = mystrsep(&chain_paths, " ")) != NULL) {
1291 TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, act) == 0);
1292 }
1293 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, trusted_ca) == 0);
Janos Follath822b2c32015-10-11 10:25:22 +02001294
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 if (strcmp(profile_name, "") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001296 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 } else if (strcmp(profile_name, "next") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001298 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 } else if (strcmp(profile_name, "suiteb") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001300 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 } else if (strcmp(profile_name, "rsa3072") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001302 profile = &profile_rsa3072;
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 } else if (strcmp(profile_name, "sha512") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001304 profile = &profile_sha512;
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 }
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1308 NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
Janos Follathef4f2582015-10-11 16:17:27 +02001309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 TEST_ASSERT(res == (result));
1311 TEST_ASSERT(flags == (uint32_t) (flags_result));
Janos Follath822b2c32015-10-11 10:25:22 +02001312
1313exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 mbedtls_x509_crt_free(&trusted);
1315 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001316 MD_OR_USE_PSA_DONE();
Janos Follath822b2c32015-10-11 10:25:22 +02001317}
1318/* END_CASE */
1319
Hanno Becker612a2f12020-10-09 09:19:39 +01001320/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001321void x509_oid_desc(data_t *buf, char *ref_desc)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001322{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001324 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001325 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001326
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001329 oid.p = buf->x;
1330 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 ret = mbedtls_oid_get_extended_key_usage(&oid, &desc);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001333
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 if (strcmp(ref_desc, "notfound") == 0) {
1335 TEST_ASSERT(ret != 0);
1336 TEST_ASSERT(desc == NULL);
1337 } else {
1338 TEST_ASSERT(ret == 0);
1339 TEST_ASSERT(desc != NULL);
1340 TEST_ASSERT(strcmp(desc, ref_desc) == 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001341 }
1342}
1343/* END_CASE */
1344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001346void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001347{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001349 char num_buf[100];
1350
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001351 memset(num_buf, 0x2a, sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001354 oid.p = oid_buf->x;
1355 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001356
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001357 TEST_ASSERT((size_t) blen <= sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 TEST_ASSERT(mbedtls_oid_get_numeric_string(num_buf, blen, &oid) == ret);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 if (ret >= 0) {
1362 TEST_ASSERT(num_buf[ret] == 0);
1363 TEST_ASSERT(strcmp(num_buf, numstr) == 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001364 }
1365}
1366/* END_CASE */
1367
TRodziewicz442fdc22021-06-07 13:52:23 +02001368/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001369void x509_check_key_usage(char *crt_file, int usage, int ret)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001370{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001372
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 TEST_ASSERT(mbedtls_x509_crt_check_key_usage(&crt, usage) == ret);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001378
Paul Bakkerbd51b262014-07-10 15:26:12 +02001379exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001381}
1382/* END_CASE */
1383
TRodziewicz442fdc22021-06-07 13:52:23 +02001384/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001385void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1386 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001391
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001392
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001394
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 TEST_ASSERT(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x,
1396 oid->len) == ret);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001397
Paul Bakkerbd51b262014-07-10 15:26:12 +02001398exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001400}
1401/* END_CASE */
1402
Andres AG4b76aec2016-09-23 13:16:02 +01001403/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001404void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1405 int day, int hour, int min, int sec)
Andres AG4b76aec2016-09-23 13:16:02 +01001406{
1407 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001408 unsigned char buf[21];
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 unsigned char *start = buf;
1410 unsigned char *end = buf;
Andres AG4b76aec2016-09-23 13:16:02 +01001411
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 memset(&time, 0x00, sizeof(time));
1413 *end = (unsigned char) tag; end++;
1414 *end = strlen(time_str);
1415 TEST_ASSERT(*end < 20);
Andres AG4b76aec2016-09-23 13:16:02 +01001416 end++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 memcpy(end, time_str, (size_t) *(end - 1));
Andres AG4b76aec2016-09-23 13:16:02 +01001418 end += *(end - 1);
1419
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 TEST_ASSERT(mbedtls_x509_get_time(&start, end, &time) == ret);
1421 if (ret == 0) {
1422 TEST_ASSERT(year == time.year);
1423 TEST_ASSERT(mon == time.mon);
1424 TEST_ASSERT(day == time.day);
1425 TEST_ASSERT(hour == time.hour);
1426 TEST_ASSERT(min == time.min);
1427 TEST_ASSERT(sec == time.sec);
Andres AG4b76aec2016-09-23 13:16:02 +01001428 }
1429}
1430/* END_CASE */
1431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001433void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1434 int ref_msg_md, int ref_mgf_md,
1435 int ref_salt_len, int ref_ret)
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001436{
1437 int my_ret;
Ronald Cronac6ae352020-06-26 14:33:03 +02001438 mbedtls_x509_buf buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001440 int my_salt_len;
1441
Ronald Cronac6ae352020-06-26 14:33:03 +02001442 buf.p = params->x;
1443 buf.len = params->len;
1444 buf.tag = params_tag;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001445
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1447 &my_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 TEST_ASSERT(my_ret == ref_ret);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001450
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 if (ref_ret == 0) {
1452 TEST_ASSERT(my_msg_md == (mbedtls_md_type_t) ref_msg_md);
1453 TEST_ASSERT(my_mgf_md == (mbedtls_md_type_t) ref_mgf_md);
1454 TEST_ASSERT(my_salt_len == ref_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001455 }
1456
Paul Bakkerbd51b262014-07-10 15:26:12 +02001457exit:
Azim Khand30ca132017-06-09 04:32:58 +01001458 ;;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001459}
1460/* END_CASE */