psa_crypto_storage.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * PSA persistent key storage
  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_CRYPTO_STORAGE_C)
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "psa/crypto.h"
  13. #include "psa_crypto_storage.h"
  14. #include "mbedtls/platform_util.h"
  15. #if defined(MBEDTLS_PSA_ITS_FILE_C)
  16. #include "psa_crypto_its.h"
  17. #else /* Native ITS implementation */
  18. #include "psa/error.h"
  19. #include "psa/internal_trusted_storage.h"
  20. #endif
  21. #include "mbedtls/platform.h"
  22. /****************************************************************/
  23. /* Key storage */
  24. /****************************************************************/
  25. /* Determine a file name (ITS file identifier) for the given key identifier.
  26. * The file name must be distinct from any file that is used for a purpose
  27. * other than storing a key. Currently, the only such file is the random seed
  28. * file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID and whose value is
  29. * 0xFFFFFF52. */
  30. static psa_storage_uid_t psa_its_identifier_of_slot(mbedtls_svc_key_id_t key)
  31. {
  32. #if defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
  33. /* Encode the owner in the upper 32 bits. This means that if
  34. * owner values are nonzero (as they are on a PSA platform),
  35. * no key file will ever have a value less than 0x100000000, so
  36. * the whole range 0..0xffffffff is available for non-key files. */
  37. uint32_t unsigned_owner_id = MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(key);
  38. return ((uint64_t) unsigned_owner_id << 32) |
  39. MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
  40. #else
  41. /* Use the key id directly as a file name.
  42. * psa_is_key_id_valid() in psa_crypto_slot_management.c
  43. * is responsible for ensuring that key identifiers do not have a
  44. * value that is reserved for non-key files. */
  45. return key;
  46. #endif
  47. }
  48. /**
  49. * \brief Load persistent data for the given key slot number.
  50. *
  51. * This function reads data from a storage backend and returns the data in a
  52. * buffer.
  53. *
  54. * \param key Persistent identifier of the key to be loaded. This
  55. * should be an occupied storage location.
  56. * \param[out] data Buffer where the data is to be written.
  57. * \param data_size Size of the \c data buffer in bytes.
  58. *
  59. * \retval #PSA_SUCCESS \emptydescription
  60. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  61. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  62. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  63. * \retval #PSA_ERROR_DOES_NOT_EXIST \emptydescription
  64. */
  65. static psa_status_t psa_crypto_storage_load(
  66. const mbedtls_svc_key_id_t key, uint8_t *data, size_t data_size)
  67. {
  68. psa_status_t status;
  69. psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
  70. struct psa_storage_info_t data_identifier_info;
  71. size_t data_length = 0;
  72. status = psa_its_get_info(data_identifier, &data_identifier_info);
  73. if (status != PSA_SUCCESS) {
  74. return status;
  75. }
  76. status = psa_its_get(data_identifier, 0, (uint32_t) data_size, data, &data_length);
  77. if (data_size != data_length) {
  78. return PSA_ERROR_DATA_INVALID;
  79. }
  80. return status;
  81. }
  82. int psa_is_key_present_in_storage(const mbedtls_svc_key_id_t key)
  83. {
  84. psa_status_t ret;
  85. psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
  86. struct psa_storage_info_t data_identifier_info;
  87. ret = psa_its_get_info(data_identifier, &data_identifier_info);
  88. if (ret == PSA_ERROR_DOES_NOT_EXIST) {
  89. return 0;
  90. }
  91. return 1;
  92. }
  93. /**
  94. * \brief Store persistent data for the given key slot number.
  95. *
  96. * This function stores the given data buffer to a persistent storage.
  97. *
  98. * \param key Persistent identifier of the key to be stored. This
  99. * should be an unoccupied storage location.
  100. * \param[in] data Buffer containing the data to be stored.
  101. * \param data_length The number of bytes
  102. * that make up the data.
  103. *
  104. * \retval #PSA_SUCCESS \emptydescription
  105. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
  106. * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
  107. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  108. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  109. */
  110. static psa_status_t psa_crypto_storage_store(const mbedtls_svc_key_id_t key,
  111. const uint8_t *data,
  112. size_t data_length)
  113. {
  114. psa_status_t status;
  115. psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
  116. struct psa_storage_info_t data_identifier_info;
  117. if (psa_is_key_present_in_storage(key) == 1) {
  118. return PSA_ERROR_ALREADY_EXISTS;
  119. }
  120. status = psa_its_set(data_identifier, (uint32_t) data_length, data, 0);
  121. if (status != PSA_SUCCESS) {
  122. return PSA_ERROR_DATA_INVALID;
  123. }
  124. status = psa_its_get_info(data_identifier, &data_identifier_info);
  125. if (status != PSA_SUCCESS) {
  126. goto exit;
  127. }
  128. if (data_identifier_info.size != data_length) {
  129. status = PSA_ERROR_DATA_INVALID;
  130. goto exit;
  131. }
  132. exit:
  133. if (status != PSA_SUCCESS) {
  134. /* Remove the file in case we managed to create it but something
  135. * went wrong. It's ok if the file doesn't exist. If the file exists
  136. * but the removal fails, we're already reporting an error so there's
  137. * nothing else we can do. */
  138. (void) psa_its_remove(data_identifier);
  139. }
  140. return status;
  141. }
  142. psa_status_t psa_destroy_persistent_key(const mbedtls_svc_key_id_t key)
  143. {
  144. psa_status_t ret;
  145. psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
  146. struct psa_storage_info_t data_identifier_info;
  147. ret = psa_its_get_info(data_identifier, &data_identifier_info);
  148. if (ret == PSA_ERROR_DOES_NOT_EXIST) {
  149. return PSA_SUCCESS;
  150. }
  151. if (psa_its_remove(data_identifier) != PSA_SUCCESS) {
  152. return PSA_ERROR_DATA_INVALID;
  153. }
  154. ret = psa_its_get_info(data_identifier, &data_identifier_info);
  155. if (ret != PSA_ERROR_DOES_NOT_EXIST) {
  156. return PSA_ERROR_DATA_INVALID;
  157. }
  158. return PSA_SUCCESS;
  159. }
  160. /**
  161. * \brief Get data length for given key slot number.
  162. *
  163. * \param key Persistent identifier whose stored data length
  164. * is to be obtained.
  165. * \param[out] data_length The number of bytes that make up the data.
  166. *
  167. * \retval #PSA_SUCCESS \emptydescription
  168. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  169. * \retval #PSA_ERROR_DOES_NOT_EXIST \emptydescription
  170. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  171. */
  172. static psa_status_t psa_crypto_storage_get_data_length(
  173. const mbedtls_svc_key_id_t key,
  174. size_t *data_length)
  175. {
  176. psa_status_t status;
  177. psa_storage_uid_t data_identifier = psa_its_identifier_of_slot(key);
  178. struct psa_storage_info_t data_identifier_info;
  179. status = psa_its_get_info(data_identifier, &data_identifier_info);
  180. if (status != PSA_SUCCESS) {
  181. return status;
  182. }
  183. *data_length = (size_t) data_identifier_info.size;
  184. return PSA_SUCCESS;
  185. }
  186. /**
  187. * Persistent key storage magic header.
  188. */
  189. #define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY"
  190. #define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH (sizeof(PSA_KEY_STORAGE_MAGIC_HEADER))
  191. typedef struct {
  192. uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH];
  193. uint8_t version[4];
  194. uint8_t lifetime[sizeof(psa_key_lifetime_t)];
  195. uint8_t type[2];
  196. uint8_t bits[2];
  197. uint8_t policy[sizeof(psa_key_policy_t)];
  198. uint8_t data_len[4];
  199. uint8_t key_data[];
  200. } psa_persistent_key_storage_format;
  201. void psa_format_key_data_for_storage(const uint8_t *data,
  202. const size_t data_length,
  203. const psa_key_attributes_t *attr,
  204. uint8_t *storage_data)
  205. {
  206. psa_persistent_key_storage_format *storage_format =
  207. (psa_persistent_key_storage_format *) storage_data;
  208. memcpy(storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER,
  209. PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH);
  210. MBEDTLS_PUT_UINT32_LE(0, storage_format->version, 0);
  211. MBEDTLS_PUT_UINT32_LE(attr->lifetime, storage_format->lifetime, 0);
  212. MBEDTLS_PUT_UINT16_LE((uint16_t) attr->type, storage_format->type, 0);
  213. MBEDTLS_PUT_UINT16_LE((uint16_t) attr->bits, storage_format->bits, 0);
  214. MBEDTLS_PUT_UINT32_LE(attr->policy.usage, storage_format->policy, 0);
  215. MBEDTLS_PUT_UINT32_LE(attr->policy.alg, storage_format->policy, sizeof(uint32_t));
  216. MBEDTLS_PUT_UINT32_LE(attr->policy.alg2, storage_format->policy, 2 * sizeof(uint32_t));
  217. MBEDTLS_PUT_UINT32_LE(data_length, storage_format->data_len, 0);
  218. memcpy(storage_format->key_data, data, data_length);
  219. }
  220. static psa_status_t check_magic_header(const uint8_t *data)
  221. {
  222. if (memcmp(data, PSA_KEY_STORAGE_MAGIC_HEADER,
  223. PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH) != 0) {
  224. return PSA_ERROR_DATA_INVALID;
  225. }
  226. return PSA_SUCCESS;
  227. }
  228. psa_status_t psa_parse_key_data_from_storage(const uint8_t *storage_data,
  229. size_t storage_data_length,
  230. uint8_t **key_data,
  231. size_t *key_data_length,
  232. psa_key_attributes_t *attr)
  233. {
  234. psa_status_t status;
  235. const psa_persistent_key_storage_format *storage_format =
  236. (const psa_persistent_key_storage_format *) storage_data;
  237. uint32_t version;
  238. if (storage_data_length < sizeof(*storage_format)) {
  239. return PSA_ERROR_DATA_INVALID;
  240. }
  241. status = check_magic_header(storage_data);
  242. if (status != PSA_SUCCESS) {
  243. return status;
  244. }
  245. version = MBEDTLS_GET_UINT32_LE(storage_format->version, 0);
  246. if (version != 0) {
  247. return PSA_ERROR_DATA_INVALID;
  248. }
  249. *key_data_length = MBEDTLS_GET_UINT32_LE(storage_format->data_len, 0);
  250. if (*key_data_length > (storage_data_length - sizeof(*storage_format)) ||
  251. *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE) {
  252. return PSA_ERROR_DATA_INVALID;
  253. }
  254. if (*key_data_length == 0) {
  255. *key_data = NULL;
  256. } else {
  257. *key_data = mbedtls_calloc(1, *key_data_length);
  258. if (*key_data == NULL) {
  259. return PSA_ERROR_INSUFFICIENT_MEMORY;
  260. }
  261. memcpy(*key_data, storage_format->key_data, *key_data_length);
  262. }
  263. attr->lifetime = MBEDTLS_GET_UINT32_LE(storage_format->lifetime, 0);
  264. attr->type = MBEDTLS_GET_UINT16_LE(storage_format->type, 0);
  265. attr->bits = MBEDTLS_GET_UINT16_LE(storage_format->bits, 0);
  266. attr->policy.usage = MBEDTLS_GET_UINT32_LE(storage_format->policy, 0);
  267. attr->policy.alg = MBEDTLS_GET_UINT32_LE(storage_format->policy, sizeof(uint32_t));
  268. attr->policy.alg2 = MBEDTLS_GET_UINT32_LE(storage_format->policy, 2 * sizeof(uint32_t));
  269. return PSA_SUCCESS;
  270. }
  271. psa_status_t psa_save_persistent_key(const psa_key_attributes_t *attr,
  272. const uint8_t *data,
  273. const size_t data_length)
  274. {
  275. size_t storage_data_length;
  276. uint8_t *storage_data;
  277. psa_status_t status;
  278. /* All keys saved to persistent storage always have a key context */
  279. if (data == NULL || data_length == 0) {
  280. return PSA_ERROR_INVALID_ARGUMENT;
  281. }
  282. if (data_length > PSA_CRYPTO_MAX_STORAGE_SIZE) {
  283. return PSA_ERROR_INSUFFICIENT_STORAGE;
  284. }
  285. storage_data_length = data_length + sizeof(psa_persistent_key_storage_format);
  286. storage_data = mbedtls_calloc(1, storage_data_length);
  287. if (storage_data == NULL) {
  288. return PSA_ERROR_INSUFFICIENT_MEMORY;
  289. }
  290. psa_format_key_data_for_storage(data, data_length, attr, storage_data);
  291. status = psa_crypto_storage_store(attr->id,
  292. storage_data, storage_data_length);
  293. mbedtls_zeroize_and_free(storage_data, storage_data_length);
  294. return status;
  295. }
  296. void psa_free_persistent_key_data(uint8_t *key_data, size_t key_data_length)
  297. {
  298. mbedtls_zeroize_and_free(key_data, key_data_length);
  299. }
  300. psa_status_t psa_load_persistent_key(psa_key_attributes_t *attr,
  301. uint8_t **data,
  302. size_t *data_length)
  303. {
  304. psa_status_t status = PSA_SUCCESS;
  305. uint8_t *loaded_data;
  306. size_t storage_data_length = 0;
  307. mbedtls_svc_key_id_t key = attr->id;
  308. status = psa_crypto_storage_get_data_length(key, &storage_data_length);
  309. if (status != PSA_SUCCESS) {
  310. return status;
  311. }
  312. loaded_data = mbedtls_calloc(1, storage_data_length);
  313. if (loaded_data == NULL) {
  314. return PSA_ERROR_INSUFFICIENT_MEMORY;
  315. }
  316. status = psa_crypto_storage_load(key, loaded_data, storage_data_length);
  317. if (status != PSA_SUCCESS) {
  318. goto exit;
  319. }
  320. status = psa_parse_key_data_from_storage(loaded_data, storage_data_length,
  321. data, data_length, attr);
  322. /* All keys saved to persistent storage always have a key context */
  323. if (status == PSA_SUCCESS &&
  324. (*data == NULL || *data_length == 0)) {
  325. status = PSA_ERROR_STORAGE_FAILURE;
  326. }
  327. exit:
  328. mbedtls_zeroize_and_free(loaded_data, storage_data_length);
  329. return status;
  330. }
  331. /****************************************************************/
  332. /* Transactions */
  333. /****************************************************************/
  334. #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
  335. psa_crypto_transaction_t psa_crypto_transaction;
  336. psa_status_t psa_crypto_save_transaction(void)
  337. {
  338. struct psa_storage_info_t p_info;
  339. psa_status_t status;
  340. status = psa_its_get_info(PSA_CRYPTO_ITS_TRANSACTION_UID, &p_info);
  341. if (status == PSA_SUCCESS) {
  342. /* This shouldn't happen: we're trying to start a transaction while
  343. * there is still a transaction that hasn't been replayed. */
  344. return PSA_ERROR_CORRUPTION_DETECTED;
  345. } else if (status != PSA_ERROR_DOES_NOT_EXIST) {
  346. return status;
  347. }
  348. return psa_its_set(PSA_CRYPTO_ITS_TRANSACTION_UID,
  349. sizeof(psa_crypto_transaction),
  350. &psa_crypto_transaction,
  351. 0);
  352. }
  353. psa_status_t psa_crypto_load_transaction(void)
  354. {
  355. psa_status_t status;
  356. size_t length;
  357. status = psa_its_get(PSA_CRYPTO_ITS_TRANSACTION_UID, 0,
  358. sizeof(psa_crypto_transaction),
  359. &psa_crypto_transaction, &length);
  360. if (status != PSA_SUCCESS) {
  361. return status;
  362. }
  363. if (length != sizeof(psa_crypto_transaction)) {
  364. return PSA_ERROR_DATA_INVALID;
  365. }
  366. return PSA_SUCCESS;
  367. }
  368. psa_status_t psa_crypto_stop_transaction(void)
  369. {
  370. psa_status_t status = psa_its_remove(PSA_CRYPTO_ITS_TRANSACTION_UID);
  371. /* Whether or not updating the storage succeeded, the transaction is
  372. * finished now. It's too late to go back, so zero out the in-memory
  373. * data. */
  374. memset(&psa_crypto_transaction, 0, sizeof(psa_crypto_transaction));
  375. return status;
  376. }
  377. #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
  378. /****************************************************************/
  379. /* Random generator state */
  380. /****************************************************************/
  381. #if defined(MBEDTLS_PSA_INJECT_ENTROPY)
  382. psa_status_t mbedtls_psa_storage_inject_entropy(const unsigned char *seed,
  383. size_t seed_size)
  384. {
  385. psa_status_t status;
  386. struct psa_storage_info_t p_info;
  387. status = psa_its_get_info(PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info);
  388. if (PSA_ERROR_DOES_NOT_EXIST == status) { /* No seed exists */
  389. status = psa_its_set(PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0);
  390. } else if (PSA_SUCCESS == status) {
  391. /* You should not be here. Seed needs to be injected only once */
  392. status = PSA_ERROR_NOT_PERMITTED;
  393. }
  394. return status;
  395. }
  396. #endif /* MBEDTLS_PSA_INJECT_ENTROPY */
  397. /****************************************************************/
  398. /* The end */
  399. /****************************************************************/
  400. #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */