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