X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fprovisioning%2Fsrc%2FOCProvisioningManager.cpp;h=3c18238cad3a1f7e8a51e407188849402f5d3597;hb=refs%2Ftags%2Fsubmit%2Ftizen_4.0%2F20171010.021147;hp=8040a999c9853f4fbbee73751960b1ed257332f3;hpb=45c364268c4f5d575d98f4cd88b571b536c6cb17;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/provisioning/src/OCProvisioningManager.cpp b/resource/provisioning/src/OCProvisioningManager.cpp index 8040a99..3c18238 100644 --- a/resource/provisioning/src/OCProvisioningManager.cpp +++ b/resource/provisioning/src/OCProvisioningManager.cpp @@ -19,8 +19,11 @@ * *****************************************************************/ #include "ocstack.h" +#include "srmutility.h" #include "base64.h" -#include "OCProvisioningManager.h" +#include "OCProvisioningManager.hpp" +#include "mbedtls/x509_crt.h" + namespace OC { @@ -29,7 +32,7 @@ namespace OC OCStackResult result; auto cLock = OCPlatform_impl::Instance().csdkLock().lock(); - if(cLock) + if (cLock) { std::lock_guard lock(*cLock); result = OCInitPM(dbPath.c_str()); @@ -43,6 +46,23 @@ namespace OC return result; } + OCStackResult OCSecure::terminatePM() + { + auto cLock = OCPlatform_impl::Instance().csdkLock().lock(); + + if (cLock) + { + std::lock_guard lock(*cLock); + OCTerminatePM(); + return OC_STACK_OK; + } + else + { + oclog() <<"Mutex not found"; + return OC_STACK_ERROR; + } + } + OCStackResult OCSecure::discoverUnownedDevices(unsigned short timeout, DeviceList_t &list) { @@ -51,7 +71,7 @@ namespace OC auto csdkLock = OCPlatform_impl::Instance().csdkLock(); auto cLock = csdkLock.lock(); - if(cLock) + if (cLock) { std::lock_guard lock(*cLock); result = OCDiscoverUnownedDevices(timeout, &pDevList); @@ -90,7 +110,7 @@ namespace OC auto csdkLock = OCPlatform_impl::Instance().csdkLock(); auto cLock = csdkLock.lock(); - if(cLock) + if (cLock) { std::lock_guard lock(*cLock); result = OCDiscoverOwnedDevices(timeout, &pDevList); @@ -120,31 +140,154 @@ namespace OC return result; } - OCStackResult OCSecure::setOwnerTransferCallbackData(OicSecOxm_t oxm, - OTMCallbackData_t* callbackData, InputPinCallback inputPin) + OCStackResult OCSecure::discoverSingleDevice(unsigned short timeout, + const OicUuid_t* deviceID, + std::shared_ptr &foundDevice) { - if(NULL == callbackData || oxm >= OIC_OXM_COUNT) + OCStackResult result; + OCProvisionDev_t *pDev = nullptr; + auto csdkLock = OCPlatform_impl::Instance().csdkLock(); + auto cLock = csdkLock.lock(); + + if (cLock) { - oclog() <<"Invalid callbackData or OXM type"; - return OC_STACK_INVALID_PARAM; + std::lock_guard lock(*cLock); + result = OCDiscoverSingleDevice(timeout, deviceID, &pDev); + if (result == OC_STACK_OK) + { + if (pDev) + { + foundDevice.reset(new OCSecureResource(csdkLock, pDev)); + } + else + { + oclog() <<"Not found Secure resource!"; + foundDevice.reset(); + } + } + else + { + oclog() <<"Secure resource discovery failed!"; + } + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; } - if((OIC_RANDOM_DEVICE_PIN == oxm) && !inputPin) + return result; + } + + OCStackResult OCSecure::discoverSingleDeviceInUnicast(unsigned short timeout, + const OicUuid_t* deviceID, + const std::string& hostAddress, + OCConnectivityType connType, + std::shared_ptr &foundDevice) + { + OCStackResult result = OC_STACK_ERROR; + OCProvisionDev_t *pDev = nullptr; + auto csdkLock = OCPlatform_impl::Instance().csdkLock(); + auto cLock = csdkLock.lock(); + + if (cLock) { - oclog() <<"for OXM type DEVICE_PIN, inputPin callback can't be null"; - return OC_STACK_INVALID_PARAM; + std::lock_guard lock(*cLock); + result = OCDiscoverSingleDeviceInUnicast(timeout, deviceID, hostAddress.c_str(), + connType, &pDev); + + if (result == OC_STACK_OK) + { + if (pDev) + { + foundDevice.reset(new OCSecureResource(csdkLock, pDev)); + } + else + { + oclog() <<"Not found Secure resource!"; + foundDevice.reset(); + } + } + else + { + oclog() <<"Secure resource discovery failed!"; + } + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + + return result; + } + +#ifdef MULTIPLE_OWNER + OCStackResult OCSecure::discoverMultipleOwnerEnabledDevices(unsigned short timeout, + DeviceList_t &list) + { + OCStackResult result; + OCProvisionDev_t *pDevList = nullptr, *pCurDev = nullptr, *tmp = nullptr; + auto csdkLock = OCPlatform_impl::Instance().csdkLock(); + auto cLock = csdkLock.lock(); + + if (cLock) + { + std::lock_guard lock(*cLock); + result = OCDiscoverMultipleOwnerEnabledDevices(timeout, &pDevList); + if (result == OC_STACK_OK) + { + pCurDev = pDevList; + while (pCurDev) + { + tmp = pCurDev; + list.push_back(std::shared_ptr( + new OCSecureResource(csdkLock, pCurDev))); + pCurDev = pCurDev->next; + tmp->next = nullptr; + } + } + else + { + oclog() <<"MultipleOwner Enabled device discovery failed!"; + } } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + + return result; + } + OCStackResult OCSecure::discoverMultipleOwnedDevices(unsigned short timeout, + DeviceList_t &list) + { OCStackResult result; - auto cLock = OCPlatform_impl::Instance().csdkLock().lock(); + OCProvisionDev_t *pDevList = nullptr, *pCurDev = nullptr, *tmp = nullptr; + auto csdkLock = OCPlatform_impl::Instance().csdkLock(); + auto cLock = csdkLock.lock(); - if(cLock) + if (cLock) { std::lock_guard lock(*cLock); - result = OCSetOwnerTransferCallbackData(oxm, callbackData); - if(result == OC_STACK_OK && (OIC_RANDOM_DEVICE_PIN & oxm)) + result = OCDiscoverMultipleOwnedDevices(timeout, &pDevList); + if (result == OC_STACK_OK) + { + pCurDev = pDevList; + while (pCurDev) + { + tmp = pCurDev; + list.push_back(std::shared_ptr( + new OCSecureResource(csdkLock, pCurDev))); + pCurDev = pCurDev->next; + tmp->next = nullptr; + } + } + else { - SetInputPinCB(inputPin); + oclog() <<"Multiple Owned device discovery failed!"; } } else @@ -154,7 +297,66 @@ namespace OC } return result; + } + +#endif + OCStackResult OCSecure::setInputPinCallback(InputPinCallback inputPin) + { + OCStackResult result; + auto cLock = OCPlatform_impl::Instance().csdkLock().lock(); + + if (cLock) + { + std::lock_guard lock(*cLock); + SetInputPinCB(inputPin); + result = OC_STACK_OK; + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + + return result; + } + + + OCStackResult OCSecure::unsetInputPinCallback() + { + OCStackResult result; + auto cLock = OCPlatform_impl::Instance().csdkLock().lock(); + + if (cLock) + { + std::lock_guard lock(*cLock); + UnsetInputPinCB(); + result = OC_STACK_OK; + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + + return result; + } + + OCStackResult OCSecure::setRandomPinPolicy(size_t pinSize, OicSecPinType_t pinType) + { + OCStackResult result; + auto cLock = OCPlatform_impl::Instance().csdkLock().lock(); + if (cLock) + { + std::lock_guard lock(*cLock); + result = SetRandomPinPolicy(pinSize, pinType); + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + return result; } OCStackResult OCSecure::getDevInfoFromNetwork(unsigned short timeout, @@ -166,7 +368,7 @@ namespace OC auto csdkLock = OCPlatform_impl::Instance().csdkLock(); auto cLock = csdkLock.lock(); - if(cLock) + if (cLock) { std::lock_guard lock(*cLock); @@ -206,7 +408,7 @@ namespace OC OCStackResult OCSecure::setDisplayPinCB(GeneratePinCallback displayPin) { - if(!displayPin) + if (!displayPin) { oclog() <<"displayPin can't be null"; return OC_STACK_INVALID_PARAM; @@ -215,7 +417,7 @@ namespace OC OCStackResult result = OC_STACK_OK; auto cLock = OCPlatform_impl::Instance().csdkLock().lock(); - if(cLock) + if (cLock) { std::lock_guard lock(*cLock); SetGeneratePinCB(displayPin); @@ -229,60 +431,60 @@ namespace OC return result; } - void OCSecureResource::callbackWrapper(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError) + OCStackResult OCSecure::removeDeviceWithUuid(unsigned short waitTimeForOwnedDeviceDiscovery, + std::string uuid, + ResultCallBack resultCallback) { - PMResultList_t *results = nullptr; - ProvisionContext* context = static_cast(ctx); - - results = new PMResultList_t; - for (int i = 0; i < nOfRes; i++) + if (!resultCallback) { - results->push_back(arr[i]); + oclog() << "Result calback can't be null"; + return OC_STACK_INVALID_CALLBACK; } - std::thread exec(context->callback, results, hasError); - exec.detach(); - - delete context; - } + OCStackResult result; + auto cLock = OCPlatform_impl::Instance().csdkLock().lock(); - OCSecureResource::OCSecureResource(): m_csdkLock(std::weak_ptr()), - devPtr(nullptr) - { - } + if (cLock) + { + ProvisionContext* context = new ProvisionContext(resultCallback); - OCSecureResource::OCSecureResource(std::weak_ptr csdkLock, - OCProvisionDev_t *dPtr) - :m_csdkLock(csdkLock), devPtr(dPtr) - { - } + std::lock_guard lock(*cLock); - OCSecureResource::~OCSecureResource() - { - if(devPtr) + OicUuid_t targetDev; + result = ConvertStrToUuid(uuid.c_str(), &targetDev); + if(OC_STACK_OK == result) + { + result = OCRemoveDeviceWithUuid(static_cast(context), waitTimeForOwnedDeviceDiscovery, + &targetDev, &OCSecureResource::callbackWrapper); + } + else + { + oclog() <<"Can not convert struuid to uuid"; + } + } + else { - OCDeleteDiscoveredDevices(devPtr); + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; } + return result; } - OCStackResult OCSecureResource::doOwnershipTransfer(ResultCallBack resultCallback) + OCStackResult OCSecure::saveACL(const OicSecAcl_t* acl) { - if(!resultCallback) + if (!acl) { - oclog() <<"Result callback can't be null"; + oclog() <<"ACL can't be null"; return OC_STACK_INVALID_PARAM; } OCStackResult result; - auto cLock = m_csdkLock.lock(); + auto cLock = OCPlatform_impl::Instance().csdkLock().lock(); - if(cLock) + if (cLock) { - ProvisionContext* context = new ProvisionContext(resultCallback); - std::lock_guard lock(*cLock); - result = OCDoOwnershipTransfer(static_cast(context), - devPtr, &OCSecureResource::callbackWrapper); + result = OCSaveACL(const_cast(acl)); } else { @@ -292,26 +494,48 @@ namespace OC return result; } - OCStackResult OCSecureResource::provisionACL( const OicSecAcl_t* acl, - ResultCallBack resultCallback) + OCStackResult OCSecure::displayNumCallbackWrapper(void* ctx, + uint8_t verifNum[MUTUAL_VERIF_NUM_LEN]) { - if(!resultCallback || !acl) + uint8_t *number = NULL; + + DisplayNumContext* context = static_cast(ctx); + if (!context) { - oclog() <<"Result callback or ACL can't be null"; + oclog() << "Invalid context"; return OC_STACK_INVALID_PARAM; } - OCStackResult result; - auto cLock = m_csdkLock.lock(); + if (NULL != verifNum) { + number = new uint8_t[MUTUAL_VERIF_NUM_LEN]; + memcpy(number, verifNum, MUTUAL_VERIF_NUM_LEN); + } - if(cLock) + return context->callback(number); + } + + OCStackResult OCSecure::registerDisplayNumCallback(DisplayNumCB displayNumCB) + { + if(!displayNumCB) { - ProvisionContext* context = new ProvisionContext(resultCallback); + oclog() << "Failed to register callback for display."; + return OC_STACK_INVALID_CALLBACK; + } + + OCStackResult result = OCSecure::deregisterDisplayNumCallback(); + if (OC_STACK_OK != result) + { + oclog() << "Failed to de-register callback for display."< lock(*cLock); - result = OCProvisionACL(static_cast(context), - devPtr, const_cast(acl), - &OCSecureResource::callbackWrapper); + SetDisplayNumCB(static_cast(context), &OCSecure::displayNumCallbackWrapper); + result = OC_STACK_OK; } else { @@ -321,28 +545,21 @@ namespace OC return result; } - OCStackResult OCSecureResource::provisionCredentials(const Credential &cred, - const OCSecureResource &device2, ResultCallBack resultCallback) + OCStackResult OCSecure::deregisterDisplayNumCallback() { - if(!resultCallback) - { - oclog() << "Result calback can't be null"; - return OC_STACK_INVALID_PARAM; - } - OCStackResult result; - auto cLock = m_csdkLock.lock(); + auto cLock = OCPlatform_impl::Instance().csdkLock().lock(); - if(cLock) + if (cLock) { - ProvisionContext* context = new ProvisionContext(resultCallback); - std::lock_guard lock(*cLock); - result = OCProvisionCredentials(static_cast(context), - cred.getCredentialType(), - cred.getCredentialKeySize(), - devPtr, device2.getDevPtr(), - &OCSecureResource::callbackWrapper); + DisplayNumContext* context = static_cast(UnsetDisplayNumCB()); + if (context) + { + oclog() << "Delete registered display num context"<(ctx); + if (!context) { - oclog() << "Result calback can't be null"; + oclog() << "Invalid context"; return OC_STACK_INVALID_PARAM; } - OCStackResult result; - auto cLock = m_csdkLock.lock(); + return context->callback(); + } - if(cLock) + OCStackResult OCSecure::registerUserConfirmCallback(UserConfirmNumCB userConfirmCB) + { + if(!userConfirmCB) { - ProvisionContext* context = new ProvisionContext(resultCallback); + oclog() << "Failed to set callback for confirming verifying callback."; + return OC_STACK_INVALID_CALLBACK; + } + + OCStackResult result = OCSecure::deregisterUserConfirmCallback(); + if (OC_STACK_OK != result) + { + oclog() << "Failed to de-register callback for comfirm."< lock(*cLock); + SetUserConfirmCB(static_cast(context), &OCSecure::confirmUserCallbackWrapper); + result = OC_STACK_OK; + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + + OCStackResult OCSecure::deregisterUserConfirmCallback() + { + OCStackResult result; + auto cLock = OCPlatform_impl::Instance().csdkLock().lock(); + + if (cLock) + { + std::lock_guard lock(*cLock); + UserConfirmNumContext* context = static_cast(UnsetUserConfirmCB()); + if (context) + { + oclog() << "Delete registered user confirm context"< lock(*cLock); + SetVerifyOption(optionMask); + result = OC_STACK_OK; + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + + OCStackResult OCSecure::pdmCleanupForTimeout() + { + OCStackResult result; + auto cLock = OCPlatform_impl::Instance().csdkLock().lock(); + if (cLock) + { + result = OCPDMCleanupForTimeout(); + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + + OCStackResult OCSecure::configSelfOwnership() + { + OCStackResult result; + auto cLock = OCPlatform_impl::Instance().csdkLock().lock(); + if (cLock) + { + std::lock_guard lock(*cLock); + result = OCConfigSelfOwnership(); + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + +#if defined(__WITH_DTLS__) || defined(__WITH_TLS__) + OCStackResult OCSecure::saveTrustCertChain(uint8_t *trustCertChain, size_t chainSize, + OicEncodingType_t encodingType, uint16_t *credId) + { + if (!trustCertChain) + { + oclog() <<"trustCertChain can't be null"; + return OC_STACK_INVALID_PARAM; + } + if (!credId) + { + oclog() <<"cred ID can not be null"; + return OC_STACK_INVALID_PARAM; + } + + OCStackResult result; + auto cLock = OCPlatform_impl::Instance().csdkLock().lock(); + + if (cLock) + { + std::lock_guard lock(*cLock); + result = OCSaveTrustCertChain(trustCertChain, chainSize, encodingType, credId ); + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + + OCStackResult OCSecure::readTrustCertChain(uint16_t credId, uint8_t **trustCertChain, + size_t *chainSize) + { + OCStackResult result; + auto cLock = OCPlatform_impl::Instance().csdkLock().lock(); + + if (cLock) + { + std::lock_guard lock(*cLock); + result = OCReadTrustCertChain(credId, trustCertChain, chainSize); + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + + void OCSecure::certCallbackWrapper(void* ctx, uint16_t credId, uint8_t *trustCertChain, + size_t chainSize) + { + TrustCertChainContext* context = static_cast(ctx); + uint8_t *certChain = new uint8_t[chainSize]; + memcpy(certChain, trustCertChain, chainSize); + std::thread exec(context->callback, credId, certChain, chainSize); + exec.detach(); + delete context; + } + + OCStackResult OCSecure::registerTrustCertChangeNotifier(CertChainCallBack callback) + { + if (!callback) + { + oclog() <<"callback can not be null"; + return OC_STACK_INVALID_CALLBACK; + } + + OCStackResult result; + auto cLock = OCPlatform_impl::Instance().csdkLock().lock(); + + if (cLock) + { + TrustCertChainContext* context = new TrustCertChainContext(callback); + std::lock_guard lock(*cLock); + result = OCRegisterTrustCertChainNotifier(static_cast(context), + &OCSecure::certCallbackWrapper); + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + + + OCStackResult OCSecure::removeTrustCertChangeNotifier() + { + OCStackResult result; + auto cLock = OCPlatform_impl::Instance().csdkLock().lock(); + + if (cLock) + { + std::lock_guard lock(*cLock); + OCRemoveTrustCertChainNotifier(); + result = OC_STACK_OK; + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + + OCStackResult OCSecure::setDeviceIdSeed(const uint8_t* seed, size_t seedSize) + { + if (!seed) + { + oclog() <<"seed can not be null"; + return OC_STACK_INVALID_PARAM; + } + + OCStackResult result; + auto cLock = OCPlatform_impl::Instance().csdkLock().lock(); + + if (cLock) + { + std::lock_guard lock(*cLock); + result = SetDeviceIdSeed(seed, seedSize); + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + + int OCSecure::peerCertCallbackWrapper(void *ctx, const mbedtls_x509_crt *cert, + int depth) + { + OCStackResult ret = OC_STACK_ERROR; + + PeerCertContext *context = static_cast(ctx); + if (NULL == context) + { + oclog() << "Invalid Context"; + return 1; + } + + if (context->callback) + { + ret = context->callback(cert, depth); + } + + if (0 == depth) + { + delete context; + } + + return (OC_STACK_OK == ret)? 0 : 1; + } + + OCStackResult OCSecure::setPeerCertCallback(PeerCertCB peerCertCallback) + { + OCStackResult result; + // UNSET cb + if (NULL == peerCertCallback) + { + result = OCSetPeerCertCallback(NULL, NULL); + if (OC_STACK_OK != result) + { + oclog() << "OCSetPeerCertCallback() Failed"; + return result; + } + } + + auto cLock = OCPlatform_impl::Instance().csdkLock().lock(); + + if (cLock) + { + PeerCertContext *context = new PeerCertContext(peerCertCallback); + + std::lock_guard lock(*cLock); + result = OCSetPeerCertCallback(static_cast(context), + &OCSecure::peerCertCallbackWrapper); + if (OC_STACK_OK != result) + { + oclog() << "OCSetPeerCertCallback() Failed"; + } + } + else + { + oclog() << "Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + +#endif // __WITH_DTLS__ || __WITH_TLS__ + + void OCSecureResource::callbackWrapper(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError) + { + PMResultList_t *results = nullptr; + ProvisionContext* context = static_cast(ctx); + + try + { + results = new PMResultList_t; + } + catch (std::bad_alloc& e) + { + oclog() <<"Bad alloc exception"; + return; + } + + for (int i = 0; i < nOfRes; i++) + { + results->push_back(arr[i]); + } + + std::thread exec(context->callback, results, hasError); + exec.detach(); + + delete context; + } + + OCSecureResource::OCSecureResource(): m_csdkLock(std::weak_ptr()), + devPtr(nullptr) + { + } + + OCSecureResource::OCSecureResource(std::weak_ptr csdkLock, + OCProvisionDev_t *dPtr) + :m_csdkLock(csdkLock), devPtr(dPtr) + { + } + + OCSecureResource::~OCSecureResource() + { + if (devPtr) + { + OCDeleteDiscoveredDevices(devPtr); + } + } + + OCStackResult OCSecureResource::doOwnershipTransfer(ResultCallBack resultCallback) + { + if (!resultCallback) + { + oclog() <<"Result callback can't be null"; + return OC_STACK_INVALID_CALLBACK; + } + + OCStackResult result; + auto cLock = m_csdkLock.lock(); + + if (cLock) + { + ProvisionContext* context = new ProvisionContext(resultCallback); + + std::lock_guard lock(*cLock); + result = OCDoOwnershipTransfer(static_cast(context), + devPtr, &OCSecureResource::callbackWrapper); + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + +#ifdef MULTIPLE_OWNER + OCStackResult OCSecureResource::doMultipleOwnershipTransfer(ResultCallBack resultCallback) + { + if (!resultCallback) + { + oclog() <<"Result callback can't be null"; + return OC_STACK_INVALID_CALLBACK; + } + + OCStackResult result; + auto cLock = m_csdkLock.lock(); + + if (cLock) + { + ProvisionContext* context = new ProvisionContext(resultCallback); + + std::lock_guard lock(*cLock); + result = OCDoMultipleOwnershipTransfer(static_cast(context), + devPtr, &OCSecureResource::callbackWrapper); + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + + OCStackResult OCSecureResource::getSubOwnerList(UuidList_t &uuidList) + { + validateSecureResource(); + OicSecSubOwner_t* tmp = NULL; + for (tmp = devPtr->doxm->subOwners; tmp; tmp = tmp->next) + { + uuidList.push_back(tmp->uuid); + } + return OC_STACK_OK; + } + +#endif + OCStackResult OCSecureResource::provisionACL( const OicSecAcl_t* acl, + ResultCallBack resultCallback) + { + if (!acl) + { + oclog() <<"ACL can't be null"; + return OC_STACK_INVALID_PARAM; + } + if (!resultCallback) + { + oclog() <<"result callback can not be null"; + return OC_STACK_INVALID_CALLBACK; + } + + OCStackResult result; + auto cLock = m_csdkLock.lock(); + + if (cLock) + { + ProvisionContext* context = new ProvisionContext(resultCallback); + + std::lock_guard lock(*cLock); + result = OCProvisionACL(static_cast(context), + devPtr, const_cast(acl), + &OCSecureResource::callbackWrapper); + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + + OCStackResult OCSecureResource::provisionCredentials(const Credential &cred, + const OCSecureResource &device2, ResultCallBack resultCallback) + { + if (!resultCallback) + { + oclog() << "Result calback can't be null"; + return OC_STACK_INVALID_CALLBACK; + } + + OCStackResult result; + auto cLock = m_csdkLock.lock(); + + if (cLock) + { + ProvisionContext* context = new ProvisionContext(resultCallback); + + std::lock_guard lock(*cLock); + result = OCProvisionCredentials(static_cast(context), + cred.getCredentialType(), + cred.getCredentialKeySize(), + devPtr, device2.getDevPtr(), + &OCSecureResource::callbackWrapper); + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + + OCStackResult OCSecureResource::provisionPairwiseDevices(const Credential &cred, + const OicSecAcl_t* acl1, const OCSecureResource &device2, const OicSecAcl_t* acl2, + ResultCallBack resultCallback) + { + if (!resultCallback) + { + oclog() << "Result callback can not be null"; + return OC_STACK_INVALID_CALLBACK; + } + + OCStackResult result; + auto cLock = m_csdkLock.lock(); + + if (cLock) + { + ProvisionContext* context = new ProvisionContext(resultCallback); std::lock_guard lock(*cLock); result = OCProvisionPairwiseDevices(static_cast(context), @@ -388,16 +1094,16 @@ namespace OC OCStackResult OCSecureResource::unlinkDevices(const OCSecureResource &device2, ResultCallBack resultCallback) { - if(!resultCallback) + if (!resultCallback) { oclog() << "Result calback can't be null"; - return OC_STACK_INVALID_PARAM; + return OC_STACK_INVALID_CALLBACK; } OCStackResult result; auto cLock = m_csdkLock.lock(); - if(cLock) + if (cLock) { ProvisionContext* context = new ProvisionContext(resultCallback); @@ -417,16 +1123,16 @@ namespace OC OCStackResult OCSecureResource::removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery, ResultCallBack resultCallback) { - if(!resultCallback) + if (!resultCallback) { oclog() << "Result calback can't be null"; - return OC_STACK_INVALID_PARAM; + return OC_STACK_INVALID_CALLBACK; } OCStackResult result; auto cLock = m_csdkLock.lock(); - if(cLock) + if (cLock) { ProvisionContext* context = new ProvisionContext(resultCallback); @@ -450,7 +1156,7 @@ namespace OC auto devUuid = devPtr->doxm->deviceID; auto cLock = m_csdkLock.lock(); - if(cLock) + if (cLock) { std::lock_guard lock(*cLock); @@ -473,20 +1179,91 @@ namespace OC return result; } + OCStackResult OCSecureResource::provisionDirectPairing( const OicSecPconf_t* pconf, + ResultCallBack resultCallback) + { + if (!pconf) + { + oclog() <<"PCONF can't be null"; + return OC_STACK_INVALID_PARAM; + } + if (!resultCallback) + { + oclog() <<"result callback can not be null"; + return OC_STACK_INVALID_CALLBACK; + } + + OCStackResult result; + auto cLock = m_csdkLock.lock(); + + if (cLock) + { + ProvisionContext* context = new ProvisionContext(resultCallback); + + std::lock_guard lock(*cLock); + result = OCProvisionDirectPairing(static_cast(context), + devPtr, const_cast(pconf), + &OCSecureResource::callbackWrapper); + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + +#if defined(__WITH_DTLS__) || defined(__WITH_TLS__) + OCStackResult OCSecureResource::provisionTrustCertChain(OicSecCredType_t type, uint16_t credId, + ResultCallBack resultCallback) + { + if (SIGNED_ASYMMETRIC_KEY != type) + { + oclog() <<"Invalid key type"; + return OC_STACK_INVALID_PARAM; + } + if (!resultCallback) + { + oclog() <<"result callback can not be null"; + return OC_STACK_INVALID_CALLBACK; + } + + OCStackResult result; + auto cLock = m_csdkLock.lock(); + + if (cLock) + { + ProvisionContext* context = new ProvisionContext(resultCallback); + + std::lock_guard lock(*cLock); + result = OCProvisionTrustCertChain(static_cast(context), + type, credId, devPtr, + &OCSecureResource::callbackWrapper); + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } +#endif // __WITH_DTLS__ or __WITH_TLS__ + std::string OCSecureResource::getDeviceID() { - char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*)0)->id)) + 1] = {0,}; - uint32_t outLen = 0; - B64Result b64Ret = B64_OK; std::ostringstream deviceId(""); + char *devID = nullptr; validateSecureResource(); - b64Ret = b64Encode(devPtr->doxm->deviceID.id, sizeof(devPtr->doxm->deviceID.id), base64Buff, - sizeof(base64Buff), &outLen); - if (B64_OK == b64Ret) + if (OC_STACK_OK == ConvertUuidToStr(&(devPtr->doxm->deviceID), &devID)) + { + deviceId << devID; + free(devID); + } + else { - deviceId << base64Buff; + oclog() <<"Can not convert uuid to struuid"; } return deviceId.str(); } @@ -522,4 +1299,247 @@ namespace OC throw OCException("Incomplete secure resource", OC_STACK_RESOURCE_ERROR); } } + + OCStackResult OCSecureResource::getOTMethod(OicSecOxm_t* oxm) + { + if(!oxm) + { + oclog() << "Null param"; + return OC_STACK_INVALID_PARAM; + } + + OCStackResult result = OC_STACK_ERROR; + auto cLock = m_csdkLock.lock(); + if (cLock) + { + std::lock_guard lock(*cLock); + if(devPtr && devPtr->doxm) + { + result = OCSelectOwnershipTransferMethod(devPtr->doxm->oxm, devPtr->doxm->oxmLen, + oxm, SUPER_OWNER); + } + } + else + { + oclog() <<"Mutex not found"; + } + return result; + } + + +#ifdef MULTIPLE_OWNER + OCStackResult OCSecureResource::getMOTMethod( OicSecOxm_t* oxm) + { + if (!oxm) + { + oclog() << "Null param"; + return OC_STACK_INVALID_PARAM; + } + + OCStackResult result = OC_STACK_ERROR; + auto cLock = m_csdkLock.lock(); + if (cLock) + { + std::lock_guard lock(*cLock); + if (devPtr && devPtr->doxm) + { + result = OCSelectOwnershipTransferMethod(devPtr->doxm->oxm, devPtr->doxm->oxmLen, + oxm, SUB_OWNER); + } + } + else + { + oclog() <<"Mutex not found"; + } + return result; + } + + bool OCSecureResource::isMOTSupported() + { + if (devPtr && devPtr->doxm) + { + return (devPtr->doxm->mom ? true : false); + } + return false; + } + + bool OCSecureResource::isMOTEnabled() + { + if (devPtr && devPtr->doxm && devPtr->doxm->mom) + { + if (OIC_MULTIPLE_OWNER_DISABLE != devPtr->doxm->mom->mode) + { + return true; + } + } + return false; + } + + OCStackResult OCSecureResource::selectMOTMethod( const OicSecOxm_t oxmSelVal, + ResultCallBack resultCallback) + { + if (!resultCallback) + { + oclog() <<"result callback can not be null"; + return OC_STACK_INVALID_CALLBACK; + } + + OCStackResult result; + auto cLock = m_csdkLock.lock(); + + if (cLock) + { + ProvisionContext* context = new ProvisionContext(resultCallback); + + std::lock_guard lock(*cLock); + result = OCSelectMOTMethod(static_cast(context), + devPtr, oxmSelVal, + &OCSecureResource::callbackWrapper); + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + + OCStackResult OCSecureResource::changeMOTMode( const OicSecMomType_t momType, + ResultCallBack resultCallback) + { + if (!resultCallback) + { + oclog() <<"result callback can not be null"; + return OC_STACK_INVALID_CALLBACK; + } + + OCStackResult result; + auto cLock = m_csdkLock.lock(); + + if (cLock) + { + ProvisionContext* context = new ProvisionContext(resultCallback); + + std::lock_guard lock(*cLock); + result = OCChangeMOTMode(static_cast(context), + devPtr, momType, + &OCSecureResource::callbackWrapper); + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + + + OCStackResult OCSecureResource::addPreconfigPIN(const char* preconfPIN, + size_t preconfPINLength) + { + if (!preconfPIN) + { + oclog() <<"pre config pin can not be null"; + return OC_STACK_INVALID_PARAM; + } + if (preconfPINLength <= 0) + { + oclog() <<"pre config pin length can not be zero or less"; + return OC_STACK_INVALID_PARAM; + } + OCStackResult result; + auto cLock = m_csdkLock.lock(); + + if (cLock) + { + std::lock_guard lock(*cLock); + result = OCAddPreconfigPin(devPtr, preconfPIN, + preconfPINLength); + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + + OCStackResult OCSecureResource::provisionPreconfPin(const char * preconfPin, + size_t preconfPinLength, ResultCallBack resultCallback) + { + if (!resultCallback) + { + oclog() <<"result callback can not be null"; + return OC_STACK_INVALID_CALLBACK; + } + if (!preconfPin) + { + oclog() <<"pre config pin can not be null"; + return OC_STACK_INVALID_PARAM; + } + if (preconfPinLength <= 0) + { + oclog() <<"pre config pin length can not be zero or less"; + return OC_STACK_INVALID_PARAM; + } + + OCStackResult result; + auto cLock = m_csdkLock.lock(); + + if (cLock) + { + ProvisionContext* context = new ProvisionContext(resultCallback); + + std::lock_guard lock(*cLock); + result = OCProvisionPreconfigPin(static_cast(context), + devPtr, preconfPin, preconfPinLength, + &OCSecureResource::callbackWrapper); + } + else + { + oclog() <<"Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + + OCStackResult OCSecureResource::removeSubOwner(const OicUuid_t* subOwnerId, ResultCallBack resultCallback) + { + validateSecureResource(); + OCStackResult result; + auto cLock = m_csdkLock.lock(); + if (cLock) + { + ProvisionContext* context = new ProvisionContext(resultCallback); + result = OCRemoveSubOwner(static_cast(context), + devPtr, subOwnerId, &OCSecureResource::callbackWrapper); + } + else + { + oclog() << "Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + + OCStackResult OCSecureResource::removeAllSubOwner(ResultCallBack resultCallback) + { + validateSecureResource(); + OCStackResult result; + auto cLock = m_csdkLock.lock(); + if (cLock) + { + ProvisionContext* context = new ProvisionContext(resultCallback); + result =OCRemoveAllSubOwner(static_cast(context), + devPtr, &OCSecureResource::callbackWrapper); + } + else + { + oclog() << "Mutex not found"; + result = OC_STACK_ERROR; + } + return result; + } + +#endif // MULTIPLE_OWNER }