Line data Source code
1 : /*
2 : qgpgmekeyformailboxjob.cpp
3 :
4 : This file is part of qgpgme, the Qt API binding for gpgme
5 : Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik
6 : Software engineering by 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 :
34 : #ifdef HAVE_CONFIG_H
35 : #include "config.h"
36 : #endif
37 :
38 : #include "qgpgmekeyformailboxjob.h"
39 : #include "qgpgmekeylistjob.h"
40 :
41 : #include <QStringList>
42 :
43 : #include <tuple>
44 :
45 : using namespace GpgME;
46 : using namespace QGpgME;
47 :
48 0 : QGpgMEKeyForMailboxJob::QGpgMEKeyForMailboxJob(Context *context)
49 0 : : mixin_type(context)
50 : {
51 0 : lateInitialization();
52 0 : }
53 :
54 0 : QGpgMEKeyForMailboxJob::~QGpgMEKeyForMailboxJob() {}
55 :
56 0 : static bool keyIsOk(const Key k)
57 : {
58 0 : return !k.isExpired() && !k.isRevoked() && !k.isInvalid() && !k.isDisabled();
59 : }
60 :
61 0 : static bool uidIsOk(const UserID uid)
62 : {
63 0 : return keyIsOk(uid.parent()) && !uid.isRevoked() && !uid.isInvalid();
64 : }
65 :
66 0 : static bool subkeyIsOk(const Subkey s)
67 : {
68 0 : return !s.isRevoked() && !s.isInvalid() && !s.isDisabled();
69 : }
70 :
71 0 : static QGpgMEKeyForMailboxJob::result_type do_work(Context *ctx, const QString &mailbox, bool canEncrypt)
72 : {
73 : /* Do a Keylisting. */
74 0 : ctx->setKeyListMode(GpgME::Extern | GpgME::Local | GpgME::Signatures | GpgME::Validate);
75 0 : std::vector<Key> keys;
76 0 : QGpgMEKeyListJob *keylist = new QGpgMEKeyListJob(ctx);
77 :
78 0 : KeyListResult result = keylist->exec(QStringList() << mailbox, false, keys);
79 :
80 0 : if (result.error()) {
81 0 : return std::make_tuple(result, Key(), UserID(), QString(), Error());
82 : }
83 :
84 : // This should ideally be decided by GnuPG and this Job changed
85 : // to just call the according API in GpgME
86 : // See: https://bugs.gnupg.org/gnupg/issue2359
87 0 : Key keyC;
88 0 : UserID uidC;
89 0 : Q_FOREACH (const Key k, keys) {
90 0 : if (canEncrypt && !k.canEncrypt()) {
91 0 : continue;
92 : }
93 : /* First get the uid that matches the mailbox */
94 0 : Q_FOREACH (const UserID u, k.userIDs()) {
95 0 : if (QString::fromUtf8(u.email()).toLower() == mailbox.toLower()) {
96 0 : if (uidC.isNull()) {
97 0 : keyC = k;
98 0 : uidC = u;
99 0 : } else if ((!uidIsOk(uidC) && uidIsOk(u)) || uidC.validity() < u.validity()) {
100 : /* Validity of the new key is better. */
101 0 : uidC = u;
102 0 : keyC = k;
103 0 : } else if (uidC.validity() == u.validity() && uidIsOk(u)) {
104 : /* Both are the same check which one is newer. */
105 0 : time_t oldTime = 0;
106 0 : Q_FOREACH (const Subkey s, keyC.subkeys()) {
107 0 : if ((canEncrypt && s.canEncrypt()) && subkeyIsOk(s)) {
108 0 : oldTime = s.creationTime();
109 : }
110 : }
111 0 : time_t newTime = 0;
112 0 : Q_FOREACH (const Subkey s, k.subkeys()) {
113 0 : if ((canEncrypt && s.canEncrypt()) && subkeyIsOk(s)) {
114 0 : newTime = s.creationTime();
115 : }
116 : }
117 0 : if (newTime > oldTime) {
118 0 : uidC = u;
119 0 : keyC = k;
120 : }
121 : }
122 : }
123 : }
124 : }
125 0 : return std::make_tuple(result, keyC, uidC, QString(), Error());
126 : }
127 :
128 0 : Error QGpgMEKeyForMailboxJob::start(const QString &mailbox, bool canEncrypt)
129 : {
130 0 : run(std::bind(&do_work, std::placeholders::_1, mailbox, canEncrypt));
131 0 : return Error();
132 : }
133 :
134 0 : KeyListResult QGpgMEKeyForMailboxJob::exec(const QString &mailbox, bool canEncrypt, Key &key, UserID &uid)
135 : {
136 0 : const result_type r = do_work(context(), mailbox, canEncrypt);
137 0 : resultHook(r);
138 0 : key = std::get<1>(r);
139 0 : uid = std::get<2>(r);
140 0 : return std::get<0>(r);
141 : }
142 :
143 : #include "qgpgmekeyformailboxjob.moc"
|