psa_crypto_storage.c 16 KB

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