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