| Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 1 | /* | 
|  | 2 | *  PSA ITS simulator over stdio files. | 
|  | 3 | */ | 
| Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 4 | /* | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | *  Copyright The Mbed TLS Contributors | 
| Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 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. | 
| Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 19 | */ | 
|  | 20 |  | 
| Mateusz Starzyk | 03f0030 | 2021-05-27 14:40:40 +0200 | [diff] [blame] | 21 | #include "common.h" | 
| Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 22 |  | 
|  | 23 | #if defined(MBEDTLS_PSA_ITS_FILE_C) | 
|  | 24 |  | 
|  | 25 | #if defined(MBEDTLS_PLATFORM_C) | 
|  | 26 | #include "mbedtls/platform.h" | 
|  | 27 | #else | 
|  | 28 | #define mbedtls_snprintf   snprintf | 
|  | 29 | #endif | 
|  | 30 |  | 
| Darryl Green | b467934 | 2019-04-10 15:37:06 +0100 | [diff] [blame] | 31 | #if defined(_WIN32) | 
|  | 32 | #include <windows.h> | 
|  | 33 | #endif | 
|  | 34 |  | 
| Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 35 | #include "psa_crypto_its.h" | 
|  | 36 |  | 
|  | 37 | #include <limits.h> | 
|  | 38 | #include <stdint.h> | 
|  | 39 | #include <stdio.h> | 
|  | 40 | #include <string.h> | 
|  | 41 |  | 
| Simon D Hughes | bda5a21 | 2019-07-10 16:34:21 +0100 | [diff] [blame] | 42 | #if !defined(PSA_ITS_STORAGE_PREFIX) | 
| Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 43 | #define PSA_ITS_STORAGE_PREFIX "" | 
| Simon D Hughes | bda5a21 | 2019-07-10 16:34:21 +0100 | [diff] [blame] | 44 | #endif | 
| Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 45 |  | 
| Paul Elliott | 3a5a107 | 2020-12-17 18:33:40 +0000 | [diff] [blame] | 46 | #define PSA_ITS_STORAGE_FILENAME_PATTERN "%08x%08x" | 
| Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 47 | #define PSA_ITS_STORAGE_SUFFIX ".psa_its" | 
|  | 48 | #define PSA_ITS_STORAGE_FILENAME_LENGTH         \ | 
|  | 49 | ( sizeof( PSA_ITS_STORAGE_PREFIX ) - 1 + /*prefix without terminating 0*/ \ | 
|  | 50 | 16 + /*UID (64-bit number in hex)*/                               \ | 
|  | 51 | sizeof( PSA_ITS_STORAGE_SUFFIX ) - 1 + /*suffix without terminating 0*/ \ | 
|  | 52 | 1 /*terminating null byte*/ ) | 
|  | 53 | #define PSA_ITS_STORAGE_TEMP \ | 
|  | 54 | PSA_ITS_STORAGE_PREFIX "tempfile" PSA_ITS_STORAGE_SUFFIX | 
|  | 55 |  | 
|  | 56 | /* The maximum value of psa_storage_info_t.size */ | 
|  | 57 | #define PSA_ITS_MAX_SIZE 0xffffffff | 
|  | 58 |  | 
|  | 59 | #define PSA_ITS_MAGIC_STRING "PSA\0ITS\0" | 
|  | 60 | #define PSA_ITS_MAGIC_LENGTH 8 | 
|  | 61 |  | 
| Darryl Green | 86095bc | 2019-04-11 14:21:14 +0100 | [diff] [blame] | 62 | /* As rename fails on Windows if the new filepath already exists, | 
|  | 63 | * use MoveFileExA with the MOVEFILE_REPLACE_EXISTING flag instead. | 
|  | 64 | * Returns 0 on success, nonzero on failure. */ | 
| Darryl Green | fdda7de | 2019-04-11 12:54:02 +0100 | [diff] [blame] | 65 | #if defined(_WIN32) | 
|  | 66 | #define rename_replace_existing( oldpath, newpath ) \ | 
| Darryl Green | 86095bc | 2019-04-11 14:21:14 +0100 | [diff] [blame] | 67 | ( ! MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING ) ) | 
| Darryl Green | fdda7de | 2019-04-11 12:54:02 +0100 | [diff] [blame] | 68 | #else | 
|  | 69 | #define rename_replace_existing( oldpath, newpath ) rename( oldpath, newpath ) | 
|  | 70 | #endif | 
|  | 71 |  | 
| Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 72 | typedef 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 |  | 
|  | 79 | static 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, | 
| Paul Elliott | 3a5a107 | 2020-12-17 18:33:40 +0000 | [diff] [blame] | 86 | (unsigned) ( uid >> 32 ), | 
|  | 87 | (unsigned) ( uid & 0xffffffff ), | 
| Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 88 | PSA_ITS_STORAGE_SUFFIX ); | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | static 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 |  | 
|  | 123 | psa_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 |  | 
|  | 134 | psa_status_t psa_its_get( psa_storage_uid_t uid, | 
|  | 135 | uint32_t data_offset, | 
|  | 136 | uint32_t data_length, | 
| Simon D Hughes | bda5a21 | 2019-07-10 16:34:21 +0100 | [diff] [blame] | 137 | void *p_data, | 
|  | 138 | size_t *p_data_length ) | 
| Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 139 | { | 
|  | 140 | psa_status_t status; | 
|  | 141 | FILE *stream = NULL; | 
|  | 142 | size_t n; | 
|  | 143 | struct psa_storage_info_t info; | 
|  | 144 |  | 
|  | 145 | status = psa_its_read_file( uid, &info, &stream ); | 
|  | 146 | if( status != PSA_SUCCESS ) | 
|  | 147 | goto exit; | 
| Gilles Peskine | bc1f272 | 2018-11-16 22:24:38 +0100 | [diff] [blame] | 148 | status = PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 149 | if( data_offset + data_length < data_offset ) | 
|  | 150 | goto exit; | 
|  | 151 | #if SIZE_MAX < 0xffffffff | 
|  | 152 | if( data_offset + data_length > SIZE_MAX ) | 
|  | 153 | goto exit; | 
|  | 154 | #endif | 
|  | 155 | if( data_offset + data_length > info.size ) | 
|  | 156 | goto exit; | 
|  | 157 |  | 
| Gilles Peskine | bc1f272 | 2018-11-16 22:24:38 +0100 | [diff] [blame] | 158 | status = PSA_ERROR_STORAGE_FAILURE; | 
| Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 159 | #if LONG_MAX < 0xffffffff | 
|  | 160 | while( data_offset > LONG_MAX ) | 
|  | 161 | { | 
|  | 162 | if( fseek( stream, LONG_MAX, SEEK_CUR ) != 0 ) | 
|  | 163 | goto exit; | 
|  | 164 | data_offset -= LONG_MAX; | 
|  | 165 | } | 
|  | 166 | #endif | 
|  | 167 | if( fseek( stream, data_offset, SEEK_CUR ) != 0 ) | 
|  | 168 | goto exit; | 
|  | 169 | n = fread( p_data, 1, data_length, stream ); | 
|  | 170 | if( n != data_length ) | 
|  | 171 | goto exit; | 
|  | 172 | status = PSA_SUCCESS; | 
| Simon D Hughes | bda5a21 | 2019-07-10 16:34:21 +0100 | [diff] [blame] | 173 | if( p_data_length != NULL ) | 
|  | 174 | *p_data_length = n; | 
| Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 175 |  | 
|  | 176 | exit: | 
|  | 177 | if( stream != NULL ) | 
|  | 178 | fclose( stream ); | 
|  | 179 | return( status ); | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | psa_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 | { | 
| pespacek | e990100 | 2022-02-08 13:52:28 +0100 | [diff] [blame] | 187 | if( uid == 0 ) | 
|  | 188 | { | 
| PeterSpace | c2774a3 | 2022-02-11 10:21:16 +0100 | [diff] [blame] | 189 | return( PSA_ERROR_INVALID_HANDLE ); | 
| pespacek | e990100 | 2022-02-08 13:52:28 +0100 | [diff] [blame] | 190 | } | 
|  | 191 |  | 
| Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 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 ); | 
| Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 199 | MBEDTLS_PUT_UINT32_LE( data_length, header.size, 0 ); | 
|  | 200 | MBEDTLS_PUT_UINT32_LE( create_flags, header.flags, 0 ); | 
| Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 201 |  | 
|  | 202 | psa_its_fill_filename( uid, filename ); | 
|  | 203 | stream = fopen( PSA_ITS_STORAGE_TEMP, "wb" ); | 
|  | 204 | if( stream == NULL ) | 
|  | 205 | goto exit; | 
|  | 206 |  | 
|  | 207 | status = PSA_ERROR_INSUFFICIENT_STORAGE; | 
|  | 208 | n = fwrite( &header, 1, sizeof( header ), stream ); | 
|  | 209 | if( n != sizeof( header ) ) | 
|  | 210 | goto exit; | 
| Andrzej Kurek | dc22d8d | 2019-09-05 09:34:34 -0400 | [diff] [blame] | 211 | if( data_length != 0 ) | 
|  | 212 | { | 
|  | 213 | n = fwrite( p_data, 1, data_length, stream ); | 
|  | 214 | if( n != data_length ) | 
|  | 215 | goto exit; | 
|  | 216 | } | 
| Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 217 | status = PSA_SUCCESS; | 
|  | 218 |  | 
|  | 219 | exit: | 
|  | 220 | if( stream != NULL ) | 
|  | 221 | { | 
|  | 222 | int ret = fclose( stream ); | 
|  | 223 | if( status == PSA_SUCCESS && ret != 0 ) | 
|  | 224 | status = PSA_ERROR_INSUFFICIENT_STORAGE; | 
|  | 225 | } | 
|  | 226 | if( status == PSA_SUCCESS ) | 
|  | 227 | { | 
| Darryl Green | fdda7de | 2019-04-11 12:54:02 +0100 | [diff] [blame] | 228 | if( rename_replace_existing( PSA_ITS_STORAGE_TEMP, filename ) != 0 ) | 
| Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 229 | status = PSA_ERROR_STORAGE_FAILURE; | 
|  | 230 | } | 
| Gilles Peskine | bab1b52 | 2020-08-25 22:49:19 +0200 | [diff] [blame] | 231 | /* The temporary file may still exist, but only in failure cases where | 
|  | 232 | * we're already reporting an error. So there's nothing we can do on | 
|  | 233 | * failure. If the function succeeded, and in some error cases, the | 
|  | 234 | * temporary file doesn't exist and so remove() is expected to fail. | 
|  | 235 | * Thus we just ignore the return status of remove(). */ | 
|  | 236 | (void) remove( PSA_ITS_STORAGE_TEMP ); | 
| Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 237 | return( status ); | 
|  | 238 | } | 
|  | 239 |  | 
|  | 240 | psa_status_t psa_its_remove( psa_storage_uid_t uid ) | 
|  | 241 | { | 
|  | 242 | char filename[PSA_ITS_STORAGE_FILENAME_LENGTH]; | 
|  | 243 | FILE *stream; | 
|  | 244 | psa_its_fill_filename( uid, filename ); | 
|  | 245 | stream = fopen( filename, "rb" ); | 
|  | 246 | if( stream == NULL ) | 
|  | 247 | return( PSA_ERROR_DOES_NOT_EXIST ); | 
|  | 248 | fclose( stream ); | 
|  | 249 | if( remove( filename ) != 0 ) | 
|  | 250 | return( PSA_ERROR_STORAGE_FAILURE ); | 
|  | 251 | return( PSA_SUCCESS ); | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | #endif /* MBEDTLS_PSA_ITS_FILE_C */ |