blob: ee11cb323f22b45a9cf88aeb1590c179bbec8fa1 [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
Joe Subbiani2bbafda2021-06-24 13:00:03 +010041#include "common.h"
42
Gilles Peskine6194dc22018-11-16 22:24:15 +010043#include <limits.h>
44#include <stdint.h>
45#include <stdio.h>
46#include <string.h>
47
Simon D Hughesbda5a212019-07-10 16:34:21 +010048#if !defined(PSA_ITS_STORAGE_PREFIX)
Gilles Peskine6194dc22018-11-16 22:24:15 +010049#define PSA_ITS_STORAGE_PREFIX ""
Simon D Hughesbda5a212019-07-10 16:34:21 +010050#endif
Gilles Peskine6194dc22018-11-16 22:24:15 +010051
Paul Elliott3a5a1072020-12-17 18:33:40 +000052#define PSA_ITS_STORAGE_FILENAME_PATTERN "%08x%08x"
Gilles Peskine6194dc22018-11-16 22:24:15 +010053#define PSA_ITS_STORAGE_SUFFIX ".psa_its"
54#define PSA_ITS_STORAGE_FILENAME_LENGTH \
55 ( sizeof( PSA_ITS_STORAGE_PREFIX ) - 1 + /*prefix without terminating 0*/ \
56 16 + /*UID (64-bit number in hex)*/ \
57 sizeof( PSA_ITS_STORAGE_SUFFIX ) - 1 + /*suffix without terminating 0*/ \
58 1 /*terminating null byte*/ )
59#define PSA_ITS_STORAGE_TEMP \
60 PSA_ITS_STORAGE_PREFIX "tempfile" PSA_ITS_STORAGE_SUFFIX
61
62/* The maximum value of psa_storage_info_t.size */
63#define PSA_ITS_MAX_SIZE 0xffffffff
64
65#define PSA_ITS_MAGIC_STRING "PSA\0ITS\0"
66#define PSA_ITS_MAGIC_LENGTH 8
67
Darryl Green86095bc2019-04-11 14:21:14 +010068/* As rename fails on Windows if the new filepath already exists,
69 * use MoveFileExA with the MOVEFILE_REPLACE_EXISTING flag instead.
70 * Returns 0 on success, nonzero on failure. */
Darryl Greenfdda7de2019-04-11 12:54:02 +010071#if defined(_WIN32)
72#define rename_replace_existing( oldpath, newpath ) \
Darryl Green86095bc2019-04-11 14:21:14 +010073 ( ! MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING ) )
Darryl Greenfdda7de2019-04-11 12:54:02 +010074#else
75#define rename_replace_existing( oldpath, newpath ) rename( oldpath, newpath )
76#endif
77
Gilles Peskine6194dc22018-11-16 22:24:15 +010078typedef struct
79{
80 uint8_t magic[PSA_ITS_MAGIC_LENGTH];
81 uint8_t size[sizeof( uint32_t )];
82 uint8_t flags[sizeof( psa_storage_create_flags_t )];
83} psa_its_file_header_t;
84
85static void psa_its_fill_filename( psa_storage_uid_t uid, char *filename )
86{
87 /* Break up the UID into two 32-bit pieces so as not to rely on
88 * long long support in snprintf. */
89 mbedtls_snprintf( filename, PSA_ITS_STORAGE_FILENAME_LENGTH,
90 "%s" PSA_ITS_STORAGE_FILENAME_PATTERN "%s",
91 PSA_ITS_STORAGE_PREFIX,
Paul Elliott3a5a1072020-12-17 18:33:40 +000092 (unsigned) ( uid >> 32 ),
93 (unsigned) ( uid & 0xffffffff ),
Gilles Peskine6194dc22018-11-16 22:24:15 +010094 PSA_ITS_STORAGE_SUFFIX );
95}
96
97static psa_status_t psa_its_read_file( psa_storage_uid_t uid,
98 struct psa_storage_info_t *p_info,
99 FILE **p_stream )
100{
101 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
102 psa_its_file_header_t header;
103 size_t n;
104
105 *p_stream = NULL;
106 psa_its_fill_filename( uid, filename );
107 *p_stream = fopen( filename, "rb" );
108 if( *p_stream == NULL )
109 return( PSA_ERROR_DOES_NOT_EXIST );
110
111 n = fread( &header, 1, sizeof( header ), *p_stream );
112 if( n != sizeof( header ) )
113 return( PSA_ERROR_DATA_CORRUPT );
114 if( memcmp( header.magic, PSA_ITS_MAGIC_STRING,
115 PSA_ITS_MAGIC_LENGTH ) != 0 )
116 return( PSA_ERROR_DATA_CORRUPT );
117
118 p_info->size = ( header.size[0] |
119 header.size[1] << 8 |
120 header.size[2] << 16 |
121 header.size[3] << 24 );
122 p_info->flags = ( header.flags[0] |
123 header.flags[1] << 8 |
124 header.flags[2] << 16 |
125 header.flags[3] << 24 );
126 return( PSA_SUCCESS );
127}
128
129psa_status_t psa_its_get_info( psa_storage_uid_t uid,
130 struct psa_storage_info_t *p_info )
131{
132 psa_status_t status;
133 FILE *stream = NULL;
134 status = psa_its_read_file( uid, p_info, &stream );
135 if( stream != NULL )
136 fclose( stream );
137 return( status );
138}
139
140psa_status_t psa_its_get( psa_storage_uid_t uid,
141 uint32_t data_offset,
142 uint32_t data_length,
Simon D Hughesbda5a212019-07-10 16:34:21 +0100143 void *p_data,
144 size_t *p_data_length )
Gilles Peskine6194dc22018-11-16 22:24:15 +0100145{
146 psa_status_t status;
147 FILE *stream = NULL;
148 size_t n;
149 struct psa_storage_info_t info;
150
151 status = psa_its_read_file( uid, &info, &stream );
152 if( status != PSA_SUCCESS )
153 goto exit;
Gilles Peskinebc1f2722018-11-16 22:24:38 +0100154 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100155 if( data_offset + data_length < data_offset )
156 goto exit;
157#if SIZE_MAX < 0xffffffff
158 if( data_offset + data_length > SIZE_MAX )
159 goto exit;
160#endif
161 if( data_offset + data_length > info.size )
162 goto exit;
163
Gilles Peskinebc1f2722018-11-16 22:24:38 +0100164 status = PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100165#if LONG_MAX < 0xffffffff
166 while( data_offset > LONG_MAX )
167 {
168 if( fseek( stream, LONG_MAX, SEEK_CUR ) != 0 )
169 goto exit;
170 data_offset -= LONG_MAX;
171 }
172#endif
173 if( fseek( stream, data_offset, SEEK_CUR ) != 0 )
174 goto exit;
175 n = fread( p_data, 1, data_length, stream );
176 if( n != data_length )
177 goto exit;
178 status = PSA_SUCCESS;
Simon D Hughesbda5a212019-07-10 16:34:21 +0100179 if( p_data_length != NULL )
180 *p_data_length = n;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100181
182exit:
183 if( stream != NULL )
184 fclose( stream );
185 return( status );
186}
187
188psa_status_t psa_its_set( psa_storage_uid_t uid,
189 uint32_t data_length,
190 const void *p_data,
191 psa_storage_create_flags_t create_flags )
192{
193 psa_status_t status = PSA_ERROR_STORAGE_FAILURE;
194 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
195 FILE *stream = NULL;
196 psa_its_file_header_t header;
197 size_t n;
198
199 memcpy( header.magic, PSA_ITS_MAGIC_STRING, PSA_ITS_MAGIC_LENGTH );
Joe Subbianic54e9082021-07-19 11:56:54 +0100200 MBEDTLS_PUT_UINT32_LE( data_length, header.size, 0 );
201 MBEDTLS_PUT_UINT32_LE( create_flags, header.flags, 0 );
Gilles Peskine6194dc22018-11-16 22:24:15 +0100202
203 psa_its_fill_filename( uid, filename );
204 stream = fopen( PSA_ITS_STORAGE_TEMP, "wb" );
205 if( stream == NULL )
206 goto exit;
207
208 status = PSA_ERROR_INSUFFICIENT_STORAGE;
209 n = fwrite( &header, 1, sizeof( header ), stream );
210 if( n != sizeof( header ) )
211 goto exit;
Andrzej Kurekdc22d8d2019-09-05 09:34:34 -0400212 if( data_length != 0 )
213 {
214 n = fwrite( p_data, 1, data_length, stream );
215 if( n != data_length )
216 goto exit;
217 }
Gilles Peskine6194dc22018-11-16 22:24:15 +0100218 status = PSA_SUCCESS;
219
220exit:
221 if( stream != NULL )
222 {
223 int ret = fclose( stream );
224 if( status == PSA_SUCCESS && ret != 0 )
225 status = PSA_ERROR_INSUFFICIENT_STORAGE;
226 }
227 if( status == PSA_SUCCESS )
228 {
Darryl Greenfdda7de2019-04-11 12:54:02 +0100229 if( rename_replace_existing( PSA_ITS_STORAGE_TEMP, filename ) != 0 )
Gilles Peskine6194dc22018-11-16 22:24:15 +0100230 status = PSA_ERROR_STORAGE_FAILURE;
231 }
Gilles Peskinebab1b522020-08-25 22:49:19 +0200232 /* The temporary file may still exist, but only in failure cases where
233 * we're already reporting an error. So there's nothing we can do on
234 * failure. If the function succeeded, and in some error cases, the
235 * temporary file doesn't exist and so remove() is expected to fail.
236 * Thus we just ignore the return status of remove(). */
237 (void) remove( PSA_ITS_STORAGE_TEMP );
Gilles Peskine6194dc22018-11-16 22:24:15 +0100238 return( status );
239}
240
241psa_status_t psa_its_remove( psa_storage_uid_t uid )
242{
243 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
244 FILE *stream;
245 psa_its_fill_filename( uid, filename );
246 stream = fopen( filename, "rb" );
247 if( stream == NULL )
248 return( PSA_ERROR_DOES_NOT_EXIST );
249 fclose( stream );
250 if( remove( filename ) != 0 )
251 return( PSA_ERROR_STORAGE_FAILURE );
252 return( PSA_SUCCESS );
253}
254
255#endif /* MBEDTLS_PSA_ITS_FILE_C */