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