Changes to PolicyEngine to support rowner and isop checks.
[platform/upstream/iotivity.git] / resource / csdk / security / src / aclresource.c
1 //******************************************************************
2 //
3 // Copyright 2015 Intel Mobile Communications GmbH 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 #ifdef WITH_ARDUINO
22 #include <string.h>
23 #else
24 #include <strings.h>
25 #endif
26 #include <stdlib.h>
27
28 #include "ocstack.h"
29 #include "ocserverrequest.h"
30 #include "oic_malloc.h"
31 #include "oic_string.h"
32 #include "ocrandom.h"
33 #include "ocpayload.h"
34 #include "utlist.h"
35 #include "payload_logging.h"
36 #include "srmresourcestrings.h"
37 #include "aclresource.h"
38 #include "doxmresource.h"
39 #include "resourcemanager.h"
40 #include "srmutility.h"
41 #include "psinterface.h"
42
43 #include "security_internals.h"
44
45 #define TAG  "SRM-ACL"
46 #define NUMBER_OF_SEC_PROV_RSCS 4
47 #define NUMBER_OF_DEFAULT_SEC_RSCS 2
48
49 static const uint8_t ACL_MAP_SIZE = 2;
50 static const uint8_t ACL_ACLIST_MAP_SIZE = 1;
51 static const uint8_t ACL_ACES_MAP_SIZE = 3;
52 static const uint8_t ACL_RESOURCE_MAP_SIZE = 4;
53
54
55 // CborSize is the default cbor payload size being used.
56 static const uint16_t CBOR_SIZE = 2048;
57
58 static OicSecAcl_t *gAcl = NULL;
59 static OCResourceHandle gAclHandle = NULL;
60
61 /**
62  * This function frees OicSecAcl_t object's fields and object itself.
63  */
64 static void FreeACE(OicSecAcl_t *ace)
65 {
66     size_t i;
67     if (NULL == ace)
68     {
69         OIC_LOG(ERROR, TAG, "Invalid Parameter");
70         return;
71     }
72
73     // Clean Resources
74     for (i = 0; i < ace->resourcesLen; i++)
75     {
76         OICFree(ace->resources[i]);
77     }
78     OICFree(ace->resources);
79
80     //Clean Period
81     if (ace->periods)
82     {
83         for (i = 0; i < ace->prdRecrLen; i++)
84         {
85             OICFree(ace->periods[i]);
86         }
87         OICFree(ace->periods);
88     }
89
90     //Clean Recurrence
91     if (ace->recurrences)
92     {
93         for (i = 0; i < ace->prdRecrLen; i++)
94         {
95             OICFree(ace->recurrences[i]);
96         }
97         OICFree(ace->recurrences);
98     }
99
100     // Clean ACL node itself
101     OICFree(ace);
102 }
103
104 void DeleteACLList(OicSecAcl_t* acl)
105 {
106     if (acl)
107     {
108         OicSecAcl_t *aclTmp1 = NULL;
109         OicSecAcl_t *aclTmp2 = NULL;
110         LL_FOREACH_SAFE(acl, aclTmp1, aclTmp2)
111         {
112             LL_DELETE(acl, aclTmp1);
113             FreeACE(aclTmp1);
114         }
115     }
116 }
117
118 static size_t OicSecAclSize(const OicSecAcl_t *secAcl)
119 {
120     if (!secAcl)
121     {
122         return 0;
123     }
124     OicSecAcl_t *acl = (OicSecAcl_t *)secAcl;
125     size_t size = 0;
126     while (acl)
127     {
128        size++;
129        acl = acl->next;
130     }
131     return size;
132 }
133
134 OCStackResult AclToCBORPayload(const OicSecAcl_t *secAcl, uint8_t **payload, size_t *size)
135 {
136      if (NULL == secAcl || NULL == payload || NULL != *payload || NULL == size)
137     {
138         return OC_STACK_INVALID_PARAM;
139     }
140
141     OCStackResult ret = OC_STACK_ERROR;
142     CborError cborEncoderResult = CborNoError;
143     OicSecAcl_t *acl = (OicSecAcl_t *)secAcl;
144     CborEncoder encoder = { {.ptr = NULL }, .end = 0 };
145     CborEncoder aclMap = { {.ptr = NULL }, .end = 0 };
146     CborEncoder aclListMap = { {.ptr = NULL }, .end = 0 };
147     CborEncoder acesArray = { {.ptr = NULL }, .end = 0 };
148     uint8_t *outPayload = NULL;
149     size_t cborLen = *size;
150     *size = 0;
151     *payload = NULL;
152
153     if (cborLen == 0)
154     {
155         cborLen = CBOR_SIZE;
156     }
157
158     outPayload = (uint8_t *)OICCalloc(1, cborLen);
159     VERIFY_NON_NULL(TAG, outPayload, ERROR);
160     cbor_encoder_init(&encoder, outPayload, cborLen, 0);
161
162     // Create ACL Map (aclist, rownerid)
163     cborEncoderResult = cbor_encoder_create_map(&encoder, &aclMap, ACL_MAP_SIZE);
164     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Creating ACL Map.");
165
166     cborEncoderResult = cbor_encode_text_string(&aclMap, OIC_JSON_ACLIST_NAME,
167         strlen(OIC_JSON_ACLIST_NAME));
168     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding aclist Name Tag.");
169
170     // Create ACLIST Map (aces)
171     cborEncoderResult = cbor_encoder_create_map(&aclMap, &aclListMap, ACL_ACLIST_MAP_SIZE);
172     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Creating ACLIST Map.");
173
174     cborEncoderResult = cbor_encode_text_string(&aclListMap, OIC_JSON_ACES_NAME,
175         strlen(OIC_JSON_ACES_NAME));
176     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACES Name Tag.");
177
178     // Create ACES Array
179     cborEncoderResult = cbor_encoder_create_array(&aclListMap, &acesArray, OicSecAclSize(secAcl));
180     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Creating ACES Array.");
181
182     while (acl)
183     {
184         CborEncoder oicSecAclMap = { {.ptr = NULL }, .end = 0, .added = 0, .flags = 0 };
185         // ACL Map size - Number of mandatory items
186         uint8_t aclMapSize = ACL_ACES_MAP_SIZE;
187         size_t inLen = 0;
188
189         // Create ACL Map
190         if (acl->periods)
191         {
192             ++aclMapSize;
193         }
194         if (acl->recurrences)
195         {
196             ++aclMapSize;
197         }
198
199         cborEncoderResult = cbor_encoder_create_map(&acesArray, &oicSecAclMap, aclMapSize);
200         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Creating ACES Map");
201
202         // Subject -- Mandatory
203         cborEncoderResult = cbor_encode_text_string(&oicSecAclMap, OIC_JSON_SUBJECTID_NAME,
204             strlen(OIC_JSON_SUBJECTID_NAME));
205         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Subject Name Tag.");
206         inLen = (memcmp(&(acl->subject), &WILDCARD_SUBJECT_ID, sizeof(OicUuid_t)) == 0) ?
207             WILDCARD_SUBJECT_ID_LEN : sizeof(OicUuid_t);
208         if(inLen == WILDCARD_SUBJECT_ID_LEN)
209         {
210             cborEncoderResult = cbor_encode_text_string(&oicSecAclMap, WILDCARD_RESOURCE_URI,
211                 strlen(WILDCARD_RESOURCE_URI));
212             VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding Subject Id wildcard Value.");
213         }
214         else
215         {
216             char *subject = NULL;
217             ret = ConvertUuidToStr(&acl->subject, &subject);
218             VERIFY_SUCCESS(TAG, ret == OC_STACK_OK, ERROR);
219             cborEncoderResult = cbor_encode_text_string(&oicSecAclMap, subject, strlen(subject));
220             VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding Subject Id Value.");
221             OICFree(subject);
222         }
223
224         // Resources
225         {
226             CborEncoder resources = { {.ptr = NULL }, .end = 0, .added = 0, .flags = 0 };
227             cborEncoderResult = cbor_encode_text_string(&oicSecAclMap, OIC_JSON_RESOURCES_NAME,
228                 strlen(OIC_JSON_RESOURCES_NAME));
229             VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Resource Name Tag.");
230
231             cborEncoderResult = cbor_encoder_create_array(&oicSecAclMap, &resources, acl->resourcesLen);
232             VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Resource Name Array.");
233
234             for (size_t i = 0; i < acl->resourcesLen; i++)
235             {
236
237                 CborEncoder rMap = { {.ptr = NULL }, .end = 0 };
238                 cborEncoderResult = cbor_encoder_create_map(&resources, &rMap, ACL_RESOURCE_MAP_SIZE);
239                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding Resource Map.");
240
241                 cborEncoderResult = cbor_encode_text_string(&rMap, OIC_JSON_HREF_NAME,
242                         strlen(OIC_JSON_HREF_NAME));
243                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding HREF Name Tag.");
244                 cborEncoderResult = cbor_encode_text_string(&rMap, acl->resources[i],
245                         strlen(acl->resources[i]));
246                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding HREF Value in Map.");
247
248                 cborEncoderResult = cbor_encode_text_string(&rMap, OIC_JSON_REL_NAME,
249                         strlen(OIC_JSON_REL_NAME));
250                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding REL Name Tag.");
251
252                 // TODO : Need to assign real value of REL
253                 cborEncoderResult = cbor_encode_text_string(&rMap, OIC_JSON_EMPTY_STRING,
254                         strlen(OIC_JSON_EMPTY_STRING));
255                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding REL Value.");
256
257                 cborEncoderResult = cbor_encode_text_string(&rMap, OIC_JSON_RT_NAME,
258                         strlen(OIC_JSON_RT_NAME));
259                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RT Name Tag.");
260
261                 // TODO : Need to assign real value of RT
262                 cborEncoderResult = cbor_encode_text_string(&rMap, OIC_JSON_EMPTY_STRING,
263                         strlen(OIC_JSON_EMPTY_STRING));
264                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RT Value.");
265
266                 cborEncoderResult = cbor_encode_text_string(&rMap, OIC_JSON_IF_NAME,
267                         strlen(OIC_JSON_IF_NAME));
268                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding IF Name Tag.");
269
270                 // TODO : Need to assign real value of IF
271                 cborEncoderResult = cbor_encode_text_string(&rMap, OIC_JSON_EMPTY_STRING,
272                         strlen(OIC_JSON_EMPTY_STRING));
273                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding IF Value.");
274
275
276                 cborEncoderResult = cbor_encoder_close_container(&resources, &rMap);
277                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Resource Map.");
278
279             }
280             cborEncoderResult = cbor_encoder_close_container(&oicSecAclMap, &resources);
281             VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Resource Name Array.");
282         }
283
284
285         // Permissions -- Mandatory
286         cborEncoderResult = cbor_encode_text_string(&oicSecAclMap, OIC_JSON_PERMISSION_NAME,
287             strlen(OIC_JSON_PERMISSION_NAME));
288         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Permission Name Tag.");
289         cborEncoderResult = cbor_encode_int(&oicSecAclMap, acl->permission);
290         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Permission Name Value.");
291
292         // Period -- Not Mandatory
293         if (acl->periods)
294         {
295
296             CborEncoder period;
297             cborEncoderResult = cbor_encode_text_string(&oicSecAclMap, OIC_JSON_PERIOD_NAME,
298                 strlen(OIC_JSON_PERIOD_NAME));
299             VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Period Tag.");
300             cborEncoderResult = cbor_encoder_create_array(&oicSecAclMap, &period, acl->prdRecrLen);
301             VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Period Array.");
302             for (size_t i = 0; i < acl->prdRecrLen; i++)
303             {
304                 cborEncoderResult = cbor_encode_text_string(&period, acl->periods[i],
305                     strlen(acl->periods[i]));
306                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Period Value in Array.");
307             }
308             cborEncoderResult = cbor_encoder_close_container(&oicSecAclMap, &period);
309             VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Period Array.");
310         }
311
312         // Recurrence -- Not Mandatory
313         if (acl->recurrences)
314         {
315             CborEncoder recurrences;
316             cborEncoderResult = cbor_encode_text_string(&oicSecAclMap, OIC_JSON_RECURRENCES_NAME,
317                 strlen(OIC_JSON_RECURRENCES_NAME));
318             VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Recurrence Tag.");
319             cborEncoderResult = cbor_encoder_create_array(&oicSecAclMap, &recurrences, acl->prdRecrLen);
320             VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Recurrence Array.");
321
322             for (size_t i = 0; i < acl->prdRecrLen; i++)
323             {
324                 cborEncoderResult = cbor_encode_text_string(&recurrences, acl->recurrences[i],
325                     strlen(acl->recurrences[i]));
326                 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Recurrence Array Value.");
327             }
328             cborEncoderResult = cbor_encoder_close_container(&oicSecAclMap, &recurrences);
329             VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Recurrence Array");
330
331         }
332
333
334         cborEncoderResult = cbor_encoder_close_container(&acesArray, &oicSecAclMap);
335         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing ACES Map.");
336         acl = acl->next;
337     }
338
339     // Close ACES Array
340     cborEncoderResult = cbor_encoder_close_container(&aclListMap, &acesArray);
341     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing ACES Array.");
342
343     // Close ACLIST Map
344     cborEncoderResult = cbor_encoder_close_container(&aclMap, &aclListMap);
345     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing ACLIST Map.");
346
347     // Rownerid
348     {
349         char *rowner = NULL;
350         cborEncoderResult = cbor_encode_text_string(&aclMap, OIC_JSON_ROWNERID_NAME,
351             strlen(OIC_JSON_ROWNERID_NAME));
352         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding rownerid Name.");
353         ret = ConvertUuidToStr(&secAcl->rownerID, &rowner);
354         VERIFY_SUCCESS(TAG, ret == OC_STACK_OK, ERROR);
355         cborEncoderResult = cbor_encode_text_string(&aclMap, rowner, strlen(rowner));
356         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding rownerid Value.");
357         OICFree(rowner);
358     }
359
360     // Close ACL Map
361     cborEncoderResult = cbor_encoder_close_container(&encoder, &aclMap);
362     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing ACL Map.");
363
364     if (CborNoError == cborEncoderResult)
365     {
366         OIC_LOG(DEBUG, TAG, "AclToCBORPayload Successed");
367         *size = encoder.ptr - outPayload;
368         *payload = outPayload;
369         ret = OC_STACK_OK;
370     }
371 exit:
372     if (CborErrorOutOfMemory == cborEncoderResult)
373     {
374         OIC_LOG(DEBUG, TAG, "AclToCBORPayload:CborErrorOutOfMemory : retry with more memory");
375
376         // reallocate and try again!
377         OICFree(outPayload);
378         // Since the allocated initial memory failed, double the memory.
379         cborLen += encoder.ptr - encoder.end;
380         cborEncoderResult = CborNoError;
381         ret = AclToCBORPayload(secAcl, payload, &cborLen);
382         *size = cborLen;
383     }
384     else if (cborEncoderResult != CborNoError)
385     {
386         OIC_LOG(ERROR, TAG, "Failed to AclToCBORPayload");
387         OICFree(outPayload);
388         outPayload = NULL;
389         *size = 0;
390         *payload = NULL;
391         ret = OC_STACK_ERROR;
392     }
393
394     return ret;
395 }
396
397 // This function converts CBOR format to ACL data.
398 // Caller needs to invoke 'free' when done using
399 // note: This function is used in unit test hence not declared static,
400 OicSecAcl_t* CBORPayloadToAcl(const uint8_t *cborPayload, const size_t size)
401 {
402     if (NULL == cborPayload)
403     {
404         return NULL;
405     }
406     OCStackResult ret = OC_STACK_ERROR;
407     CborValue aclCbor = { .parser = NULL };
408     CborParser parser = { .end = NULL };
409     CborError cborFindResult = CborNoError;
410     cbor_parser_init(cborPayload, size, 0, &parser, &aclCbor);
411
412     OicSecAcl_t *headAcl = (OicSecAcl_t *) OICCalloc(1, sizeof(OicSecAcl_t));
413
414     // Enter ACL Map
415     CborValue aclMap = { .parser = NULL, .ptr = NULL, .remaining = 0, .extra = 0, .type = 0, .flags = 0 };
416     cborFindResult = cbor_value_enter_container(&aclCbor, &aclMap);
417     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering ACL Map.");
418
419     while (cbor_value_is_valid(&aclMap))
420     {
421         char* tagName = NULL;
422         size_t len = 0;
423         CborType type = cbor_value_get_type(&aclMap);
424         if (type == CborTextStringType)
425         {
426             cborFindResult = cbor_value_dup_text_string(&aclMap, &tagName, &len, NULL);
427             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Name in ACL Map.");
428             cborFindResult = cbor_value_advance(&aclMap);
429             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing Value in ACL Map.");
430         }
431         if(tagName)
432         {
433             if (strcmp(tagName, OIC_JSON_ACLIST_NAME)  == 0)
434             {
435                 // Enter ACLIST Map
436                 CborValue aclistMap = { .parser = NULL, .ptr = NULL, .remaining = 0, .extra = 0, .type = 0, .flags = 0 };
437                 cborFindResult = cbor_value_enter_container(&aclMap, &aclistMap);
438                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering ACLIST Map.");
439
440
441                 while (cbor_value_is_valid(&aclistMap))
442                 {
443                     char* acName = NULL;
444                     size_t acLen = 0;
445                     CborType acType = cbor_value_get_type(&aclistMap);
446                     if (acType == CborTextStringType)
447                     {
448                         cborFindResult = cbor_value_dup_text_string(&aclistMap, &acName, &acLen, NULL);
449                         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Name in ACLIST Map.");
450                         cborFindResult = cbor_value_advance(&aclistMap);
451                         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing Value in ACLIST Map.");
452                     }
453                     if(acName)
454                     {
455                         if (strcmp(acName, OIC_JSON_ACES_NAME)  == 0)
456                         {
457
458                             // Enter ACES Array
459                             CborValue aclArray = { .parser = NULL, .ptr = NULL, .remaining = 0, .extra = 0, .type = 0, .flags = 0 };
460                             cborFindResult = cbor_value_enter_container(&aclistMap, &aclArray);
461                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering ACL Array.");
462
463                             int acesCount = 0;
464                             while (cbor_value_is_valid(&aclArray))
465                             {
466                                 acesCount++;
467
468                                 CborValue aclMap = { .parser = NULL, .ptr = NULL, .remaining = 0, .extra = 0, .type = 0, .flags = 0 };
469                                 cborFindResult = cbor_value_enter_container(&aclArray, &aclMap);
470                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering ACL Map.");
471                                 OicSecAcl_t *acl = NULL;
472
473                                 if(acesCount == 1)
474                                 {
475                                     acl = headAcl;
476                                 }
477                                 else
478                                 {
479                                     acl = (OicSecAcl_t *) OICCalloc(1, sizeof(OicSecAcl_t));
480                                     OicSecAcl_t *temp = headAcl;
481                                     while (temp->next)
482                                     {
483                                         temp = temp->next;
484                                     }
485                                     temp->next = acl;
486                                 }
487                                 VERIFY_NON_NULL(TAG, acl, ERROR);
488
489                                 while (cbor_value_is_valid(&aclMap))
490                                 {
491                                     char* name = NULL;
492                                     size_t len = 0;
493                                     CborType type = cbor_value_get_type(&aclMap);
494                                     if (type == CborTextStringType)
495                                     {
496                                         cborFindResult = cbor_value_dup_text_string(&aclMap, &name, &len, NULL);
497                                         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Name in ACL Map.");
498                                         cborFindResult = cbor_value_advance(&aclMap);
499                                         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing Value in ACL Map.");
500                                     }
501                                     if (name)
502                                     {
503                                         // Subject -- Mandatory
504                                         if (strcmp(name, OIC_JSON_SUBJECTID_NAME)  == 0)
505                                         {
506                                             char *subject = NULL;
507                                             cborFindResult = cbor_value_dup_text_string(&aclMap, &subject, &len, NULL);
508                                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding subject Value.");
509                                             if(strcmp(subject, WILDCARD_RESOURCE_URI) == 0)
510                                             {
511                                                 acl->subject.id[0] = '*';
512                                             }
513                                             else
514                                             {
515                                                 ret = ConvertStrToUuid(subject, &acl->subject);
516                                                 VERIFY_SUCCESS(TAG, ret == OC_STACK_OK, ERROR);
517                                             }
518                                             OICFree(subject);
519                                         }
520
521                                         // Resources -- Mandatory
522                                         if (strcmp(name, OIC_JSON_RESOURCES_NAME) == 0)
523                                         {
524                                             CborValue resources = { .parser = NULL };
525                                             cborFindResult = cbor_value_get_array_length(&aclMap, &acl->resourcesLen);
526                                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding a Resource Array Len Value.");
527                                             cborFindResult = cbor_value_enter_container(&aclMap, &resources);
528                                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering a Resource Array.");
529
530                                             acl->resources = (char **) OICMalloc(acl->resourcesLen * sizeof(char*));
531                                             VERIFY_NON_NULL(TAG, acl->resources, ERROR);
532                                             int i = 0;
533                                             while (cbor_value_is_valid(&resources))
534                                             {
535                                                 // rMap
536                                                 CborValue rMap = { .parser = NULL  };
537                                                 cborFindResult = cbor_value_enter_container(&resources, &rMap);
538                                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering Resource Map");
539
540
541                                                 while(cbor_value_is_valid(&rMap))
542                                                 {
543                                                     char *rMapName = NULL;
544                                                     size_t rMapNameLen = 0;
545                                                     cborFindResult = cbor_value_dup_text_string(&rMap, &rMapName, &rMapNameLen, NULL);
546                                                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding RMap Data Name Tag.");
547                                                     cborFindResult = cbor_value_advance(&rMap);
548                                                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding RMap Data Value.");
549
550                                                     // "href"
551                                                     if (0 == strcmp(OIC_JSON_HREF_NAME, rMapName))
552                                                     {
553                                                         // TODO : Need to check data structure of OicSecAcl_t based on RAML spec.
554                                                         cborFindResult = cbor_value_dup_text_string(&rMap, &acl->resources[i++], &len, NULL);
555                                                         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Href Value.");
556                                                     }
557                                                     // "rel"
558                                                     if (0 == strcmp(OIC_JSON_REL_NAME, rMapName))
559                                                     {
560                                                         // TODO : Need to check data structure of OicSecAcl_t based on RAML spec.
561                                                         char *relData = NULL;
562                                                         cborFindResult = cbor_value_dup_text_string(&rMap, &relData, &len, NULL);
563                                                         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding REL Value.");
564                                                         OICFree(relData);
565                                                     }
566                                                     // "rt"
567                                                     if (0 == strcmp(OIC_JSON_RT_NAME, rMapName))
568                                                     {
569                                                         // TODO : Need to check data structure of OicSecAcl_t and assign based on RAML spec.
570                                                         char *rtData = NULL;
571                                                         cborFindResult = cbor_value_dup_text_string(&rMap, &rtData, &len, NULL);
572                                                         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding RT Value.");
573                                                         OICFree(rtData);
574                                                     }
575
576                                                     // "if"
577                                                     if (0 == strcmp(OIC_JSON_IF_NAME, rMapName))
578                                                     {
579                                                         // TODO : Need to check data structure of OicSecAcl_t and assign based on RAML spec.
580                                                         char *ifData = NULL;
581                                                         cborFindResult = cbor_value_dup_text_string(&rMap, &ifData, &len, NULL);
582                                                         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding IF Value.");
583                                                         OICFree(ifData);
584                                                     }
585
586                                                     if (cbor_value_is_valid(&rMap))
587                                                     {
588                                                         cborFindResult = cbor_value_advance(&rMap);
589                                                         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing Rlist Map.");
590                                                     }
591                                                     OICFree(rMapName);
592                                                 }
593
594                                                 if (cbor_value_is_valid(&resources))
595                                                 {
596                                                     cborFindResult = cbor_value_advance(&resources);
597                                                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing Resource Array.");
598                                                 }
599                                             }
600                                         }
601
602                                         // Permissions -- Mandatory
603                                         if (strcmp(name, OIC_JSON_PERMISSION_NAME) == 0)
604                                         {
605                                             cborFindResult = cbor_value_get_uint64(&aclMap, (uint64_t *) &acl->permission);
606                                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding a PERM Value.");
607                                         }
608
609                                         // Period -- Not mandatory
610                                         if (strcmp(name, OIC_JSON_PERIOD_NAME) == 0)
611                                         {
612                                             CborValue period = { .parser = NULL };
613                                             cborFindResult = cbor_value_get_array_length(&aclMap, &acl->prdRecrLen);
614                                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding a Period Array Len.");
615                                             cborFindResult = cbor_value_enter_container(&aclMap, &period);
616                                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding a Period Array Map.");
617                                             acl->periods = (char**)OICCalloc(acl->prdRecrLen, sizeof(char*));
618                                             VERIFY_NON_NULL(TAG, acl->periods, ERROR);
619                                             int i = 0;
620                                             while (cbor_value_is_text_string(&period))
621                                             {
622                                                 cborFindResult = cbor_value_dup_text_string(&period, &acl->periods[i++],
623                                                     &len, NULL);
624                                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding a Period Array Value.");
625                                                 cborFindResult = cbor_value_advance(&period);
626                                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing a Period Array.");
627                                             }
628                                         }
629
630                                         // Recurrence -- Not mandatory
631                                         if (strcmp(name, OIC_JSON_RECURRENCES_NAME) == 0)
632                                         {
633                                             CborValue recurrences = { .parser = NULL };
634                                             cborFindResult = cbor_value_enter_container(&aclMap, &recurrences);
635                                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Adding Recurrence Array.");
636                                             acl->recurrences = (char**)OICCalloc(acl->prdRecrLen, sizeof(char*));
637                                             VERIFY_NON_NULL(TAG, acl->recurrences, ERROR);
638                                             int i = 0;
639                                             while (cbor_value_is_text_string(&recurrences))
640                                             {
641                                                 cborFindResult = cbor_value_dup_text_string(&recurrences,
642                                                     &acl->recurrences[i++], &len, NULL);
643                                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Adding Recurrence Array Value.");
644                                                 cborFindResult = cbor_value_advance(&recurrences);
645                                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Closing Recurrence Array.");
646                                             }
647                                         }
648
649                                         OICFree(name);
650                                     }
651
652                                     if (type != CborMapType && cbor_value_is_valid(&aclMap))
653                                     {
654                                         cborFindResult = cbor_value_advance(&aclMap);
655                                         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing the Array.");
656                                     }
657                                 }
658
659                                 acl->next = NULL;
660
661                                 if (cbor_value_is_valid(&aclArray))
662                                 {
663                                     cborFindResult = cbor_value_advance(&aclArray);
664                                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing ACL Array.");
665                                 }
666                             }
667                         }
668                         OICFree(acName);
669                     }
670
671                     if (cbor_value_is_valid(&aclistMap))
672                     {
673                         cborFindResult = cbor_value_advance(&aclistMap);
674                         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing ACLIST Map.");
675                     }
676                 }
677             }
678
679             // TODO : Need to modify headAcl->owners[0].id to headAcl->rowner based on RAML spec.
680             if (strcmp(tagName, OIC_JSON_ROWNERID_NAME)  == 0)
681             {
682                 char *stRowner = NULL;
683                 cborFindResult = cbor_value_dup_text_string(&aclMap, &stRowner, &len, NULL);
684                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Rownerid Value.");
685                 ret = ConvertStrToUuid(stRowner, &headAcl->rownerID);
686                 VERIFY_SUCCESS(TAG, ret == OC_STACK_OK, ERROR);
687                 OICFree(stRowner);
688             }
689             OICFree(tagName);
690         }
691         if (cbor_value_is_valid(&aclMap))
692         {
693             cborFindResult = cbor_value_advance(&aclMap);
694             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing ACL Map.");
695         }
696     }
697
698 exit:
699     if (cborFindResult != CborNoError)
700     {
701         OIC_LOG(ERROR, TAG, "Failed to CBORPayloadToAcl");
702         DeleteACLList(headAcl);
703         headAcl = NULL;
704     }
705     return headAcl;
706 }
707
708 /**
709  * This method removes ACE for the subject and resource from the ACL
710  *
711  * @param subject of the ACE
712  * @param resource of the ACE
713  *
714  * @return
715  *     ::OC_STACK_RESOURCE_DELETED on success
716  *     ::OC_STACK_NO_RESOURCE on failure to find the appropriate ACE
717  *     ::OC_STACK_INVALID_PARAM on invalid parameter
718  */
719 static OCStackResult RemoveACE(const OicUuid_t * subject, const char * resource)
720 {
721     OIC_LOG(DEBUG, TAG, "IN RemoveACE");
722
723     OicSecAcl_t *acl = NULL;
724     OicSecAcl_t *tempAcl = NULL;
725     bool deleteFlag = false;
726     OCStackResult ret = OC_STACK_NO_RESOURCE;
727
728     if (memcmp(subject->id, &WILDCARD_SUBJECT_ID, sizeof(subject->id)) == 0)
729     {
730         OIC_LOG_V(ERROR, TAG, "%s received invalid parameter", __func__ );
731         return  OC_STACK_INVALID_PARAM;
732     }
733
734     //If resource is NULL then delete all the ACE for the subject.
735     if (NULL == resource || resource[0] == '\0')
736     {
737         LL_FOREACH_SAFE(gAcl, acl, tempAcl)
738         {
739             if (memcmp(acl->subject.id, subject->id, sizeof(subject->id)) == 0)
740             {
741                 LL_DELETE(gAcl, acl);
742                 FreeACE(acl);
743                 deleteFlag = true;
744             }
745         }
746     }
747     else
748     {
749         //Looping through ACL to find the right ACE to delete. If the required resource is the only
750         //resource in the ACE for the subject then delete the whole ACE. If there are more resources
751         //than the required resource in the ACE, for the subject then just delete the resource from
752         //the resource array
753         LL_FOREACH_SAFE(gAcl, acl, tempAcl)
754         {
755             if (memcmp(acl->subject.id, subject->id, sizeof(subject->id)) == 0)
756             {
757                 if (1 == acl->resourcesLen && strcmp(acl->resources[0], resource) == 0)
758                 {
759                     LL_DELETE(gAcl, acl);
760                     FreeACE(acl);
761                     deleteFlag = true;
762                     break;
763                 }
764                 else
765                 {
766                     size_t resPos = -1;
767                     size_t i;
768                     for (i = 0; i < acl->resourcesLen; i++)
769                     {
770                         if (strcmp(acl->resources[i], resource) == 0)
771                         {
772                             resPos = i;
773                             break;
774                         }
775                     }
776                     if (0 <= (int) resPos)
777                     {
778                         OICFree(acl->resources[resPos]);
779                         acl->resources[resPos] = NULL;
780                         acl->resourcesLen -= 1;
781                         for (i = resPos; i < acl->resourcesLen; i++)
782                         {
783                             acl->resources[i] = acl->resources[i + 1];
784                         }
785                         deleteFlag = true;
786                         break;
787                     }
788                 }
789             }
790         }
791     }
792
793     if (deleteFlag)
794     {
795         // In case of unit test do not update persistant storage.
796         if (memcmp(subject->id, &WILDCARD_SUBJECT_B64_ID, sizeof(subject->id)) == 0)
797         {
798             ret = OC_STACK_RESOURCE_DELETED;
799         }
800         else
801         {
802             uint8_t *payload = NULL;
803             size_t size = 0;
804             if (OC_STACK_OK == AclToCBORPayload(gAcl, &payload, &size))
805             {
806                 if (OC_STACK_OK == UpdateSecureResourceInPS(OIC_JSON_ACL_NAME, payload, size))
807                 {
808                     ret = OC_STACK_RESOURCE_DELETED;
809                 }
810                 OICFree(payload);
811             }
812         }
813     }
814     return ret;
815 }
816
817 /**
818  * This method parses the query string received for REST requests and
819  * retrieves the 'subject' field.
820  *
821  * @param query querystring passed in REST request
822  * @param subject subject UUID parsed from query string
823  *
824  * @return true if query parsed successfully and found 'subject', else false.
825  */
826 static bool GetSubjectFromQueryString(const char *query, OicUuid_t *subject)
827 {
828     OicParseQueryIter_t parseIter = { .attrPos = NULL };
829
830     ParseQueryIterInit((unsigned char *) query, &parseIter);
831
832     while (GetNextQuery (&parseIter))
833     {
834         if (strncasecmp((char *) parseIter.attrPos, OIC_JSON_SUBJECTID_NAME, parseIter.attrLen) == 0)
835         {
836             VERIFY_SUCCESS(TAG, 0 != parseIter.valLen, ERROR);
837             memcpy(subject->id, parseIter.valPos, parseIter.valLen);
838             return true;
839         }
840     }
841
842 exit:
843     return false;
844 }
845
846 /**
847  * This method parses the query string received for REST requests and
848  * retrieves the 'resource' field.
849  *
850  * @param query querystring passed in REST request
851  * @param resource resource parsed from query string
852  * @param resourceSize size of the memory pointed to resource
853  *
854  * @return true if query parsed successfully and found 'resource', else false.
855  */
856 static bool GetResourceFromQueryString(const char *query, char *resource, size_t resourceSize)
857 {
858     OicParseQueryIter_t parseIter = { .attrPos = NULL };
859
860     ParseQueryIterInit((unsigned char *) query, &parseIter);
861
862     while (GetNextQuery (&parseIter))
863     {
864         if (strncasecmp((char *) parseIter.attrPos, OIC_JSON_RESOURCES_NAME, parseIter.attrLen)
865                 == 0)
866         {
867             VERIFY_SUCCESS(TAG, 0 != parseIter.valLen, ERROR);
868             OICStrcpy(resource, resourceSize, (char *) parseIter.valPos);
869
870             return true;
871         }
872     }
873
874 exit:
875    return false;
876 }
877
878 static OCEntityHandlerResult HandleACLGetRequest(const OCEntityHandlerRequest *ehRequest)
879 {
880     OIC_LOG(INFO, TAG, "HandleACLGetRequest processing the request");
881     uint8_t* payload = NULL;
882     size_t size = 0;
883     OCEntityHandlerResult ehRet;
884
885     // Process the REST querystring parameters
886     if (ehRequest->query)
887     {
888         OIC_LOG(DEBUG, TAG, "HandleACLGetRequest processing query");
889
890         OicUuid_t subject = {.id= { 0 } };
891         char resource[MAX_URI_LENGTH] = { 0 };
892
893         OicSecAcl_t *savePtr = NULL;
894         const OicSecAcl_t *currentAce = NULL;
895
896         // 'Subject' field is MUST for processing a querystring in REST request.
897         VERIFY_SUCCESS(TAG, true == GetSubjectFromQueryString(ehRequest->query, &subject), ERROR);
898
899         GetResourceFromQueryString(ehRequest->query, resource, sizeof(resource));
900
901         /*
902          * TODO : Currently, this code only provides one ACE for a Subject.
903          * Below code needs to be updated for scenarios when Subject have
904          * multiple ACE's in ACL resource.
905          */
906         while ((currentAce = GetACLResourceData(&subject, &savePtr)))
907         {
908             /*
909              * If REST querystring contains a specific resource, we need
910              * to search for that resource in ACE.
911              */
912             if (resource[0] != '\0')
913             {
914                 for (size_t n = 0; n < currentAce->resourcesLen; n++)
915                 {
916                     if ((currentAce->resources[n])
917                             && (0 == strcmp(resource, currentAce->resources[n])
918                                     || 0 == strcmp(WILDCARD_RESOURCE_URI, currentAce->resources[n])))
919                     {
920                         // Convert ACL data into CBOR format for transmission
921                         if (OC_STACK_OK != AclToCBORPayload(currentAce, &payload, &size))
922                         {
923                             ehRet = OC_EH_ERROR;
924                         }
925                         goto exit;
926                     }
927                 }
928             }
929             else
930             {
931                 // Convert ACL data into CBOR format for transmission
932                 if (OC_STACK_OK != AclToCBORPayload(currentAce, &payload, &size))
933                 {
934                     ehRet = OC_EH_ERROR;
935                 }
936                 goto exit;
937             }
938         }
939     }
940     else
941     {
942         // Convert ACL data into CBOR format for transmission.
943         if (OC_STACK_OK != AclToCBORPayload(gAcl, &payload, &size))
944         {
945             ehRet = OC_EH_ERROR;
946         }
947     }
948 exit:
949     // A device should always have a default acl. Therefore, payload should never be NULL.
950     ehRet = (payload ? OC_EH_OK : OC_EH_ERROR);
951
952     // Send response payload to request originator
953     SendSRMCBORResponse(ehRequest, ehRet, payload, size);
954
955     OIC_LOG_V(DEBUG, TAG, "%s RetVal %d", __func__, ehRet);
956     return ehRet;
957 }
958
959 static OCEntityHandlerResult HandleACLPostRequest(const OCEntityHandlerRequest *ehRequest)
960 {
961     OIC_LOG(INFO, TAG, "HandleACLPostRequest processing the request");
962     OCEntityHandlerResult ehRet = OC_EH_ERROR;
963
964     // Convert CBOR into ACL data and update to SVR buffers. This will also validate the ACL data received.
965     uint8_t *payload = ((OCSecurityPayload *) ehRequest->payload)->securityData1;
966     size_t size = ((OCSecurityPayload *) ehRequest->payload)->payloadSize;
967     if (payload)
968     {
969         OicSecAcl_t *newAcl = CBORPayloadToAcl(payload, size);
970         if (newAcl)
971         {
972             // Append the new ACL to existing ACL
973             LL_APPEND(gAcl, newAcl);
974             size_t size = 0;
975             // In case of unit test do not update persistant storage.
976             if (memcmp(newAcl->subject.id, &WILDCARD_SUBJECT_ID, sizeof(newAcl->subject.id)) == 0
977                 || memcmp(newAcl->subject.id, &WILDCARD_SUBJECT_B64_ID, sizeof(newAcl->subject.id)) == 0)
978             {
979                 ehRet = OC_EH_RESOURCE_CREATED;
980             }
981             else
982             {
983                 uint8_t *cborPayload = NULL;
984                 if (OC_STACK_OK == AclToCBORPayload(gAcl, &cborPayload, &size))
985                 {
986                     if (UpdateSecureResourceInPS(OIC_JSON_ACL_NAME, cborPayload, size) == OC_STACK_OK)
987                     {
988                         ehRet = OC_EH_RESOURCE_CREATED;
989                     }
990                     OICFree(cborPayload);
991                 }
992             }
993         }
994     }
995
996     // Send payload to request originator
997     SendSRMCBORResponse(ehRequest, ehRet, NULL, 0);
998
999     OIC_LOG_V(DEBUG, TAG, "%s RetVal %d", __func__, ehRet);
1000     return ehRet;
1001 }
1002
1003 static OCEntityHandlerResult HandleACLDeleteRequest(const OCEntityHandlerRequest *ehRequest)
1004 {
1005     OIC_LOG(DEBUG, TAG, "Processing ACLDeleteRequest");
1006     OCEntityHandlerResult ehRet = OC_EH_ERROR;
1007     OicUuid_t subject = { .id= { 0 } };
1008     char resource[MAX_URI_LENGTH] = { 0 };
1009
1010     VERIFY_NON_NULL(TAG, ehRequest->query, ERROR);
1011
1012     // 'Subject' field is MUST for processing a querystring in REST request.
1013     VERIFY_SUCCESS(TAG, true == GetSubjectFromQueryString(ehRequest->query, &subject), ERROR);
1014
1015     GetResourceFromQueryString(ehRequest->query, resource, sizeof(resource));
1016
1017     if (OC_STACK_RESOURCE_DELETED == RemoveACE(&subject, resource))
1018     {
1019         ehRet = OC_EH_RESOURCE_DELETED;
1020     }
1021
1022 exit:
1023     // Send payload to request originator
1024     SendSRMCBORResponse(ehRequest, ehRet, NULL, 0);
1025
1026     return ehRet;
1027 }
1028
1029 OCEntityHandlerResult ACLEntityHandler(OCEntityHandlerFlag flag, OCEntityHandlerRequest * ehRequest,
1030         void* callbackParameter)
1031 {
1032     OIC_LOG(DEBUG, TAG, "Received request ACLEntityHandler");
1033     (void)callbackParameter;
1034     OCEntityHandlerResult ehRet = OC_EH_ERROR;
1035
1036     if (!ehRequest)
1037     {
1038         return ehRet;
1039     }
1040
1041     if (flag & OC_REQUEST_FLAG)
1042     {
1043         // TODO :  Handle PUT method
1044         OIC_LOG(DEBUG, TAG, "Flag includes OC_REQUEST_FLAG");
1045         switch (ehRequest->method)
1046         {
1047             case OC_REST_GET:
1048                 ehRet = HandleACLGetRequest(ehRequest);
1049                 break;
1050
1051             case OC_REST_POST:
1052                 ehRet = HandleACLPostRequest(ehRequest);
1053                 break;
1054
1055             case OC_REST_DELETE:
1056                 ehRet = HandleACLDeleteRequest(ehRequest);
1057                 break;
1058
1059             default:
1060                 ehRet = OC_EH_ERROR;
1061                 SendSRMCBORResponse(ehRequest, ehRet, NULL, 0);
1062         }
1063     }
1064
1065     return ehRet;
1066 }
1067
1068 /**
1069  * This internal method is used to create '/oic/sec/acl' resource.
1070  */
1071 static OCStackResult CreateACLResource()
1072 {
1073     OCStackResult ret;
1074
1075     ret = OCCreateResource(&gAclHandle,
1076                            OIC_RSRC_TYPE_SEC_ACL,
1077                            OIC_MI_DEF,
1078                            OIC_RSRC_ACL_URI,
1079                            ACLEntityHandler,
1080                            NULL,
1081                            OC_OBSERVABLE | OC_SECURE | OC_EXPLICIT_DISCOVERABLE);
1082
1083     if (OC_STACK_OK != ret)
1084     {
1085         OIC_LOG(FATAL, TAG, "Unable to instantiate ACL resource");
1086         DeInitACLResource();
1087     }
1088     return ret;
1089 }
1090
1091 // This function sets the default ACL and is defined for the unit test only.
1092 OCStackResult SetDefaultACL(OicSecAcl_t *acl)
1093 {
1094     gAcl = acl;
1095     return OC_STACK_OK;
1096 }
1097
1098 OCStackResult GetDefaultACL(OicSecAcl_t** defaultAcl)
1099 {
1100     OCStackResult ret = OC_STACK_ERROR;
1101
1102     OicUuid_t ownerId = { .id = { 0 } };
1103
1104     /*
1105      * TODO In future, when new virtual resources will be added in OIC
1106      * specification, Iotivity stack should be able to add them in
1107      * existing SVR database. To support this, we need to add 'versioning'
1108      * mechanism in SVR database.
1109      */
1110
1111     const char *rsrcs[] = {
1112         OC_RSRVD_WELL_KNOWN_URI,
1113         OC_RSRVD_DEVICE_URI,
1114         OC_RSRVD_PLATFORM_URI,
1115         OC_RSRVD_RESOURCE_TYPES_URI,
1116 #ifdef WITH_PRESENCE
1117         OC_RSRVD_PRESENCE_URI,
1118 #endif //WITH_PRESENCE
1119         OIC_RSRC_ACL_URI,
1120         OIC_RSRC_DOXM_URI,
1121         OIC_RSRC_PSTAT_URI,
1122     };
1123
1124     if (!defaultAcl)
1125     {
1126         return OC_STACK_INVALID_PARAM;
1127     }
1128
1129     OicSecAcl_t *acl = (OicSecAcl_t *) OICCalloc(1, sizeof(OicSecAcl_t));
1130     VERIFY_NON_NULL(TAG, acl, ERROR);
1131
1132     // Subject -- Mandatory
1133     memcpy(&(acl->subject), &WILDCARD_SUBJECT_ID, sizeof(acl->subject));
1134
1135     // Resources -- Mandatory
1136     acl->resourcesLen = sizeof(rsrcs) / sizeof(rsrcs[0]);
1137
1138     acl->resources = (char**) OICCalloc(acl->resourcesLen, sizeof(char*));
1139     VERIFY_NON_NULL(TAG, (acl->resources), ERROR);
1140
1141     for (size_t i = 0; i < acl->resourcesLen; i++)
1142     {
1143         size_t len = strlen(rsrcs[i]) + 1;
1144         acl->resources[i] = (char*) OICMalloc(len * sizeof(char));
1145         VERIFY_NON_NULL(TAG, (acl->resources[i]), ERROR);
1146         OICStrcpy(acl->resources[i], len, rsrcs[i]);
1147     }
1148
1149     acl->permission = PERMISSION_READ;
1150     acl->prdRecrLen = 0;
1151     acl->periods = NULL;
1152     acl->recurrences = NULL;
1153
1154     // Device ID is the owner of this default ACL
1155     if (GetDoxmResourceData() != NULL)
1156     {
1157         ret = GetDoxmDeviceID(&ownerId);
1158         VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, FATAL);
1159     }
1160     else
1161     {
1162         OCRandomUuidResult rdm = OCGenerateUuid(ownerId.id);
1163         VERIFY_SUCCESS(TAG, RAND_UUID_OK == rdm, FATAL);
1164     }
1165
1166     memcpy(&acl->rownerID, &ownerId, sizeof(OicUuid_t));
1167
1168     acl->next = NULL;
1169
1170     *defaultAcl = acl;
1171     ret = OC_STACK_OK;
1172
1173 exit:
1174
1175     if (ret != OC_STACK_OK)
1176     {
1177         DeleteACLList(acl);
1178         acl = NULL;
1179     }
1180
1181     return ret;
1182 }
1183
1184 OCStackResult InitACLResource()
1185 {
1186     OCStackResult ret = OC_STACK_ERROR;
1187
1188     uint8_t *data = NULL;
1189     size_t size = 0;
1190     ret = GetSecureVirtualDatabaseFromPS(OIC_JSON_ACL_NAME, &data, &size);
1191     // If database read failed
1192     if (ret != OC_STACK_OK)
1193     {
1194         OIC_LOG(DEBUG, TAG, "ReadSVDataFromPS failed");
1195     }
1196     if (data)
1197     {
1198         // Read ACL resource from PS
1199         gAcl = CBORPayloadToAcl(data, size);
1200     }
1201     /*
1202      * If SVR database in persistent storage got corrupted or
1203      * is not available for some reason, a default ACL is created
1204      * which allows user to initiate ACL provisioning again.
1205      */
1206     if (!gAcl)
1207     {
1208         GetDefaultACL(&gAcl);
1209         // TODO Needs to update persistent storage
1210     }
1211     VERIFY_NON_NULL(TAG, gAcl, FATAL);
1212
1213     // Instantiate 'oic.sec.acl'
1214     ret = CreateACLResource();
1215
1216 exit:
1217     if (OC_STACK_OK != ret)
1218     {
1219         DeInitACLResource();
1220     }
1221     return ret;
1222 }
1223
1224 OCStackResult DeInitACLResource()
1225 {
1226     OCStackResult ret =  OCDeleteResource(gAclHandle);
1227     gAclHandle = NULL;
1228
1229     if (gAcl)
1230     {
1231         DeleteACLList(gAcl);
1232         gAcl = NULL;
1233     }
1234     return ret;
1235 }
1236
1237 const OicSecAcl_t* GetACLResourceData(const OicUuid_t* subjectId, OicSecAcl_t **savePtr)
1238 {
1239     OicSecAcl_t *acl = NULL;
1240     OicSecAcl_t *begin = NULL;
1241
1242     if (NULL == subjectId)
1243     {
1244         return NULL;
1245     }
1246
1247     /*
1248      * savePtr MUST point to NULL if this is the 'first' call to retrieve ACL for
1249      * subjectID.
1250      */
1251     if (NULL == *savePtr)
1252     {
1253         begin = gAcl;
1254     }
1255     else
1256     {
1257         /*
1258          * If this is a 'successive' call, search for location pointed by
1259          * savePtr and assign 'begin' to the next ACL after it in the linked
1260          * list and start searching from there.
1261          */
1262         LL_FOREACH(gAcl, acl)
1263         {
1264             if (acl == *savePtr)
1265             {
1266                 begin = acl->next;
1267             }
1268         }
1269     }
1270
1271     // Find the next ACL corresponding to the 'subjectID' and return it.
1272     LL_FOREACH(begin, acl)
1273     {
1274         if (memcmp(&(acl->subject), subjectId, sizeof(OicUuid_t)) == 0)
1275         {
1276             *savePtr = acl;
1277             return acl;
1278         }
1279     }
1280
1281     // Cleanup in case no ACL is found
1282     *savePtr = NULL;
1283     return NULL;
1284 }
1285
1286 OCStackResult InstallNewACL(const uint8_t *cborPayload, const size_t size)
1287 {
1288     OCStackResult ret = OC_STACK_ERROR;
1289
1290     // Convert CBOR format to ACL data. This will also validate the ACL data received.
1291     OicSecAcl_t* newAcl = CBORPayloadToAcl(cborPayload, size);
1292
1293     if (newAcl)
1294     {
1295         // Append the new ACL to existing ACL
1296         LL_APPEND(gAcl, newAcl);
1297
1298         // Update persistent storage only if it is not WILDCARD_SUBJECT_ID
1299         if (memcmp(newAcl->subject.id, &WILDCARD_SUBJECT_ID, sizeof(newAcl->subject.id)) == 0
1300             || memcmp(newAcl->subject.id, &WILDCARD_SUBJECT_B64_ID, sizeof(newAcl->subject.id)) == 0)
1301         {
1302             ret = OC_STACK_OK;
1303         }
1304         else
1305         {
1306             size_t size = 0;
1307             uint8_t *payload = NULL;
1308             if (OC_STACK_OK == AclToCBORPayload(gAcl, &payload, &size))
1309             {
1310                 if (UpdateSecureResourceInPS(OIC_JSON_ACL_NAME, payload, size) == OC_STACK_OK)
1311                 {
1312                     ret = OC_STACK_OK;
1313                 }
1314                 OICFree(payload);
1315             }
1316         }
1317     }
1318
1319     return ret;
1320 }
1321
1322 /**
1323  * This function generates default ACL for security resource in case of owned status.
1324  *
1325  * @return Default ACL for security resource.
1326  */
1327 static OicSecAcl_t* GetSecDefaultACL()
1328 {
1329     const char *sec_rsrcs[] = {
1330         OIC_RSRC_DOXM_URI,
1331         OIC_RSRC_PSTAT_URI
1332     };
1333     OicUuid_t ownerId = {.id = {0}};
1334     OCStackResult res = OC_STACK_ERROR;
1335     OicSecAcl_t* newDefaultAcl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
1336     VERIFY_NON_NULL(TAG, newDefaultAcl, ERROR);
1337
1338     // Subject -- Mandatory
1339     memcpy(&(newDefaultAcl->subject), &WILDCARD_SUBJECT_ID, WILDCARD_SUBJECT_ID_LEN);
1340
1341     // Resources -- Mandatory
1342     newDefaultAcl->resourcesLen = NUMBER_OF_DEFAULT_SEC_RSCS;
1343     newDefaultAcl->resources = (char**)OICCalloc(NUMBER_OF_DEFAULT_SEC_RSCS, sizeof(char*));
1344     VERIFY_NON_NULL(TAG, (newDefaultAcl->resources), ERROR);
1345
1346     for (size_t i = 0; i <  NUMBER_OF_DEFAULT_SEC_RSCS; i++)
1347     {
1348         size_t len = strlen(sec_rsrcs[i]) + 1;
1349         newDefaultAcl->resources[i] = (char*)OICMalloc(len * sizeof(char));
1350         VERIFY_NON_NULL(TAG, (newDefaultAcl->resources[i]), ERROR);
1351         OICStrcpy(newDefaultAcl->resources[i], len, sec_rsrcs[i]);
1352     }
1353
1354     // Permissions -- Mandatory
1355     newDefaultAcl->permission = PERMISSION_READ;
1356
1357     //Period -- Not Mandatory
1358     newDefaultAcl->prdRecrLen = 0;
1359     newDefaultAcl->periods = NULL;
1360
1361     //Recurrence -- Not Mandatory
1362     newDefaultAcl->recurrences = NULL;
1363
1364     // Device ID is the owner of this default ACL
1365     res = GetDoxmDeviceID(&ownerId);
1366     VERIFY_SUCCESS(TAG, OC_STACK_OK == res, FATAL);
1367
1368     // Owners -- Mandatory
1369     memcpy(&newDefaultAcl->rownerID, &ownerId, sizeof(OicUuid_t));
1370
1371     return newDefaultAcl;
1372 exit:
1373     DeleteACLList(newDefaultAcl);
1374     return NULL;
1375
1376 }
1377
1378 OCStackResult UpdateDefaultSecProvACL()
1379 {
1380     OCStackResult ret = OC_STACK_OK;
1381     OicSecAcl_t *acl = NULL;
1382     OicSecAcl_t *tmp = NULL;
1383
1384     if(gAcl)
1385     {
1386         int matchedRsrc = 0;
1387         bool isRemoved = false;
1388
1389         LL_FOREACH_SAFE(gAcl, acl, tmp)
1390         {
1391             //Find default security resource ACL
1392             if(memcmp(&acl->subject, &WILDCARD_SUBJECT_ID, sizeof(OicUuid_t)) == 0 &&
1393                 ((PERMISSION_READ | PERMISSION_WRITE) == acl->permission))
1394             {
1395                 matchedRsrc = 0;
1396
1397                 for(size_t i = 0; i < acl->resourcesLen; i++)
1398                 {
1399                     if(strncmp(acl->resources[i], OIC_RSRC_DOXM_URI,
1400                                strlen(OIC_RSRC_DOXM_URI) + 1) == 0 ||
1401                        strncmp(acl->resources[i], OIC_RSRC_CRED_URI,
1402                                strlen(OIC_RSRC_CRED_URI) + 1) == 0 ||
1403                        strncmp(acl->resources[i], OIC_RSRC_ACL_URI,
1404                                strlen(OIC_RSRC_ACL_URI) + 1) == 0 ||
1405                        strncmp(acl->resources[i], OIC_RSRC_PSTAT_URI,
1406                                strlen(OIC_RSRC_PSTAT_URI) + 1) == 0)
1407                     {
1408                         matchedRsrc++;
1409                     }
1410                 }
1411
1412                 //If default security resource ACL is detected, delete it.
1413                 if(NUMBER_OF_SEC_PROV_RSCS == matchedRsrc)
1414                 {
1415                     LL_DELETE(gAcl, acl);
1416                     FreeACE(acl);
1417                     isRemoved = true;
1418                 }
1419             }
1420         }
1421
1422         if(isRemoved)
1423         {
1424             /*
1425              * Generate new security resource ACL as follows :
1426              *      subject : "*"
1427              *      resources :  '/oic/sec/doxm', '/oic/sec/pstat'
1428              *      permission : READ
1429              */
1430             OicSecAcl_t *newDefaultAcl = GetSecDefaultACL();
1431             if (newDefaultAcl)
1432             {
1433                 LL_APPEND(gAcl, newDefaultAcl);
1434
1435                 size_t size = 0;
1436                 uint8_t *payload = NULL;
1437                 if (OC_STACK_OK == AclToCBORPayload(gAcl, &payload, &size))
1438                 {
1439                     if (UpdateSecureResourceInPS(OIC_JSON_ACL_NAME, payload, size) == OC_STACK_OK)
1440                     {
1441                         ret = OC_STACK_OK;
1442                     }
1443                     OICFree(payload);
1444                 }
1445             }
1446         }
1447     }
1448
1449     return ret;
1450 }
1451
1452 OCStackResult SetAclRownerId(const OicUuid_t* newROwner)
1453 {
1454     OCStackResult ret = OC_STACK_ERROR;
1455     uint8_t *cborPayload = NULL;
1456     size_t size = 0;
1457     OicUuid_t prevId = {.id={0}};
1458
1459     if(NULL == newROwner)
1460     {
1461         ret = OC_STACK_INVALID_PARAM;
1462     }
1463     if(NULL == gAcl)
1464     {
1465         ret = OC_STACK_NO_RESOURCE;
1466     }
1467
1468     if(newROwner && gAcl)
1469     {
1470         memcpy(prevId.id, gAcl->rownerID.id, sizeof(prevId.id));
1471         memcpy(gAcl->rownerID.id, newROwner->id, sizeof(newROwner->id));
1472
1473         ret = AclToCBORPayload(gAcl, &cborPayload, &size);
1474         VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
1475
1476         ret = UpdateSecureResourceInPS(OIC_JSON_ACL_NAME, cborPayload, size);
1477         VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
1478
1479         OICFree(cborPayload);
1480     }
1481
1482     return ret;
1483
1484 exit:
1485     OICFree(cborPayload);
1486     memcpy(gAcl->rownerID.id, prevId.id, sizeof(prevId.id));
1487     return ret;
1488 }
1489
1490 OCStackResult GetAclRownerId(OicUuid_t *rowneruuid)
1491 {
1492     OCStackResult retVal = OC_STACK_ERROR;
1493     if (gAcl)
1494     {
1495         *rowneruuid = gAcl->rownerID;
1496         retVal = OC_STACK_OK;
1497     }
1498     return retVal;
1499 }