2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
2017-01-03 18:40:05 +00:00
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# Copyright (C) 2012-2018 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>.
|
2017-01-03 18:40:05 +00:00
|
|
|
|
2013-10-11 01:51:45 +00:00
|
|
|
import time
|
2018-08-13 16:21:24 +00:00
|
|
|
|
2018-05-11 12:53:51 +00:00
|
|
|
import pytest
|
|
|
|
|
2017-12-12 15:40:11 +00:00
|
|
|
from trezorlib import messages as proto
|
2018-11-14 13:44:05 +00:00
|
|
|
from trezorlib.exceptions import PinException
|
2018-05-21 12:28:53 +00:00
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
from .common import TrezorTest
|
2014-02-03 23:32:10 +00:00
|
|
|
|
|
|
|
# FIXME TODO Add passphrase tests
|
2013-01-14 13:44:11 +00:00
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2017-12-19 18:24:18 +00:00
|
|
|
@pytest.mark.skip_t2
|
2017-12-23 20:20:49 +00:00
|
|
|
class TestProtectCall(TrezorTest):
|
2014-02-13 15:47:28 +00:00
|
|
|
def _some_protected_call(self, button, pin, passphrase):
|
2013-01-14 16:05:38 +00:00
|
|
|
# This method perform any call which have protection in the device
|
2017-06-23 19:31:42 +00:00
|
|
|
res = self.client.ping(
|
2018-08-13 16:21:24 +00:00
|
|
|
"random data",
|
2017-06-23 19:31:42 +00:00
|
|
|
button_protection=button,
|
|
|
|
pin_protection=pin,
|
2018-08-13 16:21:24 +00:00
|
|
|
passphrase_protection=passphrase,
|
2017-06-23 19:31:42 +00:00
|
|
|
)
|
2018-08-13 16:21:24 +00:00
|
|
|
assert res == "random data"
|
2013-10-11 01:51:45 +00:00
|
|
|
|
2014-06-04 15:59:16 +00:00
|
|
|
"""
|
2014-02-15 19:31:34 +00:00
|
|
|
def test_expected_responses(self):
|
2014-02-17 00:54:54 +00:00
|
|
|
self.setup_mnemonic_pin_passphrase()
|
|
|
|
|
2014-02-15 19:31:34 +00:00
|
|
|
# This is low-level test of set_expected_responses()
|
|
|
|
# feature of debugging client
|
|
|
|
|
2014-02-21 06:28:56 +00:00
|
|
|
with self.client:
|
|
|
|
# Scenario 1 - Received unexpected message
|
|
|
|
self.client.set_expected_responses([])
|
2017-12-23 20:20:49 +00:00
|
|
|
with pytest.raises(CallException):
|
|
|
|
self._some_protected_call(True, True, True)
|
2014-02-21 06:28:56 +00:00
|
|
|
|
|
|
|
with self.client:
|
|
|
|
# Scenario 2 - Received other than expected message
|
|
|
|
self.client.set_expected_responses([proto.Success()])
|
2017-12-23 20:20:49 +00:00
|
|
|
with pytest.raises(CallException):
|
|
|
|
self._some_protected_call(True, True, True)
|
2014-02-21 06:28:56 +00:00
|
|
|
|
|
|
|
def scenario3():
|
|
|
|
with self.client:
|
|
|
|
# Scenario 3 - Not received expected message
|
|
|
|
self.client.set_expected_responses([proto.ButtonRequest(),
|
|
|
|
proto.Success(),
|
|
|
|
proto.Success()]) # This is expected, but not received
|
|
|
|
self._some_protected_call(True, False, False)
|
2017-12-23 20:20:49 +00:00
|
|
|
with pytest.raises(Exception):
|
|
|
|
scenario3()
|
2014-02-21 06:28:56 +00:00
|
|
|
|
|
|
|
with self.client:
|
|
|
|
# Scenario 4 - Received what expected
|
|
|
|
self.client.set_expected_responses([proto.ButtonRequest(),
|
|
|
|
proto.PinMatrixRequest(),
|
|
|
|
proto.PassphraseRequest(),
|
|
|
|
proto.Success(message='random data')])
|
|
|
|
self._some_protected_call(True, True, True)
|
|
|
|
|
|
|
|
def scenario5():
|
|
|
|
with self.client:
|
|
|
|
# Scenario 5 - Failed message by field filter
|
|
|
|
self.client.set_expected_responses([proto.ButtonRequest(),
|
|
|
|
proto.Success(message='wrong data')])
|
|
|
|
self._some_protected_call(True, True, True)
|
2017-12-23 20:20:49 +00:00
|
|
|
with pytest.raises(CallException):
|
|
|
|
scenario5()
|
2014-06-04 15:59:16 +00:00
|
|
|
"""
|
2014-02-15 19:31:34 +00:00
|
|
|
|
2013-01-14 16:05:38 +00:00
|
|
|
def test_no_protection(self):
|
2014-02-17 00:54:54 +00:00
|
|
|
self.setup_mnemonic_nopin_nopassphrase()
|
|
|
|
|
2014-02-21 06:28:56 +00:00
|
|
|
with self.client:
|
2017-12-23 20:20:49 +00:00
|
|
|
assert self.client.debug.read_pin()[0] is None
|
2014-02-21 06:28:56 +00:00
|
|
|
self.client.set_expected_responses([proto.Success()])
|
|
|
|
self._some_protected_call(False, True, True)
|
2013-01-14 16:05:38 +00:00
|
|
|
|
2013-09-01 01:35:31 +00:00
|
|
|
def test_pin(self):
|
2014-02-17 00:54:54 +00:00
|
|
|
self.setup_mnemonic_pin_passphrase()
|
|
|
|
|
2014-02-21 06:28:56 +00:00
|
|
|
with self.client:
|
2017-12-23 20:20:49 +00:00
|
|
|
assert self.client.debug.read_pin()[0] == self.pin4
|
2014-02-21 06:28:56 +00:00
|
|
|
self.client.setup_debuglink(button=True, pin_correct=True)
|
2018-08-13 16:21:24 +00:00
|
|
|
self.client.set_expected_responses(
|
|
|
|
[proto.ButtonRequest(), proto.PinMatrixRequest(), proto.Success()]
|
|
|
|
)
|
2014-02-21 06:28:56 +00:00
|
|
|
self._some_protected_call(True, True, False)
|
2014-02-13 15:47:28 +00:00
|
|
|
|
2013-01-14 16:05:38 +00:00
|
|
|
def test_incorrect_pin(self):
|
2014-02-17 00:54:54 +00:00
|
|
|
self.setup_mnemonic_pin_passphrase()
|
2013-09-13 03:28:29 +00:00
|
|
|
self.client.setup_debuglink(button=True, pin_correct=False)
|
2017-12-23 20:20:49 +00:00
|
|
|
with pytest.raises(PinException):
|
|
|
|
self._some_protected_call(False, True, False)
|
2013-10-10 15:18:02 +00:00
|
|
|
|
|
|
|
def test_cancelled_pin(self):
|
2014-02-17 00:54:54 +00:00
|
|
|
self.setup_mnemonic_pin_passphrase()
|
2014-02-13 15:47:28 +00:00
|
|
|
self.client.setup_debuglink(button=True, pin_correct=False) # PIN cancel
|
2017-12-23 20:20:49 +00:00
|
|
|
with pytest.raises(PinException):
|
|
|
|
self._some_protected_call(False, True, False)
|
2013-10-11 01:51:45 +00:00
|
|
|
|
|
|
|
def test_exponential_backoff_with_reboot(self):
|
2014-02-17 00:54:54 +00:00
|
|
|
self.setup_mnemonic_pin_passphrase()
|
|
|
|
|
2013-10-11 01:51:45 +00:00
|
|
|
self.client.setup_debuglink(button=True, pin_correct=False)
|
2014-02-17 00:54:54 +00:00
|
|
|
|
2013-10-11 01:51:45 +00:00
|
|
|
def test_backoff(attempts, start):
|
2016-05-20 15:20:11 +00:00
|
|
|
if attempts <= 1:
|
2016-08-30 10:55:18 +00:00
|
|
|
expected = 0
|
2016-05-20 15:20:11 +00:00
|
|
|
else:
|
2017-12-16 21:37:21 +00:00
|
|
|
expected = (2 ** (attempts - 1)) - 1
|
2017-12-18 20:00:52 +00:00
|
|
|
got = round(time.time() - start, 2)
|
2013-10-11 01:51:45 +00:00
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
msg = "Pin delay expected to be at least %s seconds, got %s" % (
|
|
|
|
expected,
|
|
|
|
got,
|
|
|
|
)
|
2016-05-05 01:16:17 +00:00
|
|
|
print(msg)
|
2017-12-23 20:20:49 +00:00
|
|
|
assert got >= expected
|
2013-10-11 01:51:45 +00:00
|
|
|
|
2018-09-12 18:37:12 +00:00
|
|
|
for attempt in range(1, 4):
|
2013-10-11 01:51:45 +00:00
|
|
|
start = time.time()
|
2017-12-23 20:20:49 +00:00
|
|
|
with pytest.raises(PinException):
|
|
|
|
self._some_protected_call(False, True, False)
|
2013-10-11 01:51:45 +00:00
|
|
|
test_backoff(attempt, start)
|