blob: 687d22a9cce2b269863f434d2b7e3e267d491869 [file] [log] [blame]
Darryl Greendb2b8db2018-06-15 13:06:04 +01001/*
2 * PSA persistent key storage
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if defined(MBEDTLS_CONFIG_FILE)
23#include MBEDTLS_CONFIG_FILE
24#else
25#include "mbedtls/config.h"
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
29
30#include <stdlib.h>
31#include <string.h>
32
itayzafrir7723ab12019-02-14 10:28:02 +020033#include "psa_crypto_service_integration.h"
Darryl Greendb2b8db2018-06-15 13:06:04 +010034#include "psa/crypto.h"
35#include "psa_crypto_storage.h"
Darryl Greendb2b8db2018-06-15 13:06:04 +010036#include "mbedtls/platform_util.h"
37
Gilles Peskine5e80d912019-02-24 17:10:18 +010038#if defined(MBEDTLS_PSA_ITS_FILE_C)
39#include "psa_crypto_its.h"
40#else /* Native ITS implementation */
41#include "psa/error.h"
42#include "psa/internal_trusted_storage.h"
43#endif
44
Darryl Greendb2b8db2018-06-15 13:06:04 +010045#if defined(MBEDTLS_PLATFORM_C)
46#include "mbedtls/platform.h"
47#else
Jaeden Amerodb29ab52019-02-12 16:40:27 +000048#include <stdlib.h>
Darryl Greendb2b8db2018-06-15 13:06:04 +010049#define mbedtls_calloc calloc
50#define mbedtls_free free
51#endif
52
Gilles Peskinec8336cb2019-07-22 19:26:12 +020053
54
55/****************************************************************/
56/* Key storage */
57/****************************************************************/
58
Gilles Peskine088b77f2019-02-24 17:00:27 +010059/* Determine a file name (ITS file identifier) for the given key file
60 * identifier. The file name must be distinct from any file that is used
61 * for a purpose other than storing a key. Currently, the only such file
62 * is the random seed file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID
63 * and whose value is 0xFFFFFF52. */
64static psa_storage_uid_t psa_its_identifier_of_slot( psa_key_file_id_t file_id )
65{
66#if defined(MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER) && \
67 defined(PSA_CRYPTO_SECURE)
68 /* Encode the owner in the upper 32 bits. This means that if
69 * owner values are nonzero (as they are on a PSA platform),
70 * no key file will ever have a value less than 0x100000000, so
71 * the whole range 0..0xffffffff is available for non-key files. */
72 uint32_t unsigned_owner = (uint32_t) file_id.owner;
73 return( (uint64_t) unsigned_owner << 32 | file_id.key_id );
74#else
75 /* Use the key id directly as a file name.
76 * psa_is_key_file_id_valid() in psa_crypto_slot_management.c
77 * is responsible for ensuring that key identifiers do not have a
78 * value that is reserved for non-key files. */
79 return( file_id );
80#endif
81}
82
Gilles Peskine5e80d912019-02-24 17:10:18 +010083/**
84 * \brief Load persistent data for the given key slot number.
85 *
86 * This function reads data from a storage backend and returns the data in a
87 * buffer.
88 *
89 * \param key Persistent identifier of the key to be loaded. This
90 * should be an occupied storage location.
91 * \param[out] data Buffer where the data is to be written.
92 * \param data_size Size of the \c data buffer in bytes.
93 *
94 * \retval PSA_SUCCESS
95 * \retval PSA_ERROR_STORAGE_FAILURE
96 * \retval PSA_ERROR_DOES_NOT_EXIST
97 */
98static psa_status_t psa_crypto_storage_load( const psa_key_file_id_t key,
99 uint8_t *data,
100 size_t data_size )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100101{
102 psa_status_t status;
103 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
104 struct psa_storage_info_t data_identifier_info;
Simon D Hughesbda5a212019-07-10 16:34:21 +0100105 size_t data_length = 0;
Gilles Peskine088b77f2019-02-24 17:00:27 +0100106
107 status = psa_its_get_info( data_identifier, &data_identifier_info );
108 if( status != PSA_SUCCESS )
109 return( status );
110
Simon D Hughesbda5a212019-07-10 16:34:21 +0100111 status = psa_its_get( data_identifier, 0, (uint32_t) data_size, data, &data_length );
112 if( data_size != data_length )
113 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100114
115 return( status );
116}
117
118int psa_is_key_present_in_storage( const psa_key_file_id_t key )
119{
120 psa_status_t ret;
121 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
122 struct psa_storage_info_t data_identifier_info;
123
124 ret = psa_its_get_info( data_identifier, &data_identifier_info );
125
126 if( ret == PSA_ERROR_DOES_NOT_EXIST )
127 return( 0 );
128 return( 1 );
129}
130
Gilles Peskine5e80d912019-02-24 17:10:18 +0100131/**
132 * \brief Store persistent data for the given key slot number.
133 *
134 * This function stores the given data buffer to a persistent storage.
135 *
136 * \param key Persistent identifier of the key to be stored. This
137 * should be an unoccupied storage location.
138 * \param[in] data Buffer containing the data to be stored.
139 * \param data_length The number of bytes
140 * that make up the data.
141 *
142 * \retval PSA_SUCCESS
143 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
144 * \retval PSA_ERROR_STORAGE_FAILURE
145 * \retval PSA_ERROR_ALREADY_EXISTS
146 */
147static psa_status_t psa_crypto_storage_store( const psa_key_file_id_t key,
148 const uint8_t *data,
149 size_t data_length )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100150{
151 psa_status_t status;
152 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
153 struct psa_storage_info_t data_identifier_info;
154
155 if( psa_is_key_present_in_storage( key ) == 1 )
156 return( PSA_ERROR_ALREADY_EXISTS );
157
Gilles Peskinefad3a3e2019-02-25 13:36:21 +0100158 status = psa_its_set( data_identifier, (uint32_t) data_length, data, 0 );
Gilles Peskine088b77f2019-02-24 17:00:27 +0100159 if( status != PSA_SUCCESS )
160 {
161 return( PSA_ERROR_STORAGE_FAILURE );
162 }
163
164 status = psa_its_get_info( data_identifier, &data_identifier_info );
165 if( status != PSA_SUCCESS )
166 {
167 goto exit;
168 }
169
170 if( data_identifier_info.size != data_length )
171 {
172 status = PSA_ERROR_STORAGE_FAILURE;
173 goto exit;
174 }
175
176exit:
177 if( status != PSA_SUCCESS )
178 psa_its_remove( data_identifier );
179 return( status );
180}
181
182psa_status_t psa_destroy_persistent_key( const psa_key_file_id_t key )
183{
184 psa_status_t ret;
185 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
186 struct psa_storage_info_t data_identifier_info;
187
188 ret = psa_its_get_info( data_identifier, &data_identifier_info );
189 if( ret == PSA_ERROR_DOES_NOT_EXIST )
190 return( PSA_SUCCESS );
191
192 if( psa_its_remove( data_identifier ) != PSA_SUCCESS )
193 return( PSA_ERROR_STORAGE_FAILURE );
194
195 ret = psa_its_get_info( data_identifier, &data_identifier_info );
196 if( ret != PSA_ERROR_DOES_NOT_EXIST )
197 return( PSA_ERROR_STORAGE_FAILURE );
198
199 return( PSA_SUCCESS );
200}
201
Gilles Peskine5e80d912019-02-24 17:10:18 +0100202/**
203 * \brief Get data length for given key slot number.
204 *
205 * \param key Persistent identifier whose stored data length
206 * is to be obtained.
207 * \param[out] data_length The number of bytes that make up the data.
208 *
209 * \retval PSA_SUCCESS
210 * \retval PSA_ERROR_STORAGE_FAILURE
211 */
212static psa_status_t psa_crypto_storage_get_data_length(
213 const psa_key_file_id_t key,
214 size_t *data_length )
Gilles Peskine088b77f2019-02-24 17:00:27 +0100215{
216 psa_status_t status;
217 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
218 struct psa_storage_info_t data_identifier_info;
219
220 status = psa_its_get_info( data_identifier, &data_identifier_info );
221 if( status != PSA_SUCCESS )
222 return( status );
223
224 *data_length = (size_t) data_identifier_info.size;
225
226 return( PSA_SUCCESS );
227}
228
Darryl Greendb2b8db2018-06-15 13:06:04 +0100229/*
230 * 32-bit integer manipulation macros (little endian)
231 */
232#ifndef GET_UINT32_LE
Gilles Peskine274a2632019-07-23 11:27:38 +0200233#define GET_UINT32_LE( n, b, i ) \
Darryl Greendb2b8db2018-06-15 13:06:04 +0100234{ \
235 (n) = ( (uint32_t) (b)[(i) ] ) \
236 | ( (uint32_t) (b)[(i) + 1] << 8 ) \
237 | ( (uint32_t) (b)[(i) + 2] << 16 ) \
238 | ( (uint32_t) (b)[(i) + 3] << 24 ); \
239}
240#endif
241
242#ifndef PUT_UINT32_LE
Gilles Peskine274a2632019-07-23 11:27:38 +0200243#define PUT_UINT32_LE( n, b, i ) \
Darryl Greendb2b8db2018-06-15 13:06:04 +0100244{ \
245 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
246 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
247 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
248 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
249}
250#endif
251
Moran Peker96ebf9e2018-06-28 18:02:17 +0300252/**
253 * Persistent key storage magic header.
254 */
255#define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY"
256#define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ( sizeof( PSA_KEY_STORAGE_MAGIC_HEADER ) )
257
Darryl Greendb2b8db2018-06-15 13:06:04 +0100258typedef struct {
Moran Peker96ebf9e2018-06-28 18:02:17 +0300259 uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100260 uint8_t version[4];
Gilles Peskine0e8d4952019-07-23 14:46:52 +0200261 uint8_t lifetime[sizeof( psa_key_lifetime_t )];
Darryl Greendb2b8db2018-06-15 13:06:04 +0100262 uint8_t type[sizeof( psa_key_type_t )];
263 uint8_t policy[sizeof( psa_key_policy_t )];
264 uint8_t data_len[4];
265 uint8_t key_data[];
266} psa_persistent_key_storage_format;
267
268void psa_format_key_data_for_storage( const uint8_t *data,
269 const size_t data_length,
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200270 const psa_key_attributes_t *attributes,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100271 uint8_t *storage_data )
272{
273 psa_persistent_key_storage_format *storage_format =
274 (psa_persistent_key_storage_format *) storage_data;
275
Moran Peker96ebf9e2018-06-28 18:02:17 +0300276 memcpy( storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER, PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH );
Gilles Peskine274a2632019-07-23 11:27:38 +0200277 PUT_UINT32_LE( 0, storage_format->version, 0 );
Gilles Peskine0e8d4952019-07-23 14:46:52 +0200278 PUT_UINT32_LE( psa_get_key_lifetime( attributes ), storage_format->lifetime, 0 );
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200279 PUT_UINT32_LE( psa_get_key_type( attributes ), storage_format->type, 0 );
280 PUT_UINT32_LE( psa_get_key_usage_flags( attributes ), storage_format->policy, 0 );
281 PUT_UINT32_LE( psa_get_key_algorithm( attributes ), storage_format->policy, sizeof( uint32_t ) );
282 PUT_UINT32_LE( psa_get_key_enrollment_algorithm( attributes ), storage_format->policy, 2 * sizeof( uint32_t ) );
Gilles Peskine274a2632019-07-23 11:27:38 +0200283 PUT_UINT32_LE( data_length, storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100284 memcpy( storage_format->key_data, data, data_length );
285}
286
Moran Peker96ebf9e2018-06-28 18:02:17 +0300287static psa_status_t check_magic_header( const uint8_t *data )
288{
289 if( memcmp( data, PSA_KEY_STORAGE_MAGIC_HEADER,
290 PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ) != 0 )
291 return( PSA_ERROR_STORAGE_FAILURE );
292 return( PSA_SUCCESS );
293}
294
Darryl Greendb2b8db2018-06-15 13:06:04 +0100295psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data,
296 size_t storage_data_length,
297 uint8_t **key_data,
298 size_t *key_data_length,
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200299 psa_key_attributes_t *attributes )
Darryl Greendb2b8db2018-06-15 13:06:04 +0100300{
Moran Peker96ebf9e2018-06-28 18:02:17 +0300301 psa_status_t status;
Darryl Greendb2b8db2018-06-15 13:06:04 +0100302 const psa_persistent_key_storage_format *storage_format =
303 (const psa_persistent_key_storage_format *)storage_data;
304 uint32_t version;
305
Moran Peker96ebf9e2018-06-28 18:02:17 +0300306 if( storage_data_length < sizeof(*storage_format) )
307 return( PSA_ERROR_STORAGE_FAILURE );
308
309 status = check_magic_header( storage_data );
310 if( status != PSA_SUCCESS )
311 return( status );
312
Gilles Peskine274a2632019-07-23 11:27:38 +0200313 GET_UINT32_LE( version, storage_format->version, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100314 if( version != 0 )
315 return( PSA_ERROR_STORAGE_FAILURE );
316
Gilles Peskine274a2632019-07-23 11:27:38 +0200317 GET_UINT32_LE( *key_data_length, storage_format->data_len, 0 );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100318 if( *key_data_length > ( storage_data_length - sizeof(*storage_format) ) ||
319 *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
320 return( PSA_ERROR_STORAGE_FAILURE );
321
Gilles Peskine3495b582019-04-25 13:47:06 +0200322 if( *key_data_length == 0 )
323 {
324 *key_data = NULL;
325 }
326 else
327 {
328 *key_data = mbedtls_calloc( 1, *key_data_length );
329 if( *key_data == NULL )
330 return( PSA_ERROR_INSUFFICIENT_MEMORY );
331 memcpy( *key_data, storage_format->key_data, *key_data_length );
332 }
Darryl Greendb2b8db2018-06-15 13:06:04 +0100333
Gilles Peskine0e8d4952019-07-23 14:46:52 +0200334 GET_UINT32_LE( attributes->lifetime, storage_format->lifetime, 0 );
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200335 GET_UINT32_LE( attributes->type, storage_format->type, 0 );
336 GET_UINT32_LE( attributes->policy.usage, storage_format->policy, 0 );
337 GET_UINT32_LE( attributes->policy.alg, storage_format->policy, sizeof( uint32_t ) );
338 GET_UINT32_LE( attributes->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100339
Darryl Greendb2b8db2018-06-15 13:06:04 +0100340 return( PSA_SUCCESS );
341}
342
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200343psa_status_t psa_save_persistent_key( const psa_key_attributes_t *attributes,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100344 const uint8_t *data,
345 const size_t data_length )
346{
347 size_t storage_data_length;
348 uint8_t *storage_data;
349 psa_status_t status;
350
351 if( data_length > PSA_CRYPTO_MAX_STORAGE_SIZE )
352 return PSA_ERROR_INSUFFICIENT_STORAGE;
353 storage_data_length = data_length + sizeof( psa_persistent_key_storage_format );
354
355 storage_data = mbedtls_calloc( 1, storage_data_length );
356 if( storage_data == NULL )
357 return( PSA_ERROR_INSUFFICIENT_MEMORY );
358
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200359 psa_format_key_data_for_storage( data, data_length, attributes,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100360 storage_data );
361
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200362 status = psa_crypto_storage_store( psa_get_key_id( attributes ),
Darryl Greendb2b8db2018-06-15 13:06:04 +0100363 storage_data, storage_data_length );
364
365 mbedtls_free( storage_data );
366
367 return( status );
368}
369
370void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length )
371{
372 if( key_data != NULL )
373 {
374 mbedtls_platform_zeroize( key_data, key_data_length );
375 }
376 mbedtls_free( key_data );
377}
378
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200379psa_status_t psa_load_persistent_key( psa_key_attributes_t *attributes,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100380 uint8_t **data,
381 size_t *data_length )
382{
383 psa_status_t status = PSA_SUCCESS;
384 uint8_t *loaded_data;
385 size_t storage_data_length = 0;
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200386 psa_key_id_t key = psa_get_key_id( attributes );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100387
388 status = psa_crypto_storage_get_data_length( key, &storage_data_length );
389 if( status != PSA_SUCCESS )
390 return( status );
391
392 loaded_data = mbedtls_calloc( 1, storage_data_length );
393
394 if( loaded_data == NULL )
395 return( PSA_ERROR_INSUFFICIENT_MEMORY );
396
397 status = psa_crypto_storage_load( key, loaded_data, storage_data_length );
398 if( status != PSA_SUCCESS )
399 goto exit;
400
401 status = psa_parse_key_data_from_storage( loaded_data, storage_data_length,
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200402 data, data_length, attributes );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100403
404exit:
405 mbedtls_free( loaded_data );
406 return( status );
407}
408
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200409
410
411/****************************************************************/
412/* Transactions */
413/****************************************************************/
414
415#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
416
417psa_crypto_transaction_t psa_crypto_transaction;
418
419psa_status_t psa_crypto_save_transaction( void )
420{
421 struct psa_storage_info_t p_info;
422 psa_status_t status;
423 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
424 if( status == PSA_SUCCESS )
425 {
426 /* This shouldn't happen: we're trying to start a transaction while
427 * there is still a transaction that hasn't been replayed. */
428 return( PSA_ERROR_CORRUPTION_DETECTED );
429 }
430 else if( status != PSA_ERROR_DOES_NOT_EXIST )
431 return( status );
432 return( psa_its_set( PSA_CRYPTO_ITS_TRANSACTION_UID,
433 sizeof( psa_crypto_transaction ),
434 &psa_crypto_transaction,
435 0 ) );
436}
437
438psa_status_t psa_crypto_load_transaction( void )
439{
Gilles Peskine8b663892019-07-31 17:57:57 +0200440 psa_status_t status;
441 size_t length;
442 status = psa_its_get( PSA_CRYPTO_ITS_TRANSACTION_UID, 0,
443 sizeof( psa_crypto_transaction ),
444 &psa_crypto_transaction, &length );
445 if( status != PSA_SUCCESS )
446 return( status );
447 if( length != sizeof( psa_crypto_transaction ) )
448 return( PSA_ERROR_STORAGE_FAILURE );
449 return( PSA_SUCCESS );
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200450}
451
452psa_status_t psa_crypto_stop_transaction( void )
453{
454 psa_status_t status = psa_its_remove( PSA_CRYPTO_ITS_TRANSACTION_UID );
455 /* Whether or not updating the storage succeeded, the transaction is
456 * finished now. It's too late to go back, so zero out the in-memory
457 * data. */
458 memset( &psa_crypto_transaction, 0, sizeof( psa_crypto_transaction ) );
459 return( status );
460}
461
462#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
463
464
465
466/****************************************************************/
467/* Random generator state */
468/****************************************************************/
469
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100470#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
471psa_status_t mbedtls_psa_storage_inject_entropy( const unsigned char *seed,
472 size_t seed_size )
473{
474 psa_status_t status;
475 struct psa_storage_info_t p_info;
476
477 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
478
479 if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */
480 {
481 status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
482 }
483 else if( PSA_SUCCESS == status )
484 {
485 /* You should not be here. Seed needs to be injected only once */
486 status = PSA_ERROR_NOT_PERMITTED;
487 }
488 return( status );
489}
490#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
491
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200492
493
494/****************************************************************/
495/* The end */
496/****************************************************************/
497
Darryl Greendb2b8db2018-06-15 13:06:04 +0100498#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */