Modify to update rowner as PT's UUID when ownership transfer is done.
[platform/upstream/iotivity.git] / resource / csdk / security / src / amaclresource.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 #include <stdlib.h>
22 #include <string.h>
23 #include "oic_malloc.h"
24 #include "ocpayload.h"
25 #include "payload_logging.h"
26 #include "psinterface.h"
27 #include "resourcemanager.h"
28 #include "utlist.h"
29 #include "srmresourcestrings.h"
30 #include "srmutility.h"
31 #include "amaclresource.h"
32
33 #define TAG  "SRM-AMACL"
34
35 /** Default cbor payload size. This value is increased in case of CborErrorOutOfMemory.
36  * The value of payload size is increased until reaching belox max cbor size. */
37 static const uint16_t CBOR_SIZE = 1024;
38
39 /* Max cbor size payload. */
40 static const uint16_t CBOR_MAX_SIZE = 4400;
41
42 /** AMACL Map size - Number of mandatory items. */
43 static const uint8_t AMACL_MAP_SIZE = 3;
44 static const uint8_t AMACL_RSRC_MAP_SIZE = 1;
45 static const uint8_t AMACL_RLIST_MAP_SIZE = 3;
46
47 static OicSecAmacl_t *gAmacl = NULL;
48 static OCResourceHandle gAmaclHandle = NULL;
49
50 void DeleteAmaclList(OicSecAmacl_t* amacl)
51 {
52     if (amacl)
53     {
54         OicSecAmacl_t *amaclTmp1 = NULL, *amaclTmp2 = NULL;
55         LL_FOREACH_SAFE(amacl, amaclTmp1, amaclTmp2)
56         {
57             LL_DELETE(amacl, amaclTmp1);
58
59             // Clean Resources
60             for (size_t i = 0; i < amaclTmp1->resourcesLen; i++)
61             {
62                 OICFree(amaclTmp1->resources[i]);
63             }
64             OICFree(amaclTmp1->resources);
65
66             // Clean Amss
67             OICFree(amaclTmp1->amss);
68
69             // Clean Amacl node itself
70             OICFree(amaclTmp1);
71         }
72     }
73 }
74
75 static size_t OicSecAmaclCount(const OicSecAmacl_t *secAmacl)
76 {
77     size_t size = 0;
78     for (const OicSecAmacl_t *amacl = secAmacl; amacl; amacl = amacl->next)
79     {
80         size++;
81     }
82     return size;
83 }
84
85 OCStackResult AmaclToCBORPayload(const OicSecAmacl_t *amaclS, uint8_t **cborPayload,
86                                  size_t *cborSize)
87 {
88     if (NULL == amaclS || NULL == cborPayload || NULL != *cborPayload || NULL == cborSize)
89     {
90         return OC_STACK_INVALID_PARAM;
91     }
92
93     OCStackResult ret = OC_STACK_ERROR;
94     size_t cborLen = *cborSize;
95     if (0 == cborLen)
96     {
97         cborLen = CBOR_SIZE;
98     }
99
100     *cborSize = 0;
101     *cborPayload = NULL;
102
103     CborEncoder encoder = { {.ptr = NULL }, .end = 0 };
104     CborEncoder amaclMap = { {.ptr = NULL }, .end = 0 };
105     int64_t cborEncoderResult = CborNoError;
106     CborEncoder rsrcMap = { {.ptr = NULL }, .end = 0 };
107     CborEncoder rlistArray = { {.ptr = NULL }, .end = 0 };
108     CborEncoder amss = { {.ptr = NULL }, .end = 0 };
109     char *stRowner = NULL;
110
111     const OicSecAmacl_t *amacl = amaclS;
112     uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborLen);
113     VERIFY_NON_NULL(TAG, outPayload, ERROR);
114     cbor_encoder_init(&encoder, outPayload, cborLen, 0);
115
116     // Create AMACL Map
117     cborEncoderResult = cbor_encoder_create_map(&encoder, &amaclMap, AMACL_MAP_SIZE);
118     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding AMACL Map.");
119
120     // resources -- Mandatory
121     cborEncoderResult = cbor_encode_text_string(&amaclMap, OIC_JSON_RESOURCES_NAME,
122                 strlen(OIC_JSON_RESOURCES_NAME));
123     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding Resource Name Tag.");
124
125     cborEncoderResult = cbor_encoder_create_map(&amaclMap, &rsrcMap, AMACL_RSRC_MAP_SIZE);
126     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding Resource Map.");
127
128
129     cborEncoderResult = cbor_encode_text_string(&rsrcMap, OIC_JSON_RLIST_NAME,
130                 strlen(OIC_JSON_RLIST_NAME));
131     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RLIST Name Tag.");
132
133     // TODO : Need to input array length by OicSecAmacl_t->resources->rlistLen based on spec.
134     cborEncoderResult = cbor_encoder_create_array(&rsrcMap, &rlistArray, amacl->resourcesLen);
135     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RLIST Array.");
136
137     // TODO : Need to add OicSecAmacl_t->rlist as array rMap based on RAML spec.
138     for (size_t i = 0; i < amacl->resourcesLen; i++)
139     {
140         // TODO : Need to create rMap structure based on RAML spec.
141         CborEncoder rMap = { {.ptr = NULL }, .end = 0 };
142         cborEncoderResult = cbor_encoder_create_map(&rlistArray, &rMap, AMACL_RLIST_MAP_SIZE);
143         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RLIST Map.");
144
145         cborEncoderResult = cbor_encode_text_string(&rMap, OIC_JSON_HREF_NAME,
146                 strlen(OIC_JSON_HREF_NAME));
147         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding HREF Name Tag.");
148         cborEncoderResult = cbor_encode_text_string(&rMap, amacl->resources[i],
149                 strlen(amacl->resources[i]));
150         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding HREF Value in Map.");
151
152         cborEncoderResult = cbor_encode_text_string(&rMap, OIC_JSON_RT_NAME,
153                 strlen(OIC_JSON_RT_NAME));
154         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RT Name Tag.");
155
156         // TODO : Need to assign real value of RT
157         cborEncoderResult = cbor_encode_text_string(&rMap, OIC_JSON_EMPTY_STRING,
158                 strlen(OIC_JSON_EMPTY_STRING));
159         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RT Value.");
160
161         cborEncoderResult = cbor_encode_text_string(&rMap, OIC_JSON_IF_NAME,
162                 strlen(OIC_JSON_IF_NAME));
163         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding IF Name Tag.");
164
165         // TODO : Need to assign real value of IF
166         cborEncoderResult = cbor_encode_text_string(&rMap, OIC_JSON_EMPTY_STRING,
167                 strlen(OIC_JSON_EMPTY_STRING));
168         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding IF Value.");
169
170         cborEncoderResult = cbor_encoder_close_container(&rlistArray, &rMap);
171         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing RLIST Array.");
172     }
173
174     cborEncoderResult = cbor_encoder_close_container(&rsrcMap, &rlistArray);
175     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing RLIST Array.");
176
177
178     cborEncoderResult = cbor_encoder_close_container(&amaclMap, &rsrcMap);
179     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Resource Map.");
180
181     // TODO : Need to modify type of OicSecAmacl_t->amss based on RAML spec.
182     // ams -- Mandatory
183     cborEncoderResult = cbor_encode_text_string(&amaclMap, OIC_JSON_AMS_NAME,
184                 strlen(OIC_JSON_AMS_NAME));
185     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding AMSS Name Tag.");
186
187     cborEncoderResult = cbor_encoder_create_array(&amaclMap, &amss, amacl->amssLen);
188     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding AMS Name Array.");
189     for (size_t i = 0; i < amacl->amssLen; i++)
190     {
191         cborEncoderResult = cbor_encode_text_string(&amss, (const char *)amacl->amss[i].id,
192             sizeof(amacl->amss[i].id));
193         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding AMS Name Value.");
194     }
195     cborEncoderResult = cbor_encoder_close_container(&amaclMap, &amss);
196     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing AMSS Array.");
197
198     // TODO : Need to check owner property in the RAML spec.
199     // rowner -- Mandatory
200     cborEncoderResult = cbor_encode_text_string(&amaclMap, OIC_JSON_ROWNERID_NAME,
201                 strlen(OIC_JSON_ROWNERID_NAME));
202     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding ROwnerID Name Tag.");
203
204     ret = ConvertUuidToStr(&amacl->rownerID, &stRowner);
205     VERIFY_SUCCESS(TAG, ret == OC_STACK_OK, ERROR);
206     cborEncoderResult = cbor_encode_text_string(&amaclMap, stRowner, strlen(stRowner));
207     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding ROwner Value.");
208     OICFree(stRowner);
209
210     cborEncoderResult = cbor_encoder_close_container(&encoder, &amaclMap);
211     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Amacl Map.");
212
213     if (CborNoError == cborEncoderResult)
214     {
215         *cborPayload = outPayload;
216         *cborSize = encoder.ptr - outPayload;
217         ret = OC_STACK_OK;
218     }
219
220 exit:
221     if ((CborErrorOutOfMemory == cborEncoderResult) && (cborLen < CBOR_MAX_SIZE))
222     {
223        // reallocate and try again!
224        OICFree(outPayload);
225        outPayload = NULL;
226        // Since the allocated initial memory failed, double the memory.
227        cborLen += encoder.ptr - encoder.end;
228        cborEncoderResult = CborNoError;
229        ret = AmaclToCBORPayload(amaclS, cborPayload, &cborLen);
230        if (OC_STACK_OK == ret)
231        {
232            *cborSize = cborLen;
233            ret = OC_STACK_OK;
234        }
235     }
236
237     if (CborNoError != cborEncoderResult || ret != OC_STACK_OK)
238     {
239        OICFree(outPayload);
240        outPayload = NULL;
241        *cborSize = 0;
242        *cborPayload = NULL;
243        ret = OC_STACK_ERROR;
244     }
245
246     return ret;
247 }
248
249 OCStackResult CBORPayloadToAmacl(const uint8_t *cborPayload, size_t size,
250                                  OicSecAmacl_t **secAmacl)
251 {
252     if (NULL == cborPayload || NULL == secAmacl || NULL != *secAmacl)
253     {
254         return OC_STACK_INVALID_PARAM;
255     }
256
257     *secAmacl = NULL;
258
259     OCStackResult ret = OC_STACK_ERROR;
260
261     CborValue amaclCbor = { .parser = NULL };
262     CborParser parser = { .end = NULL };
263     CborError cborFindResult = CborNoError;
264     int cborLen = size;
265     if (0 == size)
266     {
267         cborLen = CBOR_SIZE;
268     }
269     cbor_parser_init(cborPayload, cborLen, 0, &parser, &amaclCbor);
270
271     OicSecAmacl_t *headAmacl = (OicSecAmacl_t *)OICCalloc(1, sizeof(OicSecAmacl_t));
272
273     CborValue amaclMap = { .parser = NULL };
274     cborFindResult = cbor_value_enter_container(&amaclCbor, &amaclMap);
275     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering Amacl Map.");
276
277     while(cbor_value_is_valid(&amaclMap))
278     {
279         char *name = NULL;
280         size_t len = 0;
281         cborFindResult = cbor_value_dup_text_string(&amaclMap, &name, &len, NULL);
282         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Amacl Data Name Tag.");
283         cborFindResult = cbor_value_advance(&amaclMap);
284         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Amacl Data Value.");
285
286         //CborType type = cbor_value_get_type(&amaclMap);
287
288         // Resources -- Mandatory
289         if (0 == strcmp(OIC_JSON_RESOURCES_NAME, name))
290         {
291             // resource map
292             CborValue rsrcMap = { .parser = NULL  };
293             cborFindResult = cbor_value_enter_container(&amaclMap, &rsrcMap);
294             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering Resource Map");
295
296             while(cbor_value_is_valid(&rsrcMap))
297             {
298                 // resource name
299                 char *rsrcName = NULL;
300                 size_t rsrcNameLen = 0;
301                 cborFindResult = cbor_value_dup_text_string(&rsrcMap, &rsrcName, &rsrcNameLen, NULL);
302                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Resource Data Name Tag.");
303                 cborFindResult = cbor_value_advance(&rsrcMap);
304                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Resource Data Value.");
305
306                 // rlist
307                 if (0 == strcmp(OIC_JSON_RLIST_NAME, rsrcName))
308                 {
309                     int i = 0;
310                     // TODO : Need to assign array length to OicSecAmacl_t->resources->rlistLen based of RAML spec.
311                     cborFindResult = cbor_value_get_array_length(&rsrcMap, &headAmacl->resourcesLen);
312                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Rlist Array Len.");
313
314                     CborValue rsrcArray = { .parser = NULL  };
315
316                     // rlist array
317                     cborFindResult = cbor_value_enter_container(&rsrcMap, &rsrcArray);
318                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering Rlist Array");
319
320                     // TODO : Need to check data structure of OicSecAmacl_t based on RAML spec.
321                     headAmacl->resources = (char **) OICCalloc(headAmacl->resourcesLen, sizeof(*headAmacl->resources));
322                     VERIFY_NON_NULL(TAG, headAmacl->resources, ERROR);
323
324                     while (cbor_value_is_valid(&rsrcArray))
325                     {
326                         // rMap
327                         CborValue rMap = { .parser = NULL  };
328                         cborFindResult = cbor_value_enter_container(&rsrcArray, &rMap);
329                         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering Rlist Map");
330
331                         while(cbor_value_is_valid(&rMap))
332                         {
333                             char *rMapName = NULL;
334                             size_t rMapNameLen = 0;
335                             cborFindResult = cbor_value_dup_text_string(&rMap, &rMapName, &rMapNameLen, NULL);
336                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding RMap Data Name Tag.");
337                             cborFindResult = cbor_value_advance(&rMap);
338                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding RMap Data Value.");
339
340                             // "href"
341                             if (0 == strcmp(OIC_JSON_HREF_NAME, rMapName))
342                             {
343                                 // TODO : Need to check data structure of OicSecAmacl_t based on RAML spec.
344                                 cborFindResult = cbor_value_dup_text_string(&rMap, &headAmacl->resources[i++], &len, NULL);
345                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Href Value.");
346                             }
347
348                             // "rt"
349                             if (0 == strcmp(OIC_JSON_RT_NAME, rMapName))
350                             {
351                                 // TODO : Need to check data structure of OicSecAmacl_t and assign based on RAML spec.
352                                 char *rtData = NULL;
353                                 cborFindResult = cbor_value_dup_text_string(&rMap, &rtData, &len, NULL);
354                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding RT Value.");
355                                 OICFree(rtData);
356                             }
357
358                             // "if"
359                             if (0 == strcmp(OIC_JSON_IF_NAME, rMapName))
360                             {
361                                 // TODO : Need to check data structure of OicSecAmacl_t and assign based on RAML spec.
362                                 char *ifData = NULL;
363                                 cborFindResult = cbor_value_dup_text_string(&rMap, &ifData, &len, NULL);
364                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding IF Value.");
365                                 OICFree(ifData);
366                             }
367
368                             if (cbor_value_is_valid(&rMap))
369                             {
370                                 cborFindResult = cbor_value_advance(&rMap);
371                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing Rlist Map.");
372                             }
373                             OICFree(rMapName);
374                         }
375
376                         if (cbor_value_is_valid(&rsrcArray))
377                         {
378                             cborFindResult = cbor_value_advance(&rsrcArray);
379                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing Resource Array.");
380                         }
381                     }
382                 }
383
384                 if (cbor_value_is_valid(&rsrcMap))
385                 {
386                     cborFindResult = cbor_value_advance(&rsrcMap);
387                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing Resource Map.");
388                 }
389                 OICFree(rsrcName);
390             }
391
392         }
393
394         // TODO : Need to modify type of OicSecAmacl_t->amss based on RAML spec.
395          // Ams -- Mandatory
396         if (0 == strcmp(OIC_JSON_AMS_NAME, name))
397         {
398             int i = 0;
399             CborValue amsArray = { .parser = NULL };
400             cborFindResult = cbor_value_get_array_length(&amaclMap, &headAmacl->amssLen);
401             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding AMS Array Len.");
402             cborFindResult = cbor_value_enter_container(&amaclMap, &amsArray);
403             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering AMS Array Container.");
404             headAmacl->amss = (OicUuid_t *)OICCalloc(headAmacl->amssLen, sizeof(*headAmacl->amss));
405             VERIFY_NON_NULL(TAG, headAmacl->amss, ERROR);
406             while (cbor_value_is_valid(&amsArray))
407             {
408                 char *amssId = NULL;
409                 cborFindResult = cbor_value_dup_text_string(&amsArray, &amssId, &len, NULL);
410                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding AMS Id.");
411                 cborFindResult = cbor_value_advance(&amsArray);
412                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing AMS.");
413                 memcpy(headAmacl->amss[i++].id, (OicUuid_t *)amssId, len);
414                 OICFree(amssId);
415             }
416         }
417
418         // Rowner -- Mandatory
419         if (0 == strcmp(OIC_JSON_ROWNERID_NAME, name))
420         {
421             char *stRowner = NULL;
422             cborFindResult = cbor_value_dup_text_string(&amaclMap, &stRowner, &len, NULL);
423             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ROwner Value.");
424
425             ret = ConvertStrToUuid(stRowner, &headAmacl->rownerID);
426             VERIFY_SUCCESS(TAG, ret == OC_STACK_OK, ERROR);
427             OICFree(stRowner);
428         }
429
430         //if (CborMapType != type && cbor_value_is_valid(&amaclMap))
431         if (cbor_value_is_valid(&amaclMap))
432         {
433             cborFindResult = cbor_value_advance(&amaclMap);
434             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing Amacl Map.");
435         }
436         OICFree(name);
437     }
438
439     *secAmacl = headAmacl;
440     ret = OC_STACK_OK;
441
442 exit:
443     if (CborNoError != cborFindResult)
444     {
445         DeleteAmaclList(headAmacl);
446         headAmacl = NULL;
447         *secAmacl = NULL;
448         ret = OC_STACK_ERROR;
449     }
450     return ret;
451 }
452
453 static OCEntityHandlerResult HandleAmaclGetRequest (const OCEntityHandlerRequest * ehRequest)
454 {
455     // Convert Amacl data into JSON for transmission
456     size_t size = 0;
457     uint8_t *cborPayload = NULL;
458     OCStackResult res = AmaclToCBORPayload(gAmacl, &cborPayload, &size);
459
460     OCEntityHandlerResult ehRet = (res == OC_STACK_OK) ? OC_EH_OK : OC_EH_ERROR;
461
462     // Send response payload to request originator
463     SendSRMCBORResponse(ehRequest, ehRet, cborPayload, size);
464
465     OICFree(cborPayload);
466
467     OIC_LOG_V (DEBUG, TAG, "%s RetVal %d", __func__ , ehRet);
468     return ehRet;
469 }
470
471 static OCEntityHandlerResult HandleAmaclPostRequest (const OCEntityHandlerRequest * ehRequest)
472 {
473     OCEntityHandlerResult ehRet = OC_EH_ERROR;
474
475     // Convert CBOR Amacl data into binary. This will also validate the Amacl data received.
476     uint8_t *payload = ((OCSecurityPayload *) ehRequest->payload)->securityData1;
477     size_t size = ((OCSecurityPayload *) ehRequest->payload)->payloadSize;
478     if (payload)
479     {
480         OicSecAmacl_t *newAmacl = NULL;
481         OCStackResult res = CBORPayloadToAmacl(payload, size, &newAmacl);
482         if (newAmacl && OC_STACK_OK == res)
483         {
484             // Append the new Amacl to existing Amacl
485             LL_APPEND(gAmacl, newAmacl);
486             size_t size = 0;
487             // Convert Amacl data into JSON for update to persistent storage.
488             uint8_t *cborPayload = NULL;
489             res = AmaclToCBORPayload(gAmacl, &cborPayload, &size);
490             if (cborPayload && (OC_STACK_OK == res) &&
491                 (OC_STACK_OK == UpdateSecureResourceInPS(OIC_JSON_AMACL_NAME, cborPayload, size)))
492             {
493                 ehRet = OC_EH_RESOURCE_CREATED;
494             }
495             OICFree(cborPayload);
496         }
497         OICFree(payload);
498     }
499
500     // Send payload to request originator
501     SendSRMCBORResponse(ehRequest, ehRet, NULL, 0);
502
503     OIC_LOG_V(DEBUG, TAG, "%s RetVal %d", __func__ , ehRet);
504     return ehRet;
505 }
506
507 /**
508  * This internal method is the entity handler for Amacl resources and
509  * will handle REST request (GET/PUT/POST/DEL) for them.
510  */
511 static OCEntityHandlerResult AmaclEntityHandler (OCEntityHandlerFlag flag,
512                                                  OCEntityHandlerRequest * ehRequest,
513                                                  void* callbackParameter)
514 {
515     (void) callbackParameter;
516     OCEntityHandlerResult ehRet = OC_EH_ERROR;
517
518     if (!ehRequest)
519     {
520         return ehRet;
521     }
522
523     if (flag & OC_REQUEST_FLAG)
524     {
525         OIC_LOG (DEBUG, TAG, "Flag includes OC_REQUEST_FLAG");
526         switch (ehRequest->method)
527         {
528             case OC_REST_GET:
529                 ehRet = HandleAmaclGetRequest(ehRequest);
530                 break;
531
532             case OC_REST_POST:
533                 ehRet = HandleAmaclPostRequest(ehRequest);
534                 break;
535
536             default:
537                 ehRet = OC_EH_ERROR;
538                 SendSRMCBORResponse(ehRequest, ehRet, NULL, 0);
539         }
540     }
541
542     return ehRet;
543 }
544
545 /**
546  * This internal method is used to create '/oic/sec/amacl' resource.
547  */
548 static OCStackResult CreateAmaclResource()
549 {
550     OCStackResult ret = OCCreateResource(&gAmaclHandle,
551                                          OIC_RSRC_TYPE_SEC_AMACL,
552                                          OIC_MI_DEF,
553                                          OIC_RSRC_AMACL_URI,
554                                          AmaclEntityHandler,
555                                          NULL,
556                                          OC_OBSERVABLE);
557
558     if (OC_STACK_OK != ret)
559     {
560         OIC_LOG (FATAL, TAG, "Unable to instantiate Amacl resource");
561         DeInitAmaclResource();
562     }
563     return ret;
564 }
565
566 OCStackResult InitAmaclResource()
567 {
568     OCStackResult ret = OC_STACK_ERROR;
569
570     uint8_t *data = NULL;
571     size_t size = 0;
572     ret = GetSecureVirtualDatabaseFromPS(OIC_JSON_AMACL_NAME, &data, &size);
573
574     // If database read failed
575     if (OC_STACK_OK != ret)
576     {
577         OIC_LOG(DEBUG, TAG, "ReadSVDataFromPS failed");
578     }
579     if (data)
580     {
581         // Read AMACL resource from PS
582         ret = CBORPayloadToAmacl(data, size, &gAmacl);
583         if (OC_STACK_OK != ret)
584         {
585             OIC_LOG(DEBUG, TAG, "ReadAMACLresourcefromPS failed");
586         }
587         OICFree(data);
588     }
589
590     // Instantiate 'oic/sec/amacl' resource
591     ret = CreateAmaclResource();
592
593     if (OC_STACK_OK != ret)
594     {
595         DeInitAmaclResource();
596     }
597     return ret;
598 }
599
600 void DeInitAmaclResource()
601 {
602     OCDeleteResource(gAmaclHandle);
603     gAmaclHandle = NULL;
604
605     DeleteAmaclList(gAmacl);
606     gAmacl = NULL;
607 }
608
609 OCStackResult AmaclGetAmsDeviceId(const char *resource, OicUuid_t *amsDeviceId)
610 {
611     OicSecAmacl_t *amacl = NULL;
612
613     VERIFY_NON_NULL(TAG, resource, ERROR);
614     VERIFY_NON_NULL(TAG, amsDeviceId, ERROR);
615
616     LL_FOREACH(gAmacl, amacl)
617     {
618         for(size_t i = 0; i < amacl->resourcesLen; i++)
619         {
620             if (0 == strncmp((amacl->resources[i]), resource, strlen(amacl->resources[i])))
621             {
622                 //Returning the ID of the first AMS service for the resource
623                 memcpy(amsDeviceId, &amacl->amss[0], sizeof(*amsDeviceId));
624                 return OC_STACK_OK;
625             }
626         }
627     }
628
629 exit:
630     return OC_STACK_ERROR;
631 }
632
633
634 OCStackResult SetAmaclRownerId(const OicUuid_t* newROwner)
635 {
636     OCStackResult ret = OC_STACK_ERROR;
637     uint8_t *cborPayload = NULL;
638     size_t size = 0;
639     OicUuid_t prevId = {.id={0}};
640
641     if(NULL == newROwner)
642     {
643         ret = OC_STACK_INVALID_PARAM;
644     }
645     if(NULL == gAmacl)
646     {
647         ret = OC_STACK_NO_RESOURCE;
648     }
649
650     if(newROwner && gAmacl)
651     {
652         memcpy(prevId.id, gAmacl->rownerID.id, sizeof(prevId.id));
653         memcpy(gAmacl->rownerID.id, newROwner->id, sizeof(newROwner->id));
654
655         ret = AmaclToCBORPayload(gAmacl, &cborPayload, &size);
656         VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
657
658         ret = UpdateSecureResourceInPS(OIC_JSON_AMACL_NAME, cborPayload, size);
659         VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
660
661         OICFree(cborPayload);
662     }
663
664     return ret;
665
666 exit:
667     OICFree(cborPayload);
668     memcpy(gAmacl->rownerID.id, prevId.id, sizeof(prevId.id));
669     return ret;
670 }
671