You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2016/06/15 02:27:55 UTC

[33/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Persist keys if both sides want bonding

BLE Host - Persist keys if both sides want bonding


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/24cd9ab9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/24cd9ab9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/24cd9ab9

Branch: refs/heads/develop
Commit: 24cd9ab92ea33cfcf87df2ac35580d85be09dd60
Parents: 18bd234
Author: Christopher Collins <cc...@apache.org>
Authored: Fri Jun 10 10:06:08 2016 +0800
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Jun 14 19:23:39 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_sm.c      | 23 ++++++++++++++---------
 net/nimble/host/src/ble_sm_priv.h |  5 +++--
 2 files changed, 17 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/24cd9ab9/net/nimble/host/src/ble_sm.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm.c b/net/nimble/host/src/ble_sm.c
index 4c93110..8c57c3d 100644
--- a/net/nimble/host/src/ble_sm.c
+++ b/net/nimble/host/src/ble_sm.c
@@ -587,7 +587,6 @@ ble_sm_persist_keys(struct ble_sm_proc *proc)
 
     authenticated = proc->flags & BLE_SM_PROC_F_AUTHENTICATED;
 
-    proc->our_keys.irk_valid = 0;
     ble_sm_fill_store_value(peer_addr_type, peer_addr, authenticated,
                             &proc->our_keys, &value_sec);
     ble_store_write_slv_sec(&value_sec);
@@ -847,9 +846,11 @@ ble_sm_process_result(uint16_t conn_handle, struct ble_sm_result *res)
             ble_gap_passkey_event(conn_handle, &res->passkey_action);
         }
 
-        /* Persist keys if pairing has successfully completed. */
-        if (res->app_status == 0 && rm) {
-            BLE_HS_DBG_ASSERT(rm);
+        /* 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);
         }
 
@@ -1417,11 +1418,14 @@ ble_sm_pair_cfg(struct ble_sm_proc *proc)
         proc->flags |= BLE_SM_PROC_F_SC;
     }
 
-    ble_sm_key_dist(proc, &init_key_dist, &resp_key_dist);
     if (proc->pair_req.authreq & BLE_SM_PAIR_AUTHREQ_BOND &&
-        proc->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_BOND &&
-        (init_key_dist != 0 || resp_key_dist != 0)) {
+        proc->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_BOND) {
+
+        proc->flags |= BLE_SM_PROC_F_BONDING;
+    }
 
+    ble_sm_key_dist(proc, &init_key_dist, &resp_key_dist);
+    if (init_key_dist != 0 || resp_key_dist != 0) {
         proc->flags |= BLE_SM_PROC_F_KEY_EXCHANGE;
     }
 
@@ -1794,7 +1798,6 @@ ble_sm_key_exch_exec(struct ble_sm_proc *proc, struct ble_sm_result *res,
         }
 
         /* copy data to pass to application */
-        proc->our_keys.irk_valid = 1;
         proc->our_keys.addr_valid = 1;
         memcpy(proc->our_keys.irk, irk, 16);
         proc->our_keys.addr_type = addr_info.addr_type;
@@ -1946,7 +1949,9 @@ ble_sm_id_info_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om,
     } else {
         proc->rx_key_flags &= ~BLE_SM_KE_F_ID_INFO;
         proc->peer_keys.irk_valid = 1;
-        memcpy(proc->peer_keys.irk, cmd.irk, 16);
+
+        /* Store IRK in little endian. */
+        swap_buf(proc->peer_keys.irk, cmd.irk, 16);
 
         ble_sm_key_rxed(proc, res);
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/24cd9ab9/net/nimble/host/src/ble_sm_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm_priv.h b/net/nimble/host/src/ble_sm_priv.h
index fe5308e..d19f43f 100644
--- a/net/nimble/host/src/ble_sm_priv.h
+++ b/net/nimble/host/src/ble_sm_priv.h
@@ -219,6 +219,7 @@ struct ble_sm_dhkey_check {
 #define BLE_SM_PROC_F_AUTHENTICATED         0x08
 #define BLE_SM_PROC_F_KEY_EXCHANGE          0x10
 #define BLE_SM_PROC_F_SC                    0x20
+#define BLE_SM_PROC_F_BONDING               0x40
 
 #define BLE_SM_KE_F_ENC_INFO                0x01
 #define BLE_SM_KE_F_MASTER_ID               0x02
@@ -237,8 +238,8 @@ struct ble_sm_keys {
     uint16_t ediv;
     uint64_t rand_val;
     uint8_t addr_type;
-    uint8_t ltk[16];
-    uint8_t irk[16];
+    uint8_t ltk[16];    /* Little endian. */
+    uint8_t irk[16];    /* Little endian. */
     uint8_t csrk[16];
     uint8_t addr[6];
 };