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
|
|
|
|
2017-12-12 15:40:11 +00:00
|
|
|
from trezorlib import messages as proto
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2019-08-27 14:58:59 +00:00
|
|
|
from .common import MNEMONIC12, TrezorTest
|
2018-08-13 16:21:24 +00:00
|
|
|
|
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):
|
2019-08-27 14:58:59 +00:00
|
|
|
@pytest.mark.setup_client(mnemonic=MNEMONIC12)
|
|
|
|
def test_set_pin(self, client):
|
|
|
|
features = 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
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = 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
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = 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
|
2019-08-27 14:58:59 +00:00
|
|
|
client.debug.press_yes()
|
|
|
|
ret = client.call_raw(proto.ButtonAck())
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Send the PIN for first time
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2019-08-27 14:58:59 +00:00
|
|
|
pin_encoded = client.debug.encode_pin(self.pin6)
|
|
|
|
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Send the PIN for second time
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2019-08-27 14:58:59 +00:00
|
|
|
pin_encoded = client.debug.encode_pin(self.pin6)
|
|
|
|
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# 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
|
2019-08-27 14:58:59 +00:00
|
|
|
features = 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
|
2019-08-27 14:58:59 +00:00
|
|
|
self.check_pin(client, self.pin6)
|
2014-02-21 03:46:33 +00:00
|
|
|
|
2019-08-27 14:58:59 +00:00
|
|
|
@pytest.mark.setup_client(mnemonic=MNEMONIC12, pin=True, passphrase=True)
|
|
|
|
def test_change_pin(self, client):
|
|
|
|
features = 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
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = client.call_raw(proto.Ping(pin_protection=True))
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2019-08-27 14:58:59 +00:00
|
|
|
client.call_raw(proto.Cancel())
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Check current PIN value
|
2019-08-27 14:58:59 +00:00
|
|
|
self.check_pin(client, self.pin4)
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Let's change PIN
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = 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
|
2019-08-27 14:58:59 +00:00
|
|
|
client.debug.press_yes()
|
|
|
|
ret = client.call_raw(proto.ButtonAck())
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Send current PIN
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2019-08-27 14:58:59 +00:00
|
|
|
pin_encoded = client.debug.read_pin_encoded()
|
|
|
|
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Send new PIN for first time
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2019-08-27 14:58:59 +00:00
|
|
|
pin_encoded = client.debug.encode_pin(self.pin6)
|
|
|
|
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Send the PIN for second time
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2019-08-27 14:58:59 +00:00
|
|
|
pin_encoded = client.debug.encode_pin(self.pin6)
|
|
|
|
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# 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
|
2019-08-27 14:58:59 +00:00
|
|
|
features = 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
|
2019-08-27 14:58:59 +00:00
|
|
|
self.check_pin(client, self.pin6)
|
2014-02-21 03:46:33 +00:00
|
|
|
|
2019-08-27 14:58:59 +00:00
|
|
|
@pytest.mark.setup_client(mnemonic=MNEMONIC12, pin=True, passphrase=True)
|
|
|
|
def test_remove_pin(self, client):
|
|
|
|
features = 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
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = client.call_raw(proto.Ping(pin_protection=True))
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2019-08-27 14:58:59 +00:00
|
|
|
client.call_raw(proto.Cancel())
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Let's remove PIN
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = 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
|
2019-08-27 14:58:59 +00:00
|
|
|
client.debug.press_yes()
|
|
|
|
ret = client.call_raw(proto.ButtonAck())
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# Send current PIN
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2019-08-27 14:58:59 +00:00
|
|
|
pin_encoded = client.debug.read_pin_encoded()
|
|
|
|
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
2014-02-21 03:46:33 +00:00
|
|
|
|
|
|
|
# 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
|
2019-08-27 14:58:59 +00:00
|
|
|
features = client.call_raw(proto.Initialize())
|
2017-12-23 20:20:49 +00:00
|
|
|
assert features.pin_protection is False
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = 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
|
|
|
|
2019-08-27 14:58:59 +00:00
|
|
|
@pytest.mark.setup_client(mnemonic=MNEMONIC12)
|
|
|
|
def test_set_failed(self, client):
|
|
|
|
features = 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
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = 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
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = 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
|
2019-08-27 14:58:59 +00:00
|
|
|
client.debug.press_yes()
|
|
|
|
ret = client.call_raw(proto.ButtonAck())
|
2014-02-21 21:22:19 +00:00
|
|
|
|
|
|
|
# Send the PIN for first time
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2019-08-27 14:58:59 +00:00
|
|
|
pin_encoded = client.debug.encode_pin(self.pin6)
|
|
|
|
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
2014-02-21 21:22:19 +00:00
|
|
|
|
|
|
|
# Send the PIN for second time, but with typo
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2019-08-27 14:58:59 +00:00
|
|
|
pin_encoded = client.debug.encode_pin(self.pin4)
|
|
|
|
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
2014-02-21 21:22:19 +00:00
|
|
|
|
|
|
|
# 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
|
2019-08-27 14:58:59 +00:00
|
|
|
features = client.call_raw(proto.Initialize())
|
2017-12-23 20:20:49 +00:00
|
|
|
assert features.pin_protection is False
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = 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
|
|
|
|
2019-08-27 14:58:59 +00:00
|
|
|
@pytest.mark.setup_client(mnemonic=MNEMONIC12, pin=True, passphrase=True)
|
|
|
|
def test_set_failed_2(self, client):
|
|
|
|
features = 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
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = 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
|
2019-08-27 14:58:59 +00:00
|
|
|
client.debug.press_yes()
|
|
|
|
ret = client.call_raw(proto.ButtonAck())
|
2014-03-02 23:29:17 +00:00
|
|
|
|
|
|
|
# Send current PIN
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2019-08-27 14:58:59 +00:00
|
|
|
pin_encoded = client.debug.read_pin_encoded()
|
|
|
|
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
2014-03-02 23:29:17 +00:00
|
|
|
|
|
|
|
# Send the PIN for first time
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2019-08-27 14:58:59 +00:00
|
|
|
pin_encoded = client.debug.encode_pin(self.pin6)
|
|
|
|
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
2014-03-02 23:29:17 +00:00
|
|
|
|
|
|
|
# Send the PIN for second time, but with typo
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2019-08-27 14:58:59 +00:00
|
|
|
pin_encoded = client.debug.encode_pin(self.pin6 + "3")
|
|
|
|
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
2014-03-02 23:29:17 +00:00
|
|
|
|
|
|
|
# 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
|
2019-08-27 14:58:59 +00:00
|
|
|
features = client.call_raw(proto.Initialize())
|
2017-12-23 20:20:49 +00:00
|
|
|
assert features.pin_protection is True
|
2019-08-27 14:58:59 +00:00
|
|
|
self.check_pin(client, self.pin4)
|
2019-02-14 18:16:43 +00:00
|
|
|
|
2019-08-27 14:58:59 +00:00
|
|
|
def check_pin(self, client, pin):
|
|
|
|
client.clear_session()
|
|
|
|
ret = client.call_raw(proto.Ping(pin_protection=True))
|
2019-02-14 18:16:43 +00:00
|
|
|
assert isinstance(ret, proto.PinMatrixRequest)
|
2019-08-27 14:58:59 +00:00
|
|
|
pin_encoded = client.debug.encode_pin(pin)
|
|
|
|
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
|
2019-02-14 18:16:43 +00:00
|
|
|
assert isinstance(ret, proto.Success)
|