/** Default cbor payload size. This value is increased in case of CborErrorOutOfMemory.
* The value of payload size is increased until reaching below max cbor size. */
-static const uint8_t CBOR_SIZE = 255;
+static const uint16_t CBOR_SIZE = 512;
// Max cbor size payload.
static const uint16_t CBOR_MAX_SIZE = 4400;
static OicSecPstat_t gDefaultPstat =
{
false, // bool isOwned
- (OicSecDpm_t)(TAKE_OWNER | BOOTSTRAP_SERVICE | SECURITY_MANAGEMENT_SERVICES |
+ (OicSecDpm_t)(BOOTSTRAP_SERVICE | SECURITY_MANAGEMENT_SERVICES |
PROVISION_CREDENTIALS | PROVISION_ACLS), // OicSecDpm_t cm
(OicSecDpm_t)(TAKE_OWNER | BOOTSTRAP_SERVICE | SECURITY_MANAGEMENT_SERVICES |
PROVISION_CREDENTIALS | PROVISION_ACLS), // OicSecDpm_t tm
1, // the number of elts in Sms
&gSm, // OicSecDpom_t *sm
0, // uint16_t commitHash
+ {.id = {0}}, // OicUuid_t rownerID
};
static OicSecPstat_t *gPstat = NULL;
CborEncoder encoder = { {.ptr = NULL }, .end = 0 };
CborEncoder pstatMap = { {.ptr = NULL }, .end = 0 };
+ char* strUuid = NULL;
- CborError cborEncoderResult = CborNoError;
+ int64_t cborEncoderResult = CborNoError;
uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborLen);
VERIFY_NON_NULL(TAG, outPayload, ERROR);
cborEncoderResult = cbor_encode_text_string(&pstatMap, 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(&pstatMap, (uint8_t *)pstat->deviceID.id,
- sizeof(pstat->deviceID.id));
+ ret = ConvertUuidToStr(&pstat->deviceID, &strUuid);
+ VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
+ cborEncoderResult = cbor_encode_text_string(&pstatMap, strUuid, strlen(strUuid));
VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Id Value.");
-
- cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_COMMIT_HASH_NAME,
- strlen(OIC_JSON_COMMIT_HASH_NAME));
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Commit Hash Tag.");
- cborEncoderResult = cbor_encode_int(&pstatMap, pstat->commitHash);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Commit Hash Value.");
+ OICFree(strUuid);
+ strUuid = NULL;
cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_CM_NAME,
strlen(OIC_JSON_CM_NAME));
cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_SM_NAME,
strlen(OIC_JSON_SM_NAME));
VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SM Name Tag.");
- {
- CborEncoder sm = { {.ptr = NULL }, .end = 0 };
- cborEncoderResult = cbor_encoder_create_array(&pstatMap, &sm, pstat->smLen);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SM Array.");
+ cborEncoderResult = cbor_encode_int(&pstatMap, pstat->sm[0]);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SM Name Value.");
+
+ cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_ROWNERID_NAME,
+ strlen(OIC_JSON_ROWNERID_NAME));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ROwner Id Tag.");
+ ret = ConvertUuidToStr(&pstat->rownerID, &strUuid);
+ VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
+ cborEncoderResult = cbor_encode_text_string(&pstatMap, strUuid, strlen(strUuid));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ROwner Id Value.");
+ OICFree(strUuid);
+ strUuid = NULL;
- for (size_t i = 0; i < pstat->smLen; i++)
- {
- cborEncoderResult = cbor_encode_int(&sm, pstat->sm[i]);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SM Value in Array.");
- }
- cborEncoderResult = cbor_encoder_close_container(&pstatMap, &sm);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing SM Array.");
- }
cborEncoderResult = cbor_encoder_close_container(&encoder, &pstatMap);
VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Closing PSTAT Map.");
- *size = encoder.ptr - outPayload;
- *payload = outPayload;
- ret = OC_STACK_OK;
-
+ if (CborNoError == cborEncoderResult)
+ {
+ *size = encoder.ptr - outPayload;
+ *payload = outPayload;
+ ret = OC_STACK_OK;
+ }
exit:
if ((CborErrorOutOfMemory == cborEncoderResult) && (cborLen < CBOR_MAX_SIZE))
{
cborLen += encoder.ptr - encoder.end;
cborEncoderResult = CborNoError;
ret = PstatToCBORPayload(pstat, payload, &cborLen);
+ if (OC_STACK_OK == ret)
+ {
+ *size = cborLen;
+ }
}
if ((CborNoError != cborEncoderResult) || (OC_STACK_OK != ret))
OCStackResult ret = OC_STACK_ERROR;
*secPstat = NULL;
- CborValue pstatCbor = { .parser = NULL };
- CborParser parser = { .end = NULL };
+ CborValue pstatCbor;
+ CborParser parser;
CborError cborFindResult = CborNoError;
+ char *strUuid = NULL;
int cborLen = size;
+ size_t len = 0;
if (0 == size)
{
cborLen = CBOR_SIZE;
}
cbor_parser_init(cborPayload, cborLen, 0, &parser, &pstatCbor);
- CborValue pstatMap = { .parser = NULL } ;
+ CborValue pstatMap = { .parser = NULL };
+
OicSecPstat_t *pstat = NULL;
- char *name = NULL;
cborFindResult = cbor_value_enter_container(&pstatCbor, &pstatMap);
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT Map.");
pstat = (OicSecPstat_t *)OICCalloc(1, sizeof(OicSecPstat_t));
VERIFY_NON_NULL(TAG, pstat, ERROR);
- while (cbor_value_is_valid(&pstatMap))
+ cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_ISOP_NAME, &pstatMap);
+ if (CborNoError == cborFindResult && cbor_value_is_boolean(&pstatMap))
{
- size_t len = 0;
- cborFindResult = cbor_value_dup_text_string(&pstatMap, &name, &len, NULL);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT Name Value.");
- cborFindResult = cbor_value_advance(&pstatMap);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing PSTAT MAP.");
-
- CborType type = cbor_value_get_type(&pstatMap);
-
- if (0 == strcmp(OIC_JSON_ISOP_NAME, name))
- {
- cborFindResult = cbor_value_get_boolean(&pstatMap, &pstat->isOp);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ISOP Name.");
- }
-
- if (0 == strcmp(OIC_JSON_DEVICE_ID_NAME, name))
- {
- uint8_t *subjectId = NULL;
- cborFindResult = cbor_value_dup_byte_string(&pstatMap, &subjectId, &len, NULL);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SubjectId.");
- memcpy(pstat->deviceID.id, subjectId, len);
- OICFree(subjectId);
- }
-
- if (0 == strcmp(OIC_JSON_COMMIT_HASH_NAME, name))
- {
- cborFindResult = cbor_value_get_int(&pstatMap, (int *) &pstat->commitHash);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding CommitHash.");
- }
+ cborFindResult = cbor_value_get_boolean(&pstatMap, &pstat->isOp);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding isOp Value.");
+ }
- if (0 == strcmp(OIC_JSON_CM_NAME, name))
- {
- cborFindResult = cbor_value_get_int(&pstatMap, (int *) &pstat->cm);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding CM Name.");
- }
+ cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_DEVICE_ID_NAME, &pstatMap);
+ if (CborNoError == cborFindResult && cbor_value_is_text_string(&pstatMap))
+ {
+ cborFindResult = cbor_value_dup_text_string(&pstatMap, &strUuid , &len, NULL);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Device Id Value.");
+ ret = ConvertStrToUuid(strUuid , &pstat->deviceID);
+ VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
+ OICFree(strUuid );
+ strUuid = NULL;
+ }
- if (0 == strcmp(OIC_JSON_OM_NAME, name))
- {
- cborFindResult = cbor_value_get_int(&pstatMap, (int *) &pstat->om);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding OM Name.");
- }
+ cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_CM_NAME, &pstatMap);
+ if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
+ {
+ cborFindResult = cbor_value_get_int(&pstatMap, (int *) &pstat->cm);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding CM.");
+ }
- if (0 == strcmp(OIC_JSON_SM_NAME, name))
- {
- CborValue sm = { .parser = NULL };
+ cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_TM_NAME, &pstatMap);
+ if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
+ {
+ cborFindResult = cbor_value_get_int(&pstatMap, (int *) &pstat->tm);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding TM.");
+ }
- cborFindResult = cbor_value_get_array_length(&pstatMap, &pstat->smLen);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SM Len Array.");
- VERIFY_SUCCESS(TAG, pstat->smLen != 0, ERROR);
+ cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_OM_NAME, &pstatMap);
+ if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
+ {
+ cborFindResult = cbor_value_get_int(&pstatMap, (int *) &pstat->om);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding OM.");
+ }
- pstat->sm = (OicSecDpom_t *)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
- VERIFY_NON_NULL(TAG, pstat->sm, ERROR);
+ cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_SM_NAME, &pstatMap);
+ if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
+ {
+ pstat->smLen = 1;
+ pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
+ cborFindResult = cbor_value_get_int(&pstatMap, (int *) &pstat->sm[0]);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SM.");
- cborFindResult = cbor_value_enter_container(&pstatMap, &sm);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SM Container.");
+ }
- int i = 0;
- while (cbor_value_is_valid(&sm))
- {
- cborFindResult = cbor_value_get_int(&sm, (int *)&pstat->sm[i++]);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SM Value.");
- cborFindResult = cbor_value_advance(&sm);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SM Array.");
- }
- }
- if (CborMapType != type && cbor_value_is_valid(&pstatMap))
- {
- cborFindResult = cbor_value_advance(&pstatMap);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT MAP.");
- }
- OICFree(name);
- name = NULL;
+ cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_ROWNERID_NAME, &pstatMap);
+ if (CborNoError == cborFindResult && cbor_value_is_text_string(&pstatMap))
+ {
+ cborFindResult = cbor_value_dup_text_string(&pstatMap, &strUuid , &len, NULL);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ROwner Id Value.");
+ ret = ConvertStrToUuid(strUuid , &pstat->rownerID);
+ VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
+ OICFree(strUuid );
+ strUuid = NULL;
}
*secPstat = pstat;
OIC_LOG(ERROR, TAG, "CBORPayloadToPstat failed");
DeletePstatBinData(pstat);
pstat = NULL;
+ *secPstat = NULL;
ret = OC_STACK_ERROR;
}
- if (name)
- {
- OICFree(name);
- }
+
return ret;
}
OCEntityHandlerResult ehRet = (res == OC_STACK_OK) ? OC_EH_OK : OC_EH_ERROR;
// Send response payload to request originator
- SendSRMCBORResponse(ehRequest, ehRet, payload);
+ SendSRMCBORResponse(ehRequest, ehRet, payload, size);
OICFree(payload);
return ehRet;
}
if (ehRequest->resource)
{
uint8_t *payload = ((OCSecurityPayload *) ehRequest->payload)->securityData1;
+ size_t size = ((OCSecurityPayload *) ehRequest->payload)->payloadSize;
VERIFY_NON_NULL(TAG, payload, ERROR);
- OCStackResult ret = CBORPayloadToPstat(payload, CBOR_SIZE, &pstat);
+ OCStackResult ret = CBORPayloadToPstat(payload, size, &pstat);
OICFree(payload);
VERIFY_NON_NULL(TAG, pstat, ERROR);
if (OC_STACK_OK == ret)
{
- if (pstat->tm)
+ if (false == (pstat->cm & TAKE_OWNER) && false == pstat->isOp)
{
- gPstat->tm = pstat->tm;
- if(0 == pstat->tm && gPstat->commitHash == pstat->commitHash)
- {
- gPstat->isOp = true;
- gPstat->cm = NORMAL;
- OIC_LOG (INFO, TAG, "CommitHash is valid and isOp is TRUE");
- }
- else
- {
- OIC_LOG(DEBUG, TAG, "CommitHash is not valid");
- }
+ gPstat->cm = pstat->cm;
+ OIC_LOG (INFO, TAG, "State changed to Ready for Provisioning");
+ }
+ else if (false == (pstat->cm & TAKE_OWNER) && true == pstat->isOp)
+ {
+ gPstat->isOp =pstat->isOp;
+ OIC_LOG (INFO, TAG, "State changed to Ready for Normal Operation");
+ }
+ else
+ {
+ OIC_LOG(DEBUG, TAG, "Invalid Device provisionig state");
}
- if (pstat->om && gPstat)
+ if (pstat->om != MULTIPLE_SERVICE_SERVER_DRIVEN && gPstat)
{
/*
* Check if the operation mode is in the supported provisioning services
* operation mode list.
*/
- for(size_t i=0; i< gPstat->smLen; i++)
+ for (size_t i=0; i< gPstat->smLen; i++)
{
if(gPstat->sm[i] == pstat->om)
{
}
}
// Convert pstat data into CBOR for update to persistent storage
- if(UpdatePersistentStorage(gPstat))
+ if (UpdatePersistentStorage(gPstat))
{
ehRet = OC_EH_OK;
}
}
-
- OICFree(payload);
}
exit:
if(OC_EH_OK != ehRet)
}
//Send payload to request originator
- if(OC_STACK_OK != SendSRMCBORResponse(ehRequest, ehRet, NULL))
+ if(OC_STACK_OK != SendSRMCBORResponse(ehRequest, ehRet, NULL, 0))
{
OIC_LOG (ERROR, TAG, "SendSRMResponse failed in HandlePstatPostRequest");
}
/**
* This internal method is the entity handler for pstat resources.
*/
-OCEntityHandlerResult PstatEntityHandler(OCEntityHandlerFlag flag,
- OCEntityHandlerRequest * ehRequest,
- void *callbackParam)
+ OCEntityHandlerResult PstatEntityHandler(OCEntityHandlerFlag flag,
+ OCEntityHandlerRequest * ehRequest,
+ void *callbackParam)
{
(void)callbackParam;
OCEntityHandlerResult ehRet = OC_EH_ERROR;
break;
default:
ehRet = OC_EH_ERROR;
- SendSRMCBORResponse(ehRequest, ehRet, NULL);
+ SendSRMCBORResponse(ehRequest, ehRet, NULL, 0);
break;
}
}
/**
* This internal method is used to create '/oic/sec/pstat' resource.
*/
-OCStackResult CreatePstatResource()
+ OCStackResult CreatePstatResource()
{
OCStackResult ret = OCCreateResource(&gPstatHandle,
OIC_RSRC_TYPE_SEC_PSTAT,
{
// Read ACL resource from PS
ret = CBORPayloadToPstat(data, size, &gPstat);
+ OICFree(data);
}
/*
* If SVR database in persistent storage got corrupted or
* is not available for some reason, a default pstat is created
* which allows user to initiate pstat provisioning again.
*/
- if ((OC_STACK_OK != ret) || !data || !gPstat)
+ if ((OC_STACK_OK != ret) || !gPstat)
{
gPstat = GetPstatDefault();
}
ret = CreatePstatResource();
exit:
- OICFree(data);
if (OC_STACK_OK != ret)
{
DeInitPstatResource();
return ret;
}
-/**
- * Perform cleanup for pstat resources.
- *
- * @retval OC_STACK_OK for Success, otherwise some error value
- */
OCStackResult DeInitPstatResource()
{
- if(gPstat != &gDefaultPstat)
+ if (gPstat != &gDefaultPstat)
{
DeletePstatBinData(gPstat);
gPstat = NULL;
{
OIC_LOG(INFO, TAG, "PSTAT resource will revert back to initial status.");
- gPstat->cm = NORMAL;
- gPstat->tm = NORMAL;
+ gPstat->cm = (OicSecDpm_t)(gPstat->cm | TAKE_OWNER);
+ gPstat->tm = (OicSecDpm_t)(gPstat->tm & (~TAKE_OWNER));
gPstat->om = SINGLE_SERVICE_CLIENT_DRIVEN;
if(gPstat->sm && 0 < gPstat->smLen)
{
if (!UpdatePersistentStorage(gPstat))
{
- OIC_LOG(ERROR, TAG, "Failed to revert DOXM in persistent storage");
+ OIC_LOG(ERROR, TAG, "Failed to revert PSTAT in persistent storage");
}
}
}
+
+OCStackResult SetPstatRownerId(const OicUuid_t* newROwner)
+{
+ OCStackResult ret = OC_STACK_ERROR;
+ uint8_t *cborPayload = NULL;
+ size_t size = 0;
+ OicUuid_t prevId = {.id={0}};
+
+ if(NULL == newROwner)
+ {
+ ret = OC_STACK_INVALID_PARAM;
+ }
+ if(NULL == gPstat)
+ {
+ ret = OC_STACK_NO_RESOURCE;
+ }
+
+ if(newROwner && gPstat)
+ {
+ memcpy(prevId.id, gPstat->rownerID.id, sizeof(prevId.id));
+ memcpy(gPstat->rownerID.id, newROwner->id, sizeof(newROwner->id));
+
+ ret = PstatToCBORPayload(gPstat, &cborPayload, &size);
+ VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
+
+ ret = UpdateSecureResourceInPS(OIC_JSON_PSTAT_NAME, cborPayload, size);
+ VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
+
+ OICFree(cborPayload);
+ }
+
+ return ret;
+
+exit:
+ OICFree(cborPayload);
+ memcpy(gPstat->rownerID.id, prevId.id, sizeof(prevId.id));
+ return ret;
+}
+