Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
identity_edit_identifier.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
39
40/* Includes ------------------------------------------------------------------*/
41#include <string.h>
42#include "os_apdu.h"
43#include "status_words.h"
44#include "tlv_library.h"
45#include "buffer.h"
46#include "bip32.h"
47#include "address_book.h"
49#include "address_book_crypto.h"
50#include "address_book_common.h"
51#include "identity.h"
52#include "io.h"
53#include "nbgl_use_case.h"
54
55#if defined(HAVE_ADDRESS_BOOK)
56
57/* Private defines------------------------------------------------------------*/
58#define STRUCT_VERSION 0x01
59
60/* Private types, structures, unions -----------------------------------------*/
61typedef struct {
64 uint8_t hmac_proof[CX_SHA256_SIZE];
65 uint8_t hmac_rest[CX_SHA256_SIZE];
68
69/* Private macros-------------------------------------------------------------*/
70#define EDIT_IDENTIFIER_TAGS(X) \
71 X(0x01, TAG_STRUCTURE_TYPE, handle_struct_type, ENFORCE_UNIQUE_TAG) \
72 X(0x02, TAG_STRUCTURE_VERSION, handle_struct_version, ENFORCE_UNIQUE_TAG) \
73 X(0xf0, TAG_CONTACT_NAME, handle_contact_name, ENFORCE_UNIQUE_TAG) \
74 X(0xf1, TAG_SCOPE, handle_scope, ENFORCE_UNIQUE_TAG) \
75 X(0xf2, TAG_ACCOUNT_IDENTIFIER, handle_identifier, ENFORCE_UNIQUE_TAG) \
76 X(0xf4, TAG_PREVIOUS_IDENTIFIER, handle_previous_identifier, ENFORCE_UNIQUE_TAG) \
77 X(0xf6, TAG_GROUP_HANDLE, handle_group_handle, ENFORCE_UNIQUE_TAG) \
78 X(0x23, TAG_CHAIN_ID, handle_chain_id, ENFORCE_UNIQUE_TAG) \
79 X(0x29, TAG_HMAC_PROOF, handle_hmac_proof, ENFORCE_UNIQUE_TAG) \
80 X(0xf7, TAG_HMAC_REST, handle_hmac_rest, ENFORCE_UNIQUE_TAG) \
81 X(0x51, TAG_BLOCKCHAIN_FAMILY, handle_blockchain_family, ENFORCE_UNIQUE_TAG)
82
83/* Private variables ---------------------------------------------------------*/
84
85/* Private functions ---------------------------------------------------------*/
86
94static bool handle_struct_type(const tlv_data_t *data, void *context_ptr)
95{
96 s_edit_ctx *context = context_ptr;
97
98 UNUSED(context);
100 PRINTF("[Edit Identifier] Invalid STRUCTURE_TYPE value\n");
101 return false;
102 }
103 return true;
104}
105
113static bool handle_struct_version(const tlv_data_t *data, void *context_ptr)
114{
115 s_edit_ctx *context = context_ptr;
116
117 UNUSED(context);
119 PRINTF("[Edit Identifier] Invalid STRUCTURE_VERSION value\n");
120 return false;
121 }
122 return true;
123}
124
132static bool handle_contact_name(const tlv_data_t *data, void *context_ptr)
133{
134 s_edit_ctx *context = context_ptr;
135
137 context->edit->identity.contact_name,
138 sizeof(context->edit->identity.contact_name))) {
139 PRINTF("[Edit Identifier] CONTACT_NAME: failed to parse\n");
140 return false;
141 }
142 return true;
143}
144
152static bool handle_scope(const tlv_data_t *data, void *context_ptr)
153{
154 s_edit_ctx *context = context_ptr;
155
157 data, context->edit->identity.scope, sizeof(context->edit->identity.scope))) {
158 PRINTF("[Edit Identifier] SCOPE: failed to parse\n");
159 return false;
160 }
161 return true;
162}
163
171static bool handle_identifier(const tlv_data_t *data, void *context_ptr)
172{
173 s_edit_ctx *context = context_ptr;
174
175 buffer_t buf = {0};
176 if (!get_buffer_from_tlv_data(data, &buf, 1, IDENTIFIER_MAX_LENGTH)) {
177 PRINTF("[Edit Identifier] IDENTIFIER: failed to extract\n");
178 return false;
179 }
180 memmove(context->edit->identity.identifier, buf.ptr, buf.size);
181 context->edit->identity.identifier_len = (uint8_t) buf.size;
182 return true;
183}
184
194static bool handle_previous_identifier(const tlv_data_t *data, void *context_ptr)
195{
196 s_edit_ctx *context = context_ptr;
197
198 buffer_t buf = {0};
199 if (!get_buffer_from_tlv_data(data, &buf, 1, IDENTIFIER_MAX_LENGTH)) {
200 PRINTF("[Edit Identifier] PREVIOUS_IDENTIFIER: failed to extract\n");
201 return false;
202 }
203 memmove(context->edit->old_identifier, buf.ptr, buf.size);
204 context->edit->old_identifier_len = (uint8_t) buf.size;
205 return true;
206}
207
215static bool handle_group_handle(const tlv_data_t *data, void *context_ptr)
216{
217 s_edit_ctx *context = context_ptr;
218
219 buffer_t buf = {0};
221 PRINTF("[Edit Identifier] GROUP_HANDLE: invalid length (expected %d bytes)\n",
223 return false;
224 }
225 memmove(context->group_handle, buf.ptr, GROUP_HANDLE_SIZE);
226 return true;
227}
228
236static bool handle_chain_id(const tlv_data_t *data, void *context_ptr)
237{
238 s_edit_ctx *context = context_ptr;
239
240 return address_book_handle_chain_id(data, &context->edit->identity.chain_id);
241}
242
250static bool handle_blockchain_family(const tlv_data_t *data, void *context_ptr)
251{
252 s_edit_ctx *context = context_ptr;
253
255}
256
264static bool handle_hmac_proof(const tlv_data_t *data, void *context_ptr)
265{
266 s_edit_ctx *context = context_ptr;
267
268 buffer_t buf = {0};
269 if (!get_buffer_from_tlv_data(data, &buf, CX_SHA256_SIZE, CX_SHA256_SIZE)) {
270 PRINTF("[Edit Identifier] HMAC_PROOF: invalid length (expected %d bytes)\n",
271 CX_SHA256_SIZE);
272 return false;
273 }
274 memmove(context->hmac_proof, buf.ptr, CX_SHA256_SIZE);
275 return true;
276}
277
285static bool handle_hmac_rest(const tlv_data_t *data, void *context_ptr)
286{
287 s_edit_ctx *context = context_ptr;
288
289 buffer_t buf = {0};
290 if (!get_buffer_from_tlv_data(data, &buf, CX_SHA256_SIZE, CX_SHA256_SIZE)) {
291 PRINTF("[Edit Identifier] HMAC_REST: invalid length (expected %d bytes)\n", CX_SHA256_SIZE);
292 return false;
293 }
294 memmove(context->hmac_rest, buf.ptr, CX_SHA256_SIZE);
295 return true;
296}
297
298DEFINE_TLV_PARSER(EDIT_IDENTIFIER_TAGS, NULL, edit_identifier_tlv_parser)
299
300
306static bool verify_fields(const s_edit_ctx *context)
307{
308 bool result = TLV_CHECK_RECEIVED_TAGS(context->received_tags,
309 TAG_STRUCTURE_TYPE,
310 TAG_STRUCTURE_VERSION,
311 TAG_CONTACT_NAME,
312 TAG_ACCOUNT_IDENTIFIER,
313 TAG_PREVIOUS_IDENTIFIER,
314 TAG_GROUP_HANDLE,
315 TAG_BLOCKCHAIN_FAMILY,
316 TAG_HMAC_PROOF,
317 TAG_HMAC_REST);
318 if (!result) {
319 PRINTF("[Edit Identifier] Missing mandatory fields!\n");
320 return false;
321 }
322 if (context->edit->identity.blockchain_family == FAMILY_ETHEREUM) {
323 result = TLV_CHECK_RECEIVED_TAGS(context->received_tags, TAG_CHAIN_ID);
324 if (!result) {
325 PRINTF("[Edit Identifier] Missing CHAIN_ID for Ethereum family!\n");
326 return false;
327 }
328 }
329 return true;
330}
331
338static void print_payload(const s_edit_ctx *context)
339{
340 UNUSED(context);
341 PRINTF("****************************************************************************\n");
342 PRINTF("[Edit Identifier] - Retrieved Descriptor:\n");
343 PRINTF("[Edit Identifier] - Contact name: %s\n", context->edit->identity.contact_name);
344 if (context->edit->identity.scope[0] != '\0') {
345 PRINTF("[Edit Identifier] - Scope: %s\n", context->edit->identity.scope);
346 }
347 PRINTF("[Edit Identifier] - New identifier len: %d\n",
348 context->edit->identity.identifier_len);
349 PRINTF("[Edit Identifier] - Old identifier len: %d\n", context->edit->old_identifier_len);
350 PRINTF("[Edit Identifier] - Blockchain family: %s\n",
352}
353
362static bool build_and_send_response(void)
363{
364 uint8_t hmac_rest[CX_SHA256_SIZE] = {0};
365
366 if (!address_book_compute_hmac_rest(g_ab_payload.edit_identifier.identity.gid,
367 g_ab_payload.edit_identifier.identity.scope,
368 g_ab_payload.edit_identifier.identity.identifier,
369 g_ab_payload.edit_identifier.identity.identifier_len,
370 g_ab_payload.edit_identifier.identity.blockchain_family,
371 g_ab_payload.edit_identifier.identity.chain_id,
372 hmac_rest)) {
373 PRINTF("[Edit Identifier] Error: Failed to compute new HMAC_REST\n");
374 explicit_bzero(hmac_rest, sizeof(hmac_rest));
375 return false;
376 }
378 explicit_bzero(hmac_rest, sizeof(hmac_rest));
379 return ok;
380}
381
387static void review_choice(bool confirm)
388{
389 if (confirm) {
390 bool ok = build_and_send_response();
391 const char *successMsg
392 = (g_ab_payload.edit_identifier.identity.blockchain_family == FAMILY_BITCOIN)
393 ? "Identifier changed"
394 : "Address changed";
395 if (ok) {
397 }
398 else {
399 PRINTF("[Edit Identifier] Error: Failed to build and send HMAC proof\n");
400 }
402 ok, successMsg, "Error during update", finalize_ui_edit_identifier);
403 }
404 else {
406 }
407}
408
412static void ui_display(void)
413{
414 memset(&g_ab_ui.list, 0, sizeof(g_ab_ui.list));
415 g_ab_ui.list.nbPairs = 4; // name + scope + old identifier + new identifier
416 if (g_ab_payload.edit_identifier.identity.blockchain_family == FAMILY_ETHEREUM) {
417 g_ab_ui.list.nbPairs++; // network
418 }
421 "Review change to contact details",
422#ifdef SCREEN_SIZE_WALLET
423 "Confirm change?",
424#else
425 "Confirm change",
426#endif
428}
429
430/* Exported functions --------------------------------------------------------*/
431
439bolos_err_t edit_identifier(uint8_t *buffer_in, size_t buffer_in_length)
440{
441 const buffer_t payload = {.ptr = buffer_in, .size = buffer_in_length};
442 s_edit_ctx ctx = {0};
443
444 // Init the structure
445 ctx.edit = &g_ab_payload.edit_identifier;
446 memset(&g_ab_payload.edit_identifier, 0, sizeof(g_ab_payload.edit_identifier));
447
448 // Parse using SDK TLV parser
449 if (!edit_identifier_tlv_parser(&payload, &ctx, &ctx.received_tags)) {
450 PRINTF("[Edit Identifier] TLV parsing failed\n");
451 return SWO_INCORRECT_DATA;
452 }
453 if (!verify_fields(&ctx)) {
454 return SWO_INCORRECT_DATA;
455 }
456 print_payload(&ctx);
457
458 // Verify the group handle and extract the gid
460 g_ab_payload.edit_identifier.identity.gid)) {
461 PRINTF("[Edit Identifier] Group handle verification failed\n");
462 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
463 }
464
465 // Verify that the wallet holds a valid HMAC_PROOF for the contact name
466 if (!address_book_verify_hmac_proof(g_ab_payload.edit_identifier.identity.gid,
467 g_ab_payload.edit_identifier.identity.contact_name,
468 ctx.hmac_proof)) {
469 PRINTF("[Edit Identifier] HMAC_PROOF verification failed\n");
470 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
471 }
472
473 // Verify that the wallet holds a valid HMAC_REST for the previous identifier
474 if (!address_book_verify_hmac_rest(g_ab_payload.edit_identifier.identity.gid,
475 g_ab_payload.edit_identifier.identity.scope,
476 g_ab_payload.edit_identifier.old_identifier,
477 g_ab_payload.edit_identifier.old_identifier_len,
478 g_ab_payload.edit_identifier.identity.blockchain_family,
479 g_ab_payload.edit_identifier.identity.chain_id,
480 ctx.hmac_rest)) {
481 PRINTF("[Edit Identifier] HMAC_REST verification failed\n");
482 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
483 }
484
485 // Notify the coin app so it can validate and store display data
486 if (!handle_check_edit_identifier(&g_ab_payload.edit_identifier)) {
487 PRINTF("[Edit Identifier] Rejected by coin application\n");
488 return SWO_WRONG_PARAMETER_VALUE;
489 }
490
491 // Display confirmation UI
492 ui_display();
493 return SWO_NO_RESPONSE;
494}
495
496#endif // HAVE_ADDRESS_BOOK
@ FAMILY_BITCOIN
@ FAMILY_ETHEREUM
#define FAMILY_AS_STR(x)
ab_ui_t g_ab_ui
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.
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.
void address_book_display_review(const nbgl_icon_details_t *icon, const char *reviewTitle, const char *confirmText, nbgl_choiceCallback_t choiceCallback)
Display a confirmation review for an Address Book operation.
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_rest(const uint8_t gid[GID_SIZE], const char *scope, const uint8_t *identifier, uint8_t identifier_len, blockchain_family_e family, uint64_t chain_id, uint8_t hmac_out[CX_SHA256_SIZE])
Compute HMAC_REST for an Identity contact.
bool address_book_verify_hmac_proof(const uint8_t gid[GID_SIZE], const char *name, const uint8_t hmac_expected[CX_SHA256_SIZE])
Verify HMAC_PROOF for an Identity contact.
bool address_book_verify_group_handle(const uint8_t group_handle[GROUP_HANDLE_SIZE], uint8_t gid_out[GID_SIZE])
Verify a group handle and extract the gid.
bool address_book_verify_hmac_rest(const uint8_t gid[GID_SIZE], const char *scope, const uint8_t *identifier, uint8_t identifier_len, blockchain_family_e family, uint64_t chain_id, const uint8_t hmac_expected[CX_SHA256_SIZE])
Verify HMAC_REST for an Identity contact.
bool handle_check_edit_identifier(const edit_identifier_t *params)
Handle called to validate and store the Edit Identifier parameters.
nbgl_contentTagValue_t * get_edit_identifier_tagValue(uint8_t pairIndex)
Callback to retrieve a tag-value pair for the Edit Identifier UI.
void on_edit_identifier_applied(const edit_identifier_t *edit)
Notification that an identifier edit was successfully applied.
void finalize_ui_edit_identifier(void)
Handle called to finalize the UI flow for editing an Identifier.
Register / Edit Contact Name / Edit Scope / Edit Identifier.
#define GROUP_HANDLE_SIZE
group_handle = gid(32) | MAC(K_group, gid)(32)
Definition identity.h:48
#define TYPE_EDIT_IDENTIFIER
Definition identity.h:42
#define IDENTIFIER_MAX_LENGTH
blockchain address or public key, raw bytes
Definition identity.h:52
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
static void print_payload(const s_edit_ctx *context)
Print the received descriptor.
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_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_struct_type(const tlv_data_t *data, void *context_ptr)
Handler for tag STRUCTURE_TYPE.
static bool handle_scope(const tlv_data_t *data, void *context_ptr)
Handler for tag SCOPE.
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 void ui_display(void)
Display the Edit Identifier review screen.
static bool handle_struct_version(const tlv_data_t *data, void *context_ptr)
Handler for tag STRUCTURE_VERSION.
static bool handle_identifier(const tlv_data_t *data, void *context_ptr)
Handler for tag ACCOUNT_IDENTIFIER.
static bool handle_previous_identifier(const tlv_data_t *data, void *context_ptr)
Handler for tag PREVIOUS_IDENTIFIER.
static bool handle_group_handle(const tlv_data_t *data, void *context_ptr)
Handler for tag GROUP_HANDLE.
static bool verify_fields(const s_edit_ctx *context)
Check that all mandatory TLV tags were received.
#define EDIT_IDENTIFIER_TAGS(X)
static bool handle_hmac_rest(const tlv_data_t *data, void *context_ptr)
Handler for tag HMAC_REST.
bolos_err_t edit_identifier(uint8_t *buffer_in, size_t buffer_in_length)
Edit the IDENTIFIER of an existing Identity contact.
#define LARGE_ADDRESS_BOOK_ICON
Definition nbgl_icons.h:228
API of the Advanced BOLOS Graphical Library, for typical application use-cases.
uint8_t * ptr
Definition buffer.h:22
size_t size
Pointer to byte buffer.
Definition buffer.h:23
Data extracted from an Edit Identifier TLV payload.
Definition identity.h:75
uint8_t old_identifier[IDENTIFIER_MAX_LENGTH]
Identifier being replaced.
Definition identity.h:77
identity_t identity
Updated identity (identifier = new).
Definition identity.h:76
uint8_t old_identifier_len
Length of the old identifier.
Definition identity.h:78
char scope[SCOPE_LENGTH]
Definition identity.h:65
blockchain_family_e blockchain_family
Definition identity.h:68
uint64_t chain_id
Mandatory when blockchain_family == FAMILY_ETHEREUM.
Definition identity.h:69
uint8_t identifier[IDENTIFIER_MAX_LENGTH]
Definition identity.h:66
uint8_t identifier_len
Definition identity.h:67
char contact_name[CONTACT_NAME_LENGTH]
Definition identity.h:64
uint8_t group_handle[GROUP_HANDLE_SIZE]
Group handle from wallet (verified later).
edit_identifier_t * edit
uint8_t hmac_rest[CX_SHA256_SIZE]
HMAC_REST — verifies old identifier.
uint8_t hmac_proof[CX_SHA256_SIZE]
HMAC_PROOF — verifies contact name.
TLV_reception_t received_tags
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,...)