diff --git a/lang/qt/src/qgpgmedecryptverifyjob.cpp b/lang/qt/src/qgpgmedecryptverifyjob.cpp index 8e9b501a..2a28389e 100644 --- a/lang/qt/src/qgpgmedecryptverifyjob.cpp +++ b/lang/qt/src/qgpgmedecryptverifyjob.cpp @@ -1,214 +1,214 @@ /* qgpgmedecryptverifyjob.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 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 "qgpgmedecryptverifyjob.h" #include "dataprovider.h" #include "decryptverifyjob_p.h" #include "util.h" #include #include #include #include #include #include "qgpgme_debug.h" #include #include #include using namespace QGpgME; using namespace GpgME; namespace { class QGpgMEDecryptVerifyJobPrivate : public DecryptVerifyJobPrivate { QGpgMEDecryptVerifyJob *q = nullptr; public: QGpgMEDecryptVerifyJobPrivate(QGpgMEDecryptVerifyJob *qq) : q{qq} { } ~QGpgMEDecryptVerifyJobPrivate() override = default; private: GpgME::Error startIt() override; void startNow() override { q->run(); } }; } QGpgMEDecryptVerifyJob::QGpgMEDecryptVerifyJob(Context *context) : mixin_type(context) { setJobPrivate(this, std::unique_ptr{new QGpgMEDecryptVerifyJobPrivate{this}}); lateInitialization(); } QGpgMEDecryptVerifyJob::~QGpgMEDecryptVerifyJob() {} static QGpgMEDecryptVerifyJob::result_type decrypt_verify(Context *ctx, QThread *thread, const std::weak_ptr &cipherText_, const std::weak_ptr &plainText_) { qCDebug(QGPGME_LOG) << __func__; const std::shared_ptr cipherText = cipherText_.lock(); const std::shared_ptr plainText = plainText_.lock(); const _detail::ToThreadMover ctMover(cipherText, thread); const _detail::ToThreadMover ptMover(plainText, thread); QGpgME::QIODeviceDataProvider in(cipherText); Data indata(&in); if (!cipherText->isSequential()) { indata.setSizeHint(cipherText->size()); } if (!plainText) { QGpgME::QByteArrayDataProvider out; Data outdata(&out); const std::pair res = ctx->decryptAndVerify(indata, outdata); Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); qCDebug(QGPGME_LOG) << __func__ << "- End no plainText. Error:" << ae.asString(); return std::make_tuple(res.first, res.second, out.data(), log, ae); } else { QGpgME::QIODeviceDataProvider out(plainText); Data outdata(&out); const std::pair res = ctx->decryptAndVerify(indata, outdata); Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); qCDebug(QGPGME_LOG) << __func__ << "- End plainText. Error:" << ae.asString(); return std::make_tuple(res.first, res.second, QByteArray(), log, ae); } } static QGpgMEDecryptVerifyJob::result_type decrypt_verify_qba(Context *ctx, const QByteArray &cipherText) { const std::shared_ptr buffer(new QBuffer); buffer->setData(cipherText); if (!buffer->open(QIODevice::ReadOnly)) { assert(!"This should never happen: QBuffer::open() failed"); } return decrypt_verify(ctx, nullptr, buffer, std::shared_ptr()); } static QGpgMEDecryptVerifyJob::result_type decrypt_verify_from_filename(Context *ctx, const QString &inputFilePath, const QString &outputFilePath) { Data indata; #ifdef Q_OS_WIN - indata.setFileName(inputFilePath().toUtf8().constData()); + indata.setFileName(inputFilePath.toUtf8().constData()); #else indata.setFileName(QFile::encodeName(inputFilePath).constData()); #endif PartialFileGuard partFileGuard{outputFilePath}; if (partFileGuard.tempFileName().isEmpty()) { return std::make_tuple(DecryptionResult{Error::fromCode(GPG_ERR_EEXIST)}, VerificationResult{Error::fromCode(GPG_ERR_EEXIST)}, QByteArray{}, QString{}, Error{}); } Data outdata; #ifdef Q_OS_WIN outdata.setFileName(partFileGuard.tempFileName().toUtf8().constData()); #else outdata.setFileName(QFile::encodeName(partFileGuard.tempFileName()).constData()); #endif const auto results = ctx->decryptAndVerify(indata, outdata); const auto &decryptionResult = results.first; const auto &verificationResult = results.second; if (!decryptionResult.error().code() && !verificationResult.error().code()) { // the operation succeeded -> save the result under the requested file name partFileGuard.commit(); } Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(decryptionResult, verificationResult, QByteArray{}, log, ae); } Error QGpgMEDecryptVerifyJob::start(const QByteArray &cipherText) { run(std::bind(&decrypt_verify_qba, std::placeholders::_1, cipherText)); return Error(); } void QGpgMEDecryptVerifyJob::start(const std::shared_ptr &cipherText, const std::shared_ptr &plainText) { run(std::bind(&decrypt_verify, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4), cipherText, plainText); } std::pair QGpgME::QGpgMEDecryptVerifyJob::exec(const QByteArray &cipherText, QByteArray &plainText) { const result_type r = decrypt_verify_qba(context(), cipherText); plainText = std::get<2>(r); return std::make_pair(std::get<0>(r), std::get<1>(r)); } GpgME::Error QGpgMEDecryptVerifyJobPrivate::startIt() { if (m_inputFilePath.isEmpty() || m_outputFilePath.isEmpty()) { return Error::fromCode(GPG_ERR_INV_VALUE); } q->run([=](Context *ctx) { return decrypt_verify_from_filename(ctx, m_inputFilePath, m_outputFilePath); }); return {}; } #include "qgpgmedecryptverifyjob.moc" diff --git a/lang/qt/src/qgpgmeencryptjob.cpp b/lang/qt/src/qgpgmeencryptjob.cpp index 213c88c7..f8c71f6b 100644 --- a/lang/qt/src/qgpgmeencryptjob.cpp +++ b/lang/qt/src/qgpgmeencryptjob.cpp @@ -1,257 +1,257 @@ /* qgpgmeencryptjob.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 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 "qgpgmeencryptjob.h" #include "dataprovider.h" #include "encryptjob_p.h" #include "util.h" #include #include #include #include #include #include using namespace QGpgME; using namespace GpgME; namespace { class QGpgMEEncryptJobPrivate : public EncryptJobPrivate { QGpgMEEncryptJob *q = nullptr; public: QGpgMEEncryptJobPrivate(QGpgMEEncryptJob *qq) : q{qq} { } ~QGpgMEEncryptJobPrivate() override = default; private: GpgME::Error startIt() override; void startNow() override { q->run(); } }; } QGpgMEEncryptJob::QGpgMEEncryptJob(Context *context) : mixin_type(context), mOutputIsBase64Encoded(false) { setJobPrivate(this, std::unique_ptr{new QGpgMEEncryptJobPrivate{this}}); lateInitialization(); } QGpgMEEncryptJob::~QGpgMEEncryptJob() {} void QGpgMEEncryptJob::setOutputIsBase64Encoded(bool on) { mOutputIsBase64Encoded = on; } static QGpgMEEncryptJob::result_type encrypt(Context *ctx, QThread *thread, const std::vector &recipients, const std::weak_ptr &plainText_, const std::weak_ptr &cipherText_, const Context::EncryptionFlags eflags, bool outputIsBsse64Encoded, Data::Encoding inputEncoding, const QString &fileName) { const std::shared_ptr plainText = plainText_.lock(); const std::shared_ptr cipherText = cipherText_.lock(); const _detail::ToThreadMover ctMover(cipherText, thread); const _detail::ToThreadMover ptMover(plainText, thread); QGpgME::QIODeviceDataProvider in(plainText); Data indata(&in); indata.setEncoding(inputEncoding); if (!plainText->isSequential()) { indata.setSizeHint(plainText->size()); } const auto pureFileName = QFileInfo{fileName}.fileName().toStdString(); if (!pureFileName.empty()) { indata.setFileName(pureFileName.c_str()); } if (!cipherText) { QGpgME::QByteArrayDataProvider out; Data outdata(&out); if (outputIsBsse64Encoded) { outdata.setEncoding(Data::Base64Encoding); } const EncryptionResult res = ctx->encrypt(recipients, indata, outdata, eflags); Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(res, out.data(), log, ae); } else { QGpgME::QIODeviceDataProvider out(cipherText); Data outdata(&out); if (outputIsBsse64Encoded) { outdata.setEncoding(Data::Base64Encoding); } const EncryptionResult res = ctx->encrypt(recipients, indata, outdata, eflags); Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(res, QByteArray(), log, ae); } } static QGpgMEEncryptJob::result_type encrypt_qba(Context *ctx, const std::vector &recipients, const QByteArray &plainText, const Context::EncryptionFlags eflags, bool outputIsBsse64Encoded, Data::Encoding inputEncoding, const QString &fileName) { const std::shared_ptr buffer(new QBuffer); buffer->setData(plainText); if (!buffer->open(QIODevice::ReadOnly)) { assert(!"This should never happen: QBuffer::open() failed"); } return encrypt(ctx, nullptr, recipients, buffer, std::shared_ptr(), eflags, outputIsBsse64Encoded, inputEncoding, fileName); } static QGpgMEEncryptJob::result_type encrypt_to_filename(Context *ctx, const std::vector &recipients, const QString &inputFilePath, const QString &outputFilePath, Context::EncryptionFlags flags) { Data indata; #ifdef Q_OS_WIN - indata.setFileName(inputFilePath().toUtf8().constData()); + indata.setFileName(inputFilePath.toUtf8().constData()); #else indata.setFileName(QFile::encodeName(inputFilePath).constData()); #endif PartialFileGuard partFileGuard{outputFilePath}; if (partFileGuard.tempFileName().isEmpty()) { return std::make_tuple(EncryptionResult{Error::fromCode(GPG_ERR_EEXIST)}, QByteArray{}, QString{}, Error{}); } Data outdata; #ifdef Q_OS_WIN outdata.setFileName(partFileGuard.tempFileName().toUtf8().constData()); #else outdata.setFileName(QFile::encodeName(partFileGuard.tempFileName()).constData()); #endif flags = static_cast(flags | Context::EncryptFile); const auto encryptionResult = ctx->encrypt(recipients, indata, outdata, flags); if (!encryptionResult.error().code()) { // the operation succeeded -> save the result under the requested file name partFileGuard.commit(); } Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(encryptionResult, QByteArray{}, log, ae); } Error QGpgMEEncryptJob::start(const std::vector &recipients, const QByteArray &plainText, bool alwaysTrust) { run(std::bind(&encrypt_qba, std::placeholders::_1, recipients, plainText, alwaysTrust ? Context::AlwaysTrust : Context::None, mOutputIsBase64Encoded, inputEncoding(), fileName())); return Error(); } void QGpgMEEncryptJob::start(const std::vector &recipients, const std::shared_ptr &plainText, const std::shared_ptr &cipherText, const Context::EncryptionFlags eflags) { run(std::bind(&encrypt, std::placeholders::_1, std::placeholders::_2, recipients, std::placeholders::_3, std::placeholders::_4, eflags, mOutputIsBase64Encoded, inputEncoding(), fileName()), plainText, cipherText); } EncryptionResult QGpgMEEncryptJob::exec(const std::vector &recipients, const QByteArray &plainText, const Context::EncryptionFlags eflags, QByteArray &cipherText) { const result_type r = encrypt_qba(context(), recipients, plainText, eflags, mOutputIsBase64Encoded, inputEncoding(), fileName()); cipherText = std::get<1>(r); return std::get<0>(r); } void QGpgMEEncryptJob::start(const std::vector &recipients, const std::shared_ptr &plainText, const std::shared_ptr &cipherText, bool alwaysTrust) { return start(recipients, plainText, cipherText, alwaysTrust ? Context::AlwaysTrust : Context::None); } EncryptionResult QGpgMEEncryptJob::exec(const std::vector &recipients, const QByteArray &plainText, bool alwaysTrust, QByteArray &cipherText) { return exec(recipients, plainText, alwaysTrust ? Context::AlwaysTrust : Context::None, cipherText); } GpgME::Error QGpgMEEncryptJobPrivate::startIt() { if (m_inputFilePath.isEmpty() || m_outputFilePath.isEmpty()) { return Error::fromCode(GPG_ERR_INV_VALUE); } q->run([=](Context *ctx) { return encrypt_to_filename(ctx, m_recipients, m_inputFilePath, m_outputFilePath, m_encryptionFlags); }); return {}; } #include "qgpgmeencryptjob.moc" diff --git a/lang/qt/src/qgpgmesignencryptjob.cpp b/lang/qt/src/qgpgmesignencryptjob.cpp index 45734d8e..8b919d86 100644 --- a/lang/qt/src/qgpgmesignencryptjob.cpp +++ b/lang/qt/src/qgpgmesignencryptjob.cpp @@ -1,266 +1,266 @@ /* qgpgmesignencryptjob.cpp This file is part of qgpgme, the Qt API binding for gpgme Copyright (c) 2004, 2007 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 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 "qgpgmesignencryptjob.h" #include "dataprovider.h" #include "signencryptjob_p.h" #include "util.h" #include #include #include #include #include #include #include using namespace QGpgME; using namespace GpgME; namespace { class QGpgMESignEncryptJobPrivate : public SignEncryptJobPrivate { QGpgMESignEncryptJob *q = nullptr; public: QGpgMESignEncryptJobPrivate(QGpgMESignEncryptJob *qq) : q{qq} { } ~QGpgMESignEncryptJobPrivate() override = default; private: GpgME::Error startIt() override; void startNow() override { q->run(); } }; } QGpgMESignEncryptJob::QGpgMESignEncryptJob(Context *context) : mixin_type(context), mOutputIsBase64Encoded(false) { setJobPrivate(this, std::unique_ptr{new QGpgMESignEncryptJobPrivate{this}}); lateInitialization(); } QGpgMESignEncryptJob::~QGpgMESignEncryptJob() {} void QGpgMESignEncryptJob::setOutputIsBase64Encoded(bool on) { mOutputIsBase64Encoded = on; } static QGpgMESignEncryptJob::result_type sign_encrypt(Context *ctx, QThread *thread, const std::vector &signers, const std::vector &recipients, const std::weak_ptr &plainText_, const std::weak_ptr &cipherText_, const Context::EncryptionFlags eflags, bool outputIsBsse64Encoded, const QString &fileName) { const std::shared_ptr &plainText = plainText_.lock(); const std::shared_ptr &cipherText = cipherText_.lock(); const _detail::ToThreadMover ctMover(cipherText, thread); const _detail::ToThreadMover ptMover(plainText, thread); QGpgME::QIODeviceDataProvider in(plainText); Data indata(&in); if (!plainText->isSequential()) { indata.setSizeHint(plainText->size()); } const auto pureFileName = QFileInfo{fileName}.fileName().toStdString(); if (!pureFileName.empty()) { indata.setFileName(pureFileName.c_str()); } ctx->clearSigningKeys(); for (const Key &signer : signers) { if (!signer.isNull()) { if (const Error err = ctx->addSigningKey(signer)) { return std::make_tuple(SigningResult(err), EncryptionResult(), QByteArray(), QString(), Error()); } } } if (!cipherText) { QGpgME::QByteArrayDataProvider out; Data outdata(&out); if (outputIsBsse64Encoded) { outdata.setEncoding(Data::Base64Encoding); } const std::pair res = ctx->signAndEncrypt(recipients, indata, outdata, eflags); Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(res.first, res.second, out.data(), log, ae); } else { QGpgME::QIODeviceDataProvider out(cipherText); Data outdata(&out); if (outputIsBsse64Encoded) { outdata.setEncoding(Data::Base64Encoding); } const std::pair res = ctx->signAndEncrypt(recipients, indata, outdata, eflags); Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(res.first, res.second, QByteArray(), log, ae); } } static QGpgMESignEncryptJob::result_type sign_encrypt_qba(Context *ctx, const std::vector &signers, const std::vector &recipients, const QByteArray &plainText, const Context::EncryptionFlags eflags, bool outputIsBsse64Encoded, const QString &fileName) { const std::shared_ptr buffer(new QBuffer); buffer->setData(plainText); if (!buffer->open(QIODevice::ReadOnly)) { assert(!"This should never happen: QBuffer::open() failed"); } return sign_encrypt(ctx, nullptr, signers, recipients, buffer, std::shared_ptr(), eflags, outputIsBsse64Encoded, fileName); } static QGpgMESignEncryptJob::result_type sign_encrypt_to_filename(Context *ctx, const std::vector &signers, const std::vector &recipients, const QString &inputFilePath, const QString &outputFilePath, Context::EncryptionFlags flags) { Data indata; #ifdef Q_OS_WIN - indata.setFileName(inputFilePath().toUtf8().constData()); + indata.setFileName(inputFilePath.toUtf8().constData()); #else indata.setFileName(QFile::encodeName(inputFilePath).constData()); #endif PartialFileGuard partFileGuard{outputFilePath}; if (partFileGuard.tempFileName().isEmpty()) { return std::make_tuple(SigningResult{Error::fromCode(GPG_ERR_EEXIST)}, EncryptionResult{Error::fromCode(GPG_ERR_EEXIST)}, QByteArray{}, QString{}, Error{}); } Data outdata; #ifdef Q_OS_WIN outdata.setFileName(partFileGuard.tempFileName().toUtf8().constData()); #else outdata.setFileName(QFile::encodeName(partFileGuard.tempFileName()).constData()); #endif ctx->clearSigningKeys(); for (const Key &signer : signers) { if (!signer.isNull()) { if (const Error err = ctx->addSigningKey(signer)) { return std::make_tuple(SigningResult{err}, EncryptionResult{}, QByteArray{}, QString{}, Error{}); } } } flags = static_cast(flags | Context::EncryptFile); const auto results = ctx->signAndEncrypt(recipients, indata, outdata, flags); const auto &signingResult = results.first; const auto &encryptionResult = results.second; if (!signingResult.error().code() && !encryptionResult.error().code()) { // the operation succeeded -> save the result under the requested file name partFileGuard.commit(); } Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(signingResult, encryptionResult, QByteArray{}, log, ae); } Error QGpgMESignEncryptJob::start(const std::vector &signers, const std::vector &recipients, const QByteArray &plainText, bool alwaysTrust) { run(std::bind(&sign_encrypt_qba, std::placeholders::_1, signers, recipients, plainText, alwaysTrust ? Context::AlwaysTrust : Context::None, mOutputIsBase64Encoded, fileName())); return Error(); } void QGpgMESignEncryptJob::start(const std::vector &signers, const std::vector &recipients, const std::shared_ptr &plainText, const std::shared_ptr &cipherText, const Context::EncryptionFlags eflags) { run(std::bind(&sign_encrypt, std::placeholders::_1, std::placeholders::_2, signers, recipients, std::placeholders::_3, std::placeholders::_4, eflags, mOutputIsBase64Encoded, fileName()), plainText, cipherText); } void QGpgMESignEncryptJob::start(const std::vector &signers, const std::vector &recipients, const std::shared_ptr &plainText, const std::shared_ptr &cipherText, bool alwaysTrust) { return start(signers, recipients, plainText, cipherText, alwaysTrust ? Context::AlwaysTrust : Context::None); } std::pair QGpgMESignEncryptJob::exec(const std::vector &signers, const std::vector &recipients, const QByteArray &plainText, const Context::EncryptionFlags eflags, QByteArray &cipherText) { const result_type r = sign_encrypt_qba(context(), signers, recipients, plainText, eflags, mOutputIsBase64Encoded, fileName()); cipherText = std::get<2>(r); return std::make_pair(std::get<0>(r), std::get<1>(r)); } std::pair QGpgMESignEncryptJob::exec(const std::vector &signers, const std::vector &recipients, const QByteArray &plainText, bool alwaysTrust, QByteArray &cipherText) { return exec(signers, recipients, plainText, alwaysTrust ? Context::AlwaysTrust : Context::None, cipherText); } GpgME::Error QGpgMESignEncryptJobPrivate::startIt() { if (m_inputFilePath.isEmpty() || m_outputFilePath.isEmpty()) { return Error::fromCode(GPG_ERR_INV_VALUE); } q->run([=](Context *ctx) { return sign_encrypt_to_filename(ctx, m_signers, m_recipients, m_inputFilePath, m_outputFilePath, m_encryptionFlags); }); return {}; } #include "qgpgmesignencryptjob.moc" diff --git a/lang/qt/src/qgpgmesignjob.cpp b/lang/qt/src/qgpgmesignjob.cpp index dc2516ca..76e60e72 100644 --- a/lang/qt/src/qgpgmesignjob.cpp +++ b/lang/qt/src/qgpgmesignjob.cpp @@ -1,250 +1,250 @@ /* qgpgmesignjob.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 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 "qgpgmesignjob.h" #include "dataprovider.h" #include "signjob_p.h" #include "util.h" #include #include #include #include #include #include using namespace QGpgME; using namespace GpgME; namespace { class QGpgMESignJobPrivate : public SignJobPrivate { QGpgMESignJob *q = nullptr; public: QGpgMESignJobPrivate(QGpgMESignJob *qq) : q{qq} { } ~QGpgMESignJobPrivate() override = default; private: GpgME::Error startIt() override; void startNow() override { q->run(); } }; } QGpgMESignJob::QGpgMESignJob(Context *context) : mixin_type(context), mOutputIsBase64Encoded(false) { setJobPrivate(this, std::unique_ptr{new QGpgMESignJobPrivate{this}}); lateInitialization(); } QGpgMESignJob::~QGpgMESignJob() {} void QGpgMESignJob::setOutputIsBase64Encoded(bool on) { mOutputIsBase64Encoded = on; } static QGpgMESignJob::result_type sign(Context *ctx, QThread *thread, const std::vector &signers, const std::weak_ptr &plainText_, const std::weak_ptr &signature_, SignatureMode mode, bool outputIsBsse64Encoded) { const std::shared_ptr plainText = plainText_.lock(); const std::shared_ptr signature = signature_.lock(); const _detail::ToThreadMover ptMover(plainText, thread); const _detail::ToThreadMover sgMover(signature, thread); QGpgME::QIODeviceDataProvider in(plainText); Data indata(&in); if (!plainText->isSequential()) { indata.setSizeHint(plainText->size()); } ctx->clearSigningKeys(); for (const Key &signer : signers) { if (!signer.isNull()) { if (const Error err = ctx->addSigningKey(signer)) { return std::make_tuple(SigningResult(err), QByteArray(), QString(), Error()); } } } if (!signature) { QGpgME::QByteArrayDataProvider out; Data outdata(&out); if (outputIsBsse64Encoded) { outdata.setEncoding(Data::Base64Encoding); } const SigningResult res = ctx->sign(indata, outdata, mode); Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(res, out.data(), log, ae); } else { QGpgME::QIODeviceDataProvider out(signature); Data outdata(&out); if (outputIsBsse64Encoded) { outdata.setEncoding(Data::Base64Encoding); } const SigningResult res = ctx->sign(indata, outdata, mode); Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(res, QByteArray(), log, ae); } } static QGpgMESignJob::result_type sign_qba(Context *ctx, const std::vector &signers, const QByteArray &plainText, SignatureMode mode, bool outputIsBsse64Encoded) { const std::shared_ptr buffer(new QBuffer); buffer->setData(plainText); if (!buffer->open(QIODevice::ReadOnly)) { assert(!"This should never happen: QBuffer::open() failed"); } return sign(ctx, nullptr, signers, buffer, std::shared_ptr(), mode, outputIsBsse64Encoded); } static QGpgMESignJob::result_type sign_to_filename(Context *ctx, const std::vector &signers, const QString &inputFilePath, const QString &outputFilePath, SignatureMode flags) { Data indata; #ifdef Q_OS_WIN - indata.setFileName(inputFilePath().toUtf8().constData()); + indata.setFileName(inputFilePath.toUtf8().constData()); #else indata.setFileName(QFile::encodeName(inputFilePath).constData()); #endif PartialFileGuard partFileGuard{outputFilePath}; if (partFileGuard.tempFileName().isEmpty()) { return std::make_tuple(SigningResult{Error::fromCode(GPG_ERR_EEXIST)}, QByteArray{}, QString{}, Error{}); } Data outdata; #ifdef Q_OS_WIN outdata.setFileName(partFileGuard.tempFileName().toUtf8().constData()); #else outdata.setFileName(QFile::encodeName(partFileGuard.tempFileName()).constData()); #endif ctx->clearSigningKeys(); for (const Key &signer : signers) { if (!signer.isNull()) { if (const Error err = ctx->addSigningKey(signer)) { return std::make_tuple(SigningResult{err}, QByteArray{}, QString{}, Error{}); } } } flags = static_cast(flags | SignFile); const auto signingResult = ctx->sign(indata, outdata, flags); if (!signingResult.error().code()) { // the operation succeeded -> save the result under the requested file name partFileGuard.commit(); } Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(signingResult, QByteArray{}, log, ae); } Error QGpgMESignJob::start(const std::vector &signers, const QByteArray &plainText, SignatureMode mode) { run(std::bind(&sign_qba, std::placeholders::_1, signers, plainText, mode, mOutputIsBase64Encoded)); return Error(); } void QGpgMESignJob::start(const std::vector &signers, const std::shared_ptr &plainText, const std::shared_ptr &signature, SignatureMode mode) { run(std::bind(&sign, std::placeholders::_1, std::placeholders::_2, signers, std::placeholders::_3, std::placeholders::_4, mode, mOutputIsBase64Encoded), plainText, signature); } SigningResult QGpgMESignJob::exec(const std::vector &signers, const QByteArray &plainText, SignatureMode mode, QByteArray &signature) { const result_type r = sign_qba(context(), signers, plainText, mode, mOutputIsBase64Encoded); signature = std::get<1>(r); return std::get<0>(r); } GpgME::Error QGpgMESignJobPrivate::startIt() { if (m_inputFilePath.isEmpty() || m_outputFilePath.isEmpty()) { return Error::fromCode(GPG_ERR_INV_VALUE); } q->run([=](Context *ctx) { return sign_to_filename(ctx, m_signers, m_inputFilePath, m_outputFilePath, m_signingFlags); }); return {}; } #include "qgpgmesignjob.moc" diff --git a/lang/qt/src/qgpgmeverifydetachedjob.cpp b/lang/qt/src/qgpgmeverifydetachedjob.cpp index 0a58f59e..467b94f3 100644 --- a/lang/qt/src/qgpgmeverifydetachedjob.cpp +++ b/lang/qt/src/qgpgmeverifydetachedjob.cpp @@ -1,188 +1,188 @@ /* qgpgmeverifydetachedjob.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 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 "qgpgmeverifydetachedjob.h" #include "dataprovider.h" #include "util.h" #include "verifydetachedjob_p.h" #include #include #include #include #include using namespace QGpgME; using namespace GpgME; namespace { class QGpgMEVerifyDetachedJobPrivate : public VerifyDetachedJobPrivate { QGpgMEVerifyDetachedJob *q = nullptr; public: QGpgMEVerifyDetachedJobPrivate(QGpgMEVerifyDetachedJob *qq) : q{qq} { } ~QGpgMEVerifyDetachedJobPrivate() override = default; private: GpgME::Error startIt() override; void startNow() override { q->run(); } }; } QGpgMEVerifyDetachedJob::QGpgMEVerifyDetachedJob(Context *context) : mixin_type(context) { setJobPrivate(this, std::unique_ptr{new QGpgMEVerifyDetachedJobPrivate{this}}); lateInitialization(); } QGpgMEVerifyDetachedJob::~QGpgMEVerifyDetachedJob() {} static QGpgMEVerifyDetachedJob::result_type verify_detached(Context *ctx, QThread *thread, const std::weak_ptr &signature_, const std::weak_ptr &signedData_) { const std::shared_ptr signature = signature_.lock(); const std::shared_ptr signedData = signedData_.lock(); const _detail::ToThreadMover sgMover(signature, thread); const _detail::ToThreadMover sdMover(signedData, thread); QGpgME::QIODeviceDataProvider sigDP(signature); Data sig(&sigDP); QGpgME::QIODeviceDataProvider dataDP(signedData); Data data(&dataDP); if (!signedData->isSequential()) { data.setSizeHint(signedData->size()); } const VerificationResult res = ctx->verifyDetachedSignature(sig, data); Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(res, log, ae); } static QGpgMEVerifyDetachedJob::result_type verify_detached_qba(Context *ctx, const QByteArray &signature, const QByteArray &signedData) { QGpgME::QByteArrayDataProvider sigDP(signature); Data sig(&sigDP); QGpgME::QByteArrayDataProvider dataDP(signedData); Data data(&dataDP); const VerificationResult res = ctx->verifyDetachedSignature(sig, data); Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(res, log, ae); } static QGpgMEVerifyDetachedJob::result_type verify_from_filename(Context *ctx, const QString &signatureFilePath, const QString &signedFilePath) { Data signatureData; #ifdef Q_OS_WIN - signatureData.setFileName(signatureFilePath().toUtf8().constData()); + signatureData.setFileName(signatureFilePath.toUtf8().constData()); #else signatureData.setFileName(QFile::encodeName(signatureFilePath).constData()); #endif Data signedData; #ifdef Q_OS_WIN - signedData.setFileName(signedFilePath().toUtf8().constData()); + signedData.setFileName(signedFilePath.toUtf8().constData()); #else signedData.setFileName(QFile::encodeName(signedFilePath).constData()); #endif const auto verificationResult = ctx->verifyDetachedSignature(signatureData, signedData); Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(verificationResult, log, ae); } Error QGpgMEVerifyDetachedJob::start(const QByteArray &signature, const QByteArray &signedData) { run(std::bind(&verify_detached_qba, std::placeholders::_1, signature, signedData)); return Error(); } void QGpgMEVerifyDetachedJob::start(const std::shared_ptr &signature, const std::shared_ptr &signedData) { run(std::bind(&verify_detached, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4), signature, signedData); } GpgME::VerificationResult QGpgME::QGpgMEVerifyDetachedJob::exec(const QByteArray &signature, const QByteArray &signedData) { const result_type r = verify_detached_qba(context(), signature, signedData); return std::get<0>(r); } GpgME::Error QGpgMEVerifyDetachedJobPrivate::startIt() { if (m_signatureFilePath.isEmpty() || m_signedFilePath.isEmpty()) { return Error::fromCode(GPG_ERR_INV_VALUE); } q->run([=](Context *ctx) { return verify_from_filename(ctx, m_signatureFilePath, m_signedFilePath); }); return {}; } #include "qgpgmeverifydetachedjob.moc" diff --git a/lang/qt/src/qgpgmeverifyopaquejob.cpp b/lang/qt/src/qgpgmeverifyopaquejob.cpp index 3a94b8b3..a3a022de 100644 --- a/lang/qt/src/qgpgmeverifyopaquejob.cpp +++ b/lang/qt/src/qgpgmeverifyopaquejob.cpp @@ -1,203 +1,203 @@ /* qgpgmeverifyopaquejob.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 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 "qgpgmeverifyopaquejob.h" #include "dataprovider.h" #include "util.h" #include "verifyopaquejob_p.h" #include #include #include #include #include #include using namespace QGpgME; using namespace GpgME; namespace { class QGpgMEVerifyOpaqueJobPrivate : public VerifyOpaqueJobPrivate { QGpgMEVerifyOpaqueJob *q = nullptr; public: QGpgMEVerifyOpaqueJobPrivate(QGpgMEVerifyOpaqueJob *qq) : q{qq} { } ~QGpgMEVerifyOpaqueJobPrivate() override = default; private: GpgME::Error startIt() override; void startNow() override { q->run(); } }; } QGpgMEVerifyOpaqueJob::QGpgMEVerifyOpaqueJob(Context *context) : mixin_type(context) { setJobPrivate(this, std::unique_ptr{new QGpgMEVerifyOpaqueJobPrivate{this}}); lateInitialization(); } QGpgMEVerifyOpaqueJob::~QGpgMEVerifyOpaqueJob() {} static QGpgMEVerifyOpaqueJob::result_type verify_opaque(Context *ctx, QThread *thread, const std::weak_ptr &signedData_, const std::weak_ptr &plainText_) { const std::shared_ptr plainText = plainText_.lock(); const std::shared_ptr signedData = signedData_.lock(); const _detail::ToThreadMover ptMover(plainText, thread); const _detail::ToThreadMover sdMover(signedData, thread); QGpgME::QIODeviceDataProvider in(signedData); Data indata(&in); if (!signedData->isSequential()) { indata.setSizeHint(signedData->size()); } if (!plainText) { QGpgME::QByteArrayDataProvider out; Data outdata(&out); const VerificationResult res = ctx->verifyOpaqueSignature(indata, outdata); Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(res, out.data(), log, ae); } else { QGpgME::QIODeviceDataProvider out(plainText); Data outdata(&out); const VerificationResult res = ctx->verifyOpaqueSignature(indata, outdata); Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(res, QByteArray(), log, ae); } } static QGpgMEVerifyOpaqueJob::result_type verify_opaque_qba(Context *ctx, const QByteArray &signedData) { const std::shared_ptr buffer(new QBuffer); buffer->setData(signedData); if (!buffer->open(QIODevice::ReadOnly)) { assert(!"This should never happen: QBuffer::open() failed"); } return verify_opaque(ctx, nullptr, buffer, std::shared_ptr()); } static QGpgMEVerifyOpaqueJob::result_type verify_from_filename(Context *ctx, const QString &inputFilePath, const QString &outputFilePath) { Data indata; #ifdef Q_OS_WIN - indata.setFileName(inputFilePath().toUtf8().constData()); + indata.setFileName(inputFilePath.toUtf8().constData()); #else indata.setFileName(QFile::encodeName(inputFilePath).constData()); #endif PartialFileGuard partFileGuard{outputFilePath}; if (partFileGuard.tempFileName().isEmpty()) { return std::make_tuple(VerificationResult{Error::fromCode(GPG_ERR_EEXIST)}, QByteArray{}, QString{}, Error{}); } Data outdata; #ifdef Q_OS_WIN outdata.setFileName(partFileGuard.tempFileName().toUtf8().constData()); #else outdata.setFileName(QFile::encodeName(partFileGuard.tempFileName()).constData()); #endif const auto verificationResult = ctx->verifyOpaqueSignature(indata, outdata); if (!verificationResult.error().code()) { // the operation succeeded -> save the result under the requested file name partFileGuard.commit(); } Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(verificationResult, QByteArray{}, log, ae); } Error QGpgMEVerifyOpaqueJob::start(const QByteArray &signedData) { run(std::bind(&verify_opaque_qba, std::placeholders::_1, signedData)); return Error(); } void QGpgMEVerifyOpaqueJob::start(const std::shared_ptr &signedData, const std::shared_ptr &plainText) { run(std::bind(&verify_opaque, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4), signedData, plainText); } GpgME::VerificationResult QGpgME::QGpgMEVerifyOpaqueJob::exec(const QByteArray &signedData, QByteArray &plainText) { const result_type r = verify_opaque_qba(context(), signedData); plainText = std::get<1>(r); return std::get<0>(r); } GpgME::Error QGpgMEVerifyOpaqueJobPrivate::startIt() { if (m_inputFilePath.isEmpty() || m_outputFilePath.isEmpty()) { return Error::fromCode(GPG_ERR_INV_VALUE); } q->run([=](Context *ctx) { return verify_from_filename(ctx, m_inputFilePath, m_outputFilePath); }); return {}; } #include "qgpgmeverifyopaquejob.moc"