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 <aleksandr.khromykh@nordicsemi.no>
This commit is contained in:
Aleksandr Khromykh 2025-07-07 14:12:11 +02:00 committed by Chris Friedt
parent 8c03410af0
commit c02ec1f645

View File

@ -452,9 +452,14 @@ int bt_mesh_key_import(enum bt_mesh_key_type type, const uint8_t in[16], struct
psa_set_key_bits(&key_attributes, 128);
status = psa_import_key(&key_attributes, in, 16, &out->key);
err = status == PSA_SUCCESS ? 0 :
status == PSA_ERROR_ALREADY_EXISTS ? -EALREADY : -EIO;
if (status == PSA_ERROR_ALREADY_EXISTS) {
LOG_WRN("Key with ID 0x%4x already exists (desync between mesh and PSA ITS)",
key_id);
(void)psa_destroy_key(key_id);
status = psa_import_key(&key_attributes, in, 16, &out->key);
}
err = status == PSA_SUCCESS ? 0 : -EIO;
if (err && key_id != PSA_KEY_ID_NULL) {
keyid_free(key_id);
}