| 1 |
use crate::crypto::{PinUvAuthProtocol, PinUvAuthToken, SharedSecret}; |
| 2 |
use crate::ctap2::commands::client_pin::{ |
| 3 |
ClientPIN, ClientPinResponse, GetKeyAgreement, GetPinToken, |
| 4 |
GetPinUvAuthTokenUsingPinWithPermissions, GetPinUvAuthTokenUsingUvWithPermissions, |
| 5 |
PinUvAuthTokenPermission, |
| 6 |
}; |
| 7 |
use crate::ctap2::commands::get_assertion::{GetAssertion, GetAssertionResult}; |
| 8 |
use crate::ctap2::commands::get_info::{AuthenticatorInfo, AuthenticatorVersion, GetInfo}; |
| 9 |
use crate::ctap2::commands::get_version::{GetVersion, U2FInfo}; |
| 10 |
use crate::ctap2::commands::make_credentials::{ |
| 11 |
dummy_make_credentials_cmd, MakeCredentials, MakeCredentialsResult, |
| 12 |
}; |
| 13 |
use crate::ctap2::commands::reset::Reset; |
| 14 |
use crate::ctap2::commands::selection::Selection; |
| 15 |
use crate::ctap2::commands::{CommandError, RequestCtap1, RequestCtap2, StatusCode}; |
| 16 |
use crate::ctap2::preflight::CheckKeyHandle; |
| 17 |
use crate::transport::device_selector::BlinkResult; |
| 18 |
use crate::transport::errors::HIDError; |
| 19 |
|
| 20 |
use crate::Pin; |
| 21 |
use std::convert::TryFrom; |
| 22 |
use std::fmt; |
| 23 |
|
| 24 |
pub mod device_selector; |
| 25 |
pub mod errors; |
| 26 |
pub mod hid; |
| 27 |
|
| 28 |
#[cfg(all( |
| 29 |
any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"), |
| 30 |
not(test) |
| 31 |
))] |
| 32 |
pub mod hidproto; |
| 33 |
|
| 34 |
#[cfg(all(target_os = "linux", not(test)))] |
| 35 |
#[path = "linux/mod.rs"] |
| 36 |
pub mod platform; |
| 37 |
|
| 38 |
#[cfg(all(target_os = "freebsd", not(test)))] |
| 39 |
#[path = "freebsd/mod.rs"] |
| 40 |
pub mod platform; |
| 41 |
|
| 42 |
#[cfg(all(target_os = "netbsd", not(test)))] |
| 43 |
#[path = "netbsd/mod.rs"] |
| 44 |
pub mod platform; |
| 45 |
|
| 46 |
#[cfg(all(target_os = "openbsd", not(test)))] |
| 47 |
#[path = "openbsd/mod.rs"] |
| 48 |
pub mod platform; |
| 49 |
|
| 50 |
#[cfg(all(target_os = "macos", not(test)))] |
| 51 |
#[path = "macos/mod.rs"] |
| 52 |
pub mod platform; |
| 53 |
|
| 54 |
#[cfg(all(target_os = "windows", not(test)))] |
| 55 |
#[path = "windows/mod.rs"] |
| 56 |
pub mod platform; |
| 57 |
|
| 58 |
#[cfg(not(any( |
| 59 |
target_os = "linux", |
| 60 |
target_os = "freebsd", |
| 61 |
target_os = "openbsd", |
| 62 |
target_os = "netbsd", |
| 63 |
target_os = "macos", |
| 64 |
target_os = "windows", |
| 65 |
test |
| 66 |
)))] |
| 67 |
#[path = "stub/mod.rs"] |
| 68 |
pub mod platform; |
| 69 |
|
| 70 |
#[cfg(test)] |
| 71 |
#[path = "mock/mod.rs"] |
| 72 |
pub mod platform; |
| 73 |
|
| 74 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 75 |
pub enum FidoProtocol { |
| 76 |
CTAP1, |
| 77 |
CTAP2, |
| 78 |
} |
| 79 |
|
| 80 |
pub trait FidoDeviceIO { |
| 81 |
fn send_msg<Out, Req: RequestCtap1<Output = Out> + RequestCtap2<Output = Out>>( |
| 82 |
&mut self, |
| 83 |
msg: &Req, |
| 84 |
) -> Result<Out, HIDError> { |
| 85 |
self.send_msg_cancellable(msg, &|| true) |
| 86 |
} |
| 87 |
|
| 88 |
fn send_cbor<Req: RequestCtap2>(&mut self, msg: &Req) -> Result<Req::Output, HIDError> { |
| 89 |
self.send_cbor_cancellable(msg, &|| true) |
| 90 |
} |
| 91 |
|
| 92 |
fn send_ctap1<Req: RequestCtap1>(&mut self, msg: &Req) -> Result<Req::Output, HIDError> { |
| 93 |
self.send_ctap1_cancellable(msg, &|| true) |
| 94 |
} |
| 95 |
|
| 96 |
fn send_msg_cancellable<Out, Req: RequestCtap1<Output = Out> + RequestCtap2<Output = Out>>( |
| 97 |
&mut self, |
| 98 |
msg: &Req, |
| 99 |
keep_alive: &dyn Fn() -> bool, |
| 100 |
) -> Result<Out, HIDError>; |
| 101 |
|
| 102 |
fn send_cbor_cancellable<Req: RequestCtap2>( |
| 103 |
&mut self, |
| 104 |
msg: &Req, |
| 105 |
keep_alive: &dyn Fn() -> bool, |
| 106 |
) -> Result<Req::Output, HIDError>; |
| 107 |
|
| 108 |
fn send_ctap1_cancellable<Req: RequestCtap1>( |
| 109 |
&mut self, |
| 110 |
msg: &Req, |
| 111 |
keep_alive: &dyn Fn() -> bool, |
| 112 |
) -> Result<Req::Output, HIDError>; |
| 113 |
} |
| 114 |
|
| 115 |
pub trait TestDevice { |
| 116 |
#[cfg(test)] |
| 117 |
fn skip_serialization(&self) -> bool; |
| 118 |
#[cfg(test)] |
| 119 |
fn send_ctap1_unserialized<Req: RequestCtap1>( |
| 120 |
&mut self, |
| 121 |
msg: &Req, |
| 122 |
) -> Result<Req::Output, HIDError>; |
| 123 |
#[cfg(test)] |
| 124 |
fn send_ctap2_unserialized<Req: RequestCtap2>( |
| 125 |
&mut self, |
| 126 |
msg: &Req, |
| 127 |
) -> Result<Req::Output, HIDError>; |
| 128 |
} |
| 129 |
|
| 130 |
pub trait FidoDevice: FidoDeviceIO |
| 131 |
where |
| 132 |
Self: Sized, |
| 133 |
Self: fmt::Debug, |
| 134 |
{ |
| 135 |
fn pre_init(&mut self) -> Result<(), HIDError>; |
| 136 |
fn initialized(&self) -> bool; |
| 137 |
|
| 138 |
// Check if the device is actually a token |
| 139 |
fn is_u2f(&mut self) -> bool; |
| 140 |
fn should_try_ctap2(&self) -> bool; |
| 141 |
fn get_authenticator_info(&self) -> Option<&AuthenticatorInfo>; |
| 142 |
fn set_authenticator_info(&mut self, authenticator_info: AuthenticatorInfo); |
| 143 |
fn refresh_authenticator_info(&mut self) -> Option<&AuthenticatorInfo> { |
| 144 |
let command = GetInfo::default(); |
| 145 |
if let Ok(info) = self.send_cbor(&command) { |
| 146 |
debug!("Refreshed authenticator info: {:?}", info); |
| 147 |
self.set_authenticator_info(info); |
| 148 |
} |
| 149 |
self.get_authenticator_info() |
| 150 |
} |
| 151 |
|
| 152 |
// `get_protocol()` indicates whether we're using CTAP1 or CTAP2. |
| 153 |
// Prior to initializing the device, `get_protocol()` should return CTAP2 unless |
| 154 |
// there's a reason to believe that the device does not support CTAP2 (e.g. if |
| 155 |
// it's a HID device and it does not have the CBOR capability). |
| 156 |
fn get_protocol(&self) -> FidoProtocol; |
| 157 |
|
| 158 |
// We do not provide a generic `set_protocol(..)` function as this would have complicated |
| 159 |
// interactions with the AuthenticatorInfo state. |
| 160 |
fn downgrade_to_ctap1(&mut self); |
| 161 |
|
| 162 |
fn get_shared_secret(&self) -> Option<&SharedSecret>; |
| 163 |
fn set_shared_secret(&mut self, secret: SharedSecret); |
| 164 |
|
| 165 |
fn init(&mut self) -> Result<(), HIDError> { |
| 166 |
self.pre_init()?; |
| 167 |
|
| 168 |
if self.should_try_ctap2() { |
| 169 |
let command = GetInfo::default(); |
| 170 |
if let Ok(info) = self.send_cbor(&command) { |
| 171 |
debug!("{:?}", info); |
| 172 |
if info.max_supported_version() == AuthenticatorVersion::U2F_V2 { |
| 173 |
self.downgrade_to_ctap1(); |
| 174 |
} |
| 175 |
self.set_authenticator_info(info); |
| 176 |
return Ok(()); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
self.downgrade_to_ctap1(); |
| 181 |
// We want to return an error here if this device doesn't support CTAP1, |
| 182 |
// so we send a U2F_VERSION command. |
| 183 |
let command = GetVersion::default(); |
| 184 |
self.send_ctap1(&command)?; |
| 185 |
Ok(()) |
| 186 |
} |
| 187 |
|
| 188 |
fn block_and_blink(&mut self, keep_alive: &dyn Fn() -> bool) -> BlinkResult { |
| 189 |
let supports_select_cmd = self.get_protocol() == FidoProtocol::CTAP2 |
| 190 |
&& self.get_authenticator_info().map_or(false, |i| { |
| 191 |
i.versions.contains(&AuthenticatorVersion::FIDO_2_1) |
| 192 |
}); |
| 193 |
let resp = if supports_select_cmd { |
| 194 |
let msg = Selection {}; |
| 195 |
self.send_cbor_cancellable(&msg, keep_alive) |
| 196 |
} else { |
| 197 |
// We need to fake a blink-request, because FIDO2.0 forgot to specify one |
| 198 |
// See: https://fidoalliance.org/specs/fido-v2.0-ps-20190130/fido-client-to-authenticator-protocol-v2.0-ps-20190130.html#using-pinToken-in-authenticatorMakeCredential |
| 199 |
let msg = dummy_make_credentials_cmd(); |
| 200 |
info!("Trying to blink: {:?}", &msg); |
| 201 |
// We don't care about the Ok-value, just if it is Ok or not |
| 202 |
self.send_msg_cancellable(&msg, keep_alive).map(|_| ()) |
| 203 |
}; |
| 204 |
|
| 205 |
match resp { |
| 206 |
// Spec only says PinInvalid or PinNotSet should be returned on the fake touch-request, |
| 207 |
// but Yubikeys for example return PinAuthInvalid. A successful return is also possible |
| 208 |
// for CTAP1-tokens so we catch those here as well. |
| 209 |
Ok(_) |
| 210 |
| Err(HIDError::Command(CommandError::StatusCode(StatusCode::PinInvalid, _))) |
| 211 |
| Err(HIDError::Command(CommandError::StatusCode(StatusCode::PinAuthInvalid, _))) |
| 212 |
| Err(HIDError::Command(CommandError::StatusCode(StatusCode::PinNotSet, _))) => { |
| 213 |
BlinkResult::DeviceSelected |
| 214 |
} |
| 215 |
// We cancelled the receive, because another device was selected. |
| 216 |
Err(HIDError::Command(CommandError::StatusCode(StatusCode::KeepaliveCancel, _))) |
| 217 |
| Err(HIDError::Command(CommandError::StatusCode(StatusCode::OperationDenied, _))) |
| 218 |
| Err(HIDError::Command(CommandError::StatusCode(StatusCode::UserActionTimeout, _))) => { |
| 219 |
// TODO: Repeat the request, if it is a UserActionTimeout? |
| 220 |
debug!("Device {:?} got cancelled", &self); |
| 221 |
BlinkResult::Cancelled |
| 222 |
} |
| 223 |
// Something unexpected happened, so we assume this device is not usable and |
| 224 |
// interpreting this equivalent to being cancelled. |
| 225 |
e => { |
| 226 |
info!("Device {:?} received unexpected answer, so we assume an error occurred and we are NOT using this device (assuming the request was cancelled): {:?}", &self, e); |
| 227 |
BlinkResult::Cancelled |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
fn establish_shared_secret( |
| 233 |
&mut self, |
| 234 |
alive: &dyn Fn() -> bool, |
| 235 |
) -> Result<SharedSecret, HIDError> { |
| 236 |
// CTAP1 devices don't support establishing a shared secret |
| 237 |
let info = match (self.get_protocol(), self.get_authenticator_info()) { |
| 238 |
(FidoProtocol::CTAP2, Some(info)) => info, |
| 239 |
_ => return Err(HIDError::UnsupportedCommand), |
| 240 |
}; |
| 241 |
|
| 242 |
let pin_protocol = PinUvAuthProtocol::try_from(info)?; |
| 243 |
|
| 244 |
// Not reusing the shared secret here, if it exists, since we might start again |
| 245 |
// with a different PIN (e.g. if the last one was wrong) |
| 246 |
let pin_command = GetKeyAgreement::new(pin_protocol.clone()); |
| 247 |
let resp = self.send_cbor_cancellable(&pin_command, alive)?; |
| 248 |
if let Some(device_key_agreement_key) = resp.key_agreement { |
| 249 |
let shared_secret = pin_protocol |
| 250 |
.encapsulate(&device_key_agreement_key) |
| 251 |
.map_err(CommandError::from)?; |
| 252 |
self.set_shared_secret(shared_secret.clone()); |
| 253 |
Ok(shared_secret) |
| 254 |
} else { |
| 255 |
Err(HIDError::Command(CommandError::MissingRequiredField( |
| 256 |
"key_agreement", |
| 257 |
))) |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
/// CTAP 2.0-only version: |
| 262 |
/// "Getting pinUvAuthToken using getPinToken (superseded)" |
| 263 |
fn get_pin_token( |
| 264 |
&mut self, |
| 265 |
pin: &Option<Pin>, |
| 266 |
alive: &dyn Fn() -> bool, |
| 267 |
) -> Result<PinUvAuthToken, HIDError> { |
| 268 |
// Asking the user for PIN before establishing the shared secret |
| 269 |
let pin = pin |
| 270 |
.as_ref() |
| 271 |
.ok_or(CommandError::StatusCode(StatusCode::PinRequired, None))?; |
| 272 |
|
| 273 |
// Not reusing the shared secret here, if it exists, since we might start again |
| 274 |
// with a different PIN (e.g. if the last one was wrong) |
| 275 |
let shared_secret = self.establish_shared_secret(alive)?; |
| 276 |
|
| 277 |
let pin_command = GetPinToken::new(&shared_secret, pin); |
| 278 |
let resp = self.send_cbor_cancellable(&pin_command, alive)?; |
| 279 |
if let Some(encrypted_pin_token) = resp.pin_token { |
| 280 |
// CTAP 2.1 spec: |
| 281 |
// If authenticatorClientPIN's getPinToken subcommand is invoked, default permissions |
| 282 |
// of `mc` and `ga` (value 0x03) are granted for the returned pinUvAuthToken. |
| 283 |
let default_permissions = PinUvAuthTokenPermission::default(); |
| 284 |
let pin_token = shared_secret |
| 285 |
.decrypt_pin_token(default_permissions, encrypted_pin_token.as_ref()) |
| 286 |
.map_err(CommandError::from)?; |
| 287 |
Ok(pin_token) |
| 288 |
} else { |
| 289 |
Err(HIDError::Command(CommandError::MissingRequiredField( |
| 290 |
"pin_token", |
| 291 |
))) |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
fn get_pin_uv_auth_token_using_uv_with_permissions( |
| 296 |
&mut self, |
| 297 |
permission: PinUvAuthTokenPermission, |
| 298 |
rp_id: Option<&String>, |
| 299 |
alive: &dyn Fn() -> bool, |
| 300 |
) -> Result<PinUvAuthToken, HIDError> { |
| 301 |
// Explicitly not reusing the shared secret here |
| 302 |
let shared_secret = self.establish_shared_secret(alive)?; |
| 303 |
let pin_command = GetPinUvAuthTokenUsingUvWithPermissions::new( |
| 304 |
&shared_secret, |
| 305 |
permission, |
| 306 |
rp_id.cloned(), |
| 307 |
); |
| 308 |
|
| 309 |
let resp = self.send_cbor_cancellable(&pin_command, alive)?; |
| 310 |
|
| 311 |
if let Some(encrypted_pin_token) = resp.pin_token { |
| 312 |
let pin_token = shared_secret |
| 313 |
.decrypt_pin_token(permission, encrypted_pin_token.as_ref()) |
| 314 |
.map_err(CommandError::from)?; |
| 315 |
Ok(pin_token) |
| 316 |
} else { |
| 317 |
Err(HIDError::Command(CommandError::MissingRequiredField( |
| 318 |
"pin_token", |
| 319 |
))) |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
fn get_pin_uv_auth_token_using_pin_with_permissions( |
| 324 |
&mut self, |
| 325 |
pin: &Option<Pin>, |
| 326 |
permission: PinUvAuthTokenPermission, |
| 327 |
rp_id: Option<&String>, |
| 328 |
alive: &dyn Fn() -> bool, |
| 329 |
) -> Result<PinUvAuthToken, HIDError> { |
| 330 |
// Asking the user for PIN before establishing the shared secret |
| 331 |
let pin = pin |
| 332 |
.as_ref() |
| 333 |
.ok_or(CommandError::StatusCode(StatusCode::PinRequired, None))?; |
| 334 |
|
| 335 |
// Not reusing the shared secret here, if it exists, since we might start again |
| 336 |
// with a different PIN (e.g. if the last one was wrong) |
| 337 |
let shared_secret = self.establish_shared_secret(alive)?; |
| 338 |
let pin_command = GetPinUvAuthTokenUsingPinWithPermissions::new( |
| 339 |
&shared_secret, |
| 340 |
pin, |
| 341 |
permission, |
| 342 |
rp_id.cloned(), |
| 343 |
); |
| 344 |
|
| 345 |
let resp = self.send_cbor_cancellable(&pin_command, alive)?; |
| 346 |
|
| 347 |
if let Some(encrypted_pin_token) = resp.pin_token { |
| 348 |
let pin_token = shared_secret |
| 349 |
.decrypt_pin_token(permission, encrypted_pin_token.as_ref()) |
| 350 |
.map_err(CommandError::from)?; |
| 351 |
Ok(pin_token) |
| 352 |
} else { |
| 353 |
Err(HIDError::Command(CommandError::MissingRequiredField( |
| 354 |
"pin_token", |
| 355 |
))) |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
pub trait VirtualFidoDevice: FidoDevice { |
| 361 |
fn check_key_handle(&self, req: &CheckKeyHandle) -> Result<(), HIDError>; |
| 362 |
fn client_pin(&self, req: &ClientPIN) -> Result<ClientPinResponse, HIDError>; |
| 363 |
fn get_assertion(&self, req: &GetAssertion) -> Result<Vec<GetAssertionResult>, HIDError>; |
| 364 |
fn get_info(&self) -> Result<AuthenticatorInfo, HIDError>; |
| 365 |
fn get_version(&self, req: &GetVersion) -> Result<U2FInfo, HIDError>; |
| 366 |
fn make_credentials(&self, req: &MakeCredentials) -> Result<MakeCredentialsResult, HIDError>; |
| 367 |
fn reset(&self, req: &Reset) -> Result<(), HIDError>; |
| 368 |
fn selection(&self, req: &Selection) -> Result<(), HIDError>; |
| 369 |
} |