2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
2017-01-03 18:40:05 +00:00
|
|
|
#
|
2019-05-29 16:44:09 +00:00
|
|
|
# Copyright (C) 2012-2019 SatoshiLabs and contributors
|
2017-01-03 18:40:05 +00:00
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
2018-06-21 14:28:34 +00:00
|
|
|
# it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
# as published by the Free Software Foundation.
|
2017-01-03 18:40:05 +00:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# 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>.
|
|
|
|
|
2018-05-11 12:53:51 +00:00
|
|
|
import pytest
|
2017-01-03 18:40:05 +00:00
|
|
|
|
2020-04-27 14:37:16 +00:00
|
|
|
from trezorlib import device, messages
|
|
|
|
from trezorlib.exceptions import TrezorFailure
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2020-09-15 11:06:41 +00:00
|
|
|
from ..common import get_test_address
|
|
|
|
|
2019-09-11 12:29:39 +00:00
|
|
|
PIN4 = "1234"
|
|
|
|
PIN6 = "789456"
|
2018-08-13 16:21:24 +00:00
|
|
|
|
2014-02-21 03:46:33 +00:00
|
|
|
|
2020-04-27 14:37:16 +00:00
|
|
|
pytestmark = pytest.mark.skip_t2
|
|
|
|
|
|
|
|
|
|
|
|
def _check_pin(client, pin):
|
2020-08-25 14:28:32 +00:00
|
|
|
client.lock()
|
2020-04-27 14:37:16 +00:00
|
|
|
with client:
|
|
|
|
client.use_pin_sequence([pin])
|
2020-09-15 11:06:41 +00:00
|
|
|
client.set_expected_responses([messages.PinMatrixRequest, messages.Address])
|
|
|
|
get_test_address(client)
|
2020-04-27 14:37:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _check_no_pin(client):
|
2020-08-25 14:28:32 +00:00
|
|
|
client.lock()
|
2020-04-27 14:37:16 +00:00
|
|
|
with client:
|
2020-09-15 11:06:41 +00:00
|
|
|
client.set_expected_responses([messages.Address])
|
|
|
|
get_test_address(client)
|
2020-04-27 14:37:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_set_pin(client):
|
|
|
|
assert client.features.pin_protection is False
|
|
|
|
|
|
|
|
# Check that there's no PIN protection
|
|
|
|
_check_no_pin(client)
|
|
|
|
|
|
|
|
# Let's set new PIN
|
|
|
|
with client:
|
|
|
|
client.use_pin_sequence([PIN6, PIN6])
|
|
|
|
client.set_expected_responses(
|
|
|
|
[
|
|
|
|
messages.ButtonRequest(code=messages.ButtonRequestType.ProtectCall),
|
2020-09-15 11:06:41 +00:00
|
|
|
messages.PinMatrixRequest,
|
|
|
|
messages.PinMatrixRequest,
|
|
|
|
messages.Success,
|
|
|
|
messages.Features,
|
2020-04-27 14:37:16 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
device.change_pin(client)
|
|
|
|
|
|
|
|
# Check that there's PIN protection now
|
|
|
|
assert client.features.pin_protection is True
|
|
|
|
# Check that the PIN is correct
|
|
|
|
_check_pin(client, PIN6)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.setup_client(pin=PIN4)
|
|
|
|
def test_change_pin(client):
|
|
|
|
assert client.features.pin_protection is True
|
|
|
|
# Check that there's PIN protection
|
|
|
|
_check_pin(client, PIN4)
|
|
|
|
|
|
|
|
# Let's change PIN
|
|
|
|
with client:
|
|
|
|
client.use_pin_sequence([PIN4, PIN6, PIN6])
|
|
|
|
client.set_expected_responses(
|
|
|
|
[
|
|
|
|
messages.ButtonRequest(code=messages.ButtonRequestType.ProtectCall),
|
2020-09-15 11:06:41 +00:00
|
|
|
messages.PinMatrixRequest,
|
|
|
|
messages.PinMatrixRequest,
|
|
|
|
messages.PinMatrixRequest,
|
|
|
|
messages.Success,
|
|
|
|
messages.Features,
|
2020-04-27 14:37:16 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
device.change_pin(client)
|
|
|
|
|
|
|
|
# Check that there's still PIN protection now
|
|
|
|
assert client.features.pin_protection is True
|
|
|
|
# Check that the PIN is correct
|
|
|
|
_check_pin(client, PIN6)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.setup_client(pin=PIN4)
|
|
|
|
def test_remove_pin(client):
|
|
|
|
assert client.features.pin_protection is True
|
|
|
|
# Check that there's PIN protection
|
|
|
|
_check_pin(client, PIN4)
|
|
|
|
|
|
|
|
# Let's remove PIN
|
|
|
|
with client:
|
|
|
|
client.use_pin_sequence([PIN4])
|
|
|
|
client.set_expected_responses(
|
|
|
|
[
|
|
|
|
messages.ButtonRequest(code=messages.ButtonRequestType.ProtectCall),
|
2020-09-15 11:06:41 +00:00
|
|
|
messages.PinMatrixRequest,
|
|
|
|
messages.Success,
|
|
|
|
messages.Features,
|
2020-04-27 14:37:16 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
device.change_pin(client, remove=True)
|
|
|
|
|
|
|
|
# Check that there's no PIN protection now
|
|
|
|
assert client.features.pin_protection is False
|
|
|
|
_check_no_pin(client)
|
|
|
|
|
|
|
|
|
|
|
|
def test_set_mismatch(client):
|
|
|
|
assert client.features.pin_protection is False
|
|
|
|
# Check that there's no PIN protection
|
|
|
|
_check_no_pin(client)
|
|
|
|
|
|
|
|
# Let's set new PIN
|
|
|
|
with pytest.raises(TrezorFailure, match="PIN mismatch"), client:
|
|
|
|
# use different PINs for first and second attempt. This will fail.
|
|
|
|
client.use_pin_sequence([PIN4, PIN6])
|
|
|
|
client.set_expected_responses(
|
|
|
|
[
|
|
|
|
messages.ButtonRequest(code=messages.ButtonRequestType.ProtectCall),
|
2020-09-15 11:06:41 +00:00
|
|
|
messages.PinMatrixRequest,
|
|
|
|
messages.PinMatrixRequest,
|
|
|
|
messages.Failure,
|
2020-04-27 14:37:16 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
device.change_pin(client)
|
|
|
|
|
|
|
|
# Check that there's still no PIN protection now
|
|
|
|
client.init_device()
|
|
|
|
assert client.features.pin_protection is False
|
|
|
|
_check_no_pin(client)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.setup_client(pin=PIN4)
|
|
|
|
def test_change_mismatch(client):
|
|
|
|
assert client.features.pin_protection is True
|
|
|
|
|
|
|
|
# Let's set new PIN
|
|
|
|
with pytest.raises(TrezorFailure, match="PIN mismatch"), client:
|
|
|
|
client.use_pin_sequence([PIN4, PIN6, PIN6 + "3"])
|
|
|
|
client.set_expected_responses(
|
|
|
|
[
|
|
|
|
messages.ButtonRequest(code=messages.ButtonRequestType.ProtectCall),
|
2020-09-15 11:06:41 +00:00
|
|
|
messages.PinMatrixRequest,
|
|
|
|
messages.PinMatrixRequest,
|
|
|
|
messages.PinMatrixRequest,
|
|
|
|
messages.Failure,
|
2020-04-27 14:37:16 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
device.change_pin(client)
|
|
|
|
|
|
|
|
# Check that there's still old PIN protection
|
|
|
|
client.init_device()
|
|
|
|
assert client.features.pin_protection is True
|
|
|
|
_check_pin(client, PIN4)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("invalid_pin", ("1204", "", "1234567891"))
|
|
|
|
def test_set_invalid(client, invalid_pin):
|
|
|
|
assert client.features.pin_protection is False
|
|
|
|
|
|
|
|
# Let's set an invalid PIN
|
|
|
|
ret = client.call_raw(messages.ChangePin())
|
|
|
|
assert isinstance(ret, messages.ButtonRequest)
|
|
|
|
|
|
|
|
# Press button
|
|
|
|
client.debug.press_yes()
|
|
|
|
ret = client.call_raw(messages.ButtonAck())
|
|
|
|
|
|
|
|
# Send a PIN containing an invalid digit
|
|
|
|
assert isinstance(ret, messages.PinMatrixRequest)
|
|
|
|
ret = client.call_raw(messages.PinMatrixAck(pin=invalid_pin))
|
|
|
|
|
|
|
|
# Ensure the invalid PIN is detected
|
|
|
|
assert isinstance(ret, messages.Failure)
|
|
|
|
|
|
|
|
# Check that there's still no PIN protection now
|
|
|
|
client.init_device()
|
|
|
|
assert client.features.pin_protection is False
|
|
|
|
_check_no_pin(client)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("invalid_pin", ("1204", "", "1234567891"))
|
|
|
|
@pytest.mark.setup_client(pin=PIN4)
|
|
|
|
def test_enter_invalid(client, invalid_pin):
|
|
|
|
assert client.features.pin_protection is True
|
|
|
|
|
|
|
|
# use an invalid PIN
|
|
|
|
ret = client.call_raw(messages.GetAddress())
|
|
|
|
|
|
|
|
# Send a PIN containing an invalid digit
|
|
|
|
assert isinstance(ret, messages.PinMatrixRequest)
|
|
|
|
ret = client.call_raw(messages.PinMatrixAck(pin=invalid_pin))
|
|
|
|
|
|
|
|
# Ensure the invalid PIN is detected
|
|
|
|
assert isinstance(ret, messages.Failure)
|