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
133 changes: 81 additions & 52 deletions src_features/provide_tx_simulation/cmd_get_tx_simulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -570,18 +570,93 @@ void clear_tx_simulation(void) {
explicit_bzero(&TX_SIMULATION, sizeof(TX_SIMULATION));
}

/**
* @brief Check the TX HASH vs Simulation payload.
*
* @return whether it was successful
*/
bool check_tx_simulation_hash(void) {
uint8_t *hash = NULL;
uint8_t *hash2 = NULL;

if (!N_storage.w3c_enable) {
// W3Checks disabled
return true;
}
switch (appState) {
case APP_STATE_SIGNING_TX:
hash = tmpCtx.transactionContext.hash;
break;
case APP_STATE_SIGNING_MESSAGE:
hash = tmpCtx.messageSigningContext.hash;
break;
case APP_STATE_SIGNING_EIP712:
hash = tmpCtx.messageSigningContext712.messageHash;
hash2 = tmpCtx.messageSigningContext712.domainHash;
break;
default:
PRINTF("[TX SIMU] Invalid app State %d!\n", appState);
TX_SIMULATION.risk = RISK_UNKNOWN;
return false;
}
if (memcmp(TX_SIMULATION.tx_hash, hash, HASH_SIZE) != 0) {
PRINTF("[TX SIMU] TX_HASH mismatch: %.*h != %.*h\n",
HASH_SIZE,
TX_SIMULATION.tx_hash,
HASH_SIZE,
hash);
PRINTF("[TX SIMU] Force Score to UNKNOWN\n");
TX_SIMULATION.risk = RISK_UNKNOWN;
return false;
}
if ((hash2 != NULL) && (memcmp(TX_SIMULATION.domain_hash, hash2, HASH_SIZE)) != 0) {
PRINTF("[TX SIMU] DOMAIN_HASH mismatch: %.*h != %.*h\n",
HASH_SIZE,
TX_SIMULATION.domain_hash,
HASH_SIZE,
hash);
PRINTF("[TX SIMU] Force Score to UNKNOWN\n");
TX_SIMULATION.risk = RISK_UNKNOWN;
return false;
}
return true;
}

/**
* @brief Check the FROM_ADDRESS vs Simulation payload.
*
* @return whether it was successful
*/
bool check_tx_simulation_from_address(void) {
uint8_t msg_sender[ADDRESS_LENGTH] = {0};
if (get_public_key(msg_sender, sizeof(msg_sender)) != APDU_RESPONSE_OK) {
PRINTF("[TX SIMU] Unable to get the public key!\n");
PRINTF("[TX SIMU] Force Score to UNKNOWN\n");
TX_SIMULATION.risk = RISK_UNKNOWN;
return false;
}
if (memcmp(TX_SIMULATION.addr, msg_sender, ADDRESS_LENGTH) != 0) {
PRINTF("[TX SIMU] FROM addr mismatch: %.*h != %.*h\n",
ADDRESS_LENGTH,
TX_SIMULATION.addr,
ADDRESS_LENGTH,
msg_sender);
PRINTF("[TX SIMU] Force Score to UNKNOWN\n");
TX_SIMULATION.risk = RISK_UNKNOWN;
return false;
}
return true;
}

/**
* @brief Check the TX vs Simulation parameters (CHAIN_ID, TX_HASH).
*
* @param[in] checkTxHash flag to check the TX_HASH
* @param[in] checkFromAddr flag to check the FROM address
* @return whether it was successful
*/
bool check_tx_simulation_params(bool checkTxHash, bool checkFromAddr) {
uint8_t msg_sender[ADDRESS_LENGTH] = {0};
static bool check_tx_simulation_params(bool checkTxHash, bool checkFromAddr) {
uint64_t chain_id = get_tx_chain_id();
uint8_t *hash = NULL;
uint8_t *hash2 = NULL;

if (!N_storage.w3c_enable) {
// W3Checks disabled
Expand Down Expand Up @@ -630,58 +705,12 @@ bool check_tx_simulation_params(bool checkTxHash, bool checkFromAddr) {
return false;
}
if (checkFromAddr) {
if (get_public_key(msg_sender, sizeof(msg_sender)) != APDU_RESPONSE_OK) {
PRINTF("[TX SIMU] Unable to get the public key!\n");
PRINTF("[TX SIMU] Force Score to UNKNOWN\n");
TX_SIMULATION.risk = RISK_UNKNOWN;
return false;
}
if (memcmp(TX_SIMULATION.addr, msg_sender, ADDRESS_LENGTH) != 0) {
PRINTF("[TX SIMU] FROM addr mismatch: %.*h != %.*h\n",
ADDRESS_LENGTH,
TX_SIMULATION.addr,
ADDRESS_LENGTH,
msg_sender);
PRINTF("[TX SIMU] Force Score to UNKNOWN\n");
TX_SIMULATION.risk = RISK_UNKNOWN;
if (check_tx_simulation_from_address() == false) {
return false;
}
}
if (checkTxHash) {
switch (appState) {
case APP_STATE_SIGNING_TX:
hash = tmpCtx.transactionContext.hash;
break;
case APP_STATE_SIGNING_MESSAGE:
hash = tmpCtx.messageSigningContext.hash;
break;
case APP_STATE_SIGNING_EIP712:
hash = tmpCtx.messageSigningContext712.messageHash;
hash2 = tmpCtx.messageSigningContext712.domainHash;
break;
default:
PRINTF("[TX SIMU] Invalid app State %d!\n", appState);
TX_SIMULATION.risk = RISK_UNKNOWN;
return false;
}
if (memcmp(TX_SIMULATION.tx_hash, hash, HASH_SIZE) != 0) {
PRINTF("[TX SIMU] TX_HASH mismatch: %.*h != %.*h\n",
HASH_SIZE,
TX_SIMULATION.tx_hash,
HASH_SIZE,
hash);
PRINTF("[TX SIMU] Force Score to UNKNOWN\n");
TX_SIMULATION.risk = RISK_UNKNOWN;
return false;
}
if ((hash2 != NULL) && (memcmp(TX_SIMULATION.domain_hash, hash2, HASH_SIZE)) != 0) {
PRINTF("[TX SIMU] DOMAIN_HASH mismatch: %.*h != %.*h\n",
HASH_SIZE,
TX_SIMULATION.domain_hash,
HASH_SIZE,
hash);
PRINTF("[TX SIMU] Force Score to UNKNOWN\n");
TX_SIMULATION.risk = RISK_UNKNOWN;
if (check_tx_simulation_hash() == false) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ void ui_tx_simulation_error(nbgl_choiceCallback_t callback);
void ui_tx_simulation_opt_in(bool response_expected);

void clear_tx_simulation(void);
bool check_tx_simulation_params(bool checkTxHash, bool checkFromAddr);
bool check_tx_simulation_hash(void);
bool check_tx_simulation_from_address(void);
void set_tx_simulation_warning(nbgl_warning_t *p_warning, bool checkTxHash, bool checkFromAddr);

const char *get_tx_simulation_risk_str(void);
Expand Down
3 changes: 2 additions & 1 deletion src_nbgl/ui_sign_712.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ void ui_712_switch_to_sign(void) {
nbgl_useCaseReviewStreamingContinueExt(&pairs_list, message_progress, review_skip);
} else {
#ifdef HAVE_WEB3_CHECKS
if (check_tx_simulation_params(true, true) == false) {
if ((TX_SIMULATION.risk != RISK_UNKNOWN) && ((check_tx_simulation_hash() == false) ||
check_tx_simulation_from_address() == false)) {
ui_tx_simulation_error(ui_712_w3c_cb);
return;
}
Expand Down
4 changes: 3 additions & 1 deletion src_nbgl/ui_sign_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ static void ui_191_process_state(void) {
break;
case UI_191_ACTION_GO_TO_SIGN:
#ifdef HAVE_WEB3_CHECKS
if (check_tx_simulation_params(true, true) == false) {
if ((TX_SIMULATION.risk != RISK_UNKNOWN) &&
((check_tx_simulation_hash() == false) ||
check_tx_simulation_from_address() == false)) {
ui_tx_simulation_error(ui_191_w3c_cb);
return;
}
Expand Down
Loading