blob: c2a2f556d24a1a94bb4b241a31018fe7e96e448b [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Azim Khand30ca132017-06-09 04:32:58 +01002#include "mbedtls/bignum.h"
Andres AG4b76aec2016-09-23 13:16:02 +01003#include "mbedtls/x509.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00004#include "mbedtls/x509_crt.h"
5#include "mbedtls/x509_crl.h"
6#include "mbedtls/x509_csr.h"
7#include "mbedtls/pem.h"
8#include "mbedtls/oid.h"
9#include "mbedtls/base64.h"
Chris Jones9f7a6932021-04-14 12:12:09 +010010#include "mbedtls/error.h"
Valerio Setti178b5bd2023-02-13 10:04:28 +010011#include "mbedtls/pk.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +010012#include "string.h"
Paul Bakkerb63b0af2011-01-13 17:54:59 +000013
Simon Butcher9e24b512017-07-28 12:15:13 +010014#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
Hanno Becker3b1422e2017-07-26 13:38:02 +010015#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
Gilles Peskine449bd832023-01-11 14:50:10 +010016 than the current threshold 19. To test larger values, please \
17 adapt the script tests/data_files/dir-max/long.sh."
Hanno Becker3b1422e2017-07-26 13:38:02 +010018#endif
19
Hanno Becker20a4ade2019-06-03 14:27:03 +010020/* Test-only profile allowing all digests, PK algorithms, and curves. */
21const mbedtls_x509_crt_profile profile_all =
22{
23 0xFFFFFFFF, /* Any MD */
24 0xFFFFFFFF, /* Any PK alg */
25 0xFFFFFFFF, /* Any curve */
26 1024,
27};
28
Gilles Peskineef86ab22017-05-05 18:59:02 +020029/* Profile for backward compatibility. Allows SHA-1, unlike the default
30 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020031const mbedtls_x509_crt_profile compat_profile =
32{
Gilles Peskine449bd832023-01-11 14:50:10 +010033 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) |
34 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160) |
35 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
36 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
37 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
38 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubney28015e12022-04-21 08:12:59 +010039 0xFFFFFFFF, /* Any PK alg */
40 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020041 1024,
42};
43
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020044const mbedtls_x509_crt_profile profile_rsa3072 =
45{
Gilles Peskine449bd832023-01-11 14:50:10 +010046 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
47 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
48 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
49 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_RSA),
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020050 0,
51 3072,
52};
53
54const mbedtls_x509_crt_profile profile_sha512 =
55{
Gilles Peskine449bd832023-01-11 14:50:10 +010056 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubney28015e12022-04-21 08:12:59 +010057 0xFFFFFFFF, /* Any PK alg */
58 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020059 1024,
60};
61
Gilles Peskine449bd832023-01-11 14:50:10 +010062int verify_none(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000063{
Paul Bakker5a624082011-01-18 16:31:52 +000064 ((void) data);
65 ((void) crt);
66 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010067 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020068
Paul Bakker915275b2012-09-28 07:10:55 +000069 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000070}
71
Gilles Peskine449bd832023-01-11 14:50:10 +010072int verify_all(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000073{
Paul Bakker5a624082011-01-18 16:31:52 +000074 ((void) data);
75 ((void) crt);
76 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000077 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000078
Paul Bakkerb63b0af2011-01-13 17:54:59 +000079 return 0;
80}
81
Jarno Lamsa03cd1202019-03-27 15:45:04 +020082#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +010083int ca_callback_fail(void *data, mbedtls_x509_crt const *child, mbedtls_x509_crt **candidates)
Jarno Lamsa557426a2019-03-27 17:08:29 +020084{
85 ((void) data);
86 ((void) child);
87 ((void) candidates);
88
89 return -1;
90}
Przemek Stekielcd204992022-04-27 15:33:43 +020091#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010092int ca_callback(void *data, mbedtls_x509_crt const *child,
93 mbedtls_x509_crt **candidates)
Jarno Lamsa03cd1202019-03-27 15:45:04 +020094{
Hanno Beckercbb59032019-03-28 14:14:22 +000095 int ret = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +020096 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +000097 mbedtls_x509_crt *first;
98
99 /* This is a test-only implementation of the CA callback
100 * which always returns the entire list of trusted certificates.
101 * Production implementations managing a large number of CAs
102 * should use an efficient presentation and lookup for the
103 * set of trusted certificates (such as a hashtable) and only
104 * return those trusted certificates which satisfy basic
105 * parental checks, such as the matching of child `Issuer`
106 * and parent `Subject` field. */
107 ((void) child);
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
110 if (first == NULL) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000111 ret = -1;
112 goto exit;
113 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 mbedtls_x509_crt_init(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000117 ret = -1;
118 goto exit;
119 }
120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 while (ca->next != NULL) {
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200122 ca = ca->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000124 ret = -1;
125 goto exit;
126 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200127 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000128
129exit:
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 if (ret != 0) {
132 mbedtls_x509_crt_free(first);
133 mbedtls_free(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000134 first = NULL;
135 }
136
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200137 *candidates = first;
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 return ret;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200139}
Przemek Stekielcd204992022-04-27 15:33:43 +0200140#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200141#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143int verify_fatal(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200144{
145 int *levels = (int *) data;
146
147 ((void) crt);
148 ((void) certificate_depth);
149
150 /* Simulate a fatal error in the callback */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 if (*levels & (1 << certificate_depth)) {
152 *flags |= (1 << certificate_depth);
153 return -1 - certificate_depth;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200154 }
155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return 0;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200157}
158
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900159/* strsep() not available on Windows */
160char *mystrsep(char **stringp, const char *delim)
161{
162 const char *p;
163 char *ret = *stringp;
164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if (*stringp == NULL) {
166 return NULL;
167 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 for (;; (*stringp)++) {
170 if (**stringp == '\0') {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900171 *stringp = NULL;
172 goto done;
173 }
174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 for (p = delim; *p != '\0'; p++) {
176 if (**stringp == *p) {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900177 **stringp = '\0';
178 (*stringp)++;
179 goto done;
180 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900182 }
183
184done:
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return ret;
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900186}
187
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200188#if defined(MBEDTLS_X509_CRT_PARSE_C)
189typedef struct {
190 char buf[512];
191 char *p;
192} verify_print_context;
193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194void verify_print_init(verify_print_context *ctx)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200195{
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 memset(ctx, 0, sizeof(verify_print_context));
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200197 ctx->p = ctx->buf;
198}
199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200int verify_print(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200201{
202 int ret;
203 verify_print_context *ctx = (verify_print_context *) data;
204 char *p = ctx->p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 size_t n = ctx->buf + sizeof(ctx->buf) - ctx->p;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200206 ((void) flags);
207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 ret = mbedtls_snprintf(p, n, "depth %d - serial ", certificate_depth);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200209 MBEDTLS_X509_SAFE_SNPRINTF;
210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200212 MBEDTLS_X509_SAFE_SNPRINTF;
213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 ret = mbedtls_snprintf(p, n, " - subject ");
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200215 MBEDTLS_X509_SAFE_SNPRINTF;
216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200218 MBEDTLS_X509_SAFE_SNPRINTF;
219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 ret = mbedtls_snprintf(p, n, " - flags 0x%08x\n", *flags);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200221 MBEDTLS_X509_SAFE_SNPRINTF;
222
223 ctx->p = p;
224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return 0;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200226}
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228int verify_parse_san(mbedtls_x509_subject_alternative_name *san,
229 char **buf, size_t *size)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200230{
231 int ret;
232 size_t i;
233 char *p = *buf;
234 size_t n = *size;
235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 ret = mbedtls_snprintf(p, n, "type : %d", san->type);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200237 MBEDTLS_X509_SAFE_SNPRINTF;
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 switch (san->type) {
240 case (MBEDTLS_X509_SAN_OTHER_NAME):
241 ret = mbedtls_snprintf(p, n, "\notherName :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300242 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
David Horstmanncfae6a12023-08-18 19:12:59 +0100245 &san->san.other_name.type_id) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 ret = mbedtls_snprintf(p, n, " hardware module name :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300247 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 ret = mbedtls_snprintf(p, n, " hardware type : ");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300249 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 ret = mbedtls_oid_get_numeric_string(p,
252 n,
Matthias Schulzedc32ea2023-10-19 16:09:08 +0200253 &san->san.other_name.value.hardware_module_name
254 .oid);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300255 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 ret = mbedtls_snprintf(p, n, ", hardware serial number : ");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300258 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 for (i = 0; i < san->san.other_name.value.hardware_module_name.val.len; i++) {
261 ret = mbedtls_snprintf(p,
262 n,
263 "%02X",
264 san->san.other_name.value.hardware_module_name.val.p[i]);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300265 MBEDTLS_X509_SAFE_SNPRINTF;
266 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200267 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 break;/* MBEDTLS_OID_ON_HW_MODULE_NAME */
269 case (MBEDTLS_X509_SAN_DNS_NAME):
270 ret = mbedtls_snprintf(p, n, "\ndNSName : ");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200271 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (san->san.unstructured_name.len >= n) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200273 *p = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200275 }
276 n -= san->san.unstructured_name.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 for (i = 0; i < san->san.unstructured_name.len; i++) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200278 *p++ = san->san.unstructured_name.p[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 }
280 break;/* MBEDTLS_X509_SAN_DNS_NAME */
Przemek Stekiel608e3ef2023-02-09 14:47:50 +0100281 case (MBEDTLS_X509_SAN_RFC822_NAME):
282 ret = mbedtls_snprintf(p, n, "\nrfc822Name : ");
283 MBEDTLS_X509_SAFE_SNPRINTF;
284 if (san->san.unstructured_name.len >= n) {
285 *p = '\0';
286 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
287 }
288 n -= san->san.unstructured_name.len;
289 for (i = 0; i < san->san.unstructured_name.len; i++) {
290 *p++ = san->san.unstructured_name.p[i];
291 }
292 break;/* MBEDTLS_X509_SAN_RFC822_NAME */
Andrzej Kureke12b01d2023-01-10 06:47:38 -0500293 case (MBEDTLS_X509_SAN_DIRECTORY_NAME):
294 ret = mbedtls_snprintf(p, n, "\ndirectoryName : ");
295 MBEDTLS_X509_SAFE_SNPRINTF;
296 ret = mbedtls_x509_dn_gets(p, n, &san->san.directory_name);
297 if (ret < 0) {
298 return ret;
299 }
300
301 p += ret;
302 n -= ret;
303 break;/* MBEDTLS_X509_SAN_DIRECTORY_NAME */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200304 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 /*
306 * Should not happen.
307 */
308 return -1;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200309 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 ret = mbedtls_snprintf(p, n, "\n");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200311 MBEDTLS_X509_SAFE_SNPRINTF;
312
313 *size = n;
314 *buf = p;
315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200317}
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319int parse_crt_ext_cb(void *p_ctx, mbedtls_x509_crt const *crt, mbedtls_x509_buf const *oid,
320 int critical, const unsigned char *cp, const unsigned char *end)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200321{
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 (void) crt;
323 (void) critical;
324 mbedtls_x509_buf *new_oid = (mbedtls_x509_buf *) p_ctx;
325 if (oid->tag == MBEDTLS_ASN1_OID &&
326 MBEDTLS_OID_CMP(MBEDTLS_OID_CERTIFICATE_POLICIES, oid) == 0) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200327 /* Handle unknown certificate policy */
328 int ret, parse_ret = 0;
329 size_t len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 unsigned char **p = (unsigned char **) &cp;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200331
332 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 ret = mbedtls_asn1_get_tag(p, end, &len,
334 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
335 if (ret != 0) {
336 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
337 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (*p + len != end) {
340 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
341 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
342 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200343
344 /*
345 * Cannot be an empty sequence.
346 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (len == 0) {
348 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
349 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
350 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 while (*p < end) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200353 const unsigned char *policy_end;
354
355 /*
356 * Get the policy sequence
357 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
359 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
360 0) {
361 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
362 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200363
364 policy_end = *p + len;
365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
367 MBEDTLS_ASN1_OID)) != 0) {
368 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
369 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200370
Nicola Di Lietob77fad82020-06-17 17:57:36 +0200371 /*
372 * Recognize exclusively the policy with OID 1
373 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if (len != 1 || *p[0] != 1) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200375 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200377
378 *p += len;
379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 /*
381 * If there is an optional qualifier, then *p < policy_end
382 * Check the Qualifier len to verify it doesn't exceed policy_end.
383 */
384 if (*p < policy_end) {
385 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
386 MBEDTLS_ASN1_CONSTRUCTED |
387 MBEDTLS_ASN1_SEQUENCE)) != 0) {
388 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
389 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200390 /*
391 * Skip the optional policy qualifiers.
392 */
393 *p += len;
394 }
395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if (*p != policy_end) {
397 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
398 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
399 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200400 }
401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if (*p != end) {
403 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
404 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
405 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 return parse_ret;
408 } else if (new_oid != NULL && new_oid->tag == oid->tag && new_oid->len == oid->len &&
409 memcmp(new_oid->p, oid->p, oid->len) == 0) {
410 return 0;
411 } else {
412 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
413 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200414 }
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200415}
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200416#endif /* MBEDTLS_X509_CRT_PARSE_C */
Matthias Schulzab408222023-10-18 13:20:59 +0200417
Matthias Schulz03bd0952023-10-19 09:52:59 +0200418#if defined(MBEDTLS_X509_CSR_PARSE_C)
Matthias Schulzab408222023-10-18 13:20:59 +0200419int parse_csr_ext_accept_cb(void *p_ctx, mbedtls_x509_csr const *csr, mbedtls_x509_buf const *oid,
Matthias Schulzedc32ea2023-10-19 16:09:08 +0200420 int critical, const unsigned char *cp, const unsigned char *end)
Matthias Schulzab408222023-10-18 13:20:59 +0200421{
422 (void) p_ctx;
423 (void) csr;
424 (void) oid;
425 (void) critical;
426 (void) cp;
427 (void) end;
428
429 return 0;
430}
431
432int parse_csr_ext_reject_cb(void *p_ctx, mbedtls_x509_csr const *csr, mbedtls_x509_buf const *oid,
Matthias Schulzedc32ea2023-10-19 16:09:08 +0200433 int critical, const unsigned char *cp, const unsigned char *end)
Matthias Schulzab408222023-10-18 13:20:59 +0200434{
435 (void) p_ctx;
436 (void) csr;
437 (void) oid;
438 (void) critical;
439 (void) cp;
440 (void) end;
441
442 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
443 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
444}
Matthias Schulz03bd0952023-10-19 09:52:59 +0200445#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200446/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000447
Thomas Daubney5c9c2ce2022-06-06 16:36:43 +0100448/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100449void x509_accessor_ext_types(int ext_type, int has_ext_type)
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100450{
451 mbedtls_x509_crt crt;
452 int expected_result = ext_type & has_ext_type;
453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200455 USE_PSA_INIT();
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100456
457 crt.ext_types = ext_type;
458
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500459 TEST_EQUAL(mbedtls_x509_crt_has_ext_type(&crt, has_ext_type), expected_result);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100460
valerio32f2ac92023-04-20 11:59:52 +0200461exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200463 USE_PSA_DONE();
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100464}
465/* END_CASE */
466
Glenn Strauss6f545ac2022-10-25 15:02:14 -0400467/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_TEST_HOOKS */
468void x509_crt_parse_cn_inet_pton(const char *cn, data_t *exp, int ref_ret)
469{
470 uint32_t addr[4];
471 size_t addrlen = mbedtls_x509_crt_parse_cn_inet_pton(cn, addr);
472 TEST_EQUAL(addrlen, (size_t) ref_ret);
473
474 if (addrlen) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100475 TEST_MEMORY_COMPARE(exp->x, exp->len, addr, addrlen);
Glenn Strauss6f545ac2022-10-25 15:02:14 -0400476 }
477}
478/* END_CASE */
479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500481void x509_parse_san(char *crt_file, char *result_str, int parse_result)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200482{
Ron Eldor890819a2019-05-13 19:03:04 +0300483 int ret;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200484 mbedtls_x509_crt crt;
Ron Eldor890819a2019-05-13 19:03:04 +0300485 mbedtls_x509_subject_alternative_name san;
486 mbedtls_x509_sequence *cur = NULL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200487 char buf[2000];
488 char *p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 size_t n = sizeof(buf);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200492 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 memset(buf, 0, 2000);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200494
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500495 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), parse_result);
Ron Eldor890819a2019-05-13 19:03:04 +0300496
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500497 if (parse_result != 0) {
498 goto exit;
499 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Ron Eldor890819a2019-05-13 19:03:04 +0300501 cur = &crt.subject_alt_names;
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 while (cur != NULL) {
503 ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
504 TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
Ron Eldor890819a2019-05-13 19:03:04 +0300505 /*
506 * If san type not supported, ignore.
507 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 if (ret == 0) {
Andrzej Kurekd40c2b62023-02-13 07:01:59 -0500509 ret = verify_parse_san(&san, &p, &n);
510 mbedtls_x509_free_subject_alt_name(&san);
511 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 }
Ron Eldor890819a2019-05-13 19:03:04 +0300513 cur = cur->next;
514 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200515 }
516
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500517 TEST_EQUAL(strcmp(buf, result_str), 0);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200518
519exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200521 USE_PSA_DONE();
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200522}
523/* END_CASE */
524
Hanno Becker612a2f12020-10-09 09:19:39 +0100525/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100526void x509_cert_info(char *crt_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000527{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000529 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000530 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200533 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000535
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500536 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 res = mbedtls_x509_crt_info(buf, 2000, "", &crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 TEST_ASSERT(res != -1);
540 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000541
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500542 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200543
544exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200546 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000547}
Paul Bakker33b43f12013-08-20 11:48:36 +0200548/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000549
Hanno Becker612a2f12020-10-09 09:19:39 +0100550/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100551void mbedtls_x509_crl_info(char *crl_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000552{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000554 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000555 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200558 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000560
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500561 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 res = mbedtls_x509_crl_info(buf, 2000, "", &crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 TEST_ASSERT(res != -1);
565 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000566
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500567 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200568
569exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +0200571 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000572}
Paul Bakker33b43f12013-08-20 11:48:36 +0200573/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000574
Andres AGa39db392016-12-08 17:10:38 +0000575/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100576void mbedtls_x509_crl_parse(char *crl_file, int result)
Andres AGa39db392016-12-08 17:10:38 +0000577{
578 mbedtls_x509_crl crl;
579 char buf[2000];
580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200582 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 memset(buf, 0, 2000);
Andres AGa39db392016-12-08 17:10:38 +0000584
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500585 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), result);
Andres AGa39db392016-12-08 17:10:38 +0000586
587exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +0200589 USE_PSA_DONE();
Andres AGa39db392016-12-08 17:10:38 +0000590}
591/* END_CASE */
592
Hanno Becker612a2f12020-10-09 09:19:39 +0100593/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100594void mbedtls_x509_csr_info(char *csr_file, char *result_str)
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100595{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100597 char buf[2000];
598 int res;
599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +0200601 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 memset(buf, 0, 2000);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100603
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500604 TEST_EQUAL(mbedtls_x509_csr_parse_file(&csr, csr_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 res = mbedtls_x509_csr_info(buf, 2000, "", &csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 TEST_ASSERT(res != -1);
608 TEST_ASSERT(res != -2);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100609
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500610 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200611
612exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +0200614 USE_PSA_DONE();
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100615}
616/* END_CASE */
617
Hanno Becker612a2f12020-10-09 09:19:39 +0100618/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100619void x509_verify_info(int flags, char *prefix, char *result_str)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100620{
621 char buf[2000];
622 int res;
623
Valerio Setti569c1712023-04-19 14:53:36 +0200624 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 memset(buf, 0, sizeof(buf));
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 res = mbedtls_x509_crt_verify_info(buf, sizeof(buf), prefix, flags);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 TEST_ASSERT(res >= 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100630
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500631 TEST_EQUAL(strcmp(buf, result_str), 0);
valerio32f2ac92023-04-20 11:59:52 +0200632
633exit:
Valerio Setti569c1712023-04-19 14:53:36 +0200634 USE_PSA_DONE();
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100635}
636/* END_CASE */
637
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200638/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100639void x509_verify_restart(char *crt_file, char *ca_file,
640 int result, int flags_result,
641 int max_ops, int min_restart, int max_restart)
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200642{
643 int ret, cnt_restart;
644 mbedtls_x509_crt_restart_ctx rs_ctx;
645 mbedtls_x509_crt crt;
646 mbedtls_x509_crt ca;
647 uint32_t flags = 0;
648
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200649 /*
650 * See comments on ecp_test_vect_restart() for op count precision.
651 *
Gilles Peskinee820c0a2023-08-03 17:45:20 +0200652 * For reference, with Mbed TLS 2.6 and default settings:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200653 * - ecdsa_verify() for P-256: ~ 6700
654 * - ecdsa_verify() for P-384: ~ 18800
655 * - x509_verify() for server5 -> test-ca2: ~ 18800
656 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
657 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 mbedtls_x509_crt_restart_init(&rs_ctx);
659 mbedtls_x509_crt_init(&crt);
660 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200661 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200662
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500663 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
664 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 mbedtls_ecp_set_max_ops(max_ops);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200667
668 cnt_restart = 0;
669 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
671 &mbedtls_x509_crt_profile_default, NULL, &flags,
672 NULL, NULL, &rs_ctx);
673 } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200674
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500675 TEST_EQUAL(ret, result);
676 TEST_EQUAL(flags, (uint32_t) flags_result);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 TEST_ASSERT(cnt_restart >= min_restart);
679 TEST_ASSERT(cnt_restart <= max_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200680
681 /* Do we leak memory when aborting? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
683 &mbedtls_x509_crt_profile_default, NULL, &flags,
684 NULL, NULL, &rs_ctx);
685 TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200686
687exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 mbedtls_x509_crt_restart_free(&rs_ctx);
689 mbedtls_x509_crt_free(&crt);
690 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100691 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200692}
693/* END_CASE */
694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100696void x509_verify(char *crt_file, char *ca_file, char *crl_file,
697 char *cn_name_str, int result, int flags_result,
698 char *profile_str,
699 char *verify_callback)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000700{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_x509_crt crt;
702 mbedtls_x509_crt ca;
703 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200704 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000705 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200706 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 char *cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200708 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 mbedtls_x509_crt_init(&crt);
711 mbedtls_x509_crt_init(&ca);
712 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200713 MD_OR_USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 if (strcmp(cn_name_str, "NULL") != 0) {
Paul Bakker33b43f12013-08-20 11:48:36 +0200716 cn_name = cn_name_str;
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 if (strcmp(profile_str, "") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200720 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 } else if (strcmp(profile_str, "next") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200722 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 } else if (strcmp(profile_str, "suite_b") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200724 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 } else if (strcmp(profile_str, "compat") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200726 profile = &compat_profile;
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 } else if (strcmp(profile_str, "all") == 0) {
Hanno Becker20a4ade2019-06-03 14:27:03 +0100728 profile = &profile_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100730 TEST_FAIL("Unknown algorithm profile");
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 }
Gilles Peskineef86ab22017-05-05 18:59:02 +0200732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 if (strcmp(verify_callback, "NULL") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200734 f_vrfy = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 } else if (strcmp(verify_callback, "verify_none") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200736 f_vrfy = verify_none;
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 } else if (strcmp(verify_callback, "verify_all") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200738 f_vrfy = verify_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100740 TEST_FAIL("No known verify callback selected");
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200742
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500743 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
744 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
745 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 res = mbedtls_x509_crt_verify_with_profile(&crt,
748 &ca,
749 &crl,
750 profile,
751 cn_name,
752 &flags,
753 f_vrfy,
754 NULL);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 TEST_EQUAL(res, result);
757 TEST_EQUAL(flags, (uint32_t) flags_result);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200758
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200759#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker0350d562019-03-28 14:23:36 +0000760 /* CRLs aren't supported with CA callbacks, so skip the CA callback
Jarno Lamsadfd22c42019-04-01 15:18:53 +0300761 * version of the test if CRLs are in use. */
Gilles Peskinebb7d92c2023-10-17 17:26:44 +0200762 if (strcmp(crl_file, "") == 0) {
Hanno Becker0350d562019-03-28 14:23:36 +0000763 flags = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 res = mbedtls_x509_crt_verify_with_ca_cb(&crt,
766 ca_callback,
767 &ca,
768 profile,
769 cn_name,
770 &flags,
771 f_vrfy,
772 NULL);
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200773
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500774 TEST_EQUAL(res, result);
775 TEST_EQUAL(flags, (uint32_t) (flags_result));
Hanno Becker0350d562019-03-28 14:23:36 +0000776 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200777#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200778exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 mbedtls_x509_crt_free(&crt);
780 mbedtls_x509_crt_free(&ca);
781 mbedtls_x509_crl_free(&crl);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100782 MD_OR_USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000783}
Paul Bakker33b43f12013-08-20 11:48:36 +0200784/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000785
Jarno Lamsa557426a2019-03-27 17:08:29 +0200786/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Gilles Peskine449bd832023-01-11 14:50:10 +0100787void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
788 int exp_ret)
Jarno Lamsa557426a2019-03-27 17:08:29 +0200789{
790 int ret;
791 mbedtls_x509_crt crt;
792 mbedtls_x509_crt ca;
793 uint32_t flags = 0;
Hanno Becker3f932bb2019-03-28 17:06:47 +0000794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 mbedtls_x509_crt_init(&crt);
796 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200797 USE_PSA_INIT();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200798
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500799 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
800 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 if (strcmp(name, "NULL") == 0) {
Jarno Lamsa557426a2019-03-27 17:08:29 +0200803 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca,
807 &compat_profile, name, &flags,
808 NULL, NULL);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200809
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500810 TEST_EQUAL(ret, exp_ret);
811 TEST_EQUAL(flags, (uint32_t) (-1));
Jarno Lamsa557426a2019-03-27 17:08:29 +0200812exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 mbedtls_x509_crt_free(&crt);
814 mbedtls_x509_crt_free(&ca);
Valerio Setti569c1712023-04-19 14:53:36 +0200815 USE_PSA_DONE();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200816}
817/* END_CASE */
818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820void x509_verify_callback(char *crt_file, char *ca_file, char *name,
821 int exp_ret, char *exp_vrfy_out)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200822{
823 int ret;
824 mbedtls_x509_crt crt;
825 mbedtls_x509_crt ca;
826 uint32_t flags = 0;
827 verify_print_context vrfy_ctx;
828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 mbedtls_x509_crt_init(&crt);
830 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200831 MD_OR_USE_PSA_INIT();
832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 verify_print_init(&vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200834
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500835 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
836 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 if (strcmp(name, "NULL") == 0) {
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200839 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 }
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL,
843 &compat_profile,
844 name, &flags,
845 verify_print, &vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200846
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500847 TEST_EQUAL(ret, exp_ret);
848 TEST_EQUAL(strcmp(vrfy_ctx.buf, exp_vrfy_out), 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200849
850exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 mbedtls_x509_crt_free(&crt);
852 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100853 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200854}
855/* END_CASE */
856
Hanno Becker612a2f12020-10-09 09:19:39 +0100857/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100858void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
859 char *new_subject_ou,
860 char *result_str,
861 int ret)
Werner Lewis31ecb962022-06-17 15:51:55 +0100862{
863 mbedtls_x509_crt crt;
864 char buf[2000];
865 int res = 0;
866
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200868 USE_PSA_INIT();
869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 memset(buf, 0, 2000);
Werner Lewis31ecb962022-06-17 15:51:55 +0100871
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500872 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100873 crt.subject.next->val.p = (unsigned char *) new_subject_ou;
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 crt.subject.next->val.len = strlen(new_subject_ou);
Werner Lewis31ecb962022-06-17 15:51:55 +0100875
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
Werner Lewis31ecb962022-06-17 15:51:55 +0100877
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 if (ret != 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500879 TEST_EQUAL(res, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 } else {
881 TEST_ASSERT(res != -1);
882 TEST_ASSERT(res != -2);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500883 TEST_EQUAL(strcmp(buf, result_str), 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100884 }
885exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200887 USE_PSA_DONE();
Werner Lewis31ecb962022-06-17 15:51:55 +0100888}
889/* END_CASE */
890
891/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100892void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000893{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000895 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200896 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200899 USE_PSA_INIT();
900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000902
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500903 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 if (strcmp(entity, "subject") == 0) {
905 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
906 } else if (strcmp(entity, "issuer") == 0) {
907 res = mbedtls_x509_dn_gets(buf, 2000, &crt.issuer);
908 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100909 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 TEST_ASSERT(res != -1);
913 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000914
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500915 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200916
917exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200919 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000920}
Paul Bakker33b43f12013-08-20 11:48:36 +0200921/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000922
David Horstmanndb73d3b2022-10-04 16:49:16 +0100923/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100924void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
David Horstmanndb73d3b2022-10-04 16:49:16 +0100925{
valerioe50831c2023-04-20 14:48:19 +0200926 unsigned char *name = NULL;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100927 unsigned char *p;
928 size_t name_len;
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100929 mbedtls_x509_name head;
David Horstmann3cd67582022-10-17 17:59:10 +0100930 int ret;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100931
Valerio Setti569c1712023-04-19 14:53:36 +0200932 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 memset(&head, 0, sizeof(head));
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100936 p = name;
937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 ret = mbedtls_x509_get_name(&p, (name + name_len), &head);
939 if (ret == 0) {
940 mbedtls_asn1_free_named_data_list_shallow(head.next);
941 }
David Horstmanndb73d3b2022-10-04 16:49:16 +0100942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 TEST_EQUAL(ret, exp_ret);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100944
valerio32f2ac92023-04-20 11:59:52 +0200945exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 mbedtls_free(name);
Valerio Setti569c1712023-04-19 14:53:36 +0200947 USE_PSA_DONE();
David Horstmanndb73d3b2022-10-04 16:49:16 +0100948}
949/* END_CASE */
950
Werner Lewisb3acb052022-06-17 15:59:58 +0100951/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100952void mbedtls_x509_dn_get_next(char *name_str,
953 int next_merged,
954 char *expected_oids,
955 int exp_count,
956 char *exp_dn_gets)
Werner Lewisb3acb052022-06-17 15:59:58 +0100957{
958 int ret = 0, i;
Werner Lewisac80a662022-06-23 11:58:02 +0100959 size_t len = 0, out_size;
Werner Lewisb3acb052022-06-17 15:59:58 +0100960 mbedtls_asn1_named_data *names = NULL;
Gilles Peskine21e46b32023-10-17 16:35:20 +0200961 mbedtls_x509_name parsed;
962 memset(&parsed, 0, sizeof(parsed));
963 mbedtls_x509_name *parsed_cur;
Werner Lewisac80a662022-06-23 11:58:02 +0100964 // Size of buf is maximum required for test cases
Gilles Peskinef2574202023-10-18 17:39:48 +0200965 unsigned char buf[80] = { 0 };
Gilles Peskine21e46b32023-10-17 16:35:20 +0200966 unsigned char *out = NULL;
967 unsigned char *c = buf + sizeof(buf);
Werner Lewisb3acb052022-06-17 15:59:58 +0100968 const char *short_name;
969
Valerio Setti569c1712023-04-19 14:53:36 +0200970 USE_PSA_INIT();
Gilles Peskine21e46b32023-10-17 16:35:20 +0200971
Werner Lewisac80a662022-06-23 11:58:02 +0100972 // Additional size required for trailing space
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 out_size = strlen(expected_oids) + 2;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100974 TEST_CALLOC(out, out_size);
Werner Lewisb3acb052022-06-17 15:59:58 +0100975
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 TEST_EQUAL(mbedtls_x509_string_to_names(&names, name_str), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 ret = mbedtls_x509_write_names(&c, buf, names);
979 TEST_LE_S(0, ret);
Werner Lewisb3acb052022-06-17 15:59:58 +0100980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
982 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
983 TEST_EQUAL(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100984
985 // Iterate over names and set next_merged nodes
986 parsed_cur = &parsed;
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 for (; next_merged != 0 && parsed_cur != NULL; next_merged = next_merged >> 1) {
Werner Lewis12657cd2022-06-20 11:47:57 +0100988 parsed_cur->next_merged = next_merged & 0x01;
Werner Lewisb3acb052022-06-17 15:59:58 +0100989 parsed_cur = parsed_cur->next;
990 }
991
992 // Iterate over RDN nodes and print OID of first element to buffer
993 parsed_cur = &parsed;
994 len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 for (i = 0; parsed_cur != NULL; i++) {
996 TEST_EQUAL(mbedtls_oid_get_attr_short_name(&parsed_cur->oid,
997 &short_name), 0);
998 len += mbedtls_snprintf((char *) out + len, out_size - len, "%s ", short_name);
999 parsed_cur = mbedtls_x509_dn_get_next(parsed_cur);
Werner Lewisb3acb052022-06-17 15:59:58 +01001000 }
1001 out[len-1] = 0;
1002
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 TEST_EQUAL(exp_count, i);
1004 TEST_EQUAL(strcmp((char *) out, expected_oids), 0);
1005 mbedtls_free(out);
Werner Lewisac80a662022-06-23 11:58:02 +01001006 out = NULL;
Werner Lewisb3acb052022-06-17 15:59:58 +01001007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 out_size = strlen(exp_dn_gets) + 1;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001009 TEST_CALLOC(out, out_size);
Werner Lewisac80a662022-06-23 11:58:02 +01001010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 TEST_LE_S(0, mbedtls_x509_dn_gets((char *) out, out_size, &parsed));
1012 TEST_EQUAL(strcmp((char *) out, exp_dn_gets), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +01001013exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 mbedtls_free(out);
1015 mbedtls_asn1_free_named_data_list(&names);
1016 mbedtls_asn1_free_named_data_list_shallow(parsed.next);
Valerio Setti569c1712023-04-19 14:53:36 +02001017 USE_PSA_DONE();
Werner Lewisb3acb052022-06-17 15:59:58 +01001018}
Werner Lewisb3acb052022-06-17 15:59:58 +01001019/* END_CASE */
1020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001022void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
Paul Bakker37940d9f2009-07-10 22:38:58 +00001023{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +00001025
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001027 USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +00001028
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001029 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 if (strcmp(entity, "valid_from") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001032 TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_from), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 } else if (strcmp(entity, "valid_to") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001034 TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_to), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +01001036 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001038
Paul Bakkerbd51b262014-07-10 15:26:12 +02001039exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001041 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +00001042}
Paul Bakker33b43f12013-08-20 11:48:36 +02001043/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +00001044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001046void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001047{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001049
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001051 USE_PSA_INIT();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001052
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001053 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if (strcmp(entity, "valid_from") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001056 TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_from), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 } else if (strcmp(entity, "valid_to") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001058 TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_to), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +01001060 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001062
Paul Bakkerbd51b262014-07-10 15:26:12 +02001063exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001065 USE_PSA_DONE();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001066}
1067/* END_CASE */
1068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001070void x509parse_crt_file(char *crt_file, int result)
Paul Bakker5a5fa922014-09-26 14:53:04 +02001071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +02001073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001075 USE_PSA_INIT();
Paul Bakker5a5fa922014-09-26 14:53:04 +02001076
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001077 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), result);
Paul Bakker5a5fa922014-09-26 14:53:04 +02001078
1079exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001081 USE_PSA_DONE();
Paul Bakker5a5fa922014-09-26 14:53:04 +02001082}
1083/* END_CASE */
1084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001086void x509parse_crt(data_t *buf, char *result_str, int result)
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001087{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 mbedtls_x509_crt crt;
Hanno Becker612a2f12020-10-09 09:19:39 +01001089#if !defined(MBEDTLS_X509_REMOVE_INFO)
Hanno Beckerfff2d572020-10-09 09:45:19 +01001090 unsigned char output[2000] = { 0 };
Azim Khanf1aaec92017-05-30 14:23:15 +01001091 int res;
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001092#else
1093 ((void) result_str);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001094#endif
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001097 USE_PSA_INIT();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001098
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001099 TEST_EQUAL(mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len), result);
Hanno Becker612a2f12020-10-09 09:19:39 +01001100#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 if ((result) == 0) {
1102 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1103 TEST_ASSERT(res != -1);
1104 TEST_ASSERT(res != -2);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001105
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001106 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001107 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001109#endif
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 mbedtls_x509_crt_free(&crt);
1112 mbedtls_x509_crt_init(&crt);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001113
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001114 TEST_EQUAL(mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len), result);
Hanno Becker612a2f12020-10-09 09:19:39 +01001115#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if ((result) == 0) {
1117 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakker33b43f12013-08-20 11:48:36 +02001120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 TEST_ASSERT(res != -1);
1122 TEST_ASSERT(res != -2);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001123
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001124 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001125 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001127#endif /* !MBEDTLS_X509_REMOVE_INFO */
Paul Bakkerb08e6842012-02-11 18:43:20 +00001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 mbedtls_x509_crt_free(&crt);
1130 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001131
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001132 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL, NULL),
1133 result);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001134#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 if ((result) == 0) {
1136 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 TEST_ASSERT(res != -1);
1139 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001140
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001141 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001142 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001144#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 mbedtls_x509_crt_free(&crt);
1147 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001148
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001149 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL, NULL),
1150 result);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001151#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 if ((result) == 0) {
1153 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 TEST_ASSERT(res != -1);
1156 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001157
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001158 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001159 }
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001160#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001161
1162exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001164 USE_PSA_DONE();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001165}
1166/* END_CASE */
1167
1168/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001169void x509parse_crt_cb(data_t *buf, char *result_str, int result)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001170{
1171 mbedtls_x509_crt crt;
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001172 mbedtls_x509_buf oid;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001173
1174#if !defined(MBEDTLS_X509_REMOVE_INFO)
1175 unsigned char output[2000] = { 0 };
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001176 int res;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001177#else
1178 ((void) result_str);
1179#endif
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001180
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001181 oid.tag = MBEDTLS_ASN1_OID;
1182 oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001184
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001186 USE_PSA_INIT();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001187
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001188 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
1189 &oid), result);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001190#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if ((result) == 0) {
1192 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001193
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 TEST_ASSERT(res != -1);
1195 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001196
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001197 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001198 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 memset(output, 0, 2000);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001200#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 mbedtls_x509_crt_free(&crt);
1203 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001204
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001205 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1206 &oid), (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001207#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 if ((result) == 0) {
1209 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 TEST_ASSERT(res != -1);
1212 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001213
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001214 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001215 }
Hanno Beckerfff2d572020-10-09 09:45:19 +01001216#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001217
Paul Bakkerbd51b262014-07-10 15:26:12 +02001218exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001220 USE_PSA_DONE();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001221}
Paul Bakker33b43f12013-08-20 11:48:36 +02001222/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001223
Hanno Becker612a2f12020-10-09 09:19:39 +01001224/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001225void x509parse_crl(data_t *buf, char *result_str, int result)
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001226{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001228 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +01001229 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +02001232 USE_PSA_INIT();
1233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 memset(output, 0, 2000);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001235
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001236
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001237 TEST_EQUAL(mbedtls_x509_crl_parse(&crl, buf->x, buf->len), (result));
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 if ((result) == 0) {
1239 res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
Paul Bakker33b43f12013-08-20 11:48:36 +02001240
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 TEST_ASSERT(res != -1);
1242 TEST_ASSERT(res != -2);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001243
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001244 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001245 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001246
Paul Bakkerbd51b262014-07-10 15:26:12 +02001247exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +02001249 USE_PSA_DONE();
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001250}
Paul Bakker33b43f12013-08-20 11:48:36 +02001251/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001252
Hanno Becker612a2f12020-10-09 09:19:39 +01001253/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001254void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001255{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001257 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001258 int my_ret;
1259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001261 USE_PSA_INIT();
1262
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 memset(my_out, 0, sizeof(my_out));
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001266 TEST_EQUAL(my_ret, ref_ret);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001267
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 if (ref_ret == 0) {
1269 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001270 TEST_EQUAL(my_out_len, strlen(ref_out));
1271 TEST_EQUAL(strcmp(my_out, ref_out), 0);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001272 }
1273
Paul Bakkerbd51b262014-07-10 15:26:12 +02001274exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001276 USE_PSA_DONE();
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001277}
1278/* END_CASE */
1279
Matthias Schulzab408222023-10-18 13:20:59 +02001280/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1281void mbedtls_x509_csr_parse_with_ext_cb(data_t *csr_der, char *ref_out, int ref_ret, int accept)
1282{
1283 mbedtls_x509_csr csr;
1284 char my_out[1000];
1285 int my_ret;
1286
1287 mbedtls_x509_csr_init(&csr);
1288 USE_PSA_INIT();
1289
1290 memset(my_out, 0, sizeof(my_out));
1291
1292 my_ret = mbedtls_x509_csr_parse_der_with_ext_cb(&csr, csr_der->x, csr_der->len,
1293 accept ? parse_csr_ext_accept_cb :
Matthias Schulzedc32ea2023-10-19 16:09:08 +02001294 parse_csr_ext_reject_cb,
Matthias Schulzab408222023-10-18 13:20:59 +02001295 NULL);
1296 TEST_EQUAL(my_ret, ref_ret);
1297
1298 if (ref_ret == 0) {
1299 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1300 TEST_EQUAL(my_out_len, strlen(ref_out));
1301 TEST_EQUAL(strcmp(my_out, ref_out), 0);
1302 }
1303
1304exit:
1305 mbedtls_x509_csr_free(&csr);
1306 USE_PSA_DONE();
1307}
1308/* END_CASE */
1309
Przemek Stekield7992df2023-01-25 16:19:50 +01001310/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1311void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
1312{
1313 mbedtls_x509_csr csr;
1314 char my_out[1000];
1315 int my_ret;
1316
1317 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001318 USE_PSA_INIT();
1319
Przemek Stekield7992df2023-01-25 16:19:50 +01001320 memset(my_out, 0, sizeof(my_out));
1321
1322 my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001323 TEST_EQUAL(my_ret, ref_ret);
Przemek Stekield7992df2023-01-25 16:19:50 +01001324
1325 if (ref_ret == 0) {
1326 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001327 TEST_EQUAL(my_out_len, strlen(ref_out));
1328 TEST_EQUAL(strcmp(my_out, ref_out), 0);
Przemek Stekield7992df2023-01-25 16:19:50 +01001329 }
1330
1331exit:
1332 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001333 USE_PSA_DONE();
Przemek Stekield7992df2023-01-25 16:19:50 +01001334}
1335/* END_CASE */
1336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1e5fec62023-04-13 18:13:48 +02001338void mbedtls_x509_crt_parse_file(char *crt_path, int ret, int nb_crt)
1339{
1340 mbedtls_x509_crt chain, *cur;
1341 int i;
1342
1343 mbedtls_x509_crt_init(&chain);
1344 USE_PSA_INIT();
1345
1346 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, crt_path), ret);
1347
1348 /* Check how many certs we got */
1349 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1350 if (cur->raw.p != NULL) {
1351 i++;
1352 }
1353 }
1354
1355 TEST_EQUAL(i, nb_crt);
1356
1357exit:
1358 mbedtls_x509_crt_free(&chain);
1359 USE_PSA_DONE();
1360}
1361/* END_CASE */
1362
1363/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001364void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001365{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001367 int i;
1368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001370 USE_PSA_INIT();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001371
Gilles Peskine55ad28a2023-04-13 18:14:45 +02001372 TEST_EQUAL(mbedtls_x509_crt_parse_path(&chain, crt_path), ret);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001373
1374 /* Check how many certs we got */
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1376 if (cur->raw.p != NULL) {
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001377 i++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 }
1379 }
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001380
Gilles Peskine55ad28a2023-04-13 18:14:45 +02001381 TEST_EQUAL(i, nb_crt);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001382
Paul Bakkerbd51b262014-07-10 15:26:12 +02001383exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 mbedtls_x509_crt_free(&chain);
Valerio Setti569c1712023-04-19 14:53:36 +02001385 USE_PSA_DONE();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001386}
1387/* END_CASE */
1388
Janos Follath822b2c32015-10-11 10:25:22 +02001389/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001390void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1391 int ret_chk, int flags_chk)
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001392{
1393 char file_buf[128];
1394 int ret;
1395 uint32_t flags;
1396 mbedtls_x509_crt trusted, chain;
1397
1398 /*
1399 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1400 * with NN.crt signed by NN-1.crt
1401 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 mbedtls_x509_crt_init(&trusted);
1403 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001404 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001405
1406 /* Load trusted root */
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001407 TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, ca_file), 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001408
1409 /* Load a chain with nb_int intermediates (from 01 to nb_int),
1410 * plus one "end-entity" cert (nb_int + 1) */
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001411 ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 nb_int + 1);
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001413 TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001414 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, file_buf), 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001415
1416 /* Try to verify that chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1418 NULL, NULL);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001419 TEST_EQUAL(ret, ret_chk);
1420 TEST_EQUAL(flags, (uint32_t) flags_chk);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001421
1422exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 mbedtls_x509_crt_free(&chain);
1424 mbedtls_x509_crt_free(&trusted);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001425 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001426}
1427/* END_CASE */
1428
1429/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001430void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1431 int flags_result, int result,
1432 char *profile_name, int vrfy_fatal_lvls)
Janos Follath822b2c32015-10-11 10:25:22 +02001433{
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 char *act;
Janos Follath822b2c32015-10-11 10:25:22 +02001435 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001436 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001437 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001438 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +02001439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 mbedtls_x509_crt_init(&chain);
1441 mbedtls_x509_crt_init(&trusted);
valerio32f2ac92023-04-20 11:59:52 +02001442 MD_OR_USE_PSA_INIT();
Janos Follath822b2c32015-10-11 10:25:22 +02001443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 while ((act = mystrsep(&chain_paths, " ")) != NULL) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001445 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, act), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 }
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001447 TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, trusted_ca), 0);
Janos Follath822b2c32015-10-11 10:25:22 +02001448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 if (strcmp(profile_name, "") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001450 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 } else if (strcmp(profile_name, "next") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001452 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 } else if (strcmp(profile_name, "suiteb") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001454 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 } else if (strcmp(profile_name, "rsa3072") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001456 profile = &profile_rsa3072;
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 } else if (strcmp(profile_name, "sha512") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001458 profile = &profile_sha512;
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 }
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001460
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1462 NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
Janos Follathef4f2582015-10-11 16:17:27 +02001463
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001464 TEST_EQUAL(res, (result));
1465 TEST_EQUAL(flags, (uint32_t) (flags_result));
Janos Follath822b2c32015-10-11 10:25:22 +02001466
1467exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 mbedtls_x509_crt_free(&trusted);
1469 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001470 MD_OR_USE_PSA_DONE();
Janos Follath822b2c32015-10-11 10:25:22 +02001471}
1472/* END_CASE */
1473
Hanno Becker612a2f12020-10-09 09:19:39 +01001474/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001475void x509_oid_desc(data_t *buf, char *ref_desc)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001476{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001478 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001479 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001480
Valerio Setti569c1712023-04-19 14:53:36 +02001481 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001484 oid.p = buf->x;
1485 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 ret = mbedtls_oid_get_extended_key_usage(&oid, &desc);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 if (strcmp(ref_desc, "notfound") == 0) {
1490 TEST_ASSERT(ret != 0);
1491 TEST_ASSERT(desc == NULL);
1492 } else {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001493 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 TEST_ASSERT(desc != NULL);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001495 TEST_EQUAL(strcmp(desc, ref_desc), 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001496 }
valerio32f2ac92023-04-20 11:59:52 +02001497
1498exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001499 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001500}
1501/* END_CASE */
1502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001504void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001505{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001507 char num_buf[100];
1508
Valerio Setti569c1712023-04-19 14:53:36 +02001509 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001510
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001511 memset(num_buf, 0x2a, sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001514 oid.p = oid_buf->x;
1515 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001516
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001517 TEST_ASSERT((size_t) blen <= sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001518
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001519 TEST_EQUAL(mbedtls_oid_get_numeric_string(num_buf, blen, &oid), ret);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 if (ret >= 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001522 TEST_EQUAL(num_buf[ret], 0);
1523 TEST_EQUAL(strcmp(num_buf, numstr), 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001524 }
valerio32f2ac92023-04-20 11:59:52 +02001525
1526exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001527 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001528}
1529/* END_CASE */
1530
TRodziewicz442fdc22021-06-07 13:52:23 +02001531/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001532void x509_check_key_usage(char *crt_file, int usage, int ret)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001533{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001535
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001537 USE_PSA_INIT();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001538
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001539 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001540
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001541 TEST_EQUAL(mbedtls_x509_crt_check_key_usage(&crt, usage), ret);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001542
Paul Bakkerbd51b262014-07-10 15:26:12 +02001543exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001545 USE_PSA_DONE();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001546}
1547/* END_CASE */
1548
TRodziewicz442fdc22021-06-07 13:52:23 +02001549/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001550void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1551 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001552{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001554
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001556 USE_PSA_INIT();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001557
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001558 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001559
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001560 TEST_EQUAL(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x, oid->len),
1561 ret);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001562
Paul Bakkerbd51b262014-07-10 15:26:12 +02001563exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001565 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001566}
1567/* END_CASE */
1568
Andres AG4b76aec2016-09-23 13:16:02 +01001569/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001570void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1571 int day, int hour, int min, int sec)
Andres AG4b76aec2016-09-23 13:16:02 +01001572{
1573 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001574 unsigned char buf[21];
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 unsigned char *start = buf;
1576 unsigned char *end = buf;
Andres AG4b76aec2016-09-23 13:16:02 +01001577
Valerio Setti569c1712023-04-19 14:53:36 +02001578 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 memset(&time, 0x00, sizeof(time));
1580 *end = (unsigned char) tag; end++;
1581 *end = strlen(time_str);
1582 TEST_ASSERT(*end < 20);
Andres AG4b76aec2016-09-23 13:16:02 +01001583 end++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 memcpy(end, time_str, (size_t) *(end - 1));
Andres AG4b76aec2016-09-23 13:16:02 +01001585 end += *(end - 1);
1586
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001587 TEST_EQUAL(mbedtls_x509_get_time(&start, end, &time), ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 if (ret == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001589 TEST_EQUAL(year, time.year);
1590 TEST_EQUAL(mon, time.mon);
1591 TEST_EQUAL(day, time.day);
1592 TEST_EQUAL(hour, time.hour);
1593 TEST_EQUAL(min, time.min);
1594 TEST_EQUAL(sec, time.sec);
Andres AG4b76aec2016-09-23 13:16:02 +01001595 }
valerio32f2ac92023-04-20 11:59:52 +02001596exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001597 USE_PSA_DONE();
Andres AG4b76aec2016-09-23 13:16:02 +01001598}
1599/* END_CASE */
1600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001602void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1603 int ref_msg_md, int ref_mgf_md,
1604 int ref_salt_len, int ref_ret)
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001605{
1606 int my_ret;
Ronald Cronac6ae352020-06-26 14:33:03 +02001607 mbedtls_x509_buf buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001609 int my_salt_len;
1610
Valerio Setti569c1712023-04-19 14:53:36 +02001611 USE_PSA_INIT();
1612
Ronald Cronac6ae352020-06-26 14:33:03 +02001613 buf.p = params->x;
1614 buf.len = params->len;
1615 buf.tag = params_tag;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001616
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1618 &my_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001619
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001620 TEST_EQUAL(my_ret, ref_ret);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001621
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 if (ref_ret == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001623 TEST_EQUAL(my_msg_md, (mbedtls_md_type_t) ref_msg_md);
1624 TEST_EQUAL(my_mgf_md, (mbedtls_md_type_t) ref_mgf_md);
1625 TEST_EQUAL(my_salt_len, ref_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001626 }
1627
Paul Bakkerbd51b262014-07-10 15:26:12 +02001628exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001629 USE_PSA_DONE();
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001630}
1631/* END_CASE */
toth92ga41954d2021-02-12 16:11:17 +01001632
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001633/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001634void x509_crt_parse_subjectkeyid(char *file, data_t *subjectKeyId, int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001635{
1636 mbedtls_x509_crt crt;
toth92ga41954d2021-02-12 16:11:17 +01001637
1638 mbedtls_x509_crt_init(&crt);
1639
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001640 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001641
toth92g357b2972021-05-04 15:41:35 +02001642 if (ref_ret == 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001643 TEST_EQUAL(crt.subject_key_id.tag, MBEDTLS_ASN1_OCTET_STRING);
1644 TEST_EQUAL(memcmp(crt.subject_key_id.p, subjectKeyId->x, subjectKeyId->len), 0);
1645 TEST_EQUAL(crt.subject_key_id.len, subjectKeyId->len);
toth92g357b2972021-05-04 15:41:35 +02001646 } else {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001647 TEST_EQUAL(crt.subject_key_id.tag, 0);
1648 TEST_EQUAL(crt.subject_key_id.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001649 }
1650
1651exit:
1652 mbedtls_x509_crt_free(&crt);
1653}
1654/* END_CASE */
1655
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001656/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Przemek Stekiel2568d472023-04-06 09:23:25 +02001657void x509_crt_parse_authoritykeyid(char *file,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001658 data_t *keyId,
toth92g5042b102021-05-06 08:22:17 +02001659 char *authorityKeyId_issuer,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001660 data_t *serial,
toth92g5042b102021-05-06 08:22:17 +02001661 int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001662{
1663 mbedtls_x509_crt crt;
Przemek Stekiel39dbe232023-04-03 10:19:22 +02001664 mbedtls_x509_subject_alternative_name san;
David Horstmann9a3a1a62023-06-22 16:59:09 +01001665 char name_buf[128];
toth92ga41954d2021-02-12 16:11:17 +01001666
1667 mbedtls_x509_crt_init(&crt);
1668
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001669 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001670
toth92g357b2972021-05-04 15:41:35 +02001671 if (ref_ret == 0) {
toth92ga41954d2021-02-12 16:11:17 +01001672 /* KeyId test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001673 if (keyId->len > 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001674 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, MBEDTLS_ASN1_OCTET_STRING);
1675 TEST_EQUAL(memcmp(crt.authority_key_id.keyIdentifier.p, keyId->x, keyId->len), 0);
1676 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, keyId->len);
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001677 } else {
1678 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1679 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001680 }
toth92ga41954d2021-02-12 16:11:17 +01001681
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001682
toth92ga41954d2021-02-12 16:11:17 +01001683 /* Issuer test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001684 if (strlen(authorityKeyId_issuer) > 0) {
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001685 mbedtls_x509_sequence *issuerPtr = &crt.authority_key_id.authorityCertIssuer;
Przemek Stekiel01984212023-02-09 09:29:34 +01001686
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001687 TEST_EQUAL(mbedtls_x509_parse_subject_alt_name(&issuerPtr->buf, &san), 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001688
David Horstmann9a3a1a62023-06-22 16:59:09 +01001689 TEST_ASSERT(mbedtls_x509_dn_gets(name_buf, sizeof(name_buf),
1690 &san.san.directory_name)
1691 > 0);
1692 TEST_EQUAL(strcmp(name_buf, authorityKeyId_issuer), 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001693
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001694 mbedtls_x509_free_subject_alt_name(&san);
toth92ga41954d2021-02-12 16:11:17 +01001695 }
1696
1697 /* Serial test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001698 if (serial->len > 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001699 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001700 MBEDTLS_ASN1_INTEGER);
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001701 TEST_EQUAL(memcmp(crt.authority_key_id.authorityCertSerialNumber.p,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001702 serial->x, serial->len), 0);
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001703 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, serial->len);
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001704 } else {
1705 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1706 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001707 }
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001708
toth92g357b2972021-05-04 15:41:35 +02001709 } else {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001710 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1711 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001712
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001713 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1714 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001715 }
1716
1717exit:
1718 mbedtls_x509_crt_free(&crt);
1719}
1720/* END_CASE */