1 /*
2  *  PSA ITS simulator over stdio files.
3  */
4 /*
5  *  Copyright The Mbed TLS Contributors
6  *  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 
21 #include "common.h"
22 
23 #if defined(MBEDTLS_PSA_ITS_FILE_C)
24 
25 #include "mbedtls/platform.h"
26 
27 #if defined(_WIN32)
28 #include <windows.h>
29 #endif
30 
31 #include "psa_crypto_its.h"
32 
33 #include <limits.h>
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <string.h>
37 
38 #if !defined(PSA_ITS_STORAGE_PREFIX)
39 #define PSA_ITS_STORAGE_PREFIX ""
40 #endif
41 
42 #define PSA_ITS_STORAGE_FILENAME_PATTERN "%08x%08x"
43 #define PSA_ITS_STORAGE_SUFFIX ".psa_its"
44 #define PSA_ITS_STORAGE_FILENAME_LENGTH         \
45     ( sizeof( PSA_ITS_STORAGE_PREFIX ) - 1 + /*prefix without terminating 0*/ \
46       16 + /*UID (64-bit number in hex)*/                               \
47       sizeof( PSA_ITS_STORAGE_SUFFIX ) - 1 + /*suffix without terminating 0*/ \
48       1 /*terminating null byte*/ )
49 #define PSA_ITS_STORAGE_TEMP \
50     PSA_ITS_STORAGE_PREFIX "tempfile" PSA_ITS_STORAGE_SUFFIX
51 
52 /* The maximum value of psa_storage_info_t.size */
53 #define PSA_ITS_MAX_SIZE 0xffffffff
54 
55 #define PSA_ITS_MAGIC_STRING "PSA\0ITS\0"
56 #define PSA_ITS_MAGIC_LENGTH 8
57 
58 /* As rename fails on Windows if the new filepath already exists,
59  * use MoveFileExA with the MOVEFILE_REPLACE_EXISTING flag instead.
60  * Returns 0 on success, nonzero on failure. */
61 #if defined(_WIN32)
62 #define rename_replace_existing( oldpath, newpath ) \
63     ( ! MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING ) )
64 #else
65 #define rename_replace_existing( oldpath, newpath ) rename( oldpath, newpath )
66 #endif
67 
68 typedef struct
69 {
70     uint8_t magic[PSA_ITS_MAGIC_LENGTH];
71     uint8_t size[sizeof( uint32_t )];
72     uint8_t flags[sizeof( psa_storage_create_flags_t )];
73 } psa_its_file_header_t;
74 
psa_its_fill_filename(psa_storage_uid_t uid,char * filename)75 static void psa_its_fill_filename( psa_storage_uid_t uid, char *filename )
76 {
77     /* Break up the UID into two 32-bit pieces so as not to rely on
78      * long long support in snprintf. */
79     mbedtls_snprintf( filename, PSA_ITS_STORAGE_FILENAME_LENGTH,
80                       "%s" PSA_ITS_STORAGE_FILENAME_PATTERN "%s",
81                       PSA_ITS_STORAGE_PREFIX,
82                       (unsigned) ( uid >> 32 ),
83                       (unsigned) ( uid & 0xffffffff ),
84                       PSA_ITS_STORAGE_SUFFIX );
85 }
86 
psa_its_read_file(psa_storage_uid_t uid,struct psa_storage_info_t * p_info,FILE ** p_stream)87 static psa_status_t psa_its_read_file( psa_storage_uid_t uid,
88                                        struct psa_storage_info_t *p_info,
89                                        FILE **p_stream )
90 {
91     char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
92     psa_its_file_header_t header;
93     size_t n;
94 
95     *p_stream = NULL;
96     psa_its_fill_filename( uid, filename );
97     *p_stream = fopen( filename, "rb" );
98     if( *p_stream == NULL )
99         return( PSA_ERROR_DOES_NOT_EXIST );
100 
101     /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
102     mbedtls_setbuf( *p_stream, NULL );
103 
104     n = fread( &header, 1, sizeof( header ), *p_stream );
105     if( n != sizeof( header ) )
106         return( PSA_ERROR_DATA_CORRUPT );
107     if( memcmp( header.magic, PSA_ITS_MAGIC_STRING,
108                 PSA_ITS_MAGIC_LENGTH ) != 0 )
109         return( PSA_ERROR_DATA_CORRUPT );
110 
111     p_info->size = ( header.size[0] |
112                      header.size[1] << 8 |
113                      header.size[2] << 16 |
114                      header.size[3] << 24 );
115     p_info->flags = ( header.flags[0] |
116                       header.flags[1] << 8 |
117                       header.flags[2] << 16 |
118                       header.flags[3] << 24 );
119     return( PSA_SUCCESS );
120 }
121 
psa_its_get_info(psa_storage_uid_t uid,struct psa_storage_info_t * p_info)122 psa_status_t psa_its_get_info( psa_storage_uid_t uid,
123                                struct psa_storage_info_t *p_info )
124 {
125     psa_status_t status;
126     FILE *stream = NULL;
127     status = psa_its_read_file( uid, p_info, &stream );
128     if( stream != NULL )
129         fclose( stream );
130     return( status );
131 }
132 
psa_its_get(psa_storage_uid_t uid,uint32_t data_offset,uint32_t data_length,void * p_data,size_t * p_data_length)133 psa_status_t psa_its_get( psa_storage_uid_t uid,
134                           uint32_t data_offset,
135                           uint32_t data_length,
136                           void *p_data,
137                           size_t *p_data_length )
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;
147     status = PSA_ERROR_INVALID_ARGUMENT;
148     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 
157     status = PSA_ERROR_STORAGE_FAILURE;
158 #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     if( p_data_length != NULL )
173         *p_data_length = n;
174 
175 exit:
176     if( stream != NULL )
177         fclose( stream );
178     return( status );
179 }
180 
psa_its_set(psa_storage_uid_t uid,uint32_t data_length,const void * p_data,psa_storage_create_flags_t create_flags)181 psa_status_t psa_its_set( psa_storage_uid_t uid,
182                           uint32_t data_length,
183                           const void *p_data,
184                           psa_storage_create_flags_t create_flags )
185 {
186     if( uid == 0 )
187     {
188         return( PSA_ERROR_INVALID_HANDLE );
189     }
190 
191     psa_status_t status = PSA_ERROR_STORAGE_FAILURE;
192     char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
193     FILE *stream = NULL;
194     psa_its_file_header_t header;
195     size_t n;
196 
197     memcpy( header.magic, PSA_ITS_MAGIC_STRING, PSA_ITS_MAGIC_LENGTH );
198     MBEDTLS_PUT_UINT32_LE( data_length, header.size, 0 );
199     MBEDTLS_PUT_UINT32_LE( create_flags, header.flags, 0 );
200 
201     psa_its_fill_filename( uid, filename );
202     stream = fopen( PSA_ITS_STORAGE_TEMP, "wb" );
203 
204     if( stream == NULL )
205         goto exit;
206 
207     /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
208     mbedtls_setbuf( stream, NULL );
209 
210     status = PSA_ERROR_INSUFFICIENT_STORAGE;
211     n = fwrite( &header, 1, sizeof( header ), stream );
212     if( n != sizeof( header ) )
213         goto exit;
214     if( data_length != 0 )
215     {
216         n = fwrite( p_data, 1, data_length, stream );
217         if( n != data_length )
218             goto exit;
219     }
220     status = PSA_SUCCESS;
221 
222 exit:
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     {
231         if( rename_replace_existing( PSA_ITS_STORAGE_TEMP, filename ) != 0 )
232             status = PSA_ERROR_STORAGE_FAILURE;
233     }
234     /* The temporary file may still exist, but only in failure cases where
235      * we're already reporting an error. So there's nothing we can do on
236      * failure. If the function succeeded, and in some error cases, the
237      * temporary file doesn't exist and so remove() is expected to fail.
238      * Thus we just ignore the return status of remove(). */
239     (void) remove( PSA_ITS_STORAGE_TEMP );
240     return( status );
241 }
242 
psa_its_remove(psa_storage_uid_t uid)243 psa_status_t psa_its_remove( psa_storage_uid_t uid )
244 {
245     char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
246     FILE *stream;
247     psa_its_fill_filename( uid, filename );
248     stream = fopen( filename, "rb" );
249     if( stream == NULL )
250         return( PSA_ERROR_DOES_NOT_EXIST );
251     fclose( stream );
252     if( remove( filename ) != 0 )
253         return( PSA_ERROR_STORAGE_FAILURE );
254     return( PSA_SUCCESS );
255 }
256 
257 #endif /* MBEDTLS_PSA_ITS_FILE_C */
258