1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 #if defined (__TIZENRT__)
25 #include <apps/netutils/cJSON.h>
30 #include "cainterface.h"
32 #include "oic_malloc.h"
33 #include "oic_string.h"
34 #include "ocpayload.h"
35 #include "ocpayloadcbor.h"
36 #include "payload_logging.h"
37 #include "secureresourcemanager.h"
38 #include "srmresourcestrings.h"
39 #include "srmutility.h"
40 #include "aclresource.h"
41 #include "pstatresource.h"
42 #include "doxmresource.h"
43 #include "amaclresource.h"
44 #include "credresource.h"
45 #include "security_internals.h"
47 #define TAG "OIC_JSON2CBOR"
48 #define MAX_RANGE ((size_t)-1)
49 //SVR database buffer block size
50 static const size_t DB_FILE_SIZE_BLOCK = 1023;
52 static OicSecPstat_t* JSONToPstatBin(const char * jsonStr);
53 static OicSecDoxm_t* JSONToDoxmBin(const char * jsonStr);
54 static OicSecAcl_t *JSONToAclBin(const char * jsonStr);
55 static OicSecAmacl_t* JSONToAmaclBin(const char * jsonStr);
56 static OicSecCred_t* JSONToCredBin(const char * jsonStr);
58 static size_t GetJSONFileSize(const char *jsonFileName)
62 char buffer[DB_FILE_SIZE_BLOCK];
63 FILE* fp = fopen(jsonFileName, "r");
68 bytesRead = fread(buffer, 1, DB_FILE_SIZE_BLOCK, fp);
69 if (bytesRead >=(MAX_RANGE - size))
75 } while (bytesRead > 0);
81 static void ConvertJsonToCBOR(const char *jsonFileName, const char *cborFileName)
86 uint8_t *aclCbor = NULL;
87 uint8_t *pstatCbor = NULL;
88 uint8_t *doxmCbor = NULL;
89 uint8_t *amaclCbor = NULL;
90 uint8_t *credCbor = NULL;
91 cJSON *jsonRoot = NULL;
92 OCStackResult ret = OC_STACK_ERROR;
93 size_t size = GetJSONFileSize(jsonFileName);
96 OIC_LOG (ERROR, TAG, "Failed converting to JSON");
100 jsonStr = (char *)OICMalloc(size + 1);
101 VERIFY_NON_NULL(TAG, jsonStr, FATAL);
103 fp = fopen(jsonFileName, "r");
106 size_t bytesRead = fread(jsonStr, 1, size, fp);
107 jsonStr[bytesRead] = '\0';
109 OIC_LOG_V(DEBUG, TAG, "Read %zu bytes", bytesRead);
115 OIC_LOG (ERROR, TAG, "Unable to open JSON file!!");
119 jsonRoot = cJSON_Parse(jsonStr);
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;
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)
131 OIC_LOG (ERROR, TAG, "Failed converting Acl to Cbor Payload");
135 printf("ACL Cbor Size: %zd\n", aclCborSize);
139 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
140 size_t pstatCborSize = 0;
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)
148 OIC_LOG (ERROR, TAG, "Failed converting Pstat to Cbor Payload");
149 DeletePstatBinData(pstat);
152 printf("PSTAT Cbor Size: %zd\n", pstatCborSize);
153 DeletePstatBinData(pstat);
155 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME);
156 size_t doxmCborSize = 0;
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)
164 OIC_LOG (ERROR, TAG, "Failed converting Doxm to Cbor Payload");
165 DeleteDoxmBinData(doxm);
168 printf("DOXM Cbor Size: %zd\n", doxmCborSize);
169 DeleteDoxmBinData(doxm);
171 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
172 size_t amaclCborSize = 0;
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)
180 OIC_LOG (ERROR, TAG, "Failed converting Amacl to Cbor Payload");
181 DeleteAmaclList(amacl);
184 printf("AMACL Cbor Size: %zd\n", amaclCborSize);
185 DeleteAmaclList(amacl);
187 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
188 //printf("CRED json : \n%s\n", cJSON_PrintUnformatted(value));
189 size_t credCborSize = 0;
193 OicSecCred_t *cred = JSONToCredBin(jsonStr);
194 VERIFY_NON_NULL(TAG, cred, FATAL);
195 ret = CredToCBORPayload(cred, &credCbor, &credCborSize, secureFlag);
196 if(OC_STACK_OK != ret)
198 OIC_LOG (ERROR, TAG, "Failed converting Cred to Cbor Payload");
199 DeleteCredList(cred);
202 printf("CRED Cbor Size: %zd\n", credCborSize);
203 DeleteCredList(cred);
207 size_t cborSize = aclCborSize + pstatCborSize + doxmCborSize + credCborSize + amaclCborSize;
209 printf("Total Cbor Size : %zd\n", cborSize);
210 cborSize += 255; // buffer margin for adding map and byte string
211 uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborSize);
212 VERIFY_NON_NULL(TAG, outPayload, ERROR);
213 cbor_encoder_init(&encoder, outPayload, cborSize, 0);
215 CborError cborEncoderResult = cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
216 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Creating Main Map.");
219 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
220 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
221 cborEncoderResult = cbor_encode_byte_string(&map, aclCbor, aclCborSize);
222 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
225 if (pstatCborSize > 0)
227 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
228 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
229 cborEncoderResult = cbor_encode_byte_string(&map, pstatCbor, pstatCborSize);
230 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
232 if (doxmCborSize > 0)
234 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
235 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Name.");
236 cborEncoderResult = cbor_encode_byte_string(&map, doxmCbor, doxmCborSize);
237 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Value.");
239 if (amaclCborSize > 0)
241 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_AMACL_NAME, strlen(OIC_JSON_AMACL_NAME));
242 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Name.");
243 cborEncoderResult = cbor_encode_byte_string(&map, amaclCbor, amaclCborSize);
244 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Value.");
246 if (credCborSize > 0)
248 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME));
249 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Name.");
250 cborEncoderResult = cbor_encode_byte_string(&map, credCbor, credCborSize);
251 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Value.");
254 cborEncoderResult = cbor_encoder_close_container(&encoder, &map);
255 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Container.");
257 size_t s = cbor_encoder_get_buffer_size(&encoder, outPayload);
258 OIC_LOG_V(DEBUG, TAG, "Payload size %zu", s);
260 fp1 = fopen(cborFileName, "w");
263 size_t bytesWritten = fwrite(outPayload, 1, s, fp1);
264 if (bytesWritten == s)
266 OIC_LOG_V(DEBUG, TAG, "Written %zu bytes", bytesWritten);
270 OIC_LOG_V(ERROR, TAG, "Failed writing %zu bytes", s);
277 cJSON_Delete(jsonRoot);
287 OicSecAcl_t* JSONToAclBin(const char * jsonStr)
289 OCStackResult ret = OC_STACK_ERROR;
290 OicSecAcl_t * headAcl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
291 cJSON *jsonRoot = NULL;
293 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
295 jsonRoot = cJSON_Parse(jsonStr);
296 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
298 cJSON *jsonAclMap = cJSON_GetObjectItem(jsonRoot, OIC_JSON_ACL_NAME);
299 VERIFY_NON_NULL(TAG, jsonAclMap, ERROR);
301 cJSON *jsonAclObj = NULL;
304 jsonAclObj = cJSON_GetObjectItem(jsonAclMap, OIC_JSON_ACLIST_NAME);
305 VERIFY_NON_NULL(TAG, jsonAclObj, ERROR);
308 cJSON *jsonAclArray = NULL;
309 jsonAclArray = cJSON_GetObjectItem(jsonAclObj, OIC_JSON_ACES_NAME);
310 VERIFY_NON_NULL(TAG, jsonAclArray, ERROR);
312 if (cJSON_Array == jsonAclArray->type)
315 int numAcl = cJSON_GetArraySize(jsonAclArray);
318 VERIFY_SUCCESS(TAG, numAcl > 0, INFO);
321 cJSON *jsonAcl = cJSON_GetArrayItem(jsonAclArray, idx);
322 VERIFY_NON_NULL(TAG, jsonAcl, ERROR);
324 OicSecAce_t *ace = (OicSecAce_t*)OICCalloc(1, sizeof(OicSecAce_t));
325 VERIFY_NON_NULL(TAG, ace, ERROR);
326 LL_APPEND(headAcl->aces, ace);
328 size_t jsonObjLen = 0;
329 cJSON *jsonObj = NULL;
330 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_SUBJECTID_NAME);
331 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
332 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
333 if(strcmp(jsonObj->valuestring, WILDCARD_RESOURCE_URI) == 0)
335 ace->subjectuuid.id[0] = '*';
339 ret = ConvertStrToUuid(jsonObj->valuestring, &ace->subjectuuid);
340 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
342 // Resources -- Mandatory
343 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_RESOURCES_NAME);
344 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
345 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
347 size_t resourcesLen = (size_t)cJSON_GetArraySize(jsonObj);
348 VERIFY_SUCCESS(TAG, resourcesLen > 0, ERROR);
350 for(size_t idxx = 0; idxx < resourcesLen; idxx++)
352 OicSecRsrc_t* rsrc = (OicSecRsrc_t*)OICCalloc(1, sizeof(OicSecRsrc_t));
353 VERIFY_NON_NULL(TAG, rsrc, ERROR);
355 cJSON *jsonRsrc = cJSON_GetArrayItem(jsonObj, idxx);
356 VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
359 size_t jsonRsrcObjLen = 0;
360 cJSON *jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_HREF_NAME);
361 VERIFY_NON_NULL(TAG, jsonRsrcObj, ERROR);
362 VERIFY_SUCCESS(TAG, cJSON_String == jsonRsrcObj->type, ERROR);
364 jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
365 rsrc->href = (char*)OICMalloc(jsonRsrcObjLen);
366 VERIFY_NON_NULL(TAG, (rsrc->href), ERROR);
367 OICStrcpy(rsrc->href, jsonRsrcObjLen, jsonRsrcObj->valuestring);
370 jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_REL_NAME);
373 jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
374 rsrc->rel = (char*)OICMalloc(jsonRsrcObjLen);
375 VERIFY_NON_NULL(TAG, (rsrc->rel), ERROR);
376 OICStrcpy(rsrc->rel, jsonRsrcObjLen, jsonRsrcObj->valuestring);
380 jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_RT_NAME);
381 if(jsonRsrcObj && cJSON_Array == jsonRsrcObj->type)
383 rsrc->typeLen = (size_t)cJSON_GetArraySize(jsonRsrcObj);
384 VERIFY_SUCCESS(TAG, (0 < rsrc->typeLen), ERROR);
385 rsrc->types = (char**)OICCalloc(rsrc->typeLen, sizeof(char*));
386 VERIFY_NON_NULL(TAG, (rsrc->types), ERROR);
387 for(size_t i = 0; i < rsrc->typeLen; i++)
389 cJSON *jsonRsrcType = cJSON_GetArrayItem(jsonRsrcObj, i);
390 VERIFY_NON_NULL(TAG, jsonRsrcType, ERROR);
391 rsrc->types[i] = OICStrdup(jsonRsrcType->valuestring);
392 VERIFY_NON_NULL(TAG, (rsrc->types[i]), ERROR);
397 jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_IF_NAME);
398 if(jsonRsrcObj && cJSON_Array == jsonRsrcObj->type)
400 rsrc->interfaceLen = (size_t)cJSON_GetArraySize(jsonRsrcObj);
401 VERIFY_SUCCESS(TAG, (0 < rsrc->interfaceLen), ERROR);
402 rsrc->interfaces = (char**)OICCalloc(rsrc->interfaceLen, sizeof(char*));
403 VERIFY_NON_NULL(TAG, (rsrc->interfaces), ERROR);
404 for(size_t i = 0; i < rsrc->interfaceLen; i++)
406 cJSON *jsonInterface = cJSON_GetArrayItem(jsonRsrcObj, i);
407 VERIFY_NON_NULL(TAG, jsonInterface, ERROR);
408 rsrc->interfaces[i] = OICStrdup(jsonInterface->valuestring);
409 VERIFY_NON_NULL(TAG, (rsrc->interfaces[i]), ERROR);
413 LL_APPEND(ace->resources, rsrc);
416 // Permissions -- Mandatory
417 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_PERMISSION_NAME);
418 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
419 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
420 ace->permission = jsonObj->valueint;
422 //Validity -- Not Mandatory
423 cJSON *jsonValidityObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_VALIDITY_NAME);
426 VERIFY_SUCCESS(TAG, cJSON_Array == jsonValidityObj->type, ERROR);
427 size_t validityLen = (size_t) cJSON_GetArraySize(jsonValidityObj);
428 VERIFY_SUCCESS(TAG, (0 < validityLen), ERROR);
430 cJSON *jsonValidity = NULL;
431 for(size_t i = 0; i < validityLen; i++)
433 jsonValidity = cJSON_GetArrayItem(jsonValidityObj, i);
434 VERIFY_NON_NULL(TAG, jsonValidity, ERROR);
435 VERIFY_SUCCESS(TAG, (jsonValidity->type == cJSON_Array), ERROR);
437 OicSecValidity_t* validity = (OicSecValidity_t*)OICCalloc(1, sizeof(OicSecValidity_t));
438 VERIFY_NON_NULL(TAG, validity, ERROR);
439 LL_APPEND(ace->validities, validity);
442 cJSON* jsonPeriod = cJSON_GetArrayItem(jsonValidity, 0);
445 VERIFY_SUCCESS(TAG, (cJSON_String == jsonPeriod->type), ERROR);
447 jsonObjLen = strlen(jsonPeriod->valuestring) + 1;
448 validity->period = (char*)OICMalloc(jsonObjLen);
449 VERIFY_NON_NULL(TAG, validity->period, ERROR);
450 OICStrcpy(validity->period, jsonObjLen, jsonPeriod->valuestring);
454 cJSON* jsonRecurObj = cJSON_GetArrayItem(jsonValidity, 1);
457 VERIFY_SUCCESS(TAG, (cJSON_Array == jsonRecurObj->type), ERROR);
458 validity->recurrenceLen = (size_t) cJSON_GetArraySize(jsonRecurObj);
459 VERIFY_SUCCESS(TAG, (0 < validity->recurrenceLen), ERROR);
461 validity->recurrences = (char**)OICCalloc(validity->recurrenceLen, sizeof(char*));
462 VERIFY_NON_NULL(TAG, validity->recurrences, ERROR);
464 cJSON *jsonRecur = NULL;
465 for(size_t i = 0; i < validity->recurrenceLen; i++)
467 jsonRecur = cJSON_GetArrayItem(jsonRecurObj, i);
468 VERIFY_NON_NULL(TAG, jsonRecur, ERROR);
469 jsonObjLen = strlen(jsonRecur->valuestring) + 1;
470 validity->recurrences[i] = (char*)OICMalloc(jsonObjLen);
471 VERIFY_NON_NULL(TAG, validity->recurrences[i], ERROR);
472 OICStrcpy(validity->recurrences[i], jsonObjLen, jsonRecur->valuestring);
477 } while( ++idx < numAcl);
482 jsonAclObj = cJSON_GetObjectItem(jsonAclMap, OIC_JSON_ROWNERID_NAME);
483 VERIFY_NON_NULL(TAG, jsonAclObj, ERROR);
484 VERIFY_SUCCESS(TAG, cJSON_String == jsonAclObj->type, ERROR);
485 ret = ConvertStrToUuid(jsonAclObj->valuestring, &headAcl->rownerID);
486 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
491 cJSON_Delete(jsonRoot);
492 if (OC_STACK_OK != ret)
494 DeleteACLList(headAcl);
500 OicSecDoxm_t* JSONToDoxmBin(const char * jsonStr)
502 printf("IN JSONToDoxmBin\n");
508 OCStackResult ret = OC_STACK_ERROR;
509 OicSecDoxm_t *doxm = NULL;
510 cJSON *jsonDoxm = NULL;
511 cJSON *jsonObj = NULL;
513 size_t jsonObjLen = 0;
515 cJSON *jsonRoot = cJSON_Parse(jsonStr);
516 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
518 jsonDoxm = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME);
519 VERIFY_NON_NULL(TAG, jsonDoxm, ERROR);
521 doxm = (OicSecDoxm_t *)OICCalloc(1, sizeof(OicSecDoxm_t));
522 VERIFY_NON_NULL(TAG, doxm, ERROR);
524 //OxmType -- not Mandatory
525 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_TYPE_NAME);
526 if ((jsonObj) && (cJSON_Array == jsonObj->type))
528 doxm->oxmTypeLen = (size_t)cJSON_GetArraySize(jsonObj);
529 VERIFY_SUCCESS(TAG, doxm->oxmTypeLen > 0, ERROR);
531 doxm->oxmType = (OicUrn_t *)OICCalloc(doxm->oxmTypeLen, sizeof(char *));
532 VERIFY_NON_NULL(TAG, (doxm->oxmType), ERROR);
534 for (size_t i = 0; i < doxm->oxmTypeLen ; i++)
536 cJSON *jsonOxmTy = cJSON_GetArrayItem(jsonObj, i);
537 VERIFY_NON_NULL(TAG, jsonOxmTy, ERROR);
539 jsonObjLen = strlen(jsonOxmTy->valuestring) + 1;
540 doxm->oxmType[i] = (char*)OICMalloc(jsonObjLen);
541 VERIFY_NON_NULL(TAG, doxm->oxmType[i], ERROR);
542 strncpy((char *)doxm->oxmType[i], (char *)jsonOxmTy->valuestring, jsonObjLen);
546 //Oxm -- not Mandatory
547 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXMS_NAME);
548 if (jsonObj && cJSON_Array == jsonObj->type)
550 doxm->oxmLen = (size_t)cJSON_GetArraySize(jsonObj);
551 VERIFY_SUCCESS(TAG, doxm->oxmLen > 0, ERROR);
553 doxm->oxm = (OicSecOxm_t*)OICCalloc(doxm->oxmLen, sizeof(OicSecOxm_t));
554 VERIFY_NON_NULL(TAG, doxm->oxm, ERROR);
556 for (size_t i = 0; i < doxm->oxmLen ; i++)
558 cJSON *jsonOxm = cJSON_GetArrayItem(jsonObj, i);
559 VERIFY_NON_NULL(TAG, jsonOxm, ERROR);
560 doxm->oxm[i] = (OicSecOxm_t)jsonOxm->valueint;
564 //OxmSel -- Mandatory
565 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_SEL_NAME);
568 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
569 doxm->oxmSel = (OicSecOxm_t)jsonObj->valueint;
573 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_SUPPORTED_CRED_TYPE_NAME);
576 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
577 doxm->sct = (OicSecCredType_t)jsonObj->valueint;
581 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OWNED_NAME);
584 VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type), ERROR);
585 doxm->owned = jsonObj->valueint;
588 #ifdef MULTIPLE_OWNER
589 //mom -- Not Mandatory
590 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_MOM_NAME);
593 VERIFY_SUCCESS(TAG, (cJSON_Number == jsonObj->type), ERROR);
594 doxm->mom = (OicSecMom_t*)OICCalloc(1, sizeof(OicSecMom_t));
595 VERIFY_NON_NULL(TAG, doxm->mom, ERROR);
596 doxm->mom->mode = (OicSecMomType_t)jsonObj->valueint;
598 #endif //MULTIPLE_OWNER
600 //DeviceId -- Mandatory
601 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVICE_ID_NAME);
604 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
605 if (cJSON_String == jsonObj->type)
607 //Check for empty string, in case DeviceId field has not been set yet
608 if (jsonObj->valuestring[0])
610 ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->deviceID);
611 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
616 //rowner -- Mandatory
617 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_ROWNERID_NAME);
618 if (true == doxm->owned)
620 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
624 ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->rownerID);
625 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
628 //Owner -- will be empty when device status is unowned.
629 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVOWNERID_NAME);
630 if (true == doxm->owned)
632 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
636 ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->owner);
637 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
643 cJSON_Delete(jsonRoot);
644 if (OC_STACK_OK != ret)
646 DeleteDoxmBinData(doxm);
649 printf("OUT JSONToDoxmBin\n");
653 OicSecPstat_t* JSONToPstatBin(const char * jsonStr)
655 printf("IN JSONToPstatBin\n");
661 OCStackResult ret = OC_STACK_ERROR;
662 OicSecPstat_t *pstat = NULL;
663 cJSON *jsonPstat = NULL;
664 cJSON *jsonObj = NULL;
666 cJSON *jsonRoot = cJSON_Parse(jsonStr);
667 VERIFY_NON_NULL(TAG, jsonRoot, INFO);
669 jsonPstat = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
670 VERIFY_NON_NULL(TAG, jsonPstat, INFO);
672 pstat = (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
673 VERIFY_NON_NULL(TAG, pstat, INFO);
674 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_ISOP_NAME);
675 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
676 VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type) , ERROR);
677 pstat->isOp = jsonObj->valueint;
679 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_DEVICE_ID_NAME);
680 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
681 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
682 ret = ConvertStrToUuid(jsonObj->valuestring, &pstat->deviceID);
683 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
685 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_ROWNERID_NAME);
686 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
687 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
688 ret = ConvertStrToUuid(jsonObj->valuestring, &pstat->rownerID);
689 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
691 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_CM_NAME);
692 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
693 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
694 pstat->cm = (OicSecDpm_t)jsonObj->valueint;
696 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_TM_NAME);
697 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
698 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
699 pstat->tm = (OicSecDpm_t)jsonObj->valueint;
701 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_OM_NAME);
702 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
703 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
704 pstat->om = (OicSecDpom_t)jsonObj->valueint;
706 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_SM_NAME);
707 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
708 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
710 pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
711 pstat->sm[0] = (OicSecDpom_t)jsonObj->valueint;
716 cJSON_Delete(jsonRoot);
717 if (OC_STACK_OK != ret)
719 OIC_LOG(ERROR, TAG, "JSONToPstatBin failed");
721 printf("OUT JSONToPstatBin\n");
725 static OicEncodingType_t GetEncodingTypeFromStr(const char* encodingType)
727 if (strcmp(OIC_SEC_ENCODING_RAW, encodingType) == 0)
729 return OIC_ENCODING_RAW;
731 if (strcmp(OIC_SEC_ENCODING_BASE64, encodingType) == 0)
733 return OIC_ENCODING_BASE64;
735 if (strcmp(OIC_SEC_ENCODING_PEM, encodingType) == 0)
737 return OIC_ENCODING_PEM;
739 if (strcmp(OIC_SEC_ENCODING_DER, encodingType) == 0)
741 return OIC_ENCODING_DER;
743 OIC_LOG(WARNING, TAG, "Unknow encoding type dectected!");
744 OIC_LOG(WARNING, TAG, "json2cbor will use \"oic.sec.encoding.raw\" as default encoding type.");
745 return OIC_ENCODING_RAW;
748 OicSecCred_t * JSONToCredBin(const char * jsonStr)
752 OIC_LOG(ERROR, TAG,"JSONToCredBin jsonStr in NULL");
756 OicSecCred_t *headCred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
757 OCStackResult ret = OC_STACK_ERROR;
758 cJSON *jsonRoot = NULL;
759 VERIFY_NON_NULL(TAG, headCred, ERROR);
761 jsonRoot = cJSON_Parse(jsonStr);
762 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
764 cJSON *jsonCredMap = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
765 VERIFY_NON_NULL(TAG, jsonCredMap, ERROR);
768 cJSON *jsonCredArray = NULL;
769 jsonCredArray = cJSON_GetObjectItem(jsonCredMap, OIC_JSON_CREDS_NAME);
770 VERIFY_NON_NULL(TAG, jsonCredArray, ERROR);
772 if (cJSON_Array == jsonCredArray->type)
774 int numCred = cJSON_GetArraySize(jsonCredArray);
775 VERIFY_SUCCESS(TAG, numCred > 0, ERROR);
779 cJSON *jsonCred = cJSON_GetArrayItem(jsonCredArray, idx);
780 VERIFY_NON_NULL(TAG, jsonCred, ERROR);
782 OicSecCred_t *cred = NULL;
789 cred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
790 OicSecCred_t *temp = headCred;
797 VERIFY_NON_NULL(TAG, cred, ERROR);
799 size_t jsonObjLen = 0;
800 cJSON *jsonObj = NULL;
802 //CredId -- Mandatory
803 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDID_NAME);
806 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
807 cred->credId = jsonObj->valueint;
810 //subject -- Mandatory
811 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_SUBJECTID_NAME);
812 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
813 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
814 if(strcmp(jsonObj->valuestring, WILDCARD_RESOURCE_URI) == 0)
816 cred->subject.id[0] = '*';
820 ret = ConvertStrToUuid(jsonObj->valuestring, &cred->subject);
821 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
824 //CredType -- Mandatory
825 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDTYPE_NAME);
826 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
827 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
828 cred->credType = (OicSecCredType_t)jsonObj->valueint;
829 //PrivateData is mandatory for some of the credential types listed below.
830 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PRIVATEDATA_NAME);
834 cJSON *jsonPriv = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
835 VERIFY_NON_NULL(TAG, jsonPriv, ERROR);
836 jsonObjLen = strlen(jsonPriv->valuestring) + 1;
837 cred->privateData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
838 VERIFY_NON_NULL(TAG, (cred->privateData.data), ERROR);
839 memcpy(cred->privateData.data, jsonPriv->valuestring, jsonObjLen);
840 cred->privateData.len = jsonObjLen;
842 cJSON *jsonEncoding = cJSON_GetObjectItem(jsonObj, OIC_JSON_ENCODING_NAME);
843 VERIFY_NON_NULL(TAG, jsonEncoding, ERROR);
844 cred->privateData.encoding = GetEncodingTypeFromStr(jsonEncoding->valuestring);
846 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
847 //PublicData is mandatory only for SIGNED_ASYMMETRIC_KEY credentials type.
848 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PUBLICDATA_NAME);
852 cJSON *jsonPub = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
853 VERIFY_NON_NULL(TAG, jsonPub, ERROR);
854 jsonObjLen = strlen(jsonPub->valuestring) + 1;
855 cred->publicData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
856 VERIFY_NON_NULL(TAG, (cred->publicData.data), ERROR);
857 memcpy(cred->publicData.data, jsonPub->valuestring, jsonObjLen);
858 cred->publicData.len = jsonObjLen;
862 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_OPTDATA_NAME);
865 cJSON *jsonOpt = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
866 VERIFY_NON_NULL(TAG, jsonOpt, ERROR);
867 jsonObjLen = strlen(jsonOpt->valuestring) + 1;
868 cred->optionalData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
869 VERIFY_NON_NULL(TAG, (cred->optionalData.data), ERROR);
870 memcpy(cred->optionalData.data, jsonOpt->valuestring, jsonObjLen);
871 cred->optionalData.len = jsonObjLen;
873 cJSON *jsonEncoding = cJSON_GetObjectItem(jsonObj, OIC_JSON_ENCODING_NAME);
874 VERIFY_NON_NULL(TAG, jsonEncoding, ERROR);
875 cred->optionalData.encoding = GetEncodingTypeFromStr(jsonEncoding->valuestring);
877 cJSON *jsonRevstat = cJSON_GetObjectItem(jsonObj, OIC_JSON_REVOCATION_STATUS_NAME);
878 VERIFY_NON_NULL(TAG, jsonRevstat, ERROR);
879 cred->optionalData.revstat = jsonObj->valueint;
883 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDUSAGE_NAME);
886 jsonObjLen = strlen(jsonObj->valuestring);
887 cred->credUsage = OICStrdup(jsonObj->valuestring);
888 VERIFY_NON_NULL(TAG, (cred->credUsage), ERROR);
891 #endif // defined(__WITH_DTLS__) || defined(__WITH_TLS__)
893 //Period -- Not Mandatory
894 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PERIOD_NAME);
895 if(jsonObj && cJSON_String == jsonObj->type)
897 jsonObjLen = strlen(jsonObj->valuestring) + 1;
898 cred->period = (char *)OICMalloc(jsonObjLen);
899 VERIFY_NON_NULL(TAG, cred->period, ERROR);
900 strncpy(cred->period, jsonObj->valuestring, jsonObjLen);
903 } while( ++idx < numCred);
907 cJSON *jsonCredObj = cJSON_GetObjectItem(jsonCredMap, OIC_JSON_ROWNERID_NAME);
908 VERIFY_NON_NULL(TAG, jsonCredObj, ERROR);
909 VERIFY_SUCCESS(TAG, cJSON_String == jsonCredObj->type, ERROR);
910 ret = ConvertStrToUuid(jsonCredObj->valuestring, &headCred->rownerID);
911 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
915 cJSON_Delete(jsonRoot);
916 if (OC_STACK_OK != ret)
918 DeleteCredList(headCred);
924 static OicSecAmacl_t* JSONToAmaclBin(const char * jsonStr)
926 OCStackResult ret = OC_STACK_ERROR;
927 OicSecAmacl_t * headAmacl = (OicSecAmacl_t*)OICCalloc(1, sizeof(OicSecAmacl_t));
929 cJSON *jsonRoot = NULL;
930 cJSON *jsonAmacl = NULL;
932 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
934 jsonRoot = cJSON_Parse(jsonStr);
935 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
937 jsonAmacl = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
938 VERIFY_NON_NULL(TAG, jsonAmacl, INFO);
940 cJSON *jsonObj = NULL;
942 // Resources -- Mandatory
943 jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_RESOURCES_NAME);
944 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
947 cJSON *jsonRlistArray = cJSON_GetObjectItem(jsonObj, OIC_JSON_RLIST_NAME);
948 VERIFY_NON_NULL(TAG, jsonRlistArray, ERROR);
949 VERIFY_SUCCESS(TAG, cJSON_Array == jsonRlistArray->type, ERROR);
951 headAmacl->resourcesLen = (size_t)cJSON_GetArraySize(jsonRlistArray);
952 headAmacl->resources = (char**)OICCalloc(headAmacl->resourcesLen, sizeof(char*));
956 cJSON *jsonRsrc = cJSON_GetArrayItem(jsonRlistArray, idxx);
957 VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
959 cJSON *jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_HREF_NAME);
960 VERIFY_NON_NULL(TAG, jsonRsrcObj, ERROR);
961 VERIFY_SUCCESS(TAG, cJSON_String == jsonRsrcObj->type, ERROR);
963 size_t jsonRsrcObjLen = 0;
964 jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
965 headAmacl->resources[idxx] = (char*)OICMalloc(jsonRsrcObjLen);
966 VERIFY_NON_NULL(TAG, (headAmacl->resources[idxx]), ERROR);
967 OICStrcpy(headAmacl->resources[idxx], jsonRsrcObjLen, jsonRsrcObj->valuestring);
969 } while ( ++idxx < headAmacl->resourcesLen);
972 jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_AMS_NAME);
973 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
974 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
976 headAmacl->amssLen = (size_t)cJSON_GetArraySize(jsonObj);
977 VERIFY_SUCCESS(TAG, headAmacl->amssLen > 0, ERROR);
978 headAmacl->amss = (OicUuid_t*)OICCalloc(headAmacl->amssLen, sizeof(OicUuid_t));
979 VERIFY_NON_NULL(TAG, headAmacl->amss, ERROR);
984 cJSON *jsonAms = cJSON_GetArrayItem(jsonObj, idxx);
985 VERIFY_NON_NULL(TAG, jsonAms, ERROR);
986 VERIFY_SUCCESS(TAG, cJSON_String == jsonAms->type, ERROR);
988 memcpy(headAmacl->amss[idxx].id, (OicUuid_t *)jsonAms->valuestring, strlen(jsonAms->valuestring));
990 } while ( ++idxx < headAmacl->amssLen);
993 // Rowner -- Mandatory
994 jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_ROWNERID_NAME);
995 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
996 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
998 ret = ConvertStrToUuid(jsonObj->valuestring, &headAmacl->rownerID);
999 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
1004 cJSON_Delete(jsonRoot);
1005 if (OC_STACK_OK != ret)
1007 DeleteAmaclList(headAmacl);
1013 int main(int argc, char* argv[])
1017 printf("JSON File Name: %s\n CBOR File Name: %s \n", argv[1], argv[2]);
1018 ConvertJsonToCBOR(argv[1], argv[2]);
1022 printf("This program requires two inputs:\n");
1023 printf("1. First input is a json file tha will be converted to cbor. \n");
1024 printf("2. Second input is a resulting cbor file that will store converted cbor. \n");
1025 printf("\t json2cbor <json_file_name> <cbor_file_name>. \n");