blob: 6106c7527a52995f589e2883c5b3d1a857d4ee59 [file] [log] [blame]
Paul Bakker37940d9f2009-07-10 22:38:58 +00001BEGIN_HEADER
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002#include <polarssl/config.h>
Paul Bakker37940d9f2009-07-10 22:38:58 +00003#include <polarssl/x509.h>
Paul Bakker96743fc2011-02-12 14:30:57 +00004#include <polarssl/pem.h>
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005
6int verify_none( void *data, x509_cert *crt, int certificate_depth, int preverify_ok )
7{
Paul Bakker5a624082011-01-18 16:31:52 +00008 ((void) data);
9 ((void) crt);
10 ((void) certificate_depth);
11 ((void) preverify_ok);
12
Paul Bakkerb63b0af2011-01-13 17:54:59 +000013 return 1;
14}
15
16int verify_all( void *data, x509_cert *crt, int certificate_depth, int preverify_ok )
17{
Paul Bakker5a624082011-01-18 16:31:52 +000018 ((void) data);
19 ((void) crt);
20 ((void) certificate_depth);
21 ((void) preverify_ok);
22
Paul Bakkerb63b0af2011-01-13 17:54:59 +000023 return 0;
24}
25
Paul Bakker37940d9f2009-07-10 22:38:58 +000026END_HEADER
27
28BEGIN_CASE
29x509_cert_info:crt_file:result_str
30{
31 x509_cert crt;
32 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +000033 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +000034
35 memset( &crt, 0, sizeof( x509_cert ) );
36 memset( buf, 0, 2000 );
37
38 TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 );
Paul Bakker69998dd2009-07-11 19:15:20 +000039 res = x509parse_cert_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +000040
41 TEST_ASSERT( res != -1 );
42 TEST_ASSERT( res != -2 );
43
44 TEST_ASSERT( strcmp( buf, {result_str} ) == 0 );
45}
46END_CASE
47
48BEGIN_CASE
49x509_crl_info:crl_file:result_str
50{
51 x509_crl crl;
52 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +000053 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +000054
55 memset( &crl, 0, sizeof( x509_crl ) );
56 memset( buf, 0, 2000 );
57
58 TEST_ASSERT( x509parse_crlfile( &crl, {crl_file} ) == 0 );
Paul Bakker69998dd2009-07-11 19:15:20 +000059 res = x509parse_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +000060
61 TEST_ASSERT( res != -1 );
62 TEST_ASSERT( res != -2 );
63
64 TEST_ASSERT( strcmp( buf, {result_str} ) == 0 );
65}
66END_CASE
67
68BEGIN_CASE
Paul Bakkerb63b0af2011-01-13 17:54:59 +000069x509_verify:crt_file:ca_file:crl_file:cn_name:result:flags:verify_callback
Paul Bakker37940d9f2009-07-10 22:38:58 +000070{
71 x509_cert crt;
72 x509_cert ca;
73 x509_crl crl;
74 int flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +000075 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +000076
77 memset( &crt, 0, sizeof( x509_cert ) );
78 memset( &ca, 0, sizeof( x509_cert ) );
79 memset( &crl, 0, sizeof( x509_crl ) );
80
81 TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 );
82 TEST_ASSERT( x509parse_crtfile( &ca, {ca_file} ) == 0 );
83 TEST_ASSERT( x509parse_crlfile( &crl, {crl_file} ) == 0 );
84
Paul Bakkerb63b0af2011-01-13 17:54:59 +000085 res = x509parse_verify( &crt, &ca, &crl, {cn_name}, &flags, {verify_callback}, NULL );
Paul Bakker37940d9f2009-07-10 22:38:58 +000086
Paul Bakkerb63b0af2011-01-13 17:54:59 +000087 TEST_ASSERT( res == ( {result} ) );
88 TEST_ASSERT( flags == ( {flags} ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +000089}
90END_CASE
91
92BEGIN_CASE
93x509_dn_gets:crt_file:entity:result_str
94{
95 x509_cert crt;
96 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +000097 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +000098
99 memset( &crt, 0, sizeof( x509_cert ) );
100 memset( buf, 0, 2000 );
101
102 TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 );
Paul Bakker69998dd2009-07-11 19:15:20 +0000103 res = x509parse_dn_gets( buf, 2000, &crt.{entity} );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000104
105 TEST_ASSERT( res != -1 );
106 TEST_ASSERT( res != -2 );
107
108 TEST_ASSERT( strcmp( buf, {result_str} ) == 0 );
109}
110END_CASE
111
112BEGIN_CASE
113x509_time_expired:crt_file:entity:result
114{
115 x509_cert crt;
116
117 memset( &crt, 0, sizeof( x509_cert ) );
118
119 TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 );
120 TEST_ASSERT( x509parse_time_expired( &crt.{entity} ) == {result} );
121}
122END_CASE
123
124BEGIN_CASE
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000125x509parse_keyfile:key_file:password:result
Paul Bakker37940d9f2009-07-10 22:38:58 +0000126{
127 rsa_context rsa;
Paul Bakker69998dd2009-07-11 19:15:20 +0000128 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000129
130 memset( &rsa, 0, sizeof( rsa_context ) );
131
Paul Bakker69998dd2009-07-11 19:15:20 +0000132 res = x509parse_keyfile( &rsa, {key_file}, {password} );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000133
134 TEST_ASSERT( res == {result} );
135
136 if( res == 0 )
137 {
138 TEST_ASSERT( rsa_check_privkey( &rsa ) == 0 );
139 }
140}
141END_CASE
142
143BEGIN_CASE
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000144x509parse_crt:crt_data:result_str:result
145{
146 x509_cert crt;
147 unsigned char buf[2000];
148 unsigned char output[2000];
149 int data_len, res;
150
151 memset( &crt, 0, sizeof( x509_cert ) );
152 memset( buf, 0, 2000 );
153 memset( output, 0, 2000 );
154
155 data_len = unhexify( buf, {crt_data} );
156
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000157 TEST_ASSERT( x509parse_crt( &crt, buf, data_len ) == ( {result} ) );
158 if( ( {result} ) == 0 )
159 {
160 res = x509parse_cert_info( (char *) output, 2000, "", &crt );
161
162 TEST_ASSERT( res != -1 );
163 TEST_ASSERT( res != -2 );
164
165 TEST_ASSERT( strcmp( (char *) output, {result_str} ) == 0 );
166 }
167}
168END_CASE
169
170BEGIN_CASE
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000171x509parse_crl:crl_data:result_str:result
172{
173 x509_crl crl;
174 unsigned char buf[2000];
175 unsigned char output[2000];
176 int data_len, res;
177
178 memset( &crl, 0, sizeof( x509_crl ) );
179 memset( buf, 0, 2000 );
180 memset( output, 0, 2000 );
181
182 data_len = unhexify( buf, {crl_data} );
183
184 TEST_ASSERT( x509parse_crl( &crl, buf, data_len ) == ( {result} ) );
185 if( ( {result} ) == 0 )
186 {
187 res = x509parse_crl_info( (char *) output, 2000, "", &crl );
188
189 TEST_ASSERT( res != -1 );
190 TEST_ASSERT( res != -2 );
191
192 TEST_ASSERT( strcmp( (char *) output, {result_str} ) == 0 );
193 }
194}
195END_CASE
196
197BEGIN_CASE
198x509parse_key:key_data:result_str:result
199{
200 rsa_context rsa;
201 unsigned char buf[2000];
202 unsigned char output[2000];
203 int data_len, res;
204
205 memset( &rsa, 0, sizeof( rsa_context ) );
206 memset( buf, 0, 2000 );
207 memset( output, 0, 2000 );
208
209 data_len = unhexify( buf, {key_data} );
210
211 res = x509parse_key( &rsa, buf, data_len, NULL, 0 );
212
213 TEST_ASSERT( x509parse_key( &rsa, buf, data_len, NULL, 0 ) == ( {result} ) );
214 if( ( {result} ) == 0 )
215 {
216 TEST_ASSERT( 1 );
217 }
218}
219END_CASE
220
221BEGIN_CASE
Paul Bakker37940d9f2009-07-10 22:38:58 +0000222x509_selftest:
223{
224 TEST_ASSERT( x509_self_test( 0 ) == 0 );
225}
226END_CASE