blob: 0dfdd61c2286fcbde7e8227709c0a2b2040f9e39 [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 Butcherefdfeeb2017-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é-Gonnarde6ef16f2015-05-11 19:54:43 +020031int verify_none( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000032{
Paul Bakker5a624082011-01-18 16:31:52 +000033 ((void) data);
34 ((void) crt);
35 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010036 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020037
Paul Bakker915275b2012-09-28 07:10:55 +000038 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000039}
40
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020041int verify_all( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000042{
Paul Bakker5a624082011-01-18 16:31:52 +000043 ((void) data);
44 ((void) crt);
45 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000046 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000047
Paul Bakkerb63b0af2011-01-13 17:54:59 +000048 return 0;
49}
50
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +090051/* strsep() not available on Windows */
52char *mystrsep(char **stringp, const char *delim)
53{
54 const char *p;
55 char *ret = *stringp;
56
57 if( *stringp == NULL )
58 return( NULL );
59
60 for( ; ; (*stringp)++ )
61 {
62 if( **stringp == '\0' )
63 {
64 *stringp = NULL;
65 goto done;
66 }
67
68 for( p = delim; *p != '\0'; p++ )
69 if( **stringp == *p )
70 {
71 **stringp = '\0';
72 (*stringp)++;
73 goto done;
74 }
75 }
76
77done:
78 return( ret );
79}
80
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +020081#if defined(MBEDTLS_X509_CRT_PARSE_C)
82typedef struct {
83 char buf[512];
84 char *p;
85} verify_print_context;
86
87void verify_print_init( verify_print_context *ctx )
88{
89 memset( ctx, 0, sizeof( verify_print_context ) );
90 ctx->p = ctx->buf;
91}
92
93int verify_print( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
94{
95 int ret;
96 verify_print_context *ctx = (verify_print_context *) data;
97 char *p = ctx->p;
98 size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p;
99 ((void) flags);
100
101 ret = mbedtls_snprintf( p, n, "depth %d - serial ", certificate_depth );
102 MBEDTLS_X509_SAFE_SNPRINTF;
103
104 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
105 MBEDTLS_X509_SAFE_SNPRINTF;
106
107 ret = mbedtls_snprintf( p, n, " - subject " );
108 MBEDTLS_X509_SAFE_SNPRINTF;
109
110 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
111 MBEDTLS_X509_SAFE_SNPRINTF;
112
113 ret = mbedtls_snprintf( p, n, "\n" );
114 MBEDTLS_X509_SAFE_SNPRINTF;
115
116 ctx->p = p;
117
118 return( 0 );
119}
120#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200121/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000122
Paul Bakker33b43f12013-08-20 11:48:36 +0200123/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200125 * END_DEPENDENCIES
126 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200129void x509_cert_info( char *crt_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000130{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000132 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000133 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000136 memset( buf, 0, 2000 );
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
139 res = mbedtls_x509_crt_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000140
141 TEST_ASSERT( res != -1 );
142 TEST_ASSERT( res != -2 );
143
Paul Bakker33b43f12013-08-20 11:48:36 +0200144 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200145
146exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000148}
Paul Bakker33b43f12013-08-20 11:48:36 +0200149/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
152void mbedtls_x509_crl_info( char *crl_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000153{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000155 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000156 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000159 memset( buf, 0, 2000 );
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
162 res = mbedtls_x509_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000163
164 TEST_ASSERT( res != -1 );
165 TEST_ASSERT( res != -2 );
166
Paul Bakker33b43f12013-08-20 11:48:36 +0200167 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200168
169exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000171}
Paul Bakker33b43f12013-08-20 11:48:36 +0200172/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000173
Andres AGa39db392016-12-08 17:10:38 +0000174/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
175void mbedtls_x509_crl_parse( char *crl_file, int result )
176{
177 mbedtls_x509_crl crl;
178 char buf[2000];
179
180 mbedtls_x509_crl_init( &crl );
181 memset( buf, 0, 2000 );
182
183 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == result );
184
185exit:
186 mbedtls_x509_crl_free( &crl );
187}
188/* END_CASE */
189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C */
191void mbedtls_x509_csr_info( char *csr_file, char *result_str )
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100192{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100194 char buf[2000];
195 int res;
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100198 memset( buf, 0, 2000 );
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 TEST_ASSERT( mbedtls_x509_csr_parse_file( &csr, csr_file ) == 0 );
201 res = mbedtls_x509_csr_info( buf, 2000, "", &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100202
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100203 TEST_ASSERT( res != -1 );
204 TEST_ASSERT( res != -2 );
205
206 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200207
208exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100210}
211/* END_CASE */
212
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100213/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
214void x509_verify_info( int flags, char *prefix, char *result_str )
215{
216 char buf[2000];
217 int res;
218
219 memset( buf, 0, sizeof( buf ) );
220
221 res = mbedtls_x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
222
223 TEST_ASSERT( res >= 0 );
224
225 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
226}
227/* END_CASE */
228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200230void x509_verify( char *crt_file, char *ca_file, char *crl_file,
231 char *cn_name_str, int result, int flags_result,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200232 char *profile_str,
Paul Bakker33b43f12013-08-20 11:48:36 +0200233 char *verify_callback )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000234{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_x509_crt crt;
236 mbedtls_x509_crt ca;
237 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200238 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000239 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200240 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200241 char * cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200242 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_x509_crt_init( &crt );
245 mbedtls_x509_crt_init( &ca );
246 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000247
Paul Bakker33b43f12013-08-20 11:48:36 +0200248 if( strcmp( cn_name_str, "NULL" ) != 0 )
249 cn_name = cn_name_str;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200250
Gilles Peskineef86ab22017-05-05 18:59:02 +0200251 if( strcmp( profile_str, "default" ) == 0 )
252 profile = &mbedtls_x509_crt_profile_default;
253 else if( strcmp( profile_str, "compat" ) == 0 )
254 profile = &compat_profile;
255 else
256 TEST_ASSERT( "Unknown algorithm profile" == 0 );
257
Paul Bakker33b43f12013-08-20 11:48:36 +0200258 if( strcmp( verify_callback, "NULL" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200259 f_vrfy = NULL;
Paul Bakker33b43f12013-08-20 11:48:36 +0200260 else if( strcmp( verify_callback, "verify_none" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200261 f_vrfy = verify_none;
Paul Bakker33b43f12013-08-20 11:48:36 +0200262 else if( strcmp( verify_callback, "verify_all" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200263 f_vrfy = verify_all;
264 else
265 TEST_ASSERT( "No known verify callback selected" == 0 );
266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
268 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
269 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000270
Gilles Peskineef86ab22017-05-05 18:59:02 +0200271 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 +0200272
Paul Bakkerbd51b262014-07-10 15:26:12 +0200273 TEST_ASSERT( res == ( result ) );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200274 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200275
276exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_x509_crt_free( &crt );
278 mbedtls_x509_crt_free( &ca );
279 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000280}
Paul Bakker33b43f12013-08-20 11:48:36 +0200281/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200284void x509_verify_callback( char *crt_file, char *ca_file,
285 int exp_ret, char *exp_vrfy_out )
286{
287 int ret;
288 mbedtls_x509_crt crt;
289 mbedtls_x509_crt ca;
290 uint32_t flags = 0;
291 verify_print_context vrfy_ctx;
292
293 mbedtls_x509_crt_init( &crt );
294 mbedtls_x509_crt_init( &ca );
295 verify_print_init( &vrfy_ctx );
296
297 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
298 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
299
Gilles Peskineef86ab22017-05-05 18:59:02 +0200300 ret = mbedtls_x509_crt_verify_with_profile( &crt, &ca, NULL,
301 &compat_profile,
302 NULL, &flags,
303 verify_print, &vrfy_ctx );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200304
305 TEST_ASSERT( ret == exp_ret );
306 TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
307
308exit:
309 mbedtls_x509_crt_free( &crt );
310 mbedtls_x509_crt_free( &ca );
311}
312/* END_CASE */
313
314/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315void mbedtls_x509_dn_gets( char *crt_file, char *entity, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000316{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000318 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200319 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000322 memset( buf, 0, 2000 );
323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200325 if( strcmp( entity, "subject" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 res = mbedtls_x509_dn_gets( buf, 2000, &crt.subject );
Paul Bakker33b43f12013-08-20 11:48:36 +0200327 else if( strcmp( entity, "issuer" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 res = mbedtls_x509_dn_gets( buf, 2000, &crt.issuer );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200329 else
330 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000331
332 TEST_ASSERT( res != -1 );
333 TEST_ASSERT( res != -2 );
334
Paul Bakker33b43f12013-08-20 11:48:36 +0200335 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200336
337exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000339}
Paul Bakker33b43f12013-08-20 11:48:36 +0200340/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100343void mbedtls_x509_time_is_past( char *crt_file, char *entity, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000344{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200350
Paul Bakker33b43f12013-08-20 11:48:36 +0200351 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100352 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_from ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200353 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100354 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_to ) == result );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200355 else
356 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000357
Paul Bakkerbd51b262014-07-10 15:26:12 +0200358exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000360}
Paul Bakker33b43f12013-08-20 11:48:36 +0200361/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100364void mbedtls_x509_time_is_future( char *crt_file, char *entity, int result )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100365{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100371
372 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100373 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_from ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100374 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100375 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_to ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100376 else
377 TEST_ASSERT( "Unknown entity" == 0 );
378
Paul Bakkerbd51b262014-07-10 15:26:12 +0200379exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100381}
382/* END_CASE */
383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Paul Bakker5a5fa922014-09-26 14:53:04 +0200385void x509parse_crt_file( char *crt_file, int result )
386{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +0200388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_x509_crt_init( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200392
393exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_x509_crt_free( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200395}
396/* END_CASE */
397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200399void x509parse_crt( char *crt_data, char *result_str, int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000400{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_x509_crt crt;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000402 unsigned char buf[2000];
403 unsigned char output[2000];
404 int data_len, res;
405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 mbedtls_x509_crt_init( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000407 memset( buf, 0, 2000 );
408 memset( output, 0, 2000 );
409
Paul Bakker33b43f12013-08-20 11:48:36 +0200410 data_len = unhexify( buf, crt_data );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 TEST_ASSERT( mbedtls_x509_crt_parse( &crt, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200413 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200416
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000417 TEST_ASSERT( res != -1 );
418 TEST_ASSERT( res != -2 );
419
Paul Bakker33b43f12013-08-20 11:48:36 +0200420 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000421 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000422
Paul Bakkerbd51b262014-07-10 15:26:12 +0200423exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000425}
Paul Bakker33b43f12013-08-20 11:48:36 +0200426/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200429void x509parse_crl( char *crl_data, char *result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000430{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000432 unsigned char buf[2000];
433 unsigned char output[2000];
434 int data_len, res;
435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000437 memset( buf, 0, 2000 );
438 memset( output, 0, 2000 );
439
Paul Bakker33b43f12013-08-20 11:48:36 +0200440 data_len = unhexify( buf, crl_data );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200443 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200446
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000447 TEST_ASSERT( res != -1 );
448 TEST_ASSERT( res != -2 );
449
Paul Bakker33b43f12013-08-20 11:48:36 +0200450 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000451 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000452
Paul Bakkerbd51b262014-07-10 15:26:12 +0200453exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000455}
Paul Bakker33b43f12013-08-20 11:48:36 +0200456/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */
459void mbedtls_x509_csr_parse( char *csr_der_hex, char *ref_out, int ref_ret )
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200460{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 mbedtls_x509_csr csr;
Paul Bakkerbd51b262014-07-10 15:26:12 +0200462 unsigned char *csr_der = NULL;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200463 char my_out[1000];
464 size_t csr_der_len;
465 int my_ret;
466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200468 memset( my_out, 0, sizeof( my_out ) );
469 csr_der = unhexify_alloc( csr_der_hex, &csr_der_len );
470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der, csr_der_len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200472 TEST_ASSERT( my_ret == ref_ret );
473
474 if( ref_ret == 0 )
475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200477 TEST_ASSERT( my_out_len == strlen( ref_out ) );
478 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
479 }
480
Paul Bakkerbd51b262014-07-10 15:26:12 +0200481exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_x509_csr_free( &csr );
483 mbedtls_free( csr_der );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200484}
485/* END_CASE */
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
488void mbedtls_x509_crt_parse_path( char *crt_path, int ret, int nb_crt )
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100489{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100491 int i;
492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_x509_crt_init( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100496
497 /* Check how many certs we got */
498 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
499 if( cur->raw.p != NULL )
500 i++;
501
502 TEST_ASSERT( i == nb_crt );
503
Paul Bakkerbd51b262014-07-10 15:26:12 +0200504exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100506}
507/* END_CASE */
508
Janos Follath822b2c32015-10-11 10:25:22 +0200509/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +0200510void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int,
511 int ret_chk, int flags_chk )
512{
513 char file_buf[128];
514 int ret;
515 uint32_t flags;
516 mbedtls_x509_crt trusted, chain;
517
518 /*
519 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
520 * with NN.crt signed by NN-1.crt
521 */
522
523 mbedtls_x509_crt_init( &trusted );
524 mbedtls_x509_crt_init( &chain );
525
526 /* Load trusted root */
527 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 );
528
529 /* Load a chain with nb_int intermediates (from 01 to nb_int),
530 * plus one "end-entity" cert (nb_int + 1) */
531 ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
532 nb_int + 1 );
533 TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf );
534 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 );
535
536 /* Try to verify that chain */
537 ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags,
538 NULL, NULL );
539 TEST_ASSERT( ret == ret_chk );
540 TEST_ASSERT( flags == (uint32_t) flags_chk );
541
542exit:
543 mbedtls_x509_crt_free( &chain );
544 mbedtls_x509_crt_free( &trusted );
545}
546/* END_CASE */
547
548/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Janos Follathef4f2582015-10-11 16:17:27 +0200549void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca, int flags_result )
Janos Follath822b2c32015-10-11 10:25:22 +0200550{
551 char* act;
552 uint32_t flags;
Janos Follathef4f2582015-10-11 16:17:27 +0200553 int result, res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100554 mbedtls_x509_crt trusted, chain;
Janos Follath822b2c32015-10-11 10:25:22 +0200555
Janos Follathef4f2582015-10-11 16:17:27 +0200556 result= flags_result?MBEDTLS_ERR_X509_CERT_VERIFY_FAILED:0;
557
Janos Follath822b2c32015-10-11 10:25:22 +0200558 mbedtls_x509_crt_init( &chain );
559 mbedtls_x509_crt_init( &trusted );
560
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900561 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100562 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
563 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
Janos Follath822b2c32015-10-11 10:25:22 +0200564
565 res = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags, NULL, NULL );
Janos Follathef4f2582015-10-11 16:17:27 +0200566
567 TEST_ASSERT( res == ( result ) );
568 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Janos Follath822b2c32015-10-11 10:25:22 +0200569
570exit:
571 mbedtls_x509_crt_free( &trusted );
572 mbedtls_x509_crt_free( &chain );
573}
574/* END_CASE */
575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100577void x509_oid_desc( char *oid_str, char *ref_desc )
578{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000580 const char *desc = NULL;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100581 unsigned char buf[20];
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000582 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100583
584 memset( buf, 0, sizeof buf );
585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 oid.tag = MBEDTLS_ASN1_OID;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100587 oid.len = unhexify( buf, oid_str );
588 oid.p = buf;
589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100591
592 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000593 {
594 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100595 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000596 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100597 else
598 {
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000599 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100600 TEST_ASSERT( desc != NULL );
601 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
602 }
603}
604/* END_CASE */
605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100607void x509_oid_numstr( char *oid_str, char *numstr, int blen, int ret )
608{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100610 unsigned char oid_buf[20];
611 char num_buf[100];
612
613 memset( oid_buf, 0x00, sizeof oid_buf );
614 memset( num_buf, 0x2a, sizeof num_buf );
615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 oid.tag = MBEDTLS_ASN1_OID;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100617 oid.len = unhexify( oid_buf, oid_str );
618 oid.p = oid_buf;
619
620 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100623
624 if( ret >= 0 )
625 {
626 TEST_ASSERT( num_buf[ret] == 0 );
627 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
628 }
629}
630/* END_CASE */
631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632/* 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 +0200633void x509_check_key_usage( char *crt_file, int usage, int ret )
634{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200642
Paul Bakkerbd51b262014-07-10 15:26:12 +0200643exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200645}
646/* END_CASE */
647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648/* 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 +0200649void x509_check_extended_key_usage( char *crt_file, char *usage_hex, int ret )
650{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200652 char oid[50];
653 size_t len;
654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200656
657 len = unhexify( (unsigned char *) oid, usage_hex );
658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 TEST_ASSERT( mbedtls_x509_crt_check_extended_key_usage( &crt, oid, len ) == ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200662
Paul Bakkerbd51b262014-07-10 15:26:12 +0200663exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200665}
666/* END_CASE */
667
Andres AG4b76aec2016-09-23 13:16:02 +0100668/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
669void x509_get_time( int tag, char *time_str, int ret,
670 int year, int mon, int day,
671 int hour, int min, int sec )
672{
673 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +0000674 unsigned char buf[21];
Andres AG4b76aec2016-09-23 13:16:02 +0100675 unsigned char* start = buf;
676 unsigned char* end = buf;
677
678 memset( &time, 0x00, sizeof( time ) );
679 *end = (unsigned char)tag; end++;
Janos Follathea7054a2017-02-08 14:13:02 +0000680 *end = strlen( time_str );
681 TEST_ASSERT( *end < 20 );
Andres AG4b76aec2016-09-23 13:16:02 +0100682 end++;
683 memcpy( end, time_str, (size_t)*(end - 1) );
684 end += *(end - 1);
685
686 TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
687 if( ret == 0 )
688 {
689 TEST_ASSERT( year == time.year );
690 TEST_ASSERT( mon == time.mon );
691 TEST_ASSERT( day == time.day );
692 TEST_ASSERT( hour == time.hour );
693 TEST_ASSERT( min == time.min );
694 TEST_ASSERT( sec == time.sec );
695 }
696}
697/* END_CASE */
698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200700void x509_parse_rsassa_pss_params( char *hex_params, int params_tag,
701 int ref_msg_md, int ref_mgf_md,
702 int ref_salt_len, int ref_ret )
703{
704 int my_ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_x509_buf params;
706 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200707 int my_salt_len;
708
709 params.p = unhexify_alloc( hex_params, &params.len );
710 params.tag = params_tag;
711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 my_ret = mbedtls_x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200713 &my_salt_len );
714
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200715 TEST_ASSERT( my_ret == ref_ret );
716
717 if( ref_ret == 0 )
718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md );
720 TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200721 TEST_ASSERT( my_salt_len == ref_salt_len );
722 }
723
Paul Bakkerbd51b262014-07-10 15:26:12 +0200724exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_free( params.p );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200726}
727/* END_CASE */
728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200730void x509_selftest()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000731{
Andres AG93012e82016-09-09 09:10:28 +0100732 TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000733}
Paul Bakker33b43f12013-08-20 11:48:36 +0200734/* END_CASE */