use zeroize::Zeroizing;
use crate::cbor::{self, Value};
use crate::core::Connection;
use super::pin_protocol::PinProtocol;
use super::session::Ctap2Session;
use super::types::{EnrollSampleResult, FingerprintSensorInfo, FingerprintTemplate};
use super::{Ctap2Error, build_args_map, ctap2_cmd};
mod bio_cmd {
pub(super) const ENROLL_BEGIN: u8 = 0x01;
pub(super) const ENROLL_CAPTURE_NEXT: u8 = 0x02;
pub(super) const ENROLL_CANCEL: u8 = 0x03;
pub(super) const ENUMERATE_ENROLLMENTS: u8 = 0x04;
pub(super) const SET_NAME: u8 = 0x05;
pub(super) const REMOVE_ENROLLMENT: u8 = 0x06;
pub(super) const GET_SENSOR_INFO: u8 = 0x07;
}
mod bio_result_key {
pub(super) const TEMPLATE_INFOS: i64 = 0x07;
}
pub struct BioEnrollment<C: Connection> {
session: Ctap2Session<C>,
protocol: PinProtocol,
pin_token: Zeroizing<Vec<u8>>,
use_legacy: bool,
}
impl<C: Connection + 'static> BioEnrollment<C> {
pub fn is_supported(info: &super::Info) -> bool {
if info.options.contains_key("bioEnroll") {
return true;
}
info.versions.contains(&"FIDO_2_1_PRE".to_string())
&& info.options.contains_key("userVerificationMgmtPreview")
}
#[allow(clippy::result_large_err)]
pub fn new(
session: Ctap2Session<C>,
protocol: PinProtocol,
pin_token: Vec<u8>,
) -> Result<Self, (Ctap2Error<C::Error>, Ctap2Session<C>)> {
let info = &session.cached_info;
if !Self::is_supported(info) {
return Err((
Ctap2Error::InvalidResponse("Authenticator does not support bioEnrollment".into()),
session,
));
}
let has_bio = info.options.contains_key("bioEnroll");
let use_legacy = !has_bio;
Ok(Self {
session,
protocol,
pin_token: Zeroizing::new(pin_token),
use_legacy,
})
}
pub fn into_session(self) -> Ctap2Session<C> {
self.session
}
fn cmd_byte(&self) -> u8 {
if self.use_legacy {
ctap2_cmd::BIO_ENROLLMENT_PRE
} else {
ctap2_cmd::BIO_ENROLLMENT
}
}
fn call(
&mut self,
modality: Option<u8>,
sub_cmd: Option<u8>,
sub_cmd_params: Option<&Value>,
auth: bool,
on_keepalive: Option<&mut dyn FnMut(u8)>,
cancel: Option<&dyn Fn() -> bool>,
) -> Result<Value, Ctap2Error<C::Error>> {
let (protocol_ver, pin_uv_param) = if auth {
let mut msg: Vec<u8> = Vec::new();
if let Some(m) = modality {
msg.push(m);
}
if let Some(sc) = sub_cmd {
msg.push(sc);
}
if let Some(p) = sub_cmd_params {
msg.extend_from_slice(&cbor::encode(p));
}
let param = self.protocol.authenticate(&self.pin_token, &msg);
(
Some(Value::Int(self.protocol.version() as i64)),
Some(Value::Bytes(param)),
)
} else {
(None, None)
};
let data = build_args_map(&[
modality.map(|m| Value::Int(m as i64)), sub_cmd.map(|sc| Value::Int(sc as i64)), sub_cmd_params.cloned(), protocol_ver, pin_uv_param, ]);
self.session
.send_cbor(self.cmd_byte(), Some(&data), on_keepalive, cancel)
}
pub fn get_fingerprint_sensor_info(
&mut self,
) -> Result<FingerprintSensorInfo, Ctap2Error<C::Error>> {
log::debug!("Getting fingerprint sensor info");
let resp = self.call(
Some(0x01),
Some(bio_cmd::GET_SENSOR_INFO),
None,
false,
None,
None,
)?;
FingerprintSensorInfo::from_cbor(&resp)
.ok_or_else(|| Ctap2Error::InvalidResponse("invalid fingerprint sensor info".into()))
}
pub fn enroll_begin(
&mut self,
timeout: Option<u32>,
on_keepalive: Option<&mut dyn FnMut(u8)>,
cancel: Option<&dyn Fn() -> bool>,
) -> Result<EnrollSampleResult, Ctap2Error<C::Error>> {
log::debug!("Starting fingerprint enrollment");
let sub_params =
timeout.map(|t| Value::Map(vec![(Value::Int(0x03), Value::Int(t as i64))]));
let resp = self.call(
Some(0x01),
Some(bio_cmd::ENROLL_BEGIN),
sub_params.as_ref(),
true,
on_keepalive,
cancel,
)?;
EnrollSampleResult::from_cbor(&resp)
.ok_or_else(|| Ctap2Error::InvalidResponse("invalid enroll result".into()))
}
pub fn enroll_capture_next(
&mut self,
template_id: &[u8],
timeout: Option<u32>,
on_keepalive: Option<&mut dyn FnMut(u8)>,
cancel: Option<&dyn Fn() -> bool>,
) -> Result<EnrollSampleResult, Ctap2Error<C::Error>> {
log::debug!("Capturing fingerprint sample");
let mut sub_entries = vec![(Value::Int(0x01), Value::Bytes(template_id.to_vec()))];
if let Some(t) = timeout {
sub_entries.push((Value::Int(0x03), Value::Int(t as i64)));
}
let sub_params = Value::Map(sub_entries);
let resp = self.call(
Some(0x01),
Some(bio_cmd::ENROLL_CAPTURE_NEXT),
Some(&sub_params),
true,
on_keepalive,
cancel,
)?;
EnrollSampleResult::from_cbor_with_template_id(&resp, Some(template_id))
.ok_or_else(|| Ctap2Error::InvalidResponse("invalid enroll result".into()))
}
pub fn enroll_cancel(&mut self) -> Result<(), Ctap2Error<C::Error>> {
log::debug!("Cancelling fingerprint enrollment");
self.call(
Some(0x01),
Some(bio_cmd::ENROLL_CANCEL),
None,
false,
None,
None,
)?;
Ok(())
}
pub fn enumerate_enrollments(
&mut self,
) -> Result<Vec<FingerprintTemplate>, Ctap2Error<C::Error>> {
log::debug!("Listing fingerprint enrollments");
let resp = self.call(
Some(0x01),
Some(bio_cmd::ENUMERATE_ENROLLMENTS),
None,
true,
None,
None,
)?;
let infos = resp
.map_get_int(bio_result_key::TEMPLATE_INFOS)
.and_then(|v| v.as_array())
.map(|arr| {
arr.iter()
.filter_map(FingerprintTemplate::from_cbor)
.collect()
})
.unwrap_or_default();
Ok(infos)
}
pub fn set_name(&mut self, template_id: &[u8], name: &str) -> Result<(), Ctap2Error<C::Error>> {
log::debug!("Renaming fingerprint template");
let sub_params = Value::Map(vec![
(Value::Int(0x01), Value::Bytes(template_id.to_vec())),
(Value::Int(0x02), Value::Text(name.to_string())),
]);
self.call(
Some(0x01),
Some(bio_cmd::SET_NAME),
Some(&sub_params),
true,
None,
None,
)?;
log::info!("Fingerprint template renamed");
Ok(())
}
pub fn remove_enrollment(&mut self, template_id: &[u8]) -> Result<(), Ctap2Error<C::Error>> {
log::debug!("Removing fingerprint enrollment");
let sub_params = Value::Map(vec![(Value::Int(0x01), Value::Bytes(template_id.to_vec()))]);
self.call(
Some(0x01),
Some(bio_cmd::REMOVE_ENROLLMENT),
Some(&sub_params),
true,
None,
None,
)?;
log::info!("Fingerprint enrollment removed");
Ok(())
}
}