Do not allocate zero sized buffers - keys

pull/10469/head
itayzafrir 2019-03-27 15:14:37 +02:00 committed by adbridge
parent e3f9a7b93c
commit b2731f100a
1 changed files with 26 additions and 29 deletions

View File

@ -1212,16 +1212,17 @@ static void psa_key_management_operation(void)
size_t key_length = msg.in_size[1]; size_t key_length = msg.in_size[1];
uint8_t *key = NULL; uint8_t *key = NULL;
if (!psa_crypto_access_control_is_handle_permitted(psa_key_mng.handle, if (!psa_crypto_access_control_is_handle_permitted(psa_key_mng.handle, partition_id)) {
partition_id)) {
status = PSA_ERROR_INVALID_HANDLE; status = PSA_ERROR_INVALID_HANDLE;
break; break;
} }
key = mbedtls_calloc(1, key_length); if (key_length > 0) {
if (key == NULL) { key = mbedtls_calloc(1, key_length);
status = PSA_ERROR_INSUFFICIENT_MEMORY; if (key == NULL) {
break; status = PSA_ERROR_INSUFFICIENT_MEMORY;
break;
}
} }
bytes_read = psa_read(msg.handle, 1, key, key_length); bytes_read = psa_read(msg.handle, 1, key, key_length);
@ -1229,9 +1230,7 @@ static void psa_key_management_operation(void)
SPM_PANIC("SPM read length mismatch"); SPM_PANIC("SPM read length mismatch");
} }
status = psa_import_key(psa_key_mng.handle, status = psa_import_key(psa_key_mng.handle, psa_key_mng.type, key, key_length);
psa_key_mng.type,
key, key_length);
mbedtls_free(key); mbedtls_free(key);
break; break;
} }
@ -1277,26 +1276,25 @@ static void psa_key_management_operation(void)
size_t data_length; size_t data_length;
uint8_t *key = NULL; uint8_t *key = NULL;
if (!psa_crypto_access_control_is_handle_permitted(psa_key_mng.handle, if (!psa_crypto_access_control_is_handle_permitted(psa_key_mng.handle, partition_id)) {
partition_id)) {
status = PSA_ERROR_INVALID_HANDLE; status = PSA_ERROR_INVALID_HANDLE;
break; break;
} }
key = mbedtls_calloc(1, key_length); if (key_length > 0) {
if (key == NULL) { key = mbedtls_calloc(1, key_length);
status = PSA_ERROR_INSUFFICIENT_MEMORY; if (key == NULL) {
break; status = PSA_ERROR_INSUFFICIENT_MEMORY;
break;
}
} }
status = psa_export_key(psa_key_mng.handle, key, status = psa_export_key(psa_key_mng.handle, key, key_length, &data_length);
key_length, &data_length);
if (status == PSA_SUCCESS) { if (status == PSA_SUCCESS) {
psa_write(msg.handle, 0, key, data_length); psa_write(msg.handle, 0, key, data_length);
} }
psa_write(msg.handle, 1, &data_length, sizeof(size_t));
psa_write(msg.handle, 1,
&data_length, sizeof(size_t));
mbedtls_free(key); mbedtls_free(key);
break; break;
} }
@ -1306,26 +1304,25 @@ static void psa_key_management_operation(void)
size_t data_length; size_t data_length;
uint8_t *key = NULL; uint8_t *key = NULL;
if (!psa_crypto_access_control_is_handle_permitted(psa_key_mng.handle, if (!psa_crypto_access_control_is_handle_permitted(psa_key_mng.handle, partition_id)) {
partition_id)) {
status = PSA_ERROR_INVALID_HANDLE; status = PSA_ERROR_INVALID_HANDLE;
break; break;
} }
key = mbedtls_calloc(1, key_length); if (key_length > 0) {
if (key == NULL) { key = mbedtls_calloc(1, key_length);
status = PSA_ERROR_INSUFFICIENT_MEMORY; if (key == NULL) {
break; status = PSA_ERROR_INSUFFICIENT_MEMORY;
break;
}
} }
status = psa_export_public_key(psa_key_mng.handle, key, status = psa_export_public_key(psa_key_mng.handle, key, key_length, &data_length);
key_length, &data_length);
if (status == PSA_SUCCESS) { if (status == PSA_SUCCESS) {
psa_write(msg.handle, 0, key, data_length); psa_write(msg.handle, 0, key, data_length);
} }
psa_write(msg.handle, 1, &data_length, sizeof(size_t));
psa_write(msg.handle, 1,
&data_length, sizeof(size_t));
mbedtls_free(key); mbedtls_free(key);
break; break;
} }