blob: 5d82f44d6d30ace66bc3816333a96bda7f844a57 [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 */
Paul Bakker33b43f12013-08-20 11:48:36 +020086/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +000087
Paul Bakker33b43f12013-08-20 11:48:36 +020088/* BEGIN_DEPENDENCIES
Paul Bakker7c6b2c32013-09-16 13:49:26 +020089 * depends_on:POLARSSL_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +020090 * END_DEPENDENCIES
91 */
Paul Bakker5690efc2011-05-26 13:16:06 +000092
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +020094void x509_cert_info( char *crt_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +000095{
Paul Bakkerc559c7a2013-09-18 14:13:26 +020096 x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +000097 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +000098 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +000099
Paul Bakker369d2eb2013-09-18 11:58:25 +0200100 x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000101 memset( buf, 0, 2000 );
102
Paul Bakkerddf26b42013-09-18 13:46:23 +0200103 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
104 res = x509_crt_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000105
106 TEST_ASSERT( res != -1 );
107 TEST_ASSERT( res != -2 );
108
Paul Bakker33b43f12013-08-20 11:48:36 +0200109 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200110
111exit:
112 x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000113}
Paul Bakker33b43f12013-08-20 11:48:36 +0200114/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000115
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200116/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200117void x509_crl_info( char *crl_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000118{
119 x509_crl crl;
120 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000121 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000122
Paul Bakker369d2eb2013-09-18 11:58:25 +0200123 x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000124 memset( buf, 0, 2000 );
125
Paul Bakkerddf26b42013-09-18 13:46:23 +0200126 TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == 0 );
127 res = x509_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000128
129 TEST_ASSERT( res != -1 );
130 TEST_ASSERT( res != -2 );
131
Paul Bakker33b43f12013-08-20 11:48:36 +0200132 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200133
134exit:
135 x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000136}
Paul Bakker33b43f12013-08-20 11:48:36 +0200137/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000138
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100139/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CSR_PARSE_C */
140void x509_csr_info( char *csr_file, char *result_str )
141{
142 x509_csr csr;
143 char buf[2000];
144 int res;
145
146 x509_csr_init( &csr );
147 memset( buf, 0, 2000 );
148
149 TEST_ASSERT( x509_csr_parse_file( &csr, csr_file ) == 0 );
150 res = x509_csr_info( buf, 2000, "", &csr );
151
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100152 TEST_ASSERT( res != -1 );
153 TEST_ASSERT( res != -2 );
154
155 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200156
157exit:
158 x509_csr_free( &csr );
Manuel Pégourié-Gonnard2a8d7fd2014-01-24 17:34:26 +0100159}
160/* END_CASE */
161
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200162/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C */
163void x509_verify_info( int flags, char *prefix, char *result_str )
164{
165 char buf[2000];
166 int res;
167
168 memset( buf, 0, sizeof( buf ) );
169
170 res = x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
171
172 TEST_ASSERT( res >= 0 );
173
174 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
175}
176/* END_CASE */
177
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200178/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C:POLARSSL_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200179void x509_verify( char *crt_file, char *ca_file, char *crl_file,
180 char *cn_name_str, int result, int flags_result,
181 char *verify_callback )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000182{
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200183 x509_crt crt;
184 x509_crt ca;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000185 x509_crl crl;
186 int flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000187 int res;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200188 int (*f_vrfy)(void *, x509_crt *, int, int *) = NULL;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200189 char * cn_name = NULL;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000190
Paul Bakker369d2eb2013-09-18 11:58:25 +0200191 x509_crt_init( &crt );
192 x509_crt_init( &ca );
193 x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000194
Paul Bakker33b43f12013-08-20 11:48:36 +0200195 if( strcmp( cn_name_str, "NULL" ) != 0 )
196 cn_name = cn_name_str;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200197
Paul Bakker33b43f12013-08-20 11:48:36 +0200198 if( strcmp( verify_callback, "NULL" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200199 f_vrfy = NULL;
Paul Bakker33b43f12013-08-20 11:48:36 +0200200 else if( strcmp( verify_callback, "verify_none" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200201 f_vrfy = verify_none;
Paul Bakker33b43f12013-08-20 11:48:36 +0200202 else if( strcmp( verify_callback, "verify_all" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200203 f_vrfy = verify_all;
204 else
205 TEST_ASSERT( "No known verify callback selected" == 0 );
206
Paul Bakkerddf26b42013-09-18 13:46:23 +0200207 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
208 TEST_ASSERT( x509_crt_parse_file( &ca, ca_file ) == 0 );
209 TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000210
Paul Bakkerddf26b42013-09-18 13:46:23 +0200211 res = x509_crt_verify( &crt, &ca, &crl, cn_name, &flags, f_vrfy, NULL );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000212
Paul Bakkerbd51b262014-07-10 15:26:12 +0200213 TEST_ASSERT( res == ( result ) );
214 TEST_ASSERT( flags == ( flags_result ) );
215
216exit:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200217 x509_crt_free( &crt );
218 x509_crt_free( &ca );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000219 x509_crl_free( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000220}
Paul Bakker33b43f12013-08-20 11:48:36 +0200221/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000222
Manuel Pégourié-Gonnard15f10882015-09-01 11:59:24 +0200223/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
224void x509_verify_callback( char *crt_file, char *ca_file,
225 int exp_ret, char *exp_vrfy_out )
226{
227 int ret;
228 x509_crt crt;
229 x509_crt ca;
230 int flags = 0;
231 verify_print_context vrfy_ctx;
232
233 x509_crt_init( &crt );
234 x509_crt_init( &ca );
235 verify_print_init( &vrfy_ctx );
236
237 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
238 TEST_ASSERT( x509_crt_parse_file( &ca, ca_file ) == 0 );
239
240 ret = x509_crt_verify( &crt, &ca, NULL, NULL, &flags,
241 verify_print, &vrfy_ctx );
242
243 TEST_ASSERT( ret == exp_ret );
244 TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
245
246exit:
247 x509_crt_free( &crt );
248 x509_crt_free( &ca );
249}
250/* END_CASE */
251
Manuel Pégourié-Gonnard8f63e952015-09-01 18:44:47 +0200252/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200253void x509_dn_gets( char *crt_file, char *entity, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000254{
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200255 x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000256 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200257 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000258
Paul Bakker369d2eb2013-09-18 11:58:25 +0200259 x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000260 memset( buf, 0, 2000 );
261
Paul Bakkerddf26b42013-09-18 13:46:23 +0200262 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200263 if( strcmp( entity, "subject" ) == 0 )
Paul Bakker86d0c192013-09-18 11:11:02 +0200264 res = x509_dn_gets( buf, 2000, &crt.subject );
Paul Bakker33b43f12013-08-20 11:48:36 +0200265 else if( strcmp( entity, "issuer" ) == 0 )
Paul Bakker86d0c192013-09-18 11:11:02 +0200266 res = x509_dn_gets( buf, 2000, &crt.issuer );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200267 else
268 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000269
270 TEST_ASSERT( res != -1 );
271 TEST_ASSERT( res != -2 );
272
Paul Bakker33b43f12013-08-20 11:48:36 +0200273 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200274
275exit:
276 x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000277}
Paul Bakker33b43f12013-08-20 11:48:36 +0200278/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000279
Manuel Pégourié-Gonnard8f63e952015-09-01 18:44:47 +0200280/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200281void x509_time_expired( char *crt_file, char *entity, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000282{
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200283 x509_crt crt;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000284
Paul Bakker369d2eb2013-09-18 11:58:25 +0200285 x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000286
Paul Bakkerddf26b42013-09-18 13:46:23 +0200287 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200288
Paul Bakker33b43f12013-08-20 11:48:36 +0200289 if( strcmp( entity, "valid_from" ) == 0 )
Paul Bakker86d0c192013-09-18 11:11:02 +0200290 TEST_ASSERT( x509_time_expired( &crt.valid_from ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200291 else if( strcmp( entity, "valid_to" ) == 0 )
Paul Bakker86d0c192013-09-18 11:11:02 +0200292 TEST_ASSERT( x509_time_expired( &crt.valid_to ) == result );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200293 else
294 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000295
Paul Bakkerbd51b262014-07-10 15:26:12 +0200296exit:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200297 x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000298}
Paul Bakker33b43f12013-08-20 11:48:36 +0200299/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000300
Manuel Pégourié-Gonnard8f63e952015-09-01 18:44:47 +0200301/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100302void x509_time_future( char *crt_file, char *entity, int result )
303{
304 x509_crt crt;
305
306 x509_crt_init( &crt );
307
308 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
309
310 if( strcmp( entity, "valid_from" ) == 0 )
311 TEST_ASSERT( x509_time_future( &crt.valid_from ) == result );
312 else if( strcmp( entity, "valid_to" ) == 0 )
313 TEST_ASSERT( x509_time_future( &crt.valid_to ) == result );
314 else
315 TEST_ASSERT( "Unknown entity" == 0 );
316
Paul Bakkerbd51b262014-07-10 15:26:12 +0200317exit:
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100318 x509_crt_free( &crt );
319}
320/* END_CASE */
321
Paul Bakker5a5fa922014-09-26 14:53:04 +0200322/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C:POLARSSL_FS_IO */
323void x509parse_crt_file( char *crt_file, int result )
324{
325 x509_crt crt;
326
327 x509_crt_init( &crt );
328
329 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == result );
330
331exit:
332 x509_crt_free( &crt );
333}
334/* END_CASE */
335
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200336/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200337void x509parse_crt( char *crt_data, char *result_str, int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000338{
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200339 x509_crt crt;
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000340 unsigned char buf[2000];
341 unsigned char output[2000];
342 int data_len, res;
343
Paul Bakker369d2eb2013-09-18 11:58:25 +0200344 x509_crt_init( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000345 memset( buf, 0, 2000 );
346 memset( output, 0, 2000 );
347
Paul Bakker33b43f12013-08-20 11:48:36 +0200348 data_len = unhexify( buf, crt_data );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000349
Paul Bakkerddf26b42013-09-18 13:46:23 +0200350 TEST_ASSERT( x509_crt_parse( &crt, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200351 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000352 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200353 res = x509_crt_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200354
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000355 TEST_ASSERT( res != -1 );
356 TEST_ASSERT( res != -2 );
357
Paul Bakker33b43f12013-08-20 11:48:36 +0200358 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000359 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000360
Paul Bakkerbd51b262014-07-10 15:26:12 +0200361exit:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362 x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000363}
Paul Bakker33b43f12013-08-20 11:48:36 +0200364/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000365
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366/* BEGIN_CASE depends_on:POLARSSL_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200367void x509parse_crl( char *crl_data, char *result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000368{
369 x509_crl crl;
370 unsigned char buf[2000];
371 unsigned char output[2000];
372 int data_len, res;
373
Paul Bakker369d2eb2013-09-18 11:58:25 +0200374 x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000375 memset( buf, 0, 2000 );
376 memset( output, 0, 2000 );
377
Paul Bakker33b43f12013-08-20 11:48:36 +0200378 data_len = unhexify( buf, crl_data );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000379
Paul Bakkerddf26b42013-09-18 13:46:23 +0200380 TEST_ASSERT( x509_crl_parse( &crl, buf, data_len ) == ( result ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200381 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000382 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200383 res = x509_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200384
Paul Bakker6b0fa4f2009-07-20 20:35:41 +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 Bakker6b0fa4f2009-07-20 20:35:41 +0000389 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000390
Paul Bakkerbd51b262014-07-10 15:26:12 +0200391exit:
Paul Bakkerb08e6842012-02-11 18:43:20 +0000392 x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000393}
Paul Bakker33b43f12013-08-20 11:48:36 +0200394/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000395
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200396/* BEGIN_CASE depends_on:POLARSSL_X509_CSR_PARSE_C */
397void x509_csr_parse( char *csr_der_hex, char *ref_out, int ref_ret )
398{
399 x509_csr csr;
Paul Bakkerbd51b262014-07-10 15:26:12 +0200400 unsigned char *csr_der = NULL;
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200401 char my_out[1000];
402 size_t csr_der_len;
403 int my_ret;
404
405 x509_csr_init( &csr );
406 memset( my_out, 0, sizeof( my_out ) );
407 csr_der = unhexify_alloc( csr_der_hex, &csr_der_len );
408
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200409 my_ret = x509_csr_parse_der( &csr, csr_der, csr_der_len );
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200410 TEST_ASSERT( my_ret == ref_ret );
411
412 if( ref_ret == 0 )
413 {
414 size_t my_out_len = x509_csr_info( my_out, sizeof( my_out ), "", &csr );
415 TEST_ASSERT( my_out_len == strlen( ref_out ) );
416 TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
417 }
418
Paul Bakkerbd51b262014-07-10 15:26:12 +0200419exit:
Manuel Pégourié-Gonnardd77cd5d2014-06-13 11:13:15 +0200420 x509_csr_free( &csr );
421 polarssl_free( csr_der );
422}
423/* END_CASE */
424
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100425/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
426void x509_crt_parse_path( char *crt_path, int ret, int nb_crt )
427{
428 x509_crt chain, *cur;
429 int i;
430
431 x509_crt_init( &chain );
432
433 TEST_ASSERT( x509_crt_parse_path( &chain, crt_path ) == ret );
434
435 /* Check how many certs we got */
436 for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
437 if( cur->raw.p != NULL )
438 i++;
439
440 TEST_ASSERT( i == nb_crt );
441
Paul Bakkerbd51b262014-07-10 15:26:12 +0200442exit:
Paul Bakkera2ffccd2013-12-02 21:56:37 +0100443 x509_crt_free( &chain );
Manuel Pégourié-Gonnardfbae2a12013-11-26 16:43:39 +0100444}
445/* END_CASE */
446
Janos Follath189c7432015-10-11 10:25:22 +0200447/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
448void x509_crt_verify_chain( char *chain_paths, char *trusted_ca, int ret )
449{
450 char* act;
451 int flags;
452 int res;
453 x509_crt trusted, chain;
454
455 x509_crt_init( &chain );
456 x509_crt_init( &trusted );
457
458 while( ( act = strsep( &chain_paths, " " ) ) != NULL )
459 TEST_ASSERT( x509_crt_parse_file( &chain, act ) == 0 );
460 TEST_ASSERT( x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
461
462 res = x509_crt_verify( &chain, &trusted, NULL, NULL, &flags, NULL, NULL );
463
464 TEST_ASSERT( ret == res );
465
466exit:
467 x509_crt_free( &trusted );
468 x509_crt_free( &chain );
469}
470/* END_CASE */
471
Manuel Pégourié-Gonnard0f7b6192014-06-24 11:37:54 +0200472/* BEGIN_CASE depends_on:POLARSSL_X509_USE_C */
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100473void x509_oid_desc( char *oid_str, char *ref_desc )
474{
475 x509_buf oid;
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000476 const char *desc = NULL;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100477 unsigned char buf[20];
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000478 int ret;
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100479
480 memset( buf, 0, sizeof buf );
481
482 oid.tag = ASN1_OID;
483 oid.len = unhexify( buf, oid_str );
484 oid.p = buf;
485
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000486 ret = oid_get_extended_key_usage( &oid, &desc );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100487
488 if( strcmp( ref_desc, "notfound" ) == 0 )
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000489 {
490 TEST_ASSERT( ret != 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100491 TEST_ASSERT( desc == NULL );
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000492 }
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100493 else
494 {
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000495 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100496 TEST_ASSERT( desc != NULL );
497 TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
498 }
499}
500/* END_CASE */
501
Manuel Pégourié-Gonnard0f7b6192014-06-24 11:37:54 +0200502/* BEGIN_CASE depends_on:POLARSSL_X509_USE_C */
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100503void x509_oid_numstr( char *oid_str, char *numstr, int blen, int ret )
504{
505 x509_buf oid;
506 unsigned char oid_buf[20];
507 char num_buf[100];
508
509 memset( oid_buf, 0x00, sizeof oid_buf );
510 memset( num_buf, 0x2a, sizeof num_buf );
511
512 oid.tag = ASN1_OID;
513 oid.len = unhexify( oid_buf, oid_str );
514 oid.p = oid_buf;
515
516 TEST_ASSERT( (size_t) blen <= sizeof num_buf );
517
Manuel Pégourié-Gonnard079333b2015-03-20 18:14:26 +0000518 TEST_ASSERT( oid_get_numeric_string( num_buf, blen, &oid ) == ret );
Manuel Pégourié-Gonnard7afdb882014-03-28 16:06:35 +0100519
520 if( ret >= 0 )
521 {
522 TEST_ASSERT( num_buf[ret] == 0 );
523 TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
524 }
525}
526/* END_CASE */
527
Paul Bakker5b11d022014-07-10 13:54:38 +0200528/* 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 +0200529void x509_check_key_usage( char *crt_file, int usage, int ret )
530{
531 x509_crt crt;
532
533 x509_crt_init( &crt );
534
535 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
536
537 TEST_ASSERT( x509_crt_check_key_usage( &crt, usage ) == ret );
538
Paul Bakkerbd51b262014-07-10 15:26:12 +0200539exit:
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200540 x509_crt_free( &crt );
541}
542/* END_CASE */
543
Paul Bakker5b11d022014-07-10 13:54:38 +0200544/* 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 +0200545void x509_check_extended_key_usage( char *crt_file, char *usage_hex, int ret )
546{
547 x509_crt crt;
548 char oid[50];
549 size_t len;
550
551 x509_crt_init( &crt );
552
553 len = unhexify( (unsigned char *) oid, usage_hex );
554
555 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
556
557 TEST_ASSERT( x509_crt_check_extended_key_usage( &crt, oid, len ) == ret );
558
Paul Bakkerbd51b262014-07-10 15:26:12 +0200559exit:
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200560 x509_crt_free( &crt );
561}
562/* END_CASE */
563
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200564/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C:POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200565void x509_parse_rsassa_pss_params( char *hex_params, int params_tag,
566 int ref_msg_md, int ref_mgf_md,
567 int ref_salt_len, int ref_ret )
568{
569 int my_ret;
570 x509_buf params;
571 md_type_t my_msg_md, my_mgf_md;
572 int my_salt_len;
573
574 params.p = unhexify_alloc( hex_params, &params.len );
575 params.tag = params_tag;
576
577 my_ret = x509_get_rsassa_pss_params( &params, &my_msg_md, &my_mgf_md,
578 &my_salt_len );
579
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200580 TEST_ASSERT( my_ret == ref_ret );
581
582 if( ref_ret == 0 )
583 {
584 TEST_ASSERT( my_msg_md == (md_type_t) ref_msg_md );
585 TEST_ASSERT( my_mgf_md == (md_type_t) ref_mgf_md );
586 TEST_ASSERT( my_salt_len == ref_salt_len );
587 }
588
Paul Bakkerbd51b262014-07-10 15:26:12 +0200589exit:
Manuel Pégourié-Gonnard85403692014-06-06 14:48:38 +0200590 polarssl_free( params.p );
591}
592/* END_CASE */
593
Manuel Pégourié-Gonnard20140162013-10-10 12:48:03 +0200594/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C:POLARSSL_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200595void x509_selftest()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000596{
597 TEST_ASSERT( x509_self_test( 0 ) == 0 );
598}
Paul Bakker33b43f12013-08-20 11:48:36 +0200599/* END_CASE */