psa_crypto_storage.c 18 KB

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