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);
117 cJSON *value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_ACL_NAME);
118 size_t aclCborSize = 0;
121 OicSecAcl_t *acl = JSONToAclBin(jsonStr);
122 VERIFY_NON_NULL(TAG, acl, FATAL);
123 ret = AclToCBORPayload(acl, &aclCbor, &aclCborSize);
124 if(OC_STACK_OK != ret)
126 OIC_LOG (ERROR, TAG, "Failed converting Acl to Cbor Payload");
130 printf("ACL Cbor Size: %zd\n", aclCborSize);
133 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
134 size_t pstatCborSize = 0;
137 OicSecPstat_t *pstat = JSONToPstatBin(jsonStr);
138 VERIFY_NON_NULL(TAG, pstat, FATAL);
139 ret = PstatToCBORPayload(pstat, &pstatCbor, &pstatCborSize);
140 if(OC_STACK_OK != ret)
142 OIC_LOG (ERROR, TAG, "Failed converting Pstat to Cbor Payload");
143 DeletePstatBinData(pstat);
146 printf("PSTAT Cbor Size: %zd\n", pstatCborSize);
147 DeletePstatBinData(pstat);
149 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME);
150 size_t doxmCborSize = 0;
153 OicSecDoxm_t *doxm = JSONToDoxmBin(jsonStr);
154 VERIFY_NON_NULL(TAG, doxm, FATAL);
155 ret = DoxmToCBORPayload(doxm, &doxmCbor, &doxmCborSize);
156 if(OC_STACK_OK != ret)
158 OIC_LOG (ERROR, TAG, "Failed converting Doxm to Cbor Payload");
159 DeleteDoxmBinData(doxm);
162 printf("DOXM Cbor Size: %zd\n", doxmCborSize);
163 DeleteDoxmBinData(doxm);
165 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
166 size_t amaclCborSize = 0;
169 OicSecAmacl_t *amacl = JSONToAmaclBin(jsonStr);
170 VERIFY_NON_NULL(TAG, amacl, FATAL);
171 ret = AmaclToCBORPayload(amacl, &amaclCbor, &amaclCborSize);
172 if(OC_STACK_OK != ret)
174 OIC_LOG (ERROR, TAG, "Failed converting Amacl to Cbor Payload");
175 DeleteAmaclList(amacl);
178 printf("AMACL Cbor Size: %zd\n", amaclCborSize);
179 DeleteAmaclList(amacl);
181 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME);
182 size_t svcCborSize = 0;
185 OicSecSvc_t *svc = JSONToSvcBin(jsonStr);
186 VERIFY_NON_NULL(TAG, svc, FATAL);
187 ret = SVCToCBORPayload(svc, &svcCbor, &svcCborSize);
188 if(OC_STACK_OK != ret)
190 OIC_LOG (ERROR, TAG, "Failed converting Svc to Cbor Payload");
194 printf("SVC Cbor Size: %zd\n", svcCborSize);
197 value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
198 size_t credCborSize = 0;
201 OicSecCred_t *cred = JSONToCredBin(jsonStr);
202 VERIFY_NON_NULL(TAG, cred, FATAL);
203 ret = CredToCBORPayload(cred, &credCbor, &credCborSize);
204 if(OC_STACK_OK != ret)
206 OIC_LOG (ERROR, TAG, "Failed converting Cred to Cbor Payload");
207 DeleteCredList(cred);
210 printf("CRED Cbor Size: %zd\n", credCborSize);
211 DeleteCredList(cred);
215 CborEncoder encoder = { {.ptr = NULL }, .end = 0 };
216 size_t cborSize = aclCborSize + pstatCborSize + doxmCborSize + svcCborSize + credCborSize + amaclCborSize;
217 printf("Total Cbor Size : %zd\n", cborSize);
218 cborSize += 255; // buffer margin for adding map and byte string
219 uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborSize);
220 VERIFY_NON_NULL(TAG, outPayload, ERROR);
221 cbor_encoder_init(&encoder, outPayload, cborSize, 0);
222 CborEncoder map = { {.ptr = NULL }, .end = 0 };
223 CborError cborEncoderResult = cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
224 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Creating Main Map.");
227 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
228 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
229 cborEncoderResult = cbor_encode_byte_string(&map, aclCbor, aclCborSize);
230 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
232 if (pstatCborSize > 0)
234 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
235 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
236 cborEncoderResult = cbor_encode_byte_string(&map, pstatCbor, pstatCborSize);
237 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
239 if (doxmCborSize > 0)
241 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
242 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Name.");
243 cborEncoderResult = cbor_encode_byte_string(&map, doxmCbor, doxmCborSize);
244 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Value.");
246 if (amaclCborSize > 0)
248 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_AMACL_NAME, strlen(OIC_JSON_AMACL_NAME));
249 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Name.");
250 cborEncoderResult = cbor_encode_byte_string(&map, amaclCbor, amaclCborSize);
251 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding AMACL Value.");
255 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_SVC_NAME, strlen(OIC_JSON_SVC_NAME));
256 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Name.");
257 cborEncoderResult = cbor_encode_byte_string(&map, svcCbor, svcCborSize);
258 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Value.");
260 if (credCborSize > 0)
262 cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME));
263 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Name.");
264 cborEncoderResult = cbor_encode_byte_string(&map, credCbor, credCborSize);
265 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Value.");
267 cborEncoderResult = cbor_encoder_close_container(&encoder, &map);
268 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Container.");
270 size_t s = encoder.ptr - outPayload;
271 OIC_LOG_V(DEBUG, TAG, "Payload size %zu", s);
273 fp1 = fopen(cborFileName, "w");
276 size_t bytesWritten = fwrite(outPayload, 1, s, fp1);
277 OIC_LOG_V(DEBUG, TAG, "Written %zu bytes", bytesWritten);
282 // cJSON_Delete(jsonRoot);
293 OicSecAcl_t* JSONToAclBin(const char * jsonStr)
295 OCStackResult ret = OC_STACK_ERROR;
296 OicSecAcl_t * headAcl = NULL;
297 OicSecAcl_t * prevAcl = NULL;
298 cJSON *jsonRoot = NULL;
299 cJSON *jsonAclArray = NULL;
301 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
303 jsonRoot = cJSON_Parse(jsonStr);
304 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
306 jsonAclArray = cJSON_GetObjectItem(jsonRoot, OIC_JSON_ACL_NAME);
307 VERIFY_NON_NULL(TAG, jsonAclArray, ERROR);
309 if (cJSON_Array == jsonAclArray->type)
311 int numAcl = cJSON_GetArraySize(jsonAclArray);
314 VERIFY_SUCCESS(TAG, numAcl > 0, INFO);
317 cJSON *jsonAcl = cJSON_GetArrayItem(jsonAclArray, idx);
318 VERIFY_NON_NULL(TAG, jsonAcl, ERROR);
320 OicSecAcl_t *acl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
321 VERIFY_NON_NULL(TAG, acl, ERROR);
323 headAcl = (headAcl) ? headAcl : acl;
329 size_t jsonObjLen = 0;
330 cJSON *jsonObj = NULL;
331 unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
333 B64Result b64Ret = B64_OK;
335 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_SUBJECT_NAME);
336 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
337 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
339 b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
340 sizeof(base64Buff), &outLen);
341 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(acl->subject.id)), ERROR);
342 memcpy(acl->subject.id, base64Buff, outLen);
344 // Resources -- Mandatory
345 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_RESOURCES_NAME);
346 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
347 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
349 acl->resourcesLen = cJSON_GetArraySize(jsonObj);
350 VERIFY_SUCCESS(TAG, acl->resourcesLen > 0, ERROR);
351 acl->resources = (char**)OICCalloc(acl->resourcesLen, sizeof(char*));
352 VERIFY_NON_NULL(TAG, (acl->resources), ERROR);
357 cJSON *jsonRsrc = cJSON_GetArrayItem(jsonObj, idxx);
358 VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
360 jsonObjLen = strlen(jsonRsrc->valuestring) + 1;
361 acl->resources[idxx] = (char*)OICMalloc(jsonObjLen);
362 VERIFY_NON_NULL(TAG, (acl->resources[idxx]), ERROR);
363 OICStrcpy(acl->resources[idxx], jsonObjLen, jsonRsrc->valuestring);
364 } while ( ++idxx < acl->resourcesLen);
366 // Permissions -- Mandatory
367 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_PERMISSION_NAME);
368 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
369 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
370 acl->permission = jsonObj->valueint;
372 //Period -- Not Mandatory
373 cJSON *jsonPeriodObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_PERIODS_NAME);
376 VERIFY_SUCCESS(TAG, cJSON_Array == jsonPeriodObj->type, ERROR);
377 acl->prdRecrLen = (size_t)cJSON_GetArraySize(jsonPeriodObj);
378 if(acl->prdRecrLen > 0)
380 acl->periods = (char**)OICCalloc(acl->prdRecrLen, sizeof(char*));
381 VERIFY_NON_NULL(TAG, acl->periods, ERROR);
383 cJSON *jsonPeriod = NULL;
384 for(size_t i = 0; i < acl->prdRecrLen; i++)
386 jsonPeriod = cJSON_GetArrayItem(jsonPeriodObj, i);
387 VERIFY_NON_NULL(TAG, jsonPeriod, ERROR);
389 jsonObjLen = strlen(jsonPeriod->valuestring) + 1;
390 acl->periods[i] = (char*)OICMalloc(jsonObjLen);
391 VERIFY_NON_NULL(TAG, acl->periods[i], ERROR);
392 OICStrcpy(acl->periods[i], jsonObjLen, jsonPeriod->valuestring);
397 //Recurrence -- Not mandatory
398 cJSON *jsonRecurObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_RECURRENCES_NAME);
401 VERIFY_SUCCESS(TAG, cJSON_Array == jsonRecurObj->type, ERROR);
403 if(acl->prdRecrLen > 0)
405 acl->recurrences = (char**)OICCalloc(acl->prdRecrLen, sizeof(char*));
406 VERIFY_NON_NULL(TAG, acl->recurrences, ERROR);
408 cJSON *jsonRecur = NULL;
409 for(size_t i = 0; i < acl->prdRecrLen; i++)
411 jsonRecur = cJSON_GetArrayItem(jsonRecurObj, i);
412 VERIFY_NON_NULL(TAG, jsonRecur, ERROR);
413 jsonObjLen = strlen(jsonRecur->valuestring) + 1;
414 acl->recurrences[i] = (char*)OICMalloc(jsonObjLen);
415 VERIFY_NON_NULL(TAG, acl->recurrences[i], ERROR);
416 OICStrcpy(acl->recurrences[i], jsonObjLen, jsonRecur->valuestring);
421 // Owners -- Mandatory
422 jsonObj = cJSON_GetObjectItem(jsonAcl, OIC_JSON_OWNERS_NAME);
423 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
424 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
426 acl->ownersLen = cJSON_GetArraySize(jsonObj);
427 VERIFY_SUCCESS(TAG, acl->ownersLen > 0, ERROR);
428 acl->owners = (OicUuid_t*)OICCalloc(acl->ownersLen, sizeof(OicUuid_t));
429 VERIFY_NON_NULL(TAG, (acl->owners), ERROR);
434 cJSON *jsonOwnr = cJSON_GetArrayItem(jsonObj, idxx);
435 VERIFY_NON_NULL(TAG, jsonOwnr, ERROR);
436 VERIFY_SUCCESS(TAG, cJSON_String == jsonOwnr->type, ERROR);
438 b64Ret = b64Decode(jsonOwnr->valuestring, strlen(jsonOwnr->valuestring), base64Buff,
439 sizeof(base64Buff), &outLen);
440 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(acl->owners[idxx].id)),
442 memcpy(acl->owners[idxx].id, base64Buff, outLen);
443 } while ( ++idxx < acl->ownersLen);
446 } while( ++idx < numAcl);
452 cJSON_Delete(jsonRoot);
453 if (OC_STACK_OK != ret)
455 DeleteACLList(headAcl);
461 OicSecDoxm_t* JSONToDoxmBin(const char * jsonStr)
468 OCStackResult ret = OC_STACK_ERROR;
469 OicSecDoxm_t *doxm = NULL;
470 cJSON *jsonDoxm = NULL;
471 cJSON *jsonObj = NULL;
473 size_t jsonObjLen = 0;
474 unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
476 B64Result b64Ret = B64_OK;
478 cJSON *jsonRoot = cJSON_Parse(jsonStr);
479 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
481 jsonDoxm = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME);
482 VERIFY_NON_NULL(TAG, jsonDoxm, ERROR);
484 doxm = (OicSecDoxm_t *)OICCalloc(1, sizeof(OicSecDoxm_t));
485 VERIFY_NON_NULL(TAG, doxm, ERROR);
487 //OxmType -- not Mandatory
488 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_TYPE_NAME);
489 if ((jsonObj) && (cJSON_Array == jsonObj->type))
491 doxm->oxmTypeLen = (size_t)cJSON_GetArraySize(jsonObj);
492 VERIFY_SUCCESS(TAG, doxm->oxmTypeLen > 0, ERROR);
494 doxm->oxmType = (OicUrn_t *)OICCalloc(doxm->oxmTypeLen, sizeof(char *));
495 VERIFY_NON_NULL(TAG, (doxm->oxmType), ERROR);
497 for (size_t i = 0; i < doxm->oxmTypeLen ; i++)
499 cJSON *jsonOxmTy = cJSON_GetArrayItem(jsonObj, i);
500 VERIFY_NON_NULL(TAG, jsonOxmTy, ERROR);
502 jsonObjLen = strlen(jsonOxmTy->valuestring) + 1;
503 doxm->oxmType[i] = (char*)OICMalloc(jsonObjLen);
504 VERIFY_NON_NULL(TAG, doxm->oxmType[i], ERROR);
505 strncpy((char *)doxm->oxmType[i], (char *)jsonOxmTy->valuestring, jsonObjLen);
509 //Oxm -- not Mandatory
510 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_NAME);
511 if (jsonObj && cJSON_Array == jsonObj->type)
513 doxm->oxmLen = cJSON_GetArraySize(jsonObj);
514 VERIFY_SUCCESS(TAG, doxm->oxmLen > 0, ERROR);
516 doxm->oxm = (OicSecOxm_t*)OICCalloc(doxm->oxmLen, sizeof(OicSecOxm_t));
517 VERIFY_NON_NULL(TAG, doxm->oxm, ERROR);
519 for (size_t i = 0; i < doxm->oxmLen ; i++)
521 cJSON *jsonOxm = cJSON_GetArrayItem(jsonObj, i);
522 VERIFY_NON_NULL(TAG, jsonOxm, ERROR);
523 doxm->oxm[i] = (OicSecOxm_t)jsonOxm->valueint;
527 //OxmSel -- Mandatory
528 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_SEL_NAME);
531 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
532 doxm->oxmSel = (OicSecOxm_t)jsonObj->valueint;
536 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_SUPPORTED_CRED_TYPE_NAME);
539 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
540 doxm->sct = (OicSecCredType_t)jsonObj->valueint;
544 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OWNED_NAME);
547 VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type), ERROR);
548 doxm->owned = jsonObj->valueint;
552 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DPC_NAME);
555 VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type), ERROR);
556 doxm->dpc = jsonObj->valueint;
559 //DeviceId -- Mandatory
560 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVICE_ID_NAME);
563 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
564 if (cJSON_String == jsonObj->type)
566 //Check for empty string, in case DeviceId field has not been set yet
567 if (jsonObj->valuestring[0])
570 b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
571 sizeof(base64Buff), &outLen);
572 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(doxm->deviceID.id)),
574 memcpy(doxm->deviceID.id, base64Buff, outLen);
579 //Owner -- will be empty when device status is unowned.
580 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OWNER_NAME);
581 if (true == doxm->owned)
583 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
588 b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
589 sizeof(base64Buff), &outLen);
590 VERIFY_SUCCESS(TAG, ((b64Ret == B64_OK) && (outLen <= sizeof(doxm->owner.id))), ERROR);
591 memcpy(doxm->owner.id, base64Buff, outLen);
597 cJSON_Delete(jsonRoot);
598 if (OC_STACK_OK != ret)
600 DeleteDoxmBinData(doxm);
607 OicSecPstat_t* JSONToPstatBin(const char * jsonStr)
614 OCStackResult ret = OC_STACK_ERROR;
615 OicSecPstat_t *pstat = NULL;
616 cJSON *jsonPstat = NULL;
617 cJSON *jsonObj = NULL;
619 unsigned char base64Buff[sizeof(((OicUuid_t*) 0)->id)] = {};
621 B64Result b64Ret = B64_OK;
623 cJSON *jsonRoot = cJSON_Parse(jsonStr);
624 VERIFY_NON_NULL(TAG, jsonRoot, INFO);
626 jsonPstat = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
627 VERIFY_NON_NULL(TAG, jsonPstat, INFO);
629 pstat = (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
630 VERIFY_NON_NULL(TAG, pstat, INFO);
631 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_ISOP_NAME);
632 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
633 VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type) , ERROR);
634 pstat->isOp = jsonObj->valueint;
636 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_DEVICE_ID_NAME);
637 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
638 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
639 b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
640 sizeof(base64Buff), &outLen);
641 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(pstat->deviceID.id)), ERROR);
642 memcpy(pstat->deviceID.id, base64Buff, outLen);
644 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_COMMIT_HASH_NAME);
645 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
646 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
647 pstat->commitHash = jsonObj->valueint;
649 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_CM_NAME);
650 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
651 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
652 pstat->cm = (OicSecDpm_t)jsonObj->valueint;
654 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_OM_NAME);
655 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
656 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
657 pstat->om = (OicSecDpom_t)jsonObj->valueint;
659 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_SM_NAME);
660 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
661 if (cJSON_Array == jsonObj->type)
663 pstat->smLen = (size_t)cJSON_GetArraySize(jsonObj);
665 VERIFY_SUCCESS(TAG, pstat->smLen != 0, ERROR);
666 pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
667 VERIFY_NON_NULL(TAG, pstat->sm, ERROR);
670 cJSON *jsonSm = cJSON_GetArrayItem(jsonObj, idxx);
671 VERIFY_NON_NULL(TAG, jsonSm, ERROR);
672 pstat->sm[idxx] = (OicSecDpom_t)jsonSm->valueint;
673 } while ( ++idxx < pstat->smLen);
678 cJSON_Delete(jsonRoot);
679 if (OC_STACK_OK != ret)
681 OIC_LOG(ERROR, TAG, "JSONToPstatBin failed");
686 OicSecCred_t * JSONToCredBin(const char * jsonStr)
688 OCStackResult ret = OC_STACK_ERROR;
689 OicSecCred_t * headCred = NULL;
690 OicSecCred_t * prevCred = NULL;
691 cJSON *jsonCredArray = NULL;
693 cJSON *jsonRoot = cJSON_Parse(jsonStr);
694 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
696 jsonCredArray = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
697 VERIFY_NON_NULL(TAG, jsonCredArray, ERROR);
698 if (cJSON_Array == jsonCredArray->type)
700 int numCred = cJSON_GetArraySize(jsonCredArray);
701 VERIFY_SUCCESS(TAG, numCred > 0, ERROR);
702 unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
704 B64Result b64Ret = B64_OK;
708 cJSON *jsonCred = cJSON_GetArrayItem(jsonCredArray, idx);
709 VERIFY_NON_NULL(TAG, jsonCred, ERROR);
711 OicSecCred_t *cred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
712 VERIFY_NON_NULL(TAG, cred, ERROR);
714 headCred = (headCred) ? headCred : cred;
717 prevCred->next = cred;
719 size_t jsonObjLen = 0;
720 cJSON *jsonObj = NULL;
722 //CredId -- Mandatory
723 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDID_NAME);
726 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
727 cred->credId = jsonObj->valueint;
730 //subject -- Mandatory
731 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_SUBJECT_NAME);
732 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
733 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
735 memset(base64Buff, 0, sizeof(base64Buff));
736 b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring),
737 base64Buff, sizeof(base64Buff), &outLen);
738 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(cred->subject.id)),
740 memcpy(cred->subject.id, base64Buff, outLen);
742 //CredType -- Mandatory
743 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDTYPE_NAME);
744 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
745 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
746 cred->credType = (OicSecCredType_t)jsonObj->valueint;
748 //PrivateData is mandatory for some of the credential types listed below.
749 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PRIVATEDATA_NAME);
750 if ((cred->credType & SYMMETRIC_PAIR_WISE_KEY) ||
751 (cred->credType & SYMMETRIC_GROUP_KEY) ||
752 (cred->credType & PIN_PASSWORD))
754 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
755 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
758 else if (cred->credType & SIGNED_ASYMMETRIC_KEY)
760 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
761 VERIFY_SUCCESS(TAG, cJSON_Object == jsonObj->type, ERROR);
763 #endif // __WITH_X509__
766 if (cJSON_String == jsonObj->type)
768 jsonObjLen = strlen(jsonObj->valuestring) + 1;
769 cred->privateData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
770 VERIFY_NON_NULL(TAG, (cred->privateData.data), ERROR);
772 uint8_t pskKey[OWNER_PSK_LENGTH_256] = {};
774 memset(pskKey, 0, sizeof(pskKey));
775 b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring),
776 pskKey, sizeof(pskKey), &outLen);
777 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK &&
778 outLen <= OWNER_PSK_LENGTH_256), ERROR);
779 memcpy(cred->privateData.data, pskKey, outLen);
780 cred->privateData.len = outLen;
783 else if (SIGNED_ASYMMETRIC_KEY == cred->credType && cJSON_Object == jsonObj->type)
785 cred->privateData.data = cJSON_PrintUnformatted(jsonObj);
786 VERIFY_NON_NULL(TAG, (cred->privateData.data), ERROR);
788 #endif // __WITH_X509__
791 //PublicData is mandatory only for SIGNED_ASYMMETRIC_KEY credentials type.
792 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PUBLICDATA_NAME);
793 if (cred->credType & SIGNED_ASYMMETRIC_KEY)
795 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
796 VERIFY_SUCCESS(TAG, cJSON_Object == jsonObj->type, ERROR);
800 if (cJSON_String == jsonObj->type)
802 jsonObjLen = strlen(jsonObj->valuestring) + 1;
803 cred->publicData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
804 VERIFY_NON_NULL(TAG, (cred->publicData.data), ERROR);
805 memcpy(cred->publicData.data, jsonObj->valuestring, jsonObjLen);
807 else if (SIGNED_ASYMMETRIC_KEY == cred->credType && cJSON_Object == jsonObj->type)
809 cred->publicData.data = cJSON_PrintUnformatted(jsonObj);
810 VERIFY_NON_NULL(TAG, (cred->publicData.data), ERROR);
813 #endif // __WITH_X509__
814 //Period -- Not Mandatory
815 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PERIOD_NAME);
816 if(jsonObj && cJSON_String == jsonObj->type)
818 jsonObjLen = strlen(jsonObj->valuestring) + 1;
819 cred->period = (char *)OICMalloc(jsonObjLen);
820 VERIFY_NON_NULL(TAG, cred->period, ERROR);
821 strncpy(cred->period, jsonObj->valuestring, jsonObjLen);
824 //Owners -- Mandatory
825 jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_OWNERS_NAME);
826 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
827 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
828 cred->ownersLen = (size_t)cJSON_GetArraySize(jsonObj);
829 VERIFY_SUCCESS(TAG, cred->ownersLen > 0, ERROR);
830 cred->owners = (OicUuid_t*)OICCalloc(cred->ownersLen, sizeof(OicUuid_t));
831 VERIFY_NON_NULL(TAG, (cred->owners), ERROR);
832 for(size_t i = 0; i < cred->ownersLen; i++)
834 cJSON *jsonOwnr = cJSON_GetArrayItem(jsonObj, i);
835 VERIFY_NON_NULL(TAG, jsonOwnr, ERROR);
836 VERIFY_SUCCESS(TAG, cJSON_String == jsonOwnr->type, ERROR);
838 memset(base64Buff, 0, sizeof(base64Buff));
839 b64Ret = b64Decode(jsonOwnr->valuestring, strlen(jsonOwnr->valuestring),
840 base64Buff, sizeof(base64Buff), &outLen);
841 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK &&
842 outLen <= sizeof(cred->owners[i].id)), ERROR);
843 memcpy(cred->owners[i].id, base64Buff, outLen);
846 } while( ++idx < numCred);
852 cJSON_Delete(jsonRoot);
853 if (OC_STACK_OK != ret)
855 DeleteCredList(headCred);
861 static OicSecSvc_t* JSONToSvcBin(const char * jsonStr)
863 OCStackResult ret = OC_STACK_ERROR;
864 OicSecSvc_t * headSvc = NULL;
865 OicSecSvc_t * prevSvc = NULL;
866 cJSON *jsonRoot = NULL;
867 cJSON *jsonSvcArray = NULL;
869 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
871 jsonRoot = cJSON_Parse(jsonStr);
872 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
874 jsonSvcArray = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME);
875 VERIFY_NON_NULL(TAG, jsonSvcArray, INFO);
877 if (cJSON_Array == jsonSvcArray->type)
879 int numSvc = cJSON_GetArraySize(jsonSvcArray);
882 VERIFY_SUCCESS(TAG, numSvc > 0, INFO);
885 cJSON *jsonSvc = cJSON_GetArrayItem(jsonSvcArray, idx);
886 VERIFY_NON_NULL(TAG, jsonSvc, ERROR);
888 OicSecSvc_t *svc = (OicSecSvc_t*)OICCalloc(1, sizeof(OicSecSvc_t));
889 VERIFY_NON_NULL(TAG, svc, ERROR);
891 headSvc = (headSvc) ? headSvc : svc;
897 cJSON *jsonObj = NULL;
898 unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
900 B64Result b64Ret = B64_OK;
902 // Service Device Identity
903 jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_DEVICE_ID);
904 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
905 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
907 b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
908 sizeof(base64Buff), &outLen);
909 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->svcdid.id)), ERROR);
910 memcpy(svc->svcdid.id, base64Buff, outLen);
913 jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_TYPE);
914 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
915 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
916 svc->svct = (OicSecSvcType_t)jsonObj->valueint;
919 jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_OWNERS_NAME);
920 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
921 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
923 svc->ownersLen = (size_t)cJSON_GetArraySize(jsonObj);
924 VERIFY_SUCCESS(TAG, svc->ownersLen > 0, ERROR);
925 svc->owners = (OicUuid_t*)OICCalloc(svc->ownersLen, sizeof(OicUuid_t));
926 VERIFY_NON_NULL(TAG, (svc->owners), ERROR);
931 cJSON *jsonOwnr = cJSON_GetArrayItem(jsonObj, idxx);
932 VERIFY_NON_NULL(TAG, jsonOwnr, ERROR);
933 VERIFY_SUCCESS(TAG, cJSON_String == jsonOwnr->type, ERROR);
935 b64Ret = b64Decode(jsonOwnr->valuestring, strlen(jsonOwnr->valuestring), base64Buff,
936 sizeof(base64Buff), &outLen);
938 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->owners[idxx].id)),
940 memcpy(svc->owners[idxx].id, base64Buff, outLen);
941 } while ( ++idxx < svc->ownersLen);
944 } while( ++idx < numSvc);
950 cJSON_Delete(jsonRoot);
951 if (OC_STACK_OK != ret)
953 DeleteSVCList(headSvc);
959 static OicSecAmacl_t* JSONToAmaclBin(const char * jsonStr)
961 OCStackResult ret = OC_STACK_ERROR;
962 OicSecAmacl_t * headAmacl = NULL;
963 OicSecAmacl_t * prevAmacl = NULL;
964 cJSON *jsonRoot = NULL;
965 cJSON *jsonAmaclArray = NULL;
967 VERIFY_NON_NULL(TAG, jsonStr, ERROR);
969 jsonRoot = cJSON_Parse(jsonStr);
970 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
972 jsonAmaclArray = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
973 VERIFY_NON_NULL(TAG, jsonAmaclArray, INFO);
975 if (cJSON_Array == jsonAmaclArray->type)
977 int numAmacl = cJSON_GetArraySize(jsonAmaclArray);
980 VERIFY_SUCCESS(TAG, numAmacl > 0, INFO);
983 cJSON *jsonAmacl = cJSON_GetArrayItem(jsonAmaclArray, idx);
984 VERIFY_NON_NULL(TAG, jsonAmacl, ERROR);
986 OicSecAmacl_t *amacl = (OicSecAmacl_t*)OICCalloc(1, sizeof(OicSecAmacl_t));
987 VERIFY_NON_NULL(TAG, amacl, ERROR);
989 headAmacl = (headAmacl) ? headAmacl : amacl;
992 prevAmacl->next = amacl;
995 size_t jsonObjLen = 0;
996 cJSON *jsonObj = NULL;
998 // Resources -- Mandatory
999 jsonObj = cJSON_GetObjectItem(jsonAmacl, OIC_JSON_RESOURCES_NAME);
1000 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
1001 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
1003 amacl->resourcesLen = (size_t)cJSON_GetArraySize(jsonObj);
1004 VERIFY_SUCCESS(TAG, amacl->resourcesLen > 0, ERROR);
1005 amacl->resources = (char**)OICCalloc(amacl->resourcesLen, sizeof(char*));
1006 VERIFY_NON_NULL(TAG, (amacl->resources), ERROR);
1011 cJSON *jsonRsrc = cJSON_GetArrayItem(jsonObj, idxx);
1012 VERIFY_NON_NULL(TAG, jsonRsrc, ERROR);
1014 jsonObjLen = strlen(jsonRsrc->valuestring) + 1;
1015 amacl->resources[idxx] = (char*)OICMalloc(jsonObjLen);
1016 VERIFY_NON_NULL(TAG, (amacl->resources[idxx]), ERROR);
1017 OICStrcpy(amacl->resources[idxx], jsonObjLen, jsonRsrc->valuestring);
1018 } while ( ++idxx < amacl->resourcesLen);
1020 // Amss -- Mandatory
1021 VERIFY_SUCCESS( TAG, OC_STACK_OK == AddUuidArray(jsonAmacl, OIC_JSON_AMSS_NAME,
1022 &(amacl->amssLen), &(amacl->amss)), ERROR);
1024 // Owners -- Mandatory
1025 VERIFY_SUCCESS( TAG, OC_STACK_OK == AddUuidArray(jsonAmacl, OIC_JSON_OWNERS_NAME,
1026 &(amacl->ownersLen), &(amacl->owners)), ERROR);
1029 } while( ++idx < numAmacl);
1035 cJSON_Delete(jsonRoot);
1036 if (OC_STACK_OK != ret)
1038 DeleteAmaclList(headAmacl);
1044 int main(int argc, char* argv[])
1048 printf("JSON File Name: %s\n CBOR File Name: %s \n", argv[1], argv[2]);
1049 ConvertJsonToCBOR(argv[1], argv[2]);
1053 printf("This program requires two inputs:\n");
1054 printf("1. First input is a json file tha will be converted to cbor. \n");
1055 printf("2. Second input is a resulting cbor file that will store converted cbor. \n");
1056 printf("\t json2cbor <json_file_name> <cbor_file_name>. \n");