blob: 079dca48c94903d65fcb7214b023eb46061227cb [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"
Gilles Peskinecd4c0d72025-05-07 23:45:12 +02009#include "mbedtls/oid.h"
Gilles Peskine86a47f82025-05-07 20:20:12 +020010#include "x509_oid.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000011#include "mbedtls/base64.h"
Chris Jones9f7a6932021-04-14 12:12:09 +010012#include "mbedtls/error.h"
Valerio Setti178b5bd2023-02-13 10:04:28 +010013#include "mbedtls/pk.h"
Ben Taylorc801d322025-07-03 15:01:39 +010014#if defined(MBEDTLS_PK_HAVE_PRIVATE_HEADER)
15#include <mbedtls/private/pk_private.h>
16#endif /* MBEDTLS_PK_HAVE_PRIVATE_HEADER */
Sam Berry4aee6a22024-07-19 15:00:41 +010017#include "mbedtls/asn1.h"
18#include "mbedtls/asn1write.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +010019#include "string.h"
Paul Bakkerb63b0af2011-01-13 17:54:59 +000020
Simon Butcher9e24b512017-07-28 12:15:13 +010021#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
Hanno Becker3b1422e2017-07-26 13:38:02 +010022#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
Gilles Peskine449bd832023-01-11 14:50:10 +010023 than the current threshold 19. To test larger values, please \
David Horstmann441b66c2024-07-01 16:39:39 +010024 adapt the script framework/data_files/dir-max/long.sh."
Hanno Becker3b1422e2017-07-26 13:38:02 +010025#endif
26
Hanno Becker20a4ade2019-06-03 14:27:03 +010027/* Test-only profile allowing all digests, PK algorithms, and curves. */
28const mbedtls_x509_crt_profile profile_all =
29{
30 0xFFFFFFFF, /* Any MD */
31 0xFFFFFFFF, /* Any PK alg */
32 0xFFFFFFFF, /* Any curve */
33 1024,
34};
35
Gilles Peskineef86ab22017-05-05 18:59:02 +020036/* Profile for backward compatibility. Allows SHA-1, unlike the default
37 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020038const mbedtls_x509_crt_profile compat_profile =
39{
Gilles Peskine449bd832023-01-11 14:50:10 +010040 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) |
41 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160) |
42 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
43 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
44 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
45 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubney28015e12022-04-21 08:12:59 +010046 0xFFFFFFFF, /* Any PK alg */
47 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020048 1024,
49};
50
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020051const mbedtls_x509_crt_profile profile_rsa3072 =
52{
Gilles Peskine449bd832023-01-11 14:50:10 +010053 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
54 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
55 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
56 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_RSA),
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020057 0,
58 3072,
59};
60
61const mbedtls_x509_crt_profile profile_sha512 =
62{
Gilles Peskine449bd832023-01-11 14:50:10 +010063 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubney28015e12022-04-21 08:12:59 +010064 0xFFFFFFFF, /* Any PK alg */
65 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020066 1024,
67};
68
Michael Schustera3cc4632024-06-07 01:51:54 +020069#if defined(MBEDTLS_X509_CRT_PARSE_C)
70
71#if defined(MBEDTLS_FS_IO)
Michael Schuster54300d42024-06-04 02:30:22 +020072static int verify_none(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000073{
Paul Bakker5a624082011-01-18 16:31:52 +000074 ((void) data);
75 ((void) crt);
76 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010077 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020078
Paul Bakker915275b2012-09-28 07:10:55 +000079 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000080}
81
Michael Schuster54300d42024-06-04 02:30:22 +020082static int verify_all(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000083{
Paul Bakker5a624082011-01-18 16:31:52 +000084 ((void) data);
85 ((void) crt);
86 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000087 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000088
Paul Bakkerb63b0af2011-01-13 17:54:59 +000089 return 0;
90}
91
Michael Schustera3cc4632024-06-07 01:51:54 +020092#if defined(MBEDTLS_X509_CRL_PARSE_C) && \
93 defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Michael Schusterf828f042024-06-07 06:47:31 +020094static int ca_callback_fail(void *data, mbedtls_x509_crt const *child,
95 mbedtls_x509_crt **candidates)
Jarno Lamsa557426a2019-03-27 17:08:29 +020096{
97 ((void) data);
98 ((void) child);
99 ((void) candidates);
100
101 return -1;
102}
Michael Schustera3cc4632024-06-07 01:51:54 +0200103
Michael Schuster54300d42024-06-04 02:30:22 +0200104static int ca_callback(void *data, mbedtls_x509_crt const *child,
Michael Schusterbd89b792024-06-04 02:41:10 +0200105 mbedtls_x509_crt **candidates)
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200106{
Hanno Beckercbb59032019-03-28 14:14:22 +0000107 int ret = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200108 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +0000109 mbedtls_x509_crt *first;
110
111 /* This is a test-only implementation of the CA callback
112 * which always returns the entire list of trusted certificates.
113 * Production implementations managing a large number of CAs
114 * should use an efficient presentation and lookup for the
115 * set of trusted certificates (such as a hashtable) and only
116 * return those trusted certificates which satisfy basic
117 * parental checks, such as the matching of child `Issuer`
118 * and parent `Subject` field. */
119 ((void) child);
120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
122 if (first == NULL) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000123 ret = -1;
124 goto exit;
125 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 mbedtls_x509_crt_init(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000129 ret = -1;
130 goto exit;
131 }
132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 while (ca->next != NULL) {
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200134 ca = ca->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000136 ret = -1;
137 goto exit;
138 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200139 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000140
141exit:
142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 if (ret != 0) {
144 mbedtls_x509_crt_free(first);
145 mbedtls_free(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000146 first = NULL;
147 }
148
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200149 *candidates = first;
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 return ret;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200151}
Michael Schustera3cc4632024-06-07 01:51:54 +0200152#endif /* MBEDTLS_X509_CRL_PARSE_C && MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200153
Michael Schuster54300d42024-06-04 02:30:22 +0200154static int verify_fatal(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200155{
156 int *levels = (int *) data;
157
158 ((void) crt);
159 ((void) certificate_depth);
160
161 /* Simulate a fatal error in the callback */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 if (*levels & (1 << certificate_depth)) {
163 *flags |= (1 << certificate_depth);
164 return -1 - certificate_depth;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200165 }
166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 return 0;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200168}
169
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900170/* strsep() not available on Windows */
Michael Schuster54300d42024-06-04 02:30:22 +0200171static char *mystrsep(char **stringp, const char *delim)
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900172{
173 const char *p;
174 char *ret = *stringp;
175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 if (*stringp == NULL) {
177 return NULL;
178 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 for (;; (*stringp)++) {
181 if (**stringp == '\0') {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900182 *stringp = NULL;
183 goto done;
184 }
185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 for (p = delim; *p != '\0'; p++) {
187 if (**stringp == *p) {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900188 **stringp = '\0';
189 (*stringp)++;
190 goto done;
191 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900193 }
194
195done:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return ret;
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900197}
198
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200199typedef struct {
200 char buf[512];
201 char *p;
202} verify_print_context;
203
Michael Schuster54300d42024-06-04 02:30:22 +0200204static void verify_print_init(verify_print_context *ctx)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200205{
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 memset(ctx, 0, sizeof(verify_print_context));
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200207 ctx->p = ctx->buf;
208}
209
Michael Schuster54300d42024-06-04 02:30:22 +0200210static int verify_print(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200211{
212 int ret;
213 verify_print_context *ctx = (verify_print_context *) data;
214 char *p = ctx->p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 size_t n = ctx->buf + sizeof(ctx->buf) - ctx->p;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200216 ((void) flags);
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 ret = mbedtls_snprintf(p, n, "depth %d - serial ", certificate_depth);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200219 MBEDTLS_X509_SAFE_SNPRINTF;
220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200222 MBEDTLS_X509_SAFE_SNPRINTF;
223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 ret = mbedtls_snprintf(p, n, " - subject ");
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200225 MBEDTLS_X509_SAFE_SNPRINTF;
226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200228 MBEDTLS_X509_SAFE_SNPRINTF;
229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 ret = mbedtls_snprintf(p, n, " - flags 0x%08x\n", *flags);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200231 MBEDTLS_X509_SAFE_SNPRINTF;
232
233 ctx->p = p;
234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 return 0;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200236}
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200237
Michael Schuster54300d42024-06-04 02:30:22 +0200238static int verify_parse_san(mbedtls_x509_subject_alternative_name *san,
Michael Schusterbd89b792024-06-04 02:41:10 +0200239 char **buf, size_t *size)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200240{
241 int ret;
242 size_t i;
243 char *p = *buf;
244 size_t n = *size;
245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 ret = mbedtls_snprintf(p, n, "type : %d", san->type);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200247 MBEDTLS_X509_SAFE_SNPRINTF;
248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 switch (san->type) {
250 case (MBEDTLS_X509_SAN_OTHER_NAME):
251 ret = mbedtls_snprintf(p, n, "\notherName :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300252 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
David Horstmanncfae6a12023-08-18 19:12:59 +0100255 &san->san.other_name.type_id) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 ret = mbedtls_snprintf(p, n, " hardware module name :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300257 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 ret = mbedtls_snprintf(p, n, " hardware type : ");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300259 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 ret = mbedtls_oid_get_numeric_string(p,
262 n,
Matthias Schulzedc32ea2023-10-19 16:09:08 +0200263 &san->san.other_name.value.hardware_module_name
264 .oid);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300265 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 ret = mbedtls_snprintf(p, n, ", hardware serial number : ");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300268 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 for (i = 0; i < san->san.other_name.value.hardware_module_name.val.len; i++) {
271 ret = mbedtls_snprintf(p,
272 n,
273 "%02X",
274 san->san.other_name.value.hardware_module_name.val.p[i]);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300275 MBEDTLS_X509_SAFE_SNPRINTF;
276 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200277 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 break;/* MBEDTLS_OID_ON_HW_MODULE_NAME */
279 case (MBEDTLS_X509_SAN_DNS_NAME):
280 ret = mbedtls_snprintf(p, n, "\ndNSName : ");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200281 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (san->san.unstructured_name.len >= n) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200283 *p = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200285 }
286 n -= san->san.unstructured_name.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 for (i = 0; i < san->san.unstructured_name.len; i++) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200288 *p++ = san->san.unstructured_name.p[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 }
290 break;/* MBEDTLS_X509_SAN_DNS_NAME */
Przemek Stekiel608e3ef2023-02-09 14:47:50 +0100291 case (MBEDTLS_X509_SAN_RFC822_NAME):
292 ret = mbedtls_snprintf(p, n, "\nrfc822Name : ");
293 MBEDTLS_X509_SAFE_SNPRINTF;
294 if (san->san.unstructured_name.len >= n) {
295 *p = '\0';
296 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
297 }
298 n -= san->san.unstructured_name.len;
299 for (i = 0; i < san->san.unstructured_name.len; i++) {
300 *p++ = san->san.unstructured_name.p[i];
301 }
302 break;/* MBEDTLS_X509_SAN_RFC822_NAME */
Andrzej Kureke12b01d2023-01-10 06:47:38 -0500303 case (MBEDTLS_X509_SAN_DIRECTORY_NAME):
304 ret = mbedtls_snprintf(p, n, "\ndirectoryName : ");
305 MBEDTLS_X509_SAFE_SNPRINTF;
306 ret = mbedtls_x509_dn_gets(p, n, &san->san.directory_name);
307 if (ret < 0) {
308 return ret;
309 }
310
311 p += ret;
312 n -= ret;
313 break;/* MBEDTLS_X509_SAN_DIRECTORY_NAME */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200314 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 /*
316 * Should not happen.
317 */
318 return -1;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200319 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 ret = mbedtls_snprintf(p, n, "\n");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200321 MBEDTLS_X509_SAFE_SNPRINTF;
322
323 *size = n;
324 *buf = p;
325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200327}
Michael Schustera3cc4632024-06-07 01:51:54 +0200328#endif /* MBEDTLS_FS_IO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200329
Michael Schuster54300d42024-06-04 02:30:22 +0200330static 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 +0200331 int critical, const unsigned char *cp, const unsigned char *end)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200332{
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 (void) crt;
334 (void) critical;
335 mbedtls_x509_buf *new_oid = (mbedtls_x509_buf *) p_ctx;
336 if (oid->tag == MBEDTLS_ASN1_OID &&
337 MBEDTLS_OID_CMP(MBEDTLS_OID_CERTIFICATE_POLICIES, oid) == 0) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200338 /* Handle unknown certificate policy */
339 int ret, parse_ret = 0;
340 size_t len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 unsigned char **p = (unsigned char **) &cp;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200342
343 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 ret = mbedtls_asn1_get_tag(p, end, &len,
345 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
346 if (ret != 0) {
347 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
348 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (*p + len != end) {
351 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
352 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
353 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200354
355 /*
356 * Cannot be an empty sequence.
357 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (len == 0) {
359 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
360 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
361 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 while (*p < end) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200364 const unsigned char *policy_end;
365
366 /*
367 * Get the policy sequence
368 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
370 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
371 0) {
372 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
373 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200374
375 policy_end = *p + len;
376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
378 MBEDTLS_ASN1_OID)) != 0) {
379 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
380 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200381
Nicola Di Lietob77fad82020-06-17 17:57:36 +0200382 /*
383 * Recognize exclusively the policy with OID 1
384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if (len != 1 || *p[0] != 1) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200386 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200388
389 *p += len;
390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 /*
392 * If there is an optional qualifier, then *p < policy_end
393 * Check the Qualifier len to verify it doesn't exceed policy_end.
394 */
395 if (*p < policy_end) {
396 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
397 MBEDTLS_ASN1_CONSTRUCTED |
398 MBEDTLS_ASN1_SEQUENCE)) != 0) {
399 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
400 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200401 /*
402 * Skip the optional policy qualifiers.
403 */
404 *p += len;
405 }
406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 if (*p != policy_end) {
408 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
409 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
410 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200411 }
412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 if (*p != end) {
414 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
415 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
416 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 return parse_ret;
419 } else if (new_oid != NULL && new_oid->tag == oid->tag && new_oid->len == oid->len &&
420 memcmp(new_oid->p, oid->p, oid->len) == 0) {
421 return 0;
422 } else {
423 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
424 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200425 }
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200426}
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200427#endif /* MBEDTLS_X509_CRT_PARSE_C */
Matthias Schulzab408222023-10-18 13:20:59 +0200428
Michael Schustera3cc4632024-06-07 01:51:54 +0200429#if defined(MBEDTLS_X509_CSR_PARSE_C) && \
430 !defined(MBEDTLS_X509_REMOVE_INFO)
Michael Schusterf828f042024-06-07 06:47:31 +0200431static int parse_csr_ext_accept_cb(void *p_ctx,
432 mbedtls_x509_csr const *csr,
433 mbedtls_x509_buf const *oid,
434 int critical,
435 const unsigned char *cp,
436 const unsigned char *end)
Matthias Schulzab408222023-10-18 13:20:59 +0200437{
438 (void) p_ctx;
439 (void) csr;
440 (void) oid;
441 (void) critical;
442 (void) cp;
443 (void) end;
444
445 return 0;
446}
447
Michael Schusterf828f042024-06-07 06:47:31 +0200448static int parse_csr_ext_reject_cb(void *p_ctx,
449 mbedtls_x509_csr const *csr,
450 mbedtls_x509_buf const *oid,
451 int critical,
452 const unsigned char *cp,
453 const unsigned char *end)
Matthias Schulzab408222023-10-18 13:20:59 +0200454{
455 (void) p_ctx;
456 (void) csr;
457 (void) oid;
458 (void) critical;
459 (void) cp;
460 (void) end;
461
462 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
463 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
464}
Michael Schustera3cc4632024-06-07 01:51:54 +0200465#endif /* MBEDTLS_X509_CSR_PARSE_C && !MBEDTLS_X509_REMOVE_INFO */
Paul Bakker33b43f12013-08-20 11:48:36 +0200466/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000467
Thomas Daubney5c9c2ce2022-06-06 16:36:43 +0100468/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100469void x509_accessor_ext_types(int ext_type, int has_ext_type)
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100470{
471 mbedtls_x509_crt crt;
472 int expected_result = ext_type & has_ext_type;
473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200475 USE_PSA_INIT();
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100476
477 crt.ext_types = ext_type;
478
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500479 TEST_EQUAL(mbedtls_x509_crt_has_ext_type(&crt, has_ext_type), expected_result);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100480
valerio32f2ac92023-04-20 11:59:52 +0200481exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200483 USE_PSA_DONE();
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100484}
485/* END_CASE */
486
Glenn Strauss6f545ac2022-10-25 15:02:14 -0400487/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_TEST_HOOKS */
488void x509_crt_parse_cn_inet_pton(const char *cn, data_t *exp, int ref_ret)
489{
490 uint32_t addr[4];
491 size_t addrlen = mbedtls_x509_crt_parse_cn_inet_pton(cn, addr);
492 TEST_EQUAL(addrlen, (size_t) ref_ret);
493
494 if (addrlen) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100495 TEST_MEMORY_COMPARE(exp->x, exp->len, addr, addrlen);
Glenn Strauss6f545ac2022-10-25 15:02:14 -0400496 }
497}
498/* END_CASE */
499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500501void x509_parse_san(char *crt_file, char *result_str, int parse_result)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200502{
Ron Eldor890819a2019-05-13 19:03:04 +0300503 int ret;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200504 mbedtls_x509_crt crt;
Ron Eldor890819a2019-05-13 19:03:04 +0300505 mbedtls_x509_subject_alternative_name san;
506 mbedtls_x509_sequence *cur = NULL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200507 char buf[2000];
508 char *p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 size_t n = sizeof(buf);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200512 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 memset(buf, 0, 2000);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200514
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500515 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), parse_result);
Ron Eldor890819a2019-05-13 19:03:04 +0300516
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500517 if (parse_result != 0) {
518 goto exit;
519 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 if (crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Ron Eldor890819a2019-05-13 19:03:04 +0300521 cur = &crt.subject_alt_names;
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 while (cur != NULL) {
523 ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
524 TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
Ron Eldor890819a2019-05-13 19:03:04 +0300525 /*
526 * If san type not supported, ignore.
527 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (ret == 0) {
Andrzej Kurekd40c2b62023-02-13 07:01:59 -0500529 ret = verify_parse_san(&san, &p, &n);
530 mbedtls_x509_free_subject_alt_name(&san);
531 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 }
Ron Eldor890819a2019-05-13 19:03:04 +0300533 cur = cur->next;
534 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200535 }
536
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500537 TEST_EQUAL(strcmp(buf, result_str), 0);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200538
539exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200541 USE_PSA_DONE();
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200542}
543/* END_CASE */
544
Hanno Becker612a2f12020-10-09 09:19:39 +0100545/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100546void x509_cert_info(char *crt_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000547{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000549 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000550 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200553 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000555
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500556 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 res = mbedtls_x509_crt_info(buf, 2000, "", &crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 TEST_ASSERT(res != -1);
560 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000561
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500562 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200563
564exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200566 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000567}
Paul Bakker33b43f12013-08-20 11:48:36 +0200568/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000569
Hanno Becker612a2f12020-10-09 09:19:39 +0100570/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100571void mbedtls_x509_crl_info(char *crl_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000572{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000574 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000575 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200578 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000580
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500581 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 res = mbedtls_x509_crl_info(buf, 2000, "", &crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 TEST_ASSERT(res != -1);
585 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000586
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500587 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200588
589exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +0200591 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000592}
Paul Bakker33b43f12013-08-20 11:48:36 +0200593/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000594
Andres AGa39db392016-12-08 17:10:38 +0000595/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100596void mbedtls_x509_crl_parse(char *crl_file, int result)
Andres AGa39db392016-12-08 17:10:38 +0000597{
598 mbedtls_x509_crl crl;
599 char buf[2000];
600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200602 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 memset(buf, 0, 2000);
Andres AGa39db392016-12-08 17:10:38 +0000604
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500605 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), result);
Andres AGa39db392016-12-08 17:10:38 +0000606
607exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +0200609 USE_PSA_DONE();
Andres AGa39db392016-12-08 17:10:38 +0000610}
611/* END_CASE */
612
Hanno Becker612a2f12020-10-09 09:19:39 +0100613/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100614void mbedtls_x509_csr_info(char *csr_file, char *result_str)
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100615{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100617 char buf[2000];
618 int res;
619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +0200621 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 memset(buf, 0, 2000);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100623
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500624 TEST_EQUAL(mbedtls_x509_csr_parse_file(&csr, csr_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 res = mbedtls_x509_csr_info(buf, 2000, "", &csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 TEST_ASSERT(res != -1);
628 TEST_ASSERT(res != -2);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100629
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500630 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200631
632exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +0200634 USE_PSA_DONE();
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100635}
636/* END_CASE */
637
Hanno Becker612a2f12020-10-09 09:19:39 +0100638/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100639void x509_verify_info(int flags, char *prefix, char *result_str)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100640{
641 char buf[2000];
642 int res;
643
Valerio Setti569c1712023-04-19 14:53:36 +0200644 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 memset(buf, 0, sizeof(buf));
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 res = mbedtls_x509_crt_verify_info(buf, sizeof(buf), prefix, flags);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 TEST_ASSERT(res >= 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100650
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500651 TEST_EQUAL(strcmp(buf, result_str), 0);
valerio32f2ac92023-04-20 11:59:52 +0200652
653exit:
Valerio Setti569c1712023-04-19 14:53:36 +0200654 USE_PSA_DONE();
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100655}
656/* END_CASE */
657
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200658/* 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 +0100659void x509_verify_restart(char *crt_file, char *ca_file,
660 int result, int flags_result,
661 int max_ops, int min_restart, int max_restart)
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200662{
663 int ret, cnt_restart;
664 mbedtls_x509_crt_restart_ctx rs_ctx;
665 mbedtls_x509_crt crt;
666 mbedtls_x509_crt ca;
667 uint32_t flags = 0;
668
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200669 /*
670 * See comments on ecp_test_vect_restart() for op count precision.
671 *
Gilles Peskinee820c0a2023-08-03 17:45:20 +0200672 * For reference, with Mbed TLS 2.6 and default settings:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200673 * - ecdsa_verify() for P-256: ~ 6700
674 * - ecdsa_verify() for P-384: ~ 18800
675 * - x509_verify() for server5 -> test-ca2: ~ 18800
676 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
677 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 mbedtls_x509_crt_restart_init(&rs_ctx);
679 mbedtls_x509_crt_init(&crt);
680 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200681 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200682
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500683 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
684 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200685
Valerio Setti4a2e7b92025-05-23 15:15:22 +0200686 psa_interruptible_set_max_ops(max_ops);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200687
688 cnt_restart = 0;
689 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
691 &mbedtls_x509_crt_profile_default, NULL, &flags,
692 NULL, NULL, &rs_ctx);
693 } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200694
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500695 TEST_EQUAL(ret, result);
696 TEST_EQUAL(flags, (uint32_t) flags_result);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 TEST_ASSERT(cnt_restart >= min_restart);
699 TEST_ASSERT(cnt_restart <= max_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200700
701 /* Do we leak memory when aborting? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
703 &mbedtls_x509_crt_profile_default, NULL, &flags,
704 NULL, NULL, &rs_ctx);
705 TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200706
707exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 mbedtls_x509_crt_restart_free(&rs_ctx);
709 mbedtls_x509_crt_free(&crt);
710 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100711 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200712}
713/* END_CASE */
714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100716void x509_verify(char *crt_file, char *ca_file, char *crl_file,
717 char *cn_name_str, int result, int flags_result,
718 char *profile_str,
719 char *verify_callback)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000720{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_x509_crt crt;
722 mbedtls_x509_crt ca;
723 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200724 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000725 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200726 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 char *cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200728 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 mbedtls_x509_crt_init(&crt);
731 mbedtls_x509_crt_init(&ca);
732 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200733 MD_OR_USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 if (strcmp(cn_name_str, "NULL") != 0) {
Paul Bakker33b43f12013-08-20 11:48:36 +0200736 cn_name = cn_name_str;
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 if (strcmp(profile_str, "") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200740 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 } else if (strcmp(profile_str, "next") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200742 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 } else if (strcmp(profile_str, "suite_b") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200744 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 } else if (strcmp(profile_str, "compat") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200746 profile = &compat_profile;
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 } else if (strcmp(profile_str, "all") == 0) {
Hanno Becker20a4ade2019-06-03 14:27:03 +0100748 profile = &profile_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100750 TEST_FAIL("Unknown algorithm profile");
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 }
Gilles Peskineef86ab22017-05-05 18:59:02 +0200752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 if (strcmp(verify_callback, "NULL") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200754 f_vrfy = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 } else if (strcmp(verify_callback, "verify_none") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200756 f_vrfy = verify_none;
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 } else if (strcmp(verify_callback, "verify_all") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200758 f_vrfy = verify_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100760 TEST_FAIL("No known verify callback selected");
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200762
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500763 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
764 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
765 TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000766
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 res = mbedtls_x509_crt_verify_with_profile(&crt,
768 &ca,
769 &crl,
770 profile,
771 cn_name,
772 &flags,
773 f_vrfy,
774 NULL);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 TEST_EQUAL(res, result);
777 TEST_EQUAL(flags, (uint32_t) flags_result);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200778
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200779#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker0350d562019-03-28 14:23:36 +0000780 /* CRLs aren't supported with CA callbacks, so skip the CA callback
Jarno Lamsadfd22c42019-04-01 15:18:53 +0300781 * version of the test if CRLs are in use. */
Gilles Peskinebb7d92c2023-10-17 17:26:44 +0200782 if (strcmp(crl_file, "") == 0) {
Hanno Becker0350d562019-03-28 14:23:36 +0000783 flags = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 res = mbedtls_x509_crt_verify_with_ca_cb(&crt,
786 ca_callback,
787 &ca,
788 profile,
789 cn_name,
790 &flags,
791 f_vrfy,
792 NULL);
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200793
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500794 TEST_EQUAL(res, result);
795 TEST_EQUAL(flags, (uint32_t) (flags_result));
Hanno Becker0350d562019-03-28 14:23:36 +0000796 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200797#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200798exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 mbedtls_x509_crt_free(&crt);
800 mbedtls_x509_crt_free(&ca);
801 mbedtls_x509_crl_free(&crl);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100802 MD_OR_USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000803}
Paul Bakker33b43f12013-08-20 11:48:36 +0200804/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000805
Jarno Lamsa557426a2019-03-27 17:08:29 +0200806/* 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 +0100807void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
808 int exp_ret)
Jarno Lamsa557426a2019-03-27 17:08:29 +0200809{
810 int ret;
811 mbedtls_x509_crt crt;
812 mbedtls_x509_crt ca;
813 uint32_t flags = 0;
Hanno Becker3f932bb2019-03-28 17:06:47 +0000814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 mbedtls_x509_crt_init(&crt);
816 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200817 USE_PSA_INIT();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200818
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500819 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
820 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if (strcmp(name, "NULL") == 0) {
Jarno Lamsa557426a2019-03-27 17:08:29 +0200823 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca,
827 &compat_profile, name, &flags,
828 NULL, NULL);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200829
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500830 TEST_EQUAL(ret, exp_ret);
831 TEST_EQUAL(flags, (uint32_t) (-1));
Jarno Lamsa557426a2019-03-27 17:08:29 +0200832exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 mbedtls_x509_crt_free(&crt);
834 mbedtls_x509_crt_free(&ca);
Valerio Setti569c1712023-04-19 14:53:36 +0200835 USE_PSA_DONE();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200836}
837/* END_CASE */
838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100840void x509_verify_callback(char *crt_file, char *ca_file, char *name,
841 int exp_ret, char *exp_vrfy_out)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200842{
843 int ret;
844 mbedtls_x509_crt crt;
845 mbedtls_x509_crt ca;
846 uint32_t flags = 0;
847 verify_print_context vrfy_ctx;
848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 mbedtls_x509_crt_init(&crt);
850 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200851 MD_OR_USE_PSA_INIT();
852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 verify_print_init(&vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200854
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500855 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
856 TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200857
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 if (strcmp(name, "NULL") == 0) {
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200859 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 }
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200861
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL,
863 &compat_profile,
864 name, &flags,
865 verify_print, &vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200866
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500867 TEST_EQUAL(ret, exp_ret);
868 TEST_EQUAL(strcmp(vrfy_ctx.buf, exp_vrfy_out), 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200869
870exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 mbedtls_x509_crt_free(&crt);
872 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100873 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200874}
875/* END_CASE */
876
Hanno Becker612a2f12020-10-09 09:19:39 +0100877/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100878void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
879 char *new_subject_ou,
880 char *result_str,
881 int ret)
Werner Lewis31ecb962022-06-17 15:51:55 +0100882{
883 mbedtls_x509_crt crt;
884 char buf[2000];
885 int res = 0;
886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200888 USE_PSA_INIT();
889
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 memset(buf, 0, 2000);
Werner Lewis31ecb962022-06-17 15:51:55 +0100891
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500892 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100893 crt.subject.next->val.p = (unsigned char *) new_subject_ou;
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 crt.subject.next->val.len = strlen(new_subject_ou);
Werner Lewis31ecb962022-06-17 15:51:55 +0100895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
Werner Lewis31ecb962022-06-17 15:51:55 +0100897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 if (ret != 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500899 TEST_EQUAL(res, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 } else {
901 TEST_ASSERT(res != -1);
902 TEST_ASSERT(res != -2);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500903 TEST_EQUAL(strcmp(buf, result_str), 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100904 }
905exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200907 USE_PSA_DONE();
Werner Lewis31ecb962022-06-17 15:51:55 +0100908}
909/* END_CASE */
910
911/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100912void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000913{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000915 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200916 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200919 USE_PSA_INIT();
920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000922
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500923 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 if (strcmp(entity, "subject") == 0) {
925 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
926 } else if (strcmp(entity, "issuer") == 0) {
927 res = mbedtls_x509_dn_gets(buf, 2000, &crt.issuer);
928 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +0100929 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 TEST_ASSERT(res != -1);
933 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000934
Demi Marie Obenour16442cc2022-11-26 22:19:48 -0500935 TEST_EQUAL(strcmp(buf, result_str), 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200936
937exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200939 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000940}
Paul Bakker33b43f12013-08-20 11:48:36 +0200941/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000942
David Horstmanndb73d3b2022-10-04 16:49:16 +0100943/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100944void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
David Horstmanndb73d3b2022-10-04 16:49:16 +0100945{
valerioe50831c2023-04-20 14:48:19 +0200946 unsigned char *name = NULL;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100947 unsigned char *p;
948 size_t name_len;
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100949 mbedtls_x509_name head;
David Horstmann3cd67582022-10-17 17:59:10 +0100950 int ret;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100951
Valerio Setti569c1712023-04-19 14:53:36 +0200952 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 memset(&head, 0, sizeof(head));
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100954
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100956 p = name;
957
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 ret = mbedtls_x509_get_name(&p, (name + name_len), &head);
959 if (ret == 0) {
960 mbedtls_asn1_free_named_data_list_shallow(head.next);
961 }
David Horstmanndb73d3b2022-10-04 16:49:16 +0100962
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 TEST_EQUAL(ret, exp_ret);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100964
valerio32f2ac92023-04-20 11:59:52 +0200965exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 mbedtls_free(name);
Valerio Setti569c1712023-04-19 14:53:36 +0200967 USE_PSA_DONE();
David Horstmanndb73d3b2022-10-04 16:49:16 +0100968}
969/* END_CASE */
970
Werner Lewisb3acb052022-06-17 15:59:58 +0100971/* 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 +0100972void mbedtls_x509_dn_get_next(char *name_str,
973 int next_merged,
974 char *expected_oids,
975 int exp_count,
976 char *exp_dn_gets)
Werner Lewisb3acb052022-06-17 15:59:58 +0100977{
978 int ret = 0, i;
Werner Lewisac80a662022-06-23 11:58:02 +0100979 size_t len = 0, out_size;
Werner Lewisb3acb052022-06-17 15:59:58 +0100980 mbedtls_asn1_named_data *names = NULL;
Gilles Peskine21e46b32023-10-17 16:35:20 +0200981 mbedtls_x509_name parsed;
982 memset(&parsed, 0, sizeof(parsed));
983 mbedtls_x509_name *parsed_cur;
Werner Lewisac80a662022-06-23 11:58:02 +0100984 // Size of buf is maximum required for test cases
Gilles Peskinef2574202023-10-18 17:39:48 +0200985 unsigned char buf[80] = { 0 };
Gilles Peskine21e46b32023-10-17 16:35:20 +0200986 unsigned char *out = NULL;
987 unsigned char *c = buf + sizeof(buf);
Werner Lewisb3acb052022-06-17 15:59:58 +0100988 const char *short_name;
989
Valerio Setti569c1712023-04-19 14:53:36 +0200990 USE_PSA_INIT();
Gilles Peskine21e46b32023-10-17 16:35:20 +0200991
Werner Lewisac80a662022-06-23 11:58:02 +0100992 // Additional size required for trailing space
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 out_size = strlen(expected_oids) + 2;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100994 TEST_CALLOC(out, out_size);
Werner Lewisb3acb052022-06-17 15:59:58 +0100995
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 TEST_EQUAL(mbedtls_x509_string_to_names(&names, name_str), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 ret = mbedtls_x509_write_names(&c, buf, names);
999 TEST_LE_S(0, ret);
Werner Lewisb3acb052022-06-17 15:59:58 +01001000
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
1002 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
1003 TEST_EQUAL(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +01001004
1005 // Iterate over names and set next_merged nodes
1006 parsed_cur = &parsed;
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 for (; next_merged != 0 && parsed_cur != NULL; next_merged = next_merged >> 1) {
Werner Lewis12657cd2022-06-20 11:47:57 +01001008 parsed_cur->next_merged = next_merged & 0x01;
Werner Lewisb3acb052022-06-17 15:59:58 +01001009 parsed_cur = parsed_cur->next;
1010 }
1011
1012 // Iterate over RDN nodes and print OID of first element to buffer
1013 parsed_cur = &parsed;
1014 len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 for (i = 0; parsed_cur != NULL; i++) {
Gilles Peskine532e3ee2025-05-07 20:37:15 +02001016 TEST_EQUAL(mbedtls_x509_oid_get_attr_short_name(&parsed_cur->oid,
1017 &short_name), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 len += mbedtls_snprintf((char *) out + len, out_size - len, "%s ", short_name);
1019 parsed_cur = mbedtls_x509_dn_get_next(parsed_cur);
Werner Lewisb3acb052022-06-17 15:59:58 +01001020 }
1021 out[len-1] = 0;
1022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 TEST_EQUAL(exp_count, i);
1024 TEST_EQUAL(strcmp((char *) out, expected_oids), 0);
1025 mbedtls_free(out);
Werner Lewisac80a662022-06-23 11:58:02 +01001026 out = NULL;
Werner Lewisb3acb052022-06-17 15:59:58 +01001027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 out_size = strlen(exp_dn_gets) + 1;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001029 TEST_CALLOC(out, out_size);
Werner Lewisac80a662022-06-23 11:58:02 +01001030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 TEST_LE_S(0, mbedtls_x509_dn_gets((char *) out, out_size, &parsed));
1032 TEST_EQUAL(strcmp((char *) out, exp_dn_gets), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +01001033exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 mbedtls_free(out);
1035 mbedtls_asn1_free_named_data_list(&names);
1036 mbedtls_asn1_free_named_data_list_shallow(parsed.next);
Valerio Setti569c1712023-04-19 14:53:36 +02001037 USE_PSA_DONE();
Werner Lewisb3acb052022-06-17 15:59:58 +01001038}
Werner Lewisb3acb052022-06-17 15:59:58 +01001039/* END_CASE */
1040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001042void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
Paul Bakker37940d9f2009-07-10 22:38:58 +00001043{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +00001045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001047 USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +00001048
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001049 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 if (strcmp(entity, "valid_from") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001052 TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_from), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 } else if (strcmp(entity, "valid_to") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001054 TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_to), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +01001056 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001058
Paul Bakkerbd51b262014-07-10 15:26:12 +02001059exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001061 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +00001062}
Paul Bakker33b43f12013-08-20 11:48:36 +02001063/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +00001064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001066void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001071 USE_PSA_INIT();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001072
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001073 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 if (strcmp(entity, "valid_from") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001076 TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_from), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 } else if (strcmp(entity, "valid_to") == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001078 TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_to), result);
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 } else {
Agathiyan Bragadeesh763b3532023-07-27 13:52:31 +01001080 TEST_FAIL("Unknown entity");
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001082
Paul Bakkerbd51b262014-07-10 15:26:12 +02001083exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001085 USE_PSA_DONE();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001086}
1087/* END_CASE */
1088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001090void x509parse_crt_file(char *crt_file, int result)
Paul Bakker5a5fa922014-09-26 14:53:04 +02001091{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +02001093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001095 USE_PSA_INIT();
Paul Bakker5a5fa922014-09-26 14:53:04 +02001096
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001097 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), result);
Paul Bakker5a5fa922014-09-26 14:53:04 +02001098
1099exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001101 USE_PSA_DONE();
Paul Bakker5a5fa922014-09-26 14:53:04 +02001102}
1103/* END_CASE */
1104
Minos Galanakisa83ada42024-01-19 10:59:07 +00001105/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
1106void mbedtls_x509_get_ca_istrue(char *crt_file, int result)
1107{
1108 mbedtls_x509_crt crt;
1109 mbedtls_x509_crt_init(&crt);
1110 USE_PSA_INIT();
1111
1112 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
1113 TEST_EQUAL(mbedtls_x509_crt_get_ca_istrue(&crt), result);
1114exit:
1115 mbedtls_x509_crt_free(&crt);
1116 USE_PSA_DONE();
1117}
1118/* END_CASE */
1119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001121void x509parse_crt(data_t *buf, char *result_str, int result)
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001122{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 mbedtls_x509_crt crt;
Valerio Setti210b6112025-06-19 23:19:05 +02001124#if !defined(MBEDTLS_X509_REMOVE_INFO)
Hanno Beckerfff2d572020-10-09 09:45:19 +01001125 unsigned char output[2000] = { 0 };
Valerio Setti210b6112025-06-19 23:19:05 +02001126#else
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001127 ((void) result_str);
Valerio Setti210b6112025-06-19 23:19:05 +02001128#endif
Valerio Setti27eb0142025-06-19 23:40:18 +02001129 /* Tests whose result is MBEDTLS_ERR_PK_INVALID_PUBKEY might return
1130 * MBEDTLS_ERR_ASN1_UNEXPECTED_TAG until psa#308 is merged. This variable
1131 * is therefore used for backward compatiblity and will be removed in
1132 * mbedtls#10229. */
1133 int result_back_comp = result;
Valerio Setti11345e92025-06-10 13:39:44 +02001134 int res;
1135
1136#if !defined(MBEDTLS_PK_USE_PSA_RSA_DATA)
1137 /* Support for mbedtls#10213 before psa#308. Once psa#308 will be
1138 * merged this dirty fix can be removed. */
1139 if (result == MBEDTLS_ERR_PK_INVALID_PUBKEY) {
Valerio Setti27eb0142025-06-19 23:40:18 +02001140 result_back_comp = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
Valerio Setti11345e92025-06-10 13:39:44 +02001141 }
1142#endif /* MBEDTLS_PK_USE_PSA_RSA_DATA */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001145 USE_PSA_INIT();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001146
Valerio Setti11345e92025-06-10 13:39:44 +02001147 res = mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len);
Valerio Setti27eb0142025-06-19 23:40:18 +02001148 TEST_ASSERT((res == result) || (res == result_back_comp));
Hanno Becker612a2f12020-10-09 09:19:39 +01001149#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 if ((result) == 0) {
1151 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1152 TEST_ASSERT(res != -1);
1153 TEST_ASSERT(res != -2);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001154
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001155 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001156 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001158#endif
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 mbedtls_x509_crt_free(&crt);
1161 mbedtls_x509_crt_init(&crt);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001162
Valerio Setti11345e92025-06-10 13:39:44 +02001163 res = mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len);
Valerio Setti27eb0142025-06-19 23:40:18 +02001164 TEST_ASSERT((res == result) || (res == result_back_comp));
Hanno Becker612a2f12020-10-09 09:19:39 +01001165#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if ((result) == 0) {
1167 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakker33b43f12013-08-20 11:48:36 +02001170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 TEST_ASSERT(res != -1);
1172 TEST_ASSERT(res != -2);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001173
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001174 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001175 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001177#endif /* !MBEDTLS_X509_REMOVE_INFO */
Paul Bakkerb08e6842012-02-11 18:43:20 +00001178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 mbedtls_x509_crt_free(&crt);
1180 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001181
Valerio Setti11345e92025-06-10 13:39:44 +02001182 res = mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL, NULL);
Valerio Setti27eb0142025-06-19 23:40:18 +02001183 TEST_ASSERT((res == result) || (res == result_back_comp));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001184#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 if ((result) == 0) {
1186 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001187
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 TEST_ASSERT(res != -1);
1189 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001190
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001191 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001192 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001194#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001195
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 mbedtls_x509_crt_free(&crt);
1197 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001198
Valerio Setti11345e92025-06-10 13:39:44 +02001199 res = mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL, NULL);
Valerio Setti27eb0142025-06-19 23:40:18 +02001200 TEST_ASSERT((res == result) || (res == result_back_comp));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001201#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 if ((result) == 0) {
1203 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 TEST_ASSERT(res != -1);
1206 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001207
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001208 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001209 }
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001210#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001211
1212exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001214 USE_PSA_DONE();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001215}
1216/* END_CASE */
1217
1218/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001219void x509parse_crt_cb(data_t *buf, char *result_str, int result)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001220{
1221 mbedtls_x509_crt crt;
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001222 mbedtls_x509_buf oid;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001223
1224#if !defined(MBEDTLS_X509_REMOVE_INFO)
1225 unsigned char output[2000] = { 0 };
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001226 int res;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001227#else
1228 ((void) result_str);
1229#endif
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001230
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001231 oid.tag = MBEDTLS_ASN1_OID;
1232 oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001236 USE_PSA_INIT();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001237
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001238 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
1239 &oid), result);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001240#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 if ((result) == 0) {
1242 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001243
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 TEST_ASSERT(res != -1);
1245 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001246
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001247 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001248 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 memset(output, 0, 2000);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001250#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 mbedtls_x509_crt_free(&crt);
1253 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001254
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001255 TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1256 &oid), (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001257#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 if ((result) == 0) {
1259 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 TEST_ASSERT(res != -1);
1262 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001263
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001264 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001265 }
Hanno Beckerfff2d572020-10-09 09:45:19 +01001266#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001267
Paul Bakkerbd51b262014-07-10 15:26:12 +02001268exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001270 USE_PSA_DONE();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001271}
Paul Bakker33b43f12013-08-20 11:48:36 +02001272/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001273
Hanno Becker612a2f12020-10-09 09:19:39 +01001274/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001275void x509parse_crl(data_t *buf, char *result_str, int result)
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001276{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001278 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +01001279 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +02001282 USE_PSA_INIT();
1283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 memset(output, 0, 2000);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001285
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001286
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001287 TEST_EQUAL(mbedtls_x509_crl_parse(&crl, buf->x, buf->len), (result));
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 if ((result) == 0) {
1289 res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
Paul Bakker33b43f12013-08-20 11:48:36 +02001290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 TEST_ASSERT(res != -1);
1292 TEST_ASSERT(res != -2);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001293
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001294 TEST_EQUAL(strcmp((char *) output, result_str), 0);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001295 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001296
Paul Bakkerbd51b262014-07-10 15:26:12 +02001297exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +02001299 USE_PSA_DONE();
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001300}
Paul Bakker33b43f12013-08-20 11:48:36 +02001301/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001302
Hanno Becker612a2f12020-10-09 09:19:39 +01001303/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001304void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001305{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001307 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001308 int my_ret;
1309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001311 USE_PSA_INIT();
1312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 memset(my_out, 0, sizeof(my_out));
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001316 TEST_EQUAL(my_ret, ref_ret);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 if (ref_ret == 0) {
1319 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001320 TEST_EQUAL(my_out_len, strlen(ref_out));
1321 TEST_EQUAL(strcmp(my_out, ref_out), 0);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001322 }
1323
Paul Bakkerbd51b262014-07-10 15:26:12 +02001324exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001326 USE_PSA_DONE();
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001327}
1328/* END_CASE */
1329
Matthias Schulzab408222023-10-18 13:20:59 +02001330/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1331void mbedtls_x509_csr_parse_with_ext_cb(data_t *csr_der, char *ref_out, int ref_ret, int accept)
1332{
1333 mbedtls_x509_csr csr;
1334 char my_out[1000];
1335 int my_ret;
1336
1337 mbedtls_x509_csr_init(&csr);
1338 USE_PSA_INIT();
1339
1340 memset(my_out, 0, sizeof(my_out));
1341
1342 my_ret = mbedtls_x509_csr_parse_der_with_ext_cb(&csr, csr_der->x, csr_der->len,
1343 accept ? parse_csr_ext_accept_cb :
Matthias Schulzedc32ea2023-10-19 16:09:08 +02001344 parse_csr_ext_reject_cb,
Matthias Schulzab408222023-10-18 13:20:59 +02001345 NULL);
1346 TEST_EQUAL(my_ret, ref_ret);
1347
1348 if (ref_ret == 0) {
1349 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1350 TEST_EQUAL(my_out_len, strlen(ref_out));
1351 TEST_EQUAL(strcmp(my_out, ref_out), 0);
1352 }
1353
1354exit:
1355 mbedtls_x509_csr_free(&csr);
1356 USE_PSA_DONE();
1357}
1358/* END_CASE */
1359
Przemek Stekield7992df2023-01-25 16:19:50 +01001360/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1361void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
1362{
1363 mbedtls_x509_csr csr;
1364 char my_out[1000];
1365 int my_ret;
1366
1367 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001368 USE_PSA_INIT();
1369
Przemek Stekield7992df2023-01-25 16:19:50 +01001370 memset(my_out, 0, sizeof(my_out));
1371
1372 my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001373 TEST_EQUAL(my_ret, ref_ret);
Przemek Stekield7992df2023-01-25 16:19:50 +01001374
1375 if (ref_ret == 0) {
1376 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001377 TEST_EQUAL(my_out_len, strlen(ref_out));
1378 TEST_EQUAL(strcmp(my_out, ref_out), 0);
Przemek Stekield7992df2023-01-25 16:19:50 +01001379 }
1380
1381exit:
1382 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001383 USE_PSA_DONE();
Przemek Stekield7992df2023-01-25 16:19:50 +01001384}
1385/* END_CASE */
1386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1e5fec62023-04-13 18:13:48 +02001388void mbedtls_x509_crt_parse_file(char *crt_path, int ret, int nb_crt)
1389{
1390 mbedtls_x509_crt chain, *cur;
1391 int i;
1392
1393 mbedtls_x509_crt_init(&chain);
1394 USE_PSA_INIT();
1395
1396 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, crt_path), ret);
1397
1398 /* Check how many certs we got */
1399 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1400 if (cur->raw.p != NULL) {
1401 i++;
1402 }
1403 }
1404
1405 TEST_EQUAL(i, nb_crt);
1406
1407exit:
1408 mbedtls_x509_crt_free(&chain);
1409 USE_PSA_DONE();
1410}
1411/* END_CASE */
1412
1413/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001414void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001415{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001417 int i;
1418
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001420 USE_PSA_INIT();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001421
Gilles Peskine55ad28a2023-04-13 18:14:45 +02001422 TEST_EQUAL(mbedtls_x509_crt_parse_path(&chain, crt_path), ret);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001423
1424 /* Check how many certs we got */
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1426 if (cur->raw.p != NULL) {
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001427 i++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 }
1429 }
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001430
Gilles Peskine55ad28a2023-04-13 18:14:45 +02001431 TEST_EQUAL(i, nb_crt);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001432
Paul Bakkerbd51b262014-07-10 15:26:12 +02001433exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 mbedtls_x509_crt_free(&chain);
Valerio Setti569c1712023-04-19 14:53:36 +02001435 USE_PSA_DONE();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001436}
1437/* END_CASE */
1438
Janos Follath822b2c32015-10-11 10:25:22 +02001439/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001440void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1441 int ret_chk, int flags_chk)
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001442{
1443 char file_buf[128];
1444 int ret;
1445 uint32_t flags;
1446 mbedtls_x509_crt trusted, chain;
1447
1448 /*
1449 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1450 * with NN.crt signed by NN-1.crt
1451 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 mbedtls_x509_crt_init(&trusted);
1453 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001454 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001455
1456 /* Load trusted root */
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001457 TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, ca_file), 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001458
1459 /* Load a chain with nb_int intermediates (from 01 to nb_int),
1460 * plus one "end-entity" cert (nb_int + 1) */
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001461 ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 nb_int + 1);
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001463 TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001464 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, file_buf), 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001465
1466 /* Try to verify that chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1468 NULL, NULL);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001469 TEST_EQUAL(ret, ret_chk);
1470 TEST_EQUAL(flags, (uint32_t) flags_chk);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001471
1472exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 mbedtls_x509_crt_free(&chain);
1474 mbedtls_x509_crt_free(&trusted);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001475 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001476}
1477/* END_CASE */
1478
1479/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001480void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1481 int flags_result, int result,
1482 char *profile_name, int vrfy_fatal_lvls)
Janos Follath822b2c32015-10-11 10:25:22 +02001483{
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 char *act;
Janos Follath822b2c32015-10-11 10:25:22 +02001485 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001486 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001487 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001488 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +02001489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 mbedtls_x509_crt_init(&chain);
1491 mbedtls_x509_crt_init(&trusted);
valerio32f2ac92023-04-20 11:59:52 +02001492 MD_OR_USE_PSA_INIT();
Janos Follath822b2c32015-10-11 10:25:22 +02001493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 while ((act = mystrsep(&chain_paths, " ")) != NULL) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001495 TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, act), 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 }
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001497 TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, trusted_ca), 0);
Janos Follath822b2c32015-10-11 10:25:22 +02001498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 if (strcmp(profile_name, "") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001500 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 } else if (strcmp(profile_name, "next") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001502 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 } else if (strcmp(profile_name, "suiteb") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001504 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 } else if (strcmp(profile_name, "rsa3072") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001506 profile = &profile_rsa3072;
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 } else if (strcmp(profile_name, "sha512") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001508 profile = &profile_sha512;
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 }
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001510
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1512 NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
Janos Follathef4f2582015-10-11 16:17:27 +02001513
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001514 TEST_EQUAL(res, (result));
1515 TEST_EQUAL(flags, (uint32_t) (flags_result));
Janos Follath822b2c32015-10-11 10:25:22 +02001516
1517exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 mbedtls_x509_crt_free(&trusted);
1519 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001520 MD_OR_USE_PSA_DONE();
Janos Follath822b2c32015-10-11 10:25:22 +02001521}
1522/* END_CASE */
1523
Gilles Peskine02ec5852025-05-12 20:52:07 +02001524/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001525void x509_oid_desc(data_t *buf, char *ref_desc)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001526{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001528 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001529 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001530
Valerio Setti569c1712023-04-19 14:53:36 +02001531 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001534 oid.p = buf->x;
1535 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001536
Gilles Peskine532e3ee2025-05-07 20:37:15 +02001537 ret = mbedtls_x509_oid_get_extended_key_usage(&oid, &desc);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 if (strcmp(ref_desc, "notfound") == 0) {
1540 TEST_ASSERT(ret != 0);
1541 TEST_ASSERT(desc == NULL);
1542 } else {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001543 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 TEST_ASSERT(desc != NULL);
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001545 TEST_EQUAL(strcmp(desc, ref_desc), 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001546 }
valerio32f2ac92023-04-20 11:59:52 +02001547
1548exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001549 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001550}
1551/* END_CASE */
1552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001554void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001555{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001557 char num_buf[100];
1558
Valerio Setti569c1712023-04-19 14:53:36 +02001559 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001560
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001561 memset(num_buf, 0x2a, sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001564 oid.p = oid_buf->x;
1565 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001566
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001567 TEST_ASSERT((size_t) blen <= sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001568
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001569 TEST_EQUAL(mbedtls_oid_get_numeric_string(num_buf, blen, &oid), ret);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001570
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 if (ret >= 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001572 TEST_EQUAL(num_buf[ret], 0);
1573 TEST_EQUAL(strcmp(num_buf, numstr), 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001574 }
valerio32f2ac92023-04-20 11:59:52 +02001575
1576exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001577 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001578}
1579/* END_CASE */
1580
TRodziewicz442fdc22021-06-07 13:52:23 +02001581/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001582void x509_check_key_usage(char *crt_file, int usage, int ret)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001583{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +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é-Gonnard603116c2014-04-09 09:50:03 +02001588
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001589 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001590
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001591 TEST_EQUAL(mbedtls_x509_crt_check_key_usage(&crt, usage), ret);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001592
Paul Bakkerbd51b262014-07-10 15:26:12 +02001593exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001595 USE_PSA_DONE();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001596}
1597/* END_CASE */
1598
TRodziewicz442fdc22021-06-07 13:52:23 +02001599/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001600void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1601 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001602{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001604
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001606 USE_PSA_INIT();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001607
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001608 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001609
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001610 TEST_EQUAL(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x, oid->len),
1611 ret);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001612
Paul Bakkerbd51b262014-07-10 15:26:12 +02001613exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001615 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001616}
1617/* END_CASE */
1618
Andres AG4b76aec2016-09-23 13:16:02 +01001619/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001620void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1621 int day, int hour, int min, int sec)
Andres AG4b76aec2016-09-23 13:16:02 +01001622{
1623 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001624 unsigned char buf[21];
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 unsigned char *start = buf;
1626 unsigned char *end = buf;
Andres AG4b76aec2016-09-23 13:16:02 +01001627
Valerio Setti569c1712023-04-19 14:53:36 +02001628 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 memset(&time, 0x00, sizeof(time));
1630 *end = (unsigned char) tag; end++;
1631 *end = strlen(time_str);
1632 TEST_ASSERT(*end < 20);
Andres AG4b76aec2016-09-23 13:16:02 +01001633 end++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 memcpy(end, time_str, (size_t) *(end - 1));
Andres AG4b76aec2016-09-23 13:16:02 +01001635 end += *(end - 1);
1636
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001637 TEST_EQUAL(mbedtls_x509_get_time(&start, end, &time), ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 if (ret == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001639 TEST_EQUAL(year, time.year);
1640 TEST_EQUAL(mon, time.mon);
1641 TEST_EQUAL(day, time.day);
1642 TEST_EQUAL(hour, time.hour);
1643 TEST_EQUAL(min, time.min);
1644 TEST_EQUAL(sec, time.sec);
Andres AG4b76aec2016-09-23 13:16:02 +01001645 }
valerio32f2ac92023-04-20 11:59:52 +02001646exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001647 USE_PSA_DONE();
Andres AG4b76aec2016-09-23 13:16:02 +01001648}
1649/* END_CASE */
1650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001652void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1653 int ref_msg_md, int ref_mgf_md,
1654 int ref_salt_len, int ref_ret)
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001655{
1656 int my_ret;
Ronald Cronac6ae352020-06-26 14:33:03 +02001657 mbedtls_x509_buf buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001659 int my_salt_len;
1660
Valerio Setti569c1712023-04-19 14:53:36 +02001661 USE_PSA_INIT();
1662
Ronald Cronac6ae352020-06-26 14:33:03 +02001663 buf.p = params->x;
1664 buf.len = params->len;
1665 buf.tag = params_tag;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001666
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1668 &my_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001669
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001670 TEST_EQUAL(my_ret, ref_ret);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 if (ref_ret == 0) {
Demi Marie Obenour16442cc2022-11-26 22:19:48 -05001673 TEST_EQUAL(my_msg_md, (mbedtls_md_type_t) ref_msg_md);
1674 TEST_EQUAL(my_mgf_md, (mbedtls_md_type_t) ref_mgf_md);
1675 TEST_EQUAL(my_salt_len, ref_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001676 }
1677
Paul Bakkerbd51b262014-07-10 15:26:12 +02001678exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001679 USE_PSA_DONE();
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001680}
1681/* END_CASE */
toth92ga41954d2021-02-12 16:11:17 +01001682
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001683/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001684void x509_crt_parse_subjectkeyid(char *file, data_t *subjectKeyId, int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001685{
1686 mbedtls_x509_crt crt;
toth92ga41954d2021-02-12 16:11:17 +01001687
1688 mbedtls_x509_crt_init(&crt);
Valerio Setti03a86e72025-05-28 12:01:14 +02001689 /* X509 relies on PK under the hood and the latter can use PSA to store keys
1690 * and perform operations so psa_crypto_init() must be called before. */
1691 USE_PSA_INIT();
toth92ga41954d2021-02-12 16:11:17 +01001692
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001693 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001694
toth92g357b2972021-05-04 15:41:35 +02001695 if (ref_ret == 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001696 TEST_EQUAL(crt.subject_key_id.tag, MBEDTLS_ASN1_OCTET_STRING);
1697 TEST_EQUAL(memcmp(crt.subject_key_id.p, subjectKeyId->x, subjectKeyId->len), 0);
1698 TEST_EQUAL(crt.subject_key_id.len, subjectKeyId->len);
toth92g357b2972021-05-04 15:41:35 +02001699 } else {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001700 TEST_EQUAL(crt.subject_key_id.tag, 0);
1701 TEST_EQUAL(crt.subject_key_id.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001702 }
1703
1704exit:
1705 mbedtls_x509_crt_free(&crt);
Valerio Setti03a86e72025-05-28 12:01:14 +02001706 USE_PSA_DONE();
toth92ga41954d2021-02-12 16:11:17 +01001707}
1708/* END_CASE */
1709
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001710/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Przemek Stekiel2568d472023-04-06 09:23:25 +02001711void x509_crt_parse_authoritykeyid(char *file,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001712 data_t *keyId,
toth92g5042b102021-05-06 08:22:17 +02001713 char *authorityKeyId_issuer,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001714 data_t *serial,
toth92g5042b102021-05-06 08:22:17 +02001715 int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001716{
1717 mbedtls_x509_crt crt;
Przemek Stekiel39dbe232023-04-03 10:19:22 +02001718 mbedtls_x509_subject_alternative_name san;
David Horstmann9a3a1a62023-06-22 16:59:09 +01001719 char name_buf[128];
toth92ga41954d2021-02-12 16:11:17 +01001720
1721 mbedtls_x509_crt_init(&crt);
Valerio Setti03a86e72025-05-28 12:01:14 +02001722 /* X509 relies on PK under the hood and the latter can use PSA to store keys
1723 * and perform operations so psa_crypto_init() must be called before. */
1724 USE_PSA_INIT();
toth92ga41954d2021-02-12 16:11:17 +01001725
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001726 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001727
toth92g357b2972021-05-04 15:41:35 +02001728 if (ref_ret == 0) {
toth92ga41954d2021-02-12 16:11:17 +01001729 /* KeyId test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001730 if (keyId->len > 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001731 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, MBEDTLS_ASN1_OCTET_STRING);
1732 TEST_EQUAL(memcmp(crt.authority_key_id.keyIdentifier.p, keyId->x, keyId->len), 0);
1733 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, keyId->len);
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001734 } else {
1735 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1736 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001737 }
toth92ga41954d2021-02-12 16:11:17 +01001738
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001739
toth92ga41954d2021-02-12 16:11:17 +01001740 /* Issuer test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001741 if (strlen(authorityKeyId_issuer) > 0) {
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001742 mbedtls_x509_sequence *issuerPtr = &crt.authority_key_id.authorityCertIssuer;
Przemek Stekiel01984212023-02-09 09:29:34 +01001743
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001744 TEST_EQUAL(mbedtls_x509_parse_subject_alt_name(&issuerPtr->buf, &san), 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001745
David Horstmann9a3a1a62023-06-22 16:59:09 +01001746 TEST_ASSERT(mbedtls_x509_dn_gets(name_buf, sizeof(name_buf),
1747 &san.san.directory_name)
1748 > 0);
1749 TEST_EQUAL(strcmp(name_buf, authorityKeyId_issuer), 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001750
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001751 mbedtls_x509_free_subject_alt_name(&san);
toth92ga41954d2021-02-12 16:11:17 +01001752 }
1753
1754 /* Serial test */
Przemek Stekielff9c2992023-05-16 19:14:19 +02001755 if (serial->len > 0) {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001756 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001757 MBEDTLS_ASN1_INTEGER);
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001758 TEST_EQUAL(memcmp(crt.authority_key_id.authorityCertSerialNumber.p,
Przemek Stekielff9c2992023-05-16 19:14:19 +02001759 serial->x, serial->len), 0);
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001760 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, serial->len);
Przemek Stekiel05d5c3e2023-05-16 16:24:44 +02001761 } else {
1762 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1763 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001764 }
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001765
toth92g357b2972021-05-04 15:41:35 +02001766 } else {
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001767 TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1768 TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001769
Przemek Stekiela6a0a792023-04-24 10:18:52 +02001770 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1771 TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
toth92ga41954d2021-02-12 16:11:17 +01001772 }
1773
1774exit:
1775 mbedtls_x509_crt_free(&crt);
Valerio Setti03a86e72025-05-28 12:01:14 +02001776 USE_PSA_DONE();
toth92ga41954d2021-02-12 16:11:17 +01001777}
1778/* END_CASE */
Sam Berry4aee6a22024-07-19 15:00:41 +01001779
Harry Ramsey94c30652024-09-19 16:13:32 +01001780/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Sam Berry4aee6a22024-07-19 15:00:41 +01001781void oid_get_numeric_string(data_t *oid, int error_ret, char *result_str)
1782{
1783 char buf[256];
1784 mbedtls_asn1_buf input_oid = { 0, 0, NULL };
1785 int ret;
1786
1787 input_oid.tag = MBEDTLS_ASN1_OID;
1788 /* Test that an empty OID is not dereferenced */
1789 input_oid.p = oid->len ? oid->x : (void *) 1;
1790 input_oid.len = oid->len;
1791
1792 ret = mbedtls_oid_get_numeric_string(buf, sizeof(buf), &input_oid);
1793
1794 if (error_ret == 0) {
1795 TEST_EQUAL(ret, strlen(result_str));
1796 TEST_ASSERT(ret >= 3);
1797 TEST_EQUAL(strcmp(buf, result_str), 0);
1798 } else {
1799 TEST_EQUAL(ret, error_ret);
1800 }
1801}
1802/* END_CASE */