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