From af476a3642fa3a63cf5e996f5f761f5eec7eaead Mon Sep 17 00:00:00 2001 From: Parkhi Date: Wed, 7 Dec 2016 14:32:42 +0900 Subject: [PATCH] Support unicast discovery of secure resource for BLE transport in easy setup In a patch, https://gerrit.iotivity.org/gerrit/#/c/15183/, an api to discover a secure resource in unicast is being developed. So if an OCResource object given for easy setup is BLE transport, this patch will make a secure resource discovery happen in unicast due to its better efficiency. Change-Id: I65bacf090b6ef5cebdb101a705a58ebdb6d8b645 Signed-off-by: Jihun Ha Signed-off-by: Parkhi Reviewed-on: https://gerrit.iotivity.org/gerrit/15205 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi (cherry picked from commit 1b17f573c2ae4accb5888d69b6038cc252e43554) Reviewed-on: https://gerrit.iotivity.org/gerrit/15319 --- .../mediator/richsdk/src/EnrolleeSecurity.cpp | 132 ++++++++++++++++++++- .../mediator/richsdk/src/EnrolleeSecurity.h | 1 + 2 files changed, 127 insertions(+), 6 deletions(-) mode change 100644 => 100755 service/easy-setup/mediator/richsdk/src/EnrolleeSecurity.cpp diff --git a/service/easy-setup/mediator/richsdk/src/EnrolleeSecurity.cpp b/service/easy-setup/mediator/richsdk/src/EnrolleeSecurity.cpp old mode 100644 new mode 100755 index 7e39ff9..da70c09 --- a/service/easy-setup/mediator/richsdk/src/EnrolleeSecurity.cpp +++ b/service/easy-setup/mediator/richsdk/src/EnrolleeSecurity.cpp @@ -31,11 +31,21 @@ #include "oic_string.h" #include "utlist.h" #include "srmutility.h" +#include "internal/doxmresource.h" namespace OIC { namespace Service { + namespace + { + static const char COAP[] = "coap://"; + static const char COAPS[] = "coaps://"; + static const char COAP_TCP[] = "coap+tcp://"; + static const char COAP_GATT[] = "coap+gatt://"; + static const char COAP_RFCOMM[] = "coap+rfcomm://"; + } + #define MAX_PERMISSION_LENGTH (5) #define CREATE (1) #define READ (2) @@ -77,6 +87,69 @@ namespace OIC uuidString = uuidArray; } + std::string EnrolleeSecurity::getResourceDeviceAddress(const std::string& host) + { + size_t prefix_len = 0; + + if (host.compare(0, sizeof(COAP) - 1, COAP) == 0) + { + prefix_len = sizeof(COAP) - 1; + } + else if (host.compare(0, sizeof(COAPS) - 1, COAPS) == 0) + { + prefix_len = sizeof(COAPS) - 1; + } + else if (host.compare(0, sizeof(COAP_TCP) - 1, COAP_TCP) == 0) + { + prefix_len = sizeof(COAP_TCP) - 1; + } + else if (host.compare(0, sizeof(COAP_GATT) - 1, COAP_GATT) == 0) + { + prefix_len = sizeof(COAP_GATT) - 1; + } + else if (host.compare(0, sizeof(COAP_RFCOMM) - 1, COAP_RFCOMM) == 0) + { + prefix_len = sizeof(COAP_RFCOMM) - 1; + } + else + { + OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, + "Well-known prefix for connectivity is not found. Please check OCResource::setHost"); + return {}; + } + + // remove prefix + std::string host_token = host.substr(prefix_len); + + if (host_token[0] == '[') // IPv6 + { + size_t bracket = host_token.find(']'); + + // extract the ipv6 address + return host_token.substr(0, bracket + 1); + } + else + { + size_t dot = host_token.find('.'); + if (std::string::npos == dot) // MAC + { + size_t semi_count = std::count(host_token.begin(), host_token.end(), ':'); + if (semi_count > 5) { + size_t found_semi = host_token.find_last_of(':'); + host_token = host_token.substr(0, found_semi); + } + return host_token; + } + else // IPv4 + { + size_t colon = host_token.find(':'); + + // extract the ipv4 address + return host_token.substr(0, colon); + } + } + } + void EnrolleeSecurity::ownershipTransferCb(OC::PMResultList_t *result, int hasError) { OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "ownershipTransferCb IN"); @@ -112,9 +185,33 @@ namespace OIC return res; } - result = OCSecure::discoverSingleDevice(ES_SEC_DISCOVERY_TIMEOUT, - &uuid, - m_securedResource); + // If a discovered resource uses BLE transport, unicast for secure resource discovery is + // used. + if( m_ocResource->connectivityType() & CT_ADAPTER_GATT_BTLE ) + { + std::string GattAddress = getResourceDeviceAddress(m_ocResource->host()); + if(!GattAddress.empty()) + { + result = OCSecure::discoverSingleDeviceInUnicast(ES_SEC_DISCOVERY_TIMEOUT, + &uuid, + GattAddress, + m_ocResource->connectivityType(), + m_securedResource); + } + else + { + OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "GATT BTLE address format is wrong."); + res = ESResult:: ES_ERROR; + return res; + } + } + else + { + result = OCSecure::discoverSingleDevice(ES_SEC_DISCOVERY_TIMEOUT, + &uuid, + m_securedResource); + } + if (result != OC_STACK_OK) { OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "Secure Resource Discovery failed."); @@ -314,10 +411,33 @@ namespace OIC return res; } + // If a discovered resource uses BLE transport, unicast for secure resource discovery is + // used. + if( m_ocResource->connectivityType() & CT_ADAPTER_GATT_BTLE ) + { + std::string GattAddress = getResourceDeviceAddress(m_ocResource->host()); + if(!GattAddress.empty()) + { + result = OCSecure::discoverSingleDeviceInUnicast(ES_SEC_DISCOVERY_TIMEOUT, + &uuid, + GattAddress, + m_ocResource->connectivityType(), + ownedDevice); + } + else + { + OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "GATT BTLE address format is wrong."); + res = ESResult:: ES_ERROR; + return res; + } + } + else + { + result = OCSecure::discoverSingleDevice(ES_SEC_DISCOVERY_TIMEOUT, + &uuid, + ownedDevice); + } - result = OCSecure::discoverSingleDevice(ES_SEC_DISCOVERY_TIMEOUT, - &uuid, - ownedDevice); if (result != OC_STACK_OK) { OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "secureResource Discovery failed."); diff --git a/service/easy-setup/mediator/richsdk/src/EnrolleeSecurity.h b/service/easy-setup/mediator/richsdk/src/EnrolleeSecurity.h index 2e9b3fa..0c4f8c3 100755 --- a/service/easy-setup/mediator/richsdk/src/EnrolleeSecurity.h +++ b/service/easy-setup/mediator/richsdk/src/EnrolleeSecurity.h @@ -76,6 +76,7 @@ namespace OIC void ownershipTransferCb(OC::PMResultList_t *result, int hasError); void convertUUIDToString(const uint8_t uuid[UUID_SIZE], std::string& uuidString); + std::string getResourceDeviceAddress(const std::string& host); #if defined(__WITH_DTLS__) && defined(__WITH_TLS__) public: -- 2.7.4