Imported Upstream version 1.1.0
[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;
172     CborEncoder pconfMap;
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;
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;
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;
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;
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;
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;
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;
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;
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
467     cbor_parser_init(cborPayload, size, 0, &parser, &pconfCbor);
468     CborValue pconfMap = { .parser = NULL } ;
469     OicSecPconf_t *pconf = NULL;
470     cborFindResult = cbor_value_enter_container(&pconfCbor, &pconfMap);
471     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to enter map");
472     pconf = (OicSecPconf_t *)OICCalloc(1, sizeof(*pconf));
473     VERIFY_NON_NULL(TAG, pconf, ERROR);
474     while (cbor_value_is_valid(&pconfMap))
475     {
476         char *name = NULL;
477         size_t len = 0;
478         CborType type = cbor_value_get_type(&pconfMap);
479         if (type == CborTextStringType)
480         {
481             cborFindResult = cbor_value_dup_text_string(&pconfMap, &name, &len, NULL);
482             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get value");
483             cborFindResult = cbor_value_advance(&pconfMap);
484             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance value");
485         }
486
487         if (name)
488         {
489             //EDP -- Mandatory
490             if(0 == strcmp(OIC_JSON_EDP_NAME, name))
491             {
492                 cborFindResult = cbor_value_get_boolean(&pconfMap, &pconf->edp);
493                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get value");
494             }
495             if (0 == strcmp(OIC_JSON_PRM_NAME, name))
496             {
497                 int i = 0;
498                 CborValue prm = { .parser = NULL };
499                 cborFindResult = cbor_value_get_array_length(&pconfMap, &pconf->prmLen);
500                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get length");
501                 VERIFY_SUCCESS(TAG, pconf->prmLen != 0, ERROR);
502
503                 pconf->prm = (OicSecPrm_t *)OICCalloc(pconf->prmLen, sizeof(OicSecPrm_t));
504                 VERIFY_NON_NULL(TAG, pconf->prm, ERROR);
505                 cborFindResult = cbor_value_enter_container(&pconfMap, &prm);
506                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to eneter array");
507
508                 while (cbor_value_is_valid(&prm))
509                 {
510                     cborFindResult = cbor_value_get_int(&prm, (int *)&pconf->prm[i++]);
511                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get value");
512                     cborFindResult = cbor_value_advance(&prm);
513                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance value");
514                 }
515             }
516             //PIN -- Mandatory
517             if (0 == strcmp(OIC_JSON_PIN_NAME, name))
518             {
519                 uint8_t *pin = NULL;
520                 cborFindResult = cbor_value_dup_byte_string(&pconfMap, &pin, &len, NULL);
521                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get value");
522                 memcpy(pconf->pin.val, pin, len);
523                 OICFree(pin);
524             }
525
526             //PDACL -- Mandatory
527             if (0 == strcmp(OIC_JSON_PDACL_NAME, name))
528             {
529                 CborValue pdAclArray = { .parser = NULL};
530                 OicSecPdAcl_t *headPdacl = NULL;
531
532                 cborFindResult = cbor_value_enter_container(&pconfMap, &pdAclArray);
533                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to enter");
534
535                 while (cbor_value_is_valid(&pdAclArray))
536                 {
537                     CborValue pdAclMap = { .parser = NULL};
538                     OicSecPdAcl_t *pdacl = (OicSecPdAcl_t *) OICCalloc(1, sizeof(OicSecPdAcl_t));
539                     VERIFY_NON_NULL(TAG, pdacl, ERROR);
540
541                     cborFindResult = cbor_value_enter_container(&pdAclArray, &pdAclMap);
542                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to enter");
543
544                     while (cbor_value_is_valid(&pdAclMap))
545                     {
546                         char* name = NULL;
547                         size_t len = 0;
548                         CborType type = cbor_value_get_type(&pdAclMap);
549                         if (type == CborTextStringType)
550                         {
551                             cborFindResult = cbor_value_dup_text_string(&pdAclMap, &name,
552                                     &len, NULL);
553                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get text");
554                             cborFindResult = cbor_value_advance(&pdAclMap);
555                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance value");
556                         }
557                         if (name)
558                         {
559                             // Resources -- Mandatory
560                             if (strcmp(name, OIC_JSON_RESOURCES_NAME) == 0)
561                             {
562                                 int i = 0;
563                                 CborValue resources = { .parser = NULL };
564                                 cborFindResult = cbor_value_get_array_length(&pdAclMap,
565                                         &pdacl->resourcesLen);
566                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get length");
567                                 cborFindResult = cbor_value_enter_container(&pdAclMap, &resources);
568                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to enter");
569                                 pdacl->resources = (char **) OICCalloc(pdacl->resourcesLen,
570                                         sizeof(char*));
571                                 VERIFY_NON_NULL(TAG, pdacl->resources, ERROR);
572
573                                 while (cbor_value_is_valid(&resources))
574                                 {
575                                     CborValue rMap = { .parser = NULL  };
576                                     cborFindResult = cbor_value_enter_container(&resources, &rMap);
577                                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering Resource Map");
578
579                                     while(cbor_value_is_valid(&rMap))
580                                     {
581                                         char *rMapName = NULL;
582                                         size_t rMapNameLen = 0;
583                                         cborFindResult = cbor_value_dup_text_string(&rMap, &rMapName, &rMapNameLen, NULL);
584                                         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding RMap Data Name Tag.");
585                                         cborFindResult = cbor_value_advance(&rMap);
586                                         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding RMap Data Value.");
587
588                                         // "href"
589                                         if (0 == strcmp(OIC_JSON_HREF_NAME, rMapName))
590                                         {
591                                             // TODO : Need to check data structure of OicSecPdAcl_t based on RAML spec.
592                                             cborFindResult = cbor_value_dup_text_string(&rMap, &pdacl->resources[i++], &len, NULL);
593                                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Href Value.");
594                                         }
595
596                                         // "rel"
597                                         if (0 == strcmp(OIC_JSON_REL_NAME, rMapName))
598                                         {
599                                             // TODO : Need to check data structure of OicSecPdAcl_t and assign based on RAML spec.
600                                             char *relData = NULL;
601                                             cborFindResult = cbor_value_dup_text_string(&rMap, &relData, &len, NULL);
602                                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding REL Value.");
603                                             OICFree(relData);
604                                         }
605
606                                         // "rt"
607                                         if (0 == strcmp(OIC_JSON_RT_NAME, rMapName))
608                                         {
609                                             // TODO : Need to check data structure of OicSecPdAcl_t and assign based on RAML spec.
610                                             char *rtData = NULL;
611                                             cborFindResult = cbor_value_dup_text_string(&rMap, &rtData, &len, NULL);
612                                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding RT Value.");
613                                             OICFree(rtData);
614                                         }
615
616                                         // "if"
617                                         if (0 == strcmp(OIC_JSON_IF_NAME, rMapName))
618                                         {
619                                             // TODO : Need to check data structure of OicSecPdAcl_t and assign based on RAML spec.
620                                             char *ifData = NULL;
621                                             cborFindResult = cbor_value_dup_text_string(&rMap, &ifData, &len, NULL);
622                                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding IF Value.");
623                                             OICFree(ifData);
624                                         }
625
626                                         if (cbor_value_is_valid(&rMap))
627                                         {
628                                             cborFindResult = cbor_value_advance(&rMap);
629                                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing Rlist Map.");
630                                         }
631                                         OICFree(rMapName);
632                                     }
633
634                                     if (cbor_value_is_valid(&resources))
635                                     {
636                                         cborFindResult = cbor_value_advance(&resources);
637                                         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance");
638                                     }
639                                 }
640                             }
641
642                             // Permissions -- Mandatory
643                             if (strcmp(name, OIC_JSON_PERMISSION_NAME) == 0)
644                             {
645                                 cborFindResult = cbor_value_get_uint64(&pdAclMap,
646                                         (uint64_t *) &pdacl->permission);
647                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get value");
648                             }
649
650                             // Period -- Not mandatory
651                             if (strcmp(name, OIC_JSON_PERIODS_NAME) == 0)
652                             {
653                                 int i = 0;
654                                 CborValue period = { .parser = NULL };
655                                 cborFindResult = cbor_value_get_array_length(&pdAclMap,
656                                         &pdacl->prdRecrLen);
657                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get length");
658                                 cborFindResult = cbor_value_enter_container(&pdAclMap, &period);
659                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to enter");
660                                 pdacl->periods = (char **) OICCalloc(pdacl->prdRecrLen, sizeof(char*));
661                                 VERIFY_NON_NULL(TAG, pdacl->periods, ERROR);
662
663                                 while (cbor_value_is_text_string(&period))
664                                 {
665                                     cborFindResult = cbor_value_dup_text_string(&period,
666                                             &pdacl->periods[i++], &len, NULL);
667                                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get text");
668                                     cborFindResult = cbor_value_advance(&period);
669                                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance");
670                                     pdacl->prdRecrLen++;
671                                 }
672                             }
673
674                             // Recurrence -- Not mandatory
675                             if (strcmp(name, OIC_JSON_RECURRENCES_NAME) == 0)
676                             {
677                                 int i = 0;
678                                 CborValue recurrences = { .parser = NULL };
679                                 cborFindResult = cbor_value_get_array_length(&pdAclMap, &pdacl->prdRecrLen);
680                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get length");
681                                 cborFindResult = cbor_value_enter_container(&pdAclMap, &recurrences);
682                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to enter");
683                                 pdacl->recurrences = (char **) OICCalloc(pdacl->prdRecrLen, sizeof(char*));
684                                 VERIFY_NON_NULL(TAG, pdacl->recurrences, ERROR);
685
686                                 while (cbor_value_is_text_string(&recurrences))
687                                 {
688                                     cborFindResult = cbor_value_dup_text_string(&recurrences,
689                                             &pdacl->recurrences[i++], &len, NULL);
690                                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get value");
691                                     cborFindResult = cbor_value_advance(&recurrences);
692                                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance");
693                                 }
694                             }
695                             if (type != CborMapType && cbor_value_is_valid(&pdAclMap))
696                             {
697                                 cborFindResult = cbor_value_advance(&pdAclMap);
698                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance");
699                             }
700                         }
701                         if (cbor_value_is_valid(&pdAclArray))
702                         {
703                             cborFindResult = cbor_value_advance(&pdAclArray);
704                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance");
705                         }
706                         OICFree(name);
707                         name = NULL;
708                     }
709                     pdacl->next = NULL;
710                     if (headPdacl == NULL)
711                     {
712                         headPdacl = pdacl;
713                     }
714                     else
715                     {
716                         OicSecPdAcl_t *temp = headPdacl;
717                         while (temp->next)
718                         {
719                             temp = temp->next;
720                         }
721                         temp->next = pdacl;
722                     }
723                 }
724                 pconf->pdacls = headPdacl;
725             }
726
727             //PDDev -- Mandatory
728             if (strcmp(name, OIC_JSON_PDDEV_LIST_NAME) == 0)
729             {
730                 int i = 0;
731                 CborValue pddevs = { .parser = NULL };
732                 cborFindResult = cbor_value_get_array_length(&pconfMap, &pconf->pddevLen);
733                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get length");
734                 cborFindResult = cbor_value_enter_container(&pconfMap, &pddevs);
735                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to enter");
736
737                 pconf->pddevs = (OicUuid_t *)OICMalloc(pconf->pddevLen * sizeof(OicUuid_t));
738                 VERIFY_NON_NULL(TAG, pconf->pddevs, ERROR);
739                 while (cbor_value_is_valid(&pddevs))
740                 {
741                     char *pddev = NULL;
742                     cborFindResult = cbor_value_dup_text_string(&pddevs, &pddev, &len, NULL);
743                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get value");
744                     cborFindResult = cbor_value_advance(&pddevs);
745                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance");
746                     ret = ConvertStrToUuid(pddev, &pconf->pddevs[i++]);
747                     VERIFY_SUCCESS(TAG, ret == OC_STACK_OK, ERROR);
748                     OICFree(pddev);
749                 }
750             }
751
752             //Mandatory - Device Id
753             if (0 == strcmp(OIC_JSON_DEVICE_ID_NAME, name))
754             {
755                 char *deviceId = NULL;
756                 cborFindResult = cbor_value_dup_text_string(&pconfMap, &deviceId, &len, NULL);
757                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get deviceID");
758                 ret = ConvertStrToUuid(deviceId, &pconf->deviceID);
759                 VERIFY_SUCCESS(TAG, ret == OC_STACK_OK, ERROR);
760                 OICFree(deviceId);
761             }
762
763             // ROwner -- Mandatory
764             if (0 == strcmp(OIC_JSON_ROWNERID_NAME, name))
765             {
766                 char *rowner = NULL;
767                 cborFindResult = cbor_value_dup_text_string(&pconfMap, &rowner, &len, NULL);
768                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get rowner");
769                 ret = ConvertStrToUuid(rowner, &pconf->rownerID);
770                 VERIFY_SUCCESS(TAG, ret == OC_STACK_OK, ERROR);
771                 OICFree(rowner);
772             }
773         }
774         if (CborMapType != type && cbor_value_is_valid(&pconfMap))
775         {
776             cborFindResult = cbor_value_advance(&pconfMap);
777             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance");
778         }
779         OICFree(name);
780         name = NULL;
781     }
782     *secPconf=pconf;
783     ret = OC_STACK_OK;
784 exit:
785     if (CborNoError != cborFindResult)
786     {
787         OIC_LOG (ERROR, TAG, "CBORPayloadToPconf failed");
788         DeletePconfBinData(pconf);
789         pconf = NULL;
790         *secPconf = NULL;
791         ret = OC_STACK_ERROR;
792     }
793     return ret;
794 }
795
796 static bool UpdatePersistentStorage(const OicSecPconf_t * pconf)
797 {
798     bool ret = false;
799
800     // Convert PCONF data into Cborpayload for update to persistent storage
801     uint8_t *payload = NULL;
802     size_t size = 0;
803     if (OC_STACK_OK == PconfToCBORPayload(pconf, &payload, &size) && NULL !=payload)
804     {
805         if (OC_STACK_OK == UpdateSecureResourceInPS(OIC_JSON_PCONF_NAME, payload, size))
806         {
807             ret = true;
808         }
809         OICFree(payload);
810     }
811     return ret;
812 }
813
814 static OCEntityHandlerResult HandlePconfGetRequest (const OCEntityHandlerRequest * ehRequest)
815 {
816     uint8_t* payload = NULL;
817     size_t size = 0;
818     OCEntityHandlerResult ehRet = OC_EH_OK;
819
820     OicSecPconf_t pconf;
821     memset(&pconf, 0, sizeof(OicSecPconf_t));
822
823     OIC_LOG (DEBUG, TAG, "Pconf EntityHandle processing GET request");
824
825     if (true == GetDoxmResourceData()->dpc)
826     {
827         //Making response elements for Get request
828         if( (true == gPconf->edp) &&
829             (gPconf->prm && 0 < gPconf->prmLen) &&
830             (0 < strlen((const char*)gPconf->deviceID.id)) &&
831             (0 < strlen((const char*)gPconf->rownerID.id)))
832         {
833             pconf.edp = true;
834             pconf.prm = gPconf->prm;
835             pconf.prmLen = gPconf->prmLen;
836             memcpy(&pconf.deviceID, &gPconf->deviceID, sizeof(OicUuid_t));
837             memcpy(&pconf.rownerID, &gPconf->rownerID, sizeof(OicUuid_t));
838             OIC_LOG (DEBUG, TAG, "PCONF - direct pairing enabled");
839         }
840         else if (false == gPconf->edp)
841         {
842             pconf.edp = false;
843             memcpy(&pconf.rownerID, &gPconf->rownerID, sizeof(OicUuid_t));
844             OIC_LOG (DEBUG, TAG, "PCONF - direct pairing disable");
845         }
846         else
847         {
848             ehRet= OC_EH_ERROR;
849             OIC_LOG (DEBUG, TAG, "PCONF - error");
850         }
851     }
852     else
853     {
854         OIC_LOG (DEBUG, TAG, "DPC == false : Direct-Pairing Disabled");
855     }
856
857
858     if (OC_STACK_OK != PconfToCBORPayload(gPconf, &payload, &size))
859     {
860         ehRet = OC_EH_ERROR;
861     }
862
863     if(OC_EH_OK == ehRet)
864     {
865         ehRet = (payload ? OC_EH_OK : OC_EH_ERROR);
866     }
867     else
868     {
869         OICFree(payload);
870         payload = NULL;
871         size = 0;
872     }
873
874     // Send response payload to request originator
875     if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, payload, size))
876     {
877         ehRet = OC_EH_ERROR;
878         OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandlePconfGetRequest");
879     }
880     OIC_LOG_V(DEBUG, TAG, "%s RetVal %d", __func__, ehRet);
881
882     OICFree(payload);
883     return ehRet;
884 }
885
886 static OCEntityHandlerResult HandlePconfPostRequest (const OCEntityHandlerRequest * ehRequest)
887 {
888     OCEntityHandlerResult ehRet = OC_EH_OK;
889     OCStackResult res=OC_STACK_OK;
890     OicSecPconf_t* newPconf = NULL;
891
892     if (true == GetDoxmResourceData()->dpc)
893     {
894         // Convert CBOR PCONF data into binary. This will also validate the PCONF data received.
895         uint8_t *payload = ((OCSecurityPayload *) ehRequest->payload)->securityData;
896         size_t size = ((OCSecurityPayload *) ehRequest->payload)->payloadSize;
897
898         if(payload){
899             res = CBORPayloadToPconf(payload, size, &newPconf);
900         }
901     }
902     else
903     {
904         OIC_LOG (DEBUG, TAG, "DPC == false : Direct-Pairing Disabled");
905         ehRet = OC_EH_ERROR;
906     }
907
908     if (newPconf && res == OC_STACK_OK)
909     {
910         // Check if valid Post request
911         if ((true == newPconf->edp) && (0 < newPconf->prmLen) &&
912                 DP_PIN_LENGTH == sizeof(newPconf->pin.val))
913         {
914             OicSecPrm_t *oldPrm = gPconf->prm;
915             OicSecPdAcl_t *oldPdacl = gPconf->pdacls;
916
917             // Update local PCONF with new PCONF
918             gPconf->edp = true;
919             memcpy(&gPconf->pin, &newPconf->pin, sizeof(OicDpPin_t));
920             gPconf->prm = newPconf->prm;
921             gPconf->prmLen = newPconf->prmLen;
922             gPconf->pdacls = newPconf->pdacls;
923             memcpy(&gPconf->rownerID, &newPconf->rownerID, sizeof(OicUuid_t));
924
925             // to delete old value(prm, pdacl)
926             newPconf->prm = oldPrm;
927             newPconf->pdacls = oldPdacl;
928         }
929         else if (false == newPconf->edp)
930         {
931             gPconf->edp = false;
932         }
933         else
934         {
935             ehRet = OC_EH_ERROR;
936         }
937
938         // Update storage
939         if(OC_EH_ERROR != ehRet && true == UpdatePersistentStorage(gPconf))
940         {
941             ehRet = OC_EH_RESOURCE_CREATED;
942         }
943
944         DeletePconfBinData(newPconf);
945     }
946     else
947     {
948         ehRet = OC_EH_ERROR;
949     }
950
951     // Send payload to request originator
952     if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL, 0))
953     {
954         ehRet = OC_EH_ERROR;
955         OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandlePconfPostRequest");
956     }
957
958     OIC_LOG_V (DEBUG, TAG, "%s RetVal %d", __func__ , ehRet);
959     return ehRet;
960 }
961
962 /*
963  * This internal method is the entity handler for PCONF resources and
964  * will handle REST request (POST) for them.
965  */
966 OCEntityHandlerResult PconfEntityHandler (OCEntityHandlerFlag flag,
967                                         OCEntityHandlerRequest * ehRequest,
968                                         void* callbackParameter)
969 {
970     OIC_LOG(DEBUG, TAG, "Received request PconfEntityHandler");
971     (void)callbackParameter;
972     OCEntityHandlerResult ehRet = OC_EH_ERROR;
973
974     if (!ehRequest)
975     {
976         return ehRet;
977     }
978
979     if (flag & OC_REQUEST_FLAG)
980     {
981         OIC_LOG (DEBUG, TAG, "Flag includes OC_REQUEST_FLAG");
982         switch (ehRequest->method)
983         {
984             case OC_REST_GET:
985                 ehRet = HandlePconfGetRequest(ehRequest);
986                 break;
987
988             case OC_REST_POST:
989                 ehRet = HandlePconfPostRequest(ehRequest);
990                 break;
991
992             case OC_REST_DELETE:
993                 break;
994
995             default:
996                 ehRet = OC_EH_ERROR;
997                 SendSRMResponse(ehRequest, ehRet, NULL, 0);
998         }
999     }
1000
1001     return ehRet;
1002 }
1003
1004 /*
1005  * This internal method is used to create '/oic/sec/pconf' resource.
1006  */
1007 OCStackResult CreatePconfResource()
1008 {
1009     OCStackResult ret;
1010
1011     ret = OCCreateResource(&gPconfHandle,
1012                            OIC_RSRC_TYPE_SEC_PCONF,
1013                            OIC_MI_DEF,
1014                            OIC_RSRC_PCONF_URI,
1015                            PconfEntityHandler,
1016                            NULL,
1017                            OC_SECURE | OC_EXPLICIT_DISCOVERABLE);
1018
1019     if (OC_STACK_OK != ret)
1020     {
1021         OIC_LOG (ERROR, TAG, "Unable to instantiate PCONF resource");
1022         DeInitPconfResource();
1023     }
1024     return ret;
1025 }
1026
1027 /**
1028  * Get the default value.
1029  * @retval  the gDefaultPconf pointer;
1030  */
1031 static OicSecPconf_t* GetPconfDefault()
1032 {
1033     OIC_LOG (DEBUG, TAG, "GetPconfDefault");
1034
1035     return &gDefaultPconf;
1036 }
1037
1038 /**
1039  * This method is used by SRM to retrieve PCONF resource data..
1040  *
1041  * @retval  reference to @ref OicSecPconf_t, binary format of Pconf resource data
1042  */
1043 const OicSecPconf_t* GetPconfResourceData()
1044 {
1045     return gPconf;
1046 }
1047
1048 /**
1049  * Initialize PCONF resource by loading data from persistent storage.
1050  *
1051  * @retval  OC_STACK_OK for Success, otherwise some error value
1052  */
1053 OCStackResult InitPconfResource()
1054 {
1055     OCStackResult ret = OC_STACK_ERROR;
1056
1057     uint8_t *data = NULL;
1058     size_t size = 0;
1059
1060     ret = GetSecureVirtualDatabaseFromPS(OIC_JSON_PCONF_NAME, &data, &size);
1061     // If database read failed
1062     if (ret != OC_STACK_OK)
1063     {
1064         OIC_LOG(DEBUG, TAG, "ReadSVDataFromPS failed");
1065     }
1066     if (data)
1067     {
1068         CBORPayloadToPconf(data, size, &gPconf);
1069     }
1070
1071     if (!data || !gPconf)
1072     {
1073         gPconf = GetPconfDefault();
1074
1075         // device id from doxm
1076         OicUuid_t deviceId = {.id = {0}};
1077         OCStackResult ret = GetDoxmDeviceID( &deviceId);
1078         VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
1079         memcpy(&gPconf->deviceID, &deviceId, sizeof(OicUuid_t));
1080     }
1081     VERIFY_NON_NULL(TAG, gPconf, ERROR);
1082
1083     // Instantiate 'oic.sec.pconf'
1084     ret = CreatePconfResource();
1085
1086 exit:
1087     if (OC_STACK_OK != ret)
1088     {
1089         DeInitPconfResource();
1090     }
1091     OICFree(data);
1092     return ret;
1093 }
1094
1095 /**
1096  * Perform cleanup for PCONF resources.
1097  *
1098  * @return
1099  * OC_STACK_OK    - no error
1100  * OC_STACK_ERROR - stack process error
1101  *
1102  */
1103 OCStackResult DeInitPconfResource()
1104 {
1105     OCStackResult ret = OCDeleteResource(gPconfHandle);
1106     if(gPconf!= &gDefaultPconf)
1107     {
1108         DeletePconfBinData(gPconf);
1109     }
1110     gPconf = NULL;
1111
1112     if(OC_STACK_OK == ret)
1113     {
1114         return OC_STACK_OK;
1115     }
1116     else
1117     {
1118         return OC_STACK_ERROR;
1119     }
1120 }
1121
1122 /**
1123  * This method might be used to add a paired device id after direct-pairing process complete.
1124  *
1125  * @param pdeviceId ID of the paired device.
1126  *
1127  * @retval  OC_STACK_OK for Success, otherwise some error value
1128  */
1129 OCStackResult AddPairedDevice(OicUuid_t *pdeviceId)
1130 {
1131     if (!gPconf || !pdeviceId)
1132     {
1133         return OC_STACK_INVALID_PARAM;
1134     }
1135
1136
1137     OicUuid_t *prevList = gPconf->pddevs;
1138     gPconf->pddevs = (OicUuid_t *)OICCalloc(gPconf->pddevLen+1, sizeof(OicUuid_t));
1139     if(!gPconf->pddevs)
1140     {
1141         return OC_STACK_NO_MEMORY;
1142     }
1143     for (size_t i=0; i<gPconf->pddevLen; i++)
1144     {
1145         memcpy(&gPconf->pddevs[i], &prevList[i], sizeof(OicUuid_t));
1146     }
1147
1148     // add new paired device id
1149     memcpy(&gPconf->pddevs[gPconf->pddevLen], pdeviceId, sizeof(OicUuid_t));
1150     gPconf->pddevLen++;
1151
1152     // Update storage
1153     if(true != UpdatePersistentStorage(gPconf))
1154     {
1155         OIC_LOG (ERROR, TAG, "Fail to update pconf resource");
1156         return OC_STACK_ERROR;
1157     }
1158
1159     OIC_LOG (ERROR, TAG, "Add paired device success");
1160     return OC_STACK_OK;
1161 }
1162
1163 /**
1164  * This method might be used by PolicyEngine to retrieve PDACL for a Subject.
1165  *
1166  * @param subjectId ID of the subject for which PDACL is required.
1167  * @param savePtr is used internally by @ref GetACLResourceData to maintain index between
1168  *                successive calls for same subjectId.
1169  *
1170  * @retval  reference to @ref OicSecPdAcl_t if PDACL is found, else NULL
1171  */
1172 const OicSecPdAcl_t* GetPdAclData(const OicUuid_t* subjectId, OicSecPdAcl_t **savePtr)
1173 {
1174     OicSecPdAcl_t *pdacl = NULL;
1175
1176     if ( NULL == subjectId)
1177     {
1178         return NULL;
1179     }
1180
1181     /*
1182      * savePtr MUST point to NULL if this is the 'first' call to retrieve PDACL for
1183      * subjectID.
1184      */
1185     if (NULL == *savePtr)
1186     {
1187         pdacl = gPconf->pdacls;
1188
1189         // Find if 'subjectID' is in paired device list.
1190         for(size_t i=0; i<gPconf->pddevLen; i++)
1191         {
1192             if (memcmp(&(gPconf->pddevs[i]), subjectId, sizeof(OicUuid_t)) == 0)
1193             {
1194                 *savePtr = pdacl;
1195                 return pdacl;
1196             }
1197         }
1198     }
1199     else
1200     {
1201         OicSecPdAcl_t *temp = NULL;
1202
1203         /*
1204          * If this is a 'successive' call, search for location pointed by
1205          * savePtr and assign 'begin' to the next PDACL after it in the linked
1206          * list and start searching from there.
1207          */
1208         LL_FOREACH(gPconf->pdacls, temp)
1209         {
1210             if (temp == *savePtr)
1211             {
1212                 pdacl = temp->next;
1213                 *savePtr = pdacl;
1214                 return pdacl;
1215             }
1216         }
1217     }
1218
1219     // Cleanup in case no PDACL is found
1220     *savePtr = NULL;
1221     return NULL;
1222 }
1223
1224 /**
1225  * This method return whether device is paired or not.
1226  *
1227  * @param pdeviceId Target device ID to find in paired list.
1228  * @retval  ture if device is already paired, else false
1229  */
1230 bool IsPairedDevice(const OicUuid_t* pdeviceId)
1231 {
1232     // Find if 'pdeviceId' is in paired device list.
1233     for(size_t i=0; i<gPconf->pddevLen; i++)
1234     {
1235         if (memcmp(&(gPconf->pddevs[i]), pdeviceId, sizeof(OicUuid_t)) == 0)
1236         {
1237             return true;
1238         }
1239     }
1240     return false;
1241 }
1242
1243 OCStackResult SetPconfRownerId(const OicUuid_t* newROwner)
1244 {
1245     OCStackResult ret = OC_STACK_ERROR;
1246     uint8_t *cborPayload = NULL;
1247     size_t size = 0;
1248     OicUuid_t prevId = {.id={0}};
1249
1250     if(NULL == newROwner)
1251     {
1252         ret = OC_STACK_INVALID_PARAM;
1253     }
1254     if(NULL == gPconf)
1255     {
1256         ret = OC_STACK_NO_RESOURCE;
1257     }
1258
1259     if(newROwner && gPconf)
1260     {
1261         memcpy(prevId.id, gPconf->rownerID.id, sizeof(prevId.id));
1262         memcpy(gPconf->rownerID.id, newROwner->id, sizeof(newROwner->id));
1263
1264         ret = PconfToCBORPayload(gPconf, &cborPayload, &size);
1265         VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
1266
1267         ret = UpdateSecureResourceInPS(OIC_JSON_PCONF_NAME, cborPayload, size);
1268         VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
1269
1270         OICFree(cborPayload);
1271     }
1272
1273     return ret;
1274
1275 exit:
1276     OICFree(cborPayload);
1277     memcpy(gPconf->rownerID.id, prevId.id, sizeof(prevId.id));
1278     return ret;
1279 }
1280
1281 OCStackResult GetPconfRownerId(OicUuid_t *rowneruuid)
1282 {
1283     OCStackResult retVal = OC_STACK_ERROR;
1284     if (gPconf)
1285     {
1286         *rowneruuid = gPconf->rownerID;
1287         retVal = OC_STACK_OK;
1288     }
1289     return retVal;
1290 }