blob: b6383d734e18ec563ef4bfaf89e319e3a32e52bd [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 Bakkerb63b0af2011-01-13 17:54:59 +000077x509_verify:crt_file:ca_file:crl_file:cn_name:result:flags: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 Bakker37940d9f2009-07-10 22:38:58 +000084
85 memset( &crt, 0, sizeof( x509_cert ) );
86 memset( &ca, 0, sizeof( x509_cert ) );
87 memset( &crl, 0, sizeof( x509_crl ) );
88
Paul Bakker69e095c2011-12-10 21:55:01 +000089 TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 );
90 TEST_ASSERT( x509parse_crtfile( &ca, {ca_file} ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +000091 TEST_ASSERT( x509parse_crlfile( &crl, {crl_file} ) == 0 );
92
Paul Bakkerb63b0af2011-01-13 17:54:59 +000093 res = x509parse_verify( &crt, &ca, &crl, {cn_name}, &flags, {verify_callback}, NULL );
Paul Bakker37940d9f2009-07-10 22:38:58 +000094
Paul Bakkerb08e6842012-02-11 18:43:20 +000095 x509_free( &crt );
96 x509_free( &ca );
97 x509_crl_free( &crl );
98
Paul Bakkerb63b0af2011-01-13 17:54:59 +000099 TEST_ASSERT( res == ( {result} ) );
100 TEST_ASSERT( flags == ( {flags} ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000101}
102END_CASE
103
104BEGIN_CASE
105x509_dn_gets:crt_file:entity:result_str
106{
107 x509_cert crt;
108 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +0000109 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000110
111 memset( &crt, 0, sizeof( x509_cert ) );
112 memset( buf, 0, 2000 );
113
Paul Bakker69e095c2011-12-10 21:55:01 +0000114 TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 );
Paul Bakker69998dd2009-07-11 19:15:20 +0000115 res = x509parse_dn_gets( buf, 2000, &crt.{entity} );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000116
Paul Bakkerb08e6842012-02-11 18:43:20 +0000117 x509_free( &crt );
118
Paul Bakker37940d9f2009-07-10 22:38:58 +0000119 TEST_ASSERT( res != -1 );
120 TEST_ASSERT( res != -2 );
121
122 TEST_ASSERT( strcmp( buf, {result_str} ) == 0 );
123}
124END_CASE
125
126BEGIN_CASE
127x509_time_expired:crt_file:entity:result
128{
129 x509_cert crt;
130
131 memset( &crt, 0, sizeof( x509_cert ) );
132
Paul Bakker69e095c2011-12-10 21:55:01 +0000133 TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000134 TEST_ASSERT( x509parse_time_expired( &crt.{entity} ) == {result} );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000135
136 x509_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000137}
138END_CASE
139
140BEGIN_CASE
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200141x509parse_keyfile_rsa:key_file:password:result
Paul Bakker37940d9f2009-07-10 22:38:58 +0000142{
143 rsa_context rsa;
Paul Bakker69998dd2009-07-11 19:15:20 +0000144 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000145
146 memset( &rsa, 0, sizeof( rsa_context ) );
147
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200148 res = x509parse_keyfile_rsa( &rsa, {key_file}, {password} );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000149
150 TEST_ASSERT( res == {result} );
151
152 if( res == 0 )
153 {
154 TEST_ASSERT( rsa_check_privkey( &rsa ) == 0 );
155 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000156
157 rsa_free( &rsa );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000158}
159END_CASE
160
161BEGIN_CASE
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200162x509parse_public_keyfile_rsa:key_file:result
Paul Bakker36f1b192011-07-13 11:32:29 +0000163{
164 rsa_context rsa;
165 int res;
166
167 memset( &rsa, 0, sizeof( rsa_context ) );
168
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200169 res = x509parse_public_keyfile_rsa( &rsa, {key_file} );
Paul Bakker36f1b192011-07-13 11:32:29 +0000170
171 TEST_ASSERT( res == {result} );
172
173 if( res == 0 )
174 {
175 TEST_ASSERT( rsa_check_pubkey( &rsa ) == 0 );
176 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000177
178 rsa_free( &rsa );
Paul Bakker36f1b192011-07-13 11:32:29 +0000179}
180END_CASE
181
182BEGIN_CASE
Manuel Pégourié-Gonnard1bc69312013-06-27 15:33:04 +0200183x509parse_public_keyfile_ec:key_file:result
184{
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200185 pk_context ctx;
Manuel Pégourié-Gonnard1bc69312013-06-27 15:33:04 +0200186 int res;
187
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200188 pk_init( &ctx );
Manuel Pégourié-Gonnard1bc69312013-06-27 15:33:04 +0200189
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200190 res = x509parse_public_keyfile( &ctx, {key_file} );
Manuel Pégourié-Gonnard1bc69312013-06-27 15:33:04 +0200191
192 TEST_ASSERT( res == {result} );
193
194 if( res == 0 )
195 {
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200196 ecp_keypair *eckey;
197 TEST_ASSERT( ctx.type == POLARSSL_PK_ECKEY );
198 eckey = (ecp_keypair *) ctx.data;
199 TEST_ASSERT( ecp_check_pubkey( &eckey->grp, &eckey->Q ) == 0 );
Manuel Pégourié-Gonnard1bc69312013-06-27 15:33:04 +0200200 }
201
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200202 pk_free( &ctx );
Manuel Pégourié-Gonnard1bc69312013-06-27 15:33:04 +0200203}
204END_CASE
205
206BEGIN_CASE
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200207x509parse_keyfile_ec:key_file:password:result
208{
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200209 pk_context ctx;
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200210 int res;
211
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200212 pk_init( &ctx );
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200213
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200214 res = x509parse_keyfile( &ctx, {key_file}, {password} );
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200215
216 TEST_ASSERT( res == {result} );
217
218 if( res == 0 )
219 {
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200220 ecp_keypair *eckey;
221 TEST_ASSERT( ctx.type == POLARSSL_PK_ECKEY );
222 eckey = (ecp_keypair *) ctx.data;
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +0200223 TEST_ASSERT( ecp_check_privkey( &eckey->grp, &eckey->d ) == 0 );
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200224 }
225
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200226 pk_free( &ctx );
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200227}
228END_CASE
229
230BEGIN_CASE
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000231x509parse_crt:crt_data:result_str:result
232{
233 x509_cert crt;
234 unsigned char buf[2000];
235 unsigned char output[2000];
236 int data_len, res;
237
238 memset( &crt, 0, sizeof( x509_cert ) );
239 memset( buf, 0, 2000 );
240 memset( output, 0, 2000 );
241
242 data_len = unhexify( buf, {crt_data} );
243
Paul Bakker69e095c2011-12-10 21:55:01 +0000244 TEST_ASSERT( x509parse_crt( &crt, buf, data_len ) == ( {result} ) );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000245 if( ( {result} ) == 0 )
246 {
247 res = x509parse_cert_info( (char *) output, 2000, "", &crt );
248
249 TEST_ASSERT( res != -1 );
250 TEST_ASSERT( res != -2 );
251
252 TEST_ASSERT( strcmp( (char *) output, {result_str} ) == 0 );
253 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000254
255 x509_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000256}
257END_CASE
258
259BEGIN_CASE
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000260x509parse_crl:crl_data:result_str:result
261{
262 x509_crl crl;
263 unsigned char buf[2000];
264 unsigned char output[2000];
265 int data_len, res;
266
267 memset( &crl, 0, sizeof( x509_crl ) );
268 memset( buf, 0, 2000 );
269 memset( output, 0, 2000 );
270
271 data_len = unhexify( buf, {crl_data} );
272
273 TEST_ASSERT( x509parse_crl( &crl, buf, data_len ) == ( {result} ) );
274 if( ( {result} ) == 0 )
275 {
276 res = x509parse_crl_info( (char *) output, 2000, "", &crl );
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_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000285}
286END_CASE
287
288BEGIN_CASE
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200289x509parse_key_rsa:key_data:result_str:result
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000290{
291 rsa_context rsa;
292 unsigned char buf[2000];
293 unsigned char output[2000];
Paul Bakkereaf90d92011-07-13 14:21:52 +0000294 int data_len;
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000295
296 memset( &rsa, 0, sizeof( rsa_context ) );
297 memset( buf, 0, 2000 );
298 memset( output, 0, 2000 );
299
300 data_len = unhexify( buf, {key_data} );
301
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200302 TEST_ASSERT( x509parse_key_rsa( &rsa, buf, data_len, NULL, 0 ) == ( {result} ) );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000303 if( ( {result} ) == 0 )
304 {
305 TEST_ASSERT( 1 );
306 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000307
308 rsa_free( &rsa );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000309}
310END_CASE
311
312BEGIN_CASE
Paul Bakker37940d9f2009-07-10 22:38:58 +0000313x509_selftest:
314{
315 TEST_ASSERT( x509_self_test( 0 ) == 0 );
316}
317END_CASE