2018-11-30 18:54:54 +01:00
|
|
|
/*
|
|
|
|
* PSA crypto layer on top of Mbed TLS crypto
|
|
|
|
*/
|
2020-06-15 11:59:37 +02:00
|
|
|
/*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2018-11-30 18:54:54 +01:00
|
|
|
*/
|
|
|
|
|
2020-06-03 01:43:33 +02:00
|
|
|
#include "common.h"
|
2018-11-30 18:54:54 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
|
|
|
|
|
|
|
#include "psa/crypto.h"
|
|
|
|
|
2018-12-10 16:29:04 +01:00
|
|
|
#include "psa_crypto_core.h"
|
2023-09-11 12:36:20 +02:00
|
|
|
#include "psa_crypto_driver_wrappers_no_static.h"
|
2018-11-30 18:54:54 +01:00
|
|
|
#include "psa_crypto_slot_management.h"
|
|
|
|
#include "psa_crypto_storage.h"
|
2019-07-30 21:32:04 +02:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
|
|
|
#include "psa_crypto_se.h"
|
|
|
|
#endif
|
2018-11-30 18:54:54 +01:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "mbedtls/platform.h"
|
2024-01-08 12:04:21 +01:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
#include "mbedtls/threading.h"
|
|
|
|
#endif
|
2018-11-30 18:54:54 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
typedef struct {
|
2021-02-15 14:03:19 +01:00
|
|
|
psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
|
2023-08-16 18:56:28 +02:00
|
|
|
uint8_t key_slots_initialized;
|
2018-12-10 16:29:04 +01:00
|
|
|
} psa_global_data_t;
|
|
|
|
|
2018-12-12 14:05:08 +01:00
|
|
|
static psa_global_data_t global_data;
|
2018-12-10 16:29:04 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok)
|
2020-07-17 16:11:30 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
|
2020-07-17 16:11:30 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((PSA_KEY_ID_USER_MIN <= key_id) &&
|
|
|
|
(key_id <= PSA_KEY_ID_USER_MAX)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2020-07-17 16:11:30 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (vendor_ok &&
|
|
|
|
(PSA_KEY_ID_VENDOR_MIN <= key_id) &&
|
|
|
|
(key_id <= PSA_KEY_ID_VENDOR_MAX)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2020-07-17 16:11:30 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-07-17 16:11:30 +02:00
|
|
|
}
|
|
|
|
|
2020-11-14 16:35:34 +01:00
|
|
|
/** Get the description in memory of a key given its identifier and lock it.
|
2020-10-15 11:17:11 +02:00
|
|
|
*
|
2020-11-14 16:35:34 +01:00
|
|
|
* The descriptions of volatile keys and loaded persistent keys are
|
|
|
|
* stored in key slots. This function returns a pointer to the key slot
|
|
|
|
* containing the description of a key given its identifier.
|
2020-10-15 11:17:11 +02:00
|
|
|
*
|
2020-11-14 16:35:34 +01:00
|
|
|
* The function searches the key slots containing the description of the key
|
|
|
|
* with \p key identifier. The function does only read accesses to the key
|
|
|
|
* slots. The function does not load any persistent key thus does not access
|
|
|
|
* any storage.
|
2020-10-15 11:17:11 +02:00
|
|
|
*
|
2020-11-14 16:35:34 +01:00
|
|
|
* For volatile key identifiers, only one key slot is queried as a volatile
|
|
|
|
* key with identifier key_id can only be stored in slot of index
|
|
|
|
* ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
|
2020-10-15 11:17:11 +02:00
|
|
|
*
|
2020-11-14 16:35:34 +01:00
|
|
|
* On success, the function locks the key slot. It is the responsibility of
|
|
|
|
* the caller to unlock the key slot when it does not access it anymore.
|
2020-10-22 15:24:49 +02:00
|
|
|
*
|
2024-01-31 14:21:33 +01:00
|
|
|
* If multi-threading is enabled, the caller must hold the
|
|
|
|
* global key slot mutex.
|
|
|
|
*
|
2020-10-15 11:17:11 +02:00
|
|
|
* \param key Key identifier to query.
|
|
|
|
* \param[out] p_slot On success, `*p_slot` contains a pointer to the
|
|
|
|
* key slot containing the description of the key
|
|
|
|
* identified by \p key.
|
|
|
|
*
|
2020-10-19 12:06:30 +02:00
|
|
|
* \retval #PSA_SUCCESS
|
2020-10-15 11:17:11 +02:00
|
|
|
* The pointer to the key slot containing the description of the key
|
|
|
|
* identified by \p key was returned.
|
2020-10-19 12:06:30 +02:00
|
|
|
* \retval #PSA_ERROR_INVALID_HANDLE
|
2020-10-15 11:17:11 +02:00
|
|
|
* \p key is not a valid key identifier.
|
|
|
|
* \retval #PSA_ERROR_DOES_NOT_EXIST
|
|
|
|
* There is no key with key identifier \p key in the key slots.
|
|
|
|
*/
|
2020-11-14 16:35:34 +01:00
|
|
|
static psa_status_t psa_get_and_lock_key_slot_in_memory(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot)
|
2018-12-10 16:29:04 +01:00
|
|
|
{
|
2020-11-12 10:07:21 +01:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
|
2020-11-12 10:07:21 +01:00
|
|
|
size_t slot_idx;
|
2020-10-15 11:17:11 +02:00
|
|
|
psa_key_slot_t *slot = NULL;
|
2018-12-10 16:29:04 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (psa_key_id_is_volatile(key_id)) {
|
|
|
|
slot = &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN];
|
2020-11-18 17:21:22 +01:00
|
|
|
|
2024-01-31 14:21:33 +01:00
|
|
|
/* Check if both the PSA key identifier key_id and the owner
|
|
|
|
* identifier of key match those of the key slot. */
|
|
|
|
if ((slot->state == PSA_SLOT_FULL) &&
|
|
|
|
(mbedtls_svc_key_id_equal(key, slot->attr.id))) {
|
|
|
|
status = PSA_SUCCESS;
|
|
|
|
} else {
|
|
|
|
status = PSA_ERROR_DOES_NOT_EXIST;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
if (!psa_is_valid_key_id(key, 1)) {
|
|
|
|
return PSA_ERROR_INVALID_HANDLE;
|
|
|
|
}
|
2020-10-15 11:17:11 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
|
|
|
|
slot = &global_data.key_slots[slot_idx];
|
2024-01-03 14:03:36 +01:00
|
|
|
/* Only consider slots which are in a full state. */
|
|
|
|
if ((slot->state == PSA_SLOT_FULL) &&
|
|
|
|
(mbedtls_svc_key_id_equal(key, slot->attr.id))) {
|
2020-10-15 11:17:11 +02:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-10-15 11:17:11 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ?
|
2020-11-12 10:07:21 +01:00
|
|
|
PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
|
2020-07-31 11:26:37 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status == PSA_SUCCESS) {
|
2024-01-03 14:03:36 +01:00
|
|
|
status = psa_register_read(slot);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status == PSA_SUCCESS) {
|
2020-11-13 15:59:59 +01:00
|
|
|
*p_slot = slot;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
}
|
2020-10-15 11:17:11 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2018-12-10 16:29:04 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_initialize_key_slots(void)
|
2018-12-10 16:29:04 +01:00
|
|
|
{
|
2024-01-19 13:59:28 +01:00
|
|
|
/* Nothing to do: program startup and psa_wipe_all_key_slots() both
|
2018-12-10 16:29:04 +01:00
|
|
|
* guarantee that the key slots are initialized to all-zero, which
|
|
|
|
* means that all the key slots are in a valid, empty state. */
|
|
|
|
global_data.key_slots_initialized = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
2018-12-10 16:29:04 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void psa_wipe_all_key_slots(void)
|
2018-12-10 16:29:04 +01:00
|
|
|
{
|
2020-07-24 16:33:11 +02:00
|
|
|
size_t slot_idx;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
|
|
|
|
psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
|
2024-01-04 13:10:18 +01:00
|
|
|
slot->registered_readers = 1;
|
|
|
|
slot->state = PSA_SLOT_PENDING_DELETION;
|
2023-01-11 14:50:10 +01:00
|
|
|
(void) psa_wipe_key_slot(slot);
|
2018-12-10 16:29:04 +01:00
|
|
|
}
|
|
|
|
global_data.key_slots_initialized = 0;
|
|
|
|
}
|
|
|
|
|
2023-12-22 16:59:45 +01:00
|
|
|
psa_status_t psa_reserve_free_key_slot(psa_key_id_t *volatile_key_id,
|
|
|
|
psa_key_slot_t **p_slot)
|
2018-12-10 16:29:04 +01:00
|
|
|
{
|
2020-10-21 09:04:34 +02:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-07-24 16:33:11 +02:00
|
|
|
size_t slot_idx;
|
2023-12-22 16:59:45 +01:00
|
|
|
psa_key_slot_t *selected_slot, *unused_persistent_key_slot;
|
2020-07-24 16:33:11 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!global_data.key_slots_initialized) {
|
2020-10-21 09:04:34 +02:00
|
|
|
status = PSA_ERROR_BAD_STATE;
|
|
|
|
goto error;
|
|
|
|
}
|
2019-05-27 19:01:54 +02:00
|
|
|
|
2023-12-22 16:59:45 +01:00
|
|
|
selected_slot = unused_persistent_key_slot = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
|
|
|
|
psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
|
2023-12-22 16:59:45 +01:00
|
|
|
if (slot->state == PSA_SLOT_EMPTY) {
|
2020-10-21 09:04:34 +02:00
|
|
|
selected_slot = slot;
|
|
|
|
break;
|
|
|
|
}
|
2020-07-17 14:13:26 +02:00
|
|
|
|
2023-12-22 16:59:45 +01:00
|
|
|
if ((unused_persistent_key_slot == NULL) &&
|
|
|
|
(slot->state == PSA_SLOT_FULL) &&
|
|
|
|
(!psa_key_slot_has_readers(slot)) &&
|
|
|
|
(!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime))) {
|
|
|
|
unused_persistent_key_slot = slot;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-10-21 09:04:34 +02:00
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
|
2020-10-21 09:04:34 +02:00
|
|
|
/*
|
2020-11-14 16:35:34 +01:00
|
|
|
* If there is no unused key slot and there is at least one unlocked key
|
2020-11-18 17:21:22 +01:00
|
|
|
* slot containing the description of a persistent key, recycle the first
|
|
|
|
* such key slot we encountered. If we later need to operate on the
|
|
|
|
* persistent key we are evicting now, we will reload its description from
|
2020-11-10 18:08:03 +01:00
|
|
|
* storage.
|
2020-10-21 09:04:34 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((selected_slot == NULL) &&
|
2023-12-22 16:59:45 +01:00
|
|
|
(unused_persistent_key_slot != NULL)) {
|
|
|
|
selected_slot = unused_persistent_key_slot;
|
|
|
|
psa_register_read(selected_slot);
|
|
|
|
status = psa_wipe_key_slot(selected_slot);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
goto error;
|
|
|
|
}
|
2018-12-10 16:29:04 +01:00
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (selected_slot != NULL) {
|
2023-12-22 16:59:45 +01:00
|
|
|
status = psa_key_slot_state_transition(selected_slot, PSA_SLOT_EMPTY,
|
|
|
|
PSA_SLOT_FILLING);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
2024-01-15 12:19:03 +01:00
|
|
|
goto error;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-13 15:59:59 +01:00
|
|
|
|
2020-10-21 09:04:34 +02:00
|
|
|
*volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
|
2023-01-11 14:50:10 +01:00
|
|
|
((psa_key_id_t) (selected_slot - global_data.key_slots));
|
2020-10-21 09:04:34 +02:00
|
|
|
*p_slot = selected_slot;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
2020-10-21 09:04:34 +02:00
|
|
|
}
|
|
|
|
status = PSA_ERROR_INSUFFICIENT_MEMORY;
|
|
|
|
|
|
|
|
error:
|
2019-05-27 19:01:54 +02:00
|
|
|
*p_slot = NULL;
|
2020-10-21 09:04:34 +02:00
|
|
|
*volatile_key_id = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2018-12-10 16:29:04 +01:00
|
|
|
}
|
|
|
|
|
2018-12-10 16:48:53 +01:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot)
|
2018-12-10 16:48:53 +01:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_SUCCESS;
|
|
|
|
uint8_t *key_data = NULL;
|
|
|
|
size_t key_data_length = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_load_persistent_key(&slot->attr,
|
|
|
|
&key_data, &key_data_length);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2018-12-10 16:48:53 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-07-23 16:13:14 +02:00
|
|
|
|
2021-01-08 19:19:40 +01:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2021-01-15 17:36:02 +01:00
|
|
|
/* Special handling is required for loading keys associated with a
|
|
|
|
* dynamically registered SE interface. */
|
|
|
|
const psa_drv_se_t *drv;
|
|
|
|
psa_drv_se_context_t *drv_context;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) {
|
2021-01-15 17:36:02 +01:00
|
|
|
psa_se_key_data_storage_t *data;
|
2020-11-25 17:52:23 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (key_data_length != sizeof(*data)) {
|
2020-11-09 17:39:56 +01:00
|
|
|
status = PSA_ERROR_DATA_INVALID;
|
2021-01-15 17:36:02 +01:00
|
|
|
goto exit;
|
2019-07-23 16:13:14 +02:00
|
|
|
}
|
2023-11-23 17:20:45 +01:00
|
|
|
data = (psa_se_key_data_storage_t *) key_data;
|
2023-11-23 17:33:12 +01:00
|
|
|
status = psa_copy_key_material_into_slot(
|
|
|
|
slot, data->slot_number, sizeof(data->slot_number));
|
|
|
|
goto exit;
|
2019-07-23 16:13:14 +02:00
|
|
|
}
|
2021-01-15 17:36:02 +01:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_copy_key_material_into_slot(slot, key_data, key_data_length);
|
2023-11-21 12:49:57 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
2023-11-16 14:37:51 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2018-12-10 16:48:53 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_free_persistent_key_data(key_data, key_data_length);
|
|
|
|
return status;
|
2018-12-10 16:48:53 +01:00
|
|
|
}
|
2020-07-31 11:26:37 +02:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
|
|
|
|
|
2021-02-19 17:21:22 +01:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot)
|
2021-02-19 17:21:22 +01:00
|
|
|
{
|
2021-03-18 17:33:46 +01:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-03-18 20:48:06 +01:00
|
|
|
psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
|
2021-03-18 17:33:46 +01:00
|
|
|
psa_drv_slot_number_t slot_number = 0;
|
|
|
|
size_t key_buffer_size = 0;
|
|
|
|
size_t key_buffer_length = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!psa_key_id_is_builtin(
|
|
|
|
MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) {
|
|
|
|
return PSA_ERROR_DOES_NOT_EXIST;
|
2021-03-18 17:17:40 +01:00
|
|
|
}
|
2021-02-19 17:21:22 +01:00
|
|
|
|
2021-03-18 17:17:40 +01:00
|
|
|
/* Check the platform function to see whether this key actually exists */
|
2021-03-18 20:48:06 +01:00
|
|
|
status = mbedtls_psa_platform_get_builtin_key(
|
2023-01-11 14:50:10 +01:00
|
|
|
slot->attr.id, &lifetime, &slot_number);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2021-02-19 17:21:22 +01:00
|
|
|
|
2021-04-13 13:45:45 +02:00
|
|
|
/* Set required key attributes to ensure get_builtin_key can retrieve the
|
|
|
|
* full attributes. */
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_id(&attributes, slot->attr.id);
|
|
|
|
psa_set_key_lifetime(&attributes, lifetime);
|
2021-03-18 20:48:06 +01:00
|
|
|
|
2021-04-07 18:09:53 +02:00
|
|
|
/* Get the full key attributes from the driver in order to be able to
|
|
|
|
* calculate the required buffer size. */
|
|
|
|
status = psa_driver_wrapper_get_builtin_key(
|
2023-01-11 14:50:10 +01:00
|
|
|
slot_number, &attributes,
|
|
|
|
NULL, 0, NULL);
|
|
|
|
if (status != PSA_ERROR_BUFFER_TOO_SMALL) {
|
2021-04-07 18:09:53 +02:00
|
|
|
/* Builtin keys cannot be defined by the attributes alone */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status == PSA_SUCCESS) {
|
2021-04-07 18:09:53 +02:00
|
|
|
status = PSA_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
return status;
|
2021-04-07 18:09:53 +02:00
|
|
|
}
|
|
|
|
|
2021-04-06 16:45:06 +02:00
|
|
|
/* If the key should exist according to the platform, then ask the driver
|
|
|
|
* what its expected size is. */
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_driver_wrapper_get_key_buffer_size(&attributes,
|
|
|
|
&key_buffer_size);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2021-03-18 17:17:40 +01:00
|
|
|
|
2021-04-06 16:45:06 +02:00
|
|
|
/* Allocate a buffer of the required size and load the builtin key directly
|
2021-04-07 18:09:53 +02:00
|
|
|
* into the (now properly sized) slot buffer. */
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2021-03-18 17:17:40 +01:00
|
|
|
|
|
|
|
status = psa_driver_wrapper_get_builtin_key(
|
2023-01-11 14:50:10 +01:00
|
|
|
slot_number, &attributes,
|
|
|
|
slot->key.data, slot->key.bytes, &key_buffer_length);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-18 17:17:40 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-18 17:17:40 +01:00
|
|
|
|
2021-04-06 16:45:06 +02:00
|
|
|
/* Copy actual key length and core attributes into the slot on success */
|
|
|
|
slot->key.bytes = key_buffer_length;
|
2021-03-18 17:34:55 +01:00
|
|
|
slot->attr = attributes.core;
|
2021-03-18 17:17:40 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
psa_remove_key_data_from_memory(slot);
|
|
|
|
}
|
|
|
|
return status;
|
2021-02-19 17:21:22 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
|
|
|
|
psa_key_slot_t **p_slot)
|
2020-07-31 11:26:37 +02:00
|
|
|
{
|
2020-10-15 11:17:11 +02:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2020-07-31 11:26:37 +02:00
|
|
|
|
|
|
|
*p_slot = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!global_data.key_slots_initialized) {
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
}
|
2020-07-31 11:26:37 +02:00
|
|
|
|
2024-01-31 14:31:00 +01:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
/* If the key is persistent and not loaded, we cannot unlock the mutex
|
|
|
|
* between checking if the key is loaded and setting the slot as FULL,
|
|
|
|
* as otherwise another thread may load and then destroy the key
|
|
|
|
* in the meantime. */
|
|
|
|
PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
|
|
|
|
&mbedtls_threading_key_slot_mutex));
|
|
|
|
#endif
|
2020-10-22 15:24:49 +02:00
|
|
|
/*
|
|
|
|
* On success, the pointer to the slot is passed directly to the caller
|
2020-11-14 16:35:34 +01:00
|
|
|
* thus no need to unlock the key slot here.
|
2020-10-22 15:24:49 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_get_and_lock_key_slot_in_memory(key, p_slot);
|
|
|
|
if (status != PSA_ERROR_DOES_NOT_EXIST) {
|
2024-01-31 14:31:00 +01:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
|
|
|
|
&mbedtls_threading_key_slot_mutex));
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
|
|
|
}
|
2020-07-31 11:26:37 +02:00
|
|
|
|
2021-04-06 15:09:57 +02:00
|
|
|
/* Loading keys from storage requires support for such a mechanism */
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
|
|
|
|
defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
|
2020-07-31 11:26:37 +02:00
|
|
|
psa_key_id_t volatile_key_id;
|
|
|
|
|
2024-01-03 14:03:36 +01:00
|
|
|
status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
2024-01-31 14:31:00 +01:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
|
|
|
|
&mbedtls_threading_key_slot_mutex));
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
|
|
|
}
|
2020-07-31 11:26:37 +02:00
|
|
|
|
|
|
|
(*p_slot)->attr.id = key;
|
2021-02-19 17:21:22 +01:00
|
|
|
(*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
|
|
|
|
|
|
|
|
status = PSA_ERROR_DOES_NOT_EXIST;
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
|
2021-04-06 13:08:42 +02:00
|
|
|
/* Load keys in the 'builtin' range through their own interface */
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_load_builtin_key_into_slot(*p_slot);
|
2021-02-19 17:21:22 +01:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
|
2020-07-31 11:26:37 +02:00
|
|
|
|
2021-02-19 17:21:22 +01:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status == PSA_ERROR_DOES_NOT_EXIST) {
|
|
|
|
status = psa_load_persistent_key_into_slot(*p_slot);
|
|
|
|
}
|
2021-02-19 17:21:22 +01:00
|
|
|
#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
psa_wipe_key_slot(*p_slot);
|
2024-01-03 14:03:36 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status == PSA_ERROR_DOES_NOT_EXIST) {
|
2021-03-15 15:48:14 +01:00
|
|
|
status = PSA_ERROR_INVALID_HANDLE;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
} else {
|
2021-06-28 14:59:52 +02:00
|
|
|
/* Add implicit usage flags. */
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_extend_key_usage_flags(&(*p_slot)->attr.policy.usage);
|
2024-01-03 14:03:36 +01:00
|
|
|
|
|
|
|
psa_key_slot_state_transition((*p_slot), PSA_SLOT_FILLING,
|
|
|
|
PSA_SLOT_FULL);
|
|
|
|
status = psa_register_read(*p_slot);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-06-23 16:48:08 +02:00
|
|
|
|
2021-04-06 15:09:57 +02:00
|
|
|
#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
|
2024-01-31 14:31:00 +01:00
|
|
|
status = PSA_ERROR_INVALID_HANDLE;
|
2021-04-06 15:09:57 +02:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
|
2024-01-31 14:31:00 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
|
|
|
|
&mbedtls_threading_key_slot_mutex));
|
|
|
|
#endif
|
|
|
|
return status;
|
2020-07-31 11:26:37 +02:00
|
|
|
}
|
|
|
|
|
2023-12-21 18:57:14 +01:00
|
|
|
psa_status_t psa_unregister_read(psa_key_slot_t *slot)
|
2020-10-22 15:24:49 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (slot == NULL) {
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
}
|
2023-12-21 18:57:14 +01:00
|
|
|
if ((slot->state != PSA_SLOT_FULL) &&
|
|
|
|
(slot->state != PSA_SLOT_PENDING_DELETION)) {
|
2024-01-12 18:45:05 +01:00
|
|
|
return PSA_ERROR_CORRUPTION_DETECTED;
|
2023-12-21 18:57:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we are the last reader and the slot is marked for deletion,
|
|
|
|
* we must wipe the slot here. */
|
|
|
|
if ((slot->state == PSA_SLOT_PENDING_DELETION) &&
|
|
|
|
(slot->registered_readers == 1)) {
|
|
|
|
return psa_wipe_key_slot(slot);
|
|
|
|
}
|
2020-10-22 15:24:49 +02:00
|
|
|
|
2023-12-21 18:57:14 +01:00
|
|
|
if (psa_key_slot_has_readers(slot)) {
|
|
|
|
slot->registered_readers--;
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* As the return error code may not be handled in case of multiple errors,
|
2023-12-21 18:57:14 +01:00
|
|
|
* do our best to report if there are no registered readers. Assert with
|
|
|
|
* MBEDTLS_TEST_HOOK_TEST_ASSERT that there are registered readers:
|
|
|
|
* if the MBEDTLS_TEST_HOOKS configuration option is enabled and
|
2023-01-11 14:50:10 +01:00
|
|
|
* the function is called as part of the execution of a test suite, the
|
|
|
|
* execution of the test suite is stopped in error if the assertion fails.
|
|
|
|
*/
|
2023-12-21 18:57:14 +01:00
|
|
|
MBEDTLS_TEST_HOOK_TEST_ASSERT(psa_key_slot_has_readers(slot));
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ERROR_CORRUPTION_DETECTED;
|
2020-10-22 15:24:49 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
|
|
|
|
psa_se_drv_table_entry_t **p_drv)
|
2019-04-19 18:19:40 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (psa_key_lifetime_is_external(lifetime)) {
|
2020-06-08 18:37:19 +02:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2021-01-15 17:36:02 +01:00
|
|
|
/* Check whether a driver is registered against this lifetime */
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
|
|
|
|
if (driver != NULL) {
|
|
|
|
if (p_drv != NULL) {
|
2020-06-08 18:54:23 +02:00
|
|
|
*p_drv = driver;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
return PSA_SUCCESS;
|
2020-06-08 18:37:19 +02:00
|
|
|
}
|
2021-01-15 17:36:02 +01:00
|
|
|
#else /* MBEDTLS_PSA_CRYPTO_SE_C */
|
2020-06-08 18:37:19 +02:00
|
|
|
(void) p_drv;
|
2021-01-15 17:36:02 +01:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
|
2021-01-08 19:19:40 +01:00
|
|
|
/* Key location for external keys gets checked by the wrapper */
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
|
|
|
} else {
|
2020-06-08 18:37:19 +02:00
|
|
|
/* Local/internal keys are always valid */
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
|
|
|
}
|
2020-06-08 18:37:19 +02:00
|
|
|
}
|
2019-04-25 13:47:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
|
2020-06-08 18:37:19 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
|
2020-06-08 18:37:19 +02:00
|
|
|
/* Volatile keys are always supported */
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
|
|
|
} else {
|
2020-06-08 18:37:19 +02:00
|
|
|
/* Persistent keys require storage support */
|
2019-04-25 13:47:40 +02:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)) {
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
} else {
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
}
|
2019-04-25 13:47:40 +02:00
|
|
|
#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2019-04-25 13:47:40 +02:00
|
|
|
#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
|
2020-06-08 18:37:19 +02:00
|
|
|
}
|
2019-04-19 18:19:40 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle)
|
2018-11-30 18:54:54 +01:00
|
|
|
{
|
2021-07-14 10:29:48 +02:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
|
|
|
|
defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
|
2018-11-30 18:54:54 +01:00
|
|
|
psa_status_t status;
|
2019-05-27 19:01:54 +02:00
|
|
|
psa_key_slot_t *slot;
|
2018-11-30 18:54:54 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_get_and_lock_key_slot(key, &slot);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2020-07-30 17:48:03 +02:00
|
|
|
*handle = PSA_KEY_HANDLE_INIT;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status == PSA_ERROR_INVALID_HANDLE) {
|
2021-03-15 15:48:14 +01:00
|
|
|
status = PSA_ERROR_DOES_NOT_EXIST;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-15 15:48:14 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2018-11-30 18:54:54 +01:00
|
|
|
}
|
2020-07-31 11:26:37 +02:00
|
|
|
|
|
|
|
*handle = key;
|
|
|
|
|
2024-01-04 11:32:49 +01:00
|
|
|
return psa_unregister_read(slot);
|
2019-05-27 19:04:07 +02:00
|
|
|
|
2021-07-14 10:29:48 +02:00
|
|
|
#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
|
2020-07-23 12:30:41 +02:00
|
|
|
(void) key;
|
2020-07-30 17:48:03 +02:00
|
|
|
*handle = PSA_KEY_HANDLE_INIT;
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2021-07-14 10:29:48 +02:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
|
2018-11-30 18:54:54 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_close_key(psa_key_handle_t handle)
|
2018-11-30 18:54:54 +01:00
|
|
|
{
|
2019-05-27 19:01:54 +02:00
|
|
|
psa_status_t status;
|
|
|
|
psa_key_slot_t *slot;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (psa_key_handle_is_null(handle)) {
|
|
|
|
return PSA_SUCCESS;
|
|
|
|
}
|
2019-10-08 15:48:25 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_get_and_lock_key_slot_in_memory(handle, &slot);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
if (status == PSA_ERROR_DOES_NOT_EXIST) {
|
2021-03-15 15:48:14 +01:00
|
|
|
status = PSA_ERROR_INVALID_HANDLE;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-05-27 19:01:54 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
|
|
|
}
|
2024-01-03 17:04:33 +01:00
|
|
|
if (slot->registered_readers == 1) {
|
2024-01-12 17:35:59 +01:00
|
|
|
return psa_wipe_key_slot(slot);
|
|
|
|
} else {
|
|
|
|
return psa_unregister_read(slot);
|
2021-03-15 15:48:14 +01:00
|
|
|
}
|
2018-11-30 18:54:54 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_purge_key(mbedtls_svc_key_id_t key)
|
2020-08-04 15:49:48 +02:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
psa_key_slot_t *slot;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_get_and_lock_key_slot_in_memory(key, &slot);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2020-08-04 15:49:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
|
2024-01-03 17:04:33 +01:00
|
|
|
(slot->registered_readers == 1)) {
|
2024-01-12 17:35:59 +01:00
|
|
|
return psa_wipe_key_slot(slot);
|
|
|
|
} else {
|
|
|
|
return psa_unregister_read(slot);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-08-04 15:49:48 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats)
|
2019-05-23 20:32:30 +02:00
|
|
|
{
|
2020-07-24 16:33:11 +02:00
|
|
|
size_t slot_idx;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(stats, 0, sizeof(*stats));
|
2020-07-24 16:33:11 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
|
|
|
|
const psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
|
2024-01-04 13:13:45 +01:00
|
|
|
if (psa_key_slot_has_readers(slot)) {
|
2020-11-15 14:21:04 +01:00
|
|
|
++stats->locked_slots;
|
2020-10-30 11:54:03 +01:00
|
|
|
}
|
2024-01-04 13:13:45 +01:00
|
|
|
if (slot->state == PSA_SLOT_EMPTY) {
|
2019-07-31 15:01:55 +02:00
|
|
|
++stats->empty_slots;
|
2019-05-23 20:32:30 +02:00
|
|
|
continue;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
|
2019-05-23 20:32:30 +02:00
|
|
|
++stats->volatile_slots;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
|
2019-05-23 20:32:30 +02:00
|
|
|
++stats->persistent_slots;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (id > stats->max_open_internal_key_id) {
|
2019-08-20 18:43:48 +02:00
|
|
|
stats->max_open_internal_key_id = id;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-05-23 20:32:30 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime) !=
|
|
|
|
PSA_KEY_LOCATION_LOCAL_STORAGE) {
|
|
|
|
psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
|
2019-05-23 20:32:30 +02:00
|
|
|
++stats->external_slots;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (id > stats->max_open_external_key_id) {
|
2019-08-20 18:43:48 +02:00
|
|
|
stats->max_open_external_key_id = id;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-05-23 20:32:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 18:54:54 +01:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_C */
|