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