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 | |
Joe Subbiani | d6ea063 | 2021-08-18 12:57:54 +0100 | [diff] [blame] | 21 | #include "common.h" |
| 22 | |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 23 | #if defined(MBEDTLS_PSA_ITS_FILE_C) |
| 24 | |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 25 | #include "mbedtls/platform.h" |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 26 | |
Darryl Green | b467934 | 2019-04-10 15:37:06 +0100 | [diff] [blame] | 27 | #if defined(_WIN32) |
| 28 | #include <windows.h> |
| 29 | #endif |
| 30 | |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 31 | #include "psa_crypto_its.h" |
| 32 | |
| 33 | #include <limits.h> |
| 34 | #include <stdint.h> |
| 35 | #include <stdio.h> |
| 36 | #include <string.h> |
| 37 | |
Simon D Hughes | bda5a21 | 2019-07-10 16:34:21 +0100 | [diff] [blame] | 38 | #if !defined(PSA_ITS_STORAGE_PREFIX) |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 39 | #define PSA_ITS_STORAGE_PREFIX "" |
Simon D Hughes | bda5a21 | 2019-07-10 16:34:21 +0100 | [diff] [blame] | 40 | #endif |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 41 | |
Paul Elliott | 3a5a107 | 2020-12-17 18:33:40 +0000 | [diff] [blame] | 42 | #define PSA_ITS_STORAGE_FILENAME_PATTERN "%08x%08x" |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 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 | |
Darryl Green | 86095bc | 2019-04-11 14:21:14 +0100 | [diff] [blame] | 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. */ |
Darryl Green | fdda7de | 2019-04-11 12:54:02 +0100 | [diff] [blame] | 61 | #if defined(_WIN32) |
| 62 | #define rename_replace_existing( oldpath, newpath ) \ |
Darryl Green | 86095bc | 2019-04-11 14:21:14 +0100 | [diff] [blame] | 63 | ( ! MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING ) ) |
Darryl Green | fdda7de | 2019-04-11 12:54:02 +0100 | [diff] [blame] | 64 | #else |
| 65 | #define rename_replace_existing( oldpath, newpath ) rename( oldpath, newpath ) |
| 66 | #endif |
| 67 | |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 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 | |
| 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, |
Paul Elliott | 3a5a107 | 2020-12-17 18:33:40 +0000 | [diff] [blame] | 82 | (unsigned) ( uid >> 32 ), |
| 83 | (unsigned) ( uid & 0xffffffff ), |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 84 | PSA_ITS_STORAGE_SUFFIX ); |
| 85 | } |
| 86 | |
| 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 | n = fread( &header, 1, sizeof( header ), *p_stream ); |
| 102 | if( n != sizeof( header ) ) |
| 103 | return( PSA_ERROR_DATA_CORRUPT ); |
| 104 | if( memcmp( header.magic, PSA_ITS_MAGIC_STRING, |
| 105 | PSA_ITS_MAGIC_LENGTH ) != 0 ) |
| 106 | return( PSA_ERROR_DATA_CORRUPT ); |
| 107 | |
| 108 | p_info->size = ( header.size[0] | |
| 109 | header.size[1] << 8 | |
| 110 | header.size[2] << 16 | |
| 111 | header.size[3] << 24 ); |
| 112 | p_info->flags = ( header.flags[0] | |
| 113 | header.flags[1] << 8 | |
| 114 | header.flags[2] << 16 | |
| 115 | header.flags[3] << 24 ); |
| 116 | return( PSA_SUCCESS ); |
| 117 | } |
| 118 | |
| 119 | psa_status_t psa_its_get_info( psa_storage_uid_t uid, |
| 120 | struct psa_storage_info_t *p_info ) |
| 121 | { |
| 122 | psa_status_t status; |
| 123 | FILE *stream = NULL; |
| 124 | status = psa_its_read_file( uid, p_info, &stream ); |
| 125 | if( stream != NULL ) |
| 126 | fclose( stream ); |
| 127 | return( status ); |
| 128 | } |
| 129 | |
| 130 | psa_status_t psa_its_get( psa_storage_uid_t uid, |
| 131 | uint32_t data_offset, |
| 132 | uint32_t data_length, |
Simon D Hughes | bda5a21 | 2019-07-10 16:34:21 +0100 | [diff] [blame] | 133 | void *p_data, |
| 134 | size_t *p_data_length ) |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 135 | { |
| 136 | psa_status_t status; |
| 137 | FILE *stream = NULL; |
| 138 | size_t n; |
| 139 | struct psa_storage_info_t info; |
| 140 | |
| 141 | status = psa_its_read_file( uid, &info, &stream ); |
| 142 | if( status != PSA_SUCCESS ) |
| 143 | goto exit; |
Gilles Peskine | bc1f272 | 2018-11-16 22:24:38 +0100 | [diff] [blame] | 144 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 145 | if( data_offset + data_length < data_offset ) |
| 146 | goto exit; |
| 147 | #if SIZE_MAX < 0xffffffff |
| 148 | if( data_offset + data_length > SIZE_MAX ) |
| 149 | goto exit; |
| 150 | #endif |
| 151 | if( data_offset + data_length > info.size ) |
| 152 | goto exit; |
| 153 | |
Gilles Peskine | bc1f272 | 2018-11-16 22:24:38 +0100 | [diff] [blame] | 154 | status = PSA_ERROR_STORAGE_FAILURE; |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 155 | #if LONG_MAX < 0xffffffff |
| 156 | while( data_offset > LONG_MAX ) |
| 157 | { |
| 158 | if( fseek( stream, LONG_MAX, SEEK_CUR ) != 0 ) |
| 159 | goto exit; |
| 160 | data_offset -= LONG_MAX; |
| 161 | } |
| 162 | #endif |
| 163 | if( fseek( stream, data_offset, SEEK_CUR ) != 0 ) |
| 164 | goto exit; |
| 165 | n = fread( p_data, 1, data_length, stream ); |
| 166 | if( n != data_length ) |
| 167 | goto exit; |
| 168 | status = PSA_SUCCESS; |
Simon D Hughes | bda5a21 | 2019-07-10 16:34:21 +0100 | [diff] [blame] | 169 | if( p_data_length != NULL ) |
| 170 | *p_data_length = n; |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 171 | |
| 172 | exit: |
| 173 | if( stream != NULL ) |
| 174 | fclose( stream ); |
| 175 | return( status ); |
| 176 | } |
| 177 | |
| 178 | psa_status_t psa_its_set( psa_storage_uid_t uid, |
| 179 | uint32_t data_length, |
| 180 | const void *p_data, |
| 181 | psa_storage_create_flags_t create_flags ) |
| 182 | { |
pespacek | 55f15c7 | 2022-02-08 13:52:28 +0100 | [diff] [blame] | 183 | if( uid == 0 ) |
| 184 | { |
PeterSpace | 9be6168 | 2022-02-11 10:21:16 +0100 | [diff] [blame] | 185 | return( PSA_ERROR_INVALID_HANDLE ); |
pespacek | 55f15c7 | 2022-02-08 13:52:28 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 188 | psa_status_t status = PSA_ERROR_STORAGE_FAILURE; |
| 189 | char filename[PSA_ITS_STORAGE_FILENAME_LENGTH]; |
| 190 | FILE *stream = NULL; |
| 191 | psa_its_file_header_t header; |
| 192 | size_t n; |
| 193 | |
| 194 | memcpy( header.magic, PSA_ITS_MAGIC_STRING, PSA_ITS_MAGIC_LENGTH ); |
Joe Subbiani | c54e908 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 195 | MBEDTLS_PUT_UINT32_LE( data_length, header.size, 0 ); |
| 196 | MBEDTLS_PUT_UINT32_LE( create_flags, header.flags, 0 ); |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 197 | |
| 198 | psa_its_fill_filename( uid, filename ); |
| 199 | stream = fopen( PSA_ITS_STORAGE_TEMP, "wb" ); |
| 200 | if( stream == NULL ) |
| 201 | goto exit; |
| 202 | |
| 203 | status = PSA_ERROR_INSUFFICIENT_STORAGE; |
| 204 | n = fwrite( &header, 1, sizeof( header ), stream ); |
| 205 | if( n != sizeof( header ) ) |
| 206 | goto exit; |
Andrzej Kurek | dc22d8d | 2019-09-05 09:34:34 -0400 | [diff] [blame] | 207 | if( data_length != 0 ) |
| 208 | { |
| 209 | n = fwrite( p_data, 1, data_length, stream ); |
| 210 | if( n != data_length ) |
| 211 | goto exit; |
| 212 | } |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 213 | status = PSA_SUCCESS; |
| 214 | |
| 215 | exit: |
| 216 | if( stream != NULL ) |
| 217 | { |
| 218 | int ret = fclose( stream ); |
| 219 | if( status == PSA_SUCCESS && ret != 0 ) |
| 220 | status = PSA_ERROR_INSUFFICIENT_STORAGE; |
| 221 | } |
| 222 | if( status == PSA_SUCCESS ) |
| 223 | { |
Darryl Green | fdda7de | 2019-04-11 12:54:02 +0100 | [diff] [blame] | 224 | if( rename_replace_existing( PSA_ITS_STORAGE_TEMP, filename ) != 0 ) |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 225 | status = PSA_ERROR_STORAGE_FAILURE; |
| 226 | } |
Gilles Peskine | bab1b52 | 2020-08-25 22:49:19 +0200 | [diff] [blame] | 227 | /* The temporary file may still exist, but only in failure cases where |
| 228 | * we're already reporting an error. So there's nothing we can do on |
| 229 | * failure. If the function succeeded, and in some error cases, the |
| 230 | * temporary file doesn't exist and so remove() is expected to fail. |
| 231 | * Thus we just ignore the return status of remove(). */ |
| 232 | (void) remove( PSA_ITS_STORAGE_TEMP ); |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 233 | return( status ); |
| 234 | } |
| 235 | |
| 236 | psa_status_t psa_its_remove( psa_storage_uid_t uid ) |
| 237 | { |
| 238 | char filename[PSA_ITS_STORAGE_FILENAME_LENGTH]; |
| 239 | FILE *stream; |
| 240 | psa_its_fill_filename( uid, filename ); |
| 241 | stream = fopen( filename, "rb" ); |
| 242 | if( stream == NULL ) |
| 243 | return( PSA_ERROR_DOES_NOT_EXIST ); |
| 244 | fclose( stream ); |
| 245 | if( remove( filename ) != 0 ) |
| 246 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 247 | return( PSA_SUCCESS ); |
| 248 | } |
| 249 | |
| 250 | #endif /* MBEDTLS_PSA_ITS_FILE_C */ |