From 7c494d7b1a90349f353bf8f3d0f6cd17fe31df14 Mon Sep 17 00:00:00 2001 From: Dave Thaler Date: Wed, 30 Nov 2016 18:10:31 -0800 Subject: [PATCH] Platform ID must be a UUID to be valid Change-Id: Ifbbeb18fa2f2bf73c857ad3bfc25b3706d972d61 Signed-off-by: Dave Thaler Reviewed-on: https://gerrit.iotivity.org/gerrit/14991 Tested-by: jenkins-iotivity Reviewed-by: Phil Coval Reviewed-by: Dan Mihai --- resource/csdk/security/src/srmutility.c | 43 +++------------------------------ resource/csdk/stack/src/ocresource.c | 12 +++++++++ resource/csdk/stack/test/stacktests.cpp | 18 +++++++++++++- 3 files changed, 32 insertions(+), 41 deletions(-) diff --git a/resource/csdk/security/src/srmutility.c b/resource/csdk/security/src/srmutility.c index 93d1e45..6263113 100644 --- a/resource/csdk/security/src/srmutility.c +++ b/resource/csdk/security/src/srmutility.c @@ -25,6 +25,7 @@ #include "logger.h" #include "oic_malloc.h" #include "base64.h" +#include "ocrandom.h" #define TAG "OIC_SRM_UTILITY" @@ -139,44 +140,6 @@ exit: OCStackResult ConvertStrToUuid(const char* strUuid, OicUuid_t* uuid) { - if(NULL == strUuid || NULL == uuid) - { - OIC_LOG(ERROR, TAG, "ConvertStrToUuid : Invalid param"); - return OC_STACK_INVALID_PARAM; - } - - size_t urnIdx = 0; - size_t uuidIdx = 0; - size_t strUuidLen = 0; - char convertedUuid[UUID_LENGTH * 2] = {0}; - - strUuidLen = strlen(strUuid); - if(0 == strUuidLen) - { - OIC_LOG(INFO, TAG, "The empty string detected, The UUID will be converted to "\ - "\"00000000-0000-0000-0000-000000000000\""); - } - else if(UUID_LENGTH * 2 + 4 == strUuidLen) - { - for(uuidIdx=0, urnIdx=0; uuidIdx < UUID_LENGTH ; uuidIdx++, urnIdx+=2) - { - if(*(strUuid + urnIdx) == '-') - { - urnIdx++; - } - sscanf(strUuid + urnIdx, "%2hhx", &convertedUuid[uuidIdx]); - } - } - else - { - OIC_LOG(ERROR, TAG, "Invalid string uuid format, Please set the uuid as correct format"); - OIC_LOG(ERROR, TAG, "e.g) \"72616E64-5069-6E44-6576-557569643030\" (4-2-2-2-6)"); - OIC_LOG(ERROR, TAG, "e.g) \"\""); - - return OC_STACK_INVALID_PARAM; - } - - memcpy(uuid->id, convertedUuid, UUID_LENGTH); - - return OC_STACK_OK; + OCRandomUuidResult result = OCConvertStringToUuid(strUuid, uuid->id); + return (result == RAND_UUID_OK) ? OC_STACK_OK : OC_STACK_INVALID_PARAM; } diff --git a/resource/csdk/stack/src/ocresource.c b/resource/csdk/stack/src/ocresource.c index 0a0b126..65ea233 100755 --- a/resource/csdk/stack/src/ocresource.c +++ b/resource/csdk/stack/src/ocresource.c @@ -1432,6 +1432,18 @@ OCStackResult OCSetPlatformInfo(OCPlatformInfo info) goto exit; } + /* + * @todo (IOT-1541) There are several versions of a UUID structure and conversion + * methods scattered around the IoTivity code base. They need to be combined + * into one PAL API. + */ + uint8_t uuid[UUID_SIZE]; + if (OCConvertStringToUuid(info.platformID, uuid) != RAND_UUID_OK) + { + OIC_LOG(ERROR, TAG, "Platform ID is not a UUID."); + goto exit; + } + resource = FindResourceByUri(OC_RSRVD_PLATFORM_URI); if (!resource) { diff --git a/resource/csdk/stack/test/stacktests.cpp b/resource/csdk/stack/test/stacktests.cpp index 8fd4cd6..9f7acfd 100644 --- a/resource/csdk/stack/test/stacktests.cpp +++ b/resource/csdk/stack/test/stacktests.cpp @@ -63,7 +63,7 @@ namespace itst = iotivity::test; //----------------------------------------------------------------------------- static const char TAG[] = "TestHarness"; -char gDeviceUUID[] = "myDeviceUUID"; +char gDeviceUUID[] = "fe3f9a68-4931-4cb0-9ea4-81702b43116c"; char gManufacturerName[] = "myName"; static OCPrm_t pmSel; static char pinNumber; @@ -301,6 +301,22 @@ TEST(StackStart, SetPlatformInfoWithNoPlatformID) EXPECT_EQ(OC_STACK_OK, OCStop()); } +TEST(StackStart, SetPlatformInfoWithBadPlatformID) +{ + itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT); + EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER)); + + OCPlatformInfo info = + { + "myDeviceUUID", + gManufacturerName, + 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + + EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSetPlatformInfo(info)); + EXPECT_EQ(OC_STACK_OK, OCStop()); +} + TEST(StackStart, SetPlatformInfoWithNoManufacturerName) { itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT); -- 2.7.4