LCOV - code coverage report
Current view: top level - lang/qt/src - qgpgmesignjob.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 37 57 64.9 %
Date: 2018-11-15 08:49:49 Functions: 7 10 70.0 %

          Line data    Source code
       1             : /*
       2             :     qgpgmesignjob.cpp
       3             : 
       4             :     This file is part of qgpgme, the Qt API binding for gpgme
       5             :     Copyright (c) 2004,2007,2008 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 "qgpgmesignjob.h"
      40             : 
      41             : #include "dataprovider.h"
      42             : 
      43             : #include "context.h"
      44             : #include "signingresult.h"
      45             : #include "data.h"
      46             : 
      47             : #include <QBuffer>
      48             : 
      49             : 
      50             : #include <cassert>
      51             : 
      52             : using namespace QGpgME;
      53             : using namespace GpgME;
      54             : 
      55           6 : QGpgMESignJob::QGpgMESignJob(Context *context)
      56             :     : mixin_type(context),
      57           6 :       mOutputIsBase64Encoded(false)
      58             : {
      59           6 :     lateInitialization();
      60           6 : }
      61             : 
      62          12 : QGpgMESignJob::~QGpgMESignJob() {}
      63             : 
      64           0 : void QGpgMESignJob::setOutputIsBase64Encoded(bool on)
      65             : {
      66           0 :     mOutputIsBase64Encoded = on;
      67           0 : }
      68             : 
      69           6 : static QGpgMESignJob::result_type sign(Context *ctx, QThread *thread,
      70             :                                        const std::vector<Key> &signers,
      71             :                                        const std::weak_ptr<QIODevice> &plainText_,
      72             :                                        const std::weak_ptr<QIODevice> &signature_,
      73             :                                        SignatureMode mode,
      74             :                                        bool outputIsBsse64Encoded)
      75             : {
      76             : 
      77          12 :     const std::shared_ptr<QIODevice> plainText = plainText_.lock();
      78          12 :     const std::shared_ptr<QIODevice> signature = signature_.lock();
      79             : 
      80          12 :     const _detail::ToThreadMover ptMover(plainText, thread);
      81          12 :     const _detail::ToThreadMover sgMover(signature, thread);
      82             : 
      83          12 :     QGpgME::QIODeviceDataProvider in(plainText);
      84          12 :     const Data indata(&in);
      85             : 
      86           6 :     ctx->clearSigningKeys();
      87          12 :     Q_FOREACH (const Key &signer, signers)
      88           6 :         if (!signer.isNull())
      89          12 :             if (const Error err = ctx->addSigningKey(signer)) {
      90           0 :                 return std::make_tuple(SigningResult(err), QByteArray(), QString(), Error());
      91             :             }
      92             : 
      93           6 :     if (!signature) {
      94          12 :         QGpgME::QByteArrayDataProvider out;
      95          12 :         Data outdata(&out);
      96             : 
      97           6 :         if (outputIsBsse64Encoded) {
      98           0 :             outdata.setEncoding(Data::Base64Encoding);
      99             :         }
     100             : 
     101          12 :         const SigningResult res = ctx->sign(indata, outdata, mode);
     102          12 :         Error ae;
     103          12 :         const QString log = _detail::audit_log_as_html(ctx, ae);
     104           6 :         return std::make_tuple(res, out.data(), log, ae);
     105             :     } else {
     106           0 :         QGpgME::QIODeviceDataProvider out(signature);
     107           0 :         Data outdata(&out);
     108             : 
     109           0 :         if (outputIsBsse64Encoded) {
     110           0 :             outdata.setEncoding(Data::Base64Encoding);
     111             :         }
     112             : 
     113           0 :         const SigningResult res = ctx->sign(indata, outdata, mode);
     114           0 :         Error ae;
     115           0 :         const QString log = _detail::audit_log_as_html(ctx, ae);
     116           0 :         return std::make_tuple(res, QByteArray(), log, ae);
     117             :     }
     118             : 
     119             : }
     120             : 
     121           6 : static QGpgMESignJob::result_type sign_qba(Context *ctx,
     122             :         const std::vector<Key> &signers,
     123             :         const QByteArray &plainText,
     124             :         SignatureMode mode,
     125             :         bool outputIsBsse64Encoded)
     126             : {
     127          12 :     const std::shared_ptr<QBuffer> buffer(new QBuffer);
     128           6 :     buffer->setData(plainText);
     129           6 :     if (!buffer->open(QIODevice::ReadOnly)) {
     130           0 :         assert(!"This should never happen: QBuffer::open() failed");
     131             :     }
     132          12 :     return sign(ctx, 0, signers, buffer, std::shared_ptr<QIODevice>(), mode, outputIsBsse64Encoded);
     133             : }
     134             : 
     135           0 : Error QGpgMESignJob::start(const std::vector<Key> &signers, const QByteArray &plainText, SignatureMode mode)
     136             : {
     137           0 :     run(std::bind(&sign_qba, std::placeholders::_1, signers, plainText, mode, mOutputIsBase64Encoded));
     138           0 :     return Error();
     139             : }
     140             : 
     141           0 : void QGpgMESignJob::start(const std::vector<Key> &signers, const std::shared_ptr<QIODevice> &plainText, const std::shared_ptr<QIODevice> &signature, SignatureMode mode)
     142             : {
     143           0 :     run(std::bind(&sign, std::placeholders::_1, std::placeholders::_2, signers, std::placeholders::_3, std::placeholders::_4, mode, mOutputIsBase64Encoded), plainText, signature);
     144           0 : }
     145             : 
     146           6 : SigningResult QGpgMESignJob::exec(const std::vector<Key> &signers, const QByteArray &plainText, SignatureMode mode, QByteArray &signature)
     147             : {
     148          12 :     const result_type r = sign_qba(context(), signers, plainText, mode, mOutputIsBase64Encoded);
     149           6 :     signature = std::get<1>(r);
     150           6 :     resultHook(r);
     151          12 :     return mResult;
     152             : }
     153             : 
     154           6 : void QGpgMESignJob::resultHook(const result_type &tuple)
     155             : {
     156           6 :     mResult = std::get<0>(tuple);
     157           6 : }
     158             : 
     159             : #if 0
     160             : TODO port
     161             : void QGpgMESignJob::showErrorDialog(QWidget *parent, const QString &caption) const
     162             : {
     163             :     if (mResult.error() && !mResult.error().isCanceled()) {
     164             :         MessageBox::error(parent, mResult, this, caption);
     165             :     }
     166             : }
     167             : #endif
     168             : #include "qgpgmesignjob.moc"

Generated by: LCOV version 1.13