Use authenticated VoidLibrary secret storage
This commit is contained in:
6 files changed
+209
-63
No files matched your search
+127
-49
@@ -1,13 +1,37 @@
|
||||
#include "storage/SecureStorage.hpp"
|
||||
|
||||
#include <Void/Security/Security.hpp>
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QCryptographicHash>
|
||||
#include <QDataStream>
|
||||
#include <QDateTime>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QRandomGenerator>
|
||||
#include <QSaveFile>
|
||||
#include <QStandardPaths>
|
||||
#include <QtEndian>
|
||||
|
||||
#include <algorithm>
|
||||
#include <span>
|
||||
|
||||
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<const unsigned char*>(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<qsizetype>(Void::Security::secret_key_bytes)) {
|
||||
return {};
|
||||
}
|
||||
cachedKey_ = key;
|
||||
return cachedKey_;
|
||||
}
|
||||
|
||||
auto generated = Void::Security::random_secret_key();
|
||||
if (!generated) {
|
||||
return {};
|
||||
}
|
||||
QByteArray key{reinterpret_cast<const char*>(generated->data()),
|
||||
static_cast<qsizetype>(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<char>(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<qsizetype>(Void::Security::secret_key_bytes)) {
|
||||
return {};
|
||||
}
|
||||
const auto* bytes =
|
||||
reinterpret_cast<const unsigned char*>(data.constData());
|
||||
auto sealed = Void::Security::seal_secret(
|
||||
std::span<const unsigned char>{
|
||||
bytes, static_cast<std::size_t>(data.size())},
|
||||
toSecretKey(encodedKey));
|
||||
if (!sealed) {
|
||||
return {};
|
||||
}
|
||||
QByteArray result = storageMagic;
|
||||
result.append(reinterpret_cast<const char*>(sealed->data()),
|
||||
static_cast<qsizetype>(sealed->size()));
|
||||
return result;
|
||||
}
|
||||
|
||||
QByteArray SecureStorage::decrypt(const QByteArray& data) const
|
||||
{
|
||||
if (data.startsWith(storageMagic)) {
|
||||
const QByteArray encodedKey = loadOrCreateKey();
|
||||
if (encodedKey.size() !=
|
||||
static_cast<qsizetype>(Void::Security::secret_key_bytes)) {
|
||||
return {};
|
||||
}
|
||||
const auto envelope = data.sliced(storageMagic.size());
|
||||
const auto* bytes =
|
||||
reinterpret_cast<const unsigned char*>(envelope.constData());
|
||||
auto opened = Void::Security::open_secret(
|
||||
std::span<const unsigned char>{
|
||||
bytes, static_cast<std::size_t>(envelope.size())},
|
||||
toSecretKey(encodedKey));
|
||||
if (!opened) {
|
||||
return {};
|
||||
}
|
||||
return QByteArray{reinterpret_cast<const char*>(opened->data()),
|
||||
static_cast<qsizetype>(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<quint32>(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
|
||||
|
||||
Reference in new issue
Block a user