diff options
Diffstat (limited to '')
-rw-r--r-- | src/ui/settings/GlobalSettingStation.cpp | 91 | ||||
-rw-r--r-- | src/ui/settings/GlobalSettingStation.h | 18 |
2 files changed, 109 insertions, 0 deletions
diff --git a/src/ui/settings/GlobalSettingStation.cpp b/src/ui/settings/GlobalSettingStation.cpp index 4e9bac73..259fe96f 100644 --- a/src/ui/settings/GlobalSettingStation.cpp +++ b/src/ui/settings/GlobalSettingStation.cpp @@ -30,6 +30,8 @@ #include <vmime/security/cert/openssl/X509Certificate_OpenSSL.hpp> #include <vmime/vmime.hpp> +#include "ui/aes/qaesencryption.h" + std::unique_ptr<GpgFrontend::UI::GlobalSettingStation> GpgFrontend::UI::GlobalSettingStation::_instance = nullptr; @@ -74,6 +76,19 @@ GpgFrontend::UI::GlobalSettingStation::GlobalSettingStation() noexcept { 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(); + } + + const auto key = + GpgFrontend::read_all_data_in_file(app_secure_key_path.string()); + 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()); @@ -136,5 +151,81 @@ 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)); + boost::filesystem::permissions( + app_secure_key_path, + boost::filesystem::owner_read | boost::filesystem::owner_write); +} + +void GpgFrontend::UI::GlobalSettingStation::SaveDataObj( + const std::string& _key, const nlohmann::json& value) { + 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; + + LOG(INFO) << "save obj" << obj_path; + + 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()); +} + +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 (!boost::filesystem::exists(obj_path)) { + return {}; + } + + auto buffer = GpgFrontend::read_all_data_in_file(obj_path.string()); + 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 {}; + } + return {}; +} + GpgFrontend::UI::GlobalSettingStation::~GlobalSettingStation() noexcept = default; diff --git a/src/ui/settings/GlobalSettingStation.h b/src/ui/settings/GlobalSettingStation.h index 11c5e5f3..3080726d 100644 --- a/src/ui/settings/GlobalSettingStation.h +++ b/src/ui/settings/GlobalSettingStation.h @@ -29,6 +29,7 @@ #include <boost/filesystem/operations.hpp> #include <boost/filesystem/path.hpp> +#include <json/json.hpp> #include "GpgFrontendBuildInstallInfo.h" #include "ui/GpgFrontendUI.h" @@ -105,6 +106,9 @@ class GlobalSettingStation : public QObject { // Program Data Location boost::filesystem::path app_log_path = app_data_path / "logs"; + // object storage path + boost::filesystem::path app_data_objs_path = app_data_path / "objs"; + #ifdef LINUX_INSTALL_BUILD // Program Data Location boost::filesystem::path app_resource_path = @@ -127,6 +131,10 @@ class GlobalSettingStation : public QObject { QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) .toStdString(); + boost::filesystem::path app_secure_path = app_configure_path / "secure"; + + boost::filesystem::path app_secure_key_path = app_secure_path / "app.key"; + // Configure File Directory Location boost::filesystem::path ui_config_dir_path = app_configure_path / "UserInterface"; @@ -138,7 +146,17 @@ class GlobalSettingStation : public QObject { std::vector<std::shared_ptr<X509>> root_certs_; + std::random_device rd; + + std::mt19937 mt; + + QByteArray hash_key_; + static std::unique_ptr<GlobalSettingStation> _instance; + + void init_app_secure_key(); + + std::string generate_passphrase(int len); }; } // namespace GpgFrontend::UI |