blob: 86e2c42a924f4e909cfe7329e5b5358fb107cf1a [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/*
5 * Copyright (C) 2018, ARM Limited, All Rights Reserved
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.
19 *
20 * This file is part of mbed TLS (https://tls.mbed.org)
21 */
22
23#if defined(MBEDTLS_CONFIG_FILE)
24#include MBEDTLS_CONFIG_FILE
25#else
26#include "mbedtls/config.h"
27#endif
28
29#if defined(MBEDTLS_PSA_ITS_FILE_C)
30
31#if defined(MBEDTLS_PLATFORM_C)
32#include "mbedtls/platform.h"
33#else
34#define mbedtls_snprintf snprintf
35#endif
36
Darryl Greenb4679342019-04-10 15:37:06 +010037#if defined(_WIN32)
38#include <windows.h>
39#endif
40
Gilles Peskine6194dc22018-11-16 22:24:15 +010041#include "psa_crypto_its.h"
42
43#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
52#define PSA_ITS_STORAGE_FILENAME_PATTERN "%08lx%08lx"
53#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,
92 (unsigned long) ( uid >> 32 ),
93 (unsigned long) ( uid & 0xffffffff ),
94 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 );
200 header.size[0] = data_length & 0xff;
201 header.size[1] = ( data_length >> 8 ) & 0xff;
202 header.size[2] = ( data_length >> 16 ) & 0xff;
203 header.size[3] = ( data_length >> 24 ) & 0xff;
204 header.flags[0] = create_flags & 0xff;
205 header.flags[1] = ( create_flags >> 8 ) & 0xff;
206 header.flags[2] = ( create_flags >> 16 ) & 0xff;
207 header.flags[3] = ( create_flags >> 24 ) & 0xff;
208
209 psa_its_fill_filename( uid, filename );
210 stream = fopen( PSA_ITS_STORAGE_TEMP, "wb" );
211 if( stream == NULL )
212 goto exit;
213
214 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 }
238 remove( PSA_ITS_STORAGE_TEMP );
239 return( status );
240}
241
242psa_status_t psa_its_remove( psa_storage_uid_t uid )
243{
244 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
245 FILE *stream;
246 psa_its_fill_filename( uid, filename );
247 stream = fopen( filename, "rb" );
248 if( stream == NULL )
249 return( PSA_ERROR_DOES_NOT_EXIST );
250 fclose( stream );
251 if( remove( filename ) != 0 )
252 return( PSA_ERROR_STORAGE_FAILURE );
253 return( PSA_SUCCESS );
254}
255
256#endif /* MBEDTLS_PSA_ITS_FILE_C */