From 9aac67cc4521e49e7ad5f4ec12ab90c1bf28bf18 Mon Sep 17 00:00:00 2001 From: "js126.lee" Date: Tue, 22 Nov 2016 16:07:13 +0900 Subject: [PATCH] Add OCConvertStringToUuid on C_common 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 Reviewed-on: https://gerrit.iotivity.org/gerrit/14605 Tested-by: jenkins-iotivity Reviewed-by: Chul Lee Reviewed-by: Joonghwan Lee Reviewed-by: Randeep Singh (cherry picked from commit d026fc6e98db05280cb7e94aacffd1d7d09838d7) Reviewed-on: https://gerrit.iotivity.org/gerrit/14723 --- resource/c_common/ocrandom/include/ocrandom.h | 13 ++++++ resource/c_common/ocrandom/src/ocrandom.c | 41 ++++++++++++++++ .../src/adapter_util/ca_adapter_net_ssl.c | 54 ++-------------------- 3 files changed, 57 insertions(+), 51 deletions(-) diff --git a/resource/c_common/ocrandom/include/ocrandom.h b/resource/c_common/ocrandom/include/ocrandom.h index 7a91dea..d4af634 100644 --- a/resource/c_common/ocrandom/include/ocrandom.h +++ b/resource/c_common/ocrandom/include/ocrandom.h @@ -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 diff --git a/resource/c_common/ocrandom/src/ocrandom.c b/resource/c_common/ocrandom/src/ocrandom.c index 32c4b4b..e53c594 100644 --- a/resource/c_common/ocrandom/src/ocrandom.c +++ b/resource/c_common/ocrandom/src/ocrandom.c @@ -41,6 +41,11 @@ #ifdef HAVE_STDLIB_H #include #endif +#ifdef HAVE_STRING_H +#include +#elif defined(HAVE_STRINGS_H) +#include +#endif #ifdef HAVE_SYS_TIME_H #include #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; +} + diff --git a/resource/csdk/connectivity/src/adapter_util/ca_adapter_net_ssl.c b/resource/csdk/connectivity/src/adapter_util/ca_adapter_net_ssl.c index 0537f4c..2a009e1 100644 --- a/resource/csdk/connectivity/src/adapter_util/ca_adapter_net_ssl.c +++ b/resource/csdk/connectivity/src/adapter_util/ca_adapter_net_ssl.c @@ -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); } -- 2.7.4