1 /*
2 * PSA ITS simulator over stdio files.
3 */
4 /*
5 * Copyright The Mbed TLS Contributors
6 * 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.
19 */
20
21 #if defined(MBEDTLS_CONFIG_FILE)
22 #include MBEDTLS_CONFIG_FILE
23 #else
24 #include "mbedtls/config.h"
25 #endif
26
27 #if defined(MBEDTLS_PSA_ITS_FILE_C)
28
29 #if defined(MBEDTLS_PLATFORM_C)
30 #include "mbedtls/platform.h"
31 #else
32 #define mbedtls_snprintf snprintf
33 #endif
34
35 #if defined(_WIN32)
36 #include <windows.h>
37 #endif
38
39 #include "psa_crypto_its.h"
40
41 #include <limits.h>
42 #include <stdint.h>
43 #include <stdio.h>
44 #include <string.h>
45
46 #if !defined(PSA_ITS_STORAGE_PREFIX)
47 #define PSA_ITS_STORAGE_PREFIX ""
48 #endif
49
50 #define PSA_ITS_STORAGE_FILENAME_PATTERN "%08lx%08lx"
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
59
60 /* The maximum value of psa_storage_info_t.size */
61 #define PSA_ITS_MAX_SIZE 0xffffffff
62
63 #define PSA_ITS_MAGIC_STRING "PSA\0ITS\0"
64 #define PSA_ITS_MAGIC_LENGTH 8
65
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
75
76 typedef struct
77 {
78 uint8_t magic[PSA_ITS_MAGIC_LENGTH];
79 uint8_t size[sizeof( uint32_t )];
80 uint8_t flags[sizeof( psa_storage_create_flags_t )];
81 } psa_its_file_header_t;
82
psa_its_fill_filename(psa_storage_uid_t uid,char * filename)83 static void psa_its_fill_filename( psa_storage_uid_t uid, char *filename )
84 {
85 /* Break up the UID into two 32-bit pieces so as not to rely on
86 * long long support in snprintf. */
87 mbedtls_snprintf( filename, PSA_ITS_STORAGE_FILENAME_LENGTH,
88 "%s" PSA_ITS_STORAGE_FILENAME_PATTERN "%s",
89 PSA_ITS_STORAGE_PREFIX,
90 (unsigned long) ( uid >> 32 ),
91 (unsigned long) ( uid & 0xffffffff ),
92 PSA_ITS_STORAGE_SUFFIX );
93 }
94
psa_its_read_file(psa_storage_uid_t uid,struct psa_storage_info_t * p_info,FILE ** p_stream)95 static psa_status_t psa_its_read_file( psa_storage_uid_t uid,
96 struct psa_storage_info_t *p_info,
97 FILE **p_stream )
98 {
99 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
100 psa_its_file_header_t header;
101 size_t n;
102
103 *p_stream = NULL;
104 psa_its_fill_filename( uid, filename );
105 *p_stream = fopen( filename, "rb" );
106 if( *p_stream == NULL )
107 return( PSA_ERROR_DOES_NOT_EXIST );
108
109 n = fread( &header, 1, sizeof( header ), *p_stream );
110 if( n != sizeof( header ) )
111 return( PSA_ERROR_DATA_CORRUPT );
112 if( memcmp( header.magic, PSA_ITS_MAGIC_STRING,
113 PSA_ITS_MAGIC_LENGTH ) != 0 )
114 return( PSA_ERROR_DATA_CORRUPT );
115
116 p_info->size = ( header.size[0] |
117 header.size[1] << 8 |
118 header.size[2] << 16 |
119 header.size[3] << 24 );
120 p_info->flags = ( header.flags[0] |
121 header.flags[1] << 8 |
122 header.flags[2] << 16 |
123 header.flags[3] << 24 );
124 return( PSA_SUCCESS );
125 }
126
psa_its_get_info(psa_storage_uid_t uid,struct psa_storage_info_t * p_info)127 psa_status_t psa_its_get_info( psa_storage_uid_t uid,
128 struct psa_storage_info_t *p_info )
129 {
130 psa_status_t status;
131 FILE *stream = NULL;
132 status = psa_its_read_file( uid, p_info, &stream );
133 if( stream != NULL )
134 fclose( stream );
135 return( status );
136 }
137
psa_its_get(psa_storage_uid_t uid,uint32_t data_offset,uint32_t data_length,void * p_data,size_t * p_data_length)138 psa_status_t psa_its_get( psa_storage_uid_t uid,
139 uint32_t data_offset,
140 uint32_t data_length,
141 void *p_data,
142 size_t *p_data_length )
143 {
144 psa_status_t status;
145 FILE *stream = NULL;
146 size_t n;
147 struct psa_storage_info_t info;
148
149 status = psa_its_read_file( uid, &info, &stream );
150 if( status != PSA_SUCCESS )
151 goto exit;
152 status = PSA_ERROR_INVALID_ARGUMENT;
153 if( data_offset + data_length < data_offset )
154 goto exit;
155 #if SIZE_MAX < 0xffffffff
156 if( data_offset + data_length > SIZE_MAX )
157 goto exit;
158 #endif
159 if( data_offset + data_length > info.size )
160 goto exit;
161
162 status = PSA_ERROR_STORAGE_FAILURE;
163 #if LONG_MAX < 0xffffffff
164 while( data_offset > LONG_MAX )
165 {
166 if( fseek( stream, LONG_MAX, SEEK_CUR ) != 0 )
167 goto exit;
168 data_offset -= LONG_MAX;
169 }
170 #endif
171 if( fseek( stream, data_offset, SEEK_CUR ) != 0 )
172 goto exit;
173 n = fread( p_data, 1, data_length, stream );
174 if( n != data_length )
175 goto exit;
176 status = PSA_SUCCESS;
177 if( p_data_length != NULL )
178 *p_data_length = n;
179
180 exit:
181 if( stream != NULL )
182 fclose( stream );
183 return( status );
184 }
185
psa_its_set(psa_storage_uid_t uid,uint32_t data_length,const void * p_data,psa_storage_create_flags_t create_flags)186 psa_status_t psa_its_set( psa_storage_uid_t uid,
187 uint32_t data_length,
188 const void *p_data,
189 psa_storage_create_flags_t create_flags )
190 {
191 psa_status_t status = PSA_ERROR_STORAGE_FAILURE;
192 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
193 FILE *stream = NULL;
194 psa_its_file_header_t header;
195 size_t n;
196
197 memcpy( header.magic, PSA_ITS_MAGIC_STRING, PSA_ITS_MAGIC_LENGTH );
198 header.size[0] = data_length & 0xff;
199 header.size[1] = ( data_length >> 8 ) & 0xff;
200 header.size[2] = ( data_length >> 16 ) & 0xff;
201 header.size[3] = ( data_length >> 24 ) & 0xff;
202 header.flags[0] = create_flags & 0xff;
203 header.flags[1] = ( create_flags >> 8 ) & 0xff;
204 header.flags[2] = ( create_flags >> 16 ) & 0xff;
205 header.flags[3] = ( create_flags >> 24 ) & 0xff;
206
207 psa_its_fill_filename( uid, filename );
208 stream = fopen( PSA_ITS_STORAGE_TEMP, "wb" );
209 if( stream == NULL )
210 goto exit;
211
212 status = PSA_ERROR_INSUFFICIENT_STORAGE;
213 n = fwrite( &header, 1, sizeof( header ), stream );
214 if( n != sizeof( header ) )
215 goto exit;
216 if( data_length != 0 )
217 {
218 n = fwrite( p_data, 1, data_length, stream );
219 if( n != data_length )
220 goto exit;
221 }
222 status = PSA_SUCCESS;
223
224 exit:
225 if( stream != NULL )
226 {
227 int ret = fclose( stream );
228 if( status == PSA_SUCCESS && ret != 0 )
229 status = PSA_ERROR_INSUFFICIENT_STORAGE;
230 }
231 if( status == PSA_SUCCESS )
232 {
233 if( rename_replace_existing( PSA_ITS_STORAGE_TEMP, filename ) != 0 )
234 status = PSA_ERROR_STORAGE_FAILURE;
235 }
236 /* The temporary file may still exist, but only in failure cases where
237 * we're already reporting an error. So there's nothing we can do on
238 * failure. If the function succeeded, and in some error cases, the
239 * temporary file doesn't exist and so remove() is expected to fail.
240 * Thus we just ignore the return status of remove(). */
241 (void) remove( PSA_ITS_STORAGE_TEMP );
242 return( status );
243 }
244
psa_its_remove(psa_storage_uid_t uid)245 psa_status_t psa_its_remove( psa_storage_uid_t uid )
246 {
247 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
248 FILE *stream;
249 psa_its_fill_filename( uid, filename );
250 stream = fopen( filename, "rb" );
251 if( stream == NULL )
252 return( PSA_ERROR_DOES_NOT_EXIST );
253 fclose( stream );
254 if( remove( filename ) != 0 )
255 return( PSA_ERROR_STORAGE_FAILURE );
256 return( PSA_SUCCESS );
257 }
258
259 #endif /* MBEDTLS_PSA_ITS_FILE_C */
260