From 99a5cb5ab598d66b090f01750877ed5b1835ec05 Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 17 Nov 2024 19:22:07 +0100 Subject: [PATCH] feat: add key server sync module --- src/CMakeLists.txt | 3 +- src/m_key_server_sync/CMakeLists.txt | 46 ++++++ src/m_key_server_sync/KeyServerSyncModule.cpp | 137 +++++++++++++++++ src/m_key_server_sync/KeyServerSyncModule.h | 33 ++++ src/m_key_server_sync/VKSInterface.cpp | 141 ++++++++++++++++++ src/m_key_server_sync/VKSInterface.h | 63 ++++++++ 6 files changed, 422 insertions(+), 1 deletion(-) create mode 100644 src/m_key_server_sync/CMakeLists.txt create mode 100644 src/m_key_server_sync/KeyServerSyncModule.cpp create mode 100644 src/m_key_server_sync/KeyServerSyncModule.h create mode 100644 src/m_key_server_sync/VKSInterface.cpp create mode 100644 src/m_key_server_sync/VKSInterface.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 795c335..9ce7323 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -28,4 +28,5 @@ add_subdirectory(m_ver_check) add_subdirectory(m_gpg_info) add_subdirectory(m_pinentry) -add_subdirectory(m_paper_key) \ No newline at end of file +add_subdirectory(m_paper_key) +add_subdirectory(m_key_server_sync) \ No newline at end of file diff --git a/src/m_key_server_sync/CMakeLists.txt b/src/m_key_server_sync/CMakeLists.txt new file mode 100644 index 0000000..c15eed3 --- /dev/null +++ b/src/m_key_server_sync/CMakeLists.txt @@ -0,0 +1,46 @@ +# Copyright (C) 2021-2024 Saturneric +# +# 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 . +# +# 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 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) diff --git a/src/m_key_server_sync/KeyServerSyncModule.cpp b/src/m_key_server_sync/KeyServerSyncModule.cpp new file mode 100644 index 0000000..2247e3d --- /dev/null +++ b/src/m_key_server_sync/KeyServerSyncModule.cpp @@ -0,0 +1,137 @@ +/** + * Copyright (C) 2021-2024 Saturneric + * + * 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 . + * + * 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 starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "KeyServerSyncModule.h" + +#include + +#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; +} \ No newline at end of file diff --git a/src/m_key_server_sync/KeyServerSyncModule.h b/src/m_key_server_sync/KeyServerSyncModule.h new file mode 100644 index 0000000..992647a --- /dev/null +++ b/src/m_key_server_sync/KeyServerSyncModule.h @@ -0,0 +1,33 @@ +/** + * Copyright (C) 2021-2024 Saturneric + * + * 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 . + * + * 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 starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#pragma once + +#include "GFModuleDeclare.h" + +GF_MODULE_API_DECLARE diff --git a/src/m_key_server_sync/VKSInterface.cpp b/src/m_key_server_sync/VKSInterface.cpp new file mode 100644 index 0000000..6bf277a --- /dev/null +++ b/src/m_key_server_sync/VKSInterface.cpp @@ -0,0 +1,141 @@ +/** + * Copyright (C) 2021-2024 Saturneric + * + * 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 . + * + * 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 starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "VKSInterface.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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(); +} diff --git a/src/m_key_server_sync/VKSInterface.h b/src/m_key_server_sync/VKSInterface.h new file mode 100644 index 0000000..d5e02c2 --- /dev/null +++ b/src/m_key_server_sync/VKSInterface.h @@ -0,0 +1,63 @@ +/** + * Copyright (C) 2021-2024 Saturneric + * + * 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 . + * + * 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 starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include + +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_; +};