replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / src / pconfresource.c
index 921b249..da0b9c0 100644 (file)
  *
  * *****************************************************************/
 
+#include "iotivity_config.h"
 #include <stdlib.h>
 #include <string.h>
 #include "ocstack.h"
 #include "logger.h"
 #include "oic_malloc.h"
 #include "oic_string.h"
+#if defined (__TIZENRT__)
+#include <apps/netutils/cJSON.h>
+#else
 #include "cJSON.h"
+#endif
 #include "base64.h"
 #include "ocpayload.h"
+#include "ocpayloadcbor.h"
 #include "payload_logging.h"
 #include "resourcemanager.h"
 #include "pconfresource.h"
 #include "doxmresource.h"
 #include "srmutility.h"
 #include "ocserverrequest.h"
-#include <stdlib.h>
 #include "psinterface.h"
 #include "security_internals.h"
-#ifdef WITH_ARDUINO
-#include <string.h>
-#else
+#ifdef HAVE_STRINGS_H
 #include <strings.h>
 #endif
 
-#define TAG  "SRM-PCONF"
+#define TAG  "OIC_SRM_PCONF"
 
 static const uint16_t CBOR_SIZE = 1024;
 static const uint64_t CBOR_MAX_SIZE = 4400;
@@ -427,7 +430,7 @@ OCStackResult PconfToCBORPayload(const OicSecPconf_t *pconf,uint8_t **payload,si
     cborEncoderResult = cbor_encoder_close_container(&encoder, &pconfMap);
     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to close pconfMap");
 
-    *size = encoder.ptr - outPayload;
+    *size = cbor_encoder_get_buffer_size(&encoder, outPayload);
     *payload = outPayload;
     ret = OC_STACK_OK;
 exit:
@@ -436,7 +439,7 @@ exit:
         // reallocate and try again!
         OICFree(outPayload);
         // Since the allocated initial memory failed, double the memory.
-        cborLen += encoder.ptr - encoder.end;
+        cborLen += cbor_encoder_get_buffer_size(&encoder, encoder.end);
         cborEncoderResult = CborNoError;
         ret = PconfToCBORPayload(pconf, payload, &cborLen);
         *size = cborLen;
@@ -476,7 +479,7 @@ OCStackResult CBORPayloadToPconf(const uint8_t *cborPayload, size_t size, OicSec
         char *name = NULL;
         size_t len = 0;
         CborType type = cbor_value_get_type(&pconfMap);
-        if (type == CborTextStringType)
+        if (type == CborTextStringType && cbor_value_is_text_string(&pconfMap))
         {
             cborFindResult = cbor_value_dup_text_string(&pconfMap, &name, &len, NULL);
             VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get value");
@@ -487,7 +490,7 @@ OCStackResult CBORPayloadToPconf(const uint8_t *cborPayload, size_t size, OicSec
         if (name)
         {
             //EDP -- Mandatory
-            if(0 == strcmp(OIC_JSON_EDP_NAME, name))
+            if(0 == strcmp(OIC_JSON_EDP_NAME, name) && cbor_value_is_boolean(&pconfMap))
             {
                 cborFindResult = cbor_value_get_boolean(&pconfMap, &pconf->edp);
                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get value");
@@ -505,16 +508,19 @@ OCStackResult CBORPayloadToPconf(const uint8_t *cborPayload, size_t size, OicSec
                 cborFindResult = cbor_value_enter_container(&pconfMap, &prm);
                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to eneter array");
 
-                while (cbor_value_is_valid(&prm))
+                while (cbor_value_is_valid(&prm) && cbor_value_is_integer(&prm))
                 {
-                    cborFindResult = cbor_value_get_int(&prm, (int *)&pconf->prm[i++]);
+                    int prm_val;
+
+                    cborFindResult = cbor_value_get_int(&prm, &prm_val);
                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get value");
+                    pconf->prm[i++] = (OicSecPrm_t)prm_val;
                     cborFindResult = cbor_value_advance(&prm);
                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance value");
                 }
             }
             //PIN -- Mandatory
-            if (0 == strcmp(OIC_JSON_PIN_NAME, name))
+            if (0 == strcmp(OIC_JSON_PIN_NAME, name) && cbor_value_is_byte_string(&pconfMap))
             {
                 uint8_t *pin = NULL;
                 cborFindResult = cbor_value_dup_byte_string(&pconfMap, &pin, &len, NULL);
@@ -546,7 +552,7 @@ OCStackResult CBORPayloadToPconf(const uint8_t *cborPayload, size_t size, OicSec
                         char* name = NULL;
                         size_t len = 0;
                         CborType type = cbor_value_get_type(&pdAclMap);
-                        if (type == CborTextStringType)
+                        if (type == CborTextStringType && cbor_value_is_text_string(&pdAclMap))
                         {
                             cborFindResult = cbor_value_dup_text_string(&pdAclMap, &name,
                                     &len, NULL);
@@ -557,7 +563,7 @@ OCStackResult CBORPayloadToPconf(const uint8_t *cborPayload, size_t size, OicSec
                         if (name)
                         {
                             // Resources -- Mandatory
-                            if (strcmp(name, OIC_JSON_RESOURCES_NAME) == 0)
+                            if (strcmp(name, OIC_JSON_RESOURCES_NAME) == 0 && cbor_value_is_array(&pdAclMap))
                             {
                                 int i = 0;
                                 CborValue resources = { .parser = NULL };
@@ -576,7 +582,7 @@ OCStackResult CBORPayloadToPconf(const uint8_t *cborPayload, size_t size, OicSec
                                     cborFindResult = cbor_value_enter_container(&resources, &rMap);
                                     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering Resource Map");
 
-                                    while(cbor_value_is_valid(&rMap))
+                                    while(cbor_value_is_valid(&rMap) && cbor_value_is_text_string(&rMap))
                                     {
                                         char *rMapName = NULL;
                                         size_t rMapNameLen = 0;
@@ -640,15 +646,16 @@ OCStackResult CBORPayloadToPconf(const uint8_t *cborPayload, size_t size, OicSec
                             }
 
                             // Permissions -- Mandatory
-                            if (strcmp(name, OIC_JSON_PERMISSION_NAME) == 0)
+                            if (strcmp(name, OIC_JSON_PERMISSION_NAME) == 0 && cbor_value_is_unsigned_integer(&pdAclMap))
                             {
-                                cborFindResult = cbor_value_get_uint64(&pdAclMap,
-                                        (uint64_t *) &pdacl->permission);
+                                uint64_t permission = 0;
+                                cborFindResult = cbor_value_get_uint64(&pdAclMap, &permission);
+                                pdacl->permission = (uint16_t)permission;
                                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get value");
                             }
 
                             // Period -- Not mandatory
-                            if (strcmp(name, OIC_JSON_PERIODS_NAME) == 0)
+                            if (strcmp(name, OIC_JSON_PERIODS_NAME) == 0 && cbor_value_is_array(&pdAclMap))
                             {
                                 int i = 0;
                                 CborValue period = { .parser = NULL };
@@ -660,7 +667,7 @@ OCStackResult CBORPayloadToPconf(const uint8_t *cborPayload, size_t size, OicSec
                                 pdacl->periods = (char **) OICCalloc(pdacl->prdRecrLen, sizeof(char*));
                                 VERIFY_NON_NULL(TAG, pdacl->periods, ERROR);
 
-                                while (cbor_value_is_text_string(&period))
+                                while (cbor_value_is_text_string(&period) && cbor_value_is_text_string(&period))
                                 {
                                     cborFindResult = cbor_value_dup_text_string(&period,
                                             &pdacl->periods[i++], &len, NULL);
@@ -672,7 +679,7 @@ OCStackResult CBORPayloadToPconf(const uint8_t *cborPayload, size_t size, OicSec
                             }
 
                             // Recurrence -- Not mandatory
-                            if (strcmp(name, OIC_JSON_RECURRENCES_NAME) == 0)
+                            if (strcmp(name, OIC_JSON_RECURRENCES_NAME) == 0 && cbor_value_is_array(&pdAclMap))
                             {
                                 int i = 0;
                                 CborValue recurrences = { .parser = NULL };
@@ -683,7 +690,7 @@ OCStackResult CBORPayloadToPconf(const uint8_t *cborPayload, size_t size, OicSec
                                 pdacl->recurrences = (char **) OICCalloc(pdacl->prdRecrLen, sizeof(char*));
                                 VERIFY_NON_NULL(TAG, pdacl->recurrences, ERROR);
 
-                                while (cbor_value_is_text_string(&recurrences))
+                                while (cbor_value_is_text_string(&recurrences) && cbor_value_is_text_string(&recurrences))
                                 {
                                     cborFindResult = cbor_value_dup_text_string(&recurrences,
                                             &pdacl->recurrences[i++], &len, NULL);
@@ -725,7 +732,7 @@ OCStackResult CBORPayloadToPconf(const uint8_t *cborPayload, size_t size, OicSec
             }
 
             //PDDev -- Mandatory
-            if (strcmp(name, OIC_JSON_PDDEV_LIST_NAME) == 0)
+            if (strcmp(name, OIC_JSON_PDDEV_LIST_NAME) == 0 && cbor_value_is_array(&pconfMap))
             {
                 int i = 0;
                 CborValue pddevs = { .parser = NULL };
@@ -736,7 +743,7 @@ OCStackResult CBORPayloadToPconf(const uint8_t *cborPayload, size_t size, OicSec
 
                 pconf->pddevs = (OicUuid_t *)OICMalloc(pconf->pddevLen * sizeof(OicUuid_t));
                 VERIFY_NON_NULL(TAG, pconf->pddevs, ERROR);
-                while (cbor_value_is_valid(&pddevs))
+                while (cbor_value_is_valid(&pddevs) && cbor_value_is_text_string(&pddevs))
                 {
                     char *pddev = NULL;
                     cborFindResult = cbor_value_dup_text_string(&pddevs, &pddev, &len, NULL);
@@ -750,7 +757,7 @@ OCStackResult CBORPayloadToPconf(const uint8_t *cborPayload, size_t size, OicSec
             }
 
             //Mandatory - Device Id
-            if (0 == strcmp(OIC_JSON_DEVICE_ID_NAME, name))
+            if (0 == strcmp(OIC_JSON_DEVICE_ID_NAME, name) && cbor_value_is_text_string(&pconfMap))
             {
                 char *deviceId = NULL;
                 cborFindResult = cbor_value_dup_text_string(&pconfMap, &deviceId, &len, NULL);
@@ -761,7 +768,7 @@ OCStackResult CBORPayloadToPconf(const uint8_t *cborPayload, size_t size, OicSec
             }
 
             // ROwner -- Mandatory
-            if (0 == strcmp(OIC_JSON_ROWNERID_NAME, name))
+            if (0 == strcmp(OIC_JSON_ROWNERID_NAME, name) && cbor_value_is_text_string(&pconfMap))
             {
                 char *rowner = NULL;
                 cborFindResult = cbor_value_dup_text_string(&pconfMap, &rowner, &len, NULL);
@@ -815,6 +822,7 @@ static OCEntityHandlerResult HandlePconfGetRequest (const OCEntityHandlerRequest
 {
     uint8_t* payload = NULL;
     size_t size = 0;
+    const OicSecDoxm_t *m_doxm = NULL;
     OCEntityHandlerResult ehRet = OC_EH_OK;
 
     OicSecPconf_t pconf;
@@ -822,7 +830,13 @@ static OCEntityHandlerResult HandlePconfGetRequest (const OCEntityHandlerRequest
 
     OIC_LOG (DEBUG, TAG, "Pconf EntityHandle processing GET request");
 
-    if (true == GetDoxmResourceData()->dpc)
+    m_doxm = GetDoxmResourceData();
+    if (NULL == m_doxm)
+    {
+      OIC_LOG (DEBUG, TAG, "Doxm resource Data is NULL");
+    }
+
+    if ((m_doxm) && (true == m_doxm->dpc))
     {
         //Making response elements for Get request
         if( (true == gPconf->edp) &&
@@ -889,7 +903,7 @@ static OCEntityHandlerResult HandlePconfPostRequest (const OCEntityHandlerReques
     OCStackResult res=OC_STACK_OK;
     OicSecPconf_t* newPconf = NULL;
 
-    if (true == GetDoxmResourceData()->dpc)
+    if (NULL != GetDoxmResourceData() && true == GetDoxmResourceData()->dpc)
     {
         // Convert CBOR PCONF data into binary. This will also validate the PCONF data received.
         uint8_t *payload = ((OCSecurityPayload *) ehRequest->payload)->securityData;
@@ -938,7 +952,7 @@ static OCEntityHandlerResult HandlePconfPostRequest (const OCEntityHandlerReques
         // Update storage
         if(OC_EH_ERROR != ehRet && true == UpdatePersistentStorage(gPconf))
         {
-            ehRet = OC_EH_RESOURCE_CREATED;
+            ehRet = OC_EH_CHANGED;
         }
 
         DeletePconfBinData(newPconf);
@@ -1010,7 +1024,7 @@ OCStackResult CreatePconfResource()
 
     ret = OCCreateResource(&gPconfHandle,
                            OIC_RSRC_TYPE_SEC_PCONF,
-                           OIC_MI_DEF,
+                           OC_RSRVD_INTERFACE_DEFAULT,
                            OIC_RSRC_PCONF_URI,
                            PconfEntityHandler,
                            NULL,