43#include "status_words.h"
53#if defined(HAVE_ADDRESS_BOOK)
56#define STRUCT_VERSION 0x01
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(0x23, TAG_CHAIN_ID, handle_chain_id, ENFORCE_UNIQUE_TAG) \
76 X(0x51, TAG_BLOCKCHAIN_FAMILY, handle_blockchain_family, ENFORCE_UNIQUE_TAG) \
77 X(0x29, TAG_HMAC_PROOF, handle_hmac_proof, ENFORCE_UNIQUE_TAG) \
78 X(0xf7, TAG_HMAC_REST, handle_hmac_rest, ENFORCE_UNIQUE_TAG)
91static bool handle_struct_type(
const tlv_data_t *data, s_provide_contact_ctx *context)
95 PRINTF(
"[Provide Contact] Invalid STRUCTURE_TYPE value\n");
108static bool handle_struct_version(
const tlv_data_t *data, s_provide_contact_ctx *context)
112 PRINTF(
"[Provide Contact] Invalid STRUCTURE_VERSION value\n");
125static bool handle_contact_name(
const tlv_data_t *data, s_provide_contact_ctx *context)
127 if (!address_book_handle_printable_string(
128 data, context->identity->contact_name,
sizeof(context->identity->contact_name))) {
129 PRINTF(
"[Provide Contact] CONTACT_NAME: failed to parse\n");
142static bool handle_scope(
const tlv_data_t *data, s_provide_contact_ctx *context)
144 if (!address_book_handle_printable_string(
145 data, context->identity->scope,
sizeof(context->identity->scope))) {
146 PRINTF(
"[Provide Contact] SCOPE: failed to parse\n");
159static bool handle_identifier(
const tlv_data_t *data, s_provide_contact_ctx *context)
163 PRINTF(
"[Provide Contact] IDENTIFIER: failed to extract\n");
166 memmove(context->identity->identifier, buf.
ptr, buf.
size);
167 context->identity->identifier_len = (uint8_t) buf.
size;
178static bool handle_group_handle(
const tlv_data_t *data, s_provide_contact_ctx *context)
182 PRINTF(
"[Provide Contact] GROUP_HANDLE: invalid length (expected %d bytes)\n",
186 memmove(context->group_handle, buf.
ptr, GROUP_HANDLE_SIZE);
199 return address_book_handle_chain_id(data, &context->identity->chain_id);
211 return address_book_handle_blockchain_family(data, &context->identity->blockchain_family);
221static bool handle_hmac_proof(
const tlv_data_t *data, s_provide_contact_ctx *context)
225 PRINTF(
"[Provide Contact] HMAC_PROOF: invalid length (expected %d bytes)\n",
229 memmove(context->hmac_proof, buf.
ptr, CX_SHA256_SIZE);
240static bool handle_hmac_rest(
const tlv_data_t *data, s_provide_contact_ctx *context)
244 PRINTF(
"[Provide Contact] HMAC_REST: invalid length (expected %d bytes)\n", CX_SHA256_SIZE);
247 memmove(context->hmac_rest, buf.
ptr, CX_SHA256_SIZE);
259static bool verify_fields(
const s_provide_contact_ctx *context)
263 TAG_STRUCTURE_VERSION,
266 TAG_ACCOUNT_IDENTIFIER,
268 TAG_BLOCKCHAIN_FAMILY,
272 PRINTF(
"[Provide Contact] Missing mandatory fields!\n");
278 PRINTF(
"[Provide Contact] Missing CHAIN_ID for Ethereum family!\n");
291static void print_payload(
const s_provide_contact_ctx *context)
294 PRINTF(
"****************************************************************************\n");
295 PRINTF(
"[Provide Contact] - Retrieved Descriptor:\n");
296 PRINTF(
"[Provide Contact] - Contact name: %s\n", context->identity->contact_name);
297 if (context->identity->scope[0] !=
'\0') {
298 PRINTF(
"[Provide Contact] - Scope: %s\n", context->identity->scope);
300 PRINTF(
"[Provide Contact] - Identifier len: %d\n", context->identity->identifier_len);
301 PRINTF(
"[Provide Contact] - Blockchain: %s\n",
317bolos_err_t provide_contact(uint8_t *buffer_in,
size_t buffer_in_length)
319 const buffer_t payload = {.
ptr = buffer_in, .size = buffer_in_length};
320 s_provide_contact_ctx ctx = {0};
323 ctx.identity = &g_ab_payload.provide_contact;
324 memset(&g_ab_payload.provide_contact, 0,
sizeof(g_ab_payload.provide_contact));
327 if (!provide_contact_tlv_parser(&payload, &ctx, &ctx.received_tags)) {
328 PRINTF(
"[Provide Contact] TLV parsing failed\n");
329 return SWO_INCORRECT_DATA;
331 if (!verify_fields(&ctx)) {
332 return SWO_INCORRECT_DATA;
337 if (!address_book_verify_group_handle(ctx.group_handle, g_ab_payload.provide_contact.gid)) {
338 PRINTF(
"[Provide Contact] Group handle verification failed\n");
339 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
343 if (!address_book_verify_hmac_proof(g_ab_payload.provide_contact.gid,
344 g_ab_payload.provide_contact.contact_name,
346 PRINTF(
"[Provide Contact] HMAC_PROOF verification failed\n");
347 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
351 if (!address_book_verify_hmac_rest(g_ab_payload.provide_contact.gid,
352 g_ab_payload.provide_contact.scope,
353 g_ab_payload.provide_contact.identifier,
354 g_ab_payload.provide_contact.identifier_len,
355 g_ab_payload.provide_contact.blockchain_family,
356 g_ab_payload.provide_contact.chain_id,
358 PRINTF(
"[Provide Contact] HMAC_REST verification failed\n");
359 return SWO_SECURITY_CONDITION_NOT_SATISFIED;
363 if (!handle_provide_identity(&g_ab_payload.provide_contact)) {
364 PRINTF(
"[Provide Contact] Rejected by coin application\n");
365 return SWO_WRONG_PARAMETER_VALUE;
Register / Edit Contact Name / Edit Scope / Edit Identifier.
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)