blob: 2e283087b522d565ee3c15926cf1e51d2b4a48fc [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Azim Khand30ca132017-06-09 04:32:58 +01002#include "mbedtls/bignum.h"
Andres AG4b76aec2016-09-23 13:16:02 +01003#include "mbedtls/x509.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00004#include "mbedtls/x509_crt.h"
5#include "mbedtls/x509_crl.h"
6#include "mbedtls/x509_csr.h"
7#include "mbedtls/pem.h"
8#include "mbedtls/oid.h"
9#include "mbedtls/base64.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +010010#include "string.h"
Paul Bakkerb63b0af2011-01-13 17:54:59 +000011
Simon Butcher9e24b512017-07-28 12:15:13 +010012#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
Hanno Becker3b1422e2017-07-26 13:38:02 +010013#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
14than the current threshold 19. To test larger values, please \
15adapt the script tests/data_files/dir-max/long.sh."
16#endif
17
Gilles Peskineef86ab22017-05-05 18:59:02 +020018/* Profile for backward compatibility. Allows SHA-1, unlike the default
19 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020020const mbedtls_x509_crt_profile compat_profile =
21{
22 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
23 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) |
24 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
25 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
26 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
27 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
28 0xFFFFFFF, /* Any PK alg */
29 0xFFFFFFF, /* Any curve */
30 1024,
31};
32
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020033const mbedtls_x509_crt_profile profile_rsa3072 =
34{
35 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
36 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
37 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
38 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA ),
39 0,
40 3072,
41};
42
43const mbedtls_x509_crt_profile profile_sha512 =
44{
45 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
46 0xFFFFFFF, /* Any PK alg */
47 0xFFFFFFF, /* Any curve */
48 1024,
49};
50
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020051int verify_none( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000052{
Paul Bakker5a624082011-01-18 16:31:52 +000053 ((void) data);
54 ((void) crt);
55 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010056 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020057
Paul Bakker915275b2012-09-28 07:10:55 +000058 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000059}
60
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020061int verify_all( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000062{
Paul Bakker5a624082011-01-18 16:31:52 +000063 ((void) data);
64 ((void) crt);
65 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000066 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000067
Paul Bakkerb63b0af2011-01-13 17:54:59 +000068 return 0;
69}
70
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +020071int verify_fatal( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
72{
73 int *levels = (int *) data;
74
75 ((void) crt);
76 ((void) certificate_depth);
77
78 /* Simulate a fatal error in the callback */
79 if( *levels & ( 1 << certificate_depth ) )
80 {
81 *flags |= ( 1 << certificate_depth );
Manuel Pégourié-Gonnard41859782017-05-23 12:58:53 +020082 return( -1 - certificate_depth );
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +020083 }
84
85 return( 0 );
86}
87
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +090088/* strsep() not available on Windows */
89char *mystrsep(char **stringp, const char *delim)
90{
91 const char *p;
92 char *ret = *stringp;
93
94 if( *stringp == NULL )
95 return( NULL );
96
97 for( ; ; (*stringp)++ )
98 {
99 if( **stringp == '\0' )
100 {
101 *stringp = NULL;
102 goto done;
103 }
104
105 for( p = delim; *p != '\0'; p++ )
106 if( **stringp == *p )
107 {
108 **stringp = '\0';
109 (*stringp)++;
110 goto done;
111 }
112 }
113
114done:
115 return( ret );
116}
117
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200118#if defined(MBEDTLS_X509_CRT_PARSE_C)
119typedef struct {
120 char buf[512];
121 char *p;
122} verify_print_context;
123
124void verify_print_init( verify_print_context *ctx )
125{
126 memset( ctx, 0, sizeof( verify_print_context ) );
127 ctx->p = ctx->buf;
128}
129
130int verify_print( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
131{
132 int ret;
133 verify_print_context *ctx = (verify_print_context *) data;
134 char *p = ctx->p;
135 size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p;
136 ((void) flags);
137
138 ret = mbedtls_snprintf( p, n, "depth %d - serial ", certificate_depth );
139 MBEDTLS_X509_SAFE_SNPRINTF;
140
141 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
142 MBEDTLS_X509_SAFE_SNPRINTF;
143
144 ret = mbedtls_snprintf( p, n, " - subject " );
145 MBEDTLS_X509_SAFE_SNPRINTF;
146
147 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
148 MBEDTLS_X509_SAFE_SNPRINTF;
149
Manuel Pégourié-Gonnardbe2f0b52017-08-21 11:00:22 +0200150 ret = mbedtls_snprintf( p, n, " - flags 0x%08x\n", *flags );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200151 MBEDTLS_X509_SAFE_SNPRINTF;
152
153 ctx->p = p;
154
155 return( 0 );
156}
157#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200158/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000159
Paul Bakker33b43f12013-08-20 11:48:36 +0200160/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200162 * END_DEPENDENCIES
163 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100166void x509_cert_info( char * crt_file, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000167{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000169 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000170 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000173 memset( buf, 0, 2000 );
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
176 res = mbedtls_x509_crt_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000177
178 TEST_ASSERT( res != -1 );
179 TEST_ASSERT( res != -2 );
180
Paul Bakker33b43f12013-08-20 11:48:36 +0200181 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200182
183exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000185}
Paul Bakker33b43f12013-08-20 11:48:36 +0200186/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100189void mbedtls_x509_crl_info( char * crl_file, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000190{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000192 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000193 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000196 memset( buf, 0, 2000 );
197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
199 res = mbedtls_x509_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000200
201 TEST_ASSERT( res != -1 );
202 TEST_ASSERT( res != -2 );
203
Paul Bakker33b43f12013-08-20 11:48:36 +0200204 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200205
206exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000208}
Paul Bakker33b43f12013-08-20 11:48:36 +0200209/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000210
Andres AGa39db392016-12-08 17:10:38 +0000211/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100212void mbedtls_x509_crl_parse( char * crl_file, int result )
Andres AGa39db392016-12-08 17:10:38 +0000213{
214 mbedtls_x509_crl crl;
215 char buf[2000];
216
217 mbedtls_x509_crl_init( &crl );
218 memset( buf, 0, 2000 );
219
220 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == result );
221
222exit:
223 mbedtls_x509_crl_free( &crl );
224}
225/* END_CASE */
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100228void mbedtls_x509_csr_info( char * csr_file, char * result_str )
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100229{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100231 char buf[2000];
232 int res;
233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100235 memset( buf, 0, 2000 );
236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 TEST_ASSERT( mbedtls_x509_csr_parse_file( &csr, csr_file ) == 0 );
238 res = mbedtls_x509_csr_info( buf, 2000, "", &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100239
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100240 TEST_ASSERT( res != -1 );
241 TEST_ASSERT( res != -2 );
242
243 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200244
245exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100247}
248/* END_CASE */
249
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100250/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100251void x509_verify_info( int flags, char * prefix, char * result_str )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100252{
253 char buf[2000];
254 int res;
255
256 memset( buf, 0, sizeof( buf ) );
257
258 res = mbedtls_x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
259
260 TEST_ASSERT( res >= 0 );
261
262 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
263}
264/* END_CASE */
265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200267void x509_verify( char *crt_file, char *ca_file, char *crl_file,
268 char *cn_name_str, int result, int flags_result,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200269 char *profile_str,
Paul Bakker33b43f12013-08-20 11:48:36 +0200270 char *verify_callback )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000271{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 mbedtls_x509_crt crt;
273 mbedtls_x509_crt ca;
274 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200275 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000276 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200277 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200278 char * cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200279 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_x509_crt_init( &crt );
282 mbedtls_x509_crt_init( &ca );
283 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000284
Paul Bakker33b43f12013-08-20 11:48:36 +0200285 if( strcmp( cn_name_str, "NULL" ) != 0 )
286 cn_name = cn_name_str;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200287
Manuel Pégourié-Gonnarda54f6cc2017-08-09 10:41:42 +0200288 if( strcmp( profile_str, "" ) == 0 )
Gilles Peskineef86ab22017-05-05 18:59:02 +0200289 profile = &mbedtls_x509_crt_profile_default;
Ron Eldorc1539982018-02-06 18:47:17 +0200290 else if( strcmp( profile_str, "next" ) == 0 )
291 profile = &mbedtls_x509_crt_profile_next;
292 else if( strcmp( profile_str, "suite_b" ) == 0 )
293 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200294 else if( strcmp( profile_str, "compat" ) == 0 )
295 profile = &compat_profile;
296 else
297 TEST_ASSERT( "Unknown algorithm profile" == 0 );
298
Paul Bakker33b43f12013-08-20 11:48:36 +0200299 if( strcmp( verify_callback, "NULL" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200300 f_vrfy = NULL;
Paul Bakker33b43f12013-08-20 11:48:36 +0200301 else if( strcmp( verify_callback, "verify_none" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200302 f_vrfy = verify_none;
Paul Bakker33b43f12013-08-20 11:48:36 +0200303 else if( strcmp( verify_callback, "verify_all" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200304 f_vrfy = verify_all;
305 else
306 TEST_ASSERT( "No known verify callback selected" == 0 );
307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
309 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
310 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000311
Gilles Peskineef86ab22017-05-05 18:59:02 +0200312 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 +0200313
Paul Bakkerbd51b262014-07-10 15:26:12 +0200314 TEST_ASSERT( res == ( result ) );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200315 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200316
317exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 mbedtls_x509_crt_free( &crt );
319 mbedtls_x509_crt_free( &ca );
320 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000321}
Paul Bakker33b43f12013-08-20 11:48:36 +0200322/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200325void x509_verify_callback( char *crt_file, char *ca_file, char *name,
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200326 int exp_ret, char *exp_vrfy_out )
327{
328 int ret;
329 mbedtls_x509_crt crt;
330 mbedtls_x509_crt ca;
331 uint32_t flags = 0;
332 verify_print_context vrfy_ctx;
333
334 mbedtls_x509_crt_init( &crt );
335 mbedtls_x509_crt_init( &ca );
336 verify_print_init( &vrfy_ctx );
337
338 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
339 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
340
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200341 if( strcmp( name, "NULL" ) == 0 )
342 name = NULL;
343
Gilles Peskineef86ab22017-05-05 18:59:02 +0200344 ret = mbedtls_x509_crt_verify_with_profile( &crt, &ca, NULL,
345 &compat_profile,
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200346 name, &flags,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200347 verify_print, &vrfy_ctx );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200348
349 TEST_ASSERT( ret == exp_ret );
350 TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
351
352exit:
353 mbedtls_x509_crt_free( &crt );
354 mbedtls_x509_crt_free( &ca );
355}
356/* END_CASE */
357
358/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100359void mbedtls_x509_dn_gets( char * crt_file, char * entity, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000360{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000362 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200363 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000366 memset( buf, 0, 2000 );
367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200369 if( strcmp( entity, "subject" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 res = mbedtls_x509_dn_gets( buf, 2000, &crt.subject );
Paul Bakker33b43f12013-08-20 11:48:36 +0200371 else if( strcmp( entity, "issuer" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 res = mbedtls_x509_dn_gets( buf, 2000, &crt.issuer );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200373 else
374 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000375
376 TEST_ASSERT( res != -1 );
377 TEST_ASSERT( res != -2 );
378
Paul Bakker33b43f12013-08-20 11:48:36 +0200379 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200380
381exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000383}
Paul Bakker33b43f12013-08-20 11:48:36 +0200384/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100387void mbedtls_x509_time_is_past( char * crt_file, char * entity, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000388{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200394
Paul Bakker33b43f12013-08-20 11:48:36 +0200395 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100396 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_from ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200397 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100398 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_to ) == result );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200399 else
400 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000401
Paul Bakkerbd51b262014-07-10 15:26:12 +0200402exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000404}
Paul Bakker33b43f12013-08-20 11:48:36 +0200405/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100408void mbedtls_x509_time_is_future( char * crt_file, char * entity, int result )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100409{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100415
416 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100417 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_from ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100418 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100419 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_to ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100420 else
421 TEST_ASSERT( "Unknown entity" == 0 );
422
Paul Bakkerbd51b262014-07-10 15:26:12 +0200423exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100425}
426/* END_CASE */
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100429void x509parse_crt_file( char * crt_file, int result )
Paul Bakker5a5fa922014-09-26 14:53:04 +0200430{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +0200432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_x509_crt_init( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200436
437exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_x509_crt_free( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200439}
440/* END_CASE */
441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Azim Khand30ca132017-06-09 04:32:58 +0100443void x509parse_crt( HexParam_t * buf, char * result_str, 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
Azim Khand30ca132017-06-09 04:32:58 +0100453 TEST_ASSERT( mbedtls_x509_crt_parse( &crt, buf->x, buf->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 Khand30ca132017-06-09 04:32:58 +0100470void x509parse_crl( HexParam_t * buf, char * result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000471{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000473 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100474 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000477 memset( output, 0, 2000 );
478
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000479
Azim Khand30ca132017-06-09 04:32:58 +0100480 TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf->x, buf->len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200481 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200484
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000485 TEST_ASSERT( res != -1 );
486 TEST_ASSERT( res != -2 );
487
Paul Bakker33b43f12013-08-20 11:48:36 +0200488 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000489 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000490
Paul Bakkerbd51b262014-07-10 15:26:12 +0200491exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000493}
Paul Bakker33b43f12013-08-20 11:48:36 +0200494/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */
Azim Khand30ca132017-06-09 04:32:58 +0100497void mbedtls_x509_csr_parse( HexParam_t * csr_der, char * ref_out, int ref_ret )
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200498{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200500 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200501 int my_ret;
502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200504 memset( my_out, 0, sizeof( my_out ) );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200505
Azim Khand30ca132017-06-09 04:32:58 +0100506 my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der->x, csr_der->len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200507 TEST_ASSERT( my_ret == ref_ret );
508
509 if( ref_ret == 0 )
510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200512 TEST_ASSERT( my_out_len == strlen( ref_out ) );
513 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
514 }
515
Paul Bakkerbd51b262014-07-10 15:26:12 +0200516exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200518}
519/* END_CASE */
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100522void mbedtls_x509_crt_parse_path( char * crt_path, int ret, int nb_crt )
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100523{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100525 int i;
526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_x509_crt_init( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100530
531 /* Check how many certs we got */
532 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
533 if( cur->raw.p != NULL )
534 i++;
535
536 TEST_ASSERT( i == nb_crt );
537
Paul Bakkerbd51b262014-07-10 15:26:12 +0200538exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100540}
541/* END_CASE */
542
Janos Follath822b2c32015-10-11 10:25:22 +0200543/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +0200544void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int,
545 int ret_chk, int flags_chk )
546{
547 char file_buf[128];
548 int ret;
549 uint32_t flags;
550 mbedtls_x509_crt trusted, chain;
551
552 /*
553 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
554 * with NN.crt signed by NN-1.crt
555 */
556
557 mbedtls_x509_crt_init( &trusted );
558 mbedtls_x509_crt_init( &chain );
559
560 /* Load trusted root */
561 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 );
562
563 /* Load a chain with nb_int intermediates (from 01 to nb_int),
564 * plus one "end-entity" cert (nb_int + 1) */
565 ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
566 nb_int + 1 );
567 TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf );
568 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 );
569
570 /* Try to verify that chain */
571 ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags,
572 NULL, NULL );
573 TEST_ASSERT( ret == ret_chk );
574 TEST_ASSERT( flags == (uint32_t) flags_chk );
575
576exit:
577 mbedtls_x509_crt_free( &chain );
578 mbedtls_x509_crt_free( &trusted );
579}
580/* END_CASE */
581
582/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200583void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca,
584 int flags_result, int result,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200585 char *profile_name, int vrfy_fatal_lvls )
Janos Follath822b2c32015-10-11 10:25:22 +0200586{
587 char* act;
588 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200589 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100590 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200591 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +0200592
Janos Follath822b2c32015-10-11 10:25:22 +0200593 mbedtls_x509_crt_init( &chain );
594 mbedtls_x509_crt_init( &trusted );
595
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900596 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100597 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
598 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
Janos Follath822b2c32015-10-11 10:25:22 +0200599
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200600 if( strcmp( profile_name, "" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200601 profile = &mbedtls_x509_crt_profile_default;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200602 else if( strcmp( profile_name, "next" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200603 profile = &mbedtls_x509_crt_profile_next;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200604 else if( strcmp( profile_name, "suiteb" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200605 profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200606 else if( strcmp( profile_name, "rsa3072" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200607 profile = &profile_rsa3072;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200608 else if( strcmp( profile_name, "sha512" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200609 profile = &profile_sha512;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200610
611 res = mbedtls_x509_crt_verify_with_profile( &chain, &trusted, NULL, profile,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200612 NULL, &flags, verify_fatal, &vrfy_fatal_lvls );
Janos Follathef4f2582015-10-11 16:17:27 +0200613
614 TEST_ASSERT( res == ( result ) );
615 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Janos Follath822b2c32015-10-11 10:25:22 +0200616
617exit:
618 mbedtls_x509_crt_free( &trusted );
619 mbedtls_x509_crt_free( &chain );
620}
621/* END_CASE */
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khand30ca132017-06-09 04:32:58 +0100624void x509_oid_desc( HexParam_t * buf, char * ref_desc )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100625{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000627 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000628 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100629
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +0100632 oid.p = buf->x;
633 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100636
637 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000638 {
639 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100640 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000641 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100642 else
643 {
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000644 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100645 TEST_ASSERT( desc != NULL );
646 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
647 }
648}
649/* END_CASE */
650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khand30ca132017-06-09 04:32:58 +0100652void x509_oid_numstr( HexParam_t * oid_buf, char * numstr, int blen, int ret )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100653{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100655 char num_buf[100];
656
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100657 memset( num_buf, 0x2a, sizeof num_buf );
658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +0100660 oid.p = oid_buf->x;
661 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100662
663 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100666
667 if( ret >= 0 )
668 {
669 TEST_ASSERT( num_buf[ret] == 0 );
670 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
671 }
672}
673/* END_CASE */
674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_KEY_USAGE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100676void x509_check_key_usage( char * crt_file, int usage, int ret )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200677{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200685
Paul Bakkerbd51b262014-07-10 15:26:12 +0200686exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200688}
689/* END_CASE */
690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Azim Khand30ca132017-06-09 04:32:58 +0100692void x509_check_extended_key_usage( char * crt_file, HexParam_t * oid, int ret
693 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200694{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200698
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200701
Azim Khand30ca132017-06-09 04:32:58 +0100702 TEST_ASSERT( mbedtls_x509_crt_check_extended_key_usage( &crt, (const char *)oid->x, oid->len ) == ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200703
Paul Bakkerbd51b262014-07-10 15:26:12 +0200704exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200706}
707/* END_CASE */
708
Andres AG4b76aec2016-09-23 13:16:02 +0100709/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100710void x509_get_time( int tag, char * time_str, int ret, int year, int mon,
711 int day, int hour, int min, int sec )
Andres AG4b76aec2016-09-23 13:16:02 +0100712{
713 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +0000714 unsigned char buf[21];
Andres AG4b76aec2016-09-23 13:16:02 +0100715 unsigned char* start = buf;
716 unsigned char* end = buf;
717
718 memset( &time, 0x00, sizeof( time ) );
719 *end = (unsigned char)tag; end++;
Janos Follathea7054a2017-02-08 14:13:02 +0000720 *end = strlen( time_str );
721 TEST_ASSERT( *end < 20 );
Andres AG4b76aec2016-09-23 13:16:02 +0100722 end++;
723 memcpy( end, time_str, (size_t)*(end - 1) );
724 end += *(end - 1);
725
726 TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
727 if( ret == 0 )
728 {
729 TEST_ASSERT( year == time.year );
730 TEST_ASSERT( mon == time.mon );
731 TEST_ASSERT( day == time.day );
732 TEST_ASSERT( hour == time.hour );
733 TEST_ASSERT( min == time.min );
734 TEST_ASSERT( sec == time.sec );
735 }
736}
737/* END_CASE */
738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Azim Khand30ca132017-06-09 04:32:58 +0100740void x509_parse_rsassa_pss_params( HexParam_t * hex_params, int params_tag,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200741 int ref_msg_md, int ref_mgf_md,
742 int ref_salt_len, int ref_ret )
743{
744 int my_ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 mbedtls_x509_buf params;
746 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200747 int my_salt_len;
748
Azim Khand30ca132017-06-09 04:32:58 +0100749 params.p = hex_params->x;
750 params.len = hex_params->len;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200751 params.tag = params_tag;
752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 my_ret = mbedtls_x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200754 &my_salt_len );
755
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200756 TEST_ASSERT( my_ret == ref_ret );
757
758 if( ref_ret == 0 )
759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md );
761 TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200762 TEST_ASSERT( my_salt_len == ref_salt_len );
763 }
764
Paul Bakkerbd51b262014-07-10 15:26:12 +0200765exit:
Azim Khand30ca132017-06-09 04:32:58 +0100766 ;;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200767}
768/* END_CASE */
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100771void x509_selftest( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000772{
Andres AG93012e82016-09-09 09:10:28 +0100773 TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000774}
Paul Bakker33b43f12013-08-20 11:48:36 +0200775/* END_CASE */