From 49326013fb844099d6f7a872a0f7d54a3214b179 Mon Sep 17 00:00:00 2001 From: Michael Starke Date: Thu, 1 Sep 2022 23:45:03 +0200 Subject: [PATCH] Refactored naming. Added better handling of settings for touchID --- MacPass/MPPasswordInputController.m | 4 ++-- MacPass/MPSettingsHelper.h | 3 ++- MacPass/MPSettingsHelper.m | 3 ++- MacPass/MPTouchIdCompositeKeyStore.m | 28 ++++++++++++++++++++++++++-- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/MacPass/MPPasswordInputController.m b/MacPass/MPPasswordInputController.m index a5aee9fb..a7529605 100644 --- a/MacPass/MPPasswordInputController.m +++ b/MacPass/MPPasswordInputController.m @@ -108,11 +108,11 @@ self.touchIdModeButton.menu = touchIDMenu; [self.touchIdModeButton bind:NSSelectedTagBinding toObject:NSUserDefaultsController.sharedUserDefaultsController - withKeyPath:[MPSettingsHelper defaultControllerPathForKey:kMPSettingsKeyEntryTouchIdEnabled] + withKeyPath:[MPSettingsHelper defaultControllerPathForKey:kMPSettingsKeyTouchIdEnabled] options:nil]; [self.touchIdEnabledButton bind:NSValueBinding toObject:NSUserDefaultsController.sharedUserDefaultsController - withKeyPath:[MPSettingsHelper defaultControllerPathForKey:kMPSettingsKeyEntryTouchIdEnabled] + withKeyPath:[MPSettingsHelper defaultControllerPathForKey:kMPSettingsKeyTouchIdEnabled] options:nil]; self.touchIdEnabledButton.hidden = YES; if (@available(macOS 10.13.4, *)) { diff --git a/MacPass/MPSettingsHelper.h b/MacPass/MPSettingsHelper.h index 3b850c2c..ce637658 100644 --- a/MacPass/MPSettingsHelper.h +++ b/MacPass/MPSettingsHelper.h @@ -23,7 +23,8 @@ #import /* TouchID */ -APPKIT_EXTERN NSString *const kMPSettingsKeyEntryTouchIdEnabled; +APPKIT_EXTERN NSString *const kMPSettingsKeyTouchIdEnabled; +APPKIT_EXTERN NSString *const kMPSettingsKeyTouchIdEncryptedKeyStore; // NSDictionary with hased file names mapped to keys APPKIT_EXTERN NSString *const kMPSettingsKeyEntryTouchIdDatabaseEncryptedKeyFormat; /* Clipboard */ diff --git a/MacPass/MPSettingsHelper.m b/MacPass/MPSettingsHelper.m index 5ac355b8..b6a7e172 100644 --- a/MacPass/MPSettingsHelper.m +++ b/MacPass/MPSettingsHelper.m @@ -67,7 +67,8 @@ NSString *const kMPSettingsKeyAutotypeMatchHost = @"Au NSString *const kMPSettingsKeyAutotypeMatchTags = @"AutotypeMatchTags"; NSString *const kMPSettingsKeyGloablAutotypeAlwaysShowCandidateSelection = @"GloablAutotypeAlwaysShowCandidateSelection"; -NSString *const kMPSettingsKeyEntryTouchIdEnabled = @"EnableSubsequentUnlocksWithTouchID"; +NSString *const kMPSettingsKeyTouchIdEnabled = @"EnableSubsequentUnlocksWithTouchID"; +NSString *const kMPSettingsKeyTouchIdEncryptedKeyStore = @"TouchIdEncryptedKeyStore"; NSString *const kMPSettingsKeyEntryTouchIdDatabaseEncryptedKeyFormat = @"EncryptedDatabaseKeyForTouchID-%@"; NSString *const kMPSettingsKeyEntrySearchFilterContext = @"EntrySearchFilterContext"; diff --git a/MacPass/MPTouchIdCompositeKeyStore.m b/MacPass/MPTouchIdCompositeKeyStore.m index cc90562f..678179f0 100644 --- a/MacPass/MPTouchIdCompositeKeyStore.m +++ b/MacPass/MPTouchIdCompositeKeyStore.m @@ -13,6 +13,7 @@ @interface MPTouchIdCompositeKeyStore () @property (readonly, strong) NSMutableDictionary* keys; +@property (nonatomic) NSInteger touchIdEnabledState; @end @implementation MPTouchIdCompositeKeyStore @@ -30,10 +31,31 @@ self = [super init]; if(self) { _keys = [[NSMutableDictionary alloc] init]; + [self bind:NSStringFromSelector(@selector(touchIdEnabledState)) + toObject:NSUserDefaultsController.sharedUserDefaultsController + withKeyPath:[MPSettingsHelper defaultControllerPathForKey:kMPSettingsKeyTouchIdEnabled] + options:nil]; } return self; } +- (void)setTouchIdEnabledState:(NSInteger)touchIdEnabledState { + switch(touchIdEnabledState) { + case NSControlStateValueMixed: + // clear persistent store + [NSUserDefaults.standardUserDefaults removeObjectForKey:kMPSettingsKeyTouchIdEncryptedKeyStore]; + break; + case NSControlStateValueOn: + // clear transient store + [self.keys removeAllObjects]; + break; + default: + // clear persitent and transient store + [NSUserDefaults.standardUserDefaults removeObjectForKey:kMPSettingsKeyTouchIdEncryptedKeyStore]; + [self.keys removeAllObjects]; + } +} + - (void)saveCompositeKey:(KPKCompositeKey *)compositeKey forDocumentKey:(NSString *)documentKey { NSError *error; NSData *encryptedCompositeKey = [self encryptedDataForCompositeKey:compositeKey error:&error]; @@ -42,7 +64,9 @@ return; } - NSInteger touchIdMode = [NSUserDefaults.standardUserDefaults integerForKey:kMPSettingsKeyEntryTouchIdEnabled]; + /* FIXME this behavour is wrong. Old keys do not get cleared so this leaves a lot of data behind that should be cleaned up*/ + + NSInteger touchIdMode = [NSUserDefaults.standardUserDefaults integerForKey:kMPSettingsKeyTouchIdEnabled]; switch(touchIdMode) { case NSControlStateValueMixed: [NSUserDefaults.standardUserDefaults removeObjectForKey:documentKey]; @@ -62,7 +86,7 @@ } } - (NSData *)loadEncryptedCompositeKeyForDocumentKey:(NSString *)documentKey { - NSInteger touchIdMode = [NSUserDefaults.standardUserDefaults integerForKey:kMPSettingsKeyEntryTouchIdEnabled]; + NSInteger touchIdMode = [NSUserDefaults.standardUserDefaults integerForKey:kMPSettingsKeyTouchIdEnabled]; NSData* transientKey = self.keys[documentKey]; NSData* persistentKey =[NSUserDefaults.standardUserDefaults dataForKey:documentKey]; if(nil == transientKey && nil == persistentKey) {