blob: 5b93b12d6d41db9d1ac85b5456a4d96a8e7a241f [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
Valerio Setti569c1712023-04-19 14:53:36 +0200431 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 mbedtls_x509_crt_init(&crt);
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
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200439 USE_PSA_DONE();
Thomas Daubneybd5466a2022-05-31 14:16:42 +0100440}
441/* END_CASE */
442
Glenn Strauss6f545ac2022-10-25 15:02:14 -0400443/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_TEST_HOOKS */
444void x509_crt_parse_cn_inet_pton(const char *cn, data_t *exp, int ref_ret)
445{
446 uint32_t addr[4];
447 size_t addrlen = mbedtls_x509_crt_parse_cn_inet_pton(cn, addr);
448 TEST_EQUAL(addrlen, (size_t) ref_ret);
449
450 if (addrlen) {
451 ASSERT_COMPARE(exp->x, exp->len, addr, addrlen);
452 }
453}
454/* END_CASE */
455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500457void x509_parse_san(char *crt_file, char *result_str, int parse_result)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200458{
Ron Eldor890819a2019-05-13 19:03:04 +0300459 int ret;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200460 mbedtls_x509_crt crt;
Ron Eldor890819a2019-05-13 19:03:04 +0300461 mbedtls_x509_subject_alternative_name san;
462 mbedtls_x509_sequence *cur = NULL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200463 char buf[2000];
464 char *p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 size_t n = sizeof(buf);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200466
Valerio Setti569c1712023-04-19 14:53:36 +0200467 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 mbedtls_x509_crt_init(&crt);
469 memset(buf, 0, 2000);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200470
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500471 TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), parse_result);
Ron Eldor890819a2019-05-13 19:03:04 +0300472
Andrzej Kurekd90376e2023-01-20 07:08:57 -0500473 if (parse_result != 0) {
474 goto exit;
475 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if (crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Ron Eldor890819a2019-05-13 19:03:04 +0300477 cur = &crt.subject_alt_names;
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 while (cur != NULL) {
479 ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
480 TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
Ron Eldor890819a2019-05-13 19:03:04 +0300481 /*
482 * If san type not supported, ignore.
483 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 if (ret == 0) {
Andrzej Kurekd40c2b62023-02-13 07:01:59 -0500485 ret = verify_parse_san(&san, &p, &n);
486 mbedtls_x509_free_subject_alt_name(&san);
487 TEST_EQUAL(ret, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 }
Ron Eldor890819a2019-05-13 19:03:04 +0300489 cur = cur->next;
490 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200491 }
492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 TEST_ASSERT(strcmp(buf, result_str) == 0);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200494
495exit:
496
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
Valerio Setti569c1712023-04-19 14:53:36 +0200509 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 mbedtls_x509_crt_init(&crt);
511 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
Valerio Setti569c1712023-04-19 14:53:36 +0200534 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 mbedtls_x509_crl_init(&crl);
536 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
Valerio Setti569c1712023-04-19 14:53:36 +0200558 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 mbedtls_x509_crl_init(&crl);
560 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
Valerio Setti569c1712023-04-19 14:53:36 +0200577 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 mbedtls_x509_csr_init(&csr);
579 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);
Valerio Setti569c1712023-04-19 14:53:36 +0200609 USE_PSA_DONE();
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100610}
611/* END_CASE */
612
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200613/* 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 +0100614void x509_verify_restart(char *crt_file, char *ca_file,
615 int result, int flags_result,
616 int max_ops, int min_restart, int max_restart)
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200617{
618 int ret, cnt_restart;
619 mbedtls_x509_crt_restart_ctx rs_ctx;
620 mbedtls_x509_crt crt;
621 mbedtls_x509_crt ca;
622 uint32_t flags = 0;
623
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200624 /*
625 * See comments on ecp_test_vect_restart() for op count precision.
626 *
627 * For reference, with mbed TLS 2.6 and default settings:
628 * - ecdsa_verify() for P-256: ~ 6700
629 * - ecdsa_verify() for P-384: ~ 18800
630 * - x509_verify() for server5 -> test-ca2: ~ 18800
631 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
632 */
Valerio Setti569c1712023-04-19 14:53:36 +0200633 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 mbedtls_x509_crt_restart_init(&rs_ctx);
635 mbedtls_x509_crt_init(&crt);
636 mbedtls_x509_crt_init(&ca);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
639 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 mbedtls_ecp_set_max_ops(max_ops);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200642
643 cnt_restart = 0;
644 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
646 &mbedtls_x509_crt_profile_default, NULL, &flags,
647 NULL, NULL, &rs_ctx);
648 } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 TEST_ASSERT(ret == result);
651 TEST_ASSERT(flags == (uint32_t) flags_result);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 TEST_ASSERT(cnt_restart >= min_restart);
654 TEST_ASSERT(cnt_restart <= max_restart);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200655
656 /* Do we leak memory when aborting? */
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
658 &mbedtls_x509_crt_profile_default, NULL, &flags,
659 NULL, NULL, &rs_ctx);
660 TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200661
662exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 mbedtls_x509_crt_restart_free(&rs_ctx);
664 mbedtls_x509_crt_free(&crt);
665 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100666 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200667}
668/* END_CASE */
669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100671void x509_verify(char *crt_file, char *ca_file, char *crl_file,
672 char *cn_name_str, int result, int flags_result,
673 char *profile_str,
674 char *verify_callback)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000675{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_x509_crt crt;
677 mbedtls_x509_crt ca;
678 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200679 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000680 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200681 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 char *cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200683 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000684
Valerio Setti569c1712023-04-19 14:53:36 +0200685 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 mbedtls_x509_crt_init(&crt);
687 mbedtls_x509_crt_init(&ca);
688 mbedtls_x509_crl_init(&crl);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 if (strcmp(cn_name_str, "NULL") != 0) {
Paul Bakker33b43f12013-08-20 11:48:36 +0200691 cn_name = cn_name_str;
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 if (strcmp(profile_str, "") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200695 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 } else if (strcmp(profile_str, "next") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200697 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 } else if (strcmp(profile_str, "suite_b") == 0) {
Ron Eldorc1539982018-02-06 18:47:17 +0200699 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 } else if (strcmp(profile_str, "compat") == 0) {
Gilles Peskineef86ab22017-05-05 18:59:02 +0200701 profile = &compat_profile;
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 } else if (strcmp(profile_str, "all") == 0) {
Hanno Becker20a4ade2019-06-03 14:27:03 +0100703 profile = &profile_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 } else {
705 TEST_ASSERT("Unknown algorithm profile" == 0);
706 }
Gilles Peskineef86ab22017-05-05 18:59:02 +0200707
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (strcmp(verify_callback, "NULL") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200709 f_vrfy = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 } else if (strcmp(verify_callback, "verify_none") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200711 f_vrfy = verify_none;
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 } else if (strcmp(verify_callback, "verify_all") == 0) {
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200713 f_vrfy = verify_all;
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 } else {
715 TEST_ASSERT("No known verify callback selected" == 0);
716 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
719 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
720 TEST_ASSERT(mbedtls_x509_crl_parse_file(&crl, crl_file) == 0);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 res = mbedtls_x509_crt_verify_with_profile(&crt,
723 &ca,
724 &crl,
725 profile,
726 cn_name,
727 &flags,
728 f_vrfy,
729 NULL);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 TEST_EQUAL(res, result);
732 TEST_EQUAL(flags, (uint32_t) flags_result);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200733
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200734#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker0350d562019-03-28 14:23:36 +0000735 /* CRLs aren't supported with CA callbacks, so skip the CA callback
Jarno Lamsadfd22c42019-04-01 15:18:53 +0300736 * version of the test if CRLs are in use. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 if (crl_file == NULL || strcmp(crl_file, "") == 0) {
Hanno Becker0350d562019-03-28 14:23:36 +0000738 flags = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 res = mbedtls_x509_crt_verify_with_ca_cb(&crt,
741 ca_callback,
742 &ca,
743 profile,
744 cn_name,
745 &flags,
746 f_vrfy,
747 NULL);
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200748
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 TEST_ASSERT(res == (result));
750 TEST_ASSERT(flags == (uint32_t) (flags_result));
Hanno Becker0350d562019-03-28 14:23:36 +0000751 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200752#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200753exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 mbedtls_x509_crt_free(&crt);
755 mbedtls_x509_crt_free(&ca);
756 mbedtls_x509_crl_free(&crl);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100757 MD_OR_USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000758}
Paul Bakker33b43f12013-08-20 11:48:36 +0200759/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000760
Jarno Lamsa557426a2019-03-27 17:08:29 +0200761/* 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 +0100762void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
763 int exp_ret)
Jarno Lamsa557426a2019-03-27 17:08:29 +0200764{
765 int ret;
766 mbedtls_x509_crt crt;
767 mbedtls_x509_crt ca;
768 uint32_t flags = 0;
Hanno Becker3f932bb2019-03-28 17:06:47 +0000769
Valerio Setti569c1712023-04-19 14:53:36 +0200770 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 mbedtls_x509_crt_init(&crt);
772 mbedtls_x509_crt_init(&ca);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
775 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if (strcmp(name, "NULL") == 0) {
Jarno Lamsa557426a2019-03-27 17:08:29 +0200778 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca,
782 &compat_profile, name, &flags,
783 NULL, NULL);
Jarno Lamsa557426a2019-03-27 17:08:29 +0200784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 TEST_ASSERT(ret == exp_ret);
786 TEST_ASSERT(flags == (uint32_t) (-1));
Jarno Lamsa557426a2019-03-27 17:08:29 +0200787exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 mbedtls_x509_crt_free(&crt);
789 mbedtls_x509_crt_free(&ca);
Valerio Setti569c1712023-04-19 14:53:36 +0200790 USE_PSA_DONE();
Jarno Lamsa557426a2019-03-27 17:08:29 +0200791}
792/* END_CASE */
793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100795void x509_verify_callback(char *crt_file, char *ca_file, char *name,
796 int exp_ret, char *exp_vrfy_out)
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200797{
798 int ret;
799 mbedtls_x509_crt crt;
800 mbedtls_x509_crt ca;
801 uint32_t flags = 0;
802 verify_print_context vrfy_ctx;
803
Valerio Setti569c1712023-04-19 14:53:36 +0200804 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 mbedtls_x509_crt_init(&crt);
806 mbedtls_x509_crt_init(&ca);
807 verify_print_init(&vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
810 TEST_ASSERT(mbedtls_x509_crt_parse_file(&ca, ca_file) == 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 if (strcmp(name, "NULL") == 0) {
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200813 name = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 }
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL,
817 &compat_profile,
818 name, &flags,
819 verify_print, &vrfy_ctx);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 TEST_ASSERT(ret == exp_ret);
822 TEST_ASSERT(strcmp(vrfy_ctx.buf, exp_vrfy_out) == 0);
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200823
824exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 mbedtls_x509_crt_free(&crt);
826 mbedtls_x509_crt_free(&ca);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +0100827 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200828}
829/* END_CASE */
830
Hanno Becker612a2f12020-10-09 09:19:39 +0100831/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100832void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
833 char *new_subject_ou,
834 char *result_str,
835 int ret)
Werner Lewis31ecb962022-06-17 15:51:55 +0100836{
837 mbedtls_x509_crt crt;
838 char buf[2000];
839 int res = 0;
840
Valerio Setti569c1712023-04-19 14:53:36 +0200841 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 mbedtls_x509_crt_init(&crt);
843 memset(buf, 0, 2000);
Werner Lewis31ecb962022-06-17 15:51:55 +0100844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100846 crt.subject.next->val.p = (unsigned char *) new_subject_ou;
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 crt.subject.next->val.len = strlen(new_subject_ou);
Werner Lewis31ecb962022-06-17 15:51:55 +0100848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
Werner Lewis31ecb962022-06-17 15:51:55 +0100850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 if (ret != 0) {
852 TEST_ASSERT(res == ret);
853 } else {
854 TEST_ASSERT(res != -1);
855 TEST_ASSERT(res != -2);
856 TEST_ASSERT(strcmp(buf, result_str) == 0);
Werner Lewis31ecb962022-06-17 15:51:55 +0100857 }
858exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200860 USE_PSA_DONE();
Werner Lewis31ecb962022-06-17 15:51:55 +0100861}
862/* END_CASE */
863
864/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100865void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000866{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000868 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200869 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000870
Valerio Setti569c1712023-04-19 14:53:36 +0200871 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 mbedtls_x509_crt_init(&crt);
873 memset(buf, 0, 2000);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
876 if (strcmp(entity, "subject") == 0) {
877 res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
878 } else if (strcmp(entity, "issuer") == 0) {
879 res = mbedtls_x509_dn_gets(buf, 2000, &crt.issuer);
880 } else {
881 TEST_ASSERT("Unknown entity" == 0);
882 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 TEST_ASSERT(res != -1);
885 TEST_ASSERT(res != -2);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 TEST_ASSERT(strcmp(buf, result_str) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200888
889exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +0200891 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +0000892}
Paul Bakker33b43f12013-08-20 11:48:36 +0200893/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000894
David Horstmanndb73d3b2022-10-04 16:49:16 +0100895/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100896void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
David Horstmanndb73d3b2022-10-04 16:49:16 +0100897{
898 unsigned char *name;
899 unsigned char *p;
900 size_t name_len;
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100901 mbedtls_x509_name head;
David Horstmann3cd67582022-10-17 17:59:10 +0100902 int ret;
David Horstmanndb73d3b2022-10-04 16:49:16 +0100903
Valerio Setti569c1712023-04-19 14:53:36 +0200904 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 memset(&head, 0, sizeof(head));
David Horstmann2bb9c8a2022-10-20 10:18:37 +0100906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100908 p = name;
909
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 ret = mbedtls_x509_get_name(&p, (name + name_len), &head);
911 if (ret == 0) {
912 mbedtls_asn1_free_named_data_list_shallow(head.next);
913 }
David Horstmanndb73d3b2022-10-04 16:49:16 +0100914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 TEST_EQUAL(ret, exp_ret);
David Horstmanndb73d3b2022-10-04 16:49:16 +0100916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 mbedtls_free(name);
Valerio Setti569c1712023-04-19 14:53:36 +0200918 USE_PSA_DONE();
David Horstmanndb73d3b2022-10-04 16:49:16 +0100919}
920/* END_CASE */
921
Werner Lewisb3acb052022-06-17 15:59:58 +0100922/* 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 +0100923void mbedtls_x509_dn_get_next(char *name_str,
924 int next_merged,
925 char *expected_oids,
926 int exp_count,
927 char *exp_dn_gets)
Werner Lewisb3acb052022-06-17 15:59:58 +0100928{
929 int ret = 0, i;
Werner Lewisac80a662022-06-23 11:58:02 +0100930 size_t len = 0, out_size;
Werner Lewisb3acb052022-06-17 15:59:58 +0100931 mbedtls_asn1_named_data *names = NULL;
Glenn Straussa4b40412022-06-26 19:32:09 -0400932 mbedtls_x509_name parsed, *parsed_cur;
Werner Lewisac80a662022-06-23 11:58:02 +0100933 // Size of buf is maximum required for test cases
934 unsigned char buf[80], *out = NULL, *c;
Werner Lewisb3acb052022-06-17 15:59:58 +0100935 const char *short_name;
936
Valerio Setti569c1712023-04-19 14:53:36 +0200937 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 memset(&parsed, 0, sizeof(parsed));
939 memset(buf, 0, sizeof(buf));
940 c = buf + sizeof(buf);
Werner Lewisac80a662022-06-23 11:58:02 +0100941 // Additional size required for trailing space
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 out_size = strlen(expected_oids) + 2;
943 ASSERT_ALLOC(out, out_size);
Werner Lewisb3acb052022-06-17 15:59:58 +0100944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 TEST_EQUAL(mbedtls_x509_string_to_names(&names, name_str), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 ret = mbedtls_x509_write_names(&c, buf, names);
948 TEST_LE_S(0, ret);
Werner Lewisb3acb052022-06-17 15:59:58 +0100949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
951 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
952 TEST_EQUAL(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100953
954 // Iterate over names and set next_merged nodes
955 parsed_cur = &parsed;
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 for (; next_merged != 0 && parsed_cur != NULL; next_merged = next_merged >> 1) {
Werner Lewis12657cd2022-06-20 11:47:57 +0100957 parsed_cur->next_merged = next_merged & 0x01;
Werner Lewisb3acb052022-06-17 15:59:58 +0100958 parsed_cur = parsed_cur->next;
959 }
960
961 // Iterate over RDN nodes and print OID of first element to buffer
962 parsed_cur = &parsed;
963 len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 for (i = 0; parsed_cur != NULL; i++) {
965 TEST_EQUAL(mbedtls_oid_get_attr_short_name(&parsed_cur->oid,
966 &short_name), 0);
967 len += mbedtls_snprintf((char *) out + len, out_size - len, "%s ", short_name);
968 parsed_cur = mbedtls_x509_dn_get_next(parsed_cur);
Werner Lewisb3acb052022-06-17 15:59:58 +0100969 }
970 out[len-1] = 0;
971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 TEST_EQUAL(exp_count, i);
973 TEST_EQUAL(strcmp((char *) out, expected_oids), 0);
974 mbedtls_free(out);
Werner Lewisac80a662022-06-23 11:58:02 +0100975 out = NULL;
Werner Lewisb3acb052022-06-17 15:59:58 +0100976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 out_size = strlen(exp_dn_gets) + 1;
978 ASSERT_ALLOC(out, out_size);
Werner Lewisac80a662022-06-23 11:58:02 +0100979
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 TEST_LE_S(0, mbedtls_x509_dn_gets((char *) out, out_size, &parsed));
981 TEST_EQUAL(strcmp((char *) out, exp_dn_gets), 0);
Werner Lewisb3acb052022-06-17 15:59:58 +0100982exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 mbedtls_free(out);
984 mbedtls_asn1_free_named_data_list(&names);
985 mbedtls_asn1_free_named_data_list_shallow(parsed.next);
Valerio Setti569c1712023-04-19 14:53:36 +0200986 USE_PSA_DONE();
Werner Lewisb3acb052022-06-17 15:59:58 +0100987}
Werner Lewisb3acb052022-06-17 15:59:58 +0100988/* END_CASE */
989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100991void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
Paul Bakker37940d9f2009-07-10 22:38:58 +0000992{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000994
Valerio Setti569c1712023-04-19 14:53:36 +0200995 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 mbedtls_x509_crt_init(&crt);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 if (strcmp(entity, "valid_from") == 0) {
1001 TEST_ASSERT(mbedtls_x509_time_is_past(&crt.valid_from) == result);
1002 } else if (strcmp(entity, "valid_to") == 0) {
1003 TEST_ASSERT(mbedtls_x509_time_is_past(&crt.valid_to) == result);
1004 } else {
1005 TEST_ASSERT("Unknown entity" == 0);
1006 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001007
Paul Bakkerbd51b262014-07-10 15:26:12 +02001008exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001010 USE_PSA_DONE();
Paul Bakker37940d9f2009-07-10 22:38:58 +00001011}
Paul Bakker33b43f12013-08-20 11:48:36 +02001012/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +00001013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001015void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001016{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001018
Valerio Setti569c1712023-04-19 14:53:36 +02001019 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001023
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 if (strcmp(entity, "valid_from") == 0) {
1025 TEST_ASSERT(mbedtls_x509_time_is_future(&crt.valid_from) == result);
1026 } else if (strcmp(entity, "valid_to") == 0) {
1027 TEST_ASSERT(mbedtls_x509_time_is_future(&crt.valid_to) == result);
1028 } else {
1029 TEST_ASSERT("Unknown entity" == 0);
1030 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001031
Paul Bakkerbd51b262014-07-10 15:26:12 +02001032exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001034 USE_PSA_DONE();
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001035}
1036/* END_CASE */
1037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001039void x509parse_crt_file(char *crt_file, int result)
Paul Bakker5a5fa922014-09-26 14:53:04 +02001040{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +02001042
Valerio Setti569c1712023-04-19 14:53:36 +02001043 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 mbedtls_x509_crt_init(&crt);
Paul Bakker5a5fa922014-09-26 14:53:04 +02001045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == result);
Paul Bakker5a5fa922014-09-26 14:53:04 +02001047
1048exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001050 USE_PSA_DONE();
Paul Bakker5a5fa922014-09-26 14:53:04 +02001051}
1052/* END_CASE */
1053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001055void x509parse_crt(data_t *buf, char *result_str, int result)
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_x509_crt crt;
Hanno Becker612a2f12020-10-09 09:19:39 +01001058#if !defined(MBEDTLS_X509_REMOVE_INFO)
Hanno Beckerfff2d572020-10-09 09:45:19 +01001059 unsigned char output[2000] = { 0 };
Azim Khanf1aaec92017-05-30 14:23:15 +01001060 int res;
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001061#else
1062 ((void) result_str);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001063#endif
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001064
Valerio Setti569c1712023-04-19 14:53:36 +02001065 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 mbedtls_x509_crt_init(&crt);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001067
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 TEST_ASSERT(mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len) == (result));
Hanno Becker612a2f12020-10-09 09:19:39 +01001069#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 if ((result) == 0) {
1071 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1072 TEST_ASSERT(res != -1);
1073 TEST_ASSERT(res != -2);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001076 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001078#endif
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 mbedtls_x509_crt_free(&crt);
1081 mbedtls_x509_crt_init(&crt);
Hanno Becker2d8a2c02019-01-31 09:15:53 +00001082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 TEST_ASSERT(mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len) == (result));
Hanno Becker612a2f12020-10-09 09:19:39 +01001084#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 if ((result) == 0) {
1086 memset(output, 0, 2000);
Peter Kolbus9a969b62018-12-11 13:55:56 -06001087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Paul Bakker33b43f12013-08-20 11:48:36 +02001089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 TEST_ASSERT(res != -1);
1091 TEST_ASSERT(res != -2);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001092
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001094 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001096#endif /* !MBEDTLS_X509_REMOVE_INFO */
Paul Bakkerb08e6842012-02-11 18:43:20 +00001097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 mbedtls_x509_crt_free(&crt);
1099 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001100
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL,
1102 NULL) == (result));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001103#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 if ((result) == 0) {
1105 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 TEST_ASSERT(res != -1);
1108 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001111 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 memset(output, 0, 2000);
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001113#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 mbedtls_x509_crt_free(&crt);
1116 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL,
1119 NULL) == (result));
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001120#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 if ((result) == 0) {
1122 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001123
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 TEST_ASSERT(res != -1);
1125 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001128 }
Hanno Beckerb4e5ddc2020-10-09 09:36:01 +01001129#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001130
1131exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001133 USE_PSA_DONE();
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001134}
1135/* END_CASE */
1136
1137/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001138void x509parse_crt_cb(data_t *buf, char *result_str, int result)
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001139{
1140 mbedtls_x509_crt crt;
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001141 mbedtls_x509_buf oid;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001142
1143#if !defined(MBEDTLS_X509_REMOVE_INFO)
1144 unsigned char output[2000] = { 0 };
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001145 int res;
Hanno Beckerfff2d572020-10-09 09:45:19 +01001146#else
1147 ((void) result_str);
1148#endif
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001149
Valerio Setti569c1712023-04-19 14:53:36 +02001150 USE_PSA_INIT();
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001151 oid.tag = MBEDTLS_ASN1_OID;
1152 oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
Nicola Di Lietoe58b4632020-05-29 22:58:25 +02001154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001156
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
1158 &oid) == (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001159#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 if ((result) == 0) {
1161 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 TEST_ASSERT(res != -1);
1164 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001167 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 memset(output, 0, 2000);
Hanno Beckerfff2d572020-10-09 09:45:19 +01001169#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 mbedtls_x509_crt_free(&crt);
1172 mbedtls_x509_crt_init(&crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 TEST_ASSERT(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1175 &oid) == (result));
Hanno Beckerfff2d572020-10-09 09:45:19 +01001176#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 if ((result) == 0) {
1178 res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 TEST_ASSERT(res != -1);
1181 TEST_ASSERT(res != -2);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001184 }
Hanno Beckerfff2d572020-10-09 09:45:19 +01001185#endif /* !MBEDTLS_X509_REMOVE_INFO */
Nicola Di Lieto17bb60c2020-05-28 23:04:15 +02001186
Paul Bakkerbd51b262014-07-10 15:26:12 +02001187exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001189 USE_PSA_DONE();
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001190}
Paul Bakker33b43f12013-08-20 11:48:36 +02001191/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +00001192
Hanno Becker612a2f12020-10-09 09:19:39 +01001193/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001194void x509parse_crl(data_t *buf, char *result_str, int result)
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001195{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001197 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +01001198 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001199
Valerio Setti569c1712023-04-19 14:53:36 +02001200 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 mbedtls_x509_crl_init(&crl);
1202 memset(output, 0, 2000);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001203
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 TEST_ASSERT(mbedtls_x509_crl_parse(&crl, buf->x, buf->len) == (result));
1206 if ((result) == 0) {
1207 res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
Paul Bakker33b43f12013-08-20 11:48:36 +02001208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 TEST_ASSERT(res != -1);
1210 TEST_ASSERT(res != -2);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 TEST_ASSERT(strcmp((char *) output, result_str) == 0);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001213 }
Paul Bakkerb08e6842012-02-11 18:43:20 +00001214
Paul Bakkerbd51b262014-07-10 15:26:12 +02001215exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 mbedtls_x509_crl_free(&crl);
Valerio Setti569c1712023-04-19 14:53:36 +02001217 USE_PSA_DONE();
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001218}
Paul Bakker33b43f12013-08-20 11:48:36 +02001219/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +00001220
Hanno Becker612a2f12020-10-09 09:19:39 +01001221/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001222void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001223{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001225 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001226 int my_ret;
1227
Valerio Setti569c1712023-04-19 14:53:36 +02001228 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 mbedtls_x509_csr_init(&csr);
1230 memset(my_out, 0, sizeof(my_out));
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
1233 TEST_ASSERT(my_ret == ref_ret);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 if (ref_ret == 0) {
1236 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1237 TEST_ASSERT(my_out_len == strlen(ref_out));
1238 TEST_ASSERT(strcmp(my_out, ref_out) == 0);
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001239 }
1240
Paul Bakkerbd51b262014-07-10 15:26:12 +02001241exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001243 USE_PSA_DONE();
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +02001244}
1245/* END_CASE */
1246
Przemek Stekield7992df2023-01-25 16:19:50 +01001247/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1248void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
1249{
1250 mbedtls_x509_csr csr;
1251 char my_out[1000];
1252 int my_ret;
1253
Valerio Setti569c1712023-04-19 14:53:36 +02001254 USE_PSA_INIT();
Przemek Stekield7992df2023-01-25 16:19:50 +01001255 mbedtls_x509_csr_init(&csr);
1256 memset(my_out, 0, sizeof(my_out));
1257
1258 my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file);
1259 TEST_ASSERT(my_ret == ref_ret);
1260
1261 if (ref_ret == 0) {
1262 size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1263 TEST_ASSERT(my_out_len == strlen(ref_out));
1264 TEST_ASSERT(strcmp(my_out, ref_out) == 0);
1265 }
1266
1267exit:
1268 mbedtls_x509_csr_free(&csr);
Valerio Setti569c1712023-04-19 14:53:36 +02001269 USE_PSA_DONE();
Przemek Stekield7992df2023-01-25 16:19:50 +01001270}
1271/* END_CASE */
1272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001274void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001275{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001277 int i;
1278
Valerio Setti569c1712023-04-19 14:53:36 +02001279 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 mbedtls_x509_crt_init(&chain);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001281
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 TEST_ASSERT(mbedtls_x509_crt_parse_path(&chain, crt_path) == ret);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001283
1284 /* Check how many certs we got */
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1286 if (cur->raw.p != NULL) {
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001287 i++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 }
1289 }
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 TEST_ASSERT(i == nb_crt);
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001292
Paul Bakkerbd51b262014-07-10 15:26:12 +02001293exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 mbedtls_x509_crt_free(&chain);
Valerio Setti569c1712023-04-19 14:53:36 +02001295 USE_PSA_DONE();
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +01001296}
1297/* END_CASE */
1298
Janos Follath822b2c32015-10-11 10:25:22 +02001299/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001300void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1301 int ret_chk, int flags_chk)
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001302{
1303 char file_buf[128];
1304 int ret;
1305 uint32_t flags;
1306 mbedtls_x509_crt trusted, chain;
1307
1308 /*
1309 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1310 * with NN.crt signed by NN-1.crt
1311 */
Valerio Setti569c1712023-04-19 14:53:36 +02001312 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 mbedtls_x509_crt_init(&trusted);
1314 mbedtls_x509_crt_init(&chain);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001315
1316 /* Load trusted root */
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, ca_file) == 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001318
1319 /* Load a chain with nb_int intermediates (from 01 to nb_int),
1320 * plus one "end-entity" cert (nb_int + 1) */
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001321 ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 nb_int + 1);
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001323 TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, file_buf) == 0);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001325
1326 /* Try to verify that chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1328 NULL, NULL);
1329 TEST_ASSERT(ret == ret_chk);
1330 TEST_ASSERT(flags == (uint32_t) flags_chk);
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001331
1332exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 mbedtls_x509_crt_free(&chain);
1334 mbedtls_x509_crt_free(&trusted);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001335 MD_OR_USE_PSA_DONE();
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +02001336}
1337/* END_CASE */
1338
1339/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001340void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1341 int flags_result, int result,
1342 char *profile_name, int vrfy_fatal_lvls)
Janos Follath822b2c32015-10-11 10:25:22 +02001343{
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 char *act;
Janos Follath822b2c32015-10-11 10:25:22 +02001345 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001346 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001347 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001348 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +02001349
Valerio Setti569c1712023-04-19 14:53:36 +02001350 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 mbedtls_x509_crt_init(&chain);
1352 mbedtls_x509_crt_init(&trusted);
Janos Follath822b2c32015-10-11 10:25:22 +02001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 while ((act = mystrsep(&chain_paths, " ")) != NULL) {
1355 TEST_ASSERT(mbedtls_x509_crt_parse_file(&chain, act) == 0);
1356 }
1357 TEST_ASSERT(mbedtls_x509_crt_parse_file(&trusted, trusted_ca) == 0);
Janos Follath822b2c32015-10-11 10:25:22 +02001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 if (strcmp(profile_name, "") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001360 profile = &mbedtls_x509_crt_profile_default;
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 } else if (strcmp(profile_name, "next") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001362 profile = &mbedtls_x509_crt_profile_next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 } else if (strcmp(profile_name, "suiteb") == 0) {
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001364 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 } else if (strcmp(profile_name, "rsa3072") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001366 profile = &profile_rsa3072;
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 } else if (strcmp(profile_name, "sha512") == 0) {
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +02001368 profile = &profile_sha512;
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 }
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +02001370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1372 NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
Janos Follathef4f2582015-10-11 16:17:27 +02001373
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 TEST_ASSERT(res == (result));
1375 TEST_ASSERT(flags == (uint32_t) (flags_result));
Janos Follath822b2c32015-10-11 10:25:22 +02001376
1377exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 mbedtls_x509_crt_free(&trusted);
1379 mbedtls_x509_crt_free(&chain);
Manuel Pégourié-Gonnard33a13022023-03-17 14:02:49 +01001380 MD_OR_USE_PSA_DONE();
Janos Follath822b2c32015-10-11 10:25:22 +02001381}
1382/* END_CASE */
1383
Hanno Becker612a2f12020-10-09 09:19:39 +01001384/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001385void x509_oid_desc(data_t *buf, char *ref_desc)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001386{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001388 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +00001389 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001390
Valerio Setti569c1712023-04-19 14:53:36 +02001391 USE_PSA_INIT();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001393 oid.p = buf->x;
1394 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 ret = mbedtls_oid_get_extended_key_usage(&oid, &desc);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001397
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 if (strcmp(ref_desc, "notfound") == 0) {
1399 TEST_ASSERT(ret != 0);
1400 TEST_ASSERT(desc == NULL);
1401 } else {
1402 TEST_ASSERT(ret == 0);
1403 TEST_ASSERT(desc != NULL);
1404 TEST_ASSERT(strcmp(desc, ref_desc) == 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001405 }
Valerio Setti569c1712023-04-19 14:53:36 +02001406 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001407}
1408/* END_CASE */
1409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001411void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001412{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001414 char num_buf[100];
1415
Valerio Setti569c1712023-04-19 14:53:36 +02001416 USE_PSA_INIT();
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001417 memset(num_buf, 0x2a, sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +01001420 oid.p = oid_buf->x;
1421 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001422
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001423 TEST_ASSERT((size_t) blen <= sizeof(num_buf));
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 TEST_ASSERT(mbedtls_oid_get_numeric_string(num_buf, blen, &oid) == ret);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (ret >= 0) {
1428 TEST_ASSERT(num_buf[ret] == 0);
1429 TEST_ASSERT(strcmp(num_buf, numstr) == 0);
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001430 }
Valerio Setti569c1712023-04-19 14:53:36 +02001431 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +01001432}
1433/* END_CASE */
1434
TRodziewicz442fdc22021-06-07 13:52:23 +02001435/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001436void x509_check_key_usage(char *crt_file, int usage, int ret)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001437{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001439
Valerio Setti569c1712023-04-19 14:53:36 +02001440 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001444
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 TEST_ASSERT(mbedtls_x509_crt_check_key_usage(&crt, usage) == ret);
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001446
Paul Bakkerbd51b262014-07-10 15:26:12 +02001447exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001449 USE_PSA_DONE();
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001450}
1451/* END_CASE */
1452
TRodziewicz442fdc22021-06-07 13:52:23 +02001453/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001454void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1455 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001456{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001458
Valerio Setti569c1712023-04-19 14:53:36 +02001459 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 mbedtls_x509_crt_init(&crt);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 TEST_ASSERT(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x,
1465 oid->len) == ret);
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001466
Paul Bakkerbd51b262014-07-10 15:26:12 +02001467exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 mbedtls_x509_crt_free(&crt);
Valerio Setti569c1712023-04-19 14:53:36 +02001469 USE_PSA_DONE();
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001470}
1471/* END_CASE */
1472
Andres AG4b76aec2016-09-23 13:16:02 +01001473/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001474void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1475 int day, int hour, int min, int sec)
Andres AG4b76aec2016-09-23 13:16:02 +01001476{
1477 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001478 unsigned char buf[21];
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 unsigned char *start = buf;
1480 unsigned char *end = buf;
Andres AG4b76aec2016-09-23 13:16:02 +01001481
Valerio Setti569c1712023-04-19 14:53:36 +02001482 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 memset(&time, 0x00, sizeof(time));
1484 *end = (unsigned char) tag; end++;
1485 *end = strlen(time_str);
1486 TEST_ASSERT(*end < 20);
Andres AG4b76aec2016-09-23 13:16:02 +01001487 end++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 memcpy(end, time_str, (size_t) *(end - 1));
Andres AG4b76aec2016-09-23 13:16:02 +01001489 end += *(end - 1);
1490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 TEST_ASSERT(mbedtls_x509_get_time(&start, end, &time) == ret);
1492 if (ret == 0) {
1493 TEST_ASSERT(year == time.year);
1494 TEST_ASSERT(mon == time.mon);
1495 TEST_ASSERT(day == time.day);
1496 TEST_ASSERT(hour == time.hour);
1497 TEST_ASSERT(min == time.min);
1498 TEST_ASSERT(sec == time.sec);
Andres AG4b76aec2016-09-23 13:16:02 +01001499 }
Valerio Setti569c1712023-04-19 14:53:36 +02001500 USE_PSA_DONE();
Andres AG4b76aec2016-09-23 13:16:02 +01001501}
1502/* END_CASE */
1503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gilles Peskine449bd832023-01-11 14:50:10 +01001505void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1506 int ref_msg_md, int ref_mgf_md,
1507 int ref_salt_len, int ref_ret)
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001508{
1509 int my_ret;
Ronald Cronac6ae352020-06-26 14:33:03 +02001510 mbedtls_x509_buf buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001512 int my_salt_len;
1513
Valerio Setti569c1712023-04-19 14:53:36 +02001514 USE_PSA_INIT();
1515
Ronald Cronac6ae352020-06-26 14:33:03 +02001516 buf.p = params->x;
1517 buf.len = params->len;
1518 buf.tag = params_tag;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001519
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1521 &my_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 TEST_ASSERT(my_ret == ref_ret);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 if (ref_ret == 0) {
1526 TEST_ASSERT(my_msg_md == (mbedtls_md_type_t) ref_msg_md);
1527 TEST_ASSERT(my_mgf_md == (mbedtls_md_type_t) ref_mgf_md);
1528 TEST_ASSERT(my_salt_len == ref_salt_len);
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001529 }
1530
Paul Bakkerbd51b262014-07-10 15:26:12 +02001531exit:
Valerio Setti569c1712023-04-19 14:53:36 +02001532 USE_PSA_DONE();
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001533}
1534/* END_CASE */