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>()),
897 OCSecureResource::OCSecureResource(std::weak_ptr<std::recursive_mutex> csdkLock,
898 OCProvisionDev_t *dPtr)
899 :m_csdkLock(csdkLock), devPtr(dPtr)
903 OCSecureResource::~OCSecureResource()
907 OCDeleteDiscoveredDevices(devPtr);
911 OCStackResult OCSecureResource::doOwnershipTransfer(ResultCallBack resultCallback)
915 oclog() <<"Result callback can't be null";
916 return OC_STACK_INVALID_CALLBACK;
919 OCStackResult result;
920 auto cLock = m_csdkLock.lock();
924 ProvisionContext* context = new ProvisionContext(resultCallback);
926 std::lock_guard<std::recursive_mutex> lock(*cLock);
927 result = OCDoOwnershipTransfer(static_cast<void*>(context),
928 devPtr, &OCSecureResource::callbackWrapper);
932 oclog() <<"Mutex not found";
933 result = OC_STACK_ERROR;
938 #ifdef MULTIPLE_OWNER
939 OCStackResult OCSecureResource::doMultipleOwnershipTransfer(ResultCallBack resultCallback)
943 oclog() <<"Result callback can't be null";
944 return OC_STACK_INVALID_CALLBACK;
947 OCStackResult result;
948 auto cLock = m_csdkLock.lock();
952 ProvisionContext* context = new ProvisionContext(resultCallback);
954 std::lock_guard<std::recursive_mutex> lock(*cLock);
955 result = OCDoMultipleOwnershipTransfer(static_cast<void*>(context),
956 devPtr, &OCSecureResource::callbackWrapper);
960 oclog() <<"Mutex not found";
961 result = OC_STACK_ERROR;
966 OCStackResult OCSecureResource::getSubOwnerList(UuidList_t &uuidList)
968 validateSecureResource();
969 OicSecSubOwner_t* tmp = NULL;
970 for (tmp = devPtr->doxm->subOwners; tmp; tmp = tmp->next)
972 uuidList.push_back(tmp->uuid);
978 OCStackResult OCSecureResource::provisionACL( const OicSecAcl_t* acl,
979 ResultCallBack resultCallback)
983 oclog() <<"ACL can't be null";
984 return OC_STACK_INVALID_PARAM;
988 oclog() <<"result callback can not be null";
989 return OC_STACK_INVALID_CALLBACK;
992 OCStackResult result;
993 auto cLock = m_csdkLock.lock();
997 ProvisionContext* context = new ProvisionContext(resultCallback);
999 std::lock_guard<std::recursive_mutex> lock(*cLock);
1000 result = OCProvisionACL(static_cast<void*>(context),
1001 devPtr, const_cast<OicSecAcl_t*>(acl),
1002 &OCSecureResource::callbackWrapper);
1006 oclog() <<"Mutex not found";
1007 result = OC_STACK_ERROR;
1012 OCStackResult OCSecureResource::provisionCredentials(const Credential &cred,
1013 const OCSecureResource &device2, ResultCallBack resultCallback)
1015 if (!resultCallback)
1017 oclog() << "Result calback can't be null";
1018 return OC_STACK_INVALID_CALLBACK;
1021 OCStackResult result;
1022 auto cLock = m_csdkLock.lock();
1026 ProvisionContext* context = new ProvisionContext(resultCallback);
1028 std::lock_guard<std::recursive_mutex> lock(*cLock);
1029 result = OCProvisionCredentials(static_cast<void*>(context),
1030 cred.getCredentialType(),
1031 cred.getCredentialKeySize(),
1032 devPtr, device2.getDevPtr(),
1033 &OCSecureResource::callbackWrapper);
1037 oclog() <<"Mutex not found";
1038 result = OC_STACK_ERROR;
1043 OCStackResult OCSecureResource::provisionPairwiseDevices(const Credential &cred,
1044 const OicSecAcl_t* acl1, const OCSecureResource &device2, const OicSecAcl_t* acl2,
1045 ResultCallBack resultCallback)
1047 if (!resultCallback)
1049 oclog() << "Result callback can not be null";
1050 return OC_STACK_INVALID_CALLBACK;
1053 OCStackResult result;
1054 auto cLock = m_csdkLock.lock();
1058 ProvisionContext* context = new ProvisionContext(resultCallback);
1060 std::lock_guard<std::recursive_mutex> lock(*cLock);
1061 result = OCProvisionPairwiseDevices(static_cast<void*>(context),
1062 cred.getCredentialType(),
1063 cred.getCredentialKeySize(),
1064 devPtr, const_cast<OicSecAcl_t*>(acl1),
1065 device2.getDevPtr(), const_cast<OicSecAcl_t*>(acl2),
1066 &OCSecureResource::callbackWrapper);
1070 oclog() <<"Mutex not found";
1071 result = OC_STACK_ERROR;
1076 OCStackResult OCSecureResource::unlinkDevices(const OCSecureResource &device2,
1077 ResultCallBack resultCallback)
1079 if (!resultCallback)
1081 oclog() << "Result calback can't be null";
1082 return OC_STACK_INVALID_CALLBACK;
1085 OCStackResult result;
1086 auto cLock = m_csdkLock.lock();
1090 ProvisionContext* context = new ProvisionContext(resultCallback);
1092 std::lock_guard<std::recursive_mutex> lock(*cLock);
1094 result = OCUnlinkDevices(static_cast<void*>(context),
1095 devPtr, device2.getDevPtr(), &OCSecureResource::callbackWrapper);
1099 oclog() <<"Mutex not found";
1100 result = OC_STACK_ERROR;
1105 OCStackResult OCSecureResource::removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery,
1106 ResultCallBack resultCallback)
1108 if (!resultCallback)
1110 oclog() << "Result calback can't be null";
1111 return OC_STACK_INVALID_CALLBACK;
1114 OCStackResult result;
1115 auto cLock = m_csdkLock.lock();
1119 ProvisionContext* context = new ProvisionContext(resultCallback);
1121 std::lock_guard<std::recursive_mutex> lock(*cLock);
1123 result = OCRemoveDevice(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
1124 devPtr, &OCSecureResource::callbackWrapper);
1128 oclog() <<"Mutex not found";
1129 result = OC_STACK_ERROR;
1134 OCStackResult OCSecureResource::getLinkedDevices(UuidList_t &uuidList)
1136 OCStackResult result;
1137 size_t numOfDevices = -1;
1138 auto devUuid = devPtr->doxm->deviceID;
1139 auto cLock = m_csdkLock.lock();
1143 std::lock_guard<std::recursive_mutex> lock(*cLock);
1145 OCUuidList_t* linkedDevs = nullptr, *tmp = nullptr;
1146 result = OCGetLinkedStatus(&devUuid, &linkedDevs, &numOfDevices);
1147 if (result == OC_STACK_OK)
1149 for (tmp = linkedDevs; tmp; tmp = tmp->next)
1151 uuidList.push_back(tmp->dev);
1153 OCDeleteUuidList(linkedDevs);
1158 oclog() <<"Mutex not found";
1159 result = OC_STACK_ERROR;
1164 OCStackResult OCSecureResource::provisionDirectPairing( const OicSecPconf_t* pconf,
1165 ResultCallBack resultCallback)
1169 oclog() <<"PCONF can't be null";
1170 return OC_STACK_INVALID_PARAM;
1172 if (!resultCallback)
1174 oclog() <<"result callback can not be null";
1175 return OC_STACK_INVALID_CALLBACK;
1178 OCStackResult result;
1179 auto cLock = m_csdkLock.lock();
1183 ProvisionContext* context = new ProvisionContext(resultCallback);
1185 std::lock_guard<std::recursive_mutex> lock(*cLock);
1186 result = OCProvisionDirectPairing(static_cast<void*>(context),
1187 devPtr, const_cast<OicSecPconf_t*>(pconf),
1188 &OCSecureResource::callbackWrapper);
1192 oclog() <<"Mutex not found";
1193 result = OC_STACK_ERROR;
1198 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
1199 OCStackResult OCSecureResource::provisionTrustCertChain(OicSecCredType_t type, uint16_t credId,
1200 ResultCallBack resultCallback)
1202 if (SIGNED_ASYMMETRIC_KEY != type)
1204 oclog() <<"Invalid key type";
1205 return OC_STACK_INVALID_PARAM;
1207 if (!resultCallback)
1209 oclog() <<"result callback can not be null";
1210 return OC_STACK_INVALID_CALLBACK;
1213 OCStackResult result;
1214 auto cLock = m_csdkLock.lock();
1218 ProvisionContext* context = new ProvisionContext(resultCallback);
1220 std::lock_guard<std::recursive_mutex> lock(*cLock);
1221 result = OCProvisionTrustCertChain(static_cast<void*>(context),
1222 type, credId, devPtr,
1223 &OCSecureResource::callbackWrapper);
1227 oclog() <<"Mutex not found";
1228 result = OC_STACK_ERROR;
1232 #endif // __WITH_DTLS__ or __WITH_TLS__
1234 std::string OCSecureResource::getDeviceID()
1236 std::ostringstream deviceId("");
1237 char *devID = nullptr;
1239 validateSecureResource();
1241 if (OC_STACK_OK == ConvertUuidToStr(&(devPtr->doxm->deviceID), &devID))
1248 oclog() <<"Can not convert uuid to struuid";
1250 return deviceId.str();
1253 OCProvisionDev_t* OCSecureResource::getDevPtr() const
1258 std::string OCSecureResource::getDevAddr()
1260 validateSecureResource();
1261 std::string ipAddr(devPtr->endpoint.addr);
1265 int OCSecureResource::getDeviceStatus()
1267 validateSecureResource();
1268 return (int)devPtr->devStatus;
1271 bool OCSecureResource::getOwnedStatus()
1273 validateSecureResource();
1274 return devPtr->doxm->owned;
1277 void OCSecureResource::validateSecureResource()
1281 throw OCException("Incomplete secure resource", OC_STACK_RESOURCE_ERROR);
1285 OCStackResult OCSecureResource::getOTMethod(OicSecOxm_t* oxm)
1289 oclog() << "Null param";
1290 return OC_STACK_INVALID_PARAM;
1293 OCStackResult result = OC_STACK_ERROR;
1294 auto cLock = m_csdkLock.lock();
1297 std::lock_guard<std::recursive_mutex> lock(*cLock);
1298 if(devPtr && devPtr->doxm)
1300 result = OCSelectOwnershipTransferMethod(devPtr->doxm->oxm, devPtr->doxm->oxmLen,
1306 oclog() <<"Mutex not found";
1312 #ifdef MULTIPLE_OWNER
1313 OCStackResult OCSecureResource::getMOTMethod( OicSecOxm_t* oxm)
1317 oclog() << "Null param";
1318 return OC_STACK_INVALID_PARAM;
1321 OCStackResult result = OC_STACK_ERROR;
1322 auto cLock = m_csdkLock.lock();
1325 std::lock_guard<std::recursive_mutex> lock(*cLock);
1326 if (devPtr && devPtr->doxm)
1328 result = OCSelectOwnershipTransferMethod(devPtr->doxm->oxm, devPtr->doxm->oxmLen,
1334 oclog() <<"Mutex not found";
1339 bool OCSecureResource::isMOTSupported()
1341 if (devPtr && devPtr->doxm)
1343 return (devPtr->doxm->mom ? true : false);
1348 bool OCSecureResource::isMOTEnabled()
1350 if (devPtr && devPtr->doxm && devPtr->doxm->mom)
1352 if (OIC_MULTIPLE_OWNER_DISABLE != devPtr->doxm->mom->mode)
1360 OCStackResult OCSecureResource::selectMOTMethod( const OicSecOxm_t oxmSelVal,
1361 ResultCallBack resultCallback)
1363 if (!resultCallback)
1365 oclog() <<"result callback can not be null";
1366 return OC_STACK_INVALID_CALLBACK;
1369 OCStackResult result;
1370 auto cLock = m_csdkLock.lock();
1374 ProvisionContext* context = new ProvisionContext(resultCallback);
1376 std::lock_guard<std::recursive_mutex> lock(*cLock);
1377 result = OCSelectMOTMethod(static_cast<void*>(context),
1379 &OCSecureResource::callbackWrapper);
1383 oclog() <<"Mutex not found";
1384 result = OC_STACK_ERROR;
1389 OCStackResult OCSecureResource::changeMOTMode( const OicSecMomType_t momType,
1390 ResultCallBack resultCallback)
1392 if (!resultCallback)
1394 oclog() <<"result callback can not be null";
1395 return OC_STACK_INVALID_CALLBACK;
1398 OCStackResult result;
1399 auto cLock = m_csdkLock.lock();
1403 ProvisionContext* context = new ProvisionContext(resultCallback);
1405 std::lock_guard<std::recursive_mutex> lock(*cLock);
1406 result = OCChangeMOTMode(static_cast<void*>(context),
1408 &OCSecureResource::callbackWrapper);
1412 oclog() <<"Mutex not found";
1413 result = OC_STACK_ERROR;
1419 OCStackResult OCSecureResource::addPreconfigPIN(const char* preconfPIN,
1420 size_t preconfPINLength)
1424 oclog() <<"pre config pin can not be null";
1425 return OC_STACK_INVALID_PARAM;
1427 if (preconfPINLength <= 0)
1429 oclog() <<"pre config pin length can not be zero or less";
1430 return OC_STACK_INVALID_PARAM;
1432 OCStackResult result;
1433 auto cLock = m_csdkLock.lock();
1437 std::lock_guard<std::recursive_mutex> lock(*cLock);
1438 result = OCAddPreconfigPin(devPtr, preconfPIN,
1443 oclog() <<"Mutex not found";
1444 result = OC_STACK_ERROR;
1449 OCStackResult OCSecureResource::provisionPreconfPin(const char * preconfPin,
1450 size_t preconfPinLength, ResultCallBack resultCallback)
1452 if (!resultCallback)
1454 oclog() <<"result callback can not be null";
1455 return OC_STACK_INVALID_CALLBACK;
1459 oclog() <<"pre config pin can not be null";
1460 return OC_STACK_INVALID_PARAM;
1462 if (preconfPinLength <= 0)
1464 oclog() <<"pre config pin length can not be zero or less";
1465 return OC_STACK_INVALID_PARAM;
1468 OCStackResult result;
1469 auto cLock = m_csdkLock.lock();
1473 ProvisionContext* context = new ProvisionContext(resultCallback);
1475 std::lock_guard<std::recursive_mutex> lock(*cLock);
1476 result = OCProvisionPreconfigPin(static_cast<void*>(context),
1477 devPtr, preconfPin, preconfPinLength,
1478 &OCSecureResource::callbackWrapper);
1482 oclog() <<"Mutex not found";
1483 result = OC_STACK_ERROR;
1488 OCStackResult OCSecureResource::removeSubOwner(const OicUuid_t* subOwnerId, ResultCallBack resultCallback)
1490 validateSecureResource();
1491 OCStackResult result;
1492 auto cLock = m_csdkLock.lock();
1495 ProvisionContext* context = new ProvisionContext(resultCallback);
1496 result = OCRemoveSubOwner(static_cast<void*>(context),
1497 devPtr, subOwnerId, &OCSecureResource::callbackWrapper);
1501 oclog() << "Mutex not found";
1502 result = OC_STACK_ERROR;
1507 OCStackResult OCSecureResource::removeAllSubOwner(ResultCallBack resultCallback)
1509 validateSecureResource();
1510 OCStackResult result;
1511 auto cLock = m_csdkLock.lock();
1514 ProvisionContext* context = new ProvisionContext(resultCallback);
1515 result =OCRemoveAllSubOwner(static_cast<void*>(context),
1516 devPtr, &OCSecureResource::callbackWrapper);
1520 oclog() << "Mutex not found";
1521 result = OC_STACK_ERROR;
1526 #endif // MULTIPLE_OWNER