+static char* EncodingValueToString(OicEncodingType_t encoding)
+{
+ char* str = NULL;
+ switch (encoding)
+ {
+ case OIC_ENCODING_RAW:
+ str = (char*)OIC_SEC_ENCODING_RAW;
+ break;
+ case OIC_ENCODING_BASE64:
+ str = (char*)OIC_SEC_ENCODING_BASE64;
+ break;
+ case OIC_ENCODING_DER:
+ str = (char*)OIC_SEC_ENCODING_DER;
+ break;
+ case OIC_ENCODING_PEM:
+ str = (char*)OIC_SEC_ENCODING_PEM;
+ break;
+ default:
+ break;
+ }
+ return str;
+}
+
+static CborError SerializeEncodingToCborInternal(CborEncoder *map, const OicSecKey_t *value)
+{
+ CborError cborEncoderResult = CborNoError;
+ char *encoding = EncodingValueToString(value->encoding);
+ if (encoding)
+ {
+ cborEncoderResult = cbor_encode_text_string(map, OIC_JSON_ENCODING_NAME,
+ strlen(OIC_JSON_ENCODING_NAME));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Encoding Tag.");
+ cborEncoderResult = cbor_encode_text_string(map, encoding,
+ strlen(encoding));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Encoding Value.");
+
+ cborEncoderResult = cbor_encode_text_string(map, OIC_JSON_DATA_NAME,
+ strlen(OIC_JSON_DATA_NAME));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Name Tag.");
+ if (OIC_ENCODING_DER == value->encoding ||
+ OIC_ENCODING_RAW == value->encoding)
+ {
+ cborEncoderResult = cbor_encode_byte_string(map,
+ value->data, value->len);
+ }
+ else
+ {
+ cborEncoderResult = cbor_encode_text_string(map,
+ (char*)value->data, value->len);
+ }
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Name Value.");
+ }
+ else
+ {
+ OIC_LOG(ERROR, TAG, "Unknown encoding type.");
+ VERIFY_CBOR_SUCCESS(TAG, CborErrorUnknownType, "Failed Adding Encoding Value.");
+ }
+ exit:
+ return cborEncoderResult;
+}
+
+static CborError SerializeEncodingToCbor(CborEncoder *rootMap, const char* tag, const OicSecKey_t *value)
+{
+ CborError cborEncoderResult = CborNoError;
+ CborEncoder map;
+ const size_t mapSize = 2;
+
+ cborEncoderResult = cbor_encode_text_string(rootMap, tag, strlen(tag));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PrivateData Tag.");
+
+ cborEncoderResult = cbor_encoder_create_map(rootMap, &map, mapSize);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Map");
+
+ VERIFY_CBOR_SUCCESS(TAG, SerializeEncodingToCborInternal(&map, value),
+ "Failed adding OicSecKey_t structure");
+
+ cborEncoderResult = cbor_encoder_close_container(rootMap, &map);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Map.");
+
+ exit:
+ return cborEncoderResult;
+}
+
+static CborError SerializeSecOptToCbor(CborEncoder *rootMap, const char* tag, const OicSecOpt_t *value)
+{
+ CborError cborEncoderResult = CborNoError;
+ CborEncoder map;
+ const size_t mapSize = 3;
+
+ cborEncoderResult = cbor_encode_text_string(rootMap, tag, strlen(tag));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PrivateData Tag.");
+
+ cborEncoderResult = cbor_encoder_create_map(rootMap, &map, mapSize);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Map");
+
+ OicSecKey_t in;
+ in.data = value->data;
+ in.encoding = value->encoding;
+ in.len = value->len;
+
+ VERIFY_CBOR_SUCCESS(TAG, SerializeEncodingToCborInternal(&map, &in),
+ "Failed adding OicSecKey_t structure");
+
+ cborEncoderResult = cbor_encode_text_string(&map, OIC_JSON_REVOCATION_STATUS_NAME,
+ strlen(OIC_JSON_REVOCATION_STATUS_NAME));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding optional revstat Tag.");
+ cborEncoderResult = cbor_encode_boolean(&map, value->revstat);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding optional revstat Value.");
+
+ cborEncoderResult = cbor_encoder_close_container(rootMap, &map);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Map.");
+
+ exit:
+ return cborEncoderResult;
+}
+
+static CborError DeserializeEncodingFromCborInternal(CborValue *map, char *name, OicSecKey_t *value)
+{
+ size_t len = 0;
+ CborError cborFindResult = CborNoError;
+
+ // data -- Mandatory
+ if (strcmp(name, OIC_JSON_DATA_NAME) == 0)
+ {
+ if(cbor_value_is_byte_string(map))
+ {
+ cborFindResult = cbor_value_dup_byte_string(map, &value->data,
+ &value->len, NULL);
+ }
+ else if(cbor_value_is_text_string(map))
+ {
+ cborFindResult = cbor_value_dup_text_string(map, (char**)(&value->data),
+ &value->len, NULL);
+ }
+ else
+ {
+ cborFindResult = CborErrorUnknownType;
+ OIC_LOG(ERROR, TAG, "Unknown type for private data.");
+ }
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PrivateData.");
+ }
+
+ // encoding -- Mandatory
+ if (strcmp(name, OIC_JSON_ENCODING_NAME) == 0)
+ {
+ char* strEncoding = NULL;
+ cborFindResult = cbor_value_dup_text_string(map, &strEncoding, &len, NULL);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding EncodingType");
+
+ if(strcmp(strEncoding, OIC_SEC_ENCODING_RAW) == 0)
+ {
+ value->encoding = OIC_ENCODING_RAW;
+ }
+ else if(strcmp(strEncoding, OIC_SEC_ENCODING_BASE64) == 0)
+ {
+ value->encoding = OIC_ENCODING_BASE64;
+ }
+ else if(strcmp(strEncoding, OIC_SEC_ENCODING_DER) == 0)
+ {
+ value->encoding = OIC_ENCODING_DER;
+ }
+ else if(strcmp(strEncoding, OIC_SEC_ENCODING_PEM) == 0)
+ {
+ value->encoding = OIC_ENCODING_PEM;
+ }
+ else
+ {
+ //For unit test
+ value->encoding = OIC_ENCODING_RAW;
+ OIC_LOG(WARNING, TAG, "Unknown encoding type detected.");
+ }
+ OICFree(strEncoding);
+ }
+ exit:
+ return cborFindResult;
+}
+
+static CborError DeserializeEncodingFromCbor(CborValue *rootMap, OicSecKey_t *value)
+{
+ CborValue map = { .parser = NULL };
+ CborError cborFindResult = cbor_value_enter_container(rootMap, &map);
+ size_t len = 0;
+
+ while (cbor_value_is_valid(&map))
+ {
+ char* name = NULL;
+ CborType type = cbor_value_get_type(&map);
+ if (type == CborTextStringType && cbor_value_is_text_string(&map))
+ {
+ cborFindResult = cbor_value_dup_text_string(&map, &name, &len, NULL);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get text");
+ cborFindResult = cbor_value_advance(&map);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance value");
+ }
+ if (name)
+ {
+ VERIFY_CBOR_SUCCESS(TAG, DeserializeEncodingFromCborInternal(&map, name, value),
+ "Failed to read OicSecKey_t value");
+ }
+ if (cbor_value_is_valid(&map))
+ {
+ cborFindResult = cbor_value_advance(&map);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing Map.");
+ }
+ OICFree(name);
+ }
+ exit:
+ return cborFindResult;
+}
+
+static CborError DeserializeSecOptFromCbor(CborValue *rootMap, OicSecOpt_t *value)
+{
+ CborValue map = { .parser = NULL };
+ CborError cborFindResult = cbor_value_enter_container(rootMap, &map);
+ size_t len = 0;
+ value->revstat = false;
+
+ while (cbor_value_is_valid(&map))
+ {
+ char* name = NULL;
+ CborType type = cbor_value_get_type(&map);
+ if (type == CborTextStringType && cbor_value_is_text_string(&map))
+ {
+ cborFindResult = cbor_value_dup_text_string(&map, &name, &len, NULL);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get text");
+ cborFindResult = cbor_value_advance(&map);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to advance value");
+ }
+ if (name)
+ {
+ // OptionalData::revstat -- Mandatory
+ if (strcmp(name, OIC_JSON_REVOCATION_STATUS_NAME) == 0)
+ {
+ cborFindResult = cbor_value_get_boolean(&map, &value->revstat);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding revstat Value.")
+ }
+ OicSecKey_t out;
+ VERIFY_CBOR_SUCCESS(TAG, DeserializeEncodingFromCborInternal(&map, name, &out),
+ "Failed to read OicSecKey_t value");
+
+ value->data = out.data;
+ value->encoding = out.encoding;
+ value->len = out.len;
+ }
+ if (cbor_value_is_valid(&map))
+ {
+ cborFindResult = cbor_value_advance(&map);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing Map.");
+ }
+ OICFree(name);
+ }
+ exit:
+ return cborFindResult;
+}
+