Commit ec81b2b5 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

In mixed mode prefer recipient keys matching preferred protocol

* use OpenPGP as preferred protocol, unless a preferred protocol is
  specified or forced
* if in mixed mode for a recipient there are keys with different
  protocols, then propose the keys matching the preferred protocol in
  the approval dialog

GnuPG-bug-id: 5283
parent 5d64856b
Pipeline #54908 passed with stage
in 10 minutes and 54 seconds
......@@ -135,7 +135,7 @@ private Q_SLOTS:
}
}
void test_all_resolved_allow_mixed()
void test_all_resolved_allow_mixed_no_protocol_preference()
{
const QStringList unresolvedSenders;
const QStringList unresolvedRecipients;
......@@ -144,7 +144,9 @@ private Q_SLOTS:
GpgME::Protocol forcedProtocol = GpgME::UnknownProtocol;
GpgME::Protocol presetProtocol = GpgME::UnknownProtocol;
const auto resolvedSenders = resolved_senders_openpgp_and_smime();
const auto resolvedRecipients = resolved_recipients_openpgp_and_smime();
auto resolvedRecipients = resolved_recipients_openpgp_and_smime();
resolvedRecipients["prefer-smime@example.net"].push_back(createTestKey("OpenPGP <prefer-smime@example.net>", GpgME::OpenPGP));
resolvedRecipients.insert("smime-only@example.net", {createTestKey("S/MIME Only <smime-only@example.net>", GpgME::CMS)});
const auto dialog = std::make_unique<NewKeyApprovalDialog>(resolvedSenders,
resolvedRecipients,
unresolvedSenders,
......@@ -167,7 +169,7 @@ private Q_SLOTS:
QString::fromLatin1(resolvedSenders["sender@example.net"][1].primaryFingerprint()));
const QList<KeySelectionCombo *> encryptionKeyWidgets = dialog->findChildren<KeySelectionCombo *>(QStringLiteral("encryption key"));
QCOMPARE(encryptionKeyWidgets.size(), 4);
QCOMPARE(encryptionKeyWidgets.size(), 5);
for (auto widget : encryptionKeyWidgets) {
QVERIFY2(widget->isVisible(),
qPrintable(QString("encryption key widget should be visible for address %1").arg(widget->property("address").toString())));
......@@ -179,13 +181,16 @@ private Q_SLOTS:
QCOMPARE(encryptionKeyWidgets[1]->property("address").toString(), sender);
QCOMPARE(encryptionKeyWidgets[1]->defaultKey(GpgME::CMS),
QString::fromLatin1(resolvedRecipients["sender@example.net"][1].primaryFingerprint()));
// further encryption key widgets shall be widgets for keys of recipients
// further encryption key widgets shall be widgets for keys of recipients, where OpenPGP keys are preferred due to no specific preset
QCOMPARE(encryptionKeyWidgets[2]->property("address").toString(), QStringLiteral("prefer-openpgp@example.net"));
QCOMPARE(encryptionKeyWidgets[2]->defaultKey(),
QString::fromLatin1(resolvedRecipients["prefer-openpgp@example.net"][0].primaryFingerprint()));
QCOMPARE(encryptionKeyWidgets[3]->property("address").toString(), QStringLiteral("prefer-smime@example.net"));
QCOMPARE(encryptionKeyWidgets[3]->defaultKey(),
QString::fromLatin1(resolvedRecipients["prefer-smime@example.net"][0].primaryFingerprint()));
QString::fromLatin1(resolvedRecipients["prefer-smime@example.net"][1].primaryFingerprint()));
QCOMPARE(encryptionKeyWidgets[4]->property("address").toString(), QStringLiteral("smime-only@example.net"));
QCOMPARE(encryptionKeyWidgets[4]->defaultKey(),
QString::fromLatin1(resolvedRecipients["smime-only@example.net"][0].primaryFingerprint()));
}
private:
......@@ -202,8 +207,12 @@ private:
QMap<QString, std::vector<GpgME::Key> > resolved_recipients_openpgp_and_smime()
{
return {
{QStringLiteral("prefer-openpgp@example.net"), {createTestKey("Full Trust <prefer-openpgp@example.net>", GpgME::OpenPGP)}},
{QStringLiteral("prefer-smime@example.net"), {createTestKey("Trusted S/MIME <prefer-smime@example.net>", GpgME::CMS)}},
{QStringLiteral("prefer-openpgp@example.net"), {
createTestKey("Full Trust <prefer-openpgp@example.net>", GpgME::OpenPGP)
}},
{QStringLiteral("prefer-smime@example.net"), {
createTestKey("Trusted S/MIME <prefer-smime@example.net>", GpgME::CMS)
}},
{QStringLiteral("sender@example.net"), {
createTestKey("sender@example.net", GpgME::OpenPGP),
createTestKey("sender@example.net", GpgME::CMS)
......
......@@ -217,15 +217,20 @@ private:
IgnoreKey,
};
public:
Private(NewKeyApprovalDialog *pub,
Private(NewKeyApprovalDialog *qq,
GpgME::Protocol forcedProtocol,
GpgME::Protocol presetProtocol,
const QString &sender, bool allowMixed):
mProto(forcedProtocol),
mSender(sender),
mAllowMixed(allowMixed),
q(pub)
const QString &sender,
bool allowMixed)
: mForcedProtocol(forcedProtocol)
, mPreferredProtocol(presetProtocol)
, mSender(sender)
, mAllowMixed(allowMixed)
, q(qq)
{
Q_ASSERT(forcedProtocol == GpgME::UnknownProtocol || presetProtocol == GpgME::UnknownProtocol || presetProtocol == forcedProtocol);
Q_ASSERT(!allowMixed || (allowMixed && forcedProtocol == GpgME::UnknownProtocol));
// We do the translation here to avoid having the same string multiple times.
mGenerateTooltip = i18nc("@info:tooltip for a 'Generate new key pair' action "
"in a combobox when a user does not yet have an OpenPGP or S/MIME key.",
......@@ -267,21 +272,23 @@ public:
mMainLay->addLayout(fmtLayout);
// Handle force / preset
if (forcedProtocol == GpgME::OpenPGP) {
pgpBtn->setChecked(true);
pgpBtn->setVisible(false);
if (mForcedProtocol != GpgME::UnknownProtocol) {
mPreferredProtocol = mForcedProtocol;
}
if (mPreferredProtocol == GpgME::UnknownProtocol) {
mPreferredProtocol = GpgME::OpenPGP;
}
if (mAllowMixed) {
smimeBtn->setVisible(false);
} else if (forcedProtocol == GpgME::CMS) {
smimeBtn->setChecked(true);
pgpBtn->setVisible(false);
smimeBtn->setVisible(false);
} else if (presetProtocol == GpgME::CMS) {
smimeBtn->setChecked(true);
} else if (!mAllowMixed) {
pgpBtn->setChecked(true);
} else if (mAllowMixed) {
smimeBtn->setVisible(false);
} else if (mForcedProtocol != GpgME::UnknownProtocol) {
pgpBtn->setChecked(mForcedProtocol == GpgME::OpenPGP);
smimeBtn->setChecked(mForcedProtocol == GpgME::CMS);
pgpBtn->setVisible(false);
smimeBtn->setVisible(false);
} else {
pgpBtn->setChecked(mPreferredProtocol == GpgME::OpenPGP);
smimeBtn->setChecked(mPreferredProtocol == GpgME::CMS);
}
updateFilter();
......@@ -481,7 +488,7 @@ public:
if (!combo->keyFilter()) {
combo->setKeyFilter(mCurSigFilter);
}
if (key.isNull() && mProto != GpgME::CMS) {
if (key.isNull() && mForcedProtocol != GpgME::CMS) {
combo->appendCustomItem(QIcon::fromTheme(QStringLiteral("document-new")),
i18n("Generate a new key pair"), GenerateKey,
mGenerateTooltip);
......@@ -615,7 +622,15 @@ public:
ComboWidget* comboWidget = createEncryptionCombo(addr, GpgME::Key());
encGrid->addWidget(comboWidget, encGrid->rowCount(), 0, 1, 2);
} else {
for (const auto &key: keys) {
// in mixed mode prefer the keys with the preferred protocol for the other recipients
std::vector<GpgME::Key> preferredKeys;
if (mAllowMixed && addr != mSender) {
std::copy_if(keys.cbegin(), keys.cend(),
std::back_inserter(preferredKeys),
[this] (const auto &key) { return key.protocol() == mPreferredProtocol; });
}
const std::vector<GpgME::Key> &encryptionKeys = !preferredKeys.empty() ? preferredKeys : keys;
for (const auto &key: encryptionKeys) {
ComboWidget* comboWidget = createEncryptionCombo(addr, key);
if (keys.size() > 1) {
comboWidget->setFromOverride(key.protocol());
......@@ -631,6 +646,7 @@ public:
if (resolved.empty() && unresolved.empty()) {
return;
}
{
auto group = new QGroupBox(i18nc("Encrypt to self (email address):", "Encrypt to self (%1):", mSender));
group->setAlignment(Qt::AlignLeft);
......@@ -762,7 +778,8 @@ public:
}
}
GpgME::Protocol mProto;
GpgME::Protocol mForcedProtocol;
GpgME::Protocol mPreferredProtocol;
QList<KeySelectionCombo *> mSigningCombos;
QList<KeySelectionCombo *> mEncCombos;
QList<KeySelectionCombo *> mAllCombos;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment