43#include "status_words.h"
44#include "tlv_library.h"
53#if defined(HAVE_ADDRESS_BOOK)
56#define STRUCT_VERSION 0x01
61 TLV_reception_t received_tags;
62 uint8_t hmac_proof[CX_SHA256_SIZE];
63 uint8_t hmac_rest[CX_SHA256_SIZE];
64 uint8_t group_handle[GROUP_HANDLE_SIZE];
65} s_provide_contact_ctx;
68#define PROVIDE_CONTACT_TAGS(X) \
69 X(0x01, TAG_STRUCTURE_TYPE, handle_struct_type, ENFORCE_UNIQUE_TAG) \
70 X(0x02, TAG_STRUCTURE_VERSION, handle_struct_version, ENFORCE_UNIQUE_TAG) \
71 X(0xf0, TAG_CONTACT_NAME, handle_contact_name, ENFORCE_UNIQUE_TAG) \
72 X(0xf1, TAG_SCOPE, handle_scope, ENFORCE_UNIQUE_TAG) \
73 X(0xf2, TAG_ACCOUNT_IDENTIFIER, handle_identifier, ENFORCE_UNIQUE_TAG) \
74 X(0xf6, TAG_GROUP_HANDLE, handle_group_handle, ENFORCE_UNIQUE_TAG) \
75 X(0x21, TAG_DERIVATION_PATH, handle_derivation_path, ENFORCE_UNIQUE_TAG) \
76 X(0x23, TAG_CHAIN_ID, handle_chain_id, ENFORCE_UNIQUE_TAG) \
77 X(0x51, TAG_BLOCKCHAIN_FAMILY, handle_blockchain_family, 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)
92static bool handle_struct_type(
const tlv_data_t *data, s_provide_contact_ctx *context)
95 if (!tlv_enforce_u8_value(data, TYPE_PROVIDE_CONTACT)) {
96 PRINTF(
"[Provide Contact] Invalid STRUCTURE_TYPE value\n");
109static bool handle_struct_version(
const tlv_data_t *data, s_provide_contact_ctx *context)
112 if (!tlv_enforce_u8_value(data, STRUCT_VERSION)) {
113 PRINTF(
"[Provide Contact] Invalid STRUCTURE_VERSION value\n");
126static bool handle_contact_name(
const tlv_data_t *data, s_provide_contact_ctx *context)
128 if (!address_book_handle_printable_string(
129 data, context->identity->contact_name,
sizeof(context->identity->contact_name))) {
130 PRINTF(
"[Provide Contact] CONTACT_NAME: failed to parse\n");
143static bool handle_scope(
const tlv_data_t *data, s_provide_contact_ctx *context)
145 if (!address_book_handle_printable_string(
146 data, context->identity->scope,
sizeof(context->identity->scope))) {
147 PRINTF(
"[Provide Contact] SCOPE: failed to parse\n");
160static bool handle_identifier(
const tlv_data_t *data, s_provide_contact_ctx *context)
163 if (!get_buffer_from_tlv_data(data, &buf, 1, IDENTIFIER_MAX_LENGTH)) {
164 PRINTF(
"[Provide Contact] IDENTIFIER: failed to extract\n");
167 memmove(context->identity->identifier, buf.
ptr, buf.
size);
168 context->identity->identifier_len = (uint8_t) buf.
size;
179static bool handle_group_handle(
const tlv_data_t *data, s_provide_contact_ctx *context)
182 if (!get_buffer_from_tlv_data(data, &buf, GROUP_HANDLE_SIZE, GROUP_HANDLE_SIZE)) {
183 PRINTF(
"[Provide Contact] GROUP_HANDLE: invalid length (expected %d bytes)\n",
187 memmove(context->group_handle, buf.
ptr, GROUP_HANDLE_SIZE);
198static bool handle_derivation_path(
const tlv_data_t *data, s_provide_contact_ctx *context)
200 return address_book_handle_derivation_path(data, &context->identity->bip32_path);
210static bool handle_chain_id(
const tlv_data_t *data, s_provide_contact_ctx *context)
212 return address_book_handle_chain_id(data, &context->identity->chain_id);
222static bool handle_blockchain_family(
const tlv_data_t *data, s_provide_contact_ctx *context)
224 return address_book_handle_blockchain_family(data, &context->identity->blockchain_family);
234static bool handle_hmac_proof(
const tlv_data_t *data, s_provide_contact_ctx *context)
237 if (!get_buffer_from_tlv_data(data, &buf, CX_SHA256_SIZE, CX_SHA256_SIZE)) {
238 PRINTF(
"[Provide Contact] HMAC_PROOF: invalid length (expected %d bytes)\n",
242 memmove(context->hmac_proof, buf.
ptr, CX_SHA256_SIZE);
253static bool handle_hmac_rest(
const tlv_data_t *data, s_provide_contact_ctx *context)
256 if (!get_buffer_from_tlv_data(data, &buf, CX_SHA256_SIZE, CX_SHA256_SIZE)) {
257 PRINTF(
"[Provide Contact] HMAC_REST: invalid length (expected %d bytes)\n", CX_SHA256_SIZE);
260 memmove(context->hmac_rest, buf.
ptr, CX_SHA256_SIZE);
264DEFINE_TLV_PARSER(PROVIDE_CONTACT_TAGS, NULL, provide_contact_tlv_parser)
272static bool verify_fields(
const s_provide_contact_ctx *context)
274 bool result = TLV_CHECK_RECEIVED_TAGS(context->received_tags,
276 TAG_STRUCTURE_VERSION,
279 TAG_ACCOUNT_IDENTIFIER,
282 TAG_BLOCKCHAIN_FAMILY,
286 PRINTF(
"[Provide Contact] Missing mandatory fields!\n");
290 result = TLV_CHECK_RECEIVED_TAGS(context->received_tags, TAG_CHAIN_ID);
292 PRINTF(
"[Provide Contact] Missing CHAIN_ID for Ethereum family!\n");
305static void print_payload(
const s_provide_contact_ctx *context)
308 PRINTF(
"****************************************************************************\n");
309 PRINTF(
"[Provide Contact] - Retrieved Descriptor:\n");
310 PRINTF(
"[Provide Contact] - Contact name: %s\n", context->identity->contact_name);
311 if (context->identity->scope[0] !=
'\0') {
312 PRINTF(
"[Provide Contact] - Scope: %s\n", context->identity->scope);
314 PRINTF(
"[Provide Contact] - Identifier len: %d\n", context->identity->identifier_len);
315 PRINTF(
"[Provide Contact] - Blockchain: %s\n",
331bolos_err_t provide_contact(uint8_t *buffer_in,
size_t buffer_in_length)
333 const buffer_t payload = {.
ptr = buffer_in, .size = buffer_in_length};
334 s_provide_contact_ctx ctx = {0};
337 ctx.identity = &g_ab_payload.provide_contact;
338 memset(&g_ab_payload.provide_contact, 0,
sizeof(g_ab_payload.provide_contact));
341 if (!provide_contact_tlv_parser(&payload, &ctx, &ctx.received_tags)) {
342 PRINTF(
"[Provide Contact] TLV parsing failed\n");
343 return SWO_INCORRECT_DATA;
345 if (!verify_fields(&ctx)) {
346 return SWO_INCORRECT_DATA;
351 if (!address_book_verify_group_handle(&g_ab_payload.provide_contact.bip32_path,
353 g_ab_payload.provide_contact.gid)) {
354 PRINTF(
"[Provide Contact] Group handle verification failed\n");
355 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
359 if (!address_book_verify_hmac_proof(&g_ab_payload.provide_contact.bip32_path,
360 g_ab_payload.provide_contact.gid,
361 g_ab_payload.provide_contact.contact_name,
363 PRINTF(
"[Provide Contact] HMAC_PROOF verification failed\n");
364 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
368 if (!address_book_verify_hmac_rest(&g_ab_payload.provide_contact.bip32_path,
369 g_ab_payload.provide_contact.gid,
370 g_ab_payload.provide_contact.scope,
371 g_ab_payload.provide_contact.identifier,
372 g_ab_payload.provide_contact.identifier_len,
373 g_ab_payload.provide_contact.blockchain_family,
374 g_ab_payload.provide_contact.chain_id,
376 PRINTF(
"[Provide Contact] HMAC_REST verification failed\n");
377 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
381 if (!handle_provide_identity(&g_ab_payload.provide_contact)) {
382 PRINTF(
"[Provide Contact] Rejected by coin application\n");
383 return SWO_WRONG_PARAMETER_VALUE;
Register / Edit Contact Name / Edit Scope / Edit Identifier.
size_t size
Pointer to byte buffer.