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::discoverSecureResource(unsigned short timeout,
125 const std::string& host,
126 OCConnectivityType connType,
129 OCStackResult result;
130 OCProvisionDev_t *pDevList = nullptr, *pCurDev = nullptr, *tmp = nullptr;
131 auto csdkLock = OCPlatform_impl::Instance().csdkLock();
132 auto cLock = csdkLock.lock();
136 std::lock_guard<std::recursive_mutex> lock(*cLock);
137 result = OCDiscoverSecureResource(timeout, host.c_str(), connType, &pDevList);
138 if (result == OC_STACK_OK)
144 list.push_back(std::shared_ptr<OCSecureResource>(
145 new OCSecureResource(csdkLock, pCurDev)));
146 pCurDev = pCurDev->next;
152 oclog() <<"Secure resource discovery failed!";
157 oclog() <<"Mutex not found";
158 result = OC_STACK_ERROR;
164 OCStackResult OCSecure::setOwnerTransferCallbackData(OicSecOxm_t oxm,
165 OTMCallbackData_t* callbackData, InputPinCallback inputPin)
167 if (NULL == callbackData || oxm >= OIC_OXM_COUNT)
169 oclog() <<"Invalid callbackData or OXM type";
170 return OC_STACK_INVALID_PARAM;
173 if ((OIC_RANDOM_DEVICE_PIN == oxm) && !inputPin)
175 oclog() <<"for OXM type DEVICE_PIN, inputPin callback can't be null";
176 return OC_STACK_INVALID_PARAM;
179 OCStackResult result;
180 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
184 std::lock_guard<std::recursive_mutex> lock(*cLock);
185 result = OCSetOwnerTransferCallbackData(oxm, callbackData);
186 if (result == OC_STACK_OK && (OIC_RANDOM_DEVICE_PIN == oxm))
188 SetInputPinCB(inputPin);
193 oclog() <<"Mutex not found";
194 result = OC_STACK_ERROR;
201 OCStackResult OCSecure::getDevInfoFromNetwork(unsigned short timeout,
202 DeviceList_t &ownedDevList,
203 DeviceList_t &unownedDevList)
205 OCStackResult result = OC_STACK_OK;
206 OCProvisionDev_t *owned = nullptr, *unowned = nullptr, *tmp = nullptr, *dev = nullptr;
207 auto csdkLock = OCPlatform_impl::Instance().csdkLock();
208 auto cLock = csdkLock.lock();
212 std::lock_guard<std::recursive_mutex> lock(*cLock);
214 result = OCGetDevInfoFromNetwork(timeout, &owned, &unowned);
216 if (result == OC_STACK_OK)
222 ownedDevList.push_back(std::shared_ptr<OCSecureResource>(
223 new OCSecureResource(csdkLock, dev)));
232 unownedDevList.push_back(std::shared_ptr<OCSecureResource>(
233 new OCSecureResource(csdkLock, dev)));
241 oclog() <<"Mutex not found";
242 result = OC_STACK_ERROR;
248 OCStackResult OCSecure::setDisplayPinCB(GeneratePinCallback displayPin)
252 oclog() <<"displayPin can't be null";
253 return OC_STACK_INVALID_PARAM;
256 OCStackResult result = OC_STACK_OK;
257 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
261 std::lock_guard<std::recursive_mutex> lock(*cLock);
262 SetGeneratePinCB(displayPin);
266 oclog() <<"Mutex not found";
267 result = OC_STACK_ERROR;
273 OCStackResult OCSecure::removeDeviceWithUuid(unsigned short waitTimeForOwnedDeviceDiscovery,
275 ResultCallBack resultCallback)
279 oclog() << "Result calback can't be null";
280 return OC_STACK_INVALID_CALLBACK;
283 OCStackResult result;
284 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
288 ProvisionContext* context = new ProvisionContext(resultCallback);
290 std::lock_guard<std::recursive_mutex> lock(*cLock);
293 result = ConvertStrToUuid(uuid.c_str(), &targetDev);
294 if(OC_STACK_OK == result)
296 result = OCRemoveDeviceWithUuid(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
297 &targetDev, &OCSecureResource::callbackWrapper);
301 oclog() <<"Can not convert struuid to uuid";
306 oclog() <<"Mutex not found";
307 result = OC_STACK_ERROR;
312 #if defined(__WITH_X509__) || defined(__WITH_TLS__)
313 OCStackResult OCSecure::saveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
314 OicEncodingType_t encodingType, uint16_t *credId)
318 oclog() <<"trustCertChain can't be null";
319 return OC_STACK_INVALID_PARAM;
323 oclog() <<"cred ID can not be null";
324 return OC_STACK_INVALID_PARAM;
327 OCStackResult result;
328 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
332 std::lock_guard<std::recursive_mutex> lock(*cLock);
333 result = OCSaveTrustCertChain(trustCertChain, chainSize, encodingType, credId );
337 oclog() <<"Mutex not found";
338 result = OC_STACK_ERROR;
342 #endif // __WITH_X509__ || __WITH_TLS__
344 void OCSecureResource::callbackWrapper(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
346 PMResultList_t *results = nullptr;
347 ProvisionContext* context = static_cast<ProvisionContext*>(ctx);
351 results = new PMResultList_t;
353 catch (std::bad_alloc& e)
355 oclog() <<"Bad alloc exception";
359 for (int i = 0; i < nOfRes; i++)
361 results->push_back(arr[i]);
364 std::thread exec(context->callback, results, hasError);
370 OCSecureResource::OCSecureResource(): m_csdkLock(std::weak_ptr<std::recursive_mutex>()),
375 OCSecureResource::OCSecureResource(std::weak_ptr<std::recursive_mutex> csdkLock,
376 OCProvisionDev_t *dPtr)
377 :m_csdkLock(csdkLock), devPtr(dPtr)
381 OCSecureResource::~OCSecureResource()
385 OCDeleteDiscoveredDevices(devPtr);
389 OCStackResult OCSecureResource::doOwnershipTransfer(ResultCallBack resultCallback)
393 oclog() <<"Result callback can't be null";
394 return OC_STACK_INVALID_CALLBACK;
397 OCStackResult result;
398 auto cLock = m_csdkLock.lock();
402 ProvisionContext* context = new ProvisionContext(resultCallback);
404 std::lock_guard<std::recursive_mutex> lock(*cLock);
405 result = OCDoOwnershipTransfer(static_cast<void*>(context),
406 devPtr, &OCSecureResource::callbackWrapper);
410 oclog() <<"Mutex not found";
411 result = OC_STACK_ERROR;
416 OCStackResult OCSecureResource::provisionACL( const OicSecAcl_t* acl,
417 ResultCallBack resultCallback)
421 oclog() <<"ACL can't be null";
422 return OC_STACK_INVALID_PARAM;
426 oclog() <<"result callback can not be null";
427 return OC_STACK_INVALID_CALLBACK;
430 OCStackResult result;
431 auto cLock = m_csdkLock.lock();
435 ProvisionContext* context = new ProvisionContext(resultCallback);
437 std::lock_guard<std::recursive_mutex> lock(*cLock);
438 result = OCProvisionACL(static_cast<void*>(context),
439 devPtr, const_cast<OicSecAcl_t*>(acl),
440 &OCSecureResource::callbackWrapper);
444 oclog() <<"Mutex not found";
445 result = OC_STACK_ERROR;
450 OCStackResult OCSecureResource::provisionCredentials(const Credential &cred,
451 const OCSecureResource &device2, ResultCallBack resultCallback)
455 oclog() << "Result calback can't be null";
456 return OC_STACK_INVALID_CALLBACK;
459 OCStackResult result;
460 auto cLock = m_csdkLock.lock();
464 ProvisionContext* context = new ProvisionContext(resultCallback);
466 std::lock_guard<std::recursive_mutex> lock(*cLock);
467 result = OCProvisionCredentials(static_cast<void*>(context),
468 cred.getCredentialType(),
469 cred.getCredentialKeySize(),
470 devPtr, device2.getDevPtr(),
471 &OCSecureResource::callbackWrapper);
475 oclog() <<"Mutex not found";
476 result = OC_STACK_ERROR;
481 OCStackResult OCSecureResource::provisionPairwiseDevices(const Credential &cred,
482 const OicSecAcl_t* acl1, const OCSecureResource &device2, const OicSecAcl_t* acl2,
483 ResultCallBack resultCallback)
487 oclog() << "Result callback can not be null";
488 return OC_STACK_INVALID_CALLBACK;
491 OCStackResult result;
492 auto cLock = m_csdkLock.lock();
496 ProvisionContext* context = new ProvisionContext(resultCallback);
498 std::lock_guard<std::recursive_mutex> lock(*cLock);
499 result = OCProvisionPairwiseDevices(static_cast<void*>(context),
500 cred.getCredentialType(),
501 cred.getCredentialKeySize(),
502 devPtr, const_cast<OicSecAcl_t*>(acl1),
503 device2.getDevPtr(), const_cast<OicSecAcl_t*>(acl2),
504 &OCSecureResource::callbackWrapper);
508 oclog() <<"Mutex not found";
509 result = OC_STACK_ERROR;
514 OCStackResult OCSecureResource::unlinkDevices(const OCSecureResource &device2,
515 ResultCallBack resultCallback)
519 oclog() << "Result calback can't be null";
520 return OC_STACK_INVALID_CALLBACK;
523 OCStackResult result;
524 auto cLock = m_csdkLock.lock();
528 ProvisionContext* context = new ProvisionContext(resultCallback);
530 std::lock_guard<std::recursive_mutex> lock(*cLock);
532 result = OCUnlinkDevices(static_cast<void*>(context),
533 devPtr, device2.getDevPtr(), &OCSecureResource::callbackWrapper);
537 oclog() <<"Mutex not found";
538 result = OC_STACK_ERROR;
543 OCStackResult OCSecureResource::removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery,
544 ResultCallBack resultCallback)
548 oclog() << "Result calback can't be null";
549 return OC_STACK_INVALID_CALLBACK;
552 OCStackResult result;
553 auto cLock = m_csdkLock.lock();
557 ProvisionContext* context = new ProvisionContext(resultCallback);
559 std::lock_guard<std::recursive_mutex> lock(*cLock);
561 result = OCRemoveDevice(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
562 devPtr, &OCSecureResource::callbackWrapper);
566 oclog() <<"Mutex not found";
567 result = OC_STACK_ERROR;
572 OCStackResult OCSecureResource::getLinkedDevices(UuidList_t &uuidList)
574 OCStackResult result;
575 size_t numOfDevices = -1;
576 auto devUuid = devPtr->doxm->deviceID;
577 auto cLock = m_csdkLock.lock();
581 std::lock_guard<std::recursive_mutex> lock(*cLock);
583 OCUuidList_t* linkedDevs = nullptr, *tmp = nullptr;
584 result = OCGetLinkedStatus(&devUuid, &linkedDevs, &numOfDevices);
585 if (result == OC_STACK_OK)
587 for (tmp = linkedDevs; tmp; tmp = tmp->next)
589 uuidList.push_back(tmp->dev);
591 OCDeleteUuidList(linkedDevs);
596 oclog() <<"Mutex not found";
597 result = OC_STACK_ERROR;
602 OCStackResult OCSecureResource::provisionDirectPairing( const OicSecPconf_t* pconf,
603 ResultCallBack resultCallback)
607 oclog() <<"PCONF can't be null";
608 return OC_STACK_INVALID_PARAM;
612 oclog() <<"result callback can not be null";
613 return OC_STACK_INVALID_CALLBACK;
616 OCStackResult result;
617 auto cLock = m_csdkLock.lock();
621 ProvisionContext* context = new ProvisionContext(resultCallback);
623 std::lock_guard<std::recursive_mutex> lock(*cLock);
624 result = OCProvisionDirectPairing(static_cast<void*>(context),
625 devPtr, const_cast<OicSecPconf_t*>(pconf),
626 &OCSecureResource::callbackWrapper);
630 oclog() <<"Mutex not found";
631 result = OC_STACK_ERROR;
636 #if defined(__WITH_X509__) || defined(__WITH_TLS__)
637 OCStackResult OCSecureResource::provisionTrustCertChain(OicSecCredType_t type, uint16_t credId,
638 ResultCallBack resultCallback)
640 if (SIGNED_ASYMMETRIC_KEY != type)
642 oclog() <<"Invalid key type";
643 return OC_STACK_INVALID_PARAM;
647 oclog() <<"result callback can not be null";
648 return OC_STACK_INVALID_CALLBACK;
651 OCStackResult result;
652 auto cLock = m_csdkLock.lock();
656 ProvisionContext* context = new ProvisionContext(resultCallback);
658 std::lock_guard<std::recursive_mutex> lock(*cLock);
659 result = OCProvisionTrustCertChain(static_cast<void*>(context),
660 type, credId, devPtr,
661 &OCSecureResource::callbackWrapper);
665 oclog() <<"Mutex not found";
666 result = OC_STACK_ERROR;
670 #endif // __WITH_X509__ || __WITH_TLS__
672 std::string OCSecureResource::getDeviceID()
674 std::ostringstream deviceId("");
675 char *devID = nullptr;
677 validateSecureResource();
679 if (OC_STACK_OK == ConvertUuidToStr(&(devPtr->doxm->deviceID), &devID))
686 oclog() <<"Can not convert uuid to struuid";
688 return deviceId.str();
691 OCProvisionDev_t* OCSecureResource::getDevPtr() const
696 std::string OCSecureResource::getDevAddr()
698 validateSecureResource();
699 std::string ipAddr(devPtr->endpoint.addr);
703 int OCSecureResource::getDeviceStatus()
705 validateSecureResource();
706 return (int)devPtr->devStatus;
709 bool OCSecureResource::getOwnedStatus()
711 validateSecureResource();
712 return devPtr->doxm->owned;
715 void OCSecureResource::validateSecureResource()
719 throw OCException("Incomplete secure resource", OC_STACK_RESOURCE_ERROR);