svace fixes
[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     VERIFY_NON_NULL(TAG, headAmacl, ERROR);
259
260     CborValue amaclMap = { .parser = NULL };
261     cborFindResult = cbor_value_enter_container(&amaclCbor, &amaclMap);
262     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering Amacl Map.");
263
264     while(cbor_value_is_valid(&amaclMap) && cbor_value_is_text_string(&amaclMap))
265     {
266         char *name = NULL;
267         size_t len = 0;
268         cborFindResult = cbor_value_dup_text_string(&amaclMap, &name, &len, NULL);
269         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Amacl Data Name Tag.");
270         cborFindResult = cbor_value_advance(&amaclMap);
271         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Amacl Data Value.");
272
273         //CborType type = cbor_value_get_type(&amaclMap);
274
275         // Resources -- Mandatory
276         if (0 == strcmp(OIC_JSON_RESOURCES_NAME, name))
277         {
278             // resource map
279             CborValue rsrcMap = { .parser = NULL  };
280             cborFindResult = cbor_value_enter_container(&amaclMap, &rsrcMap);
281             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering Resource Map");
282
283             while(cbor_value_is_valid(&rsrcMap) && cbor_value_is_text_string(&rsrcMap))
284             {
285                 // resource name
286                 char *rsrcName = NULL;
287                 size_t rsrcNameLen = 0;
288                 cborFindResult = cbor_value_dup_text_string(&rsrcMap, &rsrcName, &rsrcNameLen, NULL);
289                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Resource Data Name Tag.");
290                 cborFindResult = cbor_value_advance(&rsrcMap);
291                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Resource Data Value.");
292
293                 // rlist
294                 if (0 == strcmp(OIC_JSON_RLIST_NAME, rsrcName))
295                 {
296                     int i = 0;
297                     // TODO : Need to assign array length to OicSecAmacl_t->resources->rlistLen based of RAML spec.
298                     cborFindResult = cbor_value_get_array_length(&rsrcMap, &headAmacl->resourcesLen);
299                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Rlist Array Len.");
300
301                     CborValue rsrcArray = { .parser = NULL  };
302
303                     // rlist array
304                     cborFindResult = cbor_value_enter_container(&rsrcMap, &rsrcArray);
305                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering Rlist Array");
306
307                     // TODO : Need to check data structure of OicSecAmacl_t based on RAML spec.
308                     headAmacl->resources = (char **) OICCalloc(headAmacl->resourcesLen, sizeof(*headAmacl->resources));
309                     VERIFY_NON_NULL(TAG, headAmacl->resources, ERROR);
310
311                     while (cbor_value_is_valid(&rsrcArray))
312                     {
313                         // rMap
314                         CborValue rMap = { .parser = NULL  };
315                         cborFindResult = cbor_value_enter_container(&rsrcArray, &rMap);
316                         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering Rlist Map");
317
318                         while(cbor_value_is_valid(&rMap) && cbor_value_is_text_string(&rMap))
319                         {
320                             char *rMapName = NULL;
321                             size_t rMapNameLen = 0;
322                             cborFindResult = cbor_value_dup_text_string(&rMap, &rMapName, &rMapNameLen, NULL);
323                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding RMap Data Name Tag.");
324                             cborFindResult = cbor_value_advance(&rMap);
325                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding RMap Data Value.");
326
327                             // "href"
328                             if (0 == strcmp(OIC_JSON_HREF_NAME, rMapName))
329                             {
330                                 // TODO : Need to check data structure of OicSecAmacl_t based on RAML spec.
331                                 cborFindResult = cbor_value_dup_text_string(&rMap, &headAmacl->resources[i++], &len, NULL);
332                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Href Value.");
333                             }
334
335                             // "rt"
336                             if (0 == strcmp(OIC_JSON_RT_NAME, rMapName))
337                             {
338                                 // TODO : Need to check data structure of OicSecAmacl_t and assign based on RAML spec.
339                                 char *rtData = NULL;
340                                 cborFindResult = cbor_value_dup_text_string(&rMap, &rtData, &len, NULL);
341                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding RT Value.");
342                                 OICFree(rtData);
343                             }
344
345                             // "if"
346                             if (0 == strcmp(OIC_JSON_IF_NAME, rMapName))
347                             {
348                                 // TODO : Need to check data structure of OicSecAmacl_t and assign based on RAML spec.
349                                 char *ifData = NULL;
350                                 cborFindResult = cbor_value_dup_text_string(&rMap, &ifData, &len, NULL);
351                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding IF Value.");
352                                 OICFree(ifData);
353                             }
354
355                             if (cbor_value_is_valid(&rMap))
356                             {
357                                 cborFindResult = cbor_value_advance(&rMap);
358                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing Rlist Map.");
359                             }
360                             OICFree(rMapName);
361                         }
362
363                         if (cbor_value_is_valid(&rsrcArray))
364                         {
365                             cborFindResult = cbor_value_advance(&rsrcArray);
366                             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing Resource Array.");
367                         }
368                     }
369                 }
370
371                 if (cbor_value_is_valid(&rsrcMap))
372                 {
373                     cborFindResult = cbor_value_advance(&rsrcMap);
374                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing Resource Map.");
375                 }
376                 OICFree(rsrcName);
377             }
378
379         }
380
381         // TODO : Need to modify type of OicSecAmacl_t->amss based on RAML spec.
382          // Ams -- Mandatory
383         if (0 == strcmp(OIC_JSON_AMS_NAME, name))
384         {
385             int i = 0;
386             CborValue amsArray = { .parser = NULL };
387             cborFindResult = cbor_value_get_array_length(&amaclMap, &headAmacl->amssLen);
388             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding AMS Array Len.");
389             cborFindResult = cbor_value_enter_container(&amaclMap, &amsArray);
390             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering AMS Array Container.");
391             headAmacl->amss = (OicUuid_t *)OICCalloc(headAmacl->amssLen, sizeof(*headAmacl->amss));
392             VERIFY_NON_NULL(TAG, headAmacl->amss, ERROR);
393             while (cbor_value_is_valid(&amsArray) && cbor_value_is_text_string(&amsArray))
394             {
395                 char *amssId = NULL;
396                 cborFindResult = cbor_value_dup_text_string(&amsArray, &amssId, &len, NULL);
397                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding AMS Id.");
398                 cborFindResult = cbor_value_advance(&amsArray);
399                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing AMS.");
400                 memcpy(headAmacl->amss[i++].id, (OicUuid_t *)amssId, len);
401                 OICFree(amssId);
402             }
403         }
404
405         // Rowner -- Mandatory
406         if (0 == strcmp(OIC_JSON_ROWNERID_NAME, name) && cbor_value_is_text_string(&amaclMap))
407         {
408             char *stRowner = NULL;
409             cborFindResult = cbor_value_dup_text_string(&amaclMap, &stRowner, &len, NULL);
410             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ROwner Value.");
411
412             ret = ConvertStrToUuid(stRowner, &headAmacl->rownerID);
413             VERIFY_SUCCESS(TAG, ret == OC_STACK_OK, ERROR);
414             OICFree(stRowner);
415         }
416
417         //if (CborMapType != type && cbor_value_is_valid(&amaclMap))
418         if (cbor_value_is_valid(&amaclMap))
419         {
420             cborFindResult = cbor_value_advance(&amaclMap);
421             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing Amacl Map.");
422         }
423         OICFree(name);
424     }
425
426     *secAmacl = headAmacl;
427     ret = OC_STACK_OK;
428
429 exit:
430     if (CborNoError != cborFindResult)
431     {
432         DeleteAmaclList(headAmacl);
433         headAmacl = NULL;
434         *secAmacl = NULL;
435         ret = OC_STACK_ERROR;
436     }
437     return ret;
438 }
439
440 static OCEntityHandlerResult HandleAmaclGetRequest (const OCEntityHandlerRequest * ehRequest)
441 {
442     // Convert Amacl data into JSON for transmission
443     size_t size = 0;
444     uint8_t *cborPayload = NULL;
445     OCStackResult res = AmaclToCBORPayload(gAmacl, &cborPayload, &size);
446
447     OCEntityHandlerResult ehRet = (res == OC_STACK_OK) ? OC_EH_OK : OC_EH_ERROR;
448
449     // Send response payload to request originator
450     ehRet = ((SendSRMResponse(ehRequest, ehRet, cborPayload, size)) == OC_STACK_OK) ?
451                    OC_EH_OK : OC_EH_ERROR;
452
453     OICFree(cborPayload);
454
455     OIC_LOG_V (DEBUG, TAG, "%s RetVal %d", __func__ , ehRet);
456     return ehRet;
457 }
458
459 static OCEntityHandlerResult HandleAmaclPostRequest (const OCEntityHandlerRequest * ehRequest)
460 {
461     OCEntityHandlerResult ehRet = OC_EH_ERROR;
462
463     // Convert CBOR Amacl data into binary. This will also validate the Amacl data received.
464     uint8_t *payload = ((OCSecurityPayload *) ehRequest->payload)->securityData;
465     size_t size = ((OCSecurityPayload *) ehRequest->payload)->payloadSize;
466     if (payload)
467     {
468         OicSecAmacl_t *newAmacl = NULL;
469         OCStackResult res = CBORPayloadToAmacl(payload, size, &newAmacl);
470         if (newAmacl && OC_STACK_OK == res)
471         {
472             // Append the new Amacl to existing Amacl
473             LL_APPEND(gAmacl, newAmacl);
474             size_t size = 0;
475             // Convert Amacl data into JSON for update to persistent storage.
476             uint8_t *cborPayload = NULL;
477             res = AmaclToCBORPayload(gAmacl, &cborPayload, &size);
478             if (cborPayload && (OC_STACK_OK == res) &&
479                 (OC_STACK_OK == UpdateSecureResourceInPS(OIC_JSON_AMACL_NAME, cborPayload, size)))
480             {
481                 ehRet = OC_EH_RESOURCE_CREATED;
482             }
483             OICFree(cborPayload);
484         }
485         OICFree(payload);
486     }
487
488     // Send payload to request originator
489     if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL, 0))
490     {
491         ehRet = OC_EH_ERROR;
492         OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandleAmaclPostRequest");
493     }
494
495     OIC_LOG_V(DEBUG, TAG, "%s RetVal %d", __func__ , ehRet);
496     return ehRet;
497 }
498
499 /**
500  * This internal method is the entity handler for Amacl resources and
501  * will handle REST request (GET/PUT/POST/DEL) for them.
502  */
503 static OCEntityHandlerResult AmaclEntityHandler (OCEntityHandlerFlag flag,
504                                                  OCEntityHandlerRequest * ehRequest,
505                                                  void* callbackParameter)
506 {
507     (void) callbackParameter;
508     OCEntityHandlerResult ehRet = OC_EH_ERROR;
509
510     if (!ehRequest)
511     {
512         return ehRet;
513     }
514
515     if (flag & OC_REQUEST_FLAG)
516     {
517         OIC_LOG (DEBUG, TAG, "Flag includes OC_REQUEST_FLAG");
518         switch (ehRequest->method)
519         {
520             case OC_REST_GET:
521                 ehRet = HandleAmaclGetRequest(ehRequest);
522                 break;
523
524             case OC_REST_POST:
525                 ehRet = HandleAmaclPostRequest(ehRequest);
526                 break;
527
528             default:
529                 ehRet = OC_EH_ERROR;
530                 SendSRMResponse(ehRequest, ehRet, NULL, 0);
531         }
532     }
533
534     return ehRet;
535 }
536
537 /**
538  * This internal method is used to create '/oic/sec/amacl' resource.
539  */
540 static OCStackResult CreateAmaclResource()
541 {
542     OCStackResult ret = OCCreateResource(&gAmaclHandle,
543                                          OIC_RSRC_TYPE_SEC_AMACL,
544                                          OC_RSRVD_INTERFACE_DEFAULT,
545                                          OIC_RSRC_AMACL_URI,
546                                          AmaclEntityHandler,
547                                          NULL,
548                                          OC_OBSERVABLE);
549
550     if (OC_STACK_OK != ret)
551     {
552         OIC_LOG (FATAL, TAG, "Unable to instantiate Amacl resource");
553         DeInitAmaclResource();
554     }
555     return ret;
556 }
557
558 OCStackResult InitAmaclResource()
559 {
560     OCStackResult ret = OC_STACK_ERROR;
561
562     uint8_t *data = NULL;
563     size_t size = 0;
564     ret = GetSecureVirtualDatabaseFromPS(OIC_JSON_AMACL_NAME, &data, &size);
565
566     // If database read failed
567     if (OC_STACK_OK != ret)
568     {
569         OIC_LOG(DEBUG, TAG, "ReadSVDataFromPS failed");
570     }
571     if (data)
572     {
573         // Read AMACL resource from PS
574         ret = CBORPayloadToAmacl(data, size, &gAmacl);
575         if (OC_STACK_OK != ret)
576         {
577             OIC_LOG(DEBUG, TAG, "ReadAMACLresourcefromPS failed");
578         }
579         OICFree(data);
580     }
581
582     // Instantiate 'oic/sec/amacl' resource
583     ret = CreateAmaclResource();
584
585     if (OC_STACK_OK != ret)
586     {
587         DeInitAmaclResource();
588     }
589     return ret;
590 }
591
592 void DeInitAmaclResource()
593 {
594     OCDeleteResource(gAmaclHandle);
595     gAmaclHandle = NULL;
596
597     DeleteAmaclList(gAmacl);
598     gAmacl = NULL;
599 }
600
601 OCStackResult AmaclGetAmsDeviceId(const char *resource, OicUuid_t *amsDeviceId)
602 {
603     OicSecAmacl_t *amacl = NULL;
604
605     VERIFY_NON_NULL(TAG, resource, ERROR);
606     VERIFY_NON_NULL(TAG, amsDeviceId, ERROR);
607
608     LL_FOREACH(gAmacl, amacl)
609     {
610         for(size_t i = 0; i < amacl->resourcesLen; i++)
611         {
612             if (0 == strncmp((amacl->resources[i]), resource, strlen(amacl->resources[i])))
613             {
614                 //Returning the ID of the first AMS service for the resource
615                 memcpy(amsDeviceId, &amacl->amss[0], sizeof(*amsDeviceId));
616                 return OC_STACK_OK;
617             }
618         }
619     }
620
621 exit:
622     return OC_STACK_ERROR;
623 }
624
625 OCStackResult SetAmaclRownerId(const OicUuid_t* newROwner)
626 {
627     OCStackResult ret = OC_STACK_ERROR;
628     uint8_t *cborPayload = NULL;
629     size_t size = 0;
630     OicUuid_t prevId = {.id={0}};
631
632     if(NULL == newROwner)
633     {
634         ret = OC_STACK_INVALID_PARAM;
635     }
636     if(NULL == gAmacl)
637     {
638         ret = OC_STACK_NO_RESOURCE;
639     }
640
641     if(newROwner && gAmacl)
642     {
643         memcpy(prevId.id, gAmacl->rownerID.id, sizeof(prevId.id));
644         memcpy(gAmacl->rownerID.id, newROwner->id, sizeof(newROwner->id));
645
646         ret = AmaclToCBORPayload(gAmacl, &cborPayload, &size);
647         VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
648
649         ret = UpdateSecureResourceInPS(OIC_JSON_AMACL_NAME, cborPayload, size);
650         VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
651
652         OICFree(cborPayload);
653     }
654
655     return ret;
656
657 exit:
658     OICFree(cborPayload);
659     memcpy(gAmacl->rownerID.id, prevId.id, sizeof(prevId.id));
660     return ret;
661 }
662
663 OCStackResult GetAmaclRownerId(OicUuid_t *rowneruuid)
664 {
665     OCStackResult retVal = OC_STACK_ERROR;
666     if (gAmacl)
667     {
668         *rowneruuid = gAmacl->rownerID;
669         retVal = OC_STACK_OK;
670     }
671     return retVal;
672 }