replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / src / verresource.c
index 4f0b2d8..3f0ea0f 100644 (file)
  *
  * *****************************************************************/
 
+#include "iotivity_config.h"
 #include <stdlib.h>
 #include <string.h>
 
-#if HAVE_STRINGS_H
+#ifdef HAVE_STRINGS_H
 #include <strings.h>
 #endif
 
 #include "ocstack.h"
 #include "oic_malloc.h"
 #include "oic_string.h"
-#include "logger.h"
 #include "payload_logging.h"
 #include "ocpayload.h"
+#include "ocpayloadcbor.h"
 #include "cainterface.h"
 #include "ocserverrequest.h"
 #include "resourcemanager.h"
@@ -41,7 +42,7 @@
 #include "securevirtualresourcetypes.h"
 #include "srmutility.h"
 
-#define TAG  "SEC-VER"
+#define TAG  "OIC_SEC_VER"
 
 /** 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. */
@@ -89,8 +90,8 @@ OCStackResult VerToCBORPayload(const OicSecVer_t *ver, uint8_t **payload, size_t
 
     OCStackResult ret = OC_STACK_ERROR;
 
-    CborEncoder encoder = { {.ptr = NULL }, .end = 0 };
-    CborEncoder verMap = { {.ptr = NULL }, .end = 0 };
+    CborEncoder encoder;
+    CborEncoder verMap;
 
     int64_t cborEncoderResult = CborNoError;
     uint8_t mapSize = VER_MAP_SIZE;
@@ -128,7 +129,7 @@ OCStackResult VerToCBORPayload(const OicSecVer_t *ver, uint8_t **payload, size_t
 
     if (CborNoError == cborEncoderResult)
     {
-        *size = encoder.ptr - outPayload;
+        *size = cbor_encoder_get_buffer_size(&encoder, outPayload);
         *payload = outPayload;
         ret = OC_STACK_OK;
     }
@@ -138,8 +139,9 @@ exit:
         OIC_LOG(DEBUG, TAG, "Memory getting reallocated.");
         // reallocate and try again!
         OICFree(outPayload);
+        outPayload = NULL;
         // Since the allocated initial memory failed, double the memory.
-        cborLen += encoder.ptr - encoder.end;
+        cborLen += cbor_encoder_get_buffer_size(&encoder, encoder.end);
         OIC_LOG_V(DEBUG, TAG, "Ver reallocation size : %zd.", cborLen);
         cborEncoderResult = CborNoError;
         ret = VerToCBORPayload(ver, payload, &cborLen);
@@ -161,7 +163,7 @@ exit:
 OCStackResult CBORPayloadToVer(const uint8_t *cborPayload, size_t size,
                                 OicSecVer_t **secVer)
 {
-    if (NULL == cborPayload || NULL == secVer || NULL != *secVer)
+    if (NULL == cborPayload || NULL == secVer || NULL != *secVer || 0 == size)
     {
         return OC_STACK_INVALID_PARAM;
     }
@@ -172,10 +174,9 @@ OCStackResult CBORPayloadToVer(const uint8_t *cborPayload, size_t size,
 
     CborParser parser = { .end = NULL};
     CborError cborFindResult = CborNoError;
-    int cborLen = (size == 0) ? CBOR_SIZE : size;
     size_t len = 0;
     CborValue verCbor = { .parser = NULL };
-    cbor_parser_init(cborPayload, cborLen, 0, &parser, &verCbor);
+    cbor_parser_init(cborPayload, size, 0, &parser, &verCbor);
     CborValue verMap = { .parser = NULL };
     OicSecVer_t *ver = (OicSecVer_t *)OICCalloc(1, sizeof(OicSecVer_t));
     VERIFY_NON_NULL(TAG, ver, ERROR);
@@ -235,9 +236,10 @@ static OCEntityHandlerResult HandleVerGetRequest (const OCEntityHandlerRequest *
     }
 
     // Send response payload to request originator
-    if (OC_STACK_OK != SendSRMCBORResponse(ehRequest, ehRet, payload, size))
+    if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, payload, size))
     {
-        OIC_LOG(ERROR, TAG, "SendSRMCBORResponse failed in HandleVerGetRequest");
+        ehRet = OC_EH_ERROR;
+        OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandleVerGetRequest");
     }
 
     OICFree(payload);
@@ -269,7 +271,7 @@ OCEntityHandlerResult VerEntityHandler(OCEntityHandlerFlag flag,
 
             default:
                 ehRet = OC_EH_ERROR;
-                SendSRMCBORResponse(ehRequest, ehRet, NULL, 0);
+                SendSRMResponse(ehRequest, ehRet, NULL, 0);
                 break;
         }
     }
@@ -281,7 +283,7 @@ OCStackResult CreateVerResource()
 {
     OCStackResult ret = OCCreateResource(&gVerHandle,
                                          OIC_RSRC_TYPE_SEC_VER,
-                                         OIC_MI_DEF,
+                                         OC_RSRVD_INTERFACE_DEFAULT,
                                          OIC_RSRC_VER_URI,
                                          VerEntityHandler,
                                          NULL,
@@ -315,7 +317,7 @@ OCStackResult InitVerResource()
 {
     OCStackResult ret = OC_STACK_ERROR;
 
-    OICStrcpy(gVer.secv, strlen(SECURITY_VERSION)+1, SECURITY_VERSION);
+    OICStrcpy(gVer.secv, OIC_SEC_MAX_VER_LEN, SECURITY_VERSION);
 
     //Read device id from doxm
     OicUuid_t deviceID = {.id={0}};