Update db files and Fix json2cbor tool
[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     CborEncoder encoder = { {.ptr = NULL }, .end = 0 };
219     size_t cborSize = aclCborSize + pstatCborSize + doxmCborSize + svcCborSize + credCborSize + amaclCborSize;
220
221     printf("Total Cbor Size : %zd\n", cborSize);
222     cborSize += 255; // buffer margin for adding map and byte string
223     uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborSize);
224     VERIFY_NON_NULL(TAG, outPayload, ERROR);
225     cbor_encoder_init(&encoder, outPayload, cborSize, 0);
226     CborEncoder map = { {.ptr = NULL }, .end = 0 };
227     CborError cborEncoderResult = cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
228     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Creating Main Map.");
229     if (aclCborSize > 0)
230     {
231         cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
232         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
233         cborEncoderResult = cbor_encode_byte_string(&map, aclCbor, aclCborSize);
234         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
235     }
236
237     if (pstatCborSize > 0)
238     {
239         cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
240         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
241         cborEncoderResult = cbor_encode_byte_string(&map, pstatCbor, pstatCborSize);
242         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
243     }
244     if (doxmCborSize > 0)
245     {
246         cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
247         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Name.");
248         cborEncoderResult = cbor_encode_byte_string(&map, doxmCbor, doxmCborSize);
249         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Value.");
250     }
251     if (amaclCborSize > 0)
252     {
253         cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_AMACL_NAME, strlen(OIC_JSON_AMACL_NAME));
254         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Name.");
255         cborEncoderResult = cbor_encode_byte_string(&map, amaclCbor, amaclCborSize);
256         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Value.");
257     }
258     if (svcCborSize > 0)
259     {
260         cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_SVC_NAME, strlen(OIC_JSON_SVC_NAME));
261         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Name.");
262         cborEncoderResult = cbor_encode_byte_string(&map, svcCbor, svcCborSize);
263         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Value.");
264     }
265     if (credCborSize > 0)
266     {
267         cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME));
268         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Name.");
269         cborEncoderResult = cbor_encode_byte_string(&map, credCbor, credCborSize);
270         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Value.");
271     }
272
273     cborEncoderResult = cbor_encoder_close_container(&encoder, &map);
274     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Container.");
275
276     size_t s = encoder.ptr - outPayload;
277     OIC_LOG_V(DEBUG, TAG, "Payload size %zu", s);
278
279     fp1 = fopen(cborFileName, "w");
280     if (fp1)
281     {
282         size_t bytesWritten = fwrite(outPayload, 1, s, fp1);
283         if (bytesWritten == s)
284         {
285             OIC_LOG_V(DEBUG, TAG, "Written %zu bytes", bytesWritten);
286         }
287         else
288         {
289             OIC_LOG_V(ERROR, TAG, "Failed writing %zu bytes", s);
290         }
291         fclose(fp1);
292         fp1 = NULL;
293     }
294 exit:
295
296     cJSON_Delete(jsonRoot);
297     OICFree(aclCbor);
298     OICFree(doxmCbor);
299     OICFree(pstatCbor);
300     OICFree(amaclCbor);
301     OICFree(svcCbor);
302     OICFree(credCbor);
303     OICFree(jsonStr);
304     return ;
305 }
306
307 OicSecAcl_t* JSONToAclBin(const char * jsonStr)
308 {
309     OCStackResult ret = OC_STACK_ERROR;
310     OicSecAcl_t * headAcl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
311     cJSON *jsonRoot = NULL;
312
313     VERIFY_NON_NULL(TAG, jsonStr, ERROR);
314
315     jsonRoot = cJSON_Parse(jsonStr);
316     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
317
318     cJSON *jsonAclMap = cJSON_GetObjectItem(jsonRoot, OIC_JSON_ACL_NAME);
319     VERIFY_NON_NULL(TAG, jsonAclMap, ERROR);
320
321     cJSON *jsonAclObj = NULL;
322
323     // aclist
324     jsonAclObj = cJSON_GetObjectItem(jsonAclMap, OIC_JSON_ACLIST_NAME);
325     VERIFY_NON_NULL(TAG, jsonAclObj, ERROR);
326
327     // aclist-aces
328     cJSON *jsonAclArray = NULL;
329     jsonAclArray = cJSON_GetObjectItem(jsonAclObj, OIC_JSON_ACES_NAME);
330     VERIFY_NON_NULL(TAG, jsonAclArray, ERROR);
331
332     if (cJSON_Array == jsonAclArray->type)
333     {
334
335         int numAcl = cJSON_GetArraySize(jsonAclArray);
336         int idx = 0;
337
338         VERIFY_SUCCESS(TAG, numAcl > 0, INFO);
339         do
340         {
341             cJSON *jsonAcl = cJSON_GetArrayItem(jsonAclArray, idx);
342             VERIFY_NON_NULL(TAG, jsonAcl, ERROR);
343
344             OicSecAcl_t *acl = NULL;
345             if(idx == 0)
346             {
347                 acl = headAcl;
348             }
349             else
350             {
351                 acl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
352                 OicSecAcl_t *temp = headAcl;
353                 while (temp->next)
354                 {
355                     temp = temp->next;
356                 }
357                 temp->next = acl;
358             }
359
360             VERIFY_NON_NULL(TAG, acl, ERROR);
361
362             size_t jsonObjLen = 0;
363             cJSON *jsonObj = NULL;
364             jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_SUBJECTID_NAME);
365             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
366             VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
367             if(strcmp(jsonObj->valuestring, WILDCARD_RESOURCE_URI) == 0)
368             {
369                 acl->subject.id[0] = '*';
370             }
371             else
372             {
373                 ret = ConvertStrToUuid(jsonObj->valuestring, &acl->subject);
374                 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
375             }
376             // Resources -- Mandatory
377             jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_RESOURCES_NAME);
378             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
379             VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
380
381             acl->resourcesLen = (size_t)cJSON_GetArraySize(jsonObj);
382
383             VERIFY_SUCCESS(TAG, acl->resourcesLen > 0, ERROR);
384             acl->resources = (char**)OICCalloc(acl->resourcesLen, sizeof(char*));
385             VERIFY_NON_NULL(TAG, (acl->resources), ERROR);
386
387             size_t idxx = 0;
388             do
389             {
390                 cJSON *jsonRsrc = cJSON_GetArrayItem(jsonObj, idxx);
391                 VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
392
393                 size_t jsonRsrcObjLen = 0;
394                 cJSON *jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_HREF_NAME);
395                 VERIFY_NON_NULL(TAG, jsonRsrcObj, ERROR);
396                 VERIFY_SUCCESS(TAG, cJSON_String == jsonRsrcObj->type, ERROR);
397
398                 jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
399                 acl->resources[idxx] = (char*)OICMalloc(jsonRsrcObjLen);
400
401                 VERIFY_NON_NULL(TAG, (acl->resources[idxx]), ERROR);
402                 OICStrcpy(acl->resources[idxx], jsonRsrcObjLen, jsonRsrcObj->valuestring);
403
404             } while ( ++idxx < acl->resourcesLen);
405
406             // Permissions -- Mandatory
407             jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_PERMISSION_NAME);
408             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
409             VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
410             acl->permission = jsonObj->valueint;
411             //Period -- Not Mandatory
412             cJSON *jsonPeriodObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_PERIOD_NAME);
413             if(jsonPeriodObj)
414             {
415                 VERIFY_SUCCESS(TAG, cJSON_Array == jsonPeriodObj->type, ERROR);
416                 acl->prdRecrLen = (size_t)cJSON_GetArraySize(jsonPeriodObj);
417                 if(acl->prdRecrLen > 0)
418                 {
419                     acl->periods = (char**)OICCalloc(acl->prdRecrLen, sizeof(char*));
420                     VERIFY_NON_NULL(TAG, acl->periods, ERROR);
421
422                     cJSON *jsonPeriod = NULL;
423                     for(size_t i = 0; i < acl->prdRecrLen; i++)
424                     {
425                         jsonPeriod = cJSON_GetArrayItem(jsonPeriodObj, i);
426                         VERIFY_NON_NULL(TAG, jsonPeriod, ERROR);
427
428                         jsonObjLen = strlen(jsonPeriod->valuestring) + 1;
429                         acl->periods[i] = (char*)OICMalloc(jsonObjLen);
430                         VERIFY_NON_NULL(TAG, acl->periods[i], ERROR);
431                         OICStrcpy(acl->periods[i], jsonObjLen, jsonPeriod->valuestring);
432                     }
433                 }
434             }
435             //Recurrence -- Not mandatory
436             cJSON *jsonRecurObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_RECURRENCES_NAME);
437             if(jsonRecurObj)
438             {
439
440                 VERIFY_SUCCESS(TAG, cJSON_Array == jsonRecurObj->type, ERROR);
441
442                 if(acl->prdRecrLen > 0)
443                 {
444                     acl->recurrences = (char**)OICCalloc(acl->prdRecrLen, sizeof(char*));
445                     VERIFY_NON_NULL(TAG, acl->recurrences, ERROR);
446
447                     cJSON *jsonRecur = NULL;
448                     for(size_t i = 0; i < acl->prdRecrLen; i++)
449                     {
450                         jsonRecur = cJSON_GetArrayItem(jsonRecurObj, i);
451                         VERIFY_NON_NULL(TAG, jsonRecur, ERROR);
452                         jsonObjLen = strlen(jsonRecur->valuestring) + 1;
453                         acl->recurrences[i] = (char*)OICMalloc(jsonObjLen);
454                         VERIFY_NON_NULL(TAG, acl->recurrences[i], ERROR);
455                         OICStrcpy(acl->recurrences[i], jsonObjLen, jsonRecur->valuestring);
456                     }
457                 }
458             }
459
460             acl->next = NULL;
461
462         } while( ++idx < numAcl);
463     }
464
465
466     // rownerid
467     jsonAclObj = cJSON_GetObjectItem(jsonAclMap, OIC_JSON_ROWNERID_NAME);
468     VERIFY_NON_NULL(TAG, jsonAclObj, ERROR);
469     VERIFY_SUCCESS(TAG, cJSON_String == jsonAclObj->type, ERROR);
470     headAcl->ownersLen = 1;
471     VERIFY_SUCCESS(TAG, headAcl->ownersLen > 0, ERROR);
472     headAcl->owners = (OicUuid_t*)OICCalloc(headAcl->ownersLen, sizeof(OicUuid_t));
473     VERIFY_NON_NULL(TAG, (headAcl->owners), ERROR);
474     ret = ConvertStrToUuid(jsonAclObj->valuestring, &headAcl->owners[0]);
475     VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
476
477     ret = OC_STACK_OK;
478
479 exit:
480     cJSON_Delete(jsonRoot);
481     if (OC_STACK_OK != ret)
482     {
483         DeleteACLList(headAcl);
484         headAcl = NULL;
485     }
486     return headAcl;
487 }
488
489 OicSecDoxm_t* JSONToDoxmBin(const char * jsonStr)
490 {
491     printf("IN JSONToDoxmBin\n");
492     if (NULL == jsonStr)
493     {
494         return NULL;
495     }
496
497     OCStackResult ret = OC_STACK_ERROR;
498     OicSecDoxm_t *doxm =  NULL;
499     cJSON *jsonDoxm = NULL;
500     cJSON *jsonObj = NULL;
501
502     size_t jsonObjLen = 0;
503
504     cJSON *jsonRoot = cJSON_Parse(jsonStr);
505     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
506
507     jsonDoxm = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME);
508     VERIFY_NON_NULL(TAG, jsonDoxm, ERROR);
509
510     doxm = (OicSecDoxm_t *)OICCalloc(1, sizeof(OicSecDoxm_t));
511     VERIFY_NON_NULL(TAG, doxm, ERROR);
512
513     //OxmType -- not Mandatory
514     jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_TYPE_NAME);
515     if ((jsonObj) && (cJSON_Array == jsonObj->type))
516     {
517         doxm->oxmTypeLen = (size_t)cJSON_GetArraySize(jsonObj);
518         VERIFY_SUCCESS(TAG, doxm->oxmTypeLen > 0, ERROR);
519
520         doxm->oxmType = (OicUrn_t *)OICCalloc(doxm->oxmTypeLen, sizeof(char *));
521         VERIFY_NON_NULL(TAG, (doxm->oxmType), ERROR);
522
523         for (size_t i  = 0; i < doxm->oxmTypeLen ; i++)
524         {
525             cJSON *jsonOxmTy = cJSON_GetArrayItem(jsonObj, i);
526             VERIFY_NON_NULL(TAG, jsonOxmTy, ERROR);
527
528             jsonObjLen = strlen(jsonOxmTy->valuestring) + 1;
529             doxm->oxmType[i] = (char*)OICMalloc(jsonObjLen);
530             VERIFY_NON_NULL(TAG, doxm->oxmType[i], ERROR);
531             strncpy((char *)doxm->oxmType[i], (char *)jsonOxmTy->valuestring, jsonObjLen);
532         }
533     }
534
535     //Oxm -- not Mandatory
536     jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXMS_NAME);
537     if (jsonObj && cJSON_Array == jsonObj->type)
538     {
539         doxm->oxmLen = (size_t)cJSON_GetArraySize(jsonObj);
540         VERIFY_SUCCESS(TAG, doxm->oxmLen > 0, ERROR);
541
542         doxm->oxm = (OicSecOxm_t*)OICCalloc(doxm->oxmLen, sizeof(OicSecOxm_t));
543         VERIFY_NON_NULL(TAG, doxm->oxm, ERROR);
544
545         for (size_t i  = 0; i < doxm->oxmLen ; i++)
546         {
547             cJSON *jsonOxm = cJSON_GetArrayItem(jsonObj, i);
548             VERIFY_NON_NULL(TAG, jsonOxm, ERROR);
549             doxm->oxm[i] = (OicSecOxm_t)jsonOxm->valueint;
550         }
551     }
552
553     //OxmSel -- Mandatory
554     jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_SEL_NAME);
555     if (jsonObj)
556     {
557         VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
558         doxm->oxmSel = (OicSecOxm_t)jsonObj->valueint;
559     }
560
561     //sct -- Mandatory
562     jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_SUPPORTED_CRED_TYPE_NAME);
563     if (jsonObj)
564     {
565         VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
566         doxm->sct = (OicSecCredType_t)jsonObj->valueint;
567     }
568
569     //Owned -- Mandatory
570     jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OWNED_NAME);
571     if (jsonObj)
572     {
573         VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type), ERROR);
574         doxm->owned = jsonObj->valueint;
575     }
576
577     //DPC -- Mandatory
578     jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DPC_NAME);
579     if (jsonObj)
580     {
581         VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type), ERROR);
582         doxm->dpc = jsonObj->valueint;
583     }
584
585     //DeviceId -- Mandatory
586     jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVICE_ID_NAME);
587     if (jsonObj)
588     {
589         VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
590         if (cJSON_String == jsonObj->type)
591         {
592             //Check for empty string, in case DeviceId field has not been set yet
593             if (jsonObj->valuestring[0])
594             {
595                 ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->deviceID);
596                 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
597             }
598         }
599     }
600
601     //rowner -- Mandatory
602     jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_ROWNERID_NAME);
603     if (true == doxm->owned)
604     {
605         VERIFY_NON_NULL(TAG, jsonObj, ERROR);
606     }
607     if (jsonObj)
608     {
609         ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->rownerID);
610         VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
611     }
612
613     //Owner -- will be empty when device status is unowned.
614     jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVOWNERID_NAME);
615     if (true == doxm->owned)
616     {
617         VERIFY_NON_NULL(TAG, jsonObj, ERROR);
618     }
619     if (jsonObj)
620     {
621         ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->owner);
622                 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
623     }
624
625     ret = OC_STACK_OK;
626
627 exit:
628     cJSON_Delete(jsonRoot);
629     if (OC_STACK_OK != ret)
630     {
631         DeleteDoxmBinData(doxm);
632         doxm = NULL;
633     }
634     printf("OUT JSONToDoxmBin\n");
635     return doxm;
636 }
637
638 OicSecPstat_t* JSONToPstatBin(const char * jsonStr)
639 {
640     printf("IN JSONToPstatBin\n");
641     if(NULL == jsonStr)
642     {
643         return NULL;
644     }
645
646     OCStackResult ret = OC_STACK_ERROR;
647     OicSecPstat_t *pstat = NULL;
648     cJSON *jsonPstat = NULL;
649     cJSON *jsonObj = NULL;
650
651     cJSON *jsonRoot = cJSON_Parse(jsonStr);
652     VERIFY_NON_NULL(TAG, jsonRoot, INFO);
653
654     jsonPstat = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
655     VERIFY_NON_NULL(TAG, jsonPstat, INFO);
656
657     pstat = (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
658     VERIFY_NON_NULL(TAG, pstat, INFO);
659     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_ISOP_NAME);
660     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
661     VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type) , ERROR);
662     pstat->isOp = jsonObj->valueint;
663
664     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_DEVICE_ID_NAME);
665     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
666     VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
667     ret = ConvertStrToUuid(jsonObj->valuestring, &pstat->deviceID);
668     VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
669
670     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_ROWNERID_NAME);
671     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
672     VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
673     ret = ConvertStrToUuid(jsonObj->valuestring, &pstat->rownerID);
674     VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
675
676     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_CM_NAME);
677     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
678     VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
679     pstat->cm  = (OicSecDpm_t)jsonObj->valueint;
680
681     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_TM_NAME);
682     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
683     VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
684     pstat->tm  = (OicSecDpm_t)jsonObj->valueint;
685
686     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_OM_NAME);
687     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
688     VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
689     pstat->om  = (OicSecDpom_t)jsonObj->valueint;
690
691     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_SM_NAME);
692     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
693     VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
694     pstat->smLen = 1;
695     pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
696     pstat->sm[0] = (OicSecDpom_t)jsonObj->valueint;
697
698     ret = OC_STACK_OK;
699
700 exit:
701     cJSON_Delete(jsonRoot);
702     if (OC_STACK_OK != ret)
703     {
704         OIC_LOG(ERROR, TAG, "JSONToPstatBin failed");
705     }
706     printf("OUT JSONToPstatBin\n");
707     return pstat;
708 }
709
710 OicSecCred_t * JSONToCredBin(const char * jsonStr)
711 {
712
713     VERIFY_NON_NULL(TAG, jsonStr, ERROR);
714     OicSecCred_t *headCred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
715
716     OCStackResult ret = OC_STACK_ERROR;
717     cJSON *jsonRoot = NULL;
718
719     jsonRoot = cJSON_Parse(jsonStr);
720     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
721
722     cJSON *jsonCredMap = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
723     VERIFY_NON_NULL(TAG, jsonCredMap, ERROR);
724
725     // creds
726     cJSON *jsonCredArray = NULL;
727     jsonCredArray = cJSON_GetObjectItem(jsonCredMap, OIC_JSON_CREDS_NAME);
728     VERIFY_NON_NULL(TAG, jsonCredArray, ERROR);
729
730     if (cJSON_Array == jsonCredArray->type)
731     {
732         int numCred = cJSON_GetArraySize(jsonCredArray);
733         VERIFY_SUCCESS(TAG, numCred > 0, ERROR);
734         int idx = 0;
735         do
736         {
737             cJSON *jsonCred = cJSON_GetArrayItem(jsonCredArray, idx);
738             VERIFY_NON_NULL(TAG, jsonCred, ERROR);
739
740             OicSecCred_t *cred = NULL;
741             if(idx == 0)
742             {
743                 cred = headCred;
744             }
745             else
746             {
747                 cred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
748                 OicSecCred_t *temp = headCred;
749                 while (temp->next)
750                 {
751                     temp = temp->next;
752                 }
753                 temp->next = cred;
754             }
755             VERIFY_NON_NULL(TAG, cred, ERROR);
756
757             size_t jsonObjLen = 0;
758             cJSON *jsonObj = NULL;
759
760             //CredId -- Mandatory
761             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDID_NAME);
762             if(jsonObj)
763             {
764                 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
765                 cred->credId = jsonObj->valueint;
766             }
767
768             //subject -- Mandatory
769             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_SUBJECTID_NAME);
770             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
771             VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
772             ret = ConvertStrToUuid(jsonObj->valuestring, &cred->subject);
773             VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
774
775             //CredType -- Mandatory
776             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDTYPE_NAME);
777             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
778             VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
779             cred->credType = (OicSecCredType_t)jsonObj->valueint;
780             //PrivateData is mandatory for some of the credential types listed below.
781             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PRIVATEDATA_NAME);
782
783             if (NULL != jsonObj)
784             {
785                 cJSON *jsonPriv = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
786                 VERIFY_NON_NULL(TAG, jsonPriv, ERROR);
787                 jsonObjLen = strlen(jsonPriv->valuestring);
788                 cred->privateData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
789                 VERIFY_NON_NULL(TAG, (cred->privateData.data), ERROR);
790                 memcpy(cred->privateData.data, jsonPriv->valuestring, jsonObjLen);
791                 cred->privateData.len = jsonObjLen;
792             }
793 #ifdef __WITH_X509__
794             //PublicData is mandatory only for SIGNED_ASYMMETRIC_KEY credentials type.
795             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PUBLICDATA_NAME);
796
797             if (NULL != jsonObj)
798             {
799                 cJSON *jsonPub = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
800                 VERIFY_NON_NULL(TAG, jsonPub, ERROR);
801                 jsonObjLen = strlen(jsonPub->valuestring);
802                 cred->publicData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
803                 VERIFY_NON_NULL(TAG, (cred->publicData.data), ERROR);
804                 memcpy(cred->publicData.data, jsonPub->valuestring, jsonObjLen);
805                 cred->publicData.len = jsonObjLen;
806             }
807 #endif //  __WITH_X509__
808             //Period -- Not Mandatory
809             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PERIOD_NAME);
810             if(jsonObj && cJSON_String == jsonObj->type)
811             {
812                 jsonObjLen = strlen(jsonObj->valuestring) + 1;
813                 cred->period = (char *)OICMalloc(jsonObjLen);
814                 VERIFY_NON_NULL(TAG, cred->period, ERROR);
815                 strncpy(cred->period, jsonObj->valuestring, jsonObjLen);
816             }
817
818             cred->next = NULL;
819
820         } while( ++idx < numCred);
821     }
822
823     // rownerid
824     cJSON *jsonCredObj = cJSON_GetObjectItem(jsonCredMap, OIC_JSON_ROWNERID_NAME);
825     VERIFY_NON_NULL(TAG, jsonCredObj, ERROR);
826     VERIFY_SUCCESS(TAG, cJSON_String == jsonCredObj->type, ERROR);
827     headCred->ownersLen = 1;
828     VERIFY_SUCCESS(TAG, headCred->ownersLen > 0, ERROR);
829     headCred->owners = (OicUuid_t*)OICCalloc(headCred->ownersLen, sizeof(OicUuid_t));
830     VERIFY_NON_NULL(TAG, (headCred->owners), ERROR);
831     ret = ConvertStrToUuid(jsonCredObj->valuestring, &headCred->owners[0]);
832     VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
833     ret = OC_STACK_OK;
834
835 exit:
836
837     if (OC_STACK_OK != ret)
838     {
839         DeleteCredList(headCred);
840         headCred = NULL;
841     }
842     return headCred;
843 }
844
845 static OicSecSvc_t* JSONToSvcBin(const char * jsonStr)
846 {
847     OCStackResult ret = OC_STACK_ERROR;
848     OicSecSvc_t * headSvc = NULL;
849     OicSecSvc_t * prevSvc = NULL;
850     cJSON *jsonRoot = NULL;
851     cJSON *jsonSvcArray = NULL;
852
853     VERIFY_NON_NULL(TAG, jsonStr, ERROR);
854
855     jsonRoot = cJSON_Parse(jsonStr);
856     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
857
858     jsonSvcArray = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME);
859     VERIFY_NON_NULL(TAG, jsonSvcArray, INFO);
860
861     if (cJSON_Array == jsonSvcArray->type)
862     {
863         int numSvc = cJSON_GetArraySize(jsonSvcArray);
864         int idx = 0;
865
866         VERIFY_SUCCESS(TAG, numSvc > 0, INFO);
867         do
868         {
869             cJSON *jsonSvc = cJSON_GetArrayItem(jsonSvcArray, idx);
870             VERIFY_NON_NULL(TAG, jsonSvc, ERROR);
871
872             OicSecSvc_t *svc = (OicSecSvc_t*)OICCalloc(1, sizeof(OicSecSvc_t));
873             VERIFY_NON_NULL(TAG, svc, ERROR);
874
875             headSvc = (headSvc) ? headSvc : svc;
876             if (prevSvc)
877             {
878                 prevSvc->next = svc;
879             }
880
881             cJSON *jsonObj = NULL;
882             unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
883             uint32_t outLen = 0;
884             B64Result b64Ret = B64_OK;
885
886             // Service Device Identity
887             jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_DEVICE_ID);
888             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
889             VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
890             outLen = 0;
891             b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
892                        sizeof(base64Buff), &outLen);
893             VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->svcdid.id)), ERROR);
894             memcpy(svc->svcdid.id, base64Buff, outLen);
895
896             // Service Type
897             jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_TYPE);
898             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
899             VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
900             svc->svct = (OicSecSvcType_t)jsonObj->valueint;
901
902             // Resource Owners
903             jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_OWNERS_NAME);
904             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
905             VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
906
907             svc->ownersLen = (size_t)cJSON_GetArraySize(jsonObj);
908             VERIFY_SUCCESS(TAG, svc->ownersLen > 0, ERROR);
909             svc->owners = (OicUuid_t*)OICCalloc(svc->ownersLen, sizeof(OicUuid_t));
910             VERIFY_NON_NULL(TAG, (svc->owners), ERROR);
911
912             size_t idxx = 0;
913             do
914             {
915                 cJSON *jsonOwnr = cJSON_GetArrayItem(jsonObj, idxx);
916                 VERIFY_NON_NULL(TAG, jsonOwnr, ERROR);
917                 VERIFY_SUCCESS(TAG, cJSON_String == jsonOwnr->type, ERROR);
918                 outLen = 0;
919                 b64Ret = b64Decode(jsonOwnr->valuestring, strlen(jsonOwnr->valuestring), base64Buff,
920                            sizeof(base64Buff), &outLen);
921
922                 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->owners[idxx].id)),
923                                    ERROR);
924                 memcpy(svc->owners[idxx].id, base64Buff, outLen);
925             } while ( ++idxx < svc->ownersLen);
926
927             prevSvc = svc;
928         } while( ++idx < numSvc);
929     }
930
931     ret = OC_STACK_OK;
932
933 exit:
934     cJSON_Delete(jsonRoot);
935     if (OC_STACK_OK != ret)
936     {
937         DeleteSVCList(headSvc);
938         headSvc = NULL;
939     }
940     return headSvc;
941 }
942
943 static OicSecAmacl_t* JSONToAmaclBin(const char * jsonStr)
944 {
945     OCStackResult ret = OC_STACK_ERROR;
946     OicSecAmacl_t * headAmacl = (OicSecAmacl_t*)OICCalloc(1, sizeof(OicSecAmacl_t));
947
948     cJSON *jsonRoot = NULL;
949     cJSON *jsonAmacl = NULL;
950
951     VERIFY_NON_NULL(TAG, jsonStr, ERROR);
952
953     jsonRoot = cJSON_Parse(jsonStr);
954     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
955
956     jsonAmacl = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
957     VERIFY_NON_NULL(TAG, jsonAmacl, INFO);
958
959     cJSON *jsonObj = NULL;
960
961     // Resources -- Mandatory
962     jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_RESOURCES_NAME);
963     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
964
965     // Rlist
966     cJSON *jsonRlistArray = cJSON_GetObjectItem(jsonObj, OIC_JSON_RLIST_NAME);
967     VERIFY_NON_NULL(TAG, jsonRlistArray, ERROR);
968     VERIFY_SUCCESS(TAG, cJSON_Array == jsonRlistArray->type, ERROR);
969
970     headAmacl->resourcesLen = (size_t)cJSON_GetArraySize(jsonRlistArray);
971     headAmacl->resources = (char**)OICCalloc(headAmacl->resourcesLen, sizeof(char*));
972     size_t idxx = 0;
973     do
974     {
975         cJSON *jsonRsrc = cJSON_GetArrayItem(jsonRlistArray, idxx);
976         VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
977
978         cJSON *jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_HREF_NAME);
979         VERIFY_NON_NULL(TAG, jsonRsrcObj, ERROR);
980         VERIFY_SUCCESS(TAG, cJSON_String == jsonRsrcObj->type, ERROR);
981
982         size_t jsonRsrcObjLen = 0;
983         jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
984         headAmacl->resources[idxx] = (char*)OICMalloc(jsonRsrcObjLen);
985         VERIFY_NON_NULL(TAG, (headAmacl->resources[idxx]), ERROR);
986         OICStrcpy(headAmacl->resources[idxx], jsonRsrcObjLen, jsonRsrcObj->valuestring);
987
988     } while ( ++idxx < headAmacl->resourcesLen);
989
990     // Ams -- Mandatory
991     jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_AMS_NAME);
992     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
993     VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
994
995     headAmacl->amssLen = (size_t)cJSON_GetArraySize(jsonObj);
996     VERIFY_SUCCESS(TAG, headAmacl->amssLen > 0, ERROR);
997     headAmacl->amss = (OicUuid_t*)OICCalloc(headAmacl->amssLen, sizeof(OicUuid_t));
998     VERIFY_NON_NULL(TAG, headAmacl->amss, ERROR);
999
1000     idxx = 0;
1001     do
1002     {
1003         cJSON *jsonAms = cJSON_GetArrayItem(jsonObj, idxx);
1004         VERIFY_NON_NULL(TAG, jsonAms, ERROR);
1005         VERIFY_SUCCESS(TAG, cJSON_String == jsonAms->type, ERROR);
1006
1007         memcpy(headAmacl->amss[idxx].id, (OicUuid_t *)jsonAms->valuestring, strlen(jsonAms->valuestring));
1008
1009     } while ( ++idxx < headAmacl->amssLen);
1010
1011
1012     // Rowner -- Mandatory
1013     jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_ROWNERID_NAME);
1014     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1015     VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
1016
1017     headAmacl->ownersLen = 1;
1018     VERIFY_SUCCESS(TAG, headAmacl->ownersLen > 0, ERROR);
1019     headAmacl->owners = (OicUuid_t*)OICCalloc(headAmacl->ownersLen, sizeof(OicUuid_t));
1020     VERIFY_NON_NULL(TAG, (headAmacl->owners), ERROR);
1021
1022     ret = ConvertStrToUuid(jsonObj->valuestring, &headAmacl->owners[0]);
1023     VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
1024
1025     ret = OC_STACK_OK;
1026
1027 exit:
1028     cJSON_Delete(jsonRoot);
1029     if (OC_STACK_OK != ret)
1030     {
1031         DeleteAmaclList(headAmacl);
1032         headAmacl = NULL;
1033     }
1034     return headAmacl;
1035 }
1036
1037 int main(int argc, char* argv[])
1038 {
1039     if (argc == 3)
1040     {
1041         printf("JSON File Name: %s\n CBOR File Name: %s \n", argv[1], argv[2]);
1042         ConvertJsonToCBOR(argv[1], argv[2]);
1043     }
1044     else
1045     {
1046         printf("This program requires two inputs:\n");
1047         printf("1. First input is a json file tha will be converted to cbor. \n");
1048         printf("2. Second input is a resulting cbor file that will store converted cbor. \n");
1049         printf("\t json2cbor <json_file_name> <cbor_file_name>. \n");
1050     }
1051 }