Blame lang/qt/src/qgpgmeencryptjob.cpp

Packit Service 672cf4
/*
Packit Service 672cf4
    qgpgmeencryptjob.cpp
Packit Service 672cf4
Packit Service 672cf4
    This file is part of qgpgme, the Qt API binding for gpgme
Packit Service 672cf4
    Copyright (c) 2004,2007,2008 Klarälvdalens Datakonsult AB
Packit Service 672cf4
    Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik
Packit Service 672cf4
    Software engineering by Intevation GmbH
Packit Service 672cf4
Packit Service 672cf4
    QGpgME is free software; you can redistribute it and/or
Packit Service 672cf4
    modify it under the terms of the GNU General Public License as
Packit Service 672cf4
    published by the Free Software Foundation; either version 2 of the
Packit Service 672cf4
    License, or (at your option) any later version.
Packit Service 672cf4
Packit Service 672cf4
    QGpgME is distributed in the hope that it will be useful,
Packit Service 672cf4
    but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 672cf4
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 672cf4
    General Public License for more details.
Packit Service 672cf4
Packit Service 672cf4
    You should have received a copy of the GNU General Public License along
Packit Service 672cf4
    with this program; if not, write to the Free Software Foundation, Inc.,
Packit Service 672cf4
    51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit Service 672cf4
Packit Service 672cf4
    In addition, as a special exception, the copyright holders give
Packit Service 672cf4
    permission to link the code of this program with any edition of
Packit Service 672cf4
    the Qt library by Trolltech AS, Norway (or with modified versions
Packit Service 672cf4
    of Qt that use the same license as Qt), and distribute linked
Packit Service 672cf4
    combinations including the two.  You must obey the GNU General
Packit Service 672cf4
    Public License in all respects for all of the code used other than
Packit Service 672cf4
    Qt.  If you modify this file, you may extend this exception to
Packit Service 672cf4
    your version of the file, but you are not obligated to do so.  If
Packit Service 672cf4
    you do not wish to do so, delete this exception statement from
Packit Service 672cf4
    your version.
Packit Service 672cf4
*/
Packit Service 672cf4
Packit Service 672cf4
#ifdef HAVE_CONFIG_H
Packit Service 672cf4
 #include "config.h"
Packit Service 672cf4
#endif
Packit Service 672cf4
Packit Service 672cf4
#include "qgpgmeencryptjob.h"
Packit Service 672cf4
Packit Service 672cf4
#include "dataprovider.h"
Packit Service 672cf4
Packit Service 672cf4
#include "context.h"
Packit Service 672cf4
#include "encryptionresult.h"
Packit Service 672cf4
#include "data.h"
Packit Service 672cf4
Packit Service 672cf4
#include <QBuffer>
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
#include <cassert>
Packit Service 672cf4
Packit Service 672cf4
using namespace QGpgME;
Packit Service 672cf4
using namespace GpgME;
Packit Service 672cf4
Packit Service 672cf4
QGpgMEEncryptJob::QGpgMEEncryptJob(Context *context)
Packit Service 672cf4
    : mixin_type(context),
Packit Service 672cf4
      mOutputIsBase64Encoded(false)
Packit Service 672cf4
{
Packit Service 672cf4
    lateInitialization();
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
QGpgMEEncryptJob::~QGpgMEEncryptJob() {}
Packit Service 672cf4
Packit Service 672cf4
void QGpgMEEncryptJob::setOutputIsBase64Encoded(bool on)
Packit Service 672cf4
{
Packit Service 672cf4
    mOutputIsBase64Encoded = on;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
static QGpgMEEncryptJob::result_type encrypt(Context *ctx, QThread *thread,
Packit Service 672cf4
        const std::vector<Key> &recipients,
Packit Service 672cf4
        const std::weak_ptr<QIODevice> &plainText_,
Packit Service 672cf4
        const std::weak_ptr<QIODevice> &cipherText_,
Packit Service 672cf4
        const Context::EncryptionFlags eflags,
Packit Service 672cf4
        bool outputIsBsse64Encoded)
Packit Service 672cf4
{
Packit Service 672cf4
Packit Service 672cf4
    const std::shared_ptr<QIODevice> plainText = plainText_.lock();
Packit Service 672cf4
    const std::shared_ptr<QIODevice> cipherText = cipherText_.lock();
Packit Service 672cf4
Packit Service 672cf4
    const _detail::ToThreadMover ctMover(cipherText, thread);
Packit Service 672cf4
    const _detail::ToThreadMover ptMover(plainText,  thread);
Packit Service 672cf4
Packit Service 672cf4
    QGpgME::QIODeviceDataProvider in(plainText);
Packit Service 672cf4
    const Data indata(&in);
Packit Service 672cf4
Packit Service 672cf4
    if (!cipherText) {
Packit Service 672cf4
        QGpgME::QByteArrayDataProvider out;
Packit Service 672cf4
        Data outdata(&out;;
Packit Service 672cf4
Packit Service 672cf4
        if (outputIsBsse64Encoded) {
Packit Service 672cf4
            outdata.setEncoding(Data::Base64Encoding);
Packit Service 672cf4
        }
Packit Service 672cf4
Packit Service 672cf4
        const EncryptionResult res = ctx->encrypt(recipients, indata, outdata, eflags);
Packit Service 672cf4
        Error ae;
Packit Service 672cf4
        const QString log = _detail::audit_log_as_html(ctx, ae);
Packit Service 672cf4
        return std::make_tuple(res, out.data(), log, ae);
Packit Service 672cf4
    } else {
Packit Service 672cf4
        QGpgME::QIODeviceDataProvider out(cipherText);
Packit Service 672cf4
        Data outdata(&out;;
Packit Service 672cf4
Packit Service 672cf4
        if (outputIsBsse64Encoded) {
Packit Service 672cf4
            outdata.setEncoding(Data::Base64Encoding);
Packit Service 672cf4
        }
Packit Service 672cf4
Packit Service 672cf4
        const EncryptionResult res = ctx->encrypt(recipients, indata, outdata, eflags);
Packit Service 672cf4
        Error ae;
Packit Service 672cf4
        const QString log = _detail::audit_log_as_html(ctx, ae);
Packit Service 672cf4
        return std::make_tuple(res, QByteArray(), log, ae);
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
static QGpgMEEncryptJob::result_type encrypt_qba(Context *ctx, const std::vector<Key> &recipients, const QByteArray &plainText, const Context::EncryptionFlags eflags, bool outputIsBsse64Encoded)
Packit Service 672cf4
{
Packit Service 672cf4
    const std::shared_ptr<QBuffer> buffer(new QBuffer);
Packit Service 672cf4
    buffer->setData(plainText);
Packit Service 672cf4
    if (!buffer->open(QIODevice::ReadOnly)) {
Packit Service 672cf4
        assert(!"This should never happen: QBuffer::open() failed");
Packit Service 672cf4
    }
Packit Service 6c01f9
    return encrypt(ctx, 0, recipients, buffer, std::shared_ptr<QIODevice>(), eflags, outputIsBsse64Encoded);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Error QGpgMEEncryptJob::start(const std::vector<Key> &recipients, const QByteArray &plainText, bool alwaysTrust)
Packit Service 672cf4
{
Packit Service 672cf4
    run(std::bind(&encrypt_qba, std::placeholders::_1, recipients, plainText,
Packit Service 672cf4
                  alwaysTrust ? Context::AlwaysTrust : Context::None, mOutputIsBase64Encoded));
Packit Service 672cf4
    return Error();
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
void QGpgMEEncryptJob::start(const std::vector<Key> &recipients, const std::shared_ptr<QIODevice> &plainText,
Packit Service 672cf4
                             const std::shared_ptr<QIODevice> &cipherText, const Context::EncryptionFlags eflags)
Packit Service 672cf4
{
Packit Service 672cf4
    run(std::bind(&encrypt,
Packit Service 672cf4
                    std::placeholders::_1, std::placeholders::_2,
Packit Service 672cf4
                    recipients,
Packit Service 672cf4
                    std::placeholders::_3, std::placeholders::_4,
Packit Service 672cf4
                    eflags,
Packit Service 672cf4
                    mOutputIsBase64Encoded),
Packit Service 672cf4
        plainText, cipherText);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
EncryptionResult QGpgMEEncryptJob::exec(const std::vector<Key> &recipients, const QByteArray &plainText,
Packit Service 672cf4
                                        const Context::EncryptionFlags eflags, QByteArray &cipherText)
Packit Service 672cf4
{
Packit Service 672cf4
    const result_type r = encrypt_qba(context(), recipients, plainText, eflags, mOutputIsBase64Encoded);
Packit Service 672cf4
    cipherText = std::get<1>(r);
Packit Service 672cf4
    resultHook(r);
Packit Service 672cf4
    return mResult;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
void QGpgMEEncryptJob::start(const std::vector<Key> &recipients, const std::shared_ptr<QIODevice> &plainText, const std::shared_ptr<QIODevice> &cipherText, bool alwaysTrust)
Packit Service 672cf4
{
Packit Service 672cf4
    return start(recipients, plainText, cipherText, alwaysTrust ? Context::AlwaysTrust : Context::None);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
EncryptionResult QGpgMEEncryptJob::exec(const std::vector<Key> &recipients, const QByteArray &plainText, bool alwaysTrust, QByteArray &cipherText)
Packit Service 672cf4
{
Packit Service 672cf4
    return exec(recipients, plainText, alwaysTrust ? Context::AlwaysTrust : Context::None, cipherText);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
void QGpgMEEncryptJob::resultHook(const result_type &tuple)
Packit Service 672cf4
{
Packit Service 672cf4
    mResult = std::get<0>(tuple);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
#if 0
Packit Service 672cf4
void QGpgMEEncryptJob::showErrorDialog(QWidget *parent, const QString &caption) const
Packit Service 672cf4
{
Packit Service 672cf4
    if (mResult.error() && !mResult.error().isCanceled()) {
Packit Service 672cf4
        MessageBox::error(parent, mResult, this, caption);
Packit Service 672cf4
    }
Packit Service 672cf4
}
Packit Service 672cf4
#endif
Packit Service 672cf4
#include "qgpgmeencryptjob.moc"