Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
tlv_use_case_dynamic_descriptor.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 <stdint.h>
18#include <string.h>
19#include <ctype.h>
20
21#include "os_utils.h"
22#include "ox_ec.h"
23#include "os_pic.h"
24#include "os_app.h"
25#include "cx.h"
26#include "os_pki.h"
27#include "ledger_pki.h"
29
30#define TYPE_DYNAMIC_TOKEN 0x90
31
32// Parsed TLV data
33typedef struct tlv_extracted_s {
34 // Received tags set by the parser
36
37 // Dynamic descriptor output data
39
40 // Tags handled by the use case API and not the caller
42 char application_name[BOLOS_APPNAME_MAX_SIZE_B + 1];
43
45
46 // Progressive hash of the received TLVs (except the signature type)
47 cx_sha256_t hash_ctx;
49
50static bool handle_structure_type(const tlv_data_t *data, void *tlv_extracted_ptr)
51{
52 tlv_extracted_t *tlv_extracted = tlv_extracted_ptr;
53
54 return get_uint8_t_from_tlv_data(data, &tlv_extracted->structure_type);
55}
56
57static bool handle_version(const tlv_data_t *data, void *tlv_extracted_ptr)
58{
59 tlv_extracted_t *tlv_extracted = tlv_extracted_ptr;
60
61 return get_uint8_t_from_tlv_data(data, &tlv_extracted->output->version);
62}
63
64static bool handle_coin_type(const tlv_data_t *data, void *tlv_extracted_ptr)
65{
66 tlv_extracted_t *tlv_extracted = tlv_extracted_ptr;
67
68 return get_uint32_t_from_tlv_data(data, &tlv_extracted->output->coin_type);
69}
70
71static bool handle_application_name(const tlv_data_t *data, void *tlv_extracted_ptr)
72{
73 tlv_extracted_t *tlv_extracted = tlv_extracted_ptr;
74
76 data, tlv_extracted->application_name, 1, sizeof(tlv_extracted->application_name));
77}
78
79static bool handle_ticker(const tlv_data_t *data, void *tlv_extracted_ptr)
80{
81 tlv_extracted_t *tlv_extracted = tlv_extracted_ptr;
82
84 data, tlv_extracted->output->ticker, 1, sizeof(tlv_extracted->output->ticker));
85}
86
87static bool handle_magnitude(const tlv_data_t *data, void *tlv_extracted_ptr)
88{
89 tlv_extracted_t *tlv_extracted = tlv_extracted_ptr;
90
91 return get_uint8_t_from_tlv_data(data, &tlv_extracted->output->magnitude);
92}
93
94static bool handle_tuid(const tlv_data_t *data, void *tlv_extracted_ptr)
95{
96 tlv_extracted_t *tlv_extracted = tlv_extracted_ptr;
97
98 return get_buffer_from_tlv_data(data, &tlv_extracted->output->TUID, 0, 0);
99}
100
101static bool handle_signature(const tlv_data_t *data, void *tlv_extracted_ptr)
102{
103 tlv_extracted_t *tlv_extracted = tlv_extracted_ptr;
104
107}
108
109static bool handle_common(const tlv_data_t *data, void *tlv_extracted_ptr);
110
111// clang-format off
112// List of TLV tags recognized by the Solana application
113#define TLV_TAGS(X) \
114 X(0x01, TAG_STRUCTURE_TYPE, handle_structure_type, ENFORCE_UNIQUE_TAG) \
115 X(0x02, TAG_VERSION, handle_version, ENFORCE_UNIQUE_TAG) \
116 X(0x03, TAG_COIN_TYPE, handle_coin_type, ENFORCE_UNIQUE_TAG) \
117 X(0x04, TAG_APPLICATION_NAME, handle_application_name, ENFORCE_UNIQUE_TAG) \
118 X(0x05, TAG_TICKER, handle_ticker, ENFORCE_UNIQUE_TAG) \
119 X(0x06, TAG_MAGNITUDE, handle_magnitude, ENFORCE_UNIQUE_TAG) \
120 X(0x07, TAG_TUID, handle_tuid, ENFORCE_UNIQUE_TAG) \
121 X(0x08, TAG_SIGNATURE, handle_signature, ENFORCE_UNIQUE_TAG)
122// clang-format on
123
124DEFINE_TLV_PARSER(TLV_TAGS, &handle_common, parse_dynamic_token_tag)
125
126static bool handle_common(const tlv_data_t *data, void *tlv_extracted_ptr)
127{
128 tlv_extracted_t *tlv_extracted = tlv_extracted_ptr;
129
130 if (data->tag != TAG_SIGNATURE) {
131 CX_ASSERT(
132 cx_hash_update((cx_hash_t *) &tlv_extracted->hash_ctx, data->raw.ptr, data->raw.size));
133 }
134 return true;
135}
136
139{
140 if (payload == NULL || out == NULL) {
141 PRINTF("Received NULL parameter\n");
143 }
144
145 // Parser output
146 tlv_extracted_t tlv_extracted = {0};
147 tlv_extracted.output = out;
148
149 // Reset output, the caller is not required to pre-zero it
150 memset(out, 0, sizeof(*out));
151
152 // The parser will fill it with the hash of the whole TLV payload (except SIGN tag)
153 cx_sha256_init(&tlv_extracted.hash_ctx);
154
155 // Call the function created by the macro from the TLV lib
156 if (!parse_dynamic_token_tag(payload, &tlv_extracted, &tlv_extracted.received_tags)) {
157 PRINTF("Failed to parse tlv payload\n");
159 }
160
161 if (!TLV_CHECK_RECEIVED_TAGS(tlv_extracted.received_tags, TAG_STRUCTURE_TYPE)) {
162 PRINTF("Error: no struct type specified!\n");
164 }
165
166 if (tlv_extracted.structure_type != TYPE_DYNAMIC_TOKEN) {
167 PRINTF("Error: unexpected struct type %d\n", tlv_extracted.structure_type);
169 }
170
171 if (!TLV_CHECK_RECEIVED_TAGS(tlv_extracted.received_tags,
172 TAG_VERSION,
173 TAG_COIN_TYPE,
174 TAG_APPLICATION_NAME,
175 TAG_TICKER,
176 TAG_MAGNITUDE,
177 TAG_TUID,
178 TAG_SIGNATURE)) {
179 PRINTF("Error: missing required fields in struct version 1\n");
181 }
182
183 if (strcmp(tlv_extracted.application_name, APPNAME) != 0) {
184 PRINTF("Error: unsupported application name %s\n", tlv_extracted.application_name);
186 }
187
188 if (tlv_extracted.output->version == 0 || tlv_extracted.output->version > 1) {
189 PRINTF("Error: unsupported struct version %d\n", tlv_extracted.output->version);
191 }
192
193 // Finalize hash object filled by the parser
194 uint8_t tlv_hash[CX_SHA256_SIZE] = {0};
195 CX_ASSERT(cx_hash_final((cx_hash_t *) &tlv_extracted.hash_ctx, tlv_hash));
196 buffer_t hash = {.ptr = tlv_hash, .size = sizeof(tlv_hash)};
197
198 // Verify that the signature field of the TLV is the signature of the TLV hash by the key
199 // loaded by the PKI
201 uint8_t expected_key_usage = CERTIFICATE_PUBLIC_KEY_USAGE_COIN_META;
202 cx_curve_t expected_curve = CX_CURVE_SECP256K1;
204 hash, &expected_key_usage, &expected_curve, tlv_extracted.input_sig);
206 PRINTF("Failed to verify signature of dynamic token info\n");
208 }
209
211}
check_signature_with_pki_status_t check_signature_with_pki(const buffer_t hash, const uint8_t *expected_key_usage, const cx_curve_t *expected_curve, const buffer_t signature)
Checks a signature using the PKI certificate.
Definition ledger_pki.c:5
@ CHECK_SIGNATURE_WITH_PKI_SUCCESS
Definition ledger_pki.h:7
enum check_signature_with_pki_status_e check_signature_with_pki_status_t
char application_name[BOLOS_APPNAME_MAX_SIZE_B+1]
tlv_dynamic_descriptor_out_t * output
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_uint8_t_from_tlv_data(const tlv_data_t *data, uint8_t *value)
Extract a uint8_t value from TLV data.
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
#define DEFINE_TLV_PARSER(TAG_LIST, COMMON_HANDLER, PARSE_FUNCTION_NAME)
Creates a parser function for a given TLV use case.
struct tlv_data_s tlv_data_t
#define TLV_CHECK_RECEIVED_TAGS(received,...)
static bool handle_tuid(const tlv_data_t *data, void *tlv_extracted_ptr)
static bool handle_coin_type(const tlv_data_t *data, void *tlv_extracted_ptr)
struct tlv_extracted_s tlv_extracted_t
#define TLV_TAGS(X)
#define TYPE_DYNAMIC_TOKEN
static bool handle_ticker(const tlv_data_t *data, void *tlv_extracted_ptr)
static bool handle_version(const tlv_data_t *data, void *tlv_extracted_ptr)
static bool handle_application_name(const tlv_data_t *data, void *tlv_extracted_ptr)
static bool handle_structure_type(const tlv_data_t *data, void *tlv_extracted_ptr)
static bool handle_common(const tlv_data_t *data, void *tlv_extracted_ptr)
static bool handle_signature(const tlv_data_t *data, void *tlv_extracted_ptr)
static bool handle_magnitude(const tlv_data_t *data, void *tlv_extracted_ptr)
tlv_dynamic_descriptor_status_t tlv_use_case_dynamic_descriptor(const buffer_t *payload, tlv_dynamic_descriptor_out_t *out)
Processes a TLV dynamic descriptor use case.
@ TLV_DYNAMIC_DESCRIPTOR_MISSING_STRUCTURE_TAG
@ TLV_DYNAMIC_DESCRIPTOR_SIGNATURE_ERROR
@ TLV_DYNAMIC_DESCRIPTOR_PARSING_ERROR
@ TLV_DYNAMIC_DESCRIPTOR_WRONG_APPLICATION_NAME
@ TLV_DYNAMIC_DESCRIPTOR_WRONG_TYPE
@ TLV_DYNAMIC_DESCRIPTOR_UNKNOWN_VERSION
@ TLV_DYNAMIC_DESCRIPTOR_MISSING_TAG
#define DER_SIGNATURE_MIN_SIZE
enum tlv_dynamic_descriptor_status_e tlv_dynamic_descriptor_status_t
#define DER_SIGNATURE_MAX_SIZE
struct tlv_dynamic_descriptor_out_s tlv_dynamic_descriptor_out_t