blob: fcacaefe2386c927a645a074b2848de544003ccd [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 Bakker428b9ba2013-09-15 15:20:37 +020033/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
Paul Bakker33b43f12013-08-20 11:48:36 +020034void 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 Bakker428b9ba2013-09-15 15:20:37 +020055/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
Paul Bakker33b43f12013-08-20 11:48:36 +020056void 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 Bakker428b9ba2013-09-15 15:20:37 +020077/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
Paul Bakker33b43f12013-08-20 11:48:36 +020078void 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 Bakker428b9ba2013-09-15 15:20:37 +0200121/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
Paul Bakker33b43f12013-08-20 11:48:36 +0200122void 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 Bakker428b9ba2013-09-15 15:20:37 +0200148/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
Paul Bakker33b43f12013-08-20 11:48:36 +0200149void 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_crt( char *crt_data, char *result_str, int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000170{
171 x509_cert crt;
172 unsigned char buf[2000];
173 unsigned char output[2000];
174 int data_len, res;
175
176 memset( &crt, 0, sizeof( x509_cert ) );
177 memset( buf, 0, 2000 );
178 memset( output, 0, 2000 );
179
Paul Bakker33b43f12013-08-20 11:48:36 +0200180 data_len = unhexify( buf, crt_data );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000181
Paul Bakker33b43f12013-08-20 11:48:36 +0200182 TEST_ASSERT( x509parse_crt( &crt, buf, data_len ) == ( result ) );
183 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000184 {
185 res = x509parse_cert_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200186
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000187 TEST_ASSERT( res != -1 );
188 TEST_ASSERT( res != -2 );
189
Paul Bakker33b43f12013-08-20 11:48:36 +0200190 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000191 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000192
193 x509_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000194}
Paul Bakker33b43f12013-08-20 11:48:36 +0200195/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000196
Paul Bakker33b43f12013-08-20 11:48:36 +0200197/* BEGIN_CASE */
198void x509parse_crl( char *crl_data, char *result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000199{
200 x509_crl crl;
201 unsigned char buf[2000];
202 unsigned char output[2000];
203 int data_len, res;
204
205 memset( &crl, 0, sizeof( x509_crl ) );
206 memset( buf, 0, 2000 );
207 memset( output, 0, 2000 );
208
Paul Bakker33b43f12013-08-20 11:48:36 +0200209 data_len = unhexify( buf, crl_data );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000210
Paul Bakker33b43f12013-08-20 11:48:36 +0200211 TEST_ASSERT( x509parse_crl( &crl, buf, data_len ) == ( result ) );
212 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000213 {
214 res = x509parse_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200215
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000216 TEST_ASSERT( res != -1 );
217 TEST_ASSERT( res != -2 );
218
Paul Bakker33b43f12013-08-20 11:48:36 +0200219 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000220 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000221
222 x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000223}
Paul Bakker33b43f12013-08-20 11:48:36 +0200224/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000225
Paul Bakker33b43f12013-08-20 11:48:36 +0200226/* BEGIN_CASE */
227void x509_selftest()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000228{
229 TEST_ASSERT( x509_self_test( 0 ) == 0 );
230}
Paul Bakker33b43f12013-08-20 11:48:36 +0200231/* END_CASE */