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