mesh: Add App and Net Key Refresh keyring back-end
v2 cover mesh: Add App and Net Key Refresh keyring back-end
Version 2 is a patchset with some pre-staging cleanup and breaking the patches into functional sets.
Patches are order dependant (apply in composed order)
Brian Gix (5):
mesh: Add new required method for Key Refresh proc
mesh: Centralize definition of PRIMARY_NET_IDX
mesh: Implement Net Key keyring storage handling
mesh: Implement App Key keyring storage handling
mesh: Implement Key Refresh Phasing in keyring
doc/mesh-api.txt | 26 ++++++- mesh/manager.c | 226 +++++++++++++++++++++++++++++++++++++++++++++++-------- mesh/mesh-defs.h | 1 + mesh/node.c | 12 ++- 4 files changed, 227 insertions(+), 38 deletions(-)
Signature · 1 line
2.14.5
PATCH v2 1/5 mesh: Add new required method for Key Refresh proc
CompleteAppKeyUpdate() - Used in conjunction with SetKeyPhase()
--- doc/mesh-api.txt | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-)
doc/mesh-api.txt +25 -1
@@ -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)
@@ -451,7 +452,9 @@ Methods: void SetKeyPhase(uint16 net_index, uint8 phase) This method is used to set the master key update phase of the - given subnet. + given subnet. When finalizing the procedure, it is important + to CompleteAppKeyUpdate() on all app keys that have been + updated during the procedure prior to setting phase 3. The net_index parameter is a 12-bit value (0x000-0xFFF) specifying which subnet phase to set.
@@ -491,6 +494,7 @@ Methods: PossibleErrors: org.bluez.mesh.Error.InvalidArguments org.bluez.mesh.Error.AlreadyExists + org.bluez.mesh.Error.DoesNotExist void ImportAppKey(uint16 net_index, uint16 app_index, array{byte}[16] app_key)
@@ -512,6 +516,7 @@ Methods: PossibleErrors: org.bluez.mesh.Error.InvalidArguments org.bluez.mesh.Error.AlreadyExists + org.bluez.mesh.Error.DoesNotExist void UpdateAppKey(uint16 app_index)
@@ -524,6 +529,25 @@ Methods: This call affects the local bluetooth-meshd key database only. + PossibleErrors: + org.bluez.mesh.Error.InvalidArguments + 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.InvalidArguments org.bluez.mesh.Error.DoesNotExist
Signature · 1 line
2.14.5
PATCH v2 2/5 mesh: Centralize definition of PRIMARY_NET_IDX
In reply to Brian Gix--- mesh/mesh-defs.h | 1 + mesh/node.c | 12 +++++------- 2 files changed, 6 insertions(+), 7 deletions(-)
mesh/mesh-defs.h +1 -0
@@ -74,6 +74,7 @@#define VENDOR_ID_MASK 0xffff0000 +#define PRIMARY_NET_IDX 0x0000#define MAX_KEY_IDX 0x0fff#define MAX_MODEL_COUNT 0xff#define MAX_ELE_COUNT 0xff
mesh/node.c +5 -7
@@ -51,7 +51,6 @@/* Default values for a new locally created node */ #define DEFAULT_NEW_UNICAST 0x0001#define DEFAULT_IV_INDEX 0x0000 -#define DEFAULT_PRIMARY_NET_INDEX 0x0000/* Default element location: unknown */ #define DEFAULT_LOCATION 0x0000
@@ -1564,21 +1563,20 @@ 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.old_key,sizeof (net_key.old_key)); - net_key.net_idx =DEFAULT_PRIMARY_NET_INDEX ; - net_key.phase =0 ; + net_key.net_idx =PRIMARY_NET_IDX ; + net_key.phase =KEY_REFRESH_PHASE_NONE ;if (!add_local_node (node,DEFAULT_NEW_UNICAST , false, false,DEFAULT_IV_INDEX , dev_key, -DEFAULT_PRIMARY_NET_INDEX , - net_key.old_key)) +PRIMARY_NET_IDX , + 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)) +if (!keyring_put_net_key (node,PRIMARY_NET_IDX , &net_key)) goto fail;cb (req->user_data,MESH_ERROR_NONE , node);
Signature · 1 line
2.14.5
PATCH v2 3/5 mesh: Implement Net Key keyring storage handling
In reply to Brian GixAffected methods:CreateSubnet()ImportSubnet()UpdateSubnet()DeleteSubnet()
--- mesh/manager.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 75 insertions(+), 12 deletions(-)
mesh/manager.c +75 -12
@@ -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,112 @@ 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, +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 redandent calls only if they match */ +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 *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 ==PRIMARY_NET_IDX )return dbus_error (msg,MESH_ERROR_INVALID_ARGS ,NULL ); -/* TODO */ -return dbus_error (msg,MESH_ERROR_NOT_IMPLEMENTED ,NULL ); +/* Generate key and store */ +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 */ -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 ; + +if (!keyring_put_net_key (node, net_idx, &key)) +return dbus_error (msg,MESH_ERROR_FAILED ,NULL ); + +/* Fall Through */ + +case KEY_REFRESH_PHASE_ONE : +/* Allow redundant calls to start Key Refresh */ +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 ,"Key Refresh in progress" ); }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,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,8 +206,7 @@ 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 *create_appkey_call(struct l_dbus *dbus,
Signature · 1 line
2.14.5
PATCH v2 4/5 mesh: Implement App Key keyring storage handling
In reply to Brian GixAffected Methods:CreateAppKey()ImportAppKey()UpdateAppKey()DeleteAppKey()
--- mesh/manager.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 71 insertions(+), 15 deletions(-)
mesh/manager.c +71 -15
@@ -209,51 +209,108 @@ static struct l_dbus_message *import_subnet_call(struct l_dbus *dbus,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) +{ +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 , +"Bound net key not found" ); + +if (keyring_get_app_key (node, app_idx, &app_key)) { +/* Allow redundant calls with identical values */ +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 ); +memcpy (app_key.new_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 */ -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 ); + +if (net_key.phase !=KEY_REFRESH_PHASE_ONE ) +return dbus_error (msg,MESH_ERROR_FAILED ,"Invalid Phase" ); + +/* Generate Key if in acceptable phase */ +l_getrandom (app_key.new_key,sizeof (app_key.new_key)); + +if (!keyring_put_app_key (node, app_idx, app_key.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 *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)) +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,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;
@@ -266,8 +323,7 @@ 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,
@@ -305,9 +361,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, -"" ,"qq" ,"" ,"net_index" ,"app_index" ); +"" ,"q" ,"" ,"app_index" );l_dbus_interface_method (iface,"ImportAppKey" ,0 , import_appkey_call,"" ,"qqay" ,"" ,"net_index" ,"app_index" ,"app_key" );
Signature · 1 line
2.14.5
PATCH v2 5/5 mesh: Implement Key Refresh Phasing in keyring
In reply to Brian GixAffected methods:CompleteAppKeyUpdate()SetKeyPhase()
--- mesh/manager.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-)
mesh/manager.c +50 -3
@@ -289,6 +289,34 @@ static struct l_dbus_message *update_appkey_call(struct l_dbus *dbus,return l_dbus_message_new_method_return (msg); } +static struct l_dbus_message *complete_update_appkey_call (struct l_dbus *dbus, +struct l_dbus_message *msg, +void *user_data) +{ +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,"q" , &app_idx) || + app_idx >MAX_KEY_IDX ) +return dbus_error (msg,MESH_ERROR_INVALID_ARGS ,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 ); + +if (net_key.phase !=KEY_REFRESH_PHASE_TWO ) +return dbus_error (msg,MESH_ERROR_FAILED ,"Invalid phase" ); + +memcpy (app_key.old_key, app_key.new_key,16 ); + +if (!keyring_put_app_key (node, app_idx, app_key.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 *delete_appkey_call (struct l_dbus *dbus,struct l_dbus_message *msg,void *user_data)
@@ -330,14 +358,30 @@ 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) || + phase ==KEY_REFRESH_PHASE_ONE || + phase >KEY_REFRESH_PHASE_THREE )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 ); + +if (phase ==KEY_REFRESH_PHASE_THREE && + key.phase !=KEY_REFRESH_PHASE_NONE ) { +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 ); + +return l_dbus_message_new_method_return (msg); }static void setup_management_interface (struct l_dbus_interface *iface)
@@ -362,6 +406,9 @@ static void setup_management_interface(struct l_dbus_interface *iface) "", "qq", "", "net_index", "app_index");l_dbus_interface_method (iface,"UpdateAppKey" ,0 , update_appkey_call,"" ,"q" ,"" ,"app_index" ); +l_dbus_interface_method (iface,"CompleteAppKeyUpdate" ,0 , + complete_update_appkey_call,"" ,"q" , +"" ,"app_index" );l_dbus_interface_method (iface,"DeleteAppKey" ,0 , delete_appkey_call,"" ,"q" ,"" ,"app_index" ); l_dbus_interface_method(iface, "ImportAppKey", 0, import_appkey_call,
Signature · 1 line
2.14.5