Make uuid generation RFC 4122 compliant 38/279438/1
authorAbhay Agarwal <ay.agarwal@samsung.com>
Tue, 9 Aug 2022 07:04:04 +0000 (12:34 +0530)
committerAbhay Agarwal <ay.agarwal@samsung.com>
Tue, 9 Aug 2022 08:46:13 +0000 (14:16 +0530)
This patch makes the ble mesh uuid generation RFC 4122 compliant
as requried by bletooth-meshd stack.

Change-Id: I30d187c99e526d99b5ff08f7c2fba23a2fb8b5f7
Signed-off-by: Abhay Agarwal <ay.agarwal@samsung.com>
include/bluetooth_private.h
src/bluetooth-common.c
src/bluetooth-mesh.c

index e19f0b1..eb42b9b 100644 (file)
@@ -792,6 +792,12 @@ bool _bt_get_random_bytes(void *buf, size_t num_bytes);
 
 /**
  * @internal
+ * @brief Verify if the uuid is valid (RCF 4412 compliant) or not
+ */
+bool _bt_is_uuid_valid(const uint8_t uuid[16]);
+
+/**
+ * @internal
  * @brief Convert the visibility mode
  */
 bt_adapter_visibility_mode_e _bt_get_bt_visibility_mode_e(bluetooth_discoverable_mode_t mode);
index bdbcdaf..482f6e1 100644 (file)
@@ -753,6 +753,26 @@ bool _bt_get_random_bytes(void *buf, size_t num_bytes)
 
        return true;
 }
+
+bool _bt_is_uuid_valid(const uint8_t uuid[16])
+{
+       unsigned int version;
+       unsigned int variant;
+
+       if (!uuid)
+               return false;
+
+       variant = uuid[8] >> 6;
+       if (variant != 2)
+               return false;
+
+       version = uuid[6] >> 4;
+       if (version < 1 || version > 5)
+               return false;
+
+       return true;
+}
+
 /* LCOV_EXCL_STOP */
 
 /* LCOV_EXCL_START */
index ef0c8c7..81f82ba 100644 (file)
@@ -1369,6 +1369,18 @@ int bt_mesh_network_create(bt_mesh_node_h config_client,
        param_node.primary_unicast = 0x0001;
        _bt_get_random_bytes(param_node.uuid, 16);
 
+       /* Update uuid as per RFC 4122, section 4.4 */
+       param_node.uuid[6] &= 0x0f;
+       param_node.uuid[6] |= 4 << 4;
+
+       param_node.uuid[8] &= 0x3f;
+       param_node.uuid[8] |= 0x80;
+
+       if (!_bt_is_uuid_valid(param_node.uuid)) {
+               BT_ERR("uuid not generated properly");
+               return BT_ERROR_OPERATION_FAILED;
+       }
+
        BT_INFO("Mesh: Total Models [%d]", num_models);
        param_model = (bluetooth_mesh_model_t**)g_malloc0(num_models * sizeof(bluetooth_mesh_model_t*));
 
@@ -1569,7 +1581,21 @@ int bt_mesh_node_network_join(bt_mesh_node_h node_handle,
 
        param_node.num_elements =  g_slist_length(node->elements);
        param_node.primary_unicast = 0x0001;
+
        _bt_get_random_bytes(param_node.uuid, 16);
+
+       /* Update uuid as per RFC 4122, section 4.4 */
+       param_node.uuid[6] &= 0x0f;
+       param_node.uuid[6] |= 4 << 4;
+
+       param_node.uuid[8] &= 0x3f;
+       param_node.uuid[8] |= 0x80;
+
+       if (!_bt_is_uuid_valid(param_node.uuid)) {
+               BT_ERR("uuid not generated properly");
+               return BT_ERROR_OPERATION_FAILED;
+       }
+
        __bt_mesh_util_convert_hex_to_string((uint8_t *)param_node.uuid,
                        16, node->uuid, sizeof(node->uuid));