nimble/sm: Use packed structures for pairing req and rsp
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/cb4de66a Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/cb4de66a Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/cb4de66a Branch: refs/heads/develop Commit: cb4de66a54899d5afe5ca22a86d1c95e25c9ddd0 Parents: 286a0a2 Author: Szymon Janc <[email protected]> Authored: Tue Jan 17 19:24:28 2017 +0100 Committer: Szymon Janc <[email protected]> Committed: Wed Jan 25 15:44:14 2017 +0100 ---------------------------------------------------------------------- net/nimble/host/src/ble_sm.c | 107 ++++++++++++++--------- net/nimble/host/src/ble_sm_cmd.c | 71 +++++++++------ net/nimble/host/src/ble_sm_lgcy.c | 72 ++++----------- net/nimble/host/src/ble_sm_priv.h | 18 ++-- net/nimble/host/src/ble_sm_sc.c | 55 ++++++------ net/nimble/host/test/src/ble_sm_test_util.c | 52 +++++------ 6 files changed, 197 insertions(+), 178 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cb4de66a/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 8c647b7..5ccd72a 100644 --- a/net/nimble/host/src/ble_sm.c +++ b/net/nimble/host/src/ble_sm.c @@ -824,8 +824,12 @@ static void ble_sm_key_dist(struct ble_sm_proc *proc, uint8_t *out_init_key_dist, uint8_t *out_resp_key_dist) { - *out_init_key_dist = proc->pair_rsp.init_key_dist; - *out_resp_key_dist = proc->pair_rsp.resp_key_dist; + struct ble_sm_pair_cmd *pair_rsp; + + pair_rsp = (struct ble_sm_pair_cmd *) &proc->pair_rsp[1]; + + *out_init_key_dist = pair_rsp->init_key_dist; + *out_resp_key_dist = pair_rsp->resp_key_dist; /* Encryption info and master ID are only sent in legacy pairing. */ if (proc->flags & BLE_SM_PROC_F_SC) { @@ -1355,18 +1359,22 @@ ble_sm_state_after_pair(struct ble_sm_proc *proc) static void ble_sm_pair_cfg(struct ble_sm_proc *proc) { + struct ble_sm_pair_cmd *pair_req, *pair_rsp; uint8_t init_key_dist; uint8_t resp_key_dist; uint8_t rx_key_dist; - if (proc->pair_req.authreq & BLE_SM_PAIR_AUTHREQ_SC && - proc->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_SC) { + pair_req = (struct ble_sm_pair_cmd *) &proc->pair_req[1]; + pair_rsp = (struct ble_sm_pair_cmd *) &proc->pair_rsp[1]; + + if (pair_req->authreq & BLE_SM_PAIR_AUTHREQ_SC && + pair_rsp->authreq & BLE_SM_PAIR_AUTHREQ_SC) { proc->flags |= BLE_SM_PROC_F_SC; } - if (proc->pair_req.authreq & BLE_SM_PAIR_AUTHREQ_BOND && - proc->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_BOND) { + if (pair_req->authreq & BLE_SM_PAIR_AUTHREQ_BOND && + pair_rsp->authreq & BLE_SM_PAIR_AUTHREQ_BOND) { proc->flags |= BLE_SM_PROC_F_BONDING; } @@ -1391,48 +1399,57 @@ ble_sm_pair_cfg(struct ble_sm_proc *proc) proc->rx_key_flags |= BLE_SM_KE_F_SIGN_INFO; } - proc->key_size = min(proc->pair_req.max_enc_key_size, - proc->pair_rsp.max_enc_key_size); + proc->key_size = min(pair_req->max_enc_key_size, + pair_rsp->max_enc_key_size); } static void ble_sm_pair_exec(struct ble_sm_proc *proc, struct ble_sm_result *res, void *arg) { - struct ble_sm_pair_cmd cmd; + struct ble_sm_pair_cmd *cmd; + struct os_mbuf *txom; uint8_t ioact; int is_req; int rc; is_req = proc->flags & BLE_SM_PROC_F_INITIATOR; - cmd.io_cap = ble_hs_cfg.sm_io_cap; - cmd.oob_data_flag = ble_hs_cfg.sm_oob_data_flag; - cmd.authreq = ble_sm_build_authreq(); - cmd.max_enc_key_size = BLE_SM_PAIR_KEY_SZ_MAX; + cmd = ble_sm_cmd_get(is_req ? BLE_SM_OP_PAIR_REQ : BLE_SM_OP_PAIR_RSP, + sizeof(*cmd), &txom); + if (cmd == NULL) { + rc = BLE_HS_ENOMEM; + goto err; + } + + cmd->io_cap = ble_hs_cfg.sm_io_cap; + cmd->oob_data_flag = ble_hs_cfg.sm_oob_data_flag; + cmd->authreq = ble_sm_build_authreq(); + cmd->max_enc_key_size = BLE_SM_PAIR_KEY_SZ_MAX; if (is_req) { - cmd.init_key_dist = ble_hs_cfg.sm_our_key_dist; - cmd.resp_key_dist = ble_hs_cfg.sm_their_key_dist; + cmd->init_key_dist = ble_hs_cfg.sm_our_key_dist; + cmd->resp_key_dist = ble_hs_cfg.sm_their_key_dist; } else { + struct ble_sm_pair_cmd *pair_req; + + pair_req = (struct ble_sm_pair_cmd *) &proc->pair_req[1]; /* The response's key distribution flags field is the intersection of * the peer's preferences and our capabilities. */ - cmd.init_key_dist = proc->pair_req.init_key_dist & - ble_hs_cfg.sm_their_key_dist; - cmd.resp_key_dist = proc->pair_req.resp_key_dist & - ble_hs_cfg.sm_our_key_dist; - } - rc = ble_sm_pair_cmd_tx(proc->conn_handle, is_req, &cmd); - if (rc != 0) { - goto err; + cmd->init_key_dist = pair_req->init_key_dist & + ble_hs_cfg.sm_their_key_dist; + cmd->resp_key_dist = pair_req->resp_key_dist & + ble_hs_cfg.sm_our_key_dist; } if (is_req) { - proc->pair_req = cmd; + proc->pair_req[0] = BLE_SM_OP_PAIR_REQ; + memcpy(proc->pair_req + 1, cmd, sizeof(*cmd)); } else { - proc->pair_rsp = cmd; + proc->pair_rsp[0] = BLE_SM_OP_PAIR_RSP; + memcpy(proc->pair_rsp + 1, cmd, sizeof(*cmd)); ble_sm_pair_cfg(proc); proc->state = ble_sm_state_after_pair(proc); @@ -1443,6 +1460,11 @@ ble_sm_pair_exec(struct ble_sm_proc *proc, struct ble_sm_result *res, } } + rc = ble_sm_tx(proc->conn_handle, txom); + if (rc != 0) { + goto err; + } + res->app_status = ble_sm_gen_pair_rand(ble_sm_our_pair_rand(proc)); if (res->app_status != 0) { res->sm_err = BLE_SM_ERR_UNSPECIFIED; @@ -1464,18 +1486,19 @@ static void ble_sm_pair_req_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om, struct ble_sm_result *res) { - struct ble_sm_pair_cmd req; + struct ble_sm_pair_cmd *req; struct ble_sm_proc *proc; struct ble_sm_proc *prev; struct ble_hs_conn *conn; - res->app_status = ble_hs_mbuf_pullup_base(om, BLE_SM_PAIR_CMD_SZ); + res->app_status = ble_hs_mbuf_pullup_base(om, sizeof(*req)); if (res->app_status != 0) { return; } - ble_sm_pair_cmd_parse((*om)->om_data, (*om)->om_len, &req); - BLE_SM_LOG_CMD(0, "pair req", conn_handle, ble_sm_pair_cmd_log, &req); + req = (struct ble_sm_pair_cmd *)(*om)->om_data; + + BLE_SM_LOG_CMD(0, "pair req", conn_handle, ble_sm_pair_cmd_log, req); ble_hs_lock(); @@ -1497,16 +1520,17 @@ ble_sm_pair_req_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om, proc->state = BLE_SM_PROC_STATE_PAIR; ble_sm_insert(proc); - proc->pair_req = req; + proc->pair_req[0] = BLE_SM_OP_PAIR_REQ; + memcpy(proc->pair_req + 1, req, sizeof(*req)); conn = ble_hs_conn_find_assert(proc->conn_handle); if (conn->bhc_flags & BLE_HS_CONN_F_MASTER) { res->sm_err = BLE_SM_ERR_CMD_NOT_SUPP; res->app_status = BLE_HS_SM_US_ERR(BLE_SM_ERR_CMD_NOT_SUPP); - } else if (req.max_enc_key_size < BLE_SM_PAIR_KEY_SZ_MIN) { + } else if (req->max_enc_key_size < BLE_SM_PAIR_KEY_SZ_MIN) { res->sm_err = BLE_SM_ERR_ENC_KEY_SZ; res->app_status = BLE_HS_SM_US_ERR(BLE_SM_ERR_ENC_KEY_SZ); - } else if (req.max_enc_key_size > BLE_SM_PAIR_KEY_SZ_MAX) { + } else if (req->max_enc_key_size > BLE_SM_PAIR_KEY_SZ_MAX) { res->sm_err = BLE_SM_ERR_INVAL; res->app_status = BLE_HS_SM_US_ERR(BLE_SM_ERR_INVAL); } else { @@ -1521,27 +1545,30 @@ static void ble_sm_pair_rsp_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om, struct ble_sm_result *res) { - struct ble_sm_pair_cmd rsp; + struct ble_sm_pair_cmd *rsp; struct ble_sm_proc *proc; uint8_t ioact; - res->app_status = ble_hs_mbuf_pullup_base(om, BLE_SM_PAIR_CMD_SZ); + res->app_status = ble_hs_mbuf_pullup_base(om, sizeof(*rsp)); if (res->app_status != 0) { res->enc_cb = 1; return; } - ble_sm_pair_cmd_parse((*om)->om_data, (*om)->om_len, &rsp); - BLE_SM_LOG_CMD(0, "pair rsp", conn_handle, ble_sm_pair_cmd_log, &rsp); + rsp = (struct ble_sm_pair_cmd *)(*om)->om_data; + + BLE_SM_LOG_CMD(0, "pair rsp", conn_handle, ble_sm_pair_cmd_log, rsp); ble_hs_lock(); proc = ble_sm_proc_find(conn_handle, BLE_SM_PROC_STATE_PAIR, 1, NULL); if (proc != NULL) { - proc->pair_rsp = rsp; - if (rsp.max_enc_key_size < BLE_SM_PAIR_KEY_SZ_MIN) { + proc->pair_rsp[0] = BLE_SM_OP_PAIR_RSP; + memcpy(proc->pair_rsp + 1, rsp, sizeof(*rsp)); + + if (rsp->max_enc_key_size < BLE_SM_PAIR_KEY_SZ_MIN) { res->sm_err = BLE_SM_ERR_ENC_KEY_SZ; res->app_status = BLE_HS_SM_US_ERR(BLE_SM_ERR_ENC_KEY_SZ); - } else if (rsp.max_enc_key_size > BLE_SM_PAIR_KEY_SZ_MAX) { + } else if (rsp->max_enc_key_size > BLE_SM_PAIR_KEY_SZ_MAX) { res->sm_err = BLE_SM_ERR_INVAL; res->app_status = BLE_HS_SM_US_ERR(BLE_SM_ERR_INVAL); } else { @@ -2373,7 +2400,7 @@ ble_sm_rx(uint16_t handle, struct os_mbuf **om) return BLE_HS_ENOMEM; } - cmd = os_mbuf_extend(txom, BLE_SM_HDR_SZ + BLE_SM_PAIR_FAIL_SZ); + cmd = os_mbuf_extend(txom, sizeof(struct ble_sm_hdr) + BLE_SM_PAIR_FAIL_SZ); if (cmd == NULL) { os_mbuf_free_chain(txom); return BLE_HS_ENOMEM; http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cb4de66a/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 99fec05..9b8f5d4 100644 --- a/net/nimble/host/src/ble_sm_cmd.c +++ b/net/nimble/host/src/ble_sm_cmd.c @@ -27,12 +27,34 @@ #if NIMBLE_BLE_SM -static int +void * +ble_sm_cmd_get(uint8_t opcode, size_t len, struct os_mbuf **txom) +{ + struct ble_sm_hdr *hdr; + + *txom = ble_hs_mbuf_l2cap_pkt(); + if (*txom == NULL) { + return NULL; + } + + if (os_mbuf_extend(*txom, sizeof(*hdr) + len) == NULL) { + os_mbuf_free_chain(*txom); + return NULL; + } + + hdr = (struct ble_sm_hdr *)(*txom)->om_data; + + hdr->opcode = opcode; + + return hdr->data; +} + +/* this function consumes tx os_mbuf */ +int ble_sm_tx(uint16_t conn_handle, struct os_mbuf *txom) { struct ble_l2cap_chan *chan; struct ble_hs_conn *conn; - int rc; BLE_HS_DBG_ASSERT(ble_hs_locked_by_cur_task()); @@ -40,12 +62,7 @@ ble_sm_tx(uint16_t conn_handle, struct os_mbuf *txom) ble_hs_misc_conn_chan_find_reqd(conn_handle, BLE_L2CAP_CID_SM, &conn, &chan); - rc = ble_l2cap_tx(conn, chan, txom); - if (rc != 0) { - return rc; - } - - return 0; + return ble_l2cap_tx(conn, chan, txom); } static int @@ -60,7 +77,7 @@ ble_sm_init_req(uint16_t initial_sz, struct os_mbuf **out_txom) goto err; } - buf = os_mbuf_extend(*out_txom, BLE_SM_HDR_SZ + initial_sz); + buf = os_mbuf_extend(*out_txom, sizeof(struct ble_sm_hdr) + initial_sz); if (buf == NULL) { rc = BLE_HS_ENOMEM; goto err; @@ -79,7 +96,7 @@ ble_sm_pair_cmd_parse(void *payload, int len, struct ble_sm_pair_cmd *cmd) { uint8_t *u8ptr; - BLE_HS_DBG_ASSERT(len >= BLE_SM_PAIR_CMD_SZ); + BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_pair_cmd)); u8ptr = payload; cmd->io_cap = u8ptr[0]; @@ -96,7 +113,7 @@ ble_sm_pair_cmd_write(void *payload, int len, int is_req, { uint8_t *u8ptr; - BLE_HS_DBG_ASSERT(len >= BLE_SM_HDR_SZ + BLE_SM_PAIR_CMD_SZ); + BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_pair_cmd)); u8ptr = payload; u8ptr[0] = is_req ? BLE_SM_OP_PAIR_REQ : BLE_SM_OP_PAIR_RSP; @@ -115,7 +132,7 @@ ble_sm_pair_cmd_tx(uint16_t conn_handle, int is_req, struct os_mbuf *txom; int rc; - rc = ble_sm_init_req(BLE_SM_PAIR_CMD_SZ, &txom); + rc = ble_sm_init_req(sizeof(struct ble_sm_pair_cmd), &txom); if (rc != 0) { return BLE_HS_ENOMEM; } @@ -157,12 +174,12 @@ ble_sm_pair_confirm_write(void *payload, int len, { uint8_t *u8ptr; - BLE_HS_DBG_ASSERT(len >= BLE_SM_HDR_SZ + BLE_SM_PAIR_CONFIRM_SZ); + BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + BLE_SM_PAIR_CONFIRM_SZ); u8ptr = payload; u8ptr[0] = BLE_SM_OP_PAIR_CONFIRM; - memcpy(u8ptr + BLE_SM_HDR_SZ, cmd->value, sizeof cmd->value); + memcpy(u8ptr + sizeof(struct ble_sm_hdr), cmd->value, sizeof cmd->value); } int @@ -209,12 +226,12 @@ ble_sm_pair_random_write(void *payload, int len, uint8_t *u8ptr; BLE_HS_DBG_ASSERT(len >= - BLE_SM_HDR_SZ + BLE_SM_PAIR_RANDOM_SZ); + sizeof(struct ble_sm_hdr) + BLE_SM_PAIR_RANDOM_SZ); u8ptr = payload; u8ptr[0] = BLE_SM_OP_PAIR_RANDOM; - memcpy(u8ptr + BLE_SM_HDR_SZ, cmd->value, sizeof cmd->value); + memcpy(u8ptr + sizeof(struct ble_sm_hdr), cmd->value, sizeof cmd->value); } int @@ -262,7 +279,7 @@ ble_sm_pair_fail_write(void *payload, int len, struct ble_sm_pair_fail *cmd) { uint8_t *u8ptr; - BLE_HS_DBG_ASSERT(len >= BLE_SM_HDR_SZ + BLE_SM_PAIR_FAIL_SZ); + BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + BLE_SM_PAIR_FAIL_SZ); u8ptr = payload; @@ -315,7 +332,7 @@ ble_sm_enc_info_write(void *payload, int len, struct ble_sm_enc_info *cmd) { uint8_t *u8ptr; - BLE_HS_DBG_ASSERT(len >= BLE_SM_HDR_SZ + BLE_SM_ENC_INFO_SZ); + BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + BLE_SM_ENC_INFO_SZ); u8ptr = payload; @@ -369,7 +386,7 @@ ble_sm_master_id_write(void *payload, int len, struct ble_sm_master_id *cmd) { uint8_t *u8ptr; - BLE_HS_DBG_ASSERT(len >= BLE_SM_HDR_SZ + BLE_SM_MASTER_ID_SZ); + BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + BLE_SM_MASTER_ID_SZ); u8ptr = payload; @@ -424,12 +441,12 @@ ble_sm_id_info_write(void *payload, int len, struct ble_sm_id_info *cmd) { uint8_t *u8ptr; - BLE_HS_DBG_ASSERT(len >= BLE_SM_HDR_SZ + BLE_SM_ID_INFO_SZ); + BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + BLE_SM_ID_INFO_SZ); u8ptr = payload; u8ptr[0] = BLE_SM_OP_IDENTITY_INFO; - memcpy(u8ptr + BLE_SM_HDR_SZ, cmd->irk, sizeof cmd->irk); + memcpy(u8ptr + sizeof(struct ble_sm_hdr), cmd->irk, sizeof cmd->irk); } int @@ -477,7 +494,7 @@ ble_sm_id_addr_info_write(void *payload, int len, { uint8_t *u8ptr; - BLE_HS_DBG_ASSERT(len >= BLE_SM_HDR_SZ + BLE_SM_ID_ADDR_INFO_SZ); + BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + BLE_SM_ID_ADDR_INFO_SZ); u8ptr = payload; @@ -528,12 +545,12 @@ ble_sm_sign_info_write(void *payload, int len, struct ble_sm_sign_info *cmd) { uint8_t *u8ptr; - BLE_HS_DBG_ASSERT(len >= BLE_SM_HDR_SZ + BLE_SM_SIGN_INFO_SZ); + BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + BLE_SM_SIGN_INFO_SZ); u8ptr = payload; u8ptr[0] = BLE_SM_OP_SIGN_INFO; - memcpy(u8ptr + BLE_SM_HDR_SZ, cmd->sig_key, sizeof cmd->sig_key); + memcpy(u8ptr + sizeof(struct ble_sm_hdr), cmd->sig_key, sizeof cmd->sig_key); } int @@ -582,7 +599,7 @@ ble_sm_sec_req_write(void *payload, int len, struct ble_sm_sec_req *cmd) { uint8_t *u8ptr; - BLE_HS_DBG_ASSERT(len >= BLE_SM_HDR_SZ + BLE_SM_SEC_REQ_SZ); + BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + BLE_SM_SEC_REQ_SZ); u8ptr = payload; @@ -638,7 +655,7 @@ ble_sm_public_key_write(void *payload, int len, struct ble_sm_public_key *cmd) { uint8_t *u8ptr; - if (len < BLE_SM_HDR_SZ + BLE_SM_PUBLIC_KEY_SZ) { + if (len < sizeof(struct ble_sm_hdr) + BLE_SM_PUBLIC_KEY_SZ) { return BLE_HS_EMSGSIZE; } @@ -699,7 +716,7 @@ ble_sm_dhkey_check_write(void *payload, int len, { uint8_t *u8ptr; - if (len < BLE_SM_HDR_SZ + BLE_SM_DHKEY_CHECK_SZ) { + if (len < sizeof(struct ble_sm_hdr) + BLE_SM_DHKEY_CHECK_SZ) { return BLE_HS_EMSGSIZE; } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cb4de66a/net/nimble/host/src/ble_sm_lgcy.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/ble_sm_lgcy.c b/net/nimble/host/src/ble_sm_lgcy.c index 54a2ecb..d4734e5 100644 --- a/net/nimble/host/src/ble_sm_lgcy.c +++ b/net/nimble/host/src/ble_sm_lgcy.c @@ -63,24 +63,26 @@ static const uint8_t ble_sm_lgcy_resp_ioa[5 /*resp*/ ][5 /*init*/ ] = int ble_sm_lgcy_io_action(struct ble_sm_proc *proc) { + struct ble_sm_pair_cmd *pair_req, *pair_rsp; int action; - if (proc->pair_req.oob_data_flag == BLE_SM_PAIR_OOB_YES && - proc->pair_rsp.oob_data_flag == BLE_SM_PAIR_OOB_YES) { + pair_req = (struct ble_sm_pair_cmd *) &proc->pair_req[1]; + pair_rsp = (struct ble_sm_pair_cmd *) &proc->pair_rsp[1]; + + if (pair_req->oob_data_flag == BLE_SM_PAIR_OOB_YES && + pair_rsp->oob_data_flag == BLE_SM_PAIR_OOB_YES) { action = BLE_SM_IOACT_OOB; - } else if (!(proc->pair_req.authreq & BLE_SM_PAIR_AUTHREQ_MITM) && - !(proc->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_MITM)) { + } else if (!(pair_req->authreq & BLE_SM_PAIR_AUTHREQ_MITM) && + !(pair_rsp->authreq & BLE_SM_PAIR_AUTHREQ_MITM)) { action = BLE_SM_IOACT_NONE; - } else if (proc->pair_req.io_cap >= BLE_SM_IO_CAP_RESERVED || - proc->pair_rsp.io_cap >= BLE_SM_IO_CAP_RESERVED) { + } else if (pair_req->io_cap >= BLE_SM_IO_CAP_RESERVED || + pair_rsp->io_cap >= BLE_SM_IO_CAP_RESERVED) { action = BLE_SM_IOACT_NONE; } else if (proc->flags & BLE_SM_PROC_F_INITIATOR) { - action = ble_sm_lgcy_init_ioa[proc->pair_rsp.io_cap] - [proc->pair_req.io_cap]; + action = ble_sm_lgcy_init_ioa[pair_rsp->io_cap][pair_req->io_cap]; } else { - action = ble_sm_lgcy_resp_ioa[proc->pair_rsp.io_cap] - [proc->pair_req.io_cap]; + action = ble_sm_lgcy_resp_ioa[pair_rsp->io_cap][pair_req->io_cap]; } switch (action) { @@ -107,48 +109,20 @@ ble_sm_lgcy_io_action(struct ble_sm_proc *proc) return action; } -static int -ble_sm_lgcy_confirm_prepare_args(struct ble_sm_proc *proc, - uint8_t *k, uint8_t *preq, uint8_t *pres, - uint8_t *iat, uint8_t *rat, - uint8_t *ia, uint8_t *ra) -{ - ble_sm_ia_ra(proc, iat, ia, rat, ra); - - memcpy(k, proc->tk, sizeof proc->tk); - - ble_sm_pair_cmd_write( - preq, BLE_SM_HDR_SZ + BLE_SM_PAIR_CMD_SZ, 1, - &proc->pair_req); - - ble_sm_pair_cmd_write( - pres, BLE_SM_HDR_SZ + BLE_SM_PAIR_CMD_SZ, 0, - &proc->pair_rsp); - - return 0; -} - void ble_sm_lgcy_confirm_exec(struct ble_sm_proc *proc, struct ble_sm_result *res) { struct ble_sm_pair_confirm cmd; - uint8_t preq[BLE_SM_HDR_SZ + BLE_SM_PAIR_CMD_SZ]; - uint8_t pres[BLE_SM_HDR_SZ + BLE_SM_PAIR_CMD_SZ]; - uint8_t k[16]; uint8_t ia[6]; uint8_t ra[6]; uint8_t iat; uint8_t rat; int rc; - rc = ble_sm_lgcy_confirm_prepare_args(proc, k, preq, pres, - &iat, &rat, ia, ra); - if (rc != 0) { - goto err; - } + ble_sm_ia_ra(proc, &iat, ia, &rat, ra); - rc = ble_sm_alg_c1(k, ble_sm_our_pair_rand(proc), - preq, pres, iat, rat, ia, ra, cmd.value); + rc = ble_sm_alg_c1(proc->tk, ble_sm_our_pair_rand(proc), proc->pair_req, + proc->pair_rsp, iat, rat, ia, ra, cmd.value); if (rc != 0) { goto err; } @@ -213,27 +187,17 @@ ble_sm_lgcy_random_exec(struct ble_sm_proc *proc, struct ble_sm_result *res) void ble_sm_lgcy_random_rx(struct ble_sm_proc *proc, struct ble_sm_result *res) { - uint8_t preq[BLE_SM_HDR_SZ + BLE_SM_PAIR_CMD_SZ]; - uint8_t pres[BLE_SM_HDR_SZ + BLE_SM_PAIR_CMD_SZ]; uint8_t confirm_val[16]; - uint8_t k[16]; uint8_t ia[6]; uint8_t ra[6]; uint8_t iat; uint8_t rat; int rc; - rc = ble_sm_lgcy_confirm_prepare_args(proc, k, preq, pres, - &iat, &rat, ia, ra); - if (rc != 0) { - res->app_status = rc; - res->sm_err = BLE_SM_ERR_UNSPECIFIED; - res->enc_cb = 1; - return; - } + ble_sm_ia_ra(proc, &iat, ia, &rat, ra); - rc = ble_sm_alg_c1(k, ble_sm_peer_pair_rand(proc), preq, pres, - iat, rat, ia, ra, confirm_val); + rc = ble_sm_alg_c1(proc->tk, ble_sm_peer_pair_rand(proc), proc->pair_req, + proc->pair_rsp, iat, rat, ia, ra, confirm_val); if (rc != 0) { res->app_status = rc; res->sm_err = BLE_SM_ERR_UNSPECIFIED; http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cb4de66a/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 7ce154b..c022e14 100644 --- a/net/nimble/host/src/ble_sm_priv.h +++ b/net/nimble/host/src/ble_sm_priv.h @@ -35,8 +35,6 @@ struct hci_encrypt_change; #define BLE_SM_MTU 65 -#define BLE_SM_HDR_SZ 1 - #define BLE_SM_OP_PAIR_REQ 0x01 #define BLE_SM_OP_PAIR_RSP 0x02 #define BLE_SM_OP_PAIR_CONFIRM 0x03 @@ -52,6 +50,11 @@ struct hci_encrypt_change; #define BLE_SM_OP_PAIR_DHKEY_CHECK 0x0d #define BLE_SM_OP_PAIR_KEYPRESS_NOTIFY 0x0e +struct ble_sm_hdr { + uint8_t opcode; + uint8_t data[0]; +} __attribute__((packed)); + /** * | Parameter | Size (octets) | * +------------------------------------+-------------------+ @@ -63,7 +66,7 @@ struct hci_encrypt_change; * | Initiator Key Distribution | 1 | * | Responder Key Distribution | 1 | */ -#define BLE_SM_PAIR_CMD_SZ 6 + struct ble_sm_pair_cmd { uint8_t io_cap; uint8_t oob_data_flag; @@ -71,7 +74,7 @@ struct ble_sm_pair_cmd { uint8_t max_enc_key_size; uint8_t init_key_dist; uint8_t resp_key_dist; -}; +} __attribute__((packed)); /** * | Parameter | Size (octets) | @@ -260,8 +263,8 @@ struct ble_sm_proc { uint8_t rx_key_flags; uint8_t key_size; - struct ble_sm_pair_cmd pair_req; - struct ble_sm_pair_cmd pair_rsp; + uint8_t pair_req[sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_pair_cmd)]; + uint8_t pair_rsp[sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_pair_cmd)]; uint8_t tk[16]; uint8_t confirm_peer[16]; uint8_t randm[16]; @@ -301,6 +304,9 @@ void ble_sm_dbg_set_sc_keys(uint8_t *pubkey, uint8_t *privkey); int ble_sm_dbg_num_procs(void); #endif +void *ble_sm_cmd_get(uint8_t opcode, size_t len, struct os_mbuf **txom); +int ble_sm_tx(uint16_t conn_handle, struct os_mbuf *txom); + uint8_t ble_sm_build_authreq(void); void ble_sm_pair_cmd_parse(void *payload, int len, http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cb4de66a/net/nimble/host/src/ble_sm_sc.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/ble_sm_sc.c b/net/nimble/host/src/ble_sm_sc.c index 2ab3f8a..26564c8 100644 --- a/net/nimble/host/src/ble_sm_sc.c +++ b/net/nimble/host/src/ble_sm_sc.c @@ -110,24 +110,26 @@ ble_sm_dbg_set_sc_keys(uint8_t *pubkey, uint8_t *privkey) int ble_sm_sc_io_action(struct ble_sm_proc *proc) { + struct ble_sm_pair_cmd *pair_req, *pair_rsp; int action; - if (proc->pair_req.oob_data_flag == BLE_SM_PAIR_OOB_YES || - proc->pair_rsp.oob_data_flag == BLE_SM_PAIR_OOB_YES) { + pair_req = (struct ble_sm_pair_cmd *) &proc->pair_req[1]; + pair_rsp = (struct ble_sm_pair_cmd *) &proc->pair_rsp[1]; + + if (pair_req->oob_data_flag == BLE_SM_PAIR_OOB_YES || + pair_rsp->oob_data_flag == BLE_SM_PAIR_OOB_YES) { action = BLE_SM_IOACT_OOB; - } else if (!(proc->pair_req.authreq & BLE_SM_PAIR_AUTHREQ_MITM) && - !(proc->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_MITM)) { + } else if (!(pair_req->authreq & BLE_SM_PAIR_AUTHREQ_MITM) && + !(pair_rsp->authreq & BLE_SM_PAIR_AUTHREQ_MITM)) { action = BLE_SM_IOACT_NONE; - } else if (proc->pair_req.io_cap >= BLE_SM_IO_CAP_RESERVED || - proc->pair_rsp.io_cap >= BLE_SM_IO_CAP_RESERVED) { + } else if (pair_req->io_cap >= BLE_SM_IO_CAP_RESERVED || + pair_rsp->io_cap >= BLE_SM_IO_CAP_RESERVED) { action = BLE_SM_IOACT_NONE; } else if (proc->flags & BLE_SM_PROC_F_INITIATOR) { - action = ble_sm_sc_init_ioa[proc->pair_rsp.io_cap] - [proc->pair_req.io_cap]; + action = ble_sm_sc_init_ioa[pair_rsp->io_cap][pair_req->io_cap]; } else { - action = ble_sm_sc_resp_ioa[proc->pair_rsp.io_cap] - [proc->pair_req.io_cap]; + action = ble_sm_sc_resp_ioa[pair_rsp->io_cap][pair_req->io_cap]; } switch (action) { @@ -595,15 +597,6 @@ ble_sm_sc_dhkey_addrs(struct ble_sm_proc *proc, *out_peer_ota_addr = addrs.peer_ota_addr; } -static void -ble_sm_sc_dhkey_check_iocap(struct ble_sm_pair_cmd *pair_cmd, - uint8_t *out_iocap) -{ - out_iocap[0] = pair_cmd->io_cap; - out_iocap[1] = pair_cmd->oob_data_flag; - out_iocap[2] = pair_cmd->authreq; -} - void ble_sm_sc_dhkey_check_exec(struct ble_sm_proc *proc, struct ble_sm_result *res, void *arg) @@ -613,13 +606,19 @@ ble_sm_sc_dhkey_check_exec(struct ble_sm_proc *proc, struct ble_sm_result *res, const uint8_t *peer_ota_addr; uint8_t peer_id_addr_type; uint8_t our_id_addr_type; - uint8_t iocap[3]; + uint8_t *iocap; int rc; if (proc->flags & BLE_SM_PROC_F_INITIATOR) { - ble_sm_sc_dhkey_check_iocap(&proc->pair_req, iocap); + struct ble_sm_pair_cmd *pair_req; + + pair_req = (struct ble_sm_pair_cmd *) &proc->pair_req[1]; + iocap = &pair_req->io_cap; } else { - ble_sm_sc_dhkey_check_iocap(&proc->pair_rsp, iocap); + struct ble_sm_pair_cmd *pair_rsp; + + pair_rsp = (struct ble_sm_pair_cmd *) &proc->pair_rsp[1]; + iocap = &pair_rsp->io_cap; } ble_sm_sc_dhkey_addrs(proc, @@ -662,13 +661,19 @@ ble_sm_dhkey_check_process(struct ble_sm_proc *proc, const uint8_t *our_ota_addr; uint8_t peer_id_addr_type; uint8_t our_id_addr_type; - uint8_t iocap[3]; + uint8_t *iocap; uint8_t ioact; if (proc->flags & BLE_SM_PROC_F_INITIATOR) { - ble_sm_sc_dhkey_check_iocap(&proc->pair_rsp, iocap); + struct ble_sm_pair_cmd *pair_rsp; + + pair_rsp = (struct ble_sm_pair_cmd *) &proc->pair_rsp[1]; + iocap = &pair_rsp->io_cap; } else { - ble_sm_sc_dhkey_check_iocap(&proc->pair_req, iocap); + struct ble_sm_pair_cmd *pair_req; + + pair_req = (struct ble_sm_pair_cmd *) &proc->pair_req[1]; + iocap = &pair_req->io_cap; } ble_sm_sc_dhkey_addrs(proc, http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cb4de66a/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 b3783a0..f73fb4b 100644 --- a/net/nimble/host/test/src/ble_sm_test_util.c +++ b/net/nimble/host/test/src/ble_sm_test_util.c @@ -274,12 +274,12 @@ ble_sm_test_util_rx_pair_cmd(uint16_t conn_handle, uint8_t op, hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR( 2, BLE_HCI_PB_FIRST_FLUSH, - BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_PAIR_CMD_SZ); + BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_pair_cmd)); om = ble_hs_mbuf_l2cap_pkt(); TEST_ASSERT_FATAL(om != NULL); - payload_len = BLE_SM_HDR_SZ + BLE_SM_PAIR_CMD_SZ; + payload_len = sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_pair_cmd); v = os_mbuf_extend(om, payload_len); TEST_ASSERT_FATAL(v != NULL); @@ -321,12 +321,12 @@ ble_sm_test_util_rx_confirm(uint16_t conn_handle, hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR( 2, BLE_HCI_PB_FIRST_FLUSH, - BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_PAIR_CONFIRM_SZ); + BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + BLE_SM_PAIR_CONFIRM_SZ); om = ble_hs_mbuf_l2cap_pkt(); TEST_ASSERT_FATAL(om != NULL); - payload_len = BLE_SM_HDR_SZ + BLE_SM_PAIR_CONFIRM_SZ; + payload_len = sizeof(struct ble_sm_hdr) + BLE_SM_PAIR_CONFIRM_SZ; v = os_mbuf_extend(om, payload_len); TEST_ASSERT_FATAL(v != NULL); @@ -351,12 +351,12 @@ ble_sm_test_util_rx_random(uint16_t conn_handle, hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR( 2, BLE_HCI_PB_FIRST_FLUSH, - BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_PAIR_RANDOM_SZ); + BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + BLE_SM_PAIR_RANDOM_SZ); om = ble_hs_mbuf_l2cap_pkt(); TEST_ASSERT_FATAL(om != NULL); - payload_len = BLE_SM_HDR_SZ + BLE_SM_PAIR_RANDOM_SZ; + payload_len = sizeof(struct ble_sm_hdr) + BLE_SM_PAIR_RANDOM_SZ; v = os_mbuf_extend(om, payload_len); TEST_ASSERT_FATAL(v != NULL); @@ -380,12 +380,12 @@ ble_sm_test_util_rx_sec_req(uint16_t conn_handle, struct ble_sm_sec_req *cmd, hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR( 2, BLE_HCI_PB_FIRST_FLUSH, - BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_SEC_REQ_SZ); + BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + BLE_SM_SEC_REQ_SZ); om = ble_hs_mbuf_l2cap_pkt(); TEST_ASSERT_FATAL(om != NULL); - payload_len = BLE_SM_HDR_SZ + BLE_SM_SEC_REQ_SZ; + payload_len = sizeof(struct ble_sm_hdr) + BLE_SM_SEC_REQ_SZ; v = os_mbuf_extend(om, payload_len); TEST_ASSERT_FATAL(v != NULL); @@ -409,12 +409,12 @@ ble_sm_test_util_rx_public_key(uint16_t conn_handle, hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR( 2, BLE_HCI_PB_FIRST_FLUSH, - BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_PUBLIC_KEY_SZ); + BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + BLE_SM_PUBLIC_KEY_SZ); om = ble_hs_mbuf_l2cap_pkt(); TEST_ASSERT_FATAL(om != NULL); - payload_len = BLE_SM_HDR_SZ + BLE_SM_PUBLIC_KEY_SZ; + payload_len = sizeof(struct ble_sm_hdr) + BLE_SM_PUBLIC_KEY_SZ; v = os_mbuf_extend(om, payload_len); TEST_ASSERT_FATAL(v != NULL); @@ -439,12 +439,12 @@ ble_sm_test_util_rx_dhkey_check(uint16_t conn_handle, hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR( 2, BLE_HCI_PB_FIRST_FLUSH, - BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_DHKEY_CHECK_SZ); + BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + BLE_SM_DHKEY_CHECK_SZ); om = ble_hs_mbuf_l2cap_pkt(); TEST_ASSERT_FATAL(om != NULL); - payload_len = BLE_SM_HDR_SZ + BLE_SM_DHKEY_CHECK_SZ; + payload_len = sizeof(struct ble_sm_hdr) + BLE_SM_DHKEY_CHECK_SZ; v = os_mbuf_extend(om, payload_len); TEST_ASSERT_FATAL(v != NULL); @@ -469,12 +469,12 @@ ble_sm_test_util_rx_enc_info(uint16_t conn_handle, hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR( 2, BLE_HCI_PB_FIRST_FLUSH, - BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_ENC_INFO_SZ); + BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + BLE_SM_ENC_INFO_SZ); om = ble_hs_mbuf_l2cap_pkt(); TEST_ASSERT_FATAL(om != NULL); - payload_len = BLE_SM_HDR_SZ + BLE_SM_ENC_INFO_SZ; + payload_len = sizeof(struct ble_sm_hdr) + BLE_SM_ENC_INFO_SZ; v = os_mbuf_extend(om, payload_len); TEST_ASSERT_FATAL(v != NULL); @@ -499,12 +499,12 @@ ble_sm_test_util_rx_master_id(uint16_t conn_handle, hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR( 2, BLE_HCI_PB_FIRST_FLUSH, - BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_MASTER_ID_SZ); + BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + BLE_SM_MASTER_ID_SZ); om = ble_hs_mbuf_l2cap_pkt(); TEST_ASSERT_FATAL(om != NULL); - payload_len = BLE_SM_HDR_SZ + BLE_SM_MASTER_ID_SZ; + payload_len = sizeof(struct ble_sm_hdr) + BLE_SM_MASTER_ID_SZ; v = os_mbuf_extend(om, payload_len); TEST_ASSERT_FATAL(v != NULL); @@ -529,12 +529,12 @@ ble_sm_test_util_rx_id_info(uint16_t conn_handle, hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR( 2, BLE_HCI_PB_FIRST_FLUSH, - BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_ID_INFO_SZ); + BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + BLE_SM_ID_INFO_SZ); om = ble_hs_mbuf_l2cap_pkt(); TEST_ASSERT_FATAL(om != NULL); - payload_len = BLE_SM_HDR_SZ + BLE_SM_ID_INFO_SZ; + payload_len = sizeof(struct ble_sm_hdr) + BLE_SM_ID_INFO_SZ; v = os_mbuf_extend(om, payload_len); TEST_ASSERT_FATAL(v != NULL); @@ -559,12 +559,12 @@ ble_sm_test_util_rx_id_addr_info(uint16_t conn_handle, hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR( 2, BLE_HCI_PB_FIRST_FLUSH, - BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_ID_ADDR_INFO_SZ); + BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + BLE_SM_ID_ADDR_INFO_SZ); om = ble_hs_mbuf_l2cap_pkt(); TEST_ASSERT_FATAL(om != NULL); - payload_len = BLE_SM_HDR_SZ + BLE_SM_ID_ADDR_INFO_SZ; + payload_len = sizeof(struct ble_sm_hdr) + BLE_SM_ID_ADDR_INFO_SZ; v = os_mbuf_extend(om, payload_len); TEST_ASSERT_FATAL(v != NULL); @@ -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 + BLE_SM_HDR_SZ + BLE_SM_SIGN_INFO_SZ); + BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + BLE_SM_SIGN_INFO_SZ); om = ble_hs_mbuf_l2cap_pkt(); TEST_ASSERT_FATAL(om != NULL); - payload_len = BLE_SM_HDR_SZ + BLE_SM_SIGN_INFO_SZ; + payload_len = sizeof(struct ble_sm_hdr) + BLE_SM_SIGN_INFO_SZ; v = os_mbuf_extend(om, payload_len); TEST_ASSERT_FATAL(v != NULL); @@ -614,11 +614,11 @@ ble_sm_test_util_verify_tx_hdr(uint8_t sm_op, uint16_t payload_len) om = ble_hs_test_util_prev_tx_dequeue_pullup(); TEST_ASSERT_FATAL(om != NULL); - TEST_ASSERT(OS_MBUF_PKTLEN(om) == BLE_SM_HDR_SZ + payload_len); + TEST_ASSERT(OS_MBUF_PKTLEN(om) == sizeof(struct ble_sm_hdr) + payload_len); TEST_ASSERT_FATAL(om->om_data[0] == sm_op); - om->om_data += BLE_SM_HDR_SZ; - om->om_len -= BLE_SM_HDR_SZ; + om->om_data += sizeof(struct ble_sm_hdr); + om->om_len -= sizeof(struct ble_sm_hdr); return om; } @@ -631,7 +631,7 @@ ble_sm_test_util_verify_tx_pair_cmd( struct ble_sm_pair_cmd cmd; struct os_mbuf *om; - om = ble_sm_test_util_verify_tx_hdr(op, BLE_SM_PAIR_CMD_SZ); + om = ble_sm_test_util_verify_tx_hdr(op, sizeof(struct ble_sm_pair_cmd)); ble_sm_pair_cmd_parse(om->om_data, om->om_len, &cmd); TEST_ASSERT(cmd.io_cap == exp_cmd->io_cap);
