qt: Add job for creating encrypted signed archives

* lang/qt/src/signencryptarchivejob.cpp,
lang/qt/src/signencryptarchivejob.h,
lang/qt/src/signencryptarchivejob_p.h,
lang/qt/src/qgpgmesignencryptarchivejob.cpp,
lang/qt/src/qgpgmesignencryptarchivejob.h: New.
* lang/qt/src/protocol.h (class Protocol): Add pure virtual member
function signEncryptArchiveJob
* lang/qt/src/protocol_p.h (Protocol::signEncryptArchiveJob): ... and
implement it.
* lang/qt/src/Makefile.am: Update accordingly.

* lang/qt/tests/run-encryptarchivejob.cpp (displayName): Remove.
(CommandLineOptions): Initialize member armor. Add member sign.
(parseCommandLine): Update application description. Add and parse option
-s/--sign.
(main): Use SignEncryptArchiveJob if sign option is set.
--

GnuPG-bug-id: 6342
This commit is contained in:
Ingo Klöcker 2023-01-31 11:20:47 +01:00
parent 2b98585c89
commit 8c4436e73a
No known key found for this signature in database
GPG Key ID: F5A5D1692277A1E9
9 changed files with 494 additions and 30 deletions

View File

@ -51,6 +51,7 @@ qgpgme_sources = \
qgpgmesetprimaryuseridjob.cpp \
qgpgmesignarchivejob.cpp \
qgpgmesignencryptjob.cpp \
qgpgmesignencryptarchivejob.cpp \
qgpgmesignjob.cpp qgpgmesignkeyjob.cpp qgpgmeverifydetachedjob.cpp \
qgpgmeverifyopaquejob.cpp qgpgmewkdlookupjob.cpp threadedjobmixin.cpp \
qgpgmekeyformailboxjob.cpp qgpgme_debug.cpp \
@ -59,6 +60,7 @@ qgpgme_sources = \
qgpgmegpgcardjob.cpp changeexpiryjob.cpp encryptjob.cpp importjob.cpp \
signarchivejob.cpp \
signencryptjob.cpp \
signencryptarchivejob.cpp \
dn.cpp cryptoconfig.cpp wkdlookupresult.cpp \
util.cpp
@ -95,6 +97,7 @@ qgpgme_headers= \
signjob.h \
signkeyjob.h \
signencryptjob.h \
signencryptarchivejob.h \
verifyopaquejob.h \
refreshkeysjob.h \
cryptoconfig.h \
@ -143,6 +146,7 @@ camelcase_headers= \
SignArchiveJob \
SignJob \
SignKeyJob \
SignEncryptArchiveJob \
SignEncryptJob \
VerifyOpaqueJob \
RefreshKeysJob \
@ -195,6 +199,7 @@ private_qgpgme_headers = \
qgpgmesetprimaryuseridjob.h \
qgpgmesignarchivejob.h \
qgpgmesignencryptjob.h \
qgpgmesignencryptarchivejob.h \
qgpgmesignjob.h \
qgpgmesignkeyjob.h \
qgpgmeverifydetachedjob.h \
@ -207,6 +212,7 @@ private_qgpgme_headers = \
qgpgmequickjob.h \
signarchivejob_p.h \
signencryptjob_p.h \
signencryptarchivejob_p.h \
threadedjobmixin.h \
util.h
@ -255,6 +261,7 @@ qgpgme_moc_sources = \
qgpgmesetprimaryuseridjob.moc \
qgpgmesignarchivejob.moc \
qgpgmesignencryptjob.moc \
qgpgmesignencryptarchivejob.moc \
qgpgmesignjob.moc \
qgpgmesignkeyjob.moc \
qgpgmeverifydetachedjob.moc \
@ -269,6 +276,7 @@ qgpgme_moc_sources = \
setprimaryuseridjob.moc \
signarchivejob.moc \
signencryptjob.moc \
signencryptarchivejob.moc \
signjob.moc \
signkeyjob.moc \
specialjob.moc \

View File

@ -54,6 +54,7 @@ class EncryptArchiveJob;
class EncryptJob;
class DecryptJob;
class SignArchiveJob;
class SignEncryptArchiveJob;
class SignJob;
class SignKeyJob;
class VerifyDetachedJob;
@ -194,6 +195,7 @@ public:
virtual EncryptArchiveJob *encryptArchiveJob(bool armor = false) const = 0;
virtual SignArchiveJob *signArchiveJob(bool armor = false) const = 0;
virtual SignEncryptArchiveJob *signEncryptArchiveJob(bool armor = false) const = 0;
};
/** Obtain a reference to the OpenPGP Protocol.

View File

@ -49,6 +49,7 @@
#include "qgpgmeencryptarchivejob.h"
#include "qgpgmeencryptjob.h"
#include "qgpgmesignarchivejob.h"
#include "qgpgmesignencryptarchivejob.h"
#include "qgpgmesignjob.h"
#include "qgpgmesignkeyjob.h"
#include "qgpgmeexportjob.h"
@ -532,6 +533,18 @@ public:
}
return nullptr;
}
QGpgME::SignEncryptArchiveJob *signEncryptArchiveJob(bool armor) const override
{
if (mProtocol != GpgME::OpenPGP) {
return nullptr;
}
if (auto context = GpgME::Context::createForProtocol(mProtocol)) {
context->setArmor(armor);
return new QGpgME::QGpgMESignEncryptArchiveJob{context};
}
return nullptr;
}
};
}

View File

@ -0,0 +1,150 @@
/*
qgpgmesignencryptarchivejob.cpp
This file is part of qgpgme, the Qt API binding for gpgme
Copyright (c) 2004,2007,2008 Klarälvdalens Datakonsult AB
Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik
Software engineering by Intevation GmbH
Copyright (c) 2022,2023 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 "qgpgmesignencryptarchivejob.h"
#include "dataprovider.h"
#include "signencryptarchivejob_p.h"
#include "filelistdataprovider.h"
// #include <context.h>
#include <data.h>
// #include <encryptionresult.h>
//
// #include <QBuffer>
// #include <QFileInfo>
//
// #include <cassert>
using namespace QGpgME;
using namespace GpgME;
namespace
{
class QGpgMESignEncryptArchiveJobPrivate : public SignEncryptArchiveJobPrivate
{
QGpgMESignEncryptArchiveJob *q = nullptr;
public:
QGpgMESignEncryptArchiveJobPrivate(QGpgMESignEncryptArchiveJob *qq)
: q{qq}
{
}
~QGpgMESignEncryptArchiveJobPrivate() override = default;
private:
void start() override
{
q->run();
}
};
}
QGpgMESignEncryptArchiveJob::QGpgMESignEncryptArchiveJob(Context *context)
: mixin_type{context}
{
setJobPrivate(this, std::unique_ptr<QGpgMESignEncryptArchiveJobPrivate>{new QGpgMESignEncryptArchiveJobPrivate{this}});
lateInitialization();
}
static QGpgMESignEncryptArchiveJob::result_type sign_encrypt(Context *ctx,
QThread *thread,
const std::vector<GpgME::Key> &signers,
const std::vector<Key> &recipients,
const std::vector<QString> &paths,
const std::weak_ptr<QIODevice> &cipherText_,
Context::EncryptionFlags encryptionFlags,
const QString &baseDirectory)
{
const std::shared_ptr<QIODevice> cipherText = cipherText_.lock();
const _detail::ToThreadMover ctMover(cipherText, thread);
QGpgME::FileListDataProvider in{paths};
Data indata(&in);
if (!baseDirectory.isEmpty()) {
indata.setFileName(baseDirectory.toStdString());
}
QGpgME::QIODeviceDataProvider out{cipherText};
Data outdata(&out);
ctx->clearSigningKeys();
for (const Key &signer : signers) {
if (!signer.isNull()) {
if (const Error err = ctx->addSigningKey(signer)) {
return std::make_tuple(SigningResult{err}, EncryptionResult{}, QString{}, Error{});
}
}
}
encryptionFlags = static_cast<Context::EncryptionFlags>(encryptionFlags | Context::EncryptArchive);
const auto res = ctx->signAndEncrypt(recipients, indata, outdata, encryptionFlags);
Error ae;
const QString log = _detail::audit_log_as_html(ctx, ae);
return std::make_tuple(res.first, res.second, log, ae);
}
GpgME::Error QGpgMESignEncryptArchiveJob::start(const std::vector<GpgME::Key> &signers,
const std::vector<GpgME::Key> &recipients,
const std::vector<QString> &paths,
const std::shared_ptr<QIODevice> &cipherText,
const GpgME::Context::EncryptionFlags encryptionFlags)
{
if (!cipherText) {
return Error::fromCode(GPG_ERR_INV_VALUE);
}
run(std::bind(&sign_encrypt,
std::placeholders::_1,
std::placeholders::_2,
signers,
recipients,
paths,
std::placeholders::_3,
encryptionFlags,
baseDirectory()),
cipherText);
return {};
}
#include "qgpgmesignencryptarchivejob.moc"

View File

@ -0,0 +1,72 @@
/*
qgpgmesignencryptarchivejob.h
This file is part of qgpgme, the Qt API binding for gpgme
Copyright (c) 2023 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_QGPGMESIGNENCRYPTARCHIVEJOB_H__
#define __QGPGME_QGPGMESIGNENCRYPTARCHIVEJOB_H__
#include "signencryptarchivejob.h"
#include "threadedjobmixin.h"
#include <encryptionresult.h>
#include <signingresult.h>
namespace QGpgME
{
class QGpgMESignEncryptArchiveJob
#ifdef Q_MOC_RUN
: public SignEncryptArchiveJob
#else
: public _detail::ThreadedJobMixin<SignEncryptArchiveJob, std::tuple<GpgME::SigningResult, GpgME::EncryptionResult, QString, GpgME::Error>>
#endif
{
Q_OBJECT
#ifdef Q_MOC_RUN
public Q_SLOTS:
void slotFinished();
#endif
public:
explicit QGpgMESignEncryptArchiveJob(GpgME::Context *context);
~QGpgMESignEncryptArchiveJob() = default;
GpgME::Error start(const std::vector<GpgME::Key> &signers,
const std::vector<GpgME::Key> &recipients,
const std::vector<QString> &paths,
const std::shared_ptr<QIODevice> &cipherText,
const GpgME::Context::EncryptionFlags flags) override;
};
}
#endif // __QGPGME_QGPGMESIGNENCRYPTARCHIVEJOB_H__

View File

@ -0,0 +1,62 @@
/*
signencryptarchivejob.cpp
This file is part of qgpgme, the Qt API binding for gpgme
Copyright (c) 2023 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 "signencryptarchivejob.h"
#include "signencryptarchivejob_p.h"
using namespace QGpgME;
SignEncryptArchiveJob::SignEncryptArchiveJob(QObject *parent)
: Job{parent}
{
}
SignEncryptArchiveJob::~SignEncryptArchiveJob() = default;
void SignEncryptArchiveJob::setBaseDirectory(const QString &baseDirectory)
{
auto d = jobPrivate<SignEncryptArchiveJobPrivate>(this);
d->m_baseDirectory = baseDirectory;
}
QString SignEncryptArchiveJob::baseDirectory() const
{
auto d = jobPrivate<SignEncryptArchiveJobPrivate>(this);
return d->m_baseDirectory;
}
#include "signencryptarchivejob.moc"

View File

@ -0,0 +1,95 @@
/*
signencryptarchivejob.h
This file is part of qgpgme, the Qt API binding for gpgme
Copyright (c) 2023 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_SIGNENCRYPTARCHIVEJOB_H__
#define __QGPGME_SIGNENCRYPTARCHIVEJOB_H__
#include "job.h"
#ifdef BUILDING_QGPGME
# include "context.h"
#else
# include <gpgme++/context.h>
#endif
namespace GpgME
{
class Key;
}
namespace QGpgME
{
/**
* Abstract base class for job for creating encrypted signed archives
*/
class QGPGME_EXPORT SignEncryptArchiveJob : public Job
{
Q_OBJECT
protected:
explicit SignEncryptArchiveJob(QObject *parent);
public:
~SignEncryptArchiveJob() override;
void setBaseDirectory(const QString &baseDirectory);
QString baseDirectory() const;
/**
* Starts the creation of an encrypted signed archive.
*
* Creates an encrypted signed archive with the files and directories in
* \a paths.
* The archive is signed with the keys in \a signers or with the default
* key, if \a signers is empty. Then the archive is encrypted for the
* keys in \a recipients. If \a recipients is empty, then symmetric
* encryption is performed. The encrypted signed archive is written to
* \a cipherText.
*
* Emits result() when the job has finished.
*/
virtual GpgME::Error start(const std::vector<GpgME::Key> &signers,
const std::vector<GpgME::Key> &recipients,
const std::vector<QString> &paths,
const std::shared_ptr<QIODevice> &cipherText,
const GpgME::Context::EncryptionFlags flags) = 0;
Q_SIGNALS:
void result(const GpgME::SigningResult &signingResult,
const GpgME::EncryptionResult &encryptionResult,
const QString &auditLogAsHtml = {},
const GpgME::Error &auditLogError = {});
};
}
#endif // __QGPGME_SIGNENCRYPTARCHIVEJOB_H__

View File

@ -0,0 +1,49 @@
/*
signencryptarchivejob_p.h
This file is part of qgpgme, the Qt API binding for gpgme
Copyright (c) 2023 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_SIGNENCRYPTARCHIVEJOB_P_H__
#define __QGPGME_SIGNENCRYPTARCHIVEJOB_P_H__
#include "job_p.h"
namespace QGpgME
{
struct SignEncryptArchiveJobPrivate : public JobPrivate
{
QString m_baseDirectory;
};
}
#endif // __QGPGME_SIGNENCRYPTARCHIVEJOB_P_H__

View File

@ -36,6 +36,7 @@
#include <protocol.h>
#include <encryptarchivejob.h>
#include <signencryptarchivejob.h>
#include <QCommandLineParser>
#include <QCoreApplication>
@ -44,8 +45,8 @@
#include <context.h>
#include <encryptionresult.h>
#include <signingresult.h>
#include <algorithm>
#include <iostream>
using namespace GpgME;
@ -55,20 +56,9 @@ std::ostream &operator<<(std::ostream &os, const QString &s)
return os << s.toLocal8Bit().constData();
}
const char *displayName(Protocol protocol)
{
switch (protocol) {
case GpgME::OpenPGP:
return "OpenPGP";
case GpgME::CMS:
return "S/MIME";
default:
return "Unknown protocol";
}
}
struct CommandLineOptions {
bool armor;
bool armor = false;
bool sign = false;
QString archiveName;
QString baseDirectory;
std::vector<QString> filesAndDirectories;
@ -79,9 +69,10 @@ CommandLineOptions parseCommandLine(const QStringList &arguments)
CommandLineOptions options;
QCommandLineParser parser;
parser.setApplicationDescription("Test program for EncryptArchiveJob");
parser.setApplicationDescription("Test program for EncryptArchiveJob and SignEncryptArchiveJob");
parser.addHelpOption();
parser.addOptions({
{{"s", "sign"}, "Sign archive before encryption."},
{{"o", "output"}, "Write output to FILE.", "FILE"},
{{"a", "armor"}, "Create ASCII armored output."},
{{"C", "directory"}, "Change to DIRECTORY before creating the archive.", "DIRECTORY"},
@ -96,6 +87,7 @@ CommandLineOptions parseCommandLine(const QStringList &arguments)
}
options.armor = parser.isSet("armor");
options.sign = parser.isSet("sign");
options.archiveName = parser.value("output");
options.baseDirectory = parser.value("directory");
std::copy(args.begin(), args.end(), std::back_inserter(options.filesAndDirectories));
@ -136,22 +128,43 @@ int main(int argc, char **argv)
return 1;
}
auto job = QGpgME::openpgp()->encryptArchiveJob(options.armor);
if (!job) {
std::cerr << "Error: Could not create job" << std::endl;
return 1;
}
job->setBaseDirectory(options.baseDirectory);
QObject::connect(job, &QGpgME::EncryptArchiveJob::result, &app, [](const GpgME::EncryptionResult &result, const QString &auditLog, const GpgME::Error &) {
std::cerr << "Diagnostics: " << auditLog << std::endl;
std::cerr << "Result: " << result << std::endl;
qApp->quit();
});
if (options.sign) {
auto job = QGpgME::openpgp()->signEncryptArchiveJob(options.armor);
if (!job) {
std::cerr << "Error: Could not create job" << std::endl;
return 1;
}
job->setBaseDirectory(options.baseDirectory);
QObject::connect(job, &QGpgME::SignEncryptArchiveJob::result, &app, [](const GpgME::SigningResult &signingResult, const GpgME::EncryptionResult &encryptionResult, const QString &auditLog, const GpgME::Error &) {
std::cerr << "Diagnostics: " << auditLog << std::endl;
std::cerr << "Signing Result: " << signingResult << std::endl;
std::cerr << "Encryption Result: " << encryptionResult << std::endl;
qApp->quit();
});
const auto err = job->start({}, options.filesAndDirectories, output, GpgME::Context::None);
if (err) {
std::cerr << "Error: Starting the job failed: " << err.asString() << std::endl;
return 1;
const auto err = job->start({}, {}, options.filesAndDirectories, output, GpgME::Context::None);
if (err) {
std::cerr << "Error: Starting the job failed: " << err.asString() << std::endl;
return 1;
}
} else {
auto job = QGpgME::openpgp()->encryptArchiveJob(options.armor);
if (!job) {
std::cerr << "Error: Could not create job" << std::endl;
return 1;
}
job->setBaseDirectory(options.baseDirectory);
QObject::connect(job, &QGpgME::EncryptArchiveJob::result, &app, [](const GpgME::EncryptionResult &result, const QString &auditLog, const GpgME::Error &) {
std::cerr << "Diagnostics: " << auditLog << std::endl;
std::cerr << "Result: " << result << std::endl;
qApp->quit();
});
const auto err = job->start({}, options.filesAndDirectories, output, GpgME::Context::None);
if (err) {
std::cerr << "Error: Starting the job failed: " << err.asString() << std::endl;
return 1;
}
}
return app.exec();