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};
mod config_cmd {
pub(super) const ENABLE_ENTERPRISE_ATT: u8 = 0x01;
pub(super) const TOGGLE_ALWAYS_UV: u8 = 0x02;
pub(super) const SET_MIN_PIN_LENGTH: u8 = 0x03;
}
pub struct Config<C: Connection> {
session: Ctap2Session<C>,
protocol: Option<PinProtocol>,
pin_token: Option<Zeroizing<Vec<u8>>>,
}
impl<C: Connection + 'static> Config<C> {
pub fn is_supported(info: &super::Info) -> bool {
info.options.get("authnrCfg") == Some(&true)
}
#[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)),
})
}
#[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,
})
}
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) {
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)), sub_cmd_params.cloned(), protocol_ver, pin_uv_param, ]);
self.session
.send_cbor(ctap2_cmd::CONFIG, Some(&data), None, None)?;
Ok(())
}
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(())
}
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(())
}
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(())
}
}