diff mbox series

[BlueZ,4/5] advertising: Add support for rsi as Includes

Message ID 20230405231111.2636523-4-luiz.dentz@gmail.com (mailing list archive)
State Accepted
Commit eef2e62a5bc4eb4d90a507b3834937102d906b95
Headers show
Series [BlueZ,1/5] main.conf: Fix parsing of CSIS group | expand

Checks

Context Check Description
tedd_an/pre-ci_am success Success
tedd_an/CheckPatch fail ERROR:SPACING: space prohibited before that ',' (ctx:WxW) #170: FILE: src/advertising.c:507: + { 0 , "rsi", set_rsi }, ^ /github/workspace/src/src/13202645.patch total: 1 errors, 0 warnings, 83 lines checked NOTE: For some of the reported defects, checkpatch may be able to mechanically convert to the typical style using --fix or --fix-inplace. /github/workspace/src/src/13202645.patch has style problems, please review. NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO NOTE: If any of the errors are false positives, please report them to the maintainer, see CHECKPATCH in MAINTAINERS.
tedd_an/GitLint success Gitlint PASS
tedd_an/IncrementalBuild success Incremental Build PASS

Commit Message

Luiz Augusto von Dentz April 5, 2023, 11:11 p.m. UTC
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

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 mbox series

Patch

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);
 	}