Revert back cbor related patches.
[platform/upstream/iotivity.git] / resource / csdk / security / src / amaclresource.c
index 3671549..63aef6a 100644 (file)
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
+
 #include <stdlib.h>
 #include <string.h>
+#include "ocstack.h"
+#include "logger.h"
 #include "oic_malloc.h"
-#include "ocpayload.h"
-#include "payload_logging.h"
-#include "psinterface.h"
+#include "oic_string.h"
+#include "cJSON.h"
+#include "base64.h"
 #include "resourcemanager.h"
+#include "psinterface.h"
 #include "utlist.h"
 #include "srmresourcestrings.h"
-#include "srmutility.h"
 #include "amaclresource.h"
+#include "srmutility.h"
+#include <stdlib.h>
+#include <string.h>
 
 #define TAG  "SRM-AMACL"
 
-/** Default cbor payload size. This value is increased in case of CborErrorOutOfMemory.
- * The value of payload size is increased until reaching belox max cbor size. */
-static const uint8_t CBOR_SIZE = 255;
-
-/* Max cbor size payload. */
-static const uint16_t CBOR_MAX_SIZE = 4400;
-
-/** AMACL Map size - Number of mandatory items. */
-static const uint8_t AMACL_MAP_SIZE = 3;
-
-static OicSecAmacl_t *gAmacl = NULL;
+OicSecAmacl_t *gAmacl = NULL;
 static OCResourceHandle gAmaclHandle = NULL;
 
 void DeleteAmaclList(OicSecAmacl_t* amacl)
@@ -73,318 +69,193 @@ void DeleteAmaclList(OicSecAmacl_t* amacl)
     }
 }
 
-static size_t OicSecAmaclCount(const OicSecAmacl_t *secAmacl)
-{
-    size_t size = 0;
-    for (const OicSecAmacl_t *amacl = secAmacl; amacl; amacl = amacl->next)
-    {
-        size++;
-    }
-    return size;
-}
-
-OCStackResult AmaclToCBORPayload(const OicSecAmacl_t *amaclS, uint8_t **cborPayload,
-                                 size_t *cborSize)
+/*
+ * This internal method converts AMACL data into JSON format.
+ *
+ * Note: Caller needs to invoke 'free' when finished using the return string.
+ */
+char * BinToAmaclJSON(const OicSecAmacl_t * amacl)
 {
-    if (NULL == amaclS || NULL == cborPayload || NULL != *cborPayload || NULL == cborSize)
-    {
-        return OC_STACK_INVALID_PARAM;
-    }
+    cJSON *jsonRoot = NULL;
+    char *jsonStr = NULL;
 
-    OCStackResult ret = OC_STACK_ERROR;
-    size_t cborLen = *cborSize;
-    if (0 == cborLen)
+    if (amacl)
     {
-        cborLen = CBOR_SIZE;
-    }
-
-    *cborSize = 0;
-    *cborPayload = NULL;
+        jsonRoot = cJSON_CreateObject();
+        VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
 
-    CborEncoder encoder = { {.ptr = NULL }, .end = 0 };
-    CborEncoder amaclArray = { {.ptr = NULL }, .end = 0 };
-    CborError cborEncoderResult = CborNoError;
+        cJSON *jsonAmaclArray = NULL;
+        cJSON_AddItemToObject (jsonRoot, OIC_JSON_AMACL_NAME, jsonAmaclArray = cJSON_CreateArray());
+        VERIFY_NON_NULL(TAG, jsonAmaclArray, ERROR);
 
-    const OicSecAmacl_t *amacl = amaclS;
-    uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborLen);
-    VERIFY_NON_NULL(TAG, outPayload, ERROR);
-    cbor_encoder_init(&encoder, outPayload, cborLen, 0);
-
-    // Create AMACL Array
-    cborEncoderResult = cbor_encoder_create_array(&encoder, &amaclArray, OicSecAmaclCount(amacl));
-    VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding AMACL Array.");
+        while(amacl)
+        {
+            char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*)0)->id)) + 1] = {};
+            uint32_t outLen = 0;
+            B64Result b64Ret = B64_OK;
 
-    while (amacl)
-    {
-        CborEncoder amaclMap = { {.ptr = NULL }, .end = 0 };
-        cborEncoderResult = cbor_encoder_create_map(&amaclArray, &amaclMap, AMACL_MAP_SIZE);
-        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding AMACL MAP.");
+            cJSON *jsonAmacl = cJSON_CreateObject();
 
-        // Resources -- Mandatory
-        {
-            CborEncoder resources = { {.ptr = NULL }, .end = 0};
-            cborEncoderResult = cbor_encode_text_string(&amaclMap, OIC_JSON_RESOURCES_NAME,
-                strlen(OIC_JSON_RESOURCES_NAME));
-            VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding Resource Name Tag.");
-            cborEncoderResult = cbor_encoder_create_array(&amaclMap, &resources, amacl->resourcesLen);
-            VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding Resource Name Array.");
-            for (size_t i = 0; i < amacl->resourcesLen; i++)
+            // Resources -- Mandatory
+            cJSON *jsonRsrcArray = NULL;
+            cJSON_AddItemToObject(jsonAmacl, OIC_JSON_RESOURCES_NAME, jsonRsrcArray =
+                    cJSON_CreateArray());
+            VERIFY_NON_NULL(TAG, jsonRsrcArray, ERROR);
+            for (unsigned int i = 0; i < amacl->resourcesLen; i++)
             {
-                cborEncoderResult = cbor_encode_text_string(&resources, amacl->resources[i],
-                    strlen(amacl->resources[i]));
-                VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding Resource Name Value in Array.");
-
+                cJSON_AddItemToArray(jsonRsrcArray, cJSON_CreateString(amacl->resources[i]));
             }
-            cborEncoderResult = cbor_encoder_close_container(&amaclMap, &resources);
-            VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Resource Name ");
-        }
-        // Amss -- Mandatory
-        {
-            CborEncoder amss = { {.ptr = NULL }, .end = 0 };
-            cborEncoderResult = cbor_encode_text_string(&amaclMap, OIC_JSON_AMSS_NAME,
-                strlen(OIC_JSON_AMSS_NAME));
-            VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding AMSS Name Tag.");
-            cborEncoderResult = cbor_encoder_create_array(&amaclMap, &amss, amacl->amssLen);
-            VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding AMSS Name Array.");
-            for (size_t i = 0; i < amacl->amssLen; i++)
+
+            // Amss -- Mandatory
+            cJSON *jsonAmsArray = NULL;
+            cJSON_AddItemToObject(jsonAmacl, OIC_JSON_AMSS_NAME, jsonAmsArray =
+                    cJSON_CreateArray());
+            VERIFY_NON_NULL(TAG, jsonAmsArray, ERROR);
+            for (unsigned int i = 0; i < amacl->amssLen; i++)
             {
-                cborEncoderResult = cbor_encode_byte_string(&amss, amacl->amss[i].id,
-                    sizeof(amacl->amss[i].id));
-                VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding AMSS Name Value.");
+                outLen = 0;
+
+                b64Ret = b64Encode(amacl->amss[i].id, sizeof(((OicUuid_t*) 0)->id), base64Buff,
+                        sizeof(base64Buff), &outLen);
+                VERIFY_SUCCESS(TAG, b64Ret == B64_OK, ERROR);
+
+                cJSON_AddItemToArray(jsonAmsArray, cJSON_CreateString(base64Buff));
             }
-            cborEncoderResult = cbor_encoder_close_container(&amaclMap, &amss);
-            VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing AMSS Array.");
-        }
-        // Owners -- Mandatory
-        {
-            cborEncoderResult = cbor_encode_text_string(&amaclMap, OIC_JSON_OWNERS_NAME,
-                strlen(OIC_JSON_OWNERS_NAME));
-            VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding Owners Array Tag.");
-            CborEncoder owners = { {.ptr = NULL }, .end = 0};
-            cborEncoderResult = cbor_encoder_create_array(&amaclMap, &owners, amacl->ownersLen);
-            VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding Owners Array.");
-            for (size_t i = 0; i < amacl->ownersLen; i++)
+
+            // Owners -- Mandatory
+            cJSON *jsonOwnrArray = NULL;
+            cJSON_AddItemToObject(jsonAmacl, OIC_JSON_OWNERS_NAME, jsonOwnrArray =
+                    cJSON_CreateArray());
+            VERIFY_NON_NULL(TAG, jsonOwnrArray, ERROR);
+            for (unsigned int i = 0; i < amacl->ownersLen; i++)
             {
-                cborEncoderResult = cbor_encode_byte_string(&owners, (uint8_t *)amacl->owners[i].id,
-                    sizeof(amacl->owners[i].id));
-                VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding Owners Array Value.");
+                outLen = 0;
+
+                b64Ret = b64Encode(amacl->owners[i].id, sizeof(((OicUuid_t*) 0)->id), base64Buff,
+                        sizeof(base64Buff), &outLen);
+                VERIFY_SUCCESS(TAG, b64Ret == B64_OK, ERROR);
+
+                cJSON_AddItemToArray(jsonOwnrArray, cJSON_CreateString(base64Buff));
             }
-            cborEncoderResult = cbor_encoder_close_container(&amaclMap, &owners);
-            VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Owners Array.");
-        }
-        cborEncoderResult = cbor_encoder_close_container(&amaclArray, &amaclMap);
-        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing AMACL Map.");
 
-        amacl = amacl->next;
-    }
-    cborEncoderResult = cbor_encoder_close_container(&encoder, &amaclArray);
-    VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Amacl Array.");
+            // Attach current amacl node to Amacl Array
+            cJSON_AddItemToArray(jsonAmaclArray, jsonAmacl);
+            amacl = amacl->next;
+        }
 
-    if (CborNoError == cborEncoderResult)
-    {
-        *cborPayload = outPayload;
-        *cborSize = encoder.ptr - outPayload;
-        ret = OC_STACK_OK;
+        jsonStr = cJSON_PrintUnformatted(jsonRoot);
     }
 
 exit:
-    if ((CborErrorOutOfMemory == cborEncoderResult) && (cborLen < CBOR_MAX_SIZE))
+    if (jsonRoot)
     {
-       // reallocate and try again!
-       OICFree(outPayload);
-       outPayload = NULL;
-       // Since the allocated initial memory failed, double the memory.
-       cborLen += encoder.ptr - encoder.end;
-       cborEncoderResult = CborNoError;
-       ret = AmaclToCBORPayload(amaclS, cborPayload, &cborLen);
-       if (OC_STACK_OK == ret)
-       {
-           *cborSize = cborLen;
-           ret = OC_STACK_OK;
-       }
+        cJSON_Delete(jsonRoot);
     }
-
-    if (CborNoError != cborEncoderResult)
-    {
-       OICFree(outPayload);
-       outPayload = NULL;
-       *cborSize = 0;
-       *cborPayload = NULL;
-       ret = OC_STACK_ERROR;
-    }
-
-    return ret;
+    return jsonStr;
 }
 
-OCStackResult CBORPayloadToAmacl(const uint8_t *cborPayload, size_t size,
-                                 OicSecAmacl_t **secAmacl)
-{
-    if (NULL == cborPayload || NULL == secAmacl || NULL != *secAmacl)
-    {
-        return OC_STACK_INVALID_PARAM;
-    }
 
-    *secAmacl = NULL;
 
+
+/*
+ * This internal method converts JSON AMACL into binary AMACL.
+ */
+OicSecAmacl_t * JSONToAmaclBin(const char * jsonStr)
+{
     OCStackResult ret = OC_STACK_ERROR;
+    OicSecAmacl_t * headAmacl = NULL;
+    OicSecAmacl_t * prevAmacl = NULL;
+    cJSON *jsonRoot = NULL;
+    cJSON *jsonAmaclArray = NULL;
 
-    CborValue amaclCbor = { .parser = NULL };
-    CborParser parser = { .end = NULL };
-    CborError cborFindResult = CborNoError;
-    int cborLen = size;
-    if (0 == size)
-    {
-        cborLen = CBOR_SIZE;
-    }
-    cbor_parser_init(cborPayload, cborLen, 0, &parser, &amaclCbor);
+    VERIFY_NON_NULL(TAG, jsonStr, ERROR);
 
-    OicSecAmacl_t *headAmacl = NULL;
+    jsonRoot = cJSON_Parse(jsonStr);
+    VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
 
-    CborValue amaclArray = { .parser = NULL };
-    cborFindResult = cbor_value_enter_container(&amaclCbor, &amaclArray);
-    VERIFY_CBOR_SUCCESS(TAG, cborFindResult, ERROR);
+    jsonAmaclArray = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
+    VERIFY_NON_NULL(TAG, jsonAmaclArray, INFO);
 
-    while (cbor_value_is_valid(&amaclArray))
+    if (cJSON_Array == jsonAmaclArray->type)
     {
-        CborValue amaclMap = { .parser = NULL };
-        cborFindResult = cbor_value_enter_container(&amaclArray, &amaclMap);
-        VERIFY_CBOR_SUCCESS(TAG, cborFindResult, ERROR);
+        int numAmacl = cJSON_GetArraySize(jsonAmaclArray);
+        int idx = 0;
 
-        OicSecAmacl_t *amacl = (OicSecAmacl_t *) OICCalloc(1, sizeof(*amacl));
-        VERIFY_NON_NULL(TAG, amacl, ERROR);
-
-        while (cbor_value_is_valid(&amaclMap))
+        VERIFY_SUCCESS(TAG, numAmacl > 0, INFO);
+        do
         {
-            char *name = NULL;
-            size_t len = 0;
-            cborFindResult = cbor_value_dup_text_string(&amaclMap, &name, &len, NULL);
-            VERIFY_CBOR_SUCCESS(TAG, cborFindResult, ERROR);
-            cborFindResult = cbor_value_advance(&amaclMap);
-            VERIFY_CBOR_SUCCESS(TAG, cborFindResult, ERROR);
+            cJSON *jsonAmacl = cJSON_GetArrayItem(jsonAmaclArray, idx);
+            VERIFY_NON_NULL(TAG, jsonAmacl, ERROR);
 
-            CborType type = cbor_value_get_type(&amaclMap);
+            OicSecAmacl_t *amacl = (OicSecAmacl_t*)OICCalloc(1, sizeof(OicSecAmacl_t));
+            VERIFY_NON_NULL(TAG, amacl, ERROR);
 
-            // Resources -- Mandatory
-            if (0 == strcmp(OIC_JSON_RESOURCES_NAME, name))
+            headAmacl = (headAmacl) ? headAmacl : amacl;
+            if (prevAmacl)
             {
-                CborValue resources = { .parser = NULL  };
-                cborFindResult = cbor_value_get_array_length(&amaclMap, &amacl->resourcesLen);
-                VERIFY_CBOR_SUCCESS(TAG, cborFindResult, ERROR);
-
-                cborFindResult = cbor_value_enter_container(&amaclMap, &resources);
-                VERIFY_CBOR_SUCCESS(TAG, cborFindResult, ERROR);
-
-                amacl->resources = (char **) OICMalloc(amacl->resourcesLen * sizeof(*amacl->resources));
-                VERIFY_NON_NULL(TAG, amacl->resources, ERROR);
-                int i = 0;
-                while (cbor_value_is_text_string(&resources))
-                {
-                    cborFindResult = cbor_value_dup_text_string(&resources, &amacl->resources[i++],
-                        &len, NULL);
-                    VERIFY_CBOR_SUCCESS(TAG, cborFindResult, ERROR);
-                    cborFindResult = cbor_value_advance(&resources);
-                    VERIFY_CBOR_SUCCESS(TAG, cborFindResult, ERROR);
-                }
+                prevAmacl->next = amacl;
             }
 
-            // Amss -- Mandatory
-            if (0 == strcmp(OIC_JSON_AMSS_NAME, name))
+            size_t jsonObjLen = 0;
+            cJSON *jsonObj = NULL;
+
+            // Resources -- Mandatory
+            jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_RESOURCES_NAME);
+            VERIFY_NON_NULL(TAG, jsonObj, ERROR);
+            VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
+
+            amacl->resourcesLen = (size_t)cJSON_GetArraySize(jsonObj);
+            VERIFY_SUCCESS(TAG, amacl->resourcesLen > 0, ERROR);
+            amacl->resources = (char**)OICCalloc(amacl->resourcesLen, sizeof(char*));
+            VERIFY_NON_NULL(TAG, (amacl->resources), ERROR);
+
+            size_t idxx = 0;
+            do
             {
-                CborValue amss = { .parser = NULL };
-                cborFindResult = cbor_value_get_array_length(&amaclMap, &amacl->amssLen);
-                VERIFY_CBOR_SUCCESS(TAG, cborFindResult, ERROR);
-                cborFindResult = cbor_value_enter_container(&amaclMap, &amss);
-                VERIFY_CBOR_SUCCESS(TAG, cborFindResult, ERROR);
-                int i = 0;
-                amacl->amss = (OicUuid_t *)OICCalloc(amacl->amssLen, sizeof(*amacl->amss));
-                VERIFY_NON_NULL(TAG, amacl->amss, ERROR);
-                while (cbor_value_is_valid(&amss))
-                {
-                    uint8_t *amssId = NULL;
-                    cborFindResult = cbor_value_dup_byte_string(&amss, &amssId, &len, NULL);
-                    VERIFY_CBOR_SUCCESS(TAG, cborFindResult, ERROR);
-                    cborFindResult = cbor_value_advance(&amss);
-                    VERIFY_CBOR_SUCCESS(TAG, cborFindResult, ERROR);
-                    memcpy(amacl->amss[i].id, amssId, len);
-                    OICFree(amssId);
-                }
-            }
+                cJSON *jsonRsrc = cJSON_GetArrayItem(jsonObj, idxx);
+                VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
+
+                jsonObjLen = strlen(jsonRsrc->valuestring) + 1;
+                amacl->resources[idxx] = (char*)OICMalloc(jsonObjLen);
+                VERIFY_NON_NULL(TAG, (amacl->resources[idxx]), ERROR);
+                OICStrcpy(amacl->resources[idxx], jsonObjLen, jsonRsrc->valuestring);
+            } while ( ++idxx < amacl->resourcesLen);
+
+            // Amss -- Mandatory
+            VERIFY_SUCCESS( TAG, OC_STACK_OK == AddUuidArray(jsonAmacl, OIC_JSON_AMSS_NAME,
+                               &(amacl->amssLen), &(amacl->amss)), ERROR);
 
             // Owners -- Mandatory
-            if (0 == strcmp(OIC_JSON_OWNERS_NAME, name))
-            {
-                CborValue owners = { .parser = NULL };
-                cborFindResult = cbor_value_get_array_length(&amaclMap, &amacl->ownersLen);
-                VERIFY_CBOR_SUCCESS(TAG, cborFindResult, ERROR);
-                cborFindResult = cbor_value_enter_container(&amaclMap, &owners);
-                VERIFY_CBOR_SUCCESS(TAG, cborFindResult, ERROR);
-                int i = 0;
-                amacl->owners = (OicUuid_t *)OICCalloc(amacl->ownersLen, sizeof(*amacl->owners));
-                VERIFY_NON_NULL(TAG, amacl->owners, ERROR);
-                while (cbor_value_is_valid(&owners))
-                {
-                    uint8_t *owner = NULL;
-                    cborFindResult = cbor_value_dup_byte_string(&owners, &owner, &len, NULL);
-                    VERIFY_CBOR_SUCCESS(TAG, cborFindResult, ERROR);
-                    cborFindResult = cbor_value_advance(&owners);
-                    VERIFY_CBOR_SUCCESS(TAG, cborFindResult, ERROR);
-                    memcpy(amacl->owners[i].id, owner, len);
-                    OICFree(owner);
-                }
-            }
-            if (CborMapType != type && cbor_value_is_valid(&amaclMap))
-            {
-                cborFindResult = cbor_value_advance(&amaclMap);
-                VERIFY_CBOR_SUCCESS(TAG, cborFindResult, ERROR);
-            }
-            OICFree(name);
-        }
+            VERIFY_SUCCESS( TAG, OC_STACK_OK == AddUuidArray(jsonAmacl, OIC_JSON_OWNERS_NAME,
+                               &(amacl->ownersLen), &(amacl->owners)), ERROR);
 
-        amacl->next = NULL;
-        if (NULL == headAmacl)
-        {
-            headAmacl = amacl;
-        }
-        else
-        {
-            OicSecAmacl_t *temp = headAmacl;
-            while (temp->next)
-            {
-                temp = temp->next;
-            }
-            temp->next = amacl;
-        }
-        if (cbor_value_is_valid(&amaclArray))
-        {
-            cborFindResult = cbor_value_advance(&amaclArray);
-            VERIFY_CBOR_SUCCESS(TAG, cborFindResult, ERROR);
-        }
+            prevAmacl = amacl;
+        } while( ++idx < numAmacl);
     }
-    *secAmacl = headAmacl;
+
     ret = OC_STACK_OK;
+
 exit:
-    if (CborNoError != cborFindResult)
+    cJSON_Delete(jsonRoot);
+    if (OC_STACK_OK != ret)
     {
         DeleteAmaclList(headAmacl);
         headAmacl = NULL;
-        ret = OC_STACK_ERROR;
     }
-    return ret;
+    return headAmacl;
 }
 
 static OCEntityHandlerResult HandleAmaclGetRequest (const OCEntityHandlerRequest * ehRequest)
 {
     // Convert Amacl data into JSON for transmission
-    size_t size = 0;
-    uint8_t *cborPayload = NULL;
-    OCStackResult res = AmaclToCBORPayload(gAmacl, &cborPayload, &size);
+    char* jsonStr = BinToAmaclJSON(gAmacl);
 
-    OCEntityHandlerResult ehRet = (res == OC_STACK_OK) ? OC_EH_OK : OC_EH_ERROR;
+    OCEntityHandlerResult ehRet = (jsonStr ? OC_EH_OK : OC_EH_ERROR);
 
     // Send response payload to request originator
-    SendSRMCBORResponse(ehRequest, ehRet, cborPayload);
+    SendSRMResponse(ehRequest, ehRet, jsonStr);
 
-    OICFree(cborPayload);
+    OICFree(jsonStr);
 
     OIC_LOG_V (DEBUG, TAG, "%s RetVal %d", __func__ , ehRet);
     return ehRet;
@@ -394,44 +265,44 @@ static OCEntityHandlerResult HandleAmaclPostRequest (const OCEntityHandlerReques
 {
     OCEntityHandlerResult ehRet = OC_EH_ERROR;
 
-    // Convert CBOR Amacl data into binary. This will also validate the Amacl data received.
-    uint8_t *payload = ((OCSecurityPayload *) ehRequest->payload)->securityData1;;
-    if (payload)
+    // Convert JSON Amacl data into binary. This will also validate the Amacl data received.
+    OicSecAmacl_t* newAmacl = JSONToAmaclBin(((OCSecurityPayload*)ehRequest->payload)->securityData);
+
+    if (newAmacl)
     {
-        OicSecAmacl_t *newAmacl = NULL;
-        OCStackResult res = CBORPayloadToAmacl(payload, CBOR_SIZE, &newAmacl);
-        if (newAmacl && OC_STACK_OK == res)
+        // Append the new Amacl to existing Amacl
+        LL_APPEND(gAmacl, newAmacl);
+
+        // Convert Amacl data into JSON for update to persistent storage
+        char *jsonStr = BinToAmaclJSON(gAmacl);
+        if (jsonStr)
         {
-            // Append the new Amacl to existing Amacl
-            LL_APPEND(gAmacl, newAmacl);
-            size_t size = 0;
-            // Convert Amacl data into JSON for update to persistent storage.
-            uint8_t *cborPayload = NULL;
-            res = AmaclToCBORPayload(gAmacl, &cborPayload, &size);
-            if (cborPayload && (OC_STACK_OK == res) &&
-                (OC_STACK_OK == UpdateSecureResourceInPS(OIC_JSON_AMACL_NAME, cborPayload, size)))
+            cJSON *jsonAmacl = cJSON_Parse(jsonStr);
+            OICFree(jsonStr);
+
+            if ((jsonAmacl) &&
+                (OC_STACK_OK == UpdateSVRDatabase(OIC_JSON_AMACL_NAME, jsonAmacl)))
             {
                 ehRet = OC_EH_RESOURCE_CREATED;
             }
-            OICFree(cborPayload);
+            cJSON_Delete(jsonAmacl);
         }
-        OICFree(payload);
     }
 
     // Send payload to request originator
-    SendSRMCBORResponse(ehRequest, ehRet, NULL);
+    SendSRMResponse(ehRequest, ehRet, NULL);
 
-    OIC_LOG_V(DEBUG, TAG, "%s RetVal %d", __func__ , ehRet);
+    OIC_LOG_V (DEBUG, TAG, "%s RetVal %d", __func__ , ehRet);
     return ehRet;
 }
 
-/**
+/*
  * This internal method is the entity handler for Amacl resources and
  * will handle REST request (GET/PUT/POST/DEL) for them.
  */
-static OCEntityHandlerResult AmaclEntityHandler (OCEntityHandlerFlag flag,
-                                                 OCEntityHandlerRequest * ehRequest,
-                                                 void* callbackParameter)
+OCEntityHandlerResult AmaclEntityHandler (OCEntityHandlerFlag flag,
+                                          OCEntityHandlerRequest * ehRequest,
+                                          void* callbackParameter)
 {
     (void) callbackParameter;
     OCEntityHandlerResult ehRet = OC_EH_ERROR;
@@ -463,18 +334,20 @@ static OCEntityHandlerResult AmaclEntityHandler (OCEntityHandlerFlag flag,
     return ehRet;
 }
 
-/**
+/*
  * This internal method is used to create '/oic/sec/amacl' resource.
  */
-static OCStackResult CreateAmaclResource()
+OCStackResult CreateAmaclResource()
 {
-    OCStackResult ret = OCCreateResource(&gAmaclHandle,
-                                         OIC_RSRC_TYPE_SEC_AMACL,
-                                         OIC_MI_DEF,
-                                         OIC_RSRC_AMACL_URI,
-                                         AmaclEntityHandler,
-                                         NULL,
-                                         OC_OBSERVABLE);
+    OCStackResult ret;
+
+    ret = OCCreateResource(&gAmaclHandle,
+                           OIC_RSRC_TYPE_SEC_AMACL,
+                           OIC_MI_DEF,
+                           OIC_RSRC_AMACL_URI,
+                           AmaclEntityHandler,
+                           NULL,
+                           OC_OBSERVABLE);
 
     if (OC_STACK_OK != ret)
     {
@@ -484,24 +357,23 @@ static OCStackResult CreateAmaclResource()
     return ret;
 }
 
+/**
+ * Initialize Amacl resource by loading data from persistent storage.
+ *
+ * @retval  OC_STACK_OK for Success, otherwise some error value
+ */
 OCStackResult InitAmaclResource()
 {
     OCStackResult ret = OC_STACK_ERROR;
 
-    uint8_t *data = NULL;
-    size_t size = 0;
-    ret = GetSecureVirtualDatabaseFromPS(OIC_JSON_AMACL_NAME, &data, &size);
+    // Read Amacl resource from PS
+    char* jsonSVRDatabase = GetSVRDatabase();
 
-    // If database read failed
-    if (OC_STACK_OK != ret)
-    {
-        OIC_LOG(DEBUG, TAG, "ReadSVDataFromPS failed");
-    }
-    if (data)
+    if (jsonSVRDatabase)
     {
-        // Read AMACL resource from PS
-        ret = CBORPayloadToAmacl(data, size, &gAmacl);
-        OICFree(data);
+        // Convert JSON Amacl into binary format
+        gAmacl = JSONToAmaclBin(jsonSVRDatabase);
+        OICFree(jsonSVRDatabase);
     }
 
     // Instantiate 'oic/sec/amacl' resource
@@ -514,6 +386,11 @@ OCStackResult InitAmaclResource()
     return ret;
 }
 
+/**
+ * Perform cleanup for Amacl resources.
+ *
+ * @retval  none
+ */
 void DeInitAmaclResource()
 {
     OCDeleteResource(gAmaclHandle);
@@ -523,6 +400,7 @@ void DeInitAmaclResource()
     gAmacl = NULL;
 }
 
+
 OCStackResult AmaclGetAmsDeviceId(const char *resource, OicUuid_t *amsDeviceId)
 {
     OicSecAmacl_t *amacl = NULL;
@@ -534,7 +412,7 @@ OCStackResult AmaclGetAmsDeviceId(const char *resource, OicUuid_t *amsDeviceId)
     {
         for(size_t i = 0; i < amacl->resourcesLen; i++)
         {
-            if (0 == strncmp((amacl->resources[i]), resource, strlen(amacl->resources[i])))
+            if (strncmp((amacl->resources[i]), resource, strlen(amacl->resources[i])) == 0)
             {
                 //Returning the ID of the first AMS service for the resource
                 memcpy(amsDeviceId, &amacl->amss[0], sizeof(*amsDeviceId));