Debian Codegraph @

src/nitrokey/nk3/_device.py

raw
1 # Copyright 2021 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 from typing import TYPE_CHECKING, List
9
10 from fido2.hid import CtapHidDevice
11
12 from nitrokey import _VID_NITROKEY
13 from nitrokey._smartcard import ExclusiveConnectCardConnection, ExclusiveTransmitCardConnection
14 from nitrokey.trussed import Fido2Certs, Model, TrussedDevice, Version
15
16 FIDO2_CERTS = [
17 Fido2Certs(
18 start=Version(0, 1, 0),
19 hashes=["ad8fd1d16f59104b9e06ef323cc03f777ed5303cd421a101c9cb00bb3fdf722d"],
20 ),
21 Fido2Certs(
22 start=Version(1, 0, 3),
23 hashes=[
24 "aa1cb760c2879530e7d7fed3da75345d25774be9cfdbbcbd36fdee767025f34b", # NK3xN/lpc55
25 "4c331d7af869fd1d8217198b917a33d1fa503e9778da7638504a64a438661ae0", # NK3AM/nrf52
26 "f1ed1aba24b16e8e3fabcda72b10cbfa54488d3b778bda552162d60c6dd7b4fa", # NK3AM/nrf52 test
27 ],
28 ),
29 ]
30
31
32 class NK3(TrussedDevice):
33 """A Nitrokey 3 device running the firmware."""
34
35 def __init__(
36 self,
37 device: CtapHidDevice | ExclusiveTransmitCardConnection | ExclusiveConnectCardConnection,
38 ) -> None:
39 super().__init__(device, FIDO2_CERTS)
40
41 @property
42 def model(self) -> Model:
43 return Model.NK3
44
45 @property
46 def pid(self) -> int:
47 from . import _PID_NK3_DEVICE
48
49 return _PID_NK3_DEVICE
50
51 @property
52 def name(self) -> str:
53 return "Nitrokey 3"
54
55 @classmethod
56 def from_device(
57 cls,
58 device: CtapHidDevice | ExclusiveTransmitCardConnection | ExclusiveConnectCardConnection,
59 ) -> "NK3":
60 return cls(device)
61
62 @classmethod
63 def list_ctaphid(cls) -> List["NK3"]:
64 from . import _PID_NK3_DEVICE
65
66 return cls._list_vid_pid(_VID_NITROKEY, _PID_NK3_DEVICE)
67
68 @classmethod
69 def list_ccid(cls, exclusive: bool = True) -> List["NK3"]:
70 return cls._list_pcsc_atr(
71 list(bytes.fromhex("3B8F01805D4E6974726F6B657900000000006A")), exclusive
72 )
73
74
75 if TYPE_CHECKING:
76 from contextlib import AbstractContextManager
77
78 _: type[AbstractContextManager[NK3]] = NK3