From 8ef805debdaf9658017312acc224898b3f695f49 Mon Sep 17 00:00:00 2001 From: faumaray Date: Wed, 29 Jul 2026 22:40:35 +0300 Subject: [PATCH] Use authenticated VoidLibrary secret storage --- .gitea/workflows/release.yml | 56 ++++++++++ CMakeLists.txt | 8 +- component.release.json | 7 ++ src/storage/SecureStorage.cpp | 176 +++++++++++++++++++++++--------- src/storage/SecureStorage.hpp | 17 ++- src/storage/SettingsManager.cpp | 8 +- 6 files changed, 209 insertions(+), 63 deletions(-) create mode 100644 .gitea/workflows/release.yml create mode 100644 component.release.json diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml new file mode 100644 index 0000000..c1b4e11 --- /dev/null +++ b/.gitea/workflows/release.yml @@ -0,0 +1,56 @@ +name: Independent component release +on: + push: + tags: [v*] + schedule: + - cron: '17 2 * * *' + workflow_dispatch: +jobs: + publish: + runs-on: echoion-release-serial + steps: + - name: Check out component + uses: https://github.com/actions/checkout@11d5960a326750d5838078e36cf38b85af677262 + with: + fetch-depth: 0 + ssh-key: ${{ secrets.RELEASE_CHECKOUT_SSH_KEY }} + persist-credentials: false + - name: Check out pinned public release tools + uses: https://github.com/actions/checkout@11d5960a326750d5838078e36cf38b85af677262 + with: + repository: Void/Echoion + ref: ${{ vars.ECHOION_RELEASE_TOOLS_REF }} + path: .echoion-release-tools + fetch-depth: 0 + ssh-key: ${{ secrets.RELEASE_CHECKOUT_SSH_KEY }} + persist-credentials: false + - name: Build, publish, and merge feed transaction + shell: bash + env: + AWS_ACCESS_KEY_ID: ${{ secrets.COMPONENT_OBJECT_STORAGE_ACCESS_KEY }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.COMPONENT_OBJECT_STORAGE_SECRET_KEY }} + AWS_DEFAULT_REGION: ${{ vars.COMPONENT_OBJECT_STORAGE_REGION }} + ECHOION_COMPONENT_SSH_KEY: ${{ secrets.RELEASE_CHECKOUT_SSH_KEY }} + ECHOION_DISTRIBUTION_BASE_URL: ${{ vars.ECHOION_DISTRIBUTION_BASE_URL }} + ECHOION_FEED_BASE_URL: ${{ vars.ECHOION_FEED_BASE_URL }} + ECHOION_FEED_COMMIT_SIGNING_KEY: ${{ secrets.RELEASE_FEED_GIT_SIGNING_KEY }} + ECHOION_FEED_PRIVATE_KEY: ${{ secrets.RELEASE_FEED_ED25519_PRIVATE_KEY }} + ECHOION_FEED_REPOSITORY: ssh://gitea@git.faumaray.ru/Void/Echoion.git + ECHOION_FEED_SIGNING_KEY_ID: ${{ vars.ECHOION_FEED_SIGNING_KEY_ID }} + ECHOION_OBJECT_STORAGE_BUCKET: ${{ vars.COMPONENT_OBJECT_STORAGE_BUCKET }} + ECHOION_OBJECT_STORAGE_ENDPOINT: ${{ vars.COMPONENT_OBJECT_STORAGE_ENDPOINT }} + ECHOION_OBJECT_STORAGE_PREFIX: ${{ vars.COMPONENT_OBJECT_STORAGE_PREFIX }} + ECHOION_RELEASE_TAG_ALLOWED_SIGNERS: ${{ secrets.RELEASE_TAG_ALLOWED_SIGNERS }} + GITEA_API_URL: ${{ gitea.api_url }} + GITEA_EVENT_NAME: ${{ gitea.event_name }} + GITEA_REF_NAME: ${{ gitea.ref_name }} + GITEA_RUN_NUMBER: ${{ gitea.run_number }} + GITEA_SHA: ${{ gitea.sha }} + GITEA_REPOSITORY: ${{ gitea.repository }} + GITEA_RELEASE_TOKEN: ${{ secrets.RELEASE_BOT_TOKEN }} + RELEASE_TOOLS_REF: ${{ vars.ECHOION_RELEASE_TOOLS_REF }} + run: | + set -euo pipefail + [[ "$RELEASE_TOOLS_REF" =~ ^[0-9a-f]{40}$ ]] + test "$(git -C .echoion-release-tools rev-parse HEAD)" = "$RELEASE_TOOLS_REF" + .echoion-release-tools/scripts/release-component.sh component.release.json diff --git a/CMakeLists.txt b/CMakeLists.txt index 49d8a97..7860f6f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,11 @@ cmake_minimum_required(VERSION 4.0.0) if(NOT DEFINED ECHOION_PRODUCT_VERSION) - set(ECHOION_PRODUCT_VERSION 2.0.0) + set(ECHOION_PRODUCT_VERSION 0.0.0) endif() -project(EchoionDesktop VERSION "${ECHOION_PRODUCT_VERSION}" LANGUAGES CXX) +if(NOT DEFINED ECHOION_COMPONENT_VERSION) + set(ECHOION_COMPONENT_VERSION "${ECHOION_PRODUCT_VERSION}") +endif() +project(EchoionDesktop VERSION "${ECHOION_COMPONENT_VERSION}" LANGUAGES CXX) if(PROJECT_IS_TOP_LEVEL) file(DOWNLOAD @@ -50,6 +53,7 @@ target_include_directories(EchoionDesktop PRIVATE target_link_libraries(EchoionDesktop PRIVATE Echoion::Api + Void::Security Qt6::Core Qt6::Widgets Qt6::Network diff --git a/component.release.json b/component.release.json new file mode 100644 index 0000000..e9ca156 --- /dev/null +++ b/component.release.json @@ -0,0 +1,7 @@ +{ + "schemaVersion": 1, + "componentId": "EchoionDesktop", + "productCompatibility": ">=2.0.0 <3.0.0", + "publishNightly": true, + "cmakeOptions": {} +} diff --git a/src/storage/SecureStorage.cpp b/src/storage/SecureStorage.cpp index 191309f..a0c91a8 100644 --- a/src/storage/SecureStorage.cpp +++ b/src/storage/SecureStorage.cpp @@ -1,13 +1,37 @@ #include "storage/SecureStorage.hpp" + +#include + #include +#include +#include #include #include +#include #include +#include +#include #include -#include + +#include +#include namespace Echoion::Desktop { + namespace + { + const QByteArray storageMagic{"ECV2", 4}; + + [[nodiscard]] Void::Security::SecretKey + toSecretKey(const QByteArray& value) + { + Void::Security::SecretKey key{}; + std::copy_n( + reinterpret_cast(value.constData()), + key.size(), key.begin()); + return key; + } + } // namespace SecureStorage::SecureStorage() = default; @@ -34,14 +58,53 @@ namespace Echoion::Desktop return getStorageDir() + "/tokens.dat"; } - QByteArray SecureStorage::generateKey() const + QByteArray SecureStorage::loadOrCreateKey() const { if (!cachedKey_.isEmpty()) { return cachedKey_; } - // Generate a deterministic key from machine-specific data - // This makes it harder to copy tokens to another machine + const QString keyPath = getStorageDir() + "/secrets.key"; + const QFileInfo keyInfo{keyPath}; + if (keyInfo.isSymLink()) { + return {}; + } + QFile keyFile{keyPath}; + if (keyFile.exists()) { + if (!keyFile.open(QIODevice::ReadOnly)) { + return {}; + } + QByteArray key = keyFile.readAll(); + if (key.size() != + static_cast(Void::Security::secret_key_bytes)) { + return {}; + } + cachedKey_ = key; + return cachedKey_; + } + + auto generated = Void::Security::random_secret_key(); + if (!generated) { + return {}; + } + QByteArray key{reinterpret_cast(generated->data()), + static_cast(generated->size())}; + QSaveFile output{keyPath}; + if (!output.open(QIODevice::WriteOnly)) { + return {}; + } + if (!output.setPermissions(QFileDevice::ReadOwner | + QFileDevice::WriteOwner) || + output.write(key) != key.size() || !output.commit()) { + output.cancelWriting(); + return {}; + } + cachedKey_ = key; + return cachedKey_; + } + + QByteArray SecureStorage::legacyKey() const + { QString seed = QCoreApplication::applicationDirPath() + QCoreApplication::applicationName() + @@ -56,17 +119,19 @@ namespace Echoion::Desktop hash[i] = hash[i] ^ static_cast(rng.generate() & 0xFF); } - cachedKey_ = hash; return hash; } - QByteArray SecureStorage::xorEncrypt(const QByteArray& data, - const QByteArray& key) const + QByteArray SecureStorage::legacyXor(const QByteArray& data, + const QByteArray& key) { + if (key.isEmpty()) { + return {}; + } QByteArray result = data; - int keyLen = key.length(); + const qsizetype keyLen = key.size(); - for (int i = 0; i < result.length(); ++i) { + for (qsizetype i = 0; i < result.size(); ++i) { result[i] = result[i] ^ key[i % keyLen]; } @@ -75,39 +140,63 @@ namespace Echoion::Desktop QByteArray SecureStorage::encrypt(const QByteArray& data) const { - QByteArray key = generateKey(); - - // Add simple header for validation - QByteArray header; - QDataStream headerStream(&header, QIODevice::WriteOnly); - headerStream << quint32(0x4543484F); // "ECHO" magic - headerStream << quint32(data.length()); - - QByteArray encrypted = xorEncrypt(data, key); - - return header + encrypted; + const QByteArray encodedKey = loadOrCreateKey(); + if (encodedKey.size() != + static_cast(Void::Security::secret_key_bytes)) { + return {}; + } + const auto* bytes = + reinterpret_cast(data.constData()); + auto sealed = Void::Security::seal_secret( + std::span{ + bytes, static_cast(data.size())}, + toSecretKey(encodedKey)); + if (!sealed) { + return {}; + } + QByteArray result = storageMagic; + result.append(reinterpret_cast(sealed->data()), + static_cast(sealed->size())); + return result; } QByteArray SecureStorage::decrypt(const QByteArray& data) const { + if (data.startsWith(storageMagic)) { + const QByteArray encodedKey = loadOrCreateKey(); + if (encodedKey.size() != + static_cast(Void::Security::secret_key_bytes)) { + return {}; + } + const auto envelope = data.sliced(storageMagic.size()); + const auto* bytes = + reinterpret_cast(envelope.constData()); + auto opened = Void::Security::open_secret( + std::span{ + bytes, static_cast(envelope.size())}, + toSecretKey(encodedKey)); + if (!opened) { + return {}; + } + return QByteArray{reinterpret_cast(opened->data()), + static_cast(opened->size())}; + } + + // Accept the v1 format only to migrate existing installations. if (data.length() < 8) { - return QByteArray(); // Too short + return {}; } - // Verify header QDataStream headerStream(data); - quint32 magic; - quint32 dataLen; + quint32 magic = 0; + quint32 dataLen = 0; headerStream >> magic >> dataLen; - - if (magic != 0x4543484F) { - return QByteArray(); // Invalid magic + if (magic != 0x4543484F || + dataLen != static_cast(data.size() - 8)) { + return {}; } - QByteArray key = generateKey(); - QByteArray encrypted = data.mid(8); // Skip header - - return xorEncrypt(encrypted, key); + return legacyXor(data.sliced(8), legacyKey()); } bool SecureStorage::saveToFile(const QString& filepath, @@ -118,29 +207,18 @@ namespace Echoion::Desktop createBackup(filepath); } - // Write to temp file first - QString tempPath = filepath + ".tmp"; - QFile tempFile(tempPath); - - if (!tempFile.open(QIODevice::WriteOnly)) { + QSaveFile output{filepath}; + if (!output.open(QIODevice::WriteOnly)) { return false; } - if (tempFile.write(data) != data.length()) { - tempFile.close(); - QFile::remove(tempPath); + if (!output.setPermissions(QFileDevice::ReadOwner | + QFileDevice::WriteOwner) || + output.write(data) != data.size()) { + output.cancelWriting(); return false; } - - tempFile.close(); - - // Atomic rename - if (!QFile::rename(tempPath, filepath)) { - QFile::remove(tempPath); - return false; - } - - return true; + return output.commit(); } QByteArray SecureStorage::loadFromFile(const QString& filepath) const diff --git a/src/storage/SecureStorage.hpp b/src/storage/SecureStorage.hpp index aca1a9f..2eba65f 100644 --- a/src/storage/SecureStorage.hpp +++ b/src/storage/SecureStorage.hpp @@ -1,9 +1,6 @@ #pragma once -#include -#include -#include -#include +#include #include namespace Echoion::Desktop @@ -15,7 +12,7 @@ namespace Echoion::Desktop SecureStorage(); - // Encrypt/decrypt data using Qt crypto + // Encrypt/decrypt data with authenticated VoidLibrary primitives. [[nodiscard]] QByteArray encrypt(const QByteArray& data) const; [[nodiscard]] QByteArray decrypt(const QByteArray& data) const; @@ -40,12 +37,12 @@ namespace Echoion::Desktop private: - // Generate encryption key from machine-specific data - [[nodiscard]] QByteArray generateKey() const; + [[nodiscard]] QByteArray loadOrCreateKey() const; - // Simple XOR encryption with SHA-256 derived key - [[nodiscard]] QByteArray xorEncrypt(const QByteArray& data, - const QByteArray& key) const; + // Read-only migration support for the v1 deterministic XOR format. + [[nodiscard]] QByteArray legacyKey() const; + [[nodiscard]] static QByteArray legacyXor(const QByteArray& data, + const QByteArray& key); mutable QByteArray cachedKey_; }; diff --git a/src/storage/SettingsManager.cpp b/src/storage/SettingsManager.cpp index 40c2276..b727dd5 100644 --- a/src/storage/SettingsManager.cpp +++ b/src/storage/SettingsManager.cpp @@ -1,6 +1,9 @@ #include "storage/SecureStorage.hpp" #include "storage/SettingsManager.hpp" + #include +#include +#include namespace Echoion::Desktop { @@ -245,8 +248,9 @@ namespace Echoion::Desktop emit tokenSaved(host, port); } - std::optional SettingsManager::loadToken( - const QString& host, int port, QString& outNickname) const + std::optional + SettingsManager::loadToken(const QString& host, int port, + QString& outNickname) const { for (const auto& ct : tokens_) { if (ct.host == host && ct.port == port) {