| 1 |
# Copyright 2021-2024 Nitrokey Developers |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or |
| 4 |
# http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or |
| 5 |
# http://opensource.org/licenses/MIT>, at your option. This file may not be |
| 6 |
# copied, modified, or distributed except according to those terms. |
| 7 |
|
| 8 |
import ctypes |
| 9 |
import sys |
| 10 |
from importlib.util import find_spec |
| 11 |
from typing import List, Optional |
| 12 |
|
| 13 |
from ._base import Model as Model |
| 14 |
from ._base import TrussedBase as TrussedBase |
| 15 |
from ._bootloader import FirmwareContainer as FirmwareContainer |
| 16 |
from ._bootloader import FirmwareMetadata as FirmwareMetadata |
| 17 |
from ._bootloader import TrussedBootloader as TrussedBootloader |
| 18 |
from ._bootloader import Variant as Variant |
| 19 |
from ._bootloader import parse_firmware_image as parse_firmware_image |
| 20 |
from ._device import App as App |
| 21 |
from ._device import TrussedDevice as TrussedDevice |
| 22 |
from ._exceptions import TimeoutException as TimeoutException |
| 23 |
from ._exceptions import TrussedException as TrussedException |
| 24 |
from ._utils import Fido2Certs as Fido2Certs |
| 25 |
from ._utils import Uuid as Uuid |
| 26 |
from ._utils import Version as Version |
| 27 |
|
| 28 |
|
| 29 |
def should_default_ccid() -> bool: |
| 30 |
"""Helper function to inform whether CCID should be the default communication protocol |
| 31 |
|
| 32 |
Some features do not work over CCID, therefore it is only used when CTAPHID is not available, meaning on windows when not an administrator""" |
| 33 |
if find_spec("smartcard") is None: |
| 34 |
return False |
| 35 |
|
| 36 |
if sys.platform != "win32" and sys.platform != "cygwin": |
| 37 |
# Linux or MacOS don't need admin to access with CTAPHID |
| 38 |
return False |
| 39 |
|
| 40 |
try: |
| 41 |
if ctypes.windll.shell32.IsUserAnAdmin(): |
| 42 |
return False |
| 43 |
else: |
| 44 |
return True |
| 45 |
except Exception: |
| 46 |
return False |
| 47 |
|
| 48 |
|
| 49 |
def list( |
| 50 |
*, use_ccid: bool = False, model: Optional[Model] = None, exclusive: bool = True |
| 51 |
) -> List[TrussedBase]: |
| 52 |
devices: List[TrussedBase] = [] |
| 53 |
|
| 54 |
if model is None or model == Model.NK3: |
| 55 |
from nitrokey import nk3 |
| 56 |
|
| 57 |
devices.extend(nk3.list(use_ccid, exclusive)) |
| 58 |
|
| 59 |
if model is None or model == Model.NKPK: |
| 60 |
from nitrokey import nkpk |
| 61 |
|
| 62 |
devices.extend(nkpk.list(use_ccid, exclusive)) |
| 63 |
|
| 64 |
return devices |
| 65 |
|
| 66 |
|
| 67 |
def open(path: str, *, model: Optional[Model] = None) -> Optional[TrussedBase]: |
| 68 |
devices: List[TrussedBase] = [] |
| 69 |
|
| 70 |
if model is None or model == Model.NK3: |
| 71 |
from nitrokey import nk3 |
| 72 |
|
| 73 |
nk3_device = nk3.open(path) |
| 74 |
if nk3_device is not None: |
| 75 |
devices.append(nk3_device) |
| 76 |
|
| 77 |
if model is None or model == Model.NKPK: |
| 78 |
from nitrokey import nkpk |
| 79 |
|
| 80 |
nkpk_device = nkpk.open(path) |
| 81 |
if nkpk_device is not None: |
| 82 |
devices.append(nkpk_device) |
| 83 |
|
| 84 |
if len(devices) > 1: |
| 85 |
raise Exception(f"Found multiple devices at path {path}") |
| 86 |
if len(devices) == 1: |
| 87 |
return devices[0] |
| 88 |
return None |