Skip to contentSkip to conversation

mesh: add Keyring to CreateNetwork

PATCH In review Brian Gix 1 participant 5 messages May 14, 2019 – May 15, 2019 Bluetooth drivers

lore
Share & tools
lore

Brian Gix

author
#0 lore raw

cover mesh: add Keyring to CreateNetwork

Patch-set also fixes some file creation permissions and adds CreateNetwork test.

Brian Gix (3):
mesh: Add "create" command to test app
mesh: Fix file open error checking and permissions
mesh: Save generated Net and Dev keys in KeyRing

 mesh/keyring.c |  8 +++++---
 mesh/node.c    | 20 ++++++++++++++++----
 mesh/storage.c |  2 +-
 test/test-mesh | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 66 insertions(+), 10 deletions(-)
Signature · 1 line
2.14.5
author
#1 lore raw

PATCH 1/3 mesh: Add "create" command to test app

Like "join" this command generates a UUID and calls CreateNetwork() and displays the UUID and token.

---
 test/test-mesh | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 44 insertions(+), 2 deletions(-)
test/test-mesh +44 -2
index 7201669a8..f1d2b4e39 100755
@@ -23,6 +23,7 @@
 #
 # The main menu:
 #       token
+#       create
 #       join
 #       attach
 #       remove
@@ -37,6 +38,15 @@
 #            The token can be set from command line arguments as
 #            well.
 #
+#     create
+#            Creates a new mesh network, with it's first local
+#            node. The test generates device UUID to store the node
+#            as.
+#            In case of successful creation, the application
+#            automatically attaches as a node to the daemon. A node
+#            'token' is returned to the application and is used
+#            for the runtime of the test.
+#
 #     join
 #            Request provisioning of a device to become a node
 #            on a mesh network. The test generates device UUID
@@ -57,8 +67,8 @@
 #            For the call to be successful, the valid node token must
 #            be already set, either from command arguments or by
 #            executing "set token" operation or automatically after
-#            successfully executing "join" operation in the same test
-#            run.
+#            successfully executing "join" or "create" operation in
+#            the same test run.
 #
 #     remove
 #           Permanently removes any node configuration from daemon
@@ -241,6 +251,22 @@ def join_cb():
 def join_error_cb(reason):
 	print('Join procedure failed: ', reason)
 
+def create_cb(value):
+	global token
+	global have_token
+	global attach
+
+	print(set_yellow('Created mesh network with token ') +
+			set_green(format(value, '16x')))
+
+	token = value
+	have_token = True
+	if attached == False:
+		attach(token)
+
+def create_error_cb(reason):
+	print('Create procedure failed: ', reason)
+
 def remove_node_cb():
 	global attached
 	global have_token
@@ -737,6 +763,8 @@ class MainMenu(Menu):
 		menu_items = {
 			'token': MenuItem(' - set node ID (token)',
 						self.__cmd_set_token),
+			'create': MenuItem(' - create mesh network',
+						self.__cmd_create),
 			'join': MenuItem(' - join mesh network',
 						self.__cmd_join),
 			'attach': MenuItem(' - attach mesh node',
@@ -790,6 +818,20 @@ class MainMenu(Menu):
 		user_input = INPUT_MESSAGE_PAYLOAD;
 		print(set_cyan('Enter message payload (hex):'))
 
+	def __cmd_create(self):
+		if agent == None:
+			print(set_error('Provisioning agent not found'))
+			return
+
+		uuid = bytearray.fromhex("0a0102030405060708090A0B0C0D0E0F")
+		random.shuffle(uuid)
+		uuid_str = array_to_string(uuid)
+
+		print(set_yellow('Creating with UUID ') + set_green(uuid_str))
+		mesh_net.CreateNetwork(app.get_path(), uuid,
+			reply_handler=create_cb,
+			error_handler=create_error_cb)
+
 	def __cmd_join(self):
 		if agent == None:
 			print(set_error('Provisioning agent not found'))
Signature · 1 line
2.14.5
author
#2 lore raw

PATCH 2/3 mesh: Fix file open error checking and permissions

In reply to Brian Gix
---
 mesh/keyring.c | 8 +++++---
 mesh/storage.c | 2 +-
 2 files changed, 6 insertions(+), 4 deletions(-)
mesh/keyring.c +5 -3
index 59aa1eaf4..4b1460aa8 100644
@@ -67,7 +67,7 @@ bool keyring_put_net_key(struct mesh_node *node, uint16_t net_idx,
 								net_idx);
 	l_debug("Put Net Key %s", key_file);
 
-	fd = open(key_file, O_WRONLY | O_CREAT | O_TRUNC);
+	fd = open(key_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
 	if (fd >= 0) {
 		if (write(fd, key, sizeof(*key)) == sizeof(*key))
 			result = true;
@@ -112,7 +112,8 @@ bool keyring_put_app_key(struct mesh_node *node, uint16_t app_idx,
 		}
 		lseek(fd, 0, SEEK_SET);
 	} else
-		fd = open(key_file, O_WRONLY | O_CREAT | O_TRUNC);
+		fd = open(key_file, O_WRONLY | O_CREAT | O_TRUNC,
+							S_IRUSR | S_IWUSR);
 
 	if (fd >= 0) {
 		if (write(fd, key, sizeof(*key)) == sizeof(*key))
@@ -148,7 +149,8 @@ bool keyring_put_remote_dev_key(struct mesh_node *node, uint16_t unicast,
 						dev_key_dir, unicast + i);
 		l_debug("Put Dev Key %s", key_file);
 
-		fd = open(key_file, O_WRONLY | O_CREAT | O_TRUNC);
+		fd = open(key_file, O_WRONLY | O_CREAT | O_TRUNC,
+							S_IRUSR | S_IWUSR);
 		if (fd >= 0) {
 			if (write(fd, dev_key, 16) != 16)
 				result = false;
mesh/storage.c +1 -1
index f4e23bf49..8893b93cb 100644
@@ -197,7 +197,7 @@ static bool parse_config(char *in_file, char *out_dir, const uint8_t uuid[16])
 	l_info("Loading configuration from %s", in_file);
 
 	fd = open(in_file, O_RDONLY);
-	if (!fd)
+	if (fd < 0)
 		return false;
 
 	if (fstat(fd, &st) == -1) {
Signature · 1 line
2.14.5
author
#3 lore raw

PATCH 3/3 mesh: Save generated Net and Dev keys in KeyRing

In reply to Brian Gix

When creating a network from scratch, we need to save all keys that are created into the KeyRing for use by Config Client application.

---
 mesh/node.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
mesh/node.c +16 -4
index 3618595b3..5318e2b69 100644
@@ -35,6 +35,7 @@
 #include "mesh/mesh-db.h"
 #include "mesh/provision.h"
 #include "mesh/storage.h"
+#include "mesh/keyring.h"
 #include "mesh/appkey.h"
 #include "mesh/model.h"
 #include "mesh/cfgmod.h"
@@ -1549,8 +1550,8 @@ static void get_managed_objects_cb(struct l_dbus_message *msg, void *user_data)
 	} else {
 		/* Callback for create node request */
 		node_ready_func_t cb = req->cb;
+		struct keyring_net_key net_key;
 		uint8_t dev_key[16];
-		uint8_t net_key[16];
 
 		node->num_ele = num_ele;
 		set_defaults(node);
@@ -1561,11 +1562,22 @@ static void get_managed_objects_cb(struct l_dbus_message *msg, void *user_data)
 
 		/* Generate device and primary network keys */
 		l_getrandom(dev_key, sizeof(dev_key));
-		l_getrandom(net_key, sizeof(net_key));
+		l_getrandom(net_key.old_key, sizeof(net_key.old_key));
+		net_key.net_idx = DEFAULT_PRIMARY_NET_INDEX;
+		net_key.phase = 0;
 
 		if (!add_local_node(node, DEFAULT_NEW_UNICAST, false, false,
-					DEFAULT_IV_INDEX, dev_key,
-					DEFAULT_PRIMARY_NET_INDEX, net_key))
+						DEFAULT_IV_INDEX, dev_key,
+						DEFAULT_PRIMARY_NET_INDEX,
+							net_key.old_key))
+			goto fail;
+
+		if (!keyring_put_remote_dev_key(node, DEFAULT_NEW_UNICAST,
+							num_ele, dev_key))
+			goto fail;
+
+		if (!keyring_put_net_key(node, DEFAULT_PRIMARY_NET_INDEX,
+								&net_key))
 			goto fail;
 
 		cb(req->user_data, MESH_ERROR_NONE, node);
Signature · 1 line
2.14.5
author
#4 lore raw
In reply to Brian Gix

Patch-set applied, with minor edit to test-mesh comments

Quoted 14 lines · Brian Gix

Patch-set also fixes some file creation permissions and adds CreateNetwork test.

Brian Gix (3):
mesh: Add "create" command to test app
mesh: Fix file open error checking and permissions
mesh: Save generated Net and Dev keys in KeyRing

 mesh/keyring.c |  8 +++++---
 mesh/node.c    | 20 ++++++++++++++++----
 mesh/storage.c |  2 +-
 test/test-mesh | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 66 insertions(+), 10 deletions(-)