blob: b7c2e6b04063722226d3e5a7af51fd5c5855f818 [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
Gilles Peskineda0913b2022-06-30 17:03:40 +0200105 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
106 mbedtls_setbuf( *p_stream, NULL );
107
Gilles Peskine6194dc22018-11-16 22:24:15 +0100108 n = fread( &header, 1, sizeof( header ), *p_stream );
109 if( n != sizeof( header ) )
110 return( PSA_ERROR_DATA_CORRUPT );
111 if( memcmp( header.magic, PSA_ITS_MAGIC_STRING,
112 PSA_ITS_MAGIC_LENGTH ) != 0 )
113 return( PSA_ERROR_DATA_CORRUPT );
114
115 p_info->size = ( header.size[0] |
116 header.size[1] << 8 |
117 header.size[2] << 16 |
118 header.size[3] << 24 );
119 p_info->flags = ( header.flags[0] |
120 header.flags[1] << 8 |
121 header.flags[2] << 16 |
122 header.flags[3] << 24 );
123 return( PSA_SUCCESS );
124}
125
126psa_status_t psa_its_get_info( psa_storage_uid_t uid,
127 struct psa_storage_info_t *p_info )
128{
129 psa_status_t status;
130 FILE *stream = NULL;
131 status = psa_its_read_file( uid, p_info, &stream );
132 if( stream != NULL )
133 fclose( stream );
134 return( status );
135}
136
137psa_status_t psa_its_get( psa_storage_uid_t uid,
138 uint32_t data_offset,
139 uint32_t data_length,
Simon D Hughesbda5a212019-07-10 16:34:21 +0100140 void *p_data,
141 size_t *p_data_length )
Gilles Peskine6194dc22018-11-16 22:24:15 +0100142{
143 psa_status_t status;
144 FILE *stream = NULL;
145 size_t n;
146 struct psa_storage_info_t info;
147
148 status = psa_its_read_file( uid, &info, &stream );
149 if( status != PSA_SUCCESS )
150 goto exit;
Gilles Peskinebc1f2722018-11-16 22:24:38 +0100151 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100152 if( data_offset + data_length < data_offset )
153 goto exit;
154#if SIZE_MAX < 0xffffffff
155 if( data_offset + data_length > SIZE_MAX )
156 goto exit;
157#endif
158 if( data_offset + data_length > info.size )
159 goto exit;
160
Gilles Peskinebc1f2722018-11-16 22:24:38 +0100161 status = PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100162#if LONG_MAX < 0xffffffff
163 while( data_offset > LONG_MAX )
164 {
165 if( fseek( stream, LONG_MAX, SEEK_CUR ) != 0 )
166 goto exit;
167 data_offset -= LONG_MAX;
168 }
169#endif
170 if( fseek( stream, data_offset, SEEK_CUR ) != 0 )
171 goto exit;
172 n = fread( p_data, 1, data_length, stream );
173 if( n != data_length )
174 goto exit;
175 status = PSA_SUCCESS;
Simon D Hughesbda5a212019-07-10 16:34:21 +0100176 if( p_data_length != NULL )
177 *p_data_length = n;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100178
179exit:
180 if( stream != NULL )
181 fclose( stream );
182 return( status );
183}
184
185psa_status_t psa_its_set( psa_storage_uid_t uid,
186 uint32_t data_length,
187 const void *p_data,
188 psa_storage_create_flags_t create_flags )
189{
pespaceke9901002022-02-08 13:52:28 +0100190 if( uid == 0 )
191 {
PeterSpacec2774a32022-02-11 10:21:16 +0100192 return( PSA_ERROR_INVALID_HANDLE );
pespaceke9901002022-02-08 13:52:28 +0100193 }
194
Gilles Peskine6194dc22018-11-16 22:24:15 +0100195 psa_status_t status = PSA_ERROR_STORAGE_FAILURE;
196 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
197 FILE *stream = NULL;
198 psa_its_file_header_t header;
199 size_t n;
200
201 memcpy( header.magic, PSA_ITS_MAGIC_STRING, PSA_ITS_MAGIC_LENGTH );
Joe Subbiani6dd73642021-07-19 11:56:54 +0100202 MBEDTLS_PUT_UINT32_LE( data_length, header.size, 0 );
203 MBEDTLS_PUT_UINT32_LE( create_flags, header.flags, 0 );
Gilles Peskine6194dc22018-11-16 22:24:15 +0100204
205 psa_its_fill_filename( uid, filename );
206 stream = fopen( PSA_ITS_STORAGE_TEMP, "wb" );
Gilles Peskineda0913b2022-06-30 17:03:40 +0200207
Gilles Peskine6194dc22018-11-16 22:24:15 +0100208 if( stream == NULL )
209 goto exit;
210
Gilles Peskineda0913b2022-06-30 17:03:40 +0200211 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
212 mbedtls_setbuf( stream, NULL );
213
Gilles Peskine6194dc22018-11-16 22:24:15 +0100214 status = PSA_ERROR_INSUFFICIENT_STORAGE;
215 n = fwrite( &header, 1, sizeof( header ), stream );
216 if( n != sizeof( header ) )
217 goto exit;
Andrzej Kurekdc22d8d2019-09-05 09:34:34 -0400218 if( data_length != 0 )
219 {
220 n = fwrite( p_data, 1, data_length, stream );
221 if( n != data_length )
222 goto exit;
223 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100224 status = PSA_SUCCESS;
225
226exit:
227 if( stream != NULL )
228 {
229 int ret = fclose( stream );
230 if( status == PSA_SUCCESS && ret != 0 )
231 status = PSA_ERROR_INSUFFICIENT_STORAGE;
232 }
233 if( status == PSA_SUCCESS )
234 {
Darryl Greenfdda7de2019-04-11 12:54:02 +0100235 if( rename_replace_existing( PSA_ITS_STORAGE_TEMP, filename ) != 0 )
Gilles Peskine6194dc22018-11-16 22:24:15 +0100236 status = PSA_ERROR_STORAGE_FAILURE;
237 }
Gilles Peskinebab1b522020-08-25 22:49:19 +0200238 /* The temporary file may still exist, but only in failure cases where
239 * we're already reporting an error. So there's nothing we can do on
240 * failure. If the function succeeded, and in some error cases, the
241 * temporary file doesn't exist and so remove() is expected to fail.
242 * Thus we just ignore the return status of remove(). */
243 (void) remove( PSA_ITS_STORAGE_TEMP );
Gilles Peskine6194dc22018-11-16 22:24:15 +0100244 return( status );
245}
246
247psa_status_t psa_its_remove( psa_storage_uid_t uid )
248{
249 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
250 FILE *stream;
251 psa_its_fill_filename( uid, filename );
252 stream = fopen( filename, "rb" );
253 if( stream == NULL )
254 return( PSA_ERROR_DOES_NOT_EXIST );
255 fclose( stream );
256 if( remove( filename ) != 0 )
257 return( PSA_ERROR_STORAGE_FAILURE );
258 return( PSA_SUCCESS );
259}
260
261#endif /* MBEDTLS_PSA_ITS_FILE_C */