Add name and get_size() members in PK

This commit is contained in:
Manuel Pégourié-Gonnard 2013-08-12 19:45:32 +02:00
parent 835eb59c6a
commit f8c948a674
3 changed files with 58 additions and 14 deletions

View File

@ -89,6 +89,12 @@ typedef struct
/** Public key type */ /** Public key type */
pk_type_t type; pk_type_t type;
/** Type name */
const char *name;
/** Get key size in bits */
size_t (*get_size)( void * );
/** Tell if the context implements this type (eg ECKEY can do ECDSA) */ /** Tell if the context implements this type (eg ECKEY can do ECDSA) */
int (*can_do)( pk_type_t type ); int (*can_do)( pk_type_t type );

View File

@ -45,6 +45,11 @@ static int rsa_can_do( pk_type_t type )
return( type == POLARSSL_PK_RSA ); return( type == POLARSSL_PK_RSA );
} }
static size_t rsa_get_size( void * ctx )
{
return( mpi_size( &((rsa_context *) ctx)->N ) * 8 );
}
static int rsa_verify_wrap( void *ctx, static int rsa_verify_wrap( void *ctx,
const unsigned char *hash, const md_info_t *md_info, const unsigned char *hash, const md_info_t *md_info,
const unsigned char *sig, size_t sig_len ) const unsigned char *sig, size_t sig_len )
@ -57,6 +62,8 @@ static int rsa_verify_wrap( void *ctx,
const pk_info_t rsa_info = { const pk_info_t rsa_info = {
POLARSSL_PK_RSA, POLARSSL_PK_RSA,
"RSA",
rsa_get_size,
rsa_can_do, rsa_can_do,
rsa_verify_wrap, rsa_verify_wrap,
}; };
@ -68,6 +75,11 @@ int ecdsa_can_do( pk_type_t type )
return( type == POLARSSL_PK_ECDSA ); return( type == POLARSSL_PK_ECDSA );
} }
static size_t ecdsa_get_size( void *ctx )
{
return( ((ecdsa_context *) ctx)->grp.pbits );
}
int ecdsa_verify_wrap( void *ctx, int ecdsa_verify_wrap( void *ctx,
const unsigned char *hash, const md_info_t *md_info, const unsigned char *hash, const md_info_t *md_info,
const unsigned char *sig, size_t sig_len ) const unsigned char *sig, size_t sig_len )
@ -78,6 +90,8 @@ int ecdsa_verify_wrap( void *ctx,
const pk_info_t ecdsa_info = { const pk_info_t ecdsa_info = {
POLARSSL_PK_ECDSA, POLARSSL_PK_ECDSA,
"ECDSA",
ecdsa_get_size,
ecdsa_can_do, ecdsa_can_do,
ecdsa_verify_wrap, ecdsa_verify_wrap,
}; };
@ -94,6 +108,11 @@ static int eckey_can_do( pk_type_t type )
type == POLARSSL_PK_ECDSA ); type == POLARSSL_PK_ECDSA );
} }
static size_t eckey_get_size( void *ctx )
{
return( ((ecp_keypair *) ctx)->grp.pbits );
}
static int eckey_verify_wrap( void *ctx, static int eckey_verify_wrap( void *ctx,
const unsigned char *hash, const md_info_t *md_info, const unsigned char *hash, const md_info_t *md_info,
const unsigned char *sig, size_t sig_len ) const unsigned char *sig, size_t sig_len )
@ -123,6 +142,8 @@ static int eckey_verify_wrap( void *ctx,
const pk_info_t eckey_info = { const pk_info_t eckey_info = {
POLARSSL_PK_ECKEY, POLARSSL_PK_ECKEY,
"EC",
eckey_get_size,
eckey_can_do, eckey_can_do,
eckey_verify_wrap, eckey_verify_wrap,
}; };
@ -151,6 +172,8 @@ static int eckeydh_verify_wrap( void *ctx,
const pk_info_t eckeydh_info = { const pk_info_t eckeydh_info = {
POLARSSL_PK_ECKEY_DH, POLARSSL_PK_ECKEY_DH,
"EC_DH",
eckey_get_size, /* Same underlying key structure */
eckeydh_can_do, eckeydh_can_do,
eckeydh_verify_wrap, eckeydh_verify_wrap,
}; };

View File

@ -3021,9 +3021,29 @@ int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
return( (int) ( size - n ) ); return( (int) ( size - n ) );
} }
/*
* Helper for writing "RSA key size", "EC key size", etc
*/
static int x509_key_size_helper( char *buf, size_t size, const char *name )
{
char *p = buf;
size_t n = size;
int ret;
if( strlen( name ) + sizeof( " key size" ) > size )
return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;
ret = snprintf( p, n, "%s key size", name );
SAFE_SNPRINTF();
return( 0 );
}
/* /*
* Return an informational string about the certificate. * Return an informational string about the certificate.
*/ */
#define BEFORE_COLON 14
#define BC "14"
int x509parse_cert_info( char *buf, size_t size, const char *prefix, int x509parse_cert_info( char *buf, size_t size, const char *prefix,
const x509_cert *crt ) const x509_cert *crt )
{ {
@ -3031,6 +3051,7 @@ int x509parse_cert_info( char *buf, size_t size, const char *prefix,
size_t n; size_t n;
char *p; char *p;
const char *desc = NULL; const char *desc = NULL;
char key_size_str[BEFORE_COLON];
p = buf; p = buf;
n = size; n = size;
@ -3079,20 +3100,14 @@ int x509parse_cert_info( char *buf, size_t size, const char *prefix,
ret = snprintf( p, n, desc ); ret = snprintf( p, n, desc );
SAFE_SNPRINTF(); SAFE_SNPRINTF();
#if defined(POLARSSL_RSA_C) if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
if( crt->pk.type == POLARSSL_PK_RSA ) crt->pk.info->name ) ) != 0 )
ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix, {
(int) pk_rsa( crt->pk )->N.n * (int) sizeof( t_uint ) * 8 ); return( ret );
else }
#endif /* POLARSSL_RSA_C */
#if defined(POLARSSL_ECP_C) ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
if( crt->pk.type == POLARSSL_PK_ECKEY || (int) crt->pk.info->get_size( crt->pk.data ) );
crt->pk.type == POLARSSL_PK_ECKEY_DH )
ret = snprintf( p, n, "\n%sEC key size : %d bits\n", prefix,
(int) pk_ec( crt->pk )->grp.pbits );
else
#endif /* POLARSSL_ECP_C */
ret = snprintf(p, n, "\n%sPK type looks wrong!", prefix);
SAFE_SNPRINTF(); SAFE_SNPRINTF();
return( (int) ( size - n ) ); return( (int) ( size - n ) );