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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
26 #include "cainterface.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"
44 #define TAG "JSON2CBOR"
45 #define MAX_RANGE ((size_t)-1)
46 //SVR database buffer block size
47 static const size_t DB_FILE_SIZE_BLOCK = 1023;
49 static OicSecPstat_t* JSONToPstatBin(const char * jsonStr);
50 static OicSecDoxm_t* JSONToDoxmBin(const char * jsonStr);
51 static OicSecAcl_t *JSONToAclBin(const char * jsonStr);
52 static OicSecSvc_t* JSONToSvcBin(const char * jsonStr);
53 static OicSecAmacl_t* JSONToAmaclBin(const char * jsonStr);
54 static OicSecCred_t* JSONToCredBin(const char * jsonStr);
56 static size_t GetJSONFileSize(const char *jsonFileName)
60 char buffer[DB_FILE_SIZE_BLOCK];
61 FILE* fp = fopen(jsonFileName, "r");
66 bytesRead = fread(buffer, 1, DB_FILE_SIZE_BLOCK, fp);
67 if (bytesRead >=(MAX_RANGE - size))
73 } while (bytesRead > 0);
79 static void ConvertJsonToCBOR(const char *jsonFileName, const char *cborFileName)
84 uint8_t *aclCbor = NULL;
85 uint8_t *pstatCbor = NULL;
86 uint8_t *doxmCbor = NULL;
87 uint8_t *amaclCbor = NULL;
88 uint8_t *svcCbor = NULL;
89 uint8_t *credCbor = NULL;
90 cJSON *jsonRoot = NULL;
91 OCStackResult ret = OC_STACK_ERROR;
92 size_t size = GetJSONFileSize(jsonFileName);
95 OIC_LOG (ERROR, TAG, "Failed converting to JSON");
99 jsonStr = (char *)OICMalloc(size + 1);
100 VERIFY_NON_NULL(TAG, jsonStr, FATAL);
102 fp = fopen(jsonFileName, "r");
105 size_t bytesRead = fread(jsonStr, 1, size, fp);
106 jsonStr[bytesRead] = '\0';
108 OIC_LOG_V(DEBUG, TAG, "Read %zu bytes", bytesRead);
114 OIC_LOG (ERROR, TAG, "Unable to open JSON file!!");
118 jsonRoot = cJSON_Parse(jsonStr);
120 cJSON *value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_ACL_NAME);
121 //printf("ACL json : \n%s\n", cJSON_PrintUnformatted(value));
122 size_t aclCborSize = 0;
125 OicSecAcl_t *acl = JSONToAclBin(jsonStr);
126 VERIFY_NON_NULL(TAG, acl, FATAL);
127 ret = AclToCBORPayload(acl, &aclCbor, &aclCborSize);
128 if(OC_STACK_OK != ret)
130 OIC_LOG (ERROR, TAG, "Failed converting Acl to Cbor Payload");
134 printf("ACL Cbor Size: %zd\n", aclCborSize);
138 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
139 size_t pstatCborSize = 0;
142 OicSecPstat_t *pstat = JSONToPstatBin(jsonStr);
143 VERIFY_NON_NULL(TAG, pstat, FATAL);
144 ret = PstatToCBORPayload(pstat, &pstatCbor, &pstatCborSize, false);
145 if(OC_STACK_OK != ret)
147 OIC_LOG (ERROR, TAG, "Failed converting Pstat to Cbor Payload");
148 DeletePstatBinData(pstat);
151 printf("PSTAT Cbor Size: %zd\n", pstatCborSize);
152 DeletePstatBinData(pstat);
154 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME);
155 size_t doxmCborSize = 0;
158 OicSecDoxm_t *doxm = JSONToDoxmBin(jsonStr);
159 VERIFY_NON_NULL(TAG, doxm, FATAL);
160 ret = DoxmToCBORPayload(doxm, &doxmCbor, &doxmCborSize, false);
161 if(OC_STACK_OK != ret)
163 OIC_LOG (ERROR, TAG, "Failed converting Doxm to Cbor Payload");
164 DeleteDoxmBinData(doxm);
167 printf("DOXM Cbor Size: %zd\n", doxmCborSize);
168 DeleteDoxmBinData(doxm);
170 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
171 size_t amaclCborSize = 0;
174 OicSecAmacl_t *amacl = JSONToAmaclBin(jsonStr);
175 VERIFY_NON_NULL(TAG, amacl, FATAL);
176 ret = AmaclToCBORPayload(amacl, &amaclCbor, &amaclCborSize);
177 if(OC_STACK_OK != ret)
179 OIC_LOG (ERROR, TAG, "Failed converting Amacl to Cbor Payload");
180 DeleteAmaclList(amacl);
183 printf("AMACL Cbor Size: %zd\n", amaclCborSize);
184 DeleteAmaclList(amacl);
186 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME);
187 size_t svcCborSize = 0;
190 OicSecSvc_t *svc = JSONToSvcBin(jsonStr);
191 VERIFY_NON_NULL(TAG, svc, FATAL);
192 ret = SVCToCBORPayload(svc, &svcCbor, &svcCborSize);
193 if(OC_STACK_OK != ret)
195 OIC_LOG (ERROR, TAG, "Failed converting Svc to Cbor Payload");
199 printf("SVC Cbor Size: %zd\n", svcCborSize);
202 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
203 //printf("CRED json : \n%s\n", cJSON_PrintUnformatted(value));
204 size_t credCborSize = 0;
208 OicSecCred_t *cred = JSONToCredBin(jsonStr);
209 VERIFY_NON_NULL(TAG, cred, FATAL);
210 ret = CredToCBORPayload(cred, &credCbor, &credCborSize, secureFlag);
211 if(OC_STACK_OK != ret)
213 OIC_LOG (ERROR, TAG, "Failed converting Cred to Cbor Payload");
214 DeleteCredList(cred);
217 printf("CRED Cbor Size: %zd\n", credCborSize);
218 DeleteCredList(cred);
222 size_t cborSize = aclCborSize + pstatCborSize + doxmCborSize + svcCborSize + credCborSize + amaclCborSize;
224 printf("Total Cbor Size : %zd\n", cborSize);
225 cborSize += 255; // buffer margin for adding map and byte string
226 uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborSize);
227 VERIFY_NON_NULL(TAG, outPayload, ERROR);
228 cbor_encoder_init(&encoder, outPayload, cborSize, 0);
230 CborError cborEncoderResult = cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
231 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Creating Main Map.");
234 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
235 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
236 cborEncoderResult = cbor_encode_byte_string(&map, aclCbor, aclCborSize);
237 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
240 if (pstatCborSize > 0)
242 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
243 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
244 cborEncoderResult = cbor_encode_byte_string(&map, pstatCbor, pstatCborSize);
245 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
247 if (doxmCborSize > 0)
249 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
250 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Name.");
251 cborEncoderResult = cbor_encode_byte_string(&map, doxmCbor, doxmCborSize);
252 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Value.");
254 if (amaclCborSize > 0)
256 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_AMACL_NAME, strlen(OIC_JSON_AMACL_NAME));
257 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Name.");
258 cborEncoderResult = cbor_encode_byte_string(&map, amaclCbor, amaclCborSize);
259 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Value.");
263 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_SVC_NAME, strlen(OIC_JSON_SVC_NAME));
264 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Name.");
265 cborEncoderResult = cbor_encode_byte_string(&map, svcCbor, svcCborSize);
266 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Value.");
268 if (credCborSize > 0)
270 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME));
271 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Name.");
272 cborEncoderResult = cbor_encode_byte_string(&map, credCbor, credCborSize);
273 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Value.");
276 cborEncoderResult = cbor_encoder_close_container(&encoder, &map);
277 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Container.");
279 size_t s = encoder.ptr - outPayload;
280 OIC_LOG_V(DEBUG, TAG, "Payload size %zu", s);
282 fp1 = fopen(cborFileName, "w");
285 size_t bytesWritten = fwrite(outPayload, 1, s, fp1);
286 if (bytesWritten == s)
288 OIC_LOG_V(DEBUG, TAG, "Written %zu bytes", bytesWritten);
292 OIC_LOG_V(ERROR, TAG, "Failed writing %zu bytes", s);
299 cJSON_Delete(jsonRoot);
310 OicSecAcl_t* JSONToAclBin(const char * jsonStr)
312 OCStackResult ret = OC_STACK_ERROR;
313 OicSecAcl_t * headAcl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
314 cJSON *jsonRoot = NULL;
316 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
318 jsonRoot = cJSON_Parse(jsonStr);
319 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
321 cJSON *jsonAclMap = cJSON_GetObjectItem(jsonRoot, OIC_JSON_ACL_NAME);
322 VERIFY_NON_NULL(TAG, jsonAclMap, ERROR);
324 cJSON *jsonAclObj = NULL;
327 jsonAclObj = cJSON_GetObjectItem(jsonAclMap, OIC_JSON_ACLIST_NAME);
328 VERIFY_NON_NULL(TAG, jsonAclObj, ERROR);
331 cJSON *jsonAclArray = NULL;
332 jsonAclArray = cJSON_GetObjectItem(jsonAclObj, OIC_JSON_ACES_NAME);
333 VERIFY_NON_NULL(TAG, jsonAclArray, ERROR);
335 if (cJSON_Array == jsonAclArray->type)
338 int numAcl = cJSON_GetArraySize(jsonAclArray);
341 VERIFY_SUCCESS(TAG, numAcl > 0, INFO);
344 cJSON *jsonAcl = cJSON_GetArrayItem(jsonAclArray, idx);
345 VERIFY_NON_NULL(TAG, jsonAcl, ERROR);
347 OicSecAce_t *ace = (OicSecAce_t*)OICCalloc(1, sizeof(OicSecAce_t));
348 VERIFY_NON_NULL(TAG, ace, ERROR);
349 LL_APPEND(headAcl->aces, ace);
351 size_t jsonObjLen = 0;
352 cJSON *jsonObj = NULL;
353 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_SUBJECTID_NAME);
354 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
355 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
356 if(strcmp(jsonObj->valuestring, WILDCARD_RESOURCE_URI) == 0)
358 ace->subjectuuid.id[0] = '*';
362 ret = ConvertStrToUuid(jsonObj->valuestring, &ace->subjectuuid);
363 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
365 // Resources -- Mandatory
366 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_RESOURCES_NAME);
367 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
368 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
370 size_t resourcesLen = (size_t)cJSON_GetArraySize(jsonObj);
371 VERIFY_SUCCESS(TAG, resourcesLen > 0, ERROR);
373 for(size_t idxx = 0; idxx < resourcesLen; idxx++)
375 OicSecRsrc_t* rsrc = (OicSecRsrc_t*)OICCalloc(1, sizeof(OicSecRsrc_t));
376 VERIFY_NON_NULL(TAG, rsrc, ERROR);
378 cJSON *jsonRsrc = cJSON_GetArrayItem(jsonObj, idxx);
379 VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
381 size_t jsonRsrcObjLen = 0;
382 cJSON *jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_HREF_NAME);
383 VERIFY_NON_NULL(TAG, jsonRsrcObj, ERROR);
384 VERIFY_SUCCESS(TAG, cJSON_String == jsonRsrcObj->type, ERROR);
386 jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
387 rsrc->href = (char*)OICMalloc(jsonRsrcObjLen);
388 VERIFY_NON_NULL(TAG, (rsrc->href), ERROR);
389 OICStrcpy(rsrc->href, jsonRsrcObjLen, jsonRsrcObj->valuestring);
392 jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_REL_NAME);
395 jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
396 rsrc->rel = (char*)OICMalloc(jsonRsrcObjLen);
397 VERIFY_NON_NULL(TAG, (rsrc->rel), ERROR);
398 OICStrcpy(rsrc->rel, jsonRsrcObjLen, jsonRsrcObj->valuestring);
402 jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_RT_NAME);
403 if(jsonRsrcObj && cJSON_Array == jsonRsrcObj->type)
405 rsrc->typeLen = (size_t)cJSON_GetArraySize(jsonRsrcObj);
406 VERIFY_SUCCESS(TAG, (0 < rsrc->typeLen), ERROR);
407 rsrc->types = (char**)OICCalloc(rsrc->typeLen, sizeof(char*));
408 VERIFY_NON_NULL(TAG, (rsrc->types), ERROR);
409 for(size_t i = 0; i < rsrc->typeLen; i++)
411 cJSON *jsonRsrcType = cJSON_GetArrayItem(jsonRsrcObj, i);
412 rsrc->types[i] = OICStrdup(jsonRsrcType->valuestring);
413 VERIFY_NON_NULL(TAG, (rsrc->types[i]), ERROR);
418 jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_IF_NAME);
419 if(jsonRsrcObj && cJSON_Array == jsonRsrcObj->type)
421 rsrc->interfaceLen = (size_t)cJSON_GetArraySize(jsonRsrcObj);
422 VERIFY_SUCCESS(TAG, (0 < rsrc->interfaceLen), ERROR);
423 rsrc->interfaces = (char**)OICCalloc(rsrc->interfaceLen, sizeof(char*));
424 VERIFY_NON_NULL(TAG, (rsrc->interfaces), ERROR);
425 for(size_t i = 0; i < rsrc->interfaceLen; i++)
427 cJSON *jsonInterface = cJSON_GetArrayItem(jsonRsrcObj, i);
428 rsrc->interfaces[i] = OICStrdup(jsonInterface->valuestring);
429 VERIFY_NON_NULL(TAG, (rsrc->interfaces[i]), ERROR);
433 LL_APPEND(ace->resources, rsrc);
436 // Permissions -- Mandatory
437 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_PERMISSION_NAME);
438 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
439 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
440 ace->permission = jsonObj->valueint;
442 //Validity -- Not Mandatory
443 cJSON *jsonValidityObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_VALIDITY_NAME);
446 VERIFY_SUCCESS(TAG, cJSON_Array == jsonValidityObj->type, ERROR);
447 size_t validityLen = cJSON_GetArraySize(jsonValidityObj);
448 VERIFY_SUCCESS(TAG, (0 < validityLen), ERROR);
450 cJSON *jsonValidity = NULL;
451 for(size_t i = 0; i < validityLen; i++)
453 jsonValidity = cJSON_GetArrayItem(jsonValidityObj, i);
454 VERIFY_NON_NULL(TAG, jsonValidity, ERROR);
455 VERIFY_SUCCESS(TAG, (jsonValidity->type == cJSON_Array), ERROR);
457 OicSecValidity_t* validity = (OicSecValidity_t*)OICCalloc(1, sizeof(OicSecValidity_t));
458 VERIFY_NON_NULL(TAG, validity, ERROR);
459 LL_APPEND(ace->validities, validity);
462 cJSON* jsonPeriod = cJSON_GetArrayItem(jsonValidity, 0);
465 VERIFY_SUCCESS(TAG, (cJSON_String == jsonPeriod->type), ERROR);
467 jsonObjLen = strlen(jsonPeriod->valuestring) + 1;
468 validity->period = (char*)OICMalloc(jsonObjLen);
469 VERIFY_NON_NULL(TAG, validity->period, ERROR);
470 OICStrcpy(validity->period, jsonObjLen, jsonPeriod->valuestring);
474 cJSON* jsonRecurObj = cJSON_GetArrayItem(jsonValidity, 1);
477 VERIFY_SUCCESS(TAG, (cJSON_Array == jsonRecurObj->type), ERROR);
478 validity->recurrenceLen = cJSON_GetArraySize(jsonRecurObj);
479 VERIFY_SUCCESS(TAG, (0 < validity->recurrenceLen), ERROR);
481 validity->recurrences = (char**)OICCalloc(validity->recurrenceLen, sizeof(char*));
482 VERIFY_NON_NULL(TAG, validity->recurrences, ERROR);
484 cJSON *jsonRecur = NULL;
485 for(size_t i = 0; i < validity->recurrenceLen; i++)
487 jsonRecur = cJSON_GetArrayItem(jsonRecurObj, i);
488 VERIFY_NON_NULL(TAG, jsonRecur, ERROR);
489 jsonObjLen = strlen(jsonRecur->valuestring) + 1;
490 validity->recurrences[i] = (char*)OICMalloc(jsonObjLen);
491 VERIFY_NON_NULL(TAG, validity->recurrences[i], ERROR);
492 OICStrcpy(validity->recurrences[i], jsonObjLen, jsonRecur->valuestring);
497 } while( ++idx < numAcl);
502 jsonAclObj = cJSON_GetObjectItem(jsonAclMap, OIC_JSON_ROWNERID_NAME);
503 VERIFY_NON_NULL(TAG, jsonAclObj, ERROR);
504 VERIFY_SUCCESS(TAG, cJSON_String == jsonAclObj->type, ERROR);
505 ret = ConvertStrToUuid(jsonAclObj->valuestring, &headAcl->rownerID);
506 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
511 cJSON_Delete(jsonRoot);
512 if (OC_STACK_OK != ret)
514 DeleteACLList(headAcl);
520 OicSecDoxm_t* JSONToDoxmBin(const char * jsonStr)
522 printf("IN JSONToDoxmBin\n");
528 OCStackResult ret = OC_STACK_ERROR;
529 OicSecDoxm_t *doxm = NULL;
530 cJSON *jsonDoxm = NULL;
531 cJSON *jsonObj = NULL;
533 size_t jsonObjLen = 0;
535 cJSON *jsonRoot = cJSON_Parse(jsonStr);
536 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
538 jsonDoxm = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME);
539 VERIFY_NON_NULL(TAG, jsonDoxm, ERROR);
541 doxm = (OicSecDoxm_t *)OICCalloc(1, sizeof(OicSecDoxm_t));
542 VERIFY_NON_NULL(TAG, doxm, ERROR);
544 //OxmType -- not Mandatory
545 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_TYPE_NAME);
546 if ((jsonObj) && (cJSON_Array == jsonObj->type))
548 doxm->oxmTypeLen = (size_t)cJSON_GetArraySize(jsonObj);
549 VERIFY_SUCCESS(TAG, doxm->oxmTypeLen > 0, ERROR);
551 doxm->oxmType = (OicUrn_t *)OICCalloc(doxm->oxmTypeLen, sizeof(char *));
552 VERIFY_NON_NULL(TAG, (doxm->oxmType), ERROR);
554 for (size_t i = 0; i < doxm->oxmTypeLen ; i++)
556 cJSON *jsonOxmTy = cJSON_GetArrayItem(jsonObj, i);
557 VERIFY_NON_NULL(TAG, jsonOxmTy, ERROR);
559 jsonObjLen = strlen(jsonOxmTy->valuestring) + 1;
560 doxm->oxmType[i] = (char*)OICMalloc(jsonObjLen);
561 VERIFY_NON_NULL(TAG, doxm->oxmType[i], ERROR);
562 strncpy((char *)doxm->oxmType[i], (char *)jsonOxmTy->valuestring, jsonObjLen);
566 //Oxm -- not Mandatory
567 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXMS_NAME);
568 if (jsonObj && cJSON_Array == jsonObj->type)
570 doxm->oxmLen = (size_t)cJSON_GetArraySize(jsonObj);
571 VERIFY_SUCCESS(TAG, doxm->oxmLen > 0, ERROR);
573 doxm->oxm = (OicSecOxm_t*)OICCalloc(doxm->oxmLen, sizeof(OicSecOxm_t));
574 VERIFY_NON_NULL(TAG, doxm->oxm, ERROR);
576 for (size_t i = 0; i < doxm->oxmLen ; i++)
578 cJSON *jsonOxm = cJSON_GetArrayItem(jsonObj, i);
579 VERIFY_NON_NULL(TAG, jsonOxm, ERROR);
580 doxm->oxm[i] = (OicSecOxm_t)jsonOxm->valueint;
584 //OxmSel -- Mandatory
585 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_SEL_NAME);
588 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
589 doxm->oxmSel = (OicSecOxm_t)jsonObj->valueint;
593 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_SUPPORTED_CRED_TYPE_NAME);
596 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
597 doxm->sct = (OicSecCredType_t)jsonObj->valueint;
601 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OWNED_NAME);
604 VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type), ERROR);
605 doxm->owned = jsonObj->valueint;
609 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DPC_NAME);
612 VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type), ERROR);
613 doxm->dpc = jsonObj->valueint;
616 //DeviceId -- Mandatory
617 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVICE_ID_NAME);
620 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
621 if (cJSON_String == jsonObj->type)
623 //Check for empty string, in case DeviceId field has not been set yet
624 if (jsonObj->valuestring[0])
626 ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->deviceID);
627 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
632 //rowner -- Mandatory
633 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_ROWNERID_NAME);
634 if (true == doxm->owned)
636 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
640 ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->rownerID);
641 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
644 //Owner -- will be empty when device status is unowned.
645 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVOWNERID_NAME);
646 if (true == doxm->owned)
648 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
652 ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->owner);
653 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
659 cJSON_Delete(jsonRoot);
660 if (OC_STACK_OK != ret)
662 DeleteDoxmBinData(doxm);
665 printf("OUT JSONToDoxmBin\n");
669 OicSecPstat_t* JSONToPstatBin(const char * jsonStr)
671 printf("IN JSONToPstatBin\n");
677 OCStackResult ret = OC_STACK_ERROR;
678 OicSecPstat_t *pstat = NULL;
679 cJSON *jsonPstat = NULL;
680 cJSON *jsonObj = NULL;
682 cJSON *jsonRoot = cJSON_Parse(jsonStr);
683 VERIFY_NON_NULL(TAG, jsonRoot, INFO);
685 jsonPstat = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
686 VERIFY_NON_NULL(TAG, jsonPstat, INFO);
688 pstat = (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
689 VERIFY_NON_NULL(TAG, pstat, INFO);
690 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_ISOP_NAME);
691 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
692 VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type) , ERROR);
693 pstat->isOp = jsonObj->valueint;
695 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_DEVICE_ID_NAME);
696 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
697 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
698 ret = ConvertStrToUuid(jsonObj->valuestring, &pstat->deviceID);
699 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
701 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_ROWNERID_NAME);
702 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
703 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
704 ret = ConvertStrToUuid(jsonObj->valuestring, &pstat->rownerID);
705 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
707 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_CM_NAME);
708 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
709 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
710 pstat->cm = (OicSecDpm_t)jsonObj->valueint;
712 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_TM_NAME);
713 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
714 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
715 pstat->tm = (OicSecDpm_t)jsonObj->valueint;
717 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_OM_NAME);
718 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
719 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
720 pstat->om = (OicSecDpom_t)jsonObj->valueint;
722 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_SM_NAME);
723 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
724 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
726 pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
727 pstat->sm[0] = (OicSecDpom_t)jsonObj->valueint;
732 cJSON_Delete(jsonRoot);
733 if (OC_STACK_OK != ret)
735 OIC_LOG(ERROR, TAG, "JSONToPstatBin failed");
737 printf("OUT JSONToPstatBin\n");
741 OicSecCred_t * JSONToCredBin(const char * jsonStr)
745 OIC_LOG(ERROR, TAG,"JSONToCredBin jsonStr in NULL");
749 OicSecCred_t *headCred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
750 OCStackResult ret = OC_STACK_ERROR;
751 cJSON *jsonRoot = NULL;
752 VERIFY_NON_NULL(TAG, headCred, ERROR);
754 jsonRoot = cJSON_Parse(jsonStr);
755 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
757 cJSON *jsonCredMap = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
758 VERIFY_NON_NULL(TAG, jsonCredMap, ERROR);
761 cJSON *jsonCredArray = NULL;
762 jsonCredArray = cJSON_GetObjectItem(jsonCredMap, OIC_JSON_CREDS_NAME);
763 VERIFY_NON_NULL(TAG, jsonCredArray, ERROR);
765 if (cJSON_Array == jsonCredArray->type)
767 int numCred = cJSON_GetArraySize(jsonCredArray);
768 VERIFY_SUCCESS(TAG, numCred > 0, ERROR);
772 cJSON *jsonCred = cJSON_GetArrayItem(jsonCredArray, idx);
773 VERIFY_NON_NULL(TAG, jsonCred, ERROR);
775 OicSecCred_t *cred = NULL;
782 cred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
783 OicSecCred_t *temp = headCred;
790 VERIFY_NON_NULL(TAG, cred, ERROR);
792 size_t jsonObjLen = 0;
793 cJSON *jsonObj = NULL;
795 //CredId -- Mandatory
796 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDID_NAME);
799 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
800 cred->credId = jsonObj->valueint;
803 //subject -- Mandatory
804 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_SUBJECTID_NAME);
805 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
806 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
807 ret = ConvertStrToUuid(jsonObj->valuestring, &cred->subject);
808 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
810 //CredType -- Mandatory
811 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDTYPE_NAME);
812 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
813 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
814 cred->credType = (OicSecCredType_t)jsonObj->valueint;
815 //PrivateData is mandatory for some of the credential types listed below.
816 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PRIVATEDATA_NAME);
820 cJSON *jsonPriv = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
821 VERIFY_NON_NULL(TAG, jsonPriv, ERROR);
822 jsonObjLen = strlen(jsonPriv->valuestring);
823 cred->privateData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
824 VERIFY_NON_NULL(TAG, (cred->privateData.data), ERROR);
825 memcpy(cred->privateData.data, jsonPriv->valuestring, jsonObjLen);
826 cred->privateData.len = jsonObjLen;
828 cJSON *jsonEncoding = cJSON_GetObjectItem(jsonObj, OIC_JSON_ENCODING_NAME);
829 VERIFY_NON_NULL(TAG, jsonEncoding, ERROR);
831 if(strcmp(OIC_SEC_ENCODING_RAW, jsonEncoding->valuestring) == 0)
833 cred->privateData.encoding = OIC_ENCODING_RAW;
835 else if(strcmp(OIC_SEC_ENCODING_BASE64, jsonEncoding->valuestring) == 0)
837 cred->privateData.encoding = OIC_ENCODING_BASE64;
841 printf("Unknow encoding type dectected!\n");
842 printf("json2cbor will use \"oic.sec.encoding.raw\" as default encoding type.\n");
843 cred->privateData.encoding = OIC_ENCODING_RAW;
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);
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;
860 #endif // __WITH_X509__
861 //Period -- Not Mandatory
862 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PERIOD_NAME);
863 if(jsonObj && cJSON_String == jsonObj->type)
865 jsonObjLen = strlen(jsonObj->valuestring) + 1;
866 cred->period = (char *)OICMalloc(jsonObjLen);
867 VERIFY_NON_NULL(TAG, cred->period, ERROR);
868 strncpy(cred->period, jsonObj->valuestring, jsonObjLen);
871 } while( ++idx < numCred);
875 cJSON *jsonCredObj = cJSON_GetObjectItem(jsonCredMap, OIC_JSON_ROWNERID_NAME);
876 VERIFY_NON_NULL(TAG, jsonCredObj, ERROR);
877 VERIFY_SUCCESS(TAG, cJSON_String == jsonCredObj->type, ERROR);
878 ret = ConvertStrToUuid(jsonCredObj->valuestring, &headCred->rownerID);
879 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
884 if (OC_STACK_OK != ret)
886 DeleteCredList(headCred);
892 static OicSecSvc_t* JSONToSvcBin(const char * jsonStr)
894 OCStackResult ret = OC_STACK_ERROR;
895 OicSecSvc_t * headSvc = NULL;
896 OicSecSvc_t * prevSvc = NULL;
897 cJSON *jsonRoot = NULL;
898 cJSON *jsonSvcArray = NULL;
900 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
902 jsonRoot = cJSON_Parse(jsonStr);
903 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
905 jsonSvcArray = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME);
906 VERIFY_NON_NULL(TAG, jsonSvcArray, INFO);
908 if (cJSON_Array == jsonSvcArray->type)
910 int numSvc = cJSON_GetArraySize(jsonSvcArray);
913 VERIFY_SUCCESS(TAG, numSvc > 0, INFO);
916 cJSON *jsonSvc = cJSON_GetArrayItem(jsonSvcArray, idx);
917 VERIFY_NON_NULL(TAG, jsonSvc, ERROR);
919 OicSecSvc_t *svc = (OicSecSvc_t*)OICCalloc(1, sizeof(OicSecSvc_t));
920 VERIFY_NON_NULL(TAG, svc, ERROR);
922 headSvc = (headSvc) ? headSvc : svc;
928 cJSON *jsonObj = NULL;
929 unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
931 B64Result b64Ret = B64_OK;
933 // Service Device Identity
934 jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_DEVICE_ID);
935 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
936 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
938 b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
939 sizeof(base64Buff), &outLen);
940 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->svcdid.id)), ERROR);
941 memcpy(svc->svcdid.id, base64Buff, outLen);
944 jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_TYPE);
945 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
946 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
947 svc->svct = (OicSecSvcType_t)jsonObj->valueint;
950 jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_OWNERS_NAME);
951 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
952 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
954 svc->ownersLen = (size_t)cJSON_GetArraySize(jsonObj);
955 VERIFY_SUCCESS(TAG, svc->ownersLen > 0, ERROR);
956 svc->owners = (OicUuid_t*)OICCalloc(svc->ownersLen, sizeof(OicUuid_t));
957 VERIFY_NON_NULL(TAG, (svc->owners), ERROR);
962 cJSON *jsonOwnr = cJSON_GetArrayItem(jsonObj, idxx);
963 VERIFY_NON_NULL(TAG, jsonOwnr, ERROR);
964 VERIFY_SUCCESS(TAG, cJSON_String == jsonOwnr->type, ERROR);
966 b64Ret = b64Decode(jsonOwnr->valuestring, strlen(jsonOwnr->valuestring), base64Buff,
967 sizeof(base64Buff), &outLen);
969 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->owners[idxx].id)),
971 memcpy(svc->owners[idxx].id, base64Buff, outLen);
972 } while ( ++idxx < svc->ownersLen);
975 } while( ++idx < numSvc);
981 cJSON_Delete(jsonRoot);
982 if (OC_STACK_OK != ret)
984 DeleteSVCList(headSvc);
990 static OicSecAmacl_t* JSONToAmaclBin(const char * jsonStr)
992 OCStackResult ret = OC_STACK_ERROR;
993 OicSecAmacl_t * headAmacl = (OicSecAmacl_t*)OICCalloc(1, sizeof(OicSecAmacl_t));
995 cJSON *jsonRoot = NULL;
996 cJSON *jsonAmacl = NULL;
998 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
1000 jsonRoot = cJSON_Parse(jsonStr);
1001 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
1003 jsonAmacl = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
1004 VERIFY_NON_NULL(TAG, jsonAmacl, INFO);
1006 cJSON *jsonObj = NULL;
1008 // Resources -- Mandatory
1009 jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_RESOURCES_NAME);
1010 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1013 cJSON *jsonRlistArray = cJSON_GetObjectItem(jsonObj, OIC_JSON_RLIST_NAME);
1014 VERIFY_NON_NULL(TAG, jsonRlistArray, ERROR);
1015 VERIFY_SUCCESS(TAG, cJSON_Array == jsonRlistArray->type, ERROR);
1017 headAmacl->resourcesLen = (size_t)cJSON_GetArraySize(jsonRlistArray);
1018 headAmacl->resources = (char**)OICCalloc(headAmacl->resourcesLen, sizeof(char*));
1022 cJSON *jsonRsrc = cJSON_GetArrayItem(jsonRlistArray, idxx);
1023 VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
1025 cJSON *jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_HREF_NAME);
1026 VERIFY_NON_NULL(TAG, jsonRsrcObj, ERROR);
1027 VERIFY_SUCCESS(TAG, cJSON_String == jsonRsrcObj->type, ERROR);
1029 size_t jsonRsrcObjLen = 0;
1030 jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
1031 headAmacl->resources[idxx] = (char*)OICMalloc(jsonRsrcObjLen);
1032 VERIFY_NON_NULL(TAG, (headAmacl->resources[idxx]), ERROR);
1033 OICStrcpy(headAmacl->resources[idxx], jsonRsrcObjLen, jsonRsrcObj->valuestring);
1035 } while ( ++idxx < headAmacl->resourcesLen);
1038 jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_AMS_NAME);
1039 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1040 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
1042 headAmacl->amssLen = (size_t)cJSON_GetArraySize(jsonObj);
1043 VERIFY_SUCCESS(TAG, headAmacl->amssLen > 0, ERROR);
1044 headAmacl->amss = (OicUuid_t*)OICCalloc(headAmacl->amssLen, sizeof(OicUuid_t));
1045 VERIFY_NON_NULL(TAG, headAmacl->amss, ERROR);
1050 cJSON *jsonAms = cJSON_GetArrayItem(jsonObj, idxx);
1051 VERIFY_NON_NULL(TAG, jsonAms, ERROR);
1052 VERIFY_SUCCESS(TAG, cJSON_String == jsonAms->type, ERROR);
1054 memcpy(headAmacl->amss[idxx].id, (OicUuid_t *)jsonAms->valuestring, strlen(jsonAms->valuestring));
1056 } while ( ++idxx < headAmacl->amssLen);
1059 // Rowner -- Mandatory
1060 jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_ROWNERID_NAME);
1061 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1062 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
1064 ret = ConvertStrToUuid(jsonObj->valuestring, &headAmacl->rownerID);
1065 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
1070 cJSON_Delete(jsonRoot);
1071 if (OC_STACK_OK != ret)
1073 DeleteAmaclList(headAmacl);
1079 int main(int argc, char* argv[])
1083 printf("JSON File Name: %s\n CBOR File Name: %s \n", argv[1], argv[2]);
1084 ConvertJsonToCBOR(argv[1], argv[2]);
1088 printf("This program requires two inputs:\n");
1089 printf("1. First input is a json file tha will be converted to cbor. \n");
1090 printf("2. Second input is a resulting cbor file that will store converted cbor. \n");
1091 printf("\t json2cbor <json_file_name> <cbor_file_name>. \n");