blob: eee82412bf4cfc438b504b9021b4b56caea48fed [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
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100170/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CSR_PARSE_C */
171void x509_csr_info( char *csr_file, char *result_str )
172{
173 x509_csr csr;
174 char buf[2000];
175 int res;
176
177 x509_csr_init( &csr );
178 memset( buf, 0, 2000 );
179
180 TEST_ASSERT( x509_csr_parse_file( &csr, csr_file ) == 0 );
181 res = x509_csr_info( buf, 2000, "", &csr );
182
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100183 TEST_ASSERT( res != -1 );
184 TEST_ASSERT( res != -2 );
185
186 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200187
188exit:
189 x509_csr_free( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100190}
191/* END_CASE */
192
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200193/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C */
194void x509_verify_info( int flags, char *prefix, char *result_str )
195{
196 char buf[2000];
197 int res;
198
199 memset( buf, 0, sizeof( buf ) );
200
201 res = x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
202
203 TEST_ASSERT( res >= 0 );
204
205 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
206}
207/* END_CASE */
208
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200209/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C:POLARSSL_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200210void x509_verify( char *crt_file, char *ca_file, char *crl_file,
211 char *cn_name_str, int result, int flags_result,
212 char *verify_callback )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000213{
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200214 x509_crt crt;
215 x509_crt ca;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000216 x509_crl crl;
217 int flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000218 int res;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200219 int (*f_vrfy)(void *, x509_crt *, int, int *) = NULL;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200220 char * cn_name = NULL;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000221
Paul Bakker369d2eb2013-09-18 11:58:25 +0200222 x509_crt_init( &crt );
223 x509_crt_init( &ca );
224 x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000225
Paul Bakker33b43f12013-08-20 11:48:36 +0200226 if( strcmp( cn_name_str, "NULL" ) != 0 )
227 cn_name = cn_name_str;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200228
Paul Bakker33b43f12013-08-20 11:48:36 +0200229 if( strcmp( verify_callback, "NULL" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200230 f_vrfy = NULL;
Paul Bakker33b43f12013-08-20 11:48:36 +0200231 else if( strcmp( verify_callback, "verify_none" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200232 f_vrfy = verify_none;
Paul Bakker33b43f12013-08-20 11:48:36 +0200233 else if( strcmp( verify_callback, "verify_all" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200234 f_vrfy = verify_all;
235 else
236 TEST_ASSERT( "No known verify callback selected" == 0 );
237
Paul Bakkerddf26b42013-09-18 13:46:23 +0200238 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
239 TEST_ASSERT( x509_crt_parse_file( &ca, ca_file ) == 0 );
240 TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000241
Paul Bakkerddf26b42013-09-18 13:46:23 +0200242 res = x509_crt_verify( &crt, &ca, &crl, cn_name, &flags, f_vrfy, NULL );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000243
Paul Bakkerbd51b262014-07-10 15:26:12 +0200244 TEST_ASSERT( res == ( result ) );
245 TEST_ASSERT( flags == ( flags_result ) );
246
247exit:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200248 x509_crt_free( &crt );
249 x509_crt_free( &ca );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000250 x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000251}
Paul Bakker33b43f12013-08-20 11:48:36 +0200252/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000253
Manuel Pégourié-Gonnard15f10882015-09-01 11:59:24 +0200254/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
255void x509_verify_callback( char *crt_file, char *ca_file,
256 int exp_ret, char *exp_vrfy_out )
257{
258 int ret;
259 x509_crt crt;
260 x509_crt ca;
261 int flags = 0;
262 verify_print_context vrfy_ctx;
263
264 x509_crt_init( &crt );
265 x509_crt_init( &ca );
266 verify_print_init( &vrfy_ctx );
267
268 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
269 TEST_ASSERT( x509_crt_parse_file( &ca, ca_file ) == 0 );
270
271 ret = x509_crt_verify( &crt, &ca, NULL, NULL, &flags,
272 verify_print, &vrfy_ctx );
273
274 TEST_ASSERT( ret == exp_ret );
275 TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
276
277exit:
278 x509_crt_free( &crt );
279 x509_crt_free( &ca );
280}
281/* END_CASE */
282
Manuel Pégourié-Gonnard8f63e952015-09-01 18:44:47 +0200283/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200284void x509_dn_gets( char *crt_file, char *entity, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000285{
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200286 x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000287 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200288 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000289
Paul Bakker369d2eb2013-09-18 11:58:25 +0200290 x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000291 memset( buf, 0, 2000 );
292
Paul Bakkerddf26b42013-09-18 13:46:23 +0200293 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200294 if( strcmp( entity, "subject" ) == 0 )
Paul Bakker86d0c192013-09-18 11:11:02 +0200295 res = x509_dn_gets( buf, 2000, &crt.subject );
Paul Bakker33b43f12013-08-20 11:48:36 +0200296 else if( strcmp( entity, "issuer" ) == 0 )
Paul Bakker86d0c192013-09-18 11:11:02 +0200297 res = x509_dn_gets( buf, 2000, &crt.issuer );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200298 else
299 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000300
301 TEST_ASSERT( res != -1 );
302 TEST_ASSERT( res != -2 );
303
Paul Bakker33b43f12013-08-20 11:48:36 +0200304 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200305
306exit:
307 x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000308}
Paul Bakker33b43f12013-08-20 11:48:36 +0200309/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000310
Manuel Pégourié-Gonnard8f63e952015-09-01 18:44:47 +0200311/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200312void x509_time_expired( char *crt_file, char *entity, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000313{
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200314 x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000315
Paul Bakker369d2eb2013-09-18 11:58:25 +0200316 x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000317
Paul Bakkerddf26b42013-09-18 13:46:23 +0200318 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200319
Paul Bakker33b43f12013-08-20 11:48:36 +0200320 if( strcmp( entity, "valid_from" ) == 0 )
Paul Bakker86d0c192013-09-18 11:11:02 +0200321 TEST_ASSERT( x509_time_expired( &crt.valid_from ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200322 else if( strcmp( entity, "valid_to" ) == 0 )
Paul Bakker86d0c192013-09-18 11:11:02 +0200323 TEST_ASSERT( x509_time_expired( &crt.valid_to ) == result );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200324 else
325 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000326
Paul Bakkerbd51b262014-07-10 15:26:12 +0200327exit:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200328 x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000329}
Paul Bakker33b43f12013-08-20 11:48:36 +0200330/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000331
Manuel Pégourié-Gonnard8f63e952015-09-01 18:44:47 +0200332/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100333void x509_time_future( char *crt_file, char *entity, int result )
334{
335 x509_crt crt;
336
337 x509_crt_init( &crt );
338
339 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
340
341 if( strcmp( entity, "valid_from" ) == 0 )
342 TEST_ASSERT( x509_time_future( &crt.valid_from ) == result );
343 else if( strcmp( entity, "valid_to" ) == 0 )
344 TEST_ASSERT( x509_time_future( &crt.valid_to ) == result );
345 else
346 TEST_ASSERT( "Unknown entity" == 0 );
347
Paul Bakkerbd51b262014-07-10 15:26:12 +0200348exit:
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100349 x509_crt_free( &crt );
350}
351/* END_CASE */
352
Paul Bakker5a5fa922014-09-26 14:53:04 +0200353/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C:POLARSSL_FS_IO */
354void x509parse_crt_file( char *crt_file, int result )
355{
356 x509_crt crt;
357
358 x509_crt_init( &crt );
359
360 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == result );
361
362exit:
363 x509_crt_free( &crt );
364}
365/* END_CASE */
366
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200368void x509parse_crt( char *crt_data, char *result_str, int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000369{
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200370 x509_crt crt;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000371 unsigned char buf[2000];
372 unsigned char output[2000];
373 int data_len, res;
374
Paul Bakker369d2eb2013-09-18 11:58:25 +0200375 x509_crt_init( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000376 memset( buf, 0, 2000 );
377 memset( output, 0, 2000 );
378
Paul Bakker33b43f12013-08-20 11:48:36 +0200379 data_len = unhexify( buf, crt_data );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000380
Paul Bakkerddf26b42013-09-18 13:46:23 +0200381 TEST_ASSERT( x509_crt_parse( &crt, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200382 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000383 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200384 res = x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200385
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000386 TEST_ASSERT( res != -1 );
387 TEST_ASSERT( res != -2 );
388
Paul Bakker33b43f12013-08-20 11:48:36 +0200389 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000390 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000391
Paul Bakkerbd51b262014-07-10 15:26:12 +0200392exit:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200393 x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000394}
Paul Bakker33b43f12013-08-20 11:48:36 +0200395/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000396
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397/* BEGIN_CASE depends_on:POLARSSL_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200398void x509parse_crl( char *crl_data, char *result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000399{
400 x509_crl crl;
401 unsigned char buf[2000];
402 unsigned char output[2000];
403 int data_len, res;
404
Paul Bakker369d2eb2013-09-18 11:58:25 +0200405 x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000406 memset( buf, 0, 2000 );
407 memset( output, 0, 2000 );
408
Paul Bakker33b43f12013-08-20 11:48:36 +0200409 data_len = unhexify( buf, crl_data );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000410
Paul Bakkerddf26b42013-09-18 13:46:23 +0200411 TEST_ASSERT( x509_crl_parse( &crl, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200412 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000413 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200414 res = x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200415
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000416 TEST_ASSERT( res != -1 );
417 TEST_ASSERT( res != -2 );
418
Paul Bakker33b43f12013-08-20 11:48:36 +0200419 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000420 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000421
Paul Bakkerbd51b262014-07-10 15:26:12 +0200422exit:
Paul Bakkerb08e6842012-02-11 18:43:20 +0000423 x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000424}
Paul Bakker33b43f12013-08-20 11:48:36 +0200425/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000426
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200427/* BEGIN_CASE depends_on:POLARSSL_X509_CSR_PARSE_C */
428void x509_csr_parse( char *csr_der_hex, char *ref_out, int ref_ret )
429{
430 x509_csr csr;
Paul Bakkerbd51b262014-07-10 15:26:12 +0200431 unsigned char *csr_der = NULL;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200432 char my_out[1000];
433 size_t csr_der_len;
434 int my_ret;
435
436 x509_csr_init( &csr );
437 memset( my_out, 0, sizeof( my_out ) );
438 csr_der = unhexify_alloc( csr_der_hex, &csr_der_len );
439
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200440 my_ret = x509_csr_parse_der( &csr, csr_der, csr_der_len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200441 TEST_ASSERT( my_ret == ref_ret );
442
443 if( ref_ret == 0 )
444 {
445 size_t my_out_len = x509_csr_info( my_out, sizeof( my_out ), "", &csr );
446 TEST_ASSERT( my_out_len == strlen( ref_out ) );
447 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
448 }
449
Paul Bakkerbd51b262014-07-10 15:26:12 +0200450exit:
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200451 x509_csr_free( &csr );
452 polarssl_free( csr_der );
453}
454/* END_CASE */
455
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100456/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
457void x509_crt_parse_path( char *crt_path, int ret, int nb_crt )
458{
459 x509_crt chain, *cur;
460 int i;
461
462 x509_crt_init( &chain );
463
464 TEST_ASSERT( x509_crt_parse_path( &chain, crt_path ) == ret );
465
466 /* Check how many certs we got */
467 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
468 if( cur->raw.p != NULL )
469 i++;
470
471 TEST_ASSERT( i == nb_crt );
472
Paul Bakkerbd51b262014-07-10 15:26:12 +0200473exit:
Paul Bakkera2ffccd2013-12-02 21:56:37 +0100474 x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100475}
476/* END_CASE */
477
Janos Follath189c7432015-10-11 10:25:22 +0200478/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
Janos Follath3d98a7e2015-10-11 16:17:27 +0200479void x509_crt_verify_chain( char *chain_paths, char *trusted_ca, int flags_result )
Janos Follath189c7432015-10-11 10:25:22 +0200480{
481 char* act;
482 int flags;
Janos Follath3d98a7e2015-10-11 16:17:27 +0200483 int result, res;
Janos Follath189c7432015-10-11 10:25:22 +0200484 x509_crt trusted, chain;
485
Janos Follath3d98a7e2015-10-11 16:17:27 +0200486 result = flags_result ? POLARSSL_ERR_X509_CERT_VERIFY_FAILED : 0;
487
Janos Follath189c7432015-10-11 10:25:22 +0200488 x509_crt_init( &chain );
489 x509_crt_init( &trusted );
490
Manuel Pégourié-Gonnard28e1ac52015-11-02 06:34:46 +0900491 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Janos Follath189c7432015-10-11 10:25:22 +0200492 TEST_ASSERT( x509_crt_parse_file( &chain, act ) == 0 );
493 TEST_ASSERT( x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
494
495 res = x509_crt_verify( &chain, &trusted, NULL, NULL, &flags, NULL, NULL );
496
Janos Follath3d98a7e2015-10-11 16:17:27 +0200497 TEST_ASSERT( res == result );
498 TEST_ASSERT( flags == flags_result );
Janos Follath189c7432015-10-11 10:25:22 +0200499
500exit:
501 x509_crt_free( &trusted );
502 x509_crt_free( &chain );
503}
504/* END_CASE */
505
Manuel Pégourié-Gonnard0f7b6192014-06-24 11:37:54 +0200506/* BEGIN_CASE depends_on:POLARSSL_X509_USE_C */
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100507void x509_oid_desc( char *oid_str, char *ref_desc )
508{
509 x509_buf oid;
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000510 const char *desc = NULL;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100511 unsigned char buf[20];
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000512 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100513
514 memset( buf, 0, sizeof buf );
515
516 oid.tag = ASN1_OID;
517 oid.len = unhexify( buf, oid_str );
518 oid.p = buf;
519
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000520 ret = oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100521
522 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000523 {
524 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100525 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000526 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100527 else
528 {
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000529 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100530 TEST_ASSERT( desc != NULL );
531 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
532 }
533}
534/* END_CASE */
535
Manuel Pégourié-Gonnard0f7b6192014-06-24 11:37:54 +0200536/* BEGIN_CASE depends_on:POLARSSL_X509_USE_C */
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100537void x509_oid_numstr( char *oid_str, char *numstr, int blen, int ret )
538{
539 x509_buf oid;
540 unsigned char oid_buf[20];
541 char num_buf[100];
542
543 memset( oid_buf, 0x00, sizeof oid_buf );
544 memset( num_buf, 0x2a, sizeof num_buf );
545
546 oid.tag = ASN1_OID;
547 oid.len = unhexify( oid_buf, oid_str );
548 oid.p = oid_buf;
549
550 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
551
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000552 TEST_ASSERT( oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100553
554 if( ret >= 0 )
555 {
556 TEST_ASSERT( num_buf[ret] == 0 );
557 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
558 }
559}
560/* END_CASE */
561
Paul Bakker5b11d022014-07-10 13:54:38 +0200562/* 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 +0200563void x509_check_key_usage( char *crt_file, int usage, int ret )
564{
565 x509_crt crt;
566
567 x509_crt_init( &crt );
568
569 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
570
571 TEST_ASSERT( x509_crt_check_key_usage( &crt, usage ) == ret );
572
Paul Bakkerbd51b262014-07-10 15:26:12 +0200573exit:
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200574 x509_crt_free( &crt );
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_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200579void x509_check_extended_key_usage( char *crt_file, char *usage_hex, int ret )
580{
581 x509_crt crt;
582 char oid[50];
583 size_t len;
584
585 x509_crt_init( &crt );
586
587 len = unhexify( (unsigned char *) oid, usage_hex );
588
589 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
590
591 TEST_ASSERT( x509_crt_check_extended_key_usage( &crt, oid, len ) == ret );
592
Paul Bakkerbd51b262014-07-10 15:26:12 +0200593exit:
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200594 x509_crt_free( &crt );
595}
596/* END_CASE */
597
Andres AG0da3e442016-09-23 13:16:02 +0100598/* BEGIN_CASE depends_on:POLARSSL_X509_USE_C */
599void x509_get_time( int tag, char *time_str, int ret,
600 int year, int mon, int day,
601 int hour, int min, int sec )
602{
603 x509_time time;
604 unsigned char buf[17];
605 unsigned char* start = buf;
606 unsigned char* end = buf;
607
608 memset( &time, 0x00, sizeof( time ) );
609 *end = (unsigned char)tag; end++;
610 if( tag == ASN1_UTC_TIME )
611 *end = 13;
612 else
613 *end = 15;
614 end++;
615 memcpy( end, time_str, (size_t)*(end - 1) );
616 end += *(end - 1);
617
618 TEST_ASSERT( x509_get_time( &start, end, &time ) == ret );
619 if( ret == 0 )
620 {
621 TEST_ASSERT( year == time.year );
622 TEST_ASSERT( mon == time.mon );
623 TEST_ASSERT( day == time.day );
624 TEST_ASSERT( hour == time.hour );
625 TEST_ASSERT( min == time.min );
626 TEST_ASSERT( sec == time.sec );
627 }
628}
629/* END_CASE */
630
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200631/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C:POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200632void x509_parse_rsassa_pss_params( char *hex_params, int params_tag,
633 int ref_msg_md, int ref_mgf_md,
634 int ref_salt_len, int ref_ret )
635{
636 int my_ret;
637 x509_buf params;
638 md_type_t my_msg_md, my_mgf_md;
639 int my_salt_len;
640
641 params.p = unhexify_alloc( hex_params, &params.len );
642 params.tag = params_tag;
643
644 my_ret = x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
645 &my_salt_len );
646
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200647 TEST_ASSERT( my_ret == ref_ret );
648
649 if( ref_ret == 0 )
650 {
651 TEST_ASSERT( my_msg_md == (md_type_t) ref_msg_md );
652 TEST_ASSERT( my_mgf_md == (md_type_t) ref_mgf_md );
653 TEST_ASSERT( my_salt_len == ref_salt_len );
654 }
655
Paul Bakkerbd51b262014-07-10 15:26:12 +0200656exit:
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200657 polarssl_free( params.p );
658}
659/* END_CASE */
660
Manuel Pégourié-Gonnard20140162013-10-10 12:48:03 +0200661/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C:POLARSSL_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200662void x509_selftest()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000663{
664 TEST_ASSERT( x509_self_test( 0 ) == 0 );
665}
Paul Bakker33b43f12013-08-20 11:48:36 +0200666/* END_CASE */