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 * *****************************************************************/
23 #include "ocprovisioningmanager.h"
24 #include "pmutility.h"
25 #include "ownershiptransfermanager.h"
26 #include "oic_malloc.h"
28 #include "secureresourceprovider.h"
29 #include "provisioningdatabasemanager.h"
30 #include "credresource.h"
32 #include "aclresource.h" //Note: SRM internal header
36 typedef struct Linkdata Linkdata_t;
40 const OCProvisionDev_t *pDev1;
41 OicSecAcl_t *pDev1Acl;
42 const OCProvisionDev_t *pDev2;
43 OicSecAcl_t *pDev2Acl;
44 OCProvisionResult_t *resArr;
46 int currentCountResults;
47 OCProvisionResultCB resultCallback;
52 * The function is responsible for initializaton of the provisioning manager. It will load
53 * provisioning database which have owned device's list and their linked status.
54 * TODO: In addition, if there is a device(s) which has not up-to-date credentials, this function will
55 * automatically try to update the deivce(s).
57 * @param[in] dbPath file path of the sqlite3 db
59 * @return OC_STACK_OK in case of success and other value otherwise.
61 OCStackResult OCInitPM(const char* dbPath)
63 return PDMInit(dbPath);
67 * The function is responsible for discovery of device is current subnet. It will list
68 * all the device in subnet which are not yet owned. Please call OCInit with OC_CLIENT_SERVER as
71 * @param[in] timeout Timeout in seconds, value till which function will listen to responses from
72 * client before returning the list of devices.
73 * @param[out] ppList List of candidate devices to be provisioned
74 * @return OTM_SUCCESS in case of success and other value otherwise.
76 OCStackResult OCDiscoverUnownedDevices(unsigned short timeout, OCProvisionDev_t **ppList)
78 if( ppList == NULL || *ppList != NULL)
80 return OC_STACK_INVALID_PARAM;
83 return PMDeviceDiscovery(timeout, false, ppList);
87 * The function is responsible for discovery of owned device is current subnet. It will list
88 * all the device in subnet which are owned by calling provisioning client.
90 * @param[in] timeout Timeout in seconds, value till which function will listen to responses from
91 * client before returning the list of devices.
92 * @param[out] ppList List of device owned by provisioning tool.
93 * @return OTM_SUCCESS in case of success and other value otherwise.
95 OCStackResult OCDiscoverOwnedDevices(unsigned short timeout, OCProvisionDev_t **ppList)
97 if( ppList == NULL || *ppList != NULL)
99 return OC_STACK_INVALID_PARAM;
102 return PMDeviceDiscovery(timeout, true, ppList);
106 * API to register for particular OxM.
108 * @param[in] Ownership transfer method.
109 * @param[in] Implementation of callback functions for owership transfer.
110 * @return OC_STACK_OK in case of success and other value otherwise.
112 OCStackResult OCSetOwnerTransferCallbackData(OicSecOxm_t oxm, OTMCallbackData_t* callbackData)
114 if(NULL == callbackData)
116 return OC_STACK_INVALID_PARAM;
119 return OTMSetOwnershipTransferCallbackData(oxm, callbackData);
122 OCStackResult OCDoOwnershipTransfer(void* ctx,
123 OCProvisionDev_t *targetDevices,
124 OCProvisionResultCB resultCallback)
126 if( NULL == targetDevices )
128 return OC_STACK_INVALID_PARAM;
131 return OTMDoOwnershipTransfer(ctx, targetDevices, resultCallback);
135 * This function deletes memory allocated to linked list created by OCDiscover_XXX_Devices API.
137 * @param[in] pList Pointer to OCProvisionDev_t which should be deleted.
139 void OCDeleteDiscoveredDevices(OCProvisionDev_t *pList)
141 PMDeleteDeviceList(pList);
145 * this function sends ACL information to resource.
147 * @param[in] ctx Application context would be returned in result callback.
148 * @param[in] selectedDeviceInfo Selected target device.
149 * @param[in] acl ACL to provision.
150 * @param[in] resultCallback callback provided by API user, callback will be called when provisioning
151 request recieves a response from resource server.
152 * @return OC_STACK_OK in case of success and other value otherwise.
154 OCStackResult OCProvisionACL(void* ctx, const OCProvisionDev_t *selectedDeviceInfo, OicSecAcl_t *acl,
155 OCProvisionResultCB resultCallback)
157 return SRPProvisionACL(ctx, selectedDeviceInfo, acl, resultCallback);
161 * function to provision credential to devices.
163 * @param[in] ctx Application context would be returned in result callback.
164 * @param[in] type Type of credentials to be provisioned to the device.
165 * @param[in] pDev1 Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
166 @param[in] pDev2 Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
167 * @param[in] resultCallback callback provided by API user, callback will be called when
168 * provisioning request recieves a response from first resource server.
169 * @return OC_STACK_OK in case of success and other value otherwise.
171 OCStackResult OCProvisionCredentials(void *ctx, OicSecCredType_t type, size_t keySize,
172 const OCProvisionDev_t *pDev1,
173 const OCProvisionDev_t *pDev2,
174 OCProvisionResultCB resultCallback)
176 return SRPProvisionCredentials(ctx, type, keySize,
177 pDev1, pDev2, resultCallback);
182 * Function to unlink devices.
183 * This function will remove the credential & relationship between the two devices.
185 * @param[in] ctx Application context would be returned in result callback
186 * @param[in] pTargetDev1 first device information to be unlinked.
187 * @param[in] pTargetDev2 second device information to be unlinked.
188 * @param[in] resultCallback callback provided by API user, callback will be called when
189 * device unlink is finished.
190 * @return OC_STACK_OK in case of success and other value otherwise.
192 OCStackResult OCUnlinkDevices(void* ctx,
193 const OCProvisionDev_t* pTargetDev1,
194 const OCProvisionDev_t* pTargetDev2,
195 OCProvisionResultCB resultCallback)
197 OC_LOG(INFO, TAG, "IN OCUnlinkDevices");
198 OCUuidList_t* idList = NULL;
201 if (!pTargetDev1 || !pTargetDev2 || !resultCallback)
203 OC_LOG(ERROR, TAG, "OCUnlinkDevices : NULL parameters");
204 return OC_STACK_INVALID_PARAM;
207 // Get linked devices with the first device.
208 OCStackResult res = PDMGetLinkedDevices(&(pTargetDev1->doxm->deviceID), &idList, &numOfDev);
209 if (OC_STACK_OK != res)
211 OC_LOG(ERROR, TAG, "OCUnlinkDevices : PDMgetOwnedDevices failed");
216 OC_LOG(DEBUG, TAG, "OCUnlinkDevices : Can not find linked devices");
217 res = OC_STACK_INVALID_PARAM; // Input devices are not linked, No request is made
221 // Check the linked devices contains the second device. If yes send credential DELETE request.
222 OCUuidList_t* curDev = idList;
223 while (NULL != curDev)
225 if (memcmp(pTargetDev2->doxm->deviceID.id, curDev->dev.id, sizeof(curDev->dev.id)) == 0)
227 res = SRPUnlinkDevices(ctx, pTargetDev1, pTargetDev2, resultCallback);
228 if (OC_STACK_OK != res)
230 OC_LOG(ERROR, TAG, "OCUnlinkDevices : Failed to unlink devices.");
234 curDev = curDev->next;
236 OC_LOG(DEBUG, TAG, "No matched pair found from provisioning database");
237 res = OC_STACK_INVALID_PARAM; // Input devices are not linked, No request is made
240 OC_LOG(INFO, TAG, "OUT OCUnlinkDevices");
242 PDMDestoryOicUuidLinkList(idList);
247 * Function to device revocation
248 * This function will remove credential of target device from all devices in subnet.
250 * @param[in] ctx Application context would be returned in result callback
251 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
252 * @param[in] pTargetDev Device information to be revoked.
253 * @param[in] resultCallback callback provided by API user, callback will be called when
254 * credential revocation is finished.
255 * @return OC_STACK_OK in case of success and other value otherwise.
257 OCStackResult OCRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
258 const OCProvisionDev_t* pTargetDev,
259 OCProvisionResultCB resultCallback)
261 OC_LOG(INFO, TAG, "IN OCRemoveDevice");
262 OCStackResult res = OC_STACK_ERROR;
263 if (!pTargetDev || !resultCallback || 0 == waitTimeForOwnedDeviceDiscovery)
265 OC_LOG(INFO, TAG, "OCRemoveDevice : Invalied parameters");
266 return OC_STACK_INVALID_PARAM;
269 // Send DELETE requests to linked devices
270 OCStackResult resReq = OC_STACK_ERROR; // Check that we have to wait callback or not.
271 resReq = SRPRemoveDevice(ctx, waitTimeForOwnedDeviceDiscovery, pTargetDev, resultCallback);
272 if (OC_STACK_OK != resReq)
274 if (OC_STACK_CONTINUE == resReq)
276 OC_LOG(DEBUG, TAG, "OCRemoveDevice : Revoked device has no linked device except PT.");
280 OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to invoke SRPRemoveDevice");
286 // Remove credential of revoked device from SVR database
287 const OicSecCred_t *cred = NULL;
288 cred = GetCredResourceData(&pTargetDev->doxm->deviceID);
291 OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to get credential of remove device.");
295 res = RemoveCredential(&cred->subject);
296 if (res != OC_STACK_RESOURCE_DELETED)
298 OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to remove credential.");
302 // Remove device info from prvisioning database.
303 res = PDMDeleteDevice(&pTargetDev->doxm->deviceID);
304 if (res != OC_STACK_OK)
306 OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to delete device in PDM.");
310 // Check that we have to wait callback for DELETE request or not
314 OC_LOG(INFO, TAG, "OUT OCRemoveDevice");
320 * Internal Function to update result in link result array.
322 static void UpdateLinkResults(Linkdata_t *link, int device, OCStackResult stackresult)
325 OC_LOG_V(INFO,TAG,"value of link->currentCountResults is %d",link->currentCountResults);
328 memcpy(link->resArr[(link->currentCountResults)].deviceId.id, link->pDev1->doxm->deviceID.id,UUID_LENGTH);
332 memcpy(link->resArr[(link->currentCountResults)].deviceId.id, link->pDev2->doxm->deviceID.id,UUID_LENGTH);
334 link->resArr[(link->currentCountResults)].res = stackresult;
335 ++(link->currentCountResults);
340 * Callback to handle ACL provisioning for device 2.
342 static void AclProv2CB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
347 OC_LOG(ERROR,TAG,"Context is Null in ACLProv 2");
351 Linkdata_t *link = (Linkdata_t*)ctx;
352 OCProvisionResultCB resultCallback = link->resultCallback;
357 UpdateLinkResults(link, 2,arr[0].res);
358 OC_LOG(ERROR,TAG,"Error occured while ACL provisioning device 1");
359 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
362 OICFree(link->resArr);
366 UpdateLinkResults(link, 2, arr[0].res);
367 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
370 OICFree(link->resArr);
376 * Callback to handle ACL provisioning for device 1
378 static void AclProv1CB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
383 OC_LOG(ERROR,TAG,"Context is Null in ACLProv1");
387 Linkdata_t *link = (Linkdata_t*)ctx;
388 OCProvisionResultCB resultCallback = link->resultCallback;
392 OC_LOG(ERROR,TAG,"Error occured while ACL provisioning device 1");
393 UpdateLinkResults(link, 1, arr[0].res);
394 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
397 OICFree(link->resArr);
401 UpdateLinkResults(link, 1, arr[0].res);
402 if (NULL != link->pDev2Acl)
404 OCStackResult res = SRPProvisionACL(ctx, link->pDev2, link->pDev2Acl, &AclProv2CB);
405 if (OC_STACK_OK!=res)
407 UpdateLinkResults(link, 2, res);
408 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
416 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
419 OICFree(link->resArr);
427 * Callback to handle credential provisioning.
429 static void ProvisionCredsCB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
433 OC_LOG(ERROR,TAG,"Error occured while credential provisioning");
436 Linkdata_t *link = (Linkdata_t*)ctx;
437 OCProvisionResultCB resultCallback = link->resultCallback;
438 OC_LOG_V(INFO, TAG, "has error returned %d",hasError);
439 UpdateLinkResults(link, 1, arr[0].res);
440 UpdateLinkResults(link, 2, arr[1].res);
443 OC_LOG(ERROR,TAG,"Error occured while credential provisioning");
444 ((OCProvisionResultCB)(resultCallback))(link->ctx, nOfRes,
447 OICFree(link->resArr);
451 if (NULL != link->pDev1Acl)
454 OCStackResult res = SRPProvisionACL(ctx, link->pDev1, link->pDev1Acl, &AclProv1CB);
455 if (OC_STACK_OK!=res)
457 OC_LOG(ERROR, TAG, "Error while provisioning ACL for device 1");
458 UpdateLinkResults(link, 1, res);
459 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
462 OICFree(link->resArr);
466 else if (NULL!=link->pDev2Acl)
468 OC_LOG(ERROR, TAG, "ACL for device 1 is NULL");
469 OCStackResult res = SRPProvisionACL(ctx, link->pDev2, link->pDev2Acl, &AclProv2CB);
470 if (OC_STACK_OK!=res)
472 OC_LOG(ERROR, TAG, "Error while provisioning ACL for device 2");
473 UpdateLinkResults(link, 2, res);
474 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
477 OICFree(link->resArr);
483 OC_LOG(INFO, TAG, "ACLs of both devices are NULL");
484 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
487 OICFree(link->resArr);
493 * function to provision credentials between two devices and ACLs for the devices who act as a server.
495 * @param[in] ctx Application context would be returned in result callback.
496 * @param[in] type Type of credentials to be provisioned to the device.
497 * @param[in] pDev1 Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
498 * @param[in] acl ACL for device 1. If this is not required set NULL.
499 * @param[in] pDev2 Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
500 * @param[in] acl ACL for device 2. If this is not required set NULL.
501 * @param[in] resultCallback callback provided by API user, callback will be called when
502 * provisioning request recieves a response from first resource server.
503 * @return OC_STACK_OK in case of success and other value otherwise.
505 OCStackResult OCProvisionPairwiseDevices(void* ctx, OicSecCredType_t type, size_t keySize,
506 const OCProvisionDev_t *pDev1, OicSecAcl_t *pDev1Acl,
507 const OCProvisionDev_t *pDev2, OicSecAcl_t *pDev2Acl,
508 OCProvisionResultCB resultCallback)
511 if (!pDev1 || !pDev2 || !resultCallback)
513 OC_LOG(ERROR, TAG, "OCProvisionPairwiseDevices : Invalid parameters");
514 return OC_STACK_INVALID_PARAM;
516 if (!(keySize == OWNER_PSK_LENGTH_128 || keySize == OWNER_PSK_LENGTH_256))
518 OC_LOG(INFO, TAG, "OCProvisionPairwiseDevices : Invalid key size");
519 return OC_STACK_INVALID_PARAM;
522 OC_LOG(DEBUG, TAG, "Checking link in DB");
523 bool linkExists = true;
524 OCStackResult res = PDMIsLinkExists(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, &linkExists);
525 if(res != OC_STACK_OK)
527 OC_LOG(ERROR, TAG, "Internal Error Occured");
532 OC_LOG(ERROR, TAG, "Link already exists");
533 return OC_STACK_INVALID_PARAM;
536 int noOfResults = 2; // Initial Value
537 if (NULL != pDev1Acl)
541 if (NULL != pDev2Acl)
545 Linkdata_t *link = (Linkdata_t*) OICMalloc(sizeof(Linkdata_t));
548 OC_LOG(ERROR, TAG, "Failed to memory allocation");
549 return OC_STACK_NO_MEMORY;
551 OC_LOG_V(INFO,TAG, "Maximum no od results %d",noOfResults);
554 link->pDev1Acl = pDev1Acl;
556 link->pDev2Acl = pDev2Acl;
558 // 1 call for each device for credential provisioning. implict call by SRPProvisioning credential
559 // 1 call for ACL provisioning for device 1 and 1 call for ACL provisioning for device 2.
560 link->numOfResults = noOfResults;
561 link->resultCallback = resultCallback;
562 link->currentCountResults = 0;
563 link->resArr = (OCProvisionResult_t*) OICMalloc(sizeof(OCProvisionResult_t)*noOfResults);
564 res = SRPProvisionCredentials(link, type, keySize,
565 pDev1, pDev2, &ProvisionCredsCB);
566 if (res != OC_STACK_OK)
568 OICFree(link->resArr);
575 OCStackResult OCGetDevInfoFromNetwork(unsigned short waittime,
576 OCProvisionDev_t** pOwnedDevList,
577 OCProvisionDev_t** pUnownedDevList)
579 //TODO will be replaced by more efficient logic
580 if (pOwnedDevList == NULL || *pOwnedDevList != NULL || pUnownedDevList == NULL
581 || *pUnownedDevList != NULL)
583 return OC_STACK_INVALID_PARAM;
586 // Code for unowned discovery
587 OCProvisionDev_t *unownedDevice = NULL;
588 OCStackResult res = OCDiscoverUnownedDevices(waittime/2, &unownedDevice);
589 if (OC_STACK_OK != res)
591 OC_LOG(ERROR,TAG, "Error in unowned discovery");
595 // Code for owned discovery
596 OCProvisionDev_t *ownedDevice = NULL;
597 res = OCDiscoverOwnedDevices(waittime/2, &ownedDevice);
598 if (OC_STACK_OK != res)
600 OC_LOG(ERROR,TAG, "Error in owned discovery");
601 PMDeleteDeviceList(unownedDevice);
605 // Code to get list of all the owned devices.
606 OCUuidList_t *uuidList = NULL;
607 size_t numOfDevices = 0;
608 res = PDMGetOwnedDevices(&uuidList, &numOfDevices);
609 if (OC_STACK_OK != res)
611 OC_LOG(ERROR, TAG, "Error while getting info from DB");
612 PMDeleteDeviceList(unownedDevice);
613 PMDeleteDeviceList(ownedDevice);
617 // Code to compare devices in owned list and deviceid from DB.
618 OCProvisionDev_t* pCurDev = ownedDevice;
619 size_t deleteCnt = 0;
622 if(true == PMDeleteFromUUIDList(uuidList, &pCurDev->doxm->deviceID))
626 pCurDev = pCurDev->next;
628 // If there is no remaind device in uuidList, we have to assign NULL to prevent free.
629 if (deleteCnt == numOfDevices)
633 // Code to add information of the devices which are currently off in owned list.
634 OCUuidList_t *powerOffDeviceList = uuidList;
635 while (powerOffDeviceList)
637 OCProvisionDev_t *ptr = (OCProvisionDev_t *)OICCalloc(1, sizeof (OCProvisionDev_t));
640 OC_LOG(ERROR,TAG,"Fail to allocate memory");
641 PMDeleteDeviceList(unownedDevice);
642 PMDeleteDeviceList(ownedDevice);
643 OCDeleteUuidList(uuidList);
644 return OC_STACK_NO_MEMORY;
647 ptr->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
648 if (NULL == ptr->doxm)
650 OC_LOG(ERROR,TAG,"Fail to allocate memory");
651 PMDeleteDeviceList(unownedDevice);
652 PMDeleteDeviceList(ownedDevice);
653 OCDeleteUuidList(uuidList);
655 return OC_STACK_NO_MEMORY;
658 memcpy(ptr->doxm->deviceID.id, powerOffDeviceList->dev.id, sizeof(ptr->doxm->deviceID.id));
660 ptr->devStatus = DEV_STATUS_OFF;
661 LL_PREPEND(ownedDevice, ptr);
662 powerOffDeviceList = powerOffDeviceList->next;
665 OCDeleteUuidList(uuidList);
666 *pOwnedDevList = ownedDevice;
667 *pUnownedDevList = unownedDevice;
671 OCStackResult OCGetLinkedStatus(const OicUuid_t* uuidOfDevice, OCUuidList_t** uuidList,
672 size_t* numOfDevices)
674 return PDMGetLinkedDevices(uuidOfDevice, uuidList, numOfDevices);
677 void OCDeleteUuidList(OCUuidList_t* pList)
679 PDMDestoryOicUuidLinkList(pList);
683 * This function deletes ACL data.
685 * @param pAcl Pointer to OicSecAcl_t structure.
687 void OCDeleteACLList(OicSecAcl_t* pAcl)
695 * this function sends CRL information to resource.
697 * @param[in] ctx Application context would be returned in result callback.
698 * @param[in] selectedDeviceInfo Selected target device.
699 * @param[in] crl CRL to provision.
700 * @param[in] resultCallback callback provided by API user, callback will be called when provisioning
701 request recieves a response from resource server.
702 * @return OC_STACK_OK in case of success and other value otherwise.
704 OCStackResult OCProvisionCRL(void* ctx, const OCProvisionDev_t *selectedDeviceInfo, OicSecCrl_t *crl,
705 OCProvisionResultCB resultCallback)
707 return SRPProvisionCRL(ctx, selectedDeviceInfo, crl, resultCallback);
709 #endif // __WITH_X509__