From: Luiz Augusto von Dentz <luiz.von.dentz@xxxxxxxxx> This adds support for "rsi" when a SIRK has been set on main.conf, the clients can then enable it via Includes property which will make the daemon to automatically generate an RSI (hash+random) and include it as part of the advertising data: < HCI Command: LE Set Extended Advertising Data (0x08|0x0037) plen 15 Handle: 0x01 Operation: Complete extended advertising data (0x03) Fragment preference: Minimize fragmentation (0x01) Data length: 0x0b Resolvable Set Identifier: E2-4E-AA-1B-2B-61 Hash: 0x1b2b61 Random: 0xe24eaa Flags: 0x06 LE General Discoverable Mode BR/EDR Not Supported --- src/advertising.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/advertising.c b/src/advertising.c index f9748b1328bc..0dceb14c3be4 100644 --- a/src/advertising.c +++ b/src/advertising.c @@ -29,11 +29,13 @@ #include "error.h" #include "log.h" #include "eir.h" +#include "btd.h" #include "src/shared/ad.h" #include "src/shared/mgmt.h" #include "src/shared/queue.h" #include "src/shared/timeout.h" #include "src/shared/util.h" +#include "src/shared/crypto.h" #include "advertising.h" #define LE_ADVERTISING_MGR_IFACE "org.bluez.LEAdvertisingManager1" @@ -459,13 +461,50 @@ fail: return false; } +static bool set_rsi(struct btd_adv_client *client) +{ + struct bt_crypto *crypto; + uint8_t zero[16] = {}; + struct bt_ad_data rsi = { .type = BT_AD_CSIP_RSI }; + uint8_t data[6]; + bool ret; + + /* Check if a valid SIRK has been set */ + if (!memcmp(btd_opts.csis.sirk, zero, sizeof(zero))) + return false; + + /* Check if RSI needs to be set or data already contains RSI data */ + if (!client || bt_ad_has_data(client->data, &rsi)) + return true; + + crypto = bt_crypto_new(); + if (!crypto) + return false; + + ret = bt_crypto_random_bytes(crypto, data + 3, sizeof(data) - 3); + if (!ret) + goto done; + + ret = bt_crypto_sih(crypto, btd_opts.csis.sirk, data + 3, data); + if (!ret) + goto done; + + ret = bt_ad_add_data(client->data, BT_AD_CSIP_RSI, data, sizeof(data)); + +done: + bt_crypto_unref(crypto); + return ret; +} + static struct adv_include { uint8_t flag; const char *name; + bool (*set)(struct btd_adv_client *client); } includes[] = { { MGMT_ADV_FLAG_TX_POWER, "tx-power" }, { MGMT_ADV_FLAG_APPEARANCE, "appearance" }, { MGMT_ADV_FLAG_LOCAL_NAME, "local-name" }, + { 0 , "rsi", set_rsi }, { }, }; @@ -497,6 +536,11 @@ static bool parse_includes(DBusMessageIter *iter, if (strcmp(str, inc->name)) continue; + if (inc->set && inc->set(client)) { + DBG("Including Feature: %s", str); + continue; + } + if (!(client->manager->supported_flags & inc->flag)) continue; @@ -1644,7 +1688,8 @@ static void append_include(struct btd_adv_manager *manager, struct adv_include *inc; for (inc = includes; inc && inc->name; inc++) { - if (manager->supported_flags & inc->flag) + if ((inc->set && inc->set(NULL)) || + (manager->supported_flags & inc->flag)) dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &inc->name); } -- 2.39.2