blob: 57653f79853930412284eb4880af95daf29d8573 [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{
185 ecp_keypair eckey;
186 int res;
187
188 ecp_keypair_init( &eckey );
189
190 res = x509parse_public_keyfile_ec( &eckey, {key_file} );
191
192 TEST_ASSERT( res == {result} );
193
194 if( res == 0 )
195 {
196 TEST_ASSERT( ecp_check_pubkey( &eckey.grp, &eckey.Q ) == 0 );
197 }
198
199 ecp_keypair_free( &eckey );
200}
201END_CASE
202
203BEGIN_CASE
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200204x509parse_keyfile_ec:key_file:password:result
205{
206 ecp_keypair eckey;
207 int res;
208
209 ecp_keypair_init( &eckey );
210
211 res = x509parse_keyfile_ec( &eckey, {key_file}, {password} );
212
213 TEST_ASSERT( res == {result} );
214
215 if( res == 0 )
216 {
217 TEST_ASSERT( ecp_check_prvkey( &eckey.grp, &eckey.d ) == 0 );
218 }
219
220 ecp_keypair_free( &eckey );
221}
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
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200283x509parse_key_rsa:key_data:result_str:result
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000284{
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
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200296 TEST_ASSERT( x509parse_key_rsa( &rsa, buf, data_len, NULL, 0 ) == ( {result} ) );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000297 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
Paul Bakker37940d9f2009-07-10 22:38:58 +0000307x509_selftest:
308{
309 TEST_ASSERT( x509_self_test( 0 ) == 0 );
310}
311END_CASE