diff options
author | Saturneric <[email protected]> | 2022-05-12 17:11:32 +0000 |
---|---|---|
committer | Saturneric <[email protected]> | 2022-05-12 17:11:32 +0000 |
commit | 865ce3654497c45cba8e0f7f6bd2d613f8798c97 (patch) | |
tree | cdfc5499b6246f04ca0956dbfdd54206335ab9cc | |
parent | fix(ui): adjuest timeout value for keyserver test (diff) | |
download | GpgFrontend-dev/2.0.7.tar.gz GpgFrontend-dev/2.0.7.zip |
fix(core): fix a bug when key in smart carddev/2.0.7
-rw-r--r-- | src/core/function/gpg/GpgKeyGetter.cpp | 48 | ||||
-rw-r--r-- | src/core/function/gpg/GpgKeyGetter.h | 16 | ||||
-rw-r--r-- | src/ui/widgets/KeyList.cpp | 5 |
3 files changed, 48 insertions, 21 deletions
diff --git a/src/core/function/gpg/GpgKeyGetter.cpp b/src/core/function/gpg/GpgKeyGetter.cpp index 9a7b505c..2b7c8472 100644 --- a/src/core/function/gpg/GpgKeyGetter.cpp +++ b/src/core/function/gpg/GpgKeyGetter.cpp @@ -44,16 +44,14 @@ GpgFrontend::GpgKeyGetter::GpgKeyGetter(int channel) << "channel:" << channel; } -GpgFrontend::GpgKey GpgFrontend::GpgKeyGetter::GetKey(const std::string& fpr) { +GpgFrontend::GpgKey GpgFrontend::GpgKeyGetter::GetKey(const std::string& fpr, + bool use_cache) { LOG(INFO) << "called"; // find in cache first - { - std::lock_guard<std::mutex> lock(keys_cache_mutex_); - if (keys_cache_.find(fpr) != keys_cache_.end()) { - std::lock_guard<std::mutex> lock(ctx_mutex_); - return keys_cache_[fpr].Copy(); - } + if (use_cache) { + auto key = get_key_in_cache(fpr); + if (key.IsGood()) return key; } gpgme_key_t _p_key = nullptr; @@ -66,15 +64,12 @@ GpgFrontend::GpgKey GpgFrontend::GpgKeyGetter::GetKey(const std::string& fpr) { } } -GpgFrontend::GpgKey GpgFrontend::GpgKeyGetter::GetPubkey( - const std::string& fpr) { +GpgFrontend::GpgKey GpgFrontend::GpgKeyGetter::GetPubkey(const std::string& fpr, + bool use_cache) { // find in cache first - { - std::lock_guard<std::mutex> lock(keys_cache_mutex_); - if (keys_cache_.find(fpr) != keys_cache_.end()) { - std::lock_guard<std::mutex> lock(ctx_mutex_); - return keys_cache_[fpr].Copy(); - } + if (use_cache) { + auto key = get_key_in_cache(fpr); + if (key.IsGood()) return key; } gpgme_key_t _p_key = nullptr; @@ -124,8 +119,17 @@ void GpgFrontend::GpgKeyGetter::FlushKeyCache() { std::lock_guard<std::mutex> lock(keys_cache_mutex_); gpgme_key_t key; while ((err = gpgme_op_keylist_next(ctx_, &key)) == GPG_ERR_NO_ERROR) { + auto gpg_key = GpgKey(std::move(key)); + + // detect if the key is in a smartcard + // if so, try to get full information using gpgme_get_key() + // this maybe a bug in gpgme + if (gpg_key.IsHasCardKey()) { + gpg_key = GetKey(gpg_key.GetId(), false); + } + LOG(INFO) << "LoadKey Fpr:" << key->fpr << "Id:" << key->subkeys->keyid; - keys_cache_.insert({key->subkeys->keyid, GpgKey(std::move(key))}); + keys_cache_.insert({key->subkeys->keyid, std::move(gpg_key)}); } } @@ -164,3 +168,15 @@ GpgFrontend::KeyListPtr GpgFrontend::GpgKeyGetter::GetKeysCopy( for (const auto& key : *keys) keys_copy->push_back(key.Copy()); return keys_copy; } + +GpgFrontend::GpgKey GpgFrontend::GpgKeyGetter::get_key_in_cache( + const std::string& id) { + std::lock_guard<std::mutex> lock(keys_cache_mutex_); + if (keys_cache_.find(id) != keys_cache_.end()) { + std::lock_guard<std::mutex> lock(ctx_mutex_); + // return a copy of the key in cache + return keys_cache_[id].Copy(); + } + // return a bad key + return GpgKey(); +} diff --git a/src/core/function/gpg/GpgKeyGetter.h b/src/core/function/gpg/GpgKeyGetter.h index 72cd777c..c96dbea7 100644 --- a/src/core/function/gpg/GpgKeyGetter.h +++ b/src/core/function/gpg/GpgKeyGetter.h @@ -59,7 +59,7 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyGetter * @param fpr * @return GpgKey */ - GpgKey GetKey(const std::string& id); + GpgKey GetKey(const std::string& id, bool use_cache = true); /** * @brief Get the Keys object @@ -75,7 +75,7 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyGetter * @param fpr * @return GpgKey */ - GpgKey GetPubkey(const std::string& id); + GpgKey GetPubkey(const std::string& id, bool use_cache = true); /** * @brief Get all the keys by receiving a linked list @@ -108,7 +108,7 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyGetter private: /** - * @brief + * @brief Get the gpgme context object * */ GpgContext& ctx_ = @@ -121,7 +121,7 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyGetter mutable std::mutex ctx_mutex_; /** - * @brief cache the keys with key fpr + * @brief cache the keys with key id * */ std::map<std::string, GpgKey> keys_cache_; @@ -131,6 +131,14 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyGetter * */ mutable std::mutex keys_cache_mutex_; + + /** + * @brief Get the Key object + * + * @param id + * @return GpgKey + */ + GpgKey get_key_in_cache(const std::string& id); }; } // namespace GpgFrontend diff --git a/src/ui/widgets/KeyList.cpp b/src/ui/widgets/KeyList.cpp index 237576ad..bbddc84b 100644 --- a/src/ui/widgets/KeyList.cpp +++ b/src/ui/widgets/KeyList.cpp @@ -66,8 +66,11 @@ void KeyList::init() { connect(SignalStation::GetInstance(), &SignalStation::SignalKeyDatabaseRefreshDone, this, &KeyList::SlotRefresh); + + // register key database sync signal for refresh button connect(ui_->refreshKeyListButton, &QPushButton::clicked, this, - &KeyList::SlotRefresh); + &KeyList::SignalRefreshDatabase); + connect(ui_->uncheckButton, &QPushButton::clicked, this, &KeyList::uncheck_all); connect(ui_->checkALLButton, &QPushButton::clicked, this, |