blob: 91aa696b84d66bb5bd347cdb5724a2d0f7550210 [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
Hanno Becker5c030582019-02-26 16:45:32 +000027static void x509_free_name( mbedtls_x509_name *name )
28{
29 while( name != NULL )
30 {
31 mbedtls_x509_name *next = name->next;
32 mbedtls_platform_zeroize( name, sizeof( *name ) );
33 mbedtls_free( name );
34 name = next;
35 }
36}
37
Gilles Peskineef86ab22017-05-05 18:59:02 +020038/* Profile for backward compatibility. Allows SHA-1, unlike the default
39 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020040const mbedtls_x509_crt_profile compat_profile =
41{
42 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
43 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) |
44 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
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 0xFFFFFFF, /* Any PK alg */
49 0xFFFFFFF, /* Any curve */
50 1024,
51};
52
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020053const mbedtls_x509_crt_profile profile_rsa3072 =
54{
55 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
56 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
57 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
58 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA ),
59 0,
60 3072,
61};
62
63const mbedtls_x509_crt_profile profile_sha512 =
64{
65 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
66 0xFFFFFFF, /* Any PK alg */
67 0xFFFFFFF, /* Any curve */
68 1024,
69};
70
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020071int verify_none( 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);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010076 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020077
Paul Bakker915275b2012-09-28 07:10:55 +000078 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000079}
80
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020081int verify_all( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000082{
Paul Bakker5a624082011-01-18 16:31:52 +000083 ((void) data);
84 ((void) crt);
85 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000086 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000087
Paul Bakkerb63b0af2011-01-13 17:54:59 +000088 return 0;
89}
90
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +020091int verify_fatal( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
92{
93 int *levels = (int *) data;
94
95 ((void) crt);
96 ((void) certificate_depth);
97
98 /* Simulate a fatal error in the callback */
99 if( *levels & ( 1 << certificate_depth ) )
100 {
101 *flags |= ( 1 << certificate_depth );
Manuel Pégourié-Gonnard41859782017-05-23 12:58:53 +0200102 return( -1 - certificate_depth );
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200103 }
104
105 return( 0 );
106}
107
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900108/* strsep() not available on Windows */
109char *mystrsep(char **stringp, const char *delim)
110{
111 const char *p;
112 char *ret = *stringp;
113
114 if( *stringp == NULL )
115 return( NULL );
116
117 for( ; ; (*stringp)++ )
118 {
119 if( **stringp == '\0' )
120 {
121 *stringp = NULL;
122 goto done;
123 }
124
125 for( p = delim; *p != '\0'; p++ )
126 if( **stringp == *p )
127 {
128 **stringp = '\0';
129 (*stringp)++;
130 goto done;
131 }
132 }
133
134done:
135 return( ret );
136}
137
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200138#if defined(MBEDTLS_X509_CRT_PARSE_C)
139typedef struct {
140 char buf[512];
141 char *p;
142} verify_print_context;
143
144void verify_print_init( verify_print_context *ctx )
145{
146 memset( ctx, 0, sizeof( verify_print_context ) );
147 ctx->p = ctx->buf;
148}
149
150int verify_print( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
151{
152 int ret;
153 verify_print_context *ctx = (verify_print_context *) data;
154 char *p = ctx->p;
155 size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p;
Hanno Becker5c030582019-02-26 16:45:32 +0000156 mbedtls_x509_crt_frame *frame;
157 mbedtls_x509_name subject;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200158 ((void) flags);
159
Hanno Becker5c030582019-02-26 16:45:32 +0000160 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
161 if( ret != 0 )
162 return( ret );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200163
Hanno Becker5c030582019-02-26 16:45:32 +0000164 /* Get linked list presentation of issuer which
165 * `mbedtls_x509_dn_gets()` understands. */
166 {
167 unsigned char *subject_start = frame->subject_raw.p;
168 unsigned char *subject_end = frame->subject_raw.p + frame->subject_raw.len;
169
170 ret = mbedtls_x509_get_name( &subject_start, subject_end, &subject );
171 if( ret != 0 )
172 goto cleanup;
173 }
174
175 ret = mbedtls_snprintf( p, n, "depth %d - serial ", certificate_depth );
176 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
177
178 {
179 mbedtls_x509_buf serial;
180 serial.p = frame->serial.p;
181 serial.len = frame->serial.len;
182 ret = mbedtls_x509_serial_gets( p, n, &serial );
183 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
184 }
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200185
186 ret = mbedtls_snprintf( p, n, " - subject " );
Hanno Becker5c030582019-02-26 16:45:32 +0000187 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200188
Hanno Becker5c030582019-02-26 16:45:32 +0000189 ret = mbedtls_x509_dn_gets( p, n, &subject );
190 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200191
Manuel Pégourié-Gonnardbe2f0b52017-08-21 11:00:22 +0200192 ret = mbedtls_snprintf( p, n, " - flags 0x%08x\n", *flags );
Hanno Becker5c030582019-02-26 16:45:32 +0000193 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200194
195 ctx->p = p;
196
Hanno Becker5c030582019-02-26 16:45:32 +0000197cleanup:
198
199 x509_free_name( subject.next );
200 mbedtls_x509_crt_frame_release( crt, frame );
201
202 if( ret < 0 )
203 return( ret );
204
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200205 return( 0 );
206}
207#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200208/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000209
Paul Bakker33b43f12013-08-20 11:48:36 +0200210/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200212 * END_DEPENDENCIES
213 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000214
Hanno Becker02a21932019-06-10 15:08:43 +0100215/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100216void x509_cert_info( char * crt_file, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000217{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000219 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000220 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000223 memset( buf, 0, 2000 );
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
226 res = mbedtls_x509_crt_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000227
228 TEST_ASSERT( res != -1 );
229 TEST_ASSERT( res != -2 );
230
Paul Bakker33b43f12013-08-20 11:48:36 +0200231 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200232
233exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000235}
Paul Bakker33b43f12013-08-20 11:48:36 +0200236/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000237
Hanno Becker02a21932019-06-10 15:08:43 +0100238/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100239void mbedtls_x509_crl_info( char * crl_file, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000240{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000242 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000243 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000246 memset( buf, 0, 2000 );
247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
249 res = mbedtls_x509_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000250
251 TEST_ASSERT( res != -1 );
252 TEST_ASSERT( res != -2 );
253
Paul Bakker33b43f12013-08-20 11:48:36 +0200254 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200255
256exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000258}
Paul Bakker33b43f12013-08-20 11:48:36 +0200259/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000260
Andres AGa39db392016-12-08 17:10:38 +0000261/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100262void mbedtls_x509_crl_parse( char * crl_file, int result )
Andres AGa39db392016-12-08 17:10:38 +0000263{
264 mbedtls_x509_crl crl;
265 char buf[2000];
266
267 mbedtls_x509_crl_init( &crl );
268 memset( buf, 0, 2000 );
269
270 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == result );
271
272exit:
273 mbedtls_x509_crl_free( &crl );
274}
275/* END_CASE */
276
Hanno Becker02a21932019-06-10 15:08:43 +0100277/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100278void mbedtls_x509_csr_info( char * csr_file, char * result_str )
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100279{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100281 char buf[2000];
282 int res;
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100285 memset( buf, 0, 2000 );
286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 TEST_ASSERT( mbedtls_x509_csr_parse_file( &csr, csr_file ) == 0 );
288 res = mbedtls_x509_csr_info( buf, 2000, "", &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100289
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100290 TEST_ASSERT( res != -1 );
291 TEST_ASSERT( res != -2 );
292
293 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200294
295exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100297}
298/* END_CASE */
299
Hanno Becker02a21932019-06-10 15:08:43 +0100300/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100301void x509_verify_info( int flags, char * prefix, char * result_str )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100302{
303 char buf[2000];
304 int res;
305
306 memset( buf, 0, sizeof( buf ) );
307
308 res = mbedtls_x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
309
310 TEST_ASSERT( res >= 0 );
311
312 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
313}
314/* END_CASE */
315
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200316/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_C */
317void x509_verify_restart( char *crt_file, char *ca_file,
318 int result, int flags_result,
319 int max_ops, int min_restart, int max_restart )
320{
321 int ret, cnt_restart;
322 mbedtls_x509_crt_restart_ctx rs_ctx;
323 mbedtls_x509_crt crt;
324 mbedtls_x509_crt ca;
325 uint32_t flags = 0;
326
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200327 /*
328 * See comments on ecp_test_vect_restart() for op count precision.
329 *
330 * For reference, with mbed TLS 2.6 and default settings:
331 * - ecdsa_verify() for P-256: ~ 6700
332 * - ecdsa_verify() for P-384: ~ 18800
333 * - x509_verify() for server5 -> test-ca2: ~ 18800
334 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
335 */
336
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200337 mbedtls_x509_crt_restart_init( &rs_ctx );
338 mbedtls_x509_crt_init( &crt );
339 mbedtls_x509_crt_init( &ca );
340
341 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
342 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
343
344 mbedtls_ecp_set_max_ops( max_ops );
345
346 cnt_restart = 0;
347 do {
348 ret = mbedtls_x509_crt_verify_restartable( &crt, &ca, NULL,
349 &mbedtls_x509_crt_profile_default, NULL, &flags,
350 NULL, NULL, &rs_ctx );
351 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
352
353 TEST_ASSERT( ret == result );
354 TEST_ASSERT( flags == (uint32_t) flags_result );
355
356 TEST_ASSERT( cnt_restart >= min_restart );
357 TEST_ASSERT( cnt_restart <= max_restart );
358
359 /* Do we leak memory when aborting? */
360 ret = mbedtls_x509_crt_verify_restartable( &crt, &ca, NULL,
361 &mbedtls_x509_crt_profile_default, NULL, &flags,
362 NULL, NULL, &rs_ctx );
363 TEST_ASSERT( ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
364
365exit:
366 mbedtls_x509_crt_restart_free( &rs_ctx );
367 mbedtls_x509_crt_free( &crt );
368 mbedtls_x509_crt_free( &ca );
369}
370/* END_CASE */
371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200373void x509_verify( char *crt_file, char *ca_file, char *crl_file,
374 char *cn_name_str, int result, int flags_result,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200375 char *profile_str,
Paul Bakker33b43f12013-08-20 11:48:36 +0200376 char *verify_callback )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000377{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_x509_crt crt;
379 mbedtls_x509_crt ca;
380 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200381 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000382 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200383 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200384 char * cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200385 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_x509_crt_init( &crt );
388 mbedtls_x509_crt_init( &ca );
389 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000390
Paul Bakker33b43f12013-08-20 11:48:36 +0200391 if( strcmp( cn_name_str, "NULL" ) != 0 )
392 cn_name = cn_name_str;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200393
Manuel Pégourié-Gonnarda54f6cc2017-08-09 10:41:42 +0200394 if( strcmp( profile_str, "" ) == 0 )
Gilles Peskineef86ab22017-05-05 18:59:02 +0200395 profile = &mbedtls_x509_crt_profile_default;
Ron Eldorc1539982018-02-06 18:47:17 +0200396 else if( strcmp( profile_str, "next" ) == 0 )
397 profile = &mbedtls_x509_crt_profile_next;
398 else if( strcmp( profile_str, "suite_b" ) == 0 )
399 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200400 else if( strcmp( profile_str, "compat" ) == 0 )
401 profile = &compat_profile;
Hanno Beckercfa34182019-06-03 14:27:03 +0100402 else if( strcmp( profile_str, "all" ) == 0 )
403 profile = &profile_all;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200404 else
405 TEST_ASSERT( "Unknown algorithm profile" == 0 );
406
Paul Bakker33b43f12013-08-20 11:48:36 +0200407 if( strcmp( verify_callback, "NULL" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200408 f_vrfy = NULL;
Paul Bakker33b43f12013-08-20 11:48:36 +0200409 else if( strcmp( verify_callback, "verify_none" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200410 f_vrfy = verify_none;
Paul Bakker33b43f12013-08-20 11:48:36 +0200411 else if( strcmp( verify_callback, "verify_all" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200412 f_vrfy = verify_all;
413 else
414 TEST_ASSERT( "No known verify callback selected" == 0 );
415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
417 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
418 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000419
Gilles Peskineef86ab22017-05-05 18:59:02 +0200420 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 +0200421
Paul Bakkerbd51b262014-07-10 15:26:12 +0200422 TEST_ASSERT( res == ( result ) );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200423 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200424
425exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_x509_crt_free( &crt );
427 mbedtls_x509_crt_free( &ca );
428 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000429}
Paul Bakker33b43f12013-08-20 11:48:36 +0200430/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200433void x509_verify_callback( char *crt_file, char *ca_file, char *name,
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200434 int exp_ret, char *exp_vrfy_out )
435{
436 int ret;
437 mbedtls_x509_crt crt;
438 mbedtls_x509_crt ca;
439 uint32_t flags = 0;
440 verify_print_context vrfy_ctx;
441
442 mbedtls_x509_crt_init( &crt );
443 mbedtls_x509_crt_init( &ca );
444 verify_print_init( &vrfy_ctx );
445
446 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
447 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
448
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200449 if( strcmp( name, "NULL" ) == 0 )
450 name = NULL;
451
Gilles Peskineef86ab22017-05-05 18:59:02 +0200452 ret = mbedtls_x509_crt_verify_with_profile( &crt, &ca, NULL,
453 &compat_profile,
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200454 name, &flags,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200455 verify_print, &vrfy_ctx );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200456
457 TEST_ASSERT( ret == exp_ret );
458 TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
459
460exit:
461 mbedtls_x509_crt_free( &crt );
462 mbedtls_x509_crt_free( &ca );
463}
464/* END_CASE */
465
Hanno Becker02a21932019-06-10 15:08:43 +0100466/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100467void mbedtls_x509_dn_gets( char * crt_file, char * entity, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000468{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000470 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200471 int res = 0;
Hanno Beckerc69c4462019-02-27 09:05:41 +0000472 mbedtls_x509_name *subject = NULL, *issuer = NULL;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000475 memset( buf, 0, 2000 );
476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Hanno Beckerc69c4462019-02-27 09:05:41 +0000478 TEST_ASSERT( mbedtls_x509_crt_get_subject( &crt, &subject ) == 0 );
479 TEST_ASSERT( mbedtls_x509_crt_get_issuer( &crt, &issuer ) == 0 );
480
Paul Bakker33b43f12013-08-20 11:48:36 +0200481 if( strcmp( entity, "subject" ) == 0 )
Hanno Beckerc69c4462019-02-27 09:05:41 +0000482 res = mbedtls_x509_dn_gets( buf, 2000, subject );
Paul Bakker33b43f12013-08-20 11:48:36 +0200483 else if( strcmp( entity, "issuer" ) == 0 )
Hanno Beckerc69c4462019-02-27 09:05:41 +0000484 res = mbedtls_x509_dn_gets( buf, 2000, issuer );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200485 else
486 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000487
488 TEST_ASSERT( res != -1 );
489 TEST_ASSERT( res != -2 );
490
Paul Bakker33b43f12013-08-20 11:48:36 +0200491 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200492
493exit:
Hanno Beckerc69c4462019-02-27 09:05:41 +0000494 mbedtls_x509_name_free( issuer );
495 mbedtls_x509_name_free( subject );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000497}
Paul Bakker33b43f12013-08-20 11:48:36 +0200498/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100501void mbedtls_x509_time_is_past( char * crt_file, char * entity, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000502{
Hanno Beckerc69c4462019-02-27 09:05:41 +0000503 mbedtls_x509_crt crt;
504 mbedtls_x509_crt_frame frame;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Hanno Beckerc69c4462019-02-27 09:05:41 +0000509 TEST_ASSERT( mbedtls_x509_crt_get_frame( &crt, &frame ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200510
Paul Bakker33b43f12013-08-20 11:48:36 +0200511 if( strcmp( entity, "valid_from" ) == 0 )
Hanno Beckerc69c4462019-02-27 09:05:41 +0000512 TEST_ASSERT( mbedtls_x509_time_is_past( &frame.valid_from ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200513 else if( strcmp( entity, "valid_to" ) == 0 )
Hanno Beckerc69c4462019-02-27 09:05:41 +0000514 TEST_ASSERT( mbedtls_x509_time_is_past( &frame.valid_to ) == result );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200515 else
516 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000517
Paul Bakkerbd51b262014-07-10 15:26:12 +0200518exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000520}
Paul Bakker33b43f12013-08-20 11:48:36 +0200521/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100524void mbedtls_x509_time_is_future( char * crt_file, char * entity, int result )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100525{
Hanno Beckerc69c4462019-02-27 09:05:41 +0000526 mbedtls_x509_crt crt;
527 mbedtls_x509_crt_frame frame;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Hanno Beckerc69c4462019-02-27 09:05:41 +0000532 TEST_ASSERT( mbedtls_x509_crt_get_frame( &crt, &frame ) == 0 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100533
534 if( strcmp( entity, "valid_from" ) == 0 )
Hanno Beckerc69c4462019-02-27 09:05:41 +0000535 TEST_ASSERT( mbedtls_x509_time_is_future( &frame.valid_from ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100536 else if( strcmp( entity, "valid_to" ) == 0 )
Hanno Beckerc69c4462019-02-27 09:05:41 +0000537 TEST_ASSERT( mbedtls_x509_time_is_future( &frame.valid_to ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100538 else
539 TEST_ASSERT( "Unknown entity" == 0 );
540
Paul Bakkerbd51b262014-07-10 15:26:12 +0200541exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100543}
544/* END_CASE */
545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100547void x509parse_crt_file( char * crt_file, int result )
Paul Bakker5a5fa922014-09-26 14:53:04 +0200548{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +0200550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_x509_crt_init( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200554
555exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_x509_crt_free( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200557}
558/* END_CASE */
559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100561void x509parse_crt( data_t * buf, char * result_str, int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000562{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_x509_crt crt;
Hanno Becker02a21932019-06-10 15:08:43 +0100564#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000565 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100566 int res;
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600567#endif
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_x509_crt_init( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000570
Hanno Becker2fa5e732019-01-31 09:15:53 +0000571 TEST_ASSERT( mbedtls_x509_crt_parse_der( &crt, buf->x, buf->len ) == ( result ) );
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600572#if defined(MBEDTLS_X509_INFO)
Hanno Becker2fa5e732019-01-31 09:15:53 +0000573 if( ( result ) == 0 )
574 {
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600575 memset( output, 0, 2000 );
Hanno Becker2fa5e732019-01-31 09:15:53 +0000576 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000577
Hanno Becker2fa5e732019-01-31 09:15:53 +0000578 TEST_ASSERT( res != -1 );
579 TEST_ASSERT( res != -2 );
580
581 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
582 }
Hanno Becker98f85c82019-06-11 17:16:13 +0100583#else
584 ((void) result_str);
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600585#endif
Hanno Becker2fa5e732019-01-31 09:15:53 +0000586
587 mbedtls_x509_crt_free( &crt );
588 mbedtls_x509_crt_init( &crt );
Hanno Becker2fa5e732019-01-31 09:15:53 +0000589
590 TEST_ASSERT( mbedtls_x509_crt_parse_der_nocopy( &crt, buf->x, buf->len ) == ( result ) );
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600591
592 TEST_ASSERT( mbedtls_x509_crt_parse( &crt, buf->x, buf->len ) == ( result ) );
Hanno Becker02a21932019-06-10 15:08:43 +0100593#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker33b43f12013-08-20 11:48:36 +0200594 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000595 {
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600596 memset( output, 0, 2000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200598
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000599 TEST_ASSERT( res != -1 );
600 TEST_ASSERT( res != -2 );
601
Paul Bakker33b43f12013-08-20 11:48:36 +0200602 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000603 }
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600604#endif
605
606 mbedtls_x509_crt_free( &crt );
607 mbedtls_x509_crt_init( &crt );
Hanno Becker02a21932019-06-10 15:08:43 +0100608#if !defined(MBEDTLS_X509_REMOVE_INFO)
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600609 memset( output, 0, 2000 );
610#endif
Paul Bakkerb08e6842012-02-11 18:43:20 +0000611
Paul Bakkerbd51b262014-07-10 15:26:12 +0200612exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 mbedtls_x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000614}
Paul Bakker33b43f12013-08-20 11:48:36 +0200615/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000616
Hanno Becker02a21932019-06-10 15:08:43 +0100617/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khan5fcca462018-06-29 11:05:32 +0100618void x509parse_crl( data_t * buf, char * result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000619{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000621 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100622 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 mbedtls_x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000625 memset( output, 0, 2000 );
626
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000627
Azim Khand30ca132017-06-09 04:32:58 +0100628 TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf->x, buf->len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200629 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200632
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000633 TEST_ASSERT( res != -1 );
634 TEST_ASSERT( res != -2 );
635
Paul Bakker33b43f12013-08-20 11:48:36 +0200636 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000637 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000638
Paul Bakkerbd51b262014-07-10 15:26:12 +0200639exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 mbedtls_x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000641}
Paul Bakker33b43f12013-08-20 11:48:36 +0200642/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000643
Hanno Becker02a21932019-06-10 15:08:43 +0100644/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khan5fcca462018-06-29 11:05:32 +0100645void mbedtls_x509_csr_parse( data_t * csr_der, char * ref_out, int ref_ret )
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200646{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200648 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200649 int my_ret;
650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200652 memset( my_out, 0, sizeof( my_out ) );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200653
Azim Khand30ca132017-06-09 04:32:58 +0100654 my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der->x, csr_der->len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200655 TEST_ASSERT( my_ret == ref_ret );
656
657 if( ref_ret == 0 )
658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200660 TEST_ASSERT( my_out_len == strlen( ref_out ) );
661 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
662 }
663
Paul Bakkerbd51b262014-07-10 15:26:12 +0200664exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200666}
667/* END_CASE */
668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100670void mbedtls_x509_crt_parse_path( char * crt_path, int ret, int nb_crt )
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100671{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100673 int i;
674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_x509_crt_init( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100678
679 /* Check how many certs we got */
680 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
681 if( cur->raw.p != NULL )
682 i++;
683
684 TEST_ASSERT( i == nb_crt );
685
Paul Bakkerbd51b262014-07-10 15:26:12 +0200686exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100688}
689/* END_CASE */
690
Janos Follath822b2c32015-10-11 10:25:22 +0200691/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +0200692void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int,
693 int ret_chk, int flags_chk )
694{
695 char file_buf[128];
696 int ret;
697 uint32_t flags;
698 mbedtls_x509_crt trusted, chain;
699
700 /*
701 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
702 * with NN.crt signed by NN-1.crt
703 */
704
705 mbedtls_x509_crt_init( &trusted );
706 mbedtls_x509_crt_init( &chain );
707
708 /* Load trusted root */
709 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 );
710
711 /* Load a chain with nb_int intermediates (from 01 to nb_int),
712 * plus one "end-entity" cert (nb_int + 1) */
713 ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
714 nb_int + 1 );
715 TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf );
716 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 );
717
718 /* Try to verify that chain */
719 ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags,
720 NULL, NULL );
721 TEST_ASSERT( ret == ret_chk );
722 TEST_ASSERT( flags == (uint32_t) flags_chk );
723
724exit:
725 mbedtls_x509_crt_free( &chain );
726 mbedtls_x509_crt_free( &trusted );
727}
728/* END_CASE */
729
730/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200731void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca,
732 int flags_result, int result,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200733 char *profile_name, int vrfy_fatal_lvls )
Janos Follath822b2c32015-10-11 10:25:22 +0200734{
735 char* act;
736 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200737 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100738 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200739 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +0200740
Janos Follath822b2c32015-10-11 10:25:22 +0200741 mbedtls_x509_crt_init( &chain );
742 mbedtls_x509_crt_init( &trusted );
743
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900744 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100745 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
746 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
Janos Follath822b2c32015-10-11 10:25:22 +0200747
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200748 if( strcmp( profile_name, "" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200749 profile = &mbedtls_x509_crt_profile_default;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200750 else if( strcmp( profile_name, "next" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200751 profile = &mbedtls_x509_crt_profile_next;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200752 else if( strcmp( profile_name, "suiteb" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200753 profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200754 else if( strcmp( profile_name, "rsa3072" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200755 profile = &profile_rsa3072;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200756 else if( strcmp( profile_name, "sha512" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200757 profile = &profile_sha512;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200758
759 res = mbedtls_x509_crt_verify_with_profile( &chain, &trusted, NULL, profile,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200760 NULL, &flags, verify_fatal, &vrfy_fatal_lvls );
Janos Follathef4f2582015-10-11 16:17:27 +0200761
762 TEST_ASSERT( res == ( result ) );
763 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Janos Follath822b2c32015-10-11 10:25:22 +0200764
765exit:
766 mbedtls_x509_crt_free( &trusted );
767 mbedtls_x509_crt_free( &chain );
768}
769/* END_CASE */
770
Hanno Becker02a21932019-06-10 15:08:43 +0100771/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khan5fcca462018-06-29 11:05:32 +0100772void x509_oid_desc( data_t * buf, char * ref_desc )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100773{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000775 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000776 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100777
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +0100780 oid.p = buf->x;
781 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100784
785 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000786 {
787 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100788 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000789 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100790 else
791 {
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000792 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100793 TEST_ASSERT( desc != NULL );
794 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
795 }
796}
797/* END_CASE */
798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100800void x509_oid_numstr( data_t * oid_buf, char * numstr, int blen, int ret )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100801{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100803 char num_buf[100];
804
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100805 memset( num_buf, 0x2a, sizeof num_buf );
806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +0100808 oid.p = oid_buf->x;
809 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100810
811 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100814
815 if( ret >= 0 )
816 {
817 TEST_ASSERT( num_buf[ret] == 0 );
818 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
819 }
820}
821/* END_CASE */
822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_KEY_USAGE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100824void x509_check_key_usage( char * crt_file, int usage, int ret )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200825{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +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é-Gonnard603116c2014-04-09 09:50:03 +0200831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +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é-Gonnard603116c2014-04-09 09:50:03 +0200836}
837/* END_CASE */
838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839/* 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 +0100840void x509_check_extended_key_usage( char * crt_file, data_t * oid, int ret
Azim Khand30ca132017-06-09 04:32:58 +0100841 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200842{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200846
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200849
Azim Khand30ca132017-06-09 04:32:58 +0100850 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 +0200851
Paul Bakkerbd51b262014-07-10 15:26:12 +0200852exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200854}
855/* END_CASE */
856
Andres AG4b76aec2016-09-23 13:16:02 +0100857/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100858void x509_get_time( int tag, char * time_str, int ret, int year, int mon,
859 int day, int hour, int min, int sec )
Andres AG4b76aec2016-09-23 13:16:02 +0100860{
861 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +0000862 unsigned char buf[21];
Andres AG4b76aec2016-09-23 13:16:02 +0100863 unsigned char* start = buf;
864 unsigned char* end = buf;
865
866 memset( &time, 0x00, sizeof( time ) );
867 *end = (unsigned char)tag; end++;
Janos Follathea7054a2017-02-08 14:13:02 +0000868 *end = strlen( time_str );
869 TEST_ASSERT( *end < 20 );
Andres AG4b76aec2016-09-23 13:16:02 +0100870 end++;
871 memcpy( end, time_str, (size_t)*(end - 1) );
872 end += *(end - 1);
873
874 TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
875 if( ret == 0 )
876 {
877 TEST_ASSERT( year == time.year );
878 TEST_ASSERT( mon == time.mon );
879 TEST_ASSERT( day == time.day );
880 TEST_ASSERT( hour == time.hour );
881 TEST_ASSERT( min == time.min );
882 TEST_ASSERT( sec == time.sec );
883 }
884}
885/* END_CASE */
886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Azim Khan5fcca462018-06-29 11:05:32 +0100888void x509_parse_rsassa_pss_params( data_t * hex_params, int params_tag,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200889 int ref_msg_md, int ref_mgf_md,
890 int ref_salt_len, int ref_ret )
891{
892 int my_ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 mbedtls_x509_buf params;
894 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200895 int my_salt_len;
896
Azim Khand30ca132017-06-09 04:32:58 +0100897 params.p = hex_params->x;
898 params.len = hex_params->len;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200899 params.tag = params_tag;
900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 my_ret = mbedtls_x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200902 &my_salt_len );
903
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200904 TEST_ASSERT( my_ret == ref_ret );
905
906 if( ref_ret == 0 )
907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md );
909 TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200910 TEST_ASSERT( my_salt_len == ref_salt_len );
911 }
912
Paul Bakkerbd51b262014-07-10 15:26:12 +0200913exit:
Azim Khand30ca132017-06-09 04:32:58 +0100914 ;;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200915}
916/* END_CASE */
917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100919void x509_selftest( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000920{
Andres AG93012e82016-09-09 09:10:28 +0100921 TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000922}
Paul Bakker33b43f12013-08-20 11:48:36 +0200923/* END_CASE */