| 1 |
use crate::{ |
| 2 |
crypto::{PinUvAuthParam, PinUvAuthToken}, |
| 3 |
ctap2::server::UserVerificationRequirement, |
| 4 |
errors::AuthenticatorError, |
| 5 |
transport::errors::HIDError, |
| 6 |
AuthenticatorInfo, FidoDevice, |
| 7 |
}; |
| 8 |
use serde::{ |
| 9 |
de::{Error as SerdeError, IgnoredAny, MapAccess, Visitor}, |
| 10 |
Deserialize, Deserializer, Serialize, Serializer, |
| 11 |
}; |
| 12 |
use serde_bytes::ByteBuf; |
| 13 |
use serde_cbor::{from_slice, to_vec, Value}; |
| 14 |
use std::fmt; |
| 15 |
|
| 16 |
use super::{Command, CommandError, CtapResponse, PinUvAuthCommand, RequestCtap2, StatusCode}; |
| 17 |
|
| 18 |
#[derive(Debug, Clone, Copy)] |
| 19 |
pub enum BioEnrollmentModality { |
| 20 |
Fingerprint, |
| 21 |
Other(u8), |
| 22 |
} |
| 23 |
|
| 24 |
impl From<u8> for BioEnrollmentModality { |
| 25 |
fn from(value: u8) -> Self { |
| 26 |
match value { |
| 27 |
0x01 => BioEnrollmentModality::Fingerprint, |
| 28 |
x => BioEnrollmentModality::Other(x), |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
impl From<BioEnrollmentModality> for u8 { |
| 34 |
fn from(value: BioEnrollmentModality) -> Self { |
| 35 |
match value { |
| 36 |
BioEnrollmentModality::Fingerprint => 0x01, |
| 37 |
BioEnrollmentModality::Other(x) => x, |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
impl Serialize for BioEnrollmentModality { |
| 43 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 44 |
where |
| 45 |
S: Serializer, |
| 46 |
{ |
| 47 |
serializer.serialize_u8((*self).into()) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
impl<'de> Deserialize<'de> for BioEnrollmentModality { |
| 52 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 53 |
where |
| 54 |
D: Deserializer<'de>, |
| 55 |
{ |
| 56 |
struct BioEnrollmentModalityVisitor; |
| 57 |
|
| 58 |
impl<'de> Visitor<'de> for BioEnrollmentModalityVisitor { |
| 59 |
type Value = BioEnrollmentModality; |
| 60 |
|
| 61 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 62 |
formatter.write_str("an integer") |
| 63 |
} |
| 64 |
|
| 65 |
fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E> |
| 66 |
where |
| 67 |
E: SerdeError, |
| 68 |
{ |
| 69 |
Ok(BioEnrollmentModality::from(v)) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
deserializer.deserialize_u8(BioEnrollmentModalityVisitor) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
pub type BioTemplateId = Vec<u8>; |
| 78 |
#[derive(Debug, Clone, Deserialize, Default)] |
| 79 |
struct BioEnrollmentParams { |
| 80 |
template_id: Option<BioTemplateId>, // Template Identifier. |
| 81 |
template_friendly_name: Option<String>, // Template Friendly Name. |
| 82 |
timeout_milliseconds: Option<u64>, // Timeout in milliSeconds. |
| 83 |
} |
| 84 |
|
| 85 |
impl BioEnrollmentParams { |
| 86 |
fn has_some(&self) -> bool { |
| 87 |
self.template_id.is_some() |
| 88 |
|| self.template_friendly_name.is_some() |
| 89 |
|| self.timeout_milliseconds.is_some() |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
impl Serialize for BioEnrollmentParams { |
| 94 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 95 |
where |
| 96 |
S: Serializer, |
| 97 |
{ |
| 98 |
serialize_map_optional!( |
| 99 |
serializer, |
| 100 |
&0x01 => self.template_id.as_deref().map(ByteBuf::from), |
| 101 |
&0x02 => &self.template_friendly_name, |
| 102 |
&0x03 => self.timeout_milliseconds, |
| 103 |
) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
#[derive(Debug)] |
| 108 |
pub enum BioEnrollmentCommand { |
| 109 |
EnrollBegin(Option<u64>), |
| 110 |
EnrollCaptureNextSample((BioTemplateId, Option<u64>)), |
| 111 |
CancelCurrentEnrollment, |
| 112 |
EnumerateEnrollments, |
| 113 |
SetFriendlyName((BioTemplateId, String)), |
| 114 |
RemoveEnrollment(BioTemplateId), |
| 115 |
GetFingerprintSensorInfo, |
| 116 |
} |
| 117 |
|
| 118 |
impl BioEnrollmentCommand { |
| 119 |
fn to_id_and_param(&self) -> (u8, BioEnrollmentParams) { |
| 120 |
let mut params = BioEnrollmentParams::default(); |
| 121 |
match &self { |
| 122 |
BioEnrollmentCommand::EnrollBegin(timeout) => { |
| 123 |
params.timeout_milliseconds = *timeout; |
| 124 |
(0x01, params) |
| 125 |
} |
| 126 |
BioEnrollmentCommand::EnrollCaptureNextSample((id, timeout)) => { |
| 127 |
params.template_id = Some(id.clone()); |
| 128 |
params.timeout_milliseconds = *timeout; |
| 129 |
(0x02, params) |
| 130 |
} |
| 131 |
BioEnrollmentCommand::CancelCurrentEnrollment => (0x03, params), |
| 132 |
BioEnrollmentCommand::EnumerateEnrollments => (0x04, params), |
| 133 |
BioEnrollmentCommand::SetFriendlyName((id, name)) => { |
| 134 |
params.template_id = Some(id.clone()); |
| 135 |
params.template_friendly_name = Some(name.clone()); |
| 136 |
(0x05, params) |
| 137 |
} |
| 138 |
BioEnrollmentCommand::RemoveEnrollment(id) => { |
| 139 |
params.template_id = Some(id.clone()); |
| 140 |
(0x06, params) |
| 141 |
} |
| 142 |
BioEnrollmentCommand::GetFingerprintSensorInfo => (0x07, params), |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
#[derive(Debug)] |
| 148 |
pub struct BioEnrollment { |
| 149 |
/// The user verification modality being requested |
| 150 |
modality: BioEnrollmentModality, |
| 151 |
/// The authenticator user verification sub command currently being requested |
| 152 |
pub(crate) subcommand: BioEnrollmentCommand, |
| 153 |
/// First 16 bytes of HMAC-SHA-256 of contents using pinUvAuthToken. |
| 154 |
pin_uv_auth_param: Option<PinUvAuthParam>, |
| 155 |
use_legacy_preview: bool, |
| 156 |
} |
| 157 |
|
| 158 |
impl BioEnrollment { |
| 159 |
pub(crate) fn new(subcommand: BioEnrollmentCommand, use_legacy_preview: bool) -> Self { |
| 160 |
Self { |
| 161 |
modality: BioEnrollmentModality::Fingerprint, // As per spec: Currently always "Fingerprint" |
| 162 |
subcommand, |
| 163 |
pin_uv_auth_param: None, |
| 164 |
use_legacy_preview, |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
impl Serialize for BioEnrollment { |
| 170 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 171 |
where |
| 172 |
S: Serializer, |
| 173 |
{ |
| 174 |
let (id, params) = self.subcommand.to_id_and_param(); |
| 175 |
serialize_map_optional!( |
| 176 |
serializer, |
| 177 |
&0x01 => Some(&self.modality), // Per spec currently always Fingerprint |
| 178 |
&0x02 => Some(&id), |
| 179 |
&0x03 => params.has_some().then_some(¶ms), |
| 180 |
&0x04 => self.pin_uv_auth_param.as_ref().map(|p| p.pin_protocol.id()), |
| 181 |
&0x05 => &self.pin_uv_auth_param, |
| 182 |
) |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
impl PinUvAuthCommand for BioEnrollment { |
| 187 |
fn get_rp_id(&self) -> Option<&String> { |
| 188 |
None |
| 189 |
} |
| 190 |
|
| 191 |
fn set_pin_uv_auth_param( |
| 192 |
&mut self, |
| 193 |
pin_uv_auth_token: Option<PinUvAuthToken>, |
| 194 |
) -> Result<(), AuthenticatorError> { |
| 195 |
let mut param = None; |
| 196 |
if let Some(token) = pin_uv_auth_token { |
| 197 |
// pinUvAuthParam (0x04): the result of calling |
| 198 |
// authenticate(pinUvAuthToken, fingerprint (0x01) || uint8(subCommand) || subCommandParams). |
| 199 |
let (id, params) = self.subcommand.to_id_and_param(); |
| 200 |
let modality = self.modality.into(); |
| 201 |
let mut data = vec![modality, id]; |
| 202 |
if params.has_some() { |
| 203 |
data.extend(to_vec(¶ms).map_err(CommandError::Serializing)?); |
| 204 |
} |
| 205 |
param = Some(token.derive(&data).map_err(CommandError::Crypto)?); |
| 206 |
} |
| 207 |
self.pin_uv_auth_param = param; |
| 208 |
Ok(()) |
| 209 |
} |
| 210 |
|
| 211 |
fn can_skip_user_verification( |
| 212 |
&mut self, |
| 213 |
_info: &crate::AuthenticatorInfo, |
| 214 |
_uv: UserVerificationRequirement, |
| 215 |
) -> bool { |
| 216 |
// "discouraged" does not exist for BioEnrollment |
| 217 |
false |
| 218 |
} |
| 219 |
|
| 220 |
fn set_uv_option(&mut self, _uv: Option<bool>) { |
| 221 |
/* No-op */ |
| 222 |
} |
| 223 |
|
| 224 |
fn get_pin_uv_auth_param(&self) -> Option<&PinUvAuthParam> { |
| 225 |
self.pin_uv_auth_param.as_ref() |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
impl RequestCtap2 for BioEnrollment { |
| 230 |
type Output = BioEnrollmentResponse; |
| 231 |
|
| 232 |
fn command(&self) -> Command { |
| 233 |
if self.use_legacy_preview { |
| 234 |
Command::BioEnrollmentPreview |
| 235 |
} else { |
| 236 |
Command::BioEnrollment |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
fn wire_format(&self) -> Result<Vec<u8>, HIDError> { |
| 241 |
let output = to_vec(&self).map_err(CommandError::Serializing)?; |
| 242 |
trace!("client subcommmand: {:04X?}", &output); |
| 243 |
Ok(output) |
| 244 |
} |
| 245 |
|
| 246 |
fn handle_response_ctap2<Dev>( |
| 247 |
&self, |
| 248 |
_dev: &mut Dev, |
| 249 |
input: &[u8], |
| 250 |
) -> Result<Self::Output, HIDError> |
| 251 |
where |
| 252 |
Dev: FidoDevice, |
| 253 |
{ |
| 254 |
if input.is_empty() { |
| 255 |
return Err(CommandError::InputTooSmall.into()); |
| 256 |
} |
| 257 |
|
| 258 |
let status: StatusCode = input[0].into(); |
| 259 |
if status.is_ok() { |
| 260 |
if input.len() > 1 { |
| 261 |
trace!("parsing bio enrollment response data: {:#04X?}", &input); |
| 262 |
let bio_enrollment = |
| 263 |
from_slice(&input[1..]).map_err(CommandError::Deserializing)?; |
| 264 |
Ok(bio_enrollment) |
| 265 |
} else { |
| 266 |
// Some subcommands return only an OK-status without any data |
| 267 |
Ok(BioEnrollmentResponse::default()) |
| 268 |
} |
| 269 |
} else { |
| 270 |
let data: Option<Value> = if input.len() > 1 { |
| 271 |
Some(from_slice(&input[1..]).map_err(CommandError::Deserializing)?) |
| 272 |
} else { |
| 273 |
None |
| 274 |
}; |
| 275 |
Err(CommandError::StatusCode(status, data).into()) |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
fn send_to_virtual_device<Dev: crate::VirtualFidoDevice>( |
| 280 |
&self, |
| 281 |
_dev: &mut Dev, |
| 282 |
) -> Result<Self::Output, HIDError> { |
| 283 |
unimplemented!() |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
#[derive(Debug, Copy, Clone, Serialize)] |
| 288 |
pub enum LastEnrollmentSampleStatus { |
| 289 |
/// Good fingerprint capture. |
| 290 |
Ctap2EnrollFeedbackFpGood, |
| 291 |
/// Fingerprint was too high. |
| 292 |
Ctap2EnrollFeedbackFpTooHigh, |
| 293 |
/// Fingerprint was too low. |
| 294 |
Ctap2EnrollFeedbackFpTooLow, |
| 295 |
/// Fingerprint was too left. |
| 296 |
Ctap2EnrollFeedbackFpTooLeft, |
| 297 |
/// Fingerprint was too right. |
| 298 |
Ctap2EnrollFeedbackFpTooRight, |
| 299 |
/// Fingerprint was too fast. |
| 300 |
Ctap2EnrollFeedbackFpTooFast, |
| 301 |
/// Fingerprint was too slow. |
| 302 |
Ctap2EnrollFeedbackFpTooSlow, |
| 303 |
/// Fingerprint was of poor quality. |
| 304 |
Ctap2EnrollFeedbackFpPoorQuality, |
| 305 |
/// Fingerprint was too skewed. |
| 306 |
Ctap2EnrollFeedbackFpTooSkewed, |
| 307 |
/// Fingerprint was too short. |
| 308 |
Ctap2EnrollFeedbackFpTooShort, |
| 309 |
/// Merge failure of the capture. |
| 310 |
Ctap2EnrollFeedbackFpMergeFailure, |
| 311 |
/// Fingerprint already exists. |
| 312 |
Ctap2EnrollFeedbackFpExists, |
| 313 |
/// (this error number is available) |
| 314 |
Unused, |
| 315 |
/// User did not touch/swipe the authenticator. |
| 316 |
Ctap2EnrollFeedbackNoUserActivity, |
| 317 |
/// User did not lift the finger off the sensor. |
| 318 |
Ctap2EnrollFeedbackNoUserPresenceTransition, |
| 319 |
/// Other possible failure cases that are not (yet) defined by the spec |
| 320 |
Ctap2EnrollFeedbackOther(u8), |
| 321 |
} |
| 322 |
|
| 323 |
impl<'de> Deserialize<'de> for LastEnrollmentSampleStatus { |
| 324 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 325 |
where |
| 326 |
D: Deserializer<'de>, |
| 327 |
{ |
| 328 |
struct LastEnrollmentSampleStatusVisitor; |
| 329 |
|
| 330 |
impl<'de> Visitor<'de> for LastEnrollmentSampleStatusVisitor { |
| 331 |
type Value = LastEnrollmentSampleStatus; |
| 332 |
|
| 333 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 334 |
formatter.write_str("an integer") |
| 335 |
} |
| 336 |
|
| 337 |
fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E> |
| 338 |
where |
| 339 |
E: SerdeError, |
| 340 |
{ |
| 341 |
match v { |
| 342 |
0x00 => Ok(LastEnrollmentSampleStatus::Ctap2EnrollFeedbackFpGood), |
| 343 |
0x01 => Ok(LastEnrollmentSampleStatus::Ctap2EnrollFeedbackFpTooHigh), |
| 344 |
0x02 => Ok(LastEnrollmentSampleStatus::Ctap2EnrollFeedbackFpTooLow), |
| 345 |
0x03 => Ok(LastEnrollmentSampleStatus::Ctap2EnrollFeedbackFpTooLeft), |
| 346 |
0x04 => Ok(LastEnrollmentSampleStatus::Ctap2EnrollFeedbackFpTooRight), |
| 347 |
0x05 => Ok(LastEnrollmentSampleStatus::Ctap2EnrollFeedbackFpTooFast), |
| 348 |
0x06 => Ok(LastEnrollmentSampleStatus::Ctap2EnrollFeedbackFpTooSlow), |
| 349 |
0x07 => Ok(LastEnrollmentSampleStatus::Ctap2EnrollFeedbackFpPoorQuality), |
| 350 |
0x08 => Ok(LastEnrollmentSampleStatus::Ctap2EnrollFeedbackFpTooSkewed), |
| 351 |
0x09 => Ok(LastEnrollmentSampleStatus::Ctap2EnrollFeedbackFpTooShort), |
| 352 |
0x0A => Ok(LastEnrollmentSampleStatus::Ctap2EnrollFeedbackFpMergeFailure), |
| 353 |
0x0B => Ok(LastEnrollmentSampleStatus::Ctap2EnrollFeedbackFpExists), |
| 354 |
0x0C => Ok(LastEnrollmentSampleStatus::Unused), |
| 355 |
0x0D => Ok(LastEnrollmentSampleStatus::Ctap2EnrollFeedbackNoUserActivity), |
| 356 |
0x0E => { |
| 357 |
Ok(LastEnrollmentSampleStatus::Ctap2EnrollFeedbackNoUserPresenceTransition) |
| 358 |
} |
| 359 |
x => Ok(LastEnrollmentSampleStatus::Ctap2EnrollFeedbackOther(x)), |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
deserializer.deserialize_u8(LastEnrollmentSampleStatusVisitor) |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
#[derive(Debug, Copy, Clone, Serialize)] |
| 369 |
pub enum FingerprintKind { |
| 370 |
TouchSensor, |
| 371 |
SwipeSensor, |
| 372 |
// Not (yet) defined by the spec |
| 373 |
Other(u8), |
| 374 |
} |
| 375 |
|
| 376 |
impl<'de> Deserialize<'de> for FingerprintKind { |
| 377 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 378 |
where |
| 379 |
D: Deserializer<'de>, |
| 380 |
{ |
| 381 |
struct FingerprintKindVisitor; |
| 382 |
|
| 383 |
impl<'de> Visitor<'de> for FingerprintKindVisitor { |
| 384 |
type Value = FingerprintKind; |
| 385 |
|
| 386 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 387 |
formatter.write_str("an integer") |
| 388 |
} |
| 389 |
|
| 390 |
fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E> |
| 391 |
where |
| 392 |
E: SerdeError, |
| 393 |
{ |
| 394 |
match v { |
| 395 |
0x01 => Ok(FingerprintKind::TouchSensor), |
| 396 |
0x02 => Ok(FingerprintKind::SwipeSensor), |
| 397 |
x => Ok(FingerprintKind::Other(x)), |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
deserializer.deserialize_u8(FingerprintKindVisitor) |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
#[derive(Debug, Serialize)] |
| 407 |
pub(crate) struct BioTemplateInfo { |
| 408 |
template_id: BioTemplateId, |
| 409 |
template_friendly_name: Option<String>, |
| 410 |
} |
| 411 |
|
| 412 |
impl<'de> Deserialize<'de> for BioTemplateInfo { |
| 413 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 414 |
where |
| 415 |
D: Deserializer<'de>, |
| 416 |
{ |
| 417 |
struct BioTemplateInfoResponseVisitor; |
| 418 |
|
| 419 |
impl<'de> Visitor<'de> for BioTemplateInfoResponseVisitor { |
| 420 |
type Value = BioTemplateInfo; |
| 421 |
|
| 422 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 423 |
formatter.write_str("a map") |
| 424 |
} |
| 425 |
|
| 426 |
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error> |
| 427 |
where |
| 428 |
M: MapAccess<'de>, |
| 429 |
{ |
| 430 |
let mut template_id = None; // (0x01) |
| 431 |
let mut template_friendly_name = None; // (0x02) |
| 432 |
while let Some(key) = map.next_key()? { |
| 433 |
match key { |
| 434 |
0x01 => { |
| 435 |
if template_id.is_some() { |
| 436 |
return Err(SerdeError::duplicate_field("template_id")); |
| 437 |
} |
| 438 |
template_id = Some(map.next_value::<ByteBuf>()?.into_vec()); |
| 439 |
} |
| 440 |
0x02 => { |
| 441 |
if template_friendly_name.is_some() { |
| 442 |
return Err(SerdeError::duplicate_field("template_friendly_name")); |
| 443 |
} |
| 444 |
template_friendly_name = Some(map.next_value()?); |
| 445 |
} |
| 446 |
k => { |
| 447 |
warn!("BioTemplateInfo: unexpected key: {:?}", k); |
| 448 |
let _ = map.next_value::<IgnoredAny>()?; |
| 449 |
continue; |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
if let Some(template_id) = template_id { |
| 455 |
Ok(BioTemplateInfo { |
| 456 |
template_id, |
| 457 |
template_friendly_name, |
| 458 |
}) |
| 459 |
} else { |
| 460 |
Err(SerdeError::missing_field("template_id")) |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
deserializer.deserialize_bytes(BioTemplateInfoResponseVisitor) |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
#[derive(Default, Debug)] |
| 469 |
pub struct BioEnrollmentResponse { |
| 470 |
/// The user verification modality. |
| 471 |
pub(crate) modality: Option<BioEnrollmentModality>, |
| 472 |
/// Indicates the type of fingerprint sensor. For touch type sensor, its value is 1. For swipe type sensor its value is 2. |
| 473 |
pub(crate) fingerprint_kind: Option<FingerprintKind>, |
| 474 |
/// Indicates the maximum good samples required for enrollment. |
| 475 |
pub(crate) max_capture_samples_required_for_enroll: Option<u64>, |
| 476 |
/// Template Identifier. |
| 477 |
pub(crate) template_id: Option<BioTemplateId>, |
| 478 |
/// Last enrollment sample status. |
| 479 |
pub(crate) last_enroll_sample_status: Option<LastEnrollmentSampleStatus>, |
| 480 |
/// Number of more sample required for enrollment to complete |
| 481 |
pub(crate) remaining_samples: Option<u64>, |
| 482 |
/// Array of templateInfo’s |
| 483 |
pub(crate) template_infos: Vec<BioTemplateInfo>, |
| 484 |
/// Indicates the maximum number of bytes the authenticator will accept as a templateFriendlyName. |
| 485 |
pub(crate) max_template_friendly_name: Option<u64>, |
| 486 |
} |
| 487 |
|
| 488 |
impl CtapResponse for BioEnrollmentResponse {} |
| 489 |
|
| 490 |
impl<'de> Deserialize<'de> for BioEnrollmentResponse { |
| 491 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 492 |
where |
| 493 |
D: Deserializer<'de>, |
| 494 |
{ |
| 495 |
struct BioEnrollmentResponseVisitor; |
| 496 |
|
| 497 |
impl<'de> Visitor<'de> for BioEnrollmentResponseVisitor { |
| 498 |
type Value = BioEnrollmentResponse; |
| 499 |
|
| 500 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 501 |
formatter.write_str("a map") |
| 502 |
} |
| 503 |
|
| 504 |
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error> |
| 505 |
where |
| 506 |
M: MapAccess<'de>, |
| 507 |
{ |
| 508 |
let mut modality = None; // (0x01) |
| 509 |
let mut fingerprint_kind = None; // (0x02) |
| 510 |
let mut max_capture_samples_required_for_enroll = None; // (0x03) |
| 511 |
let mut template_id = None; // (0x04) |
| 512 |
let mut last_enroll_sample_status = None; // (0x05) |
| 513 |
let mut remaining_samples = None; // (0x06) |
| 514 |
let mut template_infos = None; // (0x07) |
| 515 |
let mut max_template_friendly_name = None; // (0x08) |
| 516 |
|
| 517 |
while let Some(key) = map.next_key()? { |
| 518 |
match key { |
| 519 |
0x01 => { |
| 520 |
if modality.is_some() { |
| 521 |
return Err(SerdeError::duplicate_field("modality")); |
| 522 |
} |
| 523 |
modality = Some(map.next_value()?); |
| 524 |
} |
| 525 |
0x02 => { |
| 526 |
if fingerprint_kind.is_some() { |
| 527 |
return Err(SerdeError::duplicate_field("fingerprint_kind")); |
| 528 |
} |
| 529 |
fingerprint_kind = Some(map.next_value()?); |
| 530 |
} |
| 531 |
0x03 => { |
| 532 |
if max_capture_samples_required_for_enroll.is_some() { |
| 533 |
return Err(SerdeError::duplicate_field( |
| 534 |
"max_capture_samples_required_for_enroll", |
| 535 |
)); |
| 536 |
} |
| 537 |
max_capture_samples_required_for_enroll = Some(map.next_value()?); |
| 538 |
} |
| 539 |
0x04 => { |
| 540 |
if template_id.is_some() { |
| 541 |
return Err(SerdeError::duplicate_field("template_id")); |
| 542 |
} |
| 543 |
template_id = Some(map.next_value::<ByteBuf>()?.into_vec()); |
| 544 |
} |
| 545 |
0x05 => { |
| 546 |
if last_enroll_sample_status.is_some() { |
| 547 |
return Err(SerdeError::duplicate_field( |
| 548 |
"last_enroll_sample_status", |
| 549 |
)); |
| 550 |
} |
| 551 |
last_enroll_sample_status = Some(map.next_value()?); |
| 552 |
} |
| 553 |
0x06 => { |
| 554 |
if remaining_samples.is_some() { |
| 555 |
return Err(SerdeError::duplicate_field("remaining_samples")); |
| 556 |
} |
| 557 |
remaining_samples = Some(map.next_value()?); |
| 558 |
} |
| 559 |
0x07 => { |
| 560 |
if template_infos.is_some() { |
| 561 |
return Err(SerdeError::duplicate_field("template_infos")); |
| 562 |
} |
| 563 |
template_infos = Some(map.next_value()?); |
| 564 |
} |
| 565 |
0x08 => { |
| 566 |
if max_template_friendly_name.is_some() { |
| 567 |
return Err(SerdeError::duplicate_field( |
| 568 |
"max_template_friendly_name", |
| 569 |
)); |
| 570 |
} |
| 571 |
max_template_friendly_name = Some(map.next_value()?); |
| 572 |
} |
| 573 |
k => { |
| 574 |
warn!("BioEnrollmentResponse: unexpected key: {:?}", k); |
| 575 |
let _ = map.next_value::<IgnoredAny>()?; |
| 576 |
continue; |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
Ok(BioEnrollmentResponse { |
| 582 |
modality, |
| 583 |
fingerprint_kind, |
| 584 |
max_capture_samples_required_for_enroll, |
| 585 |
template_id, |
| 586 |
last_enroll_sample_status, |
| 587 |
remaining_samples, |
| 588 |
template_infos: template_infos.unwrap_or_default(), |
| 589 |
max_template_friendly_name, |
| 590 |
}) |
| 591 |
} |
| 592 |
} |
| 593 |
deserializer.deserialize_bytes(BioEnrollmentResponseVisitor) |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
#[derive(Debug, Serialize)] |
| 598 |
pub struct EnrollmentInfo { |
| 599 |
pub template_id: Vec<u8>, |
| 600 |
pub template_friendly_name: Option<String>, |
| 601 |
} |
| 602 |
|
| 603 |
impl From<&BioTemplateInfo> for EnrollmentInfo { |
| 604 |
fn from(value: &BioTemplateInfo) -> Self { |
| 605 |
Self { |
| 606 |
template_id: value.template_id.to_vec(), |
| 607 |
template_friendly_name: value.template_friendly_name.clone(), |
| 608 |
} |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
#[derive(Debug, Serialize)] |
| 613 |
pub struct FingerprintSensorInfo { |
| 614 |
pub fingerprint_kind: FingerprintKind, |
| 615 |
pub max_capture_samples_required_for_enroll: u64, |
| 616 |
pub max_template_friendly_name: Option<u64>, |
| 617 |
} |
| 618 |
|
| 619 |
#[derive(Debug, Serialize)] |
| 620 |
pub enum BioEnrollmentResult { |
| 621 |
EnrollmentList(Vec<EnrollmentInfo>), |
| 622 |
DeleteSuccess(AuthenticatorInfo), |
| 623 |
UpdateSuccess, |
| 624 |
AddSuccess(AuthenticatorInfo), |
| 625 |
FingerprintSensorInfo(FingerprintSensorInfo), |
| 626 |
SampleStatus(LastEnrollmentSampleStatus, u64), |
| 627 |
} |
| 628 |
|
| 629 |
#[cfg(test)] |
| 630 |
mod test { |
| 631 |
use crate::{crypto::PinUvAuthParam, ctap2::commands::client_pin::PinUvAuthTokenPermission}; |
| 632 |
|
| 633 |
use super::{BioEnrollment, BioEnrollmentCommand, BioEnrollmentModality, BioEnrollmentParams}; |
| 634 |
|
| 635 |
#[test] |
| 636 |
fn test_serialize_bio_enrollment_params() { |
| 637 |
let bio_enrollment_params = BioEnrollmentParams { |
| 638 |
template_id: Some(vec![1, 2, 3, 4]), |
| 639 |
template_friendly_name: Some("thumb".to_string()), |
| 640 |
timeout_milliseconds: Some(1337), |
| 641 |
}; |
| 642 |
let serialized = |
| 643 |
serde_cbor::ser::to_vec(&bio_enrollment_params).expect("Failed to serialize to CBOR"); |
| 644 |
assert_eq!( |
| 645 |
serialized, |
| 646 |
[ |
| 647 |
// Value copied from test failure output as regression test snapshot |
| 648 |
163, 1, 68, 1, 2, 3, 4, 2, 101, 116, 104, 117, 109, 98, 3, 25, 5, 57 |
| 649 |
] |
| 650 |
); |
| 651 |
} |
| 652 |
|
| 653 |
#[test] |
| 654 |
fn test_serialize_bio_enrollment() { |
| 655 |
let bio_enrollment_params = BioEnrollment { |
| 656 |
modality: BioEnrollmentModality::Other(42), |
| 657 |
subcommand: BioEnrollmentCommand::SetFriendlyName(( |
| 658 |
vec![1, 2, 3, 4], |
| 659 |
"thumb".to_string(), |
| 660 |
)), |
| 661 |
pin_uv_auth_param: Some(PinUvAuthParam::create_test( |
| 662 |
2, |
| 663 |
vec![1, 2, 3, 4], |
| 664 |
PinUvAuthTokenPermission::CredentialManagement, |
| 665 |
)), |
| 666 |
use_legacy_preview: true, |
| 667 |
}; |
| 668 |
let serialized = |
| 669 |
serde_cbor::ser::to_vec(&bio_enrollment_params).expect("Failed to serialize to CBOR"); |
| 670 |
assert_eq!( |
| 671 |
serialized, |
| 672 |
[ |
| 673 |
// Value copied from test failure output as regression test snapshot |
| 674 |
165, 1, 24, 42, 2, 5, 3, 162, 1, 68, 1, 2, 3, 4, 2, 101, 116, 104, 117, 109, 98, 4, |
| 675 |
2, 5, 68, 1, 2, 3, 4 |
| 676 |
] |
| 677 |
); |
| 678 |
} |
| 679 |
} |