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>.
|
|
|
|
|
2018-05-11 12:53:51 +00:00
|
|
|
import pytest
|
2017-01-03 18:40:05 +00:00
|
|
|
|
2017-12-12 15:40:11 +00:00
|
|
|
from trezorlib import messages as proto
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
from .common import TrezorTest
|
|
|
|
|
2014-02-21 03:46:33 +00:00
|
|
|
|
2017-12-19 18:24:18 +00:00
|
|
|
@pytest.mark.skip_t2
|
2017-12-23 20:20:49 +00:00
|
|
|
class TestMsgChangepin(TrezorTest):
|
2014-02-21 03:46:33 +00:00
|
|
|
def test_set_pin(self):
|
|
|
|
self.setup_mnemonic_nopin_nopassphrase()
|
|
|
|
features = self.client.call_raw(proto.Initialize())
|
2017-12-23 20:20:49 +00:00
|
|
|
assert features.pin_protection is False
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Check that there's no PIN protection
|
|
|
|
ret = self.client.call_raw(proto.Ping(pin_protection=True))
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.Success)
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Let's set new PIN
|
|
|
|
ret = self.client.call_raw(proto.ChangePin())
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.ButtonRequest)
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Press button
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
ret = self.client.call_raw(proto.ButtonAck())
|
|
|
|
|
|
|
|
# Send the PIN for first time
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2014-02-21 03:46:33 +00:00
|
|
|
pin_encoded = self.client.debug.encode_pin(self.pin6)
|
|
|
|
ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
|
|
|
|
|
|
|
# Send the PIN for second time
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2014-02-21 03:46:33 +00:00
|
|
|
pin_encoded = self.client.debug.encode_pin(self.pin6)
|
|
|
|
ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
|
|
|
|
|
|
|
# Now we're done
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.Success)
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Check that there's PIN protection now
|
|
|
|
features = self.client.call_raw(proto.Initialize())
|
2017-12-23 20:20:49 +00:00
|
|
|
assert features.pin_protection is True
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Check that the PIN is correct
|
2017-12-23 20:20:49 +00:00
|
|
|
assert self.client.debug.read_pin()[0] == self.pin6
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
def test_change_pin(self):
|
|
|
|
self.setup_mnemonic_pin_passphrase()
|
|
|
|
features = self.client.call_raw(proto.Initialize())
|
2017-12-23 20:20:49 +00:00
|
|
|
assert features.pin_protection is True
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Check that there's PIN protection
|
|
|
|
ret = self.client.call_raw(proto.Ping(pin_protection=True))
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2014-02-21 03:46:33 +00:00
|
|
|
self.client.call_raw(proto.Cancel())
|
|
|
|
|
|
|
|
# Check current PIN value
|
2017-12-23 20:20:49 +00:00
|
|
|
assert self.client.debug.read_pin()[0] == self.pin4
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Let's change PIN
|
|
|
|
ret = self.client.call_raw(proto.ChangePin())
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.ButtonRequest)
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Press button
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
ret = self.client.call_raw(proto.ButtonAck())
|
|
|
|
|
|
|
|
# Send current PIN
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2014-02-21 03:46:33 +00:00
|
|
|
pin_encoded = self.client.debug.read_pin_encoded()
|
|
|
|
ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
|
|
|
|
|
|
|
# Send new PIN for first time
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2014-02-21 03:46:33 +00:00
|
|
|
pin_encoded = self.client.debug.encode_pin(self.pin6)
|
|
|
|
ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
|
|
|
|
|
|
|
# Send the PIN for second time
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2014-02-21 03:46:33 +00:00
|
|
|
pin_encoded = self.client.debug.encode_pin(self.pin6)
|
|
|
|
ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
|
|
|
|
|
|
|
# Now we're done
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.Success)
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Check that there's still PIN protection now
|
|
|
|
features = self.client.call_raw(proto.Initialize())
|
2017-12-23 20:20:49 +00:00
|
|
|
assert features.pin_protection is True
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Check that the PIN is correct
|
2017-12-23 20:20:49 +00:00
|
|
|
assert self.client.debug.read_pin()[0] == self.pin6
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
def test_remove_pin(self):
|
|
|
|
self.setup_mnemonic_pin_passphrase()
|
|
|
|
features = self.client.call_raw(proto.Initialize())
|
2017-12-23 20:20:49 +00:00
|
|
|
assert features.pin_protection is True
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Check that there's PIN protection
|
|
|
|
ret = self.client.call_raw(proto.Ping(pin_protection=True))
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2014-02-21 03:46:33 +00:00
|
|
|
self.client.call_raw(proto.Cancel())
|
|
|
|
|
|
|
|
# Let's remove PIN
|
|
|
|
ret = self.client.call_raw(proto.ChangePin(remove=True))
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.ButtonRequest)
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Press button
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
ret = self.client.call_raw(proto.ButtonAck())
|
|
|
|
|
|
|
|
# Send current PIN
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2014-02-21 03:46:33 +00:00
|
|
|
pin_encoded = self.client.debug.read_pin_encoded()
|
|
|
|
ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
|
|
|
|
|
|
|
# Now we're done
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.Success)
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Check that there's no PIN protection now
|
|
|
|
features = self.client.call_raw(proto.Initialize())
|
2017-12-23 20:20:49 +00:00
|
|
|
assert features.pin_protection is False
|
2014-02-21 03:46:33 +00:00
|
|
|
ret = self.client.call_raw(proto.Ping(pin_protection=True))
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.Success)
|
2014-03-02 23:29:17 +00:00
|
|
|
|
2014-02-21 21:22:19 +00:00
|
|
|
def test_set_failed(self):
|
|
|
|
self.setup_mnemonic_nopin_nopassphrase()
|
|
|
|
features = self.client.call_raw(proto.Initialize())
|
2017-12-23 20:20:49 +00:00
|
|
|
assert features.pin_protection is False
|
2014-02-21 21:22:19 +00:00
|
|
|
|
|
|
|
# Check that there's no PIN protection
|
|
|
|
ret = self.client.call_raw(proto.Ping(pin_protection=True))
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.Success)
|
2014-02-21 21:22:19 +00:00
|
|
|
|
|
|
|
# Let's set new PIN
|
|
|
|
ret = self.client.call_raw(proto.ChangePin())
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.ButtonRequest)
|
2014-02-21 21:22:19 +00:00
|
|
|
|
|
|
|
# Press button
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
ret = self.client.call_raw(proto.ButtonAck())
|
|
|
|
|
|
|
|
# Send the PIN for first time
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2014-02-21 21:22:19 +00:00
|
|
|
pin_encoded = self.client.debug.encode_pin(self.pin6)
|
|
|
|
ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
|
|
|
|
|
|
|
# Send the PIN for second time, but with typo
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2014-02-21 21:22:19 +00:00
|
|
|
pin_encoded = self.client.debug.encode_pin(self.pin4)
|
|
|
|
ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
|
|
|
|
|
|
|
# Now it should fail, because pins are different
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.Failure)
|
2014-02-21 21:22:19 +00:00
|
|
|
|
|
|
|
# Check that there's still no PIN protection now
|
|
|
|
features = self.client.call_raw(proto.Initialize())
|
2017-12-23 20:20:49 +00:00
|
|
|
assert features.pin_protection is False
|
2014-02-21 21:22:19 +00:00
|
|
|
ret = self.client.call_raw(proto.Ping(pin_protection=True))
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.Success)
|
2014-02-21 21:22:19 +00:00
|
|
|
|
2014-03-02 23:29:17 +00:00
|
|
|
def test_set_failed_2(self):
|
|
|
|
self.setup_mnemonic_pin_passphrase()
|
|
|
|
features = self.client.call_raw(proto.Initialize())
|
2017-12-23 20:20:49 +00:00
|
|
|
assert features.pin_protection is True
|
2014-03-02 23:29:17 +00:00
|
|
|
|
|
|
|
# Let's set new PIN
|
|
|
|
ret = self.client.call_raw(proto.ChangePin())
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.ButtonRequest)
|
2014-03-02 23:29:17 +00:00
|
|
|
|
|
|
|
# Press button
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
ret = self.client.call_raw(proto.ButtonAck())
|
|
|
|
|
|
|
|
# Send current PIN
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2014-03-02 23:29:17 +00:00
|
|
|
pin_encoded = self.client.debug.read_pin_encoded()
|
|
|
|
ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
|
|
|
|
|
|
|
# Send the PIN for first time
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2014-03-02 23:29:17 +00:00
|
|
|
pin_encoded = self.client.debug.encode_pin(self.pin6)
|
|
|
|
ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
|
|
|
|
|
|
|
# Send the PIN for second time, but with typo
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2018-08-13 16:21:24 +00:00
|
|
|
pin_encoded = self.client.debug.encode_pin(self.pin6 + "3")
|
2014-03-02 23:29:17 +00:00
|
|
|
ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
|
|
|
|
|
|
|
# Now it should fail, because pins are different
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.Failure)
|
2014-03-02 23:29:17 +00:00
|
|
|
|
|
|
|
# Check that there's still old PIN protection
|
|
|
|
features = self.client.call_raw(proto.Initialize())
|
2017-12-23 20:20:49 +00:00
|
|
|
assert features.pin_protection is True
|
|
|
|
assert self.client.debug.read_pin()[0] == self.pin4
|