blob: 4d36027f17a53820bccdb143c29226b2c4a55fe8 [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 */
Azim Khanf1aaec92017-05-30 14:23:15 +0100165void 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 */
Azim Khanf1aaec92017-05-30 14:23:15 +0100188void 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 */
Azim Khanf1aaec92017-05-30 14:23:15 +0100211void mbedtls_x509_crl_parse( char * crl_file, int result )
Andres AGa39db392016-12-08 17:10:38 +0000212{
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 */
Azim Khanf1aaec92017-05-30 14:23:15 +0100227void 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 */
Azim Khanf1aaec92017-05-30 14:23:15 +0100250void x509_verify_info( int flags, char * prefix, char * result_str )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100251{
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 */
Azim Khanf1aaec92017-05-30 14:23:15 +0100358void 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 */
Azim Khanf1aaec92017-05-30 14:23:15 +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 */
Azim Khanf1aaec92017-05-30 14:23:15 +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 */
Azim Khanf1aaec92017-05-30 14:23:15 +0100428void x509parse_crt_file( char * crt_file, int result )
Paul Bakker5a5fa922014-09-26 14:53:04 +0200429{
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 */
Azim Khanf1aaec92017-05-30 14:23:15 +0100442void x509parse_crt( uint8_t * buf, uint32_t data_len, char * result_str,
443 int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000444{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_x509_crt crt;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000446 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100447 int res;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_x509_crt_init( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000450 memset( output, 0, 2000 );
451
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 TEST_ASSERT( mbedtls_x509_crt_parse( &crt, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200454 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200457
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000458 TEST_ASSERT( res != -1 );
459 TEST_ASSERT( res != -2 );
460
Paul Bakker33b43f12013-08-20 11:48:36 +0200461 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000462 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000463
Paul Bakkerbd51b262014-07-10 15:26:12 +0200464exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000466}
Paul Bakker33b43f12013-08-20 11:48:36 +0200467/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100470void x509parse_crl( uint8_t * buf, uint32_t data_len, char * result_str,
471 int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000472{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000474 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100475 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000478 memset( output, 0, 2000 );
479
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200482 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200485
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000486 TEST_ASSERT( res != -1 );
487 TEST_ASSERT( res != -2 );
488
Paul Bakker33b43f12013-08-20 11:48:36 +0200489 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000490 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000491
Paul Bakkerbd51b262014-07-10 15:26:12 +0200492exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000494}
Paul Bakker33b43f12013-08-20 11:48:36 +0200495/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100498void mbedtls_x509_csr_parse( char * csr_der_hex, char * ref_out, int ref_ret )
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200499{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_x509_csr csr;
Paul Bakkerbd51b262014-07-10 15:26:12 +0200501 unsigned char *csr_der = NULL;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200502 char my_out[1000];
503 size_t csr_der_len;
504 int my_ret;
505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200507 memset( my_out, 0, sizeof( my_out ) );
508 csr_der = unhexify_alloc( csr_der_hex, &csr_der_len );
509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der, csr_der_len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200511 TEST_ASSERT( my_ret == ref_ret );
512
513 if( ref_ret == 0 )
514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200516 TEST_ASSERT( my_out_len == strlen( ref_out ) );
517 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
518 }
519
Paul Bakkerbd51b262014-07-10 15:26:12 +0200520exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_x509_csr_free( &csr );
522 mbedtls_free( csr_der );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200523}
524/* END_CASE */
525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100527void mbedtls_x509_crt_parse_path( char * crt_path, int ret, int nb_crt )
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100528{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100530 int i;
531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_x509_crt_init( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100535
536 /* Check how many certs we got */
537 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
538 if( cur->raw.p != NULL )
539 i++;
540
541 TEST_ASSERT( i == nb_crt );
542
Paul Bakkerbd51b262014-07-10 15:26:12 +0200543exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 mbedtls_x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100545}
546/* END_CASE */
547
Janos Follath822b2c32015-10-11 10:25:22 +0200548/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +0200549void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int,
550 int ret_chk, int flags_chk )
551{
552 char file_buf[128];
553 int ret;
554 uint32_t flags;
555 mbedtls_x509_crt trusted, chain;
556
557 /*
558 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
559 * with NN.crt signed by NN-1.crt
560 */
561
562 mbedtls_x509_crt_init( &trusted );
563 mbedtls_x509_crt_init( &chain );
564
565 /* Load trusted root */
566 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 );
567
568 /* Load a chain with nb_int intermediates (from 01 to nb_int),
569 * plus one "end-entity" cert (nb_int + 1) */
570 ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
571 nb_int + 1 );
572 TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf );
573 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 );
574
575 /* Try to verify that chain */
576 ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags,
577 NULL, NULL );
578 TEST_ASSERT( ret == ret_chk );
579 TEST_ASSERT( flags == (uint32_t) flags_chk );
580
581exit:
582 mbedtls_x509_crt_free( &chain );
583 mbedtls_x509_crt_free( &trusted );
584}
585/* END_CASE */
586
587/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200588void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca,
589 int flags_result, int result,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200590 char *profile_name, int vrfy_fatal_lvls )
Janos Follath822b2c32015-10-11 10:25:22 +0200591{
592 char* act;
593 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200594 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100595 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200596 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +0200597
Janos Follath822b2c32015-10-11 10:25:22 +0200598 mbedtls_x509_crt_init( &chain );
599 mbedtls_x509_crt_init( &trusted );
600
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900601 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100602 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
603 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
Janos Follath822b2c32015-10-11 10:25:22 +0200604
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200605 if( strcmp( profile_name, "" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200606 profile = &mbedtls_x509_crt_profile_default;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200607 else if( strcmp( profile_name, "next" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200608 profile = &mbedtls_x509_crt_profile_next;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200609 else if( strcmp( profile_name, "suiteb" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200610 profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200611 else if( strcmp( profile_name, "rsa3072" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200612 profile = &profile_rsa3072;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200613 else if( strcmp( profile_name, "sha512" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200614 profile = &profile_sha512;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200615
616 res = mbedtls_x509_crt_verify_with_profile( &chain, &trusted, NULL, profile,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200617 NULL, &flags, verify_fatal, &vrfy_fatal_lvls );
Janos Follathef4f2582015-10-11 16:17:27 +0200618
619 TEST_ASSERT( res == ( result ) );
620 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Janos Follath822b2c32015-10-11 10:25:22 +0200621
622exit:
623 mbedtls_x509_crt_free( &trusted );
624 mbedtls_x509_crt_free( &chain );
625}
626/* END_CASE */
627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100629void x509_oid_desc( uint8_t * buf, uint32_t buf_len, char * ref_desc )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100630{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000632 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000633 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100634
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 oid.tag = MBEDTLS_ASN1_OID;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100637 oid.p = buf;
Azim Khanf1aaec92017-05-30 14:23:15 +0100638 oid.len = buf_len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100641
642 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000643 {
644 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100645 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000646 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100647 else
648 {
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000649 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100650 TEST_ASSERT( desc != NULL );
651 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
652 }
653}
654/* END_CASE */
655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100657void x509_oid_numstr( uint8_t * oid_buf, uint32_t oid_buf_len, char * numstr,
658 int blen, int ret )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100659{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100661 char num_buf[100];
662
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100663 memset( num_buf, 0x2a, sizeof num_buf );
664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 oid.tag = MBEDTLS_ASN1_OID;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100666 oid.p = oid_buf;
Azim Khanf1aaec92017-05-30 14:23:15 +0100667 oid.len = oid_buf_len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100668
669 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100672
673 if( ret >= 0 )
674 {
675 TEST_ASSERT( num_buf[ret] == 0 );
676 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
677 }
678}
679/* END_CASE */
680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_KEY_USAGE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100682void x509_check_key_usage( char * crt_file, int usage, int ret )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200683{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200691
Paul Bakkerbd51b262014-07-10 15:26:12 +0200692exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200694}
695/* END_CASE */
696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100698void x509_check_extended_key_usage( char * crt_file, uint8_t * oid,
699 uint32_t len, int ret )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200700{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200704
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 TEST_ASSERT( mbedtls_x509_crt_check_extended_key_usage( &crt, oid, len ) == ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200709
Paul Bakkerbd51b262014-07-10 15:26:12 +0200710exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200712}
713/* END_CASE */
714
Andres AG4b76aec2016-09-23 13:16:02 +0100715/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100716void x509_get_time( int tag, char * time_str, int ret, int year, int mon,
717 int day, int hour, int min, int sec )
Andres AG4b76aec2016-09-23 13:16:02 +0100718{
719 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +0000720 unsigned char buf[21];
Andres AG4b76aec2016-09-23 13:16:02 +0100721 unsigned char* start = buf;
722 unsigned char* end = buf;
723
724 memset( &time, 0x00, sizeof( time ) );
725 *end = (unsigned char)tag; end++;
Janos Follathea7054a2017-02-08 14:13:02 +0000726 *end = strlen( time_str );
727 TEST_ASSERT( *end < 20 );
Andres AG4b76aec2016-09-23 13:16:02 +0100728 end++;
729 memcpy( end, time_str, (size_t)*(end - 1) );
730 end += *(end - 1);
731
732 TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
733 if( ret == 0 )
734 {
735 TEST_ASSERT( year == time.year );
736 TEST_ASSERT( mon == time.mon );
737 TEST_ASSERT( day == time.day );
738 TEST_ASSERT( hour == time.hour );
739 TEST_ASSERT( min == time.min );
740 TEST_ASSERT( sec == time.sec );
741 }
742}
743/* END_CASE */
744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Azim Khanf1aaec92017-05-30 14:23:15 +0100746void x509_parse_rsassa_pss_params( char * hex_params, int params_tag,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200747 int ref_msg_md, int ref_mgf_md,
748 int ref_salt_len, int ref_ret )
749{
750 int my_ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_x509_buf params;
752 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200753 int my_salt_len;
754
755 params.p = unhexify_alloc( hex_params, &params.len );
756 params.tag = params_tag;
757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 my_ret = mbedtls_x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200759 &my_salt_len );
760
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200761 TEST_ASSERT( my_ret == ref_ret );
762
763 if( ref_ret == 0 )
764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md );
766 TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200767 TEST_ASSERT( my_salt_len == ref_salt_len );
768 }
769
Paul Bakkerbd51b262014-07-10 15:26:12 +0200770exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_free( params.p );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200772}
773/* END_CASE */
774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100776void x509_selftest( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000777{
Andres AG93012e82016-09-09 09:10:28 +0100778 TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000779}
Paul Bakker33b43f12013-08-20 11:48:36 +0200780/* END_CASE */