blob: c4782cdba3fc390b754baba7e5239b82e27f24f3 [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
Mateusz Starzyk03f00302021-05-27 14:40:40 +020021#include "common.h"
Gilles Peskine6194dc22018-11-16 22:24:15 +010022
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
Darryl Greenb4679342019-04-10 15:37:06 +010031#if defined(_WIN32)
32#include <windows.h>
33#endif
34
Gilles Peskine6194dc22018-11-16 22:24:15 +010035#include "psa_crypto_its.h"
36
37#include <limits.h>
38#include <stdint.h>
39#include <stdio.h>
40#include <string.h>
41
Simon D Hughesbda5a212019-07-10 16:34:21 +010042#if !defined(PSA_ITS_STORAGE_PREFIX)
Gilles Peskine6194dc22018-11-16 22:24:15 +010043#define PSA_ITS_STORAGE_PREFIX ""
Simon D Hughesbda5a212019-07-10 16:34:21 +010044#endif
Gilles Peskine6194dc22018-11-16 22:24:15 +010045
Paul Elliott3a5a1072020-12-17 18:33:40 +000046#define PSA_ITS_STORAGE_FILENAME_PATTERN "%08x%08x"
Gilles Peskine6194dc22018-11-16 22:24:15 +010047#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
Darryl Green86095bc2019-04-11 14:21:14 +010062/* 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. */
Darryl Greenfdda7de2019-04-11 12:54:02 +010065#if defined(_WIN32)
66#define rename_replace_existing( oldpath, newpath ) \
Darryl Green86095bc2019-04-11 14:21:14 +010067 ( ! MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING ) )
Darryl Greenfdda7de2019-04-11 12:54:02 +010068#else
69#define rename_replace_existing( oldpath, newpath ) rename( oldpath, newpath )
70#endif
71
Gilles Peskine6194dc22018-11-16 22:24:15 +010072typedef 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
79static 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,
Paul Elliott3a5a1072020-12-17 18:33:40 +000086 (unsigned) ( uid >> 32 ),
87 (unsigned) ( uid & 0xffffffff ),
Gilles Peskine6194dc22018-11-16 22:24:15 +010088 PSA_ITS_STORAGE_SUFFIX );
89}
90
91static 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
123psa_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
134psa_status_t psa_its_get( psa_storage_uid_t uid,
135 uint32_t data_offset,
136 uint32_t data_length,
Simon D Hughesbda5a212019-07-10 16:34:21 +0100137 void *p_data,
138 size_t *p_data_length )
Gilles Peskine6194dc22018-11-16 22:24:15 +0100139{
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;
Gilles Peskinebc1f2722018-11-16 22:24:38 +0100148 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100149 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
Gilles Peskinebc1f2722018-11-16 22:24:38 +0100158 status = PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100159#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;
Simon D Hughesbda5a212019-07-10 16:34:21 +0100173 if( p_data_length != NULL )
174 *p_data_length = n;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100175
176exit:
177 if( stream != NULL )
178 fclose( stream );
179 return( status );
180}
181
182psa_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 );
Joe Subbiani6dd73642021-07-19 11:56:54 +0100194 MBEDTLS_PUT_UINT32_LE( data_length, header.size, 0 );
195 MBEDTLS_PUT_UINT32_LE( create_flags, header.flags, 0 );
Gilles Peskine6194dc22018-11-16 22:24:15 +0100196
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;
Andrzej Kurekdc22d8d2019-09-05 09:34:34 -0400206 if( data_length != 0 )
207 {
208 n = fwrite( p_data, 1, data_length, stream );
209 if( n != data_length )
210 goto exit;
211 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100212 status = PSA_SUCCESS;
213
214exit:
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 {
Darryl Greenfdda7de2019-04-11 12:54:02 +0100223 if( rename_replace_existing( PSA_ITS_STORAGE_TEMP, filename ) != 0 )
Gilles Peskine6194dc22018-11-16 22:24:15 +0100224 status = PSA_ERROR_STORAGE_FAILURE;
225 }
Gilles Peskinebab1b522020-08-25 22:49:19 +0200226 /* 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 );
Gilles Peskine6194dc22018-11-16 22:24:15 +0100232 return( status );
233}
234
235psa_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 */