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 (bytesRead >=(MAX_RANGE - size))
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 jsonStr = (char *)OICMalloc(size + 1);
99 VERIFY_NON_NULL(TAG, jsonStr, FATAL);
101 fp = fopen(jsonFileName, "r");
104 size_t bytesRead = fread(jsonStr, 1, size, fp);
105 jsonStr[bytesRead] = '\0';
107 OIC_LOG_V(DEBUG, TAG, "Read %zu bytes", bytesRead);
113 OIC_LOG (ERROR, TAG, "Unable to open JSON file!!");
117 jsonRoot = cJSON_Parse(jsonStr);
119 cJSON *value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_ACL_NAME);
120 //printf("ACL json : \n%s\n", cJSON_PrintUnformatted(value));
121 size_t aclCborSize = 0;
124 OicSecAcl_t *acl = JSONToAclBin(jsonStr);
125 VERIFY_NON_NULL(TAG, acl, FATAL);
126 ret = AclToCBORPayload(acl, &aclCbor, &aclCborSize);
127 if(OC_STACK_OK != ret)
129 OIC_LOG (ERROR, TAG, "Failed converting Acl to Cbor Payload");
133 printf("ACL Cbor Size: %zd\n", aclCborSize);
137 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
138 size_t pstatCborSize = 0;
141 OicSecPstat_t *pstat = JSONToPstatBin(jsonStr);
142 VERIFY_NON_NULL(TAG, pstat, FATAL);
143 ret = PstatToCBORPayload(pstat, &pstatCbor, &pstatCborSize);
144 if(OC_STACK_OK != ret)
146 OIC_LOG (ERROR, TAG, "Failed converting Pstat to Cbor Payload");
147 DeletePstatBinData(pstat);
150 printf("PSTAT Cbor Size: %zd\n", pstatCborSize);
151 DeletePstatBinData(pstat);
153 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME);
154 size_t doxmCborSize = 0;
157 OicSecDoxm_t *doxm = JSONToDoxmBin(jsonStr);
158 VERIFY_NON_NULL(TAG, doxm, FATAL);
159 ret = DoxmToCBORPayload(doxm, &doxmCbor, &doxmCborSize);
160 if(OC_STACK_OK != ret)
162 OIC_LOG (ERROR, TAG, "Failed converting Doxm to Cbor Payload");
163 DeleteDoxmBinData(doxm);
166 printf("DOXM Cbor Size: %zd\n", doxmCborSize);
167 DeleteDoxmBinData(doxm);
169 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
170 size_t amaclCborSize = 0;
173 OicSecAmacl_t *amacl = JSONToAmaclBin(jsonStr);
174 VERIFY_NON_NULL(TAG, amacl, FATAL);
175 ret = AmaclToCBORPayload(amacl, &amaclCbor, &amaclCborSize);
176 if(OC_STACK_OK != ret)
178 OIC_LOG (ERROR, TAG, "Failed converting Amacl to Cbor Payload");
179 DeleteAmaclList(amacl);
182 printf("AMACL Cbor Size: %zd\n", amaclCborSize);
183 DeleteAmaclList(amacl);
185 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME);
186 size_t svcCborSize = 0;
189 OicSecSvc_t *svc = JSONToSvcBin(jsonStr);
190 VERIFY_NON_NULL(TAG, svc, FATAL);
191 ret = SVCToCBORPayload(svc, &svcCbor, &svcCborSize);
192 if(OC_STACK_OK != ret)
194 OIC_LOG (ERROR, TAG, "Failed converting Svc to Cbor Payload");
198 printf("SVC Cbor Size: %zd\n", svcCborSize);
201 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
202 //printf("CRED json : \n%s\n", cJSON_PrintUnformatted(value));
203 size_t credCborSize = 0;
206 OicSecCred_t *cred = JSONToCredBin(jsonStr);
207 VERIFY_NON_NULL(TAG, cred, FATAL);
208 ret = CredToCBORPayload(cred, &credCbor, &credCborSize);
209 if(OC_STACK_OK != ret)
211 OIC_LOG (ERROR, TAG, "Failed converting Cred to Cbor Payload");
212 DeleteCredList(cred);
215 printf("CRED Cbor Size: %zd\n", credCborSize);
216 DeleteCredList(cred);
220 size_t cborSize = aclCborSize + pstatCborSize + doxmCborSize + svcCborSize + credCborSize + amaclCborSize;
222 printf("Total Cbor Size : %zd\n", cborSize);
223 cborSize += 255; // buffer margin for adding map and byte string
224 uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborSize);
225 VERIFY_NON_NULL(TAG, outPayload, ERROR);
226 cbor_encoder_init(&encoder, outPayload, cborSize, 0);
228 CborError cborEncoderResult = cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
229 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Creating Main Map.");
232 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
233 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
234 cborEncoderResult = cbor_encode_byte_string(&map, aclCbor, aclCborSize);
235 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
238 if (pstatCborSize > 0)
240 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
241 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
242 cborEncoderResult = cbor_encode_byte_string(&map, pstatCbor, pstatCborSize);
243 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
245 if (doxmCborSize > 0)
247 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
248 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Name.");
249 cborEncoderResult = cbor_encode_byte_string(&map, doxmCbor, doxmCborSize);
250 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Value.");
252 if (amaclCborSize > 0)
254 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_AMACL_NAME, strlen(OIC_JSON_AMACL_NAME));
255 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Name.");
256 cborEncoderResult = cbor_encode_byte_string(&map, amaclCbor, amaclCborSize);
257 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Value.");
261 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_SVC_NAME, strlen(OIC_JSON_SVC_NAME));
262 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Name.");
263 cborEncoderResult = cbor_encode_byte_string(&map, svcCbor, svcCborSize);
264 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Value.");
266 if (credCborSize > 0)
268 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME));
269 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Name.");
270 cborEncoderResult = cbor_encode_byte_string(&map, credCbor, credCborSize);
271 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Value.");
274 cborEncoderResult = cbor_encoder_close_container(&encoder, &map);
275 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Container.");
277 size_t s = encoder.ptr - outPayload;
278 OIC_LOG_V(DEBUG, TAG, "Payload size %zu", s);
280 fp1 = fopen(cborFileName, "w");
283 size_t bytesWritten = fwrite(outPayload, 1, s, fp1);
284 if (bytesWritten == s)
286 OIC_LOG_V(DEBUG, TAG, "Written %zu bytes", bytesWritten);
290 OIC_LOG_V(ERROR, TAG, "Failed writing %zu bytes", s);
297 cJSON_Delete(jsonRoot);
308 OicSecAcl_t* JSONToAclBin(const char * jsonStr)
310 OCStackResult ret = OC_STACK_ERROR;
311 OicSecAcl_t * headAcl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
312 cJSON *jsonRoot = NULL;
314 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
316 jsonRoot = cJSON_Parse(jsonStr);
317 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
319 cJSON *jsonAclMap = cJSON_GetObjectItem(jsonRoot, OIC_JSON_ACL_NAME);
320 VERIFY_NON_NULL(TAG, jsonAclMap, ERROR);
322 cJSON *jsonAclObj = NULL;
325 jsonAclObj = cJSON_GetObjectItem(jsonAclMap, OIC_JSON_ACLIST_NAME);
326 VERIFY_NON_NULL(TAG, jsonAclObj, ERROR);
329 cJSON *jsonAclArray = NULL;
330 jsonAclArray = cJSON_GetObjectItem(jsonAclObj, OIC_JSON_ACES_NAME);
331 VERIFY_NON_NULL(TAG, jsonAclArray, ERROR);
333 if (cJSON_Array == jsonAclArray->type)
336 int numAcl = cJSON_GetArraySize(jsonAclArray);
339 VERIFY_SUCCESS(TAG, numAcl > 0, INFO);
342 cJSON *jsonAcl = cJSON_GetArrayItem(jsonAclArray, idx);
343 VERIFY_NON_NULL(TAG, jsonAcl, ERROR);
345 OicSecAcl_t *acl = NULL;
352 acl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
353 OicSecAcl_t *temp = headAcl;
361 VERIFY_NON_NULL(TAG, acl, ERROR);
363 size_t jsonObjLen = 0;
364 cJSON *jsonObj = NULL;
365 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_SUBJECTID_NAME);
366 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
367 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
368 if(strcmp(jsonObj->valuestring, WILDCARD_RESOURCE_URI) == 0)
370 acl->subject.id[0] = '*';
374 ret = ConvertStrToUuid(jsonObj->valuestring, &acl->subject);
375 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
377 // Resources -- Mandatory
378 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_RESOURCES_NAME);
379 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
380 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
382 acl->resourcesLen = (size_t)cJSON_GetArraySize(jsonObj);
384 VERIFY_SUCCESS(TAG, acl->resourcesLen > 0, ERROR);
385 acl->resources = (char**)OICCalloc(acl->resourcesLen, sizeof(char*));
386 VERIFY_NON_NULL(TAG, (acl->resources), ERROR);
391 cJSON *jsonRsrc = cJSON_GetArrayItem(jsonObj, idxx);
392 VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
394 size_t jsonRsrcObjLen = 0;
395 cJSON *jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_HREF_NAME);
396 VERIFY_NON_NULL(TAG, jsonRsrcObj, ERROR);
397 VERIFY_SUCCESS(TAG, cJSON_String == jsonRsrcObj->type, ERROR);
399 jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
400 acl->resources[idxx] = (char*)OICMalloc(jsonRsrcObjLen);
402 VERIFY_NON_NULL(TAG, (acl->resources[idxx]), ERROR);
403 OICStrcpy(acl->resources[idxx], jsonRsrcObjLen, jsonRsrcObj->valuestring);
405 } while ( ++idxx < acl->resourcesLen);
407 // Permissions -- Mandatory
408 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_PERMISSION_NAME);
409 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
410 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
411 acl->permission = jsonObj->valueint;
412 //Period -- Not Mandatory
413 cJSON *jsonPeriodObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_PERIOD_NAME);
416 VERIFY_SUCCESS(TAG, cJSON_Array == jsonPeriodObj->type, ERROR);
417 acl->prdRecrLen = (size_t)cJSON_GetArraySize(jsonPeriodObj);
418 if(acl->prdRecrLen > 0)
420 acl->periods = (char**)OICCalloc(acl->prdRecrLen, sizeof(char*));
421 VERIFY_NON_NULL(TAG, acl->periods, ERROR);
423 cJSON *jsonPeriod = NULL;
424 for(size_t i = 0; i < acl->prdRecrLen; i++)
426 jsonPeriod = cJSON_GetArrayItem(jsonPeriodObj, i);
427 VERIFY_NON_NULL(TAG, jsonPeriod, ERROR);
429 jsonObjLen = strlen(jsonPeriod->valuestring) + 1;
430 acl->periods[i] = (char*)OICMalloc(jsonObjLen);
431 VERIFY_NON_NULL(TAG, acl->periods[i], ERROR);
432 OICStrcpy(acl->periods[i], jsonObjLen, jsonPeriod->valuestring);
436 //Recurrence -- Not mandatory
437 cJSON *jsonRecurObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_RECURRENCES_NAME);
441 VERIFY_SUCCESS(TAG, cJSON_Array == jsonRecurObj->type, ERROR);
443 if(acl->prdRecrLen > 0)
445 acl->recurrences = (char**)OICCalloc(acl->prdRecrLen, sizeof(char*));
446 VERIFY_NON_NULL(TAG, acl->recurrences, ERROR);
448 cJSON *jsonRecur = NULL;
449 for(size_t i = 0; i < acl->prdRecrLen; i++)
451 jsonRecur = cJSON_GetArrayItem(jsonRecurObj, i);
452 VERIFY_NON_NULL(TAG, jsonRecur, ERROR);
453 jsonObjLen = strlen(jsonRecur->valuestring) + 1;
454 acl->recurrences[i] = (char*)OICMalloc(jsonObjLen);
455 VERIFY_NON_NULL(TAG, acl->recurrences[i], ERROR);
456 OICStrcpy(acl->recurrences[i], jsonObjLen, jsonRecur->valuestring);
463 } while( ++idx < numAcl);
468 jsonAclObj = cJSON_GetObjectItem(jsonAclMap, OIC_JSON_ROWNERID_NAME);
469 VERIFY_NON_NULL(TAG, jsonAclObj, ERROR);
470 VERIFY_SUCCESS(TAG, cJSON_String == jsonAclObj->type, ERROR);
471 ret = ConvertStrToUuid(jsonAclObj->valuestring, &headAcl->rownerID);
472 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
477 cJSON_Delete(jsonRoot);
478 if (OC_STACK_OK != ret)
480 DeleteACLList(headAcl);
486 OicSecDoxm_t* JSONToDoxmBin(const char * jsonStr)
488 printf("IN JSONToDoxmBin\n");
494 OCStackResult ret = OC_STACK_ERROR;
495 OicSecDoxm_t *doxm = NULL;
496 cJSON *jsonDoxm = NULL;
497 cJSON *jsonObj = NULL;
499 size_t jsonObjLen = 0;
501 cJSON *jsonRoot = cJSON_Parse(jsonStr);
502 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
504 jsonDoxm = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME);
505 VERIFY_NON_NULL(TAG, jsonDoxm, ERROR);
507 doxm = (OicSecDoxm_t *)OICCalloc(1, sizeof(OicSecDoxm_t));
508 VERIFY_NON_NULL(TAG, doxm, ERROR);
510 //OxmType -- not Mandatory
511 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_TYPE_NAME);
512 if ((jsonObj) && (cJSON_Array == jsonObj->type))
514 doxm->oxmTypeLen = (size_t)cJSON_GetArraySize(jsonObj);
515 VERIFY_SUCCESS(TAG, doxm->oxmTypeLen > 0, ERROR);
517 doxm->oxmType = (OicUrn_t *)OICCalloc(doxm->oxmTypeLen, sizeof(char *));
518 VERIFY_NON_NULL(TAG, (doxm->oxmType), ERROR);
520 for (size_t i = 0; i < doxm->oxmTypeLen ; i++)
522 cJSON *jsonOxmTy = cJSON_GetArrayItem(jsonObj, i);
523 VERIFY_NON_NULL(TAG, jsonOxmTy, ERROR);
525 jsonObjLen = strlen(jsonOxmTy->valuestring) + 1;
526 doxm->oxmType[i] = (char*)OICMalloc(jsonObjLen);
527 VERIFY_NON_NULL(TAG, doxm->oxmType[i], ERROR);
528 strncpy((char *)doxm->oxmType[i], (char *)jsonOxmTy->valuestring, jsonObjLen);
532 //Oxm -- not Mandatory
533 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXMS_NAME);
534 if (jsonObj && cJSON_Array == jsonObj->type)
536 doxm->oxmLen = (size_t)cJSON_GetArraySize(jsonObj);
537 VERIFY_SUCCESS(TAG, doxm->oxmLen > 0, ERROR);
539 doxm->oxm = (OicSecOxm_t*)OICCalloc(doxm->oxmLen, sizeof(OicSecOxm_t));
540 VERIFY_NON_NULL(TAG, doxm->oxm, ERROR);
542 for (size_t i = 0; i < doxm->oxmLen ; i++)
544 cJSON *jsonOxm = cJSON_GetArrayItem(jsonObj, i);
545 VERIFY_NON_NULL(TAG, jsonOxm, ERROR);
546 doxm->oxm[i] = (OicSecOxm_t)jsonOxm->valueint;
550 //OxmSel -- Mandatory
551 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_SEL_NAME);
554 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
555 doxm->oxmSel = (OicSecOxm_t)jsonObj->valueint;
559 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_SUPPORTED_CRED_TYPE_NAME);
562 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
563 doxm->sct = (OicSecCredType_t)jsonObj->valueint;
567 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OWNED_NAME);
570 VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type), ERROR);
571 doxm->owned = jsonObj->valueint;
575 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DPC_NAME);
578 VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type), ERROR);
579 doxm->dpc = jsonObj->valueint;
582 //DeviceId -- Mandatory
583 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVICE_ID_NAME);
586 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
587 if (cJSON_String == jsonObj->type)
589 //Check for empty string, in case DeviceId field has not been set yet
590 if (jsonObj->valuestring[0])
592 ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->deviceID);
593 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
598 //rowner -- Mandatory
599 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_ROWNERID_NAME);
600 if (true == doxm->owned)
602 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
606 ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->rownerID);
607 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
610 //Owner -- will be empty when device status is unowned.
611 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVOWNERID_NAME);
612 if (true == doxm->owned)
614 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
618 ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->owner);
619 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
625 cJSON_Delete(jsonRoot);
626 if (OC_STACK_OK != ret)
628 DeleteDoxmBinData(doxm);
631 printf("OUT JSONToDoxmBin\n");
635 OicSecPstat_t* JSONToPstatBin(const char * jsonStr)
637 printf("IN JSONToPstatBin\n");
643 OCStackResult ret = OC_STACK_ERROR;
644 OicSecPstat_t *pstat = NULL;
645 cJSON *jsonPstat = NULL;
646 cJSON *jsonObj = NULL;
648 cJSON *jsonRoot = cJSON_Parse(jsonStr);
649 VERIFY_NON_NULL(TAG, jsonRoot, INFO);
651 jsonPstat = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
652 VERIFY_NON_NULL(TAG, jsonPstat, INFO);
654 pstat = (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
655 VERIFY_NON_NULL(TAG, pstat, INFO);
656 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_ISOP_NAME);
657 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
658 VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type) , ERROR);
659 pstat->isOp = jsonObj->valueint;
661 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_DEVICE_ID_NAME);
662 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
663 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
664 ret = ConvertStrToUuid(jsonObj->valuestring, &pstat->deviceID);
665 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
667 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_ROWNERID_NAME);
668 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
669 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
670 ret = ConvertStrToUuid(jsonObj->valuestring, &pstat->rownerID);
671 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
673 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_CM_NAME);
674 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
675 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
676 pstat->cm = (OicSecDpm_t)jsonObj->valueint;
678 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_TM_NAME);
679 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
680 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
681 pstat->tm = (OicSecDpm_t)jsonObj->valueint;
683 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_OM_NAME);
684 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
685 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
686 pstat->om = (OicSecDpom_t)jsonObj->valueint;
688 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_SM_NAME);
689 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
690 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
692 pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
693 pstat->sm[0] = (OicSecDpom_t)jsonObj->valueint;
698 cJSON_Delete(jsonRoot);
699 if (OC_STACK_OK != ret)
701 OIC_LOG(ERROR, TAG, "JSONToPstatBin failed");
703 printf("OUT JSONToPstatBin\n");
707 OicSecCred_t * JSONToCredBin(const char * jsonStr)
711 OIC_LOG(ERROR, TAG,"JSONToCredBin jsonStr in NULL");
715 OicSecCred_t *headCred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
716 OCStackResult ret = OC_STACK_ERROR;
717 cJSON *jsonRoot = NULL;
718 VERIFY_NON_NULL(TAG, headCred, ERROR);
720 jsonRoot = cJSON_Parse(jsonStr);
721 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
723 cJSON *jsonCredMap = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
724 VERIFY_NON_NULL(TAG, jsonCredMap, ERROR);
727 cJSON *jsonCredArray = NULL;
728 jsonCredArray = cJSON_GetObjectItem(jsonCredMap, OIC_JSON_CREDS_NAME);
729 VERIFY_NON_NULL(TAG, jsonCredArray, ERROR);
731 if (cJSON_Array == jsonCredArray->type)
733 int numCred = cJSON_GetArraySize(jsonCredArray);
734 VERIFY_SUCCESS(TAG, numCred > 0, ERROR);
736 size_t ownersLen = 0;
739 cJSON *jsonCred = cJSON_GetArrayItem(jsonCredArray, idx);
740 VERIFY_NON_NULL(TAG, jsonCred, ERROR);
742 OicSecCred_t *cred = NULL;
749 cred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
750 OicSecCred_t *temp = headCred;
757 VERIFY_NON_NULL(TAG, cred, ERROR);
759 size_t jsonObjLen = 0;
760 cJSON *jsonObj = NULL;
762 //CredId -- Mandatory
763 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDID_NAME);
766 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
767 cred->credId = jsonObj->valueint;
770 //subject -- Mandatory
771 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_SUBJECTID_NAME);
772 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
773 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
774 ret = ConvertStrToUuid(jsonObj->valuestring, &cred->subject);
775 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
777 //CredType -- Mandatory
778 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDTYPE_NAME);
779 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
780 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
781 cred->credType = (OicSecCredType_t)jsonObj->valueint;
782 //PrivateData is mandatory for some of the credential types listed below.
783 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PRIVATEDATA_NAME);
787 cJSON *jsonPriv = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
788 VERIFY_NON_NULL(TAG, jsonPriv, ERROR);
789 jsonObjLen = strlen(jsonPriv->valuestring);
790 cred->privateData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
791 VERIFY_NON_NULL(TAG, (cred->privateData.data), ERROR);
792 memcpy(cred->privateData.data, jsonPriv->valuestring, jsonObjLen);
793 cred->privateData.len = jsonObjLen;
796 //PublicData is mandatory only for SIGNED_ASYMMETRIC_KEY credentials type.
797 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PUBLICDATA_NAME);
801 cJSON *jsonPub = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
802 VERIFY_NON_NULL(TAG, jsonPub, ERROR);
803 jsonObjLen = strlen(jsonPub->valuestring);
804 cred->publicData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
805 VERIFY_NON_NULL(TAG, (cred->publicData.data), ERROR);
806 memcpy(cred->publicData.data, jsonPub->valuestring, jsonObjLen);
807 cred->publicData.len = jsonObjLen;
809 #endif // __WITH_X509__
810 //Period -- Not Mandatory
811 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PERIOD_NAME);
812 if(jsonObj && cJSON_String == jsonObj->type)
814 jsonObjLen = strlen(jsonObj->valuestring) + 1;
815 cred->period = (char *)OICMalloc(jsonObjLen);
816 VERIFY_NON_NULL(TAG, cred->period, ERROR);
817 strncpy(cred->period, jsonObj->valuestring, jsonObjLen);
820 } while( ++idx < numCred);
824 cJSON *jsonCredObj = cJSON_GetObjectItem(jsonCredMap, OIC_JSON_ROWNERID_NAME);
825 VERIFY_NON_NULL(TAG, jsonCredObj, ERROR);
826 VERIFY_SUCCESS(TAG, cJSON_String == jsonCredObj->type, ERROR);
827 ret = ConvertStrToUuid(jsonCredObj->valuestring, &headCred->rownerID);
828 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
833 if (OC_STACK_OK != ret)
835 DeleteCredList(headCred);
841 static OicSecSvc_t* JSONToSvcBin(const char * jsonStr)
843 OCStackResult ret = OC_STACK_ERROR;
844 OicSecSvc_t * headSvc = NULL;
845 OicSecSvc_t * prevSvc = NULL;
846 cJSON *jsonRoot = NULL;
847 cJSON *jsonSvcArray = NULL;
849 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
851 jsonRoot = cJSON_Parse(jsonStr);
852 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
854 jsonSvcArray = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME);
855 VERIFY_NON_NULL(TAG, jsonSvcArray, INFO);
857 if (cJSON_Array == jsonSvcArray->type)
859 int numSvc = cJSON_GetArraySize(jsonSvcArray);
862 VERIFY_SUCCESS(TAG, numSvc > 0, INFO);
865 cJSON *jsonSvc = cJSON_GetArrayItem(jsonSvcArray, idx);
866 VERIFY_NON_NULL(TAG, jsonSvc, ERROR);
868 OicSecSvc_t *svc = (OicSecSvc_t*)OICCalloc(1, sizeof(OicSecSvc_t));
869 VERIFY_NON_NULL(TAG, svc, ERROR);
871 headSvc = (headSvc) ? headSvc : svc;
877 cJSON *jsonObj = NULL;
878 unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
880 B64Result b64Ret = B64_OK;
882 // Service Device Identity
883 jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_DEVICE_ID);
884 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
885 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
887 b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
888 sizeof(base64Buff), &outLen);
889 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->svcdid.id)), ERROR);
890 memcpy(svc->svcdid.id, base64Buff, outLen);
893 jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_TYPE);
894 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
895 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
896 svc->svct = (OicSecSvcType_t)jsonObj->valueint;
899 jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_OWNERS_NAME);
900 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
901 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
903 svc->ownersLen = (size_t)cJSON_GetArraySize(jsonObj);
904 VERIFY_SUCCESS(TAG, svc->ownersLen > 0, ERROR);
905 svc->owners = (OicUuid_t*)OICCalloc(svc->ownersLen, sizeof(OicUuid_t));
906 VERIFY_NON_NULL(TAG, (svc->owners), ERROR);
911 cJSON *jsonOwnr = cJSON_GetArrayItem(jsonObj, idxx);
912 VERIFY_NON_NULL(TAG, jsonOwnr, ERROR);
913 VERIFY_SUCCESS(TAG, cJSON_String == jsonOwnr->type, ERROR);
915 b64Ret = b64Decode(jsonOwnr->valuestring, strlen(jsonOwnr->valuestring), base64Buff,
916 sizeof(base64Buff), &outLen);
918 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->owners[idxx].id)),
920 memcpy(svc->owners[idxx].id, base64Buff, outLen);
921 } while ( ++idxx < svc->ownersLen);
924 } while( ++idx < numSvc);
930 cJSON_Delete(jsonRoot);
931 if (OC_STACK_OK != ret)
933 DeleteSVCList(headSvc);
939 static OicSecAmacl_t* JSONToAmaclBin(const char * jsonStr)
941 OCStackResult ret = OC_STACK_ERROR;
942 OicSecAmacl_t * headAmacl = (OicSecAmacl_t*)OICCalloc(1, sizeof(OicSecAmacl_t));
944 cJSON *jsonRoot = NULL;
945 cJSON *jsonAmacl = NULL;
947 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
949 jsonRoot = cJSON_Parse(jsonStr);
950 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
952 jsonAmacl = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
953 VERIFY_NON_NULL(TAG, jsonAmacl, INFO);
955 cJSON *jsonObj = NULL;
957 // Resources -- Mandatory
958 jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_RESOURCES_NAME);
959 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
962 cJSON *jsonRlistArray = cJSON_GetObjectItem(jsonObj, OIC_JSON_RLIST_NAME);
963 VERIFY_NON_NULL(TAG, jsonRlistArray, ERROR);
964 VERIFY_SUCCESS(TAG, cJSON_Array == jsonRlistArray->type, ERROR);
966 headAmacl->resourcesLen = (size_t)cJSON_GetArraySize(jsonRlistArray);
967 headAmacl->resources = (char**)OICCalloc(headAmacl->resourcesLen, sizeof(char*));
971 cJSON *jsonRsrc = cJSON_GetArrayItem(jsonRlistArray, idxx);
972 VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
974 cJSON *jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_HREF_NAME);
975 VERIFY_NON_NULL(TAG, jsonRsrcObj, ERROR);
976 VERIFY_SUCCESS(TAG, cJSON_String == jsonRsrcObj->type, ERROR);
978 size_t jsonRsrcObjLen = 0;
979 jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
980 headAmacl->resources[idxx] = (char*)OICMalloc(jsonRsrcObjLen);
981 VERIFY_NON_NULL(TAG, (headAmacl->resources[idxx]), ERROR);
982 OICStrcpy(headAmacl->resources[idxx], jsonRsrcObjLen, jsonRsrcObj->valuestring);
984 } while ( ++idxx < headAmacl->resourcesLen);
987 jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_AMS_NAME);
988 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
989 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
991 headAmacl->amssLen = (size_t)cJSON_GetArraySize(jsonObj);
992 VERIFY_SUCCESS(TAG, headAmacl->amssLen > 0, ERROR);
993 headAmacl->amss = (OicUuid_t*)OICCalloc(headAmacl->amssLen, sizeof(OicUuid_t));
994 VERIFY_NON_NULL(TAG, headAmacl->amss, ERROR);
999 cJSON *jsonAms = cJSON_GetArrayItem(jsonObj, idxx);
1000 VERIFY_NON_NULL(TAG, jsonAms, ERROR);
1001 VERIFY_SUCCESS(TAG, cJSON_String == jsonAms->type, ERROR);
1003 memcpy(headAmacl->amss[idxx].id, (OicUuid_t *)jsonAms->valuestring, strlen(jsonAms->valuestring));
1005 } while ( ++idxx < headAmacl->amssLen);
1008 // Rowner -- Mandatory
1009 jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_ROWNERID_NAME);
1010 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1011 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
1013 ret = ConvertStrToUuid(jsonObj->valuestring, &headAmacl->rownerID);
1014 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
1019 cJSON_Delete(jsonRoot);
1020 if (OC_STACK_OK != ret)
1022 DeleteAmaclList(headAmacl);
1028 int main(int argc, char* argv[])
1032 printf("JSON File Name: %s\n CBOR File Name: %s \n", argv[1], argv[2]);
1033 ConvertJsonToCBOR(argv[1], argv[2]);
1037 printf("This program requires two inputs:\n");
1038 printf("1. First input is a json file tha will be converted to cbor. \n");
1039 printf("2. Second input is a resulting cbor file that will store converted cbor. \n");
1040 printf("\t json2cbor <json_file_name> <cbor_file_name>. \n");