Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
identity_edit_scope.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
38
39/* Includes ------------------------------------------------------------------*/
40#include <string.h>
41#include "os_apdu.h"
42#include "status_words.h"
43#include "tlv_library.h"
44#include "buffer.h"
45#include "bip32.h"
46#include "address_book.h"
48#include "address_book_crypto.h"
49#include "address_book_common.h"
50#include "identity.h"
51#include "io.h"
52#include "nbgl_use_case.h"
53
54#if defined(HAVE_ADDRESS_BOOK)
55
56/* Private defines------------------------------------------------------------*/
57#define STRUCT_VERSION 0x01
58
59/* Private types, structures, unions -----------------------------------------*/
60typedef struct {
63 uint8_t hmac_proof[CX_SHA256_SIZE];
64 uint8_t hmac_rest[CX_SHA256_SIZE];
67
68/* Private macros-------------------------------------------------------------*/
69#define EDIT_SCOPE_TAGS(X) \
70 X(0x01, TAG_STRUCTURE_TYPE, handle_struct_type, ENFORCE_UNIQUE_TAG) \
71 X(0x02, TAG_STRUCTURE_VERSION, handle_struct_version, ENFORCE_UNIQUE_TAG) \
72 X(0xf0, TAG_CONTACT_NAME, handle_contact_name, ENFORCE_UNIQUE_TAG) \
73 X(0xf1, TAG_SCOPE, handle_scope, ENFORCE_UNIQUE_TAG) \
74 X(0xf2, TAG_ACCOUNT_IDENTIFIER, handle_identifier, ENFORCE_UNIQUE_TAG) \
75 X(0xf5, TAG_PREVIOUS_SCOPE, handle_previous_scope, ENFORCE_UNIQUE_TAG) \
76 X(0xf6, TAG_GROUP_HANDLE, handle_group_handle, ENFORCE_UNIQUE_TAG) \
77 X(0x23, TAG_CHAIN_ID, handle_chain_id, ENFORCE_UNIQUE_TAG) \
78 X(0x29, TAG_HMAC_PROOF, handle_hmac_proof, ENFORCE_UNIQUE_TAG) \
79 X(0xf7, TAG_HMAC_REST, handle_hmac_rest, ENFORCE_UNIQUE_TAG) \
80 X(0x51, TAG_BLOCKCHAIN_FAMILY, handle_blockchain_family, ENFORCE_UNIQUE_TAG)
81
82/* Private variables ---------------------------------------------------------*/
83
84/* Private functions ---------------------------------------------------------*/
85
93static bool handle_struct_type(const tlv_data_t *data, void *context_ptr)
94{
95 s_edit_scope_ctx *context = context_ptr;
96
97 UNUSED(context);
99 PRINTF("[Edit Scope] Invalid STRUCTURE_TYPE value\n");
100 return false;
101 }
102 return true;
103}
104
112static bool handle_struct_version(const tlv_data_t *data, void *context_ptr)
113{
114 s_edit_scope_ctx *context = context_ptr;
115
116 UNUSED(context);
118 PRINTF("[Edit Scope] Invalid STRUCTURE_VERSION value\n");
119 return false;
120 }
121 return true;
122}
123
131static bool handle_contact_name(const tlv_data_t *data, void *context_ptr)
132{
133 s_edit_scope_ctx *context = context_ptr;
134
136 context->edit->identity.contact_name,
137 sizeof(context->edit->identity.contact_name))) {
138 PRINTF("[Edit Scope] CONTACT_NAME: failed to parse\n");
139 return false;
140 }
141 return true;
142}
143
153static bool handle_scope(const tlv_data_t *data, void *context_ptr)
154{
155 s_edit_scope_ctx *context = context_ptr;
156
158 data, context->edit->identity.scope, sizeof(context->edit->identity.scope))) {
159 PRINTF("[Edit Scope] SCOPE: failed to parse\n");
160 return false;
161 }
162 return true;
163}
164
172static bool handle_identifier(const tlv_data_t *data, void *context_ptr)
173{
174 s_edit_scope_ctx *context = context_ptr;
175
176 buffer_t buf = {0};
177 if (!get_buffer_from_tlv_data(data, &buf, 1, IDENTIFIER_MAX_LENGTH)) {
178 PRINTF("[Edit Scope] IDENTIFIER: failed to extract\n");
179 return false;
180 }
181 memmove(context->edit->identity.identifier, buf.ptr, buf.size);
182 context->edit->identity.identifier_len = (uint8_t) buf.size;
183 return true;
184}
185
195static bool handle_previous_scope(const tlv_data_t *data, void *context_ptr)
196{
197 s_edit_scope_ctx *context = context_ptr;
198
200 data, context->edit->old_scope, sizeof(context->edit->old_scope))) {
201 PRINTF("[Edit Scope] PREVIOUS_SCOPE: failed to parse\n");
202 return false;
203 }
204 return true;
205}
206
214static bool handle_group_handle(const tlv_data_t *data, void *context_ptr)
215{
216 s_edit_scope_ctx *context = context_ptr;
217
218 buffer_t buf = {0};
220 PRINTF("[Edit Scope] GROUP_HANDLE: invalid length (expected %d bytes)\n",
222 return false;
223 }
224 memmove(context->group_handle, buf.ptr, GROUP_HANDLE_SIZE);
225 return true;
226}
227
235static bool handle_chain_id(const tlv_data_t *data, void *context_ptr)
236{
237 s_edit_scope_ctx *context = context_ptr;
238
239 return address_book_handle_chain_id(data, &context->edit->identity.chain_id);
240}
241
249static bool handle_blockchain_family(const tlv_data_t *data, void *context_ptr)
250{
251 s_edit_scope_ctx *context = context_ptr;
252
254}
255
263static bool handle_hmac_proof(const tlv_data_t *data, void *context_ptr)
264{
265 s_edit_scope_ctx *context = context_ptr;
266
267 buffer_t buf = {0};
268 if (!get_buffer_from_tlv_data(data, &buf, CX_SHA256_SIZE, CX_SHA256_SIZE)) {
269 PRINTF("[Edit Scope] HMAC_PROOF: invalid length (expected %d bytes)\n", CX_SHA256_SIZE);
270 return false;
271 }
272 memmove(context->hmac_proof, buf.ptr, CX_SHA256_SIZE);
273 return true;
274}
275
283static bool handle_hmac_rest(const tlv_data_t *data, void *context_ptr)
284{
285 s_edit_scope_ctx *context = context_ptr;
286
287 buffer_t buf = {0};
288 if (!get_buffer_from_tlv_data(data, &buf, CX_SHA256_SIZE, CX_SHA256_SIZE)) {
289 PRINTF("[Edit Scope] HMAC_REST: invalid length (expected %d bytes)\n", CX_SHA256_SIZE);
290 return false;
291 }
292 memmove(context->hmac_rest, buf.ptr, CX_SHA256_SIZE);
293 return true;
294}
295
296DEFINE_TLV_PARSER(EDIT_SCOPE_TAGS, NULL, edit_scope_tlv_parser)
297
298
304static bool verify_fields(const s_edit_scope_ctx *context)
305{
306 bool result = TLV_CHECK_RECEIVED_TAGS(context->received_tags,
307 TAG_STRUCTURE_TYPE,
308 TAG_STRUCTURE_VERSION,
309 TAG_CONTACT_NAME,
310 TAG_SCOPE,
311 TAG_ACCOUNT_IDENTIFIER,
312 TAG_PREVIOUS_SCOPE,
313 TAG_GROUP_HANDLE,
314 TAG_BLOCKCHAIN_FAMILY,
315 TAG_HMAC_PROOF,
316 TAG_HMAC_REST);
317 if (!result) {
318 PRINTF("[Edit Scope] Missing mandatory fields!\n");
319 return false;
320 }
321 if (context->edit->identity.blockchain_family == FAMILY_ETHEREUM) {
322 result = TLV_CHECK_RECEIVED_TAGS(context->received_tags, TAG_CHAIN_ID);
323 if (!result) {
324 PRINTF("[Edit Scope] Missing CHAIN_ID for Ethereum!\n");
325 return false;
326 }
327 }
328 return true;
329}
330
337static void print_payload(const s_edit_scope_ctx *context)
338{
339 UNUSED(context);
340 PRINTF("****************************************************************************\n");
341 PRINTF("[Edit Scope] - Retrieved Descriptor:\n");
342 PRINTF("[Edit Scope] - Contact name: %s\n", context->edit->identity.contact_name);
343 PRINTF("[Edit Scope] - Old scope: %s\n", context->edit->old_scope);
344 PRINTF("[Edit Scope] - New scope: %s\n", context->edit->identity.scope);
345 PRINTF("[Edit Scope] - Blockchain family: %s\n",
348 PRINTF("[Edit Scope] - Chain ID: %llu\n", context->edit->identity.chain_id);
349 }
350}
351
360static bool build_and_send_response(void)
361{
362 uint8_t hmac_rest[CX_SHA256_SIZE] = {0};
363
364 if (!address_book_compute_hmac_rest(g_ab_payload.edit_scope.identity.gid,
365 g_ab_payload.edit_scope.identity.scope,
366 g_ab_payload.edit_scope.identity.identifier,
367 g_ab_payload.edit_scope.identity.identifier_len,
368 g_ab_payload.edit_scope.identity.blockchain_family,
369 g_ab_payload.edit_scope.identity.chain_id,
370 hmac_rest)) {
371 PRINTF("[Edit Scope] Error: Failed to compute new HMAC_REST\n");
372 explicit_bzero(hmac_rest, sizeof(hmac_rest));
373 return false;
374 }
375 bool ok = address_book_send_hmac_proof(TYPE_EDIT_SCOPE, hmac_rest);
376 explicit_bzero(hmac_rest, sizeof(hmac_rest));
377 return ok;
378}
379
385static void review_choice(bool confirm)
386{
387 if (confirm) {
388 bool ok = build_and_send_response();
389 const char *successMsg
390 = (g_ab_payload.edit_scope.identity.blockchain_family == FAMILY_BITCOIN)
391 ? "Scope changed"
392 : "Address name changed";
393 if (ok) {
395 }
396 else {
397 PRINTF("[Edit Scope] Error: Failed to build and send HMAC proof\n");
398 }
399 address_book_finalize_review(ok, successMsg, "Error during update", finalize_ui_edit_scope);
400 }
401 else {
403 }
404}
405
409static void ui_display(void)
410{
411 uint8_t nbPairs = 0;
412 bool isBitcoin = (g_ab_payload.edit_scope.identity.blockchain_family == FAMILY_BITCOIN);
413
414 memset(&g_ab_ui.list, 0, sizeof(g_ab_ui.list));
415 memset(g_ab_ui.pairs, 0, sizeof(g_ab_ui.pairs));
416 g_ab_ui.pairs[nbPairs].item = "Contact name";
417 g_ab_ui.pairs[nbPairs].value = g_ab_payload.edit_scope.identity.contact_name;
418 nbPairs++;
419 g_ab_ui.pairs[nbPairs].item = isBitcoin ? "Old scope" : "Old address name";
420 g_ab_ui.pairs[nbPairs].value = g_ab_payload.edit_scope.old_scope;
421 nbPairs++;
422 g_ab_ui.pairs[nbPairs].item = isBitcoin ? "New scope" : "New address name";
423 g_ab_ui.pairs[nbPairs].value = g_ab_payload.edit_scope.identity.scope;
424 nbPairs++;
425 g_ab_ui.list.pairs = g_ab_ui.pairs;
426 g_ab_ui.list.nbPairs = nbPairs;
427 address_book_display_review(&LARGE_ADDRESS_BOOK_ICON,
428 "Review change to contact details",
429#ifdef SCREEN_SIZE_WALLET
430 "Confirm change?",
431#else
432 "Confirm change",
433#endif
435}
436
437/* Exported functions --------------------------------------------------------*/
438
446bolos_err_t edit_scope(uint8_t *buffer_in, size_t buffer_in_length)
447{
448 const buffer_t payload = {.ptr = buffer_in, .size = buffer_in_length};
449 s_edit_scope_ctx ctx = {0};
450
451 // Init the structure
452 ctx.edit = &g_ab_payload.edit_scope;
453 memset(&g_ab_payload.edit_scope, 0, sizeof(g_ab_payload.edit_scope));
454
455 // Parse using SDK TLV parser
456 if (!edit_scope_tlv_parser(&payload, &ctx, &ctx.received_tags)) {
457 PRINTF("[Edit Scope] TLV parsing failed\n");
458 return SWO_INCORRECT_DATA;
459 }
460 if (!verify_fields(&ctx)) {
461 return SWO_INCORRECT_DATA;
462 }
463 print_payload(&ctx);
464
465 // Verify the group handle and extract the gid
466 if (!address_book_verify_group_handle(ctx.group_handle, g_ab_payload.edit_scope.identity.gid)) {
467 PRINTF("[Edit Scope] Group handle verification failed\n");
468 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
469 }
470
471 // Verify that the wallet holds a valid HMAC_PROOF for the contact name
472 if (!address_book_verify_hmac_proof(g_ab_payload.edit_scope.identity.gid,
473 g_ab_payload.edit_scope.identity.contact_name,
474 ctx.hmac_proof)) {
475 PRINTF("[Edit Scope] HMAC_PROOF verification failed\n");
476 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
477 }
478
479 // Verify that the wallet holds a valid HMAC_REST for the previous scope
480 if (!address_book_verify_hmac_rest(g_ab_payload.edit_scope.identity.gid,
481 g_ab_payload.edit_scope.old_scope,
482 g_ab_payload.edit_scope.identity.identifier,
483 g_ab_payload.edit_scope.identity.identifier_len,
484 g_ab_payload.edit_scope.identity.blockchain_family,
485 g_ab_payload.edit_scope.identity.chain_id,
486 ctx.hmac_rest)) {
487 PRINTF("[Edit Scope] HMAC_REST verification failed\n");
488 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
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.
void finalize_ui_edit_scope(void)
Handle called to finalize the UI flow for editing a scope.
void on_edit_scope_applied(const edit_scope_t *edit)
Notification that a scope edit was successfully applied.
Register / Edit Contact Name / Edit Scope / Edit Identifier.
#define TYPE_EDIT_SCOPE
Definition identity.h:43
#define GROUP_HANDLE_SIZE
group_handle = gid(32) | MAC(K_group, gid)(32)
Definition identity.h:48
#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 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_previous_scope(const tlv_data_t *data, void *context_ptr)
Handler for tag PREVIOUS_SCOPE.
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 Scope 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.
#define EDIT_SCOPE_TAGS(X)
bolos_err_t edit_scope(uint8_t *buffer_in, size_t buffer_in_length)
Edit the SCOPE of an existing Identity contact.
static bool handle_group_handle(const tlv_data_t *data, void *context_ptr)
Handler for tag GROUP_HANDLE.
static void print_payload(const s_edit_scope_ctx *context)
Print the received descriptor.
static bool handle_hmac_rest(const tlv_data_t *data, void *context_ptr)
Handler for tag HMAC_REST.
static bool verify_fields(const s_edit_scope_ctx *context)
Check that all mandatory TLV tags were received.
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 Scope Name TLV payload.
Definition identity.h:96
char old_scope[SCOPE_LENGTH]
Scope being replaced.
Definition identity.h:98
identity_t identity
New identity (scope = new).
Definition identity.h:97
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).
uint8_t hmac_proof[CX_SHA256_SIZE]
HMAC_PROOF — verifies contact name.
TLV_reception_t received_tags
uint8_t hmac_rest[CX_SHA256_SIZE]
HMAC_REST — verifies old scope.
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,...)