| 1 |
# Copyright 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 builtins |
| 9 |
from typing import TYPE_CHECKING, List, Optional, Sequence, Union |
| 10 |
|
| 11 |
from fido2.hid import CtapHidDevice |
| 12 |
|
| 13 |
from nitrokey import _VID_NITROKEY |
| 14 |
from nitrokey._smartcard import ExclusiveConnectCardConnection, ExclusiveTransmitCardConnection |
| 15 |
from nitrokey.trussed import Fido2Certs, TrussedDevice, Version |
| 16 |
from nitrokey.trussed._base import Model |
| 17 |
from nitrokey.trussed._bootloader import ModelData |
| 18 |
from nitrokey.trussed._bootloader.nrf52 import SignatureKey, TrussedBootloaderNrf52 |
| 19 |
|
| 20 |
_PID_NKPK_DEVICE = 0x42F3 |
| 21 |
_PID_NKPK_BOOTLOADER = 0x42F4 |
| 22 |
|
| 23 |
_FIDO2_CERTS = [ |
| 24 |
Fido2Certs( |
| 25 |
start=Version(0, 1, 0), |
| 26 |
hashes=["c7512dfcd15ffc5a7b4000e4898e5956ee858027794c5086cc137a02cd15d123"], |
| 27 |
) |
| 28 |
] |
| 29 |
|
| 30 |
_NKPK_DATA = ModelData( |
| 31 |
firmware_repository_name="nitrokey-passkey-firmware", |
| 32 |
firmware_pattern_string="firmware-nkpk-v.*\\.zip$", |
| 33 |
nrf52_signature_keys=[ |
| 34 |
SignatureKey( |
| 35 |
name="Nitrokey", |
| 36 |
is_official=True, |
| 37 |
der="3059301306072a8648ce3d020106082a8648ce3d0301070342000445121cdf7a10826faa58c8cbe7bb1a40fe71c85c7756324eac09610d4710e9dadd473c0c9d35838b5cce301e796b2e14a8c29c86f0eb15f36325096506e275e6", |
| 38 |
), |
| 39 |
SignatureKey( |
| 40 |
name="Nitrokey Test", |
| 41 |
is_official=False, |
| 42 |
der="3059301306072a8648ce3d020106082a8648ce3d03010703420004d9a355a2927bd6ecb7ed714294d4692ad31ae9dd21853bf99e2cf7182d1acd6c2ada4a9707ab43f9e6194480d94e477dce4de9be5c35119c714bac459b21cbdc", |
| 43 |
), |
| 44 |
], |
| 45 |
) |
| 46 |
|
| 47 |
|
| 48 |
class NKPK(TrussedDevice): |
| 49 |
def __init__( |
| 50 |
self, |
| 51 |
device: CtapHidDevice | ExclusiveTransmitCardConnection | ExclusiveConnectCardConnection, |
| 52 |
) -> None: |
| 53 |
super().__init__(device, _FIDO2_CERTS) |
| 54 |
|
| 55 |
@property |
| 56 |
def model(self) -> Model: |
| 57 |
return Model.NKPK |
| 58 |
|
| 59 |
@property |
| 60 |
def pid(self) -> int: |
| 61 |
return _PID_NKPK_DEVICE |
| 62 |
|
| 63 |
@property |
| 64 |
def name(self) -> str: |
| 65 |
return "Nitrokey Passkey" |
| 66 |
|
| 67 |
@classmethod |
| 68 |
def from_device( |
| 69 |
cls, |
| 70 |
device: CtapHidDevice | ExclusiveTransmitCardConnection | ExclusiveConnectCardConnection, |
| 71 |
) -> "NKPK": |
| 72 |
return cls(device) |
| 73 |
|
| 74 |
@classmethod |
| 75 |
def list_ctaphid(cls) -> List["NKPK"]: |
| 76 |
return cls._list_vid_pid(_VID_NITROKEY, _PID_NKPK_DEVICE) |
| 77 |
|
| 78 |
@classmethod |
| 79 |
def list_ccid(cls, exclusive: bool = True) -> List["NKPK"]: |
| 80 |
return cls._list_pcsc_atr( |
| 81 |
builtins.list(bytes.fromhex("3B8F01805D4E6974726F6B657900000000006A")), exclusive |
| 82 |
) |
| 83 |
|
| 84 |
|
| 85 |
class NKPKBootloader(TrussedBootloaderNrf52): |
| 86 |
@property |
| 87 |
def model(self) -> Model: |
| 88 |
return Model.NKPK |
| 89 |
|
| 90 |
@property |
| 91 |
def name(self) -> str: |
| 92 |
return "Nitrokey Passkey Bootloader" |
| 93 |
|
| 94 |
@property |
| 95 |
def pid(self) -> int: |
| 96 |
return _PID_NKPK_BOOTLOADER |
| 97 |
|
| 98 |
@classmethod |
| 99 |
def list(cls) -> List["NKPKBootloader"]: |
| 100 |
return cls._list_vid_pid(_VID_NITROKEY, _PID_NKPK_BOOTLOADER) |
| 101 |
|
| 102 |
@classmethod |
| 103 |
def open(cls, path: str) -> Optional["NKPKBootloader"]: |
| 104 |
return cls._open_vid_pid(_VID_NITROKEY, _PID_NKPK_BOOTLOADER, path) |
| 105 |
|
| 106 |
@property |
| 107 |
def _signature_keys(self) -> Sequence[SignatureKey]: |
| 108 |
return _NKPK_DATA.nrf52_signature_keys |
| 109 |
|
| 110 |
|
| 111 |
def list(use_ccid: bool = False, exclusive: bool = True) -> List[Union[NKPK, NKPKBootloader]]: |
| 112 |
devices: List[Union[NKPK, NKPKBootloader]] = [] |
| 113 |
devices.extend(NKPKBootloader.list()) |
| 114 |
if use_ccid: |
| 115 |
devices.extend(NKPK.list_ccid(exclusive)) |
| 116 |
else: |
| 117 |
devices.extend(NKPK.list_ctaphid()) |
| 118 |
return devices |
| 119 |
|
| 120 |
|
| 121 |
def open(path: str) -> Optional[Union[NKPK, NKPKBootloader]]: |
| 122 |
device = NKPK.open(path) |
| 123 |
bootloader_device = NKPKBootloader.open(path) |
| 124 |
if device and bootloader_device: |
| 125 |
raise Exception(f"Found multiple devices at path {path}") |
| 126 |
if device: |
| 127 |
return device |
| 128 |
if bootloader_device: |
| 129 |
return bootloader_device |
| 130 |
return None |
| 131 |
|
| 132 |
|
| 133 |
if TYPE_CHECKING: |
| 134 |
from contextlib import AbstractContextManager |
| 135 |
|
| 136 |
_ACM_DEV: type[AbstractContextManager[NKPK]] = NKPK |
| 137 |
_ACM_BL: type[AbstractContextManager[NKPKBootloader]] = NKPKBootloader |