From fbdb118f90b638a67f762b695d7a929381546d00 Mon Sep 17 00:00:00 2001 From: Rahul Tank Date: Mon, 6 Jan 2025 12:02:13 +0530 Subject: [PATCH] fix(nimble) Clear our and peer security records during unpair --- nimble/host/src/ble_gap.c | 45 ++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/nimble/host/src/ble_gap.c b/nimble/host/src/ble_gap.c index fdbf73a3c..c1159dcc3 100644 --- a/nimble/host/src/ble_gap.c +++ b/nimble/host/src/ble_gap.c @@ -7311,9 +7311,7 @@ int ble_gap_unpair(const ble_addr_t *peer_addr) { #if NIMBLE_BLE_SM - int rc; - int ltk_rc = 0; - int irk_rc = 0; + int rc, err = 0; struct ble_hs_conn *conn; union ble_store_value value; union ble_store_key key; @@ -7356,31 +7354,26 @@ ble_gap_unpair(const ble_addr_t *peer_addr) // Checking if the device is in ble_store if (!rc) { if (value.sec.irk_present) { - // Delete the IRK as it is Distributed - irk_rc = ble_hs_pvcy_remove_entry(key.sec.peer_addr.type, - key.sec.peer_addr.val); - if (irk_rc != 0) { - BLE_HS_LOG(ERROR, "Error while removing IRK , rc = %x\n",irk_rc); + // Delete the IRK as it is Distributed + rc = ble_hs_pvcy_remove_entry(key.sec.peer_addr.type,key.sec.peer_addr.val); + if (rc != 0) { + BLE_HS_LOG(ERROR, "Error while removing IRK , rc = %x\n",rc); } } - if (value.sec.ltk_present || value.sec.irk_present || value.sec.csrk_present) { - // Delete the Peer record from store as LTK is present - ltk_rc = ble_store_util_delete_peer(&key.sec.peer_addr); - if (ltk_rc != 0) { - BLE_HS_LOG(ERROR, "Error while removing LTK , rc = %x\n",ltk_rc); - } + // Delete the Peer record from store as LTK is present + rc = ble_store_util_delete_peer(&key.sec.peer_addr); + if (rc != 0) { + BLE_HS_LOG(ERROR, "Error while removing LTK , rc = %x\n",rc); + } + } else { + rc = ble_store_read(BLE_STORE_OBJ_TYPE_OUR_SEC, &key, &value); + if (!rc) { + ble_store_util_delete_peer(&key.sec.peer_addr); + } else { + BLE_HS_LOG(ERROR,"No record found for the given address in ble store , rc = %x\n",rc); + err = rc ; } - } - else { - rc = ble_store_read(BLE_STORE_OBJ_TYPE_OUR_SEC, &key, &value); - if (!rc) { - ble_store_util_delete_peer(&key.sec.peer_addr); - } - else { - BLE_HS_LOG(ERROR,"No record found for the given address in ble store , rc = %x\n",rc); - return rc; - } } #if MYNEWT_VAL(BLE_SMP_ID_RESET) @@ -7391,6 +7384,10 @@ ble_gap_unpair(const ble_addr_t *peer_addr) ble_gap_reset_irk(); #endif + if (err) { + return err; + } + return 0; #else return BLE_HS_ENOTSUP;