Make other YubiKey driver methods thread-safe

This commit is contained in:
Janek Bevendorff 2017-02-24 17:50:19 +01:00
parent b10cb1c83c
commit 18844d096a
No known key found for this signature in database
GPG key ID: CFEC2F6850BFFA53
3 changed files with 19 additions and 5 deletions

View file

@ -33,7 +33,6 @@ YkChallengeResponseKey::YkChallengeResponseKey(int slot, bool blocking)
: m_slot(slot), : m_slot(slot),
m_blocking(blocking) m_blocking(blocking)
{ {
connect(this, SIGNAL(userInteractionRequired()), KEEPASSXC_MAIN_WINDOW, SLOT(showYubiKeyPopup())); connect(this, SIGNAL(userInteractionRequired()), KEEPASSXC_MAIN_WINDOW, SLOT(showYubiKeyPopup()));
connect(this, SIGNAL(userConfirmed()), KEEPASSXC_MAIN_WINDOW, SLOT(hideYubiKeyPopup())); connect(this, SIGNAL(userConfirmed()), KEEPASSXC_MAIN_WINDOW, SLOT(hideYubiKeyPopup()));
} }

View file

@ -34,7 +34,7 @@
#define m_yk (static_cast<YK_KEY*>(m_yk_void)) #define m_yk (static_cast<YK_KEY*>(m_yk_void))
#define m_ykds (static_cast<YK_STATUS*>(m_ykds_void)) #define m_ykds (static_cast<YK_STATUS*>(m_ykds_void))
YubiKey::YubiKey() : m_yk_void(NULL), m_ykds_void(NULL) YubiKey::YubiKey() : m_yk_void(NULL), m_ykds_void(NULL), m_mutex(QMutex::Recursive)
{ {
} }
@ -51,11 +51,14 @@ YubiKey* YubiKey::instance()
bool YubiKey::init() bool YubiKey::init()
{ {
m_mutex.lock();
// previously initialized // previously initialized
if (m_yk != NULL && m_ykds != NULL) { if (m_yk != NULL && m_ykds != NULL) {
if (yk_get_status(m_yk, m_ykds)) { if (yk_get_status(m_yk, m_ykds)) {
// Still connected // Still connected
m_mutex.unlock();
return true; return true;
} else { } else {
// Initialized but not connected anymore, re-init // Initialized but not connected anymore, re-init
@ -64,12 +67,14 @@ bool YubiKey::init()
} }
if (!yk_init()) { if (!yk_init()) {
m_mutex.unlock();
return false; return false;
} }
// TODO: handle multiple attached hardware devices // TODO: handle multiple attached hardware devices
m_yk_void = static_cast<void*>(yk_open_first_key()); m_yk_void = static_cast<void*>(yk_open_first_key());
if (m_yk == NULL) { if (m_yk == NULL) {
m_mutex.unlock();
return false; return false;
} }
@ -77,14 +82,18 @@ bool YubiKey::init()
if (m_ykds == NULL) { if (m_ykds == NULL) {
yk_close_key(m_yk); yk_close_key(m_yk);
m_yk_void = NULL; m_yk_void = NULL;
m_mutex.unlock();
return false; return false;
} }
m_mutex.unlock();
return true; return true;
} }
bool YubiKey::deinit() bool YubiKey::deinit()
{ {
m_mutex.lock();
if (m_yk) { if (m_yk) {
yk_close_key(m_yk); yk_close_key(m_yk);
m_yk_void = NULL; m_yk_void = NULL;
@ -95,6 +104,8 @@ bool YubiKey::deinit()
m_ykds_void = NULL; m_ykds_void = NULL;
} }
m_mutex.unlock();
return true; return true;
} }
@ -119,9 +130,13 @@ void YubiKey::detect()
emit notFound(); emit notFound();
} }
bool YubiKey::getSerial(unsigned int& serial) const bool YubiKey::getSerial(unsigned int& serial)
{ {
if (!yk_get_serial(m_yk, 1, 0, &serial)) { m_mutex.lock();
int result = yk_get_serial(m_yk, 1, 0, &serial);
m_mutex.unlock();
if (!result) {
return false; return false;
} }

View file

@ -70,7 +70,7 @@ public:
* @param serial serial number * @param serial serial number
* @return true on success * @return true on success
*/ */
bool getSerial(unsigned int& serial) const; bool getSerial(unsigned int& serial);
/** /**
* @brief YubiKey::detect - probe for attached YubiKeys * @brief YubiKey::detect - probe for attached YubiKeys