Imported Upstream version 1.1.0
[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 (bytesRead >=(MAX_RANGE - size))
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     jsonStr = (char *)OICMalloc(size + 1);
99     VERIFY_NON_NULL(TAG, jsonStr, FATAL);
100
101     fp = fopen(jsonFileName, "r");
102     if (fp)
103     {
104         size_t bytesRead = fread(jsonStr, 1, size, fp);
105         jsonStr[bytesRead] = '\0';
106
107         OIC_LOG_V(DEBUG, TAG, "Read %zu bytes", bytesRead);
108         fclose(fp);
109         fp = NULL;
110     }
111     else
112     {
113         OIC_LOG (ERROR, TAG, "Unable to open JSON file!!");
114         goto exit;
115     }
116
117     jsonRoot = cJSON_Parse(jsonStr);
118
119     cJSON *value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_ACL_NAME);
120     //printf("ACL json : \n%s\n", cJSON_PrintUnformatted(value));
121     size_t aclCborSize = 0;
122     if (NULL != value)
123     {
124         OicSecAcl_t *acl = JSONToAclBin(jsonStr);
125         VERIFY_NON_NULL(TAG, acl, FATAL);
126         ret = AclToCBORPayload(acl, &aclCbor, &aclCborSize);
127         if(OC_STACK_OK != ret)
128         {
129             OIC_LOG (ERROR, TAG, "Failed converting Acl to Cbor Payload");
130             DeleteACLList(acl);
131             goto exit;
132         }
133         printf("ACL Cbor Size: %zd\n", aclCborSize);
134         DeleteACLList(acl);
135     }
136
137     value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
138     size_t pstatCborSize = 0;
139     if (NULL != value)
140     {
141         OicSecPstat_t *pstat = JSONToPstatBin(jsonStr);
142         VERIFY_NON_NULL(TAG, pstat, FATAL);
143         ret = PstatToCBORPayload(pstat, &pstatCbor, &pstatCborSize);
144         if(OC_STACK_OK != ret)
145         {
146             OIC_LOG (ERROR, TAG, "Failed converting Pstat to Cbor Payload");
147             DeletePstatBinData(pstat);
148             goto exit;
149         }
150         printf("PSTAT Cbor Size: %zd\n", pstatCborSize);
151         DeletePstatBinData(pstat);
152     }
153     value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME);
154     size_t doxmCborSize = 0;
155     if (NULL != value)
156     {
157         OicSecDoxm_t *doxm = JSONToDoxmBin(jsonStr);
158         VERIFY_NON_NULL(TAG, doxm, FATAL);
159         ret = DoxmToCBORPayload(doxm, &doxmCbor, &doxmCborSize);
160         if(OC_STACK_OK != ret)
161         {
162             OIC_LOG (ERROR, TAG, "Failed converting Doxm to Cbor Payload");
163             DeleteDoxmBinData(doxm);
164             goto exit;
165         }
166         printf("DOXM Cbor Size: %zd\n", doxmCborSize);
167         DeleteDoxmBinData(doxm);
168     }
169     value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
170     size_t amaclCborSize = 0;
171     if (NULL != value)
172     {
173         OicSecAmacl_t *amacl = JSONToAmaclBin(jsonStr);
174         VERIFY_NON_NULL(TAG, amacl, FATAL);
175         ret = AmaclToCBORPayload(amacl, &amaclCbor, &amaclCborSize);
176         if(OC_STACK_OK != ret)
177         {
178             OIC_LOG (ERROR, TAG, "Failed converting Amacl to Cbor Payload");
179             DeleteAmaclList(amacl);
180             goto exit;
181         }
182         printf("AMACL Cbor Size: %zd\n", amaclCborSize);
183         DeleteAmaclList(amacl);
184     }
185     value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME);
186     size_t svcCborSize = 0;
187     if (NULL != value)
188     {
189         OicSecSvc_t *svc = JSONToSvcBin(jsonStr);
190         VERIFY_NON_NULL(TAG, svc, FATAL);
191         ret = SVCToCBORPayload(svc, &svcCbor, &svcCborSize);
192         if(OC_STACK_OK != ret)
193         {
194             OIC_LOG (ERROR, TAG, "Failed converting Svc to Cbor Payload");
195             DeleteSVCList(svc);
196             goto exit;
197         }
198         printf("SVC Cbor Size: %zd\n", svcCborSize);
199         DeleteSVCList(svc);
200     }
201     value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
202     //printf("CRED json : \n%s\n", cJSON_PrintUnformatted(value));
203     size_t credCborSize = 0;
204     if (NULL != value)
205     {
206         OicSecCred_t *cred = JSONToCredBin(jsonStr);
207         VERIFY_NON_NULL(TAG, cred, FATAL);
208         ret = CredToCBORPayload(cred, &credCbor, &credCborSize);
209         if(OC_STACK_OK != ret)
210         {
211             OIC_LOG (ERROR, TAG, "Failed converting Cred to Cbor Payload");
212             DeleteCredList(cred);
213             goto exit;
214         }
215         printf("CRED Cbor Size: %zd\n", credCborSize);
216         DeleteCredList(cred);
217     }
218
219     CborEncoder encoder;
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;
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         if (bytesWritten == s)
285         {
286             OIC_LOG_V(DEBUG, TAG, "Written %zu bytes", bytesWritten);
287         }
288         else
289         {
290             OIC_LOG_V(ERROR, TAG, "Failed writing %zu bytes", s);
291         }
292         fclose(fp1);
293         fp1 = NULL;
294     }
295 exit:
296
297     cJSON_Delete(jsonRoot);
298     OICFree(aclCbor);
299     OICFree(doxmCbor);
300     OICFree(pstatCbor);
301     OICFree(amaclCbor);
302     OICFree(svcCbor);
303     OICFree(credCbor);
304     OICFree(jsonStr);
305     return ;
306 }
307
308 OicSecAcl_t* JSONToAclBin(const char * jsonStr)
309 {
310     OCStackResult ret = OC_STACK_ERROR;
311     OicSecAcl_t * headAcl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
312     cJSON *jsonRoot = NULL;
313
314     VERIFY_NON_NULL(TAG, jsonStr, ERROR);
315
316     jsonRoot = cJSON_Parse(jsonStr);
317     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
318
319     cJSON *jsonAclMap = cJSON_GetObjectItem(jsonRoot, OIC_JSON_ACL_NAME);
320     VERIFY_NON_NULL(TAG, jsonAclMap, ERROR);
321
322     cJSON *jsonAclObj = NULL;
323
324     // aclist
325     jsonAclObj = cJSON_GetObjectItem(jsonAclMap, OIC_JSON_ACLIST_NAME);
326     VERIFY_NON_NULL(TAG, jsonAclObj, ERROR);
327
328     // aclist-aces
329     cJSON *jsonAclArray = NULL;
330     jsonAclArray = cJSON_GetObjectItem(jsonAclObj, OIC_JSON_ACES_NAME);
331     VERIFY_NON_NULL(TAG, jsonAclArray, ERROR);
332
333     if (cJSON_Array == jsonAclArray->type)
334     {
335
336         int numAcl = cJSON_GetArraySize(jsonAclArray);
337         int idx = 0;
338
339         VERIFY_SUCCESS(TAG, numAcl > 0, INFO);
340         do
341         {
342             cJSON *jsonAcl = cJSON_GetArrayItem(jsonAclArray, idx);
343             VERIFY_NON_NULL(TAG, jsonAcl, ERROR);
344
345             OicSecAcl_t *acl = NULL;
346             if(idx == 0)
347             {
348                 acl = headAcl;
349             }
350             else
351             {
352                 acl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
353                 OicSecAcl_t *temp = headAcl;
354                 while (temp->next)
355                 {
356                     temp = temp->next;
357                 }
358                 temp->next = acl;
359             }
360
361             VERIFY_NON_NULL(TAG, acl, ERROR);
362
363             size_t jsonObjLen = 0;
364             cJSON *jsonObj = NULL;
365             jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_SUBJECTID_NAME);
366             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
367             VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
368             if(strcmp(jsonObj->valuestring, WILDCARD_RESOURCE_URI) == 0)
369             {
370                 acl->subject.id[0] = '*';
371             }
372             else
373             {
374                 ret = ConvertStrToUuid(jsonObj->valuestring, &acl->subject);
375                 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
376             }
377             // Resources -- Mandatory
378             jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_RESOURCES_NAME);
379             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
380             VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
381
382             acl->resourcesLen = (size_t)cJSON_GetArraySize(jsonObj);
383
384             VERIFY_SUCCESS(TAG, acl->resourcesLen > 0, ERROR);
385             acl->resources = (char**)OICCalloc(acl->resourcesLen, sizeof(char*));
386             VERIFY_NON_NULL(TAG, (acl->resources), ERROR);
387
388             size_t idxx = 0;
389             do
390             {
391                 cJSON *jsonRsrc = cJSON_GetArrayItem(jsonObj, idxx);
392                 VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
393
394                 size_t jsonRsrcObjLen = 0;
395                 cJSON *jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_HREF_NAME);
396                 VERIFY_NON_NULL(TAG, jsonRsrcObj, ERROR);
397                 VERIFY_SUCCESS(TAG, cJSON_String == jsonRsrcObj->type, ERROR);
398
399                 jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
400                 acl->resources[idxx] = (char*)OICMalloc(jsonRsrcObjLen);
401
402                 VERIFY_NON_NULL(TAG, (acl->resources[idxx]), ERROR);
403                 OICStrcpy(acl->resources[idxx], jsonRsrcObjLen, jsonRsrcObj->valuestring);
404
405             } while ( ++idxx < acl->resourcesLen);
406
407             // Permissions -- Mandatory
408             jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_PERMISSION_NAME);
409             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
410             VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
411             acl->permission = jsonObj->valueint;
412             //Period -- Not Mandatory
413             cJSON *jsonPeriodObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_PERIOD_NAME);
414             if(jsonPeriodObj)
415             {
416                 VERIFY_SUCCESS(TAG, cJSON_Array == jsonPeriodObj->type, ERROR);
417                 acl->prdRecrLen = (size_t)cJSON_GetArraySize(jsonPeriodObj);
418                 if(acl->prdRecrLen > 0)
419                 {
420                     acl->periods = (char**)OICCalloc(acl->prdRecrLen, sizeof(char*));
421                     VERIFY_NON_NULL(TAG, acl->periods, ERROR);
422
423                     cJSON *jsonPeriod = NULL;
424                     for(size_t i = 0; i < acl->prdRecrLen; i++)
425                     {
426                         jsonPeriod = cJSON_GetArrayItem(jsonPeriodObj, i);
427                         VERIFY_NON_NULL(TAG, jsonPeriod, ERROR);
428
429                         jsonObjLen = strlen(jsonPeriod->valuestring) + 1;
430                         acl->periods[i] = (char*)OICMalloc(jsonObjLen);
431                         VERIFY_NON_NULL(TAG, acl->periods[i], ERROR);
432                         OICStrcpy(acl->periods[i], jsonObjLen, jsonPeriod->valuestring);
433                     }
434                 }
435             }
436             //Recurrence -- Not mandatory
437             cJSON *jsonRecurObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_RECURRENCES_NAME);
438             if(jsonRecurObj)
439             {
440
441                 VERIFY_SUCCESS(TAG, cJSON_Array == jsonRecurObj->type, ERROR);
442
443                 if(acl->prdRecrLen > 0)
444                 {
445                     acl->recurrences = (char**)OICCalloc(acl->prdRecrLen, sizeof(char*));
446                     VERIFY_NON_NULL(TAG, acl->recurrences, ERROR);
447
448                     cJSON *jsonRecur = NULL;
449                     for(size_t i = 0; i < acl->prdRecrLen; i++)
450                     {
451                         jsonRecur = cJSON_GetArrayItem(jsonRecurObj, i);
452                         VERIFY_NON_NULL(TAG, jsonRecur, ERROR);
453                         jsonObjLen = strlen(jsonRecur->valuestring) + 1;
454                         acl->recurrences[i] = (char*)OICMalloc(jsonObjLen);
455                         VERIFY_NON_NULL(TAG, acl->recurrences[i], ERROR);
456                         OICStrcpy(acl->recurrences[i], jsonObjLen, jsonRecur->valuestring);
457                     }
458                 }
459             }
460
461             acl->next = NULL;
462
463         } while( ++idx < numAcl);
464     }
465
466
467     // rownerid
468     jsonAclObj = cJSON_GetObjectItem(jsonAclMap, OIC_JSON_ROWNERID_NAME);
469     VERIFY_NON_NULL(TAG, jsonAclObj, ERROR);
470     VERIFY_SUCCESS(TAG, cJSON_String == jsonAclObj->type, ERROR);
471     ret = ConvertStrToUuid(jsonAclObj->valuestring, &headAcl->rownerID);
472     VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
473
474     ret = OC_STACK_OK;
475
476 exit:
477     cJSON_Delete(jsonRoot);
478     if (OC_STACK_OK != ret)
479     {
480         DeleteACLList(headAcl);
481         headAcl = NULL;
482     }
483     return headAcl;
484 }
485
486 OicSecDoxm_t* JSONToDoxmBin(const char * jsonStr)
487 {
488     printf("IN JSONToDoxmBin\n");
489     if (NULL == jsonStr)
490     {
491         return NULL;
492     }
493
494     OCStackResult ret = OC_STACK_ERROR;
495     OicSecDoxm_t *doxm =  NULL;
496     cJSON *jsonDoxm = NULL;
497     cJSON *jsonObj = NULL;
498
499     size_t jsonObjLen = 0;
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     cJSON *jsonRoot = cJSON_Parse(jsonStr);
649     VERIFY_NON_NULL(TAG, jsonRoot, INFO);
650
651     jsonPstat = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
652     VERIFY_NON_NULL(TAG, jsonPstat, INFO);
653
654     pstat = (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
655     VERIFY_NON_NULL(TAG, pstat, INFO);
656     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_ISOP_NAME);
657     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
658     VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type) , ERROR);
659     pstat->isOp = jsonObj->valueint;
660
661     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_DEVICE_ID_NAME);
662     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
663     VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
664     ret = ConvertStrToUuid(jsonObj->valuestring, &pstat->deviceID);
665     VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
666
667     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_ROWNERID_NAME);
668     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
669     VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
670     ret = ConvertStrToUuid(jsonObj->valuestring, &pstat->rownerID);
671     VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
672
673     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_CM_NAME);
674     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
675     VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
676     pstat->cm  = (OicSecDpm_t)jsonObj->valueint;
677
678     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_TM_NAME);
679     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
680     VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
681     pstat->tm  = (OicSecDpm_t)jsonObj->valueint;
682
683     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_OM_NAME);
684     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
685     VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
686     pstat->om  = (OicSecDpom_t)jsonObj->valueint;
687
688     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_SM_NAME);
689     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
690     VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
691     pstat->smLen = 1;
692     pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
693     pstat->sm[0] = (OicSecDpom_t)jsonObj->valueint;
694
695     ret = OC_STACK_OK;
696
697 exit:
698     cJSON_Delete(jsonRoot);
699     if (OC_STACK_OK != ret)
700     {
701         OIC_LOG(ERROR, TAG, "JSONToPstatBin failed");
702     }
703     printf("OUT JSONToPstatBin\n");
704     return pstat;
705 }
706
707 OicSecCred_t * JSONToCredBin(const char * jsonStr)
708 {
709     if (NULL == jsonStr)
710     {
711         OIC_LOG(ERROR, TAG,"JSONToCredBin jsonStr in NULL");
712         return NULL;
713     }
714
715     OicSecCred_t *headCred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
716     OCStackResult ret = OC_STACK_ERROR;
717     cJSON *jsonRoot = NULL;
718     VERIFY_NON_NULL(TAG, headCred, ERROR);
719
720     jsonRoot = cJSON_Parse(jsonStr);
721     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
722
723     cJSON *jsonCredMap = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
724     VERIFY_NON_NULL(TAG, jsonCredMap, ERROR);
725
726     // creds
727     cJSON *jsonCredArray = NULL;
728     jsonCredArray = cJSON_GetObjectItem(jsonCredMap, OIC_JSON_CREDS_NAME);
729     VERIFY_NON_NULL(TAG, jsonCredArray, ERROR);
730
731     if (cJSON_Array == jsonCredArray->type)
732     {
733         int numCred = cJSON_GetArraySize(jsonCredArray);
734         VERIFY_SUCCESS(TAG, numCred > 0, ERROR);
735         int idx = 0;
736         size_t ownersLen = 0;
737         do
738         {
739             cJSON *jsonCred = cJSON_GetArrayItem(jsonCredArray, idx);
740             VERIFY_NON_NULL(TAG, jsonCred, ERROR);
741
742             OicSecCred_t *cred = NULL;
743             if(idx == 0)
744             {
745                 cred = headCred;
746             }
747             else
748             {
749                 cred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
750                 OicSecCred_t *temp = headCred;
751                 while (temp->next)
752                 {
753                     temp = temp->next;
754                 }
755                 temp->next = cred;
756             }
757             VERIFY_NON_NULL(TAG, cred, ERROR);
758
759             size_t jsonObjLen = 0;
760             cJSON *jsonObj = NULL;
761
762             //CredId -- Mandatory
763             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDID_NAME);
764             if(jsonObj)
765             {
766                 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
767                 cred->credId = jsonObj->valueint;
768             }
769
770             //subject -- Mandatory
771             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_SUBJECTID_NAME);
772             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
773             VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
774             ret = ConvertStrToUuid(jsonObj->valuestring, &cred->subject);
775             VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
776
777             //CredType -- Mandatory
778             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDTYPE_NAME);
779             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
780             VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
781             cred->credType = (OicSecCredType_t)jsonObj->valueint;
782             //PrivateData is mandatory for some of the credential types listed below.
783             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PRIVATEDATA_NAME);
784
785             if (NULL != jsonObj)
786             {
787                 cJSON *jsonPriv = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
788                 VERIFY_NON_NULL(TAG, jsonPriv, ERROR);
789                 jsonObjLen = strlen(jsonPriv->valuestring);
790                 cred->privateData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
791                 VERIFY_NON_NULL(TAG, (cred->privateData.data), ERROR);
792                 memcpy(cred->privateData.data, jsonPriv->valuestring, jsonObjLen);
793                 cred->privateData.len = jsonObjLen;
794             }
795 #ifdef __WITH_X509__
796             //PublicData is mandatory only for SIGNED_ASYMMETRIC_KEY credentials type.
797             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PUBLICDATA_NAME);
798
799             if (NULL != jsonObj)
800             {
801                 cJSON *jsonPub = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
802                 VERIFY_NON_NULL(TAG, jsonPub, ERROR);
803                 jsonObjLen = strlen(jsonPub->valuestring);
804                 cred->publicData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
805                 VERIFY_NON_NULL(TAG, (cred->publicData.data), ERROR);
806                 memcpy(cred->publicData.data, jsonPub->valuestring, jsonObjLen);
807                 cred->publicData.len = jsonObjLen;
808             }
809 #endif //  __WITH_X509__
810             //Period -- Not Mandatory
811             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PERIOD_NAME);
812             if(jsonObj && cJSON_String == jsonObj->type)
813             {
814                 jsonObjLen = strlen(jsonObj->valuestring) + 1;
815                 cred->period = (char *)OICMalloc(jsonObjLen);
816                 VERIFY_NON_NULL(TAG, cred->period, ERROR);
817                 strncpy(cred->period, jsonObj->valuestring, jsonObjLen);
818             }
819             cred->next = NULL;
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     ret = ConvertStrToUuid(jsonCredObj->valuestring, &headCred->rownerID);
828     VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
829     ret = OC_STACK_OK;
830
831 exit:
832
833     if (OC_STACK_OK != ret)
834     {
835         DeleteCredList(headCred);
836         headCred = NULL;
837     }
838     return headCred;
839 }
840
841 static OicSecSvc_t* JSONToSvcBin(const char * jsonStr)
842 {
843     OCStackResult ret = OC_STACK_ERROR;
844     OicSecSvc_t * headSvc = NULL;
845     OicSecSvc_t * prevSvc = NULL;
846     cJSON *jsonRoot = NULL;
847     cJSON *jsonSvcArray = NULL;
848
849     VERIFY_NON_NULL(TAG, jsonStr, ERROR);
850
851     jsonRoot = cJSON_Parse(jsonStr);
852     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
853
854     jsonSvcArray = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME);
855     VERIFY_NON_NULL(TAG, jsonSvcArray, INFO);
856
857     if (cJSON_Array == jsonSvcArray->type)
858     {
859         int numSvc = cJSON_GetArraySize(jsonSvcArray);
860         int idx = 0;
861
862         VERIFY_SUCCESS(TAG, numSvc > 0, INFO);
863         do
864         {
865             cJSON *jsonSvc = cJSON_GetArrayItem(jsonSvcArray, idx);
866             VERIFY_NON_NULL(TAG, jsonSvc, ERROR);
867
868             OicSecSvc_t *svc = (OicSecSvc_t*)OICCalloc(1, sizeof(OicSecSvc_t));
869             VERIFY_NON_NULL(TAG, svc, ERROR);
870
871             headSvc = (headSvc) ? headSvc : svc;
872             if (prevSvc)
873             {
874                 prevSvc->next = svc;
875             }
876
877             cJSON *jsonObj = NULL;
878             unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
879             uint32_t outLen = 0;
880             B64Result b64Ret = B64_OK;
881
882             // Service Device Identity
883             jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_DEVICE_ID);
884             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
885             VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
886             outLen = 0;
887             b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
888                        sizeof(base64Buff), &outLen);
889             VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->svcdid.id)), ERROR);
890             memcpy(svc->svcdid.id, base64Buff, outLen);
891
892             // Service Type
893             jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_TYPE);
894             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
895             VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
896             svc->svct = (OicSecSvcType_t)jsonObj->valueint;
897
898             // Resource Owners
899             jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_OWNERS_NAME);
900             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
901             VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
902
903             svc->ownersLen = (size_t)cJSON_GetArraySize(jsonObj);
904             VERIFY_SUCCESS(TAG, svc->ownersLen > 0, ERROR);
905             svc->owners = (OicUuid_t*)OICCalloc(svc->ownersLen, sizeof(OicUuid_t));
906             VERIFY_NON_NULL(TAG, (svc->owners), ERROR);
907
908             size_t idxx = 0;
909             do
910             {
911                 cJSON *jsonOwnr = cJSON_GetArrayItem(jsonObj, idxx);
912                 VERIFY_NON_NULL(TAG, jsonOwnr, ERROR);
913                 VERIFY_SUCCESS(TAG, cJSON_String == jsonOwnr->type, ERROR);
914                 outLen = 0;
915                 b64Ret = b64Decode(jsonOwnr->valuestring, strlen(jsonOwnr->valuestring), base64Buff,
916                            sizeof(base64Buff), &outLen);
917
918                 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->owners[idxx].id)),
919                                    ERROR);
920                 memcpy(svc->owners[idxx].id, base64Buff, outLen);
921             } while ( ++idxx < svc->ownersLen);
922
923             prevSvc = svc;
924         } while( ++idx < numSvc);
925     }
926
927     ret = OC_STACK_OK;
928
929 exit:
930     cJSON_Delete(jsonRoot);
931     if (OC_STACK_OK != ret)
932     {
933         DeleteSVCList(headSvc);
934         headSvc = NULL;
935     }
936     return headSvc;
937 }
938
939 static OicSecAmacl_t* JSONToAmaclBin(const char * jsonStr)
940 {
941     OCStackResult ret = OC_STACK_ERROR;
942     OicSecAmacl_t * headAmacl = (OicSecAmacl_t*)OICCalloc(1, sizeof(OicSecAmacl_t));
943
944     cJSON *jsonRoot = NULL;
945     cJSON *jsonAmacl = NULL;
946
947     VERIFY_NON_NULL(TAG, jsonStr, ERROR);
948
949     jsonRoot = cJSON_Parse(jsonStr);
950     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
951
952     jsonAmacl = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
953     VERIFY_NON_NULL(TAG, jsonAmacl, INFO);
954
955     cJSON *jsonObj = NULL;
956
957     // Resources -- Mandatory
958     jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_RESOURCES_NAME);
959     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
960
961     // Rlist
962     cJSON *jsonRlistArray = cJSON_GetObjectItem(jsonObj, OIC_JSON_RLIST_NAME);
963     VERIFY_NON_NULL(TAG, jsonRlistArray, ERROR);
964     VERIFY_SUCCESS(TAG, cJSON_Array == jsonRlistArray->type, ERROR);
965
966     headAmacl->resourcesLen = (size_t)cJSON_GetArraySize(jsonRlistArray);
967     headAmacl->resources = (char**)OICCalloc(headAmacl->resourcesLen, sizeof(char*));
968     size_t idxx = 0;
969     do
970     {
971         cJSON *jsonRsrc = cJSON_GetArrayItem(jsonRlistArray, idxx);
972         VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
973
974         cJSON *jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_HREF_NAME);
975         VERIFY_NON_NULL(TAG, jsonRsrcObj, ERROR);
976         VERIFY_SUCCESS(TAG, cJSON_String == jsonRsrcObj->type, ERROR);
977
978         size_t jsonRsrcObjLen = 0;
979         jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
980         headAmacl->resources[idxx] = (char*)OICMalloc(jsonRsrcObjLen);
981         VERIFY_NON_NULL(TAG, (headAmacl->resources[idxx]), ERROR);
982         OICStrcpy(headAmacl->resources[idxx], jsonRsrcObjLen, jsonRsrcObj->valuestring);
983
984     } while ( ++idxx < headAmacl->resourcesLen);
985
986     // Ams -- Mandatory
987     jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_AMS_NAME);
988     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
989     VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
990
991     headAmacl->amssLen = (size_t)cJSON_GetArraySize(jsonObj);
992     VERIFY_SUCCESS(TAG, headAmacl->amssLen > 0, ERROR);
993     headAmacl->amss = (OicUuid_t*)OICCalloc(headAmacl->amssLen, sizeof(OicUuid_t));
994     VERIFY_NON_NULL(TAG, headAmacl->amss, ERROR);
995
996     idxx = 0;
997     do
998     {
999         cJSON *jsonAms = cJSON_GetArrayItem(jsonObj, idxx);
1000         VERIFY_NON_NULL(TAG, jsonAms, ERROR);
1001         VERIFY_SUCCESS(TAG, cJSON_String == jsonAms->type, ERROR);
1002
1003         memcpy(headAmacl->amss[idxx].id, (OicUuid_t *)jsonAms->valuestring, strlen(jsonAms->valuestring));
1004
1005     } while ( ++idxx < headAmacl->amssLen);
1006
1007
1008     // Rowner -- Mandatory
1009     jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_ROWNERID_NAME);
1010     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1011     VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
1012
1013     ret = ConvertStrToUuid(jsonObj->valuestring, &headAmacl->rownerID);
1014     VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
1015
1016     ret = OC_STACK_OK;
1017
1018 exit:
1019     cJSON_Delete(jsonRoot);
1020     if (OC_STACK_OK != ret)
1021     {
1022         DeleteAmaclList(headAmacl);
1023         headAmacl = NULL;
1024     }
1025     return headAmacl;
1026 }
1027
1028 int main(int argc, char* argv[])
1029 {
1030     if (argc == 3)
1031     {
1032         printf("JSON File Name: %s\n CBOR File Name: %s \n", argv[1], argv[2]);
1033         ConvertJsonToCBOR(argv[1], argv[2]);
1034     }
1035     else
1036     {
1037         printf("This program requires two inputs:\n");
1038         printf("1. First input is a json file tha will be converted to cbor. \n");
1039         printf("2. Second input is a resulting cbor file that will store converted cbor. \n");
1040         printf("\t json2cbor <json_file_name> <cbor_file_name>. \n");
1041     }
1042 }