4

I'm using FMDB to work with sqlite and I'd prefer to avoid a dependency on SQLCipher. How can I simply leverage the DataProtection capability built into iOS? Is this possible - the only requirement is to protect the data in the event of the phone being stolen.

If the phone is unlocked with a PIN, it's fine that the user could access the DB - it's their data.

2 Answers 2

5
+100

Look for the line where you do databaseWithPath: (or initWithPath:), then add:

FMDatabase *db = [FMDatabase databaseWithPath:path];

NSDictionary *attributes = @{NSFileProtectionKey: NSFileProtectionCompleteUnlessOpen};
NSError *error;
BOOL success = [[NSFileManager defaultManager] setAttributes:attributes
                                                ofItemAtPath:path
                                                       error:&error];
if (!success) {
    NSLog(@"File protection failed: %@", error);
}

The possible Values for the NSFileProtectionKey key are:

  • NSFileProtectionNone: The file has no special protections associated with it. It can be read from or written to at any time.
  • NSFileProtectionComplete: The file is stored in an encrypted format on disk and cannot be read from or written to while the device is locked or booting.
  • NSFileProtectionCompleteUnlessOpen: The file is stored in an encrypted format on disk. Files can be created while the device is locked, but once closed, cannot be opened again until the device is unlocked. If the file is opened when unlocked, you may continue to access the file normally, even if the user locks the device. There is a small performance penalty when the file is created and opened, though not when being written to or read from. This can be mitigated by changing the file protection to NSFileProtectionComplete when the device is unlocked.
  • NSFileProtectionCompleteUntilFirstUserAuthentication: The file is stored in an encrypted format on disk and cannot be accessed until after the device has booted. After the user unlocks the device for the first time, your app can access the file and continue to access it even if the user subsequently locks the device.

The right type of protection may depend on the version of iOS (the last two are not available on iOS 4) and whether you use your database when the device is locked.

1
  • Does this mean the file is NOT encrypted if the user has not specified a device PIN then?
    – Oli
    Feb 8, 2014 at 23:55
3

By far the easiest way is to turn on Data Protection for the entire app. Go to App IDs, click "Edit" and set "Sharing and Permissions" to "Complete Protection."

enter image description here

Update Xcode with your new app id information, and from there on, it'll be handled for your app automatically.

12
  • Thanks, is it possible to use Data Protection for the specific sqlite file? Aug 24, 2013 at 18:04
  • Yes, using [NSFileManager setAttibutes:] with NSFileProtectionKey : NSFileProtectionComplete. See page 18 of this preso: github.com/rnapier/practical-security/blob/master/….
    – Rob Napier
    Aug 24, 2013 at 19:24
  • @RobNapier if I use your approach and turn on Data Protection for the entire app, do I need to set NSFileProtectionKey : NSFileProtectionComplete on the sqlite database file? And will background access with background context in Core Data work?
    – mrd
    May 8, 2015 at 15:53
  • @mradlmaier If you turn on app-wide data protect, then you do not have to also set the protection key (unless it's been set to something else). You will be able to access the file in the background, but not when the device is locked. If you need to do so, you'll need to use "Protected Unless Open" for either that file or app-wide.
    – Rob Napier
    May 8, 2015 at 16:10
  • So I had to enable Data Protection in the App ID, AND ADDITIONALLY use "Protected Unless Open" on my Core Data file and background access will be enabled, and Core Data encrypted? How can I know if the device is locked?
    – mrd
    May 8, 2015 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.