Skip to content

Commit

Permalink
Open only CtapHid device for the respective Nitrokey
Browse files Browse the repository at this point in the history
  • Loading branch information
mmerklinger committed Jan 14, 2025
1 parent 1249428 commit d1aadf4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
32 changes: 31 additions & 1 deletion src/nitrokey/nk3/_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
# http://opensource.org/licenses/MIT>, at your option. This file may not be
# copied, modified, or distributed except according to those terms.

from fido2.hid import CtapHidDevice
import logging
import warnings
from typing import List

from fido2.hid import CtapHidDevice, open_device

from nitrokey import _VID_NITROKEY
from nitrokey.trussed import Fido2Certs, TrussedDevice, Version

FIDO2_CERTS = [
Expand All @@ -26,6 +31,8 @@
),
]

logger = logging.getLogger(__name__)


class NK3(TrussedDevice):
"""A Nitrokey 3 device running the firmware."""
Expand All @@ -46,3 +53,26 @@ def name(self) -> str:
@classmethod
def from_device(cls, device: CtapHidDevice) -> "NK3":
return cls(device)

@classmethod
def list(cls) -> List["NK3"]:
from . import _PID_NK3_DEVICE

try:
import hid
except ImportError as err:
logger.warning("Failed to import hid module", exc_info=True)
warnings.warn(
f"Failed to list NK3s due to a missing library: {err}",
category=RuntimeWarning,
)
return []

devices = []

# iterate on all devices found and open them
for device in hid.enumerate(
vendor_id=_VID_NITROKEY, product_id=_PID_NK3_DEVICE
):
devices.append(cls.from_device(open_device(device["path"])))
return devices
27 changes: 26 additions & 1 deletion src/nitrokey/nkpk.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
# http://opensource.org/licenses/MIT>, at your option. This file may not be
# copied, modified, or distributed except according to those terms.

import logging
import warnings
from typing import List, Optional, Sequence, Union

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

from nitrokey import _VID_NITROKEY
from nitrokey.trussed import Fido2Certs, TrussedDevice, Version
Expand Down Expand Up @@ -43,6 +45,8 @@
],
)

logger = logging.getLogger(__name__)


class NKPK(TrussedDevice):
def __init__(self, device: CtapHidDevice) -> None:
Expand All @@ -60,6 +64,27 @@ def name(self) -> str:
def from_device(cls, device: CtapHidDevice) -> "NKPK":
return cls(device)

@classmethod
def list(cls) -> List["NKPK"]:
try:
import hid
except ImportError as err:
logger.warning("Failed to import hid module", exc_info=True)
warnings.warn(
f"Failed to list NKPKs due to a missing library: {err}",
category=RuntimeWarning,
)
return []

devices = []

# iterate on all devices found and open them
for device in hid.enumerate(
vendor_id=_VID_NITROKEY, product_id=_PID_NKPK_DEVICE
):
devices.append(cls.from_device(open_device(device["path"])))
return devices


class NKPKBootloader(TrussedBootloaderNrf52):
@property
Expand Down
13 changes: 3 additions & 10 deletions src/nitrokey/trussed/_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import sys
from abc import abstractmethod
from enum import Enum
from typing import Optional, Sequence, TypeVar, Union
from typing import List, Optional, Sequence, TypeVar, Union

from fido2.hid import CtapHidDevice, open_device

Expand Down Expand Up @@ -110,15 +110,8 @@ def open(cls: type[T], path: str) -> Optional[T]:
return None

@classmethod
def list(cls: type[T]) -> list[T]:
devices = []
for device in CtapHidDevice.list_devices():
try:
devices.append(cls.from_device(device))
except ValueError:
# not the correct device type, skip
pass
return devices
@abstractmethod
def list(cls: type[T]) -> List[T]: ...


def _device_path_to_str(path: Union[bytes, str]) -> str:
Expand Down

0 comments on commit d1aadf4

Please sign in to comment.