1 /* *****************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * *****************************************************************/
22 #include "srmutility.h"
24 #include "OCProvisioningManager.hpp"
25 #include "mbedtls/x509_crt.h"
30 OCStackResult OCSecure::provisionInit(const std::string& dbPath)
33 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
37 std::lock_guard<std::recursive_mutex> lock(*cLock);
38 result = OCInitPM(dbPath.c_str());
42 oclog() <<"Mutex not found";
43 result = OC_STACK_ERROR;
49 OCStackResult OCSecure::terminatePM()
51 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
55 std::lock_guard<std::recursive_mutex> lock(*cLock);
61 oclog() <<"Mutex not found";
62 return OC_STACK_ERROR;
66 OCStackResult OCSecure::discoverUnownedDevices(unsigned short timeout,
70 OCProvisionDev_t *pDevList = nullptr, *pCurDev = nullptr, *tmp = nullptr;
71 auto csdkLock = OCPlatform_impl::Instance().csdkLock();
72 auto cLock = csdkLock.lock();
76 std::lock_guard<std::recursive_mutex> lock(*cLock);
77 result = OCDiscoverUnownedDevices(timeout, &pDevList);
78 if (result == OC_STACK_OK)
80 // Create DeviceList of OCSecureResource's
85 list.push_back(std::shared_ptr<OCSecureResource>(
86 new OCSecureResource(csdkLock, pCurDev)));
87 pCurDev = pCurDev->next;
93 oclog() <<"Unowned device discovery failed!";
98 oclog() <<"Mutex not found";
99 result = OC_STACK_ERROR;
105 OCStackResult OCSecure::discoverOwnedDevices(unsigned short timeout,
108 OCStackResult result;
109 OCProvisionDev_t *pDevList = nullptr, *pCurDev = nullptr, *tmp = nullptr;
110 auto csdkLock = OCPlatform_impl::Instance().csdkLock();
111 auto cLock = csdkLock.lock();
115 std::lock_guard<std::recursive_mutex> lock(*cLock);
116 result = OCDiscoverOwnedDevices(timeout, &pDevList);
117 if (result == OC_STACK_OK)
123 list.push_back(std::shared_ptr<OCSecureResource>(
124 new OCSecureResource(csdkLock, pCurDev)));
125 pCurDev = pCurDev->next;
131 oclog() <<"Owned device discovery failed!";
136 oclog() <<"Mutex not found";
137 result = OC_STACK_ERROR;
143 OCStackResult OCSecure::discoverSingleDevice(unsigned short timeout,
144 const OicUuid_t* deviceID,
145 std::shared_ptr<OCSecureResource> &foundDevice)
147 OCStackResult result;
148 OCProvisionDev_t *pDev = nullptr;
149 auto csdkLock = OCPlatform_impl::Instance().csdkLock();
150 auto cLock = csdkLock.lock();
154 std::lock_guard<std::recursive_mutex> lock(*cLock);
155 result = OCDiscoverSingleDevice(timeout, deviceID, &pDev);
156 if (result == OC_STACK_OK)
160 foundDevice.reset(new OCSecureResource(csdkLock, pDev));
164 oclog() <<"Not found Secure resource!";
170 oclog() <<"Secure resource discovery failed!";
175 oclog() <<"Mutex not found";
176 result = OC_STACK_ERROR;
182 OCStackResult OCSecure::discoverSingleDeviceInUnicast(unsigned short timeout,
183 const OicUuid_t* deviceID,
184 const std::string& hostAddress,
185 OCConnectivityType connType,
186 std::shared_ptr<OCSecureResource> &foundDevice)
188 OCStackResult result = OC_STACK_ERROR;
189 OCProvisionDev_t *pDev = nullptr;
190 auto csdkLock = OCPlatform_impl::Instance().csdkLock();
191 auto cLock = csdkLock.lock();
195 std::lock_guard<std::recursive_mutex> lock(*cLock);
196 result = OCDiscoverSingleDeviceInUnicast(timeout, deviceID, hostAddress.c_str(),
199 if (result == OC_STACK_OK)
203 foundDevice.reset(new OCSecureResource(csdkLock, pDev));
207 oclog() <<"Not found Secure resource!";
213 oclog() <<"Secure resource discovery failed!";
218 oclog() <<"Mutex not found";
219 result = OC_STACK_ERROR;
225 #ifdef MULTIPLE_OWNER
226 OCStackResult OCSecure::discoverMultipleOwnerEnabledDevices(unsigned short timeout,
229 OCStackResult result;
230 OCProvisionDev_t *pDevList = nullptr, *pCurDev = nullptr, *tmp = nullptr;
231 auto csdkLock = OCPlatform_impl::Instance().csdkLock();
232 auto cLock = csdkLock.lock();
236 std::lock_guard<std::recursive_mutex> lock(*cLock);
237 result = OCDiscoverMultipleOwnerEnabledDevices(timeout, &pDevList);
238 if (result == OC_STACK_OK)
244 list.push_back(std::shared_ptr<OCSecureResource>(
245 new OCSecureResource(csdkLock, pCurDev)));
246 pCurDev = pCurDev->next;
252 oclog() <<"MultipleOwner Enabled device discovery failed!";
257 oclog() <<"Mutex not found";
258 result = OC_STACK_ERROR;
264 OCStackResult OCSecure::discoverMultipleOwnedDevices(unsigned short timeout,
267 OCStackResult result;
268 OCProvisionDev_t *pDevList = nullptr, *pCurDev = nullptr, *tmp = nullptr;
269 auto csdkLock = OCPlatform_impl::Instance().csdkLock();
270 auto cLock = csdkLock.lock();
274 std::lock_guard<std::recursive_mutex> lock(*cLock);
275 result = OCDiscoverMultipleOwnedDevices(timeout, &pDevList);
276 if (result == OC_STACK_OK)
282 list.push_back(std::shared_ptr<OCSecureResource>(
283 new OCSecureResource(csdkLock, pCurDev)));
284 pCurDev = pCurDev->next;
290 oclog() <<"Multiple Owned device discovery failed!";
295 oclog() <<"Mutex not found";
296 result = OC_STACK_ERROR;
303 OCStackResult OCSecure::setInputPinCallback(InputPinCallback inputPin)
305 OCStackResult result;
306 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
310 std::lock_guard<std::recursive_mutex> lock(*cLock);
311 SetInputPinCB(inputPin);
312 result = OC_STACK_OK;
316 oclog() <<"Mutex not found";
317 result = OC_STACK_ERROR;
324 OCStackResult OCSecure::unsetInputPinCallback()
326 OCStackResult result;
327 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
331 std::lock_guard<std::recursive_mutex> lock(*cLock);
333 result = OC_STACK_OK;
337 oclog() <<"Mutex not found";
338 result = OC_STACK_ERROR;
344 OCStackResult OCSecure::setRandomPinPolicy(size_t pinSize, OicSecPinType_t pinType)
346 OCStackResult result;
347 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
351 std::lock_guard<std::recursive_mutex> lock(*cLock);
352 result = SetRandomPinPolicy(pinSize, pinType);
356 oclog() <<"Mutex not found";
357 result = OC_STACK_ERROR;
362 OCStackResult OCSecure::getDevInfoFromNetwork(unsigned short timeout,
363 DeviceList_t &ownedDevList,
364 DeviceList_t &unownedDevList)
366 OCStackResult result = OC_STACK_OK;
367 OCProvisionDev_t *owned = nullptr, *unowned = nullptr, *tmp = nullptr, *dev = nullptr;
368 auto csdkLock = OCPlatform_impl::Instance().csdkLock();
369 auto cLock = csdkLock.lock();
373 std::lock_guard<std::recursive_mutex> lock(*cLock);
375 result = OCGetDevInfoFromNetwork(timeout, &owned, &unowned);
377 if (result == OC_STACK_OK)
383 ownedDevList.push_back(std::shared_ptr<OCSecureResource>(
384 new OCSecureResource(csdkLock, dev)));
393 unownedDevList.push_back(std::shared_ptr<OCSecureResource>(
394 new OCSecureResource(csdkLock, dev)));
402 oclog() <<"Mutex not found";
403 result = OC_STACK_ERROR;
409 OCStackResult OCSecure::setDisplayPinCB(GeneratePinCallback displayPin)
413 oclog() <<"displayPin can't be null";
414 return OC_STACK_INVALID_PARAM;
417 OCStackResult result = OC_STACK_OK;
418 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
422 std::lock_guard<std::recursive_mutex> lock(*cLock);
423 SetGeneratePinCB(displayPin);
427 oclog() <<"Mutex not found";
428 result = OC_STACK_ERROR;
434 OCStackResult OCSecure::removeDeviceWithUuid(unsigned short waitTimeForOwnedDeviceDiscovery,
436 ResultCallBack resultCallback)
440 oclog() << "Result calback can't be null";
441 return OC_STACK_INVALID_CALLBACK;
444 OCStackResult result;
445 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
449 ProvisionContext* context = new ProvisionContext(resultCallback);
451 std::lock_guard<std::recursive_mutex> lock(*cLock);
454 result = ConvertStrToUuid(uuid.c_str(), &targetDev);
455 if(OC_STACK_OK == result)
457 result = OCRemoveDeviceWithUuid(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
458 &targetDev, &OCSecureResource::callbackWrapper);
462 oclog() <<"Can not convert struuid to uuid";
467 oclog() <<"Mutex not found";
468 result = OC_STACK_ERROR;
473 OCStackResult OCSecure::saveACL(const OicSecAcl_t* acl)
477 oclog() <<"ACL can't be null";
478 return OC_STACK_INVALID_PARAM;
481 OCStackResult result;
482 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
486 std::lock_guard<std::recursive_mutex> lock(*cLock);
487 result = OCSaveACL(const_cast<OicSecAcl_t*>(acl));
491 oclog() <<"Mutex not found";
492 result = OC_STACK_ERROR;
497 OCStackResult OCSecure::displayNumCallbackWrapper(void* ctx,
498 uint8_t verifNum[MUTUAL_VERIF_NUM_LEN])
500 uint8_t *number = NULL;
502 DisplayNumContext* context = static_cast<DisplayNumContext*>(ctx);
505 oclog() << "Invalid context";
506 return OC_STACK_INVALID_PARAM;
509 if (NULL != verifNum) {
510 number = new uint8_t[MUTUAL_VERIF_NUM_LEN];
511 memcpy(number, verifNum, MUTUAL_VERIF_NUM_LEN);
514 return context->callback(number);
517 OCStackResult OCSecure::registerDisplayNumCallback(DisplayNumCB displayNumCB)
521 oclog() << "Failed to register callback for display.";
522 return OC_STACK_INVALID_CALLBACK;
525 OCStackResult result = OCSecure::deregisterDisplayNumCallback();
526 if (OC_STACK_OK != result)
528 oclog() << "Failed to de-register callback for display."<<std::endl;
532 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
535 DisplayNumContext* context = new DisplayNumContext(displayNumCB);
536 std::lock_guard<std::recursive_mutex> lock(*cLock);
537 SetDisplayNumCB(static_cast<void*>(context), &OCSecure::displayNumCallbackWrapper);
538 result = OC_STACK_OK;
542 oclog() <<"Mutex not found";
543 result = OC_STACK_ERROR;
548 OCStackResult OCSecure::deregisterDisplayNumCallback()
550 OCStackResult result;
551 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
555 std::lock_guard<std::recursive_mutex> lock(*cLock);
556 DisplayNumContext* context = static_cast<DisplayNumContext*>(UnsetDisplayNumCB());
559 oclog() << "Delete registered display num context"<<std::endl;
562 result = OC_STACK_OK;
566 oclog() <<"Mutex not found";
567 result = OC_STACK_ERROR;
572 OCStackResult OCSecure::confirmUserCallbackWrapper(void* ctx)
574 UserConfirmNumContext* context = static_cast<UserConfirmNumContext*>(ctx);
577 oclog() << "Invalid context";
578 return OC_STACK_INVALID_PARAM;
581 return context->callback();
584 OCStackResult OCSecure::registerUserConfirmCallback(UserConfirmNumCB userConfirmCB)
588 oclog() << "Failed to set callback for confirming verifying callback.";
589 return OC_STACK_INVALID_CALLBACK;
592 OCStackResult result = OCSecure::deregisterUserConfirmCallback();
593 if (OC_STACK_OK != result)
595 oclog() << "Failed to de-register callback for comfirm."<<std::endl;
599 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
602 UserConfirmNumContext* context = new UserConfirmNumContext(userConfirmCB);
603 std::lock_guard<std::recursive_mutex> lock(*cLock);
604 SetUserConfirmCB(static_cast<void*>(context), &OCSecure::confirmUserCallbackWrapper);
605 result = OC_STACK_OK;
609 oclog() <<"Mutex not found";
610 result = OC_STACK_ERROR;
615 OCStackResult OCSecure::deregisterUserConfirmCallback()
617 OCStackResult result;
618 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
622 std::lock_guard<std::recursive_mutex> lock(*cLock);
623 UserConfirmNumContext* context = static_cast<UserConfirmNumContext*>(UnsetUserConfirmCB());
626 oclog() << "Delete registered user confirm context"<<std::endl;
629 result = OC_STACK_OK;
633 oclog() <<"Mutex not found";
634 result = OC_STACK_ERROR;
639 OCStackResult OCSecure::setVerifyOptionMask(VerifyOptionBitmask_t optionMask)
641 OCStackResult result;
642 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
645 std::lock_guard<std::recursive_mutex> lock(*cLock);
646 SetVerifyOption(optionMask);
647 result = OC_STACK_OK;
651 oclog() <<"Mutex not found";
652 result = OC_STACK_ERROR;
657 OCStackResult OCSecure::pdmCleanupForTimeout()
659 OCStackResult result;
660 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
663 result = OCPDMCleanupForTimeout();
667 oclog() <<"Mutex not found";
668 result = OC_STACK_ERROR;
673 OCStackResult OCSecure::configSelfOwnership()
675 OCStackResult result;
676 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
679 std::lock_guard<std::recursive_mutex> lock(*cLock);
680 result = OCConfigSelfOwnership();
684 oclog() <<"Mutex not found";
685 result = OC_STACK_ERROR;
690 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
691 OCStackResult OCSecure::saveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
692 OicEncodingType_t encodingType, uint16_t *credId)
696 oclog() <<"trustCertChain can't be null";
697 return OC_STACK_INVALID_PARAM;
701 oclog() <<"cred ID can not be null";
702 return OC_STACK_INVALID_PARAM;
705 OCStackResult result;
706 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
710 std::lock_guard<std::recursive_mutex> lock(*cLock);
711 result = OCSaveTrustCertChain(trustCertChain, chainSize, encodingType, credId );
715 oclog() <<"Mutex not found";
716 result = OC_STACK_ERROR;
721 OCStackResult OCSecure::readTrustCertChain(uint16_t credId, uint8_t **trustCertChain,
724 OCStackResult result;
725 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
729 std::lock_guard<std::recursive_mutex> lock(*cLock);
730 result = OCReadTrustCertChain(credId, trustCertChain, chainSize);
734 oclog() <<"Mutex not found";
735 result = OC_STACK_ERROR;
740 void OCSecure::certCallbackWrapper(void* ctx, uint16_t credId, uint8_t *trustCertChain,
743 TrustCertChainContext* context = static_cast<TrustCertChainContext*>(ctx);
744 uint8_t *certChain = new uint8_t[chainSize];
745 memcpy(certChain, trustCertChain, chainSize);
746 std::thread exec(context->callback, credId, certChain, chainSize);
751 OCStackResult OCSecure::registerTrustCertChangeNotifier(CertChainCallBack callback)
755 oclog() <<"callback can not be null";
756 return OC_STACK_INVALID_CALLBACK;
759 OCStackResult result;
760 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
764 TrustCertChainContext* context = new TrustCertChainContext(callback);
765 std::lock_guard<std::recursive_mutex> lock(*cLock);
766 result = OCRegisterTrustCertChainNotifier(static_cast<void*>(context),
767 &OCSecure::certCallbackWrapper);
771 oclog() <<"Mutex not found";
772 result = OC_STACK_ERROR;
778 OCStackResult OCSecure::removeTrustCertChangeNotifier()
780 OCStackResult result;
781 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
785 std::lock_guard<std::recursive_mutex> lock(*cLock);
786 OCRemoveTrustCertChainNotifier();
787 result = OC_STACK_OK;
791 oclog() <<"Mutex not found";
792 result = OC_STACK_ERROR;
797 OCStackResult OCSecure::setDeviceIdSeed(const uint8_t* seed, size_t seedSize)
801 oclog() <<"seed can not be null";
802 return OC_STACK_INVALID_PARAM;
805 return SetDeviceIdSeed(seed, seedSize);
808 int OCSecure::peerCertCallbackWrapper(void *ctx, const mbedtls_x509_crt *cert,
811 OCStackResult ret = OC_STACK_ERROR;
813 PeerCertContext *context = static_cast<PeerCertContext*>(ctx);
816 oclog() << "Invalid Context";
820 if (context->callback)
822 ret = context->callback(cert, depth);
825 return (OC_STACK_OK == ret)? 0 : 1;
828 OCStackResult OCSecure::setPeerCertCallback(PeerCertCB peerCertCallback)
830 OCStackResult result;
832 if (NULL == peerCertCallback)
834 result = OCSetPeerCertCallback(NULL, NULL);
835 if (OC_STACK_OK != result)
837 oclog() << "OCSetPeerCertCallback() Failed";
842 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
846 PeerCertContext *context = new PeerCertContext(peerCertCallback);
848 std::lock_guard<std::recursive_mutex> lock(*cLock);
849 result = OCSetPeerCertCallback(static_cast<void*>(context),
850 &OCSecure::peerCertCallbackWrapper);
851 if (OC_STACK_OK != result)
853 oclog() << "OCSetPeerCertCallback() Failed";
858 oclog() << "Mutex not found";
859 result = OC_STACK_ERROR;
864 #endif // __WITH_DTLS__ || __WITH_TLS__
866 void OCSecureResource::callbackWrapper(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
868 PMResultList_t *results = nullptr;
869 ProvisionContext* context = static_cast<ProvisionContext*>(ctx);
873 results = new PMResultList_t;
875 catch (std::bad_alloc& e)
877 oclog() <<"Bad alloc exception";
881 for (int i = 0; i < nOfRes; i++)
883 results->push_back(arr[i]);
886 std::thread exec(context->callback, results, hasError);
892 OCSecureResource::OCSecureResource(): m_csdkLock(std::weak_ptr<std::recursive_mutex>()),
898 OCSecureResource::OCSecureResource(std::weak_ptr<std::recursive_mutex> csdkLock,
899 OCProvisionDev_t *dPtr)
900 :m_csdkLock(csdkLock), devPtr(dPtr), context(nullptr)
904 OCSecureResource::~OCSecureResource()
908 OCDeleteDiscoveredDevices(devPtr);
916 OCStackResult OCSecureResource::doOwnershipTransfer(ResultCallBack resultCallback)
920 oclog() <<"Result callback can't be null";
921 return OC_STACK_INVALID_CALLBACK;
924 OCStackResult result;
925 auto cLock = m_csdkLock.lock();
929 ProvisionContext* context = new ProvisionContext(resultCallback);
934 context = new ProvisionContext(resultCallback);
936 std::lock_guard<std::recursive_mutex> lock(*cLock);
937 result = OCDoOwnershipTransfer(static_cast<void*>(context),
938 devPtr, &OCSecureResource::callbackWrapper);
942 oclog() <<"Mutex not found";
943 result = OC_STACK_ERROR;
948 OCStackResult OCSecureResource::doOwnershipTransfer(ResultCallBack resultCallback, const OicSecOxm_t method)
952 oclog() <<"Result callback can't be null";
953 return OC_STACK_INVALID_CALLBACK;
956 OCStackResult result;
957 auto cLock = m_csdkLock.lock();
965 context = new ProvisionContext(resultCallback);
967 std::lock_guard<std::recursive_mutex> lock(*cLock);
968 result = OCDoCustomOwnershipTransfer(static_cast<void*>(context),
969 devPtr, &OCSecureResource::callbackWrapper, method);
973 oclog() <<"Mutex not found";
974 result = OC_STACK_ERROR;
979 #ifdef MULTIPLE_OWNER
980 OCStackResult OCSecureResource::doMultipleOwnershipTransfer(ResultCallBack resultCallback)
984 oclog() <<"Result callback can't be null";
985 return OC_STACK_INVALID_CALLBACK;
988 OCStackResult result;
989 auto cLock = m_csdkLock.lock();
993 ProvisionContext* context = new ProvisionContext(resultCallback);
995 std::lock_guard<std::recursive_mutex> lock(*cLock);
996 result = OCDoMultipleOwnershipTransfer(static_cast<void*>(context),
997 devPtr, &OCSecureResource::callbackWrapper);
1001 oclog() <<"Mutex not found";
1002 result = OC_STACK_ERROR;
1007 OCStackResult OCSecureResource::getSubOwnerList(UuidList_t &uuidList)
1009 validateSecureResource();
1010 OicSecSubOwner_t* tmp = NULL;
1011 for (tmp = devPtr->doxm->subOwners; tmp; tmp = tmp->next)
1013 uuidList.push_back(tmp->uuid);
1019 OCStackResult OCSecureResource::provisionACL( const OicSecAcl_t* acl,
1020 ResultCallBack resultCallback)
1024 oclog() <<"ACL can't be null";
1025 return OC_STACK_INVALID_PARAM;
1027 if (!resultCallback)
1029 oclog() <<"result callback can not be null";
1030 return OC_STACK_INVALID_CALLBACK;
1033 OCStackResult result;
1034 auto cLock = m_csdkLock.lock();
1038 ProvisionContext* context = new ProvisionContext(resultCallback);
1040 std::lock_guard<std::recursive_mutex> lock(*cLock);
1041 result = OCProvisionACL(static_cast<void*>(context),
1042 devPtr, const_cast<OicSecAcl_t*>(acl),
1043 &OCSecureResource::callbackWrapper);
1047 oclog() <<"Mutex not found";
1048 result = OC_STACK_ERROR;
1053 OCStackResult OCSecureResource::provisionCredentials(const Credential &cred,
1054 const OCSecureResource &device2, ResultCallBack resultCallback)
1056 if (!resultCallback)
1058 oclog() << "Result calback can't be null";
1059 return OC_STACK_INVALID_CALLBACK;
1062 OCStackResult result;
1063 auto cLock = m_csdkLock.lock();
1067 ProvisionContext* context = new ProvisionContext(resultCallback);
1069 std::lock_guard<std::recursive_mutex> lock(*cLock);
1070 result = OCProvisionCredentials(static_cast<void*>(context),
1071 cred.getCredentialType(),
1072 cred.getCredentialKeySize(),
1073 devPtr, device2.getDevPtr(),
1074 &OCSecureResource::callbackWrapper);
1078 oclog() <<"Mutex not found";
1079 result = OC_STACK_ERROR;
1084 OCStackResult OCSecureResource::provisionPairwiseDevices(const Credential &cred,
1085 const OicSecAcl_t* acl1, const OCSecureResource &device2, const OicSecAcl_t* acl2,
1086 ResultCallBack resultCallback)
1088 if (!resultCallback)
1090 oclog() << "Result callback can not be null";
1091 return OC_STACK_INVALID_CALLBACK;
1094 OCStackResult result;
1095 auto cLock = m_csdkLock.lock();
1099 ProvisionContext* context = new ProvisionContext(resultCallback);
1101 std::lock_guard<std::recursive_mutex> lock(*cLock);
1102 result = OCProvisionPairwiseDevices(static_cast<void*>(context),
1103 cred.getCredentialType(),
1104 cred.getCredentialKeySize(),
1105 devPtr, const_cast<OicSecAcl_t*>(acl1),
1106 device2.getDevPtr(), const_cast<OicSecAcl_t*>(acl2),
1107 &OCSecureResource::callbackWrapper);
1111 oclog() <<"Mutex not found";
1112 result = OC_STACK_ERROR;
1117 OCStackResult OCSecureResource::unlinkDevices(const OCSecureResource &device2,
1118 ResultCallBack resultCallback)
1120 if (!resultCallback)
1122 oclog() << "Result calback can't be null";
1123 return OC_STACK_INVALID_CALLBACK;
1126 OCStackResult result;
1127 auto cLock = m_csdkLock.lock();
1131 ProvisionContext* context = new ProvisionContext(resultCallback);
1133 std::lock_guard<std::recursive_mutex> lock(*cLock);
1135 result = OCUnlinkDevices(static_cast<void*>(context),
1136 devPtr, device2.getDevPtr(), &OCSecureResource::callbackWrapper);
1140 oclog() <<"Mutex not found";
1141 result = OC_STACK_ERROR;
1146 OCStackResult OCSecureResource::removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery,
1147 ResultCallBack resultCallback)
1149 if (!resultCallback)
1151 oclog() << "Result calback can't be null";
1152 return OC_STACK_INVALID_CALLBACK;
1155 OCStackResult result;
1156 auto cLock = m_csdkLock.lock();
1160 ProvisionContext* context = new ProvisionContext(resultCallback);
1162 std::lock_guard<std::recursive_mutex> lock(*cLock);
1164 result = OCRemoveDevice(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
1165 devPtr, &OCSecureResource::callbackWrapper);
1169 oclog() <<"Mutex not found";
1170 result = OC_STACK_ERROR;
1175 OCStackResult OCSecureResource::getLinkedDevices(UuidList_t &uuidList)
1177 OCStackResult result;
1178 size_t numOfDevices = -1;
1179 auto devUuid = devPtr->doxm->deviceID;
1180 auto cLock = m_csdkLock.lock();
1184 std::lock_guard<std::recursive_mutex> lock(*cLock);
1186 OCUuidList_t* linkedDevs = nullptr, *tmp = nullptr;
1187 result = OCGetLinkedStatus(&devUuid, &linkedDevs, &numOfDevices);
1188 if (result == OC_STACK_OK)
1190 for (tmp = linkedDevs; tmp; tmp = tmp->next)
1192 uuidList.push_back(tmp->dev);
1194 OCDeleteUuidList(linkedDevs);
1199 oclog() <<"Mutex not found";
1200 result = OC_STACK_ERROR;
1205 OCStackResult OCSecureResource::provisionDirectPairing( const OicSecPconf_t* pconf,
1206 ResultCallBack resultCallback)
1210 oclog() <<"PCONF can't be null";
1211 return OC_STACK_INVALID_PARAM;
1213 if (!resultCallback)
1215 oclog() <<"result callback can not be null";
1216 return OC_STACK_INVALID_CALLBACK;
1219 OCStackResult result;
1220 auto cLock = m_csdkLock.lock();
1224 ProvisionContext* context = new ProvisionContext(resultCallback);
1226 std::lock_guard<std::recursive_mutex> lock(*cLock);
1227 result = OCProvisionDirectPairing(static_cast<void*>(context),
1228 devPtr, const_cast<OicSecPconf_t*>(pconf),
1229 &OCSecureResource::callbackWrapper);
1233 oclog() <<"Mutex not found";
1234 result = OC_STACK_ERROR;
1239 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
1240 OCStackResult OCSecureResource::provisionTrustCertChain(OicSecCredType_t type, uint16_t credId,
1241 ResultCallBack resultCallback)
1243 if (SIGNED_ASYMMETRIC_KEY != type)
1245 oclog() <<"Invalid key type";
1246 return OC_STACK_INVALID_PARAM;
1248 if (!resultCallback)
1250 oclog() <<"result callback can not be null";
1251 return OC_STACK_INVALID_CALLBACK;
1254 OCStackResult result;
1255 auto cLock = m_csdkLock.lock();
1259 ProvisionContext* context = new ProvisionContext(resultCallback);
1261 std::lock_guard<std::recursive_mutex> lock(*cLock);
1262 result = OCProvisionTrustCertChain(static_cast<void*>(context),
1263 type, credId, devPtr,
1264 &OCSecureResource::callbackWrapper);
1268 oclog() <<"Mutex not found";
1269 result = OC_STACK_ERROR;
1273 #endif // __WITH_DTLS__ or __WITH_TLS__
1275 std::string OCSecureResource::getDeviceID()
1277 std::ostringstream deviceId("");
1278 char *devID = nullptr;
1280 validateSecureResource();
1282 if (OC_STACK_OK == ConvertUuidToStr(&(devPtr->doxm->deviceID), &devID))
1289 oclog() <<"Can not convert uuid to struuid";
1291 return deviceId.str();
1294 OCProvisionDev_t* OCSecureResource::getDevPtr() const
1299 std::string OCSecureResource::getDevAddr()
1301 validateSecureResource();
1302 std::string ipAddr(devPtr->endpoint.addr);
1306 int OCSecureResource::getDeviceStatus()
1308 validateSecureResource();
1309 return (int)devPtr->devStatus;
1312 bool OCSecureResource::getOwnedStatus()
1314 validateSecureResource();
1315 return devPtr->doxm->owned;
1318 void OCSecureResource::validateSecureResource()
1322 throw OCException("Incomplete secure resource", OC_STACK_RESOURCE_ERROR);
1326 OCStackResult OCSecureResource::getOTMethod(OicSecOxm_t* oxm)
1330 oclog() << "Null param";
1331 return OC_STACK_INVALID_PARAM;
1334 OCStackResult result = OC_STACK_ERROR;
1335 auto cLock = m_csdkLock.lock();
1338 std::lock_guard<std::recursive_mutex> lock(*cLock);
1339 if(devPtr && devPtr->doxm)
1341 result = OCSelectOwnershipTransferMethod(devPtr->doxm->oxm, devPtr->doxm->oxmLen,
1347 oclog() <<"Mutex not found";
1353 #ifdef MULTIPLE_OWNER
1354 OCStackResult OCSecureResource::getMOTMethod( OicSecOxm_t* oxm)
1358 oclog() << "Null param";
1359 return OC_STACK_INVALID_PARAM;
1362 OCStackResult result = OC_STACK_ERROR;
1363 auto cLock = m_csdkLock.lock();
1366 std::lock_guard<std::recursive_mutex> lock(*cLock);
1367 if (devPtr && devPtr->doxm)
1369 result = OCSelectOwnershipTransferMethod(devPtr->doxm->oxm, devPtr->doxm->oxmLen,
1375 oclog() <<"Mutex not found";
1380 bool OCSecureResource::isMOTSupported()
1382 if (devPtr && devPtr->doxm)
1384 return (devPtr->doxm->mom ? true : false);
1389 bool OCSecureResource::isMOTEnabled()
1391 if (devPtr && devPtr->doxm && devPtr->doxm->mom)
1393 if (OIC_MULTIPLE_OWNER_DISABLE != devPtr->doxm->mom->mode)
1401 OCStackResult OCSecureResource::selectMOTMethod( const OicSecOxm_t oxmSelVal,
1402 ResultCallBack resultCallback)
1404 if (!resultCallback)
1406 oclog() <<"result callback can not be null";
1407 return OC_STACK_INVALID_CALLBACK;
1410 OCStackResult result;
1411 auto cLock = m_csdkLock.lock();
1415 ProvisionContext* context = new ProvisionContext(resultCallback);
1417 std::lock_guard<std::recursive_mutex> lock(*cLock);
1418 result = OCSelectMOTMethod(static_cast<void*>(context),
1420 &OCSecureResource::callbackWrapper);
1424 oclog() <<"Mutex not found";
1425 result = OC_STACK_ERROR;
1430 OCStackResult OCSecureResource::changeMOTMode( const OicSecMomType_t momType,
1431 ResultCallBack resultCallback)
1433 if (!resultCallback)
1435 oclog() <<"result callback can not be null";
1436 return OC_STACK_INVALID_CALLBACK;
1439 OCStackResult result;
1440 auto cLock = m_csdkLock.lock();
1444 ProvisionContext* context = new ProvisionContext(resultCallback);
1446 std::lock_guard<std::recursive_mutex> lock(*cLock);
1447 result = OCChangeMOTMode(static_cast<void*>(context),
1449 &OCSecureResource::callbackWrapper);
1453 oclog() <<"Mutex not found";
1454 result = OC_STACK_ERROR;
1460 OCStackResult OCSecureResource::addPreconfigPIN(const char* preconfPIN,
1461 size_t preconfPINLength)
1465 oclog() <<"pre config pin can not be null";
1466 return OC_STACK_INVALID_PARAM;
1468 if (preconfPINLength <= 0)
1470 oclog() <<"pre config pin length can not be zero or less";
1471 return OC_STACK_INVALID_PARAM;
1473 OCStackResult result;
1474 auto cLock = m_csdkLock.lock();
1478 std::lock_guard<std::recursive_mutex> lock(*cLock);
1479 result = OCAddPreconfigPin(devPtr, preconfPIN,
1484 oclog() <<"Mutex not found";
1485 result = OC_STACK_ERROR;
1490 OCStackResult OCSecureResource::provisionPreconfPin(const char * preconfPin,
1491 size_t preconfPinLength, ResultCallBack resultCallback)
1493 if (!resultCallback)
1495 oclog() <<"result callback can not be null";
1496 return OC_STACK_INVALID_CALLBACK;
1500 oclog() <<"pre config pin can not be null";
1501 return OC_STACK_INVALID_PARAM;
1503 if (preconfPinLength <= 0)
1505 oclog() <<"pre config pin length can not be zero or less";
1506 return OC_STACK_INVALID_PARAM;
1509 OCStackResult result;
1510 auto cLock = m_csdkLock.lock();
1514 ProvisionContext* context = new ProvisionContext(resultCallback);
1516 std::lock_guard<std::recursive_mutex> lock(*cLock);
1517 result = OCProvisionPreconfigPin(static_cast<void*>(context),
1518 devPtr, preconfPin, preconfPinLength,
1519 &OCSecureResource::callbackWrapper);
1523 oclog() <<"Mutex not found";
1524 result = OC_STACK_ERROR;
1529 OCStackResult OCSecureResource::removeSubOwner(const OicUuid_t* subOwnerId, ResultCallBack resultCallback)
1531 validateSecureResource();
1532 OCStackResult result;
1533 auto cLock = m_csdkLock.lock();
1536 ProvisionContext* context = new ProvisionContext(resultCallback);
1537 result = OCRemoveSubOwner(static_cast<void*>(context),
1538 devPtr, subOwnerId, &OCSecureResource::callbackWrapper);
1542 oclog() << "Mutex not found";
1543 result = OC_STACK_ERROR;
1548 OCStackResult OCSecureResource::removeAllSubOwner(ResultCallBack resultCallback)
1550 validateSecureResource();
1551 OCStackResult result;
1552 auto cLock = m_csdkLock.lock();
1555 ProvisionContext* context = new ProvisionContext(resultCallback);
1556 result =OCRemoveAllSubOwner(static_cast<void*>(context),
1557 devPtr, &OCSecureResource::callbackWrapper);
1561 oclog() << "Mutex not found";
1562 result = OC_STACK_ERROR;
1567 #endif // MULTIPLE_OWNER