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