diff --git a/poetry.lock b/poetry.lock index 1c6a824c..f1c2cad0 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 2.2.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 2.3.2 and should not be changed by hand. [[package]] name = "altgraph" @@ -2035,5 +2035,5 @@ pcsc = ["pyscard"] [metadata] lock-version = "2.1" -python-versions = ">= 3.10, <3.14" -content-hash = "fc1d1aaeb641a9dc25f39c47f258359a5003bdbf8621a6b8038fe9cef1363cca" +python-versions = ">= 3.10, <3.15" +content-hash = "21a16a0b34dd3a19ea79f5167bccdd05a9baf0a7fe7fb90618163602e65275fb" diff --git a/pynitrokey/__init__.py b/pynitrokey/__init__.py index b7c63ac5..ae82e55d 100644 --- a/pynitrokey/__init__.py +++ b/pynitrokey/__init__.py @@ -4,5 +4,4 @@ """Python Library for Nitrokey devices.""" - __all__ = ["client", "commands", "dfu", "enums", "exceptions", "helpers", "operations"] diff --git a/pynitrokey/cli/nethsm.py b/pynitrokey/cli/nethsm.py index a66614f4..3eeece6f 100644 --- a/pynitrokey/cli/nethsm.py +++ b/pynitrokey/cli/nethsm.py @@ -1254,7 +1254,7 @@ def set_certificate( This command requires authentication as a user with the Administrator role.""" - (api, key_id) = get_api_or_key_id(api, key_id) + api, key_id = get_api_or_key_id(api, key_id) with connect(ctx) as nethsm: with open(filename, "rb") as f: if key_id: @@ -1283,7 +1283,7 @@ def get_certificate(ctx: Context, api: bool, key_id: Optional[str]) -> None: This command requires authentication as a user with the Administrator role. The certificate for a key can also be queried by a user with the Operator role.""" - (api, key_id) = get_api_or_key_id(api, key_id) + api, key_id = get_api_or_key_id(api, key_id) with connect(ctx) as nethsm: if key_id: cert = nethsm.get_key_certificate(key_id) @@ -1350,7 +1350,7 @@ def csr( This command requires authentication as a user with the Administrator role.""" - (api, key_id) = get_api_or_key_id(api, key_id) + api, key_id = get_api_or_key_id(api, key_id) with connect(ctx) as nethsm: if key_id: csr = nethsm.key_csr( diff --git a/pynitrokey/cli/nk3/piv.py b/pynitrokey/cli/nk3/piv.py index 3b54a874..c78a59c3 100644 --- a/pynitrokey/cli/nk3/piv.py +++ b/pynitrokey/cli/nk3/piv.py @@ -13,7 +13,7 @@ from cryptography.hazmat.primitives.asymmetric import ec, rsa from cryptography.hazmat.primitives.asymmetric import utils as asym_utils from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15 -from cryptography.hazmat.primitives.serialization import Encoding +from cryptography.hazmat.primitives.serialization import Encoding, pkcs12 from pynitrokey.cli.nk3 import nk3 from pynitrokey.helpers import check_experimental_flag, local_critical, local_print @@ -26,6 +26,66 @@ try: # noqa: C901 from pynitrokey.nk3.piv_app import PivApp, find_by_id + default_admin_key = "010203040506070801020304050607080102030405060708" + all_key_ids = [ + "9A", + "9C", + "9D", + "9E", + "82", + "83", + "84", + "85", + "86", + "87", + "88", + "89", + "8A", + "8B", + "8C", + "8D", + "8E", + "8F", + "90", + "91", + "92", + "93", + "94", + "95", + ] + + key_id_click_type = type = click.Choice( + [ + "9A", + "9C", + "9D", + "9E", + "82", + "83", + "84", + "85", + "86", + "87", + "88", + "89", + "8A", + "8B", + "8C", + "8D", + "8E", + "8F", + "90", + "91", + "92", + "93", + "94", + "95", + ], + case_sensitive=False, + ) + + default_key_id = "9A" + class RsaPivSigner(rsa.RSAPrivateKey): _device: PivApp _key_reference: int @@ -148,6 +208,53 @@ def print_table(headers: Sequence[str], data: Iterable[Sequence[Any]]) -> None: for row in str_data: print_row(row, widths) + def import_rsa2048( + device: PivApp, + key_ref: int, + key: rsa.RSAPrivateNumbers, + public_key: rsa.RSAPublicNumbers, + ) -> None: + + device.send_receive( + 0xFE, + 0x07, + key_ref, + Tlv.build( + [ + (0x01, key.p.to_bytes(256, "big")), + (0x02, key.q.to_bytes(256, "big")), + ( + 0x03, + public_key.e.to_bytes( + (public_key.e.bit_length() + 7) // 8, "big" + ), + ), + ] + ), + ) + + def import_certificate( + device: PivApp, + key_hex: str, + certificate: bytes, + ) -> None: + payload = Tlv.build( + [ + (0x5C, bytes(bytearray.fromhex(KEY_TO_CERT_OBJ_ID_MAP[key_hex]))), + ( + 0x53, + Tlv.build( + [ + (0x70, certificate), + (0x71, bytes([0])), + ] + ), + ), + ] + ) + + device.send_receive(0xDB, 0x3F, 0xFF, payload) + @nk3.group() @click.option( "--experimental", @@ -164,7 +271,7 @@ def piv(experimental: bool) -> None: @click.argument( "admin-key", type=click.STRING, - default="010203040506070801020304050607080102030405060708", + default=default_admin_key, ) def admin_auth(admin_key: str) -> None: try: @@ -183,7 +290,7 @@ def admin_auth(admin_key: str) -> None: @click.argument( "admin-key", type=click.STRING, - default="010203040506070801020304050607080102030405060708", + default=default_admin_key, ) def init(admin_key: str) -> None: try: @@ -214,7 +321,7 @@ def info() -> None: @click.option( "--current-admin-key", type=click.STRING, - default="010203040506070801020304050607080102030405060708", + default=default_admin_key, help="Current admin key.", ) @click.argument( @@ -352,45 +459,80 @@ def _validate_rfc4514( except ValueError: raise click.BadParameter("Must be valid RFC4514 string.") + @piv.command(help="Import a key and a certificate from a .p12 file") + @click.option( + "--admin-key", + type=click.STRING, + default=default_admin_key, + help="Current admin key", + ) + @click.option( + "--key", + type=key_id_click_type, + default=default_key_id, + help="Key slot for operation.", + ) + @click.option( + "--path", + type=click.Path(allow_dash=True), + default="-", + help="Path to the .pem file containing the private key", + ) + def import_key( + admin_key: str, + key: str, + path: str, + ) -> None: + try: + admin_key_bytes = bytearray.fromhex(admin_key) + except ValueError: + local_critical( + "Key is expected to be an hexadecimal string", + support_hint=False, + ) + + with open(path, "rb") as key_file: + private_key, certificate, _ = pkcs12.load_key_and_certificates( + key_file.read(), password=None + ) + if ( + not isinstance(private_key, rsa.RSAPrivateKey) + or private_key.key_size != 2048 + or certificate is None + ): + local_critical( + "--path must point to a RSA 2048 private key and certificate as a p12 file", + support_hint=False, + ) + return + + key_hex = key.upper() + key_ref = int(key_hex, 16) + + piv_app = PivApp() + piv_app.authenticate_admin(admin_key_bytes) + import_rsa2048( + piv_app, + key_ref, + private_key.private_numbers(), + private_key.public_key().public_numbers(), + ) + + import_certificate(piv_app, key_hex, certificate.public_bytes(Encoding.DER)) + + return + @piv.command(help="Generate a new key and certificate signing request.") @click.option( "--admin-key", type=click.STRING, - default="010203040506070801020304050607080102030405060708", + default=default_admin_key, help="Current admin key", ) @click.option( "--key", - type=click.Choice( - [ - "9A", - "9C", - "9D", - "9E", - "82", - "83", - "84", - "85", - "86", - "87", - "88", - "89", - "8A", - "8B", - "8C", - "8D", - "8E", - "8F", - "90", - "91", - "92", - "93", - "94", - "95", - ], - case_sensitive=False, - ), - default="9A", + type=key_id_click_type, + default=default_key_id, help="Key slot for operation.", ) @click.option( @@ -647,28 +789,13 @@ def generate_key( with click.open_file(path, mode="wb") as file: file.write(csr.public_bytes(Encoding.DER)) - payload = Tlv.build( - [ - (0x5C, bytes(bytearray.fromhex(KEY_TO_CERT_OBJ_ID_MAP[key_hex]))), - ( - 0x53, - Tlv.build( - [ - (0x70, certificate.public_bytes(Encoding.DER)), - (0x71, bytes([0])), - ] - ), - ), - ] - ) - - device.send_receive(0xDB, 0x3F, 0xFF, payload) + import_certificate(device, key_hex, certificate.public_bytes(Encoding.DER)) @piv.command(help="Write a certificate to a key slot.") @click.argument( "admin-key", type=click.STRING, - default="010203040506070801020304050607080102030405060708", + default=default_admin_key, ) @click.option( "--format", @@ -678,36 +805,8 @@ def generate_key( ) @click.option( "--key", - type=click.Choice( - [ - "9A", - "9C", - "9D", - "9E", - "82", - "83", - "84", - "85", - "86", - "87", - "88", - "89", - "8A", - "8B", - "8C", - "8D", - "8E", - "8F", - "90", - "91", - "92", - "93", - "94", - "95", - ], - case_sensitive=False, - ), - default="9A", + type=key_id_click_type, + default=default_key_id, help="Key slot for operation.", ) @click.option( @@ -756,36 +855,8 @@ def write_certificate(admin_key: str, format: str, key: str, path: str) -> None: ) @click.option( "--key", - type=click.Choice( - [ - "9A", - "9C", - "9D", - "9E", - "82", - "83", - "84", - "85", - "86", - "87", - "88", - "89", - "8A", - "8B", - "8C", - "8D", - "8E", - "8F", - "90", - "91", - "92", - "93", - "94", - "95", - ], - case_sensitive=False, - ), - default="9A", + type=key_id_click_type, + default=default_key_id, help="Key slot for operation.", ) @click.option( diff --git a/pynitrokey/cli/pro.py b/pynitrokey/cli/pro.py index 93646ff1..048c0611 100644 --- a/pynitrokey/cli/pro.py +++ b/pynitrokey/cli/pro.py @@ -90,7 +90,7 @@ def change_firmware_password(): except DeviceNotFound: local_critical(f"No {nk.friendly_name} device found", support_hint=False) - (_major, minor) = nk.fw_version + _major, minor = nk.fw_version if minor < 11: local_critical( f"The connected {nk.friendly_name} does not support firmware updates", diff --git a/pynitrokey/libnk.py b/pynitrokey/libnk.py index a268918b..a5a29bcb 100644 --- a/pynitrokey/libnk.py +++ b/pynitrokey/libnk.py @@ -19,6 +19,7 @@ SPDX-License-Identifier: LGPL-3.0-only """ + import os import sys from enum import IntEnum diff --git a/pynitrokey/start/upgrade_by_passwd.py b/pynitrokey/start/upgrade_by_passwd.py index 26313a5c..54457514 100755 --- a/pynitrokey/start/upgrade_by_passwd.py +++ b/pynitrokey/start/upgrade_by_passwd.py @@ -26,6 +26,7 @@ SPDX-License-Identifier: GPL-3.0-or-later """ + from pprint import pprint IMPORT_ERROR_HELP = """ diff --git a/pyproject.toml b/pyproject.toml index e7a47251..788401fa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -83,7 +83,7 @@ classifiers = [ ] [tool.poetry.dependencies] -python = ">= 3.10, <3.14" +python = ">= 3.10, <3.15" [tool.poetry.group.dev] optional = true