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

In mixed mode, use fixed protocols for the sender's keys

In mixed mode the sender may need to specify OpenPGP keys and S/MIME keys
for signing and for encryption-to-self. Use fixed protocol filters for
the corresponding key selectors.

GnuPG-bug-id: 5283
parent 1aa4ab48
......@@ -154,34 +154,38 @@ private Q_SLOTS:
forcedProtocol,
presetProtocol);
dialog->show();
const QList<KeySelectionCombo *> signingKeyWidgets = dialog->findChildren<KeySelectionCombo *>(QStringLiteral("signing key"));
QCOMPARE(signingKeyWidgets.size(), 2);
for (auto widget : signingKeyWidgets) {
QVERIFY2(widget->isVisible(), "signing key widget should be visible");
}
// one signing key widget should default to sender's OpenPGP key, the other to sender's S/MIME key
const std::set<QString> signingKeyFingerprints = {
QString::fromLatin1(resolvedSenders["sender@example.net"][0].primaryFingerprint()),
QString::fromLatin1(resolvedSenders["sender@example.net"][1].primaryFingerprint()),
};
const std::set<QString> defaultKeys = {
signingKeyWidgets[0]->defaultKey(),
signingKeyWidgets[1]->defaultKey()
};
QCOMPARE(defaultKeys, signingKeyFingerprints);
// first signing key widget should default to sender's OpenPGP key, the other to sender's S/MIME key
QCOMPARE(signingKeyWidgets[0]->defaultKey(GpgME::OpenPGP),
QString::fromLatin1(resolvedSenders["sender@example.net"][0].primaryFingerprint()));
QCOMPARE(signingKeyWidgets[1]->defaultKey(GpgME::CMS),
QString::fromLatin1(resolvedSenders["sender@example.net"][1].primaryFingerprint()));
const QList<KeySelectionCombo *> encryptionKeyWidgets = dialog->findChildren<KeySelectionCombo *>(QStringLiteral("encryption key"));
QCOMPARE(encryptionKeyWidgets.size(), 4);
for (auto widget : encryptionKeyWidgets) {
QVERIFY2(widget->isVisible(),
qPrintable(QString("encryption key widget should be visible for address %1").arg(widget->property("address").toString())));
}
// encryption key widgets for sender's keys shall be first two widgets
// first two encryption key widgets shall be widgets for sender's keys
QCOMPARE(encryptionKeyWidgets[0]->property("address").toString(), sender);
QCOMPARE(encryptionKeyWidgets[0]->defaultKey(),
QCOMPARE(encryptionKeyWidgets[0]->defaultKey(GpgME::OpenPGP),
QString::fromLatin1(resolvedRecipients["sender@example.net"][0].primaryFingerprint()));
QCOMPARE(encryptionKeyWidgets[1]->property("address").toString(), sender);
QCOMPARE(encryptionKeyWidgets[1]->defaultKey(),
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
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()));
}
private:
......
......@@ -164,11 +164,22 @@ public:
mFromOverride = proto;
}
GpgME::Protocol fixedProtocol() const
{
return mFixedProtocol;
}
void setFixedProtocol(GpgME::Protocol proto)
{
mFixedProtocol = proto;
}
private:
KeySelectionCombo *mCombo;
QPushButton *mFilterBtn;
QString mLastIdFilter;
GpgME::Protocol mFromOverride;
GpgME::Protocol mFixedProtocol = GpgME::UnknownProtocol;
};
static enum GpgME::UserID::Validity keyValidity(const GpgME::Key &key)
......@@ -420,46 +431,55 @@ public:
mCurSigFilter = s_signFilter;
}
for (auto combo: qAsConst(mSigningCombos)) {
combo->setKeyFilter(mCurSigFilter);
auto widget = qobject_cast <ComboWidget *>(combo->parentWidget());
auto widget = qobject_cast<ComboWidget *>(combo->parentWidget());
if (!widget) {
qCDebug(LIBKLEO_LOG) << "Failed to find signature combo widget";
continue;
}
if (widget->fixedProtocol() == GpgME::UnknownProtocol) {
combo->setKeyFilter(mCurSigFilter);
}
widget->setVisible(mAllowMixed ||
widget->fromOverride() == GpgME::UnknownProtocol ||
((isSMIME && widget->fromOverride() == GpgME::CMS) ||
(isPGP && widget->fromOverride() == GpgME::OpenPGP)));
(isSMIME && widget->fromOverride() == GpgME::CMS) ||
(isPGP && widget->fromOverride() == GpgME::OpenPGP));
}
for (auto combo: qAsConst(mEncCombos)) {
combo->setKeyFilter(mCurEncFilter);
auto widget = qobject_cast <ComboWidget *>(combo->parentWidget());
auto widget = qobject_cast<ComboWidget *>(combo->parentWidget());
if (!widget) {
qCDebug(LIBKLEO_LOG) << "Failed to find combo widget";
continue;
}
if (widget->fixedProtocol() == GpgME::UnknownProtocol) {
combo->setKeyFilter(mCurEncFilter);
}
widget->setVisible(mAllowMixed ||
widget->fromOverride() == GpgME::UnknownProtocol ||
((isSMIME && widget->fromOverride() == GpgME::CMS) ||
(isPGP && widget->fromOverride() == GpgME::OpenPGP)));
(isSMIME && widget->fromOverride() == GpgME::CMS) ||
(isPGP && widget->fromOverride() == GpgME::OpenPGP));
}
}
ComboWidget *createSigningCombo(const QString &addr, const GpgME::Key &key)
{
auto combo = new KeySelectionCombo();
auto comboWidget = new ComboWidget(combo);
#ifndef NDEBUG
combo->setObjectName(QStringLiteral("signing key"));
#endif
combo->setKeyFilter(mCurSigFilter);
if (!key.isNull()) {
if (mAllowMixed) {
// do not set the key for a specific protocol if mixed protocols are allowed
combo->setDefaultKey(QString::fromLatin1(key.primaryFingerprint()));
} else {
combo->setDefaultKey(QString::fromLatin1(key.primaryFingerprint()), key.protocol());
if (key.protocol() == GpgME::OpenPGP) {
combo->setKeyFilter(s_pgpSignFilter);
} else if (key.protocol() == GpgME::CMS) {
combo->setKeyFilter(s_smimeSignFilter);
}
comboWidget->setFixedProtocol(key.protocol());
}
combo->setDefaultKey(QString::fromLatin1(key.primaryFingerprint()), key.protocol());
}
if (!combo->keyFilter()) {
combo->setKeyFilter(mCurSigFilter);
}
if (key.isNull() && mProto != GpgME::CMS) {
combo->appendCustomItem(QIcon::fromTheme(QStringLiteral("document-new")),
......@@ -482,7 +502,7 @@ public:
updateOkButton();
});
return new ComboWidget(combo);
return comboWidget;
}
void addSigningKeys(const QMap<QString, std::vector<GpgME::Key> > &resolved,
......@@ -529,19 +549,32 @@ public:
ComboWidget *createEncryptionCombo(const QString &addr, const GpgME::Key &key)
{
auto combo = new KeySelectionCombo(false);
auto comboWidget = new ComboWidget(combo);
#ifndef NDEBUG
combo->setObjectName(QStringLiteral("encryption key"));
#endif
combo->setKeyFilter(mCurEncFilter);
if (!key.isNull()) {
if (mAllowMixed) {
if (mAllowMixed && addr == mSender) {
if (key.protocol() == GpgME::OpenPGP) {
combo->setKeyFilter(s_pgpFilter);
} else if (key.protocol() == GpgME::CMS) {
combo->setKeyFilter(s_smimeFilter);
}
comboWidget->setFixedProtocol(key.protocol());
}
if (mAllowMixed && addr != mSender) {
// do not set the key for a specific protocol if mixed protocols are allowed
combo->setDefaultKey(QString::fromLatin1(key.primaryFingerprint()));
} else {
}
else {
combo->setDefaultKey(QString::fromLatin1(key.primaryFingerprint()), key.protocol());
}
}
if (!combo->keyFilter()) {
combo->setKeyFilter(mCurSigFilter);
}
if (mSender == addr && key.isNull()) {
combo->appendCustomItem(QIcon::fromTheme(QStringLiteral("document-new")),
i18n("Generate a new key pair"), GenerateKey,
......@@ -569,7 +602,7 @@ public:
mEncCombos << combo;
mAllCombos << combo;
combo->setProperty("address", addr);
return new ComboWidget(combo);
return comboWidget;
}
void addEncryptionAddr(const QString &addr, const std::vector<GpgME::Key> &keys,
......
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