Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 33 | #include "psa_crypto_service_integration.h" |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 34 | #include "psa/crypto.h" |
| 35 | #include "psa_crypto_storage.h" |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 36 | #include "mbedtls/platform_util.h" |
| 37 | |
Gilles Peskine | 5e80d91 | 2019-02-24 17:10:18 +0100 | [diff] [blame^] | 38 | #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 Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 45 | #if defined(MBEDTLS_PLATFORM_C) |
| 46 | #include "mbedtls/platform.h" |
| 47 | #else |
Jaeden Amero | db29ab5 | 2019-02-12 16:40:27 +0000 | [diff] [blame] | 48 | #include <stdlib.h> |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 49 | #define mbedtls_calloc calloc |
| 50 | #define mbedtls_free free |
| 51 | #endif |
| 52 | |
Gilles Peskine | 088b77f | 2019-02-24 17:00:27 +0100 | [diff] [blame] | 53 | /* Determine a file name (ITS file identifier) for the given key file |
| 54 | * identifier. The file name must be distinct from any file that is used |
| 55 | * for a purpose other than storing a key. Currently, the only such file |
| 56 | * is the random seed file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID |
| 57 | * and whose value is 0xFFFFFF52. */ |
| 58 | static psa_storage_uid_t psa_its_identifier_of_slot( psa_key_file_id_t file_id ) |
| 59 | { |
| 60 | #if defined(MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER) && \ |
| 61 | defined(PSA_CRYPTO_SECURE) |
| 62 | /* Encode the owner in the upper 32 bits. This means that if |
| 63 | * owner values are nonzero (as they are on a PSA platform), |
| 64 | * no key file will ever have a value less than 0x100000000, so |
| 65 | * the whole range 0..0xffffffff is available for non-key files. */ |
| 66 | uint32_t unsigned_owner = (uint32_t) file_id.owner; |
| 67 | return( (uint64_t) unsigned_owner << 32 | file_id.key_id ); |
| 68 | #else |
| 69 | /* Use the key id directly as a file name. |
| 70 | * psa_is_key_file_id_valid() in psa_crypto_slot_management.c |
| 71 | * is responsible for ensuring that key identifiers do not have a |
| 72 | * value that is reserved for non-key files. */ |
| 73 | return( file_id ); |
| 74 | #endif |
| 75 | } |
| 76 | |
Gilles Peskine | 5e80d91 | 2019-02-24 17:10:18 +0100 | [diff] [blame^] | 77 | /** |
| 78 | * \brief Load persistent data for the given key slot number. |
| 79 | * |
| 80 | * This function reads data from a storage backend and returns the data in a |
| 81 | * buffer. |
| 82 | * |
| 83 | * \param key Persistent identifier of the key to be loaded. This |
| 84 | * should be an occupied storage location. |
| 85 | * \param[out] data Buffer where the data is to be written. |
| 86 | * \param data_size Size of the \c data buffer in bytes. |
| 87 | * |
| 88 | * \retval PSA_SUCCESS |
| 89 | * \retval PSA_ERROR_STORAGE_FAILURE |
| 90 | * \retval PSA_ERROR_DOES_NOT_EXIST |
| 91 | */ |
| 92 | static psa_status_t psa_crypto_storage_load( const psa_key_file_id_t key, |
| 93 | uint8_t *data, |
| 94 | size_t data_size ) |
Gilles Peskine | 088b77f | 2019-02-24 17:00:27 +0100 | [diff] [blame] | 95 | { |
| 96 | psa_status_t status; |
| 97 | psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key ); |
| 98 | struct psa_storage_info_t data_identifier_info; |
| 99 | |
| 100 | status = psa_its_get_info( data_identifier, &data_identifier_info ); |
| 101 | if( status != PSA_SUCCESS ) |
| 102 | return( status ); |
| 103 | |
| 104 | status = psa_its_get( data_identifier, 0, data_size, data ); |
| 105 | |
| 106 | return( status ); |
| 107 | } |
| 108 | |
| 109 | int psa_is_key_present_in_storage( const psa_key_file_id_t key ) |
| 110 | { |
| 111 | psa_status_t ret; |
| 112 | psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key ); |
| 113 | struct psa_storage_info_t data_identifier_info; |
| 114 | |
| 115 | ret = psa_its_get_info( data_identifier, &data_identifier_info ); |
| 116 | |
| 117 | if( ret == PSA_ERROR_DOES_NOT_EXIST ) |
| 118 | return( 0 ); |
| 119 | return( 1 ); |
| 120 | } |
| 121 | |
Gilles Peskine | 5e80d91 | 2019-02-24 17:10:18 +0100 | [diff] [blame^] | 122 | /** |
| 123 | * \brief Store persistent data for the given key slot number. |
| 124 | * |
| 125 | * This function stores the given data buffer to a persistent storage. |
| 126 | * |
| 127 | * \param key Persistent identifier of the key to be stored. This |
| 128 | * should be an unoccupied storage location. |
| 129 | * \param[in] data Buffer containing the data to be stored. |
| 130 | * \param data_length The number of bytes |
| 131 | * that make up the data. |
| 132 | * |
| 133 | * \retval PSA_SUCCESS |
| 134 | * \retval PSA_ERROR_INSUFFICIENT_STORAGE |
| 135 | * \retval PSA_ERROR_STORAGE_FAILURE |
| 136 | * \retval PSA_ERROR_ALREADY_EXISTS |
| 137 | */ |
| 138 | static psa_status_t psa_crypto_storage_store( const psa_key_file_id_t key, |
| 139 | const uint8_t *data, |
| 140 | size_t data_length ) |
Gilles Peskine | 088b77f | 2019-02-24 17:00:27 +0100 | [diff] [blame] | 141 | { |
| 142 | psa_status_t status; |
| 143 | psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key ); |
| 144 | struct psa_storage_info_t data_identifier_info; |
| 145 | |
| 146 | if( psa_is_key_present_in_storage( key ) == 1 ) |
| 147 | return( PSA_ERROR_ALREADY_EXISTS ); |
| 148 | |
| 149 | status = psa_its_set( data_identifier, data_length, data, 0 ); |
| 150 | if( status != PSA_SUCCESS ) |
| 151 | { |
| 152 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 153 | } |
| 154 | |
| 155 | status = psa_its_get_info( data_identifier, &data_identifier_info ); |
| 156 | if( status != PSA_SUCCESS ) |
| 157 | { |
| 158 | goto exit; |
| 159 | } |
| 160 | |
| 161 | if( data_identifier_info.size != data_length ) |
| 162 | { |
| 163 | status = PSA_ERROR_STORAGE_FAILURE; |
| 164 | goto exit; |
| 165 | } |
| 166 | |
| 167 | exit: |
| 168 | if( status != PSA_SUCCESS ) |
| 169 | psa_its_remove( data_identifier ); |
| 170 | return( status ); |
| 171 | } |
| 172 | |
| 173 | psa_status_t psa_destroy_persistent_key( const psa_key_file_id_t key ) |
| 174 | { |
| 175 | psa_status_t ret; |
| 176 | psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key ); |
| 177 | struct psa_storage_info_t data_identifier_info; |
| 178 | |
| 179 | ret = psa_its_get_info( data_identifier, &data_identifier_info ); |
| 180 | if( ret == PSA_ERROR_DOES_NOT_EXIST ) |
| 181 | return( PSA_SUCCESS ); |
| 182 | |
| 183 | if( psa_its_remove( data_identifier ) != PSA_SUCCESS ) |
| 184 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 185 | |
| 186 | ret = psa_its_get_info( data_identifier, &data_identifier_info ); |
| 187 | if( ret != PSA_ERROR_DOES_NOT_EXIST ) |
| 188 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 189 | |
| 190 | return( PSA_SUCCESS ); |
| 191 | } |
| 192 | |
Gilles Peskine | 5e80d91 | 2019-02-24 17:10:18 +0100 | [diff] [blame^] | 193 | /** |
| 194 | * \brief Get data length for given key slot number. |
| 195 | * |
| 196 | * \param key Persistent identifier whose stored data length |
| 197 | * is to be obtained. |
| 198 | * \param[out] data_length The number of bytes that make up the data. |
| 199 | * |
| 200 | * \retval PSA_SUCCESS |
| 201 | * \retval PSA_ERROR_STORAGE_FAILURE |
| 202 | */ |
| 203 | static psa_status_t psa_crypto_storage_get_data_length( |
| 204 | const psa_key_file_id_t key, |
| 205 | size_t *data_length ) |
Gilles Peskine | 088b77f | 2019-02-24 17:00:27 +0100 | [diff] [blame] | 206 | { |
| 207 | psa_status_t status; |
| 208 | psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key ); |
| 209 | struct psa_storage_info_t data_identifier_info; |
| 210 | |
| 211 | status = psa_its_get_info( data_identifier, &data_identifier_info ); |
| 212 | if( status != PSA_SUCCESS ) |
| 213 | return( status ); |
| 214 | |
| 215 | *data_length = (size_t) data_identifier_info.size; |
| 216 | |
| 217 | return( PSA_SUCCESS ); |
| 218 | } |
| 219 | |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 220 | /* |
| 221 | * 32-bit integer manipulation macros (little endian) |
| 222 | */ |
| 223 | #ifndef GET_UINT32_LE |
| 224 | #define GET_UINT32_LE(n,b,i) \ |
| 225 | { \ |
| 226 | (n) = ( (uint32_t) (b)[(i) ] ) \ |
| 227 | | ( (uint32_t) (b)[(i) + 1] << 8 ) \ |
| 228 | | ( (uint32_t) (b)[(i) + 2] << 16 ) \ |
| 229 | | ( (uint32_t) (b)[(i) + 3] << 24 ); \ |
| 230 | } |
| 231 | #endif |
| 232 | |
| 233 | #ifndef PUT_UINT32_LE |
| 234 | #define PUT_UINT32_LE(n,b,i) \ |
| 235 | { \ |
| 236 | (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ |
| 237 | (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ |
| 238 | (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ |
| 239 | (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ |
| 240 | } |
| 241 | #endif |
| 242 | |
Moran Peker | 96ebf9e | 2018-06-28 18:02:17 +0300 | [diff] [blame] | 243 | /** |
| 244 | * Persistent key storage magic header. |
| 245 | */ |
| 246 | #define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY" |
| 247 | #define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ( sizeof( PSA_KEY_STORAGE_MAGIC_HEADER ) ) |
| 248 | |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 249 | typedef struct { |
Moran Peker | 96ebf9e | 2018-06-28 18:02:17 +0300 | [diff] [blame] | 250 | uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH]; |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 251 | uint8_t version[4]; |
| 252 | uint8_t type[sizeof( psa_key_type_t )]; |
| 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 | |
| 258 | void psa_format_key_data_for_storage( const uint8_t *data, |
| 259 | const size_t data_length, |
| 260 | const psa_key_type_t type, |
| 261 | const psa_key_policy_t *policy, |
| 262 | uint8_t *storage_data ) |
| 263 | { |
| 264 | psa_persistent_key_storage_format *storage_format = |
| 265 | (psa_persistent_key_storage_format *) storage_data; |
| 266 | |
Moran Peker | 96ebf9e | 2018-06-28 18:02:17 +0300 | [diff] [blame] | 267 | memcpy( storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER, PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ); |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 268 | PUT_UINT32_LE(0, storage_format->version, 0); |
| 269 | PUT_UINT32_LE(type, storage_format->type, 0); |
| 270 | PUT_UINT32_LE(policy->usage, storage_format->policy, 0); |
| 271 | PUT_UINT32_LE(policy->alg, storage_format->policy, 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 | |
Moran Peker | 96ebf9e | 2018-06-28 18:02:17 +0300 | [diff] [blame] | 276 | static psa_status_t check_magic_header( const uint8_t *data ) |
| 277 | { |
| 278 | if( memcmp( data, PSA_KEY_STORAGE_MAGIC_HEADER, |
| 279 | PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ) != 0 ) |
| 280 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 281 | return( PSA_SUCCESS ); |
| 282 | } |
| 283 | |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 284 | psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data, |
| 285 | size_t storage_data_length, |
| 286 | uint8_t **key_data, |
| 287 | size_t *key_data_length, |
| 288 | psa_key_type_t *type, |
| 289 | psa_key_policy_t *policy ) |
| 290 | { |
Moran Peker | 96ebf9e | 2018-06-28 18:02:17 +0300 | [diff] [blame] | 291 | psa_status_t status; |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 292 | const psa_persistent_key_storage_format *storage_format = |
| 293 | (const psa_persistent_key_storage_format *)storage_data; |
| 294 | uint32_t version; |
| 295 | |
Moran Peker | 96ebf9e | 2018-06-28 18:02:17 +0300 | [diff] [blame] | 296 | if( storage_data_length < sizeof(*storage_format) ) |
| 297 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 298 | |
| 299 | status = check_magic_header( storage_data ); |
| 300 | if( status != PSA_SUCCESS ) |
| 301 | return( status ); |
| 302 | |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 303 | GET_UINT32_LE(version, storage_format->version, 0); |
| 304 | if( version != 0 ) |
| 305 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 306 | |
| 307 | GET_UINT32_LE(*key_data_length, storage_format->data_len, 0); |
| 308 | if( *key_data_length > ( storage_data_length - sizeof(*storage_format) ) || |
| 309 | *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE ) |
| 310 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 311 | |
| 312 | *key_data = mbedtls_calloc( 1, *key_data_length ); |
| 313 | if( *key_data == NULL ) |
| 314 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 315 | |
| 316 | GET_UINT32_LE(*type, storage_format->type, 0); |
| 317 | GET_UINT32_LE(policy->usage, storage_format->policy, 0); |
| 318 | GET_UINT32_LE(policy->alg, storage_format->policy, sizeof( uint32_t )); |
| 319 | |
| 320 | memcpy( *key_data, storage_format->key_data, *key_data_length ); |
| 321 | |
| 322 | return( PSA_SUCCESS ); |
| 323 | } |
| 324 | |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 325 | psa_status_t psa_save_persistent_key( const psa_key_file_id_t key, |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 326 | const psa_key_type_t type, |
| 327 | const psa_key_policy_t *policy, |
| 328 | const uint8_t *data, |
| 329 | const size_t data_length ) |
| 330 | { |
| 331 | size_t storage_data_length; |
| 332 | uint8_t *storage_data; |
| 333 | psa_status_t status; |
| 334 | |
| 335 | if( data_length > PSA_CRYPTO_MAX_STORAGE_SIZE ) |
| 336 | return PSA_ERROR_INSUFFICIENT_STORAGE; |
| 337 | storage_data_length = data_length + sizeof( psa_persistent_key_storage_format ); |
| 338 | |
| 339 | storage_data = mbedtls_calloc( 1, storage_data_length ); |
| 340 | if( storage_data == NULL ) |
| 341 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 342 | |
| 343 | psa_format_key_data_for_storage( data, data_length, type, policy, |
| 344 | storage_data ); |
| 345 | |
| 346 | status = psa_crypto_storage_store( key, |
| 347 | storage_data, storage_data_length ); |
| 348 | |
| 349 | mbedtls_free( storage_data ); |
| 350 | |
| 351 | return( status ); |
| 352 | } |
| 353 | |
| 354 | void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length ) |
| 355 | { |
| 356 | if( key_data != NULL ) |
| 357 | { |
| 358 | mbedtls_platform_zeroize( key_data, key_data_length ); |
| 359 | } |
| 360 | mbedtls_free( key_data ); |
| 361 | } |
| 362 | |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 363 | psa_status_t psa_load_persistent_key( psa_key_file_id_t key, |
Darryl Green | db2b8db | 2018-06-15 13:06:04 +0100 | [diff] [blame] | 364 | psa_key_type_t *type, |
| 365 | psa_key_policy_t *policy, |
| 366 | uint8_t **data, |
| 367 | size_t *data_length ) |
| 368 | { |
| 369 | psa_status_t status = PSA_SUCCESS; |
| 370 | uint8_t *loaded_data; |
| 371 | size_t storage_data_length = 0; |
| 372 | |
| 373 | status = psa_crypto_storage_get_data_length( key, &storage_data_length ); |
| 374 | if( status != PSA_SUCCESS ) |
| 375 | return( status ); |
| 376 | |
| 377 | loaded_data = mbedtls_calloc( 1, storage_data_length ); |
| 378 | |
| 379 | if( loaded_data == NULL ) |
| 380 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 381 | |
| 382 | status = psa_crypto_storage_load( key, loaded_data, storage_data_length ); |
| 383 | if( status != PSA_SUCCESS ) |
| 384 | goto exit; |
| 385 | |
| 386 | status = psa_parse_key_data_from_storage( loaded_data, storage_data_length, |
| 387 | data, data_length, type, policy ); |
| 388 | |
| 389 | exit: |
| 390 | mbedtls_free( loaded_data ); |
| 391 | return( status ); |
| 392 | } |
| 393 | |
| 394 | #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */ |