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