update: changed logic, added to settingsController

This commit is contained in:
MrMirDan
2025-12-09 13:50:55 +02:00
parent 0b91219c51
commit a8f084f8ef
10 changed files with 329 additions and 159 deletions
@@ -192,6 +192,36 @@ void SettingsController::clearSettings()
toggleAutoStart(false);
}
bool SettingsController::isFileEncryptionEnabled()
{
return m_appSettingsRepository->isFileEncryption();
}
void SettingsController::toggleFileEncryption(bool enable)
{
m_appSettingsRepository->setFileEncryption(enable);
}
void SettingsController::setPassword(QString pwd)
{
m_appSettingsRepository->setPassword(pwd);
}
QString SettingsController::getPassword()
{
return m_appSettingsRepository->getPassword();
}
void SettingsController::setHint(QString hint)
{
m_appSettingsRepository->setHint(hint);
}
QString SettingsController::getHint()
{
return m_appSettingsRepository->getHint();
}
bool SettingsController::isAutoConnectEnabled() const
{
return m_appSettingsRepository->isAutoConnect();
@@ -44,6 +44,15 @@ public:
void clearSettings();
bool isFileEncryptionEnabled();
void toggleFileEncryption(bool enable);
void setPassword(QString pwd);
QString getPassword();
void setHint(QString hint);
QString getHint();
bool isAutoConnectEnabled() const;
void toggleAutoConnect(bool enable);
@@ -300,6 +300,33 @@ void SecureAppSettingsRepository::setStrictKillSwitchEnabled(bool enabled)
setValue("Conf/strictKillSwitchEnabled", enabled);
}
bool SecureAppSettingsRepository::isFileEncryption() const
{
return value("Sec/fileEncryption", false).toBool();
}
void SecureAppSettingsRepository::setFileEncryption(bool enabled)
{
setValue("Sec/fileEncryption", enabled);
}
QString SecureAppSettingsRepository::getPassword() const
{
return m_settings->getPassword();
}
void SecureAppSettingsRepository::setPassword(const QString &pwd)
{
m_settings->setPassword(pwd);
}
QString SecureAppSettingsRepository::getHint() const
{
return m_settings->getHint();
}
void SecureAppSettingsRepository::setHint(const QString &hint)
{
m_settings->setHint(hint);
}
bool SecureAppSettingsRepository::isAutoConnect() const
{
return value("Conf/autoConnect", false).toBool();
@@ -64,6 +64,13 @@ public:
void setKillSwitchEnabled(bool enabled);
bool isStrictKillSwitchEnabled() const;
void setStrictKillSwitchEnabled(bool enabled);
bool isFileEncryption() const;
void setFileEncryption(bool enabled);
QString getPassword() const;
void setPassword(const QString &pwd);
QString getHint() const;
void setHint(const QString &hint);
bool isAutoConnect() const;
void setAutoConnect(bool enabled);
-137
View File
@@ -1,137 +0,0 @@
#include "protection.h"
#include <QFile>
#include <QCryptographicHash>
#include <QDebug>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <stdexcept>
static constexpr int KEY_SIZE = 32;
static constexpr int SALT_SIZE = 16;
static constexpr int IV_SIZE = 16;
static constexpr int ITERATIONS = 100000;
Protector::Protector(QObject *parent) : QObject(parent)
{
}
QByteArray Protector::generateSalt()
{
QByteArray salt(SALT_SIZE, 0);
RAND_bytes(reinterpret_cast<unsigned char *>(salt.data()), SALT_SIZE);
return salt;
}
QByteArray Protector::deriveKey(const QString &password, const QByteArray &salt)
{
QByteArray key(KEY_SIZE, 0);
if (!PKCS5_PBKDF2_HMAC(password.toUtf8().constData(), password.size(),
reinterpret_cast<const unsigned char *>(salt.constData()), salt.size(), ITERATIONS,
EVP_sha256(), KEY_SIZE, reinterpret_cast<unsigned char *>(key.data()))) {
throw std::runtime_error("PBKDF2 key derivation failed");
}
return key;
}
void Protector::encryptFile(const QString &filePath, const QString &password)
{
try {
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly))
throw std::runtime_error("Cannot open file for reading");
QByteArray plain = file.readAll();
file.close();
QByteArray salt = generateSalt();
QByteArray iv(IV_SIZE, 0);
RAND_bytes(reinterpret_cast<unsigned char *>(iv.data()), IV_SIZE);
QByteArray key = deriveKey(password, salt);
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
QByteArray encrypted(plain.size() + EVP_MAX_BLOCK_LENGTH, 0);
int len = 0, total = 0;
if (!EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, reinterpret_cast<const unsigned char *>(key.constData()),
reinterpret_cast<const unsigned char *>(iv.constData())))
throw std::runtime_error("EncryptInit failed");
if (!EVP_EncryptUpdate(ctx, reinterpret_cast<unsigned char *>(encrypted.data()), &len,
reinterpret_cast<const unsigned char *>(plain.constData()), plain.size()))
throw std::runtime_error("EncryptUpdate failed");
total = len;
if (!EVP_EncryptFinal_ex(ctx, reinterpret_cast<unsigned char *>(encrypted.data()) + len, &len))
throw std::runtime_error("EncryptFinal failed");
total += len;
EVP_CIPHER_CTX_free(ctx);
encrypted.truncate(total);
QByteArray finalData = salt + iv + encrypted;
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
throw std::runtime_error("Cannot open file for writing");
file.write(finalData);
file.close();
qInfo() << "File encrypted successfully:" << filePath;
} catch (const std::exception &e) {
qWarning() << "Encryption failed:" << e.what();
}
}
void Protector::decryptFile(const QString &filePath, const QString &password)
{
try {
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly))
throw std::runtime_error("Cannot open file for reading");
QByteArray data = file.readAll();
file.close();
if (data.size() < SALT_SIZE + IV_SIZE)
throw std::runtime_error("Corrupted or invalid encrypted file");
QByteArray salt = data.left(SALT_SIZE);
QByteArray iv = data.mid(SALT_SIZE, IV_SIZE);
QByteArray cipher = data.mid(SALT_SIZE + IV_SIZE);
QByteArray key = deriveKey(password, salt);
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
QByteArray plain(cipher.size(), 0);
int len = 0, total = 0;
if (!EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, reinterpret_cast<const unsigned char *>(key.constData()),
reinterpret_cast<const unsigned char *>(iv.constData())))
throw std::runtime_error("DecryptInit failed");
if (!EVP_DecryptUpdate(ctx, reinterpret_cast<unsigned char *>(plain.data()), &len,
reinterpret_cast<const unsigned char *>(cipher.constData()), cipher.size()))
throw std::runtime_error("DecryptUpdate failed");
total = len;
if (!EVP_DecryptFinal_ex(ctx, reinterpret_cast<unsigned char *>(plain.data()) + len, &len)) {
EVP_CIPHER_CTX_free(ctx);
throw std::runtime_error("Incorrect password or corrupted file");
}
total += len;
EVP_CIPHER_CTX_free(ctx);
plain.truncate(total);
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
throw std::runtime_error("Cannot open file for writing");
file.write(plain);
file.close();
qInfo() << "File decrypted successfully:" << filePath;
} catch (const std::exception &e) {
qWarning() << "Decryption failed:" << e.what();
}
}
-22
View File
@@ -1,22 +0,0 @@
#ifndef PROTECTOR_H
#define PROTECTOR_H
#include <QByteArray>
#include <QObject>
#include <QString>
class Protector : public QObject
{
Q_OBJECT
public:
explicit Protector(QObject *parent = nullptr);
Q_INVOKABLE static void encryptFile(const QString &filePath, const QString &password);
Q_INVOKABLE static void decryptFile(const QString &filePath, const QString &password);
private:
static QByteArray deriveKey(const QString &password, const QByteArray &salt);
static QByteArray generateSalt();
};
#endif // PROTECTOR_H
+206
View File
@@ -12,6 +12,11 @@
#include <QRandomGenerator>
#include <QSharedPointer>
#include <QTimer>
#include <QFile>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <openssl/err.h>
using namespace QKeychain;
@@ -19,6 +24,11 @@ namespace {
constexpr const char *settingsKeyTag = "settingsKeyTag";
constexpr const char *settingsIvTag = "settingsIvTag";
constexpr const char *keyChainName = "AmneziaVPN-Keychain";
constexpr int SALT_LEN = 16;
constexpr int IV_LEN = 16;
constexpr int KEY_LEN = 32;
constexpr int PBKDF2_ITER = 100000;
}
SecureQSettings::SecureQSettings(const QString &organization, const QString &application, QObject *parent, bool enableEncryption)
@@ -305,3 +315,199 @@ void SecureQSettings::setSecTag(const QString &tag, const QByteArray &data)
qCritical() << "SecureQSettings::setSecTag Error:" << job->errorString();
}
}
void SecureQSettings::setPassword(const QString &pwd)
{
m_password = pwd;
}
void SecureQSettings::setHint(const QString &hint)
{
m_hint = hint;
}
QString SecureQSettings::getPassword() const
{
return m_password;
}
QString SecureQSettings::getHint() const
{
return m_hint;
}
static QString opensslErrString()
{
unsigned long e = ERR_get_error();
if (!e)
return QStringLiteral("Unknown OpenSSL error");
char buf[256];
ERR_error_string_n(e, buf, sizeof(buf));
return QString::fromUtf8(buf);
}
static bool deriveKey(const QByteArray &password, const QByteArray &salt, QByteArray &outKey, QString *err)
{
outKey.resize(KEY_LEN);
const unsigned char *pw = reinterpret_cast<const unsigned char *>(password.constData());
const unsigned char *s = reinterpret_cast<const unsigned char *>(salt.constData());
int ok = PKCS5_PBKDF2_HMAC(reinterpret_cast<const char *>(pw), password.size(), s, salt.size(), PBKDF2_ITER,
EVP_sha256(), KEY_LEN, reinterpret_cast<unsigned char *>(outKey.data()));
if (!ok) {
if (err)
*err = opensslErrString();
}
return ok == 1;
}
static bool aesCrypt(const QByteArray &in, const QByteArray &key, const QByteArray &iv, QByteArray &out, bool encrypt,
QString *err)
{
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (!ctx) {
if (err)
*err = "EVP_CIPHER_CTX_new failed";
return false;
}
const EVP_CIPHER *cipher = EVP_aes_256_cbc();
if (1
!= EVP_CipherInit_ex(ctx, cipher, nullptr, reinterpret_cast<const unsigned char *>(key.constData()),
reinterpret_cast<const unsigned char *>(iv.constData()), encrypt ? 1 : 0)) {
if (err)
*err = opensslErrString();
EVP_CIPHER_CTX_free(ctx);
return false;
}
out.clear();
out.resize(in.size() + EVP_CIPHER_block_size(cipher));
int outlen1 = 0;
if (1
!= EVP_CipherUpdate(ctx, reinterpret_cast<unsigned char *>(out.data()), &outlen1,
reinterpret_cast<const unsigned char *>(in.constData()), in.size())) {
if (err)
*err = opensslErrString();
EVP_CIPHER_CTX_free(ctx);
return false;
}
int outlen2 = 0;
if (1 != EVP_CipherFinal_ex(ctx, reinterpret_cast<unsigned char *>(out.data()) + outlen1, &outlen2)) {
if (err)
*err = opensslErrString();
EVP_CIPHER_CTX_free(ctx);
return false;
}
out.resize(outlen1 + outlen2);
EVP_CIPHER_CTX_free(ctx);
return true;
}
bool SecureQSettings::encryptFile(const QString &filePath, const QString &password, QString *error) const
{
QFile f(filePath);
if (!f.open(QIODevice::ReadOnly)) {
if (error)
*error = QStringLiteral("Cannot open file for read: %1").arg(f.errorString());
return false;
}
QByteArray plain = f.readAll();
f.close();
if (plain.startsWith(magicString)) {
if (error)
*error = QStringLiteral("File already encrypted (magic found)");
return false;
}
QByteArray salt(SALT_LEN, 0);
QByteArray iv(IV_LEN, 0);
QByteArray key;
QByteArray cipher;
QByteArray out;
if (1 != RAND_bytes(reinterpret_cast<unsigned char *>(salt.data()), SALT_LEN)
|| 1 != RAND_bytes(reinterpret_cast<unsigned char *>(iv.data()), IV_LEN)) {
if (error)
*error = opensslErrString();
return false;
}
if (!deriveKey(password.toUtf8(), salt, key, error))
return false;
if (!aesCrypt(plain, key, iv, cipher, true, error))
return false;
out.reserve(magicString.size() + SALT_LEN + IV_LEN + cipher.size());
out += magicString;
out += salt;
out += iv;
out += cipher;
if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
if (error)
*error = QStringLiteral("Cannot open file for write: %1").arg(f.errorString());
return false;
}
qint64 written = f.write(out);
f.close();
if (written != out.size()) {
if (error)
*error = QStringLiteral("Write failed or incomplete");
return false;
}
return true;
}
bool SecureQSettings::decryptFile(const QString &filePath, const QString &password, QString *error) const
{
QFile f(filePath);
if (!f.open(QIODevice::ReadOnly)) {
if (error)
*error = QStringLiteral("Cannot open file for read: %1").arg(f.errorString());
return false;
}
QByteArray blob = f.readAll();
f.close();
if (!blob.startsWith(magicString)) {
if (error)
*error = QStringLiteral("File is not recognized as encrypted (magic missing)");
return false;
}
int pos = magicString.size();
if (blob.size() < pos + SALT_LEN + IV_LEN) {
if (error)
*error = QStringLiteral("Encrypted file too small / corrupted");
return false;
}
QByteArray salt = blob.mid(pos, SALT_LEN);
pos += SALT_LEN;
QByteArray iv = blob.mid(pos, IV_LEN);
pos += IV_LEN;
QByteArray cipher = blob.mid(pos);
QByteArray key;
QByteArray plain;
if (!deriveKey(password.toUtf8(), salt, key, error))
return false;
if (!aesCrypt(cipher, key, iv, plain, false, error))
return false;
if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
if (error)
*error = QStringLiteral("Cannot open file for write: %1").arg(f.errorString());
return false;
}
qint64 written = f.write(plain);
f.close();
if (written != plain.size()) {
if (error)
*error = QStringLiteral("Write failed or incomplete");
return false;
}
return true;
}
+12
View File
@@ -26,6 +26,15 @@ public:
void clearSettings();
void setPassword(const QString &pwd);
void setHint(const QString &hint);
QString getPassword() const;
QString getHint() const;
bool encryptFile(const QString &filePath, const QString &password, QString *error = nullptr) const;
bool decryptFile(const QString &filePath, const QString &password, QString *error = nullptr) const;
private:
QByteArray encryptText(const QByteArray &value) const;
QByteArray decryptText(const QByteArray &ba) const;
@@ -48,6 +57,9 @@ private:
"Conf/", "Servers/",
};
mutable QString m_password;
mutable QString m_hint;
mutable QByteArray m_key;
mutable QByteArray m_iv;
@@ -186,6 +186,36 @@ void SettingsUiController::clearSettings()
#endif
}
bool SettingsUiController::isFileEncryptionEnabled()
{
return m_settingsController->isFileEncryptionEnabled();
}
void SettingsUiController::toggleFileEncryption(bool enable)
{
m_settingsController->toggleFileEncryption(enable);
}
void SettingsUiController::setPassword(QString pwd)
{
m_settingsController->setPassword(pwd);
}
QString SettingsUiController::getPassword()
{
return m_settingsController->getPassword();
}
void SettingsUiController::setHint(QString hint)
{
m_settingsController->setHint(hint);
}
QString SettingsUiController::getHint()
{
return m_settingsController->getHint();
}
bool SettingsUiController::isAutoConnectEnabled()
{
return m_settingsController->isAutoConnectEnabled();
@@ -61,6 +61,14 @@ public slots:
void clearSettings();
bool isFileEncryptionEnabled();
void toggleFileEncryption(bool enable);
void setPassword(QString pwd);
QString getPassword();
void setHint(QString hint);
QString getHint();
bool isAutoConnectEnabled();
void toggleAutoConnect(bool enable);