Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
ledger_account_edit.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
33
34/* Includes ------------------------------------------------------------------*/
35#include <string.h>
36#include "os_apdu.h"
37#include "status_words.h"
38#include "tlv_library.h"
39#include "buffer.h"
40#include "bip32.h"
41#include "address_book.h"
43#include "address_book_crypto.h"
44#include "address_book_common.h"
45#include "ledger_account.h"
46#include "io.h"
47#include "nbgl_use_case.h"
48
49#if defined(HAVE_ADDRESS_BOOK) && defined(HAVE_ADDRESS_BOOK_LEDGER_ACCOUNT)
50
51/* Private defines------------------------------------------------------------*/
52#define STRUCT_VERSION 0x01
53
54/* Private enumerations ------------------------------------------------------*/
55
56/* Private types, structures, unions -----------------------------------------*/
57
63
64/* Private macros-------------------------------------------------------------*/
65#define EDIT_LEDGER_ACCOUNT_TAGS(X) \
66 X(0x01, TAG_STRUCTURE_TYPE, handle_struct_type, ENFORCE_UNIQUE_TAG) \
67 X(0x02, TAG_STRUCTURE_VERSION, handle_struct_version, ENFORCE_UNIQUE_TAG) \
68 X(0xf0, TAG_CONTACT_NAME, handle_contact_name, ENFORCE_UNIQUE_TAG) \
69 X(0xf3, TAG_PREVIOUS_NAME, handle_previous_name, ENFORCE_UNIQUE_TAG) \
70 X(0x69, TAG_DERIVATION_PATH, handle_derivation_path, ENFORCE_UNIQUE_TAG) \
71 X(0x23, TAG_CHAIN_ID, handle_chain_id, ENFORCE_UNIQUE_TAG) \
72 X(0x29, TAG_HMAC_PROOF, handle_hmac_proof, ENFORCE_UNIQUE_TAG) \
73 X(0x51, TAG_BLOCKCHAIN_FAMILY, handle_blockchain_family, ENFORCE_UNIQUE_TAG)
74
75/* Private variables ---------------------------------------------------------*/
76
77/* Private functions ---------------------------------------------------------*/
78
86static bool handle_struct_type(const tlv_data_t *data, void *context_ptr)
87{
88 s_edit_ledger_account_ctx *context = context_ptr;
89
90 UNUSED(context);
92 PRINTF("[Edit Ledger Account] Invalid STRUCTURE_TYPE value\n");
93 return false;
94 }
95 return true;
96}
97
105static bool handle_struct_version(const tlv_data_t *data, void *context_ptr)
106{
107 s_edit_ledger_account_ctx *context = context_ptr;
108
109 UNUSED(context);
111 PRINTF("[Edit Ledger Account] Invalid STRUCTURE_VERSION value\n");
112 return false;
113 }
114 return true;
115}
116
124static bool handle_contact_name(const tlv_data_t *data, void *context_ptr)
125{
126 s_edit_ledger_account_ctx *context = context_ptr;
127
130 sizeof(context->edit->ledger_account.account_name))) {
131 PRINTF("CONTACT_NAME: failed to parse\n");
132 return false;
133 }
134 return true;
135}
136
144static bool handle_previous_name(const tlv_data_t *data, void *context_ptr)
145{
146 s_edit_ledger_account_ctx *context = context_ptr;
147
149 data, context->edit->old_account_name, sizeof(context->edit->old_account_name))) {
150 PRINTF("PREVIOUS_NAME: failed to parse\n");
151 return false;
152 }
153 return true;
154}
155
163static bool handle_derivation_path(const tlv_data_t *data, void *context_ptr)
164{
165 s_edit_ledger_account_ctx *context = context_ptr;
166
168}
169
177static bool handle_chain_id(const tlv_data_t *data, void *context_ptr)
178{
179 s_edit_ledger_account_ctx *context = context_ptr;
180
182}
183
191static bool handle_blockchain_family(const tlv_data_t *data, void *context_ptr)
192{
193 s_edit_ledger_account_ctx *context = context_ptr;
194
197}
198
206static bool handle_hmac_proof(const tlv_data_t *data, void *context_ptr)
207{
208 s_edit_ledger_account_ctx *context = context_ptr;
209
210 buffer_t buf = {0};
211 if (!get_buffer_from_tlv_data(data, &buf, CX_SHA256_SIZE, CX_SHA256_SIZE)) {
212 PRINTF("[Edit Ledger Account] HMAC_PROOF: invalid length (expected %d bytes)\n",
213 CX_SHA256_SIZE);
214 return false;
215 }
216 memmove(context->hmac_proof, buf.ptr, CX_SHA256_SIZE);
217 return true;
218}
219
220DEFINE_TLV_PARSER(EDIT_LEDGER_ACCOUNT_TAGS, NULL, edit_ledger_account_tlv_parser)
221
222
228static bool verify_fields(const s_edit_ledger_account_ctx *context)
229{
230 bool result = TLV_CHECK_RECEIVED_TAGS(context->received_tags,
231 TAG_STRUCTURE_TYPE,
232 TAG_STRUCTURE_VERSION,
233 TAG_CONTACT_NAME,
234 TAG_PREVIOUS_NAME,
235 TAG_DERIVATION_PATH,
236 TAG_BLOCKCHAIN_FAMILY,
237 TAG_HMAC_PROOF);
238 if (!result) {
239 PRINTF("Missing mandatory fields in Edit Ledger Account descriptor!\n");
240 return false;
241 }
242 if (context->edit->ledger_account.blockchain_family == FAMILY_ETHEREUM) {
243 result = TLV_CHECK_RECEIVED_TAGS(context->received_tags, TAG_CHAIN_ID);
244 if (!result) {
245 PRINTF("Missing CHAIN_ID for Ethereum family in Edit Ledger Account descriptor!\n");
246 return false;
247 }
248 }
249 return true;
250}
251
258static void print_payload(const s_edit_ledger_account_ctx *context)
259{
260 UNUSED(context);
261 char out[50] = {0};
262
263 PRINTF("****************************************************************************\n");
264 PRINTF("[Edit Ledger Account] - Retrieved Descriptor:\n");
265 PRINTF("[Edit Ledger Account] - Old name: %s\n", context->edit->old_account_name);
266 PRINTF("[Edit Ledger Account] - New name: %s\n",
268 if (bip32_path_format_simple(&context->edit->ledger_account.bip32_path, out, sizeof(out))) {
269 PRINTF("[Edit Ledger Account] - Derivation path[%d]: %s\n",
271 out);
272 }
273 else {
274 PRINTF("[Edit Ledger Account] - Derivation path length[%d] (failed to format)\n",
276 }
277 PRINTF("[Edit Ledger Account] - Blockchain family: %s\n",
280 PRINTF("[Edit Ledger Account] - Chain ID: %llu\n",
281 context->edit->ledger_account.chain_id);
282 }
283}
284
293static bool build_and_send_response(void)
294{
295 uint8_t hmac_proof[CX_SHA256_SIZE] = {0};
296
298 (const char *) g_ab_payload.edit_ledger_account.ledger_account.account_name,
299 &g_ab_payload.edit_ledger_account.ledger_account.bip32_path,
300 g_ab_payload.edit_ledger_account.ledger_account.blockchain_family,
301 g_ab_payload.edit_ledger_account.ledger_account.chain_id,
302 hmac_proof)) {
303 PRINTF("[Edit Ledger Account] Error: Failed to compute new HMAC proof\n");
304 return false;
305 }
306
308 explicit_bzero(hmac_proof, sizeof(hmac_proof));
309 return ok;
310}
311
317static void review_choice(bool confirm)
318{
319 if (confirm) {
320 bool ok = build_and_send_response();
321 if (ok) {
322 on_edit_ledger_account_applied(&g_ab_payload.edit_ledger_account);
323 }
324 else {
325 PRINTF("[Edit Ledger Account] Error: Failed to build and send HMAC proof\n");
326 }
328 ok, "Account name changed", "Error during update", finalize_ui_ledger_account);
329 }
330 else {
332 }
333}
334
335/* Exported functions --------------------------------------------------------*/
336
344bolos_err_t edit_ledger_account(uint8_t *buffer_in, size_t buffer_in_length)
345{
346 const buffer_t payload = {.ptr = buffer_in, .size = buffer_in_length};
348
349 // Init the structure
350 ctx.edit = &g_ab_payload.edit_ledger_account;
351 memset(&g_ab_payload.edit_ledger_account, 0, sizeof(g_ab_payload.edit_ledger_account));
352
353 // Parse using SDK TLV parser
354 if (!edit_ledger_account_tlv_parser(&payload, &ctx, &ctx.received_tags)) {
355 PRINTF("[Edit Ledger Account] TLV parsing failed\n");
356 return SWO_INCORRECT_DATA;
357 }
358 if (!verify_fields(&ctx)) {
359 return SWO_INCORRECT_DATA;
360 }
361 print_payload(&ctx);
362
363 // Verify that the host holds a valid proof from the previous registration
365 g_ab_payload.edit_ledger_account.old_account_name,
366 &g_ab_payload.edit_ledger_account.ledger_account.bip32_path,
367 g_ab_payload.edit_ledger_account.ledger_account.blockchain_family,
368 g_ab_payload.edit_ledger_account.ledger_account.chain_id,
369 ctx.hmac_proof)) {
370 PRINTF("[Edit Ledger Account] HMAC proof verification failed\n");
371 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
372 }
373
374 // Coin-app validation: derive and cache the address
375 if (!handle_check_edit_ledger_account(&g_ab_payload.edit_ledger_account)) {
376 PRINTF("[Edit Ledger Account] Coin-app rejected edit\n");
377 return SWO_INCORRECT_DATA;
378 }
379
380 // Display confirmation UI — reuse the Register Ledger Account review
382 return SWO_NO_RESPONSE;
383}
384
385#endif // HAVE_ADDRESS_BOOK && HAVE_ADDRESS_BOOK_LEDGER_ACCOUNT
@ FAMILY_ETHEREUM
#define FAMILY_AS_STR(x)
void address_book_finalize_review(bool success, const char *successMsg, const char *errorMsg, nbgl_callback_t finalize)
Common handling of a confirmed Address Book review's outcome.
void address_book_handle_review_rejected(nbgl_callback_t finalize)
Common handling of a rejected Address Book review.
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_send_hmac_proof(uint8_t type, const uint8_t hmac_proof[CX_SHA256_SIZE])
Send an HMAC proof response for Edit operations.
bool address_book_compute_hmac_proof_ledger_account(const char *name, const path_bip32_t *bip32_path, blockchain_family_e family, uint64_t chain_id, uint8_t hmac_out[CX_SHA256_SIZE])
Compute an HMAC-SHA256 Proof of Registration for a Ledger Account.
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.
void on_edit_ledger_account_applied(const edit_ledger_account_t *edit)
Notification that a Ledger Account edit was successfully applied.
bool handle_check_edit_ledger_account(edit_ledger_account_t *params)
Handle called to validate the Edit Ledger Account parameters.
void display_register_ledger_account_review(nbgl_choiceCallback_t choice_callback)
Handle called to display the Ledger Account registration review screen.
void finalize_ui_ledger_account(void)
Handle called to finalize the UI flow for a Ledger Account operation.
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 void review_choice(bool confirm)
NBGL callback invoked after the user confirms or rejects the edit.
static bool verify_fields(const s_edit_contact_name_ctx *context)
Check that all mandatory TLV tags were received.
#define STRUCT_VERSION
#define TYPE_EDIT_LEDGER_ACCOUNT
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.
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 void review_choice(bool confirm)
NBGL callback invoked after the user confirms or rejects the edit.
static bool handle_previous_name(const tlv_data_t *data, void *context_ptr)
Handler for tag PREVIOUS_NAME.
static bool handle_struct_type(const tlv_data_t *data, void *context_ptr)
Handler for tag STRUCTURE_TYPE.
static bool handle_chain_id(const tlv_data_t *data, void *context_ptr)
Handler for tag CHAIN_ID.
static bool build_and_send_response(void)
Build and send response.
static bool handle_struct_version(const tlv_data_t *data, void *context_ptr)
Handler for tag STRUCTURE_VERSION.
#define EDIT_LEDGER_ACCOUNT_TAGS(X)
bolos_err_t edit_ledger_account(uint8_t *buffer_in, size_t buffer_in_length)
Edit the name of an existing Ledger Account.
static bool verify_fields(const s_edit_ledger_account_ctx *context)
Check that all mandatory TLV tags were received.
static void print_payload(const s_edit_ledger_account_ctx *context)
Print the received descriptor.
API of the Advanced BOLOS Graphical Library, for typical application use-cases.
uint8_t * ptr
Definition buffer.h:22
Data extracted from a Rename Ledger Account TLV payload.
char old_account_name[ACCOUNT_NAME_LENGTH]
Name being replaced.
ledger_account_t ledger_account
New account (account_name = new name).
path_bip32_t bip32_path
char account_name[ACCOUNT_NAME_LENGTH]
blockchain_family_e blockchain_family
uint8_t length
Definition bip32.h:18
edit_ledger_account_t * edit
uint8_t hmac_proof[CX_SHA256_SIZE]
HMAC proof of the previous registration.
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,...)