blob: e7f3870ee365b9c6be056068003b4b4c14921a30 [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"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +010011#include "string.h"
Valerio Setti25fd51f2023-02-10 14:57:36 +010012#include "pk_wrap.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 */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200282
283 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 /*
285 * Should not happen.
286 */
287 return -1;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200288 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 ret = mbedtls_snprintf(p, n, "\n");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200290 MBEDTLS_X509_SAFE_SNPRINTF;
291
292 *size = n;
293 *buf = p;
294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200296}
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298int parse_crt_ext_cb(void *p_ctx, mbedtls_x509_crt const *crt, mbedtls_x509_buf const *oid,
299 int critical, const unsigned char *cp, const unsigned char *end)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200300{
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 (void) crt;
302 (void) critical;
303 mbedtls_x509_buf *new_oid = (mbedtls_x509_buf *) p_ctx;
304 if (oid->tag == MBEDTLS_ASN1_OID &&
305 MBEDTLS_OID_CMP(MBEDTLS_OID_CERTIFICATE_POLICIES, oid) == 0) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200306 /* Handle unknown certificate policy */
307 int ret, parse_ret = 0;
308 size_t len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 unsigned char **p = (unsigned char **) &cp;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200310
311 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 ret = mbedtls_asn1_get_tag(p, end, &len,
313 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
314 if (ret != 0) {
315 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
316 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if (*p + len != end) {
319 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
320 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
321 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200322
323 /*
324 * Cannot be an empty sequence.
325 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (len == 0) {
327 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
328 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
329 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 while (*p < end) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200332 const unsigned char *policy_end;
333
334 /*
335 * Get the policy sequence
336 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
338 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
339 0) {
340 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
341 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200342
343 policy_end = *p + len;
344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
346 MBEDTLS_ASN1_OID)) != 0) {
347 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
348 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200349
Nicola Di Lietob77fad82020-06-17 17:57:36 +0200350 /*
351 * Recognize exclusively the policy with OID 1
352 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (len != 1 || *p[0] != 1) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200354 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200356
357 *p += len;
358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 /*
360 * If there is an optional qualifier, then *p < policy_end
361 * Check the Qualifier len to verify it doesn't exceed policy_end.
362 */
363 if (*p < policy_end) {
364 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
365 MBEDTLS_ASN1_CONSTRUCTED |
366 MBEDTLS_ASN1_SEQUENCE)) != 0) {
367 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
368 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200369 /*
370 * Skip the optional policy qualifiers.
371 */
372 *p += len;
373 }
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (*p != policy_end) {
376 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
377 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
378 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200379 }
380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if (*p != end) {
382 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
383 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
384 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 return parse_ret;
387 } else if (new_oid != NULL && new_oid->tag == oid->tag && new_oid->len == oid->len &&
388 memcmp(new_oid->p, oid->p, oid->len) == 0) {
389 return 0;
390 } else {
391 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
392 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200393 }
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200394}
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200395#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200396/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000397
Paul Bakker33b43f12013-08-20 11:48:36 +0200398/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200400 * END_DEPENDENCIES
401 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000402
Thomas Daubney5c9c2ce2022-06-06 16:36:43 +0100403/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404void x509_accessor_ext_types(int ext_type, int has_ext_type)
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100405{
406 mbedtls_x509_crt crt;
407 int expected_result = ext_type & has_ext_type;
408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 mbedtls_x509_crt_init(&crt);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100410
411 crt.ext_types = ext_type;
412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 TEST_ASSERT(mbedtls_x509_crt_has_ext_type(&crt, has_ext_type) == expected_result);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 mbedtls_x509_crt_free(&crt);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100416}
417/* END_CASE */
418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100420void x509_parse_san(char *crt_file, char *result_str)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200421{
Ron Eldor890819a2019-05-13 19:03:04 +0300422 int ret;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200423 mbedtls_x509_crt crt;
Ron Eldor890819a2019-05-13 19:03:04 +0300424 mbedtls_x509_subject_alternative_name san;
425 mbedtls_x509_sequence *cur = NULL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200426 char buf[2000];
427 char *p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 size_t n = sizeof(buf);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 mbedtls_x509_crt_init(&crt);
431 memset(buf, 0, 2000);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Ron Eldor890819a2019-05-13 19:03:04 +0300434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if (crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Ron Eldor890819a2019-05-13 19:03:04 +0300436 cur = &crt.subject_alt_names;
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 while (cur != NULL) {
438 ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
439 TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
Ron Eldor890819a2019-05-13 19:03:04 +0300440 /*
441 * If san type not supported, ignore.
442 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (ret == 0) {
444 TEST_ASSERT(verify_parse_san(&san, &p, &n) == 0);
445 }
Ron Eldor890819a2019-05-13 19:03:04 +0300446 cur = cur->next;
447 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200448 }
449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 TEST_ASSERT(strcmp(buf, result_str) == 0);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200451
452exit:
453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 mbedtls_x509_crt_free(&crt);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200455}
456/* END_CASE */
457
Hanno Becker612a2f12020-10-09 09:19:39 +0100458/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100459void x509_cert_info(char *crt_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000460{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000462 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000463 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 mbedtls_x509_crt_init(&crt);
466 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
469 res = mbedtls_x509_crt_info(buf, 2000, "", &crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 TEST_ASSERT(res != -1);
472 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200475
476exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000478}
Paul Bakker33b43f12013-08-20 11:48:36 +0200479/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000480
Hanno Becker612a2f12020-10-09 09:19:39 +0100481/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100482void mbedtls_x509_crl_info(char *crl_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000483{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000485 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000486 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 mbedtls_x509_crl_init(&crl);
489 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == 0);
492 res = mbedtls_x509_crl_info(buf, 2000, "", &crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 TEST_ASSERT(res != -1);
495 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200498
499exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 mbedtls_x509_crl_free(&crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000501}
Paul Bakker33b43f12013-08-20 11:48:36 +0200502/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000503
Andres AGa39db392016-12-08 17:10:38 +0000504/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100505void mbedtls_x509_crl_parse(char *crl_file, int result)
Andres AGa39db392016-12-08 17:10:38 +0000506{
507 mbedtls_x509_crl crl;
508 char buf[2000];
509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 mbedtls_x509_crl_init(&crl);
511 memset(buf, 0, 2000);
Andres AGa39db392016-12-08 17:10:38 +0000512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == result);
Andres AGa39db392016-12-08 17:10:38 +0000514
515exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 mbedtls_x509_crl_free(&crl);
Andres AGa39db392016-12-08 17:10:38 +0000517}
518/* END_CASE */
519
Hanno Becker612a2f12020-10-09 09:19:39 +0100520/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100521void mbedtls_x509_csr_info(char *csr_file, char *result_str)
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100522{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100524 char buf[2000];
525 int res;
526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 mbedtls_x509_csr_init(&csr);
528 memset(buf, 0, 2000);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 TEST_ASSERT(mbedtls_x509_csr_parse_file(&csr, csr_file) == 0);
531 res = mbedtls_x509_csr_info(buf, 2000, "", &csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 TEST_ASSERT(res != -1);
534 TEST_ASSERT(res != -2);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200537
538exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 mbedtls_x509_csr_free(&csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100540}
541/* END_CASE */
542
Hanno Becker612a2f12020-10-09 09:19:39 +0100543/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100544void x509_verify_info(int flags, char *prefix, char *result_str)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100545{
546 char buf[2000];
547 int res;
548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 memset(buf, 0, sizeof(buf));
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 res = mbedtls_x509_crt_verify_info(buf, sizeof(buf), prefix, flags);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 TEST_ASSERT(res >= 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 TEST_ASSERT(strcmp(buf, result_str) == 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100556}
557/* END_CASE */
558
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200559/* 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 +0100560void x509_verify_restart(char *crt_file, char *ca_file,
561 int result, int flags_result,
562 int max_ops, int min_restart, int max_restart)
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200563{
564 int ret, cnt_restart;
565 mbedtls_x509_crt_restart_ctx rs_ctx;
566 mbedtls_x509_crt crt;
567 mbedtls_x509_crt ca;
568 uint32_t flags = 0;
569
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200570 /*
571 * See comments on ecp_test_vect_restart() for op count precision.
572 *
573 * For reference, with mbed TLS 2.6 and default settings:
574 * - ecdsa_verify() for P-256: ~ 6700
575 * - ecdsa_verify() for P-384: ~ 18800
576 * - x509_verify() for server5 -> test-ca2: ~ 18800
577 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
578 */
579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 mbedtls_x509_crt_restart_init(&rs_ctx);
581 mbedtls_x509_crt_init(&crt);
582 mbedtls_x509_crt_init(&ca);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 USE_PSA_INIT();
Manuel Pégourié-Gonnardcc6e0a62022-12-05 12:55:05 +0100585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
587 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 mbedtls_ecp_set_max_ops(max_ops);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200590
591 cnt_restart = 0;
592 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
594 &mbedtls_x509_crt_profile_default, NULL, &flags,
595 NULL, NULL, &rs_ctx);
596 } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 TEST_ASSERT(ret == result);
599 TEST_ASSERT(flags == (uint32_t) flags_result);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 TEST_ASSERT(cnt_restart >= min_restart);
602 TEST_ASSERT(cnt_restart <= max_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200603
604 /* Do we leak memory when aborting? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
606 &mbedtls_x509_crt_profile_default, NULL, &flags,
607 NULL, NULL, &rs_ctx);
608 TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200609
610exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 mbedtls_x509_crt_restart_free(&rs_ctx);
612 mbedtls_x509_crt_free(&crt);
613 mbedtls_x509_crt_free(&ca);
614 USE_PSA_DONE();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200615}
616/* END_CASE */
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100619void x509_verify(char *crt_file, char *ca_file, char *crl_file,
620 char *cn_name_str, int result, int flags_result,
621 char *profile_str,
622 char *verify_callback)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000623{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 mbedtls_x509_crt crt;
625 mbedtls_x509_crt ca;
626 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200627 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000628 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200629 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 char *cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200631 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 mbedtls_x509_crt_init(&crt);
634 mbedtls_x509_crt_init(&ca);
635 mbedtls_x509_crl_init(&crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +0100638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (strcmp(cn_name_str, "NULL") != 0) {
Paul Bakker33b43f12013-08-20 11:48:36 +0200640 cn_name = cn_name_str;
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 if (strcmp(profile_str, "") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200644 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 } else if (strcmp(profile_str, "next") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200646 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 } else if (strcmp(profile_str, "suite_b") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200648 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 } else if (strcmp(profile_str, "compat") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200650 profile = &compat_profile;
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 } else if (strcmp(profile_str, "all") == 0) {
Hanno Becker20a4ade2019-06-03 14:27:03 +0100652 profile = &profile_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 } else {
654 TEST_ASSERT("Unknown algorithm profile" == 0);
655 }
Gilles Peskineef86ab22017-05-05 18:59:02 +0200656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 if (strcmp(verify_callback, "NULL") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200658 f_vrfy = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 } else if (strcmp(verify_callback, "verify_none") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200660 f_vrfy = verify_none;
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 } else if (strcmp(verify_callback, "verify_all") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200662 f_vrfy = verify_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 } else {
664 TEST_ASSERT("No known verify callback selected" == 0);
665 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
668 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
669 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == 0);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000670
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 res = mbedtls_x509_crt_verify_with_profile(&crt,
672 &ca,
673 &crl,
674 profile,
675 cn_name,
676 &flags,
677 f_vrfy,
678 NULL);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 TEST_EQUAL(res, result);
681 TEST_EQUAL(flags, (uint32_t) flags_result);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200682
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200683#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker0350d562019-03-28 14:23:36 +0000684 /* CRLs aren't supported with CA callbacks, so skip the CA callback
Jarno Lamsadfd22c42019-04-01 15:18:53 +0300685 * version of the test if CRLs are in use. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if (crl_file == NULL || strcmp(crl_file, "") == 0) {
Hanno Becker0350d562019-03-28 14:23:36 +0000687 flags = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 res = mbedtls_x509_crt_verify_with_ca_cb(&crt,
690 ca_callback,
691 &ca,
692 profile,
693 cn_name,
694 &flags,
695 f_vrfy,
696 NULL);
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 TEST_ASSERT(res == (result));
699 TEST_ASSERT(flags == (uint32_t) (flags_result));
Hanno Becker0350d562019-03-28 14:23:36 +0000700 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200701#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200702exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 mbedtls_x509_crt_free(&crt);
704 mbedtls_x509_crt_free(&ca);
705 mbedtls_x509_crl_free(&crl);
706 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000707}
Paul Bakker33b43f12013-08-20 11:48:36 +0200708/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000709
Jarno Lamsa557426a2019-03-27 17:08:29 +0200710/* 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 +0100711void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
712 int exp_ret)
Jarno Lamsa557426a2019-03-27 17:08:29 +0200713{
714 int ret;
715 mbedtls_x509_crt crt;
716 mbedtls_x509_crt ca;
717 uint32_t flags = 0;
Hanno Becker3f932bb2019-03-28 17:06:47 +0000718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 mbedtls_x509_crt_init(&crt);
720 mbedtls_x509_crt_init(&ca);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
723 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200724
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if (strcmp(name, "NULL") == 0) {
Jarno Lamsa557426a2019-03-27 17:08:29 +0200726 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca,
730 &compat_profile, name, &flags,
731 NULL, NULL);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 TEST_ASSERT(ret == exp_ret);
734 TEST_ASSERT(flags == (uint32_t) (-1));
Jarno Lamsa557426a2019-03-27 17:08:29 +0200735exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 mbedtls_x509_crt_free(&crt);
737 mbedtls_x509_crt_free(&ca);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200738}
739/* END_CASE */
740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100742void x509_verify_callback(char *crt_file, char *ca_file, char *name,
743 int exp_ret, char *exp_vrfy_out)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200744{
745 int ret;
746 mbedtls_x509_crt crt;
747 mbedtls_x509_crt ca;
748 uint32_t flags = 0;
749 verify_print_context vrfy_ctx;
750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 mbedtls_x509_crt_init(&crt);
752 mbedtls_x509_crt_init(&ca);
753 verify_print_init(&vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +0100756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
758 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 if (strcmp(name, "NULL") == 0) {
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200761 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 }
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL,
765 &compat_profile,
766 name, &flags,
767 verify_print, &vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 TEST_ASSERT(ret == exp_ret);
770 TEST_ASSERT(strcmp(vrfy_ctx.buf, exp_vrfy_out) == 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200771
772exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 mbedtls_x509_crt_free(&crt);
774 mbedtls_x509_crt_free(&ca);
775 USE_PSA_DONE();
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200776}
777/* END_CASE */
778
Hanno Becker612a2f12020-10-09 09:19:39 +0100779/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100780void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
781 char *new_subject_ou,
782 char *result_str,
783 int ret)
Werner Lewis31ecb962022-06-17 15:51:55 +0100784{
785 mbedtls_x509_crt crt;
786 char buf[2000];
787 int res = 0;
788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 mbedtls_x509_crt_init(&crt);
790 memset(buf, 0, 2000);
Werner Lewis31ecb962022-06-17 15:51:55 +0100791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100793 crt.subject.next->val.p = (unsigned char *) new_subject_ou;
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 crt.subject.next->val.len = strlen(new_subject_ou);
Werner Lewis31ecb962022-06-17 15:51:55 +0100795
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
Werner Lewis31ecb962022-06-17 15:51:55 +0100797
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if (ret != 0) {
799 TEST_ASSERT(res == ret);
800 } else {
801 TEST_ASSERT(res != -1);
802 TEST_ASSERT(res != -2);
803 TEST_ASSERT(strcmp(buf, result_str) == 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100804 }
805exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 mbedtls_x509_crt_free(&crt);
Werner Lewis31ecb962022-06-17 15:51:55 +0100807}
808/* END_CASE */
809
810/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100811void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000812{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000814 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200815 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 mbedtls_x509_crt_init(&crt);
818 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
821 if (strcmp(entity, "subject") == 0) {
822 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
823 } else if (strcmp(entity, "issuer") == 0) {
824 res = mbedtls_x509_dn_gets(buf, 2000, &crt.issuer);
825 } else {
826 TEST_ASSERT("Unknown entity" == 0);
827 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 TEST_ASSERT(res != -1);
830 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200833
834exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000836}
Paul Bakker33b43f12013-08-20 11:48:36 +0200837/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000838
David Horstmanndb73d3b2022-10-04 16:49:16 +0100839/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100840void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
David Horstmanndb73d3b2022-10-04 16:49:16 +0100841{
842 unsigned char *name;
843 unsigned char *p;
844 size_t name_len;
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100845 mbedtls_x509_name head;
David Horstmann3cd67582022-10-17 17:59:10 +0100846 int ret;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 memset(&head, 0, sizeof(head));
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100851 p = name;
852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 ret = mbedtls_x509_get_name(&p, (name + name_len), &head);
854 if (ret == 0) {
855 mbedtls_asn1_free_named_data_list_shallow(head.next);
856 }
David Horstmanndb73d3b2022-10-04 16:49:16 +0100857
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 TEST_EQUAL(ret, exp_ret);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 mbedtls_free(name);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100861}
862/* END_CASE */
863
Werner Lewisb3acb052022-06-17 15:59:58 +0100864/* 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 +0100865void mbedtls_x509_dn_get_next(char *name_str,
866 int next_merged,
867 char *expected_oids,
868 int exp_count,
869 char *exp_dn_gets)
Werner Lewisb3acb052022-06-17 15:59:58 +0100870{
871 int ret = 0, i;
Werner Lewisac80a662022-06-23 11:58:02 +0100872 size_t len = 0, out_size;
Werner Lewisb3acb052022-06-17 15:59:58 +0100873 mbedtls_asn1_named_data *names = NULL;
Glenn Straussa4b40412022-06-26 19:32:09 -0400874 mbedtls_x509_name parsed, *parsed_cur;
Werner Lewisac80a662022-06-23 11:58:02 +0100875 // Size of buf is maximum required for test cases
876 unsigned char buf[80], *out = NULL, *c;
Werner Lewisb3acb052022-06-17 15:59:58 +0100877 const char *short_name;
878
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 memset(&parsed, 0, sizeof(parsed));
880 memset(buf, 0, sizeof(buf));
881 c = buf + sizeof(buf);
Werner Lewisac80a662022-06-23 11:58:02 +0100882 // Additional size required for trailing space
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 out_size = strlen(expected_oids) + 2;
884 ASSERT_ALLOC(out, out_size);
Werner Lewisb3acb052022-06-17 15:59:58 +0100885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 TEST_EQUAL(mbedtls_x509_string_to_names(&names, name_str), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 ret = mbedtls_x509_write_names(&c, buf, names);
889 TEST_LE_S(0, ret);
Werner Lewisb3acb052022-06-17 15:59:58 +0100890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
892 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
893 TEST_EQUAL(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100894
895 // Iterate over names and set next_merged nodes
896 parsed_cur = &parsed;
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 for (; next_merged != 0 && parsed_cur != NULL; next_merged = next_merged >> 1) {
Werner Lewis12657cd2022-06-20 11:47:57 +0100898 parsed_cur->next_merged = next_merged & 0x01;
Werner Lewisb3acb052022-06-17 15:59:58 +0100899 parsed_cur = parsed_cur->next;
900 }
901
902 // Iterate over RDN nodes and print OID of first element to buffer
903 parsed_cur = &parsed;
904 len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 for (i = 0; parsed_cur != NULL; i++) {
906 TEST_EQUAL(mbedtls_oid_get_attr_short_name(&parsed_cur->oid,
907 &short_name), 0);
908 len += mbedtls_snprintf((char *) out + len, out_size - len, "%s ", short_name);
909 parsed_cur = mbedtls_x509_dn_get_next(parsed_cur);
Werner Lewisb3acb052022-06-17 15:59:58 +0100910 }
911 out[len-1] = 0;
912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 TEST_EQUAL(exp_count, i);
914 TEST_EQUAL(strcmp((char *) out, expected_oids), 0);
915 mbedtls_free(out);
Werner Lewisac80a662022-06-23 11:58:02 +0100916 out = NULL;
Werner Lewisb3acb052022-06-17 15:59:58 +0100917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 out_size = strlen(exp_dn_gets) + 1;
919 ASSERT_ALLOC(out, out_size);
Werner Lewisac80a662022-06-23 11:58:02 +0100920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 TEST_LE_S(0, mbedtls_x509_dn_gets((char *) out, out_size, &parsed));
922 TEST_EQUAL(strcmp((char *) out, exp_dn_gets), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100923exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 mbedtls_free(out);
925 mbedtls_asn1_free_named_data_list(&names);
926 mbedtls_asn1_free_named_data_list_shallow(parsed.next);
Werner Lewisb3acb052022-06-17 15:59:58 +0100927}
Werner Lewisb3acb052022-06-17 15:59:58 +0100928/* END_CASE */
929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100931void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000932{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 mbedtls_x509_crt_init(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 if (strcmp(entity, "valid_from") == 0) {
940 TEST_ASSERT(mbedtls_x509_time_is_past(&crt.valid_from) == result);
941 } else if (strcmp(entity, "valid_to") == 0) {
942 TEST_ASSERT(mbedtls_x509_time_is_past(&crt.valid_to) == result);
943 } else {
944 TEST_ASSERT("Unknown entity" == 0);
945 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000946
Paul Bakkerbd51b262014-07-10 15:26:12 +0200947exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000949}
Paul Bakker33b43f12013-08-20 11:48:36 +0200950/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100953void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100954{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 if (strcmp(entity, "valid_from") == 0) {
962 TEST_ASSERT(mbedtls_x509_time_is_future(&crt.valid_from) == result);
963 } else if (strcmp(entity, "valid_to") == 0) {
964 TEST_ASSERT(mbedtls_x509_time_is_future(&crt.valid_to) == result);
965 } else {
966 TEST_ASSERT("Unknown entity" == 0);
967 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100968
Paul Bakkerbd51b262014-07-10 15:26:12 +0200969exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100971}
972/* END_CASE */
973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100975void x509parse_crt_file(char *crt_file, int result)
Paul Bakker5a5fa922014-09-26 14:53:04 +0200976{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +0200978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 mbedtls_x509_crt_init(&crt);
Paul Bakker5a5fa922014-09-26 14:53:04 +0200980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == result);
Paul Bakker5a5fa922014-09-26 14:53:04 +0200982
983exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 mbedtls_x509_crt_free(&crt);
Paul Bakker5a5fa922014-09-26 14:53:04 +0200985}
986/* END_CASE */
987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100989void x509parse_crt(data_t *buf, char *result_str, int result)
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000990{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 mbedtls_x509_crt crt;
Hanno Becker612a2f12020-10-09 09:19:39 +0100992#if !defined(MBEDTLS_X509_REMOVE_INFO)
Hanno Beckerfff2d572020-10-09 09:45:19 +0100993 unsigned char output[2000] = { 0 };
Azim Khanf1aaec92017-05-30 14:23:15 +0100994 int res;
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +0100995#else
996 ((void) result_str);
Peter Kolbus9a969b62018-12-11 13:55:56 -0600997#endif
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 mbedtls_x509_crt_init(&crt);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001000
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 TEST_ASSERT(mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len) == (result));
Hanno Becker612a2f12020-10-09 09:19:39 +01001002#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 if ((result) == 0) {
1004 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1005 TEST_ASSERT(res != -1);
1006 TEST_ASSERT(res != -2);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001009 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001011#endif
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001012
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 mbedtls_x509_crt_free(&crt);
1014 mbedtls_x509_crt_init(&crt);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001015
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 TEST_ASSERT(mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len) == (result));
Hanno Becker612a2f12020-10-09 09:19:39 +01001017#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 if ((result) == 0) {
1019 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakker33b43f12013-08-20 11:48:36 +02001022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 TEST_ASSERT(res != -1);
1024 TEST_ASSERT(res != -2);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001025
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001027 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001029#endif /* !MBEDTLS_X509_REMOVE_INFO */
Paul Bakkerb08e6842012-02-11 18:43:20 +00001030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 mbedtls_x509_crt_free(&crt);
1032 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL,
1035 NULL) == (result));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001036#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 if ((result) == 0) {
1038 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001039
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 TEST_ASSERT(res != -1);
1041 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001042
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001044 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001046#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 mbedtls_x509_crt_free(&crt);
1049 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL,
1052 NULL) == (result));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001053#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if ((result) == 0) {
1055 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 TEST_ASSERT(res != -1);
1058 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001061 }
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001062#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001063
1064exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 mbedtls_x509_crt_free(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001066}
1067/* END_CASE */
1068
1069/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001070void x509parse_crt_cb(data_t *buf, char *result_str, int result)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001071{
1072 mbedtls_x509_crt crt;
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001073 mbedtls_x509_buf oid;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001074
1075#if !defined(MBEDTLS_X509_REMOVE_INFO)
1076 unsigned char output[2000] = { 0 };
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001077 int res;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001078#else
1079 ((void) result_str);
1080#endif
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001081
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001082 oid.tag = MBEDTLS_ASN1_OID;
1083 oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
1089 &oid) == (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001090#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if ((result) == 0) {
1092 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 TEST_ASSERT(res != -1);
1095 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001098 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 memset(output, 0, 2000);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001100#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 mbedtls_x509_crt_free(&crt);
1103 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1106 &oid) == (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001107#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 if ((result) == 0) {
1109 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 TEST_ASSERT(res != -1);
1112 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001115 }
Hanno Beckerfff2d572020-10-09 09:45:19 +01001116#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001117
Paul Bakkerbd51b262014-07-10 15:26:12 +02001118exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 mbedtls_x509_crt_free(&crt);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001120}
Paul Bakker33b43f12013-08-20 11:48:36 +02001121/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001122
Hanno Becker612a2f12020-10-09 09:19:39 +01001123/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124void x509parse_crl(data_t *buf, char *result_str, int result)
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001127 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +01001128 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 mbedtls_x509_crl_init(&crl);
1131 memset(output, 0, 2000);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001132
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 TEST_ASSERT(mbedtls_x509_crl_parse(&crl, buf->x, buf->len) == (result));
1135 if ((result) == 0) {
1136 res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
Paul Bakker33b43f12013-08-20 11:48:36 +02001137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 TEST_ASSERT(res != -1);
1139 TEST_ASSERT(res != -2);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001142 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001143
Paul Bakkerbd51b262014-07-10 15:26:12 +02001144exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 mbedtls_x509_crl_free(&crl);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001146}
Paul Bakker33b43f12013-08-20 11:48:36 +02001147/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001148
Hanno Becker612a2f12020-10-09 09:19:39 +01001149/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001150void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001151{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001153 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001154 int my_ret;
1155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 mbedtls_x509_csr_init(&csr);
1157 memset(my_out, 0, sizeof(my_out));
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
1160 TEST_ASSERT(my_ret == ref_ret);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 if (ref_ret == 0) {
1163 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1164 TEST_ASSERT(my_out_len == strlen(ref_out));
1165 TEST_ASSERT(strcmp(my_out, ref_out) == 0);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001166 }
1167
Paul Bakkerbd51b262014-07-10 15:26:12 +02001168exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 mbedtls_x509_csr_free(&csr);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001170}
1171/* END_CASE */
1172
Przemek Stekield7992df2023-01-25 16:19:50 +01001173/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1174void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
1175{
1176 mbedtls_x509_csr csr;
1177 char my_out[1000];
1178 int my_ret;
1179
1180 mbedtls_x509_csr_init(&csr);
1181 memset(my_out, 0, sizeof(my_out));
1182
1183 my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file);
1184 TEST_ASSERT(my_ret == ref_ret);
1185
1186 if (ref_ret == 0) {
1187 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1188 TEST_ASSERT(my_out_len == strlen(ref_out));
1189 TEST_ASSERT(strcmp(my_out, ref_out) == 0);
1190 }
1191
1192exit:
1193 mbedtls_x509_csr_free(&csr);
1194}
1195/* END_CASE */
1196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001198void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001199{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001201 int i;
1202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 mbedtls_x509_crt_init(&chain);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 TEST_ASSERT(mbedtls_x509_crt_parse_path(&chain, crt_path) == ret);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001206
1207 /* Check how many certs we got */
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1209 if (cur->raw.p != NULL) {
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001210 i++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 }
1212 }
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001213
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 TEST_ASSERT(i == nb_crt);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001215
Paul Bakkerbd51b262014-07-10 15:26:12 +02001216exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001218}
1219/* END_CASE */
1220
Janos Follath822b2c32015-10-11 10:25:22 +02001221/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001222void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1223 int ret_chk, int flags_chk)
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001224{
1225 char file_buf[128];
1226 int ret;
1227 uint32_t flags;
1228 mbedtls_x509_crt trusted, chain;
1229
1230 /*
1231 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1232 * with NN.crt signed by NN-1.crt
1233 */
1234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 mbedtls_x509_crt_init(&trusted);
1236 mbedtls_x509_crt_init(&chain);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001237
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +01001239
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001240 /* Load trusted root */
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, ca_file) == 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001242
1243 /* Load a chain with nb_int intermediates (from 01 to nb_int),
1244 * plus one "end-entity" cert (nb_int + 1) */
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001245 ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 nb_int + 1);
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001247 TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, file_buf) == 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001249
1250 /* Try to verify that chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1252 NULL, NULL);
1253 TEST_ASSERT(ret == ret_chk);
1254 TEST_ASSERT(flags == (uint32_t) flags_chk);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001255
1256exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 mbedtls_x509_crt_free(&chain);
1258 mbedtls_x509_crt_free(&trusted);
1259 USE_PSA_DONE();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001260}
1261/* END_CASE */
1262
1263/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001264void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1265 int flags_result, int result,
1266 char *profile_name, int vrfy_fatal_lvls)
Janos Follath822b2c32015-10-11 10:25:22 +02001267{
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 char *act;
Janos Follath822b2c32015-10-11 10:25:22 +02001269 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001270 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001271 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001272 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +02001273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 mbedtls_x509_crt_init(&chain);
1275 mbedtls_x509_crt_init(&trusted);
Janos Follath822b2c32015-10-11 10:25:22 +02001276
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +01001278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 while ((act = mystrsep(&chain_paths, " ")) != NULL) {
1280 TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, act) == 0);
1281 }
1282 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, trusted_ca) == 0);
Janos Follath822b2c32015-10-11 10:25:22 +02001283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 if (strcmp(profile_name, "") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001285 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 } else if (strcmp(profile_name, "next") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001287 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 } else if (strcmp(profile_name, "suiteb") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001289 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 } else if (strcmp(profile_name, "rsa3072") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001291 profile = &profile_rsa3072;
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 } else if (strcmp(profile_name, "sha512") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001293 profile = &profile_sha512;
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 }
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1297 NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
Janos Follathef4f2582015-10-11 16:17:27 +02001298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 TEST_ASSERT(res == (result));
1300 TEST_ASSERT(flags == (uint32_t) (flags_result));
Janos Follath822b2c32015-10-11 10:25:22 +02001301
1302exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 mbedtls_x509_crt_free(&trusted);
1304 mbedtls_x509_crt_free(&chain);
1305 USE_PSA_DONE();
Janos Follath822b2c32015-10-11 10:25:22 +02001306}
1307/* END_CASE */
1308
Hanno Becker612a2f12020-10-09 09:19:39 +01001309/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001310void x509_oid_desc(data_t *buf, char *ref_desc)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001311{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001313 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001314 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001315
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001318 oid.p = buf->x;
1319 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 ret = mbedtls_oid_get_extended_key_usage(&oid, &desc);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 if (strcmp(ref_desc, "notfound") == 0) {
1324 TEST_ASSERT(ret != 0);
1325 TEST_ASSERT(desc == NULL);
1326 } else {
1327 TEST_ASSERT(ret == 0);
1328 TEST_ASSERT(desc != NULL);
1329 TEST_ASSERT(strcmp(desc, ref_desc) == 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001330 }
1331}
1332/* END_CASE */
1333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001335void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001336{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001338 char num_buf[100];
1339
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001340 memset(num_buf, 0x2a, sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001343 oid.p = oid_buf->x;
1344 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001345
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001346 TEST_ASSERT((size_t) blen <= sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 TEST_ASSERT(mbedtls_oid_get_numeric_string(num_buf, blen, &oid) == ret);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 if (ret >= 0) {
1351 TEST_ASSERT(num_buf[ret] == 0);
1352 TEST_ASSERT(strcmp(num_buf, numstr) == 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001353 }
1354}
1355/* END_CASE */
1356
TRodziewicz442fdc22021-06-07 13:52:23 +02001357/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001358void x509_check_key_usage(char *crt_file, int usage, int ret)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001359{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001363
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001365
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 TEST_ASSERT(mbedtls_x509_crt_check_key_usage(&crt, usage) == ret);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001367
Paul Bakkerbd51b262014-07-10 15:26:12 +02001368exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001370}
1371/* END_CASE */
1372
TRodziewicz442fdc22021-06-07 13:52:23 +02001373/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001374void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1375 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001376{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001378
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001380
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 TEST_ASSERT(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x,
1385 oid->len) == ret);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001386
Paul Bakkerbd51b262014-07-10 15:26:12 +02001387exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001389}
1390/* END_CASE */
1391
Andres AG4b76aec2016-09-23 13:16:02 +01001392/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001393void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1394 int day, int hour, int min, int sec)
Andres AG4b76aec2016-09-23 13:16:02 +01001395{
1396 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001397 unsigned char buf[21];
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 unsigned char *start = buf;
1399 unsigned char *end = buf;
Andres AG4b76aec2016-09-23 13:16:02 +01001400
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 memset(&time, 0x00, sizeof(time));
1402 *end = (unsigned char) tag; end++;
1403 *end = strlen(time_str);
1404 TEST_ASSERT(*end < 20);
Andres AG4b76aec2016-09-23 13:16:02 +01001405 end++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 memcpy(end, time_str, (size_t) *(end - 1));
Andres AG4b76aec2016-09-23 13:16:02 +01001407 end += *(end - 1);
1408
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 TEST_ASSERT(mbedtls_x509_get_time(&start, end, &time) == ret);
1410 if (ret == 0) {
1411 TEST_ASSERT(year == time.year);
1412 TEST_ASSERT(mon == time.mon);
1413 TEST_ASSERT(day == time.day);
1414 TEST_ASSERT(hour == time.hour);
1415 TEST_ASSERT(min == time.min);
1416 TEST_ASSERT(sec == time.sec);
Andres AG4b76aec2016-09-23 13:16:02 +01001417 }
1418}
1419/* END_CASE */
1420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001422void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1423 int ref_msg_md, int ref_mgf_md,
1424 int ref_salt_len, int ref_ret)
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001425{
1426 int my_ret;
Ronald Cronac6ae352020-06-26 14:33:03 +02001427 mbedtls_x509_buf buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001429 int my_salt_len;
1430
Ronald Cronac6ae352020-06-26 14:33:03 +02001431 buf.p = params->x;
1432 buf.len = params->len;
1433 buf.tag = params_tag;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001434
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1436 &my_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 TEST_ASSERT(my_ret == ref_ret);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 if (ref_ret == 0) {
1441 TEST_ASSERT(my_msg_md == (mbedtls_md_type_t) ref_msg_md);
1442 TEST_ASSERT(my_mgf_md == (mbedtls_md_type_t) ref_mgf_md);
1443 TEST_ASSERT(my_salt_len == ref_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001444 }
1445
Paul Bakkerbd51b262014-07-10 15:26:12 +02001446exit:
Azim Khand30ca132017-06-09 04:32:58 +01001447 ;;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001448}
1449/* END_CASE */