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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
25 #include "cainterface.h"
27 #include "oic_malloc.h"
28 #include "oic_string.h"
29 #include "ocpayload.h"
30 #include "ocpayloadcbor.h"
31 #include "payload_logging.h"
32 #include "secureresourcemanager.h"
33 #include "srmresourcestrings.h"
34 #include "srmutility.h"
35 #include "aclresource.h"
36 #include "pstatresource.h"
37 #include "doxmresource.h"
38 #include "amaclresource.h"
39 #include "credresource.h"
40 #include "svcresource.h"
41 #include "security_internals.h"
43 #define TAG "JSON2CBOR"
44 #define MAX_RANGE ((size_t)-1)
45 //SVR database buffer block size
46 static const size_t DB_FILE_SIZE_BLOCK = 1023;
48 static OicSecPstat_t* JSONToPstatBin(const char * jsonStr);
49 static OicSecDoxm_t* JSONToDoxmBin(const char * jsonStr);
50 static OicSecAcl_t *JSONToAclBin(const char * jsonStr);
51 static OicSecSvc_t* JSONToSvcBin(const char * jsonStr);
52 static OicSecAmacl_t* JSONToAmaclBin(const char * jsonStr);
53 static OicSecCred_t* JSONToCredBin(const char * jsonStr);
55 static size_t GetJSONFileSize(const char *jsonFileName)
59 char buffer[DB_FILE_SIZE_BLOCK];
60 FILE* fp = fopen(jsonFileName, "r");
65 bytesRead = fread(buffer, 1, DB_FILE_SIZE_BLOCK, fp);
66 if (size + bytesRead > MAX_RANGE)
72 } while (bytesRead > 0);
78 static void ConvertJsonToCBOR(const char *jsonFileName, const char *cborFileName)
83 uint8_t *aclCbor = NULL;
84 uint8_t *pstatCbor = NULL;
85 uint8_t *doxmCbor = NULL;
86 uint8_t *amaclCbor = NULL;
87 uint8_t *svcCbor = NULL;
88 uint8_t *credCbor = NULL;
89 cJSON *jsonRoot = NULL;
90 OCStackResult ret = OC_STACK_ERROR;
91 size_t size = GetJSONFileSize(jsonFileName);
94 OIC_LOG (ERROR, TAG, "Failed converting to JSON");
98 fp = fopen(jsonFileName, "r");
101 jsonStr = (char *)OICMalloc(size + 1);
102 VERIFY_NON_NULL(TAG, jsonStr, FATAL);
103 size_t bytesRead = fread(jsonStr, 1, size, fp);
104 jsonStr[bytesRead] = '\0';
106 OIC_LOG_V(DEBUG, TAG, "Read %zu bytes", bytesRead);
112 OIC_LOG (ERROR, TAG, "Unable to open JSON file!!");
116 jsonRoot = cJSON_Parse(jsonStr);
118 cJSON *value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_ACL_NAME);
119 //printf("ACL json : \n%s\n", cJSON_PrintUnformatted(value));
120 size_t aclCborSize = 0;
123 OicSecAcl_t *acl = JSONToAclBin(jsonStr);
124 VERIFY_NON_NULL(TAG, acl, FATAL);
125 ret = AclToCBORPayload(acl, &aclCbor, &aclCborSize);
126 if(OC_STACK_OK != ret)
128 OIC_LOG (ERROR, TAG, "Failed converting Acl to Cbor Payload");
132 printf("ACL Cbor Size: %zd\n", aclCborSize);
136 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
137 size_t pstatCborSize = 0;
140 OicSecPstat_t *pstat = JSONToPstatBin(jsonStr);
141 VERIFY_NON_NULL(TAG, pstat, FATAL);
142 ret = PstatToCBORPayload(pstat, &pstatCbor, &pstatCborSize);
143 if(OC_STACK_OK != ret)
145 OIC_LOG (ERROR, TAG, "Failed converting Pstat to Cbor Payload");
146 DeletePstatBinData(pstat);
149 printf("PSTAT Cbor Size: %zd\n", pstatCborSize);
150 DeletePstatBinData(pstat);
152 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME);
153 size_t doxmCborSize = 0;
156 OicSecDoxm_t *doxm = JSONToDoxmBin(jsonStr);
157 VERIFY_NON_NULL(TAG, doxm, FATAL);
158 ret = DoxmToCBORPayload(doxm, &doxmCbor, &doxmCborSize);
159 if(OC_STACK_OK != ret)
161 OIC_LOG (ERROR, TAG, "Failed converting Doxm to Cbor Payload");
162 DeleteDoxmBinData(doxm);
165 printf("DOXM Cbor Size: %zd\n", doxmCborSize);
166 DeleteDoxmBinData(doxm);
168 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
169 size_t amaclCborSize = 0;
172 OicSecAmacl_t *amacl = JSONToAmaclBin(jsonStr);
173 VERIFY_NON_NULL(TAG, amacl, FATAL);
174 ret = AmaclToCBORPayload(amacl, &amaclCbor, &amaclCborSize);
175 if(OC_STACK_OK != ret)
177 OIC_LOG (ERROR, TAG, "Failed converting Amacl to Cbor Payload");
178 DeleteAmaclList(amacl);
181 printf("AMACL Cbor Size: %zd\n", amaclCborSize);
182 DeleteAmaclList(amacl);
184 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME);
185 size_t svcCborSize = 0;
188 OicSecSvc_t *svc = JSONToSvcBin(jsonStr);
189 VERIFY_NON_NULL(TAG, svc, FATAL);
190 ret = SVCToCBORPayload(svc, &svcCbor, &svcCborSize);
191 if(OC_STACK_OK != ret)
193 OIC_LOG (ERROR, TAG, "Failed converting Svc to Cbor Payload");
197 printf("SVC Cbor Size: %zd\n", svcCborSize);
200 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
201 //printf("CRED json : \n%s\n", cJSON_PrintUnformatted(value));
202 size_t credCborSize = 0;
205 OicSecCred_t *cred = JSONToCredBin(jsonStr);
206 VERIFY_NON_NULL(TAG, cred, FATAL);
207 ret = CredToCBORPayload(cred, &credCbor, &credCborSize);
208 if(OC_STACK_OK != ret)
210 OIC_LOG (ERROR, TAG, "Failed converting Cred to Cbor Payload");
211 DeleteCredList(cred);
214 printf("CRED Cbor Size: %zd\n", credCborSize);
215 DeleteCredList(cred);
218 CborEncoder encoder = { {.ptr = NULL }, .end = 0 };
219 size_t cborSize = aclCborSize + pstatCborSize + doxmCborSize + svcCborSize + credCborSize + amaclCborSize;
221 printf("Total Cbor Size : %zd\n", cborSize);
222 cborSize += 255; // buffer margin for adding map and byte string
223 uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborSize);
224 VERIFY_NON_NULL(TAG, outPayload, ERROR);
225 cbor_encoder_init(&encoder, outPayload, cborSize, 0);
226 CborEncoder map = { {.ptr = NULL }, .end = 0 };
227 CborError cborEncoderResult = cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
228 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Creating Main Map.");
231 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
232 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
233 cborEncoderResult = cbor_encode_byte_string(&map, aclCbor, aclCborSize);
234 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
237 if (pstatCborSize > 0)
239 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
240 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
241 cborEncoderResult = cbor_encode_byte_string(&map, pstatCbor, pstatCborSize);
242 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
244 if (doxmCborSize > 0)
246 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
247 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Name.");
248 cborEncoderResult = cbor_encode_byte_string(&map, doxmCbor, doxmCborSize);
249 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Value.");
251 if (amaclCborSize > 0)
253 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_AMACL_NAME, strlen(OIC_JSON_AMACL_NAME));
254 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Name.");
255 cborEncoderResult = cbor_encode_byte_string(&map, amaclCbor, amaclCborSize);
256 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Value.");
260 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_SVC_NAME, strlen(OIC_JSON_SVC_NAME));
261 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Name.");
262 cborEncoderResult = cbor_encode_byte_string(&map, svcCbor, svcCborSize);
263 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Value.");
265 if (credCborSize > 0)
267 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME));
268 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Name.");
269 cborEncoderResult = cbor_encode_byte_string(&map, credCbor, credCborSize);
270 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Value.");
273 cborEncoderResult = cbor_encoder_close_container(&encoder, &map);
274 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Container.");
276 size_t s = encoder.ptr - outPayload;
277 OIC_LOG_V(DEBUG, TAG, "Payload size %zu", s);
279 fp1 = fopen(cborFileName, "w");
282 size_t bytesWritten = fwrite(outPayload, 1, s, fp1);
283 if (bytesWritten == s)
285 OIC_LOG_V(DEBUG, TAG, "Written %zu bytes", bytesWritten);
289 OIC_LOG_V(ERROR, TAG, "Failed writing %zu bytes", s);
296 cJSON_Delete(jsonRoot);
307 OicSecAcl_t* JSONToAclBin(const char * jsonStr)
309 OCStackResult ret = OC_STACK_ERROR;
310 OicSecAcl_t * headAcl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
311 cJSON *jsonRoot = NULL;
313 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
315 jsonRoot = cJSON_Parse(jsonStr);
316 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
318 cJSON *jsonAclMap = cJSON_GetObjectItem(jsonRoot, OIC_JSON_ACL_NAME);
319 VERIFY_NON_NULL(TAG, jsonAclMap, ERROR);
321 cJSON *jsonAclObj = NULL;
324 jsonAclObj = cJSON_GetObjectItem(jsonAclMap, OIC_JSON_ACLIST_NAME);
325 VERIFY_NON_NULL(TAG, jsonAclObj, ERROR);
328 cJSON *jsonAclArray = NULL;
329 jsonAclArray = cJSON_GetObjectItem(jsonAclObj, OIC_JSON_ACES_NAME);
330 VERIFY_NON_NULL(TAG, jsonAclArray, ERROR);
332 if (cJSON_Array == jsonAclArray->type)
335 int numAcl = cJSON_GetArraySize(jsonAclArray);
338 VERIFY_SUCCESS(TAG, numAcl > 0, INFO);
341 cJSON *jsonAcl = cJSON_GetArrayItem(jsonAclArray, idx);
342 VERIFY_NON_NULL(TAG, jsonAcl, ERROR);
344 OicSecAcl_t *acl = NULL;
351 acl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
352 OicSecAcl_t *temp = headAcl;
360 VERIFY_NON_NULL(TAG, acl, ERROR);
362 size_t jsonObjLen = 0;
363 cJSON *jsonObj = NULL;
364 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_SUBJECTID_NAME);
365 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
366 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
367 if(strcmp(jsonObj->valuestring, WILDCARD_RESOURCE_URI) == 0)
369 acl->subject.id[0] = '*';
373 ret = ConvertStrToUuid(jsonObj->valuestring, &acl->subject);
374 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
376 // Resources -- Mandatory
377 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_RESOURCES_NAME);
378 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
379 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
381 acl->resourcesLen = (size_t)cJSON_GetArraySize(jsonObj);
383 VERIFY_SUCCESS(TAG, acl->resourcesLen > 0, ERROR);
384 acl->resources = (char**)OICCalloc(acl->resourcesLen, sizeof(char*));
385 VERIFY_NON_NULL(TAG, (acl->resources), ERROR);
390 cJSON *jsonRsrc = cJSON_GetArrayItem(jsonObj, idxx);
391 VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
393 size_t jsonRsrcObjLen = 0;
394 cJSON *jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_HREF_NAME);
395 VERIFY_NON_NULL(TAG, jsonRsrcObj, ERROR);
396 VERIFY_SUCCESS(TAG, cJSON_String == jsonRsrcObj->type, ERROR);
398 jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
399 acl->resources[idxx] = (char*)OICMalloc(jsonRsrcObjLen);
401 VERIFY_NON_NULL(TAG, (acl->resources[idxx]), ERROR);
402 OICStrcpy(acl->resources[idxx], jsonRsrcObjLen, jsonRsrcObj->valuestring);
404 } while ( ++idxx < acl->resourcesLen);
406 // Permissions -- Mandatory
407 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_PERMISSION_NAME);
408 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
409 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
410 acl->permission = jsonObj->valueint;
411 //Period -- Not Mandatory
412 cJSON *jsonPeriodObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_PERIOD_NAME);
415 VERIFY_SUCCESS(TAG, cJSON_Array == jsonPeriodObj->type, ERROR);
416 acl->prdRecrLen = (size_t)cJSON_GetArraySize(jsonPeriodObj);
417 if(acl->prdRecrLen > 0)
419 acl->periods = (char**)OICCalloc(acl->prdRecrLen, sizeof(char*));
420 VERIFY_NON_NULL(TAG, acl->periods, ERROR);
422 cJSON *jsonPeriod = NULL;
423 for(size_t i = 0; i < acl->prdRecrLen; i++)
425 jsonPeriod = cJSON_GetArrayItem(jsonPeriodObj, i);
426 VERIFY_NON_NULL(TAG, jsonPeriod, ERROR);
428 jsonObjLen = strlen(jsonPeriod->valuestring) + 1;
429 acl->periods[i] = (char*)OICMalloc(jsonObjLen);
430 VERIFY_NON_NULL(TAG, acl->periods[i], ERROR);
431 OICStrcpy(acl->periods[i], jsonObjLen, jsonPeriod->valuestring);
435 //Recurrence -- Not mandatory
436 cJSON *jsonRecurObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_RECURRENCES_NAME);
440 VERIFY_SUCCESS(TAG, cJSON_Array == jsonRecurObj->type, ERROR);
442 if(acl->prdRecrLen > 0)
444 acl->recurrences = (char**)OICCalloc(acl->prdRecrLen, sizeof(char*));
445 VERIFY_NON_NULL(TAG, acl->recurrences, ERROR);
447 cJSON *jsonRecur = NULL;
448 for(size_t i = 0; i < acl->prdRecrLen; i++)
450 jsonRecur = cJSON_GetArrayItem(jsonRecurObj, i);
451 VERIFY_NON_NULL(TAG, jsonRecur, ERROR);
452 jsonObjLen = strlen(jsonRecur->valuestring) + 1;
453 acl->recurrences[i] = (char*)OICMalloc(jsonObjLen);
454 VERIFY_NON_NULL(TAG, acl->recurrences[i], ERROR);
455 OICStrcpy(acl->recurrences[i], jsonObjLen, jsonRecur->valuestring);
462 } while( ++idx < numAcl);
467 jsonAclObj = cJSON_GetObjectItem(jsonAclMap, OIC_JSON_ROWNERID_NAME);
468 VERIFY_NON_NULL(TAG, jsonAclObj, ERROR);
469 VERIFY_SUCCESS(TAG, cJSON_String == jsonAclObj->type, ERROR);
470 ret = ConvertStrToUuid(jsonAclObj->valuestring, &headAcl->rownerID);
471 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
476 cJSON_Delete(jsonRoot);
477 if (OC_STACK_OK != ret)
479 DeleteACLList(headAcl);
485 OicSecDoxm_t* JSONToDoxmBin(const char * jsonStr)
487 printf("IN JSONToDoxmBin\n");
493 OCStackResult ret = OC_STACK_ERROR;
494 OicSecDoxm_t *doxm = NULL;
495 cJSON *jsonDoxm = NULL;
496 cJSON *jsonObj = NULL;
498 size_t jsonObjLen = 0;
500 cJSON *jsonRoot = cJSON_Parse(jsonStr);
501 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
503 jsonDoxm = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME);
504 VERIFY_NON_NULL(TAG, jsonDoxm, ERROR);
506 doxm = (OicSecDoxm_t *)OICCalloc(1, sizeof(OicSecDoxm_t));
507 VERIFY_NON_NULL(TAG, doxm, ERROR);
509 //OxmType -- not Mandatory
510 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_TYPE_NAME);
511 if ((jsonObj) && (cJSON_Array == jsonObj->type))
513 doxm->oxmTypeLen = (size_t)cJSON_GetArraySize(jsonObj);
514 VERIFY_SUCCESS(TAG, doxm->oxmTypeLen > 0, ERROR);
516 doxm->oxmType = (OicUrn_t *)OICCalloc(doxm->oxmTypeLen, sizeof(char *));
517 VERIFY_NON_NULL(TAG, (doxm->oxmType), ERROR);
519 for (size_t i = 0; i < doxm->oxmTypeLen ; i++)
521 cJSON *jsonOxmTy = cJSON_GetArrayItem(jsonObj, i);
522 VERIFY_NON_NULL(TAG, jsonOxmTy, ERROR);
524 jsonObjLen = strlen(jsonOxmTy->valuestring) + 1;
525 doxm->oxmType[i] = (char*)OICMalloc(jsonObjLen);
526 VERIFY_NON_NULL(TAG, doxm->oxmType[i], ERROR);
527 strncpy((char *)doxm->oxmType[i], (char *)jsonOxmTy->valuestring, jsonObjLen);
531 //Oxm -- not Mandatory
532 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXMS_NAME);
533 if (jsonObj && cJSON_Array == jsonObj->type)
535 doxm->oxmLen = (size_t)cJSON_GetArraySize(jsonObj);
536 VERIFY_SUCCESS(TAG, doxm->oxmLen > 0, ERROR);
538 doxm->oxm = (OicSecOxm_t*)OICCalloc(doxm->oxmLen, sizeof(OicSecOxm_t));
539 VERIFY_NON_NULL(TAG, doxm->oxm, ERROR);
541 for (size_t i = 0; i < doxm->oxmLen ; i++)
543 cJSON *jsonOxm = cJSON_GetArrayItem(jsonObj, i);
544 VERIFY_NON_NULL(TAG, jsonOxm, ERROR);
545 doxm->oxm[i] = (OicSecOxm_t)jsonOxm->valueint;
549 //OxmSel -- Mandatory
550 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_SEL_NAME);
553 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
554 doxm->oxmSel = (OicSecOxm_t)jsonObj->valueint;
558 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_SUPPORTED_CRED_TYPE_NAME);
561 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
562 doxm->sct = (OicSecCredType_t)jsonObj->valueint;
566 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OWNED_NAME);
569 VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type), ERROR);
570 doxm->owned = jsonObj->valueint;
574 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DPC_NAME);
577 VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type), ERROR);
578 doxm->dpc = jsonObj->valueint;
581 //DeviceId -- Mandatory
582 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVICE_ID_NAME);
585 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
586 if (cJSON_String == jsonObj->type)
588 //Check for empty string, in case DeviceId field has not been set yet
589 if (jsonObj->valuestring[0])
591 ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->deviceID);
592 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
597 //rowner -- Mandatory
598 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_ROWNERID_NAME);
599 if (true == doxm->owned)
601 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
605 ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->rownerID);
606 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
609 //Owner -- will be empty when device status is unowned.
610 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVOWNERID_NAME);
611 if (true == doxm->owned)
613 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
617 ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->owner);
618 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
624 cJSON_Delete(jsonRoot);
625 if (OC_STACK_OK != ret)
627 DeleteDoxmBinData(doxm);
630 printf("OUT JSONToDoxmBin\n");
634 OicSecPstat_t* JSONToPstatBin(const char * jsonStr)
636 printf("IN JSONToPstatBin\n");
642 OCStackResult ret = OC_STACK_ERROR;
643 OicSecPstat_t *pstat = NULL;
644 cJSON *jsonPstat = NULL;
645 cJSON *jsonObj = NULL;
647 cJSON *jsonRoot = cJSON_Parse(jsonStr);
648 VERIFY_NON_NULL(TAG, jsonRoot, INFO);
650 jsonPstat = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
651 VERIFY_NON_NULL(TAG, jsonPstat, INFO);
653 pstat = (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
654 VERIFY_NON_NULL(TAG, pstat, INFO);
655 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_ISOP_NAME);
656 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
657 VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type) , ERROR);
658 pstat->isOp = jsonObj->valueint;
660 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_DEVICE_ID_NAME);
661 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
662 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
663 ret = ConvertStrToUuid(jsonObj->valuestring, &pstat->deviceID);
664 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
666 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_ROWNERID_NAME);
667 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
668 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
669 ret = ConvertStrToUuid(jsonObj->valuestring, &pstat->rownerID);
670 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
672 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_CM_NAME);
673 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
674 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
675 pstat->cm = (OicSecDpm_t)jsonObj->valueint;
677 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_TM_NAME);
678 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
679 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
680 pstat->tm = (OicSecDpm_t)jsonObj->valueint;
682 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_OM_NAME);
683 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
684 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
685 pstat->om = (OicSecDpom_t)jsonObj->valueint;
687 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_SM_NAME);
688 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
689 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
691 pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
692 pstat->sm[0] = (OicSecDpom_t)jsonObj->valueint;
697 cJSON_Delete(jsonRoot);
698 if (OC_STACK_OK != ret)
700 OIC_LOG(ERROR, TAG, "JSONToPstatBin failed");
702 printf("OUT JSONToPstatBin\n");
706 OicSecCred_t * JSONToCredBin(const char * jsonStr)
709 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
710 OicSecCred_t *headCred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
712 OCStackResult ret = OC_STACK_ERROR;
713 cJSON *jsonRoot = NULL;
715 jsonRoot = cJSON_Parse(jsonStr);
716 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
718 cJSON *jsonCredMap = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
719 VERIFY_NON_NULL(TAG, jsonCredMap, ERROR);
722 cJSON *jsonCredArray = NULL;
723 jsonCredArray = cJSON_GetObjectItem(jsonCredMap, OIC_JSON_CREDS_NAME);
724 VERIFY_NON_NULL(TAG, jsonCredArray, ERROR);
726 if (cJSON_Array == jsonCredArray->type)
728 int numCred = cJSON_GetArraySize(jsonCredArray);
729 VERIFY_SUCCESS(TAG, numCred > 0, ERROR);
731 size_t ownersLen = 0;
734 cJSON *jsonCred = cJSON_GetArrayItem(jsonCredArray, idx);
735 VERIFY_NON_NULL(TAG, jsonCred, ERROR);
737 OicSecCred_t *cred = NULL;
744 cred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
745 OicSecCred_t *temp = headCred;
752 VERIFY_NON_NULL(TAG, cred, ERROR);
754 size_t jsonObjLen = 0;
755 cJSON *jsonObj = NULL;
757 //CredId -- Mandatory
758 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDID_NAME);
761 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
762 cred->credId = jsonObj->valueint;
765 //subject -- Mandatory
766 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_SUBJECTID_NAME);
767 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
768 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
769 ret = ConvertStrToUuid(jsonObj->valuestring, &cred->subject);
770 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
772 //CredType -- Mandatory
773 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDTYPE_NAME);
774 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
775 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
776 cred->credType = (OicSecCredType_t)jsonObj->valueint;
777 //PrivateData is mandatory for some of the credential types listed below.
778 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PRIVATEDATA_NAME);
782 cJSON *jsonPriv = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
783 VERIFY_NON_NULL(TAG, jsonPriv, ERROR);
784 jsonObjLen = strlen(jsonPriv->valuestring);
785 cred->privateData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
786 VERIFY_NON_NULL(TAG, (cred->privateData.data), ERROR);
787 memcpy(cred->privateData.data, jsonPriv->valuestring, jsonObjLen);
788 cred->privateData.len = jsonObjLen;
791 //PublicData is mandatory only for SIGNED_ASYMMETRIC_KEY credentials type.
792 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PUBLICDATA_NAME);
796 cJSON *jsonPub = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
797 VERIFY_NON_NULL(TAG, jsonPub, ERROR);
798 jsonObjLen = strlen(jsonPub->valuestring);
799 cred->publicData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
800 VERIFY_NON_NULL(TAG, (cred->publicData.data), ERROR);
801 memcpy(cred->publicData.data, jsonPub->valuestring, jsonObjLen);
802 cred->publicData.len = jsonObjLen;
804 #endif // __WITH_X509__
805 //Period -- Not Mandatory
806 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PERIOD_NAME);
807 if(jsonObj && cJSON_String == jsonObj->type)
809 jsonObjLen = strlen(jsonObj->valuestring) + 1;
810 cred->period = (char *)OICMalloc(jsonObjLen);
811 VERIFY_NON_NULL(TAG, cred->period, ERROR);
812 strncpy(cred->period, jsonObj->valuestring, jsonObjLen);
815 } while( ++idx < numCred);
819 cJSON *jsonCredObj = cJSON_GetObjectItem(jsonCredMap, OIC_JSON_ROWNERID_NAME);
820 VERIFY_NON_NULL(TAG, jsonCredObj, ERROR);
821 VERIFY_SUCCESS(TAG, cJSON_String == jsonCredObj->type, ERROR);
822 ret = ConvertStrToUuid(jsonCredObj->valuestring, &headCred->rownerID);
823 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
828 if (OC_STACK_OK != ret)
830 DeleteCredList(headCred);
836 static OicSecSvc_t* JSONToSvcBin(const char * jsonStr)
838 OCStackResult ret = OC_STACK_ERROR;
839 OicSecSvc_t * headSvc = NULL;
840 OicSecSvc_t * prevSvc = NULL;
841 cJSON *jsonRoot = NULL;
842 cJSON *jsonSvcArray = NULL;
844 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
846 jsonRoot = cJSON_Parse(jsonStr);
847 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
849 jsonSvcArray = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME);
850 VERIFY_NON_NULL(TAG, jsonSvcArray, INFO);
852 if (cJSON_Array == jsonSvcArray->type)
854 int numSvc = cJSON_GetArraySize(jsonSvcArray);
857 VERIFY_SUCCESS(TAG, numSvc > 0, INFO);
860 cJSON *jsonSvc = cJSON_GetArrayItem(jsonSvcArray, idx);
861 VERIFY_NON_NULL(TAG, jsonSvc, ERROR);
863 OicSecSvc_t *svc = (OicSecSvc_t*)OICCalloc(1, sizeof(OicSecSvc_t));
864 VERIFY_NON_NULL(TAG, svc, ERROR);
866 headSvc = (headSvc) ? headSvc : svc;
872 cJSON *jsonObj = NULL;
873 unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
875 B64Result b64Ret = B64_OK;
877 // Service Device Identity
878 jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_DEVICE_ID);
879 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
880 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
882 b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
883 sizeof(base64Buff), &outLen);
884 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->svcdid.id)), ERROR);
885 memcpy(svc->svcdid.id, base64Buff, outLen);
888 jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_TYPE);
889 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
890 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
891 svc->svct = (OicSecSvcType_t)jsonObj->valueint;
894 jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_OWNERS_NAME);
895 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
896 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
898 svc->ownersLen = (size_t)cJSON_GetArraySize(jsonObj);
899 VERIFY_SUCCESS(TAG, svc->ownersLen > 0, ERROR);
900 svc->owners = (OicUuid_t*)OICCalloc(svc->ownersLen, sizeof(OicUuid_t));
901 VERIFY_NON_NULL(TAG, (svc->owners), ERROR);
906 cJSON *jsonOwnr = cJSON_GetArrayItem(jsonObj, idxx);
907 VERIFY_NON_NULL(TAG, jsonOwnr, ERROR);
908 VERIFY_SUCCESS(TAG, cJSON_String == jsonOwnr->type, ERROR);
910 b64Ret = b64Decode(jsonOwnr->valuestring, strlen(jsonOwnr->valuestring), base64Buff,
911 sizeof(base64Buff), &outLen);
913 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->owners[idxx].id)),
915 memcpy(svc->owners[idxx].id, base64Buff, outLen);
916 } while ( ++idxx < svc->ownersLen);
919 } while( ++idx < numSvc);
925 cJSON_Delete(jsonRoot);
926 if (OC_STACK_OK != ret)
928 DeleteSVCList(headSvc);
934 static OicSecAmacl_t* JSONToAmaclBin(const char * jsonStr)
936 OCStackResult ret = OC_STACK_ERROR;
937 OicSecAmacl_t * headAmacl = (OicSecAmacl_t*)OICCalloc(1, sizeof(OicSecAmacl_t));
939 cJSON *jsonRoot = NULL;
940 cJSON *jsonAmacl = NULL;
942 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
944 jsonRoot = cJSON_Parse(jsonStr);
945 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
947 jsonAmacl = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
948 VERIFY_NON_NULL(TAG, jsonAmacl, INFO);
950 cJSON *jsonObj = NULL;
952 // Resources -- Mandatory
953 jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_RESOURCES_NAME);
954 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
957 cJSON *jsonRlistArray = cJSON_GetObjectItem(jsonObj, OIC_JSON_RLIST_NAME);
958 VERIFY_NON_NULL(TAG, jsonRlistArray, ERROR);
959 VERIFY_SUCCESS(TAG, cJSON_Array == jsonRlistArray->type, ERROR);
961 headAmacl->resourcesLen = (size_t)cJSON_GetArraySize(jsonRlistArray);
962 headAmacl->resources = (char**)OICCalloc(headAmacl->resourcesLen, sizeof(char*));
966 cJSON *jsonRsrc = cJSON_GetArrayItem(jsonRlistArray, idxx);
967 VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
969 cJSON *jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_HREF_NAME);
970 VERIFY_NON_NULL(TAG, jsonRsrcObj, ERROR);
971 VERIFY_SUCCESS(TAG, cJSON_String == jsonRsrcObj->type, ERROR);
973 size_t jsonRsrcObjLen = 0;
974 jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
975 headAmacl->resources[idxx] = (char*)OICMalloc(jsonRsrcObjLen);
976 VERIFY_NON_NULL(TAG, (headAmacl->resources[idxx]), ERROR);
977 OICStrcpy(headAmacl->resources[idxx], jsonRsrcObjLen, jsonRsrcObj->valuestring);
979 } while ( ++idxx < headAmacl->resourcesLen);
982 jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_AMS_NAME);
983 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
984 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
986 headAmacl->amssLen = (size_t)cJSON_GetArraySize(jsonObj);
987 VERIFY_SUCCESS(TAG, headAmacl->amssLen > 0, ERROR);
988 headAmacl->amss = (OicUuid_t*)OICCalloc(headAmacl->amssLen, sizeof(OicUuid_t));
989 VERIFY_NON_NULL(TAG, headAmacl->amss, ERROR);
994 cJSON *jsonAms = cJSON_GetArrayItem(jsonObj, idxx);
995 VERIFY_NON_NULL(TAG, jsonAms, ERROR);
996 VERIFY_SUCCESS(TAG, cJSON_String == jsonAms->type, ERROR);
998 memcpy(headAmacl->amss[idxx].id, (OicUuid_t *)jsonAms->valuestring, strlen(jsonAms->valuestring));
1000 } while ( ++idxx < headAmacl->amssLen);
1003 // Rowner -- Mandatory
1004 jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_ROWNERID_NAME);
1005 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1006 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
1008 ret = ConvertStrToUuid(jsonObj->valuestring, &headAmacl->rownerID);
1009 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
1014 cJSON_Delete(jsonRoot);
1015 if (OC_STACK_OK != ret)
1017 DeleteAmaclList(headAmacl);
1023 int main(int argc, char* argv[])
1027 printf("JSON File Name: %s\n CBOR File Name: %s \n", argv[1], argv[2]);
1028 ConvertJsonToCBOR(argv[1], argv[2]);
1032 printf("This program requires two inputs:\n");
1033 printf("1. First input is a json file tha will be converted to cbor. \n");
1034 printf("2. Second input is a resulting cbor file that will store converted cbor. \n");
1035 printf("\t json2cbor <json_file_name> <cbor_file_name>. \n");