Nicer interface between PK and debug.

Finally get rid of pk_context.type member, too.
This commit is contained in:
Manuel Pégourié-Gonnard 2013-08-14 18:04:18 +02:00
parent b3d9187cea
commit c6ac8870d5
5 changed files with 113 additions and 28 deletions

View file

@ -225,6 +225,39 @@ void debug_print_mpi( const ssl_context *ssl, int level,
#endif /* POLARSSL_BIGNUM_C */
#if defined(POLARSSL_X509_PARSE_C)
static void debug_print_pk( const ssl_context *ssl, int level,
const char *file, int line,
const char *text, const pk_context *pk )
{
size_t i;
pk_debug_item items[POLARSSL_PK_DEBUG_MAX_ITEMS];
char name[16];
memset( items, 0, sizeof( items ) );
if( pk_debug( pk, items ) != 0 )
{
debug_print_msg( ssl, level, file, line, "invalid PK context" );
return;
}
for( i = 0; i < sizeof( items ); i++ )
{
if( items[i].type == POLARSSL_PK_DEBUG_NONE )
return;
snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
name[sizeof( name ) - 1] = '\0';
if( items[i].type == POLARSSL_PK_DEBUG_MPI )
debug_print_mpi( ssl, level, file, line, name, items[i].value );
else if( items[i].type == POLARSSL_PK_DEBUG_ECP )
debug_print_ecp( ssl, level, file, line, name, items[i].value );
else
debug_print_msg( ssl, level, file, line, "should not happen" );
}
}
void debug_print_crt( const ssl_context *ssl, int level,
const char *file, int line,
const char *text, const x509_cert *crt )
@ -250,25 +283,7 @@ void debug_print_crt( const ssl_context *ssl, int level,
str[maxlen] = '\0';
ssl->f_dbg( ssl->p_dbg, level, str );
#if defined(POLARSSL_RSA_C)
if( crt->pk.type == POLARSSL_PK_RSA )
{
debug_print_mpi( ssl, level, file, line,
"crt->rsa.N", &pk_rsa( crt->pk )->N );
debug_print_mpi( ssl, level, file, line,
"crt->rsa.E", &pk_rsa( crt->pk )->E );
} else
#endif /* POLARSSL_RSA_C */
#if defined(POLARSSL_ECP_C)
if( crt->pk.type == POLARSSL_PK_ECKEY ||
crt->pk.type == POLARSSL_PK_ECKEY_DH )
{
debug_print_ecp( ssl, level, file, line,
"crt->eckey.Q", &pk_ec( crt->pk )->Q );
} else
#endif /* POLARSSL_ECP_C */
debug_print_msg( ssl, level, file, line,
"crt->pk.type is not valid" );
debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
crt = crt->next;
}

View file

@ -56,7 +56,6 @@ void pk_init( pk_context *ctx )
return;
ctx->info = NULL;
ctx->type = POLARSSL_PK_NONE;
ctx->data = NULL;
}
@ -72,7 +71,6 @@ void pk_free( pk_context *ctx )
ctx->data = NULL;
ctx->info = NULL;
ctx->type = POLARSSL_PK_NONE;
}
/*
@ -107,11 +105,13 @@ int pk_set_type( pk_context *ctx, pk_type_t type )
{
const pk_info_t *info;
if( ctx->type == type )
return( 0 );
if( ctx->info != NULL )
{
if( ctx->info->type == type )
return 0;
if( ctx->type != POLARSSL_PK_NONE )
return( POLARSSL_ERR_PK_TYPE_MISMATCH );
}
if( ( info = pk_info_from_type( type ) ) == NULL )
return( POLARSSL_ERR_PK_TYPE_MISMATCH );
@ -119,7 +119,6 @@ int pk_set_type( pk_context *ctx, pk_type_t type )
if( ( ctx->data = info->ctx_alloc_func() ) == NULL )
return( POLARSSL_ERR_PK_MALLOC_FAILED );
ctx->type = type;
ctx->info = info;
return( 0 );
@ -160,3 +159,15 @@ size_t pk_get_size( const pk_context *ctx )
return( ctx->info->get_size( ctx->data ) );
}
/*
* Export debug information
*/
int pk_debug( const pk_context *ctx, pk_debug_item *items )
{
if( ctx == NULL || ctx->info == NULL )
return( POLARSSL_ERR_PK_TYPE_MISMATCH ); // TODO
ctx->info->debug_func( ctx->data, items );
return( 0 );
}

View file

@ -84,6 +84,19 @@ static void rsa_free_wrap( void *ctx )
polarssl_free( ctx );
}
static void rsa_debug( const void *ctx, pk_debug_item *items )
{
items->type = POLARSSL_PK_DEBUG_MPI;
items->name = "rsa.N";
items->value = &( ((rsa_context *) ctx)->N );
items++;
items->type = POLARSSL_PK_DEBUG_MPI;
items->name = "rsa.E";
items->value = &( ((rsa_context *) ctx)->E );
}
const pk_info_t rsa_info = {
POLARSSL_PK_RSA,
"RSA",
@ -92,6 +105,7 @@ const pk_info_t rsa_info = {
rsa_verify_wrap,
rsa_alloc_wrap,
rsa_free_wrap,
rsa_debug,
};
#endif /* POLARSSL_RSA_C */
@ -138,6 +152,7 @@ const pk_info_t ecdsa_info = {
ecdsa_verify_wrap,
ecdsa_alloc_wrap,
ecdsa_free_wrap,
NULL,
};
#endif /* POLARSSL_ECDSA_C */
@ -200,6 +215,13 @@ static void eckey_free_wrap( void *ctx )
polarssl_free( ctx );
}
static void eckey_debug( const void *ctx, pk_debug_item *items )
{
items->type = POLARSSL_PK_DEBUG_ECP;
items->name = "eckey.Q";
items->value = &( ((ecp_keypair *) ctx)->Q );
}
const pk_info_t eckey_info = {
POLARSSL_PK_ECKEY,
"EC",
@ -208,6 +230,7 @@ const pk_info_t eckey_info = {
eckey_verify_wrap,
eckey_alloc_wrap,
eckey_free_wrap,
eckey_debug,
};
/*
@ -240,5 +263,6 @@ const pk_info_t eckeydh_info = {
eckeydh_verify_wrap,
eckey_alloc_wrap, /* Same underlying key structure */
eckey_free_wrap, /* Same underlying key structure */
NULL,
};
#endif /* POLARSSL_ECP_C */