blob: 73727b521abea346240ed3ba3fd9182d1ac20d8d [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é-Gonnarda8838af2015-11-02 06:34:46 +090069/* strsep() not available on Windows */
70char *mystrsep(char **stringp, const char *delim)
71{
72 const char *p;
73 char *ret = *stringp;
74
75 if( *stringp == NULL )
76 return( NULL );
77
78 for( ; ; (*stringp)++ )
79 {
80 if( **stringp == '\0' )
81 {
82 *stringp = NULL;
83 goto done;
84 }
85
86 for( p = delim; *p != '\0'; p++ )
87 if( **stringp == *p )
88 {
89 **stringp = '\0';
90 (*stringp)++;
91 goto done;
92 }
93 }
94
95done:
96 return( ret );
97}
98
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +020099#if defined(MBEDTLS_X509_CRT_PARSE_C)
100typedef struct {
101 char buf[512];
102 char *p;
103} verify_print_context;
104
105void verify_print_init( verify_print_context *ctx )
106{
107 memset( ctx, 0, sizeof( verify_print_context ) );
108 ctx->p = ctx->buf;
109}
110
111int verify_print( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
112{
113 int ret;
114 verify_print_context *ctx = (verify_print_context *) data;
115 char *p = ctx->p;
116 size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p;
117 ((void) flags);
118
119 ret = mbedtls_snprintf( p, n, "depth %d - serial ", certificate_depth );
120 MBEDTLS_X509_SAFE_SNPRINTF;
121
122 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
123 MBEDTLS_X509_SAFE_SNPRINTF;
124
125 ret = mbedtls_snprintf( p, n, " - subject " );
126 MBEDTLS_X509_SAFE_SNPRINTF;
127
128 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
129 MBEDTLS_X509_SAFE_SNPRINTF;
130
131 ret = mbedtls_snprintf( p, n, "\n" );
132 MBEDTLS_X509_SAFE_SNPRINTF;
133
134 ctx->p = p;
135
136 return( 0 );
137}
138#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200139/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000140
Paul Bakker33b43f12013-08-20 11:48:36 +0200141/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200143 * END_DEPENDENCIES
144 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200147void x509_cert_info( char *crt_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000148{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000150 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000151 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000154 memset( buf, 0, 2000 );
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
157 res = mbedtls_x509_crt_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000158
159 TEST_ASSERT( res != -1 );
160 TEST_ASSERT( res != -2 );
161
Paul Bakker33b43f12013-08-20 11:48:36 +0200162 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200163
164exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000166}
Paul Bakker33b43f12013-08-20 11:48:36 +0200167/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
170void mbedtls_x509_crl_info( char *crl_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000171{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000173 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000174 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000177 memset( buf, 0, 2000 );
178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
180 res = mbedtls_x509_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000181
182 TEST_ASSERT( res != -1 );
183 TEST_ASSERT( res != -2 );
184
Paul Bakker33b43f12013-08-20 11:48:36 +0200185 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200186
187exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000189}
Paul Bakker33b43f12013-08-20 11:48:36 +0200190/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000191
Andres AGa39db392016-12-08 17:10:38 +0000192/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
193void mbedtls_x509_crl_parse( char *crl_file, int result )
194{
195 mbedtls_x509_crl crl;
196 char buf[2000];
197
198 mbedtls_x509_crl_init( &crl );
199 memset( buf, 0, 2000 );
200
201 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == result );
202
203exit:
204 mbedtls_x509_crl_free( &crl );
205}
206/* END_CASE */
207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C */
209void mbedtls_x509_csr_info( char *csr_file, char *result_str )
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100210{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100212 char buf[2000];
213 int res;
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100216 memset( buf, 0, 2000 );
217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 TEST_ASSERT( mbedtls_x509_csr_parse_file( &csr, csr_file ) == 0 );
219 res = mbedtls_x509_csr_info( buf, 2000, "", &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100220
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100221 TEST_ASSERT( res != -1 );
222 TEST_ASSERT( res != -2 );
223
224 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200225
226exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100228}
229/* END_CASE */
230
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100231/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
232void x509_verify_info( int flags, char *prefix, char *result_str )
233{
234 char buf[2000];
235 int res;
236
237 memset( buf, 0, sizeof( buf ) );
238
239 res = mbedtls_x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
240
241 TEST_ASSERT( res >= 0 );
242
243 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
244}
245/* END_CASE */
246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200248void x509_verify( char *crt_file, char *ca_file, char *crl_file,
249 char *cn_name_str, int result, int flags_result,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200250 char *profile_str,
Paul Bakker33b43f12013-08-20 11:48:36 +0200251 char *verify_callback )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000252{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_x509_crt crt;
254 mbedtls_x509_crt ca;
255 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200256 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000257 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200258 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200259 char * cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200260 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_x509_crt_init( &crt );
263 mbedtls_x509_crt_init( &ca );
264 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000265
Paul Bakker33b43f12013-08-20 11:48:36 +0200266 if( strcmp( cn_name_str, "NULL" ) != 0 )
267 cn_name = cn_name_str;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200268
Gilles Peskineef86ab22017-05-05 18:59:02 +0200269 if( strcmp( profile_str, "default" ) == 0 )
270 profile = &mbedtls_x509_crt_profile_default;
271 else if( strcmp( profile_str, "compat" ) == 0 )
272 profile = &compat_profile;
273 else
274 TEST_ASSERT( "Unknown algorithm profile" == 0 );
275
Paul Bakker33b43f12013-08-20 11:48:36 +0200276 if( strcmp( verify_callback, "NULL" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200277 f_vrfy = NULL;
Paul Bakker33b43f12013-08-20 11:48:36 +0200278 else if( strcmp( verify_callback, "verify_none" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200279 f_vrfy = verify_none;
Paul Bakker33b43f12013-08-20 11:48:36 +0200280 else if( strcmp( verify_callback, "verify_all" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200281 f_vrfy = verify_all;
282 else
283 TEST_ASSERT( "No known verify callback selected" == 0 );
284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
286 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
287 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000288
Gilles Peskineef86ab22017-05-05 18:59:02 +0200289 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 +0200290
Paul Bakkerbd51b262014-07-10 15:26:12 +0200291 TEST_ASSERT( res == ( result ) );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200292 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200293
294exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_x509_crt_free( &crt );
296 mbedtls_x509_crt_free( &ca );
297 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000298}
Paul Bakker33b43f12013-08-20 11:48:36 +0200299/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200302void x509_verify_callback( char *crt_file, char *ca_file,
303 int exp_ret, char *exp_vrfy_out )
304{
305 int ret;
306 mbedtls_x509_crt crt;
307 mbedtls_x509_crt ca;
308 uint32_t flags = 0;
309 verify_print_context vrfy_ctx;
310
311 mbedtls_x509_crt_init( &crt );
312 mbedtls_x509_crt_init( &ca );
313 verify_print_init( &vrfy_ctx );
314
315 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
316 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
317
Gilles Peskineef86ab22017-05-05 18:59:02 +0200318 ret = mbedtls_x509_crt_verify_with_profile( &crt, &ca, NULL,
319 &compat_profile,
320 NULL, &flags,
321 verify_print, &vrfy_ctx );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200322
323 TEST_ASSERT( ret == exp_ret );
324 TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
325
326exit:
327 mbedtls_x509_crt_free( &crt );
328 mbedtls_x509_crt_free( &ca );
329}
330/* END_CASE */
331
332/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333void mbedtls_x509_dn_gets( char *crt_file, char *entity, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000334{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000336 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200337 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000340 memset( buf, 0, 2000 );
341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200343 if( strcmp( entity, "subject" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 res = mbedtls_x509_dn_gets( buf, 2000, &crt.subject );
Paul Bakker33b43f12013-08-20 11:48:36 +0200345 else if( strcmp( entity, "issuer" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 res = mbedtls_x509_dn_gets( buf, 2000, &crt.issuer );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200347 else
348 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000349
350 TEST_ASSERT( res != -1 );
351 TEST_ASSERT( res != -2 );
352
Paul Bakker33b43f12013-08-20 11:48:36 +0200353 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200354
355exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000357}
Paul Bakker33b43f12013-08-20 11:48:36 +0200358/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100361void mbedtls_x509_time_is_past( char *crt_file, char *entity, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000362{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200368
Paul Bakker33b43f12013-08-20 11:48:36 +0200369 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100370 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_from ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200371 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100372 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_to ) == result );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200373 else
374 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000375
Paul Bakkerbd51b262014-07-10 15:26:12 +0200376exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000378}
Paul Bakker33b43f12013-08-20 11:48:36 +0200379/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100382void mbedtls_x509_time_is_future( char *crt_file, char *entity, int result )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100383{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100389
390 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100391 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_from ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100392 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100393 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_to ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100394 else
395 TEST_ASSERT( "Unknown entity" == 0 );
396
Paul Bakkerbd51b262014-07-10 15:26:12 +0200397exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100399}
400/* END_CASE */
401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Paul Bakker5a5fa922014-09-26 14:53:04 +0200403void x509parse_crt_file( char *crt_file, int result )
404{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +0200406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_x509_crt_init( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200410
411exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_x509_crt_free( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200413}
414/* END_CASE */
415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200417void x509parse_crt( char *crt_data, char *result_str, int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000418{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_x509_crt crt;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000420 unsigned char buf[2000];
421 unsigned char output[2000];
422 int data_len, res;
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_x509_crt_init( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000425 memset( buf, 0, 2000 );
426 memset( output, 0, 2000 );
427
Paul Bakker33b43f12013-08-20 11:48:36 +0200428 data_len = unhexify( buf, crt_data );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 TEST_ASSERT( mbedtls_x509_crt_parse( &crt, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200431 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200434
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000435 TEST_ASSERT( res != -1 );
436 TEST_ASSERT( res != -2 );
437
Paul Bakker33b43f12013-08-20 11:48:36 +0200438 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000439 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000440
Paul Bakkerbd51b262014-07-10 15:26:12 +0200441exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000443}
Paul Bakker33b43f12013-08-20 11:48:36 +0200444/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200447void x509parse_crl( char *crl_data, char *result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000448{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000450 unsigned char buf[2000];
451 unsigned char output[2000];
452 int data_len, res;
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000455 memset( buf, 0, 2000 );
456 memset( output, 0, 2000 );
457
Paul Bakker33b43f12013-08-20 11:48:36 +0200458 data_len = unhexify( buf, crl_data );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200461 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200464
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000465 TEST_ASSERT( res != -1 );
466 TEST_ASSERT( res != -2 );
467
Paul Bakker33b43f12013-08-20 11:48:36 +0200468 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000469 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000470
Paul Bakkerbd51b262014-07-10 15:26:12 +0200471exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000473}
Paul Bakker33b43f12013-08-20 11:48:36 +0200474/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */
477void mbedtls_x509_csr_parse( char *csr_der_hex, char *ref_out, int ref_ret )
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200478{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 mbedtls_x509_csr csr;
Paul Bakkerbd51b262014-07-10 15:26:12 +0200480 unsigned char *csr_der = NULL;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200481 char my_out[1000];
482 size_t csr_der_len;
483 int my_ret;
484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200486 memset( my_out, 0, sizeof( my_out ) );
487 csr_der = unhexify_alloc( csr_der_hex, &csr_der_len );
488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der, csr_der_len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200490 TEST_ASSERT( my_ret == ref_ret );
491
492 if( ref_ret == 0 )
493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200495 TEST_ASSERT( my_out_len == strlen( ref_out ) );
496 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
497 }
498
Paul Bakkerbd51b262014-07-10 15:26:12 +0200499exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_x509_csr_free( &csr );
501 mbedtls_free( csr_der );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200502}
503/* END_CASE */
504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
506void mbedtls_x509_crt_parse_path( char *crt_path, int ret, int nb_crt )
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100507{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100509 int i;
510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_x509_crt_init( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100514
515 /* Check how many certs we got */
516 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
517 if( cur->raw.p != NULL )
518 i++;
519
520 TEST_ASSERT( i == nb_crt );
521
Paul Bakkerbd51b262014-07-10 15:26:12 +0200522exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100524}
525/* END_CASE */
526
Janos Follath822b2c32015-10-11 10:25:22 +0200527/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +0200528void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int,
529 int ret_chk, int flags_chk )
530{
531 char file_buf[128];
532 int ret;
533 uint32_t flags;
534 mbedtls_x509_crt trusted, chain;
535
536 /*
537 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
538 * with NN.crt signed by NN-1.crt
539 */
540
541 mbedtls_x509_crt_init( &trusted );
542 mbedtls_x509_crt_init( &chain );
543
544 /* Load trusted root */
545 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 );
546
547 /* Load a chain with nb_int intermediates (from 01 to nb_int),
548 * plus one "end-entity" cert (nb_int + 1) */
549 ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
550 nb_int + 1 );
551 TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf );
552 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 );
553
554 /* Try to verify that chain */
555 ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags,
556 NULL, NULL );
557 TEST_ASSERT( ret == ret_chk );
558 TEST_ASSERT( flags == (uint32_t) flags_chk );
559
560exit:
561 mbedtls_x509_crt_free( &chain );
562 mbedtls_x509_crt_free( &trusted );
563}
564/* END_CASE */
565
566/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200567void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca,
568 int flags_result, int result,
569 char *profile_name )
Janos Follath822b2c32015-10-11 10:25:22 +0200570{
571 char* act;
572 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200573 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100574 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200575 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +0200576
Janos Follath822b2c32015-10-11 10:25:22 +0200577 mbedtls_x509_crt_init( &chain );
578 mbedtls_x509_crt_init( &trusted );
579
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900580 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100581 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
582 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
Janos Follath822b2c32015-10-11 10:25:22 +0200583
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200584 if( strcmp(profile_name, "") == 0 )
585 profile = &mbedtls_x509_crt_profile_default;
586 else if( strcmp(profile_name, "next") == 0 )
587 profile = &mbedtls_x509_crt_profile_next;
588 else if( strcmp(profile_name, "suiteb") == 0 )
589 profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200590 else if( strcmp(profile_name, "rsa3072") == 0 )
591 profile = &profile_rsa3072;
592 else if( strcmp(profile_name, "sha512") == 0 )
593 profile = &profile_sha512;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200594
595 res = mbedtls_x509_crt_verify_with_profile( &chain, &trusted, NULL, profile,
596 NULL, &flags, NULL, NULL );
Janos Follathef4f2582015-10-11 16:17:27 +0200597
598 TEST_ASSERT( res == ( result ) );
599 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Janos Follath822b2c32015-10-11 10:25:22 +0200600
601exit:
602 mbedtls_x509_crt_free( &trusted );
603 mbedtls_x509_crt_free( &chain );
604}
605/* END_CASE */
606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100608void x509_oid_desc( char *oid_str, char *ref_desc )
609{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000611 const char *desc = NULL;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100612 unsigned char buf[20];
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000613 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100614
615 memset( buf, 0, sizeof buf );
616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 oid.tag = MBEDTLS_ASN1_OID;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100618 oid.len = unhexify( buf, oid_str );
619 oid.p = buf;
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100622
623 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000624 {
625 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100626 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000627 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100628 else
629 {
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000630 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100631 TEST_ASSERT( desc != NULL );
632 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
633 }
634}
635/* END_CASE */
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100638void x509_oid_numstr( char *oid_str, char *numstr, int blen, int ret )
639{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100641 unsigned char oid_buf[20];
642 char num_buf[100];
643
644 memset( oid_buf, 0x00, sizeof oid_buf );
645 memset( num_buf, 0x2a, sizeof num_buf );
646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 oid.tag = MBEDTLS_ASN1_OID;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100648 oid.len = unhexify( oid_buf, oid_str );
649 oid.p = oid_buf;
650
651 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100654
655 if( ret >= 0 )
656 {
657 TEST_ASSERT( num_buf[ret] == 0 );
658 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
659 }
660}
661/* END_CASE */
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663/* 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 +0200664void x509_check_key_usage( char *crt_file, int usage, int ret )
665{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200673
Paul Bakkerbd51b262014-07-10 15:26:12 +0200674exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200676}
677/* END_CASE */
678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679/* 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 +0200680void x509_check_extended_key_usage( char *crt_file, char *usage_hex, int ret )
681{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200683 char oid[50];
684 size_t len;
685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200687
688 len = unhexify( (unsigned char *) oid, usage_hex );
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 TEST_ASSERT( mbedtls_x509_crt_check_extended_key_usage( &crt, oid, len ) == ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200693
Paul Bakkerbd51b262014-07-10 15:26:12 +0200694exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200696}
697/* END_CASE */
698
Andres AG4b76aec2016-09-23 13:16:02 +0100699/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
700void x509_get_time( int tag, char *time_str, int ret,
701 int year, int mon, int day,
702 int hour, int min, int sec )
703{
704 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +0000705 unsigned char buf[21];
Andres AG4b76aec2016-09-23 13:16:02 +0100706 unsigned char* start = buf;
707 unsigned char* end = buf;
708
709 memset( &time, 0x00, sizeof( time ) );
710 *end = (unsigned char)tag; end++;
Janos Follathea7054a2017-02-08 14:13:02 +0000711 *end = strlen( time_str );
712 TEST_ASSERT( *end < 20 );
Andres AG4b76aec2016-09-23 13:16:02 +0100713 end++;
714 memcpy( end, time_str, (size_t)*(end - 1) );
715 end += *(end - 1);
716
717 TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
718 if( ret == 0 )
719 {
720 TEST_ASSERT( year == time.year );
721 TEST_ASSERT( mon == time.mon );
722 TEST_ASSERT( day == time.day );
723 TEST_ASSERT( hour == time.hour );
724 TEST_ASSERT( min == time.min );
725 TEST_ASSERT( sec == time.sec );
726 }
727}
728/* END_CASE */
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200731void x509_parse_rsassa_pss_params( char *hex_params, int params_tag,
732 int ref_msg_md, int ref_mgf_md,
733 int ref_salt_len, int ref_ret )
734{
735 int my_ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_x509_buf params;
737 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200738 int my_salt_len;
739
740 params.p = unhexify_alloc( hex_params, &params.len );
741 params.tag = params_tag;
742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 my_ret = mbedtls_x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200744 &my_salt_len );
745
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200746 TEST_ASSERT( my_ret == ref_ret );
747
748 if( ref_ret == 0 )
749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md );
751 TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200752 TEST_ASSERT( my_salt_len == ref_salt_len );
753 }
754
Paul Bakkerbd51b262014-07-10 15:26:12 +0200755exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_free( params.p );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200757}
758/* END_CASE */
759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200761void x509_selftest()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000762{
Andres AG93012e82016-09-09 09:10:28 +0100763 TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000764}
Paul Bakker33b43f12013-08-20 11:48:36 +0200765/* END_CASE */