Blame lang/qt/src/qgpgmekeyformailboxjob.cpp

Packit d7e8d0
/*
Packit d7e8d0
    qgpgmekeyformailboxjob.cpp
Packit d7e8d0
Packit d7e8d0
    This file is part of qgpgme, the Qt API binding for gpgme
Packit d7e8d0
    Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik
Packit d7e8d0
    Software engineering by Intevation GmbH
Packit d7e8d0
Packit d7e8d0
    QGpgME is free software; you can redistribute it and/or
Packit d7e8d0
    modify it under the terms of the GNU General Public License as
Packit d7e8d0
    published by the Free Software Foundation; either version 2 of the
Packit d7e8d0
    License, or (at your option) any later version.
Packit d7e8d0
Packit d7e8d0
    QGpgME is distributed in the hope that it will be useful,
Packit d7e8d0
    but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit d7e8d0
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit d7e8d0
    General Public License for more details.
Packit d7e8d0
Packit d7e8d0
    You should have received a copy of the GNU General Public License
Packit d7e8d0
    along with this program; if not, write to the Free Software
Packit d7e8d0
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit d7e8d0
Packit d7e8d0
    In addition, as a special exception, the copyright holders give
Packit d7e8d0
    permission to link the code of this program with any edition of
Packit d7e8d0
    the Qt library by Trolltech AS, Norway (or with modified versions
Packit d7e8d0
    of Qt that use the same license as Qt), and distribute linked
Packit d7e8d0
    combinations including the two.  You must obey the GNU General
Packit d7e8d0
    Public License in all respects for all of the code used other than
Packit d7e8d0
    Qt.  If you modify this file, you may extend this exception to
Packit d7e8d0
    your version of the file, but you are not obligated to do so.  If
Packit d7e8d0
    you do not wish to do so, delete this exception statement from
Packit d7e8d0
    your version.
Packit d7e8d0
*/
Packit d7e8d0
Packit d7e8d0
#ifdef HAVE_CONFIG_H
Packit d7e8d0
 #include "config.h"
Packit d7e8d0
#endif
Packit d7e8d0
Packit d7e8d0
#include "qgpgmekeyformailboxjob.h"
Packit d7e8d0
#include "qgpgmekeylistjob.h"
Packit d7e8d0
Packit d7e8d0
#include <QStringList>
Packit d7e8d0
Packit d7e8d0
#include <tuple>
Packit d7e8d0
Packit d7e8d0
using namespace GpgME;
Packit d7e8d0
using namespace QGpgME;
Packit d7e8d0
Packit d7e8d0
QGpgMEKeyForMailboxJob::QGpgMEKeyForMailboxJob(Context *context)
Packit d7e8d0
    : mixin_type(context)
Packit d7e8d0
{
Packit d7e8d0
    lateInitialization();
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
QGpgMEKeyForMailboxJob::~QGpgMEKeyForMailboxJob() {}
Packit d7e8d0
Packit d7e8d0
static bool keyIsOk(const Key k)
Packit d7e8d0
{
Packit d7e8d0
    return !k.isExpired() && !k.isRevoked() && !k.isInvalid() && !k.isDisabled();
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
static bool uidIsOk(const UserID uid)
Packit d7e8d0
{
Packit d7e8d0
    return keyIsOk(uid.parent()) && !uid.isRevoked() && !uid.isInvalid();
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
static bool subkeyIsOk(const Subkey s)
Packit d7e8d0
{
Packit d7e8d0
    return !s.isRevoked() && !s.isInvalid() && !s.isDisabled();
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
static QGpgMEKeyForMailboxJob::result_type do_work(Context *ctx, const QString &mailbox, bool canEncrypt)
Packit d7e8d0
{
Packit d7e8d0
    /* Do a Keylisting. */
Packit d7e8d0
    ctx->setKeyListMode(GpgME::Extern | GpgME::Local | GpgME::Signatures | GpgME::Validate);
Packit d7e8d0
    std::vector<Key> keys;
Packit d7e8d0
    QGpgMEKeyListJob *keylist = new QGpgMEKeyListJob(ctx);
Packit d7e8d0
Packit d7e8d0
    KeyListResult result = keylist->exec(QStringList() << mailbox, false, keys);
Packit d7e8d0
Packit d7e8d0
    if (result.error()) {
Packit d7e8d0
        return std::make_tuple(result, Key(), UserID(), QString(), Error());
Packit d7e8d0
    }
Packit d7e8d0
Packit d7e8d0
    // This should ideally be decided by GnuPG and this Job changed
Packit d7e8d0
    // to just call the according API in GpgME
Packit d7e8d0
    // See: https://bugs.gnupg.org/gnupg/issue2359
Packit d7e8d0
    Key keyC;
Packit d7e8d0
    UserID uidC;
Packit d7e8d0
    Q_FOREACH (const Key k, keys) {
Packit d7e8d0
        if (canEncrypt && !k.canEncrypt()) {
Packit d7e8d0
            continue;
Packit d7e8d0
        }
Packit d7e8d0
        /* First get the uid that matches the mailbox */
Packit d7e8d0
        Q_FOREACH (const UserID u, k.userIDs()) {
Packit d7e8d0
            if (QString::fromUtf8(u.email()).toLower() == mailbox.toLower()) {
Packit d7e8d0
                if (uidC.isNull()) {
Packit d7e8d0
                    keyC = k;
Packit d7e8d0
                    uidC = u;
Packit d7e8d0
                } else if ((!uidIsOk(uidC) && uidIsOk(u)) || uidC.validity() < u.validity()) {
Packit d7e8d0
                    /* Validity of the new key is better. */
Packit d7e8d0
                    uidC = u;
Packit d7e8d0
                    keyC = k;
Packit d7e8d0
                } else if (uidC.validity() == u.validity() && uidIsOk(u)) {
Packit d7e8d0
                    /* Both are the same check which one is newer. */
Packit d7e8d0
                    time_t oldTime = 0;
Packit d7e8d0
                    Q_FOREACH (const Subkey s, keyC.subkeys()) {
Packit d7e8d0
                        if ((canEncrypt && s.canEncrypt()) && subkeyIsOk(s)) {
Packit d7e8d0
                            oldTime = s.creationTime();
Packit d7e8d0
                        }
Packit d7e8d0
                    }
Packit d7e8d0
                    time_t newTime = 0;
Packit d7e8d0
                    Q_FOREACH (const Subkey s, k.subkeys()) {
Packit d7e8d0
                        if ((canEncrypt && s.canEncrypt()) && subkeyIsOk(s)) {
Packit d7e8d0
                            newTime = s.creationTime();
Packit d7e8d0
                        }
Packit d7e8d0
                    }
Packit d7e8d0
                    if (newTime > oldTime) {
Packit d7e8d0
                        uidC = u;
Packit d7e8d0
                        keyC = k;
Packit d7e8d0
                    }
Packit d7e8d0
                }
Packit d7e8d0
            }
Packit d7e8d0
        }
Packit d7e8d0
    }
Packit d7e8d0
    return std::make_tuple(result, keyC, uidC, QString(), Error());
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
Error QGpgMEKeyForMailboxJob::start(const QString &mailbox, bool canEncrypt)
Packit d7e8d0
{
Packit d7e8d0
    run(std::bind(&do_work, std::placeholders::_1, mailbox, canEncrypt));
Packit d7e8d0
    return Error();
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
KeyListResult QGpgMEKeyForMailboxJob::exec(const QString &mailbox, bool canEncrypt, Key &key, UserID &uid)
Packit d7e8d0
{
Packit d7e8d0
    const result_type r = do_work(context(), mailbox, canEncrypt);
Packit d7e8d0
    resultHook(r);
Packit d7e8d0
    key = std::get<1>(r);
Packit d7e8d0
    uid = std::get<2>(r);
Packit d7e8d0
    return std::get<0>(r);
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
#include "qgpgmekeyformailboxjob.moc"