blob: d02068d5f67d851850afcb9dda548086781a91d0 [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"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +01009#include "string.h"
Paul Bakkerb63b0af2011-01-13 17:54:59 +000010
Simon Butcher9e24b512017-07-28 12:15:13 +010011#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
Hanno Becker3b1422e2017-07-26 13:38:02 +010012#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
13than the current threshold 19. To test larger values, please \
14adapt the script tests/data_files/dir-max/long.sh."
15#endif
16
Gilles Peskineef86ab22017-05-05 18:59:02 +020017/* Profile for backward compatibility. Allows SHA-1, unlike the default
18 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020019const mbedtls_x509_crt_profile compat_profile =
20{
21 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
22 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) |
23 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
24 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
25 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
26 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
27 0xFFFFFFF, /* Any PK alg */
28 0xFFFFFFF, /* Any curve */
29 1024,
30};
31
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020032const mbedtls_x509_crt_profile profile_rsa3072 =
33{
34 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
35 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
36 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
37 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA ),
38 0,
39 3072,
40};
41
42const mbedtls_x509_crt_profile profile_sha512 =
43{
44 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
45 0xFFFFFFF, /* Any PK alg */
46 0xFFFFFFF, /* Any curve */
47 1024,
48};
49
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020050int verify_none( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000051{
Paul Bakker5a624082011-01-18 16:31:52 +000052 ((void) data);
53 ((void) crt);
54 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010055 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020056
Paul Bakker915275b2012-09-28 07:10:55 +000057 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000058}
59
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020060int verify_all( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000061{
Paul Bakker5a624082011-01-18 16:31:52 +000062 ((void) data);
63 ((void) crt);
64 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000065 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000066
Paul Bakkerb63b0af2011-01-13 17:54:59 +000067 return 0;
68}
69
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +020070int verify_fatal( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
71{
72 int *levels = (int *) data;
73
74 ((void) crt);
75 ((void) certificate_depth);
76
77 /* Simulate a fatal error in the callback */
78 if( *levels & ( 1 << certificate_depth ) )
79 {
80 *flags |= ( 1 << certificate_depth );
Manuel Pégourié-Gonnard41859782017-05-23 12:58:53 +020081 return( -1 - certificate_depth );
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +020082 }
83
84 return( 0 );
85}
86
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +090087/* strsep() not available on Windows */
88char *mystrsep(char **stringp, const char *delim)
89{
90 const char *p;
91 char *ret = *stringp;
92
93 if( *stringp == NULL )
94 return( NULL );
95
96 for( ; ; (*stringp)++ )
97 {
98 if( **stringp == '\0' )
99 {
100 *stringp = NULL;
101 goto done;
102 }
103
104 for( p = delim; *p != '\0'; p++ )
105 if( **stringp == *p )
106 {
107 **stringp = '\0';
108 (*stringp)++;
109 goto done;
110 }
111 }
112
113done:
114 return( ret );
115}
116
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200117#if defined(MBEDTLS_X509_CRT_PARSE_C)
118typedef struct {
119 char buf[512];
120 char *p;
121} verify_print_context;
122
123void verify_print_init( verify_print_context *ctx )
124{
125 memset( ctx, 0, sizeof( verify_print_context ) );
126 ctx->p = ctx->buf;
127}
128
129int verify_print( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
130{
131 int ret;
132 verify_print_context *ctx = (verify_print_context *) data;
133 char *p = ctx->p;
134 size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p;
135 ((void) flags);
136
137 ret = mbedtls_snprintf( p, n, "depth %d - serial ", certificate_depth );
138 MBEDTLS_X509_SAFE_SNPRINTF;
139
140 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
141 MBEDTLS_X509_SAFE_SNPRINTF;
142
143 ret = mbedtls_snprintf( p, n, " - subject " );
144 MBEDTLS_X509_SAFE_SNPRINTF;
145
146 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
147 MBEDTLS_X509_SAFE_SNPRINTF;
148
Manuel Pégourié-Gonnardbe2f0b52017-08-21 11:00:22 +0200149 ret = mbedtls_snprintf( p, n, " - flags 0x%08x\n", *flags );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200150 MBEDTLS_X509_SAFE_SNPRINTF;
151
152 ctx->p = p;
153
154 return( 0 );
155}
156#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200157/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000158
Paul Bakker33b43f12013-08-20 11:48:36 +0200159/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200161 * END_DEPENDENCIES
162 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200165void x509_cert_info( char *crt_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000166{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000168 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000169 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000172 memset( buf, 0, 2000 );
173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
175 res = mbedtls_x509_crt_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000176
177 TEST_ASSERT( res != -1 );
178 TEST_ASSERT( res != -2 );
179
Paul Bakker33b43f12013-08-20 11:48:36 +0200180 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200181
182exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000184}
Paul Bakker33b43f12013-08-20 11:48:36 +0200185/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
188void mbedtls_x509_crl_info( char *crl_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000189{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000191 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000192 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000195 memset( buf, 0, 2000 );
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
198 res = mbedtls_x509_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000199
200 TEST_ASSERT( res != -1 );
201 TEST_ASSERT( res != -2 );
202
Paul Bakker33b43f12013-08-20 11:48:36 +0200203 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200204
205exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000207}
Paul Bakker33b43f12013-08-20 11:48:36 +0200208/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000209
Andres AGa39db392016-12-08 17:10:38 +0000210/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
211void mbedtls_x509_crl_parse( char *crl_file, int result )
212{
213 mbedtls_x509_crl crl;
214 char buf[2000];
215
216 mbedtls_x509_crl_init( &crl );
217 memset( buf, 0, 2000 );
218
219 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == result );
220
221exit:
222 mbedtls_x509_crl_free( &crl );
223}
224/* END_CASE */
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C */
227void mbedtls_x509_csr_info( char *csr_file, char *result_str )
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100228{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100230 char buf[2000];
231 int res;
232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100234 memset( buf, 0, 2000 );
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 TEST_ASSERT( mbedtls_x509_csr_parse_file( &csr, csr_file ) == 0 );
237 res = mbedtls_x509_csr_info( buf, 2000, "", &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100238
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100239 TEST_ASSERT( res != -1 );
240 TEST_ASSERT( res != -2 );
241
242 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200243
244exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100246}
247/* END_CASE */
248
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100249/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
250void x509_verify_info( int flags, char *prefix, char *result_str )
251{
252 char buf[2000];
253 int res;
254
255 memset( buf, 0, sizeof( buf ) );
256
257 res = mbedtls_x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
258
259 TEST_ASSERT( res >= 0 );
260
261 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
262}
263/* END_CASE */
264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200266void x509_verify( char *crt_file, char *ca_file, char *crl_file,
267 char *cn_name_str, int result, int flags_result,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200268 char *profile_str,
Paul Bakker33b43f12013-08-20 11:48:36 +0200269 char *verify_callback )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000270{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_x509_crt crt;
272 mbedtls_x509_crt ca;
273 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200274 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000275 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200276 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200277 char * cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200278 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_x509_crt_init( &crt );
281 mbedtls_x509_crt_init( &ca );
282 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000283
Paul Bakker33b43f12013-08-20 11:48:36 +0200284 if( strcmp( cn_name_str, "NULL" ) != 0 )
285 cn_name = cn_name_str;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200286
Manuel Pégourié-Gonnarda54f6cc2017-08-09 10:41:42 +0200287 if( strcmp( profile_str, "" ) == 0 )
Gilles Peskineef86ab22017-05-05 18:59:02 +0200288 profile = &mbedtls_x509_crt_profile_default;
Ron Eldorc1539982018-02-06 18:47:17 +0200289 else if( strcmp( profile_str, "next" ) == 0 )
290 profile = &mbedtls_x509_crt_profile_next;
291 else if( strcmp( profile_str, "suite_b" ) == 0 )
292 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200293 else if( strcmp( profile_str, "compat" ) == 0 )
294 profile = &compat_profile;
295 else
296 TEST_ASSERT( "Unknown algorithm profile" == 0 );
297
Paul Bakker33b43f12013-08-20 11:48:36 +0200298 if( strcmp( verify_callback, "NULL" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200299 f_vrfy = NULL;
Paul Bakker33b43f12013-08-20 11:48:36 +0200300 else if( strcmp( verify_callback, "verify_none" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200301 f_vrfy = verify_none;
Paul Bakker33b43f12013-08-20 11:48:36 +0200302 else if( strcmp( verify_callback, "verify_all" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200303 f_vrfy = verify_all;
304 else
305 TEST_ASSERT( "No known verify callback selected" == 0 );
306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
308 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
309 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000310
Gilles Peskineef86ab22017-05-05 18:59:02 +0200311 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 +0200312
Paul Bakkerbd51b262014-07-10 15:26:12 +0200313 TEST_ASSERT( res == ( result ) );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200314 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200315
316exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_x509_crt_free( &crt );
318 mbedtls_x509_crt_free( &ca );
319 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000320}
Paul Bakker33b43f12013-08-20 11:48:36 +0200321/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200324void x509_verify_callback( char *crt_file, char *ca_file, char *name,
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200325 int exp_ret, char *exp_vrfy_out )
326{
327 int ret;
328 mbedtls_x509_crt crt;
329 mbedtls_x509_crt ca;
330 uint32_t flags = 0;
331 verify_print_context vrfy_ctx;
332
333 mbedtls_x509_crt_init( &crt );
334 mbedtls_x509_crt_init( &ca );
335 verify_print_init( &vrfy_ctx );
336
337 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
338 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
339
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200340 if( strcmp( name, "NULL" ) == 0 )
341 name = NULL;
342
Gilles Peskineef86ab22017-05-05 18:59:02 +0200343 ret = mbedtls_x509_crt_verify_with_profile( &crt, &ca, NULL,
344 &compat_profile,
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200345 name, &flags,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200346 verify_print, &vrfy_ctx );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200347
348 TEST_ASSERT( ret == exp_ret );
349 TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
350
351exit:
352 mbedtls_x509_crt_free( &crt );
353 mbedtls_x509_crt_free( &ca );
354}
355/* END_CASE */
356
357/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358void mbedtls_x509_dn_gets( char *crt_file, char *entity, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000359{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000361 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200362 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000365 memset( buf, 0, 2000 );
366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200368 if( strcmp( entity, "subject" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 res = mbedtls_x509_dn_gets( buf, 2000, &crt.subject );
Paul Bakker33b43f12013-08-20 11:48:36 +0200370 else if( strcmp( entity, "issuer" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 res = mbedtls_x509_dn_gets( buf, 2000, &crt.issuer );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200372 else
373 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000374
375 TEST_ASSERT( res != -1 );
376 TEST_ASSERT( res != -2 );
377
Paul Bakker33b43f12013-08-20 11:48:36 +0200378 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200379
380exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000382}
Paul Bakker33b43f12013-08-20 11:48:36 +0200383/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100386void mbedtls_x509_time_is_past( char *crt_file, char *entity, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200393
Paul Bakker33b43f12013-08-20 11:48:36 +0200394 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100395 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_from ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200396 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100397 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_to ) == result );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200398 else
399 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000400
Paul Bakkerbd51b262014-07-10 15:26:12 +0200401exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000403}
Paul Bakker33b43f12013-08-20 11:48:36 +0200404/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100407void mbedtls_x509_time_is_future( char *crt_file, char *entity, int result )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100408{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100414
415 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100416 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_from ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100417 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100418 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_to ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100419 else
420 TEST_ASSERT( "Unknown entity" == 0 );
421
Paul Bakkerbd51b262014-07-10 15:26:12 +0200422exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100424}
425/* END_CASE */
426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Paul Bakker5a5fa922014-09-26 14:53:04 +0200428void x509parse_crt_file( char *crt_file, int result )
429{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +0200431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_x509_crt_init( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200435
436exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_x509_crt_free( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200438}
439/* END_CASE */
440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200442void x509parse_crt( char *crt_data, char *result_str, int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000443{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_x509_crt crt;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000445 unsigned char buf[2000];
446 unsigned char output[2000];
447 int data_len, res;
448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_x509_crt_init( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000450 memset( buf, 0, 2000 );
451 memset( output, 0, 2000 );
452
Paul Bakker33b43f12013-08-20 11:48:36 +0200453 data_len = unhexify( buf, crt_data );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 TEST_ASSERT( mbedtls_x509_crt_parse( &crt, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200456 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200459
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000460 TEST_ASSERT( res != -1 );
461 TEST_ASSERT( res != -2 );
462
Paul Bakker33b43f12013-08-20 11:48:36 +0200463 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000464 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000465
Paul Bakkerbd51b262014-07-10 15:26:12 +0200466exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000468}
Paul Bakker33b43f12013-08-20 11:48:36 +0200469/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200472void x509parse_crl( char *crl_data, char *result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000473{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000475 unsigned char buf[2000];
476 unsigned char output[2000];
477 int data_len, res;
478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 mbedtls_x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000480 memset( buf, 0, 2000 );
481 memset( output, 0, 2000 );
482
Paul Bakker33b43f12013-08-20 11:48:36 +0200483 data_len = unhexify( buf, crl_data );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200486 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200489
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000490 TEST_ASSERT( res != -1 );
491 TEST_ASSERT( res != -2 );
492
Paul Bakker33b43f12013-08-20 11:48:36 +0200493 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000494 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000495
Paul Bakkerbd51b262014-07-10 15:26:12 +0200496exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000498}
Paul Bakker33b43f12013-08-20 11:48:36 +0200499/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */
502void mbedtls_x509_csr_parse( char *csr_der_hex, char *ref_out, int ref_ret )
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200503{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_x509_csr csr;
Paul Bakkerbd51b262014-07-10 15:26:12 +0200505 unsigned char *csr_der = NULL;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200506 char my_out[1000];
507 size_t csr_der_len;
508 int my_ret;
509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200511 memset( my_out, 0, sizeof( my_out ) );
512 csr_der = unhexify_alloc( csr_der_hex, &csr_der_len );
513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der, csr_der_len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200515 TEST_ASSERT( my_ret == ref_ret );
516
517 if( ref_ret == 0 )
518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200520 TEST_ASSERT( my_out_len == strlen( ref_out ) );
521 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
522 }
523
Paul Bakkerbd51b262014-07-10 15:26:12 +0200524exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_x509_csr_free( &csr );
526 mbedtls_free( csr_der );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200527}
528/* END_CASE */
529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
531void mbedtls_x509_crt_parse_path( char *crt_path, int ret, int nb_crt )
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100532{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100534 int i;
535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_x509_crt_init( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100539
540 /* Check how many certs we got */
541 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
542 if( cur->raw.p != NULL )
543 i++;
544
545 TEST_ASSERT( i == nb_crt );
546
Paul Bakkerbd51b262014-07-10 15:26:12 +0200547exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100549}
550/* END_CASE */
551
Janos Follath822b2c32015-10-11 10:25:22 +0200552/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +0200553void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int,
554 int ret_chk, int flags_chk )
555{
556 char file_buf[128];
557 int ret;
558 uint32_t flags;
559 mbedtls_x509_crt trusted, chain;
560
561 /*
562 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
563 * with NN.crt signed by NN-1.crt
564 */
565
566 mbedtls_x509_crt_init( &trusted );
567 mbedtls_x509_crt_init( &chain );
568
569 /* Load trusted root */
570 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 );
571
572 /* Load a chain with nb_int intermediates (from 01 to nb_int),
573 * plus one "end-entity" cert (nb_int + 1) */
574 ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
575 nb_int + 1 );
576 TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf );
577 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 );
578
579 /* Try to verify that chain */
580 ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags,
581 NULL, NULL );
582 TEST_ASSERT( ret == ret_chk );
583 TEST_ASSERT( flags == (uint32_t) flags_chk );
584
585exit:
586 mbedtls_x509_crt_free( &chain );
587 mbedtls_x509_crt_free( &trusted );
588}
589/* END_CASE */
590
591/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200592void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca,
593 int flags_result, int result,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200594 char *profile_name, int vrfy_fatal_lvls )
Janos Follath822b2c32015-10-11 10:25:22 +0200595{
596 char* act;
597 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200598 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100599 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200600 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +0200601
Janos Follath822b2c32015-10-11 10:25:22 +0200602 mbedtls_x509_crt_init( &chain );
603 mbedtls_x509_crt_init( &trusted );
604
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900605 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100606 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
607 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
Janos Follath822b2c32015-10-11 10:25:22 +0200608
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200609 if( strcmp( profile_name, "" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200610 profile = &mbedtls_x509_crt_profile_default;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200611 else if( strcmp( profile_name, "next" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200612 profile = &mbedtls_x509_crt_profile_next;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200613 else if( strcmp( profile_name, "suiteb" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200614 profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200615 else if( strcmp( profile_name, "rsa3072" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200616 profile = &profile_rsa3072;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200617 else if( strcmp( profile_name, "sha512" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200618 profile = &profile_sha512;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200619
620 res = mbedtls_x509_crt_verify_with_profile( &chain, &trusted, NULL, profile,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200621 NULL, &flags, verify_fatal, &vrfy_fatal_lvls );
Janos Follathef4f2582015-10-11 16:17:27 +0200622
623 TEST_ASSERT( res == ( result ) );
624 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Janos Follath822b2c32015-10-11 10:25:22 +0200625
626exit:
627 mbedtls_x509_crt_free( &trusted );
628 mbedtls_x509_crt_free( &chain );
629}
630/* END_CASE */
631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100633void x509_oid_desc( char *oid_str, char *ref_desc )
634{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000636 const char *desc = NULL;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100637 unsigned char buf[20];
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000638 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100639
640 memset( buf, 0, sizeof buf );
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 oid.tag = MBEDTLS_ASN1_OID;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100643 oid.len = unhexify( buf, oid_str );
644 oid.p = buf;
645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100647
648 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000649 {
650 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100651 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000652 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100653 else
654 {
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000655 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100656 TEST_ASSERT( desc != NULL );
657 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
658 }
659}
660/* END_CASE */
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100663void x509_oid_numstr( char *oid_str, char *numstr, int blen, int ret )
664{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100666 unsigned char oid_buf[20];
667 char num_buf[100];
668
669 memset( oid_buf, 0x00, sizeof oid_buf );
670 memset( num_buf, 0x2a, sizeof num_buf );
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 oid.tag = MBEDTLS_ASN1_OID;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100673 oid.len = unhexify( oid_buf, oid_str );
674 oid.p = oid_buf;
675
676 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100679
680 if( ret >= 0 )
681 {
682 TEST_ASSERT( num_buf[ret] == 0 );
683 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
684 }
685}
686/* END_CASE */
687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688/* 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 +0200689void x509_check_key_usage( char *crt_file, int usage, int ret )
690{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200698
Paul Bakkerbd51b262014-07-10 15:26:12 +0200699exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200701}
702/* END_CASE */
703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704/* 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 +0200705void x509_check_extended_key_usage( char *crt_file, char *usage_hex, int ret )
706{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200708 char oid[50];
709 size_t len;
710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200712
713 len = unhexify( (unsigned char *) oid, usage_hex );
714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 TEST_ASSERT( mbedtls_x509_crt_check_extended_key_usage( &crt, oid, len ) == ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200718
Paul Bakkerbd51b262014-07-10 15:26:12 +0200719exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200721}
722/* END_CASE */
723
Andres AG4b76aec2016-09-23 13:16:02 +0100724/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
725void x509_get_time( int tag, char *time_str, int ret,
726 int year, int mon, int day,
727 int hour, int min, int sec )
728{
729 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +0000730 unsigned char buf[21];
Andres AG4b76aec2016-09-23 13:16:02 +0100731 unsigned char* start = buf;
732 unsigned char* end = buf;
733
734 memset( &time, 0x00, sizeof( time ) );
735 *end = (unsigned char)tag; end++;
Janos Follathea7054a2017-02-08 14:13:02 +0000736 *end = strlen( time_str );
737 TEST_ASSERT( *end < 20 );
Andres AG4b76aec2016-09-23 13:16:02 +0100738 end++;
739 memcpy( end, time_str, (size_t)*(end - 1) );
740 end += *(end - 1);
741
742 TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
743 if( ret == 0 )
744 {
745 TEST_ASSERT( year == time.year );
746 TEST_ASSERT( mon == time.mon );
747 TEST_ASSERT( day == time.day );
748 TEST_ASSERT( hour == time.hour );
749 TEST_ASSERT( min == time.min );
750 TEST_ASSERT( sec == time.sec );
751 }
752}
753/* END_CASE */
754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200756void x509_parse_rsassa_pss_params( char *hex_params, int params_tag,
757 int ref_msg_md, int ref_mgf_md,
758 int ref_salt_len, int ref_ret )
759{
760 int my_ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_x509_buf params;
762 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200763 int my_salt_len;
764
765 params.p = unhexify_alloc( hex_params, &params.len );
766 params.tag = params_tag;
767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 my_ret = mbedtls_x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200769 &my_salt_len );
770
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200771 TEST_ASSERT( my_ret == ref_ret );
772
773 if( ref_ret == 0 )
774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md );
776 TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200777 TEST_ASSERT( my_salt_len == ref_salt_len );
778 }
779
Paul Bakkerbd51b262014-07-10 15:26:12 +0200780exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 mbedtls_free( params.p );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200782}
783/* END_CASE */
784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200786void x509_selftest()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000787{
Andres AG93012e82016-09-09 09:10:28 +0100788 TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000789}
Paul Bakker33b43f12013-08-20 11:48:36 +0200790/* END_CASE */