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;
207 OicSecCred_t *cred = JSONToCredBin(jsonStr);
208 VERIFY_NON_NULL(TAG, cred, FATAL);
209 ret = CredToCBORPayload(cred, &credCbor, &credCborSize, secureFlag);
210 if(OC_STACK_OK != ret)
212 OIC_LOG (ERROR, TAG, "Failed converting Cred to Cbor Payload");
213 DeleteCredList(cred);
216 printf("CRED Cbor Size: %zd\n", credCborSize);
217 DeleteCredList(cred);
221 size_t cborSize = aclCborSize + pstatCborSize + doxmCborSize + svcCborSize + credCborSize + amaclCborSize;
223 printf("Total Cbor Size : %zd\n", cborSize);
224 cborSize += 255; // buffer margin for adding map and byte string
225 uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborSize);
226 VERIFY_NON_NULL(TAG, outPayload, ERROR);
227 cbor_encoder_init(&encoder, outPayload, cborSize, 0);
229 CborError cborEncoderResult = cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
230 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Creating Main Map.");
233 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
234 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
235 cborEncoderResult = cbor_encode_byte_string(&map, aclCbor, aclCborSize);
236 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
239 if (pstatCborSize > 0)
241 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
242 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
243 cborEncoderResult = cbor_encode_byte_string(&map, pstatCbor, pstatCborSize);
244 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
246 if (doxmCborSize > 0)
248 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
249 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Name.");
250 cborEncoderResult = cbor_encode_byte_string(&map, doxmCbor, doxmCborSize);
251 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Value.");
253 if (amaclCborSize > 0)
255 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_AMACL_NAME, strlen(OIC_JSON_AMACL_NAME));
256 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Name.");
257 cborEncoderResult = cbor_encode_byte_string(&map, amaclCbor, amaclCborSize);
258 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Value.");
262 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_SVC_NAME, strlen(OIC_JSON_SVC_NAME));
263 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Name.");
264 cborEncoderResult = cbor_encode_byte_string(&map, svcCbor, svcCborSize);
265 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Value.");
267 if (credCborSize > 0)
269 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME));
270 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Name.");
271 cborEncoderResult = cbor_encode_byte_string(&map, credCbor, credCborSize);
272 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Value.");
275 cborEncoderResult = cbor_encoder_close_container(&encoder, &map);
276 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Container.");
278 size_t s = encoder.ptr - outPayload;
279 OIC_LOG_V(DEBUG, TAG, "Payload size %zu", s);
281 fp1 = fopen(cborFileName, "w");
284 size_t bytesWritten = fwrite(outPayload, 1, s, fp1);
285 if (bytesWritten == s)
287 OIC_LOG_V(DEBUG, TAG, "Written %zu bytes", bytesWritten);
291 OIC_LOG_V(ERROR, TAG, "Failed writing %zu bytes", s);
298 cJSON_Delete(jsonRoot);
309 OicSecAcl_t* JSONToAclBin(const char * jsonStr)
311 OCStackResult ret = OC_STACK_ERROR;
312 OicSecAcl_t * headAcl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
313 cJSON *jsonRoot = NULL;
315 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
317 jsonRoot = cJSON_Parse(jsonStr);
318 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
320 cJSON *jsonAclMap = cJSON_GetObjectItem(jsonRoot, OIC_JSON_ACL_NAME);
321 VERIFY_NON_NULL(TAG, jsonAclMap, ERROR);
323 cJSON *jsonAclObj = NULL;
326 jsonAclObj = cJSON_GetObjectItem(jsonAclMap, OIC_JSON_ACLIST_NAME);
327 VERIFY_NON_NULL(TAG, jsonAclObj, ERROR);
330 cJSON *jsonAclArray = NULL;
331 jsonAclArray = cJSON_GetObjectItem(jsonAclObj, OIC_JSON_ACES_NAME);
332 VERIFY_NON_NULL(TAG, jsonAclArray, ERROR);
334 if (cJSON_Array == jsonAclArray->type)
337 int numAcl = cJSON_GetArraySize(jsonAclArray);
340 VERIFY_SUCCESS(TAG, numAcl > 0, INFO);
343 cJSON *jsonAcl = cJSON_GetArrayItem(jsonAclArray, idx);
344 VERIFY_NON_NULL(TAG, jsonAcl, ERROR);
346 OicSecAcl_t *acl = NULL;
353 acl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
354 OicSecAcl_t *temp = headAcl;
362 VERIFY_NON_NULL(TAG, acl, ERROR);
364 size_t jsonObjLen = 0;
365 cJSON *jsonObj = NULL;
366 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_SUBJECTID_NAME);
367 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
368 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
369 if(strcmp(jsonObj->valuestring, WILDCARD_RESOURCE_URI) == 0)
371 acl->subject.id[0] = '*';
375 ret = ConvertStrToUuid(jsonObj->valuestring, &acl->subject);
376 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
378 // Resources -- Mandatory
379 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_RESOURCES_NAME);
380 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
381 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
383 acl->resourcesLen = (size_t)cJSON_GetArraySize(jsonObj);
385 VERIFY_SUCCESS(TAG, acl->resourcesLen > 0, ERROR);
386 acl->resources = (char**)OICCalloc(acl->resourcesLen, sizeof(char*));
387 VERIFY_NON_NULL(TAG, (acl->resources), ERROR);
392 cJSON *jsonRsrc = cJSON_GetArrayItem(jsonObj, idxx);
393 VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
395 size_t jsonRsrcObjLen = 0;
396 cJSON *jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_HREF_NAME);
397 VERIFY_NON_NULL(TAG, jsonRsrcObj, ERROR);
398 VERIFY_SUCCESS(TAG, cJSON_String == jsonRsrcObj->type, ERROR);
400 jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
401 acl->resources[idxx] = (char*)OICMalloc(jsonRsrcObjLen);
403 VERIFY_NON_NULL(TAG, (acl->resources[idxx]), ERROR);
404 OICStrcpy(acl->resources[idxx], jsonRsrcObjLen, jsonRsrcObj->valuestring);
406 } while ( ++idxx < acl->resourcesLen);
408 // Permissions -- Mandatory
409 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_PERMISSION_NAME);
410 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
411 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
412 acl->permission = jsonObj->valueint;
413 //Period -- Not Mandatory
414 cJSON *jsonPeriodObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_PERIOD_NAME);
417 VERIFY_SUCCESS(TAG, cJSON_Array == jsonPeriodObj->type, ERROR);
418 acl->prdRecrLen = (size_t)cJSON_GetArraySize(jsonPeriodObj);
419 if(acl->prdRecrLen > 0)
421 acl->periods = (char**)OICCalloc(acl->prdRecrLen, sizeof(char*));
422 VERIFY_NON_NULL(TAG, acl->periods, ERROR);
424 cJSON *jsonPeriod = NULL;
425 for(size_t i = 0; i < acl->prdRecrLen; i++)
427 jsonPeriod = cJSON_GetArrayItem(jsonPeriodObj, i);
428 VERIFY_NON_NULL(TAG, jsonPeriod, ERROR);
430 jsonObjLen = strlen(jsonPeriod->valuestring) + 1;
431 acl->periods[i] = (char*)OICMalloc(jsonObjLen);
432 VERIFY_NON_NULL(TAG, acl->periods[i], ERROR);
433 OICStrcpy(acl->periods[i], jsonObjLen, jsonPeriod->valuestring);
437 //Recurrence -- Not mandatory
438 cJSON *jsonRecurObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_RECURRENCES_NAME);
442 VERIFY_SUCCESS(TAG, cJSON_Array == jsonRecurObj->type, ERROR);
444 if(acl->prdRecrLen > 0)
446 acl->recurrences = (char**)OICCalloc(acl->prdRecrLen, sizeof(char*));
447 VERIFY_NON_NULL(TAG, acl->recurrences, ERROR);
449 cJSON *jsonRecur = NULL;
450 for(size_t i = 0; i < acl->prdRecrLen; i++)
452 jsonRecur = cJSON_GetArrayItem(jsonRecurObj, i);
453 VERIFY_NON_NULL(TAG, jsonRecur, ERROR);
454 jsonObjLen = strlen(jsonRecur->valuestring) + 1;
455 acl->recurrences[i] = (char*)OICMalloc(jsonObjLen);
456 VERIFY_NON_NULL(TAG, acl->recurrences[i], ERROR);
457 OICStrcpy(acl->recurrences[i], jsonObjLen, jsonRecur->valuestring);
464 } while( ++idx < numAcl);
469 jsonAclObj = cJSON_GetObjectItem(jsonAclMap, OIC_JSON_ROWNERID_NAME);
470 VERIFY_NON_NULL(TAG, jsonAclObj, ERROR);
471 VERIFY_SUCCESS(TAG, cJSON_String == jsonAclObj->type, ERROR);
472 ret = ConvertStrToUuid(jsonAclObj->valuestring, &headAcl->rownerID);
473 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
478 cJSON_Delete(jsonRoot);
479 if (OC_STACK_OK != ret)
481 DeleteACLList(headAcl);
487 OicSecDoxm_t* JSONToDoxmBin(const char * jsonStr)
489 printf("IN JSONToDoxmBin\n");
495 OCStackResult ret = OC_STACK_ERROR;
496 OicSecDoxm_t *doxm = NULL;
497 cJSON *jsonDoxm = NULL;
498 cJSON *jsonObj = NULL;
500 size_t jsonObjLen = 0;
502 cJSON *jsonRoot = cJSON_Parse(jsonStr);
503 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
505 jsonDoxm = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME);
506 VERIFY_NON_NULL(TAG, jsonDoxm, ERROR);
508 doxm = (OicSecDoxm_t *)OICCalloc(1, sizeof(OicSecDoxm_t));
509 VERIFY_NON_NULL(TAG, doxm, ERROR);
511 //OxmType -- not Mandatory
512 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_TYPE_NAME);
513 if ((jsonObj) && (cJSON_Array == jsonObj->type))
515 doxm->oxmTypeLen = (size_t)cJSON_GetArraySize(jsonObj);
516 VERIFY_SUCCESS(TAG, doxm->oxmTypeLen > 0, ERROR);
518 doxm->oxmType = (OicUrn_t *)OICCalloc(doxm->oxmTypeLen, sizeof(char *));
519 VERIFY_NON_NULL(TAG, (doxm->oxmType), ERROR);
521 for (size_t i = 0; i < doxm->oxmTypeLen ; i++)
523 cJSON *jsonOxmTy = cJSON_GetArrayItem(jsonObj, i);
524 VERIFY_NON_NULL(TAG, jsonOxmTy, ERROR);
526 jsonObjLen = strlen(jsonOxmTy->valuestring) + 1;
527 doxm->oxmType[i] = (char*)OICMalloc(jsonObjLen);
528 VERIFY_NON_NULL(TAG, doxm->oxmType[i], ERROR);
529 strncpy((char *)doxm->oxmType[i], (char *)jsonOxmTy->valuestring, jsonObjLen);
533 //Oxm -- not Mandatory
534 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXMS_NAME);
535 if (jsonObj && cJSON_Array == jsonObj->type)
537 doxm->oxmLen = (size_t)cJSON_GetArraySize(jsonObj);
538 VERIFY_SUCCESS(TAG, doxm->oxmLen > 0, ERROR);
540 doxm->oxm = (OicSecOxm_t*)OICCalloc(doxm->oxmLen, sizeof(OicSecOxm_t));
541 VERIFY_NON_NULL(TAG, doxm->oxm, ERROR);
543 for (size_t i = 0; i < doxm->oxmLen ; i++)
545 cJSON *jsonOxm = cJSON_GetArrayItem(jsonObj, i);
546 VERIFY_NON_NULL(TAG, jsonOxm, ERROR);
547 doxm->oxm[i] = (OicSecOxm_t)jsonOxm->valueint;
551 //OxmSel -- Mandatory
552 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_SEL_NAME);
555 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
556 doxm->oxmSel = (OicSecOxm_t)jsonObj->valueint;
560 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_SUPPORTED_CRED_TYPE_NAME);
563 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
564 doxm->sct = (OicSecCredType_t)jsonObj->valueint;
568 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OWNED_NAME);
571 VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type), ERROR);
572 doxm->owned = jsonObj->valueint;
576 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DPC_NAME);
579 VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type), ERROR);
580 doxm->dpc = jsonObj->valueint;
583 //DeviceId -- Mandatory
584 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVICE_ID_NAME);
587 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
588 if (cJSON_String == jsonObj->type)
590 //Check for empty string, in case DeviceId field has not been set yet
591 if (jsonObj->valuestring[0])
593 ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->deviceID);
594 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
599 //rowner -- Mandatory
600 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_ROWNERID_NAME);
601 if (true == doxm->owned)
603 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
607 ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->rownerID);
608 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
611 //Owner -- will be empty when device status is unowned.
612 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVOWNERID_NAME);
613 if (true == doxm->owned)
615 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
619 ret = ConvertStrToUuid(jsonObj->valuestring, &doxm->owner);
620 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
626 cJSON_Delete(jsonRoot);
627 if (OC_STACK_OK != ret)
629 DeleteDoxmBinData(doxm);
632 printf("OUT JSONToDoxmBin\n");
636 OicSecPstat_t* JSONToPstatBin(const char * jsonStr)
638 printf("IN JSONToPstatBin\n");
644 OCStackResult ret = OC_STACK_ERROR;
645 OicSecPstat_t *pstat = NULL;
646 cJSON *jsonPstat = NULL;
647 cJSON *jsonObj = NULL;
649 cJSON *jsonRoot = cJSON_Parse(jsonStr);
650 VERIFY_NON_NULL(TAG, jsonRoot, INFO);
652 jsonPstat = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
653 VERIFY_NON_NULL(TAG, jsonPstat, INFO);
655 pstat = (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
656 VERIFY_NON_NULL(TAG, pstat, INFO);
657 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_ISOP_NAME);
658 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
659 VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type) , ERROR);
660 pstat->isOp = jsonObj->valueint;
662 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_DEVICE_ID_NAME);
663 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
664 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
665 ret = ConvertStrToUuid(jsonObj->valuestring, &pstat->deviceID);
666 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
668 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_ROWNERID_NAME);
669 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
670 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
671 ret = ConvertStrToUuid(jsonObj->valuestring, &pstat->rownerID);
672 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
674 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_CM_NAME);
675 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
676 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
677 pstat->cm = (OicSecDpm_t)jsonObj->valueint;
679 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_TM_NAME);
680 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
681 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
682 pstat->tm = (OicSecDpm_t)jsonObj->valueint;
684 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_OM_NAME);
685 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
686 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
687 pstat->om = (OicSecDpom_t)jsonObj->valueint;
689 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_SM_NAME);
690 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
691 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
693 pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
694 pstat->sm[0] = (OicSecDpom_t)jsonObj->valueint;
699 cJSON_Delete(jsonRoot);
700 if (OC_STACK_OK != ret)
702 OIC_LOG(ERROR, TAG, "JSONToPstatBin failed");
704 printf("OUT JSONToPstatBin\n");
708 OicSecCred_t * JSONToCredBin(const char * jsonStr)
712 OIC_LOG(ERROR, TAG,"JSONToCredBin jsonStr in NULL");
716 OicSecCred_t *headCred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
717 OCStackResult ret = OC_STACK_ERROR;
718 cJSON *jsonRoot = NULL;
719 VERIFY_NON_NULL(TAG, headCred, ERROR);
721 jsonRoot = cJSON_Parse(jsonStr);
722 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
724 cJSON *jsonCredMap = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
725 VERIFY_NON_NULL(TAG, jsonCredMap, ERROR);
728 cJSON *jsonCredArray = NULL;
729 jsonCredArray = cJSON_GetObjectItem(jsonCredMap, OIC_JSON_CREDS_NAME);
730 VERIFY_NON_NULL(TAG, jsonCredArray, ERROR);
732 if (cJSON_Array == jsonCredArray->type)
734 int numCred = cJSON_GetArraySize(jsonCredArray);
735 VERIFY_SUCCESS(TAG, numCred > 0, ERROR);
737 size_t ownersLen = 0;
740 cJSON *jsonCred = cJSON_GetArrayItem(jsonCredArray, idx);
741 VERIFY_NON_NULL(TAG, jsonCred, ERROR);
743 OicSecCred_t *cred = NULL;
750 cred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
751 OicSecCred_t *temp = headCred;
758 VERIFY_NON_NULL(TAG, cred, ERROR);
760 size_t jsonObjLen = 0;
761 cJSON *jsonObj = NULL;
763 //CredId -- Mandatory
764 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDID_NAME);
767 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
768 cred->credId = jsonObj->valueint;
771 //subject -- Mandatory
772 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_SUBJECTID_NAME);
773 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
774 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
775 ret = ConvertStrToUuid(jsonObj->valuestring, &cred->subject);
776 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
778 //CredType -- Mandatory
779 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDTYPE_NAME);
780 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
781 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
782 cred->credType = (OicSecCredType_t)jsonObj->valueint;
783 //PrivateData is mandatory for some of the credential types listed below.
784 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PRIVATEDATA_NAME);
788 cJSON *jsonPriv = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
789 VERIFY_NON_NULL(TAG, jsonPriv, ERROR);
790 jsonObjLen = strlen(jsonPriv->valuestring);
791 cred->privateData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
792 VERIFY_NON_NULL(TAG, (cred->privateData.data), ERROR);
793 memcpy(cred->privateData.data, jsonPriv->valuestring, jsonObjLen);
794 cred->privateData.len = jsonObjLen;
797 //PublicData is mandatory only for SIGNED_ASYMMETRIC_KEY credentials type.
798 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PUBLICDATA_NAME);
802 cJSON *jsonPub = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
803 VERIFY_NON_NULL(TAG, jsonPub, ERROR);
804 jsonObjLen = strlen(jsonPub->valuestring);
805 cred->publicData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
806 VERIFY_NON_NULL(TAG, (cred->publicData.data), ERROR);
807 memcpy(cred->publicData.data, jsonPub->valuestring, jsonObjLen);
808 cred->publicData.len = jsonObjLen;
810 #endif // __WITH_X509__
811 //Period -- Not Mandatory
812 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PERIOD_NAME);
813 if(jsonObj && cJSON_String == jsonObj->type)
815 jsonObjLen = strlen(jsonObj->valuestring) + 1;
816 cred->period = (char *)OICMalloc(jsonObjLen);
817 VERIFY_NON_NULL(TAG, cred->period, ERROR);
818 strncpy(cred->period, jsonObj->valuestring, jsonObjLen);
821 } while( ++idx < numCred);
825 cJSON *jsonCredObj = cJSON_GetObjectItem(jsonCredMap, OIC_JSON_ROWNERID_NAME);
826 VERIFY_NON_NULL(TAG, jsonCredObj, ERROR);
827 VERIFY_SUCCESS(TAG, cJSON_String == jsonCredObj->type, ERROR);
828 ret = ConvertStrToUuid(jsonCredObj->valuestring, &headCred->rownerID);
829 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
834 if (OC_STACK_OK != ret)
836 DeleteCredList(headCred);
842 static OicSecSvc_t* JSONToSvcBin(const char * jsonStr)
844 OCStackResult ret = OC_STACK_ERROR;
845 OicSecSvc_t * headSvc = NULL;
846 OicSecSvc_t * prevSvc = NULL;
847 cJSON *jsonRoot = NULL;
848 cJSON *jsonSvcArray = NULL;
850 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
852 jsonRoot = cJSON_Parse(jsonStr);
853 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
855 jsonSvcArray = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME);
856 VERIFY_NON_NULL(TAG, jsonSvcArray, INFO);
858 if (cJSON_Array == jsonSvcArray->type)
860 int numSvc = cJSON_GetArraySize(jsonSvcArray);
863 VERIFY_SUCCESS(TAG, numSvc > 0, INFO);
866 cJSON *jsonSvc = cJSON_GetArrayItem(jsonSvcArray, idx);
867 VERIFY_NON_NULL(TAG, jsonSvc, ERROR);
869 OicSecSvc_t *svc = (OicSecSvc_t*)OICCalloc(1, sizeof(OicSecSvc_t));
870 VERIFY_NON_NULL(TAG, svc, ERROR);
872 headSvc = (headSvc) ? headSvc : svc;
878 cJSON *jsonObj = NULL;
879 unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
881 B64Result b64Ret = B64_OK;
883 // Service Device Identity
884 jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_DEVICE_ID);
885 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
886 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
888 b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
889 sizeof(base64Buff), &outLen);
890 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->svcdid.id)), ERROR);
891 memcpy(svc->svcdid.id, base64Buff, outLen);
894 jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_TYPE);
895 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
896 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
897 svc->svct = (OicSecSvcType_t)jsonObj->valueint;
900 jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_OWNERS_NAME);
901 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
902 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
904 svc->ownersLen = (size_t)cJSON_GetArraySize(jsonObj);
905 VERIFY_SUCCESS(TAG, svc->ownersLen > 0, ERROR);
906 svc->owners = (OicUuid_t*)OICCalloc(svc->ownersLen, sizeof(OicUuid_t));
907 VERIFY_NON_NULL(TAG, (svc->owners), ERROR);
912 cJSON *jsonOwnr = cJSON_GetArrayItem(jsonObj, idxx);
913 VERIFY_NON_NULL(TAG, jsonOwnr, ERROR);
914 VERIFY_SUCCESS(TAG, cJSON_String == jsonOwnr->type, ERROR);
916 b64Ret = b64Decode(jsonOwnr->valuestring, strlen(jsonOwnr->valuestring), base64Buff,
917 sizeof(base64Buff), &outLen);
919 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->owners[idxx].id)),
921 memcpy(svc->owners[idxx].id, base64Buff, outLen);
922 } while ( ++idxx < svc->ownersLen);
925 } while( ++idx < numSvc);
931 cJSON_Delete(jsonRoot);
932 if (OC_STACK_OK != ret)
934 DeleteSVCList(headSvc);
940 static OicSecAmacl_t* JSONToAmaclBin(const char * jsonStr)
942 OCStackResult ret = OC_STACK_ERROR;
943 OicSecAmacl_t * headAmacl = (OicSecAmacl_t*)OICCalloc(1, sizeof(OicSecAmacl_t));
945 cJSON *jsonRoot = NULL;
946 cJSON *jsonAmacl = NULL;
948 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
950 jsonRoot = cJSON_Parse(jsonStr);
951 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
953 jsonAmacl = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
954 VERIFY_NON_NULL(TAG, jsonAmacl, INFO);
956 cJSON *jsonObj = NULL;
958 // Resources -- Mandatory
959 jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_RESOURCES_NAME);
960 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
963 cJSON *jsonRlistArray = cJSON_GetObjectItem(jsonObj, OIC_JSON_RLIST_NAME);
964 VERIFY_NON_NULL(TAG, jsonRlistArray, ERROR);
965 VERIFY_SUCCESS(TAG, cJSON_Array == jsonRlistArray->type, ERROR);
967 headAmacl->resourcesLen = (size_t)cJSON_GetArraySize(jsonRlistArray);
968 headAmacl->resources = (char**)OICCalloc(headAmacl->resourcesLen, sizeof(char*));
972 cJSON *jsonRsrc = cJSON_GetArrayItem(jsonRlistArray, idxx);
973 VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
975 cJSON *jsonRsrcObj = cJSON_GetObjectItem(jsonRsrc, OIC_JSON_HREF_NAME);
976 VERIFY_NON_NULL(TAG, jsonRsrcObj, ERROR);
977 VERIFY_SUCCESS(TAG, cJSON_String == jsonRsrcObj->type, ERROR);
979 size_t jsonRsrcObjLen = 0;
980 jsonRsrcObjLen = strlen(jsonRsrcObj->valuestring) + 1;
981 headAmacl->resources[idxx] = (char*)OICMalloc(jsonRsrcObjLen);
982 VERIFY_NON_NULL(TAG, (headAmacl->resources[idxx]), ERROR);
983 OICStrcpy(headAmacl->resources[idxx], jsonRsrcObjLen, jsonRsrcObj->valuestring);
985 } while ( ++idxx < headAmacl->resourcesLen);
988 jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_AMS_NAME);
989 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
990 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
992 headAmacl->amssLen = (size_t)cJSON_GetArraySize(jsonObj);
993 VERIFY_SUCCESS(TAG, headAmacl->amssLen > 0, ERROR);
994 headAmacl->amss = (OicUuid_t*)OICCalloc(headAmacl->amssLen, sizeof(OicUuid_t));
995 VERIFY_NON_NULL(TAG, headAmacl->amss, ERROR);
1000 cJSON *jsonAms = cJSON_GetArrayItem(jsonObj, idxx);
1001 VERIFY_NON_NULL(TAG, jsonAms, ERROR);
1002 VERIFY_SUCCESS(TAG, cJSON_String == jsonAms->type, ERROR);
1004 memcpy(headAmacl->amss[idxx].id, (OicUuid_t *)jsonAms->valuestring, strlen(jsonAms->valuestring));
1006 } while ( ++idxx < headAmacl->amssLen);
1009 // Rowner -- Mandatory
1010 jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_ROWNERID_NAME);
1011 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1012 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
1014 ret = ConvertStrToUuid(jsonObj->valuestring, &headAmacl->rownerID);
1015 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
1020 cJSON_Delete(jsonRoot);
1021 if (OC_STACK_OK != ret)
1023 DeleteAmaclList(headAmacl);
1029 int main(int argc, char* argv[])
1033 printf("JSON File Name: %s\n CBOR File Name: %s \n", argv[1], argv[2]);
1034 ConvertJsonToCBOR(argv[1], argv[2]);
1038 printf("This program requires two inputs:\n");
1039 printf("1. First input is a json file tha will be converted to cbor. \n");
1040 printf("2. Second input is a resulting cbor file that will store converted cbor. \n");
1041 printf("\t json2cbor <json_file_name> <cbor_file_name>. \n");