Files
amnezia-client/client/secure_qsettings.cpp
T

224 lines
6.0 KiB
C++
Raw Normal View History

2022-08-05 14:31:12 +03:00
#include "secure_qsettings.h"
2022-08-16 08:28:41 -07:00
#include "platforms/ios/MobileUtils.h"
2022-08-05 14:31:12 +03:00
#include <QDataStream>
2022-08-05 18:59:47 +03:00
#include <QDebug>
2022-08-23 22:47:23 +03:00
#include "utils.h"
#include <QRandomGenerator>
#include "QAead.h"
#include "QBlockCipher.h"
2022-08-05 14:31:12 +03:00
SecureQSettings::SecureQSettings(const QString &organization, const QString &application, QObject *parent)
: QObject{parent},
2022-08-23 22:47:23 +03:00
m_settings(organization, application, parent),
2022-08-05 18:59:47 +03:00
encryptedKeys({"Servers/serversList"})
2022-08-05 14:31:12 +03:00
{
2022-08-23 22:47:23 +03:00
qDebug() << "SecureQSettings::SecureQSettings CTOR";
2022-08-06 19:47:29 +03:00
2022-08-23 22:47:23 +03:00
bool encrypted = m_settings.value("Conf/encrypted").toBool();
2022-08-05 14:31:12 +03:00
2022-08-23 22:47:23 +03:00
// convert settings to encrypted for if updated to >= 2.1.0
2022-08-06 19:47:29 +03:00
if (encryptionRequired() && ! encrypted) {
2022-08-23 22:47:23 +03:00
for (const QString &key : m_settings.allKeys()) {
2022-08-05 18:59:47 +03:00
if (encryptedKeys.contains(key)) {
const QVariant &val = value(key);
setValue(key, val);
}
}
2022-08-23 22:47:23 +03:00
m_settings.setValue("Conf/encrypted", true);
m_settings.sync();
2022-08-05 14:31:12 +03:00
}
}
QVariant SecureQSettings::value(const QString &key, const QVariant &defaultValue) const
{
2022-08-05 18:59:47 +03:00
if (m_cache.contains(key)) {
return m_cache.value(key);
}
2022-08-23 22:47:23 +03:00
if (!m_settings.contains(key)) return defaultValue;
2022-08-05 18:59:47 +03:00
QVariant retVal;
2022-08-23 22:47:23 +03:00
// check if value is not encrypted, v. < 2.0.x
retVal = m_settings.value(key);
if (retVal.isValid()) {
if (retVal.userType() == QVariant::ByteArray &&
retVal.toByteArray().mid(0, magicString.size()) == magicString) {
2022-08-24 07:38:13 -07:00
if (getEncKey().isEmpty() || getEncIv().isEmpty()) {
2022-08-23 22:47:23 +03:00
qCritical() << "SecureQSettings::setValue Decryption requested, but key is empty";
return {};
}
QByteArray encryptedValue = retVal.toByteArray().mid(magicString.size());
QByteArray decryptedValue = decryptText(encryptedValue);
QDataStream ds(&decryptedValue, QIODevice::ReadOnly);
2022-08-05 14:31:12 +03:00
2022-08-23 22:47:23 +03:00
ds >> retVal;
if (!retVal.isValid()) {
qWarning() << "SecureQSettings::value settings decryption failed";
2022-08-24 07:38:13 -07:00
retVal = QVariant();
2022-08-23 22:47:23 +03:00
}
}
2022-08-05 14:31:12 +03:00
}
else {
2022-08-23 22:47:23 +03:00
qWarning() << "SecureQSettings::value invalid QVariant value";
retVal = QVariant();
2022-08-05 14:31:12 +03:00
}
2022-08-05 18:59:47 +03:00
m_cache.insert(key, retVal);
return retVal;
2022-08-05 14:31:12 +03:00
}
void SecureQSettings::setValue(const QString &key, const QVariant &value)
{
2022-08-06 19:47:29 +03:00
if (encryptionRequired() && encryptedKeys.contains(key)) {
2022-08-24 07:38:13 -07:00
if (!getEncKey().isEmpty() && !getEncIv().isEmpty()) {
2022-08-23 22:47:23 +03:00
QByteArray decryptedValue;
{
QDataStream ds(&decryptedValue, QIODevice::WriteOnly);
ds << value;
}
QByteArray encryptedValue = encryptText(decryptedValue);
m_settings.setValue(key, magicString + encryptedValue);
}
else {
qCritical() << "SecureQSettings::setValue Encryption required, but key is empty";
return;
2022-08-06 19:47:29 +03:00
}
}
else {
2022-08-23 22:47:23 +03:00
m_settings.setValue(key, value);
2022-08-05 14:31:12 +03:00
}
2022-08-05 18:59:47 +03:00
m_cache.insert(key, value);
sync();
}
void SecureQSettings::remove(const QString &key)
{
2022-08-23 22:47:23 +03:00
m_settings.remove(key);
2022-08-05 18:59:47 +03:00
m_cache.remove(key);
sync();
}
void SecureQSettings::sync()
{
2022-08-23 22:47:23 +03:00
m_settings.sync();
2022-08-05 18:59:47 +03:00
}
QByteArray SecureQSettings::backupAppConfig() const
{
QMap<QString, QVariant> cfg;
2022-08-23 22:47:23 +03:00
for (const QString &key : m_settings.allKeys()) {
2022-08-05 18:59:47 +03:00
cfg.insert(key, value(key));
}
QByteArray ba;
{
QDataStream ds(&ba, QIODevice::WriteOnly);
ds << cfg;
}
return ba.toBase64();
}
void SecureQSettings::restoreAppConfig(const QByteArray &base64Cfg)
{
QByteArray ba = QByteArray::fromBase64(base64Cfg);
QMap<QString, QVariant> cfg;
{
QDataStream ds(&ba, QIODevice::ReadOnly);
ds >> cfg;
}
for (const QString &key : cfg.keys()) {
setValue(key, cfg.value(key));
}
sync();
2022-08-05 14:31:12 +03:00
}
2022-08-23 22:47:23 +03:00
QByteArray SecureQSettings::encryptText(const QByteArray& value) const
{
QSimpleCrypto::QBlockCipher cipher;
2022-08-24 07:38:13 -07:00
return cipher.encryptAesBlockCipher(value, getEncKey(), getEncIv());
2022-08-06 19:47:29 +03:00
}
2022-08-23 22:47:23 +03:00
QByteArray SecureQSettings::decryptText(const QByteArray& ba) const
{
QSimpleCrypto::QBlockCipher cipher;
2022-08-24 07:38:13 -07:00
return cipher.decryptAesBlockCipher(ba, getEncKey(), getEncIv());
2022-08-06 19:47:29 +03:00
}
bool SecureQSettings::encryptionRequired() const
{
2022-08-24 07:38:13 -07:00
#if defined Q_OS_IOS // || defined Q_OS_ANDROID
2022-08-06 19:47:29 +03:00
return true;
#endif
return false;
}
2022-08-24 07:38:13 -07:00
QByteArray SecureQSettings::getEncKey() const
{
// load keys from system key storage
m_key = MobileUtils::readFromKeychain(settingsKeyTag);
if (m_key.isEmpty()) {
// Create new key
QSimpleCrypto::QBlockCipher cipher;
QByteArray key = cipher.generateSecureRandomBytes(32);
if (key.isEmpty()) {
qCritical() << "SecureQSettings::getEncKey Unable to generate new enc key";
}
MobileUtils::writeToKeychain(settingsKeyTag, key);
// check
m_key = MobileUtils::readFromKeychain(settingsKeyTag);
if (key != m_key) {
qCritical() << "SecureQSettings::getEncKey Unable to store key in keychain" << key.size() << m_key.size();
return {};
}
}
return m_key;
}
QByteArray SecureQSettings::getEncIv() const
{
// load keys from system key storage
m_iv = MobileUtils::readFromKeychain(settingsIvTag);
if (m_iv.isEmpty()) {
// Create new IV
QSimpleCrypto::QBlockCipher cipher;
QByteArray iv = cipher.generateSecureRandomBytes(32);
if (iv.isEmpty()) {
qCritical() << "SecureQSettings::getEncIv Unable to generate new enc IV";
}
MobileUtils::writeToKeychain(settingsIvTag, iv);
// check
m_iv = MobileUtils::readFromKeychain(settingsIvTag);
if (iv != m_iv) {
qCritical() << "SecureQSettings::getEncIv Unable to store IV in keychain" << iv.size() << m_iv.size();
return {};
}
}
return m_iv;
}
2022-08-06 19:47:29 +03:00