2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2013-09-16 13:49:26 +02:00
|
|
|
#include <polarssl/x509_crt.h>
|
|
|
|
#include <polarssl/x509_crl.h>
|
2014-01-24 17:34:26 +01:00
|
|
|
#include <polarssl/x509_csr.h>
|
2011-02-12 15:30:57 +01:00
|
|
|
#include <polarssl/pem.h>
|
2013-04-07 22:00:46 +02:00
|
|
|
#include <polarssl/oid.h>
|
2011-01-13 18:54:59 +01:00
|
|
|
|
2013-09-18 14:13:26 +02:00
|
|
|
int verify_none( void *data, x509_crt *crt, int certificate_depth, int *flags )
|
2011-01-13 18:54:59 +01:00
|
|
|
{
|
2011-01-18 17:31:52 +01:00
|
|
|
((void) data);
|
|
|
|
((void) crt);
|
|
|
|
((void) certificate_depth);
|
2012-09-28 09:10:55 +02:00
|
|
|
*flags |= BADCERT_OTHER;
|
2013-09-18 13:46:23 +02:00
|
|
|
|
2012-09-28 09:10:55 +02:00
|
|
|
return 0;
|
2011-01-13 18:54:59 +01:00
|
|
|
}
|
|
|
|
|
2013-09-18 14:13:26 +02:00
|
|
|
int verify_all( void *data, x509_crt *crt, int certificate_depth, int *flags )
|
2011-01-13 18:54:59 +01:00
|
|
|
{
|
2011-01-18 17:31:52 +01:00
|
|
|
((void) data);
|
|
|
|
((void) crt);
|
|
|
|
((void) certificate_depth);
|
2012-09-28 09:10:55 +02:00
|
|
|
*flags = 0;
|
2011-01-18 17:31:52 +01:00
|
|
|
|
2011-01-13 18:54:59 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2013-09-16 13:49:26 +02:00
|
|
|
* depends_on:POLARSSL_BIGNUM_C
|
2013-08-20 11:48:36 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2011-05-26 15:16:06 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
|
2013-08-20 11:48:36 +02:00
|
|
|
void x509_cert_info( char *crt_file, char *result_str )
|
2009-07-11 00:38:58 +02:00
|
|
|
{
|
2013-09-18 14:13:26 +02:00
|
|
|
x509_crt crt;
|
2009-07-11 00:38:58 +02:00
|
|
|
char buf[2000];
|
2009-07-11 21:15:20 +02:00
|
|
|
int res;
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2013-09-18 11:58:25 +02:00
|
|
|
x509_crt_init( &crt );
|
2009-07-11 00:38:58 +02:00
|
|
|
memset( buf, 0, 2000 );
|
|
|
|
|
2013-09-18 13:46:23 +02:00
|
|
|
TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
|
|
|
|
res = x509_crt_info( buf, 2000, "", &crt );
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( &crt );
|
2012-02-11 19:43:20 +01:00
|
|
|
|
2009-07-11 00:38:58 +02:00
|
|
|
TEST_ASSERT( res != -1 );
|
|
|
|
TEST_ASSERT( res != -2 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcmp( buf, result_str ) == 0 );
|
2009-07-11 00:38:58 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRL_PARSE_C */
|
2013-08-20 11:48:36 +02:00
|
|
|
void x509_crl_info( char *crl_file, char *result_str )
|
2009-07-11 00:38:58 +02:00
|
|
|
{
|
|
|
|
x509_crl crl;
|
|
|
|
char buf[2000];
|
2009-07-11 21:15:20 +02:00
|
|
|
int res;
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2013-09-18 11:58:25 +02:00
|
|
|
x509_crl_init( &crl );
|
2009-07-11 00:38:58 +02:00
|
|
|
memset( buf, 0, 2000 );
|
|
|
|
|
2013-09-18 13:46:23 +02:00
|
|
|
TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == 0 );
|
|
|
|
res = x509_crl_info( buf, 2000, "", &crl );
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2012-02-11 19:43:20 +01:00
|
|
|
x509_crl_free( &crl );
|
|
|
|
|
2009-07-11 00:38:58 +02:00
|
|
|
TEST_ASSERT( res != -1 );
|
|
|
|
TEST_ASSERT( res != -2 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcmp( buf, result_str ) == 0 );
|
2009-07-11 00:38:58 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2014-01-24 17:34:26 +01:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CSR_PARSE_C */
|
|
|
|
void x509_csr_info( char *csr_file, char *result_str )
|
|
|
|
{
|
|
|
|
x509_csr csr;
|
|
|
|
char buf[2000];
|
|
|
|
int res;
|
|
|
|
|
|
|
|
x509_csr_init( &csr );
|
|
|
|
memset( buf, 0, 2000 );
|
|
|
|
|
|
|
|
TEST_ASSERT( x509_csr_parse_file( &csr, csr_file ) == 0 );
|
|
|
|
res = x509_csr_info( buf, 2000, "", &csr );
|
|
|
|
|
|
|
|
x509_csr_free( &csr );
|
|
|
|
|
|
|
|
TEST_ASSERT( res != -1 );
|
|
|
|
TEST_ASSERT( res != -2 );
|
|
|
|
|
|
|
|
TEST_ASSERT( strcmp( buf, result_str ) == 0 );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-09-23 12:20:02 +02:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C:POLARSSL_X509_CRL_PARSE_C */
|
2013-08-20 11:48:36 +02:00
|
|
|
void x509_verify( char *crt_file, char *ca_file, char *crl_file,
|
|
|
|
char *cn_name_str, int result, int flags_result,
|
|
|
|
char *verify_callback )
|
2009-07-11 00:38:58 +02:00
|
|
|
{
|
2013-09-18 14:13:26 +02:00
|
|
|
x509_crt crt;
|
|
|
|
x509_crt ca;
|
2009-07-11 00:38:58 +02:00
|
|
|
x509_crl crl;
|
|
|
|
int flags = 0;
|
2009-07-11 21:15:20 +02:00
|
|
|
int res;
|
2013-09-18 14:13:26 +02:00
|
|
|
int (*f_vrfy)(void *, x509_crt *, int, int *) = NULL;
|
2013-08-16 13:38:47 +02:00
|
|
|
char * cn_name = NULL;
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2013-09-18 11:58:25 +02:00
|
|
|
x509_crt_init( &crt );
|
|
|
|
x509_crt_init( &ca );
|
|
|
|
x509_crl_init( &crl );
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( strcmp( cn_name_str, "NULL" ) != 0 )
|
|
|
|
cn_name = cn_name_str;
|
2013-08-16 13:38:47 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( strcmp( verify_callback, "NULL" ) == 0 )
|
2013-08-16 13:38:47 +02:00
|
|
|
f_vrfy = NULL;
|
2013-08-20 11:48:36 +02:00
|
|
|
else if( strcmp( verify_callback, "verify_none" ) == 0 )
|
2013-08-16 13:38:47 +02:00
|
|
|
f_vrfy = verify_none;
|
2013-08-20 11:48:36 +02:00
|
|
|
else if( strcmp( verify_callback, "verify_all" ) == 0 )
|
2013-08-16 13:38:47 +02:00
|
|
|
f_vrfy = verify_all;
|
|
|
|
else
|
|
|
|
TEST_ASSERT( "No known verify callback selected" == 0 );
|
|
|
|
|
2013-09-18 13:46:23 +02:00
|
|
|
TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
|
|
|
|
TEST_ASSERT( x509_crt_parse_file( &ca, ca_file ) == 0 );
|
|
|
|
TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == 0 );
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2013-09-18 13:46:23 +02:00
|
|
|
res = x509_crt_verify( &crt, &ca, &crl, cn_name, &flags, f_vrfy, NULL );
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( &crt );
|
|
|
|
x509_crt_free( &ca );
|
2012-02-11 19:43:20 +01:00
|
|
|
x509_crl_free( &crl );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( res == ( result ) );
|
|
|
|
TEST_ASSERT( flags == ( flags_result ) );
|
2009-07-11 00:38:58 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_USE_C */
|
2013-08-20 11:48:36 +02:00
|
|
|
void x509_dn_gets( char *crt_file, char *entity, char *result_str )
|
2009-07-11 00:38:58 +02:00
|
|
|
{
|
2013-09-18 14:13:26 +02:00
|
|
|
x509_crt crt;
|
2009-07-11 00:38:58 +02:00
|
|
|
char buf[2000];
|
2013-08-16 13:38:47 +02:00
|
|
|
int res = 0;
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2013-09-18 11:58:25 +02:00
|
|
|
x509_crt_init( &crt );
|
2009-07-11 00:38:58 +02:00
|
|
|
memset( buf, 0, 2000 );
|
|
|
|
|
2013-09-18 13:46:23 +02:00
|
|
|
TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
|
2013-08-20 11:48:36 +02:00
|
|
|
if( strcmp( entity, "subject" ) == 0 )
|
2013-09-18 11:11:02 +02:00
|
|
|
res = x509_dn_gets( buf, 2000, &crt.subject );
|
2013-08-20 11:48:36 +02:00
|
|
|
else if( strcmp( entity, "issuer" ) == 0 )
|
2013-09-18 11:11:02 +02:00
|
|
|
res = x509_dn_gets( buf, 2000, &crt.issuer );
|
2013-08-16 13:38:47 +02:00
|
|
|
else
|
|
|
|
TEST_ASSERT( "Unknown entity" == 0 );
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( &crt );
|
2012-02-11 19:43:20 +01:00
|
|
|
|
2009-07-11 00:38:58 +02:00
|
|
|
TEST_ASSERT( res != -1 );
|
|
|
|
TEST_ASSERT( res != -2 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcmp( buf, result_str ) == 0 );
|
2009-07-11 00:38:58 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_USE_C */
|
2013-08-20 11:48:36 +02:00
|
|
|
void x509_time_expired( char *crt_file, char *entity, int result )
|
2009-07-11 00:38:58 +02:00
|
|
|
{
|
2013-09-18 14:13:26 +02:00
|
|
|
x509_crt crt;
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2013-09-18 11:58:25 +02:00
|
|
|
x509_crt_init( &crt );
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2013-09-18 13:46:23 +02:00
|
|
|
TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
|
2013-08-16 13:38:47 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( strcmp( entity, "valid_from" ) == 0 )
|
2013-09-18 11:11:02 +02:00
|
|
|
TEST_ASSERT( x509_time_expired( &crt.valid_from ) == result );
|
2013-08-20 11:48:36 +02:00
|
|
|
else if( strcmp( entity, "valid_to" ) == 0 )
|
2013-09-18 11:11:02 +02:00
|
|
|
TEST_ASSERT( x509_time_expired( &crt.valid_to ) == result );
|
2013-08-16 13:38:47 +02:00
|
|
|
else
|
|
|
|
TEST_ASSERT( "Unknown entity" == 0 );
|
2012-02-11 19:43:20 +01:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( &crt );
|
2009-07-11 00:38:58 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C */
|
2013-08-20 11:48:36 +02:00
|
|
|
void x509parse_crt( char *crt_data, char *result_str, int result )
|
2009-07-19 21:36:15 +02:00
|
|
|
{
|
2013-09-18 14:13:26 +02:00
|
|
|
x509_crt crt;
|
2009-07-19 21:36:15 +02:00
|
|
|
unsigned char buf[2000];
|
|
|
|
unsigned char output[2000];
|
|
|
|
int data_len, res;
|
|
|
|
|
2013-09-18 11:58:25 +02:00
|
|
|
x509_crt_init( &crt );
|
2009-07-19 21:36:15 +02:00
|
|
|
memset( buf, 0, 2000 );
|
|
|
|
memset( output, 0, 2000 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
data_len = unhexify( buf, crt_data );
|
2009-07-19 21:36:15 +02:00
|
|
|
|
2013-09-18 13:46:23 +02:00
|
|
|
TEST_ASSERT( x509_crt_parse( &crt, buf, data_len ) == ( result ) );
|
2013-08-20 11:48:36 +02:00
|
|
|
if( ( result ) == 0 )
|
2009-07-19 21:36:15 +02:00
|
|
|
{
|
2013-09-18 13:46:23 +02:00
|
|
|
res = x509_crt_info( (char *) output, 2000, "", &crt );
|
2013-08-20 11:48:36 +02:00
|
|
|
|
2009-07-19 21:36:15 +02:00
|
|
|
TEST_ASSERT( res != -1 );
|
|
|
|
TEST_ASSERT( res != -2 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
|
2009-07-19 21:36:15 +02:00
|
|
|
}
|
2012-02-11 19:43:20 +01:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
x509_crt_free( &crt );
|
2009-07-19 21:36:15 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-19 21:36:15 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_X509_CRL_PARSE_C */
|
2013-08-20 11:48:36 +02:00
|
|
|
void x509parse_crl( char *crl_data, char *result_str, int result )
|
2009-07-20 22:35:41 +02:00
|
|
|
{
|
|
|
|
x509_crl crl;
|
|
|
|
unsigned char buf[2000];
|
|
|
|
unsigned char output[2000];
|
|
|
|
int data_len, res;
|
|
|
|
|
2013-09-18 11:58:25 +02:00
|
|
|
x509_crl_init( &crl );
|
2009-07-20 22:35:41 +02:00
|
|
|
memset( buf, 0, 2000 );
|
|
|
|
memset( output, 0, 2000 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
data_len = unhexify( buf, crl_data );
|
2009-07-20 22:35:41 +02:00
|
|
|
|
2013-09-18 13:46:23 +02:00
|
|
|
TEST_ASSERT( x509_crl_parse( &crl, buf, data_len ) == ( result ) );
|
2013-08-20 11:48:36 +02:00
|
|
|
if( ( result ) == 0 )
|
2009-07-20 22:35:41 +02:00
|
|
|
{
|
2013-09-18 13:46:23 +02:00
|
|
|
res = x509_crl_info( (char *) output, 2000, "", &crl );
|
2013-08-20 11:48:36 +02:00
|
|
|
|
2009-07-20 22:35:41 +02:00
|
|
|
TEST_ASSERT( res != -1 );
|
|
|
|
TEST_ASSERT( res != -2 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
|
2009-07-20 22:35:41 +02:00
|
|
|
}
|
2012-02-11 19:43:20 +01:00
|
|
|
|
|
|
|
x509_crl_free( &crl );
|
2009-07-20 22:35:41 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-20 22:35:41 +02:00
|
|
|
|
2013-11-26 16:43:39 +01:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */
|
|
|
|
void x509_crt_parse_path( char *crt_path, int ret, int nb_crt )
|
|
|
|
{
|
|
|
|
x509_crt chain, *cur;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
x509_crt_init( &chain );
|
|
|
|
|
|
|
|
TEST_ASSERT( x509_crt_parse_path( &chain, crt_path ) == ret );
|
|
|
|
|
|
|
|
/* Check how many certs we got */
|
|
|
|
for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
|
|
|
|
if( cur->raw.p != NULL )
|
|
|
|
i++;
|
|
|
|
|
|
|
|
TEST_ASSERT( i == nb_crt );
|
|
|
|
|
2013-12-02 21:56:37 +01:00
|
|
|
x509_crt_free( &chain );
|
2013-11-26 16:43:39 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-10-10 12:48:03 +02:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C:POLARSSL_SELF_TEST */
|
2013-08-20 11:48:36 +02:00
|
|
|
void x509_selftest()
|
2009-07-11 00:38:58 +02:00
|
|
|
{
|
|
|
|
TEST_ASSERT( x509_self_test( 0 ) == 0 );
|
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|