diff --git a/pynitrokey/cli/fido2.py b/pynitrokey/cli/fido2.py index c97a348f..81e56f5c 100644 --- a/pynitrokey/cli/fido2.py +++ b/pynitrokey/cli/fido2.py @@ -18,9 +18,10 @@ from fido2.cose import ES256, EdDSA from fido2.ctap import CtapError from fido2.ctap2.base import Ctap2, Info +from fido2.ctap2.config import Config from fido2.ctap2.credman import CredentialManagement from fido2.ctap2.extensions import HMACGetSecretInput, HMACGetSecretOutput, HmacSecretExtension -from fido2.ctap2.pin import ClientPin +from fido2.ctap2.pin import ClientPin, PinProtocol from fido2.hid import CtapHidDevice, open_device from fido2.webauthn import ( Aaguid, @@ -93,18 +94,18 @@ def _ctap2(device: CtapHidDevice) -> Ctap2: raise CliException("Device does not support CTAP2", support_hint=False) -def _credential_management(device: CtapHidDevice, pin: str) -> CredentialManagement: - ctap2 = _ctap2(device) +def _get_pin_token( + ctap2: Ctap2, pin: str | None, permissions: ClientPin.PERMISSION +) -> tuple[PinProtocol, bytes]: + if pin is None: + pin = AskUser.hidden("Please provide pin: ") client_pin = ClientPin(ctap2) - try: - client_token = client_pin.get_pin_token( - pin, permissions=ClientPin.PERMISSION.CREDENTIAL_MGMT - ) + client_token = client_pin.get_pin_token(pin, permissions=permissions) except CtapError as error: if error.code == CtapError.ERR.PIN_NOT_SET: - local_critical("Please set a pin in order to manage credentials", support_hint=False) + local_critical("Please set a pin to use this feature", support_hint=False) if error.code == CtapError.ERR.PIN_AUTH_BLOCKED: local_critical( "Pin authentication has been blocked, try reinserting the key or setting a pin if none is set", @@ -119,8 +120,21 @@ def _credential_management(device: CtapHidDevice, pin: str) -> CredentialManagem if error.code == CtapError.ERR.PIN_INVALID: local_critical("Wrong pin, please retry", support_hint=False) raise + return client_pin.protocol, client_token + - return CredentialManagement(ctap2, client_pin.protocol, client_token) +def _config(ctap2: Ctap2, pin: str | None) -> Config: + pin_protocol = None + pin_token = None + if ctap2.info.options.get("clientPin", False): + pin_protocol, pin_token = _get_pin_token(ctap2, pin, ClientPin.PERMISSION.AUTHENTICATOR_CFG) + return Config(ctap2, pin_protocol, pin_token) + + +def _credential_management(device: CtapHidDevice, pin: str | None) -> CredentialManagement: + ctap2 = _ctap2(device) + pin_protocol, pin_token = _get_pin_token(ctap2, pin, ClientPin.PERMISSION.CREDENTIAL_MGMT) + return CredentialManagement(ctap2, pin_protocol, pin_token) def _fido2( @@ -222,13 +236,17 @@ def _simple_secret( return output.output1 +def _format_bool(value: bool) -> str: + return str(value).lower() + + @click.group() def fido2() -> None: """Interact with Nitrokey FIDO2 devices, see subcommands.""" require_windows_admin() -@click.command() +@fido2.command() @click.option( "-s", "--serial", @@ -261,20 +279,16 @@ def get_info(serial: Optional[str]) -> None: print(f"{field.name}: {value}") -@click.command() +@fido2.command() @click.option( "-s", "--serial", help="Serial number of Nitrokey to use. Prefix with 'device=' to provide device file, e.g. 'device=/dev/hidraw5'.", ) @click.option("--pin", help="provide PIN instead of asking the user", default=None) -def list_credentials(serial: str, pin: str) -> None: +def list_credentials(serial: str, pin: str | None) -> None: """List all credentials saved on the key as well as the amount of remaining slots.""" - # Makes sure pin exists - if not pin: - pin = AskUser.hidden("Please provide pin: ") - device = _device(serial) cred_manager = _credential_management(device, pin) @@ -319,7 +333,7 @@ def list_credentials(serial: str, pin: str) -> None: local_print(f"There is an estimated amount of {remaining_cred_space} credential slots left") -@click.command() +@fido2.command() @click.option( "-s", "--serial", @@ -327,15 +341,12 @@ def list_credentials(serial: str, pin: str) -> None: ) @click.option("--pin", help="provide PIN instead of asking the user", default=None) @click.option("-cid", "--cred-id", help="Credential id of the Credential to be deleted") -def delete_credential(serial: str, pin: str, cred_id: str) -> None: +def delete_credential(serial: str, pin: str | None, cred_id: str) -> None: """Delete a specific credential from the key""" if not cred_id: cred_id = AskUser.hidden("Please provide credential-id") - if not pin: - pin = AskUser.hidden("Please provide pin: ") - device = _device(serial) cred_manager = _credential_management(device, pin) @@ -354,7 +365,7 @@ def delete_credential(serial: str, pin: str, cred_id: str) -> None: REQUIREMENT_CHOICE = click.Choice(["discouraged", "preferred", "required"]) -@click.command() +@fido2.command() @click.option("--host", help="Relying party's host", default="nitrokeys.dev", show_default=True) @click.option("--user", help="User ID", default="they", show_default=True) @click.option( @@ -393,7 +404,7 @@ def make_credential(host: str, user: str, resident_key: str, user_verification: print(credential.credential_id.hex()) -@click.command() +@fido2.command() @click.option( "-s", "--serial", @@ -422,7 +433,7 @@ def challenge_response( print(output.hex()) -@click.command() +@fido2.command() @click.option( "-s", "--serial", @@ -451,7 +462,7 @@ def reset(serial: Optional[str], yes: bool) -> None: # @fixme: lacking functionality? remove? implement? -@click.command() +@fido2.command() @click.option( "-s", "--serial", @@ -479,7 +490,7 @@ def change_pin(serial: Optional[str]) -> None: local_critical("failed changing to new pin!", "did you set one already? or is it wrong?", e) -@click.command() +@fido2.command() @click.option( "-s", "--serial", @@ -516,7 +527,132 @@ def set_pin(serial: Optional[str]) -> None: ) -@click.command() +@fido2.command() +@click.option( + "-s", + "--serial", + help="Serial number of Nitrokey to use. Prefix with 'device=' to provide device file, e.g. 'device=/dev/hidraw5'.", +) +@click.option( + "--force-change-pin", is_flag=True, help="Require a PIN change before the PIN can be used" +) +@click.option("--min-pin-length", type=int, help="Set the minimum PIN length") +@click.option( + "--rp-id", "rp_ids", multiple=True, help="Set the RP IDs that can query the minimum PIN length" +) +@click.option("--pin", help="provide PIN instead of asking the user", default=None) +def set_pin_config( + serial: str | None, + force_change_pin: bool, + min_pin_length: int | None, + rp_ids: tuple[str], + pin: str | None, +) -> None: + """Set the pin-related configuration of the device.""" + + rp_ids_list = None + if len(rp_ids) > 0: + rp_ids_list = list(rp_ids) + + if not force_change_pin and min_pin_length is None and rp_ids_list is None: + raise CliException( + "At least one of these options must be set: --force-change-pin, --min-pin-length, --rp-id", + support_hint=False, + ) + + device = _device(serial) + ctap2 = _ctap2(device) + if not ctap2.info.options.get("setMinPINLength", False): + raise CliException( + "Device does not support the setMinPINLength command", support_hint=False + ) + old_min_pin_length = ctap2.info.min_pin_length + + config = _config(ctap2, pin) + try: + config.set_min_pin_length( + min_pin_length=min_pin_length, rp_ids=rp_ids_list, force_change_pin=force_change_pin + ) + except CtapError as error: + if error.code == CtapError.ERR.PIN_POLICY_VIOLATION: + if min_pin_length is not None and min_pin_length < old_min_pin_length: + raise CliException( + "The minimum PIN length can only be increased " + f"(current value: {old_min_pin_length})", + support_hint=False, + ) from error + raise + + local_print("PIN configuration was updated successfully") + + +@fido2.command() +@click.option( + "-s", + "--serial", + help="Serial number of Nitrokey to use. Prefix with 'device=' to provide device file, e.g. 'device=/dev/hidraw5'.", +) +def get_pin_config(serial: str | None) -> None: + """Get the pin-related configuration of the device.""" + + device = _device(serial) + ctap2 = _ctap2(device) + info = ctap2.info + + print(f"minimum PIN length: {info.min_pin_length}") + print(f"maximum PIN length: {info.max_pin_length}") + print(f"force PIN change: {_format_bool(info.force_pin_change)}") + + +@fido2.command() +@click.option( + "-s", + "--serial", + help="Serial number of Nitrokey to use. Prefix with 'device=' to provide device file, e.g. 'device=/dev/hidraw5'.", +) +@click.option("--pin", help="provide PIN instead of asking the user", default=None) +@click.argument("always-uv", type=bool) +def set_always_uv(serial: str | None, always_uv: bool, pin: str | None) -> None: + """Set the alwaysUv flag of the device (possible values: true or false).""" + + device = _device(serial) + ctap2 = _ctap2(device) + + old_always_uv = ctap2.info.options.get("alwaysUv", None) + if old_always_uv is None: + raise CliException("Device does not support the alwaysUv feature", support_hint=False) + assert isinstance(old_always_uv, bool) + + if old_always_uv == always_uv: + local_print(f"alwaysUv flag is already set to {_format_bool(always_uv)}") + return + + config = _config(ctap2, pin) + config.toggle_always_uv() + + local_print("alwaysUv flag was updated successfully") + + +@fido2.command() +@click.option( + "-s", + "--serial", + help="Serial number of Nitrokey to use. Prefix with 'device=' to provide device file, e.g. 'device=/dev/hidraw5'.", +) +def get_always_uv(serial: str | None) -> None: + """Get the alwaysUv flag of the device.""" + + device = _device(serial) + ctap2 = _ctap2(device) + always_uv = ctap2.info.options.get("alwaysUv", None) + if always_uv is None: + raise CliException("Device does not support the alwaysUv feature", support_hint=False) + assert isinstance(always_uv, bool) + + local_print(_format_bool(always_uv)) + + +@fido2.command() @click.option( "-s", "--serial", @@ -593,7 +729,7 @@ def verify(serial: Optional[str], pin: Optional[str]) -> None: local_print(f"unknown fingerprint! {cert}") -@click.command() +@fido2.command() @click.option( "-s", "--serial", @@ -610,7 +746,7 @@ def wink(serial: Optional[str]) -> None: } -@click.command() +@fido2.command() @click.option( "-s", "--serial", @@ -633,16 +769,3 @@ def provision_credential( client = _fido2(device, host) result = service_ob.provision(create_user, user, client) local_print(result) - - -fido2.add_command(challenge_response) -fido2.add_command(change_pin) -fido2.add_command(delete_credential) -fido2.add_command(get_info) -fido2.add_command(list_credentials) -fido2.add_command(make_credential) -fido2.add_command(reset) -fido2.add_command(set_pin) -fido2.add_command(verify) -fido2.add_command(wink) -fido2.add_command(provision_credential)