Add API to set a seed value for UUID generation,
authorChul Lee <chuls.lee@samsung.com>
Mon, 9 Jan 2017 12:04:05 +0000 (21:04 +0900)
committerKevin Kane <kkane@microsoft.com>
Tue, 31 Jan 2017 17:54:23 +0000 (17:54 +0000)
to ensure that a device always uses the same UUID.
If the seed value is not set, a UUID is generated randomly.

Patch #1 : Initial upload
Patch #2 : add header file include on srmutility.c.
Patch #3 : Updated according to comments.
Patch #4 : Fix build error
Patch #5 : rebase
Patch #6 : Fix coredump
Patch #7-8 : Retrigger
Patch #9-10 : Updated according to comments.
Patch #11-13 : Retrigger

Change-Id: I6ce668866b5881386a52ef8cb9de5226b8595749
Signed-off-by: Chul Lee <chuls.lee@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/16259
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Kevin Kane <kkane@microsoft.com>
Reviewed-by: Joonghwan Lee <jh05.lee@samsung.com>
Reviewed-by: Randeep Singh <randeep.s@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/16921
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Phil Coval <philippe.coval@osg.samsung.com>
resource/csdk/octbstack_product_secured.def
resource/csdk/security/include/internal/doxmresource.h
resource/csdk/security/include/srmutility.h
resource/csdk/security/src/doxmresource.c
resource/csdk/security/src/srmutility.c
resource/csdk/security/unittest/srmutility.cpp

index f524b63..f52c77f 100644 (file)
@@ -48,6 +48,7 @@ OCSetOxmAllowStatus
 
 SetDisplayPinWithContextCB
 UnsetDisplayPinWithContextCB
+SetDeviceIdSeed
 SetGeneratePinCB
 UnsetGeneratePinCB
 SetInputPinCB
index 757bfd8..5b14ea8 100644 (file)
@@ -82,6 +82,18 @@ OCStackResult CBORPayloadToDoxm(const uint8_t *cborPayload, size_t size,
 OCStackResult DoxmToCBORPayload(const OicSecDoxm_t * doxm, uint8_t **cborPayload,
                                 size_t *cborSize, bool rwOnly);
 
+#if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
+/**
+ * API to save the seed value to generate device UUID.
+ *
+ * @param seed buffer of seed value.
+ * @param seedSize byte length of seed
+ *
+ * @return ::OC_STACK_OK for Success, otherwise some error value.
+ */
+OCStackResult SetDoxmDeviceIDSeed(const uint8_t* seed, size_t seedSize);
+#endif
+
 /**
  * This method returns the SRM device ID for this device.
  *
index b311b4a..c2f0466 100644 (file)
@@ -122,6 +122,19 @@ OCStackResult ConvertUuidToStr(const OicUuid_t* uuid, char** strUuid);
 OCStackResult ConvertStrToUuid(const char* strUuid, OicUuid_t* uuid);
 
 
+#if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
+/**
+ * API to save the seed value to generate device UUID.
+ * Seed value MUST be unique per device (e.g. MAC address)
+ *
+ * @param seed buffer of seed value.
+ * @param seedSize byte length of seed
+ *
+ * @return ::OC_STACK_OK for Success, otherwise some error value.
+ */
+OCStackResult SetDeviceIdSeed(const uint8_t* seed, size_t seedSize);
+#endif
+
 #ifdef __cplusplus
 }
 #endif // __cplusplus
index 0844aaa..3cb4196 100644 (file)
@@ -54,6 +54,7 @@
 #include "oxmverifycommon.h"
 
 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
+#include "mbedtls/md.h"
 #include "pkix_interface.h"
 #endif
 
@@ -67,6 +68,17 @@ static const uint16_t CBOR_SIZE = 512;
 /** Max cbor size payload. */
 static const uint16_t CBOR_MAX_SIZE = 4400;
 
+#if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
+/** MAX uuid seed size */
+#define MAX_UUID_SEED_SIZE (64)
+/** MIN uuid seed size */
+#define MIN_UUID_SEED_SIZE (8)
+
+/** Buffer to save the seed of device UUID */
+static uint8_t gUuidSeed[MAX_UUID_SEED_SIZE];
+static size_t gUuidSeedSize = 0;
+#endif
+
 static OicSecDoxm_t        *gDoxm = NULL;
 static OCResourceHandle    gDoxmHandle = NULL;
 
@@ -1540,6 +1552,7 @@ static OCStackResult CheckDeviceID()
 {
     OCStackResult ret = OC_STACK_ERROR;
     bool validId = false;
+
     for (uint8_t i = 0; i < UUID_LENGTH; i++)
     {
         if (gDoxm->deviceID.id[i] != 0)
@@ -1551,12 +1564,65 @@ static OCStackResult CheckDeviceID()
 
     if (!validId)
     {
+        char* strUuid = NULL;
+#if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
+        //If seed value is exists, generate UUID using seed with SHA256
+        if (0 != gUuidSeedSize)
+        {
+            uint8_t hashVal[MBEDTLS_MD_MAX_SIZE] = {0};
+            int mbedret = 0;
+
+            OIC_LOG(DEBUG, TAG, "UUID will be generated using seed w/ SHA256");
+            OIC_LOG(DEBUG, TAG, "Seed value : ");
+            OIC_LOG_BUFFER(DEBUG, TAG, gUuidSeed, gUuidSeedSize);
+
+            mbedret = mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
+                                 gUuidSeed, gUuidSeedSize, hashVal);
+            if(0 == mbedret)
+            {
+                memcpy(gDoxm->deviceID.id, hashVal, sizeof(gDoxm->deviceID.id));
+                ret = OC_STACK_OK;
+            }
+            else
+            {
+                OIC_LOG_V(ERROR, TAG, "mbedtls_md error : %d", mbedret);
+                ret = OC_STACK_ERROR;
+            }
+        }
+        else
+        {
+            if (!OCGenerateUuid(gDoxm->deviceID.id))
+            {
+                OIC_LOG(FATAL, TAG, "Generate UUID for Server Instance failed!");
+                ret = OC_STACK_ERROR;
+            }
+            else
+            {
+                ret = OC_STACK_OK;
+            }
+        }
+#else
         if (!OCGenerateUuid(gDoxm->deviceID.id))
         {
             OIC_LOG(FATAL, TAG, "Generate UUID for Server Instance failed!");
-            return OC_STACK_ERROR;
+            ret = OC_STACK_ERROR;
         }
-        ret = OC_STACK_OK;
+        else
+        {
+            ret = OC_STACK_OK;
+        }
+#endif
+
+        if (OC_STACK_OK == ConvertUuidToStr(&gDoxm->deviceID, &strUuid))
+        {
+            OIC_LOG_V(DEBUG, TAG, "Generated device UUID is [%s]", strUuid);
+            OICFree(strUuid);
+        }
+        else
+        {
+            OIC_LOG(WARNING, TAG, "Failed to convert UUID to string");
+        }
+
 
         if (!UpdatePersistentStorage(gDoxm))
         {
@@ -1568,6 +1634,7 @@ static OCStackResult CheckDeviceID()
     {
         ret = OC_STACK_OK;
     }
+
     return ret;
 }
 
@@ -1733,6 +1800,36 @@ OCStackResult GetDoxmIsOwned(bool *isOwned)
     return OC_STACK_ERROR;
 }
 
+#if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
+OCStackResult SetDoxmDeviceIDSeed(const uint8_t* seed, size_t seedSize)
+{
+    OIC_LOG_V(INFO, TAG, "In %s", __func__);
+
+    if (NULL == seed)
+    {
+        return OC_STACK_INVALID_PARAM;
+    }
+    if (MAX_UUID_SEED_SIZE < seedSize)
+    {
+        OIC_LOG_V(ERROR, TAG, "Seed size is too long (MAX size is %d bytes)", MAX_UUID_SEED_SIZE);
+        return OC_STACK_INVALID_PARAM;
+    }
+    if (MIN_UUID_SEED_SIZE > seedSize)
+    {
+        OIC_LOG_V(ERROR, TAG, "Seed size is too small (MIN size is %d bytes)", MIN_UUID_SEED_SIZE);
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    memset(gUuidSeed, 0x00, sizeof(gUuidSeed));
+    memcpy(gUuidSeed, seed, seedSize);
+    gUuidSeedSize = seedSize;
+
+    OIC_LOG_V(INFO, TAG, "Out %s", __func__);
+
+    return OC_STACK_OK;
+}
+#endif
+
 OCStackResult SetDoxmDeviceID(const OicUuid_t *deviceID)
 {
     bool isPT = false;
index 9a9b8c9..0080407 100644 (file)
@@ -26,6 +26,7 @@
 #include "oic_malloc.h"
 #include "base64.h"
 #include "ocrandom.h"
+#include "doxmresource.h"
 
 #define TAG  "OIC_SRM_UTILITY"
 
@@ -161,3 +162,10 @@ OCStackResult ConvertStrToUuid(const char* strUuid, OicUuid_t* uuid)
 
     return OC_STACK_OK;
 }
+
+#if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
+OCStackResult SetDeviceIdSeed(const uint8_t* seed, size_t seedSize)
+{
+    return SetDoxmDeviceIDSeed(seed, seedSize);
+}
+#endif
index a5df9f7..10dbaba 100644 (file)
@@ -158,3 +158,25 @@ TEST(ConvertStrToUuidTest, EmptyStringConversion)
         EXPECT_EQ((uint8_t)0, uuid.id[i]);
     }
 }
+
+#if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
+TEST(SetUuidSeedTest, NullParam)
+{
+    EXPECT_EQ(OC_STACK_INVALID_PARAM, SetDeviceIdSeed(NULL, 0));
+}
+
+TEST(SetUuidSeedTest, InvalidParam)
+{
+    uint8_t seed[1024] = {0};
+
+    EXPECT_EQ(OC_STACK_INVALID_PARAM, SetDeviceIdSeed(seed, 0));
+    EXPECT_EQ(OC_STACK_INVALID_PARAM, SetDeviceIdSeed(seed, sizeof(seed)));
+}
+
+TEST(SetUuidSeedTest, ValidValue)
+{
+    uint8_t seed[16] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
+                        0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10};
+    EXPECT_EQ(OC_STACK_OK, SetDeviceIdSeed(seed, sizeof(seed)));
+}
+#endif