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);
463 oclog() <<"Can not convert struuid to uuid";
468 oclog() <<"Mutex not found";
469 result = OC_STACK_ERROR;
474 OCStackResult OCSecure::saveACL(const OicSecAcl_t* acl)
478 oclog() <<"ACL can't be null";
479 return OC_STACK_INVALID_PARAM;
482 OCStackResult result;
483 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
487 std::lock_guard<std::recursive_mutex> lock(*cLock);
488 result = OCSaveACL(const_cast<OicSecAcl_t*>(acl));
492 oclog() <<"Mutex not found";
493 result = OC_STACK_ERROR;
498 OCStackResult OCSecure::displayNumCallbackWrapper(void* ctx,
499 uint8_t verifNum[MUTUAL_VERIF_NUM_LEN])
501 uint8_t *number = NULL;
503 DisplayNumContext* context = static_cast<DisplayNumContext*>(ctx);
506 oclog() << "Invalid context";
507 return OC_STACK_INVALID_PARAM;
510 if (NULL != verifNum) {
511 number = new uint8_t[MUTUAL_VERIF_NUM_LEN];
512 memcpy(number, verifNum, MUTUAL_VERIF_NUM_LEN);
515 return context->callback(number);
518 OCStackResult OCSecure::registerDisplayNumCallback(DisplayNumCB displayNumCB)
522 oclog() << "Failed to register callback for display.";
523 return OC_STACK_INVALID_CALLBACK;
526 OCStackResult result = OCSecure::deregisterDisplayNumCallback();
527 if (OC_STACK_OK != result)
529 oclog() << "Failed to de-register callback for display."<<std::endl;
533 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
536 DisplayNumContext* context = new DisplayNumContext(displayNumCB);
537 std::lock_guard<std::recursive_mutex> lock(*cLock);
538 SetDisplayNumCB(static_cast<void*>(context), &OCSecure::displayNumCallbackWrapper);
539 result = OC_STACK_OK;
543 oclog() <<"Mutex not found";
544 result = OC_STACK_ERROR;
549 OCStackResult OCSecure::deregisterDisplayNumCallback()
551 OCStackResult result;
552 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
556 std::lock_guard<std::recursive_mutex> lock(*cLock);
557 DisplayNumContext* context = static_cast<DisplayNumContext*>(UnsetDisplayNumCB());
560 oclog() << "Delete registered display num context"<<std::endl;
563 result = OC_STACK_OK;
567 oclog() <<"Mutex not found";
568 result = OC_STACK_ERROR;
573 OCStackResult OCSecure::confirmUserCallbackWrapper(void* ctx)
575 UserConfirmNumContext* context = static_cast<UserConfirmNumContext*>(ctx);
578 oclog() << "Invalid context";
579 return OC_STACK_INVALID_PARAM;
582 return context->callback();
585 OCStackResult OCSecure::registerUserConfirmCallback(UserConfirmNumCB userConfirmCB)
589 oclog() << "Failed to set callback for confirming verifying callback.";
590 return OC_STACK_INVALID_CALLBACK;
593 OCStackResult result = OCSecure::deregisterUserConfirmCallback();
594 if (OC_STACK_OK != result)
596 oclog() << "Failed to de-register callback for comfirm."<<std::endl;
600 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
603 UserConfirmNumContext* context = new UserConfirmNumContext(userConfirmCB);
604 std::lock_guard<std::recursive_mutex> lock(*cLock);
605 SetUserConfirmCB(static_cast<void*>(context), &OCSecure::confirmUserCallbackWrapper);
606 result = OC_STACK_OK;
610 oclog() <<"Mutex not found";
611 result = OC_STACK_ERROR;
616 OCStackResult OCSecure::deregisterUserConfirmCallback()
618 OCStackResult result;
619 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
623 std::lock_guard<std::recursive_mutex> lock(*cLock);
624 UserConfirmNumContext* context = static_cast<UserConfirmNumContext*>(UnsetUserConfirmCB());
627 oclog() << "Delete registered user confirm context"<<std::endl;
630 result = OC_STACK_OK;
634 oclog() <<"Mutex not found";
635 result = OC_STACK_ERROR;
640 OCStackResult OCSecure::setVerifyOptionMask(VerifyOptionBitmask_t optionMask)
642 OCStackResult result;
643 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
646 std::lock_guard<std::recursive_mutex> lock(*cLock);
647 SetVerifyOption(optionMask);
648 result = OC_STACK_OK;
652 oclog() <<"Mutex not found";
653 result = OC_STACK_ERROR;
658 OCStackResult OCSecure::pdmCleanupForTimeout()
660 OCStackResult result;
661 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
664 result = OCPDMCleanupForTimeout();
668 oclog() <<"Mutex not found";
669 result = OC_STACK_ERROR;
674 OCStackResult OCSecure::configSelfOwnership()
676 OCStackResult result;
677 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
680 std::lock_guard<std::recursive_mutex> lock(*cLock);
681 result = OCConfigSelfOwnership();
685 oclog() <<"Mutex not found";
686 result = OC_STACK_ERROR;
691 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
692 OCStackResult OCSecure::saveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
693 OicEncodingType_t encodingType, uint16_t *credId)
697 oclog() <<"trustCertChain can't be null";
698 return OC_STACK_INVALID_PARAM;
702 oclog() <<"cred ID can not be null";
703 return OC_STACK_INVALID_PARAM;
706 OCStackResult result;
707 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
711 std::lock_guard<std::recursive_mutex> lock(*cLock);
712 result = OCSaveTrustCertChain(trustCertChain, chainSize, encodingType, credId );
716 oclog() <<"Mutex not found";
717 result = OC_STACK_ERROR;
722 OCStackResult OCSecure::readTrustCertChain(uint16_t credId, uint8_t **trustCertChain,
725 OCStackResult result;
726 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
730 std::lock_guard<std::recursive_mutex> lock(*cLock);
731 result = OCReadTrustCertChain(credId, trustCertChain, chainSize);
735 oclog() <<"Mutex not found";
736 result = OC_STACK_ERROR;
741 void OCSecure::certCallbackWrapper(void* ctx, uint16_t credId, uint8_t *trustCertChain,
744 TrustCertChainContext* context = static_cast<TrustCertChainContext*>(ctx);
745 uint8_t *certChain = new uint8_t[chainSize];
746 memcpy(certChain, trustCertChain, chainSize);
747 std::thread exec(context->callback, credId, certChain, chainSize);
752 OCStackResult OCSecure::registerTrustCertChangeNotifier(CertChainCallBack callback)
756 oclog() <<"callback can not be null";
757 return OC_STACK_INVALID_CALLBACK;
760 OCStackResult result;
761 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
765 TrustCertChainContext* context = new TrustCertChainContext(callback);
766 std::lock_guard<std::recursive_mutex> lock(*cLock);
767 result = OCRegisterTrustCertChainNotifier(static_cast<void*>(context),
768 &OCSecure::certCallbackWrapper);
772 oclog() <<"Mutex not found";
773 result = OC_STACK_ERROR;
779 OCStackResult OCSecure::removeTrustCertChangeNotifier()
781 OCStackResult result;
782 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
786 std::lock_guard<std::recursive_mutex> lock(*cLock);
787 OCRemoveTrustCertChainNotifier();
788 result = OC_STACK_OK;
792 oclog() <<"Mutex not found";
793 result = OC_STACK_ERROR;
798 OCStackResult OCSecure::setDeviceIdSeed(const uint8_t* seed, size_t seedSize)
802 oclog() <<"seed can not be null";
803 return OC_STACK_INVALID_PARAM;
806 return SetDeviceIdSeed(seed, seedSize);
809 int OCSecure::peerCertCallbackWrapper(void *ctx, const mbedtls_x509_crt *cert,
812 OCStackResult ret = OC_STACK_ERROR;
814 PeerCertContext *context = static_cast<PeerCertContext*>(ctx);
817 oclog() << "Invalid Context";
821 if (context->callback)
823 ret = context->callback(cert, depth);
826 return (OC_STACK_OK == ret)? 0 : 1;
829 OCStackResult OCSecure::setPeerCertCallback(PeerCertCB peerCertCallback)
831 OCStackResult result;
833 if (NULL == peerCertCallback)
835 result = OCSetPeerCertCallback(NULL, NULL);
836 if (OC_STACK_OK != result)
838 oclog() << "OCSetPeerCertCallback() Failed";
843 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
847 PeerCertContext *context = new PeerCertContext(peerCertCallback);
849 std::lock_guard<std::recursive_mutex> lock(*cLock);
850 result = OCSetPeerCertCallback(static_cast<void*>(context),
851 &OCSecure::peerCertCallbackWrapper);
852 if (OC_STACK_OK != result)
854 oclog() << "OCSetPeerCertCallback() Failed";
859 oclog() << "Mutex not found";
860 result = OC_STACK_ERROR;
865 #endif // __WITH_DTLS__ || __WITH_TLS__
867 void OCSecureResource::callbackWrapper(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
869 PMResultList_t *results = nullptr;
870 ProvisionContext* context = static_cast<ProvisionContext*>(ctx);
874 results = new PMResultList_t;
876 catch (std::bad_alloc& e)
878 oclog() <<"Bad alloc exception";
882 for (int i = 0; i < nOfRes; i++)
884 results->push_back(arr[i]);
887 std::thread exec(context->callback, results, hasError);
893 OCSecureResource::OCSecureResource(): m_csdkLock(std::weak_ptr<std::recursive_mutex>()),
899 OCSecureResource::OCSecureResource(std::weak_ptr<std::recursive_mutex> csdkLock,
900 OCProvisionDev_t *dPtr)
901 :m_csdkLock(csdkLock), devPtr(dPtr), context(nullptr)
905 OCSecureResource::~OCSecureResource()
909 OCDeleteDiscoveredDevices(devPtr);
917 OCStackResult OCSecureResource::doOwnershipTransfer(ResultCallBack resultCallback)
921 oclog() <<"Result callback can't be null";
922 return OC_STACK_INVALID_CALLBACK;
925 OCStackResult result;
926 auto cLock = m_csdkLock.lock();
930 ProvisionContext* context = new ProvisionContext(resultCallback);
935 context = new ProvisionContext(resultCallback);
937 std::lock_guard<std::recursive_mutex> lock(*cLock);
938 result = OCDoOwnershipTransfer(static_cast<void*>(context),
939 devPtr, &OCSecureResource::callbackWrapper);
943 oclog() <<"Mutex not found";
944 result = OC_STACK_ERROR;
949 OCStackResult OCSecureResource::doOwnershipTransfer(ResultCallBack resultCallback, const OicSecOxm_t method)
953 oclog() <<"Result callback can't be null";
954 return OC_STACK_INVALID_CALLBACK;
957 OCStackResult result;
958 auto cLock = m_csdkLock.lock();
966 context = new ProvisionContext(resultCallback);
968 std::lock_guard<std::recursive_mutex> lock(*cLock);
969 result = OCDoCustomOwnershipTransfer(static_cast<void*>(context),
970 devPtr, &OCSecureResource::callbackWrapper, method);
974 oclog() <<"Mutex not found";
975 result = OC_STACK_ERROR;
980 #ifdef MULTIPLE_OWNER
981 OCStackResult OCSecureResource::doMultipleOwnershipTransfer(ResultCallBack resultCallback)
985 oclog() <<"Result callback can't be null";
986 return OC_STACK_INVALID_CALLBACK;
989 OCStackResult result;
990 auto cLock = m_csdkLock.lock();
994 ProvisionContext* context = new ProvisionContext(resultCallback);
996 std::lock_guard<std::recursive_mutex> lock(*cLock);
997 result = OCDoMultipleOwnershipTransfer(static_cast<void*>(context),
998 devPtr, &OCSecureResource::callbackWrapper);
1002 oclog() <<"Mutex not found";
1003 result = OC_STACK_ERROR;
1008 OCStackResult OCSecureResource::getSubOwnerList(UuidList_t &uuidList)
1010 validateSecureResource();
1011 OicSecSubOwner_t* tmp = NULL;
1012 for (tmp = devPtr->doxm->subOwners; tmp; tmp = tmp->next)
1014 uuidList.push_back(tmp->uuid);
1020 OCStackResult OCSecureResource::provisionACL( const OicSecAcl_t* acl,
1021 ResultCallBack resultCallback)
1025 oclog() <<"ACL can't be null";
1026 return OC_STACK_INVALID_PARAM;
1028 if (!resultCallback)
1030 oclog() <<"result callback can not be null";
1031 return OC_STACK_INVALID_CALLBACK;
1034 OCStackResult result;
1035 auto cLock = m_csdkLock.lock();
1039 ProvisionContext* context = new ProvisionContext(resultCallback);
1041 std::lock_guard<std::recursive_mutex> lock(*cLock);
1042 result = OCProvisionACL(static_cast<void*>(context),
1043 devPtr, const_cast<OicSecAcl_t*>(acl),
1044 &OCSecureResource::callbackWrapper);
1048 oclog() <<"Mutex not found";
1049 result = OC_STACK_ERROR;
1054 OCStackResult OCSecureResource::provisionCredentials(const Credential &cred,
1055 const OCSecureResource &device2, ResultCallBack resultCallback)
1057 if (!resultCallback)
1059 oclog() << "Result calback can't be null";
1060 return OC_STACK_INVALID_CALLBACK;
1063 OCStackResult result;
1064 auto cLock = m_csdkLock.lock();
1068 ProvisionContext* context = new ProvisionContext(resultCallback);
1070 std::lock_guard<std::recursive_mutex> lock(*cLock);
1071 result = OCProvisionCredentials(static_cast<void*>(context),
1072 cred.getCredentialType(),
1073 cred.getCredentialKeySize(),
1074 devPtr, device2.getDevPtr(),
1075 &OCSecureResource::callbackWrapper);
1079 oclog() <<"Mutex not found";
1080 result = OC_STACK_ERROR;
1085 OCStackResult OCSecureResource::provisionPairwiseDevices(const Credential &cred,
1086 const OicSecAcl_t* acl1, const OCSecureResource &device2, const OicSecAcl_t* acl2,
1087 ResultCallBack resultCallback)
1089 if (!resultCallback)
1091 oclog() << "Result callback can not be null";
1092 return OC_STACK_INVALID_CALLBACK;
1095 OCStackResult result;
1096 auto cLock = m_csdkLock.lock();
1100 ProvisionContext* context = new ProvisionContext(resultCallback);
1102 std::lock_guard<std::recursive_mutex> lock(*cLock);
1103 result = OCProvisionPairwiseDevices(static_cast<void*>(context),
1104 cred.getCredentialType(),
1105 cred.getCredentialKeySize(),
1106 devPtr, const_cast<OicSecAcl_t*>(acl1),
1107 device2.getDevPtr(), const_cast<OicSecAcl_t*>(acl2),
1108 &OCSecureResource::callbackWrapper);
1112 oclog() <<"Mutex not found";
1113 result = OC_STACK_ERROR;
1118 OCStackResult OCSecureResource::unlinkDevices(const OCSecureResource &device2,
1119 ResultCallBack resultCallback)
1121 if (!resultCallback)
1123 oclog() << "Result calback can't be null";
1124 return OC_STACK_INVALID_CALLBACK;
1127 OCStackResult result;
1128 auto cLock = m_csdkLock.lock();
1132 ProvisionContext* context = new ProvisionContext(resultCallback);
1134 std::lock_guard<std::recursive_mutex> lock(*cLock);
1136 result = OCUnlinkDevices(static_cast<void*>(context),
1137 devPtr, device2.getDevPtr(), &OCSecureResource::callbackWrapper);
1141 oclog() <<"Mutex not found";
1142 result = OC_STACK_ERROR;
1147 OCStackResult OCSecureResource::removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery,
1148 ResultCallBack resultCallback)
1150 if (!resultCallback)
1152 oclog() << "Result calback can't be null";
1153 return OC_STACK_INVALID_CALLBACK;
1156 OCStackResult result;
1157 auto cLock = m_csdkLock.lock();
1161 ProvisionContext* context = new ProvisionContext(resultCallback);
1163 std::lock_guard<std::recursive_mutex> lock(*cLock);
1165 result = OCRemoveDevice(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
1166 devPtr, &OCSecureResource::callbackWrapper);
1170 oclog() <<"Mutex not found";
1171 result = OC_STACK_ERROR;
1176 OCStackResult OCSecureResource::getLinkedDevices(UuidList_t &uuidList)
1178 OCStackResult result;
1179 size_t numOfDevices = -1;
1180 auto devUuid = devPtr->doxm->deviceID;
1181 auto cLock = m_csdkLock.lock();
1185 std::lock_guard<std::recursive_mutex> lock(*cLock);
1187 OCUuidList_t* linkedDevs = nullptr, *tmp = nullptr;
1188 result = OCGetLinkedStatus(&devUuid, &linkedDevs, &numOfDevices);
1189 if (result == OC_STACK_OK)
1191 for (tmp = linkedDevs; tmp; tmp = tmp->next)
1193 uuidList.push_back(tmp->dev);
1195 OCDeleteUuidList(linkedDevs);
1200 oclog() <<"Mutex not found";
1201 result = OC_STACK_ERROR;
1206 OCStackResult OCSecureResource::provisionDirectPairing( const OicSecPconf_t* pconf,
1207 ResultCallBack resultCallback)
1211 oclog() <<"PCONF can't be null";
1212 return OC_STACK_INVALID_PARAM;
1214 if (!resultCallback)
1216 oclog() <<"result callback can not be null";
1217 return OC_STACK_INVALID_CALLBACK;
1220 OCStackResult result;
1221 auto cLock = m_csdkLock.lock();
1225 ProvisionContext* context = new ProvisionContext(resultCallback);
1227 std::lock_guard<std::recursive_mutex> lock(*cLock);
1228 result = OCProvisionDirectPairing(static_cast<void*>(context),
1229 devPtr, const_cast<OicSecPconf_t*>(pconf),
1230 &OCSecureResource::callbackWrapper);
1234 oclog() <<"Mutex not found";
1235 result = OC_STACK_ERROR;
1240 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
1241 OCStackResult OCSecureResource::provisionTrustCertChain(OicSecCredType_t type, uint16_t credId,
1242 ResultCallBack resultCallback)
1244 if (SIGNED_ASYMMETRIC_KEY != type)
1246 oclog() <<"Invalid key type";
1247 return OC_STACK_INVALID_PARAM;
1249 if (!resultCallback)
1251 oclog() <<"result callback can not be null";
1252 return OC_STACK_INVALID_CALLBACK;
1255 OCStackResult result;
1256 auto cLock = m_csdkLock.lock();
1260 ProvisionContext* context = new ProvisionContext(resultCallback);
1262 std::lock_guard<std::recursive_mutex> lock(*cLock);
1263 result = OCProvisionTrustCertChain(static_cast<void*>(context),
1264 type, credId, devPtr,
1265 &OCSecureResource::callbackWrapper);
1269 oclog() <<"Mutex not found";
1270 result = OC_STACK_ERROR;
1274 #endif // __WITH_DTLS__ or __WITH_TLS__
1276 std::string OCSecureResource::getDeviceID()
1278 std::ostringstream deviceId("");
1279 char *devID = nullptr;
1281 validateSecureResource();
1283 if (OC_STACK_OK == ConvertUuidToStr(&(devPtr->doxm->deviceID), &devID))
1290 oclog() <<"Can not convert uuid to struuid";
1292 return deviceId.str();
1295 OCProvisionDev_t* OCSecureResource::getDevPtr() const
1300 std::string OCSecureResource::getDevAddr()
1302 validateSecureResource();
1303 std::string ipAddr(devPtr->endpoint.addr);
1307 int OCSecureResource::getDeviceStatus()
1309 validateSecureResource();
1310 return (int)devPtr->devStatus;
1313 bool OCSecureResource::getOwnedStatus()
1315 validateSecureResource();
1316 return devPtr->doxm->owned;
1319 void OCSecureResource::validateSecureResource()
1323 throw OCException("Incomplete secure resource", OC_STACK_RESOURCE_ERROR);
1327 OCStackResult OCSecureResource::getOTMethod(OicSecOxm_t* oxm)
1331 oclog() << "Null param";
1332 return OC_STACK_INVALID_PARAM;
1335 OCStackResult result = OC_STACK_ERROR;
1336 auto cLock = m_csdkLock.lock();
1339 std::lock_guard<std::recursive_mutex> lock(*cLock);
1340 if(devPtr && devPtr->doxm)
1342 result = OCSelectOwnershipTransferMethod(devPtr->doxm->oxm, devPtr->doxm->oxmLen,
1348 oclog() <<"Mutex not found";
1354 #ifdef MULTIPLE_OWNER
1355 OCStackResult OCSecureResource::getMOTMethod( OicSecOxm_t* oxm)
1359 oclog() << "Null param";
1360 return OC_STACK_INVALID_PARAM;
1363 OCStackResult result = OC_STACK_ERROR;
1364 auto cLock = m_csdkLock.lock();
1367 std::lock_guard<std::recursive_mutex> lock(*cLock);
1368 if (devPtr && devPtr->doxm)
1370 result = OCSelectOwnershipTransferMethod(devPtr->doxm->oxm, devPtr->doxm->oxmLen,
1376 oclog() <<"Mutex not found";
1381 bool OCSecureResource::isMOTSupported()
1383 if (devPtr && devPtr->doxm)
1385 return (devPtr->doxm->mom ? true : false);
1390 bool OCSecureResource::isMOTEnabled()
1392 if (devPtr && devPtr->doxm && devPtr->doxm->mom)
1394 if (OIC_MULTIPLE_OWNER_DISABLE != devPtr->doxm->mom->mode)
1402 OCStackResult OCSecureResource::selectMOTMethod( const OicSecOxm_t oxmSelVal,
1403 ResultCallBack resultCallback)
1405 if (!resultCallback)
1407 oclog() <<"result callback can not be null";
1408 return OC_STACK_INVALID_CALLBACK;
1411 OCStackResult result;
1412 auto cLock = m_csdkLock.lock();
1416 ProvisionContext* context = new ProvisionContext(resultCallback);
1418 std::lock_guard<std::recursive_mutex> lock(*cLock);
1419 result = OCSelectMOTMethod(static_cast<void*>(context),
1421 &OCSecureResource::callbackWrapper);
1425 oclog() <<"Mutex not found";
1426 result = OC_STACK_ERROR;
1431 OCStackResult OCSecureResource::changeMOTMode( const OicSecMomType_t momType,
1432 ResultCallBack resultCallback)
1434 if (!resultCallback)
1436 oclog() <<"result callback can not be null";
1437 return OC_STACK_INVALID_CALLBACK;
1440 OCStackResult result;
1441 auto cLock = m_csdkLock.lock();
1445 ProvisionContext* context = new ProvisionContext(resultCallback);
1447 std::lock_guard<std::recursive_mutex> lock(*cLock);
1448 result = OCChangeMOTMode(static_cast<void*>(context),
1450 &OCSecureResource::callbackWrapper);
1454 oclog() <<"Mutex not found";
1455 result = OC_STACK_ERROR;
1461 OCStackResult OCSecureResource::addPreconfigPIN(const char* preconfPIN,
1462 size_t preconfPINLength)
1466 oclog() <<"pre config pin can not be null";
1467 return OC_STACK_INVALID_PARAM;
1469 if (preconfPINLength <= 0)
1471 oclog() <<"pre config pin length can not be zero or less";
1472 return OC_STACK_INVALID_PARAM;
1474 OCStackResult result;
1475 auto cLock = m_csdkLock.lock();
1479 std::lock_guard<std::recursive_mutex> lock(*cLock);
1480 result = OCAddPreconfigPin(devPtr, preconfPIN,
1485 oclog() <<"Mutex not found";
1486 result = OC_STACK_ERROR;
1491 OCStackResult OCSecureResource::provisionPreconfPin(const char * preconfPin,
1492 size_t preconfPinLength, ResultCallBack resultCallback)
1494 if (!resultCallback)
1496 oclog() <<"result callback can not be null";
1497 return OC_STACK_INVALID_CALLBACK;
1501 oclog() <<"pre config pin can not be null";
1502 return OC_STACK_INVALID_PARAM;
1504 if (preconfPinLength <= 0)
1506 oclog() <<"pre config pin length can not be zero or less";
1507 return OC_STACK_INVALID_PARAM;
1510 OCStackResult result;
1511 auto cLock = m_csdkLock.lock();
1515 ProvisionContext* context = new ProvisionContext(resultCallback);
1517 std::lock_guard<std::recursive_mutex> lock(*cLock);
1518 result = OCProvisionPreconfigPin(static_cast<void*>(context),
1519 devPtr, preconfPin, preconfPinLength,
1520 &OCSecureResource::callbackWrapper);
1524 oclog() <<"Mutex not found";
1525 result = OC_STACK_ERROR;
1530 OCStackResult OCSecureResource::removeSubOwner(const OicUuid_t* subOwnerId, ResultCallBack resultCallback)
1532 validateSecureResource();
1533 OCStackResult result;
1534 auto cLock = m_csdkLock.lock();
1537 ProvisionContext* context = new ProvisionContext(resultCallback);
1538 result = OCRemoveSubOwner(static_cast<void*>(context),
1539 devPtr, subOwnerId, &OCSecureResource::callbackWrapper);
1543 oclog() << "Mutex not found";
1544 result = OC_STACK_ERROR;
1549 OCStackResult OCSecureResource::removeAllSubOwner(ResultCallBack resultCallback)
1551 validateSecureResource();
1552 OCStackResult result;
1553 auto cLock = m_csdkLock.lock();
1556 ProvisionContext* context = new ProvisionContext(resultCallback);
1557 result =OCRemoveAllSubOwner(static_cast<void*>(context),
1558 devPtr, &OCSecureResource::callbackWrapper);
1562 oclog() << "Mutex not found";
1563 result = OC_STACK_ERROR;
1568 #endif // MULTIPLE_OWNER