blob: 8dff7834ded9b34616051053a52285ff020f1dca [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
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
Darryl Greenb4679342019-04-10 15:37:06 +010035#if defined(_WIN32)
36#include <windows.h>
37#endif
38
Gilles Peskine6194dc22018-11-16 22:24:15 +010039#include "psa_crypto_its.h"
40
41#include <limits.h>
42#include <stdint.h>
43#include <stdio.h>
44#include <string.h>
45
Simon D Hughesbda5a212019-07-10 16:34:21 +010046#if !defined(PSA_ITS_STORAGE_PREFIX)
Gilles Peskine6194dc22018-11-16 22:24:15 +010047#define PSA_ITS_STORAGE_PREFIX ""
Simon D Hughesbda5a212019-07-10 16:34:21 +010048#endif
Gilles Peskine6194dc22018-11-16 22:24:15 +010049
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)*/ \
Paul Elliottdd9e8f62020-12-09 14:53:24 +000055 16 + /*UID (64-bit number in hex)*/ \
Gilles Peskine6194dc22018-11-16 22:24:15 +010056 sizeof( PSA_ITS_STORAGE_SUFFIX ) - 1 + /*suffix without terminating 0*/ \
57 1 /*terminating null byte*/ )
58#define PSA_ITS_STORAGE_TEMP \
59 PSA_ITS_STORAGE_PREFIX "tempfile" PSA_ITS_STORAGE_SUFFIX
60
61/* The maximum value of psa_storage_info_t.size */
62#define PSA_ITS_MAX_SIZE 0xffffffff
63
64#define PSA_ITS_MAGIC_STRING "PSA\0ITS\0"
65#define PSA_ITS_MAGIC_LENGTH 8
66
Darryl Green86095bc2019-04-11 14:21:14 +010067/* As rename fails on Windows if the new filepath already exists,
68 * use MoveFileExA with the MOVEFILE_REPLACE_EXISTING flag instead.
69 * Returns 0 on success, nonzero on failure. */
Darryl Greenfdda7de2019-04-11 12:54:02 +010070#if defined(_WIN32)
71#define rename_replace_existing( oldpath, newpath ) \
Darryl Green86095bc2019-04-11 14:21:14 +010072 ( ! MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING ) )
Darryl Greenfdda7de2019-04-11 12:54:02 +010073#else
74#define rename_replace_existing( oldpath, newpath ) rename( oldpath, newpath )
75#endif
76
Gilles Peskine6194dc22018-11-16 22:24:15 +010077typedef struct
78{
79 uint8_t magic[PSA_ITS_MAGIC_LENGTH];
80 uint8_t size[sizeof( uint32_t )];
81 uint8_t flags[sizeof( psa_storage_create_flags_t )];
82} psa_its_file_header_t;
83
84static void psa_its_fill_filename( psa_storage_uid_t uid, char *filename )
85{
86 /* Break up the UID into two 32-bit pieces so as not to rely on
87 * long long support in snprintf. */
88 mbedtls_snprintf( filename, PSA_ITS_STORAGE_FILENAME_LENGTH,
89 "%s" PSA_ITS_STORAGE_FILENAME_PATTERN "%s",
90 PSA_ITS_STORAGE_PREFIX,
91 (unsigned long) ( uid >> 32 ),
92 (unsigned long) ( uid & 0xffffffff ),
93 PSA_ITS_STORAGE_SUFFIX );
94}
95
96static psa_status_t psa_its_read_file( psa_storage_uid_t uid,
97 struct psa_storage_info_t *p_info,
98 FILE **p_stream )
99{
100 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
101 psa_its_file_header_t header;
102 size_t n;
103
104 *p_stream = NULL;
105 psa_its_fill_filename( uid, filename );
106 *p_stream = fopen( filename, "rb" );
107 if( *p_stream == NULL )
108 return( PSA_ERROR_DOES_NOT_EXIST );
109
110 n = fread( &header, 1, sizeof( header ), *p_stream );
111 if( n != sizeof( header ) )
112 return( PSA_ERROR_DATA_CORRUPT );
113 if( memcmp( header.magic, PSA_ITS_MAGIC_STRING,
114 PSA_ITS_MAGIC_LENGTH ) != 0 )
115 return( PSA_ERROR_DATA_CORRUPT );
116
117 p_info->size = ( header.size[0] |
118 header.size[1] << 8 |
119 header.size[2] << 16 |
120 header.size[3] << 24 );
121 p_info->flags = ( header.flags[0] |
122 header.flags[1] << 8 |
123 header.flags[2] << 16 |
124 header.flags[3] << 24 );
125 return( PSA_SUCCESS );
126}
127
128psa_status_t psa_its_get_info( psa_storage_uid_t uid,
129 struct psa_storage_info_t *p_info )
130{
131 psa_status_t status;
132 FILE *stream = NULL;
133 status = psa_its_read_file( uid, p_info, &stream );
134 if( stream != NULL )
135 fclose( stream );
136 return( status );
137}
138
139psa_status_t psa_its_get( psa_storage_uid_t uid,
140 uint32_t data_offset,
141 uint32_t data_length,
Simon D Hughesbda5a212019-07-10 16:34:21 +0100142 void *p_data,
143 size_t *p_data_length )
Gilles Peskine6194dc22018-11-16 22:24:15 +0100144{
145 psa_status_t status;
146 FILE *stream = NULL;
147 size_t n;
148 struct psa_storage_info_t info;
149
150 status = psa_its_read_file( uid, &info, &stream );
151 if( status != PSA_SUCCESS )
152 goto exit;
Gilles Peskinebc1f2722018-11-16 22:24:38 +0100153 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100154 if( data_offset + data_length < data_offset )
155 goto exit;
156#if SIZE_MAX < 0xffffffff
157 if( data_offset + data_length > SIZE_MAX )
158 goto exit;
159#endif
160 if( data_offset + data_length > info.size )
161 goto exit;
162
Gilles Peskinebc1f2722018-11-16 22:24:38 +0100163 status = PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100164#if LONG_MAX < 0xffffffff
165 while( data_offset > LONG_MAX )
166 {
167 if( fseek( stream, LONG_MAX, SEEK_CUR ) != 0 )
168 goto exit;
169 data_offset -= LONG_MAX;
170 }
171#endif
172 if( fseek( stream, data_offset, SEEK_CUR ) != 0 )
173 goto exit;
174 n = fread( p_data, 1, data_length, stream );
175 if( n != data_length )
176 goto exit;
177 status = PSA_SUCCESS;
Simon D Hughesbda5a212019-07-10 16:34:21 +0100178 if( p_data_length != NULL )
179 *p_data_length = n;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100180
181exit:
182 if( stream != NULL )
183 fclose( stream );
184 return( status );
185}
186
187psa_status_t psa_its_set( psa_storage_uid_t uid,
188 uint32_t data_length,
189 const void *p_data,
190 psa_storage_create_flags_t create_flags )
191{
192 psa_status_t status = PSA_ERROR_STORAGE_FAILURE;
193 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
194 FILE *stream = NULL;
195 psa_its_file_header_t header;
196 size_t n;
197
198 memcpy( header.magic, PSA_ITS_MAGIC_STRING, PSA_ITS_MAGIC_LENGTH );
199 header.size[0] = data_length & 0xff;
200 header.size[1] = ( data_length >> 8 ) & 0xff;
201 header.size[2] = ( data_length >> 16 ) & 0xff;
202 header.size[3] = ( data_length >> 24 ) & 0xff;
203 header.flags[0] = create_flags & 0xff;
204 header.flags[1] = ( create_flags >> 8 ) & 0xff;
205 header.flags[2] = ( create_flags >> 16 ) & 0xff;
206 header.flags[3] = ( create_flags >> 24 ) & 0xff;
207
208 psa_its_fill_filename( uid, filename );
209 stream = fopen( PSA_ITS_STORAGE_TEMP, "wb" );
210 if( stream == NULL )
211 goto exit;
212
213 status = PSA_ERROR_INSUFFICIENT_STORAGE;
214 n = fwrite( &header, 1, sizeof( header ), stream );
215 if( n != sizeof( header ) )
216 goto exit;
Andrzej Kurekdc22d8d2019-09-05 09:34:34 -0400217 if( data_length != 0 )
218 {
219 n = fwrite( p_data, 1, data_length, stream );
220 if( n != data_length )
221 goto exit;
222 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100223 status = PSA_SUCCESS;
224
225exit:
226 if( stream != NULL )
227 {
228 int ret = fclose( stream );
229 if( status == PSA_SUCCESS && ret != 0 )
230 status = PSA_ERROR_INSUFFICIENT_STORAGE;
231 }
232 if( status == PSA_SUCCESS )
233 {
Darryl Greenfdda7de2019-04-11 12:54:02 +0100234 if( rename_replace_existing( PSA_ITS_STORAGE_TEMP, filename ) != 0 )
Gilles Peskine6194dc22018-11-16 22:24:15 +0100235 status = PSA_ERROR_STORAGE_FAILURE;
236 }
Gilles Peskinebab1b522020-08-25 22:49:19 +0200237 /* The temporary file may still exist, but only in failure cases where
238 * we're already reporting an error. So there's nothing we can do on
239 * failure. If the function succeeded, and in some error cases, the
240 * temporary file doesn't exist and so remove() is expected to fail.
241 * Thus we just ignore the return status of remove(). */
242 (void) remove( PSA_ITS_STORAGE_TEMP );
Gilles Peskine6194dc22018-11-16 22:24:15 +0100243 return( status );
244}
245
246psa_status_t psa_its_remove( psa_storage_uid_t uid )
247{
248 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
249 FILE *stream;
250 psa_its_fill_filename( uid, filename );
251 stream = fopen( filename, "rb" );
252 if( stream == NULL )
253 return( PSA_ERROR_DOES_NOT_EXIST );
254 fclose( stream );
255 if( remove( filename ) != 0 )
256 return( PSA_ERROR_STORAGE_FAILURE );
257 return( PSA_SUCCESS );
258}
259
260#endif /* MBEDTLS_PSA_ITS_FILE_C */