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
This commit is contained in:
Ingo Klöcker 2021-12-14 11:47:09 +01:00
parent 3a43d9dc67
commit fd6bec617d
6 changed files with 229 additions and 9 deletions

View File

@ -40,7 +40,7 @@ qgpgme_sources = \
qgpgmekeyformailboxjob.cpp qgpgme_debug.cpp \ qgpgmekeyformailboxjob.cpp qgpgme_debug.cpp \
qgpgmetofupolicyjob.cpp qgpgmequickjob.cpp \ qgpgmetofupolicyjob.cpp qgpgmequickjob.cpp \
defaultkeygenerationjob.cpp qgpgmewkspublishjob.cpp \ defaultkeygenerationjob.cpp qgpgmewkspublishjob.cpp \
qgpgmegpgcardjob.cpp changeexpiryjob.cpp \ qgpgmegpgcardjob.cpp changeexpiryjob.cpp importjob.cpp \
dn.cpp cryptoconfig.cpp wkdlookupresult.cpp dn.cpp cryptoconfig.cpp wkdlookupresult.cpp
# If you add one here make sure that you also add one in camelcase # If you add one here make sure that you also add one in camelcase

78
lang/qt/src/importjob.cpp Normal file
View File

@ -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 <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 "importjob.h"
#include "job_p.h"
#include <context.h>
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<ImportJobPrivate>(this);
d->m_keyOrigin = origin;
d->m_keyOriginUrl = url;
}
GpgME::Key::Origin ImportJob::keyOrigin() const
{
const auto d = jobPrivate<ImportJobPrivate>(this);
return d->m_keyOrigin;
}
QString ImportJob::keyOriginUrl() const
{
const auto d = jobPrivate<ImportJobPrivate>(this);
return d->m_keyOriginUrl;
}

View File

@ -40,6 +40,8 @@
#include <QtCore/QByteArray> #include <QtCore/QByteArray>
#include <gpgme++/key.h>
namespace GpgME namespace GpgME
{ {
class Error; class Error;
@ -68,7 +70,11 @@ class QGPGME_EXPORT ImportJob : public AbstractImportJob
protected: protected:
explicit ImportJob(QObject *parent); explicit ImportJob(QObject *parent);
public: 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 Starts the importing operation. \a keyData contains the data to

View File

@ -55,10 +55,36 @@ QGpgMEImportJob::QGpgMEImportJob(Context *context)
lateInitialization(); 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<Key::Origin, const char *> 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); QGpgME::QByteArrayDataProvider dp(certData);
Data data(&dp); Data data(&dp);
@ -70,13 +96,13 @@ static QGpgMEImportJob::result_type import_qba(Context *ctx, const QByteArray &c
Error QGpgMEImportJob::start(const QByteArray &certData) 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(); return Error();
} }
GpgME::ImportResult QGpgME::QGpgMEImportJob::exec(const QByteArray &keyData) 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); resultHook(r);
return mResult; return mResult;
} }

View File

@ -29,14 +29,14 @@ EXTRA_DIST = initial.test final.test
the_tests = \ the_tests = \
t-keylist t-keylocate t-ownertrust t-tofuinfo \ t-keylist t-keylocate t-ownertrust t-tofuinfo \
t-encrypt t-verify t-various t-config t-remarks t-trustsignatures \ 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 TESTS = initial.test $(the_tests) final.test
moc_files = t-keylist.moc t-keylocate.moc t-ownertrust.moc t-tofuinfo.moc \ 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-encrypt.moc t-support.hmoc t-wkspublish.moc t-verify.moc \
t-various.moc t-config.moc t-remarks.moc t-trustsignatures.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 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_trustsignatures_SOURCES = t-trustsignatures.cpp $(support_src)
t_changeexpiryjob_SOURCES = t-changeexpiryjob.cpp $(support_src) t_changeexpiryjob_SOURCES = t-changeexpiryjob.cpp $(support_src)
t_wkdlookup_SOURCES = t-wkdlookup.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 run_keyformailboxjob_SOURCES = run-keyformailboxjob.cpp
nodist_t_keylist_SOURCES = $(moc_files) 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 \ noinst_PROGRAMS = t-keylist t-keylocate t-ownertrust t-tofuinfo t-encrypt \
run-keyformailboxjob t-wkspublish t-verify t-various t-config t-remarks \ 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 \ CLEANFILES = secring.gpg pubring.gpg pubring.kbx trustdb.gpg dirmngr.conf \
gpg-agent.conf pubring.kbx~ S.gpg-agent gpg.conf pubring.gpg~ \ gpg-agent.conf pubring.kbx~ S.gpg-agent gpg.conf pubring.gpg~ \

109
lang/qt/tests/t-import.cpp Normal file
View File

@ -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 <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 "t-support.h"
#include "context.h"
#include "engineinfo.h"
#include "protocol.h"
#include "importjob.h"
#include <gpgme++/importresult.h>
#include <QDebug>
#include <QSignalSpy>
#include <QTest>
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"