blob: bc0f84caed9b7a67552a44c230dbf0fb9c3c8643 [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 Greenfdda7de2019-04-11 12:54:02 +010065#if defined(_WIN32)
66#define rename_replace_existing( oldpath, newpath ) \
67 (!MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING ))
68#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,
86 (unsigned long) ( uid >> 32 ),
87 (unsigned long) ( uid & 0xffffffff ),
88 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
105 n = fread( &header, 1, sizeof( header ), *p_stream );
106 if( n != sizeof( header ) )
107 return( PSA_ERROR_DATA_CORRUPT );
108 if( memcmp( header.magic, PSA_ITS_MAGIC_STRING,
109 PSA_ITS_MAGIC_LENGTH ) != 0 )
110 return( PSA_ERROR_DATA_CORRUPT );
111
112 p_info->size = ( header.size[0] |
113 header.size[1] << 8 |
114 header.size[2] << 16 |
115 header.size[3] << 24 );
116 p_info->flags = ( header.flags[0] |
117 header.flags[1] << 8 |
118 header.flags[2] << 16 |
119 header.flags[3] << 24 );
120 return( PSA_SUCCESS );
121}
122
123psa_status_t psa_its_get_info( psa_storage_uid_t uid,
124 struct psa_storage_info_t *p_info )
125{
126 psa_status_t status;
127 FILE *stream = NULL;
128 status = psa_its_read_file( uid, p_info, &stream );
129 if( stream != NULL )
130 fclose( stream );
131 return( status );
132}
133
134psa_status_t psa_its_get( psa_storage_uid_t uid,
135 uint32_t data_offset,
136 uint32_t data_length,
137 void *p_data )
138{
139 psa_status_t status;
140 FILE *stream = NULL;
141 size_t n;
142 struct psa_storage_info_t info;
143
144 status = psa_its_read_file( uid, &info, &stream );
145 if( status != PSA_SUCCESS )
146 goto exit;
Gilles Peskinebc1f2722018-11-16 22:24:38 +0100147 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100148 if( data_offset + data_length < data_offset )
149 goto exit;
150#if SIZE_MAX < 0xffffffff
151 if( data_offset + data_length > SIZE_MAX )
152 goto exit;
153#endif
154 if( data_offset + data_length > info.size )
155 goto exit;
156
Gilles Peskinebc1f2722018-11-16 22:24:38 +0100157 status = PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine6194dc22018-11-16 22:24:15 +0100158#if LONG_MAX < 0xffffffff
159 while( data_offset > LONG_MAX )
160 {
161 if( fseek( stream, LONG_MAX, SEEK_CUR ) != 0 )
162 goto exit;
163 data_offset -= LONG_MAX;
164 }
165#endif
166 if( fseek( stream, data_offset, SEEK_CUR ) != 0 )
167 goto exit;
168 n = fread( p_data, 1, data_length, stream );
169 if( n != data_length )
170 goto exit;
171 status = PSA_SUCCESS;
172
173exit:
174 if( stream != NULL )
175 fclose( stream );
176 return( status );
177}
178
179psa_status_t psa_its_set( psa_storage_uid_t uid,
180 uint32_t data_length,
181 const void *p_data,
182 psa_storage_create_flags_t create_flags )
183{
184 psa_status_t status = PSA_ERROR_STORAGE_FAILURE;
185 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
186 FILE *stream = NULL;
187 psa_its_file_header_t header;
188 size_t n;
189
190 memcpy( header.magic, PSA_ITS_MAGIC_STRING, PSA_ITS_MAGIC_LENGTH );
191 header.size[0] = data_length & 0xff;
192 header.size[1] = ( data_length >> 8 ) & 0xff;
193 header.size[2] = ( data_length >> 16 ) & 0xff;
194 header.size[3] = ( data_length >> 24 ) & 0xff;
195 header.flags[0] = create_flags & 0xff;
196 header.flags[1] = ( create_flags >> 8 ) & 0xff;
197 header.flags[2] = ( create_flags >> 16 ) & 0xff;
198 header.flags[3] = ( create_flags >> 24 ) & 0xff;
199
200 psa_its_fill_filename( uid, filename );
201 stream = fopen( PSA_ITS_STORAGE_TEMP, "wb" );
202 if( stream == NULL )
203 goto exit;
204
205 status = PSA_ERROR_INSUFFICIENT_STORAGE;
206 n = fwrite( &header, 1, sizeof( header ), stream );
207 if( n != sizeof( header ) )
208 goto exit;
209 n = fwrite( p_data, 1, data_length, stream );
210 if( n != data_length )
211 goto exit;
212 status = PSA_SUCCESS;
213
214exit:
215 if( stream != NULL )
216 {
217 int ret = fclose( stream );
218 if( status == PSA_SUCCESS && ret != 0 )
219 status = PSA_ERROR_INSUFFICIENT_STORAGE;
220 }
221 if( status == PSA_SUCCESS )
222 {
Darryl Greenfdda7de2019-04-11 12:54:02 +0100223 if( rename_replace_existing( PSA_ITS_STORAGE_TEMP, filename ) != 0 )
Gilles Peskine6194dc22018-11-16 22:24:15 +0100224 status = PSA_ERROR_STORAGE_FAILURE;
225 }
226 remove( PSA_ITS_STORAGE_TEMP );
227 return( status );
228}
229
230psa_status_t psa_its_remove( psa_storage_uid_t uid )
231{
232 char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
233 FILE *stream;
234 psa_its_fill_filename( uid, filename );
235 stream = fopen( filename, "rb" );
236 if( stream == NULL )
237 return( PSA_ERROR_DOES_NOT_EXIST );
238 fclose( stream );
239 if( remove( filename ) != 0 )
240 return( PSA_ERROR_STORAGE_FAILURE );
241 return( PSA_SUCCESS );
242}
243
244#endif /* MBEDTLS_PSA_ITS_FILE_C */