2015-05-30 13:05:26 +02:00
|
|
|
/* keys.c - Bluetooth key handling */
|
|
|
|
|
|
|
|
/*
|
2016-06-10 11:10:18 +02:00
|
|
|
* Copyright (c) 2015-2016 Intel Corporation
|
2015-05-30 13:05:26 +02:00
|
|
|
*
|
2017-01-19 02:01:01 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-05-30 13:05:26 +02:00
|
|
|
*/
|
|
|
|
|
includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.
The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.
NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-25 09:58:46 +02:00
|
|
|
#include <zephyr/kernel.h>
|
2015-05-30 13:05:26 +02:00
|
|
|
#include <string.h>
|
2018-07-04 11:58:10 +02:00
|
|
|
#include <stdlib.h>
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/sys/atomic.h>
|
|
|
|
#include <zephyr/sys/util.h>
|
|
|
|
#include <zephyr/sys/byteorder.h>
|
2015-05-30 13:05:26 +02:00
|
|
|
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/settings/settings.h>
|
2018-04-27 19:01:51 +02:00
|
|
|
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/bluetooth/bluetooth.h>
|
|
|
|
#include <zephyr/bluetooth/buf.h>
|
|
|
|
#include <zephyr/bluetooth/conn.h>
|
|
|
|
#include <zephyr/bluetooth/hci.h>
|
2015-05-30 13:05:26 +02:00
|
|
|
|
2022-10-25 08:48:54 +02:00
|
|
|
#include "common/bt_str.h"
|
2017-05-10 16:27:16 +02:00
|
|
|
|
|
|
|
#include "common/rpa.h"
|
2020-10-16 04:19:02 +02:00
|
|
|
#include "conn_internal.h"
|
2018-11-26 13:28:29 +01:00
|
|
|
#include "gatt_internal.h"
|
2015-05-30 13:05:26 +02:00
|
|
|
#include "hci_core.h"
|
2015-05-31 19:19:39 +02:00
|
|
|
#include "smp.h"
|
2018-04-27 19:01:51 +02:00
|
|
|
#include "settings.h"
|
2015-05-30 13:05:26 +02:00
|
|
|
#include "keys.h"
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
#define LOG_LEVEL CONFIG_BT_KEYS_LOG_LEVEL
|
|
|
|
#include <zephyr/logging/log.h>
|
|
|
|
LOG_MODULE_REGISTER(bt_keys);
|
|
|
|
|
2017-08-09 08:21:11 +02:00
|
|
|
static struct bt_keys key_pool[CONFIG_BT_MAX_PAIRED];
|
2015-05-31 17:17:19 +02:00
|
|
|
|
2019-09-26 16:27:50 +02:00
|
|
|
#define BT_KEYS_STORAGE_LEN_COMPAT (BT_KEYS_STORAGE_LEN - sizeof(uint32_t))
|
|
|
|
|
2022-12-19 17:11:01 +01:00
|
|
|
#if defined(CONFIG_BT_KEYS_OVERWRITE_OLDEST)
|
2020-05-27 18:26:57 +02:00
|
|
|
static uint32_t aging_counter_val;
|
2019-09-26 16:27:50 +02:00
|
|
|
static struct bt_keys *last_keys_updated;
|
2020-10-16 04:19:02 +02:00
|
|
|
|
|
|
|
struct key_data {
|
|
|
|
bool in_use;
|
|
|
|
uint8_t id;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void find_key_in_use(struct bt_conn *conn, void *data)
|
|
|
|
{
|
|
|
|
struct key_data *kdata = data;
|
|
|
|
struct bt_keys *key;
|
|
|
|
|
2022-08-26 11:39:38 +02:00
|
|
|
__ASSERT_NO_MSG(conn != NULL);
|
|
|
|
__ASSERT_NO_MSG(data != NULL);
|
|
|
|
|
2020-10-16 04:19:02 +02:00
|
|
|
if (conn->state == BT_CONN_CONNECTED) {
|
|
|
|
key = bt_keys_find_addr(conn->id, bt_conn_get_dst(conn));
|
|
|
|
if (key == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2022-08-26 14:21:41 +02:00
|
|
|
|
|
|
|
/* Ensure that the reference returned matches the current pool item */
|
|
|
|
if (key == &key_pool[kdata->id]) {
|
2020-10-16 04:19:02 +02:00
|
|
|
kdata->in_use = true;
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Connected device %s is using key_pool[%d]",
|
|
|
|
bt_addr_le_str(bt_conn_get_dst(conn)), kdata->id);
|
2020-10-16 04:19:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool key_is_in_use(uint8_t id)
|
|
|
|
{
|
|
|
|
struct key_data kdata = { false, id };
|
|
|
|
|
|
|
|
bt_conn_foreach(BT_CONN_TYPE_ALL, find_key_in_use, &kdata);
|
|
|
|
|
|
|
|
return kdata.in_use;
|
|
|
|
}
|
2019-09-26 16:27:50 +02:00
|
|
|
#endif /* CONFIG_BT_KEYS_OVERWRITE_OLDEST */
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
struct bt_keys *bt_keys_get_addr(uint8_t id, const bt_addr_le_t *addr)
|
2015-05-30 13:05:26 +02:00
|
|
|
{
|
|
|
|
struct bt_keys *keys;
|
|
|
|
int i;
|
2019-05-29 15:57:26 +02:00
|
|
|
size_t first_free_slot = ARRAY_SIZE(key_pool);
|
2015-05-30 13:05:26 +02:00
|
|
|
|
2022-08-26 11:39:38 +02:00
|
|
|
__ASSERT_NO_MSG(addr != NULL);
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("%s", bt_addr_le_str(addr));
|
2015-05-30 13:05:26 +02:00
|
|
|
|
2015-05-31 17:17:19 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(key_pool); i++) {
|
|
|
|
keys = &key_pool[i];
|
2015-05-30 13:05:26 +02:00
|
|
|
|
2022-09-20 10:57:54 +02:00
|
|
|
if (keys->id == id && bt_addr_le_eq(&keys->addr, addr)) {
|
2015-05-31 17:17:19 +02:00
|
|
|
return keys;
|
|
|
|
}
|
2019-05-29 15:57:26 +02:00
|
|
|
if (first_free_slot == ARRAY_SIZE(key_pool) &&
|
2022-09-20 10:57:54 +02:00
|
|
|
bt_addr_le_eq(&keys->addr, BT_ADDR_LE_ANY)) {
|
2019-05-29 15:57:26 +02:00
|
|
|
first_free_slot = i;
|
2015-05-30 13:05:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-19 17:11:01 +01:00
|
|
|
#if defined(CONFIG_BT_KEYS_OVERWRITE_OLDEST)
|
2019-09-26 16:27:50 +02:00
|
|
|
if (first_free_slot == ARRAY_SIZE(key_pool)) {
|
2020-10-16 04:19:02 +02:00
|
|
|
struct bt_keys *oldest = NULL;
|
2020-05-08 14:45:28 +02:00
|
|
|
bt_addr_le_t oldest_addr;
|
2019-09-26 16:27:50 +02:00
|
|
|
|
2020-10-16 04:19:02 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(key_pool); i++) {
|
2019-09-26 16:27:50 +02:00
|
|
|
struct bt_keys *current = &key_pool[i];
|
2022-08-26 16:33:01 +02:00
|
|
|
bool key_in_use = key_is_in_use(i);
|
2019-09-26 16:27:50 +02:00
|
|
|
|
2020-10-16 04:19:02 +02:00
|
|
|
if (key_in_use) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((oldest == NULL) || (current->aging_counter < oldest->aging_counter)) {
|
2019-09-26 16:27:50 +02:00
|
|
|
oldest = current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 04:19:02 +02:00
|
|
|
if (oldest == NULL) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("unable to create keys for %s", bt_addr_le_str(addr));
|
2020-10-16 04:19:02 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-05-08 14:45:28 +02:00
|
|
|
/* Use a copy as bt_unpair will clear the oldest key. */
|
|
|
|
bt_addr_le_copy(&oldest_addr, &oldest->addr);
|
|
|
|
bt_unpair(oldest->id, &oldest_addr);
|
2022-09-20 10:57:54 +02:00
|
|
|
if (bt_addr_le_eq(&oldest->addr, BT_ADDR_LE_ANY)) {
|
2019-09-26 16:27:50 +02:00
|
|
|
first_free_slot = oldest - &key_pool[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_BT_KEYS_OVERWRITE_OLDEST */
|
2019-05-29 15:57:26 +02:00
|
|
|
if (first_free_slot < ARRAY_SIZE(key_pool)) {
|
|
|
|
keys = &key_pool[first_free_slot];
|
|
|
|
keys->id = id;
|
|
|
|
bt_addr_le_copy(&keys->addr, addr);
|
2022-12-19 17:11:01 +01:00
|
|
|
#if defined(CONFIG_BT_KEYS_OVERWRITE_OLDEST)
|
2019-09-26 16:27:50 +02:00
|
|
|
keys->aging_counter = ++aging_counter_val;
|
|
|
|
last_keys_updated = keys;
|
|
|
|
#endif /* CONFIG_BT_KEYS_OVERWRITE_OLDEST */
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("created %p for %s", keys, bt_addr_le_str(addr));
|
2019-05-29 15:57:26 +02:00
|
|
|
return keys;
|
|
|
|
}
|
2019-09-26 16:27:50 +02:00
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("unable to create keys for %s", bt_addr_le_str(addr));
|
2015-05-30 13:05:26 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-10-03 13:57:03 +02:00
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
void bt_foreach_bond(uint8_t id, void (*func)(const struct bt_bond_info *info,
|
2018-09-20 14:19:40 +02:00
|
|
|
void *user_data),
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2022-08-26 11:39:38 +02:00
|
|
|
__ASSERT_NO_MSG(func != NULL);
|
|
|
|
|
2018-09-20 14:19:40 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(key_pool); i++) {
|
|
|
|
struct bt_keys *keys = &key_pool[i];
|
|
|
|
|
|
|
|
if (keys->keys && keys->id == id) {
|
|
|
|
struct bt_bond_info info;
|
|
|
|
|
|
|
|
bt_addr_le_copy(&info.addr, &keys->addr);
|
|
|
|
func(&info, user_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 10:27:45 +02:00
|
|
|
void bt_keys_foreach_type(enum bt_keys_type type, void (*func)(struct bt_keys *keys, void *data),
|
|
|
|
void *data)
|
2017-10-03 13:57:03 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2022-08-26 11:39:38 +02:00
|
|
|
__ASSERT_NO_MSG(func != NULL);
|
|
|
|
|
2017-10-03 13:57:03 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(key_pool); i++) {
|
|
|
|
if ((key_pool[i].keys & type)) {
|
2018-07-18 12:45:44 +02:00
|
|
|
func(&key_pool[i], data);
|
2017-10-03 13:57:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 10:27:45 +02:00
|
|
|
struct bt_keys *bt_keys_find(enum bt_keys_type type, uint8_t id, const bt_addr_le_t *addr)
|
2015-05-31 17:17:19 +02:00
|
|
|
{
|
2015-08-27 16:14:41 +02:00
|
|
|
int i;
|
2015-05-31 17:17:19 +02:00
|
|
|
|
2022-08-26 11:39:38 +02:00
|
|
|
__ASSERT_NO_MSG(addr != NULL);
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("type %d %s", type, bt_addr_le_str(addr));
|
2015-05-31 17:17:19 +02:00
|
|
|
|
2015-08-27 16:14:41 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(key_pool); i++) {
|
2018-07-04 11:58:10 +02:00
|
|
|
if ((key_pool[i].keys & type) && key_pool[i].id == id &&
|
2022-09-20 10:57:54 +02:00
|
|
|
bt_addr_le_eq(&key_pool[i].addr, addr)) {
|
2015-08-27 16:14:41 +02:00
|
|
|
return &key_pool[i];
|
2015-07-15 15:05:10 +02:00
|
|
|
}
|
2015-05-31 17:17:19 +02:00
|
|
|
}
|
2015-08-27 16:14:41 +02:00
|
|
|
|
|
|
|
return NULL;
|
2015-05-30 13:05:26 +02:00
|
|
|
}
|
|
|
|
|
2022-09-19 10:27:45 +02:00
|
|
|
struct bt_keys *bt_keys_get_type(enum bt_keys_type type, uint8_t id, const bt_addr_le_t *addr)
|
2015-07-08 20:36:41 +02:00
|
|
|
{
|
|
|
|
struct bt_keys *keys;
|
|
|
|
|
2022-08-26 11:39:38 +02:00
|
|
|
__ASSERT_NO_MSG(addr != NULL);
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("type %d %s", type, bt_addr_le_str(addr));
|
2015-07-08 20:36:41 +02:00
|
|
|
|
2018-07-04 11:58:10 +02:00
|
|
|
keys = bt_keys_find(type, id, addr);
|
2015-07-08 20:36:41 +02:00
|
|
|
if (keys) {
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
2018-07-04 11:58:10 +02:00
|
|
|
keys = bt_keys_get_addr(id, addr);
|
2015-07-08 20:36:41 +02:00
|
|
|
if (!keys) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_keys_add_type(keys, type);
|
2015-05-31 17:17:19 +02:00
|
|
|
|
|
|
|
return keys;
|
2015-05-30 13:05:26 +02:00
|
|
|
}
|
2015-05-31 19:19:39 +02:00
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
struct bt_keys *bt_keys_find_irk(uint8_t id, const bt_addr_le_t *addr)
|
2015-05-31 19:19:39 +02:00
|
|
|
{
|
2015-08-27 16:14:41 +02:00
|
|
|
int i;
|
2015-05-31 19:19:39 +02:00
|
|
|
|
2022-08-26 11:39:38 +02:00
|
|
|
__ASSERT_NO_MSG(addr != NULL);
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("%s", bt_addr_le_str(addr));
|
2015-05-31 19:19:39 +02:00
|
|
|
|
|
|
|
if (!bt_addr_le_is_rpa(addr)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-27 16:14:41 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(key_pool); i++) {
|
|
|
|
if (!(key_pool[i].keys & BT_KEYS_IRK)) {
|
|
|
|
continue;
|
2015-05-31 19:19:39 +02:00
|
|
|
}
|
|
|
|
|
2018-07-04 11:58:10 +02:00
|
|
|
if (key_pool[i].id == id &&
|
2023-05-08 14:09:45 +02:00
|
|
|
bt_addr_eq(&addr->a, &key_pool[i].irk.rpa)) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("cached RPA %s for %s", bt_addr_str(&key_pool[i].irk.rpa),
|
|
|
|
bt_addr_le_str(&key_pool[i].addr));
|
2015-08-27 16:14:41 +02:00
|
|
|
return &key_pool[i];
|
|
|
|
}
|
|
|
|
}
|
2015-05-31 19:19:39 +02:00
|
|
|
|
2015-08-27 16:14:41 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(key_pool); i++) {
|
|
|
|
if (!(key_pool[i].keys & BT_KEYS_IRK)) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-05-31 19:19:39 +02:00
|
|
|
|
2018-07-04 11:58:10 +02:00
|
|
|
if (key_pool[i].id != id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-05-10 16:27:16 +02:00
|
|
|
if (bt_rpa_irk_matches(key_pool[i].irk.val, &addr->a)) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("RPA %s matches %s", bt_addr_str(&key_pool[i].irk.rpa),
|
|
|
|
bt_addr_le_str(&key_pool[i].addr));
|
2015-05-31 19:19:39 +02:00
|
|
|
|
2016-04-04 12:06:55 +02:00
|
|
|
bt_addr_copy(&key_pool[i].irk.rpa, &addr->a);
|
2015-05-31 19:19:39 +02:00
|
|
|
|
2015-08-27 16:14:41 +02:00
|
|
|
return &key_pool[i];
|
2015-05-31 19:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("No IRK for %s", bt_addr_le_str(addr));
|
2015-05-31 19:19:39 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-07-29 14:02:10 +02:00
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
struct bt_keys *bt_keys_find_addr(uint8_t id, const bt_addr_le_t *addr)
|
2015-07-29 14:02:10 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2022-08-26 11:39:38 +02:00
|
|
|
__ASSERT_NO_MSG(addr != NULL);
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("%s", bt_addr_le_str(addr));
|
2015-07-29 14:02:10 +02:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(key_pool); i++) {
|
2018-07-04 11:58:10 +02:00
|
|
|
if (key_pool[i].id == id &&
|
2022-09-20 10:57:54 +02:00
|
|
|
bt_addr_le_eq(&key_pool[i].addr, addr)) {
|
2015-08-27 16:14:41 +02:00
|
|
|
return &key_pool[i];
|
2015-07-29 14:02:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-23 09:49:19 +01:00
|
|
|
|
2022-09-19 10:27:45 +02:00
|
|
|
void bt_keys_add_type(struct bt_keys *keys, enum bt_keys_type type)
|
2016-03-23 09:49:19 +01:00
|
|
|
{
|
2022-08-26 11:39:38 +02:00
|
|
|
__ASSERT_NO_MSG(keys != NULL);
|
|
|
|
|
2016-03-23 09:49:19 +01:00
|
|
|
keys->keys |= type;
|
|
|
|
}
|
|
|
|
|
2016-08-08 16:40:30 +02:00
|
|
|
void bt_keys_clear(struct bt_keys *keys)
|
2016-03-23 09:49:19 +01:00
|
|
|
{
|
2022-08-26 11:39:38 +02:00
|
|
|
__ASSERT_NO_MSG(keys != NULL);
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("%s (keys 0x%04x)", bt_addr_le_str(&keys->addr), keys->keys);
|
2016-03-23 09:49:19 +01:00
|
|
|
|
2020-03-25 17:28:28 +01:00
|
|
|
if (keys->state & BT_KEYS_ID_ADDED) {
|
2017-10-05 21:01:55 +02:00
|
|
|
bt_id_del(keys);
|
2017-10-05 08:43:56 +02:00
|
|
|
}
|
|
|
|
|
2018-04-27 19:01:51 +02:00
|
|
|
if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
|
|
|
|
/* Delete stored keys from flash */
|
2023-06-13 10:05:18 +02:00
|
|
|
bt_settings_delete_keys(keys->id, &keys->addr);
|
2018-04-27 19:01:51 +02:00
|
|
|
}
|
|
|
|
|
2018-09-12 04:09:03 +02:00
|
|
|
(void)memset(keys, 0, sizeof(*keys));
|
2015-12-28 16:39:57 +01:00
|
|
|
}
|
2016-11-04 20:34:05 +01:00
|
|
|
|
2018-04-27 19:01:51 +02:00
|
|
|
#if defined(CONFIG_BT_SETTINGS)
|
|
|
|
int bt_keys_store(struct bt_keys *keys)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2022-08-26 11:39:38 +02:00
|
|
|
__ASSERT_NO_MSG(keys != NULL);
|
|
|
|
|
2023-06-13 10:05:18 +02:00
|
|
|
err = bt_settings_store_keys(keys->id, &keys->addr, keys->storage_start,
|
|
|
|
BT_KEYS_STORAGE_LEN);
|
2018-04-27 19:01:51 +02:00
|
|
|
if (err) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Failed to save keys (err %d)", err);
|
2018-04-27 19:01:51 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-06-13 10:05:18 +02:00
|
|
|
LOG_DBG("Stored keys for %s", bt_addr_le_str(&keys->addr));
|
2018-04-27 19:01:51 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-14 18:23:39 +02:00
|
|
|
static int keys_set(const char *name, size_t len_rd, settings_read_cb read_cb,
|
|
|
|
void *cb_arg)
|
2018-04-27 19:01:51 +02:00
|
|
|
{
|
|
|
|
struct bt_keys *keys;
|
|
|
|
bt_addr_le_t addr;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t id;
|
2020-04-02 16:08:46 +02:00
|
|
|
ssize_t len;
|
2019-04-02 11:06:05 +02:00
|
|
|
int err;
|
2018-11-24 16:32:41 +01:00
|
|
|
char val[BT_KEYS_STORAGE_LEN];
|
2019-06-14 18:23:39 +02:00
|
|
|
const char *next;
|
2018-04-27 19:01:51 +02:00
|
|
|
|
2019-06-14 18:23:39 +02:00
|
|
|
if (!name) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Insufficient number of arguments");
|
2018-04-27 19:01:51 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-04-02 11:06:05 +02:00
|
|
|
len = read_cb(cb_arg, val, sizeof(val));
|
2018-11-24 16:32:41 +01:00
|
|
|
if (len < 0) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Failed to read value (err %zd)", len);
|
2018-11-24 16:32:41 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("name %s val %s", name, (len) ? bt_hex(val, sizeof(val)) : "(null)");
|
2018-04-27 19:01:51 +02:00
|
|
|
|
2019-06-14 18:23:39 +02:00
|
|
|
err = bt_settings_decode_key(name, &addr);
|
2018-04-27 19:01:51 +02:00
|
|
|
if (err) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Unable to decode address %s", name);
|
2018-04-27 19:01:51 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-06-14 18:23:39 +02:00
|
|
|
settings_name_next(name, &next);
|
|
|
|
|
|
|
|
if (!next) {
|
2018-07-04 11:58:10 +02:00
|
|
|
id = BT_ID_DEFAULT;
|
|
|
|
} else {
|
2021-09-24 16:23:49 +02:00
|
|
|
unsigned long next_id = strtoul(next, NULL, 10);
|
|
|
|
|
|
|
|
if (next_id >= CONFIG_BT_ID_MAX) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Invalid local identity %lu", next_id);
|
2021-09-24 16:23:49 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = (uint8_t)next_id;
|
2018-07-04 11:58:10 +02:00
|
|
|
}
|
|
|
|
|
2018-11-24 16:32:41 +01:00
|
|
|
if (!len) {
|
2018-07-04 11:58:10 +02:00
|
|
|
keys = bt_keys_find(BT_KEYS_ALL, id, &addr);
|
2018-04-27 19:01:51 +02:00
|
|
|
if (keys) {
|
2018-09-12 04:09:03 +02:00
|
|
|
(void)memset(keys, 0, sizeof(*keys));
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Cleared keys for %s", bt_addr_le_str(&addr));
|
2018-04-27 19:01:51 +02:00
|
|
|
} else {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_WRN("Unable to find deleted keys for %s", bt_addr_le_str(&addr));
|
2018-04-27 19:01:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-04 11:58:10 +02:00
|
|
|
keys = bt_keys_get_addr(id, &addr);
|
2018-04-27 19:01:51 +02:00
|
|
|
if (!keys) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Failed to allocate keys for %s", bt_addr_le_str(&addr));
|
2018-04-27 19:01:51 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2019-09-26 16:27:50 +02:00
|
|
|
if (len != BT_KEYS_STORAGE_LEN) {
|
2021-09-24 08:41:25 +02:00
|
|
|
if (IS_ENABLED(CONFIG_BT_KEYS_OVERWRITE_OLDEST) &&
|
|
|
|
len == BT_KEYS_STORAGE_LEN_COMPAT) {
|
2019-09-26 16:27:50 +02:00
|
|
|
/* Load shorter structure for compatibility with old
|
|
|
|
* records format with no counter.
|
|
|
|
*/
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_WRN("Keys for %s have no aging counter", bt_addr_le_str(&addr));
|
2021-09-24 08:41:25 +02:00
|
|
|
memcpy(keys->storage_start, val, len);
|
|
|
|
} else {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("Invalid key length %zd != %zu", len, BT_KEYS_STORAGE_LEN);
|
2019-09-26 16:27:50 +02:00
|
|
|
bt_keys_clear(keys);
|
|
|
|
|
|
|
|
return -EINVAL;
|
2021-09-24 08:41:25 +02:00
|
|
|
}
|
2018-11-24 16:32:41 +01:00
|
|
|
} else {
|
|
|
|
memcpy(keys->storage_start, val, len);
|
2018-04-27 19:01:51 +02:00
|
|
|
}
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Successfully restored keys for %s", bt_addr_le_str(&addr));
|
2022-12-19 17:11:01 +01:00
|
|
|
#if defined(CONFIG_BT_KEYS_OVERWRITE_OLDEST)
|
2019-09-26 16:27:50 +02:00
|
|
|
if (aging_counter_val < keys->aging_counter) {
|
|
|
|
aging_counter_val = keys->aging_counter;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_BT_KEYS_OVERWRITE_OLDEST */
|
2018-04-27 19:01:51 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-08 09:26:42 +02:00
|
|
|
static void id_add(struct bt_keys *keys, void *user_data)
|
|
|
|
{
|
2022-08-26 11:39:38 +02:00
|
|
|
__ASSERT_NO_MSG(keys != NULL);
|
|
|
|
|
2018-08-08 09:26:42 +02:00
|
|
|
bt_id_add(keys);
|
|
|
|
}
|
|
|
|
|
2018-04-27 19:01:51 +02:00
|
|
|
static int keys_commit(void)
|
|
|
|
{
|
|
|
|
/* We do this in commit() rather than add() since add() may get
|
|
|
|
* called multiple times for the same address, especially if
|
|
|
|
* the keys were already removed.
|
|
|
|
*/
|
2020-03-20 19:42:53 +01:00
|
|
|
if (IS_ENABLED(CONFIG_BT_CENTRAL) && IS_ENABLED(CONFIG_BT_PRIVACY)) {
|
2022-09-04 09:36:18 +02:00
|
|
|
bt_keys_foreach_type(BT_KEYS_ALL, id_add, NULL);
|
2020-03-20 19:42:53 +01:00
|
|
|
} else {
|
2022-09-04 09:36:18 +02:00
|
|
|
bt_keys_foreach_type(BT_KEYS_IRK, id_add, NULL);
|
2020-03-20 19:42:53 +01:00
|
|
|
}
|
2018-04-27 19:01:51 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-13 10:05:18 +02:00
|
|
|
BT_SETTINGS_DEFINE(keys, "keys", keys_set, keys_commit);
|
2019-06-20 11:52:18 +02:00
|
|
|
|
2018-04-27 19:01:51 +02:00
|
|
|
#endif /* CONFIG_BT_SETTINGS */
|
2019-09-26 16:27:50 +02:00
|
|
|
|
2022-12-19 17:11:01 +01:00
|
|
|
#if defined(CONFIG_BT_KEYS_OVERWRITE_OLDEST)
|
2020-05-27 18:26:57 +02:00
|
|
|
void bt_keys_update_usage(uint8_t id, const bt_addr_le_t *addr)
|
2019-09-26 16:27:50 +02:00
|
|
|
{
|
2022-08-26 11:39:38 +02:00
|
|
|
__ASSERT_NO_MSG(addr != NULL);
|
|
|
|
|
2019-09-26 16:27:50 +02:00
|
|
|
struct bt_keys *keys = bt_keys_find_addr(id, addr);
|
|
|
|
|
|
|
|
if (!keys) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last_keys_updated == keys) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
keys->aging_counter = ++aging_counter_val;
|
|
|
|
last_keys_updated = keys;
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Aging counter for %s is set to %u", bt_addr_le_str(addr), keys->aging_counter);
|
2019-09-26 16:27:50 +02:00
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_BT_KEYS_SAVE_AGING_COUNTER_ON_PAIRING)) {
|
|
|
|
bt_keys_store(keys);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_BT_KEYS_OVERWRITE_OLDEST */
|
2020-11-19 09:26:34 +01:00
|
|
|
|
|
|
|
#if defined(CONFIG_BT_LOG_SNIFFER_INFO)
|
|
|
|
void bt_keys_show_sniffer_info(struct bt_keys *keys, void *data)
|
|
|
|
{
|
|
|
|
uint8_t ltk[16];
|
|
|
|
|
2022-08-26 11:39:38 +02:00
|
|
|
__ASSERT_NO_MSG(keys != NULL);
|
|
|
|
|
2020-11-19 09:26:34 +01:00
|
|
|
if (keys->keys & BT_KEYS_LTK_P256) {
|
|
|
|
sys_memcpy_swap(ltk, keys->ltk.val, keys->enc_size);
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_INF("SC LTK: 0x%s", bt_hex(ltk, keys->enc_size));
|
2020-11-19 09:26:34 +01:00
|
|
|
}
|
|
|
|
|
2021-12-10 12:29:16 +01:00
|
|
|
#if !defined(CONFIG_BT_SMP_SC_PAIR_ONLY)
|
2021-09-17 13:49:15 +02:00
|
|
|
if (keys->keys & BT_KEYS_PERIPH_LTK) {
|
|
|
|
sys_memcpy_swap(ltk, keys->periph_ltk.val, keys->enc_size);
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_INF("Legacy LTK: 0x%s (peripheral)", bt_hex(ltk, keys->enc_size));
|
2020-11-19 09:26:34 +01:00
|
|
|
}
|
2021-12-10 12:29:16 +01:00
|
|
|
#endif /* !CONFIG_BT_SMP_SC_PAIR_ONLY */
|
2020-11-19 09:26:34 +01:00
|
|
|
|
|
|
|
if (keys->keys & BT_KEYS_LTK) {
|
|
|
|
sys_memcpy_swap(ltk, keys->ltk.val, keys->enc_size);
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_INF("Legacy LTK: 0x%s (central)", bt_hex(ltk, keys->enc_size));
|
2020-11-19 09:26:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* defined(CONFIG_BT_LOG_SNIFFER_INFO) */
|
2022-09-05 16:42:36 +02:00
|
|
|
|
|
|
|
#ifdef ZTEST_UNITTEST
|
|
|
|
struct bt_keys *bt_keys_get_key_pool(void)
|
|
|
|
{
|
|
|
|
return key_pool;
|
|
|
|
}
|
2022-09-05 16:43:26 +02:00
|
|
|
|
2022-12-19 17:11:01 +01:00
|
|
|
#if defined(CONFIG_BT_KEYS_OVERWRITE_OLDEST)
|
2022-09-05 16:43:26 +02:00
|
|
|
uint32_t bt_keys_get_aging_counter_val(void)
|
|
|
|
{
|
|
|
|
return aging_counter_val;
|
|
|
|
}
|
2022-09-08 20:28:46 +02:00
|
|
|
|
|
|
|
struct bt_keys *bt_keys_get_last_keys_updated(void)
|
|
|
|
{
|
|
|
|
return last_keys_updated;
|
|
|
|
}
|
2022-09-05 16:43:26 +02:00
|
|
|
#endif /* CONFIG_BT_KEYS_OVERWRITE_OLDEST */
|
2022-09-05 16:42:36 +02:00
|
|
|
#endif /* ZTEST_UNITTEST */
|