Skip to content

Commit c02ec1f

Browse files
alxelaxcfriedt
authored andcommitted
bluetooth: mesh: remove persisted psa key if mesh does not own it
Commit adds destruction of the persisted in PSA ITS key if mesh does not own it (zero bit in the bitmap of persisted keys). This is not standard mesh behavior, but might happen if something happens between removing key data in mesh and in the crypto library (for example power off in bettwen). Previously, mesh wasn't able to import key with gotten stuck key id. The current fix reproduces more robust behavior. Signed-off-by: Aleksandr Khromykh <[email protected]>
1 parent 8c03410 commit c02ec1f

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

subsys/bluetooth/mesh/crypto_psa.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,14 @@ int bt_mesh_key_import(enum bt_mesh_key_type type, const uint8_t in[16], struct
452452
psa_set_key_bits(&key_attributes, 128);
453453

454454
status = psa_import_key(&key_attributes, in, 16, &out->key);
455-
err = status == PSA_SUCCESS ? 0 :
456-
status == PSA_ERROR_ALREADY_EXISTS ? -EALREADY : -EIO;
455+
if (status == PSA_ERROR_ALREADY_EXISTS) {
456+
LOG_WRN("Key with ID 0x%4x already exists (desync between mesh and PSA ITS)",
457+
key_id);
458+
(void)psa_destroy_key(key_id);
459+
status = psa_import_key(&key_attributes, in, 16, &out->key);
460+
}
457461

462+
err = status == PSA_SUCCESS ? 0 : -EIO;
458463
if (err && key_id != PSA_KEY_ID_NULL) {
459464
keyid_free(key_id);
460465
}

0 commit comments

Comments
 (0)