2018-11-16 21:24:15 +00:00
|
|
|
/*
|
|
|
|
* PSA ITS simulator over stdio files.
|
|
|
|
*/
|
2020-06-15 09:59:37 +00:00
|
|
|
/*
|
2020-08-07 11:07:28 +00:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2018-11-16 21:24:15 +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.
|
|
|
|
*/
|
|
|
|
|
2021-05-27 12:40:40 +00:00
|
|
|
#include "common.h"
|
2018-11-16 21:24:15 +00:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_ITS_FILE_C)
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
#else
|
|
|
|
#define mbedtls_snprintf snprintf
|
|
|
|
#endif
|
|
|
|
|
2019-04-10 14:37:06 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2018-11-16 21:24:15 +00:00
|
|
|
#include "psa_crypto_its.h"
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-07-10 15:34:21 +00:00
|
|
|
#if !defined(PSA_ITS_STORAGE_PREFIX)
|
2018-11-16 21:24:15 +00:00
|
|
|
#define PSA_ITS_STORAGE_PREFIX ""
|
2019-07-10 15:34:21 +00:00
|
|
|
#endif
|
2018-11-16 21:24:15 +00:00
|
|
|
|
2020-12-17 18:33:40 +00:00
|
|
|
#define PSA_ITS_STORAGE_FILENAME_PATTERN "%08x%08x"
|
2018-11-16 21:24:15 +00:00
|
|
|
#define PSA_ITS_STORAGE_SUFFIX ".psa_its"
|
|
|
|
#define PSA_ITS_STORAGE_FILENAME_LENGTH \
|
|
|
|
( sizeof( PSA_ITS_STORAGE_PREFIX ) - 1 + /*prefix without terminating 0*/ \
|
|
|
|
16 + /*UID (64-bit number in hex)*/ \
|
|
|
|
sizeof( PSA_ITS_STORAGE_SUFFIX ) - 1 + /*suffix without terminating 0*/ \
|
|
|
|
1 /*terminating null byte*/ )
|
|
|
|
#define PSA_ITS_STORAGE_TEMP \
|
|
|
|
PSA_ITS_STORAGE_PREFIX "tempfile" PSA_ITS_STORAGE_SUFFIX
|
|
|
|
|
|
|
|
/* The maximum value of psa_storage_info_t.size */
|
|
|
|
#define PSA_ITS_MAX_SIZE 0xffffffff
|
|
|
|
|
|
|
|
#define PSA_ITS_MAGIC_STRING "PSA\0ITS\0"
|
|
|
|
#define PSA_ITS_MAGIC_LENGTH 8
|
|
|
|
|
2019-04-11 13:21:14 +00:00
|
|
|
/* As rename fails on Windows if the new filepath already exists,
|
|
|
|
* use MoveFileExA with the MOVEFILE_REPLACE_EXISTING flag instead.
|
|
|
|
* Returns 0 on success, nonzero on failure. */
|
2019-04-11 11:54:02 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#define rename_replace_existing( oldpath, newpath ) \
|
2019-04-11 13:21:14 +00:00
|
|
|
( ! MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING ) )
|
2019-04-11 11:54:02 +00:00
|
|
|
#else
|
|
|
|
#define rename_replace_existing( oldpath, newpath ) rename( oldpath, newpath )
|
|
|
|
#endif
|
|
|
|
|
2018-11-16 21:24:15 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint8_t magic[PSA_ITS_MAGIC_LENGTH];
|
|
|
|
uint8_t size[sizeof( uint32_t )];
|
|
|
|
uint8_t flags[sizeof( psa_storage_create_flags_t )];
|
|
|
|
} psa_its_file_header_t;
|
|
|
|
|
|
|
|
static void psa_its_fill_filename( psa_storage_uid_t uid, char *filename )
|
|
|
|
{
|
|
|
|
/* Break up the UID into two 32-bit pieces so as not to rely on
|
|
|
|
* long long support in snprintf. */
|
|
|
|
mbedtls_snprintf( filename, PSA_ITS_STORAGE_FILENAME_LENGTH,
|
|
|
|
"%s" PSA_ITS_STORAGE_FILENAME_PATTERN "%s",
|
|
|
|
PSA_ITS_STORAGE_PREFIX,
|
2020-12-17 18:33:40 +00:00
|
|
|
(unsigned) ( uid >> 32 ),
|
|
|
|
(unsigned) ( uid & 0xffffffff ),
|
2018-11-16 21:24:15 +00:00
|
|
|
PSA_ITS_STORAGE_SUFFIX );
|
|
|
|
}
|
|
|
|
|
|
|
|
static psa_status_t psa_its_read_file( psa_storage_uid_t uid,
|
|
|
|
struct psa_storage_info_t *p_info,
|
|
|
|
FILE **p_stream )
|
|
|
|
{
|
|
|
|
char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
|
|
|
|
psa_its_file_header_t header;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
*p_stream = NULL;
|
|
|
|
psa_its_fill_filename( uid, filename );
|
|
|
|
*p_stream = fopen( filename, "rb" );
|
|
|
|
if( *p_stream == NULL )
|
|
|
|
return( PSA_ERROR_DOES_NOT_EXIST );
|
|
|
|
|
2022-06-30 15:03:40 +00:00
|
|
|
/* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
|
|
|
|
mbedtls_setbuf( *p_stream, NULL );
|
|
|
|
|
2018-11-16 21:24:15 +00:00
|
|
|
n = fread( &header, 1, sizeof( header ), *p_stream );
|
|
|
|
if( n != sizeof( header ) )
|
|
|
|
return( PSA_ERROR_DATA_CORRUPT );
|
|
|
|
if( memcmp( header.magic, PSA_ITS_MAGIC_STRING,
|
|
|
|
PSA_ITS_MAGIC_LENGTH ) != 0 )
|
|
|
|
return( PSA_ERROR_DATA_CORRUPT );
|
|
|
|
|
|
|
|
p_info->size = ( header.size[0] |
|
|
|
|
header.size[1] << 8 |
|
|
|
|
header.size[2] << 16 |
|
|
|
|
header.size[3] << 24 );
|
|
|
|
p_info->flags = ( header.flags[0] |
|
|
|
|
header.flags[1] << 8 |
|
|
|
|
header.flags[2] << 16 |
|
|
|
|
header.flags[3] << 24 );
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
psa_status_t psa_its_get_info( psa_storage_uid_t uid,
|
|
|
|
struct psa_storage_info_t *p_info )
|
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
FILE *stream = NULL;
|
|
|
|
status = psa_its_read_file( uid, p_info, &stream );
|
|
|
|
if( stream != NULL )
|
|
|
|
fclose( stream );
|
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
|
|
|
|
psa_status_t psa_its_get( psa_storage_uid_t uid,
|
|
|
|
uint32_t data_offset,
|
|
|
|
uint32_t data_length,
|
2019-07-10 15:34:21 +00:00
|
|
|
void *p_data,
|
|
|
|
size_t *p_data_length )
|
2018-11-16 21:24:15 +00:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
FILE *stream = NULL;
|
|
|
|
size_t n;
|
|
|
|
struct psa_storage_info_t info;
|
|
|
|
|
|
|
|
status = psa_its_read_file( uid, &info, &stream );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
2018-11-16 21:24:38 +00:00
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
2018-11-16 21:24:15 +00:00
|
|
|
if( data_offset + data_length < data_offset )
|
|
|
|
goto exit;
|
|
|
|
#if SIZE_MAX < 0xffffffff
|
|
|
|
if( data_offset + data_length > SIZE_MAX )
|
|
|
|
goto exit;
|
|
|
|
#endif
|
|
|
|
if( data_offset + data_length > info.size )
|
|
|
|
goto exit;
|
|
|
|
|
2018-11-16 21:24:38 +00:00
|
|
|
status = PSA_ERROR_STORAGE_FAILURE;
|
2018-11-16 21:24:15 +00:00
|
|
|
#if LONG_MAX < 0xffffffff
|
|
|
|
while( data_offset > LONG_MAX )
|
|
|
|
{
|
|
|
|
if( fseek( stream, LONG_MAX, SEEK_CUR ) != 0 )
|
|
|
|
goto exit;
|
|
|
|
data_offset -= LONG_MAX;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if( fseek( stream, data_offset, SEEK_CUR ) != 0 )
|
|
|
|
goto exit;
|
|
|
|
n = fread( p_data, 1, data_length, stream );
|
|
|
|
if( n != data_length )
|
|
|
|
goto exit;
|
|
|
|
status = PSA_SUCCESS;
|
2019-07-10 15:34:21 +00:00
|
|
|
if( p_data_length != NULL )
|
|
|
|
*p_data_length = n;
|
2018-11-16 21:24:15 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
if( stream != NULL )
|
|
|
|
fclose( stream );
|
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
|
|
|
|
psa_status_t psa_its_set( psa_storage_uid_t uid,
|
|
|
|
uint32_t data_length,
|
|
|
|
const void *p_data,
|
|
|
|
psa_storage_create_flags_t create_flags )
|
|
|
|
{
|
2022-02-08 12:52:28 +00:00
|
|
|
if( uid == 0 )
|
|
|
|
{
|
2022-02-11 09:21:16 +00:00
|
|
|
return( PSA_ERROR_INVALID_HANDLE );
|
2022-02-08 12:52:28 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 21:24:15 +00:00
|
|
|
psa_status_t status = PSA_ERROR_STORAGE_FAILURE;
|
|
|
|
char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
|
|
|
|
FILE *stream = NULL;
|
|
|
|
psa_its_file_header_t header;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
memcpy( header.magic, PSA_ITS_MAGIC_STRING, PSA_ITS_MAGIC_LENGTH );
|
2021-07-19 10:56:54 +00:00
|
|
|
MBEDTLS_PUT_UINT32_LE( data_length, header.size, 0 );
|
|
|
|
MBEDTLS_PUT_UINT32_LE( create_flags, header.flags, 0 );
|
2018-11-16 21:24:15 +00:00
|
|
|
|
|
|
|
psa_its_fill_filename( uid, filename );
|
|
|
|
stream = fopen( PSA_ITS_STORAGE_TEMP, "wb" );
|
2022-06-30 15:03:40 +00:00
|
|
|
|
2018-11-16 21:24:15 +00:00
|
|
|
if( stream == NULL )
|
|
|
|
goto exit;
|
|
|
|
|
2022-06-30 15:03:40 +00:00
|
|
|
/* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
|
|
|
|
mbedtls_setbuf( stream, NULL );
|
|
|
|
|
2018-11-16 21:24:15 +00:00
|
|
|
status = PSA_ERROR_INSUFFICIENT_STORAGE;
|
|
|
|
n = fwrite( &header, 1, sizeof( header ), stream );
|
|
|
|
if( n != sizeof( header ) )
|
|
|
|
goto exit;
|
2019-09-05 13:34:34 +00:00
|
|
|
if( data_length != 0 )
|
|
|
|
{
|
|
|
|
n = fwrite( p_data, 1, data_length, stream );
|
|
|
|
if( n != data_length )
|
|
|
|
goto exit;
|
|
|
|
}
|
2018-11-16 21:24:15 +00:00
|
|
|
status = PSA_SUCCESS;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
if( stream != NULL )
|
|
|
|
{
|
|
|
|
int ret = fclose( stream );
|
|
|
|
if( status == PSA_SUCCESS && ret != 0 )
|
|
|
|
status = PSA_ERROR_INSUFFICIENT_STORAGE;
|
|
|
|
}
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
2019-04-11 11:54:02 +00:00
|
|
|
if( rename_replace_existing( PSA_ITS_STORAGE_TEMP, filename ) != 0 )
|
2018-11-16 21:24:15 +00:00
|
|
|
status = PSA_ERROR_STORAGE_FAILURE;
|
|
|
|
}
|
2020-08-25 20:49:19 +00:00
|
|
|
/* The temporary file may still exist, but only in failure cases where
|
|
|
|
* we're already reporting an error. So there's nothing we can do on
|
|
|
|
* failure. If the function succeeded, and in some error cases, the
|
|
|
|
* temporary file doesn't exist and so remove() is expected to fail.
|
|
|
|
* Thus we just ignore the return status of remove(). */
|
|
|
|
(void) remove( PSA_ITS_STORAGE_TEMP );
|
2018-11-16 21:24:15 +00:00
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
|
|
|
|
psa_status_t psa_its_remove( psa_storage_uid_t uid )
|
|
|
|
{
|
|
|
|
char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
|
|
|
|
FILE *stream;
|
|
|
|
psa_its_fill_filename( uid, filename );
|
|
|
|
stream = fopen( filename, "rb" );
|
|
|
|
if( stream == NULL )
|
|
|
|
return( PSA_ERROR_DOES_NOT_EXIST );
|
|
|
|
fclose( stream );
|
|
|
|
if( remove( filename ) != 0 )
|
|
|
|
return( PSA_ERROR_STORAGE_FAILURE );
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_PSA_ITS_FILE_C */
|