Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3372874
D4846.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D4846.diff
View Options
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
@@ -323,6 +323,20 @@
}
}
+bool is_database_queryable(sqlite3 *db, bool use_encryption_key) {
+ char *err_msg;
+ sqlite3_open(SQLiteQueryExecutor::sqliteFilePath.c_str(), &db);
+ // According to SQLCipher documentation running some SELECT is the only way to
+ // check for key validity
+ if (use_encryption_key) {
+ set_encryption_key(db);
+ }
+ sqlite3_exec(
+ db, "SELECT COUNT(*) FROM sqlite_master;", nullptr, nullptr, &err_msg);
+ sqlite3_close(db);
+ return !err_msg;
+}
+
void validate_encryption() {
std::string temp_encrypted_db_path =
SQLiteQueryExecutor::sqliteFilePath + "_temp_encrypted";
@@ -355,28 +369,25 @@
}
sqlite3 *db;
- sqlite3_open(SQLiteQueryExecutor::sqliteFilePath.c_str(), &db);
- on_database_open(db);
-
- char *key_validation_error;
- // According to SQLCipher documentation running some SELECT is the only way to
- // check for key validity
- sqlite3_exec(
- db,
- "SELECT COUNT(*) FROM sqlite_master;",
- nullptr,
- nullptr,
- &key_validation_error);
- sqlite3_close(db);
-
- if (!key_validation_error) {
+ if (is_database_queryable(db, true)) {
Logger::log(
"Database exists under default path and it is correctly encrypted.");
return;
}
- Logger::log(
- "Validation of encryption key failed. Attempting encryption process.");
+ if (!is_database_queryable(db, false)) {
+ Logger::log(
+ "Database exists but it is encrypted with key that was lost. "
+ "Attempting database deletion. New encrypted one will be created.");
+ attempt_delete_file(
+ SQLiteQueryExecutor::sqliteFilePath.c_str(),
+ "Failed to delete database encrypted with lost key.");
+ return;
+ } else {
+ Logger::log(
+ "Database exists but it is not encrypted. Attempting encryption "
+ "process.");
+ }
sqlite3_open(SQLiteQueryExecutor::sqliteFilePath.c_str(), &db);
std::string createEncryptedCopySQL = "ATTACH DATABASE '" +
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Nov 27, 8:13 AM (20 h, 26 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2589546
Default Alt Text
D4846.diff (2 KB)
Attached To
Mode
D4846: Check for the case of database encrypted with key that is lost
Attached
Detach File
Event Timeline
Log In to Comment