Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLiteConnection throws error in ctor on iOS 14 #986

Open
AdamBowler opened this issue Aug 18, 2020 · 10 comments
Open

SQLiteConnection throws error in ctor on iOS 14 #986

AdamBowler opened this issue Aug 18, 2020 · 10 comments
Labels

Comments

@AdamBowler
Copy link

AdamBowler commented Aug 18, 2020

When constructing the SQLiteConnection object on a device running iOS 14 the following error is throw

SQLite.SQLiteException: not an error
at SQLite.SQLiteCommand.ExecuteNonQuery () [0x000cd] in <d1788edcec634c19b907698bb77ed371>:0 \n  
at SQLite.SQLiteConnection.Execute (System.String query, System.Object[] args) [0x00039] in <d1788edcec634c19b907698bb77ed371>:0 \n  
at SQLite.SQLiteConnection.SetKey (System.String key) [0x00026] in <d1788edcec634c19b907698bb77ed371>:0 \n  
at SQLite.SQLiteConnection..ctor (SQLite.SQLiteConnectionString connectionString) [0x00126] in <d1788edcec634c19b907698bb77ed371>:0 \n  
at Expenses.Classes.SQLiteCrossPlatform.GetConnection () [0x00079] in /Users/StarLord/Documents/app/MyApp/Classes/SQLiteCrossPlatform.cs:48 "

Here is my code for the ctor

var sqLiteConnectionString = new SQLiteConnectionString(encryptedDbPath, true, AppConstants.KeyEncryption);
var encryptedDb = new SQLiteConnection(sqLiteConnectionString);

This only occurs on iOS 14 and not on any other version of iOS.

@AdamBowler
Copy link
Author

After investigating I've found that when this was implemented in our product the dev didn't add the sqlite-net-sqlcipher package or SQLitePCLRaw.bundle_sqlcipher which is what's referenced in the readme on the version of sqlite-net we are on (1.5.231), when they migrated the data into a DB with a key for encryption.

What would this mean to the level of encryption to the database? Would it still be encrypted at all?

After adding the sqlite-net-sqlcipher package I am getting an error stating the file is encrypted or not a database is there anyway to access the old database after adding the sqlite-net-sqlcipher package?

@xhashimks
Copy link

This is the same issue in my case also. I was using Sqlite-net-pcl earlier with which i created encrypted db. Now for iOS14, i started using sqlite-net-sqlcipher and when I tried to use the previously created encrypted db using this sqlcipher, i get 'file is not a database' error. I am stuck here.

@sjlombardo
Copy link
Contributor

@AdamBowler if you were previously using sqlite-net-pcl then your database may not even be encrypted even if you provided a key parameter to the constructor. You should extract a database of a device or simulator to check. If it is not encrypted you would need to perform steps to encrypt it. If it is encrypted you would need to either migrate it from SQLCipher 4 or setup backwards compatibility.

@sjlombardo
Copy link
Contributor

@AdamBowler Issue #991 is reporting a similar error, but I have not been able to reproduce a difference in behavior between iOS 13 and 14 with a test application so far. If you or @xhashimks can provide a standalone project that reliably reproduces the reported issue it would be helpful.

@praeclarum
Copy link
Owner

@AdamBowler if you were not using sql cipher then the database is not encrypted.

The reported exception is troubling and I'll investigate.

@praeclarum praeclarum added the Bug label Sep 23, 2020
@sjlombardo
Copy link
Contributor

While investigating similar reports we have discovered that iOS 13 and 14 appear to have changed the behavior of the system iOS SQLite library such that using PRAGMA key is no longer a no-op and instead causes the underlying file to be encrypted using an undefined scheme. This seems to be private and undocumented, not a public feature of the iOS ecosystem. The encryption is unrelated to and incompatible with SQLCipher. When an application using sqlite-net-pcl with the system library is run on iOS 13+ and triggers a PRAGMA key statement (by passing a key parameter to the constructor) the resulting database will be encrypted with this non-SQLCipher encryption. The behavior of the iOS System Library behavior changed under iOS 14 so that the string "ok" is returned as a row from PRAGMA key, which is consistent with more recent upstream SQLite changes related to that PRAGMA. That is causing the "Row" or "Not an error" exceptions. Under iOS 11 and 12 no encryption functionality is present, so a database from the same application run on those iOS versions will be unencrypted.

@ericsink
Copy link
Contributor

Wow.

@AdamBowler
Copy link
Author

Thanks @sjlombardo and @praeclarum for your assistance, I'll inform my company. Good news is we've switched to sqlite-net-pcl so it wont be an issue going forwarded.

@mingxli
Copy link

mingxli commented Jan 21, 2021

@praeclarum @ericsink @sjlombardo Thank you guys for the great libraries.

Unfortunately, I am having the same issue. After reading through this thread, #955, #972 and here, I am still unable to resolve my issue. I have uploaded a sample project, which demonstrate the "file is not a database" issue. Any help would be greatly appreciated.

@nelutzu91
Copy link

Hey guys, adding postKeyAction: c => c.Execute ("PRAGMA cipher_compatibility = 3") as stated here solved my issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants