| 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 log; |
| 6 |
|
| 7 |
use crate::consts::{Capability, CID_BROADCAST, MAX_HID_RPT_SIZE}; |
| 8 |
use crate::ctap2::commands::get_info::AuthenticatorInfo; |
| 9 |
use crate::transport::hid::HIDDevice; |
| 10 |
use crate::transport::platform::iokit::*; |
| 11 |
use crate::transport::{FidoDevice, FidoProtocol, HIDError, SharedSecret}; |
| 12 |
use crate::u2ftypes::U2FDeviceInfo; |
| 13 |
use core_foundation::base::*; |
| 14 |
use core_foundation::string::*; |
| 15 |
use std::convert::TryInto; |
| 16 |
use std::fmt; |
| 17 |
use std::hash::{Hash, Hasher}; |
| 18 |
use std::io; |
| 19 |
use std::io::{Read, Write}; |
| 20 |
use std::sync::mpsc::{Receiver, RecvTimeoutError}; |
| 21 |
use std::time::Duration; |
| 22 |
|
| 23 |
const READ_TIMEOUT: u64 = 15; |
| 24 |
|
| 25 |
pub struct Device { |
| 26 |
device_ref: IOHIDDeviceRef, |
| 27 |
cid: [u8; 4], |
| 28 |
report_rx: Option<Receiver<Vec<u8>>>, |
| 29 |
dev_info: Option<U2FDeviceInfo>, |
| 30 |
secret: Option<SharedSecret>, |
| 31 |
authenticator_info: Option<AuthenticatorInfo>, |
| 32 |
protocol: FidoProtocol, |
| 33 |
} |
| 34 |
|
| 35 |
impl Device { |
| 36 |
unsafe fn get_property_macos(&self, prop_name: &str) -> io::Result<String> { |
| 37 |
let prop_ref = IOHIDDeviceGetProperty( |
| 38 |
self.device_ref, |
| 39 |
CFString::new(prop_name).as_concrete_TypeRef(), |
| 40 |
); |
| 41 |
if prop_ref.is_null() { |
| 42 |
return Err(io::Error::new( |
| 43 |
io::ErrorKind::InvalidData, |
| 44 |
format!("IOHIDDeviceGetProperty received nullptr for property {prop_name}"), |
| 45 |
)); |
| 46 |
} |
| 47 |
|
| 48 |
if CFGetTypeID(prop_ref) != CFStringGetTypeID() { |
| 49 |
return Err(io::Error::new( |
| 50 |
io::ErrorKind::InvalidInput, |
| 51 |
format!("IOHIDDeviceGetProperty returned non-string type for property {prop_name}"), |
| 52 |
)); |
| 53 |
} |
| 54 |
|
| 55 |
Ok(CFString::from_void(prop_ref).to_string()) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
impl fmt::Debug for Device { |
| 60 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 61 |
f.debug_struct("Device").field("cid", &self.cid).finish() |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
impl PartialEq for Device { |
| 66 |
fn eq(&self, other_device: &Device) -> bool { |
| 67 |
self.device_ref == other_device.device_ref |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
impl Eq for Device {} |
| 72 |
|
| 73 |
impl Hash for Device { |
| 74 |
fn hash<H: Hasher>(&self, state: &mut H) { |
| 75 |
// The path should be the only identifying member for a device |
| 76 |
// If the path is the same, its the same device |
| 77 |
self.device_ref.hash(state); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
impl Read for Device { |
| 82 |
fn read(&mut self, mut bytes: &mut [u8]) -> io::Result<usize> { |
| 83 |
if let Some(rx) = &self.report_rx { |
| 84 |
let timeout = Duration::from_secs(READ_TIMEOUT); |
| 85 |
let data = match rx.recv_timeout(timeout) { |
| 86 |
Ok(v) => v, |
| 87 |
Err(e) if e == RecvTimeoutError::Timeout => { |
| 88 |
return Err(io::Error::new(io::ErrorKind::TimedOut, e)); |
| 89 |
} |
| 90 |
Err(e) => { |
| 91 |
return Err(io::Error::new(io::ErrorKind::UnexpectedEof, e)); |
| 92 |
} |
| 93 |
}; |
| 94 |
bytes.write(&data) |
| 95 |
} else { |
| 96 |
Err(io::Error::from(io::ErrorKind::Unsupported)) |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
impl Write for Device { |
| 102 |
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> { |
| 103 |
assert_eq!(bytes.len(), self.out_rpt_size() + 1); |
| 104 |
|
| 105 |
let report_id = i64::from(bytes[0]); |
| 106 |
// Skip report number when not using numbered reports. |
| 107 |
let start = if report_id == 0x0 { 1 } else { 0 }; |
| 108 |
let data = &bytes[start..]; |
| 109 |
|
| 110 |
let result = unsafe { |
| 111 |
IOHIDDeviceSetReport( |
| 112 |
self.device_ref, |
| 113 |
kIOHIDReportTypeOutput, |
| 114 |
report_id.try_into().unwrap(), |
| 115 |
data.as_ptr(), |
| 116 |
data.len() as CFIndex, |
| 117 |
) |
| 118 |
}; |
| 119 |
if result != 0 { |
| 120 |
warn!("set_report sending failure = {0:X}", result); |
| 121 |
return Err(io::Error::from_raw_os_error(result)); |
| 122 |
} |
| 123 |
trace!("set_report sending success = {0:X}", result); |
| 124 |
|
| 125 |
Ok(bytes.len()) |
| 126 |
} |
| 127 |
|
| 128 |
// USB HID writes don't buffer, so this will be a nop. |
| 129 |
fn flush(&mut self) -> io::Result<()> { |
| 130 |
Ok(()) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
impl HIDDevice for Device { |
| 135 |
type BuildParameters = (IOHIDDeviceRef, Receiver<Vec<u8>>); |
| 136 |
type Id = IOHIDDeviceRef; |
| 137 |
|
| 138 |
fn new(dev_ids: Self::BuildParameters) -> Result<Self, (HIDError, Self::Id)> { |
| 139 |
let (device_ref, report_rx) = dev_ids; |
| 140 |
Ok(Self { |
| 141 |
device_ref, |
| 142 |
cid: CID_BROADCAST, |
| 143 |
report_rx: Some(report_rx), |
| 144 |
dev_info: None, |
| 145 |
secret: None, |
| 146 |
authenticator_info: None, |
| 147 |
protocol: FidoProtocol::CTAP2, |
| 148 |
}) |
| 149 |
} |
| 150 |
|
| 151 |
fn id(&self) -> Self::Id { |
| 152 |
self.device_ref |
| 153 |
} |
| 154 |
|
| 155 |
fn get_cid(&self) -> &[u8; 4] { |
| 156 |
&self.cid |
| 157 |
} |
| 158 |
|
| 159 |
fn set_cid(&mut self, cid: [u8; 4]) { |
| 160 |
self.cid = cid; |
| 161 |
} |
| 162 |
|
| 163 |
fn in_rpt_size(&self) -> usize { |
| 164 |
MAX_HID_RPT_SIZE |
| 165 |
} |
| 166 |
|
| 167 |
fn out_rpt_size(&self) -> usize { |
| 168 |
MAX_HID_RPT_SIZE |
| 169 |
} |
| 170 |
|
| 171 |
fn get_property(&self, prop_name: &str) -> io::Result<String> { |
| 172 |
unsafe { self.get_property_macos(prop_name) } |
| 173 |
} |
| 174 |
|
| 175 |
fn get_device_info(&self) -> U2FDeviceInfo { |
| 176 |
// unwrap is okay, as dev_info must have already been set, else |
| 177 |
// a programmer error |
| 178 |
self.dev_info.clone().unwrap() |
| 179 |
} |
| 180 |
|
| 181 |
fn set_device_info(&mut self, dev_info: U2FDeviceInfo) { |
| 182 |
self.dev_info = Some(dev_info); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
impl FidoDevice for Device { |
| 187 |
fn pre_init(&mut self) -> Result<(), HIDError> { |
| 188 |
HIDDevice::pre_init(self) |
| 189 |
} |
| 190 |
|
| 191 |
fn should_try_ctap2(&self) -> bool { |
| 192 |
HIDDevice::get_device_info(self) |
| 193 |
.cap_flags |
| 194 |
.contains(Capability::CBOR) |
| 195 |
} |
| 196 |
|
| 197 |
fn initialized(&self) -> bool { |
| 198 |
self.cid != CID_BROADCAST |
| 199 |
} |
| 200 |
fn is_u2f(&mut self) -> bool { |
| 201 |
true |
| 202 |
} |
| 203 |
fn get_shared_secret(&self) -> Option<&SharedSecret> { |
| 204 |
self.secret.as_ref() |
| 205 |
} |
| 206 |
|
| 207 |
fn set_shared_secret(&mut self, secret: SharedSecret) { |
| 208 |
self.secret = Some(secret); |
| 209 |
} |
| 210 |
|
| 211 |
fn get_authenticator_info(&self) -> Option<&AuthenticatorInfo> { |
| 212 |
self.authenticator_info.as_ref() |
| 213 |
} |
| 214 |
|
| 215 |
fn set_authenticator_info(&mut self, authenticator_info: AuthenticatorInfo) { |
| 216 |
self.authenticator_info = Some(authenticator_info); |
| 217 |
} |
| 218 |
|
| 219 |
fn get_protocol(&self) -> FidoProtocol { |
| 220 |
self.protocol |
| 221 |
} |
| 222 |
|
| 223 |
fn downgrade_to_ctap1(&mut self) { |
| 224 |
self.protocol = FidoProtocol::CTAP1; |
| 225 |
} |
| 226 |
} |