BLE Host - Fix some SM mst/slv mismatches.
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/cc094c5c Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/cc094c5c Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/cc094c5c Branch: refs/heads/develop Commit: cc094c5cd1c0b3ffc604853bdffab75db63f849b Parents: b6fa07a Author: Christopher Collins <[email protected]> Authored: Wed May 25 15:03:45 2016 -0700 Committer: Christopher Collins <[email protected]> Committed: Wed May 25 15:21:22 2016 -0700 ---------------------------------------------------------------------- net/nimble/host/src/ble_l2cap_sm.c | 41 +++++++++-------------- net/nimble/host/src/ble_store.c | 8 ++--- net/nimble/host/src/test/ble_l2cap_sm_test.c | 6 ++-- 3 files changed, 23 insertions(+), 32 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cc094c5c/net/nimble/host/src/ble_l2cap_sm.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/ble_l2cap_sm.c b/net/nimble/host/src/ble_l2cap_sm.c index 8e17ad7..a16ad55 100644 --- a/net/nimble/host/src/ble_l2cap_sm.c +++ b/net/nimble/host/src/ble_l2cap_sm.c @@ -507,29 +507,21 @@ ble_l2cap_sm_key_exchange_events(struct ble_l2cap_sm_proc *proc) !!(proc->flags & BLE_L2CAP_SM_PROC_F_AUTHENTICATED); value_ltk.sc = 0; - if (proc->flags & BLE_L2CAP_SM_PROC_F_INITIATOR) { - ble_store_write_mst_ltk(&value_ltk); - } else { - ble_store_write_slv_ltk(&value_ltk); - } + ble_store_write_slv_ltk(&value_ltk); } if (proc->peer_keys.ediv_rand_valid && proc->peer_keys.ltk_valid) { value_ltk.peer_addr_type = peer_addr_type; memcpy(value_ltk.peer_addr, peer_addr, sizeof value_ltk.peer_addr); - value_ltk.ediv = proc->our_keys.ediv; - value_ltk.rand_num = proc->our_keys.rand_val; - memcpy(value_ltk.key, proc->our_keys.ltk, + value_ltk.ediv = proc->peer_keys.ediv; + value_ltk.rand_num = proc->peer_keys.rand_val; + memcpy(value_ltk.key, proc->peer_keys.ltk, sizeof value_ltk.key); value_ltk.authenticated = !!(proc->flags & BLE_L2CAP_SM_PROC_F_AUTHENTICATED); value_ltk.sc = 0; - if (proc->flags & BLE_L2CAP_SM_PROC_F_INITIATOR) { - ble_store_write_slv_ltk(&value_ltk); - } else { - ble_store_write_mst_ltk(&value_ltk); - } + ble_store_write_mst_ltk(&value_ltk); } /* XXX: Persist other key data. */ @@ -1788,8 +1780,8 @@ ble_l2cap_sm_rx_pair_fail(uint16_t conn_handle, uint8_t op, static int ble_l2cap_sm_lt_key_req_ltk_handle(struct hci_le_lt_key_req *evt) { - union ble_store_value store_value; - union ble_store_key store_key; + struct ble_store_value_ltk value_ltk; + struct ble_store_key_ltk key_ltk; struct ble_l2cap_sm_proc *proc; struct ble_l2cap_sm_proc *prev; int store_rc; @@ -1797,18 +1789,17 @@ ble_l2cap_sm_lt_key_req_ltk_handle(struct hci_le_lt_key_req *evt) /* Tell applicaiton to look up LTK by ediv/rand pair. */ /* XXX: Also filter by peer address? */ - memset(&store_key, 0, sizeof store_key); - store_key.ltk.peer_addr_type = BLE_STORE_ADDR_TYPE_NONE; - store_key.ltk.ediv = evt->encrypted_diversifier; - store_key.ltk.ediv_present = 1; - store_key.ltk.rand_num = evt->random_number; - store_key.ltk.rand_num_present = 1; - store_rc = ble_store_read(BLE_STORE_OBJ_TYPE_MST_LTK, &store_key, - &store_value); + memset(&key_ltk, 0, sizeof key_ltk); + key_ltk.peer_addr_type = BLE_STORE_ADDR_TYPE_NONE; + key_ltk.ediv = evt->encrypted_diversifier; + key_ltk.ediv_present = 1; + key_ltk.rand_num = evt->random_number; + key_ltk.rand_num_present = 1; + store_rc = ble_store_read_slv_ltk(&key_ltk, &value_ltk); if (store_rc == 0) { /* Store provided a key; send it to the controller. */ rc = ble_l2cap_sm_lt_key_req_reply_tx(evt->connection_handle, - store_value.ltk.key); + value_ltk.key); } else { /* Application does not have the requested key in its database. Send a * negative reply to the controller. @@ -1824,7 +1815,7 @@ ble_l2cap_sm_lt_key_req_ltk_handle(struct hci_le_lt_key_req *evt) rc = BLE_HS_EUNKNOWN; } else if (store_rc == 0 && rc == 0) { proc->state = BLE_L2CAP_SM_PROC_STATE_ENC_CHANGE; - if (store_value.ltk.authenticated) { + if (value_ltk.authenticated) { proc->flags |= BLE_L2CAP_SM_PROC_F_AUTHENTICATED; } } else { http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cc094c5c/net/nimble/host/src/ble_store.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/ble_store.c b/net/nimble/host/src/ble_store.c index 2694d60..91e7fb5 100644 --- a/net/nimble/host/src/ble_store.c +++ b/net/nimble/host/src/ble_store.c @@ -75,7 +75,7 @@ ble_store_read_slv_ltk(struct ble_store_key_ltk *key_ltk, store_key = (void *)key_ltk; store_value = (void *)value_ltk; - rc = ble_store_read(BLE_STORE_OBJ_TYPE_MST_LTK, store_key, store_value); + rc = ble_store_read(BLE_STORE_OBJ_TYPE_SLV_LTK, store_key, store_value); return rc; } @@ -86,7 +86,7 @@ ble_store_write_slv_ltk(struct ble_store_value_ltk *value_ltk) int rc; store_value = (void *)value_ltk; - rc = ble_store_write(BLE_STORE_OBJ_TYPE_MST_LTK, store_value); + rc = ble_store_write(BLE_STORE_OBJ_TYPE_SLV_LTK, store_value); return rc; } @@ -100,7 +100,7 @@ ble_store_read_mst_ltk(struct ble_store_key_ltk *key_ltk, store_key = (void *)key_ltk; store_value = (void *)value_ltk; - rc = ble_store_read(BLE_STORE_OBJ_TYPE_SLV_LTK, store_key, store_value); + rc = ble_store_read(BLE_STORE_OBJ_TYPE_MST_LTK, store_key, store_value); return rc; } @@ -111,7 +111,7 @@ ble_store_write_mst_ltk(struct ble_store_value_ltk *value_ltk) int rc; store_value = (void *)value_ltk; - rc = ble_store_write(BLE_STORE_OBJ_TYPE_SLV_LTK, store_value); + rc = ble_store_write(BLE_STORE_OBJ_TYPE_MST_LTK, store_value); return rc; } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cc094c5c/net/nimble/host/src/test/ble_l2cap_sm_test.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/test/ble_l2cap_sm_test.c b/net/nimble/host/src/test/ble_l2cap_sm_test.c index 88cec19..60c52c8 100644 --- a/net/nimble/host/src/test/ble_l2cap_sm_test.c +++ b/net/nimble/host/src/test/ble_l2cap_sm_test.c @@ -1158,11 +1158,10 @@ ble_l2cap_sm_test_util_peer_bonding_good(int send_enc_req, uint8_t *ltk, ble_l2cap_sm_test_util_set_lt_key_req_reply_ack(0, 2); ble_l2cap_sm_test_util_rx_lt_key_req(2, rand_num, ediv); TEST_ASSERT(!conn->bhc_sec_state.enc_enabled); - TEST_ASSERT(ble_l2cap_sm_dbg_num_procs() == 1); /* Ensure the LTK request event got sent to the application. */ TEST_ASSERT(ble_l2cap_sm_test_store_obj_type == - BLE_STORE_OBJ_TYPE_MST_LTK); + BLE_STORE_OBJ_TYPE_SLV_LTK); TEST_ASSERT(ble_l2cap_sm_test_store_key.ltk.peer_addr_type == BLE_STORE_ADDR_TYPE_NONE); TEST_ASSERT(ble_l2cap_sm_test_store_key.ltk.ediv_present); @@ -1215,6 +1214,7 @@ ble_l2cap_sm_test_util_peer_bonding_bad(uint16_t ediv, uint64_t rand_num) ble_hs_lock(); conn = ble_hs_conn_find(2); TEST_ASSERT_FATAL(conn != NULL); + conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER; ble_hs_unlock(); TEST_ASSERT(!conn->bhc_sec_state.enc_enabled); @@ -1227,7 +1227,7 @@ ble_l2cap_sm_test_util_peer_bonding_bad(uint16_t ediv, uint64_t rand_num) /* Ensure the LTK request event got sent to the application. */ TEST_ASSERT(ble_l2cap_sm_test_store_obj_type == - BLE_STORE_OBJ_TYPE_MST_LTK); + BLE_STORE_OBJ_TYPE_SLV_LTK); TEST_ASSERT(ble_l2cap_sm_test_store_key.ltk.ediv_present); TEST_ASSERT(ble_l2cap_sm_test_store_key.ltk.ediv == ediv); TEST_ASSERT(ble_l2cap_sm_test_store_key.ltk.rand_num_present);
