Debian Codegraph @

src/nitrokey/nk3/__init__.py

raw
1 # Copyright 2021-2022 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 List, Optional, Union
9
10 from nitrokey.trussed._bootloader import ModelData
11 from nitrokey.trussed._bootloader.nrf52 import SignatureKey
12
13 from ._bootloader import NK3Bootloader as NK3Bootloader
14 from ._device import NK3 as NK3
15
16 _PID_NK3_DEVICE = 0x42B2
17 _PID_NK3_LPC55_BOOTLOADER = 0x42DD
18 _PID_NK3_NRF52_BOOTLOADER = 0x42E8
19
20 _NK3_DATA = ModelData(
21 firmware_repository_name="nitrokey-3-firmware",
22 firmware_pattern_string="firmware-nk3-v.*\\.zip$",
23 nrf52_signature_keys=[
24 SignatureKey(
25 name="Nitrokey",
26 is_official=True,
27 der="3059301306072a8648ce3d020106082a8648ce3d03010703420004a0849b19007ccd4661c01c533804b7fd0c4d8c0e7583653f1f36a8331afff298b542bd00a3dc47c16bf428ac4d2864137d63f702d89e5b42674e0549b4232618",
28 ),
29 SignatureKey(
30 name="Nitrokey Test",
31 is_official=False,
32 der="3059301306072a8648ce3d020106082a8648ce3d0301070342000493e461ab0582bda1f45b0ce47d66bc4e8623e289c31af2098cde6ebd8631da85acf17e412d406c1e38c2de654a8fd0196506a85b169a756aeac2505a541cdd5d",
33 ),
34 ],
35 )
36
37
38 def list(use_ccid: bool = False, exclusive: bool = True) -> List[Union[NK3, NK3Bootloader]]:
39 devices: List[Union[NK3, NK3Bootloader]] = []
40 devices.extend(NK3Bootloader.list())
41 if use_ccid:
42 devices.extend(NK3.list_ccid(exclusive))
43 else:
44 devices.extend(NK3.list_ctaphid())
45
46 return devices
47
48
49 def open(path: str) -> Optional[Union[NK3, NK3Bootloader]]:
50 device = NK3.open(path)
51 bootloader_device = NK3Bootloader.open(path)
52 if device and bootloader_device:
53 raise Exception(f"Found multiple devices at path {path}")
54 if device:
55 return device
56 if bootloader_device:
57 return bootloader_device
58 return None