From e12861f18c6b431b40bfa78eb6f1d149690a5fcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingo=20Kl=C3=B6cker?= Date: Mon, 2 May 2022 15:28:09 +0200 Subject: [PATCH] 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 --- lang/qt/src/Makefile.am | 3 + lang/qt/src/protocol.h | 8 ++ lang/qt/src/protocol_p.h | 12 +- lang/qt/src/qgpgmerefreshopenpgpkeysjob.cpp | 132 ++++++++++++++++++++ lang/qt/src/qgpgmerefreshopenpgpkeysjob.h | 67 ++++++++++ 5 files changed, 218 insertions(+), 4 deletions(-) create mode 100644 lang/qt/src/qgpgmerefreshopenpgpkeysjob.cpp create mode 100644 lang/qt/src/qgpgmerefreshopenpgpkeysjob.h diff --git a/lang/qt/src/Makefile.am b/lang/qt/src/Makefile.am index e20d239d..18456d54 100644 --- a/lang/qt/src/Makefile.am +++ b/lang/qt/src/Makefile.am @@ -35,6 +35,7 @@ qgpgme_sources = \ qgpgmeimportjob.cpp qgpgmekeygenerationjob.cpp qgpgmekeylistjob.cpp \ qgpgmelistallkeysjob.cpp qgpgmenewcryptoconfig.cpp \ qgpgmereceivekeysjob.cpp \ + qgpgmerefreshopenpgpkeysjob.cpp \ qgpgmerefreshsmimekeysjob.cpp \ qgpgmerevokekeyjob.cpp \ qgpgmesignencryptjob.cpp \ @@ -161,6 +162,7 @@ private_qgpgme_headers = \ qgpgmekeylistjob.h \ qgpgmelistallkeysjob.h \ qgpgmereceivekeysjob.h \ + qgpgmerefreshopenpgpkeysjob.h \ qgpgmerefreshsmimekeysjob.h \ qgpgmerevokekeyjob.h \ qgpgmesignencryptjob.h \ @@ -215,6 +217,7 @@ qgpgme_moc_sources = \ qgpgmekeylistjob.moc \ qgpgmelistallkeysjob.moc \ qgpgmereceivekeysjob.moc \ + qgpgmerefreshopenpgpkeysjob.moc \ qgpgmerefreshsmimekeysjob.moc \ qgpgmerevokekeyjob.moc \ qgpgmesignencryptjob.moc \ diff --git a/lang/qt/src/protocol.h b/lang/qt/src/protocol.h index 8538bd8d..d8500174 100644 --- a/lang/qt/src/protocol.h +++ b/lang/qt/src/protocol.h @@ -135,6 +135,14 @@ public: virtual DeleteJob *deleteJob() const = 0; virtual SignEncryptJob *signEncryptJob(bool armor = false, 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 ChangeExpiryJob *changeExpiryJob() const = 0; virtual SignKeyJob *signKeyJob() const = 0; diff --git a/lang/qt/src/protocol_p.h b/lang/qt/src/protocol_p.h index 91c522f1..9ff62ee6 100644 --- a/lang/qt/src/protocol_p.h +++ b/lang/qt/src/protocol_p.h @@ -42,6 +42,7 @@ #include "qgpgmelistallkeysjob.h" #include "qgpgmedecryptjob.h" #include "qgpgmedecryptverifyjob.h" +#include "qgpgmerefreshopenpgpkeysjob.h" #include "qgpgmerefreshsmimekeysjob.h" #include "qgpgmedeletejob.h" #include "qgpgmedownloadjob.h" @@ -283,12 +284,15 @@ public: QGpgME::RefreshKeysJob *refreshKeysJob() const Q_DECL_OVERRIDE { - if (mProtocol != GpgME::CMS) { // fixme: add support for gpg, too - return nullptr; + if (mProtocol == GpgME::CMS) { + return new QGpgME::QGpgMERefreshSMIMEKeysJob; } - // this operation is not supported by gpgme, so we have to call gpgsm ourselves: - return new QGpgME::QGpgMERefreshSMIMEKeysJob(); + GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); + if (!context) { + return nullptr; + } + return new QGpgME::QGpgMERefreshOpenPGPKeysJob{context}; } QGpgME::DownloadJob *downloadJob(bool armor) const Q_DECL_OVERRIDE diff --git a/lang/qt/src/qgpgmerefreshopenpgpkeysjob.cpp b/lang/qt/src/qgpgmerefreshopenpgpkeysjob.cpp new file mode 100644 index 00000000..425b9bf7 --- /dev/null +++ b/lang/qt/src/qgpgmerefreshopenpgpkeysjob.cpp @@ -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 + + 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 +#include + +#include + +#include "qgpgme_debug.h" + +using namespace QGpgME; +using namespace GpgME; + +QStringList toEmailAddresses(const std::vector &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 &keys) +{ + Context::KeyListModeSaver saver{ctx}; + ctx->setKeyListMode(GpgME::LocateExternal); + + const auto emails = toEmailAddresses(keys); + std::vector dummy; + auto job = std::unique_ptr{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 &keys) +{ + const auto fprs = toFingerprints(keys); + + auto job = std::unique_ptr{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 &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 &keys) +{ + run(std::bind(&refresh_keys, std::placeholders::_1, keys)); + return Error{}; +} + +#include "qgpgmerefreshopenpgpkeysjob.moc" diff --git a/lang/qt/src/qgpgmerefreshopenpgpkeysjob.h b/lang/qt/src/qgpgmerefreshopenpgpkeysjob.h new file mode 100644 index 00000000..3ccfb5cf --- /dev/null +++ b/lang/qt/src/qgpgmerefreshopenpgpkeysjob.h @@ -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 + + 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 +#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 &keys) override; +}; + +} + +#endif // __QGPGME_QGPGMEREFRESHOPENPGPKEYSJOB_H__