YubiKey: Retry to recover hotplugging

* Attempt one retry in the event the event the device was removed and
  re-inserted.

Signed-off-by: Kyle Manna <kyle@kylemanna.com>
This commit is contained in:
Kyle Manna 2014-09-06 22:09:06 -07:00
parent faa055010f
commit f7ee528d41
3 changed files with 29 additions and 1 deletions

View File

@ -26,6 +26,8 @@
#include "keys/YkChallengeResponseKey.h" #include "keys/YkChallengeResponseKey.h"
#include "keys/drivers/YubiKey.h" #include "keys/drivers/YubiKey.h"
#include <QDebug>
YkChallengeResponseKey::YkChallengeResponseKey(int slot, YkChallengeResponseKey::YkChallengeResponseKey(int slot,
bool blocking) bool blocking)
: m_slot(slot), : m_slot(slot),
@ -46,11 +48,31 @@ YkChallengeResponseKey* YkChallengeResponseKey::clone() const
/** Assumes yubikey()->init() was called */ /** Assumes yubikey()->init() was called */
bool YkChallengeResponseKey::challenge(const QByteArray& chal) bool YkChallengeResponseKey::challenge(const QByteArray& chal)
{
return challenge(chal, 1);
}
bool YkChallengeResponseKey::challenge(const QByteArray& chal, int retries)
{ {
if (YubiKey::instance()->challenge(m_slot, true, chal, m_key) != YubiKey::ERROR) { if (YubiKey::instance()->challenge(m_slot, true, chal, m_key) != YubiKey::ERROR) {
return true; return true;
} }
/* If challenge failed, retry to detect YubiKeys int the event the YubiKey
* was un-plugged and re-plugged */
while (retries > 0) {
qDebug() << "Attempt" << retries << "to re-detect YubiKey(s)";
retries--;
if (YubiKey::instance()->init() != true) {
continue;
}
if (YubiKey::instance()->challenge(m_slot, true, chal, m_key) != YubiKey::ERROR) {
return true;
}
}
return false; return false;
} }

View File

@ -31,7 +31,8 @@ public:
QByteArray rawKey() const; QByteArray rawKey() const;
YkChallengeResponseKey* clone() const; YkChallengeResponseKey* clone() const;
bool challenge(const QByteArray& challenge); bool challenge(const QByteArray& chal);
bool challenge(const QByteArray& chal, int retries);
QString getName() const; QString getName() const;
bool isBlocking() const; bool isBlocking() const;

View File

@ -182,6 +182,11 @@ YubiKey::ChallengeResult YubiKey::challenge(int slot, bool mayBlock,
int yk_cmd = (slot == 1) ? SLOT_CHAL_HMAC1 : SLOT_CHAL_HMAC2; int yk_cmd = (slot == 1) ? SLOT_CHAL_HMAC1 : SLOT_CHAL_HMAC2;
QByteArray paddedChal = chal; QByteArray paddedChal = chal;
/* Ensure that YubiKey::init() succeeded */
if (m_yk == NULL) {
return ERROR;
}
/* yk_challenge_response() insists on 64 byte response buffer */ /* yk_challenge_response() insists on 64 byte response buffer */
resp.resize(64); resp.resize(64);