use std::collections::HashMap;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::thread;
use std::time::Duration;
use crate::core::Transport;
#[cfg(feature = "pcsc")]
use crate::device::read_info_ccid;
use crate::device::{DeviceError, ReinsertStatus, YubiKeyDevice, usb_interfaces_from_pid};
#[cfg(feature = "hid")]
use crate::device::{read_info_fido, read_info_otp};
use crate::fido::FidoConnection;
use crate::management::{Capability, DeviceInfo, UsbInterface};
use crate::otp::OtpConnection;
use crate::smartcard::SmartCardConnection;
#[cfg(feature = "hid")]
use super::hidapi::{
FidoDeviceInfo, HidDeviceInfo, HidError, HidFidoConnection, HidOtpConnection,
list_fido_devices, list_otp_devices,
};
#[cfg(feature = "pcsc")]
use super::pcsc::{
PcscError, PcscSmartCardConnection, is_reader_usb, list_readers, list_readers_with_state,
};
#[cfg(windows)]
use super::setupdi::list_setupdi_devices;
#[cfg(not(feature = "hid"))]
#[derive(Debug, Clone)]
struct HidDeviceInfo {
pid: u16,
}
#[cfg(not(feature = "hid"))]
#[derive(Debug, Clone)]
struct FidoDeviceInfo {
pid: u16,
}
#[cfg(feature = "pcsc")]
impl From<PcscError> for DeviceError {
fn from(e: PcscError) -> Self {
Self::Transport(Box::new(e))
}
}
#[cfg(feature = "hid")]
impl From<HidError> for DeviceError {
fn from(e: HidError) -> Self {
Self::Transport(Box::new(e))
}
}
#[derive(Debug, Clone)]
pub struct LocalYubiKeyDevice {
pub reader_name: Option<String>,
pub hid_path: Option<String>,
pub fido_path: Option<String>,
pid: Option<u16>,
transport: Transport,
info: DeviceInfo,
}
impl LocalYubiKeyDevice {
pub fn info(&self) -> &DeviceInfo {
&self.info
}
pub fn refresh_info(&mut self) -> Result<(), DeviceError> {
self.refresh_info_with_reader(|device| {
#[cfg(feature = "pcsc")]
if device.reader_name.is_some() {
return read_info_ccid(device.open_smartcard()?, device.pid).map(|(info, _)| info);
}
#[cfg(feature = "hid")]
if device.hid_path.is_some() {
return read_info_otp(
device.open_otp()?,
device.pid.ok_or(DeviceError::NoDeviceFound)?,
)
.map(|(info, _)| info)
.map_err(|(error, _)| error);
}
#[cfg(feature = "hid")]
if device.fido_path.is_some() {
return read_info_fido(
device.open_fido()?,
device.pid.ok_or(DeviceError::NoDeviceFound)?,
)
.map(|(info, _)| info)
.map_err(|(error, _)| error);
}
let _ = device;
Err(DeviceError::NoDeviceFound)
})
}
fn refresh_info_with_reader(
&mut self,
read: impl FnOnce(&Self) -> Result<DeviceInfo, DeviceError>,
) -> Result<(), DeviceError> {
let info = read(self)?;
if info.serial != self.info.serial || info.version != self.info.version {
return Err(DeviceError::WrongDevice);
}
self.info = info;
Ok(())
}
pub fn pid(&self) -> Option<u16> {
self.pid
}
pub fn transport(&self) -> Transport {
self.transport
}
pub fn name(&self) -> String {
get_name(&self.info)
}
pub(crate) fn from_parts(
reader_name: Option<String>,
hid_path: Option<String>,
fido_path: Option<String>,
pid: Option<u16>,
transport: Transport,
info: DeviceInfo,
) -> Self {
LocalYubiKeyDevice {
reader_name,
hid_path,
fido_path,
pid,
transport,
info,
}
}
#[cfg(feature = "pcsc")]
pub fn open_reader(reader_name: &str) -> Result<Self, DeviceError> {
let (info, transport) = read_info_reader(reader_name)?;
Ok(LocalYubiKeyDevice {
reader_name: Some(reader_name.to_string()),
hid_path: None,
fido_path: None,
pid: None,
transport,
info,
})
}
#[cfg(not(feature = "pcsc"))]
pub fn open_reader(_reader_name: &str) -> Result<Self, DeviceError> {
Err(DeviceError::UnsupportedFeature("pcsc"))
}
#[cfg(feature = "pcsc")]
pub fn open_smartcard(&self) -> Result<PcscSmartCardConnection, DeviceError> {
let reader = self
.reader_name
.as_deref()
.ok_or(DeviceError::CcidUnavailable)?;
let mut last_err = None;
for attempt in 0..9 {
match PcscSmartCardConnection::open(reader) {
Ok(conn) => return Ok(conn),
Err(e) => {
if attempt < 8 && e.is_no_card() {
log::debug!(
"SmartCard not ready (attempt {}), retrying in 500ms...",
attempt + 1
);
thread::sleep(Duration::from_millis(500));
last_err = Some(e);
} else if e.is_sharing_violation() {
return Err(DeviceError::CcidUnavailable);
} else {
return Err(e.into());
}
}
}
}
Err(last_err.unwrap().into())
}
#[cfg(not(feature = "pcsc"))]
pub fn open_smartcard(&self) -> Result<Box<dyn SmartCardConnection + Send>, DeviceError> {
Err(DeviceError::UnsupportedFeature("pcsc"))
}
#[cfg(feature = "hid")]
pub fn open_otp(&self) -> Result<HidOtpConnection, DeviceError> {
let path = self.hid_path.as_deref().ok_or(DeviceError::NoDeviceFound)?;
Ok(HidOtpConnection::new(path)?)
}
#[cfg(not(feature = "hid"))]
pub fn open_otp(&self) -> Result<Box<dyn OtpConnection + Send>, DeviceError> {
Err(DeviceError::UnsupportedFeature("hid"))
}
#[cfg(feature = "hid")]
pub fn open_fido(&self) -> Result<HidFidoConnection, DeviceError> {
let path = self
.fido_path
.as_deref()
.ok_or(DeviceError::NoDeviceFound)?;
let fido_devs = list_fido_devices()?;
let info = fido_devs
.into_iter()
.find(|d| d.path == path)
.ok_or(DeviceError::NoDeviceFound)?;
Ok(HidFidoConnection::open(&info)?)
}
#[cfg(not(feature = "hid"))]
pub fn open_fido(&self) -> Result<Box<dyn FidoConnection + Send>, DeviceError> {
Err(DeviceError::UnsupportedFeature("hid"))
}
#[cfg(any(feature = "hid", test))]
fn merge_from(&mut self, other: LocalYubiKeyDevice) {
if self.reader_name.is_none() {
self.reader_name = other.reader_name;
}
if self.hid_path.is_none() {
self.hid_path = other.hid_path;
}
if self.fido_path.is_none() {
self.fido_path = other.fido_path;
}
if self.pid.is_none() {
self.pid = other.pid;
}
if self.info.serial.is_none() && other.info.serial.is_some()
|| self.info.serial == other.info.serial && other.info.version > self.info.version
{
self.info = other.info;
}
}
pub fn reinsert(
&mut self,
status_cb: &dyn Fn(ReinsertStatus),
cancelled: &dyn Fn() -> bool,
) -> Result<(), DeviceError> {
super::monitor::with_paused_monitors(|| {
match self.transport {
Transport::Usb => self.reinsert_usb(status_cb, cancelled)?,
Transport::Nfc => self.reinsert_nfc(status_cb, cancelled)?,
}
Ok(self.clone())
})
}
fn reinsert_usb(
&mut self,
status_cb: &dyn Fn(ReinsertStatus),
cancelled: &dyn Fn() -> bool,
) -> Result<(), DeviceError> {
self.reinsert_usb_with_discovery(
status_cb,
cancelled,
scan_usb_devices,
list_devices,
|| thread::sleep(Duration::from_millis(250)),
)
}
fn reinsert_usb_with_discovery(
&mut self,
status_cb: &dyn Fn(ReinsertStatus),
cancelled: &dyn Fn() -> bool,
mut scan: impl FnMut() -> (HashMap<u16, usize>, u64),
mut discover: impl FnMut(UsbInterface) -> Result<Vec<Self>, DeviceError>,
wait: impl Fn(),
) -> Result<(), DeviceError> {
let mut interfaces = UsbInterface(0);
if self.reader_name.is_some() {
interfaces = interfaces | UsbInterface::CCID;
}
if self.hid_path.is_some() {
interfaces = interfaces | UsbInterface::OTP;
}
if self.fido_path.is_some() {
interfaces = interfaces | UsbInterface::FIDO;
}
let (pids, mut state) = scan();
let n_devs: usize = pids.values().sum();
let my_serial = self.info.serial;
let my_version = self.info.version;
let mut removed = false;
log::debug!("Waiting for removal of device serial={my_serial:?}");
status_cb(ReinsertStatus::Remove);
'reinsert: loop {
wait();
if cancelled() {
return Err(DeviceError::Cancelled);
}
let (new_pids, new_state) = scan();
if new_state == state && (!removed || new_pids.values().sum::<usize>() != n_devs) {
continue;
}
state = new_state;
let devs = match discover(interfaces) {
Ok(devs) => devs,
Err(e) if is_transient_reinsert_error(&e) => {
log::debug!("Ignoring transient reinsert enumeration error: {e}");
Vec::new()
}
Err(e) => return Err(e),
};
if !removed {
if new_pids == pids {
continue;
}
let new_n: usize = new_pids.values().sum();
if new_n + 1 != n_devs
|| devs
.iter()
.any(|d| d.info.serial == my_serial && d.info.version == my_version)
{
return Err(DeviceError::WrongDevice);
}
removed = true;
log::debug!("Device removed, waiting for reinsertion");
status_cb(ReinsertStatus::Reinsert);
} else {
let new_n: usize = new_pids.values().sum();
if new_n != n_devs {
return Err(DeviceError::WrongDevice);
}
let mut found_different = false;
for d in &devs {
if d.info.serial == my_serial && d.info.version == my_version {
if !d.has_usb_paths(interfaces) {
continue 'reinsert;
}
log::debug!("Device reinserted");
let found = d;
self.reader_name = found.reader_name.clone();
self.hid_path = found.hid_path.clone();
self.fido_path = found.fido_path.clone();
self.pid = found.pid;
self.info = found.info.clone();
return Ok(());
}
if d.info.serial.is_some()
&& (d.info.serial != my_serial || d.info.version != my_version)
{
found_different = true;
}
}
if found_different {
return Err(DeviceError::WrongDevice);
}
}
}
}
fn has_usb_paths(&self, interfaces: UsbInterface) -> bool {
(!interfaces.contains(UsbInterface::CCID) || self.reader_name.is_some())
&& (!interfaces.contains(UsbInterface::OTP) || self.hid_path.is_some())
&& (!interfaces.contains(UsbInterface::FIDO) || self.fido_path.is_some())
}
fn reinsert_nfc(
&mut self,
status_cb: &dyn Fn(ReinsertStatus),
cancelled: &dyn Fn() -> bool,
) -> Result<(), DeviceError> {
#[cfg(not(feature = "pcsc"))]
{
let _ = (status_cb, cancelled);
Err(DeviceError::UnsupportedFeature("pcsc"))
}
#[cfg(feature = "pcsc")]
{
let reader = self
.reader_name
.as_deref()
.ok_or(DeviceError::NoDeviceFound)?;
let my_serial = self.info.serial;
let my_version = self.info.version;
let mut removed = false;
log::debug!("NFC reinsert: waiting for card removal from reader {reader}");
status_cb(ReinsertStatus::Remove);
loop {
thread::sleep(Duration::from_millis(500));
if cancelled() {
return Err(DeviceError::Cancelled);
}
if !removed {
match PcscSmartCardConnection::open(reader) {
Ok(_conn) => continue, Err(e) if e.is_no_card() => {
removed = true;
log::debug!("NFC card removed, waiting for tap");
status_cb(ReinsertStatus::Reinsert);
}
Err(e) => return Err(e.into()),
}
} else {
match read_info_reader(reader) {
Ok((info, _transport)) => {
if info.serial == my_serial && info.version == my_version {
log::debug!("NFC card reinserted successfully");
self.info = info;
thread::sleep(Duration::from_secs(1));
return Ok(());
}
return Err(DeviceError::WrongDevice);
}
Err(_) => continue, }
}
}
}
}
}
fn is_transient_reinsert_error(e: &DeviceError) -> bool {
#[cfg(feature = "pcsc")]
if let DeviceError::Transport(source) = e {
return source
.downcast_ref::<PcscError>()
.is_some_and(|e| e.is_unavailable() || e.is_no_card());
}
let _ = e;
false
}
impl YubiKeyDevice for LocalYubiKeyDevice {
fn info(&self) -> &DeviceInfo {
self.info()
}
fn transport(&self) -> Transport {
self.transport()
}
fn name(&self) -> String {
self.name()
}
fn pid(&self) -> Option<u16> {
self.pid
}
fn reader_name(&self) -> Option<&str> {
self.reader_name.as_deref()
}
fn usb_interfaces(&self) -> UsbInterface {
self.pid.map(usb_interfaces_from_pid).unwrap_or_else(|| {
if let Some(&usb_caps) = self.info.config.enabled_capabilities.get(&Transport::Usb) {
let mut ifaces = UsbInterface(0);
if usb_caps.contains(Capability::OTP) {
ifaces = ifaces | UsbInterface::OTP;
}
if usb_caps.contains(Capability::FIDO2) || usb_caps.contains(Capability::U2F) {
ifaces = ifaces | UsbInterface::FIDO;
}
if usb_caps.contains(Capability::PIV)
|| usb_caps.contains(Capability::OATH)
|| usb_caps.contains(Capability::OPENPGP)
|| usb_caps.contains(Capability::HSMAUTH)
{
ifaces = ifaces | UsbInterface::CCID;
}
ifaces
} else {
UsbInterface(0)
}
})
}
fn open_smartcard(&self) -> Result<Box<dyn SmartCardConnection + Send>, DeviceError> {
#[cfg(feature = "pcsc")]
{
Ok(Box::new(self.open_smartcard()?))
}
#[cfg(not(feature = "pcsc"))]
{
Err(DeviceError::UnsupportedFeature("pcsc"))
}
}
fn open_fido(&self) -> Result<Box<dyn FidoConnection + Send>, DeviceError> {
#[cfg(feature = "hid")]
{
Ok(Box::new(self.open_fido()?))
}
#[cfg(not(feature = "hid"))]
{
Err(DeviceError::UnsupportedFeature("hid"))
}
}
fn open_otp(&self) -> Result<Box<dyn OtpConnection + Send>, DeviceError> {
#[cfg(feature = "hid")]
{
Ok(Box::new(self.open_otp()?))
}
#[cfg(not(feature = "hid"))]
{
Err(DeviceError::UnsupportedFeature("hid"))
}
}
fn reinsert(
&mut self,
status_cb: &dyn Fn(ReinsertStatus),
cancelled: &dyn Fn() -> bool,
) -> Result<(), DeviceError> {
self.reinsert(status_cb, cancelled)
}
fn clone_box(&self) -> Box<dyn YubiKeyDevice> {
Box::new(self.clone())
}
}
impl fmt::Display for LocalYubiKeyDevice {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name())?;
if let Some(serial) = self.info.serial {
write!(f, " (serial: {serial})")?;
}
Ok(())
}
}
#[cfg(feature = "pcsc")]
pub(crate) fn pid_from_reader_name(name: &str) -> Option<u16> {
if !is_reader_usb(name) {
return None;
}
let mut interfaces = UsbInterface(0);
if name.contains("OTP") {
interfaces = interfaces | UsbInterface::OTP;
}
if name.contains("CCID") {
interfaces = interfaces | UsbInterface::CCID;
}
if name.contains("FIDO") || name.contains("U2F") {
interfaces = interfaces | UsbInterface::FIDO;
}
let is_neo = name.contains("NEO");
pid_from_interfaces(interfaces, is_neo)
}
#[cfg(feature = "pcsc")]
fn pid_from_interfaces(interfaces: UsbInterface, is_neo: bool) -> Option<u16> {
let otp = (interfaces & UsbInterface::OTP).0 != 0;
let fido = (interfaces & UsbInterface::FIDO).0 != 0;
let ccid = (interfaces & UsbInterface::CCID).0 != 0;
if is_neo {
match (otp, fido, ccid) {
(true, false, false) => Some(0x0110),
(true, false, true) => Some(0x0111),
(false, false, true) => Some(0x0112),
(false, true, false) => Some(0x0113),
(true, true, false) => Some(0x0114),
(false, true, true) => Some(0x0115),
(true, true, true) => Some(0x0116),
_ => None,
}
} else {
match (otp, fido, ccid) {
(true, false, false) => Some(0x0401),
(false, true, false) => Some(0x0402),
(true, true, false) => Some(0x0403),
(false, false, true) => Some(0x0404),
(true, false, true) => Some(0x0405),
(false, true, true) => Some(0x0406),
(true, true, true) => Some(0x0407),
_ => None,
}
}
}
pub fn scan_usb_devices() -> (HashMap<u16, usize>, u64) {
let mut counts: HashMap<u16, usize> = HashMap::new();
let mut fingerprints: Vec<String> = Vec::new();
let mut transport_counts: HashMap<u16, usize> = HashMap::new();
#[cfg(feature = "pcsc")]
if let Ok(readers) = list_readers_with_state() {
for (reader, card_present) in readers {
if let Some(pid) = pid_from_reader_name(&reader) {
*transport_counts.entry(pid).or_insert(0) += 1;
fingerprints.push(reader);
} else {
fingerprints.push(format!(
"{reader}:{}",
if card_present { "present" } else { "absent" }
));
}
}
}
for (pid, count) in &transport_counts {
let entry = counts.entry(*pid).or_insert(0);
*entry = (*entry).max(*count);
}
transport_counts.clear();
#[cfg(feature = "hid")]
if let Ok(hid_devices) = list_otp_devices() {
for hid in hid_devices {
*transport_counts.entry(hid.pid).or_insert(0) += 1;
fingerprints.push(hid.path);
}
}
for (pid, count) in &transport_counts {
let entry = counts.entry(*pid).or_insert(0);
*entry = (*entry).max(*count);
}
transport_counts.clear();
#[cfg(feature = "hid")]
if let Ok(fido_devs) = list_fido_devices() {
for fido in fido_devs {
*transport_counts.entry(fido.pid).or_insert(0) += 1;
fingerprints.push(fido.path);
}
}
for (pid, count) in &transport_counts {
let entry = counts.entry(*pid).or_insert(0);
*entry = (*entry).max(*count);
}
#[cfg(windows)]
{
for dev in list_setupdi_devices() {
if !counts.contains_key(&dev.pid) {
*counts.entry(dev.pid).or_insert(0) += 1;
fingerprints.push(dev.path);
}
}
}
fingerprints.sort();
let mut hasher = std::hash::DefaultHasher::new();
fingerprints.hash(&mut hasher);
let state = hasher.finish();
(counts, state)
}
pub fn list_devices(interfaces: UsbInterface) -> Result<Vec<LocalYubiKeyDevice>, DeviceError> {
log::debug!("Listing YubiKey devices (interfaces: {interfaces})");
let want_ccid = interfaces.contains(UsbInterface::CCID);
let want_otp = interfaces.contains(UsbInterface::OTP);
let want_fido = interfaces.contains(UsbInterface::FIDO);
if want_ccid && !cfg!(feature = "pcsc") {
return Err(DeviceError::UnsupportedFeature("pcsc"));
}
if (want_otp || want_fido) && !cfg!(feature = "hid") {
return Err(DeviceError::UnsupportedFeature("hid"));
}
#[cfg(feature = "pcsc")]
let mut usb_readers: Vec<(u16, String)> = Vec::new(); #[cfg(not(feature = "pcsc"))]
let usb_readers: Vec<(u16, String)> = Vec::new();
#[cfg(feature = "pcsc")]
let mut nfc_readers: Vec<String> = Vec::new();
#[cfg(feature = "pcsc")]
if want_ccid && let Ok(readers) = list_readers() {
for reader in readers {
if is_reader_usb(&reader) {
if let Some(pid) = pid_from_reader_name(&reader) {
usb_readers.push((pid, reader));
}
} else {
nfc_readers.push(reader);
}
}
}
#[cfg(feature = "hid")]
let mut otp_devs: Vec<HidDeviceInfo> = Vec::new();
#[cfg(not(feature = "hid"))]
let otp_devs: Vec<HidDeviceInfo> = Vec::new();
#[cfg(feature = "hid")]
if want_otp && let Ok(devs) = list_otp_devices() {
otp_devs = devs;
}
#[cfg(feature = "hid")]
let mut fido_devs: Vec<FidoDeviceInfo> = Vec::new();
#[cfg(not(feature = "hid"))]
let fido_devs: Vec<FidoDeviceInfo> = Vec::new();
#[cfg(feature = "hid")]
if want_fido && let Ok(devs) = list_fido_devices() {
fido_devs = devs;
}
let mut pid_counts: HashMap<u16, usize> = HashMap::new();
{
let mut ccid_per_pid: HashMap<u16, usize> = HashMap::new();
for &(pid, _) in &usb_readers {
*ccid_per_pid.entry(pid).or_insert(0) += 1;
}
let mut otp_per_pid: HashMap<u16, usize> = HashMap::new();
for hid in &otp_devs {
*otp_per_pid.entry(hid.pid).or_insert(0) += 1;
}
let mut fido_per_pid: HashMap<u16, usize> = HashMap::new();
for fido in &fido_devs {
*fido_per_pid.entry(fido.pid).or_insert(0) += 1;
}
let all_pids: std::collections::HashSet<u16> = ccid_per_pid
.keys()
.chain(otp_per_pid.keys())
.chain(fido_per_pid.keys())
.copied()
.collect();
for pid in all_pids {
let c = ccid_per_pid.get(&pid).copied().unwrap_or(0);
let o = otp_per_pid.get(&pid).copied().unwrap_or(0);
let f = fido_per_pid.get(&pid).copied().unwrap_or(0);
pid_counts.insert(pid, c.max(o).max(f));
}
}
let n_usb: usize = pid_counts.values().sum();
log::debug!(
"Fast scan: {n_usb} USB device(s) across {} PID(s)",
pid_counts.len()
);
let mut devices: Vec<LocalYubiKeyDevice> = Vec::new();
for (&pid, &count) in &pid_counts {
if count <= 1 {
if let Some(dev) = open_single_usb(pid, &usb_readers, &otp_devs, &fido_devs)? {
devices.push(dev);
}
} else {
let mut base: Vec<LocalYubiKeyDevice> = Vec::new();
#[cfg(feature = "pcsc")]
if want_ccid {
for &(p, ref reader) in &usb_readers {
if p == pid
&& let Ok((info, transport)) = read_info_reader(reader)
{
base.push(LocalYubiKeyDevice {
reader_name: Some(reader.clone()),
hid_path: None,
fido_path: None,
pid: Some(pid),
transport,
info,
});
}
}
}
#[cfg(feature = "hid")]
if want_otp {
let mut otp_group = Vec::new();
for hid in &otp_devs {
if hid.pid == pid {
let info = read_info_otp_device(hid)?;
otp_group.push(LocalYubiKeyDevice {
reader_name: None,
hid_path: Some(hid.path.clone()),
fido_path: None,
pid: Some(pid),
transport: Transport::Usb,
info,
});
}
}
if base.is_empty() {
base = otp_group;
} else {
merge_devices(&mut base, otp_group);
}
}
#[cfg(feature = "hid")]
if want_fido {
let mut fido_group = Vec::new();
for fido in &fido_devs {
if fido.pid == pid {
let info = read_info_fido_device(fido)?;
fido_group.push(LocalYubiKeyDevice {
reader_name: None,
hid_path: None,
fido_path: Some(fido.path.clone()),
pid: Some(pid),
transport: Transport::Usb,
info,
});
}
}
if base.is_empty() {
base = fido_group;
} else {
merge_devices(&mut base, fido_group);
}
}
devices.extend(base);
}
}
#[cfg(feature = "pcsc")]
for reader in &nfc_readers {
log::debug!("Checking NFC reader: {reader}");
match read_info_reader(reader) {
Ok((info, transport)) => {
devices.push(LocalYubiKeyDevice {
reader_name: Some(reader.clone()),
hid_path: None,
fido_path: None,
pid: None,
transport,
info,
});
}
Err(e) => {
log::debug!("Skipping NFC reader {reader}: {e}");
}
}
}
Ok(devices)
}
fn open_single_usb(
pid: u16,
_usb_readers: &[(u16, String)],
_otp_devs: &[HidDeviceInfo],
_fido_devs: &[FidoDeviceInfo],
) -> Result<Option<LocalYubiKeyDevice>, DeviceError> {
#[cfg(feature = "pcsc")]
let reader = _usb_readers.iter().find(|(p, _)| *p == pid).map(|(_, r)| r);
#[cfg(feature = "hid")]
let otp = _otp_devs.iter().find(|h| h.pid == pid);
#[cfg(feature = "hid")]
let fido = _fido_devs.iter().find(|f| f.pid == pid);
let mut last_err = None;
#[cfg(feature = "pcsc")]
if let Some(reader_name) = reader {
match read_info_reader(reader_name) {
Ok((info, transport)) => {
return Ok(Some(LocalYubiKeyDevice {
reader_name: Some(reader_name.clone()),
hid_path: {
#[cfg(feature = "hid")]
{
otp.map(|h| h.path.clone())
}
#[cfg(not(feature = "hid"))]
{
None
}
},
fido_path: {
#[cfg(feature = "hid")]
{
fido.map(|f| f.path.clone())
}
#[cfg(not(feature = "hid"))]
{
None
}
},
pid: Some(pid),
transport,
info,
}));
}
Err(e) => last_err = Some(e),
}
}
#[cfg(feature = "hid")]
if let Some(hid) = otp {
match read_info_otp_device(hid) {
Ok(info) => {
return Ok(Some(LocalYubiKeyDevice {
reader_name: None,
hid_path: Some(hid.path.clone()),
fido_path: fido.map(|f| f.path.clone()),
pid: Some(pid),
transport: Transport::Usb,
info,
}));
}
Err(e) => last_err = Some(e),
}
}
#[cfg(feature = "hid")]
if let Some(f) = fido {
match read_info_fido_device(f) {
Ok(info) => {
return Ok(Some(LocalYubiKeyDevice {
reader_name: None,
hid_path: None,
fido_path: Some(f.path.clone()),
pid: Some(pid),
transport: Transport::Usb,
info,
}));
}
Err(e) => last_err = Some(e),
}
}
if let Some(e) = last_err {
Err(e)
} else {
Ok(None)
}
}
#[cfg(any(feature = "hid", test))]
fn merge_devices(base: &mut Vec<LocalYubiKeyDevice>, incoming: Vec<LocalYubiKeyDevice>) {
let mut pid_counts: HashMap<u16, usize> = HashMap::new();
for dev in base.iter().chain(incoming.iter()) {
if let Some(pid) = dev.pid {
*pid_counts.entry(pid).or_insert(0) += 1;
}
}
for inc in incoming {
if let Some(pid) = inc.pid
&& pid_counts.get(&pid) == Some(&2)
{
if let Some(target) = base.iter_mut().find(|d| d.pid == Some(pid)) {
target.merge_from(inc);
continue;
}
}
let identity = (inc.info.version, inc.info.serial);
if let Some(target) = base
.iter_mut()
.find(|d| (d.info.version, d.info.serial) == identity)
{
target.merge_from(inc);
continue;
}
base.push(inc);
}
}
#[cfg(feature = "pcsc")]
fn read_info_reader(reader_name: &str) -> Result<(DeviceInfo, Transport), DeviceError> {
let conn = PcscSmartCardConnection::open(reader_name)?;
let transport = conn.transport();
let pid = pid_from_reader_name(reader_name);
let (info, _conn) = read_info_ccid(conn, pid)?;
Ok((info, transport))
}
#[cfg(feature = "hid")]
fn read_info_otp_device(hid: &HidDeviceInfo) -> Result<DeviceInfo, DeviceError> {
let conn = HidOtpConnection::new(&hid.path)?;
read_info_otp(conn, hid.pid)
.map(|(info, _)| info)
.map_err(|(e, _)| e)
}
#[cfg(feature = "hid")]
fn read_info_fido_device(fido: &FidoDeviceInfo) -> Result<DeviceInfo, DeviceError> {
let conn = HidFidoConnection::open(fido)?;
read_info_fido(conn, fido.pid)
.map(|(info, _)| info)
.map_err(|(e, _)| e)
}
#[cfg(feature = "hid")]
pub fn select_fido(cancel: Option<&dyn Fn() -> bool>) -> Result<LocalYubiKeyDevice, DeviceError> {
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use crate::ctap::CtapSession;
use crate::ctap2::Ctap2Session;
let is_cancelled = || cancel.is_some_and(|f| f());
let fido_devs = list_fido_devices()?;
let done = Arc::new(AtomicBool::new(false));
let selected_path: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None));
let mut active: Vec<(String, thread::JoinHandle<()>)> = Vec::new();
let spawn_select = |info: FidoDeviceInfo,
done: Arc<AtomicBool>,
selected: Arc<Mutex<Option<String>>>|
-> thread::JoinHandle<()> {
thread::spawn(move || {
let Ok(conn) = HidFidoConnection::open(&info) else {
return;
};
let Ok(ctap) = CtapSession::new_fido(conn) else {
return;
};
let Ok(mut session) = Ctap2Session::new(ctap) else {
return;
};
let d = done.clone();
if session
.selection(None, Some(&move || d.load(Ordering::Relaxed)))
.is_ok()
&& !done.swap(true, Ordering::Relaxed)
{
*selected.lock().unwrap() = Some(info.path);
}
})
};
for dev in fido_devs {
let path = dev.path.clone();
let handle = spawn_select(dev, done.clone(), selected_path.clone());
active.push((path, handle));
}
while !done.load(Ordering::Relaxed) && !is_cancelled() {
thread::sleep(Duration::from_millis(250));
active.retain(|(_, h)| !h.is_finished());
if let Ok(devs) = list_fido_devices() {
for dev in devs {
if !active.iter().any(|(p, _)| *p == dev.path) {
let path = dev.path.clone();
let handle = spawn_select(dev, done.clone(), selected_path.clone());
active.push((path, handle));
}
}
}
}
done.store(true, Ordering::Relaxed);
for (_, h) in active {
let _ = h.join();
}
if is_cancelled() {
return Err(DeviceError::Cancelled);
}
let path = selected_path
.lock()
.unwrap()
.take()
.ok_or(DeviceError::NoDeviceFound)?;
let all = UsbInterface::CCID | UsbInterface::OTP | UsbInterface::FIDO;
let devices = list_devices(all)?;
let fido_devs = list_fido_devices()?;
if let Some(fido_info) = fido_devs.iter().find(|d| d.path == path) {
let info = read_info_fido_device(fido_info)?;
if let Some(dev) = devices
.into_iter()
.find(|d| d.info().serial == info.serial && d.info().version == info.version)
{
return Ok(dev);
}
}
Err(DeviceError::NoDeviceFound)
}
pub use crate::device::get_name;
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
use crate::core::Version;
use crate::management::FormFactor;
fn make_info(
version: Version,
form_factor: FormFactor,
is_sky: bool,
is_fips: bool,
serial: Option<u32>,
nfc: bool,
usb_cap: Capability,
pin_complexity: bool,
) -> DeviceInfo {
let mut supported = HashMap::new();
supported.insert(Transport::Usb, usb_cap);
if nfc {
supported.insert(Transport::Nfc, usb_cap);
}
DeviceInfo {
config: crate::management::DeviceConfig {
enabled_capabilities: HashMap::new(),
auto_eject_timeout: None,
challenge_response_timeout: None,
device_flags: None,
nfc_restricted: None,
},
serial,
version,
form_factor,
supported_capabilities: supported,
is_locked: false,
is_fips,
is_sky,
part_number: None,
fips_capable: Capability::NONE,
fips_approved: Capability::NONE,
pin_complexity,
reset_blocked: Capability::NONE,
fps_version: None,
stm_version: None,
version_qualifier: crate::management::VersionQualifier::final_release(version),
name: None,
}
}
fn make_device(
reader_name: Option<&str>,
hid_path: Option<&str>,
fido_path: Option<&str>,
pid: Option<u16>,
version: Version,
serial: Option<u32>,
) -> LocalYubiKeyDevice {
let info = make_info(
version,
FormFactor::UsbAKeychain,
false,
false,
serial,
false,
Capability(Capability::OTP.0 | Capability::PIV.0),
false,
);
LocalYubiKeyDevice {
reader_name: reader_name.map(String::from),
hid_path: hid_path.map(String::from),
fido_path: fido_path.map(String::from),
pid,
transport: Transport::Usb,
info,
}
}
#[test]
fn refresh_updates_configuration_without_losing_transport_paths() {
let mut device = make_device(
Some("reader"),
Some("OTP"),
Some("FIDO"),
Some(0x0407),
Version(5, 7, 0),
Some(12345),
);
let mut info = device.info().clone();
info.is_locked = true;
info.config
.enabled_capabilities
.insert(Transport::Nfc, Capability::PIV);
device.refresh_info_with_reader(|_| Ok(info)).unwrap();
assert!(device.info().is_locked);
assert_eq!(
device.info().config.enabled_capabilities[&Transport::Nfc],
Capability::PIV
);
assert_eq!(device.reader_name.as_deref(), Some("reader"));
assert_eq!(device.hid_path.as_deref(), Some("OTP"));
assert_eq!(device.fido_path.as_deref(), Some("FIDO"));
}
#[test]
fn failed_refresh_preserves_the_original_snapshot() {
let mut device = make_device(
None,
Some("OTP"),
None,
Some(0x0407),
Version(5, 7, 0),
Some(12345),
);
let error = device
.refresh_info_with_reader(|_| Err(DeviceError::CcidUnavailable))
.unwrap_err();
assert!(matches!(error, DeviceError::CcidUnavailable));
for change_serial in [true, false] {
let mut wrong = device.info().clone();
wrong.is_locked = true;
if change_serial {
wrong.serial = Some(54321);
} else {
wrong.version = Version(5, 7, 1);
}
assert!(matches!(
device.refresh_info_with_reader(|_| Ok(wrong)),
Err(DeviceError::WrongDevice)
));
assert_eq!(device.info().serial, Some(12345));
assert_eq!(device.info().version, Version(5, 7, 0));
assert!(!device.info().is_locked);
}
}
#[test]
fn reinsert_retries_partial_discovery_without_a_topology_change() {
let mut device = make_device(
Some("old reader"),
Some("old OTP"),
Some("old FIDO"),
Some(0x0407),
Version(5, 7, 0),
Some(12345),
);
let ready = make_device(
Some("new reader"),
Some("new OTP"),
Some("new FIDO"),
Some(0x0407),
Version(5, 7, 0),
Some(12345),
);
let mut partial = ready.clone();
partial.reader_name = None;
let mut other = ready.clone();
other.info.serial = Some(54321);
let all = HashMap::from([(0x0407, 2)]);
let removed = HashMap::from([(0x0407, 1)]);
let mut scans = [
(all.clone(), 0),
(removed.clone(), 1),
(removed, 1),
(all.clone(), 2),
(all, 2),
]
.into_iter();
let mut discoveries = [
vec![other.clone()],
vec![other.clone(), partial],
vec![other, ready.clone()],
]
.into_iter();
device
.reinsert_usb_with_discovery(
&|_| {},
&|| false,
|| scans.next().expect("reinsertion did not finish"),
|interfaces| {
assert_eq!(
interfaces,
UsbInterface::CCID | UsbInterface::OTP | UsbInterface::FIDO,
);
Ok(discoveries.next().expect("unexpected discovery"))
},
|| {},
)
.unwrap();
assert!(discoveries.next().is_none());
assert_eq!(device.reader_name, ready.reader_name);
assert_eq!(device.hid_path, ready.hid_path);
assert_eq!(device.fido_path, ready.fido_path);
}
#[test]
fn reinsert_rejects_a_different_device() {
let mut device = make_device(
None,
Some("old OTP"),
None,
Some(0x0407),
Version(5, 7, 0),
Some(12345),
);
let mut other = device.clone();
other.info.serial = Some(54321);
let present = HashMap::from([(0x0407, 1)]);
let mut scans = [(present.clone(), 0), (HashMap::new(), 1), (present, 2)].into_iter();
let mut discoveries = [vec![], vec![other]].into_iter();
let result = device.reinsert_usb_with_discovery(
&|_| {},
&|| false,
|| scans.next().expect("wrong device was not rejected"),
|_| Ok(discoveries.next().expect("unexpected discovery")),
|| {},
);
assert!(matches!(result, Err(DeviceError::WrongDevice)));
}
#[test]
fn reinsert_requires_paths_not_just_advertised_interfaces() {
let full = make_device(
Some("Yubico YubiKey OTP+FIDO+CCID 00 00"),
Some("/dev/hidraw0"),
Some("/dev/hidraw1"),
Some(0x0407),
Version(5, 7, 0),
Some(12345),
);
let all = UsbInterface::CCID | UsbInterface::OTP | UsbInterface::FIDO;
assert!(full.has_usb_paths(all));
for missing in [UsbInterface::CCID, UsbInterface::OTP, UsbInterface::FIDO] {
let mut partial = full.clone();
if missing == UsbInterface::CCID {
partial.reader_name = None;
} else if missing == UsbInterface::OTP {
partial.hid_path = None;
} else {
partial.fido_path = None;
}
assert!(partial.usb_interfaces().contains(all));
assert!(!partial.has_usb_paths(all));
assert!(partial.has_usb_paths(UsbInterface(all.0 & !missing.0)));
}
}
#[cfg(all(feature = "hid", not(feature = "pcsc")))]
#[test]
fn test_list_devices_ccid_requires_pcsc() {
assert!(matches!(
list_devices(UsbInterface::CCID),
Err(DeviceError::UnsupportedFeature("pcsc"))
));
}
#[cfg(all(feature = "pcsc", not(feature = "hid")))]
#[test]
fn test_list_devices_hid_interfaces_require_hid() {
assert!(matches!(
list_devices(UsbInterface::OTP),
Err(DeviceError::UnsupportedFeature("hid"))
));
assert!(matches!(
list_devices(UsbInterface::FIDO),
Err(DeviceError::UnsupportedFeature("hid"))
));
}
#[cfg(all(feature = "pcsc", not(feature = "hid")))]
#[test]
fn test_open_fido_requires_hid() {
let dev = make_device(
None,
None,
None,
Some(0x0402),
Version(5, 4, 3),
Some(12345),
);
assert!(matches!(
dev.open_fido(),
Err(DeviceError::UnsupportedFeature("hid"))
));
}
#[cfg(all(feature = "hid", not(feature = "pcsc")))]
#[test]
fn test_open_smartcard_requires_pcsc() {
let dev = make_device(
None,
None,
None,
Some(0x0404),
Version(5, 4, 3),
Some(12345),
);
assert!(matches!(
dev.open_smartcard(),
Err(DeviceError::UnsupportedFeature("pcsc"))
));
}
#[cfg(feature = "pcsc")]
#[test]
fn test_open_smartcard_without_reader_reports_ccid_unavailable() {
let dev = make_device(
None,
None,
Some("/dev/hidraw1"),
Some(0x0407),
Version(5, 4, 3),
Some(12345),
);
assert!(matches!(
dev.open_smartcard(),
Err(DeviceError::CcidUnavailable)
));
}
#[test]
fn test_merge_by_pid_uniqueness() {
let ccid = vec![make_device(
Some("Yubico YubiKey OTP+FIDO+CCID 00"),
None,
None,
Some(0x0407),
Version(5, 4, 3),
Some(12345),
)];
let otp = vec![make_device(
None,
Some("/dev/hidraw0"),
None,
Some(0x0407),
Version(5, 4, 3),
Some(12345),
)];
let fido = vec![make_device(
None,
None,
Some("/dev/hidraw1"),
Some(0x0407),
Version(5, 4, 3),
Some(12345),
)];
let mut result = ccid;
merge_devices(&mut result, otp);
merge_devices(&mut result, fido);
assert_eq!(result.len(), 1, "Should merge into a single device");
let dev = &result[0];
assert!(dev.reader_name.is_some());
assert!(dev.hid_path.is_some());
assert!(dev.fido_path.is_some());
assert_eq!(dev.pid(), Some(0x0407));
}
#[test]
fn test_merge_by_identity() {
let ccid = vec![
make_device(
Some("reader0"),
None,
None,
Some(0x0407),
Version(5, 4, 3),
Some(111),
),
make_device(
Some("reader1"),
None,
None,
Some(0x0407),
Version(5, 4, 3),
Some(222),
),
];
let otp = vec![
make_device(
None,
Some("/dev/h0"),
None,
Some(0x0407),
Version(5, 4, 3),
Some(111),
),
make_device(
None,
Some("/dev/h1"),
None,
Some(0x0407),
Version(5, 4, 3),
Some(222),
),
];
let mut result = ccid;
merge_devices(&mut result, otp);
assert_eq!(result.len(), 2, "Should remain as two devices");
let d1 = result
.iter()
.find(|d| d.info().serial == Some(111))
.unwrap();
assert!(d1.reader_name.is_some());
assert!(d1.hid_path.is_some());
let d2 = result
.iter()
.find(|d| d.info().serial == Some(222))
.unwrap();
assert!(d2.reader_name.is_some());
assert!(d2.hid_path.is_some());
}
}