blob: 01b18f6e0ef8e668bc2a271e5e363b395c6088fe [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
Hanno Beckercfa34182019-06-03 14:27:03 +010018/* Test-only profile allowing all digests, PK algorithms, and curves. */
19const mbedtls_x509_crt_profile profile_all =
20{
21 0xFFFFFFFF, /* Any MD */
22 0xFFFFFFFF, /* Any PK alg */
23 0xFFFFFFFF, /* Any curve */
24 1024,
25};
26
Gilles Peskineef86ab22017-05-05 18:59:02 +020027/* Profile for backward compatibility. Allows SHA-1, unlike the default
28 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020029const mbedtls_x509_crt_profile compat_profile =
30{
31 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
32 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) |
33 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
34 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
35 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
36 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
37 0xFFFFFFF, /* Any PK alg */
38 0xFFFFFFF, /* Any curve */
39 1024,
40};
41
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020042const mbedtls_x509_crt_profile profile_rsa3072 =
43{
44 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
45 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
46 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
47 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA ),
48 0,
49 3072,
50};
51
52const mbedtls_x509_crt_profile profile_sha512 =
53{
54 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
55 0xFFFFFFF, /* Any PK alg */
56 0xFFFFFFF, /* Any curve */
57 1024,
58};
59
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020060int verify_none( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000061{
Paul Bakker5a624082011-01-18 16:31:52 +000062 ((void) data);
63 ((void) crt);
64 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010065 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020066
Paul Bakker915275b2012-09-28 07:10:55 +000067 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000068}
69
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020070int verify_all( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000071{
Paul Bakker5a624082011-01-18 16:31:52 +000072 ((void) data);
73 ((void) crt);
74 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000075 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000076
Paul Bakkerb63b0af2011-01-13 17:54:59 +000077 return 0;
78}
79
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +020080int verify_fatal( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
81{
82 int *levels = (int *) data;
83
84 ((void) crt);
85 ((void) certificate_depth);
86
87 /* Simulate a fatal error in the callback */
88 if( *levels & ( 1 << certificate_depth ) )
89 {
90 *flags |= ( 1 << certificate_depth );
Manuel Pégourié-Gonnard41859782017-05-23 12:58:53 +020091 return( -1 - certificate_depth );
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +020092 }
93
94 return( 0 );
95}
96
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +090097/* strsep() not available on Windows */
98char *mystrsep(char **stringp, const char *delim)
99{
100 const char *p;
101 char *ret = *stringp;
102
103 if( *stringp == NULL )
104 return( NULL );
105
106 for( ; ; (*stringp)++ )
107 {
108 if( **stringp == '\0' )
109 {
110 *stringp = NULL;
111 goto done;
112 }
113
114 for( p = delim; *p != '\0'; p++ )
115 if( **stringp == *p )
116 {
117 **stringp = '\0';
118 (*stringp)++;
119 goto done;
120 }
121 }
122
123done:
124 return( ret );
125}
126
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200127#if defined(MBEDTLS_X509_CRT_PARSE_C)
128typedef struct {
129 char buf[512];
130 char *p;
131} verify_print_context;
132
133void verify_print_init( verify_print_context *ctx )
134{
135 memset( ctx, 0, sizeof( verify_print_context ) );
136 ctx->p = ctx->buf;
137}
138
139int verify_print( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
140{
141 int ret;
142 verify_print_context *ctx = (verify_print_context *) data;
143 char *p = ctx->p;
144 size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p;
145 ((void) flags);
146
147 ret = mbedtls_snprintf( p, n, "depth %d - serial ", certificate_depth );
148 MBEDTLS_X509_SAFE_SNPRINTF;
149
150 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
151 MBEDTLS_X509_SAFE_SNPRINTF;
152
153 ret = mbedtls_snprintf( p, n, " - subject " );
154 MBEDTLS_X509_SAFE_SNPRINTF;
155
156 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
157 MBEDTLS_X509_SAFE_SNPRINTF;
158
Manuel Pégourié-Gonnardbe2f0b52017-08-21 11:00:22 +0200159 ret = mbedtls_snprintf( p, n, " - flags 0x%08x\n", *flags );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200160 MBEDTLS_X509_SAFE_SNPRINTF;
161
162 ctx->p = p;
163
164 return( 0 );
165}
166#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200167/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000168
Paul Bakker33b43f12013-08-20 11:48:36 +0200169/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200171 * END_DEPENDENCIES
172 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100175void x509_cert_info( char * crt_file, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000176{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000178 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000179 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000182 memset( buf, 0, 2000 );
183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
185 res = mbedtls_x509_crt_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000186
187 TEST_ASSERT( res != -1 );
188 TEST_ASSERT( res != -2 );
189
Paul Bakker33b43f12013-08-20 11:48:36 +0200190 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200191
192exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000194}
Paul Bakker33b43f12013-08-20 11:48:36 +0200195/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100198void mbedtls_x509_crl_info( char * crl_file, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000199{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000201 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000202 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000205 memset( buf, 0, 2000 );
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
208 res = mbedtls_x509_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000209
210 TEST_ASSERT( res != -1 );
211 TEST_ASSERT( res != -2 );
212
Paul Bakker33b43f12013-08-20 11:48:36 +0200213 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200214
215exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000217}
Paul Bakker33b43f12013-08-20 11:48:36 +0200218/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000219
Andres AGa39db392016-12-08 17:10:38 +0000220/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100221void mbedtls_x509_crl_parse( char * crl_file, int result )
Andres AGa39db392016-12-08 17:10:38 +0000222{
223 mbedtls_x509_crl crl;
224 char buf[2000];
225
226 mbedtls_x509_crl_init( &crl );
227 memset( buf, 0, 2000 );
228
229 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == result );
230
231exit:
232 mbedtls_x509_crl_free( &crl );
233}
234/* END_CASE */
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100237void mbedtls_x509_csr_info( char * csr_file, char * result_str )
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100238{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100240 char buf[2000];
241 int res;
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100244 memset( buf, 0, 2000 );
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 TEST_ASSERT( mbedtls_x509_csr_parse_file( &csr, csr_file ) == 0 );
247 res = mbedtls_x509_csr_info( buf, 2000, "", &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100248
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100249 TEST_ASSERT( res != -1 );
250 TEST_ASSERT( res != -2 );
251
252 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200253
254exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100256}
257/* END_CASE */
258
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100259/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100260void x509_verify_info( int flags, char * prefix, char * result_str )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100261{
262 char buf[2000];
263 int res;
264
265 memset( buf, 0, sizeof( buf ) );
266
267 res = mbedtls_x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
268
269 TEST_ASSERT( res >= 0 );
270
271 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
272}
273/* END_CASE */
274
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200275/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_C */
276void x509_verify_restart( char *crt_file, char *ca_file,
277 int result, int flags_result,
278 int max_ops, int min_restart, int max_restart )
279{
280 int ret, cnt_restart;
281 mbedtls_x509_crt_restart_ctx rs_ctx;
282 mbedtls_x509_crt crt;
283 mbedtls_x509_crt ca;
284 uint32_t flags = 0;
285
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200286 /*
287 * See comments on ecp_test_vect_restart() for op count precision.
288 *
289 * For reference, with mbed TLS 2.6 and default settings:
290 * - ecdsa_verify() for P-256: ~ 6700
291 * - ecdsa_verify() for P-384: ~ 18800
292 * - x509_verify() for server5 -> test-ca2: ~ 18800
293 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
294 */
295
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200296 mbedtls_x509_crt_restart_init( &rs_ctx );
297 mbedtls_x509_crt_init( &crt );
298 mbedtls_x509_crt_init( &ca );
299
300 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
301 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
302
303 mbedtls_ecp_set_max_ops( max_ops );
304
305 cnt_restart = 0;
306 do {
307 ret = mbedtls_x509_crt_verify_restartable( &crt, &ca, NULL,
308 &mbedtls_x509_crt_profile_default, NULL, &flags,
309 NULL, NULL, &rs_ctx );
310 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
311
312 TEST_ASSERT( ret == result );
313 TEST_ASSERT( flags == (uint32_t) flags_result );
314
315 TEST_ASSERT( cnt_restart >= min_restart );
316 TEST_ASSERT( cnt_restart <= max_restart );
317
318 /* Do we leak memory when aborting? */
319 ret = mbedtls_x509_crt_verify_restartable( &crt, &ca, NULL,
320 &mbedtls_x509_crt_profile_default, NULL, &flags,
321 NULL, NULL, &rs_ctx );
322 TEST_ASSERT( ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
323
324exit:
325 mbedtls_x509_crt_restart_free( &rs_ctx );
326 mbedtls_x509_crt_free( &crt );
327 mbedtls_x509_crt_free( &ca );
328}
329/* END_CASE */
330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200332void x509_verify( char *crt_file, char *ca_file, char *crl_file,
333 char *cn_name_str, int result, int flags_result,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200334 char *profile_str,
Paul Bakker33b43f12013-08-20 11:48:36 +0200335 char *verify_callback )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000336{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_x509_crt crt;
338 mbedtls_x509_crt ca;
339 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200340 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000341 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200342 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200343 char * cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200344 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 mbedtls_x509_crt_init( &crt );
347 mbedtls_x509_crt_init( &ca );
348 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000349
Paul Bakker33b43f12013-08-20 11:48:36 +0200350 if( strcmp( cn_name_str, "NULL" ) != 0 )
351 cn_name = cn_name_str;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200352
Manuel Pégourié-Gonnarda54f6cc2017-08-09 10:41:42 +0200353 if( strcmp( profile_str, "" ) == 0 )
Gilles Peskineef86ab22017-05-05 18:59:02 +0200354 profile = &mbedtls_x509_crt_profile_default;
Ron Eldorc1539982018-02-06 18:47:17 +0200355 else if( strcmp( profile_str, "next" ) == 0 )
356 profile = &mbedtls_x509_crt_profile_next;
357 else if( strcmp( profile_str, "suite_b" ) == 0 )
358 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200359 else if( strcmp( profile_str, "compat" ) == 0 )
360 profile = &compat_profile;
Hanno Beckercfa34182019-06-03 14:27:03 +0100361 else if( strcmp( profile_str, "all" ) == 0 )
362 profile = &profile_all;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200363 else
364 TEST_ASSERT( "Unknown algorithm profile" == 0 );
365
Paul Bakker33b43f12013-08-20 11:48:36 +0200366 if( strcmp( verify_callback, "NULL" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200367 f_vrfy = NULL;
Paul Bakker33b43f12013-08-20 11:48:36 +0200368 else if( strcmp( verify_callback, "verify_none" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200369 f_vrfy = verify_none;
Paul Bakker33b43f12013-08-20 11:48:36 +0200370 else if( strcmp( verify_callback, "verify_all" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200371 f_vrfy = verify_all;
372 else
373 TEST_ASSERT( "No known verify callback selected" == 0 );
374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
376 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
377 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000378
Gilles Peskineef86ab22017-05-05 18:59:02 +0200379 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 +0200380
Paul Bakkerbd51b262014-07-10 15:26:12 +0200381 TEST_ASSERT( res == ( result ) );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200382 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200383
384exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_x509_crt_free( &crt );
386 mbedtls_x509_crt_free( &ca );
387 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000388}
Paul Bakker33b43f12013-08-20 11:48:36 +0200389/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200392void x509_verify_callback( char *crt_file, char *ca_file, char *name,
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200393 int exp_ret, char *exp_vrfy_out )
394{
395 int ret;
396 mbedtls_x509_crt crt;
397 mbedtls_x509_crt ca;
398 uint32_t flags = 0;
399 verify_print_context vrfy_ctx;
400
401 mbedtls_x509_crt_init( &crt );
402 mbedtls_x509_crt_init( &ca );
403 verify_print_init( &vrfy_ctx );
404
405 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
406 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
407
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200408 if( strcmp( name, "NULL" ) == 0 )
409 name = NULL;
410
Gilles Peskineef86ab22017-05-05 18:59:02 +0200411 ret = mbedtls_x509_crt_verify_with_profile( &crt, &ca, NULL,
412 &compat_profile,
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200413 name, &flags,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200414 verify_print, &vrfy_ctx );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200415
416 TEST_ASSERT( ret == exp_ret );
417 TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
418
419exit:
420 mbedtls_x509_crt_free( &crt );
421 mbedtls_x509_crt_free( &ca );
422}
423/* END_CASE */
424
425/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100426void mbedtls_x509_dn_gets( char * crt_file, char * entity, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000427{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000429 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200430 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000433 memset( buf, 0, 2000 );
434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200436 if( strcmp( entity, "subject" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 res = mbedtls_x509_dn_gets( buf, 2000, &crt.subject );
Paul Bakker33b43f12013-08-20 11:48:36 +0200438 else if( strcmp( entity, "issuer" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 res = mbedtls_x509_dn_gets( buf, 2000, &crt.issuer );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200440 else
441 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000442
443 TEST_ASSERT( res != -1 );
444 TEST_ASSERT( res != -2 );
445
Paul Bakker33b43f12013-08-20 11:48:36 +0200446 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200447
448exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000450}
Paul Bakker33b43f12013-08-20 11:48:36 +0200451/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100454void mbedtls_x509_time_is_past( char * crt_file, char * entity, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000455{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200461
Paul Bakker33b43f12013-08-20 11:48:36 +0200462 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100463 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_from ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200464 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100465 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_to ) == result );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200466 else
467 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000468
Paul Bakkerbd51b262014-07-10 15:26:12 +0200469exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000471}
Paul Bakker33b43f12013-08-20 11:48:36 +0200472/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100475void mbedtls_x509_time_is_future( char * crt_file, char * entity, int result )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100476{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100482
483 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100484 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_from ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100485 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100486 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_to ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100487 else
488 TEST_ASSERT( "Unknown entity" == 0 );
489
Paul Bakkerbd51b262014-07-10 15:26:12 +0200490exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100492}
493/* END_CASE */
494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100496void x509parse_crt_file( char * crt_file, int result )
Paul Bakker5a5fa922014-09-26 14:53:04 +0200497{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +0200499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_x509_crt_init( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200503
504exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_x509_crt_free( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200506}
507/* END_CASE */
508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100510void x509parse_crt( data_t * buf, char * result_str, int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000511{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_x509_crt crt;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000513 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100514 int res;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_x509_crt_init( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000517 memset( output, 0, 2000 );
518
Hanno Becker2fa5e732019-01-31 09:15:53 +0000519 TEST_ASSERT( mbedtls_x509_crt_parse_der( &crt, buf->x, buf->len ) == ( result ) );
520 if( ( result ) == 0 )
521 {
522 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000523
Hanno Becker2fa5e732019-01-31 09:15:53 +0000524 TEST_ASSERT( res != -1 );
525 TEST_ASSERT( res != -2 );
526
527 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
528 }
529
530 mbedtls_x509_crt_free( &crt );
531 mbedtls_x509_crt_init( &crt );
532 memset( output, 0, 2000 );
533
534 TEST_ASSERT( mbedtls_x509_crt_parse_der_nocopy( &crt, buf->x, buf->len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200535 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200538
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000539 TEST_ASSERT( res != -1 );
540 TEST_ASSERT( res != -2 );
541
Paul Bakker33b43f12013-08-20 11:48:36 +0200542 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000543 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000544
Paul Bakkerbd51b262014-07-10 15:26:12 +0200545exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000547}
Paul Bakker33b43f12013-08-20 11:48:36 +0200548/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100551void x509parse_crl( data_t * buf, char * result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000552{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000554 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100555 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000558 memset( output, 0, 2000 );
559
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000560
Azim Khand30ca132017-06-09 04:32:58 +0100561 TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf->x, buf->len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200562 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200565
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000566 TEST_ASSERT( res != -1 );
567 TEST_ASSERT( res != -2 );
568
Paul Bakker33b43f12013-08-20 11:48:36 +0200569 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000570 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000571
Paul Bakkerbd51b262014-07-10 15:26:12 +0200572exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000574}
Paul Bakker33b43f12013-08-20 11:48:36 +0200575/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100578void mbedtls_x509_csr_parse( data_t * csr_der, char * ref_out, int ref_ret )
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200579{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200581 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200582 int my_ret;
583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200585 memset( my_out, 0, sizeof( my_out ) );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200586
Azim Khand30ca132017-06-09 04:32:58 +0100587 my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der->x, csr_der->len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200588 TEST_ASSERT( my_ret == ref_ret );
589
590 if( ref_ret == 0 )
591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200593 TEST_ASSERT( my_out_len == strlen( ref_out ) );
594 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
595 }
596
Paul Bakkerbd51b262014-07-10 15:26:12 +0200597exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200599}
600/* END_CASE */
601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100603void mbedtls_x509_crt_parse_path( char * crt_path, int ret, int nb_crt )
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100604{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100606 int i;
607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_x509_crt_init( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100611
612 /* Check how many certs we got */
613 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
614 if( cur->raw.p != NULL )
615 i++;
616
617 TEST_ASSERT( i == nb_crt );
618
Paul Bakkerbd51b262014-07-10 15:26:12 +0200619exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100621}
622/* END_CASE */
623
Janos Follath822b2c32015-10-11 10:25:22 +0200624/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +0200625void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int,
626 int ret_chk, int flags_chk )
627{
628 char file_buf[128];
629 int ret;
630 uint32_t flags;
631 mbedtls_x509_crt trusted, chain;
632
633 /*
634 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
635 * with NN.crt signed by NN-1.crt
636 */
637
638 mbedtls_x509_crt_init( &trusted );
639 mbedtls_x509_crt_init( &chain );
640
641 /* Load trusted root */
642 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 );
643
644 /* Load a chain with nb_int intermediates (from 01 to nb_int),
645 * plus one "end-entity" cert (nb_int + 1) */
646 ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
647 nb_int + 1 );
648 TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf );
649 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 );
650
651 /* Try to verify that chain */
652 ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags,
653 NULL, NULL );
654 TEST_ASSERT( ret == ret_chk );
655 TEST_ASSERT( flags == (uint32_t) flags_chk );
656
657exit:
658 mbedtls_x509_crt_free( &chain );
659 mbedtls_x509_crt_free( &trusted );
660}
661/* END_CASE */
662
663/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200664void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca,
665 int flags_result, int result,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200666 char *profile_name, int vrfy_fatal_lvls )
Janos Follath822b2c32015-10-11 10:25:22 +0200667{
668 char* act;
669 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200670 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100671 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200672 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +0200673
Janos Follath822b2c32015-10-11 10:25:22 +0200674 mbedtls_x509_crt_init( &chain );
675 mbedtls_x509_crt_init( &trusted );
676
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900677 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100678 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
679 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
Janos Follath822b2c32015-10-11 10:25:22 +0200680
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200681 if( strcmp( profile_name, "" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200682 profile = &mbedtls_x509_crt_profile_default;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200683 else if( strcmp( profile_name, "next" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200684 profile = &mbedtls_x509_crt_profile_next;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200685 else if( strcmp( profile_name, "suiteb" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200686 profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200687 else if( strcmp( profile_name, "rsa3072" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200688 profile = &profile_rsa3072;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200689 else if( strcmp( profile_name, "sha512" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200690 profile = &profile_sha512;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200691
692 res = mbedtls_x509_crt_verify_with_profile( &chain, &trusted, NULL, profile,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200693 NULL, &flags, verify_fatal, &vrfy_fatal_lvls );
Janos Follathef4f2582015-10-11 16:17:27 +0200694
695 TEST_ASSERT( res == ( result ) );
696 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Janos Follath822b2c32015-10-11 10:25:22 +0200697
698exit:
699 mbedtls_x509_crt_free( &trusted );
700 mbedtls_x509_crt_free( &chain );
701}
702/* END_CASE */
703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100705void x509_oid_desc( data_t * buf, char * ref_desc )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100706{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000708 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000709 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100710
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +0100713 oid.p = buf->x;
714 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100717
718 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000719 {
720 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100721 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000722 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100723 else
724 {
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000725 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100726 TEST_ASSERT( desc != NULL );
727 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
728 }
729}
730/* END_CASE */
731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100733void x509_oid_numstr( data_t * oid_buf, char * numstr, int blen, int ret )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100734{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100736 char num_buf[100];
737
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100738 memset( num_buf, 0x2a, sizeof num_buf );
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +0100741 oid.p = oid_buf->x;
742 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100743
744 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100747
748 if( ret >= 0 )
749 {
750 TEST_ASSERT( num_buf[ret] == 0 );
751 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
752 }
753}
754/* END_CASE */
755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_KEY_USAGE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100757void x509_check_key_usage( char * crt_file, int usage, int ret )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200758{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200766
Paul Bakkerbd51b262014-07-10 15:26:12 +0200767exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200769}
770/* END_CASE */
771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772/* 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 +0100773void x509_check_extended_key_usage( char * crt_file, data_t * oid, int ret
Azim Khand30ca132017-06-09 04:32:58 +0100774 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200775{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200779
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200782
Azim Khand30ca132017-06-09 04:32:58 +0100783 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 +0200784
Paul Bakkerbd51b262014-07-10 15:26:12 +0200785exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200787}
788/* END_CASE */
789
Andres AG4b76aec2016-09-23 13:16:02 +0100790/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100791void x509_get_time( int tag, char * time_str, int ret, int year, int mon,
792 int day, int hour, int min, int sec )
Andres AG4b76aec2016-09-23 13:16:02 +0100793{
794 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +0000795 unsigned char buf[21];
Andres AG4b76aec2016-09-23 13:16:02 +0100796 unsigned char* start = buf;
797 unsigned char* end = buf;
798
799 memset( &time, 0x00, sizeof( time ) );
800 *end = (unsigned char)tag; end++;
Janos Follathea7054a2017-02-08 14:13:02 +0000801 *end = strlen( time_str );
802 TEST_ASSERT( *end < 20 );
Andres AG4b76aec2016-09-23 13:16:02 +0100803 end++;
804 memcpy( end, time_str, (size_t)*(end - 1) );
805 end += *(end - 1);
806
807 TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
808 if( ret == 0 )
809 {
810 TEST_ASSERT( year == time.year );
811 TEST_ASSERT( mon == time.mon );
812 TEST_ASSERT( day == time.day );
813 TEST_ASSERT( hour == time.hour );
814 TEST_ASSERT( min == time.min );
815 TEST_ASSERT( sec == time.sec );
816 }
817}
818/* END_CASE */
819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Azim Khan5fcca462018-06-29 11:05:32 +0100821void x509_parse_rsassa_pss_params( data_t * hex_params, int params_tag,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200822 int ref_msg_md, int ref_mgf_md,
823 int ref_salt_len, int ref_ret )
824{
825 int my_ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 mbedtls_x509_buf params;
827 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200828 int my_salt_len;
829
Azim Khand30ca132017-06-09 04:32:58 +0100830 params.p = hex_params->x;
831 params.len = hex_params->len;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200832 params.tag = params_tag;
833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 my_ret = mbedtls_x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200835 &my_salt_len );
836
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200837 TEST_ASSERT( my_ret == ref_ret );
838
839 if( ref_ret == 0 )
840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md );
842 TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200843 TEST_ASSERT( my_salt_len == ref_salt_len );
844 }
845
Paul Bakkerbd51b262014-07-10 15:26:12 +0200846exit:
Azim Khand30ca132017-06-09 04:32:58 +0100847 ;;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200848}
849/* END_CASE */
850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100852void x509_selftest( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000853{
Andres AG93012e82016-09-09 09:10:28 +0100854 TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000855}
Paul Bakker33b43f12013-08-20 11:48:36 +0200856/* END_CASE */