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 #include "common.h"
22
23 #if defined(MBEDTLS_PSA_ITS_FILE_C)
24
25 #if defined(MBEDTLS_PLATFORM_C)
26 #include "mbedtls/platform.h"
27 #else
28 #define mbedtls_snprintf snprintf
29 #endif
30
31 #if defined(_WIN32)
32 #include <windows.h>
33 #endif
34
35 #include "psa_crypto_its.h"
36
37 #include <limits.h>
38 #include <stdint.h>
39 #include <stdio.h>
40 #include <string.h>
41
42 #if !defined(PSA_ITS_STORAGE_PREFIX)
43 #define PSA_ITS_STORAGE_PREFIX ""
44 #endif
45
46 #define PSA_ITS_STORAGE_FILENAME_PATTERN "%08x%08x"
47 #define PSA_ITS_STORAGE_SUFFIX ".psa_its"
48 #define PSA_ITS_STORAGE_FILENAME_LENGTH \
49 ( sizeof( PSA_ITS_STORAGE_PREFIX ) - 1 + /*prefix without terminating 0*/ \
50 16 + /*UID (64-bit number in hex)*/ \
51 sizeof( PSA_ITS_STORAGE_SUFFIX ) - 1 + /*suffix without terminating 0*/ \
52 1 /*terminating null byte*/ )
53 #define PSA_ITS_STORAGE_TEMP \
54 PSA_ITS_STORAGE_PREFIX "tempfile" PSA_ITS_STORAGE_SUFFIX
55
56 /* The maximum value of psa_storage_info_t.size */
57 #define PSA_ITS_MAX_SIZE 0xffffffff
58
59 #define PSA_ITS_MAGIC_STRING "PSA\0ITS\0"
60 #define PSA_ITS_MAGIC_LENGTH 8
61
62 /* As rename fails on Windows if the new filepath already exists,
63 * use MoveFileExA with the MOVEFILE_REPLACE_EXISTING flag instead.
64 * Returns 0 on success, nonzero on failure. */
65 #if defined(_WIN32)
66 #define rename_replace_existing( oldpath, newpath ) \
67 ( ! MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING ) )
68 #else
69 #define rename_replace_existing( oldpath, newpath ) rename( oldpath, newpath )
70 #endif
71
72 typedef struct
73 {
74 uint8_t magic[PSA_ITS_MAGIC_LENGTH];
75 uint8_t size[sizeof( uint32_t )];
76 uint8_t flags[sizeof( psa_storage_create_flags_t )];
77 } psa_its_file_header_t;
78
psa_its_fill_filename(psa_storage_uid_t uid,char * filename)79 static void psa_its_fill_filename( psa_storage_uid_t uid, char *filename )
80 {
81 /* Break up the UID into two 32-bit pieces so as not to rely on
82 * long long support in snprintf. */
83 mbedtls_snprintf( filename, PSA_ITS_STORAGE_FILENAME_LENGTH,
84 "%s" PSA_ITS_STORAGE_FILENAME_PATTERN "%s",
85 PSA_ITS_STORAGE_PREFIX,
86 (unsigned) ( uid >> 32 ),
87 (unsigned) ( uid & 0xffffffff ),
88 PSA_ITS_STORAGE_SUFFIX );
89 }
90
psa_its_read_file(psa_storage_uid_t uid,struct psa_storage_info_t * p_info,FILE ** p_stream)91 static psa_status_t psa_its_read_file( psa_storage_uid_t uid,
92 struct psa_storage_info_t *p_info,
93 FILE **p_stream )
94 {
95 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
96 psa_its_file_header_t header;
97 size_t n;
98
99 *p_stream = NULL;
100 psa_its_fill_filename( uid, filename );
101 *p_stream = fopen( filename, "rb" );
102 if( *p_stream == NULL )
103 return( PSA_ERROR_DOES_NOT_EXIST );
104
105 n = fread( &header, 1, sizeof( header ), *p_stream );
106 if( n != sizeof( header ) )
107 return( PSA_ERROR_DATA_CORRUPT );
108 if( memcmp( header.magic, PSA_ITS_MAGIC_STRING,
109 PSA_ITS_MAGIC_LENGTH ) != 0 )
110 return( PSA_ERROR_DATA_CORRUPT );
111
112 p_info->size = ( header.size[0] |
113 header.size[1] << 8 |
114 header.size[2] << 16 |
115 header.size[3] << 24 );
116 p_info->flags = ( header.flags[0] |
117 header.flags[1] << 8 |
118 header.flags[2] << 16 |
119 header.flags[3] << 24 );
120 return( PSA_SUCCESS );
121 }
122
psa_its_get_info(psa_storage_uid_t uid,struct psa_storage_info_t * p_info)123 psa_status_t psa_its_get_info( psa_storage_uid_t uid,
124 struct psa_storage_info_t *p_info )
125 {
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 );
132 }
133
psa_its_get(psa_storage_uid_t uid,uint32_t data_offset,uint32_t data_length,void * p_data,size_t * p_data_length)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 {
140 psa_status_t status;
141 FILE *stream = NULL;
142 size_t n;
143 struct psa_storage_info_t info;
144
145 status = psa_its_read_file( uid, &info, &stream );
146 if( status != PSA_SUCCESS )
147 goto exit;
148 status = PSA_ERROR_INVALID_ARGUMENT;
149 if( data_offset + data_length < data_offset )
150 goto exit;
151 #if SIZE_MAX < 0xffffffff
152 if( data_offset + data_length > SIZE_MAX )
153 goto exit;
154 #endif
155 if( data_offset + data_length > info.size )
156 goto exit;
157
158 status = PSA_ERROR_STORAGE_FAILURE;
159 #if LONG_MAX < 0xffffffff
160 while( data_offset > LONG_MAX )
161 {
162 if( fseek( stream, LONG_MAX, SEEK_CUR ) != 0 )
163 goto exit;
164 data_offset -= LONG_MAX;
165 }
166 #endif
167 if( fseek( stream, data_offset, SEEK_CUR ) != 0 )
168 goto exit;
169 n = fread( p_data, 1, data_length, stream );
170 if( n != data_length )
171 goto exit;
172 status = PSA_SUCCESS;
173 if( p_data_length != NULL )
174 *p_data_length = n;
175
176 exit:
177 if( stream != NULL )
178 fclose( stream );
179 return( status );
180 }
181
psa_its_set(psa_storage_uid_t uid,uint32_t data_length,const void * p_data,psa_storage_create_flags_t create_flags)182 psa_status_t psa_its_set( psa_storage_uid_t uid,
183 uint32_t data_length,
184 const void *p_data,
185 psa_storage_create_flags_t create_flags )
186 {
187 psa_status_t status = PSA_ERROR_STORAGE_FAILURE;
188 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
189 FILE *stream = NULL;
190 psa_its_file_header_t header;
191 size_t n;
192
193 memcpy( header.magic, PSA_ITS_MAGIC_STRING, PSA_ITS_MAGIC_LENGTH );
194 MBEDTLS_PUT_UINT32_LE( data_length, header.size, 0 );
195 MBEDTLS_PUT_UINT32_LE( create_flags, header.flags, 0 );
196
197 psa_its_fill_filename( uid, filename );
198 stream = fopen( PSA_ITS_STORAGE_TEMP, "wb" );
199 if( stream == NULL )
200 goto exit;
201
202 status = PSA_ERROR_INSUFFICIENT_STORAGE;
203 n = fwrite( &header, 1, sizeof( header ), stream );
204 if( n != sizeof( header ) )
205 goto exit;
206 if( data_length != 0 )
207 {
208 n = fwrite( p_data, 1, data_length, stream );
209 if( n != data_length )
210 goto exit;
211 }
212 status = PSA_SUCCESS;
213
214 exit:
215 if( stream != NULL )
216 {
217 int ret = fclose( stream );
218 if( status == PSA_SUCCESS && ret != 0 )
219 status = PSA_ERROR_INSUFFICIENT_STORAGE;
220 }
221 if( status == PSA_SUCCESS )
222 {
223 if( rename_replace_existing( PSA_ITS_STORAGE_TEMP, filename ) != 0 )
224 status = PSA_ERROR_STORAGE_FAILURE;
225 }
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 );
233 }
234
psa_its_remove(psa_storage_uid_t uid)235 psa_status_t psa_its_remove( psa_storage_uid_t uid )
236 {
237 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
238 FILE *stream;
239 psa_its_fill_filename( uid, filename );
240 stream = fopen( filename, "rb" );
241 if( stream == NULL )
242 return( PSA_ERROR_DOES_NOT_EXIST );
243 fclose( stream );
244 if( remove( filename ) != 0 )
245 return( PSA_ERROR_STORAGE_FAILURE );
246 return( PSA_SUCCESS );
247 }
248
249 #endif /* MBEDTLS_PSA_ITS_FILE_C */
250