blob: 082dd33c6201fccee0df9a80e58640fd466e2298 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002#include <polarssl/x509_crt.h>
3#include <polarssl/x509_crl.h>
Paul Bakker96743fc2011-02-12 14:30:57 +00004#include <polarssl/pem.h>
Paul Bakkerc70b9822013-04-07 22:00:46 +02005#include <polarssl/oid.h>
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006
Paul Bakker915275b2012-09-28 07:10:55 +00007int verify_none( void *data, x509_cert *crt, int certificate_depth, int *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008{
Paul Bakker5a624082011-01-18 16:31:52 +00009 ((void) data);
10 ((void) crt);
11 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000012 *flags |= BADCERT_OTHER;
13
14 return 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +000015}
16
Paul Bakker915275b2012-09-28 07:10:55 +000017int verify_all( void *data, x509_cert *crt, int certificate_depth, int *flags )
Paul Bakkerb63b0af2011-01-13 17:54:59 +000018{
Paul Bakker5a624082011-01-18 16:31:52 +000019 ((void) data);
20 ((void) crt);
21 ((void) certificate_depth);
Paul Bakker915275b2012-09-28 07:10:55 +000022 *flags = 0;
Paul Bakker5a624082011-01-18 16:31:52 +000023
Paul Bakkerb63b0af2011-01-13 17:54:59 +000024 return 0;
25}
26
Paul Bakker33b43f12013-08-20 11:48:36 +020027/* END_HEADER */
Paul Bakker37940d9f2009-07-10 22:38:58 +000028
Paul Bakker33b43f12013-08-20 11:48:36 +020029/* BEGIN_DEPENDENCIES
Paul Bakker7c6b2c32013-09-16 13:49:26 +020030 * depends_on:POLARSSL_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +020031 * END_DEPENDENCIES
32 */
Paul Bakker5690efc2011-05-26 13:16:06 +000033
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +020035void x509_cert_info( char *crt_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +000036{
37 x509_cert crt;
38 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +000039 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +000040
Paul Bakker369d2eb2013-09-18 11:58:25 +020041 x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +000042 memset( buf, 0, 2000 );
43
Paul Bakker33b43f12013-08-20 11:48:36 +020044 TEST_ASSERT( x509parse_crtfile( &crt, crt_file ) == 0 );
Paul Bakker69998dd2009-07-11 19:15:20 +000045 res = x509parse_cert_info( buf, 2000, "", &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +000046
Paul Bakker7c6b2c32013-09-16 13:49:26 +020047 x509_crt_free( &crt );
Paul Bakkerb08e6842012-02-11 18:43:20 +000048
Paul Bakker37940d9f2009-07-10 22:38:58 +000049 TEST_ASSERT( res != -1 );
50 TEST_ASSERT( res != -2 );
51
Paul Bakker33b43f12013-08-20 11:48:36 +020052 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +000053}
Paul Bakker33b43f12013-08-20 11:48:36 +020054/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +000055
Paul Bakker7c6b2c32013-09-16 13:49:26 +020056/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +020057void x509_crl_info( char *crl_file, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +000058{
59 x509_crl crl;
60 char buf[2000];
Paul Bakker69998dd2009-07-11 19:15:20 +000061 int res;
Paul Bakker37940d9f2009-07-10 22:38:58 +000062
Paul Bakker369d2eb2013-09-18 11:58:25 +020063 x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +000064 memset( buf, 0, 2000 );
65
Paul Bakker33b43f12013-08-20 11:48:36 +020066 TEST_ASSERT( x509parse_crlfile( &crl, crl_file ) == 0 );
Paul Bakker69998dd2009-07-11 19:15:20 +000067 res = x509parse_crl_info( buf, 2000, "", &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +000068
Paul Bakkerb08e6842012-02-11 18:43:20 +000069 x509_crl_free( &crl );
70
Paul Bakker37940d9f2009-07-10 22:38:58 +000071 TEST_ASSERT( res != -1 );
72 TEST_ASSERT( res != -2 );
73
Paul Bakker33b43f12013-08-20 11:48:36 +020074 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +000075}
Paul Bakker33b43f12013-08-20 11:48:36 +020076/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +000077
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +020079void x509_verify( char *crt_file, char *ca_file, char *crl_file,
80 char *cn_name_str, int result, int flags_result,
81 char *verify_callback )
Paul Bakker37940d9f2009-07-10 22:38:58 +000082{
83 x509_cert crt;
84 x509_cert ca;
85 x509_crl crl;
86 int flags = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +000087 int res;
Paul Bakkerdbd443d2013-08-16 13:38:47 +020088 int (*f_vrfy)(void *, x509_cert *, int, int *) = NULL;
89 char * cn_name = NULL;
Paul Bakker37940d9f2009-07-10 22:38:58 +000090
Paul Bakker369d2eb2013-09-18 11:58:25 +020091 x509_crt_init( &crt );
92 x509_crt_init( &ca );
93 x509_crl_init( &crl );
Paul Bakker37940d9f2009-07-10 22:38:58 +000094
Paul Bakker33b43f12013-08-20 11:48:36 +020095 if( strcmp( cn_name_str, "NULL" ) != 0 )
96 cn_name = cn_name_str;
Paul Bakkerdbd443d2013-08-16 13:38:47 +020097
Paul Bakker33b43f12013-08-20 11:48:36 +020098 if( strcmp( verify_callback, "NULL" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +020099 f_vrfy = NULL;
Paul Bakker33b43f12013-08-20 11:48:36 +0200100 else if( strcmp( verify_callback, "verify_none" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200101 f_vrfy = verify_none;
Paul Bakker33b43f12013-08-20 11:48:36 +0200102 else if( strcmp( verify_callback, "verify_all" ) == 0 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200103 f_vrfy = verify_all;
104 else
105 TEST_ASSERT( "No known verify callback selected" == 0 );
106
Paul Bakker33b43f12013-08-20 11:48:36 +0200107 TEST_ASSERT( x509parse_crtfile( &crt, crt_file ) == 0 );
108 TEST_ASSERT( x509parse_crtfile( &ca, ca_file ) == 0 );
109 TEST_ASSERT( x509parse_crlfile( &crl, crl_file ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000110
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200111 res = x509parse_verify( &crt, &ca, &crl, cn_name, &flags, f_vrfy, NULL );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000112
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200113 x509_crt_free( &crt );
114 x509_crt_free( &ca );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000115 x509_crl_free( &crl );
116
Paul Bakker33b43f12013-08-20 11:48:36 +0200117 TEST_ASSERT( res == ( result ) );
118 TEST_ASSERT( flags == ( flags_result ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000119}
Paul Bakker33b43f12013-08-20 11:48:36 +0200120/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000121
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200122/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_USE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200123void x509_dn_gets( char *crt_file, char *entity, char *result_str )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000124{
125 x509_cert crt;
126 char buf[2000];
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200127 int res = 0;
Paul Bakker37940d9f2009-07-10 22:38:58 +0000128
Paul Bakker369d2eb2013-09-18 11:58:25 +0200129 x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000130 memset( buf, 0, 2000 );
131
Paul Bakker33b43f12013-08-20 11:48:36 +0200132 TEST_ASSERT( x509parse_crtfile( &crt, crt_file ) == 0 );
133 if( strcmp( entity, "subject" ) == 0 )
Paul Bakker86d0c192013-09-18 11:11:02 +0200134 res = x509_dn_gets( buf, 2000, &crt.subject );
Paul Bakker33b43f12013-08-20 11:48:36 +0200135 else if( strcmp( entity, "issuer" ) == 0 )
Paul Bakker86d0c192013-09-18 11:11:02 +0200136 res = x509_dn_gets( buf, 2000, &crt.issuer );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200137 else
138 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000139
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200140 x509_crt_free( &crt );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000141
Paul Bakker37940d9f2009-07-10 22:38:58 +0000142 TEST_ASSERT( res != -1 );
143 TEST_ASSERT( res != -2 );
144
Paul Bakker33b43f12013-08-20 11:48:36 +0200145 TEST_ASSERT( strcmp( buf, result_str ) == 0 );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000146}
Paul Bakker33b43f12013-08-20 11:48:36 +0200147/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000148
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200149/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_USE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200150void x509_time_expired( char *crt_file, char *entity, int result )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000151{
152 x509_cert crt;
153
Paul Bakker369d2eb2013-09-18 11:58:25 +0200154 x509_crt_init( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000155
Paul Bakker33b43f12013-08-20 11:48:36 +0200156 TEST_ASSERT( x509parse_crtfile( &crt, crt_file ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200157
Paul Bakker33b43f12013-08-20 11:48:36 +0200158 if( strcmp( entity, "valid_from" ) == 0 )
Paul Bakker86d0c192013-09-18 11:11:02 +0200159 TEST_ASSERT( x509_time_expired( &crt.valid_from ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200160 else if( strcmp( entity, "valid_to" ) == 0 )
Paul Bakker86d0c192013-09-18 11:11:02 +0200161 TEST_ASSERT( x509_time_expired( &crt.valid_to ) == result );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200162 else
163 TEST_ASSERT( "Unknown entity" == 0 );
Paul Bakkerb08e6842012-02-11 18:43:20 +0000164
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200165 x509_crt_free( &crt );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000166}
Paul Bakker33b43f12013-08-20 11:48:36 +0200167/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000168
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200169/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200170void x509parse_crt( char *crt_data, char *result_str, int result )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000171{
172 x509_cert crt;
173 unsigned char buf[2000];
174 unsigned char output[2000];
175 int data_len, res;
176
Paul Bakker369d2eb2013-09-18 11:58:25 +0200177 x509_crt_init( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000178 memset( buf, 0, 2000 );
179 memset( output, 0, 2000 );
180
Paul Bakker33b43f12013-08-20 11:48:36 +0200181 data_len = unhexify( buf, crt_data );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000182
Paul Bakker33b43f12013-08-20 11:48:36 +0200183 TEST_ASSERT( x509parse_crt( &crt, buf, data_len ) == ( result ) );
184 if( ( result ) == 0 )
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000185 {
186 res = x509parse_cert_info( (char *) output, 2000, "", &crt );
Paul Bakker33b43f12013-08-20 11:48:36 +0200187
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000188 TEST_ASSERT( res != -1 );
189 TEST_ASSERT( res != -2 );
190
Paul Bakker33b43f12013-08-20 11:48:36 +0200191 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000192 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000193
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200194 x509_crt_free( &crt );
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000195}
Paul Bakker33b43f12013-08-20 11:48:36 +0200196/* END_CASE */
Paul Bakkerb2c38f52009-07-19 19:36:15 +0000197
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200198/* BEGIN_CASE depends_on:POLARSSL_X509_CRL_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200199void x509parse_crl( char *crl_data, char *result_str, int result )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000200{
201 x509_crl crl;
202 unsigned char buf[2000];
203 unsigned char output[2000];
204 int data_len, res;
205
Paul Bakker369d2eb2013-09-18 11:58:25 +0200206 x509_crl_init( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000207 memset( buf, 0, 2000 );
208 memset( output, 0, 2000 );
209
Paul Bakker33b43f12013-08-20 11:48:36 +0200210 data_len = unhexify( buf, crl_data );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000211
Paul Bakker33b43f12013-08-20 11:48:36 +0200212 TEST_ASSERT( x509parse_crl( &crl, buf, data_len ) == ( result ) );
213 if( ( result ) == 0 )
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000214 {
215 res = x509parse_crl_info( (char *) output, 2000, "", &crl );
Paul Bakker33b43f12013-08-20 11:48:36 +0200216
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000217 TEST_ASSERT( res != -1 );
218 TEST_ASSERT( res != -2 );
219
Paul Bakker33b43f12013-08-20 11:48:36 +0200220 TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000221 }
Paul Bakkerb08e6842012-02-11 18:43:20 +0000222
223 x509_crl_free( &crl );
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000224}
Paul Bakker33b43f12013-08-20 11:48:36 +0200225/* END_CASE */
Paul Bakker6b0fa4f2009-07-20 20:35:41 +0000226
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200227/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200228void x509_selftest()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000229{
230 TEST_ASSERT( x509_self_test( 0 ) == 0 );
231}
Paul Bakker33b43f12013-08-20 11:48:36 +0200232/* END_CASE */