| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "common.h" |
| 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 |
|
| 31 |
#if defined(_WIN32) |
| 32 |
#include <windows.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
#include "psa_crypto_its.h" |
| 36 |
|
| 37 |
#include <limits.h> |
| 38 |
#include <stdint.h> |
| 39 |
#include <stdio.h> |
| 40 |
#include <string.h> |
| 41 |
|
| 42 |
#if !defined(PSA_ITS_STORAGE_PREFIX) |
| 43 |
#define PSA_ITS_STORAGE_PREFIX "" |
| 44 |
#endif |
| 45 |
|
| 46 |
#define PSA_ITS_STORAGE_FILENAME_PATTERN "%08x%08x" |
| 47 |
#define PSA_ITS_STORAGE_SUFFIX ".psa_its" |
| 48 |
#define PSA_ITS_STORAGE_FILENAME_LENGTH \ |
| 49 |
( sizeof( PSA_ITS_STORAGE_PREFIX ) - 1 + \ |
| 50 |
16 + \ |
| 51 |
sizeof( PSA_ITS_STORAGE_SUFFIX ) - 1 + \ |
| 52 |
1 ) |
| 53 |
#define PSA_ITS_STORAGE_TEMP \ |
| 54 |
PSA_ITS_STORAGE_PREFIX "tempfile" PSA_ITS_STORAGE_SUFFIX |
| 55 |
|
| 56 |
|
| 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 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#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 |
|
| 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 |
|
| 82 |
|
| 83 |
mbedtls_snprintf( filename, PSA_ITS_STORAGE_FILENAME_LENGTH, |
| 84 |
"%s" PSA_ITS_STORAGE_FILENAME_PATTERN "%s", |
| 85 |
PSA_ITS_STORAGE_PREFIX, |
| 86 |
(unsigned) ( uid >> 32 ), |
| 87 |
(unsigned) ( uid & 0xffffffff ), |
| 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, |
| 137 |
void *p_data, |
| 138 |
size_t *p_data_length ) |
| 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; |
| 148 |
status = PSA_ERROR_INVALID_ARGUMENT; |
| 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 |
|
| 158 |
status = PSA_ERROR_STORAGE_FAILURE; |
| 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; |
| 173 |
if( p_data_length != NULL ) |
| 174 |
*p_data_length = n; |
| 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 |
{ |
| 187 |
psa_status_t status = PSA_ERROR_STORAGE_FAILURE; |
| 188 |
char filename[PSA_ITS_STORAGE_FILENAME_LENGTH]; |
| 189 |
FILE *stream = NULL; |
| 190 |
psa_its_file_header_t header; |
| 191 |
size_t n; |
| 192 |
|
| 193 |
memcpy( header.magic, PSA_ITS_MAGIC_STRING, PSA_ITS_MAGIC_LENGTH ); |
| 194 |
MBEDTLS_PUT_UINT32_LE( data_length, header.size, 0 ); |
| 195 |
MBEDTLS_PUT_UINT32_LE( create_flags, header.flags, 0 ); |
| 196 |
|
| 197 |
psa_its_fill_filename( uid, filename ); |
| 198 |
stream = fopen( PSA_ITS_STORAGE_TEMP, "wb" ); |
| 199 |
if( stream == NULL ) |
| 200 |
goto exit; |
| 201 |
|
| 202 |
status = PSA_ERROR_INSUFFICIENT_STORAGE; |
| 203 |
n = fwrite( &header, 1, sizeof( header ), stream ); |
| 204 |
if( n != sizeof( header ) ) |
| 205 |
goto exit; |
| 206 |
if( data_length != 0 ) |
| 207 |
{ |
| 208 |
n = fwrite( p_data, 1, data_length, stream ); |
| 209 |
if( n != data_length ) |
| 210 |
goto exit; |
| 211 |
} |
| 212 |
status = PSA_SUCCESS; |
| 213 |
|
| 214 |
exit: |
| 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 |
{ |
| 223 |
if( rename_replace_existing( PSA_ITS_STORAGE_TEMP, filename ) != 0 ) |
| 224 |
status = PSA_ERROR_STORAGE_FAILURE; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
(void) remove( PSA_ITS_STORAGE_TEMP ); |
| 232 |
return( status ); |
| 233 |
} |
| 234 |
|
| 235 |
psa_status_t psa_its_remove( psa_storage_uid_t uid ) |
| 236 |
{ |
| 237 |
char filename[PSA_ITS_STORAGE_FILENAME_LENGTH]; |
| 238 |
FILE *stream; |
| 239 |
psa_its_fill_filename( uid, filename ); |
| 240 |
stream = fopen( filename, "rb" ); |
| 241 |
if( stream == NULL ) |
| 242 |
return( PSA_ERROR_DOES_NOT_EXIST ); |
| 243 |
fclose( stream ); |
| 244 |
if( remove( filename ) != 0 ) |
| 245 |
return( PSA_ERROR_STORAGE_FAILURE ); |
| 246 |
return( PSA_SUCCESS ); |
| 247 |
} |
| 248 |
|
| 249 |
#endif |