| 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 |
use crate::consts::{Capability, HIDCmd, CID_BROADCAST}; |
| 5 |
use crate::crypto::SharedSecret; |
| 6 |
use crate::ctap2::commands::get_info::AuthenticatorInfo; |
| 7 |
use crate::ctap2::commands::{CtapResponse, RequestCtap1, RequestCtap2}; |
| 8 |
use crate::transport::device_selector::DeviceCommand; |
| 9 |
use crate::transport::TestDevice; |
| 10 |
use crate::transport::{hid::HIDDevice, FidoDevice, FidoProtocol, HIDError}; |
| 11 |
use crate::u2ftypes::{U2FDeviceInfo, U2FHIDInitResp}; |
| 12 |
use std::any::Any; |
| 13 |
use std::collections::VecDeque; |
| 14 |
use std::hash::{Hash, Hasher}; |
| 15 |
use std::io::{self, Read, Write}; |
| 16 |
use std::sync::mpsc::{channel, Receiver, Sender}; |
| 17 |
|
| 18 |
pub(crate) const IN_HID_RPT_SIZE: usize = 64; |
| 19 |
const OUT_HID_RPT_SIZE: usize = 64; |
| 20 |
|
| 21 |
#[derive(Debug)] |
| 22 |
pub struct Device { |
| 23 |
pub id: String, |
| 24 |
pub cid: [u8; 4], |
| 25 |
pub reads: Vec<[u8; IN_HID_RPT_SIZE]>, |
| 26 |
pub writes: Vec<[u8; OUT_HID_RPT_SIZE + 1]>, |
| 27 |
pub dev_info: Option<U2FDeviceInfo>, |
| 28 |
pub authenticator_info: Option<AuthenticatorInfo>, |
| 29 |
pub sender: Option<Sender<DeviceCommand>>, |
| 30 |
pub receiver: Option<Receiver<DeviceCommand>>, |
| 31 |
pub protocol: FidoProtocol, |
| 32 |
skip_serialization: bool, |
| 33 |
pub upcoming_requests: VecDeque<Vec<u8>>, |
| 34 |
pub upcoming_responses: VecDeque<Result<Box<dyn Any>, HIDError>>, |
| 35 |
pub shared_secret: Option<SharedSecret>, |
| 36 |
} |
| 37 |
|
| 38 |
impl Device { |
| 39 |
pub fn add_write(&mut self, packet: &[u8], fill_value: u8) { |
| 40 |
// Add one to deal with record index check |
| 41 |
let mut write = [fill_value; OUT_HID_RPT_SIZE + 1]; |
| 42 |
// Make sure we start with a 0, for HID record index |
| 43 |
write[0] = 0; |
| 44 |
// Clone packet data in at 1, since front is padded with HID record index |
| 45 |
write[1..=packet.len()].clone_from_slice(packet); |
| 46 |
self.writes.push(write); |
| 47 |
} |
| 48 |
|
| 49 |
pub fn add_read(&mut self, packet: &[u8], fill_value: u8) { |
| 50 |
let mut read = [fill_value; IN_HID_RPT_SIZE]; |
| 51 |
read[..packet.len()].clone_from_slice(packet); |
| 52 |
self.reads.push(read); |
| 53 |
} |
| 54 |
|
| 55 |
pub fn add_upcoming_ctap2_request(&mut self, msg: &impl RequestCtap2) { |
| 56 |
self.upcoming_requests |
| 57 |
.push_back(msg.wire_format().expect("Failed to serialize CTAP request")); |
| 58 |
} |
| 59 |
|
| 60 |
pub fn add_upcoming_ctap1_request(&mut self, msg: &impl RequestCtap1) { |
| 61 |
let (upcoming, _) = msg |
| 62 |
.ctap1_format() |
| 63 |
.expect("Failed to serialize CTAP request"); |
| 64 |
self.upcoming_requests.push_back(upcoming); |
| 65 |
} |
| 66 |
|
| 67 |
pub fn add_upcoming_ctap_response(&mut self, msg: impl CtapResponse) { |
| 68 |
self.upcoming_responses.push_back(Ok(Box::new(msg))); |
| 69 |
} |
| 70 |
|
| 71 |
pub fn add_upcoming_ctap_error(&mut self, msg: HIDError) { |
| 72 |
self.upcoming_responses.push_back(Err(msg)); |
| 73 |
} |
| 74 |
|
| 75 |
pub fn create_channel(&mut self) { |
| 76 |
let (tx, rx) = channel(); |
| 77 |
self.sender = Some(tx); |
| 78 |
self.receiver = Some(rx); |
| 79 |
} |
| 80 |
|
| 81 |
pub fn new_skipping_serialization(id: &str) -> Result<Self, (HIDError, String)> { |
| 82 |
Ok(Device { |
| 83 |
id: id.to_string(), |
| 84 |
cid: CID_BROADCAST, |
| 85 |
reads: vec![], |
| 86 |
writes: vec![], |
| 87 |
dev_info: None, |
| 88 |
authenticator_info: None, |
| 89 |
sender: None, |
| 90 |
receiver: None, |
| 91 |
protocol: FidoProtocol::CTAP2, |
| 92 |
skip_serialization: true, |
| 93 |
upcoming_requests: VecDeque::new(), |
| 94 |
upcoming_responses: VecDeque::new(), |
| 95 |
shared_secret: None, |
| 96 |
}) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
impl Write for Device { |
| 101 |
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> { |
| 102 |
// Pop a vector from the expected writes, check for quality |
| 103 |
// against bytes array. |
| 104 |
assert!( |
| 105 |
!self.writes.is_empty(), |
| 106 |
"Ran out of expected write values! Wanted to write {:?}", |
| 107 |
bytes |
| 108 |
); |
| 109 |
let check = self.writes.remove(0); |
| 110 |
assert_eq!(check.len(), bytes.len()); |
| 111 |
assert_eq!(&check, bytes); |
| 112 |
Ok(bytes.len()) |
| 113 |
} |
| 114 |
|
| 115 |
// nop |
| 116 |
fn flush(&mut self) -> io::Result<()> { |
| 117 |
Ok(()) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
impl Read for Device { |
| 122 |
fn read(&mut self, bytes: &mut [u8]) -> io::Result<usize> { |
| 123 |
assert!(!self.reads.is_empty(), "Ran out of read values!"); |
| 124 |
let check = self.reads.remove(0); |
| 125 |
assert_eq!(check.len(), bytes.len()); |
| 126 |
bytes.clone_from_slice(&check); |
| 127 |
Ok(check.len()) |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
impl Drop for Device { |
| 132 |
fn drop(&mut self) { |
| 133 |
if !std::thread::panicking() { |
| 134 |
assert!(self.reads.is_empty()); |
| 135 |
assert!(self.writes.is_empty()); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
impl PartialEq for Device { |
| 141 |
fn eq(&self, other: &Device) -> bool { |
| 142 |
self.id == other.id |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
impl Eq for Device {} |
| 147 |
|
| 148 |
impl Hash for Device { |
| 149 |
fn hash<H: Hasher>(&self, state: &mut H) { |
| 150 |
self.id.hash(state); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
impl HIDDevice for Device { |
| 155 |
type Id = String; |
| 156 |
type BuildParameters = &'static str; // None used |
| 157 |
|
| 158 |
fn id(&self) -> Self::Id { |
| 159 |
self.id.clone() |
| 160 |
} |
| 161 |
|
| 162 |
fn new(id: Self::BuildParameters) -> Result<Self, (HIDError, Self::Id)> { |
| 163 |
Ok(Device { |
| 164 |
id: id.to_string(), |
| 165 |
cid: CID_BROADCAST, |
| 166 |
reads: vec![], |
| 167 |
writes: vec![], |
| 168 |
dev_info: None, |
| 169 |
authenticator_info: None, |
| 170 |
sender: None, |
| 171 |
receiver: None, |
| 172 |
protocol: FidoProtocol::CTAP2, |
| 173 |
skip_serialization: false, |
| 174 |
upcoming_requests: VecDeque::new(), |
| 175 |
upcoming_responses: VecDeque::new(), |
| 176 |
shared_secret: None, |
| 177 |
}) |
| 178 |
} |
| 179 |
|
| 180 |
fn get_cid(&self) -> &[u8; 4] { |
| 181 |
&self.cid |
| 182 |
} |
| 183 |
|
| 184 |
fn set_cid(&mut self, cid: [u8; 4]) { |
| 185 |
self.cid = cid; |
| 186 |
} |
| 187 |
|
| 188 |
fn in_rpt_size(&self) -> usize { |
| 189 |
IN_HID_RPT_SIZE |
| 190 |
} |
| 191 |
|
| 192 |
fn out_rpt_size(&self) -> usize { |
| 193 |
OUT_HID_RPT_SIZE |
| 194 |
} |
| 195 |
|
| 196 |
fn get_property(&self, prop_name: &str) -> io::Result<String> { |
| 197 |
Ok(format!("{prop_name} not implemented")) |
| 198 |
} |
| 199 |
|
| 200 |
fn get_device_info(&self) -> U2FDeviceInfo { |
| 201 |
self.dev_info.clone().unwrap() |
| 202 |
} |
| 203 |
|
| 204 |
fn set_device_info(&mut self, dev_info: U2FDeviceInfo) { |
| 205 |
self.dev_info = Some(dev_info); |
| 206 |
} |
| 207 |
|
| 208 |
fn pre_init(&mut self) -> Result<(), HIDError> { |
| 209 |
if self.initialized() { |
| 210 |
return Ok(()); |
| 211 |
} |
| 212 |
|
| 213 |
let nonce = [0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01]; |
| 214 |
|
| 215 |
// Send Init to broadcast address to create a new channel |
| 216 |
self.set_cid(CID_BROADCAST); |
| 217 |
let (cmd, raw) = HIDDevice::sendrecv(self, HIDCmd::Init, &nonce, &|| true)?; |
| 218 |
if cmd != HIDCmd::Init { |
| 219 |
return Err(HIDError::DeviceError); |
| 220 |
} |
| 221 |
|
| 222 |
let rsp = U2FHIDInitResp::read(&raw, &nonce)?; |
| 223 |
|
| 224 |
// Set the new Channel ID |
| 225 |
self.set_cid(rsp.cid); |
| 226 |
|
| 227 |
let info = U2FDeviceInfo { |
| 228 |
vendor_name: "Test vendor".as_bytes().to_vec(), |
| 229 |
device_name: "Test device".as_bytes().to_vec(), |
| 230 |
version_interface: rsp.version_interface, |
| 231 |
version_major: rsp.version_major, |
| 232 |
version_minor: rsp.version_minor, |
| 233 |
version_build: rsp.version_build, |
| 234 |
cap_flags: rsp.cap_flags, |
| 235 |
}; |
| 236 |
debug!("{:?}: {:?}", self.id(), info); |
| 237 |
self.set_device_info(info); |
| 238 |
|
| 239 |
Ok(()) |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
impl TestDevice for Device { |
| 244 |
fn skip_serialization(&self) -> bool { |
| 245 |
self.skip_serialization |
| 246 |
} |
| 247 |
|
| 248 |
fn send_ctap1_unserialized<Req: RequestCtap1>( |
| 249 |
&mut self, |
| 250 |
msg: &Req, |
| 251 |
) -> Result<Req::Output, HIDError> { |
| 252 |
let expected = self |
| 253 |
.upcoming_requests |
| 254 |
.pop_front() |
| 255 |
.expect("No expected CTAP1 command left"); |
| 256 |
let (incoming, _) = msg.ctap1_format().expect("Can't serialize CTAP1 request"); |
| 257 |
assert_eq!(expected, incoming); |
| 258 |
let response = self |
| 259 |
.upcoming_responses |
| 260 |
.pop_front() |
| 261 |
.expect("No response given!"); |
| 262 |
response.map(|x| { |
| 263 |
*x.downcast() |
| 264 |
.expect("Failed to downcast given CTAP response") |
| 265 |
}) |
| 266 |
} |
| 267 |
|
| 268 |
fn send_ctap2_unserialized<Req: RequestCtap2>( |
| 269 |
&mut self, |
| 270 |
msg: &Req, |
| 271 |
) -> Result<Req::Output, HIDError> { |
| 272 |
let expected = self |
| 273 |
.upcoming_requests |
| 274 |
.pop_front() |
| 275 |
.expect("No expected CTAP2 command left"); |
| 276 |
let incoming = msg.wire_format().expect("Can't serialize CTAP2 request"); |
| 277 |
assert_eq!(expected, incoming); |
| 278 |
let response = self |
| 279 |
.upcoming_responses |
| 280 |
.pop_front() |
| 281 |
.expect("No response given!"); |
| 282 |
response.map(|x| { |
| 283 |
*x.downcast() |
| 284 |
.expect("Failed to downcast given CTAP response") |
| 285 |
}) |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
impl FidoDevice for Device { |
| 290 |
fn pre_init(&mut self) -> Result<(), HIDError> { |
| 291 |
HIDDevice::pre_init(self) |
| 292 |
} |
| 293 |
|
| 294 |
fn should_try_ctap2(&self) -> bool { |
| 295 |
HIDDevice::get_device_info(self) |
| 296 |
.cap_flags |
| 297 |
.contains(Capability::CBOR) |
| 298 |
} |
| 299 |
|
| 300 |
fn initialized(&self) -> bool { |
| 301 |
self.get_cid() != &CID_BROADCAST |
| 302 |
} |
| 303 |
|
| 304 |
fn is_u2f(&mut self) -> bool { |
| 305 |
self.sender.is_some() |
| 306 |
} |
| 307 |
|
| 308 |
fn get_shared_secret(&self) -> std::option::Option<&SharedSecret> { |
| 309 |
self.shared_secret.as_ref() |
| 310 |
} |
| 311 |
|
| 312 |
fn set_shared_secret(&mut self, shared_secret: SharedSecret) { |
| 313 |
self.shared_secret = Some(shared_secret); |
| 314 |
} |
| 315 |
|
| 316 |
fn get_authenticator_info(&self) -> Option<&AuthenticatorInfo> { |
| 317 |
self.authenticator_info.as_ref() |
| 318 |
} |
| 319 |
|
| 320 |
fn set_authenticator_info(&mut self, authenticator_info: AuthenticatorInfo) { |
| 321 |
self.authenticator_info = Some(authenticator_info); |
| 322 |
} |
| 323 |
|
| 324 |
fn get_protocol(&self) -> FidoProtocol { |
| 325 |
self.protocol |
| 326 |
} |
| 327 |
|
| 328 |
fn downgrade_to_ctap1(&mut self) { |
| 329 |
self.protocol = FidoProtocol::CTAP1; |
| 330 |
} |
| 331 |
} |