blob: ed1dcaf8fcbacd1e32750fc94af5c97c42f67828 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Azim Khand30ca132017-06-09 04:32:58 +01002#include "mbedtls/bignum.h"
Andres AG4b76aec2016-09-23 13:16:02 +01003#include "mbedtls/x509.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00004#include "mbedtls/x509_crt.h"
5#include "mbedtls/x509_crl.h"
6#include "mbedtls/x509_csr.h"
7#include "mbedtls/pem.h"
8#include "mbedtls/oid.h"
9#include "mbedtls/base64.h"
Chris Jones9f7a6932021-04-14 12:12:09 +010010#include "mbedtls/error.h"
Valerio Setti178b5bd2023-02-13 10:04:28 +010011#include "mbedtls/pk.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +010012#include "string.h"
Paul Bakkerb63b0af2011-01-13 17:54:59 +000013
Simon Butcher9e24b512017-07-28 12:15:13 +010014#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
Hanno Becker3b1422e2017-07-26 13:38:02 +010015#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
Gilles Peskine449bd832023-01-11 14:50:10 +010016 than the current threshold 19. To test larger values, please \
17 adapt the script tests/data_files/dir-max/long.sh."
Hanno Becker3b1422e2017-07-26 13:38:02 +010018#endif
19
Hanno Becker20a4ade2019-06-03 14:27:03 +010020/* Test-only profile allowing all digests, PK algorithms, and curves. */
21const mbedtls_x509_crt_profile profile_all =
22{
23 0xFFFFFFFF, /* Any MD */
24 0xFFFFFFFF, /* Any PK alg */
25 0xFFFFFFFF, /* Any curve */
26 1024,
27};
28
Gilles Peskineef86ab22017-05-05 18:59:02 +020029/* Profile for backward compatibility. Allows SHA-1, unlike the default
30 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020031const mbedtls_x509_crt_profile compat_profile =
32{
Gilles Peskine449bd832023-01-11 14:50:10 +010033 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) |
34 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160) |
35 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
36 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
37 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
38 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubney28015e12022-04-21 08:12:59 +010039 0xFFFFFFFF, /* Any PK alg */
40 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020041 1024,
42};
43
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020044const mbedtls_x509_crt_profile profile_rsa3072 =
45{
Gilles Peskine449bd832023-01-11 14:50:10 +010046 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
47 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
48 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
49 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_RSA),
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020050 0,
51 3072,
52};
53
54const mbedtls_x509_crt_profile profile_sha512 =
55{
Gilles Peskine449bd832023-01-11 14:50:10 +010056 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubney28015e12022-04-21 08:12:59 +010057 0xFFFFFFFF, /* Any PK alg */
58 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020059 1024,
60};
61
Gilles Peskine449bd832023-01-11 14:50:10 +010062int verify_none(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000063{
Paul Bakker5a624082011-01-18 16:31:52 +000064 ((void) data);
65 ((void) crt);
66 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010067 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020068
Paul Bakker915275b2012-09-28 07:10:55 +000069 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000070}
71
Gilles Peskine449bd832023-01-11 14:50:10 +010072int verify_all(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000073{
Paul Bakker5a624082011-01-18 16:31:52 +000074 ((void) data);
75 ((void) crt);
76 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000077 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000078
Paul Bakkerb63b0af2011-01-13 17:54:59 +000079 return 0;
80}
81
Jarno Lamsa03cd1202019-03-27 15:45:04 +020082#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +010083int ca_callback_fail(void *data, mbedtls_x509_crt const *child, mbedtls_x509_crt **candidates)
Jarno Lamsa557426a2019-03-27 17:08:29 +020084{
85 ((void) data);
86 ((void) child);
87 ((void) candidates);
88
89 return -1;
90}
Przemek Stekielcd204992022-04-27 15:33:43 +020091#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010092int ca_callback(void *data, mbedtls_x509_crt const *child,
93 mbedtls_x509_crt **candidates)
Jarno Lamsa03cd1202019-03-27 15:45:04 +020094{
Hanno Beckercbb59032019-03-28 14:14:22 +000095 int ret = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +020096 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +000097 mbedtls_x509_crt *first;
98
99 /* This is a test-only implementation of the CA callback
100 * which always returns the entire list of trusted certificates.
101 * Production implementations managing a large number of CAs
102 * should use an efficient presentation and lookup for the
103 * set of trusted certificates (such as a hashtable) and only
104 * return those trusted certificates which satisfy basic
105 * parental checks, such as the matching of child `Issuer`
106 * and parent `Subject` field. */
107 ((void) child);
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
110 if (first == NULL) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000111 ret = -1;
112 goto exit;
113 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 mbedtls_x509_crt_init(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000117 ret = -1;
118 goto exit;
119 }
120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 while (ca->next != NULL) {
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200122 ca = ca->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000124 ret = -1;
125 goto exit;
126 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200127 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000128
129exit:
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 if (ret != 0) {
132 mbedtls_x509_crt_free(first);
133 mbedtls_free(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000134 first = NULL;
135 }
136
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200137 *candidates = first;
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 return ret;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200139}
Przemek Stekielcd204992022-04-27 15:33:43 +0200140#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200141#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143int verify_fatal(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200144{
145 int *levels = (int *) data;
146
147 ((void) crt);
148 ((void) certificate_depth);
149
150 /* Simulate a fatal error in the callback */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 if (*levels & (1 << certificate_depth)) {
152 *flags |= (1 << certificate_depth);
153 return -1 - certificate_depth;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200154 }
155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return 0;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200157}
158
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900159/* strsep() not available on Windows */
160char *mystrsep(char **stringp, const char *delim)
161{
162 const char *p;
163 char *ret = *stringp;
164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if (*stringp == NULL) {
166 return NULL;
167 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 for (;; (*stringp)++) {
170 if (**stringp == '\0') {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900171 *stringp = NULL;
172 goto done;
173 }
174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 for (p = delim; *p != '\0'; p++) {
176 if (**stringp == *p) {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900177 **stringp = '\0';
178 (*stringp)++;
179 goto done;
180 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900182 }
183
184done:
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return ret;
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900186}
187
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200188#if defined(MBEDTLS_X509_CRT_PARSE_C)
189typedef struct {
190 char buf[512];
191 char *p;
192} verify_print_context;
193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194void verify_print_init(verify_print_context *ctx)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200195{
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 memset(ctx, 0, sizeof(verify_print_context));
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200197 ctx->p = ctx->buf;
198}
199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200int verify_print(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200201{
202 int ret;
203 verify_print_context *ctx = (verify_print_context *) data;
204 char *p = ctx->p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 size_t n = ctx->buf + sizeof(ctx->buf) - ctx->p;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200206 ((void) flags);
207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 ret = mbedtls_snprintf(p, n, "depth %d - serial ", certificate_depth);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200209 MBEDTLS_X509_SAFE_SNPRINTF;
210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200212 MBEDTLS_X509_SAFE_SNPRINTF;
213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 ret = mbedtls_snprintf(p, n, " - subject ");
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200215 MBEDTLS_X509_SAFE_SNPRINTF;
216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200218 MBEDTLS_X509_SAFE_SNPRINTF;
219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 ret = mbedtls_snprintf(p, n, " - flags 0x%08x\n", *flags);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200221 MBEDTLS_X509_SAFE_SNPRINTF;
222
223 ctx->p = p;
224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return 0;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200226}
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228int verify_parse_san(mbedtls_x509_subject_alternative_name *san,
229 char **buf, size_t *size)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200230{
231 int ret;
232 size_t i;
233 char *p = *buf;
234 size_t n = *size;
235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 ret = mbedtls_snprintf(p, n, "type : %d", san->type);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200237 MBEDTLS_X509_SAFE_SNPRINTF;
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 switch (san->type) {
240 case (MBEDTLS_X509_SAN_OTHER_NAME):
241 ret = mbedtls_snprintf(p, n, "\notherName :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300242 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
245 &san->san.other_name.value.hardware_module_name.oid) != 0) {
246 ret = mbedtls_snprintf(p, n, " hardware module name :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300247 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 ret = mbedtls_snprintf(p, n, " hardware type : ");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300249 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 ret = mbedtls_oid_get_numeric_string(p,
252 n,
253 &san->san.other_name.value.hardware_module_name.oid);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300254 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 ret = mbedtls_snprintf(p, n, ", hardware serial number : ");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300257 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 for (i = 0; i < san->san.other_name.value.hardware_module_name.val.len; i++) {
260 ret = mbedtls_snprintf(p,
261 n,
262 "%02X",
263 san->san.other_name.value.hardware_module_name.val.p[i]);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300264 MBEDTLS_X509_SAFE_SNPRINTF;
265 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200266 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 break;/* MBEDTLS_OID_ON_HW_MODULE_NAME */
268 case (MBEDTLS_X509_SAN_DNS_NAME):
269 ret = mbedtls_snprintf(p, n, "\ndNSName : ");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200270 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 if (san->san.unstructured_name.len >= n) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200272 *p = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200274 }
275 n -= san->san.unstructured_name.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 for (i = 0; i < san->san.unstructured_name.len; i++) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200277 *p++ = san->san.unstructured_name.p[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 }
279 break;/* MBEDTLS_X509_SAN_DNS_NAME */
Przemek Stekiel608e3ef2023-02-09 14:47:50 +0100280 case (MBEDTLS_X509_SAN_RFC822_NAME):
281 ret = mbedtls_snprintf(p, n, "\nrfc822Name : ");
282 MBEDTLS_X509_SAFE_SNPRINTF;
283 if (san->san.unstructured_name.len >= n) {
284 *p = '\0';
285 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
286 }
287 n -= san->san.unstructured_name.len;
288 for (i = 0; i < san->san.unstructured_name.len; i++) {
289 *p++ = san->san.unstructured_name.p[i];
290 }
291 break;/* MBEDTLS_X509_SAN_RFC822_NAME */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200292 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 /*
294 * Should not happen.
295 */
296 return -1;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200297 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 ret = mbedtls_snprintf(p, n, "\n");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200299 MBEDTLS_X509_SAFE_SNPRINTF;
300
301 *size = n;
302 *buf = p;
303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200305}
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307int parse_crt_ext_cb(void *p_ctx, mbedtls_x509_crt const *crt, mbedtls_x509_buf const *oid,
308 int critical, const unsigned char *cp, const unsigned char *end)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200309{
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 (void) crt;
311 (void) critical;
312 mbedtls_x509_buf *new_oid = (mbedtls_x509_buf *) p_ctx;
313 if (oid->tag == MBEDTLS_ASN1_OID &&
314 MBEDTLS_OID_CMP(MBEDTLS_OID_CERTIFICATE_POLICIES, oid) == 0) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200315 /* Handle unknown certificate policy */
316 int ret, parse_ret = 0;
317 size_t len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 unsigned char **p = (unsigned char **) &cp;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200319
320 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 ret = mbedtls_asn1_get_tag(p, end, &len,
322 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
323 if (ret != 0) {
324 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
325 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 if (*p + len != end) {
328 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
329 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
330 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200331
332 /*
333 * Cannot be an empty sequence.
334 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 if (len == 0) {
336 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
337 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
338 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 while (*p < end) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200341 const unsigned char *policy_end;
342
343 /*
344 * Get the policy sequence
345 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
347 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
348 0) {
349 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
350 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200351
352 policy_end = *p + len;
353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
355 MBEDTLS_ASN1_OID)) != 0) {
356 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
357 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200358
Nicola Di Lietob77fad82020-06-17 17:57:36 +0200359 /*
360 * Recognize exclusively the policy with OID 1
361 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if (len != 1 || *p[0] != 1) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200363 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200365
366 *p += len;
367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 /*
369 * If there is an optional qualifier, then *p < policy_end
370 * Check the Qualifier len to verify it doesn't exceed policy_end.
371 */
372 if (*p < policy_end) {
373 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
374 MBEDTLS_ASN1_CONSTRUCTED |
375 MBEDTLS_ASN1_SEQUENCE)) != 0) {
376 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
377 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200378 /*
379 * Skip the optional policy qualifiers.
380 */
381 *p += len;
382 }
383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (*p != policy_end) {
385 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
386 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
387 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200388 }
389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 if (*p != end) {
391 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
392 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
393 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return parse_ret;
396 } else if (new_oid != NULL && new_oid->tag == oid->tag && new_oid->len == oid->len &&
397 memcmp(new_oid->p, oid->p, oid->len) == 0) {
398 return 0;
399 } else {
400 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
401 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200402 }
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200403}
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200404#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200405/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000406
Paul Bakker33b43f12013-08-20 11:48:36 +0200407/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200409 * END_DEPENDENCIES
410 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000411
Thomas Daubney5c9c2ce2022-06-06 16:36:43 +0100412/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100413void x509_accessor_ext_types(int ext_type, int has_ext_type)
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100414{
415 mbedtls_x509_crt crt;
416 int expected_result = ext_type & has_ext_type;
417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 mbedtls_x509_crt_init(&crt);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100419
420 crt.ext_types = ext_type;
421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 TEST_ASSERT(mbedtls_x509_crt_has_ext_type(&crt, has_ext_type) == expected_result);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 mbedtls_x509_crt_free(&crt);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100425}
426/* END_CASE */
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100429void x509_parse_san(char *crt_file, char *result_str)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200430{
Ron Eldor890819a2019-05-13 19:03:04 +0300431 int ret;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200432 mbedtls_x509_crt crt;
Ron Eldor890819a2019-05-13 19:03:04 +0300433 mbedtls_x509_subject_alternative_name san;
434 mbedtls_x509_sequence *cur = NULL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200435 char buf[2000];
436 char *p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 size_t n = sizeof(buf);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 mbedtls_x509_crt_init(&crt);
440 memset(buf, 0, 2000);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Ron Eldor890819a2019-05-13 19:03:04 +0300443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Ron Eldor890819a2019-05-13 19:03:04 +0300445 cur = &crt.subject_alt_names;
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 while (cur != NULL) {
447 ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
448 TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
Ron Eldor890819a2019-05-13 19:03:04 +0300449 /*
450 * If san type not supported, ignore.
451 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if (ret == 0) {
453 TEST_ASSERT(verify_parse_san(&san, &p, &n) == 0);
454 }
Ron Eldor890819a2019-05-13 19:03:04 +0300455 cur = cur->next;
456 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200457 }
458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 TEST_ASSERT(strcmp(buf, result_str) == 0);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200460
461exit:
462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 mbedtls_x509_crt_free(&crt);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200464}
465/* END_CASE */
466
Hanno Becker612a2f12020-10-09 09:19:39 +0100467/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100468void x509_cert_info(char *crt_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000469{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000471 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000472 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 mbedtls_x509_crt_init(&crt);
475 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
478 res = mbedtls_x509_crt_info(buf, 2000, "", &crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 TEST_ASSERT(res != -1);
481 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200484
485exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000487}
Paul Bakker33b43f12013-08-20 11:48:36 +0200488/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000489
Hanno Becker612a2f12020-10-09 09:19:39 +0100490/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100491void mbedtls_x509_crl_info(char *crl_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000492{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000494 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000495 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 mbedtls_x509_crl_init(&crl);
498 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == 0);
501 res = mbedtls_x509_crl_info(buf, 2000, "", &crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 TEST_ASSERT(res != -1);
504 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200507
508exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 mbedtls_x509_crl_free(&crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000510}
Paul Bakker33b43f12013-08-20 11:48:36 +0200511/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000512
Andres AGa39db392016-12-08 17:10:38 +0000513/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100514void mbedtls_x509_crl_parse(char *crl_file, int result)
Andres AGa39db392016-12-08 17:10:38 +0000515{
516 mbedtls_x509_crl crl;
517 char buf[2000];
518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 mbedtls_x509_crl_init(&crl);
520 memset(buf, 0, 2000);
Andres AGa39db392016-12-08 17:10:38 +0000521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == result);
Andres AGa39db392016-12-08 17:10:38 +0000523
524exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 mbedtls_x509_crl_free(&crl);
Andres AGa39db392016-12-08 17:10:38 +0000526}
527/* END_CASE */
528
Hanno Becker612a2f12020-10-09 09:19:39 +0100529/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100530void mbedtls_x509_csr_info(char *csr_file, char *result_str)
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100531{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100533 char buf[2000];
534 int res;
535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 mbedtls_x509_csr_init(&csr);
537 memset(buf, 0, 2000);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 TEST_ASSERT(mbedtls_x509_csr_parse_file(&csr, csr_file) == 0);
540 res = mbedtls_x509_csr_info(buf, 2000, "", &csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 TEST_ASSERT(res != -1);
543 TEST_ASSERT(res != -2);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200546
547exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 mbedtls_x509_csr_free(&csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100549}
550/* END_CASE */
551
Hanno Becker612a2f12020-10-09 09:19:39 +0100552/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100553void x509_verify_info(int flags, char *prefix, char *result_str)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100554{
555 char buf[2000];
556 int res;
557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 memset(buf, 0, sizeof(buf));
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 res = mbedtls_x509_crt_verify_info(buf, sizeof(buf), prefix, flags);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 TEST_ASSERT(res >= 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 TEST_ASSERT(strcmp(buf, result_str) == 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100565}
566/* END_CASE */
567
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200568/* 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 +0100569void x509_verify_restart(char *crt_file, char *ca_file,
570 int result, int flags_result,
571 int max_ops, int min_restart, int max_restart)
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200572{
573 int ret, cnt_restart;
574 mbedtls_x509_crt_restart_ctx rs_ctx;
575 mbedtls_x509_crt crt;
576 mbedtls_x509_crt ca;
577 uint32_t flags = 0;
578
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200579 /*
580 * See comments on ecp_test_vect_restart() for op count precision.
581 *
582 * For reference, with mbed TLS 2.6 and default settings:
583 * - ecdsa_verify() for P-256: ~ 6700
584 * - ecdsa_verify() for P-384: ~ 18800
585 * - x509_verify() for server5 -> test-ca2: ~ 18800
586 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
587 */
588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 mbedtls_x509_crt_restart_init(&rs_ctx);
590 mbedtls_x509_crt_init(&crt);
591 mbedtls_x509_crt_init(&ca);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200592
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100593 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnardcc6e0a62022-12-05 12:55:05 +0100594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
596 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 mbedtls_ecp_set_max_ops(max_ops);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200599
600 cnt_restart = 0;
601 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
603 &mbedtls_x509_crt_profile_default, NULL, &flags,
604 NULL, NULL, &rs_ctx);
605 } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 TEST_ASSERT(ret == result);
608 TEST_ASSERT(flags == (uint32_t) flags_result);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 TEST_ASSERT(cnt_restart >= min_restart);
611 TEST_ASSERT(cnt_restart <= max_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200612
613 /* Do we leak memory when aborting? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
615 &mbedtls_x509_crt_profile_default, NULL, &flags,
616 NULL, NULL, &rs_ctx);
617 TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200618
619exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 mbedtls_x509_crt_restart_free(&rs_ctx);
621 mbedtls_x509_crt_free(&crt);
622 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100623 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200624}
625/* END_CASE */
626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100628void x509_verify(char *crt_file, char *ca_file, char *crl_file,
629 char *cn_name_str, int result, int flags_result,
630 char *profile_str,
631 char *verify_callback)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000632{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_x509_crt crt;
634 mbedtls_x509_crt ca;
635 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200636 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000637 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200638 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 char *cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200640 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000641
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 mbedtls_x509_crt_init(&crt);
643 mbedtls_x509_crt_init(&ca);
644 mbedtls_x509_crl_init(&crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000645
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100646 MD_OR_USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +0100647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 if (strcmp(cn_name_str, "NULL") != 0) {
Paul Bakker33b43f12013-08-20 11:48:36 +0200649 cn_name = cn_name_str;
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (strcmp(profile_str, "") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200653 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 } else if (strcmp(profile_str, "next") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200655 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 } else if (strcmp(profile_str, "suite_b") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200657 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 } else if (strcmp(profile_str, "compat") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200659 profile = &compat_profile;
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 } else if (strcmp(profile_str, "all") == 0) {
Hanno Becker20a4ade2019-06-03 14:27:03 +0100661 profile = &profile_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 } else {
663 TEST_ASSERT("Unknown algorithm profile" == 0);
664 }
Gilles Peskineef86ab22017-05-05 18:59:02 +0200665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (strcmp(verify_callback, "NULL") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200667 f_vrfy = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 } else if (strcmp(verify_callback, "verify_none") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200669 f_vrfy = verify_none;
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 } else if (strcmp(verify_callback, "verify_all") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200671 f_vrfy = verify_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 } else {
673 TEST_ASSERT("No known verify callback selected" == 0);
674 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
677 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
678 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == 0);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 res = mbedtls_x509_crt_verify_with_profile(&crt,
681 &ca,
682 &crl,
683 profile,
684 cn_name,
685 &flags,
686 f_vrfy,
687 NULL);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 TEST_EQUAL(res, result);
690 TEST_EQUAL(flags, (uint32_t) flags_result);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200691
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200692#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker0350d562019-03-28 14:23:36 +0000693 /* CRLs aren't supported with CA callbacks, so skip the CA callback
Jarno Lamsadfd22c42019-04-01 15:18:53 +0300694 * version of the test if CRLs are in use. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 if (crl_file == NULL || strcmp(crl_file, "") == 0) {
Hanno Becker0350d562019-03-28 14:23:36 +0000696 flags = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 res = mbedtls_x509_crt_verify_with_ca_cb(&crt,
699 ca_callback,
700 &ca,
701 profile,
702 cn_name,
703 &flags,
704 f_vrfy,
705 NULL);
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 TEST_ASSERT(res == (result));
708 TEST_ASSERT(flags == (uint32_t) (flags_result));
Hanno Becker0350d562019-03-28 14:23:36 +0000709 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200710#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200711exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 mbedtls_x509_crt_free(&crt);
713 mbedtls_x509_crt_free(&ca);
714 mbedtls_x509_crl_free(&crl);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100715 MD_OR_USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000716}
Paul Bakker33b43f12013-08-20 11:48:36 +0200717/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000718
Jarno Lamsa557426a2019-03-27 17:08:29 +0200719/* 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 +0100720void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
721 int exp_ret)
Jarno Lamsa557426a2019-03-27 17:08:29 +0200722{
723 int ret;
724 mbedtls_x509_crt crt;
725 mbedtls_x509_crt ca;
726 uint32_t flags = 0;
Hanno Becker3f932bb2019-03-28 17:06:47 +0000727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 mbedtls_x509_crt_init(&crt);
729 mbedtls_x509_crt_init(&ca);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
732 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if (strcmp(name, "NULL") == 0) {
Jarno Lamsa557426a2019-03-27 17:08:29 +0200735 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca,
739 &compat_profile, name, &flags,
740 NULL, NULL);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 TEST_ASSERT(ret == exp_ret);
743 TEST_ASSERT(flags == (uint32_t) (-1));
Jarno Lamsa557426a2019-03-27 17:08:29 +0200744exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 mbedtls_x509_crt_free(&crt);
746 mbedtls_x509_crt_free(&ca);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200747}
748/* END_CASE */
749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100751void x509_verify_callback(char *crt_file, char *ca_file, char *name,
752 int exp_ret, char *exp_vrfy_out)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200753{
754 int ret;
755 mbedtls_x509_crt crt;
756 mbedtls_x509_crt ca;
757 uint32_t flags = 0;
758 verify_print_context vrfy_ctx;
759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 mbedtls_x509_crt_init(&crt);
761 mbedtls_x509_crt_init(&ca);
762 verify_print_init(&vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200763
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100764 MD_OR_USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +0100765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
767 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if (strcmp(name, "NULL") == 0) {
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200770 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 }
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL,
774 &compat_profile,
775 name, &flags,
776 verify_print, &vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 TEST_ASSERT(ret == exp_ret);
779 TEST_ASSERT(strcmp(vrfy_ctx.buf, exp_vrfy_out) == 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200780
781exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 mbedtls_x509_crt_free(&crt);
783 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100784 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200785}
786/* END_CASE */
787
Hanno Becker612a2f12020-10-09 09:19:39 +0100788/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100789void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
790 char *new_subject_ou,
791 char *result_str,
792 int ret)
Werner Lewis31ecb962022-06-17 15:51:55 +0100793{
794 mbedtls_x509_crt crt;
795 char buf[2000];
796 int res = 0;
797
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 mbedtls_x509_crt_init(&crt);
799 memset(buf, 0, 2000);
Werner Lewis31ecb962022-06-17 15:51:55 +0100800
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100802 crt.subject.next->val.p = (unsigned char *) new_subject_ou;
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 crt.subject.next->val.len = strlen(new_subject_ou);
Werner Lewis31ecb962022-06-17 15:51:55 +0100804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
Werner Lewis31ecb962022-06-17 15:51:55 +0100806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if (ret != 0) {
808 TEST_ASSERT(res == ret);
809 } else {
810 TEST_ASSERT(res != -1);
811 TEST_ASSERT(res != -2);
812 TEST_ASSERT(strcmp(buf, result_str) == 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100813 }
814exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 mbedtls_x509_crt_free(&crt);
Werner Lewis31ecb962022-06-17 15:51:55 +0100816}
817/* END_CASE */
818
819/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000821{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000823 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200824 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 mbedtls_x509_crt_init(&crt);
827 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
830 if (strcmp(entity, "subject") == 0) {
831 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
832 } else if (strcmp(entity, "issuer") == 0) {
833 res = mbedtls_x509_dn_gets(buf, 2000, &crt.issuer);
834 } else {
835 TEST_ASSERT("Unknown entity" == 0);
836 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 TEST_ASSERT(res != -1);
839 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200842
843exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000845}
Paul Bakker33b43f12013-08-20 11:48:36 +0200846/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000847
David Horstmanndb73d3b2022-10-04 16:49:16 +0100848/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100849void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
David Horstmanndb73d3b2022-10-04 16:49:16 +0100850{
851 unsigned char *name;
852 unsigned char *p;
853 size_t name_len;
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100854 mbedtls_x509_name head;
David Horstmann3cd67582022-10-17 17:59:10 +0100855 int ret;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100856
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 memset(&head, 0, sizeof(head));
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100858
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100860 p = name;
861
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 ret = mbedtls_x509_get_name(&p, (name + name_len), &head);
863 if (ret == 0) {
864 mbedtls_asn1_free_named_data_list_shallow(head.next);
865 }
David Horstmanndb73d3b2022-10-04 16:49:16 +0100866
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 TEST_EQUAL(ret, exp_ret);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 mbedtls_free(name);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100870}
871/* END_CASE */
872
Werner Lewisb3acb052022-06-17 15:59:58 +0100873/* 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 +0100874void mbedtls_x509_dn_get_next(char *name_str,
875 int next_merged,
876 char *expected_oids,
877 int exp_count,
878 char *exp_dn_gets)
Werner Lewisb3acb052022-06-17 15:59:58 +0100879{
880 int ret = 0, i;
Werner Lewisac80a662022-06-23 11:58:02 +0100881 size_t len = 0, out_size;
Werner Lewisb3acb052022-06-17 15:59:58 +0100882 mbedtls_asn1_named_data *names = NULL;
Glenn Straussa4b40412022-06-26 19:32:09 -0400883 mbedtls_x509_name parsed, *parsed_cur;
Werner Lewisac80a662022-06-23 11:58:02 +0100884 // Size of buf is maximum required for test cases
885 unsigned char buf[80], *out = NULL, *c;
Werner Lewisb3acb052022-06-17 15:59:58 +0100886 const char *short_name;
887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 memset(&parsed, 0, sizeof(parsed));
889 memset(buf, 0, sizeof(buf));
890 c = buf + sizeof(buf);
Werner Lewisac80a662022-06-23 11:58:02 +0100891 // Additional size required for trailing space
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 out_size = strlen(expected_oids) + 2;
893 ASSERT_ALLOC(out, out_size);
Werner Lewisb3acb052022-06-17 15:59:58 +0100894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 TEST_EQUAL(mbedtls_x509_string_to_names(&names, name_str), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 ret = mbedtls_x509_write_names(&c, buf, names);
898 TEST_LE_S(0, ret);
Werner Lewisb3acb052022-06-17 15:59:58 +0100899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
901 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
902 TEST_EQUAL(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100903
904 // Iterate over names and set next_merged nodes
905 parsed_cur = &parsed;
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 for (; next_merged != 0 && parsed_cur != NULL; next_merged = next_merged >> 1) {
Werner Lewis12657cd2022-06-20 11:47:57 +0100907 parsed_cur->next_merged = next_merged & 0x01;
Werner Lewisb3acb052022-06-17 15:59:58 +0100908 parsed_cur = parsed_cur->next;
909 }
910
911 // Iterate over RDN nodes and print OID of first element to buffer
912 parsed_cur = &parsed;
913 len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 for (i = 0; parsed_cur != NULL; i++) {
915 TEST_EQUAL(mbedtls_oid_get_attr_short_name(&parsed_cur->oid,
916 &short_name), 0);
917 len += mbedtls_snprintf((char *) out + len, out_size - len, "%s ", short_name);
918 parsed_cur = mbedtls_x509_dn_get_next(parsed_cur);
Werner Lewisb3acb052022-06-17 15:59:58 +0100919 }
920 out[len-1] = 0;
921
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 TEST_EQUAL(exp_count, i);
923 TEST_EQUAL(strcmp((char *) out, expected_oids), 0);
924 mbedtls_free(out);
Werner Lewisac80a662022-06-23 11:58:02 +0100925 out = NULL;
Werner Lewisb3acb052022-06-17 15:59:58 +0100926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 out_size = strlen(exp_dn_gets) + 1;
928 ASSERT_ALLOC(out, out_size);
Werner Lewisac80a662022-06-23 11:58:02 +0100929
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 TEST_LE_S(0, mbedtls_x509_dn_gets((char *) out, out_size, &parsed));
931 TEST_EQUAL(strcmp((char *) out, exp_dn_gets), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100932exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 mbedtls_free(out);
934 mbedtls_asn1_free_named_data_list(&names);
935 mbedtls_asn1_free_named_data_list_shallow(parsed.next);
Werner Lewisb3acb052022-06-17 15:59:58 +0100936}
Werner Lewisb3acb052022-06-17 15:59:58 +0100937/* END_CASE */
938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100940void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000941{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 mbedtls_x509_crt_init(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (strcmp(entity, "valid_from") == 0) {
949 TEST_ASSERT(mbedtls_x509_time_is_past(&crt.valid_from) == result);
950 } else if (strcmp(entity, "valid_to") == 0) {
951 TEST_ASSERT(mbedtls_x509_time_is_past(&crt.valid_to) == result);
952 } else {
953 TEST_ASSERT("Unknown entity" == 0);
954 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000955
Paul Bakkerbd51b262014-07-10 15:26:12 +0200956exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000958}
Paul Bakker33b43f12013-08-20 11:48:36 +0200959/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100962void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100963{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if (strcmp(entity, "valid_from") == 0) {
971 TEST_ASSERT(mbedtls_x509_time_is_future(&crt.valid_from) == result);
972 } else if (strcmp(entity, "valid_to") == 0) {
973 TEST_ASSERT(mbedtls_x509_time_is_future(&crt.valid_to) == result);
974 } else {
975 TEST_ASSERT("Unknown entity" == 0);
976 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100977
Paul Bakkerbd51b262014-07-10 15:26:12 +0200978exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100980}
981/* END_CASE */
982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100984void x509parse_crt_file(char *crt_file, int result)
Paul Bakker5a5fa922014-09-26 14:53:04 +0200985{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +0200987
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 mbedtls_x509_crt_init(&crt);
Paul Bakker5a5fa922014-09-26 14:53:04 +0200989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == result);
Paul Bakker5a5fa922014-09-26 14:53:04 +0200991
992exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 mbedtls_x509_crt_free(&crt);
Paul Bakker5a5fa922014-09-26 14:53:04 +0200994}
995/* END_CASE */
996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100998void x509parse_crt(data_t *buf, char *result_str, int result)
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000999{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 mbedtls_x509_crt crt;
Hanno Becker612a2f12020-10-09 09:19:39 +01001001#if !defined(MBEDTLS_X509_REMOVE_INFO)
Hanno Beckerfff2d572020-10-09 09:45:19 +01001002 unsigned char output[2000] = { 0 };
Azim Khanf1aaec92017-05-30 14:23:15 +01001003 int res;
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001004#else
1005 ((void) result_str);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001006#endif
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 mbedtls_x509_crt_init(&crt);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001009
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 TEST_ASSERT(mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len) == (result));
Hanno Becker612a2f12020-10-09 09:19:39 +01001011#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 if ((result) == 0) {
1013 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1014 TEST_ASSERT(res != -1);
1015 TEST_ASSERT(res != -2);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001016
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001018 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001020#endif
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 mbedtls_x509_crt_free(&crt);
1023 mbedtls_x509_crt_init(&crt);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 TEST_ASSERT(mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len) == (result));
Hanno Becker612a2f12020-10-09 09:19:39 +01001026#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if ((result) == 0) {
1028 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakker33b43f12013-08-20 11:48:36 +02001031
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 TEST_ASSERT(res != -1);
1033 TEST_ASSERT(res != -2);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001034
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001036 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001038#endif /* !MBEDTLS_X509_REMOVE_INFO */
Paul Bakkerb08e6842012-02-11 18:43:20 +00001039
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 mbedtls_x509_crt_free(&crt);
1041 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001042
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL,
1044 NULL) == (result));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001045#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 if ((result) == 0) {
1047 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 TEST_ASSERT(res != -1);
1050 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001051
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001053 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001055#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 mbedtls_x509_crt_free(&crt);
1058 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL,
1061 NULL) == (result));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001062#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 if ((result) == 0) {
1064 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 TEST_ASSERT(res != -1);
1067 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001070 }
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001071#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001072
1073exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 mbedtls_x509_crt_free(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001075}
1076/* END_CASE */
1077
1078/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001079void x509parse_crt_cb(data_t *buf, char *result_str, int result)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001080{
1081 mbedtls_x509_crt crt;
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001082 mbedtls_x509_buf oid;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001083
1084#if !defined(MBEDTLS_X509_REMOVE_INFO)
1085 unsigned char output[2000] = { 0 };
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001086 int res;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001087#else
1088 ((void) result_str);
1089#endif
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001090
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001091 oid.tag = MBEDTLS_ASN1_OID;
1092 oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
1098 &oid) == (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001099#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 if ((result) == 0) {
1101 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 TEST_ASSERT(res != -1);
1104 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001107 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 memset(output, 0, 2000);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001109#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 mbedtls_x509_crt_free(&crt);
1112 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1115 &oid) == (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001116#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 if ((result) == 0) {
1118 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 TEST_ASSERT(res != -1);
1121 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001124 }
Hanno Beckerfff2d572020-10-09 09:45:19 +01001125#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001126
Paul Bakkerbd51b262014-07-10 15:26:12 +02001127exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 mbedtls_x509_crt_free(&crt);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001129}
Paul Bakker33b43f12013-08-20 11:48:36 +02001130/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001131
Hanno Becker612a2f12020-10-09 09:19:39 +01001132/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001133void x509parse_crl(data_t *buf, char *result_str, int result)
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001134{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001136 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +01001137 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 mbedtls_x509_crl_init(&crl);
1140 memset(output, 0, 2000);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001141
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 TEST_ASSERT(mbedtls_x509_crl_parse(&crl, buf->x, buf->len) == (result));
1144 if ((result) == 0) {
1145 res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
Paul Bakker33b43f12013-08-20 11:48:36 +02001146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 TEST_ASSERT(res != -1);
1148 TEST_ASSERT(res != -2);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001151 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001152
Paul Bakkerbd51b262014-07-10 15:26:12 +02001153exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 mbedtls_x509_crl_free(&crl);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001155}
Paul Bakker33b43f12013-08-20 11:48:36 +02001156/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001157
Hanno Becker612a2f12020-10-09 09:19:39 +01001158/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001159void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001160{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001162 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001163 int my_ret;
1164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 mbedtls_x509_csr_init(&csr);
1166 memset(my_out, 0, sizeof(my_out));
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
1169 TEST_ASSERT(my_ret == ref_ret);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 if (ref_ret == 0) {
1172 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1173 TEST_ASSERT(my_out_len == strlen(ref_out));
1174 TEST_ASSERT(strcmp(my_out, ref_out) == 0);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001175 }
1176
Paul Bakkerbd51b262014-07-10 15:26:12 +02001177exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 mbedtls_x509_csr_free(&csr);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001179}
1180/* END_CASE */
1181
Przemek Stekield7992df2023-01-25 16:19:50 +01001182/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1183void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
1184{
1185 mbedtls_x509_csr csr;
1186 char my_out[1000];
1187 int my_ret;
1188
1189 mbedtls_x509_csr_init(&csr);
1190 memset(my_out, 0, sizeof(my_out));
1191
1192 my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file);
1193 TEST_ASSERT(my_ret == ref_ret);
1194
1195 if (ref_ret == 0) {
1196 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1197 TEST_ASSERT(my_out_len == strlen(ref_out));
1198 TEST_ASSERT(strcmp(my_out, ref_out) == 0);
1199 }
1200
1201exit:
1202 mbedtls_x509_csr_free(&csr);
1203}
1204/* END_CASE */
1205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001207void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001208{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001210 int i;
1211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 mbedtls_x509_crt_init(&chain);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001213
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 TEST_ASSERT(mbedtls_x509_crt_parse_path(&chain, crt_path) == ret);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001215
1216 /* Check how many certs we got */
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1218 if (cur->raw.p != NULL) {
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001219 i++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 }
1221 }
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 TEST_ASSERT(i == nb_crt);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001224
Paul Bakkerbd51b262014-07-10 15:26:12 +02001225exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001227}
1228/* END_CASE */
1229
Janos Follath822b2c32015-10-11 10:25:22 +02001230/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001231void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1232 int ret_chk, int flags_chk)
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001233{
1234 char file_buf[128];
1235 int ret;
1236 uint32_t flags;
1237 mbedtls_x509_crt trusted, chain;
1238
1239 /*
1240 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1241 * with NN.crt signed by NN-1.crt
1242 */
1243
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 mbedtls_x509_crt_init(&trusted);
1245 mbedtls_x509_crt_init(&chain);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001246
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001247 MD_OR_USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +01001248
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001249 /* Load trusted root */
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, ca_file) == 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001251
1252 /* Load a chain with nb_int intermediates (from 01 to nb_int),
1253 * plus one "end-entity" cert (nb_int + 1) */
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001254 ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 nb_int + 1);
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001256 TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, file_buf) == 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001258
1259 /* Try to verify that chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1261 NULL, NULL);
1262 TEST_ASSERT(ret == ret_chk);
1263 TEST_ASSERT(flags == (uint32_t) flags_chk);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001264
1265exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 mbedtls_x509_crt_free(&chain);
1267 mbedtls_x509_crt_free(&trusted);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001268 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001269}
1270/* END_CASE */
1271
1272/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001273void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1274 int flags_result, int result,
1275 char *profile_name, int vrfy_fatal_lvls)
Janos Follath822b2c32015-10-11 10:25:22 +02001276{
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 char *act;
Janos Follath822b2c32015-10-11 10:25:22 +02001278 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001279 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001280 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001281 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +02001282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 mbedtls_x509_crt_init(&chain);
1284 mbedtls_x509_crt_init(&trusted);
Janos Follath822b2c32015-10-11 10:25:22 +02001285
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001286 MD_OR_USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +01001287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 while ((act = mystrsep(&chain_paths, " ")) != NULL) {
1289 TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, act) == 0);
1290 }
1291 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, trusted_ca) == 0);
Janos Follath822b2c32015-10-11 10:25:22 +02001292
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 if (strcmp(profile_name, "") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001294 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 } else if (strcmp(profile_name, "next") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001296 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 } else if (strcmp(profile_name, "suiteb") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001298 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 } else if (strcmp(profile_name, "rsa3072") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001300 profile = &profile_rsa3072;
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 } else if (strcmp(profile_name, "sha512") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001302 profile = &profile_sha512;
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 }
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1306 NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
Janos Follathef4f2582015-10-11 16:17:27 +02001307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 TEST_ASSERT(res == (result));
1309 TEST_ASSERT(flags == (uint32_t) (flags_result));
Janos Follath822b2c32015-10-11 10:25:22 +02001310
1311exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 mbedtls_x509_crt_free(&trusted);
1313 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001314 MD_OR_USE_PSA_DONE();
Janos Follath822b2c32015-10-11 10:25:22 +02001315}
1316/* END_CASE */
1317
Hanno Becker612a2f12020-10-09 09:19:39 +01001318/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001319void x509_oid_desc(data_t *buf, char *ref_desc)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001320{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001322 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001323 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001324
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001327 oid.p = buf->x;
1328 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 ret = mbedtls_oid_get_extended_key_usage(&oid, &desc);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 if (strcmp(ref_desc, "notfound") == 0) {
1333 TEST_ASSERT(ret != 0);
1334 TEST_ASSERT(desc == NULL);
1335 } else {
1336 TEST_ASSERT(ret == 0);
1337 TEST_ASSERT(desc != NULL);
1338 TEST_ASSERT(strcmp(desc, ref_desc) == 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001339 }
1340}
1341/* END_CASE */
1342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001344void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001345{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001347 char num_buf[100];
1348
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001349 memset(num_buf, 0x2a, sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001352 oid.p = oid_buf->x;
1353 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001354
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001355 TEST_ASSERT((size_t) blen <= sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 TEST_ASSERT(mbedtls_oid_get_numeric_string(num_buf, blen, &oid) == ret);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 if (ret >= 0) {
1360 TEST_ASSERT(num_buf[ret] == 0);
1361 TEST_ASSERT(strcmp(num_buf, numstr) == 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001362 }
1363}
1364/* END_CASE */
1365
TRodziewicz442fdc22021-06-07 13:52:23 +02001366/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001367void x509_check_key_usage(char *crt_file, int usage, int ret)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001368{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001372
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 TEST_ASSERT(mbedtls_x509_crt_check_key_usage(&crt, usage) == ret);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001376
Paul Bakkerbd51b262014-07-10 15:26:12 +02001377exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001379}
1380/* END_CASE */
1381
TRodziewicz442fdc22021-06-07 13:52:23 +02001382/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001383void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1384 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001385{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001389
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001392
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 TEST_ASSERT(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x,
1394 oid->len) == ret);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001395
Paul Bakkerbd51b262014-07-10 15:26:12 +02001396exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001398}
1399/* END_CASE */
1400
Andres AG4b76aec2016-09-23 13:16:02 +01001401/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001402void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1403 int day, int hour, int min, int sec)
Andres AG4b76aec2016-09-23 13:16:02 +01001404{
1405 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001406 unsigned char buf[21];
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 unsigned char *start = buf;
1408 unsigned char *end = buf;
Andres AG4b76aec2016-09-23 13:16:02 +01001409
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 memset(&time, 0x00, sizeof(time));
1411 *end = (unsigned char) tag; end++;
1412 *end = strlen(time_str);
1413 TEST_ASSERT(*end < 20);
Andres AG4b76aec2016-09-23 13:16:02 +01001414 end++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 memcpy(end, time_str, (size_t) *(end - 1));
Andres AG4b76aec2016-09-23 13:16:02 +01001416 end += *(end - 1);
1417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 TEST_ASSERT(mbedtls_x509_get_time(&start, end, &time) == ret);
1419 if (ret == 0) {
1420 TEST_ASSERT(year == time.year);
1421 TEST_ASSERT(mon == time.mon);
1422 TEST_ASSERT(day == time.day);
1423 TEST_ASSERT(hour == time.hour);
1424 TEST_ASSERT(min == time.min);
1425 TEST_ASSERT(sec == time.sec);
Andres AG4b76aec2016-09-23 13:16:02 +01001426 }
1427}
1428/* END_CASE */
1429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001431void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1432 int ref_msg_md, int ref_mgf_md,
1433 int ref_salt_len, int ref_ret)
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001434{
1435 int my_ret;
Ronald Cronac6ae352020-06-26 14:33:03 +02001436 mbedtls_x509_buf buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001438 int my_salt_len;
1439
Ronald Cronac6ae352020-06-26 14:33:03 +02001440 buf.p = params->x;
1441 buf.len = params->len;
1442 buf.tag = params_tag;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1445 &my_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001446
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 TEST_ASSERT(my_ret == ref_ret);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 if (ref_ret == 0) {
1450 TEST_ASSERT(my_msg_md == (mbedtls_md_type_t) ref_msg_md);
1451 TEST_ASSERT(my_mgf_md == (mbedtls_md_type_t) ref_mgf_md);
1452 TEST_ASSERT(my_salt_len == ref_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001453 }
1454
Paul Bakkerbd51b262014-07-10 15:26:12 +02001455exit:
Azim Khand30ca132017-06-09 04:32:58 +01001456 ;;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001457}
1458/* END_CASE */