blob: e6b1b4783cdeed85c07cbaeba2944d8f968c551c [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
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000519
Azim Khand30ca132017-06-09 04:32:58 +0100520 TEST_ASSERT( mbedtls_x509_crt_parse( &crt, buf->x, buf->len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200521 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200524
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000525 TEST_ASSERT( res != -1 );
526 TEST_ASSERT( res != -2 );
527
Paul Bakker33b43f12013-08-20 11:48:36 +0200528 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000529 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000530
Paul Bakkerbd51b262014-07-10 15:26:12 +0200531exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000533}
Paul Bakker33b43f12013-08-20 11:48:36 +0200534/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100537void x509parse_crl( data_t * buf, char * result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000538{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000540 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100541 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000544 memset( output, 0, 2000 );
545
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000546
Azim Khand30ca132017-06-09 04:32:58 +0100547 TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf->x, buf->len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200548 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200551
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000552 TEST_ASSERT( res != -1 );
553 TEST_ASSERT( res != -2 );
554
Paul Bakker33b43f12013-08-20 11:48:36 +0200555 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000556 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000557
Paul Bakkerbd51b262014-07-10 15:26:12 +0200558exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000560}
Paul Bakker33b43f12013-08-20 11:48:36 +0200561/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100564void mbedtls_x509_csr_parse( data_t * csr_der, char * ref_out, int ref_ret )
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200565{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200567 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200568 int my_ret;
569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200571 memset( my_out, 0, sizeof( my_out ) );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200572
Azim Khand30ca132017-06-09 04:32:58 +0100573 my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der->x, csr_der->len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200574 TEST_ASSERT( my_ret == ref_ret );
575
576 if( ref_ret == 0 )
577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200579 TEST_ASSERT( my_out_len == strlen( ref_out ) );
580 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
581 }
582
Paul Bakkerbd51b262014-07-10 15:26:12 +0200583exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200585}
586/* END_CASE */
587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100589void mbedtls_x509_crt_parse_path( char * crt_path, int ret, int nb_crt )
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100590{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100592 int i;
593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 mbedtls_x509_crt_init( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100597
598 /* Check how many certs we got */
599 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
600 if( cur->raw.p != NULL )
601 i++;
602
603 TEST_ASSERT( i == nb_crt );
604
Paul Bakkerbd51b262014-07-10 15:26:12 +0200605exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100607}
608/* END_CASE */
609
Janos Follath822b2c32015-10-11 10:25:22 +0200610/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +0200611void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int,
612 int ret_chk, int flags_chk )
613{
614 char file_buf[128];
615 int ret;
616 uint32_t flags;
617 mbedtls_x509_crt trusted, chain;
618
619 /*
620 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
621 * with NN.crt signed by NN-1.crt
622 */
623
624 mbedtls_x509_crt_init( &trusted );
625 mbedtls_x509_crt_init( &chain );
626
627 /* Load trusted root */
628 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 );
629
630 /* Load a chain with nb_int intermediates (from 01 to nb_int),
631 * plus one "end-entity" cert (nb_int + 1) */
632 ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
633 nb_int + 1 );
634 TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf );
635 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 );
636
637 /* Try to verify that chain */
638 ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags,
639 NULL, NULL );
640 TEST_ASSERT( ret == ret_chk );
641 TEST_ASSERT( flags == (uint32_t) flags_chk );
642
643exit:
644 mbedtls_x509_crt_free( &chain );
645 mbedtls_x509_crt_free( &trusted );
646}
647/* END_CASE */
648
649/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200650void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca,
651 int flags_result, int result,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200652 char *profile_name, int vrfy_fatal_lvls )
Janos Follath822b2c32015-10-11 10:25:22 +0200653{
654 char* act;
655 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200656 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100657 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200658 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +0200659
Janos Follath822b2c32015-10-11 10:25:22 +0200660 mbedtls_x509_crt_init( &chain );
661 mbedtls_x509_crt_init( &trusted );
662
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900663 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100664 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
665 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
Janos Follath822b2c32015-10-11 10:25:22 +0200666
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200667 if( strcmp( profile_name, "" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200668 profile = &mbedtls_x509_crt_profile_default;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200669 else if( strcmp( profile_name, "next" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200670 profile = &mbedtls_x509_crt_profile_next;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200671 else if( strcmp( profile_name, "suiteb" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200672 profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200673 else if( strcmp( profile_name, "rsa3072" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200674 profile = &profile_rsa3072;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200675 else if( strcmp( profile_name, "sha512" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200676 profile = &profile_sha512;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200677
678 res = mbedtls_x509_crt_verify_with_profile( &chain, &trusted, NULL, profile,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200679 NULL, &flags, verify_fatal, &vrfy_fatal_lvls );
Janos Follathef4f2582015-10-11 16:17:27 +0200680
681 TEST_ASSERT( res == ( result ) );
682 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Janos Follath822b2c32015-10-11 10:25:22 +0200683
684exit:
685 mbedtls_x509_crt_free( &trusted );
686 mbedtls_x509_crt_free( &chain );
687}
688/* END_CASE */
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100691void x509_oid_desc( data_t * buf, char * ref_desc )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100692{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000694 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000695 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100696
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +0100699 oid.p = buf->x;
700 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100703
704 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000705 {
706 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100707 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000708 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100709 else
710 {
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000711 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100712 TEST_ASSERT( desc != NULL );
713 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
714 }
715}
716/* END_CASE */
717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100719void x509_oid_numstr( data_t * oid_buf, char * numstr, int blen, int ret )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100720{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100722 char num_buf[100];
723
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100724 memset( num_buf, 0x2a, sizeof num_buf );
725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +0100727 oid.p = oid_buf->x;
728 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100729
730 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100733
734 if( ret >= 0 )
735 {
736 TEST_ASSERT( num_buf[ret] == 0 );
737 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
738 }
739}
740/* END_CASE */
741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_KEY_USAGE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100743void x509_check_key_usage( char * crt_file, int usage, int ret )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200744{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200752
Paul Bakkerbd51b262014-07-10 15:26:12 +0200753exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200755}
756/* END_CASE */
757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758/* 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 +0100759void x509_check_extended_key_usage( char * crt_file, data_t * oid, int ret
Azim Khand30ca132017-06-09 04:32:58 +0100760 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200761{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200765
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200768
Azim Khand30ca132017-06-09 04:32:58 +0100769 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 +0200770
Paul Bakkerbd51b262014-07-10 15:26:12 +0200771exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200773}
774/* END_CASE */
775
Andres AG4b76aec2016-09-23 13:16:02 +0100776/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100777void x509_get_time( int tag, char * time_str, int ret, int year, int mon,
778 int day, int hour, int min, int sec )
Andres AG4b76aec2016-09-23 13:16:02 +0100779{
780 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +0000781 unsigned char buf[21];
Andres AG4b76aec2016-09-23 13:16:02 +0100782 unsigned char* start = buf;
783 unsigned char* end = buf;
784
785 memset( &time, 0x00, sizeof( time ) );
786 *end = (unsigned char)tag; end++;
Janos Follathea7054a2017-02-08 14:13:02 +0000787 *end = strlen( time_str );
788 TEST_ASSERT( *end < 20 );
Andres AG4b76aec2016-09-23 13:16:02 +0100789 end++;
790 memcpy( end, time_str, (size_t)*(end - 1) );
791 end += *(end - 1);
792
793 TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
794 if( ret == 0 )
795 {
796 TEST_ASSERT( year == time.year );
797 TEST_ASSERT( mon == time.mon );
798 TEST_ASSERT( day == time.day );
799 TEST_ASSERT( hour == time.hour );
800 TEST_ASSERT( min == time.min );
801 TEST_ASSERT( sec == time.sec );
802 }
803}
804/* END_CASE */
805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Azim Khan5fcca462018-06-29 11:05:32 +0100807void x509_parse_rsassa_pss_params( data_t * hex_params, int params_tag,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200808 int ref_msg_md, int ref_mgf_md,
809 int ref_salt_len, int ref_ret )
810{
811 int my_ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_x509_buf params;
813 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200814 int my_salt_len;
815
Azim Khand30ca132017-06-09 04:32:58 +0100816 params.p = hex_params->x;
817 params.len = hex_params->len;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200818 params.tag = params_tag;
819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 my_ret = mbedtls_x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200821 &my_salt_len );
822
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200823 TEST_ASSERT( my_ret == ref_ret );
824
825 if( ref_ret == 0 )
826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md );
828 TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200829 TEST_ASSERT( my_salt_len == ref_salt_len );
830 }
831
Paul Bakkerbd51b262014-07-10 15:26:12 +0200832exit:
Azim Khand30ca132017-06-09 04:32:58 +0100833 ;;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200834}
835/* END_CASE */
836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100838void x509_selftest( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000839{
Andres AG93012e82016-09-09 09:10:28 +0100840 TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000841}
Paul Bakker33b43f12013-08-20 11:48:36 +0200842/* END_CASE */