Search code examples
app-storecore-data-migrationnspersistentstorekeychainitemwrapperencrypted-core-data-sql

Application is crashing when creating a options Dictionary used by addPersistentStoreWithType method


I am using encrypted-core-data to encrypts all data that is persisted , previously simple CoreData was using. persistentStoreCoordinator creation code are as follows .

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (_persistentStoreCoordinator != nil) {
    return _persistentStoreCoordinator;
}

NSURL *oldStoreURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"VistaJetApp.sqlite"];
NSURL *newStoreURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"VistaJet.sqlite"];
NSError *error = nil;

NSString *currentPassword = [[VJAesCryptoWrapper getInstance] getCurrentPassword];
NSDictionary *options = [self getEncryptedStoreOptionsWithPassword:currentPassword andDatabaseStore:newStoreURL];

_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

//if old store not exists , it means fresh installation
if([[NSFileManager defaultManager] fileExistsAtPath:oldStoreURL.path] == NO) {             
    if (![_persistentStoreCoordinator addPersistentStoreWithType:EncryptedStoreType configuration:nil URL:newStoreURL options:options error: &error]) {         
    }        
} else {

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:oldStoreURL options:@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} error: &error]) {                     
    }

    NSPersistentStore *oldUnsecureStore = [_persistentStoreCoordinator persistentStoreForURL:oldStoreURL];
    [ConsoleLogger logText:[NSString stringWithFormat:@"Migration started"]];

    //start migration
    if(![_persistentStoreCoordinator migratePersistentStore:oldUnsecureStore toURL:newStoreURL options:options withType:EncryptedStoreType error:&error]) {


    } else {
        [[NSFileManager defaultManager] removeItemAtURL:oldStoreURL error:nil];
    }

}

return _persistentStoreCoordinator;

}

Creation of options dictionary

- (NSDictionary*)getEncryptedStoreOptionsWithPassword:(NSString*)password andDatabaseStore:(NSURL*)storeUrl {
return @{ EncryptedStorePassphraseKey: password,
          EncryptedStoreDatabaseLocation: storeUrl,
          NSMigratePersistentStoresAutomaticallyOption:@YES,
          NSInferMappingModelAutomaticallyOption:@YES
          };
}

I am saving password in keychain using KeychainItemWrapper and my code crashing exactly on getEncryptedStoreOptionsWithPassword:currentPassword method. App is live and I am not able to reproduce the crash , but on crashlytics it is showing so many crashes

crashlytics crash logs image

Also using AESCrypt to encrypt password then saving it to keychain using KeychainItemWrapper.

Observation:
The crash that crashlytics is displaying only appear when we upload build on test flight using distribution profile.

crash is happening 100% on iOS 11 as reported by crashlytics


Solution

  • I think this is a known bug in iOS 10 that you are seeing, there is a work-around: enable "Keychain Sharing" (under your app -> Capabilities tab in Xcode).

    KeychainItemWrapper crash on iOS10