blob: 6831b0d4b240a4a69ea04977a54b91ef4ed4682f [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);
Valerio Settie7373a82023-04-19 14:53:36 +0200412 USE_PSA_INIT();
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Ron Eldor890819a2019-05-13 19:03:04 +0300415
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416 if (crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Ron Eldor890819a2019-05-13 19:03:04 +0300417 cur = &crt.subject_alt_names;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100418 while (cur != NULL) {
419 ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
420 TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
Ron Eldor890819a2019-05-13 19:03:04 +0300421 /*
422 * If san type not supported, ignore.
423 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100424 if (ret == 0) {
425 TEST_ASSERT(verify_parse_san(&san, &p, &n) == 0);
426 }
Ron Eldor890819a2019-05-13 19:03:04 +0300427 cur = cur->next;
428 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200429 }
430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100431 TEST_ASSERT(strcmp(buf, result_str) == 0);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200432
433exit:
434
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 mbedtls_x509_crt_free(&crt);
Valerio Settie7373a82023-04-19 14:53:36 +0200436 USE_PSA_DONE();
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200437}
438/* END_CASE */
439
440/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441void x509_cert_info(char *crt_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000442{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000444 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000445 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100447 mbedtls_x509_crt_init(&crt);
448 memset(buf, 0, 2000);
Valerio Settie7373a82023-04-19 14:53:36 +0200449 USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000450
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
452 res = mbedtls_x509_crt_info(buf, 2000, "", &crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100454 TEST_ASSERT(res != -1);
455 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200458
459exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100460 mbedtls_x509_crt_free(&crt);
Valerio Settie7373a82023-04-19 14:53:36 +0200461 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000462}
Paul Bakker33b43f12013-08-20 11:48:36 +0200463/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100466void mbedtls_x509_crl_info(char *crl_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000467{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000469 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000470 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100472 mbedtls_x509_crl_init(&crl);
473 memset(buf, 0, 2000);
Valerio Settie7373a82023-04-19 14:53:36 +0200474 USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000475
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == 0);
477 res = mbedtls_x509_crl_info(buf, 2000, "", &crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100479 TEST_ASSERT(res != -1);
480 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100482 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200483
484exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485 mbedtls_x509_crl_free(&crl);
Valerio Settie7373a82023-04-19 14:53:36 +0200486 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000487}
Paul Bakker33b43f12013-08-20 11:48:36 +0200488/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000489
Andres AGa39db392016-12-08 17:10:38 +0000490/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100491void mbedtls_x509_crl_parse(char *crl_file, int result)
Andres AGa39db392016-12-08 17:10:38 +0000492{
493 mbedtls_x509_crl crl;
494 char buf[2000];
495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100496 mbedtls_x509_crl_init(&crl);
497 memset(buf, 0, 2000);
Valerio Settie7373a82023-04-19 14:53:36 +0200498 USE_PSA_INIT();
Andres AGa39db392016-12-08 17:10:38 +0000499
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100500 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == result);
Andres AGa39db392016-12-08 17:10:38 +0000501
502exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100503 mbedtls_x509_crl_free(&crl);
Valerio Settie7373a82023-04-19 14:53:36 +0200504 USE_PSA_DONE();
Andres AGa39db392016-12-08 17:10:38 +0000505}
506/* END_CASE */
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100509void mbedtls_x509_csr_info(char *csr_file, char *result_str)
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100510{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100512 char buf[2000];
513 int res;
514
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100515 mbedtls_x509_csr_init(&csr);
516 memset(buf, 0, 2000);
Valerio Settie7373a82023-04-19 14:53:36 +0200517 USE_PSA_INIT();
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100518
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100519 TEST_ASSERT(mbedtls_x509_csr_parse_file(&csr, csr_file) == 0);
520 res = mbedtls_x509_csr_info(buf, 2000, "", &csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100522 TEST_ASSERT(res != -1);
523 TEST_ASSERT(res != -2);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100524
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100525 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200526
527exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100528 mbedtls_x509_csr_free(&csr);
Valerio Settie7373a82023-04-19 14:53:36 +0200529 USE_PSA_DONE();
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100530}
531/* END_CASE */
532
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100533/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100534void x509_verify_info(int flags, char *prefix, char *result_str)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100535{
536 char buf[2000];
537 int res;
538
Valerio Settie7373a82023-04-19 14:53:36 +0200539 USE_PSA_INIT();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100540 memset(buf, 0, sizeof(buf));
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100541
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100542 res = mbedtls_x509_crt_verify_info(buf, sizeof(buf), prefix, flags);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100544 TEST_ASSERT(res >= 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100545
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100546 TEST_ASSERT(strcmp(buf, result_str) == 0);
Valerio Settie7373a82023-04-19 14:53:36 +0200547
548exit:
549 USE_PSA_DONE();
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100550}
551/* END_CASE */
552
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200553/* 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 +0100554void x509_verify_restart(char *crt_file, char *ca_file,
555 int result, int flags_result,
556 int max_ops, int min_restart, int max_restart)
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200557{
558 int ret, cnt_restart;
559 mbedtls_x509_crt_restart_ctx rs_ctx;
560 mbedtls_x509_crt crt;
561 mbedtls_x509_crt ca;
562 uint32_t flags = 0;
563
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200564 /*
565 * See comments on ecp_test_vect_restart() for op count precision.
566 *
567 * For reference, with mbed TLS 2.6 and default settings:
568 * - ecdsa_verify() for P-256: ~ 6700
569 * - ecdsa_verify() for P-384: ~ 18800
570 * - x509_verify() for server5 -> test-ca2: ~ 18800
571 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
572 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100573 mbedtls_x509_crt_restart_init(&rs_ctx);
574 mbedtls_x509_crt_init(&crt);
575 mbedtls_x509_crt_init(&ca);
Valerio Settie7373a82023-04-19 14:53:36 +0200576 USE_PSA_INIT();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200577
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100578 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
579 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200580
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100581 mbedtls_ecp_set_max_ops(max_ops);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200582
583 cnt_restart = 0;
584 do {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100585 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
586 &mbedtls_x509_crt_profile_default, NULL, &flags,
587 NULL, NULL, &rs_ctx);
588 } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200589
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100590 TEST_ASSERT(ret == result);
591 TEST_ASSERT(flags == (uint32_t) flags_result);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200592
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100593 TEST_ASSERT(cnt_restart >= min_restart);
594 TEST_ASSERT(cnt_restart <= max_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200595
596 /* Do we leak memory when aborting? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100597 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
598 &mbedtls_x509_crt_profile_default, NULL, &flags,
599 NULL, NULL, &rs_ctx);
600 TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200601
602exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100603 mbedtls_x509_crt_restart_free(&rs_ctx);
604 mbedtls_x509_crt_free(&crt);
605 mbedtls_x509_crt_free(&ca);
Valerio Settie7373a82023-04-19 14:53:36 +0200606 USE_PSA_DONE();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200607}
608/* END_CASE */
609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100611void x509_verify(char *crt_file, char *ca_file, char *crl_file,
612 char *cn_name_str, int result, int flags_result,
613 char *profile_str,
614 char *verify_callback)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000615{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_x509_crt crt;
617 mbedtls_x509_crt ca;
618 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200619 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000620 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200621 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100622 char *cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200623 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000624
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100625 mbedtls_x509_crt_init(&crt);
626 mbedtls_x509_crt_init(&ca);
627 mbedtls_x509_crl_init(&crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100629 USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +0100630
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100631 if (strcmp(cn_name_str, "NULL") != 0) {
Paul Bakker33b43f12013-08-20 11:48:36 +0200632 cn_name = cn_name_str;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100633 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200634
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100635 if (strcmp(profile_str, "") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200636 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100637 } else if (strcmp(profile_str, "next") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200638 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100639 } else if (strcmp(profile_str, "suite_b") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200640 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100641 } else if (strcmp(profile_str, "compat") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200642 profile = &compat_profile;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100643 } else if (strcmp(profile_str, "all") == 0) {
Hanno Becker20a4ade2019-06-03 14:27:03 +0100644 profile = &profile_all;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100645 } else {
646 TEST_ASSERT("Unknown algorithm profile" == 0);
647 }
Gilles Peskineef86ab22017-05-05 18:59:02 +0200648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100649 if (strcmp(verify_callback, "NULL") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200650 f_vrfy = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100651 } else if (strcmp(verify_callback, "verify_none") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200652 f_vrfy = verify_none;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100653 } else if (strcmp(verify_callback, "verify_all") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200654 f_vrfy = verify_all;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100655 } else {
656 TEST_ASSERT("No known verify callback selected" == 0);
657 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200658
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100659 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
660 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
661 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == 0);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100663 res = mbedtls_x509_crt_verify_with_profile(&crt,
664 &ca,
665 &crl,
666 profile,
667 cn_name,
668 &flags,
669 f_vrfy,
670 NULL);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200671
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100672 TEST_ASSERT(res == (result));
673 TEST_ASSERT(flags == (uint32_t) (flags_result));
Paul Bakkerbd51b262014-07-10 15:26:12 +0200674
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200675#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker0350d562019-03-28 14:23:36 +0000676 /* CRLs aren't supported with CA callbacks, so skip the CA callback
Jarno Lamsadfd22c42019-04-01 15:18:53 +0300677 * version of the test if CRLs are in use. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100678 if (crl_file == NULL || strcmp(crl_file, "") == 0) {
Hanno Becker0350d562019-03-28 14:23:36 +0000679 flags = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100681 res = mbedtls_x509_crt_verify_with_ca_cb(&crt,
682 ca_callback,
683 &ca,
684 profile,
685 cn_name,
686 &flags,
687 f_vrfy,
688 NULL);
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100690 TEST_ASSERT(res == (result));
691 TEST_ASSERT(flags == (uint32_t) (flags_result));
Hanno Becker0350d562019-03-28 14:23:36 +0000692 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200693#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200694exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100695 mbedtls_x509_crt_free(&crt);
696 mbedtls_x509_crt_free(&ca);
697 mbedtls_x509_crl_free(&crl);
698 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000699}
Paul Bakker33b43f12013-08-20 11:48:36 +0200700/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000701
Jarno Lamsa557426a2019-03-27 17:08:29 +0200702/* 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 +0100703void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
704 int exp_ret)
Jarno Lamsa557426a2019-03-27 17:08:29 +0200705{
706 int ret;
707 mbedtls_x509_crt crt;
708 mbedtls_x509_crt ca;
709 uint32_t flags = 0;
Hanno Becker3f932bb2019-03-28 17:06:47 +0000710
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100711 mbedtls_x509_crt_init(&crt);
712 mbedtls_x509_crt_init(&ca);
Valerio Settie7373a82023-04-19 14:53:36 +0200713 USE_PSA_INIT();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200714
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100715 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
716 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200717
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100718 if (strcmp(name, "NULL") == 0) {
Jarno Lamsa557426a2019-03-27 17:08:29 +0200719 name = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100720 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000721
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100722 ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca,
723 &compat_profile, name, &flags,
724 NULL, NULL);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100726 TEST_ASSERT(ret == exp_ret);
727 TEST_ASSERT(flags == (uint32_t) (-1));
Jarno Lamsa557426a2019-03-27 17:08:29 +0200728exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100729 mbedtls_x509_crt_free(&crt);
730 mbedtls_x509_crt_free(&ca);
Valerio Settie7373a82023-04-19 14:53:36 +0200731 USE_PSA_DONE();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200732}
733/* END_CASE */
734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100736void x509_verify_callback(char *crt_file, char *ca_file, char *name,
737 int exp_ret, char *exp_vrfy_out)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200738{
739 int ret;
740 mbedtls_x509_crt crt;
741 mbedtls_x509_crt ca;
742 uint32_t flags = 0;
743 verify_print_context vrfy_ctx;
744
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100745 mbedtls_x509_crt_init(&crt);
746 mbedtls_x509_crt_init(&ca);
747 verify_print_init(&vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200748
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100749 USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +0100750
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100751 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
752 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200753
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100754 if (strcmp(name, "NULL") == 0) {
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200755 name = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100756 }
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100758 ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL,
759 &compat_profile,
760 name, &flags,
761 verify_print, &vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200762
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100763 TEST_ASSERT(ret == exp_ret);
764 TEST_ASSERT(strcmp(vrfy_ctx.buf, exp_vrfy_out) == 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200765
766exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100767 mbedtls_x509_crt_free(&crt);
768 mbedtls_x509_crt_free(&ca);
769 USE_PSA_DONE();
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200770}
771/* END_CASE */
772
773/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100774void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000775{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000777 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200778 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000779
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100780 mbedtls_x509_crt_init(&crt);
781 memset(buf, 0, 2000);
Valerio Settie7373a82023-04-19 14:53:36 +0200782 USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000783
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100784 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
785 if (strcmp(entity, "subject") == 0) {
786 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
787 } else if (strcmp(entity, "issuer") == 0) {
788 res = mbedtls_x509_dn_gets(buf, 2000, &crt.issuer);
789 } else {
790 TEST_ASSERT("Unknown entity" == 0);
791 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000792
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100793 TEST_ASSERT(res != -1);
794 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000795
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100796 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200797
798exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100799 mbedtls_x509_crt_free(&crt);
Valerio Settie7373a82023-04-19 14:53:36 +0200800 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000801}
Paul Bakker33b43f12013-08-20 11:48:36 +0200802/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000803
Werner Lewis9a2356b2022-06-17 15:51:55 +0100804/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100805void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
806 char *new_subject_ou,
807 char *result_str,
808 int ret)
Werner Lewis9a2356b2022-06-17 15:51:55 +0100809{
810 mbedtls_x509_crt crt;
811 char buf[2000];
812 int res = 0;
813
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100814 mbedtls_x509_crt_init(&crt);
815 memset(buf, 0, 2000);
Valerio Settie7373a82023-04-19 14:53:36 +0200816 USE_PSA_INIT();
Werner Lewis9a2356b2022-06-17 15:51:55 +0100817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100818 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Werner Lewis9a2356b2022-06-17 15:51:55 +0100819 crt.subject.next->val.p = (unsigned char *) new_subject_ou;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100820 crt.subject.next->val.len = strlen(new_subject_ou);
Werner Lewis9a2356b2022-06-17 15:51:55 +0100821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100822 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
Werner Lewis9a2356b2022-06-17 15:51:55 +0100823
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100824 if (ret != 0) {
825 TEST_ASSERT(res == ret);
826 } else {
827 TEST_ASSERT(res != -1);
828 TEST_ASSERT(res != -2);
829 TEST_ASSERT(strcmp(buf, result_str) == 0);
Werner Lewis9a2356b2022-06-17 15:51:55 +0100830 }
831exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100832 mbedtls_x509_crt_free(&crt);
Valerio Settie7373a82023-04-19 14:53:36 +0200833 USE_PSA_DONE();
Werner Lewis9a2356b2022-06-17 15:51:55 +0100834}
835/* END_CASE */
836
David Horstmann77ecc6e2022-10-05 13:08:45 +0100837/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100838void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
David Horstmann77ecc6e2022-10-05 13:08:45 +0100839{
valerio5346ef52023-04-20 14:48:19 +0200840 unsigned char *name = NULL;
David Horstmann77ecc6e2022-10-05 13:08:45 +0100841 unsigned char *p;
842 size_t name_len;
David Horstmann6c4226c2022-10-20 10:18:37 +0100843 mbedtls_x509_name head;
David Horstmann77ecc6e2022-10-05 13:08:45 +0100844 mbedtls_x509_name *allocated, *prev;
David Horstmann4a67c352022-10-17 17:59:10 +0100845 int ret;
David Horstmann77ecc6e2022-10-05 13:08:45 +0100846
Valerio Settie7373a82023-04-19 14:53:36 +0200847 USE_PSA_INIT();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100848 memset(&head, 0, sizeof(head));
David Horstmann6c4226c2022-10-20 10:18:37 +0100849
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100850 name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
David Horstmann77ecc6e2022-10-05 13:08:45 +0100851 p = name;
852
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100853 ret = mbedtls_x509_get_name(&p, (name + name_len), &head);
854 if (ret == 0) {
David Horstmann77ecc6e2022-10-05 13:08:45 +0100855 allocated = head.next;
David Horstmann77ecc6e2022-10-05 13:08:45 +0100856
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100857 while (allocated != NULL) {
David Horstmann77ecc6e2022-10-05 13:08:45 +0100858 prev = allocated;
859 allocated = allocated->next;
860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100861 mbedtls_free(prev);
David Horstmann77ecc6e2022-10-05 13:08:45 +0100862 }
863 }
864
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100865 TEST_EQUAL(ret, exp_ret);
David Horstmann77ecc6e2022-10-05 13:08:45 +0100866
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100867 mbedtls_free(name);
Valerio Settie7373a82023-04-19 14:53:36 +0200868
869exit:
870 USE_PSA_DONE();
David Horstmann77ecc6e2022-10-05 13:08:45 +0100871}
872/* END_CASE */
873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100875void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000876{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100879 mbedtls_x509_crt_init(&crt);
Valerio Settie7373a82023-04-19 14:53:36 +0200880 USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100882 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100884 if (strcmp(entity, "valid_from") == 0) {
885 TEST_ASSERT(mbedtls_x509_time_is_past(&crt.valid_from) == result);
886 } else if (strcmp(entity, "valid_to") == 0) {
887 TEST_ASSERT(mbedtls_x509_time_is_past(&crt.valid_to) == result);
888 } else {
889 TEST_ASSERT("Unknown entity" == 0);
890 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000891
Paul Bakkerbd51b262014-07-10 15:26:12 +0200892exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100893 mbedtls_x509_crt_free(&crt);
Valerio Settie7373a82023-04-19 14:53:36 +0200894 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000895}
Paul Bakker33b43f12013-08-20 11:48:36 +0200896/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100899void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100900{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100902
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100903 mbedtls_x509_crt_init(&crt);
Valerio Settie7373a82023-04-19 14:53:36 +0200904 USE_PSA_INIT();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100905
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100906 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100907
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100908 if (strcmp(entity, "valid_from") == 0) {
909 TEST_ASSERT(mbedtls_x509_time_is_future(&crt.valid_from) == result);
910 } else if (strcmp(entity, "valid_to") == 0) {
911 TEST_ASSERT(mbedtls_x509_time_is_future(&crt.valid_to) == result);
912 } else {
913 TEST_ASSERT("Unknown entity" == 0);
914 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100915
Paul Bakkerbd51b262014-07-10 15:26:12 +0200916exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100917 mbedtls_x509_crt_free(&crt);
Valerio Settie7373a82023-04-19 14:53:36 +0200918 USE_PSA_DONE();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100919}
920/* END_CASE */
921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100923void x509parse_crt_file(char *crt_file, int result)
Paul Bakker5a5fa922014-09-26 14:53:04 +0200924{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +0200926
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100927 mbedtls_x509_crt_init(&crt);
Valerio Settie7373a82023-04-19 14:53:36 +0200928 USE_PSA_INIT();
Paul Bakker5a5fa922014-09-26 14:53:04 +0200929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100930 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == result);
Paul Bakker5a5fa922014-09-26 14:53:04 +0200931
932exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100933 mbedtls_x509_crt_free(&crt);
Valerio Settie7373a82023-04-19 14:53:36 +0200934 USE_PSA_DONE();
Paul Bakker5a5fa922014-09-26 14:53:04 +0200935}
936/* END_CASE */
937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100939void x509parse_crt(data_t *buf, char *result_str, int result)
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000940{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 mbedtls_x509_crt crt;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000942 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100943 int res;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000944
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100945 mbedtls_x509_crt_init(&crt);
946 memset(output, 0, 2000);
Valerio Settie7373a82023-04-19 14:53:36 +0200947 USE_PSA_INIT();
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000948
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100949 TEST_ASSERT(mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len) == (result));
950 if ((result) == 0) {
951 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000952
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100953 TEST_ASSERT(res != -1);
954 TEST_ASSERT(res != -2);
Hanno Becker2d8a2c02019-01-31 09:15:53 +0000955
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100956 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Hanno Becker2d8a2c02019-01-31 09:15:53 +0000957 }
958
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100959 mbedtls_x509_crt_free(&crt);
960 mbedtls_x509_crt_init(&crt);
961 memset(output, 0, 2000);
Hanno Becker2d8a2c02019-01-31 09:15:53 +0000962
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100963 TEST_ASSERT(mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len) == (result));
964 if ((result) == 0) {
965 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakker33b43f12013-08-20 11:48:36 +0200966
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100967 TEST_ASSERT(res != -1);
968 TEST_ASSERT(res != -2);
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000969
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100970 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000971 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000972
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100973 mbedtls_x509_crt_free(&crt);
974 mbedtls_x509_crt_init(&crt);
975 memset(output, 0, 2000);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200976
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100977 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL,
978 NULL) == (result));
979 if ((result) == 0) {
980 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200981
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100982 TEST_ASSERT(res != -1);
983 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200984
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100985 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200986 }
987
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100988 mbedtls_x509_crt_free(&crt);
989 mbedtls_x509_crt_init(&crt);
990 memset(output, 0, 2000);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200991
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100992 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL,
993 NULL) == (result));
994 if ((result) == 0) {
995 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200996
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100997 TEST_ASSERT(res != -1);
998 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200999
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001000 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001001 }
1002
1003exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001004 mbedtls_x509_crt_free(&crt);
Valerio Settie7373a82023-04-19 14:53:36 +02001005 USE_PSA_DONE();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001006}
1007/* END_CASE */
1008
1009/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001010void x509parse_crt_cb(data_t *buf, char *result_str, int result)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001011{
1012 mbedtls_x509_crt crt;
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001013 mbedtls_x509_buf oid;
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001014 unsigned char output[2000];
1015 int res;
1016
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001017 oid.tag = MBEDTLS_ASN1_OID;
1018 oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001019 oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001020
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001021 mbedtls_x509_crt_init(&crt);
1022 memset(output, 0, 2000);
Valerio Settie7373a82023-04-19 14:53:36 +02001023 USE_PSA_INIT();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001024
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001025 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
1026 &oid) == (result));
1027 if ((result) == 0) {
1028 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001029
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001030 TEST_ASSERT(res != -1);
1031 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001032
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001033 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001034 }
1035
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001036 mbedtls_x509_crt_free(&crt);
1037 mbedtls_x509_crt_init(&crt);
1038 memset(output, 0, 2000);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001040 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1041 &oid) == (result));
1042 if ((result) == 0) {
1043 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001045 TEST_ASSERT(res != -1);
1046 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001048 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001049 }
1050
Paul Bakkerbd51b262014-07-10 15:26:12 +02001051exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001052 mbedtls_x509_crt_free(&crt);
Valerio Settie7373a82023-04-19 14:53:36 +02001053 USE_PSA_DONE();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001054}
Paul Bakker33b43f12013-08-20 11:48:36 +02001055/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001058void x509parse_crl(data_t *buf, char *result_str, int result)
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001061 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +01001062 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001063
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001064 mbedtls_x509_crl_init(&crl);
1065 memset(output, 0, 2000);
Valerio Settie7373a82023-04-19 14:53:36 +02001066 USE_PSA_INIT();
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001067
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001068 TEST_ASSERT(mbedtls_x509_crl_parse(&crl, buf->x, buf->len) == (result));
1069 if ((result) == 0) {
1070 res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
Paul Bakker33b43f12013-08-20 11:48:36 +02001071
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001072 TEST_ASSERT(res != -1);
1073 TEST_ASSERT(res != -2);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001074
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001075 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001076 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001077
Paul Bakkerbd51b262014-07-10 15:26:12 +02001078exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001079 mbedtls_x509_crl_free(&crl);
Valerio Settie7373a82023-04-19 14:53:36 +02001080 USE_PSA_DONE();
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001081}
Paul Bakker33b43f12013-08-20 11:48:36 +02001082/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001085void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001086{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001088 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001089 int my_ret;
1090
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001091 mbedtls_x509_csr_init(&csr);
1092 memset(my_out, 0, sizeof(my_out));
Valerio Settie7373a82023-04-19 14:53:36 +02001093 USE_PSA_INIT();
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001095 my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
1096 TEST_ASSERT(my_ret == ref_ret);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001097
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001098 if (ref_ret == 0) {
1099 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1100 TEST_ASSERT(my_out_len == strlen(ref_out));
1101 TEST_ASSERT(strcmp(my_out, ref_out) == 0);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001102 }
1103
Paul Bakkerbd51b262014-07-10 15:26:12 +02001104exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001105 mbedtls_x509_csr_free(&csr);
Valerio Settie7373a82023-04-19 14:53:36 +02001106 USE_PSA_DONE();
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001107}
1108/* END_CASE */
1109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001111void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001112{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001114 int i;
1115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001116 mbedtls_x509_crt_init(&chain);
Valerio Settie7373a82023-04-19 14:53:36 +02001117 USE_PSA_INIT();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001119 TEST_ASSERT(mbedtls_x509_crt_parse_path(&chain, crt_path) == ret);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001120
1121 /* Check how many certs we got */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001122 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1123 if (cur->raw.p != NULL) {
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001124 i++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001125 }
1126 }
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001128 TEST_ASSERT(i == nb_crt);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001129
Paul Bakkerbd51b262014-07-10 15:26:12 +02001130exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001131 mbedtls_x509_crt_free(&chain);
Valerio Settie7373a82023-04-19 14:53:36 +02001132 USE_PSA_DONE();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001133}
1134/* END_CASE */
1135
Janos Follath822b2c32015-10-11 10:25:22 +02001136/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001137void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1138 int ret_chk, int flags_chk)
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001139{
1140 char file_buf[128];
1141 int ret;
1142 uint32_t flags;
1143 mbedtls_x509_crt trusted, chain;
1144
1145 /*
1146 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1147 * with NN.crt signed by NN-1.crt
1148 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001149 mbedtls_x509_crt_init(&trusted);
1150 mbedtls_x509_crt_init(&chain);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001151 USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +01001152
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001153 /* Load trusted root */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001154 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, ca_file) == 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001155
1156 /* Load a chain with nb_int intermediates (from 01 to nb_int),
1157 * plus one "end-entity" cert (nb_int + 1) */
Dave Rodgman18688702023-02-02 12:40:50 +00001158 ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001159 nb_int + 1);
Dave Rodgman18688702023-02-02 12:40:50 +00001160 TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001161 TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, file_buf) == 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001162
1163 /* Try to verify that chain */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001164 ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1165 NULL, NULL);
1166 TEST_ASSERT(ret == ret_chk);
1167 TEST_ASSERT(flags == (uint32_t) flags_chk);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001168
1169exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001170 mbedtls_x509_crt_free(&chain);
1171 mbedtls_x509_crt_free(&trusted);
1172 USE_PSA_DONE();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001173}
1174/* END_CASE */
1175
1176/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001177void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1178 int flags_result, int result,
1179 char *profile_name, int vrfy_fatal_lvls)
Janos Follath822b2c32015-10-11 10:25:22 +02001180{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001181 char *act;
Janos Follath822b2c32015-10-11 10:25:22 +02001182 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001183 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001184 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001185 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +02001186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001187 mbedtls_x509_crt_init(&chain);
1188 mbedtls_x509_crt_init(&trusted);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001189 USE_PSA_INIT();
Gilles Peskine9de97e22021-02-02 21:00:11 +01001190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001191 while ((act = mystrsep(&chain_paths, " ")) != NULL) {
1192 TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, act) == 0);
1193 }
1194 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, trusted_ca) == 0);
Janos Follath822b2c32015-10-11 10:25:22 +02001195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001196 if (strcmp(profile_name, "") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001197 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001198 } else if (strcmp(profile_name, "next") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001199 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001200 } else if (strcmp(profile_name, "suiteb") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001201 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001202 } else if (strcmp(profile_name, "rsa3072") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001203 profile = &profile_rsa3072;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001204 } else if (strcmp(profile_name, "sha512") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001205 profile = &profile_sha512;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001206 }
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001208 res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1209 NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
Janos Follathef4f2582015-10-11 16:17:27 +02001210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001211 TEST_ASSERT(res == (result));
1212 TEST_ASSERT(flags == (uint32_t) (flags_result));
Janos Follath822b2c32015-10-11 10:25:22 +02001213
1214exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001215 mbedtls_x509_crt_free(&trusted);
1216 mbedtls_x509_crt_free(&chain);
1217 USE_PSA_DONE();
Janos Follath822b2c32015-10-11 10:25:22 +02001218}
1219/* END_CASE */
1220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001222void x509_oid_desc(data_t *buf, char *ref_desc)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001223{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001225 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001226 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001227
Valerio Settie7373a82023-04-19 14:53:36 +02001228 USE_PSA_INIT();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001230 oid.p = buf->x;
1231 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001233 ret = mbedtls_oid_get_extended_key_usage(&oid, &desc);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001235 if (strcmp(ref_desc, "notfound") == 0) {
1236 TEST_ASSERT(ret != 0);
1237 TEST_ASSERT(desc == NULL);
1238 } else {
1239 TEST_ASSERT(ret == 0);
1240 TEST_ASSERT(desc != NULL);
1241 TEST_ASSERT(strcmp(desc, ref_desc) == 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001242 }
Valerio Settie7373a82023-04-19 14:53:36 +02001243
1244exit:
1245 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001246}
1247/* END_CASE */
1248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001250void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001251{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001253 char num_buf[100];
1254
Valerio Settie7373a82023-04-19 14:53:36 +02001255 USE_PSA_INIT();
Dave Rodgman18688702023-02-02 12:40:50 +00001256 memset(num_buf, 0x2a, sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001259 oid.p = oid_buf->x;
1260 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001261
Dave Rodgman18688702023-02-02 12:40:50 +00001262 TEST_ASSERT((size_t) blen <= sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001264 TEST_ASSERT(mbedtls_oid_get_numeric_string(num_buf, blen, &oid) == ret);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001266 if (ret >= 0) {
1267 TEST_ASSERT(num_buf[ret] == 0);
1268 TEST_ASSERT(strcmp(num_buf, numstr) == 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001269 }
Valerio Settie7373a82023-04-19 14:53:36 +02001270
1271exit:
1272 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001273}
1274/* END_CASE */
1275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_KEY_USAGE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001277void x509_check_key_usage(char *crt_file, int usage, int ret)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001278{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001281 mbedtls_x509_crt_init(&crt);
Valerio Settie7373a82023-04-19 14:53:36 +02001282 USE_PSA_INIT();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001283
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001284 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001286 TEST_ASSERT(mbedtls_x509_crt_check_key_usage(&crt, usage) == ret);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001287
Paul Bakkerbd51b262014-07-10 15:26:12 +02001288exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001289 mbedtls_x509_crt_free(&crt);
Valerio Settie7373a82023-04-19 14:53:36 +02001290 USE_PSA_DONE();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001291}
1292/* END_CASE */
1293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294/* 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 +01001295void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1296 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001297{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001299
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001300 mbedtls_x509_crt_init(&crt);
Valerio Settie7373a82023-04-19 14:53:36 +02001301 USE_PSA_INIT();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001302
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001303 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001305 TEST_ASSERT(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x,
1306 oid->len) == ret);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001307
Paul Bakkerbd51b262014-07-10 15:26:12 +02001308exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001309 mbedtls_x509_crt_free(&crt);
Valerio Settie7373a82023-04-19 14:53:36 +02001310 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001311}
1312/* END_CASE */
1313
Andres AG4b76aec2016-09-23 13:16:02 +01001314/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001315void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1316 int day, int hour, int min, int sec)
Andres AG4b76aec2016-09-23 13:16:02 +01001317{
1318 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001319 unsigned char buf[21];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001320 unsigned char *start = buf;
1321 unsigned char *end = buf;
Andres AG4b76aec2016-09-23 13:16:02 +01001322
Valerio Settie7373a82023-04-19 14:53:36 +02001323 USE_PSA_INIT();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001324 memset(&time, 0x00, sizeof(time));
1325 *end = (unsigned char) tag; end++;
1326 *end = strlen(time_str);
1327 TEST_ASSERT(*end < 20);
Andres AG4b76aec2016-09-23 13:16:02 +01001328 end++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001329 memcpy(end, time_str, (size_t) *(end - 1));
Andres AG4b76aec2016-09-23 13:16:02 +01001330 end += *(end - 1);
1331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001332 TEST_ASSERT(mbedtls_x509_get_time(&start, end, &time) == ret);
1333 if (ret == 0) {
1334 TEST_ASSERT(year == time.year);
1335 TEST_ASSERT(mon == time.mon);
1336 TEST_ASSERT(day == time.day);
1337 TEST_ASSERT(hour == time.hour);
1338 TEST_ASSERT(min == time.min);
1339 TEST_ASSERT(sec == time.sec);
Andres AG4b76aec2016-09-23 13:16:02 +01001340 }
Valerio Settie7373a82023-04-19 14:53:36 +02001341
1342exit:
1343 USE_PSA_DONE();
Andres AG4b76aec2016-09-23 13:16:02 +01001344}
1345/* END_CASE */
1346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001348void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1349 int ref_msg_md, int ref_mgf_md,
1350 int ref_salt_len, int ref_ret)
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001351{
1352 int my_ret;
Ronald Cronac6ae352020-06-26 14:33:03 +02001353 mbedtls_x509_buf buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001355 int my_salt_len;
1356
Valerio Settie7373a82023-04-19 14:53:36 +02001357 USE_PSA_INIT();
1358
Ronald Cronac6ae352020-06-26 14:33:03 +02001359 buf.p = params->x;
1360 buf.len = params->len;
1361 buf.tag = params_tag;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001363 my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1364 &my_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001366 TEST_ASSERT(my_ret == ref_ret);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001368 if (ref_ret == 0) {
1369 TEST_ASSERT(my_msg_md == (mbedtls_md_type_t) ref_msg_md);
1370 TEST_ASSERT(my_mgf_md == (mbedtls_md_type_t) ref_mgf_md);
1371 TEST_ASSERT(my_salt_len == ref_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001372 }
1373
Paul Bakkerbd51b262014-07-10 15:26:12 +02001374exit:
Valerio Settie7373a82023-04-19 14:53:36 +02001375 USE_PSA_DONE();
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001376}
1377/* END_CASE */
1378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001380void x509_selftest()
Paul Bakker37940d9f2009-07-10 22:38:58 +00001381{
Valerio Settie7373a82023-04-19 14:53:36 +02001382 USE_PSA_INIT();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001383 TEST_ASSERT(mbedtls_x509_self_test(1) == 0);
Valerio Settie7373a82023-04-19 14:53:36 +02001384
1385exit:
1386 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +00001387}
Paul Bakker33b43f12013-08-20 11:48:36 +02001388/* END_CASE */