blob: cec4d8d8d528cc4d3373b6a69d75e57aa691cd95 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +00002#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 Bakker33b43f12013-08-20 11:48:36 +020026/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +000027
Paul Bakker33b43f12013-08-20 11:48:36 +020028/* BEGIN_DEPENDENCIES
29 * depends_on:POLARSSL_X509_PARSE_C:POLARSSL_BIGNUM_C
30 * END_DEPENDENCIES
31 */
Paul Bakker5690efc2011-05-26 13:16:06 +000032
Paul Bakker33b43f12013-08-20 11:48:36 +020033/* BEGIN_CASE */
34void x509_cert_info( char *crt_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +000035{
36 x509_cert crt;
37 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +000038 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +000039
40 memset( &crt, 0, sizeof( x509_cert ) );
41 memset( buf, 0, 2000 );
42
Paul Bakker33b43f12013-08-20 11:48:36 +020043 TEST_ASSERT( x509parse_crtfile( &crt, crt_file ) == 0 );
Paul Bakker69998dd2009-07-11 19:15:20 +000044 res = x509parse_cert_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +000045
Paul Bakkerb08e6842012-02-11 18:43:20 +000046 x509_free( &crt );
47
Paul Bakker37940d9f2009-07-10 22:38:58 +000048 TEST_ASSERT( res != -1 );
49 TEST_ASSERT( res != -2 );
50
Paul Bakker33b43f12013-08-20 11:48:36 +020051 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +000052}
Paul Bakker33b43f12013-08-20 11:48:36 +020053/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +000054
Paul Bakker33b43f12013-08-20 11:48:36 +020055/* BEGIN_CASE */
56void x509_crl_info( char *crl_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +000057{
58 x509_crl crl;
59 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +000060 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +000061
62 memset( &crl, 0, sizeof( x509_crl ) );
63 memset( buf, 0, 2000 );
64
Paul Bakker33b43f12013-08-20 11:48:36 +020065 TEST_ASSERT( x509parse_crlfile( &crl, crl_file ) == 0 );
Paul Bakker69998dd2009-07-11 19:15:20 +000066 res = x509parse_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +000067
Paul Bakkerb08e6842012-02-11 18:43:20 +000068 x509_crl_free( &crl );
69
Paul Bakker37940d9f2009-07-10 22:38:58 +000070 TEST_ASSERT( res != -1 );
71 TEST_ASSERT( res != -2 );
72
Paul Bakker33b43f12013-08-20 11:48:36 +020073 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +000074}
Paul Bakker33b43f12013-08-20 11:48:36 +020075/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +000076
Paul Bakker33b43f12013-08-20 11:48:36 +020077/* BEGIN_CASE */
78void x509_verify( char *crt_file, char *ca_file, char *crl_file,
79 char *cn_name_str, int result, int flags_result,
80 char *verify_callback )
Paul Bakker37940d9f2009-07-10 22:38:58 +000081{
82 x509_cert crt;
83 x509_cert ca;
84 x509_crl crl;
85 int flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +000086 int res;
Paul Bakkerdbd443d2013-08-16 13:38:47 +020087 int (*f_vrfy)(void *, x509_cert *, int, int *) = NULL;
88 char * cn_name = NULL;
Paul Bakker37940d9f2009-07-10 22:38:58 +000089
90 memset( &crt, 0, sizeof( x509_cert ) );
91 memset( &ca, 0, sizeof( x509_cert ) );
92 memset( &crl, 0, sizeof( x509_crl ) );
93
Paul Bakker33b43f12013-08-20 11:48:36 +020094 if( strcmp( cn_name_str, "NULL" ) != 0 )
95 cn_name = cn_name_str;
Paul Bakkerdbd443d2013-08-16 13:38:47 +020096
Paul Bakker33b43f12013-08-20 11:48:36 +020097 if( strcmp( verify_callback, "NULL" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +020098 f_vrfy = NULL;
Paul Bakker33b43f12013-08-20 11:48:36 +020099 else if( strcmp( verify_callback, "verify_none" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200100 f_vrfy = verify_none;
Paul Bakker33b43f12013-08-20 11:48:36 +0200101 else if( strcmp( verify_callback, "verify_all" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200102 f_vrfy = verify_all;
103 else
104 TEST_ASSERT( "No known verify callback selected" == 0 );
105
Paul Bakker33b43f12013-08-20 11:48:36 +0200106 TEST_ASSERT( x509parse_crtfile( &crt, crt_file ) == 0 );
107 TEST_ASSERT( x509parse_crtfile( &ca, ca_file ) == 0 );
108 TEST_ASSERT( x509parse_crlfile( &crl, crl_file ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000109
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200110 res = x509parse_verify( &crt, &ca, &crl, cn_name, &flags, f_vrfy, NULL );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000111
Paul Bakkerb08e6842012-02-11 18:43:20 +0000112 x509_free( &crt );
113 x509_free( &ca );
114 x509_crl_free( &crl );
115
Paul Bakker33b43f12013-08-20 11:48:36 +0200116 TEST_ASSERT( res == ( result ) );
117 TEST_ASSERT( flags == ( flags_result ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000118}
Paul Bakker33b43f12013-08-20 11:48:36 +0200119/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000120
Paul Bakker33b43f12013-08-20 11:48:36 +0200121/* BEGIN_CASE */
122void x509_dn_gets( char *crt_file, char *entity, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000123{
124 x509_cert crt;
125 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200126 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000127
128 memset( &crt, 0, sizeof( x509_cert ) );
129 memset( buf, 0, 2000 );
130
Paul Bakker33b43f12013-08-20 11:48:36 +0200131 TEST_ASSERT( x509parse_crtfile( &crt, crt_file ) == 0 );
132 if( strcmp( entity, "subject" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200133 res = x509parse_dn_gets( buf, 2000, &crt.subject );
Paul Bakker33b43f12013-08-20 11:48:36 +0200134 else if( strcmp( entity, "issuer" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200135 res = x509parse_dn_gets( buf, 2000, &crt.issuer );
136 else
137 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000138
Paul Bakkerb08e6842012-02-11 18:43:20 +0000139 x509_free( &crt );
140
Paul Bakker37940d9f2009-07-10 22:38:58 +0000141 TEST_ASSERT( res != -1 );
142 TEST_ASSERT( res != -2 );
143
Paul Bakker33b43f12013-08-20 11:48:36 +0200144 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000145}
Paul Bakker33b43f12013-08-20 11:48:36 +0200146/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000147
Paul Bakker33b43f12013-08-20 11:48:36 +0200148/* BEGIN_CASE */
149void x509_time_expired( char *crt_file, char *entity, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000150{
151 x509_cert crt;
152
153 memset( &crt, 0, sizeof( x509_cert ) );
154
Paul Bakker33b43f12013-08-20 11:48:36 +0200155 TEST_ASSERT( x509parse_crtfile( &crt, crt_file ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200156
Paul Bakker33b43f12013-08-20 11:48:36 +0200157 if( strcmp( entity, "valid_from" ) == 0 )
158 TEST_ASSERT( x509parse_time_expired( &crt.valid_from ) == result );
159 else if( strcmp( entity, "valid_to" ) == 0 )
160 TEST_ASSERT( x509parse_time_expired( &crt.valid_to ) == result );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200161 else
162 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000163
164 x509_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000165}
Paul Bakker33b43f12013-08-20 11:48:36 +0200166/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000167
Paul Bakker33b43f12013-08-20 11:48:36 +0200168/* BEGIN_CASE */
169void x509parse_keyfile_rsa( char *key_file, char *password, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000170{
171 rsa_context rsa;
Paul Bakker69998dd2009-07-11 19:15:20 +0000172 int res;
Paul Bakker33b43f12013-08-20 11:48:36 +0200173 char *pwd = password;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000174
175 memset( &rsa, 0, sizeof( rsa_context ) );
176
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200177 if( strcmp( pwd, "NULL" ) == 0 )
178 pwd = NULL;
179
Paul Bakker33b43f12013-08-20 11:48:36 +0200180 res = x509parse_keyfile_rsa( &rsa, key_file, pwd );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000181
Paul Bakker33b43f12013-08-20 11:48:36 +0200182 TEST_ASSERT( res == result );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000183
184 if( res == 0 )
185 {
186 TEST_ASSERT( rsa_check_privkey( &rsa ) == 0 );
187 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000188
189 rsa_free( &rsa );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000190}
Paul Bakker33b43f12013-08-20 11:48:36 +0200191/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000192
Paul Bakker33b43f12013-08-20 11:48:36 +0200193/* BEGIN_CASE */
194void x509parse_public_keyfile_rsa( char *key_file, int result )
Paul Bakker36f1b192011-07-13 11:32:29 +0000195{
196 rsa_context rsa;
197 int res;
198
199 memset( &rsa, 0, sizeof( rsa_context ) );
200
Paul Bakker33b43f12013-08-20 11:48:36 +0200201 res = x509parse_public_keyfile_rsa( &rsa, key_file );
Paul Bakker36f1b192011-07-13 11:32:29 +0000202
Paul Bakker33b43f12013-08-20 11:48:36 +0200203 TEST_ASSERT( res == result );
Paul Bakker36f1b192011-07-13 11:32:29 +0000204
205 if( res == 0 )
206 {
207 TEST_ASSERT( rsa_check_pubkey( &rsa ) == 0 );
208 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000209
210 rsa_free( &rsa );
Paul Bakker36f1b192011-07-13 11:32:29 +0000211}
Paul Bakker33b43f12013-08-20 11:48:36 +0200212/* END_CASE */
Paul Bakker36f1b192011-07-13 11:32:29 +0000213
Paul Bakker33b43f12013-08-20 11:48:36 +0200214/* BEGIN_CASE */
215void x509parse_public_keyfile_ec( char *key_file, int result )
Manuel Pégourié-Gonnard1bc69312013-06-27 15:33:04 +0200216{
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200217 pk_context ctx;
Manuel Pégourié-Gonnard1bc69312013-06-27 15:33:04 +0200218 int res;
219
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200220 pk_init( &ctx );
Manuel Pégourié-Gonnard1bc69312013-06-27 15:33:04 +0200221
Paul Bakker33b43f12013-08-20 11:48:36 +0200222 res = x509parse_public_keyfile( &ctx, key_file );
Manuel Pégourié-Gonnard1bc69312013-06-27 15:33:04 +0200223
Paul Bakker33b43f12013-08-20 11:48:36 +0200224 TEST_ASSERT( res == result );
Manuel Pégourié-Gonnard1bc69312013-06-27 15:33:04 +0200225
226 if( res == 0 )
227 {
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200228 ecp_keypair *eckey;
229 TEST_ASSERT( ctx.type == POLARSSL_PK_ECKEY );
230 eckey = (ecp_keypair *) ctx.data;
231 TEST_ASSERT( ecp_check_pubkey( &eckey->grp, &eckey->Q ) == 0 );
Manuel Pégourié-Gonnard1bc69312013-06-27 15:33:04 +0200232 }
233
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200234 pk_free( &ctx );
Manuel Pégourié-Gonnard1bc69312013-06-27 15:33:04 +0200235}
Paul Bakker33b43f12013-08-20 11:48:36 +0200236/* END_CASE */
Manuel Pégourié-Gonnard1bc69312013-06-27 15:33:04 +0200237
Paul Bakker33b43f12013-08-20 11:48:36 +0200238/* BEGIN_CASE */
239void x509parse_keyfile_ec( char *key_file, char *password, int result )
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200240{
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200241 pk_context ctx;
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200242 int res;
243
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200244 pk_init( &ctx );
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200245
Paul Bakker33b43f12013-08-20 11:48:36 +0200246 res = x509parse_keyfile( &ctx, key_file, password );
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200247
Paul Bakker33b43f12013-08-20 11:48:36 +0200248 TEST_ASSERT( res == result );
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200249
250 if( res == 0 )
251 {
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200252 ecp_keypair *eckey;
253 TEST_ASSERT( ctx.type == POLARSSL_PK_ECKEY );
254 eckey = (ecp_keypair *) ctx.data;
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +0200255 TEST_ASSERT( ecp_check_privkey( &eckey->grp, &eckey->d ) == 0 );
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200256 }
257
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200258 pk_free( &ctx );
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200259}
Paul Bakker33b43f12013-08-20 11:48:36 +0200260/* END_CASE */
Manuel Pégourié-Gonnard2b9252c2013-07-03 12:13:56 +0200261
Paul Bakker33b43f12013-08-20 11:48:36 +0200262/* BEGIN_CASE */
263void x509parse_crt( char *crt_data, char *result_str, int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000264{
265 x509_cert crt;
266 unsigned char buf[2000];
267 unsigned char output[2000];
268 int data_len, res;
269
270 memset( &crt, 0, sizeof( x509_cert ) );
271 memset( buf, 0, 2000 );
272 memset( output, 0, 2000 );
273
Paul Bakker33b43f12013-08-20 11:48:36 +0200274 data_len = unhexify( buf, crt_data );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000275
Paul Bakker33b43f12013-08-20 11:48:36 +0200276 TEST_ASSERT( x509parse_crt( &crt, buf, data_len ) == ( result ) );
277 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000278 {
279 res = x509parse_cert_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200280
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000281 TEST_ASSERT( res != -1 );
282 TEST_ASSERT( res != -2 );
283
Paul Bakker33b43f12013-08-20 11:48:36 +0200284 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000285 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000286
287 x509_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000288}
Paul Bakker33b43f12013-08-20 11:48:36 +0200289/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000290
Paul Bakker33b43f12013-08-20 11:48:36 +0200291/* BEGIN_CASE */
292void x509parse_crl( char *crl_data, char *result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000293{
294 x509_crl crl;
295 unsigned char buf[2000];
296 unsigned char output[2000];
297 int data_len, res;
298
299 memset( &crl, 0, sizeof( x509_crl ) );
300 memset( buf, 0, 2000 );
301 memset( output, 0, 2000 );
302
Paul Bakker33b43f12013-08-20 11:48:36 +0200303 data_len = unhexify( buf, crl_data );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000304
Paul Bakker33b43f12013-08-20 11:48:36 +0200305 TEST_ASSERT( x509parse_crl( &crl, buf, data_len ) == ( result ) );
306 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000307 {
308 res = x509parse_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200309
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000310 TEST_ASSERT( res != -1 );
311 TEST_ASSERT( res != -2 );
312
Paul Bakker33b43f12013-08-20 11:48:36 +0200313 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000314 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000315
316 x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000317}
Paul Bakker33b43f12013-08-20 11:48:36 +0200318/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000319
Paul Bakker33b43f12013-08-20 11:48:36 +0200320/* BEGIN_CASE */
321void x509parse_key_rsa( char *key_data, char *result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000322{
323 rsa_context rsa;
324 unsigned char buf[2000];
325 unsigned char output[2000];
Paul Bakkereaf90d92011-07-13 14:21:52 +0000326 int data_len;
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200327 ((void) result_str);
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000328
329 memset( &rsa, 0, sizeof( rsa_context ) );
330 memset( buf, 0, 2000 );
331 memset( output, 0, 2000 );
332
Paul Bakker33b43f12013-08-20 11:48:36 +0200333 data_len = unhexify( buf, key_data );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000334
Paul Bakker33b43f12013-08-20 11:48:36 +0200335 TEST_ASSERT( x509parse_key_rsa( &rsa, buf, data_len, NULL, 0 ) == ( result ) );
336 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000337 {
338 TEST_ASSERT( 1 );
339 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000340
341 rsa_free( &rsa );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000342}
Paul Bakker33b43f12013-08-20 11:48:36 +0200343/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000344
Paul Bakker33b43f12013-08-20 11:48:36 +0200345/* BEGIN_CASE */
346void x509_selftest()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000347{
348 TEST_ASSERT( x509_self_test( 0 ) == 0 );
349}
Paul Bakker33b43f12013-08-20 11:48:36 +0200350/* END_CASE */