blob: 97486165e26e215b23baac661969a0799ec183cd [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
Gilles Peskine6194dc22018-11-16 22:24:15 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskine6194dc22018-11-16 22:24:15 +010019 */
20
Mateusz Starzyk03f00302021-05-27 14:40:40 +020021#include "common.h"
Gilles Peskine6194dc22018-11-16 22:24:15 +010022
23#if defined(MBEDTLS_PSA_ITS_FILE_C)
24
Gilles Peskine6194dc22018-11-16 22:24:15 +010025#include "mbedtls/platform.h"
Gilles Peskine6194dc22018-11-16 22:24:15 +010026
Darryl Greenb4679342019-04-10 15:37:06 +010027#if defined(_WIN32)
28#include <windows.h>
29#endif
30
Gilles Peskine6194dc22018-11-16 22:24:15 +010031#include "psa_crypto_its.h"
32
33#include <limits.h>
34#include <stdint.h>
35#include <stdio.h>
36#include <string.h>
37
Simon D Hughesbda5a212019-07-10 16:34:21 +010038#if !defined(PSA_ITS_STORAGE_PREFIX)
Gilles Peskine6194dc22018-11-16 22:24:15 +010039#define PSA_ITS_STORAGE_PREFIX ""
Simon D Hughesbda5a212019-07-10 16:34:21 +010040#endif
Gilles Peskine6194dc22018-11-16 22:24:15 +010041
Paul Elliott3a5a1072020-12-17 18:33:40 +000042#define PSA_ITS_STORAGE_FILENAME_PATTERN "%08x%08x"
Gilles Peskine6194dc22018-11-16 22:24:15 +010043#define PSA_ITS_STORAGE_SUFFIX ".psa_its"
44#define PSA_ITS_STORAGE_FILENAME_LENGTH \
Gilles Peskine449bd832023-01-11 14:50:10 +010045 (sizeof(PSA_ITS_STORAGE_PREFIX) - 1 + /*prefix without terminating 0*/ \
46 16 + /*UID (64-bit number in hex)*/ \
47 sizeof(PSA_ITS_STORAGE_SUFFIX) - 1 + /*suffix without terminating 0*/ \
48 1 /*terminating null byte*/)
Gilles Peskine6194dc22018-11-16 22:24:15 +010049#define PSA_ITS_STORAGE_TEMP \
50 PSA_ITS_STORAGE_PREFIX "tempfile" PSA_ITS_STORAGE_SUFFIX
51
52/* The maximum value of psa_storage_info_t.size */
53#define PSA_ITS_MAX_SIZE 0xffffffff
54
55#define PSA_ITS_MAGIC_STRING "PSA\0ITS\0"
56#define PSA_ITS_MAGIC_LENGTH 8
57
Darryl Green86095bc2019-04-11 14:21:14 +010058/* As rename fails on Windows if the new filepath already exists,
59 * use MoveFileExA with the MOVEFILE_REPLACE_EXISTING flag instead.
60 * Returns 0 on success, nonzero on failure. */
Darryl Greenfdda7de2019-04-11 12:54:02 +010061#if defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010062#define rename_replace_existing(oldpath, newpath) \
63 (!MoveFileExA(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
Darryl Greenfdda7de2019-04-11 12:54:02 +010064#else
Gilles Peskine449bd832023-01-11 14:50:10 +010065#define rename_replace_existing(oldpath, newpath) rename(oldpath, newpath)
Darryl Greenfdda7de2019-04-11 12:54:02 +010066#endif
67
Gilles Peskine449bd832023-01-11 14:50:10 +010068typedef struct {
Gilles Peskine6194dc22018-11-16 22:24:15 +010069 uint8_t magic[PSA_ITS_MAGIC_LENGTH];
Gilles Peskine449bd832023-01-11 14:50:10 +010070 uint8_t size[sizeof(uint32_t)];
71 uint8_t flags[sizeof(psa_storage_create_flags_t)];
Gilles Peskine6194dc22018-11-16 22:24:15 +010072} psa_its_file_header_t;
73
Gilles Peskine449bd832023-01-11 14:50:10 +010074static void psa_its_fill_filename(psa_storage_uid_t uid, char *filename)
Gilles Peskine6194dc22018-11-16 22:24:15 +010075{
76 /* Break up the UID into two 32-bit pieces so as not to rely on
77 * long long support in snprintf. */
Gilles Peskine449bd832023-01-11 14:50:10 +010078 mbedtls_snprintf(filename, PSA_ITS_STORAGE_FILENAME_LENGTH,
79 "%s" PSA_ITS_STORAGE_FILENAME_PATTERN "%s",
80 PSA_ITS_STORAGE_PREFIX,
81 (unsigned) (uid >> 32),
82 (unsigned) (uid & 0xffffffff),
83 PSA_ITS_STORAGE_SUFFIX);
Gilles Peskine6194dc22018-11-16 22:24:15 +010084}
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086static psa_status_t psa_its_read_file(psa_storage_uid_t uid,
87 struct psa_storage_info_t *p_info,
88 FILE **p_stream)
Gilles Peskine6194dc22018-11-16 22:24:15 +010089{
90 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
91 psa_its_file_header_t header;
92 size_t n;
93
94 *p_stream = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +010095 psa_its_fill_filename(uid, filename);
96 *p_stream = fopen(filename, "rb");
97 if (*p_stream == NULL) {
98 return PSA_ERROR_DOES_NOT_EXIST;
99 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100100
Gilles Peskineda0913b2022-06-30 17:03:40 +0200101 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 mbedtls_setbuf(*p_stream, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +0200103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 n = fread(&header, 1, sizeof(header), *p_stream);
105 if (n != sizeof(header)) {
106 return PSA_ERROR_DATA_CORRUPT;
107 }
108 if (memcmp(header.magic, PSA_ITS_MAGIC_STRING,
109 PSA_ITS_MAGIC_LENGTH) != 0) {
110 return PSA_ERROR_DATA_CORRUPT;
111 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 p_info->size = (header.size[0] |
114 header.size[1] << 8 |
115 header.size[2] << 16 |
116 header.size[3] << 24);
117 p_info->flags = (header.flags[0] |
118 header.flags[1] << 8 |
119 header.flags[2] << 16 |
120 header.flags[3] << 24);
121 return PSA_SUCCESS;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100122}
123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124psa_status_t psa_its_get_info(psa_storage_uid_t uid,
125 struct psa_storage_info_t *p_info)
Gilles Peskine6194dc22018-11-16 22:24:15 +0100126{
127 psa_status_t status;
128 FILE *stream = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 status = psa_its_read_file(uid, p_info, &stream);
130 if (stream != NULL) {
131 fclose(stream);
132 }
133 return status;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100134}
135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136psa_status_t psa_its_get(psa_storage_uid_t uid,
137 uint32_t data_offset,
138 uint32_t data_length,
139 void *p_data,
140 size_t *p_data_length)
Gilles Peskine6194dc22018-11-16 22:24:15 +0100141{
142 psa_status_t status;
143 FILE *stream = NULL;
144 size_t n;
145 struct psa_storage_info_t info;
146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 status = psa_its_read_file(uid, &info, &stream);
148 if (status != PSA_SUCCESS) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100149 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 }
Gilles Peskinebc1f2722018-11-16 22:24:38 +0100151 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 if (data_offset + data_length < data_offset) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100153 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100155#if SIZE_MAX < 0xffffffff
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 if (data_offset + data_length > SIZE_MAX) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100157 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100159#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 if (data_offset + data_length > info.size) {
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
Gilles Peskinebc1f2722018-11-16 22:24:38 +0100164 status = PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100165#if LONG_MAX < 0xffffffff
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 while (data_offset > LONG_MAX) {
167 if (fseek(stream, LONG_MAX, SEEK_CUR) != 0) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100168 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100170 data_offset -= LONG_MAX;
171 }
172#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 if (fseek(stream, data_offset, SEEK_CUR) != 0) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100174 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 }
176 n = fread(p_data, 1, data_length, stream);
177 if (n != data_length) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100178 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100180 status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if (p_data_length != NULL) {
Simon D Hughesbda5a212019-07-10 16:34:21 +0100182 *p_data_length = n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100184
185exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 if (stream != NULL) {
187 fclose(stream);
188 }
189 return status;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100190}
191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192psa_status_t psa_its_set(psa_storage_uid_t uid,
193 uint32_t data_length,
194 const void *p_data,
195 psa_storage_create_flags_t create_flags)
Gilles Peskine6194dc22018-11-16 22:24:15 +0100196{
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 if (uid == 0) {
198 return PSA_ERROR_INVALID_HANDLE;
pespaceke9901002022-02-08 13:52:28 +0100199 }
200
Gilles Peskine6194dc22018-11-16 22:24:15 +0100201 psa_status_t status = PSA_ERROR_STORAGE_FAILURE;
202 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
203 FILE *stream = NULL;
204 psa_its_file_header_t header;
205 size_t n;
206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 memcpy(header.magic, PSA_ITS_MAGIC_STRING, PSA_ITS_MAGIC_LENGTH);
208 MBEDTLS_PUT_UINT32_LE(data_length, header.size, 0);
209 MBEDTLS_PUT_UINT32_LE(create_flags, header.flags, 0);
Gilles Peskine6194dc22018-11-16 22:24:15 +0100210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 psa_its_fill_filename(uid, filename);
212 stream = fopen(PSA_ITS_STORAGE_TEMP, "wb");
Gilles Peskineda0913b2022-06-30 17:03:40 +0200213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if (stream == NULL) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100215 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100217
Gilles Peskineda0913b2022-06-30 17:03:40 +0200218 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 mbedtls_setbuf(stream, NULL);
Gilles Peskineda0913b2022-06-30 17:03:40 +0200220
Gilles Peskine6194dc22018-11-16 22:24:15 +0100221 status = PSA_ERROR_INSUFFICIENT_STORAGE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 n = fwrite(&header, 1, sizeof(header), stream);
223 if (n != sizeof(header)) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100224 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 }
226 if (data_length != 0) {
227 n = fwrite(p_data, 1, data_length, stream);
228 if (n != data_length) {
Andrzej Kurekdc22d8d2019-09-05 09:34:34 -0400229 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 }
Andrzej Kurekdc22d8d2019-09-05 09:34:34 -0400231 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100232 status = PSA_SUCCESS;
233
234exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 if (stream != NULL) {
236 int ret = fclose(stream);
237 if (status == PSA_SUCCESS && ret != 0) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100238 status = PSA_ERROR_INSUFFICIENT_STORAGE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100240 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if (status == PSA_SUCCESS) {
242 if (rename_replace_existing(PSA_ITS_STORAGE_TEMP, filename) != 0) {
Gilles Peskine6194dc22018-11-16 22:24:15 +0100243 status = PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100245 }
Gilles Peskinebab1b522020-08-25 22:49:19 +0200246 /* The temporary file may still exist, but only in failure cases where
247 * we're already reporting an error. So there's nothing we can do on
248 * failure. If the function succeeded, and in some error cases, the
249 * temporary file doesn't exist and so remove() is expected to fail.
250 * Thus we just ignore the return status of remove(). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 (void) remove(PSA_ITS_STORAGE_TEMP);
252 return status;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100253}
254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255psa_status_t psa_its_remove(psa_storage_uid_t uid)
Gilles Peskine6194dc22018-11-16 22:24:15 +0100256{
257 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
258 FILE *stream;
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 psa_its_fill_filename(uid, filename);
260 stream = fopen(filename, "rb");
261 if (stream == NULL) {
262 return PSA_ERROR_DOES_NOT_EXIST;
263 }
264 fclose(stream);
265 if (remove(filename) != 0) {
266 return PSA_ERROR_STORAGE_FAILURE;
267 }
268 return PSA_SUCCESS;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100269}
270
271#endif /* MBEDTLS_PSA_ITS_FILE_C */