diff options
author | Saturneric <[email protected]> | 2022-02-06 09:34:14 +0000 |
---|---|---|
committer | Saturneric <[email protected]> | 2022-02-06 09:34:39 +0000 |
commit | cacca627a62ab2eba9eb4d37cfea40629ca0a89a (patch) | |
tree | c596c3c2252ebb0e3792ab4c561d7c7cd3f3e4b0 | |
parent | <refactor>(core, ui): Adjust the core code directory structure. (diff) | |
download | GpgFrontend-cacca627a62ab2eba9eb4d37cfea40629ca0a89a.tar.gz GpgFrontend-cacca627a62ab2eba9eb4d37cfea40629ca0a89a.zip |
<refactor>(src): Move and split the Global Settings Station
1. Move Global Settings Station to core
2. Separate the logic of DataObject
3. Resolve dependencies
41 files changed, 608 insertions, 485 deletions
diff --git a/src/GpgFrontend.h.in b/src/GpgFrontend.h.in index 4de8d55c..3e467f3d 100644 --- a/src/GpgFrontend.h.in +++ b/src/GpgFrontend.h.in @@ -67,12 +67,15 @@ // macros to find resource files -#if defined(MACOS) && defined(RELEASE) +#if defined(MACOS) && defined(RELEASE) #define RESOURCE_DIR(appDir) (appDir + "/../Resources/") +#define RESOURCE_DIR_BOOST_PATH(appDir) (appDir / ".." / "Resources") #elif defined(LINUX) && defined(RELEASE) #define RESOURCE_DIR(appDir) (appDir + "/../share/") +#define RESOURCE_DIR_BOOST_PATH(appDir) (appDir / ".." / "share") #else #define RESOURCE_DIR(appDir) (appDir) +#define RESOURCE_DIR_BOOST_PATH(appDir) (appDir) #endif #endif // GPGFRONTEND_H_IN diff --git a/src/before_exit.cpp b/src/before_exit.cpp index abdf7d62..50604a54 100644 --- a/src/before_exit.cpp +++ b/src/before_exit.cpp @@ -26,7 +26,7 @@ * */ -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" /** * @brief Actions performed before exiting the application @@ -34,5 +34,5 @@ */ void before_exit() { LOG(INFO) << "called"; - GpgFrontend::UI::GlobalSettingStation::GetInstance().ResetRootCerts(); + GpgFrontend::GlobalSettingStation::GetInstance().ResetRootCerts(); } diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 7463e1e7..f5688b8d 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -31,6 +31,7 @@ aux_source_directory(./function GPG_SOURCE) aux_source_directory(./model GPG_SOURCE) aux_source_directory(. GPG_SOURCE) +# define libgpgfrontend_core add_library(gpgfrontend_core STATIC ${GPG_SOURCE}) set(UTILS_DIR ${CMAKE_SOURCE_DIR}/utils) @@ -44,6 +45,19 @@ target_link_libraries(gpgfrontend_core Boost::date_time) target_link_libraries(gpgfrontend_core gpgme assuan gpg-error) # link openssl target_link_libraries(gpgfrontend_core OpenSSL::SSL OpenSSL::Crypto) +# link Qt AES +target_link_libraries(gpgfrontend_core QtAES) +# link vmime +if (NOT LINUX) + target_link_libraries(gpgfrontend_core + gpgfrontend_vmime ssl crypto intl iconv) + if (MINGW) + target_link_libraries(gpgfrontend_core ws2_32) + endif () +else () + target_link_libraries(gpgfrontend_core + gpgfrontend_vmime anl ssl crypto) +endif () # link libarchive if (MINGW) find_library(LIBARCHIVE_LIB libarchive.a) @@ -62,6 +76,7 @@ target_precompile_headers(gpgfrontend_core PUBLIC ${CMAKE_SOURCE_DIR}/src/GpgFrontend.h PUBLIC GpgFrontendCore.h) +# link for different platforms if (MINGW) message(STATUS "Link GPG Static Library For MINGW") target_link_libraries(gpgfrontend_core wsock32) diff --git a/src/core/GpgFrontendCore.h b/src/core/GpgFrontendCore.h index 696f97bc..e4fe7a10 100644 --- a/src/core/GpgFrontendCore.h +++ b/src/core/GpgFrontendCore.h @@ -40,6 +40,14 @@ // Qt includes #include <QtCore> +// vmime includes +#define VMIME_STATIC +#undef VMIME_HAVE_MLANG +#include <vmime/vmime.hpp> + +// libconfig includes +#include <libconfig.h++> + #include "core/GpgModel.h" diff --git a/src/core/function/DataObjectOperator.cpp b/src/core/function/DataObjectOperator.cpp new file mode 100644 index 00000000..f1395152 --- /dev/null +++ b/src/core/function/DataObjectOperator.cpp @@ -0,0 +1,154 @@ +/** + * Copyright (C) 2021 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 <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<[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "DataObjectOperator.h" + +#include <qt-aes/qaesencryption.h> + +#include "core/function/FileOperator.h" +#include "core/function/PassphraseGenerator.h" + +void GpgFrontend::DataObjectOperator::init_app_secure_key() { + FileOperator::WriteFileStd(app_secure_key_path_, + PassphraseGenerator::GetInstance().Generate(256)); + std::filesystem::permissions( + app_secure_key_path_, + std::filesystem::perms::owner_read | std::filesystem::perms::owner_write); +} + +GpgFrontend::DataObjectOperator::DataObjectOperator(int channel) + : SingletonFunctionObject<DataObjectOperator>(channel) { + if (!is_directory(app_secure_path_)) create_directory(app_secure_path_); + + if (!exists(app_secure_key_path_)) { + init_app_secure_key(); + } + + std::string key; + if (!FileOperator::ReadFileStd(app_secure_key_path_.string(), key)) { + LOG(ERROR) << _("Failed to read app secure key file") + << app_secure_key_path_; + } + hash_key_ = QCryptographicHash::hash(QByteArray::fromStdString(key), + QCryptographicHash::Sha256); + + if (!exists(app_data_objs_path_)) create_directory(app_data_objs_path_); +} + +std::string GpgFrontend::DataObjectOperator::SaveDataObj( + const std::string& _key, const nlohmann::json& value) { + std::string _hash_obj_key = {}; + if (_key.empty()) { + _hash_obj_key = + QCryptographicHash::hash( + hash_key_ + QByteArray::fromStdString( + PassphraseGenerator::GetInstance().Generate(32) + + to_iso_extended_string( + boost::posix_time::second_clock::local_time())), + QCryptographicHash::Sha256) + .toHex() + .toStdString(); + } else { + _hash_obj_key = + QCryptographicHash::hash(hash_key_ + QByteArray::fromStdString(_key), + QCryptographicHash::Sha256) + .toHex() + .toStdString(); + } + + const auto obj_path = app_data_objs_path_ / _hash_obj_key; + + QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB, + QAESEncryption::Padding::ISO); + auto encoded = + encryption.encode(QByteArray::fromStdString(to_string(value)), hash_key_); + + GpgFrontend::write_buffer_to_file(obj_path.string(), encoded.toStdString()); + + return _key.empty() ? _hash_obj_key : std::string(); +} + +std::optional<nlohmann::json> GpgFrontend::DataObjectOperator::GetDataObject( + const std::string& _key) { + try { + auto _hash_obj_key = + QCryptographicHash::hash(hash_key_ + QByteArray::fromStdString(_key), + QCryptographicHash::Sha256) + .toHex() + .toStdString(); + + const auto obj_path = app_data_objs_path_ / _hash_obj_key; + + if (!std::filesystem::exists(obj_path)) { + return {}; + } + + std::string buffer; + if (!FileOperator::ReadFileStd(obj_path.string(), buffer)) { + return {}; + } + + auto encoded = QByteArray::fromStdString(buffer); + + QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB, + QAESEncryption::Padding::ISO); + + auto decoded = + encryption.removePadding(encryption.decode(encoded, hash_key_)); + + return nlohmann::json::parse(decoded.toStdString()); + } catch (...) { + return {}; + } +} + +std::optional<nlohmann::json> +GpgFrontend::DataObjectOperator::GetDataObjectByRef(const std::string& _ref) { + if (_ref.size() != 64) return {}; + + try { + const auto& _hash_obj_key = _ref; + const auto obj_path = app_data_objs_path_ / _hash_obj_key; + + if (!std::filesystem::exists(obj_path)) return {}; + + std::string buffer; + if (!FileOperator::ReadFileStd(obj_path.string(), buffer)) return {}; + auto encoded = QByteArray::fromStdString(buffer); + + QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB, + QAESEncryption::Padding::ISO); + + auto decoded = + encryption.removePadding(encryption.decode(encoded, hash_key_)); + + return nlohmann::json::parse(decoded.toStdString()); + } catch (...) { + return {}; + } +} diff --git a/src/core/function/DataObjectOperator.h b/src/core/function/DataObjectOperator.h new file mode 100644 index 00000000..0ce4e313 --- /dev/null +++ b/src/core/function/DataObjectOperator.h @@ -0,0 +1,82 @@ +/** + * Copyright (C) 2021 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 <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<[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#ifndef GPGFRONTEND_DATAOBJECTOPERATOR_H +#define GPGFRONTEND_DATAOBJECTOPERATOR_H + +#include <json/single_include/nlohmann/json.hpp> + +#include "core/GpgFrontendCore.h" +#include "core/GpgFunctionObject.h" +#include "core/function/GlobalSettingStation.h" + +namespace GpgFrontend { + +class DataObjectOperator : public SingletonFunctionObject<DataObjectOperator> { + public: + /** + * @brief DataObjectOperator constructor + * + * @param channel channel + */ + explicit DataObjectOperator( + int channel = SingletonFunctionObject::GetDefaultChannel()); + + std::string SaveDataObj(const std::string &_key, const nlohmann::json &value); + + std::optional<nlohmann::json> GetDataObject(const std::string &_key); + + std::optional<nlohmann::json> GetDataObjectByRef(const std::string &_ref); + + private: + /** + * @brief init the secure key of application data object + * + */ + void init_app_secure_key(); + + GlobalSettingStation &global_setting_station_ = + GlobalSettingStation::GetInstance(); ///< GlobalSettingStation + std::filesystem::path app_secure_path_ = + global_setting_station_.GetAppConfigPath() / + "secure"; ///< Where sensitive information is stored + std::filesystem::path app_secure_key_path_ = + app_secure_path_ / "app.key"; ///< Where the key of data object is stored + std::filesystem::path app_data_objs_path_ = + global_setting_station_.GetAppDataPath() / "data_objs"; ///< Where data + ///< object is + ///< stored + + std::random_device rd_; ///< Random device + std::mt19937 mt_ = std::mt19937(rd_()); ///< Mersenne twister + QByteArray hash_key_; ///< Hash key +}; + +} // namespace GpgFrontend + +#endif // GPGFRONTEND_DATAOBJECTOPERATOR_H diff --git a/src/core/function/GlobalSettingStation.cpp b/src/core/function/GlobalSettingStation.cpp new file mode 100644 index 00000000..7b3e868e --- /dev/null +++ b/src/core/function/GlobalSettingStation.cpp @@ -0,0 +1,141 @@ +/** + * Copyright (C) 2021 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 <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<[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "GlobalSettingStation.h" + +#include <openssl/bio.h> +#include <openssl/pem.h> + +#include <vmime/security/cert/openssl/X509Certificate_OpenSSL.hpp> +#include <vmime/vmime.hpp> + +#include "core/function/FileOperator.h" + +void GpgFrontend::GlobalSettingStation::SyncSettings() noexcept { + using namespace libconfig; + try { + ui_cfg_.writeFile(ui_config_path_.string().c_str()); + LOG(INFO) << _("Updated ui configuration successfully written to") + << ui_config_path_; + + } catch (const FileIOException &fioex) { + LOG(ERROR) << _("I/O error while writing ui configuration file") + << ui_config_path_; + } +} + +GpgFrontend::GlobalSettingStation::GlobalSettingStation(int channel) noexcept + : SingletonFunctionObject<GlobalSettingStation>(channel) { + using namespace std::filesystem; + using namespace libconfig; + + el::Loggers::addFlag(el::LoggingFlag::AutoSpacing); + + LOG(INFO) << _("App Path") << app_path_; + LOG(INFO) << _("App Configure Path") << app_configure_path_; + LOG(INFO) << _("App Data Path") << app_data_path_; + LOG(INFO) << _("App Log Path") << app_log_path_; + LOG(INFO) << _("App Locale Path") << app_locale_path_; + + if (!is_directory(app_configure_path_)) create_directory(app_configure_path_); + + if (!is_directory(app_data_path_)) create_directory(app_data_path_); + + if (!is_directory(app_log_path_)) create_directory(app_log_path_); + + if (!is_directory(ui_config_dir_path_)) create_directory(ui_config_dir_path_); + + if (!exists(ui_config_path_)) { + try { + this->ui_cfg_.writeFile(ui_config_path_.string().c_str()); + LOG(INFO) << _("UserInterface configuration successfully written to") + << ui_config_path_; + + } catch (const FileIOException &fioex) { + LOG(ERROR) + << _("I/O error while writing UserInterface configuration file") + << ui_config_path_; + } + } else { + try { + this->ui_cfg_.readFile(ui_config_path_.string().c_str()); + LOG(INFO) << _("UserInterface configuration successfully read from") + << ui_config_path_; + } catch (const FileIOException &fioex) { + LOG(ERROR) << _("I/O error while reading UserInterface configure file"); + } catch (const ParseException &pex) { + LOG(ERROR) << _("Parse error at ") << pex.getFile() << ":" + << pex.getLine() << " - " << pex.getError(); + } + } +} + +void GpgFrontend::GlobalSettingStation::AddRootCert( + const std::filesystem::path &path) { + std::string out_buffer; + if (!FileOperator::ReadFileStd(path.string(), out_buffer)) { + LOG(ERROR) << _("Failed to read root certificate file") << path; + return; + } + + auto mem_bio = std::shared_ptr<BIO>( + BIO_new_mem_buf(out_buffer.data(), static_cast<int>(out_buffer.size())), + [](BIO *_p) { BIO_free(_p); }); + + auto x509 = std::shared_ptr<X509>( + PEM_read_bio_X509(mem_bio.get(), nullptr, nullptr, nullptr), + [](X509 *_p) { X509_free(_p); }); + + if (!x509) return; + + root_certs_.push_back(x509); +} + +vmime::shared_ptr<vmime::security::cert::defaultCertificateVerifier> +GpgFrontend::GlobalSettingStation::GetCertVerifier() const { + auto p_cv = + vmime::make_shared<vmime::security::cert::defaultCertificateVerifier>(); + + std::vector<vmime::shared_ptr<vmime::security::cert::X509Certificate>> + _root_certs; + for (const auto &cert : root_certs_) { + _root_certs.push_back( + std::make_shared<vmime::security::cert::X509Certificate_OpenSSL>( + cert.get())); + } + return p_cv; +} + +const std::vector<std::shared_ptr<X509>> + &GpgFrontend::GlobalSettingStation::GetRootCerts() { + return root_certs_; +} + +void GpgFrontend::GlobalSettingStation::init_app_secure_key() {} + +GpgFrontend::GlobalSettingStation::~GlobalSettingStation() noexcept = default; diff --git a/src/ui/settings/GlobalSettingStation.h b/src/core/function/GlobalSettingStation.h index 3d9f4024..d6521c8a 100644 --- a/src/ui/settings/GlobalSettingStation.h +++ b/src/core/function/GlobalSettingStation.h @@ -33,37 +33,31 @@ #include <boost/filesystem/operations.hpp> #include <boost/filesystem/path.hpp> -#include <nlohmann/json.hpp> #include "GpgFrontendBuildInstallInfo.h" -#include "ui/GpgFrontendUI.h" +#include "core/GpgFrontendCore.h" +#include "core/GpgFunctionObject.h" namespace vmime::security::cert { class defaultCertificateVerifier; class X509Certificate; -} // namespace vmime::security::cert +} // namespace vmime::security::cert -namespace GpgFrontend::UI { +namespace GpgFrontend { /** * @brief * */ -class GlobalSettingStation : public QObject { - Q_OBJECT -public: - /** - * @brief Get the Instance object - * - * @return GlobalSettingStation& - */ - static GlobalSettingStation &GetInstance(); - +class GlobalSettingStation + : public SingletonFunctionObject<GlobalSettingStation> { + public: /** * @brief Construct a new Global Setting Station object * */ - GlobalSettingStation() noexcept; + explicit GlobalSettingStation( + int channel = SingletonFunctionObject::GetDefaultChannel()) noexcept; /** * @brief Destroy the Global Setting Station object @@ -85,6 +79,10 @@ public: */ [[nodiscard]] std::filesystem::path GetAppDir() const { return app_path_; } + [[nodiscard]] std::filesystem::path GetAppDataPath() const { + return app_data_path_; + } + /** * @brief Get the Log Dir object * @@ -107,6 +105,10 @@ public: return db_path; } + [[nodiscard]] std::filesystem::path GetAppConfigPath() const { + return app_configure_path_; + } + /** * @brief Get the Standalone Gpg Bin Dir object * @@ -174,100 +176,57 @@ public: void ResetRootCerts() { root_certs_.clear(); } /** - * @brief + * @brief sync the settings to the file * */ void SyncSettings() noexcept; - /** - * @brief - * - * @param _key - * @param value - * @return std::string - */ - - std::string SaveDataObj(const std::string &_key, const nlohmann::json &value); - - /** - * @brief Get the Data Object object - * - * @param _key - * @return std::optional<nlohmann::json> - */ - std::optional<nlohmann::json> GetDataObject(const std::string &_key); - - /** - * @brief Get the Data Object By Ref object - * - * @param _ref - * @return std::optional<nlohmann::json> - */ - std::optional<nlohmann::json> GetDataObjectByRef(const std::string &_ref); - -private: + private: std::filesystem::path app_path_ = - qApp->applicationDirPath().toStdString(); ///< Program Location + qApp->applicationDirPath().toStdString(); ///< Program Location std::filesystem::path app_data_path_ = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) - .toStdString(); ///< Program Data Location + .toStdString(); ///< Program Data Location std::filesystem::path app_log_path_ = - app_data_path_ / "logs"; ///< Program Data Location + app_data_path_ / "logs"; ///< Program Data Location std::filesystem::path app_data_objs_path_ = - app_data_path_ / "objs"; ///< Object storage path + app_data_path_ / "objs"; ///< Object storage path #ifdef LINUX_INSTALL_BUILD std::filesystem::path app_resource_path_ = std::filesystem::path(APP_LOCALSTATE_PATH) / - "gpgfrontend"; ///< Program Data Location + "gpgfrontend"; ///< Program Data Location #else std::filesystem::path app_resource_path_ = - RESOURCE_DIR_BOOST_PATH(app_path_); ///< Program Data Location + RESOURCE_DIR_BOOST_PATH(app_path_); ///< Program Data Location #endif #ifdef LINUX_INSTALL_BUILD std::filesystem::path app_locale_path_ = - std::string(APP_LOCALE_PATH); ///< Program Data Location + std::string(APP_LOCALE_PATH); ///< Program Data Location #else std::filesystem::path app_locale_path_ = - app_resource_path_ / "locales"; ///< Program Data Location + app_resource_path_ / "locales"; ///< Program Data Location #endif std::filesystem::path app_configure_path_ = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) - .toStdString(); ///< Program Configure Location - std::filesystem::path app_secure_path_ = - app_configure_path_ / "secure"; ///< Where sensitive information is stored - std::filesystem::path app_secure_key_path_ = - app_secure_path_ / "app.key"; ///< + .toStdString(); ///< Program Configure Location std::filesystem::path ui_config_dir_path_ = app_configure_path_ / - "UserInterface"; ///< Configure File Directory Location + "UserInterface"; ///< Configure File Directory Location std::filesystem::path ui_config_path_ = - ui_config_dir_path_ / "ui.cfg"; ///< UI Configure File Location - - libconfig::Config ui_cfg_; ///< - std::vector<std::shared_ptr<X509>> root_certs_; ///< - std::random_device rd_; ///< - std::mt19937 mt_; ///< - QByteArray hash_key_; ///< + ui_config_dir_path_ / "ui.cfg"; ///< UI Configure File Location - static std::unique_ptr<GlobalSettingStation> instance_; ///< + libconfig::Config ui_cfg_; ///< + std::vector<std::shared_ptr<X509>> root_certs_; ///< /** * @brief * */ void init_app_secure_key(); - - /** - * @brief - * - * @param len - * @return std::string - */ - std::string generate_passphrase(int len); }; -} // namespace GpgFrontend::UI +} // namespace GpgFrontend -#endif // GPGFRONTEND_GLOBALSETTINGSTATION_H +#endif // GPGFRONTEND_GLOBALSETTINGSTATION_H diff --git a/src/core/function/KeyPackageOperator.cpp b/src/core/function/KeyPackageOperator.cpp index 89210987..7ff65b50 100644 --- a/src/core/function/KeyPackageOperator.cpp +++ b/src/core/function/KeyPackageOperator.cpp @@ -28,17 +28,17 @@ #include "KeyPackageOperator.h" -#include "qt-aes/qaesencryption.h" - #include "FileOperator.h" +#include "function/PassphraseGenerator.h" #include "function/gpg/GpgKeyGetter.h" #include "function/gpg/GpgKeyImportExporter.h" +#include "qt-aes/qaesencryption.h" namespace GpgFrontend { bool KeyPackageOperator::GeneratePassphrase( const std::filesystem::path& phrase_path, std::string& phrase) { - phrase = generate_passphrase(256); + phrase = PassphraseGenerator::GetInstance().Generate(256); return FileOperator::WriteFileStd(phrase_path, phrase); } @@ -65,8 +65,8 @@ bool KeyPackageOperator::GenerateKeyPackage( bool KeyPackageOperator::ImportKeyPackage( const std::filesystem::path& key_package_path, - const std::filesystem::path& phrase_path, GpgFrontend::GpgImportInformation &import_info) { - + const std::filesystem::path& phrase_path, + GpgFrontend::GpgImportInformation& import_info) { std::string encrypted_data; FileOperator::ReadFileStd(key_package_path, encrypted_data); diff --git a/src/core/function/KeyPackageOperator.h b/src/core/function/KeyPackageOperator.h index ee81e86e..cd344688 100644 --- a/src/core/function/KeyPackageOperator.h +++ b/src/core/function/KeyPackageOperator.h @@ -39,7 +39,7 @@ namespace GpgFrontend { * */ class KeyPackageOperator { -public: + public: /** * @brief generate passphrase for key package and save it to file * @@ -87,44 +87,21 @@ public: const std::filesystem::path &phrase_path, GpgFrontend::GpgImportInformation &import_info); -private: - /** - * @brief genearte passphrase - * - * @param len length of the passphrase - * @return std::string passphrase - */ - static std::string generate_passphrase(const int len) { - std::random_device rd_; ///< Random device - auto mt_ = std::mt19937(rd_()); ///< Mersenne twister - - std::uniform_int_distribution<int> dist(999, 99999); - static const char alphanum[] = "0123456789" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz"; - std::string tmp_str; - tmp_str.reserve(len); - - for (int i = 0; i < len; ++i) { - tmp_str += alphanum[dist(mt_) % (sizeof(alphanum) - 1)]; - } - return tmp_str; - } - + private: /** * @brief generate key package name * * @return std::string key package name */ static std::string generate_key_package_name() { - std::random_device rd_; ///< Random device - auto mt_ = std::mt19937(rd_()); ///< Mersenne twister + std::random_device rd_; ///< Random device + auto mt_ = std::mt19937(rd_()); ///< Mersenne twister std::uniform_int_distribution<int> dist(999, 99999); auto file_string = boost::format("KeyPackage_%1%") % dist(mt_); return file_string.str(); } }; -} // namespace GpgFrontend +} // namespace GpgFrontend -#endif // GPGFRONTEND_KEYPACKAGEOPERATOR_H +#endif // GPGFRONTEND_KEYPACKAGEOPERATOR_H diff --git a/src/core/function/PassphraseGenerator.cpp b/src/core/function/PassphraseGenerator.cpp new file mode 100644 index 00000000..0267edda --- /dev/null +++ b/src/core/function/PassphraseGenerator.cpp @@ -0,0 +1,29 @@ +/** +* Copyright (C) 2021 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 <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<[email protected]> starting on May 12, 2021. +* +* SPDX-License-Identifier: GPL-3.0-or-later +* + */ + +#include "PassphraseGenerator.h" diff --git a/src/core/function/PassphraseGenerator.h b/src/core/function/PassphraseGenerator.h new file mode 100644 index 00000000..5e55b2dd --- /dev/null +++ b/src/core/function/PassphraseGenerator.h @@ -0,0 +1,73 @@ +/** + * Copyright (C) 2021 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 <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<[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#ifndef GPGFRONTEND_PASSPHRASEGENERATOR_H +#define GPGFRONTEND_PASSPHRASEGENERATOR_H + +#include "core/GpgFrontendCore.h" +#include "core/GpgFunctionObject.h" + +namespace GpgFrontend { + +/** + * @brief The PassphraseGenerator class + * + * This class is used to generate a passphrase. + */ +class PassphraseGenerator + : public SingletonFunctionObject<PassphraseGenerator> { + public: + + /** + * @brief PassphraseGenerator constructor + * + * @param channel The channel to use + */ + explicit PassphraseGenerator( + int channel = SingletonFunctionObject::GetDefaultChannel()) + : SingletonFunctionObject<PassphraseGenerator>(channel) {} + + /** + * @brief generate passphrase + * + * @param len length of the passphrase + * @return std::string passphrase + */ + std::string Generate(int len) { + std::uniform_int_distribution<int> dist(999, 99999); + auto file_string = boost::format("KeyPackage_%1%") % dist(mt_); + return file_string.str(); + } + + std::random_device rd_; ///< Random device + std::mt19937 mt_ = std::mt19937(rd_()); ///< Mersenne twister +}; + +} // namespace GpgFrontend + +#endif // GPGFRONTEND_PASSPHRASEGENERATOR_H diff --git a/src/init.cpp b/src/init.cpp index b1ea8df8..ccfeca90 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -28,7 +28,7 @@ #include <boost/date_time.hpp> -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" /** * @brief Get the files of a given directory @@ -72,7 +72,7 @@ void init_logging() { // get the log directory auto logfile_path = - (GpgFrontend::UI::GlobalSettingStation::GetInstance().GetLogDir() / + (GpgFrontend::GlobalSettingStation::GetInstance().GetLogDir() / to_iso_string(now)); logfile_path.replace_extension(".log"); defaultConf.setGlobally(el::ConfigurationType::Filename, @@ -90,10 +90,10 @@ void init_logging() { void init_certs() { // get the certificate directory auto cert_file_paths = get_files_of_directory( - GpgFrontend::UI::GlobalSettingStation::GetInstance().GetCertsDir()); + GpgFrontend::GlobalSettingStation::GetInstance().GetCertsDir()); // get the instance of the GlobalSettingStation - auto& _instance = GpgFrontend::UI::GlobalSettingStation::GetInstance(); + auto& _instance = GpgFrontend::GlobalSettingStation::GetInstance(); for (const auto& cert_file_path : cert_file_paths) { // add the certificate to the store _instance.AddRootCert(cert_file_path); @@ -110,7 +110,7 @@ void init_certs() { void init_locale() { // get the instance of the GlobalSettingStation auto& settings = - GpgFrontend::UI::GlobalSettingStation::GetInstance().GetUISettings(); + GpgFrontend::GlobalSettingStation::GetInstance().GetUISettings(); // create general settings if not exist if (!settings.exists("general") || @@ -123,7 +123,7 @@ void init_locale() { general.add("lang", libconfig::Setting::TypeString) = ""; // sync the settings to the file - GpgFrontend::UI::GlobalSettingStation::GetInstance().SyncSettings(); + GpgFrontend::GlobalSettingStation::GetInstance().SyncSettings(); LOG(INFO) << "current system locale" << setlocale(LC_ALL, nullptr); @@ -136,7 +136,7 @@ void init_locale() { LOG(INFO) << "lang from settings" << lang; LOG(INFO) << "project name" << PROJECT_NAME; LOG(INFO) << "locales path" - << GpgFrontend::UI::GlobalSettingStation::GetInstance() + << GpgFrontend::GlobalSettingStation::GetInstance() .GetLocaleDir() .c_str(); @@ -177,7 +177,7 @@ void init_locale() { #endif bindtextdomain(PROJECT_NAME, - GpgFrontend::UI::GlobalSettingStation::GetInstance() + GpgFrontend::GlobalSettingStation::GetInstance() .GetLocaleDir() .string() .c_str()); diff --git a/src/main.cpp b/src/main.cpp index 7e65a847..9886fa0a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -133,7 +133,7 @@ int main(int argc, char* argv[]) { #if !defined(RELEASE) && defined(WINDOWS) // css std::filesystem::path css_path = - GpgFrontend::UI::GlobalSettingStation::GetInstance().GetResourceDir() / + GpgFrontend::GlobalSettingStation::GetInstance().GetResourceDir() / "css" / "default.qss"; QFile file(css_path.string().c_str()); file.open(QFile::ReadOnly); @@ -144,9 +144,9 @@ int main(int argc, char* argv[]) { #ifdef GPG_STANDALONE_MODE LOG(INFO) << "GPG_STANDALONE_MODE Enabled"; - auto gpg_path = GpgFrontend::UI::GlobalSettingStation::GetInstance() + auto gpg_path = GpgFrontend::GlobalSettingStation::GetInstance() .GetStandaloneGpgBinDir(); - auto db_path = GpgFrontend::UI::GlobalSettingStation::GetInstance() + auto db_path = GpgFrontend::GlobalSettingStation::GetInstance() .GetStandaloneDatabaseDir(); GpgFrontend::GpgContext::CreateInstance( GpgFrontend::SingletonFunctionObject< diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index 608747f1..12810174 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -42,52 +42,48 @@ if (SMTP_SUPPORT) aux_source_directory(mail UI_SOURCE) endif () -# libgpgfrontend_ui +# define libgpgfrontend_ui add_library(gpgfrontend_ui STATIC ${UI_SOURCE}) -set(GPGFRONTEND_UI_LIB_NAME gpgfrontend_ui) # link smtp-mime if (SMTP_SUPPORT) - target_link_libraries(${GPGFRONTEND_UI_LIB_NAME} + target_link_libraries(gpgfrontend_ui smtp-mime) endif () -# Qt AES -target_link_libraries(${GPGFRONTEND_UI_LIB_NAME} - QtAES) # link Qt -target_link_libraries(${GPGFRONTEND_UI_LIB_NAME} +target_link_libraries(gpgfrontend_ui Qt5::Network Qt5::PrintSupport Qt5::Widgets Qt5::Test Qt5::Core) # link vmime if (NOT LINUX) - target_link_libraries(${GPGFRONTEND_UI_LIB_NAME} + target_link_libraries(gpgfrontend_ui gpgfrontend_vmime ssl crypto intl iconv) if (MINGW) - target_link_libraries(${GPGFRONTEND_UI_LIB_NAME} ws2_32) + target_link_libraries(gpgfrontend_ui ws2_32) endif () else () - target_link_libraries(${GPGFRONTEND_UI_LIB_NAME} + target_link_libraries(gpgfrontend_ui gpgfrontend_vmime anl ssl crypto) endif () # link gpgfrontend_core -target_link_libraries(${GPGFRONTEND_UI_LIB_NAME} +target_link_libraries(gpgfrontend_ui gpgfrontend_core) # link encoding_detect -target_link_libraries(${GPGFRONTEND_UI_LIB_NAME} encoding_detect) +target_link_libraries(gpgfrontend_ui encoding_detect) # set up pch -target_precompile_headers(${GPGFRONTEND_UI_LIB_NAME} PUBLIC GpgFrontendUI.h) +target_precompile_headers(gpgfrontend_ui PUBLIC GpgFrontendUI.h) # add ui generator include path target_include_directories(gpgfrontend_ui PUBLIC - ${CMAKE_CURRENT_BINARY_DIR}/${GPGFRONTEND_UI_LIB_NAME}_autogen/include) + ${CMAKE_CURRENT_BINARY_DIR}/gpgfrontend_ui_autogen/include) # for xcode archive build if (XCODE_BUILD) - set_target_properties(${GPGFRONTEND_UI_LIB_NAME} + set_target_properties(gpgfrontend_ui PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE} LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE} diff --git a/src/ui/GpgFrontendUI.h b/src/ui/GpgFrontendUI.h index 1b271d1c..cfe9a8c5 100644 --- a/src/ui/GpgFrontendUI.h +++ b/src/ui/GpgFrontendUI.h @@ -53,28 +53,8 @@ #define LIBCONFIGXX_STATIC #include <qt-aes/qaesencryption.h> -#include <libconfig.h++> - #ifdef SMTP_SUPPORT #include <SmtpMime> #endif -#define VMIME_STATIC -#undef VMIME_HAVE_MLANG -#include <vmime/vmime.hpp> - -/** - * Resources File(s) Path Vars - */ -#if defined(MACOS) && defined(RELEASE) -#define RESOURCE_DIR(appDir) (appDir + "/../Resources/") -#define RESOURCE_DIR_BOOST_PATH(appDir) (appDir / ".." / "Resources") -#elif defined(LINUX) && defined(RELEASE) -#define RESOURCE_DIR(appDir) (appDir + "/../share/") -#define RESOURCE_DIR_BOOST_PATH(appDir) (appDir / ".." / "share") -#else -#define RESOURCE_DIR(appDir) (appDir) -#define RESOURCE_DIR_BOOST_PATH(appDir) (appDir) -#endif - #endif // GPGFRONTEND_GPGFRONTENDUI_H diff --git a/src/ui/KeyMgmt.cpp b/src/ui/KeyMgmt.cpp index 070ecd7f..56097355 100755 --- a/src/ui/KeyMgmt.cpp +++ b/src/ui/KeyMgmt.cpp @@ -39,7 +39,7 @@ #include "ui/import_export/ExportKeyPackageDialog.h" #include "ui/key_generate/SubkeyGenerateDialog.h" #include "ui/main_window/MainWindow.h" -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" namespace GpgFrontend::UI { KeyMgmt::KeyMgmt(QWidget* parent) : QMainWindow(parent) { @@ -435,7 +435,7 @@ void KeyMgmt::SlotGenerateSubKey() { } void KeyMgmt::SlotSaveWindowState() { auto& settings = - GpgFrontend::UI::GlobalSettingStation::GetInstance().GetUISettings(); + GpgFrontend::GlobalSettingStation::GetInstance().GetUISettings(); if (!settings.exists("window") || settings.lookup("window").getType() != libconfig::Setting::TypeGroup) diff --git a/src/ui/UserInterfaceUtils.cpp b/src/ui/UserInterfaceUtils.cpp index 8e2b0b18..8353d28b 100644 --- a/src/ui/UserInterfaceUtils.cpp +++ b/src/ui/UserInterfaceUtils.cpp @@ -35,7 +35,7 @@ #include "ui/SignalStation.h" #include "ui/dialog/WaitingDialog.h" #include "ui/mail/SendMailDialog.h" -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" #include "ui/widgets/InfoBoardWidget.h" #include "ui/widgets/TextEdit.h" diff --git a/src/ui/dialog/Wizard.cpp b/src/ui/dialog/Wizard.cpp index 8d6d6890..0f051874 100644 --- a/src/ui/dialog/Wizard.cpp +++ b/src/ui/dialog/Wizard.cpp @@ -28,7 +28,7 @@ #include "Wizard.h" -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" namespace GpgFrontend::UI { diff --git a/src/ui/help/AboutDialog.cpp b/src/ui/help/AboutDialog.cpp index 53917217..4a23c314 100644 --- a/src/ui/help/AboutDialog.cpp +++ b/src/ui/help/AboutDialog.cpp @@ -29,7 +29,7 @@ #include "ui/help/AboutDialog.h" #include "GpgFrontendBuildInfo.h" -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" #include "ui/thread/VersionCheckThread.h" namespace GpgFrontend::UI { diff --git a/src/ui/import_export/KeyServerImportDialog.cpp b/src/ui/import_export/KeyServerImportDialog.cpp index 59323e42..c35e3db7 100644 --- a/src/ui/import_export/KeyServerImportDialog.cpp +++ b/src/ui/import_export/KeyServerImportDialog.cpp @@ -32,7 +32,7 @@ #include "core/function/gpg/GpgKeyImportExporter.h" #include "ui/SignalStation.h" -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" namespace GpgFrontend::UI { @@ -560,7 +560,7 @@ void KeyServerImportDialog::slot_save_window_state() { if (m_automatic_) return; auto& settings = - GpgFrontend::UI::GlobalSettingStation::GetInstance().GetUISettings(); + GpgFrontend::GlobalSettingStation::GetInstance().GetUISettings(); if (!settings.exists("window") || settings.lookup("window").getType() != libconfig::Setting::TypeGroup) diff --git a/src/ui/import_export/KeyUploadDialog.cpp b/src/ui/import_export/KeyUploadDialog.cpp index ce6c8fed..a0436a8e 100644 --- a/src/ui/import_export/KeyUploadDialog.cpp +++ b/src/ui/import_export/KeyUploadDialog.cpp @@ -32,7 +32,7 @@ #include "core/function/gpg/GpgKeyGetter.h" #include "core/function/gpg/GpgKeyImportExporter.h" -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" namespace GpgFrontend::UI { diff --git a/src/ui/key_generate/KeygenDialog.cpp b/src/ui/key_generate/KeygenDialog.cpp index 826a00af..6df0bcf0 100644 --- a/src/ui/key_generate/KeygenDialog.cpp +++ b/src/ui/key_generate/KeygenDialog.cpp @@ -31,7 +31,7 @@ #include "dialog/WaitingDialog.h" #include "core/function/gpg/GpgKeyOpera.h" #include "ui/SignalStation.h" -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" namespace GpgFrontend::UI { diff --git a/src/ui/key_generate/SubkeyGenerateDialog.cpp b/src/ui/key_generate/SubkeyGenerateDialog.cpp index 829fc62d..4eb041aa 100644 --- a/src/ui/key_generate/SubkeyGenerateDialog.cpp +++ b/src/ui/key_generate/SubkeyGenerateDialog.cpp @@ -30,7 +30,7 @@ #include "core/function/gpg/GpgKeyGetter.h" #include "core/function/gpg/GpgKeyOpera.h" #include "ui/SignalStation.h" -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" namespace GpgFrontend::UI { diff --git a/src/ui/keypair_details/KeySetExpireDateDialog.cpp b/src/ui/keypair_details/KeySetExpireDateDialog.cpp index 62d2f095..79325e8b 100644 --- a/src/ui/keypair_details/KeySetExpireDateDialog.cpp +++ b/src/ui/keypair_details/KeySetExpireDateDialog.cpp @@ -33,7 +33,7 @@ #include "core/function/gpg/GpgKeyGetter.h" #include "core/function/gpg/GpgKeyOpera.h" #include "ui/SignalStation.h" -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" #include "ui_ModifiedExpirationDateTime.h" namespace GpgFrontend::UI { diff --git a/src/ui/mail/SendMailDialog.cpp b/src/ui/mail/SendMailDialog.cpp index cf343f24..137c941f 100644 --- a/src/ui/mail/SendMailDialog.cpp +++ b/src/ui/mail/SendMailDialog.cpp @@ -35,7 +35,7 @@ #include "ui_SendMailDialog.h" #ifdef SMTP_SUPPORT -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" #include "ui/thread/SMTPSendMailThread.h" #endif diff --git a/src/ui/main_window/MainWindow.cpp b/src/ui/main_window/MainWindow.cpp index 18b40f78..66c240a9 100644 --- a/src/ui/main_window/MainWindow.cpp +++ b/src/ui/main_window/MainWindow.cpp @@ -33,7 +33,7 @@ #include "ui/thread/VersionCheckThread.h" #endif #include "ui/SignalStation.h" -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" #include "ui/struct/SettingsObject.h" namespace GpgFrontend::UI { diff --git a/src/ui/main_window/MainWindowFileSlotFunction.cpp b/src/ui/main_window/MainWindowFileSlotFunction.cpp index 31a79297..21516c6d 100644 --- a/src/ui/main_window/MainWindowFileSlotFunction.cpp +++ b/src/ui/main_window/MainWindowFileSlotFunction.cpp @@ -30,7 +30,7 @@ #include "core/function/gpg/GpgFileOpera.h" #include "core/function/gpg/GpgKeyGetter.h" #include "ui/UserInterfaceUtils.h" -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" #include "ui/widgets/SignersPicker.h" namespace GpgFrontend::UI { diff --git a/src/ui/main_window/MainWindowSlotFunction.cpp b/src/ui/main_window/MainWindowSlotFunction.cpp index 1dc58f5b..6286ed49 100644 --- a/src/ui/main_window/MainWindowSlotFunction.cpp +++ b/src/ui/main_window/MainWindowSlotFunction.cpp @@ -44,7 +44,7 @@ #include "core/function/gpg/GpgKeyImportExporter.h" #include "ui/UserInterfaceUtils.h" #include "ui/help/AboutDialog.h" -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" #include "ui/widgets/SignersPicker.h" namespace GpgFrontend::UI { diff --git a/src/ui/main_window/MainWindowSlotUI.cpp b/src/ui/main_window/MainWindowSlotUI.cpp index f790c98e..9b7e358d 100644 --- a/src/ui/main_window/MainWindowSlotUI.cpp +++ b/src/ui/main_window/MainWindowSlotUI.cpp @@ -28,7 +28,7 @@ #include "MainWindow.h" #include "ui/UserInterfaceUtils.h" -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" namespace GpgFrontend::UI { diff --git a/src/ui/settings/GlobalSettingStation.cpp b/src/ui/settings/GlobalSettingStation.cpp deleted file mode 100644 index 6f1dbaf7..00000000 --- a/src/ui/settings/GlobalSettingStation.cpp +++ /dev/null @@ -1,294 +0,0 @@ -/** - * Copyright (C) 2021 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 <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<[email protected]> starting on May 12, 2021. - * - * SPDX-License-Identifier: GPL-3.0-or-later - * - */ - -#include "GlobalSettingStation.h" - -#include <openssl/bio.h> -#include <openssl/pem.h> - -#include <vmime/security/cert/openssl/X509Certificate_OpenSSL.hpp> -#include <vmime/vmime.hpp> - -#include "core/function/FileOperator.h" - -std::unique_ptr<GpgFrontend::UI::GlobalSettingStation> - GpgFrontend::UI::GlobalSettingStation::instance_ = nullptr; - -GpgFrontend::UI::GlobalSettingStation & -GpgFrontend::UI::GlobalSettingStation::GetInstance() { - if (instance_ == nullptr) { - instance_ = std::make_unique<GlobalSettingStation>(); - } - return *instance_; -} - -void GpgFrontend::UI::GlobalSettingStation::SyncSettings() noexcept { - using namespace libconfig; - try { - ui_cfg_.writeFile(ui_config_path_.string().c_str()); - LOG(INFO) << _("Updated ui configuration successfully written to") - << ui_config_path_; - - } catch (const FileIOException &fioex) { - LOG(ERROR) << _("I/O error while writing ui configuration file") - << ui_config_path_; - } -} - -GpgFrontend::UI::GlobalSettingStation::GlobalSettingStation() noexcept { - using namespace std::filesystem; - using namespace libconfig; - - el::Loggers::addFlag(el::LoggingFlag::AutoSpacing); - - LOG(INFO) << _("App Path") << app_path_; - LOG(INFO) << _("App Configure Path") << app_configure_path_; - LOG(INFO) << _("App Data Path") << app_data_path_; - LOG(INFO) << _("App Log Path") << app_log_path_; - LOG(INFO) << _("App Locale Path") << app_locale_path_; - - if (!is_directory(app_configure_path_)) - create_directory(app_configure_path_); - - if (!is_directory(app_data_path_)) - create_directory(app_data_path_); - - if (!is_directory(app_log_path_)) - create_directory(app_log_path_); - - if (!is_directory(ui_config_dir_path_)) - create_directory(ui_config_dir_path_); - - if (!is_directory(app_secure_path_)) - create_directory(app_secure_path_); - - if (!exists(app_secure_key_path_)) { - init_app_secure_key(); - } - - std::string key; - if (!FileOperator::ReadFileStd(app_secure_key_path_.string(), key)) { - LOG(ERROR) << _("Failed to read app secure key file") - << app_secure_key_path_; - } - hash_key_ = QCryptographicHash::hash(QByteArray::fromStdString(key), - QCryptographicHash::Sha256); - - if (!exists(app_data_objs_path_)) - create_directory(app_data_objs_path_); - - if (!exists(ui_config_path_)) { - try { - this->ui_cfg_.writeFile(ui_config_path_.string().c_str()); - LOG(INFO) << _("UserInterface configuration successfully written to") - << ui_config_path_; - - } catch (const FileIOException &fioex) { - LOG(ERROR) - << _("I/O error while writing UserInterface configuration file") - << ui_config_path_; - } - } else { - try { - this->ui_cfg_.readFile(ui_config_path_.string().c_str()); - LOG(INFO) << _("UserInterface configuration successfully read from") - << ui_config_path_; - } catch (const FileIOException &fioex) { - LOG(ERROR) << _("I/O error while reading UserInterface configure file"); - } catch (const ParseException &pex) { - LOG(ERROR) << _("Parse error at ") << pex.getFile() << ":" - << pex.getLine() << " - " << pex.getError(); - } - } -} - -void GpgFrontend::UI::GlobalSettingStation::AddRootCert( - const std::filesystem::path &path) { - - std::string out_buffer; - if (!FileOperator::ReadFileStd(path.string(), out_buffer)) { - LOG(ERROR) << _("Failed to read root certificate file") << path; - return; - } - - auto mem_bio = std::shared_ptr<BIO>( - BIO_new_mem_buf(out_buffer.data(), static_cast<int>(out_buffer.size())), - [](BIO *_p) { BIO_free(_p); }); - - auto x509 = std::shared_ptr<X509>( - PEM_read_bio_X509(mem_bio.get(), nullptr, nullptr, nullptr), - [](X509 *_p) { X509_free(_p); }); - - if (!x509) - return; - - root_certs_.push_back(x509); -} - -vmime::shared_ptr<vmime::security::cert::defaultCertificateVerifier> -GpgFrontend::UI::GlobalSettingStation::GetCertVerifier() const { - auto p_cv = - vmime::make_shared<vmime::security::cert::defaultCertificateVerifier>(); - - std::vector<vmime::shared_ptr<vmime::security::cert::X509Certificate>> - _root_certs; - for (const auto &cert : root_certs_) { - _root_certs.push_back( - std::make_shared<vmime::security::cert::X509Certificate_OpenSSL>( - cert.get())); - } - return p_cv; -} - -const std::vector<std::shared_ptr<X509>> & -GpgFrontend::UI::GlobalSettingStation::GetRootCerts() { - return root_certs_; -} - -std::string -GpgFrontend::UI::GlobalSettingStation::generate_passphrase(int len) { - std::uniform_int_distribution<int> dist(999, 99999); - static const char alphanum[] = "0123456789" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz"; - std::string tmp_str; - tmp_str.reserve(len); - - for (int i = 0; i < len; ++i) { - tmp_str += alphanum[dist(mt_) % (sizeof(alphanum) - 1)]; - } - - return tmp_str; -} - -void GpgFrontend::UI::GlobalSettingStation::init_app_secure_key() { - GpgFrontend::write_buffer_to_file(app_secure_key_path_.string(), - generate_passphrase(256)); - std::filesystem::permissions(app_secure_key_path_, - std::filesystem::perms::owner_read | - std::filesystem::perms::owner_write); -} - -std::string GpgFrontend::UI::GlobalSettingStation::SaveDataObj( - const std::string &_key, const nlohmann::json &value) { - std::string _hash_obj_key = {}; - if (_key.empty()) { - _hash_obj_key = - QCryptographicHash::hash( - hash_key_ + QByteArray::fromStdString( - generate_passphrase(32) + - to_iso_extended_string( - boost::posix_time::second_clock::local_time())), - QCryptographicHash::Sha256) - .toHex() - .toStdString(); - } else { - _hash_obj_key = - QCryptographicHash::hash(hash_key_ + QByteArray::fromStdString(_key), - QCryptographicHash::Sha256) - .toHex() - .toStdString(); - } - - const auto obj_path = app_data_objs_path_ / _hash_obj_key; - - QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB, - QAESEncryption::Padding::ISO); - auto encoded = - encryption.encode(QByteArray::fromStdString(to_string(value)), hash_key_); - - GpgFrontend::write_buffer_to_file(obj_path.string(), encoded.toStdString()); - - return _key.empty() ? _hash_obj_key : std::string(); -} - -std::optional<nlohmann::json> -GpgFrontend::UI::GlobalSettingStation::GetDataObject(const std::string &_key) { - try { - auto _hash_obj_key = - QCryptographicHash::hash(hash_key_ + QByteArray::fromStdString(_key), - QCryptographicHash::Sha256) - .toHex() - .toStdString(); - - const auto obj_path = app_data_objs_path_ / _hash_obj_key; - - if (!std::filesystem::exists(obj_path)) { - return {}; - } - - std::string buffer; - if (!FileOperator::ReadFileStd(obj_path.string(), buffer)) { - return {}; - } - - auto encoded = QByteArray::fromStdString(buffer); - - QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB, - QAESEncryption::Padding::ISO); - - auto decoded = - encryption.removePadding(encryption.decode(encoded, hash_key_)); - - return nlohmann::json::parse(decoded.toStdString()); - } catch (...) { - return {}; - } -} -std::optional<nlohmann::json> -GpgFrontend::UI::GlobalSettingStation::GetDataObjectByRef( - const std::string &_ref) { - if (_ref.size() != 64) - return {}; - - try { - auto _hash_obj_key = _ref; - const auto obj_path = app_data_objs_path_ / _hash_obj_key; - - if (!std::filesystem::exists(obj_path)) - return {}; - - std::string buffer; - if (!FileOperator::ReadFileStd(obj_path.string(), buffer)) - return {}; - auto encoded = QByteArray::fromStdString(buffer); - - QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB, - QAESEncryption::Padding::ISO); - - auto decoded = - encryption.removePadding(encryption.decode(encoded, hash_key_)); - - return nlohmann::json::parse(decoded.toStdString()); - } catch (...) { - return {}; - } -} - -GpgFrontend::UI::GlobalSettingStation::~GlobalSettingStation() noexcept = - default; diff --git a/src/ui/settings/SettingsAdvanced.cpp b/src/ui/settings/SettingsAdvanced.cpp index 093e34a0..516d4d02 100644 --- a/src/ui/settings/SettingsAdvanced.cpp +++ b/src/ui/settings/SettingsAdvanced.cpp @@ -28,7 +28,7 @@ #include "SettingsAdvanced.h" -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" namespace GpgFrontend::UI { @@ -76,7 +76,7 @@ void AdvancedTab::SetSettings() { void AdvancedTab::ApplySettings() { auto& settings = - GpgFrontend::UI::GlobalSettingStation::GetInstance().GetUISettings(); + GpgFrontend::GlobalSettingStation::GetInstance().GetUISettings(); if (!settings.exists("advanced") || settings.lookup("advanced").getType() != libconfig::Setting::TypeGroup) diff --git a/src/ui/settings/SettingsAppearance.cpp b/src/ui/settings/SettingsAppearance.cpp index c9288ce6..d9fe8c42 100644 --- a/src/ui/settings/SettingsAppearance.cpp +++ b/src/ui/settings/SettingsAppearance.cpp @@ -28,7 +28,7 @@ #include "SettingsAppearance.h" -#include "GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" namespace GpgFrontend::UI { @@ -185,7 +185,7 @@ void AppearanceTab::SetSettings() { *************************************/ void AppearanceTab::ApplySettings() { auto& settings = - GpgFrontend::UI::GlobalSettingStation::GetInstance().GetUISettings(); + GpgFrontend::GlobalSettingStation::GetInstance().GetUISettings(); if (!settings.exists("window") || settings.lookup("window").getType() != libconfig::Setting::TypeGroup) diff --git a/src/ui/settings/SettingsDialog.cpp b/src/ui/settings/SettingsDialog.cpp index c633a06e..2c23858f 100644 --- a/src/ui/settings/SettingsDialog.cpp +++ b/src/ui/settings/SettingsDialog.cpp @@ -28,7 +28,7 @@ #include "SettingsDialog.h" -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" #include "ui/settings/SettingsAdvanced.h" #include "ui/settings/SettingsAppearance.h" #include "ui/settings/SettingsGeneral.h" diff --git a/src/ui/settings/SettingsGeneral.cpp b/src/ui/settings/SettingsGeneral.cpp index 2b5bcfcf..3c7bca32 100644 --- a/src/ui/settings/SettingsGeneral.cpp +++ b/src/ui/settings/SettingsGeneral.cpp @@ -32,7 +32,7 @@ #include "SettingsDialog.h" #endif -#include "GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" #include "ui_GeneralSettings.h" namespace GpgFrontend::UI { @@ -140,7 +140,7 @@ void GeneralTab::SetSettings() { *************************************/ void GeneralTab::ApplySettings() { auto& settings = - GpgFrontend::UI::GlobalSettingStation::GetInstance().GetUISettings(); + GpgFrontend::GlobalSettingStation::GetInstance().GetUISettings(); if (!settings.exists("general") || settings.lookup("general").getType() != libconfig::Setting::TypeGroup) diff --git a/src/ui/settings/SettingsKeyServer.cpp b/src/ui/settings/SettingsKeyServer.cpp index 41e749c1..70edb2c4 100644 --- a/src/ui/settings/SettingsKeyServer.cpp +++ b/src/ui/settings/SettingsKeyServer.cpp @@ -28,7 +28,7 @@ #include "SettingsKeyServer.h" -#include "GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" #include "ui/thread/ListedKeyServerTestThread.h" #include "ui_KeyServerSettings.h" diff --git a/src/ui/settings/SettingsNetwork.cpp b/src/ui/settings/SettingsNetwork.cpp index a3cff2cd..d4edae42 100644 --- a/src/ui/settings/SettingsNetwork.cpp +++ b/src/ui/settings/SettingsNetwork.cpp @@ -28,7 +28,7 @@ #include "SettingsNetwork.h" -#include "ui/settings//GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" #include "ui/thread/ProxyConnectionTestThread.h" #include "ui_NetworkSettings.h" @@ -154,7 +154,7 @@ void GpgFrontend::UI::NetworkTab::ApplySettings() { LOG(INFO) << "called"; auto &settings = - GpgFrontend::UI::GlobalSettingStation::GetInstance().GetUISettings(); + GpgFrontend::GlobalSettingStation::GetInstance().GetUISettings(); if (!settings.exists("proxy") || settings.lookup("proxy").getType() != libconfig::Setting::TypeGroup) diff --git a/src/ui/struct/SettingsObject.cpp b/src/ui/struct/SettingsObject.cpp index 31e71e46..1bd426a6 100644 --- a/src/ui/struct/SettingsObject.cpp +++ b/src/ui/struct/SettingsObject.cpp @@ -50,7 +50,7 @@ GpgFrontend::UI::SettingsObject::SettingsObject(std::string settings_name) : settings_name_(std::move(settings_name)) { try { auto _json_optional = - GlobalSettingStation::GetInstance().GetDataObject(settings_name_); + GpgFrontend::DataObjectOperator::GetInstance().GetDataObject(settings_name_); if (_json_optional.has_value()) { nlohmann::json::operator=(_json_optional.value()); @@ -65,6 +65,6 @@ GpgFrontend::UI::SettingsObject::SettingsObject(nlohmann::json _sub_json, bool) GpgFrontend::UI::SettingsObject::~SettingsObject() { if (!settings_name_.empty()) - GpgFrontend::UI::GlobalSettingStation::GetInstance().SaveDataObj( + GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj( settings_name_, *this); } diff --git a/src/ui/struct/SettingsObject.h b/src/ui/struct/SettingsObject.h index 4fb5ad6b..0bc760c8 100644 --- a/src/ui/struct/SettingsObject.h +++ b/src/ui/struct/SettingsObject.h @@ -31,7 +31,7 @@ #include <utility> -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/DataObjectOperator.h" namespace GpgFrontend::UI { diff --git a/src/ui/widgets/InfoBoardWidget.cpp b/src/ui/widgets/InfoBoardWidget.cpp index bedcdf49..b8a64145 100644 --- a/src/ui/widgets/InfoBoardWidget.cpp +++ b/src/ui/widgets/InfoBoardWidget.cpp @@ -29,7 +29,7 @@ #include "ui/widgets/InfoBoardWidget.h" #include "ui/SignalStation.h" -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" #include "ui_InfoBoard.h" namespace GpgFrontend::UI { diff --git a/src/ui/widgets/KeyList.cpp b/src/ui/widgets/KeyList.cpp index 38d9d359..75558edc 100644 --- a/src/ui/widgets/KeyList.cpp +++ b/src/ui/widgets/KeyList.cpp @@ -35,7 +35,7 @@ #include "core/function/gpg/GpgKeyGetter.h" #include "ui/SignalStation.h" #include "ui/UserInterfaceUtils.h" -#include "ui/settings/GlobalSettingStation.h" +#include "core/function/GlobalSettingStation.h" #include "ui_KeyList.h" namespace GpgFrontend::UI { @@ -53,9 +53,9 @@ KeyList::KeyList(KeyMenuAbility::AbilityType menu_ability, QWidget* parent) void KeyList::init() { #ifdef GPG_STANDALONE_MODE LOG(INFO) << "GPG_STANDALONE_MODE Enabled"; - auto gpg_path = GpgFrontend::UI::GlobalSettingStation::GetInstance() + auto gpg_path = GpgFrontend::GlobalSettingStation::GetInstance() .GetStandaloneGpgBinDir(); - auto db_path = GpgFrontend::UI::GlobalSettingStation::GetInstance() + auto db_path = GpgFrontend::GlobalSettingStation::GetInstance() .GetStandaloneDatabaseDir(); GpgContext::CreateInstance( _m_key_list_id, std::make_unique<GpgContext>(true, db_path.string(), true, |