blob: 8914bd0d10cf57e027d7ad142f7b35fb0d4c300a [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"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +010010#include "string.h"
Paul Bakkerb63b0af2011-01-13 17:54:59 +000011
Simon Butcher9e24b512017-07-28 12:15:13 +010012#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
Hanno Becker3b1422e2017-07-26 13:38:02 +010013#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
14than the current threshold 19. To test larger values, please \
15adapt the script tests/data_files/dir-max/long.sh."
16#endif
17
Gilles Peskineef86ab22017-05-05 18:59:02 +020018/* Profile for backward compatibility. Allows SHA-1, unlike the default
19 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020020const mbedtls_x509_crt_profile compat_profile =
21{
22 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
23 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) |
24 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
25 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
26 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
27 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
28 0xFFFFFFF, /* Any PK alg */
29 0xFFFFFFF, /* Any curve */
30 1024,
31};
32
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020033const mbedtls_x509_crt_profile profile_rsa3072 =
34{
35 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
36 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
37 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
38 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA ),
39 0,
40 3072,
41};
42
43const mbedtls_x509_crt_profile profile_sha512 =
44{
45 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
46 0xFFFFFFF, /* Any PK alg */
47 0xFFFFFFF, /* Any curve */
48 1024,
49};
50
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020051int verify_none( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000052{
Paul Bakker5a624082011-01-18 16:31:52 +000053 ((void) data);
54 ((void) crt);
55 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010056 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020057
Paul Bakker915275b2012-09-28 07:10:55 +000058 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000059}
60
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020061int verify_all( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000062{
Paul Bakker5a624082011-01-18 16:31:52 +000063 ((void) data);
64 ((void) crt);
65 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000066 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000067
Paul Bakkerb63b0af2011-01-13 17:54:59 +000068 return 0;
69}
70
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +020071int verify_fatal( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
72{
73 int *levels = (int *) data;
74
75 ((void) crt);
76 ((void) certificate_depth);
77
78 /* Simulate a fatal error in the callback */
79 if( *levels & ( 1 << certificate_depth ) )
80 {
81 *flags |= ( 1 << certificate_depth );
Manuel Pégourié-Gonnard41859782017-05-23 12:58:53 +020082 return( -1 - certificate_depth );
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +020083 }
84
85 return( 0 );
86}
87
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +090088/* strsep() not available on Windows */
89char *mystrsep(char **stringp, const char *delim)
90{
91 const char *p;
92 char *ret = *stringp;
93
94 if( *stringp == NULL )
95 return( NULL );
96
97 for( ; ; (*stringp)++ )
98 {
99 if( **stringp == '\0' )
100 {
101 *stringp = NULL;
102 goto done;
103 }
104
105 for( p = delim; *p != '\0'; p++ )
106 if( **stringp == *p )
107 {
108 **stringp = '\0';
109 (*stringp)++;
110 goto done;
111 }
112 }
113
114done:
115 return( ret );
116}
117
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200118#if defined(MBEDTLS_X509_CRT_PARSE_C)
119typedef struct {
120 char buf[512];
121 char *p;
122} verify_print_context;
123
124void verify_print_init( verify_print_context *ctx )
125{
126 memset( ctx, 0, sizeof( verify_print_context ) );
127 ctx->p = ctx->buf;
128}
129
130int verify_print( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
131{
132 int ret;
133 verify_print_context *ctx = (verify_print_context *) data;
134 char *p = ctx->p;
135 size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p;
136 ((void) flags);
137
138 ret = mbedtls_snprintf( p, n, "depth %d - serial ", certificate_depth );
139 MBEDTLS_X509_SAFE_SNPRINTF;
140
141 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
142 MBEDTLS_X509_SAFE_SNPRINTF;
143
144 ret = mbedtls_snprintf( p, n, " - subject " );
145 MBEDTLS_X509_SAFE_SNPRINTF;
146
147 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
148 MBEDTLS_X509_SAFE_SNPRINTF;
149
Manuel Pégourié-Gonnardbe2f0b52017-08-21 11:00:22 +0200150 ret = mbedtls_snprintf( p, n, " - flags 0x%08x\n", *flags );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200151 MBEDTLS_X509_SAFE_SNPRINTF;
152
153 ctx->p = p;
154
155 return( 0 );
156}
157#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200158/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000159
Paul Bakker33b43f12013-08-20 11:48:36 +0200160/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200162 * END_DEPENDENCIES
163 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100166void x509_cert_info( char * crt_file, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000167{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000169 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000170 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000173 memset( buf, 0, 2000 );
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
176 res = mbedtls_x509_crt_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000177
178 TEST_ASSERT( res != -1 );
179 TEST_ASSERT( res != -2 );
180
Paul Bakker33b43f12013-08-20 11:48:36 +0200181 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200182
183exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000185}
Paul Bakker33b43f12013-08-20 11:48:36 +0200186/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100189void mbedtls_x509_crl_info( char * crl_file, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000190{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000192 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000193 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000196 memset( buf, 0, 2000 );
197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
199 res = mbedtls_x509_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000200
201 TEST_ASSERT( res != -1 );
202 TEST_ASSERT( res != -2 );
203
Paul Bakker33b43f12013-08-20 11:48:36 +0200204 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200205
206exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000208}
Paul Bakker33b43f12013-08-20 11:48:36 +0200209/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000210
Andres AGa39db392016-12-08 17:10:38 +0000211/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100212void mbedtls_x509_crl_parse( char * crl_file, int result )
Andres AGa39db392016-12-08 17:10:38 +0000213{
214 mbedtls_x509_crl crl;
215 char buf[2000];
216
217 mbedtls_x509_crl_init( &crl );
218 memset( buf, 0, 2000 );
219
220 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == result );
221
222exit:
223 mbedtls_x509_crl_free( &crl );
224}
225/* END_CASE */
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100228void mbedtls_x509_csr_info( char * csr_file, char * result_str )
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100229{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100231 char buf[2000];
232 int res;
233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100235 memset( buf, 0, 2000 );
236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 TEST_ASSERT( mbedtls_x509_csr_parse_file( &csr, csr_file ) == 0 );
238 res = mbedtls_x509_csr_info( buf, 2000, "", &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100239
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100240 TEST_ASSERT( res != -1 );
241 TEST_ASSERT( res != -2 );
242
243 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200244
245exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100247}
248/* END_CASE */
249
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100250/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100251void x509_verify_info( int flags, char * prefix, char * result_str )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100252{
253 char buf[2000];
254 int res;
255
256 memset( buf, 0, sizeof( buf ) );
257
258 res = mbedtls_x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
259
260 TEST_ASSERT( res >= 0 );
261
262 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
263}
264/* END_CASE */
265
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200266/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_C */
267void x509_verify_restart( char *crt_file, char *ca_file,
268 int result, int flags_result,
269 int max_ops, int min_restart, int max_restart )
270{
271 int ret, cnt_restart;
272 mbedtls_x509_crt_restart_ctx rs_ctx;
273 mbedtls_x509_crt crt;
274 mbedtls_x509_crt ca;
275 uint32_t flags = 0;
276
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200277 /*
278 * See comments on ecp_test_vect_restart() for op count precision.
279 *
280 * For reference, with mbed TLS 2.6 and default settings:
281 * - ecdsa_verify() for P-256: ~ 6700
282 * - ecdsa_verify() for P-384: ~ 18800
283 * - x509_verify() for server5 -> test-ca2: ~ 18800
284 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
285 */
286
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200287 mbedtls_x509_crt_restart_init( &rs_ctx );
288 mbedtls_x509_crt_init( &crt );
289 mbedtls_x509_crt_init( &ca );
290
291 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
292 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
293
294 mbedtls_ecp_set_max_ops( max_ops );
295
296 cnt_restart = 0;
297 do {
298 ret = mbedtls_x509_crt_verify_restartable( &crt, &ca, NULL,
299 &mbedtls_x509_crt_profile_default, NULL, &flags,
300 NULL, NULL, &rs_ctx );
301 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
302
303 TEST_ASSERT( ret == result );
304 TEST_ASSERT( flags == (uint32_t) flags_result );
305
306 TEST_ASSERT( cnt_restart >= min_restart );
307 TEST_ASSERT( cnt_restart <= max_restart );
308
309 /* Do we leak memory when aborting? */
310 ret = mbedtls_x509_crt_verify_restartable( &crt, &ca, NULL,
311 &mbedtls_x509_crt_profile_default, NULL, &flags,
312 NULL, NULL, &rs_ctx );
313 TEST_ASSERT( ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
314
315exit:
316 mbedtls_x509_crt_restart_free( &rs_ctx );
317 mbedtls_x509_crt_free( &crt );
318 mbedtls_x509_crt_free( &ca );
319}
320/* END_CASE */
321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200323void x509_verify( char *crt_file, char *ca_file, char *crl_file,
324 char *cn_name_str, int result, int flags_result,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200325 char *profile_str,
Paul Bakker33b43f12013-08-20 11:48:36 +0200326 char *verify_callback )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000327{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_x509_crt crt;
329 mbedtls_x509_crt ca;
330 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200331 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000332 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200333 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200334 char * cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200335 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000336
Jaeden Amero3ea26872019-02-13 11:30:22 +0000337#if defined(MBEDTLS_USE_PSA_CRYPTO)
338 TEST_ASSERT( psa_crypto_init() == 0 );
339#endif
340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 mbedtls_x509_crt_init( &crt );
342 mbedtls_x509_crt_init( &ca );
343 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000344
Paul Bakker33b43f12013-08-20 11:48:36 +0200345 if( strcmp( cn_name_str, "NULL" ) != 0 )
346 cn_name = cn_name_str;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200347
Manuel Pégourié-Gonnarda54f6cc2017-08-09 10:41:42 +0200348 if( strcmp( profile_str, "" ) == 0 )
Gilles Peskineef86ab22017-05-05 18:59:02 +0200349 profile = &mbedtls_x509_crt_profile_default;
Ron Eldorc1539982018-02-06 18:47:17 +0200350 else if( strcmp( profile_str, "next" ) == 0 )
351 profile = &mbedtls_x509_crt_profile_next;
352 else if( strcmp( profile_str, "suite_b" ) == 0 )
353 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200354 else if( strcmp( profile_str, "compat" ) == 0 )
355 profile = &compat_profile;
356 else
357 TEST_ASSERT( "Unknown algorithm profile" == 0 );
358
Paul Bakker33b43f12013-08-20 11:48:36 +0200359 if( strcmp( verify_callback, "NULL" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200360 f_vrfy = NULL;
Paul Bakker33b43f12013-08-20 11:48:36 +0200361 else if( strcmp( verify_callback, "verify_none" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200362 f_vrfy = verify_none;
Paul Bakker33b43f12013-08-20 11:48:36 +0200363 else if( strcmp( verify_callback, "verify_all" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200364 f_vrfy = verify_all;
365 else
366 TEST_ASSERT( "No known verify callback selected" == 0 );
367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
369 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
370 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000371
Gilles Peskineef86ab22017-05-05 18:59:02 +0200372 res = mbedtls_x509_crt_verify_with_profile( &crt, &ca, &crl, profile, cn_name, &flags, f_vrfy, NULL );
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200373
Paul Bakkerbd51b262014-07-10 15:26:12 +0200374 TEST_ASSERT( res == ( result ) );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200375 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200376
377exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_x509_crt_free( &crt );
379 mbedtls_x509_crt_free( &ca );
380 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000381}
Paul Bakker33b43f12013-08-20 11:48:36 +0200382/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200385void x509_verify_callback( char *crt_file, char *ca_file, char *name,
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200386 int exp_ret, char *exp_vrfy_out )
387{
388 int ret;
389 mbedtls_x509_crt crt;
390 mbedtls_x509_crt ca;
391 uint32_t flags = 0;
392 verify_print_context vrfy_ctx;
393
Jaeden Amero3ea26872019-02-13 11:30:22 +0000394#if defined(MBEDTLS_USE_PSA_CRYPTO)
395 TEST_ASSERT( psa_crypto_init() == 0 );
396#endif
397
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200398 mbedtls_x509_crt_init( &crt );
399 mbedtls_x509_crt_init( &ca );
400 verify_print_init( &vrfy_ctx );
401
402 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
403 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
404
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200405 if( strcmp( name, "NULL" ) == 0 )
406 name = NULL;
407
Gilles Peskineef86ab22017-05-05 18:59:02 +0200408 ret = mbedtls_x509_crt_verify_with_profile( &crt, &ca, NULL,
409 &compat_profile,
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200410 name, &flags,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200411 verify_print, &vrfy_ctx );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200412
413 TEST_ASSERT( ret == exp_ret );
414 TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
415
416exit:
417 mbedtls_x509_crt_free( &crt );
418 mbedtls_x509_crt_free( &ca );
419}
420/* END_CASE */
421
422/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100423void mbedtls_x509_dn_gets( char * crt_file, char * entity, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000424{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000426 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200427 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000430 memset( buf, 0, 2000 );
431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200433 if( strcmp( entity, "subject" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 res = mbedtls_x509_dn_gets( buf, 2000, &crt.subject );
Paul Bakker33b43f12013-08-20 11:48:36 +0200435 else if( strcmp( entity, "issuer" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 res = mbedtls_x509_dn_gets( buf, 2000, &crt.issuer );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200437 else
438 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000439
440 TEST_ASSERT( res != -1 );
441 TEST_ASSERT( res != -2 );
442
Paul Bakker33b43f12013-08-20 11:48:36 +0200443 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200444
445exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000447}
Paul Bakker33b43f12013-08-20 11:48:36 +0200448/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100451void mbedtls_x509_time_is_past( char * crt_file, char * entity, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000452{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200458
Paul Bakker33b43f12013-08-20 11:48:36 +0200459 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100460 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_from ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200461 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100462 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_to ) == result );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200463 else
464 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000465
Paul Bakkerbd51b262014-07-10 15:26:12 +0200466exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000468}
Paul Bakker33b43f12013-08-20 11:48:36 +0200469/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100472void mbedtls_x509_time_is_future( char * crt_file, char * entity, int result )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100473{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100479
480 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100481 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_from ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100482 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100483 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_to ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100484 else
485 TEST_ASSERT( "Unknown entity" == 0 );
486
Paul Bakkerbd51b262014-07-10 15:26:12 +0200487exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100489}
490/* END_CASE */
491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100493void x509parse_crt_file( char * crt_file, int result )
Paul Bakker5a5fa922014-09-26 14:53:04 +0200494{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +0200496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_x509_crt_init( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200500
501exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_x509_crt_free( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200503}
504/* END_CASE */
505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100507void x509parse_crt( data_t * buf, char * result_str, int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000508{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 mbedtls_x509_crt crt;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000510 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100511 int res;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_x509_crt_init( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000514 memset( output, 0, 2000 );
515
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000516
Azim Khand30ca132017-06-09 04:32:58 +0100517 TEST_ASSERT( mbedtls_x509_crt_parse( &crt, buf->x, buf->len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200518 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200521
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000522 TEST_ASSERT( res != -1 );
523 TEST_ASSERT( res != -2 );
524
Paul Bakker33b43f12013-08-20 11:48:36 +0200525 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000526 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000527
Paul Bakkerbd51b262014-07-10 15:26:12 +0200528exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000530}
Paul Bakker33b43f12013-08-20 11:48:36 +0200531/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100534void x509parse_crl( data_t * buf, char * result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000535{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000537 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100538 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000541 memset( output, 0, 2000 );
542
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000543
Azim Khand30ca132017-06-09 04:32:58 +0100544 TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf->x, buf->len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200545 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200548
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000549 TEST_ASSERT( res != -1 );
550 TEST_ASSERT( res != -2 );
551
Paul Bakker33b43f12013-08-20 11:48:36 +0200552 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000553 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000554
Paul Bakkerbd51b262014-07-10 15:26:12 +0200555exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000557}
Paul Bakker33b43f12013-08-20 11:48:36 +0200558/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100561void mbedtls_x509_csr_parse( data_t * csr_der, char * ref_out, int ref_ret )
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200562{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200564 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200565 int my_ret;
566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200568 memset( my_out, 0, sizeof( my_out ) );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200569
Azim Khand30ca132017-06-09 04:32:58 +0100570 my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der->x, csr_der->len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200571 TEST_ASSERT( my_ret == ref_ret );
572
573 if( ref_ret == 0 )
574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200576 TEST_ASSERT( my_out_len == strlen( ref_out ) );
577 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
578 }
579
Paul Bakkerbd51b262014-07-10 15:26:12 +0200580exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200582}
583/* END_CASE */
584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100586void mbedtls_x509_crt_parse_path( char * crt_path, int ret, int nb_crt )
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100587{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100589 int i;
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 mbedtls_x509_crt_init( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100594
595 /* Check how many certs we got */
596 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
597 if( cur->raw.p != NULL )
598 i++;
599
600 TEST_ASSERT( i == nb_crt );
601
Paul Bakkerbd51b262014-07-10 15:26:12 +0200602exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 mbedtls_x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100604}
605/* END_CASE */
606
Janos Follath822b2c32015-10-11 10:25:22 +0200607/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +0200608void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int,
609 int ret_chk, int flags_chk )
610{
611 char file_buf[128];
612 int ret;
613 uint32_t flags;
614 mbedtls_x509_crt trusted, chain;
615
Jaeden Amero3ea26872019-02-13 11:30:22 +0000616#if defined(MBEDTLS_USE_PSA_CRYPTO)
617 TEST_ASSERT( psa_crypto_init() == 0 );
618#endif
619
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +0200620 /*
621 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
622 * with NN.crt signed by NN-1.crt
623 */
624
625 mbedtls_x509_crt_init( &trusted );
626 mbedtls_x509_crt_init( &chain );
627
628 /* Load trusted root */
629 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 );
630
631 /* Load a chain with nb_int intermediates (from 01 to nb_int),
632 * plus one "end-entity" cert (nb_int + 1) */
633 ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
634 nb_int + 1 );
635 TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf );
636 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 );
637
638 /* Try to verify that chain */
639 ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags,
640 NULL, NULL );
641 TEST_ASSERT( ret == ret_chk );
642 TEST_ASSERT( flags == (uint32_t) flags_chk );
643
644exit:
645 mbedtls_x509_crt_free( &chain );
646 mbedtls_x509_crt_free( &trusted );
647}
648/* END_CASE */
649
650/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200651void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca,
652 int flags_result, int result,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200653 char *profile_name, int vrfy_fatal_lvls )
Janos Follath822b2c32015-10-11 10:25:22 +0200654{
655 char* act;
656 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200657 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100658 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200659 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +0200660
Jaeden Amero3ea26872019-02-13 11:30:22 +0000661#if defined(MBEDTLS_USE_PSA_CRYPTO)
662 TEST_ASSERT( psa_crypto_init() == 0 );
663#endif
664
Janos Follath822b2c32015-10-11 10:25:22 +0200665 mbedtls_x509_crt_init( &chain );
666 mbedtls_x509_crt_init( &trusted );
667
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900668 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100669 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
670 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
Janos Follath822b2c32015-10-11 10:25:22 +0200671
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200672 if( strcmp( profile_name, "" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200673 profile = &mbedtls_x509_crt_profile_default;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200674 else if( strcmp( profile_name, "next" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200675 profile = &mbedtls_x509_crt_profile_next;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200676 else if( strcmp( profile_name, "suiteb" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200677 profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200678 else if( strcmp( profile_name, "rsa3072" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200679 profile = &profile_rsa3072;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200680 else if( strcmp( profile_name, "sha512" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200681 profile = &profile_sha512;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200682
683 res = mbedtls_x509_crt_verify_with_profile( &chain, &trusted, NULL, profile,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200684 NULL, &flags, verify_fatal, &vrfy_fatal_lvls );
Janos Follathef4f2582015-10-11 16:17:27 +0200685
686 TEST_ASSERT( res == ( result ) );
687 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Janos Follath822b2c32015-10-11 10:25:22 +0200688
689exit:
690 mbedtls_x509_crt_free( &trusted );
691 mbedtls_x509_crt_free( &chain );
692}
693/* END_CASE */
694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100696void x509_oid_desc( data_t * buf, char * ref_desc )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100697{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000699 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000700 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100701
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +0100704 oid.p = buf->x;
705 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100708
709 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000710 {
711 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100712 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000713 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100714 else
715 {
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000716 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100717 TEST_ASSERT( desc != NULL );
718 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
719 }
720}
721/* END_CASE */
722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100724void x509_oid_numstr( data_t * oid_buf, char * numstr, int blen, int ret )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100725{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100727 char num_buf[100];
728
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100729 memset( num_buf, 0x2a, sizeof num_buf );
730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +0100732 oid.p = oid_buf->x;
733 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100734
735 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100738
739 if( ret >= 0 )
740 {
741 TEST_ASSERT( num_buf[ret] == 0 );
742 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
743 }
744}
745/* END_CASE */
746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_KEY_USAGE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100748void x509_check_key_usage( char * crt_file, int usage, int ret )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200749{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200757
Paul Bakkerbd51b262014-07-10 15:26:12 +0200758exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200760}
761/* END_CASE */
762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Azim Khan5fcca462018-06-29 11:05:32 +0100764void x509_check_extended_key_usage( char * crt_file, data_t * oid, int ret
Azim Khand30ca132017-06-09 04:32:58 +0100765 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200766{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200770
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200773
Azim Khand30ca132017-06-09 04:32:58 +0100774 TEST_ASSERT( mbedtls_x509_crt_check_extended_key_usage( &crt, (const char *)oid->x, oid->len ) == ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200775
Paul Bakkerbd51b262014-07-10 15:26:12 +0200776exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200778}
779/* END_CASE */
780
Andres AG4b76aec2016-09-23 13:16:02 +0100781/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100782void x509_get_time( int tag, char * time_str, int ret, int year, int mon,
783 int day, int hour, int min, int sec )
Andres AG4b76aec2016-09-23 13:16:02 +0100784{
785 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +0000786 unsigned char buf[21];
Andres AG4b76aec2016-09-23 13:16:02 +0100787 unsigned char* start = buf;
788 unsigned char* end = buf;
789
790 memset( &time, 0x00, sizeof( time ) );
791 *end = (unsigned char)tag; end++;
Janos Follathea7054a2017-02-08 14:13:02 +0000792 *end = strlen( time_str );
793 TEST_ASSERT( *end < 20 );
Andres AG4b76aec2016-09-23 13:16:02 +0100794 end++;
795 memcpy( end, time_str, (size_t)*(end - 1) );
796 end += *(end - 1);
797
798 TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
799 if( ret == 0 )
800 {
801 TEST_ASSERT( year == time.year );
802 TEST_ASSERT( mon == time.mon );
803 TEST_ASSERT( day == time.day );
804 TEST_ASSERT( hour == time.hour );
805 TEST_ASSERT( min == time.min );
806 TEST_ASSERT( sec == time.sec );
807 }
808}
809/* END_CASE */
810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Azim Khan5fcca462018-06-29 11:05:32 +0100812void x509_parse_rsassa_pss_params( data_t * hex_params, int params_tag,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200813 int ref_msg_md, int ref_mgf_md,
814 int ref_salt_len, int ref_ret )
815{
816 int my_ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_x509_buf params;
818 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200819 int my_salt_len;
820
Azim Khand30ca132017-06-09 04:32:58 +0100821 params.p = hex_params->x;
822 params.len = hex_params->len;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200823 params.tag = params_tag;
824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 my_ret = mbedtls_x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200826 &my_salt_len );
827
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200828 TEST_ASSERT( my_ret == ref_ret );
829
830 if( ref_ret == 0 )
831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md );
833 TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200834 TEST_ASSERT( my_salt_len == ref_salt_len );
835 }
836
Paul Bakkerbd51b262014-07-10 15:26:12 +0200837exit:
Azim Khand30ca132017-06-09 04:32:58 +0100838 ;;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200839}
840/* END_CASE */
841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100843void x509_selftest( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000844{
Andres AG93012e82016-09-09 09:10:28 +0100845 TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000846}
Paul Bakker33b43f12013-08-20 11:48:36 +0200847/* END_CASE */