blob: f0defa3cf0d144c5c0744cdf1cb6191d37f2437e [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 Bakkerc70b9822013-04-07 22:00:46 +02004#include <polarssl/oid.h>
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005
Paul Bakker915275b2012-09-28 07:10:55 +00006int verify_none( void *data, x509_cert *crt, int certificate_depth, int *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007{
Paul Bakker5a624082011-01-18 16:31:52 +00008 ((void) data);
9 ((void) crt);
10 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000011 *flags |= BADCERT_OTHER;
12
13 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000014}
15
Paul Bakker915275b2012-09-28 07:10:55 +000016int verify_all( void *data, x509_cert *crt, int certificate_depth, int *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000017{
Paul Bakker5a624082011-01-18 16:31:52 +000018 ((void) data);
19 ((void) crt);
20 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000021 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000022
Paul Bakkerb63b0af2011-01-13 17:54:59 +000023 return 0;
24}
25
Paul Bakker37940d9f2009-07-10 22:38:58 +000026END_HEADER
27
Paul Bakker5690efc2011-05-26 13:16:06 +000028BEGIN_DEPENDENCIES
29depends_on:POLARSSL_X509_PARSE_C:POLARSSL_BIGNUM_C
30END_DEPENDENCIES
31
Paul Bakker37940d9f2009-07-10 22:38:58 +000032BEGIN_CASE
33x509_cert_info:crt_file:result_str
34{
35 x509_cert crt;
36 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +000037 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +000038
39 memset( &crt, 0, sizeof( x509_cert ) );
40 memset( buf, 0, 2000 );
41
Paul Bakker69e095c2011-12-10 21:55:01 +000042 TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 );
Paul Bakker69998dd2009-07-11 19:15:20 +000043 res = x509parse_cert_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +000044
Paul Bakkerb08e6842012-02-11 18:43:20 +000045 x509_free( &crt );
46
Paul Bakker37940d9f2009-07-10 22:38:58 +000047 TEST_ASSERT( res != -1 );
48 TEST_ASSERT( res != -2 );
49
50 TEST_ASSERT( strcmp( buf, {result_str} ) == 0 );
51}
52END_CASE
53
54BEGIN_CASE
55x509_crl_info:crl_file:result_str
56{
57 x509_crl crl;
58 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +000059 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +000060
61 memset( &crl, 0, sizeof( x509_crl ) );
62 memset( buf, 0, 2000 );
63
64 TEST_ASSERT( x509parse_crlfile( &crl, {crl_file} ) == 0 );
Paul Bakker69998dd2009-07-11 19:15:20 +000065 res = x509parse_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +000066
Paul Bakkerb08e6842012-02-11 18:43:20 +000067 x509_crl_free( &crl );
68
Paul Bakker37940d9f2009-07-10 22:38:58 +000069 TEST_ASSERT( res != -1 );
70 TEST_ASSERT( res != -2 );
71
72 TEST_ASSERT( strcmp( buf, {result_str} ) == 0 );
73}
74END_CASE
75
76BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +020077x509_verify:crt_file:ca_file:crl_file:cn_name_str:#result:#flags_result:verify_callback
Paul Bakker37940d9f2009-07-10 22:38:58 +000078{
79 x509_cert crt;
80 x509_cert ca;
81 x509_crl crl;
82 int flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +000083 int res;
Paul Bakkerdbd443d2013-08-16 13:38:47 +020084 int (*f_vrfy)(void *, x509_cert *, int, int *) = NULL;
85 char * cn_name = NULL;
Paul Bakker37940d9f2009-07-10 22:38:58 +000086
87 memset( &crt, 0, sizeof( x509_cert ) );
88 memset( &ca, 0, sizeof( x509_cert ) );
89 memset( &crl, 0, sizeof( x509_crl ) );
90
Paul Bakkerdbd443d2013-08-16 13:38:47 +020091 if( strcmp( {cn_name_str}, "NULL" ) != 0 )
92 cn_name = {cn_name_str};
93
94 if( strcmp( {verify_callback}, "NULL" ) == 0 )
95 f_vrfy = NULL;
96 else if( strcmp( {verify_callback}, "verify_none" ) == 0 )
97 f_vrfy = verify_none;
98 else if( strcmp( {verify_callback}, "verify_all" ) == 0 )
99 f_vrfy = verify_all;
100 else
101 TEST_ASSERT( "No known verify callback selected" == 0 );
102
Paul Bakker69e095c2011-12-10 21:55:01 +0000103 TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 );
104 TEST_ASSERT( x509parse_crtfile( &ca, {ca_file} ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000105 TEST_ASSERT( x509parse_crlfile( &crl, {crl_file} ) == 0 );
106
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200107 res = x509parse_verify( &crt, &ca, &crl, cn_name, &flags, f_vrfy, NULL );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000108
Paul Bakkerb08e6842012-02-11 18:43:20 +0000109 x509_free( &crt );
110 x509_free( &ca );
111 x509_crl_free( &crl );
112
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000113 TEST_ASSERT( res == ( {result} ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200114 TEST_ASSERT( flags == ( {flags_result} ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000115}
116END_CASE
117
118BEGIN_CASE
119x509_dn_gets:crt_file:entity:result_str
120{
121 x509_cert crt;
122 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200123 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000124
125 memset( &crt, 0, sizeof( x509_cert ) );
126 memset( buf, 0, 2000 );
127
Paul Bakker69e095c2011-12-10 21:55:01 +0000128 TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200129 if( strcmp( {entity}, "subject" ) == 0 )
130 res = x509parse_dn_gets( buf, 2000, &crt.subject );
131 else if( strcmp( {entity}, "issuer" ) == 0 )
132 res = x509parse_dn_gets( buf, 2000, &crt.issuer );
133 else
134 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000135
Paul Bakkerb08e6842012-02-11 18:43:20 +0000136 x509_free( &crt );
137
Paul Bakker37940d9f2009-07-10 22:38:58 +0000138 TEST_ASSERT( res != -1 );
139 TEST_ASSERT( res != -2 );
140
141 TEST_ASSERT( strcmp( buf, {result_str} ) == 0 );
142}
143END_CASE
144
145BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200146x509_time_expired:crt_file:entity:#result
Paul Bakker37940d9f2009-07-10 22:38:58 +0000147{
148 x509_cert crt;
149
150 memset( &crt, 0, sizeof( x509_cert ) );
151
Paul Bakker69e095c2011-12-10 21:55:01 +0000152 TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200153
154 if( strcmp( {entity}, "valid_from" ) == 0 )
155 TEST_ASSERT( x509parse_time_expired( &crt.valid_from ) == {result} );
156 else if( strcmp( {entity}, "valid_to" ) == 0 )
157 TEST_ASSERT( x509parse_time_expired( &crt.valid_to ) == {result} );
158 else
159 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000160
161 x509_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000162}
163END_CASE
164
165BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200166x509parse_keyfile_rsa:key_file:password:#result
Paul Bakker37940d9f2009-07-10 22:38:58 +0000167{
168 rsa_context rsa;
Paul Bakker69998dd2009-07-11 19:15:20 +0000169 int res;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200170 char *pwd = {password};
Paul Bakker37940d9f2009-07-10 22:38:58 +0000171
172 memset( &rsa, 0, sizeof( rsa_context ) );
173
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200174 if( strcmp( pwd, "NULL" ) == 0 )
175 pwd = NULL;
176
177 res = x509parse_keyfile_rsa( &rsa, {key_file}, pwd );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000178
179 TEST_ASSERT( res == {result} );
180
181 if( res == 0 )
182 {
183 TEST_ASSERT( rsa_check_privkey( &rsa ) == 0 );
184 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000185
186 rsa_free( &rsa );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000187}
188END_CASE
189
190BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200191x509parse_public_keyfile_rsa:key_file:#result
Paul Bakker36f1b192011-07-13 11:32:29 +0000192{
193 rsa_context rsa;
194 int res;
195
196 memset( &rsa, 0, sizeof( rsa_context ) );
197
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200198 res = x509parse_public_keyfile_rsa( &rsa, {key_file} );
Paul Bakker36f1b192011-07-13 11:32:29 +0000199
200 TEST_ASSERT( res == {result} );
201
202 if( res == 0 )
203 {
204 TEST_ASSERT( rsa_check_pubkey( &rsa ) == 0 );
205 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000206
207 rsa_free( &rsa );
Paul Bakker36f1b192011-07-13 11:32:29 +0000208}
209END_CASE
210
211BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200212x509parse_public_keyfile_ec:key_file:#result
Manuel Pégourié-Gonnard1bc69312013-06-27 15:33:04 +0200213{
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200214 pk_context ctx;
Manuel Pégourié-Gonnard1bc69312013-06-27 15:33:04 +0200215 int res;
216
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200217 pk_init( &ctx );
Manuel Pégourié-Gonnard1bc69312013-06-27 15:33:04 +0200218
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200219 res = x509parse_public_keyfile( &ctx, {key_file} );
Manuel Pégourié-Gonnard1bc69312013-06-27 15:33:04 +0200220
221 TEST_ASSERT( res == {result} );
222
223 if( res == 0 )
224 {
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200225 ecp_keypair *eckey;
226 TEST_ASSERT( ctx.type == POLARSSL_PK_ECKEY );
227 eckey = (ecp_keypair *) ctx.data;
228 TEST_ASSERT( ecp_check_pubkey( &eckey->grp, &eckey->Q ) == 0 );
Manuel Pégourié-Gonnard1bc69312013-06-27 15:33:04 +0200229 }
230
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200231 pk_free( &ctx );
Manuel Pégourié-Gonnard1bc69312013-06-27 15:33:04 +0200232}
233END_CASE
234
235BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200236x509parse_keyfile_ec:key_file:password:#result
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200237{
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200238 pk_context ctx;
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200239 int res;
240
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200241 pk_init( &ctx );
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200242
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200243 res = x509parse_keyfile( &ctx, {key_file}, {password} );
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200244
245 TEST_ASSERT( res == {result} );
246
247 if( res == 0 )
248 {
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200249 ecp_keypair *eckey;
250 TEST_ASSERT( ctx.type == POLARSSL_PK_ECKEY );
251 eckey = (ecp_keypair *) ctx.data;
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +0200252 TEST_ASSERT( ecp_check_privkey( &eckey->grp, &eckey->d ) == 0 );
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200253 }
254
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200255 pk_free( &ctx );
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200256}
257END_CASE
258
259BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200260x509parse_crt:crt_data:result_str:#result
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000261{
262 x509_cert crt;
263 unsigned char buf[2000];
264 unsigned char output[2000];
265 int data_len, res;
266
267 memset( &crt, 0, sizeof( x509_cert ) );
268 memset( buf, 0, 2000 );
269 memset( output, 0, 2000 );
270
271 data_len = unhexify( buf, {crt_data} );
272
Paul Bakker69e095c2011-12-10 21:55:01 +0000273 TEST_ASSERT( x509parse_crt( &crt, buf, data_len ) == ( {result} ) );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000274 if( ( {result} ) == 0 )
275 {
276 res = x509parse_cert_info( (char *) output, 2000, "", &crt );
277
278 TEST_ASSERT( res != -1 );
279 TEST_ASSERT( res != -2 );
280
281 TEST_ASSERT( strcmp( (char *) output, {result_str} ) == 0 );
282 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000283
284 x509_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000285}
286END_CASE
287
288BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200289x509parse_crl:crl_data:result_str:#result
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000290{
291 x509_crl crl;
292 unsigned char buf[2000];
293 unsigned char output[2000];
294 int data_len, res;
295
296 memset( &crl, 0, sizeof( x509_crl ) );
297 memset( buf, 0, 2000 );
298 memset( output, 0, 2000 );
299
300 data_len = unhexify( buf, {crl_data} );
301
302 TEST_ASSERT( x509parse_crl( &crl, buf, data_len ) == ( {result} ) );
303 if( ( {result} ) == 0 )
304 {
305 res = x509parse_crl_info( (char *) output, 2000, "", &crl );
306
307 TEST_ASSERT( res != -1 );
308 TEST_ASSERT( res != -2 );
309
310 TEST_ASSERT( strcmp( (char *) output, {result_str} ) == 0 );
311 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000312
313 x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000314}
315END_CASE
316
317BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200318x509parse_key_rsa:key_data:result_str:#result
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000319{
320 rsa_context rsa;
321 unsigned char buf[2000];
322 unsigned char output[2000];
Paul Bakkereaf90d92011-07-13 14:21:52 +0000323 int data_len;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200324 ((void) result_str);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000325
326 memset( &rsa, 0, sizeof( rsa_context ) );
327 memset( buf, 0, 2000 );
328 memset( output, 0, 2000 );
329
330 data_len = unhexify( buf, {key_data} );
331
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200332 TEST_ASSERT( x509parse_key_rsa( &rsa, buf, data_len, NULL, 0 ) == ( {result} ) );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000333 if( ( {result} ) == 0 )
334 {
335 TEST_ASSERT( 1 );
336 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000337
338 rsa_free( &rsa );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000339}
340END_CASE
341
342BEGIN_CASE
Paul Bakker37940d9f2009-07-10 22:38:58 +0000343x509_selftest:
344{
345 TEST_ASSERT( x509_self_test( 0 ) == 0 );
346}
347END_CASE