Skip to contentSkip to conversation

tools/btgatt-server: Fix sign counter

PATCH New Luiz Augusto von Dentz 1 participant 5 messages Feb 25, 2015 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/5 tools/btgatt-server: Fix sign counter

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

The sing counter should be set with the counter received not incremented by one since the check is for > the counter received may be bigger than the increment making the counters go out of sync.

---
 tools/btgatt-server.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/btgatt-server.c +1 -1
index 09a123c..9eeea3b 100644
@@ -1004,7 +1004,7 @@ static bool remote_counter(uint32_t *sign_cnt, void *user_data)
 	if (*sign_cnt < cnt)
 		return false;
 
-	cnt++;
+	cnt = *sign_cnt;
 
 	return true;
 }
Signature · 1 line
2.1.0
author maintainer
#1 lore raw

PATCH 2/5 core: Store signature counters

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

This stores signature counters in LocalSignatureCounter and RemoteSignatureCounter entries so they can be reloaded later.

---
 src/adapter.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
src/adapter.c +3 -2
index 3353297..32faa26 100644
@@ -6156,7 +6156,7 @@ static void new_long_term_key_callback(uint16_t index, uint16_t length,
 
 static void store_csrk(const bdaddr_t *local, const bdaddr_t *peer,
 				uint8_t bdaddr_type, const unsigned char *key,
-				uint8_t master)
+				uint32_t counter, uint8_t master)
 {
 	const char *group;
 	char adapter_addr[18];
@@ -6190,6 +6190,7 @@ static void store_csrk(const bdaddr_t *local, const bdaddr_t *peer,
 		sprintf(key_str + (i * 2), "%2.2X", key[i]);
 
 	g_key_file_set_string(key_file, group, "Key", key_str);
+	g_key_file_set_integer(key_file, group, "Counter", counter);
 
 	create_file(filename, S_IRUSR | S_IWUSR);
 
@@ -6230,7 +6231,7 @@ static void new_csrk_callback(uint16_t index, uint16_t length,
 	if (!ev->store_hint)
 		return;
 
-	store_csrk(bdaddr, &key->addr.bdaddr, key->addr.type, key->val,
+	store_csrk(bdaddr, &key->addr.bdaddr, key->addr.type, key->val, 0,
 								key->master);
 
 	if (device_is_temporary(device))
Signature · 1 line
2.1.0
author maintainer
#2 lore raw

PATCH 3/5 core: Load CSRK from storage

In reply to Luiz Augusto von Dentz

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

This loads both Local and Remote CSRK information from storage so it can be used later.

---
 src/device.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
src/device.c +44 -0
index 002dbea..bef1f0c 100644
@@ -173,6 +173,11 @@ struct bearer_state {
 	bool svc_resolved;
 };
 
+struct csrk_info {
+	uint8_t key[16];
+	uint32_t counter;
+};
+
 struct btd_device {
 	int ref_count;
 
@@ -231,6 +236,9 @@ struct btd_device {
 	struct bearer_state bredr_state;
 	struct bearer_state le_state;
 
+	struct csrk_info *local_csrk;
+	struct csrk_info *remote_csrk;
+
 	sdp_list_t	*tmp_records;
 
 	time_t		bredr_seen;
@@ -627,6 +635,8 @@ static void device_free(gpointer user_data)
 	if (device->eir_uuids)
 		g_slist_free_full(device->eir_uuids, g_free);
 
+	g_free(device->local_csrk);
+	g_free(device->remote_csrk);
 	g_free(device->path);
 	g_free(device->alias);
 	free(device->modalias);
@@ -2280,6 +2290,37 @@ failed:
 	return str;
 }
 
+static void load_csrk(struct csrk_info **csrk, GKeyFile *key_file,
+							const char *group)
+{
+	char *str;
+	int i;
+	uint8_t *key;
+
+	str = g_key_file_get_string(key_file, group, "Key", NULL);
+	if (!str)
+		return;
+
+	*csrk = g_new0(struct csrk_info, 1);
+	key = (*csrk)->key;
+
+	for (i = 0; i < 16; i++) {
+		if (sscanf(str + (i * 2), "%2hhx", &key[i]) != 1)
+			goto fail;
+	}
+
+	(*csrk)->counter = g_key_file_get_integer(key_file, group, "Counter",
+									NULL);
+	g_free(str);
+
+	return;
+
+fail:
+	g_free(str);
+	g_free(*csrk);
+	*csrk = NULL;
+}
+
 static void load_info(struct btd_device *device, const char *local,
 			const char *peer, GKeyFile *key_file)
 {
@@ -2355,6 +2396,9 @@ static void load_info(struct btd_device *device, const char *local,
 			error("Unknown LE device technology");
 
 		g_free(str);
+
+		load_csrk(&device->local_csrk, key_file, "LocalSignatureKey");
+		load_csrk(&device->remote_csrk, key_file, "RemoteSignatureKey");
 	}
 
 	g_strfreev(techno);
Signature · 1 line
2.1.0
author maintainer
#3 lore raw

PATCH 4/5 core/device: Set Local/Remote CSRK

In reply to Luiz Augusto von Dentz

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

This set Local/Remote CSRK as keys to bt_att so it can use for signed writes.

---
 src/device.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
src/device.c +32 -0
index bef1f0c..a801bb3 100644
@@ -4012,6 +4012,30 @@ static void gatt_server_init(struct btd_device *device, struct gatt_db *db)
 		error("Failed to initialize bt_gatt_server");
 }
 
+static bool local_counter(uint32_t *sign_cnt, void *user_data)
+{
+	struct btd_device *dev = user_data;
+
+	if (!dev->local_csrk)
+		return false;
+
+	*sign_cnt = dev->local_csrk->counter++;
+
+	return true;
+}
+
+static bool remote_counter(uint32_t *sign_cnt, void *user_data)
+{
+	struct btd_device *dev = user_data;
+
+	if (!dev->remote_csrk || *sign_cnt < dev->remote_csrk->counter)
+		return false;
+
+	dev->remote_csrk->counter = *sign_cnt;
+
+	return true;
+}
+
 bool device_attach_att(struct btd_device *dev, GIOChannel *io)
 {
 	GError *gerr = NULL;
@@ -4061,6 +4085,14 @@ bool device_attach_att(struct btd_device *dev, GIOChannel *io)
 						att_disconnected_cb, dev, NULL);
 	bt_att_set_close_on_unref(dev->att, true);
 
+	if (dev->local_csrk)
+		bt_att_set_local_key(dev->att, dev->local_csrk->key,
+							local_counter, dev);
+
+	if (dev->remote_csrk)
+		bt_att_set_remote_key(dev->att, dev->remote_csrk->key,
+							remote_counter, dev);
+
 	database = btd_adapter_get_database(dev->adapter);
 
 	gatt_client_init(dev);
Signature · 1 line
2.1.0
author maintainer
#4 lore raw

PATCH 5/5 core/device: Update storage if CSRK counter change

In reply to Luiz Augusto von Dentz

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

This makes sure the counter is stored so once the device is loaded again it should contain the proper value.

---
 src/device.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
src/device.c +23 -0
index a801bb3..20aaa3e 100644
@@ -345,6 +345,19 @@ static void update_technologies(GKeyFile *file, struct btd_device *dev)
 								list, len);
 }
 
+static void store_csrk(struct csrk_info *csrk, GKeyFile *key_file,
+							const char *group)
+{
+	char key[33];
+	int i;
+
+	for (i = 0; i < 16; i++)
+		sprintf(key + (i * 2), "%2.2X", csrk->key[i]);
+
+	g_key_file_set_string(key_file, group, "Key", key);
+	g_key_file_set_integer(key_file, group, "Counter", csrk->counter);
+}
+
 static gboolean store_device_info_cb(gpointer user_data)
 {
 	struct btd_device *device = user_data;
@@ -423,6 +436,12 @@ static gboolean store_device_info_cb(gpointer user_data)
 		g_key_file_remove_group(key_file, "DeviceID", NULL);
 	}
 
+	if (device->local_csrk)
+		store_csrk(device->local_csrk, key_file, "LocalSignatureKey");
+
+	if (device->remote_csrk)
+		store_csrk(device->remote_csrk, key_file, "RemoteSignatureKey");
+
 	create_file(filename, S_IRUSR | S_IWUSR);
 
 	str = g_key_file_to_data(key_file, &length, NULL);
@@ -4021,6 +4040,8 @@ static bool local_counter(uint32_t *sign_cnt, void *user_data)
 
 	*sign_cnt = dev->local_csrk->counter++;
 
+	store_device_info(dev);
+
 	return true;
 }
 
@@ -4033,6 +4054,8 @@ static bool remote_counter(uint32_t *sign_cnt, void *user_data)
 
 	dev->remote_csrk->counter = *sign_cnt;
 
+	store_device_info(dev);
+
 	return true;
 }
 
Signature · 1 line
2.1.0