From 1f7f79c702c4ea859f3b8b61958fbdba48a0e4f7 Mon Sep 17 00:00:00 2001 From: Julien Wadel Date: Tue, 4 Apr 2023 16:53:07 +0200 Subject: [PATCH] Replace vfs_encryption_enabled by QSettings to avoid keychain check at startup. vfs_encryption_enabled is now used only to check if vfs is mandatory on first start. --- linphone-app/src/app/AppController.cpp | 22 +++--- linphone-app/src/app/AppController.hpp | 1 + linphone-app/src/app/main.cpp | 2 +- .../src/components/settings/SettingsModel.cpp | 16 +++-- linphone-app/src/components/vfs/VfsUtils.cpp | 69 ++++++++++++------- linphone-app/src/components/vfs/VfsUtils.hpp | 6 +- 6 files changed, 77 insertions(+), 39 deletions(-) diff --git a/linphone-app/src/app/AppController.cpp b/linphone-app/src/app/AppController.cpp index 1d92bc196..ed85c6698 100644 --- a/linphone-app/src/app/AppController.cpp +++ b/linphone-app/src/app/AppController.cpp @@ -43,15 +43,7 @@ AppController::AppController (int &argc, char *argv[]) { Q_ASSERT(!mApp); // Disable QML cache. Avoid malformed cache. qputenv("QML_DISABLE_DISK_CACHE", "true"); - QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); - // Useful to share camera on Fullscreen (other context) - QApplication::setAttribute(Qt::AA_ShareOpenGLContexts); - - // Do not use APPLICATION_NAME here. - // The EXECUTABLE_NAME will be used in qt standard paths. It's our goal. - QCoreApplication::setApplicationName(EXECUTABLE_NAME); - QApplication::setOrganizationDomain(EXECUTABLE_NAME); - QCoreApplication::setApplicationVersion(APPLICATION_SEMVER); + initQtAppDetails(); #ifdef ENABLE_APP_WEBVIEW #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) mApp = new App(argc, argv); @@ -126,4 +118,16 @@ void AppController::stopApp(){ } catch(...){ } +} + +void AppController::initQtAppDetails(){ + QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + // Useful to share camera on Fullscreen (other context) + QApplication::setAttribute(Qt::AA_ShareOpenGLContexts); + + // Do not use APPLICATION_NAME here. + // The EXECUTABLE_NAME will be used in qt standard paths. It's our goal. + QCoreApplication::setApplicationName(EXECUTABLE_NAME); + QApplication::setOrganizationDomain(EXECUTABLE_NAME); + QCoreApplication::setApplicationVersion(APPLICATION_SEMVER); } \ No newline at end of file diff --git a/linphone-app/src/app/AppController.hpp b/linphone-app/src/app/AppController.hpp index 2a3078709..ffc1df853 100644 --- a/linphone-app/src/app/AppController.hpp +++ b/linphone-app/src/app/AppController.hpp @@ -35,6 +35,7 @@ public: return mApp; } void stopApp(); + static void initQtAppDetails(); private: App *mApp = nullptr; }; diff --git a/linphone-app/src/app/main.cpp b/linphone-app/src/app/main.cpp index ad26cf04d..d503ff00b 100644 --- a/linphone-app/src/app/main.cpp +++ b/linphone-app/src/app/main.cpp @@ -62,7 +62,7 @@ int main (int argc, char *argv[]) { #endif #ifdef ENABLE_QT_KEYCHAIN - bool vfsEncrypted = VfsUtils::updateSDKWithKey(); + bool vfsEncrypted = VfsUtils::updateSDKWithKey(argc, argv); #else bool vfsEncrypted = false; #endif diff --git a/linphone-app/src/components/settings/SettingsModel.cpp b/linphone-app/src/components/settings/SettingsModel.cpp index 3744f5cc5..4eea12fed 100644 --- a/linphone-app/src/components/settings/SettingsModel.cpp +++ b/linphone-app/src/components/settings/SettingsModel.cpp @@ -73,7 +73,9 @@ SettingsModel::SettingsModel (QObject *parent) : QObject(parent) { connect(&mVfsUtils, &VfsUtils::keyRead, this, [&](const QString& key, const QString& value){ if(key == mVfsUtils.getApplicationVfsEncryptionKey()){ if(!getVfsEncrypted()){ - mConfig->setBool(UiSection, "vfs_encryption_enabled", true); + QSettings settings; + settings.beginGroup("keychain"); + settings.setValue("enabled", true); emit vfsEncryptedChanged(); } } @@ -81,14 +83,18 @@ SettingsModel::SettingsModel (QObject *parent) : QObject(parent) { connect(&mVfsUtils, &VfsUtils::keyWritten, this, [&](const QString& key){ if(key == mVfsUtils.getApplicationVfsEncryptionKey()){ if(!getVfsEncrypted()){ - mConfig->setBool(UiSection, "vfs_encryption_enabled", true); + QSettings settings; + settings.beginGroup("keychain"); + settings.setValue("enabled", true); emit vfsEncryptedChanged(); } } }); connect(&mVfsUtils, &VfsUtils::keyDeleted, this, [&](const QString& key){ if(key == mVfsUtils.getApplicationVfsEncryptionKey()){ - mConfig->setBool(UiSection, "vfs_encryption_enabled", false); + QSettings settings; + settings.beginGroup("keychain"); + settings.setValue("enabled", false); emit vfsEncryptedChanged(); if(mVfsUtils.needToDeleteUserData()) Utils::deleteAllUserData(); @@ -1865,7 +1871,9 @@ bool SettingsModel::getFullLogsEnabled (const shared_ptr &conf // --------------------------------------------------------------------------- bool SettingsModel::getVfsEncrypted (){ - return mConfig->getBool(UiSection, "vfs_encryption_enabled", false); + QSettings settings; + settings.beginGroup("keychain"); + return settings.value("enabled", false).toBool(); } void SettingsModel::setVfsEncrypted (bool encrypted, const bool deleteUserData){ diff --git a/linphone-app/src/components/vfs/VfsUtils.cpp b/linphone-app/src/components/vfs/VfsUtils.cpp index 5f99b4189..fa04a546d 100644 --- a/linphone-app/src/components/vfs/VfsUtils.cpp +++ b/linphone-app/src/components/vfs/VfsUtils.cpp @@ -24,6 +24,7 @@ #include #include +#include "app/AppController.hpp" #include #include #include @@ -125,32 +126,54 @@ bool VfsUtils::newEncryptionKey(){ vfs.newEncryptionKeyAsync(); return vfsSetter.exec() != -1; } - -bool VfsUtils::updateSDKWithKey() { - int argc = 1; - const char * argv = "dummy"; - QCoreApplication vfsSetter(argc,(char**)&argv); - VfsUtils vfs; - QObject::connect(&vfs, &VfsUtils::keyRead, &vfsSetter, [&vfsSetter, &vfs] (const QString& key, const QString& value){ - VfsUtils::updateSDKWithKey(value); - vfs.mVfsEncrypted = true; - vfsSetter.quit(); - }, Qt::QueuedConnection); - QObject::connect(&vfs, &VfsUtils::error, &vfsSetter, [&vfsSetter](const QString& errorText){ - vfsSetter.quit(); - }, Qt::QueuedConnection); - vfs.readKey(vfs.getApplicationVfsEncryptionKey()); - vfsSetter.exec(); +bool VfsUtils::updateSDKWithKey(int argc, char *argv[]){ + QCoreApplication core(argc,argv); + AppController::initQtAppDetails(); // Set settings context. + QSettings settings; + return updateSDKWithKey(&settings); +} +bool VfsUtils::updateSDKWithKey(){ + QSettings settings; + return updateSDKWithKey(&settings); +} - - if(!vfs.mVfsEncrypted){// Doesn't have key. Check in factory if it is mandatory. - auto config = linphone::Factory::get()->createConfigWithFactory("", Paths::getFactoryConfigFilePath()); - if(config->getBool(SettingsModel::UiSection, "vfs_encryption_enabled", false)){ - return VfsUtils::newEncryptionKey();// Return false on error. - } +bool VfsUtils::updateSDKWithKey(QSettings * settings){ // Update SDK if key exists. Return true if encrypted. + bool isEnabled = false; + //Check in factory if it is mandatory. + auto config = linphone::Factory::get()->createConfigWithFactory("", Paths::getFactoryConfigFilePath()); + if(config->getBool(SettingsModel::UiSection, "vfs_encryption_enabled", false)){ + isEnabled = true; } - return vfs.mVfsEncrypted; + settings->beginGroup("keychain"); + bool settingsValue = settings->value("enabled", false).toBool(); + if( isEnabled && !settingsValue) + settings->setValue("enabled", isEnabled); + else if(!isEnabled) + isEnabled = settingsValue; + if( isEnabled){ + int argc = 1; + const char * argv = "dummy"; + QCoreApplication vfsSetter(argc,(char**)&argv); + VfsUtils vfs; + QObject::connect(&vfs, &VfsUtils::keyRead, &vfsSetter, [&vfsSetter, &vfs] (const QString& key, const QString& value){ + VfsUtils::updateSDKWithKey(value); + vfs.mVfsEncrypted = true; + vfsSetter.quit(); + }, Qt::QueuedConnection); + QObject::connect(&vfs, &VfsUtils::error, &vfsSetter, [&vfsSetter](const QString& errorText){ + vfsSetter.quit(); + }, Qt::QueuedConnection); + vfs.readKey(vfs.getApplicationVfsEncryptionKey()); + vfsSetter.exec(); + + if(!vfs.mVfsEncrypted){// Doesn't have key. + return VfsUtils::newEncryptionKey();// Return false on error. + } + + return vfs.mVfsEncrypted; + }else + return false; } void VfsUtils::updateSDKWithKey(const QString& key){ diff --git a/linphone-app/src/components/vfs/VfsUtils.hpp b/linphone-app/src/components/vfs/VfsUtils.hpp index 4dc4db3fb..47c8f5a38 100644 --- a/linphone-app/src/components/vfs/VfsUtils.hpp +++ b/linphone-app/src/components/vfs/VfsUtils.hpp @@ -23,7 +23,7 @@ #include #include - +#include // ============================================================================= class VfsUtils : public QObject { @@ -40,7 +40,9 @@ public: void newEncryptionKeyAsync(); // Generate a key, store it and update SDK. Wait for keyWritten() or error(). static bool newEncryptionKey(); // Generate a key, store it and update SDK. - static bool updateSDKWithKey(); // Update SDK if key exists. Return true if encrypted. + static bool updateSDKWithKey(int argc, char *argv[]); // Can be calle outside application. + static bool updateSDKWithKey(QSettings * settings); // Update SDK if key exists. Return true if encrypted. + static bool updateSDKWithKey();// Need it to pass QSettings static void updateSDKWithKey(const QString& key);// SDK->setVfsEncryption(key) QString getApplicationVfsEncryptionKey() const;// Get the key in store keys for VFS encryyption