blob: 71ab32cbd56467be3a6a8260b7ab7a4be97f91a9 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Azim Khand30ca132017-06-09 04:32:58 +01002#include "mbedtls/bignum.h"
Andres AG4b76aec2016-09-23 13:16:02 +01003#include "mbedtls/x509.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00004#include "mbedtls/x509_crt.h"
5#include "mbedtls/x509_crl.h"
6#include "mbedtls/x509_csr.h"
7#include "mbedtls/pem.h"
8#include "mbedtls/oid.h"
9#include "mbedtls/base64.h"
Chris Jones9f7a6932021-04-14 12:12:09 +010010#include "mbedtls/error.h"
Valerio Setti178b5bd2023-02-13 10:04:28 +010011#include "mbedtls/pk.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +010012#include "string.h"
Paul Bakkerb63b0af2011-01-13 17:54:59 +000013
Glenn Strauss6f545ac2022-10-25 15:02:14 -040014#include "x509_invasive.h"
15
Simon Butcher9e24b512017-07-28 12:15:13 +010016#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
Hanno Becker3b1422e2017-07-26 13:38:02 +010017#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
Gilles Peskine449bd832023-01-11 14:50:10 +010018 than the current threshold 19. To test larger values, please \
19 adapt the script tests/data_files/dir-max/long.sh."
Hanno Becker3b1422e2017-07-26 13:38:02 +010020#endif
21
Hanno Becker20a4ade2019-06-03 14:27:03 +010022/* Test-only profile allowing all digests, PK algorithms, and curves. */
23const mbedtls_x509_crt_profile profile_all =
24{
25 0xFFFFFFFF, /* Any MD */
26 0xFFFFFFFF, /* Any PK alg */
27 0xFFFFFFFF, /* Any curve */
28 1024,
29};
30
Gilles Peskineef86ab22017-05-05 18:59:02 +020031/* Profile for backward compatibility. Allows SHA-1, unlike the default
32 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020033const mbedtls_x509_crt_profile compat_profile =
34{
Gilles Peskine449bd832023-01-11 14:50:10 +010035 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) |
36 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160) |
37 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
38 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
39 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
40 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubney28015e12022-04-21 08:12:59 +010041 0xFFFFFFFF, /* Any PK alg */
42 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020043 1024,
44};
45
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020046const mbedtls_x509_crt_profile profile_rsa3072 =
47{
Gilles Peskine449bd832023-01-11 14:50:10 +010048 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
49 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
50 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
51 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_RSA),
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020052 0,
53 3072,
54};
55
56const mbedtls_x509_crt_profile profile_sha512 =
57{
Gilles Peskine449bd832023-01-11 14:50:10 +010058 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Thomas Daubney28015e12022-04-21 08:12:59 +010059 0xFFFFFFFF, /* Any PK alg */
60 0xFFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020061 1024,
62};
63
Gilles Peskine449bd832023-01-11 14:50:10 +010064int verify_none(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000065{
Paul Bakker5a624082011-01-18 16:31:52 +000066 ((void) data);
67 ((void) crt);
68 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010069 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020070
Paul Bakker915275b2012-09-28 07:10:55 +000071 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000072}
73
Gilles Peskine449bd832023-01-11 14:50:10 +010074int verify_all(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Paul Bakkerb63b0af2011-01-13 17:54:59 +000075{
Paul Bakker5a624082011-01-18 16:31:52 +000076 ((void) data);
77 ((void) crt);
78 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000079 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000080
Paul Bakkerb63b0af2011-01-13 17:54:59 +000081 return 0;
82}
83
Jarno Lamsa03cd1202019-03-27 15:45:04 +020084#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +010085int ca_callback_fail(void *data, mbedtls_x509_crt const *child, mbedtls_x509_crt **candidates)
Jarno Lamsa557426a2019-03-27 17:08:29 +020086{
87 ((void) data);
88 ((void) child);
89 ((void) candidates);
90
91 return -1;
92}
Przemek Stekielcd204992022-04-27 15:33:43 +020093#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010094int ca_callback(void *data, mbedtls_x509_crt const *child,
95 mbedtls_x509_crt **candidates)
Jarno Lamsa03cd1202019-03-27 15:45:04 +020096{
Hanno Beckercbb59032019-03-28 14:14:22 +000097 int ret = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +020098 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +000099 mbedtls_x509_crt *first;
100
101 /* This is a test-only implementation of the CA callback
102 * which always returns the entire list of trusted certificates.
103 * Production implementations managing a large number of CAs
104 * should use an efficient presentation and lookup for the
105 * set of trusted certificates (such as a hashtable) and only
106 * return those trusted certificates which satisfy basic
107 * parental checks, such as the matching of child `Issuer`
108 * and parent `Subject` field. */
109 ((void) child);
110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
112 if (first == NULL) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000113 ret = -1;
114 goto exit;
115 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_x509_crt_init(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000119 ret = -1;
120 goto exit;
121 }
122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 while (ca->next != NULL) {
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200124 ca = ca->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Hanno Beckercbb59032019-03-28 14:14:22 +0000126 ret = -1;
127 goto exit;
128 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200129 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000130
131exit:
132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 if (ret != 0) {
134 mbedtls_x509_crt_free(first);
135 mbedtls_free(first);
Hanno Beckercbb59032019-03-28 14:14:22 +0000136 first = NULL;
137 }
138
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200139 *candidates = first;
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 return ret;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200141}
Przemek Stekielcd204992022-04-27 15:33:43 +0200142#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200143#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145int verify_fatal(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200146{
147 int *levels = (int *) data;
148
149 ((void) crt);
150 ((void) certificate_depth);
151
152 /* Simulate a fatal error in the callback */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if (*levels & (1 << certificate_depth)) {
154 *flags |= (1 << certificate_depth);
155 return -1 - certificate_depth;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200156 }
157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return 0;
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200159}
160
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900161/* strsep() not available on Windows */
162char *mystrsep(char **stringp, const char *delim)
163{
164 const char *p;
165 char *ret = *stringp;
166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 if (*stringp == NULL) {
168 return NULL;
169 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 for (;; (*stringp)++) {
172 if (**stringp == '\0') {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900173 *stringp = NULL;
174 goto done;
175 }
176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 for (p = delim; *p != '\0'; p++) {
178 if (**stringp == *p) {
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900179 **stringp = '\0';
180 (*stringp)++;
181 goto done;
182 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 }
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900184 }
185
186done:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return ret;
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900188}
189
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200190#if defined(MBEDTLS_X509_CRT_PARSE_C)
191typedef struct {
192 char buf[512];
193 char *p;
194} verify_print_context;
195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196void verify_print_init(verify_print_context *ctx)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200197{
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 memset(ctx, 0, sizeof(verify_print_context));
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200199 ctx->p = ctx->buf;
200}
201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202int verify_print(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200203{
204 int ret;
205 verify_print_context *ctx = (verify_print_context *) data;
206 char *p = ctx->p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 size_t n = ctx->buf + sizeof(ctx->buf) - ctx->p;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200208 ((void) flags);
209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 ret = mbedtls_snprintf(p, n, "depth %d - serial ", certificate_depth);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200211 MBEDTLS_X509_SAFE_SNPRINTF;
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200214 MBEDTLS_X509_SAFE_SNPRINTF;
215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 ret = mbedtls_snprintf(p, n, " - subject ");
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200217 MBEDTLS_X509_SAFE_SNPRINTF;
218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200220 MBEDTLS_X509_SAFE_SNPRINTF;
221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 ret = mbedtls_snprintf(p, n, " - flags 0x%08x\n", *flags);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200223 MBEDTLS_X509_SAFE_SNPRINTF;
224
225 ctx->p = p;
226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 return 0;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200228}
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230int verify_parse_san(mbedtls_x509_subject_alternative_name *san,
231 char **buf, size_t *size)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200232{
233 int ret;
234 size_t i;
235 char *p = *buf;
236 size_t n = *size;
237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 ret = mbedtls_snprintf(p, n, "type : %d", san->type);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200239 MBEDTLS_X509_SAFE_SNPRINTF;
240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 switch (san->type) {
242 case (MBEDTLS_X509_SAN_OTHER_NAME):
243 ret = mbedtls_snprintf(p, n, "\notherName :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300244 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
247 &san->san.other_name.value.hardware_module_name.oid) != 0) {
248 ret = mbedtls_snprintf(p, n, " hardware module name :");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300249 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 ret = mbedtls_snprintf(p, n, " hardware type : ");
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300251 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 ret = mbedtls_oid_get_numeric_string(p,
254 n,
255 &san->san.other_name.value.hardware_module_name.oid);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300256 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 ret = mbedtls_snprintf(p, n, ", hardware serial number : ");
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 for (i = 0; i < san->san.other_name.value.hardware_module_name.val.len; i++) {
262 ret = mbedtls_snprintf(p,
263 n,
264 "%02X",
265 san->san.other_name.value.hardware_module_name.val.p[i]);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -0300266 MBEDTLS_X509_SAFE_SNPRINTF;
267 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200268 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 break;/* MBEDTLS_OID_ON_HW_MODULE_NAME */
270 case (MBEDTLS_X509_SAN_DNS_NAME):
271 ret = mbedtls_snprintf(p, n, "\ndNSName : ");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200272 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (san->san.unstructured_name.len >= n) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200274 *p = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200276 }
277 n -= san->san.unstructured_name.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 for (i = 0; i < san->san.unstructured_name.len; i++) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200279 *p++ = san->san.unstructured_name.p[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 }
281 break;/* MBEDTLS_X509_SAN_DNS_NAME */
Przemek Stekiel608e3ef2023-02-09 14:47:50 +0100282 case (MBEDTLS_X509_SAN_RFC822_NAME):
283 ret = mbedtls_snprintf(p, n, "\nrfc822Name : ");
284 MBEDTLS_X509_SAFE_SNPRINTF;
285 if (san->san.unstructured_name.len >= n) {
286 *p = '\0';
287 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
288 }
289 n -= san->san.unstructured_name.len;
290 for (i = 0; i < san->san.unstructured_name.len; i++) {
291 *p++ = san->san.unstructured_name.p[i];
292 }
293 break;/* MBEDTLS_X509_SAN_RFC822_NAME */
Andrzej Kureke12b01d2023-01-10 06:47:38 -0500294 case (MBEDTLS_X509_SAN_DIRECTORY_NAME):
295 ret = mbedtls_snprintf(p, n, "\ndirectoryName : ");
296 MBEDTLS_X509_SAFE_SNPRINTF;
297 ret = mbedtls_x509_dn_gets(p, n, &san->san.directory_name);
298 if (ret < 0) {
299 return ret;
300 }
301
302 p += ret;
303 n -= ret;
304 break;/* MBEDTLS_X509_SAN_DIRECTORY_NAME */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200305 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 /*
307 * Should not happen.
308 */
309 return -1;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200310 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 ret = mbedtls_snprintf(p, n, "\n");
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200312 MBEDTLS_X509_SAFE_SNPRINTF;
313
314 *size = n;
315 *buf = p;
316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200318}
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320int parse_crt_ext_cb(void *p_ctx, mbedtls_x509_crt const *crt, mbedtls_x509_buf const *oid,
321 int critical, const unsigned char *cp, const unsigned char *end)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200322{
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 (void) crt;
324 (void) critical;
325 mbedtls_x509_buf *new_oid = (mbedtls_x509_buf *) p_ctx;
326 if (oid->tag == MBEDTLS_ASN1_OID &&
327 MBEDTLS_OID_CMP(MBEDTLS_OID_CERTIFICATE_POLICIES, oid) == 0) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200328 /* Handle unknown certificate policy */
329 int ret, parse_ret = 0;
330 size_t len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 unsigned char **p = (unsigned char **) &cp;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200332
333 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 ret = mbedtls_asn1_get_tag(p, end, &len,
335 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
336 if (ret != 0) {
337 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
338 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (*p + len != end) {
341 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
342 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
343 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200344
345 /*
346 * Cannot be an empty sequence.
347 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (len == 0) {
349 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
350 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
351 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 while (*p < end) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200354 const unsigned char *policy_end;
355
356 /*
357 * Get the policy sequence
358 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
360 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
361 0) {
362 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
363 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200364
365 policy_end = *p + len;
366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
368 MBEDTLS_ASN1_OID)) != 0) {
369 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
370 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200371
Nicola Di Lietob77fad82020-06-17 17:57:36 +0200372 /*
373 * Recognize exclusively the policy with OID 1
374 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (len != 1 || *p[0] != 1) {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200376 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200378
379 *p += len;
380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 /*
382 * If there is an optional qualifier, then *p < policy_end
383 * Check the Qualifier len to verify it doesn't exceed policy_end.
384 */
385 if (*p < policy_end) {
386 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
387 MBEDTLS_ASN1_CONSTRUCTED |
388 MBEDTLS_ASN1_SEQUENCE)) != 0) {
389 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
390 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200391 /*
392 * Skip the optional policy qualifiers.
393 */
394 *p += len;
395 }
396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 if (*p != policy_end) {
398 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
399 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
400 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200401 }
402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (*p != end) {
404 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
405 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
406 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 return parse_ret;
409 } else if (new_oid != NULL && new_oid->tag == oid->tag && new_oid->len == oid->len &&
410 memcmp(new_oid->p, oid->p, oid->len) == 0) {
411 return 0;
412 } else {
413 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
414 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200415 }
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +0200416}
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200417#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200418/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000419
Paul Bakker33b43f12013-08-20 11:48:36 +0200420/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200422 * END_DEPENDENCIES
423 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000424
Thomas Daubney5c9c2ce2022-06-06 16:36:43 +0100425/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100426void x509_accessor_ext_types(int ext_type, int has_ext_type)
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100427{
428 mbedtls_x509_crt crt;
429 int expected_result = ext_type & has_ext_type;
430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200432 USE_PSA_INIT();
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100433
434 crt.ext_types = ext_type;
435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 TEST_ASSERT(mbedtls_x509_crt_has_ext_type(&crt, has_ext_type) == expected_result);
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100437
valerio32f2ac92023-04-20 11:59:52 +0200438exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200440 USE_PSA_DONE();
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100441}
442/* END_CASE */
443
Glenn Strauss6f545ac2022-10-25 15:02:14 -0400444/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_TEST_HOOKS */
445void x509_crt_parse_cn_inet_pton(const char *cn, data_t *exp, int ref_ret)
446{
447 uint32_t addr[4];
448 size_t addrlen = mbedtls_x509_crt_parse_cn_inet_pton(cn, addr);
449 TEST_EQUAL(addrlen, (size_t) ref_ret);
450
451 if (addrlen) {
452 ASSERT_COMPARE(exp->x, exp->len, addr, addrlen);
453 }
454}
455/* END_CASE */
456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500458void x509_parse_san(char *crt_file, char *result_str, int parse_result)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200459{
Ron Eldor890819a2019-05-13 19:03:04 +0300460 int ret;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200461 mbedtls_x509_crt crt;
Ron Eldor890819a2019-05-13 19:03:04 +0300462 mbedtls_x509_subject_alternative_name san;
463 mbedtls_x509_sequence *cur = NULL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200464 char buf[2000];
465 char *p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 size_t n = sizeof(buf);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200469 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 memset(buf, 0, 2000);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200471
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500472 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), parse_result);
Ron Eldor890819a2019-05-13 19:03:04 +0300473
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500474 if (parse_result != 0) {
475 goto exit;
476 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 if (crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Ron Eldor890819a2019-05-13 19:03:04 +0300478 cur = &crt.subject_alt_names;
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 while (cur != NULL) {
480 ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
481 TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
Ron Eldor890819a2019-05-13 19:03:04 +0300482 /*
483 * If san type not supported, ignore.
484 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (ret == 0) {
Andrzej Kurekd40c2b62023-02-13 07:01:59 -0500486 ret = verify_parse_san(&san, &p, &n);
487 mbedtls_x509_free_subject_alt_name(&san);
488 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 }
Ron Eldor890819a2019-05-13 19:03:04 +0300490 cur = cur->next;
491 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200492 }
493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 TEST_ASSERT(strcmp(buf, result_str) == 0);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200495
496exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200498 USE_PSA_DONE();
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200499}
500/* END_CASE */
501
Hanno Becker612a2f12020-10-09 09:19:39 +0100502/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100503void x509_cert_info(char *crt_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000504{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000506 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000507 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200510 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
514 res = mbedtls_x509_crt_info(buf, 2000, "", &crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 TEST_ASSERT(res != -1);
517 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200520
521exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200523 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000524}
Paul Bakker33b43f12013-08-20 11:48:36 +0200525/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000526
Hanno Becker612a2f12020-10-09 09:19:39 +0100527/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100528void mbedtls_x509_crl_info(char *crl_file, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000529{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000531 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000532 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200535 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == 0);
539 res = mbedtls_x509_crl_info(buf, 2000, "", &crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 TEST_ASSERT(res != -1);
542 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200545
546exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +0200548 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000549}
Paul Bakker33b43f12013-08-20 11:48:36 +0200550/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000551
Andres AGa39db392016-12-08 17:10:38 +0000552/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100553void mbedtls_x509_crl_parse(char *crl_file, int result)
Andres AGa39db392016-12-08 17:10:38 +0000554{
555 mbedtls_x509_crl crl;
556 char buf[2000];
557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200559 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 memset(buf, 0, 2000);
Andres AGa39db392016-12-08 17:10:38 +0000561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == result);
Andres AGa39db392016-12-08 17:10:38 +0000563
564exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +0200566 USE_PSA_DONE();
Andres AGa39db392016-12-08 17:10:38 +0000567}
568/* END_CASE */
569
Hanno Becker612a2f12020-10-09 09:19:39 +0100570/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100571void mbedtls_x509_csr_info(char *csr_file, char *result_str)
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100572{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100574 char buf[2000];
575 int res;
576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +0200578 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 memset(buf, 0, 2000);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 TEST_ASSERT(mbedtls_x509_csr_parse_file(&csr, csr_file) == 0);
582 res = mbedtls_x509_csr_info(buf, 2000, "", &csr);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 TEST_ASSERT(res != -1);
585 TEST_ASSERT(res != -2);
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200588
589exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +0200591 USE_PSA_DONE();
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100592}
593/* END_CASE */
594
Hanno Becker612a2f12020-10-09 09:19:39 +0100595/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100596void x509_verify_info(int flags, char *prefix, char *result_str)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100597{
598 char buf[2000];
599 int res;
600
Valerio Setti569c1712023-04-19 14:53:36 +0200601 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 memset(buf, 0, sizeof(buf));
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 res = mbedtls_x509_crt_verify_info(buf, sizeof(buf), prefix, flags);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 TEST_ASSERT(res >= 0);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 TEST_ASSERT(strcmp(buf, result_str) == 0);
valerio32f2ac92023-04-20 11:59:52 +0200609
610exit:
Valerio Setti569c1712023-04-19 14:53:36 +0200611 USE_PSA_DONE();
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100612}
613/* END_CASE */
614
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200615/* 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 +0100616void x509_verify_restart(char *crt_file, char *ca_file,
617 int result, int flags_result,
618 int max_ops, int min_restart, int max_restart)
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200619{
620 int ret, cnt_restart;
621 mbedtls_x509_crt_restart_ctx rs_ctx;
622 mbedtls_x509_crt crt;
623 mbedtls_x509_crt ca;
624 uint32_t flags = 0;
625
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200626 /*
627 * See comments on ecp_test_vect_restart() for op count precision.
628 *
629 * For reference, with mbed TLS 2.6 and default settings:
630 * - ecdsa_verify() for P-256: ~ 6700
631 * - ecdsa_verify() for P-384: ~ 18800
632 * - x509_verify() for server5 -> test-ca2: ~ 18800
633 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
634 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 mbedtls_x509_crt_restart_init(&rs_ctx);
636 mbedtls_x509_crt_init(&crt);
637 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200638 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200639
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
641 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 mbedtls_ecp_set_max_ops(max_ops);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200644
645 cnt_restart = 0;
646 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
648 &mbedtls_x509_crt_profile_default, NULL, &flags,
649 NULL, NULL, &rs_ctx);
650 } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 TEST_ASSERT(ret == result);
653 TEST_ASSERT(flags == (uint32_t) flags_result);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 TEST_ASSERT(cnt_restart >= min_restart);
656 TEST_ASSERT(cnt_restart <= max_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200657
658 /* Do we leak memory when aborting? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
660 &mbedtls_x509_crt_profile_default, NULL, &flags,
661 NULL, NULL, &rs_ctx);
662 TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200663
664exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 mbedtls_x509_crt_restart_free(&rs_ctx);
666 mbedtls_x509_crt_free(&crt);
667 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100668 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200669}
670/* END_CASE */
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100673void x509_verify(char *crt_file, char *ca_file, char *crl_file,
674 char *cn_name_str, int result, int flags_result,
675 char *profile_str,
676 char *verify_callback)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000677{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_x509_crt crt;
679 mbedtls_x509_crt ca;
680 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200681 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000682 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200683 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 char *cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200685 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 mbedtls_x509_crt_init(&crt);
688 mbedtls_x509_crt_init(&ca);
689 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +0200690 MD_OR_USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 if (strcmp(cn_name_str, "NULL") != 0) {
Paul Bakker33b43f12013-08-20 11:48:36 +0200693 cn_name = cn_name_str;
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (strcmp(profile_str, "") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200697 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 } else if (strcmp(profile_str, "next") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200699 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 } else if (strcmp(profile_str, "suite_b") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200701 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 } else if (strcmp(profile_str, "compat") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200703 profile = &compat_profile;
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 } else if (strcmp(profile_str, "all") == 0) {
Hanno Becker20a4ade2019-06-03 14:27:03 +0100705 profile = &profile_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 } else {
707 TEST_ASSERT("Unknown algorithm profile" == 0);
708 }
Gilles Peskineef86ab22017-05-05 18:59:02 +0200709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 if (strcmp(verify_callback, "NULL") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200711 f_vrfy = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 } else if (strcmp(verify_callback, "verify_none") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200713 f_vrfy = verify_none;
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 } else if (strcmp(verify_callback, "verify_all") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200715 f_vrfy = verify_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 } else {
717 TEST_ASSERT("No known verify callback selected" == 0);
718 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
721 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
722 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == 0);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 res = mbedtls_x509_crt_verify_with_profile(&crt,
725 &ca,
726 &crl,
727 profile,
728 cn_name,
729 &flags,
730 f_vrfy,
731 NULL);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 TEST_EQUAL(res, result);
734 TEST_EQUAL(flags, (uint32_t) flags_result);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200735
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200736#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker0350d562019-03-28 14:23:36 +0000737 /* CRLs aren't supported with CA callbacks, so skip the CA callback
Jarno Lamsadfd22c42019-04-01 15:18:53 +0300738 * version of the test if CRLs are in use. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 if (crl_file == NULL || strcmp(crl_file, "") == 0) {
Hanno Becker0350d562019-03-28 14:23:36 +0000740 flags = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 res = mbedtls_x509_crt_verify_with_ca_cb(&crt,
743 ca_callback,
744 &ca,
745 profile,
746 cn_name,
747 &flags,
748 f_vrfy,
749 NULL);
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 TEST_ASSERT(res == (result));
752 TEST_ASSERT(flags == (uint32_t) (flags_result));
Hanno Becker0350d562019-03-28 14:23:36 +0000753 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200754#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200755exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 mbedtls_x509_crt_free(&crt);
757 mbedtls_x509_crt_free(&ca);
758 mbedtls_x509_crl_free(&crl);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100759 MD_OR_USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000760}
Paul Bakker33b43f12013-08-20 11:48:36 +0200761/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000762
Jarno Lamsa557426a2019-03-27 17:08:29 +0200763/* 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 +0100764void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
765 int exp_ret)
Jarno Lamsa557426a2019-03-27 17:08:29 +0200766{
767 int ret;
768 mbedtls_x509_crt crt;
769 mbedtls_x509_crt ca;
770 uint32_t flags = 0;
Hanno Becker3f932bb2019-03-28 17:06:47 +0000771
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 mbedtls_x509_crt_init(&crt);
773 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200774 USE_PSA_INIT();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
777 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 if (strcmp(name, "NULL") == 0) {
Jarno Lamsa557426a2019-03-27 17:08:29 +0200780 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca,
784 &compat_profile, name, &flags,
785 NULL, NULL);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 TEST_ASSERT(ret == exp_ret);
788 TEST_ASSERT(flags == (uint32_t) (-1));
Jarno Lamsa557426a2019-03-27 17:08:29 +0200789exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 mbedtls_x509_crt_free(&crt);
791 mbedtls_x509_crt_free(&ca);
Valerio Setti569c1712023-04-19 14:53:36 +0200792 USE_PSA_DONE();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200793}
794/* END_CASE */
795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100797void x509_verify_callback(char *crt_file, char *ca_file, char *name,
798 int exp_ret, char *exp_vrfy_out)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200799{
800 int ret;
801 mbedtls_x509_crt crt;
802 mbedtls_x509_crt ca;
803 uint32_t flags = 0;
804 verify_print_context vrfy_ctx;
805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 mbedtls_x509_crt_init(&crt);
807 mbedtls_x509_crt_init(&ca);
valerio32f2ac92023-04-20 11:59:52 +0200808 MD_OR_USE_PSA_INIT();
809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 verify_print_init(&vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
813 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if (strcmp(name, "NULL") == 0) {
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200816 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 }
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL,
820 &compat_profile,
821 name, &flags,
822 verify_print, &vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 TEST_ASSERT(ret == exp_ret);
825 TEST_ASSERT(strcmp(vrfy_ctx.buf, exp_vrfy_out) == 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200826
827exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 mbedtls_x509_crt_free(&crt);
829 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100830 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200831}
832/* END_CASE */
833
Hanno Becker612a2f12020-10-09 09:19:39 +0100834/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100835void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
836 char *new_subject_ou,
837 char *result_str,
838 int ret)
Werner Lewis31ecb962022-06-17 15:51:55 +0100839{
840 mbedtls_x509_crt crt;
841 char buf[2000];
842 int res = 0;
843
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200845 USE_PSA_INIT();
846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 memset(buf, 0, 2000);
Werner Lewis31ecb962022-06-17 15:51:55 +0100848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100850 crt.subject.next->val.p = (unsigned char *) new_subject_ou;
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 crt.subject.next->val.len = strlen(new_subject_ou);
Werner Lewis31ecb962022-06-17 15:51:55 +0100852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
Werner Lewis31ecb962022-06-17 15:51:55 +0100854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (ret != 0) {
856 TEST_ASSERT(res == ret);
857 } else {
858 TEST_ASSERT(res != -1);
859 TEST_ASSERT(res != -2);
860 TEST_ASSERT(strcmp(buf, result_str) == 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100861 }
862exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200864 USE_PSA_DONE();
Werner Lewis31ecb962022-06-17 15:51:55 +0100865}
866/* END_CASE */
867
868/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100869void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000870{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000872 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200873 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +0200876 USE_PSA_INIT();
877
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000879
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
881 if (strcmp(entity, "subject") == 0) {
882 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
883 } else if (strcmp(entity, "issuer") == 0) {
884 res = mbedtls_x509_dn_gets(buf, 2000, &crt.issuer);
885 } else {
886 TEST_ASSERT("Unknown entity" == 0);
887 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 TEST_ASSERT(res != -1);
890 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200893
894exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200896 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000897}
Paul Bakker33b43f12013-08-20 11:48:36 +0200898/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000899
David Horstmanndb73d3b2022-10-04 16:49:16 +0100900/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100901void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
David Horstmanndb73d3b2022-10-04 16:49:16 +0100902{
valerioe50831c2023-04-20 14:48:19 +0200903 unsigned char *name = NULL;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100904 unsigned char *p;
905 size_t name_len;
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100906 mbedtls_x509_name head;
David Horstmann3cd67582022-10-17 17:59:10 +0100907 int ret;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100908
Valerio Setti569c1712023-04-19 14:53:36 +0200909 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 memset(&head, 0, sizeof(head));
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100913 p = name;
914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 ret = mbedtls_x509_get_name(&p, (name + name_len), &head);
916 if (ret == 0) {
917 mbedtls_asn1_free_named_data_list_shallow(head.next);
918 }
David Horstmanndb73d3b2022-10-04 16:49:16 +0100919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 TEST_EQUAL(ret, exp_ret);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100921
valerio32f2ac92023-04-20 11:59:52 +0200922exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 mbedtls_free(name);
Valerio Setti569c1712023-04-19 14:53:36 +0200924 USE_PSA_DONE();
David Horstmanndb73d3b2022-10-04 16:49:16 +0100925}
926/* END_CASE */
927
Werner Lewisb3acb052022-06-17 15:59:58 +0100928/* 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 +0100929void mbedtls_x509_dn_get_next(char *name_str,
930 int next_merged,
931 char *expected_oids,
932 int exp_count,
933 char *exp_dn_gets)
Werner Lewisb3acb052022-06-17 15:59:58 +0100934{
935 int ret = 0, i;
Werner Lewisac80a662022-06-23 11:58:02 +0100936 size_t len = 0, out_size;
Werner Lewisb3acb052022-06-17 15:59:58 +0100937 mbedtls_asn1_named_data *names = NULL;
Glenn Straussa4b40412022-06-26 19:32:09 -0400938 mbedtls_x509_name parsed, *parsed_cur;
Werner Lewisac80a662022-06-23 11:58:02 +0100939 // Size of buf is maximum required for test cases
940 unsigned char buf[80], *out = NULL, *c;
Werner Lewisb3acb052022-06-17 15:59:58 +0100941 const char *short_name;
942
Valerio Setti569c1712023-04-19 14:53:36 +0200943 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 memset(&parsed, 0, sizeof(parsed));
945 memset(buf, 0, sizeof(buf));
946 c = buf + sizeof(buf);
Werner Lewisac80a662022-06-23 11:58:02 +0100947 // Additional size required for trailing space
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 out_size = strlen(expected_oids) + 2;
949 ASSERT_ALLOC(out, out_size);
Werner Lewisb3acb052022-06-17 15:59:58 +0100950
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 TEST_EQUAL(mbedtls_x509_string_to_names(&names, name_str), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 ret = mbedtls_x509_write_names(&c, buf, names);
954 TEST_LE_S(0, ret);
Werner Lewisb3acb052022-06-17 15:59:58 +0100955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
957 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
958 TEST_EQUAL(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100959
960 // Iterate over names and set next_merged nodes
961 parsed_cur = &parsed;
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 for (; next_merged != 0 && parsed_cur != NULL; next_merged = next_merged >> 1) {
Werner Lewis12657cd2022-06-20 11:47:57 +0100963 parsed_cur->next_merged = next_merged & 0x01;
Werner Lewisb3acb052022-06-17 15:59:58 +0100964 parsed_cur = parsed_cur->next;
965 }
966
967 // Iterate over RDN nodes and print OID of first element to buffer
968 parsed_cur = &parsed;
969 len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 for (i = 0; parsed_cur != NULL; i++) {
971 TEST_EQUAL(mbedtls_oid_get_attr_short_name(&parsed_cur->oid,
972 &short_name), 0);
973 len += mbedtls_snprintf((char *) out + len, out_size - len, "%s ", short_name);
974 parsed_cur = mbedtls_x509_dn_get_next(parsed_cur);
Werner Lewisb3acb052022-06-17 15:59:58 +0100975 }
976 out[len-1] = 0;
977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 TEST_EQUAL(exp_count, i);
979 TEST_EQUAL(strcmp((char *) out, expected_oids), 0);
980 mbedtls_free(out);
Werner Lewisac80a662022-06-23 11:58:02 +0100981 out = NULL;
Werner Lewisb3acb052022-06-17 15:59:58 +0100982
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 out_size = strlen(exp_dn_gets) + 1;
984 ASSERT_ALLOC(out, out_size);
Werner Lewisac80a662022-06-23 11:58:02 +0100985
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 TEST_LE_S(0, mbedtls_x509_dn_gets((char *) out, out_size, &parsed));
987 TEST_EQUAL(strcmp((char *) out, exp_dn_gets), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100988exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 mbedtls_free(out);
990 mbedtls_asn1_free_named_data_list(&names);
991 mbedtls_asn1_free_named_data_list_shallow(parsed.next);
Valerio Setti569c1712023-04-19 14:53:36 +0200992 USE_PSA_DONE();
Werner Lewisb3acb052022-06-17 15:59:58 +0100993}
Werner Lewisb3acb052022-06-17 15:59:58 +0100994/* END_CASE */
995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100997void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000998{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +00001000
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001002 USE_PSA_INIT();
Paul Bakker37940d9f2009-07-10 22:38:58 +00001003
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001005
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 if (strcmp(entity, "valid_from") == 0) {
1007 TEST_ASSERT(mbedtls_x509_time_is_past(&crt.valid_from) == result);
1008 } else if (strcmp(entity, "valid_to") == 0) {
1009 TEST_ASSERT(mbedtls_x509_time_is_past(&crt.valid_to) == result);
1010 } else {
1011 TEST_ASSERT("Unknown entity" == 0);
1012 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001013
Paul Bakkerbd51b262014-07-10 15:26:12 +02001014exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001016 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +00001017}
Paul Bakker33b43f12013-08-20 11:48:36 +02001018/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +00001019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001021void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001022{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001026 USE_PSA_INIT();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 if (strcmp(entity, "valid_from") == 0) {
1031 TEST_ASSERT(mbedtls_x509_time_is_future(&crt.valid_from) == result);
1032 } else if (strcmp(entity, "valid_to") == 0) {
1033 TEST_ASSERT(mbedtls_x509_time_is_future(&crt.valid_to) == result);
1034 } else {
1035 TEST_ASSERT("Unknown entity" == 0);
1036 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001037
Paul Bakkerbd51b262014-07-10 15:26:12 +02001038exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001040 USE_PSA_DONE();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001041}
1042/* END_CASE */
1043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001045void x509parse_crt_file(char *crt_file, int result)
Paul Bakker5a5fa922014-09-26 14:53:04 +02001046{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +02001048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001050 USE_PSA_INIT();
Paul Bakker5a5fa922014-09-26 14:53:04 +02001051
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == result);
Paul Bakker5a5fa922014-09-26 14:53:04 +02001053
1054exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001056 USE_PSA_DONE();
Paul Bakker5a5fa922014-09-26 14:53:04 +02001057}
1058/* END_CASE */
1059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001061void x509parse_crt(data_t *buf, char *result_str, int result)
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001062{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 mbedtls_x509_crt crt;
Hanno Becker612a2f12020-10-09 09:19:39 +01001064#if !defined(MBEDTLS_X509_REMOVE_INFO)
Hanno Beckerfff2d572020-10-09 09:45:19 +01001065 unsigned char output[2000] = { 0 };
Azim Khanf1aaec92017-05-30 14:23:15 +01001066 int res;
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001067#else
1068 ((void) result_str);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001069#endif
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001072 USE_PSA_INIT();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 TEST_ASSERT(mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len) == (result));
Hanno Becker612a2f12020-10-09 09:19:39 +01001075#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 if ((result) == 0) {
1077 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1078 TEST_ASSERT(res != -1);
1079 TEST_ASSERT(res != -2);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001080
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001082 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001084#endif
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 mbedtls_x509_crt_free(&crt);
1087 mbedtls_x509_crt_init(&crt);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 TEST_ASSERT(mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len) == (result));
Hanno Becker612a2f12020-10-09 09:19:39 +01001090#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if ((result) == 0) {
1092 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakker33b43f12013-08-20 11:48:36 +02001095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 TEST_ASSERT(res != -1);
1097 TEST_ASSERT(res != -2);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001098
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001100 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001102#endif /* !MBEDTLS_X509_REMOVE_INFO */
Paul Bakkerb08e6842012-02-11 18:43:20 +00001103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 mbedtls_x509_crt_free(&crt);
1105 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL,
1108 NULL) == (result));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001109#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 if ((result) == 0) {
1111 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001112
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 TEST_ASSERT(res != -1);
1114 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001117 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001119#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 mbedtls_x509_crt_free(&crt);
1122 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001123
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL,
1125 NULL) == (result));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001126#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 if ((result) == 0) {
1128 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 TEST_ASSERT(res != -1);
1131 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001134 }
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001135#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001136
1137exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001139 USE_PSA_DONE();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001140}
1141/* END_CASE */
1142
1143/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001144void x509parse_crt_cb(data_t *buf, char *result_str, int result)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001145{
1146 mbedtls_x509_crt crt;
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001147 mbedtls_x509_buf oid;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001148
1149#if !defined(MBEDTLS_X509_REMOVE_INFO)
1150 unsigned char output[2000] = { 0 };
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001151 int res;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001152#else
1153 ((void) result_str);
1154#endif
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001155
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001156 oid.tag = MBEDTLS_ASN1_OID;
1157 oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001161 USE_PSA_INIT();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
1164 &oid) == (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001165#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if ((result) == 0) {
1167 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 TEST_ASSERT(res != -1);
1170 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001173 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 memset(output, 0, 2000);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001175#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 mbedtls_x509_crt_free(&crt);
1178 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1181 &oid) == (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001182#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 if ((result) == 0) {
1184 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 TEST_ASSERT(res != -1);
1187 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001190 }
Hanno Beckerfff2d572020-10-09 09:45:19 +01001191#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001192
Paul Bakkerbd51b262014-07-10 15:26:12 +02001193exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001195 USE_PSA_DONE();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001196}
Paul Bakker33b43f12013-08-20 11:48:36 +02001197/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001198
Hanno Becker612a2f12020-10-09 09:19:39 +01001199/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001200void x509parse_crl(data_t *buf, char *result_str, int result)
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001201{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001203 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +01001204 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 mbedtls_x509_crl_init(&crl);
valerio32f2ac92023-04-20 11:59:52 +02001207 USE_PSA_INIT();
1208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 memset(output, 0, 2000);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001210
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 TEST_ASSERT(mbedtls_x509_crl_parse(&crl, buf->x, buf->len) == (result));
1213 if ((result) == 0) {
1214 res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
Paul Bakker33b43f12013-08-20 11:48:36 +02001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 TEST_ASSERT(res != -1);
1217 TEST_ASSERT(res != -2);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001220 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001221
Paul Bakkerbd51b262014-07-10 15:26:12 +02001222exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +02001224 USE_PSA_DONE();
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001225}
Paul Bakker33b43f12013-08-20 11:48:36 +02001226/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001227
Hanno Becker612a2f12020-10-09 09:19:39 +01001228/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001229void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001230{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001232 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001233 int my_ret;
1234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001236 USE_PSA_INIT();
1237
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 memset(my_out, 0, sizeof(my_out));
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001239
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
1241 TEST_ASSERT(my_ret == ref_ret);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (ref_ret == 0) {
1244 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1245 TEST_ASSERT(my_out_len == strlen(ref_out));
1246 TEST_ASSERT(strcmp(my_out, ref_out) == 0);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001247 }
1248
Paul Bakkerbd51b262014-07-10 15:26:12 +02001249exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001251 USE_PSA_DONE();
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001252}
1253/* END_CASE */
1254
Przemek Stekield7992df2023-01-25 16:19:50 +01001255/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1256void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
1257{
1258 mbedtls_x509_csr csr;
1259 char my_out[1000];
1260 int my_ret;
1261
1262 mbedtls_x509_csr_init(&csr);
valerio32f2ac92023-04-20 11:59:52 +02001263 USE_PSA_INIT();
1264
Przemek Stekield7992df2023-01-25 16:19:50 +01001265 memset(my_out, 0, sizeof(my_out));
1266
1267 my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file);
1268 TEST_ASSERT(my_ret == ref_ret);
1269
1270 if (ref_ret == 0) {
1271 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1272 TEST_ASSERT(my_out_len == strlen(ref_out));
1273 TEST_ASSERT(strcmp(my_out, ref_out) == 0);
1274 }
1275
1276exit:
1277 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001278 USE_PSA_DONE();
Przemek Stekield7992df2023-01-25 16:19:50 +01001279}
1280/* END_CASE */
1281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001283void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001286 int i;
1287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001289 USE_PSA_INIT();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 TEST_ASSERT(mbedtls_x509_crt_parse_path(&chain, crt_path) == ret);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001292
1293 /* Check how many certs we got */
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1295 if (cur->raw.p != NULL) {
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001296 i++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 }
1298 }
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 TEST_ASSERT(i == nb_crt);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001301
Paul Bakkerbd51b262014-07-10 15:26:12 +02001302exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 mbedtls_x509_crt_free(&chain);
Valerio Setti569c1712023-04-19 14:53:36 +02001304 USE_PSA_DONE();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001305}
1306/* END_CASE */
1307
Janos Follath822b2c32015-10-11 10:25:22 +02001308/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001309void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1310 int ret_chk, int flags_chk)
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001311{
1312 char file_buf[128];
1313 int ret;
1314 uint32_t flags;
1315 mbedtls_x509_crt trusted, chain;
1316
1317 /*
1318 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1319 * with NN.crt signed by NN-1.crt
1320 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 mbedtls_x509_crt_init(&trusted);
1322 mbedtls_x509_crt_init(&chain);
valerio32f2ac92023-04-20 11:59:52 +02001323 MD_OR_USE_PSA_INIT();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001324
1325 /* Load trusted root */
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, ca_file) == 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001327
1328 /* Load a chain with nb_int intermediates (from 01 to nb_int),
1329 * plus one "end-entity" cert (nb_int + 1) */
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001330 ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 nb_int + 1);
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001332 TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, file_buf) == 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001334
1335 /* Try to verify that chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1337 NULL, NULL);
1338 TEST_ASSERT(ret == ret_chk);
1339 TEST_ASSERT(flags == (uint32_t) flags_chk);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001340
1341exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 mbedtls_x509_crt_free(&chain);
1343 mbedtls_x509_crt_free(&trusted);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001344 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001345}
1346/* END_CASE */
1347
1348/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001349void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1350 int flags_result, int result,
1351 char *profile_name, int vrfy_fatal_lvls)
Janos Follath822b2c32015-10-11 10:25:22 +02001352{
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 char *act;
Janos Follath822b2c32015-10-11 10:25:22 +02001354 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001355 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001356 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001357 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +02001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 mbedtls_x509_crt_init(&chain);
1360 mbedtls_x509_crt_init(&trusted);
valerio32f2ac92023-04-20 11:59:52 +02001361 MD_OR_USE_PSA_INIT();
Janos Follath822b2c32015-10-11 10:25:22 +02001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 while ((act = mystrsep(&chain_paths, " ")) != NULL) {
1364 TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, act) == 0);
1365 }
1366 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, trusted_ca) == 0);
Janos Follath822b2c32015-10-11 10:25:22 +02001367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 if (strcmp(profile_name, "") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001369 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 } else if (strcmp(profile_name, "next") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001371 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 } else if (strcmp(profile_name, "suiteb") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001373 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 } else if (strcmp(profile_name, "rsa3072") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001375 profile = &profile_rsa3072;
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 } else if (strcmp(profile_name, "sha512") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001377 profile = &profile_sha512;
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 }
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001379
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1381 NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
Janos Follathef4f2582015-10-11 16:17:27 +02001382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 TEST_ASSERT(res == (result));
1384 TEST_ASSERT(flags == (uint32_t) (flags_result));
Janos Follath822b2c32015-10-11 10:25:22 +02001385
1386exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 mbedtls_x509_crt_free(&trusted);
1388 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001389 MD_OR_USE_PSA_DONE();
Janos Follath822b2c32015-10-11 10:25:22 +02001390}
1391/* END_CASE */
1392
Hanno Becker612a2f12020-10-09 09:19:39 +01001393/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001394void x509_oid_desc(data_t *buf, char *ref_desc)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001395{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001397 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001398 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001399
Valerio Setti569c1712023-04-19 14:53:36 +02001400 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001403 oid.p = buf->x;
1404 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 ret = mbedtls_oid_get_extended_key_usage(&oid, &desc);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001407
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 if (strcmp(ref_desc, "notfound") == 0) {
1409 TEST_ASSERT(ret != 0);
1410 TEST_ASSERT(desc == NULL);
1411 } else {
1412 TEST_ASSERT(ret == 0);
1413 TEST_ASSERT(desc != NULL);
1414 TEST_ASSERT(strcmp(desc, ref_desc) == 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001415 }
valerio32f2ac92023-04-20 11:59:52 +02001416
1417exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001418 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001419}
1420/* END_CASE */
1421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001423void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001424{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001426 char num_buf[100];
1427
Valerio Setti569c1712023-04-19 14:53:36 +02001428 USE_PSA_INIT();
valerio32f2ac92023-04-20 11:59:52 +02001429
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001430 memset(num_buf, 0x2a, sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001433 oid.p = oid_buf->x;
1434 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001435
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001436 TEST_ASSERT((size_t) blen <= sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 TEST_ASSERT(mbedtls_oid_get_numeric_string(num_buf, blen, &oid) == ret);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 if (ret >= 0) {
1441 TEST_ASSERT(num_buf[ret] == 0);
1442 TEST_ASSERT(strcmp(num_buf, numstr) == 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001443 }
valerio32f2ac92023-04-20 11:59:52 +02001444
1445exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001446 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001447}
1448/* END_CASE */
1449
TRodziewicz442fdc22021-06-07 13:52:23 +02001450/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001451void x509_check_key_usage(char *crt_file, int usage, int ret)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001452{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001456 USE_PSA_INIT();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 TEST_ASSERT(mbedtls_x509_crt_check_key_usage(&crt, usage) == ret);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001461
Paul Bakkerbd51b262014-07-10 15:26:12 +02001462exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001464 USE_PSA_DONE();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001465}
1466/* END_CASE */
1467
TRodziewicz442fdc22021-06-07 13:52:23 +02001468/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001469void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1470 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001471{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001473
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 mbedtls_x509_crt_init(&crt);
valerio32f2ac92023-04-20 11:59:52 +02001475 USE_PSA_INIT();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 TEST_ASSERT(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x,
1480 oid->len) == ret);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001481
Paul Bakkerbd51b262014-07-10 15:26:12 +02001482exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001484 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001485}
1486/* END_CASE */
1487
Andres AG4b76aec2016-09-23 13:16:02 +01001488/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001489void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1490 int day, int hour, int min, int sec)
Andres AG4b76aec2016-09-23 13:16:02 +01001491{
1492 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001493 unsigned char buf[21];
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 unsigned char *start = buf;
1495 unsigned char *end = buf;
Andres AG4b76aec2016-09-23 13:16:02 +01001496
Valerio Setti569c1712023-04-19 14:53:36 +02001497 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 memset(&time, 0x00, sizeof(time));
1499 *end = (unsigned char) tag; end++;
1500 *end = strlen(time_str);
1501 TEST_ASSERT(*end < 20);
Andres AG4b76aec2016-09-23 13:16:02 +01001502 end++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 memcpy(end, time_str, (size_t) *(end - 1));
Andres AG4b76aec2016-09-23 13:16:02 +01001504 end += *(end - 1);
1505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 TEST_ASSERT(mbedtls_x509_get_time(&start, end, &time) == ret);
1507 if (ret == 0) {
1508 TEST_ASSERT(year == time.year);
1509 TEST_ASSERT(mon == time.mon);
1510 TEST_ASSERT(day == time.day);
1511 TEST_ASSERT(hour == time.hour);
1512 TEST_ASSERT(min == time.min);
1513 TEST_ASSERT(sec == time.sec);
Andres AG4b76aec2016-09-23 13:16:02 +01001514 }
valerio32f2ac92023-04-20 11:59:52 +02001515exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001516 USE_PSA_DONE();
Andres AG4b76aec2016-09-23 13:16:02 +01001517}
1518/* END_CASE */
1519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001521void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1522 int ref_msg_md, int ref_mgf_md,
1523 int ref_salt_len, int ref_ret)
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001524{
1525 int my_ret;
Ronald Cronac6ae352020-06-26 14:33:03 +02001526 mbedtls_x509_buf buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001528 int my_salt_len;
1529
Valerio Setti569c1712023-04-19 14:53:36 +02001530 USE_PSA_INIT();
1531
Ronald Cronac6ae352020-06-26 14:33:03 +02001532 buf.p = params->x;
1533 buf.len = params->len;
1534 buf.tag = params_tag;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001535
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1537 &my_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 TEST_ASSERT(my_ret == ref_ret);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 if (ref_ret == 0) {
1542 TEST_ASSERT(my_msg_md == (mbedtls_md_type_t) ref_msg_md);
1543 TEST_ASSERT(my_mgf_md == (mbedtls_md_type_t) ref_mgf_md);
1544 TEST_ASSERT(my_salt_len == ref_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001545 }
1546
Paul Bakkerbd51b262014-07-10 15:26:12 +02001547exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001548 USE_PSA_DONE();
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001549}
1550/* END_CASE */
toth92ga41954d2021-02-12 16:11:17 +01001551
Przemek Stekiel8661fed2023-01-31 08:30:06 +01001552/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001553void x509_crt_parse_subjectkeyid(char *file, data_t *subjectKeyId, int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001554{
Przemek Stekiel67d3f522023-05-06 21:14:12 +02001555 size_t subjectKeyIdLength = subjectKeyIdLength_arg;
toth92ga41954d2021-02-12 16:11:17 +01001556 mbedtls_x509_crt crt;
toth92ga41954d2021-02-12 16:11:17 +01001557
1558 mbedtls_x509_crt_init(&crt);
1559
Przemek Stekiel2568d472023-04-06 09:23:25 +02001560 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, file) == ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001561
toth92g357b2972021-05-04 15:41:35 +02001562 if (ref_ret == 0) {
1563 TEST_ASSERT(crt.subject_key_id.tag == MBEDTLS_ASN1_OCTET_STRING);
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001564 TEST_ASSERT(memcmp(crt.subject_key_id.p, subjectKeyId->x, subjectKeyId->len) == 0);
1565 TEST_ASSERT(crt.subject_key_id.len == subjectKeyId->len);
toth92g357b2972021-05-04 15:41:35 +02001566 } else {
1567 TEST_ASSERT(crt.subject_key_id.tag == 0);
1568 TEST_ASSERT(crt.subject_key_id.len == 0);
toth92ga41954d2021-02-12 16:11:17 +01001569 }
1570
1571exit:
1572 mbedtls_x509_crt_free(&crt);
1573}
1574/* END_CASE */
1575
Przemek Stekiel8661fed2023-01-31 08:30:06 +01001576/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Przemek Stekiel2568d472023-04-06 09:23:25 +02001577void x509_crt_parse_authoritykeyid(char *file,
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001578 data_t* keyId,
toth92g5042b102021-05-06 08:22:17 +02001579 char *authorityKeyId_issuer,
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001580 data_t* serial,
toth92g5042b102021-05-06 08:22:17 +02001581 int ref_ret)
toth92ga41954d2021-02-12 16:11:17 +01001582{
1583 mbedtls_x509_crt crt;
toth92ga41954d2021-02-12 16:11:17 +01001584 int bufferCounter = 0;
1585 size_t issuerCounter = 0;
Przemek Stekiel67d3f522023-05-06 21:14:12 +02001586 size_t keyIdLength = keyIdLength_arg;
1587 size_t serialLength = serialLength_arg;
toth92g357b2972021-05-04 15:41:35 +02001588 unsigned int result = 0;
Przemek Stekiel39dbe232023-04-03 10:19:22 +02001589 mbedtls_x509_subject_alternative_name san;
Przemek Stekiel01984212023-02-09 09:29:34 +01001590 mbedtls_x509_name *pname = NULL;
toth92ga41954d2021-02-12 16:11:17 +01001591
1592 mbedtls_x509_crt_init(&crt);
1593
Przemek Stekiel2568d472023-04-06 09:23:25 +02001594 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, file) == ref_ret);
toth92ga41954d2021-02-12 16:11:17 +01001595
toth92g357b2972021-05-04 15:41:35 +02001596 if (ref_ret == 0) {
toth92ga41954d2021-02-12 16:11:17 +01001597 /* KeyId test */
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001598 if (crt.authority_key_id.keyIdentifier.len > 0)
1599 {
1600 TEST_ASSERT(crt.authority_key_id.keyIdentifier.tag == MBEDTLS_ASN1_OCTET_STRING);
1601 TEST_ASSERT(memcmp(crt.authority_key_id.keyIdentifier.p, keyId->x, keyId->len) == 0);
1602 TEST_ASSERT(crt.authority_key_id.keyIdentifier.len == keyId->len);
1603 }
toth92ga41954d2021-02-12 16:11:17 +01001604
1605 /* Issuer test */
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001606 if (crt.authority_key_id.authorityCertIssuer.buf.len > 0)
1607 {
1608 mbedtls_x509_sequence *issuerPtr = &crt.authority_key_id.authorityCertIssuer;
Przemek Stekiel01984212023-02-09 09:29:34 +01001609
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001610 TEST_ASSERT(mbedtls_x509_parse_subject_alt_name(&issuerPtr->buf, &san) == 0);
Przemek Stekiel01984212023-02-09 09:29:34 +01001611
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001612 pname = &san.san.directory_name;
Przemek Stekiel01984212023-02-09 09:29:34 +01001613
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001614 while (pname != NULL) {
1615 for (issuerCounter = 0; issuerCounter < pname->val.len; issuerCounter++) {
1616 result |=
1617 (authorityKeyId_issuer[bufferCounter++] != pname->val.p[issuerCounter]);
1618 }
1619 bufferCounter++; /* Skipping the slash */
1620 pname = pname->next;
toth92ga41954d2021-02-12 16:11:17 +01001621 }
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001622 mbedtls_x509_free_subject_alt_name(&san);
1623 TEST_ASSERT(result == 0);
toth92ga41954d2021-02-12 16:11:17 +01001624 }
1625
1626 /* Serial test */
Przemek Stekiel1969f6a2023-04-18 08:38:16 +02001627 if (crt.authority_key_id.authorityCertSerialNumber.len > 0)
1628 {
1629 TEST_ASSERT(crt.authority_key_id.authorityCertSerialNumber.tag ==
1630 MBEDTLS_ASN1_INTEGER);
1631 TEST_ASSERT(memcmp(crt.authority_key_id.authorityCertSerialNumber.p,
1632 serial->x, serial->len) == 0);
1633 TEST_ASSERT(crt.authority_key_id.authorityCertSerialNumber.len == serial->len);
1634 }
Przemek Stekiel0ad10062023-04-06 11:11:58 +02001635
toth92g357b2972021-05-04 15:41:35 +02001636 } else {
1637 TEST_ASSERT(crt.authority_key_id.keyIdentifier.tag == 0);
1638 TEST_ASSERT(crt.authority_key_id.keyIdentifier.len == 0);
toth92ga41954d2021-02-12 16:11:17 +01001639
toth92g357b2972021-05-04 15:41:35 +02001640 TEST_ASSERT(crt.authority_key_id.authorityCertSerialNumber.tag == 0);
1641 TEST_ASSERT(crt.authority_key_id.authorityCertSerialNumber.len == 0);
toth92ga41954d2021-02-12 16:11:17 +01001642 }
1643
1644exit:
1645 mbedtls_x509_crt_free(&crt);
1646}
1647/* END_CASE */