Generate device ID from Secure Virtual Resource
authorSakthivel Samidurai <sakthivel.samidurai@intel.com>
Thu, 27 Aug 2015 20:25:54 +0000 (13:25 -0700)
committerSachin Agrawal <sachin.agrawal@intel.com>
Thu, 27 Aug 2015 23:16:20 +0000 (23:16 +0000)
SRM should generate the device ID for the resource server

Change-Id: Ic4f83f63a59827bf5cfb412afc34e1d955ee69e2
Signed-off-by: Sakthivel Samidurai <sakthivel.samidurai@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2162
Reviewed-by: Sachin Agrawal <sachin.agrawal@intel.com>
Tested-by: Sachin Agrawal <sachin.agrawal@intel.com>
resource/csdk/security/src/doxmresource.c
resource/csdk/stack/include/internal/ocstackinternal.h
resource/csdk/stack/src/ocresource.c
resource/csdk/stack/src/ocstack.c
resource/unittests/OCPlatformTest.cpp

index 0423a21..83c570f 100644 (file)
@@ -279,8 +279,7 @@ OicSecDoxm_t * JSONToDoxmBin(const char * jsonStr)
     else // PUT/POST JSON will not have deviceID so set it to the gDoxm->deviceID.id
     {
         VERIFY_NON_NULL(TAG, gDoxm, ERROR);
-        VERIFY_SUCCESS(TAG, strcmp((char *)gDoxm->deviceID.id, "") != 0, ERROR);
-        strncpy((char *)doxm->deviceID.id, (char *)gDoxm->deviceID.id, sizeof(doxm->deviceID.id));
+        memcpy((char *)doxm->deviceID.id, (char *)gDoxm->deviceID.id, sizeof(doxm->deviceID.id));
     }
 
     //Owner -- will be empty when device status is unowned.
@@ -696,13 +695,39 @@ OCStackResult CreateDoxmResource()
  * Once DeviceID is assigned to the device it does not change for the lifetime of the device.
  *
  */
-void CheckDeviceID()
+static OCStackResult CheckDeviceID()
 {
-    if(strcmp((char *)gDoxm->deviceID.id, "") == 0 )
+    OCStackResult ret = OC_STACK_ERROR;
+    bool validId = false;
+    for (uint8_t i = 0; i < UUID_LENGTH; i++)
+    {
+        if (gDoxm->deviceID.id[i] != 0)
+        {
+            validId = true;
+            break;
+        }
+    }
+
+    if (!validId)
+    {
+        if (OCGenerateUuid(gDoxm->deviceID.id) != RAND_UUID_OK)
+        {
+            OC_LOG(FATAL, TAG, PCF("Generate UUID for Server Instance failed!"));
+            return ret;
+        }
+        ret = OC_STACK_OK;
+
+        if (UpdatePersistentStorage(gDoxm))
+        {
+            //TODO: After registering PSI handler in all samples, do ret = OC_STACK_OK here.
+            OC_LOG(FATAL, TAG, PCF("UpdatePersistentStorage failed!"));
+        }
+    }
+    else
     {
-        OCFillRandomMem(gDoxm->deviceID.id, sizeof(gDoxm->deviceID.id));
-        UpdatePersistentStorage(gDoxm);
+        ret = OC_STACK_OK;
     }
+    return ret;
 }
 
 /**
@@ -750,9 +775,16 @@ OCStackResult InitDoxmResource()
     {
         gDoxm = GetDoxmDefault();
     }
-    CheckDeviceID();
-    //Instantiate 'oic.sec.doxm'
-    ret = CreateDoxmResource();
+    ret = CheckDeviceID();
+    if (ret == OC_STACK_OK)
+    {
+        //Instantiate 'oic.sec.doxm'
+        ret = CreateDoxmResource();
+    }
+    else
+    {
+        OC_LOG (ERROR, TAG, PCF("CheckDeviceID failed"));
+    }
     OICFree(jsonSVRDatabase);
     return ret;
 }
index 028da46..a44654d 100644 (file)
@@ -43,6 +43,7 @@
 
 #include "cacommon.h"
 #include "cainterface.h"
+#include "securevirtualresourcetypes.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -273,7 +274,7 @@ OCStackResult CAResultToOCResult(CAResult_t caResult);
  *
  * @return A uint8_t representation the server instance ID.
  */
-const uint8_t* OCGetServerInstanceID(void);
+const OicUuid_t* OCGetServerInstanceID(void);
 
 /**
  * Get a string representation the server instance ID.
index 9f4f675..c38bc6d 100644 (file)
@@ -591,16 +591,24 @@ static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource
     }
     else if (virtualUriInRequest == OC_DEVICE_URI)
     {
-        payload = (OCPayload*)OCDevicePayloadCreate(OC_RSRVD_DEVICE_URI,
-                OCGetServerInstanceID(), savedDeviceInfo.deviceName,
-                OC_SPEC_VERSION, OC_DATA_MODEL_VERSION);
-        if (!payload)
+        const OicUuid_t* deviceId = OCGetServerInstanceID();
+        if (!deviceId)
         {
-            discoveryResult = OC_STACK_NO_MEMORY;
+            discoveryResult = OC_STACK_ERROR;
         }
         else
         {
-            discoveryResult = OC_STACK_OK;
+            payload = (OCPayload*) OCDevicePayloadCreate(OC_RSRVD_DEVICE_URI,
+                    (const uint8_t*) &deviceId->id, savedDeviceInfo.deviceName,
+                    OC_SPEC_VERSION, OC_DATA_MODEL_VERSION);
+            if (!payload)
+            {
+                discoveryResult = OC_STACK_NO_MEMORY;
+            }
+            else
+            {
+                discoveryResult = OC_STACK_OK;
+            }
         }
     }
     else if (virtualUriInRequest == OC_PLATFORM_URI)
index f34cf8c..9bede93 100644 (file)
@@ -43,6 +43,7 @@
 #include "oic_string.h"
 #include "ocserverrequest.h"
 #include "secureresourcemanager.h"
+#include "doxmresource.h"
 #include "cacommon.h"
 #include "cainterface.h"
 #include "ocpayload.h"
@@ -3767,22 +3768,22 @@ OCStackResult getQueryFromUri(const char * uri, char** query, char ** uriWithout
         return OC_STACK_NO_MEMORY;
 }
 
-const uint8_t* OCGetServerInstanceID(void)
+const OicUuid_t* OCGetServerInstanceID(void)
 {
     static bool generated = false;
-    static ServerID sid;
-    if(generated)
+    static OicUuid_t sid;
+    if (generated)
     {
-        return sid;
+        return &sid;
     }
 
-    if (OCGenerateUuid(sid) != RAND_UUID_OK)
+    if (GetDoxmDeviceID(&sid) != OC_STACK_OK)
     {
         OC_LOG(FATAL, TAG, PCF("Generate UUID for Server Instance failed!"));
         return NULL;
     }
     generated = true;
-    return sid;
+    return &sid;
 }
 
 const char* OCGetServerInstanceIDString(void)
@@ -3795,9 +3796,9 @@ const char* OCGetServerInstanceIDString(void)
         return sidStr;
     }
 
-    const uint8_t* sid = OCGetServerInstanceID();
+    const OicUuid_t* sid = OCGetServerInstanceID();
 
-    if(OCConvertUuidToString(sid, sidStr) != RAND_UUID_OK)
+    if(OCConvertUuidToString(sid->id, sidStr) != RAND_UUID_OK)
     {
         OC_LOG(FATAL, TAG, PCF("Generate UUID String for Server Instance failed!"));
         return NULL;
index ec8d14e..078aa89 100644 (file)
@@ -31,13 +31,16 @@ namespace OCPlatformTest
     const std::string gResourceInterface = DEFAULT_INTERFACE;
     const uint8_t gResourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
     OCResourceHandle resourceHandle;
-  //OCPersistent Storage Handlers
 
-   static FILE* client_open(const char* /*path*/, const char *mode)
-   {
-       std::cout << "<===Opening SVR DB file = './oic_svr_db_client.json' with mode = '"<< mode<<"' "<<std::endl;
-               return fopen("./oic_svr_db_client.json", mode);
-   }
+    //OCPersistent Storage Handlers
+    static FILE* client_open(const char * /*path*/, const char *mode)
+    {
+        std::cout << "<===Opening SVR DB file = './oic_svr_db_client.json' with mode = '" << mode
+                << "' " << std::endl;
+        return fopen("./oic_svr_db_client.json", mode);
+    }
+    OCPersistentStorage gps {client_open, fread, fwrite, fclose, unlink };
+
     // Callbacks
     OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> /*request*/)
     {
@@ -72,7 +75,9 @@ namespace OCPlatformTest
 
     OCResourceHandle RegisterResource(std::string uri, std::string type, std::string iface)
     {
-        PlatformConfig cfg = {};
+        PlatformConfig cfg
+        { OC::ServiceType::OutOfProc, OC::ModeType::Server, "0.0.0.0", 0,
+                OC::QualityOfService::LowQos, &gps };
         OCPlatform::Configure(cfg);
         EXPECT_EQ(OC_STACK_OK,OCPlatform::registerResource(
                                         resourceHandle, uri, type,
@@ -82,7 +87,9 @@ namespace OCPlatformTest
 
     OCResourceHandle RegisterResource(std::string uri, std::string type)
     {
-        PlatformConfig cfg = {};
+        PlatformConfig cfg
+        { OC::ServiceType::OutOfProc, OC::ModeType::Server, "0.0.0.0", 0,
+                OC::QualityOfService::LowQos, &gps };
         OCPlatform::Configure(cfg);
         EXPECT_EQ(OC_STACK_OK, OCPlatform::registerResource(
                                         resourceHandle, uri, type,
@@ -92,7 +99,9 @@ namespace OCPlatformTest
 
     OCResourceHandle RegisterResource(std::string uri)
     {
-        PlatformConfig cfg = {};
+        PlatformConfig cfg
+        { OC::ServiceType::OutOfProc, OC::ModeType::Server, "0.0.0.0", 0,
+                OC::QualityOfService::LowQos, &gps };
         OCPlatform::Configure(cfg);
         EXPECT_EQ(OC_STACK_OK, OCPlatform::registerResource(
                                         resourceHandle, uri, gResourceTypeName,
@@ -153,12 +162,13 @@ namespace OCPlatformTest
 
     TEST(ConfigureTest, ConfigureServerOutProc)
     {
-        PlatformConfig cfg {
+        PlatformConfig cfg
+        {
             OC::ServiceType::OutOfProc,
             OC::ModeType::Server,
             "0.0.0.0",
             0,
-            OC::QualityOfService::LowQos
+            OC::QualityOfService::LowQos, &gps
         };
         std::string uri = "/a/light67";
         std::string type = "core.light";
@@ -193,7 +203,7 @@ namespace OCPlatformTest
             OC::ModeType::Server,
             "0.0.0.0",
             0,
-            OC::QualityOfService::LowQos
+            OC::QualityOfService::LowQos, &gps
         };
         OCPlatform::Configure(cfg);
 
@@ -207,14 +217,15 @@ namespace OCPlatformTest
         std::string uri = "/a/light70";
         std::string type = "core.light";
         uint8_t gResourceProperty = 0;
-        PlatformConfig cfg {
+        PlatformConfig cfg
+        {
             OC::ServiceType::InProc,
             OC::ModeType::Client,
             "0.0.0.0",
             0,
-            OC::QualityOfService::LowQos
+            OC::QualityOfService::LowQos,
+            &gps
         };
-        OCPlatform::Configure(cfg);
 
         EXPECT_NO_THROW(OCPlatform::registerResource(
                  resourceHandle, uri, type,
@@ -252,14 +263,13 @@ namespace OCPlatformTest
     //PersistentStorageTest
     TEST(ConfigureTest, ConfigurePersistentStorage)
     {
-        OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
-        PlatformConfig cfg {
+         PlatformConfig cfg {
              OC::ServiceType::InProc,
              OC::ModeType::Both,
              "0.0.0.0",
              0,
              OC::QualityOfService::LowQos,
-             &ps
+             &gps
          };
          OCPlatform::Configure(cfg);
          EXPECT_NO_THROW(OCPlatform::setDefaultDeviceEntityHandler(nullptr));
@@ -268,14 +278,13 @@ namespace OCPlatformTest
     //PersistentStorageTest
     TEST(ConfigureTest, ConfigureNULLHandlersPersistentStorage)
     {
-        OCPersistentStorage ps {client_open, nullptr, nullptr, nullptr, nullptr };
         PlatformConfig cfg {
              OC::ServiceType::InProc,
              OC::ModeType::Both,
              "0.0.0.0",
              0,
              OC::QualityOfService::LowQos,
-             &ps
+             &gps
          };
          OCPlatform::Configure(cfg);
          EXPECT_NO_THROW(OCPlatform::setDefaultDeviceEntityHandler(nullptr));
@@ -690,7 +699,6 @@ namespace OCPlatformTest
         OCDeviceInfo deviceInfo;
 
         DuplicateString(&deviceInfo.deviceName, "myDeviceName");
-
         EXPECT_EQ(OC_STACK_OK, OCPlatform::registerDeviceInfo(deviceInfo));
         EXPECT_NO_THROW(DeleteDeviceInfo(deviceInfo));
     }