Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 1 | /* |
| 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 Green | b467934 | 2019-04-10 15:37:06 +0100 | [diff] [blame^] | 36 | #if defined(_WIN32) |
| 37 | #include <windows.h> |
| 38 | #endif |
| 39 | |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 40 | #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 | |
| 65 | typedef struct |
| 66 | { |
| 67 | uint8_t magic[PSA_ITS_MAGIC_LENGTH]; |
| 68 | uint8_t size[sizeof( uint32_t )]; |
| 69 | uint8_t flags[sizeof( psa_storage_create_flags_t )]; |
| 70 | } psa_its_file_header_t; |
| 71 | |
| 72 | static void psa_its_fill_filename( psa_storage_uid_t uid, char *filename ) |
| 73 | { |
| 74 | /* Break up the UID into two 32-bit pieces so as not to rely on |
| 75 | * long long support in snprintf. */ |
| 76 | mbedtls_snprintf( filename, PSA_ITS_STORAGE_FILENAME_LENGTH, |
| 77 | "%s" PSA_ITS_STORAGE_FILENAME_PATTERN "%s", |
| 78 | PSA_ITS_STORAGE_PREFIX, |
| 79 | (unsigned long) ( uid >> 32 ), |
| 80 | (unsigned long) ( uid & 0xffffffff ), |
| 81 | PSA_ITS_STORAGE_SUFFIX ); |
| 82 | } |
| 83 | |
| 84 | static psa_status_t psa_its_read_file( psa_storage_uid_t uid, |
| 85 | struct psa_storage_info_t *p_info, |
| 86 | FILE **p_stream ) |
| 87 | { |
| 88 | char filename[PSA_ITS_STORAGE_FILENAME_LENGTH]; |
| 89 | psa_its_file_header_t header; |
| 90 | size_t n; |
| 91 | |
| 92 | *p_stream = NULL; |
| 93 | psa_its_fill_filename( uid, filename ); |
| 94 | *p_stream = fopen( filename, "rb" ); |
| 95 | if( *p_stream == NULL ) |
| 96 | return( PSA_ERROR_DOES_NOT_EXIST ); |
| 97 | |
| 98 | n = fread( &header, 1, sizeof( header ), *p_stream ); |
| 99 | if( n != sizeof( header ) ) |
| 100 | return( PSA_ERROR_DATA_CORRUPT ); |
| 101 | if( memcmp( header.magic, PSA_ITS_MAGIC_STRING, |
| 102 | PSA_ITS_MAGIC_LENGTH ) != 0 ) |
| 103 | return( PSA_ERROR_DATA_CORRUPT ); |
| 104 | |
| 105 | p_info->size = ( header.size[0] | |
| 106 | header.size[1] << 8 | |
| 107 | header.size[2] << 16 | |
| 108 | header.size[3] << 24 ); |
| 109 | p_info->flags = ( header.flags[0] | |
| 110 | header.flags[1] << 8 | |
| 111 | header.flags[2] << 16 | |
| 112 | header.flags[3] << 24 ); |
| 113 | return( PSA_SUCCESS ); |
| 114 | } |
| 115 | |
| 116 | psa_status_t psa_its_get_info( psa_storage_uid_t uid, |
| 117 | struct psa_storage_info_t *p_info ) |
| 118 | { |
| 119 | psa_status_t status; |
| 120 | FILE *stream = NULL; |
| 121 | status = psa_its_read_file( uid, p_info, &stream ); |
| 122 | if( stream != NULL ) |
| 123 | fclose( stream ); |
| 124 | return( status ); |
| 125 | } |
| 126 | |
| 127 | psa_status_t psa_its_get( psa_storage_uid_t uid, |
| 128 | uint32_t data_offset, |
| 129 | uint32_t data_length, |
| 130 | void *p_data ) |
| 131 | { |
| 132 | psa_status_t status; |
| 133 | FILE *stream = NULL; |
| 134 | size_t n; |
| 135 | struct psa_storage_info_t info; |
| 136 | |
| 137 | status = psa_its_read_file( uid, &info, &stream ); |
| 138 | if( status != PSA_SUCCESS ) |
| 139 | goto exit; |
Gilles Peskine | bc1f272 | 2018-11-16 22:24:38 +0100 | [diff] [blame] | 140 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 141 | if( data_offset + data_length < data_offset ) |
| 142 | goto exit; |
| 143 | #if SIZE_MAX < 0xffffffff |
| 144 | if( data_offset + data_length > SIZE_MAX ) |
| 145 | goto exit; |
| 146 | #endif |
| 147 | if( data_offset + data_length > info.size ) |
| 148 | goto exit; |
| 149 | |
Gilles Peskine | bc1f272 | 2018-11-16 22:24:38 +0100 | [diff] [blame] | 150 | status = PSA_ERROR_STORAGE_FAILURE; |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 151 | #if LONG_MAX < 0xffffffff |
| 152 | while( data_offset > LONG_MAX ) |
| 153 | { |
| 154 | if( fseek( stream, LONG_MAX, SEEK_CUR ) != 0 ) |
| 155 | goto exit; |
| 156 | data_offset -= LONG_MAX; |
| 157 | } |
| 158 | #endif |
| 159 | if( fseek( stream, data_offset, SEEK_CUR ) != 0 ) |
| 160 | goto exit; |
| 161 | n = fread( p_data, 1, data_length, stream ); |
| 162 | if( n != data_length ) |
| 163 | goto exit; |
| 164 | status = PSA_SUCCESS; |
| 165 | |
| 166 | exit: |
| 167 | if( stream != NULL ) |
| 168 | fclose( stream ); |
| 169 | return( status ); |
| 170 | } |
| 171 | |
| 172 | psa_status_t psa_its_set( psa_storage_uid_t uid, |
| 173 | uint32_t data_length, |
| 174 | const void *p_data, |
| 175 | psa_storage_create_flags_t create_flags ) |
| 176 | { |
| 177 | psa_status_t status = PSA_ERROR_STORAGE_FAILURE; |
| 178 | char filename[PSA_ITS_STORAGE_FILENAME_LENGTH]; |
| 179 | FILE *stream = NULL; |
| 180 | psa_its_file_header_t header; |
| 181 | size_t n; |
| 182 | |
| 183 | memcpy( header.magic, PSA_ITS_MAGIC_STRING, PSA_ITS_MAGIC_LENGTH ); |
| 184 | header.size[0] = data_length & 0xff; |
| 185 | header.size[1] = ( data_length >> 8 ) & 0xff; |
| 186 | header.size[2] = ( data_length >> 16 ) & 0xff; |
| 187 | header.size[3] = ( data_length >> 24 ) & 0xff; |
| 188 | header.flags[0] = create_flags & 0xff; |
| 189 | header.flags[1] = ( create_flags >> 8 ) & 0xff; |
| 190 | header.flags[2] = ( create_flags >> 16 ) & 0xff; |
| 191 | header.flags[3] = ( create_flags >> 24 ) & 0xff; |
| 192 | |
| 193 | psa_its_fill_filename( uid, filename ); |
| 194 | stream = fopen( PSA_ITS_STORAGE_TEMP, "wb" ); |
| 195 | if( stream == NULL ) |
| 196 | goto exit; |
| 197 | |
| 198 | status = PSA_ERROR_INSUFFICIENT_STORAGE; |
| 199 | n = fwrite( &header, 1, sizeof( header ), stream ); |
| 200 | if( n != sizeof( header ) ) |
| 201 | goto exit; |
| 202 | n = fwrite( p_data, 1, data_length, stream ); |
| 203 | if( n != data_length ) |
| 204 | goto exit; |
| 205 | status = PSA_SUCCESS; |
| 206 | |
| 207 | exit: |
| 208 | if( stream != NULL ) |
| 209 | { |
| 210 | int ret = fclose( stream ); |
| 211 | if( status == PSA_SUCCESS && ret != 0 ) |
| 212 | status = PSA_ERROR_INSUFFICIENT_STORAGE; |
| 213 | } |
| 214 | if( status == PSA_SUCCESS ) |
| 215 | { |
Darryl Green | b467934 | 2019-04-10 15:37:06 +0100 | [diff] [blame^] | 216 | #if defined(_WIN32) |
| 217 | if( MoveFileExA( PSA_ITS_STORAGE_TEMP, filename, |
| 218 | MOVEFILE_REPLACE_EXISTING ) == 0 ) |
| 219 | #else |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 220 | if( rename( PSA_ITS_STORAGE_TEMP, filename ) != 0 ) |
Darryl Green | b467934 | 2019-04-10 15:37:06 +0100 | [diff] [blame^] | 221 | #endif |
Gilles Peskine | 6194dc2 | 2018-11-16 22:24:15 +0100 | [diff] [blame] | 222 | status = PSA_ERROR_STORAGE_FAILURE; |
| 223 | } |
| 224 | remove( PSA_ITS_STORAGE_TEMP ); |
| 225 | return( status ); |
| 226 | } |
| 227 | |
| 228 | psa_status_t psa_its_remove( psa_storage_uid_t uid ) |
| 229 | { |
| 230 | char filename[PSA_ITS_STORAGE_FILENAME_LENGTH]; |
| 231 | FILE *stream; |
| 232 | psa_its_fill_filename( uid, filename ); |
| 233 | stream = fopen( filename, "rb" ); |
| 234 | if( stream == NULL ) |
| 235 | return( PSA_ERROR_DOES_NOT_EXIST ); |
| 236 | fclose( stream ); |
| 237 | if( remove( filename ) != 0 ) |
| 238 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 239 | return( PSA_SUCCESS ); |
| 240 | } |
| 241 | |
| 242 | #endif /* MBEDTLS_PSA_ITS_FILE_C */ |