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 @@ -210,6 +210,9 @@ #else virtual void createMainCompaction(std::string backupID) const = 0; virtual void captureBackupLogs() const = 0; + virtual void setUserDataKeys( + const std::string &backupDataKey, + const std::string &backupLogDataKey) const = 0; #endif }; 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 @@ -225,6 +225,9 @@ static void initialize(std::string &databasePath); void createMainCompaction(std::string backupID) const override; void captureBackupLogs() const override; + void setUserDataKeys( + const std::string &backupDataKey, + const std::string &backupLogDataKey) const override; #endif }; 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 @@ -3160,6 +3160,38 @@ } this->setMetadata("logID", std::to_string(std::stoi(logID) + 1)); } + +void SQLiteQueryExecutor::setUserDataKeys( + const std::string &backupDataKey, + const std::string &backupLogDataKey) const { + if (SQLiteQueryExecutor::backupDataKey.empty()) { + throw std::runtime_error("backupDataKey is not set"); + } + + if (SQLiteQueryExecutor::backupLogDataKey.empty()) { + throw std::runtime_error("invalid backupLogDataKey size"); + } + + if (backupDataKey.size() != SQLiteQueryExecutor::backupDataKeySize) { + throw std::runtime_error("invalid backupDataKey size"); + } + + if (backupLogDataKey.size() != SQLiteQueryExecutor::backupLogDataKeySize) { + throw std::runtime_error("invalid backupLogDataKey size"); + } + + std::string rekey_encryption_key_query = + "PRAGMA rekey = \"x'" + backupDataKey + "'\";"; + + executeQuery( + SQLiteQueryExecutor::getConnection(), rekey_encryption_key_query); + + CommSecureStore::set(CommSecureStore::backupDataKey, backupDataKey); + SQLiteQueryExecutor::backupDataKey = backupDataKey; + + CommSecureStore::set(CommSecureStore::backupLogDataKey, backupLogDataKey); + SQLiteQueryExecutor::backupLogDataKey = backupLogDataKey; +} #endif void SQLiteQueryExecutor::restoreFromMainCompaction(