diff --git a/native/cpp/CommonCpp/DatabaseManagers/DatabaseManager.h b/native/cpp/CommonCpp/DatabaseManagers/DatabaseManager.h --- a/native/cpp/CommonCpp/DatabaseManagers/DatabaseManager.h +++ b/native/cpp/CommonCpp/DatabaseManagers/DatabaseManager.h @@ -11,10 +11,6 @@ namespace comm { class DatabaseManager { - // Constant key sizes - static const int backupDataKeySize; - static const int backupLogDataKeySize; - // Connection manager instance, should be only one (globally) to each // database. // DatabaseIdentifier::MAIN connectionManager. diff --git a/native/cpp/CommonCpp/DatabaseManagers/DatabaseManager.cpp b/native/cpp/CommonCpp/DatabaseManagers/DatabaseManager.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/DatabaseManager.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/DatabaseManager.cpp @@ -18,9 +18,6 @@ namespace comm { -const int DatabaseManager::backupDataKeySize = 64; -const int DatabaseManager::backupLogDataKeySize = 32; - std::shared_ptr DatabaseManager::mainConnectionManager; @@ -167,15 +164,13 @@ } std::string DatabaseManager::generateBackupDataKey() { - std::string backupDataKey = comm::crypto::Tools::generateRandomHexString( - DatabaseManager::backupDataKeySize); + std::string backupDataKey = SQLiteBackup::generateRandomBackupDataKey(); CommSecureStore::set(CommSecureStore::backupDataKey, backupDataKey); return backupDataKey; } std::string DatabaseManager::generateBackupLogDataKey() { - std::string backupLogDataKey = comm::crypto::Tools::generateRandomHexString( - DatabaseManager::backupLogDataKeySize); + std::string backupLogDataKey = SQLiteBackup::generateRandomBackupLogDataKey(); CommSecureStore::set(CommSecureStore::backupLogDataKey, backupLogDataKey); return backupLogDataKey; } @@ -237,11 +232,11 @@ throw std::runtime_error("backupLogDataKey is not set"); } - if (backupDataKey.size() != DatabaseManager::backupDataKeySize) { + if (backupDataKey.size() != SQLiteBackup::backupDataKeySize) { throw std::runtime_error("invalid backupDataKey size"); } - if (backupLogDataKey.size() != DatabaseManager::backupLogDataKeySize) { + if (backupLogDataKey.size() != SQLiteBackup::backupLogDataKeySize) { throw std::runtime_error("invalid backupLogDataKey size"); } diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.h b/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.h --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.h +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.h @@ -8,6 +8,10 @@ namespace comm { class SQLiteBackup { public: + // Constant key sizes + static const int backupDataKeySize; + static const int backupLogDataKeySize; + static std::unordered_set tablesAllowlist; static void cleanupDatabaseExceptAllowlist(sqlite3 *db); @@ -27,5 +31,10 @@ std::string mainCompactionEncryptionKey, std::optional plaintextDatabasePath, std::string maxVersion); + + // Generate a random backup key used as a database encryption key. + static std::string generateRandomBackupDataKey(); + // Generate a random key used for encrypt backup logs. + static std::string generateRandomBackupLogDataKey(); }; } // namespace comm diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.cpp b/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.cpp @@ -1,5 +1,6 @@ #include "SQLiteBackup.h" +#include "../CryptoTools/Tools.h" #include "SQLiteUtils.h" #include "entities/EntityQueryHelpers.h" @@ -11,6 +12,9 @@ namespace comm { +const int SQLiteBackup::backupDataKeySize = 64; +const int SQLiteBackup::backupLogDataKeySize = 32; + std::unordered_set SQLiteBackup::tablesAllowlist = { "drafts", "threads", @@ -96,4 +100,16 @@ return plaintextBackupPath; } +std::string SQLiteBackup::generateRandomBackupDataKey() { + std::string backupDataKey = comm::crypto::Tools::generateRandomHexString( + SQLiteBackup::backupDataKeySize); + return backupDataKey; +} + +std::string SQLiteBackup::generateRandomBackupLogDataKey() { + std::string backupLogDataKey = comm::crypto::Tools::generateRandomHexString( + SQLiteBackup::backupLogDataKeySize); + return backupLogDataKey; +} + } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/BackupOperationsUtilities/BackupOperationsExecutor.h b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/BackupOperationsUtilities/BackupOperationsExecutor.h --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/BackupOperationsUtilities/BackupOperationsExecutor.h +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/BackupOperationsUtilities/BackupOperationsExecutor.h @@ -16,5 +16,7 @@ const std::vector &backupLog, size_t futureID); static void setBackupID(std::string backupID, size_t futureID); + static std::string generateBackupDataKey(); + static std::string generateBackupLogDataKey(); }; } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/BackupOperationsUtilities/BackupOperationsExecutor.cpp b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/BackupOperationsUtilities/BackupOperationsExecutor.cpp --- a/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/BackupOperationsUtilities/BackupOperationsExecutor.cpp +++ b/native/cpp/CommonCpp/NativeModules/PersistentStorageUtilities/BackupOperationsUtilities/BackupOperationsExecutor.cpp @@ -3,6 +3,7 @@ #include "GlobalDBSingleton.h" #include "Logger.h" #include "RustPromiseManager.h" +#include "SQLiteBackup.h" #include "WorkerThread.h" #include "lib.rs.h" @@ -77,4 +78,12 @@ }; GlobalDBSingleton::instance.scheduleOrRunCancellable(job); } + +std::string BackupOperationsExecutor::generateBackupDataKey() { + return SQLiteBackup::generateRandomBackupDataKey(); +} + +std::string BackupOperationsExecutor::generateBackupLogDataKey() { + return SQLiteBackup::generateRandomBackupLogDataKey(); +} } // namespace comm diff --git a/native/native_rust_library/RustBackupExecutor.h b/native/native_rust_library/RustBackupExecutor.h --- a/native/native_rust_library/RustBackupExecutor.h +++ b/native/native_rust_library/RustBackupExecutor.h @@ -18,5 +18,7 @@ size_t futureID); void restoreFromBackupLog(rust::Vec backupLog, size_t futureID); void setBackupID(rust::Str backupID, size_t futureID); +rust::String generateBackupDataKey(); +rust::String generateBackupLogDataKey(); } // namespace comm diff --git a/native/native_rust_library/RustBackupExecutor.cpp b/native/native_rust_library/RustBackupExecutor.cpp --- a/native/native_rust_library/RustBackupExecutor.cpp +++ b/native/native_rust_library/RustBackupExecutor.cpp @@ -57,4 +57,12 @@ std::move(std::vector(backupLog.begin(), backupLog.end())), futureID); } + +rust::String generateBackupDataKey() { + return rust::String(BackupOperationsExecutor::generateBackupDataKey()); +} + +rust::String generateBackupLogDataKey() { + return rust::String(BackupOperationsExecutor::generateBackupLogDataKey()); +} } // namespace comm diff --git a/native/native_rust_library/src/lib.rs b/native/native_rust_library/src/lib.rs --- a/native/native_rust_library/src/lib.rs +++ b/native/native_rust_library/src/lib.rs @@ -13,9 +13,7 @@ mod utils; use crate::argon2_tools::compute_backup_key_str; -use crate::utils::jsi_callbacks::{ - handle_string_result_as_callback, handle_void_result_as_callback, -}; +use crate::utils::jsi_callbacks::handle_string_result_as_callback; mod generated { // We get the CODE_VERSION from this generated file @@ -531,6 +529,12 @@ #[cxx_name = "setBackupID"] fn set_backup_id(backup_id: &str, future_id: usize); + + #[cxx_name = "generateBackupDataKey"] + fn generate_backup_data_key() -> Result; + + #[cxx_name = "generateBackupLogDataKey"] + fn generate_backup_log_data_key() -> Result; } // Future handling from C++ @@ -575,6 +579,7 @@ } #[test] + #[allow(clippy::const_is_empty)] fn test_identity_socket_addr_exists() { assert!(!IDENTITY_SOCKET_ADDR.is_empty()); assert!(!BACKUP_SOCKET_ADDR.is_empty());