Skip to content

Commit

Permalink
[SQUASH] rework again to move most code to signTx*
Browse files Browse the repository at this point in the history
  • Loading branch information
mkv-vcm committed Oct 26, 2021
1 parent f108e12 commit 15b5a14
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 88 deletions.
34 changes: 6 additions & 28 deletions src/securityPolicy.c
Expand Up @@ -2,9 +2,7 @@
#include "addressUtilsByron.h"
#include "bip44.h"
#include "cardano.h"
#include "singleAccount.h"
#include "signTxUtils.h"
#include "state.h"

#include "securityPolicy.h"

Expand Down Expand Up @@ -377,7 +375,7 @@ security_policy_t policyForSignTxOutputAddressParams(
case SIGN_TX_SIGNINGMODE_POOL_REGISTRATION_OPERATOR:
case SIGN_TX_SIGNINGMODE_ORDINARY_TX: {
DENY_UNLESS(determineSpendingChoice(params->type) == SPENDING_PATH);
validateOrStoreSingleAccount(&(instructionState.signTxContext.commonTxData.singleAccountData), &params->spendingKeyPath);
DENY_IF(violatesAndStoreSingleAccount(&params->spendingKeyPath));
SHOW_UNLESS(is_standard_base_address(params));
ALLOW();
break;
Expand Down Expand Up @@ -517,7 +515,7 @@ security_policy_t policyForSignTxCertificateStaking(
case SIGN_TX_SIGNINGMODE_ORDINARY_TX:
DENY_UNLESS(stakeCredential->type == STAKE_CREDENTIAL_KEY_PATH);
DENY_UNLESS(bip44_isOrdinaryStakingKeyPath(&stakeCredential->keyPath));
validateOrStoreSingleAccount(&(instructionState.signTxContext.commonTxData.singleAccountData), &stakeCredential->keyPath);
DENY_IF(violatesAndStoreSingleAccount(&stakeCredential->keyPath));
break;
case SIGN_TX_SIGNINGMODE_MULTISIG_TX:
DENY_UNLESS(stakeCredential->type == STAKE_CREDENTIAL_SCRIPT_HASH);
Expand Down Expand Up @@ -620,7 +618,7 @@ security_policy_t policyForSignTxStakePoolRegistrationOwner(
{
if (owner->keyReferenceType == KEY_REFERENCE_PATH) {
DENY_UNLESS(bip44_isOrdinaryStakingKeyPath(&owner->path));
validateOrStoreSingleAccount(&(instructionState.signTxContext.commonTxData.singleAccountData), &owner->path);
DENY_IF(violatesAndStoreSingleAccount(&owner->path));
}

switch (txSigningMode) {
Expand Down Expand Up @@ -690,7 +688,7 @@ security_policy_t policyForSignTxWithdrawal(
case SIGN_TX_SIGNINGMODE_ORDINARY_TX:
DENY_UNLESS(stakeCredential->type == STAKE_CREDENTIAL_KEY_PATH);
DENY_UNLESS(bip44_isOrdinaryStakingKeyPath(&stakeCredential->keyPath));
validateOrStoreSingleAccount(&(instructionState.signTxContext.commonTxData.singleAccountData), &stakeCredential->keyPath);
DENY_IF(violatesAndStoreSingleAccount(&stakeCredential->keyPath));
SHOW();
break;

Expand All @@ -710,8 +708,8 @@ static inline security_policy_t _ordinaryWitnessPolicy(const bip44_path_t* path,
{
switch (bip44_classifyPath(path)) {
case PATH_ORDINARY_SPENDING_KEY:
validateOrStoreSingleAccount(&(instructionState.signTxContext.commonTxData.singleAccountData), path);
// intentional fallthrough
DENY_IF(violatesAndStoreSingleAccount(path));
// intentional fallthrough
case PATH_ORDINARY_STAKING_KEY:
case PATH_POOL_COLD_KEY:
WARN_UNLESS(bip44_isPathReasonable(path));
Expand Down Expand Up @@ -916,23 +914,3 @@ security_policy_t policyForSignOpCert(const bip44_path_t* poolColdKeyPathSpec)

DENY(); // should not be reached
}

single_account_return_t policyForSingleAccountPath(single_account_data_t* accountData, const bip44_path_t* path)
{
if (!bip44_hasOrdinaryWalletKeyPrefix(path) || !bip44_containsAccount(path)) {
return SINGLE_ACCOUNT_IGNORE;
}
if (accountData->pathSet) {
const uint32_t storedAccount = accountData->accountNumber;
if (bip44_getAccount(path) != storedAccount) {
return SINGLE_ACCOUNT_VIOLATE;
}
if ((accountData->byron ^ bip44_hasByronPrefix(path))
&& storedAccount != 0) {
return SINGLE_ACCOUNT_VIOLATE;
}
} else {
return SINGLE_ACCOUNT_VALID;
}
return false;
}
7 changes: 0 additions & 7 deletions src/securityPolicy.h
Expand Up @@ -129,11 +129,4 @@ security_policy_t policyForCatalystRegistrationVotingKey();
security_policy_t policyForCatalystRegistrationNonce();
security_policy_t policyForCatalystRegistrationConfirm();

typedef enum {
SINGLE_ACCOUNT_VIOLATE,
SINGLE_ACCOUNT_VALID,
SINGLE_ACCOUNT_IGNORE
} single_account_return_t;
single_account_return_t policyForSingleAccountPath(single_account_data_t* accountData, const bip44_path_t* path);

#endif // H_CARDANO_APP_SECURITY_POLICY
41 changes: 23 additions & 18 deletions src/signTxUtils.c
Expand Up @@ -4,6 +4,9 @@
#include "utils.h"
#include "signTxUtils.h"
#include "securityPolicy.h"
#include "state.h"

static single_account_data_t* singleAccountData = &(instructionState.signTxContext.commonTxData.singleAccountData);

void respondSuccessEmptyMsg()
{
Expand All @@ -12,25 +15,27 @@ void respondSuccessEmptyMsg()
ui_displayBusy(); // needs to happen after I/O
}

void validateOrStoreSingleAccount(single_account_data_t* accountData, const bip44_path_t* path)
bool violatesAndStoreSingleAccount(const bip44_path_t* path)
{
switch (policyForSingleAccountPath(accountData, path))
{
case SINGLE_ACCOUNT_VALID:
if (!accountData->pathSet) {
accountData->pathSet = true;
accountData->byron = bip44_hasByronPrefix(path);
accountData->accountNumber = bip44_getAccount(path);
}
break;
case SINGLE_ACCOUNT_VIOLATE:
THROW(ERR_REJECTED_BY_POLICY);
break;
case SINGLE_ACCOUNT_IGNORE:
break;

default:
if (!bip44_hasOrdinaryWalletKeyPrefix(path) || !bip44_containsAccount(path)) {
TRACE("Invalid path in single account check");
ASSERT(false);
break;
}
const bool byron = bip44_hasByronPrefix(path);
const uint32_t account = bip44_getAccount(path);
if (singleAccountData->pathSet) {
const uint32_t storedAccount = singleAccountData->accountNumber;
if (account != storedAccount) {
return true;
}
if ((singleAccountData->byron ^ byron)
&& storedAccount != 0) {
return true;
}
} else {
singleAccountData->pathSet = true;
singleAccountData->byron = byron;
singleAccountData->accountNumber = account;
}
return false;
}
2 changes: 1 addition & 1 deletion src/signTxUtils.h
Expand Up @@ -6,6 +6,6 @@

void respondSuccessEmptyMsg();

void validateOrStoreSingleAccount(single_account_data_t* accountData, const bip44_path_t* path);
bool violatesAndStoreSingleAccount(const bip44_path_t* path);

#endif // H_CARDANO_APP_SIGN_TX_UTILS
26 changes: 0 additions & 26 deletions src/singleAccount.c

This file was deleted.

8 changes: 0 additions & 8 deletions src/singleAccount.h

This file was deleted.

0 comments on commit 15b5a14

Please sign in to comment.