Skip to contentSkip to conversation

mesh: Add App and Net Key Refresh keyring back-end

PATCH Superseded Brian Gix 2 participants 3 messages May 21, 2019 – May 22, 2019 Bluetooth drivers

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Brian Gix

author
#0 lore raw

PATCH 1/1 mesh: Add App and Net Key Refresh keyring back-end

Fill in skeleton App Key and Net Key KeyRing methods that are affected by the Key Refresh Procedure.

---
 doc/mesh-api.txt |   2 +
 mesh/manager.c   | 198 ++++++++++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 170 insertions(+), 30 deletions(-)
doc/mesh-api.txt +2 -0
index 112990a5d..9644553e8 100644
@@ -434,6 +434,7 @@ Methods:
 		PossibleErrors:
 			org.bluez.mesh.Error.InvalidArguments
 			org.bluez.mesh.Error.DoesNotExist
+			org.bluez.mesh.Error.Busy
 
 	void DeleteSubnet(uint16 net_index)
 
@@ -527,6 +528,7 @@ Methods:
 		PossibleErrors:
 			org.bluez.mesh.Error.InvalidArguments
 			org.bluez.mesh.Error.DoesNotExist
+			org.bluez.mesh.Error.Busy
 
 	void DeleteAppKey(uint16 app_index)
 
mesh/manager.c +168 -30

314 lines not on this page · raw message

Signature · 1 line
2.14.5
#1 lore raw

Hi Brian,

Quoted 3 lines · Brian Gix

Fill in skeleton App Key and Net Key KeyRing methods that are affected
by the Key Refresh Procedure.

Would be nice to list the implemented methods here.
I would strongly recommned breaking this in to two, maybe even three patches:
net key methods, app key methods, phase method

Quoted 51 lines
---
 doc/mesh-api.txt |   2 +
 mesh/manager.c   | 198
++++++++++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 170 insertions(+), 30 deletions(-)
doc/mesh-api.txt +2 -0
index 112990a5d..9644553e8 100644
@@ -434,6 +434,7 @@ Methods:
 		PossibleErrors:
 			org.bluez.mesh.Error.InvalidArguments
 			org.bluez.mesh.Error.DoesNotExist
+			org.bluez.mesh.Error.Busy
 
 	void DeleteSubnet(uint16 net_index)
 
@@ -527,6 +528,7 @@ Methods:
 		PossibleErrors:
 			org.bluez.mesh.Error.InvalidArguments
 			org.bluez.mesh.Error.DoesNotExist
+			org.bluez.mesh.Error.Busy
 
 	void DeleteAppKey(uint16 app_index)
 
mesh/manager.c +5 -0
index d990ceec2..8ef32d735 100644
@@ -24,9 +24,12 @@
 #define _GNU_SOURCE
 #include <ell/ell.h>
 
+#include "mesh/mesh-defs.h"
 #include "mesh/dbus.h"
 #include "mesh/error.h"
 #include "mesh/mesh.h"
+#include "mesh/node.h"
+#include "mesh/keyring.h"
 #include "mesh/manager.h"
 
 static struct l_dbus_message *add_node_call(struct l_dbus *dbus,
@@ -86,51 +89,110 @@ static struct l_dbus_message
 *cancel_scan_call(struct l_dbus *dbus,
 	return dbus_error(msg, MESH_ERROR_NOT_IMPLEMENTED, NULL);
 }
 
+static struct l_dbus_message *store_new_subnet(struct mesh_node
 *node,
+					struct l_dbus_message *msg,

mesh/manager.c static struct l_dbus_message · line 105

Quoted 10 lines
mesh/manager.c +9 -0
+					uint16_t net_idx, uint8_t
 *new_key)
+{
+	struct keyring_net_key key;
+
+	if (net_idx > MAX_KEY_IDX)
+		return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
+
+	if (keyring_get_net_key(node, net_idx, &key)) {
+		/* Allow redundent calls only if they match */

Allow redundant calls only if the key values match

Quoted 20 lines
mesh/manager.c +16 -0
+		if (!memcmp(key.old_key, new_key, 16))
+			return l_dbus_message_new_method_return(msg);
+
+		return dbus_error(msg, MESH_ERROR_ALREADY_EXISTS,
 NULL);
+	}
+
+	memcpy(key.old_key, new_key, 16);
+	key.net_idx = net_idx;
+	key.phase = KEY_REFRESH_PHASE_NONE;
+
+	if (!keyring_put_net_key(node, net_idx, &key))
+		return dbus_error(msg, MESH_ERROR_FAILED, NULL);
+
+	return l_dbus_message_new_method_return(msg);
+}
+
 static struct l_dbus_message *create_subnet_call(struct l_dbus
 *dbus,
 						struct l_dbus_message

mesh/manager.c static struct l_dbus_message *cancel_scan_call(struct l_dbus *dbus,

Quoted 10 lines
mesh/manager.c +3 -1
 *msg,
 						void *user_data)
 {
+	struct mesh_node *node = user_data;
+	uint8_t key[16];
 	uint16_t net_idx;
 
-	if (!l_dbus_message_get_arguments(msg, "q", &net_idx))
+	if (!l_dbus_message_get_arguments(msg, "q", &net_idx) ||
 !net_idx)

Maybe use explicit value for net_idx check, i.e.,
net_idx == MESH_PRIMARY_NET_INDEX_DEFAULT

Currently DEFAULT_PRIMARY_NET_INDEX is defined in node.c WE should move it into mesh-defs.h and rename accordingly

Quoted 26 lines
mesh/manager.c +8 -4
 		return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
 
-	/* TODO */
-	return dbus_error(msg, MESH_ERROR_NOT_IMPLEMENTED, NULL);
+	/* Generate key and submit */
+	l_getrandom(key, sizeof(key));
+
+	return store_new_subnet(node, msg, net_idx, key);
 }
 
 static struct l_dbus_message *update_subnet_call(struct l_dbus
 *dbus,
 						struct l_dbus_message
 *msg,
 						void *user_data)
 {
+	struct mesh_node *node = user_data;
+	struct keyring_net_key key;
 	uint16_t net_idx;
 
-	if (!l_dbus_message_get_arguments(msg, "q", &net_idx))
+	if (!l_dbus_message_get_arguments(msg, "q", &net_idx) ||
+						net_idx > MAX_KEY_IDX)
 		return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
 
-	/* TODO */

mesh/manager.c static struct l_dbus_message *cancel_scan_call(struct l_dbus *dbus, · line 155

Quoted 10 lines
mesh/manager.c +8 -1
-	return dbus_error(msg, MESH_ERROR_NOT_IMPLEMENTED, NULL);
+	if (!keyring_get_net_key(node, net_idx, &key))
+		return dbus_error(msg, MESH_ERROR_DOES_NOT_EXIST,
 NULL);
+
+	switch (key.phase) {
+	case KEY_REFRESH_PHASE_NONE:
+		/* Generate Key and update phase */
+		l_getrandom(key.new_key, sizeof(key.new_key));
+		key.phase = KEY_REFRESH_PHASE_ONE;

line break?

mesh/manager.c static struct l_dbus_message · line 168

Quoted 3 lines
mesh/manager.c +2 -0
+		if (!keyring_put_net_key(node, net_idx, &key))
+			return dbus_error(msg, MESH_ERROR_FAILED,
 NULL);

Wouldn't we want to revert the key phase back to KEY_REFRESH_PHASE_NONE in case of failure?

mesh/manager.c static struct l_dbus_message *cancel_scan_call(struct l_dbus *dbus, · line 162

Quoted 5 lines
mesh/manager.c +5 -0
+
+		/* Fall Through */
+
+	case KEY_REFRESH_PHASE_ONE:
+		/* Allow redundent calls to start Key Refresh */

"redundant"

mesh/manager.c static struct l_dbus_message *cancel_scan_call(struct l_dbus *dbus, · line 170

Quoted 8 lines
mesh/manager.c +8 -0
+		return l_dbus_message_new_method_return(msg);
+
+	default:
+		break;
+	}
+
+	/* All other phases mean KR already in progress over-the-air */
+	return dbus_error(msg, MESH_ERROR_BUSY, "KR in progress");

KR -> Key refresh

Quoted 53 lines
mesh/manager.c +6 -3
 }
 
 static struct l_dbus_message *delete_subnet_call(struct l_dbus
 *dbus,
 						struct l_dbus_message
 *msg,
 						void *user_data)
 {
+	struct mesh_node *node = user_data;
 	uint16_t net_idx;
 
-	if (!l_dbus_message_get_arguments(msg, "q", &net_idx))
+	if (!l_dbus_message_get_arguments(msg, "q", &net_idx) ||
+						net_idx > MAX_KEY_IDX)
 		return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
 
-	/* TODO */
-	return dbus_error(msg, MESH_ERROR_NOT_IMPLEMENTED, NULL);
+	keyring_del_net_key(node, net_idx);
+
+	return l_dbus_message_new_method_return(msg);
 }
 
 static struct l_dbus_message *import_subnet_call(struct l_dbus

*dbus,
struct l_dbus_message
*msg,

mesh/manager.c +9 -3
 						void *user_data)
 {
-	uint16_t net_idx;
+	struct mesh_node *node = user_data;
 	struct l_dbus_message_iter iter_key;
+	uint16_t net_idx;
 	uint8_t *key;
 	uint32_t n;
@@ -142,55 +204,116 @@ static struct l_dbus_message
 *import_subnet_call(struct l_dbus *dbus,
 		return dbus_error(msg, MESH_ERROR_INVALID_ARGS,
 							"Bad network
 key");
 
-	/* TODO */
-	return dbus_error(msg, MESH_ERROR_NOT_IMPLEMENTED, NULL);
+	return store_new_subnet(node, msg, net_idx, key);
+}
+
+static struct l_dbus_message *store_new_appkey(struct mesh_node
 *node,
+					struct l_dbus_message *msg,
+					uint16_t net_idx, uint16_t
 app_idx,
+					uint8_t *new_key)

mesh/manager.c static struct l_dbus_message · line 227

Quoted 10 lines
mesh/manager.c +9 -0
+{
+	struct keyring_net_key net_key;
+	struct keyring_app_key app_key;
+
+	if (net_idx > MAX_KEY_IDX || app_idx > MAX_KEY_IDX)
+		return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
+
+	if (!keyring_get_net_key(node, net_idx, &net_key))
+		return dbus_error(msg, MESH_ERROR_DOES_NOT_EXIST,
 NULL);

NULL -> "Bound net key not found"
Also, I don't think "org.bluez.mesh.Error.DoesNotExist" error is documented in mesh-api.txt for either CreateAppKey() or ImportAppKey(), needs to be added.

mesh/manager.c static struct l_dbus_message *import_subnet_call(struct l_dbus *dbus, · line 225

Quoted 3 lines
mesh/manager.c +3 -0
+
+	if (keyring_get_app_key(node, app_idx, &app_key)) {
+		/* Allow redundent calls with identical data */

"Allow redundant calls with identical values"

Quoted 59 lines
mesh/manager.c +26 -5
+		if (!memcmp(app_key.old_key, new_key, 16) &&
+						app_key.net_idx ==
 net_idx)
+			return l_dbus_message_new_method_return(msg);
+
+		return dbus_error(msg, MESH_ERROR_ALREADY_EXISTS,
 NULL);
+	}
+
+	memcpy(app_key.old_key, new_key, 16);
+	app_key.net_idx = net_idx;
+	app_key.app_idx = app_idx;
+
+	if (!keyring_put_app_key(node, app_idx, net_idx, &app_key))
+		return dbus_error(msg, MESH_ERROR_FAILED, NULL);
+
+	return l_dbus_message_new_method_return(msg);
 }
 
 static struct l_dbus_message *create_appkey_call(struct l_dbus
 *dbus,
 						struct l_dbus_message
 *msg,
 						void *user_data)
 {
+	struct mesh_node *node = user_data;
 	uint16_t net_idx, app_idx;
+	uint8_t key[16];
 
 	if (!l_dbus_message_get_arguments(msg, "qq", &net_idx,
 &app_idx))
 		return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
 
-	/* TODO */
-	return dbus_error(msg, MESH_ERROR_NOT_IMPLEMENTED, NULL);
+	l_getrandom(key, sizeof(key));
+
+	return store_new_appkey(node, msg, net_idx, app_idx, key);
 }
 
 static struct l_dbus_message *update_appkey_call(struct l_dbus
 *dbus,
 						struct l_dbus_message
 *msg,
 						void *user_data)
 {
-	uint16_t net_idx, app_idx;
+	struct mesh_node *node = user_data;
+	struct keyring_net_key net_key;
+	struct keyring_app_key app_key;
+	uint16_t app_idx;
 
-	if (!l_dbus_message_get_arguments(msg, "qq", &net_idx,
 &app_idx))
+	if (!l_dbus_message_get_arguments(msg, "q", &app_idx) ||
+						app_idx > MAX_KEY_IDX)
 		return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
 
-	/* TODO */

mesh/manager.c static struct l_dbus_message *import_subnet_call(struct l_dbus *dbus, · line 278

Quoted 10 lines
mesh/manager.c +7 -1
-	return dbus_error(msg, MESH_ERROR_NOT_IMPLEMENTED, NULL);
+	if (!keyring_get_app_key(node, app_idx, &app_key) ||
+			!keyring_get_net_key(node, app_key.net_idx,
 &net_key))
+		return dbus_error(msg, MESH_ERROR_DOES_NOT_EXIST,
 NULL);
+
+	switch (net_key.phase) {
+	case KEY_REFRESH_PHASE_NONE:
+	case KEY_REFRESH_PHASE_ONE:

What if the net key is in key refresh phase one and This method has been called mre than once? i.e., do we want to protect the application from potentially overwriting already updated application key?

mesh/manager.c static struct l_dbus_message *import_subnet_call(struct l_dbus *dbus, · line 280

Quoted 2 lines
mesh/manager.c +2 -0
+		/* Generate Key if in acceptable phase */
+		l_getrandom(app_key.new_key, sizeof(app_key.new_key));

line break

Quoted 5 lines
mesh/manager.c +3 -0
+		if (!keyring_put_app_key(node, app_idx,
 app_key.net_idx,
+								&app_ke
 y))
+			return dbus_error(msg, MESH_ERROR_FAILED,

mesh/manager.c static struct l_dbus_message *import_subnet_call(struct l_dbus *dbus, · line 292

Quoted 10 lines
mesh/manager.c +9 -0
 NULL);
+
+		return l_dbus_message_new_method_return(msg);
+
+	default:
+		break;
+	}
+
+	/* Don't allow Updates from invalid phase */
+	return dbus_error(msg, MESH_ERROR_BUSY, "KR in progress");

KR -> Key refresh

Quoted 77 lines
mesh/manager.c +2 -2
 }
 
 static struct l_dbus_message *delete_appkey_call(struct l_dbus
 *dbus,
 						struct l_dbus_message
 *msg,
 						void *user_data)
 {
-	uint16_t net_idx, app_idx;
+	struct mesh_node *node = user_data;
+	uint16_t app_idx;
 
-	if (!l_dbus_message_get_arguments(msg, "qq", &net_idx,

&app_idx))

mesh/manager.c +4 -2
+	if (!l_dbus_message_get_arguments(msg, "q", &app_idx))
 		return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
 
-	/* TODO */
-	return dbus_error(msg, MESH_ERROR_NOT_IMPLEMENTED, NULL);
+	keyring_del_app_key(node, app_idx);
+
+	return l_dbus_message_new_method_return(msg);
 }
 
 static struct l_dbus_message *import_appkey_call(struct l_dbus

*dbus,
struct l_dbus_message
*msg,

mesh/manager.c +6 -4
 						void *user_data)
 {
-	uint16_t net_idx, app_idx;
+	struct mesh_node *node = user_data;
 	struct l_dbus_message_iter iter_key;
+	uint16_t net_idx, app_idx;
 	uint8_t *key;
 	uint32_t n;
@@ -203,22 +326,37 @@ static struct l_dbus_message
 *import_appkey_call(struct l_dbus *dbus,
 		return dbus_error(msg, MESH_ERROR_INVALID_ARGS,
 							"Bad
 application key");
 
-	/* TODO */
-	return dbus_error(msg, MESH_ERROR_NOT_IMPLEMENTED, NULL);
+	return store_new_appkey(node, msg, net_idx, app_idx, key);
 }
 
 static struct l_dbus_message *set_key_phase_call(struct l_dbus
 *dbus,
 						struct l_dbus_message
 *msg,
 						void *user_data)
 {
+	struct mesh_node *node = user_data;
+	struct keyring_net_key key;
 	uint16_t net_idx;
 	uint8_t phase;
 
-	if (!l_dbus_message_get_arguments(msg, "qy", &net_idx, &phase))
+	if (!l_dbus_message_get_arguments(msg, "qy", &net_idx, &phase)

||

mesh/manager.c +1 -0
+					phase == KEY_REFRESH_PHASE_ONE

||

mesh/manager.c +1 -0
+					phase >

KEY_REFRESH_PHASE_THREE)

mesh/manager.c +2 -2
 		return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
 
-	/* TODO */
-	return dbus_error(msg, MESH_ERROR_NOT_IMPLEMENTED, NULL);
+	if (!keyring_get_net_key(node, net_idx, &key))
+		return dbus_error(msg, MESH_ERROR_DOES_NOT_EXIST,

NULL);

mesh/manager.c +3 -0
+
+	if (phase == KEY_REFRESH_PHASE_THREE &&
+					key.phase !=

KEY_REFRESH_PHASE_NONE) {

mesh/manager.c static struct l_dbus_message *import_appkey_call(struct l_dbus *dbus, · line 357

Quoted 7 lines
mesh/manager.c +7 -0
+		memcpy(key.old_key, key.new_key, 16);
+		key.phase = KEY_REFRESH_PHASE_NONE;
+	} else
+		key.phase = phase;
+
+	if (!keyring_put_net_key(node, net_idx, &key))
+		return dbus_error(msg, MESH_ERROR_FAILED, NULL);

Restore the phase to the previous value in case of failure?

Quoted 23 lines
mesh/manager.c +2 -0
+
+	return l_dbus_message_new_method_return(msg);
 }
 
 static void setup_management_interface(struct l_dbus_interface

*iface)

mesh/manager.c +1 -1
@@ -242,9 +380,9 @@ static void setup_management_interface(struct
 l_dbus_interface *iface)
 	l_dbus_interface_method(iface, "CreateAppKey", 0,
 create_appkey_call,
 					"", "qq", "", "net_index",
 "app_index");
 	l_dbus_interface_method(iface, "UpdateAppKey", 0,
 update_appkey_call,
-					"", "qq", "", "net_index",
 "app_index");
+					"", "q", "", "app_index");

l_dbus_interface_method(iface, "DeleteAppKey", 0,
delete_appkey_call,

mesh/manager.c +0 -1
-					"", "qq", "", "net_index",

"app_index");

mesh/manager.c +1 -0
+					"", "q", "", "app_index");
 	l_dbus_interface_method(iface, "ImportAppKey", 0,
Quoted 5 lines

import_appkey_call,
"", "qqay", "", "net_index",
"app_index",
"app_ke
y");

Thank you,

Inga

author
#2 lore raw

Hi Inga... I will correct the various spelling issues, and perhaps also segment the patches as you suggest

Quoted 14 lines · Inga
Quoted 8 lines · Brian Gix
mesh/manager.c +3 -1
+	struct mesh_node *node = user_data;
+	uint8_t key[16];
 	uint16_t net_idx;
 
-	if (!l_dbus_message_get_arguments(msg, "q", &net_idx))
+	if (!l_dbus_message_get_arguments(msg, "q", &net_idx) ||

!net_idx)

Maybe use explicit value for net_idx check, i.e.,
net_idx == MESH_PRIMARY_NET_INDEX_DEFAULT

Quoted 2 lines

Currently DEFAULT_PRIMARY_NET_INDEX is defined in node.c WE should move it into mesh-defs.h and rename accordingly

I can do this... Although I don't know that we will ever have any other Net Index except 0 ever be defined by the spec as "Primary".

Quoted 20 lines
Quoted 11 lines
mesh/manager.c +4 -3
-	if (!l_dbus_message_get_arguments(msg, "q", &net_idx))
+	if (!l_dbus_message_get_arguments(msg, "q", &net_idx) ||
+						net_idx > MAX_KEY_IDX)
 		return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
 
-	/* TODO */
-	return dbus_error(msg, MESH_ERROR_NOT_IMPLEMENTED, NULL);
+	if (!keyring_get_net_key(node, net_idx, &key))
+		return dbus_error(msg, MESH_ERROR_DOES_NOT_EXIST,

NULL);

mesh/manager.c static struct l_dbus_message *cancel_scan_call(struct l_dbus *dbus, · line 155

Quoted 6 lines
mesh/manager.c +6 -0
+
+	switch (key.phase) {
+	case KEY_REFRESH_PHASE_NONE:
+		/* Generate Key and update phase */
+		l_getrandom(key.new_key, sizeof(key.new_key));
+		key.phase = KEY_REFRESH_PHASE_ONE;

line break?

Quoted 6 lines
Quoted 3 lines
(quoted diff) +2 -0
+		if (!keyring_put_net_key(node, net_idx, &key))
+			return dbus_error(msg, MESH_ERROR_FAILED,

NULL);

Wouldn't we want to revert the key phase back to KEY_REFRESH_PHASE_NONE in case of failure?

I don't think this is neccessary. This is the only place in the function where a *Change* is made to the persistent data... If it fails then it fails, and the old data will still be there.

Quoted 14 lines
Quoted 14 lines

+static struct l_dbus_message *store_new_appkey(struct mesh_node *node,

(quoted diff) +2 -0
+					struct l_dbus_message *msg,
+					uint16_t net_idx, uint16_t

app_idx,

mesh/manager.c +8 -0
+					uint8_t *new_key)
+{
+	struct keyring_net_key net_key;
+	struct keyring_app_key app_key;
+
+	if (net_idx > MAX_KEY_IDX || app_idx > MAX_KEY_IDX)
+		return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
+
Quoted 8 lines
Quoted 3 lines
(quoted diff) +2 -0
+	if (!keyring_get_net_key(node, net_idx, &net_key))
+		return dbus_error(msg, MESH_ERROR_DOES_NOT_EXIST,

NULL);

NULL -> "Bound net key not found"
Also, I don't think "org.bluez.mesh.Error.DoesNotExist" error is documented in mesh-api.txt for either CreateAppKey() or ImportAppKey(), needs to be added.

Will add

Quoted 20 lines
Quoted 20 lines

static struct l_dbus_message *update_appkey_call(struct l_dbus
*dbus,
struct l_dbus_message
*msg,

mesh/manager.c +4 -2
 						void *user_data)
 {
-	uint16_t net_idx, app_idx;
+	struct mesh_node *node = user_data;
+	struct keyring_net_key net_key;
+	struct keyring_app_key app_key;
+	uint16_t app_idx;
 
-	if (!l_dbus_message_get_arguments(msg, "qq", &net_idx,

&app_idx))

mesh/manager.c +2 -0
+	if (!l_dbus_message_get_arguments(msg, "q", &app_idx) ||
+						app_idx > MAX_KEY_IDX)
 		return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
Quoted 15 lines

mesh/manager.c static struct l_dbus_message *import_subnet_call(struct l_dbus *dbus, · line 278

Quoted 11 lines
mesh/manager.c +2 -2
-	/* TODO */
-	return dbus_error(msg, MESH_ERROR_NOT_IMPLEMENTED, NULL);
+	if (!keyring_get_app_key(node, app_idx, &app_key) ||
+			!keyring_get_net_key(node, app_key.net_idx,

&net_key))

(quoted diff) +1 -0
+		return dbus_error(msg, MESH_ERROR_DOES_NOT_EXIST,

NULL);

mesh/manager.c +4 -0
+
+	switch (net_key.phase) {
+	case KEY_REFRESH_PHASE_NONE:
+	case KEY_REFRESH_PHASE_ONE:

What if the net key is in key refresh phase one and This method has been called mre than once? i.e., do we want to protect the application from potentially overwriting already updated application key?

Well, Application keys don't actually have a phase... It is inherited from the bound net key. The controlling Config Client app needs to set up *all* the updates in the keyring before starting to send the updates to the remote nodes. We can warn in the mesh-api.txt the correct order of things, but as long as the App does not start sending the updates over-the-air, there is no problem with Updating the key-ring's App Key redundantly prior to starting to propagate. As long as the *spec* is followed, we don't have a problem.

Quoted 34 lines
Quoted 34 lines

static struct l_dbus_message *set_key_phase_call(struct l_dbus
*dbus,
struct l_dbus_message
*msg,

mesh/manager.c +3 -1
 						void *user_data)
 {
+	struct mesh_node *node = user_data;
+	struct keyring_net_key key;
 	uint16_t net_idx;
 	uint8_t phase;
 
-	if (!l_dbus_message_get_arguments(msg, "qy", &net_idx, &phase))
+	if (!l_dbus_message_get_arguments(msg, "qy", &net_idx, &phase)
Quoted 1 line
Quoted 1 line
(quoted diff) +1 -0
+					phase == KEY_REFRESH_PHASE_ONE
Quoted 1 line
Quoted 1 line
(quoted diff) +1 -0
+					phase >

KEY_REFRESH_PHASE_THREE)

mesh/manager.c +2 -2
 		return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
 
-	/* TODO */
-	return dbus_error(msg, MESH_ERROR_NOT_IMPLEMENTED, NULL);
+	if (!keyring_get_net_key(node, net_idx, &key))
+		return dbus_error(msg, MESH_ERROR_DOES_NOT_EXIST,

NULL);

mesh/manager.c +3 -0
+
+	if (phase == KEY_REFRESH_PHASE_THREE &&
+					key.phase !=

KEY_REFRESH_PHASE_NONE) {

mesh/manager.c +1 -0
+		memcpy(key.old_key, key.new_key, 16);
Quoted 8 lines

mesh/manager.c static struct l_dbus_message *import_appkey_call(struct l_dbus *dbus, · line 357

Quoted 6 lines
mesh/manager.c +6 -0
+		key.phase = KEY_REFRESH_PHASE_NONE;
+	} else
+		key.phase = phase;
+
+	if (!keyring_put_net_key(node, net_idx, &key))
+		return dbus_error(msg, MESH_ERROR_FAILED, NULL);

Restore the phase to the previous value in case of failure?

As with the above comment, a failure is a failure... If this method fails for *any* reason, it means nothing about the keys were updated, so there is nothing to restore.

Quoted 6 lines
Quoted 2 lines

Thank you,

Inga