Avoid possible miscast of PK key
I don't think this can cause a crash as the member accessed is in the
beginning of the context, so wouldn't be outside of valid memory if the actual
context was RSA.
Also, the mismatch will be caught later when checking signature, so the cert
chain will be rejected anyway.
diff --git a/library/x509_crt.c b/library/x509_crt.c
index e8a46da..8f8f693 100644
--- a/library/x509_crt.c
+++ b/library/x509_crt.c
@@ -208,7 +208,19 @@
pk_alg == MBEDTLS_PK_ECKEY ||
pk_alg == MBEDTLS_PK_ECKEY_DH )
{
- mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
+ mbedtls_ecp_group_id gid;
+ mbedtls_pk_type_t pk_type;
+
+ /* Avoid calling pk_ec() if this is not an EC key */
+ pk_type = mbedtls_pk_get_type( pk );
+ if( pk_type != MBEDTLS_PK_ECDSA &&
+ pk_type != MBEDTLS_PK_ECKEY &&
+ pk_type != MBEDTLS_PK_ECKEY_DH )
+ {
+ return( -1 );
+ }
+
+ gid = mbedtls_pk_ec( *pk )->grp.id;
if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
return( 0 );