Skip to contentSkip to conversation

client/mgmt: Add options to ltks command for loading entries

PATCH In review Luiz Augusto von Dentz 2 participants 3 messages Jun 3 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/2 client/mgmt: Add options to ltks command for loading entries

From: Luiz Augusto von Dentz <[email protected]>

Add support for optionally specifying a single LTK entry when using the ltks command. When called without arguments it clears all LTKs (existing behavior). When called with an address and key parameters it loads exactly one LTK entry, useful for testing.

Options:

  -a addr_type  Address type (1=LE Public, 2=LE Random)
  -t key_type   Key type (0=Unauthenticated, 1=Authenticated)
  -c central    Central flag (0 or 1)
  -e enc_size   Encryption key size (7-16)
  -d ediv       Encrypted Diversifier
  -r rand       Random number (64-bit)
  -k key        128-bit key as 32 hex characters
---
 client/mgmt.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 94 insertions(+), 6 deletions(-)
client/mgmt.c +94 -6
index 50558a313866..6d6ada95f43d 100644
@@ -3535,21 +3535,107 @@ static void ltks_rsp(uint8_t status, uint16_t len, const void *param,
 	bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
+static const struct option ltks_options[] = {
+	{ "help",	0, 0, 'h' },
+	{ "address-type", 1, 0, 'a' },
+	{ "type",	1, 0, 't' },
+	{ "central",	1, 0, 'c' },
+	{ "enc-size",	1, 0, 'e' },
+	{ "ediv",	1, 0, 'd' },
+	{ "rand",	1, 0, 'r' },
+	{ "key",	1, 0, 'k' },
+	{ 0, 0, 0, 0 }
+};
+
 static void cmd_ltks(int argc, char **argv)
 {
-	struct mgmt_cp_load_long_term_keys cp;
+	struct mgmt_cp_load_long_term_keys *cp;
+	uint8_t buf[sizeof(*cp) + sizeof(struct mgmt_ltk_info)];
+	uint16_t count = 0;
+	struct mgmt_ltk_info *ltk;
+	uint8_t addr_type = BDADDR_LE_PUBLIC;
+	int opt;
 	uint16_t index;
 
 	index = mgmt_index;
 	if (index == MGMT_INDEX_NONE)
 		index = 0;
 
-	memset(&cp, 0, sizeof(cp));
+	cp = (void *) buf;
+	memset(buf, 0, sizeof(buf));
+	ltk = &cp->keys[0];
 
-	if (mgmt_send(mgmt, MGMT_OP_LOAD_LONG_TERM_KEYS, index, sizeof(cp), &cp,
-						ltks_rsp, NULL, NULL) == 0) {
+	while ((opt = getopt_long(argc, argv, "+a:t:c:e:d:r:k:h",
+					ltks_options, NULL)) != -1) {
+		switch (opt) {
+		case 'a':
+			addr_type = strtol(optarg, NULL, 0);
+			if (addr_type != BDADDR_LE_PUBLIC &&
+					addr_type != BDADDR_LE_RANDOM) {
+				error("Invalid address type (expected 1=LE "
+							"Public, 2=LE Random)");
+				optind = 0;
+				return bt_shell_noninteractive_quit(
+								EXIT_FAILURE);
+			}
+			break;
+		case 't':
+			ltk->type = strtol(optarg, NULL, 0);
+			break;
+		case 'c':
+			ltk->central = strtol(optarg, NULL, 0);
+			break;
+		case 'e':
+			ltk->enc_size = strtol(optarg, NULL, 0);
+			break;
+		case 'd':
+			ltk->ediv = strtol(optarg, NULL, 0);
+			break;
+		case 'r':
+			ltk->rand = strtoull(optarg, NULL, 0);
+			break;
+		case 'k':
+			if (strlen(optarg) != 32) {
+				error("Invalid key length (expected 32 hex "
+								"chars)");
+				optind = 0;
+				return bt_shell_noninteractive_quit(
+								EXIT_FAILURE);
+			}
+			for (int i = 0; i < 16; i++) {
+				char byte[3] = { optarg[i * 2],
+						optarg[i * 2 + 1], 0 };
+				ltk->val[i] = strtol(byte, NULL, 16);
+			}
+			break;
+		case 'h':
+			bt_shell_usage();
+			optind = 0;
+			return bt_shell_noninteractive_quit(EXIT_SUCCESS);
+		default:
+			bt_shell_usage();
+			optind = 0;
+			return bt_shell_noninteractive_quit(EXIT_FAILURE);
+		}
+	}
+
+	argc -= optind;
+	argv += optind;
+	optind = 0;
+
+	if (argc > 0) {
+		str2ba(argv[0], &ltk->addr.bdaddr);
+		ltk->addr.type = addr_type;
+		count = 1;
+	}
+
+	cp->key_count = cpu_to_le16(count);
+
+	if (mgmt_send(mgmt, MGMT_OP_LOAD_LONG_TERM_KEYS, index,
+				sizeof(*cp) + count * sizeof(*ltk), cp,
+				ltks_rsp, NULL, NULL) == 0) {
 		error("Unable to send load_ltks cmd");
-		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
 	}
 }
 
@@ -6136,7 +6222,9 @@ static const struct bt_shell_menu mgmt_menu = {
 		cmd_unpair,		"Unpair device"			},
 	{ "keys",		NULL,
 		cmd_keys,		"Load Link Keys"		},
-	{ "ltks",		NULL,
+	{ "ltks",		"[-a addr_type] [-t key_type] [-c central] "
+				"[-e enc_size] [-d ediv] [-r rand] [-k key] "
+				"[address]",
 		cmd_ltks,		"Load Long Term Keys"		},
 	{ "irks",		"[--local index] [--file file path]",
 		cmd_irks,		"Load Identity Resolving Keys"	},
Signature · 1 line
2.54.0
author maintainer
#1 lore raw

PATCH 2/2 doc/bluetoothctl-mgmt: Update ltks command documentation

From: Luiz Augusto von Dentz <[email protected]>

Document the new options for the ltks command including address type, key type, central flag, encryption size, EDIV, random number and key value parameters.

---
 doc/bluetoothctl-mgmt.rst | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
doc/bluetoothctl-mgmt.rst +16 -3
index 646c2dcb4365..da04f9c225b3 100644
@@ -509,11 +509,24 @@ Load Link Keys
 ltks
 ----
 
-Load Long Term Keys
+Load Long Term Keys. Note that loading keys replaces all previously loaded
+keys, so loading a single entry invalidates all previously loaded keys.
 
-:Usage: **> ltks**
-:Example Load stored LE long term keys:
+:Usage: **> ltks [-a addr_type] [-t key_type] [-c central] [-e enc_size] [-d ediv] [-r rand] [-k key] [address]**
+:[-a addr_type]: Address type (1=LE Public, 2=LE Random). Default: 1 (LE Public)
+:[-t key_type]: Key type (0=Unauthenticated, 1=Authenticated). Default: 0 (Unauthenticated)
+:[-c central]: Central flag (0 or 1). Default: 0
+:[-e enc_size]: Encryption key size (7-16). Default: 0
+:[-d ediv]: Encrypted Diversifier (16-bit value). Default: 0x0000
+:[-r rand]: Random number (64-bit value). Default: 0x0000000000000000
+:[-k key]: 128-bit key as 32 hex characters. Default: all zeros
+:[address]: Bluetooth address of the peer device
+:Example Clear all long term keys:
 	| **> ltks**
+:Example Load an authenticated LTK for a specific device:
+	| **> ltks -t 1 -c 1 -e 16 -d 0x1234 -r 0xABCD -k 0123456789abcdef0123456789abcdef 00:11:22:33:44:55**
+:Example Load an LTK with LE Random address type:
+	| **> ltks -a 2 -t 0 -c 1 -e 16 -d 0 -r 0 -k 0123456789abcdef0123456789abcdef AA:BB:CC:DD:EE:FF**
 
 irks
 ----
Signature · 1 line
2.54.0
#2 lore raw
In reply to Luiz Augusto von Dentz

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=1105509

---Test result---

Test Summary:

CheckPatch                    PASS      0.88 seconds
GitLint                       PASS      0.58 seconds
BuildEll                      PASS      17.95 seconds
BluezMake                     PASS      654.52 seconds
MakeCheck                     PASS      18.33 seconds
MakeDistcheck                 PASS      223.87 seconds
CheckValgrind                 PASS      277.86 seconds
CheckSmatch                   PASS      312.38 seconds
bluezmakeextell               PASS      166.67 seconds
IncrementalBuild              PASS      636.29 seconds
ScanBuild                     PASS      923.83 seconds

https://github.com/bluez/bluez/pull/2173

---
Regards,
Linux Bluetooth