[IOT-1719] Restore empty UUID string conversion
[platform/upstream/iotivity.git] / resource / csdk / security / tool / json2cbor.c
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include "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 %s: %s\n", __func__, (doxm != NULL) ? "success" : "failure");
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         DeletePstatBinData(pstat);
744         pstat = NULL;
745     }
746     printf("OUT %s: %s\n", __func__, (pstat != NULL) ? "success" : "failure");
747     return pstat;
748 }
749
750 static OicEncodingType_t GetEncodingTypeFromStr(const char* encodingType)
751 {
752     if (strcmp(OIC_SEC_ENCODING_RAW, encodingType) == 0)
753     {
754         return OIC_ENCODING_RAW;
755     }
756     if (strcmp(OIC_SEC_ENCODING_BASE64, encodingType) == 0)
757     {
758         return OIC_ENCODING_BASE64;
759     }
760     if (strcmp(OIC_SEC_ENCODING_PEM, encodingType) == 0)
761     {
762         return OIC_ENCODING_PEM;
763     }
764     if (strcmp(OIC_SEC_ENCODING_DER, encodingType) == 0)
765     {
766         return OIC_ENCODING_DER;
767     }
768     OIC_LOG(WARNING, TAG, "Unknow encoding type dectected!");
769     OIC_LOG(WARNING, TAG, "json2cbor will use \"oic.sec.encoding.raw\" as default encoding type.");
770     return OIC_ENCODING_RAW;
771 }
772
773 OicSecCred_t * JSONToCredBin(const char * jsonStr)
774 {
775     if (NULL == jsonStr)
776     {
777         OIC_LOG(ERROR, TAG,"JSONToCredBin jsonStr in NULL");
778         return NULL;
779     }
780
781     OicSecCred_t *headCred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
782     OCStackResult ret = OC_STACK_ERROR;
783     cJSON *jsonRoot = NULL;
784     VERIFY_NON_NULL(TAG, headCred, ERROR);
785
786     jsonRoot = cJSON_Parse(jsonStr);
787     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
788
789     cJSON *jsonCredMap = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
790     VERIFY_NON_NULL(TAG, jsonCredMap, ERROR);
791
792     // creds
793     cJSON *jsonCredArray = NULL;
794     jsonCredArray = cJSON_GetObjectItem(jsonCredMap, OIC_JSON_CREDS_NAME);
795     VERIFY_NON_NULL(TAG, jsonCredArray, ERROR);
796
797     if (cJSON_Array == jsonCredArray->type)
798     {
799         int numCred = cJSON_GetArraySize(jsonCredArray);
800         VERIFY_SUCCESS(TAG, numCred > 0, ERROR);
801         int idx = 0;
802         do
803         {
804             cJSON *jsonCred = cJSON_GetArrayItem(jsonCredArray, idx);
805             VERIFY_NON_NULL(TAG, jsonCred, ERROR);
806
807             OicSecCred_t *cred = NULL;
808             if(idx == 0)
809             {
810                 cred = headCred;
811             }
812             else
813             {
814                 cred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
815                 OicSecCred_t *temp = headCred;
816                 while (temp->next)
817                 {
818                     temp = temp->next;
819                 }
820                 temp->next = cred;
821             }
822             VERIFY_NON_NULL(TAG, cred, ERROR);
823
824             size_t jsonObjLen = 0;
825             cJSON *jsonObj = NULL;
826
827             //CredId -- Mandatory
828             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDID_NAME);
829             if(jsonObj)
830             {
831                 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
832                 cred->credId = jsonObj->valueint;
833             }
834
835             //subject -- Mandatory
836             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_SUBJECTID_NAME);
837             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
838             VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
839             if(strcmp(jsonObj->valuestring, WILDCARD_RESOURCE_URI) == 0)
840             {
841                 cred->subject.id[0] = '*';
842             }
843             else
844             {
845                 ret = ConvertStrToUuid(jsonObj->valuestring, &cred->subject);
846                 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
847             }
848
849             //CredType -- Mandatory
850             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDTYPE_NAME);
851             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
852             VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
853             cred->credType = (OicSecCredType_t)jsonObj->valueint;
854             //PrivateData is mandatory for some of the credential types listed below.
855             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PRIVATEDATA_NAME);
856
857             if (NULL != jsonObj)
858             {
859                 cJSON *jsonPriv = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
860                 VERIFY_NON_NULL(TAG, jsonPriv, ERROR);
861                 jsonObjLen = strlen(jsonPriv->valuestring);
862                 cred->privateData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
863                 VERIFY_NON_NULL(TAG, (cred->privateData.data), ERROR);
864                 memcpy(cred->privateData.data, jsonPriv->valuestring, jsonObjLen);
865                 cred->privateData.len = jsonObjLen;
866
867                 cJSON *jsonEncoding = cJSON_GetObjectItem(jsonObj, OIC_JSON_ENCODING_NAME);
868                 VERIFY_NON_NULL(TAG, jsonEncoding, ERROR);
869                 cred->privateData.encoding = GetEncodingTypeFromStr(jsonEncoding->valuestring);
870             }
871 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
872             //PublicData is mandatory only for SIGNED_ASYMMETRIC_KEY credentials type.
873             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PUBLICDATA_NAME);
874
875             if (NULL != jsonObj)
876             {
877                 cJSON *jsonPub = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
878                 VERIFY_NON_NULL(TAG, jsonPub, ERROR);
879                 jsonObjLen = strlen(jsonPub->valuestring);
880                 cred->publicData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
881                 VERIFY_NON_NULL(TAG, (cred->publicData.data), ERROR);
882                 memcpy(cred->publicData.data, jsonPub->valuestring, jsonObjLen);
883                 cred->publicData.len = jsonObjLen;
884             }
885
886             //Optional Data
887             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_OPTDATA_NAME);
888             if (NULL != jsonObj)
889             {
890                 cJSON *jsonOpt = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
891                 VERIFY_NON_NULL(TAG, jsonOpt, ERROR);
892                 jsonObjLen = strlen(jsonOpt->valuestring);
893                 cred->optionalData.data =  (uint8_t *)OICCalloc(1, jsonObjLen);
894                 VERIFY_NON_NULL(TAG, (cred->optionalData.data), ERROR);
895                 memcpy(cred->optionalData.data, jsonOpt->valuestring, jsonObjLen);
896                 cred->optionalData.len = jsonObjLen;
897
898                 cJSON *jsonEncoding = cJSON_GetObjectItem(jsonObj, OIC_JSON_ENCODING_NAME);
899                 VERIFY_NON_NULL(TAG, jsonEncoding, ERROR);
900                 cred->optionalData.encoding = GetEncodingTypeFromStr(jsonEncoding->valuestring);
901
902                 cJSON *jsonRevstat = cJSON_GetObjectItem(jsonObj, OIC_JSON_REVOCATION_STATUS_NAME);
903                 VERIFY_NON_NULL(TAG, jsonRevstat, ERROR);
904                 cred->optionalData.revstat = jsonObj->valueint;
905             }
906
907             //CredUsage
908             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDUSAGE_NAME);
909             if (NULL != jsonObj)
910             {
911                 jsonObjLen = strlen(jsonObj->valuestring);
912                 cred->credUsage = OICStrdup(jsonObj->valuestring);
913                 VERIFY_NON_NULL(TAG, (cred->credUsage), ERROR);
914             }
915
916 #endif // defined(__WITH_DTLS__) || defined(__WITH_TLS__)
917
918             //Period -- Not Mandatory
919             jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PERIOD_NAME);
920             if(jsonObj && cJSON_String == jsonObj->type)
921             {
922                 jsonObjLen = strlen(jsonObj->valuestring) + 1;
923                 cred->period = (char *)OICMalloc(jsonObjLen);
924                 VERIFY_NON_NULL(TAG, cred->period, ERROR);
925                 strncpy(cred->period, jsonObj->valuestring, jsonObjLen);
926             }
927             cred->next = NULL;
928         } while( ++idx < numCred);
929     }
930
931     // rownerid
932     cJSON *jsonCredObj = cJSON_GetObjectItem(jsonCredMap, OIC_JSON_ROWNERID_NAME);
933     VERIFY_NON_NULL(TAG, jsonCredObj, ERROR);
934     VERIFY_SUCCESS(TAG, cJSON_String == jsonCredObj->type, ERROR);
935     ret = ConvertStrToUuid(jsonCredObj->valuestring, &headCred->rownerID);
936     VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
937     ret = OC_STACK_OK;
938
939 exit:
940     cJSON_Delete(jsonRoot);
941     if (OC_STACK_OK != ret)
942     {
943         DeleteCredList(headCred);
944         headCred = NULL;
945     }
946     return headCred;
947 }
948
949 static OicSecSvc_t* JSONToSvcBin(const char * jsonStr)
950 {
951     OCStackResult ret = OC_STACK_ERROR;
952     OicSecSvc_t * headSvc = NULL;
953     OicSecSvc_t * prevSvc = NULL;
954     cJSON *jsonRoot = NULL;
955     cJSON *jsonSvcArray = NULL;
956
957     VERIFY_NON_NULL(TAG, jsonStr, ERROR);
958
959     jsonRoot = cJSON_Parse(jsonStr);
960     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
961
962     jsonSvcArray = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME);
963     VERIFY_NON_NULL(TAG, jsonSvcArray, INFO);
964
965     if (cJSON_Array == jsonSvcArray->type)
966     {
967         int numSvc = cJSON_GetArraySize(jsonSvcArray);
968         int idx = 0;
969
970         VERIFY_SUCCESS(TAG, numSvc > 0, INFO);
971         do
972         {
973             cJSON *jsonSvc = cJSON_GetArrayItem(jsonSvcArray, idx);
974             VERIFY_NON_NULL(TAG, jsonSvc, ERROR);
975
976             OicSecSvc_t *svc = (OicSecSvc_t*)OICCalloc(1, sizeof(OicSecSvc_t));
977             VERIFY_NON_NULL(TAG, svc, ERROR);
978
979             headSvc = (headSvc) ? headSvc : svc;
980             if (prevSvc)
981             {
982                 prevSvc->next = svc;
983             }
984
985             cJSON *jsonObj = NULL;
986             unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {0};
987             uint32_t outLen = 0;
988             B64Result b64Ret = B64_OK;
989
990             // Service Device Identity
991             jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_DEVICE_ID);
992             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
993             VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
994             outLen = 0;
995             b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
996                        sizeof(base64Buff), &outLen);
997             VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->svcdid.id)), ERROR);
998             memcpy(svc->svcdid.id, base64Buff, outLen);
999
1000             // Service Type
1001             jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_TYPE);
1002             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1003             VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
1004             svc->svct = (OicSecSvcType_t)jsonObj->valueint;
1005
1006             // Resource Owners
1007             jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_OWNERS_NAME);
1008             VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1009             VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
1010
1011             svc->ownersLen = (size_t)cJSON_GetArraySize(jsonObj);
1012             VERIFY_SUCCESS(TAG, svc->ownersLen > 0, ERROR);
1013             svc->owners = (OicUuid_t*)OICCalloc(svc->ownersLen, sizeof(OicUuid_t));
1014             VERIFY_NON_NULL(TAG, (svc->owners), ERROR);
1015
1016             size_t idxx = 0;
1017             do
1018             {
1019                 cJSON *jsonOwnr = cJSON_GetArrayItem(jsonObj, idxx);
1020                 VERIFY_NON_NULL(TAG, jsonOwnr, ERROR);
1021                 VERIFY_SUCCESS(TAG, cJSON_String == jsonOwnr->type, ERROR);
1022                 outLen = 0;
1023                 b64Ret = b64Decode(jsonOwnr->valuestring, strlen(jsonOwnr->valuestring), base64Buff,
1024                            sizeof(base64Buff), &outLen);
1025
1026                 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->owners[idxx].id)),
1027                                    ERROR);
1028                 memcpy(svc->owners[idxx].id, base64Buff, outLen);
1029             } while ( ++idxx < svc->ownersLen);
1030
1031             prevSvc = svc;
1032         } while( ++idx < numSvc);
1033     }
1034
1035     ret = OC_STACK_OK;
1036
1037 exit:
1038     cJSON_Delete(jsonRoot);
1039     if (OC_STACK_OK != ret)
1040     {
1041         DeleteSVCList(headSvc);
1042         headSvc = NULL;
1043     }
1044     return headSvc;
1045 }
1046
1047 static OicSecAmacl_t* JSONToAmaclBin(const char * jsonStr)
1048 {
1049     OCStackResult ret = OC_STACK_ERROR;
1050     OicSecAmacl_t * headAmacl = (OicSecAmacl_t*)OICCalloc(1, sizeof(OicSecAmacl_t));
1051
1052     cJSON *jsonRoot = NULL;
1053     cJSON *jsonAmacl = NULL;
1054
1055     VERIFY_NON_NULL(TAG, jsonStr, ERROR);
1056
1057     jsonRoot = cJSON_Parse(jsonStr);
1058     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
1059
1060     jsonAmacl = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
1061     VERIFY_NON_NULL(TAG, jsonAmacl, INFO);
1062
1063     cJSON *jsonObj = NULL;
1064
1065     // Resources -- Mandatory
1066     jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_RESOURCES_NAME);
1067     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1068
1069     // Rlist
1070     cJSON *jsonRlistArray = cJSON_GetObjectItem(jsonObj, OIC_JSON_RLIST_NAME);
1071     VERIFY_NON_NULL(TAG, jsonRlistArray, ERROR);
1072     VERIFY_SUCCESS(TAG, cJSON_Array == jsonRlistArray->type, ERROR);
1073
1074     headAmacl->resourcesLen = (size_t)cJSON_GetArraySize(jsonRlistArray);
1075     headAmacl->resources = (char**)OICCalloc(headAmacl->resourcesLen, sizeof(char*));
1076     size_t idxx = 0;
1077     do
1078     {
1079         cJSON *jsonRsrc = cJSON_GetArrayItem(jsonRlistArray, idxx);
1080         VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
1081
1082         cJSON *jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_HREF_NAME);
1083         VERIFY_NON_NULL(TAG, jsonRsrcObj, ERROR);
1084         VERIFY_SUCCESS(TAG, cJSON_String == jsonRsrcObj->type, ERROR);
1085
1086         size_t jsonRsrcObjLen = 0;
1087         jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
1088         headAmacl->resources[idxx] = (char*)OICMalloc(jsonRsrcObjLen);
1089         VERIFY_NON_NULL(TAG, (headAmacl->resources[idxx]), ERROR);
1090         OICStrcpy(headAmacl->resources[idxx], jsonRsrcObjLen, jsonRsrcObj->valuestring);
1091
1092     } while ( ++idxx < headAmacl->resourcesLen);
1093
1094     // Ams -- Mandatory
1095     jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_AMS_NAME);
1096     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1097     VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
1098
1099     headAmacl->amssLen = (size_t)cJSON_GetArraySize(jsonObj);
1100     VERIFY_SUCCESS(TAG, headAmacl->amssLen > 0, ERROR);
1101     headAmacl->amss = (OicUuid_t*)OICCalloc(headAmacl->amssLen, sizeof(OicUuid_t));
1102     VERIFY_NON_NULL(TAG, headAmacl->amss, ERROR);
1103
1104     idxx = 0;
1105     do
1106     {
1107         cJSON *jsonAms = cJSON_GetArrayItem(jsonObj, idxx);
1108         VERIFY_NON_NULL(TAG, jsonAms, ERROR);
1109         VERIFY_SUCCESS(TAG, cJSON_String == jsonAms->type, ERROR);
1110
1111         memcpy(headAmacl->amss[idxx].id, (OicUuid_t *)jsonAms->valuestring, strlen(jsonAms->valuestring));
1112
1113     } while ( ++idxx < headAmacl->amssLen);
1114
1115
1116     // Rowner -- Mandatory
1117     jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_ROWNERID_NAME);
1118     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1119     VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
1120
1121     ret = ConvertStrToUuid(jsonObj->valuestring, &headAmacl->rownerID);
1122     VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
1123
1124     ret = OC_STACK_OK;
1125
1126 exit:
1127     cJSON_Delete(jsonRoot);
1128     if (OC_STACK_OK != ret)
1129     {
1130         DeleteAmaclList(headAmacl);
1131         headAmacl = NULL;
1132     }
1133     return headAmacl;
1134 }
1135
1136 int main(int argc, char* argv[])
1137 {
1138     if (argc == 3)
1139     {
1140         printf("JSON File Name: %s\n CBOR File Name: %s \n", argv[1], argv[2]);
1141         ConvertJsonToCBOR(argv[1], argv[2]);
1142     }
1143     else
1144     {
1145         printf("This program requires two inputs:\n");
1146         printf("1. First input is a json file tha will be converted to cbor. \n");
1147         printf("2. Second input is a resulting cbor file that will store converted cbor. \n");
1148         printf("\t json2cbor <json_file_name> <cbor_file_name>. \n");
1149     }
1150 }