2020-02-13 14:10:16 +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>.
|
|
|
|
|
2022-10-25 10:46:37 +00:00
|
|
|
from typing import Iterator
|
|
|
|
|
2020-02-13 14:10:16 +00:00
|
|
|
import pytest
|
|
|
|
|
2021-11-26 15:31:35 +00:00
|
|
|
from trezorlib import btc, device, mapping, messages, models, protobuf
|
2022-01-28 20:04:50 +00:00
|
|
|
from trezorlib._internal.emulator import Emulator
|
2020-02-13 14:10:16 +00:00
|
|
|
from trezorlib.tools import parse_path
|
2024-11-22 08:25:49 +00:00
|
|
|
from trezorlib.debuglink import SessionDebugWrapper as Session
|
2020-02-13 14:10:16 +00:00
|
|
|
|
|
|
|
from ..emulators import EmulatorWrapper
|
|
|
|
from . import for_all
|
|
|
|
|
|
|
|
SOURCE_ASK = 0
|
|
|
|
SOURCE_DEVICE = 1
|
|
|
|
SOURCE_HOST = 2
|
|
|
|
|
|
|
|
|
|
|
|
class ApplySettingsCompat(protobuf.MessageType):
|
|
|
|
MESSAGE_WIRE_TYPE = 25
|
|
|
|
|
2021-06-01 09:29:20 +00:00
|
|
|
FIELDS = {
|
|
|
|
3: protobuf.Field("use_passphrase", "bool"),
|
|
|
|
5: protobuf.Field("passphrase_source", "uint32"),
|
|
|
|
}
|
2020-02-13 14:10:16 +00:00
|
|
|
|
|
|
|
|
2021-02-05 10:57:44 +00:00
|
|
|
mapping.DEFAULT_MAPPING.register(ApplySettingsCompat)
|
2020-02-13 14:10:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2022-10-25 10:46:37 +00:00
|
|
|
def emulator(gen: str, tag: str) -> Iterator[Emulator]:
|
2020-02-13 14:10:16 +00:00
|
|
|
with EmulatorWrapper(gen, tag) as emu:
|
|
|
|
# set up a passphrase-protected device
|
|
|
|
device.reset(
|
2024-11-22 08:25:49 +00:00
|
|
|
emu.client.get_management_session(),
|
2020-09-25 11:59:16 +00:00
|
|
|
pin_protection=False,
|
|
|
|
skip_backup=True,
|
2020-02-13 14:10:16 +00:00
|
|
|
)
|
2024-11-22 08:25:49 +00:00
|
|
|
emu.client = emu.client.get_new_client()
|
|
|
|
resp = emu.client.get_management_session().call(
|
2020-02-13 14:10:16 +00:00
|
|
|
ApplySettingsCompat(use_passphrase=True, passphrase_source=SOURCE_HOST)
|
|
|
|
)
|
|
|
|
assert isinstance(resp, messages.Success)
|
|
|
|
|
|
|
|
yield emu
|
|
|
|
|
|
|
|
|
|
|
|
@for_all(
|
2021-11-26 15:31:35 +00:00
|
|
|
core_minimum_version=models.TREZOR_T.minimum_version,
|
|
|
|
legacy_minimum_version=models.TREZOR_ONE.minimum_version,
|
2020-02-13 14:10:16 +00:00
|
|
|
)
|
2022-01-28 20:04:50 +00:00
|
|
|
def test_passphrase_works(emulator: Emulator):
|
2020-02-13 14:10:16 +00:00
|
|
|
"""Check that passphrase handling in trezorlib works correctly in all versions."""
|
|
|
|
if emulator.client.features.model == "T" and emulator.client.version < (2, 3, 0):
|
|
|
|
expected_responses = [
|
2020-09-15 11:06:41 +00:00
|
|
|
messages.PassphraseRequest,
|
|
|
|
messages.Deprecated_PassphraseStateRequest,
|
|
|
|
messages.Address,
|
2020-02-13 14:10:16 +00:00
|
|
|
]
|
2020-07-20 13:31:52 +00:00
|
|
|
elif (
|
|
|
|
emulator.client.features.model == "T" and emulator.client.version < (2, 3, 3)
|
|
|
|
) or (
|
|
|
|
emulator.client.features.model == "1" and emulator.client.version < (1, 9, 3)
|
|
|
|
):
|
|
|
|
expected_responses = [
|
2020-09-15 11:06:41 +00:00
|
|
|
messages.PassphraseRequest,
|
|
|
|
messages.Address,
|
2020-07-20 13:31:52 +00:00
|
|
|
]
|
2020-02-13 14:10:16 +00:00
|
|
|
else:
|
|
|
|
expected_responses = [
|
2020-09-15 11:06:41 +00:00
|
|
|
messages.PassphraseRequest,
|
|
|
|
messages.ButtonRequest,
|
|
|
|
messages.ButtonRequest,
|
|
|
|
messages.Address,
|
2020-02-13 14:10:16 +00:00
|
|
|
]
|
2024-11-22 08:25:49 +00:00
|
|
|
emu_session = emulator.client.get_session(passphrase="")
|
|
|
|
with emulator.client, Session(emu_session) as session:
|
2020-02-13 14:10:16 +00:00
|
|
|
emulator.client.use_passphrase("TREZOR")
|
2024-11-22 08:25:49 +00:00
|
|
|
session.set_expected_responses(expected_responses)
|
|
|
|
btc.get_address(session, "Testnet", parse_path("44h/1h/0h/0/0"))
|
2020-02-13 14:10:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
@for_all(
|
2021-11-26 15:31:35 +00:00
|
|
|
core_minimum_version=models.TREZOR_T.minimum_version,
|
2020-02-13 14:10:16 +00:00
|
|
|
legacy_minimum_version=(1, 9, 0),
|
|
|
|
)
|
2022-01-28 20:04:50 +00:00
|
|
|
def test_init_device(emulator: Emulator):
|
2020-02-13 14:10:16 +00:00
|
|
|
"""Check that passphrase caching and session_id retaining works correctly across
|
|
|
|
supported versions.
|
|
|
|
"""
|
|
|
|
if emulator.client.features.model == "T" and emulator.client.version < (2, 3, 0):
|
|
|
|
expected_responses = [
|
2020-09-15 11:06:41 +00:00
|
|
|
messages.PassphraseRequest,
|
|
|
|
messages.Deprecated_PassphraseStateRequest,
|
|
|
|
messages.Address,
|
|
|
|
messages.Features,
|
|
|
|
messages.Address,
|
2020-02-13 14:10:16 +00:00
|
|
|
]
|
2020-07-20 13:31:52 +00:00
|
|
|
elif (
|
|
|
|
emulator.client.features.model == "T" and emulator.client.version < (2, 3, 3)
|
|
|
|
) or (
|
|
|
|
emulator.client.features.model == "1" and emulator.client.version < (1, 9, 3)
|
|
|
|
):
|
|
|
|
expected_responses = [
|
2020-09-15 11:06:41 +00:00
|
|
|
messages.PassphraseRequest,
|
|
|
|
messages.Address,
|
|
|
|
messages.Features,
|
|
|
|
messages.Address,
|
2020-07-20 13:31:52 +00:00
|
|
|
]
|
2020-02-13 14:10:16 +00:00
|
|
|
else:
|
|
|
|
expected_responses = [
|
2020-09-15 11:06:41 +00:00
|
|
|
messages.PassphraseRequest,
|
|
|
|
messages.ButtonRequest,
|
|
|
|
messages.ButtonRequest,
|
|
|
|
messages.Address,
|
|
|
|
messages.Features,
|
|
|
|
messages.Address,
|
2020-02-13 14:10:16 +00:00
|
|
|
]
|
|
|
|
|
2024-11-22 08:25:49 +00:00
|
|
|
emu_session = emulator.client.get_session(passphrase="")
|
|
|
|
with emulator.client, Session(emu_session) as session:
|
2020-02-13 14:10:16 +00:00
|
|
|
emulator.client.use_passphrase("TREZOR")
|
2024-11-22 08:25:49 +00:00
|
|
|
session.set_expected_responses(expected_responses)
|
2020-02-13 14:10:16 +00:00
|
|
|
|
2024-11-22 08:25:49 +00:00
|
|
|
btc.get_address(session, "Testnet", parse_path("44h/1h/0h/0/0"))
|
2020-02-13 14:10:16 +00:00
|
|
|
# in TT < 2.3.0 session_id will only be available after PassphraseStateRequest
|
2024-11-22 08:25:49 +00:00
|
|
|
session_id = emulator.client.features.session_id
|
|
|
|
# emulator.client.init_device()
|
2020-02-13 14:10:16 +00:00
|
|
|
btc.get_address(emulator.client, "Testnet", parse_path("44h/1h/0h/0/0"))
|
2024-11-22 08:25:49 +00:00
|
|
|
assert session_id == emulator.client.features.session_id
|