| 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::io; |
| 8 |
use std::os::unix::io::RawFd; |
| 9 |
use std::ptr; |
| 10 |
|
| 11 |
use crate::transport::hidproto::*; |
| 12 |
use crate::util::from_unix_result; |
| 13 |
|
| 14 |
#[allow(non_camel_case_types)] |
| 15 |
#[repr(C)] |
| 16 |
#[derive(Debug)] |
| 17 |
pub struct GenDescriptor { |
| 18 |
ugd_data: *mut u8, |
| 19 |
ugd_lang_id: u16, |
| 20 |
ugd_maxlen: u16, |
| 21 |
ugd_actlen: u16, |
| 22 |
ugd_offset: u16, |
| 23 |
ugd_config_index: u8, |
| 24 |
ugd_string_index: u8, |
| 25 |
ugd_iface_index: u8, |
| 26 |
ugd_altif_index: u8, |
| 27 |
ugd_endpt_index: u8, |
| 28 |
ugd_report_index: u8, |
| 29 |
reserved: [u8; 16], |
| 30 |
} |
| 31 |
|
| 32 |
impl Default for GenDescriptor { |
| 33 |
fn default() -> GenDescriptor { |
| 34 |
GenDescriptor { |
| 35 |
ugd_data: ptr::null_mut(), |
| 36 |
ugd_lang_id: 0, |
| 37 |
ugd_maxlen: 65535, |
| 38 |
ugd_actlen: 0, |
| 39 |
ugd_offset: 0, |
| 40 |
ugd_config_index: 0, |
| 41 |
ugd_string_index: 0, |
| 42 |
ugd_iface_index: 0, |
| 43 |
ugd_altif_index: 0, |
| 44 |
ugd_endpt_index: 0, |
| 45 |
ugd_report_index: 0, |
| 46 |
reserved: [0; 16], |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
const IOWR: u32 = 0x40000000 | 0x80000000; |
| 52 |
|
| 53 |
const IOCPARM_SHIFT: u32 = 13; |
| 54 |
const IOCPARM_MASK: u32 = (1 << IOCPARM_SHIFT) - 1; |
| 55 |
|
| 56 |
const TYPESHIFT: u32 = 8; |
| 57 |
const SIZESHIFT: u32 = 16; |
| 58 |
|
| 59 |
macro_rules! ioctl { |
| 60 |
($dir:expr, $name:ident, $ioty:expr, $nr:expr, $size:expr; $ty:ty) => { |
| 61 |
pub unsafe fn $name(fd: libc::c_int, val: *mut $ty) -> io::Result<libc::c_int> { |
| 62 |
let ioc = ($dir as u32) |
| 63 |
| (($size as u32 & IOCPARM_MASK) << SIZESHIFT) |
| 64 |
| (($ioty as u32) << TYPESHIFT) |
| 65 |
| ($nr as u32); |
| 66 |
from_unix_result(libc::ioctl(fd, ioc as libc::c_ulong, val)) |
| 67 |
} |
| 68 |
}; |
| 69 |
} |
| 70 |
|
| 71 |
// https://github.com/freebsd/freebsd/blob/master/sys/dev/usb/usb_ioctl.h |
| 72 |
ioctl!(IOWR, usb_get_report_desc, b'U', 21, 32; /*struct*/ GenDescriptor); |
| 73 |
|
| 74 |
fn read_report_descriptor(fd: RawFd) -> io::Result<ReportDescriptor> { |
| 75 |
let mut desc = GenDescriptor::default(); |
| 76 |
let _ = unsafe { usb_get_report_desc(fd, &mut desc)? }; |
| 77 |
desc.ugd_maxlen = desc.ugd_actlen; |
| 78 |
let mut value = vec![0; desc.ugd_actlen as usize]; |
| 79 |
desc.ugd_data = value.as_mut_ptr(); |
| 80 |
let _ = unsafe { usb_get_report_desc(fd, &mut desc)? }; |
| 81 |
Ok(ReportDescriptor { value }) |
| 82 |
} |
| 83 |
|
| 84 |
pub fn is_u2f_device(fd: RawFd) -> bool { |
| 85 |
match read_report_descriptor(fd) { |
| 86 |
Ok(desc) => has_fido_usage(desc), |
| 87 |
Err(_) => false, // Upon failure, just say it's not a U2F device. |
| 88 |
} |
| 89 |
} |