blob: 5d896bf73971101e375a4ec695c9437c490e4923 [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"
Paul Bakkerb63b0af2011-01-13 17:54:59 +000012
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020013#include "mbedtls/legacy_or_psa.h"
Przemek Stekield34f8c32022-08-02 09:09:29 +020014
Simon Butcher9e24b512017-07-28 12:15:13 +010015#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
Hanno Becker3b1422e2017-07-26 13:38:02 +010016#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
Gilles Peskine449bd832023-01-11 14:50:10 +010017 than the current threshold 19. To test larger values, please \
18 adapt the script tests/data_files/dir-max/long.sh."
Hanno Becker3b1422e2017-07-26 13:38:02 +010019#endif
20
Hanno Becker20a4ade2019-06-03 14:27:03 +010021/* Test-only profile allowing all digests, PK algorithms, and curves. */
22const mbedtls_x509_crt_profile profile_all =
23{
24 0xFFFFFFFF, /* Any MD */
25 0xFFFFFFFF, /* Any PK alg */
26 0xFFFFFFFF, /* Any curve */
27 1024,
28};
29
Gilles Peskineef86ab22017-05-05 18:59:02 +020030/* Profile for backward compatibility. Allows SHA-1, unlike the default
31 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020032const mbedtls_x509_crt_profile compat_profile =
33{
Gilles Peskine449bd832023-01-11 14:50:10 +010034 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) |
35 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160) |
36 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
37 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
38 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
39 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubney28015e12022-04-21 08:12:59 +010040 0xFFFFFFFF, /* Any PK alg */
41 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020042 1024,
43};
44
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020045const mbedtls_x509_crt_profile profile_rsa3072 =
46{
Gilles Peskine449bd832023-01-11 14:50:10 +010047 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
48 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
49 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
50 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_RSA),
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020051 0,
52 3072,
53};
54
55const mbedtls_x509_crt_profile profile_sha512 =
56{
Gilles Peskine449bd832023-01-11 14:50:10 +010057 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubney28015e12022-04-21 08:12:59 +010058 0xFFFFFFFF, /* Any PK alg */
59 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020060 1024,
61};
62
Gilles Peskine449bd832023-01-11 14:50:10 +010063int verify_none(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000064{
Paul Bakker5a624082011-01-18 16:31:52 +000065 ((void) data);
66 ((void) crt);
67 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010068 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020069
Paul Bakker915275b2012-09-28 07:10:55 +000070 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000071}
72
Gilles Peskine449bd832023-01-11 14:50:10 +010073int verify_all(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000074{
Paul Bakker5a624082011-01-18 16:31:52 +000075 ((void) data);
76 ((void) crt);
77 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000078 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000079
Paul Bakkerb63b0af2011-01-13 17:54:59 +000080 return 0;
81}
82
Jarno Lamsa03cd1202019-03-27 15:45:04 +020083#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +010084int ca_callback_fail(void *data, mbedtls_x509_crt const *child, mbedtls_x509_crt **candidates)
Jarno Lamsa557426a2019-03-27 17:08:29 +020085{
86 ((void) data);
87 ((void) child);
88 ((void) candidates);
89
90 return -1;
91}
Przemek Stekielcd204992022-04-27 15:33:43 +020092#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010093int ca_callback(void *data, mbedtls_x509_crt const *child,
94 mbedtls_x509_crt **candidates)
Jarno Lamsa03cd1202019-03-27 15:45:04 +020095{
Hanno Beckercbb59032019-03-28 14:14:22 +000096 int ret = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +020097 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +000098 mbedtls_x509_crt *first;
99
100 /* This is a test-only implementation of the CA callback
101 * which always returns the entire list of trusted certificates.
102 * Production implementations managing a large number of CAs
103 * should use an efficient presentation and lookup for the
104 * set of trusted certificates (such as a hashtable) and only
105 * return those trusted certificates which satisfy basic
106 * parental checks, such as the matching of child `Issuer`
107 * and parent `Subject` field. */
108 ((void) child);
109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
111 if (first == NULL) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000112 ret = -1;
113 goto exit;
114 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 mbedtls_x509_crt_init(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000118 ret = -1;
119 goto exit;
120 }
121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 while (ca->next != NULL) {
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200123 ca = ca->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000125 ret = -1;
126 goto exit;
127 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200128 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000129
130exit:
131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 if (ret != 0) {
133 mbedtls_x509_crt_free(first);
134 mbedtls_free(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000135 first = NULL;
136 }
137
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200138 *candidates = first;
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 return ret;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200140}
Przemek Stekielcd204992022-04-27 15:33:43 +0200141#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200142#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144int verify_fatal(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200145{
146 int *levels = (int *) data;
147
148 ((void) crt);
149 ((void) certificate_depth);
150
151 /* Simulate a fatal error in the callback */
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 if (*levels & (1 << certificate_depth)) {
153 *flags |= (1 << certificate_depth);
154 return -1 - certificate_depth;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200155 }
156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return 0;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200158}
159
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900160/* strsep() not available on Windows */
161char *mystrsep(char **stringp, const char *delim)
162{
163 const char *p;
164 char *ret = *stringp;
165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 if (*stringp == NULL) {
167 return NULL;
168 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 for (;; (*stringp)++) {
171 if (**stringp == '\0') {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900172 *stringp = NULL;
173 goto done;
174 }
175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 for (p = delim; *p != '\0'; p++) {
177 if (**stringp == *p) {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900178 **stringp = '\0';
179 (*stringp)++;
180 goto done;
181 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900183 }
184
185done:
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 return ret;
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900187}
188
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200189#if defined(MBEDTLS_X509_CRT_PARSE_C)
190typedef struct {
191 char buf[512];
192 char *p;
193} verify_print_context;
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195void verify_print_init(verify_print_context *ctx)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200196{
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 memset(ctx, 0, sizeof(verify_print_context));
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200198 ctx->p = ctx->buf;
199}
200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201int verify_print(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200202{
203 int ret;
204 verify_print_context *ctx = (verify_print_context *) data;
205 char *p = ctx->p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 size_t n = ctx->buf + sizeof(ctx->buf) - ctx->p;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200207 ((void) flags);
208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 ret = mbedtls_snprintf(p, n, "depth %d - serial ", certificate_depth);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200210 MBEDTLS_X509_SAFE_SNPRINTF;
211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200213 MBEDTLS_X509_SAFE_SNPRINTF;
214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 ret = mbedtls_snprintf(p, n, " - subject ");
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200216 MBEDTLS_X509_SAFE_SNPRINTF;
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200219 MBEDTLS_X509_SAFE_SNPRINTF;
220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 ret = mbedtls_snprintf(p, n, " - flags 0x%08x\n", *flags);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200222 MBEDTLS_X509_SAFE_SNPRINTF;
223
224 ctx->p = p;
225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return 0;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200227}
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229int verify_parse_san(mbedtls_x509_subject_alternative_name *san,
230 char **buf, size_t *size)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200231{
232 int ret;
233 size_t i;
234 char *p = *buf;
235 size_t n = *size;
236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 ret = mbedtls_snprintf(p, n, "type : %d", san->type);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200238 MBEDTLS_X509_SAFE_SNPRINTF;
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 switch (san->type) {
241 case (MBEDTLS_X509_SAN_OTHER_NAME):
242 ret = mbedtls_snprintf(p, n, "\notherName :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300243 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
246 &san->san.other_name.value.hardware_module_name.oid) != 0) {
247 ret = mbedtls_snprintf(p, n, " hardware module name :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300248 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 ret = mbedtls_snprintf(p, n, " hardware type : ");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300250 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 ret = mbedtls_oid_get_numeric_string(p,
253 n,
254 &san->san.other_name.value.hardware_module_name.oid);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300255 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 ret = mbedtls_snprintf(p, n, ", hardware serial number : ");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300258 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 for (i = 0; i < san->san.other_name.value.hardware_module_name.val.len; i++) {
261 ret = mbedtls_snprintf(p,
262 n,
263 "%02X",
264 san->san.other_name.value.hardware_module_name.val.p[i]);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300265 MBEDTLS_X509_SAFE_SNPRINTF;
266 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200267 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 break;/* MBEDTLS_OID_ON_HW_MODULE_NAME */
269 case (MBEDTLS_X509_SAN_DNS_NAME):
270 ret = mbedtls_snprintf(p, n, "\ndNSName : ");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200271 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (san->san.unstructured_name.len >= n) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200273 *p = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200275 }
276 n -= san->san.unstructured_name.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 for (i = 0; i < san->san.unstructured_name.len; i++) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200278 *p++ = san->san.unstructured_name.p[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 }
280 break;/* MBEDTLS_X509_SAN_DNS_NAME */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200281
282 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 /*
284 * Should not happen.
285 */
286 return -1;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200287 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 ret = mbedtls_snprintf(p, n, "\n");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200289 MBEDTLS_X509_SAFE_SNPRINTF;
290
291 *size = n;
292 *buf = p;
293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200295}
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297int parse_crt_ext_cb(void *p_ctx, mbedtls_x509_crt const *crt, mbedtls_x509_buf const *oid,
298 int critical, const unsigned char *cp, const unsigned char *end)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200299{
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 (void) crt;
301 (void) critical;
302 mbedtls_x509_buf *new_oid = (mbedtls_x509_buf *) p_ctx;
303 if (oid->tag == MBEDTLS_ASN1_OID &&
304 MBEDTLS_OID_CMP(MBEDTLS_OID_CERTIFICATE_POLICIES, oid) == 0) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200305 /* Handle unknown certificate policy */
306 int ret, parse_ret = 0;
307 size_t len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 unsigned char **p = (unsigned char **) &cp;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200309
310 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 ret = mbedtls_asn1_get_tag(p, end, &len,
312 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
313 if (ret != 0) {
314 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
315 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if (*p + len != end) {
318 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
319 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
320 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200321
322 /*
323 * Cannot be an empty sequence.
324 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (len == 0) {
326 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
327 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
328 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 while (*p < end) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200331 const unsigned char *policy_end;
332
333 /*
334 * Get the policy sequence
335 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
337 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
338 0) {
339 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
340 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200341
342 policy_end = *p + len;
343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
345 MBEDTLS_ASN1_OID)) != 0) {
346 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
347 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200348
Nicola Di Lietob77fad82020-06-17 17:57:36 +0200349 /*
350 * Recognize exclusively the policy with OID 1
351 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if (len != 1 || *p[0] != 1) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200353 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200355
356 *p += len;
357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 /*
359 * If there is an optional qualifier, then *p < policy_end
360 * Check the Qualifier len to verify it doesn't exceed policy_end.
361 */
362 if (*p < policy_end) {
363 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
364 MBEDTLS_ASN1_CONSTRUCTED |
365 MBEDTLS_ASN1_SEQUENCE)) != 0) {
366 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
367 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200368 /*
369 * Skip the optional policy qualifiers.
370 */
371 *p += len;
372 }
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if (*p != policy_end) {
375 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
376 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
377 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200378 }
379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (*p != end) {
381 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
382 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
383 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 return parse_ret;
386 } else if (new_oid != NULL && new_oid->tag == oid->tag && new_oid->len == oid->len &&
387 memcmp(new_oid->p, oid->p, oid->len) == 0) {
388 return 0;
389 } else {
390 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
391 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200392 }
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200393}
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200394#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200395/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000396
Paul Bakker33b43f12013-08-20 11:48:36 +0200397/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200399 * END_DEPENDENCIES
400 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000401
Thomas Daubney5c9c2ce2022-06-06 16:36:43 +0100402/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100403void x509_accessor_ext_types(int ext_type, int has_ext_type)
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100404{
405 mbedtls_x509_crt crt;
406 int expected_result = ext_type & has_ext_type;
407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 mbedtls_x509_crt_init(&crt);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100409
410 crt.ext_types = ext_type;
411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 TEST_ASSERT(mbedtls_x509_crt_has_ext_type(&crt, has_ext_type) == expected_result);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 mbedtls_x509_crt_free(&crt);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100415}
416/* END_CASE */
417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100419void x509_parse_san(char *crt_file, char *result_str)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200420{
Ron Eldor890819a2019-05-13 19:03:04 +0300421 int ret;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200422 mbedtls_x509_crt crt;
Ron Eldor890819a2019-05-13 19:03:04 +0300423 mbedtls_x509_subject_alternative_name san;
424 mbedtls_x509_sequence *cur = NULL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200425 char buf[2000];
426 char *p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 size_t n = sizeof(buf);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 mbedtls_x509_crt_init(&crt);
430 memset(buf, 0, 2000);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Ron Eldor890819a2019-05-13 19:03:04 +0300433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 if (crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Ron Eldor890819a2019-05-13 19:03:04 +0300435 cur = &crt.subject_alt_names;
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 while (cur != NULL) {
437 ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
438 TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
Ron Eldor890819a2019-05-13 19:03:04 +0300439 /*
440 * If san type not supported, ignore.
441 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if (ret == 0) {
443 TEST_ASSERT(verify_parse_san(&san, &p, &n) == 0);
444 }
Ron Eldor890819a2019-05-13 19:03:04 +0300445 cur = cur->next;
446 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200447 }
448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 TEST_ASSERT(strcmp(buf, result_str) == 0);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200450
451exit:
452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 mbedtls_x509_crt_free(&crt);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200454}
455/* END_CASE */
456
Hanno Becker612a2f12020-10-09 09:19:39 +0100457/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100458void x509_cert_info(char *crt_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000459{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000461 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000462 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 mbedtls_x509_crt_init(&crt);
465 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
468 res = mbedtls_x509_crt_info(buf, 2000, "", &crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 TEST_ASSERT(res != -1);
471 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200474
475exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000477}
Paul Bakker33b43f12013-08-20 11:48:36 +0200478/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000479
Hanno Becker612a2f12020-10-09 09:19:39 +0100480/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100481void mbedtls_x509_crl_info(char *crl_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000482{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000484 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000485 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 mbedtls_x509_crl_init(&crl);
488 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == 0);
491 res = mbedtls_x509_crl_info(buf, 2000, "", &crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 TEST_ASSERT(res != -1);
494 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200497
498exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 mbedtls_x509_crl_free(&crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000500}
Paul Bakker33b43f12013-08-20 11:48:36 +0200501/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000502
Andres AGa39db392016-12-08 17:10:38 +0000503/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100504void mbedtls_x509_crl_parse(char *crl_file, int result)
Andres AGa39db392016-12-08 17:10:38 +0000505{
506 mbedtls_x509_crl crl;
507 char buf[2000];
508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 mbedtls_x509_crl_init(&crl);
510 memset(buf, 0, 2000);
Andres AGa39db392016-12-08 17:10:38 +0000511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == result);
Andres AGa39db392016-12-08 17:10:38 +0000513
514exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 mbedtls_x509_crl_free(&crl);
Andres AGa39db392016-12-08 17:10:38 +0000516}
517/* END_CASE */
518
Hanno Becker612a2f12020-10-09 09:19:39 +0100519/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100520void mbedtls_x509_csr_info(char *csr_file, char *result_str)
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100521{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100523 char buf[2000];
524 int res;
525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 mbedtls_x509_csr_init(&csr);
527 memset(buf, 0, 2000);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 TEST_ASSERT(mbedtls_x509_csr_parse_file(&csr, csr_file) == 0);
530 res = mbedtls_x509_csr_info(buf, 2000, "", &csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 TEST_ASSERT(res != -1);
533 TEST_ASSERT(res != -2);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200536
537exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 mbedtls_x509_csr_free(&csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100539}
540/* END_CASE */
541
Hanno Becker612a2f12020-10-09 09:19:39 +0100542/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100543void x509_verify_info(int flags, char *prefix, char *result_str)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100544{
545 char buf[2000];
546 int res;
547
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 memset(buf, 0, sizeof(buf));
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 res = mbedtls_x509_crt_verify_info(buf, sizeof(buf), prefix, flags);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 TEST_ASSERT(res >= 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 TEST_ASSERT(strcmp(buf, result_str) == 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100555}
556/* END_CASE */
557
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200558/* 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 +0100559void x509_verify_restart(char *crt_file, char *ca_file,
560 int result, int flags_result,
561 int max_ops, int min_restart, int max_restart)
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200562{
563 int ret, cnt_restart;
564 mbedtls_x509_crt_restart_ctx rs_ctx;
565 mbedtls_x509_crt crt;
566 mbedtls_x509_crt ca;
567 uint32_t flags = 0;
568
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200569 /*
570 * See comments on ecp_test_vect_restart() for op count precision.
571 *
572 * For reference, with mbed TLS 2.6 and default settings:
573 * - ecdsa_verify() for P-256: ~ 6700
574 * - ecdsa_verify() for P-384: ~ 18800
575 * - x509_verify() for server5 -> test-ca2: ~ 18800
576 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
577 */
578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 mbedtls_x509_crt_restart_init(&rs_ctx);
580 mbedtls_x509_crt_init(&crt);
581 mbedtls_x509_crt_init(&ca);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 USE_PSA_INIT();
Manuel Pégourié-Gonnardcc6e0a62022-12-05 12:55:05 +0100584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
586 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 mbedtls_ecp_set_max_ops(max_ops);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200589
590 cnt_restart = 0;
591 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
593 &mbedtls_x509_crt_profile_default, NULL, &flags,
594 NULL, NULL, &rs_ctx);
595 } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 TEST_ASSERT(ret == result);
598 TEST_ASSERT(flags == (uint32_t) flags_result);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 TEST_ASSERT(cnt_restart >= min_restart);
601 TEST_ASSERT(cnt_restart <= max_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200602
603 /* Do we leak memory when aborting? */
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 TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200608
609exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 mbedtls_x509_crt_restart_free(&rs_ctx);
611 mbedtls_x509_crt_free(&crt);
612 mbedtls_x509_crt_free(&ca);
613 USE_PSA_DONE();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200614}
615/* END_CASE */
616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100618void x509_verify(char *crt_file, char *ca_file, char *crl_file,
619 char *cn_name_str, int result, int flags_result,
620 char *profile_str,
621 char *verify_callback)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000622{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_x509_crt crt;
624 mbedtls_x509_crt ca;
625 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200626 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000627 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200628 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 char *cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200630 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 mbedtls_x509_crt_init(&crt);
633 mbedtls_x509_crt_init(&ca);
634 mbedtls_x509_crl_init(&crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +0100637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (strcmp(cn_name_str, "NULL") != 0) {
Paul Bakker33b43f12013-08-20 11:48:36 +0200639 cn_name = cn_name_str;
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200641
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 if (strcmp(profile_str, "") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200643 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 } else if (strcmp(profile_str, "next") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200645 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 } else if (strcmp(profile_str, "suite_b") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200647 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 } else if (strcmp(profile_str, "compat") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200649 profile = &compat_profile;
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 } else if (strcmp(profile_str, "all") == 0) {
Hanno Becker20a4ade2019-06-03 14:27:03 +0100651 profile = &profile_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 } else {
653 TEST_ASSERT("Unknown algorithm profile" == 0);
654 }
Gilles Peskineef86ab22017-05-05 18:59:02 +0200655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (strcmp(verify_callback, "NULL") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200657 f_vrfy = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 } else if (strcmp(verify_callback, "verify_none") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200659 f_vrfy = verify_none;
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 } else if (strcmp(verify_callback, "verify_all") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200661 f_vrfy = verify_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 } else {
663 TEST_ASSERT("No known verify callback selected" == 0);
664 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
667 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
668 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == 0);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 res = mbedtls_x509_crt_verify_with_profile(&crt,
671 &ca,
672 &crl,
673 profile,
674 cn_name,
675 &flags,
676 f_vrfy,
677 NULL);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 TEST_EQUAL(res, result);
680 TEST_EQUAL(flags, (uint32_t) flags_result);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200681
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200682#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker0350d562019-03-28 14:23:36 +0000683 /* CRLs aren't supported with CA callbacks, so skip the CA callback
Jarno Lamsadfd22c42019-04-01 15:18:53 +0300684 * version of the test if CRLs are in use. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (crl_file == NULL || strcmp(crl_file, "") == 0) {
Hanno Becker0350d562019-03-28 14:23:36 +0000686 flags = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 res = mbedtls_x509_crt_verify_with_ca_cb(&crt,
689 ca_callback,
690 &ca,
691 profile,
692 cn_name,
693 &flags,
694 f_vrfy,
695 NULL);
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 TEST_ASSERT(res == (result));
698 TEST_ASSERT(flags == (uint32_t) (flags_result));
Hanno Becker0350d562019-03-28 14:23:36 +0000699 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200700#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200701exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 mbedtls_x509_crt_free(&crt);
703 mbedtls_x509_crt_free(&ca);
704 mbedtls_x509_crl_free(&crl);
705 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000706}
Paul Bakker33b43f12013-08-20 11:48:36 +0200707/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000708
Jarno Lamsa557426a2019-03-27 17:08:29 +0200709/* 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 +0100710void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
711 int exp_ret)
Jarno Lamsa557426a2019-03-27 17:08:29 +0200712{
713 int ret;
714 mbedtls_x509_crt crt;
715 mbedtls_x509_crt ca;
716 uint32_t flags = 0;
Hanno Becker3f932bb2019-03-28 17:06:47 +0000717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 mbedtls_x509_crt_init(&crt);
719 mbedtls_x509_crt_init(&ca);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
722 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (strcmp(name, "NULL") == 0) {
Jarno Lamsa557426a2019-03-27 17:08:29 +0200725 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca,
729 &compat_profile, name, &flags,
730 NULL, NULL);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 TEST_ASSERT(ret == exp_ret);
733 TEST_ASSERT(flags == (uint32_t) (-1));
Jarno Lamsa557426a2019-03-27 17:08:29 +0200734exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 mbedtls_x509_crt_free(&crt);
736 mbedtls_x509_crt_free(&ca);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200737}
738/* END_CASE */
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100741void x509_verify_callback(char *crt_file, char *ca_file, char *name,
742 int exp_ret, char *exp_vrfy_out)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200743{
744 int ret;
745 mbedtls_x509_crt crt;
746 mbedtls_x509_crt ca;
747 uint32_t flags = 0;
748 verify_print_context vrfy_ctx;
749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 mbedtls_x509_crt_init(&crt);
751 mbedtls_x509_crt_init(&ca);
752 verify_print_init(&vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +0100755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
757 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 if (strcmp(name, "NULL") == 0) {
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200760 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 }
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL,
764 &compat_profile,
765 name, &flags,
766 verify_print, &vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 TEST_ASSERT(ret == exp_ret);
769 TEST_ASSERT(strcmp(vrfy_ctx.buf, exp_vrfy_out) == 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200770
771exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 mbedtls_x509_crt_free(&crt);
773 mbedtls_x509_crt_free(&ca);
774 USE_PSA_DONE();
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200775}
776/* END_CASE */
777
Hanno Becker612a2f12020-10-09 09:19:39 +0100778/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100779void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
780 char *new_subject_ou,
781 char *result_str,
782 int ret)
Werner Lewis31ecb962022-06-17 15:51:55 +0100783{
784 mbedtls_x509_crt crt;
785 char buf[2000];
786 int res = 0;
787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 mbedtls_x509_crt_init(&crt);
789 memset(buf, 0, 2000);
Werner Lewis31ecb962022-06-17 15:51:55 +0100790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100792 crt.subject.next->val.p = (unsigned char *) new_subject_ou;
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 crt.subject.next->val.len = strlen(new_subject_ou);
Werner Lewis31ecb962022-06-17 15:51:55 +0100794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
Werner Lewis31ecb962022-06-17 15:51:55 +0100796
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 if (ret != 0) {
798 TEST_ASSERT(res == ret);
799 } else {
800 TEST_ASSERT(res != -1);
801 TEST_ASSERT(res != -2);
802 TEST_ASSERT(strcmp(buf, result_str) == 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100803 }
804exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 mbedtls_x509_crt_free(&crt);
Werner Lewis31ecb962022-06-17 15:51:55 +0100806}
807/* END_CASE */
808
809/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000811{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000813 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200814 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 mbedtls_x509_crt_init(&crt);
817 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
820 if (strcmp(entity, "subject") == 0) {
821 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
822 } else if (strcmp(entity, "issuer") == 0) {
823 res = mbedtls_x509_dn_gets(buf, 2000, &crt.issuer);
824 } else {
825 TEST_ASSERT("Unknown entity" == 0);
826 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 TEST_ASSERT(res != -1);
829 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200832
833exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000835}
Paul Bakker33b43f12013-08-20 11:48:36 +0200836/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000837
David Horstmanndb73d3b2022-10-04 16:49:16 +0100838/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
David Horstmanndb73d3b2022-10-04 16:49:16 +0100840{
841 unsigned char *name;
842 unsigned char *p;
843 size_t name_len;
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100844 mbedtls_x509_name head;
David Horstmann3cd67582022-10-17 17:59:10 +0100845 int ret;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 memset(&head, 0, sizeof(head));
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100850 p = name;
851
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 ret = mbedtls_x509_get_name(&p, (name + name_len), &head);
853 if (ret == 0) {
854 mbedtls_asn1_free_named_data_list_shallow(head.next);
855 }
David Horstmanndb73d3b2022-10-04 16:49:16 +0100856
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 TEST_EQUAL(ret, exp_ret);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100858
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 mbedtls_free(name);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100860}
861/* END_CASE */
862
Werner Lewisb3acb052022-06-17 15:59:58 +0100863/* 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 +0100864void mbedtls_x509_dn_get_next(char *name_str,
865 int next_merged,
866 char *expected_oids,
867 int exp_count,
868 char *exp_dn_gets)
Werner Lewisb3acb052022-06-17 15:59:58 +0100869{
870 int ret = 0, i;
Werner Lewisac80a662022-06-23 11:58:02 +0100871 size_t len = 0, out_size;
Werner Lewisb3acb052022-06-17 15:59:58 +0100872 mbedtls_asn1_named_data *names = NULL;
Glenn Straussa4b40412022-06-26 19:32:09 -0400873 mbedtls_x509_name parsed, *parsed_cur;
Werner Lewisac80a662022-06-23 11:58:02 +0100874 // Size of buf is maximum required for test cases
875 unsigned char buf[80], *out = NULL, *c;
Werner Lewisb3acb052022-06-17 15:59:58 +0100876 const char *short_name;
877
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 memset(&parsed, 0, sizeof(parsed));
879 memset(buf, 0, sizeof(buf));
880 c = buf + sizeof(buf);
Werner Lewisac80a662022-06-23 11:58:02 +0100881 // Additional size required for trailing space
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 out_size = strlen(expected_oids) + 2;
883 ASSERT_ALLOC(out, out_size);
Werner Lewisb3acb052022-06-17 15:59:58 +0100884
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 TEST_EQUAL(mbedtls_x509_string_to_names(&names, name_str), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 ret = mbedtls_x509_write_names(&c, buf, names);
888 TEST_LE_S(0, ret);
Werner Lewisb3acb052022-06-17 15:59:58 +0100889
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
891 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
892 TEST_EQUAL(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100893
894 // Iterate over names and set next_merged nodes
895 parsed_cur = &parsed;
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 for (; next_merged != 0 && parsed_cur != NULL; next_merged = next_merged >> 1) {
Werner Lewis12657cd2022-06-20 11:47:57 +0100897 parsed_cur->next_merged = next_merged & 0x01;
Werner Lewisb3acb052022-06-17 15:59:58 +0100898 parsed_cur = parsed_cur->next;
899 }
900
901 // Iterate over RDN nodes and print OID of first element to buffer
902 parsed_cur = &parsed;
903 len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 for (i = 0; parsed_cur != NULL; i++) {
905 TEST_EQUAL(mbedtls_oid_get_attr_short_name(&parsed_cur->oid,
906 &short_name), 0);
907 len += mbedtls_snprintf((char *) out + len, out_size - len, "%s ", short_name);
908 parsed_cur = mbedtls_x509_dn_get_next(parsed_cur);
Werner Lewisb3acb052022-06-17 15:59:58 +0100909 }
910 out[len-1] = 0;
911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 TEST_EQUAL(exp_count, i);
913 TEST_EQUAL(strcmp((char *) out, expected_oids), 0);
914 mbedtls_free(out);
Werner Lewisac80a662022-06-23 11:58:02 +0100915 out = NULL;
Werner Lewisb3acb052022-06-17 15:59:58 +0100916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 out_size = strlen(exp_dn_gets) + 1;
918 ASSERT_ALLOC(out, out_size);
Werner Lewisac80a662022-06-23 11:58:02 +0100919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 TEST_LE_S(0, mbedtls_x509_dn_gets((char *) out, out_size, &parsed));
921 TEST_EQUAL(strcmp((char *) out, exp_dn_gets), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100922exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 mbedtls_free(out);
924 mbedtls_asn1_free_named_data_list(&names);
925 mbedtls_asn1_free_named_data_list_shallow(parsed.next);
Werner Lewisb3acb052022-06-17 15:59:58 +0100926}
Werner Lewisb3acb052022-06-17 15:59:58 +0100927/* END_CASE */
928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100930void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000931{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 mbedtls_x509_crt_init(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000935
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if (strcmp(entity, "valid_from") == 0) {
939 TEST_ASSERT(mbedtls_x509_time_is_past(&crt.valid_from) == result);
940 } else if (strcmp(entity, "valid_to") == 0) {
941 TEST_ASSERT(mbedtls_x509_time_is_past(&crt.valid_to) == result);
942 } else {
943 TEST_ASSERT("Unknown entity" == 0);
944 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000945
Paul Bakkerbd51b262014-07-10 15:26:12 +0200946exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000948}
Paul Bakker33b43f12013-08-20 11:48:36 +0200949/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100952void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100953{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100957
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100959
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 if (strcmp(entity, "valid_from") == 0) {
961 TEST_ASSERT(mbedtls_x509_time_is_future(&crt.valid_from) == result);
962 } else if (strcmp(entity, "valid_to") == 0) {
963 TEST_ASSERT(mbedtls_x509_time_is_future(&crt.valid_to) == result);
964 } else {
965 TEST_ASSERT("Unknown entity" == 0);
966 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100967
Paul Bakkerbd51b262014-07-10 15:26:12 +0200968exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100970}
971/* END_CASE */
972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100974void x509parse_crt_file(char *crt_file, int result)
Paul Bakker5a5fa922014-09-26 14:53:04 +0200975{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +0200977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 mbedtls_x509_crt_init(&crt);
Paul Bakker5a5fa922014-09-26 14:53:04 +0200979
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == result);
Paul Bakker5a5fa922014-09-26 14:53:04 +0200981
982exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 mbedtls_x509_crt_free(&crt);
Paul Bakker5a5fa922014-09-26 14:53:04 +0200984}
985/* END_CASE */
986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100988void x509parse_crt(data_t *buf, char *result_str, int result)
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000989{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 mbedtls_x509_crt crt;
Hanno Becker612a2f12020-10-09 09:19:39 +0100991#if !defined(MBEDTLS_X509_REMOVE_INFO)
Hanno Beckerfff2d572020-10-09 09:45:19 +0100992 unsigned char output[2000] = { 0 };
Azim Khanf1aaec92017-05-30 14:23:15 +0100993 int res;
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +0100994#else
995 ((void) result_str);
Peter Kolbus9a969b62018-12-11 13:55:56 -0600996#endif
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 mbedtls_x509_crt_init(&crt);
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 TEST_ASSERT(mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len) == (result));
Hanno Becker612a2f12020-10-09 09:19:39 +01001001#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 if ((result) == 0) {
1003 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1004 TEST_ASSERT(res != -1);
1005 TEST_ASSERT(res != -2);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001008 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001010#endif
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 mbedtls_x509_crt_free(&crt);
1013 mbedtls_x509_crt_init(&crt);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001014
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 TEST_ASSERT(mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len) == (result));
Hanno Becker612a2f12020-10-09 09:19:39 +01001016#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 if ((result) == 0) {
1018 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakker33b43f12013-08-20 11:48:36 +02001021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 TEST_ASSERT(res != -1);
1023 TEST_ASSERT(res != -2);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001026 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001028#endif /* !MBEDTLS_X509_REMOVE_INFO */
Paul Bakkerb08e6842012-02-11 18:43:20 +00001029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 mbedtls_x509_crt_free(&crt);
1031 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001032
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL,
1034 NULL) == (result));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001035#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 if ((result) == 0) {
1037 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001038
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 TEST_ASSERT(res != -1);
1040 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001043 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001045#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 mbedtls_x509_crt_free(&crt);
1048 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001049
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL,
1051 NULL) == (result));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001052#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if ((result) == 0) {
1054 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 TEST_ASSERT(res != -1);
1057 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001060 }
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001061#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001062
1063exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 mbedtls_x509_crt_free(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001065}
1066/* END_CASE */
1067
1068/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001069void x509parse_crt_cb(data_t *buf, char *result_str, int result)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001070{
1071 mbedtls_x509_crt crt;
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001072 mbedtls_x509_buf oid;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001073
1074#if !defined(MBEDTLS_X509_REMOVE_INFO)
1075 unsigned char output[2000] = { 0 };
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001076 int res;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001077#else
1078 ((void) result_str);
1079#endif
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001080
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001081 oid.tag = MBEDTLS_ASN1_OID;
1082 oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
1088 &oid) == (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001089#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 if ((result) == 0) {
1091 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001092
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 TEST_ASSERT(res != -1);
1094 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001097 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 memset(output, 0, 2000);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001099#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001100
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 mbedtls_x509_crt_free(&crt);
1102 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1105 &oid) == (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001106#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 if ((result) == 0) {
1108 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 TEST_ASSERT(res != -1);
1111 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001112
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001114 }
Hanno Beckerfff2d572020-10-09 09:45:19 +01001115#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001116
Paul Bakkerbd51b262014-07-10 15:26:12 +02001117exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 mbedtls_x509_crt_free(&crt);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001119}
Paul Bakker33b43f12013-08-20 11:48:36 +02001120/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001121
Hanno Becker612a2f12020-10-09 09:19:39 +01001122/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001123void x509parse_crl(data_t *buf, char *result_str, int result)
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001124{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001126 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +01001127 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 mbedtls_x509_crl_init(&crl);
1130 memset(output, 0, 2000);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001131
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 TEST_ASSERT(mbedtls_x509_crl_parse(&crl, buf->x, buf->len) == (result));
1134 if ((result) == 0) {
1135 res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
Paul Bakker33b43f12013-08-20 11:48:36 +02001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 TEST_ASSERT(res != -1);
1138 TEST_ASSERT(res != -2);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001141 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001142
Paul Bakkerbd51b262014-07-10 15:26:12 +02001143exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 mbedtls_x509_crl_free(&crl);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001145}
Paul Bakker33b43f12013-08-20 11:48:36 +02001146/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001147
Hanno Becker612a2f12020-10-09 09:19:39 +01001148/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001149void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001150{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001152 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001153 int my_ret;
1154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 mbedtls_x509_csr_init(&csr);
1156 memset(my_out, 0, sizeof(my_out));
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
1159 TEST_ASSERT(my_ret == ref_ret);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 if (ref_ret == 0) {
1162 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1163 TEST_ASSERT(my_out_len == strlen(ref_out));
1164 TEST_ASSERT(strcmp(my_out, ref_out) == 0);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001165 }
1166
Paul Bakkerbd51b262014-07-10 15:26:12 +02001167exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 mbedtls_x509_csr_free(&csr);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001169}
1170/* END_CASE */
1171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001173void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001174{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001176 int i;
1177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 mbedtls_x509_crt_init(&chain);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 TEST_ASSERT(mbedtls_x509_crt_parse_path(&chain, crt_path) == ret);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001181
1182 /* Check how many certs we got */
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1184 if (cur->raw.p != NULL) {
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001185 i++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 }
1187 }
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 TEST_ASSERT(i == nb_crt);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001190
Paul Bakkerbd51b262014-07-10 15:26:12 +02001191exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001193}
1194/* END_CASE */
1195
Janos Follath822b2c32015-10-11 10:25:22 +02001196/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001197void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1198 int ret_chk, int flags_chk)
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001199{
1200 char file_buf[128];
1201 int ret;
1202 uint32_t flags;
1203 mbedtls_x509_crt trusted, chain;
1204
1205 /*
1206 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1207 * with NN.crt signed by NN-1.crt
1208 */
1209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 mbedtls_x509_crt_init(&trusted);
1211 mbedtls_x509_crt_init(&chain);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +01001214
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001215 /* Load trusted root */
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, ca_file) == 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001217
1218 /* Load a chain with nb_int intermediates (from 01 to nb_int),
1219 * plus one "end-entity" cert (nb_int + 1) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 ret = mbedtls_snprintf(file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
1221 nb_int + 1);
1222 TEST_ASSERT(ret > 0 && (size_t) ret < sizeof file_buf);
1223 TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, file_buf) == 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001224
1225 /* Try to verify that chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1227 NULL, NULL);
1228 TEST_ASSERT(ret == ret_chk);
1229 TEST_ASSERT(flags == (uint32_t) flags_chk);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001230
1231exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 mbedtls_x509_crt_free(&chain);
1233 mbedtls_x509_crt_free(&trusted);
1234 USE_PSA_DONE();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001235}
1236/* END_CASE */
1237
1238/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001239void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1240 int flags_result, int result,
1241 char *profile_name, int vrfy_fatal_lvls)
Janos Follath822b2c32015-10-11 10:25:22 +02001242{
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 char *act;
Janos Follath822b2c32015-10-11 10:25:22 +02001244 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001245 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001246 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001247 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +02001248
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 mbedtls_x509_crt_init(&chain);
1250 mbedtls_x509_crt_init(&trusted);
Janos Follath822b2c32015-10-11 10:25:22 +02001251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +01001253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 while ((act = mystrsep(&chain_paths, " ")) != NULL) {
1255 TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, act) == 0);
1256 }
1257 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, trusted_ca) == 0);
Janos Follath822b2c32015-10-11 10:25:22 +02001258
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 if (strcmp(profile_name, "") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001260 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 } else if (strcmp(profile_name, "next") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001262 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 } else if (strcmp(profile_name, "suiteb") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001264 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 } else if (strcmp(profile_name, "rsa3072") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001266 profile = &profile_rsa3072;
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 } else if (strcmp(profile_name, "sha512") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001268 profile = &profile_sha512;
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 }
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1272 NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
Janos Follathef4f2582015-10-11 16:17:27 +02001273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 TEST_ASSERT(res == (result));
1275 TEST_ASSERT(flags == (uint32_t) (flags_result));
Janos Follath822b2c32015-10-11 10:25:22 +02001276
1277exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 mbedtls_x509_crt_free(&trusted);
1279 mbedtls_x509_crt_free(&chain);
1280 USE_PSA_DONE();
Janos Follath822b2c32015-10-11 10:25:22 +02001281}
1282/* END_CASE */
1283
Hanno Becker612a2f12020-10-09 09:19:39 +01001284/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001285void x509_oid_desc(data_t *buf, char *ref_desc)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001286{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001288 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001289 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001290
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001293 oid.p = buf->x;
1294 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 ret = mbedtls_oid_get_extended_key_usage(&oid, &desc);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 if (strcmp(ref_desc, "notfound") == 0) {
1299 TEST_ASSERT(ret != 0);
1300 TEST_ASSERT(desc == NULL);
1301 } else {
1302 TEST_ASSERT(ret == 0);
1303 TEST_ASSERT(desc != NULL);
1304 TEST_ASSERT(strcmp(desc, ref_desc) == 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001305 }
1306}
1307/* END_CASE */
1308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001310void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
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é-Gonnard7afdb882014-03-28 16:06:35 +01001313 char num_buf[100];
1314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 memset(num_buf, 0x2a, sizeof num_buf);
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 = oid_buf->x;
1319 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 TEST_ASSERT((size_t) blen <= sizeof num_buf);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 TEST_ASSERT(mbedtls_oid_get_numeric_string(num_buf, blen, &oid) == ret);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 if (ret >= 0) {
1326 TEST_ASSERT(num_buf[ret] == 0);
1327 TEST_ASSERT(strcmp(num_buf, numstr) == 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001328 }
1329}
1330/* END_CASE */
1331
TRodziewicz442fdc22021-06-07 13:52:23 +02001332/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001333void x509_check_key_usage(char *crt_file, int usage, int ret)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001334{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001340
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 TEST_ASSERT(mbedtls_x509_crt_check_key_usage(&crt, usage) == ret);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001342
Paul Bakkerbd51b262014-07-10 15:26:12 +02001343exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001345}
1346/* END_CASE */
1347
TRodziewicz442fdc22021-06-07 13:52:23 +02001348/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001349void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1350 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001351{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001355
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 TEST_ASSERT(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x,
1360 oid->len) == ret);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001361
Paul Bakkerbd51b262014-07-10 15:26:12 +02001362exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001364}
1365/* END_CASE */
1366
Andres AG4b76aec2016-09-23 13:16:02 +01001367/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001368void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1369 int day, int hour, int min, int sec)
Andres AG4b76aec2016-09-23 13:16:02 +01001370{
1371 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001372 unsigned char buf[21];
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 unsigned char *start = buf;
1374 unsigned char *end = buf;
Andres AG4b76aec2016-09-23 13:16:02 +01001375
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 memset(&time, 0x00, sizeof(time));
1377 *end = (unsigned char) tag; end++;
1378 *end = strlen(time_str);
1379 TEST_ASSERT(*end < 20);
Andres AG4b76aec2016-09-23 13:16:02 +01001380 end++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 memcpy(end, time_str, (size_t) *(end - 1));
Andres AG4b76aec2016-09-23 13:16:02 +01001382 end += *(end - 1);
1383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 TEST_ASSERT(mbedtls_x509_get_time(&start, end, &time) == ret);
1385 if (ret == 0) {
1386 TEST_ASSERT(year == time.year);
1387 TEST_ASSERT(mon == time.mon);
1388 TEST_ASSERT(day == time.day);
1389 TEST_ASSERT(hour == time.hour);
1390 TEST_ASSERT(min == time.min);
1391 TEST_ASSERT(sec == time.sec);
Andres AG4b76aec2016-09-23 13:16:02 +01001392 }
1393}
1394/* END_CASE */
1395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001397void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1398 int ref_msg_md, int ref_mgf_md,
1399 int ref_salt_len, int ref_ret)
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001400{
1401 int my_ret;
Ronald Cronac6ae352020-06-26 14:33:03 +02001402 mbedtls_x509_buf buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001404 int my_salt_len;
1405
Ronald Cronac6ae352020-06-26 14:33:03 +02001406 buf.p = params->x;
1407 buf.len = params->len;
1408 buf.tag = params_tag;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001409
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1411 &my_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001412
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 TEST_ASSERT(my_ret == ref_ret);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 if (ref_ret == 0) {
1416 TEST_ASSERT(my_msg_md == (mbedtls_md_type_t) ref_msg_md);
1417 TEST_ASSERT(my_mgf_md == (mbedtls_md_type_t) ref_mgf_md);
1418 TEST_ASSERT(my_salt_len == ref_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001419 }
1420
Paul Bakkerbd51b262014-07-10 15:26:12 +02001421exit:
Azim Khand30ca132017-06-09 04:32:58 +01001422 ;;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001423}
1424/* END_CASE */