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
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
58typedef struct {
59 edit_contact_name_t *edit;
60 TLV_reception_t received_tags;
61 uint8_t hmac_proof[CX_SHA256_SIZE];
62 uint8_t group_handle[GROUP_HANDLE_SIZE];
63} s_edit_contact_name_ctx;
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(0x21, TAG_DERIVATION_PATH, handle_derivation_path, ENFORCE_UNIQUE_TAG) \
73 X(0x29, TAG_HMAC_PROOF, handle_hmac_proof, ENFORCE_UNIQUE_TAG)
74
75/* Private variables ---------------------------------------------------------*/
76
77/* Private functions ---------------------------------------------------------*/
78
86static bool handle_struct_type(const tlv_data_t *data, s_edit_contact_name_ctx *context)
87{
88 UNUSED(context);
89 if (!tlv_enforce_u8_value(data, TYPE_EDIT_CONTACT_NAME)) {
90 PRINTF("[Edit Contact Name] Invalid STRUCTURE_TYPE value\n");
91 return false;
92 }
93 return true;
94}
95
103static bool handle_struct_version(const tlv_data_t *data, s_edit_contact_name_ctx *context)
104{
105 UNUSED(context);
106 if (!tlv_enforce_u8_value(data, STRUCT_VERSION)) {
107 PRINTF("[Edit Contact Name] Invalid STRUCTURE_VERSION value\n");
108 return false;
109 }
110 return true;
111}
112
120static bool handle_contact_name(const tlv_data_t *data, s_edit_contact_name_ctx *context)
121{
122 if (!address_book_handle_printable_string(
123 data, context->edit->contact_name, sizeof(context->edit->contact_name))) {
124 PRINTF("[Edit Contact Name] CONTACT_NAME: failed to parse\n");
125 return false;
126 }
127 return true;
128}
129
137static bool handle_previous_name(const tlv_data_t *data, s_edit_contact_name_ctx *context)
138{
139 if (!address_book_handle_printable_string(
140 data, context->edit->old_contact_name, sizeof(context->edit->old_contact_name))) {
141 PRINTF("[Edit Contact Name] PREVIOUS_CONTACT_NAME: failed to parse\n");
142 return false;
143 }
144 return true;
145}
146
154static bool handle_group_handle(const tlv_data_t *data, s_edit_contact_name_ctx *context)
155{
156 buffer_t buf = {0};
157 if (!get_buffer_from_tlv_data(data, &buf, GROUP_HANDLE_SIZE, GROUP_HANDLE_SIZE)) {
158 PRINTF("[Edit Contact Name] GROUP_HANDLE: invalid length (expected %d bytes)\n",
159 GROUP_HANDLE_SIZE);
160 return false;
161 }
162 memmove(context->group_handle, buf.ptr, GROUP_HANDLE_SIZE);
163 return true;
164}
165
173static bool handle_derivation_path(const tlv_data_t *data, s_edit_contact_name_ctx *context)
174{
175 return address_book_handle_derivation_path(data, &context->edit->bip32_path);
176}
177
185static bool handle_hmac_proof(const tlv_data_t *data, s_edit_contact_name_ctx *context)
186{
187 buffer_t buf = {0};
188 if (!get_buffer_from_tlv_data(data, &buf, CX_SHA256_SIZE, CX_SHA256_SIZE)) {
189 PRINTF("[Edit Contact Name] HMAC_PROOF: invalid length (expected %d bytes)\n",
190 CX_SHA256_SIZE);
191 return false;
192 }
193 memmove(context->hmac_proof, buf.ptr, CX_SHA256_SIZE);
194 return true;
195}
196
197DEFINE_TLV_PARSER(EDIT_CONTACT_NAME_TAGS, NULL, edit_contact_name_tlv_parser)
198
199
205static bool verify_fields(const s_edit_contact_name_ctx *context)
206{
207 bool result = TLV_CHECK_RECEIVED_TAGS(context->received_tags,
208 TAG_STRUCTURE_TYPE,
209 TAG_STRUCTURE_VERSION,
210 TAG_CONTACT_NAME,
211 TAG_PREVIOUS_NAME,
212 TAG_GROUP_HANDLE,
213 TAG_DERIVATION_PATH,
214 TAG_HMAC_PROOF);
215 if (!result) {
216 PRINTF("[Edit Contact Name] Missing mandatory fields!\n");
217 }
218 return result;
219}
220
227static void print_payload(const s_edit_contact_name_ctx *context)
228{
229 UNUSED(context);
230 PRINTF("****************************************************************************\n");
231 PRINTF("[Edit Contact Name] - Retrieved Descriptor:\n");
232 PRINTF("[Edit Contact Name] - Old name: %s\n", context->edit->old_contact_name);
233 PRINTF("[Edit Contact Name] - New name: %s\n", context->edit->contact_name);
234}
235
244static bool build_and_send_response(void)
245{
246 uint8_t hmac_proof[CX_SHA256_SIZE] = {0};
247
248 if (!address_book_compute_hmac_proof(&g_ab_payload.edit_contact_name.bip32_path,
249 g_ab_payload.edit_contact_name.gid,
250 g_ab_payload.edit_contact_name.contact_name,
251 hmac_proof)) {
252 PRINTF("[Edit Contact Name] Error: Failed to compute new HMAC_PROOF\n");
253 return false;
254 }
255
256 bool ok = address_book_send_hmac_proof(TYPE_EDIT_CONTACT_NAME, hmac_proof);
257 explicit_bzero(hmac_proof, sizeof(hmac_proof));
258 return ok;
259}
260
266static void review_choice(bool confirm)
267{
268 if (confirm) {
269 bool ok = build_and_send_response();
270 if (ok) {
271 on_edit_contact_name_applied(&g_ab_payload.edit_contact_name);
272 }
273 else {
274 PRINTF("[Edit Contact Name] Error: Failed to build and send HMAC proof\n");
275 }
276 address_book_finalize_review(
277 ok, "Contact name changed", "Error during update", finalize_ui_edit_contact_name);
278 }
279 else {
280 address_book_handle_review_rejected(finalize_ui_edit_contact_name);
281 }
282}
283
287static void ui_display(void)
288{
289 uint8_t nbPairs = 0;
290 memset(&g_ab_ui.list, 0, sizeof(g_ab_ui.list));
291 memset(g_ab_ui.pairs, 0, sizeof(g_ab_ui.pairs));
292 g_ab_ui.pairs[nbPairs].item = "Old name";
293 g_ab_ui.pairs[nbPairs].value = g_ab_payload.edit_contact_name.old_contact_name;
294 nbPairs++;
295 g_ab_ui.pairs[nbPairs].item = "New name";
296 g_ab_ui.pairs[nbPairs].value = g_ab_payload.edit_contact_name.contact_name;
297 nbPairs++;
298 g_ab_ui.list.pairs = g_ab_ui.pairs;
299 g_ab_ui.list.nbPairs = nbPairs;
300 address_book_display_review(&LARGE_ADDRESS_BOOK_ICON,
301 "Review change to contact details",
302#ifdef SCREEN_SIZE_WALLET
303 "Confirm change?",
304#else
305 "Confirm change",
306#endif
307 review_choice);
308}
309
310/* Exported functions --------------------------------------------------------*/
311
319bolos_err_t edit_contact_name(uint8_t *buffer_in, size_t buffer_in_length)
320{
321 const buffer_t payload = {.ptr = buffer_in, .size = buffer_in_length};
322 s_edit_contact_name_ctx ctx = {0};
323
324 // Init the structure
325 ctx.edit = &g_ab_payload.edit_contact_name;
326 memset(&g_ab_payload.edit_contact_name, 0, sizeof(g_ab_payload.edit_contact_name));
327
328 // Parse using SDK TLV parser
329 if (!edit_contact_name_tlv_parser(&payload, &ctx, &ctx.received_tags)) {
330 PRINTF("[Edit Contact Name] TLV parsing failed\n");
331 return SWO_INCORRECT_DATA;
332 }
333 if (!verify_fields(&ctx)) {
334 return SWO_INCORRECT_DATA;
335 }
336 print_payload(&ctx);
337
338 // Verify the group handle and extract the gid
339 if (!address_book_verify_group_handle(&g_ab_payload.edit_contact_name.bip32_path,
340 ctx.group_handle,
341 g_ab_payload.edit_contact_name.gid)) {
342 PRINTF("[Edit Contact Name] Group handle verification failed\n");
343 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
344 }
345
346 // Verify the wallet holds a valid HMAC_PROOF for the previous name
347 if (!address_book_verify_hmac_proof(&g_ab_payload.edit_contact_name.bip32_path,
348 g_ab_payload.edit_contact_name.gid,
349 g_ab_payload.edit_contact_name.old_contact_name,
350 ctx.hmac_proof)) {
351 PRINTF("[Edit Contact Name] HMAC_PROOF verification failed\n");
352 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
353 }
354
355 // Display confirmation UI
356 ui_display();
357 return SWO_NO_RESPONSE;
358}
359
360#endif // HAVE_ADDRESS_BOOK
Register / Edit Contact Name / Edit Scope / Edit Identifier.
#define LARGE_ADDRESS_BOOK_ICON
Definition nbgl_obj.h:277
API of the Advanced BOLOS Graphical Library, for typical application use-cases.
uint8_t * ptr
Definition buffer.h:22