Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
tlv_library.c
Go to the documentation of this file.
1/*****************************************************************************
2 * (c) 2025 Ledger SAS.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#include <stddef.h>
18#include <stdint.h>
19#include <stdbool.h>
20#include <string.h>
21
22#include "os_utils.h"
23#include "os_pic.h"
24#include "cx.h"
25
26#include "tlv_library.h"
27
28#define DER_LONG_FORM_FLAG 0x80 // 8th bit set
29#define DER_FIRST_BYTE_VALUE_MASK 0x7f
30
38bool tlv_enforce_u8_value(const tlv_data_t *data, uint8_t expected_value)
39{
40 uint8_t value = 0;
41 if (!get_uint8_t_from_tlv_data(data, &value)) {
42 PRINTF("Failed to extract uint8 value from TLV data (tag 0x%x)\n", data->tag);
43 return false;
44 }
45 if (value != expected_value) {
46 PRINTF(
47 "Value mismatch for tag 0x%x: expected %u, got %u\n", data->tag, expected_value, value);
48 return false;
49 }
50 return true;
51}
52
64bool get_uint64_t_from_tlv_data(const tlv_data_t *data, uint64_t *value)
65{
66 if (data == NULL || value == NULL) {
67 PRINTF("Received NULL parameter\n");
68 return false;
69 }
70 if (data->value.size == 0 || data->value.size > sizeof(uint64_t)) {
71 PRINTF("Invalid length (%u bytes) for tag 0x%x (expected 1-%zu bytes)\n",
72 data->value.size,
73 data->tag,
74 sizeof(uint64_t));
75 return false;
76 }
77
78 // Pad with zeros before calling U8BE()
79 uint8_t buffer[sizeof(uint64_t)] = {0};
80 memcpy(buffer + (sizeof(buffer) - data->value.size), data->value.ptr, data->value.size);
81
82 *value = U8BE(buffer, 0);
83 return true;
84}
85
96bool get_uint32_t_from_tlv_data(const tlv_data_t *data, uint32_t *value)
97{
98 if (data == NULL || value == NULL) {
99 PRINTF("Received NULL parameter\n");
100 return false;
101 }
102 uint64_t tmp_value;
103 if (!get_uint64_t_from_tlv_data(data, &tmp_value) || tmp_value > UINT32_MAX) {
104 return false;
105 }
106 *value = (uint32_t) tmp_value;
107 return true;
108}
109
120bool get_uint16_t_from_tlv_data(const tlv_data_t *data, uint16_t *value)
121{
122 if (data == NULL || value == NULL) {
123 PRINTF("Received NULL parameter\n");
124 return false;
125 }
126 uint64_t tmp_value;
127 if (!get_uint64_t_from_tlv_data(data, &tmp_value) || (tmp_value > UINT16_MAX)) {
128 return false;
129 }
130 *value = (uint16_t) tmp_value;
131 return true;
132}
133
144bool get_uint8_t_from_tlv_data(const tlv_data_t *data, uint8_t *value)
145{
146 if (data == NULL || value == NULL) {
147 PRINTF("Received NULL parameter\n");
148 return false;
149 }
150 uint64_t tmp_value;
151 if (!get_uint64_t_from_tlv_data(data, &tmp_value) || (tmp_value > UINT8_MAX)) {
152 return false;
153 }
154 *value = (uint8_t) tmp_value;
155 return true;
156}
157
168bool get_int64_from_tlv_data(const tlv_data_t *data, int64_t *value)
169{
170 if (data == NULL || value == NULL) {
171 PRINTF("Received NULL parameter\n");
172 return false;
173 }
174 if (data->value.size == 0 || data->value.size > sizeof(int64_t)) {
175 PRINTF("Invalid length (%u bytes) for tag 0x%x (expected 1-%zu bytes)\n",
176 data->value.size,
177 data->tag,
178 sizeof(int64_t));
179 return false;
180 }
181
182 // Sign-extend before calling U8BE()
183 uint8_t buffer[sizeof(int64_t)];
184 memset(buffer, (data->value.ptr[0] & 0x80) ? 0xFF : 0x00, sizeof(buffer));
185 memcpy(buffer + (sizeof(buffer) - data->value.size), data->value.ptr, data->value.size);
186
187 *value = (int64_t) U8BE(buffer, 0);
188 return true;
189}
190
202bool get_int32_from_tlv_data(const tlv_data_t *data, int32_t *value)
203{
204 if (data == NULL || value == NULL) {
205 PRINTF("Received NULL parameter\n");
206 return false;
207 }
208 int64_t tmp_value;
209 if (!get_int64_from_tlv_data(data, &tmp_value) || (tmp_value < INT32_MIN)
210 || (tmp_value > INT32_MAX)) {
211 return false;
212 }
213 *value = (int32_t) tmp_value;
214 return true;
215}
216
228bool get_int16_from_tlv_data(const tlv_data_t *data, int16_t *value)
229{
230 if (data == NULL || value == NULL) {
231 PRINTF("Received NULL parameter\n");
232 return false;
233 }
234 int64_t tmp_value;
235 if (!get_int64_from_tlv_data(data, &tmp_value) || (tmp_value < INT16_MIN)
236 || (tmp_value > INT16_MAX)) {
237 return false;
238 }
239 *value = (int16_t) tmp_value;
240 return true;
241}
242
254bool get_int8_from_tlv_data(const tlv_data_t *data, int8_t *value)
255{
256 if (data == NULL || value == NULL) {
257 PRINTF("Received NULL parameter\n");
258 return false;
259 }
260 int64_t tmp_value;
261 if (!get_int64_from_tlv_data(data, &tmp_value) || (tmp_value < INT8_MIN)
262 || (tmp_value > INT8_MAX)) {
263 return false;
264 }
265 *value = (int8_t) tmp_value;
266 return true;
267}
268
280bool get_bool_from_tlv_data(const tlv_data_t *data, bool *value)
281{
282 if (data == NULL || value == NULL) {
283 PRINTF("Received NULL parameter\n");
284 return false;
285 }
286 uint8_t tmp_value;
287 if (!get_uint8_t_from_tlv_data(data, &tmp_value) || (tmp_value > 1)) {
288 return false;
289 }
290 *value = (tmp_value == 1);
291 return true;
292}
293
307 buffer_t *out,
308 uint16_t min_size,
309 uint16_t max_size)
310{
311 if (data == NULL || out == NULL) {
312 PRINTF("Received NULL parameter\n");
313 return false;
314 }
315
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);
318 return false;
319 }
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);
322 return false;
323 }
324 if (data->value.size != 0 && data->value.ptr == NULL) {
325 PRINTF("Non-empty TLV value has a NULL pointer\n");
326 return false;
327 }
328 // Fully initialize the output, the caller is not required to pre-zero it
329 *out = (buffer_t){
330 .ptr = data->value.ptr,
331 .size = data->value.size,
332 .offset = 0,
333 };
334 return true;
335}
336
353 char *out,
354 uint16_t min_length,
355 uint16_t out_size)
356{
357 if (data == NULL || out == NULL) {
358 PRINTF("Received NULL parameter\n");
359 return false;
360 }
361
362 // Reject TLV strings with embedded null bytes
363 size_t actual_length = 0;
364 if (data->value.size > 0) {
365 actual_length = strnlen((const char *) data->value.ptr, data->value.size);
366 }
367 if (actual_length != data->value.size) {
368 PRINTF("Embedded null byte at offset %u\n", (unsigned) actual_length);
369 return false;
370 }
371
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);
374 return false;
375 }
376 // The input is not '\0' terminated
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);
379 return false;
380 }
381
382 if (data->value.size > 0) {
383 memcpy(out, data->value.ptr, data->value.size);
384 }
385 out[data->value.size] = '\0';
386
387 return true;
388}
389
400static bool get_der_value_as_uint32(const buffer_t *payload, size_t *offset, uint32_t *value)
401{
402 uint8_t byte_length;
403 uint8_t buf[sizeof(*value)];
404
405 if (payload == NULL || offset == NULL || value == NULL) {
406 PRINTF("Received NULL parameter\n");
407 return false;
408 }
409
410 if (payload->ptr[*offset] & DER_LONG_FORM_FLAG) { // long form
411 byte_length = payload->ptr[*offset] & DER_FIRST_BYTE_VALUE_MASK;
412 *offset += 1;
413 if ((*offset + byte_length) > payload->size) {
414 PRINTF("TLV payload too small for DER encoded value\n");
415 return false;
416 }
417 if (byte_length > sizeof(buf) || byte_length == 0) {
418 PRINTF("Unexpectedly long DER-encoded value (%u bytes)\n", byte_length);
419 return false;
420 }
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;
425 return true;
426 }
427 else { // short form
428 *value = payload->ptr[*offset];
429 *offset += 1;
430 return true;
431 }
432}
433
444static bool get_der_value_as_uint16(const buffer_t *payload, size_t *offset, uint16_t *value)
445{
446 uint32_t tmp_value;
447 if (!get_der_value_as_uint32(payload, offset, &tmp_value) || (tmp_value > UINT16_MAX)) {
448 return false;
449 }
450
451 *value = (uint16_t) tmp_value;
452 return true;
453}
454
468bool tlv_check_received_tags(TLV_reception_t received, const TLV_tag_t *tags, size_t tag_count)
469{
470 for (size_t i = 0; i < tag_count; i++) {
471 TLV_flag_t flag = received.tag_to_flag_function(tags[i]);
472 if (flag == 0) {
473 PRINTF("No flag found for tag 0x%x\n", tags[i]);
474 return false;
475 }
476 if ((received.flags & flag) != flag) {
477 PRINTF("Tag 0x%x no received\n", tags[i]);
478 return false;
479 }
480 }
481 return true;
482}
483
493 uint8_t handlers_count,
494 TLV_tag_t tag)
495{
496 // check if a handler exists for this tag
497 for (uint8_t idx = 0; idx < handlers_count; ++idx) {
498 if (handlers[idx].tag == tag) {
499 return &handlers[idx];
500 }
501 }
502 return NULL;
503}
504
510
528 uint8_t handlers_count,
529 tlv_handler_cb_t *common_handler,
530 tag_to_flag_function_t *tag_to_flag_function,
531 const buffer_t *payload,
532 void *tlv_out,
533 TLV_reception_t *received_tags_flags)
534{
535 tlv_step_t step = TLV_TAG;
536 tlv_data_t data = {0};
537 size_t offset = 0;
538 size_t tag_start_offset;
539 const _internal_tlv_handler_t *handler;
540 tlv_handler_cb_t *fptr;
541 uint16_t size;
542 TLV_flag_t flag;
543
544 explicit_bzero(received_tags_flags, sizeof(*received_tags_flags));
545 received_tags_flags->tag_to_flag_function = PIC(tag_to_flag_function);
546
547 PRINTF("Parsing TLV payload %.*H\n", payload->size, payload->ptr);
548
549 // handle TLV payload
550 while (offset < payload->size || (step == TLV_VALUE && data.value.size == 0)) {
551 switch (step) {
552 case TLV_TAG:
553 tag_start_offset = offset;
554 if (!get_der_value_as_uint32(payload, &offset, &data.tag)) {
555 return false;
556 }
557 handler = find_handler(handlers, handlers_count, data.tag);
558 if (handler == NULL) {
559 PRINTF("No handler found for tag 0x%x\n", data.tag);
560 return false;
561 }
562 step = TLV_LENGTH;
563 break;
564
565 case TLV_LENGTH:
566 if (!get_der_value_as_uint16(payload, &offset, &size)) {
567 return false;
568 }
569 data.value.size = size;
570 step = TLV_VALUE;
571 break;
572
573 case TLV_VALUE:
574 if ((offset + data.value.size) > payload->size) {
575 PRINTF("Error: can't read %d + %d, only %d\n",
576 offset,
577 data.value.size,
578 payload->size);
579 return false;
580 }
581 // Fully (re)build the views given to the handlers: they are exposed as regular
582 // buffer_t, so their offset must start at 0 for buffer_read_* to be correct
583 data.value = (buffer_t){
584 .ptr = (data.value.size > 0) ? &payload->ptr[offset] : NULL,
585 .size = data.value.size,
586 .offset = 0,
587 };
588 if (data.value.size > 0) {
589 PRINTF("Handling tag 0x%02x length %d value '%.*H'\n",
590 data.tag,
591 data.value.size,
592 data.value.size,
593 data.value.ptr);
594 }
595 else {
596 PRINTF("Handling tag 0x%02x length %d\n", data.tag, data.value.size);
597 }
598 offset += data.value.size;
599
600 // Calculate raw TLV start/end to give to the handler
601 data.raw = (buffer_t){
602 .ptr = &payload->ptr[tag_start_offset],
603 .size = offset - tag_start_offset,
604 .offset = 0,
605 };
606
607 // Check for duplicate tag
608 flag = received_tags_flags->tag_to_flag_function(data.tag);
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);
611 return false;
612 }
613
614 // Call the common handler if there is one
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);
618 return false;
619 }
620
621 // Call this tag's handler if there is one
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);
625 return false;
626 }
627
628 // Flag reception after handler callback in case the handler wants to check it
629 received_tags_flags->flags |= flag;
630 step = TLV_TAG;
631 break;
632
633 default:
634 return false;
635 }
636 }
637 if (step != TLV_TAG) {
638 PRINTF("Error: unexpected end step %d\n", step);
639 return false;
640 }
641 if (offset != payload->size) {
642 PRINTF("Error: unexpected data at the end of the TLV payload!\n");
643 return false;
644 }
645
646 return true;
647}
tag_to_flag_function_t * tag_to_flag_function
tlv_handler_cb_t * func
uint8_t * ptr
Definition buffer.h:22
size_t size
Pointer to byte buffer.
Definition buffer.h:23
buffer_t value
Definition tlv_library.h:91
buffer_t raw
Definition tlv_library.h:97
TLV_tag_t tag
Definition tlv_library.h:87
uint32_t TLV_tag_t
TLV_flag_t tag_to_flag_function_t(TLV_tag_t tag)
uint64_t TLV_flag_t
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
Definition tlv_library.c:28
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.
Definition tlv_library.c:64
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
Definition tlv_library.c:29
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.
tlv_step_e
@ TLV_TAG
@ TLV_VALUE
@ TLV_LENGTH
bool tlv_enforce_u8_value(const tlv_data_t *data, uint8_t expected_value)
Enforce that a TLV data contains the expected uint8 value.
Definition tlv_library.c:38
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.
Definition tlv_library.c:96
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)