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