blob: 66a7c8df9b6f981697b9ac007a9c1155d3cfb866 [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"
Valerio Setti25b282e2024-01-17 10:55:32 +01007#include "x509_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00008#include "mbedtls/pem.h"
9#include "mbedtls/oid.h"
10#include "mbedtls/base64.h"
Chris Jones9f7a6932021-04-14 12:12:09 +010011#include "mbedtls/error.h"
Valerio Setti178b5bd2023-02-13 10:04:28 +010012#include "mbedtls/pk.h"
Sam Berry4aee6a22024-07-19 15:00:41 +010013#include "mbedtls/asn1.h"
14#include "mbedtls/asn1write.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +010015#include "string.h"
Paul Bakkerb63b0af2011-01-13 17:54:59 +000016
Simon Butcher9e24b512017-07-28 12:15:13 +010017#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
Hanno Becker3b1422e2017-07-26 13:38:02 +010018#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
Gilles Peskine449bd832023-01-11 14:50:10 +010019 than the current threshold 19. To test larger values, please \
David Horstmann441b66c2024-07-01 16:39:39 +010020 adapt the script framework/data_files/dir-max/long.sh."
Hanno Becker3b1422e2017-07-26 13:38:02 +010021#endif
22
Hanno Becker20a4ade2019-06-03 14:27:03 +010023/* Test-only profile allowing all digests, PK algorithms, and curves. */
24const mbedtls_x509_crt_profile profile_all =
25{
26 0xFFFFFFFF, /* Any MD */
27 0xFFFFFFFF, /* Any PK alg */
28 0xFFFFFFFF, /* Any curve */
29 1024,
30};
31
Gilles Peskineef86ab22017-05-05 18:59:02 +020032/* Profile for backward compatibility. Allows SHA-1, unlike the default
33 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020034const mbedtls_x509_crt_profile compat_profile =
35{
Gilles Peskine449bd832023-01-11 14:50:10 +010036 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) |
37 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160) |
38 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
39 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
40 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
41 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubney28015e12022-04-21 08:12:59 +010042 0xFFFFFFFF, /* Any PK alg */
43 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020044 1024,
45};
46
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020047const mbedtls_x509_crt_profile profile_rsa3072 =
48{
Gilles Peskine449bd832023-01-11 14:50:10 +010049 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
50 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
51 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
52 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_RSA),
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020053 0,
54 3072,
55};
56
57const mbedtls_x509_crt_profile profile_sha512 =
58{
Gilles Peskine449bd832023-01-11 14:50:10 +010059 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubney28015e12022-04-21 08:12:59 +010060 0xFFFFFFFF, /* Any PK alg */
61 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020062 1024,
63};
64
Michael Schustera3cc4632024-06-07 01:51:54 +020065#if defined(MBEDTLS_X509_CRT_PARSE_C)
66
67#if defined(MBEDTLS_FS_IO)
Michael Schuster54300d42024-06-04 02:30:22 +020068static int verify_none(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000069{
Paul Bakker5a624082011-01-18 16:31:52 +000070 ((void) data);
71 ((void) crt);
72 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010073 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020074
Paul Bakker915275b2012-09-28 07:10:55 +000075 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000076}
77
Michael Schuster54300d42024-06-04 02:30:22 +020078static int verify_all(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000079{
Paul Bakker5a624082011-01-18 16:31:52 +000080 ((void) data);
81 ((void) crt);
82 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000083 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000084
Paul Bakkerb63b0af2011-01-13 17:54:59 +000085 return 0;
86}
87
Michael Schustera3cc4632024-06-07 01:51:54 +020088#if defined(MBEDTLS_X509_CRL_PARSE_C) && \
89 defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Michael Schusterf828f042024-06-07 06:47:31 +020090static int ca_callback_fail(void *data, mbedtls_x509_crt const *child,
91 mbedtls_x509_crt **candidates)
Jarno Lamsa557426a2019-03-27 17:08:29 +020092{
93 ((void) data);
94 ((void) child);
95 ((void) candidates);
96
97 return -1;
98}
Michael Schustera3cc4632024-06-07 01:51:54 +020099
Michael Schuster54300d42024-06-04 02:30:22 +0200100static int ca_callback(void *data, mbedtls_x509_crt const *child,
Michael Schusterbd89b792024-06-04 02:41:10 +0200101 mbedtls_x509_crt **candidates)
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200102{
Hanno Beckercbb59032019-03-28 14:14:22 +0000103 int ret = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200104 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +0000105 mbedtls_x509_crt *first;
106
107 /* This is a test-only implementation of the CA callback
108 * which always returns the entire list of trusted certificates.
109 * Production implementations managing a large number of CAs
110 * should use an efficient presentation and lookup for the
111 * set of trusted certificates (such as a hashtable) and only
112 * return those trusted certificates which satisfy basic
113 * parental checks, such as the matching of child `Issuer`
114 * and parent `Subject` field. */
115 ((void) child);
116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
118 if (first == NULL) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000119 ret = -1;
120 goto exit;
121 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 mbedtls_x509_crt_init(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000125 ret = -1;
126 goto exit;
127 }
128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 while (ca->next != NULL) {
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200130 ca = ca->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000132 ret = -1;
133 goto exit;
134 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200135 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000136
137exit:
138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 if (ret != 0) {
140 mbedtls_x509_crt_free(first);
141 mbedtls_free(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000142 first = NULL;
143 }
144
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200145 *candidates = first;
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 return ret;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200147}
Michael Schustera3cc4632024-06-07 01:51:54 +0200148#endif /* MBEDTLS_X509_CRL_PARSE_C && MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200149
Michael Schuster54300d42024-06-04 02:30:22 +0200150static int verify_fatal(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200151{
152 int *levels = (int *) data;
153
154 ((void) crt);
155 ((void) certificate_depth);
156
157 /* Simulate a fatal error in the callback */
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 if (*levels & (1 << certificate_depth)) {
159 *flags |= (1 << certificate_depth);
160 return -1 - certificate_depth;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200161 }
162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 return 0;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200164}
165
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900166/* strsep() not available on Windows */
Michael Schuster54300d42024-06-04 02:30:22 +0200167static char *mystrsep(char **stringp, const char *delim)
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900168{
169 const char *p;
170 char *ret = *stringp;
171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 if (*stringp == NULL) {
173 return NULL;
174 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 for (;; (*stringp)++) {
177 if (**stringp == '\0') {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900178 *stringp = NULL;
179 goto done;
180 }
181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 for (p = delim; *p != '\0'; p++) {
183 if (**stringp == *p) {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900184 **stringp = '\0';
185 (*stringp)++;
186 goto done;
187 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900189 }
190
191done:
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return ret;
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900193}
194
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200195typedef struct {
196 char buf[512];
197 char *p;
198} verify_print_context;
199
Michael Schuster54300d42024-06-04 02:30:22 +0200200static void verify_print_init(verify_print_context *ctx)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200201{
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 memset(ctx, 0, sizeof(verify_print_context));
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200203 ctx->p = ctx->buf;
204}
205
Michael Schuster54300d42024-06-04 02:30:22 +0200206static int verify_print(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200207{
208 int ret;
209 verify_print_context *ctx = (verify_print_context *) data;
210 char *p = ctx->p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 size_t n = ctx->buf + sizeof(ctx->buf) - ctx->p;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200212 ((void) flags);
213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 ret = mbedtls_snprintf(p, n, "depth %d - serial ", certificate_depth);
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_serial_gets(p, n, &crt->serial);
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, " - subject ");
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200221 MBEDTLS_X509_SAFE_SNPRINTF;
222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200224 MBEDTLS_X509_SAFE_SNPRINTF;
225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 ret = mbedtls_snprintf(p, n, " - flags 0x%08x\n", *flags);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200227 MBEDTLS_X509_SAFE_SNPRINTF;
228
229 ctx->p = p;
230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return 0;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200232}
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200233
Michael Schuster54300d42024-06-04 02:30:22 +0200234static int verify_parse_san(mbedtls_x509_subject_alternative_name *san,
Michael Schusterbd89b792024-06-04 02:41:10 +0200235 char **buf, size_t *size)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200236{
237 int ret;
238 size_t i;
239 char *p = *buf;
240 size_t n = *size;
241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 ret = mbedtls_snprintf(p, n, "type : %d", san->type);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200243 MBEDTLS_X509_SAFE_SNPRINTF;
244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 switch (san->type) {
246 case (MBEDTLS_X509_SAN_OTHER_NAME):
247 ret = mbedtls_snprintf(p, n, "\notherName :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300248 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
David Horstmanncfae6a12023-08-18 19:12:59 +0100251 &san->san.other_name.type_id) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 ret = mbedtls_snprintf(p, n, " hardware module name :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300253 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 ret = mbedtls_snprintf(p, n, " hardware type : ");
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_oid_get_numeric_string(p,
258 n,
Matthias Schulzedc32ea2023-10-19 16:09:08 +0200259 &san->san.other_name.value.hardware_module_name
260 .oid);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300261 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 ret = mbedtls_snprintf(p, n, ", hardware serial number : ");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300264 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 for (i = 0; i < san->san.other_name.value.hardware_module_name.val.len; i++) {
267 ret = mbedtls_snprintf(p,
268 n,
269 "%02X",
270 san->san.other_name.value.hardware_module_name.val.p[i]);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300271 MBEDTLS_X509_SAFE_SNPRINTF;
272 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200273 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 break;/* MBEDTLS_OID_ON_HW_MODULE_NAME */
275 case (MBEDTLS_X509_SAN_DNS_NAME):
276 ret = mbedtls_snprintf(p, n, "\ndNSName : ");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200277 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if (san->san.unstructured_name.len >= n) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200279 *p = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200281 }
282 n -= san->san.unstructured_name.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 for (i = 0; i < san->san.unstructured_name.len; i++) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200284 *p++ = san->san.unstructured_name.p[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 }
286 break;/* MBEDTLS_X509_SAN_DNS_NAME */
Przemek Stekiel608e3ef2023-02-09 14:47:50 +0100287 case (MBEDTLS_X509_SAN_RFC822_NAME):
288 ret = mbedtls_snprintf(p, n, "\nrfc822Name : ");
289 MBEDTLS_X509_SAFE_SNPRINTF;
290 if (san->san.unstructured_name.len >= n) {
291 *p = '\0';
292 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
293 }
294 n -= san->san.unstructured_name.len;
295 for (i = 0; i < san->san.unstructured_name.len; i++) {
296 *p++ = san->san.unstructured_name.p[i];
297 }
298 break;/* MBEDTLS_X509_SAN_RFC822_NAME */
Andrzej Kureke12b01d2023-01-10 06:47:38 -0500299 case (MBEDTLS_X509_SAN_DIRECTORY_NAME):
300 ret = mbedtls_snprintf(p, n, "\ndirectoryName : ");
301 MBEDTLS_X509_SAFE_SNPRINTF;
302 ret = mbedtls_x509_dn_gets(p, n, &san->san.directory_name);
303 if (ret < 0) {
304 return ret;
305 }
306
307 p += ret;
308 n -= ret;
309 break;/* MBEDTLS_X509_SAN_DIRECTORY_NAME */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200310 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 /*
312 * Should not happen.
313 */
314 return -1;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200315 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 ret = mbedtls_snprintf(p, n, "\n");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200317 MBEDTLS_X509_SAFE_SNPRINTF;
318
319 *size = n;
320 *buf = p;
321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200323}
Michael Schustera3cc4632024-06-07 01:51:54 +0200324#endif /* MBEDTLS_FS_IO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200325
Michael Schuster54300d42024-06-04 02:30:22 +0200326static int parse_crt_ext_cb(void *p_ctx, mbedtls_x509_crt const *crt, mbedtls_x509_buf const *oid,
Michael Schusterbd89b792024-06-04 02:41:10 +0200327 int critical, const unsigned char *cp, const unsigned char *end)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200328{
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 (void) crt;
330 (void) critical;
331 mbedtls_x509_buf *new_oid = (mbedtls_x509_buf *) p_ctx;
332 if (oid->tag == MBEDTLS_ASN1_OID &&
333 MBEDTLS_OID_CMP(MBEDTLS_OID_CERTIFICATE_POLICIES, oid) == 0) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200334 /* Handle unknown certificate policy */
335 int ret, parse_ret = 0;
336 size_t len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 unsigned char **p = (unsigned char **) &cp;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200338
339 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 ret = mbedtls_asn1_get_tag(p, end, &len,
341 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
342 if (ret != 0) {
343 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
344 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (*p + len != end) {
347 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
348 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
349 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200350
351 /*
352 * Cannot be an empty sequence.
353 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 if (len == 0) {
355 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
356 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
357 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 while (*p < end) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200360 const unsigned char *policy_end;
361
362 /*
363 * Get the policy sequence
364 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
366 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
367 0) {
368 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
369 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200370
371 policy_end = *p + len;
372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
374 MBEDTLS_ASN1_OID)) != 0) {
375 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
376 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200377
Nicola Di Lietob77fad82020-06-17 17:57:36 +0200378 /*
379 * Recognize exclusively the policy with OID 1
380 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if (len != 1 || *p[0] != 1) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200382 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200384
385 *p += len;
386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 /*
388 * If there is an optional qualifier, then *p < policy_end
389 * Check the Qualifier len to verify it doesn't exceed policy_end.
390 */
391 if (*p < policy_end) {
392 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
393 MBEDTLS_ASN1_CONSTRUCTED |
394 MBEDTLS_ASN1_SEQUENCE)) != 0) {
395 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
396 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200397 /*
398 * Skip the optional policy qualifiers.
399 */
400 *p += len;
401 }
402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (*p != policy_end) {
404 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
405 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
406 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200407 }
408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 if (*p != end) {
410 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
411 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
412 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 return parse_ret;
415 } else if (new_oid != NULL && new_oid->tag == oid->tag && new_oid->len == oid->len &&
416 memcmp(new_oid->p, oid->p, oid->len) == 0) {
417 return 0;
418 } else {
419 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
420 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200421 }
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200422}
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200423#endif /* MBEDTLS_X509_CRT_PARSE_C */
Matthias Schulzab408222023-10-18 13:20:59 +0200424
Michael Schustera3cc4632024-06-07 01:51:54 +0200425#if defined(MBEDTLS_X509_CSR_PARSE_C) && \
426 !defined(MBEDTLS_X509_REMOVE_INFO)
Michael Schusterf828f042024-06-07 06:47:31 +0200427static int parse_csr_ext_accept_cb(void *p_ctx,
428 mbedtls_x509_csr const *csr,
429 mbedtls_x509_buf const *oid,
430 int critical,
431 const unsigned char *cp,
432 const unsigned char *end)
Matthias Schulzab408222023-10-18 13:20:59 +0200433{
434 (void) p_ctx;
435 (void) csr;
436 (void) oid;
437 (void) critical;
438 (void) cp;
439 (void) end;
440
441 return 0;
442}
443
Michael Schusterf828f042024-06-07 06:47:31 +0200444static int parse_csr_ext_reject_cb(void *p_ctx,
445 mbedtls_x509_csr const *csr,
446 mbedtls_x509_buf const *oid,
447 int critical,
448 const unsigned char *cp,
449 const unsigned char *end)
Matthias Schulzab408222023-10-18 13:20:59 +0200450{
451 (void) p_ctx;
452 (void) csr;
453 (void) oid;
454 (void) critical;
455 (void) cp;
456 (void) end;
457
458 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
459 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
460}
Michael Schustera3cc4632024-06-07 01:51:54 +0200461#endif /* MBEDTLS_X509_CSR_PARSE_C && !MBEDTLS_X509_REMOVE_INFO */
Paul Bakker33b43f12013-08-20 11:48:36 +0200462/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000463
Thomas Daubney5c9c2ce2022-06-06 16:36:43 +0100464/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100465void x509_accessor_ext_types(int ext_type, int has_ext_type)
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100466{
467 mbedtls_x509_crt crt;
468 int expected_result = ext_type & has_ext_type;
469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200471 USE_PSA_INIT();
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100472
473 crt.ext_types = ext_type;
474
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500475 TEST_EQUAL(mbedtls_x509_crt_has_ext_type(&crt, has_ext_type), expected_result);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100476
valerio32f2ac92023-04-20 11:59:52 +0200477exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200479 USE_PSA_DONE();
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100480}
481/* END_CASE */
482
Glenn Strauss6f545ac2022-10-25 15:02:14 -0400483/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_TEST_HOOKS */
484void x509_crt_parse_cn_inet_pton(const char *cn, data_t *exp, int ref_ret)
485{
486 uint32_t addr[4];
487 size_t addrlen = mbedtls_x509_crt_parse_cn_inet_pton(cn, addr);
488 TEST_EQUAL(addrlen, (size_t) ref_ret);
489
490 if (addrlen) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100491 TEST_MEMORY_COMPARE(exp->x, exp->len, addr, addrlen);
Glenn Strauss6f545ac2022-10-25 15:02:14 -0400492 }
493}
494/* END_CASE */
495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500497void x509_parse_san(char *crt_file, char *result_str, int parse_result)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200498{
Ron Eldor890819a2019-05-13 19:03:04 +0300499 int ret;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200500 mbedtls_x509_crt crt;
Ron Eldor890819a2019-05-13 19:03:04 +0300501 mbedtls_x509_subject_alternative_name san;
502 mbedtls_x509_sequence *cur = NULL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200503 char buf[2000];
504 char *p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 size_t n = sizeof(buf);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200508 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 memset(buf, 0, 2000);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200510
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500511 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), parse_result);
Ron Eldor890819a2019-05-13 19:03:04 +0300512
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500513 if (parse_result != 0) {
514 goto exit;
515 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 if (crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Ron Eldor890819a2019-05-13 19:03:04 +0300517 cur = &crt.subject_alt_names;
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 while (cur != NULL) {
519 ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
520 TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
Ron Eldor890819a2019-05-13 19:03:04 +0300521 /*
522 * If san type not supported, ignore.
523 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (ret == 0) {
Andrzej Kurekd40c2b62023-02-13 07:01:59 -0500525 ret = verify_parse_san(&san, &p, &n);
526 mbedtls_x509_free_subject_alt_name(&san);
527 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 }
Ron Eldor890819a2019-05-13 19:03:04 +0300529 cur = cur->next;
530 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200531 }
532
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500533 TEST_EQUAL(strcmp(buf, result_str), 0);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200534
535exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200537 USE_PSA_DONE();
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200538}
539/* END_CASE */
540
Hanno Becker612a2f12020-10-09 09:19:39 +0100541/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100542void x509_cert_info(char *crt_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000543{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000545 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000546 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000547
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200549 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000551
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500552 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 res = mbedtls_x509_crt_info(buf, 2000, "", &crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 TEST_ASSERT(res != -1);
556 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000557
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500558 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200559
560exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200562 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000563}
Paul Bakker33b43f12013-08-20 11:48:36 +0200564/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000565
Hanno Becker612a2f12020-10-09 09:19:39 +0100566/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100567void mbedtls_x509_crl_info(char *crl_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000568{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000570 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000571 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200574 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000576
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500577 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 res = mbedtls_x509_crl_info(buf, 2000, "", &crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 TEST_ASSERT(res != -1);
581 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000582
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500583 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200584
585exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +0200587 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000588}
Paul Bakker33b43f12013-08-20 11:48:36 +0200589/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000590
Andres AGa39db392016-12-08 17:10:38 +0000591/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100592void mbedtls_x509_crl_parse(char *crl_file, int result)
Andres AGa39db392016-12-08 17:10:38 +0000593{
594 mbedtls_x509_crl crl;
595 char buf[2000];
596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200598 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 memset(buf, 0, 2000);
Andres AGa39db392016-12-08 17:10:38 +0000600
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500601 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), result);
Andres AGa39db392016-12-08 17:10:38 +0000602
603exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +0200605 USE_PSA_DONE();
Andres AGa39db392016-12-08 17:10:38 +0000606}
607/* END_CASE */
608
Hanno Becker612a2f12020-10-09 09:19:39 +0100609/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100610void mbedtls_x509_csr_info(char *csr_file, char *result_str)
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100611{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100613 char buf[2000];
614 int res;
615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +0200617 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 memset(buf, 0, 2000);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100619
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500620 TEST_EQUAL(mbedtls_x509_csr_parse_file(&csr, csr_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 res = mbedtls_x509_csr_info(buf, 2000, "", &csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100622
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 TEST_ASSERT(res != -1);
624 TEST_ASSERT(res != -2);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100625
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500626 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200627
628exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +0200630 USE_PSA_DONE();
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100631}
632/* END_CASE */
633
Hanno Becker612a2f12020-10-09 09:19:39 +0100634/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100635void x509_verify_info(int flags, char *prefix, char *result_str)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100636{
637 char buf[2000];
638 int res;
639
Valerio Setti569c1712023-04-19 14:53:36 +0200640 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 memset(buf, 0, sizeof(buf));
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 res = mbedtls_x509_crt_verify_info(buf, sizeof(buf), prefix, flags);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 TEST_ASSERT(res >= 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100646
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500647 TEST_EQUAL(strcmp(buf, result_str), 0);
valerio32f2ac92023-04-20 11:59:52 +0200648
649exit:
Valerio Setti569c1712023-04-19 14:53:36 +0200650 USE_PSA_DONE();
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100651}
652/* END_CASE */
653
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200654/* 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 +0100655void x509_verify_restart(char *crt_file, char *ca_file,
656 int result, int flags_result,
657 int max_ops, int min_restart, int max_restart)
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200658{
659 int ret, cnt_restart;
660 mbedtls_x509_crt_restart_ctx rs_ctx;
661 mbedtls_x509_crt crt;
662 mbedtls_x509_crt ca;
663 uint32_t flags = 0;
664
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200665 /*
666 * See comments on ecp_test_vect_restart() for op count precision.
667 *
Gilles Peskinee820c0a2023-08-03 17:45:20 +0200668 * For reference, with Mbed TLS 2.6 and default settings:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200669 * - ecdsa_verify() for P-256: ~ 6700
670 * - ecdsa_verify() for P-384: ~ 18800
671 * - x509_verify() for server5 -> test-ca2: ~ 18800
672 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
673 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 mbedtls_x509_crt_restart_init(&rs_ctx);
675 mbedtls_x509_crt_init(&crt);
676 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200677 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200678
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500679 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
680 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 mbedtls_ecp_set_max_ops(max_ops);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200683
684 cnt_restart = 0;
685 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
687 &mbedtls_x509_crt_profile_default, NULL, &flags,
688 NULL, NULL, &rs_ctx);
689 } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200690
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500691 TEST_EQUAL(ret, result);
692 TEST_EQUAL(flags, (uint32_t) flags_result);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 TEST_ASSERT(cnt_restart >= min_restart);
695 TEST_ASSERT(cnt_restart <= max_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200696
697 /* Do we leak memory when aborting? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
699 &mbedtls_x509_crt_profile_default, NULL, &flags,
700 NULL, NULL, &rs_ctx);
701 TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200702
703exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 mbedtls_x509_crt_restart_free(&rs_ctx);
705 mbedtls_x509_crt_free(&crt);
706 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100707 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200708}
709/* END_CASE */
710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100712void x509_verify(char *crt_file, char *ca_file, char *crl_file,
713 char *cn_name_str, int result, int flags_result,
714 char *profile_str,
715 char *verify_callback)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000716{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 mbedtls_x509_crt crt;
718 mbedtls_x509_crt ca;
719 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200720 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000721 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200722 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 char *cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200724 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 mbedtls_x509_crt_init(&crt);
727 mbedtls_x509_crt_init(&ca);
728 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200729 MD_OR_USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if (strcmp(cn_name_str, "NULL") != 0) {
Paul Bakker33b43f12013-08-20 11:48:36 +0200732 cn_name = cn_name_str;
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 if (strcmp(profile_str, "") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200736 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 } else if (strcmp(profile_str, "next") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200738 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 } else if (strcmp(profile_str, "suite_b") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200740 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 } else if (strcmp(profile_str, "compat") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200742 profile = &compat_profile;
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 } else if (strcmp(profile_str, "all") == 0) {
Hanno Becker20a4ade2019-06-03 14:27:03 +0100744 profile = &profile_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100746 TEST_FAIL("Unknown algorithm profile");
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 }
Gilles Peskineef86ab22017-05-05 18:59:02 +0200748
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 if (strcmp(verify_callback, "NULL") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200750 f_vrfy = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 } else if (strcmp(verify_callback, "verify_none") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200752 f_vrfy = verify_none;
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 } else if (strcmp(verify_callback, "verify_all") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200754 f_vrfy = verify_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100756 TEST_FAIL("No known verify callback selected");
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200758
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500759 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
760 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
761 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 res = mbedtls_x509_crt_verify_with_profile(&crt,
764 &ca,
765 &crl,
766 profile,
767 cn_name,
768 &flags,
769 f_vrfy,
770 NULL);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200771
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 TEST_EQUAL(res, result);
773 TEST_EQUAL(flags, (uint32_t) flags_result);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200774
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200775#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker0350d562019-03-28 14:23:36 +0000776 /* CRLs aren't supported with CA callbacks, so skip the CA callback
Jarno Lamsadfd22c42019-04-01 15:18:53 +0300777 * version of the test if CRLs are in use. */
Gilles Peskinebb7d92c2023-10-17 17:26:44 +0200778 if (strcmp(crl_file, "") == 0) {
Hanno Becker0350d562019-03-28 14:23:36 +0000779 flags = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 res = mbedtls_x509_crt_verify_with_ca_cb(&crt,
782 ca_callback,
783 &ca,
784 profile,
785 cn_name,
786 &flags,
787 f_vrfy,
788 NULL);
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200789
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500790 TEST_EQUAL(res, result);
791 TEST_EQUAL(flags, (uint32_t) (flags_result));
Hanno Becker0350d562019-03-28 14:23:36 +0000792 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200793#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200794exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 mbedtls_x509_crt_free(&crt);
796 mbedtls_x509_crt_free(&ca);
797 mbedtls_x509_crl_free(&crl);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100798 MD_OR_USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000799}
Paul Bakker33b43f12013-08-20 11:48:36 +0200800/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000801
Jarno Lamsa557426a2019-03-27 17:08:29 +0200802/* 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 +0100803void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
804 int exp_ret)
Jarno Lamsa557426a2019-03-27 17:08:29 +0200805{
806 int ret;
807 mbedtls_x509_crt crt;
808 mbedtls_x509_crt ca;
809 uint32_t flags = 0;
Hanno Becker3f932bb2019-03-28 17:06:47 +0000810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 mbedtls_x509_crt_init(&crt);
812 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200813 USE_PSA_INIT();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200814
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500815 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
816 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 if (strcmp(name, "NULL") == 0) {
Jarno Lamsa557426a2019-03-27 17:08:29 +0200819 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca,
823 &compat_profile, name, &flags,
824 NULL, NULL);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200825
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500826 TEST_EQUAL(ret, exp_ret);
827 TEST_EQUAL(flags, (uint32_t) (-1));
Jarno Lamsa557426a2019-03-27 17:08:29 +0200828exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 mbedtls_x509_crt_free(&crt);
830 mbedtls_x509_crt_free(&ca);
Valerio Setti569c1712023-04-19 14:53:36 +0200831 USE_PSA_DONE();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200832}
833/* END_CASE */
834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100836void x509_verify_callback(char *crt_file, char *ca_file, char *name,
837 int exp_ret, char *exp_vrfy_out)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200838{
839 int ret;
840 mbedtls_x509_crt crt;
841 mbedtls_x509_crt ca;
842 uint32_t flags = 0;
843 verify_print_context vrfy_ctx;
844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 mbedtls_x509_crt_init(&crt);
846 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200847 MD_OR_USE_PSA_INIT();
848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 verify_print_init(&vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200850
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500851 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
852 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if (strcmp(name, "NULL") == 0) {
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200855 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 }
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200857
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL,
859 &compat_profile,
860 name, &flags,
861 verify_print, &vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200862
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500863 TEST_EQUAL(ret, exp_ret);
864 TEST_EQUAL(strcmp(vrfy_ctx.buf, exp_vrfy_out), 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200865
866exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 mbedtls_x509_crt_free(&crt);
868 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100869 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200870}
871/* END_CASE */
872
Hanno Becker612a2f12020-10-09 09:19:39 +0100873/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100874void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
875 char *new_subject_ou,
876 char *result_str,
877 int ret)
Werner Lewis31ecb962022-06-17 15:51:55 +0100878{
879 mbedtls_x509_crt crt;
880 char buf[2000];
881 int res = 0;
882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200884 USE_PSA_INIT();
885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 memset(buf, 0, 2000);
Werner Lewis31ecb962022-06-17 15:51:55 +0100887
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500888 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100889 crt.subject.next->val.p = (unsigned char *) new_subject_ou;
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 crt.subject.next->val.len = strlen(new_subject_ou);
Werner Lewis31ecb962022-06-17 15:51:55 +0100891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
Werner Lewis31ecb962022-06-17 15:51:55 +0100893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if (ret != 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500895 TEST_EQUAL(res, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 } else {
897 TEST_ASSERT(res != -1);
898 TEST_ASSERT(res != -2);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500899 TEST_EQUAL(strcmp(buf, result_str), 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100900 }
901exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200903 USE_PSA_DONE();
Werner Lewis31ecb962022-06-17 15:51:55 +0100904}
905/* END_CASE */
906
907/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000909{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000911 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200912 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200915 USE_PSA_INIT();
916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000918
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500919 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 if (strcmp(entity, "subject") == 0) {
921 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
922 } else if (strcmp(entity, "issuer") == 0) {
923 res = mbedtls_x509_dn_gets(buf, 2000, &crt.issuer);
924 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100925 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 TEST_ASSERT(res != -1);
929 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000930
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500931 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200932
933exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200935 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000936}
Paul Bakker33b43f12013-08-20 11:48:36 +0200937/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000938
David Horstmanndb73d3b2022-10-04 16:49:16 +0100939/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100940void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
David Horstmanndb73d3b2022-10-04 16:49:16 +0100941{
valerioe50831c2023-04-20 14:48:19 +0200942 unsigned char *name = NULL;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100943 unsigned char *p;
944 size_t name_len;
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100945 mbedtls_x509_name head;
David Horstmann3cd67582022-10-17 17:59:10 +0100946 int ret;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100947
Valerio Setti569c1712023-04-19 14:53:36 +0200948 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 memset(&head, 0, sizeof(head));
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100950
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100952 p = name;
953
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 ret = mbedtls_x509_get_name(&p, (name + name_len), &head);
955 if (ret == 0) {
956 mbedtls_asn1_free_named_data_list_shallow(head.next);
957 }
David Horstmanndb73d3b2022-10-04 16:49:16 +0100958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 TEST_EQUAL(ret, exp_ret);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100960
valerio32f2ac92023-04-20 11:59:52 +0200961exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 mbedtls_free(name);
Valerio Setti569c1712023-04-19 14:53:36 +0200963 USE_PSA_DONE();
David Horstmanndb73d3b2022-10-04 16:49:16 +0100964}
965/* END_CASE */
966
Werner Lewisb3acb052022-06-17 15:59:58 +0100967/* 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 +0100968void mbedtls_x509_dn_get_next(char *name_str,
969 int next_merged,
970 char *expected_oids,
971 int exp_count,
972 char *exp_dn_gets)
Werner Lewisb3acb052022-06-17 15:59:58 +0100973{
974 int ret = 0, i;
Werner Lewisac80a662022-06-23 11:58:02 +0100975 size_t len = 0, out_size;
Werner Lewisb3acb052022-06-17 15:59:58 +0100976 mbedtls_asn1_named_data *names = NULL;
Gilles Peskine21e46b32023-10-17 16:35:20 +0200977 mbedtls_x509_name parsed;
978 memset(&parsed, 0, sizeof(parsed));
979 mbedtls_x509_name *parsed_cur;
Werner Lewisac80a662022-06-23 11:58:02 +0100980 // Size of buf is maximum required for test cases
Gilles Peskinef2574202023-10-18 17:39:48 +0200981 unsigned char buf[80] = { 0 };
Gilles Peskine21e46b32023-10-17 16:35:20 +0200982 unsigned char *out = NULL;
983 unsigned char *c = buf + sizeof(buf);
Werner Lewisb3acb052022-06-17 15:59:58 +0100984 const char *short_name;
985
Valerio Setti569c1712023-04-19 14:53:36 +0200986 USE_PSA_INIT();
Gilles Peskine21e46b32023-10-17 16:35:20 +0200987
Werner Lewisac80a662022-06-23 11:58:02 +0100988 // Additional size required for trailing space
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 out_size = strlen(expected_oids) + 2;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100990 TEST_CALLOC(out, out_size);
Werner Lewisb3acb052022-06-17 15:59:58 +0100991
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 TEST_EQUAL(mbedtls_x509_string_to_names(&names, name_str), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 ret = mbedtls_x509_write_names(&c, buf, names);
995 TEST_LE_S(0, ret);
Werner Lewisb3acb052022-06-17 15:59:58 +0100996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
998 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
999 TEST_EQUAL(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +01001000
1001 // Iterate over names and set next_merged nodes
1002 parsed_cur = &parsed;
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 for (; next_merged != 0 && parsed_cur != NULL; next_merged = next_merged >> 1) {
Werner Lewis12657cd2022-06-20 11:47:57 +01001004 parsed_cur->next_merged = next_merged & 0x01;
Werner Lewisb3acb052022-06-17 15:59:58 +01001005 parsed_cur = parsed_cur->next;
1006 }
1007
1008 // Iterate over RDN nodes and print OID of first element to buffer
1009 parsed_cur = &parsed;
1010 len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 for (i = 0; parsed_cur != NULL; i++) {
1012 TEST_EQUAL(mbedtls_oid_get_attr_short_name(&parsed_cur->oid,
1013 &short_name), 0);
1014 len += mbedtls_snprintf((char *) out + len, out_size - len, "%s ", short_name);
1015 parsed_cur = mbedtls_x509_dn_get_next(parsed_cur);
Werner Lewisb3acb052022-06-17 15:59:58 +01001016 }
1017 out[len-1] = 0;
1018
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 TEST_EQUAL(exp_count, i);
1020 TEST_EQUAL(strcmp((char *) out, expected_oids), 0);
1021 mbedtls_free(out);
Werner Lewisac80a662022-06-23 11:58:02 +01001022 out = NULL;
Werner Lewisb3acb052022-06-17 15:59:58 +01001023
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 out_size = strlen(exp_dn_gets) + 1;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001025 TEST_CALLOC(out, out_size);
Werner Lewisac80a662022-06-23 11:58:02 +01001026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 TEST_LE_S(0, mbedtls_x509_dn_gets((char *) out, out_size, &parsed));
1028 TEST_EQUAL(strcmp((char *) out, exp_dn_gets), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +01001029exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 mbedtls_free(out);
1031 mbedtls_asn1_free_named_data_list(&names);
1032 mbedtls_asn1_free_named_data_list_shallow(parsed.next);
Valerio Setti569c1712023-04-19 14:53:36 +02001033 USE_PSA_DONE();
Werner Lewisb3acb052022-06-17 15:59:58 +01001034}
Werner Lewisb3acb052022-06-17 15:59:58 +01001035/* END_CASE */
1036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001038void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
Paul Bakker37940d9f2009-07-10 22:38:58 +00001039{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +00001041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001043 USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +00001044
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001045 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 if (strcmp(entity, "valid_from") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001048 TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_from), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 } else if (strcmp(entity, "valid_to") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001050 TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_to), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +01001052 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001054
Paul Bakkerbd51b262014-07-10 15:26:12 +02001055exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001057 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +00001058}
Paul Bakker33b43f12013-08-20 11:48:36 +02001059/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +00001060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001062void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001063{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001067 USE_PSA_INIT();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001068
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001069 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if (strcmp(entity, "valid_from") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001072 TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_from), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 } else if (strcmp(entity, "valid_to") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001074 TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_to), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +01001076 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001078
Paul Bakkerbd51b262014-07-10 15:26:12 +02001079exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001081 USE_PSA_DONE();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001082}
1083/* END_CASE */
1084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001086void x509parse_crt_file(char *crt_file, int result)
Paul Bakker5a5fa922014-09-26 14:53:04 +02001087{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +02001089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001091 USE_PSA_INIT();
Paul Bakker5a5fa922014-09-26 14:53:04 +02001092
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001093 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), result);
Paul Bakker5a5fa922014-09-26 14:53:04 +02001094
1095exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001097 USE_PSA_DONE();
Paul Bakker5a5fa922014-09-26 14:53:04 +02001098}
1099/* END_CASE */
1100
Minos Galanakisa83ada42024-01-19 10:59:07 +00001101/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
1102void mbedtls_x509_get_ca_istrue(char *crt_file, int result)
1103{
1104 mbedtls_x509_crt crt;
1105 mbedtls_x509_crt_init(&crt);
1106 USE_PSA_INIT();
1107
1108 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
1109 TEST_EQUAL(mbedtls_x509_crt_get_ca_istrue(&crt), result);
1110exit:
1111 mbedtls_x509_crt_free(&crt);
1112 USE_PSA_DONE();
1113}
1114/* END_CASE */
1115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001117void x509parse_crt(data_t *buf, char *result_str, int result)
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001118{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 mbedtls_x509_crt crt;
Hanno Becker612a2f12020-10-09 09:19:39 +01001120#if !defined(MBEDTLS_X509_REMOVE_INFO)
Hanno Beckerfff2d572020-10-09 09:45:19 +01001121 unsigned char output[2000] = { 0 };
Azim Khanf1aaec92017-05-30 14:23:15 +01001122 int res;
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001123#else
1124 ((void) result_str);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001125#endif
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001128 USE_PSA_INIT();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001129
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001130 TEST_EQUAL(mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len), result);
Hanno Becker612a2f12020-10-09 09:19:39 +01001131#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 if ((result) == 0) {
1133 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1134 TEST_ASSERT(res != -1);
1135 TEST_ASSERT(res != -2);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001136
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001137 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001138 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001140#endif
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 mbedtls_x509_crt_free(&crt);
1143 mbedtls_x509_crt_init(&crt);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001144
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001145 TEST_EQUAL(mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len), result);
Hanno Becker612a2f12020-10-09 09:19:39 +01001146#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 if ((result) == 0) {
1148 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakker33b43f12013-08-20 11:48:36 +02001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 TEST_ASSERT(res != -1);
1153 TEST_ASSERT(res != -2);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001154
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001155 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001156 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001158#endif /* !MBEDTLS_X509_REMOVE_INFO */
Paul Bakkerb08e6842012-02-11 18:43:20 +00001159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 mbedtls_x509_crt_free(&crt);
1161 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001162
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001163 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL, NULL),
1164 result);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001165#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if ((result) == 0) {
1167 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 TEST_ASSERT(res != -1);
1170 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001171
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001172 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001173 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001175#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 mbedtls_x509_crt_free(&crt);
1178 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001179
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001180 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL, NULL),
1181 result);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001182#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 if ((result) == 0) {
1184 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 TEST_ASSERT(res != -1);
1187 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001188
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001189 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001190 }
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001191#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001192
1193exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001195 USE_PSA_DONE();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001196}
1197/* END_CASE */
1198
1199/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001200void x509parse_crt_cb(data_t *buf, char *result_str, int result)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001201{
1202 mbedtls_x509_crt crt;
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001203 mbedtls_x509_buf oid;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001204
1205#if !defined(MBEDTLS_X509_REMOVE_INFO)
1206 unsigned char output[2000] = { 0 };
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001207 int res;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001208#else
1209 ((void) result_str);
1210#endif
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001211
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001212 oid.tag = MBEDTLS_ASN1_OID;
1213 oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001217 USE_PSA_INIT();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001218
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001219 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
1220 &oid), result);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001221#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 if ((result) == 0) {
1223 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 TEST_ASSERT(res != -1);
1226 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001227
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001228 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001229 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 memset(output, 0, 2000);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001231#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001232
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 mbedtls_x509_crt_free(&crt);
1234 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001235
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001236 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1237 &oid), (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001238#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 if ((result) == 0) {
1240 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 TEST_ASSERT(res != -1);
1243 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001244
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001245 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001246 }
Hanno Beckerfff2d572020-10-09 09:45:19 +01001247#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001248
Paul Bakkerbd51b262014-07-10 15:26:12 +02001249exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001251 USE_PSA_DONE();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001252}
Paul Bakker33b43f12013-08-20 11:48:36 +02001253/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001254
Hanno Becker612a2f12020-10-09 09:19:39 +01001255/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001256void x509parse_crl(data_t *buf, char *result_str, int result)
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001257{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001259 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +01001260 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +02001263 USE_PSA_INIT();
1264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 memset(output, 0, 2000);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001266
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001267
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001268 TEST_EQUAL(mbedtls_x509_crl_parse(&crl, buf->x, buf->len), (result));
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 if ((result) == 0) {
1270 res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
Paul Bakker33b43f12013-08-20 11:48:36 +02001271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 TEST_ASSERT(res != -1);
1273 TEST_ASSERT(res != -2);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001274
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001275 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001276 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001277
Paul Bakkerbd51b262014-07-10 15:26:12 +02001278exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +02001280 USE_PSA_DONE();
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001281}
Paul Bakker33b43f12013-08-20 11:48:36 +02001282/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001283
Hanno Becker612a2f12020-10-09 09:19:39 +01001284/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001285void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001286{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001288 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001289 int my_ret;
1290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001292 USE_PSA_INIT();
1293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 memset(my_out, 0, sizeof(my_out));
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001297 TEST_EQUAL(my_ret, ref_ret);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 if (ref_ret == 0) {
1300 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001301 TEST_EQUAL(my_out_len, strlen(ref_out));
1302 TEST_EQUAL(strcmp(my_out, ref_out), 0);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001303 }
1304
Paul Bakkerbd51b262014-07-10 15:26:12 +02001305exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001307 USE_PSA_DONE();
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001308}
1309/* END_CASE */
1310
Matthias Schulzab408222023-10-18 13:20:59 +02001311/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1312void mbedtls_x509_csr_parse_with_ext_cb(data_t *csr_der, char *ref_out, int ref_ret, int accept)
1313{
1314 mbedtls_x509_csr csr;
1315 char my_out[1000];
1316 int my_ret;
1317
1318 mbedtls_x509_csr_init(&csr);
1319 USE_PSA_INIT();
1320
1321 memset(my_out, 0, sizeof(my_out));
1322
1323 my_ret = mbedtls_x509_csr_parse_der_with_ext_cb(&csr, csr_der->x, csr_der->len,
1324 accept ? parse_csr_ext_accept_cb :
Matthias Schulzedc32ea2023-10-19 16:09:08 +02001325 parse_csr_ext_reject_cb,
Matthias Schulzab408222023-10-18 13:20:59 +02001326 NULL);
1327 TEST_EQUAL(my_ret, ref_ret);
1328
1329 if (ref_ret == 0) {
1330 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1331 TEST_EQUAL(my_out_len, strlen(ref_out));
1332 TEST_EQUAL(strcmp(my_out, ref_out), 0);
1333 }
1334
1335exit:
1336 mbedtls_x509_csr_free(&csr);
1337 USE_PSA_DONE();
1338}
1339/* END_CASE */
1340
Przemek Stekield7992df2023-01-25 16:19:50 +01001341/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1342void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
1343{
1344 mbedtls_x509_csr csr;
1345 char my_out[1000];
1346 int my_ret;
1347
1348 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001349 USE_PSA_INIT();
1350
Przemek Stekield7992df2023-01-25 16:19:50 +01001351 memset(my_out, 0, sizeof(my_out));
1352
1353 my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001354 TEST_EQUAL(my_ret, ref_ret);
Przemek Stekield7992df2023-01-25 16:19:50 +01001355
1356 if (ref_ret == 0) {
1357 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001358 TEST_EQUAL(my_out_len, strlen(ref_out));
1359 TEST_EQUAL(strcmp(my_out, ref_out), 0);
Przemek Stekield7992df2023-01-25 16:19:50 +01001360 }
1361
1362exit:
1363 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001364 USE_PSA_DONE();
Przemek Stekield7992df2023-01-25 16:19:50 +01001365}
1366/* END_CASE */
1367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1e5fec62023-04-13 18:13:48 +02001369void mbedtls_x509_crt_parse_file(char *crt_path, int ret, int nb_crt)
1370{
1371 mbedtls_x509_crt chain, *cur;
1372 int i;
1373
1374 mbedtls_x509_crt_init(&chain);
1375 USE_PSA_INIT();
1376
1377 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, crt_path), ret);
1378
1379 /* Check how many certs we got */
1380 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1381 if (cur->raw.p != NULL) {
1382 i++;
1383 }
1384 }
1385
1386 TEST_EQUAL(i, nb_crt);
1387
1388exit:
1389 mbedtls_x509_crt_free(&chain);
1390 USE_PSA_DONE();
1391}
1392/* END_CASE */
1393
1394/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001395void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001396{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001398 int i;
1399
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001401 USE_PSA_INIT();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001402
Gilles Peskine55ad28a2023-04-13 18:14:45 +02001403 TEST_EQUAL(mbedtls_x509_crt_parse_path(&chain, crt_path), ret);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001404
1405 /* Check how many certs we got */
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1407 if (cur->raw.p != NULL) {
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001408 i++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 }
1410 }
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001411
Gilles Peskine55ad28a2023-04-13 18:14:45 +02001412 TEST_EQUAL(i, nb_crt);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001413
Paul Bakkerbd51b262014-07-10 15:26:12 +02001414exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 mbedtls_x509_crt_free(&chain);
Valerio Setti569c1712023-04-19 14:53:36 +02001416 USE_PSA_DONE();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001417}
1418/* END_CASE */
1419
Janos Follath822b2c32015-10-11 10:25:22 +02001420/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001421void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1422 int ret_chk, int flags_chk)
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001423{
1424 char file_buf[128];
1425 int ret;
1426 uint32_t flags;
1427 mbedtls_x509_crt trusted, chain;
1428
1429 /*
1430 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1431 * with NN.crt signed by NN-1.crt
1432 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 mbedtls_x509_crt_init(&trusted);
1434 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001435 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001436
1437 /* Load trusted root */
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001438 TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, ca_file), 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001439
1440 /* Load a chain with nb_int intermediates (from 01 to nb_int),
1441 * plus one "end-entity" cert (nb_int + 1) */
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001442 ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 nb_int + 1);
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001444 TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001445 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, file_buf), 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001446
1447 /* Try to verify that chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1449 NULL, NULL);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001450 TEST_EQUAL(ret, ret_chk);
1451 TEST_EQUAL(flags, (uint32_t) flags_chk);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001452
1453exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 mbedtls_x509_crt_free(&chain);
1455 mbedtls_x509_crt_free(&trusted);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001456 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001457}
1458/* END_CASE */
1459
1460/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001461void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1462 int flags_result, int result,
1463 char *profile_name, int vrfy_fatal_lvls)
Janos Follath822b2c32015-10-11 10:25:22 +02001464{
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 char *act;
Janos Follath822b2c32015-10-11 10:25:22 +02001466 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001467 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001468 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001469 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +02001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 mbedtls_x509_crt_init(&chain);
1472 mbedtls_x509_crt_init(&trusted);
valerio32f2ac92023-04-20 11:59:52 +02001473 MD_OR_USE_PSA_INIT();
Janos Follath822b2c32015-10-11 10:25:22 +02001474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 while ((act = mystrsep(&chain_paths, " ")) != NULL) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001476 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, act), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 }
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001478 TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, trusted_ca), 0);
Janos Follath822b2c32015-10-11 10:25:22 +02001479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 if (strcmp(profile_name, "") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001481 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 } else if (strcmp(profile_name, "next") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001483 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 } else if (strcmp(profile_name, "suiteb") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001485 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 } else if (strcmp(profile_name, "rsa3072") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001487 profile = &profile_rsa3072;
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 } else if (strcmp(profile_name, "sha512") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001489 profile = &profile_sha512;
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 }
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001491
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1493 NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
Janos Follathef4f2582015-10-11 16:17:27 +02001494
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001495 TEST_EQUAL(res, (result));
1496 TEST_EQUAL(flags, (uint32_t) (flags_result));
Janos Follath822b2c32015-10-11 10:25:22 +02001497
1498exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 mbedtls_x509_crt_free(&trusted);
1500 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001501 MD_OR_USE_PSA_DONE();
Janos Follath822b2c32015-10-11 10:25:22 +02001502}
1503/* END_CASE */
1504
Hanno Becker612a2f12020-10-09 09:19:39 +01001505/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001506void x509_oid_desc(data_t *buf, char *ref_desc)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001507{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001509 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001510 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001511
Valerio Setti569c1712023-04-19 14:53:36 +02001512 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001515 oid.p = buf->x;
1516 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001517
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 ret = mbedtls_oid_get_extended_key_usage(&oid, &desc);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001519
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 if (strcmp(ref_desc, "notfound") == 0) {
1521 TEST_ASSERT(ret != 0);
1522 TEST_ASSERT(desc == NULL);
1523 } else {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001524 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 TEST_ASSERT(desc != NULL);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001526 TEST_EQUAL(strcmp(desc, ref_desc), 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001527 }
valerio32f2ac92023-04-20 11:59:52 +02001528
1529exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001530 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001531}
1532/* END_CASE */
1533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001535void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001536{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001538 char num_buf[100];
1539
Valerio Setti569c1712023-04-19 14:53:36 +02001540 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001541
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001542 memset(num_buf, 0x2a, sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001545 oid.p = oid_buf->x;
1546 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001547
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001548 TEST_ASSERT((size_t) blen <= sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001549
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001550 TEST_EQUAL(mbedtls_oid_get_numeric_string(num_buf, blen, &oid), ret);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 if (ret >= 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001553 TEST_EQUAL(num_buf[ret], 0);
1554 TEST_EQUAL(strcmp(num_buf, numstr), 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001555 }
valerio32f2ac92023-04-20 11:59:52 +02001556
1557exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001558 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001559}
1560/* END_CASE */
1561
TRodziewicz442fdc22021-06-07 13:52:23 +02001562/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001563void x509_check_key_usage(char *crt_file, int usage, int ret)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001564{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001568 USE_PSA_INIT();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001569
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001570 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001571
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001572 TEST_EQUAL(mbedtls_x509_crt_check_key_usage(&crt, usage), ret);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001573
Paul Bakkerbd51b262014-07-10 15:26:12 +02001574exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001576 USE_PSA_DONE();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001577}
1578/* END_CASE */
1579
TRodziewicz442fdc22021-06-07 13:52:23 +02001580/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001581void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1582 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001583{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001585
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001587 USE_PSA_INIT();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001588
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001589 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001590
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001591 TEST_EQUAL(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x, oid->len),
1592 ret);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001593
Paul Bakkerbd51b262014-07-10 15:26:12 +02001594exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001596 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001597}
1598/* END_CASE */
1599
Andres AG4b76aec2016-09-23 13:16:02 +01001600/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001601void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1602 int day, int hour, int min, int sec)
Andres AG4b76aec2016-09-23 13:16:02 +01001603{
1604 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001605 unsigned char buf[21];
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 unsigned char *start = buf;
1607 unsigned char *end = buf;
Andres AG4b76aec2016-09-23 13:16:02 +01001608
Valerio Setti569c1712023-04-19 14:53:36 +02001609 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 memset(&time, 0x00, sizeof(time));
1611 *end = (unsigned char) tag; end++;
1612 *end = strlen(time_str);
1613 TEST_ASSERT(*end < 20);
Andres AG4b76aec2016-09-23 13:16:02 +01001614 end++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 memcpy(end, time_str, (size_t) *(end - 1));
Andres AG4b76aec2016-09-23 13:16:02 +01001616 end += *(end - 1);
1617
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001618 TEST_EQUAL(mbedtls_x509_get_time(&start, end, &time), ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 if (ret == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001620 TEST_EQUAL(year, time.year);
1621 TEST_EQUAL(mon, time.mon);
1622 TEST_EQUAL(day, time.day);
1623 TEST_EQUAL(hour, time.hour);
1624 TEST_EQUAL(min, time.min);
1625 TEST_EQUAL(sec, time.sec);
Andres AG4b76aec2016-09-23 13:16:02 +01001626 }
valerio32f2ac92023-04-20 11:59:52 +02001627exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001628 USE_PSA_DONE();
Andres AG4b76aec2016-09-23 13:16:02 +01001629}
1630/* END_CASE */
1631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001633void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1634 int ref_msg_md, int ref_mgf_md,
1635 int ref_salt_len, int ref_ret)
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001636{
1637 int my_ret;
Ronald Cronac6ae352020-06-26 14:33:03 +02001638 mbedtls_x509_buf buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001640 int my_salt_len;
1641
Valerio Setti569c1712023-04-19 14:53:36 +02001642 USE_PSA_INIT();
1643
Ronald Cronac6ae352020-06-26 14:33:03 +02001644 buf.p = params->x;
1645 buf.len = params->len;
1646 buf.tag = params_tag;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001647
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1649 &my_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001650
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001651 TEST_EQUAL(my_ret, ref_ret);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 if (ref_ret == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001654 TEST_EQUAL(my_msg_md, (mbedtls_md_type_t) ref_msg_md);
1655 TEST_EQUAL(my_mgf_md, (mbedtls_md_type_t) ref_mgf_md);
1656 TEST_EQUAL(my_salt_len, ref_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001657 }
1658
Paul Bakkerbd51b262014-07-10 15:26:12 +02001659exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001660 USE_PSA_DONE();
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001661}
1662/* END_CASE */
toth92ga41954d2021-02-12 16:11:17 +01001663
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001664/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001665void x509_crt_parse_subjectkeyid(char *file, data_t *subjectKeyId, int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001666{
1667 mbedtls_x509_crt crt;
toth92ga41954d2021-02-12 16:11:17 +01001668
1669 mbedtls_x509_crt_init(&crt);
1670
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001671 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001672
toth92g357b2972021-05-04 15:41:35 +02001673 if (ref_ret == 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001674 TEST_EQUAL(crt.subject_key_id.tag, MBEDTLS_ASN1_OCTET_STRING);
1675 TEST_EQUAL(memcmp(crt.subject_key_id.p, subjectKeyId->x, subjectKeyId->len), 0);
1676 TEST_EQUAL(crt.subject_key_id.len, subjectKeyId->len);
toth92g357b2972021-05-04 15:41:35 +02001677 } else {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001678 TEST_EQUAL(crt.subject_key_id.tag, 0);
1679 TEST_EQUAL(crt.subject_key_id.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001680 }
1681
1682exit:
1683 mbedtls_x509_crt_free(&crt);
1684}
1685/* END_CASE */
1686
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001687/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Przemek Stekiel2568d472023-04-06 09:23:25 +02001688void x509_crt_parse_authoritykeyid(char *file,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001689 data_t *keyId,
toth92g5042b102021-05-06 08:22:17 +02001690 char *authorityKeyId_issuer,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001691 data_t *serial,
toth92g5042b102021-05-06 08:22:17 +02001692 int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001693{
1694 mbedtls_x509_crt crt;
Przemek Stekiel39dbe232023-04-03 10:19:22 +02001695 mbedtls_x509_subject_alternative_name san;
David Horstmann9a3a1a62023-06-22 16:59:09 +01001696 char name_buf[128];
toth92ga41954d2021-02-12 16:11:17 +01001697
1698 mbedtls_x509_crt_init(&crt);
1699
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001700 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001701
toth92g357b2972021-05-04 15:41:35 +02001702 if (ref_ret == 0) {
toth92ga41954d2021-02-12 16:11:17 +01001703 /* KeyId test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001704 if (keyId->len > 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001705 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, MBEDTLS_ASN1_OCTET_STRING);
1706 TEST_EQUAL(memcmp(crt.authority_key_id.keyIdentifier.p, keyId->x, keyId->len), 0);
1707 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, keyId->len);
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001708 } else {
1709 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1710 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001711 }
toth92ga41954d2021-02-12 16:11:17 +01001712
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001713
toth92ga41954d2021-02-12 16:11:17 +01001714 /* Issuer test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001715 if (strlen(authorityKeyId_issuer) > 0) {
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001716 mbedtls_x509_sequence *issuerPtr = &crt.authority_key_id.authorityCertIssuer;
Przemek Stekiel01984212023-02-09 09:29:34 +01001717
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001718 TEST_EQUAL(mbedtls_x509_parse_subject_alt_name(&issuerPtr->buf, &san), 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001719
David Horstmann9a3a1a62023-06-22 16:59:09 +01001720 TEST_ASSERT(mbedtls_x509_dn_gets(name_buf, sizeof(name_buf),
1721 &san.san.directory_name)
1722 > 0);
1723 TEST_EQUAL(strcmp(name_buf, authorityKeyId_issuer), 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001724
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001725 mbedtls_x509_free_subject_alt_name(&san);
toth92ga41954d2021-02-12 16:11:17 +01001726 }
1727
1728 /* Serial test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001729 if (serial->len > 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001730 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001731 MBEDTLS_ASN1_INTEGER);
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001732 TEST_EQUAL(memcmp(crt.authority_key_id.authorityCertSerialNumber.p,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001733 serial->x, serial->len), 0);
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001734 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, serial->len);
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001735 } else {
1736 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1737 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001738 }
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001739
toth92g357b2972021-05-04 15:41:35 +02001740 } else {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001741 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1742 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001743
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001744 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1745 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001746 }
1747
1748exit:
1749 mbedtls_x509_crt_free(&crt);
1750}
1751/* END_CASE */
Sam Berry4aee6a22024-07-19 15:00:41 +01001752
1753/* BEGIN_CASE depends_on:MBEDTLS_x509_USE_C */
1754void oid_get_numeric_string(data_t *oid, int error_ret, char *result_str)
1755{
1756 char buf[256];
1757 mbedtls_asn1_buf input_oid = { 0, 0, NULL };
1758 int ret;
1759
1760 input_oid.tag = MBEDTLS_ASN1_OID;
1761 /* Test that an empty OID is not dereferenced */
1762 input_oid.p = oid->len ? oid->x : (void *) 1;
1763 input_oid.len = oid->len;
1764
1765 ret = mbedtls_oid_get_numeric_string(buf, sizeof(buf), &input_oid);
1766
1767 if (error_ret == 0) {
1768 TEST_EQUAL(ret, strlen(result_str));
1769 TEST_ASSERT(ret >= 3);
1770 TEST_EQUAL(strcmp(buf, result_str), 0);
1771 } else {
1772 TEST_EQUAL(ret, error_ret);
1773 }
1774}
1775/* END_CASE */