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 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
312 OCStackResult OCSecure::saveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
313 OicEncodingType_t encodingType, uint16_t *credId)
317 oclog() <<"trustCertChain can't be null";
318 return OC_STACK_INVALID_PARAM;
322 oclog() <<"cred ID can not be null";
323 return OC_STACK_INVALID_PARAM;
326 OCStackResult result;
327 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
331 std::lock_guard<std::recursive_mutex> lock(*cLock);
332 result = OCSaveTrustCertChain(trustCertChain, chainSize, encodingType, credId );
336 oclog() <<"Mutex not found";
337 result = OC_STACK_ERROR;
341 #endif // __WITH_DTLS__ || __WITH_TLS__
343 void OCSecureResource::callbackWrapper(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
345 PMResultList_t *results = nullptr;
346 ProvisionContext* context = static_cast<ProvisionContext*>(ctx);
350 results = new PMResultList_t;
352 catch (std::bad_alloc& e)
354 oclog() <<"Bad alloc exception";
358 for (int i = 0; i < nOfRes; i++)
360 results->push_back(arr[i]);
363 std::thread exec(context->callback, results, hasError);
369 OCSecureResource::OCSecureResource(): m_csdkLock(std::weak_ptr<std::recursive_mutex>()),
374 OCSecureResource::OCSecureResource(std::weak_ptr<std::recursive_mutex> csdkLock,
375 OCProvisionDev_t *dPtr)
376 :m_csdkLock(csdkLock), devPtr(dPtr)
380 OCSecureResource::~OCSecureResource()
384 OCDeleteDiscoveredDevices(devPtr);
388 OCStackResult OCSecureResource::doOwnershipTransfer(ResultCallBack resultCallback)
392 oclog() <<"Result callback can't be null";
393 return OC_STACK_INVALID_CALLBACK;
396 OCStackResult result;
397 auto cLock = m_csdkLock.lock();
401 ProvisionContext* context = new ProvisionContext(resultCallback);
403 std::lock_guard<std::recursive_mutex> lock(*cLock);
404 result = OCDoOwnershipTransfer(static_cast<void*>(context),
405 devPtr, &OCSecureResource::callbackWrapper);
409 oclog() <<"Mutex not found";
410 result = OC_STACK_ERROR;
415 OCStackResult OCSecureResource::provisionACL( const OicSecAcl_t* acl,
416 ResultCallBack resultCallback)
420 oclog() <<"ACL can't be null";
421 return OC_STACK_INVALID_PARAM;
425 oclog() <<"result callback can not be null";
426 return OC_STACK_INVALID_CALLBACK;
429 OCStackResult result;
430 auto cLock = m_csdkLock.lock();
434 ProvisionContext* context = new ProvisionContext(resultCallback);
436 std::lock_guard<std::recursive_mutex> lock(*cLock);
437 result = OCProvisionACL(static_cast<void*>(context),
438 devPtr, const_cast<OicSecAcl_t*>(acl),
439 &OCSecureResource::callbackWrapper);
443 oclog() <<"Mutex not found";
444 result = OC_STACK_ERROR;
449 OCStackResult OCSecureResource::provisionCredentials(const Credential &cred,
450 const OCSecureResource &device2, ResultCallBack resultCallback)
454 oclog() << "Result calback can't be null";
455 return OC_STACK_INVALID_CALLBACK;
458 OCStackResult result;
459 auto cLock = m_csdkLock.lock();
463 ProvisionContext* context = new ProvisionContext(resultCallback);
465 std::lock_guard<std::recursive_mutex> lock(*cLock);
466 result = OCProvisionCredentials(static_cast<void*>(context),
467 cred.getCredentialType(),
468 cred.getCredentialKeySize(),
469 devPtr, device2.getDevPtr(),
470 &OCSecureResource::callbackWrapper);
474 oclog() <<"Mutex not found";
475 result = OC_STACK_ERROR;
480 OCStackResult OCSecureResource::provisionPairwiseDevices(const Credential &cred,
481 const OicSecAcl_t* acl1, const OCSecureResource &device2, const OicSecAcl_t* acl2,
482 ResultCallBack resultCallback)
486 oclog() << "Result callback can not be null";
487 return OC_STACK_INVALID_CALLBACK;
490 OCStackResult result;
491 auto cLock = m_csdkLock.lock();
495 ProvisionContext* context = new ProvisionContext(resultCallback);
497 std::lock_guard<std::recursive_mutex> lock(*cLock);
498 result = OCProvisionPairwiseDevices(static_cast<void*>(context),
499 cred.getCredentialType(),
500 cred.getCredentialKeySize(),
501 devPtr, const_cast<OicSecAcl_t*>(acl1),
502 device2.getDevPtr(), const_cast<OicSecAcl_t*>(acl2),
503 &OCSecureResource::callbackWrapper);
507 oclog() <<"Mutex not found";
508 result = OC_STACK_ERROR;
513 OCStackResult OCSecureResource::unlinkDevices(const OCSecureResource &device2,
514 ResultCallBack resultCallback)
518 oclog() << "Result calback can't be null";
519 return OC_STACK_INVALID_CALLBACK;
522 OCStackResult result;
523 auto cLock = m_csdkLock.lock();
527 ProvisionContext* context = new ProvisionContext(resultCallback);
529 std::lock_guard<std::recursive_mutex> lock(*cLock);
531 result = OCUnlinkDevices(static_cast<void*>(context),
532 devPtr, device2.getDevPtr(), &OCSecureResource::callbackWrapper);
536 oclog() <<"Mutex not found";
537 result = OC_STACK_ERROR;
542 OCStackResult OCSecureResource::removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery,
543 ResultCallBack resultCallback)
547 oclog() << "Result calback can't be null";
548 return OC_STACK_INVALID_CALLBACK;
551 OCStackResult result;
552 auto cLock = m_csdkLock.lock();
556 ProvisionContext* context = new ProvisionContext(resultCallback);
558 std::lock_guard<std::recursive_mutex> lock(*cLock);
560 result = OCRemoveDevice(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
561 devPtr, &OCSecureResource::callbackWrapper);
565 oclog() <<"Mutex not found";
566 result = OC_STACK_ERROR;
571 OCStackResult OCSecureResource::getLinkedDevices(UuidList_t &uuidList)
573 OCStackResult result;
574 size_t numOfDevices = -1;
575 auto devUuid = devPtr->doxm->deviceID;
576 auto cLock = m_csdkLock.lock();
580 std::lock_guard<std::recursive_mutex> lock(*cLock);
582 OCUuidList_t* linkedDevs = nullptr, *tmp = nullptr;
583 result = OCGetLinkedStatus(&devUuid, &linkedDevs, &numOfDevices);
584 if (result == OC_STACK_OK)
586 for (tmp = linkedDevs; tmp; tmp = tmp->next)
588 uuidList.push_back(tmp->dev);
590 OCDeleteUuidList(linkedDevs);
595 oclog() <<"Mutex not found";
596 result = OC_STACK_ERROR;
601 OCStackResult OCSecureResource::provisionDirectPairing( const OicSecPconf_t* pconf,
602 ResultCallBack resultCallback)
606 oclog() <<"PCONF can't be null";
607 return OC_STACK_INVALID_PARAM;
611 oclog() <<"result callback can not be null";
612 return OC_STACK_INVALID_CALLBACK;
615 OCStackResult result;
616 auto cLock = m_csdkLock.lock();
620 ProvisionContext* context = new ProvisionContext(resultCallback);
622 std::lock_guard<std::recursive_mutex> lock(*cLock);
623 result = OCProvisionDirectPairing(static_cast<void*>(context),
624 devPtr, const_cast<OicSecPconf_t*>(pconf),
625 &OCSecureResource::callbackWrapper);
629 oclog() <<"Mutex not found";
630 result = OC_STACK_ERROR;
635 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
636 OCStackResult OCSecureResource::provisionTrustCertChain(OicSecCredType_t type, uint16_t credId,
637 ResultCallBack resultCallback)
639 if (SIGNED_ASYMMETRIC_KEY != type)
641 oclog() <<"Invalid key type";
642 return OC_STACK_INVALID_PARAM;
646 oclog() <<"result callback can not be null";
647 return OC_STACK_INVALID_CALLBACK;
650 OCStackResult result;
651 auto cLock = m_csdkLock.lock();
655 ProvisionContext* context = new ProvisionContext(resultCallback);
657 std::lock_guard<std::recursive_mutex> lock(*cLock);
658 result = OCProvisionTrustCertChain(static_cast<void*>(context),
659 type, credId, devPtr,
660 &OCSecureResource::callbackWrapper);
664 oclog() <<"Mutex not found";
665 result = OC_STACK_ERROR;
669 #endif // __WITH_DTLS__ or __WITH_TLS__
671 std::string OCSecureResource::getDeviceID()
673 std::ostringstream deviceId("");
674 char *devID = nullptr;
676 validateSecureResource();
678 if (OC_STACK_OK == ConvertUuidToStr(&(devPtr->doxm->deviceID), &devID))
685 oclog() <<"Can not convert uuid to struuid";
687 return deviceId.str();
690 OCProvisionDev_t* OCSecureResource::getDevPtr() const
695 std::string OCSecureResource::getDevAddr()
697 validateSecureResource();
698 std::string ipAddr(devPtr->endpoint.addr);
702 int OCSecureResource::getDeviceStatus()
704 validateSecureResource();
705 return (int)devPtr->devStatus;
708 bool OCSecureResource::getOwnedStatus()
710 validateSecureResource();
711 return devPtr->doxm->owned;
714 void OCSecureResource::validateSecureResource()
718 throw OCException("Incomplete secure resource", OC_STACK_RESOURCE_ERROR);