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