Add OCConvertStringToUuid on C_common
authorjs126.lee <js126.lee@samsung.com>
Tue, 22 Nov 2016 07:07:13 +0000 (16:07 +0900)
committerRandeep Singh <randeep.s@samsung.com>
Thu, 24 Nov 2016 08:48:24 +0000 (08:48 +0000)
Patch 1,2: Expose ConvertStrToUUID on OC stack to use common
Ptach 3: Add OCConvertStringToUuid on C_common

Change-Id: Icf9b0ee1bda6fa4d914c696fbbdc27e0998a7262
Signed-off-by: js126.lee <js126.lee@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/14605
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Chul Lee <chuls.lee@samsung.com>
Reviewed-by: Joonghwan Lee <jh05.lee@samsung.com>
Reviewed-by: Randeep Singh <randeep.s@samsung.com>
(cherry picked from commit d026fc6e98db05280cb7e94aacffd1d7d09838d7)
Reviewed-on: https://gerrit.iotivity.org/gerrit/14723

resource/c_common/ocrandom/include/ocrandom.h
resource/c_common/ocrandom/src/ocrandom.c
resource/csdk/connectivity/src/adapter_util/ca_adapter_net_ssl.c

index 7a91dea..d4af634 100644 (file)
@@ -124,6 +124,19 @@ OCRandomUuidResult OCGenerateUuidString(char uuidString[UUID_STRING_SIZE]);
 OCRandomUuidResult OCConvertUuidToString(const uint8_t uuid[UUID_SIZE],
         char uuidString[UUID_STRING_SIZE]);
 
+/**
+ * Convert a C style string to a UUID based on RFC 4122
+ *
+ * @param[in] uuidString
+ *              a 37 byte length string to fill with the string
+ *              representation of the passed UUID.
+ * @param[out]  uuid
+ *              The 16 byte array filled with UUID data
+ * @retval RAND_UUID_OK for success, otherwise an error value
+ */
+OCRandomUuidResult OCConvertStringToUuid(const char uuidString[UUID_STRING_SIZE],
+                                         uint8_t uuid[UUID_SIZE]);
+
 #ifdef __cplusplus
 }
 #endif
index 32c4b4b..e53c594 100644 (file)
 #ifdef HAVE_STDLIB_H
 #include <stdlib.h>
 #endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#elif defined(HAVE_STRINGS_H)
+#include <strings.h>
+#endif
 #ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
 #endif
@@ -375,3 +380,39 @@ OCRandomUuidResult OCConvertUuidToString(const uint8_t uuid[UUID_SIZE],
 
     return RAND_UUID_OK;
 }
+
+OCRandomUuidResult OCConvertStringToUuid(const char uuidString[UUID_STRING_SIZE],
+                                         uint8_t uuid[UUID_SIZE])
+{
+    if(NULL == uuidString || NULL == uuid)
+    {
+        return RAND_UUID_INVALID_PARAM;
+    }
+
+    size_t urnIdx = 0;
+    size_t uuidIdx = 0;
+    size_t strUuidLen = 0;
+    char convertedUuid[UUID_SIZE * 2] = {0};
+
+    strUuidLen = strlen(uuidString);
+    if((UUID_STRING_SIZE - 1) == strUuidLen)
+    {
+        for(uuidIdx=0, urnIdx=0; uuidIdx < UUID_SIZE ; uuidIdx++, urnIdx+=2)
+        {
+            if(*(uuidString + urnIdx) == '-')
+            {
+                urnIdx++;
+            }
+            sscanf(uuidString + urnIdx, "%2hhx", &convertedUuid[uuidIdx]);
+        }
+    }
+    else
+    {
+        return RAND_UUID_CONVERT_ERROR;
+    }
+
+    memcpy(uuid, convertedUuid, UUID_SIZE);
+
+    return RAND_UUID_OK;
+}
+
index 0537f4c..2a009e1 100644 (file)
@@ -26,6 +26,7 @@
 #include "cacommon.h"
 #include "caipinterface.h"
 #include "oic_malloc.h"
+#include "ocrandom.h"
 #include "byte_array.h"
 #include "octhread.h"
 #include "timer.h"
@@ -1613,55 +1614,6 @@ void CAsetSslHandshakeCallback(CAErrorCallback tlsHandshakeCallback)
     g_sslCallback = tlsHandshakeCallback;
     OIC_LOG_V(DEBUG, NET_SSL_TAG, "Out %s", __func__);
 }
-// TODO move ConvertStrToUuid function to common module
-/*
- * Converts string UUID to CARemoteId_t
- *
- * @param strUuid Device UUID in string format
- * @param uuid converted UUID in CARemoteId_t format
- *
- * @return 0 for success.
- * */
-static int ConvertStrToUuid(const char* strUuid, CARemoteId_t* uuid)
-{
-    if(NULL == strUuid || NULL == uuid)
-    {
-        OIC_LOG(ERROR, NET_SSL_TAG, "ConvertStrToUuid : Invalid param");
-        return -1;
-    }
-
-    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, NET_SSL_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, NET_SSL_TAG, "Invalid string uuid format");
-        return -1;
-    }
-
-    memcpy(uuid->id, convertedUuid, UUID_LENGTH);
-    uuid->id_length = UUID_LENGTH;
-    return 0;
-}
 
 /* Read data from TLS connection
  */
@@ -1769,7 +1721,7 @@ CAResult_t CAdecryptSsl(const CASecureEndpoint_t *sep, uint8_t *data, uint32_t d
                 if (NULL != uuidPos)
                 {
                     memcpy(uuid, (char*) uuidPos + sizeof(UUID_PREFIX) - 1, UUID_LENGTH * 2 + 4);
-                    ret = ConvertStrToUuid(uuid, &peer->sep.identity);
+                    ret = OCConvertStringToUuid(uuid, peer->sep.identity.id);
                     SSL_CHECK_FAIL(peer, ret, "Failed to convert subject", 1,
                                           CA_STATUS_FAILED, MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
                 }
@@ -1783,7 +1735,7 @@ CAResult_t CAdecryptSsl(const CASecureEndpoint_t *sep, uint8_t *data, uint32_t d
                 if (NULL != userIdPos)
                 {
                     memcpy(uuid, (char*) userIdPos + sizeof(USERID_PREFIX) - 1, UUID_LENGTH * 2 + 4);
-                    ret = ConvertStrToUuid(uuid, &peer->sep.userId);
+                    ret = OCConvertStringToUuid(uuid, peer->sep.userId.id);
                     SSL_CHECK_FAIL(peer, ret, "Failed to convert subject alt name", 1,
                                       CA_STATUS_FAILED, MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
                 }