- Updates return type to OCStackResult and parameters as part of the conversion function.
- Changes in ownership transfer to return uint8_t instead of chari for payload conversion.
- security_internals includes function which are used in the testing.
- Unit test is updated to match the changes in conversion.
Change-Id: I088d75d67d8520e853db9b14fc802e3b31481e8b
Signed-off-by: Habib Virji <habib.virji@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/5096
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-on: https://gerrit.iotivity.org/gerrit/5797
delete jniPinListener;
jniPinListener = new JniPinCheckListener(env, jListener);
CBData.loadSecretCB = InputPinCodeCallback;
- CBData.createSecureSessionCB = CreateSecureSessionRandomPinCallbak;
+ CBData.createSecureSessionCB = CreateSecureSessionRandomPinCallback;
CBData.createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload;
CBData.createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload;
result = OCSecure::setOwnerTransferCallbackData((OicSecOxm_t)OxmType,
const OicSecDoxm_t* GetDoxmResourceData();
/**
- * This method converts JSON DOXM into binary DOXM.
- * The JSON DOXM can be from persistent database or
+ * This method converts CBOR DOXM into binary DOXM.
+ * The CBOR DOXM can be from persistent database or
* or received as PUT/POST request.
*
- * @param jsonStr is a doxm data in json string.
- *
+ * @param cborPayload is a doxm data in cbor.
* @note Caller needs to invoke OCFree after done using the return pointer.
+ * @param doxm is the pointer to @ref OicSecDoxm_t.
+ * @param size of the cborPayload. In case value is 0, CBOR_SIZE value is assigned.
*
- * @return pointer to @ref OicSecDoxm_t.
+ * @return ::OC_STACK_OK for Success, otherwise some error value.
*/
-OicSecDoxm_t * JSONToDoxmBin(const char * jsonStr);
+OCStackResult CBORPayloadToDoxm(const uint8_t *cborPayload, size_t size,
+ OicSecDoxm_t **doxm);
/**
- * This method converts DOXM data into JSON format.
+ * This method converts DOXM data into CBOR format.
* Caller needs to invoke 'free' when finished done using
* return string.
*
- * @param[in] doxm Pointer to OicSecDoxm_t.
+ * @param doxm Pointer to @ref OicSecDoxm_t.
* @note Caller needs to invoke OCFree after done using the return pointer.
+ * @param cborPayload is the payload of the cbor.
+ * @param cborSize is the size of the cbor payload. Passed parameter should not be NULL.
*
- * @return pointer to the json string.
+ * @return ::OC_STACK_OK for Success, otherwise some error value.
*/
-char * BinToDoxmJSON(const OicSecDoxm_t * doxm);
+OCStackResult DoxmToCBORPayload(const OicSecDoxm_t * doxm, uint8_t **cborPayload,
+ size_t *cborSize);
/**
* This method returns the SRM device ID for this device.
OCStackResult CBORPayloadToCred(const uint8_t *cborPayload, size_t size,
OicSecCred_t **secCred);
+/**
+ * This internal method is used to create '/oic/sec/doxm' resource.
+ */
+OCStackResult CreateDoxmResource();
+
+/**
+ * This internal method is the entity handler for DOXM resources.
+ */
+OCEntityHandlerResult DoxmEntityHandler(OCEntityHandlerFlag flag,
+ OCEntityHandlerRequest * ehRequest,
+ void* callbackParam);
+
#ifdef __cplusplus
}
#endif
/*\r
* Callback for creating CoAP payload.\r
*/\r
-typedef char* (*OTMCreatePayloadCallback)(OTMContext_t* otmCtx);\r
+typedef OCStackResult (*OTMCreatePayloadCallback)(OTMContext_t* otmCtx, uint8_t **payload, \r
+ size_t *size);\r
\r
/**\r
* Required callback for performing ownership transfer\r
-/* *****************************************************************
- *
- * Copyright 2015 Samsung Electronics All Rights Reserved.
- *
- *
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * *****************************************************************/
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#ifndef OXM_JUST_WORKS_H_
#define OXM_JUST_WORKS_H_
/**
* In case of just works OxM, no need to implement.
+ *
+ * @return ::OC_STACK_SUCCESS in case of success and other value otherwise.
*/
OCStackResult LoadSecretJustWorksCallback(OTMContext_t* UNUSED_PARAM);
/**
* To establish a secure channel with anonymous cipher suite
*
- * @param[in] selectedDeviceInfo Selected device infomation
- * @return OC_STACK_SUCCESS in case of success and other value otherwise.
+ * @param otmCtx Context of OTM, It includes current device information.
+ *
+ * @return ::OC_STACK_SUCCESS in case of success and other value otherwise.
*/
OCStackResult CreateSecureSessionJustWorksCallback(OTMContext_t* otmCtx);
/**
* Generate payload for select OxM request.
*
- * @param[in] selectedDeviceInfo Selected device infomation
- * @return DOXM JSON payload including the selected OxM.
- * NOTE : Returned memory should be deallocated by caller.
+ * @param otmCtx Context of OTM, It includes current device information.
+ * @param cborPayload is the DOXM CBOR payload including the selected OxM.
+ * @note Returned memory should be deallocated by caller.
+ * @param cborSize is the size of the cborPayload.
+ *
+ * @return ::OC_STACK_SUCCESS in case of success and other value otherwise.
*/
-char* CreateJustWorksSelectOxmPayload(OTMContext_t* otmCtx);
+OCStackResult CreateJustWorksSelectOxmPayload(OTMContext_t *otmCtx, uint8_t **cborPayload,
+ size_t *cborSize);
/**
* Generate payload for owner transfer request.
*
- * @param[in] selectedDeviceInfo Selected device infomation
- * @return DOXM JSON payload including the owner information.
- * NOTE : Returned memory should be deallocated by caller.
+ * @param otmCtx Context of OTM, It includes current device information.
+ * @param cborPayload is the DOXM CBOR payload including the owner information.
+ * @note Returned memory should be deallocated by caller.
+ * @param cborSize is the size of the cborPayload.
+ *
+ * @return ::OC_STACK_SUCCESS in case of success and other value otherwise.
*/
-char* CreateJustWorksOwnerTransferPayload(OTMContext_t* otmCtx);
-
+OCStackResult CreateJustWorksOwnerTransferPayload(OTMContext_t *otmCtx, uint8_t **cborPayload,
+ size_t *cborSize);
#ifdef __cplusplus
}
#endif
-#endif //OXM_JUST_WORKS_H_
\ No newline at end of file
+#endif //OXM_JUST_WORKS_H_
-/* *****************************************************************
- *
- * Copyright 2015 Samsung Electronics All Rights Reserved.
- *
- *
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * *****************************************************************/
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#ifndef OXM_RANDOM_PIN_H_
#define OXM_RANDOM_PIN_H_
/**
* Callback implementation to input the PIN code from user.
*
- * @otmCtx Context of OTM, It includes current device infomation.
- * @return OC_STACK_SUCCESS in case of success and other value otherwise.
+ * @param otmCtx Context of OTM, It includes current device information.
+ *
+ * @return ::OC_STACK_SUCCESS in case of success and other value otherwise.
*/
-OCStackResult InputPinCodeCallback(OTMContext_t* otmCtx);
+OCStackResult InputPinCodeCallback(OTMContext_t *otmCtx);
/**
- * Callback implemenration to establish a secure channel with PSK cipher suite
+ * Callback implemenration to establish a secure channel with PSK cipher suite.
+ *
+ * @param otmCtx Context of OTM, It includes current device information.
*
- * @param[in] selectedDeviceInfo Selected device infomation
- * @return OC_STACK_SUCCESS in case of success and other value otherwise.
+ * @return ::OC_STACK_SUCCESS in case of success and other value otherwise.
*/
-OCStackResult CreateSecureSessionRandomPinCallbak(OTMContext_t* otmCtx);
+OCStackResult CreateSecureSessionRandomPinCallback(OTMContext_t *otmCtx);
/**
* Generate payload for select OxM request.
*
- * @param[in] selectedDeviceInfo Selected device infomation
- * @return DOXM JSON payload including the selected OxM.
- * NOTE : Returned memory should be deallocated by caller.
+ * @param otmCtx Context of OTM, It includes current device information.
+ * @param cborPaylaod is the DOXM CBOR payload including the selected OxM.
+ * @note Returned memory should be deallocated by caller.
+ * @param cborSize is the size of the cborPayload.
+ *
+ * @return ::OC_STACK_SUCCESS in case of success and other value otherwise.
*/
-char* CreatePinBasedSelectOxmPayload(OTMContext_t* otmCtx);
+OCStackResult CreatePinBasedSelectOxmPayload(OTMContext_t *otmCtx, uint8_t **cborPayload,
+ size_t *cborSize);
/**
* Generate payload for owner transfer request.
*
- * @param[in] selectedDeviceInfo Selected device infomation
- * @return DOXM JSON payload including the owner information.
- * NOTE : Returned memory should be deallocated by caller.
+ * @param otmCtx Context of OTM, It includes current device information.
+ * @param cborPaylaod is the DOXM CBOR payload including the owner information.
+ * @note Returned memory should be deallocated by caller.
+ * @param cborSize is the size of the cborPayload.
+ *
+ * @return ::OC_STACK_SUCCESS in case of success and other value otherwise.
*/
-char* CreatePinBasedOwnerTransferPayload(OTMContext_t* otmCtx);
+OCStackResult CreatePinBasedOwnerTransferPayload(OTMContext_t *otmCtx, uint8_t **cborPayload,
+ size_t *cborSize);
#ifdef __cplusplus
}
return -1;
}
otmcb.loadSecretCB = InputPinCodeCallback;
- otmcb.createSecureSessionCB = CreateSecureSessionRandomPinCallbak;
+ otmcb.createSecureSessionCB = CreateSecureSessionRandomPinCallback;
otmcb.createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload;
otmcb.createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload;
if(OC_STACK_OK != OCSetOwnerTransferCallbackData(OIC_RANDOM_DEVICE_PIN, &otmcb))
return OC_STACK_NO_MEMORY;
}
secPayload->base.type = PAYLOAD_TYPE_SECURITY;
- secPayload->securityData = g_OTMDatas[selectedOxm].createSelectOxmPayloadCB(otmCtx);
- if (NULL == secPayload->securityData)
+ size_t size = 0;
+ OCStackResult res = g_OTMDatas[selectedOxm].createSelectOxmPayloadCB(otmCtx,
+ &secPayload->securityData1,
+ &size);
+ if (OC_STACK_OK != res && NULL == secPayload->securityData1)
{
- OICFree(secPayload);
- OIC_LOG(ERROR, TAG, "Error while converting bin to json");
+ OCPayloadDestroy((OCPayload *)secPayload);
+ OIC_LOG(ERROR, TAG, "Error while converting bin to cbor");
return OC_STACK_ERROR;
}
cbData.cb = &OwnerTransferModeHandler;
cbData.context = (void *)otmCtx;
cbData.cd = NULL;
- OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query,
- &deviceInfo->endpoint, (OCPayload*)secPayload,
- deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
+ res = OCDoResource(NULL, OC_REST_PUT, query,
+ &deviceInfo->endpoint, (OCPayload *)secPayload,
+ deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
if (res != OC_STACK_OK)
{
OIC_LOG(ERROR, TAG, "OCStack resource error");
return OC_STACK_NO_MEMORY;
}
secPayload->base.type = PAYLOAD_TYPE_SECURITY;
- secPayload->securityData =
- g_OTMDatas[deviceInfo->doxm->oxmSel].createOwnerTransferPayloadCB(otmCtx);
- if (NULL == secPayload->securityData)
+ size_t size = 0;
+ OCStackResult res = g_OTMDatas[deviceInfo->doxm->oxmSel].createOwnerTransferPayloadCB(
+ otmCtx, &secPayload->securityData1, &size);
+ if (NULL == secPayload->securityData1)
{
- OICFree(secPayload);
- OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
+ OCPayloadDestroy((OCPayload *)secPayload);
+ OIC_LOG(ERROR, TAG, "Error while converting doxm bin to cbor.");
return OC_STACK_INVALID_PARAM;
}
- OIC_LOG_V(DEBUG, TAG, "Payload : %s", secPayload->securityData);
+ OIC_LOG_V(DEBUG, TAG, "Payload : %s", secPayload->securityData1);
OCCallbackData cbData;
cbData.cb = &OwnerUuidUpdateHandler;
cbData.context = (void *)otmCtx;
cbData.cd = NULL;
- OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
- deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
+ res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload *)secPayload,
+ deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
if (res != OC_STACK_OK)
{
OIC_LOG(ERROR, TAG, "OCStack resource error");
}
otmCtx->selectedDeviceInfo->doxm->owned = true;
- secPayload->securityData = BinToDoxmJSON(otmCtx->selectedDeviceInfo->doxm);
- if (NULL == secPayload->securityData)
+
+ secPayload->base.type = PAYLOAD_TYPE_SECURITY;
+ size_t size = 0;
+ OCStackResult res = DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm,
+ &secPayload->securityData1, &size);
+ if (OC_STACK_OK != res && NULL == secPayload->securityData1)
{
- OICFree(secPayload);
+ OCPayloadDestroy((OCPayload *)secPayload);
OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
return OC_STACK_INVALID_PARAM;
}
cbData.context = (void *)otmCtx;
cbData.cd = NULL;
- OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
- deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
+ res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
+ deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
if (res != OC_STACK_OK)
{
OIC_LOG(ERROR, TAG, "OCStack resource error");
#define TAG "OXM_JustWorks"
-char* CreateJustWorksSelectOxmPayload(OTMContext_t* otmCtx)
+OCStackResult CreateJustWorksSelectOxmPayload(OTMContext_t *otmCtx, uint8_t **payload, size_t *size)
{
- if(!otmCtx || !otmCtx->selectedDeviceInfo)
+ if (!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
{
- return NULL;
+ return OC_STACK_INVALID_PARAM;
}
otmCtx->selectedDeviceInfo->doxm->oxmSel = OIC_JUST_WORKS;
- return BinToDoxmJSON(otmCtx->selectedDeviceInfo->doxm);
+ *payload = NULL;
+ *size = 0;
+
+ return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size);
}
-char* CreateJustWorksOwnerTransferPayload(OTMContext_t* otmCtx)
+OCStackResult CreateJustWorksOwnerTransferPayload(OTMContext_t* otmCtx, uint8_t **payload, size_t *size)
{
- if(!otmCtx || !otmCtx->selectedDeviceInfo)
+ if (!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
{
- return NULL;
+ return OC_STACK_INVALID_PARAM;
}
OicUuid_t uuidPT = {.id={0}};
if (OC_STACK_OK != GetDoxmDeviceID(&uuidPT))
{
OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
- return NULL;
+ return OC_STACK_ERROR;
}
memcpy(otmCtx->selectedDeviceInfo->doxm->owner.id, uuidPT.id , UUID_LENGTH);
- return BinToDoxmJSON(otmCtx->selectedDeviceInfo->doxm);
+ *payload = NULL;
+ *size = 0;
+
+ return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size);;
}
OCStackResult LoadSecretJustWorksCallback(OTMContext_t* UNUSED_PARAM)
OCStackResult CreateSecureSessionJustWorksCallback(OTMContext_t* otmCtx)
{
OIC_LOG(INFO, TAG, "IN CreateSecureSessionJustWorksCallback");
- if(!otmCtx || !otmCtx->selectedDeviceInfo)
+ if (!otmCtx || !otmCtx->selectedDeviceInfo)
{
return OC_STACK_INVALID_PARAM;
}
}
OIC_LOG(INFO, TAG, "TLS_ECDH_anon_WITH_AES_128_CBC_SHA_256 cipher suite selected.");
- OCProvisionDev_t* selDevInfo = otmCtx->selectedDeviceInfo;
+ OCProvisionDev_t *selDevInfo = otmCtx->selectedDeviceInfo;
CAEndpoint_t *endpoint = (CAEndpoint_t *)OICCalloc(1, sizeof (CAEndpoint_t));
if(NULL == endpoint)
{
return OC_STACK_NO_MEMORY;
}
- memcpy(endpoint,&selDevInfo->endpoint,sizeof(CAEndpoint_t));
+ memcpy(endpoint, &selDevInfo->endpoint, sizeof(CAEndpoint_t));
endpoint->port = selDevInfo->securePort;
caresult = CAInitiateHandshake(endpoint);
#define TAG "OXM_RandomPIN"
-char* CreatePinBasedSelectOxmPayload(OTMContext_t* otmCtx)
+OCStackResult CreatePinBasedSelectOxmPayload(OTMContext_t* otmCtx, uint8_t **payload, size_t *size)
{
- if(!otmCtx || !otmCtx->selectedDeviceInfo)
+ *payload = NULL;
+ *size = 0;
+ if(!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
{
- return NULL;
+ return OC_STACK_INVALID_PARAM;
}
otmCtx->selectedDeviceInfo->doxm->oxmSel = OIC_RANDOM_DEVICE_PIN;
- return BinToDoxmJSON(otmCtx->selectedDeviceInfo->doxm);
+ return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size);
}
-char* CreatePinBasedOwnerTransferPayload(OTMContext_t* otmCtx)
+OCStackResult CreatePinBasedOwnerTransferPayload(OTMContext_t* otmCtx, uint8_t **payload, size_t *size)
{
- if(!otmCtx || !otmCtx->selectedDeviceInfo)
+ if(!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
{
- return NULL;
+ return OC_STACK_INVALID_PARAM;
}
OicUuid_t uuidPT = {.id={0}};
+ *payload = NULL;
+ *size = 0;
if (OC_STACK_OK != GetDoxmDeviceID(&uuidPT))
{
OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
- return NULL;
+ return OC_STACK_ERROR;
}
memcpy(otmCtx->selectedDeviceInfo->doxm->owner.id, uuidPT.id , UUID_LENGTH);
- return BinToDoxmJSON(otmCtx->selectedDeviceInfo->doxm);
+ return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size);
}
-OCStackResult InputPinCodeCallback(OTMContext_t* otmCtx)
+OCStackResult InputPinCodeCallback(OTMContext_t *otmCtx)
{
- if(!otmCtx || !otmCtx->selectedDeviceInfo)
+ if (!otmCtx || !otmCtx->selectedDeviceInfo)
{
return OC_STACK_INVALID_PARAM;
}
uint8_t pinData[OXM_RANDOM_PIN_SIZE + 1];
OCStackResult res = InputPin((char*)pinData, OXM_RANDOM_PIN_SIZE + 1);
- if(OC_STACK_OK != res)
+ if (OC_STACK_OK != res)
{
OIC_LOG(ERROR, TAG, "Failed to input PIN");
return res;
return res;
}
-OCStackResult CreateSecureSessionRandomPinCallbak(OTMContext_t* otmCtx)
+OCStackResult CreateSecureSessionRandomPinCallback(OTMContext_t* otmCtx)
{
OIC_LOG(INFO, TAG, "IN CreateSecureSessionRandomPinCallbak");
- if(!otmCtx || !otmCtx->selectedDeviceInfo)
+ if (!otmCtx || !otmCtx->selectedDeviceInfo)
{
return OC_STACK_INVALID_PARAM;
}
}
OIC_LOG(INFO, TAG, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256 cipher suite selected.");
-
OCProvisionDev_t* selDevInfo = otmCtx->selectedDeviceInfo;
CAEndpoint_t *endpoint = (CAEndpoint_t *)OICCalloc(1, sizeof (CAEndpoint_t));
- if(NULL == endpoint)
+ if (NULL == endpoint)
{
return OC_STACK_NO_MEMORY;
}
return OC_STACK_OK;
}
-
OIC_LOG(INFO, TAG, "Unknown payload type");
return OC_STACK_KEEP_TRANSACTION;
}
- OicSecDoxm_t *ptrDoxm = JSONToDoxmBin(
- ((OCSecurityPayload*)clientResponse->payload)->securityData);
- if (NULL == ptrDoxm)
+ OicSecDoxm_t *ptrDoxm = NULL;
+ uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData1;
+ OCStackResult res = CBORPayloadToDoxm(payload, 0, &ptrDoxm);
+ if ((NULL == ptrDoxm) && (OC_STACK_OK != res))
{
- OIC_LOG(INFO, TAG, "Ignoring malformed JSON");
+ OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
return OC_STACK_KEEP_TRANSACTION;
}
else
{
- OIC_LOG(DEBUG, TAG, "Successfully converted doxm json to bin.");
+ OIC_LOG(DEBUG, TAG, "Successfully converted doxm cbor to bin.");
//If this is owend device discovery we have to filter out the responses.
DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
TEST(JustWorksOxMTest, NullParam)
{
-
OTMContext_t* otmCtx = NULL;
- OCStackResult res = OC_STACK_ERROR;
- char* payloadRes;
+ uint8_t *payloadRes = NULL;
+ size_t size = 0;
//LoadSecretJustWorksCallback always returns OC_STACK_OK.
- res = LoadSecretJustWorksCallback(otmCtx);
- EXPECT_TRUE(OC_STACK_OK == res);
+ EXPECT_EQ(OC_STACK_OK, LoadSecretJustWorksCallback(otmCtx));
- res = CreateSecureSessionJustWorksCallback(otmCtx);
- EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CreateSecureSessionJustWorksCallback(otmCtx));
- payloadRes = CreateJustWorksSelectOxmPayload(otmCtx);
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CreateJustWorksSelectOxmPayload(otmCtx, &payloadRes, &size));
EXPECT_TRUE(NULL == payloadRes);
- payloadRes = CreateJustWorksOwnerTransferPayload(otmCtx);
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CreateJustWorksOwnerTransferPayload(otmCtx, &payloadRes, &size));
EXPECT_TRUE(NULL == payloadRes);
OTMContext_t otmCtx2;
otmCtx2.selectedDeviceInfo = NULL;
//LoadSecretJustWorksCallback always returns OC_STACK_OK.
- res = LoadSecretJustWorksCallback(&otmCtx2);
- EXPECT_TRUE(OC_STACK_OK == res);
+ EXPECT_EQ(OC_STACK_OK, LoadSecretJustWorksCallback(&otmCtx2));
- res = CreateSecureSessionJustWorksCallback(&otmCtx2);
- EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CreateSecureSessionJustWorksCallback(&otmCtx2));
- payloadRes = CreateJustWorksSelectOxmPayload(&otmCtx2);
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CreateJustWorksSelectOxmPayload(&otmCtx2, &payloadRes, &size));
EXPECT_TRUE(NULL == payloadRes);
- payloadRes = CreateJustWorksOwnerTransferPayload(&otmCtx2);
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CreateJustWorksOwnerTransferPayload(&otmCtx2, &payloadRes, &size));
EXPECT_TRUE(NULL == payloadRes);
}
TEST(RandomPinOxMTest, NullParam)
{
OTMContext_t* otmCtx = NULL;
- OCStackResult res = OC_STACK_ERROR;
- char* payloadRes;
+ uint8_t *payloadRes = NULL;
+ size_t size = 0;
//LoadSecretJustWorksCallback always returns OC_STACK_OK.
- res = InputPinCodeCallback(otmCtx);
- EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, InputPinCodeCallback(otmCtx));
- res = CreateSecureSessionRandomPinCallbak(otmCtx);
- EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CreateSecureSessionRandomPinCallback(otmCtx));
- payloadRes = CreatePinBasedSelectOxmPayload(otmCtx);
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CreatePinBasedSelectOxmPayload(otmCtx, &payloadRes, &size));
EXPECT_TRUE(NULL == payloadRes);
- payloadRes = CreatePinBasedOwnerTransferPayload(otmCtx);
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CreatePinBasedOwnerTransferPayload(otmCtx, &payloadRes, &size));
EXPECT_TRUE(NULL == payloadRes);
OTMContext_t otmCtx2;
otmCtx2.selectedDeviceInfo = NULL;
//LoadSecretJustWorksCallback always returns OC_STACK_OK.
- res = InputPinCodeCallback(&otmCtx2);
- EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, InputPinCodeCallback(&otmCtx2));
- res = CreateSecureSessionRandomPinCallbak(&otmCtx2);
- EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CreateSecureSessionRandomPinCallback(&otmCtx2));
- payloadRes = CreatePinBasedSelectOxmPayload(&otmCtx2);
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CreatePinBasedSelectOxmPayload(&otmCtx2, &payloadRes, &size));
EXPECT_TRUE(NULL == payloadRes);
- payloadRes = CreatePinBasedOwnerTransferPayload(&otmCtx2);
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CreatePinBasedOwnerTransferPayload(&otmCtx2, &payloadRes, &size));
EXPECT_TRUE(NULL == payloadRes);
}
}
OicSecDoxm_t *doxm = NULL;
+
OIC_LOG_V(INFO, TAG, "Doxm DeviceId Discovery response = %s\n",
- ((OCSecurityPayload*)clientResponse->payload)->securityData);
- doxm = JSONToDoxmBin(((OCSecurityPayload*)clientResponse->payload)->securityData);
+ ((OCSecurityPayload*)clientResponse->payload)->securityData1);
+ uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData1;
//As doxm is NULL amsmgr can't test if response from trusted AMS service
//so keep the transaction.
- if (NULL == doxm)
+ if (OC_STACK_OK == CBORPayloadToDoxm(payload, 0, &doxm))
{
- OIC_LOG_V(ERROR, TAG, "%s : Unable to convert JSON to Binary",__func__);
+ OIC_LOG_V(ERROR, TAG, "%s : Unable to convert CBOR to Binary",__func__);
return OC_STACK_KEEP_TRANSACTION;
}
#endif
#include "ocstack.h"
-#include "logger.h"
#include "oic_malloc.h"
-#include "cJSON.h"
+#include "payload_logging.h"
+#include "utlist.h"
+#include "ocrandom.h"
+#include "ocpayload.h"
+#include "cainterface.h"
+#include "ocserverrequest.h"
#include "resourcemanager.h"
#include "doxmresource.h"
#include "pstatresource.h"
#include "aclresource.h"
#include "psinterface.h"
-#include "utlist.h"
#include "srmresourcestrings.h"
#include "securevirtualresourcetypes.h"
-#include "base64.h"
-#include "ocrandom.h"
-#include "cainterface.h"
#include "credresource.h"
-#include "ocserverrequest.h"
#include "srmutility.h"
#include "pinoxmcommon.h"
#define TAG "SRM-DOXM"
+/** Default cbor payload size. This value is increased in case of CborErrorOutOfMemory.
+ * The value of payload size is increased until reaching belox max cbor size. */
+static const uint8_t CBOR_SIZE = 255;
+
+/** Max cbor size payload. */
+static const uint16_t CBOR_MAX_SIZE = 4400;
+
+/** DOXM Map size - Number of mandatory items. */
+static const uint8_t DOXM_MAP_SIZE = 5;
+
static OicSecDoxm_t *gDoxm = NULL;
static OCResourceHandle gDoxmHandle = NULL;
}
}
-char * BinToDoxmJSON(const OicSecDoxm_t * doxm)
+OCStackResult DoxmToCBORPayload(const OicSecDoxm_t *doxm, uint8_t **payload, size_t *size)
{
- if (NULL == doxm)
+ if (NULL == doxm || NULL == payload || NULL != *payload || NULL == size)
{
- return NULL;
+ return OC_STACK_INVALID_PARAM;
}
- char *jsonStr = NULL;
- cJSON *jsonDoxm = NULL;
- char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*)0)->id)) + 1] = {};
- uint32_t outLen = 0;
- B64Result b64Ret = B64_OK;
+ size_t cborLen = *size;
+ if (0 == cborLen)
+ {
+ cborLen = CBOR_SIZE;
+ }
+
+ *payload = NULL;
+ *size = 0;
- cJSON *jsonRoot = cJSON_CreateObject();
- VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
+ OCStackResult ret = OC_STACK_ERROR;
- jsonDoxm = cJSON_CreateObject();
- VERIFY_NON_NULL(TAG, jsonDoxm, ERROR);
- cJSON_AddItemToObject(jsonRoot, OIC_JSON_DOXM_NAME, jsonDoxm );
+ CborEncoder encoder = { {.ptr = NULL }, .end = 0 };
+ CborEncoder doxmMap = { {.ptr = NULL }, .end = 0 };
+
+ CborError cborEncoderResult = CborNoError;
+
+ uint8_t mapSize = DOXM_MAP_SIZE;
+
+ if (doxm->oxmTypeLen > 0)
+ {
+ mapSize++;
+ }
+ if (doxm->oxmLen > 0)
+ {
+ mapSize++;
+ }
+
+ uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborLen);
+ VERIFY_NON_NULL(TAG, outPayload, ERROR);
+ cbor_encoder_init(&encoder, outPayload, cborLen, 0);
+
+ cborEncoderResult = cbor_encoder_create_map(&encoder, &doxmMap, mapSize);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Map.");
//OxmType -- Not Mandatory
if (doxm->oxmTypeLen > 0)
{
- cJSON *jsonOxmTyArray = cJSON_CreateArray();
- VERIFY_NON_NULL(TAG, jsonOxmTyArray, ERROR);
- cJSON_AddItemToObject (jsonDoxm, OIC_JSON_OXM_TYPE_NAME, jsonOxmTyArray );
+ cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_OXM_TYPE_NAME,
+ strlen(OIC_JSON_OXM_TYPE_NAME));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmType Tag.");
+ CborEncoder oxmType = { {.ptr = NULL }, .end = 0 };
+ cborEncoderResult = cbor_encoder_create_array(&doxmMap, &oxmType, doxm->oxmTypeLen);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmType Array.");
+
for (size_t i = 0; i < doxm->oxmTypeLen; i++)
{
- cJSON_AddItemToArray (jsonOxmTyArray, cJSON_CreateString(doxm->oxmType[i]));
+ cborEncoderResult = cbor_encode_text_string(&oxmType, doxm->oxmType[i],
+ strlen(doxm->oxmType[i]));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmType Value.");
}
+ cborEncoderResult = cbor_encoder_close_container(&doxmMap, &oxmType);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing oxmType.");
}
//Oxm -- Not Mandatory
if (doxm->oxmLen > 0)
{
- cJSON *jsonOxmArray = cJSON_CreateArray();
- VERIFY_NON_NULL(TAG, jsonOxmArray, ERROR);
- cJSON_AddItemToObject (jsonDoxm, OIC_JSON_OXM_NAME,jsonOxmArray );
+ cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_OXM_NAME,
+ strlen(OIC_JSON_OXM_NAME));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmName Tag.");
+ CborEncoder oxm = { {.ptr = NULL }, .end = 0 };
+ cborEncoderResult = cbor_encoder_create_array(&doxmMap, &oxm, doxm->oxmLen);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmName Array.");
+
for (size_t i = 0; i < doxm->oxmLen; i++)
{
- cJSON_AddItemToArray (jsonOxmArray, cJSON_CreateNumber(doxm->oxm[i]));
+ cborEncoderResult = cbor_encode_int(&oxm, doxm->oxm[i]);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmName Value");
}
+ cborEncoderResult = cbor_encoder_close_container(&doxmMap, &oxm);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing oxmName.");
}
//OxmSel -- Mandatory
- cJSON_AddNumberToObject(jsonDoxm, OIC_JSON_OXM_SEL_NAME, (int)doxm->oxmSel);
+ cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_OXM_SEL_NAME,
+ strlen(OIC_JSON_OXM_SEL_NAME));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Sel Tag.");
+ cborEncoderResult = cbor_encode_int(&doxmMap, doxm->oxmSel);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Sel Value.");
//sct -- Mandatory
- cJSON_AddNumberToObject(jsonDoxm, OIC_JSON_SUPPORTED_CRED_TYPE_NAME, (int)doxm->sct);
+ cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_SUPPORTED_CRED_TYPE_NAME,
+ strlen(OIC_JSON_SUPPORTED_CRED_TYPE_NAME));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Type Tag");
+ cborEncoderResult = cbor_encode_int(&doxmMap, doxm->sct);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Type Value.");
//Owned -- Mandatory
- cJSON_AddBoolToObject(jsonDoxm, OIC_JSON_OWNED_NAME, doxm->owned);
+ cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_OWNED_NAME,
+ strlen(OIC_JSON_OWNED_NAME));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Owned Tag.");
+ cborEncoderResult = cbor_encode_boolean(&doxmMap, doxm->owned);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Owned Value.");
//TODO: Need more clarification on deviceIDFormat field type.
#if 0
#endif
//DeviceId -- Mandatory
- outLen = 0;
- b64Ret = b64Encode(doxm->deviceID.id, sizeof(doxm->deviceID.id), base64Buff,
- sizeof(base64Buff), &outLen);
- VERIFY_SUCCESS(TAG, b64Ret == B64_OK, ERROR);
- cJSON_AddStringToObject(jsonDoxm, OIC_JSON_DEVICE_ID_NAME, base64Buff);
+ cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_DEVICE_ID_NAME,
+ strlen(OIC_JSON_DEVICE_ID_NAME));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Id Tag.");
+ cborEncoderResult = cbor_encode_byte_string(&doxmMap, doxm->deviceID.id,
+ sizeof(doxm->deviceID.id));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Id Value.");
//DPC -- Mandatory
- cJSON_AddBoolToObject(jsonDoxm, OIC_JSON_DPC_NAME, doxm->dpc);
+ cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_DPC_NAME,
+ strlen(OIC_JSON_DPC_NAME));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DPC Tag.");
+ cborEncoderResult = cbor_encode_boolean(&doxmMap, doxm->dpc);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DPC Value.");
//Owner -- Mandatory
- outLen = 0;
- b64Ret = b64Encode(doxm->owner.id, sizeof(doxm->owner.id), base64Buff,
- sizeof(base64Buff), &outLen);
- VERIFY_SUCCESS(TAG, b64Ret == B64_OK, ERROR);
- cJSON_AddStringToObject(jsonDoxm, OIC_JSON_OWNER_NAME, base64Buff);
-
- jsonStr = cJSON_PrintUnformatted(jsonRoot);
+ cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_OWNER_NAME,
+ strlen(OIC_JSON_OWNER_NAME));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Owner tag.");
+ cborEncoderResult = cbor_encode_byte_string(&doxmMap, doxm->owner.id,
+ sizeof(doxm->owner.id));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Owner Value");
+
+ cborEncoderResult = cbor_encoder_close_container(&encoder, &doxmMap);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing DoxmMap.");
+
+ *size = encoder.ptr - outPayload;
+ *payload = outPayload;
+ ret = OC_STACK_OK;
exit:
- if (jsonRoot)
+ if ((CborErrorOutOfMemory == cborEncoderResult) && (cborLen < CBOR_MAX_SIZE))
+ {
+ // reallocate and try again!
+ OICFree(outPayload);
+ // Since the allocated initial memory failed, double the memory.
+ cborLen += encoder.ptr - encoder.end;
+ cborEncoderResult = CborNoError;
+ ret = DoxmToCBORPayload(doxm, payload, &cborLen);
+ }
+
+ if ((CborNoError != cborEncoderResult) || (OC_STACK_OK != ret))
{
- cJSON_Delete(jsonRoot);
+ OICFree(outPayload);
+ outPayload = NULL;
+ *payload = NULL;
+ *size = 0;
+ ret = OC_STACK_ERROR;
}
- return jsonStr;
+
+ return ret;
}
-OicSecDoxm_t * JSONToDoxmBin(const char * jsonStr)
+OCStackResult CBORPayloadToDoxm(const uint8_t *cborPayload, size_t size,
+ OicSecDoxm_t **secDoxm)
{
- if (NULL == jsonStr)
+ if (NULL == cborPayload || NULL == secDoxm || NULL != *secDoxm)
{
- return NULL;
+ return OC_STACK_INVALID_PARAM;
}
OCStackResult ret = OC_STACK_ERROR;
- OicSecDoxm_t *doxm = NULL;
- cJSON *jsonDoxm = NULL;
- cJSON *jsonObj = NULL;
-
- size_t jsonObjLen = 0;
- unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
- uint32_t outLen = 0;
- B64Result b64Ret = B64_OK;
-
- cJSON *jsonRoot = cJSON_Parse(jsonStr);
- VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
+ *secDoxm = NULL;
- jsonDoxm = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME);
- VERIFY_NON_NULL(TAG, jsonDoxm, ERROR);
+ CborValue doxmCbor = { .parser = NULL };
+ CborParser parser = { .end = NULL };
+ CborError cborFindResult = CborNoError;
+ int cborLen = size;
+ if (0 == size)
+ {
+ cborLen = CBOR_SIZE;
+ }
+ cbor_parser_init(cborPayload, cborLen, 0, &parser, &doxmCbor);
+ CborValue doxmMap = { .parser = NULL } ;
+ OicSecDoxm_t *doxm = NULL;
+ cborFindResult = cbor_value_enter_container(&doxmCbor, &doxmMap);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering Doxm Map.")
- doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
+ doxm = (OicSecDoxm_t *)OICCalloc(1, sizeof(*doxm));
VERIFY_NON_NULL(TAG, doxm, ERROR);
- //OxmType -- not Mandatory
- jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_TYPE_NAME);
- if ((jsonObj) && (cJSON_Array == jsonObj->type))
+ while (cbor_value_is_valid(&doxmMap))
{
- doxm->oxmTypeLen = (size_t)cJSON_GetArraySize(jsonObj);
- VERIFY_SUCCESS(TAG, doxm->oxmTypeLen > 0, ERROR);
+ char *name = NULL;
+ size_t len = 0;
+ cborFindResult = cbor_value_dup_text_string(&doxmMap, &name, &len, NULL);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Doxm Map Name.")
+ cborFindResult = cbor_value_advance(&doxmMap);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing Doxm Map.")
- doxm->oxmType = (OicUrn_t *)OICCalloc(doxm->oxmTypeLen, sizeof(char *));
- VERIFY_NON_NULL(TAG, (doxm->oxmType), ERROR);
+ CborType type = cbor_value_get_type(&doxmMap);
- for (size_t i = 0; i < doxm->oxmTypeLen ; i++)
+ //OxmType -- not Mandatory
+ if (0 == strcmp(OIC_JSON_OXM_TYPE_NAME, name))
{
- cJSON *jsonOxmTy = cJSON_GetArrayItem(jsonObj, i);
- VERIFY_NON_NULL(TAG, jsonOxmTy, ERROR);
+ CborValue oxmType = { .parser = NULL };
- jsonObjLen = strlen(jsonOxmTy->valuestring) + 1;
- doxm->oxmType[i] = (char*)OICMalloc(jsonObjLen);
- VERIFY_NON_NULL(TAG, doxm->oxmType[i], ERROR);
- strncpy((char *)doxm->oxmType[i], (char *)jsonOxmTy->valuestring, jsonObjLen);
- }
- }
+ cborFindResult = cbor_value_get_array_length(&doxmMap, &doxm->oxmTypeLen);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding oxmTypeLen.")
+ VERIFY_SUCCESS(TAG, doxm->oxmTypeLen != 0, ERROR);
- //Oxm -- not Mandatory
- jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_NAME);
- if (jsonObj && cJSON_Array == jsonObj->type)
- {
- doxm->oxmLen = (size_t)cJSON_GetArraySize(jsonObj);
- VERIFY_SUCCESS(TAG, doxm->oxmLen > 0, ERROR);
+ doxm->oxmType = (OicUrn_t *)OICCalloc(doxm->oxmTypeLen, sizeof(*doxm->oxmType));
+ VERIFY_NON_NULL(TAG, doxm->oxmType, ERROR);
- doxm->oxm = (OicSecOxm_t*)OICCalloc(doxm->oxmLen, sizeof(OicSecOxm_t));
- VERIFY_NON_NULL(TAG, doxm->oxm, ERROR);
+ cborFindResult = cbor_value_enter_container(&doxmMap, &oxmType);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering oxmType Array.")
- for (size_t i = 0; i < doxm->oxmLen ; i++)
- {
- cJSON *jsonOxm = cJSON_GetArrayItem(jsonObj, i);
- VERIFY_NON_NULL(TAG, jsonOxm, ERROR);
- doxm->oxm[i] = (OicSecOxm_t)jsonOxm->valueint;
+ int i = 0;
+ size_t len = 0;
+ while (cbor_value_is_valid(&oxmType))
+ {
+ cborFindResult = cbor_value_dup_text_string(&oxmType, &doxm->oxmType[i++],
+ &len, NULL);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding omxType text string.")
+ cborFindResult = cbor_value_advance(&oxmType);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing oxmType.")
+ }
}
- }
- //OxmSel -- Mandatory
- jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_SEL_NAME);
- if (jsonObj)
- {
- VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
- doxm->oxmSel = (OicSecOxm_t)jsonObj->valueint;
- }
- else // PUT/POST JSON may not have oxmsel so set it to the gDoxm->oxmSel
- {
- VERIFY_NON_NULL(TAG, gDoxm, ERROR);
- doxm->oxmSel = gDoxm->oxmSel;
- }
+ //Oxm -- not Mandatory
+ if (0 == strcmp(OIC_JSON_OXM_NAME, name))
+ {
+ CborValue oxm = { .parser = NULL };
- //sct -- Mandatory
- jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_SUPPORTED_CRED_TYPE_NAME);
- if (jsonObj)
- {
- VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
- doxm->sct = (OicSecCredType_t)jsonObj->valueint;
- }
- else // PUT/POST JSON may not have sct so set it to the gDoxm->sct
- {
- VERIFY_NON_NULL(TAG, gDoxm, ERROR);
- doxm->sct = gDoxm->sct;
- }
+ cborFindResult = cbor_value_get_array_length(&doxmMap, &doxm->oxmLen);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding oxmName array Length.")
+ VERIFY_SUCCESS(TAG, doxm->oxmLen != 0, ERROR);
- //Owned -- Mandatory
- jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OWNED_NAME);
- if (jsonObj)
- {
- VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type), ERROR);
- doxm->owned = jsonObj->valueint;
- }
- else // PUT/POST JSON may not have owned so set it to the gDomx->owned
- {
- VERIFY_NON_NULL(TAG, gDoxm, ERROR);
- doxm->owned = gDoxm->owned;
- }
+ doxm->oxm = (OicSecOxm_t *)OICCalloc(doxm->oxmLen, sizeof(*doxm->oxm));
+ VERIFY_NON_NULL(TAG, doxm->oxmType, ERROR);
- //DeviceId -- Mandatory
- jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVICE_ID_NAME);
- if (jsonObj)
- {
- VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
- if (cJSON_String == jsonObj->type)
- {
- //Check for empty string, in case DeviceId field has not been set yet
- if (jsonObj->valuestring[0])
+ cborFindResult = cbor_value_enter_container(&doxmMap, &oxm);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering oxmName Array.")
+
+ int i = 0;
+ while (cbor_value_is_valid(&oxm))
{
- outLen = 0;
- b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
- sizeof(base64Buff), &outLen);
- VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(doxm->deviceID.id)),
- ERROR);
- memcpy(doxm->deviceID.id, base64Buff, outLen);
+ cborFindResult = cbor_value_get_int(&oxm, (int *) &doxm->oxm[i++]);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding oxmName Value")
+ cborFindResult = cbor_value_advance(&oxm);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing oxmName.")
}
}
- }
- else // PUT/POST JSON will not have deviceID so set it to the gDoxm->deviceID.id
- {
- VERIFY_NON_NULL(TAG, gDoxm, ERROR);
- memcpy((char *)doxm->deviceID.id, (char *)gDoxm->deviceID.id, sizeof(doxm->deviceID.id));
- }
- //DPC -- Mandatory
- jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DPC_NAME);
- if(jsonObj)
- {
- VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type), ERROR);
- doxm->dpc = jsonObj->valueint;
- }
- else // PUT/POST JSON may not have owned so set it to the gDomx->dpc
- {
- if(NULL != gDoxm)
+ if (0 == strcmp(OIC_JSON_OXM_SEL_NAME, name))
{
- doxm->dpc = gDoxm->dpc;
+ cborFindResult = cbor_value_get_int(&doxmMap, (int *) &doxm->oxmSel);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Sel Name Value.")
}
- else
+
+ if (0 == strcmp(OIC_JSON_SUPPORTED_CRED_TYPE_NAME, name))
{
- doxm->dpc = false; // default is false
+ cborFindResult = cbor_value_get_int(&doxmMap, (int *) &doxm->sct);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Sct Name Value.")
}
- }
- //Owner -- will be empty when device status is unowned.
- jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OWNER_NAME);
- if (true == doxm->owned)
- {
- VERIFY_NON_NULL(TAG, jsonObj, ERROR);
- }
- if (jsonObj)
- {
- VERIFY_SUCCESS(TAG, (cJSON_String == jsonObj->type), ERROR);
- outLen = 0;
- b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
- sizeof(base64Buff), &outLen);
- VERIFY_SUCCESS(TAG, ((b64Ret == B64_OK) && (outLen <= sizeof(doxm->owner.id))), ERROR);
- memcpy(doxm->owner.id, base64Buff, outLen);
+ if (0 == strcmp(OIC_JSON_OWNED_NAME, name))
+ {
+ cborFindResult = cbor_value_get_boolean(&doxmMap, &doxm->owned);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Owned Value.")
+ }
+
+ if (0 == strcmp(OIC_JSON_DPC_NAME, name))
+ {
+ cborFindResult = cbor_value_get_boolean(&doxmMap, &doxm->dpc);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding DPC Value.")
+ }
+
+ if (0 == strcmp(OIC_JSON_DEVICE_ID_NAME, name))
+ {
+ uint8_t *id = NULL;
+ cborFindResult = cbor_value_dup_byte_string(&doxmMap, &id, &len, NULL);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding DeviceId Value.")
+ memcpy(doxm->deviceID.id, id, len);
+ OICFree(id);
+ }
+ if (0 == strcmp(OIC_JSON_OWNER_NAME, name))
+ {
+ uint8_t *id = NULL;
+ cborFindResult = cbor_value_dup_byte_string(&doxmMap, &id , &len, NULL);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Owner Name Value.")
+ memcpy(doxm->owner.id, id, len);
+ OICFree(id);
+ }
+ if (CborMapType != type && cbor_value_is_valid(&doxmMap))
+ {
+ cborFindResult = cbor_value_advance(&doxmMap);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing DoxmMap.")
+ }
+ OICFree(name);
}
+ *secDoxm = doxm;
ret = OC_STACK_OK;
exit:
- cJSON_Delete(jsonRoot);
- if (OC_STACK_OK != ret)
+ if (CborNoError != cborFindResult)
{
+ OIC_LOG (ERROR, TAG, "CBORPayloadToDoxm failed!!!");
DeleteDoxmBinData(doxm);
doxm = NULL;
+ ret = OC_STACK_ERROR;
}
-
- return doxm;
+ return ret;
}
/**
if (NULL != doxm)
{
- // Convert Doxm data into JSON for update to persistent storage
- char *jsonStr = BinToDoxmJSON(doxm);
- if (jsonStr)
+ // Convert Doxm data into CBOR for update to persistent storage
+ uint8_t *payload = NULL;
+ size_t size = 0;
+ OCStackResult res = DoxmToCBORPayload(doxm, &payload, &size);
+ if (payload && (OC_STACK_OK == res)
+ && (OC_STACK_OK == UpdateSecureResourceInPS(OIC_JSON_DOXM_NAME, payload, size)))
{
- cJSON *jsonDoxm = cJSON_Parse(jsonStr);
- OICFree(jsonStr);
-
- if (jsonDoxm &&
- (OC_STACK_OK == UpdateSVRDatabase(OIC_JSON_DOXM_NAME, jsonDoxm)))
- {
bRet = true;
- }
- cJSON_Delete(jsonDoxm);
}
+ OICFree(payload);
}
return bRet;
{
bDeviceIDQry = true;
OicUuid_t subject = {.id={0}};
- unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
- uint32_t outLen = 0;
- B64Result b64Ret = B64_OK;
- b64Ret = b64Decode((char *)parseIter.valPos, parseIter.valLen, base64Buff,
- sizeof(base64Buff), &outLen);
-
- VERIFY_SUCCESS(TAG, (B64_OK == b64Ret && outLen <= sizeof(subject.id)), ERROR);
- memcpy(subject.id, base64Buff, outLen);
+ memcpy(subject.id, parseIter.valPos, parseIter.valLen);
if (0 == memcmp(&gDoxm->deviceID.id, &subject.id, sizeof(gDoxm->deviceID.id)))
{
bDeviceIDMatch = true;
}
}
-exit:
return ((bOwnedQry ? bOwnedMatch : true) && (bDeviceIDQry ? bDeviceIDMatch : true));
}
static OCEntityHandlerResult HandleDoxmGetRequest (const OCEntityHandlerRequest * ehRequest)
{
- char* jsonStr = NULL;
OCEntityHandlerResult ehRet = OC_EH_OK;
OIC_LOG(DEBUG, TAG, "Doxm EntityHandle processing GET request");
}
/*
- * For GET or Valid Query request return doxm resource json payload.
+ * For GET or Valid Query request return doxm resource CBOR payload.
* For non-valid query return NULL json payload.
- * A device will 'always' have a default Doxm, so BinToDoxmJSON will
+ * A device will 'always' have a default Doxm, so DoxmToCBORPayload will
* return valid doxm resource json.
*/
+ uint8_t *payload = NULL;
- jsonStr = (ehRet == OC_EH_OK) ? BinToDoxmJSON(gDoxm) : NULL;
+ if (ehRet == OC_EH_OK)
+ {
+ size_t size = 0;
+ if (OC_STACK_OK != DoxmToCBORPayload(gDoxm, &payload, &size))
+ {
+ payload = NULL;
+ }
+ }
// Send response payload to request originator
- if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, jsonStr))
+ if (OC_STACK_OK != SendSRMCBORResponse(ehRequest, ehRet, payload))
{
- OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandleDoxmGetRequest");
+ OIC_LOG(ERROR, TAG, "SendSRMCBORResponse failed in HandleDoxmGetRequest");
}
- OICFree(jsonStr);
+ OICFree(payload);
return ehRet;
}
-static OCEntityHandlerResult HandleDoxmPutRequest (const OCEntityHandlerRequest * ehRequest)
+static OCEntityHandlerResult HandleDoxmPutRequest(const OCEntityHandlerRequest * ehRequest)
{
OIC_LOG (DEBUG, TAG, "Doxm EntityHandle processing PUT request");
OCEntityHandlerResult ehRet = OC_EH_ERROR;
- OicUuid_t emptyOwner = {.id = {0}};
+ OicUuid_t emptyOwner = {.id = {0} };
/*
- * Convert JSON Doxm data into binary. This will also validate
+ * Convert CBOR Doxm data into binary. This will also validate
* the Doxm data received.
*/
- OicSecDoxm_t* newDoxm = JSONToDoxmBin(((OCSecurityPayload*)ehRequest->payload)->securityData);
+ uint8_t *payload = ((OCSecurityPayload *)ehRequest->payload)->securityData1;
+ OicSecDoxm_t *newDoxm = NULL;
- if (newDoxm)
+ if (payload)
{
- // Iotivity SRM ONLY supports OIC_JUST_WORKS now
- if (OIC_JUST_WORKS == newDoxm->oxmSel)
+ OCStackResult res = CBORPayloadToDoxm(payload, CBOR_SIZE, &newDoxm);
+
+ if (newDoxm && OC_STACK_OK == res)
{
- if ((false == gDoxm->owned) && (false == newDoxm->owned))
+ // Iotivity SRM ONLY supports OIC_JUST_WORKS now
+ if (OIC_JUST_WORKS == newDoxm->oxmSel)
{
/*
* If current state of the device is un-owned, enable
* anonymous ECDH cipher in tinyDTLS so that Provisioning
* tool can initiate JUST_WORKS ownership transfer process.
*/
- if(memcmp(&(newDoxm->owner), &emptyOwner, sizeof(OicUuid_t)) == 0)
+ if ((false == gDoxm->owned) && (false == newDoxm->owned))
{
OIC_LOG (INFO, TAG, "Doxm EntityHandle enabling AnonECDHCipherSuite");
#ifdef __WITH_DTLS__
//Save the owner's UUID to derive owner credential
memcpy(&(gDoxm->owner), &(newDoxm->owner), sizeof(OicUuid_t));
-// OCServerRequest *request = (OCServerRequest *)ehRequest->requestHandle;
-// //Generating OwnerPSK
-// OIC_LOG (INFO, TAG, "Doxm EntityHandle generating OwnerPSK");
-// //Generate new credential for provisioning tool
-// ehRet = AddOwnerPSK((CAEndpoint_t *)&request->devAddr, newDoxm,
-// (uint8_t*) OXM_JUST_WORKS, strlen(OXM_JUST_WORKS));
-// VERIFY_SUCCESS(TAG, OC_EH_OK == ehRet, ERROR);
+ // OCServerRequest *request = (OCServerRequest *)ehRequest->requestHandle;
+ // Generating OwnerPSK
+ // OIC_LOG (INFO, TAG, "Doxm EntityHandle generating OwnerPSK");
+ // Generate new credential for provisioning tool
+ // ehRet = AddOwnerPSK((CAEndpoint_t *)&request->devAddr, newDoxm,
+ // (uint8_t*) OXM_JUST_WORKS, strlen(OXM_JUST_WORKS));
+ // VERIFY_SUCCESS(TAG, OC_EH_OK == ehRet, ERROR);
// Update new state in persistent storage
if (true == UpdatePersistentStorage(gDoxm))
ehRet = OC_EH_ERROR;
}
- /*
- * Disable anonymous ECDH cipher in tinyDTLS since device is now
- * in owned state.
- */
+ /*
+ * Disable anonymous ECDH cipher in tinyDTLS since device is now
+ * in owned state.
+ */
CAResult_t caRes = CA_STATUS_OK;
caRes = CAEnableAnonECDHCipherSuite(false);
VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
memcpy(&(gDoxm->owner), &(newDoxm->owner), sizeof(OicUuid_t));
//Update new state in persistent storage
- if((UpdatePersistentStorage(gDoxm) == true))
+ if (UpdatePersistentStorage(gDoxm) == true)
{
ehRet = OC_EH_OK;
}
}
//Send payload to request originator
- if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL))
+ if (OC_STACK_OK != SendSRMCBORResponse(ehRequest, ehRet, NULL))
{
- OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandlePstatPostRequest");
+ OIC_LOG(ERROR, TAG, "SendSRMCBORResponse failed in HandleDoxmPostRequest");
}
DeleteDoxmBinData(newDoxm);
return ehRet;
}
-/**
- * This internal method is the entity handler for DOXM resources.
- */
OCEntityHandlerResult DoxmEntityHandler(OCEntityHandlerFlag flag,
OCEntityHandlerRequest * ehRequest,
void* callbackParam)
if (flag & OC_REQUEST_FLAG)
{
- OIC_LOG (DEBUG, TAG, "Flag includes OC_REQUEST_FLAG");
+ OIC_LOG(DEBUG, TAG, "Flag includes OC_REQUEST_FLAG");
+
switch (ehRequest->method)
{
case OC_REST_GET:
default:
ehRet = OC_EH_ERROR;
- SendSRMResponse(ehRequest, ehRet, NULL);
+ SendSRMCBORResponse(ehRequest, ehRet, NULL);
break;
}
}
return ehRet;
}
-/**
- * This internal method is used to create '/oic/sec/doxm' resource.
- */
OCStackResult CreateDoxmResource()
{
OCStackResult ret = OCCreateResource(&gDoxmHandle,
OCStackResult ret = OC_STACK_ERROR;
//Read DOXM resource from PS
- char* jsonSVRDatabase = GetSVRDatabase();
- if (jsonSVRDatabase)
+ uint8_t *data = NULL;
+ size_t size = 0;
+ ret = GetSecureVirtualDatabaseFromPS(OIC_JSON_DOXM_NAME, &data, &size);
+ // If database read failed
+ if (OC_STACK_OK != ret)
{
- //Convert JSON DOXM into binary format
- gDoxm = JSONToDoxmBin(jsonSVRDatabase);
+ OIC_LOG (DEBUG, TAG, "ReadSVDataFromPS failed");
+ }
+ if (data)
+ {
+ // Read DOXM resource from PS
+ ret = CBORPayloadToDoxm(data, size, &gDoxm);
}
/*
* If SVR database in persistent storage got corrupted or
* is not available for some reason, a default doxm is created
* which allows user to initiate doxm provisioning again.
*/
- if (!jsonSVRDatabase || !gDoxm)
+ if ((OC_STACK_OK != ret) || !data || !gDoxm)
{
gDoxm = GetDoxmDefault();
}
{
OIC_LOG (ERROR, TAG, "CheckDeviceID failed");
}
- OICFree(jsonSVRDatabase);
+ OICFree(data);
return ret;
}
OCStackResult GetDoxmDevOwnerId(OicUuid_t *devOwner)
{
OCStackResult retVal = OC_STACK_ERROR;
- if(gDoxm)
+ if (gDoxm)
{
- if(gDoxm->owned) {
+ if (gDoxm->owned)
+ {
*devOwner = gDoxm->owner; // TODO change to devOwner when available
retVal = OC_STACK_OK;
}
+//******************************************************************
+//
// Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include "gtest/gtest.h"
#include "ocstack.h"
+#include "ocserverrequest.h"
+#include "oic_string.h"
+#include "oic_malloc.h"
#include "resourcemanager.h"
#include "securevirtualresourcetypes.h"
#include "srmresourcestrings.h"
#include "doxmresource.h"
-#include "ocserverrequest.h"
-#include "oic_string.h"
-#include "oic_malloc.h"
-#include "logger.h"
+#include "security_internals.h"
#define TAG "SRM-DOXM"
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-
-//Declare Doxm resource methods for testing
-OCStackResult CreateDoxmResource();
-OCEntityHandlerResult DoxmEntityHandler (OCEntityHandlerFlag flag,
- OCEntityHandlerRequest * ehRequest);
-char * BinToDoxmJSON(const OicSecDoxm_t * doxm);
-OicSecDoxm_t * JSONToDoxmBin(const char * jsonStr);
-void InitSecDoxmInstance(OicSecDoxm_t * doxm);
-OCEntityHandlerResult HandleDoxmPostRequest (const OCEntityHandlerRequest * ehRequest);
-void DeleteDoxmBinData(OicSecDoxm_t* doxm);
-OCEntityHandlerResult HandleDoxmGetRequest (const OCEntityHandlerRequest * ehRequest);
-#ifdef __cplusplus
-}
-#endif
-
-
OicSecDoxm_t * getBinDoxm()
{
- OicSecDoxm_t * doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
- if(!doxm)
+ OicSecDoxm_t *doxm = (OicSecDoxm_t *)OICCalloc(1, sizeof(*doxm));
+ if (!doxm)
{
return NULL;
}
doxm->oxmTypeLen = 1;
- doxm->oxmType = (OicUrn_t *)OICCalloc(doxm->oxmTypeLen, sizeof(char *));
- if(!doxm->oxmType)
+ doxm->oxmType = (OicUrn_t *)OICCalloc(doxm->oxmTypeLen, sizeof(*doxm->oxmType));
+ if (!doxm->oxmType)
{
OICFree(doxm);
return NULL;
}
- doxm->oxmType[0] = (char*)OICMalloc(strlen(OXM_JUST_WORKS) + 1);
- if(!doxm->oxmType[0])
+ doxm->oxmType[0] = (char *) OICMalloc(strlen(OXM_JUST_WORKS) + 1);
+ if (!doxm->oxmType[0])
{
OICFree(doxm->oxmType);
OICFree(doxm);
doxm->owned = true;
//TODO: Need more clarification on deviceIDFormat field type.
//doxm.deviceIDFormat = URN;
- OICStrcpy((char *) doxm->deviceID.id, strlen("deviceId") + 1, "deviceId");
- OICStrcpy((char *) doxm->owner.id, strlen("ownersId") + 1, "ownersId");
+
+ uint8_t deviceId[] = {0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x49, 0x64};
+ memcpy(doxm->deviceID.id, deviceId, sizeof(deviceId));
+ uint8_t ownerId[] = {0x6f, 0x77, 0x6e, 0x65, 0x72, 0x73, 0x49, 0x64};
+ memcpy(doxm->owner.id, ownerId, sizeof(ownerId));
return doxm;
}
//InitDoxmResource Tests
-TEST(InitDoxmResourceTest, InitDoxmResource)
+TEST(DoxmResourceTest, InitDoxmResource)
{
EXPECT_EQ(OC_STACK_INVALID_PARAM, InitDoxmResource());
}
//DeInitDoxmResource Tests
-TEST(DeInitDoxmResourceTest, DeInitDoxmResource)
+TEST(DoxmResourceTest, DeInitDoxmResource)
{
EXPECT_EQ(OC_STACK_ERROR, DeInitDoxmResource());
}
//CreateDoxmResource Tests
-TEST(CreateDoxmResourceTest, CreateDoxmResource)
+TEST(DoxmResourceTest, CreateDoxmResource)
{
EXPECT_EQ(OC_STACK_INVALID_PARAM, CreateDoxmResource());
}
//DoxmEntityHandler Tests
-TEST(DoxmEntityHandlerTest, DoxmEntityHandlerWithDummyRequest)
+TEST(DoxmResourceTest, DoxmEntityHandlerWithDummyRequest)
{
OCEntityHandlerRequest req;
- EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
+ EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req, NULL));
}
-TEST(DoxmEntityHandlerTest, DoxmEntityHandlerWithNULLRequest)
+TEST(DoxmResourceTest, DoxmEntityHandlerWithNULLRequest)
{
- EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, NULL));
+ EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, NULL, NULL));
}
-TEST(DoxmEntityHandlerTest, DoxmEntityHandlerInvalidFlag)
+TEST(DoxmResourceTest, DoxmEntityHandlerInvalidFlag)
{
OCEntityHandlerRequest req;
- EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_OBSERVE_FLAG, &req));
+ EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_OBSERVE_FLAG, &req, NULL));
}
-TEST(DoxmEntityHandlerTest, DoxmEntityHandlerValidRequest)
+TEST(DoxmResourceTest, DoxmEntityHandlerValidRequest)
{
EXPECT_EQ(OC_STACK_INVALID_PARAM, InitDoxmResource());
char query[] = "oxm=0;owned=false;owner=owner1";
OCEntityHandlerRequest req = OCEntityHandlerRequest();
req.method = OC_REST_GET;
req.query = OICStrdup(query);
- EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
+ EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req, NULL));
OICFree(req.query);
}
-TEST(DoxmEntityHandlerTest, DoxmEntityHandlerDeviceIdQuery)
+TEST(DoxmResourceTest, DoxmEntityHandlerDeviceIdQuery)
{
EXPECT_EQ(OC_STACK_INVALID_PARAM, InitDoxmResource());
- char query[] = "deviceid=MjIyMjIyMjIyMjIyMjIyMg==";
+ char query[] = "deviceid=2222222222222222";
OCEntityHandlerRequest req = OCEntityHandlerRequest();
req.method = OC_REST_GET;
req.query = OICStrdup(query);
- EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req));
+ EXPECT_EQ(OC_EH_ERROR, DoxmEntityHandler(OCEntityHandlerFlag::OC_REQUEST_FLAG, &req, NULL));
OICFree(req.query);
}
-//BinToDoxmJSON Tests
-TEST(BinToDoxmJSONTest, BinToDoxmJSONNullDoxm)
+TEST(DoxmResourceTest, DoxmToCBORPayloadNULL)
{
- char* value = BinToDoxmJSON(NULL);
- EXPECT_TRUE(value == NULL);
+ OicSecDoxm_t *doxm = getBinDoxm();
+ size_t size = 10;
+ uint8_t *payload = NULL;
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, DoxmToCBORPayload(NULL, NULL, 0));
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, DoxmToCBORPayload(doxm, NULL, &size));
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, DoxmToCBORPayload(doxm, &payload, 0));
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, DoxmToCBORPayload(NULL, &payload, &size));
+ DeleteDoxmBinData(doxm);
}
-TEST(BinToDoxmJSONTest, BinToDoxmJSONValidDoxm)
+TEST(DoxmResourceTest, DoxmToCBORPayloadVALID)
{
- OicSecDoxm_t * doxm = getBinDoxm();
+ OicSecDoxm_t *doxm = getBinDoxm();
- char * json = BinToDoxmJSON(doxm);
- OIC_LOG_V(INFO, TAG, "BinToDoxmJSON:%s", json);
- EXPECT_TRUE(json != NULL);
+ uint8_t *payload = NULL;
+ size_t size = 0;
+ EXPECT_EQ(OC_STACK_OK, DoxmToCBORPayload(doxm, &payload, &size));
+ EXPECT_TRUE(payload != NULL);
DeleteDoxmBinData(doxm);
- OICFree(json);
+ OICFree(payload);
}
-//JSONToDoxmBin Tests
-TEST(JSONToDoxmBinTest, JSONToDoxmBinValidJSON)
+//CBORPayloadToDoxm Tests
+TEST(DoxmResourceTest, CBORPayloadToDoxmNULL)
{
- OicSecDoxm_t * doxm1 = getBinDoxm();
- char * json = BinToDoxmJSON(doxm1);
- EXPECT_TRUE(json != NULL);
-
- OicSecDoxm_t *doxm2 = JSONToDoxmBin(json);
- EXPECT_TRUE(doxm2 != NULL);
-
- DeleteDoxmBinData(doxm1);
- DeleteDoxmBinData(doxm2);
- OICFree(json);
+ OicSecDoxm_t *doxm = NULL;
+ uint8_t *cborPayload = (uint8_t *)OICCalloc(1, sizeof(10));
+ size_t size = 10;
+ ASSERT_TRUE(NULL != cborPayload);
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CBORPayloadToDoxm(NULL, 0, NULL));
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CBORPayloadToDoxm(NULL, size, &doxm));
+ EXPECT_EQ(OC_STACK_INVALID_PARAM, CBORPayloadToDoxm(cborPayload, size, NULL));
+ OICFree(cborPayload);
}
-TEST(JSONToDoxmBinTest, JSONToDoxmBinNullJSON)
+TEST(DoxmResourceTest, CBORPayloadToDoxmVALID)
{
- OicSecDoxm_t *doxm = JSONToDoxmBin(NULL);
- EXPECT_TRUE(doxm == NULL);
+ OicSecDoxm_t *doxm = getBinDoxm();
+ uint8_t *payload = NULL;
+ size_t size = 0;
+ EXPECT_EQ(OC_STACK_OK, DoxmToCBORPayload(doxm, &payload, &size));
+ EXPECT_TRUE(payload != NULL);
+
+ OicSecDoxm_t *doxmSec = NULL;
+ EXPECT_EQ(OC_STACK_OK, CBORPayloadToDoxm(payload, size, &doxmSec));
+ ASSERT_TRUE(doxmSec != NULL);
+ EXPECT_EQ(doxmSec->oxmTypeLen, doxm->oxmTypeLen);
+ EXPECT_STREQ(doxmSec->oxmType[0], doxm->oxmType[0]);
+ EXPECT_EQ(doxmSec->oxmLen, doxm->oxmLen);
+ EXPECT_EQ(doxmSec->oxm[0], doxm->oxm[0]);
+ EXPECT_EQ(doxmSec->oxmSel, doxm->oxmSel);
+ EXPECT_EQ(doxmSec->sct, doxm->sct);
+ EXPECT_EQ(doxmSec->owned, doxm->owned);
+
+ DeleteDoxmBinData(doxmSec);
+ DeleteDoxmBinData(doxm);
+ OICFree(payload);
}
#if 0
OTMCallbackData_t pinBasedCBData;
pinBasedCBData.loadSecretCB = InputPinCodeCallback;
pinBasedCBData.createSecureSessionCB =
- CreateSecureSessionRandomPinCallbak;
+ CreateSecureSessionRandomPinCallback;
pinBasedCBData.createSelectOxmPayloadCB =
CreatePinBasedSelectOxmPayload;
pinBasedCBData.createOwnerTransferPayloadCB =
{
OTMCallbackData_t pinBasedCBData;
pinBasedCBData.loadSecretCB = InputPinCodeCallback;
- pinBasedCBData.createSecureSessionCB = CreateSecureSessionRandomPinCallbak;
+ pinBasedCBData.createSecureSessionCB = CreateSecureSessionRandomPinCallback;
pinBasedCBData.createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload;
pinBasedCBData.createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload;
OTMSetOwnershipTransferCallbackData(OIC_RANDOM_DEVICE_PIN, &pinBasedCBData);