Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
ledger_account_provide_contact.c
Go to the documentation of this file.
1/*****************************************************************************
2 * (c) 2026 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
41
42/* Includes ------------------------------------------------------------------*/
43#include <string.h>
44#include "os_utils.h"
45#include "status_words.h"
46#include "tlv_library.h"
47#include "buffer.h"
48#include "bip32.h"
49#include "address_book.h"
51#include "address_book_crypto.h"
52#include "address_book_common.h"
53#include "ledger_account.h"
54
55#if defined(HAVE_ADDRESS_BOOK) && defined(HAVE_ADDRESS_BOOK_LEDGER_ACCOUNT)
56
57/* Private defines------------------------------------------------------------*/
58#define STRUCT_VERSION 0x01
59
60/* Private types, structures, unions -----------------------------------------*/
66
67/* Private macros-------------------------------------------------------------*/
68#define PROVIDE_LEDGER_ACCOUNT_TAGS(X) \
69 X(0x01, TAG_STRUCTURE_TYPE, handle_struct_type, ENFORCE_UNIQUE_TAG) \
70 X(0x02, TAG_STRUCTURE_VERSION, handle_struct_version, ENFORCE_UNIQUE_TAG) \
71 X(0xf0, TAG_CONTACT_NAME, handle_contact_name, ENFORCE_UNIQUE_TAG) \
72 X(0x69, TAG_DERIVATION_PATH, handle_derivation_path, ENFORCE_UNIQUE_TAG) \
73 X(0x23, TAG_CHAIN_ID, handle_chain_id, ENFORCE_UNIQUE_TAG) \
74 X(0x51, TAG_BLOCKCHAIN_FAMILY, handle_blockchain_family, ENFORCE_UNIQUE_TAG) \
75 X(0x29, TAG_HMAC_PROOF, handle_hmac_proof, ENFORCE_UNIQUE_TAG)
76
77/* Private variables ---------------------------------------------------------*/
78
79/* Private functions ---------------------------------------------------------*/
80
88static bool handle_struct_type(const tlv_data_t *data, void *context_ptr)
89{
90 s_provide_ledger_account_ctx *context = context_ptr;
91
92 UNUSED(context);
94 PRINTF("[Provide Ledger Account] Invalid STRUCTURE_TYPE value\n");
95 return false;
96 }
97 return true;
98}
99
107static bool handle_struct_version(const tlv_data_t *data, void *context_ptr)
108{
109 s_provide_ledger_account_ctx *context = context_ptr;
110
111 UNUSED(context);
113 PRINTF("[Provide Ledger Account] Invalid STRUCTURE_VERSION value\n");
114 return false;
115 }
116 return true;
117}
118
126static bool handle_contact_name(const tlv_data_t *data, void *context_ptr)
127{
128 s_provide_ledger_account_ctx *context = context_ptr;
129
132 sizeof(context->ledger_account->account_name))) {
133 PRINTF("[Provide Ledger Account] CONTACT_NAME: failed to parse\n");
134 return false;
135 }
136 return true;
137}
138
146static bool handle_derivation_path(const tlv_data_t *data, void *context_ptr)
147{
148 s_provide_ledger_account_ctx *context = context_ptr;
149
151}
152
160static bool handle_chain_id(const tlv_data_t *data, void *context_ptr)
161{
162 s_provide_ledger_account_ctx *context = context_ptr;
163
165}
166
174static bool handle_blockchain_family(const tlv_data_t *data, void *context_ptr)
175{
176 s_provide_ledger_account_ctx *context = context_ptr;
177
179}
180
188static bool handle_hmac_proof(const tlv_data_t *data, void *context_ptr)
189{
190 s_provide_ledger_account_ctx *context = context_ptr;
191
192 buffer_t buf = {0};
193 if (!get_buffer_from_tlv_data(data, &buf, CX_SHA256_SIZE, CX_SHA256_SIZE)) {
194 PRINTF("[Provide Ledger Account] HMAC_PROOF: invalid length (expected %d bytes)\n",
195 CX_SHA256_SIZE);
196 return false;
197 }
198 memmove(context->hmac_proof, buf.ptr, CX_SHA256_SIZE);
199 return true;
200}
201
202DEFINE_TLV_PARSER(PROVIDE_LEDGER_ACCOUNT_TAGS, NULL, provide_ledger_account_tlv_parser)
203
204
211{
212 bool result = TLV_CHECK_RECEIVED_TAGS(context->received_tags,
213 TAG_STRUCTURE_TYPE,
214 TAG_STRUCTURE_VERSION,
215 TAG_CONTACT_NAME,
216 TAG_DERIVATION_PATH,
217 TAG_BLOCKCHAIN_FAMILY,
218 TAG_HMAC_PROOF);
219 if (!result) {
220 PRINTF("[Provide Ledger Account] Missing mandatory fields!\n");
221 return false;
222 }
223 if (context->ledger_account->blockchain_family == FAMILY_ETHEREUM) {
224 result = TLV_CHECK_RECEIVED_TAGS(context->received_tags, TAG_CHAIN_ID);
225 if (!result) {
226 PRINTF("[Provide Ledger Account] Missing CHAIN_ID for Ethereum family!\n");
227 return false;
228 }
229 }
230 return true;
231}
232
239{
240 UNUSED(context);
241 char out[50] = {0};
242
243 PRINTF("****************************************************************************\n");
244 PRINTF("[Provide Ledger Account] - Retrieved Descriptor:\n");
245 PRINTF("[Provide Ledger Account] - Account name: %s\n",
246 context->ledger_account->account_name);
247 if (bip32_path_format_simple(&context->ledger_account->bip32_path, out, sizeof(out))) {
248 PRINTF("[Provide Ledger Account] - Derivation path[%d]: %s\n",
250 out);
251 }
252 else {
253 PRINTF("[Provide Ledger Account] - Derivation path length[%d] (failed to format)\n",
255 }
256 PRINTF("[Provide Ledger Account] - Blockchain family: %s\n",
259 PRINTF("[Provide Ledger Account] - Chain ID: %llu\n",
260 context->ledger_account->chain_id);
261 }
262}
263
264/* Exported functions --------------------------------------------------------*/
265
276bolos_err_t provide_ledger_account_contact(uint8_t *buffer_in, size_t buffer_in_length)
277{
278 const buffer_t payload = {.ptr = buffer_in, .size = buffer_in_length};
280
281 // Init the structure
282 ctx.ledger_account = &g_ab_payload.ledger_account;
283 memset(&g_ab_payload.ledger_account, 0, sizeof(g_ab_payload.ledger_account));
284
285 // Parse using SDK TLV parser
286 if (!provide_ledger_account_tlv_parser(&payload, &ctx, &ctx.received_tags)) {
287 PRINTF("[Provide Ledger Account] TLV parsing failed\n");
288 return SWO_INCORRECT_DATA;
289 }
290 if (!verify_fields(&ctx)) {
291 return SWO_INCORRECT_DATA;
292 }
293 print_payload(&ctx);
294
295 // Verify HMAC Proof of Registration: HMAC(account_name, path, family[, chain_id])
297 g_ab_payload.ledger_account.account_name,
298 &g_ab_payload.ledger_account.bip32_path,
299 g_ab_payload.ledger_account.blockchain_family,
300 g_ab_payload.ledger_account.chain_id,
301 ctx.hmac_proof)) {
302 PRINTF("[Provide Ledger Account] HMAC proof verification failed\n");
303 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
304 }
305
306 // Pass verified contact to the coin app for storage
307 if (!handle_provide_ledger_account(&g_ab_payload.ledger_account)) {
308 PRINTF("[Provide Ledger Account] Rejected by coin application\n");
309 return SWO_WRONG_PARAMETER_VALUE;
310 }
311
312 return SWO_SUCCESS;
313}
314
315#endif // HAVE_ADDRESS_BOOK && HAVE_ADDRESS_BOOK_LEDGER_ACCOUNT
@ FAMILY_ETHEREUM
#define FAMILY_AS_STR(x)
bool address_book_handle_blockchain_family(const tlv_data_t *data, blockchain_family_e *family)
Generic handler for blockchain family.
bool address_book_handle_derivation_path(const tlv_data_t *data, path_bip32_t *bip32_path)
Generic handler for BIP32 derivation path.
ab_payload_u g_ab_payload
bool address_book_handle_printable_string(const tlv_data_t *data, char *output_buffer, size_t buffer_size)
Generic handler for printable string extraction.
bool address_book_handle_chain_id(const tlv_data_t *data, uint64_t *chain_id)
Generic handler for chain ID.
bool address_book_verify_hmac_proof_ledger_account(const char *name, const path_bip32_t *bip32_path, blockchain_family_e family, uint64_t chain_id, const uint8_t hmac_expected[CX_SHA256_SIZE])
Verify an HMAC Proof of Registration for a Ledger Account.
bool handle_provide_ledger_account(const ledger_account_t *account)
Handle called to store a verified Ledger Account contact.
bool bip32_path_format_simple(path_bip32_t *bip32, char *out, size_t out_len)
Format a BIP32 path as a string.
Definition bip32.c:92
static bool verify_fields(const s_edit_contact_name_ctx *context)
Check that all mandatory TLV tags were received.
#define STRUCT_VERSION
#define TYPE_PROVIDE_LEDGER_ACCOUNT_CONTACT
static bool handle_contact_name(const tlv_data_t *data, void *context_ptr)
Handler for tag CONTACT_NAME.
static bool handle_blockchain_family(const tlv_data_t *data, void *context_ptr)
Handler for tag BLOCKCHAIN_FAMILY.
bolos_err_t provide_ledger_account_contact(uint8_t *buffer_in, size_t buffer_in_length)
Deliver a verified Ledger Account contact to the coin application.
static bool handle_derivation_path(const tlv_data_t *data, void *context_ptr)
Handler for tag DERIVATION_PATH.
static bool handle_hmac_proof(const tlv_data_t *data, void *context_ptr)
Handler for tag HMAC_PROOF.
static bool verify_fields(const s_provide_ledger_account_ctx *context)
Check that all mandatory TLV tags were received.
static bool handle_struct_type(const tlv_data_t *data, void *context_ptr)
Handler for tag STRUCTURE_TYPE.
#define PROVIDE_LEDGER_ACCOUNT_TAGS(X)
static bool handle_chain_id(const tlv_data_t *data, void *context_ptr)
Handler for tag CHAIN_ID.
static bool handle_struct_version(const tlv_data_t *data, void *context_ptr)
Handler for tag STRUCTURE_VERSION.
static void print_payload(const s_provide_ledger_account_ctx *context)
Print the received descriptor.
uint8_t * ptr
Definition buffer.h:22
Data extracted from a Register Ledger Account TLV payload.
path_bip32_t bip32_path
char account_name[ACCOUNT_NAME_LENGTH]
blockchain_family_e blockchain_family
uint8_t length
Definition bip32.h:18
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 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).
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,...)