blob: 05ca8afc79a01beecd2c1b68359bbee7fb8f42e2 [file] [log] [blame]
Gilles Peskine6194dc22018-11-16 22:24:15 +01001/*
2 * PSA ITS simulator over stdio files.
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if defined(MBEDTLS_CONFIG_FILE)
23#include MBEDTLS_CONFIG_FILE
24#else
25#include "mbedtls/config.h"
26#endif
27
28#if defined(MBEDTLS_PSA_ITS_FILE_C)
29
30#if defined(MBEDTLS_PLATFORM_C)
31#include "mbedtls/platform.h"
32#else
33#define mbedtls_snprintf snprintf
34#endif
35
Darryl Greenb4679342019-04-10 15:37:06 +010036#if defined(_WIN32)
37#include <windows.h>
38#endif
39
Gilles Peskine6194dc22018-11-16 22:24:15 +010040#include "psa_crypto_its.h"
41
42#include <limits.h>
43#include <stdint.h>
44#include <stdio.h>
45#include <string.h>
46
Simon D Hughesbda5a212019-07-10 16:34:21 +010047#if !defined(PSA_ITS_STORAGE_PREFIX)
Gilles Peskine6194dc22018-11-16 22:24:15 +010048#define PSA_ITS_STORAGE_PREFIX ""
Simon D Hughesbda5a212019-07-10 16:34:21 +010049#endif
Gilles Peskine6194dc22018-11-16 22:24:15 +010050
51#define PSA_ITS_STORAGE_FILENAME_PATTERN "%08lx%08lx"
52#define PSA_ITS_STORAGE_SUFFIX ".psa_its"
53#define PSA_ITS_STORAGE_FILENAME_LENGTH \
54 ( sizeof( PSA_ITS_STORAGE_PREFIX ) - 1 + /*prefix without terminating 0*/ \
55 16 + /*UID (64-bit number in hex)*/ \
56 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;
217 n = fwrite( p_data, 1, data_length, stream );
218 if( n != data_length )
219 goto exit;
220 status = PSA_SUCCESS;
221
222exit:
223 if( stream != NULL )
224 {
225 int ret = fclose( stream );
226 if( status == PSA_SUCCESS && ret != 0 )
227 status = PSA_ERROR_INSUFFICIENT_STORAGE;
228 }
229 if( status == PSA_SUCCESS )
230 {
Darryl Greenfdda7de2019-04-11 12:54:02 +0100231 if( rename_replace_existing( PSA_ITS_STORAGE_TEMP, filename ) != 0 )
Gilles Peskine6194dc22018-11-16 22:24:15 +0100232 status = PSA_ERROR_STORAGE_FAILURE;
233 }
234 remove( PSA_ITS_STORAGE_TEMP );
235 return( status );
236}
237
238psa_status_t psa_its_remove( psa_storage_uid_t uid )
239{
240 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
241 FILE *stream;
242 psa_its_fill_filename( uid, filename );
243 stream = fopen( filename, "rb" );
244 if( stream == NULL )
245 return( PSA_ERROR_DOES_NOT_EXIST );
246 fclose( stream );
247 if( remove( filename ) != 0 )
248 return( PSA_ERROR_STORAGE_FAILURE );
249 return( PSA_SUCCESS );
250}
251
252#endif /* MBEDTLS_PSA_ITS_FILE_C */