psa_its_file.c 7.1 KB

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