Merge pull request #3067 from gilles-peskine-arm/fuzz_pubkey-rsa_export

Fix fuzz_pubkey on valid RSA keys
This commit is contained in:
Gilles Peskine 2020-03-04 11:23:14 +01:00 committed by GitHub
commit 59e584949d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 18 deletions

View file

@ -44,20 +44,26 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
else else
#endif #endif
#if defined(MBEDTLS_ECP_C) #if defined(MBEDTLS_ECP_C)
if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY ) if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY ||
mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY_DH )
{ {
mbedtls_ecp_keypair *ecp; mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
mbedtls_ecp_group_id grp_id = ecp->grp.id;
const mbedtls_ecp_curve_info *curve_info =
mbedtls_ecp_curve_info_from_grp_id( grp_id );
ecp = mbedtls_pk_ec( pk ); /* If the curve is not supported, the key should not have been
if (ecp) { * accepted. */
ret = 0; if( curve_info == NULL )
} abort( );
} }
else else
#endif #endif
{ {
ret = 0; /* The key is valid but is not of a supported type.
} * This should not happen. */
abort( );
}
} }
mbedtls_pk_free( &pk ); mbedtls_pk_free( &pk );
#else #else

View file

@ -21,7 +21,10 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP ); mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
rsa = mbedtls_pk_rsa( pk ); rsa = mbedtls_pk_rsa( pk );
if ( mbedtls_rsa_export( rsa, &N, &P, &Q, &D, &E ) != 0 ) { if ( mbedtls_rsa_export( rsa, &N, NULL, NULL, NULL, &E ) != 0 ) {
abort();
}
if ( mbedtls_rsa_export( rsa, &N, &P, &Q, &D, &E ) != MBEDTLS_ERR_RSA_BAD_INPUT_DATA ) {
abort(); abort();
} }
if ( mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) != MBEDTLS_ERR_RSA_BAD_INPUT_DATA ) { if ( mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) != MBEDTLS_ERR_RSA_BAD_INPUT_DATA ) {
@ -36,20 +39,30 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
else else
#endif #endif
#if defined(MBEDTLS_ECP_C) #if defined(MBEDTLS_ECP_C)
if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY ) if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY ||
mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY_DH )
{ {
mbedtls_ecp_keypair *ecp; mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
mbedtls_ecp_group_id grp_id = ecp->grp.id;
const mbedtls_ecp_curve_info *curve_info =
mbedtls_ecp_curve_info_from_grp_id( grp_id );
ecp = mbedtls_pk_ec( pk ); /* If the curve is not supported, the key should not have been
//dummy use of value * accepted. */
if (ecp) { if( curve_info == NULL )
ret = 0; abort( );
}
/* It's a public key, so the private value should not have
* been changed from its initialization to 0. */
if( mbedtls_mpi_cmp_int( &ecp->d, 0 ) != 0 )
abort( );
} }
else else
#endif #endif
{ {
ret = 0; /* The key is valid but is not of a supported type.
* This should not happen. */
abort( );
} }
} }
mbedtls_pk_free( &pk ); mbedtls_pk_free( &pk );