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