blob: 552c494b0e77b097f7390a3b9bdaf37643e89c2c [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_x509_crt_init( &crt );
338 mbedtls_x509_crt_init( &ca );
339 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000340
Paul Bakker33b43f12013-08-20 11:48:36 +0200341 if( strcmp( cn_name_str, "NULL" ) != 0 )
342 cn_name = cn_name_str;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200343
Manuel Pégourié-Gonnarda54f6cc2017-08-09 10:41:42 +0200344 if( strcmp( profile_str, "" ) == 0 )
Gilles Peskineef86ab22017-05-05 18:59:02 +0200345 profile = &mbedtls_x509_crt_profile_default;
Ron Eldorc1539982018-02-06 18:47:17 +0200346 else if( strcmp( profile_str, "next" ) == 0 )
347 profile = &mbedtls_x509_crt_profile_next;
348 else if( strcmp( profile_str, "suite_b" ) == 0 )
349 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200350 else if( strcmp( profile_str, "compat" ) == 0 )
351 profile = &compat_profile;
352 else
353 TEST_ASSERT( "Unknown algorithm profile" == 0 );
354
Paul Bakker33b43f12013-08-20 11:48:36 +0200355 if( strcmp( verify_callback, "NULL" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200356 f_vrfy = NULL;
Paul Bakker33b43f12013-08-20 11:48:36 +0200357 else if( strcmp( verify_callback, "verify_none" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200358 f_vrfy = verify_none;
Paul Bakker33b43f12013-08-20 11:48:36 +0200359 else if( strcmp( verify_callback, "verify_all" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200360 f_vrfy = verify_all;
361 else
362 TEST_ASSERT( "No known verify callback selected" == 0 );
363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
365 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
366 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000367
Gilles Peskineef86ab22017-05-05 18:59:02 +0200368 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 +0200369
Paul Bakkerbd51b262014-07-10 15:26:12 +0200370 TEST_ASSERT( res == ( result ) );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200371 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200372
373exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_x509_crt_free( &crt );
375 mbedtls_x509_crt_free( &ca );
376 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000377}
Paul Bakker33b43f12013-08-20 11:48:36 +0200378/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200381void x509_verify_callback( char *crt_file, char *ca_file, char *name,
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200382 int exp_ret, char *exp_vrfy_out )
383{
384 int ret;
385 mbedtls_x509_crt crt;
386 mbedtls_x509_crt ca;
387 uint32_t flags = 0;
388 verify_print_context vrfy_ctx;
389
390 mbedtls_x509_crt_init( &crt );
391 mbedtls_x509_crt_init( &ca );
392 verify_print_init( &vrfy_ctx );
393
394 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
395 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
396
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200397 if( strcmp( name, "NULL" ) == 0 )
398 name = NULL;
399
Gilles Peskineef86ab22017-05-05 18:59:02 +0200400 ret = mbedtls_x509_crt_verify_with_profile( &crt, &ca, NULL,
401 &compat_profile,
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200402 name, &flags,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200403 verify_print, &vrfy_ctx );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200404
405 TEST_ASSERT( ret == exp_ret );
406 TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
407
408exit:
409 mbedtls_x509_crt_free( &crt );
410 mbedtls_x509_crt_free( &ca );
411}
412/* END_CASE */
413
414/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100415void mbedtls_x509_dn_gets( char * crt_file, char * entity, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000416{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000418 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200419 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000422 memset( buf, 0, 2000 );
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200425 if( strcmp( entity, "subject" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 res = mbedtls_x509_dn_gets( buf, 2000, &crt.subject );
Paul Bakker33b43f12013-08-20 11:48:36 +0200427 else if( strcmp( entity, "issuer" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 res = mbedtls_x509_dn_gets( buf, 2000, &crt.issuer );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200429 else
430 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000431
432 TEST_ASSERT( res != -1 );
433 TEST_ASSERT( res != -2 );
434
Paul Bakker33b43f12013-08-20 11:48:36 +0200435 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200436
437exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000439}
Paul Bakker33b43f12013-08-20 11:48:36 +0200440/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100443void mbedtls_x509_time_is_past( char * crt_file, char * entity, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000444{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200450
Paul Bakker33b43f12013-08-20 11:48:36 +0200451 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100452 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_from ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200453 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100454 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_to ) == result );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200455 else
456 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000457
Paul Bakkerbd51b262014-07-10 15:26:12 +0200458exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000460}
Paul Bakker33b43f12013-08-20 11:48:36 +0200461/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100464void mbedtls_x509_time_is_future( char * crt_file, char * entity, int result )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100465{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100471
472 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100473 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_from ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100474 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100475 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_to ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100476 else
477 TEST_ASSERT( "Unknown entity" == 0 );
478
Paul Bakkerbd51b262014-07-10 15:26:12 +0200479exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100481}
482/* END_CASE */
483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100485void x509parse_crt_file( char * crt_file, int result )
Paul Bakker5a5fa922014-09-26 14:53:04 +0200486{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +0200488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_x509_crt_init( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200492
493exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_x509_crt_free( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200495}
496/* END_CASE */
497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100499void x509parse_crt( data_t * buf, char * result_str, int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000500{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_x509_crt crt;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000502 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100503 int res;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_x509_crt_init( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000506 memset( output, 0, 2000 );
507
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000508
Azim Khand30ca132017-06-09 04:32:58 +0100509 TEST_ASSERT( mbedtls_x509_crt_parse( &crt, buf->x, buf->len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200510 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200513
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000514 TEST_ASSERT( res != -1 );
515 TEST_ASSERT( res != -2 );
516
Paul Bakker33b43f12013-08-20 11:48:36 +0200517 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000518 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000519
Paul Bakkerbd51b262014-07-10 15:26:12 +0200520exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000522}
Paul Bakker33b43f12013-08-20 11:48:36 +0200523/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100526void x509parse_crl( data_t * buf, char * result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000527{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000529 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100530 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000533 memset( output, 0, 2000 );
534
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000535
Azim Khand30ca132017-06-09 04:32:58 +0100536 TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf->x, buf->len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200537 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200540
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000541 TEST_ASSERT( res != -1 );
542 TEST_ASSERT( res != -2 );
543
Paul Bakker33b43f12013-08-20 11:48:36 +0200544 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000545 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000546
Paul Bakkerbd51b262014-07-10 15:26:12 +0200547exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000549}
Paul Bakker33b43f12013-08-20 11:48:36 +0200550/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100553void mbedtls_x509_csr_parse( data_t * csr_der, char * ref_out, int ref_ret )
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200554{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200556 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200557 int my_ret;
558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200560 memset( my_out, 0, sizeof( my_out ) );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200561
Azim Khand30ca132017-06-09 04:32:58 +0100562 my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der->x, csr_der->len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200563 TEST_ASSERT( my_ret == ref_ret );
564
565 if( ref_ret == 0 )
566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200568 TEST_ASSERT( my_out_len == strlen( ref_out ) );
569 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
570 }
571
Paul Bakkerbd51b262014-07-10 15:26:12 +0200572exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200574}
575/* END_CASE */
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100578void mbedtls_x509_crt_parse_path( char * crt_path, int ret, int nb_crt )
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100579{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100581 int i;
582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_x509_crt_init( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100586
587 /* Check how many certs we got */
588 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
589 if( cur->raw.p != NULL )
590 i++;
591
592 TEST_ASSERT( i == nb_crt );
593
Paul Bakkerbd51b262014-07-10 15:26:12 +0200594exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100596}
597/* END_CASE */
598
Janos Follath822b2c32015-10-11 10:25:22 +0200599/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +0200600void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int,
601 int ret_chk, int flags_chk )
602{
603 char file_buf[128];
604 int ret;
605 uint32_t flags;
606 mbedtls_x509_crt trusted, chain;
607
608 /*
609 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
610 * with NN.crt signed by NN-1.crt
611 */
612
613 mbedtls_x509_crt_init( &trusted );
614 mbedtls_x509_crt_init( &chain );
615
616 /* Load trusted root */
617 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 );
618
619 /* Load a chain with nb_int intermediates (from 01 to nb_int),
620 * plus one "end-entity" cert (nb_int + 1) */
621 ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
622 nb_int + 1 );
623 TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf );
624 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 );
625
626 /* Try to verify that chain */
627 ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags,
628 NULL, NULL );
629 TEST_ASSERT( ret == ret_chk );
630 TEST_ASSERT( flags == (uint32_t) flags_chk );
631
632exit:
633 mbedtls_x509_crt_free( &chain );
634 mbedtls_x509_crt_free( &trusted );
635}
636/* END_CASE */
637
638/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200639void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca,
640 int flags_result, int result,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200641 char *profile_name, int vrfy_fatal_lvls )
Janos Follath822b2c32015-10-11 10:25:22 +0200642{
643 char* act;
644 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200645 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100646 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200647 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +0200648
Janos Follath822b2c32015-10-11 10:25:22 +0200649 mbedtls_x509_crt_init( &chain );
650 mbedtls_x509_crt_init( &trusted );
651
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900652 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100653 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
654 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
Janos Follath822b2c32015-10-11 10:25:22 +0200655
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200656 if( strcmp( profile_name, "" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200657 profile = &mbedtls_x509_crt_profile_default;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200658 else if( strcmp( profile_name, "next" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200659 profile = &mbedtls_x509_crt_profile_next;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200660 else if( strcmp( profile_name, "suiteb" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200661 profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200662 else if( strcmp( profile_name, "rsa3072" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200663 profile = &profile_rsa3072;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200664 else if( strcmp( profile_name, "sha512" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200665 profile = &profile_sha512;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200666
667 res = mbedtls_x509_crt_verify_with_profile( &chain, &trusted, NULL, profile,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200668 NULL, &flags, verify_fatal, &vrfy_fatal_lvls );
Janos Follathef4f2582015-10-11 16:17:27 +0200669
670 TEST_ASSERT( res == ( result ) );
671 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Janos Follath822b2c32015-10-11 10:25:22 +0200672
673exit:
674 mbedtls_x509_crt_free( &trusted );
675 mbedtls_x509_crt_free( &chain );
676}
677/* END_CASE */
678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100680void x509_oid_desc( data_t * buf, char * ref_desc )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100681{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000683 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000684 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100685
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +0100688 oid.p = buf->x;
689 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100692
693 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000694 {
695 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100696 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000697 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100698 else
699 {
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000700 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100701 TEST_ASSERT( desc != NULL );
702 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
703 }
704}
705/* END_CASE */
706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100708void x509_oid_numstr( data_t * oid_buf, char * numstr, int blen, int ret )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100709{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100711 char num_buf[100];
712
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100713 memset( num_buf, 0x2a, sizeof num_buf );
714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +0100716 oid.p = oid_buf->x;
717 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100718
719 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100722
723 if( ret >= 0 )
724 {
725 TEST_ASSERT( num_buf[ret] == 0 );
726 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
727 }
728}
729/* END_CASE */
730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_KEY_USAGE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100732void x509_check_key_usage( char * crt_file, int usage, int ret )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200733{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200741
Paul Bakkerbd51b262014-07-10 15:26:12 +0200742exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200744}
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_EXTENDED_KEY_USAGE */
Azim Khan5fcca462018-06-29 11:05:32 +0100748void x509_check_extended_key_usage( char * crt_file, data_t * oid, int ret
Azim Khand30ca132017-06-09 04:32:58 +0100749 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200750{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200754
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200757
Azim Khand30ca132017-06-09 04:32:58 +0100758 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 +0200759
Paul Bakkerbd51b262014-07-10 15:26:12 +0200760exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200762}
763/* END_CASE */
764
Andres AG4b76aec2016-09-23 13:16:02 +0100765/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100766void x509_get_time( int tag, char * time_str, int ret, int year, int mon,
767 int day, int hour, int min, int sec )
Andres AG4b76aec2016-09-23 13:16:02 +0100768{
769 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +0000770 unsigned char buf[21];
Andres AG4b76aec2016-09-23 13:16:02 +0100771 unsigned char* start = buf;
772 unsigned char* end = buf;
773
774 memset( &time, 0x00, sizeof( time ) );
775 *end = (unsigned char)tag; end++;
Janos Follathea7054a2017-02-08 14:13:02 +0000776 *end = strlen( time_str );
777 TEST_ASSERT( *end < 20 );
Andres AG4b76aec2016-09-23 13:16:02 +0100778 end++;
779 memcpy( end, time_str, (size_t)*(end - 1) );
780 end += *(end - 1);
781
782 TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
783 if( ret == 0 )
784 {
785 TEST_ASSERT( year == time.year );
786 TEST_ASSERT( mon == time.mon );
787 TEST_ASSERT( day == time.day );
788 TEST_ASSERT( hour == time.hour );
789 TEST_ASSERT( min == time.min );
790 TEST_ASSERT( sec == time.sec );
791 }
792}
793/* END_CASE */
794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Azim Khan5fcca462018-06-29 11:05:32 +0100796void x509_parse_rsassa_pss_params( data_t * hex_params, int params_tag,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200797 int ref_msg_md, int ref_mgf_md,
798 int ref_salt_len, int ref_ret )
799{
800 int my_ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 mbedtls_x509_buf params;
802 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200803 int my_salt_len;
804
Azim Khand30ca132017-06-09 04:32:58 +0100805 params.p = hex_params->x;
806 params.len = hex_params->len;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200807 params.tag = params_tag;
808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 my_ret = mbedtls_x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200810 &my_salt_len );
811
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200812 TEST_ASSERT( my_ret == ref_ret );
813
814 if( ref_ret == 0 )
815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md );
817 TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200818 TEST_ASSERT( my_salt_len == ref_salt_len );
819 }
820
Paul Bakkerbd51b262014-07-10 15:26:12 +0200821exit:
Azim Khand30ca132017-06-09 04:32:58 +0100822 ;;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200823}
824/* END_CASE */
825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100827void x509_selftest( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000828{
Andres AG93012e82016-09-09 09:10:28 +0100829 TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000830}
Paul Bakker33b43f12013-08-20 11:48:36 +0200831/* END_CASE */