| 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 |
extern crate libc; |
| 6 |
use crate::consts::{Capability, CID_BROADCAST, MAX_HID_RPT_SIZE}; |
| 7 |
use crate::ctap2::commands::get_info::AuthenticatorInfo; |
| 8 |
use crate::transport::hid::HIDDevice; |
| 9 |
use crate::transport::platform::monitor::WrappedOpenDevice; |
| 10 |
use crate::transport::{FidoDevice, FidoProtocol, HIDError, SharedSecret}; |
| 11 |
use crate::u2ftypes::U2FDeviceInfo; |
| 12 |
use crate::util::{from_unix_result, io_err}; |
| 13 |
use std::ffi::{CString, OsString}; |
| 14 |
use std::hash::{Hash, Hasher}; |
| 15 |
use std::io::{self, Read, Write}; |
| 16 |
use std::mem; |
| 17 |
use std::os::unix::ffi::OsStrExt; |
| 18 |
|
| 19 |
#[derive(Debug)] |
| 20 |
pub struct Device { |
| 21 |
path: OsString, |
| 22 |
fd: libc::c_int, |
| 23 |
in_rpt_size: usize, |
| 24 |
out_rpt_size: usize, |
| 25 |
cid: [u8; 4], |
| 26 |
dev_info: Option<U2FDeviceInfo>, |
| 27 |
secret: Option<SharedSecret>, |
| 28 |
authenticator_info: Option<AuthenticatorInfo>, |
| 29 |
protocol: FidoProtocol, |
| 30 |
} |
| 31 |
|
| 32 |
impl Device { |
| 33 |
fn ping(&mut self) -> io::Result<()> { |
| 34 |
let capacity = 256; |
| 35 |
|
| 36 |
for _ in 0..10 { |
| 37 |
let mut data = vec![0u8; capacity]; |
| 38 |
|
| 39 |
// Send 1 byte ping |
| 40 |
// self.write_all requires Device to be mut. This can't be done at the moment, |
| 41 |
// and this is a workaround anyways, so writing by hand instead. |
| 42 |
self.write_all(&[0, 0xff, 0xff, 0xff, 0xff, 0x81, 0, 1])?; |
| 43 |
|
| 44 |
// Wait for response |
| 45 |
let mut pfd: libc::pollfd = unsafe { mem::zeroed() }; |
| 46 |
pfd.fd = self.fd; |
| 47 |
pfd.events = libc::POLLIN; |
| 48 |
if from_unix_result(unsafe { libc::poll(&mut pfd, 1, 100) })? == 0 { |
| 49 |
debug!("device {:?} timeout", self.path); |
| 50 |
continue; |
| 51 |
} |
| 52 |
|
| 53 |
// Read response |
| 54 |
self.read(&mut data[..])?; |
| 55 |
|
| 56 |
return Ok(()); |
| 57 |
} |
| 58 |
|
| 59 |
Err(io_err("no response from device")) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
impl Drop for Device { |
| 64 |
fn drop(&mut self) { |
| 65 |
// Close the fd, ignore any errors. |
| 66 |
let _ = unsafe { libc::close(self.fd) }; |
| 67 |
debug!("device {:?} closed", self.path); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
impl PartialEq for Device { |
| 72 |
fn eq(&self, other: &Device) -> bool { |
| 73 |
self.path == other.path |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
impl Eq for Device {} |
| 78 |
|
| 79 |
impl Hash for Device { |
| 80 |
fn hash<H: Hasher>(&self, state: &mut H) { |
| 81 |
// The path should be the only identifying member for a device |
| 82 |
// If the path is the same, its the same device |
| 83 |
self.path.hash(state); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
impl Read for Device { |
| 88 |
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
| 89 |
let buf_ptr = buf.as_mut_ptr() as *mut libc::c_void; |
| 90 |
let rv = unsafe { libc::read(self.fd, buf_ptr, buf.len()) }; |
| 91 |
from_unix_result(rv as usize) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
impl Write for Device { |
| 96 |
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 97 |
// Always skip the first byte (report number) |
| 98 |
let data = &buf[1..]; |
| 99 |
let data_ptr = data.as_ptr() as *const libc::c_void; |
| 100 |
let rv = unsafe { libc::write(self.fd, data_ptr, data.len()) }; |
| 101 |
Ok(from_unix_result(rv as usize)? + 1) |
| 102 |
} |
| 103 |
|
| 104 |
fn flush(&mut self) -> io::Result<()> { |
| 105 |
Ok(()) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
impl HIDDevice for Device { |
| 110 |
type BuildParameters = WrappedOpenDevice; |
| 111 |
type Id = OsString; |
| 112 |
|
| 113 |
fn new(fido: WrappedOpenDevice) -> Result<Self, (HIDError, Self::Id)> { |
| 114 |
debug!("device found: {:?}", fido); |
| 115 |
let mut res = Self { |
| 116 |
path: fido.os_path, |
| 117 |
fd: fido.fd, |
| 118 |
in_rpt_size: MAX_HID_RPT_SIZE, |
| 119 |
out_rpt_size: MAX_HID_RPT_SIZE, |
| 120 |
cid: CID_BROADCAST, |
| 121 |
dev_info: None, |
| 122 |
secret: None, |
| 123 |
authenticator_info: None, |
| 124 |
protocol: FidoProtocol::CTAP2, |
| 125 |
}; |
| 126 |
if res.is_u2f() { |
| 127 |
info!("new device {:?}", res.path); |
| 128 |
Ok(res) |
| 129 |
} else { |
| 130 |
Err((HIDError::DeviceNotSupported, res.path.clone())) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
fn id(&self) -> Self::Id { |
| 135 |
self.path.clone() |
| 136 |
} |
| 137 |
|
| 138 |
fn get_cid(&self) -> &[u8; 4] { |
| 139 |
&self.cid |
| 140 |
} |
| 141 |
|
| 142 |
fn set_cid(&mut self, cid: [u8; 4]) { |
| 143 |
self.cid = cid; |
| 144 |
} |
| 145 |
|
| 146 |
fn in_rpt_size(&self) -> usize { |
| 147 |
self.in_rpt_size |
| 148 |
} |
| 149 |
|
| 150 |
fn out_rpt_size(&self) -> usize { |
| 151 |
self.out_rpt_size |
| 152 |
} |
| 153 |
|
| 154 |
fn get_property(&self, _prop_name: &str) -> io::Result<String> { |
| 155 |
Err(io::Error::new(io::ErrorKind::Other, "Not implemented")) |
| 156 |
} |
| 157 |
|
| 158 |
fn get_device_info(&self) -> U2FDeviceInfo { |
| 159 |
// unwrap is okay, as dev_info must have already been set, else |
| 160 |
// a programmer error |
| 161 |
self.dev_info.clone().unwrap() |
| 162 |
} |
| 163 |
|
| 164 |
fn set_device_info(&mut self, dev_info: U2FDeviceInfo) { |
| 165 |
self.dev_info = Some(dev_info); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
impl FidoDevice for Device { |
| 170 |
fn pre_init(&mut self) -> Result<(), HIDError> { |
| 171 |
HIDDevice::pre_init(self) |
| 172 |
} |
| 173 |
|
| 174 |
fn should_try_ctap2(&self) -> bool { |
| 175 |
HIDDevice::get_device_info(self) |
| 176 |
.cap_flags |
| 177 |
.contains(Capability::CBOR) |
| 178 |
} |
| 179 |
|
| 180 |
fn initialized(&self) -> bool { |
| 181 |
// During successful init, the broadcast channel id gets repplaced by an actual one |
| 182 |
self.cid != CID_BROADCAST |
| 183 |
} |
| 184 |
|
| 185 |
fn is_u2f(&mut self) -> bool { |
| 186 |
debug!("device {:?} is U2F/FIDO", self.path); |
| 187 |
|
| 188 |
// From OpenBSD's libfido2 in 6.6-current: |
| 189 |
// "OpenBSD (as of 201910) has a bug that causes it to lose |
| 190 |
// track of the DATA0/DATA1 sequence toggle across uhid device |
| 191 |
// open and close. This is a terrible hack to work around it." |
| 192 |
match self.ping() { |
| 193 |
Ok(_) => true, |
| 194 |
Err(err) => { |
| 195 |
debug!("device {:?} is not responding: {}", self.path, err); |
| 196 |
false |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
fn get_shared_secret(&self) -> Option<&SharedSecret> { |
| 202 |
self.secret.as_ref() |
| 203 |
} |
| 204 |
|
| 205 |
fn set_shared_secret(&mut self, secret: SharedSecret) { |
| 206 |
self.secret = Some(secret); |
| 207 |
} |
| 208 |
|
| 209 |
fn get_authenticator_info(&self) -> Option<&AuthenticatorInfo> { |
| 210 |
self.authenticator_info.as_ref() |
| 211 |
} |
| 212 |
|
| 213 |
fn set_authenticator_info(&mut self, authenticator_info: AuthenticatorInfo) { |
| 214 |
self.authenticator_info = Some(authenticator_info); |
| 215 |
} |
| 216 |
|
| 217 |
fn get_protocol(&self) -> FidoProtocol { |
| 218 |
self.protocol |
| 219 |
} |
| 220 |
|
| 221 |
fn downgrade_to_ctap1(&mut self) { |
| 222 |
self.protocol = FidoProtocol::CTAP1; |
| 223 |
} |
| 224 |
} |