Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
identity_edit_contact_name.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
34
35/* Includes ------------------------------------------------------------------*/
36#include <string.h>
37#include "os_utils.h"
38#include "os_apdu.h"
39#include "status_words.h"
40#include "tlv_library.h"
41#include "buffer.h"
42#include "bip32.h"
43#include "address_book.h"
45#include "address_book_crypto.h"
46#include "address_book_common.h"
47#include "identity.h"
48#include "io.h"
49#include "nbgl_use_case.h"
50
51#if defined(HAVE_ADDRESS_BOOK)
52
53/* Private defines------------------------------------------------------------*/
54#define STRUCT_VERSION 0x01
55
56/* Private types, structures, unions -----------------------------------------*/
57
64
65/* Private macros-------------------------------------------------------------*/
66#define EDIT_CONTACT_NAME_TAGS(X) \
67 X(0x01, TAG_STRUCTURE_TYPE, handle_struct_type, ENFORCE_UNIQUE_TAG) \
68 X(0x02, TAG_STRUCTURE_VERSION, handle_struct_version, ENFORCE_UNIQUE_TAG) \
69 X(0xf0, TAG_CONTACT_NAME, handle_contact_name, ENFORCE_UNIQUE_TAG) \
70 X(0xf3, TAG_PREVIOUS_NAME, handle_previous_name, ENFORCE_UNIQUE_TAG) \
71 X(0xf6, TAG_GROUP_HANDLE, handle_group_handle, ENFORCE_UNIQUE_TAG) \
72 X(0x29, TAG_HMAC_PROOF, handle_hmac_proof, ENFORCE_UNIQUE_TAG)
73
74/* Private variables ---------------------------------------------------------*/
75
76/* Private functions ---------------------------------------------------------*/
77
85static bool handle_struct_type(const tlv_data_t *data, void *context_ptr)
86{
87 s_edit_contact_name_ctx *context = context_ptr;
88
89 UNUSED(context);
91 PRINTF("[Edit Contact Name] Invalid STRUCTURE_TYPE value\n");
92 return false;
93 }
94 return true;
95}
96
104static bool handle_struct_version(const tlv_data_t *data, void *context_ptr)
105{
106 s_edit_contact_name_ctx *context = context_ptr;
107
108 UNUSED(context);
110 PRINTF("[Edit Contact Name] Invalid STRUCTURE_VERSION value\n");
111 return false;
112 }
113 return true;
114}
115
123static bool handle_contact_name(const tlv_data_t *data, void *context_ptr)
124{
125 s_edit_contact_name_ctx *context = context_ptr;
126
128 data, context->edit->contact_name, sizeof(context->edit->contact_name))) {
129 PRINTF("[Edit Contact Name] CONTACT_NAME: failed to parse\n");
130 return false;
131 }
132 return true;
133}
134
142static bool handle_previous_name(const tlv_data_t *data, void *context_ptr)
143{
144 s_edit_contact_name_ctx *context = context_ptr;
145
147 data, context->edit->old_contact_name, sizeof(context->edit->old_contact_name))) {
148 PRINTF("[Edit Contact Name] PREVIOUS_CONTACT_NAME: failed to parse\n");
149 return false;
150 }
151 return true;
152}
153
161static bool handle_group_handle(const tlv_data_t *data, void *context_ptr)
162{
163 s_edit_contact_name_ctx *context = context_ptr;
164
165 buffer_t buf = {0};
167 PRINTF("[Edit Contact Name] GROUP_HANDLE: invalid length (expected %d bytes)\n",
169 return false;
170 }
171 memmove(context->group_handle, buf.ptr, GROUP_HANDLE_SIZE);
172 return true;
173}
174
182static bool handle_hmac_proof(const tlv_data_t *data, void *context_ptr)
183{
184 s_edit_contact_name_ctx *context = context_ptr;
185
186 buffer_t buf = {0};
187 if (!get_buffer_from_tlv_data(data, &buf, CX_SHA256_SIZE, CX_SHA256_SIZE)) {
188 PRINTF("[Edit Contact Name] HMAC_PROOF: invalid length (expected %d bytes)\n",
189 CX_SHA256_SIZE);
190 return false;
191 }
192 memmove(context->hmac_proof, buf.ptr, CX_SHA256_SIZE);
193 return true;
194}
195
196DEFINE_TLV_PARSER(EDIT_CONTACT_NAME_TAGS, NULL, edit_contact_name_tlv_parser)
197
198
204static bool verify_fields(const s_edit_contact_name_ctx *context)
205{
206 bool result = TLV_CHECK_RECEIVED_TAGS(context->received_tags,
207 TAG_STRUCTURE_TYPE,
208 TAG_STRUCTURE_VERSION,
209 TAG_CONTACT_NAME,
210 TAG_PREVIOUS_NAME,
211 TAG_GROUP_HANDLE,
212 TAG_HMAC_PROOF);
213 if (!result) {
214 PRINTF("[Edit Contact Name] Missing mandatory fields!\n");
215 }
216 return result;
217}
218
225static void print_payload(const s_edit_contact_name_ctx *context)
226{
227 UNUSED(context);
228 PRINTF("****************************************************************************\n");
229 PRINTF("[Edit Contact Name] - Retrieved Descriptor:\n");
230 PRINTF("[Edit Contact Name] - Old name: %s\n", context->edit->old_contact_name);
231 PRINTF("[Edit Contact Name] - New name: %s\n", context->edit->contact_name);
232}
233
242static bool build_and_send_response(void)
243{
244 uint8_t hmac_proof[CX_SHA256_SIZE] = {0};
245
246 if (!address_book_compute_hmac_proof(g_ab_payload.edit_contact_name.gid,
247 g_ab_payload.edit_contact_name.contact_name,
248 hmac_proof)) {
249 PRINTF("[Edit Contact Name] Error: Failed to compute new HMAC_PROOF\n");
250 explicit_bzero(hmac_proof, sizeof(hmac_proof));
251 return false;
252 }
253
255 explicit_bzero(hmac_proof, sizeof(hmac_proof));
256 return ok;
257}
258
264static void review_choice(bool confirm)
265{
266 if (confirm) {
267 bool ok = build_and_send_response();
268 if (ok) {
269 on_edit_contact_name_applied(&g_ab_payload.edit_contact_name);
270 }
271 else {
272 PRINTF("[Edit Contact Name] Error: Failed to build and send HMAC proof\n");
273 }
275 ok, "Contact name changed", "Error during update", finalize_ui_edit_contact_name);
276 }
277 else {
279 }
280}
281
285static void ui_display(void)
286{
287 uint8_t nbPairs = 0;
288 memset(&g_ab_ui.list, 0, sizeof(g_ab_ui.list));
289 memset(g_ab_ui.pairs, 0, sizeof(g_ab_ui.pairs));
290 g_ab_ui.pairs[nbPairs].item = "Old name";
291 g_ab_ui.pairs[nbPairs].value = g_ab_payload.edit_contact_name.old_contact_name;
292 nbPairs++;
293 g_ab_ui.pairs[nbPairs].item = "New name";
294 g_ab_ui.pairs[nbPairs].value = g_ab_payload.edit_contact_name.contact_name;
295 nbPairs++;
296 g_ab_ui.list.pairs = g_ab_ui.pairs;
297 g_ab_ui.list.nbPairs = nbPairs;
298 address_book_display_review(&LARGE_ADDRESS_BOOK_ICON,
299 "Review change to contact details",
300#ifdef SCREEN_SIZE_WALLET
301 "Confirm change?",
302#else
303 "Confirm change",
304#endif
306}
307
308/* Exported functions --------------------------------------------------------*/
309
317bolos_err_t edit_contact_name(uint8_t *buffer_in, size_t buffer_in_length)
318{
319 const buffer_t payload = {.ptr = buffer_in, .size = buffer_in_length};
320 s_edit_contact_name_ctx ctx = {0};
321
322 // Init the structure
323 ctx.edit = &g_ab_payload.edit_contact_name;
324 memset(&g_ab_payload.edit_contact_name, 0, sizeof(g_ab_payload.edit_contact_name));
325
326 // Parse using SDK TLV parser
327 if (!edit_contact_name_tlv_parser(&payload, &ctx, &ctx.received_tags)) {
328 PRINTF("[Edit Contact Name] TLV parsing failed\n");
329 return SWO_INCORRECT_DATA;
330 }
331 if (!verify_fields(&ctx)) {
332 return SWO_INCORRECT_DATA;
333 }
334 print_payload(&ctx);
335
336 // Verify the group handle and extract the gid
337 if (!address_book_verify_group_handle(ctx.group_handle, g_ab_payload.edit_contact_name.gid)) {
338 PRINTF("[Edit Contact Name] Group handle verification failed\n");
339 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
340 }
341
342 // Verify the wallet holds a valid HMAC_PROOF for the previous name
343 if (!address_book_verify_hmac_proof(g_ab_payload.edit_contact_name.gid,
344 g_ab_payload.edit_contact_name.old_contact_name,
345 ctx.hmac_proof)) {
346 PRINTF("[Edit Contact Name] HMAC_PROOF verification failed\n");
347 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
348 }
349
350 // Display confirmation UI
351 ui_display();
352 return SWO_NO_RESPONSE;
353}
354
355#endif // HAVE_ADDRESS_BOOK
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.
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.
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_proof(const uint8_t gid[GID_SIZE], const char *name, uint8_t hmac_out[CX_SHA256_SIZE])
Compute HMAC_PROOF 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.
void finalize_ui_edit_contact_name(void)
Handle called to finalize the UI flow for editing a contact name.
void on_edit_contact_name_applied(const edit_contact_name_t *edit)
Notification that a contact name edit was successfully applied.
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_CONTACT_NAME
Definition identity.h:41
static bool handle_contact_name(const tlv_data_t *data, void *context_ptr)
Handler for tag CONTACT_NAME.
static bool handle_hmac_proof(const tlv_data_t *data, void *context_ptr)
Handler for tag HMAC_PROOF.
static void print_payload(const s_edit_contact_name_ctx *context)
Print the received descriptor.
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 verify_fields(const s_edit_contact_name_ctx *context)
Check that all mandatory TLV tags were received.
static bool build_and_send_response(void)
Build and send response.
static void ui_display(void)
Display the Edit Contact Name review screen.
static bool handle_struct_version(const tlv_data_t *data, void *context_ptr)
Handler for tag STRUCTURE_VERSION.
static bool handle_group_handle(const tlv_data_t *data, void *context_ptr)
Handler for tag GROUP_HANDLE.
#define EDIT_CONTACT_NAME_TAGS(X)
bolos_err_t edit_contact_name(uint8_t *buffer_in, size_t buffer_in_length)
Edit the CONTACT_NAME of an existing Identity contact.
#define STRUCT_VERSION
API of the Advanced BOLOS Graphical Library, for typical application use-cases.
uint8_t * ptr
Definition buffer.h:22
Data extracted from an Edit Contact Name TLV payload.
Definition identity.h:84
char contact_name[CONTACT_NAME_LENGTH]
New contact name.
Definition identity.h:86
char old_contact_name[CONTACT_NAME_LENGTH]
old contact name
Definition identity.h:87
uint8_t group_handle[GROUP_HANDLE_SIZE]
Group handle from wallet (verified later).
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,...)