diff --git a/subsys/bluetooth/mesh/app_keys.c b/subsys/bluetooth/mesh/app_keys.c index 64677a3386e89ce..86ecde92617a4d1 100644 --- a/subsys/bluetooth/mesh/app_keys.c +++ b/subsys/bluetooth/mesh/app_keys.c @@ -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; } diff --git a/subsys/bluetooth/mesh/main.c b/subsys/bluetooth/mesh/main.c index 18a52e238ffe58a..a7115ee473e78fd 100644 --- a/subsys/bluetooth/mesh/main.c +++ b/subsys/bluetooth/mesh/main.c @@ -51,6 +51,7 @@ 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)) { @@ -58,6 +59,7 @@ int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx, } 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); @@ -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) { @@ -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; @@ -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; + } + + 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) { @@ -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); }