Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/app_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ void reset_app_context(void) {
eth2WithdrawalIndex = 0;
#endif
memset((uint8_t *) &tmpCtx, 0, sizeof(tmpCtx));
memset((uint8_t *) &dataContext, 0, sizeof(dataContext));
forget_known_assets();
if (txContext.store_calldata) {
gcs_cleanup();
Expand Down
2 changes: 1 addition & 1 deletion src/features/get_eth2_public_key/cmd_get_eth2_public_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ uint16_t handle_get_eth2_public_key(uint8_t p1,
appState = APP_STATE_VERIFYING_ADDRESS;
ui_display_public_eth2();
// Return code will be sent after UI approve/cancel
error = 0;
error = SWO_NO_RESPONSE;
end:
// A cx_err_t does not fit a status word; the low half is what the client sees.
return (uint16_t) error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,5 @@ uint16_t handle_perform_privacy_operation(uint8_t p1,
explicit_bzero(privateKeyDataSwapped, sizeof(privateKeyDataSwapped));
explicit_bzero(&privateKey, sizeof(privateKey));
explicit_bzero(privateKeyData, sizeof(privateKeyData));
return error;
return (uint16_t) error;
}
19 changes: 12 additions & 7 deletions src/features/provide_network_info/cmd_network_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ uint16_t handle_network_info(uint8_t p1,
switch (p2) {
case P2_NETWORK_CONFIG:
if (!tlv_from_apdu(p1 == P1_FIRST_CHUNK, length, data, &handle_network_tlv_payload)) {
// If there was an error, free the allocated memory
network_info_cleanup(g_last_added_network);
if (g_last_added_network != NULL) {
network_info_cleanup(g_last_added_network);
}
sw = SWO_INCORRECT_DATA;
break;
}
Expand All @@ -78,24 +79,28 @@ uint16_t handle_network_info(uint8_t p1,
case P2_NETWORK_ICON:
sw = handle_network_icon_chunks(p1, &buf);
if (sw != SWO_SUCCESS) {
// If there was an error, free the allocated memory
network_info_cleanup(g_last_added_network);
if (g_last_added_network != NULL) {
network_info_cleanup(g_last_added_network);
}
}
break;

case P2_GET_INFO:
if (p1 != 0x00) {
PRINTF("Error: Unexpected P1 (%u)!\n", p1);
// If there was an error, free the allocated memory
network_info_cleanup(g_last_added_network);
if (g_last_added_network != NULL) {
network_info_cleanup(g_last_added_network);
}
sw = SWO_WRONG_P1_P2;
break;
}
*tx = handle_get_config();
sw = SWO_SUCCESS;
break;
default:
network_info_cleanup(g_last_added_network);
if (g_last_added_network != NULL) {
network_info_cleanup(g_last_added_network);
}
sw = SWO_WRONG_P1_P2;
break;
}
Expand Down
6 changes: 6 additions & 0 deletions src/features/sign_message/cmd_sign_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ uint16_t handle_sign_personal_message(uint8_t p1, const uint8_t *const payload,
set_idle();
return SWO_INCORRECT_DATA;
}
// NULL hash context: final_process() already ran, signMsgCtx is still live for the UI.
if (g_msg_hash_ctx == NULL) {
PRINTF("Error: Hash context already finalized!\n");
set_idle();
return SWO_INCORRECT_DATA;
}
// Check if the received chunk data is too long
if ((length + signMsgCtx->processed_size) > signMsgCtx->msg_length) {
PRINTF("Error: Length mismatch ! (%u > %u)!\n",
Expand Down
1 change: 1 addition & 0 deletions src/features/sign_message_eip712/eip712_v1_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ bool eip712_v1_context_init(void) {
return false;
}

appState = APP_STATE_PREPARING_EIP712;
return true;
}

Expand Down
10 changes: 10 additions & 0 deletions src/features/sign_message_eip712/eip712_v1_filtering.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ bool filtering_calldata_spender(const uint8_t *payload,
}

// Handling
if (discarded) return true;
if (get_calldata_info(index) == NULL) {
PRINTF("Error: no matching calldata info (index=%u)\n", index);
return false;
Expand Down Expand Up @@ -488,6 +489,7 @@ bool filtering_calldata_amount(const uint8_t *payload,
}

// Handling
if (discarded) return true;
if (get_calldata_info(index) == NULL) {
PRINTF("Error: no matching calldata info (index=%u)\n", index);
return false;
Expand Down Expand Up @@ -550,6 +552,7 @@ bool filtering_calldata_selector(const uint8_t *payload,
}

// Handling
if (discarded) return true;
if (get_calldata_info(index) == NULL) {
PRINTF("Error: no matching calldata info (index=%u)\n", index);
return false;
Expand Down Expand Up @@ -612,6 +615,7 @@ bool filtering_calldata_chain_id(const uint8_t *payload,
}

// Handling
if (discarded) return true;
if (get_calldata_info(index) == NULL) {
PRINTF("Error: no matching calldata info (index=%u)\n", index);
return false;
Expand Down Expand Up @@ -674,6 +678,7 @@ bool filtering_calldata_callee(const uint8_t *payload,
}

// Handling
if (discarded) return true;
if (get_calldata_info(index) == NULL) {
PRINTF("Error: no matching calldata info (index=%u)\n", index);
return false;
Expand Down Expand Up @@ -736,6 +741,7 @@ bool filtering_calldata_value(const uint8_t *payload,
}

// Handling
if (discarded) return true;
if (get_calldata_info(index) == NULL) {
PRINTF("Error: no matching calldata info (index=%u)\n", index);
return false;
Expand Down Expand Up @@ -1041,6 +1047,7 @@ bool filtering_trusted_name(const uint8_t *payload,
}

// Handling
if (discarded) return true;
if (!check_typename("address")) {
return false;
}
Expand Down Expand Up @@ -1111,6 +1118,7 @@ bool filtering_date_time(const uint8_t *payload,
}

// Handling
if (discarded) return true;
if (!check_typename("uint")) {
return false;
}
Expand Down Expand Up @@ -1175,6 +1183,7 @@ bool filtering_amount_join_token(const uint8_t *payload,
}

// Handling
if (discarded) return true;
if (!check_typename("address")) {
return false;
}
Expand Down Expand Up @@ -1252,6 +1261,7 @@ bool filtering_amount_join_value(const uint8_t *payload,
}

// Handling
if (discarded) return true;
if (join_id == TOKEN_IDX_ADDR_IN_DOMAIN) {
// Permit (ERC-2612)
ui_712_token_join_prepare_addr_check(join_id);
Expand Down
18 changes: 13 additions & 5 deletions src/features/sign_message_eip712/eip712_v1_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "read.h"
#include "eip712_v1_parse.h"
#include "typed_data.h"
#include "shared_context.h" // strings.tmp.tmp

static s_struct_712 *g_pending_struct = NULL;

Expand All @@ -18,11 +17,17 @@ bool v1_set_struct_name(uint8_t length, const uint8_t *name) {
return false;
}

// make NULL-terminated
memcpy(strings.tmp.tmp, name, length);
strings.tmp.tmp[length] = '\0';
char *name_tmp = APP_MEM_ALLOC(length + 1);
if (name_tmp == NULL) {
return false;
}
memcpy(name_tmp, name, length);
name_tmp[length] = '\0';

g_pending_struct = td_create_struct_def(name_tmp);
APP_MEM_FREE_AND_NULL((void **) &name_tmp);

if ((g_pending_struct = td_create_struct_def(strings.tmp.tmp)) == NULL) {
if (g_pending_struct == NULL) {
return false;
}
return td_add_struct_def(g_pending_struct);
Expand Down Expand Up @@ -467,6 +472,9 @@ bool v1_set_array(size_t count) {
}
if ((f->array_remaining > 0) && (f->array_levels_remaining > 0)) {
levels_remaining_after = f->array_levels_remaining - 1;
} else if (f->array_remaining > 0) {
// leaf-level array still in progress — reject re-entry
return false;
} else {
if (elem_field->array_level_count == 0) {
return false;
Expand Down
21 changes: 2 additions & 19 deletions src/features/sign_message_eip712/eip712_v1_ui_logic.c
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ bool ui_712_init(void) {
if (APP_MEM_CALLOC((void **) &ui_ctx, sizeof(*ui_ctx)) == false) {
} else {
ui_712_set_filtering_mode(EIP712_FILTERING_BASIC);
explicit_bzero(&strings, sizeof(strings));
explicit_bzero(strings.tmp.tmp, sizeof(strings.tmp.tmp));
}
return ui_ctx != NULL;
}
Expand All @@ -979,6 +979,7 @@ static void delete_calldata_info(s_eip712_calldata_info *node) {
* Deinit function that simply unsets the struct pointer to NULL
*/
void ui_712_deinit(void) {
eip712_hash_strs_cleanup();
if (ui_ctx != NULL) {
if (ui_ctx->filters_crc != NULL) {
flist_clear((flist_node_t **) &ui_ctx->filters_crc,
Expand All @@ -1001,24 +1002,6 @@ void ui_712_deinit(void) {
}
}

/**
* Approve button handling, calls the common handler function then
* resets the app context (which tears down the EIP712 context as part of it).
*/
void ui_712_approve(void) {
ui_712_approve_cb();
reset_app_context();
}

/**
* Reject button handling, calls the common handler function then
* resets the app context (which tears down the EIP712 context as part of it).
*/
void ui_712_reject(void) {
ui_712_reject_cb();
reset_app_context();
}

/**
* Set a structure field's UI flags
*
Expand Down
2 changes: 0 additions & 2 deletions src/features/sign_message_eip712/eip712_v1_ui_logic.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ typedef struct {
bool ui_712_init(void);
void ui_712_deinit(void);
bool ui_712_end_sign(void);
void ui_712_approve(void);
void ui_712_reject(void);
bool ui_712_set_intent(void);
bool ui_712_set_title(const char *str, size_t length);
bool ui_712_set_value(const char *str, size_t length);
Expand Down
34 changes: 22 additions & 12 deletions src/features/sign_message_eip712_common/common_712.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
#include "eip712_v1_ui_logic.h"
#include "ui_nbgl.h"
#include "cmd_get_tx_simulation.h"
#include "app_mem_utils.h"

static const uint8_t EIP_712_MAGIC[] = {0x19, 0x01};

static char *s_domain_hash_str = NULL;
static char *s_message_hash_str = NULL;

void ui_712_approve_cb(void) {
if (appState != APP_STATE_SIGNING_EIP712) {
io_seproxyhal_send_status(SWO_CONDITIONS_NOT_SATISFIED, 0, true, false);
Expand Down Expand Up @@ -65,26 +69,32 @@ void ui_712_reject_cb(void) {
io_seproxyhal_send_status(SWO_CONDITIONS_NOT_SATISFIED, 0, true, false);
}

static char *format_hash(const uint8_t *hash, char *buffer, size_t buffer_size, size_t offset) {
array_bytes_string(buffer + offset, buffer_size - offset, hash, CX_KECCAK_256_SIZE);
return buffer + offset;
void eip712_hash_strs_cleanup(void) {
APP_MEM_FREE_AND_NULL((void **) &s_domain_hash_str);
APP_MEM_FREE_AND_NULL((void **) &s_message_hash_str);
}

void eip712_format_hash(uint8_t index) {
if ((g_pairs == NULL) || (g_pairsList == NULL) || (index >= g_pairsList->nbPairs)) {
if ((g_pairs == NULL) || (g_pairsList == NULL) || (index + 1 >= g_pairsList->nbPairs)) {
return;
}
const size_t hash_str_size = 2 * CX_KECCAK_256_SIZE + 3;
if ((s_domain_hash_str == NULL) &&
!APP_MEM_CALLOC((void **) &s_domain_hash_str, hash_str_size)) {
return;
}
if ((s_message_hash_str == NULL) &&
!APP_MEM_CALLOC((void **) &s_message_hash_str, hash_str_size)) {
return;
}
messageSigningContext712_t *ctx = &tmpCtx.messageSigningContext712;
array_bytes_string(s_domain_hash_str, hash_str_size, ctx->domainHash, CX_KECCAK_256_SIZE);
array_bytes_string(s_message_hash_str, hash_str_size, ctx->messageHash, CX_KECCAK_256_SIZE);
g_pairs[index].item = "Domain hash";
g_pairs[index].value = format_hash(tmpCtx.messageSigningContext712.domainHash,
strings.tmp.tmp,
sizeof(strings.tmp.tmp),
0);
g_pairs[index].value = s_domain_hash_str;
index++;
g_pairs[index].item = "Message hash";
g_pairs[index].value = format_hash(tmpCtx.messageSigningContext712.messageHash,
strings.tmp.tmp,
sizeof(strings.tmp.tmp),
70);
g_pairs[index].value = s_message_hash_str;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/features/sign_message_eip712_common/common_712.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
bool ui_712_start(e_eip712_filtering_mode filtering);

void eip712_format_hash(uint8_t index);
void eip712_hash_strs_cleanup(void);

void ui_712_approve_cb(void);
void ui_712_reject_cb(void);
3 changes: 1 addition & 2 deletions src/nbgl/ui_message_signing.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "ui_nbgl.h"
#include "eip712_v1_ui_logic.h"
#include "common_712.h"
#include "common_ui.h"
#include "ui_utils.h"
Expand All @@ -25,5 +24,5 @@ void ui_typed_message_review_choice_v0(bool confirm) {
}

void ui_typed_message_review_choice(bool confirm) {
ui_typed_message_review_choice_common(confirm, ui_712_approve, ui_712_reject);
ui_typed_message_review_choice_common(confirm, ui_712_approve_cb, ui_712_reject_cb);
}
6 changes: 3 additions & 3 deletions src/nbgl/ui_sign_712.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
static void ui_712_start_review(e_eip712_filtering_mode filtering_mode,
nbgl_operationType_t operationType,
nbgl_choiceCallback_t choiceCallback) {
#ifdef HAVE_TRANSACTION_CHECKS
set_tx_simulation_warning();
#endif
#ifdef SCREEN_SIZE_WALLET
const char *tx_check_str = ui_tx_simulation_finish_str();
const char *title_suffix = " typed message?";
Expand All @@ -37,9 +40,6 @@ static void ui_712_start_review(e_eip712_filtering_mode filtering_mode,
return;
}
snprintf(g_finishMsg, finish_len, "%s%s", tx_check_str, title_suffix);
#ifdef HAVE_TRANSACTION_CHECKS
set_tx_simulation_warning();
#endif

// Use review with skip button in case of raw message
#ifdef SCREEN_SIZE_WALLET
Expand Down
7 changes: 7 additions & 0 deletions src/nbgl/ui_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ static void _cleanup(void) {
}

void ui_pairs_cleanup(void) {
// Zero out the list header before freeing the backing array so that any
// NBGL render callback still in flight sees an empty list rather than a
// dangling pairs pointer.
if (g_pairsList != NULL) {
g_pairsList->pairs = NULL;
g_pairsList->nbPairs = 0;
}
APP_MEM_FREE_AND_NULL((void **) &g_pairs);
APP_MEM_FREE_AND_NULL((void **) &g_pairsList);
}
Expand Down
1 change: 0 additions & 1 deletion src/plugins/erc1155/erc1155_plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ static const uint8_t *const ERC1155_SELECTORS[] = {
void handle_init_contract_1155(ethPluginInitContract_t *msg) {
erc1155_context_t *context = (erc1155_context_t *) msg->pluginContext;
explicit_bzero(context, sizeof(*context));

uint8_t i;
for (i = 0; i < ARRAYLEN(ERC1155_SELECTORS); i++) {
if (memcmp(PIC(ERC1155_SELECTORS[i]), msg->selector, SELECTOR_SIZE) == 0) {
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/erc1155/erc1155_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ static void set_batch_transfer_ui(ethQueryContractUI_t *msg, erc1155_context_t *
uint8_t pair_offset = (uint8_t) (idx - BATCH_SCREEN_PAIR_BASE);
uint8_t pair_idx = (uint8_t) (pair_offset / 2);
bool show_value = (pair_offset % 2) == 1;
if (pair_idx >= ERC1155_BATCH_DISPLAY_MAX) {
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}
if (show_value) {
uint256_t v;
convertUint256BE(context->batch_values[pair_idx], INT256_LENGTH, &v);
Expand Down
Loading
Loading