Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 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.h" |
| 24 | #else |
| 25 | #include MBEDTLS_CONFIG_FILE |
| 26 | #endif |
| 27 | |
| 28 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 29 | |
itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 30 | #include "psa_crypto_service_integration.h" |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 31 | #include "psa/crypto.h" |
| 32 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 33 | #include "psa_crypto_core.h" |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 34 | #include "psa_crypto_slot_management.h" |
| 35 | #include "psa_crypto_storage.h" |
| 36 | |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | #if defined(MBEDTLS_PLATFORM_C) |
| 40 | #include "mbedtls/platform.h" |
| 41 | #else |
| 42 | #define mbedtls_calloc calloc |
| 43 | #define mbedtls_free free |
| 44 | #endif |
| 45 | |
| 46 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 47 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 48 | typedef struct |
| 49 | { |
| 50 | psa_key_slot_t key_slots[PSA_KEY_SLOT_COUNT]; |
| 51 | unsigned key_slots_initialized : 1; |
| 52 | } psa_global_data_t; |
| 53 | |
Gilles Peskine | 2e14bd3 | 2018-12-12 14:05:08 +0100 | [diff] [blame] | 54 | static psa_global_data_t global_data; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 55 | |
| 56 | /* Access a key slot at the given handle. The handle of a key slot is |
| 57 | * the index of the slot in the global slot array, plus one so that handles |
| 58 | * start at 1 and not 0. */ |
| 59 | psa_status_t psa_get_key_slot( psa_key_handle_t handle, |
| 60 | psa_key_slot_t **p_slot ) |
| 61 | { |
| 62 | psa_key_slot_t *slot = NULL; |
| 63 | |
| 64 | if( ! global_data.key_slots_initialized ) |
| 65 | return( PSA_ERROR_BAD_STATE ); |
| 66 | |
| 67 | /* 0 is not a valid handle under any circumstance. This |
| 68 | * implementation provides slots number 1 to N where N is the |
| 69 | * number of available slots. */ |
| 70 | if( handle == 0 || handle > ARRAY_LENGTH( global_data.key_slots ) ) |
| 71 | return( PSA_ERROR_INVALID_HANDLE ); |
| 72 | slot = &global_data.key_slots[handle - 1]; |
| 73 | |
| 74 | /* If the slot hasn't been allocated, the handle is invalid. */ |
| 75 | if( ! slot->allocated ) |
| 76 | return( PSA_ERROR_INVALID_HANDLE ); |
| 77 | |
| 78 | *p_slot = slot; |
| 79 | return( PSA_SUCCESS ); |
| 80 | } |
| 81 | |
| 82 | psa_status_t psa_initialize_key_slots( void ) |
| 83 | { |
| 84 | /* Nothing to do: program startup and psa_wipe_all_key_slots() both |
| 85 | * guarantee that the key slots are initialized to all-zero, which |
| 86 | * means that all the key slots are in a valid, empty state. */ |
| 87 | global_data.key_slots_initialized = 1; |
| 88 | return( PSA_SUCCESS ); |
| 89 | } |
| 90 | |
| 91 | void psa_wipe_all_key_slots( void ) |
| 92 | { |
| 93 | psa_key_handle_t key; |
| 94 | for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ ) |
| 95 | { |
| 96 | psa_key_slot_t *slot = &global_data.key_slots[key - 1]; |
| 97 | (void) psa_wipe_key_slot( slot ); |
| 98 | } |
| 99 | global_data.key_slots_initialized = 0; |
| 100 | } |
| 101 | |
| 102 | /** Find a free key slot and mark it as in use. |
| 103 | * |
Gilles Peskine | 23fd2bd | 2018-12-11 15:51:32 +0100 | [diff] [blame] | 104 | * \param[out] handle On success, a slot number that is not in use. This |
| 105 | * value can be used as a handle to the slot. |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 106 | * |
| 107 | * \retval #PSA_SUCCESS |
| 108 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 109 | */ |
| 110 | static psa_status_t psa_internal_allocate_key_slot( psa_key_handle_t *handle ) |
| 111 | { |
| 112 | for( *handle = PSA_KEY_SLOT_COUNT; *handle != 0; --( *handle ) ) |
| 113 | { |
| 114 | psa_key_slot_t *slot = &global_data.key_slots[*handle - 1]; |
| 115 | if( ! slot->allocated ) |
| 116 | { |
| 117 | slot->allocated = 1; |
| 118 | return( PSA_SUCCESS ); |
| 119 | } |
| 120 | } |
| 121 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 122 | } |
| 123 | |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 124 | /** Wipe a key slot and mark it as available. |
| 125 | * |
| 126 | * This does not affect persistent storage. |
| 127 | * |
Gilles Peskine | 23fd2bd | 2018-12-11 15:51:32 +0100 | [diff] [blame] | 128 | * \param handle The handle to the key slot to release. |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 129 | * |
| 130 | * \retval #PSA_SUCCESS |
| 131 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 132 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 133 | */ |
| 134 | static psa_status_t psa_internal_release_key_slot( psa_key_handle_t handle ) |
| 135 | { |
| 136 | psa_key_slot_t *slot; |
| 137 | psa_status_t status; |
| 138 | |
| 139 | status = psa_get_key_slot( handle, &slot ); |
| 140 | if( status != PSA_SUCCESS ) |
| 141 | return( status ); |
| 142 | |
| 143 | return( psa_wipe_key_slot( slot ) ); |
| 144 | } |
| 145 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 146 | psa_status_t psa_allocate_key( psa_key_handle_t *handle ) |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 147 | { |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 148 | *handle = 0; |
| 149 | return( psa_internal_allocate_key_slot( handle ) ); |
| 150 | } |
| 151 | |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 152 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
| 153 | static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *p_slot ) |
| 154 | { |
| 155 | psa_status_t status = PSA_SUCCESS; |
| 156 | uint8_t *key_data = NULL; |
| 157 | size_t key_data_length = 0; |
| 158 | |
| 159 | status = psa_load_persistent_key( p_slot->persistent_storage_id, |
| 160 | &( p_slot )->type, |
| 161 | &( p_slot )->policy, &key_data, |
| 162 | &key_data_length ); |
| 163 | if( status != PSA_SUCCESS ) |
| 164 | goto exit; |
| 165 | status = psa_import_key_into_slot( p_slot, |
| 166 | key_data, key_data_length ); |
| 167 | exit: |
| 168 | psa_free_persistent_key_data( key_data, key_data_length ); |
| 169 | return( status ); |
| 170 | } |
Gilles Peskine | c8569bc | 2019-02-19 13:04:02 +0100 | [diff] [blame] | 171 | |
| 172 | /** Check whether a key identifier is acceptable. |
| 173 | * |
| 174 | * For backward compatibility, key identifiers that were valid in a |
| 175 | * past released version must remain valid, unless a migration path |
| 176 | * is provided. |
| 177 | * |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 178 | * \param file_id The key identifier to check. |
Gilles Peskine | f966659 | 2019-05-06 18:56:30 +0200 | [diff] [blame] | 179 | * \param vendor_ok Nonzero to allow key ids in the vendor range. |
| 180 | * 0 to allow only key ids in the application range. |
Gilles Peskine | c8569bc | 2019-02-19 13:04:02 +0100 | [diff] [blame] | 181 | * |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 182 | * \return 1 if \p file_id is acceptable, otherwise 0. |
Gilles Peskine | c8569bc | 2019-02-19 13:04:02 +0100 | [diff] [blame] | 183 | */ |
Gilles Peskine | f966659 | 2019-05-06 18:56:30 +0200 | [diff] [blame] | 184 | static int psa_is_key_id_valid( psa_key_file_id_t file_id, |
| 185 | int vendor_ok ) |
Gilles Peskine | c8569bc | 2019-02-19 13:04:02 +0100 | [diff] [blame] | 186 | { |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 187 | psa_app_key_id_t key_id = PSA_KEY_FILE_GET_KEY_ID( file_id ); |
Gilles Peskine | f9fbc38 | 2019-05-15 18:42:09 +0200 | [diff] [blame^] | 188 | if( PSA_KEY_ID_USER_MIN <= key_id && key_id <= PSA_KEY_ID_USER_MAX ) |
| 189 | return( 1 ); |
| 190 | else if( vendor_ok && |
| 191 | PSA_KEY_ID_VENDOR_MIN <= key_id && |
| 192 | key_id <= PSA_KEY_ID_VENDOR_MAX ) |
| 193 | return( 1 ); |
| 194 | else |
Gilles Peskine | c8569bc | 2019-02-19 13:04:02 +0100 | [diff] [blame] | 195 | return( 0 ); |
Gilles Peskine | c8569bc | 2019-02-19 13:04:02 +0100 | [diff] [blame] | 196 | } |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 197 | |
| 198 | /** Declare a slot as persistent and load it from storage. |
| 199 | * |
| 200 | * This function may only be called immediately after a successful call |
| 201 | * to psa_internal_allocate_key_slot(). |
| 202 | * |
| 203 | * \param handle A handle to a key slot freshly allocated with |
| 204 | * psa_internal_allocate_key_slot(). |
| 205 | * |
| 206 | * \retval #PSA_SUCCESS |
| 207 | * The slot content was loaded successfully. |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 208 | * \retval #PSA_ERROR_DOES_NOT_EXIST |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 209 | * There is no content for this slot in persistent storage. |
| 210 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 211 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 212 | * \p id is not acceptable. |
| 213 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 214 | * \retval #PSA_ERROR_STORAGE_FAILURE |
| 215 | */ |
| 216 | static psa_status_t psa_internal_make_key_persistent( psa_key_handle_t handle, |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 217 | psa_key_file_id_t id ) |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 218 | { |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 219 | psa_key_slot_t *slot; |
| 220 | psa_status_t status; |
| 221 | |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 222 | status = psa_get_key_slot( handle, &slot ); |
| 223 | if( status != PSA_SUCCESS ) |
| 224 | return( status ); |
| 225 | |
| 226 | slot->lifetime = PSA_KEY_LIFETIME_PERSISTENT; |
| 227 | slot->persistent_storage_id = id; |
| 228 | status = psa_load_persistent_key_into_slot( slot ); |
| 229 | |
| 230 | return( status ); |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 231 | } |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 232 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 233 | |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 234 | psa_status_t psa_validate_persistent_key_parameters( |
| 235 | psa_key_lifetime_t lifetime, |
Gilles Peskine | f966659 | 2019-05-06 18:56:30 +0200 | [diff] [blame] | 236 | psa_key_file_id_t id, |
| 237 | int creating ) |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 238 | { |
| 239 | if( lifetime != PSA_KEY_LIFETIME_PERSISTENT ) |
| 240 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 241 | |
| 242 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | f966659 | 2019-05-06 18:56:30 +0200 | [diff] [blame] | 243 | if( ! psa_is_key_id_valid( id, ! creating ) ) |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 244 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 245 | return( PSA_SUCCESS ); |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 246 | |
| 247 | #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
| 248 | (void) id; |
| 249 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 250 | #endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 251 | } |
| 252 | |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 253 | static psa_status_t persistent_key_setup( psa_key_lifetime_t lifetime, |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 254 | psa_key_file_id_t id, |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 255 | psa_key_handle_t *handle, |
Gilles Peskine | f966659 | 2019-05-06 18:56:30 +0200 | [diff] [blame] | 256 | int creating ) |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 257 | { |
| 258 | psa_status_t status; |
Gilles Peskine | f966659 | 2019-05-06 18:56:30 +0200 | [diff] [blame] | 259 | psa_status_t wanted_load_status = |
| 260 | ( creating ? PSA_ERROR_DOES_NOT_EXIST : PSA_SUCCESS ); |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 261 | |
| 262 | *handle = 0; |
| 263 | |
Gilles Peskine | f966659 | 2019-05-06 18:56:30 +0200 | [diff] [blame] | 264 | status = psa_validate_persistent_key_parameters( lifetime, id, creating ); |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 265 | if( status != PSA_SUCCESS ) |
| 266 | return( status ); |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 267 | |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 268 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 269 | status = psa_internal_allocate_key_slot( handle ); |
| 270 | if( status != PSA_SUCCESS ) |
| 271 | return( status ); |
| 272 | |
| 273 | status = psa_internal_make_key_persistent( *handle, id ); |
| 274 | if( status != wanted_load_status ) |
| 275 | { |
| 276 | psa_internal_release_key_slot( *handle ); |
| 277 | *handle = 0; |
| 278 | } |
| 279 | return( status ); |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 280 | #else /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 281 | (void) wanted_load_status; |
| 282 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 283 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 284 | } |
| 285 | |
Gilles Peskine | 225010f | 2019-05-06 18:44:55 +0200 | [diff] [blame] | 286 | psa_status_t psa_open_key( psa_key_file_id_t id, psa_key_handle_t *handle ) |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 287 | { |
Gilles Peskine | 225010f | 2019-05-06 18:44:55 +0200 | [diff] [blame] | 288 | return( persistent_key_setup( PSA_KEY_LIFETIME_PERSISTENT, |
Gilles Peskine | f966659 | 2019-05-06 18:56:30 +0200 | [diff] [blame] | 289 | id, handle, 0 ) ); |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | psa_status_t psa_create_key( psa_key_lifetime_t lifetime, |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 293 | psa_key_file_id_t id, |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 294 | psa_key_handle_t *handle ) |
| 295 | { |
| 296 | psa_status_t status; |
| 297 | |
Gilles Peskine | f966659 | 2019-05-06 18:56:30 +0200 | [diff] [blame] | 298 | status = persistent_key_setup( lifetime, id, handle, 1 ); |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 299 | switch( status ) |
| 300 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 301 | case PSA_SUCCESS: return( PSA_ERROR_ALREADY_EXISTS ); |
| 302 | case PSA_ERROR_DOES_NOT_EXIST: return( PSA_SUCCESS ); |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 303 | default: return( status ); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | psa_status_t psa_close_key( psa_key_handle_t handle ) |
| 308 | { |
| 309 | return( psa_internal_release_key_slot( handle ) ); |
| 310 | } |
| 311 | |
| 312 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |