replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / tool / json2cbor.c
index 947edd3..b6fa1aa 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include "utlist.h"
+#if defined (__TIZENRT__)
+#include <apps/netutils/cJSON.h>
+#else
 #include "cJSON.h"
+#endif
 #include "base64.h"
 #include "cainterface.h"
 #include "ocstack.h"
 #include "doxmresource.h"
 #include "amaclresource.h"
 #include "credresource.h"
-#include "svcresource.h"
 #include "security_internals.h"
 
-#define TAG  "JSON2CBOR"
+#define TAG  "OIC_JSON2CBOR"
 #define MAX_RANGE   ((size_t)-1)
 //SVR database buffer block size
 static const size_t DB_FILE_SIZE_BLOCK = 1023;
@@ -49,7 +52,6 @@ static const size_t DB_FILE_SIZE_BLOCK = 1023;
 static OicSecPstat_t* JSONToPstatBin(const char * jsonStr);
 static OicSecDoxm_t* JSONToDoxmBin(const char * jsonStr);
 static OicSecAcl_t *JSONToAclBin(const char * jsonStr);
-static OicSecSvc_t* JSONToSvcBin(const char * jsonStr);
 static OicSecAmacl_t* JSONToAmaclBin(const char * jsonStr);
 static OicSecCred_t* JSONToCredBin(const char * jsonStr);
 
@@ -85,7 +87,6 @@ static void ConvertJsonToCBOR(const char *jsonFileName, const char *cborFileName
     uint8_t *pstatCbor = NULL;
     uint8_t *doxmCbor = NULL;
     uint8_t *amaclCbor = NULL;
-    uint8_t *svcCbor = NULL;
     uint8_t *credCbor = NULL;
     cJSON *jsonRoot = NULL;
     OCStackResult ret = OC_STACK_ERROR;
@@ -183,22 +184,6 @@ static void ConvertJsonToCBOR(const char *jsonFileName, const char *cborFileName
         printf("AMACL Cbor Size: %zd\n", amaclCborSize);
         DeleteAmaclList(amacl);
     }
-    value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME);
-    size_t svcCborSize = 0;
-    if (NULL != value)
-    {
-        OicSecSvc_t *svc = JSONToSvcBin(jsonStr);
-        VERIFY_NON_NULL(TAG, svc, FATAL);
-        ret = SVCToCBORPayload(svc, &svcCbor, &svcCborSize);
-        if(OC_STACK_OK != ret)
-        {
-            OIC_LOG (ERROR, TAG, "Failed converting Svc to Cbor Payload");
-            DeleteSVCList(svc);
-            goto exit;
-        }
-        printf("SVC Cbor Size: %zd\n", svcCborSize);
-        DeleteSVCList(svc);
-    }
     value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
     //printf("CRED json : \n%s\n", cJSON_PrintUnformatted(value));
     size_t credCborSize = 0;
@@ -219,7 +204,7 @@ static void ConvertJsonToCBOR(const char *jsonFileName, const char *cborFileName
     }
 
     CborEncoder encoder;
-    size_t cborSize = aclCborSize + pstatCborSize + doxmCborSize + svcCborSize + credCborSize + amaclCborSize;
+    size_t cborSize = aclCborSize + pstatCborSize + doxmCborSize + credCborSize + amaclCborSize;
 
     printf("Total Cbor Size : %zd\n", cborSize);
     cborSize += 255; // buffer margin for adding map and byte string
@@ -258,13 +243,6 @@ static void ConvertJsonToCBOR(const char *jsonFileName, const char *cborFileName
         cborEncoderResult = cbor_encode_byte_string(&map, amaclCbor, amaclCborSize);
         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Value.");
     }
-    if (svcCborSize > 0)
-    {
-        cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_SVC_NAME, strlen(OIC_JSON_SVC_NAME));
-        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Name.");
-        cborEncoderResult = cbor_encode_byte_string(&map, svcCbor, svcCborSize);
-        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Value.");
-    }
     if (credCborSize > 0)
     {
         cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME));
@@ -276,7 +254,7 @@ static void ConvertJsonToCBOR(const char *jsonFileName, const char *cborFileName
     cborEncoderResult = cbor_encoder_close_container(&encoder, &map);
     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Container.");
 
-    size_t s = encoder.ptr - outPayload;
+    size_t s = cbor_encoder_get_buffer_size(&encoder, outPayload);
     OIC_LOG_V(DEBUG, TAG, "Payload size %zu", s);
 
     fp1 = fopen(cborFileName, "w");
@@ -301,7 +279,6 @@ exit:
     OICFree(doxmCbor);
     OICFree(pstatCbor);
     OICFree(amaclCbor);
-    OICFree(svcCbor);
     OICFree(credCbor);
     OICFree(jsonStr);
     return ;
@@ -410,6 +387,7 @@ OicSecAcl_t* JSONToAclBin(const char * jsonStr)
                     for(size_t i = 0; i < rsrc->typeLen; i++)
                     {
                         cJSON *jsonRsrcType = cJSON_GetArrayItem(jsonRsrcObj, i);
+                        VERIFY_NON_NULL(TAG, jsonRsrcType, ERROR);
                         rsrc->types[i] = OICStrdup(jsonRsrcType->valuestring);
                         VERIFY_NON_NULL(TAG, (rsrc->types[i]), ERROR);
                     }
@@ -426,6 +404,7 @@ OicSecAcl_t* JSONToAclBin(const char * jsonStr)
                     for(size_t i = 0; i < rsrc->interfaceLen; i++)
                     {
                         cJSON *jsonInterface = cJSON_GetArrayItem(jsonRsrcObj, i);
+                        VERIFY_NON_NULL(TAG, jsonInterface, ERROR);
                         rsrc->interfaces[i] = OICStrdup(jsonInterface->valuestring);
                         VERIFY_NON_NULL(TAG, (rsrc->interfaces[i]), ERROR);
                     }
@@ -445,7 +424,7 @@ OicSecAcl_t* JSONToAclBin(const char * jsonStr)
             if(jsonValidityObj)
             {
                 VERIFY_SUCCESS(TAG, cJSON_Array == jsonValidityObj->type, ERROR);
-                size_t validityLen = cJSON_GetArraySize(jsonValidityObj);
+                size_t validityLen = (size_t) cJSON_GetArraySize(jsonValidityObj);
                 VERIFY_SUCCESS(TAG, (0 < validityLen), ERROR);
 
                 cJSON *jsonValidity = NULL;
@@ -476,7 +455,7 @@ OicSecAcl_t* JSONToAclBin(const char * jsonStr)
                     if(jsonRecurObj)
                     {
                         VERIFY_SUCCESS(TAG, (cJSON_Array == jsonRecurObj->type), ERROR);
-                        validity->recurrenceLen = cJSON_GetArraySize(jsonRecurObj);
+                        validity->recurrenceLen = (size_t) cJSON_GetArraySize(jsonRecurObj);
                         VERIFY_SUCCESS(TAG, (0 < validity->recurrenceLen), ERROR);
 
                         validity->recurrences = (char**)OICCalloc(validity->recurrenceLen, sizeof(char*));
@@ -606,13 +585,17 @@ OicSecDoxm_t* JSONToDoxmBin(const char * jsonStr)
         doxm->owned = jsonObj->valueint;
     }
 
-    //DPC -- Mandatory
-    jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DPC_NAME);
+#ifdef MULTIPLE_OWNER
+    //mom -- Not Mandatory
+    jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_MOM_NAME);
     if (jsonObj)
     {
-        VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type), ERROR);
-        doxm->dpc = jsonObj->valueint;
+        VERIFY_SUCCESS(TAG, (cJSON_Number == jsonObj->type), ERROR);
+        doxm->mom = (OicSecMom_t*)OICCalloc(1, sizeof(OicSecMom_t));
+        VERIFY_NON_NULL(TAG, doxm->mom, ERROR);
+        doxm->mom->mode = (OicSecMomType_t)jsonObj->valueint;
     }
+#endif //MULTIPLE_OWNER
 
     //DeviceId -- Mandatory
     jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVICE_ID_NAME);
@@ -739,6 +722,29 @@ exit:
     return pstat;
 }
 
+static OicEncodingType_t GetEncodingTypeFromStr(const char* encodingType)
+{
+    if (strcmp(OIC_SEC_ENCODING_RAW, encodingType) == 0)
+    {
+        return OIC_ENCODING_RAW;
+    }
+    if (strcmp(OIC_SEC_ENCODING_BASE64, encodingType) == 0)
+    {
+        return OIC_ENCODING_BASE64;
+    }
+    if (strcmp(OIC_SEC_ENCODING_PEM, encodingType) == 0)
+    {
+        return OIC_ENCODING_PEM;
+    }
+    if (strcmp(OIC_SEC_ENCODING_DER, encodingType) == 0)
+    {
+        return OIC_ENCODING_DER;
+    }
+    OIC_LOG(WARNING, TAG, "Unknow encoding type dectected!");
+    OIC_LOG(WARNING, TAG, "json2cbor will use \"oic.sec.encoding.raw\" as default encoding type.");
+    return OIC_ENCODING_RAW;
+}
+
 OicSecCred_t * JSONToCredBin(const char * jsonStr)
 {
     if (NULL == jsonStr)
@@ -805,8 +811,15 @@ OicSecCred_t * JSONToCredBin(const char * jsonStr)
             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_SUBJECTID_NAME);
             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
             VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
-            ret = ConvertStrToUuid(jsonObj->valuestring, &cred->subject);
-            VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
+            if(strcmp(jsonObj->valuestring, WILDCARD_RESOURCE_URI) == 0)
+            {
+                cred->subject.id[0] = '*';
+            }
+            else
+            {
+                ret = ConvertStrToUuid(jsonObj->valuestring, &cred->subject);
+                VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
+            }
 
             //CredType -- Mandatory
             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDTYPE_NAME);
@@ -820,7 +833,7 @@ OicSecCred_t * JSONToCredBin(const char * jsonStr)
             {
                 cJSON *jsonPriv = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
                 VERIFY_NON_NULL(TAG, jsonPriv, ERROR);
-                jsonObjLen = strlen(jsonPriv->valuestring);
+                jsonObjLen = strlen(jsonPriv->valuestring) + 1;
                 cred->privateData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
                 VERIFY_NON_NULL(TAG, (cred->privateData.data), ERROR);
                 memcpy(cred->privateData.data, jsonPriv->valuestring, jsonObjLen);
@@ -828,23 +841,9 @@ OicSecCred_t * JSONToCredBin(const char * jsonStr)
 
                 cJSON *jsonEncoding = cJSON_GetObjectItem(jsonObj, OIC_JSON_ENCODING_NAME);
                 VERIFY_NON_NULL(TAG, jsonEncoding, ERROR);
-
-                if(strcmp(OIC_SEC_ENCODING_RAW, jsonEncoding->valuestring) == 0)
-                {
-                    cred->privateData.encoding = OIC_ENCODING_RAW;
-                }
-                else if(strcmp(OIC_SEC_ENCODING_BASE64, jsonEncoding->valuestring) == 0)
-                {
-                    cred->privateData.encoding = OIC_ENCODING_BASE64;
-                }
-                else
-                {
-                    printf("Unknow encoding type dectected!\n");
-                    printf("json2cbor will use \"oic.sec.encoding.raw\" as default encoding type.\n");
-                    cred->privateData.encoding = OIC_ENCODING_RAW;
-                }
+                cred->privateData.encoding = GetEncodingTypeFromStr(jsonEncoding->valuestring);
             }
-#ifdef __WITH_X509__
+#if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
             //PublicData is mandatory only for SIGNED_ASYMMETRIC_KEY credentials type.
             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PUBLICDATA_NAME);
 
@@ -852,13 +851,45 @@ OicSecCred_t * JSONToCredBin(const char * jsonStr)
             {
                 cJSON *jsonPub = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
                 VERIFY_NON_NULL(TAG, jsonPub, ERROR);
-                jsonObjLen = strlen(jsonPub->valuestring);
+                jsonObjLen = strlen(jsonPub->valuestring) + 1;
                 cred->publicData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
                 VERIFY_NON_NULL(TAG, (cred->publicData.data), ERROR);
                 memcpy(cred->publicData.data, jsonPub->valuestring, jsonObjLen);
                 cred->publicData.len = jsonObjLen;
             }
-#endif //  __WITH_X509__
+
+            //Optional Data
+            jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_OPTDATA_NAME);
+            if (NULL != jsonObj)
+            {
+                cJSON *jsonOpt = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
+                VERIFY_NON_NULL(TAG, jsonOpt, ERROR);
+                jsonObjLen = strlen(jsonOpt->valuestring) + 1;
+                cred->optionalData.data =  (uint8_t *)OICCalloc(1, jsonObjLen);
+                VERIFY_NON_NULL(TAG, (cred->optionalData.data), ERROR);
+                memcpy(cred->optionalData.data, jsonOpt->valuestring, jsonObjLen);
+                cred->optionalData.len = jsonObjLen;
+
+                cJSON *jsonEncoding = cJSON_GetObjectItem(jsonObj, OIC_JSON_ENCODING_NAME);
+                VERIFY_NON_NULL(TAG, jsonEncoding, ERROR);
+                cred->optionalData.encoding = GetEncodingTypeFromStr(jsonEncoding->valuestring);
+
+                cJSON *jsonRevstat = cJSON_GetObjectItem(jsonObj, OIC_JSON_REVOCATION_STATUS_NAME);
+                VERIFY_NON_NULL(TAG, jsonRevstat, ERROR);
+                cred->optionalData.revstat = jsonObj->valueint;
+            }
+
+            //CredUsage
+            jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDUSAGE_NAME);
+            if (NULL != jsonObj)
+            {
+                jsonObjLen = strlen(jsonObj->valuestring);
+                cred->credUsage = OICStrdup(jsonObj->valuestring);
+                VERIFY_NON_NULL(TAG, (cred->credUsage), ERROR);
+            }
+
+#endif // defined(__WITH_DTLS__) || defined(__WITH_TLS__)
+
             //Period -- Not Mandatory
             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PERIOD_NAME);
             if(jsonObj && cJSON_String == jsonObj->type)
@@ -881,7 +912,7 @@ OicSecCred_t * JSONToCredBin(const char * jsonStr)
     ret = OC_STACK_OK;
 
 exit:
-
+    cJSON_Delete(jsonRoot);
     if (OC_STACK_OK != ret)
     {
         DeleteCredList(headCred);
@@ -890,104 +921,6 @@ exit:
     return headCred;
 }
 
-static OicSecSvc_t* JSONToSvcBin(const char * jsonStr)
-{
-    OCStackResult ret = OC_STACK_ERROR;
-    OicSecSvc_t * headSvc = NULL;
-    OicSecSvc_t * prevSvc = NULL;
-    cJSON *jsonRoot = NULL;
-    cJSON *jsonSvcArray = NULL;
-
-    VERIFY_NON_NULL(TAG, jsonStr, ERROR);
-
-    jsonRoot = cJSON_Parse(jsonStr);
-    VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
-
-    jsonSvcArray = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME);
-    VERIFY_NON_NULL(TAG, jsonSvcArray, INFO);
-
-    if (cJSON_Array == jsonSvcArray->type)
-    {
-        int numSvc = cJSON_GetArraySize(jsonSvcArray);
-        int idx = 0;
-
-        VERIFY_SUCCESS(TAG, numSvc > 0, INFO);
-        do
-        {
-            cJSON *jsonSvc = cJSON_GetArrayItem(jsonSvcArray, idx);
-            VERIFY_NON_NULL(TAG, jsonSvc, ERROR);
-
-            OicSecSvc_t *svc = (OicSecSvc_t*)OICCalloc(1, sizeof(OicSecSvc_t));
-            VERIFY_NON_NULL(TAG, svc, ERROR);
-
-            headSvc = (headSvc) ? headSvc : svc;
-            if (prevSvc)
-            {
-                prevSvc->next = svc;
-            }
-
-            cJSON *jsonObj = NULL;
-            unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
-            uint32_t outLen = 0;
-            B64Result b64Ret = B64_OK;
-
-            // Service Device Identity
-            jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_DEVICE_ID);
-            VERIFY_NON_NULL(TAG, jsonObj, ERROR);
-            VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
-            outLen = 0;
-            b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
-                       sizeof(base64Buff), &outLen);
-            VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->svcdid.id)), ERROR);
-            memcpy(svc->svcdid.id, base64Buff, outLen);
-
-            // Service Type
-            jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_TYPE);
-            VERIFY_NON_NULL(TAG, jsonObj, ERROR);
-            VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
-            svc->svct = (OicSecSvcType_t)jsonObj->valueint;
-
-            // Resource Owners
-            jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_OWNERS_NAME);
-            VERIFY_NON_NULL(TAG, jsonObj, ERROR);
-            VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
-
-            svc->ownersLen = (size_t)cJSON_GetArraySize(jsonObj);
-            VERIFY_SUCCESS(TAG, svc->ownersLen > 0, ERROR);
-            svc->owners = (OicUuid_t*)OICCalloc(svc->ownersLen, sizeof(OicUuid_t));
-            VERIFY_NON_NULL(TAG, (svc->owners), ERROR);
-
-            size_t idxx = 0;
-            do
-            {
-                cJSON *jsonOwnr = cJSON_GetArrayItem(jsonObj, idxx);
-                VERIFY_NON_NULL(TAG, jsonOwnr, ERROR);
-                VERIFY_SUCCESS(TAG, cJSON_String == jsonOwnr->type, ERROR);
-                outLen = 0;
-                b64Ret = b64Decode(jsonOwnr->valuestring, strlen(jsonOwnr->valuestring), base64Buff,
-                           sizeof(base64Buff), &outLen);
-
-                VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->owners[idxx].id)),
-                                   ERROR);
-                memcpy(svc->owners[idxx].id, base64Buff, outLen);
-            } while ( ++idxx < svc->ownersLen);
-
-            prevSvc = svc;
-        } while( ++idx < numSvc);
-    }
-
-    ret = OC_STACK_OK;
-
-exit:
-    cJSON_Delete(jsonRoot);
-    if (OC_STACK_OK != ret)
-    {
-        DeleteSVCList(headSvc);
-        headSvc = NULL;
-    }
-    return headSvc;
-}
-
 static OicSecAmacl_t* JSONToAmaclBin(const char * jsonStr)
 {
     OCStackResult ret = OC_STACK_ERROR;