From fd6bec617d8fb6ddcbc622a5d8cf94594a7d5520 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingo=20Kl=C3=B6cker?= Date: Tue, 14 Dec 2021 11:47:09 +0100 Subject: [PATCH] qt: Allow setting key origin when importing keys * lang/qt/src/Makefile.am (qgpgme_sources): Add importjob.cpp. * lang/qt/src/importjob.cpp: New. * lang/qt/src/importjob.h (class ImportJob): Add member functions setKeyOrigin, keyOrigin, keyOriginUrl. * lang/qt/src/qgpgmeimportjob.cpp (originToString): New. (import_qba): Set key origin context flag. (QGpgMEImportJob::start, QGpgMEImportJob::exec): Add options to call of import_qba. * lang/qt/tests/Makefile.am (the_tests, moc_files, noinst_PROGRAMS): Add new test. (t_import_SOURCES): New. * lang/qt/tests/t-import.cpp: New. -- GnuPG-bug-id: 5733 --- lang/qt/src/Makefile.am | 2 +- lang/qt/src/importjob.cpp | 78 +++++++++++++++++++++++ lang/qt/src/importjob.h | 8 ++- lang/qt/src/qgpgmeimportjob.cpp | 34 ++++++++-- lang/qt/tests/Makefile.am | 7 +- lang/qt/tests/t-import.cpp | 109 ++++++++++++++++++++++++++++++++ 6 files changed, 229 insertions(+), 9 deletions(-) create mode 100644 lang/qt/src/importjob.cpp create mode 100644 lang/qt/tests/t-import.cpp diff --git a/lang/qt/src/Makefile.am b/lang/qt/src/Makefile.am index 261eff60..b9171a60 100644 --- a/lang/qt/src/Makefile.am +++ b/lang/qt/src/Makefile.am @@ -40,7 +40,7 @@ qgpgme_sources = \ qgpgmekeyformailboxjob.cpp qgpgme_debug.cpp \ qgpgmetofupolicyjob.cpp qgpgmequickjob.cpp \ defaultkeygenerationjob.cpp qgpgmewkspublishjob.cpp \ - qgpgmegpgcardjob.cpp changeexpiryjob.cpp \ + qgpgmegpgcardjob.cpp changeexpiryjob.cpp importjob.cpp \ dn.cpp cryptoconfig.cpp wkdlookupresult.cpp # If you add one here make sure that you also add one in camelcase diff --git a/lang/qt/src/importjob.cpp b/lang/qt/src/importjob.cpp new file mode 100644 index 00000000..3f28606f --- /dev/null +++ b/lang/qt/src/importjob.cpp @@ -0,0 +1,78 @@ +/* + importjob.cpp + + This file is part of qgpgme, the Qt API binding for gpgme + Copyright (c) 2021 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 "importjob.h" +#include "job_p.h" + +#include + +using namespace GpgME; +using namespace QGpgME; + +namespace +{ +struct ImportJobPrivate : public JobPrivate +{ + ImportJobPrivate() + { + } + + ~ImportJobPrivate() override = default; + + Key::Origin m_keyOrigin = Key::OriginUnknown; + QString m_keyOriginUrl; +}; +} + +void ImportJob::setKeyOrigin(GpgME::Key::Origin origin, const QString &url) +{ + const auto d = jobPrivate(this); + d->m_keyOrigin = origin; + d->m_keyOriginUrl = url; +} + +GpgME::Key::Origin ImportJob::keyOrigin() const +{ + const auto d = jobPrivate(this); + return d->m_keyOrigin; +} + +QString ImportJob::keyOriginUrl() const +{ + const auto d = jobPrivate(this); + return d->m_keyOriginUrl; +} diff --git a/lang/qt/src/importjob.h b/lang/qt/src/importjob.h index 7437fbdc..1ded1cde 100644 --- a/lang/qt/src/importjob.h +++ b/lang/qt/src/importjob.h @@ -40,6 +40,8 @@ #include +#include + namespace GpgME { class Error; @@ -68,7 +70,11 @@ class QGPGME_EXPORT ImportJob : public AbstractImportJob protected: explicit ImportJob(QObject *parent); public: - ~ImportJob(); + ~ImportJob() override; + + void setKeyOrigin(GpgME::Key::Origin origin, const QString &url = {}); + GpgME::Key::Origin keyOrigin() const; + QString keyOriginUrl() const; /** Starts the importing operation. \a keyData contains the data to diff --git a/lang/qt/src/qgpgmeimportjob.cpp b/lang/qt/src/qgpgmeimportjob.cpp index a62d05f9..ad5c2aee 100644 --- a/lang/qt/src/qgpgmeimportjob.cpp +++ b/lang/qt/src/qgpgmeimportjob.cpp @@ -55,10 +55,36 @@ QGpgMEImportJob::QGpgMEImportJob(Context *context) lateInitialization(); } -QGpgMEImportJob::~QGpgMEImportJob() {} +QGpgMEImportJob::~QGpgMEImportJob() = default; -static QGpgMEImportJob::result_type import_qba(Context *ctx, const QByteArray &certData) +static const char *originToString(Key::Origin origin) { + static const std::map mapping = { + { Key::OriginUnknown, "unknown" }, + { Key::OriginKS, "ks" }, + { Key::OriginDane, "dane" }, + { Key::OriginWKD, "wkd" }, + { Key::OriginURL, "url" }, + { Key::OriginFile, "file" }, + { Key::OriginSelf, "self" }, + }; + const auto it = mapping.find(origin); + return (it != std::end(mapping)) ? it->second : nullptr; +} + +static QGpgMEImportJob::result_type import_qba(Context *ctx, const QByteArray &certData, Key::Origin keyOrigin, const QString &keyOriginUrl) +{ + if (keyOrigin != Key::OriginUnknown) { + if (const auto origin = originToString(keyOrigin)) { + std::string value{origin}; + if (!keyOriginUrl.isEmpty()) { + value += ","; + value += keyOriginUrl.toStdString(); + } + ctx->setFlag("key-origin", value.c_str()); + } + } + QGpgME::QByteArrayDataProvider dp(certData); Data data(&dp); @@ -70,13 +96,13 @@ static QGpgMEImportJob::result_type import_qba(Context *ctx, const QByteArray &c Error QGpgMEImportJob::start(const QByteArray &certData) { - run(std::bind(&import_qba, std::placeholders::_1, certData)); + run(std::bind(&import_qba, std::placeholders::_1, certData, keyOrigin(), keyOriginUrl())); return Error(); } GpgME::ImportResult QGpgME::QGpgMEImportJob::exec(const QByteArray &keyData) { - const result_type r = import_qba(context(), keyData); + const result_type r = import_qba(context(), keyData, keyOrigin(), keyOriginUrl()); resultHook(r); return mResult; } diff --git a/lang/qt/tests/Makefile.am b/lang/qt/tests/Makefile.am index b7ec546e..4537c7f8 100644 --- a/lang/qt/tests/Makefile.am +++ b/lang/qt/tests/Makefile.am @@ -29,14 +29,14 @@ EXTRA_DIST = initial.test final.test the_tests = \ t-keylist t-keylocate t-ownertrust t-tofuinfo \ t-encrypt t-verify t-various t-config t-remarks t-trustsignatures \ - t-changeexpiryjob t-wkdlookup + t-changeexpiryjob t-wkdlookup t-import TESTS = initial.test $(the_tests) final.test moc_files = t-keylist.moc t-keylocate.moc t-ownertrust.moc t-tofuinfo.moc \ t-encrypt.moc t-support.hmoc t-wkspublish.moc t-verify.moc \ t-various.moc t-config.moc t-remarks.moc t-trustsignatures.moc \ - t-changeexpiryjob.moc t-wkdlookup.moc + t-changeexpiryjob.moc t-wkdlookup.moc t-import.moc AM_LDFLAGS = -no-install @@ -65,6 +65,7 @@ t_remarks_SOURCES = t-remarks.cpp $(support_src) t_trustsignatures_SOURCES = t-trustsignatures.cpp $(support_src) t_changeexpiryjob_SOURCES = t-changeexpiryjob.cpp $(support_src) t_wkdlookup_SOURCES = t-wkdlookup.cpp $(support_src) +t_import_SOURCES = t-import.cpp $(support_src) run_keyformailboxjob_SOURCES = run-keyformailboxjob.cpp nodist_t_keylist_SOURCES = $(moc_files) @@ -73,7 +74,7 @@ BUILT_SOURCES = $(moc_files) pubring-stamp noinst_PROGRAMS = t-keylist t-keylocate t-ownertrust t-tofuinfo t-encrypt \ run-keyformailboxjob t-wkspublish t-verify t-various t-config t-remarks \ - t-trustsignatures t-changeexpiryjob t-wkdlookup + t-trustsignatures t-changeexpiryjob t-wkdlookup t-import CLEANFILES = secring.gpg pubring.gpg pubring.kbx trustdb.gpg dirmngr.conf \ gpg-agent.conf pubring.kbx~ S.gpg-agent gpg.conf pubring.gpg~ \ diff --git a/lang/qt/tests/t-import.cpp b/lang/qt/tests/t-import.cpp new file mode 100644 index 00000000..aaa05c0d --- /dev/null +++ b/lang/qt/tests/t-import.cpp @@ -0,0 +1,109 @@ +/* t-import.cpp + + This file is part of qgpgme, the Qt API binding for gpgme + Copyright (c) 2021 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 "t-support.h" + +#include "context.h" +#include "engineinfo.h" +#include "protocol.h" +#include "importjob.h" + +#include + +#include +#include +#include + +using namespace QGpgME; +using namespace GpgME; + +static const char keyFpr[] = "5C5C428FABCC20F6913464BCCA6FB442887289B3"; + +static const char keyData[] = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" +"\n" +"mDMEYbhuixYJKwYBBAHaRw8BAQdAulOM3IksCjdOJluEVlwalD8oZ5oa6wCw3EgW\n" +"NswXXb60H2ltcG9ydFdpdGhLZXlPcmlnaW5AZXhhbXBsZS5uZXSIlAQTFgoAPBYh\n" +"BFxcQo+rzCD2kTRkvMpvtEKIcomzBQJhuG6LAhsDBQsJCAcCAyICAQYVCgkICwIE\n" +"FgIDAQIeBwIXgAAKCRDKb7RCiHKJs+cIAQDaeoOw1OCAGpZQb8xJmLJHul5dLLzU\n" +"RBdHauMx9NROmQEA23QUVedc7walQjNKFzyIJA/YqRdbAKPiLonRBmxk9Ay4OARh\n" +"uG6LEgorBgEEAZdVAQUBAQdAMVdO9mNWIP/q8PtNOnBGlPyhx/vs07sF5sXk50A+\n" +"61QDAQgHiHgEGBYKACAWIQRcXEKPq8wg9pE0ZLzKb7RCiHKJswUCYbhuiwIbDAAK\n" +"CRDKb7RCiHKJs/x6AP0SEbZqW4iLCz2i1JntQghK5qpSZOVqsBTcARd6pcJ/cwEA\n" +"mrwskWazuS9+GVbHT5RATWOXnGaj+AICSDPE6qHtGgA=\n" +"=putz\n" +"-----END PGP PUBLIC KEY BLOCK-----\n"; + +class ImportTest : public QGpgMETest +{ + Q_OBJECT + +Q_SIGNALS: + void asyncDone(); + +private Q_SLOTS: + + void testImportWithKeyOrigin() + { + if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < "2.1.22") { + QSKIP("gpg does not yet support the --key-origin option"); + } + + auto *job = openpgp()->importJob(); + job->setKeyOrigin(GpgME::Key::OriginWKD, "https://example.net"); + connect(job, &ImportJob::result, this, + [this](ImportResult result, QString, Error) + { + QVERIFY(!result.error()); + QVERIFY(!result.imports().empty()); + QVERIFY(result.numImported()); + Q_EMIT asyncDone(); + }); + job->start(QByteArray{keyData}); + QSignalSpy spy (this, SIGNAL(asyncDone())); + QVERIFY(spy.wait()); + + auto ctx = Context::createForProtocol(GpgME::OpenPGP); + GpgME::Error err; + const auto key = ctx->key(keyFpr, err, false); + QVERIFY(!key.isNull()); + QVERIFY(key.origin() == Key::OriginWKD); + // the origin URL is currently not available in GpgME + } +}; + +QTEST_MAIN(ImportTest) + +#include "t-import.moc"