From 8f596c7e6b4af80dda07632ba90f9a0048ae5e8a Mon Sep 17 00:00:00 2001 From: Julien Wadel Date: Wed, 28 Dec 2022 11:52:29 +0100 Subject: [PATCH] VFS sync API to write a key. Set a key if it is mandatory at startup. --- .../src/components/settings/SettingsModel.cpp | 2 +- linphone-app/src/components/vfs/VfsUtils.cpp | 31 +++++++++++++++++-- linphone-app/src/components/vfs/VfsUtils.hpp | 4 ++- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/linphone-app/src/components/settings/SettingsModel.cpp b/linphone-app/src/components/settings/SettingsModel.cpp index fb3c5ed62..5670b7e90 100644 --- a/linphone-app/src/components/settings/SettingsModel.cpp +++ b/linphone-app/src/components/settings/SettingsModel.cpp @@ -1745,7 +1745,7 @@ void SettingsModel::setVfsEncrypted (bool encrypted, const bool deleteUserData){ #ifdef ENABLE_QT_KEYCHAIN if(getVfsEncrypted() != encrypted){ if(encrypted) { - mVfsUtils.newEncryptionKey(); + mVfsUtils.newEncryptionKeyAsync(); }else{// Remove key, stop core, delete data and initiate reboot mVfsUtils.needToDeleteUserData(deleteUserData); mVfsUtils.deleteKey(mVfsUtils.getApplicationVfsEncryptionKey()); diff --git a/linphone-app/src/components/vfs/VfsUtils.cpp b/linphone-app/src/components/vfs/VfsUtils.cpp index 1ab077002..5f99b4189 100644 --- a/linphone-app/src/components/vfs/VfsUtils.cpp +++ b/linphone-app/src/components/vfs/VfsUtils.cpp @@ -24,6 +24,8 @@ #include #include +#include +#include #include #include @@ -96,7 +98,7 @@ void VfsUtils::needToDeleteUserData(const bool& need){ //----------------------------------------------------------------------------------------------- -void VfsUtils::newEncryptionKey(){ +void VfsUtils::newEncryptionKeyAsync(){ QString value; bctoolbox::RNG rng; auto key = rng.randomize(32); @@ -108,6 +110,22 @@ void VfsUtils::newEncryptionKey(){ writeKey(getApplicationVfsEncryptionKey(), value); } +bool VfsUtils::newEncryptionKey(){ + int argc = 1; + const char * argv = "dummy"; + QCoreApplication vfsSetter(argc,(char**)&argv); + VfsUtils vfs; + QObject::connect(&vfs, &VfsUtils::keyWritten, &vfsSetter, [&vfsSetter, &vfs] (const QString& key){ + vfsSetter.quit(); + }, Qt::QueuedConnection); + QObject::connect(&vfs, &VfsUtils::error, &vfsSetter, [&vfsSetter](const QString& errorText){ + qCritical() << "[VFS] " << errorText; + vfsSetter.exit(-1); + }, Qt::QueuedConnection); + vfs.newEncryptionKeyAsync(); + return vfsSetter.exec() != -1; +} + bool VfsUtils::updateSDKWithKey() { int argc = 1; const char * argv = "dummy"; @@ -118,11 +136,20 @@ bool VfsUtils::updateSDKWithKey() { vfs.mVfsEncrypted = true; vfsSetter.quit(); }, Qt::QueuedConnection); - QObject::connect(&vfs, &VfsUtils::error, &vfsSetter, [&vfsSetter](){ + 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. 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. + } + } + return vfs.mVfsEncrypted; } diff --git a/linphone-app/src/components/vfs/VfsUtils.hpp b/linphone-app/src/components/vfs/VfsUtils.hpp index dbf7eb047..4dc4db3fb 100644 --- a/linphone-app/src/components/vfs/VfsUtils.hpp +++ b/linphone-app/src/components/vfs/VfsUtils.hpp @@ -37,7 +37,9 @@ public: Q_INVOKABLE void writeKey(const QString& key, const QString& value); // Write a key and send error() or keyWritten() - void newEncryptionKey(); // Generate a key, store it and update SDK. + 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 void updateSDKWithKey(const QString& key);// SDK->setVfsEncryption(key)