2018-11-16 22:24:15 +01:00
|
|
|
/*
|
|
|
|
* PSA ITS simulator over stdio files.
|
|
|
|
*/
|
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-16 22:24:15 +01:00
|
|
|
*/
|
|
|
|
|
2021-05-27 14:40:40 +02:00
|
|
|
#include "common.h"
|
2018-11-16 22:24:15 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_ITS_FILE_C)
|
|
|
|
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
|
2019-04-10 16:37:06 +02:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2018-11-16 22:24:15 +01:00
|
|
|
#include "psa_crypto_its.h"
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-07-10 17:34:21 +02:00
|
|
|
#if !defined(PSA_ITS_STORAGE_PREFIX)
|
2018-11-16 22:24:15 +01:00
|
|
|
#define PSA_ITS_STORAGE_PREFIX ""
|
2019-07-10 17:34:21 +02:00
|
|
|
#endif
|
2018-11-16 22:24:15 +01:00
|
|
|
|
2020-12-17 19:33:40 +01:00
|
|
|
#define PSA_ITS_STORAGE_FILENAME_PATTERN "%08x%08x"
|
2018-11-16 22:24:15 +01:00
|
|
|
#define PSA_ITS_STORAGE_SUFFIX ".psa_its"
|
|
|
|
#define PSA_ITS_STORAGE_FILENAME_LENGTH \
|
2023-01-11 14:50:10 +01:00
|
|
|
(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*/)
|
2018-11-16 22:24:15 +01:00
|
|
|
#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 15:21:14 +02: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 13:54:02 +02:00
|
|
|
#if defined(_WIN32)
|
2023-01-11 14:50:10 +01:00
|
|
|
#define rename_replace_existing(oldpath, newpath) \
|
|
|
|
(!MoveFileExA(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
|
2019-04-11 13:54:02 +02:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
#define rename_replace_existing(oldpath, newpath) rename(oldpath, newpath)
|
2019-04-11 13:54:02 +02:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
typedef struct {
|
2018-11-16 22:24:15 +01:00
|
|
|
uint8_t magic[PSA_ITS_MAGIC_LENGTH];
|
2023-01-11 14:50:10 +01:00
|
|
|
uint8_t size[sizeof(uint32_t)];
|
|
|
|
uint8_t flags[sizeof(psa_storage_create_flags_t)];
|
2018-11-16 22:24:15 +01:00
|
|
|
} psa_its_file_header_t;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void psa_its_fill_filename(psa_storage_uid_t uid, char *filename)
|
2018-11-16 22:24:15 +01:00
|
|
|
{
|
|
|
|
/* Break up the UID into two 32-bit pieces so as not to rely on
|
|
|
|
* long long support in snprintf. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_snprintf(filename, PSA_ITS_STORAGE_FILENAME_LENGTH,
|
|
|
|
"%s" PSA_ITS_STORAGE_FILENAME_PATTERN "%s",
|
|
|
|
PSA_ITS_STORAGE_PREFIX,
|
|
|
|
(unsigned) (uid >> 32),
|
|
|
|
(unsigned) (uid & 0xffffffff),
|
|
|
|
PSA_ITS_STORAGE_SUFFIX);
|
2018-11-16 22:24:15 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static psa_status_t psa_its_read_file(psa_storage_uid_t uid,
|
|
|
|
struct psa_storage_info_t *p_info,
|
|
|
|
FILE **p_stream)
|
2018-11-16 22:24:15 +01:00
|
|
|
{
|
|
|
|
char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
|
|
|
|
psa_its_file_header_t header;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
*p_stream = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_its_fill_filename(uid, filename);
|
|
|
|
*p_stream = fopen(filename, "rb");
|
|
|
|
if (*p_stream == NULL) {
|
|
|
|
return PSA_ERROR_DOES_NOT_EXIST;
|
|
|
|
}
|
2018-11-16 22:24:15 +01:00
|
|
|
|
2022-06-30 17:03:40 +02:00
|
|
|
/* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_setbuf(*p_stream, NULL);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-11-04 00:34:02 +01:00
|
|
|
p_info->size = MBEDTLS_GET_UINT32_LE(header.size, 0);
|
|
|
|
p_info->flags = MBEDTLS_GET_UINT32_LE(header.flags, 0);
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
2018-11-16 22:24:15 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_its_get_info(psa_storage_uid_t uid,
|
|
|
|
struct psa_storage_info_t *p_info)
|
2018-11-16 22:24:15 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
FILE *stream = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_its_read_file(uid, p_info, &stream);
|
|
|
|
if (stream != NULL) {
|
|
|
|
fclose(stream);
|
|
|
|
}
|
|
|
|
return status;
|
2018-11-16 22:24:15 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_its_get(psa_storage_uid_t uid,
|
|
|
|
uint32_t data_offset,
|
|
|
|
uint32_t data_length,
|
|
|
|
void *p_data,
|
|
|
|
size_t *p_data_length)
|
2018-11-16 22:24:15 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
FILE *stream = NULL;
|
|
|
|
size_t n;
|
|
|
|
struct psa_storage_info_t info;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_its_read_file(uid, &info, &stream);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2018-11-16 22:24:15 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-11-16 22:24:38 +01:00
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (data_offset + data_length < data_offset) {
|
2018-11-16 22:24:15 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-11-16 22:24:15 +01:00
|
|
|
#if SIZE_MAX < 0xffffffff
|
2023-01-11 14:50:10 +01:00
|
|
|
if (data_offset + data_length > SIZE_MAX) {
|
2018-11-16 22:24:15 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-11-16 22:24:15 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
if (data_offset + data_length > info.size) {
|
2018-11-16 22:24:15 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-11-16 22:24:15 +01:00
|
|
|
|
2018-11-16 22:24:38 +01:00
|
|
|
status = PSA_ERROR_STORAGE_FAILURE;
|
2018-11-16 22:24:15 +01:00
|
|
|
#if LONG_MAX < 0xffffffff
|
2023-01-11 14:50:10 +01:00
|
|
|
while (data_offset > LONG_MAX) {
|
|
|
|
if (fseek(stream, LONG_MAX, SEEK_CUR) != 0) {
|
2018-11-16 22:24:15 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-11-16 22:24:15 +01:00
|
|
|
data_offset -= LONG_MAX;
|
|
|
|
}
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fseek(stream, data_offset, SEEK_CUR) != 0) {
|
2018-11-16 22:24:15 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
n = fread(p_data, 1, data_length, stream);
|
|
|
|
if (n != data_length) {
|
2018-11-16 22:24:15 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-11-16 22:24:15 +01:00
|
|
|
status = PSA_SUCCESS;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p_data_length != NULL) {
|
2019-07-10 17:34:21 +02:00
|
|
|
*p_data_length = n;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-11-16 22:24:15 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
if (stream != NULL) {
|
|
|
|
fclose(stream);
|
|
|
|
}
|
|
|
|
return status;
|
2018-11-16 22:24:15 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
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)
|
2018-11-16 22:24:15 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (uid == 0) {
|
|
|
|
return PSA_ERROR_INVALID_HANDLE;
|
2022-02-08 13:52:28 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 22:24:15 +01: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;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(header.magic, PSA_ITS_MAGIC_STRING, PSA_ITS_MAGIC_LENGTH);
|
|
|
|
MBEDTLS_PUT_UINT32_LE(data_length, header.size, 0);
|
|
|
|
MBEDTLS_PUT_UINT32_LE(create_flags, header.flags, 0);
|
2018-11-16 22:24:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_its_fill_filename(uid, filename);
|
|
|
|
stream = fopen(PSA_ITS_STORAGE_TEMP, "wb");
|
2022-06-30 17:03:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (stream == NULL) {
|
2018-11-16 22:24:15 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-11-16 22:24:15 +01:00
|
|
|
|
2022-06-30 17:03:40 +02:00
|
|
|
/* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_setbuf(stream, NULL);
|
2022-06-30 17:03:40 +02:00
|
|
|
|
2018-11-16 22:24:15 +01:00
|
|
|
status = PSA_ERROR_INSUFFICIENT_STORAGE;
|
2023-01-11 14:50:10 +01:00
|
|
|
n = fwrite(&header, 1, sizeof(header), stream);
|
|
|
|
if (n != sizeof(header)) {
|
2018-11-16 22:24:15 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
if (data_length != 0) {
|
|
|
|
n = fwrite(p_data, 1, data_length, stream);
|
|
|
|
if (n != data_length) {
|
2019-09-05 15:34:34 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-09-05 15:34:34 +02:00
|
|
|
}
|
2018-11-16 22:24:15 +01:00
|
|
|
status = PSA_SUCCESS;
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
if (stream != NULL) {
|
|
|
|
int ret = fclose(stream);
|
|
|
|
if (status == PSA_SUCCESS && ret != 0) {
|
2018-11-16 22:24:15 +01:00
|
|
|
status = PSA_ERROR_INSUFFICIENT_STORAGE;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-11-16 22:24:15 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
if (rename_replace_existing(PSA_ITS_STORAGE_TEMP, filename) != 0) {
|
2018-11-16 22:24:15 +01:00
|
|
|
status = PSA_ERROR_STORAGE_FAILURE;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-11-16 22:24:15 +01:00
|
|
|
}
|
2020-08-25 22:49:19 +02: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(). */
|
2023-01-11 14:50:10 +01:00
|
|
|
(void) remove(PSA_ITS_STORAGE_TEMP);
|
|
|
|
return status;
|
2018-11-16 22:24:15 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t psa_its_remove(psa_storage_uid_t uid)
|
2018-11-16 22:24:15 +01:00
|
|
|
{
|
|
|
|
char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
|
|
|
|
FILE *stream;
|
2023-01-11 14:50:10 +01:00
|
|
|
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;
|
2018-11-16 22:24:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_PSA_ITS_FILE_C */
|