replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / src / crlresource.c
index 6b1aaff..a59df3b 100644 (file)
 #include "srmutility.h"
 #include "doxmresource.h"
 #include "ocpayload.h"
+#include "ocpayloadcbor.h"
 #include "oic_malloc.h"
 #include "oic_string.h"
 #include "crlresource.h"
-#include "crl.h"
 #include "ocpayloadcbor.h"
 #include "base64.h"
 #include <time.h>
 
-#define TAG  "SRM-CRL"
+#define TAG  "OIC_SRM_CRL"
 
 #define SEPARATOR                   ":"
 #define SEPARATOR_LEN               (1)
@@ -75,16 +75,22 @@ void DeleteCrl(OicSecCrl_t *crl)
     }
 }
 
-static void printCrl(const OicSecCrl_t *crl)
+void printCrl(const OicSecCrl_t *crl)
 {
-    OIC_LOG(DEBUG, TAG, "Crl object contain:");
-    OIC_LOG_V(DEBUG, TAG, "id = %d", crl->CrlId);
-    OIC_LOG_V(DEBUG, TAG, "this update = %s", crl->ThisUpdate.data);
-
-    OIC_LOG(DEBUG, TAG, "crl:");
-    OIC_LOG_V(DEBUG, TAG, "encoding = %d", crl->CrlData.encoding);
-    OIC_LOG_V(DEBUG, TAG, "data (length = %zu):", crl->CrlData.len);
-    OIC_LOG_BUFFER(DEBUG, TAG, crl->CrlData.data, crl->CrlData.len);
+    if (NULL == crl)
+    {
+        OIC_LOG(INFO, TAG, "Received NULL CRL");
+        return;
+    }
+
+    OIC_LOG(INFO, TAG, "Crl object contain:");
+    OIC_LOG_V(INFO, TAG, "id = %d", crl->CrlId);
+    OIC_LOG_V(INFO, TAG, "this update = %s", crl->ThisUpdate.data);
+
+    OIC_LOG(INFO, TAG, "crl:");
+    OIC_LOG_V(INFO, TAG, "encoding = %d", crl->CrlData.encoding);
+    OIC_LOG_V(INFO, TAG, "data (length = %zu):", crl->CrlData.len);
+    OIC_LOG_BUFFER(INFO, TAG, crl->CrlData.data, crl->CrlData.len);
 }
 
 static bool copyByteArray(const uint8_t *in, size_t in_len, uint8_t **out, size_t *out_len)
@@ -351,21 +357,11 @@ OCStackResult CrlToCBORPayload(const OicSecCrl_t *crl, uint8_t **payload, size_t
     cborEncoderResult = cbor_encoder_close_container(&encoder, &crlMap);
     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add close Crl map");
 
-    *size = encoder.ptr - outPayload;
+    *size = cbor_encoder_get_buffer_size(&encoder, outPayload);
     *payload = outPayload;
     ret = OC_STACK_OK;
 
 exit:
-    if ((CborErrorOutOfMemory == cborEncoderResult) && (cborLen < CBOR_MAX_SIZE))
-    {
-        // reallocate and try again!
-        OICFree(outPayload);
-        // Since the allocated initial memory failed, double the memory.
-        cborLen += encoder.ptr - encoder.end;
-        cborEncoderResult = CborNoError;
-        ret = CrlToCBORPayload(crl, payload, &cborLen, lastUpdate);
-    }
-
     if ((CborNoError != cborEncoderResult) || (OC_STACK_OK != ret))
     {
         OICFree(outPayload);
@@ -373,6 +369,13 @@ exit:
         *payload = NULL;
         *size = 0;
         ret = OC_STACK_ERROR;
+        if ((CborErrorOutOfMemory == cborEncoderResult) && (cborLen < CBOR_MAX_SIZE))
+        {
+            // Since the allocated initial memory failed, double the memory.
+            cborLen += cbor_encoder_get_buffer_size(&encoder, encoder.end);
+            cborEncoderResult = CborNoError;
+            ret = CrlToCBORPayload(crl, payload, &cborLen, lastUpdate);
+        }
     }
 
     return ret;
@@ -452,11 +455,16 @@ static void getCurrentUTCTime(char *out, size_t len)
 {
     //TODO: how to implement it in cross-platform way?
     time_t rawtime;
-    struct tm * timeinfo;
+    struct tm * timeinfo = NULL;
 
     time ( &rawtime );
     timeinfo = localtime ( &rawtime );
 
+    if (NULL == timeinfo)
+    {
+        return;
+    }
+
     snprintf(out, len, "%04d%02d%02d%02d%02d%02d",
             timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday,
             timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
@@ -598,6 +606,7 @@ static OicSecCrl_t *GetCrlDefault()
     }
 
     defaultCrl->CrlId = CRL_DEFAULT_CRL_ID;
+    defaultCrl->CrlData.encoding = OIC_ENCODING_DER;
 
     bool result1 = copyByteArray((const uint8_t *)CRL_DEFAULT_CRL_DATA,
                                  strlen(CRL_DEFAULT_CRL_DATA),
@@ -757,7 +766,7 @@ uint8_t *GetCrl()
     return NULL;
 }
 
-void GetDerCrl(ByteArray* out)
+void GetDerCrl(ByteArray_t* out)
 {
     if(NULL == out)
     {
@@ -795,25 +804,20 @@ void GetDerCrl(ByteArray* out)
         {
             OIC_LOG (ERROR, TAG, "Base64 decoding failed.");
         }
+        OICFree(out);
     }
 
     out->len = 0;
 
-#ifdef __WITH_X509__
-    char *str = "Not enough space in out buffer to store crl!";
-    if (out->data && crl->data && crl->len <= out->len)
-#else
-    char *str = "Can't allocate memory for out->data";
-    out->data = OICMalloc(crl->len);
+    out->data = OICRealloc(out->data, crl->len);
     if (out->data)
-#endif
     {
         memcpy(out->data, crl->data, crl->len);
         out->len = crl->len;
     }
     else
     {
-        OIC_LOG_V(ERROR, TAG, "%s", str);
+        OIC_LOG(ERROR, TAG, "Can't allocate memory for out->data");
     }
     DeleteCrl(crlRes);
 }