11#include <config-libkleo.h>
13#include "keyhelpers.h"
15#include <libkleo/algorithm.h>
16#include <libkleo/compat.h>
17#include <libkleo/keycache.h>
19#include <libkleo_debug.h>
33bool havePublicKeyForSignature(
const GpgME::UserID::Signature &signature)
37 return (signature.status() != GpgME::UserID::Signature::NoPublicKey)
38 || !KeyCache::instance()->findByKeyIDOrFingerprint(signature.signerKeyID()).isNull();
41auto _getMissingSignerKeyIds(
const std::vector<GpgME::UserID::Signature> &signatures)
43 return std::accumulate(std::begin(signatures), std::end(signatures), std::set<QString>{}, [](
auto &keyIds,
const auto &signature) {
44 if (!havePublicKeyForSignature(signature)) {
52std::set<QString> Kleo::getMissingSignerKeyIds(
const std::vector<GpgME::UserID> &userIds)
54 return std::accumulate(std::begin(userIds), std::end(userIds), std::set<QString>{}, [](
auto &keyIds,
const auto &userID) {
55 if (!userID.isBad()) {
56 const auto newKeyIds = _getMissingSignerKeyIds(userID.signatures());
57 std::copy(std::begin(newKeyIds), std::end(newKeyIds), std::inserter(keyIds, std::end(keyIds)));
63std::set<QString> Kleo::getMissingSignerKeyIds(
const std::vector<GpgME::Key> &keys)
65 return std::accumulate(std::begin(keys), std::end(keys), std::set<QString>{}, [](
auto &keyIds,
const auto &key) {
67 const auto newKeyIds = getMissingSignerKeyIds(key.userIDs());
68 std::copy(std::begin(newKeyIds), std::end(newKeyIds), std::inserter(keyIds, std::end(keyIds)));
74bool Kleo::isRemoteKey(
const GpgME::Key &key)
77 return (key.keyListMode() == GpgME::Extern) || KeyCache::instance()->findByFingerprint(key.primaryFingerprint()).isNull();
80GpgME::UserID::Validity Kleo::minimalValidityOfNotRevokedUserIDs(
const Key &key)
82 const std::vector<UserID> userIDs = key.userIDs();
83 const int minValidity = std::accumulate(userIDs.begin(), userIDs.end(), UserID::Ultimate + 1, [](
int validity,
const UserID &userID) {
84 return userID.isRevoked() ? validity : std::min(validity, static_cast<int>(userID.validity()));
86 return minValidity <= UserID::Ultimate ? static_cast<UserID::Validity>(minValidity) : UserID::
Unknown;
89GpgME::UserID::Validity Kleo::maximalValidityOfUserIDs(
const Key &key)
91 const auto userIDs = key.userIDs();
92 const int maxValidity = std::accumulate(userIDs.begin(), userIDs.end(), 0, [](
int validity,
const UserID &userID) {
93 return std::max(validity, static_cast<int>(userID.validity()));
95 return static_cast<UserID::Validity
>(maxValidity);
98bool Kleo::allUserIDsHaveFullValidity(
const GpgME::Key &key)
100 return minimalValidityOfNotRevokedUserIDs(key) >= UserID::Full;
105bool isLastValidUserID(
const GpgME::UserID &userId)
107 if (Kleo::isRevokedOrExpired(userId)) {
110 const auto userIds = userId.parent().userIDs();
111 const int numberOfValidUserIds = std::count_if(std::begin(userIds), std::end(userIds), [](
const auto &u) {
112 return !Kleo::isRevokedOrExpired(u);
114 return numberOfValidUserIds == 1;
117bool hasValidUserID(
const GpgME::Key &key)
119 return Kleo::any_of(key.userIDs(), [](
const auto &u) {
120 return !Kleo::isRevokedOrExpired(u);
125bool Kleo::isSelfSignature(
const GpgME::UserID::Signature &signature)
127 return !qstrcmp(signature.parent().parent().keyID(), signature.signerKeyID());
130bool Kleo::isRevokedOrExpired(
const GpgME::UserID &userId)
132 if (userId.isRevoked() || userId.parent().isExpired()) {
135 const auto sigs = userId.signatures();
136 std::vector<GpgME::UserID::Signature> selfSigs;
137 std::copy_if(std::begin(sigs), std::end(sigs), std::back_inserter(selfSigs), &Kleo::isSelfSignature);
138 std::sort(std::begin(selfSigs), std::end(selfSigs));
140 const auto sig = !selfSigs.empty() ? selfSigs.back() : GpgME::UserID::Signature{};
141 return !sig.isNull() && (sig.isRevokation() || sig.isExpired());
144bool Kleo::isExpired(
const UserID &userID)
146 if (userID.parent().isExpired()) {
149 const auto sigs = userID.signatures();
150 std::vector<GpgME::UserID::Signature> selfSigs;
151 std::copy_if(std::begin(sigs), std::end(sigs), std::back_inserter(selfSigs), &Kleo::isSelfSignature);
152 std::sort(std::begin(selfSigs), std::end(selfSigs));
154 const auto sig = !selfSigs.empty() ? selfSigs.back() : GpgME::UserID::Signature{};
155 return !sig.isNull() && sig.isExpired();
158bool Kleo::canCreateCertifications(
const GpgME::Key &key)
160 return Kleo::keyHasCertify(key) && canBeUsedForSecretKeyOperations(key);
163bool Kleo::canBeCertified(
const GpgME::Key &key)
165 return key.protocol() == GpgME::OpenPGP
167 && hasValidUserID(key);
172static inline bool subkeyHasSecret(
const GpgME::Subkey &subkey)
174#if GPGME_VERSION_NUMBER >= 0x011102
176 return subkey.isSecret();
179 return subkey.isSecret() || subkey.isCardKey();
184bool Kleo::canBeUsedForEncryption(
const GpgME::Key &key)
186 return !key.isBad() && Kleo::any_of(key.subkeys(), [](
const auto &subkey) {
187 return subkey.canEncrypt() && !subkey.isBad();
191bool Kleo::canBeUsedForSigning(
const GpgME::Key &key)
193 return !key.isBad() && Kleo::any_of(key.subkeys(), [](
const auto &subkey) {
194 return subkey.canSign() && !subkey.isBad() && subkeyHasSecret(subkey);
198bool Kleo::canBeUsedForSecretKeyOperations(
const GpgME::Key &key)
200 return subkeyHasSecret(key.subkey(0));
203bool Kleo::canRevokeUserID(
const GpgME::UserID &userId)
205 return (!userId.isNull()
206 && userId.parent().protocol() == GpgME::OpenPGP
207 && !isLastValidUserID(userId));
210bool Kleo::isSecretKeyStoredInKeyRing(
const GpgME::Key &key)
212 return key.subkey(0).isSecret() && !key.subkey(0).isCardKey();
215bool Kleo::userHasCertificationKey()
217 const auto secretKeys = KeyCache::instance()->secretKeys();
218 return Kleo::any_of(secretKeys, [](
const auto &k) {
219 return (k.protocol() == GpgME::OpenPGP) && canCreateCertifications(k);
223Kleo::CertificationRevocationFeasibility Kleo::userCanRevokeCertification(
const GpgME::UserID::Signature &certification)
225 const auto certificationKey = KeyCache::instance()->findByKeyIDOrFingerprint(certification.signerKeyID());
226 const bool isSelfSignature = qstrcmp(certification.parent().parent().keyID(), certification.signerKeyID()) == 0;
227 if (!certificationKey.hasSecret()) {
228 return CertificationNotMadeWithOwnKey;
229 }
else if (isSelfSignature) {
230 return CertificationIsSelfSignature;
231 }
else if (certification.isRevokation()) {
232 return CertificationIsRevocation;
233 }
else if (certification.isExpired()) {
234 return CertificationIsExpired;
235 }
else if (certification.isInvalid()) {
236 return CertificationIsInvalid;
237 }
else if (!canCreateCertifications(certificationKey)) {
238 return CertificationKeyNotAvailable;
240 return CertificationCanBeRevoked;
243bool Kleo::userCanRevokeCertifications(
const GpgME::UserID &userId)
245 if (userId.numSignatures() == 0) {
246 qCWarning(LIBKLEO_LOG) << __func__ <<
"- Error: Signatures of user ID" <<
QString::fromUtf8(userId.id()) <<
"not available";
248 return Kleo::any_of(userId.signatures(), [](
const auto &certification) {
249 return userCanRevokeCertification(certification) == CertificationCanBeRevoked;
253bool Kleo::userIDBelongsToKey(
const GpgME::UserID &userID,
const GpgME::Key &key)
255 return !qstricmp(userID.parent().primaryFingerprint(), key.primaryFingerprint());
258static time_t creationDate(
const GpgME::UserID &uid)
261 for (
unsigned int i = 0, numSignatures = uid.numSignatures(); i < numSignatures; ++i) {
262 const auto sig = uid.signature(i);
263 if (Kleo::isSelfSignature(sig)) {
264 return sig.creationTime();
270bool Kleo::userIDsAreEqual(
const GpgME::UserID &lhs,
const GpgME::UserID &rhs)
272 return (qstrcmp(lhs.parent().primaryFingerprint(), rhs.parent().primaryFingerprint()) == 0
273 && qstrcmp(lhs.id(), rhs.id()) == 0
274 && creationDate(lhs) == creationDate(rhs));
277static inline bool isOpenPGPCertification(
const GpgME::UserID::Signature &sig)
280 return (sig.certClass() & ~0x03) == 0x10;
283static bool isOpenPGPCertificationByUser(
const GpgME::UserID::Signature &sig)
285 if (!isOpenPGPCertification(sig)) {
288 const auto certificationKey = KeyCache::instance()->findByKeyIDOrFingerprint(sig.signerKeyID());
289 return certificationKey.ownerTrust() == Key::Ultimate;
292bool Kleo::userIDIsCertifiedByUser(
const GpgME::UserID &userId)
294 if (userId.parent().protocol() != GpgME::OpenPGP) {
295 qCWarning(LIBKLEO_LOG) << __func__ <<
"not called with OpenPGP key";
298 if (userId.numSignatures() == 0) {
299 qCWarning(LIBKLEO_LOG) << __func__ <<
"- Error: Signatures of user ID" <<
QString::fromUtf8(userId.id()) <<
"not available";
301 for (
unsigned int i = 0, numSignatures = userId.numSignatures(); i < numSignatures; ++i) {
302 const auto sig = userId.signature(i);
303 if ((sig.status() == UserID::Signature::NoError) && !sig.isBad() && sig.isExportable() && isOpenPGPCertificationByUser(sig)) {
QString fromUtf8(QByteArrayView str)