feat: add key server sync module

This commit is contained in:
saturneric 2024-11-17 19:22:07 +01:00
parent 5155077972
commit 99a5cb5ab5
6 changed files with 422 additions and 1 deletions

View File

@ -29,3 +29,4 @@ add_subdirectory(m_ver_check)
add_subdirectory(m_gpg_info) add_subdirectory(m_gpg_info)
add_subdirectory(m_pinentry) add_subdirectory(m_pinentry)
add_subdirectory(m_paper_key) add_subdirectory(m_paper_key)
add_subdirectory(m_key_server_sync)

View File

@ -0,0 +1,46 @@
# Copyright (C) 2021-2024 Saturneric <eric@bktus.com>
#
# This file is part of GpgFrontend.
#
# GpgFrontend is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GpgFrontend is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
#
# The initial version of the source code is inherited from
# the gpg4usb project, which is under GPL-3.0-or-later.
#
# All the source code of GpgFrontend was modified and released by
# Saturneric <eric@bktus.com> starting on May 12, 2021.
#
# SPDX-License-Identifier: GPL-3.0-or-later
# com.bktus.gpgfrontend.module.integrated.gnupg_info_gathering
set(INTEGRATED_MODULE_SOURCE "")
aux_source_directory(. INTEGRATED_MODULE_SOURCE)
# define libgpgfrontend_module
add_library(mod_key_server_sync SHARED ${INTEGRATED_MODULE_SOURCE})
# install dir
install(TARGETS mod_key_server_sync
LIBRARY DESTINATION "${CMAKE_INSTALL_PREFIX}/modules")
# link sdk
target_link_libraries(mod_key_server_sync PRIVATE
gpgfrontend_module_sdk)
# link qt
target_link_libraries(mod_key_server_sync PRIVATE Qt::Core Qt::Network)
# using std c++ 17
target_compile_features(mod_key_server_sync PRIVATE cxx_std_17)

View File

@ -0,0 +1,137 @@
/**
* Copyright (C) 2021-2024 Saturneric <eric@bktus.com>
*
* This file is part of GpgFrontend.
*
* GpgFrontend is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GpgFrontend is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
*
* The initial version of the source code is inherited from
* the gpg4usb project, which is under GPL-3.0-or-later.
*
* All the source code of GpgFrontend was modified and released by
* Saturneric <eric@bktus.com> starting on May 12, 2021.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#include "KeyServerSyncModule.h"
#include <QtCore>
#include "GFModuleDefine.h"
#include "VKSInterface.h"
GF_MODULE_API_DEFINE("com.bktus.gpgfrontend.module.key_server_sync",
"KeyServerSync", "1.0.0",
"Sync Information From Trusted Key Server.", "Saturneric")
auto GFRegisterModule() -> int {
LOG_DEBUG("key server sync module registering");
return 0;
}
auto GFActiveModule() -> int {
LISTEN("REQUEST_GET_PUBLIC_KEY_BY_FINGERPRINT");
LISTEN("REQUEST_GET_PUBLIC_KEY_BY_KEY_ID");
return 0;
}
EXECUTE_MODULE() {
FLOG_DEBUG("key server sync module executing, event id: %1",
event["event_id"]);
if (event["event_id"] == "REQUEST_GET_PUBLIC_KEY_BY_FINGERPRINT") {
if (event["fingerprint"].isEmpty())
CB_ERR(event, -1, "fingerprint is empty");
QByteArray fingerprint = event["fingerprint"].toLatin1();
FLOG_DEBUG("try to get key info of fingerprint: %1", fingerprint);
auto* vks = new VKSInterface();
vks->GetByFingerprint(fingerprint);
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved,
QThread::currentThread(), [event](const QString& key) {
CB(event, GFGetModuleID(),
{
{"ret", QString::number(0)},
{"key_data", key},
});
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
QObject::connect(vks, &VKSInterface::SignalErrorOccurred,
QThread::currentThread(),
[event](const QString& error, const QString& data) {
CB(event, GFGetModuleID(),
{
{"ret", QString::number(-1)},
{"error_msg", error},
{"reply_data", data},
});
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
return 0;
}
if (event["event_id"] == "REQUEST_GET_PUBLIC_KEY_BY_KEY_ID") {
if (event["key_id"].isEmpty()) CB_ERR(event, -1, "key_id is empty");
QByteArray key_id = event["key_id"].toLatin1();
FLOG_DEBUG("try to get key info of key id: %1", key_id);
auto* vks = new VKSInterface();
vks->GetByKeyId(key_id);
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved,
QThread::currentThread(), [event](const QString& key) {
CB(event, GFGetModuleID(),
{
{"ret", QString::number(0)},
{"key_data", key},
});
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
QObject::connect(vks, &VKSInterface::SignalErrorOccurred,
QThread::currentThread(),
[event](const QString& error, const QString& data) {
CB(event, GFGetModuleID(),
{
{"ret", QString::number(-1)},
{"error_msg", error},
{"reply_data", data},
});
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
return 0;
}
CB_SUCC(event);
}
END_EXECUTE_MODULE()
auto GFDeactivateModule() -> int { return 0; }
auto GFUnregisterModule() -> int {
MLogDebug("paper key module unregistering");
return 0;
}

View File

@ -0,0 +1,33 @@
/**
* Copyright (C) 2021-2024 Saturneric <eric@bktus.com>
*
* This file is part of GpgFrontend.
*
* GpgFrontend is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GpgFrontend is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
*
* The initial version of the source code is inherited from
* the gpg4usb project, which is under GPL-3.0-or-later.
*
* All the source code of GpgFrontend was modified and released by
* Saturneric <eric@bktus.com> starting on May 12, 2021.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#pragma once
#include "GFModuleDeclare.h"
GF_MODULE_API_DECLARE

View File

@ -0,0 +1,141 @@
/**
* Copyright (C) 2021-2024 Saturneric <eric@bktus.com>
*
* This file is part of GpgFrontend.
*
* GpgFrontend is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GpgFrontend is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
*
* The initial version of the source code is inherited from
* the gpg4usb project, which is under GPL-3.0-or-later.
*
* All the source code of GpgFrontend was modified and released by
* Saturneric <eric@bktus.com> starting on May 12, 2021.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#include "VKSInterface.h"
#include <QByteArray>
#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QString>
#include <QUrl>
#include <QUrlQuery>
#include <utility>
VKSInterface::VKSInterface(QString key_server, QObject* parent)
: QObject(parent),
target_key_server_(std::move(key_server)),
network_manager_(new QNetworkAccessManager(this)) {
connect(network_manager_, &QNetworkAccessManager::finished, this,
&VKSInterface::on_reply_finished);
}
void VKSInterface::GetByFingerprint(const QString& fingerprint) {
QUrl url(QString("%1/vks/v1/by-fingerprint/%2")
.arg(target_key_server_)
.arg(fingerprint));
QNetworkRequest request(url);
network_manager_->get(request);
}
void VKSInterface::GetByKeyId(const QString& keyId) {
QUrl url(QString("%1/vks/v1/by-keyid/%2").arg(target_key_server_).arg(keyId));
QNetworkRequest request(url);
network_manager_->get(request);
}
void VKSInterface::GetByEmail(const QString& email) {
QUrl url(QString("%1/vks/v1/by-email/%2")
.arg(target_key_server_)
.arg(QUrl::toPercentEncoding(email)));
QNetworkRequest request(url);
network_manager_->get(request);
}
void VKSInterface::UploadKey(const QString& key_text) {
QUrl url(QString("%1/vks/v1/upload").arg(target_key_server_));
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QJsonObject json;
json["keytext"] = key_text;
network_manager_->post(request, QJsonDocument(json).toJson());
}
void VKSInterface::RequestVerify(const QString& token,
const QStringList& addresses,
const QStringList& locale) {
QUrl url(QString("%1/vks/v1/request-verify").arg(target_key_server_));
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QJsonObject json;
json["token"] = token;
QJsonArray addresses_array;
for (const QString& address : addresses) {
addresses_array.append(address);
}
json["addresses"] = addresses_array;
if (!locale.isEmpty()) {
QJsonArray locale_array;
for (const QString& loc : locale) {
locale_array.append(loc);
}
json["locale"] = locale_array;
}
network_manager_->post(request, QJsonDocument(json).toJson());
}
void VKSInterface::on_reply_finished(QNetworkReply* reply) {
if (reply->error() != QNetworkReply::NoError) {
emit SignalErrorOccurred(reply->errorString(), reply->readAll());
reply->deleteLater();
return;
}
QUrl url = reply->url();
QByteArray response_data = reply->readAll();
QJsonDocument json_response = QJsonDocument::fromJson(response_data);
if (url.path().contains("/vks/v1/by-fingerprint") ||
url.path().contains("/vks/v1/by-keyid") ||
url.path().contains("/vks/v1/by-email")) {
emit SignalKeyRetrieved(QString(response_data));
} else if (url.path().contains("/vks/v1/upload")) {
if (json_response.isObject()) {
QJsonObject response_object = json_response.object();
emit SignalKeyUploaded(response_object["key_fpr"].toString(),
response_object["status"].toObject());
}
} else if (url.path().contains("/vks/v1/request-verify")) {
if (json_response.isObject()) {
QJsonObject response_object = json_response.object();
emit SignalVerificationRequested(response_object["key_fpr"].toString(),
response_object["status"].toObject());
}
}
reply->deleteLater();
}

View File

@ -0,0 +1,63 @@
/**
* Copyright (C) 2021-2024 Saturneric <eric@bktus.com>
*
* This file is part of GpgFrontend.
*
* GpgFrontend is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GpgFrontend is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
*
* The initial version of the source code is inherited from
* the gpg4usb project, which is under GPL-3.0-or-later.
*
* All the source code of GpgFrontend was modified and released by
* Saturneric <eric@bktus.com> starting on May 12, 2021.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#include <QObject>
class QNetworkAccessManager;
class QNetworkReply;
class VKSInterface : public QObject {
Q_OBJECT
public:
explicit VKSInterface(QString target_key_server = "https://keys.openpgp.org",
QObject* parent = nullptr);
void GetByFingerprint(const QString& fingerprint);
void GetByKeyId(const QString& keyId);
void GetByEmail(const QString& email);
void UploadKey(const QString& key_text);
void RequestVerify(const QString& token, const QStringList& addresses,
const QStringList& locale = QStringList());
signals:
void SignalKeyRetrieved(const QString& key);
void SignalKeyUploaded(const QString& key_fingerprint,
const QJsonObject& status);
void SignalVerificationRequested(const QString& key_fingerprint,
const QJsonObject& status);
void SignalErrorOccurred(const QString& error_string,
const QString& reply_data);
private slots:
void on_reply_finished(QNetworkReply* reply);
private:
QString target_key_server_;
QNetworkAccessManager* network_manager_;
};