diff --git a/native/cpp/CommonCpp/DatabaseManagers/DatabaseQueryExecutor.h b/native/cpp/CommonCpp/DatabaseManagers/DatabaseQueryExecutor.h --- a/native/cpp/CommonCpp/DatabaseManagers/DatabaseQueryExecutor.h +++ b/native/cpp/CommonCpp/DatabaseManagers/DatabaseQueryExecutor.h @@ -58,6 +58,7 @@ virtual void storeOlmPersistData(crypto::Persist persist) const = 0; virtual void setNotifyToken(std::string token) const = 0; virtual void clearNotifyToken() const = 0; + virtual void clearSensitiveData() const = 0; }; } // namespace comm diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h @@ -10,7 +10,7 @@ namespace comm { class SQLiteQueryExecutor : public DatabaseQueryExecutor { - void migrate(); + void migrate() const; static auto &getStorage(); static std::once_flag initialized; @@ -57,6 +57,7 @@ void storeOlmPersistData(crypto::Persist persist) const override; void setNotifyToken(std::string token) const override; void clearNotifyToken() const override; + void clearSensitiveData() const override; }; } // namespace comm diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp @@ -401,7 +401,7 @@ {22, {enable_write_ahead_logging_mode, false}}, {23, {create_metadata_table, true}}}}; -void SQLiteQueryExecutor::migrate() { +void SQLiteQueryExecutor::migrate() const { validate_encryption(); sqlite3 *db; @@ -775,4 +775,20 @@ SQLiteQueryExecutor::getStorage().remove("notify_token"); } +void SQLiteQueryExecutor::clearSensitiveData() const { + if (file_exists(SQLiteQueryExecutor::sqliteFilePath) && + std::remove(SQLiteQueryExecutor::sqliteFilePath.c_str())) { + std::string error_message = "Failed to delete database file. Details: " + + std::string(strerror(errno)); + throw std::system_error(errno, std::generic_category(), error_message); + } + CommSecureStore commSecureStore; + std::string encryptionKey = comm::crypto::Tools::generateRandomHexString( + SQLiteQueryExecutor::sqlcipherEncryptionKeySize); + commSecureStore.set( + SQLiteQueryExecutor::secureStoreEncryptionKeyID, encryptionKey); + SQLiteQueryExecutor::encryptionKey = encryptionKey; + this->migrate(); +} + } // namespace comm