Modify to update rowner as PT's UUID when ownership transfer is done.
[platform/upstream/iotivity.git] / resource / csdk / security / src / pconfresource.c
1 /* *****************************************************************
2  *
3  * Copyright 2016 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
21 #include <stdlib.h>
22 #include <string.h>
23 #include "ocstack.h"
24 #include "logger.h"
25 #include "oic_malloc.h"
26 #include "oic_string.h"
27 #include "cJSON.h"
28 #include "base64.h"
29 #include "ocpayload.h"
30 #include "payload_logging.h"
31 #include "resourcemanager.h"
32 #include "pconfresource.h"
33 #include "psinterface.h"
34 #include "utlist.h"
35 #include "srmresourcestrings.h"
36 #include "doxmresource.h"
37 #include "srmutility.h"
38 #include "ocserverrequest.h"
39 #include <stdlib.h>
40 #include "psinterface.h"
41 #include "security_internals.h"
42 #ifdef WITH_ARDUINO
43 #include <string.h>
44 #else
45 #include <strings.h>
46 #endif
47
48 #define TAG  "SRM-PCONF"
49
50 static const uint16_t CBOR_SIZE = 1024;
51 static const uint64_t CBOR_MAX_SIZE = 4400;
52 static const uint8_t  PCONF_MAP_SIZE = 4;
53 static const uint8_t  PCONF_RESOURCE_MAP_SIZE = 4;
54
55 static OicSecPconf_t          *gPconf = NULL;
56 static OCResourceHandle   gPconfHandle = NULL;
57 static OicSecPconf_t         gDefaultPconf =
58 {
59     false,                  /* bool edp */
60     NULL,                  /* OicSecPrm *prm */
61     0,                       /* size_t prmLen */
62     {.val = {0}},       /* OicDpPin_t pin */
63     NULL,                  /* OicSecPdAcl_t *pdacls */
64     NULL,                  /* OicUuid_t *pddevs */
65     0,                       /* size_t pddevLen */
66     {.id = {0}},        /* OicUuid_t deviceID */
67     {.id = {0}},        /* OicUuid_t rowner */
68 };
69
70 /**
71  * This function frees OicSecPdAcl_t object's fields and object itself.
72  */
73 void FreePdAclList(OicSecPdAcl_t* pdacls)
74 {
75     if (pdacls)
76     {
77         size_t i = 0;
78
79         //Clean pdacl objecs
80         OicSecPdAcl_t *aclTmp1 = NULL;
81         OicSecPdAcl_t *aclTmp2 = NULL;
82         LL_FOREACH_SAFE(pdacls, aclTmp1, aclTmp2)
83         {
84             LL_DELETE(pdacls, aclTmp1);
85
86             // Clean Resources
87             for (i = 0; i < aclTmp1->resourcesLen; i++)
88             {
89                 OICFree(aclTmp1->resources[i]);
90             }
91             OICFree(aclTmp1->resources);
92
93             //Clean Period
94             if(aclTmp1->periods)
95             {
96                 for(i = 0; i < aclTmp1->prdRecrLen; i++)
97                 {
98                     OICFree(aclTmp1->periods[i]);
99                 }
100                 OICFree(aclTmp1->periods);
101             }
102
103             //Clean Recurrence
104             if(aclTmp1->recurrences)
105             {
106                 for(i = 0; i < aclTmp1->prdRecrLen; i++)
107                 {
108                     OICFree(aclTmp1->recurrences[i]);
109                 }
110                 OICFree(aclTmp1->recurrences);
111             }
112         }
113
114         //Clean pconf itself
115         OICFree(pdacls);
116     }
117 }
118
119 void DeletePconfBinData(OicSecPconf_t* pconf)
120 {
121     if (pconf)
122     {
123         //Clean prm
124         OICFree(pconf->prm);
125
126         //Clean pdacl
127         if (pconf->pdacls)
128         {
129             FreePdAclList(pconf->pdacls);
130         }
131
132         //Clean pddev
133         OICFree(pconf->pddevs);
134
135         //Clean pconf itself
136         OICFree(pconf);
137     }
138 }
139
140 static size_t OicPdAclSize(const OicSecPdAcl_t *pdAcl)
141 {
142     if (!pdAcl)
143     {
144         return 0;
145     }
146
147     OicSecPdAcl_t *tmp = (OicSecPdAcl_t *)pdAcl;
148     size_t size = 0;
149     while (tmp)
150     {
151         size++;
152         tmp = tmp->next;
153     }
154     return size;
155 }
156
157 OCStackResult PconfToCBORPayload(const OicSecPconf_t *pconf,uint8_t **payload,size_t *size)
158 {
159     if (NULL == pconf || NULL == payload || NULL != *payload || NULL == size)
160     {
161         return OC_STACK_INVALID_PARAM;
162     }
163     size_t cborLen = *size;
164     if(0 == cborLen)
165     {
166         cborLen = CBOR_SIZE;
167     }
168     *payload = NULL;
169
170     OCStackResult ret = OC_STACK_ERROR;
171     CborEncoder encoder = { {.ptr = NULL}, .end = 0};
172     CborEncoder pconfMap = { {.ptr = NULL}, .end = 0};
173
174     int64_t cborEncoderResult = CborNoError;
175     uint8_t mapSize = PCONF_MAP_SIZE;
176
177     if (pconf->prmLen > 0)
178     {
179         mapSize++;
180     }
181     if (pconf->pdacls)
182     {
183         mapSize++;
184     }
185     if (pconf->pddevs)
186     {
187         mapSize++;
188     }
189
190     uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborLen);
191     VERIFY_NON_NULL(TAG, outPayload, ERROR);
192
193     cbor_encoder_init(&encoder, outPayload, cborLen, 0);
194     cborEncoderResult = cbor_encoder_create_map(&encoder, &pconfMap, mapSize);
195     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Creating Pconf Map.");
196
197     //edp  -- Mandatory
198     cborEncoderResult = cbor_encode_text_string(&pconfMap, OIC_JSON_EDP_NAME,
199             strlen(OIC_JSON_EDP_NAME));
200     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Encode EDP String.");
201     cborEncoderResult = cbor_encode_boolean(&pconfMap, pconf->edp);
202     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Convert PconfEDP value");
203
204     //PRM type -- Not Mandatory
205     if(pconf->prmLen > 0)
206     {
207         CborEncoder prm = { {.ptr = NULL }, .end = 0 };
208         cborEncoderResult = cbor_encode_text_string(&pconfMap, OIC_JSON_PRM_NAME,
209                 strlen(OIC_JSON_PRM_NAME));
210         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Convert Pconf PRM NAME");
211         cborEncoderResult = cbor_encoder_create_array(&pconfMap, &prm, pconf->prmLen);
212         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Convert Pconf PRM value");
213
214         for (size_t i = 0; i < pconf->prmLen; i++)
215         {
216             cborEncoderResult = cbor_encode_int(&prm, pconf->prm[i]);
217             VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Convert Pconf PRM Array");
218         }
219         cborEncoderResult = cbor_encoder_close_container(&pconfMap, &prm);
220         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to close encode array");
221     }
222
223     //PIN -- Mandatory
224     cborEncoderResult = cbor_encode_text_string(&pconfMap, OIC_JSON_PIN_NAME,
225             strlen(OIC_JSON_PIN_NAME));
226     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create OIC_JSON_PIN_NAME");
227     cborEncoderResult = cbor_encode_byte_string(&pconfMap, pconf->pin.val, sizeof(pconf->pin.val));
228     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to convert pin value");
229
230     //PDACL -- Mandatory
231     if (pconf->pdacls)
232     {
233         OicSecPdAcl_t *pdacl = pconf->pdacls;
234         CborEncoder pdAclArray = {{.ptr = NULL }, .end = 0 };
235         cborEncoderResult = cbor_encode_text_string(&pconfMap, OIC_JSON_PDACL_NAME,
236                 strlen(OIC_JSON_PDACL_NAME));
237         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create OIC_JSON_PDACL_NAME");
238         cborEncoderResult = cbor_encoder_create_array(&pconfMap, &pdAclArray,
239                 OicPdAclSize(pconf->pdacls));
240         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to creeate _pdacl array");
241
242         while(pdacl)
243         {
244             CborEncoder pdAclMap = { {.ptr = NULL }, .end = 0, .added = 0, .flags = 0 };
245             // PDACL Map size - Number of mandatory items
246             uint8_t aclMapSize = 2;
247
248             if (pdacl->prdRecrLen)
249             {
250                 ++aclMapSize;
251             }
252             if (pdacl->recurrences)
253             {
254                 ++aclMapSize;
255             }
256
257             cborEncoderResult = cbor_encoder_create_map(&pdAclArray, &pdAclMap, aclMapSize);
258             VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult,  "Failed to creeate _pdacl array");
259
260             // Resources -- Mandatory
261             cborEncoderResult = cbor_encode_text_string(&pdAclMap, OIC_JSON_RESOURCES_NAME,
262                     strlen(OIC_JSON_RESOURCES_NAME));
263             VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult,  "Failed to encode resource result");
264
265             CborEncoder resources = { {.ptr = NULL }, .end = 0, .added = 0, .flags = 0 };
266             cborEncoderResult = cbor_encoder_create_array(&pdAclMap, &resources,
267                     pdacl->resourcesLen);
268             VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult,  "Failed to create resource array");
269
270             for (size_t i = 0; i < pdacl->resourcesLen; i++)
271             {
272                 CborEncoder rMap = { {.ptr = NULL }, .end = 0 };
273                 cborEncoderResult = cbor_encoder_create_map(&resources, &rMap,
274                         PCONF_RESOURCE_MAP_SIZE);
275                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding Resource Map.");
276
277                 cborEncoderResult = cbor_encode_text_string(&rMap, OIC_JSON_HREF_NAME,
278                         strlen(OIC_JSON_HREF_NAME));
279                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding HREF Name Tag.");
280                 cborEncoderResult = cbor_encode_text_string(&rMap, pdacl->resources[i],
281                         strlen(pdacl->resources[i]));
282                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding HREF Value in Map.");
283
284                 cborEncoderResult = cbor_encode_text_string(&rMap, OIC_JSON_REL_NAME,
285                         strlen(OIC_JSON_REL_NAME));
286                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding REL Name Tag.");
287
288                 // TODO : Need to assign real value of REL
289                 cborEncoderResult = cbor_encode_text_string(&rMap, OIC_JSON_EMPTY_STRING,
290                         strlen(OIC_JSON_EMPTY_STRING));
291                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding REL Value.");
292
293                 cborEncoderResult = cbor_encode_text_string(&rMap, OIC_JSON_RT_NAME,
294                         strlen(OIC_JSON_RT_NAME));
295                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RT Name Tag.");
296
297                 // TODO : Need to assign real value of RT
298                 cborEncoderResult = cbor_encode_text_string(&rMap, OIC_JSON_EMPTY_STRING,
299                         strlen(OIC_JSON_EMPTY_STRING));
300                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RT Value.");
301
302                 cborEncoderResult = cbor_encode_text_string(&rMap, OIC_JSON_IF_NAME,
303                         strlen(OIC_JSON_IF_NAME));
304                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding IF Name Tag.");
305
306                 // TODO : Need to assign real value of IF
307                 cborEncoderResult = cbor_encode_text_string(&rMap, OIC_JSON_EMPTY_STRING,
308                         strlen(OIC_JSON_EMPTY_STRING));
309                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding IF Value.");
310
311                 cborEncoderResult = cbor_encoder_close_container(&resources, &rMap);
312                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Resource Map.");
313             }
314
315             cborEncoderResult = cbor_encoder_close_container(&pdAclMap, &resources);
316             VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult,  "Failed to close resource array");
317
318             // Permissions -- Mandatory
319             cborEncoderResult = cbor_encode_text_string(&pdAclMap, OIC_JSON_PERMISSION_NAME,
320                     strlen(OIC_JSON_PERMISSION_NAME));
321             VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult,  "Failed to create permition string");
322             cborEncoderResult = cbor_encode_int(&pdAclMap, pdacl->permission);
323             VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to encode permition calue");
324
325             // Period -- Not Mandatory
326             if (pdacl->periods)
327             {
328                 CborEncoder period = { {.ptr = NULL }, .end = 0, .added = 0, .flags = 0 };
329                 cborEncoderResult = cbor_encode_text_string(&pdAclMap, OIC_JSON_PERIODS_NAME,
330                         strlen(OIC_JSON_PERIODS_NAME));
331                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to encode period value");
332                 cborEncoderResult = cbor_encoder_create_array(&pdAclMap, &period,
333                         pdacl->prdRecrLen);
334                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create array");
335
336                 for (size_t i = 0; i < pdacl->prdRecrLen; i++)
337                 {
338                     cborEncoderResult = cbor_encode_text_string(&period, pdacl->periods[i],
339                             strlen(pdacl->periods[i]));
340                     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to encode period");
341                 }
342                 cborEncoderResult = cbor_encoder_close_container(&pdAclMap, &period);
343                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult,"Failed to close period array");
344             }
345
346             // Period -- Not Mandatory
347             if(0 != pdacl->prdRecrLen && pdacl->recurrences)
348             {
349                 CborEncoder recurrences = { {.ptr = NULL }, .end = 0, .added = 0, .flags = 0 };
350                 cborEncoderResult = cbor_encode_text_string(&pdAclMap, OIC_JSON_RECURRENCES_NAME,
351                         strlen(OIC_JSON_RECURRENCES_NAME));
352                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult,"Failed to encode recurrences");
353                 cborEncoderResult = cbor_encoder_create_array(&pdAclMap, &recurrences,
354                         pdacl->prdRecrLen);
355                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create rec array");
356
357                 for (size_t i = 0; i < pdacl->prdRecrLen; i++)
358                 {
359                     cborEncoderResult = cbor_encode_text_string(&recurrences,
360                             pdacl->recurrences[i], strlen(pdacl->recurrences[i]));
361                     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to encode recurrences");
362                 }
363                 cborEncoderResult = cbor_encoder_close_container(&pdAclMap, &recurrences);
364                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to close rec array");
365             }
366             cborEncoderResult = cbor_encoder_close_container(&pdAclArray, &pdAclMap);
367             VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to close acl map");
368
369             pdacl = pdacl->next;
370         }
371         //clsoe the array
372         cborEncoderResult = cbor_encoder_close_container(&pconfMap, &pdAclArray);
373         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to close acl array");
374     }
375
376     //PDDev -- Mandatory
377     //There may not be paired devices if it did not pairing before
378     if (pconf->pddevs && 0 < pconf->pddevLen)
379     {
380         CborEncoder pddev = { {.ptr = NULL }, .end = 0, .added = 0, .flags = 0 };
381         cborEncoderResult = cbor_encode_text_string(&pconfMap, OIC_JSON_PDDEV_LIST_NAME,
382                 strlen(OIC_JSON_PDDEV_LIST_NAME));
383         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to encode pddev");
384         cborEncoderResult = cbor_encoder_create_array(&pconfMap, &pddev,  pconf->pddevLen);
385         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create array");
386
387         for (size_t i = 0; i < pconf->pddevLen; i++)
388         {
389             char *pddevId = NULL;
390             ret = ConvertUuidToStr(&pconf->pddevs[i], &pddevId);
391             VERIFY_SUCCESS(TAG, ret == OC_STACK_OK, ERROR);
392             cborEncoderResult = cbor_encode_text_string(&pddev, pddevId, strlen(pddevId));
393             VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding pddev Id Value.");
394             OICFree(pddevId);
395         }
396         cborEncoderResult = cbor_encoder_close_container(&pconfMap, &pddev);
397         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to close pddev array");
398     }
399
400     //DeviceId -- Mandatory
401     //There may not be devicd id if caller is provisoning tool
402     cborEncoderResult = cbor_encode_text_string(&pconfMap, OIC_JSON_DEVICE_ID_NAME,
403             strlen(OIC_JSON_DEVICE_ID_NAME));
404     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to encode device id");
405     {
406         char *deviceId = NULL;
407         ret = ConvertUuidToStr(&pconf->deviceID, &deviceId);
408         VERIFY_SUCCESS(TAG, ret == OC_STACK_OK, ERROR);
409         cborEncoderResult = cbor_encode_text_string(&pconfMap, deviceId, strlen(deviceId));
410         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to encode deviceID value");
411         OICFree(deviceId);
412     }
413
414     //ROwner -- Mandatory
415     {
416         char *rowner = NULL;
417         cborEncoderResult = cbor_encode_text_string(&pconfMap, OIC_JSON_ROWNERID_NAME,
418                 strlen(OIC_JSON_ROWNERID_NAME));
419         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to encode rowner string");
420         ret = ConvertUuidToStr(&pconf->rownerID, &rowner);
421         VERIFY_SUCCESS(TAG, ret == OC_STACK_OK, ERROR);
422         cborEncoderResult = cbor_encode_text_string(&pconfMap, rowner, strlen(rowner));
423         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to encode rwoner value");
424         OICFree(rowner);
425     }
426
427     cborEncoderResult = cbor_encoder_close_container(&encoder, &pconfMap);
428     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to close pconfMap");
429
430     *size = encoder.ptr - outPayload;
431     *payload = outPayload;
432     ret = OC_STACK_OK;
433 exit:
434     if ((CborErrorOutOfMemory == cborEncoderResult) && (cborLen < CBOR_MAX_SIZE))
435     {
436         // reallocate and try again!
437         OICFree(outPayload);
438         // Since the allocated initial memory failed, double the memory.
439         cborLen += encoder.ptr - encoder.end;
440         cborEncoderResult = CborNoError;
441         ret = PconfToCBORPayload(pconf, payload, &cborLen);
442         *size = cborLen;
443     }
444     if ((CborNoError != cborEncoderResult) || (OC_STACK_OK != ret))
445     {
446         OICFree(outPayload);
447         outPayload = NULL;
448         *payload = NULL;
449         *size = 0;
450         ret = OC_STACK_ERROR;
451     }
452     return ret;
453 }
454
455 OCStackResult CBORPayloadToPconf(const uint8_t *cborPayload, size_t size, OicSecPconf_t **secPconf)
456 {
457     if (NULL == cborPayload || NULL == secPconf || NULL != *secPconf || 0 == size)
458     {
459         return OC_STACK_INVALID_PARAM;
460     }
461     OCStackResult ret = OC_STACK_ERROR;
462     *secPconf = NULL;
463     CborValue pconfCbor = { .parser = NULL };
464     CborParser parser = { .end = NULL };
465     CborError cborFindResult = CborNoError;
466     int cborLen = size;
467
468     cbor_parser_init(cborPayload, cborLen, 0, &parser, &pconfCbor);
469     CborValue pconfMap = { .parser = NULL } ;
470     OicSecPconf_t *pconf = NULL;
471     cborFindResult = cbor_value_enter_container(&pconfCbor, &pconfMap);
472     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to enter map");
473     pconf = (OicSecPconf_t *)OICCalloc(1, sizeof(*pconf));
474     VERIFY_NON_NULL(TAG, pconf, ERROR);
475     while (cbor_value_is_valid(&pconfMap))
476     {
477         char *name = NULL;
478         size_t len = 0;
479         CborType type = cbor_value_get_type(&pconfMap);
480         if (type == CborTextStringType)
481         {
482             cborFindResult = cbor_value_dup_text_string(&pconfMap, &name, &len, NULL);
483             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get value");
484             cborFindResult = cbor_value_advance(&pconfMap);
485             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance value");
486         }
487
488         if (name)
489         {
490             //EDP -- Mandatory
491             if(0 == strcmp(OIC_JSON_EDP_NAME, name))
492             {
493                 cborFindResult = cbor_value_get_boolean(&pconfMap, &pconf->edp);
494                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get value");
495             }
496             if (0 == strcmp(OIC_JSON_PRM_NAME, name))
497             {
498                 int i = 0;
499                 CborValue prm = { .parser = NULL };
500                 cborFindResult = cbor_value_get_array_length(&pconfMap, &pconf->prmLen);
501                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get length");
502                 VERIFY_SUCCESS(TAG, pconf->prmLen != 0, ERROR);
503
504                 pconf->prm = (OicSecPrm_t *)OICCalloc(pconf->prmLen, sizeof(OicSecPrm_t));
505                 VERIFY_NON_NULL(TAG, pconf->prm, ERROR);
506                 cborFindResult = cbor_value_enter_container(&pconfMap, &prm);
507                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to eneter array");
508
509                 while (cbor_value_is_valid(&prm))
510                 {
511                     cborFindResult = cbor_value_get_int(&prm, (int *)&pconf->prm[i++]);
512                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get value");
513                     cborFindResult = cbor_value_advance(&prm);
514                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance value");
515                 }
516             }
517             //PIN -- Mandatory
518             if (0 == strcmp(OIC_JSON_PIN_NAME, name))
519             {
520                 uint8_t *pin = NULL;
521                 cborFindResult = cbor_value_dup_byte_string(&pconfMap, &pin, &len, NULL);
522                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get value");
523                 memcpy(pconf->pin.val, pin, len);
524                 OICFree(pin);
525             }
526
527             //PDACL -- Mandatory
528             if (0 == strcmp(OIC_JSON_PDACL_NAME, name))
529             {
530                 CborValue pdAclArray = { .parser = NULL};
531                 OicSecPdAcl_t *headPdacl = NULL;
532
533                 cborFindResult = cbor_value_enter_container(&pconfMap, &pdAclArray);
534                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to enter");
535
536                 while (cbor_value_is_valid(&pdAclArray))
537                 {
538                     CborValue pdAclMap = { .parser = NULL};
539                     OicSecPdAcl_t *pdacl = (OicSecPdAcl_t *) OICCalloc(1, sizeof(OicSecPdAcl_t));
540                     VERIFY_NON_NULL(TAG, pdacl, ERROR);
541
542                     cborFindResult = cbor_value_enter_container(&pdAclArray, &pdAclMap);
543                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to enter");
544
545                     while (cbor_value_is_valid(&pdAclMap))
546                     {
547                         char* name = NULL;
548                         size_t len = 0;
549                         CborType type = cbor_value_get_type(&pdAclMap);
550                         if (type == CborTextStringType)
551                         {
552                             cborFindResult = cbor_value_dup_text_string(&pdAclMap, &name,
553                                     &len, NULL);
554                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get text");
555                             cborFindResult = cbor_value_advance(&pdAclMap);
556                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance value");
557                         }
558                         if (name)
559                         {
560                             // Resources -- Mandatory
561                             if (strcmp(name, OIC_JSON_RESOURCES_NAME) == 0)
562                             {
563                                 int i = 0;
564                                 CborValue resources = { .parser = NULL };
565                                 cborFindResult = cbor_value_get_array_length(&pdAclMap,
566                                         &pdacl->resourcesLen);
567                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get length");
568                                 cborFindResult = cbor_value_enter_container(&pdAclMap, &resources);
569                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to enter");
570                                 pdacl->resources = (char **) OICCalloc(pdacl->resourcesLen,
571                                         sizeof(char*));
572                                 VERIFY_NON_NULL(TAG, pdacl->resources, ERROR);
573
574                                 while (cbor_value_is_valid(&resources))
575                                 {
576                                     CborValue rMap = { .parser = NULL  };
577                                     cborFindResult = cbor_value_enter_container(&resources, &rMap);
578                                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering Resource Map");
579
580                                     while(cbor_value_is_valid(&rMap))
581                                     {
582                                         char *rMapName = NULL;
583                                         size_t rMapNameLen = 0;
584                                         cborFindResult = cbor_value_dup_text_string(&rMap, &rMapName, &rMapNameLen, NULL);
585                                         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding RMap Data Name Tag.");
586                                         cborFindResult = cbor_value_advance(&rMap);
587                                         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding RMap Data Value.");
588
589                                         // "href"
590                                         if (0 == strcmp(OIC_JSON_HREF_NAME, rMapName))
591                                         {
592                                             // TODO : Need to check data structure of OicSecPdAcl_t based on RAML spec.
593                                             cborFindResult = cbor_value_dup_text_string(&rMap, &pdacl->resources[i++], &len, NULL);
594                                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Href Value.");
595                                         }
596
597                                         // "rel"
598                                         if (0 == strcmp(OIC_JSON_REL_NAME, rMapName))
599                                         {
600                                             // TODO : Need to check data structure of OicSecPdAcl_t and assign based on RAML spec.
601                                             char *relData = NULL;
602                                             cborFindResult = cbor_value_dup_text_string(&rMap, &relData, &len, NULL);
603                                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding REL Value.");
604                                             OICFree(relData);
605                                         }
606
607                                         // "rt"
608                                         if (0 == strcmp(OIC_JSON_RT_NAME, rMapName))
609                                         {
610                                             // TODO : Need to check data structure of OicSecPdAcl_t and assign based on RAML spec.
611                                             char *rtData = NULL;
612                                             cborFindResult = cbor_value_dup_text_string(&rMap, &rtData, &len, NULL);
613                                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding RT Value.");
614                                             OICFree(rtData);
615                                         }
616
617                                         // "if"
618                                         if (0 == strcmp(OIC_JSON_IF_NAME, rMapName))
619                                         {
620                                             // TODO : Need to check data structure of OicSecPdAcl_t and assign based on RAML spec.
621                                             char *ifData = NULL;
622                                             cborFindResult = cbor_value_dup_text_string(&rMap, &ifData, &len, NULL);
623                                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding IF Value.");
624                                             OICFree(ifData);
625                                         }
626
627                                         if (cbor_value_is_valid(&rMap))
628                                         {
629                                             cborFindResult = cbor_value_advance(&rMap);
630                                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing Rlist Map.");
631                                         }
632                                         OICFree(rMapName);
633                                     }
634
635                                     if (cbor_value_is_valid(&resources))
636                                     {
637                                         cborFindResult = cbor_value_advance(&resources);
638                                         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance");
639                                     }
640                                 }
641                             }
642
643                             // Permissions -- Mandatory
644                             if (strcmp(name, OIC_JSON_PERMISSION_NAME) == 0)
645                             {
646                                 cborFindResult = cbor_value_get_uint64(&pdAclMap,
647                                         (uint64_t *) &pdacl->permission);
648                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get value");
649                             }
650
651                             // Period -- Not mandatory
652                             if (strcmp(name, OIC_JSON_PERIODS_NAME) == 0)
653                             {
654                                 int i = 0;
655                                 CborValue period = { .parser = NULL };
656                                 cborFindResult = cbor_value_get_array_length(&pdAclMap,
657                                         &pdacl->prdRecrLen);
658                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get length");
659                                 cborFindResult = cbor_value_enter_container(&pdAclMap, &period);
660                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to enter");
661                                 pdacl->periods = (char **) OICCalloc(pdacl->prdRecrLen, sizeof(char*));
662                                 VERIFY_NON_NULL(TAG, pdacl->periods, ERROR);
663
664                                 while (cbor_value_is_text_string(&period))
665                                 {
666                                     cborFindResult = cbor_value_dup_text_string(&period,
667                                             &pdacl->periods[i++], &len, NULL);
668                                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get text");
669                                     cborFindResult = cbor_value_advance(&period);
670                                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance");
671                                     pdacl->prdRecrLen++;
672                                 }
673                             }
674
675                             // Recurrence -- Not mandatory
676                             if (strcmp(name, OIC_JSON_RECURRENCES_NAME) == 0)
677                             {
678                                 int i = 0;
679                                 CborValue recurrences = { .parser = NULL };
680                                 cborFindResult = cbor_value_get_array_length(&pdAclMap, &pdacl->prdRecrLen);
681                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get length");
682                                 cborFindResult = cbor_value_enter_container(&pdAclMap, &recurrences);
683                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to enter");
684                                 pdacl->recurrences = (char **) OICCalloc(pdacl->prdRecrLen, sizeof(char*));
685                                 VERIFY_NON_NULL(TAG, pdacl->recurrences, ERROR);
686
687                                 while (cbor_value_is_text_string(&recurrences))
688                                 {
689                                     cborFindResult = cbor_value_dup_text_string(&recurrences,
690                                             &pdacl->recurrences[i++], &len, NULL);
691                                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get value");
692                                     cborFindResult = cbor_value_advance(&recurrences);
693                                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance");
694                                 }
695                             }
696                             if (type != CborMapType && cbor_value_is_valid(&pdAclMap))
697                             {
698                                 cborFindResult = cbor_value_advance(&pdAclMap);
699                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance");
700                             }
701                         }
702                         if (cbor_value_is_valid(&pdAclArray))
703                         {
704                             cborFindResult = cbor_value_advance(&pdAclArray);
705                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance");
706                         }
707                         OICFree(name);
708                         name = NULL;
709                     }
710                     pdacl->next = NULL;
711                     if (headPdacl == NULL)
712                     {
713                         headPdacl = pdacl;
714                     }
715                     else
716                     {
717                         OicSecPdAcl_t *temp = headPdacl;
718                         while (temp->next)
719                         {
720                             temp = temp->next;
721                         }
722                         temp->next = pdacl;
723                     }
724                 }
725                 pconf->pdacls = headPdacl;
726             }
727
728             //PDDev -- Mandatory
729             if (strcmp(name, OIC_JSON_PDDEV_LIST_NAME) == 0)
730             {
731                 int i = 0;
732                 CborValue pddevs = { .parser = NULL };
733                 cborFindResult = cbor_value_get_array_length(&pconfMap, &pconf->pddevLen);
734                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get length");
735                 cborFindResult = cbor_value_enter_container(&pconfMap, &pddevs);
736                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to enter");
737
738                 pconf->pddevs = (OicUuid_t *)OICMalloc(pconf->pddevLen * sizeof(OicUuid_t));
739                 VERIFY_NON_NULL(TAG, pconf->pddevs, ERROR);
740                 while (cbor_value_is_valid(&pddevs))
741                 {
742                     char *pddev = NULL;
743                     cborFindResult = cbor_value_dup_text_string(&pddevs, &pddev, &len, NULL);
744                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get value");
745                     cborFindResult = cbor_value_advance(&pddevs);
746                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance");
747                     ret = ConvertStrToUuid(pddev, &pconf->pddevs[i++]);
748                     VERIFY_SUCCESS(TAG, ret == OC_STACK_OK, ERROR);
749                     OICFree(pddev);
750                 }
751             }
752
753             //Mandatory - Device Id
754             if (0 == strcmp(OIC_JSON_DEVICE_ID_NAME, name))
755             {
756                 char *deviceId = NULL;
757                 cborFindResult = cbor_value_dup_text_string(&pconfMap, &deviceId, &len, NULL);
758                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get deviceID");
759                 ret = ConvertStrToUuid(deviceId, &pconf->deviceID);
760                 VERIFY_SUCCESS(TAG, ret == OC_STACK_OK, ERROR);
761                 OICFree(deviceId);
762             }
763
764             // ROwner -- Mandatory
765             if (0 == strcmp(OIC_JSON_ROWNERID_NAME, name))
766             {
767                 char *rowner = NULL;
768                 cborFindResult = cbor_value_dup_text_string(&pconfMap, &rowner, &len, NULL);
769                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get rowner");
770                 ret = ConvertStrToUuid(rowner, &pconf->rownerID);
771                 VERIFY_SUCCESS(TAG, ret == OC_STACK_OK, ERROR);
772                 OICFree(rowner);
773             }
774         }
775         if (CborMapType != type && cbor_value_is_valid(&pconfMap))
776         {
777             cborFindResult = cbor_value_advance(&pconfMap);
778             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance");
779         }
780         OICFree(name);
781         name = NULL;
782     }
783     *secPconf=pconf;
784     ret = OC_STACK_OK;
785 exit:
786     if (CborNoError != cborFindResult)
787     {
788         OIC_LOG (ERROR, TAG, "CBORPayloadToPconf failed");
789         DeletePconfBinData(pconf);
790         pconf = NULL;
791         *secPconf = NULL;
792         ret = OC_STACK_ERROR;
793     }
794     return ret;
795 }
796
797 static bool UpdatePersistentStorage(const OicSecPconf_t * pconf)
798 {
799     bool ret = false;
800
801     // Convert PCONF data into Cborpayload for update to persistent storage
802     uint8_t *payload = NULL;
803     size_t size = 0;
804     if (OC_STACK_OK == PconfToCBORPayload(pconf, &payload, &size) && NULL !=payload)
805     {
806         if (OC_STACK_OK == UpdateSecureResourceInPS(OIC_JSON_PCONF_NAME, payload, size))
807         {
808             ret = true;
809         }
810         OICFree(payload);
811     }
812     return ret;
813 }
814
815 static OCEntityHandlerResult HandlePconfGetRequest (const OCEntityHandlerRequest * ehRequest)
816 {
817     uint8_t* payload = NULL;
818     size_t size = 0;
819     OCEntityHandlerResult ehRet = OC_EH_OK;
820
821     OicSecPconf_t pconf;
822     memset(&pconf, 0, sizeof(OicSecPconf_t));
823
824     OIC_LOG (DEBUG, TAG, "Pconf EntityHandle processing GET request");
825
826     if (true == GetDoxmResourceData()->dpc)
827     {
828         //Making response elements for Get request
829         if( (true == gPconf->edp) &&
830             (gPconf->prm && 0 < gPconf->prmLen) &&
831             (0 < strlen((const char*)gPconf->deviceID.id)) &&
832             (0 < strlen((const char*)gPconf->rownerID.id)))
833         {
834             pconf.edp = true;
835             pconf.prm = gPconf->prm;
836             pconf.prmLen = gPconf->prmLen;
837             memcpy(&pconf.deviceID, &gPconf->deviceID, sizeof(OicUuid_t));
838             memcpy(&pconf.rownerID, &gPconf->rownerID, sizeof(OicUuid_t));
839             OIC_LOG (DEBUG, TAG, "PCONF - direct pairing enabled");
840         }
841         else if (false == gPconf->edp)
842         {
843             pconf.edp = false;
844             memcpy(&pconf.rownerID, &gPconf->rownerID, sizeof(OicUuid_t));
845             OIC_LOG (DEBUG, TAG, "PCONF - direct pairing disable");
846         }
847         else
848         {
849             ehRet= OC_EH_ERROR;
850             OIC_LOG (DEBUG, TAG, "PCONF - error");
851         }
852     }
853     else
854     {
855         OIC_LOG (DEBUG, TAG, "DPC == false : Direct-Pairing Disabled");
856     }
857
858
859     if (OC_STACK_OK != PconfToCBORPayload(gPconf, &payload, &size))
860     {
861         ehRet = OC_EH_ERROR;
862     }
863
864     if(OC_EH_OK == ehRet)
865     {
866         ehRet = (payload ? OC_EH_OK : OC_EH_ERROR);
867     }
868     else
869     {
870         OICFree(payload);
871         payload = NULL;
872         size = 0;
873     }
874
875     // Send response payload to request originator
876     SendSRMCBORResponse(ehRequest, ehRet, payload, size);
877     OIC_LOG_V(DEBUG, TAG, "%s RetVal %d", __func__, ehRet);
878
879     return ehRet;
880 }
881
882 static OCEntityHandlerResult HandlePconfPostRequest (const OCEntityHandlerRequest * ehRequest)
883 {
884     OCEntityHandlerResult ehRet = OC_EH_OK;
885     OCStackResult res=OC_STACK_OK;
886     OicSecPconf_t* newPconf = NULL;
887
888     if (true == GetDoxmResourceData()->dpc)
889     {
890         // Convert CBOR PCONF data into binary. This will also validate the PCONF data received.
891         uint8_t *payload = ((OCSecurityPayload *) ehRequest->payload)->securityData1;
892         size_t size = ((OCSecurityPayload *) ehRequest->payload)->payloadSize;
893
894         if(payload){
895             res = CBORPayloadToPconf(payload, size, &newPconf);
896         }
897     }
898     else
899     {
900         OIC_LOG (DEBUG, TAG, "DPC == false : Direct-Pairing Disabled");
901         ehRet = OC_EH_ERROR;
902     }
903
904     if (newPconf && res == OC_STACK_OK)
905     {
906         // Check if valid Post request
907         if ((true == newPconf->edp) && (0 < newPconf->prmLen) &&
908                 DP_PIN_LENGTH == sizeof((const char*)newPconf->pin.val))
909         {
910             OicSecPrm_t *oldPrm = gPconf->prm;
911             OicSecPdAcl_t *oldPdacl = gPconf->pdacls;
912
913             // Update local PCONF with new PCONF
914             gPconf->edp = true;
915             memcpy(&gPconf->pin, &newPconf->pin, sizeof(OicDpPin_t));
916             gPconf->prm = newPconf->prm;
917             gPconf->prmLen = newPconf->prmLen;
918             gPconf->pdacls = newPconf->pdacls;
919             memcpy(&gPconf->rownerID, &newPconf->rownerID, sizeof(OicUuid_t));
920
921             // to delete old value(prm, pdacl)
922             newPconf->prm = oldPrm;
923             newPconf->pdacls = oldPdacl;
924         }
925         else if (false == newPconf->edp)
926         {
927             gPconf->edp = false;
928         }
929         else
930         {
931             ehRet = OC_EH_ERROR;
932         }
933
934         // Update storage
935         if(OC_EH_ERROR != ehRet && true == UpdatePersistentStorage(gPconf))
936         {
937             ehRet = OC_EH_RESOURCE_CREATED;
938         }
939
940         DeletePconfBinData(newPconf);
941     }
942     else
943     {
944         ehRet = OC_EH_ERROR;
945     }
946
947     // Send payload to request originator
948     SendSRMCBORResponse(ehRequest, ehRet, NULL, 0);
949
950     OIC_LOG_V (DEBUG, TAG, "%s RetVal %d", __func__ , ehRet);
951     return ehRet;
952 }
953
954 /*
955  * This internal method is the entity handler for PCONF resources and
956  * will handle REST request (POST) for them.
957  */
958 OCEntityHandlerResult PconfEntityHandler (OCEntityHandlerFlag flag,
959                                         OCEntityHandlerRequest * ehRequest,
960                                         void* callbackParameter)
961 {
962     OIC_LOG(DEBUG, TAG, "Received request PconfEntityHandler");
963     (void)callbackParameter;
964     OCEntityHandlerResult ehRet = OC_EH_ERROR;
965
966     if (!ehRequest)
967     {
968         return ehRet;
969     }
970
971     if (flag & OC_REQUEST_FLAG)
972     {
973         OIC_LOG (DEBUG, TAG, "Flag includes OC_REQUEST_FLAG");
974         switch (ehRequest->method)
975         {
976             case OC_REST_GET:
977                 ehRet = HandlePconfGetRequest(ehRequest);
978                 break;
979
980             case OC_REST_POST:
981                 ehRet = HandlePconfPostRequest(ehRequest);
982                 break;
983
984             case OC_REST_DELETE:
985                 break;
986
987             default:
988                 ehRet = OC_EH_ERROR;
989                 SendSRMCBORResponse(ehRequest, ehRet, NULL, 0);
990         }
991     }
992
993     return ehRet;
994 }
995
996 /*
997  * This internal method is used to create '/oic/sec/pconf' resource.
998  */
999 OCStackResult CreatePconfResource()
1000 {
1001     OCStackResult ret;
1002
1003     ret = OCCreateResource(&gPconfHandle,
1004                            OIC_RSRC_TYPE_SEC_PCONF,
1005                            OIC_MI_DEF,
1006                            OIC_RSRC_PCONF_URI,
1007                            PconfEntityHandler,
1008                            NULL,
1009                            OC_SECURE | OC_EXPLICIT_DISCOVERABLE);
1010
1011     if (OC_STACK_OK != ret)
1012     {
1013         OIC_LOG (ERROR, TAG, "Unable to instantiate PCONF resource");
1014         DeInitPconfResource();
1015     }
1016     return ret;
1017 }
1018
1019 /**
1020  * Get the default value.
1021  * @retval  the gDefaultPconf pointer;
1022  */
1023 static OicSecPconf_t* GetPconfDefault()
1024 {
1025     OIC_LOG (DEBUG, TAG, "GetPconfDefault");
1026
1027     return &gDefaultPconf;
1028 }
1029
1030 /**
1031  * This method is used by SRM to retrieve PCONF resource data..
1032  *
1033  * @retval  reference to @ref OicSecPconf_t, binary format of Pconf resource data
1034  */
1035 const OicSecPconf_t* GetPconfResourceData()
1036 {
1037     return gPconf;
1038 }
1039
1040 /**
1041  * Initialize PCONF resource by loading data from persistent storage.
1042  *
1043  * @retval  OC_STACK_OK for Success, otherwise some error value
1044  */
1045 OCStackResult InitPconfResource()
1046 {
1047     OCStackResult ret = OC_STACK_ERROR;
1048
1049     uint8_t *data = NULL;
1050     size_t size = 0;
1051
1052     ret = GetSecureVirtualDatabaseFromPS(OIC_JSON_PCONF_NAME, &data, &size);
1053     // If database read failed
1054     if (ret != OC_STACK_OK)
1055     {
1056         OIC_LOG(DEBUG, TAG, "ReadSVDataFromPS failed");
1057     }
1058     if (data)
1059     {
1060         CBORPayloadToPconf(data, size, &gPconf);
1061     }
1062
1063     if (!data || !gPconf)
1064     {
1065         gPconf = GetPconfDefault();
1066
1067         // device id from doxm
1068         OicUuid_t deviceId = {.id = {0}};
1069         OCStackResult ret = GetDoxmDeviceID( &deviceId);
1070         VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
1071         memcpy(&gPconf->deviceID, &deviceId, sizeof(OicUuid_t));
1072     }
1073     VERIFY_NON_NULL(TAG, gPconf, ERROR);
1074
1075     // Instantiate 'oic.sec.pconf'
1076     ret = CreatePconfResource();
1077
1078 exit:
1079     if (OC_STACK_OK != ret)
1080     {
1081         DeInitPconfResource();
1082     }
1083     OICFree(data);
1084     return ret;
1085 }
1086
1087 /**
1088  * Perform cleanup for PCONF resources.
1089  *
1090  * @return
1091  * OC_STACK_OK    - no error
1092  * OC_STACK_ERROR - stack process error
1093  *
1094  */
1095 OCStackResult DeInitPconfResource()
1096 {
1097     OCStackResult ret = OCDeleteResource(gPconfHandle);
1098     if(gPconf!= &gDefaultPconf)
1099     {
1100         DeletePconfBinData(gPconf);
1101     }
1102     gPconf = NULL;
1103
1104     if(OC_STACK_OK == ret)
1105     {
1106         return OC_STACK_OK;
1107     }
1108     else
1109     {
1110         return OC_STACK_ERROR;
1111     }
1112 }
1113
1114 /**
1115  * This method might be used to add a paired device id after direct-pairing process complete.
1116  *
1117  * @param pdeviceId ID of the paired device.
1118  *
1119  * @retval  OC_STACK_OK for Success, otherwise some error value
1120  */
1121 OCStackResult AddPairedDevice(OicUuid_t *pdeviceId)
1122 {
1123     if (!gPconf || !pdeviceId)
1124     {
1125         return OC_STACK_INVALID_PARAM;
1126     }
1127
1128
1129     OicUuid_t *prevList = gPconf->pddevs;
1130     gPconf->pddevs = (OicUuid_t *)OICCalloc(gPconf->pddevLen+1, sizeof(OicUuid_t));
1131     if(!gPconf->pddevs)
1132     {
1133         return OC_STACK_NO_MEMORY;
1134     }
1135     for (size_t i=0; i<gPconf->pddevLen; i++)
1136     {
1137         memcpy(&gPconf->pddevs[i], &prevList[i], sizeof(OicUuid_t));
1138     }
1139
1140     // add new paired device id
1141     memcpy(&gPconf->pddevs[gPconf->pddevLen], pdeviceId, sizeof(OicUuid_t));
1142     gPconf->pddevLen++;
1143
1144     // Update storage
1145     if(true != UpdatePersistentStorage(gPconf))
1146     {
1147         OIC_LOG (ERROR, TAG, "Fail to update pconf resource");
1148         return OC_STACK_ERROR;
1149     }
1150
1151     OIC_LOG (ERROR, TAG, "Add paired device success");
1152     return OC_STACK_OK;
1153 }
1154
1155 /**
1156  * This method might be used by PolicyEngine to retrieve PDACL for a Subject.
1157  *
1158  * @param subjectId ID of the subject for which PDACL is required.
1159  * @param savePtr is used internally by @ref GetACLResourceData to maintain index between
1160  *                successive calls for same subjectId.
1161  *
1162  * @retval  reference to @ref OicSecPdAcl_t if PDACL is found, else NULL
1163  */
1164 const OicSecPdAcl_t* GetPdAclData(const OicUuid_t* subjectId, OicSecPdAcl_t **savePtr)
1165 {
1166     OicSecPdAcl_t *pdacl = NULL;
1167
1168     if ( NULL == subjectId)
1169     {
1170         return NULL;
1171     }
1172
1173     /*
1174      * savePtr MUST point to NULL if this is the 'first' call to retrieve PDACL for
1175      * subjectID.
1176      */
1177     if (NULL == *savePtr)
1178     {
1179         pdacl = gPconf->pdacls;
1180
1181         // Find if 'subjectID' is in paired device list.
1182         for(size_t i=0; i<gPconf->pddevLen; i++)
1183         {
1184             if (memcmp(&(gPconf->pddevs[i]), subjectId, sizeof(OicUuid_t)) == 0)
1185             {
1186                 *savePtr = pdacl;
1187                 return pdacl;
1188             }
1189         }
1190     }
1191     else
1192     {
1193         OicSecPdAcl_t *temp = NULL;
1194
1195         /*
1196          * If this is a 'successive' call, search for location pointed by
1197          * savePtr and assign 'begin' to the next PDACL after it in the linked
1198          * list and start searching from there.
1199          */
1200         LL_FOREACH(gPconf->pdacls, temp)
1201         {
1202             if (temp == *savePtr)
1203             {
1204                 pdacl = temp->next;
1205                 *savePtr = pdacl;
1206                 return pdacl;
1207             }
1208         }
1209     }
1210
1211     // Cleanup in case no PDACL is found
1212     *savePtr = NULL;
1213     return NULL;
1214 }
1215
1216 /**
1217  * This method return whether device is paired or not.
1218  *
1219  * @param pdeviceId Target device ID to find in paired list.
1220  * @retval  ture if device is already paired, else false
1221  */
1222 bool IsPairedDevice(const OicUuid_t* pdeviceId)
1223 {
1224     // Find if 'pdeviceId' is in paired device list.
1225     for(size_t i=0; i<gPconf->pddevLen; i++)
1226     {
1227         if (memcmp(&(gPconf->pddevs[i]), pdeviceId, sizeof(OicUuid_t)) == 0)
1228         {
1229             return true;
1230         }
1231     }
1232     return false;
1233 }
1234
1235 OCStackResult SetPconfRownerId(const OicUuid_t* newROwner)
1236 {
1237     OCStackResult ret = OC_STACK_ERROR;
1238     uint8_t *cborPayload = NULL;
1239     size_t size = 0;
1240     OicUuid_t prevId = {.id={0}};
1241
1242     if(NULL == newROwner)
1243     {
1244         ret = OC_STACK_INVALID_PARAM;
1245     }
1246     if(NULL == gPconf)
1247     {
1248         ret = OC_STACK_NO_RESOURCE;
1249     }
1250
1251     if(newROwner && gPconf)
1252     {
1253         memcpy(prevId.id, gPconf->rownerID.id, sizeof(prevId.id));
1254         memcpy(gPconf->rownerID.id, newROwner->id, sizeof(newROwner->id));
1255
1256         ret = PconfToCBORPayload(gPconf, &cborPayload, &size);
1257         VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
1258
1259         ret = UpdateSecureResourceInPS(OIC_JSON_PCONF_NAME, cborPayload, size);
1260         VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
1261
1262         OICFree(cborPayload);
1263     }
1264
1265     return ret;
1266
1267 exit:
1268     OICFree(cborPayload);
1269     memcpy(gPconf->rownerID.id, prevId.id, sizeof(prevId.id));
1270     return ret;
1271 }
1272