28#define DER_LONG_FORM_FLAG 0x80
29#define DER_FIRST_BYTE_VALUE_MASK 0x7f
42 PRINTF(
"Failed to extract uint8 value from TLV data (tag 0x%x)\n", data->
tag);
45 if (value != expected_value) {
47 "Value mismatch for tag 0x%x: expected %u, got %u\n", data->
tag, expected_value, value);
66 if (data == NULL || value == NULL) {
67 PRINTF(
"Received NULL parameter\n");
71 PRINTF(
"Invalid length (%u bytes) for tag 0x%x (expected 1-%zu bytes)\n",
79 uint8_t buffer[
sizeof(uint64_t)] = {0};
82 *value = U8BE(buffer, 0);
98 if (data == NULL || value == NULL) {
99 PRINTF(
"Received NULL parameter\n");
106 *value = (uint32_t) tmp_value;
122 if (data == NULL || value == NULL) {
123 PRINTF(
"Received NULL parameter\n");
130 *value = (uint16_t) tmp_value;
146 if (data == NULL || value == NULL) {
147 PRINTF(
"Received NULL parameter\n");
154 *value = (uint8_t) tmp_value;
170 if (data == NULL || value == NULL) {
171 PRINTF(
"Received NULL parameter\n");
175 PRINTF(
"Invalid length (%u bytes) for tag 0x%x (expected 1-%zu bytes)\n",
183 uint8_t buffer[
sizeof(int64_t)];
184 memset(buffer, (data->
value.
ptr[0] & 0x80) ? 0xFF : 0x00,
sizeof(buffer));
187 *value = (int64_t) U8BE(buffer, 0);
204 if (data == NULL || value == NULL) {
205 PRINTF(
"Received NULL parameter\n");
210 || (tmp_value > INT32_MAX)) {
213 *value = (int32_t) tmp_value;
230 if (data == NULL || value == NULL) {
231 PRINTF(
"Received NULL parameter\n");
236 || (tmp_value > INT16_MAX)) {
239 *value = (int16_t) tmp_value;
256 if (data == NULL || value == NULL) {
257 PRINTF(
"Received NULL parameter\n");
262 || (tmp_value > INT8_MAX)) {
265 *value = (int8_t) tmp_value;
282 if (data == NULL || value == NULL) {
283 PRINTF(
"Received NULL parameter\n");
290 *value = (tmp_value == 1);
311 if (data == NULL || out == NULL) {
312 PRINTF(
"Received NULL parameter\n");
316 if (min_size != 0 && data->
value.
size < min_size) {
317 PRINTF(
"Expected at least %d bytes, found %d\n", min_size, data->
value.
size);
320 if (max_size != 0 && data->
value.
size > max_size) {
321 PRINTF(
"Expected at most %d bytes, found %d\n", max_size, data->
value.
size);
325 PRINTF(
"Non-empty TLV value has a NULL pointer\n");
357 if (data == NULL || out == NULL) {
358 PRINTF(
"Received NULL parameter\n");
363 size_t actual_length = 0;
368 PRINTF(
"Embedded null byte at offset %u\n", (
unsigned) actual_length);
372 if (min_length != 0 && data->
value.
size < min_length) {
373 PRINTF(
"Expected at least %u bytes, found %u\n", min_length, data->
value.
size);
377 if (out_size != 0 && data->
value.
size + 1 > out_size) {
378 PRINTF(
"Expected at most %u bytes, found %u (+1)\n", out_size, data->
value.
size);
403 uint8_t buf[
sizeof(*value)];
405 if (payload == NULL || offset == NULL || value == NULL) {
406 PRINTF(
"Received NULL parameter\n");
413 if ((*offset + byte_length) > payload->
size) {
414 PRINTF(
"TLV payload too small for DER encoded value\n");
417 if (byte_length >
sizeof(buf) || byte_length == 0) {
418 PRINTF(
"Unexpectedly long DER-encoded value (%u bytes)\n", byte_length);
421 memset(buf, 0, (
sizeof(buf) - byte_length));
422 memcpy(buf + (
sizeof(buf) - byte_length), &payload->
ptr[*offset], byte_length);
423 *value = U4BE(buf, 0);
424 *offset += byte_length;
428 *value = payload->
ptr[*offset];
451 *value = (uint16_t) tmp_value;
470 for (
size_t i = 0; i < tag_count; i++) {
473 PRINTF(
"No flag found for tag 0x%x\n", tags[i]);
476 if ((received.
flags & flag) != flag) {
477 PRINTF(
"Tag 0x%x no received\n", tags[i]);
493 uint8_t handlers_count,
497 for (uint8_t idx = 0; idx < handlers_count; ++idx) {
498 if (handlers[idx].tag == tag) {
499 return &handlers[idx];
528 uint8_t handlers_count,
538 size_t tag_start_offset;
544 explicit_bzero(received_tags_flags,
sizeof(*received_tags_flags));
547 PRINTF(
"Parsing TLV payload %.*H\n", payload->
size, payload->
ptr);
553 tag_start_offset = offset;
558 if (handler == NULL) {
559 PRINTF(
"No handler found for tag 0x%x\n", data.
tag);
575 PRINTF(
"Error: can't read %d + %d, only %d\n",
584 .ptr = (data.
value.
size > 0) ? &payload->
ptr[offset] : NULL,
589 PRINTF(
"Handling tag 0x%02x length %d value '%.*H'\n",
596 PRINTF(
"Handling tag 0x%02x length %d\n", data.
tag, data.
value.
size);
602 .ptr = &payload->
ptr[tag_start_offset],
603 .size = offset - tag_start_offset,
609 if (handler->
is_unique && ((received_tags_flags->
flags & flag) == flag)) {
610 PRINTF(
"Tag = %d was already received and is flagged unique\n", data.
tag);
615 fptr = PIC(common_handler);
616 if (fptr != NULL && !(*fptr)(&data, tlv_out)) {
617 PRINTF(
"Common handler error while handling tag 0x%x\n", handler->
tag);
622 fptr = PIC(handler->
func);
623 if (fptr != NULL && !(*fptr)(&data, tlv_out)) {
624 PRINTF(
"Handler error while handling tag 0x%x\n", handler->
tag);
629 received_tags_flags->
flags |= flag;
638 PRINTF(
"Error: unexpected end step %d\n", step);
641 if (offset != payload->
size) {
642 PRINTF(
"Error: unexpected data at the end of the TLV payload!\n");
tag_to_flag_function_t * tag_to_flag_function
size_t size
Pointer to byte buffer.
TLV_flag_t tag_to_flag_function_t(TLV_tag_t tag)
enum tlv_step_e tlv_step_t
bool get_int64_from_tlv_data(const tlv_data_t *data, int64_t *value)
Extract an int64_t value from TLV data.
bool get_uint16_t_from_tlv_data(const tlv_data_t *data, uint16_t *value)
Extract a uint16_t value from TLV data.
#define DER_LONG_FORM_FLAG
bool get_string_from_tlv_data(const tlv_data_t *data, char *out, uint16_t min_length, uint16_t out_size)
Copy a NUL-terminated string from TLV data.
bool get_uint64_t_from_tlv_data(const tlv_data_t *data, uint64_t *value)
Extract a uint64_t value from TLV data.
bool get_bool_from_tlv_data(const tlv_data_t *data, bool *value)
Extract a boolean value from TLV data.
#define DER_FIRST_BYTE_VALUE_MASK
bool get_uint8_t_from_tlv_data(const tlv_data_t *data, uint8_t *value)
Extract a uint8_t value from TLV data.
static bool get_der_value_as_uint32(const buffer_t *payload, size_t *offset, uint32_t *value)
bool tlv_check_received_tags(TLV_reception_t received, const TLV_tag_t *tags, size_t tag_count)
Check that every tag in tags was received during parsing.
bool get_int16_from_tlv_data(const tlv_data_t *data, int16_t *value)
Extract an int16_t value from TLV data.
static const _internal_tlv_handler_t * find_handler(const _internal_tlv_handler_t *handlers, uint8_t handlers_count, TLV_tag_t tag)
Find the handler registered for a given tag.
static bool get_der_value_as_uint16(const buffer_t *payload, size_t *offset, uint16_t *value)
Parse a DER-encoded value and fit it into a uint16_t, or fail.
bool get_int32_from_tlv_data(const tlv_data_t *data, int32_t *value)
Extract an int32_t value from TLV data.
bool get_int8_from_tlv_data(const tlv_data_t *data, int8_t *value)
Extract an int8_t value from TLV data.
bool tlv_enforce_u8_value(const tlv_data_t *data, uint8_t expected_value)
Enforce that a TLV data contains the expected uint8 value.
bool _parse_tlv_internal(const _internal_tlv_handler_t *handlers, uint8_t handlers_count, tlv_handler_cb_t *common_handler, tag_to_flag_function_t *tag_to_flag_function, const buffer_t *payload, void *tlv_out, TLV_reception_t *received_tags_flags)
Parse a raw TLV payload using a pre-built handler table.
bool get_buffer_from_tlv_data(const tlv_data_t *data, buffer_t *out, uint16_t min_size, uint16_t max_size)
Extract a sized buffer from TLV data (zero-copy).
bool get_uint32_t_from_tlv_data(const tlv_data_t *data, uint32_t *value)
Extract a uint32_t value from TLV data.
TLV_reception_internal_t TLV_reception_t
struct tlv_data_s tlv_data_t
bool tlv_handler_cb_t(const tlv_data_t *data, void *tlv_extracted)