1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 #include "EnrolleeSecurity.h"
24 #include "oxmjustworks.h"
25 #include "oxmrandompin.h"
26 #include "EnrolleeResource.h"
28 #include "ESException.h"
29 #include "oic_malloc.h"
30 #include "provisioningdatabasemanager.h"
31 #include "oic_string.h"
33 #include "srmutility.h"
39 #define MAX_PERMISSION_LENGTH (5)
46 #define DISCOVERY_TIMEOUT (10)
48 //TODO : Currently discovery timeout for owned and unowned devices is fixed as 5
49 // The value should be accepted from the application as a parameter during ocplatform
51 #define ES_SEC_DISCOVERY_TIMEOUT 5
53 EnrolleeSecurity::EnrolleeSecurity(
54 std::shared_ptr< OC::OCResource > resource,
55 const std::string secDbPath)
58 m_ocResource = resource;
61 void EnrolleeSecurity::registerCallbackHandler(
62 const SecurityProvStatusCb securityProvStatusCb,
63 const SecurityPinCb securityPinCb,
64 const SecProvisioningDbPathCb secProvisioningDbPathCb)
66 m_securityProvStatusCb = securityProvStatusCb;
67 m_securityPinCb = securityPinCb;
68 m_secProvisioningDbPathCb = secProvisioningDbPathCb;
71 std::shared_ptr< OC::OCSecureResource > EnrolleeSecurity::findEnrolleeSecurityResource(
74 for (unsigned int i = 0; i < list.size(); i++)
76 if(m_ocResource->sid() == list[i]->getDeviceID().c_str())
78 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Device %d ID %s ", i + 1,
79 list[i]->getDeviceID().c_str());
80 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "From IP :%s",
81 list[i]->getDevAddr().c_str());
85 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG,"Error!!! DeviceList_t is NULL");
89 void EnrolleeSecurity::convertUUIDToString(const uint8_t uuid[UUID_SIZE],
90 std::string& uuidString)
92 char uuidArray[UUID_STRING_SIZE] = {'\0',};
93 int ret = snprintf(uuidArray, UUID_STRING_SIZE,
94 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
95 uuid[0], uuid[1], uuid[2], uuid[3],
96 uuid[4], uuid[5], uuid[6], uuid[7],
97 uuid[8], uuid[9], uuid[10], uuid[11],
98 uuid[12], uuid[13], uuid[14], uuid[15]
101 if (ret != UUID_STRING_SIZE - 1)
106 uuidString = uuidArray;
109 void EnrolleeSecurity::ownershipTransferCb(OC::PMResultList_t *result, int hasError)
113 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG,"Error!!! in OwnershipTransfer");
116 convertUUIDToString(result->at(0).deviceId.id, uuid);
117 std::shared_ptr< SecProvisioningStatus > securityProvisioningStatus =
118 std::make_shared< SecProvisioningStatus >(uuid, ES_ERROR);
119 m_securityProvStatusCb(securityProvisioningStatus);
124 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "ownershipTransferCb : Received provisioning results: ");
125 for (unsigned int i = 0; i < result->size(); i++)
127 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Result is = %d for device",result->at(i).res);
129 convertUUIDToString(result->at(0).deviceId.id, uuid);
131 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "UUID : %s",uuid.c_str());
132 std::shared_ptr< SecProvisioningStatus > securityProvisioningStatus =
133 std::make_shared< SecProvisioningStatus >(uuid, ES_OK);
134 m_securityProvStatusCb(securityProvisioningStatus);
142 void EnrolleeSecurity::provisionOwnership()
144 OC::DeviceList_t pUnownedDevList, pOwnedDevList;
146 pOwnedDevList.clear();
147 pUnownedDevList.clear();
149 OCStackResult result = OC_STACK_ERROR;
151 result = OCSecure::discoverOwnedDevices(ES_SEC_DISCOVERY_TIMEOUT,
153 if (result != OC_STACK_OK)
155 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "Owned Discovery failed.");
157 throw ESPlatformException(result);
159 else if (pOwnedDevList.size())
161 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Found owned devices. Count =%d",
162 pOwnedDevList.size());
163 std::shared_ptr< OC::OCSecureResource > ownedDevice =
164 findEnrolleeSecurityResource(pOwnedDevList);
168 std::shared_ptr< SecProvisioningStatus > securityProvisioningStatus =
169 std::make_shared< SecProvisioningStatus >(ownedDevice->getDeviceID(), ES_OK);
170 m_securityProvStatusCb(securityProvisioningStatus);
175 result = OCSecure::discoverUnownedDevices(ES_SEC_DISCOVERY_TIMEOUT, pUnownedDevList);
176 if (result != OC_STACK_OK)
178 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "UnOwned Discovery failed.");
180 throw ESPlatformException(result);
182 else if (pUnownedDevList.size())
184 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Found Unowned devices. Count =%d",
185 pUnownedDevList.size());
187 m_unownedDevice = findEnrolleeSecurityResource(pUnownedDevList);
190 if(isOwnedDeviceRegisteredInSVRDB())
192 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG,
193 "Found Unowned device's DevID at DB of ownedDevices list");
195 OC::ResultCallBack removeDeviceWithUuidCB = std::bind(
196 &EnrolleeSecurity::removeDeviceWithUuidCB,
197 this, std::placeholders::_1, std::placeholders::_2);
199 OCSecure::removeDeviceWithUuid(DISCOVERY_TIMEOUT,
201 removeDeviceWithUuidCB);
205 performOwnershipTransfer();
210 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "No matched unowned devices found.");
211 throw ESException("No matched unowned devices found.");
216 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "No unowned devices found.");
217 throw ESException("No unowned devices found.");
221 void EnrolleeSecurity::performOwnershipTransfer()
223 OCStackResult result = OC_STACK_ERROR;
225 OTMCallbackData_t justWorksCBData;
226 justWorksCBData.loadSecretCB = LoadSecretJustWorksCallback;
227 justWorksCBData.createSecureSessionCB = CreateSecureSessionJustWorksCallback;
228 justWorksCBData.createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload;
229 justWorksCBData.createOwnerTransferPayloadCB =
230 CreateJustWorksOwnerTransferPayload;
231 OCSecure::setOwnerTransferCallbackData(OIC_JUST_WORKS, &justWorksCBData, NULL);
233 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Transfering ownership for : %s ",
234 m_unownedDevice->getDeviceID().c_str());
236 OC::ResultCallBack ownershipTransferCb = std::bind(
237 &EnrolleeSecurity::ownershipTransferCb, this, std::placeholders::_1,
238 std::placeholders::_2);
240 result = m_unownedDevice->doOwnershipTransfer(ownershipTransferCb);
241 if (result != OC_STACK_OK)
243 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "doOwnershipTransfer is failed");
244 throw ESPlatformException(result);
248 void EnrolleeSecurity::removeDeviceWithUuidCB(OC::PMResultList_t *result, int hasError)
252 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Error in removeDeviceWithUuid operation!");
253 throw ESException("removeDeviceWithUuid Error");
258 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Received provisioning results: ");
260 for (unsigned int i = 0; i < result->size(); i++)
263 convertUUIDToString(result->at(i).deviceId.id, uuid);
265 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG,
266 "Result is = %d for device %s", result->at(i).res, uuid.c_str());
268 performOwnershipTransfer();
272 bool EnrolleeSecurity::isOwnedDeviceRegisteredInSVRDB()
274 OCStackResult res = OC_STACK_ERROR;
276 OCUuidList_t *uuidList = NULL;
277 size_t numOfDevices = 0;
279 res = PDMGetOwnedDevices(&uuidList, &numOfDevices);
280 if (OC_STACK_OK != res)
282 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "Error while getting info from DB");
286 OCUuidList_t *pUuidList = uuidList;
291 convertUUIDToString(pUuidList->dev.id, uuid);
292 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG,
293 "m_ocResource->sid(): %s, cur DB UUID %s",
294 m_ocResource->sid().c_str(), uuid.c_str());
295 if(m_ocResource->sid() == uuid.c_str())
299 pUuidList = pUuidList->next;
305 std::string EnrolleeSecurity::getUUID() const
307 return m_ocResource->sid();
310 #if defined(__WITH_DTLS__) && defined(__WITH_TLS__)
311 void EnrolleeSecurity::provisionSecurityForCloudServer(
312 std::string cloudUuid, int credId)
314 // Need to discover Owned device in a given network, again
315 OC::DeviceList_t pOwnedDevList;
316 std::shared_ptr< OC::OCSecureResource > ownedDevice = NULL;
318 pOwnedDevList.clear();
320 OCStackResult result;
322 result = OCSecure::discoverOwnedDevices(ES_SEC_DISCOVERY_TIMEOUT,
324 if (result != OC_STACK_OK)
326 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "Owned Discovery failed.");
328 throw ESPlatformException(result);
330 else if (pOwnedDevList.size())
332 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Found owned devices. Count =%d",
333 pOwnedDevList.size());
334 ownedDevice = findEnrolleeSecurityResource(pOwnedDevList);
338 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Not found matched owned device.");
339 throw ESException("Not found matched owned device.");
344 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Not found owned devices.");
345 throw ESException("Not found owned devices.");
348 if(!cloudUuid.empty()
349 && performACLProvisioningForCloudServer(ownedDevice, cloudUuid) != ESResult::ES_OK)
351 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "error performACLProvisioningForCloudServer");
352 throw ESException("error performACLProvisioningForCloudServer");
356 && performCertProvisioningForCloudServer(ownedDevice, credId) != ESResult::ES_OK)
358 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "error performCertProvisioningForCloudServer");
359 throw ESException("error performCertProvisioningForCloudServer");
363 ESResult EnrolleeSecurity::performCertProvisioningForCloudServer(
364 std::shared_ptr< OC::OCSecureResource > ownedDevice, int credId)
366 ESResult res = ESResult::ES_ERROR;
370 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Invalid param");
373 OC::ResultCallBack CertProvisioningCb = std::bind(
374 &EnrolleeSecurity::CertProvisioningCb, this, std::placeholders::_1,
375 std::placeholders::_2);
376 OCStackResult rst = ownedDevice->provisionTrustCertChain(SIGNED_ASYMMETRIC_KEY,
377 static_cast<uint16_t>(credId),
379 if(OC_STACK_OK != rst)
381 OIC_LOG_V(ERROR, ENROLEE_SECURITY_TAG, "provisionTrustCertChain error: %d", rst);
385 std::unique_lock<std::mutex> lck(m_mtx);
386 m_cond.wait_for(lck, std::chrono::seconds(ES_SEC_DISCOVERY_TIMEOUT));
390 res = ESResult::ES_OK;
396 ESResult EnrolleeSecurity::performACLProvisioningForCloudServer(
397 std::shared_ptr< OC::OCSecureResource > ownedDevice, std::string& cloudUuid)
399 ESResult res = ESResult::ES_ERROR;
403 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Invalid param");
406 if(cloudUuid.empty())
408 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Invalid param");
413 ConvertStrToUuid(cloudUuid.c_str(), &uuid);
415 // Create Acl for Cloud Server to be provisioned to Enrollee
416 OicSecAcl_t* acl = createAcl(uuid);
419 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "createAcl error return");
423 OC::ResultCallBack aclProvisioningCb = std::bind(
424 &EnrolleeSecurity::ACLProvisioningCb, this, std::placeholders::_1,
425 std::placeholders::_2);
426 // ACL provisioning to Enrollee
427 OCStackResult rst = ownedDevice->provisionACL(acl, aclProvisioningCb);
428 if(OC_STACK_OK != rst)
430 OIC_LOG_V(ERROR, ENROLEE_SECURITY_TAG, "OCProvisionACL API error: %d", rst);
434 std::unique_lock<std::mutex> lck(m_mtx);
435 m_cond.wait_for(lck, std::chrono::seconds(ES_SEC_DISCOVERY_TIMEOUT));
439 res = ESResult::ES_OK;
445 OicSecAcl_t* EnrolleeSecurity::createAcl(const OicUuid_t cloudUuid)
447 // allocate memory for |acl| struct
448 OicSecAcl_t* acl = (OicSecAcl_t*) OICCalloc(1, sizeof(OicSecAcl_t));
451 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "createAcl: OICCalloc error return");
452 return NULL; // not need to 'goto' |ERROR| before allocating |acl|
454 OicSecAce_t* ace = (OicSecAce_t*) OICCalloc(1, sizeof(OicSecAce_t));
457 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "createAcl: OICCalloc error return");
458 return NULL; // not need to 'goto' |ERROR| before allocating |acl|
460 LL_APPEND(acl->aces, ace);
462 memcpy(&ace->subjectuuid, &cloudUuid, UUID_LENGTH);
464 OicSecRsrc_t* rsrc = (OicSecRsrc_t*)OICCalloc(1, sizeof(OicSecRsrc_t));
467 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "createAcl: OICCalloc error return");
468 OCDeleteACLList(acl);
473 size_t len = strlen(href)+1; // '1' for null termination
474 rsrc->href = (char*) OICCalloc(len, sizeof(char));
477 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "createAcl: OICCalloc error return");
478 OCDeleteACLList(acl);
481 OICStrcpy(rsrc->href, len, href);
484 rsrc->typeLen = arrLen;
485 rsrc->types = (char**)OICCalloc(arrLen, sizeof(char*));
486 rsrc->interfaceLen = 1;
487 rsrc->interfaces = (char**)OICCalloc(arrLen, sizeof(char*));
488 rsrc->types[0] = OICStrdup("rt"); // ignore
489 rsrc->interfaces[0] = OICStrdup("if"); // ignore
491 LL_APPEND(ace->resources, rsrc);
493 ace->permission = 31; // R/W/U/D
498 void EnrolleeSecurity::ACLProvisioningCb(PMResultList_t *result, int hasError)
502 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Error in ACL provisioning operation!");
507 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Received ACL provisioning results: ");
510 for (unsigned int i = 0; i < result->size(); i++)
512 convertUUIDToString(result->at(i).deviceId.id, devUuid);
513 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Result is = %d for device %s",
514 result->at(i).res, devUuid.c_str());
522 void EnrolleeSecurity::CertProvisioningCb(PMResultList_t *result, int hasError)
526 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Error in Cert. provisioning operation!");
531 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Received Cert. provisioning results: ");
534 for (unsigned int i = 0; i < result->size(); i++)
536 convertUUIDToString(result->at(i).deviceId.id, devUuid);
537 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Result is = %d for device %s",
538 result->at(i).res, devUuid.c_str());
545 #endif //defined(__WITH_DTLS__) && defined(__WITH_TLS__)