Commit 954c8a20 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Add command for importing keys given by key ids from keyservers

This command will be used to retrieve missing key certification keys.

GnuPG-bug-id: 5805
parent e9f76b6d
......@@ -114,6 +114,7 @@ if (QGpgme_VERSION VERSION_GREATER_EQUAL "1.16.1")
set(QGPGME_SUPPORTS_IMPORT_WITH_KEY_ORIGIN 1)
set(QGPGME_SUPPORTS_SECRET_KEY_EXPORT 1)
set(QGPGME_SUPPORTS_SECRET_SUBKEY_EXPORT 1)
set(QGPGME_SUPPORTS_RECEIVING_KEYS_BY_KEY_ID 1)
endif()
# Kdepimlibs packages
......
......@@ -53,3 +53,6 @@
/* Defined if QGpgME supports the export of secret subkeys */
#cmakedefine QGPGME_SUPPORTS_SECRET_SUBKEY_EXPORT 1
/* Defined if QGpgME supports receiving keys by their key ids */
#cmakedefine QGPGME_SUPPORTS_RECEIVING_KEYS_BY_KEY_ID 1
......@@ -213,6 +213,7 @@ set(_kleopatra_SRCS
commands/importcertificatefromfilecommand.cpp
commands/importcertificatefromclipboardcommand.cpp
commands/importcertificatefromdatacommand.cpp
commands/importcertificatefromkeyservercommand.cpp
commands/lookupcertificatescommand.cpp
commands/reloadkeyscommand.cpp
commands/refreshx509certscommand.cpp
......
/* commands/importcertificatefromkeyservercommand.cpp
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2022 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <config-kleopatra.h>
#include "importcertificatefromkeyservercommand.h"
#include "importcertificatescommand_p.h"
#include <KLocalizedString>
#include "kleopatra_debug.h"
using namespace Kleo;
class ImportCertificateFromKeyserverCommand::Private : public ImportCertificatesCommand::Private
{
friend class ::ImportCertificateFromKeyserverCommand;
ImportCertificateFromKeyserverCommand *q_func() const
{
return static_cast<ImportCertificateFromKeyserverCommand *>(q);
}
public:
explicit Private(ImportCertificateFromKeyserverCommand *qq,
const QStringList &keyIds, const QString &id);
~Private() override;
private:
void start();
private:
QStringList mKeyIds;
QString mId;
};
ImportCertificateFromKeyserverCommand::Private *ImportCertificateFromKeyserverCommand::d_func()
{
return static_cast<Private *>(d.get());
}
const ImportCertificateFromKeyserverCommand::Private *ImportCertificateFromKeyserverCommand::d_func() const
{
return static_cast<const Private *>(d.get());
}
#define q q_func()
#define d d_func()
ImportCertificateFromKeyserverCommand::Private::Private(ImportCertificateFromKeyserverCommand *qq,
const QStringList &keyIds, const QString &id)
: ImportCertificatesCommand::Private{qq, nullptr}
, mKeyIds{keyIds}
, mId{id}
{
}
ImportCertificateFromKeyserverCommand::Private::~Private() = default;
void ImportCertificateFromKeyserverCommand::Private::start()
{
startImport(GpgME::OpenPGP, mKeyIds, mId);
}
ImportCertificateFromKeyserverCommand::ImportCertificateFromKeyserverCommand(const QStringList &keyIds, const QString &id)
: ImportCertificatesCommand{new Private{this, keyIds, id}}
{
}
ImportCertificateFromKeyserverCommand::~ImportCertificateFromKeyserverCommand() = default;
void ImportCertificateFromKeyserverCommand::doStart()
{
d->start();
}
#undef q_func
#undef d_func
/* -*- mode: c++; c-basic-offset:4 -*-
importcertificatefromkeyservercommand.h
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2022 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include "importcertificatescommand.h"
namespace Kleo
{
class ImportCertificateFromKeyserverCommand : public ImportCertificatesCommand
{
Q_OBJECT
public:
explicit ImportCertificateFromKeyserverCommand(const QStringList &keyIds,
const QString &id = {});
~ImportCertificateFromKeyserverCommand() override;
private:
void doStart() override;
private:
class Private;
inline Private *d_func();
inline const Private *d_func() const;
};
}
......@@ -5,7 +5,7 @@
SPDX-FileCopyrightText: 2007, 2008 Klarälvdalens Datakonsult AB
SPDX-FileCopyrightText: 2016 Bundesamt für Sicherheit in der Informationstechnik
SPDX-FileContributor: Intevation GmbH
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileCopyrightText: 2021, 2022 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
......@@ -33,6 +33,9 @@
#include <QGpgME/ImportJob>
#include <QGpgME/ImportFromKeyserverJob>
#include <QGpgME/ChangeOwnerTrustJob>
#ifdef QGPGME_SUPPORTS_RECEIVING_KEYS_BY_KEY_ID
#include <QGpgME/ReceiveKeysJob>
#endif
#include <gpgme++/global.h>
#include <gpgme++/importresult.h>
......@@ -780,6 +783,48 @@ void ImportCertificatesCommand::Private::startImport(GpgME::Protocol protocol, c
}
}
static auto get_receive_keys_job(GpgME::Protocol protocol)
{
Q_ASSERT(protocol != UnknownProtocol);
#ifdef QGPGME_SUPPORTS_RECEIVING_KEYS_BY_KEY_ID
std::unique_ptr<ReceiveKeysJob> job{};
if (const auto backend = (protocol == GpgME::OpenPGP ? QGpgME::openpgp() : QGpgME::smime())) {
job.reset(backend->receiveKeysJob());
}
return job;
#else
return std::unique_ptr<Job>{};
#endif
}
void ImportCertificatesCommand::Private::startImport(GpgME::Protocol protocol, const QStringList &keyIds, const QString &id)
{
Q_ASSERT(protocol != UnknownProtocol);
auto job = get_receive_keys_job(protocol);
if (!job.get()) {
qCWarning(KLEOPATRA_LOG) << "Failed to get ReceiveKeysJob for protocol" << Formatting::displayName(protocol);
importResult({id, protocol, ImportType::External, ImportResult{}});
return;
}
#ifdef QGPGME_SUPPORTS_RECEIVING_KEYS_BY_KEY_ID
keyCacheAutoRefreshSuspension = KeyCache::mutableInstance()->suspendAutoRefresh();
connect(job.get(), SIGNAL(result(GpgME::ImportResult)),
q, SLOT(importResult(GpgME::ImportResult)));
connect(job.get(), &Job::progress,
q, &Command::progress);
const GpgME::Error err = job->start(keyIds);
if (err.code()) {
importResult({id, protocol, ImportType::External, ImportResult{err}});
} else {
jobs.push_back({id, protocol, ImportType::External, job.release()});
}
#endif
}
void ImportCertificatesCommand::Private::importGroupsFromFile(const QString &filename)
{
filesToImportGroupsFrom.push_back(filename);
......
......@@ -3,7 +3,7 @@
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2007, 2008 Klarälvdalens Datakonsult AB
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileCopyrightText: 2021, 2022 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
......@@ -100,6 +100,7 @@ public:
void startImport(GpgME::Protocol proto, const QByteArray &data, const QString &id = QString(), const ImportOptions &options = {});
void startImport(GpgME::Protocol proto, const std::vector<GpgME::Key> &keys, const QString &id = QString());
void startImport(GpgME::Protocol proto, const QStringList &keyIds, const QString &id = {});
void importResult(const GpgME::ImportResult &);
void importResult(const ImportResultData &result);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment