yubikit 1.0.0-beta.0

YubiKey management library
Documentation
// Copyright 2026 Yubico AB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! CTAP2 Authenticator Config — toggle features and set configuration.

use zeroize::Zeroizing;

use crate::cbor::{self, Value};
use crate::core::Connection;

use super::pin_protocol::PinProtocol;
use super::session::Ctap2Session;
use super::{Ctap2Error, build_args_map, ctap2_cmd};

/// Config sub-command identifiers (§6.11).
mod config_cmd {
    /// Enable enterprise attestation mode.
    pub(super) const ENABLE_ENTERPRISE_ATT: u8 = 0x01;
    /// Toggle the alwaysUv (always require user verification) option.
    pub(super) const TOGGLE_ALWAYS_UV: u8 = 0x02;
    /// Set minimum PIN length and related policies.
    pub(super) const SET_MIN_PIN_LENGTH: u8 = 0x03;
}

/// CTAP2 Authenticator Config operations (§6.11).
///
/// Provides authenticator configuration management: enterprise attestation,
/// always-UV toggle, and minimum PIN length enforcement.
/// Owns a [`Ctap2Session`](crate::ctap2::Ctap2Session) and a [`PinProtocol`](crate::ctap2::PinProtocol) for authenticated commands.
pub struct Config<C: Connection> {
    session: Ctap2Session<C>,
    protocol: Option<PinProtocol>,
    pin_token: Option<Zeroizing<Vec<u8>>>,
}

impl<C: Connection + 'static> Config<C> {
    /// Whether the authenticator supports authenticator config.
    pub fn is_supported(info: &super::Info) -> bool {
        info.options.get("authnrCfg") == Some(&true)
    }

    /// Create a new `Config` from a `Ctap2Session` and a PIN token.
    ///
    /// The PIN token must have the `AUTHENTICATOR_CFG` permission.
    /// On failure, returns the session alongside the error.
    #[allow(clippy::result_large_err)]
    pub fn new(
        session: Ctap2Session<C>,
        protocol: PinProtocol,
        pin_token: Vec<u8>,
    ) -> Result<Self, (Ctap2Error<C::Error>, Ctap2Session<C>)> {
        if !Self::is_supported(&session.cached_info) {
            return Err((
                Ctap2Error::InvalidResponse(
                    "Authenticator does not support authenticatorConfig".into(),
                ),
                session,
            ));
        }
        Ok(Self {
            session,
            protocol: Some(protocol),
            pin_token: Some(Zeroizing::new(pin_token)),
        })
    }

    /// Create a new `Config` without PIN authentication.
    ///
    /// Used when no PIN is set on the authenticator but config commands are needed.
    /// On failure, returns the session alongside the error.
    #[allow(clippy::result_large_err)]
    pub fn new_unauthenticated(
        session: Ctap2Session<C>,
    ) -> Result<Self, (Ctap2Error<C::Error>, Ctap2Session<C>)> {
        if !Self::is_supported(&session.cached_info) {
            return Err((
                Ctap2Error::InvalidResponse(
                    "Authenticator does not support authenticatorConfig".into(),
                ),
                session,
            ));
        }
        Ok(Self {
            session,
            protocol: None,
            pin_token: None,
        })
    }

    /// Consume this `Config`, returning the underlying `Ctap2Session`.
    pub fn into_session(self) -> Ctap2Session<C> {
        self.session
    }

    fn call(
        &mut self,
        sub_cmd: u8,
        sub_cmd_params: Option<&Value>,
    ) -> Result<(), Ctap2Error<C::Error>> {
        let (protocol_ver, pin_uv_param) =
            if let (Some(protocol), Some(pin_token)) = (&self.protocol, &self.pin_token) {
                // Auth message: 0xff*32 || 0x0d || subCmd || serialize(subCmdParams)
                let mut msg = vec![0xff; 32];
                msg.push(ctap2_cmd::CONFIG);
                msg.push(sub_cmd);
                if let Some(p) = sub_cmd_params {
                    msg.extend_from_slice(&cbor::encode(p));
                }
                let param = protocol.authenticate(pin_token, &msg);
                (
                    Some(Value::Int(protocol.version() as i64)),
                    Some(Value::Bytes(param)),
                )
            } else {
                (None, None)
            };

        let data = build_args_map(&[
            Some(Value::Int(sub_cmd as i64)), // 0x01
            sub_cmd_params.cloned(),          // 0x02
            protocol_ver,                     // 0x03
            pin_uv_param,                     // 0x04
        ]);
        self.session
            .send_cbor(ctap2_cmd::CONFIG, Some(&data), None, None)?;
        Ok(())
    }

    /// Enable enterprise attestation.
    pub fn enable_enterprise_attestation(&mut self) -> Result<(), Ctap2Error<C::Error>> {
        log::debug!("Enabling enterprise attestation");
        self.call(config_cmd::ENABLE_ENTERPRISE_ATT, None)?;
        log::info!("Enterprise attestation enabled");
        Ok(())
    }

    /// Toggle the alwaysUv option.
    pub fn toggle_always_uv(&mut self) -> Result<(), Ctap2Error<C::Error>> {
        log::debug!("Toggling always-UV");
        self.call(config_cmd::TOGGLE_ALWAYS_UV, None)?;
        log::info!("Always-UV toggled");
        Ok(())
    }

    /// Set minimum PIN length and related policies.
    pub fn set_min_pin_length(
        &mut self,
        min_pin_length: Option<u32>,
        rp_ids: Option<&[String]>,
        force_change_pin: bool,
    ) -> Result<(), Ctap2Error<C::Error>> {
        let mut sub_params: Vec<(Value, Value)> = Vec::new();
        if let Some(len) = min_pin_length {
            sub_params.push((Value::Int(0x01), Value::Int(len as i64)));
        }
        if let Some(ids) = rp_ids {
            let arr: Vec<Value> = ids.iter().map(|s| Value::Text(s.clone())).collect();
            sub_params.push((Value::Int(0x02), Value::Array(arr)));
        }
        if force_change_pin {
            sub_params.push((Value::Int(0x03), Value::Bool(true)));
        }
        let params = if sub_params.is_empty() {
            None
        } else {
            Some(Value::Map(sub_params))
        };
        log::debug!("Setting minimum PIN length");
        self.call(config_cmd::SET_MIN_PIN_LENGTH, params.as_ref())?;
        log::info!("Minimum PIN length updated");
        Ok(())
    }
}