[CONPRO-311] Fix due to TinyCBOR v0.4 Release
[platform/upstream/iotivity.git] / resource / csdk / security / tool / json2cbor.c
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include "utlist.h"
24 #include "cJSON.h"
25 #include "base64.h"
26 #include "cainterface.h"
27 #include "ocstack.h"
28 #include "oic_malloc.h"
29 #include "oic_string.h"
30 #include "ocpayload.h"
31 #include "ocpayloadcbor.h"
32 #include "payload_logging.h"
33 #include "secureresourcemanager.h"
34 #include "srmresourcestrings.h"
35 #include "srmutility.h"
36 #include "aclresource.h"
37 #include "pstatresource.h"
38 #include "doxmresource.h"
39 #include "amaclresource.h"
40 #include "credresource.h"
41 #include "svcresource.h"
42 #include "security_internals.h"
43
44 #define TAG  "OIC_JSON2CBOR"
45 #define MAX_RANGE   ((size_t)-1)
46 //SVR database buffer block size
47
48 #define DB_FILE_SIZE_BLOCK 1023
49
50 static OicSecPstat_t* JSONToPstatBin(const char * jsonStr);
51 static OicSecDoxm_t* JSONToDoxmBin(const char * jsonStr);
52 static OicSecAcl_t *JSONToAclBin(const char * jsonStr);
53 static OicSecSvc_t* JSONToSvcBin(const char * jsonStr);
54 static OicSecAmacl_t* JSONToAmaclBin(const char * jsonStr);
55 static OicSecCred_t* JSONToCredBin(const char * jsonStr);
56
57 static size_t GetJSONFileSize(const char *jsonFileName)
58 {
59     size_t size = 0;
60     size_t bytesRead  = 0;
61     char buffer[DB_FILE_SIZE_BLOCK];
62     FILE* fp = fopen(jsonFileName, "r");
63     if (fp)
64     {
65         do
66         {
67             bytesRead = fread(buffer, 1, DB_FILE_SIZE_BLOCK, fp);
68             if (bytesRead >=(MAX_RANGE - size))
69             {
70                 fclose(fp);
71                 return 0;
72             }
73             size += bytesRead;
74         } while (bytesRead > 0);
75         fclose(fp);
76     }
77     return size;
78 }
79
80 static void ConvertJsonToCBOR(const char *jsonFileName, const char *cborFileName)
81 {
82     char *jsonStr = NULL;
83     FILE *fp = NULL;
84     FILE *fp1 = NULL;
85     uint8_t *aclCbor = NULL;
86     uint8_t *pstatCbor = NULL;
87     uint8_t *doxmCbor = NULL;
88     uint8_t *amaclCbor = NULL;
89     uint8_t *svcCbor = NULL;
90     uint8_t *credCbor = NULL;
91     cJSON *jsonRoot = NULL;
92     OCStackResult ret = OC_STACK_ERROR;
93     size_t size = GetJSONFileSize(jsonFileName);
94     if (0 == size)
95     {
96         OIC_LOG (ERROR, TAG, "Failed converting to JSON");
97         return;
98     }
99
100     jsonStr = (char *)OICMalloc(size + 1);
101     VERIFY_NON_NULL(TAG, jsonStr, FATAL);
102
103     fp = fopen(jsonFileName, "r");
104     if (fp)
105     {
106         size_t bytesRead = fread(jsonStr, 1, size, fp);
107         jsonStr[bytesRead] = '\0';
108
109         OIC_LOG_V(DEBUG, TAG, "Read %zu bytes", bytesRead);
110         fclose(fp);
111         fp = NULL;
112     }
113     else
114     {
115         OIC_LOG (ERROR, TAG, "Unable to open JSON file!!");
116         goto exit;
117     }
118
119     jsonRoot = cJSON_Parse(jsonStr);
120
121     cJSON *value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_ACL_NAME);
122     //printf("ACL json : \n%s\n", cJSON_PrintUnformatted(value));
123     size_t aclCborSize = 0;
124     if (NULL != value)
125     {
126         OicSecAcl_t *acl = JSONToAclBin(jsonStr);
127         VERIFY_NON_NULL(TAG, acl, FATAL);
128         ret = AclToCBORPayload(acl, &aclCbor, &aclCborSize);
129         if(OC_STACK_OK != ret)
130         {
131             OIC_LOG (ERROR, TAG, "Failed converting Acl to Cbor Payload");
132             DeleteACLList(acl);
133             goto exit;
134         }
135         printf("ACL Cbor Size: %zd\n", aclCborSize);
136         DeleteACLList(acl);
137     }
138
139     value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
140     size_t pstatCborSize = 0;
141     if (NULL != value)
142     {
143         OicSecPstat_t *pstat = JSONToPstatBin(jsonStr);
144         VERIFY_NON_NULL(TAG, pstat, FATAL);
145         ret = PstatToCBORPayload(pstat, &pstatCbor, &pstatCborSize, false);
146         if(OC_STACK_OK != ret)
147         {
148             OIC_LOG (ERROR, TAG, "Failed converting Pstat to Cbor Payload");
149             DeletePstatBinData(pstat);
150             goto exit;
151         }
152         printf("PSTAT Cbor Size: %zd\n", pstatCborSize);
153         DeletePstatBinData(pstat);
154     }
155     value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME);
156     size_t doxmCborSize = 0;
157     if (NULL != value)
158     {
159         OicSecDoxm_t *doxm = JSONToDoxmBin(jsonStr);
160         VERIFY_NON_NULL(TAG, doxm, FATAL);
161         ret = DoxmToCBORPayload(doxm, &doxmCbor, &doxmCborSize, false);
162         if(OC_STACK_OK != ret)
163         {
164             OIC_LOG (ERROR, TAG, "Failed converting Doxm to Cbor Payload");
165             DeleteDoxmBinData(doxm);
166             goto exit;
167         }
168         printf("DOXM Cbor Size: %zd\n", doxmCborSize);
169         DeleteDoxmBinData(doxm);
170     }
171     value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
172     size_t amaclCborSize = 0;
173     if (NULL != value)
174     {
175         OicSecAmacl_t *amacl = JSONToAmaclBin(jsonStr);
176         VERIFY_NON_NULL(TAG, amacl, FATAL);
177         ret = AmaclToCBORPayload(amacl, &amaclCbor, &amaclCborSize);
178         if(OC_STACK_OK != ret)
179         {
180             OIC_LOG (ERROR, TAG, "Failed converting Amacl to Cbor Payload");
181             DeleteAmaclList(amacl);
182             goto exit;
183         }
184         printf("AMACL Cbor Size: %zd\n", amaclCborSize);
185         DeleteAmaclList(amacl);
186     }
187     value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME);
188     size_t svcCborSize = 0;
189     if (NULL != value)
190     {
191         OicSecSvc_t *svc = JSONToSvcBin(jsonStr);
192         VERIFY_NON_NULL(TAG, svc, FATAL);
193         ret = SVCToCBORPayload(svc, &svcCbor, &svcCborSize);
194         if(OC_STACK_OK != ret)
195         {
196             OIC_LOG (ERROR, TAG, "Failed converting Svc to Cbor Payload");
197             DeleteSVCList(svc);
198             goto exit;
199         }
200         printf("SVC Cbor Size: %zd\n", svcCborSize);
201         DeleteSVCList(svc);
202     }
203     value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
204     //printf("CRED json : \n%s\n", cJSON_PrintUnformatted(value));
205     size_t credCborSize = 0;
206     int secureFlag = 0;
207     if (NULL != value)
208     {
209         OicSecCred_t *cred = JSONToCredBin(jsonStr);
210         VERIFY_NON_NULL(TAG, cred, FATAL);
211         ret = CredToCBORPayload(cred, &credCbor, &credCborSize, secureFlag);
212         if(OC_STACK_OK != ret)
213         {
214             OIC_LOG (ERROR, TAG, "Failed converting Cred to Cbor Payload");
215             DeleteCredList(cred);
216             goto exit;
217         }
218         printf("CRED Cbor Size: %zd\n", credCborSize);
219         DeleteCredList(cred);
220     }
221
222     CborEncoder encoder;
223     size_t cborSize = aclCborSize + pstatCborSize + doxmCborSize + svcCborSize + credCborSize + amaclCborSize;
224
225     printf("Total Cbor Size : %zd\n", cborSize);
226     cborSize += 255; // buffer margin for adding map and byte string
227     uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborSize);
228     VERIFY_NON_NULL(TAG, outPayload, ERROR);
229     cbor_encoder_init(&encoder, outPayload, cborSize, 0);
230     CborEncoder map;
231     CborError cborEncoderResult = cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
232     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Creating Main Map.");
233     if (aclCborSize > 0)
234     {
235         cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
236         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
237         cborEncoderResult = cbor_encode_byte_string(&map, aclCbor, aclCborSize);
238         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
239     }
240
241     if (pstatCborSize > 0)
242     {
243         cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
244         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
245         cborEncoderResult = cbor_encode_byte_string(&map, pstatCbor, pstatCborSize);
246         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
247     }
248     if (doxmCborSize > 0)
249     {
250         cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
251         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Name.");
252         cborEncoderResult = cbor_encode_byte_string(&map, doxmCbor, doxmCborSize);
253         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Value.");
254     }
255     if (amaclCborSize > 0)
256     {
257         cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_AMACL_NAME, strlen(OIC_JSON_AMACL_NAME));
258         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Name.");
259         cborEncoderResult = cbor_encode_byte_string(&map, amaclCbor, amaclCborSize);
260         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Value.");
261     }
262     if (svcCborSize > 0)
263     {
264         cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_SVC_NAME, strlen(OIC_JSON_SVC_NAME));
265         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Name.");
266         cborEncoderResult = cbor_encode_byte_string(&map, svcCbor, svcCborSize);
267         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Value.");
268     }
269     if (credCborSize > 0)
270     {
271         cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME));
272         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Name.");
273         cborEncoderResult = cbor_encode_byte_string(&map, credCbor, credCborSize);
274         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Value.");
275     }
276
277     cborEncoderResult = cbor_encoder_close_container(&encoder, &map);
278     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Container.");
279
280     size_t s = cbor_encoder_get_buffer_size(&encoder, outPayload);
281     OIC_LOG_V(DEBUG, TAG, "Payload size %zu", s);
282
283     fp1 = fopen(cborFileName, "w");
284     if (fp1)
285     {
286         size_t bytesWritten = fwrite(outPayload, 1, s, fp1);
287         if (bytesWritten == s)
288         {
289             OIC_LOG_V(DEBUG, TAG, "Written %zu bytes", bytesWritten);
290         }
291         else
292         {
293             OIC_LOG_V(ERROR, TAG, "Failed writing %zu bytes", s);
294         }
295         fclose(fp1);
296         fp1 = NULL;
297     }
298 exit:
299
300     cJSON_Delete(jsonRoot);
301     OICFree(aclCbor);
302     OICFree(doxmCbor);
303     OICFree(pstatCbor);
304     OICFree(amaclCbor);
305     OICFree(svcCbor);
306     OICFree(credCbor);
307     OICFree(jsonStr);
308     return ;
309 }
310
311 OicSecAcl_t* JSONToAclBin(const char * jsonStr)
312 {
313     OCStackResult ret = OC_STACK_ERROR;
314     OicSecAcl_t * headAcl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
315     cJSON *jsonRoot = NULL;
316
317     VERIFY_NON_NULL(TAG, jsonStr, ERROR);
318
319     jsonRoot = cJSON_Parse(jsonStr);
320     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
321
322     cJSON *jsonAclMap = cJSON_GetObjectItem(jsonRoot, OIC_JSON_ACL_NAME);
323     VERIFY_NON_NULL(TAG, jsonAclMap, ERROR);
324
325     cJSON *jsonAclObj = NULL;
326
327     // aclist
328     jsonAclObj = cJSON_GetObjectItem(jsonAclMap, OIC_JSON_ACLIST_NAME);
329     VERIFY_NON_NULL(TAG, jsonAclObj, ERROR);
330
331     // aclist-aces
332     cJSON *jsonAclArray = NULL;
333     jsonAclArray = cJSON_GetObjectItem(jsonAclObj, OIC_JSON_ACES_NAME);
334     VERIFY_NON_NULL(TAG, jsonAclArray, ERROR);
335
336     if (cJSON_Array == jsonAclArray->type)
337     {
338
339         int numAcl = cJSON_GetArraySize(jsonAclArray);
340         int idx = 0;
341
342         VERIFY_SUCCESS(TAG, numAcl > 0, INFO);
343         do
344         {
345             cJSON *jsonAcl = cJSON_GetArrayItem(jsonAclArray, idx);
346             VERIFY_NON_NULL(TAG, jsonAcl, ERROR);
347
348             OicSecAce_t *ace = (OicSecAce_t*)OICCalloc(1, sizeof(OicSecAce_t));
349             VERIFY_NON_NULL(TAG, ace, ERROR);
350             LL_APPEND(headAcl->aces, ace);
351
352             size_t jsonObjLen = 0;
353             cJSON *jsonObj = NULL;
354             jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_SUBJECTID_NAME);
355             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
356             VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
357             if(strcmp(jsonObj->valuestring, WILDCARD_RESOURCE_URI) == 0)
358             {
359                 ace->subjectuuid.id[0] = '*';
360             }
361             else
362             {
363                 ret = ConvertStrToUuid(jsonObj->valuestring, &ace->subjectuuid);
364                 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
365             }
366             // Resources -- Mandatory
367             jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_RESOURCES_NAME);
368             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
369             VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
370
371             size_t resourcesLen = (size_t)cJSON_GetArraySize(jsonObj);
372             VERIFY_SUCCESS(TAG, resourcesLen > 0, ERROR);
373
374             for(size_t idxx = 0; idxx < resourcesLen; idxx++)
375             {
376                 OicSecRsrc_t* rsrc = (OicSecRsrc_t*)OICCalloc(1, sizeof(OicSecRsrc_t));
377                 VERIFY_NON_NULL(TAG, rsrc, ERROR);
378
379                 cJSON *jsonRsrc = cJSON_GetArrayItem(jsonObj, idxx);
380                 VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
381
382                 //href
383                 size_t jsonRsrcObjLen = 0;
384                 cJSON *jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_HREF_NAME);
385                 VERIFY_NON_NULL(TAG, jsonRsrcObj, ERROR);
386                 VERIFY_SUCCESS(TAG, cJSON_String == jsonRsrcObj->type, ERROR);
387
388                 jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
389                 rsrc->href = (char*)OICMalloc(jsonRsrcObjLen);
390                 VERIFY_NON_NULL(TAG, (rsrc->href), ERROR);
391                 OICStrcpy(rsrc->href, jsonRsrcObjLen, jsonRsrcObj->valuestring);
392
393                 //rel
394                 jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_REL_NAME);
395                 if(jsonRsrcObj)
396                 {
397                     jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
398                     rsrc->rel = (char*)OICMalloc(jsonRsrcObjLen);
399                     VERIFY_NON_NULL(TAG, (rsrc->rel), ERROR);
400                     OICStrcpy(rsrc->rel, jsonRsrcObjLen, jsonRsrcObj->valuestring);
401                 }
402
403                 //rt
404                 jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_RT_NAME);
405                 if(jsonRsrcObj && cJSON_Array == jsonRsrcObj->type)
406                 {
407                     rsrc->typeLen = (size_t)cJSON_GetArraySize(jsonRsrcObj);
408                     VERIFY_SUCCESS(TAG, (0 < rsrc->typeLen), ERROR);
409                     rsrc->types = (char**)OICCalloc(rsrc->typeLen, sizeof(char*));
410                     VERIFY_NON_NULL(TAG, (rsrc->types), ERROR);
411                     for(size_t i = 0; i < rsrc->typeLen; i++)
412                     {
413                         cJSON *jsonRsrcType = cJSON_GetArrayItem(jsonRsrcObj, i);
414                         VERIFY_NON_NULL(TAG, jsonRsrcType, ERROR);
415                         rsrc->types[i] = OICStrdup(jsonRsrcType->valuestring);
416                         VERIFY_NON_NULL(TAG, (rsrc->types[i]), ERROR);
417                     }
418                 }
419
420                 //if
421                 jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_IF_NAME);
422                 if(jsonRsrcObj && cJSON_Array == jsonRsrcObj->type)
423                 {
424                     rsrc->interfaceLen = (size_t)cJSON_GetArraySize(jsonRsrcObj);
425                     VERIFY_SUCCESS(TAG, (0 < rsrc->interfaceLen), ERROR);
426                     rsrc->interfaces = (char**)OICCalloc(rsrc->interfaceLen, sizeof(char*));
427                     VERIFY_NON_NULL(TAG, (rsrc->interfaces), ERROR);
428                     for(size_t i = 0; i < rsrc->interfaceLen; i++)
429                     {
430                         cJSON *jsonInterface = cJSON_GetArrayItem(jsonRsrcObj, i);
431                         VERIFY_NON_NULL(TAG, jsonInterface, ERROR);
432                         rsrc->interfaces[i] = OICStrdup(jsonInterface->valuestring);
433                         VERIFY_NON_NULL(TAG, (rsrc->interfaces[i]), ERROR);
434                     }
435                 }
436
437                 LL_APPEND(ace->resources, rsrc);
438             }
439
440             // Permissions -- Mandatory
441             jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_PERMISSION_NAME);
442             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
443             VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
444             ace->permission = jsonObj->valueint;
445
446             //Validity -- Not Mandatory
447             cJSON *jsonValidityObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_VALIDITY_NAME);
448             if(jsonValidityObj)
449             {
450                 VERIFY_SUCCESS(TAG, cJSON_Array == jsonValidityObj->type, ERROR);
451                 size_t validityLen = (size_t) cJSON_GetArraySize(jsonValidityObj);
452                 VERIFY_SUCCESS(TAG, (0 < validityLen), ERROR);
453
454                 cJSON *jsonValidity = NULL;
455                 for(size_t i = 0; i < validityLen; i++)
456                 {
457                     jsonValidity = cJSON_GetArrayItem(jsonValidityObj, i);
458                     VERIFY_NON_NULL(TAG, jsonValidity, ERROR);
459                     VERIFY_SUCCESS(TAG, (jsonValidity->type == cJSON_Array), ERROR);
460
461                     OicSecValidity_t* validity = (OicSecValidity_t*)OICCalloc(1, sizeof(OicSecValidity_t));
462                     VERIFY_NON_NULL(TAG, validity, ERROR);
463                     LL_APPEND(ace->validities, validity);
464
465                     //Period
466                     cJSON* jsonPeriod = cJSON_GetArrayItem(jsonValidity, 0);
467                     if(jsonPeriod)
468                     {
469                         VERIFY_SUCCESS(TAG, (cJSON_String == jsonPeriod->type), ERROR);
470
471                         jsonObjLen = strlen(jsonPeriod->valuestring) + 1;
472                         validity->period = (char*)OICMalloc(jsonObjLen);
473                         VERIFY_NON_NULL(TAG, validity->period, ERROR);
474                         OICStrcpy(validity->period, jsonObjLen, jsonPeriod->valuestring);
475                     }
476
477                     //Recurrence
478                     cJSON* jsonRecurObj = cJSON_GetArrayItem(jsonValidity, 1);
479                     if(jsonRecurObj)
480                     {
481                         VERIFY_SUCCESS(TAG, (cJSON_Array == jsonRecurObj->type), ERROR);
482                         validity->recurrenceLen = (size_t) cJSON_GetArraySize(jsonRecurObj);
483                         VERIFY_SUCCESS(TAG, (0 < validity->recurrenceLen), ERROR);
484
485                         validity->recurrences = (char**)OICCalloc(validity->recurrenceLen, sizeof(char*));
486                         VERIFY_NON_NULL(TAG, validity->recurrences, ERROR);
487
488                         cJSON *jsonRecur = NULL;
489                         for(size_t i = 0; i < validity->recurrenceLen; i++)
490                         {
491                             jsonRecur = cJSON_GetArrayItem(jsonRecurObj, i);
492                             VERIFY_NON_NULL(TAG, jsonRecur, ERROR);
493                             jsonObjLen = strlen(jsonRecur->valuestring) + 1;
494                             validity->recurrences[i] = (char*)OICMalloc(jsonObjLen);
495                             VERIFY_NON_NULL(TAG, validity->recurrences[i], ERROR);
496                             OICStrcpy(validity->recurrences[i], jsonObjLen, jsonRecur->valuestring);
497                         }
498                     }
499                 }
500             }
501         } while( ++idx < numAcl);
502     }
503
504
505     // rownerid
506     jsonAclObj = cJSON_GetObjectItem(jsonAclMap, OIC_JSON_ROWNERID_NAME);
507     VERIFY_NON_NULL(TAG, jsonAclObj, ERROR);
508     VERIFY_SUCCESS(TAG, cJSON_String == jsonAclObj->type, ERROR);
509     ret = ConvertStrToUuid(jsonAclObj->valuestring, &headAcl->rownerID);
510     VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
511
512     ret = OC_STACK_OK;
513
514 exit:
515     cJSON_Delete(jsonRoot);
516     if (OC_STACK_OK != ret)
517     {
518         DeleteACLList(headAcl);
519         headAcl = NULL;
520     }
521     return headAcl;
522 }
523
524 OicSecDoxm_t* JSONToDoxmBin(const char * jsonStr)
525 {
526     printf("IN JSONToDoxmBin\n");
527     if (NULL == jsonStr)
528     {
529         return NULL;
530     }
531
532     OCStackResult ret = OC_STACK_ERROR;
533     OicSecDoxm_t *doxm =  NULL;
534     cJSON *jsonDoxm = NULL;
535     cJSON *jsonObj = NULL;
536
537     size_t jsonObjLen = 0;
538
539     cJSON *jsonRoot = cJSON_Parse(jsonStr);
540     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
541
542     jsonDoxm = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME);
543     VERIFY_NON_NULL(TAG, jsonDoxm, ERROR);
544
545     doxm = (OicSecDoxm_t *)OICCalloc(1, sizeof(OicSecDoxm_t));
546     VERIFY_NON_NULL(TAG, doxm, ERROR);
547
548     //OxmType -- not Mandatory
549     jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_TYPE_NAME);
550     if ((jsonObj) && (cJSON_Array == jsonObj->type))
551     {
552         doxm->oxmTypeLen = (size_t)cJSON_GetArraySize(jsonObj);
553         VERIFY_SUCCESS(TAG, doxm->oxmTypeLen > 0, ERROR);
554
555         doxm->oxmType = (OicUrn_t *)OICCalloc(doxm->oxmTypeLen, sizeof(char *));
556         VERIFY_NON_NULL(TAG, (doxm->oxmType), ERROR);
557
558         for (size_t i  = 0; i < doxm->oxmTypeLen ; i++)
559         {
560             cJSON *jsonOxmTy = cJSON_GetArrayItem(jsonObj, i);
561             VERIFY_NON_NULL(TAG, jsonOxmTy, ERROR);
562
563             jsonObjLen = strlen(jsonOxmTy->valuestring) + 1;
564             doxm->oxmType[i] = (char*)OICMalloc(jsonObjLen);
565             VERIFY_NON_NULL(TAG, doxm->oxmType[i], ERROR);
566             strncpy((char *)doxm->oxmType[i], (char *)jsonOxmTy->valuestring, jsonObjLen);
567         }
568     }
569
570     //Oxm -- not Mandatory
571     jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXMS_NAME);
572     if (jsonObj && cJSON_Array == jsonObj->type)
573     {
574         doxm->oxmLen = (size_t)cJSON_GetArraySize(jsonObj);
575         VERIFY_SUCCESS(TAG, doxm->oxmLen > 0, ERROR);
576
577         doxm->oxm = (OicSecOxm_t*)OICCalloc(doxm->oxmLen, sizeof(OicSecOxm_t));
578         VERIFY_NON_NULL(TAG, doxm->oxm, ERROR);
579
580         for (size_t i  = 0; i < doxm->oxmLen ; i++)
581         {
582             cJSON *jsonOxm = cJSON_GetArrayItem(jsonObj, i);
583             VERIFY_NON_NULL(TAG, jsonOxm, ERROR);
584             doxm->oxm[i] = (OicSecOxm_t)jsonOxm->valueint;
585         }
586     }
587
588     //OxmSel -- Mandatory
589     jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_SEL_NAME);
590     if (jsonObj)
591     {
592         VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
593         doxm->oxmSel = (OicSecOxm_t)jsonObj->valueint;
594     }
595
596     //sct -- Mandatory
597     jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_SUPPORTED_CRED_TYPE_NAME);
598     if (jsonObj)
599     {
600         VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
601         doxm->sct = (OicSecCredType_t)jsonObj->valueint;
602     }
603
604     //Owned -- Mandatory
605     jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OWNED_NAME);
606     if (jsonObj)
607     {
608         VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type), ERROR);
609         doxm->owned = jsonObj->valueint;
610     }
611
612     //DPC -- Mandatory
613     jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DPC_NAME);
614     if (jsonObj)
615     {
616         VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type), ERROR);
617         doxm->dpc = jsonObj->valueint;
618     }
619
620 #ifdef _ENABLE_MULTIPLE_OWNER_
621     //mom -- Not Mandatory
622     jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_MOM_NAME);
623     if (jsonObj)
624     {
625         VERIFY_SUCCESS(TAG, (cJSON_Number == jsonObj->type), ERROR);
626         doxm->mom = (OicSecMom_t*)OICCalloc(1, sizeof(OicSecMom_t));
627         VERIFY_NON_NULL(TAG, doxm->mom, ERROR);
628         doxm->mom->mode = (OicSecMomType_t)jsonObj->valueint;
629     }
630 #endif //_ENABLE_MULTIPLE_OWNER_
631
632     //DeviceId -- Mandatory
633     jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVICE_ID_NAME);
634     if (jsonObj)
635     {
636         VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
637         if (cJSON_String == jsonObj->type)
638         {
639             //Check for empty string, in case DeviceId field has not been set yet
640             if (jsonObj->valuestring[0])
641             {
642                 ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->deviceID);
643                 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
644             }
645         }
646     }
647
648     //rowner -- Mandatory
649     jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_ROWNERID_NAME);
650     if (true == doxm->owned)
651     {
652         VERIFY_NON_NULL(TAG, jsonObj, ERROR);
653     }
654     if (jsonObj)
655     {
656         ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->rownerID);
657         VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
658     }
659
660     //Owner -- will be empty when device status is unowned.
661     jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVOWNERID_NAME);
662     if (true == doxm->owned)
663     {
664         VERIFY_NON_NULL(TAG, jsonObj, ERROR);
665     }
666     if (jsonObj)
667     {
668         ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->owner);
669                 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
670     }
671
672     ret = OC_STACK_OK;
673
674 exit:
675     cJSON_Delete(jsonRoot);
676     if (OC_STACK_OK != ret)
677     {
678         DeleteDoxmBinData(doxm);
679         doxm = NULL;
680     }
681     printf("OUT JSONToDoxmBin\n");
682     return doxm;
683 }
684
685 OicSecPstat_t* JSONToPstatBin(const char * jsonStr)
686 {
687     printf("IN JSONToPstatBin\n");
688     if(NULL == jsonStr)
689     {
690         return NULL;
691     }
692
693     OCStackResult ret = OC_STACK_ERROR;
694     OicSecPstat_t *pstat = NULL;
695     cJSON *jsonPstat = NULL;
696     cJSON *jsonObj = NULL;
697
698     cJSON *jsonRoot = cJSON_Parse(jsonStr);
699     VERIFY_NON_NULL(TAG, jsonRoot, INFO);
700
701     jsonPstat = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
702     VERIFY_NON_NULL(TAG, jsonPstat, INFO);
703
704     pstat = (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
705     VERIFY_NON_NULL(TAG, pstat, INFO);
706     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_ISOP_NAME);
707     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
708     VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type) , ERROR);
709     pstat->isOp = jsonObj->valueint;
710
711     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_DEVICE_ID_NAME);
712     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
713     VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
714     ret = ConvertStrToUuid(jsonObj->valuestring, &pstat->deviceID);
715     VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
716
717     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_ROWNERID_NAME);
718     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
719     VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
720     ret = ConvertStrToUuid(jsonObj->valuestring, &pstat->rownerID);
721     VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
722
723     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_CM_NAME);
724     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
725     VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
726     pstat->cm  = (OicSecDpm_t)jsonObj->valueint;
727
728     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_TM_NAME);
729     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
730     VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
731     pstat->tm  = (OicSecDpm_t)jsonObj->valueint;
732
733     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_OM_NAME);
734     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
735     VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
736     pstat->om  = (OicSecDpom_t)jsonObj->valueint;
737
738     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_SM_NAME);
739     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
740     VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
741     pstat->smLen = 1;
742     pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
743     pstat->sm[0] = (OicSecDpom_t)jsonObj->valueint;
744
745     ret = OC_STACK_OK;
746
747 exit:
748     cJSON_Delete(jsonRoot);
749     if (OC_STACK_OK != ret)
750     {
751         OIC_LOG(ERROR, TAG, "JSONToPstatBin failed");
752     }
753     printf("OUT JSONToPstatBin\n");
754     return pstat;
755 }
756
757 static OicEncodingType_t GetEncodingTypeFromStr(const char* encodingType)
758 {
759     if (strcmp(OIC_SEC_ENCODING_RAW, encodingType) == 0)
760     {
761         return OIC_ENCODING_RAW;
762     }
763     if (strcmp(OIC_SEC_ENCODING_BASE64, encodingType) == 0)
764     {
765         return OIC_ENCODING_BASE64;
766     }
767     if (strcmp(OIC_SEC_ENCODING_PEM, encodingType) == 0)
768     {
769         return OIC_ENCODING_PEM;
770     }
771     if (strcmp(OIC_SEC_ENCODING_DER, encodingType) == 0)
772     {
773         return OIC_ENCODING_DER;
774     }
775     OIC_LOG(WARNING, TAG, "Unknow encoding type dectected!");
776     OIC_LOG(WARNING, TAG, "json2cbor will use \"oic.sec.encoding.raw\" as default encoding type.");
777     return OIC_ENCODING_RAW;
778 }
779
780 OicSecCred_t * JSONToCredBin(const char * jsonStr)
781 {
782     if (NULL == jsonStr)
783     {
784         OIC_LOG(ERROR, TAG,"JSONToCredBin jsonStr in NULL");
785         return NULL;
786     }
787
788     OicSecCred_t *headCred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
789     OCStackResult ret = OC_STACK_ERROR;
790     cJSON *jsonRoot = NULL;
791     VERIFY_NON_NULL(TAG, headCred, ERROR);
792
793     jsonRoot = cJSON_Parse(jsonStr);
794     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
795
796     cJSON *jsonCredMap = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
797     VERIFY_NON_NULL(TAG, jsonCredMap, ERROR);
798
799     // creds
800     cJSON *jsonCredArray = NULL;
801     jsonCredArray = cJSON_GetObjectItem(jsonCredMap, OIC_JSON_CREDS_NAME);
802     VERIFY_NON_NULL(TAG, jsonCredArray, ERROR);
803
804     if (cJSON_Array == jsonCredArray->type)
805     {
806         int numCred = cJSON_GetArraySize(jsonCredArray);
807         VERIFY_SUCCESS(TAG, numCred > 0, ERROR);
808         int idx = 0;
809         do
810         {
811             cJSON *jsonCred = cJSON_GetArrayItem(jsonCredArray, idx);
812             VERIFY_NON_NULL(TAG, jsonCred, ERROR);
813
814             OicSecCred_t *cred = NULL;
815             if(idx == 0)
816             {
817                 cred = headCred;
818             }
819             else
820             {
821                 cred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
822                 OicSecCred_t *temp = headCred;
823                 while (temp->next)
824                 {
825                     temp = temp->next;
826                 }
827                 temp->next = cred;
828             }
829             VERIFY_NON_NULL(TAG, cred, ERROR);
830
831             size_t jsonObjLen = 0;
832             cJSON *jsonObj = NULL;
833
834             //CredId -- Mandatory
835             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDID_NAME);
836             if(jsonObj)
837             {
838                 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
839                 cred->credId = jsonObj->valueint;
840             }
841
842             //subject -- Mandatory
843             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_SUBJECTID_NAME);
844             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
845             VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
846             if(strcmp(jsonObj->valuestring, WILDCARD_RESOURCE_URI) == 0)
847             {
848                 cred->subject.id[0] = '*';
849             }
850             else
851             {
852                 ret = ConvertStrToUuid(jsonObj->valuestring, &cred->subject);
853                 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
854             }
855
856             //CredType -- Mandatory
857             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDTYPE_NAME);
858             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
859             VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
860             cred->credType = (OicSecCredType_t)jsonObj->valueint;
861             //PrivateData is mandatory for some of the credential types listed below.
862             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PRIVATEDATA_NAME);
863
864             if (NULL != jsonObj)
865             {
866                 cJSON *jsonPriv = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
867                 VERIFY_NON_NULL(TAG, jsonPriv, ERROR);
868                 jsonObjLen = strlen(jsonPriv->valuestring);
869                 cred->privateData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
870                 VERIFY_NON_NULL(TAG, (cred->privateData.data), ERROR);
871                 memcpy(cred->privateData.data, jsonPriv->valuestring, jsonObjLen);
872                 cred->privateData.len = jsonObjLen;
873
874                 cJSON *jsonEncoding = cJSON_GetObjectItem(jsonObj, OIC_JSON_ENCODING_NAME);
875                 VERIFY_NON_NULL(TAG, jsonEncoding, ERROR);
876                 cred->privateData.encoding = GetEncodingTypeFromStr(jsonEncoding->valuestring);
877             }
878 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
879             //PublicData is mandatory only for SIGNED_ASYMMETRIC_KEY credentials type.
880             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PUBLICDATA_NAME);
881
882             if (NULL != jsonObj)
883             {
884                 cJSON *jsonPub = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
885                 VERIFY_NON_NULL(TAG, jsonPub, ERROR);
886                 jsonObjLen = strlen(jsonPub->valuestring);
887                 cred->publicData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
888                 VERIFY_NON_NULL(TAG, (cred->publicData.data), ERROR);
889                 memcpy(cred->publicData.data, jsonPub->valuestring, jsonObjLen);
890                 cred->publicData.len = jsonObjLen;
891             }
892
893             //Optional Data
894             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_OPTDATA_NAME);
895             if (NULL != jsonObj)
896             {
897                 cJSON *jsonOpt = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
898                 VERIFY_NON_NULL(TAG, jsonOpt, ERROR);
899                 jsonObjLen = strlen(jsonOpt->valuestring);
900                 cred->optionalData.data =  (uint8_t *)OICCalloc(1, jsonObjLen);
901                 VERIFY_NON_NULL(TAG, (cred->optionalData.data), ERROR);
902                 memcpy(cred->optionalData.data, jsonOpt->valuestring, jsonObjLen);
903                 cred->optionalData.len = jsonObjLen;
904
905                 cJSON *jsonEncoding = cJSON_GetObjectItem(jsonObj, OIC_JSON_ENCODING_NAME);
906                 VERIFY_NON_NULL(TAG, jsonEncoding, ERROR);
907                 cred->optionalData.encoding = GetEncodingTypeFromStr(jsonEncoding->valuestring);
908             }
909
910             //CredUsage
911             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDUSAGE_NAME);
912             if (NULL != jsonObj)
913             {
914                 jsonObjLen = strlen(jsonObj->valuestring);
915                 cred->credUsage = OICStrdup(jsonObj->valuestring);
916                 VERIFY_NON_NULL(TAG, (cred->credUsage), ERROR);
917             }
918
919 #endif // defined(__WITH_DTLS__) || defined(__WITH_TLS__)
920
921             //Period -- Not Mandatory
922             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PERIOD_NAME);
923             if(jsonObj && cJSON_String == jsonObj->type)
924             {
925                 jsonObjLen = strlen(jsonObj->valuestring) + 1;
926                 cred->period = (char *)OICMalloc(jsonObjLen);
927                 VERIFY_NON_NULL(TAG, cred->period, ERROR);
928                 strncpy(cred->period, jsonObj->valuestring, jsonObjLen);
929             }
930             cred->next = NULL;
931         } while( ++idx < numCred);
932     }
933
934     // rownerid
935     cJSON *jsonCredObj = cJSON_GetObjectItem(jsonCredMap, OIC_JSON_ROWNERID_NAME);
936     VERIFY_NON_NULL(TAG, jsonCredObj, ERROR);
937     VERIFY_SUCCESS(TAG, cJSON_String == jsonCredObj->type, ERROR);
938     ret = ConvertStrToUuid(jsonCredObj->valuestring, &headCred->rownerID);
939     VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
940     ret = OC_STACK_OK;
941
942 exit:
943     cJSON_Delete(jsonRoot);
944     if (OC_STACK_OK != ret)
945     {
946         DeleteCredList(headCred);
947         headCred = NULL;
948     }
949     return headCred;
950 }
951
952 static OicSecSvc_t* JSONToSvcBin(const char * jsonStr)
953 {
954     OCStackResult ret = OC_STACK_ERROR;
955     OicSecSvc_t * headSvc = NULL;
956     OicSecSvc_t * prevSvc = NULL;
957     cJSON *jsonRoot = NULL;
958     cJSON *jsonSvcArray = NULL;
959
960     VERIFY_NON_NULL(TAG, jsonStr, ERROR);
961
962     jsonRoot = cJSON_Parse(jsonStr);
963     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
964
965     jsonSvcArray = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME);
966     VERIFY_NON_NULL(TAG, jsonSvcArray, INFO);
967
968     if (cJSON_Array == jsonSvcArray->type)
969     {
970         int numSvc = cJSON_GetArraySize(jsonSvcArray);
971         int idx = 0;
972
973         VERIFY_SUCCESS(TAG, numSvc > 0, INFO);
974         do
975         {
976             cJSON *jsonSvc = cJSON_GetArrayItem(jsonSvcArray, idx);
977             VERIFY_NON_NULL(TAG, jsonSvc, ERROR);
978
979             OicSecSvc_t *svc = (OicSecSvc_t*)OICCalloc(1, sizeof(OicSecSvc_t));
980             VERIFY_NON_NULL(TAG, svc, ERROR);
981
982             headSvc = (headSvc) ? headSvc : svc;
983             if (prevSvc)
984             {
985                 prevSvc->next = svc;
986             }
987
988             cJSON *jsonObj = NULL;
989             unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {0};
990             uint32_t outLen = 0;
991             B64Result b64Ret = B64_OK;
992
993             // Service Device Identity
994             jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_DEVICE_ID);
995             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
996             VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
997             outLen = 0;
998             b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
999                        sizeof(base64Buff), &outLen);
1000             VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->svcdid.id)), ERROR);
1001             memcpy(svc->svcdid.id, base64Buff, outLen);
1002
1003             // Service Type
1004             jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_TYPE);
1005             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1006             VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
1007             svc->svct = (OicSecSvcType_t)jsonObj->valueint;
1008
1009             // Resource Owners
1010             jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_OWNERS_NAME);
1011             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1012             VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
1013
1014             svc->ownersLen = (size_t)cJSON_GetArraySize(jsonObj);
1015             VERIFY_SUCCESS(TAG, svc->ownersLen > 0, ERROR);
1016             svc->owners = (OicUuid_t*)OICCalloc(svc->ownersLen, sizeof(OicUuid_t));
1017             VERIFY_NON_NULL(TAG, (svc->owners), ERROR);
1018
1019             size_t idxx = 0;
1020             do
1021             {
1022                 cJSON *jsonOwnr = cJSON_GetArrayItem(jsonObj, idxx);
1023                 VERIFY_NON_NULL(TAG, jsonOwnr, ERROR);
1024                 VERIFY_SUCCESS(TAG, cJSON_String == jsonOwnr->type, ERROR);
1025                 outLen = 0;
1026                 b64Ret = b64Decode(jsonOwnr->valuestring, strlen(jsonOwnr->valuestring), base64Buff,
1027                            sizeof(base64Buff), &outLen);
1028
1029                 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->owners[idxx].id)),
1030                                    ERROR);
1031                 memcpy(svc->owners[idxx].id, base64Buff, outLen);
1032             } while ( ++idxx < svc->ownersLen);
1033
1034             prevSvc = svc;
1035         } while( ++idx < numSvc);
1036     }
1037
1038     ret = OC_STACK_OK;
1039
1040 exit:
1041     cJSON_Delete(jsonRoot);
1042     if (OC_STACK_OK != ret)
1043     {
1044         DeleteSVCList(headSvc);
1045         headSvc = NULL;
1046     }
1047     return headSvc;
1048 }
1049
1050 static OicSecAmacl_t* JSONToAmaclBin(const char * jsonStr)
1051 {
1052     OCStackResult ret = OC_STACK_ERROR;
1053     OicSecAmacl_t * headAmacl = (OicSecAmacl_t*)OICCalloc(1, sizeof(OicSecAmacl_t));
1054
1055     cJSON *jsonRoot = NULL;
1056     cJSON *jsonAmacl = NULL;
1057
1058     VERIFY_NON_NULL(TAG, jsonStr, ERROR);
1059
1060     jsonRoot = cJSON_Parse(jsonStr);
1061     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
1062
1063     jsonAmacl = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
1064     VERIFY_NON_NULL(TAG, jsonAmacl, INFO);
1065
1066     cJSON *jsonObj = NULL;
1067
1068     // Resources -- Mandatory
1069     jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_RESOURCES_NAME);
1070     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1071
1072     // Rlist
1073     cJSON *jsonRlistArray = cJSON_GetObjectItem(jsonObj, OIC_JSON_RLIST_NAME);
1074     VERIFY_NON_NULL(TAG, jsonRlistArray, ERROR);
1075     VERIFY_SUCCESS(TAG, cJSON_Array == jsonRlistArray->type, ERROR);
1076
1077     headAmacl->resourcesLen = (size_t)cJSON_GetArraySize(jsonRlistArray);
1078     headAmacl->resources = (char**)OICCalloc(headAmacl->resourcesLen, sizeof(char*));
1079     size_t idxx = 0;
1080     do
1081     {
1082         cJSON *jsonRsrc = cJSON_GetArrayItem(jsonRlistArray, idxx);
1083         VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
1084
1085         cJSON *jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_HREF_NAME);
1086         VERIFY_NON_NULL(TAG, jsonRsrcObj, ERROR);
1087         VERIFY_SUCCESS(TAG, cJSON_String == jsonRsrcObj->type, ERROR);
1088
1089         size_t jsonRsrcObjLen = 0;
1090         jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
1091         headAmacl->resources[idxx] = (char*)OICMalloc(jsonRsrcObjLen);
1092         VERIFY_NON_NULL(TAG, (headAmacl->resources[idxx]), ERROR);
1093         OICStrcpy(headAmacl->resources[idxx], jsonRsrcObjLen, jsonRsrcObj->valuestring);
1094
1095     } while ( ++idxx < headAmacl->resourcesLen);
1096
1097     // Ams -- Mandatory
1098     jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_AMS_NAME);
1099     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1100     VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
1101
1102     headAmacl->amssLen = (size_t)cJSON_GetArraySize(jsonObj);
1103     VERIFY_SUCCESS(TAG, headAmacl->amssLen > 0, ERROR);
1104     headAmacl->amss = (OicUuid_t*)OICCalloc(headAmacl->amssLen, sizeof(OicUuid_t));
1105     VERIFY_NON_NULL(TAG, headAmacl->amss, ERROR);
1106
1107     idxx = 0;
1108     do
1109     {
1110         cJSON *jsonAms = cJSON_GetArrayItem(jsonObj, idxx);
1111         VERIFY_NON_NULL(TAG, jsonAms, ERROR);
1112         VERIFY_SUCCESS(TAG, cJSON_String == jsonAms->type, ERROR);
1113
1114         memcpy(headAmacl->amss[idxx].id, (OicUuid_t *)jsonAms->valuestring, strlen(jsonAms->valuestring));
1115
1116     } while ( ++idxx < headAmacl->amssLen);
1117
1118
1119     // Rowner -- Mandatory
1120     jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_ROWNERID_NAME);
1121     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1122     VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
1123
1124     ret = ConvertStrToUuid(jsonObj->valuestring, &headAmacl->rownerID);
1125     VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
1126
1127     ret = OC_STACK_OK;
1128
1129 exit:
1130     cJSON_Delete(jsonRoot);
1131     if (OC_STACK_OK != ret)
1132     {
1133         DeleteAmaclList(headAmacl);
1134         headAmacl = NULL;
1135     }
1136     return headAmacl;
1137 }
1138
1139 int main(int argc, char* argv[])
1140 {
1141     if (argc == 3)
1142     {
1143         printf("JSON File Name: %s\n CBOR File Name: %s \n", argv[1], argv[2]);
1144         ConvertJsonToCBOR(argv[1], argv[2]);
1145     }
1146     else
1147     {
1148         printf("This program requires two inputs:\n");
1149         printf("1. First input is a json file tha will be converted to cbor. \n");
1150         printf("2. Second input is a resulting cbor file that will store converted cbor. \n");
1151         printf("\t json2cbor <json_file_name> <cbor_file_name>. \n");
1152     }
1153 }