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 OCStackResult result;
806 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
810 std::lock_guard<std::recursive_mutex> lock(*cLock);
811 result = SetDeviceIdSeed(seed, seedSize);
815 oclog() <<"Mutex not found";
816 result = OC_STACK_ERROR;
821 int OCSecure::peerCertCallbackWrapper(void *ctx, const mbedtls_x509_crt *cert,
824 OCStackResult ret = OC_STACK_ERROR;
826 PeerCertContext *context = static_cast<PeerCertContext*>(ctx);
829 oclog() << "Invalid Context";
833 if (context->callback)
835 ret = context->callback(cert, depth);
843 return (OC_STACK_OK == ret)? 0 : 1;
846 OCStackResult OCSecure::setPeerCertCallback(PeerCertCB peerCertCallback)
848 OCStackResult result;
850 if (NULL == peerCertCallback)
852 result = OCSetPeerCertCallback(NULL, NULL);
853 if (OC_STACK_OK != result)
855 oclog() << "OCSetPeerCertCallback() Failed";
860 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
864 PeerCertContext *context = new PeerCertContext(peerCertCallback);
866 std::lock_guard<std::recursive_mutex> lock(*cLock);
867 result = OCSetPeerCertCallback(static_cast<void*>(context),
868 &OCSecure::peerCertCallbackWrapper);
869 if (OC_STACK_OK != result)
871 oclog() << "OCSetPeerCertCallback() Failed";
876 oclog() << "Mutex not found";
877 result = OC_STACK_ERROR;
882 #endif // __WITH_DTLS__ || __WITH_TLS__
884 void OCSecureResource::callbackWrapper(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
886 PMResultList_t *results = nullptr;
887 ProvisionContext* context = static_cast<ProvisionContext*>(ctx);
891 results = new PMResultList_t;
893 catch (std::bad_alloc& e)
895 oclog() <<"Bad alloc exception";
899 for (int i = 0; i < nOfRes; i++)
901 results->push_back(arr[i]);
904 std::thread exec(context->callback, results, hasError);
910 OCSecureResource::OCSecureResource(): m_csdkLock(std::weak_ptr<std::recursive_mutex>()),
915 OCSecureResource::OCSecureResource(std::weak_ptr<std::recursive_mutex> csdkLock,
916 OCProvisionDev_t *dPtr)
917 :m_csdkLock(csdkLock), devPtr(dPtr)
921 OCSecureResource::~OCSecureResource()
925 OCDeleteDiscoveredDevices(devPtr);
929 OCStackResult OCSecureResource::doOwnershipTransfer(ResultCallBack resultCallback)
933 oclog() <<"Result callback can't be null";
934 return OC_STACK_INVALID_CALLBACK;
937 OCStackResult result;
938 auto cLock = m_csdkLock.lock();
942 ProvisionContext* context = new ProvisionContext(resultCallback);
944 std::lock_guard<std::recursive_mutex> lock(*cLock);
945 result = OCDoOwnershipTransfer(static_cast<void*>(context),
946 devPtr, &OCSecureResource::callbackWrapper);
950 oclog() <<"Mutex not found";
951 result = OC_STACK_ERROR;
956 #ifdef MULTIPLE_OWNER
957 OCStackResult OCSecureResource::doMultipleOwnershipTransfer(ResultCallBack resultCallback)
961 oclog() <<"Result callback can't be null";
962 return OC_STACK_INVALID_CALLBACK;
965 OCStackResult result;
966 auto cLock = m_csdkLock.lock();
970 ProvisionContext* context = new ProvisionContext(resultCallback);
972 std::lock_guard<std::recursive_mutex> lock(*cLock);
973 result = OCDoMultipleOwnershipTransfer(static_cast<void*>(context),
974 devPtr, &OCSecureResource::callbackWrapper);
978 oclog() <<"Mutex not found";
979 result = OC_STACK_ERROR;
984 OCStackResult OCSecureResource::getSubOwnerList(UuidList_t &uuidList)
986 validateSecureResource();
987 OicSecSubOwner_t* tmp = NULL;
988 for (tmp = devPtr->doxm->subOwners; tmp; tmp = tmp->next)
990 uuidList.push_back(tmp->uuid);
996 OCStackResult OCSecureResource::provisionACL( const OicSecAcl_t* acl,
997 ResultCallBack resultCallback)
1001 oclog() <<"ACL can't be null";
1002 return OC_STACK_INVALID_PARAM;
1004 if (!resultCallback)
1006 oclog() <<"result callback can not be null";
1007 return OC_STACK_INVALID_CALLBACK;
1010 OCStackResult result;
1011 auto cLock = m_csdkLock.lock();
1015 ProvisionContext* context = new ProvisionContext(resultCallback);
1017 std::lock_guard<std::recursive_mutex> lock(*cLock);
1018 result = OCProvisionACL(static_cast<void*>(context),
1019 devPtr, const_cast<OicSecAcl_t*>(acl),
1020 &OCSecureResource::callbackWrapper);
1024 oclog() <<"Mutex not found";
1025 result = OC_STACK_ERROR;
1030 OCStackResult OCSecureResource::provisionCredentials(const Credential &cred,
1031 const OCSecureResource &device2, ResultCallBack resultCallback)
1033 if (!resultCallback)
1035 oclog() << "Result calback can't be null";
1036 return OC_STACK_INVALID_CALLBACK;
1039 OCStackResult result;
1040 auto cLock = m_csdkLock.lock();
1044 ProvisionContext* context = new ProvisionContext(resultCallback);
1046 std::lock_guard<std::recursive_mutex> lock(*cLock);
1047 result = OCProvisionCredentials(static_cast<void*>(context),
1048 cred.getCredentialType(),
1049 cred.getCredentialKeySize(),
1050 devPtr, device2.getDevPtr(),
1051 &OCSecureResource::callbackWrapper);
1055 oclog() <<"Mutex not found";
1056 result = OC_STACK_ERROR;
1061 OCStackResult OCSecureResource::provisionPairwiseDevices(const Credential &cred,
1062 const OicSecAcl_t* acl1, const OCSecureResource &device2, const OicSecAcl_t* acl2,
1063 ResultCallBack resultCallback)
1065 if (!resultCallback)
1067 oclog() << "Result callback can not be null";
1068 return OC_STACK_INVALID_CALLBACK;
1071 OCStackResult result;
1072 auto cLock = m_csdkLock.lock();
1076 ProvisionContext* context = new ProvisionContext(resultCallback);
1078 std::lock_guard<std::recursive_mutex> lock(*cLock);
1079 result = OCProvisionPairwiseDevices(static_cast<void*>(context),
1080 cred.getCredentialType(),
1081 cred.getCredentialKeySize(),
1082 devPtr, const_cast<OicSecAcl_t*>(acl1),
1083 device2.getDevPtr(), const_cast<OicSecAcl_t*>(acl2),
1084 &OCSecureResource::callbackWrapper);
1088 oclog() <<"Mutex not found";
1089 result = OC_STACK_ERROR;
1094 OCStackResult OCSecureResource::unlinkDevices(const OCSecureResource &device2,
1095 ResultCallBack resultCallback)
1097 if (!resultCallback)
1099 oclog() << "Result calback can't be null";
1100 return OC_STACK_INVALID_CALLBACK;
1103 OCStackResult result;
1104 auto cLock = m_csdkLock.lock();
1108 ProvisionContext* context = new ProvisionContext(resultCallback);
1110 std::lock_guard<std::recursive_mutex> lock(*cLock);
1112 result = OCUnlinkDevices(static_cast<void*>(context),
1113 devPtr, device2.getDevPtr(), &OCSecureResource::callbackWrapper);
1117 oclog() <<"Mutex not found";
1118 result = OC_STACK_ERROR;
1123 OCStackResult OCSecureResource::removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery,
1124 ResultCallBack resultCallback)
1126 if (!resultCallback)
1128 oclog() << "Result calback can't be null";
1129 return OC_STACK_INVALID_CALLBACK;
1132 OCStackResult result;
1133 auto cLock = m_csdkLock.lock();
1137 ProvisionContext* context = new ProvisionContext(resultCallback);
1139 std::lock_guard<std::recursive_mutex> lock(*cLock);
1141 result = OCRemoveDevice(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
1142 devPtr, &OCSecureResource::callbackWrapper);
1146 oclog() <<"Mutex not found";
1147 result = OC_STACK_ERROR;
1152 OCStackResult OCSecureResource::getLinkedDevices(UuidList_t &uuidList)
1154 OCStackResult result;
1155 size_t numOfDevices = -1;
1156 auto devUuid = devPtr->doxm->deviceID;
1157 auto cLock = m_csdkLock.lock();
1161 std::lock_guard<std::recursive_mutex> lock(*cLock);
1163 OCUuidList_t* linkedDevs = nullptr, *tmp = nullptr;
1164 result = OCGetLinkedStatus(&devUuid, &linkedDevs, &numOfDevices);
1165 if (result == OC_STACK_OK)
1167 for (tmp = linkedDevs; tmp; tmp = tmp->next)
1169 uuidList.push_back(tmp->dev);
1171 OCDeleteUuidList(linkedDevs);
1176 oclog() <<"Mutex not found";
1177 result = OC_STACK_ERROR;
1182 OCStackResult OCSecureResource::provisionDirectPairing( const OicSecPconf_t* pconf,
1183 ResultCallBack resultCallback)
1187 oclog() <<"PCONF can't be null";
1188 return OC_STACK_INVALID_PARAM;
1190 if (!resultCallback)
1192 oclog() <<"result callback can not be null";
1193 return OC_STACK_INVALID_CALLBACK;
1196 OCStackResult result;
1197 auto cLock = m_csdkLock.lock();
1201 ProvisionContext* context = new ProvisionContext(resultCallback);
1203 std::lock_guard<std::recursive_mutex> lock(*cLock);
1204 result = OCProvisionDirectPairing(static_cast<void*>(context),
1205 devPtr, const_cast<OicSecPconf_t*>(pconf),
1206 &OCSecureResource::callbackWrapper);
1210 oclog() <<"Mutex not found";
1211 result = OC_STACK_ERROR;
1216 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
1217 OCStackResult OCSecureResource::provisionTrustCertChain(OicSecCredType_t type, uint16_t credId,
1218 ResultCallBack resultCallback)
1220 if (SIGNED_ASYMMETRIC_KEY != type)
1222 oclog() <<"Invalid key type";
1223 return OC_STACK_INVALID_PARAM;
1225 if (!resultCallback)
1227 oclog() <<"result callback can not be null";
1228 return OC_STACK_INVALID_CALLBACK;
1231 OCStackResult result;
1232 auto cLock = m_csdkLock.lock();
1236 ProvisionContext* context = new ProvisionContext(resultCallback);
1238 std::lock_guard<std::recursive_mutex> lock(*cLock);
1239 result = OCProvisionTrustCertChain(static_cast<void*>(context),
1240 type, credId, devPtr,
1241 &OCSecureResource::callbackWrapper);
1245 oclog() <<"Mutex not found";
1246 result = OC_STACK_ERROR;
1250 #endif // __WITH_DTLS__ or __WITH_TLS__
1252 std::string OCSecureResource::getDeviceID()
1254 std::ostringstream deviceId("");
1255 char *devID = nullptr;
1257 validateSecureResource();
1259 if (OC_STACK_OK == ConvertUuidToStr(&(devPtr->doxm->deviceID), &devID))
1266 oclog() <<"Can not convert uuid to struuid";
1268 return deviceId.str();
1271 OCProvisionDev_t* OCSecureResource::getDevPtr() const
1276 std::string OCSecureResource::getDevAddr()
1278 validateSecureResource();
1279 std::string ipAddr(devPtr->endpoint.addr);
1283 int OCSecureResource::getDeviceStatus()
1285 validateSecureResource();
1286 return (int)devPtr->devStatus;
1289 bool OCSecureResource::getOwnedStatus()
1291 validateSecureResource();
1292 return devPtr->doxm->owned;
1295 void OCSecureResource::validateSecureResource()
1299 throw OCException("Incomplete secure resource", OC_STACK_RESOURCE_ERROR);
1303 OCStackResult OCSecureResource::getOTMethod(OicSecOxm_t* oxm)
1307 oclog() << "Null param";
1308 return OC_STACK_INVALID_PARAM;
1311 OCStackResult result = OC_STACK_ERROR;
1312 auto cLock = m_csdkLock.lock();
1315 std::lock_guard<std::recursive_mutex> lock(*cLock);
1316 if(devPtr && devPtr->doxm)
1318 result = OCSelectOwnershipTransferMethod(devPtr->doxm->oxm, devPtr->doxm->oxmLen,
1324 oclog() <<"Mutex not found";
1330 #ifdef MULTIPLE_OWNER
1331 OCStackResult OCSecureResource::getMOTMethod( OicSecOxm_t* oxm)
1335 oclog() << "Null param";
1336 return OC_STACK_INVALID_PARAM;
1339 OCStackResult result = OC_STACK_ERROR;
1340 auto cLock = m_csdkLock.lock();
1343 std::lock_guard<std::recursive_mutex> lock(*cLock);
1344 if (devPtr && devPtr->doxm)
1346 result = OCSelectOwnershipTransferMethod(devPtr->doxm->oxm, devPtr->doxm->oxmLen,
1352 oclog() <<"Mutex not found";
1357 bool OCSecureResource::isMOTSupported()
1359 if (devPtr && devPtr->doxm)
1361 return (devPtr->doxm->mom ? true : false);
1366 bool OCSecureResource::isMOTEnabled()
1368 if (devPtr && devPtr->doxm && devPtr->doxm->mom)
1370 if (OIC_MULTIPLE_OWNER_DISABLE != devPtr->doxm->mom->mode)
1378 OCStackResult OCSecureResource::selectMOTMethod( const OicSecOxm_t oxmSelVal,
1379 ResultCallBack resultCallback)
1381 if (!resultCallback)
1383 oclog() <<"result callback can not be null";
1384 return OC_STACK_INVALID_CALLBACK;
1387 OCStackResult result;
1388 auto cLock = m_csdkLock.lock();
1392 ProvisionContext* context = new ProvisionContext(resultCallback);
1394 std::lock_guard<std::recursive_mutex> lock(*cLock);
1395 result = OCSelectMOTMethod(static_cast<void*>(context),
1397 &OCSecureResource::callbackWrapper);
1401 oclog() <<"Mutex not found";
1402 result = OC_STACK_ERROR;
1407 OCStackResult OCSecureResource::changeMOTMode( const OicSecMomType_t momType,
1408 ResultCallBack resultCallback)
1410 if (!resultCallback)
1412 oclog() <<"result callback can not be null";
1413 return OC_STACK_INVALID_CALLBACK;
1416 OCStackResult result;
1417 auto cLock = m_csdkLock.lock();
1421 ProvisionContext* context = new ProvisionContext(resultCallback);
1423 std::lock_guard<std::recursive_mutex> lock(*cLock);
1424 result = OCChangeMOTMode(static_cast<void*>(context),
1426 &OCSecureResource::callbackWrapper);
1430 oclog() <<"Mutex not found";
1431 result = OC_STACK_ERROR;
1437 OCStackResult OCSecureResource::addPreconfigPIN(const char* preconfPIN,
1438 size_t preconfPINLength)
1442 oclog() <<"pre config pin can not be null";
1443 return OC_STACK_INVALID_PARAM;
1445 if (preconfPINLength <= 0)
1447 oclog() <<"pre config pin length can not be zero or less";
1448 return OC_STACK_INVALID_PARAM;
1450 OCStackResult result;
1451 auto cLock = m_csdkLock.lock();
1455 std::lock_guard<std::recursive_mutex> lock(*cLock);
1456 result = OCAddPreconfigPin(devPtr, preconfPIN,
1461 oclog() <<"Mutex not found";
1462 result = OC_STACK_ERROR;
1467 OCStackResult OCSecureResource::provisionPreconfPin(const char * preconfPin,
1468 size_t preconfPinLength, ResultCallBack resultCallback)
1470 if (!resultCallback)
1472 oclog() <<"result callback can not be null";
1473 return OC_STACK_INVALID_CALLBACK;
1477 oclog() <<"pre config pin can not be null";
1478 return OC_STACK_INVALID_PARAM;
1480 if (preconfPinLength <= 0)
1482 oclog() <<"pre config pin length can not be zero or less";
1483 return OC_STACK_INVALID_PARAM;
1486 OCStackResult result;
1487 auto cLock = m_csdkLock.lock();
1491 ProvisionContext* context = new ProvisionContext(resultCallback);
1493 std::lock_guard<std::recursive_mutex> lock(*cLock);
1494 result = OCProvisionPreconfigPin(static_cast<void*>(context),
1495 devPtr, preconfPin, preconfPinLength,
1496 &OCSecureResource::callbackWrapper);
1500 oclog() <<"Mutex not found";
1501 result = OC_STACK_ERROR;
1506 OCStackResult OCSecureResource::removeSubOwner(const OicUuid_t* subOwnerId, ResultCallBack resultCallback)
1508 validateSecureResource();
1509 OCStackResult result;
1510 auto cLock = m_csdkLock.lock();
1513 ProvisionContext* context = new ProvisionContext(resultCallback);
1514 result = OCRemoveSubOwner(static_cast<void*>(context),
1515 devPtr, subOwnerId, &OCSecureResource::callbackWrapper);
1519 oclog() << "Mutex not found";
1520 result = OC_STACK_ERROR;
1525 OCStackResult OCSecureResource::removeAllSubOwner(ResultCallBack resultCallback)
1527 validateSecureResource();
1528 OCStackResult result;
1529 auto cLock = m_csdkLock.lock();
1532 ProvisionContext* context = new ProvisionContext(resultCallback);
1533 result =OCRemoveAllSubOwner(static_cast<void*>(context),
1534 devPtr, &OCSecureResource::callbackWrapper);
1538 oclog() << "Mutex not found";
1539 result = OC_STACK_ERROR;
1544 #endif // MULTIPLE_OWNER