Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
ledger_account_register.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
30
31/* Includes ------------------------------------------------------------------*/
32#include <string.h>
33#include "os_utils.h"
34#include "os_apdu.h"
35#include "os_pic.h"
36#include "status_words.h"
37#include "tlv_library.h"
38#include "buffer.h"
39#include "bip32.h"
40#include "address_book.h"
42#include "address_book_crypto.h"
43#include "address_book_common.h"
44#include "ledger_account.h"
45#include "io.h"
46#include "nbgl_use_case.h"
47
48#if defined(HAVE_ADDRESS_BOOK) && defined(HAVE_ADDRESS_BOOK_LEDGER_ACCOUNT)
49
50/* Private defines------------------------------------------------------------*/
51#define STRUCT_VERSION 0x01
52
53/* Private enumerations ------------------------------------------------------*/
54
55/* Private types, structures, unions -----------------------------------------*/
56
61
62/* Private macros-------------------------------------------------------------*/
63#define LEDGER_ACCOUNT_TAGS(X) \
64 X(0x01, TAG_STRUCTURE_TYPE, handle_struct_type, ENFORCE_UNIQUE_TAG) \
65 X(0x02, TAG_STRUCTURE_VERSION, handle_struct_version, ENFORCE_UNIQUE_TAG) \
66 X(0xf0, TAG_CONTACT_NAME, handle_contact_name, ENFORCE_UNIQUE_TAG) \
67 X(0x69, TAG_DERIVATION_PATH, handle_derivation_path, ENFORCE_UNIQUE_TAG) \
68 X(0x23, TAG_CHAIN_ID, handle_chain_id, ENFORCE_UNIQUE_TAG) \
69 X(0x51, TAG_BLOCKCHAIN_FAMILY, handle_blockchain_family, ENFORCE_UNIQUE_TAG)
70
71/* Private variables ---------------------------------------------------------*/
72
73/* Private functions ---------------------------------------------------------*/
74
82static bool handle_struct_type(const tlv_data_t *data, void *context_ptr)
83{
84 s_ledger_account_ctx *context = context_ptr;
85
86 UNUSED(context);
88 PRINTF("[Register Ledger Account] Invalid STRUCTURE_TYPE value\n");
89 return false;
90 }
91 return true;
92}
93
101static bool handle_struct_version(const tlv_data_t *data, void *context_ptr)
102{
103 s_ledger_account_ctx *context = context_ptr;
104
105 UNUSED(context);
107 PRINTF("[Register Ledger Account] Invalid STRUCTURE_VERSION value\n");
108 return false;
109 }
110 return true;
111}
112
120static bool handle_contact_name(const tlv_data_t *data, void *context_ptr)
121{
122 s_ledger_account_ctx *context = context_ptr;
123
126 sizeof(context->ledger_account->account_name))) {
127 PRINTF("CONTACT_NAME: failed to parse\n");
128 return false;
129 }
130 return true;
131}
132
140static bool handle_derivation_path(const tlv_data_t *data, void *context_ptr)
141{
142 s_ledger_account_ctx *context = context_ptr;
143
145}
146
154static bool handle_chain_id(const tlv_data_t *data, void *context_ptr)
155{
156 s_ledger_account_ctx *context = context_ptr;
157
159}
160
168static bool handle_blockchain_family(const tlv_data_t *data, void *context_ptr)
169{
170 s_ledger_account_ctx *context = context_ptr;
171
173}
174
175DEFINE_TLV_PARSER(LEDGER_ACCOUNT_TAGS, NULL, ledger_account_tlv_parser)
176
177
183static bool verify_fields(const s_ledger_account_ctx *context)
184{
185 bool result = TLV_CHECK_RECEIVED_TAGS(context->received_tags,
186 TAG_STRUCTURE_TYPE,
187 TAG_STRUCTURE_VERSION,
188 TAG_CONTACT_NAME,
189 TAG_DERIVATION_PATH,
190 TAG_BLOCKCHAIN_FAMILY);
191 if (!result) {
192 PRINTF("Missing mandatory fields in Ledger Account descriptor!\n");
193 return false;
194 }
195 if (context->ledger_account->blockchain_family == FAMILY_ETHEREUM) {
196 result = TLV_CHECK_RECEIVED_TAGS(context->received_tags, TAG_CHAIN_ID);
197 if (!result) {
198 PRINTF("Missing CHAIN_ID for Ethereum family in Ledger Account descriptor!\n");
199 return false;
200 }
201 }
202 return true;
203}
204
211static void print_payload(const s_ledger_account_ctx *context)
212{
213 UNUSED(context);
214 char out[50] = {0};
215
216 PRINTF("****************************************************************************\n");
217 PRINTF("[Ledger Account] - Retrieved Descriptor:\n");
218 PRINTF("[Ledger Account] - Account name: %s\n",
219 context->ledger_account->account_name);
220 if (bip32_path_format_simple(&context->ledger_account->bip32_path, out, sizeof(out))) {
221 PRINTF("[Ledger Account] - Derivation path[%d]: %s\n",
223 out);
224 }
225 else {
226 PRINTF("[Ledger Account] - Derivation path length[%d] (failed to format)\n",
228 }
229 PRINTF("[Ledger Account] - Blockchain family: %s\n",
232 PRINTF("[Ledger Account] - Chain ID: %llu\n",
233 context->ledger_account->chain_id);
234 }
235}
236
245static bool build_and_send_response(void)
246{
247 uint8_t hmac_proof[CX_SHA256_SIZE] = {0};
248
250 (const char *) g_ab_payload.ledger_account.account_name,
251 &g_ab_payload.ledger_account.bip32_path,
252 g_ab_payload.ledger_account.blockchain_family,
253 g_ab_payload.ledger_account.chain_id,
254 hmac_proof)) {
255 PRINTF("[Ledger Account] Error: Failed to compute HMAC proof\n");
256 return false;
257 }
258
260 explicit_bzero(hmac_proof, sizeof(hmac_proof));
261 return ok;
262}
263
269static void review_choice(bool confirm)
270{
271 if (confirm) {
272 bool ok = build_and_send_response();
273 if (!ok) {
274 PRINTF("[Ledger Account] Error: Failed to build and send response\n");
275 }
277 ok, "Name confirmed", "Error saving account", finalize_ui_ledger_account);
278 }
279 else {
281 }
282}
283
284/* Exported functions --------------------------------------------------------*/
285
293bolos_err_t register_ledger_account(uint8_t *buffer_in, size_t buffer_in_length)
294{
295 const buffer_t payload = {.ptr = buffer_in, .size = buffer_in_length};
296 s_ledger_account_ctx ctx = {0};
297
298 // Init the structure
299 ctx.ledger_account = &g_ab_payload.ledger_account;
300 memset(&g_ab_payload.ledger_account, 0, sizeof(g_ab_payload.ledger_account));
301
302 // Parse using SDK TLV parser
303 if (!ledger_account_tlv_parser(&payload, &ctx, &ctx.received_tags)) {
304 PRINTF("[Ledger Account] Failed to parse TLV payload\n");
305 return SWO_INCORRECT_DATA;
306 }
307 if (!verify_fields(&ctx)) {
308 return SWO_INCORRECT_DATA;
309 }
310 print_payload(&ctx);
311
312 // Check the account validity according to the Coin application logic
314 PRINTF("[Ledger Account] Error: Account rejected by coin application\n");
315 return SWO_WRONG_PARAMETER_VALUE;
316 }
317
318 // Display confirmation UI (delegated to coin app)
320 return SWO_NO_RESPONSE;
321}
322
323#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 handle_check_register_ledger_account(ledger_account_t *params)
Handle called to validate the received Ledger Account.
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_REGISTER_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 verify_fields(const s_ledger_account_ctx *context)
Check that all mandatory TLV tags were received.
static void review_choice(bool confirm)
NBGL callback invoked after the user confirms or rejects the registration.
static bool handle_struct_type(const tlv_data_t *data, void *context_ptr)
Handler for tag STRUCTURE_TYPE.
bolos_err_t register_ledger_account(uint8_t *buffer_in, size_t buffer_in_length)
Register a new Ledger Account.
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.
static void print_payload(const s_ledger_account_ctx *context)
Print the received descriptor.
#define LEDGER_ACCOUNT_TAGS(X)
API of the Advanced BOLOS Graphical Library, for typical application use-cases.
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
ledger_account_t * ledger_account
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
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,...)