Skip to content

Commit

Permalink
nk3: Improve error message for missing confirmation
Browse files Browse the repository at this point in the history
This patch improves the error message shown if the user does not confirm
a reboot operation with a touch button press.

Fixes Nitrokey/pynitrokey#173
  • Loading branch information
robin-nitrokey committed Feb 3, 2022
1 parent e3c3834 commit 729b1d5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
17 changes: 15 additions & 2 deletions pynitrokey/cli/nk3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
check_firmware_image,
)
from pynitrokey.nk3.device import BootMode, Nitrokey3Device
from pynitrokey.nk3.exceptions import TimeoutException
from pynitrokey.nk3.updates import get_latest_update, get_update
from pynitrokey.nk3.utils import Version

Expand Down Expand Up @@ -117,7 +118,13 @@ def reboot(ctx: Context, bootloader: bool) -> None:
local_print(
"Please press the touch button to reboot the device into bootloader mode ..."
)
device.reboot(BootMode.BOOTROM)
try:
device.reboot(BootMode.BOOTROM)
except TimeoutException:
local_critical(
"The reboot was not confirmed with the touch button.",
support_hint=False,
)
else:
local_critical(
"A Nitrokey 3 device in bootloader mode can only reboot into firmware mode."
Expand Down Expand Up @@ -354,7 +361,13 @@ def update(ctx: Context, image: Optional[str], experimental: bool) -> None:
local_print(
"Please press the touch button to reboot the device into bootloader mode ..."
)
device.reboot(BootMode.BOOTROM)
try:
device.reboot(BootMode.BOOTROM)
except TimeoutException:
local_critical(
"The reboot was not confirmed with the touch button.",
support_hint=False,
)

local_print("")

Expand Down
12 changes: 11 additions & 1 deletion pynitrokey/nk3/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
from enum import Enum
from typing import List, Optional

from fido2.ctap import CtapError
from fido2.hid import CtapHidDevice, open_device

from pynitrokey.fido2 import device_path_to_str

from .base import Nitrokey3Base
from .exceptions import TimeoutException
from .utils import Version

RNG_LEN = 57
Expand Down Expand Up @@ -77,7 +79,15 @@ def reboot(self, mode: BootMode = BootMode.FIRMWARE) -> None:
if mode == BootMode.FIRMWARE:
self._call(Command.REBOOT)
elif mode == BootMode.BOOTROM:
self._call(Command.UPDATE)
try:
self._call(Command.UPDATE)
except CtapError as e:
# The admin app returns an Invalid Length error if the user confirmation
# request times out
if e.code == CtapError.ERR.INVALID_LENGTH:
raise TimeoutException()
else:
raise e
except OSError as e:
# OS error is expected as the device does not respond during the reboot
self.logger.debug("ignoring OSError after reboot", exc_info=e)
Expand Down
17 changes: 17 additions & 0 deletions pynitrokey/nk3/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
#
# Copyright 2022 Nitrokey Developers
#
# Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
# http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
# http://opensource.org/licenses/MIT>, at your option. This file may not be
# copied, modified, or distributed except according to those terms.


class Nitrokey3Exception(Exception):
pass


class TimeoutException(Nitrokey3Exception):
def __init__(self) -> None:
super().__init__("The user confirmation request timed out")
11 changes: 10 additions & 1 deletion pynitrokey/stubs/fido2/ctap.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,14 @@
# http://opensource.org/licenses/MIT>, at your option. This file may not be
# copied, modified, or distributed except according to those terms.

from enum import IntEnum, unique
from typing import Union

class CtapDevice: ...
class CtapError(Exception): ...

class CtapError(Exception):
class UNKNOWN_ERR(int): ...
@unique
class ERR(IntEnum):
INVALID_LENGTH: int
code: Union[UNKNOWN_ERR, ERR]

0 comments on commit 729b1d5

Please sign in to comment.