| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use authenticator::{ |
| 6 |
authenticatorservice::AuthenticatorService, |
| 7 |
ctap2::commands::{ |
| 8 |
authenticator_config::{AuthConfigCommand, AuthConfigResult, SetMinPINLength}, |
| 9 |
bio_enrollment::EnrollmentInfo, |
| 10 |
credential_management::CredentialList, |
| 11 |
PinUvAuthResult, |
| 12 |
}, |
| 13 |
errors::AuthenticatorError, |
| 14 |
statecallback::StateCallback, |
| 15 |
AuthenticatorInfo, BioEnrollmentCmd, BioEnrollmentResult, CredManagementCmd, |
| 16 |
CredentialManagementResult, InteractiveRequest, InteractiveUpdate, ManageResult, Pin, |
| 17 |
StatusPinUv, StatusUpdate, |
| 18 |
}; |
| 19 |
use getopts::Options; |
| 20 |
use log::debug; |
| 21 |
use std::{env, io, sync::mpsc::Sender, thread}; |
| 22 |
use std::{ |
| 23 |
fmt::Display, |
| 24 |
io::Write, |
| 25 |
sync::mpsc::{channel, Receiver, RecvError}, |
| 26 |
}; |
| 27 |
|
| 28 |
#[derive(Debug, PartialEq, Clone)] |
| 29 |
enum PinOperation { |
| 30 |
Set, |
| 31 |
Change, |
| 32 |
} |
| 33 |
|
| 34 |
#[derive(Debug, Clone, PartialEq)] |
| 35 |
enum ConfigureOperation { |
| 36 |
ToggleAlwaysUV, |
| 37 |
EnableEnterpriseAttestation, |
| 38 |
SetMinPINLength, |
| 39 |
} |
| 40 |
|
| 41 |
impl ConfigureOperation { |
| 42 |
fn ask_user(info: &AuthenticatorInfo) -> Self { |
| 43 |
let sub_level = Self::parse_possible_operations(info); |
| 44 |
println!(); |
| 45 |
println!("What do you wish to do?"); |
| 46 |
let choice = ask_user_choice(&sub_level); |
| 47 |
sub_level[choice].clone() |
| 48 |
} |
| 49 |
|
| 50 |
fn parse_possible_operations(info: &AuthenticatorInfo) -> Vec<Self> { |
| 51 |
let mut configure_ops = vec![]; |
| 52 |
if info.options.authnr_cfg == Some(true) && info.options.always_uv.is_some() { |
| 53 |
configure_ops.push(ConfigureOperation::ToggleAlwaysUV); |
| 54 |
} |
| 55 |
if info.options.authnr_cfg == Some(true) && info.options.set_min_pin_length.is_some() { |
| 56 |
configure_ops.push(ConfigureOperation::SetMinPINLength); |
| 57 |
} |
| 58 |
if info.options.ep.is_some() { |
| 59 |
configure_ops.push(ConfigureOperation::EnableEnterpriseAttestation); |
| 60 |
} |
| 61 |
configure_ops |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
impl Display for ConfigureOperation { |
| 66 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 67 |
match self { |
| 68 |
ConfigureOperation::ToggleAlwaysUV => write!(f, "Toggle option 'Always UV'"), |
| 69 |
ConfigureOperation::EnableEnterpriseAttestation => { |
| 70 |
write!(f, "Enable Enterprise attestation") |
| 71 |
} |
| 72 |
ConfigureOperation::SetMinPINLength => write!(f, "Set min. PIN length"), |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
#[derive(Debug, Clone, PartialEq)] |
| 78 |
enum CredentialsOperation { |
| 79 |
List, |
| 80 |
Delete, |
| 81 |
UpdateUser, |
| 82 |
} |
| 83 |
|
| 84 |
impl CredentialsOperation { |
| 85 |
fn ask_user(info: &AuthenticatorInfo, creds: &CredentialList) -> Self { |
| 86 |
let sub_level = Self::parse_possible_operations(info, creds); |
| 87 |
println!(); |
| 88 |
println!("What do you wish to do?"); |
| 89 |
let choice = ask_user_choice(&sub_level); |
| 90 |
sub_level[choice].clone() |
| 91 |
} |
| 92 |
|
| 93 |
fn parse_possible_operations(info: &AuthenticatorInfo, creds: &CredentialList) -> Vec<Self> { |
| 94 |
let mut credentials_ops = vec![]; |
| 95 |
if info.options.cred_mgmt == Some(true) |
| 96 |
|| info.options.credential_mgmt_preview == Some(true) |
| 97 |
{ |
| 98 |
credentials_ops.push(CredentialsOperation::List); |
| 99 |
} |
| 100 |
if creds.existing_resident_credentials_count > 0 { |
| 101 |
credentials_ops.push(CredentialsOperation::Delete); |
| 102 |
// FIDO_2_1_PRE devices do not (all?) support UpdateUser. |
| 103 |
// So we require devices to support full 2.1 for this. |
| 104 |
if info |
| 105 |
.versions |
| 106 |
.contains(&authenticator::ctap2::commands::get_info::AuthenticatorVersion::FIDO_2_1) |
| 107 |
{ |
| 108 |
credentials_ops.push(CredentialsOperation::UpdateUser); |
| 109 |
} |
| 110 |
} |
| 111 |
credentials_ops |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
impl Display for CredentialsOperation { |
| 116 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 117 |
match self { |
| 118 |
CredentialsOperation::List => write!(f, "List credentials"), |
| 119 |
CredentialsOperation::Delete => write!(f, "Delete credentials"), |
| 120 |
CredentialsOperation::UpdateUser => write!(f, "Update user info"), |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
#[derive(Debug, Clone, PartialEq)] |
| 126 |
enum BioOperation { |
| 127 |
ShowInfo, |
| 128 |
Add, |
| 129 |
List, |
| 130 |
Delete, |
| 131 |
Rename, |
| 132 |
} |
| 133 |
|
| 134 |
impl BioOperation { |
| 135 |
fn ask_user(info: &AuthenticatorInfo) -> Self { |
| 136 |
let sub_level = Self::parse_possible_operations(info); |
| 137 |
println!(); |
| 138 |
println!("What do you wish to do?"); |
| 139 |
let choice = ask_user_choice(&sub_level); |
| 140 |
sub_level[choice].clone() |
| 141 |
} |
| 142 |
|
| 143 |
fn parse_possible_operations(info: &AuthenticatorInfo) -> Vec<Self> { |
| 144 |
let mut bio_ops = vec![]; |
| 145 |
if info.options.bio_enroll.is_some() |
| 146 |
|| info.options.user_verification_mgmt_preview.is_some() |
| 147 |
{ |
| 148 |
bio_ops.extend([BioOperation::ShowInfo, BioOperation::Add]); |
| 149 |
} |
| 150 |
if info.options.bio_enroll == Some(true) |
| 151 |
|| info.options.user_verification_mgmt_preview == Some(true) |
| 152 |
{ |
| 153 |
bio_ops.extend([ |
| 154 |
BioOperation::Delete, |
| 155 |
BioOperation::List, |
| 156 |
BioOperation::Rename, |
| 157 |
]); |
| 158 |
} |
| 159 |
bio_ops |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
impl Display for BioOperation { |
| 164 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 165 |
match self { |
| 166 |
BioOperation::ShowInfo => write!(f, "Show fingerprint sensor info"), |
| 167 |
BioOperation::List => write!(f, "List enrollments"), |
| 168 |
BioOperation::Add => write!(f, "Add enrollment"), |
| 169 |
BioOperation::Delete => write!(f, "Delete enrollment"), |
| 170 |
BioOperation::Rename => write!(f, "Rename enrollment"), |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
#[derive(Debug, Clone, PartialEq)] |
| 176 |
enum TopLevelOperation { |
| 177 |
Quit, |
| 178 |
Reset, |
| 179 |
ShowFullInfo, |
| 180 |
Pin(PinOperation), |
| 181 |
Configure, |
| 182 |
Credentials, |
| 183 |
Bio, |
| 184 |
} |
| 185 |
|
| 186 |
impl Display for TopLevelOperation { |
| 187 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 188 |
match self { |
| 189 |
TopLevelOperation::Quit => write!(f, "Quit"), |
| 190 |
TopLevelOperation::Reset => write!(f, "Reset"), |
| 191 |
TopLevelOperation::ShowFullInfo => write!(f, "Show full info"), |
| 192 |
TopLevelOperation::Pin(PinOperation::Change) => write!(f, "Change Pin"), |
| 193 |
TopLevelOperation::Pin(PinOperation::Set) => write!(f, "Set Pin"), |
| 194 |
TopLevelOperation::Configure => write!(f, "Configure Authenticator"), |
| 195 |
TopLevelOperation::Credentials => write!(f, "Manage Credentials"), |
| 196 |
TopLevelOperation::Bio => write!(f, "Manage BioEnrollments"), |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
impl TopLevelOperation { |
| 202 |
fn ask_user(info: &AuthenticatorInfo) -> TopLevelOperation { |
| 203 |
let top_level = Self::parse_possible_operations(info); |
| 204 |
println!(); |
| 205 |
println!("What do you wish to do?"); |
| 206 |
let choice = ask_user_choice(&top_level); |
| 207 |
top_level[choice].clone() |
| 208 |
} |
| 209 |
|
| 210 |
fn parse_possible_operations(info: &AuthenticatorInfo) -> Vec<TopLevelOperation> { |
| 211 |
let mut ops = vec![ |
| 212 |
TopLevelOperation::Quit, |
| 213 |
TopLevelOperation::Reset, |
| 214 |
TopLevelOperation::ShowFullInfo, |
| 215 |
]; |
| 216 |
|
| 217 |
// PIN-related |
| 218 |
match info.options.client_pin { |
| 219 |
None => {} |
| 220 |
Some(true) => ops.push(TopLevelOperation::Pin(PinOperation::Change)), |
| 221 |
Some(false) => ops.push(TopLevelOperation::Pin(PinOperation::Set)), |
| 222 |
} |
| 223 |
|
| 224 |
// Authenticator-Configuration |
| 225 |
if info.options.authnr_cfg == Some(true) |
| 226 |
&& (info.options.always_uv.is_some() |
| 227 |
|| info.options.set_min_pin_length.is_some() |
| 228 |
|| info.options.ep.is_some()) |
| 229 |
{ |
| 230 |
ops.push(TopLevelOperation::Configure); |
| 231 |
} |
| 232 |
|
| 233 |
// Credential Management |
| 234 |
if info.options.cred_mgmt == Some(true) |
| 235 |
|| info.options.credential_mgmt_preview == Some(true) |
| 236 |
{ |
| 237 |
ops.push(TopLevelOperation::Credentials); |
| 238 |
} |
| 239 |
|
| 240 |
// Bio Enrollment |
| 241 |
if info.options.bio_enroll.is_some() |
| 242 |
|| info.options.user_verification_mgmt_preview.is_some() |
| 243 |
{ |
| 244 |
ops.push(TopLevelOperation::Bio); |
| 245 |
} |
| 246 |
|
| 247 |
ops |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
fn print_usage(program: &str, opts: Options) { |
| 252 |
let brief = format!("Usage: {program} [options]"); |
| 253 |
print!("{}", opts.usage(&brief)); |
| 254 |
} |
| 255 |
|
| 256 |
fn ask_user_choice<T: Display>(choices: &[T]) -> usize { |
| 257 |
for (idx, op) in choices.iter().enumerate() { |
| 258 |
println!("({idx}) {op}"); |
| 259 |
} |
| 260 |
|
| 261 |
let mut input = String::new(); |
| 262 |
loop { |
| 263 |
input.clear(); |
| 264 |
print!("Your choice: "); |
| 265 |
io::stdout() |
| 266 |
.lock() |
| 267 |
.flush() |
| 268 |
.expect("Failed to flush stdout!"); |
| 269 |
io::stdin() |
| 270 |
.read_line(&mut input) |
| 271 |
.expect("error: unable to read user input"); |
| 272 |
if let Ok(idx) = input.trim().parse::<usize>() { |
| 273 |
if idx < choices.len() { |
| 274 |
// Add a newline in case of success for better separation of in/output |
| 275 |
println!(); |
| 276 |
return idx; |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
fn handle_authenticator_config( |
| 283 |
res: Option<AuthConfigResult>, |
| 284 |
puat: Option<PinUvAuthResult>, |
| 285 |
auth_info: &mut Option<AuthenticatorInfo>, |
| 286 |
tx: &Sender<InteractiveRequest>, |
| 287 |
) { |
| 288 |
if let Some(AuthConfigResult::Success(info)) = res { |
| 289 |
println!("{:#?}", info.options); |
| 290 |
*auth_info = Some(info); |
| 291 |
} |
| 292 |
let choice = ConfigureOperation::ask_user(auth_info.as_ref().unwrap()); |
| 293 |
match choice { |
| 294 |
ConfigureOperation::ToggleAlwaysUV => { |
| 295 |
tx.send(InteractiveRequest::ChangeConfig( |
| 296 |
AuthConfigCommand::ToggleAlwaysUv, |
| 297 |
puat, |
| 298 |
)) |
| 299 |
.expect("Failed to send ToggleAlwaysUV request."); |
| 300 |
} |
| 301 |
ConfigureOperation::EnableEnterpriseAttestation => { |
| 302 |
tx.send(InteractiveRequest::ChangeConfig( |
| 303 |
AuthConfigCommand::EnableEnterpriseAttestation, |
| 304 |
puat, |
| 305 |
)) |
| 306 |
.expect("Failed to send EnableEnterpriseAttestation request."); |
| 307 |
} |
| 308 |
ConfigureOperation::SetMinPINLength => { |
| 309 |
let mut length = String::new(); |
| 310 |
while length.trim().parse::<u64>().is_err() { |
| 311 |
length.clear(); |
| 312 |
print!("New minimum PIN length: "); |
| 313 |
io::stdout() |
| 314 |
.lock() |
| 315 |
.flush() |
| 316 |
.expect("Failed to flush stdout!"); |
| 317 |
io::stdin() |
| 318 |
.read_line(&mut length) |
| 319 |
.expect("error: unable to read user input"); |
| 320 |
} |
| 321 |
let new_length = length.trim().parse::<u64>().unwrap(); |
| 322 |
let cmd = SetMinPINLength { |
| 323 |
new_min_pin_length: Some(new_length), |
| 324 |
min_pin_length_rpids: None, |
| 325 |
force_change_pin: None, |
| 326 |
}; |
| 327 |
|
| 328 |
tx.send(InteractiveRequest::ChangeConfig( |
| 329 |
AuthConfigCommand::SetMinPINLength(cmd), |
| 330 |
puat, |
| 331 |
)) |
| 332 |
.expect("Failed to send SetMinPINLength request."); |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
fn handle_credential_management( |
| 338 |
res: Option<CredentialManagementResult>, |
| 339 |
puat: Option<PinUvAuthResult>, |
| 340 |
auth_info: &mut Option<AuthenticatorInfo>, |
| 341 |
tx: &Sender<InteractiveRequest>, |
| 342 |
) { |
| 343 |
match res { |
| 344 |
Some(CredentialManagementResult::CredentialList(credlist)) => { |
| 345 |
let mut creds = vec![]; |
| 346 |
for rp in &credlist.credential_list { |
| 347 |
for cred in &rp.credentials { |
| 348 |
creds.push(( |
| 349 |
rp.rp.name.clone(), |
| 350 |
cred.user.clone(), |
| 351 |
cred.credential_id.clone(), |
| 352 |
)); |
| 353 |
} |
| 354 |
} |
| 355 |
let display_creds: Vec<_> = creds |
| 356 |
.iter() |
| 357 |
.map(|(rp, user, id)| format!("{:?} - {:?} - {:?}", rp, user, id)) |
| 358 |
.collect(); |
| 359 |
for (idx, op) in display_creds.iter().enumerate() { |
| 360 |
println!("({idx}) {op}"); |
| 361 |
} |
| 362 |
|
| 363 |
loop { |
| 364 |
match CredentialsOperation::ask_user(auth_info.as_ref().unwrap(), &credlist) { |
| 365 |
CredentialsOperation::List => { |
| 366 |
let mut idx = 0; |
| 367 |
for rp in credlist.credential_list.iter() { |
| 368 |
for cred in &rp.credentials { |
| 369 |
println!("({idx}) - {:?}: {:?}", rp.rp.name, cred); |
| 370 |
idx += 1; |
| 371 |
} |
| 372 |
} |
| 373 |
continue; |
| 374 |
} |
| 375 |
CredentialsOperation::Delete => { |
| 376 |
let choice = ask_user_choice(&display_creds); |
| 377 |
tx.send(InteractiveRequest::CredentialManagement( |
| 378 |
CredManagementCmd::DeleteCredential(creds[choice].2.clone()), |
| 379 |
puat, |
| 380 |
)) |
| 381 |
.expect("Failed to send DeleteCredentials request."); |
| 382 |
break; |
| 383 |
} |
| 384 |
CredentialsOperation::UpdateUser => { |
| 385 |
let choice = ask_user_choice(&display_creds); |
| 386 |
// Updating username. Asking for the new one. |
| 387 |
let mut input = String::new(); |
| 388 |
print!("New username: "); |
| 389 |
io::stdout() |
| 390 |
.lock() |
| 391 |
.flush() |
| 392 |
.expect("Failed to flush stdout!"); |
| 393 |
io::stdin() |
| 394 |
.read_line(&mut input) |
| 395 |
.expect("error: unable to read user input"); |
| 396 |
input = input.trim().to_string(); |
| 397 |
let name = if input.is_empty() { None } else { Some(input) }; |
| 398 |
let mut new_user = creds[choice].1.clone(); |
| 399 |
new_user.name = name; |
| 400 |
tx.send(InteractiveRequest::CredentialManagement( |
| 401 |
CredManagementCmd::UpdateUserInformation( |
| 402 |
creds[choice].2.clone(), |
| 403 |
new_user, |
| 404 |
), |
| 405 |
puat, |
| 406 |
)) |
| 407 |
.expect("Failed to send UpdateUserinformation request."); |
| 408 |
break; |
| 409 |
} |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
None |
| 414 |
| Some(CredentialManagementResult::DeleteSucess) |
| 415 |
| Some(CredentialManagementResult::UpdateSuccess) => { |
| 416 |
tx.send(InteractiveRequest::CredentialManagement( |
| 417 |
CredManagementCmd::GetCredentials, |
| 418 |
puat, |
| 419 |
)) |
| 420 |
.expect("Failed to send GetCredentials request."); |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
fn ask_user_bio_options( |
| 426 |
biolist: Vec<EnrollmentInfo>, |
| 427 |
puat: Option<PinUvAuthResult>, |
| 428 |
auth_info: &mut Option<AuthenticatorInfo>, |
| 429 |
tx: &Sender<InteractiveRequest>, |
| 430 |
) { |
| 431 |
let display_bios: Vec<_> = biolist |
| 432 |
.iter() |
| 433 |
.map(|x| format!("{:?}: {:?}", x.template_friendly_name, x.template_id)) |
| 434 |
.collect(); |
| 435 |
loop { |
| 436 |
match BioOperation::ask_user(auth_info.as_ref().unwrap()) { |
| 437 |
BioOperation::List => { |
| 438 |
for (idx, bio) in display_bios.iter().enumerate() { |
| 439 |
println!("({idx}) - {bio}"); |
| 440 |
} |
| 441 |
continue; |
| 442 |
} |
| 443 |
BioOperation::ShowInfo => { |
| 444 |
tx.send(InteractiveRequest::BioEnrollment( |
| 445 |
BioEnrollmentCmd::GetFingerprintSensorInfo, |
| 446 |
puat, |
| 447 |
)) |
| 448 |
.expect("Failed to send GetFingerprintSensorInfo request."); |
| 449 |
break; |
| 450 |
} |
| 451 |
BioOperation::Delete => { |
| 452 |
let choice = ask_user_choice(&display_bios); |
| 453 |
tx.send(InteractiveRequest::BioEnrollment( |
| 454 |
BioEnrollmentCmd::DeleteEnrollment(biolist[choice].template_id.clone()), |
| 455 |
puat, |
| 456 |
)) |
| 457 |
.expect("Failed to send GetEnrollments request."); |
| 458 |
break; |
| 459 |
} |
| 460 |
BioOperation::Rename => { |
| 461 |
let choice = ask_user_choice(&display_bios); |
| 462 |
let chosen_id = biolist[choice].template_id.clone(); |
| 463 |
// Updating enrollment name. Asking for the new one. |
| 464 |
let mut input = String::new(); |
| 465 |
print!("New name: "); |
| 466 |
io::stdout() |
| 467 |
.lock() |
| 468 |
.flush() |
| 469 |
.expect("Failed to flush stdout!"); |
| 470 |
io::stdin() |
| 471 |
.read_line(&mut input) |
| 472 |
.expect("error: unable to read user input"); |
| 473 |
let name = input.trim().to_string(); |
| 474 |
tx.send(InteractiveRequest::BioEnrollment( |
| 475 |
BioEnrollmentCmd::ChangeName(chosen_id, name), |
| 476 |
puat, |
| 477 |
)) |
| 478 |
.expect("Failed to send GetEnrollments request."); |
| 479 |
break; |
| 480 |
} |
| 481 |
BioOperation::Add => { |
| 482 |
let mut input = String::new(); |
| 483 |
print!( |
| 484 |
"The name of the new bio enrollment (leave empty if you don't want to name it): " |
| 485 |
); |
| 486 |
io::stdout() |
| 487 |
.lock() |
| 488 |
.flush() |
| 489 |
.expect("Failed to flush stdout!"); |
| 490 |
io::stdin() |
| 491 |
.read_line(&mut input) |
| 492 |
.expect("error: unable to read user input"); |
| 493 |
input = input.trim().to_string(); |
| 494 |
let name = if input.is_empty() { None } else { Some(input) }; |
| 495 |
tx.send(InteractiveRequest::BioEnrollment( |
| 496 |
BioEnrollmentCmd::StartNewEnrollment(name), |
| 497 |
puat, |
| 498 |
)) |
| 499 |
.expect("Failed to send StartNewEnrollment request."); |
| 500 |
break; |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
fn handle_bio_enrollments( |
| 507 |
res: Option<BioEnrollmentResult>, |
| 508 |
puat: Option<PinUvAuthResult>, |
| 509 |
auth_info: &mut Option<AuthenticatorInfo>, |
| 510 |
tx: &Sender<InteractiveRequest>, |
| 511 |
) { |
| 512 |
match res { |
| 513 |
Some(BioEnrollmentResult::EnrollmentList(biolist)) => { |
| 514 |
ask_user_bio_options(biolist, puat, auth_info, tx); |
| 515 |
} |
| 516 |
None => { |
| 517 |
if BioOperation::parse_possible_operations(auth_info.as_ref().unwrap()) |
| 518 |
.contains(&BioOperation::List) |
| 519 |
{ |
| 520 |
tx.send(InteractiveRequest::BioEnrollment( |
| 521 |
BioEnrollmentCmd::GetEnrollments, |
| 522 |
puat, |
| 523 |
)) |
| 524 |
.expect("Failed to send GetEnrollments request."); |
| 525 |
} else { |
| 526 |
ask_user_bio_options(vec![], puat, auth_info, tx); |
| 527 |
} |
| 528 |
} |
| 529 |
Some(BioEnrollmentResult::UpdateSuccess) => { |
| 530 |
tx.send(InteractiveRequest::BioEnrollment( |
| 531 |
BioEnrollmentCmd::GetEnrollments, |
| 532 |
puat, |
| 533 |
)) |
| 534 |
.expect("Failed to send GetEnrollments request."); |
| 535 |
} |
| 536 |
Some(BioEnrollmentResult::DeleteSuccess(info)) => { |
| 537 |
*auth_info = Some(info.clone()); |
| 538 |
if BioOperation::parse_possible_operations(&info).contains(&BioOperation::List) { |
| 539 |
tx.send(InteractiveRequest::BioEnrollment( |
| 540 |
BioEnrollmentCmd::GetEnrollments, |
| 541 |
puat, |
| 542 |
)) |
| 543 |
.expect("Failed to send GetEnrollments request."); |
| 544 |
} else { |
| 545 |
ask_user_bio_options(vec![], puat, auth_info, tx); |
| 546 |
} |
| 547 |
} |
| 548 |
Some(BioEnrollmentResult::AddSuccess(info)) => { |
| 549 |
*auth_info = Some(info); |
| 550 |
tx.send(InteractiveRequest::BioEnrollment( |
| 551 |
BioEnrollmentCmd::GetEnrollments, |
| 552 |
puat, |
| 553 |
)) |
| 554 |
.expect("Failed to send GetEnrollments request."); |
| 555 |
} |
| 556 |
Some(BioEnrollmentResult::FingerprintSensorInfo(info)) => { |
| 557 |
println!("{info:#?}"); |
| 558 |
if BioOperation::parse_possible_operations(auth_info.as_ref().unwrap()) |
| 559 |
.contains(&BioOperation::List) |
| 560 |
{ |
| 561 |
tx.send(InteractiveRequest::BioEnrollment( |
| 562 |
BioEnrollmentCmd::GetEnrollments, |
| 563 |
puat, |
| 564 |
)) |
| 565 |
.expect("Failed to send GetEnrollments request."); |
| 566 |
} else { |
| 567 |
ask_user_bio_options(vec![], puat, auth_info, tx); |
| 568 |
} |
| 569 |
} |
| 570 |
Some(BioEnrollmentResult::SampleStatus(last_sample_status, remaining_samples)) => { |
| 571 |
println!("Last sample status: {last_sample_status:?}, remaining samples: {remaining_samples:?}"); |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
fn interactive_status_callback(status_rx: Receiver<StatusUpdate>) { |
| 577 |
let mut tx = None; |
| 578 |
let mut auth_info = None; |
| 579 |
loop { |
| 580 |
match status_rx.recv() { |
| 581 |
Ok(StatusUpdate::InteractiveManagement(InteractiveUpdate::StartManagement(( |
| 582 |
tx_new, |
| 583 |
auth_info_new, |
| 584 |
)))) => { |
| 585 |
let info = match auth_info_new { |
| 586 |
Some(info) => info, |
| 587 |
None => { |
| 588 |
println!("Device only supports CTAP1 and can't be managed."); |
| 589 |
return; |
| 590 |
} |
| 591 |
}; |
| 592 |
auth_info = Some(info.clone()); |
| 593 |
tx = Some(tx_new); |
| 594 |
match TopLevelOperation::ask_user(&info) { |
| 595 |
TopLevelOperation::Quit => { |
| 596 |
tx.as_ref() |
| 597 |
.unwrap() |
| 598 |
.send(InteractiveRequest::Quit) |
| 599 |
.expect("Failed to send PIN-set request"); |
| 600 |
} |
| 601 |
TopLevelOperation::Reset => tx |
| 602 |
.as_ref() |
| 603 |
.unwrap() |
| 604 |
.send(InteractiveRequest::Reset) |
| 605 |
.expect("Failed to send Reset request."), |
| 606 |
TopLevelOperation::ShowFullInfo => { |
| 607 |
println!("Authenticator Info {:#?}", info); |
| 608 |
return; |
| 609 |
} |
| 610 |
TopLevelOperation::Pin(op) => { |
| 611 |
let raw_new_pin = rpassword::prompt_password_stderr("Enter new PIN: ") |
| 612 |
.expect("Failed to read PIN"); |
| 613 |
let new_pin = Pin::new(&raw_new_pin); |
| 614 |
if op == PinOperation::Change { |
| 615 |
let raw_curr_pin = |
| 616 |
rpassword::prompt_password_stderr("Enter current PIN: ") |
| 617 |
.expect("Failed to read PIN"); |
| 618 |
let curr_pin = Pin::new(&raw_curr_pin); |
| 619 |
tx.as_ref() |
| 620 |
.unwrap() |
| 621 |
.send(InteractiveRequest::ChangePIN(curr_pin, new_pin)) |
| 622 |
.expect("Failed to send PIN-change request"); |
| 623 |
} else { |
| 624 |
tx.as_ref() |
| 625 |
.unwrap() |
| 626 |
.send(InteractiveRequest::SetPIN(new_pin)) |
| 627 |
.expect("Failed to send PIN-set request"); |
| 628 |
} |
| 629 |
} |
| 630 |
TopLevelOperation::Configure => handle_authenticator_config( |
| 631 |
None, |
| 632 |
None, |
| 633 |
&mut auth_info, |
| 634 |
tx.as_ref().unwrap(), |
| 635 |
), |
| 636 |
TopLevelOperation::Credentials => handle_credential_management( |
| 637 |
None, |
| 638 |
None, |
| 639 |
&mut auth_info, |
| 640 |
tx.as_ref().unwrap(), |
| 641 |
), |
| 642 |
TopLevelOperation::Bio => { |
| 643 |
handle_bio_enrollments(None, None, &mut auth_info, tx.as_ref().unwrap()) |
| 644 |
} |
| 645 |
} |
| 646 |
continue; |
| 647 |
} |
| 648 |
Ok(StatusUpdate::InteractiveManagement(InteractiveUpdate::AuthConfigUpdate(( |
| 649 |
cfg_result, |
| 650 |
puat_res, |
| 651 |
)))) => { |
| 652 |
handle_authenticator_config( |
| 653 |
Some(cfg_result), |
| 654 |
puat_res, |
| 655 |
&mut auth_info, |
| 656 |
tx.as_ref().unwrap(), |
| 657 |
); |
| 658 |
continue; |
| 659 |
} |
| 660 |
Ok(StatusUpdate::InteractiveManagement( |
| 661 |
InteractiveUpdate::CredentialManagementUpdate((cfg_result, puat_res)), |
| 662 |
)) => { |
| 663 |
handle_credential_management( |
| 664 |
Some(cfg_result), |
| 665 |
puat_res, |
| 666 |
&mut auth_info, |
| 667 |
tx.as_ref().unwrap(), |
| 668 |
); |
| 669 |
continue; |
| 670 |
} |
| 671 |
Ok(StatusUpdate::InteractiveManagement(InteractiveUpdate::BioEnrollmentUpdate(( |
| 672 |
bio_res, |
| 673 |
puat_res, |
| 674 |
)))) => { |
| 675 |
handle_bio_enrollments( |
| 676 |
Some(bio_res), |
| 677 |
puat_res, |
| 678 |
&mut auth_info, |
| 679 |
tx.as_ref().unwrap(), |
| 680 |
); |
| 681 |
continue; |
| 682 |
} |
| 683 |
Ok(StatusUpdate::SelectDeviceNotice) => { |
| 684 |
println!("STATUS: Please select a device by touching one of them."); |
| 685 |
} |
| 686 |
Ok(StatusUpdate::PinUvError(StatusPinUv::PinRequired(sender))) => { |
| 687 |
let raw_pin = |
| 688 |
rpassword::prompt_password_stderr("Enter PIN: ").expect("Failed to read PIN"); |
| 689 |
sender.send(Pin::new(&raw_pin)).expect("Failed to send PIN"); |
| 690 |
continue; |
| 691 |
} |
| 692 |
Ok(StatusUpdate::PinUvError(StatusPinUv::InvalidPin(sender, attempts))) => { |
| 693 |
println!( |
| 694 |
"Wrong PIN! {}", |
| 695 |
attempts.map_or("Try again.".to_string(), |a| format!( |
| 696 |
"You have {a} attempts left." |
| 697 |
)) |
| 698 |
); |
| 699 |
let raw_pin = |
| 700 |
rpassword::prompt_password_stderr("Enter PIN: ").expect("Failed to read PIN"); |
| 701 |
sender.send(Pin::new(&raw_pin)).expect("Failed to send PIN"); |
| 702 |
continue; |
| 703 |
} |
| 704 |
Ok(StatusUpdate::PinUvError(StatusPinUv::PinAuthBlocked)) => { |
| 705 |
panic!("Too many failed attempts in one row. Your device has been temporarily blocked. Please unplug it and plug in again.") |
| 706 |
} |
| 707 |
Ok(StatusUpdate::PinUvError(StatusPinUv::PinBlocked)) => { |
| 708 |
panic!("Too many failed attempts. Your device has been blocked. Reset it.") |
| 709 |
} |
| 710 |
Ok(StatusUpdate::PinUvError(StatusPinUv::InvalidUv(attempts))) => { |
| 711 |
println!( |
| 712 |
"Wrong UV! {}", |
| 713 |
attempts.map_or("Try again.".to_string(), |a| format!( |
| 714 |
"You have {a} attempts left." |
| 715 |
)) |
| 716 |
); |
| 717 |
continue; |
| 718 |
} |
| 719 |
Ok(StatusUpdate::PinUvError(StatusPinUv::UvBlocked)) => { |
| 720 |
println!("Too many failed UV-attempts."); |
| 721 |
continue; |
| 722 |
} |
| 723 |
Ok(StatusUpdate::PresenceRequired) => { |
| 724 |
println!("Please touch your device!"); |
| 725 |
continue; |
| 726 |
} |
| 727 |
Ok(StatusUpdate::PinUvError(e)) => { |
| 728 |
panic!("Unexpected error: {:?}", e) |
| 729 |
} |
| 730 |
Ok(StatusUpdate::SelectResultNotice(_, _)) => { |
| 731 |
panic!("Unexpected select device notice") |
| 732 |
} |
| 733 |
Err(RecvError) => { |
| 734 |
println!("STATUS: end"); |
| 735 |
return; |
| 736 |
} |
| 737 |
} |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
fn main() { |
| 742 |
env_logger::init(); |
| 743 |
|
| 744 |
let args: Vec<String> = env::args().collect(); |
| 745 |
let program = args[0].clone(); |
| 746 |
|
| 747 |
let mut opts = Options::new(); |
| 748 |
opts.optflag("h", "help", "print this help menu").optopt( |
| 749 |
"t", |
| 750 |
"timeout", |
| 751 |
"timeout in seconds", |
| 752 |
"SEC", |
| 753 |
); |
| 754 |
opts.optflag("h", "help", "print this help menu"); |
| 755 |
let matches = match opts.parse(&args[1..]) { |
| 756 |
Ok(m) => m, |
| 757 |
Err(f) => panic!("{}", f.to_string()), |
| 758 |
}; |
| 759 |
if matches.opt_present("help") { |
| 760 |
print_usage(&program, opts); |
| 761 |
return; |
| 762 |
} |
| 763 |
|
| 764 |
let mut manager = |
| 765 |
AuthenticatorService::new().expect("The auth service should initialize safely"); |
| 766 |
manager.add_u2f_usb_hid_platform_transports(); |
| 767 |
|
| 768 |
let timeout_ms = match matches.opt_get_default::<u64>("timeout", 120) { |
| 769 |
Ok(timeout_s) => { |
| 770 |
println!("Using {}s as the timeout", &timeout_s); |
| 771 |
timeout_s * 1_000 |
| 772 |
} |
| 773 |
Err(e) => { |
| 774 |
println!("{e}"); |
| 775 |
print_usage(&program, opts); |
| 776 |
return; |
| 777 |
} |
| 778 |
}; |
| 779 |
|
| 780 |
let (status_tx, status_rx) = channel::<StatusUpdate>(); |
| 781 |
thread::spawn(move || interactive_status_callback(status_rx)); |
| 782 |
|
| 783 |
let (manage_tx, manage_rx) = channel(); |
| 784 |
let state_callback = |
| 785 |
StateCallback::<Result<ManageResult, AuthenticatorError>>::new(Box::new(move |rv| { |
| 786 |
manage_tx.send(rv).unwrap(); |
| 787 |
})); |
| 788 |
|
| 789 |
match manager.manage(timeout_ms, status_tx, state_callback) { |
| 790 |
Ok(_) => { |
| 791 |
debug!("Started management"); |
| 792 |
} |
| 793 |
Err(e) => { |
| 794 |
println!("Error! Failed to start interactive management: {:?}", e); |
| 795 |
return; |
| 796 |
} |
| 797 |
} |
| 798 |
let manage_result = manage_rx |
| 799 |
.recv() |
| 800 |
.expect("Problem receiving, unable to continue"); |
| 801 |
match manage_result { |
| 802 |
Ok(r) => { |
| 803 |
println!("Success! Result = {r:?}"); |
| 804 |
} |
| 805 |
Err(e) => { |
| 806 |
println!("Error! {:?}", e); |
| 807 |
} |
| 808 |
}; |
| 809 |
println!("Done"); |
| 810 |
} |