diff options
author | Saturn&Eric <[email protected]> | 2022-05-13 19:31:40 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2022-05-13 19:31:40 +0000 |
commit | 49090d1d511a4a0fbcfac253656a40a42716c82e (patch) | |
tree | 2553bd9a1d14c099a0a990576fb7c874f72b3859 /src/core/function/gpg | |
parent | Merge pull request #60 from saturneric/develop-2.0.7 (diff) | |
parent | fix(core): solve memory access issues (diff) | |
download | GpgFrontend-49090d1d511a4a0fbcfac253656a40a42716c82e.tar.gz GpgFrontend-49090d1d511a4a0fbcfac253656a40a42716c82e.zip |
Merge pull request #62 from saturneric/develop-2.0.8v2.0.8
Develop 2.0.8
Diffstat (limited to 'src/core/function/gpg')
-rw-r--r-- | src/core/function/gpg/GpgKeyGetter.cpp | 57 | ||||
-rw-r--r-- | src/core/function/gpg/GpgKeyGetter.h | 16 |
2 files changed, 49 insertions, 24 deletions
diff --git a/src/core/function/gpg/GpgKeyGetter.cpp b/src/core/function/gpg/GpgKeyGetter.cpp index 9a7b505c..ff848e0e 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,18 @@ 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) { - LOG(INFO) << "LoadKey Fpr:" << key->fpr << "Id:" << key->subkeys->keyid; - keys_cache_.insert({key->subkeys->keyid, GpgKey(std::move(key))}); + 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:" << gpg_key.GetFingerprint() + << "Id:" << gpg_key.GetId(); + keys_cache_.insert({gpg_key.GetId(), std::move(gpg_key)}); } } @@ -143,7 +148,7 @@ void GpgFrontend::GpgKeyGetter::FlushKeyCache() { GpgFrontend::KeyListPtr GpgFrontend::GpgKeyGetter::GetKeys( const KeyIdArgsListPtr& ids) { auto keys = std::make_unique<KeyArgsList>(); - for (const auto& id : *ids) keys->push_back(GetKey(id)); + for (const auto& id : *ids) keys->emplace_back(GetKey(id)); return keys; } @@ -152,7 +157,7 @@ GpgFrontend::KeyLinkListPtr GpgFrontend::GpgKeyGetter::GetKeysCopy( // get the lock std::lock_guard<std::mutex> lock(ctx_mutex_); auto keys_copy = std::make_unique<GpgKeyLinkList>(); - for (const auto& key : *keys) keys_copy->push_back(key.Copy()); + for (const auto& key : *keys) keys_copy->emplace_back(key.Copy()); return keys_copy; } @@ -161,6 +166,18 @@ GpgFrontend::KeyListPtr GpgFrontend::GpgKeyGetter::GetKeysCopy( // get the lock std::lock_guard<std::mutex> lock(ctx_mutex_); auto keys_copy = std::make_unique<KeyArgsList>(); - for (const auto& key : *keys) keys_copy->push_back(key.Copy()); + for (const auto& key : *keys) keys_copy->emplace_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 |