psa_its_file.c 8.0 KB

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