blob: 2df187de0ba27704423e8febce215ebc49d0931a [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"
Hanno Beckerf6bc8882019-05-02 13:05:58 +01007#include "mbedtls/x509_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00008#include "mbedtls/pem.h"
9#include "mbedtls/oid.h"
10#include "mbedtls/base64.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +010011#include "string.h"
Paul Bakkerb63b0af2011-01-13 17:54:59 +000012
Simon Butcher9e24b512017-07-28 12:15:13 +010013#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
Hanno Becker3b1422e2017-07-26 13:38:02 +010014#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
15than the current threshold 19. To test larger values, please \
16adapt the script tests/data_files/dir-max/long.sh."
17#endif
18
Hanno Beckercfa34182019-06-03 14:27:03 +010019/* Test-only profile allowing all digests, PK algorithms, and curves. */
20const mbedtls_x509_crt_profile profile_all =
21{
22 0xFFFFFFFF, /* Any MD */
23 0xFFFFFFFF, /* Any PK alg */
24 0xFFFFFFFF, /* Any curve */
25 1024,
26};
27
Gilles Peskineef86ab22017-05-05 18:59:02 +020028/* Profile for backward compatibility. Allows SHA-1, unlike the default
29 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020030const mbedtls_x509_crt_profile compat_profile =
31{
32 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
33 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) |
34 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
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 0xFFFFFFF, /* Any PK alg */
39 0xFFFFFFF, /* Any curve */
40 1024,
41};
42
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020043const mbedtls_x509_crt_profile profile_rsa3072 =
44{
45 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
46 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
47 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
48 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA ),
49 0,
50 3072,
51};
52
53const mbedtls_x509_crt_profile profile_sha512 =
54{
55 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
56 0xFFFFFFF, /* Any PK alg */
57 0xFFFFFFF, /* Any curve */
58 1024,
59};
60
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020061int verify_none( 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);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010066 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020067
Paul Bakker915275b2012-09-28 07:10:55 +000068 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000069}
70
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020071int verify_all( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000072{
Paul Bakker5a624082011-01-18 16:31:52 +000073 ((void) data);
74 ((void) crt);
75 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000076 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000077
Paul Bakkerb63b0af2011-01-13 17:54:59 +000078 return 0;
79}
80
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +020081int verify_fatal( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
82{
83 int *levels = (int *) data;
84
85 ((void) crt);
86 ((void) certificate_depth);
87
88 /* Simulate a fatal error in the callback */
89 if( *levels & ( 1 << certificate_depth ) )
90 {
91 *flags |= ( 1 << certificate_depth );
Manuel Pégourié-Gonnard41859782017-05-23 12:58:53 +020092 return( -1 - certificate_depth );
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +020093 }
94
95 return( 0 );
96}
97
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +090098/* strsep() not available on Windows */
99char *mystrsep(char **stringp, const char *delim)
100{
101 const char *p;
102 char *ret = *stringp;
103
104 if( *stringp == NULL )
105 return( NULL );
106
107 for( ; ; (*stringp)++ )
108 {
109 if( **stringp == '\0' )
110 {
111 *stringp = NULL;
112 goto done;
113 }
114
115 for( p = delim; *p != '\0'; p++ )
116 if( **stringp == *p )
117 {
118 **stringp = '\0';
119 (*stringp)++;
120 goto done;
121 }
122 }
123
124done:
125 return( ret );
126}
127
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200128#if defined(MBEDTLS_X509_CRT_PARSE_C)
129typedef struct {
130 char buf[512];
131 char *p;
132} verify_print_context;
133
134void verify_print_init( verify_print_context *ctx )
135{
136 memset( ctx, 0, sizeof( verify_print_context ) );
137 ctx->p = ctx->buf;
138}
139
140int verify_print( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
141{
142 int ret;
143 verify_print_context *ctx = (verify_print_context *) data;
144 char *p = ctx->p;
145 size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p;
Hanno Becker5f268b32019-05-20 16:26:34 +0100146 mbedtls_x509_crt_frame const *frame;
Hanno Becker3f8f0dc2019-02-27 18:06:47 +0000147 mbedtls_x509_name *subject;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200148 ((void) flags);
149
Hanno Becker3f8f0dc2019-02-27 18:06:47 +0000150 ret = mbedtls_x509_crt_get_subject( crt, &subject );
151 if( ret != 0 )
152 return( ret );
153
Hanno Becker5c030582019-02-26 16:45:32 +0000154 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
155 if( ret != 0 )
156 return( ret );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200157
Hanno Becker5c030582019-02-26 16:45:32 +0000158 ret = mbedtls_snprintf( p, n, "depth %d - serial ", certificate_depth );
Hanno Becker54f1c2c2019-05-13 11:58:47 +0100159 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker5c030582019-02-26 16:45:32 +0000160
161 {
162 mbedtls_x509_buf serial;
163 serial.p = frame->serial.p;
164 serial.len = frame->serial.len;
165 ret = mbedtls_x509_serial_gets( p, n, &serial );
Hanno Becker54f1c2c2019-05-13 11:58:47 +0100166 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker5c030582019-02-26 16:45:32 +0000167 }
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200168
169 ret = mbedtls_snprintf( p, n, " - subject " );
Hanno Becker54f1c2c2019-05-13 11:58:47 +0100170 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200171
Hanno Becker3f8f0dc2019-02-27 18:06:47 +0000172 ret = mbedtls_x509_dn_gets( p, n, subject );
Hanno Becker54f1c2c2019-05-13 11:58:47 +0100173 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200174
Manuel Pégourié-Gonnardbe2f0b52017-08-21 11:00:22 +0200175 ret = mbedtls_snprintf( p, n, " - flags 0x%08x\n", *flags );
Hanno Becker54f1c2c2019-05-13 11:58:47 +0100176 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200177
178 ctx->p = p;
179
Hanno Becker5c030582019-02-26 16:45:32 +0000180cleanup:
181
Hanno Becker3f8f0dc2019-02-27 18:06:47 +0000182 mbedtls_x509_name_free( subject );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000183 mbedtls_x509_crt_frame_release( crt );
Hanno Becker5c030582019-02-26 16:45:32 +0000184
185 if( ret < 0 )
186 return( ret );
187
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200188 return( 0 );
189}
190#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200191/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000192
Paul Bakker33b43f12013-08-20 11:48:36 +0200193/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200195 * END_DEPENDENCIES
196 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000197
Hanno Becker02a21932019-06-10 15:08:43 +0100198/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100199void x509_cert_info( char * crt_file, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000200{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000202 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000203 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000206 memset( buf, 0, 2000 );
207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
209 res = mbedtls_x509_crt_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000210
211 TEST_ASSERT( res != -1 );
212 TEST_ASSERT( res != -2 );
213
Paul Bakker33b43f12013-08-20 11:48:36 +0200214 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200215
216exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000218}
Paul Bakker33b43f12013-08-20 11:48:36 +0200219/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000220
Hanno Becker02a21932019-06-10 15:08:43 +0100221/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100222void mbedtls_x509_crl_info( char * crl_file, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000223{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000225 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000226 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000229 memset( buf, 0, 2000 );
230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
232 res = mbedtls_x509_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000233
234 TEST_ASSERT( res != -1 );
235 TEST_ASSERT( res != -2 );
236
Paul Bakker33b43f12013-08-20 11:48:36 +0200237 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200238
239exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000241}
Paul Bakker33b43f12013-08-20 11:48:36 +0200242/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000243
Andres AGa39db392016-12-08 17:10:38 +0000244/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100245void mbedtls_x509_crl_parse( char * crl_file, int result )
Andres AGa39db392016-12-08 17:10:38 +0000246{
247 mbedtls_x509_crl crl;
248 char buf[2000];
249
250 mbedtls_x509_crl_init( &crl );
251 memset( buf, 0, 2000 );
252
253 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == result );
254
255exit:
256 mbedtls_x509_crl_free( &crl );
257}
258/* END_CASE */
259
Hanno Becker02a21932019-06-10 15:08:43 +0100260/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100261void mbedtls_x509_csr_info( char * csr_file, char * result_str )
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100262{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100264 char buf[2000];
265 int res;
266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100268 memset( buf, 0, 2000 );
269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 TEST_ASSERT( mbedtls_x509_csr_parse_file( &csr, csr_file ) == 0 );
271 res = mbedtls_x509_csr_info( buf, 2000, "", &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100272
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100273 TEST_ASSERT( res != -1 );
274 TEST_ASSERT( res != -2 );
275
276 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200277
278exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100280}
281/* END_CASE */
282
Hanno Becker02a21932019-06-10 15:08:43 +0100283/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100284void x509_verify_info( int flags, char * prefix, char * result_str )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100285{
286 char buf[2000];
287 int res;
288
289 memset( buf, 0, sizeof( buf ) );
290
291 res = mbedtls_x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
292
293 TEST_ASSERT( res >= 0 );
294
295 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
296}
297/* END_CASE */
298
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200299/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_C */
300void x509_verify_restart( char *crt_file, char *ca_file,
301 int result, int flags_result,
302 int max_ops, int min_restart, int max_restart )
303{
304 int ret, cnt_restart;
305 mbedtls_x509_crt_restart_ctx rs_ctx;
306 mbedtls_x509_crt crt;
307 mbedtls_x509_crt ca;
308 uint32_t flags = 0;
309
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200310 /*
311 * See comments on ecp_test_vect_restart() for op count precision.
312 *
313 * For reference, with mbed TLS 2.6 and default settings:
314 * - ecdsa_verify() for P-256: ~ 6700
315 * - ecdsa_verify() for P-384: ~ 18800
316 * - x509_verify() for server5 -> test-ca2: ~ 18800
317 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
318 */
319
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200320 mbedtls_x509_crt_restart_init( &rs_ctx );
321 mbedtls_x509_crt_init( &crt );
322 mbedtls_x509_crt_init( &ca );
323
324 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
325 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
326
327 mbedtls_ecp_set_max_ops( max_ops );
328
329 cnt_restart = 0;
330 do {
331 ret = mbedtls_x509_crt_verify_restartable( &crt, &ca, NULL,
332 &mbedtls_x509_crt_profile_default, NULL, &flags,
333 NULL, NULL, &rs_ctx );
334 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
335
336 TEST_ASSERT( ret == result );
337 TEST_ASSERT( flags == (uint32_t) flags_result );
338
339 TEST_ASSERT( cnt_restart >= min_restart );
340 TEST_ASSERT( cnt_restart <= max_restart );
341
342 /* Do we leak memory when aborting? */
343 ret = mbedtls_x509_crt_verify_restartable( &crt, &ca, NULL,
344 &mbedtls_x509_crt_profile_default, NULL, &flags,
345 NULL, NULL, &rs_ctx );
346 TEST_ASSERT( ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
347
348exit:
349 mbedtls_x509_crt_restart_free( &rs_ctx );
350 mbedtls_x509_crt_free( &crt );
351 mbedtls_x509_crt_free( &ca );
352}
353/* END_CASE */
354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200356void x509_verify( char *crt_file, char *ca_file, char *crl_file,
357 char *cn_name_str, int result, int flags_result,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200358 char *profile_str,
Paul Bakker33b43f12013-08-20 11:48:36 +0200359 char *verify_callback )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000360{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 mbedtls_x509_crt crt;
362 mbedtls_x509_crt ca;
363 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200364 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000365 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200366 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200367 char * cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200368 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_x509_crt_init( &crt );
371 mbedtls_x509_crt_init( &ca );
372 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000373
Paul Bakker33b43f12013-08-20 11:48:36 +0200374 if( strcmp( cn_name_str, "NULL" ) != 0 )
375 cn_name = cn_name_str;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200376
Manuel Pégourié-Gonnarda54f6cc2017-08-09 10:41:42 +0200377 if( strcmp( profile_str, "" ) == 0 )
Gilles Peskineef86ab22017-05-05 18:59:02 +0200378 profile = &mbedtls_x509_crt_profile_default;
Ron Eldorc1539982018-02-06 18:47:17 +0200379 else if( strcmp( profile_str, "next" ) == 0 )
380 profile = &mbedtls_x509_crt_profile_next;
381 else if( strcmp( profile_str, "suite_b" ) == 0 )
382 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200383 else if( strcmp( profile_str, "compat" ) == 0 )
384 profile = &compat_profile;
Hanno Beckercfa34182019-06-03 14:27:03 +0100385 else if( strcmp( profile_str, "all" ) == 0 )
386 profile = &profile_all;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200387 else
388 TEST_ASSERT( "Unknown algorithm profile" == 0 );
389
Paul Bakker33b43f12013-08-20 11:48:36 +0200390 if( strcmp( verify_callback, "NULL" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200391 f_vrfy = NULL;
Paul Bakker33b43f12013-08-20 11:48:36 +0200392 else if( strcmp( verify_callback, "verify_none" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200393 f_vrfy = verify_none;
Paul Bakker33b43f12013-08-20 11:48:36 +0200394 else if( strcmp( verify_callback, "verify_all" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200395 f_vrfy = verify_all;
396 else
397 TEST_ASSERT( "No known verify callback selected" == 0 );
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
400 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
401 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000402
Gilles Peskineef86ab22017-05-05 18:59:02 +0200403 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 +0200404
Paul Bakkerbd51b262014-07-10 15:26:12 +0200405 TEST_ASSERT( res == ( result ) );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200406 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200407
408exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_x509_crt_free( &crt );
410 mbedtls_x509_crt_free( &ca );
411 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000412}
Paul Bakker33b43f12013-08-20 11:48:36 +0200413/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200416void x509_verify_callback( char *crt_file, char *ca_file, char *name,
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200417 int exp_ret, char *exp_vrfy_out )
418{
419 int ret;
420 mbedtls_x509_crt crt;
421 mbedtls_x509_crt ca;
422 uint32_t flags = 0;
423 verify_print_context vrfy_ctx;
424
425 mbedtls_x509_crt_init( &crt );
426 mbedtls_x509_crt_init( &ca );
427 verify_print_init( &vrfy_ctx );
428
429 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
430 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
431
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200432 if( strcmp( name, "NULL" ) == 0 )
433 name = NULL;
434
Gilles Peskineef86ab22017-05-05 18:59:02 +0200435 ret = mbedtls_x509_crt_verify_with_profile( &crt, &ca, NULL,
436 &compat_profile,
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200437 name, &flags,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200438 verify_print, &vrfy_ctx );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200439
440 TEST_ASSERT( ret == exp_ret );
441 TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
442
443exit:
444 mbedtls_x509_crt_free( &crt );
445 mbedtls_x509_crt_free( &ca );
446}
447/* END_CASE */
448
Hanno Becker02a21932019-06-10 15:08:43 +0100449/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100450void mbedtls_x509_dn_gets( char * crt_file, char * entity, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000451{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000453 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200454 int res = 0;
Hanno Beckerc69c4462019-02-27 09:05:41 +0000455 mbedtls_x509_name *subject = NULL, *issuer = NULL;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000458 memset( buf, 0, 2000 );
459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Hanno Beckerc69c4462019-02-27 09:05:41 +0000461 TEST_ASSERT( mbedtls_x509_crt_get_subject( &crt, &subject ) == 0 );
462 TEST_ASSERT( mbedtls_x509_crt_get_issuer( &crt, &issuer ) == 0 );
463
Paul Bakker33b43f12013-08-20 11:48:36 +0200464 if( strcmp( entity, "subject" ) == 0 )
Hanno Beckerc69c4462019-02-27 09:05:41 +0000465 res = mbedtls_x509_dn_gets( buf, 2000, subject );
Paul Bakker33b43f12013-08-20 11:48:36 +0200466 else if( strcmp( entity, "issuer" ) == 0 )
Hanno Beckerc69c4462019-02-27 09:05:41 +0000467 res = mbedtls_x509_dn_gets( buf, 2000, issuer );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200468 else
469 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000470
471 TEST_ASSERT( res != -1 );
472 TEST_ASSERT( res != -2 );
473
Paul Bakker33b43f12013-08-20 11:48:36 +0200474 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200475
476exit:
Hanno Beckerc69c4462019-02-27 09:05:41 +0000477 mbedtls_x509_name_free( issuer );
478 mbedtls_x509_name_free( subject );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000480}
Paul Bakker33b43f12013-08-20 11:48:36 +0200481/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100484void mbedtls_x509_time_is_past( char * crt_file, char * entity, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000485{
Hanno Beckerc69c4462019-02-27 09:05:41 +0000486 mbedtls_x509_crt crt;
487 mbedtls_x509_crt_frame frame;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Hanno Beckerc69c4462019-02-27 09:05:41 +0000492 TEST_ASSERT( mbedtls_x509_crt_get_frame( &crt, &frame ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200493
Paul Bakker33b43f12013-08-20 11:48:36 +0200494 if( strcmp( entity, "valid_from" ) == 0 )
Hanno Beckerc69c4462019-02-27 09:05:41 +0000495 TEST_ASSERT( mbedtls_x509_time_is_past( &frame.valid_from ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200496 else if( strcmp( entity, "valid_to" ) == 0 )
Hanno Beckerc69c4462019-02-27 09:05:41 +0000497 TEST_ASSERT( mbedtls_x509_time_is_past( &frame.valid_to ) == result );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200498 else
499 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000500
Paul Bakkerbd51b262014-07-10 15:26:12 +0200501exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000503}
Paul Bakker33b43f12013-08-20 11:48:36 +0200504/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100507void mbedtls_x509_time_is_future( char * crt_file, char * entity, int result )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100508{
Hanno Beckerc69c4462019-02-27 09:05:41 +0000509 mbedtls_x509_crt crt;
510 mbedtls_x509_crt_frame frame;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Hanno Beckerc69c4462019-02-27 09:05:41 +0000515 TEST_ASSERT( mbedtls_x509_crt_get_frame( &crt, &frame ) == 0 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100516
517 if( strcmp( entity, "valid_from" ) == 0 )
Hanno Beckerc69c4462019-02-27 09:05:41 +0000518 TEST_ASSERT( mbedtls_x509_time_is_future( &frame.valid_from ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100519 else if( strcmp( entity, "valid_to" ) == 0 )
Hanno Beckerc69c4462019-02-27 09:05:41 +0000520 TEST_ASSERT( mbedtls_x509_time_is_future( &frame.valid_to ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100521 else
522 TEST_ASSERT( "Unknown entity" == 0 );
523
Paul Bakkerbd51b262014-07-10 15:26:12 +0200524exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100526}
527/* END_CASE */
528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100530void x509parse_crt_file( char * crt_file, int result )
Paul Bakker5a5fa922014-09-26 14:53:04 +0200531{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +0200533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_x509_crt_init( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200537
538exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_x509_crt_free( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200540}
541/* END_CASE */
542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100544void x509parse_crt( data_t * buf, char * result_str, int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000545{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_x509_crt crt;
Hanno Becker02a21932019-06-10 15:08:43 +0100547#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000548 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100549 int res;
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600550#endif
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_x509_crt_init( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000553
Hanno Becker2fa5e732019-01-31 09:15:53 +0000554 TEST_ASSERT( mbedtls_x509_crt_parse_der( &crt, buf->x, buf->len ) == ( result ) );
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600555#if defined(MBEDTLS_X509_INFO)
Hanno Becker2fa5e732019-01-31 09:15:53 +0000556 if( ( result ) == 0 )
557 {
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600558 memset( output, 0, 2000 );
Hanno Becker2fa5e732019-01-31 09:15:53 +0000559 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000560
Hanno Becker2fa5e732019-01-31 09:15:53 +0000561 TEST_ASSERT( res != -1 );
562 TEST_ASSERT( res != -2 );
563
564 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
565 }
Hanno Becker98f85c82019-06-11 17:16:13 +0100566#else
567 ((void) result_str);
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600568#endif
Hanno Becker2fa5e732019-01-31 09:15:53 +0000569
570 mbedtls_x509_crt_free( &crt );
571 mbedtls_x509_crt_init( &crt );
Hanno Becker2fa5e732019-01-31 09:15:53 +0000572
573 TEST_ASSERT( mbedtls_x509_crt_parse_der_nocopy( &crt, buf->x, buf->len ) == ( result ) );
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600574
575 TEST_ASSERT( mbedtls_x509_crt_parse( &crt, buf->x, buf->len ) == ( result ) );
Hanno Becker02a21932019-06-10 15:08:43 +0100576#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker33b43f12013-08-20 11:48:36 +0200577 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000578 {
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600579 memset( output, 0, 2000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200581
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000582 TEST_ASSERT( res != -1 );
583 TEST_ASSERT( res != -2 );
584
Paul Bakker33b43f12013-08-20 11:48:36 +0200585 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000586 }
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600587#endif
588
589 mbedtls_x509_crt_free( &crt );
590 mbedtls_x509_crt_init( &crt );
Hanno Becker02a21932019-06-10 15:08:43 +0100591#if !defined(MBEDTLS_X509_REMOVE_INFO)
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600592 memset( output, 0, 2000 );
593#endif
Paul Bakkerb08e6842012-02-11 18:43:20 +0000594
Paul Bakkerbd51b262014-07-10 15:26:12 +0200595exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000597}
Paul Bakker33b43f12013-08-20 11:48:36 +0200598/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000599
Hanno Becker02a21932019-06-10 15:08:43 +0100600/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khan5fcca462018-06-29 11:05:32 +0100601void x509parse_crl( data_t * buf, char * result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000602{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000604 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100605 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 mbedtls_x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000608 memset( output, 0, 2000 );
609
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000610
Azim Khand30ca132017-06-09 04:32:58 +0100611 TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf->x, buf->len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200612 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200615
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000616 TEST_ASSERT( res != -1 );
617 TEST_ASSERT( res != -2 );
618
Paul Bakker33b43f12013-08-20 11:48:36 +0200619 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000620 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000621
Paul Bakkerbd51b262014-07-10 15:26:12 +0200622exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000624}
Paul Bakker33b43f12013-08-20 11:48:36 +0200625/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000626
Hanno Becker02a21932019-06-10 15:08:43 +0100627/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khan5fcca462018-06-29 11:05:32 +0100628void mbedtls_x509_csr_parse( data_t * csr_der, char * ref_out, int ref_ret )
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200629{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200631 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200632 int my_ret;
633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200635 memset( my_out, 0, sizeof( my_out ) );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200636
Azim Khand30ca132017-06-09 04:32:58 +0100637 my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der->x, csr_der->len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200638 TEST_ASSERT( my_ret == ref_ret );
639
640 if( ref_ret == 0 )
641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200643 TEST_ASSERT( my_out_len == strlen( ref_out ) );
644 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
645 }
646
Paul Bakkerbd51b262014-07-10 15:26:12 +0200647exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200649}
650/* END_CASE */
651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100653void mbedtls_x509_crt_parse_path( char * crt_path, int ret, int nb_crt )
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100654{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100656 int i;
657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_x509_crt_init( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100661
662 /* Check how many certs we got */
663 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
664 if( cur->raw.p != NULL )
665 i++;
666
667 TEST_ASSERT( i == nb_crt );
668
Paul Bakkerbd51b262014-07-10 15:26:12 +0200669exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100671}
672/* END_CASE */
673
Janos Follath822b2c32015-10-11 10:25:22 +0200674/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +0200675void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int,
676 int ret_chk, int flags_chk )
677{
678 char file_buf[128];
679 int ret;
680 uint32_t flags;
681 mbedtls_x509_crt trusted, chain;
682
683 /*
684 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
685 * with NN.crt signed by NN-1.crt
686 */
687
688 mbedtls_x509_crt_init( &trusted );
689 mbedtls_x509_crt_init( &chain );
690
691 /* Load trusted root */
692 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 );
693
694 /* Load a chain with nb_int intermediates (from 01 to nb_int),
695 * plus one "end-entity" cert (nb_int + 1) */
696 ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
697 nb_int + 1 );
698 TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf );
699 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 );
700
701 /* Try to verify that chain */
702 ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags,
703 NULL, NULL );
704 TEST_ASSERT( ret == ret_chk );
705 TEST_ASSERT( flags == (uint32_t) flags_chk );
706
707exit:
708 mbedtls_x509_crt_free( &chain );
709 mbedtls_x509_crt_free( &trusted );
710}
711/* END_CASE */
712
713/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200714void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca,
715 int flags_result, int result,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200716 char *profile_name, int vrfy_fatal_lvls )
Janos Follath822b2c32015-10-11 10:25:22 +0200717{
718 char* act;
719 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200720 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100721 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200722 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +0200723
Janos Follath822b2c32015-10-11 10:25:22 +0200724 mbedtls_x509_crt_init( &chain );
725 mbedtls_x509_crt_init( &trusted );
726
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900727 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100728 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
729 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
Janos Follath822b2c32015-10-11 10:25:22 +0200730
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200731 if( strcmp( profile_name, "" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200732 profile = &mbedtls_x509_crt_profile_default;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200733 else if( strcmp( profile_name, "next" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200734 profile = &mbedtls_x509_crt_profile_next;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200735 else if( strcmp( profile_name, "suiteb" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200736 profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200737 else if( strcmp( profile_name, "rsa3072" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200738 profile = &profile_rsa3072;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200739 else if( strcmp( profile_name, "sha512" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200740 profile = &profile_sha512;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200741
742 res = mbedtls_x509_crt_verify_with_profile( &chain, &trusted, NULL, profile,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200743 NULL, &flags, verify_fatal, &vrfy_fatal_lvls );
Janos Follathef4f2582015-10-11 16:17:27 +0200744
745 TEST_ASSERT( res == ( result ) );
746 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Janos Follath822b2c32015-10-11 10:25:22 +0200747
748exit:
749 mbedtls_x509_crt_free( &trusted );
750 mbedtls_x509_crt_free( &chain );
751}
752/* END_CASE */
753
Hanno Becker02a21932019-06-10 15:08:43 +0100754/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khan5fcca462018-06-29 11:05:32 +0100755void x509_oid_desc( data_t * buf, char * ref_desc )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100756{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000758 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000759 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100760
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +0100763 oid.p = buf->x;
764 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100767
768 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000769 {
770 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100771 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000772 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100773 else
774 {
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000775 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100776 TEST_ASSERT( desc != NULL );
777 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
778 }
779}
780/* END_CASE */
781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100783void x509_oid_numstr( data_t * oid_buf, char * numstr, int blen, int ret )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100784{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100786 char num_buf[100];
787
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100788 memset( num_buf, 0x2a, sizeof num_buf );
789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +0100791 oid.p = oid_buf->x;
792 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100793
794 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100797
798 if( ret >= 0 )
799 {
800 TEST_ASSERT( num_buf[ret] == 0 );
801 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
802 }
803}
804/* END_CASE */
805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_KEY_USAGE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100807void x509_check_key_usage( char * crt_file, int usage, int ret )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200808{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200816
Paul Bakkerbd51b262014-07-10 15:26:12 +0200817exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200819}
820/* END_CASE */
821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822/* 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 +0100823void x509_check_extended_key_usage( char * crt_file, data_t * oid, int ret
Azim Khand30ca132017-06-09 04:32:58 +0100824 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200825{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200829
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200832
Azim Khand30ca132017-06-09 04:32:58 +0100833 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 +0200834
Paul Bakkerbd51b262014-07-10 15:26:12 +0200835exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200837}
838/* END_CASE */
839
Andres AG4b76aec2016-09-23 13:16:02 +0100840/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100841void x509_get_time( int tag, char * time_str, int ret, int year, int mon,
842 int day, int hour, int min, int sec )
Andres AG4b76aec2016-09-23 13:16:02 +0100843{
844 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +0000845 unsigned char buf[21];
Andres AG4b76aec2016-09-23 13:16:02 +0100846 unsigned char* start = buf;
847 unsigned char* end = buf;
848
849 memset( &time, 0x00, sizeof( time ) );
850 *end = (unsigned char)tag; end++;
Janos Follathea7054a2017-02-08 14:13:02 +0000851 *end = strlen( time_str );
852 TEST_ASSERT( *end < 20 );
Andres AG4b76aec2016-09-23 13:16:02 +0100853 end++;
854 memcpy( end, time_str, (size_t)*(end - 1) );
855 end += *(end - 1);
856
857 TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
858 if( ret == 0 )
859 {
860 TEST_ASSERT( year == time.year );
861 TEST_ASSERT( mon == time.mon );
862 TEST_ASSERT( day == time.day );
863 TEST_ASSERT( hour == time.hour );
864 TEST_ASSERT( min == time.min );
865 TEST_ASSERT( sec == time.sec );
866 }
867}
868/* END_CASE */
869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Azim Khan5fcca462018-06-29 11:05:32 +0100871void x509_parse_rsassa_pss_params( data_t * hex_params, int params_tag,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200872 int ref_msg_md, int ref_mgf_md,
873 int ref_salt_len, int ref_ret )
874{
875 int my_ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 mbedtls_x509_buf params;
877 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200878 int my_salt_len;
879
Azim Khand30ca132017-06-09 04:32:58 +0100880 params.p = hex_params->x;
881 params.len = hex_params->len;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200882 params.tag = params_tag;
883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 my_ret = mbedtls_x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200885 &my_salt_len );
886
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200887 TEST_ASSERT( my_ret == ref_ret );
888
889 if( ref_ret == 0 )
890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md );
892 TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200893 TEST_ASSERT( my_salt_len == ref_salt_len );
894 }
895
Paul Bakkerbd51b262014-07-10 15:26:12 +0200896exit:
Azim Khand30ca132017-06-09 04:32:58 +0100897 ;;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200898}
899/* END_CASE */
900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100902void x509_selftest( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000903{
Andres AG93012e82016-09-09 09:10:28 +0100904 TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000905}
Paul Bakker33b43f12013-08-20 11:48:36 +0200906/* END_CASE */