mesh: split prov.c into two separate modules

Currently all provisioning procedure into common source
files call `prov.c`, that will not compile separately.

Add `BT_MESH_NODE` to control whether nodes are supported
and device provisioning is supported, this will be used in
provisioner role.

Add more provisioner OOB authentication method.

this is port of 5374245dd66e630932267663546d5f1108aff9b5 with patch
0bcd66ac36d8c02d2c3b3851c4086e8b692578a6
This commit is contained in:
Krzysztof Kopyściński
2021-01-12 14:05:19 +01:00
committed by Łukasz Rymanowski
parent 0b3db9a0df
commit 044f08b117
15 changed files with 1754 additions and 1081 deletions
+1
View File
@@ -402,6 +402,7 @@ static inline unsigned int find_msb_set(uint32_t op)
#define CONFIG_BT_SETTINGS BLE_MESH_SETTINGS
#define CONFIG_SETTINGS BLE_MESH_SETTINGS
#define CONFIG_BT_MESH_PROVISIONER BLE_MESH_PROVISIONER
#define CONFIG_BT_MESH_PROV_DEVICE BLE_MESH_PROV_DEVICE
/* Above flags are used with IS_ENABLED macro */
#define IS_ENABLED(config) MYNEWT_VAL(config)
+125
View File
@@ -59,6 +59,33 @@ typedef enum {
BT_MESH_PROV_OOB_ON_DEV = BIT(15),
} bt_mesh_prov_oob_info_t;
/** Device Capabilities. */
struct bt_mesh_dev_capabilities {
/** Number of elements supported by the device */
uint8_t elem_count;
/** Supported algorithms and other capabilities */
uint16_t algorithms;
/** Supported public key types */
uint8_t pub_key_type;
/** Supported static OOB Types */
uint8_t static_oob;
/** Supported Output OOB Actions */
bt_mesh_output_action_t output_actions;
/** Supported Input OOB Actions */
bt_mesh_input_action_t input_actions;
/** Maximum size of Output OOB supported */
uint8_t output_size;
/** Maximum size in octets of Input OOB supported */
uint8_t input_size;
};
/** Provisioning properties & capabilities. */
struct bt_mesh_prov {
/** The UUID that's used when advertising as unprovisioned */
@@ -89,6 +116,21 @@ struct bt_mesh_prov {
/** Supported Input OOB Actions */
uint16_t input_actions;
/** @brief Provisioning Capabilities.
*
* This callback notifies the application that the provisioning capabilities
* of the unprovisioned device has been received.
*
* The application can consequently call bt_mesh_auth_method_set_<*> to
* select suitable provisioning oob authentication method.
*
* When this callback returns, the provisioner will start authentication with
* the chosen method.
*
* @param cap capabilities supported by device.
*/
void (*capabilities)(const struct bt_mesh_dev_capabilities *cap);
/** @brief Output of a number is requested.
*
* This callback notifies the application that it should
@@ -226,6 +268,89 @@ int bt_mesh_input_string(const char *str);
*/
int bt_mesh_input_number(uint32_t num);
/** @brief Provide Device public key.
*
* @param public_key Device public key.
*
* @return Zero on success or (negative) error code otherwise.
*/
int bt_mesh_prov_remote_pub_key_set(const uint8_t public_key[64]);
/** @brief Use Input OOB authentication.
*
* Provisioner only.
*
* Instruct the unprovisioned device to use the specified Input OOB
* authentication action. When using @ref BT_MESH_PUSH, @ref BT_MESH_TWIST or
* @ref BT_MESH_ENTER_NUMBER, the @ref bt_mesh_prov::output_number callback is
* called with a random number that has to be entered on the unprovisioned
* device.
*
* When using @ref BT_MESH_ENTER_STRING, the @ref bt_mesh_prov::output_string
* callback is called with a random string that has to be entered on the
* unprovisioned device.
*
* @param action Authentication action used by the unprovisioned device.
* @param size Authentication size.
*
* @return Zero on success or (negative) error code otherwise.
*/
int bt_mesh_auth_method_set_input(bt_mesh_input_action_t action, uint8_t size);
/** @brief Use Output OOB authentication.
*
* Provisioner only.
*
* Instruct the unprovisioned device to use the specified Output OOB
* authentication action. The @ref bt_mesh_prov::input callback will
* be called.
*
* When using @ref BT_MESH_BLINK, @ref BT_MESH_BEEP, @ref BT_MESH_VIBRATE
* or @ref BT_MESH_DISPLAY_NUMBER, and the application has to call
* @ref bt_mesh_input_number with the random number indicated by
* the unprovisioned device.
*
* When using @ref BT_MESH_DISPLAY_STRING, the application has to call
* @ref bt_mesh_input_string with the random string displayed by the
* unprovisioned device.
*
* @param action Authentication action used by the unprovisioned device.
* @param size Authentication size.
*
* @return Zero on success or (negative) error code otherwise.
*/
int bt_mesh_auth_method_set_output(bt_mesh_output_action_t action, uint8_t size);
/** @brief Use static OOB authentication.
*
* Provisioner only.
*
* Instruct the unprovisioned device to use static OOB authentication, and use
* the given static authentication value when provisioning.
*
* @param static_val Static OOB value.
* @param size Static OOB value size.
*
* @return Zero on success or (negative) error code otherwise.
*/
int bt_mesh_auth_method_set_static(const uint8_t *static_val, uint8_t size);
/** @brief Don't use OOB authentication.
*
* Provisioner only.
*
* Don't use any authentication when provisioning new devices. This is the
* default behavior.
*
* @warning Not using any authentication exposes the mesh network to
* impersonation attacks, where attackers can pretend to be the
* unprovisioned device to gain access to the network. Authentication
* is strongly encouraged.
*
* @return Zero on success or (negative) error code otherwise.
*/
int bt_mesh_auth_method_set_none(void);
/** @brief Enable specific provisioning bearers
*
* Enable one or more provisioning bearers.
+1 -1
View File
@@ -229,7 +229,7 @@ static void update_beacon_observation(void)
static void beacon_send(struct ble_npl_event *work)
{
/* Don't send anything if we have an active provisioning link */
if ((MYNEWT_VAL(BLE_MESH_PROV)) && bt_prov_active()) {
if (IS_ENABLED(CONFIG_BT_MESH_PB_ADV) && bt_mesh_prov_active()) {
k_delayed_work_submit(&beacon_timer,
K_SECONDS(MYNEWT_VAL(BLE_MESH_UNPROV_BEACON_INT)));
return;
+1
View File
@@ -18,6 +18,7 @@
#include "adv.h"
#include "prov.h"
#include "provisioner.h"
#include "net.h"
#include "app_keys.h"
#include "rpl.h"
+34 -34
View File
@@ -16,7 +16,7 @@
#include "adv.h"
#include "crypto.h"
#include "beacon.h"
#include "prov_bearer.h"
#include "prov.h"
#include "mesh/glue.h"
#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_MESH_DEBUG_PROV)
@@ -61,20 +61,20 @@
#define RETRANSMITS_ACK 2
enum {
LINK_ACTIVE, /* Link has been opened */
LINK_ACK_RECVD, /* Ack for link has been received */
LINK_CLOSING, /* Link is closing down */
LINK_INVALID, /* Error occurred during provisioning */
ACK_PENDING, /* An acknowledgment is being sent */
PROVISIONER, /* The link was opened as provisioner */
ADV_LINK_ACTIVE, /* Link has been opened */
ADV_LINK_ACK_RECVD, /* Ack for link has been received */
ADV_LINK_CLOSING, /* Link is closing down */
ADV_LINK_INVALID, /* Error occurred during provisioning */
ADV_ACK_PENDING, /* An acknowledgment is being sent */
ADV_PROVISIONER, /* The link was opened as provisioner */
NUM_FLAGS,
ADV_NUM_FLAGS,
};
struct pb_adv {
uint32_t id; /* Link ID */
ATOMIC_DEFINE(flags, NUM_FLAGS);
ATOMIC_DEFINE(flags, ADV_NUM_FLAGS);
const struct prov_bearer_cb *cb;
void *cb_data;
@@ -190,7 +190,7 @@ static void reset_adv_link(void)
k_delayed_work_cancel(&link.prot_timer);
if (atomic_test_bit(link.flags, PROVISIONER)) {
if (atomic_test_bit(link.flags, ADV_PROVISIONER)) {
/* Clear everything except the retransmit and protocol timer
* delayed work objects.
*/
@@ -235,19 +235,19 @@ static struct os_mbuf *adv_buf_create(uint8_t retransmits)
static void ack_complete(uint16_t duration, int err, void *user_data)
{
BT_DBG("xact 0x%x complete", (uint8_t)link.tx.pending_ack);
atomic_clear_bit(link.flags, ACK_PENDING);
atomic_clear_bit(link.flags, ADV_ACK_PENDING);
}
static bool ack_pending(void)
{
return atomic_test_bit(link.flags, ACK_PENDING);
return atomic_test_bit(link.flags, ADV_ACK_PENDING);
}
static void prov_failed(uint8_t err)
{
BT_DBG("%u", err);
link.cb->error(&pb_adv, link.cb_data, err);
atomic_set_bit(link.flags, LINK_INVALID);
atomic_set_bit(link.flags, ADV_LINK_INVALID);
}
static void prov_msg_recv(void)
@@ -261,7 +261,7 @@ static void prov_msg_recv(void)
gen_prov_ack_send(link.rx.id);
if (atomic_test_bit(link.flags, LINK_INVALID)) {
if (atomic_test_bit(link.flags, ADV_LINK_INVALID)) {
BT_WARN("Unexpected msg 0x%02x on invalidated link",
link.rx.buf->om_data[0]);
prov_failed(PROV_ERR_UNEXP_PDU);
@@ -289,7 +289,7 @@ static void gen_prov_ack_send(uint8_t xact_id)
};
const struct bt_mesh_send_cb *complete;
struct os_mbuf *buf;
bool pending = atomic_test_and_set_bit(link.flags, ACK_PENDING);
bool pending = atomic_test_and_set_bit(link.flags, ADV_ACK_PENDING);
BT_DBG("xact_id 0x%x", xact_id);
@@ -300,7 +300,7 @@ static void gen_prov_ack_send(uint8_t xact_id)
buf = adv_buf_create(RETRANSMITS_ACK);
if (!buf) {
atomic_clear_bit(link.flags, ACK_PENDING);
atomic_clear_bit(link.flags, ADV_ACK_PENDING);
return;
}
@@ -392,8 +392,8 @@ static void gen_prov_ack(struct prov_rx *rx, struct os_mbuf *buf)
}
if (rx->xact_id == link.tx.id) {
/* Don't clear resending of LINK_CLOSE messages */
if (!atomic_test_bit(link.flags, LINK_CLOSING)) {
/* Don't clear resending of link_close messages */
if (!atomic_test_bit(link.flags, ADV_LINK_CLOSING)) {
prov_clear_tx();
}
@@ -479,14 +479,14 @@ static void gen_prov_ctl(struct prov_rx *rx, struct os_mbuf *buf)
link_open(rx, buf);
break;
case LINK_ACK:
if (!atomic_test_bit(link.flags, LINK_ACTIVE)) {
if (!atomic_test_bit(link.flags, ADV_LINK_ACTIVE)) {
return;
}
link_ack(rx, buf);
break;
case LINK_CLOSE:
if (!atomic_test_bit(link.flags, LINK_ACTIVE)) {
if (!atomic_test_bit(link.flags, ADV_LINK_ACTIVE)) {
return;
}
@@ -521,7 +521,7 @@ static void gen_prov_recv(struct prov_rx *rx, struct os_mbuf *buf)
return;
}
if (!atomic_test_bit(link.flags, LINK_ACTIVE) &&
if (!atomic_test_bit(link.flags, ADV_LINK_ACTIVE) &&
gen_prov[GPCF(rx->gpc)].require_link) {
BT_DBG("Ignoring message that requires active link");
return;
@@ -561,24 +561,24 @@ static void prov_retransmit(struct ble_npl_event *work)
BT_DBG("");
if (!atomic_test_bit(link.flags, LINK_ACTIVE)) {
if (!atomic_test_bit(link.flags, ADV_LINK_ACTIVE)) {
BT_WARN("Link not active");
return;
}
/*
* According to mesh profile spec (5.3.1.4.3), the close message should
* be restransmitted at least three times. Retransmit the LINK_CLOSE
* be restransmitted at least three times. Retransmit the link_close
* message until CLOSING_TIMEOUT has elapsed.
*/
if (atomic_test_bit(link.flags, LINK_CLOSING)) {
if (atomic_test_bit(link.flags, ADV_LINK_CLOSING)) {
timeout = CLOSING_TIMEOUT;
} else {
timeout = TRANSACTION_TIMEOUT;
}
if (k_uptime_get() - link.tx.start > timeout) {
if (atomic_test_bit(link.flags, LINK_CLOSING)) {
if (atomic_test_bit(link.flags, ADV_LINK_CLOSING)) {
close_link(PROV_BEARER_LINK_STATUS_SUCCESS);
} else {
BT_WARN("Giving up transaction");
@@ -721,7 +721,7 @@ static void link_open(struct prov_rx *rx, struct os_mbuf *buf)
return;
}
if (atomic_test_bit(link.flags, LINK_ACTIVE)) {
if (atomic_test_bit(link.flags, ADV_LINK_ACTIVE)) {
/* Send another link ack if the provisioner missed the last */
if (link.id == rx->link_id && link.tx.id == 0x7F) {
BT_DBG("Resending link ack");
@@ -739,7 +739,7 @@ static void link_open(struct prov_rx *rx, struct os_mbuf *buf)
}
link.id = rx->link_id;
atomic_set_bit(link.flags, LINK_ACTIVE);
atomic_set_bit(link.flags, ADV_LINK_ACTIVE);
os_mbuf_reset(link.rx.buf);
bearer_ctl_send(LINK_ACK, NULL, 0, false);
@@ -751,8 +751,8 @@ static void link_ack(struct prov_rx *rx, struct os_mbuf *buf)
{
BT_DBG("len %u", buf->om_len);
if (atomic_test_bit(link.flags, PROVISIONER)) {
if (atomic_test_and_set_bit(link.flags, LINK_ACK_RECVD)) {
if (atomic_test_bit(link.flags, ADV_PROVISIONER)) {
if (atomic_test_and_set_bit(link.flags, ADV_LINK_ACK_RECVD)) {
return;
}
@@ -794,7 +794,7 @@ void bt_mesh_pb_adv_recv(struct os_mbuf *buf)
rx.xact_id = net_buf_simple_pull_u8(buf);
rx.gpc = net_buf_simple_pull_u8(buf);
if (atomic_test_bit(link.flags, LINK_ACTIVE) && link.id != rx.link_id) {
if (atomic_test_bit(link.flags, ADV_LINK_ACTIVE) && link.id != rx.link_id) {
return;
}
@@ -808,11 +808,11 @@ static int prov_link_open(const uint8_t uuid[16], int32_t timeout,
{
BT_DBG("uuid %s", bt_hex(uuid, 16));
if (atomic_test_and_set_bit(link.flags, LINK_ACTIVE)) {
if (atomic_test_and_set_bit(link.flags, ADV_LINK_ACTIVE)) {
return -EBUSY;
}
atomic_set_bit(link.flags, PROVISIONER);
atomic_set_bit(link.flags, ADV_PROVISIONER);
bt_rand(&link.id, sizeof(link.id));
link.tx.id = XACT_ID_MAX;
@@ -829,7 +829,7 @@ static int prov_link_open(const uint8_t uuid[16], int32_t timeout,
static int prov_link_accept(const struct prov_bearer_cb *cb, void *cb_data)
{
if (atomic_test_bit(link.flags, LINK_ACTIVE)) {
if (atomic_test_bit(link.flags, ADV_LINK_ACTIVE)) {
return -EBUSY;
}
@@ -848,7 +848,7 @@ static int prov_link_accept(const struct prov_bearer_cb *cb, void *cb_data)
static void prov_link_close(enum prov_bearer_link_status status)
{
if (atomic_test_and_set_bit(link.flags, LINK_CLOSING)) {
if (atomic_test_and_set_bit(link.flags, ADV_LINK_CLOSING)) {
return;
}
+5 -1
View File
@@ -11,7 +11,7 @@
#include "net.h"
#include "proxy.h"
#include "adv.h"
#include "prov_bearer.h"
#include "prov.h"
#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_MESH_DEBUG_PROV)
#define LOG_MODULE_NAME bt_mesh_pb_gatt
@@ -148,6 +148,10 @@ void pb_gatt_init(void)
k_delayed_work_init(&link.prot_timer, protocol_timeout);
}
void pb_gatt_reset(void)
{
reset_state();
}
const struct prov_bearer pb_gatt = {
.type = BT_MESH_PROV_GATT,
.link_accept = link_accept,
+109 -1034
View File
File diff suppressed because it is too large Load Diff
+114 -7
View File
@@ -9,6 +9,7 @@
#ifndef __PROV_H__
#define __PROV_H__
#include "prov_bearer.h"
#include "os/os_mbuf.h"
#include "mesh/mesh.h"
#include "../src/ble_hs_conn_priv.h"
@@ -23,12 +24,116 @@
#define PROV_ERR_UNEXP_ERR 0x07
#define PROV_ERR_ADDR 0x08
int bt_mesh_pb_adv_open(const uint8_t uuid[16], uint16_t net_idx, uint16_t addr,
uint8_t attention_duration);
#define AUTH_METHOD_NO_OOB 0x00
#define AUTH_METHOD_STATIC 0x01
#define AUTH_METHOD_OUTPUT 0x02
#define AUTH_METHOD_INPUT 0x03
void bt_mesh_pb_adv_recv(struct os_mbuf *buf);
#define OUTPUT_OOB_BLINK 0x00
#define OUTPUT_OOB_BEEP 0x01
#define OUTPUT_OOB_VIBRATE 0x02
#define OUTPUT_OOB_NUMBER 0x03
#define OUTPUT_OOB_STRING 0x04
bool bt_prov_active(void);
#define INPUT_OOB_PUSH 0x00
#define INPUT_OOB_TWIST 0x01
#define INPUT_OOB_NUMBER 0x02
#define INPUT_OOB_STRING 0x03
#define PUB_KEY_NO_OOB 0x00
#define PUB_KEY_OOB 0x01
#define PROV_INVITE 0x00
#define PROV_CAPABILITIES 0x01
#define PROV_START 0x02
#define PROV_PUB_KEY 0x03
#define PROV_INPUT_COMPLETE 0x04
#define PROV_CONFIRM 0x05
#define PROV_RANDOM 0x06
#define PROV_DATA 0x07
#define PROV_COMPLETE 0x08
#define PROV_FAILED 0x09
#define PROV_NO_PDU 0xff
#define PROV_ALG_P256 0x00
#define PROV_BUF(len) \
NET_BUF_SIMPLE(PROV_BEARER_BUF_HEADROOM + len)
enum {
WAIT_PUB_KEY, /* Waiting for local PubKey to be generated */
LINK_ACTIVE, /* Link has been opened */
WAIT_NUMBER, /* Waiting for number input from user */
WAIT_STRING, /* Waiting for string input from user */
NOTIFY_INPUT_COMPLETE, /* Notify that input has been completed. */
PROVISIONER, /* The link was opened as provisioner */
OOB_PUB_KEY, /* OOB Public key used */
PUB_KEY_SENT, /* Public key has been sent */
REMOTE_PUB_KEY, /* Remote key has been received */
INPUT_COMPLETE, /* Device input completed */
WAIT_CONFIRM, /* Wait for send confirm */
WAIT_AUTH, /* Wait for auth response */
OOB_STATIC_KEY, /* OOB Static Authentication */
NUM_FLAGS,
};
/** Provisioning role */
struct bt_mesh_prov_role {
void (*link_opened)(void);
void (*link_closed)(void);
void (*error)(uint8_t reason);
void (*input_complete)(void);
void (*op[10])(const uint8_t *data);
};
struct bt_mesh_prov_link {
uint8_t oob_method; /* Authen method */
uint8_t oob_action; /* Authen action */
uint8_t oob_size; /* Authen size */
uint8_t auth[16]; /* Authen value */
uint8_t dhkey[32]; /* Calculated DHKey */
uint8_t expect; /* Next expected PDU */
uint8_t conf[16]; /* Remote Confirmation */
uint8_t rand[16]; /* Local Random */
uint8_t conf_salt[16]; /* ConfirmationSalt */
uint8_t conf_key[16]; /* ConfirmationKey */
uint8_t conf_inputs[145]; /* ConfirmationInputs */
uint8_t prov_salt[16]; /* Provisioning Salt */
const struct prov_bearer *bearer;
const struct bt_mesh_prov_role *role;
ATOMIC_DEFINE(flags, NUM_FLAGS);
};
extern struct bt_mesh_prov_link bt_mesh_prov_link;
extern const struct bt_mesh_prov *bt_mesh_prov;
static inline int bt_mesh_prov_send(struct os_mbuf *buf,
prov_bearer_send_complete_t cb)
{
return bt_mesh_prov_link.bearer->send(buf, cb, NULL);
}
static inline void bt_mesh_prov_buf_init(struct os_mbuf *buf, uint8_t type)
{
net_buf_reserve(buf, PROV_BEARER_BUF_HEADROOM);
net_buf_simple_add_u8(buf, type);
}
int bt_mesh_prov_reset_state(void (*func)(const uint8_t key[64]));
bool bt_mesh_prov_active(void);
int bt_mesh_prov_auth(uint8_t method, uint8_t action, uint8_t size);
int bt_mesh_pb_gatt_open(uint16_t conn_handle);
int bt_mesh_pb_gatt_close(uint16_t conn_handle);
@@ -36,12 +141,14 @@ int bt_mesh_pb_gatt_recv(uint16_t conn_handle, struct os_mbuf *buf);
const struct bt_mesh_prov *bt_mesh_prov_get(void);
int bt_mesh_prov_init(const struct bt_mesh_prov *prov);
void bt_mesh_prov_reset_link(void);
void bt_mesh_prov_complete(uint16_t net_idx, uint16_t addr);
void bt_mesh_prov_node_added(uint16_t net_idx, uint16_t addr, uint8_t num_elem);
void bt_mesh_prov_reset(void);
const struct prov_bearer_cb *bt_mesh_prov_bearer_cb_get(void);
void bt_mesh_pb_adv_recv(struct os_mbuf *buf);
int bt_mesh_prov_init(const struct bt_mesh_prov *prov);
#endif
+4 -1
View File
@@ -110,4 +110,7 @@ extern const struct prov_bearer pb_adv;
extern const struct prov_bearer pb_gatt;
void pb_adv_init(void);
void pb_gatt_init(void);
void pb_gatt_init(void);
void pb_adv_reset(void);
void pb_gatt_reset(void);
+566
View File
@@ -0,0 +1,566 @@
/* Bluetooth Mesh */
/*
* Copyright (c) 2017 Intel Corporation
* Copyright (c) 2020 Lingao Meng
*
* SPDX-License-Identifier: Apache-2.0
*/
#define MESH_LOG_MODULE BLE_MESH_PROV_DEVICE_LOG
#include "testing.h"
#include "crypto.h"
#include "adv.h"
#include "mesh/mesh.h"
#include "net.h"
#include "rpl.h"
#include "beacon.h"
#include "access.h"
#include "foundation.h"
#include "proxy.h"
#include "prov.h"
#include "settings.h"
static void send_pub_key(void);
static void pub_key_ready(const uint8_t *pkey);
static int reset_state(void)
{
return bt_mesh_prov_reset_state(pub_key_ready);
}
static void prov_send_fail_msg(uint8_t err)
{
struct os_mbuf *buf = PROV_BUF(2);
BT_DBG("%u", err);
bt_mesh_prov_link.expect = PROV_NO_PDU;
bt_mesh_prov_buf_init(buf, PROV_FAILED);
net_buf_simple_add_u8(buf, err);
if (bt_mesh_prov_send(buf, NULL)) {
BT_ERR("Failed to send Provisioning Failed message");
}
}
static void prov_fail(uint8_t reason)
{
/* According to Bluetooth Mesh Specification v1.0.1, Section 5.4.4, the
* provisioner just closes the link when something fails, while the
* provisionee sends the fail message, and waits for the provisioner to
* close the link.
*/
prov_send_fail_msg(reason);
}
static void prov_invite(const uint8_t *data)
{
struct os_mbuf *buf = PROV_BUF(12);
BT_DBG("Attention Duration: %u seconds", data[0]);
if (data[0]) {
bt_mesh_attention(NULL, data[0]);
}
bt_mesh_prov_link.conf_inputs[0] = data[0];
bt_mesh_prov_buf_init(buf, PROV_CAPABILITIES);
/* Number of Elements supported */
net_buf_simple_add_u8(buf, bt_mesh_elem_count());
/* Supported algorithms - FIPS P-256 Eliptic Curve */
net_buf_simple_add_be16(buf, BIT(PROV_ALG_P256));
/* Public Key Type, Only "No OOB" Public Key is supported */
net_buf_simple_add_u8(buf, PUB_KEY_NO_OOB);
/* Static OOB Type */
net_buf_simple_add_u8(buf, bt_mesh_prov->static_val ? BIT(0) : 0x00);
/* Output OOB Size */
net_buf_simple_add_u8(buf, bt_mesh_prov->output_size);
/* Output OOB Action */
net_buf_simple_add_be16(buf, bt_mesh_prov->output_actions);
/* Input OOB Size */
net_buf_simple_add_u8(buf, bt_mesh_prov->input_size);
/* Input OOB Action */
net_buf_simple_add_be16(buf, bt_mesh_prov->input_actions);
memcpy(&bt_mesh_prov_link.conf_inputs[1], &buf->om_databuf[1], 11);
if (bt_mesh_prov_send(buf, NULL)) {
BT_ERR("Failed to send capabilities");
return;
}
bt_mesh_prov_link.expect = PROV_START;
}
static void prov_start(const uint8_t *data)
{
BT_DBG("Algorithm: 0x%02x", data[0]);
BT_DBG("Public Key: 0x%02x", data[1]);
BT_DBG("Auth Method: 0x%02x", data[2]);
BT_DBG("Auth Action: 0x%02x", data[3]);
BT_DBG("Auth Size: 0x%02x", data[4]);
if (data[0] != PROV_ALG_P256) {
BT_ERR("Unknown algorithm 0x%02x", data[0]);
prov_fail(PROV_ERR_NVAL_FMT);
return;
}
if (data[1] != PUB_KEY_NO_OOB) {
BT_ERR("Invalid public key type: 0x%02x", data[1]);
prov_fail(PROV_ERR_NVAL_FMT);
return;
}
memcpy(&bt_mesh_prov_link.conf_inputs[12], data, 5);
bt_mesh_prov_link.expect = PROV_PUB_KEY;
if (bt_mesh_prov_auth(data[2], data[3], data[4]) < 0) {
BT_ERR("Invalid authentication method: 0x%02x; "
"action: 0x%02x; size: 0x%02x", data[2], data[3],
data[4]);
prov_fail(PROV_ERR_NVAL_FMT);
}
if (atomic_test_bit(bt_mesh_prov_link.flags, OOB_STATIC_KEY)) {
memcpy(bt_mesh_prov_link.auth + 16 - bt_mesh_prov->static_val_len,
bt_mesh_prov->static_val, bt_mesh_prov->static_val_len);
(void)memset(bt_mesh_prov_link.auth, 0,
sizeof(bt_mesh_prov_link.auth) - bt_mesh_prov->static_val_len);
}
}
static void send_confirm(void)
{
struct os_mbuf *cfm = PROV_BUF(17);
BT_DBG("ConfInputs[0] %s", bt_hex(bt_mesh_prov_link.conf_inputs, 64));
BT_DBG("ConfInputs[64] %s", bt_hex(&bt_mesh_prov_link.conf_inputs[64], 64));
BT_DBG("ConfInputs[128] %s", bt_hex(&bt_mesh_prov_link.conf_inputs[128], 17));
if (bt_mesh_prov_conf_salt(bt_mesh_prov_link.conf_inputs,
bt_mesh_prov_link.conf_salt)) {
BT_ERR("Unable to generate confirmation salt");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("ConfirmationSalt: %s", bt_hex(bt_mesh_prov_link.conf_salt, 16));
if (bt_mesh_prov_conf_key(bt_mesh_prov_link.dhkey, bt_mesh_prov_link.conf_salt,
bt_mesh_prov_link.conf_key)) {
BT_ERR("Unable to generate confirmation key");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("ConfirmationKey: %s", bt_hex(bt_mesh_prov_link.conf_key, 16));
if (bt_rand(bt_mesh_prov_link.rand, 16)) {
BT_ERR("Unable to generate random number");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("LocalRandom: %s", bt_hex(bt_mesh_prov_link.rand, 16));
bt_mesh_prov_buf_init(cfm, PROV_CONFIRM);
if (bt_mesh_prov_conf(bt_mesh_prov_link.conf_key, bt_mesh_prov_link.rand,
bt_mesh_prov_link.auth, net_buf_simple_add(cfm, 16))) {
BT_ERR("Unable to generate confirmation value");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
if (bt_mesh_prov_send(cfm, NULL)) {
BT_ERR("Failed to send Provisioning Confirm");
return;
}
bt_mesh_prov_link.expect = PROV_RANDOM;
}
static void send_input_complete(void)
{
struct os_mbuf *buf = PROV_BUF(1);
bt_mesh_prov_buf_init(buf, PROV_INPUT_COMPLETE);
if (bt_mesh_prov_send(buf, NULL)) {
BT_ERR("Failed to send Provisioning Input Complete");
}
bt_mesh_prov_link.expect = PROV_CONFIRM;
}
static void public_key_sent(int err, void *cb_data)
{
atomic_set_bit(bt_mesh_prov_link.flags, PUB_KEY_SENT);
if (atomic_test_bit(bt_mesh_prov_link.flags, INPUT_COMPLETE)) {
send_input_complete();
return;
}
}
static void send_pub_key(void)
{
struct os_mbuf *buf = PROV_BUF(65);
const uint8_t *key;
key = bt_pub_key_get();
if (!key) {
BT_ERR("No public key available");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("Local Public Key: %s", bt_hex(key, 64));
bt_mesh_prov_buf_init(buf, PROV_PUB_KEY);
/* Swap X and Y halves independently to big-endian */
sys_memcpy_swap(net_buf_simple_add(buf, 32), key, 32);
sys_memcpy_swap(net_buf_simple_add(buf, 32), &key[32], 32);
/* PublicKeyRemote */
memcpy(&bt_mesh_prov_link.conf_inputs[81], &buf->om_databuf[1], 64);
if (bt_mesh_prov_send(buf, public_key_sent)) {
BT_ERR("Failed to send Public Key");
return;
}
if (atomic_test_bit(bt_mesh_prov_link.flags, WAIT_NUMBER) ||
atomic_test_bit(bt_mesh_prov_link.flags, WAIT_STRING)) {
bt_mesh_prov_link.expect = PROV_NO_PDU; /* Wait for input */
} else {
bt_mesh_prov_link.expect = PROV_CONFIRM;
}
}
static void prov_dh_key_cb(const uint8_t dhkey[32])
{
BT_DBG("%p", dhkey);
if (!dhkey) {
BT_ERR("DHKey generation failed");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
sys_memcpy_swap(bt_mesh_prov_link.dhkey, dhkey, 32);
BT_DBG("DHkey: %s", bt_hex(bt_mesh_prov_link.dhkey, 32));
send_pub_key();
}
static void prov_dh_key_gen(void)
{
uint8_t remote_pk_le[64], *remote_pk;
remote_pk = &bt_mesh_prov_link.conf_inputs[17];
/* Copy remote key in little-endian for bt_dh_key_gen().
* X and Y halves are swapped independently. The bt_dh_key_gen()
* will also take care of validating the remote public key.
*/
sys_memcpy_swap(remote_pk_le, remote_pk, 32);
sys_memcpy_swap(&remote_pk_le[32], &remote_pk[32], 32);
if (bt_dh_key_gen(remote_pk_le, prov_dh_key_cb)) {
BT_ERR("Failed to generate DHKey");
prov_fail(PROV_ERR_UNEXP_ERR);
}
}
static void prov_pub_key(const uint8_t *data)
{
BT_DBG("Remote Public Key: %s", bt_hex(data, 64));
/* PublicKeyProvisioner */
memcpy(&bt_mesh_prov_link.conf_inputs[17], data, 64);
if (!bt_pub_key_get()) {
/* Clear retransmit timer */
bt_mesh_prov_link.bearer->clear_tx();
atomic_set_bit(bt_mesh_prov_link.flags, WAIT_PUB_KEY);
BT_WARN("Waiting for local public key");
return;
}
prov_dh_key_gen();
}
static void pub_key_ready(const uint8_t *pkey)
{
if (!pkey) {
BT_WARN("Public key not available");
return;
}
BT_DBG("Local public key ready");
if (atomic_test_and_clear_bit(bt_mesh_prov_link.flags, WAIT_PUB_KEY)) {
prov_dh_key_gen();
}
}
static void notify_input_complete(void)
{
if (atomic_test_and_clear_bit(bt_mesh_prov_link.flags,
NOTIFY_INPUT_COMPLETE) &&
bt_mesh_prov->input_complete) {
bt_mesh_prov->input_complete();
}
}
static void send_random(void)
{
struct os_mbuf *rnd = PROV_BUF(17);
bt_mesh_prov_buf_init(rnd, PROV_RANDOM);
net_buf_simple_add_mem(rnd, bt_mesh_prov_link.rand, 16);
if (bt_mesh_prov_send(rnd, NULL)) {
BT_ERR("Failed to send Provisioning Random");
return;
}
bt_mesh_prov_link.expect = PROV_DATA;
}
static void prov_random(const uint8_t *data)
{
uint8_t conf_verify[16];
BT_DBG("Remote Random: %s", bt_hex(data, 16));
if (!memcmp(data, bt_mesh_prov_link.rand, 16)) {
BT_ERR("Random value is identical to ours, rejecting.");
prov_fail(PROV_ERR_CFM_FAILED);
return;
}
if (bt_mesh_prov_conf(bt_mesh_prov_link.conf_key, data,
bt_mesh_prov_link.auth, conf_verify)) {
BT_ERR("Unable to calculate confirmation verification");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
if (memcmp(conf_verify, bt_mesh_prov_link.conf, 16)) {
BT_ERR("Invalid confirmation value");
BT_DBG("Received: %s", bt_hex(bt_mesh_prov_link.conf, 16));
BT_DBG("Calculated: %s", bt_hex(conf_verify, 16));
prov_fail(PROV_ERR_CFM_FAILED);
return;
}
if (bt_mesh_prov_salt(bt_mesh_prov_link.conf_salt, data,
bt_mesh_prov_link.rand, bt_mesh_prov_link.prov_salt)) {
BT_ERR("Failed to generate provisioning salt");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("ProvisioningSalt: %s", bt_hex(bt_mesh_prov_link.prov_salt, 16));
send_random();
}
static void prov_confirm(const uint8_t *data)
{
BT_DBG("Remote Confirm: %s", bt_hex(data, 16));
memcpy(bt_mesh_prov_link.conf, data, 16);
notify_input_complete();
send_confirm();
}
static inline bool is_pb_gatt(void)
{
return bt_mesh_prov_link.bearer &&
bt_mesh_prov_link.bearer->type == BT_MESH_PROV_GATT;
}
static void prov_data(const uint8_t *data)
{
struct os_mbuf *msg = PROV_BUF(1);
uint8_t session_key[16];
uint8_t nonce[13];
uint8_t dev_key[16];
uint8_t pdu[25];
uint8_t flags;
uint32_t iv_index;
uint16_t addr;
uint16_t net_idx;
int err;
bool identity_enable;
BT_DBG("");
err = bt_mesh_session_key(bt_mesh_prov_link.dhkey,
bt_mesh_prov_link.prov_salt, session_key);
if (err) {
BT_ERR("Unable to generate session key");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("SessionKey: %s", bt_hex(session_key, 16));
err = bt_mesh_prov_nonce(bt_mesh_prov_link.dhkey,
bt_mesh_prov_link.prov_salt, nonce);
if (err) {
BT_ERR("Unable to generate session nonce");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("Nonce: %s", bt_hex(nonce, 13));
err = bt_mesh_prov_decrypt(session_key, nonce, data, pdu);
if (err) {
BT_ERR("Unable to decrypt provisioning data");
prov_fail(PROV_ERR_DECRYPT);
return;
}
err = bt_mesh_dev_key(bt_mesh_prov_link.dhkey,
bt_mesh_prov_link.prov_salt, dev_key);
if (err) {
BT_ERR("Unable to generate device key");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("DevKey: %s", bt_hex(dev_key, 16));
net_idx = sys_get_be16(&pdu[16]);
flags = pdu[18];
iv_index = sys_get_be32(&pdu[19]);
addr = sys_get_be16(&pdu[23]);
BT_DBG("net_idx %u iv_index 0x%08x, addr 0x%04x",
net_idx, iv_index, addr);
bt_mesh_prov_buf_init(msg, PROV_COMPLETE);
if (bt_mesh_prov_send(msg, NULL)) {
BT_ERR("Failed to send Provisioning Complete");
return;
}
/* Ignore any further PDUs on this link */
bt_mesh_prov_link.expect = PROV_NO_PDU;
/* Store info, since bt_mesh_provision() will end up clearing it */
if (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY)) {
identity_enable = is_pb_gatt();
} else {
identity_enable = false;
}
err = bt_mesh_provision(pdu, net_idx, flags, iv_index, addr, dev_key);
if (err) {
BT_ERR("Failed to provision (err %d)", err);
return;
}
/* After PB-GATT provisioning we should start advertising
* using Node Identity.
*/
if (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY) && identity_enable) {
bt_mesh_proxy_identity_enable();
}
}
static void local_input_complete(void)
{
if (atomic_test_bit(bt_mesh_prov_link.flags, PUB_KEY_SENT)) {
send_input_complete();
} else {
atomic_set_bit(bt_mesh_prov_link.flags, INPUT_COMPLETE);
}
}
static void prov_link_closed(void)
{
reset_state();
}
static void prov_link_opened(void)
{
bt_mesh_prov_link.expect = PROV_INVITE;
}
static const struct bt_mesh_prov_role role_device = {
.input_complete = local_input_complete,
.link_opened = prov_link_opened,
.link_closed = prov_link_closed,
.error = prov_fail,
.op = {
[PROV_INVITE] = prov_invite,
[PROV_START] = prov_start,
[PROV_PUB_KEY] = prov_pub_key,
[PROV_CONFIRM] = prov_confirm,
[PROV_RANDOM] = prov_random,
[PROV_DATA] = prov_data,
},
};
int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers)
{
if (bt_mesh_is_provisioned()) {
return -EALREADY;
}
if (IS_ENABLED(CONFIG_BT_MESH_PB_ADV) &&
(bearers & BT_MESH_PROV_ADV)) {
pb_adv.link_accept(bt_mesh_prov_bearer_cb_get(), NULL);
}
if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT) &&
(bearers & BT_MESH_PROV_GATT)) {
pb_gatt.link_accept(bt_mesh_prov_bearer_cb_get(), NULL);
}
bt_mesh_prov_link.role = &role_device;
return 0;
}
int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers)
{
if (bt_mesh_is_provisioned()) {
return -EALREADY;
}
if (IS_ENABLED(CONFIG_BT_MESH_PB_ADV) &&
(bearers & BT_MESH_PROV_ADV)) {
bt_mesh_beacon_disable();
bt_mesh_scan_disable();
}
if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT) &&
(bearers & BT_MESH_PROV_GATT)) {
bt_mesh_proxy_prov_disable(true);
}
return 0;
}
+741
View File
@@ -0,0 +1,741 @@
/* Bluetooth Mesh */
/*
* Copyright (c) 2017 Intel Corporation
* Copyright (c) 2020 Lingao Meng
*
* SPDX-License-Identifier: Apache-2.0
*/
#define LOG_MODULE_NAME bt_mesh_provisioner
#include "testing.h"
#include "crypto.h"
#include "adv.h"
#include "mesh/mesh.h"
#include "net.h"
#include "rpl.h"
#include "beacon.h"
#include "access.h"
#include "foundation.h"
#include "proxy.h"
#include "prov.h"
#include "settings.h"
static struct {
struct bt_mesh_cdb_node *node;
uint16_t addr;
uint16_t net_idx;
uint8_t attention_duration;
uint8_t uuid[16];
} prov_device;
static void send_pub_key(void);
static void prov_dh_key_gen(void);
static void pub_key_ready(const uint8_t *pkey);
static int reset_state(void)
{
if (prov_device.node != NULL) {
bt_mesh_cdb_node_del(prov_device.node, false);
}
return bt_mesh_prov_reset_state(pub_key_ready);
}
static void prov_link_close(enum prov_bearer_link_status status)
{
BT_DBG("%u", status);
bt_mesh_prov_link.expect = PROV_NO_PDU;
bt_mesh_prov_link.bearer->link_close(status);
}
static void prov_fail(uint8_t reason)
{
/* According to Bluetooth Mesh Specification v1.0.1, Section 5.4.4, the
* provisioner just closes the link when something fails, while the
* provisionee sends the fail message, and waits for the provisioner to
* close the link.
*/
prov_link_close(PROV_BEARER_LINK_STATUS_FAIL);
}
static void send_invite(void)
{
struct os_mbuf *inv = PROV_BUF(2);
BT_DBG("");
bt_mesh_prov_buf_init(inv, PROV_INVITE);
net_buf_simple_add_u8(inv, prov_device.attention_duration);
bt_mesh_prov_link.conf_inputs[0] = prov_device.attention_duration;
if (bt_mesh_prov_send(inv, NULL)) {
BT_ERR("Failed to send invite");
return;
}
bt_mesh_prov_link.expect = PROV_CAPABILITIES;
}
static void start_sent(int err, void *cb_data)
{
if (!bt_pub_key_get()) {
atomic_set_bit(bt_mesh_prov_link.flags, WAIT_PUB_KEY);
BT_WARN("Waiting for local public key");
} else {
send_pub_key();
}
}
static void send_start(void)
{
BT_DBG("");
uint8_t method, action;
struct os_mbuf *start = PROV_BUF(6);
const uint8_t *data = &bt_mesh_prov_link.conf_inputs[1 + 3];
bt_mesh_prov_buf_init(start, PROV_START);
net_buf_simple_add_u8(start, PROV_ALG_P256);
if (atomic_test_bit(bt_mesh_prov_link.flags, REMOTE_PUB_KEY) &&
*data == PUB_KEY_OOB) {
net_buf_simple_add_u8(start, PUB_KEY_OOB);
atomic_set_bit(bt_mesh_prov_link.flags, OOB_PUB_KEY);
} else {
net_buf_simple_add_u8(start, PUB_KEY_NO_OOB);
}
if (bt_mesh_prov_link.oob_method == AUTH_METHOD_INPUT) {
method = AUTH_METHOD_OUTPUT;
if (bt_mesh_prov_link.oob_action == INPUT_OOB_STRING) {
action = OUTPUT_OOB_STRING;
} else {
action = OUTPUT_OOB_NUMBER;
}
} else if (bt_mesh_prov_link.oob_method == AUTH_METHOD_OUTPUT) {
method = AUTH_METHOD_INPUT;
if (bt_mesh_prov_link.oob_action == OUTPUT_OOB_STRING) {
action = INPUT_OOB_STRING;
} else {
action = INPUT_OOB_NUMBER;
}
} else {
method = bt_mesh_prov_link.oob_method;
action = 0x00;
}
net_buf_simple_add_u8(start, bt_mesh_prov_link.oob_method);
net_buf_simple_add_u8(start, bt_mesh_prov_link.oob_action);
net_buf_simple_add_u8(start, bt_mesh_prov_link.oob_size);
memcpy(&bt_mesh_prov_link.conf_inputs[12], &start->om_omp[1], 5);
if (bt_mesh_prov_auth(method, action, bt_mesh_prov_link.oob_size) < 0) {
BT_ERR("Invalid authentication method: 0x%02x; "
"action: 0x%02x; size: 0x%02x", method,
action, bt_mesh_prov_link.oob_size);
return;
}
if (bt_mesh_prov_send(start, start_sent)) {
BT_ERR("Failed to send Provisioning Start");
return;
}
}
static bool prov_check_method(struct bt_mesh_dev_capabilities *caps)
{
if (bt_mesh_prov_link.oob_method == AUTH_METHOD_STATIC) {
if (!caps->static_oob) {
BT_WARN("Device not support OOB static authentication provisioning");
return false;
}
} else if (bt_mesh_prov_link.oob_method == AUTH_METHOD_INPUT) {
if (bt_mesh_prov_link.oob_size > caps->input_size) {
BT_WARN("The required input length (0x%02x) "
"exceeds the device capacity (0x%02x)",
bt_mesh_prov_link.oob_size, caps->input_size);
return false;
}
if (!(BIT(bt_mesh_prov_link.oob_action) & caps->input_actions)) {
BT_WARN("The required input action (0x%02x) "
"not supported by the device (0x%02x)",
bt_mesh_prov_link.oob_action, caps->input_actions);
return false;
}
if (bt_mesh_prov_link.oob_action == INPUT_OOB_STRING) {
if (!bt_mesh_prov->output_string) {
BT_WARN("Not support output string");
return false;
}
} else {
if (!bt_mesh_prov->output_number) {
BT_WARN("Not support output number");
return false;
}
}
} else if (bt_mesh_prov_link.oob_method == AUTH_METHOD_OUTPUT) {
if (bt_mesh_prov_link.oob_size > caps->output_size) {
BT_WARN("The required output length (0x%02x) "
"exceeds the device capacity (0x%02x)",
bt_mesh_prov_link.oob_size, caps->output_size);
return false;
}
if (!(BIT(bt_mesh_prov_link.oob_action) & caps->output_actions)) {
BT_WARN("The required output action (0x%02x) "
"not supported by the device (0x%02x)",
bt_mesh_prov_link.oob_action, caps->output_actions);
return false;
}
if (!bt_mesh_prov->input) {
BT_WARN("Not support input");
return false;
}
}
return true;
}
static void prov_capabilities(const uint8_t *data)
{
struct bt_mesh_dev_capabilities caps;
caps.elem_count = data[0];
BT_DBG("Elements: %u", caps.elem_count);
caps.algorithms = sys_get_be16(&data[1]);
BT_DBG("Algorithms: %u", caps.algorithms);
caps.pub_key_type = data[3];
caps.static_oob = data[4];
caps.output_size = data[5];
BT_DBG("Public Key Type: 0x%02x", caps.pub_key_type);
BT_DBG("Static OOB Type: 0x%02x", caps.static_oob);
BT_DBG("Output OOB Size: %u", caps.output_size);
caps.output_actions = (bt_mesh_output_action_t)data[6];
caps.input_size = data[8];
caps.input_actions = (bt_mesh_input_action_t)data[9];
BT_DBG("Output OOB Action: 0x%04x", caps.output_actions);
BT_DBG("Input OOB Size: %u", caps.input_size);
BT_DBG("Input OOB Action: 0x%04x", caps.input_actions);
if (data[0] == 0) {
BT_ERR("Invalid number of elements");
prov_fail(PROV_ERR_NVAL_FMT);
return;
}
prov_device.node =
bt_mesh_cdb_node_alloc(prov_device.uuid,
prov_device.addr, data[0],
prov_device.net_idx);
if (prov_device.node == NULL) {
BT_ERR("Failed allocating node 0x%04x", prov_device.addr);
prov_fail(PROV_ERR_RESOURCES);
return;
}
memcpy(&bt_mesh_prov_link.conf_inputs[1], data, 11);
if (bt_mesh_prov->capabilities) {
bt_mesh_prov->capabilities(&caps);
}
if (!prov_check_method(&caps)) {
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
send_start();
}
static void send_confirm(void)
{
struct os_mbuf *cfm = PROV_BUF(17);
BT_DBG("ConfInputs[0] %s", bt_hex(bt_mesh_prov_link.conf_inputs, 64));
BT_DBG("ConfInputs[64] %s", bt_hex(&bt_mesh_prov_link.conf_inputs[64], 64));
BT_DBG("ConfInputs[128] %s", bt_hex(&bt_mesh_prov_link.conf_inputs[128], 17));
if (bt_mesh_prov_conf_salt(bt_mesh_prov_link.conf_inputs,
bt_mesh_prov_link.conf_salt)) {
BT_ERR("Unable to generate confirmation salt");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("ConfirmationSalt: %s", bt_hex(bt_mesh_prov_link.conf_salt, 16));
if (bt_mesh_prov_conf_key(bt_mesh_prov_link.dhkey,
bt_mesh_prov_link.conf_salt, bt_mesh_prov_link.conf_key)) {
BT_ERR("Unable to generate confirmation key");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("ConfirmationKey: %s", bt_hex(bt_mesh_prov_link.conf_key, 16));
if (bt_rand(bt_mesh_prov_link.rand, 16)) {
BT_ERR("Unable to generate random number");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("LocalRandom: %s", bt_hex(bt_mesh_prov_link.rand, 16));
bt_mesh_prov_buf_init(cfm, PROV_CONFIRM);
if (bt_mesh_prov_conf(bt_mesh_prov_link.conf_key,
bt_mesh_prov_link.rand, bt_mesh_prov_link.auth,
net_buf_simple_add(cfm, 16))) {
BT_ERR("Unable to generate confirmation value");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
if (bt_mesh_prov_send(cfm, NULL)) {
BT_ERR("Failed to send Provisioning Confirm");
return;
}
bt_mesh_prov_link.expect = PROV_CONFIRM;
}
static void public_key_sent(int err, void *cb_data)
{
atomic_set_bit(bt_mesh_prov_link.flags, PUB_KEY_SENT);
if (atomic_test_bit(bt_mesh_prov_link.flags, OOB_PUB_KEY) &&
atomic_test_bit(bt_mesh_prov_link.flags, REMOTE_PUB_KEY)) {
prov_dh_key_gen();
return;
}
bt_mesh_prov_link.expect = PROV_PUB_KEY;
}
static void send_pub_key(void)
{
struct os_mbuf *buf = PROV_BUF(65);
const uint8_t *key;
key = bt_pub_key_get();
if (!key) {
BT_ERR("No public key available");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("Local Public Key: %s", bt_hex(key, 64));
bt_mesh_prov_buf_init(buf, PROV_PUB_KEY);
/* Swap X and Y halves independently to big-endian */
sys_memcpy_swap(net_buf_simple_add(buf, 32), key, 32);
sys_memcpy_swap(net_buf_simple_add(buf, 32), &key[32], 32);
/* PublicKeyProvisioner */
memcpy(&bt_mesh_prov_link.conf_inputs[17], &buf->om_databuf[1], 64);
if (bt_mesh_prov_send(buf, public_key_sent)) {
BT_ERR("Failed to send Public Key");
return;
}
}
static void prov_dh_key_cb(const uint8_t dhkey[32])
{
BT_DBG("%p", dhkey);
if (!dhkey) {
BT_ERR("DHKey generation failed");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
sys_memcpy_swap(bt_mesh_prov_link.dhkey, dhkey, 32);
BT_DBG("DHkey: %s", bt_hex(bt_mesh_prov_link.dhkey, 32));
if (atomic_test_bit(bt_mesh_prov_link.flags, WAIT_STRING) ||
atomic_test_bit(bt_mesh_prov_link.flags, WAIT_NUMBER) ||
atomic_test_bit(bt_mesh_prov_link.flags, NOTIFY_INPUT_COMPLETE)) {
atomic_set_bit(bt_mesh_prov_link.flags, WAIT_CONFIRM);
return;
}
send_confirm();
}
static void prov_dh_key_gen(void)
{
uint8_t remote_pk_le[64], *remote_pk;
remote_pk = &bt_mesh_prov_link.conf_inputs[81];
/* Copy remote key in little-endian for bt_dh_key_gen().
* X and Y halves are swapped independently. The bt_dh_key_gen()
* will also take care of validating the remote public key.
*/
sys_memcpy_swap(remote_pk_le, remote_pk, 32);
sys_memcpy_swap(&remote_pk_le[32], &remote_pk[32], 32);
if (bt_dh_key_gen(remote_pk_le, prov_dh_key_cb)) {
BT_ERR("Failed to generate DHKey");
prov_fail(PROV_ERR_UNEXP_ERR);
}
if (atomic_test_bit(bt_mesh_prov_link.flags, NOTIFY_INPUT_COMPLETE)) {
bt_mesh_prov_link.expect = PROV_INPUT_COMPLETE;
}
}
static void prov_pub_key(const uint8_t *data)
{
BT_DBG("Remote Public Key: %s", bt_hex(data, 64));
atomic_set_bit(bt_mesh_prov_link.flags, REMOTE_PUB_KEY);
/* PublicKeyDevice */
memcpy(&bt_mesh_prov_link.conf_inputs[81], data, 64);
bt_mesh_prov_link.bearer->clear_tx();
prov_dh_key_gen();
}
static void pub_key_ready(const uint8_t *pkey)
{
if (!pkey) {
BT_WARN("Public key not available");
return;
}
BT_DBG("Local public key ready");
if (atomic_test_and_clear_bit(bt_mesh_prov_link.flags, WAIT_PUB_KEY)) {
send_pub_key();
}
}
static void notify_input_complete(void)
{
if (atomic_test_and_clear_bit(bt_mesh_prov_link.flags,
NOTIFY_INPUT_COMPLETE) &&
bt_mesh_prov->input_complete) {
bt_mesh_prov->input_complete();
}
}
static void prov_input_complete(const uint8_t *data)
{
BT_DBG("");
notify_input_complete();
if (atomic_test_and_clear_bit(bt_mesh_prov_link.flags, WAIT_CONFIRM)) {
send_confirm();
}
}
static void send_prov_data(void)
{
struct os_mbuf *pdu = PROV_BUF(34);
struct bt_mesh_cdb_subnet *sub;
uint8_t session_key[16];
uint8_t nonce[13];
int err;
err = bt_mesh_session_key(bt_mesh_prov_link.dhkey,
bt_mesh_prov_link.prov_salt, session_key);
if (err) {
BT_ERR("Unable to generate session key");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("SessionKey: %s", bt_hex(session_key, 16));
err = bt_mesh_prov_nonce(bt_mesh_prov_link.dhkey,
bt_mesh_prov_link.prov_salt, nonce);
if (err) {
BT_ERR("Unable to generate session nonce");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("Nonce: %s", bt_hex(nonce, 13));
err = bt_mesh_dev_key(bt_mesh_prov_link.dhkey,
bt_mesh_prov_link.prov_salt, prov_device.node->dev_key);
if (err) {
BT_ERR("Unable to generate device key");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("DevKey: %s", bt_hex(prov_device.node->dev_key, 16));
sub = bt_mesh_cdb_subnet_get(prov_device.node->net_idx);
if (sub == NULL) {
BT_ERR("No subnet with net_idx %u",
prov_device.node->net_idx);
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
bt_mesh_prov_buf_init(pdu, PROV_DATA);
net_buf_simple_add_mem(pdu, sub->keys[sub->kr_flag].net_key, 16);
net_buf_simple_add_be16(pdu, prov_device.node->net_idx);
net_buf_simple_add_u8(pdu, bt_mesh_cdb_subnet_flags(sub));
net_buf_simple_add_be32(pdu, bt_mesh_cdb.iv_index);
net_buf_simple_add_be16(pdu, prov_device.node->addr);
net_buf_simple_add(pdu, 8); /* For MIC */
BT_DBG("net_idx %u, iv_index 0x%08x, addr 0x%04x",
prov_device.node->net_idx, bt_mesh.iv_index,
prov_device.node->addr);
err = bt_mesh_prov_encrypt(session_key, nonce, &pdu->om_databuf[1],
&pdu->om_databuf[1]);
if (err) {
BT_ERR("Unable to encrypt provisioning data");
prov_fail(PROV_ERR_DECRYPT);
return;
}
if (bt_mesh_prov_send(pdu, NULL)) {
BT_ERR("Failed to send Provisioning Data");
return;
}
bt_mesh_prov_link.expect = PROV_COMPLETE;
}
static void prov_complete(const uint8_t *data)
{
struct bt_mesh_cdb_node *node = prov_device.node;
BT_DBG("key %s, net_idx %u, num_elem %u, addr 0x%04x",
bt_hex(node->dev_key, 16), node->net_idx, node->num_elem,
node->addr);
if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
bt_mesh_store_cdb_node(node);
}
prov_device.node = NULL;
prov_link_close(PROV_BEARER_LINK_STATUS_SUCCESS);
if (bt_mesh_prov->node_added) {
bt_mesh_prov->node_added(node->net_idx, node->uuid, node->addr,
node->num_elem);
}
}
static void send_random(void)
{
struct os_mbuf *rnd = PROV_BUF(17);
bt_mesh_prov_buf_init(rnd, PROV_RANDOM);
net_buf_simple_add_mem(rnd, bt_mesh_prov_link.rand, 16);
if (bt_mesh_prov_send(rnd, NULL)) {
BT_ERR("Failed to send Provisioning Random");
return;
}
bt_mesh_prov_link.expect = PROV_RANDOM;
}
static void prov_random(const uint8_t *data)
{
uint8_t conf_verify[16];
BT_DBG("Remote Random: %s", bt_hex(data, 16));
if (!memcmp(data, bt_mesh_prov_link.rand, 16)) {
BT_ERR("Random value is identical to ours, rejecting.");
prov_fail(PROV_ERR_CFM_FAILED);
return;
}
if (bt_mesh_prov_conf(bt_mesh_prov_link.conf_key,
data, bt_mesh_prov_link.auth, conf_verify)) {
BT_ERR("Unable to calculate confirmation verification");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
if (memcmp(conf_verify, bt_mesh_prov_link.conf, 16)) {
BT_ERR("Invalid confirmation value");
BT_DBG("Received: %s", bt_hex(bt_mesh_prov_link.conf, 16));
BT_DBG("Calculated: %s", bt_hex(conf_verify, 16));
prov_fail(PROV_ERR_CFM_FAILED);
return;
}
if (bt_mesh_prov_salt(bt_mesh_prov_link.conf_salt,
bt_mesh_prov_link.rand, data, bt_mesh_prov_link.prov_salt)) {
BT_ERR("Failed to generate provisioning salt");
prov_fail(PROV_ERR_UNEXP_ERR);
return;
}
BT_DBG("ProvisioningSalt: %s", bt_hex(bt_mesh_prov_link.prov_salt, 16));
send_prov_data();
}
static void prov_confirm(const uint8_t *data)
{
BT_DBG("Remote Confirm: %s", bt_hex(data, 16));
memcpy(bt_mesh_prov_link.conf, data, 16);
send_random();
}
static void prov_failed(const uint8_t *data)
{
BT_WARN("Error: 0x%02x", data[0]);
reset_state();
}
static void local_input_complete(void)
{
if (atomic_test_and_clear_bit(bt_mesh_prov_link.flags, WAIT_CONFIRM)) {
send_confirm();
}
}
static void prov_link_closed(void)
{
reset_state();
}
static void prov_link_opened(void)
{
send_invite();
}
static const struct bt_mesh_prov_role role_provisioner = {
.input_complete = local_input_complete,
.link_opened = prov_link_opened,
.link_closed = prov_link_closed,
.error = prov_fail,
.op = {
[PROV_CAPABILITIES] = prov_capabilities,
[PROV_PUB_KEY] = prov_pub_key,
[PROV_INPUT_COMPLETE] = prov_input_complete,
[PROV_CONFIRM] = prov_confirm,
[PROV_RANDOM] = prov_random,
[PROV_COMPLETE] = prov_complete,
[PROV_FAILED] = prov_failed,
},
};
static void prov_set_method(uint8_t method, uint8_t action, uint8_t size)
{
bt_mesh_prov_link.oob_method = method;
bt_mesh_prov_link.oob_action = action;
bt_mesh_prov_link.oob_size = size;
}
int bt_mesh_auth_method_set_input(bt_mesh_input_action_t action, uint8_t size)
{
if (!action || !size || size > 8) {
return -EINVAL;
}
prov_set_method(AUTH_METHOD_INPUT, find_msb_set(action) - 1, size);
return 0;
}
int bt_mesh_auth_method_set_output(bt_mesh_output_action_t action, uint8_t size)
{
if (!action || !size || size > 8) {
return -EINVAL;
}
prov_set_method(AUTH_METHOD_OUTPUT, find_msb_set(action) - 1, size);
return 0;
}
int bt_mesh_auth_method_set_static(const uint8_t *static_val, uint8_t size)
{
if (!size || !static_val || size > 16) {
return -EINVAL;
}
prov_set_method(AUTH_METHOD_STATIC, 0, 0);
memcpy(bt_mesh_prov_link.auth + 16 - size, static_val, size);
if (size < 16) {
(void)memset(bt_mesh_prov_link.auth, 0,
sizeof(bt_mesh_prov_link.auth) - size);
}
return 0;
}
int bt_mesh_auth_method_set_none(void)
{
prov_set_method(AUTH_METHOD_NO_OOB, 0, 0);
return 0;
}
int bt_mesh_prov_remote_pub_key_set(const uint8_t public_key[64])
{
if (public_key == NULL) {
return -EINVAL;
}
if (atomic_test_and_set_bit(bt_mesh_prov_link.flags, REMOTE_PUB_KEY)) {
return -EALREADY;
}
/* Swap X and Y halves independently to big-endian */
memcpy(&bt_mesh_prov_link.conf_inputs[81], public_key, 32);
memcpy(&bt_mesh_prov_link.conf_inputs[81 + 32], &public_key[32], 32);
return 0;
}
#if defined(CONFIG_BT_MESH_PB_ADV)
int bt_mesh_pb_adv_open(const uint8_t uuid[16], uint16_t net_idx, uint16_t addr,
uint8_t attention_duration)
{
int err;
if (atomic_test_and_set_bit(bt_mesh_prov_link.flags, LINK_ACTIVE)) {
return -EBUSY;
}
atomic_set_bit(bt_mesh_prov_link.flags, PROVISIONER);
memcpy(prov_device.uuid, uuid, 16);
prov_device.addr = addr;
prov_device.net_idx = net_idx;
prov_device.attention_duration = attention_duration;
bt_mesh_prov_link.bearer = &pb_adv;
bt_mesh_prov_link.role = &role_provisioner;
err = bt_mesh_prov_link.bearer->link_open(prov_device.uuid, PROTOCOL_TIMEOUT,
bt_mesh_prov_bearer_cb_get(), NULL);
if (err) {
atomic_clear_bit(bt_mesh_prov_link.flags, LINK_ACTIVE);
}
return err;
}
#endif
+10
View File
@@ -0,0 +1,10 @@
/* Bluetooth Mesh */
/*
* Copyright (c) 2017 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
int bt_mesh_pb_adv_open(const uint8_t uuid[16], uint16_t net_idx, uint16_t addr,
uint8_t attention_duration);
+1 -1
View File
@@ -2229,7 +2229,7 @@ struct shell_cmd_help cmd_hb_pub_help = {
#endif /* MYNEWT_VAL(BLE_MESH_CFG_CLI) */
#if MYNEWT_VAL(BLE_MESH_PROV)
#if MYNEWT_VAL(BLE_MESH_PROV_DEVICE)
static int cmd_pb(bt_mesh_prov_bearer_t bearer, int argc, char *argv[])
{
int err;
+35 -2
View File
@@ -40,12 +40,19 @@ syscfg.defs:
Timeout value of retransmit provisioning PDUs.
value: 500
BLE_MESH_PROV_DEVICE:
description: >
Enable this option to allow the device to be provisioned into a mesh network.
value: 1
restrictions: BLE_MESH_PROV
BLE_MESH_PROVISIONER:
description: >
Enable this option to have support for provisioning remote devices.
value: 0
value: 1
restrictions:
- (BLE_MESH_PROV)
- BLE_MESH_PROV
- BLE_MESH_PB_ADV
BLE_MESH_CDB:
description: >
@@ -697,6 +704,24 @@ syscfg.defs:
Minimum level for the BLE Mesh Replay protection list log.
value: 1
BLE_MESH_PROV_DEVICE_LOG_MOD:
description: >
Numeric module ID to use for BLE Mesh Replay protection list messages.
value: 24
BLE_MESH_PROV_DEVICE_LOG_LVL:
description: >
Minimum level for the BLE Mesh Replay protection list log.
value: 1
BLE_MESH_PROVISIONER_LOG_MOD:
description: >
Numeric module ID to use for BLE Mesh Replay protection list messages.
value: 25
BLE_MESH_PROVISIONER_LOG_LVL:
description: >
Minimum level for the BLE Mesh Replay protection list log.
value: 1
syscfg.logs:
BLE_MESH_LOG:
module: MYNEWT_VAL(BLE_MESH_LOG_MOD)
@@ -758,6 +783,14 @@ syscfg.logs:
module: MYNEWT_VAL(BLE_MESH_NET_KEYS_LOG_MOD)
level: MYNEWT_VAL(BLE_MESH_NET_KEYS_LOG_LVL)
BLE_MESH_PROV_DEVICE_LOG:
module: MYNEWT_VAL(BLE_MESH_PROV_DEVICE_LOG_MOD)
level: MYNEWT_VAL(BLE_MESH_PROV_DEVICE_LOG_LVL)
BLE_MESH_PROVISIONER_LOG:
module: MYNEWT_VAL(BLE_MESH_PROVISIONER_LOG_MOD)
level: MYNEWT_VAL(BLE_MESH_PROVISIONER_LOG_LVL)
syscfg.vals.BLE_MESH_SHELL:
BLE_MESH_CFG_CLI: 1
BLE_MESH_HEALTH_CLI: 1
@@ -120,6 +120,13 @@
#define BLE_MESH_NET_KEYS_LOG_CRITICAL(...) MODLOG_CRITICAL(23, __VA_ARGS__)
#define BLE_MESH_NET_KEYS_LOG_DISABLED(...) MODLOG_DISABLED(23, __VA_ARGS__)
#define BLE_MESH_PROV_DEVICE_LOG_DEBUG(...) IGNORE(__VA_ARGS__)
#define BLE_MESH_PROV_DEVICE_LOG_INFO(...) MODLOG_INFO(24, __VA_ARGS__)
#define BLE_MESH_PROV_DEVICE_LOG_WARN(...) MODLOG_WARN(24, __VA_ARGS__)
#define BLE_MESH_PROV_DEVICE_LOG_ERROR(...) MODLOG_ERROR(24, __VA_ARGS__)
#define BLE_MESH_PROV_DEVICE_LOG_CRITICAL(...) MODLOG_CRITICAL(24, __VA_ARGS__)
#define BLE_MESH_PROV_DEVICE_LOG_DISABLED(...) MODLOG_DISABLED(24, __VA_ARGS__)
#define DFLT_LOG_DEBUG(...) IGNORE(__VA_ARGS__)
#define DFLT_LOG_INFO(...) MODLOG_INFO(0, __VA_ARGS__)
#define DFLT_LOG_WARN(...) MODLOG_WARN(0, __VA_ARGS__)