blob: 85ff41df27aaf5fa5ceb5303ed9325ea1c2aa0eb [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 Becker20a4ade2019-06-03 14:27:03 +010018/* Test-only profile allowing all digests, PK algorithms, and curves. */
19const mbedtls_x509_crt_profile profile_all =
20{
21 0xFFFFFFFF, /* Any MD */
22 0xFFFFFFFF, /* Any PK alg */
23 0xFFFFFFFF, /* Any curve */
24 1024,
25};
26
Gilles Peskineef86ab22017-05-05 18:59:02 +020027/* Profile for backward compatibility. Allows SHA-1, unlike the default
28 profile. */
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +020029const mbedtls_x509_crt_profile compat_profile =
30{
31 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
32 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) |
33 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
34 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
35 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
36 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
37 0xFFFFFFF, /* Any PK alg */
38 0xFFFFFFF, /* Any curve */
39 1024,
40};
41
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +020042const mbedtls_x509_crt_profile profile_rsa3072 =
43{
44 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
45 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
46 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
47 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA ),
48 0,
49 3072,
50};
51
52const mbedtls_x509_crt_profile profile_sha512 =
53{
54 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
55 0xFFFFFFF, /* Any PK alg */
56 0xFFFFFFF, /* Any curve */
57 1024,
58};
59
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020060int verify_none( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000061{
Paul Bakker5a624082011-01-18 16:31:52 +000062 ((void) data);
63 ((void) crt);
64 ((void) certificate_depth);
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010065 *flags |= MBEDTLS_X509_BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020066
Paul Bakker915275b2012-09-28 07:10:55 +000067 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000068}
69
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020070int verify_all( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000071{
Paul Bakker5a624082011-01-18 16:31:52 +000072 ((void) data);
73 ((void) crt);
74 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000075 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000076
Paul Bakkerb63b0af2011-01-13 17:54:59 +000077 return 0;
78}
79
Jarno Lamsa03cd1202019-03-27 15:45:04 +020080#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Janos Follathd7ecbd62019-04-05 14:52:17 +010081int ca_callback_fail( void *data, mbedtls_x509_crt const *child, mbedtls_x509_crt **candidates )
Jarno Lamsa557426a2019-03-27 17:08:29 +020082{
83 ((void) data);
84 ((void) child);
85 ((void) candidates);
86
87 return -1;
88}
89
Hanno Beckercbb59032019-03-28 14:14:22 +000090int ca_callback( void *data, mbedtls_x509_crt const *child,
Janos Follathd7ecbd62019-04-05 14:52:17 +010091 mbedtls_x509_crt **candidates )
Jarno Lamsa03cd1202019-03-27 15:45:04 +020092{
Hanno Beckercbb59032019-03-28 14:14:22 +000093 int ret = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +020094 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
Hanno Beckercbb59032019-03-28 14:14:22 +000095 mbedtls_x509_crt *first;
96
97 /* This is a test-only implementation of the CA callback
98 * which always returns the entire list of trusted certificates.
99 * Production implementations managing a large number of CAs
100 * should use an efficient presentation and lookup for the
101 * set of trusted certificates (such as a hashtable) and only
102 * return those trusted certificates which satisfy basic
103 * parental checks, such as the matching of child `Issuer`
104 * and parent `Subject` field. */
105 ((void) child);
106
107 first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
108 if( first == NULL )
109 {
110 ret = -1;
111 goto exit;
112 }
113 mbedtls_x509_crt_init( first );
114
115 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
116 {
117 ret = -1;
118 goto exit;
119 }
120
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200121 while( ca->next != NULL )
122 {
123 ca = ca->next;
Hanno Beckercbb59032019-03-28 14:14:22 +0000124 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
125 {
126 ret = -1;
127 goto exit;
128 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200129 }
Hanno Beckercbb59032019-03-28 14:14:22 +0000130
131exit:
132
133 if( ret != 0 )
134 {
135 mbedtls_x509_crt_free( first );
136 mbedtls_free( first );
137 first = NULL;
138 }
139
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200140 *candidates = first;
Hanno Beckercbb59032019-03-28 14:14:22 +0000141 return( ret );
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200142}
143#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
144
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200145int verify_fatal( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
146{
147 int *levels = (int *) data;
148
149 ((void) crt);
150 ((void) certificate_depth);
151
152 /* Simulate a fatal error in the callback */
153 if( *levels & ( 1 << certificate_depth ) )
154 {
155 *flags |= ( 1 << certificate_depth );
Manuel Pégourié-Gonnard41859782017-05-23 12:58:53 +0200156 return( -1 - certificate_depth );
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200157 }
158
159 return( 0 );
160}
161
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900162/* strsep() not available on Windows */
163char *mystrsep(char **stringp, const char *delim)
164{
165 const char *p;
166 char *ret = *stringp;
167
168 if( *stringp == NULL )
169 return( NULL );
170
171 for( ; ; (*stringp)++ )
172 {
173 if( **stringp == '\0' )
174 {
175 *stringp = NULL;
176 goto done;
177 }
178
179 for( p = delim; *p != '\0'; p++ )
180 if( **stringp == *p )
181 {
182 **stringp = '\0';
183 (*stringp)++;
184 goto done;
185 }
186 }
187
188done:
189 return( ret );
190}
191
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200192#if defined(MBEDTLS_X509_CRT_PARSE_C)
193typedef struct {
194 char buf[512];
195 char *p;
196} verify_print_context;
197
198void verify_print_init( verify_print_context *ctx )
199{
200 memset( ctx, 0, sizeof( verify_print_context ) );
201 ctx->p = ctx->buf;
202}
203
204int verify_print( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
205{
206 int ret;
207 verify_print_context *ctx = (verify_print_context *) data;
208 char *p = ctx->p;
209 size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p;
210 ((void) flags);
211
212 ret = mbedtls_snprintf( p, n, "depth %d - serial ", certificate_depth );
213 MBEDTLS_X509_SAFE_SNPRINTF;
214
215 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
216 MBEDTLS_X509_SAFE_SNPRINTF;
217
218 ret = mbedtls_snprintf( p, n, " - subject " );
219 MBEDTLS_X509_SAFE_SNPRINTF;
220
221 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
222 MBEDTLS_X509_SAFE_SNPRINTF;
223
Manuel Pégourié-Gonnardbe2f0b52017-08-21 11:00:22 +0200224 ret = mbedtls_snprintf( p, n, " - flags 0x%08x\n", *flags );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200225 MBEDTLS_X509_SAFE_SNPRINTF;
226
227 ctx->p = p;
228
229 return( 0 );
230}
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200231
232int verify_parse_san( mbedtls_x509_subject_alternative_name *san,
233 char **buf, size_t *size )
234{
235 int ret;
236 size_t i;
237 char *p = *buf;
238 size_t n = *size;
239
240 ret = mbedtls_snprintf( p, n, "type : %u", san->type );
241 MBEDTLS_X509_SAFE_SNPRINTF;
242
243 switch( san->type )
244 {
245 case( MBEDTLS_X509_SAN_OTHER_NAME ):
246 ret = mbedtls_snprintf( p, n, "\notherName :");
247 MBEDTLS_X509_SAFE_SNPRINTF;
248
249 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
250 &san->san.other_name.value.hardware_module_name.oid ) != 0 )
251 {
252 ret = mbedtls_snprintf( p, n, " hardware module name :" );
253 MBEDTLS_X509_SAFE_SNPRINTF;
254 ret = mbedtls_snprintf( p, n, " hardware type : " );
255 MBEDTLS_X509_SAFE_SNPRINTF;
256
257 ret = mbedtls_oid_get_numeric_string( p, n,
258 &san->san.other_name.value.hardware_module_name.oid );
259 MBEDTLS_X509_SAFE_SNPRINTF;
260
261 ret = mbedtls_snprintf( p, n, ", hardware serial number : " );
262 MBEDTLS_X509_SAFE_SNPRINTF;
263
264 if( san->san.other_name.value.hardware_module_name.val.len >= n )
265 {
266 *p = '\0';
267 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
268 }
269
270 for( i=0; i < san->san.other_name.value.hardware_module_name.val.len; i++ )
271 {
272 *p++ = san->san.other_name.value.hardware_module_name.val.p[i];
273 }
274 n -= san->san.other_name.value.hardware_module_name.val.len;
275 }
276 break;/* MBEDTLS_OID_ON_HW_MODULE_NAME */
277 case( MBEDTLS_X509_SAN_DNS_NAME ):
278 ret = mbedtls_snprintf( p, n, "\ndNSName : " );
279 MBEDTLS_X509_SAFE_SNPRINTF;
280 if( san->san.unstructured_name.len >= n )
281 {
282 *p = '\0';
283 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
284 }
285 n -= san->san.unstructured_name.len;
286 for( i = 0; i < san->san.unstructured_name.len; i++ )
287 *p++ = san->san.unstructured_name.p[i];
288 break;/* MBEDTLS_X509_SAN_DNS_NAME */
289
290 default:
291 /*
292 * Should not happen.
293 */
294 return( -1 );
295 }
296 ret = mbedtls_snprintf( p, n, "\n" );
297 MBEDTLS_X509_SAFE_SNPRINTF;
298
299 *size = n;
300 *buf = p;
301
302 return( 0 );
303}
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200304#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200305/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000306
Paul Bakker33b43f12013-08-20 11:48:36 +0200307/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200309 * END_DEPENDENCIES
310 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200313void x509_parse_san( char * crt_file, char * result_str )
314{
Ron Eldor890819a2019-05-13 19:03:04 +0300315 int ret;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200316 mbedtls_x509_crt crt;
Ron Eldor890819a2019-05-13 19:03:04 +0300317 mbedtls_x509_subject_alternative_name san;
318 mbedtls_x509_sequence *cur = NULL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200319 char buf[2000];
320 char *p = buf;
321 size_t n = sizeof( buf );
322
323 mbedtls_x509_crt_init( &crt );
324 memset( buf, 0, 2000 );
325
326 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Ron Eldor890819a2019-05-13 19:03:04 +0300327
328 if( crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200329 {
Ron Eldor890819a2019-05-13 19:03:04 +0300330 cur = &crt.subject_alt_names;
331 while( cur != NULL )
332 {
333 ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
334 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
335 /*
336 * If san type not supported, ignore.
337 */
338 if( ret == 0)
339 TEST_ASSERT( verify_parse_san( &san, &p, &n ) == 0 );
340 cur = cur->next;
341 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200342 }
343
344 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
345
346exit:
347
Ron Eldorb2dc3fa2019-03-21 13:40:13 +0200348 mbedtls_x509_crt_free( &crt );
349}
350/* END_CASE */
351
352/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100353void x509_cert_info( char * crt_file, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000354{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000356 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000357 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000360 memset( buf, 0, 2000 );
361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
363 res = mbedtls_x509_crt_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000364
365 TEST_ASSERT( res != -1 );
366 TEST_ASSERT( res != -2 );
367
Paul Bakker33b43f12013-08-20 11:48:36 +0200368 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200369
370exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000372}
Paul Bakker33b43f12013-08-20 11:48:36 +0200373/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100376void mbedtls_x509_crl_info( char * crl_file, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000377{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_x509_crl crl;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000379 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000380 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000383 memset( buf, 0, 2000 );
384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
386 res = mbedtls_x509_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000387
388 TEST_ASSERT( res != -1 );
389 TEST_ASSERT( res != -2 );
390
Paul Bakker33b43f12013-08-20 11:48:36 +0200391 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200392
393exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000395}
Paul Bakker33b43f12013-08-20 11:48:36 +0200396/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000397
Andres AGa39db392016-12-08 17:10:38 +0000398/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100399void mbedtls_x509_crl_parse( char * crl_file, int result )
Andres AGa39db392016-12-08 17:10:38 +0000400{
401 mbedtls_x509_crl crl;
402 char buf[2000];
403
404 mbedtls_x509_crl_init( &crl );
405 memset( buf, 0, 2000 );
406
407 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == result );
408
409exit:
410 mbedtls_x509_crl_free( &crl );
411}
412/* END_CASE */
413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100415void mbedtls_x509_csr_info( char * csr_file, char * result_str )
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100416{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100418 char buf[2000];
419 int res;
420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100422 memset( buf, 0, 2000 );
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 TEST_ASSERT( mbedtls_x509_csr_parse_file( &csr, csr_file ) == 0 );
425 res = mbedtls_x509_csr_info( buf, 2000, "", &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100426
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100427 TEST_ASSERT( res != -1 );
428 TEST_ASSERT( res != -2 );
429
430 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200431
432exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100434}
435/* END_CASE */
436
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100437/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100438void x509_verify_info( int flags, char * prefix, char * result_str )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +0100439{
440 char buf[2000];
441 int res;
442
443 memset( buf, 0, sizeof( buf ) );
444
445 res = mbedtls_x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
446
447 TEST_ASSERT( res >= 0 );
448
449 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
450}
451/* END_CASE */
452
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200453/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_C */
454void x509_verify_restart( char *crt_file, char *ca_file,
455 int result, int flags_result,
456 int max_ops, int min_restart, int max_restart )
457{
458 int ret, cnt_restart;
459 mbedtls_x509_crt_restart_ctx rs_ctx;
460 mbedtls_x509_crt crt;
461 mbedtls_x509_crt ca;
462 uint32_t flags = 0;
463
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +0200464 /*
465 * See comments on ecp_test_vect_restart() for op count precision.
466 *
467 * For reference, with mbed TLS 2.6 and default settings:
468 * - ecdsa_verify() for P-256: ~ 6700
469 * - ecdsa_verify() for P-384: ~ 18800
470 * - x509_verify() for server5 -> test-ca2: ~ 18800
471 * - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
472 */
473
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +0200474 mbedtls_x509_crt_restart_init( &rs_ctx );
475 mbedtls_x509_crt_init( &crt );
476 mbedtls_x509_crt_init( &ca );
477
478 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
479 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
480
481 mbedtls_ecp_set_max_ops( max_ops );
482
483 cnt_restart = 0;
484 do {
485 ret = mbedtls_x509_crt_verify_restartable( &crt, &ca, NULL,
486 &mbedtls_x509_crt_profile_default, NULL, &flags,
487 NULL, NULL, &rs_ctx );
488 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
489
490 TEST_ASSERT( ret == result );
491 TEST_ASSERT( flags == (uint32_t) flags_result );
492
493 TEST_ASSERT( cnt_restart >= min_restart );
494 TEST_ASSERT( cnt_restart <= max_restart );
495
496 /* Do we leak memory when aborting? */
497 ret = mbedtls_x509_crt_verify_restartable( &crt, &ca, NULL,
498 &mbedtls_x509_crt_profile_default, NULL, &flags,
499 NULL, NULL, &rs_ctx );
500 TEST_ASSERT( ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
501
502exit:
503 mbedtls_x509_crt_restart_free( &rs_ctx );
504 mbedtls_x509_crt_free( &crt );
505 mbedtls_x509_crt_free( &ca );
506}
507/* END_CASE */
508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200510void x509_verify( char *crt_file, char *ca_file, char *crl_file,
511 char *cn_name_str, int result, int flags_result,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200512 char *profile_str,
Paul Bakker33b43f12013-08-20 11:48:36 +0200513 char *verify_callback )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000514{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_x509_crt crt;
516 mbedtls_x509_crt ca;
517 mbedtls_x509_crl crl;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200518 uint32_t flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000519 int res;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200520 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200521 char * cn_name = NULL;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200522 const mbedtls_x509_crt_profile *profile;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000523
Jaeden Amero3ea26872019-02-13 11:30:22 +0000524#if defined(MBEDTLS_USE_PSA_CRYPTO)
525 TEST_ASSERT( psa_crypto_init() == 0 );
526#endif
527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_x509_crt_init( &crt );
529 mbedtls_x509_crt_init( &ca );
530 mbedtls_x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000531
Paul Bakker33b43f12013-08-20 11:48:36 +0200532 if( strcmp( cn_name_str, "NULL" ) != 0 )
533 cn_name = cn_name_str;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200534
Manuel Pégourié-Gonnarda54f6cc2017-08-09 10:41:42 +0200535 if( strcmp( profile_str, "" ) == 0 )
Gilles Peskineef86ab22017-05-05 18:59:02 +0200536 profile = &mbedtls_x509_crt_profile_default;
Ron Eldorc1539982018-02-06 18:47:17 +0200537 else if( strcmp( profile_str, "next" ) == 0 )
538 profile = &mbedtls_x509_crt_profile_next;
539 else if( strcmp( profile_str, "suite_b" ) == 0 )
540 profile = &mbedtls_x509_crt_profile_suiteb;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200541 else if( strcmp( profile_str, "compat" ) == 0 )
542 profile = &compat_profile;
Hanno Becker20a4ade2019-06-03 14:27:03 +0100543 else if( strcmp( profile_str, "all" ) == 0 )
544 profile = &profile_all;
Gilles Peskineef86ab22017-05-05 18:59:02 +0200545 else
546 TEST_ASSERT( "Unknown algorithm profile" == 0 );
547
Paul Bakker33b43f12013-08-20 11:48:36 +0200548 if( strcmp( verify_callback, "NULL" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200549 f_vrfy = NULL;
Paul Bakker33b43f12013-08-20 11:48:36 +0200550 else if( strcmp( verify_callback, "verify_none" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200551 f_vrfy = verify_none;
Paul Bakker33b43f12013-08-20 11:48:36 +0200552 else if( strcmp( verify_callback, "verify_all" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200553 f_vrfy = verify_all;
554 else
555 TEST_ASSERT( "No known verify callback selected" == 0 );
556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
558 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
559 TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000560
Gilles Peskineef86ab22017-05-05 18:59:02 +0200561 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 +0200562
Paul Bakkerbd51b262014-07-10 15:26:12 +0200563 TEST_ASSERT( res == ( result ) );
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200564 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200565
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200566#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker0350d562019-03-28 14:23:36 +0000567 /* CRLs aren't supported with CA callbacks, so skip the CA callback
Jarno Lamsadfd22c42019-04-01 15:18:53 +0300568 * version of the test if CRLs are in use. */
Hanno Becker0350d562019-03-28 14:23:36 +0000569 if( crl_file == NULL || strcmp( crl_file, "" ) == 0 )
570 {
571 flags = 0;
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200572
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300573 res = mbedtls_x509_crt_verify_with_ca_cb( &crt, ca_callback, &ca, profile, cn_name, &flags, f_vrfy, NULL );
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200574
Hanno Becker0350d562019-03-28 14:23:36 +0000575 TEST_ASSERT( res == ( result ) );
576 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
577 }
Jarno Lamsa03cd1202019-03-27 15:45:04 +0200578#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200579exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_x509_crt_free( &crt );
581 mbedtls_x509_crt_free( &ca );
582 mbedtls_x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000583}
Paul Bakker33b43f12013-08-20 11:48:36 +0200584/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000585
Jarno Lamsa557426a2019-03-27 17:08:29 +0200586/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
587void x509_verify_ca_cb_failure( char *crt_file, char *ca_file, char *name,
Hanno Beckercbb59032019-03-28 14:14:22 +0000588 int exp_ret )
Jarno Lamsa557426a2019-03-27 17:08:29 +0200589{
590 int ret;
591 mbedtls_x509_crt crt;
592 mbedtls_x509_crt ca;
593 uint32_t flags = 0;
Hanno Becker3f932bb2019-03-28 17:06:47 +0000594
Jarno Lamsa557426a2019-03-27 17:08:29 +0200595 mbedtls_x509_crt_init( &crt );
596 mbedtls_x509_crt_init( &ca );
597
598 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
599 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
600
601 if( strcmp( name, "NULL" ) == 0 )
602 name = NULL;
Hanno Beckercbb59032019-03-28 14:14:22 +0000603
Jarno Lamsa31d9db62019-04-01 14:33:49 +0300604 ret = mbedtls_x509_crt_verify_with_ca_cb( &crt, ca_callback_fail, &ca,
Janos Follathd7ecbd62019-04-05 14:52:17 +0100605 &compat_profile, name, &flags,
606 NULL, NULL );
Jarno Lamsa557426a2019-03-27 17:08:29 +0200607
608 TEST_ASSERT( ret == exp_ret );
Janos Follath846ae7a2019-04-05 16:45:01 +0100609 TEST_ASSERT( flags == (uint32_t)( -1 ) );
Jarno Lamsa557426a2019-03-27 17:08:29 +0200610exit:
611 mbedtls_x509_crt_free( &crt );
612 mbedtls_x509_crt_free( &ca );
613}
614/* END_CASE */
615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200617void x509_verify_callback( char *crt_file, char *ca_file, char *name,
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200618 int exp_ret, char *exp_vrfy_out )
619{
620 int ret;
621 mbedtls_x509_crt crt;
622 mbedtls_x509_crt ca;
623 uint32_t flags = 0;
624 verify_print_context vrfy_ctx;
625
Jaeden Amero3ea26872019-02-13 11:30:22 +0000626#if defined(MBEDTLS_USE_PSA_CRYPTO)
627 TEST_ASSERT( psa_crypto_init() == 0 );
628#endif
629
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200630 mbedtls_x509_crt_init( &crt );
631 mbedtls_x509_crt_init( &ca );
632 verify_print_init( &vrfy_ctx );
633
634 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
635 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
636
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200637 if( strcmp( name, "NULL" ) == 0 )
638 name = NULL;
639
Gilles Peskineef86ab22017-05-05 18:59:02 +0200640 ret = mbedtls_x509_crt_verify_with_profile( &crt, &ca, NULL,
641 &compat_profile,
Manuel Pégourié-Gonnarda6568252017-07-05 18:14:38 +0200642 name, &flags,
Gilles Peskineef86ab22017-05-05 18:59:02 +0200643 verify_print, &vrfy_ctx );
Manuel Pégourié-Gonnard560fea32015-09-01 11:59:24 +0200644
645 TEST_ASSERT( ret == exp_ret );
646 TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
647
648exit:
649 mbedtls_x509_crt_free( &crt );
650 mbedtls_x509_crt_free( &ca );
651}
652/* END_CASE */
653
654/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100655void mbedtls_x509_dn_gets( char * crt_file, char * entity, char * result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000656{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000658 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200659 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000662 memset( buf, 0, 2000 );
663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200665 if( strcmp( entity, "subject" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 res = mbedtls_x509_dn_gets( buf, 2000, &crt.subject );
Paul Bakker33b43f12013-08-20 11:48:36 +0200667 else if( strcmp( entity, "issuer" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 res = mbedtls_x509_dn_gets( buf, 2000, &crt.issuer );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200669 else
670 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000671
672 TEST_ASSERT( res != -1 );
673 TEST_ASSERT( res != -2 );
674
Paul Bakker33b43f12013-08-20 11:48:36 +0200675 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200676
677exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000679}
Paul Bakker33b43f12013-08-20 11:48:36 +0200680/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100683void mbedtls_x509_time_is_past( char * crt_file, char * entity, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000684{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200690
Paul Bakker33b43f12013-08-20 11:48:36 +0200691 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100692 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_from ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200693 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100694 TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_to ) == result );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200695 else
696 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000697
Paul Bakkerbd51b262014-07-10 15:26:12 +0200698exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000700}
Paul Bakker33b43f12013-08-20 11:48:36 +0200701/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100704void mbedtls_x509_time_is_future( char * crt_file, char * entity, int result )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100705{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100711
712 if( strcmp( entity, "valid_from" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100713 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_from ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100714 else if( strcmp( entity, "valid_to" ) == 0 )
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100715 TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_to ) == result );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100716 else
717 TEST_ASSERT( "Unknown entity" == 0 );
718
Paul Bakkerbd51b262014-07-10 15:26:12 +0200719exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100721}
722/* END_CASE */
723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100725void x509parse_crt_file( char * crt_file, int result )
Paul Bakker5a5fa922014-09-26 14:53:04 +0200726{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_x509_crt crt;
Paul Bakker5a5fa922014-09-26 14:53:04 +0200728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_x509_crt_init( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200732
733exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_x509_crt_free( &crt );
Paul Bakker5a5fa922014-09-26 14:53:04 +0200735}
736/* END_CASE */
737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100739void x509parse_crt( data_t * buf, char * result_str, int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000740{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_x509_crt crt;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000742 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100743 int res;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 mbedtls_x509_crt_init( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000746 memset( output, 0, 2000 );
747
Hanno Becker2d8a2c02019-01-31 09:15:53 +0000748 TEST_ASSERT( mbedtls_x509_crt_parse_der( &crt, buf->x, buf->len ) == ( result ) );
749 if( ( result ) == 0 )
750 {
751 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000752
Hanno Becker2d8a2c02019-01-31 09:15:53 +0000753 TEST_ASSERT( res != -1 );
754 TEST_ASSERT( res != -2 );
755
756 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
757 }
758
759 mbedtls_x509_crt_free( &crt );
760 mbedtls_x509_crt_init( &crt );
761 memset( output, 0, 2000 );
762
763 TEST_ASSERT( mbedtls_x509_crt_parse_der_nocopy( &crt, buf->x, buf->len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200764 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200767
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000768 TEST_ASSERT( res != -1 );
769 TEST_ASSERT( res != -2 );
770
Paul Bakker33b43f12013-08-20 11:48:36 +0200771 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000772 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000773
Paul Bakkerbd51b262014-07-10 15:26:12 +0200774exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000776}
Paul Bakker33b43f12013-08-20 11:48:36 +0200777/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100780void x509parse_crl( data_t * buf, char * result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000781{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 mbedtls_x509_crl crl;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000783 unsigned char output[2000];
Azim Khanf1aaec92017-05-30 14:23:15 +0100784 int res;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 mbedtls_x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000787 memset( output, 0, 2000 );
788
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000789
Azim Khand30ca132017-06-09 04:32:58 +0100790 TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf->x, buf->len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200791 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200794
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000795 TEST_ASSERT( res != -1 );
796 TEST_ASSERT( res != -2 );
797
Paul Bakker33b43f12013-08-20 11:48:36 +0200798 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000799 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000800
Paul Bakkerbd51b262014-07-10 15:26:12 +0200801exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000803}
Paul Bakker33b43f12013-08-20 11:48:36 +0200804/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100807void mbedtls_x509_csr_parse( data_t * csr_der, char * ref_out, int ref_ret )
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200808{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 mbedtls_x509_csr csr;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200810 char my_out[1000];
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200811 int my_ret;
812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 mbedtls_x509_csr_init( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200814 memset( my_out, 0, sizeof( my_out ) );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200815
Azim Khand30ca132017-06-09 04:32:58 +0100816 my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der->x, csr_der->len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200817 TEST_ASSERT( my_ret == ref_ret );
818
819 if( ref_ret == 0 )
820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200822 TEST_ASSERT( my_out_len == strlen( ref_out ) );
823 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
824 }
825
Paul Bakkerbd51b262014-07-10 15:26:12 +0200826exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 mbedtls_x509_csr_free( &csr );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200828}
829/* END_CASE */
830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100832void mbedtls_x509_crt_parse_path( char * crt_path, int ret, int nb_crt )
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100833{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 mbedtls_x509_crt chain, *cur;
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100835 int i;
836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 mbedtls_x509_crt_init( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100840
841 /* Check how many certs we got */
842 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
843 if( cur->raw.p != NULL )
844 i++;
845
846 TEST_ASSERT( i == nb_crt );
847
Paul Bakkerbd51b262014-07-10 15:26:12 +0200848exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 mbedtls_x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100850}
851/* END_CASE */
852
Janos Follath822b2c32015-10-11 10:25:22 +0200853/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +0200854void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int,
855 int ret_chk, int flags_chk )
856{
857 char file_buf[128];
858 int ret;
859 uint32_t flags;
860 mbedtls_x509_crt trusted, chain;
861
Jaeden Amero3ea26872019-02-13 11:30:22 +0000862#if defined(MBEDTLS_USE_PSA_CRYPTO)
863 TEST_ASSERT( psa_crypto_init() == 0 );
864#endif
865
Manuel Pégourié-Gonnard1beb0482017-06-05 13:49:44 +0200866 /*
867 * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
868 * with NN.crt signed by NN-1.crt
869 */
870
871 mbedtls_x509_crt_init( &trusted );
872 mbedtls_x509_crt_init( &chain );
873
874 /* Load trusted root */
875 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 );
876
877 /* Load a chain with nb_int intermediates (from 01 to nb_int),
878 * plus one "end-entity" cert (nb_int + 1) */
879 ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
880 nb_int + 1 );
881 TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf );
882 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 );
883
884 /* Try to verify that chain */
885 ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags,
886 NULL, NULL );
887 TEST_ASSERT( ret == ret_chk );
888 TEST_ASSERT( flags == (uint32_t) flags_chk );
889
890exit:
891 mbedtls_x509_crt_free( &chain );
892 mbedtls_x509_crt_free( &trusted );
893}
894/* END_CASE */
895
896/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200897void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca,
898 int flags_result, int result,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200899 char *profile_name, int vrfy_fatal_lvls )
Janos Follath822b2c32015-10-11 10:25:22 +0200900{
901 char* act;
902 uint32_t flags;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200903 int res;
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100904 mbedtls_x509_crt trusted, chain;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200905 const mbedtls_x509_crt_profile *profile = NULL;
Janos Follathef4f2582015-10-11 16:17:27 +0200906
Jaeden Amero3ea26872019-02-13 11:30:22 +0000907#if defined(MBEDTLS_USE_PSA_CRYPTO)
908 TEST_ASSERT( psa_crypto_init() == 0 );
909#endif
910
Janos Follath822b2c32015-10-11 10:25:22 +0200911 mbedtls_x509_crt_init( &chain );
912 mbedtls_x509_crt_init( &trusted );
913
Manuel Pégourié-Gonnarda8838af2015-11-02 06:34:46 +0900914 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100915 TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
916 TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
Janos Follath822b2c32015-10-11 10:25:22 +0200917
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200918 if( strcmp( profile_name, "" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200919 profile = &mbedtls_x509_crt_profile_default;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200920 else if( strcmp( profile_name, "next" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200921 profile = &mbedtls_x509_crt_profile_next;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200922 else if( strcmp( profile_name, "suiteb" ) == 0 )
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200923 profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200924 else if( strcmp( profile_name, "rsa3072" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200925 profile = &profile_rsa3072;
Manuel Pégourié-Gonnardea2dc142017-08-08 11:10:37 +0200926 else if( strcmp( profile_name, "sha512" ) == 0 )
Manuel Pégourié-Gonnard6622fed2017-05-23 11:29:29 +0200927 profile = &profile_sha512;
Manuel Pégourié-Gonnarde54931f2017-05-22 12:04:25 +0200928
929 res = mbedtls_x509_crt_verify_with_profile( &chain, &trusted, NULL, profile,
Manuel Pégourié-Gonnard6b9d53f2017-05-23 12:26:58 +0200930 NULL, &flags, verify_fatal, &vrfy_fatal_lvls );
Janos Follathef4f2582015-10-11 16:17:27 +0200931
932 TEST_ASSERT( res == ( result ) );
933 TEST_ASSERT( flags == (uint32_t)( flags_result ) );
Janos Follath822b2c32015-10-11 10:25:22 +0200934
935exit:
936 mbedtls_x509_crt_free( &trusted );
937 mbedtls_x509_crt_free( &chain );
938}
939/* END_CASE */
940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100942void x509_oid_desc( data_t * buf, char * ref_desc )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100943{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000945 const char *desc = NULL;
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000946 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100947
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +0100950 oid.p = buf->x;
951 oid.len = buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100954
955 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000956 {
957 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100958 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000959 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100960 else
961 {
Manuel Pégourié-Gonnard48d3cef2015-03-20 18:14:26 +0000962 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100963 TEST_ASSERT( desc != NULL );
964 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
965 }
966}
967/* END_CASE */
968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100970void x509_oid_numstr( data_t * oid_buf, char * numstr, int blen, int ret )
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100971{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 mbedtls_x509_buf oid;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100973 char num_buf[100];
974
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100975 memset( num_buf, 0x2a, sizeof num_buf );
976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 oid.tag = MBEDTLS_ASN1_OID;
Azim Khand30ca132017-06-09 04:32:58 +0100978 oid.p = oid_buf->x;
979 oid.len = oid_buf->len;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100980
981 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100984
985 if( ret >= 0 )
986 {
987 TEST_ASSERT( num_buf[ret] == 0 );
988 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
989 }
990}
991/* END_CASE */
992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_KEY_USAGE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100994void x509_check_key_usage( char * crt_file, int usage, int ret )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200995{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001003
Paul Bakkerbd51b262014-07-10 15:26:12 +02001004exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001006}
1007/* END_CASE */
1008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009/* 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 +01001010void x509_check_extended_key_usage( char * crt_file, data_t * oid, int ret
Azim Khand30ca132017-06-09 04:32:58 +01001011 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001012{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 mbedtls_x509_crt crt;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 mbedtls_x509_crt_init( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001016
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001019
Azim Khand30ca132017-06-09 04:32:58 +01001020 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 +02001021
Paul Bakkerbd51b262014-07-10 15:26:12 +02001022exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001024}
1025/* END_CASE */
1026
Andres AG4b76aec2016-09-23 13:16:02 +01001027/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +01001028void x509_get_time( int tag, char * time_str, int ret, int year, int mon,
1029 int day, int hour, int min, int sec )
Andres AG4b76aec2016-09-23 13:16:02 +01001030{
1031 mbedtls_x509_time time;
Janos Follathea7054a2017-02-08 14:13:02 +00001032 unsigned char buf[21];
Andres AG4b76aec2016-09-23 13:16:02 +01001033 unsigned char* start = buf;
1034 unsigned char* end = buf;
1035
1036 memset( &time, 0x00, sizeof( time ) );
1037 *end = (unsigned char)tag; end++;
Janos Follathea7054a2017-02-08 14:13:02 +00001038 *end = strlen( time_str );
1039 TEST_ASSERT( *end < 20 );
Andres AG4b76aec2016-09-23 13:16:02 +01001040 end++;
1041 memcpy( end, time_str, (size_t)*(end - 1) );
1042 end += *(end - 1);
1043
1044 TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
1045 if( ret == 0 )
1046 {
1047 TEST_ASSERT( year == time.year );
1048 TEST_ASSERT( mon == time.mon );
1049 TEST_ASSERT( day == time.day );
1050 TEST_ASSERT( hour == time.hour );
1051 TEST_ASSERT( min == time.min );
1052 TEST_ASSERT( sec == time.sec );
1053 }
1054}
1055/* END_CASE */
1056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Azim Khan5fcca462018-06-29 11:05:32 +01001058void x509_parse_rsassa_pss_params( data_t * hex_params, int params_tag,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001059 int ref_msg_md, int ref_mgf_md,
1060 int ref_salt_len, int ref_ret )
1061{
1062 int my_ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 mbedtls_x509_buf params;
1064 mbedtls_md_type_t my_msg_md, my_mgf_md;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001065 int my_salt_len;
1066
Azim Khand30ca132017-06-09 04:32:58 +01001067 params.p = hex_params->x;
1068 params.len = hex_params->len;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001069 params.tag = params_tag;
1070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 my_ret = mbedtls_x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001072 &my_salt_len );
1073
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001074 TEST_ASSERT( my_ret == ref_ret );
1075
1076 if( ref_ret == 0 )
1077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md );
1079 TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md );
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001080 TEST_ASSERT( my_salt_len == ref_salt_len );
1081 }
1082
Paul Bakkerbd51b262014-07-10 15:26:12 +02001083exit:
Azim Khand30ca132017-06-09 04:32:58 +01001084 ;;
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +02001085}
1086/* END_CASE */
1087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001089void x509_selftest( )
Paul Bakker37940d9f2009-07-10 22:38:58 +00001090{
Andres AG93012e82016-09-09 09:10:28 +01001091 TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +00001092}
Paul Bakker33b43f12013-08-20 11:48:36 +02001093/* END_CASE */