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