blob: 8cdf783a7b624a3839793b8342e2fea22d4c45b4 [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
47#define PSA_ITS_STORAGE_PREFIX ""
48
49#define PSA_ITS_STORAGE_FILENAME_PATTERN "%08lx%08lx"
50#define PSA_ITS_STORAGE_SUFFIX ".psa_its"
51#define PSA_ITS_STORAGE_FILENAME_LENGTH \
52 ( sizeof( PSA_ITS_STORAGE_PREFIX ) - 1 + /*prefix without terminating 0*/ \
53 16 + /*UID (64-bit number in hex)*/ \
54 sizeof( PSA_ITS_STORAGE_SUFFIX ) - 1 + /*suffix without terminating 0*/ \
55 1 /*terminating null byte*/ )
56#define PSA_ITS_STORAGE_TEMP \
57 PSA_ITS_STORAGE_PREFIX "tempfile" PSA_ITS_STORAGE_SUFFIX
58
59/* The maximum value of psa_storage_info_t.size */
60#define PSA_ITS_MAX_SIZE 0xffffffff
61
62#define PSA_ITS_MAGIC_STRING "PSA\0ITS\0"
63#define PSA_ITS_MAGIC_LENGTH 8
64
Darryl Green86095bc2019-04-11 14:21:14 +010065/* As rename fails on Windows if the new filepath already exists,
66 * use MoveFileExA with the MOVEFILE_REPLACE_EXISTING flag instead.
67 * Returns 0 on success, nonzero on failure. */
Darryl Greenfdda7de2019-04-11 12:54:02 +010068#if defined(_WIN32)
69#define rename_replace_existing( oldpath, newpath ) \
Darryl Green86095bc2019-04-11 14:21:14 +010070 ( ! MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING ) )
Darryl Greenfdda7de2019-04-11 12:54:02 +010071#else
72#define rename_replace_existing( oldpath, newpath ) rename( oldpath, newpath )
73#endif
74
Gilles Peskine6194dc22018-11-16 22:24:15 +010075typedef struct
76{
77 uint8_t magic[PSA_ITS_MAGIC_LENGTH];
78 uint8_t size[sizeof( uint32_t )];
79 uint8_t flags[sizeof( psa_storage_create_flags_t )];
80} psa_its_file_header_t;
81
82static void psa_its_fill_filename( psa_storage_uid_t uid, char *filename )
83{
84 /* Break up the UID into two 32-bit pieces so as not to rely on
85 * long long support in snprintf. */
86 mbedtls_snprintf( filename, PSA_ITS_STORAGE_FILENAME_LENGTH,
87 "%s" PSA_ITS_STORAGE_FILENAME_PATTERN "%s",
88 PSA_ITS_STORAGE_PREFIX,
89 (unsigned long) ( uid >> 32 ),
90 (unsigned long) ( uid & 0xffffffff ),
91 PSA_ITS_STORAGE_SUFFIX );
92}
93
94static psa_status_t psa_its_read_file( psa_storage_uid_t uid,
95 struct psa_storage_info_t *p_info,
96 FILE **p_stream )
97{
98 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
99 psa_its_file_header_t header;
100 size_t n;
101
102 *p_stream = NULL;
103 psa_its_fill_filename( uid, filename );
104 *p_stream = fopen( filename, "rb" );
105 if( *p_stream == NULL )
106 return( PSA_ERROR_DOES_NOT_EXIST );
107
108 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,
140 void *p_data )
141{
142 psa_status_t status;
143 FILE *stream = NULL;
144 size_t n;
145 struct psa_storage_info_t info;
146
147 status = psa_its_read_file( uid, &info, &stream );
148 if( status != PSA_SUCCESS )
149 goto exit;
Gilles Peskinebc1f2722018-11-16 22:24:38 +0100150 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100151 if( data_offset + data_length < data_offset )
152 goto exit;
153#if SIZE_MAX < 0xffffffff
154 if( data_offset + data_length > SIZE_MAX )
155 goto exit;
156#endif
157 if( data_offset + data_length > info.size )
158 goto exit;
159
Gilles Peskinebc1f2722018-11-16 22:24:38 +0100160 status = PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100161#if LONG_MAX < 0xffffffff
162 while( data_offset > LONG_MAX )
163 {
164 if( fseek( stream, LONG_MAX, SEEK_CUR ) != 0 )
165 goto exit;
166 data_offset -= LONG_MAX;
167 }
168#endif
169 if( fseek( stream, data_offset, SEEK_CUR ) != 0 )
170 goto exit;
171 n = fread( p_data, 1, data_length, stream );
172 if( n != data_length )
173 goto exit;
174 status = PSA_SUCCESS;
175
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 );
194 header.size[0] = data_length & 0xff;
195 header.size[1] = ( data_length >> 8 ) & 0xff;
196 header.size[2] = ( data_length >> 16 ) & 0xff;
197 header.size[3] = ( data_length >> 24 ) & 0xff;
198 header.flags[0] = create_flags & 0xff;
199 header.flags[1] = ( create_flags >> 8 ) & 0xff;
200 header.flags[2] = ( create_flags >> 16 ) & 0xff;
201 header.flags[3] = ( create_flags >> 24 ) & 0xff;
202
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;
212 n = fwrite( p_data, 1, data_length, stream );
213 if( n != data_length )
214 goto exit;
215 status = PSA_SUCCESS;
216
217exit:
218 if( stream != NULL )
219 {
220 int ret = fclose( stream );
221 if( status == PSA_SUCCESS && ret != 0 )
222 status = PSA_ERROR_INSUFFICIENT_STORAGE;
223 }
224 if( status == PSA_SUCCESS )
225 {
Darryl Greenfdda7de2019-04-11 12:54:02 +0100226 if( rename_replace_existing( PSA_ITS_STORAGE_TEMP, filename ) != 0 )
Gilles Peskine6194dc22018-11-16 22:24:15 +0100227 status = PSA_ERROR_STORAGE_FAILURE;
228 }
229 remove( PSA_ITS_STORAGE_TEMP );
230 return( status );
231}
232
233psa_status_t psa_its_remove( psa_storage_uid_t uid )
234{
235 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
236 FILE *stream;
237 psa_its_fill_filename( uid, filename );
238 stream = fopen( filename, "rb" );
239 if( stream == NULL )
240 return( PSA_ERROR_DOES_NOT_EXIST );
241 fclose( stream );
242 if( remove( filename ) != 0 )
243 return( PSA_ERROR_STORAGE_FAILURE );
244 return( PSA_SUCCESS );
245}
246
247#endif /* MBEDTLS_PSA_ITS_FILE_C */