blob: a9f7ceed416b36bae9f69249457c58f6d4a25542 [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
Gilles Peskineef86ab22017-05-05 18:59:02 +020010/* Profile for backward compatibility. Allows SHA-1, unlike the default
11 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020012const mbedtls_x509_crt_profile compat_profile =
13{
14 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
15 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) |
16 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
17 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
18 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
19 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
20 0xFFFFFFF, /* Any PK alg */
21 0xFFFFFFF, /* Any curve */
22 1024,
23};
24
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020025int verify_none( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000026{
Paul Bakker5a624082011-01-18 16:31:52 +000027 ((void) data);
28 ((void) crt);
29 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010030 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020031
Paul Bakker915275b2012-09-28 07:10:55 +000032 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000033}
34
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020035int verify_all( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000036{
Paul Bakker5a624082011-01-18 16:31:52 +000037 ((void) data);
38 ((void) crt);
39 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000040 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000041
Paul Bakkerb63b0af2011-01-13 17:54:59 +000042 return 0;
43}
44
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +090045/* strsep() not available on Windows */
46char *mystrsep(char **stringp, const char *delim)
47{
48 const char *p;
49 char *ret = *stringp;
50
51 if( *stringp == NULL )
52 return( NULL );
53
54 for( ; ; (*stringp)++ )
55 {
56 if( **stringp == '\0' )
57 {
58 *stringp = NULL;
59 goto done;
60 }
61
62 for( p = delim; *p != '\0'; p++ )
63 if( **stringp == *p )
64 {
65 **stringp = '\0';
66 (*stringp)++;
67 goto done;
68 }
69 }
70
71done:
72 return( ret );
73}
74
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +020075#if defined(MBEDTLS_X509_CRT_PARSE_C)
76typedef struct {
77 char buf[512];
78 char *p;
79} verify_print_context;
80
81void verify_print_init( verify_print_context *ctx )
82{
83 memset( ctx, 0, sizeof( verify_print_context ) );
84 ctx->p = ctx->buf;
85}
86
87int verify_print( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
88{
89 int ret;
90 verify_print_context *ctx = (verify_print_context *) data;
91 char *p = ctx->p;
92 size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p;
93 ((void) flags);
94
95 ret = mbedtls_snprintf( p, n, "depth %d - serial ", certificate_depth );
96 MBEDTLS_X509_SAFE_SNPRINTF;
97
98 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
99 MBEDTLS_X509_SAFE_SNPRINTF;
100
101 ret = mbedtls_snprintf( p, n, " - subject " );
102 MBEDTLS_X509_SAFE_SNPRINTF;
103
104 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
105 MBEDTLS_X509_SAFE_SNPRINTF;
106
107 ret = mbedtls_snprintf( p, n, "\n" );
108 MBEDTLS_X509_SAFE_SNPRINTF;
109
110 ctx->p = p;
111
112 return( 0 );
113}
114#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200115/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000116
Paul Bakker33b43f12013-08-20 11:48:36 +0200117/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200119 * END_DEPENDENCIES
120 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200123void x509_cert_info( char *crt_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000124{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000126 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000127 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000130 memset( buf, 0, 2000 );
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
133 res = mbedtls_x509_crt_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000134
135 TEST_ASSERT( res != -1 );
136 TEST_ASSERT( res != -2 );
137
Paul Bakker33b43f12013-08-20 11:48:36 +0200138 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200139
140exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000142}
Paul Bakker33b43f12013-08-20 11:48:36 +0200143/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
146void mbedtls_x509_crl_info( char *crl_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000147{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000149 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000150 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000153 memset( buf, 0, 2000 );
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
156 res = mbedtls_x509_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000157
158 TEST_ASSERT( res != -1 );
159 TEST_ASSERT( res != -2 );
160
Paul Bakker33b43f12013-08-20 11:48:36 +0200161 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200162
163exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000165}
Paul Bakker33b43f12013-08-20 11:48:36 +0200166/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000167
Andres AGa39db392016-12-08 17:10:38 +0000168/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
169void mbedtls_x509_crl_parse( char *crl_file, int result )
170{
171 mbedtls_x509_crl crl;
172 char buf[2000];
173
174 mbedtls_x509_crl_init( &crl );
175 memset( buf, 0, 2000 );
176
177 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == result );
178
179exit:
180 mbedtls_x509_crl_free( &crl );
181}
182/* END_CASE */
183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C */
185void mbedtls_x509_csr_info( char *csr_file, char *result_str )
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100186{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100188 char buf[2000];
189 int res;
190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100192 memset( buf, 0, 2000 );
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 TEST_ASSERT( mbedtls_x509_csr_parse_file( &csr, csr_file ) == 0 );
195 res = mbedtls_x509_csr_info( buf, 2000, "", &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100196
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100197 TEST_ASSERT( res != -1 );
198 TEST_ASSERT( res != -2 );
199
200 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200201
202exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100204}
205/* END_CASE */
206
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100207/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
208void x509_verify_info( int flags, char *prefix, char *result_str )
209{
210 char buf[2000];
211 int res;
212
213 memset( buf, 0, sizeof( buf ) );
214
215 res = mbedtls_x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
216
217 TEST_ASSERT( res >= 0 );
218
219 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
220}
221/* END_CASE */
222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200224void x509_verify( char *crt_file, char *ca_file, char *crl_file,
225 char *cn_name_str, int result, int flags_result,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200226 char *profile_str,
Paul Bakker33b43f12013-08-20 11:48:36 +0200227 char *verify_callback )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000228{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_x509_crt crt;
230 mbedtls_x509_crt ca;
231 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200232 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000233 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200234 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200235 char * cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200236 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_x509_crt_init( &crt );
239 mbedtls_x509_crt_init( &ca );
240 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000241
Paul Bakker33b43f12013-08-20 11:48:36 +0200242 if( strcmp( cn_name_str, "NULL" ) != 0 )
243 cn_name = cn_name_str;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200244
Gilles Peskineef86ab22017-05-05 18:59:02 +0200245 if( strcmp( profile_str, "default" ) == 0 )
246 profile = &mbedtls_x509_crt_profile_default;
247 else if( strcmp( profile_str, "compat" ) == 0 )
248 profile = &compat_profile;
249 else
250 TEST_ASSERT( "Unknown algorithm profile" == 0 );
251
Paul Bakker33b43f12013-08-20 11:48:36 +0200252 if( strcmp( verify_callback, "NULL" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200253 f_vrfy = NULL;
Paul Bakker33b43f12013-08-20 11:48:36 +0200254 else if( strcmp( verify_callback, "verify_none" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200255 f_vrfy = verify_none;
Paul Bakker33b43f12013-08-20 11:48:36 +0200256 else if( strcmp( verify_callback, "verify_all" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200257 f_vrfy = verify_all;
258 else
259 TEST_ASSERT( "No known verify callback selected" == 0 );
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
262 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
263 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000264
Gilles Peskineef86ab22017-05-05 18:59:02 +0200265 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 +0200266
Paul Bakkerbd51b262014-07-10 15:26:12 +0200267 TEST_ASSERT( res == ( result ) );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200268 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200269
270exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_x509_crt_free( &crt );
272 mbedtls_x509_crt_free( &ca );
273 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000274}
Paul Bakker33b43f12013-08-20 11:48:36 +0200275/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200278void x509_verify_callback( char *crt_file, char *ca_file,
279 int exp_ret, char *exp_vrfy_out )
280{
281 int ret;
282 mbedtls_x509_crt crt;
283 mbedtls_x509_crt ca;
284 uint32_t flags = 0;
285 verify_print_context vrfy_ctx;
286
287 mbedtls_x509_crt_init( &crt );
288 mbedtls_x509_crt_init( &ca );
289 verify_print_init( &vrfy_ctx );
290
291 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
292 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
293
Gilles Peskineef86ab22017-05-05 18:59:02 +0200294 ret = mbedtls_x509_crt_verify_with_profile( &crt, &ca, NULL,
295 &compat_profile,
296 NULL, &flags,
297 verify_print, &vrfy_ctx );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200298
299 TEST_ASSERT( ret == exp_ret );
300 TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
301
302exit:
303 mbedtls_x509_crt_free( &crt );
304 mbedtls_x509_crt_free( &ca );
305}
306/* END_CASE */
307
308/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309void mbedtls_x509_dn_gets( char *crt_file, char *entity, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000310{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000312 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200313 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000316 memset( buf, 0, 2000 );
317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200319 if( strcmp( entity, "subject" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 res = mbedtls_x509_dn_gets( buf, 2000, &crt.subject );
Paul Bakker33b43f12013-08-20 11:48:36 +0200321 else if( strcmp( entity, "issuer" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 res = mbedtls_x509_dn_gets( buf, 2000, &crt.issuer );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200323 else
324 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000325
326 TEST_ASSERT( res != -1 );
327 TEST_ASSERT( res != -2 );
328
Paul Bakker33b43f12013-08-20 11:48:36 +0200329 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200330
331exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000333}
Paul Bakker33b43f12013-08-20 11:48:36 +0200334/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100337void mbedtls_x509_time_is_past( char *crt_file, char *entity, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000338{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200344
Paul Bakker33b43f12013-08-20 11:48:36 +0200345 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100346 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_from ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200347 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100348 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_to ) == result );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200349 else
350 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000351
Paul Bakkerbd51b262014-07-10 15:26:12 +0200352exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000354}
Paul Bakker33b43f12013-08-20 11:48:36 +0200355/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100358void mbedtls_x509_time_is_future( char *crt_file, char *entity, int result )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100359{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100365
366 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100367 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_from ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100368 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100369 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_to ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100370 else
371 TEST_ASSERT( "Unknown entity" == 0 );
372
Paul Bakkerbd51b262014-07-10 15:26:12 +0200373exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100375}
376/* END_CASE */
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Paul Bakker5a5fa922014-09-26 14:53:04 +0200379void x509parse_crt_file( char *crt_file, int result )
380{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +0200382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_x509_crt_init( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200386
387exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_x509_crt_free( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200389}
390/* END_CASE */
391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200393void x509parse_crt( char *crt_data, char *result_str, int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000394{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_x509_crt crt;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000396 unsigned char buf[2000];
397 unsigned char output[2000];
398 int data_len, res;
399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_x509_crt_init( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000401 memset( buf, 0, 2000 );
402 memset( output, 0, 2000 );
403
Paul Bakker33b43f12013-08-20 11:48:36 +0200404 data_len = unhexify( buf, crt_data );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 TEST_ASSERT( mbedtls_x509_crt_parse( &crt, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200407 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200410
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000411 TEST_ASSERT( res != -1 );
412 TEST_ASSERT( res != -2 );
413
Paul Bakker33b43f12013-08-20 11:48:36 +0200414 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000415 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000416
Paul Bakkerbd51b262014-07-10 15:26:12 +0200417exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 mbedtls_x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000419}
Paul Bakker33b43f12013-08-20 11:48:36 +0200420/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200423void x509parse_crl( char *crl_data, char *result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000424{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000426 unsigned char buf[2000];
427 unsigned char output[2000];
428 int data_len, res;
429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000431 memset( buf, 0, 2000 );
432 memset( output, 0, 2000 );
433
Paul Bakker33b43f12013-08-20 11:48:36 +0200434 data_len = unhexify( buf, crl_data );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200437 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200440
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000441 TEST_ASSERT( res != -1 );
442 TEST_ASSERT( res != -2 );
443
Paul Bakker33b43f12013-08-20 11:48:36 +0200444 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000445 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000446
Paul Bakkerbd51b262014-07-10 15:26:12 +0200447exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000449}
Paul Bakker33b43f12013-08-20 11:48:36 +0200450/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */
453void mbedtls_x509_csr_parse( char *csr_der_hex, char *ref_out, int ref_ret )
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200454{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_x509_csr csr;
Paul Bakkerbd51b262014-07-10 15:26:12 +0200456 unsigned char *csr_der = NULL;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200457 char my_out[1000];
458 size_t csr_der_len;
459 int my_ret;
460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200462 memset( my_out, 0, sizeof( my_out ) );
463 csr_der = unhexify_alloc( csr_der_hex, &csr_der_len );
464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der, csr_der_len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200466 TEST_ASSERT( my_ret == ref_ret );
467
468 if( ref_ret == 0 )
469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200471 TEST_ASSERT( my_out_len == strlen( ref_out ) );
472 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
473 }
474
Paul Bakkerbd51b262014-07-10 15:26:12 +0200475exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_x509_csr_free( &csr );
477 mbedtls_free( csr_der );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200478}
479/* END_CASE */
480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
482void mbedtls_x509_crt_parse_path( char *crt_path, int ret, int nb_crt )
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100483{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100485 int i;
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_x509_crt_init( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100490
491 /* Check how many certs we got */
492 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
493 if( cur->raw.p != NULL )
494 i++;
495
496 TEST_ASSERT( i == nb_crt );
497
Paul Bakkerbd51b262014-07-10 15:26:12 +0200498exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100500}
501/* END_CASE */
502
Janos Follath822b2c32015-10-11 10:25:22 +0200503/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Janos Follathef4f2582015-10-11 16:17:27 +0200504void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca, int flags_result )
Janos Follath822b2c32015-10-11 10:25:22 +0200505{
506 char* act;
507 uint32_t flags;
Janos Follathef4f2582015-10-11 16:17:27 +0200508 int result, res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100509 mbedtls_x509_crt trusted, chain;
Janos Follath822b2c32015-10-11 10:25:22 +0200510
Janos Follathef4f2582015-10-11 16:17:27 +0200511 result= flags_result?MBEDTLS_ERR_X509_CERT_VERIFY_FAILED:0;
512
Janos Follath822b2c32015-10-11 10:25:22 +0200513 mbedtls_x509_crt_init( &chain );
514 mbedtls_x509_crt_init( &trusted );
515
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900516 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100517 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
518 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
Janos Follath822b2c32015-10-11 10:25:22 +0200519
520 res = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags, NULL, NULL );
Janos Follathef4f2582015-10-11 16:17:27 +0200521
522 TEST_ASSERT( res == ( result ) );
523 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Janos Follath822b2c32015-10-11 10:25:22 +0200524
525exit:
526 mbedtls_x509_crt_free( &trusted );
527 mbedtls_x509_crt_free( &chain );
528}
529/* END_CASE */
530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100532void x509_oid_desc( char *oid_str, char *ref_desc )
533{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000535 const char *desc = NULL;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100536 unsigned char buf[20];
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000537 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100538
539 memset( buf, 0, sizeof buf );
540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 oid.tag = MBEDTLS_ASN1_OID;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100542 oid.len = unhexify( buf, oid_str );
543 oid.p = buf;
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100546
547 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000548 {
549 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100550 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000551 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100552 else
553 {
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000554 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100555 TEST_ASSERT( desc != NULL );
556 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
557 }
558}
559/* END_CASE */
560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100562void x509_oid_numstr( char *oid_str, char *numstr, int blen, int ret )
563{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100565 unsigned char oid_buf[20];
566 char num_buf[100];
567
568 memset( oid_buf, 0x00, sizeof oid_buf );
569 memset( num_buf, 0x2a, sizeof num_buf );
570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 oid.tag = MBEDTLS_ASN1_OID;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100572 oid.len = unhexify( oid_buf, oid_str );
573 oid.p = oid_buf;
574
575 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100578
579 if( ret >= 0 )
580 {
581 TEST_ASSERT( num_buf[ret] == 0 );
582 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
583 }
584}
585/* END_CASE */
586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587/* 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 +0200588void x509_check_key_usage( char *crt_file, int usage, int ret )
589{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200597
Paul Bakkerbd51b262014-07-10 15:26:12 +0200598exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200600}
601/* END_CASE */
602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603/* 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 +0200604void x509_check_extended_key_usage( char *crt_file, char *usage_hex, int ret )
605{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200607 char oid[50];
608 size_t len;
609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200611
612 len = unhexify( (unsigned char *) oid, usage_hex );
613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 TEST_ASSERT( mbedtls_x509_crt_check_extended_key_usage( &crt, oid, len ) == ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200617
Paul Bakkerbd51b262014-07-10 15:26:12 +0200618exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200620}
621/* END_CASE */
622
Andres AG4b76aec2016-09-23 13:16:02 +0100623/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
624void x509_get_time( int tag, char *time_str, int ret,
625 int year, int mon, int day,
626 int hour, int min, int sec )
627{
628 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +0000629 unsigned char buf[21];
Andres AG4b76aec2016-09-23 13:16:02 +0100630 unsigned char* start = buf;
631 unsigned char* end = buf;
632
633 memset( &time, 0x00, sizeof( time ) );
634 *end = (unsigned char)tag; end++;
Janos Follathea7054a2017-02-08 14:13:02 +0000635 *end = strlen( time_str );
636 TEST_ASSERT( *end < 20 );
Andres AG4b76aec2016-09-23 13:16:02 +0100637 end++;
638 memcpy( end, time_str, (size_t)*(end - 1) );
639 end += *(end - 1);
640
641 TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
642 if( ret == 0 )
643 {
644 TEST_ASSERT( year == time.year );
645 TEST_ASSERT( mon == time.mon );
646 TEST_ASSERT( day == time.day );
647 TEST_ASSERT( hour == time.hour );
648 TEST_ASSERT( min == time.min );
649 TEST_ASSERT( sec == time.sec );
650 }
651}
652/* END_CASE */
653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200655void x509_parse_rsassa_pss_params( char *hex_params, int params_tag,
656 int ref_msg_md, int ref_mgf_md,
657 int ref_salt_len, int ref_ret )
658{
659 int my_ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_x509_buf params;
661 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200662 int my_salt_len;
663
664 params.p = unhexify_alloc( hex_params, &params.len );
665 params.tag = params_tag;
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 my_ret = mbedtls_x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200668 &my_salt_len );
669
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200670 TEST_ASSERT( my_ret == ref_ret );
671
672 if( ref_ret == 0 )
673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md );
675 TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200676 TEST_ASSERT( my_salt_len == ref_salt_len );
677 }
678
Paul Bakkerbd51b262014-07-10 15:26:12 +0200679exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 mbedtls_free( params.p );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200681}
682/* END_CASE */
683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200685void x509_selftest()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000686{
Andres AG93012e82016-09-09 09:10:28 +0100687 TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000688}
Paul Bakker33b43f12013-08-20 11:48:36 +0200689/* END_CASE */