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:28:06 UTC

[44/50] [abbrv] incubator-mynewt-core git commit: BLE Host - Search DB when initiating security.

BLE Host - Search DB when initiating security.


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/7792ec58
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/7792ec58
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/7792ec58

Branch: refs/heads/develop
Commit: 7792ec58b93c914698df77c9f2db3d848e844b13
Parents: ad243cf
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Jun 14 12:36:57 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Jun 14 19:24:36 2016 -0700

----------------------------------------------------------------------
 apps/bletiny/src/bletiny.h    |  1 +
 apps/bletiny/src/cmd.c        | 27 ++++++++++++++++++++++++---
 apps/bletiny/src/main.c       | 13 +++++++++++++
 net/nimble/host/src/ble_gap.c | 32 +++++++++++++++++++++++++++-----
 4 files changed, 65 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7792ec58/apps/bletiny/src/bletiny.h
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/bletiny.h b/apps/bletiny/src/bletiny.h
index b66a301..60c80c7 100644
--- a/apps/bletiny/src/bletiny.h
+++ b/apps/bletiny/src/bletiny.h
@@ -172,6 +172,7 @@ int bletiny_datalen(uint16_t conn_handle, uint16_t tx_octets,
 int bletiny_l2cap_update(uint16_t conn_handle,
                           struct ble_l2cap_sig_update_params *params);
 int bletiny_sec_start(uint16_t conn_handle);
+int bletiny_sec_pair(uint16_t conn_handle);
 int bletiny_sec_restart(uint16_t conn_handle, uint8_t *ltk, uint16_t ediv,
                         uint64_t rand_val, int auth);
 int bletiny_tx_start(uint16_t handle, uint16_t len, uint16_t rate,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7792ec58/apps/bletiny/src/cmd.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/cmd.c b/apps/bletiny/src/cmd.c
index 3598bb3..3ac8592 100644
--- a/apps/bletiny/src/cmd.c
+++ b/apps/bletiny/src/cmd.c
@@ -1022,6 +1022,26 @@ cmd_show(int argc, char **argv)
  *****************************************************************************/
 
 static int
+cmd_sec_pair(int argc, char **argv)
+{
+    uint16_t conn_handle;
+    int rc;
+
+    conn_handle = parse_arg_uint16("conn", &rc);
+    if (rc != 0) {
+        return rc;
+    }
+
+    rc = bletiny_sec_pair(conn_handle);
+    if (rc != 0) {
+        console_printf("error initiating pairing; rc=%d\n", rc);
+        return rc;
+    }
+
+    return 0;
+}
+
+static int
 cmd_sec_start(int argc, char **argv)
 {
     uint16_t conn_handle;
@@ -1042,7 +1062,7 @@ cmd_sec_start(int argc, char **argv)
 }
 
 static int
-cmd_sec_restart(int argc, char **argv)
+cmd_sec_enc(int argc, char **argv)
 {
     uint16_t conn_handle;
     uint16_t ediv;
@@ -1079,7 +1099,7 @@ cmd_sec_restart(int argc, char **argv)
     }
 
     if (rc != 0) {
-        console_printf("error starting encryption; rc=%d\n", rc);
+        console_printf("error initiating encryption; rc=%d\n", rc);
         return rc;
     }
 
@@ -1087,8 +1107,9 @@ cmd_sec_restart(int argc, char **argv)
 }
 
 static struct cmd_entry cmd_sec_entries[] = {
+    { "pair", cmd_sec_pair },
     { "start", cmd_sec_start },
-    { "restart", cmd_sec_restart },
+    { "enc", cmd_sec_enc },
 };
 
 static int

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7792ec58/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index c8a4840..ad55e68 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -1351,6 +1351,19 @@ bletiny_l2cap_update(uint16_t conn_handle,
 }
 
 int
+bletiny_sec_pair(uint16_t conn_handle)
+{
+#if !NIMBLE_OPT(SM)
+    return BLE_HS_ENOTSUP;
+#endif
+
+    int rc;
+
+    rc = ble_sm_pair_initiate(conn_handle);
+    return rc;
+}
+
+int
 bletiny_sec_start(uint16_t conn_handle)
 {
 #if !NIMBLE_OPT(SM)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7792ec58/net/nimble/host/src/ble_gap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gap.c b/net/nimble/host/src/ble_gap.c
index a7ba2e4..a0fb13c 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -2113,20 +2113,42 @@ ble_gap_security_initiate(uint16_t conn_handle)
     return BLE_HS_ENOTSUP;
 #endif
 
+    struct ble_store_value_sec value_sec;
+    struct ble_store_key_sec key_sec;
+    struct ble_hs_conn_addrs addrs;
     ble_hs_conn_flags_t conn_flags;
+    struct ble_hs_conn *conn;
     int rc;
 
-    rc = ble_hs_atomic_conn_flags(conn_handle, &conn_flags);
-    if (rc != 0) {
-        return rc;
+    ble_hs_lock();
+    conn = ble_hs_conn_find(conn_handle);
+    if (conn != NULL) {
+        conn_flags = conn->bhc_flags;
+        ble_hs_conn_addrs(conn, &addrs);
+
+        memset(&key_sec, 0, sizeof key_sec);
+        key_sec.peer_addr_type = addrs.peer_id_addr_type;
+        memcpy(key_sec.peer_addr, addrs.peer_id_addr, 6);
+    }
+    ble_hs_unlock();
+
+    if (conn == NULL) {
+        return BLE_HS_ENOTCONN;
     }
 
     if (conn_flags & BLE_HS_CONN_F_MASTER) {
-        /* XXX: Search the security database for an LTK for this peer.  If one
+        /* Search the security database for an LTK for this peer.  If one
          * is found, perform the encryption procedure rather than the pairing
          * procedure.
          */
-        rc = ble_sm_pair_initiate(conn_handle);
+        rc = ble_store_read_peer_sec(&key_sec, &value_sec);
+        if (rc == 0 && value_sec.ltk_present) {
+            rc = ble_sm_enc_initiate(conn_handle, value_sec.ltk,
+                                     value_sec.ediv, value_sec.rand_num,
+                                     value_sec.authenticated);
+        } else {
+            rc = ble_sm_pair_initiate(conn_handle);
+        }
     } else {
         rc = ble_sm_slave_initiate(conn_handle);
     }