blob: e2b89778e0b00983462bcc5df35bc41440b4589a [file] [log] [blame]
Paul Bakker37940d9f2009-07-10 22:38:58 +00001BEGIN_HEADER
2#include <polarssl/x509.h>
Paul Bakker96743fc2011-02-12 14:30:57 +00003#include <polarssl/pem.h>
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004
Paul Bakker915275b2012-09-28 07:10:55 +00005int verify_none( void *data, x509_cert *crt, int certificate_depth, int *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006{
Paul Bakker5a624082011-01-18 16:31:52 +00007 ((void) data);
8 ((void) crt);
9 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000010 *flags |= BADCERT_OTHER;
11
12 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000013}
14
Paul Bakker915275b2012-09-28 07:10:55 +000015int verify_all( void *data, x509_cert *crt, int certificate_depth, int *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000016{
Paul Bakker5a624082011-01-18 16:31:52 +000017 ((void) data);
18 ((void) crt);
19 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000020 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000021
Paul Bakkerb63b0af2011-01-13 17:54:59 +000022 return 0;
23}
24
Manuel Pégourié-Gonnard4dd43ae2015-11-02 06:52:52 +090025/* strsep() not available on Windows */
26char *mystrsep(char **stringp, const char *delim)
27{
28 const char *p;
29 char *ret = *stringp;
30
31 if( *stringp == NULL )
32 return( NULL );
33
34 for( ; ; (*stringp)++ )
35 {
36 if( **stringp == '\0' )
37 {
38 *stringp = NULL;
39 goto done;
40 }
41
42 for( p = delim; *p != '\0'; p++ )
43 if( **stringp == *p )
44 {
45 **stringp = '\0';
46 (*stringp)++;
47 goto done;
48 }
49 }
50
51done:
52 return( ret );
53}
Paul Bakker37940d9f2009-07-10 22:38:58 +000054END_HEADER
55
Paul Bakker5690efc2011-05-26 13:16:06 +000056BEGIN_DEPENDENCIES
57depends_on:POLARSSL_X509_PARSE_C:POLARSSL_BIGNUM_C
58END_DEPENDENCIES
59
Paul Bakker37940d9f2009-07-10 22:38:58 +000060BEGIN_CASE
61x509_cert_info:crt_file:result_str
62{
63 x509_cert crt;
64 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +000065 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +000066
67 memset( &crt, 0, sizeof( x509_cert ) );
68 memset( buf, 0, 2000 );
69
Paul Bakker69e095c2011-12-10 21:55:01 +000070 TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 );
Paul Bakker69998dd2009-07-11 19:15:20 +000071 res = x509parse_cert_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +000072
Paul Bakkerb08e6842012-02-11 18:43:20 +000073 x509_free( &crt );
74
Paul Bakker37940d9f2009-07-10 22:38:58 +000075 TEST_ASSERT( res != -1 );
76 TEST_ASSERT( res != -2 );
77
78 TEST_ASSERT( strcmp( buf, {result_str} ) == 0 );
79}
80END_CASE
81
82BEGIN_CASE
83x509_crl_info:crl_file:result_str
84{
85 x509_crl crl;
86 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +000087 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +000088
89 memset( &crl, 0, sizeof( x509_crl ) );
90 memset( buf, 0, 2000 );
91
92 TEST_ASSERT( x509parse_crlfile( &crl, {crl_file} ) == 0 );
Paul Bakker69998dd2009-07-11 19:15:20 +000093 res = x509parse_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +000094
Paul Bakkerb08e6842012-02-11 18:43:20 +000095 x509_crl_free( &crl );
96
Paul Bakker37940d9f2009-07-10 22:38:58 +000097 TEST_ASSERT( res != -1 );
98 TEST_ASSERT( res != -2 );
99
100 TEST_ASSERT( strcmp( buf, {result_str} ) == 0 );
101}
102END_CASE
103
104BEGIN_CASE
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000105x509_verify:crt_file:ca_file:crl_file:cn_name:result:flags:verify_callback
Paul Bakker37940d9f2009-07-10 22:38:58 +0000106{
107 x509_cert crt;
108 x509_cert ca;
109 x509_crl crl;
110 int flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000111 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000112
113 memset( &crt, 0, sizeof( x509_cert ) );
114 memset( &ca, 0, sizeof( x509_cert ) );
115 memset( &crl, 0, sizeof( x509_crl ) );
116
Paul Bakker69e095c2011-12-10 21:55:01 +0000117 TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 );
118 TEST_ASSERT( x509parse_crtfile( &ca, {ca_file} ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000119 TEST_ASSERT( x509parse_crlfile( &crl, {crl_file} ) == 0 );
120
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000121 res = x509parse_verify( &crt, &ca, &crl, {cn_name}, &flags, {verify_callback}, NULL );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000122
Paul Bakkerb08e6842012-02-11 18:43:20 +0000123 x509_free( &crt );
124 x509_free( &ca );
125 x509_crl_free( &crl );
126
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000127 TEST_ASSERT( res == ( {result} ) );
128 TEST_ASSERT( flags == ( {flags} ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000129}
130END_CASE
131
132BEGIN_CASE
133x509_dn_gets:crt_file:entity:result_str
134{
135 x509_cert crt;
136 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000137 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000138
139 memset( &crt, 0, sizeof( x509_cert ) );
140 memset( buf, 0, 2000 );
141
Paul Bakker69e095c2011-12-10 21:55:01 +0000142 TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 );
Paul Bakker69998dd2009-07-11 19:15:20 +0000143 res = x509parse_dn_gets( buf, 2000, &crt.{entity} );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000144
Paul Bakkerb08e6842012-02-11 18:43:20 +0000145 x509_free( &crt );
146
Paul Bakker37940d9f2009-07-10 22:38:58 +0000147 TEST_ASSERT( res != -1 );
148 TEST_ASSERT( res != -2 );
149
150 TEST_ASSERT( strcmp( buf, {result_str} ) == 0 );
151}
152END_CASE
153
154BEGIN_CASE
155x509_time_expired:crt_file:entity:result
156{
157 x509_cert crt;
158
159 memset( &crt, 0, sizeof( x509_cert ) );
160
Paul Bakker69e095c2011-12-10 21:55:01 +0000161 TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000162 TEST_ASSERT( x509parse_time_expired( &crt.{entity} ) == {result} );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000163
164 x509_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000165}
166END_CASE
167
168BEGIN_CASE
Paul Bakker0d844dd2014-07-07 17:44:14 +0200169x509_time_future:crt_file:entity:result
170{
171 x509_cert crt;
172
173 memset( &crt, 0, sizeof( x509_cert ) );
174
175 TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 );
176 TEST_ASSERT( x509parse_time_future( &crt.{entity} ) == {result} );
177
178 x509_free( &crt );
179}
180END_CASE
181
182BEGIN_CASE
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000183x509parse_keyfile:key_file:password:result
Paul Bakker37940d9f2009-07-10 22:38:58 +0000184{
185 rsa_context rsa;
Paul Bakker69998dd2009-07-11 19:15:20 +0000186 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000187
188 memset( &rsa, 0, sizeof( rsa_context ) );
189
Paul Bakker69998dd2009-07-11 19:15:20 +0000190 res = x509parse_keyfile( &rsa, {key_file}, {password} );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000191
192 TEST_ASSERT( res == {result} );
193
194 if( res == 0 )
195 {
196 TEST_ASSERT( rsa_check_privkey( &rsa ) == 0 );
197 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000198
199 rsa_free( &rsa );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000200}
201END_CASE
202
203BEGIN_CASE
Paul Bakker36f1b192011-07-13 11:32:29 +0000204x509parse_public_keyfile:key_file:result
205{
206 rsa_context rsa;
207 int res;
208
209 memset( &rsa, 0, sizeof( rsa_context ) );
210
211 res = x509parse_public_keyfile( &rsa, {key_file} );
212
213 TEST_ASSERT( res == {result} );
214
215 if( res == 0 )
216 {
217 TEST_ASSERT( rsa_check_pubkey( &rsa ) == 0 );
218 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000219
220 rsa_free( &rsa );
Paul Bakker36f1b192011-07-13 11:32:29 +0000221}
222END_CASE
223
224BEGIN_CASE
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000225x509parse_crt:crt_data:result_str:result
226{
227 x509_cert crt;
228 unsigned char buf[2000];
229 unsigned char output[2000];
230 int data_len, res;
231
232 memset( &crt, 0, sizeof( x509_cert ) );
233 memset( buf, 0, 2000 );
234 memset( output, 0, 2000 );
235
236 data_len = unhexify( buf, {crt_data} );
237
Paul Bakker69e095c2011-12-10 21:55:01 +0000238 TEST_ASSERT( x509parse_crt( &crt, buf, data_len ) == ( {result} ) );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000239 if( ( {result} ) == 0 )
240 {
241 res = x509parse_cert_info( (char *) output, 2000, "", &crt );
242
243 TEST_ASSERT( res != -1 );
244 TEST_ASSERT( res != -2 );
245
246 TEST_ASSERT( strcmp( (char *) output, {result_str} ) == 0 );
247 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000248
249 x509_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000250}
251END_CASE
252
253BEGIN_CASE
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000254x509parse_crl:crl_data:result_str:result
255{
256 x509_crl crl;
257 unsigned char buf[2000];
258 unsigned char output[2000];
259 int data_len, res;
260
261 memset( &crl, 0, sizeof( x509_crl ) );
262 memset( buf, 0, 2000 );
263 memset( output, 0, 2000 );
264
265 data_len = unhexify( buf, {crl_data} );
266
267 TEST_ASSERT( x509parse_crl( &crl, buf, data_len ) == ( {result} ) );
268 if( ( {result} ) == 0 )
269 {
270 res = x509parse_crl_info( (char *) output, 2000, "", &crl );
271
272 TEST_ASSERT( res != -1 );
273 TEST_ASSERT( res != -2 );
274
275 TEST_ASSERT( strcmp( (char *) output, {result_str} ) == 0 );
276 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000277
278 x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000279}
280END_CASE
281
282BEGIN_CASE
283x509parse_key:key_data:result_str:result
284{
285 rsa_context rsa;
286 unsigned char buf[2000];
287 unsigned char output[2000];
Paul Bakkereaf90d92011-07-13 14:21:52 +0000288 int data_len;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000289
290 memset( &rsa, 0, sizeof( rsa_context ) );
291 memset( buf, 0, 2000 );
292 memset( output, 0, 2000 );
293
294 data_len = unhexify( buf, {key_data} );
295
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000296 TEST_ASSERT( x509parse_key( &rsa, buf, data_len, NULL, 0 ) == ( {result} ) );
297 if( ( {result} ) == 0 )
298 {
299 TEST_ASSERT( 1 );
300 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000301
302 rsa_free( &rsa );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000303}
304END_CASE
305
306BEGIN_CASE
Manuel Pégourié-Gonnardab003742015-10-29 15:27:03 +0100307x509_crt_verify_chain:chain_paths_str:trusted_ca:flags_result
Manuel Pégourié-Gonnard03ae1bc2015-11-02 06:09:57 +0900308{
309 char *act;
310 int flags;
Manuel Pégourié-Gonnardab003742015-10-29 15:27:03 +0100311 int result, res;
Manuel Pégourié-Gonnard03ae1bc2015-11-02 06:09:57 +0900312 x509_cert trusted, chain;
313 char *chain_paths;
314
315 memset( &chain, 0, sizeof( x509_cert ) );
316 memset( &trusted, 0, sizeof( x509_cert ) );
317 chain_paths = strdup( {chain_paths_str} );
318 TEST_ASSERT( chain_paths != NULL );
319
Manuel Pégourié-Gonnard4dd43ae2015-11-02 06:52:52 +0900320 while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
Manuel Pégourié-Gonnard03ae1bc2015-11-02 06:09:57 +0900321 TEST_ASSERT( x509parse_crtfile( &chain, act ) == 0 );
322 TEST_ASSERT( x509parse_crtfile( &trusted, {trusted_ca} ) == 0 );
323
324 res = x509parse_verify( &chain, &trusted, NULL, NULL, &flags, NULL, NULL );
325
326 x509_free( &trusted );
327 x509_free( &chain );
328
Manuel Pégourié-Gonnardab003742015-10-29 15:27:03 +0100329 result = ( {flags_result} ) ? POLARSSL_ERR_X509_CERT_VERIFY_FAILED : 0;
330
331 TEST_ASSERT( res == result );
332 TEST_ASSERT( flags == ( {flags_result} ) );
Manuel Pégourié-Gonnard03ae1bc2015-11-02 06:09:57 +0900333}
334END_CASE
335
336BEGIN_CASE
Paul Bakker37940d9f2009-07-10 22:38:58 +0000337x509_selftest:
338{
339 TEST_ASSERT( x509_self_test( 0 ) == 0 );
340}
341END_CASE