1 /*
2 * PSA crypto layer on top of Mbed TLS crypto
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
21 #include "common.h"
22
23 #if defined(MBEDTLS_PSA_CRYPTO_C)
24
25 #include "psa/crypto.h"
26
27 #include "psa_crypto_core.h"
28 #include "psa_crypto_driver_wrappers.h"
29 #include "psa_crypto_slot_management.h"
30 #include "psa_crypto_storage.h"
31 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
32 #include "psa_crypto_se.h"
33 #endif
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include "mbedtls/platform.h"
38
39 #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
40
41 typedef struct {
42 psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
43 unsigned key_slots_initialized : 1;
44 } psa_global_data_t;
45
46 static psa_global_data_t global_data;
47
psa_is_valid_key_id(mbedtls_svc_key_id_t key,int vendor_ok)48 int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok)
49 {
50 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
51
52 if ((PSA_KEY_ID_USER_MIN <= key_id) &&
53 (key_id <= PSA_KEY_ID_USER_MAX)) {
54 return 1;
55 }
56
57 if (vendor_ok &&
58 (PSA_KEY_ID_VENDOR_MIN <= key_id) &&
59 (key_id <= PSA_KEY_ID_VENDOR_MAX)) {
60 return 1;
61 }
62
63 return 0;
64 }
65
66 /** Get the description in memory of a key given its identifier and lock it.
67 *
68 * The descriptions of volatile keys and loaded persistent keys are
69 * stored in key slots. This function returns a pointer to the key slot
70 * containing the description of a key given its identifier.
71 *
72 * The function searches the key slots containing the description of the key
73 * with \p key identifier. The function does only read accesses to the key
74 * slots. The function does not load any persistent key thus does not access
75 * any storage.
76 *
77 * For volatile key identifiers, only one key slot is queried as a volatile
78 * key with identifier key_id can only be stored in slot of index
79 * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
80 *
81 * On success, the function locks the key slot. It is the responsibility of
82 * the caller to unlock the key slot when it does not access it anymore.
83 *
84 * \param key Key identifier to query.
85 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
86 * key slot containing the description of the key
87 * identified by \p key.
88 *
89 * \retval #PSA_SUCCESS
90 * The pointer to the key slot containing the description of the key
91 * identified by \p key was returned.
92 * \retval #PSA_ERROR_INVALID_HANDLE
93 * \p key is not a valid key identifier.
94 * \retval #PSA_ERROR_DOES_NOT_EXIST
95 * There is no key with key identifier \p key in the key slots.
96 */
psa_get_and_lock_key_slot_in_memory(mbedtls_svc_key_id_t key,psa_key_slot_t ** p_slot)97 static psa_status_t psa_get_and_lock_key_slot_in_memory(
98 mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot)
99 {
100 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
101 psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
102 size_t slot_idx;
103 psa_key_slot_t *slot = NULL;
104
105 if (psa_key_id_is_volatile(key_id)) {
106 slot = &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN];
107
108 /*
109 * Check if both the PSA key identifier key_id and the owner
110 * identifier of key match those of the key slot.
111 *
112 * Note that, if the key slot is not occupied, its PSA key identifier
113 * is equal to zero. This is an invalid value for a PSA key identifier
114 * and thus cannot be equal to the valid PSA key identifier key_id.
115 */
116 status = mbedtls_svc_key_id_equal(key, slot->attr.id) ?
117 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
118 } else {
119 if (!psa_is_valid_key_id(key, 1)) {
120 return PSA_ERROR_INVALID_HANDLE;
121 }
122
123 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
124 slot = &global_data.key_slots[slot_idx];
125 if (mbedtls_svc_key_id_equal(key, slot->attr.id)) {
126 break;
127 }
128 }
129 status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ?
130 PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
131 }
132
133 if (status == PSA_SUCCESS) {
134 status = psa_lock_key_slot(slot);
135 if (status == PSA_SUCCESS) {
136 *p_slot = slot;
137 }
138 }
139
140 return status;
141 }
142
psa_initialize_key_slots(void)143 psa_status_t psa_initialize_key_slots(void)
144 {
145 /* Nothing to do: program startup and psa_wipe_all_key_slots() both
146 * guarantee that the key slots are initialized to all-zero, which
147 * means that all the key slots are in a valid, empty state. */
148 global_data.key_slots_initialized = 1;
149 return PSA_SUCCESS;
150 }
151
psa_wipe_all_key_slots(void)152 void psa_wipe_all_key_slots(void)
153 {
154 size_t slot_idx;
155
156 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
157 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
158 slot->lock_count = 1;
159 (void) psa_wipe_key_slot(slot);
160 }
161 global_data.key_slots_initialized = 0;
162 }
163
psa_get_empty_key_slot(psa_key_id_t * volatile_key_id,psa_key_slot_t ** p_slot)164 psa_status_t psa_get_empty_key_slot(psa_key_id_t *volatile_key_id,
165 psa_key_slot_t **p_slot)
166 {
167 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
168 size_t slot_idx;
169 psa_key_slot_t *selected_slot, *unlocked_persistent_key_slot;
170
171 if (!global_data.key_slots_initialized) {
172 status = PSA_ERROR_BAD_STATE;
173 goto error;
174 }
175
176 selected_slot = unlocked_persistent_key_slot = NULL;
177 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
178 psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
179 if (!psa_is_key_slot_occupied(slot)) {
180 selected_slot = slot;
181 break;
182 }
183
184 if ((unlocked_persistent_key_slot == NULL) &&
185 (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
186 (!psa_is_key_slot_locked(slot))) {
187 unlocked_persistent_key_slot = slot;
188 }
189 }
190
191 /*
192 * If there is no unused key slot and there is at least one unlocked key
193 * slot containing the description of a persistent key, recycle the first
194 * such key slot we encountered. If we later need to operate on the
195 * persistent key we are evicting now, we will reload its description from
196 * storage.
197 */
198 if ((selected_slot == NULL) &&
199 (unlocked_persistent_key_slot != NULL)) {
200 selected_slot = unlocked_persistent_key_slot;
201 selected_slot->lock_count = 1;
202 psa_wipe_key_slot(selected_slot);
203 }
204
205 if (selected_slot != NULL) {
206 status = psa_lock_key_slot(selected_slot);
207 if (status != PSA_SUCCESS) {
208 goto error;
209 }
210
211 *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
212 ((psa_key_id_t) (selected_slot - global_data.key_slots));
213 *p_slot = selected_slot;
214
215 return PSA_SUCCESS;
216 }
217 status = PSA_ERROR_INSUFFICIENT_MEMORY;
218
219 error:
220 *p_slot = NULL;
221 *volatile_key_id = 0;
222
223 return status;
224 }
225
226 #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
psa_load_persistent_key_into_slot(psa_key_slot_t * slot)227 static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot)
228 {
229 psa_status_t status = PSA_SUCCESS;
230 uint8_t *key_data = NULL;
231 size_t key_data_length = 0;
232
233 status = psa_load_persistent_key(&slot->attr,
234 &key_data, &key_data_length);
235 if (status != PSA_SUCCESS) {
236 goto exit;
237 }
238
239 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
240 /* Special handling is required for loading keys associated with a
241 * dynamically registered SE interface. */
242 const psa_drv_se_t *drv;
243 psa_drv_se_context_t *drv_context;
244 if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) {
245 psa_se_key_data_storage_t *data;
246
247 if (key_data_length != sizeof(*data)) {
248 status = PSA_ERROR_DATA_INVALID;
249 goto exit;
250 }
251 data = (psa_se_key_data_storage_t *) key_data;
252 status = psa_copy_key_material_into_slot(
253 slot, data->slot_number, sizeof(data->slot_number));
254 goto exit;
255 }
256 #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
257
258 status = psa_copy_key_material_into_slot(slot, key_data, key_data_length);
259
260 exit:
261 psa_free_persistent_key_data(key_data, key_data_length);
262 return status;
263 }
264 #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
265
266 #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
267
psa_load_builtin_key_into_slot(psa_key_slot_t * slot)268 static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot)
269 {
270 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
271 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
272 psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
273 psa_drv_slot_number_t slot_number = 0;
274 size_t key_buffer_size = 0;
275 size_t key_buffer_length = 0;
276
277 if (!psa_key_id_is_builtin(
278 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) {
279 return PSA_ERROR_DOES_NOT_EXIST;
280 }
281
282 /* Check the platform function to see whether this key actually exists */
283 status = mbedtls_psa_platform_get_builtin_key(
284 slot->attr.id, &lifetime, &slot_number);
285 if (status != PSA_SUCCESS) {
286 return status;
287 }
288
289 /* Set required key attributes to ensure get_builtin_key can retrieve the
290 * full attributes. */
291 psa_set_key_id(&attributes, slot->attr.id);
292 psa_set_key_lifetime(&attributes, lifetime);
293
294 /* Get the full key attributes from the driver in order to be able to
295 * calculate the required buffer size. */
296 status = psa_driver_wrapper_get_builtin_key(
297 slot_number, &attributes,
298 NULL, 0, NULL);
299 if (status != PSA_ERROR_BUFFER_TOO_SMALL) {
300 /* Builtin keys cannot be defined by the attributes alone */
301 if (status == PSA_SUCCESS) {
302 status = PSA_ERROR_CORRUPTION_DETECTED;
303 }
304 return status;
305 }
306
307 /* If the key should exist according to the platform, then ask the driver
308 * what its expected size is. */
309 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
310 &key_buffer_size);
311 if (status != PSA_SUCCESS) {
312 return status;
313 }
314
315 /* Allocate a buffer of the required size and load the builtin key directly
316 * into the (now properly sized) slot buffer. */
317 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
318 if (status != PSA_SUCCESS) {
319 return status;
320 }
321
322 status = psa_driver_wrapper_get_builtin_key(
323 slot_number, &attributes,
324 slot->key.data, slot->key.bytes, &key_buffer_length);
325 if (status != PSA_SUCCESS) {
326 goto exit;
327 }
328
329 /* Copy actual key length and core attributes into the slot on success */
330 slot->key.bytes = key_buffer_length;
331 slot->attr = attributes.core;
332
333 exit:
334 if (status != PSA_SUCCESS) {
335 psa_remove_key_data_from_memory(slot);
336 }
337 return status;
338 }
339 #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
340
psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,psa_key_slot_t ** p_slot)341 psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
342 psa_key_slot_t **p_slot)
343 {
344 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
345
346 *p_slot = NULL;
347 if (!global_data.key_slots_initialized) {
348 return PSA_ERROR_BAD_STATE;
349 }
350
351 /*
352 * On success, the pointer to the slot is passed directly to the caller
353 * thus no need to unlock the key slot here.
354 */
355 status = psa_get_and_lock_key_slot_in_memory(key, p_slot);
356 if (status != PSA_ERROR_DOES_NOT_EXIST) {
357 return status;
358 }
359
360 /* Loading keys from storage requires support for such a mechanism */
361 #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
362 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
363 psa_key_id_t volatile_key_id;
364
365 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
366 if (status != PSA_SUCCESS) {
367 return status;
368 }
369
370 (*p_slot)->attr.id = key;
371 (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
372
373 status = PSA_ERROR_DOES_NOT_EXIST;
374 #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
375 /* Load keys in the 'builtin' range through their own interface */
376 status = psa_load_builtin_key_into_slot(*p_slot);
377 #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
378
379 #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
380 if (status == PSA_ERROR_DOES_NOT_EXIST) {
381 status = psa_load_persistent_key_into_slot(*p_slot);
382 }
383 #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
384
385 if (status != PSA_SUCCESS) {
386 psa_wipe_key_slot(*p_slot);
387 if (status == PSA_ERROR_DOES_NOT_EXIST) {
388 status = PSA_ERROR_INVALID_HANDLE;
389 }
390 } else {
391 /* Add implicit usage flags. */
392 psa_extend_key_usage_flags(&(*p_slot)->attr.policy.usage);
393 }
394
395 return status;
396 #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
397 return PSA_ERROR_INVALID_HANDLE;
398 #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
399 }
400
psa_unlock_key_slot(psa_key_slot_t * slot)401 psa_status_t psa_unlock_key_slot(psa_key_slot_t *slot)
402 {
403 if (slot == NULL) {
404 return PSA_SUCCESS;
405 }
406
407 if (slot->lock_count > 0) {
408 slot->lock_count--;
409 return PSA_SUCCESS;
410 }
411
412 /*
413 * As the return error code may not be handled in case of multiple errors,
414 * do our best to report if the lock counter is equal to zero. Assert with
415 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is strictly greater
416 * than zero: if the MBEDTLS_TEST_HOOKS configuration option is enabled and
417 * the function is called as part of the execution of a test suite, the
418 * execution of the test suite is stopped in error if the assertion fails.
419 */
420 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count > 0);
421 return PSA_ERROR_CORRUPTION_DETECTED;
422 }
423
psa_validate_key_location(psa_key_lifetime_t lifetime,psa_se_drv_table_entry_t ** p_drv)424 psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
425 psa_se_drv_table_entry_t **p_drv)
426 {
427 if (psa_key_lifetime_is_external(lifetime)) {
428 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
429 /* Check whether a driver is registered against this lifetime */
430 psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
431 if (driver != NULL) {
432 if (p_drv != NULL) {
433 *p_drv = driver;
434 }
435 return PSA_SUCCESS;
436 }
437 #else /* MBEDTLS_PSA_CRYPTO_SE_C */
438 (void) p_drv;
439 #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
440
441 #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
442 /* Key location for external keys gets checked by the wrapper */
443 return PSA_SUCCESS;
444 #else /* MBEDTLS_PSA_CRYPTO_DRIVERS */
445 /* No support for external lifetimes at all, or dynamic interface
446 * did not find driver for requested lifetime. */
447 return PSA_ERROR_INVALID_ARGUMENT;
448 #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
449 } else {
450 /* Local/internal keys are always valid */
451 return PSA_SUCCESS;
452 }
453 }
454
psa_validate_key_persistence(psa_key_lifetime_t lifetime)455 psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
456 {
457 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
458 /* Volatile keys are always supported */
459 return PSA_SUCCESS;
460 } else {
461 /* Persistent keys require storage support */
462 #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
463 if (PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)) {
464 return PSA_ERROR_INVALID_ARGUMENT;
465 } else {
466 return PSA_SUCCESS;
467 }
468 #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
469 return PSA_ERROR_NOT_SUPPORTED;
470 #endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
471 }
472 }
473
psa_open_key(mbedtls_svc_key_id_t key,psa_key_handle_t * handle)474 psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle)
475 {
476 #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
477 defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
478 psa_status_t status;
479 psa_key_slot_t *slot;
480
481 status = psa_get_and_lock_key_slot(key, &slot);
482 if (status != PSA_SUCCESS) {
483 *handle = PSA_KEY_HANDLE_INIT;
484 if (status == PSA_ERROR_INVALID_HANDLE) {
485 status = PSA_ERROR_DOES_NOT_EXIST;
486 }
487
488 return status;
489 }
490
491 *handle = key;
492
493 return psa_unlock_key_slot(slot);
494
495 #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
496 (void) key;
497 *handle = PSA_KEY_HANDLE_INIT;
498 return PSA_ERROR_NOT_SUPPORTED;
499 #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
500 }
501
psa_close_key(psa_key_handle_t handle)502 psa_status_t psa_close_key(psa_key_handle_t handle)
503 {
504 psa_status_t status;
505 psa_key_slot_t *slot;
506
507 if (psa_key_handle_is_null(handle)) {
508 return PSA_SUCCESS;
509 }
510
511 status = psa_get_and_lock_key_slot_in_memory(handle, &slot);
512 if (status != PSA_SUCCESS) {
513 if (status == PSA_ERROR_DOES_NOT_EXIST) {
514 status = PSA_ERROR_INVALID_HANDLE;
515 }
516
517 return status;
518 }
519 if (slot->lock_count <= 1) {
520 return psa_wipe_key_slot(slot);
521 } else {
522 return psa_unlock_key_slot(slot);
523 }
524 }
525
psa_purge_key(mbedtls_svc_key_id_t key)526 psa_status_t psa_purge_key(mbedtls_svc_key_id_t key)
527 {
528 psa_status_t status;
529 psa_key_slot_t *slot;
530
531 status = psa_get_and_lock_key_slot_in_memory(key, &slot);
532 if (status != PSA_SUCCESS) {
533 return status;
534 }
535
536 if ((!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
537 (slot->lock_count <= 1)) {
538 return psa_wipe_key_slot(slot);
539 } else {
540 return psa_unlock_key_slot(slot);
541 }
542 }
543
mbedtls_psa_get_stats(mbedtls_psa_stats_t * stats)544 void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats)
545 {
546 size_t slot_idx;
547
548 memset(stats, 0, sizeof(*stats));
549
550 for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
551 const psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
552 if (psa_is_key_slot_locked(slot)) {
553 ++stats->locked_slots;
554 }
555 if (!psa_is_key_slot_occupied(slot)) {
556 ++stats->empty_slots;
557 continue;
558 }
559 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
560 ++stats->volatile_slots;
561 } else {
562 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
563 ++stats->persistent_slots;
564 if (id > stats->max_open_internal_key_id) {
565 stats->max_open_internal_key_id = id;
566 }
567 }
568 if (PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime) !=
569 PSA_KEY_LOCATION_LOCAL_STORAGE) {
570 psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
571 ++stats->external_slots;
572 if (id > stats->max_open_external_key_id) {
573 stats->max_open_external_key_id = id;
574 }
575 }
576 }
577 }
578
579 #endif /* MBEDTLS_PSA_CRYPTO_C */
580