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);
830 return (OC_STACK_OK == ret)? 0 : 1;
833 OCStackResult OCSecure::setPeerCertCallback(PeerCertCB peerCertCallback)
835 OCStackResult result;
837 if (NULL == peerCertCallback)
839 result = OCSetPeerCertCallback(NULL, NULL);
840 if (OC_STACK_OK != result)
842 oclog() << "OCSetPeerCertCallback() Failed";
847 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
851 PeerCertContext *context = new PeerCertContext(peerCertCallback);
853 std::lock_guard<std::recursive_mutex> lock(*cLock);
854 result = OCSetPeerCertCallback(static_cast<void*>(context),
855 &OCSecure::peerCertCallbackWrapper);
856 if (OC_STACK_OK != result)
858 oclog() << "OCSetPeerCertCallback() Failed";
863 oclog() << "Mutex not found";
864 result = OC_STACK_ERROR;
869 #endif // __WITH_DTLS__ || __WITH_TLS__
871 void OCSecureResource::callbackWrapper(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
873 PMResultList_t *results = nullptr;
874 ProvisionContext* context = static_cast<ProvisionContext*>(ctx);
878 results = new PMResultList_t;
880 catch (std::bad_alloc& e)
882 oclog() <<"Bad alloc exception";
886 for (int i = 0; i < nOfRes; i++)
888 results->push_back(arr[i]);
891 std::thread exec(context->callback, results, hasError);
897 OCSecureResource::OCSecureResource(): m_csdkLock(std::weak_ptr<std::recursive_mutex>()),
902 OCSecureResource::OCSecureResource(std::weak_ptr<std::recursive_mutex> csdkLock,
903 OCProvisionDev_t *dPtr)
904 :m_csdkLock(csdkLock), devPtr(dPtr)
908 OCSecureResource::~OCSecureResource()
912 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);
931 std::lock_guard<std::recursive_mutex> lock(*cLock);
932 result = OCDoOwnershipTransfer(static_cast<void*>(context),
933 devPtr, &OCSecureResource::callbackWrapper);
937 oclog() <<"Mutex not found";
938 result = OC_STACK_ERROR;
943 #ifdef MULTIPLE_OWNER
944 OCStackResult OCSecureResource::doMultipleOwnershipTransfer(ResultCallBack resultCallback)
948 oclog() <<"Result callback can't be null";
949 return OC_STACK_INVALID_CALLBACK;
952 OCStackResult result;
953 auto cLock = m_csdkLock.lock();
957 ProvisionContext* context = new ProvisionContext(resultCallback);
959 std::lock_guard<std::recursive_mutex> lock(*cLock);
960 result = OCDoMultipleOwnershipTransfer(static_cast<void*>(context),
961 devPtr, &OCSecureResource::callbackWrapper);
965 oclog() <<"Mutex not found";
966 result = OC_STACK_ERROR;
971 OCStackResult OCSecureResource::getSubOwnerList(UuidList_t &uuidList)
973 validateSecureResource();
974 OicSecSubOwner_t* tmp = NULL;
975 for (tmp = devPtr->doxm->subOwners; tmp; tmp = tmp->next)
977 uuidList.push_back(tmp->uuid);
983 OCStackResult OCSecureResource::provisionACL( const OicSecAcl_t* acl,
984 ResultCallBack resultCallback)
988 oclog() <<"ACL can't be null";
989 return OC_STACK_INVALID_PARAM;
993 oclog() <<"result callback can not be null";
994 return OC_STACK_INVALID_CALLBACK;
997 OCStackResult result;
998 auto cLock = m_csdkLock.lock();
1002 ProvisionContext* context = new ProvisionContext(resultCallback);
1004 std::lock_guard<std::recursive_mutex> lock(*cLock);
1005 result = OCProvisionACL(static_cast<void*>(context),
1006 devPtr, const_cast<OicSecAcl_t*>(acl),
1007 &OCSecureResource::callbackWrapper);
1011 oclog() <<"Mutex not found";
1012 result = OC_STACK_ERROR;
1017 OCStackResult OCSecureResource::provisionCredentials(const Credential &cred,
1018 const OCSecureResource &device2, ResultCallBack resultCallback)
1020 if (!resultCallback)
1022 oclog() << "Result calback can't be null";
1023 return OC_STACK_INVALID_CALLBACK;
1026 OCStackResult result;
1027 auto cLock = m_csdkLock.lock();
1031 ProvisionContext* context = new ProvisionContext(resultCallback);
1033 std::lock_guard<std::recursive_mutex> lock(*cLock);
1034 result = OCProvisionCredentials(static_cast<void*>(context),
1035 cred.getCredentialType(),
1036 cred.getCredentialKeySize(),
1037 devPtr, device2.getDevPtr(),
1038 &OCSecureResource::callbackWrapper);
1042 oclog() <<"Mutex not found";
1043 result = OC_STACK_ERROR;
1048 OCStackResult OCSecureResource::provisionPairwiseDevices(const Credential &cred,
1049 const OicSecAcl_t* acl1, const OCSecureResource &device2, const OicSecAcl_t* acl2,
1050 ResultCallBack resultCallback)
1052 if (!resultCallback)
1054 oclog() << "Result callback can not be null";
1055 return OC_STACK_INVALID_CALLBACK;
1058 OCStackResult result;
1059 auto cLock = m_csdkLock.lock();
1063 ProvisionContext* context = new ProvisionContext(resultCallback);
1065 std::lock_guard<std::recursive_mutex> lock(*cLock);
1066 result = OCProvisionPairwiseDevices(static_cast<void*>(context),
1067 cred.getCredentialType(),
1068 cred.getCredentialKeySize(),
1069 devPtr, const_cast<OicSecAcl_t*>(acl1),
1070 device2.getDevPtr(), const_cast<OicSecAcl_t*>(acl2),
1071 &OCSecureResource::callbackWrapper);
1075 oclog() <<"Mutex not found";
1076 result = OC_STACK_ERROR;
1081 OCStackResult OCSecureResource::unlinkDevices(const OCSecureResource &device2,
1082 ResultCallBack resultCallback)
1084 if (!resultCallback)
1086 oclog() << "Result calback can't be null";
1087 return OC_STACK_INVALID_CALLBACK;
1090 OCStackResult result;
1091 auto cLock = m_csdkLock.lock();
1095 ProvisionContext* context = new ProvisionContext(resultCallback);
1097 std::lock_guard<std::recursive_mutex> lock(*cLock);
1099 result = OCUnlinkDevices(static_cast<void*>(context),
1100 devPtr, device2.getDevPtr(), &OCSecureResource::callbackWrapper);
1104 oclog() <<"Mutex not found";
1105 result = OC_STACK_ERROR;
1110 OCStackResult OCSecureResource::removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery,
1111 ResultCallBack resultCallback)
1113 if (!resultCallback)
1115 oclog() << "Result calback can't be null";
1116 return OC_STACK_INVALID_CALLBACK;
1119 OCStackResult result;
1120 auto cLock = m_csdkLock.lock();
1124 ProvisionContext* context = new ProvisionContext(resultCallback);
1126 std::lock_guard<std::recursive_mutex> lock(*cLock);
1128 result = OCRemoveDevice(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
1129 devPtr, &OCSecureResource::callbackWrapper);
1133 oclog() <<"Mutex not found";
1134 result = OC_STACK_ERROR;
1139 OCStackResult OCSecureResource::getLinkedDevices(UuidList_t &uuidList)
1141 OCStackResult result;
1142 size_t numOfDevices = -1;
1143 auto devUuid = devPtr->doxm->deviceID;
1144 auto cLock = m_csdkLock.lock();
1148 std::lock_guard<std::recursive_mutex> lock(*cLock);
1150 OCUuidList_t* linkedDevs = nullptr, *tmp = nullptr;
1151 result = OCGetLinkedStatus(&devUuid, &linkedDevs, &numOfDevices);
1152 if (result == OC_STACK_OK)
1154 for (tmp = linkedDevs; tmp; tmp = tmp->next)
1156 uuidList.push_back(tmp->dev);
1158 OCDeleteUuidList(linkedDevs);
1163 oclog() <<"Mutex not found";
1164 result = OC_STACK_ERROR;
1169 OCStackResult OCSecureResource::provisionDirectPairing( const OicSecPconf_t* pconf,
1170 ResultCallBack resultCallback)
1174 oclog() <<"PCONF can't be null";
1175 return OC_STACK_INVALID_PARAM;
1177 if (!resultCallback)
1179 oclog() <<"result callback can not be null";
1180 return OC_STACK_INVALID_CALLBACK;
1183 OCStackResult result;
1184 auto cLock = m_csdkLock.lock();
1188 ProvisionContext* context = new ProvisionContext(resultCallback);
1190 std::lock_guard<std::recursive_mutex> lock(*cLock);
1191 result = OCProvisionDirectPairing(static_cast<void*>(context),
1192 devPtr, const_cast<OicSecPconf_t*>(pconf),
1193 &OCSecureResource::callbackWrapper);
1197 oclog() <<"Mutex not found";
1198 result = OC_STACK_ERROR;
1203 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
1204 OCStackResult OCSecureResource::provisionTrustCertChain(OicSecCredType_t type, uint16_t credId,
1205 ResultCallBack resultCallback)
1207 if (SIGNED_ASYMMETRIC_KEY != type)
1209 oclog() <<"Invalid key type";
1210 return OC_STACK_INVALID_PARAM;
1212 if (!resultCallback)
1214 oclog() <<"result callback can not be null";
1215 return OC_STACK_INVALID_CALLBACK;
1218 OCStackResult result;
1219 auto cLock = m_csdkLock.lock();
1223 ProvisionContext* context = new ProvisionContext(resultCallback);
1225 std::lock_guard<std::recursive_mutex> lock(*cLock);
1226 result = OCProvisionTrustCertChain(static_cast<void*>(context),
1227 type, credId, devPtr,
1228 &OCSecureResource::callbackWrapper);
1232 oclog() <<"Mutex not found";
1233 result = OC_STACK_ERROR;
1237 #endif // __WITH_DTLS__ or __WITH_TLS__
1239 std::string OCSecureResource::getDeviceID()
1241 std::ostringstream deviceId("");
1242 char *devID = nullptr;
1244 validateSecureResource();
1246 if (OC_STACK_OK == ConvertUuidToStr(&(devPtr->doxm->deviceID), &devID))
1253 oclog() <<"Can not convert uuid to struuid";
1255 return deviceId.str();
1258 OCProvisionDev_t* OCSecureResource::getDevPtr() const
1263 std::string OCSecureResource::getDevAddr()
1265 validateSecureResource();
1266 std::string ipAddr(devPtr->endpoint.addr);
1270 int OCSecureResource::getDeviceStatus()
1272 validateSecureResource();
1273 return (int)devPtr->devStatus;
1276 bool OCSecureResource::getOwnedStatus()
1278 validateSecureResource();
1279 return devPtr->doxm->owned;
1282 void OCSecureResource::validateSecureResource()
1286 throw OCException("Incomplete secure resource", OC_STACK_RESOURCE_ERROR);
1290 OCStackResult OCSecureResource::getOTMethod(OicSecOxm_t* oxm)
1294 oclog() << "Null param";
1295 return OC_STACK_INVALID_PARAM;
1298 OCStackResult result = OC_STACK_ERROR;
1299 auto cLock = m_csdkLock.lock();
1302 std::lock_guard<std::recursive_mutex> lock(*cLock);
1303 if(devPtr && devPtr->doxm)
1305 result = OCSelectOwnershipTransferMethod(devPtr->doxm->oxm, devPtr->doxm->oxmLen,
1311 oclog() <<"Mutex not found";
1317 #ifdef MULTIPLE_OWNER
1318 OCStackResult OCSecureResource::getMOTMethod( OicSecOxm_t* oxm)
1322 oclog() << "Null param";
1323 return OC_STACK_INVALID_PARAM;
1326 OCStackResult result = OC_STACK_ERROR;
1327 auto cLock = m_csdkLock.lock();
1330 std::lock_guard<std::recursive_mutex> lock(*cLock);
1331 if (devPtr && devPtr->doxm)
1333 result = OCSelectOwnershipTransferMethod(devPtr->doxm->oxm, devPtr->doxm->oxmLen,
1339 oclog() <<"Mutex not found";
1344 bool OCSecureResource::isMOTSupported()
1346 if (devPtr && devPtr->doxm)
1348 return (devPtr->doxm->mom ? true : false);
1353 bool OCSecureResource::isMOTEnabled()
1355 if (devPtr && devPtr->doxm && devPtr->doxm->mom)
1357 if (OIC_MULTIPLE_OWNER_DISABLE != devPtr->doxm->mom->mode)
1365 OCStackResult OCSecureResource::selectMOTMethod( const OicSecOxm_t oxmSelVal,
1366 ResultCallBack resultCallback)
1368 if (!resultCallback)
1370 oclog() <<"result callback can not be null";
1371 return OC_STACK_INVALID_CALLBACK;
1374 OCStackResult result;
1375 auto cLock = m_csdkLock.lock();
1379 ProvisionContext* context = new ProvisionContext(resultCallback);
1381 std::lock_guard<std::recursive_mutex> lock(*cLock);
1382 result = OCSelectMOTMethod(static_cast<void*>(context),
1384 &OCSecureResource::callbackWrapper);
1388 oclog() <<"Mutex not found";
1389 result = OC_STACK_ERROR;
1394 OCStackResult OCSecureResource::changeMOTMode( const OicSecMomType_t momType,
1395 ResultCallBack resultCallback)
1397 if (!resultCallback)
1399 oclog() <<"result callback can not be null";
1400 return OC_STACK_INVALID_CALLBACK;
1403 OCStackResult result;
1404 auto cLock = m_csdkLock.lock();
1408 ProvisionContext* context = new ProvisionContext(resultCallback);
1410 std::lock_guard<std::recursive_mutex> lock(*cLock);
1411 result = OCChangeMOTMode(static_cast<void*>(context),
1413 &OCSecureResource::callbackWrapper);
1417 oclog() <<"Mutex not found";
1418 result = OC_STACK_ERROR;
1424 OCStackResult OCSecureResource::addPreconfigPIN(const char* preconfPIN,
1425 size_t preconfPINLength)
1429 oclog() <<"pre config pin can not be null";
1430 return OC_STACK_INVALID_PARAM;
1432 if (preconfPINLength <= 0)
1434 oclog() <<"pre config pin length can not be zero or less";
1435 return OC_STACK_INVALID_PARAM;
1437 OCStackResult result;
1438 auto cLock = m_csdkLock.lock();
1442 std::lock_guard<std::recursive_mutex> lock(*cLock);
1443 result = OCAddPreconfigPin(devPtr, preconfPIN,
1448 oclog() <<"Mutex not found";
1449 result = OC_STACK_ERROR;
1454 OCStackResult OCSecureResource::provisionPreconfPin(const char * preconfPin,
1455 size_t preconfPinLength, ResultCallBack resultCallback)
1457 if (!resultCallback)
1459 oclog() <<"result callback can not be null";
1460 return OC_STACK_INVALID_CALLBACK;
1464 oclog() <<"pre config pin can not be null";
1465 return OC_STACK_INVALID_PARAM;
1467 if (preconfPinLength <= 0)
1469 oclog() <<"pre config pin length can not be zero or less";
1470 return OC_STACK_INVALID_PARAM;
1473 OCStackResult result;
1474 auto cLock = m_csdkLock.lock();
1478 ProvisionContext* context = new ProvisionContext(resultCallback);
1480 std::lock_guard<std::recursive_mutex> lock(*cLock);
1481 result = OCProvisionPreconfigPin(static_cast<void*>(context),
1482 devPtr, preconfPin, preconfPinLength,
1483 &OCSecureResource::callbackWrapper);
1487 oclog() <<"Mutex not found";
1488 result = OC_STACK_ERROR;
1493 OCStackResult OCSecureResource::removeSubOwner(const OicUuid_t* subOwnerId, ResultCallBack resultCallback)
1495 validateSecureResource();
1496 OCStackResult result;
1497 auto cLock = m_csdkLock.lock();
1500 ProvisionContext* context = new ProvisionContext(resultCallback);
1501 result = OCRemoveSubOwner(static_cast<void*>(context),
1502 devPtr, subOwnerId, &OCSecureResource::callbackWrapper);
1506 oclog() << "Mutex not found";
1507 result = OC_STACK_ERROR;
1512 OCStackResult OCSecureResource::removeAllSubOwner(ResultCallBack resultCallback)
1514 validateSecureResource();
1515 OCStackResult result;
1516 auto cLock = m_csdkLock.lock();
1519 ProvisionContext* context = new ProvisionContext(resultCallback);
1520 result =OCRemoveAllSubOwner(static_cast<void*>(context),
1521 devPtr, &OCSecureResource::callbackWrapper);
1525 oclog() << "Mutex not found";
1526 result = OC_STACK_ERROR;
1531 #endif // MULTIPLE_OWNER