| 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}; |
| 7 |
use crate::ctap2::commands::get_info::AuthenticatorInfo; |
| 8 |
use crate::transport::hid::HIDDevice; |
| 9 |
use crate::transport::platform::{hidraw, monitor}; |
| 10 |
use crate::transport::{FidoDevice, FidoProtocol, HIDError, SharedSecret}; |
| 11 |
use crate::u2ftypes::U2FDeviceInfo; |
| 12 |
use crate::util::from_unix_result; |
| 13 |
use std::fs::OpenOptions; |
| 14 |
use std::hash::{Hash, Hasher}; |
| 15 |
use std::io; |
| 16 |
use std::io::{Read, Write}; |
| 17 |
use std::os::unix::io::AsRawFd; |
| 18 |
use std::path::PathBuf; |
| 19 |
|
| 20 |
#[derive(Debug)] |
| 21 |
pub struct Device { |
| 22 |
path: PathBuf, |
| 23 |
fd: std::fs::File, |
| 24 |
in_rpt_size: usize, |
| 25 |
out_rpt_size: usize, |
| 26 |
cid: [u8; 4], |
| 27 |
dev_info: Option<U2FDeviceInfo>, |
| 28 |
secret: Option<SharedSecret>, |
| 29 |
authenticator_info: Option<AuthenticatorInfo>, |
| 30 |
protocol: FidoProtocol, |
| 31 |
} |
| 32 |
|
| 33 |
impl PartialEq for Device { |
| 34 |
fn eq(&self, other: &Device) -> bool { |
| 35 |
// The path should be the only identifying member for a device |
| 36 |
// If the path is the same, its the same device |
| 37 |
self.path == other.path |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
impl Eq for Device {} |
| 42 |
|
| 43 |
impl Hash for Device { |
| 44 |
fn hash<H: Hasher>(&self, state: &mut H) { |
| 45 |
// The path should be the only identifying member for a device |
| 46 |
// If the path is the same, its the same device |
| 47 |
self.path.hash(state); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
impl Read for Device { |
| 52 |
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
| 53 |
let bufp = buf.as_mut_ptr() as *mut libc::c_void; |
| 54 |
let rv = unsafe { libc::read(self.fd.as_raw_fd(), bufp, buf.len()) }; |
| 55 |
from_unix_result(rv as usize) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
impl Write for Device { |
| 60 |
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 61 |
let bufp = buf.as_ptr() as *const libc::c_void; |
| 62 |
let rv = unsafe { libc::write(self.fd.as_raw_fd(), bufp, buf.len()) }; |
| 63 |
from_unix_result(rv as usize) |
| 64 |
} |
| 65 |
|
| 66 |
// USB HID writes don't buffer, so this will be a nop. |
| 67 |
fn flush(&mut self) -> io::Result<()> { |
| 68 |
Ok(()) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
impl HIDDevice for Device { |
| 73 |
type BuildParameters = PathBuf; |
| 74 |
type Id = PathBuf; |
| 75 |
|
| 76 |
fn new(path: PathBuf) -> Result<Self, (HIDError, Self::Id)> { |
| 77 |
debug!("Opening device {:?}", path); |
| 78 |
let fd = OpenOptions::new() |
| 79 |
.read(true) |
| 80 |
.write(true) |
| 81 |
.open(&path) |
| 82 |
.map_err(|e| (HIDError::IO(Some(path.clone()), e), path.clone()))?; |
| 83 |
let (in_rpt_size, out_rpt_size) = hidraw::read_hid_rpt_sizes_or_defaults(fd.as_raw_fd()); |
| 84 |
let mut res = Self { |
| 85 |
path, |
| 86 |
fd, |
| 87 |
in_rpt_size, |
| 88 |
out_rpt_size, |
| 89 |
cid: CID_BROADCAST, |
| 90 |
dev_info: None, |
| 91 |
secret: None, |
| 92 |
authenticator_info: None, |
| 93 |
protocol: FidoProtocol::CTAP2, |
| 94 |
}; |
| 95 |
if res.is_u2f() { |
| 96 |
info!("new device {:?}", res.path); |
| 97 |
Ok(res) |
| 98 |
} else { |
| 99 |
Err((HIDError::DeviceNotSupported, res.path)) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
fn id(&self) -> Self::Id { |
| 104 |
self.path.clone() |
| 105 |
} |
| 106 |
|
| 107 |
fn get_cid(&self) -> &[u8; 4] { |
| 108 |
&self.cid |
| 109 |
} |
| 110 |
|
| 111 |
fn set_cid(&mut self, cid: [u8; 4]) { |
| 112 |
self.cid = cid; |
| 113 |
} |
| 114 |
|
| 115 |
fn in_rpt_size(&self) -> usize { |
| 116 |
self.in_rpt_size |
| 117 |
} |
| 118 |
|
| 119 |
fn out_rpt_size(&self) -> usize { |
| 120 |
self.out_rpt_size |
| 121 |
} |
| 122 |
|
| 123 |
fn get_property(&self, prop_name: &str) -> io::Result<String> { |
| 124 |
monitor::get_property_linux(&self.path, prop_name) |
| 125 |
} |
| 126 |
|
| 127 |
fn get_device_info(&self) -> U2FDeviceInfo { |
| 128 |
// unwrap is okay, as dev_info must have already been set, else |
| 129 |
// a programmer error |
| 130 |
self.dev_info.clone().unwrap() |
| 131 |
} |
| 132 |
|
| 133 |
fn set_device_info(&mut self, dev_info: U2FDeviceInfo) { |
| 134 |
self.dev_info = Some(dev_info); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
impl FidoDevice for Device { |
| 139 |
fn pre_init(&mut self) -> Result<(), HIDError> { |
| 140 |
HIDDevice::pre_init(self) |
| 141 |
} |
| 142 |
|
| 143 |
fn should_try_ctap2(&self) -> bool { |
| 144 |
HIDDevice::get_device_info(self) |
| 145 |
.cap_flags |
| 146 |
.contains(Capability::CBOR) |
| 147 |
} |
| 148 |
|
| 149 |
fn initialized(&self) -> bool { |
| 150 |
// During successful init, the broadcast channel id gets replaced by an actual one |
| 151 |
self.cid != CID_BROADCAST |
| 152 |
} |
| 153 |
|
| 154 |
fn is_u2f(&mut self) -> bool { |
| 155 |
hidraw::is_u2f_device(self.fd.as_raw_fd()) |
| 156 |
} |
| 157 |
|
| 158 |
fn get_shared_secret(&self) -> Option<&SharedSecret> { |
| 159 |
self.secret.as_ref() |
| 160 |
} |
| 161 |
|
| 162 |
fn set_shared_secret(&mut self, secret: SharedSecret) { |
| 163 |
self.secret = Some(secret); |
| 164 |
} |
| 165 |
|
| 166 |
fn get_authenticator_info(&self) -> Option<&AuthenticatorInfo> { |
| 167 |
self.authenticator_info.as_ref() |
| 168 |
} |
| 169 |
|
| 170 |
fn set_authenticator_info(&mut self, authenticator_info: AuthenticatorInfo) { |
| 171 |
self.authenticator_info = Some(authenticator_info); |
| 172 |
} |
| 173 |
|
| 174 |
fn get_protocol(&self) -> FidoProtocol { |
| 175 |
self.protocol |
| 176 |
} |
| 177 |
|
| 178 |
fn downgrade_to_ctap1(&mut self) { |
| 179 |
self.protocol = FidoProtocol::CTAP1; |
| 180 |
} |
| 181 |
} |