blob: de0bc6d55911b5a3762a9e1de785d2573bdfe4f2 [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
Hanno Beckercfa34182019-06-03 14:27:03 +010018/* Test-only profile allowing all digests, PK algorithms, and curves. */
19const mbedtls_x509_crt_profile profile_all =
20{
21 0xFFFFFFFF, /* Any MD */
22 0xFFFFFFFF, /* Any PK alg */
23 0xFFFFFFFF, /* Any curve */
24 1024,
25};
26
Gilles Peskineef86ab22017-05-05 18:59:02 +020027/* Profile for backward compatibility. Allows SHA-1, unlike the default
28 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020029const mbedtls_x509_crt_profile compat_profile =
30{
31 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
32 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) |
33 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
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 0xFFFFFFF, /* Any PK alg */
38 0xFFFFFFF, /* Any curve */
39 1024,
40};
41
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020042const mbedtls_x509_crt_profile profile_rsa3072 =
43{
44 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
45 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
46 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
47 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA ),
48 0,
49 3072,
50};
51
52const mbedtls_x509_crt_profile profile_sha512 =
53{
54 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
55 0xFFFFFFF, /* Any PK alg */
56 0xFFFFFFF, /* Any curve */
57 1024,
58};
59
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020060int verify_none( 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);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010065 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020066
Paul Bakker915275b2012-09-28 07:10:55 +000067 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000068}
69
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020070int verify_all( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000071{
Paul Bakker5a624082011-01-18 16:31:52 +000072 ((void) data);
73 ((void) crt);
74 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000075 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000076
Paul Bakkerb63b0af2011-01-13 17:54:59 +000077 return 0;
78}
79
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +020080int verify_fatal( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
81{
82 int *levels = (int *) data;
83
84 ((void) crt);
85 ((void) certificate_depth);
86
87 /* Simulate a fatal error in the callback */
88 if( *levels & ( 1 << certificate_depth ) )
89 {
90 *flags |= ( 1 << certificate_depth );
Manuel Pégourié-Gonnard41859782017-05-23 12:58:53 +020091 return( -1 - certificate_depth );
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +020092 }
93
94 return( 0 );
95}
96
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +090097/* strsep() not available on Windows */
98char *mystrsep(char **stringp, const char *delim)
99{
100 const char *p;
101 char *ret = *stringp;
102
103 if( *stringp == NULL )
104 return( NULL );
105
106 for( ; ; (*stringp)++ )
107 {
108 if( **stringp == '\0' )
109 {
110 *stringp = NULL;
111 goto done;
112 }
113
114 for( p = delim; *p != '\0'; p++ )
115 if( **stringp == *p )
116 {
117 **stringp = '\0';
118 (*stringp)++;
119 goto done;
120 }
121 }
122
123done:
124 return( ret );
125}
126
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200127#if defined(MBEDTLS_X509_CRT_PARSE_C)
128typedef struct {
129 char buf[512];
130 char *p;
131} verify_print_context;
132
133void verify_print_init( verify_print_context *ctx )
134{
135 memset( ctx, 0, sizeof( verify_print_context ) );
136 ctx->p = ctx->buf;
137}
138
139int verify_print( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
140{
141 int ret;
142 verify_print_context *ctx = (verify_print_context *) data;
143 char *p = ctx->p;
144 size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p;
Hanno Becker5c030582019-02-26 16:45:32 +0000145 mbedtls_x509_crt_frame *frame;
Hanno Becker3f8f0dc2019-02-27 18:06:47 +0000146 mbedtls_x509_name *subject;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200147 ((void) flags);
148
Hanno Becker3f8f0dc2019-02-27 18:06:47 +0000149 ret = mbedtls_x509_crt_get_subject( crt, &subject );
150 if( ret != 0 )
151 return( ret );
152
Hanno Becker5c030582019-02-26 16:45:32 +0000153 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
154 if( ret != 0 )
155 return( ret );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200156
Hanno Becker5c030582019-02-26 16:45:32 +0000157 ret = mbedtls_snprintf( p, n, "depth %d - serial ", certificate_depth );
158 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
159
160 {
161 mbedtls_x509_buf serial;
162 serial.p = frame->serial.p;
163 serial.len = frame->serial.len;
164 ret = mbedtls_x509_serial_gets( p, n, &serial );
165 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
166 }
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200167
168 ret = mbedtls_snprintf( p, n, " - subject " );
Hanno Becker5c030582019-02-26 16:45:32 +0000169 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200170
Hanno Becker3f8f0dc2019-02-27 18:06:47 +0000171 ret = mbedtls_x509_dn_gets( p, n, subject );
Hanno Becker5c030582019-02-26 16:45:32 +0000172 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200173
Manuel Pégourié-Gonnardbe2f0b52017-08-21 11:00:22 +0200174 ret = mbedtls_snprintf( p, n, " - flags 0x%08x\n", *flags );
Hanno Becker5c030582019-02-26 16:45:32 +0000175 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200176
177 ctx->p = p;
178
Hanno Becker5c030582019-02-26 16:45:32 +0000179cleanup:
180
Hanno Becker3f8f0dc2019-02-27 18:06:47 +0000181 mbedtls_x509_name_free( subject );
Hanno Becker5c030582019-02-26 16:45:32 +0000182 mbedtls_x509_crt_frame_release( crt, frame );
183
184 if( ret < 0 )
185 return( ret );
186
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200187 return( 0 );
188}
189#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200190/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000191
Paul Bakker33b43f12013-08-20 11:48:36 +0200192/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200194 * END_DEPENDENCIES
195 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000196
Hanno Becker02a21932019-06-10 15:08:43 +0100197/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100198void x509_cert_info( char * crt_file, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000199{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000201 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000202 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000205 memset( buf, 0, 2000 );
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
208 res = mbedtls_x509_crt_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000209
210 TEST_ASSERT( res != -1 );
211 TEST_ASSERT( res != -2 );
212
Paul Bakker33b43f12013-08-20 11:48:36 +0200213 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200214
215exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000217}
Paul Bakker33b43f12013-08-20 11:48:36 +0200218/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000219
Hanno Becker02a21932019-06-10 15:08:43 +0100220/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100221void mbedtls_x509_crl_info( char * crl_file, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000222{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000224 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000225 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000228 memset( buf, 0, 2000 );
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
231 res = mbedtls_x509_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000232
233 TEST_ASSERT( res != -1 );
234 TEST_ASSERT( res != -2 );
235
Paul Bakker33b43f12013-08-20 11:48:36 +0200236 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200237
238exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000240}
Paul Bakker33b43f12013-08-20 11:48:36 +0200241/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000242
Andres AGa39db392016-12-08 17:10:38 +0000243/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100244void mbedtls_x509_crl_parse( char * crl_file, int result )
Andres AGa39db392016-12-08 17:10:38 +0000245{
246 mbedtls_x509_crl crl;
247 char buf[2000];
248
249 mbedtls_x509_crl_init( &crl );
250 memset( buf, 0, 2000 );
251
252 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == result );
253
254exit:
255 mbedtls_x509_crl_free( &crl );
256}
257/* END_CASE */
258
Hanno Becker02a21932019-06-10 15:08:43 +0100259/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100260void mbedtls_x509_csr_info( char * csr_file, char * result_str )
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100261{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100263 char buf[2000];
264 int res;
265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100267 memset( buf, 0, 2000 );
268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 TEST_ASSERT( mbedtls_x509_csr_parse_file( &csr, csr_file ) == 0 );
270 res = mbedtls_x509_csr_info( buf, 2000, "", &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100271
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100272 TEST_ASSERT( res != -1 );
273 TEST_ASSERT( res != -2 );
274
275 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200276
277exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100279}
280/* END_CASE */
281
Hanno Becker02a21932019-06-10 15:08:43 +0100282/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100283void x509_verify_info( int flags, char * prefix, char * result_str )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100284{
285 char buf[2000];
286 int res;
287
288 memset( buf, 0, sizeof( buf ) );
289
290 res = mbedtls_x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
291
292 TEST_ASSERT( res >= 0 );
293
294 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
295}
296/* END_CASE */
297
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200298/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_C */
299void x509_verify_restart( char *crt_file, char *ca_file,
300 int result, int flags_result,
301 int max_ops, int min_restart, int max_restart )
302{
303 int ret, cnt_restart;
304 mbedtls_x509_crt_restart_ctx rs_ctx;
305 mbedtls_x509_crt crt;
306 mbedtls_x509_crt ca;
307 uint32_t flags = 0;
308
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200309 /*
310 * See comments on ecp_test_vect_restart() for op count precision.
311 *
312 * For reference, with mbed TLS 2.6 and default settings:
313 * - ecdsa_verify() for P-256: ~ 6700
314 * - ecdsa_verify() for P-384: ~ 18800
315 * - x509_verify() for server5 -> test-ca2: ~ 18800
316 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
317 */
318
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200319 mbedtls_x509_crt_restart_init( &rs_ctx );
320 mbedtls_x509_crt_init( &crt );
321 mbedtls_x509_crt_init( &ca );
322
323 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
324 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
325
326 mbedtls_ecp_set_max_ops( max_ops );
327
328 cnt_restart = 0;
329 do {
330 ret = mbedtls_x509_crt_verify_restartable( &crt, &ca, NULL,
331 &mbedtls_x509_crt_profile_default, NULL, &flags,
332 NULL, NULL, &rs_ctx );
333 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
334
335 TEST_ASSERT( ret == result );
336 TEST_ASSERT( flags == (uint32_t) flags_result );
337
338 TEST_ASSERT( cnt_restart >= min_restart );
339 TEST_ASSERT( cnt_restart <= max_restart );
340
341 /* Do we leak memory when aborting? */
342 ret = mbedtls_x509_crt_verify_restartable( &crt, &ca, NULL,
343 &mbedtls_x509_crt_profile_default, NULL, &flags,
344 NULL, NULL, &rs_ctx );
345 TEST_ASSERT( ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
346
347exit:
348 mbedtls_x509_crt_restart_free( &rs_ctx );
349 mbedtls_x509_crt_free( &crt );
350 mbedtls_x509_crt_free( &ca );
351}
352/* END_CASE */
353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200355void x509_verify( char *crt_file, char *ca_file, char *crl_file,
356 char *cn_name_str, int result, int flags_result,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200357 char *profile_str,
Paul Bakker33b43f12013-08-20 11:48:36 +0200358 char *verify_callback )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000359{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_x509_crt crt;
361 mbedtls_x509_crt ca;
362 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200363 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000364 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200365 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200366 char * cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200367 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_x509_crt_init( &crt );
370 mbedtls_x509_crt_init( &ca );
371 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000372
Paul Bakker33b43f12013-08-20 11:48:36 +0200373 if( strcmp( cn_name_str, "NULL" ) != 0 )
374 cn_name = cn_name_str;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200375
Manuel Pégourié-Gonnarda54f6cc2017-08-09 10:41:42 +0200376 if( strcmp( profile_str, "" ) == 0 )
Gilles Peskineef86ab22017-05-05 18:59:02 +0200377 profile = &mbedtls_x509_crt_profile_default;
Ron Eldorc1539982018-02-06 18:47:17 +0200378 else if( strcmp( profile_str, "next" ) == 0 )
379 profile = &mbedtls_x509_crt_profile_next;
380 else if( strcmp( profile_str, "suite_b" ) == 0 )
381 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200382 else if( strcmp( profile_str, "compat" ) == 0 )
383 profile = &compat_profile;
Hanno Beckercfa34182019-06-03 14:27:03 +0100384 else if( strcmp( profile_str, "all" ) == 0 )
385 profile = &profile_all;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200386 else
387 TEST_ASSERT( "Unknown algorithm profile" == 0 );
388
Paul Bakker33b43f12013-08-20 11:48:36 +0200389 if( strcmp( verify_callback, "NULL" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200390 f_vrfy = NULL;
Paul Bakker33b43f12013-08-20 11:48:36 +0200391 else if( strcmp( verify_callback, "verify_none" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200392 f_vrfy = verify_none;
Paul Bakker33b43f12013-08-20 11:48:36 +0200393 else if( strcmp( verify_callback, "verify_all" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200394 f_vrfy = verify_all;
395 else
396 TEST_ASSERT( "No known verify callback selected" == 0 );
397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
399 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
400 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000401
Gilles Peskineef86ab22017-05-05 18:59:02 +0200402 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 +0200403
Paul Bakkerbd51b262014-07-10 15:26:12 +0200404 TEST_ASSERT( res == ( result ) );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200405 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200406
407exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 mbedtls_x509_crt_free( &crt );
409 mbedtls_x509_crt_free( &ca );
410 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000411}
Paul Bakker33b43f12013-08-20 11:48:36 +0200412/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200415void x509_verify_callback( char *crt_file, char *ca_file, char *name,
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200416 int exp_ret, char *exp_vrfy_out )
417{
418 int ret;
419 mbedtls_x509_crt crt;
420 mbedtls_x509_crt ca;
421 uint32_t flags = 0;
422 verify_print_context vrfy_ctx;
423
424 mbedtls_x509_crt_init( &crt );
425 mbedtls_x509_crt_init( &ca );
426 verify_print_init( &vrfy_ctx );
427
428 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
429 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
430
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200431 if( strcmp( name, "NULL" ) == 0 )
432 name = NULL;
433
Gilles Peskineef86ab22017-05-05 18:59:02 +0200434 ret = mbedtls_x509_crt_verify_with_profile( &crt, &ca, NULL,
435 &compat_profile,
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200436 name, &flags,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200437 verify_print, &vrfy_ctx );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200438
439 TEST_ASSERT( ret == exp_ret );
440 TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
441
442exit:
443 mbedtls_x509_crt_free( &crt );
444 mbedtls_x509_crt_free( &ca );
445}
446/* END_CASE */
447
Hanno Becker02a21932019-06-10 15:08:43 +0100448/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100449void mbedtls_x509_dn_gets( char * crt_file, char * entity, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000450{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000452 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200453 int res = 0;
Hanno Beckerc69c4462019-02-27 09:05:41 +0000454 mbedtls_x509_name *subject = NULL, *issuer = NULL;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000457 memset( buf, 0, 2000 );
458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Hanno Beckerc69c4462019-02-27 09:05:41 +0000460 TEST_ASSERT( mbedtls_x509_crt_get_subject( &crt, &subject ) == 0 );
461 TEST_ASSERT( mbedtls_x509_crt_get_issuer( &crt, &issuer ) == 0 );
462
Paul Bakker33b43f12013-08-20 11:48:36 +0200463 if( strcmp( entity, "subject" ) == 0 )
Hanno Beckerc69c4462019-02-27 09:05:41 +0000464 res = mbedtls_x509_dn_gets( buf, 2000, subject );
Paul Bakker33b43f12013-08-20 11:48:36 +0200465 else if( strcmp( entity, "issuer" ) == 0 )
Hanno Beckerc69c4462019-02-27 09:05:41 +0000466 res = mbedtls_x509_dn_gets( buf, 2000, issuer );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200467 else
468 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000469
470 TEST_ASSERT( res != -1 );
471 TEST_ASSERT( res != -2 );
472
Paul Bakker33b43f12013-08-20 11:48:36 +0200473 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200474
475exit:
Hanno Beckerc69c4462019-02-27 09:05:41 +0000476 mbedtls_x509_name_free( issuer );
477 mbedtls_x509_name_free( subject );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000479}
Paul Bakker33b43f12013-08-20 11:48:36 +0200480/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100483void mbedtls_x509_time_is_past( char * crt_file, char * entity, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000484{
Hanno Beckerc69c4462019-02-27 09:05:41 +0000485 mbedtls_x509_crt crt;
486 mbedtls_x509_crt_frame frame;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Hanno Beckerc69c4462019-02-27 09:05:41 +0000491 TEST_ASSERT( mbedtls_x509_crt_get_frame( &crt, &frame ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200492
Paul Bakker33b43f12013-08-20 11:48:36 +0200493 if( strcmp( entity, "valid_from" ) == 0 )
Hanno Beckerc69c4462019-02-27 09:05:41 +0000494 TEST_ASSERT( mbedtls_x509_time_is_past( &frame.valid_from ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200495 else if( strcmp( entity, "valid_to" ) == 0 )
Hanno Beckerc69c4462019-02-27 09:05:41 +0000496 TEST_ASSERT( mbedtls_x509_time_is_past( &frame.valid_to ) == result );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200497 else
498 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000499
Paul Bakkerbd51b262014-07-10 15:26:12 +0200500exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000502}
Paul Bakker33b43f12013-08-20 11:48:36 +0200503/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100506void mbedtls_x509_time_is_future( char * crt_file, char * entity, int result )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100507{
Hanno Beckerc69c4462019-02-27 09:05:41 +0000508 mbedtls_x509_crt crt;
509 mbedtls_x509_crt_frame frame;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Hanno Beckerc69c4462019-02-27 09:05:41 +0000514 TEST_ASSERT( mbedtls_x509_crt_get_frame( &crt, &frame ) == 0 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100515
516 if( strcmp( entity, "valid_from" ) == 0 )
Hanno Beckerc69c4462019-02-27 09:05:41 +0000517 TEST_ASSERT( mbedtls_x509_time_is_future( &frame.valid_from ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100518 else if( strcmp( entity, "valid_to" ) == 0 )
Hanno Beckerc69c4462019-02-27 09:05:41 +0000519 TEST_ASSERT( mbedtls_x509_time_is_future( &frame.valid_to ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100520 else
521 TEST_ASSERT( "Unknown entity" == 0 );
522
Paul Bakkerbd51b262014-07-10 15:26:12 +0200523exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100525}
526/* END_CASE */
527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100529void x509parse_crt_file( char * crt_file, int result )
Paul Bakker5a5fa922014-09-26 14:53:04 +0200530{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +0200532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 mbedtls_x509_crt_init( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200536
537exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_x509_crt_free( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200539}
540/* END_CASE */
541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100543void x509parse_crt( data_t * buf, char * result_str, int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000544{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_x509_crt crt;
Hanno Becker02a21932019-06-10 15:08:43 +0100546#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000547 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100548 int res;
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600549#endif
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_x509_crt_init( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000552
Hanno Becker2fa5e732019-01-31 09:15:53 +0000553 TEST_ASSERT( mbedtls_x509_crt_parse_der( &crt, buf->x, buf->len ) == ( result ) );
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600554#if defined(MBEDTLS_X509_INFO)
Hanno Becker2fa5e732019-01-31 09:15:53 +0000555 if( ( result ) == 0 )
556 {
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600557 memset( output, 0, 2000 );
Hanno Becker2fa5e732019-01-31 09:15:53 +0000558 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000559
Hanno Becker2fa5e732019-01-31 09:15:53 +0000560 TEST_ASSERT( res != -1 );
561 TEST_ASSERT( res != -2 );
562
563 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
564 }
Hanno Becker98f85c82019-06-11 17:16:13 +0100565#else
566 ((void) result_str);
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600567#endif
Hanno Becker2fa5e732019-01-31 09:15:53 +0000568
569 mbedtls_x509_crt_free( &crt );
570 mbedtls_x509_crt_init( &crt );
Hanno Becker2fa5e732019-01-31 09:15:53 +0000571
572 TEST_ASSERT( mbedtls_x509_crt_parse_der_nocopy( &crt, buf->x, buf->len ) == ( result ) );
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600573
574 TEST_ASSERT( mbedtls_x509_crt_parse( &crt, buf->x, buf->len ) == ( result ) );
Hanno Becker02a21932019-06-10 15:08:43 +0100575#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker33b43f12013-08-20 11:48:36 +0200576 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000577 {
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600578 memset( output, 0, 2000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200580
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000581 TEST_ASSERT( res != -1 );
582 TEST_ASSERT( res != -2 );
583
Paul Bakker33b43f12013-08-20 11:48:36 +0200584 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000585 }
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600586#endif
587
588 mbedtls_x509_crt_free( &crt );
589 mbedtls_x509_crt_init( &crt );
Hanno Becker02a21932019-06-10 15:08:43 +0100590#if !defined(MBEDTLS_X509_REMOVE_INFO)
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600591 memset( output, 0, 2000 );
592#endif
Paul Bakkerb08e6842012-02-11 18:43:20 +0000593
Paul Bakkerbd51b262014-07-10 15:26:12 +0200594exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000596}
Paul Bakker33b43f12013-08-20 11:48:36 +0200597/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000598
Hanno Becker02a21932019-06-10 15:08:43 +0100599/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khan5fcca462018-06-29 11:05:32 +0100600void x509parse_crl( data_t * buf, char * result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000601{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000603 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100604 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000607 memset( output, 0, 2000 );
608
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000609
Azim Khand30ca132017-06-09 04:32:58 +0100610 TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf->x, buf->len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200611 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200614
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000615 TEST_ASSERT( res != -1 );
616 TEST_ASSERT( res != -2 );
617
Paul Bakker33b43f12013-08-20 11:48:36 +0200618 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000619 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000620
Paul Bakkerbd51b262014-07-10 15:26:12 +0200621exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000623}
Paul Bakker33b43f12013-08-20 11:48:36 +0200624/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000625
Hanno Becker02a21932019-06-10 15:08:43 +0100626/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khan5fcca462018-06-29 11:05:32 +0100627void mbedtls_x509_csr_parse( data_t * csr_der, char * ref_out, int ref_ret )
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200628{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200630 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200631 int my_ret;
632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200634 memset( my_out, 0, sizeof( my_out ) );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200635
Azim Khand30ca132017-06-09 04:32:58 +0100636 my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der->x, csr_der->len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200637 TEST_ASSERT( my_ret == ref_ret );
638
639 if( ref_ret == 0 )
640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200642 TEST_ASSERT( my_out_len == strlen( ref_out ) );
643 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
644 }
645
Paul Bakkerbd51b262014-07-10 15:26:12 +0200646exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200648}
649/* END_CASE */
650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100652void mbedtls_x509_crt_parse_path( char * crt_path, int ret, int nb_crt )
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100653{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100655 int i;
656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_x509_crt_init( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100660
661 /* Check how many certs we got */
662 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
663 if( cur->raw.p != NULL )
664 i++;
665
666 TEST_ASSERT( i == nb_crt );
667
Paul Bakkerbd51b262014-07-10 15:26:12 +0200668exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100670}
671/* END_CASE */
672
Janos Follath822b2c32015-10-11 10:25:22 +0200673/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +0200674void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int,
675 int ret_chk, int flags_chk )
676{
677 char file_buf[128];
678 int ret;
679 uint32_t flags;
680 mbedtls_x509_crt trusted, chain;
681
682 /*
683 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
684 * with NN.crt signed by NN-1.crt
685 */
686
687 mbedtls_x509_crt_init( &trusted );
688 mbedtls_x509_crt_init( &chain );
689
690 /* Load trusted root */
691 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 );
692
693 /* Load a chain with nb_int intermediates (from 01 to nb_int),
694 * plus one "end-entity" cert (nb_int + 1) */
695 ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
696 nb_int + 1 );
697 TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf );
698 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 );
699
700 /* Try to verify that chain */
701 ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags,
702 NULL, NULL );
703 TEST_ASSERT( ret == ret_chk );
704 TEST_ASSERT( flags == (uint32_t) flags_chk );
705
706exit:
707 mbedtls_x509_crt_free( &chain );
708 mbedtls_x509_crt_free( &trusted );
709}
710/* END_CASE */
711
712/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200713void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca,
714 int flags_result, int result,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200715 char *profile_name, int vrfy_fatal_lvls )
Janos Follath822b2c32015-10-11 10:25:22 +0200716{
717 char* act;
718 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200719 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100720 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200721 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +0200722
Janos Follath822b2c32015-10-11 10:25:22 +0200723 mbedtls_x509_crt_init( &chain );
724 mbedtls_x509_crt_init( &trusted );
725
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900726 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100727 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
728 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
Janos Follath822b2c32015-10-11 10:25:22 +0200729
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200730 if( strcmp( profile_name, "" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200731 profile = &mbedtls_x509_crt_profile_default;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200732 else if( strcmp( profile_name, "next" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200733 profile = &mbedtls_x509_crt_profile_next;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200734 else if( strcmp( profile_name, "suiteb" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200735 profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200736 else if( strcmp( profile_name, "rsa3072" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200737 profile = &profile_rsa3072;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200738 else if( strcmp( profile_name, "sha512" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200739 profile = &profile_sha512;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200740
741 res = mbedtls_x509_crt_verify_with_profile( &chain, &trusted, NULL, profile,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200742 NULL, &flags, verify_fatal, &vrfy_fatal_lvls );
Janos Follathef4f2582015-10-11 16:17:27 +0200743
744 TEST_ASSERT( res == ( result ) );
745 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Janos Follath822b2c32015-10-11 10:25:22 +0200746
747exit:
748 mbedtls_x509_crt_free( &trusted );
749 mbedtls_x509_crt_free( &chain );
750}
751/* END_CASE */
752
Hanno Becker02a21932019-06-10 15:08:43 +0100753/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khan5fcca462018-06-29 11:05:32 +0100754void x509_oid_desc( data_t * buf, char * ref_desc )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100755{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000757 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000758 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100759
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +0100762 oid.p = buf->x;
763 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100766
767 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000768 {
769 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100770 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000771 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100772 else
773 {
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000774 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100775 TEST_ASSERT( desc != NULL );
776 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
777 }
778}
779/* END_CASE */
780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100782void x509_oid_numstr( data_t * oid_buf, char * numstr, int blen, int ret )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100783{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100785 char num_buf[100];
786
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100787 memset( num_buf, 0x2a, sizeof num_buf );
788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +0100790 oid.p = oid_buf->x;
791 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100792
793 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100796
797 if( ret >= 0 )
798 {
799 TEST_ASSERT( num_buf[ret] == 0 );
800 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
801 }
802}
803/* END_CASE */
804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_KEY_USAGE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100806void x509_check_key_usage( char * crt_file, int usage, int ret )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200807{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200815
Paul Bakkerbd51b262014-07-10 15:26:12 +0200816exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200818}
819/* END_CASE */
820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Azim Khan5fcca462018-06-29 11:05:32 +0100822void x509_check_extended_key_usage( char * crt_file, data_t * oid, int ret
Azim Khand30ca132017-06-09 04:32:58 +0100823 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200824{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200828
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200831
Azim Khand30ca132017-06-09 04:32:58 +0100832 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 +0200833
Paul Bakkerbd51b262014-07-10 15:26:12 +0200834exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200836}
837/* END_CASE */
838
Andres AG4b76aec2016-09-23 13:16:02 +0100839/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100840void x509_get_time( int tag, char * time_str, int ret, int year, int mon,
841 int day, int hour, int min, int sec )
Andres AG4b76aec2016-09-23 13:16:02 +0100842{
843 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +0000844 unsigned char buf[21];
Andres AG4b76aec2016-09-23 13:16:02 +0100845 unsigned char* start = buf;
846 unsigned char* end = buf;
847
848 memset( &time, 0x00, sizeof( time ) );
849 *end = (unsigned char)tag; end++;
Janos Follathea7054a2017-02-08 14:13:02 +0000850 *end = strlen( time_str );
851 TEST_ASSERT( *end < 20 );
Andres AG4b76aec2016-09-23 13:16:02 +0100852 end++;
853 memcpy( end, time_str, (size_t)*(end - 1) );
854 end += *(end - 1);
855
856 TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
857 if( ret == 0 )
858 {
859 TEST_ASSERT( year == time.year );
860 TEST_ASSERT( mon == time.mon );
861 TEST_ASSERT( day == time.day );
862 TEST_ASSERT( hour == time.hour );
863 TEST_ASSERT( min == time.min );
864 TEST_ASSERT( sec == time.sec );
865 }
866}
867/* END_CASE */
868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Azim Khan5fcca462018-06-29 11:05:32 +0100870void x509_parse_rsassa_pss_params( data_t * hex_params, int params_tag,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200871 int ref_msg_md, int ref_mgf_md,
872 int ref_salt_len, int ref_ret )
873{
874 int my_ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 mbedtls_x509_buf params;
876 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200877 int my_salt_len;
878
Azim Khand30ca132017-06-09 04:32:58 +0100879 params.p = hex_params->x;
880 params.len = hex_params->len;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200881 params.tag = params_tag;
882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 my_ret = mbedtls_x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200884 &my_salt_len );
885
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200886 TEST_ASSERT( my_ret == ref_ret );
887
888 if( ref_ret == 0 )
889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md );
891 TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200892 TEST_ASSERT( my_salt_len == ref_salt_len );
893 }
894
Paul Bakkerbd51b262014-07-10 15:26:12 +0200895exit:
Azim Khand30ca132017-06-09 04:32:58 +0100896 ;;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200897}
898/* END_CASE */
899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100901void x509_selftest( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000902{
Andres AG93012e82016-09-09 09:10:28 +0100903 TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000904}
Paul Bakker33b43f12013-08-20 11:48:36 +0200905/* END_CASE */