|
| 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 | + |
| 7 | +use std::mem; |
| 8 | +use std::io::Read; |
| 9 | +use std::io::Write; |
| 10 | +use std::io; |
| 11 | + |
| 12 | +use consts::CID_BROADCAST; |
| 13 | +use consts::HID_RPT_SIZE; |
| 14 | +use platform::fd::Fd; |
| 15 | +use platform::uhid; |
| 16 | +use u2ftypes::U2FDevice; |
| 17 | +use util::io_err; |
| 18 | + |
| 19 | +#[derive(Debug)] |
| 20 | +pub struct Device { |
| 21 | + fd: Fd, |
| 22 | + cid: [u8; 4], |
| 23 | +} |
| 24 | + |
| 25 | +impl Device { |
| 26 | + pub fn new(fd: Fd) -> io::Result<Self> { |
| 27 | + Ok(Self { fd, cid: CID_BROADCAST }) |
| 28 | + } |
| 29 | + |
| 30 | + pub fn is_u2f(&mut self) -> bool { |
| 31 | + if !uhid::is_u2f_device(&self.fd) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + // This step is not strictly necessary -- NetBSD puts fido |
| 35 | + // devices into raw mode automatically by default, but in |
| 36 | + // principle that might change, and this serves as a test to |
| 37 | + // verify that we're running on a kernel with support for raw |
| 38 | + // mode at all so we don't get confused issuing writes that try |
| 39 | + // to set the report descriptor rather than transfer data on |
| 40 | + // the output interrupt pipe as we need. |
| 41 | + match uhid::hid_set_raw(&self.fd, true) { |
| 42 | + Ok(_) => (), |
| 43 | + Err(_) => return false, |
| 44 | + } |
| 45 | + if let Err(_) = self.ping() { |
| 46 | + return false; |
| 47 | + } |
| 48 | + true |
| 49 | + } |
| 50 | + |
| 51 | + fn ping(&mut self) -> io::Result<()> { |
| 52 | + for i in 0..10 { |
| 53 | + let mut buf = vec![0u8; 1 + HID_RPT_SIZE]; |
| 54 | + |
| 55 | + buf[0] = 0; // report number |
| 56 | + buf[1] = 0xff; // CID_BROADCAST |
| 57 | + buf[2] = 0xff; |
| 58 | + buf[3] = 0xff; |
| 59 | + buf[4] = 0xff; |
| 60 | + buf[5] = 0x81; // ping |
| 61 | + buf[6] = 0; |
| 62 | + buf[7] = 1; // one byte |
| 63 | + |
| 64 | + self.write(&buf[..])?; |
| 65 | + |
| 66 | + // Wait for response |
| 67 | + let mut pfd: libc::pollfd = unsafe { mem::zeroed() }; |
| 68 | + pfd.fd = self.fd.fileno; |
| 69 | + pfd.events = libc::POLLIN; |
| 70 | + let nfds = unsafe { libc::poll(&mut pfd, 1, 100) }; |
| 71 | + if nfds == -1 { |
| 72 | + return Err(io::Error::last_os_error()); |
| 73 | + } |
| 74 | + if nfds == 0 { |
| 75 | + debug!("device timeout {}", i); |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + // Read response |
| 80 | + self.read(&mut buf[..])?; |
| 81 | + |
| 82 | + return Ok(()); |
| 83 | + } |
| 84 | + |
| 85 | + Err(io_err("no response from device")) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl PartialEq for Device { |
| 90 | + fn eq(&self, other: &Device) -> bool { |
| 91 | + self.fd == other.fd |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl Read for Device { |
| 96 | + fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
| 97 | + let bufp = buf.as_mut_ptr() as *mut libc::c_void; |
| 98 | + let nread = unsafe { libc::read(self.fd.fileno, bufp, buf.len()) }; |
| 99 | + if nread == -1 { |
| 100 | + return Err(io::Error::last_os_error()); |
| 101 | + } |
| 102 | + Ok(nread as usize) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +impl Write for Device { |
| 107 | + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 108 | + // Always skip the first byte (report number) |
| 109 | + let data = &buf[1..]; |
| 110 | + let data_ptr = data.as_ptr() as *const libc::c_void; |
| 111 | + let nwrit = unsafe { |
| 112 | + libc::write(self.fd.fileno, data_ptr, data.len()) |
| 113 | + }; |
| 114 | + if nwrit == -1 { |
| 115 | + return Err(io::Error::last_os_error()); |
| 116 | + } |
| 117 | + // Pretend we wrote the report number byte |
| 118 | + Ok(nwrit as usize + 1) |
| 119 | + } |
| 120 | + |
| 121 | + fn flush(&mut self) -> io::Result<()> { |
| 122 | + Ok(()) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +impl U2FDevice for Device { |
| 127 | + fn get_cid<'a>(&'a self) -> &'a [u8; 4] { |
| 128 | + &self.cid |
| 129 | + } |
| 130 | + |
| 131 | + fn set_cid(&mut self, cid: [u8; 4]) { |
| 132 | + self.cid = cid; |
| 133 | + } |
| 134 | +} |
0 commit comments