session-ios/src/Storage/TSStorageManager.m

491 lines
16 KiB
Mathematica
Raw Normal View History

//
// TSStorageManager.m
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
2015-12-07 03:31:43 +01:00
#import "TSStorageManager.h"
2015-12-07 03:31:43 +01:00
#import "NSData+Base64.h"
#import "OWSAnalytics.h"
#import "OWSDisappearingMessagesFinder.h"
#import "OWSReadReceipt.h"
#import "SignalRecipient.h"
2015-12-07 03:31:43 +01:00
#import "TSAttachmentStream.h"
#import "TSDatabaseSecondaryIndexes.h"
#import "TSDatabaseView.h"
#import "TSInteraction.h"
#import "TSPrivacyPreferences.h"
2015-12-07 03:31:43 +01:00
#import "TSThread.h"
#import <25519/Randomness.h>
#import <SAMKeychain/SAMKeychain.h>
#import <YapDatabase/YapDatabaseRelationship.h>
2015-12-07 03:31:43 +01:00
NSString *const TSUIDatabaseConnectionDidUpdateNotification = @"TSUIDatabaseConnectionDidUpdateNotification";
NSString *const TSStorageManagerExceptionNameDatabasePasswordInaccessible = @"TSStorageManagerExceptionNameDatabasePasswordInaccessible";
NSString *const TSStorageManagerExceptionNameDatabasePasswordInaccessibleWhileBackgrounded = @"TSStorageManagerExceptionNameDatabasePasswordInaccessibleWhileBackgrounded";
NSString *const TSStorageManagerExceptionNameDatabasePasswordUnwritable = @"TSStorageManagerExceptionNameDatabasePasswordUnwritable";
NSString *const TSStorageManagerExceptionNameNoDatabase = @"TSStorageManagerExceptionNameNoDatabase";
2015-12-07 03:31:43 +01:00
static const NSString *const databaseName = @"Signal.sqlite";
static NSString *keychainService = @"TSKeyChainService";
static NSString *keychainDBPassAccount = @"TSDatabasePass";
@interface TSStorageManager ()
@property YapDatabase *database;
@end
#pragma mark -
// Some lingering TSRecipient records in the wild causing crashes.
// This is a stop gap until a proper cleanup happens.
@interface TSRecipient : NSObject <NSCoding>
@end
#pragma mark -
@interface OWSUnknownObject : NSObject <NSCoding>
@end
#pragma mark -
/**
* A default object to return when we can't deserialize an object from YapDB. This can prevent crashes when
* old objects linger after their definition file is removed. The danger is that, the objects can lay in wait
* until the next time a DB extension is added and we necessarily enumerate the entire DB.
*/
@implementation OWSUnknownObject
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
return nil;
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
}
@end
#pragma mark -
@interface OWSUnarchiverDelegate : NSObject <NSKeyedUnarchiverDelegate>
@end
#pragma mark -
@implementation OWSUnarchiverDelegate
- (nullable Class)unarchiver:(NSKeyedUnarchiver *)unarchiver cannotDecodeObjectOfClassName:(NSString *)name originalClasses:(NSArray<NSString *> *)classNames
{
DDLogError(@"[OWSUnarchiverDelegate] Ignoring unknown class name: %@. Was the class definition deleted?", name);
return [OWSUnknownObject class];
}
@end
#pragma mark -
2015-12-07 03:31:43 +01:00
@implementation TSStorageManager
+ (instancetype)sharedManager {
static TSStorageManager *sharedManager = nil;
2015-12-07 03:31:43 +01:00
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedManager = [[self alloc] initDefault];
2015-12-07 03:31:43 +01:00
#if TARGET_OS_IPHONE
[sharedManager protectSignalFiles];
2015-12-07 03:31:43 +01:00
#endif
});
return sharedManager;
2015-12-07 03:31:43 +01:00
}
- (instancetype)initDefault
{
2015-12-07 03:31:43 +01:00
self = [super init];
if (![self tryToLoadDatabase]) {
// Failing to load the database is catastrophic.
//
// The best we can try to do is to discard the current database
// and behave like a clean install.
OWSAnalyticsCritical(@"Could not load database");
// Try to reset app by deleting database.
[self resetSignalStorage];
if (![self tryToLoadDatabase]) {
OWSAnalyticsCritical(@"Could not load database (second attempt)");
[NSException raise:TSStorageManagerExceptionNameNoDatabase format:@"Failed to initialize database."];
}
}
return self;
}
- (BOOL)tryToLoadDatabase
{
// We determine the database password first, since a side effect of
// this can be deleting any existing database file (if we're recovering
// from a corrupt keychain).
NSData *databasePassword = [self databasePassword];
2015-12-07 03:31:43 +01:00
YapDatabaseOptions *options = [[YapDatabaseOptions alloc] init];
options.corruptAction = YapDatabaseCorruptAction_Fail;
options.cipherKeyBlock = ^{
return databasePassword;
2015-12-07 03:31:43 +01:00
};
_database = [[YapDatabase alloc] initWithPath:[self dbPath]
serializer:NULL
deserializer:[[self class] logOnFailureDeserializer]
options:options];
if (!_database) {
return NO;
}
2015-12-07 03:31:43 +01:00
_dbConnection = self.newDatabaseConnection;
return YES;
2015-12-07 03:31:43 +01:00
}
/**
* NSCoding sometimes throws exceptions killing our app. We want to log that exception.
**/
+ (YapDatabaseDeserializer)logOnFailureDeserializer
{
OWSUnarchiverDelegate *unarchiverDelegate = [OWSUnarchiverDelegate new];
return ^id(NSString __unused *collection, NSString __unused *key, NSData *data) {
if (!data || data.length <= 0) {
return nil;
}
@try {
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
unarchiver.delegate = unarchiverDelegate;
return [unarchiver decodeObjectForKey:@"root"];
} @catch (NSException *exception) {
// Sync log in case we bail.
DDLogError(@"%@ Unarchiving key:%@ from collection:%@ and data %@ failed with error: %@",
self.tag,
key,
collection,
data,
exception.reason);
DDLogError(@"%@ Raising exception.", self.tag);
@throw exception;
}
};
}
- (void)setupDatabase
{
// Register extensions which are essential for rendering threads synchronously
2015-12-07 03:31:43 +01:00
[TSDatabaseView registerThreadDatabaseView];
[TSDatabaseView registerBuddyConversationDatabaseView];
[TSDatabaseView registerUnreadDatabaseView];
[self.database registerExtension:[TSDatabaseSecondaryIndexes registerTimeStampIndex] withName:@"idx"];
// Register extensions which aren't essential for rendering threads async
[TSDatabaseView asyncRegisterSecondaryDevicesDatabaseView];
[OWSReadReceipt asyncRegisterIndexOnSenderIdAndTimestampWithDatabase:self.database];
OWSDisappearingMessagesFinder *finder = [[OWSDisappearingMessagesFinder alloc] initWithStorageManager:self];
[finder asyncRegisterDatabaseExtensions];
2015-12-07 03:31:43 +01:00
}
- (void)protectSignalFiles {
[self protectFolderAtPath:[TSAttachmentStream attachmentsFolder]];
[self protectFolderAtPath:[self dbPath]];
[self protectFolderAtPath:[[self dbPath] stringByAppendingString:@"-shm"]];
[self protectFolderAtPath:[[self dbPath] stringByAppendingString:@"-wal"]];
}
- (void)protectFolderAtPath:(NSString *)path {
if (![NSFileManager.defaultManager fileExistsAtPath:path]) {
return;
}
NSError *error;
NSDictionary *fileProtection = @{NSFileProtectionKey : NSFileProtectionCompleteUntilFirstUserAuthentication};
[[NSFileManager defaultManager] setAttributes:fileProtection ofItemAtPath:path error:&error];
NSDictionary *resourcesAttrs = @{ NSURLIsExcludedFromBackupKey : @YES };
NSURL *ressourceURL = [NSURL fileURLWithPath:path];
BOOL success = [ressourceURL setResourceValues:resourcesAttrs error:&error];
if (error || !success) {
DDLogError(@"Error while removing files from backup: %@", error.description);
return;
}
}
- (YapDatabaseConnection *)newDatabaseConnection {
return self.database.newConnection;
}
- (TSPrivacyPreferences *)privacyPreferences
{
return [TSPrivacyPreferences sharedInstance];
}
2015-12-07 03:31:43 +01:00
- (BOOL)userSetPassword {
return FALSE;
}
- (BOOL)dbExists {
return [[NSFileManager defaultManager] fileExistsAtPath:[self dbPath]];
}
- (NSString *)dbPath {
NSString *databasePath;
NSFileManager *fileManager = [NSFileManager defaultManager];
#if TARGET_OS_IPHONE
NSURL *fileURL = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSString *path = [fileURL path];
databasePath = [path stringByAppendingFormat:@"/%@", databaseName];
#elif TARGET_OS_MAC
NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
NSArray *urlPaths = [fileManager URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];
NSURL *appDirectory = [[urlPaths objectAtIndex:0] URLByAppendingPathComponent:bundleID isDirectory:YES];
if (![fileManager fileExistsAtPath:[appDirectory path]]) {
[fileManager createDirectoryAtURL:appDirectory withIntermediateDirectories:NO attributes:nil error:nil];
}
databasePath = [appDirectory.filePathURL.absoluteString stringByAppendingFormat:@"/%@", databaseName];
#endif
return databasePath;
}
- (BOOL)databasePasswordAccessible
{
[SAMKeychain setAccessibilityType:kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly];
2015-12-07 03:31:43 +01:00
NSError *error;
NSString *dbPassword = [SAMKeychain passwordForService:keychainService account:keychainDBPassAccount error:&error];
2015-12-07 03:31:43 +01:00
if (dbPassword && !error) {
return YES;
}
if (error) {
DDLogWarn(@"Database password couldn't be accessed: %@", error.localizedDescription);
}
return NO;
}
- (void)backgroundedAppDatabasePasswordInaccessibleWithError:(NSError *)error
{
OWSAssert([UIApplication sharedApplication].applicationState == UIApplicationStateBackground);
// Presumably this happened in response to a push notification. It's possible that the keychain is corrupted
// but it could also just be that the user hasn't yet unlocked their device since our password is
// kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
[NSException raise:TSStorageManagerExceptionNameDatabasePasswordInaccessibleWhileBackgrounded
format:@"Unable to access database password. No unlock since device restart? Error: %@", error];
}
- (NSData *)databasePassword
{
[SAMKeychain setAccessibilityType:kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly];
NSError *keyFetchError;
NSString *dbPassword =
[SAMKeychain passwordForService:keychainService account:keychainDBPassAccount error:&keyFetchError];
if (keyFetchError) {
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
// TODO: Rather than crash here, we should detect the situation earlier
// and exit gracefully - (in the app delegate?). See the `
// This is a last ditch effort to avoid blowing away the user's database.
[self backgroundedAppDatabasePasswordInaccessibleWithError:keyFetchError];
}
// At this point, either this is a new install so there's no existing password to retrieve
// or the keychain has become corrupt. Either way, we want to get back to a
// "known good state" and behave like a new install.
BOOL shouldHavePassword = [NSFileManager.defaultManager fileExistsAtPath:[self dbPath]];
if (shouldHavePassword) {
OWSAnalyticsCriticalWithParameters(@"Could not retrieve database password from keychain",
@{ @"ErrorCode" : @(keyFetchError.code) });
}
// Try to reset app by deleting database.
[self resetSignalStorage];
dbPassword = [self createAndSetNewDatabasePassword];
2015-12-07 03:31:43 +01:00
}
return [dbPassword dataUsingEncoding:NSUTF8StringEncoding];
}
- (NSString *)createAndSetNewDatabasePassword
{
NSString *newDBPassword = [[Randomness generateRandomBytes:30] base64EncodedString];
NSError *keySetError;
[SAMKeychain setPassword:newDBPassword forService:keychainService account:keychainDBPassAccount error:&keySetError];
if (keySetError) {
DDLogError(@"%@ Setting DB password failed with error: %@", self.tag, keySetError);
[self deletePasswordFromKeychain];
[NSException raise:TSStorageManagerExceptionNameDatabasePasswordUnwritable
format:@"Setting DB password failed with error: %@", keySetError];
} else {
DDLogError(@"Succesfully set new DB password.");
}
return newDBPassword;
}
#pragma mark - convenience methods
2015-12-07 03:31:43 +01:00
- (void)purgeCollection:(NSString *)collection {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[transaction removeAllObjectsInCollection:collection];
}];
}
- (void)setObject:(id)object forKey:(NSString *)key inCollection:(NSString *)collection {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[transaction setObject:object forKey:key inCollection:collection];
}];
}
- (void)removeObjectForKey:(NSString *)string inCollection:(NSString *)collection {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[transaction removeObjectForKey:string inCollection:collection];
}];
}
- (id)objectForKey:(NSString *)key inCollection:(NSString *)collection {
__block NSString *object;
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
object = [transaction objectForKey:key inCollection:collection];
}];
return object;
}
- (NSDictionary *)dictionaryForKey:(NSString *)key inCollection:(NSString *)collection {
__block NSDictionary *object;
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
object = [transaction objectForKey:key inCollection:collection];
}];
return object;
}
- (NSString *)stringForKey:(NSString *)key inCollection:(NSString *)collection {
NSString *string = [self objectForKey:key inCollection:collection];
return string;
}
- (BOOL)boolForKey:(NSString *)key inCollection:(NSString *)collection {
NSNumber *boolNum = [self objectForKey:key inCollection:collection];
return [boolNum boolValue];
}
- (NSData *)dataForKey:(NSString *)key inCollection:(NSString *)collection {
NSData *data = [self objectForKey:key inCollection:collection];
return data;
}
- (ECKeyPair *)keyPairForKey:(NSString *)key inCollection:(NSString *)collection {
ECKeyPair *keyPair = [self objectForKey:key inCollection:collection];
return keyPair;
}
- (PreKeyRecord *)preKeyRecordForKey:(NSString *)key inCollection:(NSString *)collection {
PreKeyRecord *preKeyRecord = [self objectForKey:key inCollection:collection];
return preKeyRecord;
}
- (SignedPreKeyRecord *)signedPreKeyRecordForKey:(NSString *)key inCollection:(NSString *)collection {
SignedPreKeyRecord *preKeyRecord = [self objectForKey:key inCollection:collection];
return preKeyRecord;
}
- (int)intForKey:(NSString *)key inCollection:(NSString *)collection {
int integer = [[self objectForKey:key inCollection:collection] intValue];
return integer;
}
- (void)setInt:(int)integer forKey:(NSString *)key inCollection:(NSString *)collection {
[self setObject:[NSNumber numberWithInt:integer] forKey:key inCollection:collection];
}
- (void)deleteThreadsAndMessages {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[transaction removeAllObjectsInCollection:[TSThread collection]];
[transaction removeAllObjectsInCollection:[SignalRecipient collection]];
[transaction removeAllObjectsInCollection:[TSInteraction collection]];
[transaction removeAllObjectsInCollection:[TSAttachment collection]];
}];
[TSAttachmentStream deleteAttachments];
}
- (void)deletePasswordFromKeychain
{
[SAMKeychain deletePasswordForService:keychainService account:keychainDBPassAccount];
}
2015-12-07 03:31:43 +01:00
- (void)deleteDatabaseFile
{
NSError *error;
[[NSFileManager defaultManager] removeItemAtPath:[self dbPath] error:&error];
2015-12-07 03:31:43 +01:00
if (error) {
DDLogError(@"Failed to delete database: %@", error.description);
}
}
2015-12-07 03:31:43 +01:00
- (void)resetSignalStorage
{
self.database = nil;
_dbConnection = nil;
[self deletePasswordFromKeychain];
2015-12-07 03:31:43 +01:00
[self deleteDatabaseFile];
[TSAttachmentStream deleteAttachments];
2015-12-07 03:31:43 +01:00
}
#pragma mark - Logging
+ (NSString *)tag
{
return [NSString stringWithFormat:@"[%@]", self.class];
}
- (NSString *)tag
{
return self.class.tag;
}
2015-12-07 03:31:43 +01:00
@end