13#include <config-libkleo.h>
15#include "defaultkeyfilter.h"
16#include "utils/compliance.h"
18#include <libkleo/compliance.h>
19#include <libkleo/formatting.h>
20#include <libkleo/keyhelpers.h>
27static bool is_card_key(
const Key &key)
29 const std::vector<Subkey> sks = key.subkeys();
30 return std::find_if(sks.begin(), sks.end(), std::mem_fn(&Subkey::isCardKey)) != sks.end();
33class DefaultKeyFilter::Private
46 MatchContexts mMatchContexts = AnyMatchContext;
47 unsigned int mSpecificity = 0;
50 bool mStrikeOut =
false;
51 bool mUseFullFont =
false;
54 TriState mRevoked = DoesNotMatter;
55 TriState mExpired = DoesNotMatter;
56 TriState mInvalid = DoesNotMatter;
57 TriState mDisabled = DoesNotMatter;
58 TriState mRoot = DoesNotMatter;
59 TriState mCanEncrypt = DoesNotMatter;
60 TriState mCanSign = DoesNotMatter;
61 TriState mCanCertify = DoesNotMatter;
62 TriState mCanAuthenticate = DoesNotMatter;
63 TriState mHasEncrypt = DoesNotMatter;
64 TriState mHasSign = DoesNotMatter;
65 TriState mHasCertify = DoesNotMatter;
66 TriState mHasAuthenticate = DoesNotMatter;
67 TriState mQualified = DoesNotMatter;
68 TriState mCardKey = DoesNotMatter;
69 TriState mHasSecret = DoesNotMatter;
70 TriState mIsOpenPGP = DoesNotMatter;
71 TriState mWasValidated = DoesNotMatter;
72 TriState mIsDeVs = DoesNotMatter;
73 TriState mBad = DoesNotMatter;
74 TriState mValidIfSMIME = DoesNotMatter;
76 LevelState mOwnerTrust = LevelDoesNotMatter;
77 GpgME::Key::OwnerTrust mOwnerTrustReferenceLevel = Key::OwnerTrust::Unknown;
78 LevelState mValidity = LevelDoesNotMatter;
79 GpgME::UserID::Validity mValidityReferenceLevel = UserID::Validity::Unknown;
82DefaultKeyFilter::DefaultKeyFilter()
88DefaultKeyFilter::~DefaultKeyFilter() =
default;
90bool DefaultKeyFilter::matches(
const Key &key, MatchContexts contexts)
const
92 if (!(d->mMatchContexts & contexts)) {
98#define MATCH(member, method) \
100 if (member != DoesNotMatter && key.method() != bool(member == Set)) { \
104#define IS_MATCH(what) MATCH(d->m##what, is##what)
105#define CAN_MATCH(what) MATCH(d->mCan##what, can##what)
106#define HAS_MATCH(what) MATCH(d->mHas##what, has##what)
115 CAN_MATCH(Authenticate);
119 HAS_MATCH(Authenticate);
121 if (d->mCardKey != DoesNotMatter) {
122 if ((d->mCardKey == Set && !is_card_key(key)) || (d->mCardKey == NotSet && is_card_key(key))) {
126 MATCH(d->mHasSecret, hasSecret);
128 if (d->mIsOpenPGP != DoesNotMatter &&
bool(key.protocol() == GpgME::OpenPGP) !=
bool(d->mIsOpenPGP == Set)) {
131 if (d->mWasValidated != DoesNotMatter &&
bool(key.keyListMode() & GpgME::Validate) !=
bool(d->mWasValidated == Set)) {
134 if (d->mIsDeVs != DoesNotMatter &&
bool(DeVSCompliance::keyIsCompliant(key)) !=
bool(d->mIsDeVs == Set)) {
137 if (d->mBad != DoesNotMatter &&
139 bool(key.isNull() || key.isRevoked() || key.isExpired() || key.isDisabled() || key.isInvalid()) !=
bool(d->mBad == Set)) {
142 const UserID uid = key.userID(0);
143 if ((key.protocol() == GpgME::CMS)
144 && (d->mValidIfSMIME != DoesNotMatter)
145 && (
bool(uid.validity() >= UserID::Full) !=
bool(d->mValidIfSMIME == Set))) {
148 switch (d->mOwnerTrust) {
150 case LevelDoesNotMatter:
153 if (key.ownerTrust() != d->mOwnerTrustReferenceLevel) {
158 if (key.ownerTrust() == d->mOwnerTrustReferenceLevel) {
163 if (
static_cast<int>(key.ownerTrust()) <
static_cast<int>(d->mOwnerTrustReferenceLevel)) {
168 if (
static_cast<int>(key.ownerTrust()) >
static_cast<int>(d->mOwnerTrustReferenceLevel)) {
173 switch (d->mValidity) {
175 case LevelDoesNotMatter:
178 if (uid.validity() != d->mValidityReferenceLevel) {
183 if (uid.validity() == d->mValidityReferenceLevel) {
188 if (
static_cast<int>(uid.validity()) <
static_cast<int>(d->mValidityReferenceLevel)) {
193 if (
static_cast<int>(uid.validity()) >
static_cast<int>(d->mValidityReferenceLevel)) {
201bool DefaultKeyFilter::matches(
const UserID &userID, MatchContexts contexts)
const
203 if (!(d->mMatchContexts & contexts)) {
209#define MATCH_KEY(member, method) \
211 if (member != DoesNotMatter && userID.parent().method() != bool(member == Set)) { \
215#define IS_MATCH_KEY(what) MATCH_KEY(d->m##what, is##what)
216#define CAN_MATCH_KEY(what) MATCH_KEY(d->mCan##what, can##what)
217#define HAS_MATCH_KEY(what) MATCH_KEY(d->mHas##what, has##what)
222#define MATCH(member, method) \
224 if (member != DoesNotMatter && (userID.parent().method() != bool(member == Set) || userID.method() != bool(member == Set))) { \
228#define IS_MATCH(what) MATCH(d->m##what, is##what)
230 IS_MATCH_KEY(Expired);
232 if (d->mExpired != DoesNotMatter && (userID.parent().isExpired() !=
bool(d->mExpired == Set) || isExpired(userID) !=
bool(d->mExpired == Set))) {
236 IS_MATCH_KEY(Disabled);
238 CAN_MATCH_KEY(Encrypt);
240 CAN_MATCH_KEY(Certify);
241 CAN_MATCH_KEY(Authenticate);
242 HAS_MATCH_KEY(Encrypt);
244 HAS_MATCH_KEY(Certify);
245 HAS_MATCH_KEY(Authenticate);
246 IS_MATCH_KEY(Qualified);
247 if (d->mCardKey != DoesNotMatter) {
248 if ((d->mCardKey == Set && !is_card_key(userID.parent())) || (d->mCardKey == NotSet && is_card_key(userID.parent()))) {
252 MATCH_KEY(d->mHasSecret, hasSecret);
254 if (d->mIsOpenPGP != DoesNotMatter &&
bool(userID.parent().protocol() == GpgME::OpenPGP) !=
bool(d->mIsOpenPGP == Set)) {
257 if (d->mWasValidated != DoesNotMatter &&
bool(userID.parent().keyListMode() & GpgME::Validate) !=
bool(d->mWasValidated == Set)) {
260 if (d->mIsDeVs != DoesNotMatter &&
bool(DeVSCompliance::userIDIsCompliant(userID)) !=
bool(d->mIsDeVs == Set)) {
263 if (d->mBad != DoesNotMatter &&
265 bool(userID.parent().isNull() || userID.isNull() || userID.parent().isRevoked() || userID.isRevoked() || userID.parent().isExpired()
266 || userID.parent().isDisabled() || userID.parent().isInvalid() || userID.isInvalid())
267 !=
bool(d->mBad == Set)) {
270 if ((userID.parent().protocol() == GpgME::CMS)
271 && (d->mValidIfSMIME != DoesNotMatter)
272 && (
bool(userID.validity() >= UserID::Full) !=
bool(d->mValidIfSMIME == Set))) {
275 switch (d->mOwnerTrust) {
277 case LevelDoesNotMatter:
280 if (userID.parent().ownerTrust() != d->mOwnerTrustReferenceLevel) {
285 if (userID.parent().ownerTrust() == d->mOwnerTrustReferenceLevel) {
290 if (
static_cast<int>(userID.parent().ownerTrust()) <
static_cast<int>(d->mOwnerTrustReferenceLevel)) {
295 if (
static_cast<int>(userID.parent().ownerTrust()) >
static_cast<int>(d->mOwnerTrustReferenceLevel)) {
300 switch (d->mValidity) {
302 case LevelDoesNotMatter:
305 if (userID.validity() != d->mValidityReferenceLevel) {
310 if (userID.validity() == d->mValidityReferenceLevel) {
315 if (
static_cast<int>(userID.validity()) <
static_cast<int>(d->mValidityReferenceLevel)) {
320 if (
static_cast<int>(userID.validity()) >
static_cast<int>(d->mValidityReferenceLevel)) {
328KeyFilter::FontDescription DefaultKeyFilter::fontDescription()
const
330 if (d->mUseFullFont) {
331 return FontDescription::create(font(), bold(), italic(), strikeOut());
333 return FontDescription::create(bold(), italic(), strikeOut());
337void DefaultKeyFilter::setFgColor(
const QColor &value)
342void DefaultKeyFilter::setBgColor(
const QColor &value)
347void DefaultKeyFilter::setName(
const QString &value)
352void DefaultKeyFilter::setIcon(
const QString &value)
357void DefaultKeyFilter::setId(
const QString &value)
362void DefaultKeyFilter::setMatchContexts(MatchContexts value)
364 d->mMatchContexts = value;
367void DefaultKeyFilter::setSpecificity(
unsigned int value)
369 d->mSpecificity = value;
372void DefaultKeyFilter::setItalic(
bool value)
377void DefaultKeyFilter::setBold(
bool value)
382void DefaultKeyFilter::setStrikeOut(
bool value)
384 d->mStrikeOut = value;
387void DefaultKeyFilter::setUseFullFont(
bool value)
389 d->mUseFullFont = value;
392void DefaultKeyFilter::setFont(
const QFont &value)
414 d->mDisabled = value;
424 d->mCanEncrypt = value;
434 d->mCanCertify = value;
439 d->mCanAuthenticate = value;
444 d->mHasEncrypt = value;
454 d->mHasCertify = value;
459 d->mHasAuthenticate = value;
464 d->mQualified = value;
474 d->mHasSecret = value;
479 d->mIsOpenPGP = value;
484 d->mWasValidated = value;
489 d->mOwnerTrust = value;
492void DefaultKeyFilter::setOwnerTrustReferenceLevel(GpgME::Key::OwnerTrust value)
494 d->mOwnerTrustReferenceLevel = value;
499 d->mValidity = value;
502void DefaultKeyFilter::setValidityReferenceLevel(GpgME::UserID::Validity value)
504 d->mValidityReferenceLevel = value;
519 d->mValidIfSMIME = value;
522QColor DefaultKeyFilter::fgColor()
const
527QColor DefaultKeyFilter::bgColor()
const
532QString DefaultKeyFilter::name()
const
537QString DefaultKeyFilter::icon()
const
542QString DefaultKeyFilter::id()
const
547QFont DefaultKeyFilter::font()
const
554 return d->mMatchContexts;
557unsigned int DefaultKeyFilter::specificity()
const
559 return d->mSpecificity;
562bool DefaultKeyFilter::italic()
const
567bool DefaultKeyFilter::bold()
const
572bool DefaultKeyFilter::strikeOut()
const
574 return d->mStrikeOut;
577bool DefaultKeyFilter::useFullFont()
const
579 return d->mUseFullFont;
609 return d->mCanEncrypt;
619 return d->mCanCertify;
624 return d->mCanAuthenticate;
629 return d->mHasEncrypt;
639 return d->mHasCertify;
644 return d->mHasAuthenticate;
649 return d->mQualified;
659 return d->mHasSecret;
664 return d->mIsOpenPGP;
669 return d->mWasValidated;
674 return d->mOwnerTrust;
677GpgME::Key::OwnerTrust DefaultKeyFilter::ownerTrustReferenceLevel()
const
679 return d->mOwnerTrustReferenceLevel;
687GpgME::UserID::Validity DefaultKeyFilter::validityReferenceLevel()
const
689 return d->mValidityReferenceLevel;
704 return d->mValidIfSMIME;
707QString DefaultKeyFilter::description()
const
709 return d->mDescription;
712void DefaultKeyFilter::setDescription(
const QString &description)
714 d->mDescription = description;
TriState
Used for bool checks.
LevelState
Used for level checks.
void setValidIfSMIME(TriState value)
If value is Set, then invalid S/MIME certificates do not match.
An abstract base class key filters.