1 /*
2  *  PSA ITS simulator over stdio files.
3  */
4 /*
5  *  Copyright The Mbed TLS Contributors
6  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7  */
8 
9 #include "common.h"
10 
11 #if defined(MBEDTLS_PSA_ITS_FILE_C)
12 
13 #include "mbedtls/platform.h"
14 
15 #if defined(_WIN32)
16 #include <windows.h>
17 #endif
18 
19 #include "psa_crypto_its.h"
20 
21 #include <limits.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <string.h>
25 
26 #if !defined(PSA_ITS_STORAGE_PREFIX)
27 #define PSA_ITS_STORAGE_PREFIX ""
28 #endif
29 
30 #define PSA_ITS_STORAGE_FILENAME_PATTERN "%08x%08x"
31 #define PSA_ITS_STORAGE_SUFFIX ".psa_its"
32 #define PSA_ITS_STORAGE_FILENAME_LENGTH         \
33     (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*/)
37 #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 
46 /* 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. */
49 #if defined(_WIN32)
50 #define rename_replace_existing(oldpath, newpath) \
51     (!MoveFileExA(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
52 #else
53 #define rename_replace_existing(oldpath, newpath) rename(oldpath, newpath)
54 #endif
55 
56 typedef struct {
57     uint8_t magic[PSA_ITS_MAGIC_LENGTH];
58     uint8_t size[sizeof(uint32_t)];
59     uint8_t flags[sizeof(psa_storage_create_flags_t)];
60 } psa_its_file_header_t;
61 
psa_its_fill_filename(psa_storage_uid_t uid,char * filename)62 static void psa_its_fill_filename(psa_storage_uid_t uid, char *filename)
63 {
64     /* Break up the UID into two 32-bit pieces so as not to rely on
65      * long long support in snprintf. */
66     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);
72 }
73 
psa_its_read_file(psa_storage_uid_t uid,struct psa_storage_info_t * p_info,FILE ** p_stream)74 static psa_status_t psa_its_read_file(psa_storage_uid_t uid,
75                                       struct psa_storage_info_t *p_info,
76                                       FILE **p_stream)
77 {
78     char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
79     psa_its_file_header_t header;
80     size_t n;
81 
82     *p_stream = NULL;
83     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     }
88 
89     /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
90     mbedtls_setbuf(*p_stream, NULL);
91 
92     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     }
100 
101     p_info->size  = MBEDTLS_GET_UINT32_LE(header.size, 0);
102     p_info->flags = MBEDTLS_GET_UINT32_LE(header.flags, 0);
103 
104     return PSA_SUCCESS;
105 }
106 
psa_its_get_info(psa_storage_uid_t uid,struct psa_storage_info_t * p_info)107 psa_status_t psa_its_get_info(psa_storage_uid_t uid,
108                               struct psa_storage_info_t *p_info)
109 {
110     psa_status_t status;
111     FILE *stream = NULL;
112     status = psa_its_read_file(uid, p_info, &stream);
113     if (stream != NULL) {
114         fclose(stream);
115     }
116     return status;
117 }
118 
psa_its_get(psa_storage_uid_t uid,uint32_t data_offset,uint32_t data_length,void * p_data,size_t * p_data_length)119 psa_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)
124 {
125     psa_status_t status;
126     FILE *stream = NULL;
127     size_t n;
128     struct psa_storage_info_t info;
129 
130     status = psa_its_read_file(uid, &info, &stream);
131     if (status != PSA_SUCCESS) {
132         goto exit;
133     }
134     status = PSA_ERROR_INVALID_ARGUMENT;
135     if (data_offset + data_length < data_offset) {
136         goto exit;
137     }
138 #if SIZE_MAX < 0xffffffff
139     if (data_offset + data_length > SIZE_MAX) {
140         goto exit;
141     }
142 #endif
143     if (data_offset + data_length > info.size) {
144         goto exit;
145     }
146 
147     status = PSA_ERROR_STORAGE_FAILURE;
148 #if LONG_MAX < 0xffffffff
149     while (data_offset > LONG_MAX) {
150         if (fseek(stream, LONG_MAX, SEEK_CUR) != 0) {
151             goto exit;
152         }
153         data_offset -= LONG_MAX;
154     }
155 #endif
156     if (fseek(stream, data_offset, SEEK_CUR) != 0) {
157         goto exit;
158     }
159     n = fread(p_data, 1, data_length, stream);
160     if (n != data_length) {
161         goto exit;
162     }
163     status = PSA_SUCCESS;
164     if (p_data_length != NULL) {
165         *p_data_length = n;
166     }
167 
168 exit:
169     if (stream != NULL) {
170         fclose(stream);
171     }
172     return status;
173 }
174 
psa_its_set(psa_storage_uid_t uid,uint32_t data_length,const void * p_data,psa_storage_create_flags_t create_flags)175 psa_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)
179 {
180     if (uid == 0) {
181         return PSA_ERROR_INVALID_HANDLE;
182     }
183 
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;
189 
190     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);
193 
194     psa_its_fill_filename(uid, filename);
195     stream = fopen(PSA_ITS_STORAGE_TEMP, "wb");
196 
197     if (stream == NULL) {
198         goto exit;
199     }
200 
201     /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
202     mbedtls_setbuf(stream, NULL);
203 
204     status = PSA_ERROR_INSUFFICIENT_STORAGE;
205     n = fwrite(&header, 1, sizeof(header), stream);
206     if (n != sizeof(header)) {
207         goto exit;
208     }
209     if (data_length != 0) {
210         n = fwrite(p_data, 1, data_length, stream);
211         if (n != data_length) {
212             goto exit;
213         }
214     }
215     status = PSA_SUCCESS;
216 
217 exit:
218     if (stream != NULL) {
219         int ret = fclose(stream);
220         if (status == PSA_SUCCESS && ret != 0) {
221             status = PSA_ERROR_INSUFFICIENT_STORAGE;
222         }
223     }
224     if (status == PSA_SUCCESS) {
225         if (rename_replace_existing(PSA_ITS_STORAGE_TEMP, filename) != 0) {
226             status = PSA_ERROR_STORAGE_FAILURE;
227         }
228     }
229     /* 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(). */
234     (void) remove(PSA_ITS_STORAGE_TEMP);
235     return status;
236 }
237 
psa_its_remove(psa_storage_uid_t uid)238 psa_status_t psa_its_remove(psa_storage_uid_t uid)
239 {
240     char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
241     FILE *stream;
242     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;
252 }
253 
254 #endif /* MBEDTLS_PSA_ITS_FILE_C */
255