proxmark3/common/mbedtls/psa_crypto_slot_management.c

446 lines
14 KiB
C
Raw Normal View History

2021-05-14 15:53:00 +08:00
/*
* PSA crypto layer on top of Mbed TLS crypto
*/
/*
* Copyright The Mbed TLS Contributors
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "common.h"
#if defined(MBEDTLS_PSA_CRYPTO_C)
#include "psa_crypto_service_integration.h"
#include "psa/crypto.h"
#include "psa_crypto_core.h"
#include "psa_crypto_slot_management.h"
#include "psa_crypto_storage.h"
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
#include "psa_crypto_se.h"
#endif
#include <stdlib.h>
#include <string.h>
#if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h"
#else
#define mbedtls_calloc calloc
#define mbedtls_free free
#endif
#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
2021-05-14 17:00:46 +08:00
typedef struct {
2021-05-14 15:53:00 +08:00
psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
unsigned key_slots_initialized : 1;
} psa_global_data_t;
static psa_global_data_t global_data;
psa_status_t psa_validate_key_id(
2021-05-14 17:00:46 +08:00
mbedtls_svc_key_id_t key, int vendor_ok) {
psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
2021-05-14 15:53:00 +08:00
2021-05-14 17:00:46 +08:00
if ((PSA_KEY_ID_USER_MIN <= key_id) &&
(key_id <= PSA_KEY_ID_USER_MAX))
return (PSA_SUCCESS);
2021-05-14 15:53:00 +08:00
2021-05-14 17:00:46 +08:00
if (vendor_ok &&
(PSA_KEY_ID_VENDOR_MIN <= key_id) &&
(key_id <= PSA_KEY_ID_VENDOR_MAX))
return (PSA_SUCCESS);
2021-05-14 15:53:00 +08:00
2021-05-14 17:00:46 +08:00
return (PSA_ERROR_INVALID_HANDLE);
2021-05-14 15:53:00 +08:00
}
/** Get the description in memory of a key given its identifier and lock it.
*
* 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.
*
* 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.
*
* 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 ).
*
* 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.
*
* \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.
*
* \retval #PSA_SUCCESS
* The pointer to the key slot containing the description of the key
* identified by \p key was returned.
* \retval #PSA_ERROR_INVALID_HANDLE
* \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.
*/
static psa_status_t psa_get_and_lock_key_slot_in_memory(
2021-05-14 17:00:46 +08:00
mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot) {
2021-05-14 15:53:00 +08:00
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2021-05-14 17:00:46 +08:00
psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
2021-05-14 15:53:00 +08:00
size_t slot_idx;
psa_key_slot_t *slot = NULL;
2021-05-14 17:00:46 +08:00
if (psa_key_id_is_volatile(key_id)) {
2021-05-14 15:53:00 +08:00
slot = &global_data.key_slots[ key_id - PSA_KEY_ID_VOLATILE_MIN ];
/*
* Check if both the PSA key identifier key_id and the owner
* identifier of key match those of the key slot.
*
* Note that, if the key slot is not occupied, its PSA key identifier
* is equal to zero. This is an invalid value for a PSA key identifier
* and thus cannot be equal to the valid PSA key identifier key_id.
*/
2021-05-14 17:00:46 +08:00
status = mbedtls_svc_key_id_equal(key, slot->attr.id) ?
2021-05-14 15:53:00 +08:00
PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
2021-05-14 17:00:46 +08:00
} else {
status = psa_validate_key_id(key, 1);
if (status != PSA_SUCCESS)
return (status);
2021-05-14 15:53:00 +08:00
2021-05-14 17:00:46 +08:00
for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
2021-05-14 15:53:00 +08:00
slot = &global_data.key_slots[ slot_idx ];
2021-05-14 17:00:46 +08:00
if (mbedtls_svc_key_id_equal(key, slot->attr.id))
2021-05-14 15:53:00 +08:00
break;
}
2021-05-14 17:00:46 +08:00
status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ?
2021-05-14 15:53:00 +08:00
PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
}
2021-05-14 17:00:46 +08:00
if (status == PSA_SUCCESS) {
status = psa_lock_key_slot(slot);
if (status == PSA_SUCCESS)
2021-05-14 15:53:00 +08:00
*p_slot = slot;
}
2021-05-14 17:00:46 +08:00
return (status);
2021-05-14 15:53:00 +08:00
}
2021-05-14 17:00:46 +08:00
psa_status_t psa_initialize_key_slots(void) {
2021-05-14 15:53:00 +08:00
/* Nothing to do: program startup and psa_wipe_all_key_slots() both
* 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;
2021-05-14 17:00:46 +08:00
return (PSA_SUCCESS);
2021-05-14 15:53:00 +08:00
}
2021-05-14 17:00:46 +08:00
void psa_wipe_all_key_slots(void) {
2021-05-14 15:53:00 +08:00
size_t slot_idx;
2021-05-14 17:00:46 +08:00
for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
2021-05-14 15:53:00 +08:00
psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
slot->lock_count = 1;
2021-05-14 17:00:46 +08:00
(void) psa_wipe_key_slot(slot);
2021-05-14 15:53:00 +08:00
}
global_data.key_slots_initialized = 0;
}
2021-05-14 17:00:46 +08:00
psa_status_t psa_get_empty_key_slot(psa_key_id_t *volatile_key_id,
psa_key_slot_t **p_slot) {
2021-05-14 15:53:00 +08:00
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
size_t slot_idx;
psa_key_slot_t *selected_slot, *unlocked_persistent_key_slot;
2021-05-14 17:00:46 +08:00
if (! global_data.key_slots_initialized) {
2021-05-14 15:53:00 +08:00
status = PSA_ERROR_BAD_STATE;
goto error;
}
selected_slot = unlocked_persistent_key_slot = NULL;
2021-05-14 17:00:46 +08:00
for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
2021-05-14 15:53:00 +08:00
psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
2021-05-14 17:00:46 +08:00
if (! psa_is_key_slot_occupied(slot)) {
2021-05-14 15:53:00 +08:00
selected_slot = slot;
break;
}
2021-05-14 17:00:46 +08:00
if ((unlocked_persistent_key_slot == NULL) &&
(! PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
(! psa_is_key_slot_locked(slot)))
2021-05-14 15:53:00 +08:00
unlocked_persistent_key_slot = slot;
}
/*
* If there is no unused key slot and there is at least one unlocked key
* 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
* storage.
*/
2021-05-14 17:00:46 +08:00
if ((selected_slot == NULL) &&
(unlocked_persistent_key_slot != NULL)) {
2021-05-14 15:53:00 +08:00
selected_slot = unlocked_persistent_key_slot;
selected_slot->lock_count = 1;
2021-05-14 17:00:46 +08:00
psa_wipe_key_slot(selected_slot);
2021-05-14 15:53:00 +08:00
}
2021-05-14 17:00:46 +08:00
if (selected_slot != NULL) {
status = psa_lock_key_slot(selected_slot);
if (status != PSA_SUCCESS)
goto error;
2021-05-14 15:53:00 +08:00
*volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
2021-05-14 17:00:46 +08:00
((psa_key_id_t)(selected_slot - global_data.key_slots));
2021-05-14 15:53:00 +08:00
*p_slot = selected_slot;
2021-05-14 17:00:46 +08:00
return (PSA_SUCCESS);
2021-05-14 15:53:00 +08:00
}
status = PSA_ERROR_INSUFFICIENT_MEMORY;
error:
*p_slot = NULL;
*volatile_key_id = 0;
2021-05-14 17:00:46 +08:00
return (status);
2021-05-14 15:53:00 +08:00
}
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
2021-05-14 17:00:46 +08:00
static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot) {
2021-05-14 15:53:00 +08:00
psa_status_t status = PSA_SUCCESS;
uint8_t *key_data = NULL;
size_t key_data_length = 0;
2021-05-14 17:00:46 +08:00
status = psa_load_persistent_key(&slot->attr,
&key_data, &key_data_length);
if (status != PSA_SUCCESS)
2021-05-14 15:53:00 +08:00
goto exit;
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
/* 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;
2021-05-14 17:00:46 +08:00
if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) {
2021-05-14 15:53:00 +08:00
psa_se_key_data_storage_t *data;
2021-05-14 17:00:46 +08:00
if (key_data_length != sizeof(*data)) {
2021-05-14 15:53:00 +08:00
status = PSA_ERROR_DATA_INVALID;
goto exit;
}
data = (psa_se_key_data_storage_t *) key_data;
status = psa_copy_key_material_into_slot(
2021-05-14 17:00:46 +08:00
slot, data->slot_number, sizeof(data->slot_number));
2021-05-14 15:53:00 +08:00
goto exit;
}
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2021-05-14 17:00:46 +08:00
status = psa_copy_key_material_into_slot(slot, key_data, key_data_length);
2021-05-14 15:53:00 +08:00
exit:
2021-05-14 17:00:46 +08:00
psa_free_persistent_key_data(key_data, key_data_length);
return (status);
2021-05-14 15:53:00 +08:00
}
#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
2021-05-14 17:00:46 +08:00
psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
psa_key_slot_t **p_slot) {
2021-05-14 15:53:00 +08:00
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
*p_slot = NULL;
2021-05-14 17:00:46 +08:00
if (! global_data.key_slots_initialized)
return (PSA_ERROR_BAD_STATE);
2021-05-14 15:53:00 +08:00
/*
* On success, the pointer to the slot is passed directly to the caller
* thus no need to unlock the key slot here.
*/
2021-05-14 17:00:46 +08:00
status = psa_get_and_lock_key_slot_in_memory(key, p_slot);
if (status != PSA_ERROR_DOES_NOT_EXIST)
return (status);
2021-05-14 15:53:00 +08:00
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
psa_key_id_t volatile_key_id;
2021-05-14 17:00:46 +08:00
status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
if (status != PSA_SUCCESS)
return (status);
2021-05-14 15:53:00 +08:00
(*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
(*p_slot)->attr.id = key;
2021-05-14 17:00:46 +08:00
status = psa_load_persistent_key_into_slot(*p_slot);
if (status != PSA_SUCCESS)
psa_wipe_key_slot(*p_slot);
2021-05-14 15:53:00 +08:00
2021-05-14 17:00:46 +08:00
return (status);
2021-05-14 15:53:00 +08:00
#else
2021-05-14 17:00:46 +08:00
return (PSA_ERROR_DOES_NOT_EXIST);
2021-05-14 15:53:00 +08:00
#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
}
2021-05-14 17:00:46 +08:00
psa_status_t psa_unlock_key_slot(psa_key_slot_t *slot) {
if (slot == NULL)
return (PSA_SUCCESS);
2021-05-14 15:53:00 +08:00
2021-05-14 17:00:46 +08:00
if (slot->lock_count > 0) {
2021-05-14 15:53:00 +08:00
slot->lock_count--;
2021-05-14 17:00:46 +08:00
return (PSA_SUCCESS);
2021-05-14 15:53:00 +08:00
}
/*
* As the return error code may not be handled in case of multiple errors,
* do our best to report if the lock counter is equal to zero: if
* available call MBEDTLS_PARAM_FAILED that may terminate execution (if
* called as part of the execution of a unit test suite this will stop the
* test suite execution).
*/
#ifdef MBEDTLS_CHECK_PARAMS
2021-05-14 17:00:46 +08:00
MBEDTLS_PARAM_FAILED(slot->lock_count > 0);
2021-05-14 15:53:00 +08:00
#endif
2021-05-14 17:00:46 +08:00
return (PSA_ERROR_CORRUPTION_DETECTED);
2021-05-14 15:53:00 +08:00
}
2021-05-14 17:00:46 +08:00
psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
psa_se_drv_table_entry_t **p_drv) {
if (psa_key_lifetime_is_external(lifetime)) {
2021-05-14 15:53:00 +08:00
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
/* Check whether a driver is registered against this lifetime */
2021-05-14 17:00:46 +08:00
psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
if (driver != NULL) {
2021-05-14 15:53:00 +08:00
if (p_drv != NULL)
*p_drv = driver;
2021-05-14 17:00:46 +08:00
return (PSA_SUCCESS);
2021-05-14 15:53:00 +08:00
}
#else /* MBEDTLS_PSA_CRYPTO_SE_C */
(void) p_drv;
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
/* Key location for external keys gets checked by the wrapper */
2021-05-14 17:00:46 +08:00
return (PSA_SUCCESS);
2021-05-14 15:53:00 +08:00
#else /* MBEDTLS_PSA_CRYPTO_DRIVERS */
/* No support for external lifetimes at all, or dynamic interface
* did not find driver for requested lifetime. */
2021-05-14 17:00:46 +08:00
return (PSA_ERROR_INVALID_ARGUMENT);
2021-05-14 15:53:00 +08:00
#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
2021-05-14 17:00:46 +08:00
} else
2021-05-14 15:53:00 +08:00
/* Local/internal keys are always valid */
2021-05-14 17:00:46 +08:00
return (PSA_SUCCESS);
2021-05-14 15:53:00 +08:00
}
2021-05-14 17:00:46 +08:00
psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime) {
if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
2021-05-14 15:53:00 +08:00
/* Volatile keys are always supported */
2021-05-14 17:00:46 +08:00
return (PSA_SUCCESS);
} else {
2021-05-14 15:53:00 +08:00
/* Persistent keys require storage support */
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
2021-05-14 17:00:46 +08:00
return (PSA_SUCCESS);
2021-05-14 15:53:00 +08:00
#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
2021-05-14 17:00:46 +08:00
return (PSA_ERROR_NOT_SUPPORTED);
2021-05-14 15:53:00 +08:00
#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
}
}
2021-05-14 17:00:46 +08:00
psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle) {
2021-05-14 15:53:00 +08:00
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
psa_status_t status;
psa_key_slot_t *slot;
2021-05-14 17:00:46 +08:00
status = psa_get_and_lock_key_slot(key, &slot);
if (status != PSA_SUCCESS) {
2021-05-14 15:53:00 +08:00
*handle = PSA_KEY_HANDLE_INIT;
2021-05-14 17:00:46 +08:00
return (status);
2021-05-14 15:53:00 +08:00
}
*handle = key;
2021-05-14 17:00:46 +08:00
return (psa_unlock_key_slot(slot));
2021-05-14 15:53:00 +08:00
#else /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
(void) key;
*handle = PSA_KEY_HANDLE_INIT;
2021-05-14 17:00:46 +08:00
return (PSA_ERROR_NOT_SUPPORTED);
2021-05-14 15:53:00 +08:00
#endif /* !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
}
2021-05-14 17:00:46 +08:00
psa_status_t psa_close_key(psa_key_handle_t handle) {
2021-05-14 15:53:00 +08:00
psa_status_t status;
psa_key_slot_t *slot;
2021-05-14 17:00:46 +08:00
if (psa_key_handle_is_null(handle))
return (PSA_SUCCESS);
2021-05-14 15:53:00 +08:00
2021-05-14 17:00:46 +08:00
status = psa_get_and_lock_key_slot_in_memory(handle, &slot);
if (status != PSA_SUCCESS)
return (status);
2021-05-14 15:53:00 +08:00
2021-05-14 17:00:46 +08:00
if (slot->lock_count <= 1)
return (psa_wipe_key_slot(slot));
2021-05-14 15:53:00 +08:00
else
2021-05-14 17:00:46 +08:00
return (psa_unlock_key_slot(slot));
2021-05-14 15:53:00 +08:00
}
2021-05-14 17:00:46 +08:00
psa_status_t psa_purge_key(mbedtls_svc_key_id_t key) {
2021-05-14 15:53:00 +08:00
psa_status_t status;
psa_key_slot_t *slot;
2021-05-14 17:00:46 +08:00
status = psa_get_and_lock_key_slot_in_memory(key, &slot);
if (status != PSA_SUCCESS)
return (status);
2021-05-14 15:53:00 +08:00
2021-05-14 17:00:46 +08:00
if ((! PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
(slot->lock_count <= 1))
return (psa_wipe_key_slot(slot));
2021-05-14 15:53:00 +08:00
else
2021-05-14 17:00:46 +08:00
return (psa_unlock_key_slot(slot));
2021-05-14 15:53:00 +08:00
}
2021-05-14 17:00:46 +08:00
void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats) {
2021-05-14 15:53:00 +08:00
size_t slot_idx;
2021-05-14 17:00:46 +08:00
memset(stats, 0, sizeof(*stats));
2021-05-14 15:53:00 +08:00
2021-05-14 17:00:46 +08:00
for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
2021-05-14 15:53:00 +08:00
const psa_key_slot_t *slot = &global_data.key_slots[ slot_idx ];
2021-05-14 17:00:46 +08:00
if (psa_is_key_slot_locked(slot)) {
2021-05-14 15:53:00 +08:00
++stats->locked_slots;
}
2021-05-14 17:00:46 +08:00
if (! psa_is_key_slot_occupied(slot)) {
2021-05-14 15:53:00 +08:00
++stats->empty_slots;
continue;
}
2021-05-14 17:00:46 +08:00
if (slot->attr.lifetime == PSA_KEY_LIFETIME_VOLATILE)
2021-05-14 15:53:00 +08:00
++stats->volatile_slots;
2021-05-14 17:00:46 +08:00
else if (slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT) {
psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
2021-05-14 15:53:00 +08:00
++stats->persistent_slots;
2021-05-14 17:00:46 +08:00
if (id > stats->max_open_internal_key_id)
2021-05-14 15:53:00 +08:00
stats->max_open_internal_key_id = id;
2021-05-14 17:00:46 +08:00
} else {
psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
2021-05-14 15:53:00 +08:00
++stats->external_slots;
2021-05-14 17:00:46 +08:00
if (id > stats->max_open_external_key_id)
2021-05-14 15:53:00 +08:00
stats->max_open_external_key_id = id;
}
}
}
#endif /* MBEDTLS_PSA_CRYPTO_C */