This is an automated email from the ASF dual-hosted git repository.

janc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git


The following commit(s) were added to refs/heads/master by this push:
     new 5778cd6d2 host/sm: Persist keys before firing pairing complete callback
5778cd6d2 is described below

commit 5778cd6d2228e9222f8bb5797c323e4c12da0a06
Author: Guy Mishol <[email protected]>
AuthorDate: Mon Mar 16 17:19:37 2026 +0200

    host/sm: Persist keys before firing pairing complete callback
    
    Applications might expect security keys (LTK, IRK, etc.) to be
    available in the store when the ble_gap_pairing_complete_event()
    and ble_gap_enc_event() are received.
    Previously, ble_sm_persist_keys() was called after those events
    causing store reads to fail with BLE_HS_ENOENT on the first pairing.
    
    Move ble_sm_persist_keys() before the pairing complete callback so
    applications can immediately read the persisted keys.
---
 nimble/host/src/ble_sm.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/nimble/host/src/ble_sm.c b/nimble/host/src/ble_sm.c
index 5e06e5172..79b321753 100644
--- a/nimble/host/src/ble_sm.c
+++ b/nimble/host/src/ble_sm.c
@@ -944,6 +944,16 @@ ble_sm_process_result(uint16_t conn_handle, struct 
ble_sm_result *res,
 
         ble_hs_unlock();
 
+        /* Persist keys if bonding has successfully completed.
+         * This must happen before firing the pairing complete callback
+         * so that applications can read the persisted keys from the store.
+         */
+        if (res->app_status == 0 && rm && proc != NULL &&
+            proc->flags & BLE_SM_PROC_F_BONDING) {
+
+            ble_sm_persist_keys(proc);
+        }
+
         if (res->enc_cb &&
             res->app_status != BLE_HS_ENOTCONN) {
             /* Do not send this event on broken connection */
@@ -965,14 +975,6 @@ ble_sm_process_result(uint16_t conn_handle, struct 
ble_sm_result *res,
             ble_gap_passkey_event(conn_handle, &res->passkey_params);
         }
 
-        /* Persist keys if bonding has successfully completed. */
-        if (res->app_status == 0    &&
-            rm                      &&
-            proc->flags & BLE_SM_PROC_F_BONDING) {
-
-            ble_sm_persist_keys(proc);
-        }
-
         if (rm) {
             ble_sm_proc_free(proc);
             break;

Reply via email to