text
[RRG-proxmark3.git] / common / mbedtls / psa_its_file.c
blob9065ea43152be80f28297ac8a2b848046b6a6a8f
1 /*
2 * PSA ITS simulator over stdio files.
3 */
4 /*
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #if defined(MBEDTLS_CONFIG_FILE)
22 #include MBEDTLS_CONFIG_FILE
23 #else
24 #include "mbedtls/config.h"
25 #endif
27 #if defined(MBEDTLS_PSA_ITS_FILE_C)
29 #if defined(MBEDTLS_PLATFORM_C)
30 #include "mbedtls/platform.h"
31 #else
32 #define mbedtls_snprintf snprintf
33 #endif
35 #if defined(_WIN32)
36 #include <windows.h>
37 #endif
39 #include "psa_crypto_its.h"
41 #include <limits.h>
42 #include <stdint.h>
43 #include <stdio.h>
44 #include <string.h>
46 #if !defined(PSA_ITS_STORAGE_PREFIX)
47 #define PSA_ITS_STORAGE_PREFIX ""
48 #endif
50 #define PSA_ITS_STORAGE_FILENAME_PATTERN "%08x%08x"
51 #define PSA_ITS_STORAGE_SUFFIX ".psa_its"
52 #define PSA_ITS_STORAGE_FILENAME_LENGTH \
53 ( sizeof( PSA_ITS_STORAGE_PREFIX ) - 1 + /*prefix without terminating 0*/ \
54 16 + /*UID (64-bit number in hex)*/ \
55 sizeof( PSA_ITS_STORAGE_SUFFIX ) - 1 + /*suffix without terminating 0*/ \
56 1 /*terminating null byte*/ )
57 #define PSA_ITS_STORAGE_TEMP \
58 PSA_ITS_STORAGE_PREFIX "tempfile" PSA_ITS_STORAGE_SUFFIX
60 /* The maximum value of psa_storage_info_t.size */
61 #define PSA_ITS_MAX_SIZE 0xffffffff
63 #define PSA_ITS_MAGIC_STRING "PSA\0ITS\0"
64 #define PSA_ITS_MAGIC_LENGTH 8
66 /* As rename fails on Windows if the new filepath already exists,
67 * use MoveFileExA with the MOVEFILE_REPLACE_EXISTING flag instead.
68 * Returns 0 on success, nonzero on failure. */
69 #if defined(_WIN32)
70 #define rename_replace_existing( oldpath, newpath ) \
71 ( ! MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING ) )
72 #else
73 #define rename_replace_existing( oldpath, newpath ) rename( oldpath, newpath )
74 #endif
76 typedef struct {
77 uint8_t magic[PSA_ITS_MAGIC_LENGTH];
78 uint8_t size[sizeof(uint32_t)];
79 uint8_t flags[sizeof(psa_storage_create_flags_t)];
80 } psa_its_file_header_t;
82 static void psa_its_fill_filename(psa_storage_uid_t uid, char *filename) {
83 /* Break up the UID into two 32-bit pieces so as not to rely on
84 * long long support in snprintf. */
85 mbedtls_snprintf(filename, PSA_ITS_STORAGE_FILENAME_LENGTH,
86 "%s" PSA_ITS_STORAGE_FILENAME_PATTERN "%s",
87 PSA_ITS_STORAGE_PREFIX,
88 (unsigned)(uid >> 32),
89 (unsigned)(uid & 0xffffffff),
90 PSA_ITS_STORAGE_SUFFIX);
93 static psa_status_t psa_its_read_file(psa_storage_uid_t uid,
94 struct psa_storage_info_t *p_info,
95 FILE **p_stream) {
96 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
97 psa_its_file_header_t header;
98 size_t n;
100 *p_stream = NULL;
101 psa_its_fill_filename(uid, filename);
102 *p_stream = fopen(filename, "rb");
103 if (*p_stream == NULL)
104 return (PSA_ERROR_DOES_NOT_EXIST);
106 n = fread(&header, 1, sizeof(header), *p_stream);
107 if (n != sizeof(header))
108 return (PSA_ERROR_DATA_CORRUPT);
109 if (memcmp(header.magic, PSA_ITS_MAGIC_STRING,
110 PSA_ITS_MAGIC_LENGTH) != 0)
111 return (PSA_ERROR_DATA_CORRUPT);
113 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);
124 psa_status_t psa_its_get_info(psa_storage_uid_t uid,
125 struct psa_storage_info_t *p_info) {
126 psa_status_t status;
127 FILE *stream = NULL;
128 status = psa_its_read_file(uid, p_info, &stream);
129 if (stream != NULL)
130 fclose(stream);
131 return (status);
134 psa_status_t psa_its_get(psa_storage_uid_t uid,
135 uint32_t data_offset,
136 uint32_t data_length,
137 void *p_data,
138 size_t *p_data_length) {
139 psa_status_t status;
140 FILE *stream = NULL;
141 size_t n;
142 struct psa_storage_info_t info;
144 status = psa_its_read_file(uid, &info, &stream);
145 if (status != PSA_SUCCESS)
146 goto exit;
147 status = PSA_ERROR_INVALID_ARGUMENT;
148 if (data_offset + data_length < data_offset)
149 goto exit;
150 #if SIZE_MAX < 0xffffffff
151 if (data_offset + data_length > SIZE_MAX)
152 goto exit;
153 #endif
154 if (data_offset + data_length > info.size)
155 goto exit;
157 status = PSA_ERROR_STORAGE_FAILURE;
158 #if LONG_MAX < 0xffffffff
159 while (data_offset > LONG_MAX) {
160 if (fseek(stream, LONG_MAX, SEEK_CUR) != 0)
161 goto exit;
162 data_offset -= LONG_MAX;
164 #endif
165 if (fseek(stream, data_offset, SEEK_CUR) != 0)
166 goto exit;
167 n = fread(p_data, 1, data_length, stream);
168 if (n != data_length)
169 goto exit;
170 status = PSA_SUCCESS;
171 if (p_data_length != NULL)
172 *p_data_length = n;
174 exit:
175 if (stream != NULL)
176 fclose(stream);
177 return (status);
180 psa_status_t psa_its_set(psa_storage_uid_t uid,
181 uint32_t data_length,
182 const void *p_data,
183 psa_storage_create_flags_t create_flags) {
184 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;
190 memcpy(header.magic, PSA_ITS_MAGIC_STRING, PSA_ITS_MAGIC_LENGTH);
191 header.size[0] = data_length & 0xff;
192 header.size[1] = (data_length >> 8) & 0xff;
193 header.size[2] = (data_length >> 16) & 0xff;
194 header.size[3] = (data_length >> 24) & 0xff;
195 header.flags[0] = create_flags & 0xff;
196 header.flags[1] = (create_flags >> 8) & 0xff;
197 header.flags[2] = (create_flags >> 16) & 0xff;
198 header.flags[3] = (create_flags >> 24) & 0xff;
200 psa_its_fill_filename(uid, filename);
201 stream = fopen(PSA_ITS_STORAGE_TEMP, "wb");
202 if (stream == NULL)
203 goto exit;
205 status = PSA_ERROR_INSUFFICIENT_STORAGE;
206 n = fwrite(&header, 1, sizeof(header), stream);
207 if (n != sizeof(header))
208 goto exit;
209 if (data_length != 0) {
210 n = fwrite(p_data, 1, data_length, stream);
211 if (n != data_length)
212 goto exit;
214 status = PSA_SUCCESS;
216 exit:
217 if (stream != NULL) {
218 int ret = fclose(stream);
219 if (status == PSA_SUCCESS && ret != 0)
220 status = PSA_ERROR_INSUFFICIENT_STORAGE;
222 if (status == PSA_SUCCESS) {
223 if (rename_replace_existing(PSA_ITS_STORAGE_TEMP, filename) != 0)
224 status = PSA_ERROR_STORAGE_FAILURE;
226 /* The temporary file may still exist, but only in failure cases where
227 * we're already reporting an error. So there's nothing we can do on
228 * failure. If the function succeeded, and in some error cases, the
229 * temporary file doesn't exist and so remove() is expected to fail.
230 * Thus we just ignore the return status of remove(). */
231 (void) remove(PSA_ITS_STORAGE_TEMP);
232 return (status);
235 psa_status_t psa_its_remove(psa_storage_uid_t uid) {
236 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
237 FILE *stream;
238 psa_its_fill_filename(uid, filename);
239 stream = fopen(filename, "rb");
240 if (stream == NULL)
241 return (PSA_ERROR_DOES_NOT_EXIST);
242 fclose(stream);
243 if (remove(filename) != 0)
244 return (PSA_ERROR_STORAGE_FAILURE);
245 return (PSA_SUCCESS);
248 #endif /* MBEDTLS_PSA_ITS_FILE_C */