psa_its_file.c 7.7 KB

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