diff --git a/src/utils/keyhelpers.cpp b/src/utils/keyhelpers.cpp index 4dc57e94a..8c564d71e 100644 --- a/src/utils/keyhelpers.cpp +++ b/src/utils/keyhelpers.cpp @@ -1,62 +1,68 @@ /* utils/keyhelpers.cpp This file is part of libkleopatra, the KDE keymanagement library SPDX-FileCopyrightText: 2022 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ #include #include "keyhelpers.h" #include #include using namespace Kleo; namespace { bool havePublicKeyForSignature(const GpgME::UserID::Signature &signature) { // GnuPG returns status "NoPublicKey" for missing signing keys, but also // for expired or revoked signing keys. return (signature.status() != GpgME::UserID::Signature::NoPublicKey) // || !KeyCache::instance()->findByKeyIDOrFingerprint(signature.signerKeyID()).isNull(); } auto _getMissingSignerKeyIds(const std::vector &signatures) { return std::accumulate(std::begin(signatures), std::end(signatures), std::set{}, [](auto &keyIds, const auto &signature) { if (!havePublicKeyForSignature(signature)) { keyIds.insert(QLatin1String{signature.signerKeyID()}); } return keyIds; }); } } std::set Kleo::getMissingSignerKeyIds(const std::vector &userIds) { return std::accumulate(std::begin(userIds), std::end(userIds), std::set{}, [](auto &keyIds, const auto &userID) { if (!userID.isBad()) { const auto newKeyIds = _getMissingSignerKeyIds(userID.signatures()); std::copy(std::begin(newKeyIds), std::end(newKeyIds), std::inserter(keyIds, std::end(keyIds))); } return keyIds; }); } std::set Kleo::getMissingSignerKeyIds(const std::vector &keys) { return std::accumulate(std::begin(keys), std::end(keys), std::set{}, [](auto &keyIds, const auto &key) { if (!key.isBad()) { const auto newKeyIds = getMissingSignerKeyIds(key.userIDs()); std::copy(std::begin(newKeyIds), std::end(newKeyIds), std::inserter(keyIds, std::end(keyIds))); } return keyIds; }); } + +bool Kleo::isRemoteKey(const GpgME::Key &key) +{ + // a remote key looked up via WKD has key list mode Local; therefore we also look for the key in the local key ring + return (key.keyListMode() == GpgME::Extern) || KeyCache::instance()->findByFingerprint(key.primaryFingerprint()).isNull(); +} diff --git a/src/utils/keyhelpers.h b/src/utils/keyhelpers.h index 17e9b183e..58a0c3152 100644 --- a/src/utils/keyhelpers.h +++ b/src/utils/keyhelpers.h @@ -1,47 +1,53 @@ /* utils/keyhelpers.h This file is part of libkleopatra, the KDE keymanagement library SPDX-FileCopyrightText: 2021-2022 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ #pragma once #include "kleo_export.h" #include #include #include #include namespace GpgME { class Key; class UserID; } namespace Kleo { template QStringList getFingerprints(const KeyContainer &keys) { QStringList fingerprints; fingerprints.reserve(keys.size()); std::transform(std::begin(keys), std::end(keys), std::back_inserter(fingerprints), [](const auto &key) { return QString::fromLatin1(key.primaryFingerprint()); }); return fingerprints; } KLEO_EXPORT std::set getMissingSignerKeyIds(const std::vector &userIds); KLEO_EXPORT std::set getMissingSignerKeyIds(const std::vector &keys); +/** + * Returns true, if the key \p key is the result of a lookup which is not present + * in the local key ring. + */ +KLEO_EXPORT bool isRemoteKey(const GpgME::Key &key); + }