blob: c19bce89fc3f6f14bed7a26253464a8f3f8d74b0 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Andres AG4b76aec2016-09-23 13:16:02 +01002#include "mbedtls/x509.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00003#include "mbedtls/x509_crt.h"
4#include "mbedtls/x509_crl.h"
5#include "mbedtls/x509_csr.h"
6#include "mbedtls/pem.h"
7#include "mbedtls/oid.h"
8#include "mbedtls/base64.h"
Paul Bakkerb63b0af2011-01-13 17:54:59 +00009
Simon Butcher9e24b512017-07-28 12:15:13 +010010#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
Hanno Becker3b1422e2017-07-26 13:38:02 +010011#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
12than the current threshold 19. To test larger values, please \
13adapt the script tests/data_files/dir-max/long.sh."
14#endif
15
Gilles Peskineef86ab22017-05-05 18:59:02 +020016/* Profile for backward compatibility. Allows SHA-1, unlike the default
17 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020018const mbedtls_x509_crt_profile compat_profile =
19{
20 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
21 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) |
22 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
23 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
24 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
25 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
26 0xFFFFFFF, /* Any PK alg */
27 0xFFFFFFF, /* Any curve */
28 1024,
29};
30
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020031const mbedtls_x509_crt_profile profile_rsa3072 =
32{
33 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
34 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
35 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
36 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA ),
37 0,
38 3072,
39};
40
41const mbedtls_x509_crt_profile profile_sha512 =
42{
43 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
44 0xFFFFFFF, /* Any PK alg */
45 0xFFFFFFF, /* Any curve */
46 1024,
47};
48
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020049int verify_none( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000050{
Paul Bakker5a624082011-01-18 16:31:52 +000051 ((void) data);
52 ((void) crt);
53 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010054 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020055
Paul Bakker915275b2012-09-28 07:10:55 +000056 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000057}
58
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020059int verify_all( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000060{
Paul Bakker5a624082011-01-18 16:31:52 +000061 ((void) data);
62 ((void) crt);
63 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000064 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000065
Paul Bakkerb63b0af2011-01-13 17:54:59 +000066 return 0;
67}
68
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +020069int verify_fatal( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
70{
71 int *levels = (int *) data;
72
73 ((void) crt);
74 ((void) certificate_depth);
75
76 /* Simulate a fatal error in the callback */
77 if( *levels & ( 1 << certificate_depth ) )
78 {
79 *flags |= ( 1 << certificate_depth );
Manuel Pégourié-Gonnard41859782017-05-23 12:58:53 +020080 return( -1 - certificate_depth );
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +020081 }
82
83 return( 0 );
84}
85
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +090086/* strsep() not available on Windows */
87char *mystrsep(char **stringp, const char *delim)
88{
89 const char *p;
90 char *ret = *stringp;
91
92 if( *stringp == NULL )
93 return( NULL );
94
95 for( ; ; (*stringp)++ )
96 {
97 if( **stringp == '\0' )
98 {
99 *stringp = NULL;
100 goto done;
101 }
102
103 for( p = delim; *p != '\0'; p++ )
104 if( **stringp == *p )
105 {
106 **stringp = '\0';
107 (*stringp)++;
108 goto done;
109 }
110 }
111
112done:
113 return( ret );
114}
115
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200116#if defined(MBEDTLS_X509_CRT_PARSE_C)
117typedef struct {
118 char buf[512];
119 char *p;
120} verify_print_context;
121
122void verify_print_init( verify_print_context *ctx )
123{
124 memset( ctx, 0, sizeof( verify_print_context ) );
125 ctx->p = ctx->buf;
126}
127
128int verify_print( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
129{
130 int ret;
131 verify_print_context *ctx = (verify_print_context *) data;
132 char *p = ctx->p;
133 size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p;
134 ((void) flags);
135
136 ret = mbedtls_snprintf( p, n, "depth %d - serial ", certificate_depth );
137 MBEDTLS_X509_SAFE_SNPRINTF;
138
139 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
140 MBEDTLS_X509_SAFE_SNPRINTF;
141
142 ret = mbedtls_snprintf( p, n, " - subject " );
143 MBEDTLS_X509_SAFE_SNPRINTF;
144
145 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
146 MBEDTLS_X509_SAFE_SNPRINTF;
147
Manuel Pégourié-Gonnardbe2f0b52017-08-21 11:00:22 +0200148 ret = mbedtls_snprintf( p, n, " - flags 0x%08x\n", *flags );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200149 MBEDTLS_X509_SAFE_SNPRINTF;
150
151 ctx->p = p;
152
153 return( 0 );
154}
155#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200156/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000157
Paul Bakker33b43f12013-08-20 11:48:36 +0200158/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200160 * END_DEPENDENCIES
161 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200164void x509_cert_info( char *crt_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000165{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000167 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000168 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000171 memset( buf, 0, 2000 );
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
174 res = mbedtls_x509_crt_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000175
176 TEST_ASSERT( res != -1 );
177 TEST_ASSERT( res != -2 );
178
Paul Bakker33b43f12013-08-20 11:48:36 +0200179 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200180
181exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000183}
Paul Bakker33b43f12013-08-20 11:48:36 +0200184/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
187void mbedtls_x509_crl_info( char *crl_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000188{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000190 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000191 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000194 memset( buf, 0, 2000 );
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
197 res = mbedtls_x509_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000198
199 TEST_ASSERT( res != -1 );
200 TEST_ASSERT( res != -2 );
201
Paul Bakker33b43f12013-08-20 11:48:36 +0200202 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200203
204exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000206}
Paul Bakker33b43f12013-08-20 11:48:36 +0200207/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000208
Andres AGa39db392016-12-08 17:10:38 +0000209/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
210void mbedtls_x509_crl_parse( char *crl_file, int result )
211{
212 mbedtls_x509_crl crl;
213 char buf[2000];
214
215 mbedtls_x509_crl_init( &crl );
216 memset( buf, 0, 2000 );
217
218 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == result );
219
220exit:
221 mbedtls_x509_crl_free( &crl );
222}
223/* END_CASE */
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C */
226void mbedtls_x509_csr_info( char *csr_file, char *result_str )
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100229 char buf[2000];
230 int res;
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100233 memset( buf, 0, 2000 );
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 TEST_ASSERT( mbedtls_x509_csr_parse_file( &csr, csr_file ) == 0 );
236 res = mbedtls_x509_csr_info( buf, 2000, "", &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100237
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100238 TEST_ASSERT( res != -1 );
239 TEST_ASSERT( res != -2 );
240
241 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200242
243exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100245}
246/* END_CASE */
247
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100248/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
249void x509_verify_info( int flags, char *prefix, char *result_str )
250{
251 char buf[2000];
252 int res;
253
254 memset( buf, 0, sizeof( buf ) );
255
256 res = mbedtls_x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
257
258 TEST_ASSERT( res >= 0 );
259
260 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
261}
262/* END_CASE */
263
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200264/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_C */
265void x509_verify_restart( char *crt_file, char *ca_file,
266 int result, int flags_result,
267 int max_ops, int min_restart, int max_restart )
268{
269 int ret, cnt_restart;
270 mbedtls_x509_crt_restart_ctx rs_ctx;
271 mbedtls_x509_crt crt;
272 mbedtls_x509_crt ca;
273 uint32_t flags = 0;
274
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200275 /*
276 * See comments on ecp_test_vect_restart() for op count precision.
277 *
278 * For reference, with mbed TLS 2.6 and default settings:
279 * - ecdsa_verify() for P-256: ~ 6700
280 * - ecdsa_verify() for P-384: ~ 18800
281 * - x509_verify() for server5 -> test-ca2: ~ 18800
282 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
283 */
284
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200285 mbedtls_x509_crt_restart_init( &rs_ctx );
286 mbedtls_x509_crt_init( &crt );
287 mbedtls_x509_crt_init( &ca );
288
289 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
290 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
291
292 mbedtls_ecp_set_max_ops( max_ops );
293
294 cnt_restart = 0;
295 do {
296 ret = mbedtls_x509_crt_verify_restartable( &crt, &ca, NULL,
297 &mbedtls_x509_crt_profile_default, NULL, &flags,
298 NULL, NULL, &rs_ctx );
299 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
300
301 TEST_ASSERT( ret == result );
302 TEST_ASSERT( flags == (uint32_t) flags_result );
303
304 TEST_ASSERT( cnt_restart >= min_restart );
305 TEST_ASSERT( cnt_restart <= max_restart );
306
307 /* Do we leak memory when aborting? */
308 ret = mbedtls_x509_crt_verify_restartable( &crt, &ca, NULL,
309 &mbedtls_x509_crt_profile_default, NULL, &flags,
310 NULL, NULL, &rs_ctx );
311 TEST_ASSERT( ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
312
313exit:
314 mbedtls_x509_crt_restart_free( &rs_ctx );
315 mbedtls_x509_crt_free( &crt );
316 mbedtls_x509_crt_free( &ca );
317}
318/* END_CASE */
319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200321void x509_verify( char *crt_file, char *ca_file, char *crl_file,
322 char *cn_name_str, int result, int flags_result,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200323 char *profile_str,
Paul Bakker33b43f12013-08-20 11:48:36 +0200324 char *verify_callback )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000325{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 mbedtls_x509_crt crt;
327 mbedtls_x509_crt ca;
328 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200329 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000330 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200331 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200332 char * cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200333 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_x509_crt_init( &crt );
336 mbedtls_x509_crt_init( &ca );
337 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000338
Paul Bakker33b43f12013-08-20 11:48:36 +0200339 if( strcmp( cn_name_str, "NULL" ) != 0 )
340 cn_name = cn_name_str;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200341
Manuel Pégourié-Gonnarda54f6cc2017-08-09 10:41:42 +0200342 if( strcmp( profile_str, "" ) == 0 )
Gilles Peskineef86ab22017-05-05 18:59:02 +0200343 profile = &mbedtls_x509_crt_profile_default;
Ron Eldorc1539982018-02-06 18:47:17 +0200344 else if( strcmp( profile_str, "next" ) == 0 )
345 profile = &mbedtls_x509_crt_profile_next;
346 else if( strcmp( profile_str, "suite_b" ) == 0 )
347 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200348 else if( strcmp( profile_str, "compat" ) == 0 )
349 profile = &compat_profile;
350 else
351 TEST_ASSERT( "Unknown algorithm profile" == 0 );
352
Paul Bakker33b43f12013-08-20 11:48:36 +0200353 if( strcmp( verify_callback, "NULL" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200354 f_vrfy = NULL;
Paul Bakker33b43f12013-08-20 11:48:36 +0200355 else if( strcmp( verify_callback, "verify_none" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200356 f_vrfy = verify_none;
Paul Bakker33b43f12013-08-20 11:48:36 +0200357 else if( strcmp( verify_callback, "verify_all" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200358 f_vrfy = verify_all;
359 else
360 TEST_ASSERT( "No known verify callback selected" == 0 );
361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
363 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
364 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000365
Gilles Peskineef86ab22017-05-05 18:59:02 +0200366 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 +0200367
Paul Bakkerbd51b262014-07-10 15:26:12 +0200368 TEST_ASSERT( res == ( result ) );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200369 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200370
371exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_x509_crt_free( &crt );
373 mbedtls_x509_crt_free( &ca );
374 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000375}
Paul Bakker33b43f12013-08-20 11:48:36 +0200376/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200379void x509_verify_callback( char *crt_file, char *ca_file, char *name,
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200380 int exp_ret, char *exp_vrfy_out )
381{
382 int ret;
383 mbedtls_x509_crt crt;
384 mbedtls_x509_crt ca;
385 uint32_t flags = 0;
386 verify_print_context vrfy_ctx;
387
388 mbedtls_x509_crt_init( &crt );
389 mbedtls_x509_crt_init( &ca );
390 verify_print_init( &vrfy_ctx );
391
392 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
393 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
394
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200395 if( strcmp( name, "NULL" ) == 0 )
396 name = NULL;
397
Gilles Peskineef86ab22017-05-05 18:59:02 +0200398 ret = mbedtls_x509_crt_verify_with_profile( &crt, &ca, NULL,
399 &compat_profile,
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200400 name, &flags,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200401 verify_print, &vrfy_ctx );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200402
403 TEST_ASSERT( ret == exp_ret );
404 TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
405
406exit:
407 mbedtls_x509_crt_free( &crt );
408 mbedtls_x509_crt_free( &ca );
409}
410/* END_CASE */
411
412/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413void mbedtls_x509_dn_gets( char *crt_file, char *entity, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000414{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000416 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200417 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000420 memset( buf, 0, 2000 );
421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200423 if( strcmp( entity, "subject" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 res = mbedtls_x509_dn_gets( buf, 2000, &crt.subject );
Paul Bakker33b43f12013-08-20 11:48:36 +0200425 else if( strcmp( entity, "issuer" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 res = mbedtls_x509_dn_gets( buf, 2000, &crt.issuer );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200427 else
428 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000429
430 TEST_ASSERT( res != -1 );
431 TEST_ASSERT( res != -2 );
432
Paul Bakker33b43f12013-08-20 11:48:36 +0200433 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200434
435exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000437}
Paul Bakker33b43f12013-08-20 11:48:36 +0200438/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100441void mbedtls_x509_time_is_past( char *crt_file, char *entity, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000442{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200448
Paul Bakker33b43f12013-08-20 11:48:36 +0200449 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100450 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_from ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200451 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100452 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_to ) == result );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200453 else
454 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000455
Paul Bakkerbd51b262014-07-10 15:26:12 +0200456exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000458}
Paul Bakker33b43f12013-08-20 11:48:36 +0200459/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100462void mbedtls_x509_time_is_future( char *crt_file, char *entity, int result )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100463{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100469
470 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100471 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_from ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100472 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100473 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_to ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100474 else
475 TEST_ASSERT( "Unknown entity" == 0 );
476
Paul Bakkerbd51b262014-07-10 15:26:12 +0200477exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100479}
480/* END_CASE */
481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Paul Bakker5a5fa922014-09-26 14:53:04 +0200483void x509parse_crt_file( char *crt_file, int result )
484{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +0200486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_x509_crt_init( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200490
491exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_x509_crt_free( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200493}
494/* END_CASE */
495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200497void x509parse_crt( char *crt_data, char *result_str, int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000498{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_x509_crt crt;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000500 unsigned char buf[2000];
501 unsigned char output[2000];
502 int data_len, res;
503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_x509_crt_init( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000505 memset( buf, 0, 2000 );
506 memset( output, 0, 2000 );
507
Paul Bakker33b43f12013-08-20 11:48:36 +0200508 data_len = unhexify( buf, crt_data );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 TEST_ASSERT( mbedtls_x509_crt_parse( &crt, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200511 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200514
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000515 TEST_ASSERT( res != -1 );
516 TEST_ASSERT( res != -2 );
517
Paul Bakker33b43f12013-08-20 11:48:36 +0200518 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000519 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000520
Paul Bakkerbd51b262014-07-10 15:26:12 +0200521exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000523}
Paul Bakker33b43f12013-08-20 11:48:36 +0200524/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200527void x509parse_crl( char *crl_data, char *result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000528{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000530 unsigned char buf[2000];
531 unsigned char output[2000];
532 int data_len, res;
533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000535 memset( buf, 0, 2000 );
536 memset( output, 0, 2000 );
537
Paul Bakker33b43f12013-08-20 11:48:36 +0200538 data_len = unhexify( buf, crl_data );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200541 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200544
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000545 TEST_ASSERT( res != -1 );
546 TEST_ASSERT( res != -2 );
547
Paul Bakker33b43f12013-08-20 11:48:36 +0200548 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000549 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000550
Paul Bakkerbd51b262014-07-10 15:26:12 +0200551exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000553}
Paul Bakker33b43f12013-08-20 11:48:36 +0200554/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */
557void mbedtls_x509_csr_parse( char *csr_der_hex, char *ref_out, int ref_ret )
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200558{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_x509_csr csr;
Paul Bakkerbd51b262014-07-10 15:26:12 +0200560 unsigned char *csr_der = NULL;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200561 char my_out[1000];
562 size_t csr_der_len;
563 int my_ret;
564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200566 memset( my_out, 0, sizeof( my_out ) );
567 csr_der = unhexify_alloc( csr_der_hex, &csr_der_len );
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der, csr_der_len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200570 TEST_ASSERT( my_ret == ref_ret );
571
572 if( ref_ret == 0 )
573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200575 TEST_ASSERT( my_out_len == strlen( ref_out ) );
576 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
577 }
578
Paul Bakkerbd51b262014-07-10 15:26:12 +0200579exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_x509_csr_free( &csr );
581 mbedtls_free( csr_der );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200582}
583/* END_CASE */
584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
586void mbedtls_x509_crt_parse_path( char *crt_path, int ret, int nb_crt )
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100587{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100589 int i;
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 mbedtls_x509_crt_init( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100594
595 /* Check how many certs we got */
596 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
597 if( cur->raw.p != NULL )
598 i++;
599
600 TEST_ASSERT( i == nb_crt );
601
Paul Bakkerbd51b262014-07-10 15:26:12 +0200602exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 mbedtls_x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100604}
605/* END_CASE */
606
Janos Follath822b2c32015-10-11 10:25:22 +0200607/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +0200608void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int,
609 int ret_chk, int flags_chk )
610{
611 char file_buf[128];
612 int ret;
613 uint32_t flags;
614 mbedtls_x509_crt trusted, chain;
615
616 /*
617 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
618 * with NN.crt signed by NN-1.crt
619 */
620
621 mbedtls_x509_crt_init( &trusted );
622 mbedtls_x509_crt_init( &chain );
623
624 /* Load trusted root */
625 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 );
626
627 /* Load a chain with nb_int intermediates (from 01 to nb_int),
628 * plus one "end-entity" cert (nb_int + 1) */
629 ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
630 nb_int + 1 );
631 TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf );
632 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 );
633
634 /* Try to verify that chain */
635 ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags,
636 NULL, NULL );
637 TEST_ASSERT( ret == ret_chk );
638 TEST_ASSERT( flags == (uint32_t) flags_chk );
639
640exit:
641 mbedtls_x509_crt_free( &chain );
642 mbedtls_x509_crt_free( &trusted );
643}
644/* END_CASE */
645
646/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200647void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca,
648 int flags_result, int result,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200649 char *profile_name, int vrfy_fatal_lvls )
Janos Follath822b2c32015-10-11 10:25:22 +0200650{
651 char* act;
652 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200653 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100654 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200655 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +0200656
Janos Follath822b2c32015-10-11 10:25:22 +0200657 mbedtls_x509_crt_init( &chain );
658 mbedtls_x509_crt_init( &trusted );
659
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900660 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100661 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
662 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
Janos Follath822b2c32015-10-11 10:25:22 +0200663
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200664 if( strcmp( profile_name, "" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200665 profile = &mbedtls_x509_crt_profile_default;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200666 else if( strcmp( profile_name, "next" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200667 profile = &mbedtls_x509_crt_profile_next;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200668 else if( strcmp( profile_name, "suiteb" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200669 profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200670 else if( strcmp( profile_name, "rsa3072" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200671 profile = &profile_rsa3072;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200672 else if( strcmp( profile_name, "sha512" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200673 profile = &profile_sha512;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200674
675 res = mbedtls_x509_crt_verify_with_profile( &chain, &trusted, NULL, profile,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200676 NULL, &flags, verify_fatal, &vrfy_fatal_lvls );
Janos Follathef4f2582015-10-11 16:17:27 +0200677
678 TEST_ASSERT( res == ( result ) );
679 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Janos Follath822b2c32015-10-11 10:25:22 +0200680
681exit:
682 mbedtls_x509_crt_free( &trusted );
683 mbedtls_x509_crt_free( &chain );
684}
685/* END_CASE */
686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100688void x509_oid_desc( char *oid_str, char *ref_desc )
689{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000691 const char *desc = NULL;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100692 unsigned char buf[20];
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000693 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100694
695 memset( buf, 0, sizeof buf );
696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 oid.tag = MBEDTLS_ASN1_OID;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100698 oid.len = unhexify( buf, oid_str );
699 oid.p = buf;
700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100702
703 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000704 {
705 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100706 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000707 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100708 else
709 {
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000710 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100711 TEST_ASSERT( desc != NULL );
712 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
713 }
714}
715/* END_CASE */
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100718void x509_oid_numstr( char *oid_str, char *numstr, int blen, int ret )
719{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100721 unsigned char oid_buf[20];
722 char num_buf[100];
723
724 memset( oid_buf, 0x00, sizeof oid_buf );
725 memset( num_buf, 0x2a, sizeof num_buf );
726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 oid.tag = MBEDTLS_ASN1_OID;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100728 oid.len = unhexify( oid_buf, oid_str );
729 oid.p = oid_buf;
730
731 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100734
735 if( ret >= 0 )
736 {
737 TEST_ASSERT( num_buf[ret] == 0 );
738 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
739 }
740}
741/* END_CASE */
742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200744void x509_check_key_usage( char *crt_file, int usage, int ret )
745{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
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_check_key_usage( &crt, usage ) == ret );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200753
Paul Bakkerbd51b262014-07-10 15:26:12 +0200754exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200756}
757/* END_CASE */
758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200760void x509_check_extended_key_usage( char *crt_file, char *usage_hex, int ret )
761{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200763 char oid[50];
764 size_t len;
765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200767
768 len = unhexify( (unsigned char *) oid, usage_hex );
769
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 TEST_ASSERT( mbedtls_x509_crt_check_extended_key_usage( &crt, 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 */
780void x509_get_time( int tag, char *time_str, int ret,
781 int year, int mon, int day,
782 int hour, int min, int sec )
783{
784 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +0000785 unsigned char buf[21];
Andres AG4b76aec2016-09-23 13:16:02 +0100786 unsigned char* start = buf;
787 unsigned char* end = buf;
788
789 memset( &time, 0x00, sizeof( time ) );
790 *end = (unsigned char)tag; end++;
Janos Follathea7054a2017-02-08 14:13:02 +0000791 *end = strlen( time_str );
792 TEST_ASSERT( *end < 20 );
Andres AG4b76aec2016-09-23 13:16:02 +0100793 end++;
794 memcpy( end, time_str, (size_t)*(end - 1) );
795 end += *(end - 1);
796
797 TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
798 if( ret == 0 )
799 {
800 TEST_ASSERT( year == time.year );
801 TEST_ASSERT( mon == time.mon );
802 TEST_ASSERT( day == time.day );
803 TEST_ASSERT( hour == time.hour );
804 TEST_ASSERT( min == time.min );
805 TEST_ASSERT( sec == time.sec );
806 }
807}
808/* END_CASE */
809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200811void x509_parse_rsassa_pss_params( char *hex_params, int params_tag,
812 int ref_msg_md, int ref_mgf_md,
813 int ref_salt_len, int ref_ret )
814{
815 int my_ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 mbedtls_x509_buf params;
817 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200818 int my_salt_len;
819
820 params.p = unhexify_alloc( hex_params, &params.len );
821 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:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 mbedtls_free( params.p );
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 */
Paul Bakker33b43f12013-08-20 11:48:36 +0200841void 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 */