2020-08-04 18:02:57 +00:00
|
|
|
# This file is part of the Trezor project.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012-2019 SatoshiLabs and contributors
|
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
# as published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the License along with this library.
|
|
|
|
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2021-11-10 21:28:11 +00:00
|
|
|
from trezorlib import btc, device, messages
|
2020-08-04 18:02:57 +00:00
|
|
|
from trezorlib.exceptions import TrezorFailure
|
2021-12-08 13:53:56 +00:00
|
|
|
from trezorlib.tools import H_, parse_path
|
2020-08-04 18:02:57 +00:00
|
|
|
|
2021-11-29 11:00:01 +00:00
|
|
|
from ...tx_cache import TxCache
|
2021-12-08 13:53:56 +00:00
|
|
|
from ..signtx import request_finished, request_input, request_meta, request_output
|
2020-08-04 18:02:57 +00:00
|
|
|
|
2021-12-08 13:53:56 +00:00
|
|
|
B = messages.ButtonRequestType
|
2020-08-04 18:02:57 +00:00
|
|
|
TX_CACHE_MAINNET = TxCache("Bitcoin")
|
2021-12-14 12:57:56 +00:00
|
|
|
TX_CACHE_TESTNET = TxCache("Testnet")
|
2020-08-04 18:02:57 +00:00
|
|
|
TX_CACHE_BCASH = TxCache("Bcash")
|
|
|
|
|
|
|
|
TXHASH_8cc1f4 = bytes.fromhex(
|
|
|
|
"8cc1f4adf7224ce855cf535a5104594a0004cb3b640d6714fdb00b9128832dd5"
|
|
|
|
)
|
2021-12-14 12:57:56 +00:00
|
|
|
TXHASH_a5cd2a = bytes.fromhex(
|
|
|
|
"a5cd2a706d680587e572df16a8ce5233139a094ebbd148cc66a8004dcc88819c"
|
|
|
|
)
|
2020-08-04 18:02:57 +00:00
|
|
|
TXHASH_d5f65e = bytes.fromhex(
|
|
|
|
"d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882"
|
|
|
|
)
|
2021-12-08 13:53:56 +00:00
|
|
|
TXHASH_fa80a9 = bytes.fromhex(
|
|
|
|
"fa80a9949f1094119195064462f54d0e0eabd3139becd4514ae635b8c7fe3a46"
|
|
|
|
)
|
|
|
|
TXHASH_5dfd1b = bytes.fromhex(
|
|
|
|
"5dfd1b037633adc7f84a17b2df31c9994fe50b3ab3e246c44c4ceff3d326f62e"
|
|
|
|
)
|
2021-11-10 21:19:53 +00:00
|
|
|
|
2021-11-29 11:00:01 +00:00
|
|
|
|
2021-11-10 21:19:53 +00:00
|
|
|
# Adapted from TestMsgSigntx.test_one_one_fee,
|
|
|
|
# only changed the coin from Bitcoin to Litecoin.
|
|
|
|
# Litecoin does not have strong replay protection using SIGHASH_FORKID,
|
|
|
|
# spending from Bitcoin path should fail.
|
2021-12-08 13:53:56 +00:00
|
|
|
@pytest.mark.altcoin
|
2021-11-10 21:19:53 +00:00
|
|
|
def test_invalid_path_fail(client):
|
|
|
|
# tx: d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882
|
|
|
|
# input 0: 0.0039 BTC
|
|
|
|
|
2021-11-10 21:28:11 +00:00
|
|
|
inp1 = messages.TxInputType(
|
2021-11-10 21:19:53 +00:00
|
|
|
address_n=parse_path("44h/0h/0h/0/0"),
|
|
|
|
amount=390000,
|
|
|
|
prev_hash=TXHASH_d5f65e,
|
|
|
|
prev_index=0,
|
|
|
|
)
|
|
|
|
|
|
|
|
# address is converted from 1MJ2tj2ThBE62zXbBYA5ZaN3fdve5CPAz1 by changing the version
|
2021-11-10 21:28:11 +00:00
|
|
|
out1 = messages.TxOutputType(
|
2021-11-10 21:19:53 +00:00
|
|
|
address="LfWz9wLHmqU9HoDkMg9NqbRosrHvEixeVZ",
|
|
|
|
amount=390000 - 10000,
|
2021-11-10 21:28:11 +00:00
|
|
|
script_type=messages.OutputScriptType.PAYTOADDRESS,
|
2021-11-10 21:19:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(TrezorFailure) as exc:
|
2021-05-21 18:05:19 +00:00
|
|
|
btc.sign_tx(client, "Litecoin", [inp1], [out1], prev_txes=TX_CACHE_MAINNET)
|
|
|
|
|
2021-11-10 21:28:11 +00:00
|
|
|
assert exc.value.code == messages.FailureType.DataError
|
2021-11-10 21:19:53 +00:00
|
|
|
assert exc.value.message.endswith("Forbidden key path")
|
|
|
|
|
|
|
|
|
|
|
|
# Adapted from TestMsgSigntx.test_one_one_fee,
|
|
|
|
# only changed the coin from Bitcoin to Litecoin and set safety checks to prompt.
|
|
|
|
# Litecoin does not have strong replay protection using SIGHASH_FORKID, but
|
|
|
|
# spending from Bitcoin path should pass with safety checks set to prompt.
|
2021-12-08 13:53:56 +00:00
|
|
|
@pytest.mark.altcoin
|
2021-11-10 21:19:53 +00:00
|
|
|
def test_invalid_path_prompt(client):
|
|
|
|
# tx: d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882
|
|
|
|
# input 0: 0.0039 BTC
|
|
|
|
|
2021-11-10 21:28:11 +00:00
|
|
|
inp1 = messages.TxInputType(
|
2021-11-10 21:19:53 +00:00
|
|
|
address_n=parse_path("44h/0h/0h/0/0"),
|
|
|
|
amount=390000,
|
|
|
|
prev_hash=TXHASH_d5f65e,
|
|
|
|
prev_index=0,
|
|
|
|
)
|
|
|
|
|
|
|
|
# address is converted from 1MJ2tj2ThBE62zXbBYA5ZaN3fdve5CPAz1 by changing the version
|
2021-11-10 21:28:11 +00:00
|
|
|
out1 = messages.TxOutputType(
|
2021-11-10 21:19:53 +00:00
|
|
|
address="LfWz9wLHmqU9HoDkMg9NqbRosrHvEixeVZ",
|
|
|
|
amount=390000 - 10000,
|
2021-11-10 21:28:11 +00:00
|
|
|
script_type=messages.OutputScriptType.PAYTOADDRESS,
|
2021-11-10 21:19:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
device.apply_settings(
|
2021-11-10 21:28:11 +00:00
|
|
|
client, safety_checks=messages.SafetyCheckLevel.PromptTemporarily
|
2021-11-10 21:19:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
btc.sign_tx(client, "Litecoin", [inp1], [out1], prev_txes=TX_CACHE_MAINNET)
|
|
|
|
|
|
|
|
|
|
|
|
# Adapted from TestMsgSigntx.test_one_one_fee,
|
|
|
|
# only changed the coin from Bitcoin to Bcash.
|
|
|
|
# Bcash does have strong replay protection using SIGHASH_FORKID,
|
|
|
|
# spending from Bitcoin path should work.
|
2021-12-08 13:53:56 +00:00
|
|
|
@pytest.mark.altcoin
|
2021-11-10 21:19:53 +00:00
|
|
|
def test_invalid_path_pass_forkid(client):
|
|
|
|
# tx: 8cc1f4adf7224ce855cf535a5104594a0004cb3b640d6714fdb00b9128832dd5
|
|
|
|
# input 0: 0.0039 BTC
|
|
|
|
|
2021-11-10 21:28:11 +00:00
|
|
|
inp1 = messages.TxInputType(
|
2021-11-10 21:19:53 +00:00
|
|
|
address_n=parse_path("44h/0h/0h/0/0"),
|
|
|
|
amount=390000,
|
|
|
|
prev_hash=TXHASH_8cc1f4,
|
|
|
|
prev_index=0,
|
|
|
|
)
|
|
|
|
|
|
|
|
# address is converted from 1MJ2tj2ThBE62zXbBYA5ZaN3fdve5CPAz1 to cashaddr format
|
2021-11-10 21:28:11 +00:00
|
|
|
out1 = messages.TxOutputType(
|
2021-11-10 21:19:53 +00:00
|
|
|
address="bitcoincash:qr0fk25d5zygyn50u5w7h6jkvctas52n0qxff9ja6r",
|
|
|
|
amount=390000 - 10000,
|
2021-11-10 21:28:11 +00:00
|
|
|
script_type=messages.OutputScriptType.PAYTOADDRESS,
|
2021-11-10 21:19:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
btc.sign_tx(client, "Bcash", [inp1], [out1], prev_txes=TX_CACHE_BCASH)
|
2021-12-08 13:53:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_attack_path_segwit(client):
|
|
|
|
# Scenario: The attacker falsely claims that the transaction uses Testnet paths to
|
|
|
|
# avoid the path warning dialog, but in step6_sign_segwit_inputs() uses Bitcoin paths
|
|
|
|
# to get a valid signature.
|
|
|
|
|
|
|
|
device.apply_settings(
|
|
|
|
client, safety_checks=messages.SafetyCheckLevel.PromptTemporarily
|
|
|
|
)
|
|
|
|
|
|
|
|
inp1 = messages.TxInputType(
|
|
|
|
# The actual input that the attcker wants to get signed.
|
|
|
|
address_n=parse_path("84'/0'/0'/0/0"),
|
|
|
|
amount=9426,
|
|
|
|
prev_hash=TXHASH_fa80a9,
|
|
|
|
prev_index=0,
|
|
|
|
script_type=messages.InputScriptType.SPENDWITNESS,
|
|
|
|
)
|
|
|
|
inp2 = messages.TxInputType(
|
|
|
|
# The actual input that the attcker wants to get signed.
|
|
|
|
# We need this one to be from a different account, so that the match checker
|
|
|
|
# allows the transaction to pass.
|
|
|
|
address_n=parse_path("84'/0'/1'/0/1"),
|
|
|
|
amount=7086,
|
|
|
|
prev_hash=TXHASH_5dfd1b,
|
|
|
|
prev_index=0,
|
|
|
|
script_type=messages.InputScriptType.SPENDWITNESS,
|
|
|
|
)
|
|
|
|
|
|
|
|
out1 = messages.TxOutputType(
|
|
|
|
# Attacker's Mainnet address encoded as Testnet.
|
|
|
|
address="tb1q694ccp5qcc0udmfwgp692u2s2hjpq5h407urtu",
|
|
|
|
script_type=messages.OutputScriptType.PAYTOADDRESS,
|
|
|
|
amount=9426 + 7086 - 500,
|
|
|
|
)
|
|
|
|
|
|
|
|
attack_count = 6
|
|
|
|
|
|
|
|
def attack_processor(msg):
|
|
|
|
nonlocal attack_count
|
|
|
|
# Make the inputs look like they are coming from Testnet paths until we reach the
|
|
|
|
# signing phase.
|
|
|
|
if attack_count > 0 and msg.tx.inputs and msg.tx.inputs[0] in (inp1, inp2):
|
|
|
|
attack_count -= 1
|
|
|
|
msg.tx.inputs[0].address_n[1] = H_(1)
|
|
|
|
|
|
|
|
return msg
|
|
|
|
|
|
|
|
with client:
|
|
|
|
client.set_filter(messages.TxAck, attack_processor)
|
|
|
|
client.set_expected_responses(
|
|
|
|
[
|
|
|
|
# Step: process inputs
|
|
|
|
request_input(0),
|
|
|
|
# Attacker bypasses warning about non-standard path.
|
|
|
|
request_input(1),
|
|
|
|
# Attacker bypasses warning about non-standard path.
|
|
|
|
# Step: approve outputs
|
|
|
|
request_output(0),
|
|
|
|
messages.ButtonRequest(code=B.ConfirmOutput),
|
|
|
|
messages.ButtonRequest(code=B.SignTx),
|
|
|
|
# Step: verify inputs
|
|
|
|
request_input(0),
|
|
|
|
request_meta(TXHASH_fa80a9),
|
|
|
|
request_input(0, TXHASH_fa80a9),
|
|
|
|
request_output(0, TXHASH_fa80a9),
|
|
|
|
request_output(1, TXHASH_fa80a9),
|
|
|
|
request_input(1),
|
|
|
|
request_meta(TXHASH_5dfd1b),
|
|
|
|
request_input(0, TXHASH_5dfd1b),
|
|
|
|
request_output(0, TXHASH_5dfd1b),
|
|
|
|
request_output(1, TXHASH_5dfd1b),
|
|
|
|
# Step: serialize inputs
|
|
|
|
request_input(0),
|
|
|
|
request_input(1),
|
|
|
|
# Step: serialize outputs
|
|
|
|
request_output(0),
|
|
|
|
# Step: sign segwit inputs
|
|
|
|
request_input(0),
|
|
|
|
# Trezor must warn about non-standard path before signing.
|
|
|
|
messages.ButtonRequest(code=B.UnknownDerivationPath),
|
|
|
|
request_input(1),
|
|
|
|
# Trezor must warn about non-standard path before signing.
|
|
|
|
messages.ButtonRequest(code=B.UnknownDerivationPath),
|
|
|
|
request_finished(),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2021-12-08 16:06:37 +00:00
|
|
|
btc.sign_tx(client, "Testnet", [inp1, inp2], [out1], prev_txes=TX_CACHE_MAINNET)
|
2021-12-14 12:57:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skip_t1(reason="T1 only prevents using paths known to be altcoins")
|
|
|
|
def test_invalid_path_fail_asap(client):
|
|
|
|
inp1 = messages.TxInputType(
|
|
|
|
address_n=parse_path("0"),
|
|
|
|
amount=4977040,
|
|
|
|
prev_hash=TXHASH_a5cd2a,
|
|
|
|
prev_index=0,
|
|
|
|
script_type=messages.InputScriptType.SPENDWITNESS,
|
|
|
|
sequence=4294967293,
|
|
|
|
)
|
|
|
|
|
|
|
|
out1 = messages.TxOutputType(
|
|
|
|
address_n=parse_path("84h/0h/0h/1/0"),
|
|
|
|
amount=4977040,
|
|
|
|
script_type=messages.OutputScriptType.PAYTOWITNESS,
|
|
|
|
)
|
|
|
|
|
|
|
|
with client:
|
|
|
|
client.set_expected_responses(
|
|
|
|
[request_input(0), messages.Failure(code=messages.FailureType.DataError)]
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
btc.sign_tx(client, "Testnet", [inp1], [out1], prev_txes=TX_CACHE_TESTNET)
|
|
|
|
except TrezorFailure:
|
|
|
|
pass
|