androidsqlciphercommonsware-cwac

SQLCipher with Room Persistence Library exception in SQLCipherUtils.getDatabaseState


I have a Room database in my Android app, and in my AppClass, I have some logic that checks the database encryption state in order to handle some potential encryption operations that might need to be performed on the database. However, whenever I call SQLCipherUtils.getDatabaseState on an encrypted database, it throws a SQLiteException. But the funny thing is that the state that is returned from getDatabaseState is not null and works perfectly fine, and the try-catch block surrounding the call to getDatabaseState doesn't catch the exception. So the exception doesn't affect the rest of the logic, from what I can tell. However, it is frustrating to see this error show up in the LogCat every single time the app lauches after the database has been encrypted. So my question is, is this error actually indicative of a problem? If so, how can I fix it, and if not, how can I silence the error?

Database Logic in Application class (Exception thrown on first line):

    try {
        // Get database state
        SQLCipherUtils.State state = SQLCipherUtils.getDatabaseState(getApplicationContext(), DB_NAME);

        // Check whether database is encrypted
        if (state == SQLCipherUtils.State.UNENCRYPTED) {
            // If it's unencrypted, encrypt the database
            try {
                // Get database instance
                FinancesDatabase db = FinancesDatabase.getDatabase(getApplicationContext());

                // Close database
                db.close();

                // Encrypt database with encryption key
                SQLCipherUtils.encrypt(getApplicationContext(), DB_NAME, sharedPrefs.getEncryptKey());
                Timber.i("Successfully encrypted database!");

            } catch (IOException e) {
                Timber.e(e, "Failed to encrypt previously unencrypted database!");
            }
        } else if (state == SQLCipherUtils.State.ENCRYPTED)
            // Otherwise, good to go
            Timber.i("Database is encrypted.  No action necessary.");

        else if (state == SQLCipherUtils.State.DOES_NOT_EXIST)
            // No database found.  Normal if first launch
            Timber.w("No database found.");

    } catch(Exception e) {
        Timber.e(e, "Failed to get database encryption state!");
    }

Exception:

E/Database: file is not a database: , while compiling: select count(*) from sqlite_master;
net.sqlcipher.database.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master;
    at net.sqlcipher.database.SQLiteCompiledSql.native_compile(Native Method)
    at net.sqlcipher.database.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
    at net.sqlcipher.database.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
    at net.sqlcipher.database.SQLiteProgram.<init>(SQLiteProgram.java:89)
    at net.sqlcipher.database.SQLiteQuery.<init>(SQLiteQuery.java:48)
    at net.sqlcipher.database.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:60)
    at net.sqlcipher.database.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1867)
    at net.sqlcipher.database.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1785)
    at net.sqlcipher.database.SQLiteDatabase.keyDatabase(SQLiteDatabase.java:2486)
    at net.sqlcipher.database.SQLiteDatabase.openDatabaseInternal(SQLiteDatabase.java:2415)
    at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1149)
    at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1116)
    at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1065)
    at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1019)
    at com.commonsware.cwac.saferoom.SQLCipherUtils.getDatabaseState(SQLCipherUtils.java:62)
    at com.commonsware.cwac.saferoom.SQLCipherUtils.getDatabaseState(SQLCipherUtils.java:46)
    at edu.usm.cs.csc414.pocketfinances.AppClass.onCreate(AppClass.java:118)
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1125)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6056)
    at android.app.ActivityThread.-wrap1(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6938)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

SQLCipherUtils.getDatabaseState implementation in library:

public static State getDatabaseState(Context ctxt, String dbName) {
    SQLiteDatabase.loadLibs(ctxt);

    return(getDatabaseState(ctxt.getDatabasePath(dbName)));
}



public static State getDatabaseState(File dbPath) {
    if (dbPath.exists()) {
      SQLiteDatabase db=null;

      try {
        db=
          SQLiteDatabase.openDatabase(dbPath.getAbsolutePath(), "",
            null, SQLiteDatabase.OPEN_READONLY);

        db.getVersion();

        return(State.UNENCRYPTED);
      }
      catch (Exception e) {
        return(State.ENCRYPTED);
      }
      finally {
        if (db != null) {
          db.close();
        }
      }
    }

    return(State.DOES_NOT_EXIST);
}

Solution

  • That is being logged directly by SQLCipher for Android. It will be logged any time an invalid passphrase is used. getDatabaseState() tries opening the database with the empty string as a passphrase, as that is the "passphrase" for an unencrypted database.

    Your options to avoid this log message are: