blob: 584ee822b6c9438f944900339b80e75cfb5ea6c5 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Andres AG4b76aec2016-09-23 13:16:02 +01002#include "mbedtls/x509.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00003#include "mbedtls/x509_crt.h"
4#include "mbedtls/x509_crl.h"
5#include "mbedtls/x509_csr.h"
6#include "mbedtls/pem.h"
7#include "mbedtls/oid.h"
8#include "mbedtls/base64.h"
Paul Bakkerb63b0af2011-01-13 17:54:59 +00009
Simon Butcherefdfeeb2017-07-28 12:15:13 +010010#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
Hanno Becker3b1422e2017-07-26 13:38:02 +010011#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
12than the current threshold 19. To test larger values, please \
13adapt the script tests/data_files/dir-max/long.sh."
14#endif
15
Hanno Becker958c4112019-06-03 14:27:03 +010016/* Test-only profile allowing all digests, PK algorithms, and curves. */
17const mbedtls_x509_crt_profile profile_all =
18{
19 0xFFFFFFFF, /* Any MD */
20 0xFFFFFFFF, /* Any PK alg */
21 0xFFFFFFFF, /* Any curve */
22 1024,
23};
24
Gilles Peskineef86ab22017-05-05 18:59:02 +020025/* Profile for backward compatibility. Allows SHA-1, unlike the default
26 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020027const mbedtls_x509_crt_profile compat_profile =
28{
29 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
30 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) |
31 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
32 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
33 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
34 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
35 0xFFFFFFF, /* Any PK alg */
36 0xFFFFFFF, /* Any curve */
37 1024,
38};
39
Manuel Pégourié-Gonnard189bb402017-05-23 11:29:29 +020040const mbedtls_x509_crt_profile profile_rsa3072 =
41{
42 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
43 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
44 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
45 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA ),
46 0,
47 3072,
48};
49
50const mbedtls_x509_crt_profile profile_sha512 =
51{
52 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
53 0xFFFFFFF, /* Any PK alg */
54 0xFFFFFFF, /* Any curve */
55 1024,
56};
57
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020058int verify_none( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000059{
Paul Bakker5a624082011-01-18 16:31:52 +000060 ((void) data);
61 ((void) crt);
62 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010063 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020064
Paul Bakker915275b2012-09-28 07:10:55 +000065 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000066}
67
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020068int verify_all( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000069{
Paul Bakker5a624082011-01-18 16:31:52 +000070 ((void) data);
71 ((void) crt);
72 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000073 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000074
Paul Bakkerb63b0af2011-01-13 17:54:59 +000075 return 0;
76}
77
Manuel Pégourié-Gonnard9cca2672017-05-23 12:26:58 +020078int verify_fatal( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
79{
80 int *levels = (int *) data;
81
82 ((void) crt);
83 ((void) certificate_depth);
84
85 /* Simulate a fatal error in the callback */
86 if( *levels & ( 1 << certificate_depth ) )
87 {
88 *flags |= ( 1 << certificate_depth );
Manuel Pégourié-Gonnardb0ef3e22017-05-23 12:58:53 +020089 return( -1 - certificate_depth );
Manuel Pégourié-Gonnard9cca2672017-05-23 12:26:58 +020090 }
91
92 return( 0 );
93}
94
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +090095/* strsep() not available on Windows */
96char *mystrsep(char **stringp, const char *delim)
97{
98 const char *p;
99 char *ret = *stringp;
100
101 if( *stringp == NULL )
102 return( NULL );
103
104 for( ; ; (*stringp)++ )
105 {
106 if( **stringp == '\0' )
107 {
108 *stringp = NULL;
109 goto done;
110 }
111
112 for( p = delim; *p != '\0'; p++ )
113 if( **stringp == *p )
114 {
115 **stringp = '\0';
116 (*stringp)++;
117 goto done;
118 }
119 }
120
121done:
122 return( ret );
123}
124
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200125#if defined(MBEDTLS_X509_CRT_PARSE_C)
126typedef struct {
127 char buf[512];
128 char *p;
129} verify_print_context;
130
131void verify_print_init( verify_print_context *ctx )
132{
133 memset( ctx, 0, sizeof( verify_print_context ) );
134 ctx->p = ctx->buf;
135}
136
137int verify_print( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
138{
139 int ret;
140 verify_print_context *ctx = (verify_print_context *) data;
141 char *p = ctx->p;
142 size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p;
143 ((void) flags);
144
145 ret = mbedtls_snprintf( p, n, "depth %d - serial ", certificate_depth );
146 MBEDTLS_X509_SAFE_SNPRINTF;
147
148 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
149 MBEDTLS_X509_SAFE_SNPRINTF;
150
151 ret = mbedtls_snprintf( p, n, " - subject " );
152 MBEDTLS_X509_SAFE_SNPRINTF;
153
154 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
155 MBEDTLS_X509_SAFE_SNPRINTF;
156
Manuel Pégourié-Gonnard7c28b562017-08-21 11:00:22 +0200157 ret = mbedtls_snprintf( p, n, " - flags 0x%08x\n", *flags );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200158 MBEDTLS_X509_SAFE_SNPRINTF;
159
160 ctx->p = p;
161
162 return( 0 );
163}
164#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200165/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000166
Paul Bakker33b43f12013-08-20 11:48:36 +0200167/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200169 * END_DEPENDENCIES
170 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200173void x509_cert_info( char *crt_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000174{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000176 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000177 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000180 memset( buf, 0, 2000 );
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
183 res = mbedtls_x509_crt_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000184
185 TEST_ASSERT( res != -1 );
186 TEST_ASSERT( res != -2 );
187
Paul Bakker33b43f12013-08-20 11:48:36 +0200188 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200189
190exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000192}
Paul Bakker33b43f12013-08-20 11:48:36 +0200193/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
196void mbedtls_x509_crl_info( char *crl_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000197{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000199 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000200 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000203 memset( buf, 0, 2000 );
204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
206 res = mbedtls_x509_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000207
208 TEST_ASSERT( res != -1 );
209 TEST_ASSERT( res != -2 );
210
Paul Bakker33b43f12013-08-20 11:48:36 +0200211 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200212
213exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000215}
Paul Bakker33b43f12013-08-20 11:48:36 +0200216/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000217
Andres AGa39db392016-12-08 17:10:38 +0000218/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
219void mbedtls_x509_crl_parse( char *crl_file, int result )
220{
221 mbedtls_x509_crl crl;
222 char buf[2000];
223
224 mbedtls_x509_crl_init( &crl );
225 memset( buf, 0, 2000 );
226
227 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == result );
228
229exit:
230 mbedtls_x509_crl_free( &crl );
231}
232/* END_CASE */
233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C */
235void mbedtls_x509_csr_info( char *csr_file, char *result_str )
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100236{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100238 char buf[2000];
239 int res;
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100242 memset( buf, 0, 2000 );
243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 TEST_ASSERT( mbedtls_x509_csr_parse_file( &csr, csr_file ) == 0 );
245 res = mbedtls_x509_csr_info( buf, 2000, "", &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100246
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100247 TEST_ASSERT( res != -1 );
248 TEST_ASSERT( res != -2 );
249
250 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200251
252exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100254}
255/* END_CASE */
256
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100257/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
258void x509_verify_info( int flags, char *prefix, char *result_str )
259{
260 char buf[2000];
261 int res;
262
263 memset( buf, 0, sizeof( buf ) );
264
265 res = mbedtls_x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
266
267 TEST_ASSERT( res >= 0 );
268
269 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
270}
271/* END_CASE */
272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200274void x509_verify( char *crt_file, char *ca_file, char *crl_file,
275 char *cn_name_str, int result, int flags_result,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200276 char *profile_str,
Paul Bakker33b43f12013-08-20 11:48:36 +0200277 char *verify_callback )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000278{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_x509_crt crt;
280 mbedtls_x509_crt ca;
281 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200282 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000283 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200284 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200285 char * cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200286 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_x509_crt_init( &crt );
289 mbedtls_x509_crt_init( &ca );
290 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000291
Paul Bakker33b43f12013-08-20 11:48:36 +0200292 if( strcmp( cn_name_str, "NULL" ) != 0 )
293 cn_name = cn_name_str;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200294
Manuel Pégourié-Gonnardaefd2dc2017-08-09 10:41:42 +0200295 if( strcmp( profile_str, "" ) == 0 )
Gilles Peskineef86ab22017-05-05 18:59:02 +0200296 profile = &mbedtls_x509_crt_profile_default;
Ron Eldorc1539982018-02-06 18:47:17 +0200297 else if( strcmp( profile_str, "next" ) == 0 )
298 profile = &mbedtls_x509_crt_profile_next;
299 else if( strcmp( profile_str, "suite_b" ) == 0 )
300 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200301 else if( strcmp( profile_str, "compat" ) == 0 )
302 profile = &compat_profile;
Hanno Becker958c4112019-06-03 14:27:03 +0100303 else if( strcmp( profile_str, "all" ) == 0 )
304 profile = &profile_all;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200305 else
306 TEST_ASSERT( "Unknown algorithm profile" == 0 );
307
Paul Bakker33b43f12013-08-20 11:48:36 +0200308 if( strcmp( verify_callback, "NULL" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200309 f_vrfy = NULL;
Paul Bakker33b43f12013-08-20 11:48:36 +0200310 else if( strcmp( verify_callback, "verify_none" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200311 f_vrfy = verify_none;
Paul Bakker33b43f12013-08-20 11:48:36 +0200312 else if( strcmp( verify_callback, "verify_all" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200313 f_vrfy = verify_all;
314 else
315 TEST_ASSERT( "No known verify callback selected" == 0 );
316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
318 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
319 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000320
Gilles Peskineef86ab22017-05-05 18:59:02 +0200321 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 +0200322
Paul Bakkerbd51b262014-07-10 15:26:12 +0200323 TEST_ASSERT( res == ( result ) );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200324 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200325
326exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_x509_crt_free( &crt );
328 mbedtls_x509_crt_free( &ca );
329 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000330}
Paul Bakker33b43f12013-08-20 11:48:36 +0200331/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard50fc51a2017-07-05 18:14:38 +0200334void x509_verify_callback( char *crt_file, char *ca_file, char *name,
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200335 int exp_ret, char *exp_vrfy_out )
336{
337 int ret;
338 mbedtls_x509_crt crt;
339 mbedtls_x509_crt ca;
340 uint32_t flags = 0;
341 verify_print_context vrfy_ctx;
342
343 mbedtls_x509_crt_init( &crt );
344 mbedtls_x509_crt_init( &ca );
345 verify_print_init( &vrfy_ctx );
346
347 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
348 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
349
Manuel Pégourié-Gonnard50fc51a2017-07-05 18:14:38 +0200350 if( strcmp( name, "NULL" ) == 0 )
351 name = NULL;
352
Gilles Peskineef86ab22017-05-05 18:59:02 +0200353 ret = mbedtls_x509_crt_verify_with_profile( &crt, &ca, NULL,
354 &compat_profile,
Manuel Pégourié-Gonnard50fc51a2017-07-05 18:14:38 +0200355 name, &flags,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200356 verify_print, &vrfy_ctx );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200357
358 TEST_ASSERT( ret == exp_ret );
359 TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
360
361exit:
362 mbedtls_x509_crt_free( &crt );
363 mbedtls_x509_crt_free( &ca );
364}
365/* END_CASE */
366
367/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368void mbedtls_x509_dn_gets( char *crt_file, char *entity, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000369{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000371 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200372 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000375 memset( buf, 0, 2000 );
376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200378 if( strcmp( entity, "subject" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 res = mbedtls_x509_dn_gets( buf, 2000, &crt.subject );
Paul Bakker33b43f12013-08-20 11:48:36 +0200380 else if( strcmp( entity, "issuer" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 res = mbedtls_x509_dn_gets( buf, 2000, &crt.issuer );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200382 else
383 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000384
385 TEST_ASSERT( res != -1 );
386 TEST_ASSERT( res != -2 );
387
Paul Bakker33b43f12013-08-20 11:48:36 +0200388 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200389
390exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000392}
Paul Bakker33b43f12013-08-20 11:48:36 +0200393/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100396void mbedtls_x509_time_is_past( char *crt_file, char *entity, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000397{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200403
Paul Bakker33b43f12013-08-20 11:48:36 +0200404 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100405 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_from ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200406 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100407 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_to ) == result );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200408 else
409 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000410
Paul Bakkerbd51b262014-07-10 15:26:12 +0200411exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000413}
Paul Bakker33b43f12013-08-20 11:48:36 +0200414/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100417void mbedtls_x509_time_is_future( char *crt_file, char *entity, int result )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100418{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100424
425 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100426 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_from ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100427 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100428 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_to ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100429 else
430 TEST_ASSERT( "Unknown entity" == 0 );
431
Paul Bakkerbd51b262014-07-10 15:26:12 +0200432exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100434}
435/* END_CASE */
436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Paul Bakker5a5fa922014-09-26 14:53:04 +0200438void x509parse_crt_file( char *crt_file, int result )
439{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +0200441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_x509_crt_init( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200445
446exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_x509_crt_free( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200448}
449/* END_CASE */
450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200452void x509parse_crt( char *crt_data, char *result_str, int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000453{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_x509_crt crt;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000455 unsigned char buf[2000];
456 unsigned char output[2000];
457 int data_len, res;
458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_x509_crt_init( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000460 memset( buf, 0, 2000 );
461 memset( output, 0, 2000 );
462
Paul Bakker33b43f12013-08-20 11:48:36 +0200463 data_len = unhexify( buf, crt_data );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 TEST_ASSERT( mbedtls_x509_crt_parse( &crt, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200466 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200469
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000470 TEST_ASSERT( res != -1 );
471 TEST_ASSERT( res != -2 );
472
Paul Bakker33b43f12013-08-20 11:48:36 +0200473 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000474 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000475
Paul Bakkerbd51b262014-07-10 15:26:12 +0200476exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000478}
Paul Bakker33b43f12013-08-20 11:48:36 +0200479/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200482void x509parse_crl( char *crl_data, char *result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000483{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000485 unsigned char buf[2000];
486 unsigned char output[2000];
487 int data_len, res;
488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000490 memset( buf, 0, 2000 );
491 memset( output, 0, 2000 );
492
Paul Bakker33b43f12013-08-20 11:48:36 +0200493 data_len = unhexify( buf, crl_data );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200496 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200499
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000500 TEST_ASSERT( res != -1 );
501 TEST_ASSERT( res != -2 );
502
Paul Bakker33b43f12013-08-20 11:48:36 +0200503 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000504 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000505
Paul Bakkerbd51b262014-07-10 15:26:12 +0200506exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000508}
Paul Bakker33b43f12013-08-20 11:48:36 +0200509/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */
512void mbedtls_x509_csr_parse( char *csr_der_hex, char *ref_out, int ref_ret )
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200513{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_x509_csr csr;
Paul Bakkerbd51b262014-07-10 15:26:12 +0200515 unsigned char *csr_der = NULL;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200516 char my_out[1000];
517 size_t csr_der_len;
518 int my_ret;
519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200521 memset( my_out, 0, sizeof( my_out ) );
522 csr_der = unhexify_alloc( csr_der_hex, &csr_der_len );
523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der, csr_der_len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200525 TEST_ASSERT( my_ret == ref_ret );
526
527 if( ref_ret == 0 )
528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200530 TEST_ASSERT( my_out_len == strlen( ref_out ) );
531 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
532 }
533
Paul Bakkerbd51b262014-07-10 15:26:12 +0200534exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_x509_csr_free( &csr );
536 mbedtls_free( csr_der );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200537}
538/* END_CASE */
539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
541void mbedtls_x509_crt_parse_path( char *crt_path, int ret, int nb_crt )
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100542{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100544 int i;
545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_x509_crt_init( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100549
550 /* Check how many certs we got */
551 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
552 if( cur->raw.p != NULL )
553 i++;
554
555 TEST_ASSERT( i == nb_crt );
556
Paul Bakkerbd51b262014-07-10 15:26:12 +0200557exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100559}
560/* END_CASE */
561
Janos Follath822b2c32015-10-11 10:25:22 +0200562/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +0200563void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int,
564 int ret_chk, int flags_chk )
565{
566 char file_buf[128];
567 int ret;
568 uint32_t flags;
569 mbedtls_x509_crt trusted, chain;
570
571 /*
572 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
573 * with NN.crt signed by NN-1.crt
574 */
575
576 mbedtls_x509_crt_init( &trusted );
577 mbedtls_x509_crt_init( &chain );
578
579 /* Load trusted root */
580 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 );
581
582 /* Load a chain with nb_int intermediates (from 01 to nb_int),
583 * plus one "end-entity" cert (nb_int + 1) */
584 ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
585 nb_int + 1 );
586 TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf );
587 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 );
588
589 /* Try to verify that chain */
590 ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags,
591 NULL, NULL );
592 TEST_ASSERT( ret == ret_chk );
593 TEST_ASSERT( flags == (uint32_t) flags_chk );
594
595exit:
596 mbedtls_x509_crt_free( &chain );
597 mbedtls_x509_crt_free( &trusted );
598}
599/* END_CASE */
600
601/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard7e9709a2017-05-22 12:04:25 +0200602void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca,
603 int flags_result, int result,
Manuel Pégourié-Gonnard9cca2672017-05-23 12:26:58 +0200604 char *profile_name, int vrfy_fatal_lvls )
Janos Follath822b2c32015-10-11 10:25:22 +0200605{
606 char* act;
607 uint32_t flags;
Manuel Pégourié-Gonnard7e9709a2017-05-22 12:04:25 +0200608 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100609 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnard7e9709a2017-05-22 12:04:25 +0200610 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +0200611
Janos Follath822b2c32015-10-11 10:25:22 +0200612 mbedtls_x509_crt_init( &chain );
613 mbedtls_x509_crt_init( &trusted );
614
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900615 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100616 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
617 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
Janos Follath822b2c32015-10-11 10:25:22 +0200618
Manuel Pégourié-Gonnard650780c2017-08-08 11:10:37 +0200619 if( strcmp( profile_name, "" ) == 0 )
Manuel Pégourié-Gonnard7e9709a2017-05-22 12:04:25 +0200620 profile = &mbedtls_x509_crt_profile_default;
Manuel Pégourié-Gonnard650780c2017-08-08 11:10:37 +0200621 else if( strcmp( profile_name, "next" ) == 0 )
Manuel Pégourié-Gonnard7e9709a2017-05-22 12:04:25 +0200622 profile = &mbedtls_x509_crt_profile_next;
Manuel Pégourié-Gonnard650780c2017-08-08 11:10:37 +0200623 else if( strcmp( profile_name, "suiteb" ) == 0 )
Manuel Pégourié-Gonnard7e9709a2017-05-22 12:04:25 +0200624 profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnard650780c2017-08-08 11:10:37 +0200625 else if( strcmp( profile_name, "rsa3072" ) == 0 )
Manuel Pégourié-Gonnard189bb402017-05-23 11:29:29 +0200626 profile = &profile_rsa3072;
Manuel Pégourié-Gonnard650780c2017-08-08 11:10:37 +0200627 else if( strcmp( profile_name, "sha512" ) == 0 )
Manuel Pégourié-Gonnard189bb402017-05-23 11:29:29 +0200628 profile = &profile_sha512;
Manuel Pégourié-Gonnard7e9709a2017-05-22 12:04:25 +0200629
630 res = mbedtls_x509_crt_verify_with_profile( &chain, &trusted, NULL, profile,
Manuel Pégourié-Gonnard9cca2672017-05-23 12:26:58 +0200631 NULL, &flags, verify_fatal, &vrfy_fatal_lvls );
Janos Follathef4f2582015-10-11 16:17:27 +0200632
633 TEST_ASSERT( res == ( result ) );
634 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Janos Follath822b2c32015-10-11 10:25:22 +0200635
636exit:
637 mbedtls_x509_crt_free( &trusted );
638 mbedtls_x509_crt_free( &chain );
639}
640/* END_CASE */
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100643void x509_oid_desc( char *oid_str, char *ref_desc )
644{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000646 const char *desc = NULL;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100647 unsigned char buf[20];
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000648 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100649
650 memset( buf, 0, sizeof buf );
651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 oid.tag = MBEDTLS_ASN1_OID;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100653 oid.len = unhexify( buf, oid_str );
654 oid.p = buf;
655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100657
658 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000659 {
660 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100661 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000662 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100663 else
664 {
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000665 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100666 TEST_ASSERT( desc != NULL );
667 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
668 }
669}
670/* END_CASE */
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100673void x509_oid_numstr( char *oid_str, char *numstr, int blen, int ret )
674{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100676 unsigned char oid_buf[20];
677 char num_buf[100];
678
679 memset( oid_buf, 0x00, sizeof oid_buf );
680 memset( num_buf, 0x2a, sizeof num_buf );
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 oid.tag = MBEDTLS_ASN1_OID;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100683 oid.len = unhexify( oid_buf, oid_str );
684 oid.p = oid_buf;
685
686 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100689
690 if( ret >= 0 )
691 {
692 TEST_ASSERT( num_buf[ret] == 0 );
693 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
694 }
695}
696/* END_CASE */
697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698/* 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 +0200699void x509_check_key_usage( char *crt_file, int usage, int ret )
700{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200708
Paul Bakkerbd51b262014-07-10 15:26:12 +0200709exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200711}
712/* END_CASE */
713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714/* 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 +0200715void x509_check_extended_key_usage( char *crt_file, char *usage_hex, int ret )
716{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200718 char oid[50];
719 size_t len;
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200722
723 len = unhexify( (unsigned char *) oid, usage_hex );
724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 TEST_ASSERT( mbedtls_x509_crt_check_extended_key_usage( &crt, oid, len ) == ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200728
Paul Bakkerbd51b262014-07-10 15:26:12 +0200729exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200731}
732/* END_CASE */
733
Andres AG4b76aec2016-09-23 13:16:02 +0100734/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
735void x509_get_time( int tag, char *time_str, int ret,
736 int year, int mon, int day,
737 int hour, int min, int sec )
738{
739 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +0000740 unsigned char buf[21];
Andres AG4b76aec2016-09-23 13:16:02 +0100741 unsigned char* start = buf;
742 unsigned char* end = buf;
743
744 memset( &time, 0x00, sizeof( time ) );
745 *end = (unsigned char)tag; end++;
Janos Follathea7054a2017-02-08 14:13:02 +0000746 *end = strlen( time_str );
747 TEST_ASSERT( *end < 20 );
Andres AG4b76aec2016-09-23 13:16:02 +0100748 end++;
749 memcpy( end, time_str, (size_t)*(end - 1) );
750 end += *(end - 1);
751
752 TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
753 if( ret == 0 )
754 {
755 TEST_ASSERT( year == time.year );
756 TEST_ASSERT( mon == time.mon );
757 TEST_ASSERT( day == time.day );
758 TEST_ASSERT( hour == time.hour );
759 TEST_ASSERT( min == time.min );
760 TEST_ASSERT( sec == time.sec );
761 }
762}
763/* END_CASE */
764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200766void x509_parse_rsassa_pss_params( char *hex_params, int params_tag,
767 int ref_msg_md, int ref_mgf_md,
768 int ref_salt_len, int ref_ret )
769{
770 int my_ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_x509_buf params;
772 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200773 int my_salt_len;
774
775 params.p = unhexify_alloc( hex_params, &params.len );
776 params.tag = params_tag;
777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 my_ret = mbedtls_x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200779 &my_salt_len );
780
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200781 TEST_ASSERT( my_ret == ref_ret );
782
783 if( ref_ret == 0 )
784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md );
786 TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200787 TEST_ASSERT( my_salt_len == ref_salt_len );
788 }
789
Paul Bakkerbd51b262014-07-10 15:26:12 +0200790exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_free( params.p );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200792}
793/* END_CASE */
794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200796void x509_selftest()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000797{
Andres AG93012e82016-09-09 09:10:28 +0100798 TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000799}
Paul Bakker33b43f12013-08-20 11:48:36 +0200800/* END_CASE */