Exemple #1
0
def test_set_wipe_code_mismatch(client):
    # Let's set a wipe code.
    def input_flow():
        yield  # do you want to set the wipe code?
        client.debug.press_yes()
        yield  # enter new wipe code
        client.debug.input(WIPE_CODE4)
        yield  # enter new wipe code again (but different)
        client.debug.input(WIPE_CODE6)

        # failed retry
        yield  # enter new wipe code
        client.cancel()

    with client, pytest.raises(Cancelled):
        client.set_expected_responses(
            [messages.ButtonRequest()] * 4 + [messages.Failure()]
        )
        client.set_input_flow(input_flow)

        device.change_wipe_code(client)

    # Check that there's still no wipe code protection now
    client.init_device()
    assert client.features.wipe_code_protection is False
Exemple #2
0
def test_set_wipe_code_to_pin(client):
    def input_flow():
        yield  # do you want to set the wipe code?
        client.debug.press_yes()
        yield  # enter current pin
        client.debug.input(PIN4)
        yield  # enter new wipe code (same as PIN)
        client.debug.input(PIN4)

        # failed retry
        yield  # enter new wipe code
        client.debug.input(WIPE_CODE4)
        yield  # enter new wipe code again
        client.debug.input(WIPE_CODE4)
        yield  # success
        client.debug.press_yes()

    with client:
        client.set_expected_responses(
            [messages.ButtonRequest()] * 6 + [messages.Success(), messages.Features()]
        )
        client.set_input_flow(input_flow)

        device.change_wipe_code(client)

    client.init_device()
    assert client.features.wipe_code_protection is True
    _check_wipe_code(client, PIN4, WIPE_CODE4)
def _set_wipe_code(client, wipe_code):
    # Set/change wipe code.
    with client:
        if client.features.pin_protection:
            pin, _ = client.debug.read_pin()
            pins = [pin, wipe_code, wipe_code]
            pin_matrices = [
                messages.PinMatrixRequest(type=PinType.Current),
                messages.PinMatrixRequest(type=PinType.WipeCodeFirst),
                messages.PinMatrixRequest(type=PinType.WipeCodeSecond),
            ]
        else:
            pins = [wipe_code, wipe_code]
            pin_matrices = [
                messages.PinMatrixRequest(type=PinType.WipeCodeFirst),
                messages.PinMatrixRequest(type=PinType.WipeCodeSecond),
            ]

        client.use_pin_sequence(pins)
        client.set_expected_responses(
            [messages.ButtonRequest()]
            + pin_matrices
            + [messages.Success(), messages.Features()]
        )
        device.change_wipe_code(client)
def test_set_remove_wipe_code(client: Client):
    # Check that wipe code protection status is not revealed in locked state.
    assert client.features.wipe_code_protection is None

    # Test set wipe code.
    _set_wipe_code(client, PIN4, WIPE_CODE_MAX)

    # Check that there's wipe code protection now.
    client.init_device()
    assert client.features.wipe_code_protection is True

    # Check that the wipe code is correct.
    _check_wipe_code(client, PIN4, WIPE_CODE_MAX)

    # Test change wipe code.
    _set_wipe_code(client, PIN4, WIPE_CODE6)

    # Check that there's still wipe code protection now.
    client.init_device()
    assert client.features.wipe_code_protection is True

    # Check that the wipe code is correct.
    _check_wipe_code(client, PIN4, WIPE_CODE6)

    # Test remove wipe code.
    with client:
        client.use_pin_sequence([PIN4])
        device.change_wipe_code(client, remove=True)

    # Check that there's no wipe code protection now.
    client.init_device()
    assert client.features.wipe_code_protection is False
Exemple #5
0
def setup_device_core(client, pin, wipe_code):
    device.wipe(client)
    debuglink.load_device(client,
                          MNEMONIC12,
                          pin,
                          passphrase_protection=False,
                          label="WIPECODE")

    def input_flow():
        yield  # do you want to set/change the wipe_code?
        client.debug.press_yes()
        if pin is not None:
            yield  # enter current pin
            client.debug.input(pin)
        yield  # enter new wipe code
        client.debug.input(wipe_code)
        yield  # enter new wipe code again
        client.debug.input(wipe_code)
        yield  # success
        client.debug.press_yes()

    with client:
        client.set_expected_responses(
            [messages.ButtonRequest()] * 5 +
            [messages.Success(), messages.Features()])
        client.set_input_flow(input_flow)
        device.change_wipe_code(client)
Exemple #6
0
def setup_device_legacy(client, pin, wipe_code):
    device.wipe(client)
    debuglink.load_device(
        client, MNEMONIC12, pin, passphrase_protection=False, label="WIPECODE"
    )

    with client:
        client.use_pin_sequence([PIN, WIPE_CODE, WIPE_CODE])
        device.change_wipe_code(client)
Exemple #7
0
def test_set_wipe_code_to_pin(client):
    _ensure_unlocked(client, PIN4)

    with client:
        client.set_expected_responses([messages.ButtonRequest()] * 6 +
                                      [messages.Success, messages.Features])
        client.use_pin_sequence([PIN4, PIN4, WIPE_CODE4, WIPE_CODE4])
        device.change_wipe_code(client)

    client.init_device()
    assert client.features.wipe_code_protection is True
    _check_wipe_code(client, PIN4, WIPE_CODE4)
Exemple #8
0
def set_wipe_code(hw_client, remove=False):
    if hw_client:
        try:
            device.change_wipe_code(hw_client, remove)
        except exceptions.Cancelled:
            pass
        except exceptions.TrezorFailure as e:
            if e.failure.message == 'Device not initialized':
                raise HwNotInitialized(e.failure.message)
            else:
                raise
    else:
        raise Exception('HW client not set.')
Exemple #9
0
def test_set_pin_to_wipe_code(client):
    # Set wipe code.
    with client:
        client.set_expected_responses([messages.ButtonRequest()] * 4 +
                                      [messages.Success, messages.Features])
        client.use_pin_sequence([WIPE_CODE4, WIPE_CODE4])
        device.change_wipe_code(client)

    # Try to set the PIN to the current wipe code value.
    with client, pytest.raises(TrezorFailure):
        client.set_expected_responses(
            [messages.ButtonRequest()] * 4 +
            [messages.Failure(code=messages.FailureType.PinInvalid)])
        client.use_pin_sequence([WIPE_CODE4, WIPE_CODE4])
        device.change_pin(client)
Exemple #10
0
def test_set_pin_to_wipe_code(client):
    # Set wipe code.
    with client:
        client.set_expected_responses(
            [messages.ButtonRequest()] * 4 + [messages.Success(), messages.Features()]
        )
        client.set_input_flow(_input_flow_set_wipe_code(client.debug, None, WIPE_CODE4))

        device.change_wipe_code(client)

    # Try to set the PIN to the current wipe code value.
    with client, pytest.raises(TrezorFailure):
        client.set_expected_responses(
            [messages.ButtonRequest()] * 4
            + [messages.Failure(code=messages.FailureType.PinInvalid)]
        )
        client.set_input_flow(_input_flow_set_pin(client.debug, WIPE_CODE4))
        device.change_pin(client)
def test_set_wipe_code_to_pin(client: Client):
    # Check that wipe code protection status is not revealed in locked state.
    assert client.features.wipe_code_protection is None

    # Let's try setting the wipe code to the curent PIN value.
    with client:
        client.use_pin_sequence([PIN4, PIN4])
        client.set_expected_responses([
            messages.ButtonRequest(),
            messages.PinMatrixRequest(type=PinType.Current),
            messages.PinMatrixRequest(type=PinType.WipeCodeFirst),
            messages.Failure(code=messages.FailureType.ProcessError),
        ])
        with pytest.raises(exceptions.TrezorFailure):
            device.change_wipe_code(client)

    # Check that there is no wipe code protection.
    client.init_device()
    assert client.features.wipe_code_protection is False
Exemple #12
0
def test_set_wipe_code_mismatch(client):
    # Check that there is no wipe code protection.
    assert client.features.wipe_code_protection is False

    # Let's set a new wipe code.
    with client:
        client.use_pin_sequence([WIPE_CODE4, WIPE_CODE6])
        client.set_expected_responses([
            messages.ButtonRequest(),
            messages.PinMatrixRequest(type=PinType.WipeCodeFirst),
            messages.PinMatrixRequest(type=PinType.WipeCodeSecond),
            messages.Failure(code=messages.FailureType.WipeCodeMismatch),
        ])
        with pytest.raises(exceptions.TrezorFailure):
            device.change_wipe_code(client)

    # Check that there is no wipe code protection.
    client.init_device()
    assert client.features.wipe_code_protection is False
Exemple #13
0
def test_upgrade_wipe_code(gen, tag):
    PIN = "1234"
    WIPE_CODE = "4321"

    def asserts(client):
        assert client.features.pin_protection
        assert not client.features.passphrase_protection
        assert client.features.initialized
        assert client.features.label == LABEL
        client.use_pin_sequence([PIN])
        assert btc.get_address(client, "Bitcoin", PATH) == ADDRESS

    with EmulatorWrapper(gen, tag) as emu:
        debuglink.load_device_by_mnemonic(
            emu.client,
            mnemonic=MNEMONIC,
            pin=PIN,
            passphrase_protection=False,
            label=LABEL,
            language=LANGUAGE,
        )

        # Set wipe code.
        emu.client.use_pin_sequence([PIN, WIPE_CODE, WIPE_CODE])
        device.change_wipe_code(emu.client)

        device_id = emu.client.features.device_id
        asserts(emu.client)
        storage = emu.get_storage()

    with EmulatorWrapper(gen, storage=storage) as emu:
        assert device_id == emu.client.features.device_id
        asserts(emu.client)
        assert emu.client.features.language == LANGUAGE

        # Check that wipe code is set by changing the PIN to it.
        emu.client.use_pin_sequence([PIN, WIPE_CODE, WIPE_CODE])
        with pytest.raises(
                exceptions.TrezorFailure,
                match="The new PIN must be different from your wipe code",
        ):
            return device.change_pin(emu.client)
def test_wipe_code_activate(client):
    import time

    device_id = client.features.device_id

    # Set wipe code.
    with client:
        client.set_expected_responses(
            [messages.ButtonRequest()] * 5 +
            [messages.Success(), messages.Features()])
        client.set_input_flow(
            _input_flow_set_wipe_code(client.debug, PIN4, WIPE_CODE4))

        device.change_wipe_code(client)

    # Try to change the PIN.
    ret = client.call_raw(messages.ChangePin(remove=False))

    # Confirm change PIN.
    assert isinstance(ret, messages.ButtonRequest)
    client.debug.press_yes()
    ret = client.call_raw(messages.ButtonAck())

    # Enter the wipe code instead of the current PIN
    assert ret == messages.ButtonRequest(code=messages.ButtonRequestType.Other)
    client.debug.input(WIPE_CODE4)
    client._raw_write(messages.ButtonAck())

    # Allow the device to display wipe code popup and restart.
    time.sleep(7)

    # Check that the device has been wiped.
    client.init_device()
    assert client.features.initialized is False
    assert client.features.pin_protection is False
    assert client.features.wipe_code_protection is False
    assert client.features.device_id != device_id
def test_set_remove_wipe_code(client):
    # Test set wipe code.
    assert client.features.wipe_code_protection is False

    with client:
        client.set_expected_responses(
            [messages.ButtonRequest()] * 5 +
            [messages.Success(), messages.Features()])
        client.set_input_flow(
            _input_flow_set_wipe_code(client.debug, PIN4, WIPE_CODE4))

        device.change_wipe_code(client)

    client.init_device()
    assert client.features.wipe_code_protection is True
    _check_wipe_code(client, PIN4, WIPE_CODE4)

    # Test change wipe code.
    with client:
        client.set_expected_responses(
            [messages.ButtonRequest()] * 5 +
            [messages.Success(), messages.Features()])
        client.set_input_flow(
            _input_flow_set_wipe_code(client.debug, PIN4, WIPE_CODE6))

        device.change_wipe_code(client)

    client.init_device()
    assert client.features.wipe_code_protection is True
    _check_wipe_code(client, PIN4, WIPE_CODE6)

    # Test remove wipe code.
    with client:
        client.set_expected_responses(
            [messages.ButtonRequest()] * 3 +
            [messages.Success(), messages.Features()])
        client.set_input_flow(_input_flow_clear_pin(client.debug, PIN4))

        device.change_wipe_code(client, remove=True)

    client.init_device()
    assert client.features.wipe_code_protection is False
def test_set_remove_wipe_code(client):
    # Test set wipe code.
    assert client.features.wipe_code_protection is None

    _ensure_unlocked(client, PIN4)
    assert client.features.wipe_code_protection is False

    with client:
        client.set_expected_responses(
            [messages.ButtonRequest()] * 5 +
            [messages.Success(), messages.Features()])
        client.use_pin_sequence([PIN4, WIPE_CODE4, WIPE_CODE4])
        device.change_wipe_code(client)

    client.init_device()
    assert client.features.wipe_code_protection is True
    _check_wipe_code(client, PIN4, WIPE_CODE4)

    # Test change wipe code.
    with client:
        client.set_expected_responses(
            [messages.ButtonRequest()] * 5 +
            [messages.Success(), messages.Features()])
        client.use_pin_sequence([PIN4, WIPE_CODE6, WIPE_CODE6])
        device.change_wipe_code(client)

    client.init_device()
    assert client.features.wipe_code_protection is True
    _check_wipe_code(client, PIN4, WIPE_CODE6)

    # Test remove wipe code.
    with client:
        client.set_expected_responses(
            [messages.ButtonRequest()] * 3 +
            [messages.Success(), messages.Features()])
        client.use_pin_sequence([PIN4])
        device.change_wipe_code(client, remove=True)

    client.init_device()
    assert client.features.wipe_code_protection is False