#include <string.h>
#include "oic_malloc.h"
#include "ocpayload.h"
+#include "ocpayloadcbor.h"
#include "payload_logging.h"
#include "psinterface.h"
#include "resourcemanager.h"
#include "srmutility.h"
#include "amaclresource.h"
-#define TAG "SRM-AMACL"
+#define TAG "OIC_SRM_AMACL"
/** 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. */
// Clean Amss
OICFree(amaclTmp1->amss);
- // Clean Owners
- OICFree(amaclTmp1->owners);
-
// Clean Amacl node itself
OICFree(amaclTmp1);
}
}
}
-static size_t OicSecAmaclCount(const OicSecAmacl_t *secAmacl)
-{
- size_t size = 0;
- for (const OicSecAmacl_t *amacl = secAmacl; amacl; amacl = amacl->next)
- {
- size++;
- }
- return size;
-}
-
OCStackResult AmaclToCBORPayload(const OicSecAmacl_t *amaclS, uint8_t **cborPayload,
size_t *cborSize)
{
*cborSize = 0;
*cborPayload = NULL;
- CborEncoder encoder = { {.ptr = NULL }, .end = 0 };
- CborEncoder amaclMap = { {.ptr = NULL }, .end = 0 };
+ CborEncoder encoder;
+ CborEncoder amaclMap;
int64_t cborEncoderResult = CborNoError;
- CborEncoder rsrcMap = { {.ptr = NULL }, .end = 0 };
- CborEncoder rlistArray = { {.ptr = NULL }, .end = 0 };
- CborEncoder amss = { {.ptr = NULL }, .end = 0 };
+ CborEncoder rsrcMap;
+ CborEncoder rlistArray;
+ CborEncoder amss;
char *stRowner = NULL;
const OicSecAmacl_t *amacl = amaclS;
for (size_t i = 0; i < amacl->resourcesLen; i++)
{
// TODO : Need to create rMap structure based on RAML spec.
- CborEncoder rMap = { {.ptr = NULL }, .end = 0 };
+ CborEncoder rMap;
cborEncoderResult = cbor_encoder_create_map(&rlistArray, &rMap, AMACL_RLIST_MAP_SIZE);
VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RLIST Map.");
VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing AMSS Array.");
// TODO : Need to check owner property in the RAML spec.
- // rowner
+ // rowner -- Mandatory
cborEncoderResult = cbor_encode_text_string(&amaclMap, OIC_JSON_ROWNERID_NAME,
strlen(OIC_JSON_ROWNERID_NAME));
VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding ROwnerID Name Tag.");
- // TODO : Need to modify amacl->owners[0] to amacl->rownerid based on RAML spec.
- ret = ConvertUuidToStr(&amacl->owners[0], &stRowner);
+ ret = ConvertUuidToStr(&amacl->rownerID, &stRowner);
VERIFY_SUCCESS(TAG, ret == OC_STACK_OK, ERROR);
cborEncoderResult = cbor_encode_text_string(&amaclMap, stRowner, strlen(stRowner));
VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding ROwner Value.");
if (CborNoError == cborEncoderResult)
{
*cborPayload = outPayload;
- *cborSize = encoder.ptr - outPayload;
+ *cborSize = cbor_encoder_get_buffer_size(&encoder, outPayload);
ret = OC_STACK_OK;
}
OICFree(outPayload);
outPayload = NULL;
// Since the allocated initial memory failed, double the memory.
- cborLen += encoder.ptr - encoder.end;
+ cborLen += cbor_encoder_get_buffer_size(&encoder, encoder.end);
cborEncoderResult = CborNoError;
ret = AmaclToCBORPayload(amaclS, cborPayload, &cborLen);
if (OC_STACK_OK == ret)
OCStackResult CBORPayloadToAmacl(const uint8_t *cborPayload, size_t size,
OicSecAmacl_t **secAmacl)
{
- if (NULL == cborPayload || NULL == secAmacl || NULL != *secAmacl)
+ if (NULL == cborPayload || NULL == secAmacl || NULL != *secAmacl || 0 == size)
{
return OC_STACK_INVALID_PARAM;
}
CborValue amaclCbor = { .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, &amaclCbor);
+ cbor_parser_init(cborPayload, size, 0, &parser, &amaclCbor);
OicSecAmacl_t *headAmacl = (OicSecAmacl_t *)OICCalloc(1, sizeof(OicSecAmacl_t));
CborValue amaclMap = { .parser = NULL };
cborFindResult = cbor_value_enter_container(&amaclCbor, &amaclMap);
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering Amacl Map.");
- while(cbor_value_is_valid(&amaclMap))
+ while(cbor_value_is_valid(&amaclMap) && cbor_value_is_text_string(&amaclMap))
{
char *name = NULL;
size_t len = 0;
cborFindResult = cbor_value_enter_container(&amaclMap, &rsrcMap);
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering Resource Map");
- while(cbor_value_is_valid(&rsrcMap))
+ while(cbor_value_is_valid(&rsrcMap) && cbor_value_is_text_string(&rsrcMap))
{
// resource name
char *rsrcName = NULL;
cborFindResult = cbor_value_enter_container(&rsrcArray, &rMap);
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering Rlist Map");
- while(cbor_value_is_valid(&rMap))
+ while(cbor_value_is_valid(&rMap) && cbor_value_is_text_string(&rMap))
{
char *rMapName = NULL;
size_t rMapNameLen = 0;
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering AMS Array Container.");
headAmacl->amss = (OicUuid_t *)OICCalloc(headAmacl->amssLen, sizeof(*headAmacl->amss));
VERIFY_NON_NULL(TAG, headAmacl->amss, ERROR);
- while (cbor_value_is_valid(&amsArray))
+ while (cbor_value_is_valid(&amsArray) && cbor_value_is_text_string(&amsArray))
{
char *amssId = NULL;
cborFindResult = cbor_value_dup_text_string(&amsArray, &amssId, &len, NULL);
}
}
- // TODO : Need to modify headAmacl->owners[0].id to headAmacl->rowner based on RAML spec.
// Rowner -- Mandatory
- if (0 == strcmp(OIC_JSON_ROWNERID_NAME, name))
+ if (0 == strcmp(OIC_JSON_ROWNERID_NAME, name) && cbor_value_is_text_string(&amaclMap))
{
char *stRowner = NULL;
cborFindResult = cbor_value_dup_text_string(&amaclMap, &stRowner, &len, NULL);
VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ROwner Value.");
- headAmacl->ownersLen = 1;
- headAmacl->owners = (OicUuid_t *)OICCalloc(headAmacl->ownersLen, sizeof(*headAmacl->owners));
- VERIFY_NON_NULL(TAG, headAmacl->owners, ERROR);
- ret = ConvertStrToUuid(stRowner, &headAmacl->owners[0]);
+
+ ret = ConvertStrToUuid(stRowner, &headAmacl->rownerID);
VERIFY_SUCCESS(TAG, ret == OC_STACK_OK, ERROR);
OICFree(stRowner);
}
OCEntityHandlerResult ehRet = (res == OC_STACK_OK) ? OC_EH_OK : OC_EH_ERROR;
// Send response payload to request originator
- SendSRMCBORResponse(ehRequest, ehRet, cborPayload, size);
-
+ if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, cborPayload, size))
+ {
+ ehRet = OC_EH_ERROR;
+ OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandleAmaclGetRequest");
+ }
OICFree(cborPayload);
OIC_LOG_V (DEBUG, TAG, "%s RetVal %d", __func__ , ehRet);
OCEntityHandlerResult ehRet = OC_EH_ERROR;
// Convert CBOR Amacl data into binary. This will also validate the Amacl data received.
- uint8_t *payload = ((OCSecurityPayload *) ehRequest->payload)->securityData1;
+ uint8_t *payload = ((OCSecurityPayload *) ehRequest->payload)->securityData;
size_t size = ((OCSecurityPayload *) ehRequest->payload)->payloadSize;
if (payload)
{
}
// Send payload to request originator
- SendSRMCBORResponse(ehRequest, ehRet, NULL, 0);
+ if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL, 0))
+ {
+ ehRet = OC_EH_ERROR;
+ OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandleAmaclPostRequest");
+ }
OIC_LOG_V(DEBUG, TAG, "%s RetVal %d", __func__ , ehRet);
return ehRet;
default:
ehRet = OC_EH_ERROR;
- SendSRMCBORResponse(ehRequest, ehRet, NULL, 0);
+ SendSRMResponse(ehRequest, ehRet, NULL, 0);
}
}
{
OCStackResult ret = OCCreateResource(&gAmaclHandle,
OIC_RSRC_TYPE_SEC_AMACL,
- OIC_MI_DEF,
+ OC_RSRVD_INTERFACE_DEFAULT,
OIC_RSRC_AMACL_URI,
AmaclEntityHandler,
NULL,
exit:
return OC_STACK_ERROR;
}
+
+OCStackResult SetAmaclRownerId(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 == gAmacl)
+ {
+ ret = OC_STACK_NO_RESOURCE;
+ }
+
+ if(newROwner && gAmacl)
+ {
+ memcpy(prevId.id, gAmacl->rownerID.id, sizeof(prevId.id));
+ memcpy(gAmacl->rownerID.id, newROwner->id, sizeof(newROwner->id));
+
+ ret = AmaclToCBORPayload(gAmacl, &cborPayload, &size);
+ VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
+
+ ret = UpdateSecureResourceInPS(OIC_JSON_AMACL_NAME, cborPayload, size);
+ VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
+
+ OICFree(cborPayload);
+ }
+
+ return ret;
+
+exit:
+ OICFree(cborPayload);
+ memcpy(gAmacl->rownerID.id, prevId.id, sizeof(prevId.id));
+ return ret;
+}
+
+OCStackResult GetAmaclRownerId(OicUuid_t *rowneruuid)
+{
+ OCStackResult retVal = OC_STACK_ERROR;
+ if (gAmacl)
+ {
+ *rowneruuid = gAmacl->rownerID;
+ retVal = OC_STACK_OK;
+ }
+ return retVal;
+}