blob: e6bce1d4fb7bfd137725b4b1fa8b2c95a61f5af1 [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,
David Horstmanncfae6a12023-08-18 19:12:59 +0100245 &san->san.other_name.type_id) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 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 */
Andrzej Kureke12b01d2023-01-10 06:47:38 -0500292 case (MBEDTLS_X509_SAN_DIRECTORY_NAME):
293 ret = mbedtls_snprintf(p, n, "\ndirectoryName : ");
294 MBEDTLS_X509_SAFE_SNPRINTF;
295 ret = mbedtls_x509_dn_gets(p, n, &san->san.directory_name);
296 if (ret < 0) {
297 return ret;
298 }
299
300 p += ret;
301 n -= ret;
302 break;/* MBEDTLS_X509_SAN_DIRECTORY_NAME */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200303 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 /*
305 * Should not happen.
306 */
307 return -1;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200308 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 ret = mbedtls_snprintf(p, n, "\n");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200310 MBEDTLS_X509_SAFE_SNPRINTF;
311
312 *size = n;
313 *buf = p;
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200316}
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318int parse_crt_ext_cb(void *p_ctx, mbedtls_x509_crt const *crt, mbedtls_x509_buf const *oid,
319 int critical, const unsigned char *cp, const unsigned char *end)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200320{
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 (void) crt;
322 (void) critical;
323 mbedtls_x509_buf *new_oid = (mbedtls_x509_buf *) p_ctx;
324 if (oid->tag == MBEDTLS_ASN1_OID &&
325 MBEDTLS_OID_CMP(MBEDTLS_OID_CERTIFICATE_POLICIES, oid) == 0) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200326 /* Handle unknown certificate policy */
327 int ret, parse_ret = 0;
328 size_t len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 unsigned char **p = (unsigned char **) &cp;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200330
331 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 ret = mbedtls_asn1_get_tag(p, end, &len,
333 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
334 if (ret != 0) {
335 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
336 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (*p + len != end) {
339 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
340 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
341 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200342
343 /*
344 * Cannot be an empty sequence.
345 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (len == 0) {
347 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
348 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
349 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 while (*p < end) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200352 const unsigned char *policy_end;
353
354 /*
355 * Get the policy sequence
356 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
358 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
359 0) {
360 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
361 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200362
363 policy_end = *p + len;
364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
366 MBEDTLS_ASN1_OID)) != 0) {
367 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
368 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200369
Nicola Di Lietob77fad82020-06-17 17:57:36 +0200370 /*
371 * Recognize exclusively the policy with OID 1
372 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if (len != 1 || *p[0] != 1) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200374 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200376
377 *p += len;
378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 /*
380 * If there is an optional qualifier, then *p < policy_end
381 * Check the Qualifier len to verify it doesn't exceed policy_end.
382 */
383 if (*p < policy_end) {
384 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
385 MBEDTLS_ASN1_CONSTRUCTED |
386 MBEDTLS_ASN1_SEQUENCE)) != 0) {
387 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
388 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200389 /*
390 * Skip the optional policy qualifiers.
391 */
392 *p += len;
393 }
394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if (*p != policy_end) {
396 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
397 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
398 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200399 }
400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (*p != end) {
402 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
403 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
404 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 return parse_ret;
407 } else if (new_oid != NULL && new_oid->tag == oid->tag && new_oid->len == oid->len &&
408 memcmp(new_oid->p, oid->p, oid->len) == 0) {
409 return 0;
410 } else {
411 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
412 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200413 }
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200414}
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200415#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200416/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000417
Thomas Daubney5c9c2ce2022-06-06 16:36:43 +0100418/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100419void x509_accessor_ext_types(int ext_type, int has_ext_type)
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100420{
421 mbedtls_x509_crt crt;
422 int expected_result = ext_type & has_ext_type;
423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200425 USE_PSA_INIT();
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100426
427 crt.ext_types = ext_type;
428
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500429 TEST_EQUAL(mbedtls_x509_crt_has_ext_type(&crt, has_ext_type), expected_result);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100430
valerio32f2ac92023-04-20 11:59:52 +0200431exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200433 USE_PSA_DONE();
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100434}
435/* END_CASE */
436
Glenn Strauss6f545ac2022-10-25 15:02:14 -0400437/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_TEST_HOOKS */
438void x509_crt_parse_cn_inet_pton(const char *cn, data_t *exp, int ref_ret)
439{
440 uint32_t addr[4];
441 size_t addrlen = mbedtls_x509_crt_parse_cn_inet_pton(cn, addr);
442 TEST_EQUAL(addrlen, (size_t) ref_ret);
443
444 if (addrlen) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100445 TEST_MEMORY_COMPARE(exp->x, exp->len, addr, addrlen);
Glenn Strauss6f545ac2022-10-25 15:02:14 -0400446 }
447}
448/* END_CASE */
449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500451void x509_parse_san(char *crt_file, char *result_str, int parse_result)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200452{
Ron Eldor890819a2019-05-13 19:03:04 +0300453 int ret;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200454 mbedtls_x509_crt crt;
Ron Eldor890819a2019-05-13 19:03:04 +0300455 mbedtls_x509_subject_alternative_name san;
456 mbedtls_x509_sequence *cur = NULL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200457 char buf[2000];
458 char *p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 size_t n = sizeof(buf);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200462 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 memset(buf, 0, 2000);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200464
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500465 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), parse_result);
Ron Eldor890819a2019-05-13 19:03:04 +0300466
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500467 if (parse_result != 0) {
468 goto exit;
469 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Ron Eldor890819a2019-05-13 19:03:04 +0300471 cur = &crt.subject_alt_names;
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 while (cur != NULL) {
473 ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
474 TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
Ron Eldor890819a2019-05-13 19:03:04 +0300475 /*
476 * If san type not supported, ignore.
477 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 if (ret == 0) {
Andrzej Kurekd40c2b62023-02-13 07:01:59 -0500479 ret = verify_parse_san(&san, &p, &n);
480 mbedtls_x509_free_subject_alt_name(&san);
481 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 }
Ron Eldor890819a2019-05-13 19:03:04 +0300483 cur = cur->next;
484 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200485 }
486
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500487 TEST_EQUAL(strcmp(buf, result_str), 0);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200488
489exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200491 USE_PSA_DONE();
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200492}
493/* END_CASE */
494
Hanno Becker612a2f12020-10-09 09:19:39 +0100495/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100496void x509_cert_info(char *crt_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000497{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000499 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000500 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200503 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000505
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500506 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 res = mbedtls_x509_crt_info(buf, 2000, "", &crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 TEST_ASSERT(res != -1);
510 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000511
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500512 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200513
514exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200516 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000517}
Paul Bakker33b43f12013-08-20 11:48:36 +0200518/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000519
Hanno Becker612a2f12020-10-09 09:19:39 +0100520/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100521void mbedtls_x509_crl_info(char *crl_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000522{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000524 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000525 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200528 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000530
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500531 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 res = mbedtls_x509_crl_info(buf, 2000, "", &crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 TEST_ASSERT(res != -1);
535 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000536
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500537 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200538
539exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +0200541 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000542}
Paul Bakker33b43f12013-08-20 11:48:36 +0200543/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000544
Andres AGa39db392016-12-08 17:10:38 +0000545/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100546void mbedtls_x509_crl_parse(char *crl_file, int result)
Andres AGa39db392016-12-08 17:10:38 +0000547{
548 mbedtls_x509_crl crl;
549 char buf[2000];
550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200552 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 memset(buf, 0, 2000);
Andres AGa39db392016-12-08 17:10:38 +0000554
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500555 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), result);
Andres AGa39db392016-12-08 17:10:38 +0000556
557exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +0200559 USE_PSA_DONE();
Andres AGa39db392016-12-08 17:10:38 +0000560}
561/* END_CASE */
562
Hanno Becker612a2f12020-10-09 09:19:39 +0100563/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100564void mbedtls_x509_csr_info(char *csr_file, char *result_str)
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100565{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100567 char buf[2000];
568 int res;
569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +0200571 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 memset(buf, 0, 2000);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100573
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500574 TEST_EQUAL(mbedtls_x509_csr_parse_file(&csr, csr_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 res = mbedtls_x509_csr_info(buf, 2000, "", &csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 TEST_ASSERT(res != -1);
578 TEST_ASSERT(res != -2);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100579
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500580 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200581
582exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +0200584 USE_PSA_DONE();
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100585}
586/* END_CASE */
587
Hanno Becker612a2f12020-10-09 09:19:39 +0100588/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100589void x509_verify_info(int flags, char *prefix, char *result_str)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100590{
591 char buf[2000];
592 int res;
593
Valerio Setti569c1712023-04-19 14:53:36 +0200594 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 memset(buf, 0, sizeof(buf));
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 res = mbedtls_x509_crt_verify_info(buf, sizeof(buf), prefix, flags);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 TEST_ASSERT(res >= 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100600
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500601 TEST_EQUAL(strcmp(buf, result_str), 0);
valerio32f2ac92023-04-20 11:59:52 +0200602
603exit:
Valerio Setti569c1712023-04-19 14:53:36 +0200604 USE_PSA_DONE();
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100605}
606/* END_CASE */
607
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200608/* 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 +0100609void x509_verify_restart(char *crt_file, char *ca_file,
610 int result, int flags_result,
611 int max_ops, int min_restart, int max_restart)
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200612{
613 int ret, cnt_restart;
614 mbedtls_x509_crt_restart_ctx rs_ctx;
615 mbedtls_x509_crt crt;
616 mbedtls_x509_crt ca;
617 uint32_t flags = 0;
618
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200619 /*
620 * See comments on ecp_test_vect_restart() for op count precision.
621 *
622 * For reference, with mbed TLS 2.6 and default settings:
623 * - ecdsa_verify() for P-256: ~ 6700
624 * - ecdsa_verify() for P-384: ~ 18800
625 * - x509_verify() for server5 -> test-ca2: ~ 18800
626 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
627 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 mbedtls_x509_crt_restart_init(&rs_ctx);
629 mbedtls_x509_crt_init(&crt);
630 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200631 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200632
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500633 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
634 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 mbedtls_ecp_set_max_ops(max_ops);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200637
638 cnt_restart = 0;
639 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
641 &mbedtls_x509_crt_profile_default, NULL, &flags,
642 NULL, NULL, &rs_ctx);
643 } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200644
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500645 TEST_EQUAL(ret, result);
646 TEST_EQUAL(flags, (uint32_t) flags_result);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 TEST_ASSERT(cnt_restart >= min_restart);
649 TEST_ASSERT(cnt_restart <= max_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200650
651 /* Do we leak memory when aborting? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
653 &mbedtls_x509_crt_profile_default, NULL, &flags,
654 NULL, NULL, &rs_ctx);
655 TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200656
657exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 mbedtls_x509_crt_restart_free(&rs_ctx);
659 mbedtls_x509_crt_free(&crt);
660 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100661 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200662}
663/* END_CASE */
664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100666void x509_verify(char *crt_file, char *ca_file, char *crl_file,
667 char *cn_name_str, int result, int flags_result,
668 char *profile_str,
669 char *verify_callback)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000670{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_x509_crt crt;
672 mbedtls_x509_crt ca;
673 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200674 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000675 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200676 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 char *cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200678 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 mbedtls_x509_crt_init(&crt);
681 mbedtls_x509_crt_init(&ca);
682 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200683 MD_OR_USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (strcmp(cn_name_str, "NULL") != 0) {
Paul Bakker33b43f12013-08-20 11:48:36 +0200686 cn_name = cn_name_str;
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if (strcmp(profile_str, "") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200690 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 } else if (strcmp(profile_str, "next") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200692 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 } else if (strcmp(profile_str, "suite_b") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200694 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 } else if (strcmp(profile_str, "compat") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200696 profile = &compat_profile;
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 } else if (strcmp(profile_str, "all") == 0) {
Hanno Becker20a4ade2019-06-03 14:27:03 +0100698 profile = &profile_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100700 TEST_FAIL("Unknown algorithm profile");
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 }
Gilles Peskineef86ab22017-05-05 18:59:02 +0200702
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if (strcmp(verify_callback, "NULL") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200704 f_vrfy = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 } else if (strcmp(verify_callback, "verify_none") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200706 f_vrfy = verify_none;
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 } else if (strcmp(verify_callback, "verify_all") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200708 f_vrfy = verify_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100710 TEST_FAIL("No known verify callback selected");
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200712
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500713 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
714 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
715 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 res = mbedtls_x509_crt_verify_with_profile(&crt,
718 &ca,
719 &crl,
720 profile,
721 cn_name,
722 &flags,
723 f_vrfy,
724 NULL);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 TEST_EQUAL(res, result);
727 TEST_EQUAL(flags, (uint32_t) flags_result);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200728
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200729#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker0350d562019-03-28 14:23:36 +0000730 /* CRLs aren't supported with CA callbacks, so skip the CA callback
Jarno Lamsadfd22c42019-04-01 15:18:53 +0300731 * version of the test if CRLs are in use. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (crl_file == NULL || strcmp(crl_file, "") == 0) {
Hanno Becker0350d562019-03-28 14:23:36 +0000733 flags = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 res = mbedtls_x509_crt_verify_with_ca_cb(&crt,
736 ca_callback,
737 &ca,
738 profile,
739 cn_name,
740 &flags,
741 f_vrfy,
742 NULL);
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200743
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500744 TEST_EQUAL(res, result);
745 TEST_EQUAL(flags, (uint32_t) (flags_result));
Hanno Becker0350d562019-03-28 14:23:36 +0000746 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200747#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200748exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 mbedtls_x509_crt_free(&crt);
750 mbedtls_x509_crt_free(&ca);
751 mbedtls_x509_crl_free(&crl);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100752 MD_OR_USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000753}
Paul Bakker33b43f12013-08-20 11:48:36 +0200754/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000755
Jarno Lamsa557426a2019-03-27 17:08:29 +0200756/* 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 +0100757void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
758 int exp_ret)
Jarno Lamsa557426a2019-03-27 17:08:29 +0200759{
760 int ret;
761 mbedtls_x509_crt crt;
762 mbedtls_x509_crt ca;
763 uint32_t flags = 0;
Hanno Becker3f932bb2019-03-28 17:06:47 +0000764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 mbedtls_x509_crt_init(&crt);
766 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200767 USE_PSA_INIT();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200768
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500769 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
770 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200771
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 if (strcmp(name, "NULL") == 0) {
Jarno Lamsa557426a2019-03-27 17:08:29 +0200773 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca,
777 &compat_profile, name, &flags,
778 NULL, NULL);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200779
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500780 TEST_EQUAL(ret, exp_ret);
781 TEST_EQUAL(flags, (uint32_t) (-1));
Jarno Lamsa557426a2019-03-27 17:08:29 +0200782exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 mbedtls_x509_crt_free(&crt);
784 mbedtls_x509_crt_free(&ca);
Valerio Setti569c1712023-04-19 14:53:36 +0200785 USE_PSA_DONE();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200786}
787/* END_CASE */
788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100790void x509_verify_callback(char *crt_file, char *ca_file, char *name,
791 int exp_ret, char *exp_vrfy_out)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200792{
793 int ret;
794 mbedtls_x509_crt crt;
795 mbedtls_x509_crt ca;
796 uint32_t flags = 0;
797 verify_print_context vrfy_ctx;
798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 mbedtls_x509_crt_init(&crt);
800 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200801 MD_OR_USE_PSA_INIT();
802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 verify_print_init(&vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200804
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500805 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
806 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if (strcmp(name, "NULL") == 0) {
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200809 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 }
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL,
813 &compat_profile,
814 name, &flags,
815 verify_print, &vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200816
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500817 TEST_EQUAL(ret, exp_ret);
818 TEST_EQUAL(strcmp(vrfy_ctx.buf, exp_vrfy_out), 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200819
820exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 mbedtls_x509_crt_free(&crt);
822 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100823 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200824}
825/* END_CASE */
826
Hanno Becker612a2f12020-10-09 09:19:39 +0100827/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100828void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
829 char *new_subject_ou,
830 char *result_str,
831 int ret)
Werner Lewis31ecb962022-06-17 15:51:55 +0100832{
833 mbedtls_x509_crt crt;
834 char buf[2000];
835 int res = 0;
836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200838 USE_PSA_INIT();
839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 memset(buf, 0, 2000);
Werner Lewis31ecb962022-06-17 15:51:55 +0100841
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500842 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100843 crt.subject.next->val.p = (unsigned char *) new_subject_ou;
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 crt.subject.next->val.len = strlen(new_subject_ou);
Werner Lewis31ecb962022-06-17 15:51:55 +0100845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
Werner Lewis31ecb962022-06-17 15:51:55 +0100847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 if (ret != 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500849 TEST_EQUAL(res, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 } else {
851 TEST_ASSERT(res != -1);
852 TEST_ASSERT(res != -2);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500853 TEST_EQUAL(strcmp(buf, result_str), 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100854 }
855exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200857 USE_PSA_DONE();
Werner Lewis31ecb962022-06-17 15:51:55 +0100858}
859/* END_CASE */
860
861/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000863{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000865 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200866 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200869 USE_PSA_INIT();
870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000872
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500873 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 if (strcmp(entity, "subject") == 0) {
875 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
876 } else if (strcmp(entity, "issuer") == 0) {
877 res = mbedtls_x509_dn_gets(buf, 2000, &crt.issuer);
878 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100879 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 TEST_ASSERT(res != -1);
883 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000884
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500885 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200886
887exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200889 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000890}
Paul Bakker33b43f12013-08-20 11:48:36 +0200891/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000892
David Horstmanndb73d3b2022-10-04 16:49:16 +0100893/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100894void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
David Horstmanndb73d3b2022-10-04 16:49:16 +0100895{
valerioe50831c2023-04-20 14:48:19 +0200896 unsigned char *name = NULL;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100897 unsigned char *p;
898 size_t name_len;
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100899 mbedtls_x509_name head;
David Horstmann3cd67582022-10-17 17:59:10 +0100900 int ret;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100901
Valerio Setti569c1712023-04-19 14:53:36 +0200902 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 memset(&head, 0, sizeof(head));
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100906 p = name;
907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 ret = mbedtls_x509_get_name(&p, (name + name_len), &head);
909 if (ret == 0) {
910 mbedtls_asn1_free_named_data_list_shallow(head.next);
911 }
David Horstmanndb73d3b2022-10-04 16:49:16 +0100912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 TEST_EQUAL(ret, exp_ret);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100914
valerio32f2ac92023-04-20 11:59:52 +0200915exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 mbedtls_free(name);
Valerio Setti569c1712023-04-19 14:53:36 +0200917 USE_PSA_DONE();
David Horstmanndb73d3b2022-10-04 16:49:16 +0100918}
919/* END_CASE */
920
Werner Lewisb3acb052022-06-17 15:59:58 +0100921/* 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 +0100922void mbedtls_x509_dn_get_next(char *name_str,
923 int next_merged,
924 char *expected_oids,
925 int exp_count,
926 char *exp_dn_gets)
Werner Lewisb3acb052022-06-17 15:59:58 +0100927{
928 int ret = 0, i;
Werner Lewisac80a662022-06-23 11:58:02 +0100929 size_t len = 0, out_size;
Werner Lewisb3acb052022-06-17 15:59:58 +0100930 mbedtls_asn1_named_data *names = NULL;
Glenn Straussa4b40412022-06-26 19:32:09 -0400931 mbedtls_x509_name parsed, *parsed_cur;
Werner Lewisac80a662022-06-23 11:58:02 +0100932 // Size of buf is maximum required for test cases
933 unsigned char buf[80], *out = NULL, *c;
Werner Lewisb3acb052022-06-17 15:59:58 +0100934 const char *short_name;
935
Valerio Setti569c1712023-04-19 14:53:36 +0200936 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 memset(&parsed, 0, sizeof(parsed));
938 memset(buf, 0, sizeof(buf));
939 c = buf + sizeof(buf);
Werner Lewisac80a662022-06-23 11:58:02 +0100940 // Additional size required for trailing space
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 out_size = strlen(expected_oids) + 2;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100942 TEST_CALLOC(out, out_size);
Werner Lewisb3acb052022-06-17 15:59:58 +0100943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 TEST_EQUAL(mbedtls_x509_string_to_names(&names, name_str), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 ret = mbedtls_x509_write_names(&c, buf, names);
947 TEST_LE_S(0, ret);
Werner Lewisb3acb052022-06-17 15:59:58 +0100948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
950 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
951 TEST_EQUAL(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100952
953 // Iterate over names and set next_merged nodes
954 parsed_cur = &parsed;
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 for (; next_merged != 0 && parsed_cur != NULL; next_merged = next_merged >> 1) {
Werner Lewis12657cd2022-06-20 11:47:57 +0100956 parsed_cur->next_merged = next_merged & 0x01;
Werner Lewisb3acb052022-06-17 15:59:58 +0100957 parsed_cur = parsed_cur->next;
958 }
959
960 // Iterate over RDN nodes and print OID of first element to buffer
961 parsed_cur = &parsed;
962 len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 for (i = 0; parsed_cur != NULL; i++) {
964 TEST_EQUAL(mbedtls_oid_get_attr_short_name(&parsed_cur->oid,
965 &short_name), 0);
966 len += mbedtls_snprintf((char *) out + len, out_size - len, "%s ", short_name);
967 parsed_cur = mbedtls_x509_dn_get_next(parsed_cur);
Werner Lewisb3acb052022-06-17 15:59:58 +0100968 }
969 out[len-1] = 0;
970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 TEST_EQUAL(exp_count, i);
972 TEST_EQUAL(strcmp((char *) out, expected_oids), 0);
973 mbedtls_free(out);
Werner Lewisac80a662022-06-23 11:58:02 +0100974 out = NULL;
Werner Lewisb3acb052022-06-17 15:59:58 +0100975
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 out_size = strlen(exp_dn_gets) + 1;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100977 TEST_CALLOC(out, out_size);
Werner Lewisac80a662022-06-23 11:58:02 +0100978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 TEST_LE_S(0, mbedtls_x509_dn_gets((char *) out, out_size, &parsed));
980 TEST_EQUAL(strcmp((char *) out, exp_dn_gets), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100981exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 mbedtls_free(out);
983 mbedtls_asn1_free_named_data_list(&names);
984 mbedtls_asn1_free_named_data_list_shallow(parsed.next);
Valerio Setti569c1712023-04-19 14:53:36 +0200985 USE_PSA_DONE();
Werner Lewisb3acb052022-06-17 15:59:58 +0100986}
Werner Lewisb3acb052022-06-17 15:59:58 +0100987/* END_CASE */
988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100990void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000991{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200995 USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000996
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500997 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 if (strcmp(entity, "valid_from") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001000 TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_from), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 } else if (strcmp(entity, "valid_to") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001002 TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_to), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +01001004 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001006
Paul Bakkerbd51b262014-07-10 15:26:12 +02001007exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001009 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +00001010}
Paul Bakker33b43f12013-08-20 11:48:36 +02001011/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +00001012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001014void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001015{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001017
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001019 USE_PSA_INIT();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001020
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001021 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 if (strcmp(entity, "valid_from") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001024 TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_from), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 } else if (strcmp(entity, "valid_to") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001026 TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_to), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +01001028 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001030
Paul Bakkerbd51b262014-07-10 15:26:12 +02001031exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001033 USE_PSA_DONE();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001034}
1035/* END_CASE */
1036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001038void x509parse_crt_file(char *crt_file, int result)
Paul Bakker5a5fa922014-09-26 14:53:04 +02001039{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +02001041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001043 USE_PSA_INIT();
Paul Bakker5a5fa922014-09-26 14:53:04 +02001044
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001045 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), result);
Paul Bakker5a5fa922014-09-26 14:53:04 +02001046
1047exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001049 USE_PSA_DONE();
Paul Bakker5a5fa922014-09-26 14:53:04 +02001050}
1051/* END_CASE */
1052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001054void x509parse_crt(data_t *buf, char *result_str, int result)
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001055{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 mbedtls_x509_crt crt;
Hanno Becker612a2f12020-10-09 09:19:39 +01001057#if !defined(MBEDTLS_X509_REMOVE_INFO)
Hanno Beckerfff2d572020-10-09 09:45:19 +01001058 unsigned char output[2000] = { 0 };
Azim Khanf1aaec92017-05-30 14:23:15 +01001059 int res;
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001060#else
1061 ((void) result_str);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001062#endif
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001065 USE_PSA_INIT();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001066
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001067 TEST_EQUAL(mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len), result);
Hanno Becker612a2f12020-10-09 09:19:39 +01001068#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if ((result) == 0) {
1070 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1071 TEST_ASSERT(res != -1);
1072 TEST_ASSERT(res != -2);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001073
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001074 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001075 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001077#endif
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 mbedtls_x509_crt_free(&crt);
1080 mbedtls_x509_crt_init(&crt);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001081
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001082 TEST_EQUAL(mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len), result);
Hanno Becker612a2f12020-10-09 09:19:39 +01001083#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 if ((result) == 0) {
1085 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakker33b43f12013-08-20 11:48:36 +02001088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 TEST_ASSERT(res != -1);
1090 TEST_ASSERT(res != -2);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001091
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001092 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001093 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001095#endif /* !MBEDTLS_X509_REMOVE_INFO */
Paul Bakkerb08e6842012-02-11 18:43:20 +00001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 mbedtls_x509_crt_free(&crt);
1098 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001099
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001100 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL, NULL),
1101 result);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001102#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 if ((result) == 0) {
1104 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 TEST_ASSERT(res != -1);
1107 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001108
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001109 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001110 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001112#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 mbedtls_x509_crt_free(&crt);
1115 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001116
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001117 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL, NULL),
1118 result);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001119#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if ((result) == 0) {
1121 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 TEST_ASSERT(res != -1);
1124 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001125
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001126 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001127 }
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001128#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001129
1130exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001132 USE_PSA_DONE();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001133}
1134/* END_CASE */
1135
1136/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137void x509parse_crt_cb(data_t *buf, char *result_str, int result)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001138{
1139 mbedtls_x509_crt crt;
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001140 mbedtls_x509_buf oid;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001141
1142#if !defined(MBEDTLS_X509_REMOVE_INFO)
1143 unsigned char output[2000] = { 0 };
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001144 int res;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001145#else
1146 ((void) result_str);
1147#endif
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001148
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001149 oid.tag = MBEDTLS_ASN1_OID;
1150 oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001154 USE_PSA_INIT();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001155
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001156 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
1157 &oid), result);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001158#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 if ((result) == 0) {
1160 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 TEST_ASSERT(res != -1);
1163 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001164
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001165 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001166 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 memset(output, 0, 2000);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001168#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 mbedtls_x509_crt_free(&crt);
1171 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001172
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001173 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1174 &oid), (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001175#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 if ((result) == 0) {
1177 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 TEST_ASSERT(res != -1);
1180 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001181
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001182 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001183 }
Hanno Beckerfff2d572020-10-09 09:45:19 +01001184#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001185
Paul Bakkerbd51b262014-07-10 15:26:12 +02001186exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001188 USE_PSA_DONE();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001189}
Paul Bakker33b43f12013-08-20 11:48:36 +02001190/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001191
Hanno Becker612a2f12020-10-09 09:19:39 +01001192/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001193void x509parse_crl(data_t *buf, char *result_str, int result)
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001194{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001196 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +01001197 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +02001200 USE_PSA_INIT();
1201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 memset(output, 0, 2000);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001203
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001204
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001205 TEST_EQUAL(mbedtls_x509_crl_parse(&crl, buf->x, buf->len), (result));
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if ((result) == 0) {
1207 res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
Paul Bakker33b43f12013-08-20 11:48:36 +02001208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 TEST_ASSERT(res != -1);
1210 TEST_ASSERT(res != -2);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001211
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001212 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001213 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001214
Paul Bakkerbd51b262014-07-10 15:26:12 +02001215exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +02001217 USE_PSA_DONE();
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001218}
Paul Bakker33b43f12013-08-20 11:48:36 +02001219/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001220
Hanno Becker612a2f12020-10-09 09:19:39 +01001221/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001222void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001223{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001225 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001226 int my_ret;
1227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001229 USE_PSA_INIT();
1230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 memset(my_out, 0, sizeof(my_out));
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001232
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001234 TEST_EQUAL(my_ret, ref_ret);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 if (ref_ret == 0) {
1237 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001238 TEST_EQUAL(my_out_len, strlen(ref_out));
1239 TEST_EQUAL(strcmp(my_out, ref_out), 0);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001240 }
1241
Paul Bakkerbd51b262014-07-10 15:26:12 +02001242exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001244 USE_PSA_DONE();
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001245}
1246/* END_CASE */
1247
Przemek Stekield7992df2023-01-25 16:19:50 +01001248/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1249void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
1250{
1251 mbedtls_x509_csr csr;
1252 char my_out[1000];
1253 int my_ret;
1254
1255 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001256 USE_PSA_INIT();
1257
Przemek Stekield7992df2023-01-25 16:19:50 +01001258 memset(my_out, 0, sizeof(my_out));
1259
1260 my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001261 TEST_EQUAL(my_ret, ref_ret);
Przemek Stekield7992df2023-01-25 16:19:50 +01001262
1263 if (ref_ret == 0) {
1264 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001265 TEST_EQUAL(my_out_len, strlen(ref_out));
1266 TEST_EQUAL(strcmp(my_out, ref_out), 0);
Przemek Stekield7992df2023-01-25 16:19:50 +01001267 }
1268
1269exit:
1270 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001271 USE_PSA_DONE();
Przemek Stekield7992df2023-01-25 16:19:50 +01001272}
1273/* END_CASE */
1274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1e5fec62023-04-13 18:13:48 +02001276void mbedtls_x509_crt_parse_file(char *crt_path, int ret, int nb_crt)
1277{
1278 mbedtls_x509_crt chain, *cur;
1279 int i;
1280
1281 mbedtls_x509_crt_init(&chain);
1282 USE_PSA_INIT();
1283
1284 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, crt_path), ret);
1285
1286 /* Check how many certs we got */
1287 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1288 if (cur->raw.p != NULL) {
1289 i++;
1290 }
1291 }
1292
1293 TEST_EQUAL(i, nb_crt);
1294
1295exit:
1296 mbedtls_x509_crt_free(&chain);
1297 USE_PSA_DONE();
1298}
1299/* END_CASE */
1300
1301/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001302void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001303{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001305 int i;
1306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001308 USE_PSA_INIT();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001309
Gilles Peskine55ad28a2023-04-13 18:14:45 +02001310 TEST_EQUAL(mbedtls_x509_crt_parse_path(&chain, crt_path), ret);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001311
1312 /* Check how many certs we got */
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1314 if (cur->raw.p != NULL) {
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001315 i++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 }
1317 }
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001318
Gilles Peskine55ad28a2023-04-13 18:14:45 +02001319 TEST_EQUAL(i, nb_crt);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001320
Paul Bakkerbd51b262014-07-10 15:26:12 +02001321exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 mbedtls_x509_crt_free(&chain);
Valerio Setti569c1712023-04-19 14:53:36 +02001323 USE_PSA_DONE();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001324}
1325/* END_CASE */
1326
Janos Follath822b2c32015-10-11 10:25:22 +02001327/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001328void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1329 int ret_chk, int flags_chk)
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001330{
1331 char file_buf[128];
1332 int ret;
1333 uint32_t flags;
1334 mbedtls_x509_crt trusted, chain;
1335
1336 /*
1337 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1338 * with NN.crt signed by NN-1.crt
1339 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 mbedtls_x509_crt_init(&trusted);
1341 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001342 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001343
1344 /* Load trusted root */
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001345 TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, ca_file), 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001346
1347 /* Load a chain with nb_int intermediates (from 01 to nb_int),
1348 * plus one "end-entity" cert (nb_int + 1) */
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001349 ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 nb_int + 1);
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001351 TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001352 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, file_buf), 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001353
1354 /* Try to verify that chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1356 NULL, NULL);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001357 TEST_EQUAL(ret, ret_chk);
1358 TEST_EQUAL(flags, (uint32_t) flags_chk);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001359
1360exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 mbedtls_x509_crt_free(&chain);
1362 mbedtls_x509_crt_free(&trusted);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001363 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001364}
1365/* END_CASE */
1366
1367/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001368void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1369 int flags_result, int result,
1370 char *profile_name, int vrfy_fatal_lvls)
Janos Follath822b2c32015-10-11 10:25:22 +02001371{
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 char *act;
Janos Follath822b2c32015-10-11 10:25:22 +02001373 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001374 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001375 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001376 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +02001377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 mbedtls_x509_crt_init(&chain);
1379 mbedtls_x509_crt_init(&trusted);
valerio32f2ac92023-04-20 11:59:52 +02001380 MD_OR_USE_PSA_INIT();
Janos Follath822b2c32015-10-11 10:25:22 +02001381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 while ((act = mystrsep(&chain_paths, " ")) != NULL) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001383 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, act), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 }
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001385 TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, trusted_ca), 0);
Janos Follath822b2c32015-10-11 10:25:22 +02001386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 if (strcmp(profile_name, "") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001388 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 } else if (strcmp(profile_name, "next") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001390 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 } else if (strcmp(profile_name, "suiteb") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001392 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 } else if (strcmp(profile_name, "rsa3072") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001394 profile = &profile_rsa3072;
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 } else if (strcmp(profile_name, "sha512") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001396 profile = &profile_sha512;
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 }
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001398
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1400 NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
Janos Follathef4f2582015-10-11 16:17:27 +02001401
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001402 TEST_EQUAL(res, (result));
1403 TEST_EQUAL(flags, (uint32_t) (flags_result));
Janos Follath822b2c32015-10-11 10:25:22 +02001404
1405exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 mbedtls_x509_crt_free(&trusted);
1407 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001408 MD_OR_USE_PSA_DONE();
Janos Follath822b2c32015-10-11 10:25:22 +02001409}
1410/* END_CASE */
1411
Hanno Becker612a2f12020-10-09 09:19:39 +01001412/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001413void x509_oid_desc(data_t *buf, char *ref_desc)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001414{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001416 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001417 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001418
Valerio Setti569c1712023-04-19 14:53:36 +02001419 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001422 oid.p = buf->x;
1423 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 ret = mbedtls_oid_get_extended_key_usage(&oid, &desc);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (strcmp(ref_desc, "notfound") == 0) {
1428 TEST_ASSERT(ret != 0);
1429 TEST_ASSERT(desc == NULL);
1430 } else {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001431 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 TEST_ASSERT(desc != NULL);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001433 TEST_EQUAL(strcmp(desc, ref_desc), 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001434 }
valerio32f2ac92023-04-20 11:59:52 +02001435
1436exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001437 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001438}
1439/* END_CASE */
1440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001442void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001443{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001445 char num_buf[100];
1446
Valerio Setti569c1712023-04-19 14:53:36 +02001447 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001448
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001449 memset(num_buf, 0x2a, sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001452 oid.p = oid_buf->x;
1453 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001454
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001455 TEST_ASSERT((size_t) blen <= sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001456
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001457 TEST_EQUAL(mbedtls_oid_get_numeric_string(num_buf, blen, &oid), ret);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001458
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 if (ret >= 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001460 TEST_EQUAL(num_buf[ret], 0);
1461 TEST_EQUAL(strcmp(num_buf, numstr), 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001462 }
valerio32f2ac92023-04-20 11:59:52 +02001463
1464exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001465 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001466}
1467/* END_CASE */
1468
TRodziewicz442fdc22021-06-07 13:52:23 +02001469/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001470void x509_check_key_usage(char *crt_file, int usage, int ret)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001471{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001473
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001475 USE_PSA_INIT();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001476
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001477 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001478
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001479 TEST_EQUAL(mbedtls_x509_crt_check_key_usage(&crt, usage), ret);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001480
Paul Bakkerbd51b262014-07-10 15:26:12 +02001481exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001483 USE_PSA_DONE();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001484}
1485/* END_CASE */
1486
TRodziewicz442fdc22021-06-07 13:52:23 +02001487/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001488void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1489 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001490{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001494 USE_PSA_INIT();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001495
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001496 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001497
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001498 TEST_EQUAL(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x, oid->len),
1499 ret);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001500
Paul Bakkerbd51b262014-07-10 15:26:12 +02001501exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001503 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001504}
1505/* END_CASE */
1506
Andres AG4b76aec2016-09-23 13:16:02 +01001507/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001508void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1509 int day, int hour, int min, int sec)
Andres AG4b76aec2016-09-23 13:16:02 +01001510{
1511 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001512 unsigned char buf[21];
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 unsigned char *start = buf;
1514 unsigned char *end = buf;
Andres AG4b76aec2016-09-23 13:16:02 +01001515
Valerio Setti569c1712023-04-19 14:53:36 +02001516 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 memset(&time, 0x00, sizeof(time));
1518 *end = (unsigned char) tag; end++;
1519 *end = strlen(time_str);
1520 TEST_ASSERT(*end < 20);
Andres AG4b76aec2016-09-23 13:16:02 +01001521 end++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 memcpy(end, time_str, (size_t) *(end - 1));
Andres AG4b76aec2016-09-23 13:16:02 +01001523 end += *(end - 1);
1524
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001525 TEST_EQUAL(mbedtls_x509_get_time(&start, end, &time), ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 if (ret == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001527 TEST_EQUAL(year, time.year);
1528 TEST_EQUAL(mon, time.mon);
1529 TEST_EQUAL(day, time.day);
1530 TEST_EQUAL(hour, time.hour);
1531 TEST_EQUAL(min, time.min);
1532 TEST_EQUAL(sec, time.sec);
Andres AG4b76aec2016-09-23 13:16:02 +01001533 }
valerio32f2ac92023-04-20 11:59:52 +02001534exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001535 USE_PSA_DONE();
Andres AG4b76aec2016-09-23 13:16:02 +01001536}
1537/* END_CASE */
1538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001540void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1541 int ref_msg_md, int ref_mgf_md,
1542 int ref_salt_len, int ref_ret)
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001543{
1544 int my_ret;
Ronald Cronac6ae352020-06-26 14:33:03 +02001545 mbedtls_x509_buf buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001547 int my_salt_len;
1548
Valerio Setti569c1712023-04-19 14:53:36 +02001549 USE_PSA_INIT();
1550
Ronald Cronac6ae352020-06-26 14:33:03 +02001551 buf.p = params->x;
1552 buf.len = params->len;
1553 buf.tag = params_tag;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001554
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1556 &my_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001557
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001558 TEST_EQUAL(my_ret, ref_ret);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 if (ref_ret == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001561 TEST_EQUAL(my_msg_md, (mbedtls_md_type_t) ref_msg_md);
1562 TEST_EQUAL(my_mgf_md, (mbedtls_md_type_t) ref_mgf_md);
1563 TEST_EQUAL(my_salt_len, ref_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001564 }
1565
Paul Bakkerbd51b262014-07-10 15:26:12 +02001566exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001567 USE_PSA_DONE();
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001568}
1569/* END_CASE */
toth92ga41954d2021-02-12 16:11:17 +01001570
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001571/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001572void x509_crt_parse_subjectkeyid(char *file, data_t *subjectKeyId, int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001573{
1574 mbedtls_x509_crt crt;
toth92ga41954d2021-02-12 16:11:17 +01001575
1576 mbedtls_x509_crt_init(&crt);
1577
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001578 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001579
toth92g357b2972021-05-04 15:41:35 +02001580 if (ref_ret == 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001581 TEST_EQUAL(crt.subject_key_id.tag, MBEDTLS_ASN1_OCTET_STRING);
1582 TEST_EQUAL(memcmp(crt.subject_key_id.p, subjectKeyId->x, subjectKeyId->len), 0);
1583 TEST_EQUAL(crt.subject_key_id.len, subjectKeyId->len);
toth92g357b2972021-05-04 15:41:35 +02001584 } else {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001585 TEST_EQUAL(crt.subject_key_id.tag, 0);
1586 TEST_EQUAL(crt.subject_key_id.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001587 }
1588
1589exit:
1590 mbedtls_x509_crt_free(&crt);
1591}
1592/* END_CASE */
1593
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001594/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Przemek Stekiel2568d472023-04-06 09:23:25 +02001595void x509_crt_parse_authoritykeyid(char *file,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001596 data_t *keyId,
toth92g5042b102021-05-06 08:22:17 +02001597 char *authorityKeyId_issuer,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001598 data_t *serial,
toth92g5042b102021-05-06 08:22:17 +02001599 int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001600{
1601 mbedtls_x509_crt crt;
Przemek Stekiel39dbe232023-04-03 10:19:22 +02001602 mbedtls_x509_subject_alternative_name san;
David Horstmann9a3a1a62023-06-22 16:59:09 +01001603 char name_buf[128];
toth92ga41954d2021-02-12 16:11:17 +01001604
1605 mbedtls_x509_crt_init(&crt);
1606
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001607 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001608
toth92g357b2972021-05-04 15:41:35 +02001609 if (ref_ret == 0) {
toth92ga41954d2021-02-12 16:11:17 +01001610 /* KeyId test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001611 if (keyId->len > 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001612 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, MBEDTLS_ASN1_OCTET_STRING);
1613 TEST_EQUAL(memcmp(crt.authority_key_id.keyIdentifier.p, keyId->x, keyId->len), 0);
1614 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, keyId->len);
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001615 } else {
1616 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1617 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001618 }
toth92ga41954d2021-02-12 16:11:17 +01001619
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001620
toth92ga41954d2021-02-12 16:11:17 +01001621 /* Issuer test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001622 if (strlen(authorityKeyId_issuer) > 0) {
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001623 mbedtls_x509_sequence *issuerPtr = &crt.authority_key_id.authorityCertIssuer;
Przemek Stekiel01984212023-02-09 09:29:34 +01001624
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001625 TEST_EQUAL(mbedtls_x509_parse_subject_alt_name(&issuerPtr->buf, &san), 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001626
David Horstmann9a3a1a62023-06-22 16:59:09 +01001627 TEST_ASSERT(mbedtls_x509_dn_gets(name_buf, sizeof(name_buf),
1628 &san.san.directory_name)
1629 > 0);
1630 TEST_EQUAL(strcmp(name_buf, authorityKeyId_issuer), 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001631
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001632 mbedtls_x509_free_subject_alt_name(&san);
toth92ga41954d2021-02-12 16:11:17 +01001633 }
1634
1635 /* Serial test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001636 if (serial->len > 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001637 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001638 MBEDTLS_ASN1_INTEGER);
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001639 TEST_EQUAL(memcmp(crt.authority_key_id.authorityCertSerialNumber.p,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001640 serial->x, serial->len), 0);
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001641 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, serial->len);
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001642 } else {
1643 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1644 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001645 }
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001646
toth92g357b2972021-05-04 15:41:35 +02001647 } else {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001648 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1649 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001650
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001651 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1652 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001653 }
1654
1655exit:
1656 mbedtls_x509_crt_free(&crt);
1657}
1658/* END_CASE */