Line data Source code
1 : /*
2 : protocol_p.h
3 :
4 : This file is part of qgpgme, the Qt API binding for gpgme
5 : Copyright (c) 2004,2005 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
19 : along with this program; if not, write to the Free Software
20 : Foundation, Inc., 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 : #ifndef __QGPGME_PROTOCOL_P_H__
34 : #define __QGPGME_PROTOCOL_P_H__
35 : #include "qgpgmenewcryptoconfig.h"
36 :
37 : #include "qgpgmekeygenerationjob.h"
38 : #include "qgpgmekeylistjob.h"
39 : #include "qgpgmelistallkeysjob.h"
40 : #include "qgpgmedecryptjob.h"
41 : #include "qgpgmedecryptverifyjob.h"
42 : #include "qgpgmerefreshkeysjob.h"
43 : #include "qgpgmedeletejob.h"
44 : #include "qgpgmesecretkeyexportjob.h"
45 : #include "qgpgmedownloadjob.h"
46 : #include "qgpgmesignencryptjob.h"
47 : #include "qgpgmeencryptjob.h"
48 : #include "qgpgmesignjob.h"
49 : #include "qgpgmesignkeyjob.h"
50 : #include "qgpgmeexportjob.h"
51 : #include "qgpgmeverifydetachedjob.h"
52 : #include "qgpgmeimportjob.h"
53 : #include "qgpgmeimportfromkeyserverjob.h"
54 : #include "qgpgmeverifyopaquejob.h"
55 : #include "qgpgmechangeexpiryjob.h"
56 : #include "qgpgmechangeownertrustjob.h"
57 : #include "qgpgmechangepasswdjob.h"
58 : #include "qgpgmeadduseridjob.h"
59 : #include "qgpgmekeyformailboxjob.h"
60 : #include "qgpgmewkspublishjob.h"
61 : #include "qgpgmetofupolicyjob.h"
62 :
63 : namespace
64 : {
65 :
66 0 : class Protocol : public QGpgME::Protocol
67 : {
68 : GpgME::Protocol mProtocol;
69 : public:
70 7 : explicit Protocol(GpgME::Protocol proto) : mProtocol(proto) {}
71 :
72 0 : QString name() const Q_DECL_OVERRIDE
73 : {
74 0 : switch (mProtocol) {
75 0 : case GpgME::OpenPGP: return QStringLiteral("OpenPGP");
76 0 : case GpgME::CMS: return QStringLiteral("SMIME");
77 0 : default: return QString();
78 : }
79 : }
80 :
81 0 : QString displayName() const Q_DECL_OVERRIDE
82 : {
83 : // ah (2.4.16): Where is this used and isn't this inverted
84 : // with name
85 0 : switch (mProtocol) {
86 0 : case GpgME::OpenPGP: return QStringLiteral("gpg");
87 0 : case GpgME::CMS: return QStringLiteral("gpgsm");
88 0 : default: return QStringLiteral("unknown");
89 : }
90 : }
91 :
92 0 : QGpgME::SpecialJob *specialJob(const char *, const QMap<QString, QVariant> &) const Q_DECL_OVERRIDE
93 : {
94 0 : return 0;
95 : }
96 :
97 12 : QGpgME::KeyListJob *keyListJob(bool remote, bool includeSigs, bool validate) const Q_DECL_OVERRIDE
98 : {
99 12 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
100 12 : if (!context) {
101 0 : return 0;
102 : }
103 :
104 12 : unsigned int mode = context->keyListMode();
105 12 : if (remote) {
106 0 : mode |= GpgME::Extern;
107 0 : mode &= ~GpgME::Local;
108 : } else {
109 12 : mode |= GpgME::Local;
110 12 : mode &= ~GpgME::Extern;
111 : }
112 12 : if (includeSigs) {
113 4 : mode |= GpgME::Signatures;
114 : }
115 12 : if (validate) {
116 4 : mode |= GpgME::Validate;
117 : }
118 12 : context->setKeyListMode(mode);
119 12 : return new QGpgME::QGpgMEKeyListJob(context);
120 : }
121 :
122 0 : QGpgME::ListAllKeysJob *listAllKeysJob(bool includeSigs, bool validate) const Q_DECL_OVERRIDE
123 : {
124 0 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
125 0 : if (!context) {
126 0 : return 0;
127 : }
128 :
129 0 : unsigned int mode = context->keyListMode();
130 0 : mode |= GpgME::Local;
131 0 : mode &= ~GpgME::Extern;
132 0 : if (includeSigs) {
133 0 : mode |= GpgME::Signatures;
134 : }
135 0 : if (validate) {
136 0 : mode |= GpgME::Validate;
137 : /* Setting the context to offline mode disables CRL / OCSP checks in
138 : this Job. Otherwise we would try to fetch the CRL's for all CMS
139 : keys in the users keyring because GpgME::Validate includes remote
140 : resources by default in the validity check.
141 : This setting only has any effect if gpgsm >= 2.1.6 is used.
142 : */
143 0 : context->setOffline(true);
144 : }
145 0 : context->setKeyListMode(mode);
146 0 : return new QGpgME::QGpgMEListAllKeysJob(context);
147 : }
148 :
149 2 : QGpgME::EncryptJob *encryptJob(bool armor, bool textmode) const Q_DECL_OVERRIDE
150 : {
151 2 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
152 2 : if (!context) {
153 0 : return 0;
154 : }
155 :
156 2 : context->setArmor(armor);
157 2 : context->setTextMode(textmode);
158 2 : return new QGpgME::QGpgMEEncryptJob(context);
159 : }
160 :
161 0 : QGpgME::DecryptJob *decryptJob() const Q_DECL_OVERRIDE
162 : {
163 0 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
164 0 : if (!context) {
165 0 : return 0;
166 : }
167 0 : return new QGpgME::QGpgMEDecryptJob(context);
168 : }
169 :
170 0 : QGpgME::SignJob *signJob(bool armor, bool textMode) const Q_DECL_OVERRIDE
171 : {
172 0 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
173 0 : if (!context) {
174 0 : return 0;
175 : }
176 :
177 0 : context->setArmor(armor);
178 0 : context->setTextMode(textMode);
179 0 : return new QGpgME::QGpgMESignJob(context);
180 : }
181 :
182 0 : QGpgME::VerifyDetachedJob *verifyDetachedJob(bool textMode) const Q_DECL_OVERRIDE
183 : {
184 0 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
185 0 : if (!context) {
186 0 : return 0;
187 : }
188 :
189 0 : context->setTextMode(textMode);
190 0 : return new QGpgME::QGpgMEVerifyDetachedJob(context);
191 : }
192 :
193 10 : QGpgME::VerifyOpaqueJob *verifyOpaqueJob(bool textMode) const Q_DECL_OVERRIDE
194 : {
195 10 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
196 10 : if (!context) {
197 0 : return 0;
198 : }
199 :
200 10 : context->setTextMode(textMode);
201 10 : return new QGpgME::QGpgMEVerifyOpaqueJob(context);
202 : }
203 :
204 0 : QGpgME::KeyGenerationJob *keyGenerationJob() const Q_DECL_OVERRIDE
205 : {
206 0 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
207 0 : if (!context) {
208 0 : return 0;
209 : }
210 0 : return new QGpgME::QGpgMEKeyGenerationJob(context);
211 : }
212 :
213 0 : QGpgME::ImportJob *importJob() const Q_DECL_OVERRIDE
214 : {
215 0 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
216 0 : if (!context) {
217 0 : return 0;
218 : }
219 0 : return new QGpgME::QGpgMEImportJob(context);
220 : }
221 :
222 0 : QGpgME::ImportFromKeyserverJob *importFromKeyserverJob() const Q_DECL_OVERRIDE
223 : {
224 0 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
225 0 : if (!context) {
226 0 : return 0;
227 : }
228 0 : return new QGpgME::QGpgMEImportFromKeyserverJob(context);
229 : }
230 :
231 0 : QGpgME::ExportJob *publicKeyExportJob(bool armor) const Q_DECL_OVERRIDE
232 : {
233 0 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
234 0 : if (!context) {
235 0 : return 0;
236 : }
237 :
238 0 : context->setArmor(armor);
239 0 : return new QGpgME::QGpgMEExportJob(context);
240 : }
241 :
242 0 : QGpgME::ExportJob *secretKeyExportJob(bool armor, const QString &charset) const Q_DECL_OVERRIDE
243 : {
244 0 : if (mProtocol != GpgME::CMS) { // fixme: add support for gpg, too
245 0 : return 0;
246 : }
247 :
248 : // this operation is not supported by gpgme, so we have to call gpgsm ourselves:
249 0 : return new QGpgME::QGpgMESecretKeyExportJob(armor, charset);
250 : }
251 :
252 0 : QGpgME::RefreshKeysJob *refreshKeysJob() const Q_DECL_OVERRIDE
253 : {
254 0 : if (mProtocol != GpgME::CMS) { // fixme: add support for gpg, too
255 0 : return 0;
256 : }
257 :
258 : // this operation is not supported by gpgme, so we have to call gpgsm ourselves:
259 0 : return new QGpgME::QGpgMERefreshKeysJob();
260 : }
261 :
262 0 : QGpgME::DownloadJob *downloadJob(bool armor) const Q_DECL_OVERRIDE
263 : {
264 0 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
265 0 : if (!context) {
266 0 : return 0;
267 : }
268 :
269 0 : context->setArmor(armor);
270 : // this is the hackish interface for downloading from keyserers currently:
271 0 : context->setKeyListMode(GpgME::Extern);
272 0 : return new QGpgME::QGpgMEDownloadJob(context);
273 : }
274 :
275 0 : QGpgME::DeleteJob *deleteJob() const Q_DECL_OVERRIDE
276 : {
277 0 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
278 0 : if (!context) {
279 0 : return 0;
280 : }
281 0 : return new QGpgME::QGpgMEDeleteJob(context);
282 : }
283 :
284 0 : QGpgME::SignEncryptJob *signEncryptJob(bool armor, bool textMode) const Q_DECL_OVERRIDE
285 : {
286 0 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
287 0 : if (!context) {
288 0 : return 0;
289 : }
290 :
291 0 : context->setArmor(armor);
292 0 : context->setTextMode(textMode);
293 0 : return new QGpgME::QGpgMESignEncryptJob(context);
294 : }
295 :
296 0 : QGpgME::DecryptVerifyJob *decryptVerifyJob(bool textMode) const Q_DECL_OVERRIDE
297 : {
298 0 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
299 0 : if (!context) {
300 0 : return 0;
301 : }
302 :
303 0 : context->setTextMode(textMode);
304 0 : return new QGpgME::QGpgMEDecryptVerifyJob(context);
305 : }
306 :
307 0 : QGpgME::ChangeExpiryJob *changeExpiryJob() const Q_DECL_OVERRIDE
308 : {
309 0 : if (mProtocol != GpgME::OpenPGP) {
310 0 : return 0; // only supported by gpg
311 : }
312 :
313 0 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
314 0 : if (!context) {
315 0 : return 0;
316 : }
317 0 : return new QGpgME::QGpgMEChangeExpiryJob(context);
318 : }
319 :
320 0 : QGpgME::ChangePasswdJob *changePasswdJob() const Q_DECL_OVERRIDE
321 : {
322 0 : if (!GpgME::hasFeature(GpgME::PasswdFeature, 0)) {
323 0 : return 0;
324 : }
325 0 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
326 0 : if (!context) {
327 0 : return 0;
328 : }
329 0 : return new QGpgME::QGpgMEChangePasswdJob(context);
330 : }
331 :
332 0 : QGpgME::SignKeyJob *signKeyJob() const Q_DECL_OVERRIDE
333 : {
334 0 : if (mProtocol != GpgME::OpenPGP) {
335 0 : return 0; // only supported by gpg
336 : }
337 :
338 0 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
339 0 : if (!context) {
340 0 : return 0;
341 : }
342 0 : return new QGpgME::QGpgMESignKeyJob(context);
343 : }
344 :
345 2 : QGpgME::ChangeOwnerTrustJob *changeOwnerTrustJob() const Q_DECL_OVERRIDE
346 : {
347 2 : if (mProtocol != GpgME::OpenPGP) {
348 0 : return 0; // only supported by gpg
349 : }
350 :
351 2 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
352 2 : if (!context) {
353 0 : return 0;
354 : }
355 2 : return new QGpgME::QGpgMEChangeOwnerTrustJob(context);
356 : }
357 :
358 0 : QGpgME::AddUserIDJob *addUserIDJob() const Q_DECL_OVERRIDE
359 : {
360 0 : if (mProtocol != GpgME::OpenPGP) {
361 0 : return 0; // only supported by gpg
362 : }
363 :
364 0 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
365 0 : if (!context) {
366 0 : return 0;
367 : }
368 0 : return new QGpgME::QGpgMEAddUserIDJob(context);
369 : }
370 :
371 1 : QGpgME::KeyListJob *locateKeysJob() const Q_DECL_OVERRIDE
372 : {
373 1 : if (mProtocol != GpgME::OpenPGP) {
374 0 : return Q_NULLPTR;
375 : }
376 1 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
377 1 : if (!context) {
378 0 : return Q_NULLPTR;
379 : }
380 1 : context->setKeyListMode(GpgME::Extern | GpgME::Local | GpgME::Signatures | GpgME::Validate);
381 1 : return new QGpgME::QGpgMEKeyListJob(context);
382 : }
383 :
384 0 : QGpgME::KeyForMailboxJob *keyForMailboxJob() const Q_DECL_OVERRIDE
385 : {
386 0 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
387 0 : if (!context) {
388 0 : return Q_NULLPTR;
389 : }
390 0 : return new QGpgME::QGpgMEKeyForMailboxJob(context);
391 : }
392 :
393 0 : QGpgME::WKSPublishJob *wksPublishJob() const Q_DECL_OVERRIDE
394 : {
395 0 : if (mProtocol != GpgME::OpenPGP) {
396 0 : return Q_NULLPTR;
397 : }
398 0 : auto context = GpgME::Context::createForEngine(GpgME::SpawnEngine);
399 0 : if (!context) {
400 0 : return Q_NULLPTR;
401 : }
402 0 : return new QGpgME::QGpgMEWKSPublishJob(context.release());
403 : }
404 :
405 1 : QGpgME::TofuPolicyJob *tofuPolicyJob() const Q_DECL_OVERRIDE
406 : {
407 1 : if (mProtocol != GpgME::OpenPGP) {
408 0 : return Q_NULLPTR;
409 : }
410 1 : GpgME::Context *context = GpgME::Context::createForProtocol(mProtocol);
411 1 : if (!context) {
412 0 : return Q_NULLPTR;
413 : }
414 1 : return new QGpgME::QGpgMETofuPolicyJob(context);
415 : }
416 : };
417 :
418 : }
419 : #endif
|