Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / src / ocprovisioningmanager.c
1 /* *****************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * *****************************************************************/
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include "ocprovisioningmanager.h"
24 #include "pmutility.h"
25 #include "ownershiptransfermanager.h"
26 #include "oic_malloc.h"
27 #include "logger.h"
28 #include "secureresourceprovider.h"
29 #include "provisioningdatabasemanager.h"
30 #include "credresource.h"
31 #include "utlist.h"
32 #include "aclresource.h" //Note: SRM internal header
33
34 #define TAG "OCPMAPI"
35
36 typedef struct Linkdata Linkdata_t;
37 struct Linkdata
38 {
39     void *ctx;
40     const OCProvisionDev_t *pDev1;
41     OicSecAcl_t *pDev1Acl;
42     const OCProvisionDev_t *pDev2;
43     OicSecAcl_t *pDev2Acl;
44     OCProvisionResult_t *resArr;
45     int numOfResults;
46     int currentCountResults;
47     OCProvisionResultCB resultCallback;
48
49 };
50
51 /**
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).
56  *
57  * @param[in] dbPath file path of the sqlite3 db
58  *
59  * @return OC_STACK_OK in case of success and other value otherwise.
60  */
61 OCStackResult OCInitPM(const char* dbPath)
62 {
63     return PDMInit(dbPath);
64 }
65
66 /**
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
69  * OCMode.
70  *
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.
75  */
76 OCStackResult OCDiscoverUnownedDevices(unsigned short timeout, OCProvisionDev_t **ppList)
77 {
78     if( ppList == NULL || *ppList != NULL)
79     {
80         return OC_STACK_INVALID_PARAM;
81     }
82
83     return PMDeviceDiscovery(timeout, false, ppList);
84 }
85
86 /**
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.
89  *
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.
94  */
95 OCStackResult OCDiscoverOwnedDevices(unsigned short timeout, OCProvisionDev_t **ppList)
96 {
97     if( ppList == NULL || *ppList != NULL)
98     {
99         return OC_STACK_INVALID_PARAM;
100     }
101
102     return PMDeviceDiscovery(timeout, true, ppList);
103 }
104
105 /**
106  * API to register for particular OxM.
107  *
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.
111  */
112 OCStackResult OCSetOwnerTransferCallbackData(OicSecOxm_t oxm, OTMCallbackData_t* callbackData)
113 {
114     if(NULL == callbackData)
115     {
116         return OC_STACK_INVALID_PARAM;
117     }
118
119     return OTMSetOwnershipTransferCallbackData(oxm, callbackData);
120 }
121
122 OCStackResult OCDoOwnershipTransfer(void* ctx,
123                                       OCProvisionDev_t *targetDevices,
124                                       OCProvisionResultCB resultCallback)
125 {
126     if( NULL == targetDevices )
127     {
128         return OC_STACK_INVALID_PARAM;
129     }
130
131     return OTMDoOwnershipTransfer(ctx, targetDevices, resultCallback);
132 }
133
134 /**
135  * This function deletes memory allocated to linked list created by OCDiscover_XXX_Devices API.
136  *
137  * @param[in] pList Pointer to OCProvisionDev_t which should be deleted.
138  */
139 void OCDeleteDiscoveredDevices(OCProvisionDev_t *pList)
140 {
141     PMDeleteDeviceList(pList);
142 }
143
144 /**
145  * this function sends ACL information to resource.
146  *
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.
153  */
154 OCStackResult OCProvisionACL(void* ctx, const OCProvisionDev_t *selectedDeviceInfo, OicSecAcl_t *acl,
155                              OCProvisionResultCB resultCallback)
156 {
157     return SRPProvisionACL(ctx, selectedDeviceInfo, acl, resultCallback);
158 }
159
160 /**
161  * function to provision credential to devices.
162  *
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.
170  */
171 OCStackResult OCProvisionCredentials(void *ctx, OicSecCredType_t type, size_t keySize,
172                                       const OCProvisionDev_t *pDev1,
173                                       const OCProvisionDev_t *pDev2,
174                                       OCProvisionResultCB resultCallback)
175 {
176     return SRPProvisionCredentials(ctx, type, keySize,
177                                       pDev1, pDev2, resultCallback);
178
179 }
180
181 /*
182 * Function to unlink devices.
183 * This function will remove the credential & relationship between the two devices.
184 *
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.
191 */
192 OCStackResult OCUnlinkDevices(void* ctx,
193                               const OCProvisionDev_t* pTargetDev1,
194                               const OCProvisionDev_t* pTargetDev2,
195                               OCProvisionResultCB resultCallback)
196 {
197     OC_LOG(INFO, TAG, "IN OCUnlinkDevices");
198     OCUuidList_t* idList = NULL;
199     size_t numOfDev = 0;
200
201     if (!pTargetDev1 || !pTargetDev2 || !resultCallback)
202     {
203         OC_LOG(ERROR, TAG, "OCUnlinkDevices : NULL parameters");
204         return OC_STACK_INVALID_PARAM;
205     }
206
207     // Get linked devices with the first device.
208     OCStackResult res = PDMGetLinkedDevices(&(pTargetDev1->doxm->deviceID), &idList, &numOfDev);
209     if (OC_STACK_OK != res)
210     {
211         OC_LOG(ERROR, TAG, "OCUnlinkDevices : PDMgetOwnedDevices failed");
212         goto error;
213     }
214     if (1 > numOfDev)
215     {
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
218         goto error;
219     }
220
221     // Check the linked devices contains the second device. If yes send credential DELETE request.
222     OCUuidList_t* curDev = idList;
223     while (NULL != curDev)
224     {
225         if (memcmp(pTargetDev2->doxm->deviceID.id, curDev->dev.id, sizeof(curDev->dev.id)) == 0)
226         {
227             res = SRPUnlinkDevices(ctx, pTargetDev1, pTargetDev2, resultCallback);
228             if (OC_STACK_OK != res)
229             {
230                 OC_LOG(ERROR, TAG, "OCUnlinkDevices : Failed to unlink devices.");
231             }
232             goto error;
233         }
234         curDev = curDev->next;
235     }
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
238
239 error:
240     OC_LOG(INFO, TAG, "OUT OCUnlinkDevices");
241
242     PDMDestoryOicUuidLinkList(idList);
243     return res;
244 }
245
246 /*
247 * Function to device revocation
248 * This function will remove credential of target device from all devices in subnet.
249 *
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.
256 */
257 OCStackResult OCRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
258                             const OCProvisionDev_t* pTargetDev,
259                             OCProvisionResultCB resultCallback)
260 {
261     OC_LOG(INFO, TAG, "IN OCRemoveDevice");
262     OCStackResult res = OC_STACK_ERROR;
263     if (!pTargetDev || !resultCallback || 0 == waitTimeForOwnedDeviceDiscovery)
264     {
265         OC_LOG(INFO, TAG, "OCRemoveDevice : Invalied parameters");
266         return OC_STACK_INVALID_PARAM;
267     }
268
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)
273     {
274         if (OC_STACK_CONTINUE == resReq)
275         {
276             OC_LOG(DEBUG, TAG, "OCRemoveDevice : Revoked device has no linked device except PT.");
277         }
278         else
279         {
280             OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to invoke SRPRemoveDevice");
281             res = resReq;
282             goto error;
283         }
284     }
285
286     // Remove credential of revoked device from SVR database
287     const OicSecCred_t *cred = NULL;
288     cred = GetCredResourceData(&pTargetDev->doxm->deviceID);
289     if (cred == NULL)
290     {
291         OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to get credential of remove device.");
292         goto error;
293     }
294
295     res = RemoveCredential(&cred->subject);
296     if (res != OC_STACK_RESOURCE_DELETED)
297     {
298         OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to remove credential.");
299         goto error;
300     }
301
302     // Remove device info from prvisioning database.
303     res = PDMDeleteDevice(&pTargetDev->doxm->deviceID);
304     if (res != OC_STACK_OK)
305     {
306         OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to delete device in PDM.");
307         goto error;
308     }
309
310     // Check that we have to wait callback for DELETE request or not
311     res = resReq;
312
313 error:
314     OC_LOG(INFO, TAG, "OUT OCRemoveDevice");
315     return res;
316 }
317
318
319 /**
320  * Internal Function to update result in link result array.
321  */
322 static void UpdateLinkResults(Linkdata_t *link, int device, OCStackResult stackresult)
323 {
324
325     OC_LOG_V(INFO,TAG,"value of link->currentCountResults is %d",link->currentCountResults);
326     if (1 == device)
327     {
328         memcpy(link->resArr[(link->currentCountResults)].deviceId.id, link->pDev1->doxm->deviceID.id,UUID_LENGTH);
329     }
330     else
331     {
332         memcpy(link->resArr[(link->currentCountResults)].deviceId.id, link->pDev2->doxm->deviceID.id,UUID_LENGTH);
333     }
334     link->resArr[(link->currentCountResults)].res = stackresult;
335     ++(link->currentCountResults);
336
337 }
338
339 /**
340  * Callback to handle ACL provisioning for device 2.
341  */
342 static void AclProv2CB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
343 {
344
345     if (NULL == ctx)
346     {
347         OC_LOG(ERROR,TAG,"Context is Null in ACLProv 2");
348         return;
349     }
350     (void)nOfRes;
351     Linkdata_t *link = (Linkdata_t*)ctx;
352     OCProvisionResultCB resultCallback = link->resultCallback;
353
354
355     if (hasError)
356     {
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,
360                                                 link->resArr,
361                                                 true);
362         OICFree(link->resArr);
363         OICFree(link) ;
364         return;
365     }
366     UpdateLinkResults(link, 2, arr[0].res);
367    ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
368                                            link->resArr,
369                                            false);
370     OICFree(link->resArr);
371     OICFree(link);
372     return;
373 }
374
375 /**
376  * Callback to handle ACL provisioning for device 1
377  */
378 static void AclProv1CB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
379 {
380
381     if (NULL == ctx)
382     {
383         OC_LOG(ERROR,TAG,"Context is Null in ACLProv1");
384         return;
385     }
386     (void)nOfRes;
387     Linkdata_t *link = (Linkdata_t*)ctx;
388     OCProvisionResultCB resultCallback = link->resultCallback;
389
390     if (hasError)
391     {
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,
395                                                 link->resArr,
396                                                 true);
397         OICFree(link->resArr);
398         OICFree(link);
399         return;
400     }
401     UpdateLinkResults(link, 1, arr[0].res);
402     if (NULL != link->pDev2Acl)
403     {
404         OCStackResult res =  SRPProvisionACL(ctx, link->pDev2, link->pDev2Acl, &AclProv2CB);
405         if (OC_STACK_OK!=res)
406         {
407              UpdateLinkResults(link, 2, res);
408              ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
409                                                      link->resArr,
410                                                      true);
411
412         }
413     }
414     else
415     {
416         ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
417                                                 link->resArr,
418                                                 false);
419         OICFree(link->resArr);
420         OICFree(link);
421     }
422
423     return;
424 }
425
426 /**
427  * Callback to handle credential provisioning.
428  */
429 static void ProvisionCredsCB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
430 {
431     if (NULL == ctx)
432     {
433         OC_LOG(ERROR,TAG,"Error occured while credential provisioning");
434         return;
435     }
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);
441     if (hasError)
442     {
443         OC_LOG(ERROR,TAG,"Error occured while credential provisioning");
444         ((OCProvisionResultCB)(resultCallback))(link->ctx, nOfRes,
445                                                 link->resArr,
446                                                 true);
447          OICFree(link->resArr);
448          OICFree(link);
449          return;
450     }
451     if (NULL != link->pDev1Acl)
452     {
453
454         OCStackResult res =  SRPProvisionACL(ctx, link->pDev1, link->pDev1Acl, &AclProv1CB);
455         if (OC_STACK_OK!=res)
456         {
457              OC_LOG(ERROR, TAG, "Error while provisioning ACL for device 1");
458              UpdateLinkResults(link, 1, res);
459              ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
460                                                      link->resArr,
461                                                      true);
462               OICFree(link->resArr);
463               OICFree(link);
464         }
465     }
466     else if (NULL!=link->pDev2Acl)
467     {
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)
471         {
472              OC_LOG(ERROR, TAG, "Error while provisioning ACL for device 2");
473               UpdateLinkResults(link, 2, res);
474              ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
475                                                      link->resArr,
476                                                      true);
477               OICFree(link->resArr);
478               OICFree(link);
479         }
480     }
481     else
482     {
483         OC_LOG(INFO, TAG, "ACLs of both devices are NULL");
484         ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
485                                                 link->resArr,
486                                                 false);
487         OICFree(link->resArr);
488         OICFree(link);
489     }
490     return;
491 }
492 /**
493  * function to provision credentials between two devices and ACLs for the devices who act as a server.
494  *
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.
504  */
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)
509 {
510
511     if (!pDev1 || !pDev2 || !resultCallback)
512     {
513         OC_LOG(ERROR, TAG, "OCProvisionPairwiseDevices : Invalid parameters");
514         return OC_STACK_INVALID_PARAM;
515     }
516     if (!(keySize == OWNER_PSK_LENGTH_128 || keySize == OWNER_PSK_LENGTH_256))
517     {
518         OC_LOG(INFO, TAG, "OCProvisionPairwiseDevices : Invalid key size");
519         return OC_STACK_INVALID_PARAM;
520     }
521
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)
526     {
527         OC_LOG(ERROR, TAG, "Internal Error Occured");
528         return res;
529     }
530     if (linkExists)
531     {
532         OC_LOG(ERROR, TAG, "Link already exists");
533         return OC_STACK_INVALID_PARAM;
534     }
535
536     int noOfResults = 2; // Initial Value
537     if (NULL != pDev1Acl)
538     {
539         ++noOfResults;
540     }
541     if (NULL != pDev2Acl)
542     {
543        ++noOfResults;
544     }
545     Linkdata_t *link = (Linkdata_t*) OICMalloc(sizeof(Linkdata_t));
546     if (!link)
547     {
548         OC_LOG(ERROR, TAG, "Failed to memory allocation");
549         return OC_STACK_NO_MEMORY;
550     }
551     OC_LOG_V(INFO,TAG, "Maximum no od results %d",noOfResults);
552
553     link->pDev1 = pDev1;
554     link->pDev1Acl = pDev1Acl;
555     link->pDev2 = pDev2;
556     link->pDev2Acl = pDev2Acl;
557     link->ctx = ctx;
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)
567     {
568         OICFree(link->resArr);
569         OICFree(link);
570     }
571     return res;
572
573 }
574
575 OCStackResult OCGetDevInfoFromNetwork(unsigned short waittime,
576                                        OCProvisionDev_t** pOwnedDevList,
577                                        OCProvisionDev_t** pUnownedDevList)
578 {
579     //TODO will be replaced by more efficient logic
580     if (pOwnedDevList == NULL || *pOwnedDevList != NULL || pUnownedDevList == NULL
581          || *pUnownedDevList != NULL)
582     {
583         return OC_STACK_INVALID_PARAM;
584     }
585
586     // Code for unowned discovery
587     OCProvisionDev_t *unownedDevice = NULL;
588     OCStackResult res =  OCDiscoverUnownedDevices(waittime/2, &unownedDevice);
589     if (OC_STACK_OK != res)
590     {
591         OC_LOG(ERROR,TAG, "Error in unowned discovery");
592         return res;
593     }
594
595     // Code for owned discovery
596     OCProvisionDev_t *ownedDevice = NULL;
597     res =  OCDiscoverOwnedDevices(waittime/2, &ownedDevice);
598     if (OC_STACK_OK != res)
599     {
600         OC_LOG(ERROR,TAG, "Error in owned discovery");
601         PMDeleteDeviceList(unownedDevice);
602         return res;
603     }
604
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)
610     {
611         OC_LOG(ERROR, TAG, "Error while getting info from DB");
612         PMDeleteDeviceList(unownedDevice);
613         PMDeleteDeviceList(ownedDevice);
614         return res;
615     }
616
617     // Code to compare devices in owned list and deviceid from DB.
618     OCProvisionDev_t* pCurDev = ownedDevice;
619     size_t deleteCnt = 0;
620     while (pCurDev)
621     {
622         if(true == PMDeleteFromUUIDList(uuidList, &pCurDev->doxm->deviceID))
623         {
624             deleteCnt++;
625         }
626         pCurDev = pCurDev->next;
627     }
628     // If there is no remaind device in uuidList, we have to assign NULL to prevent free.
629     if (deleteCnt == numOfDevices)
630     {
631         uuidList = NULL;
632     }
633     // Code to add information of the devices which are currently off in owned list.
634     OCUuidList_t *powerOffDeviceList = uuidList;
635     while (powerOffDeviceList)
636     {
637         OCProvisionDev_t *ptr = (OCProvisionDev_t *)OICCalloc(1, sizeof (OCProvisionDev_t));
638         if (NULL == ptr)
639         {
640             OC_LOG(ERROR,TAG,"Fail to allocate memory");
641             PMDeleteDeviceList(unownedDevice);
642             PMDeleteDeviceList(ownedDevice);
643             OCDeleteUuidList(uuidList);
644             return OC_STACK_NO_MEMORY;
645         }
646
647         ptr->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
648         if (NULL == ptr->doxm)
649         {
650             OC_LOG(ERROR,TAG,"Fail to allocate memory");
651             PMDeleteDeviceList(unownedDevice);
652             PMDeleteDeviceList(ownedDevice);
653             OCDeleteUuidList(uuidList);
654             OICFree(ptr);
655             return OC_STACK_NO_MEMORY;
656         }
657
658         memcpy(ptr->doxm->deviceID.id, powerOffDeviceList->dev.id, sizeof(ptr->doxm->deviceID.id));
659
660         ptr->devStatus = DEV_STATUS_OFF;
661         LL_PREPEND(ownedDevice, ptr);
662         powerOffDeviceList = powerOffDeviceList->next;
663
664     }
665     OCDeleteUuidList(uuidList);
666     *pOwnedDevList = ownedDevice;
667     *pUnownedDevList = unownedDevice;
668     return OC_STACK_OK;
669 }
670
671 OCStackResult OCGetLinkedStatus(const OicUuid_t* uuidOfDevice, OCUuidList_t** uuidList,
672                                  size_t* numOfDevices)
673 {
674     return PDMGetLinkedDevices(uuidOfDevice, uuidList, numOfDevices);
675 }
676
677 void OCDeleteUuidList(OCUuidList_t* pList)
678 {
679     PDMDestoryOicUuidLinkList(pList);
680 }
681
682 /**
683  * This function deletes ACL data.
684  *
685  * @param pAcl Pointer to OicSecAcl_t structure.
686  */
687 void OCDeleteACLList(OicSecAcl_t* pAcl)
688 {
689     DeleteACLList(pAcl);
690 }
691
692
693 #ifdef __WITH_X509__
694 /**
695  * this function sends CRL information to resource.
696  *
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.
703  */
704 OCStackResult OCProvisionCRL(void* ctx, const OCProvisionDev_t *selectedDeviceInfo, OicSecCrl_t *crl,
705                              OCProvisionResultCB resultCallback)
706 {
707     return SRPProvisionCRL(ctx, selectedDeviceInfo, crl, resultCallback);
708 }
709 #endif // __WITH_X509__
710