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);
219 CborEncoder encoder = { {.ptr = NULL }, .end = 0 };
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);
227 CborEncoder map = { {.ptr = NULL }, .end = 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 OIC_LOG_V(DEBUG, TAG, "Written %zu bytes", bytesWritten);
290 cJSON_Delete(jsonRoot);
301 OicSecAcl_t* JSONToAclBin(const char * jsonStr)
303 OCStackResult ret = OC_STACK_ERROR;
304 OicSecAcl_t * headAcl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
305 cJSON *jsonRoot = NULL;
307 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
309 jsonRoot = cJSON_Parse(jsonStr);
310 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
312 cJSON *jsonAclMap = cJSON_GetObjectItem(jsonRoot, OIC_JSON_ACL_NAME);
313 VERIFY_NON_NULL(TAG, jsonAclMap, ERROR);
315 cJSON *jsonAclObj = NULL;
318 jsonAclObj = cJSON_GetObjectItem(jsonAclMap, OIC_JSON_ACLIST_NAME);
319 VERIFY_NON_NULL(TAG, jsonAclObj, ERROR);
322 cJSON *jsonAclArray = NULL;
323 jsonAclArray = cJSON_GetObjectItem(jsonAclObj, OIC_JSON_ACES_NAME);
324 VERIFY_NON_NULL(TAG, jsonAclArray, ERROR);
326 if (cJSON_Array == jsonAclArray->type)
329 int numAcl = cJSON_GetArraySize(jsonAclArray);
332 VERIFY_SUCCESS(TAG, numAcl > 0, INFO);
335 cJSON *jsonAcl = cJSON_GetArrayItem(jsonAclArray, idx);
336 VERIFY_NON_NULL(TAG, jsonAcl, ERROR);
338 OicSecAcl_t *acl = NULL;
345 acl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
346 OicSecAcl_t *temp = headAcl;
354 VERIFY_NON_NULL(TAG, acl, ERROR);
356 size_t jsonObjLen = 0;
357 cJSON *jsonObj = NULL;
358 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_SUBJECT_NAME);
359 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
360 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
361 if(strcmp(jsonObj->valuestring, WILDCARD_RESOURCE_URI) == 0)
363 acl->subject.id[0] = '*';
367 ret = ConvertStrToUuid(jsonObj->valuestring, &acl->subject);
368 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
370 // Resources -- Mandatory
371 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_RESOURCES_NAME);
372 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
373 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
375 acl->resourcesLen = (size_t)cJSON_GetArraySize(jsonObj);
377 VERIFY_SUCCESS(TAG, acl->resourcesLen > 0, ERROR);
378 acl->resources = (char**)OICCalloc(acl->resourcesLen, sizeof(char*));
379 VERIFY_NON_NULL(TAG, (acl->resources), ERROR);
384 cJSON *jsonRsrc = cJSON_GetArrayItem(jsonObj, idxx);
385 VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
387 size_t jsonRsrcObjLen = 0;
388 cJSON *jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_HREF_NAME);
389 VERIFY_NON_NULL(TAG, jsonRsrcObj, ERROR);
390 VERIFY_SUCCESS(TAG, cJSON_String == jsonRsrcObj->type, ERROR);
392 jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
393 acl->resources[idxx] = (char*)OICMalloc(jsonRsrcObjLen);
395 VERIFY_NON_NULL(TAG, (acl->resources[idxx]), ERROR);
396 OICStrcpy(acl->resources[idxx], jsonRsrcObjLen, jsonRsrcObj->valuestring);
398 } while ( ++idxx < acl->resourcesLen);
400 // Permissions -- Mandatory
401 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_PERMISSION_NAME);
402 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
403 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
404 acl->permission = jsonObj->valueint;
405 //Period -- Not Mandatory
406 cJSON *jsonPeriodObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_PERIOD_NAME);
409 VERIFY_SUCCESS(TAG, cJSON_Array == jsonPeriodObj->type, ERROR);
410 acl->prdRecrLen = (size_t)cJSON_GetArraySize(jsonPeriodObj);
411 if(acl->prdRecrLen > 0)
413 acl->periods = (char**)OICCalloc(acl->prdRecrLen, sizeof(char*));
414 VERIFY_NON_NULL(TAG, acl->periods, ERROR);
416 cJSON *jsonPeriod = NULL;
417 for(size_t i = 0; i < acl->prdRecrLen; i++)
419 jsonPeriod = cJSON_GetArrayItem(jsonPeriodObj, i);
420 VERIFY_NON_NULL(TAG, jsonPeriod, ERROR);
422 jsonObjLen = strlen(jsonPeriod->valuestring) + 1;
423 acl->periods[i] = (char*)OICMalloc(jsonObjLen);
424 VERIFY_NON_NULL(TAG, acl->periods[i], ERROR);
425 OICStrcpy(acl->periods[i], jsonObjLen, jsonPeriod->valuestring);
429 //Recurrence -- Not mandatory
430 cJSON *jsonRecurObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_RECURRENCES_NAME);
434 VERIFY_SUCCESS(TAG, cJSON_Array == jsonRecurObj->type, ERROR);
436 if(acl->prdRecrLen > 0)
438 acl->recurrences = (char**)OICCalloc(acl->prdRecrLen, sizeof(char*));
439 VERIFY_NON_NULL(TAG, acl->recurrences, ERROR);
441 cJSON *jsonRecur = NULL;
442 for(size_t i = 0; i < acl->prdRecrLen; i++)
444 jsonRecur = cJSON_GetArrayItem(jsonRecurObj, i);
445 VERIFY_NON_NULL(TAG, jsonRecur, ERROR);
446 jsonObjLen = strlen(jsonRecur->valuestring) + 1;
447 acl->recurrences[i] = (char*)OICMalloc(jsonObjLen);
448 VERIFY_NON_NULL(TAG, acl->recurrences[i], ERROR);
449 OICStrcpy(acl->recurrences[i], jsonObjLen, jsonRecur->valuestring);
456 } while( ++idx < numAcl);
461 jsonAclObj = cJSON_GetObjectItem(jsonAclMap, OIC_JSON_ROWNERID_NAME);
462 VERIFY_NON_NULL(TAG, jsonAclObj, ERROR);
463 VERIFY_SUCCESS(TAG, cJSON_String == jsonAclObj->type, ERROR);
464 headAcl->ownersLen = 1;
465 VERIFY_SUCCESS(TAG, headAcl->ownersLen > 0, ERROR);
466 headAcl->owners = (OicUuid_t*)OICCalloc(headAcl->ownersLen, sizeof(OicUuid_t));
467 VERIFY_NON_NULL(TAG, (headAcl->owners), ERROR);
468 ret = ConvertStrToUuid(jsonAclObj->valuestring, &headAcl->owners[0]);
469 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
474 cJSON_Delete(jsonRoot);
475 if (OC_STACK_OK != ret)
477 DeleteACLList(headAcl);
483 OicSecDoxm_t* JSONToDoxmBin(const char * jsonStr)
485 printf("IN JSONToDoxmBin\n");
491 OCStackResult ret = OC_STACK_ERROR;
492 OicSecDoxm_t *doxm = NULL;
493 cJSON *jsonDoxm = NULL;
494 cJSON *jsonObj = NULL;
496 size_t jsonObjLen = 0;
497 unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
499 B64Result b64Ret = B64_OK;
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 //DidFormat -- Mandatory
583 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVICE_ID_FORMAT_NAME);
586 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
587 //TODO: handle didformat value
590 //DeviceId -- Mandatory
591 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVICE_ID_NAME);
594 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
595 if (cJSON_String == jsonObj->type)
597 //Check for empty string, in case DeviceId field has not been set yet
598 if (jsonObj->valuestring[0])
600 ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->deviceID);
601 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
606 //rowner -- Mandatory
607 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_ROWNERID_NAME);
608 if (true == doxm->owned)
610 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
614 ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->rownerID);
615 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
618 //Owner -- will be empty when device status is unowned.
619 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVOWNERID_NAME);
620 if (true == doxm->owned)
622 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
626 ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->owner);
627 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
633 cJSON_Delete(jsonRoot);
634 if (OC_STACK_OK != ret)
636 DeleteDoxmBinData(doxm);
639 printf("OUT JSONToDoxmBin\n");
643 OicSecPstat_t* JSONToPstatBin(const char * jsonStr)
645 printf("IN JSONToPstatBin\n");
651 OCStackResult ret = OC_STACK_ERROR;
652 OicSecPstat_t *pstat = NULL;
653 cJSON *jsonPstat = NULL;
654 cJSON *jsonObj = NULL;
656 unsigned char base64Buff[sizeof(((OicUuid_t*) 0)->id)] = {};
658 B64Result b64Ret = B64_OK;
660 cJSON *jsonRoot = cJSON_Parse(jsonStr);
661 VERIFY_NON_NULL(TAG, jsonRoot, INFO);
663 jsonPstat = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
664 VERIFY_NON_NULL(TAG, jsonPstat, INFO);
666 pstat = (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
667 VERIFY_NON_NULL(TAG, pstat, INFO);
668 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_ISOP_NAME);
669 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
670 VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type) , ERROR);
671 pstat->isOp = jsonObj->valueint;
673 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_DEVICE_ID_NAME);
674 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
675 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
676 ret = ConvertStrToUuid(jsonObj->valuestring, &pstat->deviceID);
677 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
679 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_ROWNERID_NAME);
680 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
681 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
682 ret = ConvertStrToUuid(jsonObj->valuestring, &pstat->rownerID);
683 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
685 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_CM_NAME);
686 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
687 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
688 pstat->cm = (OicSecDpm_t)jsonObj->valueint;
690 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_TM_NAME);
691 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
692 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
693 pstat->tm = (OicSecDpm_t)jsonObj->valueint;
695 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_OM_NAME);
696 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
697 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
698 pstat->om = (OicSecDpom_t)jsonObj->valueint;
700 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_SM_NAME);
701 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
702 if (cJSON_Array == jsonObj->type)
704 pstat->smLen = (size_t)cJSON_GetArraySize(jsonObj);
706 VERIFY_SUCCESS(TAG, pstat->smLen != 0, ERROR);
707 pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
708 VERIFY_NON_NULL(TAG, pstat->sm, ERROR);
711 cJSON *jsonSm = cJSON_GetArrayItem(jsonObj, idxx);
712 VERIFY_NON_NULL(TAG, jsonSm, ERROR);
713 pstat->sm[idxx] = (OicSecDpom_t)jsonSm->valueint;
714 } while ( ++idxx < pstat->smLen);
719 cJSON_Delete(jsonRoot);
720 if (OC_STACK_OK != ret)
722 OIC_LOG(ERROR, TAG, "JSONToPstatBin failed");
724 printf("OUT JSONToPstatBin\n");
728 OicSecCred_t * JSONToCredBin(const char * jsonStr)
730 OCStackResult ret = OC_STACK_ERROR;
731 OicSecCred_t * headCred = NULL;
732 OicSecCred_t * prevCred = NULL;
733 cJSON *jsonCredArray = NULL;
735 cJSON *jsonRoot = cJSON_Parse(jsonStr);
736 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
738 jsonCredArray = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
739 VERIFY_NON_NULL(TAG, jsonCredArray, ERROR);
740 if (cJSON_Array == jsonCredArray->type)
742 int numCred = cJSON_GetArraySize(jsonCredArray);
743 VERIFY_SUCCESS(TAG, numCred > 0, ERROR);
744 unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
746 B64Result b64Ret = B64_OK;
750 cJSON *jsonCred = cJSON_GetArrayItem(jsonCredArray, idx);
751 VERIFY_NON_NULL(TAG, jsonCred, ERROR);
753 OicSecCred_t *cred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
754 VERIFY_NON_NULL(TAG, cred, ERROR);
756 headCred = (headCred) ? headCred : cred;
759 prevCred->next = cred;
761 size_t jsonObjLen = 0;
762 cJSON *jsonObj = NULL;
764 //CredId -- Mandatory
765 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDID_NAME);
768 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
769 cred->credId = jsonObj->valueint;
772 //subject -- Mandatory
773 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_SUBJECT_NAME);
774 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
775 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
777 memset(base64Buff, 0, sizeof(base64Buff));
778 b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring),
779 base64Buff, sizeof(base64Buff), &outLen);
780 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(cred->subject.id)),
782 memcpy(cred->subject.id, base64Buff, outLen);
784 //CredType -- Mandatory
785 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDTYPE_NAME);
786 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
787 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
788 cred->credType = (OicSecCredType_t)jsonObj->valueint;
790 //PrivateData is mandatory for some of the credential types listed below.
791 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PRIVATEDATA_NAME);
792 if ((cred->credType & SYMMETRIC_PAIR_WISE_KEY) ||
793 (cred->credType & SYMMETRIC_GROUP_KEY) ||
794 (cred->credType & PIN_PASSWORD))
796 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
797 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
800 else if (cred->credType & SIGNED_ASYMMETRIC_KEY)
802 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
803 VERIFY_SUCCESS(TAG, cJSON_Object == jsonObj->type, ERROR);
805 #endif // __WITH_X509__
808 if (cJSON_String == jsonObj->type)
810 jsonObjLen = strlen(jsonObj->valuestring) + 1;
811 cred->privateData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
812 VERIFY_NON_NULL(TAG, (cred->privateData.data), ERROR);
814 uint8_t pskKey[OWNER_PSK_LENGTH_256] = {};
816 memset(pskKey, 0, sizeof(pskKey));
817 b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring),
818 pskKey, sizeof(pskKey), &outLen);
819 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK &&
820 outLen <= OWNER_PSK_LENGTH_256), ERROR);
821 memcpy(cred->privateData.data, pskKey, outLen);
822 cred->privateData.len = outLen;
825 else if (SIGNED_ASYMMETRIC_KEY == cred->credType && cJSON_Object == jsonObj->type)
827 cred->privateData.data = cJSON_PrintUnformatted(jsonObj);
828 VERIFY_NON_NULL(TAG, (cred->privateData.data), ERROR);
830 #endif // __WITH_X509__
833 //PublicData is mandatory only for SIGNED_ASYMMETRIC_KEY credentials type.
834 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PUBLICDATA_NAME);
835 if (cred->credType & SIGNED_ASYMMETRIC_KEY)
837 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
838 VERIFY_SUCCESS(TAG, cJSON_Object == jsonObj->type, ERROR);
842 if (cJSON_String == jsonObj->type)
844 jsonObjLen = strlen(jsonObj->valuestring) + 1;
845 cred->publicData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
846 VERIFY_NON_NULL(TAG, (cred->publicData.data), ERROR);
847 memcpy(cred->publicData.data, jsonObj->valuestring, jsonObjLen);
849 else if (SIGNED_ASYMMETRIC_KEY == cred->credType && cJSON_Object == jsonObj->type)
851 cred->publicData.data = cJSON_PrintUnformatted(jsonObj);
852 VERIFY_NON_NULL(TAG, (cred->publicData.data), ERROR);
855 #endif // __WITH_X509__
856 //Period -- Not Mandatory
857 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PERIOD_NAME);
858 if(jsonObj && cJSON_String == jsonObj->type)
860 jsonObjLen = strlen(jsonObj->valuestring) + 1;
861 cred->period = (char *)OICMalloc(jsonObjLen);
862 VERIFY_NON_NULL(TAG, cred->period, ERROR);
863 strncpy(cred->period, jsonObj->valuestring, jsonObjLen);
866 //Owners -- Mandatory
867 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_OWNERS_NAME);
868 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
869 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
870 cred->ownersLen = (size_t)cJSON_GetArraySize(jsonObj);
871 VERIFY_SUCCESS(TAG, cred->ownersLen > 0, ERROR);
872 cred->owners = (OicUuid_t*)OICCalloc(cred->ownersLen, sizeof(OicUuid_t));
873 VERIFY_NON_NULL(TAG, (cred->owners), ERROR);
874 for(size_t i = 0; i < cred->ownersLen; i++)
876 cJSON *jsonOwnr = cJSON_GetArrayItem(jsonObj, i);
877 VERIFY_NON_NULL(TAG, jsonOwnr, ERROR);
878 VERIFY_SUCCESS(TAG, cJSON_String == jsonOwnr->type, ERROR);
880 memset(base64Buff, 0, sizeof(base64Buff));
881 b64Ret = b64Decode(jsonOwnr->valuestring, strlen(jsonOwnr->valuestring),
882 base64Buff, sizeof(base64Buff), &outLen);
883 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK &&
884 outLen <= sizeof(cred->owners[i].id)), ERROR);
885 memcpy(cred->owners[i].id, base64Buff, outLen);
888 } while( ++idx < numCred);
894 cJSON_Delete(jsonRoot);
895 if (OC_STACK_OK != ret)
897 DeleteCredList(headCred);
903 static OicSecSvc_t* JSONToSvcBin(const char * jsonStr)
905 OCStackResult ret = OC_STACK_ERROR;
906 OicSecSvc_t * headSvc = NULL;
907 OicSecSvc_t * prevSvc = NULL;
908 cJSON *jsonRoot = NULL;
909 cJSON *jsonSvcArray = NULL;
911 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
913 jsonRoot = cJSON_Parse(jsonStr);
914 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
916 jsonSvcArray = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME);
917 VERIFY_NON_NULL(TAG, jsonSvcArray, INFO);
919 if (cJSON_Array == jsonSvcArray->type)
921 int numSvc = cJSON_GetArraySize(jsonSvcArray);
924 VERIFY_SUCCESS(TAG, numSvc > 0, INFO);
927 cJSON *jsonSvc = cJSON_GetArrayItem(jsonSvcArray, idx);
928 VERIFY_NON_NULL(TAG, jsonSvc, ERROR);
930 OicSecSvc_t *svc = (OicSecSvc_t*)OICCalloc(1, sizeof(OicSecSvc_t));
931 VERIFY_NON_NULL(TAG, svc, ERROR);
933 headSvc = (headSvc) ? headSvc : svc;
939 cJSON *jsonObj = NULL;
940 unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
942 B64Result b64Ret = B64_OK;
944 // Service Device Identity
945 jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_DEVICE_ID);
946 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
947 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
949 b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
950 sizeof(base64Buff), &outLen);
951 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->svcdid.id)), ERROR);
952 memcpy(svc->svcdid.id, base64Buff, outLen);
955 jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_TYPE);
956 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
957 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
958 svc->svct = (OicSecSvcType_t)jsonObj->valueint;
961 jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_OWNERS_NAME);
962 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
963 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
965 svc->ownersLen = (size_t)cJSON_GetArraySize(jsonObj);
966 VERIFY_SUCCESS(TAG, svc->ownersLen > 0, ERROR);
967 svc->owners = (OicUuid_t*)OICCalloc(svc->ownersLen, sizeof(OicUuid_t));
968 VERIFY_NON_NULL(TAG, (svc->owners), ERROR);
973 cJSON *jsonOwnr = cJSON_GetArrayItem(jsonObj, idxx);
974 VERIFY_NON_NULL(TAG, jsonOwnr, ERROR);
975 VERIFY_SUCCESS(TAG, cJSON_String == jsonOwnr->type, ERROR);
977 b64Ret = b64Decode(jsonOwnr->valuestring, strlen(jsonOwnr->valuestring), base64Buff,
978 sizeof(base64Buff), &outLen);
980 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->owners[idxx].id)),
982 memcpy(svc->owners[idxx].id, base64Buff, outLen);
983 } while ( ++idxx < svc->ownersLen);
986 } while( ++idx < numSvc);
992 cJSON_Delete(jsonRoot);
993 if (OC_STACK_OK != ret)
995 DeleteSVCList(headSvc);
1001 static OicSecAmacl_t* JSONToAmaclBin(const char * jsonStr)
1003 OCStackResult ret = OC_STACK_ERROR;
1004 OicSecAmacl_t * headAmacl = (OicSecAmacl_t*)OICCalloc(1, sizeof(OicSecAmacl_t));
1006 cJSON *jsonRoot = NULL;
1007 cJSON *jsonAmacl = NULL;
1009 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
1011 jsonRoot = cJSON_Parse(jsonStr);
1012 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
1014 jsonAmacl = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
1015 VERIFY_NON_NULL(TAG, jsonAmacl, INFO);
1017 size_t jsonObjLen = 0;
1018 cJSON *jsonObj = NULL;
1020 // Resources -- Mandatory
1021 jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_RESOURCES_NAME);
1022 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1025 cJSON *jsonRlistArray = cJSON_GetObjectItem(jsonObj, OIC_JSON_RLIST_NAME);
1026 VERIFY_NON_NULL(TAG, jsonRlistArray, ERROR);
1027 VERIFY_SUCCESS(TAG, cJSON_Array == jsonRlistArray->type, ERROR);
1029 headAmacl->resourcesLen = (size_t)cJSON_GetArraySize(jsonRlistArray);
1030 headAmacl->resources = (char**)OICCalloc(headAmacl->resourcesLen, sizeof(char*));
1034 cJSON *jsonRsrc = cJSON_GetArrayItem(jsonRlistArray, idxx);
1035 VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
1037 cJSON *jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_HREF_NAME);
1038 VERIFY_NON_NULL(TAG, jsonRsrcObj, ERROR);
1039 VERIFY_SUCCESS(TAG, cJSON_String == jsonRsrcObj->type, ERROR);
1041 size_t jsonRsrcObjLen = 0;
1042 jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
1043 headAmacl->resources[idxx] = (char*)OICMalloc(jsonRsrcObjLen);
1044 VERIFY_NON_NULL(TAG, (headAmacl->resources[idxx]), ERROR);
1045 OICStrcpy(headAmacl->resources[idxx], jsonRsrcObjLen, jsonRsrcObj->valuestring);
1047 } while ( ++idxx < headAmacl->resourcesLen);
1050 jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_AMS_NAME);
1051 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1052 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
1054 headAmacl->amssLen = (size_t)cJSON_GetArraySize(jsonObj);
1055 VERIFY_SUCCESS(TAG, headAmacl->amssLen > 0, ERROR);
1056 headAmacl->amss = (OicUuid_t*)OICCalloc(headAmacl->amssLen, sizeof(OicUuid_t));
1057 VERIFY_NON_NULL(TAG, headAmacl->amss, ERROR);
1062 cJSON *jsonAms = cJSON_GetArrayItem(jsonObj, idxx);
1063 VERIFY_NON_NULL(TAG, jsonAms, ERROR);
1064 VERIFY_SUCCESS(TAG, cJSON_String == jsonAms->type, ERROR);
1066 memcpy(headAmacl->amss[idxx].id, (OicUuid_t *)jsonAms->valuestring, strlen(jsonAms->valuestring));
1068 } while ( ++idxx < headAmacl->amssLen);
1071 // Rowner -- Mandatory
1072 jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_ROWNERID_NAME);
1073 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1074 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
1076 headAmacl->ownersLen = 1;
1077 VERIFY_SUCCESS(TAG, headAmacl->ownersLen > 0, ERROR);
1078 headAmacl->owners = (OicUuid_t*)OICCalloc(headAmacl->ownersLen, sizeof(OicUuid_t));
1079 VERIFY_NON_NULL(TAG, (headAmacl->owners), ERROR);
1081 ret = ConvertStrToUuid(jsonObj->valuestring, &headAmacl->owners[0]);
1082 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
1087 cJSON_Delete(jsonRoot);
1088 if (OC_STACK_OK != ret)
1090 DeleteAmaclList(headAmacl);
1096 int main(int argc, char* argv[])
1100 printf("JSON File Name: %s\n CBOR File Name: %s \n", argv[1], argv[2]);
1101 ConvertJsonToCBOR(argv[1], argv[2]);
1105 printf("This program requires two inputs:\n");
1106 printf("1. First input is a json file tha will be converted to cbor. \n");
1107 printf("2. Second input is a resulting cbor file that will store converted cbor. \n");
1108 printf("\t json2cbor <json_file_name> <cbor_file_name>. \n");