Debian Codegraph @

third_party/rust/authenticator/src/transport/windows/device.rs

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