blob: 4ae3c9fc8db215f080b3a3a27dfdddf3659be801 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Andres AG0da3e442016-09-23 13:16:02 +01002#include "polarssl/x509.h"
Rich Evansce2f2372015-02-06 13:57:42 +00003#include "polarssl/x509_crt.h"
4#include "polarssl/x509_crl.h"
5#include "polarssl/x509_csr.h"
6#include "polarssl/pem.h"
7#include "polarssl/oid.h"
8#include "polarssl/base64.h"
Paul Bakkerb63b0af2011-01-13 17:54:59 +00009
Paul Bakkerc559c7a2013-09-18 14:13:26 +020010int verify_none( void *data, x509_crt *crt, int certificate_depth, int *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000011{
Paul Bakker5a624082011-01-18 16:31:52 +000012 ((void) data);
13 ((void) crt);
14 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000015 *flags |= BADCERT_OTHER;
Paul Bakkerddf26b42013-09-18 13:46:23 +020016
Paul Bakker915275b2012-09-28 07:10:55 +000017 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000018}
19
Paul Bakkerc559c7a2013-09-18 14:13:26 +020020int verify_all( void *data, x509_crt *crt, int certificate_depth, int *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000021{
Paul Bakker5a624082011-01-18 16:31:52 +000022 ((void) data);
23 ((void) crt);
24 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000025 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000026
Paul Bakkerb63b0af2011-01-13 17:54:59 +000027 return 0;
28}
29
Manuel Pégourié-Gonnard15f10882015-09-01 11:59:24 +020030#if defined(POLARSSL_X509_CRT_PARSE_C)
31typedef struct {
32 char buf[512];
33 char *p;
34} verify_print_context;
35
36void verify_print_init( verify_print_context *ctx )
37{
38 memset( ctx, 0, sizeof( verify_print_context ) );
39 ctx->p = ctx->buf;
40}
41
42#if defined(_MSC_VER) && !defined snprintf
43#define snprintf _snprintf
44#endif
45
46#define SAFE_SNPRINTF \
47do \
48{ \
49 if( ret < 0 || (size_t) ret > n ) \
50 { \
51 p[n - 1] = '\0'; \
52 return( -1 ); \
53 } \
54 \
55 n -= (unsigned int) ret; \
56 p += (unsigned int) ret; \
57} while( 0 )
58
59int verify_print( void *data, x509_crt *crt, int certificate_depth, int *flags )
60{
61 int ret;
62 verify_print_context *ctx = (verify_print_context *) data;
63 char *p = ctx->p;
64 size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p;
65 ((void) flags);
66
67 ret = polarssl_snprintf( p, n, "depth %d - serial ", certificate_depth );
68 SAFE_SNPRINTF;
69
70 ret = x509_serial_gets( p, n, &crt->serial );
71 SAFE_SNPRINTF;
72
73 ret = polarssl_snprintf( p, n, " - subject " );
74 SAFE_SNPRINTF;
75
76 ret = x509_dn_gets( p, n, &crt->subject );
77 SAFE_SNPRINTF;
78
79 ret = polarssl_snprintf( p, n, "\n" );
80 SAFE_SNPRINTF;
81
82 ctx->p = p;
83
84 return( 0 );
85}
86#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard28e1ac52015-11-02 06:34:46 +090087
88/* strsep() not available on Windows */
89char *mystrsep(char **stringp, const char *delim)
90{
91 const char *p;
92 char *ret = *stringp;
93
94 if( *stringp == NULL )
95 return( NULL );
96
97 for( ; ; (*stringp)++ )
98 {
99 if( **stringp == '\0' )
100 {
101 *stringp = NULL;
102 goto done;
103 }
104
105 for( p = delim; *p != '\0'; p++ )
106 if( **stringp == *p )
107 {
108 **stringp = '\0';
109 (*stringp)++;
110 goto done;
111 }
112 }
113
114done:
115 return( ret );
116}
Paul Bakker33b43f12013-08-20 11:48:36 +0200117/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000118
Paul Bakker33b43f12013-08-20 11:48:36 +0200119/* BEGIN_DEPENDENCIES
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200120 * depends_on:POLARSSL_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200121 * END_DEPENDENCIES
122 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000123
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200124/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200125void x509_cert_info( char *crt_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000126{
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200127 x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000128 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000129 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000130
Paul Bakker369d2eb2013-09-18 11:58:25 +0200131 x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000132 memset( buf, 0, 2000 );
133
Paul Bakkerddf26b42013-09-18 13:46:23 +0200134 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
135 res = x509_crt_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000136
137 TEST_ASSERT( res != -1 );
138 TEST_ASSERT( res != -2 );
139
Paul Bakker33b43f12013-08-20 11:48:36 +0200140 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200141
142exit:
143 x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000144}
Paul Bakker33b43f12013-08-20 11:48:36 +0200145/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000146
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200147/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200148void x509_crl_info( char *crl_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000149{
150 x509_crl crl;
151 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000152 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000153
Paul Bakker369d2eb2013-09-18 11:58:25 +0200154 x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000155 memset( buf, 0, 2000 );
156
Paul Bakkerddf26b42013-09-18 13:46:23 +0200157 TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == 0 );
158 res = x509_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000159
160 TEST_ASSERT( res != -1 );
161 TEST_ASSERT( res != -2 );
162
Paul Bakker33b43f12013-08-20 11:48:36 +0200163 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200164
165exit:
166 x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000167}
Paul Bakker33b43f12013-08-20 11:48:36 +0200168/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000169
Andres AG67c6df42016-12-08 17:10:38 +0000170/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRL_PARSE_C */
171void x509_crl_parse( char *crl_file, int result )
172{
173 x509_crl crl;
174 char buf[2000];
175
176 x509_crl_init( &crl );
177 memset( buf, 0, 2000 );
178
179 TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == result );
180
181exit:
182 x509_crl_free( &crl );
183}
184/* END_CASE */
185
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100186/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CSR_PARSE_C */
187void x509_csr_info( char *csr_file, char *result_str )
188{
189 x509_csr csr;
190 char buf[2000];
191 int res;
192
193 x509_csr_init( &csr );
194 memset( buf, 0, 2000 );
195
196 TEST_ASSERT( x509_csr_parse_file( &csr, csr_file ) == 0 );
197 res = x509_csr_info( buf, 2000, "", &csr );
198
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100199 TEST_ASSERT( res != -1 );
200 TEST_ASSERT( res != -2 );
201
202 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200203
204exit:
205 x509_csr_free( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100206}
207/* END_CASE */
208
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200209/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C */
210void x509_verify_info( int flags, char *prefix, char *result_str )
211{
212 char buf[2000];
213 int res;
214
215 memset( buf, 0, sizeof( buf ) );
216
217 res = x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
218
219 TEST_ASSERT( res >= 0 );
220
221 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
222}
223/* END_CASE */
224
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200225/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C:POLARSSL_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200226void x509_verify( char *crt_file, char *ca_file, char *crl_file,
227 char *cn_name_str, int result, int flags_result,
228 char *verify_callback )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000229{
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200230 x509_crt crt;
231 x509_crt ca;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000232 x509_crl crl;
233 int flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000234 int res;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200235 int (*f_vrfy)(void *, x509_crt *, int, int *) = NULL;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200236 char * cn_name = NULL;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000237
Paul Bakker369d2eb2013-09-18 11:58:25 +0200238 x509_crt_init( &crt );
239 x509_crt_init( &ca );
240 x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000241
Paul Bakker33b43f12013-08-20 11:48:36 +0200242 if( strcmp( cn_name_str, "NULL" ) != 0 )
243 cn_name = cn_name_str;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200244
Paul Bakker33b43f12013-08-20 11:48:36 +0200245 if( strcmp( verify_callback, "NULL" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200246 f_vrfy = NULL;
Paul Bakker33b43f12013-08-20 11:48:36 +0200247 else if( strcmp( verify_callback, "verify_none" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200248 f_vrfy = verify_none;
Paul Bakker33b43f12013-08-20 11:48:36 +0200249 else if( strcmp( verify_callback, "verify_all" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200250 f_vrfy = verify_all;
251 else
252 TEST_ASSERT( "No known verify callback selected" == 0 );
253
Paul Bakkerddf26b42013-09-18 13:46:23 +0200254 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
255 TEST_ASSERT( x509_crt_parse_file( &ca, ca_file ) == 0 );
256 TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000257
Paul Bakkerddf26b42013-09-18 13:46:23 +0200258 res = x509_crt_verify( &crt, &ca, &crl, cn_name, &flags, f_vrfy, NULL );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000259
Paul Bakkerbd51b262014-07-10 15:26:12 +0200260 TEST_ASSERT( res == ( result ) );
261 TEST_ASSERT( flags == ( flags_result ) );
262
263exit:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200264 x509_crt_free( &crt );
265 x509_crt_free( &ca );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000266 x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000267}
Paul Bakker33b43f12013-08-20 11:48:36 +0200268/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000269
Manuel Pégourié-Gonnard15f10882015-09-01 11:59:24 +0200270/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
271void x509_verify_callback( char *crt_file, char *ca_file,
272 int exp_ret, char *exp_vrfy_out )
273{
274 int ret;
275 x509_crt crt;
276 x509_crt ca;
277 int flags = 0;
278 verify_print_context vrfy_ctx;
279
280 x509_crt_init( &crt );
281 x509_crt_init( &ca );
282 verify_print_init( &vrfy_ctx );
283
284 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
285 TEST_ASSERT( x509_crt_parse_file( &ca, ca_file ) == 0 );
286
287 ret = x509_crt_verify( &crt, &ca, NULL, NULL, &flags,
288 verify_print, &vrfy_ctx );
289
290 TEST_ASSERT( ret == exp_ret );
291 TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
292
293exit:
294 x509_crt_free( &crt );
295 x509_crt_free( &ca );
296}
297/* END_CASE */
298
Manuel Pégourié-Gonnard8f63e952015-09-01 18:44:47 +0200299/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200300void x509_dn_gets( char *crt_file, char *entity, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000301{
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200302 x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000303 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200304 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000305
Paul Bakker369d2eb2013-09-18 11:58:25 +0200306 x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000307 memset( buf, 0, 2000 );
308
Paul Bakkerddf26b42013-09-18 13:46:23 +0200309 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200310 if( strcmp( entity, "subject" ) == 0 )
Paul Bakker86d0c192013-09-18 11:11:02 +0200311 res = x509_dn_gets( buf, 2000, &crt.subject );
Paul Bakker33b43f12013-08-20 11:48:36 +0200312 else if( strcmp( entity, "issuer" ) == 0 )
Paul Bakker86d0c192013-09-18 11:11:02 +0200313 res = x509_dn_gets( buf, 2000, &crt.issuer );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200314 else
315 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000316
317 TEST_ASSERT( res != -1 );
318 TEST_ASSERT( res != -2 );
319
Paul Bakker33b43f12013-08-20 11:48:36 +0200320 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200321
322exit:
323 x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000324}
Paul Bakker33b43f12013-08-20 11:48:36 +0200325/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000326
Manuel Pégourié-Gonnard8f63e952015-09-01 18:44:47 +0200327/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200328void x509_time_expired( char *crt_file, char *entity, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000329{
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200330 x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000331
Paul Bakker369d2eb2013-09-18 11:58:25 +0200332 x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000333
Paul Bakkerddf26b42013-09-18 13:46:23 +0200334 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200335
Paul Bakker33b43f12013-08-20 11:48:36 +0200336 if( strcmp( entity, "valid_from" ) == 0 )
Paul Bakker86d0c192013-09-18 11:11:02 +0200337 TEST_ASSERT( x509_time_expired( &crt.valid_from ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200338 else if( strcmp( entity, "valid_to" ) == 0 )
Paul Bakker86d0c192013-09-18 11:11:02 +0200339 TEST_ASSERT( x509_time_expired( &crt.valid_to ) == result );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200340 else
341 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000342
Paul Bakkerbd51b262014-07-10 15:26:12 +0200343exit:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344 x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000345}
Paul Bakker33b43f12013-08-20 11:48:36 +0200346/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000347
Manuel Pégourié-Gonnard8f63e952015-09-01 18:44:47 +0200348/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100349void x509_time_future( char *crt_file, char *entity, int result )
350{
351 x509_crt crt;
352
353 x509_crt_init( &crt );
354
355 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
356
357 if( strcmp( entity, "valid_from" ) == 0 )
358 TEST_ASSERT( x509_time_future( &crt.valid_from ) == result );
359 else if( strcmp( entity, "valid_to" ) == 0 )
360 TEST_ASSERT( x509_time_future( &crt.valid_to ) == result );
361 else
362 TEST_ASSERT( "Unknown entity" == 0 );
363
Paul Bakkerbd51b262014-07-10 15:26:12 +0200364exit:
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100365 x509_crt_free( &crt );
366}
367/* END_CASE */
368
Paul Bakker5a5fa922014-09-26 14:53:04 +0200369/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C:POLARSSL_FS_IO */
370void x509parse_crt_file( char *crt_file, int result )
371{
372 x509_crt crt;
373
374 x509_crt_init( &crt );
375
376 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == result );
377
378exit:
379 x509_crt_free( &crt );
380}
381/* END_CASE */
382
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200384void x509parse_crt( char *crt_data, char *result_str, int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000385{
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200386 x509_crt crt;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000387 unsigned char buf[2000];
388 unsigned char output[2000];
389 int data_len, res;
390
Paul Bakker369d2eb2013-09-18 11:58:25 +0200391 x509_crt_init( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000392 memset( buf, 0, 2000 );
393 memset( output, 0, 2000 );
394
Paul Bakker33b43f12013-08-20 11:48:36 +0200395 data_len = unhexify( buf, crt_data );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000396
Paul Bakkerddf26b42013-09-18 13:46:23 +0200397 TEST_ASSERT( x509_crt_parse( &crt, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200398 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000399 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200400 res = x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200401
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000402 TEST_ASSERT( res != -1 );
403 TEST_ASSERT( res != -2 );
404
Paul Bakker33b43f12013-08-20 11:48:36 +0200405 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000406 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000407
Paul Bakkerbd51b262014-07-10 15:26:12 +0200408exit:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200409 x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000410}
Paul Bakker33b43f12013-08-20 11:48:36 +0200411/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000412
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413/* BEGIN_CASE depends_on:POLARSSL_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200414void x509parse_crl( char *crl_data, char *result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000415{
416 x509_crl crl;
417 unsigned char buf[2000];
418 unsigned char output[2000];
419 int data_len, res;
420
Paul Bakker369d2eb2013-09-18 11:58:25 +0200421 x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000422 memset( buf, 0, 2000 );
423 memset( output, 0, 2000 );
424
Paul Bakker33b43f12013-08-20 11:48:36 +0200425 data_len = unhexify( buf, crl_data );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000426
Paul Bakkerddf26b42013-09-18 13:46:23 +0200427 TEST_ASSERT( x509_crl_parse( &crl, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200428 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000429 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200430 res = x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200431
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000432 TEST_ASSERT( res != -1 );
433 TEST_ASSERT( res != -2 );
434
Paul Bakker33b43f12013-08-20 11:48:36 +0200435 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000436 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000437
Paul Bakkerbd51b262014-07-10 15:26:12 +0200438exit:
Paul Bakkerb08e6842012-02-11 18:43:20 +0000439 x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000440}
Paul Bakker33b43f12013-08-20 11:48:36 +0200441/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000442
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200443/* BEGIN_CASE depends_on:POLARSSL_X509_CSR_PARSE_C */
444void x509_csr_parse( char *csr_der_hex, char *ref_out, int ref_ret )
445{
446 x509_csr csr;
Paul Bakkerbd51b262014-07-10 15:26:12 +0200447 unsigned char *csr_der = NULL;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200448 char my_out[1000];
449 size_t csr_der_len;
450 int my_ret;
451
452 x509_csr_init( &csr );
453 memset( my_out, 0, sizeof( my_out ) );
454 csr_der = unhexify_alloc( csr_der_hex, &csr_der_len );
455
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200456 my_ret = x509_csr_parse_der( &csr, csr_der, csr_der_len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200457 TEST_ASSERT( my_ret == ref_ret );
458
459 if( ref_ret == 0 )
460 {
461 size_t my_out_len = x509_csr_info( my_out, sizeof( my_out ), "", &csr );
462 TEST_ASSERT( my_out_len == strlen( ref_out ) );
463 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
464 }
465
Paul Bakkerbd51b262014-07-10 15:26:12 +0200466exit:
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200467 x509_csr_free( &csr );
468 polarssl_free( csr_der );
469}
470/* END_CASE */
471
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100472/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
473void x509_crt_parse_path( char *crt_path, int ret, int nb_crt )
474{
475 x509_crt chain, *cur;
476 int i;
477
478 x509_crt_init( &chain );
479
480 TEST_ASSERT( x509_crt_parse_path( &chain, crt_path ) == ret );
481
482 /* Check how many certs we got */
483 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
484 if( cur->raw.p != NULL )
485 i++;
486
487 TEST_ASSERT( i == nb_crt );
488
Paul Bakkerbd51b262014-07-10 15:26:12 +0200489exit:
Paul Bakkera2ffccd2013-12-02 21:56:37 +0100490 x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100491}
492/* END_CASE */
493
Janos Follath189c7432015-10-11 10:25:22 +0200494/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
Janos Follath3d98a7e2015-10-11 16:17:27 +0200495void x509_crt_verify_chain( char *chain_paths, char *trusted_ca, int flags_result )
Janos Follath189c7432015-10-11 10:25:22 +0200496{
497 char* act;
498 int flags;
Janos Follath3d98a7e2015-10-11 16:17:27 +0200499 int result, res;
Janos Follath189c7432015-10-11 10:25:22 +0200500 x509_crt trusted, chain;
501
Janos Follath3d98a7e2015-10-11 16:17:27 +0200502 result = flags_result ? POLARSSL_ERR_X509_CERT_VERIFY_FAILED : 0;
503
Janos Follath189c7432015-10-11 10:25:22 +0200504 x509_crt_init( &chain );
505 x509_crt_init( &trusted );
506
Manuel Pégourié-Gonnard28e1ac52015-11-02 06:34:46 +0900507 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Janos Follath189c7432015-10-11 10:25:22 +0200508 TEST_ASSERT( x509_crt_parse_file( &chain, act ) == 0 );
509 TEST_ASSERT( x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
510
511 res = x509_crt_verify( &chain, &trusted, NULL, NULL, &flags, NULL, NULL );
512
Janos Follath3d98a7e2015-10-11 16:17:27 +0200513 TEST_ASSERT( res == result );
514 TEST_ASSERT( flags == flags_result );
Janos Follath189c7432015-10-11 10:25:22 +0200515
516exit:
517 x509_crt_free( &trusted );
518 x509_crt_free( &chain );
519}
520/* END_CASE */
521
Manuel Pégourié-Gonnard0f7b6192014-06-24 11:37:54 +0200522/* BEGIN_CASE depends_on:POLARSSL_X509_USE_C */
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100523void x509_oid_desc( char *oid_str, char *ref_desc )
524{
525 x509_buf oid;
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000526 const char *desc = NULL;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100527 unsigned char buf[20];
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000528 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100529
530 memset( buf, 0, sizeof buf );
531
532 oid.tag = ASN1_OID;
533 oid.len = unhexify( buf, oid_str );
534 oid.p = buf;
535
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000536 ret = oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100537
538 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000539 {
540 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100541 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000542 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100543 else
544 {
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000545 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100546 TEST_ASSERT( desc != NULL );
547 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
548 }
549}
550/* END_CASE */
551
Manuel Pégourié-Gonnard0f7b6192014-06-24 11:37:54 +0200552/* BEGIN_CASE depends_on:POLARSSL_X509_USE_C */
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100553void x509_oid_numstr( char *oid_str, char *numstr, int blen, int ret )
554{
555 x509_buf oid;
556 unsigned char oid_buf[20];
557 char num_buf[100];
558
559 memset( oid_buf, 0x00, sizeof oid_buf );
560 memset( num_buf, 0x2a, sizeof num_buf );
561
562 oid.tag = ASN1_OID;
563 oid.len = unhexify( oid_buf, oid_str );
564 oid.p = oid_buf;
565
566 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
567
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000568 TEST_ASSERT( oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100569
570 if( ret >= 0 )
571 {
572 TEST_ASSERT( num_buf[ret] == 0 );
573 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
574 }
575}
576/* END_CASE */
577
Paul Bakker5b11d022014-07-10 13:54:38 +0200578/* 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 +0200579void x509_check_key_usage( char *crt_file, int usage, int ret )
580{
581 x509_crt crt;
582
583 x509_crt_init( &crt );
584
585 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
586
587 TEST_ASSERT( x509_crt_check_key_usage( &crt, usage ) == ret );
588
Paul Bakkerbd51b262014-07-10 15:26:12 +0200589exit:
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200590 x509_crt_free( &crt );
591}
592/* END_CASE */
593
Paul Bakker5b11d022014-07-10 13:54:38 +0200594/* 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 +0200595void x509_check_extended_key_usage( char *crt_file, char *usage_hex, int ret )
596{
597 x509_crt crt;
598 char oid[50];
599 size_t len;
600
601 x509_crt_init( &crt );
602
603 len = unhexify( (unsigned char *) oid, usage_hex );
604
605 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
606
607 TEST_ASSERT( x509_crt_check_extended_key_usage( &crt, oid, len ) == ret );
608
Paul Bakkerbd51b262014-07-10 15:26:12 +0200609exit:
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200610 x509_crt_free( &crt );
611}
612/* END_CASE */
613
Andres AG0da3e442016-09-23 13:16:02 +0100614/* BEGIN_CASE depends_on:POLARSSL_X509_USE_C */
615void x509_get_time( int tag, char *time_str, int ret,
616 int year, int mon, int day,
617 int hour, int min, int sec )
618{
619 x509_time time;
620 unsigned char buf[17];
621 unsigned char* start = buf;
622 unsigned char* end = buf;
623
624 memset( &time, 0x00, sizeof( time ) );
625 *end = (unsigned char)tag; end++;
626 if( tag == ASN1_UTC_TIME )
627 *end = 13;
628 else
629 *end = 15;
630 end++;
631 memcpy( end, time_str, (size_t)*(end - 1) );
632 end += *(end - 1);
633
634 TEST_ASSERT( x509_get_time( &start, end, &time ) == ret );
635 if( ret == 0 )
636 {
637 TEST_ASSERT( year == time.year );
638 TEST_ASSERT( mon == time.mon );
639 TEST_ASSERT( day == time.day );
640 TEST_ASSERT( hour == time.hour );
641 TEST_ASSERT( min == time.min );
642 TEST_ASSERT( sec == time.sec );
643 }
644}
645/* END_CASE */
646
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200647/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C:POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200648void x509_parse_rsassa_pss_params( char *hex_params, int params_tag,
649 int ref_msg_md, int ref_mgf_md,
650 int ref_salt_len, int ref_ret )
651{
652 int my_ret;
653 x509_buf params;
654 md_type_t my_msg_md, my_mgf_md;
655 int my_salt_len;
656
657 params.p = unhexify_alloc( hex_params, &params.len );
658 params.tag = params_tag;
659
660 my_ret = x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
661 &my_salt_len );
662
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200663 TEST_ASSERT( my_ret == ref_ret );
664
665 if( ref_ret == 0 )
666 {
667 TEST_ASSERT( my_msg_md == (md_type_t) ref_msg_md );
668 TEST_ASSERT( my_mgf_md == (md_type_t) ref_mgf_md );
669 TEST_ASSERT( my_salt_len == ref_salt_len );
670 }
671
Paul Bakkerbd51b262014-07-10 15:26:12 +0200672exit:
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200673 polarssl_free( params.p );
674}
675/* END_CASE */
676
Manuel Pégourié-Gonnard20140162013-10-10 12:48:03 +0200677/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C:POLARSSL_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200678void x509_selftest()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000679{
680 TEST_ASSERT( x509_self_test( 0 ) == 0 );
681}
Paul Bakker33b43f12013-08-20 11:48:36 +0200682/* END_CASE */