LCOV - code coverage report
Current view: top level - lang/qt/src - qgpgmesignencryptjob.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 37 61 60.7 %
Date: 2018-11-14 16:53:58 Functions: 7 12 58.3 %

          Line data    Source code
       1             : /*
       2             :     qgpgmesignencryptjob.cpp
       3             : 
       4             :     This file is part of qgpgme, the Qt API binding for gpgme
       5             :     Copyright (c) 2004, 2007 Klarälvdalens Datakonsult AB
       6             :     Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik
       7             :     Software engineering by Intevation GmbH
       8             : 
       9             :     QGpgME is free software; you can redistribute it and/or
      10             :     modify it under the terms of the GNU General Public License as
      11             :     published by the Free Software Foundation; either version 2 of the
      12             :     License, or (at your option) any later version.
      13             : 
      14             :     QGpgME is distributed in the hope that it will be useful,
      15             :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17             :     General Public License for more details.
      18             : 
      19             :     You should have received a copy of the GNU General Public License along
      20             :     with this program; if not, write to the Free Software Foundation, Inc.,
      21             :     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
      22             : 
      23             :     In addition, as a special exception, the copyright holders give
      24             :     permission to link the code of this program with any edition of
      25             :     the Qt library by Trolltech AS, Norway (or with modified versions
      26             :     of Qt that use the same license as Qt), and distribute linked
      27             :     combinations including the two.  You must obey the GNU General
      28             :     Public License in all respects for all of the code used other than
      29             :     Qt.  If you modify this file, you may extend this exception to
      30             :     your version of the file, but you are not obligated to do so.  If
      31             :     you do not wish to do so, delete this exception statement from
      32             :     your version.
      33             : */
      34             : 
      35             : #ifdef HAVE_CONFIG_H
      36             :  #include "config.h"
      37             : #endif
      38             : 
      39             : #include "qgpgmesignencryptjob.h"
      40             : 
      41             : #include "dataprovider.h"
      42             : 
      43             : #include "context.h"
      44             : #include "data.h"
      45             : #include "key.h"
      46             : #include "exception.h"
      47             : 
      48             : #include <QBuffer>
      49             : 
      50             : 
      51             : #include <cassert>
      52             : 
      53             : using namespace QGpgME;
      54             : using namespace GpgME;
      55             : 
      56           1 : QGpgMESignEncryptJob::QGpgMESignEncryptJob(Context *context)
      57             :     : mixin_type(context),
      58           1 :       mOutputIsBase64Encoded(false)
      59             : {
      60           1 :     lateInitialization();
      61           1 : }
      62             : 
      63           2 : QGpgMESignEncryptJob::~QGpgMESignEncryptJob() {}
      64             : 
      65           0 : void QGpgMESignEncryptJob::setOutputIsBase64Encoded(bool on)
      66             : {
      67           0 :     mOutputIsBase64Encoded = on;
      68           0 : }
      69             : 
      70           1 : static QGpgMESignEncryptJob::result_type sign_encrypt(Context *ctx, QThread *thread, const std::vector<Key> &signers,
      71             :                                                       const std::vector<Key> &recipients, const std::weak_ptr<QIODevice> &plainText_,
      72             :                                                       const std::weak_ptr<QIODevice> &cipherText_, const Context::EncryptionFlags eflags, bool outputIsBsse64Encoded)
      73             : {
      74           2 :     const std::shared_ptr<QIODevice> &plainText = plainText_.lock();
      75           2 :     const std::shared_ptr<QIODevice> &cipherText = cipherText_.lock();
      76             : 
      77           2 :     const _detail::ToThreadMover ctMover(cipherText, thread);
      78           2 :     const _detail::ToThreadMover ptMover(plainText, thread);
      79             : 
      80           2 :     QGpgME::QIODeviceDataProvider in(plainText);
      81           2 :     const Data indata(&in);
      82             : 
      83           1 :     ctx->clearSigningKeys();
      84           2 :     Q_FOREACH (const Key &signer, signers)
      85           1 :         if (!signer.isNull())
      86           2 :             if (const Error err = ctx->addSigningKey(signer)) {
      87           0 :                 return std::make_tuple(SigningResult(err), EncryptionResult(), QByteArray(), QString(), Error());
      88             :             }
      89             : 
      90           1 :     if (!cipherText) {
      91           2 :         QGpgME::QByteArrayDataProvider out;
      92           2 :         Data outdata(&out);
      93             : 
      94           1 :         if (outputIsBsse64Encoded) {
      95           0 :             outdata.setEncoding(Data::Base64Encoding);
      96             :         }
      97             : 
      98           2 :         const std::pair<SigningResult, EncryptionResult> res = ctx->signAndEncrypt(recipients, indata, outdata, eflags);
      99           2 :         Error ae;
     100           2 :         const QString log = _detail::audit_log_as_html(ctx, ae);
     101           1 :         return std::make_tuple(res.first, res.second, out.data(), log, ae);
     102             :     } else {
     103           0 :         QGpgME::QIODeviceDataProvider out(cipherText);
     104           0 :         Data outdata(&out);
     105             : 
     106           0 :         if (outputIsBsse64Encoded) {
     107           0 :             outdata.setEncoding(Data::Base64Encoding);
     108             :         }
     109             : 
     110           0 :         const std::pair<SigningResult, EncryptionResult> res = ctx->signAndEncrypt(recipients, indata, outdata, eflags);
     111           0 :         Error ae;
     112           0 :         const QString log = _detail::audit_log_as_html(ctx, ae);
     113           0 :         return std::make_tuple(res.first, res.second, QByteArray(), log, ae);
     114             :     }
     115             : 
     116             : }
     117             : 
     118           1 : static QGpgMESignEncryptJob::result_type sign_encrypt_qba(Context *ctx, const std::vector<Key> &signers,
     119             :                                                           const std::vector<Key> &recipients, const QByteArray &plainText, const Context::EncryptionFlags eflags, bool outputIsBsse64Encoded)
     120             : {
     121           2 :     const std::shared_ptr<QBuffer> buffer(new QBuffer);
     122           1 :     buffer->setData(plainText);
     123           1 :     if (!buffer->open(QIODevice::ReadOnly)) {
     124           0 :         assert(!"This should never happen: QBuffer::open() failed");
     125             :     }
     126           2 :     return sign_encrypt(ctx, 0, signers, recipients, buffer, std::shared_ptr<QIODevice>(), eflags, outputIsBsse64Encoded);
     127             : }
     128             : 
     129           0 : Error QGpgMESignEncryptJob::start(const std::vector<Key> &signers, const std::vector<Key> &recipients, const QByteArray &plainText, bool alwaysTrust)
     130             : {
     131           0 :     run(std::bind(&sign_encrypt_qba, std::placeholders::_1, signers, recipients, plainText, alwaysTrust ? Context::AlwaysTrust : Context::None, mOutputIsBase64Encoded));
     132           0 :     return Error();
     133             : }
     134             : 
     135           0 : void QGpgMESignEncryptJob::start(const std::vector<Key> &signers, const std::vector<Key> &recipients,
     136             :                                  const std::shared_ptr<QIODevice> &plainText, const std::shared_ptr<QIODevice> &cipherText, const Context::EncryptionFlags eflags)
     137             : {
     138           0 :     run(std::bind(&sign_encrypt, std::placeholders::_1, std::placeholders::_2, signers, recipients, std::placeholders::_3, std::placeholders::_4, eflags, mOutputIsBase64Encoded), plainText, cipherText);
     139           0 : }
     140             : 
     141           0 : void QGpgMESignEncryptJob::start(const std::vector<Key> &signers, const std::vector<Key> &recipients, const std::shared_ptr<QIODevice> &plainText, const std::shared_ptr<QIODevice> &cipherText, bool alwaysTrust)
     142             : {
     143           0 :     return start(signers, recipients, plainText, cipherText, alwaysTrust ? Context::AlwaysTrust : Context::None);
     144             : }
     145             : 
     146           1 : std::pair<SigningResult, EncryptionResult> QGpgMESignEncryptJob::exec(const std::vector<Key> &signers, const std::vector<Key> &recipients, const QByteArray &plainText, const Context::EncryptionFlags eflags, QByteArray &cipherText)
     147             : {
     148           2 :     const result_type r = sign_encrypt_qba(context(), signers, recipients, plainText, eflags, mOutputIsBase64Encoded);
     149           1 :     cipherText = std::get<2>(r);
     150           1 :     resultHook(r);
     151           2 :     return mResult;
     152             : }
     153             : 
     154           0 : std::pair<SigningResult, EncryptionResult> QGpgMESignEncryptJob::exec(const std::vector<Key> &signers, const std::vector<Key> &recipients, const QByteArray &plainText, bool alwaysTrust, QByteArray &cipherText)
     155             : {
     156           0 :     return exec(signers, recipients, plainText, alwaysTrust ? Context::AlwaysTrust : Context::None, cipherText);
     157             : }
     158             : 
     159             : 
     160             : #if 0
     161             : 
     162             : TODO port?
     163             : void QGpgMESignEncryptJob::showErrorDialog(QWidget *parent, const QString &caption) const
     164             : {
     165             :     if ((mResult.first.error()  && !mResult.first.error().isCanceled()) ||
     166             :             (mResult.second.error() && !mResult.second.error().isCanceled())) {
     167             :         MessageBox::error(parent, mResult.first, mResult.second, this, caption);
     168             :     }
     169             : }
     170             : #endif
     171             : 
     172           1 : void QGpgMESignEncryptJob::resultHook(const result_type &tuple)
     173             : {
     174           1 :     mResult = std::make_pair(std::get<0>(tuple), std::get<1>(tuple));
     175           1 : }
     176             : #include "qgpgmesignencryptjob.moc"

Generated by: LCOV version 1.13