Skip to contentSkip to conversation

mesh: Streamline Key Refresh finalization

PATCH Superseded Brian Gix 1 participant 3 messages Sep 5, 2019 Bluetooth drivers

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Brian Gix

author
#0 lore raw

v2 cover mesh: Streamline Key Refresh finalization

All App key refresh finalization will happen when the bound Net Key is finalized.

Version 2: Style guide corrections, and stricter adherence to rules as specified in mesh-api.txt

Brian Gix (2):
doc: Remove uneeded dbus API for App Key Refresh
mesh: Automate AppKey update on KR phase 2-->3-->0

 doc/mesh-api.txt | 19 --------------
 mesh/keyring.c   | 66 ++++++++++++++++++++++++++++++++++++++++++++++++
 mesh/keyring.h   |  1 +
 mesh/manager.c   | 43 +++++++++----------------------
 4 files changed, 79 insertions(+), 50 deletions(-)
Signature · 1 line
2.21.0
author
#1 lore raw

PATCH v2 1/2 doc: Remove uneeded dbus API for App Key Refresh

---
 doc/mesh-api.txt | 19 -------------------
 1 file changed, 19 deletions(-)
doc/mesh-api.txt +0 -19
index 470751f7a..9b9f4e3de 100644
@@ -627,25 +627,6 @@ Methods:
 			org.bluez.mesh.Error.DoesNotExist
 			org.bluez.mesh.Error.Busy
 
-	void CompleteAppKeyUpdate(uint16 app_index)
-
-		This method is used by the application at the completion of
-		a Key Refresh Procedure.  This method should be called for each
-		app key being updated during the procedure, and must be
-		performed prior to changing the phase of the bound net key
-		to phase 3. (See SetKeyPhase() method).
-
-		The app_index parameter is a 12-bit value (0x000-0xFFF)
-		specifying which app key was updated. Note that the subnet that
-		the key is bound to must exist and be in Phase 2.
-
-		This call affects the local bluetooth-meshd key database only.
-
-		PossibleErrors:
-			org.bluez.mesh.Error.Failed
-			org.bluez.mesh.Error.InvalidArguments
-			org.bluez.mesh.Error.DoesNotExist
-
 	void DeleteAppKey(uint16 app_index)
 
 		This method is used by the application to delete an application
Signature · 1 line
2.21.0
author
#2 lore raw

PATCH v2 2/2 mesh: Automate AppKey update on KR phase 2-->3-->0

In reply to Brian Gix

Designed so that if an unexpcted abort() occurs, the bound NetKey remains in state 2 or 3. If successful, the NetKey is set to Phase 0, and all bound AppKeys are in their correct state.

---
 mesh/keyring.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++
 mesh/keyring.h |  1 +
 mesh/manager.c | 43 +++++++++-----------------------
 3 files changed, 79 insertions(+), 31 deletions(-)
mesh/keyring.c +66 -0
index 4b3d8b296..5011c1442 100644
@@ -23,6 +23,7 @@
 
 #define _GNU_SOURCE
 #include <fcntl.h>
+#include <dirent.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <dirent.h>
@@ -105,6 +106,7 @@ bool keyring_put_app_key(struct mesh_node *node, uint16_t app_idx,
 				return false;
 			}
 		}
+
 		lseek(fd, 0, SEEK_SET);
 	} else
 		fd = open(key_file, O_WRONLY | O_CREAT | O_TRUNC,
@@ -120,6 +122,70 @@ bool keyring_put_app_key(struct mesh_node *node, uint16_t app_idx,
 	return result;
 }
 
+static int finalize(const char *fpath, uint16_t net_idx)
+{
+	struct keyring_app_key key;
+	bool result = false;
+	int fd;
+
+	fd = open(fpath, O_RDWR);
+	if (fd >= 0) {
+		if (read(fd, &key, sizeof(key)) == sizeof(key)) {
+			if (key.net_idx != net_idx) {
+				close(fd);
+				return 0;
+			}
+		}
+
+		l_debug("Finalize %s", fpath);
+
+		memcpy(key.old_key, key.new_key, 16);
+		lseek(fd, 0, SEEK_SET);
+
+		if (write(fd, &key, sizeof(key)) == sizeof(key))
+			result = true;
+
+		close(fd);
+	}
+
+	return result ? 0 : -1;
+}
+
+bool keyring_finalize_app_keys(struct mesh_node *node, uint16_t net_idx)
+{
+	const char *node_path;
+	char key_dir[PATH_MAX];
+	DIR *dir;
+	struct dirent *entry;
+
+	if (!node)
+		return false;
+
+	node_path = node_get_storage_dir(node);
+
+	if (strlen(node_path) + strlen(app_key_dir) + 1 >= PATH_MAX)
+		return false;
+
+	snprintf(key_dir, PATH_MAX, "%s%s", node_path, app_key_dir);
+	dir = opendir(key_dir);
+	if (!dir) {
+		l_error("Failed to App Key storage directory: %s", key_dir);
+		return false;
+	}
+
+	while ((entry = readdir(dir)) != NULL) {
+		/* AppKeys are stored in regular files */
+		if (entry->d_type != DT_REG)
+			continue;
+
+		finalize(entry->d_name, net_idx);
+	}
+
+	closedir(dir);
+
+	return true;
+}
+
 bool keyring_put_remote_dev_key(struct mesh_node *node, uint16_t unicast,
 					uint8_t count, uint8_t dev_key[16])
 {
mesh/keyring.h +1 -0
index 167191013..2fab6b0dc 100644
@@ -38,6 +38,7 @@ bool keyring_get_net_key(struct mesh_node *node, uint16_t net_idx,
 bool keyring_del_net_key(struct mesh_node *node, uint16_t net_idx);
 bool keyring_put_app_key(struct mesh_node *node, uint16_t app_idx,
 				uint16_t net_idx, struct keyring_app_key *key);
+bool keyring_finalize_app_keys(struct mesh_node *node, uint16_t net_id);
 bool keyring_get_app_key(struct mesh_node *node, uint16_t app_idx,
 						struct keyring_app_key *key);
 bool keyring_del_app_key(struct mesh_node *node, uint16_t app_idx);
mesh/manager.c +12 -31

70 lines not on this page · raw message

Signature · 1 line
2.21.0