42#include "status_words.h"
54#if defined(HAVE_ADDRESS_BOOK)
57#define STRUCT_VERSION 0x01
63 uint8_t hmac_proof[CX_SHA256_SIZE];
64 uint8_t hmac_rest[CX_SHA256_SIZE];
65 uint8_t group_handle[GROUP_HANDLE_SIZE];
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)
93static bool handle_struct_type(
const tlv_data_t *data, s_edit_scope_ctx *context)
97 PRINTF(
"[Edit Scope] Invalid STRUCTURE_TYPE value\n");
110static bool handle_struct_version(
const tlv_data_t *data, s_edit_scope_ctx *context)
114 PRINTF(
"[Edit Scope] Invalid STRUCTURE_VERSION value\n");
127static bool handle_contact_name(
const tlv_data_t *data, s_edit_scope_ctx *context)
129 if (!address_book_handle_printable_string(data,
130 context->edit->identity.contact_name,
131 sizeof(context->edit->identity.contact_name))) {
132 PRINTF(
"[Edit Scope] CONTACT_NAME: failed to parse\n");
147static bool handle_scope(
const tlv_data_t *data, s_edit_scope_ctx *context)
149 if (!address_book_handle_printable_string(
150 data, context->edit->identity.scope,
sizeof(context->edit->identity.scope))) {
151 PRINTF(
"[Edit Scope] SCOPE: failed to parse\n");
164static bool handle_identifier(
const tlv_data_t *data, s_edit_scope_ctx *context)
168 PRINTF(
"[Edit Scope] IDENTIFIER: failed to extract\n");
171 memmove(context->edit->identity.identifier, buf.
ptr, buf.
size);
172 context->edit->identity.identifier_len = (uint8_t) buf.
size;
185static bool handle_previous_scope(
const tlv_data_t *data, s_edit_scope_ctx *context)
187 if (!address_book_handle_printable_string(
188 data, context->edit->old_scope,
sizeof(context->edit->old_scope))) {
189 PRINTF(
"[Edit Scope] PREVIOUS_SCOPE: failed to parse\n");
202static bool handle_group_handle(
const tlv_data_t *data, s_edit_scope_ctx *context)
206 PRINTF(
"[Edit Scope] GROUP_HANDLE: invalid length (expected %d bytes)\n",
210 memmove(context->group_handle, buf.
ptr, GROUP_HANDLE_SIZE);
223 return address_book_handle_chain_id(data, &context->edit->identity.chain_id);
235 return address_book_handle_blockchain_family(data, &context->edit->identity.blockchain_family);
245static bool handle_hmac_proof(
const tlv_data_t *data, s_edit_scope_ctx *context)
249 PRINTF(
"[Edit Scope] HMAC_PROOF: invalid length (expected %d bytes)\n", CX_SHA256_SIZE);
252 memmove(context->hmac_proof, buf.
ptr, CX_SHA256_SIZE);
263static bool handle_hmac_rest(
const tlv_data_t *data, s_edit_scope_ctx *context)
267 PRINTF(
"[Edit Scope] HMAC_REST: invalid length (expected %d bytes)\n", CX_SHA256_SIZE);
270 memmove(context->hmac_rest, buf.
ptr, CX_SHA256_SIZE);
282static bool verify_fields(
const s_edit_scope_ctx *context)
286 TAG_STRUCTURE_VERSION,
289 TAG_ACCOUNT_IDENTIFIER,
292 TAG_BLOCKCHAIN_FAMILY,
296 PRINTF(
"[Edit Scope] Missing mandatory fields!\n");
302 PRINTF(
"[Edit Scope] Missing CHAIN_ID for Ethereum!\n");
315static void print_payload(
const s_edit_scope_ctx *context)
318 PRINTF(
"****************************************************************************\n");
319 PRINTF(
"[Edit Scope] - Retrieved Descriptor:\n");
320 PRINTF(
"[Edit Scope] - Contact name: %s\n", context->edit->identity.contact_name);
321 PRINTF(
"[Edit Scope] - Old scope: %s\n", context->edit->old_scope);
322 PRINTF(
"[Edit Scope] - New scope: %s\n", context->edit->identity.scope);
323 PRINTF(
"[Edit Scope] - Blockchain family: %s\n",
326 PRINTF(
"[Edit Scope] - Chain ID: %llu\n", context->edit->identity.chain_id);
338static bool build_and_send_response(
void)
340 uint8_t hmac_rest[CX_SHA256_SIZE] = {0};
342 if (!address_book_compute_hmac_rest(g_ab_payload.edit_scope.identity.gid,
343 g_ab_payload.edit_scope.identity.scope,
344 g_ab_payload.edit_scope.identity.identifier,
345 g_ab_payload.edit_scope.identity.identifier_len,
346 g_ab_payload.edit_scope.identity.blockchain_family,
347 g_ab_payload.edit_scope.identity.chain_id,
349 PRINTF(
"[Edit Scope] Error: Failed to compute new HMAC_REST\n");
350 explicit_bzero(hmac_rest,
sizeof(hmac_rest));
353 bool ok = address_book_send_hmac_proof(TYPE_EDIT_SCOPE, hmac_rest);
354 explicit_bzero(hmac_rest,
sizeof(hmac_rest));
363static void review_choice(
bool confirm)
366 bool ok = build_and_send_response();
367 const char *successMsg
368 = (g_ab_payload.edit_scope.identity.blockchain_family ==
FAMILY_BITCOIN)
370 :
"Address name changed";
372 on_edit_scope_applied(&g_ab_payload.edit_scope);
375 PRINTF(
"[Edit Scope] Error: Failed to build and send HMAC proof\n");
377 address_book_finalize_review(ok, successMsg,
"Error during update", finalize_ui_edit_scope);
380 address_book_handle_review_rejected(finalize_ui_edit_scope);
387static void ui_display(
void)
390 bool isBitcoin = (g_ab_payload.edit_scope.identity.blockchain_family ==
FAMILY_BITCOIN);
392 memset(&g_ab_ui.list, 0,
sizeof(g_ab_ui.list));
393 memset(g_ab_ui.pairs, 0,
sizeof(g_ab_ui.pairs));
394 g_ab_ui.pairs[nbPairs].item =
"Contact name";
395 g_ab_ui.pairs[nbPairs].value = g_ab_payload.edit_scope.identity.contact_name;
397 g_ab_ui.pairs[nbPairs].item = isBitcoin ?
"Old scope" :
"Old address name";
398 g_ab_ui.pairs[nbPairs].value = g_ab_payload.edit_scope.old_scope;
400 g_ab_ui.pairs[nbPairs].item = isBitcoin ?
"New scope" :
"New address name";
401 g_ab_ui.pairs[nbPairs].value = g_ab_payload.edit_scope.identity.scope;
403 g_ab_ui.list.pairs = g_ab_ui.pairs;
404 g_ab_ui.list.nbPairs = nbPairs;
406 "Review change to contact details",
407#ifdef SCREEN_SIZE_WALLET
424bolos_err_t edit_scope(uint8_t *buffer_in,
size_t buffer_in_length)
426 const buffer_t payload = {.
ptr = buffer_in, .size = buffer_in_length};
427 s_edit_scope_ctx ctx = {0};
430 ctx.edit = &g_ab_payload.edit_scope;
431 memset(&g_ab_payload.edit_scope, 0,
sizeof(g_ab_payload.edit_scope));
434 if (!edit_scope_tlv_parser(&payload, &ctx, &ctx.received_tags)) {
435 PRINTF(
"[Edit Scope] TLV parsing failed\n");
436 return SWO_INCORRECT_DATA;
438 if (!verify_fields(&ctx)) {
439 return SWO_INCORRECT_DATA;
444 if (!address_book_verify_group_handle(ctx.group_handle, g_ab_payload.edit_scope.identity.gid)) {
445 PRINTF(
"[Edit Scope] Group handle verification failed\n");
446 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
450 if (!address_book_verify_hmac_proof(g_ab_payload.edit_scope.identity.gid,
451 g_ab_payload.edit_scope.identity.contact_name,
453 PRINTF(
"[Edit Scope] HMAC_PROOF verification failed\n");
454 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
458 if (!address_book_verify_hmac_rest(g_ab_payload.edit_scope.identity.gid,
459 g_ab_payload.edit_scope.old_scope,
460 g_ab_payload.edit_scope.identity.identifier,
461 g_ab_payload.edit_scope.identity.identifier_len,
462 g_ab_payload.edit_scope.identity.blockchain_family,
463 g_ab_payload.edit_scope.identity.chain_id,
465 PRINTF(
"[Edit Scope] HMAC_REST verification failed\n");
466 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
471 return SWO_NO_RESPONSE;
Register / Edit Contact Name / Edit Scope / Edit Identifier.
#define LARGE_ADDRESS_BOOK_ICON
API of the Advanced BOLOS Graphical Library, for typical application use-cases.
size_t size
Pointer to byte buffer.
bool tlv_enforce_u8_value(const tlv_data_t *data, uint8_t expected_value)
Enforce that a TLV data contains the expected uint8 value.
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).
#define DEFINE_TLV_PARSER(TAG_LIST, COMMON_HANDLER, PARSE_FUNCTION_NAME)
Creates a parser function for a given TLV use case.
#define TLV_CHECK_RECEIVED_TAGS(received,...)
static bool handle_chain_id(const tlv_data_t *data, tlv_extracted_t *tlv_extracted)
static bool handle_blockchain_family(const tlv_data_t *data, tlv_extracted_t *tlv_extracted)