Skip to content

Commit

Permalink
Fix issues with Hardware Key auto detection
Browse files Browse the repository at this point in the history
* Fix #10656 - Add a small delay when before auto-polling hardware keys to all them to settle immediately after plugging in. This resolves an issue where the key's serial number could not be resolved due to hardware timeout.
* Also fix use of uninitialized variable if polling serial number fails for whatever reason.

* Fix typo in macOS key registration code

* Prevent registering duplicate listeners on window focus. These were not de-registered because we didn't trigger on unfocus. Show/Hide are sufficient triggers to add and remove listeners.
  • Loading branch information
droidmonkey committed May 4, 2024
1 parent 1896883 commit 388aeb0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
8 changes: 5 additions & 3 deletions src/gui/DatabaseOpenWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ bool DatabaseOpenWidget::event(QEvent* event)
bool ret = DialogyWidget::event(event);

switch (event->type()) {
case QEvent::Show:
case QEvent::WindowActivate: {
case QEvent::Show: {
if (isOnQuickUnlockScreen() && (m_db.isNull() || !canPerformQuickUnlock())) {
resetQuickUnlock();
}
Expand Down Expand Up @@ -512,7 +511,10 @@ void DatabaseOpenWidget::pollHardwareKey(bool manualTrigger)
m_pollingHardwareKey = true;
m_manualHardwareKeyRefresh = manualTrigger;

YubiKey::instance()->findValidKeysAsync();
// Add a delay, if this is an automatic trigger, to allow the USB device to settle as
// the device may not report a valid serial number immediately after plugging in
int delay = manualTrigger ? 0 : 1000;
QTimer::singleShot(delay, this, [this] { YubiKey::instance()->findValidKeysAsync(); });
}

void DatabaseOpenWidget::hardwareKeyResponse(bool found)
Expand Down
2 changes: 1 addition & 1 deletion src/gui/osutils/macutils/DeviceListenerMac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void DeviceListenerMac::registerHotplugCallback(bool arrived, bool left, int ven
CFRelease(vid);
}
if (productId > 0) {
auto pid = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vendorId);
auto pid = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &productId);
CFDictionaryAddValue(matchingDict, CFSTR(kIOHIDProductIDKey), pid);
CFRelease(pid);
}
Expand Down
19 changes: 14 additions & 5 deletions src/keys/drivers/YubiKeyInterfaceUSB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,22 @@ namespace
yk_close_key(key);
}

void printError()
{
if (yk_errno == YK_EUSBERR) {
qWarning("Hardware key USB error: %s", yk_usb_strerror());
} else {
qWarning("Hardware key error: %s", yk_strerror(yk_errno));
}
}

unsigned int getSerial(YK_KEY* key)
{
unsigned int serial;
yk_get_serial(key, 1, 0, &serial);
if (!yk_get_serial(key, 1, 0, &serial)) {
printError();
return 0;
}
return serial;
}

Expand All @@ -70,11 +82,8 @@ namespace
} else if (yk_errno == YK_ENOKEY) {
// No more connected keys
break;
} else if (yk_errno == YK_EUSBERR) {
qWarning("Hardware key USB error: %s", yk_usb_strerror());
} else {
qWarning("Hardware key error: %s", yk_strerror(yk_errno));
}
printError();
}
return nullptr;
}
Expand Down

0 comments on commit 388aeb0

Please sign in to comment.