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.h"
28 OCStackResult OCSecure::provisionInit(const std::string& dbPath)
31 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
35 std::lock_guard<std::recursive_mutex> lock(*cLock);
36 result = OCInitPM(dbPath.c_str());
40 oclog() <<"Mutex not found";
41 result = OC_STACK_ERROR;
47 OCStackResult OCSecure::discoverUnownedDevices(unsigned short timeout,
51 OCProvisionDev_t *pDevList = nullptr, *pCurDev = nullptr, *tmp = nullptr;
52 auto csdkLock = OCPlatform_impl::Instance().csdkLock();
53 auto cLock = csdkLock.lock();
57 std::lock_guard<std::recursive_mutex> lock(*cLock);
58 result = OCDiscoverUnownedDevices(timeout, &pDevList);
59 if (result == OC_STACK_OK)
61 // Create DeviceList of OCSecureResource's
66 list.push_back(std::shared_ptr<OCSecureResource>(
67 new OCSecureResource(csdkLock, pCurDev)));
68 pCurDev = pCurDev->next;
74 oclog() <<"Unowned device discovery failed!";
79 oclog() <<"Mutex not found";
80 result = OC_STACK_ERROR;
86 OCStackResult OCSecure::discoverOwnedDevices(unsigned short timeout,
90 OCProvisionDev_t *pDevList = nullptr, *pCurDev = nullptr, *tmp = nullptr;
91 auto csdkLock = OCPlatform_impl::Instance().csdkLock();
92 auto cLock = csdkLock.lock();
96 std::lock_guard<std::recursive_mutex> lock(*cLock);
97 result = OCDiscoverOwnedDevices(timeout, &pDevList);
98 if (result == OC_STACK_OK)
104 list.push_back(std::shared_ptr<OCSecureResource>(
105 new OCSecureResource(csdkLock, pCurDev)));
106 pCurDev = pCurDev->next;
112 oclog() <<"Owned device discovery failed!";
117 oclog() <<"Mutex not found";
118 result = OC_STACK_ERROR;
124 OCStackResult OCSecure::discoverSingleDevice(unsigned short timeout,
125 const OicUuid_t* deviceID,
126 std::shared_ptr<OCSecureResource> &foundDevice)
128 OCStackResult result;
129 OCProvisionDev_t *pDev = nullptr;
130 auto csdkLock = OCPlatform_impl::Instance().csdkLock();
131 auto cLock = csdkLock.lock();
135 std::lock_guard<std::recursive_mutex> lock(*cLock);
136 result = OCDiscoverSingleDevice(timeout, deviceID, &pDev);
137 if (result == OC_STACK_OK)
141 foundDevice.reset(new OCSecureResource(csdkLock, pDev));
145 oclog() <<"Not found Secure resource!";
151 oclog() <<"Secure resource discovery failed!";
156 oclog() <<"Mutex not found";
157 result = OC_STACK_ERROR;
163 OCStackResult OCSecure::setOwnerTransferCallbackData(OicSecOxm_t oxm,
164 OTMCallbackData_t* callbackData, InputPinCallback inputPin)
166 if (NULL == callbackData || oxm >= OIC_OXM_COUNT)
168 oclog() <<"Invalid callbackData or OXM type";
169 return OC_STACK_INVALID_PARAM;
172 if ((OIC_RANDOM_DEVICE_PIN == oxm) && !inputPin)
174 oclog() <<"for OXM type DEVICE_PIN, inputPin callback can't be null";
175 return OC_STACK_INVALID_PARAM;
178 OCStackResult result;
179 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
183 std::lock_guard<std::recursive_mutex> lock(*cLock);
184 result = OCSetOwnerTransferCallbackData(oxm, callbackData);
185 if (result == OC_STACK_OK && (OIC_RANDOM_DEVICE_PIN == oxm))
187 SetInputPinCB(inputPin);
192 oclog() <<"Mutex not found";
193 result = OC_STACK_ERROR;
200 OCStackResult OCSecure::getDevInfoFromNetwork(unsigned short timeout,
201 DeviceList_t &ownedDevList,
202 DeviceList_t &unownedDevList)
204 OCStackResult result = OC_STACK_OK;
205 OCProvisionDev_t *owned = nullptr, *unowned = nullptr, *tmp = nullptr, *dev = nullptr;
206 auto csdkLock = OCPlatform_impl::Instance().csdkLock();
207 auto cLock = csdkLock.lock();
211 std::lock_guard<std::recursive_mutex> lock(*cLock);
213 result = OCGetDevInfoFromNetwork(timeout, &owned, &unowned);
215 if (result == OC_STACK_OK)
221 ownedDevList.push_back(std::shared_ptr<OCSecureResource>(
222 new OCSecureResource(csdkLock, dev)));
231 unownedDevList.push_back(std::shared_ptr<OCSecureResource>(
232 new OCSecureResource(csdkLock, dev)));
240 oclog() <<"Mutex not found";
241 result = OC_STACK_ERROR;
247 OCStackResult OCSecure::setDisplayPinCB(GeneratePinCallback displayPin)
251 oclog() <<"displayPin can't be null";
252 return OC_STACK_INVALID_PARAM;
255 OCStackResult result = OC_STACK_OK;
256 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
260 std::lock_guard<std::recursive_mutex> lock(*cLock);
261 SetGeneratePinCB(displayPin);
265 oclog() <<"Mutex not found";
266 result = OC_STACK_ERROR;
272 OCStackResult OCSecure::removeDeviceWithUuid(unsigned short waitTimeForOwnedDeviceDiscovery,
274 ResultCallBack resultCallback)
278 oclog() << "Result calback can't be null";
279 return OC_STACK_INVALID_CALLBACK;
282 OCStackResult result;
283 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
287 ProvisionContext* context = new ProvisionContext(resultCallback);
289 std::lock_guard<std::recursive_mutex> lock(*cLock);
292 result = ConvertStrToUuid(uuid.c_str(), &targetDev);
293 if(OC_STACK_OK == result)
295 result = OCRemoveDeviceWithUuid(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
296 &targetDev, &OCSecureResource::callbackWrapper);
300 oclog() <<"Can not convert struuid to uuid";
305 oclog() <<"Mutex not found";
306 result = OC_STACK_ERROR;
311 OCStackResult OCSecure::saveACL(const OicSecAcl_t* acl)
315 oclog() <<"ACL can't be null";
316 return OC_STACK_INVALID_PARAM;
319 OCStackResult result;
320 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
324 std::lock_guard<std::recursive_mutex> lock(*cLock);
325 result = OCSaveACL(const_cast<OicSecAcl_t*>(acl));
329 oclog() <<"Mutex not found";
330 result = OC_STACK_ERROR;
335 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
336 OCStackResult OCSecure::saveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
337 OicEncodingType_t encodingType, uint16_t *credId)
341 oclog() <<"trustCertChain can't be null";
342 return OC_STACK_INVALID_PARAM;
346 oclog() <<"cred ID can not be null";
347 return OC_STACK_INVALID_PARAM;
350 OCStackResult result;
351 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
355 std::lock_guard<std::recursive_mutex> lock(*cLock);
356 result = OCSaveTrustCertChain(trustCertChain, chainSize, encodingType, credId );
360 oclog() <<"Mutex not found";
361 result = OC_STACK_ERROR;
366 OCStackResult OCSecure::readTrustCertChain(uint16_t credId, uint8_t **trustCertChain,
369 OCStackResult result;
370 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
374 std::lock_guard<std::recursive_mutex> lock(*cLock);
375 result = OCReadTrustCertChain(credId, trustCertChain, chainSize);
379 oclog() <<"Mutex not found";
380 result = OC_STACK_ERROR;
384 #endif // __WITH_DTLS__ || __WITH_TLS__
386 void OCSecureResource::callbackWrapper(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
388 PMResultList_t *results = nullptr;
389 ProvisionContext* context = static_cast<ProvisionContext*>(ctx);
393 results = new PMResultList_t;
395 catch (std::bad_alloc& e)
397 oclog() <<"Bad alloc exception";
401 for (int i = 0; i < nOfRes; i++)
403 results->push_back(arr[i]);
406 std::thread exec(context->callback, results, hasError);
412 OCSecureResource::OCSecureResource(): m_csdkLock(std::weak_ptr<std::recursive_mutex>()),
417 OCSecureResource::OCSecureResource(std::weak_ptr<std::recursive_mutex> csdkLock,
418 OCProvisionDev_t *dPtr)
419 :m_csdkLock(csdkLock), devPtr(dPtr)
423 OCSecureResource::~OCSecureResource()
427 OCDeleteDiscoveredDevices(devPtr);
431 OCStackResult OCSecureResource::doOwnershipTransfer(ResultCallBack resultCallback)
435 oclog() <<"Result callback can't be null";
436 return OC_STACK_INVALID_CALLBACK;
439 OCStackResult result;
440 auto cLock = m_csdkLock.lock();
444 ProvisionContext* context = new ProvisionContext(resultCallback);
446 std::lock_guard<std::recursive_mutex> lock(*cLock);
447 result = OCDoOwnershipTransfer(static_cast<void*>(context),
448 devPtr, &OCSecureResource::callbackWrapper);
452 oclog() <<"Mutex not found";
453 result = OC_STACK_ERROR;
458 OCStackResult OCSecureResource::provisionACL( const OicSecAcl_t* acl,
459 ResultCallBack resultCallback)
463 oclog() <<"ACL can't be null";
464 return OC_STACK_INVALID_PARAM;
468 oclog() <<"result callback can not be null";
469 return OC_STACK_INVALID_CALLBACK;
472 OCStackResult result;
473 auto cLock = m_csdkLock.lock();
477 ProvisionContext* context = new ProvisionContext(resultCallback);
479 std::lock_guard<std::recursive_mutex> lock(*cLock);
480 result = OCProvisionACL(static_cast<void*>(context),
481 devPtr, const_cast<OicSecAcl_t*>(acl),
482 &OCSecureResource::callbackWrapper);
486 oclog() <<"Mutex not found";
487 result = OC_STACK_ERROR;
492 OCStackResult OCSecureResource::provisionCredentials(const Credential &cred,
493 const OCSecureResource &device2, ResultCallBack resultCallback)
497 oclog() << "Result calback can't be null";
498 return OC_STACK_INVALID_CALLBACK;
501 OCStackResult result;
502 auto cLock = m_csdkLock.lock();
506 ProvisionContext* context = new ProvisionContext(resultCallback);
508 std::lock_guard<std::recursive_mutex> lock(*cLock);
509 result = OCProvisionCredentials(static_cast<void*>(context),
510 cred.getCredentialType(),
511 cred.getCredentialKeySize(),
512 devPtr, device2.getDevPtr(),
513 &OCSecureResource::callbackWrapper);
517 oclog() <<"Mutex not found";
518 result = OC_STACK_ERROR;
523 OCStackResult OCSecureResource::provisionPairwiseDevices(const Credential &cred,
524 const OicSecAcl_t* acl1, const OCSecureResource &device2, const OicSecAcl_t* acl2,
525 ResultCallBack resultCallback)
529 oclog() << "Result callback can not be null";
530 return OC_STACK_INVALID_CALLBACK;
533 OCStackResult result;
534 auto cLock = m_csdkLock.lock();
538 ProvisionContext* context = new ProvisionContext(resultCallback);
540 std::lock_guard<std::recursive_mutex> lock(*cLock);
541 result = OCProvisionPairwiseDevices(static_cast<void*>(context),
542 cred.getCredentialType(),
543 cred.getCredentialKeySize(),
544 devPtr, const_cast<OicSecAcl_t*>(acl1),
545 device2.getDevPtr(), const_cast<OicSecAcl_t*>(acl2),
546 &OCSecureResource::callbackWrapper);
550 oclog() <<"Mutex not found";
551 result = OC_STACK_ERROR;
556 OCStackResult OCSecureResource::unlinkDevices(const OCSecureResource &device2,
557 ResultCallBack resultCallback)
561 oclog() << "Result calback can't be null";
562 return OC_STACK_INVALID_CALLBACK;
565 OCStackResult result;
566 auto cLock = m_csdkLock.lock();
570 ProvisionContext* context = new ProvisionContext(resultCallback);
572 std::lock_guard<std::recursive_mutex> lock(*cLock);
574 result = OCUnlinkDevices(static_cast<void*>(context),
575 devPtr, device2.getDevPtr(), &OCSecureResource::callbackWrapper);
579 oclog() <<"Mutex not found";
580 result = OC_STACK_ERROR;
585 OCStackResult OCSecureResource::removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery,
586 ResultCallBack resultCallback)
590 oclog() << "Result calback can't be null";
591 return OC_STACK_INVALID_CALLBACK;
594 OCStackResult result;
595 auto cLock = m_csdkLock.lock();
599 ProvisionContext* context = new ProvisionContext(resultCallback);
601 std::lock_guard<std::recursive_mutex> lock(*cLock);
603 result = OCRemoveDevice(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
604 devPtr, &OCSecureResource::callbackWrapper);
608 oclog() <<"Mutex not found";
609 result = OC_STACK_ERROR;
614 OCStackResult OCSecureResource::getLinkedDevices(UuidList_t &uuidList)
616 OCStackResult result;
617 size_t numOfDevices = -1;
618 auto devUuid = devPtr->doxm->deviceID;
619 auto cLock = m_csdkLock.lock();
623 std::lock_guard<std::recursive_mutex> lock(*cLock);
625 OCUuidList_t* linkedDevs = nullptr, *tmp = nullptr;
626 result = OCGetLinkedStatus(&devUuid, &linkedDevs, &numOfDevices);
627 if (result == OC_STACK_OK)
629 for (tmp = linkedDevs; tmp; tmp = tmp->next)
631 uuidList.push_back(tmp->dev);
633 OCDeleteUuidList(linkedDevs);
638 oclog() <<"Mutex not found";
639 result = OC_STACK_ERROR;
644 OCStackResult OCSecureResource::provisionDirectPairing( const OicSecPconf_t* pconf,
645 ResultCallBack resultCallback)
649 oclog() <<"PCONF can't be null";
650 return OC_STACK_INVALID_PARAM;
654 oclog() <<"result callback can not be null";
655 return OC_STACK_INVALID_CALLBACK;
658 OCStackResult result;
659 auto cLock = m_csdkLock.lock();
663 ProvisionContext* context = new ProvisionContext(resultCallback);
665 std::lock_guard<std::recursive_mutex> lock(*cLock);
666 result = OCProvisionDirectPairing(static_cast<void*>(context),
667 devPtr, const_cast<OicSecPconf_t*>(pconf),
668 &OCSecureResource::callbackWrapper);
672 oclog() <<"Mutex not found";
673 result = OC_STACK_ERROR;
678 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
679 OCStackResult OCSecureResource::provisionTrustCertChain(OicSecCredType_t type, uint16_t credId,
680 ResultCallBack resultCallback)
682 if (SIGNED_ASYMMETRIC_KEY != type)
684 oclog() <<"Invalid key type";
685 return OC_STACK_INVALID_PARAM;
689 oclog() <<"result callback can not be null";
690 return OC_STACK_INVALID_CALLBACK;
693 OCStackResult result;
694 auto cLock = m_csdkLock.lock();
698 ProvisionContext* context = new ProvisionContext(resultCallback);
700 std::lock_guard<std::recursive_mutex> lock(*cLock);
701 result = OCProvisionTrustCertChain(static_cast<void*>(context),
702 type, credId, devPtr,
703 &OCSecureResource::callbackWrapper);
707 oclog() <<"Mutex not found";
708 result = OC_STACK_ERROR;
712 #endif // __WITH_DTLS__ or __WITH_TLS__
714 std::string OCSecureResource::getDeviceID()
716 std::ostringstream deviceId("");
717 char *devID = nullptr;
719 validateSecureResource();
721 if (OC_STACK_OK == ConvertUuidToStr(&(devPtr->doxm->deviceID), &devID))
728 oclog() <<"Can not convert uuid to struuid";
730 return deviceId.str();
733 OCProvisionDev_t* OCSecureResource::getDevPtr() const
738 std::string OCSecureResource::getDevAddr()
740 validateSecureResource();
741 std::string ipAddr(devPtr->endpoint.addr);
745 int OCSecureResource::getDeviceStatus()
747 validateSecureResource();
748 return (int)devPtr->devStatus;
751 bool OCSecureResource::getOwnedStatus()
753 validateSecureResource();
754 return devPtr->doxm->owned;
757 void OCSecureResource::validateSecureResource()
761 throw OCException("Incomplete secure resource", OC_STACK_RESOURCE_ERROR);