qt: Add job for refreshing OpenPGP keys

* lang/qt/src/qgpgmerefreshopenpgpkeysjob.h,
lang/qt/src/qgpgmerefreshopenpgpkeysjob.cpp: New.
* lang/qt/src/Makefile.am: Add new files.
* lang/qt/src/protocol_p.h (Protocol::refreshKeysJob): Add support for
OpenPGP protocol.
--

The new job performs a --locate-external-keys for the email addresses
of all not revoked user IDs of the given keys, and it performs a
--recv-keys for the fingerprints of the given keys. The former may
import new keys from WKD or an LDAP server or via some other
(hopefully trusted) auto-key-locate mechanism.

GnuPG-bug-id: 5951
This commit is contained in:
Ingo Klöcker 2022-05-02 15:28:09 +02:00
parent 34786132fe
commit e12861f18c
5 changed files with 218 additions and 4 deletions

View File

@ -35,6 +35,7 @@ qgpgme_sources = \
qgpgmeimportjob.cpp qgpgmekeygenerationjob.cpp qgpgmekeylistjob.cpp \ qgpgmeimportjob.cpp qgpgmekeygenerationjob.cpp qgpgmekeylistjob.cpp \
qgpgmelistallkeysjob.cpp qgpgmenewcryptoconfig.cpp \ qgpgmelistallkeysjob.cpp qgpgmenewcryptoconfig.cpp \
qgpgmereceivekeysjob.cpp \ qgpgmereceivekeysjob.cpp \
qgpgmerefreshopenpgpkeysjob.cpp \
qgpgmerefreshsmimekeysjob.cpp \ qgpgmerefreshsmimekeysjob.cpp \
qgpgmerevokekeyjob.cpp \ qgpgmerevokekeyjob.cpp \
qgpgmesignencryptjob.cpp \ qgpgmesignencryptjob.cpp \
@ -161,6 +162,7 @@ private_qgpgme_headers = \
qgpgmekeylistjob.h \ qgpgmekeylistjob.h \
qgpgmelistallkeysjob.h \ qgpgmelistallkeysjob.h \
qgpgmereceivekeysjob.h \ qgpgmereceivekeysjob.h \
qgpgmerefreshopenpgpkeysjob.h \
qgpgmerefreshsmimekeysjob.h \ qgpgmerefreshsmimekeysjob.h \
qgpgmerevokekeyjob.h \ qgpgmerevokekeyjob.h \
qgpgmesignencryptjob.h \ qgpgmesignencryptjob.h \
@ -215,6 +217,7 @@ qgpgme_moc_sources = \
qgpgmekeylistjob.moc \ qgpgmekeylistjob.moc \
qgpgmelistallkeysjob.moc \ qgpgmelistallkeysjob.moc \
qgpgmereceivekeysjob.moc \ qgpgmereceivekeysjob.moc \
qgpgmerefreshopenpgpkeysjob.moc \
qgpgmerefreshsmimekeysjob.moc \ qgpgmerefreshsmimekeysjob.moc \
qgpgmerevokekeyjob.moc \ qgpgmerevokekeyjob.moc \
qgpgmesignencryptjob.moc \ qgpgmesignencryptjob.moc \

View File

@ -135,6 +135,14 @@ public:
virtual DeleteJob *deleteJob() const = 0; virtual DeleteJob *deleteJob() const = 0;
virtual SignEncryptJob *signEncryptJob(bool armor = false, bool textMode = false) const = 0; virtual SignEncryptJob *signEncryptJob(bool armor = false, bool textMode = false) const = 0;
virtual DecryptVerifyJob *decryptVerifyJob(bool textmode = false) const = 0; virtual DecryptVerifyJob *decryptVerifyJob(bool textmode = false) const = 0;
/**
* For S/MIME keys this job performs a full validation check of the keys
* with updated CRLs.
* For OpenPGP keys this job performs a refresh of keys via the external
* methods as defined by the \c auto-key-locate option and from the
* configured keyserver.
*/
virtual RefreshKeysJob *refreshKeysJob() const = 0; virtual RefreshKeysJob *refreshKeysJob() const = 0;
virtual ChangeExpiryJob *changeExpiryJob() const = 0; virtual ChangeExpiryJob *changeExpiryJob() const = 0;
virtual SignKeyJob *signKeyJob() const = 0; virtual SignKeyJob *signKeyJob() const = 0;

View File

@ -42,6 +42,7 @@
#include "qgpgmelistallkeysjob.h" #include "qgpgmelistallkeysjob.h"
#include "qgpgmedecryptjob.h" #include "qgpgmedecryptjob.h"
#include "qgpgmedecryptverifyjob.h" #include "qgpgmedecryptverifyjob.h"
#include "qgpgmerefreshopenpgpkeysjob.h"
#include "qgpgmerefreshsmimekeysjob.h" #include "qgpgmerefreshsmimekeysjob.h"
#include "qgpgmedeletejob.h" #include "qgpgmedeletejob.h"
#include "qgpgmedownloadjob.h" #include "qgpgmedownloadjob.h"
@ -283,12 +284,15 @@ public:
QGpgME::RefreshKeysJob *refreshKeysJob() const Q_DECL_OVERRIDE QGpgME::RefreshKeysJob *refreshKeysJob() const Q_DECL_OVERRIDE
{ {
if (mProtocol != GpgME::CMS) { // fixme: add support for gpg, too if (mProtocol == GpgME::CMS) {
return nullptr; return new QGpgME::QGpgMERefreshSMIMEKeysJob;
} }
// this operation is not supported by gpgme, so we have to call gpgsm ourselves: GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
return new QGpgME::QGpgMERefreshSMIMEKeysJob(); if (!context) {
return nullptr;
}
return new QGpgME::QGpgMERefreshOpenPGPKeysJob{context};
} }
QGpgME::DownloadJob *downloadJob(bool armor) const Q_DECL_OVERRIDE QGpgME::DownloadJob *downloadJob(bool armor) const Q_DECL_OVERRIDE

View File

@ -0,0 +1,132 @@
/*
qgpgmerefreshopenpgpkeysjob.cpp
This file is part of qgpgme, the Qt API binding for gpgme
Copyright (c) 2022 g10 Code GmbH
Software engineering by Ingo Klöcker <dev@ingo-kloecker.de>
QGpgME is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
QGpgME is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the Qt library by Trolltech AS, Norway (or with modified versions
of Qt that use the same license as Qt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
Qt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "qgpgmerefreshopenpgpkeysjob.h"
#include "qgpgmekeylistjob.h"
#include "qgpgmereceivekeysjob.h"
#include "util.h"
#include <context.h>
#include <key.h>
#include <memory>
#include "qgpgme_debug.h"
using namespace QGpgME;
using namespace GpgME;
QStringList toEmailAddresses(const std::vector<GpgME::Key> &keys)
{
const auto numUserIDs = std::accumulate(std::begin(keys), std::end(keys), 0, [](auto num, const auto &key) {
return num + key.numUserIDs();
});
QStringList emails;
emails.reserve(numUserIDs);
emails = std::accumulate(std::begin(keys), std::end(keys), emails, [](auto &emails, const auto &key) {
const auto userIDs = key.userIDs();
emails = std::accumulate(std::begin(userIDs), std::end(userIDs), emails, [](auto &emails, const auto &userID) {
if (!userID.isRevoked() && !userID.addrSpec().empty()) {
emails.push_back(QString::fromStdString(userID.addrSpec()));
}
return emails;
});
return emails;
});
return emails;
}
QGpgMERefreshOpenPGPKeysJob::QGpgMERefreshOpenPGPKeysJob(Context *context)
: mixin_type{context}
{
lateInitialization();
}
QGpgMERefreshOpenPGPKeysJob::~QGpgMERefreshOpenPGPKeysJob() = default;
static Error locate_external_keys(Context *ctx, const std::vector<Key> &keys)
{
Context::KeyListModeSaver saver{ctx};
ctx->setKeyListMode(GpgME::LocateExternal);
const auto emails = toEmailAddresses(keys);
std::vector<Key> dummy;
auto job = std::unique_ptr<KeyListJob>{new QGpgMEKeyListJob{ctx}};
const auto result = job->exec(emails, false, dummy);
job.release();
return result.error();
}
static Error receive_keys(Context *ctx, const std::vector<Key> &keys)
{
const auto fprs = toFingerprints(keys);
auto job = std::unique_ptr<ReceiveKeysJob>{new QGpgMEReceiveKeysJob{ctx}};
const auto result = job->exec(fprs);
job.release();
return result.error();
}
static QGpgMERefreshOpenPGPKeysJob::result_type refresh_keys(Context *ctx, const std::vector<Key> &keys)
{
Error err;
err = locate_external_keys(ctx, keys);
if (!err) {
err = receive_keys(ctx, keys);
}
return std::make_tuple(err, /*err ? WKDLookupResult{pattern, err} : result,*/ QString{}, Error{});
}
GpgME::Error QGpgMERefreshOpenPGPKeysJob::start(const QStringList &patterns)
{
Q_UNUSED(patterns);
return GpgME::Error::fromCode(GPG_ERR_NOT_IMPLEMENTED);
}
GpgME::Error QGpgMERefreshOpenPGPKeysJob::start(const std::vector<GpgME::Key> &keys)
{
run(std::bind(&refresh_keys, std::placeholders::_1, keys));
return Error{};
}
#include "qgpgmerefreshopenpgpkeysjob.moc"

View File

@ -0,0 +1,67 @@
/*
qgpgmerefreshopenpgpkeysjob.h
This file is part of qgpgme, the Qt API binding for gpgme
Copyright (c) 2022 g10 Code GmbH
Software engineering by Ingo Klöcker <dev@ingo-kloecker.de>
QGpgME is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
QGpgME is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the Qt library by Trolltech AS, Norway (or with modified versions
of Qt that use the same license as Qt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
Qt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
#ifndef __QGPGME_QGPGMEREFRESHOPENPGPKEYSJOB_H__
#define __QGPGME_QGPGMEREFRESHOPENPGPKEYSJOB_H__
#include "refreshkeysjob.h"
#include "threadedjobmixin.h"
namespace QGpgME
{
class QGpgMERefreshOpenPGPKeysJob
#ifdef Q_MOC_RUN
: public RefreshKeysJob
#else
: public _detail::ThreadedJobMixin<RefreshKeysJob>
#endif
{
Q_OBJECT
#ifdef Q_MOC_RUN
public Q_SLOTS:
void slotFinished();
#endif
public:
explicit QGpgMERefreshOpenPGPKeysJob(GpgME::Context *context);
~QGpgMERefreshOpenPGPKeysJob() override;
/** This overload is not implemented. Use the other overload. */
GpgME::Error start(const QStringList &patterns) override;
GpgME::Error start(const std::vector<GpgME::Key> &keys) override;
};
}
#endif // __QGPGME_QGPGMEREFRESHOPENPGPKEYSJOB_H__