Line data Source code
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 Intevation GmbH
7 :
8 : QGpgME is free software; you can redistribute it and/or
9 : modify it under the terms of the GNU General Public License as
10 : published by the Free Software Foundation; either version 2 of the
11 : License, or (at your option) any later version.
12 :
13 : QGpgME is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License along
19 : with this program; if not, write to the Free Software Foundation, Inc.,
20 : 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 :
22 : In addition, as a special exception, the copyright holders give
23 : permission to link the code of this program with any edition of
24 : the Qt library by Trolltech AS, Norway (or with modified versions
25 : of Qt that use the same license as Qt), and distribute linked
26 : combinations including the two. You must obey the GNU General
27 : Public License in all respects for all of the code used other than
28 : Qt. If you modify this file, you may extend this exception to
29 : your version of the file, but you are not obligated to do so. If
30 : you do not wish to do so, delete this exception statement from
31 : your version.
32 : */
33 :
34 : #include "qgpgmeverifyopaquejob.h"
35 :
36 : #include "dataprovider.h"
37 :
38 : #include "context.h"
39 : #include "verificationresult.h"
40 : #include "data.h"
41 :
42 : #include <QBuffer>
43 :
44 :
45 : #include <cassert>
46 :
47 : using namespace QGpgME;
48 : using namespace GpgME;
49 :
50 9 : QGpgMEVerifyOpaqueJob::QGpgMEVerifyOpaqueJob(Context *context)
51 9 : : mixin_type(context)
52 : {
53 9 : lateInitialization();
54 9 : }
55 :
56 18 : QGpgMEVerifyOpaqueJob::~QGpgMEVerifyOpaqueJob() {}
57 :
58 9 : static QGpgMEVerifyOpaqueJob::result_type verify_opaque(Context *ctx, QThread *thread, const std::weak_ptr<QIODevice> &signedData_, const std::weak_ptr<QIODevice> &plainText_)
59 : {
60 :
61 9 : const std::shared_ptr<QIODevice> plainText = plainText_.lock();
62 18 : const std::shared_ptr<QIODevice> signedData = signedData_.lock();
63 :
64 18 : const _detail::ToThreadMover ptMover(plainText, thread);
65 18 : const _detail::ToThreadMover sdMover(signedData, thread);
66 :
67 18 : QGpgME::QIODeviceDataProvider in(signedData);
68 18 : const Data indata(&in);
69 :
70 9 : if (!plainText) {
71 9 : QGpgME::QByteArrayDataProvider out;
72 18 : Data outdata(&out);
73 :
74 18 : const VerificationResult res = ctx->verifyOpaqueSignature(indata, outdata);
75 18 : Error ae;
76 18 : const QString log = _detail::audit_log_as_html(ctx, ae);
77 18 : return std::make_tuple(res, out.data(), log, ae);
78 : } else {
79 0 : QGpgME::QIODeviceDataProvider out(plainText);
80 0 : Data outdata(&out);
81 :
82 0 : const VerificationResult res = ctx->verifyOpaqueSignature(indata, outdata);
83 0 : Error ae;
84 0 : const QString log = _detail::audit_log_as_html(ctx, ae);
85 0 : return std::make_tuple(res, QByteArray(), log, ae);
86 9 : }
87 :
88 : }
89 :
90 9 : static QGpgMEVerifyOpaqueJob::result_type verify_opaque_qba(Context *ctx, const QByteArray &signedData)
91 : {
92 9 : const std::shared_ptr<QBuffer> buffer(new QBuffer);
93 9 : buffer->setData(signedData);
94 9 : if (!buffer->open(QIODevice::ReadOnly)) {
95 0 : assert(!"This should never happen: QBuffer::open() failed");
96 : }
97 9 : return verify_opaque(ctx, 0, buffer, std::shared_ptr<QIODevice>());
98 : }
99 :
100 0 : Error QGpgMEVerifyOpaqueJob::start(const QByteArray &signedData)
101 : {
102 0 : run(std::bind(&verify_opaque_qba, std::placeholders::_1, signedData));
103 0 : return Error();
104 : }
105 :
106 0 : void QGpgMEVerifyOpaqueJob::start(const std::shared_ptr<QIODevice> &signedData, const std::shared_ptr<QIODevice> &plainText)
107 : {
108 0 : run(std::bind(&verify_opaque, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4), signedData, plainText);
109 0 : }
110 :
111 9 : GpgME::VerificationResult QGpgME::QGpgMEVerifyOpaqueJob::exec(const QByteArray &signedData, QByteArray &plainText)
112 : {
113 9 : const result_type r = verify_opaque_qba(context(), signedData);
114 9 : plainText = std::get<1>(r);
115 9 : resultHook(r);
116 9 : return mResult;
117 : }
118 :
119 : //PENDING(marc) implement showErrorDialog()
120 :
121 9 : void QGpgME::QGpgMEVerifyOpaqueJob::resultHook(const result_type &tuple)
122 : {
123 9 : mResult = std::get<0>(tuple);
124 9 : }
125 : #include "qgpgmeverifyopaquejob.moc"
|