blob: a921310c658b0a9f5a851b369688cc4458d08efa [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
Hanno Becker2d8a2c02019-01-31 09:15:53 +0000508 TEST_ASSERT( mbedtls_x509_crt_parse_der( &crt, buf->x, buf->len ) == ( result ) );
509 if( ( result ) == 0 )
510 {
511 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000512
Hanno Becker2d8a2c02019-01-31 09:15:53 +0000513 TEST_ASSERT( res != -1 );
514 TEST_ASSERT( res != -2 );
515
516 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
517 }
518
519 mbedtls_x509_crt_free( &crt );
520 mbedtls_x509_crt_init( &crt );
521 memset( output, 0, 2000 );
522
523 TEST_ASSERT( mbedtls_x509_crt_parse_der_nocopy( &crt, buf->x, buf->len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200524 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200527
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000528 TEST_ASSERT( res != -1 );
529 TEST_ASSERT( res != -2 );
530
Paul Bakker33b43f12013-08-20 11:48:36 +0200531 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000532 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000533
Paul Bakkerbd51b262014-07-10 15:26:12 +0200534exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000536}
Paul Bakker33b43f12013-08-20 11:48:36 +0200537/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100540void x509parse_crl( data_t * buf, char * result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000541{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000543 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100544 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000547 memset( output, 0, 2000 );
548
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000549
Azim Khand30ca132017-06-09 04:32:58 +0100550 TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf->x, buf->len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200551 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200554
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000555 TEST_ASSERT( res != -1 );
556 TEST_ASSERT( res != -2 );
557
Paul Bakker33b43f12013-08-20 11:48:36 +0200558 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000559 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000560
Paul Bakkerbd51b262014-07-10 15:26:12 +0200561exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000563}
Paul Bakker33b43f12013-08-20 11:48:36 +0200564/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100567void mbedtls_x509_csr_parse( data_t * csr_der, char * ref_out, int ref_ret )
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200568{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200570 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200571 int my_ret;
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200574 memset( my_out, 0, sizeof( my_out ) );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200575
Azim Khand30ca132017-06-09 04:32:58 +0100576 my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der->x, csr_der->len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200577 TEST_ASSERT( my_ret == ref_ret );
578
579 if( ref_ret == 0 )
580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200582 TEST_ASSERT( my_out_len == strlen( ref_out ) );
583 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
584 }
585
Paul Bakkerbd51b262014-07-10 15:26:12 +0200586exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200588}
589/* END_CASE */
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100592void mbedtls_x509_crt_parse_path( char * crt_path, int ret, int nb_crt )
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100593{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100595 int i;
596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_x509_crt_init( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100600
601 /* Check how many certs we got */
602 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
603 if( cur->raw.p != NULL )
604 i++;
605
606 TEST_ASSERT( i == nb_crt );
607
Paul Bakkerbd51b262014-07-10 15:26:12 +0200608exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100610}
611/* END_CASE */
612
Janos Follath822b2c32015-10-11 10:25:22 +0200613/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +0200614void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int,
615 int ret_chk, int flags_chk )
616{
617 char file_buf[128];
618 int ret;
619 uint32_t flags;
620 mbedtls_x509_crt trusted, chain;
621
622 /*
623 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
624 * with NN.crt signed by NN-1.crt
625 */
626
627 mbedtls_x509_crt_init( &trusted );
628 mbedtls_x509_crt_init( &chain );
629
630 /* Load trusted root */
631 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 );
632
633 /* Load a chain with nb_int intermediates (from 01 to nb_int),
634 * plus one "end-entity" cert (nb_int + 1) */
635 ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
636 nb_int + 1 );
637 TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf );
638 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 );
639
640 /* Try to verify that chain */
641 ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags,
642 NULL, NULL );
643 TEST_ASSERT( ret == ret_chk );
644 TEST_ASSERT( flags == (uint32_t) flags_chk );
645
646exit:
647 mbedtls_x509_crt_free( &chain );
648 mbedtls_x509_crt_free( &trusted );
649}
650/* END_CASE */
651
652/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200653void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca,
654 int flags_result, int result,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200655 char *profile_name, int vrfy_fatal_lvls )
Janos Follath822b2c32015-10-11 10:25:22 +0200656{
657 char* act;
658 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200659 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100660 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200661 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +0200662
Janos Follath822b2c32015-10-11 10:25:22 +0200663 mbedtls_x509_crt_init( &chain );
664 mbedtls_x509_crt_init( &trusted );
665
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900666 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100667 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
668 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
Janos Follath822b2c32015-10-11 10:25:22 +0200669
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200670 if( strcmp( profile_name, "" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200671 profile = &mbedtls_x509_crt_profile_default;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200672 else if( strcmp( profile_name, "next" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200673 profile = &mbedtls_x509_crt_profile_next;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200674 else if( strcmp( profile_name, "suiteb" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200675 profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200676 else if( strcmp( profile_name, "rsa3072" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200677 profile = &profile_rsa3072;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200678 else if( strcmp( profile_name, "sha512" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200679 profile = &profile_sha512;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200680
681 res = mbedtls_x509_crt_verify_with_profile( &chain, &trusted, NULL, profile,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200682 NULL, &flags, verify_fatal, &vrfy_fatal_lvls );
Janos Follathef4f2582015-10-11 16:17:27 +0200683
684 TEST_ASSERT( res == ( result ) );
685 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Janos Follath822b2c32015-10-11 10:25:22 +0200686
687exit:
688 mbedtls_x509_crt_free( &trusted );
689 mbedtls_x509_crt_free( &chain );
690}
691/* END_CASE */
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100694void x509_oid_desc( data_t * buf, char * ref_desc )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100695{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000697 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000698 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100699
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +0100702 oid.p = buf->x;
703 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100706
707 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000708 {
709 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100710 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000711 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100712 else
713 {
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000714 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100715 TEST_ASSERT( desc != NULL );
716 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
717 }
718}
719/* END_CASE */
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100722void x509_oid_numstr( data_t * oid_buf, char * numstr, int blen, int ret )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100723{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100725 char num_buf[100];
726
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100727 memset( num_buf, 0x2a, sizeof num_buf );
728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +0100730 oid.p = oid_buf->x;
731 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100732
733 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100736
737 if( ret >= 0 )
738 {
739 TEST_ASSERT( num_buf[ret] == 0 );
740 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
741 }
742}
743/* END_CASE */
744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_KEY_USAGE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100746void x509_check_key_usage( char * crt_file, int usage, int ret )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200747{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200755
Paul Bakkerbd51b262014-07-10 15:26:12 +0200756exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200758}
759/* END_CASE */
760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761/* 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 +0100762void x509_check_extended_key_usage( char * crt_file, data_t * oid, int ret
Azim Khand30ca132017-06-09 04:32:58 +0100763 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200764{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200768
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200771
Azim Khand30ca132017-06-09 04:32:58 +0100772 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 +0200773
Paul Bakkerbd51b262014-07-10 15:26:12 +0200774exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200776}
777/* END_CASE */
778
Andres AG4b76aec2016-09-23 13:16:02 +0100779/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100780void x509_get_time( int tag, char * time_str, int ret, int year, int mon,
781 int day, int hour, int min, int sec )
Andres AG4b76aec2016-09-23 13:16:02 +0100782{
783 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +0000784 unsigned char buf[21];
Andres AG4b76aec2016-09-23 13:16:02 +0100785 unsigned char* start = buf;
786 unsigned char* end = buf;
787
788 memset( &time, 0x00, sizeof( time ) );
789 *end = (unsigned char)tag; end++;
Janos Follathea7054a2017-02-08 14:13:02 +0000790 *end = strlen( time_str );
791 TEST_ASSERT( *end < 20 );
Andres AG4b76aec2016-09-23 13:16:02 +0100792 end++;
793 memcpy( end, time_str, (size_t)*(end - 1) );
794 end += *(end - 1);
795
796 TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
797 if( ret == 0 )
798 {
799 TEST_ASSERT( year == time.year );
800 TEST_ASSERT( mon == time.mon );
801 TEST_ASSERT( day == time.day );
802 TEST_ASSERT( hour == time.hour );
803 TEST_ASSERT( min == time.min );
804 TEST_ASSERT( sec == time.sec );
805 }
806}
807/* END_CASE */
808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Azim Khan5fcca462018-06-29 11:05:32 +0100810void x509_parse_rsassa_pss_params( data_t * hex_params, int params_tag,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200811 int ref_msg_md, int ref_mgf_md,
812 int ref_salt_len, int ref_ret )
813{
814 int my_ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_x509_buf params;
816 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200817 int my_salt_len;
818
Azim Khand30ca132017-06-09 04:32:58 +0100819 params.p = hex_params->x;
820 params.len = hex_params->len;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200821 params.tag = params_tag;
822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 my_ret = mbedtls_x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200824 &my_salt_len );
825
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200826 TEST_ASSERT( my_ret == ref_ret );
827
828 if( ref_ret == 0 )
829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md );
831 TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200832 TEST_ASSERT( my_salt_len == ref_salt_len );
833 }
834
Paul Bakkerbd51b262014-07-10 15:26:12 +0200835exit:
Azim Khand30ca132017-06-09 04:32:58 +0100836 ;;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200837}
838/* END_CASE */
839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100841void x509_selftest( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000842{
Andres AG93012e82016-09-09 09:10:28 +0100843 TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000844}
Paul Bakker33b43f12013-08-20 11:48:36 +0200845/* END_CASE */