From b6593bda1f7bfaf6cf3b869b8379cbb2b619e00a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingo=20Kl=C3=B6cker?= Date: Fri, 4 Nov 2022 10:39:30 +0100 Subject: [PATCH] qt: Allow disabling automatic trust database check on key listing * lang/qt/src/Makefile.am (qgpgme_sources): Add listallkeysjob.cpp. * lang/qt/src/listallkeysjob.cpp: New. * lang/qt/src/listallkeysjob.h (enum ListAllKeysJob::Option, typedef ListAllKeysJob::Options, ListAllKeysJob::setOptions, ListAllKeysJob::options): New. * lang/qt/src/qgpgmelistallkeysjob.cpp (list_keys): Set context flag "no-auto-check-trustdb" if requested. (QGpgMEListAllKeysJob::start, QGpgMEListAllKeysJob::exec): Add options to call of list_keys. -- GnuPG-bug-id: 6261 --- NEWS | 7 +++ lang/qt/src/Makefile.am | 2 +- lang/qt/src/listallkeysjob.cpp | 65 ++++++++++++++++++++++++++++ lang/qt/src/listallkeysjob.h | 14 ++++++ lang/qt/src/qgpgmelistallkeysjob.cpp | 18 ++++++-- 5 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 lang/qt/src/listallkeysjob.cpp diff --git a/NEWS b/NEWS index c6cec593..c5f457a3 100644 --- a/NEWS +++ b/NEWS @@ -15,12 +15,19 @@ Noteworthy changes in version 1.18.1 (unreleased) * cpp: Allow setting the curve to use when generating ECC keys for smart cards. [T4429] + * qt: Extend ListAllKeysJob to allow disabling the automatic trust database + check when listing all keys. [T6261] + * Interface changes relative to the 1.18.0 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_get_ctx_flag EXTENDED: New flag 'no-auto-check-trustdb'. gpgme_set_ctx_flag EXTENDED: New flag 'no-auto-check-trustdb'. cpp: GpgGenCardKeyInteractor::Curve NEW. cpp: GpgGenCardKeyInteractor::setCurve NEW. + qt: ListAllKeysJob::Option NEW. + qt: ListAllKeysJob::Options NEW. + qt: ListAllKeysJob::setOptions NEW. + qt: ListAllKeysJob::options NEW. Noteworthy changes in version 1.18.0 (2022-08-10) diff --git a/lang/qt/src/Makefile.am b/lang/qt/src/Makefile.am index a9b54c94..e5358b49 100644 --- a/lang/qt/src/Makefile.am +++ b/lang/qt/src/Makefile.am @@ -41,7 +41,7 @@ qgpgme_sources = \ qgpgmedecryptverifyjob.cpp qgpgmedeletejob.cpp qgpgmedownloadjob.cpp \ qgpgmeencryptjob.cpp qgpgmeexportjob.cpp qgpgmeimportfromkeyserverjob.cpp \ qgpgmeimportjob.cpp qgpgmekeygenerationjob.cpp qgpgmekeylistjob.cpp \ - qgpgmelistallkeysjob.cpp qgpgmenewcryptoconfig.cpp \ + listallkeysjob.cpp qgpgmelistallkeysjob.cpp qgpgmenewcryptoconfig.cpp \ qgpgmereceivekeysjob.cpp \ qgpgmerefreshsmimekeysjob.cpp \ qgpgmerevokekeyjob.cpp \ diff --git a/lang/qt/src/listallkeysjob.cpp b/lang/qt/src/listallkeysjob.cpp new file mode 100644 index 00000000..f72a45d1 --- /dev/null +++ b/lang/qt/src/listallkeysjob.cpp @@ -0,0 +1,65 @@ +/* + listallkeysjob.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 "listallkeysjob.h" +#include "job_p.h" + +using namespace QGpgME; + +namespace +{ +struct ListAllKeysJobPrivate : public JobPrivate +{ + ListAllKeysJobPrivate() = default; + + ~ListAllKeysJobPrivate() override = default; + + ListAllKeysJob::Options m_options = ListAllKeysJob::Default; +}; +} + +void ListAllKeysJob::setOptions(ListAllKeysJob::Options options) +{ + auto d = jobPrivate(this); + d->m_options = options; +} + +ListAllKeysJob::Options ListAllKeysJob::options() const +{ + auto d = jobPrivate(this); + return d->m_options; +} diff --git a/lang/qt/src/listallkeysjob.h b/lang/qt/src/listallkeysjob.h index 85cd04b1..561293ca 100644 --- a/lang/qt/src/listallkeysjob.h +++ b/lang/qt/src/listallkeysjob.h @@ -5,6 +5,8 @@ Copyright (c) 2004 Klarälvdalens Datakonsult AB Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik Software engineering by Intevation GmbH + 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 @@ -74,12 +76,22 @@ namespace QGpgME class QGPGME_EXPORT ListAllKeysJob : public Job { Q_OBJECT +public: + enum Option { + Default = 0x00, + DisableAutomaticTrustDatabaseCheck = 0x01, + }; + Q_DECLARE_FLAGS(Options, Option) + protected: explicit ListAllKeysJob(QObject *parent); public: ~ListAllKeysJob(); + void setOptions(Options options); + Options options() const; + /** Starts the listallkeys operation. In general, all keys are returned (however, the backend is free to truncate the result @@ -101,6 +113,8 @@ Q_SIGNALS: void result(const GpgME::KeyListResult &result, const std::vector &pub = std::vector(), const std::vector &sec = std::vector(), const QString &auditLogAsHtml = QString(), const GpgME::Error &auditLogError = GpgME::Error()); }; +Q_DECLARE_OPERATORS_FOR_FLAGS(ListAllKeysJob::Options) + } #endif // __KLEO_LISTALLKEYSJOB_H__ diff --git a/lang/qt/src/qgpgmelistallkeysjob.cpp b/lang/qt/src/qgpgmelistallkeysjob.cpp index 6f169ac9..2d1dc091 100644 --- a/lang/qt/src/qgpgmelistallkeysjob.cpp +++ b/lang/qt/src/qgpgmelistallkeysjob.cpp @@ -5,6 +5,8 @@ Copyright (c) 2004,2008 Klarälvdalens Datakonsult AB Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik Software engineering by Intevation GmbH + 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 @@ -38,11 +40,13 @@ #include "qgpgmelistallkeysjob.h" +#include "debug.h" #include "key.h" #include "context.h" #include "engineinfo.h" #include "global.h" #include "keylistresult.h" +#include "qgpgme_debug.h" #include @@ -162,12 +166,20 @@ static KeyListResult do_list_keys(Context *ctx, std::vector &keys) return result; } -static QGpgMEListAllKeysJob::result_type list_keys(Context *ctx, bool mergeKeys) +static QGpgMEListAllKeysJob::result_type list_keys(Context *ctx, bool mergeKeys, ListAllKeysJob::Options options) { if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < "2.1.0") { return list_keys_legacy(ctx, mergeKeys); } + if (options & ListAllKeysJob::DisableAutomaticTrustDatabaseCheck) { + auto err = ctx->setFlag("no-auto-check-trustdb", "1"); + if (err) { + // ignore error, but log a warning + qCWarning(QGPGME_LOG) << "Setting context flag no-auto-check-trustdb failed:" << err; + } + } + std::vector keys; KeyListResult r = do_list_keys(ctx, keys); std::sort(keys.begin(), keys.end(), ByFingerprint()); @@ -182,13 +194,13 @@ static QGpgMEListAllKeysJob::result_type list_keys(Context *ctx, bool mergeKeys) Error QGpgMEListAllKeysJob::start(bool mergeKeys) { - run(std::bind(&list_keys, std::placeholders::_1, mergeKeys)); + run(std::bind(&list_keys, std::placeholders::_1, mergeKeys, options())); return Error(); } KeyListResult QGpgMEListAllKeysJob::exec(std::vector &pub, std::vector &sec, bool mergeKeys) { - const result_type r = list_keys(context(), mergeKeys); + const result_type r = list_keys(context(), mergeKeys, options()); resultHook(r); pub = std::get<1>(r); sec = std::get<2>(r);