blob: db7c086feeeab8ef3e2e79586d41a709bbc17486 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Azim Khand30ca132017-06-09 04:32:58 +01002#include "mbedtls/bignum.h"
Andres AG4b76aec2016-09-23 13:16:02 +01003#include "mbedtls/x509.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00004#include "mbedtls/x509_crt.h"
5#include "mbedtls/x509_crl.h"
6#include "mbedtls/x509_csr.h"
7#include "mbedtls/pem.h"
8#include "mbedtls/oid.h"
9#include "mbedtls/base64.h"
Chris Jones9f7a6932021-04-14 12:12:09 +010010#include "mbedtls/error.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +010011#include "string.h"
Paul Bakkerb63b0af2011-01-13 17:54:59 +000012
Simon Butcher9e24b512017-07-28 12:15:13 +010013#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
Hanno Becker3b1422e2017-07-26 13:38:02 +010014#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010015 than the current threshold 19. To test larger values, please \
16 adapt the script tests/data_files/dir-max/long.sh."
Hanno Becker3b1422e2017-07-26 13:38:02 +010017#endif
18
Hanno Becker20a4ade2019-06-03 14:27:03 +010019/* Test-only profile allowing all digests, PK algorithms, and curves. */
20const mbedtls_x509_crt_profile profile_all =
21{
22 0xFFFFFFFF, /* Any MD */
23 0xFFFFFFFF, /* Any PK alg */
24 0xFFFFFFFF, /* Any curve */
25 1024,
26};
27
Gilles Peskineef86ab22017-05-05 18:59:02 +020028/* Profile for backward compatibility. Allows SHA-1, unlike the default
29 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020030const mbedtls_x509_crt_profile compat_profile =
31{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010032 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) |
33 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160) |
34 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
35 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
36 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
37 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubneyb84f8d42022-04-21 08:35:29 +010038 0xFFFFFFFF, /* Any PK alg */
39 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020040 1024,
41};
42
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020043const mbedtls_x509_crt_profile profile_rsa3072 =
44{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
46 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
47 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
48 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_RSA),
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020049 0,
50 3072,
51};
52
53const mbedtls_x509_crt_profile profile_sha512 =
54{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010055 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubneyb84f8d42022-04-21 08:35:29 +010056 0xFFFFFFFF, /* Any PK alg */
57 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020058 1024,
59};
60
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061int verify_none(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000062{
Paul Bakker5a624082011-01-18 16:31:52 +000063 ((void) data);
64 ((void) crt);
65 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010066 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020067
Paul Bakker915275b2012-09-28 07:10:55 +000068 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000069}
70
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071int verify_all(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000072{
Paul Bakker5a624082011-01-18 16:31:52 +000073 ((void) data);
74 ((void) crt);
75 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000076 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000077
Paul Bakkerb63b0af2011-01-13 17:54:59 +000078 return 0;
79}
80
Jarno Lamsa03cd1202019-03-27 15:45:04 +020081#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082int ca_callback_fail(void *data, mbedtls_x509_crt const *child, mbedtls_x509_crt **candidates)
Jarno Lamsa557426a2019-03-27 17:08:29 +020083{
84 ((void) data);
85 ((void) child);
86 ((void) candidates);
87
88 return -1;
89}
90
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091int ca_callback(void *data, mbedtls_x509_crt const *child,
92 mbedtls_x509_crt **candidates)
Jarno Lamsa03cd1202019-03-27 15:45:04 +020093{
Hanno Beckercbb59032019-03-28 14:14:22 +000094 int ret = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +020095 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +000096 mbedtls_x509_crt *first;
97
98 /* This is a test-only implementation of the CA callback
99 * which always returns the entire list of trusted certificates.
100 * Production implementations managing a large number of CAs
101 * should use an efficient presentation and lookup for the
102 * set of trusted certificates (such as a hashtable) and only
103 * return those trusted certificates which satisfy basic
104 * parental checks, such as the matching of child `Issuer`
105 * and parent `Subject` field. */
106 ((void) child);
107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
109 if (first == NULL) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000110 ret = -1;
111 goto exit;
112 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 mbedtls_x509_crt_init(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000116 ret = -1;
117 goto exit;
118 }
119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 while (ca->next != NULL) {
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200121 ca = ca->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000123 ret = -1;
124 goto exit;
125 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200126 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000127
128exit:
129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 if (ret != 0) {
131 mbedtls_x509_crt_free(first);
132 mbedtls_free(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000133 first = NULL;
134 }
135
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200136 *candidates = first;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137 return ret;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200138}
139#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141int verify_fatal(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200142{
143 int *levels = (int *) data;
144
145 ((void) crt);
146 ((void) certificate_depth);
147
148 /* Simulate a fatal error in the callback */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 if (*levels & (1 << certificate_depth)) {
150 *flags |= (1 << certificate_depth);
151 return -1 - certificate_depth;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200152 }
153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154 return 0;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200155}
156
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900157/* strsep() not available on Windows */
158char *mystrsep(char **stringp, const char *delim)
159{
160 const char *p;
161 char *ret = *stringp;
162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163 if (*stringp == NULL) {
164 return NULL;
165 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 for (;; (*stringp)++) {
168 if (**stringp == '\0') {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900169 *stringp = NULL;
170 goto done;
171 }
172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100173 for (p = delim; *p != '\0'; p++) {
174 if (**stringp == *p) {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900175 **stringp = '\0';
176 (*stringp)++;
177 goto done;
178 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900180 }
181
182done:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100183 return ret;
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900184}
185
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200186#if defined(MBEDTLS_X509_CRT_PARSE_C)
187typedef struct {
188 char buf[512];
189 char *p;
190} verify_print_context;
191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192void verify_print_init(verify_print_context *ctx)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200193{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 memset(ctx, 0, sizeof(verify_print_context));
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200195 ctx->p = ctx->buf;
196}
197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198int verify_print(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200199{
200 int ret;
201 verify_print_context *ctx = (verify_print_context *) data;
202 char *p = ctx->p;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 size_t n = ctx->buf + sizeof(ctx->buf) - ctx->p;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200204 ((void) flags);
205
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 ret = mbedtls_snprintf(p, n, "depth %d - serial ", certificate_depth);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200207 MBEDTLS_X509_SAFE_SNPRINTF;
208
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200210 MBEDTLS_X509_SAFE_SNPRINTF;
211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100212 ret = mbedtls_snprintf(p, n, " - subject ");
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200213 MBEDTLS_X509_SAFE_SNPRINTF;
214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200216 MBEDTLS_X509_SAFE_SNPRINTF;
217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 ret = mbedtls_snprintf(p, n, " - flags 0x%08x\n", *flags);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200219 MBEDTLS_X509_SAFE_SNPRINTF;
220
221 ctx->p = p;
222
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223 return 0;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200224}
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226int verify_parse_san(mbedtls_x509_subject_alternative_name *san,
227 char **buf, size_t *size)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200228{
229 int ret;
230 size_t i;
231 char *p = *buf;
232 size_t n = *size;
233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 ret = mbedtls_snprintf(p, n, "type : %d", san->type);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200235 MBEDTLS_X509_SAFE_SNPRINTF;
236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 switch (san->type) {
238 case (MBEDTLS_X509_SAN_OTHER_NAME):
239 ret = mbedtls_snprintf(p, n, "\notherName :");
Victor Barpp Gomesfb4723a2022-09-29 10:00:32 -0300240 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100242 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
243 &san->san.other_name.value.hardware_module_name.oid) != 0) {
244 ret = mbedtls_snprintf(p, n, " hardware module name :");
Victor Barpp Gomesfb4723a2022-09-29 10:00:32 -0300245 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 ret = mbedtls_snprintf(p, n, " hardware type : ");
Victor Barpp Gomesfb4723a2022-09-29 10:00:32 -0300247 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249 ret = mbedtls_oid_get_numeric_string(p,
250 n,
251 &san->san.other_name.value.hardware_module_name.oid);
Victor Barpp Gomesfb4723a2022-09-29 10:00:32 -0300252 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 ret = mbedtls_snprintf(p, n, ", hardware serial number : ");
Victor Barpp Gomesfb4723a2022-09-29 10:00:32 -0300255 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 for (i = 0; i < san->san.other_name.value.hardware_module_name.val.len; i++) {
258 ret = mbedtls_snprintf(p,
259 n,
260 "%02X",
261 san->san.other_name.value.hardware_module_name.val.p[i]);
Victor Barpp Gomesfb4723a2022-09-29 10:00:32 -0300262 MBEDTLS_X509_SAFE_SNPRINTF;
263 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200264 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265 break;/* MBEDTLS_OID_ON_HW_MODULE_NAME */
266 case (MBEDTLS_X509_SAN_DNS_NAME):
267 ret = mbedtls_snprintf(p, n, "\ndNSName : ");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200268 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 if (san->san.unstructured_name.len >= n) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200270 *p = '\0';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200272 }
273 n -= san->san.unstructured_name.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 for (i = 0; i < san->san.unstructured_name.len; i++) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200275 *p++ = san->san.unstructured_name.p[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 }
277 break;/* MBEDTLS_X509_SAN_DNS_NAME */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200278
279 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 /*
281 * Should not happen.
282 */
283 return -1;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200284 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285 ret = mbedtls_snprintf(p, n, "\n");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200286 MBEDTLS_X509_SAFE_SNPRINTF;
287
288 *size = n;
289 *buf = p;
290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200292}
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294int parse_crt_ext_cb(void *p_ctx, mbedtls_x509_crt const *crt, mbedtls_x509_buf const *oid,
295 int critical, const unsigned char *cp, const unsigned char *end)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200296{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 (void) crt;
298 (void) critical;
299 mbedtls_x509_buf *new_oid = (mbedtls_x509_buf *) p_ctx;
300 if (oid->tag == MBEDTLS_ASN1_OID &&
301 MBEDTLS_OID_CMP(MBEDTLS_OID_CERTIFICATE_POLICIES, oid) == 0) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200302 /* Handle unknown certificate policy */
303 int ret, parse_ret = 0;
304 size_t len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305 unsigned char **p = (unsigned char **) &cp;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200306
307 /* Get main sequence tag */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100308 ret = mbedtls_asn1_get_tag(p, end, &len,
309 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
310 if (ret != 0) {
311 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
312 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314 if (*p + len != end) {
315 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
316 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
317 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200318
319 /*
320 * Cannot be an empty sequence.
321 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 if (len == 0) {
323 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
324 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
325 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 while (*p < end) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200328 const unsigned char *policy_end;
329
330 /*
331 * Get the policy sequence
332 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
334 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
335 0) {
336 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
337 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200338
339 policy_end = *p + len;
340
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
342 MBEDTLS_ASN1_OID)) != 0) {
343 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
344 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200345
Nicola Di Lietob77fad82020-06-17 17:57:36 +0200346 /*
347 * Recognize exclusively the policy with OID 1
348 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349 if (len != 1 || *p[0] != 1) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200350 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200352
353 *p += len;
354
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 /*
356 * If there is an optional qualifier, then *p < policy_end
357 * Check the Qualifier len to verify it doesn't exceed policy_end.
358 */
359 if (*p < policy_end) {
360 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
361 MBEDTLS_ASN1_CONSTRUCTED |
362 MBEDTLS_ASN1_SEQUENCE)) != 0) {
363 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
364 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200365 /*
366 * Skip the optional policy qualifiers.
367 */
368 *p += len;
369 }
370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 if (*p != policy_end) {
372 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
373 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
374 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200375 }
376
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100377 if (*p != end) {
378 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
379 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
380 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 return parse_ret;
383 } else if (new_oid != NULL && new_oid->tag == oid->tag && new_oid->len == oid->len &&
384 memcmp(new_oid->p, oid->p, oid->len) == 0) {
385 return 0;
386 } else {
387 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
388 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200389 }
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200390}
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200391#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200392/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000393
Paul Bakker33b43f12013-08-20 11:48:36 +0200394/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200396 * END_DEPENDENCIES
397 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100400void x509_parse_san(char *crt_file, char *result_str)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200401{
Ron Eldor890819a2019-05-13 19:03:04 +0300402 int ret;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200403 mbedtls_x509_crt crt;
Ron Eldor890819a2019-05-13 19:03:04 +0300404 mbedtls_x509_subject_alternative_name san;
405 mbedtls_x509_sequence *cur = NULL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200406 char buf[2000];
407 char *p = buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100408 size_t n = sizeof(buf);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200409
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410 mbedtls_x509_crt_init(&crt);
411 memset(buf, 0, 2000);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200412
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100413 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Ron Eldor890819a2019-05-13 19:03:04 +0300414
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100415 if (crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Ron Eldor890819a2019-05-13 19:03:04 +0300416 cur = &crt.subject_alt_names;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417 while (cur != NULL) {
418 ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
419 TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
Ron Eldor890819a2019-05-13 19:03:04 +0300420 /*
421 * If san type not supported, ignore.
422 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 if (ret == 0) {
424 TEST_ASSERT(verify_parse_san(&san, &p, &n) == 0);
425 }
Ron Eldor890819a2019-05-13 19:03:04 +0300426 cur = cur->next;
427 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200428 }
429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 TEST_ASSERT(strcmp(buf, result_str) == 0);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200431
432exit:
433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 mbedtls_x509_crt_free(&crt);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200435}
436/* END_CASE */
437
438/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100439void x509_cert_info(char *crt_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000440{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000442 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000443 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000444
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445 mbedtls_x509_crt_init(&crt);
446 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000447
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100448 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
449 res = mbedtls_x509_crt_info(buf, 2000, "", &crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000450
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 TEST_ASSERT(res != -1);
452 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100454 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200455
456exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000458}
Paul Bakker33b43f12013-08-20 11:48:36 +0200459/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100462void mbedtls_x509_crl_info(char *crl_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000463{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000465 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000466 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000467
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100468 mbedtls_x509_crl_init(&crl);
469 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100471 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == 0);
472 res = mbedtls_x509_crl_info(buf, 2000, "", &crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000473
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100474 TEST_ASSERT(res != -1);
475 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000476
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100477 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200478
479exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480 mbedtls_x509_crl_free(&crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000481}
Paul Bakker33b43f12013-08-20 11:48:36 +0200482/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000483
Andres AGa39db392016-12-08 17:10:38 +0000484/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485void mbedtls_x509_crl_parse(char *crl_file, int result)
Andres AGa39db392016-12-08 17:10:38 +0000486{
487 mbedtls_x509_crl crl;
488 char buf[2000];
489
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100490 mbedtls_x509_crl_init(&crl);
491 memset(buf, 0, 2000);
Andres AGa39db392016-12-08 17:10:38 +0000492
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100493 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == result);
Andres AGa39db392016-12-08 17:10:38 +0000494
495exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100496 mbedtls_x509_crl_free(&crl);
Andres AGa39db392016-12-08 17:10:38 +0000497}
498/* END_CASE */
499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100501void mbedtls_x509_csr_info(char *csr_file, char *result_str)
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100502{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100504 char buf[2000];
505 int res;
506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100507 mbedtls_x509_csr_init(&csr);
508 memset(buf, 0, 2000);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100509
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100510 TEST_ASSERT(mbedtls_x509_csr_parse_file(&csr, csr_file) == 0);
511 res = mbedtls_x509_csr_info(buf, 2000, "", &csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100512
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100513 TEST_ASSERT(res != -1);
514 TEST_ASSERT(res != -2);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100516 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200517
518exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100519 mbedtls_x509_csr_free(&csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100520}
521/* END_CASE */
522
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100523/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100524void x509_verify_info(int flags, char *prefix, char *result_str)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100525{
526 char buf[2000];
527 int res;
528
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100529 memset(buf, 0, sizeof(buf));
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100530
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100531 res = mbedtls_x509_crt_verify_info(buf, sizeof(buf), prefix, flags);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100532
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100533 TEST_ASSERT(res >= 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100534
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100535 TEST_ASSERT(strcmp(buf, result_str) == 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100536}
537/* END_CASE */
538
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200539/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100540void x509_verify_restart(char *crt_file, char *ca_file,
541 int result, int flags_result,
542 int max_ops, int min_restart, int max_restart)
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200543{
544 int ret, cnt_restart;
545 mbedtls_x509_crt_restart_ctx rs_ctx;
546 mbedtls_x509_crt crt;
547 mbedtls_x509_crt ca;
548 uint32_t flags = 0;
549
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200550 /*
551 * See comments on ecp_test_vect_restart() for op count precision.
552 *
553 * For reference, with mbed TLS 2.6 and default settings:
554 * - ecdsa_verify() for P-256: ~ 6700
555 * - ecdsa_verify() for P-384: ~ 18800
556 * - x509_verify() for server5 -> test-ca2: ~ 18800
557 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
558 */
559
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100560 mbedtls_x509_crt_restart_init(&rs_ctx);
561 mbedtls_x509_crt_init(&crt);
562 mbedtls_x509_crt_init(&ca);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200563
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100564 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
565 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200566
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100567 mbedtls_ecp_set_max_ops(max_ops);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200568
569 cnt_restart = 0;
570 do {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100571 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
572 &mbedtls_x509_crt_profile_default, NULL, &flags,
573 NULL, NULL, &rs_ctx);
574 } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200575
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100576 TEST_ASSERT(ret == result);
577 TEST_ASSERT(flags == (uint32_t) flags_result);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100579 TEST_ASSERT(cnt_restart >= min_restart);
580 TEST_ASSERT(cnt_restart <= max_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200581
582 /* Do we leak memory when aborting? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100583 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
584 &mbedtls_x509_crt_profile_default, NULL, &flags,
585 NULL, NULL, &rs_ctx);
586 TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200587
588exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100589 mbedtls_x509_crt_restart_free(&rs_ctx);
590 mbedtls_x509_crt_free(&crt);
591 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200592}
593/* END_CASE */
594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100596void x509_verify(char *crt_file, char *ca_file, char *crl_file,
597 char *cn_name_str, int result, int flags_result,
598 char *profile_str,
599 char *verify_callback)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000600{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 mbedtls_x509_crt crt;
602 mbedtls_x509_crt ca;
603 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200604 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000605 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200606 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100607 char *cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200608 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000609
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100610 mbedtls_x509_crt_init(&crt);
611 mbedtls_x509_crt_init(&ca);
612 mbedtls_x509_crl_init(&crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000613
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100614 USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +0100615
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100616 if (strcmp(cn_name_str, "NULL") != 0) {
Paul Bakker33b43f12013-08-20 11:48:36 +0200617 cn_name = cn_name_str;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100618 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100620 if (strcmp(profile_str, "") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200621 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100622 } else if (strcmp(profile_str, "next") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200623 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100624 } else if (strcmp(profile_str, "suite_b") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200625 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100626 } else if (strcmp(profile_str, "compat") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200627 profile = &compat_profile;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100628 } else if (strcmp(profile_str, "all") == 0) {
Hanno Becker20a4ade2019-06-03 14:27:03 +0100629 profile = &profile_all;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100630 } else {
631 TEST_ASSERT("Unknown algorithm profile" == 0);
632 }
Gilles Peskineef86ab22017-05-05 18:59:02 +0200633
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100634 if (strcmp(verify_callback, "NULL") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200635 f_vrfy = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636 } else if (strcmp(verify_callback, "verify_none") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200637 f_vrfy = verify_none;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100638 } else if (strcmp(verify_callback, "verify_all") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200639 f_vrfy = verify_all;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100640 } else {
641 TEST_ASSERT("No known verify callback selected" == 0);
642 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100644 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
645 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
646 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == 0);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000647
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100648 res = mbedtls_x509_crt_verify_with_profile(&crt,
649 &ca,
650 &crl,
651 profile,
652 cn_name,
653 &flags,
654 f_vrfy,
655 NULL);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100657 TEST_ASSERT(res == (result));
658 TEST_ASSERT(flags == (uint32_t) (flags_result));
Paul Bakkerbd51b262014-07-10 15:26:12 +0200659
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200660#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker0350d562019-03-28 14:23:36 +0000661 /* CRLs aren't supported with CA callbacks, so skip the CA callback
Jarno Lamsadfd22c42019-04-01 15:18:53 +0300662 * version of the test if CRLs are in use. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100663 if (crl_file == NULL || strcmp(crl_file, "") == 0) {
Hanno Becker0350d562019-03-28 14:23:36 +0000664 flags = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100666 res = mbedtls_x509_crt_verify_with_ca_cb(&crt,
667 ca_callback,
668 &ca,
669 profile,
670 cn_name,
671 &flags,
672 f_vrfy,
673 NULL);
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200674
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100675 TEST_ASSERT(res == (result));
676 TEST_ASSERT(flags == (uint32_t) (flags_result));
Hanno Becker0350d562019-03-28 14:23:36 +0000677 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200678#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200679exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100680 mbedtls_x509_crt_free(&crt);
681 mbedtls_x509_crt_free(&ca);
682 mbedtls_x509_crl_free(&crl);
683 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000684}
Paul Bakker33b43f12013-08-20 11:48:36 +0200685/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000686
Jarno Lamsa557426a2019-03-27 17:08:29 +0200687/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100688void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
689 int exp_ret)
Jarno Lamsa557426a2019-03-27 17:08:29 +0200690{
691 int ret;
692 mbedtls_x509_crt crt;
693 mbedtls_x509_crt ca;
694 uint32_t flags = 0;
Hanno Becker3f932bb2019-03-28 17:06:47 +0000695
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100696 mbedtls_x509_crt_init(&crt);
697 mbedtls_x509_crt_init(&ca);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100699 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
700 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100702 if (strcmp(name, "NULL") == 0) {
Jarno Lamsa557426a2019-03-27 17:08:29 +0200703 name = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100704 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000705
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100706 ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca,
707 &compat_profile, name, &flags,
708 NULL, NULL);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100710 TEST_ASSERT(ret == exp_ret);
711 TEST_ASSERT(flags == (uint32_t) (-1));
Jarno Lamsa557426a2019-03-27 17:08:29 +0200712exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100713 mbedtls_x509_crt_free(&crt);
714 mbedtls_x509_crt_free(&ca);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200715}
716/* END_CASE */
717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100719void x509_verify_callback(char *crt_file, char *ca_file, char *name,
720 int exp_ret, char *exp_vrfy_out)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200721{
722 int ret;
723 mbedtls_x509_crt crt;
724 mbedtls_x509_crt ca;
725 uint32_t flags = 0;
726 verify_print_context vrfy_ctx;
727
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100728 mbedtls_x509_crt_init(&crt);
729 mbedtls_x509_crt_init(&ca);
730 verify_print_init(&vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200731
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100732 USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +0100733
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100734 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
735 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100737 if (strcmp(name, "NULL") == 0) {
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200738 name = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100739 }
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200740
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100741 ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL,
742 &compat_profile,
743 name, &flags,
744 verify_print, &vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200745
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100746 TEST_ASSERT(ret == exp_ret);
747 TEST_ASSERT(strcmp(vrfy_ctx.buf, exp_vrfy_out) == 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200748
749exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100750 mbedtls_x509_crt_free(&crt);
751 mbedtls_x509_crt_free(&ca);
752 USE_PSA_DONE();
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200753}
754/* END_CASE */
755
756/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100757void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000758{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000760 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200761 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000762
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100763 mbedtls_x509_crt_init(&crt);
764 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000765
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100766 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
767 if (strcmp(entity, "subject") == 0) {
768 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
769 } else if (strcmp(entity, "issuer") == 0) {
770 res = mbedtls_x509_dn_gets(buf, 2000, &crt.issuer);
771 } else {
772 TEST_ASSERT("Unknown entity" == 0);
773 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000774
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100775 TEST_ASSERT(res != -1);
776 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000777
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100778 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200779
780exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100781 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000782}
Paul Bakker33b43f12013-08-20 11:48:36 +0200783/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000784
Werner Lewis9a2356b2022-06-17 15:51:55 +0100785/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100786void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
787 char *new_subject_ou,
788 char *result_str,
789 int ret)
Werner Lewis9a2356b2022-06-17 15:51:55 +0100790{
791 mbedtls_x509_crt crt;
792 char buf[2000];
793 int res = 0;
794
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100795 mbedtls_x509_crt_init(&crt);
796 memset(buf, 0, 2000);
Werner Lewis9a2356b2022-06-17 15:51:55 +0100797
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100798 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Werner Lewis9a2356b2022-06-17 15:51:55 +0100799 crt.subject.next->val.p = (unsigned char *) new_subject_ou;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100800 crt.subject.next->val.len = strlen(new_subject_ou);
Werner Lewis9a2356b2022-06-17 15:51:55 +0100801
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100802 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
Werner Lewis9a2356b2022-06-17 15:51:55 +0100803
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100804 if (ret != 0) {
805 TEST_ASSERT(res == ret);
806 } else {
807 TEST_ASSERT(res != -1);
808 TEST_ASSERT(res != -2);
809 TEST_ASSERT(strcmp(buf, result_str) == 0);
Werner Lewis9a2356b2022-06-17 15:51:55 +0100810 }
811exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100812 mbedtls_x509_crt_free(&crt);
Werner Lewis9a2356b2022-06-17 15:51:55 +0100813}
814/* END_CASE */
815
David Horstmann77ecc6e2022-10-05 13:08:45 +0100816/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100817void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
David Horstmann77ecc6e2022-10-05 13:08:45 +0100818{
819 unsigned char *name;
820 unsigned char *p;
821 size_t name_len;
David Horstmann6c4226c2022-10-20 10:18:37 +0100822 mbedtls_x509_name head;
David Horstmann77ecc6e2022-10-05 13:08:45 +0100823 mbedtls_x509_name *allocated, *prev;
David Horstmann4a67c352022-10-17 17:59:10 +0100824 int ret;
David Horstmann77ecc6e2022-10-05 13:08:45 +0100825
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100826 memset(&head, 0, sizeof(head));
David Horstmann6c4226c2022-10-20 10:18:37 +0100827
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100828 name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
David Horstmann77ecc6e2022-10-05 13:08:45 +0100829 p = name;
830
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100831 ret = mbedtls_x509_get_name(&p, (name + name_len), &head);
832 if (ret == 0) {
David Horstmann77ecc6e2022-10-05 13:08:45 +0100833 allocated = head.next;
David Horstmann77ecc6e2022-10-05 13:08:45 +0100834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100835 while (allocated != NULL) {
David Horstmann77ecc6e2022-10-05 13:08:45 +0100836 prev = allocated;
837 allocated = allocated->next;
838
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100839 mbedtls_free(prev);
David Horstmann77ecc6e2022-10-05 13:08:45 +0100840 }
841 }
842
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100843 TEST_EQUAL(ret, exp_ret);
David Horstmann77ecc6e2022-10-05 13:08:45 +0100844
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100845 mbedtls_free(name);
David Horstmann77ecc6e2022-10-05 13:08:45 +0100846}
847/* END_CASE */
848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100850void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000851{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100854 mbedtls_x509_crt_init(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000855
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100856 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200857
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100858 if (strcmp(entity, "valid_from") == 0) {
859 TEST_ASSERT(mbedtls_x509_time_is_past(&crt.valid_from) == result);
860 } else if (strcmp(entity, "valid_to") == 0) {
861 TEST_ASSERT(mbedtls_x509_time_is_past(&crt.valid_to) == result);
862 } else {
863 TEST_ASSERT("Unknown entity" == 0);
864 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000865
Paul Bakkerbd51b262014-07-10 15:26:12 +0200866exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100867 mbedtls_x509_crt_free(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000868}
Paul Bakker33b43f12013-08-20 11:48:36 +0200869/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100872void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100873{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100875
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100876 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100877
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100878 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100880 if (strcmp(entity, "valid_from") == 0) {
881 TEST_ASSERT(mbedtls_x509_time_is_future(&crt.valid_from) == result);
882 } else if (strcmp(entity, "valid_to") == 0) {
883 TEST_ASSERT(mbedtls_x509_time_is_future(&crt.valid_to) == result);
884 } else {
885 TEST_ASSERT("Unknown entity" == 0);
886 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100887
Paul Bakkerbd51b262014-07-10 15:26:12 +0200888exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100889 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100890}
891/* END_CASE */
892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100894void x509parse_crt_file(char *crt_file, int result)
Paul Bakker5a5fa922014-09-26 14:53:04 +0200895{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +0200897
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100898 mbedtls_x509_crt_init(&crt);
Paul Bakker5a5fa922014-09-26 14:53:04 +0200899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100900 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == result);
Paul Bakker5a5fa922014-09-26 14:53:04 +0200901
902exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100903 mbedtls_x509_crt_free(&crt);
Paul Bakker5a5fa922014-09-26 14:53:04 +0200904}
905/* END_CASE */
906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100908void x509parse_crt(data_t *buf, char *result_str, int result)
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000909{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 mbedtls_x509_crt crt;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000911 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100912 int res;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000913
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100914 mbedtls_x509_crt_init(&crt);
915 memset(output, 0, 2000);
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000916
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100917 TEST_ASSERT(mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len) == (result));
918 if ((result) == 0) {
919 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000920
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100921 TEST_ASSERT(res != -1);
922 TEST_ASSERT(res != -2);
Hanno Becker2d8a2c02019-01-31 09:15:53 +0000923
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100924 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Hanno Becker2d8a2c02019-01-31 09:15:53 +0000925 }
926
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100927 mbedtls_x509_crt_free(&crt);
928 mbedtls_x509_crt_init(&crt);
929 memset(output, 0, 2000);
Hanno Becker2d8a2c02019-01-31 09:15:53 +0000930
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100931 TEST_ASSERT(mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len) == (result));
932 if ((result) == 0) {
933 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakker33b43f12013-08-20 11:48:36 +0200934
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100935 TEST_ASSERT(res != -1);
936 TEST_ASSERT(res != -2);
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000937
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100938 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000939 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000940
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100941 mbedtls_x509_crt_free(&crt);
942 mbedtls_x509_crt_init(&crt);
943 memset(output, 0, 2000);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200944
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100945 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL,
946 NULL) == (result));
947 if ((result) == 0) {
948 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200949
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100950 TEST_ASSERT(res != -1);
951 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200952
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100953 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200954 }
955
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100956 mbedtls_x509_crt_free(&crt);
957 mbedtls_x509_crt_init(&crt);
958 memset(output, 0, 2000);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200959
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100960 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL,
961 NULL) == (result));
962 if ((result) == 0) {
963 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200964
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100965 TEST_ASSERT(res != -1);
966 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100968 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200969 }
970
971exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100972 mbedtls_x509_crt_free(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200973}
974/* END_CASE */
975
976/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100977void x509parse_crt_cb(data_t *buf, char *result_str, int result)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200978{
979 mbedtls_x509_crt crt;
Nicola Di Lietoe58b4632020-05-29 22:58:25 +0200980 mbedtls_x509_buf oid;
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200981 unsigned char output[2000];
982 int res;
983
Nicola Di Lietoe58b4632020-05-29 22:58:25 +0200984 oid.tag = MBEDTLS_ASN1_OID;
985 oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100986 oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
Nicola Di Lietoe58b4632020-05-29 22:58:25 +0200987
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100988 mbedtls_x509_crt_init(&crt);
989 memset(output, 0, 2000);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200990
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100991 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
992 &oid) == (result));
993 if ((result) == 0) {
994 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200995
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100996 TEST_ASSERT(res != -1);
997 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200998
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100999 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001000 }
1001
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001002 mbedtls_x509_crt_free(&crt);
1003 mbedtls_x509_crt_init(&crt);
1004 memset(output, 0, 2000);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001005
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001006 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1007 &oid) == (result));
1008 if ((result) == 0) {
1009 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001010
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001011 TEST_ASSERT(res != -1);
1012 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001013
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001014 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001015 }
1016
Paul Bakkerbd51b262014-07-10 15:26:12 +02001017exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001018 mbedtls_x509_crt_free(&crt);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001019}
Paul Bakker33b43f12013-08-20 11:48:36 +02001020/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001023void x509parse_crl(data_t *buf, char *result_str, int result)
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001024{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001026 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +01001027 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001028
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001029 mbedtls_x509_crl_init(&crl);
1030 memset(output, 0, 2000);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001031
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001032
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001033 TEST_ASSERT(mbedtls_x509_crl_parse(&crl, buf->x, buf->len) == (result));
1034 if ((result) == 0) {
1035 res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
Paul Bakker33b43f12013-08-20 11:48:36 +02001036
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001037 TEST_ASSERT(res != -1);
1038 TEST_ASSERT(res != -2);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001040 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001041 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001042
Paul Bakkerbd51b262014-07-10 15:26:12 +02001043exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001044 mbedtls_x509_crl_free(&crl);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001045}
Paul Bakker33b43f12013-08-20 11:48:36 +02001046/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001049void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001050{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001052 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001053 int my_ret;
1054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001055 mbedtls_x509_csr_init(&csr);
1056 memset(my_out, 0, sizeof(my_out));
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001058 my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
1059 TEST_ASSERT(my_ret == ref_ret);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001060
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001061 if (ref_ret == 0) {
1062 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1063 TEST_ASSERT(my_out_len == strlen(ref_out));
1064 TEST_ASSERT(strcmp(my_out, ref_out) == 0);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001065 }
1066
Paul Bakkerbd51b262014-07-10 15:26:12 +02001067exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001068 mbedtls_x509_csr_free(&csr);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001069}
1070/* END_CASE */
1071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001073void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001074{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001076 int i;
1077
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001078 mbedtls_x509_crt_init(&chain);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001080 TEST_ASSERT(mbedtls_x509_crt_parse_path(&chain, crt_path) == ret);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001081
1082 /* Check how many certs we got */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001083 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1084 if (cur->raw.p != NULL) {
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001085 i++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001086 }
1087 }
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001088
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001089 TEST_ASSERT(i == nb_crt);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001090
Paul Bakkerbd51b262014-07-10 15:26:12 +02001091exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001092 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001093}
1094/* END_CASE */
1095
Janos Follath822b2c32015-10-11 10:25:22 +02001096/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001097void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1098 int ret_chk, int flags_chk)
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001099{
1100 char file_buf[128];
1101 int ret;
1102 uint32_t flags;
1103 mbedtls_x509_crt trusted, chain;
1104
1105 /*
1106 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1107 * with NN.crt signed by NN-1.crt
1108 */
1109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001110 mbedtls_x509_crt_init(&trusted);
1111 mbedtls_x509_crt_init(&chain);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001113 USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +01001114
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001115 /* Load trusted root */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001116 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, ca_file) == 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001117
1118 /* Load a chain with nb_int intermediates (from 01 to nb_int),
1119 * plus one "end-entity" cert (nb_int + 1) */
Dave Rodgman18688702023-02-02 12:40:50 +00001120 ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001121 nb_int + 1);
Dave Rodgman18688702023-02-02 12:40:50 +00001122 TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001123 TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, file_buf) == 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001124
1125 /* Try to verify that chain */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001126 ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1127 NULL, NULL);
1128 TEST_ASSERT(ret == ret_chk);
1129 TEST_ASSERT(flags == (uint32_t) flags_chk);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001130
1131exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001132 mbedtls_x509_crt_free(&chain);
1133 mbedtls_x509_crt_free(&trusted);
1134 USE_PSA_DONE();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001135}
1136/* END_CASE */
1137
1138/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001139void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1140 int flags_result, int result,
1141 char *profile_name, int vrfy_fatal_lvls)
Janos Follath822b2c32015-10-11 10:25:22 +02001142{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001143 char *act;
Janos Follath822b2c32015-10-11 10:25:22 +02001144 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001145 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001146 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001147 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +02001148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001149 mbedtls_x509_crt_init(&chain);
1150 mbedtls_x509_crt_init(&trusted);
Janos Follath822b2c32015-10-11 10:25:22 +02001151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001152 USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +01001153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001154 while ((act = mystrsep(&chain_paths, " ")) != NULL) {
1155 TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, act) == 0);
1156 }
1157 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, trusted_ca) == 0);
Janos Follath822b2c32015-10-11 10:25:22 +02001158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001159 if (strcmp(profile_name, "") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001160 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001161 } else if (strcmp(profile_name, "next") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001162 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001163 } else if (strcmp(profile_name, "suiteb") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001164 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001165 } else if (strcmp(profile_name, "rsa3072") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001166 profile = &profile_rsa3072;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001167 } else if (strcmp(profile_name, "sha512") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001168 profile = &profile_sha512;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001169 }
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001171 res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1172 NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
Janos Follathef4f2582015-10-11 16:17:27 +02001173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001174 TEST_ASSERT(res == (result));
1175 TEST_ASSERT(flags == (uint32_t) (flags_result));
Janos Follath822b2c32015-10-11 10:25:22 +02001176
1177exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001178 mbedtls_x509_crt_free(&trusted);
1179 mbedtls_x509_crt_free(&chain);
1180 USE_PSA_DONE();
Janos Follath822b2c32015-10-11 10:25:22 +02001181}
1182/* END_CASE */
1183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001185void x509_oid_desc(data_t *buf, char *ref_desc)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001186{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001188 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001189 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001190
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001193 oid.p = buf->x;
1194 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001196 ret = mbedtls_oid_get_extended_key_usage(&oid, &desc);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001198 if (strcmp(ref_desc, "notfound") == 0) {
1199 TEST_ASSERT(ret != 0);
1200 TEST_ASSERT(desc == NULL);
1201 } else {
1202 TEST_ASSERT(ret == 0);
1203 TEST_ASSERT(desc != NULL);
1204 TEST_ASSERT(strcmp(desc, ref_desc) == 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001205 }
1206}
1207/* END_CASE */
1208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001210void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001211{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001213 char num_buf[100];
1214
Dave Rodgman18688702023-02-02 12:40:50 +00001215 memset(num_buf, 0x2a, sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001218 oid.p = oid_buf->x;
1219 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001220
Dave Rodgman18688702023-02-02 12:40:50 +00001221 TEST_ASSERT((size_t) blen <= sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001222
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001223 TEST_ASSERT(mbedtls_oid_get_numeric_string(num_buf, blen, &oid) == ret);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001224
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001225 if (ret >= 0) {
1226 TEST_ASSERT(num_buf[ret] == 0);
1227 TEST_ASSERT(strcmp(num_buf, numstr) == 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001228 }
1229}
1230/* END_CASE */
1231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_KEY_USAGE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001233void x509_check_key_usage(char *crt_file, int usage, int ret)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001234{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001237 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001239 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001241 TEST_ASSERT(mbedtls_x509_crt_check_key_usage(&crt, usage) == ret);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001242
Paul Bakkerbd51b262014-07-10 15:26:12 +02001243exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001244 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001245}
1246/* END_CASE */
1247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001249void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1250 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001251{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001254 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001255
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001257 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001259 TEST_ASSERT(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x,
1260 oid->len) == ret);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001261
Paul Bakkerbd51b262014-07-10 15:26:12 +02001262exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001263 mbedtls_x509_crt_free(&crt);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001264}
1265/* END_CASE */
1266
Andres AG4b76aec2016-09-23 13:16:02 +01001267/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001268void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1269 int day, int hour, int min, int sec)
Andres AG4b76aec2016-09-23 13:16:02 +01001270{
1271 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001272 unsigned char buf[21];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001273 unsigned char *start = buf;
1274 unsigned char *end = buf;
Andres AG4b76aec2016-09-23 13:16:02 +01001275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001276 memset(&time, 0x00, sizeof(time));
1277 *end = (unsigned char) tag; end++;
1278 *end = strlen(time_str);
1279 TEST_ASSERT(*end < 20);
Andres AG4b76aec2016-09-23 13:16:02 +01001280 end++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001281 memcpy(end, time_str, (size_t) *(end - 1));
Andres AG4b76aec2016-09-23 13:16:02 +01001282 end += *(end - 1);
1283
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001284 TEST_ASSERT(mbedtls_x509_get_time(&start, end, &time) == ret);
1285 if (ret == 0) {
1286 TEST_ASSERT(year == time.year);
1287 TEST_ASSERT(mon == time.mon);
1288 TEST_ASSERT(day == time.day);
1289 TEST_ASSERT(hour == time.hour);
1290 TEST_ASSERT(min == time.min);
1291 TEST_ASSERT(sec == time.sec);
Andres AG4b76aec2016-09-23 13:16:02 +01001292 }
1293}
1294/* END_CASE */
1295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001297void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1298 int ref_msg_md, int ref_mgf_md,
1299 int ref_salt_len, int ref_ret)
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001300{
1301 int my_ret;
Ronald Cronac6ae352020-06-26 14:33:03 +02001302 mbedtls_x509_buf buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001304 int my_salt_len;
1305
Ronald Cronac6ae352020-06-26 14:33:03 +02001306 buf.p = params->x;
1307 buf.len = params->len;
1308 buf.tag = params_tag;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001310 my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1311 &my_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001313 TEST_ASSERT(my_ret == ref_ret);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001315 if (ref_ret == 0) {
1316 TEST_ASSERT(my_msg_md == (mbedtls_md_type_t) ref_msg_md);
1317 TEST_ASSERT(my_mgf_md == (mbedtls_md_type_t) ref_mgf_md);
1318 TEST_ASSERT(my_salt_len == ref_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001319 }
1320
Paul Bakkerbd51b262014-07-10 15:26:12 +02001321exit:
Azim Khand30ca132017-06-09 04:32:58 +01001322 ;;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001323}
1324/* END_CASE */
1325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001327void x509_selftest()
Paul Bakker37940d9f2009-07-10 22:38:58 +00001328{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001329 TEST_ASSERT(mbedtls_x509_self_test(1) == 0);
Paul Bakker37940d9f2009-07-10 22:38:58 +00001330}
Paul Bakker33b43f12013-08-20 11:48:36 +02001331/* END_CASE */