From 5c19d291407c91ad313e4212ab14520cf277ca91 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 20 Aug 2019 13:04:46 -0300 Subject: [PATCH] Rework isDatabasePasswordAccessible. --- Signal/src/AppDelegate.m | 7 +++++- .../Storage/Database/SDSDatabaseStorage.swift | 22 ++++++++++++++----- .../src/Storage/StorageCoordinator.h | 2 ++ .../src/Storage/StorageCoordinator.m | 15 +++++++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 3d5097906e..3ded69c23d 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -170,6 +170,11 @@ static NSTimeInterval launchStartedAt; return SSKEnvironment.shared.syncManager; } +- (StorageCoordinator *)storageCoordinator +{ + return SSKEnvironment.shared.storageCoordinator; +} + #pragma mark - - (void)applicationDidEnterBackground:(UIApplication *)application @@ -333,7 +338,7 @@ static NSTimeInterval launchStartedAt; // GRDB TODO: We should consult YDB and GRDB. This should be done via storage coordinator, // which knows which checks we need to do. - if (![OWSPrimaryStorage isDatabasePasswordAccessible]) { + if (![self.storageCoordinator isDatabasePasswordAccessible]) { OWSLogInfo(@"exiting because we are in the background and the database password is not accessible."); UILocalNotification *notification = [UILocalNotification new]; diff --git a/SignalServiceKit/src/Storage/Database/SDSDatabaseStorage.swift b/SignalServiceKit/src/Storage/Database/SDSDatabaseStorage.swift index abf2ec4b87..f8179952da 100644 --- a/SignalServiceKit/src/Storage/Database/SDSDatabaseStorage.swift +++ b/SignalServiceKit/src/Storage/Database/SDSDatabaseStorage.swift @@ -149,6 +149,7 @@ public class SDSDatabaseStorage: SDSTransactable { Logger.error("storageMode: \(FeatureFlags.storageMode).") Logger.error( "StorageCoordinatorState: \(storageCoordinatorStateDescription).") + switch FeatureFlags.storageModeStrictness { case .fail: owsFail("Unexpected YDB load.") @@ -679,8 +680,20 @@ public class GRDBDatabaseStorageAdapter: NSObject { private let databaseUrl: URL - private let keyServiceName: String = "TSKeyChainService" - private let keyName: String = "OWSDatabaseCipherKeySpec" + private static let keyServiceName: String = "TSKeyChainService" + private static let keyName: String = "OWSDatabaseCipherKeySpec" + private static var keyspec: GRDBKeySpecSource { + return GRDBKeySpecSource(keyServiceName: keyServiceName, keyName: keyName) + } + @objc + public static var isKeyAccessible: Bool { + do { + return try keyspec.fetchString().count > 0 + } catch { + owsFailDebug("Key not accessible: \(error)") + return false + } + } private let storage: GRDBStorage @@ -691,7 +704,7 @@ public class GRDBDatabaseStorageAdapter: NSObject { init(baseDir: URL) throws { databaseUrl = GRDBDatabaseStorageAdapter.databaseFileUrl(baseDir: baseDir) - storage = try GRDBStorage(dbURL: databaseUrl, keyServiceName: keyServiceName, keyName: keyName) + storage = try GRDBStorage(dbURL: databaseUrl, keyspec: GRDBDatabaseStorageAdapter.keyspec) super.init() @@ -1083,9 +1096,8 @@ private struct GRDBStorage { private let dbURL: URL private let configuration: Configuration - init(dbURL: URL, keyServiceName: String, keyName: String) throws { + init(dbURL: URL, keyspec: GRDBKeySpecSource) throws { self.dbURL = dbURL - let keyspec = GRDBKeySpecSource(keyServiceName: keyServiceName, keyName: keyName) var configuration = Configuration() configuration.readonly = false diff --git a/SignalServiceKit/src/Storage/StorageCoordinator.h b/SignalServiceKit/src/Storage/StorageCoordinator.h index 4f3649ab3b..1ea050c2e3 100644 --- a/SignalServiceKit/src/Storage/StorageCoordinator.h +++ b/SignalServiceKit/src/Storage/StorageCoordinator.h @@ -28,6 +28,8 @@ NSString *NSStringFromStorageCoordinatorState(StorageCoordinatorState value); - (void)migrationYDBToGRDBWillBegin; - (void)migrationYDBToGRDBDidComplete; +- (BOOL)isDatabasePasswordAccessible; + @end NS_ASSUME_NONNULL_END diff --git a/SignalServiceKit/src/Storage/StorageCoordinator.m b/SignalServiceKit/src/Storage/StorageCoordinator.m index 7580fb3a4f..6b7f1bc834 100644 --- a/SignalServiceKit/src/Storage/StorageCoordinator.m +++ b/SignalServiceKit/src/Storage/StorageCoordinator.m @@ -132,6 +132,21 @@ NSString *NSStringFromStorageCoordinatorState(StorageCoordinatorState value) } } +- (BOOL)isDatabasePasswordAccessible +{ + if (self.databaseStorage.canLoadYdb) { + if (![OWSPrimaryStorage isDatabasePasswordAccessible]) { + return NO; + } + } + if (self.databaseStorage.canLoadGrdb) { + if (![GRDBDatabaseStorageAdapter isKeyAccessible]) { + return NO; + } + } + return YES; +} + @end NS_ASSUME_NONNULL_END