nimble/sm: Use packed structures for sign info
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/14d869c4 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/14d869c4 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/14d869c4 Branch: refs/heads/develop Commit: 14d869c43b821a4459667c29e1baa4fa776e9a05 Parents: b371c86 Author: Szymon Janc <[email protected]> Authored: Tue Jan 17 19:24:39 2017 +0100 Committer: Szymon Janc <[email protected]> Committed: Wed Jan 25 15:44:15 2017 +0100 ---------------------------------------------------------------------- net/nimble/host/src/ble_sm.c | 30 ++++++++++++++++-------- net/nimble/host/src/ble_sm_cmd.c | 4 ++-- net/nimble/host/src/ble_sm_priv.h | 3 +-- net/nimble/host/test/src/ble_sm_test_util.c | 6 ++--- 4 files changed, 26 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/14d869c4/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 6ca3ef4..7bc6428 100644 --- a/net/nimble/host/src/ble_sm.c +++ b/net/nimble/host/src/ble_sm.c @@ -1720,7 +1720,7 @@ ble_sm_key_exch_exec(struct ble_sm_proc *proc, struct ble_sm_result *res, { struct ble_sm_id_addr_info *addr_info; struct ble_hs_conn_addrs addrs; - struct ble_sm_sign_info sign_info; + struct ble_sm_sign_info *sign_info; struct ble_sm_master_id *master_id; struct ble_sm_enc_info *enc_info; struct ble_sm_id_info *id_info; @@ -1834,16 +1834,26 @@ ble_sm_key_exch_exec(struct ble_sm_proc *proc, struct ble_sm_result *res, if (our_key_dist & BLE_SM_PAIR_KEY_DIST_SIGN) { /* Send signing information. */ - rc = ble_sm_gen_csrk(proc, sign_info.sig_key); - if (rc != 0) { + sign_info = ble_sm_cmd_get(BLE_SM_OP_SIGN_INFO, sizeof(*sign_info), + &txom); + if (!sign_info) { + rc = BLE_HS_ENOMEM; goto err; } - rc = ble_sm_sign_info_tx(proc->conn_handle, &sign_info); + + rc = ble_sm_gen_csrk(proc, sign_info->sig_key); if (rc != 0) { + os_mbuf_free_chain(txom); goto err; } + proc->our_keys.csrk_valid = 1; - memcpy(proc->our_keys.csrk, sign_info.sig_key, 16); + memcpy(proc->our_keys.csrk, sign_info->sig_key, 16); + + rc = ble_sm_tx(proc->conn_handle, txom); + if (rc != 0) { + goto err; + } } if (proc->flags & BLE_SM_PROC_F_INITIATOR || proc->rx_key_flags == 0) { @@ -2021,18 +2031,18 @@ static void ble_sm_sign_info_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om, struct ble_sm_result *res) { - struct ble_sm_sign_info cmd; + struct ble_sm_sign_info *cmd; struct ble_sm_proc *proc; - res->app_status = ble_hs_mbuf_pullup_base(om, BLE_SM_SIGN_INFO_SZ); + res->app_status = ble_hs_mbuf_pullup_base(om, sizeof(*cmd)); if (res->app_status != 0) { res->sm_err = BLE_SM_ERR_UNSPECIFIED; res->enc_cb = 1; return; } - ble_sm_sign_info_parse((*om)->om_data, (*om)->om_len, &cmd); - BLE_SM_LOG_CMD(0, "sign info", conn_handle, ble_sm_sign_info_log, &cmd); + cmd = (struct ble_sm_sign_info *)(*om)->om_data; + BLE_SM_LOG_CMD(0, "sign info", conn_handle, ble_sm_sign_info_log, cmd); ble_hs_lock(); @@ -2043,7 +2053,7 @@ ble_sm_sign_info_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om, } else { proc->rx_key_flags &= ~BLE_SM_KE_F_SIGN_INFO; - memcpy(proc->peer_keys.csrk, cmd.sig_key, 16); + memcpy(proc->peer_keys.csrk, cmd->sig_key, 16); proc->peer_keys.csrk_valid = 1; ble_sm_key_rxed(proc, res); http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/14d869c4/net/nimble/host/src/ble_sm_cmd.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/ble_sm_cmd.c b/net/nimble/host/src/ble_sm_cmd.c index a3b693c..6d2b5d7 100644 --- a/net/nimble/host/src/ble_sm_cmd.c +++ b/net/nimble/host/src/ble_sm_cmd.c @@ -544,7 +544,7 @@ ble_sm_sign_info_write(void *payload, int len, struct ble_sm_sign_info *cmd) { uint8_t *u8ptr; - BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + BLE_SM_SIGN_INFO_SZ); + BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_sign_info)); u8ptr = payload; @@ -560,7 +560,7 @@ ble_sm_sign_info_tx(uint16_t conn_handle, struct ble_sm_sign_info *cmd) BLE_SM_LOG_CMD(1, "sign info", conn_handle, ble_sm_sign_info_log, cmd); - rc = ble_sm_init_req(BLE_SM_SIGN_INFO_SZ, &txom); + rc = ble_sm_init_req(sizeof(struct ble_sm_sign_info), &txom); if (rc != 0) { return BLE_HS_ENOMEM; } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/14d869c4/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 45fd484..1820592 100644 --- a/net/nimble/host/src/ble_sm_priv.h +++ b/net/nimble/host/src/ble_sm_priv.h @@ -157,10 +157,9 @@ struct ble_sm_id_addr_info { * | (Code=0x0A) | 1 | * | csrk | 16 | */ -#define BLE_SM_SIGN_INFO_SZ 16 struct ble_sm_sign_info { uint8_t sig_key[16]; -}; +} __attribute__((packed)); /** * | Parameter | Size (octets) | http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/14d869c4/net/nimble/host/test/src/ble_sm_test_util.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/test/src/ble_sm_test_util.c b/net/nimble/host/test/src/ble_sm_test_util.c index ad72efa..8ff0f1a 100644 --- a/net/nimble/host/test/src/ble_sm_test_util.c +++ b/net/nimble/host/test/src/ble_sm_test_util.c @@ -589,12 +589,12 @@ ble_sm_test_util_rx_sign_info(uint16_t conn_handle, hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR( 2, BLE_HCI_PB_FIRST_FLUSH, - BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + BLE_SM_SIGN_INFO_SZ); + BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_sign_info)); om = ble_hs_mbuf_l2cap_pkt(); TEST_ASSERT_FATAL(om != NULL); - payload_len = sizeof(struct ble_sm_hdr) + BLE_SM_SIGN_INFO_SZ; + payload_len = sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_sign_info); v = os_mbuf_extend(om, payload_len); TEST_ASSERT_FATAL(v != NULL); @@ -797,7 +797,7 @@ ble_sm_test_util_verify_tx_sign_info(struct ble_sm_sign_info *exp_cmd) ble_hs_test_util_tx_all(); om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_SIGN_INFO, - BLE_SM_SIGN_INFO_SZ); + sizeof(struct ble_sm_sign_info)); ble_sm_sign_info_parse(om->om_data, om->om_len, &cmd); TEST_ASSERT(memcmp(cmd.sig_key, exp_cmd->sig_key, 16) == 0);
