10000 Dropping setUseHMACPageProtection in favor of SQLiteDatabaseHook. · latty/android-database-sqlcipher@6929436 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6929436

Browse files
Dropping setUseHMACPageProtection in favor of SQLiteDatabaseHook.
An optional SQLiteDatabaseHook interface is introduced. This allows for abitrary execution both before and after the PRAGMA key is set, but prior to the setLocale allowing users to configure their instance accordingly. The preKey operation will likely be used by the cipher_default_use_hmac PRAGMA. The postKey callback allows for operations such as setting the cipher PRAGMA.
1 parent 0b56007 commit 6929436

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

src/net/sqlcipher/database/SQLiteDatabase.java

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import net.sqlcipher.DatabaseUtils;
2121
import net.sqlcipher.SQLException;
2222
import net.sqlcipher.database.SQLiteDebug.DbStats;
23+
import net.sqlcipher.database.SQLiteDatabaseHook;
2324

2425
import java.io.File;
2526
import java.io.FileOutputStream;
@@ -71,11 +72,6 @@ public class SQLiteDatabase extends SQLiteClosable {
7172
private static final String TAG = "Database";
7273
private static final int EVENT_DB_OPERATION = 52000;
7374
private static final int EVENT_DB_CORRUPT = 75004;
74-
private static boolean useHMACPageProtection = true;
75-
76-
public static void setUseHMACPageProtection(boolean value){
77-
SQLiteDatabase.useHMACPageProtection = value;
78-
}
7975

8076
private static void loadICUData(Context context) {
8177

@@ -862,11 +858,11 @@ public Cursor newCursor(SQLiteDatabase db,
862858
* @return the newly opened database
863859
* @throws SQLiteException if the database cannot be opened
864860
*/
865-
public static SQLiteDatabase openDatabase(String path, String password, CursorFactory factory, int flags) {
861+
public static SQLiteDatabase openDatabase(String path, String password, CursorFactory factory, int flags, SQLiteDatabaseHook databaseHook) {
866862
SQLiteDatabase sqliteDatabase = null;
867863
try {
868864
// Open the database.
869-
sqliteDatabase = new SQLiteDatabase(path, password, factory, flags);
865+
sqliteDatabase = new SQLiteDatabase(path, password, factory, flags, databaseHook);
870866
if (SQLiteDebug.DEBUG_SQL_STATEMENTS) {
871867
sqliteDatabase.enableSqlTracing(path);
872868
}
@@ -882,25 +878,38 @@ public static SQLiteDatabase openDatabase(String path, String password, CursorFa
882878
// delete is only for non-memory database files
883879
new File(path).delete();
884880
}
885-
sqliteDatabase = new SQLiteDatabase(path, password, factory, flags);
881+
sqliteDatabase = new SQLiteDatabase(path, password, factory, flags, databaseHook);
886882
}
887883
ActiveDatabases.getInstance().mActiveDatabases.add(
888884
new WeakReference<SQLiteDatabase>(sqliteDatabase));
889885
return sqliteDatabase;
890886
}
887+
888+
public static SQLiteDatabase openOrCreateDatabase(File file, String password, CursorFactory factory, SQLiteDatabaseHook databaseHook){
889+
return openOrCreateDatabase(file.getPath(), password, factory, databaseHook);
890+
}
891891

892+
public static SQLiteDatabase openOrCreateDatabase(String path, String password, CursorFactory factory, SQLiteDatabaseHook databaseHook) {
893+
return openDatabase(path, password, factory, CREATE_IF_NECESSARY, databaseHook);
894+
}
895+
892896
/**
893897
* Equivalent to openDatabase(file.getPath(), factory, CREATE_IF_NECESSARY).
894898
*/
895899
public static SQLiteDatabase openOrCreateDatabase(File file, String password, CursorFactory factory) {
896-
return openOrCreateDatabase(file.getPath(), password, factory);
900+
return openOrCreateDatabase(file.getPath(), password, factory, null);
897901
}
898902

899903
/**
900904
* Equivalent to openDatabase(path, factory, CREATE_IF_NECESSARY).
901905
*/
906+
902907
public static SQLiteDatabase openOrCreateDatabase(String path, String password, CursorFactory factory) {
903-
return openDatabase(path, password, factory, CREATE_IF_NECESSARY);
908+
return openDatabase(path, password, factory, CREATE_IF_NECESSARY, null);
909+
}
910+
911+
public static SQLiteDatabase openDatabase(String path, String password, CursorFactory factory, int flags) {
912+
return openDatabase(path, password, factory, CREATE_IF_NECESSARY, null);
904913
}
905914

906915
/**
@@ -1881,6 +1890,10 @@ protected void finalize() {
18811890
}
18821891
}
18831892

1893+
public SQLiteDatabase(String path, String password, CursorFactory factory, int flags) {
1894+
this(path, password, factory, flags, null);
1895+
}
1896+
18841897
/**
18851898
* Private constructor. See {@link #create} and {@link #openDatabase}.
18861899
*
@@ -1889,8 +1902,7 @@ protected void finalize() {
18891902
* @param flags 0 or {@link #NO_LOCALIZED_COLLATORS}. If the database file already
18901903
* exists, mFlags will be updated appropriately.
18911904
*/
1892-
public SQLiteDatabase(String path, String password, CursorFactory factory, int flags) {
1893-
1905+
public SQLiteDatabase(String path, String password, CursorFactory factory, int flags, SQLiteDatabaseHook databaseHook) {
18941906

18951907
if (path == null) {
18961908
throw new IllegalArgumentException("path should not be null");
@@ -1901,15 +1913,17 @@ public SQLiteDatabase(String path, String password, CursorFactory factory, int f
19011913
mStackTrace = new DatabaseObjectNotClosedException().fillInStackTrace();
19021914
mFactory = factory;
19031915
dbopen(mPath, mFlags);
1904-
1905-
if(SQLiteDatabase.useHMACPageProtection){
1906-
execSQL("PRAGMA cipher_default_use_hmac = ON");
1907-
} else {
1908-
execSQL("PRAGMA cipher_default_use_hmac = OFF");
1916+
1917+
if(databaseHook != null){
1918+
databaseHook.preKey(this);
19091919
}
19101920

19111921
execSQL("PRAGMA key = '" + password + "'");
19121922

1923+
if(databaseHook != null){
1924+
databaseHook.postKey(this);
1925+
}
1926+
19131927
if (SQLiteDebug.DEBUG_SQL_CACHE) {
19141928
mTimeOpened = getTime();
19151929
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package net.sqlcipher.database;
2+
3+
public interface SQLiteDatabaseHook {
4+
void preKey(SQLiteDatabase database);
5+
void postKey(SQLiteDatabase database);
6+
}

0 commit comments

Comments
 (0)
0