1 /*
2     qgpgmeverifyopaquejob.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 "qgpgmeverifyopaquejob.h"
40 
41 #include "dataprovider.h"
42 
43 #include "context.h"
44 #include "verificationresult.h"
45 #include "data.h"
46 
47 #include <QBuffer>
48 
49 
50 #include <cassert>
51 
52 using namespace QGpgME;
53 using namespace GpgME;
54 
QGpgMEVerifyOpaqueJob(Context * context)55 QGpgMEVerifyOpaqueJob::QGpgMEVerifyOpaqueJob(Context *context)
56     : mixin_type(context)
57 {
58     lateInitialization();
59 }
60 
~QGpgMEVerifyOpaqueJob()61 QGpgMEVerifyOpaqueJob::~QGpgMEVerifyOpaqueJob() {}
62 
verify_opaque(Context * ctx,QThread * thread,const std::weak_ptr<QIODevice> & signedData_,const std::weak_ptr<QIODevice> & plainText_)63 static QGpgMEVerifyOpaqueJob::result_type verify_opaque(Context *ctx, QThread *thread, const std::weak_ptr<QIODevice> &signedData_, const std::weak_ptr<QIODevice> &plainText_)
64 {
65 
66     const std::shared_ptr<QIODevice> plainText = plainText_.lock();
67     const std::shared_ptr<QIODevice> signedData = signedData_.lock();
68 
69     const _detail::ToThreadMover ptMover(plainText,  thread);
70     const _detail::ToThreadMover sdMover(signedData, thread);
71 
72     QGpgME::QIODeviceDataProvider in(signedData);
73     const Data indata(&in);
74 
75     if (!plainText) {
76         QGpgME::QByteArrayDataProvider out;
77         Data outdata(&out);
78 
79         const VerificationResult res = ctx->verifyOpaqueSignature(indata, outdata);
80         Error ae;
81         const QString log = _detail::audit_log_as_html(ctx, ae);
82         return std::make_tuple(res, out.data(), log, ae);
83     } else {
84         QGpgME::QIODeviceDataProvider out(plainText);
85         Data outdata(&out);
86 
87         const VerificationResult res = ctx->verifyOpaqueSignature(indata, outdata);
88         Error ae;
89         const QString log = _detail::audit_log_as_html(ctx, ae);
90         return std::make_tuple(res, QByteArray(), log, ae);
91     }
92 
93 }
94 
verify_opaque_qba(Context * ctx,const QByteArray & signedData)95 static QGpgMEVerifyOpaqueJob::result_type verify_opaque_qba(Context *ctx, const QByteArray &signedData)
96 {
97     const std::shared_ptr<QBuffer> buffer(new QBuffer);
98     buffer->setData(signedData);
99     if (!buffer->open(QIODevice::ReadOnly)) {
100         assert(!"This should never happen: QBuffer::open() failed");
101     }
102     return verify_opaque(ctx, nullptr, buffer, std::shared_ptr<QIODevice>());
103 }
104 
start(const QByteArray & signedData)105 Error QGpgMEVerifyOpaqueJob::start(const QByteArray &signedData)
106 {
107     run(std::bind(&verify_opaque_qba, std::placeholders::_1, signedData));
108     return Error();
109 }
110 
start(const std::shared_ptr<QIODevice> & signedData,const std::shared_ptr<QIODevice> & plainText)111 void QGpgMEVerifyOpaqueJob::start(const std::shared_ptr<QIODevice> &signedData, const std::shared_ptr<QIODevice> &plainText)
112 {
113     run(std::bind(&verify_opaque, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4), signedData, plainText);
114 }
115 
exec(const QByteArray & signedData,QByteArray & plainText)116 GpgME::VerificationResult QGpgME::QGpgMEVerifyOpaqueJob::exec(const QByteArray &signedData, QByteArray &plainText)
117 {
118     const result_type r = verify_opaque_qba(context(), signedData);
119     plainText = std::get<1>(r);
120     resultHook(r);
121     return mResult;
122 }
123 
124 //PENDING(marc) implement showErrorDialog()
125 
resultHook(const result_type & tuple)126 void QGpgME::QGpgMEVerifyOpaqueJob::resultHook(const result_type &tuple)
127 {
128     mResult = std::get<0>(tuple);
129 }
130 #include "qgpgmeverifyopaquejob.moc"
131