2018-11-30 17:54:54 +00:00
|
|
|
/*
|
|
|
|
* PSA crypto layer on top of Mbed TLS crypto
|
|
|
|
*/
|
2020-06-15 09:59:37 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018, ARM Limited, All Rights Reserved
|
2018-11-30 17:54:54 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
|
|
|
*/
|
|
|
|
|
2020-06-02 23:43:33 +00:00
|
|
|
#include "common.h"
|
2018-11-30 17:54:54 +00:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
|
|
|
|
2019-02-14 08:28:02 +00:00
|
|
|
#include "psa_crypto_service_integration.h"
|
2018-11-30 17:54:54 +00:00
|
|
|
#include "psa/crypto.h"
|
|
|
|
|
2018-12-10 15:29:04 +00:00
|
|
|
#include "psa_crypto_core.h"
|
2018-11-30 17:54:54 +00:00
|
|
|
#include "psa_crypto_slot_management.h"
|
|
|
|
#include "psa_crypto_storage.h"
|
2019-07-30 19:32:04 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
|
|
|
#include "psa_crypto_se.h"
|
|
|
|
#endif
|
2018-11-30 17:54:54 +00:00
|
|
|
|
|
|
|
#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 ) ) )
|
|
|
|
|
2018-12-10 15:29:04 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
psa_key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
|
|
|
|
unsigned key_slots_initialized : 1;
|
|
|
|
} psa_global_data_t;
|
|
|
|
|
2018-12-12 13:05:08 +00:00
|
|
|
static psa_global_data_t global_data;
|
2018-12-10 15:29:04 +00:00
|
|
|
|
|
|
|
/* Access a key slot at the given handle. The handle of a key slot is
|
|
|
|
* the index of the slot in the global slot array, plus one so that handles
|
|
|
|
* start at 1 and not 0. */
|
|
|
|
psa_status_t psa_get_key_slot( psa_key_handle_t handle,
|
|
|
|
psa_key_slot_t **p_slot )
|
|
|
|
{
|
|
|
|
psa_key_slot_t *slot = NULL;
|
|
|
|
|
|
|
|
if( ! global_data.key_slots_initialized )
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
/* 0 is not a valid handle under any circumstance. This
|
|
|
|
* implementation provides slots number 1 to N where N is the
|
|
|
|
* number of available slots. */
|
|
|
|
if( handle == 0 || handle > ARRAY_LENGTH( global_data.key_slots ) )
|
|
|
|
return( PSA_ERROR_INVALID_HANDLE );
|
|
|
|
slot = &global_data.key_slots[handle - 1];
|
|
|
|
|
2019-07-31 13:01:55 +00:00
|
|
|
/* If the slot isn't occupied, the handle is invalid. */
|
|
|
|
if( ! psa_is_key_slot_occupied( slot ) )
|
2018-12-10 15:29:04 +00:00
|
|
|
return( PSA_ERROR_INVALID_HANDLE );
|
|
|
|
|
|
|
|
*p_slot = slot;
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
psa_status_t psa_initialize_key_slots( void )
|
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
void psa_wipe_all_key_slots( void )
|
|
|
|
{
|
|
|
|
psa_key_handle_t key;
|
|
|
|
for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
|
|
|
|
{
|
|
|
|
psa_key_slot_t *slot = &global_data.key_slots[key - 1];
|
|
|
|
(void) psa_wipe_key_slot( slot );
|
|
|
|
}
|
|
|
|
global_data.key_slots_initialized = 0;
|
|
|
|
}
|
|
|
|
|
2019-08-07 16:19:59 +00:00
|
|
|
psa_status_t psa_get_empty_key_slot( psa_key_handle_t *handle,
|
2019-05-27 17:01:54 +00:00
|
|
|
psa_key_slot_t **p_slot )
|
2018-12-10 15:29:04 +00:00
|
|
|
{
|
2019-05-27 17:01:54 +00:00
|
|
|
if( ! global_data.key_slots_initialized )
|
|
|
|
return( PSA_ERROR_BAD_STATE );
|
|
|
|
|
2018-12-10 15:29:04 +00:00
|
|
|
for( *handle = PSA_KEY_SLOT_COUNT; *handle != 0; --( *handle ) )
|
|
|
|
{
|
2019-05-27 17:01:54 +00:00
|
|
|
*p_slot = &global_data.key_slots[*handle - 1];
|
2019-07-31 13:01:55 +00:00
|
|
|
if( ! psa_is_key_slot_occupied( *p_slot ) )
|
2018-12-10 15:29:04 +00:00
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
2019-05-27 17:01:54 +00:00
|
|
|
*p_slot = NULL;
|
2018-12-10 15:29:04 +00:00
|
|
|
return( PSA_ERROR_INSUFFICIENT_MEMORY );
|
|
|
|
}
|
|
|
|
|
2018-12-10 15:48:53 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
|
2019-07-30 18:30:51 +00:00
|
|
|
static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *slot )
|
2018-12-10 15:48:53 +00:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_SUCCESS;
|
|
|
|
uint8_t *key_data = NULL;
|
|
|
|
size_t key_data_length = 0;
|
|
|
|
|
2019-07-30 18:30:51 +00:00
|
|
|
status = psa_load_persistent_key( &slot->attr,
|
2019-07-23 09:58:03 +00:00
|
|
|
&key_data, &key_data_length );
|
2018-12-10 15:48:53 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
2019-07-23 14:13:14 +00:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2019-07-30 18:30:51 +00:00
|
|
|
if( psa_key_lifetime_is_external( slot->attr.lifetime ) )
|
2019-07-23 14:13:14 +00:00
|
|
|
{
|
2019-07-30 19:32:04 +00:00
|
|
|
psa_se_key_data_storage_t *data;
|
|
|
|
if( key_data_length != sizeof( *data ) )
|
2019-07-23 14:13:14 +00:00
|
|
|
{
|
|
|
|
status = PSA_ERROR_STORAGE_FAILURE;
|
|
|
|
goto exit;
|
|
|
|
}
|
2019-07-30 19:32:04 +00:00
|
|
|
data = (psa_se_key_data_storage_t *) key_data;
|
|
|
|
memcpy( &slot->data.se.slot_number, &data->slot_number,
|
|
|
|
sizeof( slot->data.se.slot_number ) );
|
|
|
|
memcpy( &slot->attr.bits, &data->bits,
|
|
|
|
sizeof( slot->attr.bits ) );
|
2019-07-23 14:13:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
{
|
2019-07-30 18:30:51 +00:00
|
|
|
status = psa_import_key_into_slot( slot, key_data, key_data_length );
|
2019-07-23 14:13:14 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 15:48:53 +00:00
|
|
|
exit:
|
|
|
|
psa_free_persistent_key_data( key_data, key_data_length );
|
|
|
|
return( status );
|
|
|
|
}
|
2019-02-19 12:04:02 +00:00
|
|
|
|
|
|
|
/** Check whether a key identifier is acceptable.
|
|
|
|
*
|
|
|
|
* For backward compatibility, key identifiers that were valid in a
|
|
|
|
* past released version must remain valid, unless a migration path
|
|
|
|
* is provided.
|
|
|
|
*
|
Support encoding an owner in key file IDs
Differentiate between _key identifiers_, which are always `uint32_t`,
and _key file identifiers_, which are platform-dependent. Normally,
the two are the same.
In `psa/crypto_platform.h`, define `psa_app_key_id_t` (which is always
32 bits, the standard key identifier type) and
`psa_key_file_id_t` (which will be different in some service builds).
A subsequent commit will introduce a platform where the two are different.
It would make sense for the function declarations in `psa/crypto.h` to
use `psa_key_file_id_t`. However this file is currently part of the
PSA Crypto API specification, so it must stick to the standard type
`psa_key_id_t`. Hence, as long as the specification and Mbed Crypto
are not separate, use the implementation-specific file
`psa/crypto_platform.h` to define `psa_key_id_t` as `psa_key_file_id_t`.
In the library, systematically use `psa_key_file_id_t`.
perl -i -pe 's/psa_key_id_t/psa_key_file_id_t/g' library/*.[hc]
2019-02-19 12:24:37 +00:00
|
|
|
* \param file_id The key identifier to check.
|
2019-05-06 16:56:30 +00:00
|
|
|
* \param vendor_ok Nonzero to allow key ids in the vendor range.
|
|
|
|
* 0 to allow only key ids in the application range.
|
2019-02-19 12:04:02 +00:00
|
|
|
*
|
Support encoding an owner in key file IDs
Differentiate between _key identifiers_, which are always `uint32_t`,
and _key file identifiers_, which are platform-dependent. Normally,
the two are the same.
In `psa/crypto_platform.h`, define `psa_app_key_id_t` (which is always
32 bits, the standard key identifier type) and
`psa_key_file_id_t` (which will be different in some service builds).
A subsequent commit will introduce a platform where the two are different.
It would make sense for the function declarations in `psa/crypto.h` to
use `psa_key_file_id_t`. However this file is currently part of the
PSA Crypto API specification, so it must stick to the standard type
`psa_key_id_t`. Hence, as long as the specification and Mbed Crypto
are not separate, use the implementation-specific file
`psa/crypto_platform.h` to define `psa_key_id_t` as `psa_key_file_id_t`.
In the library, systematically use `psa_key_file_id_t`.
perl -i -pe 's/psa_key_id_t/psa_key_file_id_t/g' library/*.[hc]
2019-02-19 12:24:37 +00:00
|
|
|
* \return 1 if \p file_id is acceptable, otherwise 0.
|
2019-02-19 12:04:02 +00:00
|
|
|
*/
|
2019-05-06 16:56:30 +00:00
|
|
|
static int psa_is_key_id_valid( psa_key_file_id_t file_id,
|
|
|
|
int vendor_ok )
|
2019-02-19 12:04:02 +00:00
|
|
|
{
|
Support encoding an owner in key file IDs
Differentiate between _key identifiers_, which are always `uint32_t`,
and _key file identifiers_, which are platform-dependent. Normally,
the two are the same.
In `psa/crypto_platform.h`, define `psa_app_key_id_t` (which is always
32 bits, the standard key identifier type) and
`psa_key_file_id_t` (which will be different in some service builds).
A subsequent commit will introduce a platform where the two are different.
It would make sense for the function declarations in `psa/crypto.h` to
use `psa_key_file_id_t`. However this file is currently part of the
PSA Crypto API specification, so it must stick to the standard type
`psa_key_id_t`. Hence, as long as the specification and Mbed Crypto
are not separate, use the implementation-specific file
`psa/crypto_platform.h` to define `psa_key_id_t` as `psa_key_file_id_t`.
In the library, systematically use `psa_key_file_id_t`.
perl -i -pe 's/psa_key_id_t/psa_key_file_id_t/g' library/*.[hc]
2019-02-19 12:24:37 +00:00
|
|
|
psa_app_key_id_t key_id = PSA_KEY_FILE_GET_KEY_ID( file_id );
|
2019-05-15 16:42:09 +00:00
|
|
|
if( PSA_KEY_ID_USER_MIN <= key_id && key_id <= PSA_KEY_ID_USER_MAX )
|
|
|
|
return( 1 );
|
|
|
|
else if( vendor_ok &&
|
|
|
|
PSA_KEY_ID_VENDOR_MIN <= key_id &&
|
|
|
|
key_id <= PSA_KEY_ID_VENDOR_MAX )
|
|
|
|
return( 1 );
|
|
|
|
else
|
2019-02-19 12:04:02 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
2019-04-25 11:47:40 +00:00
|
|
|
#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
|
2018-12-10 15:48:53 +00:00
|
|
|
|
2020-06-17 12:52:05 +00:00
|
|
|
psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime,
|
2020-06-08 16:37:19 +00:00
|
|
|
psa_se_drv_table_entry_t **p_drv )
|
2019-04-19 16:19:40 +00:00
|
|
|
{
|
2020-06-08 16:37:19 +00:00
|
|
|
if ( psa_key_lifetime_is_external( lifetime ) )
|
2019-06-26 16:34:38 +00:00
|
|
|
{
|
2020-06-08 16:37:19 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
2020-06-08 16:54:23 +00:00
|
|
|
psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry( lifetime );
|
|
|
|
if( driver == NULL )
|
2019-06-26 16:34:38 +00:00
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
2020-06-08 16:37:19 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (p_drv != NULL)
|
2020-06-08 16:54:23 +00:00
|
|
|
*p_drv = driver;
|
2020-06-08 16:37:19 +00:00
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
(void) p_drv;
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
2019-06-26 16:34:38 +00:00
|
|
|
}
|
|
|
|
else
|
2020-06-08 16:37:19 +00:00
|
|
|
/* Local/internal keys are always valid */
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
2019-04-25 11:47:40 +00:00
|
|
|
|
2020-06-17 12:52:05 +00:00
|
|
|
psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime,
|
|
|
|
psa_key_id_t key_id )
|
2020-06-08 16:37:19 +00:00
|
|
|
{
|
|
|
|
if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
|
|
|
|
{
|
|
|
|
/* Volatile keys are always supported */
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Persistent keys require storage support */
|
2019-04-25 11:47:40 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
|
2020-06-17 12:52:05 +00:00
|
|
|
if( psa_is_key_id_valid( key_id,
|
2020-06-08 16:37:19 +00:00
|
|
|
psa_key_lifetime_is_external( lifetime ) ) )
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
else
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
2019-04-25 11:47:40 +00:00
|
|
|
#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
|
2020-06-18 12:37:31 +00:00
|
|
|
(void) key_id;
|
2020-06-08 16:37:19 +00:00
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
2019-04-25 11:47:40 +00:00
|
|
|
#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
|
2020-06-08 16:37:19 +00:00
|
|
|
}
|
2019-04-19 16:19:40 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 17:04:07 +00:00
|
|
|
psa_status_t psa_open_key( psa_key_file_id_t id, psa_key_handle_t *handle )
|
2018-11-30 17:54:54 +00:00
|
|
|
{
|
2019-05-27 17:04:07 +00:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
|
2018-11-30 17:54:54 +00:00
|
|
|
psa_status_t status;
|
2019-05-27 17:01:54 +00:00
|
|
|
psa_key_slot_t *slot;
|
2018-11-30 17:54:54 +00:00
|
|
|
|
|
|
|
*handle = 0;
|
|
|
|
|
2020-06-08 16:37:19 +00:00
|
|
|
if( ! psa_is_key_id_valid( id, 1 ) )
|
|
|
|
return( PSA_ERROR_INVALID_ARGUMENT );
|
2018-11-30 17:54:54 +00:00
|
|
|
|
2019-08-07 16:19:59 +00:00
|
|
|
status = psa_get_empty_key_slot( handle, &slot );
|
2018-11-30 17:54:54 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
|
2019-07-30 18:06:31 +00:00
|
|
|
slot->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
|
|
|
|
slot->attr.id = id;
|
2019-05-27 17:01:54 +00:00
|
|
|
|
|
|
|
status = psa_load_persistent_key_into_slot( slot );
|
2019-05-27 17:04:07 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
2018-11-30 17:54:54 +00:00
|
|
|
{
|
2019-05-27 17:01:54 +00:00
|
|
|
psa_wipe_key_slot( slot );
|
2018-11-30 17:54:54 +00:00
|
|
|
*handle = 0;
|
|
|
|
}
|
|
|
|
return( status );
|
2019-05-27 17:04:07 +00:00
|
|
|
|
2019-04-25 11:47:40 +00:00
|
|
|
#else /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
|
2019-05-27 17:04:07 +00:00
|
|
|
(void) id;
|
|
|
|
*handle = 0;
|
2019-04-25 11:47:40 +00:00
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
#endif /* !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
|
2018-11-30 17:54:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
psa_status_t psa_close_key( psa_key_handle_t handle )
|
|
|
|
{
|
2019-05-27 17:01:54 +00:00
|
|
|
psa_status_t status;
|
|
|
|
psa_key_slot_t *slot;
|
|
|
|
|
2019-10-08 13:48:25 +00:00
|
|
|
if( handle == 0 )
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
|
2019-05-27 17:01:54 +00:00
|
|
|
status = psa_get_key_slot( handle, &slot );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
|
|
|
|
return( psa_wipe_key_slot( slot ) );
|
2018-11-30 17:54:54 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 18:32:30 +00:00
|
|
|
void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats )
|
|
|
|
{
|
|
|
|
psa_key_handle_t key;
|
|
|
|
memset( stats, 0, sizeof( *stats ) );
|
|
|
|
for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
|
|
|
|
{
|
2019-07-31 13:01:55 +00:00
|
|
|
const psa_key_slot_t *slot = &global_data.key_slots[key - 1];
|
|
|
|
if( ! psa_is_key_slot_occupied( slot ) )
|
2019-05-23 18:32:30 +00:00
|
|
|
{
|
2019-07-31 13:01:55 +00:00
|
|
|
++stats->empty_slots;
|
2019-05-23 18:32:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-07-30 18:06:31 +00:00
|
|
|
if( slot->attr.lifetime == PSA_KEY_LIFETIME_VOLATILE )
|
2019-05-23 18:32:30 +00:00
|
|
|
++stats->volatile_slots;
|
2019-07-30 18:06:31 +00:00
|
|
|
else if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
|
2019-05-23 18:32:30 +00:00
|
|
|
{
|
2019-08-20 16:43:48 +00:00
|
|
|
psa_app_key_id_t id = PSA_KEY_FILE_GET_KEY_ID(slot->attr.id);
|
2019-05-23 18:32:30 +00:00
|
|
|
++stats->persistent_slots;
|
2019-08-20 16:43:48 +00:00
|
|
|
if( id > stats->max_open_internal_key_id )
|
|
|
|
stats->max_open_internal_key_id = id;
|
2019-05-23 18:32:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-08-20 16:43:48 +00:00
|
|
|
psa_app_key_id_t id = PSA_KEY_FILE_GET_KEY_ID(slot->attr.id);
|
2019-05-23 18:32:30 +00:00
|
|
|
++stats->external_slots;
|
2019-08-20 16:43:48 +00:00
|
|
|
if( id > stats->max_open_external_key_id )
|
|
|
|
stats->max_open_external_key_id = id;
|
2019-05-23 18:32:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 17:54:54 +00:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_C */
|