From 3495b58fcfea9667f5170d0aab171d2e172f4a57 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 25 Apr 2019 13:47:06 +0200 Subject: [PATCH] Fix loading of 0-sized key on platforms where malloc(0)=NULL --- library/psa_crypto_storage.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/library/psa_crypto_storage.c b/library/psa_crypto_storage.c index 840f418c3..1e3ce0891 100644 --- a/library/psa_crypto_storage.c +++ b/library/psa_crypto_storage.c @@ -309,16 +309,22 @@ psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data, *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE ) return( PSA_ERROR_STORAGE_FAILURE ); - *key_data = mbedtls_calloc( 1, *key_data_length ); - if( *key_data == NULL ) - return( PSA_ERROR_INSUFFICIENT_MEMORY ); + if( *key_data_length == 0 ) + { + *key_data = NULL; + } + else + { + *key_data = mbedtls_calloc( 1, *key_data_length ); + if( *key_data == NULL ) + return( PSA_ERROR_INSUFFICIENT_MEMORY ); + memcpy( *key_data, storage_format->key_data, *key_data_length ); + } GET_UINT32_LE(*type, storage_format->type, 0); GET_UINT32_LE(policy->usage, storage_format->policy, 0); GET_UINT32_LE(policy->alg, storage_format->policy, sizeof( uint32_t )); - memcpy( *key_data, storage_format->key_data, *key_data_length ); - return( PSA_SUCCESS ); }