psa_crypto_storage.c 17 KB

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