blob: 95671374837981e6480bd2b2ca2a01038daae8e2 [file] [log] [blame]
Gilles Peskine6194dc22018-11-16 22:24:15 +01001/*
2 * PSA ITS simulator over stdio files.
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine6194dc22018-11-16 22:24:15 +01007 */
8
Mateusz Starzyk03f00302021-05-27 14:40:40 +02009#include "common.h"
Gilles Peskine6194dc22018-11-16 22:24:15 +010010
11#if defined(MBEDTLS_PSA_ITS_FILE_C)
12
Gilles Peskine6194dc22018-11-16 22:24:15 +010013#include "mbedtls/platform.h"
Gilles Peskine6194dc22018-11-16 22:24:15 +010014
Darryl Greenb4679342019-04-10 15:37:06 +010015#if defined(_WIN32)
16#include <windows.h>
17#endif
18
Gilles Peskine6194dc22018-11-16 22:24:15 +010019#include "psa_crypto_its.h"
20
21#include <limits.h>
22#include <stdint.h>
23#include <stdio.h>
24#include <string.h>
25
Simon D Hughesbda5a212019-07-10 16:34:21 +010026#if !defined(PSA_ITS_STORAGE_PREFIX)
Gilles Peskine6194dc22018-11-16 22:24:15 +010027#define PSA_ITS_STORAGE_PREFIX ""
Simon D Hughesbda5a212019-07-10 16:34:21 +010028#endif
Gilles Peskine6194dc22018-11-16 22:24:15 +010029
Paul Elliott3a5a1072020-12-17 18:33:40 +000030#define PSA_ITS_STORAGE_FILENAME_PATTERN "%08x%08x"
Gilles Peskine6194dc22018-11-16 22:24:15 +010031#define PSA_ITS_STORAGE_SUFFIX ".psa_its"
32#define PSA_ITS_STORAGE_FILENAME_LENGTH \
Gilles Peskine449bd832023-01-11 14:50:10 +010033 (sizeof(PSA_ITS_STORAGE_PREFIX) - 1 + /*prefix without terminating 0*/ \
34 16 + /*UID (64-bit number in hex)*/ \
35 sizeof(PSA_ITS_STORAGE_SUFFIX) - 1 + /*suffix without terminating 0*/ \
36 1 /*terminating null byte*/)
Gilles Peskine6194dc22018-11-16 22:24:15 +010037#define PSA_ITS_STORAGE_TEMP \
38 PSA_ITS_STORAGE_PREFIX "tempfile" PSA_ITS_STORAGE_SUFFIX
39
40/* The maximum value of psa_storage_info_t.size */
41#define PSA_ITS_MAX_SIZE 0xffffffff
42
43#define PSA_ITS_MAGIC_STRING "PSA\0ITS\0"
44#define PSA_ITS_MAGIC_LENGTH 8
45
Darryl Green86095bc2019-04-11 14:21:14 +010046/* As rename fails on Windows if the new filepath already exists,
47 * use MoveFileExA with the MOVEFILE_REPLACE_EXISTING flag instead.
48 * Returns 0 on success, nonzero on failure. */
Darryl Greenfdda7de2019-04-11 12:54:02 +010049#if defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010050#define rename_replace_existing(oldpath, newpath) \
51 (!MoveFileExA(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
Darryl Greenfdda7de2019-04-11 12:54:02 +010052#else
Gilles Peskine449bd832023-01-11 14:50:10 +010053#define rename_replace_existing(oldpath, newpath) rename(oldpath, newpath)
Darryl Greenfdda7de2019-04-11 12:54:02 +010054#endif
55
Gilles Peskine449bd832023-01-11 14:50:10 +010056typedef struct {
Gilles Peskine6194dc22018-11-16 22:24:15 +010057 uint8_t magic[PSA_ITS_MAGIC_LENGTH];
Gilles Peskine449bd832023-01-11 14:50:10 +010058 uint8_t size[sizeof(uint32_t)];
59 uint8_t flags[sizeof(psa_storage_create_flags_t)];
Gilles Peskine6194dc22018-11-16 22:24:15 +010060} psa_its_file_header_t;
61
Gilles Peskine449bd832023-01-11 14:50:10 +010062static void psa_its_fill_filename(psa_storage_uid_t uid, char *filename)
Gilles Peskine6194dc22018-11-16 22:24:15 +010063{
64 /* Break up the UID into two 32-bit pieces so as not to rely on
65 * long long support in snprintf. */
Gilles Peskine449bd832023-01-11 14:50:10 +010066 mbedtls_snprintf(filename, PSA_ITS_STORAGE_FILENAME_LENGTH,
67 "%s" PSA_ITS_STORAGE_FILENAME_PATTERN "%s",
68 PSA_ITS_STORAGE_PREFIX,
69 (unsigned) (uid >> 32),
70 (unsigned) (uid & 0xffffffff),
71 PSA_ITS_STORAGE_SUFFIX);
Gilles Peskine6194dc22018-11-16 22:24:15 +010072}
73
Gilles Peskine449bd832023-01-11 14:50:10 +010074static psa_status_t psa_its_read_file(psa_storage_uid_t uid,
75 struct psa_storage_info_t *p_info,
76 FILE **p_stream)
Gilles Peskine6194dc22018-11-16 22:24:15 +010077{
78 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
79 psa_its_file_header_t header;
80 size_t n;
81
82 *p_stream = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +010083 psa_its_fill_filename(uid, filename);
84 *p_stream = fopen(filename, "rb");
85 if (*p_stream == NULL) {
86 return PSA_ERROR_DOES_NOT_EXIST;
87 }
Gilles Peskine6194dc22018-11-16 22:24:15 +010088
Gilles Peskineda0913b2022-06-30 17:03:40 +020089 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +010090 mbedtls_setbuf(*p_stream, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +020091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 n = fread(&header, 1, sizeof(header), *p_stream);
93 if (n != sizeof(header)) {
94 return PSA_ERROR_DATA_CORRUPT;
95 }
96 if (memcmp(header.magic, PSA_ITS_MAGIC_STRING,
97 PSA_ITS_MAGIC_LENGTH) != 0) {
98 return PSA_ERROR_DATA_CORRUPT;
99 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100100
Dave Rodgmana3d0f612023-11-03 23:34:02 +0000101 p_info->size = MBEDTLS_GET_UINT32_LE(header.size, 0);
102 p_info->flags = MBEDTLS_GET_UINT32_LE(header.flags, 0);
103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 return PSA_SUCCESS;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100105}
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107psa_status_t psa_its_get_info(psa_storage_uid_t uid,
108 struct psa_storage_info_t *p_info)
Gilles Peskine6194dc22018-11-16 22:24:15 +0100109{
110 psa_status_t status;
111 FILE *stream = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 status = psa_its_read_file(uid, p_info, &stream);
113 if (stream != NULL) {
114 fclose(stream);
115 }
116 return status;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100117}
118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119psa_status_t psa_its_get(psa_storage_uid_t uid,
120 uint32_t data_offset,
121 uint32_t data_length,
122 void *p_data,
123 size_t *p_data_length)
Gilles Peskine6194dc22018-11-16 22:24:15 +0100124{
125 psa_status_t status;
126 FILE *stream = NULL;
127 size_t n;
128 struct psa_storage_info_t info;
129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 status = psa_its_read_file(uid, &info, &stream);
131 if (status != PSA_SUCCESS) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100132 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 }
Gilles Peskinebc1f2722018-11-16 22:24:38 +0100134 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 if (data_offset + data_length < data_offset) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100136 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100138#if SIZE_MAX < 0xffffffff
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 if (data_offset + data_length > SIZE_MAX) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100140 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100142#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 if (data_offset + data_length > info.size) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100144 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100146
Gilles Peskinebc1f2722018-11-16 22:24:38 +0100147 status = PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100148#if LONG_MAX < 0xffffffff
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 while (data_offset > LONG_MAX) {
150 if (fseek(stream, LONG_MAX, SEEK_CUR) != 0) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100151 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100153 data_offset -= LONG_MAX;
154 }
155#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 if (fseek(stream, data_offset, SEEK_CUR) != 0) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100157 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 }
159 n = fread(p_data, 1, data_length, stream);
160 if (n != data_length) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100161 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100163 status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 if (p_data_length != NULL) {
Simon D Hughesbda5a212019-07-10 16:34:21 +0100165 *p_data_length = n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100167
168exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 if (stream != NULL) {
170 fclose(stream);
171 }
172 return status;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100173}
174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175psa_status_t psa_its_set(psa_storage_uid_t uid,
176 uint32_t data_length,
177 const void *p_data,
178 psa_storage_create_flags_t create_flags)
Gilles Peskine6194dc22018-11-16 22:24:15 +0100179{
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (uid == 0) {
181 return PSA_ERROR_INVALID_HANDLE;
pespaceke9901002022-02-08 13:52:28 +0100182 }
183
Gilles Peskine6194dc22018-11-16 22:24:15 +0100184 psa_status_t status = PSA_ERROR_STORAGE_FAILURE;
185 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
186 FILE *stream = NULL;
187 psa_its_file_header_t header;
188 size_t n;
189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 memcpy(header.magic, PSA_ITS_MAGIC_STRING, PSA_ITS_MAGIC_LENGTH);
191 MBEDTLS_PUT_UINT32_LE(data_length, header.size, 0);
192 MBEDTLS_PUT_UINT32_LE(create_flags, header.flags, 0);
Gilles Peskine6194dc22018-11-16 22:24:15 +0100193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 psa_its_fill_filename(uid, filename);
195 stream = fopen(PSA_ITS_STORAGE_TEMP, "wb");
Gilles Peskineda0913b2022-06-30 17:03:40 +0200196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 if (stream == NULL) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100198 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100200
Gilles Peskineda0913b2022-06-30 17:03:40 +0200201 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 mbedtls_setbuf(stream, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +0200203
Gilles Peskine6194dc22018-11-16 22:24:15 +0100204 status = PSA_ERROR_INSUFFICIENT_STORAGE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 n = fwrite(&header, 1, sizeof(header), stream);
206 if (n != sizeof(header)) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100207 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 }
209 if (data_length != 0) {
210 n = fwrite(p_data, 1, data_length, stream);
211 if (n != data_length) {
Andrzej Kurekdc22d8d2019-09-05 09:34:34 -0400212 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 }
Andrzej Kurekdc22d8d2019-09-05 09:34:34 -0400214 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100215 status = PSA_SUCCESS;
216
217exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 if (stream != NULL) {
219 int ret = fclose(stream);
220 if (status == PSA_SUCCESS && ret != 0) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100221 status = PSA_ERROR_INSUFFICIENT_STORAGE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100223 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if (status == PSA_SUCCESS) {
225 if (rename_replace_existing(PSA_ITS_STORAGE_TEMP, filename) != 0) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100226 status = PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100228 }
Gilles Peskinebab1b522020-08-25 22:49:19 +0200229 /* The temporary file may still exist, but only in failure cases where
230 * we're already reporting an error. So there's nothing we can do on
231 * failure. If the function succeeded, and in some error cases, the
232 * temporary file doesn't exist and so remove() is expected to fail.
233 * Thus we just ignore the return status of remove(). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 (void) remove(PSA_ITS_STORAGE_TEMP);
235 return status;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100236}
237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238psa_status_t psa_its_remove(psa_storage_uid_t uid)
Gilles Peskine6194dc22018-11-16 22:24:15 +0100239{
240 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
241 FILE *stream;
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 psa_its_fill_filename(uid, filename);
243 stream = fopen(filename, "rb");
244 if (stream == NULL) {
245 return PSA_ERROR_DOES_NOT_EXIST;
246 }
247 fclose(stream);
248 if (remove(filename) != 0) {
249 return PSA_ERROR_STORAGE_FAILURE;
250 }
251 return PSA_SUCCESS;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100252}
253
254#endif /* MBEDTLS_PSA_ITS_FILE_C */