From 360a5830299599f5099e4dd39c06d5a12c1a4b95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 10 Jul 2013 14:56:36 +0200 Subject: [PATCH] Adapt x509parse_cert_info() for EC --- include/polarssl/pk.h | 21 +++++++++++++++++++++ library/x509parse.c | 19 +++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/include/polarssl/pk.h b/include/polarssl/pk.h index 7a54c7142..f5797f373 100644 --- a/include/polarssl/pk.h +++ b/include/polarssl/pk.h @@ -36,6 +36,27 @@ #define POLARSSL_ERR_PK_MALLOC_FAILED -0x2F80 /**< Memory alloation failed. */ #define POLARSSL_ERR_PK_TYPE_MISMATCH -0x2F00 /**< Type mismatch, eg attempt to use a RSA key as EC, or to modify key type */ +#if defined(POLARSSL_RSA_C) +/** + * Quick access to an RSA context inside a PK context. + * + * \warning You must make sure the PK context actually holds an RSA context + * before using this macro! + */ +#define pk_rsa( pk ) ( (rsa_context *) pk.data ) +#endif + +#if defined(POLARSSL_ECP_C) +/** + * Quick access to an EC context inside a PK context. + * + * \warning You must make sure the PK context actually holds an EC context + * before using this macro! + */ +#define pk_ec( pk ) ( (ecp_keypair *) pk.data ) +#endif + + #ifdef __cplusplus extern "C" { #endif diff --git a/library/x509parse.c b/library/x509parse.c index 7c69aa2e7..8abbd0ec8 100644 --- a/library/x509parse.c +++ b/library/x509parse.c @@ -3341,8 +3341,23 @@ int x509parse_cert_info( char *buf, size_t size, const char *prefix, ret = snprintf( p, n, desc ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix, - (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 ); + switch( crt->pk.type ) + { + case POLARSSL_PK_NONE: + case POLARSSL_PK_ECDSA: + ret = snprintf(p, n, "\n%sPK type looks wrong!", prefix); + break; + + case POLARSSL_PK_RSA: + ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix, + (int) pk_rsa( crt->pk )->N.n * (int) sizeof( t_uint ) * 8 ); + break; + + case POLARSSL_PK_ECKEY: + case POLARSSL_PK_ECKEY_DH: + ret = snprintf( p, n, "\n%sEC key size : %d bits\n", prefix, + (int) pk_ec( crt->pk )->grp.pbits ); + } SAFE_SNPRINTF(); return( (int) ( size - n ) );