psa_export_key: for raw-byte keys, zero the end of the output buffer

Skip all writing to the target buffer if its size is 0, since in this
case the pointer might be invalid and this would cause the calls to
memcpy and memset to have undefined behavior.
This commit is contained in:
Gilles Peskine 2018-10-29 19:26:27 +01:00
parent 188c71e382
commit 52b9018cf7

View file

@ -864,8 +864,12 @@ static psa_status_t psa_internal_export_key( psa_key_slot_t key,
{
if( slot->data.raw.bytes > data_size )
return( PSA_ERROR_BUFFER_TOO_SMALL );
if( slot->data.raw.bytes != 0 )
if( data_size != 0 )
{
memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
memset( data + slot->data.raw.bytes, 0,
data_size - slot->data.raw.bytes );
}
*data_length = slot->data.raw.bytes;
return( PSA_SUCCESS );
}