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;
365 #endif // __WITH_DTLS__ || __WITH_TLS__
367 void OCSecureResource::callbackWrapper(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
369 PMResultList_t *results = nullptr;
370 ProvisionContext* context = static_cast<ProvisionContext*>(ctx);
374 results = new PMResultList_t;
376 catch (std::bad_alloc& e)
378 oclog() <<"Bad alloc exception";
382 for (int i = 0; i < nOfRes; i++)
384 results->push_back(arr[i]);
387 std::thread exec(context->callback, results, hasError);
393 OCSecureResource::OCSecureResource(): m_csdkLock(std::weak_ptr<std::recursive_mutex>()),
398 OCSecureResource::OCSecureResource(std::weak_ptr<std::recursive_mutex> csdkLock,
399 OCProvisionDev_t *dPtr)
400 :m_csdkLock(csdkLock), devPtr(dPtr)
404 OCSecureResource::~OCSecureResource()
408 OCDeleteDiscoveredDevices(devPtr);
412 OCStackResult OCSecureResource::doOwnershipTransfer(ResultCallBack resultCallback)
416 oclog() <<"Result callback can't be null";
417 return OC_STACK_INVALID_CALLBACK;
420 OCStackResult result;
421 auto cLock = m_csdkLock.lock();
425 ProvisionContext* context = new ProvisionContext(resultCallback);
427 std::lock_guard<std::recursive_mutex> lock(*cLock);
428 result = OCDoOwnershipTransfer(static_cast<void*>(context),
429 devPtr, &OCSecureResource::callbackWrapper);
433 oclog() <<"Mutex not found";
434 result = OC_STACK_ERROR;
439 OCStackResult OCSecureResource::provisionACL( const OicSecAcl_t* acl,
440 ResultCallBack resultCallback)
444 oclog() <<"ACL can't be null";
445 return OC_STACK_INVALID_PARAM;
449 oclog() <<"result callback can not be null";
450 return OC_STACK_INVALID_CALLBACK;
453 OCStackResult result;
454 auto cLock = m_csdkLock.lock();
458 ProvisionContext* context = new ProvisionContext(resultCallback);
460 std::lock_guard<std::recursive_mutex> lock(*cLock);
461 result = OCProvisionACL(static_cast<void*>(context),
462 devPtr, const_cast<OicSecAcl_t*>(acl),
463 &OCSecureResource::callbackWrapper);
467 oclog() <<"Mutex not found";
468 result = OC_STACK_ERROR;
473 OCStackResult OCSecureResource::provisionCredentials(const Credential &cred,
474 const OCSecureResource &device2, ResultCallBack resultCallback)
478 oclog() << "Result calback can't be null";
479 return OC_STACK_INVALID_CALLBACK;
482 OCStackResult result;
483 auto cLock = m_csdkLock.lock();
487 ProvisionContext* context = new ProvisionContext(resultCallback);
489 std::lock_guard<std::recursive_mutex> lock(*cLock);
490 result = OCProvisionCredentials(static_cast<void*>(context),
491 cred.getCredentialType(),
492 cred.getCredentialKeySize(),
493 devPtr, device2.getDevPtr(),
494 &OCSecureResource::callbackWrapper);
498 oclog() <<"Mutex not found";
499 result = OC_STACK_ERROR;
504 OCStackResult OCSecureResource::provisionPairwiseDevices(const Credential &cred,
505 const OicSecAcl_t* acl1, const OCSecureResource &device2, const OicSecAcl_t* acl2,
506 ResultCallBack resultCallback)
510 oclog() << "Result callback can not be null";
511 return OC_STACK_INVALID_CALLBACK;
514 OCStackResult result;
515 auto cLock = m_csdkLock.lock();
519 ProvisionContext* context = new ProvisionContext(resultCallback);
521 std::lock_guard<std::recursive_mutex> lock(*cLock);
522 result = OCProvisionPairwiseDevices(static_cast<void*>(context),
523 cred.getCredentialType(),
524 cred.getCredentialKeySize(),
525 devPtr, const_cast<OicSecAcl_t*>(acl1),
526 device2.getDevPtr(), const_cast<OicSecAcl_t*>(acl2),
527 &OCSecureResource::callbackWrapper);
531 oclog() <<"Mutex not found";
532 result = OC_STACK_ERROR;
537 OCStackResult OCSecureResource::unlinkDevices(const OCSecureResource &device2,
538 ResultCallBack resultCallback)
542 oclog() << "Result calback can't be null";
543 return OC_STACK_INVALID_CALLBACK;
546 OCStackResult result;
547 auto cLock = m_csdkLock.lock();
551 ProvisionContext* context = new ProvisionContext(resultCallback);
553 std::lock_guard<std::recursive_mutex> lock(*cLock);
555 result = OCUnlinkDevices(static_cast<void*>(context),
556 devPtr, device2.getDevPtr(), &OCSecureResource::callbackWrapper);
560 oclog() <<"Mutex not found";
561 result = OC_STACK_ERROR;
566 OCStackResult OCSecureResource::removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery,
567 ResultCallBack resultCallback)
571 oclog() << "Result calback can't be null";
572 return OC_STACK_INVALID_CALLBACK;
575 OCStackResult result;
576 auto cLock = m_csdkLock.lock();
580 ProvisionContext* context = new ProvisionContext(resultCallback);
582 std::lock_guard<std::recursive_mutex> lock(*cLock);
584 result = OCRemoveDevice(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
585 devPtr, &OCSecureResource::callbackWrapper);
589 oclog() <<"Mutex not found";
590 result = OC_STACK_ERROR;
595 OCStackResult OCSecureResource::getLinkedDevices(UuidList_t &uuidList)
597 OCStackResult result;
598 size_t numOfDevices = -1;
599 auto devUuid = devPtr->doxm->deviceID;
600 auto cLock = m_csdkLock.lock();
604 std::lock_guard<std::recursive_mutex> lock(*cLock);
606 OCUuidList_t* linkedDevs = nullptr, *tmp = nullptr;
607 result = OCGetLinkedStatus(&devUuid, &linkedDevs, &numOfDevices);
608 if (result == OC_STACK_OK)
610 for (tmp = linkedDevs; tmp; tmp = tmp->next)
612 uuidList.push_back(tmp->dev);
614 OCDeleteUuidList(linkedDevs);
619 oclog() <<"Mutex not found";
620 result = OC_STACK_ERROR;
625 OCStackResult OCSecureResource::provisionDirectPairing( const OicSecPconf_t* pconf,
626 ResultCallBack resultCallback)
630 oclog() <<"PCONF can't be null";
631 return OC_STACK_INVALID_PARAM;
635 oclog() <<"result callback can not be null";
636 return OC_STACK_INVALID_CALLBACK;
639 OCStackResult result;
640 auto cLock = m_csdkLock.lock();
644 ProvisionContext* context = new ProvisionContext(resultCallback);
646 std::lock_guard<std::recursive_mutex> lock(*cLock);
647 result = OCProvisionDirectPairing(static_cast<void*>(context),
648 devPtr, const_cast<OicSecPconf_t*>(pconf),
649 &OCSecureResource::callbackWrapper);
653 oclog() <<"Mutex not found";
654 result = OC_STACK_ERROR;
659 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
660 OCStackResult OCSecureResource::provisionTrustCertChain(OicSecCredType_t type, uint16_t credId,
661 ResultCallBack resultCallback)
663 if (SIGNED_ASYMMETRIC_KEY != type)
665 oclog() <<"Invalid key type";
666 return OC_STACK_INVALID_PARAM;
670 oclog() <<"result callback can not be null";
671 return OC_STACK_INVALID_CALLBACK;
674 OCStackResult result;
675 auto cLock = m_csdkLock.lock();
679 ProvisionContext* context = new ProvisionContext(resultCallback);
681 std::lock_guard<std::recursive_mutex> lock(*cLock);
682 result = OCProvisionTrustCertChain(static_cast<void*>(context),
683 type, credId, devPtr,
684 &OCSecureResource::callbackWrapper);
688 oclog() <<"Mutex not found";
689 result = OC_STACK_ERROR;
693 #endif // __WITH_DTLS__ or __WITH_TLS__
695 std::string OCSecureResource::getDeviceID()
697 std::ostringstream deviceId("");
698 char *devID = nullptr;
700 validateSecureResource();
702 if (OC_STACK_OK == ConvertUuidToStr(&(devPtr->doxm->deviceID), &devID))
709 oclog() <<"Can not convert uuid to struuid";
711 return deviceId.str();
714 OCProvisionDev_t* OCSecureResource::getDevPtr() const
719 std::string OCSecureResource::getDevAddr()
721 validateSecureResource();
722 std::string ipAddr(devPtr->endpoint.addr);
726 int OCSecureResource::getDeviceStatus()
728 validateSecureResource();
729 return (int)devPtr->devStatus;
732 bool OCSecureResource::getOwnedStatus()
734 validateSecureResource();
735 return devPtr->doxm->owned;
738 void OCSecureResource::validateSecureResource()
742 throw OCException("Incomplete secure resource", OC_STACK_RESOURCE_ERROR);