psa_its_file.c 7.9 KB

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