blob: 18837c532b62203dd01d3f39212aceb19b543a53 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Rich Evansce2f2372015-02-06 13:57:42 +00002#include "polarssl/x509_crt.h"
3#include "polarssl/x509_crl.h"
4#include "polarssl/x509_csr.h"
5#include "polarssl/pem.h"
6#include "polarssl/oid.h"
7#include "polarssl/base64.h"
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008
Paul Bakkerc559c7a2013-09-18 14:13:26 +02009int verify_none( void *data, x509_crt *crt, int certificate_depth, int *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000010{
Paul Bakker5a624082011-01-18 16:31:52 +000011 ((void) data);
12 ((void) crt);
13 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000014 *flags |= BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020015
Paul Bakker915275b2012-09-28 07:10:55 +000016 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000017}
18
Paul Bakkerc559c7a2013-09-18 14:13:26 +020019int verify_all( void *data, x509_crt *crt, int certificate_depth, int *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000020{
Paul Bakker5a624082011-01-18 16:31:52 +000021 ((void) data);
22 ((void) crt);
23 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000024 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000025
Paul Bakkerb63b0af2011-01-13 17:54:59 +000026 return 0;
27}
28
Manuel Pégourié-Gonnard15f10882015-09-01 11:59:24 +020029#if defined(POLARSSL_X509_CRT_PARSE_C)
30typedef struct {
31 char buf[512];
32 char *p;
33} verify_print_context;
34
35void verify_print_init( verify_print_context *ctx )
36{
37 memset( ctx, 0, sizeof( verify_print_context ) );
38 ctx->p = ctx->buf;
39}
40
41#if defined(_MSC_VER) && !defined snprintf
42#define snprintf _snprintf
43#endif
44
45#define SAFE_SNPRINTF \
46do \
47{ \
48 if( ret < 0 || (size_t) ret > n ) \
49 { \
50 p[n - 1] = '\0'; \
51 return( -1 ); \
52 } \
53 \
54 n -= (unsigned int) ret; \
55 p += (unsigned int) ret; \
56} while( 0 )
57
58int verify_print( void *data, x509_crt *crt, int certificate_depth, int *flags )
59{
60 int ret;
61 verify_print_context *ctx = (verify_print_context *) data;
62 char *p = ctx->p;
63 size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p;
64 ((void) flags);
65
66 ret = polarssl_snprintf( p, n, "depth %d - serial ", certificate_depth );
67 SAFE_SNPRINTF;
68
69 ret = x509_serial_gets( p, n, &crt->serial );
70 SAFE_SNPRINTF;
71
72 ret = polarssl_snprintf( p, n, " - subject " );
73 SAFE_SNPRINTF;
74
75 ret = x509_dn_gets( p, n, &crt->subject );
76 SAFE_SNPRINTF;
77
78 ret = polarssl_snprintf( p, n, "\n" );
79 SAFE_SNPRINTF;
80
81 ctx->p = p;
82
83 return( 0 );
84}
85#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard28e1ac52015-11-02 06:34:46 +090086
87/* strsep() not available on Windows */
88char *mystrsep(char **stringp, const char *delim)
89{
90 const char *p;
91 char *ret = *stringp;
92
93 if( *stringp == NULL )
94 return( NULL );
95
96 for( ; ; (*stringp)++ )
97 {
98 if( **stringp == '\0' )
99 {
100 *stringp = NULL;
101 goto done;
102 }
103
104 for( p = delim; *p != '\0'; p++ )
105 if( **stringp == *p )
106 {
107 **stringp = '\0';
108 (*stringp)++;
109 goto done;
110 }
111 }
112
113done:
114 return( ret );
115}
Paul Bakker33b43f12013-08-20 11:48:36 +0200116/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000117
Paul Bakker33b43f12013-08-20 11:48:36 +0200118/* BEGIN_DEPENDENCIES
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119 * depends_on:POLARSSL_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200120 * END_DEPENDENCIES
121 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000122
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200123/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200124void x509_cert_info( char *crt_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000125{
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200126 x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000127 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000128 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000129
Paul Bakker369d2eb2013-09-18 11:58:25 +0200130 x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000131 memset( buf, 0, 2000 );
132
Paul Bakkerddf26b42013-09-18 13:46:23 +0200133 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
134 res = x509_crt_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000135
136 TEST_ASSERT( res != -1 );
137 TEST_ASSERT( res != -2 );
138
Paul Bakker33b43f12013-08-20 11:48:36 +0200139 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200140
141exit:
142 x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000143}
Paul Bakker33b43f12013-08-20 11:48:36 +0200144/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000145
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200146/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200147void x509_crl_info( char *crl_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000148{
149 x509_crl crl;
150 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000151 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000152
Paul Bakker369d2eb2013-09-18 11:58:25 +0200153 x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000154 memset( buf, 0, 2000 );
155
Paul Bakkerddf26b42013-09-18 13:46:23 +0200156 TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == 0 );
157 res = x509_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000158
159 TEST_ASSERT( res != -1 );
160 TEST_ASSERT( res != -2 );
161
Paul Bakker33b43f12013-08-20 11:48:36 +0200162 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200163
164exit:
165 x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000166}
Paul Bakker33b43f12013-08-20 11:48:36 +0200167/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000168
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100169/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CSR_PARSE_C */
170void x509_csr_info( char *csr_file, char *result_str )
171{
172 x509_csr csr;
173 char buf[2000];
174 int res;
175
176 x509_csr_init( &csr );
177 memset( buf, 0, 2000 );
178
179 TEST_ASSERT( x509_csr_parse_file( &csr, csr_file ) == 0 );
180 res = x509_csr_info( buf, 2000, "", &csr );
181
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100182 TEST_ASSERT( res != -1 );
183 TEST_ASSERT( res != -2 );
184
185 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200186
187exit:
188 x509_csr_free( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100189}
190/* END_CASE */
191
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200192/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C */
193void x509_verify_info( int flags, char *prefix, char *result_str )
194{
195 char buf[2000];
196 int res;
197
198 memset( buf, 0, sizeof( buf ) );
199
200 res = x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
201
202 TEST_ASSERT( res >= 0 );
203
204 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
205}
206/* END_CASE */
207
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200208/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C:POLARSSL_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200209void x509_verify( char *crt_file, char *ca_file, char *crl_file,
210 char *cn_name_str, int result, int flags_result,
211 char *verify_callback )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000212{
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200213 x509_crt crt;
214 x509_crt ca;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000215 x509_crl crl;
216 int flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000217 int res;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200218 int (*f_vrfy)(void *, x509_crt *, int, int *) = NULL;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200219 char * cn_name = NULL;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000220
Paul Bakker369d2eb2013-09-18 11:58:25 +0200221 x509_crt_init( &crt );
222 x509_crt_init( &ca );
223 x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000224
Paul Bakker33b43f12013-08-20 11:48:36 +0200225 if( strcmp( cn_name_str, "NULL" ) != 0 )
226 cn_name = cn_name_str;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200227
Paul Bakker33b43f12013-08-20 11:48:36 +0200228 if( strcmp( verify_callback, "NULL" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200229 f_vrfy = NULL;
Paul Bakker33b43f12013-08-20 11:48:36 +0200230 else if( strcmp( verify_callback, "verify_none" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200231 f_vrfy = verify_none;
Paul Bakker33b43f12013-08-20 11:48:36 +0200232 else if( strcmp( verify_callback, "verify_all" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200233 f_vrfy = verify_all;
234 else
235 TEST_ASSERT( "No known verify callback selected" == 0 );
236
Paul Bakkerddf26b42013-09-18 13:46:23 +0200237 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
238 TEST_ASSERT( x509_crt_parse_file( &ca, ca_file ) == 0 );
239 TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000240
Paul Bakkerddf26b42013-09-18 13:46:23 +0200241 res = x509_crt_verify( &crt, &ca, &crl, cn_name, &flags, f_vrfy, NULL );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000242
Paul Bakkerbd51b262014-07-10 15:26:12 +0200243 TEST_ASSERT( res == ( result ) );
244 TEST_ASSERT( flags == ( flags_result ) );
245
246exit:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200247 x509_crt_free( &crt );
248 x509_crt_free( &ca );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000249 x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000250}
Paul Bakker33b43f12013-08-20 11:48:36 +0200251/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000252
Manuel Pégourié-Gonnard15f10882015-09-01 11:59:24 +0200253/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
254void x509_verify_callback( char *crt_file, char *ca_file,
255 int exp_ret, char *exp_vrfy_out )
256{
257 int ret;
258 x509_crt crt;
259 x509_crt ca;
260 int flags = 0;
261 verify_print_context vrfy_ctx;
262
263 x509_crt_init( &crt );
264 x509_crt_init( &ca );
265 verify_print_init( &vrfy_ctx );
266
267 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
268 TEST_ASSERT( x509_crt_parse_file( &ca, ca_file ) == 0 );
269
270 ret = x509_crt_verify( &crt, &ca, NULL, NULL, &flags,
271 verify_print, &vrfy_ctx );
272
273 TEST_ASSERT( ret == exp_ret );
274 TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
275
276exit:
277 x509_crt_free( &crt );
278 x509_crt_free( &ca );
279}
280/* END_CASE */
281
Manuel Pégourié-Gonnard8f63e952015-09-01 18:44:47 +0200282/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200283void x509_dn_gets( char *crt_file, char *entity, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000284{
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200285 x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000286 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200287 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000288
Paul Bakker369d2eb2013-09-18 11:58:25 +0200289 x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000290 memset( buf, 0, 2000 );
291
Paul Bakkerddf26b42013-09-18 13:46:23 +0200292 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200293 if( strcmp( entity, "subject" ) == 0 )
Paul Bakker86d0c192013-09-18 11:11:02 +0200294 res = x509_dn_gets( buf, 2000, &crt.subject );
Paul Bakker33b43f12013-08-20 11:48:36 +0200295 else if( strcmp( entity, "issuer" ) == 0 )
Paul Bakker86d0c192013-09-18 11:11:02 +0200296 res = x509_dn_gets( buf, 2000, &crt.issuer );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200297 else
298 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000299
300 TEST_ASSERT( res != -1 );
301 TEST_ASSERT( res != -2 );
302
Paul Bakker33b43f12013-08-20 11:48:36 +0200303 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200304
305exit:
306 x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000307}
Paul Bakker33b43f12013-08-20 11:48:36 +0200308/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000309
Manuel Pégourié-Gonnard8f63e952015-09-01 18:44:47 +0200310/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200311void x509_time_expired( char *crt_file, char *entity, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000312{
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200313 x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000314
Paul Bakker369d2eb2013-09-18 11:58:25 +0200315 x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000316
Paul Bakkerddf26b42013-09-18 13:46:23 +0200317 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200318
Paul Bakker33b43f12013-08-20 11:48:36 +0200319 if( strcmp( entity, "valid_from" ) == 0 )
Paul Bakker86d0c192013-09-18 11:11:02 +0200320 TEST_ASSERT( x509_time_expired( &crt.valid_from ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200321 else if( strcmp( entity, "valid_to" ) == 0 )
Paul Bakker86d0c192013-09-18 11:11:02 +0200322 TEST_ASSERT( x509_time_expired( &crt.valid_to ) == result );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200323 else
324 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000325
Paul Bakkerbd51b262014-07-10 15:26:12 +0200326exit:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200327 x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000328}
Paul Bakker33b43f12013-08-20 11:48:36 +0200329/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000330
Manuel Pégourié-Gonnard8f63e952015-09-01 18:44:47 +0200331/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100332void x509_time_future( char *crt_file, char *entity, int result )
333{
334 x509_crt crt;
335
336 x509_crt_init( &crt );
337
338 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
339
340 if( strcmp( entity, "valid_from" ) == 0 )
341 TEST_ASSERT( x509_time_future( &crt.valid_from ) == result );
342 else if( strcmp( entity, "valid_to" ) == 0 )
343 TEST_ASSERT( x509_time_future( &crt.valid_to ) == result );
344 else
345 TEST_ASSERT( "Unknown entity" == 0 );
346
Paul Bakkerbd51b262014-07-10 15:26:12 +0200347exit:
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100348 x509_crt_free( &crt );
349}
350/* END_CASE */
351
Paul Bakker5a5fa922014-09-26 14:53:04 +0200352/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C:POLARSSL_FS_IO */
353void x509parse_crt_file( char *crt_file, int result )
354{
355 x509_crt crt;
356
357 x509_crt_init( &crt );
358
359 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == result );
360
361exit:
362 x509_crt_free( &crt );
363}
364/* END_CASE */
365
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200367void x509parse_crt( char *crt_data, char *result_str, int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000368{
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200369 x509_crt crt;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000370 unsigned char buf[2000];
371 unsigned char output[2000];
372 int data_len, res;
373
Paul Bakker369d2eb2013-09-18 11:58:25 +0200374 x509_crt_init( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000375 memset( buf, 0, 2000 );
376 memset( output, 0, 2000 );
377
Paul Bakker33b43f12013-08-20 11:48:36 +0200378 data_len = unhexify( buf, crt_data );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000379
Paul Bakkerddf26b42013-09-18 13:46:23 +0200380 TEST_ASSERT( x509_crt_parse( &crt, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200381 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000382 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200383 res = x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200384
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000385 TEST_ASSERT( res != -1 );
386 TEST_ASSERT( res != -2 );
387
Paul Bakker33b43f12013-08-20 11:48:36 +0200388 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000389 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000390
Paul Bakkerbd51b262014-07-10 15:26:12 +0200391exit:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392 x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000393}
Paul Bakker33b43f12013-08-20 11:48:36 +0200394/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000395
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396/* BEGIN_CASE depends_on:POLARSSL_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200397void x509parse_crl( char *crl_data, char *result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000398{
399 x509_crl crl;
400 unsigned char buf[2000];
401 unsigned char output[2000];
402 int data_len, res;
403
Paul Bakker369d2eb2013-09-18 11:58:25 +0200404 x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000405 memset( buf, 0, 2000 );
406 memset( output, 0, 2000 );
407
Paul Bakker33b43f12013-08-20 11:48:36 +0200408 data_len = unhexify( buf, crl_data );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000409
Paul Bakkerddf26b42013-09-18 13:46:23 +0200410 TEST_ASSERT( x509_crl_parse( &crl, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200411 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000412 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200413 res = x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200414
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000415 TEST_ASSERT( res != -1 );
416 TEST_ASSERT( res != -2 );
417
Paul Bakker33b43f12013-08-20 11:48:36 +0200418 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000419 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000420
Paul Bakkerbd51b262014-07-10 15:26:12 +0200421exit:
Paul Bakkerb08e6842012-02-11 18:43:20 +0000422 x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000423}
Paul Bakker33b43f12013-08-20 11:48:36 +0200424/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000425
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200426/* BEGIN_CASE depends_on:POLARSSL_X509_CSR_PARSE_C */
427void x509_csr_parse( char *csr_der_hex, char *ref_out, int ref_ret )
428{
429 x509_csr csr;
Paul Bakkerbd51b262014-07-10 15:26:12 +0200430 unsigned char *csr_der = NULL;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200431 char my_out[1000];
432 size_t csr_der_len;
433 int my_ret;
434
435 x509_csr_init( &csr );
436 memset( my_out, 0, sizeof( my_out ) );
437 csr_der = unhexify_alloc( csr_der_hex, &csr_der_len );
438
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200439 my_ret = x509_csr_parse_der( &csr, csr_der, csr_der_len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200440 TEST_ASSERT( my_ret == ref_ret );
441
442 if( ref_ret == 0 )
443 {
444 size_t my_out_len = x509_csr_info( my_out, sizeof( my_out ), "", &csr );
445 TEST_ASSERT( my_out_len == strlen( ref_out ) );
446 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
447 }
448
Paul Bakkerbd51b262014-07-10 15:26:12 +0200449exit:
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200450 x509_csr_free( &csr );
451 polarssl_free( csr_der );
452}
453/* END_CASE */
454
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100455/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
456void x509_crt_parse_path( char *crt_path, int ret, int nb_crt )
457{
458 x509_crt chain, *cur;
459 int i;
460
461 x509_crt_init( &chain );
462
463 TEST_ASSERT( x509_crt_parse_path( &chain, crt_path ) == ret );
464
465 /* Check how many certs we got */
466 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
467 if( cur->raw.p != NULL )
468 i++;
469
470 TEST_ASSERT( i == nb_crt );
471
Paul Bakkerbd51b262014-07-10 15:26:12 +0200472exit:
Paul Bakkera2ffccd2013-12-02 21:56:37 +0100473 x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100474}
475/* END_CASE */
476
Janos Follath189c7432015-10-11 10:25:22 +0200477/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
Janos Follath3d98a7e2015-10-11 16:17:27 +0200478void x509_crt_verify_chain( char *chain_paths, char *trusted_ca, int flags_result )
Janos Follath189c7432015-10-11 10:25:22 +0200479{
480 char* act;
481 int flags;
Janos Follath3d98a7e2015-10-11 16:17:27 +0200482 int result, res;
Janos Follath189c7432015-10-11 10:25:22 +0200483 x509_crt trusted, chain;
484
Janos Follath3d98a7e2015-10-11 16:17:27 +0200485 result = flags_result ? POLARSSL_ERR_X509_CERT_VERIFY_FAILED : 0;
486
Janos Follath189c7432015-10-11 10:25:22 +0200487 x509_crt_init( &chain );
488 x509_crt_init( &trusted );
489
Manuel Pégourié-Gonnard28e1ac52015-11-02 06:34:46 +0900490 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Janos Follath189c7432015-10-11 10:25:22 +0200491 TEST_ASSERT( x509_crt_parse_file( &chain, act ) == 0 );
492 TEST_ASSERT( x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
493
494 res = x509_crt_verify( &chain, &trusted, NULL, NULL, &flags, NULL, NULL );
495
Janos Follath3d98a7e2015-10-11 16:17:27 +0200496 TEST_ASSERT( res == result );
497 TEST_ASSERT( flags == flags_result );
Janos Follath189c7432015-10-11 10:25:22 +0200498
499exit:
500 x509_crt_free( &trusted );
501 x509_crt_free( &chain );
502}
503/* END_CASE */
504
Manuel Pégourié-Gonnard0f7b6192014-06-24 11:37:54 +0200505/* BEGIN_CASE depends_on:POLARSSL_X509_USE_C */
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100506void x509_oid_desc( char *oid_str, char *ref_desc )
507{
508 x509_buf oid;
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000509 const char *desc = NULL;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100510 unsigned char buf[20];
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000511 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100512
513 memset( buf, 0, sizeof buf );
514
515 oid.tag = ASN1_OID;
516 oid.len = unhexify( buf, oid_str );
517 oid.p = buf;
518
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000519 ret = oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100520
521 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000522 {
523 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100524 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000525 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100526 else
527 {
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000528 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100529 TEST_ASSERT( desc != NULL );
530 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
531 }
532}
533/* END_CASE */
534
Manuel Pégourié-Gonnard0f7b6192014-06-24 11:37:54 +0200535/* BEGIN_CASE depends_on:POLARSSL_X509_USE_C */
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100536void x509_oid_numstr( char *oid_str, char *numstr, int blen, int ret )
537{
538 x509_buf oid;
539 unsigned char oid_buf[20];
540 char num_buf[100];
541
542 memset( oid_buf, 0x00, sizeof oid_buf );
543 memset( num_buf, 0x2a, sizeof num_buf );
544
545 oid.tag = ASN1_OID;
546 oid.len = unhexify( oid_buf, oid_str );
547 oid.p = oid_buf;
548
549 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
550
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000551 TEST_ASSERT( oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100552
553 if( ret >= 0 )
554 {
555 TEST_ASSERT( num_buf[ret] == 0 );
556 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
557 }
558}
559/* END_CASE */
560
Paul Bakker5b11d022014-07-10 13:54:38 +0200561/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C:POLARSSL_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200562void x509_check_key_usage( char *crt_file, int usage, int ret )
563{
564 x509_crt crt;
565
566 x509_crt_init( &crt );
567
568 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
569
570 TEST_ASSERT( x509_crt_check_key_usage( &crt, usage ) == ret );
571
Paul Bakkerbd51b262014-07-10 15:26:12 +0200572exit:
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200573 x509_crt_free( &crt );
574}
575/* END_CASE */
576
Paul Bakker5b11d022014-07-10 13:54:38 +0200577/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C:POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200578void x509_check_extended_key_usage( char *crt_file, char *usage_hex, int ret )
579{
580 x509_crt crt;
581 char oid[50];
582 size_t len;
583
584 x509_crt_init( &crt );
585
586 len = unhexify( (unsigned char *) oid, usage_hex );
587
588 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
589
590 TEST_ASSERT( x509_crt_check_extended_key_usage( &crt, oid, len ) == ret );
591
Paul Bakkerbd51b262014-07-10 15:26:12 +0200592exit:
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200593 x509_crt_free( &crt );
594}
595/* END_CASE */
596
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200597/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C:POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200598void x509_parse_rsassa_pss_params( char *hex_params, int params_tag,
599 int ref_msg_md, int ref_mgf_md,
600 int ref_salt_len, int ref_ret )
601{
602 int my_ret;
603 x509_buf params;
604 md_type_t my_msg_md, my_mgf_md;
605 int my_salt_len;
606
607 params.p = unhexify_alloc( hex_params, &params.len );
608 params.tag = params_tag;
609
610 my_ret = x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
611 &my_salt_len );
612
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200613 TEST_ASSERT( my_ret == ref_ret );
614
615 if( ref_ret == 0 )
616 {
617 TEST_ASSERT( my_msg_md == (md_type_t) ref_msg_md );
618 TEST_ASSERT( my_mgf_md == (md_type_t) ref_mgf_md );
619 TEST_ASSERT( my_salt_len == ref_salt_len );
620 }
621
Paul Bakkerbd51b262014-07-10 15:26:12 +0200622exit:
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200623 polarssl_free( params.p );
624}
625/* END_CASE */
626
Manuel Pégourié-Gonnard20140162013-10-10 12:48:03 +0200627/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C:POLARSSL_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200628void x509_selftest()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000629{
630 TEST_ASSERT( x509_self_test( 0 ) == 0 );
631}
Paul Bakker33b43f12013-08-20 11:48:36 +0200632/* END_CASE */