diff --git a/lang/qt/src/protocol.h b/lang/qt/src/protocol.h index cffd53b2..35789414 100644 --- a/lang/qt/src/protocol.h +++ b/lang/qt/src/protocol.h @@ -1,201 +1,201 @@ /* protocol.h This file is part of qgpgme, the Qt API binding for gpgme Copyright (c) 2004,2005 Klarälvdalens Datakonsult AB Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik Software engineering by Intevation GmbH 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_PROTOCOL_H__ #define __QGPGME_PROTOCOL_H__ #include #include #include "qgpgme_export.h" namespace QGpgME { class CryptoConfig; class KeyListJob; class ListAllKeysJob; class KeyGenerationJob; class ImportJob; class ImportFromKeyserverJob; class ExportJob; class DownloadJob; class DeleteJob; class EncryptJob; class DecryptJob; class SignJob; class SignKeyJob; class VerifyDetachedJob; class VerifyOpaqueJob; class SignEncryptJob; class DecryptVerifyJob; class RefreshKeysJob; class ChangeExpiryJob; class ChangeOwnerTrustJob; class ChangePasswdJob; class AddUserIDJob; class SpecialJob; class KeyForMailboxJob; class WKDLookupJob; class WKSPublishJob; class TofuPolicyJob; class QuickJob; class GpgCardJob; /** The main entry point for QGpgME Comes in OpenPGP and SMIME(CMS) flavors. * * Use the proctocol class to obtain an instance of a job. Jobs * provide async API for GnuPG that can be connected to signals / slots. * * A job is usually started with start() and emits a result signal. * The parameters of the result signal depend on the job but the last * two are always a QString for the auditlog and an GpgME::Error for * an eventual error. * * In case async API is used and the result signal is emitted a * job schedules its own deletion. * * Most jobs also provide a synchronous call exec in which case * you have to explicitly delete the job if you don't need it anymore. * * \code * // Async example: * KeyListJob *job = openpgp()->keyListJob(); * connect(job, &KeyListJob::result, job, [this, job](KeyListResult, std::vector keys, QString, Error) * { * // keys and resuls can now be used. * }); * job->start({QStringLiteral("alfa@example.net")}, false); * \endcode * * \code * // Sync eaxmple: * KeyListJob *job = openpgp()->keyListJob(false, false, false); * std::vector keys; * GpgME::KeyListResult result = job->exec(QStringList() << * QStringLiteral("alfa@example.net"), * false, keys); * delete job; * \endcode */ class QGPGME_EXPORT Protocol { public: virtual ~Protocol() {} virtual QString name() const = 0; virtual QString displayName() const = 0; virtual KeyListJob *keyListJob(bool remote = false, bool includeSigs = false, bool validate = false) const = 0; virtual ListAllKeysJob *listAllKeysJob(bool includeSigs = false, bool validate = false) const = 0; virtual EncryptJob *encryptJob(bool armor = false, bool textmode = false) const = 0; virtual DecryptJob *decryptJob() const = 0; virtual SignJob *signJob(bool armor = false, bool textMode = false) const = 0; virtual VerifyDetachedJob *verifyDetachedJob(bool textmode = false) const = 0; virtual VerifyOpaqueJob *verifyOpaqueJob(bool textmode = false) const = 0; virtual KeyGenerationJob *keyGenerationJob() const = 0; virtual ImportJob *importJob() const = 0; virtual ImportFromKeyserverJob *importFromKeyserverJob() const = 0; virtual ExportJob *publicKeyExportJob(bool armor = false) const = 0; - // @param charset the encoding of the passphrase in the exported file - virtual ExportJob *secretKeyExportJob(bool armor = false, const QString &charset = QString()) const = 0; + // the second parameter is ignored; the passphrase in the exported file is always utf-8 encoded + virtual ExportJob *secretKeyExportJob(bool armor = false, const QString & = QString()) const = 0; virtual DownloadJob *downloadJob(bool armor = false) const = 0; virtual DeleteJob *deleteJob() const = 0; virtual SignEncryptJob *signEncryptJob(bool armor = false, bool textMode = false) const = 0; virtual DecryptVerifyJob *decryptVerifyJob(bool textmode = false) const = 0; virtual RefreshKeysJob *refreshKeysJob() const = 0; virtual ChangeExpiryJob *changeExpiryJob() const = 0; virtual SignKeyJob *signKeyJob() const = 0; virtual ChangePasswdJob *changePasswdJob() const = 0; virtual ChangeOwnerTrustJob *changeOwnerTrustJob() const = 0; virtual AddUserIDJob *addUserIDJob() const = 0; virtual SpecialJob *specialJob(const char *type, const QMap &args) const = 0; /** A key locate job. * * This tries to find a key in local * and remote sources, if the key was remote it is imported * by GnuPG. Same as KeyListJob but intended to be used * to locate keys automatically. This ends up calling --locate-keys. * * Only available for OpenPGP * * Results are validated. As if keyListJob was called * with both includeSigs and validate options. */ virtual KeyListJob *locateKeysJob() const = 0; /** Find the best key to use for a mailbox. */ virtual KeyForMailboxJob *keyForMailboxJob() const = 0; /** This job looks up a key via WKD without importing it. */ virtual WKDLookupJob *wkdLookupJob() const = 0; /** A Job for interacting with gnupg's wks tools. */ virtual WKSPublishJob *wksPublishJob() const = 0; /** A Job to set tofu policy */ virtual TofuPolicyJob *tofuPolicyJob() const = 0; /** A Job for the quick commands */ virtual QuickJob *quickJob() const = 0; }; /** Obtain a reference to the OpenPGP Protocol. * * The reference is to a static object. * @returns Reference to the OpenPGP Protocol. */ QGPGME_EXPORT Protocol *openpgp(); /** Obtain a reference to the smime Protocol. * * The reference is to a static object. * @returns Reference to the smime Protocol. */ QGPGME_EXPORT Protocol *smime(); /** Obtain a reference to a cryptoConfig object. * * The reference is to a static object. * @returns reference to cryptoConfig object. */ QGPGME_EXPORT CryptoConfig *cryptoConfig(); /** Obtain a reference to a protocol agnostic GpgCardJob. * * The reference is to a static object. * @returns reference to a GpgCardJob following the job pattern. */ QGPGME_EXPORT GpgCardJob *gpgCardJob(); } #endif diff --git a/lang/qt/src/protocol_p.h b/lang/qt/src/protocol_p.h index da5ce011..054fec6e 100644 --- a/lang/qt/src/protocol_p.h +++ b/lang/qt/src/protocol_p.h @@ -1,446 +1,448 @@ /* protocol_p.h This file is part of qgpgme, the Qt API binding for gpgme Copyright (c) 2004,2005 Klarälvdalens Datakonsult AB Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik Software engineering by Intevation GmbH + Copyright (c) 2022 by 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_PROTOCOL_P_H__ #define __QGPGME_PROTOCOL_P_H__ #include "qgpgmenewcryptoconfig.h" #include "qgpgmekeygenerationjob.h" #include "qgpgmekeylistjob.h" #include "qgpgmelistallkeysjob.h" #include "qgpgmedecryptjob.h" #include "qgpgmedecryptverifyjob.h" #include "qgpgmerefreshkeysjob.h" #include "qgpgmedeletejob.h" -#include "qgpgmesecretkeyexportjob.h" #include "qgpgmedownloadjob.h" #include "qgpgmesignencryptjob.h" #include "qgpgmeencryptjob.h" #include "qgpgmesignjob.h" #include "qgpgmesignkeyjob.h" #include "qgpgmeexportjob.h" #include "qgpgmeverifydetachedjob.h" #include "qgpgmeimportjob.h" #include "qgpgmeimportfromkeyserverjob.h" #include "qgpgmeverifyopaquejob.h" #include "qgpgmechangeexpiryjob.h" #include "qgpgmechangeownertrustjob.h" #include "qgpgmechangepasswdjob.h" #include "qgpgmeadduseridjob.h" #include "qgpgmekeyformailboxjob.h" #include "qgpgmewkdlookupjob.h" #include "qgpgmewkspublishjob.h" #include "qgpgmetofupolicyjob.h" #include "qgpgmequickjob.h" namespace { class Protocol : public QGpgME::Protocol { GpgME::Protocol mProtocol; public: explicit Protocol(GpgME::Protocol proto) : mProtocol(proto) {} QString name() const Q_DECL_OVERRIDE { switch (mProtocol) { case GpgME::OpenPGP: return QStringLiteral("OpenPGP"); case GpgME::CMS: return QStringLiteral("SMIME"); default: return QString(); } } QString displayName() const Q_DECL_OVERRIDE { // ah (2.4.16): Where is this used and isn't this inverted // with name switch (mProtocol) { case GpgME::OpenPGP: return QStringLiteral("gpg"); case GpgME::CMS: return QStringLiteral("gpgsm"); default: return QStringLiteral("unknown"); } } QGpgME::SpecialJob *specialJob(const char *, const QMap &) const Q_DECL_OVERRIDE { return nullptr; } QGpgME::KeyListJob *keyListJob(bool remote, bool includeSigs, bool validate) const Q_DECL_OVERRIDE { GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } unsigned int mode = context->keyListMode(); if (remote) { mode |= GpgME::Extern; mode &= ~GpgME::Local; } else { mode |= GpgME::Local; mode &= ~GpgME::Extern; } if (includeSigs) { mode |= GpgME::Signatures; } if (validate) { mode |= GpgME::Validate; } context->setKeyListMode(mode); return new QGpgME::QGpgMEKeyListJob(context); } QGpgME::ListAllKeysJob *listAllKeysJob(bool includeSigs, bool validate) const Q_DECL_OVERRIDE { GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } unsigned int mode = context->keyListMode(); mode |= GpgME::Local; mode &= ~GpgME::Extern; if (includeSigs) { mode |= GpgME::Signatures; } if (validate) { mode |= GpgME::Validate; /* Setting the context to offline mode disables CRL / OCSP checks in this Job. Otherwise we would try to fetch the CRL's for all CMS keys in the users keyring because GpgME::Validate includes remote resources by default in the validity check. This setting only has any effect if gpgsm >= 2.1.6 is used. */ context->setOffline(true); } context->setKeyListMode(mode); return new QGpgME::QGpgMEListAllKeysJob(context); } QGpgME::EncryptJob *encryptJob(bool armor, bool textmode) const Q_DECL_OVERRIDE { GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } context->setArmor(armor); context->setTextMode(textmode); return new QGpgME::QGpgMEEncryptJob(context); } QGpgME::DecryptJob *decryptJob() const Q_DECL_OVERRIDE { GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } return new QGpgME::QGpgMEDecryptJob(context); } QGpgME::SignJob *signJob(bool armor, bool textMode) const Q_DECL_OVERRIDE { GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } context->setArmor(armor); context->setTextMode(textMode); return new QGpgME::QGpgMESignJob(context); } QGpgME::VerifyDetachedJob *verifyDetachedJob(bool textMode) const Q_DECL_OVERRIDE { GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } context->setTextMode(textMode); return new QGpgME::QGpgMEVerifyDetachedJob(context); } QGpgME::VerifyOpaqueJob *verifyOpaqueJob(bool textMode) const Q_DECL_OVERRIDE { GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } context->setTextMode(textMode); return new QGpgME::QGpgMEVerifyOpaqueJob(context); } QGpgME::KeyGenerationJob *keyGenerationJob() const Q_DECL_OVERRIDE { GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } return new QGpgME::QGpgMEKeyGenerationJob(context); } QGpgME::ImportJob *importJob() const Q_DECL_OVERRIDE { GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } return new QGpgME::QGpgMEImportJob(context); } QGpgME::ImportFromKeyserverJob *importFromKeyserverJob() const Q_DECL_OVERRIDE { GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } return new QGpgME::QGpgMEImportFromKeyserverJob(context); } QGpgME::ExportJob *publicKeyExportJob(bool armor) const Q_DECL_OVERRIDE { GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } context->setArmor(armor); return new QGpgME::QGpgMEExportJob(context); } - QGpgME::ExportJob *secretKeyExportJob(bool armor, const QString &charset) const Q_DECL_OVERRIDE + QGpgME::ExportJob *secretKeyExportJob(bool armor, const QString &) const Q_DECL_OVERRIDE { - if (mProtocol != GpgME::CMS) { // fixme: add support for gpg, too + GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); + if (!context) { return nullptr; } - // this operation is not supported by gpgme, so we have to call gpgsm ourselves: - return new QGpgME::QGpgMESecretKeyExportJob(armor, charset); + context->setArmor(armor); + return new QGpgME::QGpgMEExportJob(context, GpgME::Context::ExportSecret); } QGpgME::RefreshKeysJob *refreshKeysJob() const Q_DECL_OVERRIDE { if (mProtocol != GpgME::CMS) { // fixme: add support for gpg, too return nullptr; } // this operation is not supported by gpgme, so we have to call gpgsm ourselves: return new QGpgME::QGpgMERefreshKeysJob(); } QGpgME::DownloadJob *downloadJob(bool armor) const Q_DECL_OVERRIDE { GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } context->setArmor(armor); // this is the hackish interface for downloading from keyserers currently: context->setKeyListMode(GpgME::Extern); return new QGpgME::QGpgMEDownloadJob(context); } QGpgME::DeleteJob *deleteJob() const Q_DECL_OVERRIDE { GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } return new QGpgME::QGpgMEDeleteJob(context); } QGpgME::SignEncryptJob *signEncryptJob(bool armor, bool textMode) const Q_DECL_OVERRIDE { GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } context->setArmor(armor); context->setTextMode(textMode); return new QGpgME::QGpgMESignEncryptJob(context); } QGpgME::DecryptVerifyJob *decryptVerifyJob(bool textMode) const Q_DECL_OVERRIDE { GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } context->setTextMode(textMode); return new QGpgME::QGpgMEDecryptVerifyJob(context); } QGpgME::ChangeExpiryJob *changeExpiryJob() const Q_DECL_OVERRIDE { if (mProtocol != GpgME::OpenPGP) { return nullptr; // only supported by gpg } GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } return new QGpgME::QGpgMEChangeExpiryJob(context); } QGpgME::ChangePasswdJob *changePasswdJob() const Q_DECL_OVERRIDE { if (!GpgME::hasFeature(GpgME::PasswdFeature, 0)) { return nullptr; } GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } return new QGpgME::QGpgMEChangePasswdJob(context); } QGpgME::SignKeyJob *signKeyJob() const Q_DECL_OVERRIDE { if (mProtocol != GpgME::OpenPGP) { return nullptr; // only supported by gpg } GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } return new QGpgME::QGpgMESignKeyJob(context); } QGpgME::ChangeOwnerTrustJob *changeOwnerTrustJob() const Q_DECL_OVERRIDE { if (mProtocol != GpgME::OpenPGP) { return nullptr; // only supported by gpg } GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } return new QGpgME::QGpgMEChangeOwnerTrustJob(context); } QGpgME::AddUserIDJob *addUserIDJob() const Q_DECL_OVERRIDE { if (mProtocol != GpgME::OpenPGP) { return nullptr; // only supported by gpg } GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } return new QGpgME::QGpgMEAddUserIDJob(context); } QGpgME::KeyListJob *locateKeysJob() const Q_DECL_OVERRIDE { if (mProtocol != GpgME::OpenPGP) { return nullptr; } GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } context->setKeyListMode(GpgME::Extern | GpgME::Local | GpgME::Signatures | GpgME::Validate); return new QGpgME::QGpgMEKeyListJob(context); } QGpgME::KeyForMailboxJob *keyForMailboxJob() const Q_DECL_OVERRIDE { GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } return new QGpgME::QGpgMEKeyForMailboxJob(context); } QGpgME::WKDLookupJob *wkdLookupJob() const Q_DECL_OVERRIDE { if (mProtocol != GpgME::OpenPGP) { return nullptr; } auto context = GpgME::Context::createForEngine(GpgME::AssuanEngine); if (!context) { return nullptr; } return new QGpgME::QGpgMEWKDLookupJob(context.release()); } QGpgME::WKSPublishJob *wksPublishJob() const Q_DECL_OVERRIDE { if (mProtocol != GpgME::OpenPGP) { return nullptr; } auto context = GpgME::Context::createForEngine(GpgME::SpawnEngine); if (!context) { return nullptr; } return new QGpgME::QGpgMEWKSPublishJob(context.release()); } QGpgME::TofuPolicyJob *tofuPolicyJob() const Q_DECL_OVERRIDE { if (mProtocol != GpgME::OpenPGP) { return nullptr; } GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } return new QGpgME::QGpgMETofuPolicyJob(context); } QGpgME::QuickJob *quickJob() const Q_DECL_OVERRIDE { if (mProtocol != GpgME::OpenPGP) { return nullptr; } GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol); if (!context) { return nullptr; } return new QGpgME::QGpgMEQuickJob(context); } }; } #endif diff --git a/lang/qt/src/qgpgmeexportjob.cpp b/lang/qt/src/qgpgmeexportjob.cpp index bf3297a7..e9bc0a4d 100644 --- a/lang/qt/src/qgpgmeexportjob.cpp +++ b/lang/qt/src/qgpgmeexportjob.cpp @@ -1,92 +1,100 @@ /* qgpgmeexportjob.cpp This file is part of qgpgme, the Qt API binding for gpgme 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 by 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 "qgpgmeexportjob.h" #include "dataprovider.h" #include "context.h" #include "data.h" #include "key.h" #include #include using namespace QGpgME; using namespace GpgME; QGpgMEExportJob::QGpgMEExportJob(Context *context) - : mixin_type(context), - m_flags(0) + : QGpgMEExportJob{context, 0} +{ +} + +QGpgMEExportJob::QGpgMEExportJob(Context *context, unsigned int forcedMode) + : mixin_type{context} + , m_exportMode{forcedMode} + , m_additionalExportModeFlags{0} { lateInitialization(); } -QGpgMEExportJob::~QGpgMEExportJob() {} +QGpgMEExportJob::~QGpgMEExportJob() = default; -static QGpgMEExportJob::result_type export_qba(Context *ctx, const QStringList &patterns, unsigned int flags) +static QGpgMEExportJob::result_type export_qba(Context *ctx, const QStringList &patterns, unsigned int mode) { - const _detail::PatternConverter pc(patterns); QGpgME::QByteArrayDataProvider dp; Data data(&dp); - const Error err = ctx->exportPublicKeys(pc.patterns(), data, flags); + const Error err = ctx->exportKeys(pc.patterns(), data, mode); Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(err, dp.data(), log, ae); } Error QGpgMEExportJob::start(const QStringList &patterns) { - run(std::bind(&export_qba, std::placeholders::_1, patterns, m_flags)); + auto mode = m_exportMode | m_additionalExportModeFlags; + run(std::bind(&export_qba, std::placeholders::_1, patterns, mode)); return Error(); } void QGpgMEExportJob::setExportFlags(unsigned int flags) { - m_flags = flags; + m_additionalExportModeFlags = flags; } /* For ABI compat not pure virtual. */ void ExportJob::setExportFlags(unsigned int) { } #include "qgpgmeexportjob.moc" diff --git a/lang/qt/src/qgpgmeexportjob.h b/lang/qt/src/qgpgmeexportjob.h index 3f6bd0e2..b77bad1c 100644 --- a/lang/qt/src/qgpgmeexportjob.h +++ b/lang/qt/src/qgpgmeexportjob.h @@ -1,72 +1,80 @@ /* qgpgmeexportjob.h This file is part of qgpgme, the Qt API binding for gpgme 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 by 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_QGPGMEEXPORTJOB_H__ #define __QGPGME_QGPGMEEXPORTJOB_H__ #include "exportjob.h" #include "threadedjobmixin.h" namespace QGpgME { class QGpgMEExportJob #ifdef Q_MOC_RUN : public ExportJob #else : public _detail::ThreadedJobMixin > #endif { Q_OBJECT #ifdef Q_MOC_RUN public Q_SLOTS: void slotFinished(); #endif public: explicit QGpgMEExportJob(GpgME::Context *context); - ~QGpgMEExportJob(); + // Creates an export job with forced export mode @p exportMode. The + // export mode flags set with @p exportMode cannot be overridden with + // setExportFlags. + explicit QGpgMEExportJob(GpgME::Context *context, unsigned int exportMode); + ~QGpgMEExportJob() Q_DECL_OVERRIDE; /* from ExportJob */ - void setExportFlags (unsigned int flags) Q_DECL_OVERRIDE; + void setExportFlags(unsigned int flags) Q_DECL_OVERRIDE; /* from ExportJob */ GpgME::Error start(const QStringList &patterns) Q_DECL_OVERRIDE; + private: - unsigned int m_flags; + unsigned int m_exportMode; + unsigned int m_additionalExportModeFlags; }; } #endif // __QGPGME_QGPGMEEXPORTJOB_H__ diff --git a/lang/qt/tests/Makefile.am b/lang/qt/tests/Makefile.am index ef67a637..57ae59af 100644 --- a/lang/qt/tests/Makefile.am +++ b/lang/qt/tests/Makefile.am @@ -1,107 +1,109 @@ # Makefile.am - Makefile for GPGME Qt tests. # Copyright (C) 2016 Bundesamt für Sicherheit in der Informationstechnik # Software engineering by Intevation GmbH # # This file is part of GPGME. # # GPGME is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # GPGME 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 Lesser General # Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this program; if not, see . ## Process this file with automake to produce Makefile.in GPG = gpg GNUPGHOME=$(abs_builddir) TESTS_ENVIRONMENT = GNUPGHOME=$(GNUPGHOME) 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-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-import.moc AM_LDFLAGS = -no-install LDADD = ../../cpp/src/libgpgmepp.la ../src/libqgpgme.la \ ../../../src/libgpgme.la @GPGME_QT_LIBS@ @GPG_ERROR_LIBS@ \ @GPGME_QTTEST_LIBS@ @LDADD_FOR_TESTS_KLUDGE@ -lstdc++ AM_CPPFLAGS = -I$(top_srcdir)/lang/cpp/src -I$(top_builddir)/src \ @GPG_ERROR_CFLAGS@ @GPGME_QT_CFLAGS@ @GPG_ERROR_CFLAGS@ \ @LIBASSUAN_CFLAGS@ @GPGME_QTTEST_CFLAGS@ -DBUILDING_QGPGME \ -I$(top_srcdir)/lang/qt/src \ -DTOP_SRCDIR="$(top_srcdir)" support_src = t-support.h t-support.cpp t_keylist_SOURCES = t-keylist.cpp $(support_src) t_keylocate_SOURCES = t-keylocate.cpp $(support_src) t_ownertrust_SOURCES = t-ownertrust.cpp $(support_src) t_tofuinfo_SOURCES = t-tofuinfo.cpp $(support_src) t_encrypt_SOURCES = t-encrypt.cpp $(support_src) t_wkspublish_SOURCES = t-wkspublish.cpp $(support_src) t_verify_SOURCES = t-verify.cpp $(support_src) t_various_SOURCES = t-various.cpp $(support_src) t_config_SOURCES = t-config.cpp $(support_src) 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_exportjob_SOURCES = run-exportjob.cpp run_importjob_SOURCES = run-importjob.cpp run_keyformailboxjob_SOURCES = run-keyformailboxjob.cpp nodist_t_keylist_SOURCES = $(moc_files) 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-import run-importjob + t-trustsignatures t-changeexpiryjob t-wkdlookup t-import run-importjob \ + run-exportjob CLEANFILES = secring.gpg pubring.gpg pubring.kbx trustdb.gpg dirmngr.conf \ gpg-agent.conf pubring.kbx~ S.gpg-agent gpg.conf pubring.gpg~ \ random_seed S.gpg-agent .gpg-v21-migrated pubring-stamp $(moc_files) \ gpg.conf tofu.db reader_0.status reader_1.status clean-local: -$(TESTS_ENVIRONMENT) $(top_srcdir)/tests/start-stop-agent --stop -rm -fR private-keys-v1.d crls.d pubring-stamp: $(top_srcdir)/tests/gpg/pubdemo.asc \ $(top_srcdir)/tests/gpg/secdemo.asc -$(TESTS_ENVIRONMENT) gpgconf --kill all echo "ignore-invalid-option allow-loopback-pinentry" > $(abs_builddir)/gpg-agent.conf echo "allow-loopback-pinentry" >> gpg-agent.conf echo "ignore-invalid-option pinentry-mode" > gpg.conf echo "pinentry-mode loopback" >> gpg.conf $(TESTS_ENVIRONMENT) $(GPG) --no-permission-warning \ --import $(top_srcdir)/tests/gpg/pubdemo.asc $(TESTS_ENVIRONMENT) $(GPG) --no-permission-warning \ --passphrase "abc" \ --import $(top_srcdir)/tests/gpg/secdemo.asc touch pubring-stamp .cpp.moc: $(MOC) `test -f '$<' || echo '$(srcdir)/'`$< -o $@ .h.hmoc: $(MOC) `test -f '$<' || echo '$(srcdir)/'`$< -o $@ diff --git a/lang/qt/tests/run-exportjob.cpp b/lang/qt/tests/run-exportjob.cpp new file mode 100644 index 00000000..1a1617da --- /dev/null +++ b/lang/qt/tests/run-exportjob.cpp @@ -0,0 +1,113 @@ +/* + run-exportjob.cpp + + This file is part of QGpgME's test suite. + Copyright (c) 2022 by 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, + version 2, as published by the Free Software Foundation. + + 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 +#include + +#include + +#include + +#include + +using namespace GpgME; +using std::cout; +using std::cerr; + +static void showUsageAndExitWithCode(int exitCode) +{ + cerr << "Usage: run-exportjob [OPTION]... [PATTERN]...\n" + "Options:\n" + " --secret export secret keys instead of public keys\n" + + exit(exitCode); +} + +static auto createExportJob(unsigned int mode) +{ + if (mode & Context::ExportSecret) { + return QGpgME::openpgp()->secretKeyExportJob(/*armor=*/true); + } + return QGpgME::openpgp()->publicKeyExportJob(/*armor=*/true); +} + +int main(int argc, char *argv[]) +{ + GpgME::initializeLibrary(); + + QCoreApplication app{argc, argv}; + + unsigned int exportMode = 0; + + auto arguments = app.arguments(); + if (!arguments.isEmpty()) { + arguments.pop_front(); // remove program name + } + while (!arguments.isEmpty()) { + const auto &arg = arguments.front(); + if (!arg.startsWith(QLatin1String{"--"})) { + break; + } + if (arg == QLatin1String{"--"}) { + arguments.pop_front(); + break; + } + if (arg == QLatin1String{"--help"}) { + showUsageAndExitWithCode(0); + } else if (arg == QLatin1String{"--secret"}) { + exportMode = Context::ExportSecret; + arguments.pop_front(); + } else { + cerr << "Error: Invalid option " << arg.toStdString() << std::endl; + showUsageAndExitWithCode(1); + } + } + + auto job = createExportJob(exportMode); + QObject::connect(job, &QGpgME::ExportJob::result, + &app, [&app] (const GpgME::Error &err, const QByteArray &keyData, const QString &, const GpgME::Error &) { + if (err) { + cerr << "The ChangeExpiryJob failed with" << err.asString() << "."; + app.exit(1); + return; + } + cout << "Begin Result:\n" << keyData.toStdString() << "End Result:\n"; + app.exit(); + }); + job->start(arguments); + + return app.exec(); +}