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::convertUUIDToString(const uint8_t uuid[UUID_SIZE],
62 std::string& uuidString)
64 char uuidArray[UUID_STRING_SIZE] = {'\0',};
65 int ret = snprintf(uuidArray, UUID_STRING_SIZE,
66 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
67 uuid[0], uuid[1], uuid[2], uuid[3],
68 uuid[4], uuid[5], uuid[6], uuid[7],
69 uuid[8], uuid[9], uuid[10], uuid[11],
70 uuid[12], uuid[13], uuid[14], uuid[15]
73 if (ret != UUID_STRING_SIZE - 1)
78 uuidString = uuidArray;
81 void EnrolleeSecurity::ownershipTransferCb(OC::PMResultList_t *result, int hasError)
85 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG,"Error!!! in OwnershipTransfer");
90 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "ownershipTransferCb : Received provisioning results: ");
91 for (unsigned int i = 0; i < result->size(); i++)
93 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Result is = %d for device",result->at(i).res);
101 ESResult EnrolleeSecurity::provisionOwnership()
103 ESResult res = ESResult::ES_ERROR;
105 OCStackResult result = OC_STACK_ERROR;
107 ConvertStrToUuid(m_ocResource->sid().c_str(), &uuid);
109 result = OCSecure::discoverSingleDevice(ES_SEC_DISCOVERY_TIMEOUT,
112 if (result != OC_STACK_OK)
114 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "Secure Resource Discovery failed.");
115 res = ESResult:: ES_SECURE_RESOURCE_DISCOVERY_FAILURE;
118 else if (m_securedResource)
120 if (m_securedResource->getOwnedStatus()) // owned check logic
122 if(isOwnedDeviceRegisteredInSVRDB())
124 res = ESResult::ES_OK;
128 res = ESResult::ES_ERROR;
132 else // unowned check logic
134 if(isOwnedDeviceRegisteredInSVRDB())
136 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG,
137 "Found Unowned device's DevID at DB of ownedDevices list");
139 OC::ResultCallBack removeDeviceWithUuidCB = std::bind(
140 &EnrolleeSecurity::removeDeviceWithUuidCB,
141 this, std::placeholders::_1, std::placeholders::_2);
143 result = OCSecure::removeDeviceWithUuid(ES_SEC_DISCOVERY_TIMEOUT,
145 removeDeviceWithUuidCB);
146 if(result != OC_STACK_OK)
148 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "removeDeviceWithUuid failed.");
149 res = ESResult::ES_OWNERSHIP_TRANSFER_FAILURE;
153 std::unique_lock<std::mutex> lck(m_mtx);
154 m_cond.wait_for(lck, std::chrono::seconds(ES_SEC_DISCOVERY_TIMEOUT));
156 if(!removeDeviceResult)
158 res = ESResult::ES_OWNERSHIP_TRANSFER_FAILURE;
163 res = performOwnershipTransfer();
165 if(res != ESResult::ES_OK)
167 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "Ownership-Transfer failed.");
168 res = ESResult::ES_OWNERSHIP_TRANSFER_FAILURE;
172 std::unique_lock<std::mutex> lck(m_mtx);
177 res = ESResult::ES_OWNERSHIP_TRANSFER_FAILURE;
183 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "No secure resource found.");
184 res = ESResult:: ES_SECURE_RESOURCE_DISCOVERY_FAILURE;
189 ESResult EnrolleeSecurity::performOwnershipTransfer()
191 OCStackResult result = OC_STACK_ERROR;
193 OTMCallbackData_t justWorksCBData;
194 justWorksCBData.loadSecretCB = LoadSecretJustWorksCallback;
195 justWorksCBData.createSecureSessionCB = CreateSecureSessionJustWorksCallback;
196 justWorksCBData.createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload;
197 justWorksCBData.createOwnerTransferPayloadCB =
198 CreateJustWorksOwnerTransferPayload;
199 OCSecure::setOwnerTransferCallbackData(OIC_JUST_WORKS, &justWorksCBData, NULL);
201 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Transfering ownership for : %s ",
202 m_securedResource->getDeviceID().c_str());
204 OC::ResultCallBack ownershipTransferCb = std::bind(
205 &EnrolleeSecurity::ownershipTransferCb, this, std::placeholders::_1,
206 std::placeholders::_2);
208 result = m_securedResource->doOwnershipTransfer(ownershipTransferCb);
209 if (result != OC_STACK_OK)
211 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "doOwnershipTransfer is failed");
212 return ESResult::ES_ERROR;
214 return ESResult::ES_OK;
217 void EnrolleeSecurity::removeDeviceWithUuidCB(OC::PMResultList_t *result, int hasError)
221 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Error in removeDeviceWithUuid operation!");
222 removeDeviceResult = false;
227 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Received provisioning results: ");
229 for (unsigned int i = 0; i < result->size(); i++)
232 convertUUIDToString(result->at(i).deviceId.id, uuid);
234 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG,
235 "Result is = %d for device %s", result->at(i).res, uuid.c_str());
237 removeDeviceResult = true;
242 bool EnrolleeSecurity::isOwnedDeviceRegisteredInSVRDB()
244 OCStackResult res = OC_STACK_ERROR;
246 OCUuidList_t *uuidList = NULL;
247 size_t numOfDevices = 0;
249 res = PDMGetOwnedDevices(&uuidList, &numOfDevices);
250 if (OC_STACK_OK != res)
252 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "Error while getting info from DB");
256 OCUuidList_t *pUuidList = uuidList;
261 convertUUIDToString(pUuidList->dev.id, uuid);
262 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG,
263 "m_ocResource->sid(): %s, cur DB UUID %s",
264 m_ocResource->sid().c_str(), uuid.c_str());
265 if(m_ocResource->sid() == uuid.c_str())
269 pUuidList = pUuidList->next;
275 std::string EnrolleeSecurity::getUUID() const
277 return m_ocResource->sid();
280 #if defined(__WITH_DTLS__) && defined(__WITH_TLS__)
281 ESResult EnrolleeSecurity::provisionSecurityForCloudServer(
282 std::string cloudUuid, int credId)
284 ESResult res = ESResult::ES_ERROR;
286 // Need to discover Owned device in a given network, again
287 std::shared_ptr< OC::OCSecureResource > ownedDevice = NULL;
289 OCStackResult result;
291 ConvertStrToUuid(m_ocResource->sid().c_str(), &uuid);
293 result = OCSecure::discoverSingleDevice(ES_SEC_DISCOVERY_TIMEOUT,
296 if (result != OC_STACK_OK)
298 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "secureResource Discovery failed.");
299 res = ESResult::ES_SECURE_RESOURCE_DISCOVERY_FAILURE;
302 else if (ownedDevice)
304 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Found secureResource.");
306 if (ownedDevice->getOwnedStatus())
308 if(!isOwnedDeviceRegisteredInSVRDB())
310 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG,
311 "Not found matched owned deivce in SVR DB.");
312 res = ESResult::ES_SECURE_RESOURCE_DISCOVERY_FAILURE;
318 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Target Enrollee is unowned.");
319 res = ESResult::ES_SECURE_RESOURCE_DISCOVERY_FAILURE;
325 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Not found secureResource.");
326 res = ESResult::ES_SECURE_RESOURCE_DISCOVERY_FAILURE;
330 if(cloudUuid.empty())
332 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG,
333 "ACL provisioning is skipped due to empty UUID of cloud server");
337 res = performACLProvisioningForCloudServer(ownedDevice, cloudUuid);
338 if(res != ESResult::ES_OK)
340 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "error performACLProvisioningForCloudServer");
347 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG,
348 "Cert. provisioning is skipped due to wrong cred ID (<1)");
352 res = performCertProvisioningForCloudServer(ownedDevice, credId);
353 if(res != ESResult::ES_OK)
355 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "error performCertProvisioningForCloudServer");
363 ESResult EnrolleeSecurity::performCertProvisioningForCloudServer(
364 std::shared_ptr< OC::OCSecureResource > ownedDevice, int credId)
366 ESResult res = ESResult::ES_CERT_PROVISIONING_FAILURE;
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);
390 res = ESResult::ES_OK;
396 ESResult EnrolleeSecurity::performACLProvisioningForCloudServer(
397 std::shared_ptr< OC::OCSecureResource > ownedDevice, std::string& cloudUuid)
399 ESResult res = ESResult::ES_ACL_PROVISIONING_FAILURE;
403 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Invalid param");
408 ConvertStrToUuid(cloudUuid.c_str(), &uuid);
410 // Create Acl for Cloud Server to be provisioned to Enrollee
411 OicSecAcl_t* acl = createAcl(uuid);
414 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "createAcl error return");
418 OC::ResultCallBack aclProvisioningCb = std::bind(
419 &EnrolleeSecurity::ACLProvisioningCb, this, std::placeholders::_1,
420 std::placeholders::_2);
421 // ACL provisioning to Enrollee
422 OCStackResult rst = ownedDevice->provisionACL(acl, aclProvisioningCb);
423 if(OC_STACK_OK != rst)
425 OIC_LOG_V(ERROR, ENROLEE_SECURITY_TAG, "OCProvisionACL API error: %d", rst);
429 std::unique_lock<std::mutex> lck(m_mtx);
434 res = ESResult::ES_OK;
440 OicSecAcl_t* EnrolleeSecurity::createAcl(const OicUuid_t cloudUuid)
442 // allocate memory for |acl| struct
443 OicSecAcl_t* acl = (OicSecAcl_t*) OICCalloc(1, sizeof(OicSecAcl_t));
446 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "createAcl: OICCalloc error return");
447 return NULL; // not need to 'goto' |ERROR| before allocating |acl|
449 OicSecAce_t* ace = (OicSecAce_t*) OICCalloc(1, sizeof(OicSecAce_t));
452 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "createAcl: OICCalloc error return");
453 return NULL; // not need to 'goto' |ERROR| before allocating |acl|
455 LL_APPEND(acl->aces, ace);
457 memcpy(&ace->subjectuuid, &cloudUuid, UUID_LENGTH);
459 OicSecRsrc_t* rsrc = (OicSecRsrc_t*)OICCalloc(1, sizeof(OicSecRsrc_t));
462 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "createAcl: OICCalloc error return");
463 OCDeleteACLList(acl);
468 size_t len = strlen(href)+1; // '1' for null termination
469 rsrc->href = (char*) OICCalloc(len, sizeof(char));
472 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "createAcl: OICCalloc error return");
473 OCDeleteACLList(acl);
476 OICStrcpy(rsrc->href, len, href);
479 rsrc->typeLen = arrLen;
480 rsrc->types = (char**)OICCalloc(arrLen, sizeof(char*));
481 rsrc->interfaceLen = 1;
482 rsrc->interfaces = (char**)OICCalloc(arrLen, sizeof(char*));
483 rsrc->types[0] = OICStrdup("rt"); // ignore
484 rsrc->interfaces[0] = OICStrdup("if"); // ignore
486 LL_APPEND(ace->resources, rsrc);
488 ace->permission = 31; // R/W/U/D
493 void EnrolleeSecurity::ACLProvisioningCb(PMResultList_t *result, int hasError)
497 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Error in ACL provisioning operation!");
502 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Received ACL provisioning results: ");
505 for (unsigned int i = 0; i < result->size(); i++)
507 convertUUIDToString(result->at(i).deviceId.id, devUuid);
508 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Result is = %d for device %s",
509 result->at(i).res, devUuid.c_str());
517 void EnrolleeSecurity::CertProvisioningCb(PMResultList_t *result, int hasError)
521 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Error in Cert. provisioning operation!");
526 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Received Cert. provisioning results: ");
529 for (unsigned int i = 0; i < result->size(); i++)
531 convertUUIDToString(result->at(i).deviceId.id, devUuid);
532 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Result is = %d for device %s",
533 result->at(i).res, devUuid.c_str());
540 #endif //defined(__WITH_DTLS__) && defined(__WITH_TLS__)