ssl_tls1X_populate_transform(): import psa keys only if alg is not MBEDTLS_SSL_NULL_CIPHER

Signed-off-by: Przemyslaw Stekiel <przemyslaw.stekiel@mobica.com>
This commit is contained in:
Przemyslaw Stekiel 2022-02-01 11:25:55 +01:00
parent 77aec8d181
commit f9cd60853f
2 changed files with 47 additions and 40 deletions

View file

@ -1020,32 +1020,36 @@ static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
goto end;
}
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
psa_set_key_algorithm( &attributes, alg );
psa_set_key_type( &attributes, key_type );
transform->psa_alg = alg;
if( ( status = psa_import_key( &attributes,
key1,
PSA_BITS_TO_BYTES( key_bits ),
&transform->psa_key_enc ) ) != PSA_SUCCESS )
if ( alg != MBEDTLS_SSL_NULL_CIPHER )
{
ret = psa_ssl_status_to_mbedtls( status );
MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
goto end;
}
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
psa_set_key_algorithm( &attributes, alg );
psa_set_key_type( &attributes, key_type );
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
if( ( status = psa_import_key( &attributes,
key1,
PSA_BITS_TO_BYTES( key_bits ),
&transform->psa_key_enc ) ) != PSA_SUCCESS )
{
MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
ret = psa_ssl_status_to_mbedtls( status );
MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
goto end;
}
if( ( status = psa_import_key( &attributes,
key2,
PSA_BITS_TO_BYTES( key_bits ),
&transform->psa_key_dec ) ) != PSA_SUCCESS )
{
ret = psa_ssl_status_to_mbedtls( status );
MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
goto end;
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
if( ( status = psa_import_key( &attributes,
key2,
PSA_BITS_TO_BYTES( key_bits ),
&transform->psa_key_dec ) ) != PSA_SUCCESS )
{
ret = psa_ssl_status_to_mbedtls( status );
MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
goto end;
}
}
#else
if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,

View file

@ -940,30 +940,33 @@ int mbedtls_ssl_tls13_populate_transform( mbedtls_ssl_transform *transform,
return( psa_ssl_status_to_mbedtls( status ) );
}
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
psa_set_key_algorithm( &attributes, alg );
psa_set_key_type( &attributes, key_type );
transform->psa_alg = alg;
if( ( status = psa_import_key( &attributes,
key_enc,
PSA_BITS_TO_BYTES( key_bits ),
&transform->psa_key_enc ) ) != PSA_SUCCESS )
if ( alg != MBEDTLS_SSL_NULL_CIPHER )
{
MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) );
return( psa_ssl_status_to_mbedtls( status ) );
}
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
psa_set_key_algorithm( &attributes, alg );
psa_set_key_type( &attributes, key_type );
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
if( ( status = psa_import_key( &attributes,
key_enc,
PSA_BITS_TO_BYTES( key_bits ),
&transform->psa_key_enc ) ) != PSA_SUCCESS )
{
MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) );
return( psa_ssl_status_to_mbedtls( status ) );
}
if( ( status = psa_import_key( &attributes,
key_dec,
PSA_BITS_TO_BYTES( key_bits ),
&transform->psa_key_dec ) ) != PSA_SUCCESS )
{
MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) );
return( psa_ssl_status_to_mbedtls( status ) );
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
if( ( status = psa_import_key( &attributes,
key_dec,
PSA_BITS_TO_BYTES( key_bits ),
&transform->psa_key_dec ) ) != PSA_SUCCESS )
{
MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) );
return( psa_ssl_status_to_mbedtls( status ) );
}
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */