Skip to content

Commit

Permalink
Bluetooth: Mesh: instantiate cdb and core keys
Browse files Browse the repository at this point in the history
PR instantiates cdb and core keys to prevent scenario
when key is changed over cdb APIand gets new ID but
core still operates with old ID(potentially invalid).

Known issue the keys will be desynchronized
during\after key refresh procedure.

Signed-off-by: Aleksandr Khromykh <[email protected]>
  • Loading branch information
alxelax committed Jun 28, 2023
1 parent 8042218 commit ec2a013
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
15 changes: 7 additions & 8 deletions subsys/bluetooth/mesh/app_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,20 +488,19 @@ int bt_mesh_keys_resolve(struct bt_mesh_msg_ctx *ctx,

if (ctx->app_idx == BT_MESH_KEY_DEV_REMOTE &&
!bt_mesh_has_addr(ctx->addr)) {
struct bt_mesh_cdb_node *node;

if (!IS_ENABLED(CONFIG_BT_MESH_CDB)) {
LOG_WRN("No DevKey for 0x%04x", ctx->addr);
return -EINVAL;
}
} else {
struct bt_mesh_cdb_node *node = bt_mesh_cdb_node_get(ctx->addr);

node = bt_mesh_cdb_node_get(ctx->addr);
if (!node) {
LOG_WRN("No DevKey for 0x%04x", ctx->addr);
return -EINVAL;
if (!node) {
LOG_WRN("No DevKey for 0x%04x", ctx->addr);
return -EINVAL;
}
*app_key = &node->dev_key;
}

*app_key = &node->dev_key;
} else {
*app_key = &bt_mesh.dev_key;
}
Expand Down
43 changes: 23 additions & 20 deletions subsys/bluetooth/mesh/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx,
struct bt_mesh_key mesh_net_key;
bool is_net_key_valid = false;
bool is_dev_key_valid = false;
bool is_cdb_dev_key_valid = false;
int err = 0;

if (!atomic_test_bit(bt_mesh.flags, BT_MESH_INIT)) {
return -ENODEV;
}

struct bt_mesh_cdb_subnet *subnet = NULL;
struct bt_mesh_cdb_node *node = NULL;

LOG_INF("Primary Element: 0x%04x", addr);
LOG_DBG("net_idx 0x%04x flags 0x%02x iv_index 0x%04x", net_idx, flags, iv_index);
Expand All @@ -70,7 +72,6 @@ int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx,
atomic_test_bit(bt_mesh_cdb.flags, BT_MESH_CDB_VALID)) {
const struct bt_mesh_comp *comp;
const struct bt_mesh_prov *prov;
struct bt_mesh_cdb_node *node;

comp = bt_mesh_comp_get();
if (comp == NULL) {
Expand Down Expand Up @@ -101,16 +102,15 @@ int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx,
subnet->kr_phase = BT_MESH_KR_NORMAL;
}

/* The network key has already been existed. Importing leaves it 'as is'
* if key is the same. Otherwise, cdb replaces the old one by the new one.
*/
err = bt_mesh_cdb_subnet_key_import(subnet, BT_MESH_KEY_REFRESH(flags) ? 1 : 0,
net_key);
if (err) {
LOG_ERR("Failed to import cdb network key");
goto end;
}
memcpy(&mesh_net_key, &subnet->keys[BT_MESH_KEY_REFRESH(flags) ? 1 : 0].net_key,
sizeof(struct bt_mesh_key));
is_net_key_valid = true;

bt_mesh_cdb_subnet_store(subnet);

addr = node->addr;
Expand All @@ -121,27 +121,26 @@ int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx,
LOG_ERR("Failed to import cdb device key");
goto end;
}
memcpy(&mesh_dev_key, &node->dev_key, sizeof(struct bt_mesh_key));
is_dev_key_valid = true;
is_cdb_dev_key_valid = true;

if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
bt_mesh_cdb_node_store(node);
}
} else {
err = bt_mesh_key_import(BT_MESH_KEY_TYPE_NET, net_key, &mesh_net_key);
if (err) {
LOG_ERR("Failed to import network key");
goto end;
}
is_net_key_valid = true;
}

Check failure on line 130 in subsys/bluetooth/mesh/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

TRAILING_WHITESPACE

subsys/bluetooth/mesh/main.c:130 trailing whitespace
err = bt_mesh_key_import(BT_MESH_KEY_TYPE_DEV, dev_key, &mesh_dev_key);
if (err) {
LOG_ERR("Failed to import device key");
goto end;
}
is_dev_key_valid = true;

err = bt_mesh_key_import(BT_MESH_KEY_TYPE_DEV, dev_key, &mesh_dev_key);
if (err) {
LOG_ERR("Failed to import device key");
goto end;
}
is_dev_key_valid = true;
err = bt_mesh_key_import(BT_MESH_KEY_TYPE_NET, net_key, &mesh_net_key);
if (err) {
LOG_ERR("Failed to import network key");
goto end;
}
is_net_key_valid = true;

err = bt_mesh_net_create(net_idx, flags, &mesh_net_key, iv_index);
if (err) {
Expand Down Expand Up @@ -169,6 +168,10 @@ int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx,
bt_mesh_start();

end:
if (err && is_cdb_dev_key_valid && IS_ENABLED(CONFIG_BT_MESH_CDB)) {
bt_mesh_cdb_node_del(node, true);
}

if (err && is_dev_key_valid) {
bt_mesh_key_destroy(&mesh_dev_key);
}
Expand Down

0 comments on commit ec2a013

Please sign in to comment.