2018-11-20 17:33:41 +01:00
|
|
|
/*
|
|
|
|
* PSA storage backend for persistent keys using psa_its APIs.
|
|
|
|
*/
|
|
|
|
/* Copyright (C) 2018, ARM Limited, All Rights Reserved
|
|
|
|
* 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)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_CONFIG_FILE)
|
|
|
|
#include MBEDTLS_CONFIG_FILE
|
|
|
|
#else
|
|
|
|
#include "mbedtls/config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_ITS_C)
|
|
|
|
|
2019-02-14 12:48:10 +01:00
|
|
|
#include "psa/error.h"
|
2019-02-14 09:28:02 +01:00
|
|
|
#include "psa_crypto_service_integration.h"
|
2018-11-20 17:33:41 +01:00
|
|
|
#include "psa/crypto.h"
|
|
|
|
#include "psa_crypto_storage_backend.h"
|
2019-01-24 13:32:11 +01:00
|
|
|
#include "psa/internal_trusted_storage.h"
|
2018-11-20 17:33:41 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
#endif
|
|
|
|
|
2019-02-19 14:16:17 +01:00
|
|
|
/* Determine a file name (ITS file identifier) for the given key file
|
|
|
|
* identifier. The file name must be distinct from any file that is used
|
|
|
|
* for a purpose other than storing a key. Currently, the only such file
|
|
|
|
* is the random seed file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID
|
|
|
|
* and whose value is 0xFFFFFF52. */
|
|
|
|
static psa_storage_uid_t psa_its_identifier_of_slot( psa_key_file_id_t file_id )
|
2018-11-20 17:33:41 +01:00
|
|
|
{
|
2019-02-19 14:16:17 +01:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER) && \
|
|
|
|
defined(PSA_CRYPTO_SECURE)
|
|
|
|
/* Encode the owner in the upper 32 bits. This means that if
|
|
|
|
* owner values are nonzero (as they are on a PSA platform),
|
|
|
|
* no key file will ever have a value less than 0x100000000, so
|
|
|
|
* the whole range 0..0xffffffff is available for non-key files. */
|
|
|
|
uint32_t unsigned_owner = (uint32_t) file_id.owner;
|
|
|
|
return( (uint64_t) unsigned_owner << 32 | file_id.key_id );
|
|
|
|
#else
|
|
|
|
/* Use the key id directly as a file name.
|
|
|
|
* psa_is_key_file_id_valid() in psa_crypto_slot_management.c
|
|
|
|
* is responsible for ensuring that key identifiers do not have a
|
|
|
|
* value that is reserved for non-key files. */
|
|
|
|
return( file_id );
|
|
|
|
#endif
|
2018-11-20 17:33:41 +01: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 13:24:37 +01:00
|
|
|
psa_status_t psa_crypto_storage_load( const psa_key_file_id_t key, uint8_t *data,
|
2018-11-20 17:33:41 +01:00
|
|
|
size_t data_size )
|
|
|
|
{
|
|
|
|
psa_status_t status;
|
2019-02-18 12:56:26 +01:00
|
|
|
psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
|
|
|
|
struct psa_storage_info_t data_identifier_info;
|
2018-11-20 17:33:41 +01:00
|
|
|
|
2019-02-18 12:56:26 +01:00
|
|
|
status = psa_its_get_info( data_identifier, &data_identifier_info );
|
|
|
|
if( status != PSA_SUCCESS )
|
2018-11-20 17:33:41 +01:00
|
|
|
return( status );
|
|
|
|
|
2019-02-18 12:56:26 +01:00
|
|
|
status = psa_its_get( data_identifier, 0, data_size, data );
|
2018-11-20 17:33:41 +01:00
|
|
|
|
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
|
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 13:24:37 +01:00
|
|
|
int psa_is_key_present_in_storage( const psa_key_file_id_t key )
|
2018-11-20 17:33:41 +01:00
|
|
|
{
|
2019-02-18 12:56:26 +01:00
|
|
|
psa_status_t ret;
|
|
|
|
psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
|
|
|
|
struct psa_storage_info_t data_identifier_info;
|
2018-11-20 17:33:41 +01:00
|
|
|
|
|
|
|
ret = psa_its_get_info( data_identifier, &data_identifier_info );
|
|
|
|
|
2019-02-18 12:56:26 +01:00
|
|
|
if( ret == PSA_ERROR_DOES_NOT_EXIST )
|
2018-11-20 17:33:41 +01:00
|
|
|
return( 0 );
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
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 13:24:37 +01:00
|
|
|
psa_status_t psa_crypto_storage_store( const psa_key_file_id_t key,
|
2018-11-20 17:33:41 +01:00
|
|
|
const uint8_t *data,
|
|
|
|
size_t data_length )
|
|
|
|
{
|
|
|
|
psa_status_t status;
|
2019-02-18 12:56:26 +01:00
|
|
|
psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
|
|
|
|
struct psa_storage_info_t data_identifier_info;
|
2018-11-20 17:33:41 +01:00
|
|
|
|
|
|
|
if( psa_is_key_present_in_storage( key ) == 1 )
|
2019-02-14 12:48:10 +01:00
|
|
|
return( PSA_ERROR_ALREADY_EXISTS );
|
2018-11-20 17:33:41 +01:00
|
|
|
|
2019-02-18 12:56:26 +01:00
|
|
|
status = psa_its_set( data_identifier, data_length, data, 0 );
|
2018-11-20 17:33:41 +01:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
return( PSA_ERROR_STORAGE_FAILURE );
|
|
|
|
}
|
|
|
|
|
2019-02-18 12:56:26 +01:00
|
|
|
status = psa_its_get_info( data_identifier, &data_identifier_info );
|
2018-11-20 17:33:41 +01:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( data_identifier_info.size != data_length )
|
|
|
|
{
|
|
|
|
status = PSA_ERROR_STORAGE_FAILURE;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
psa_its_remove( data_identifier );
|
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
|
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 13:24:37 +01:00
|
|
|
psa_status_t psa_destroy_persistent_key( const psa_key_file_id_t key )
|
2018-11-20 17:33:41 +01:00
|
|
|
{
|
2019-02-18 12:56:26 +01:00
|
|
|
psa_status_t ret;
|
|
|
|
psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
|
|
|
|
struct psa_storage_info_t data_identifier_info;
|
2018-11-20 17:33:41 +01:00
|
|
|
|
|
|
|
ret = psa_its_get_info( data_identifier, &data_identifier_info );
|
2019-02-18 12:56:26 +01:00
|
|
|
if( ret == PSA_ERROR_DOES_NOT_EXIST )
|
2018-11-20 17:33:41 +01:00
|
|
|
return( PSA_SUCCESS );
|
|
|
|
|
2019-02-18 12:56:26 +01:00
|
|
|
if( psa_its_remove( data_identifier ) != PSA_SUCCESS )
|
2018-11-20 17:33:41 +01:00
|
|
|
return( PSA_ERROR_STORAGE_FAILURE );
|
|
|
|
|
|
|
|
ret = psa_its_get_info( data_identifier, &data_identifier_info );
|
2019-02-18 12:56:26 +01:00
|
|
|
if( ret != PSA_ERROR_DOES_NOT_EXIST )
|
2018-11-20 17:33:41 +01:00
|
|
|
return( PSA_ERROR_STORAGE_FAILURE );
|
|
|
|
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
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 13:24:37 +01:00
|
|
|
psa_status_t psa_crypto_storage_get_data_length( const psa_key_file_id_t key,
|
2018-11-20 17:33:41 +01:00
|
|
|
size_t *data_length )
|
|
|
|
{
|
|
|
|
psa_status_t status;
|
2019-02-18 12:56:26 +01:00
|
|
|
psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
|
|
|
|
struct psa_storage_info_t data_identifier_info;
|
2018-11-20 17:33:41 +01:00
|
|
|
|
2019-02-18 12:56:26 +01:00
|
|
|
status = psa_its_get_info( data_identifier, &data_identifier_info );
|
2018-11-20 17:33:41 +01:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
|
|
|
|
*data_length = (size_t) data_identifier_info.size;
|
|
|
|
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_ITS_C */
|