From 6463db41106b4648433362d0eb128b7ddff9d1d6 Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 1 Aug 2019 14:27:51 +0400 Subject: [PATCH 1/2] fix accessibility for iPad, implement storing current accessibilty type --- Sources/SAMKeychain.h | 2 +- Sources/SAMKeychain.m | 4 ++-- Sources/SAMKeychainQuery.h | 24 ++++++++++++++++-------- Sources/SAMKeychainQuery.m | 19 +++++++++++++------ 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/Sources/SAMKeychain.h b/Sources/SAMKeychain.h index 2af305d..07f266f 100644 --- a/Sources/SAMKeychain.h +++ b/Sources/SAMKeychain.h @@ -167,7 +167,7 @@ extern NSString *const kSAMKeychainWhereKey; #pragma mark - Configuration -#if __IPHONE_4_0 && TARGET_OS_IPHONE +#ifdef SSKEYCHAIN_ACCESSIBLE_AVAILABLE /** Returns the accessibility type for all future passwords saved to the Keychain. diff --git a/Sources/SAMKeychain.m b/Sources/SAMKeychain.m index 6d01d3d..2245e17 100644 --- a/Sources/SAMKeychain.m +++ b/Sources/SAMKeychain.m @@ -18,7 +18,7 @@ NSString *const kSAMKeychainLastModifiedKey = @"mdat"; NSString *const kSAMKeychainWhereKey = @"svce"; -#if __IPHONE_4_0 && TARGET_OS_IPHONE +#ifdef SSKEYCHAIN_ACCESSIBLE_AVAILABLE static CFTypeRef SAMKeychainAccessibilityType = NULL; #endif @@ -112,7 +112,7 @@ + (nullable NSArray *)accountsForService:(nullable NSString *)serviceName error: } -#if __IPHONE_4_0 && TARGET_OS_IPHONE +#ifdef SSKEYCHAIN_ACCESSIBLE_AVAILABLE + (CFTypeRef)accessibilityType { return SAMKeychainAccessibilityType; } diff --git a/Sources/SAMKeychainQuery.h b/Sources/SAMKeychainQuery.h index 84658e2..48ddb43 100644 --- a/Sources/SAMKeychainQuery.h +++ b/Sources/SAMKeychainQuery.h @@ -16,15 +16,13 @@ NS_ASSUME_NONNULL_BEGIN -#if __IPHONE_7_0 || __MAC_10_9 - // Keychain synchronization available at compile time - #define SAMKEYCHAIN_SYNCHRONIZATION_AVAILABLE 1 -#endif +#define SSKEYCHAIN_ACCESSIBLE_AVAILABLE 1 -#if __IPHONE_3_0 || __MAC_10_9 - // Keychain access group available at compile time - #define SAMKEYCHAIN_ACCESS_GROUP_AVAILABLE 1 -#endif +// Keychain synchronization available at compile time +#define SAMKEYCHAIN_SYNCHRONIZATION_AVAILABLE 1 + +// Keychain access group available at compile time +#define SAMKEYCHAIN_ACCESS_GROUP_AVAILABLE 1 #ifdef SAMKEYCHAIN_SYNCHRONIZATION_AVAILABLE typedef NS_ENUM(NSUInteger, SAMKeychainQuerySynchronizationMode) { @@ -48,6 +46,16 @@ typedef NS_ENUM(NSUInteger, SAMKeychainQuerySynchronizationMode) { /** kSecAttrLabel */ @property (nonatomic, copy, nullable) NSString *label; +/** kSecAttrComment **/ +@property (nonatomic, copy) NSString *comment; + +#ifdef SSKEYCHAIN_ACCESSIBLE_AVAILABLE +/** + kSecAttrAccessible + Sets the accessibility type for an individual item. If set, this overrides +[SSKeychain accessibilityType]. + */ +@property (nonatomic, copy) __attribute__((NSObject)) CFTypeRef accessibilityType; +#endif #ifdef SAMKEYCHAIN_ACCESS_GROUP_AVAILABLE /** kSecAttrAccessGroup (only used on iOS) */ @property (nonatomic, copy, nullable) NSString *accessGroup; diff --git a/Sources/SAMKeychainQuery.m b/Sources/SAMKeychainQuery.m index 00ecb80..b37ad24 100644 --- a/Sources/SAMKeychainQuery.m +++ b/Sources/SAMKeychainQuery.m @@ -40,8 +40,13 @@ - (BOOL)save:(NSError *__autoreleasing *)error { if (status == errSecSuccess) {//item already exists, update it! query = [[NSMutableDictionary alloc]init]; [query setObject:self.passwordData forKey:(__bridge id)kSecValueData]; -#if __IPHONE_4_0 && TARGET_OS_IPHONE - CFTypeRef accessibilityType = [SAMKeychain accessibilityType]; + + if (self.comment) { + [query setObject:self.comment forKey:(__bridge id)kSecAttrComment]; + } +#ifdef SSKEYCHAIN_ACCESSIBLE_AVAILABLE + CFTypeRef accessibilityType = self.accessibilityType ?: [SSKeychain accessibilityType]; + if (accessibilityType) { [query setObject:(__bridge id)accessibilityType forKey:(__bridge id)kSecAttrAccessible]; } @@ -53,8 +58,8 @@ - (BOOL)save:(NSError *__autoreleasing *)error { [query setObject:self.label forKey:(__bridge id)kSecAttrLabel]; } [query setObject:self.passwordData forKey:(__bridge id)kSecValueData]; -#if __IPHONE_4_0 && TARGET_OS_IPHONE - CFTypeRef accessibilityType = [SAMKeychain accessibilityType]; +#if SSKEYCHAIN_ACCESSIBLE_AVAILABLE + CFTypeRef accessibilityType = self.accessibilityType ?: [SSKeychain accessibilityType]; if (accessibilityType) { [query setObject:(__bridge id)accessibilityType forKey:(__bridge id)kSecAttrAccessible]; } @@ -110,8 +115,10 @@ - (nullable NSArray *)fetchAll:(NSError *__autoreleasing *)error { NSMutableDictionary *query = [self query]; [query setObject:@YES forKey:(__bridge id)kSecReturnAttributes]; [query setObject:(__bridge id)kSecMatchLimitAll forKey:(__bridge id)kSecMatchLimit]; -#if __IPHONE_4_0 && TARGET_OS_IPHONE - CFTypeRef accessibilityType = [SAMKeychain accessibilityType]; + +#if SSKEYCHAIN_ACCESSIBLE_AVAILABLE + CFTypeRef accessibilityType = self.accessibilityType ?: [SSKeychain accessibilityType]; + if (accessibilityType) { [query setObject:(__bridge id)accessibilityType forKey:(__bridge id)kSecAttrAccessible]; } From 2268633d2ea86b19446c89013cf6711f7da9d957 Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 1 Aug 2019 15:46:15 +0400 Subject: [PATCH 2/2] fix compile problems --- Sources/SAMKeychain.h | 3 ++- Sources/SAMKeychainQuery.h | 2 +- Sources/SAMKeychainQuery.m | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Sources/SAMKeychain.h b/Sources/SAMKeychain.h index 07f266f..ed7c609 100644 --- a/Sources/SAMKeychain.h +++ b/Sources/SAMKeychain.h @@ -12,6 +12,8 @@ #import #endif +#import + NS_ASSUME_NONNULL_BEGIN /** @@ -200,4 +202,3 @@ extern NSString *const kSAMKeychainWhereKey; NS_ASSUME_NONNULL_END -#import diff --git a/Sources/SAMKeychainQuery.h b/Sources/SAMKeychainQuery.h index 48ddb43..3e4bc7b 100644 --- a/Sources/SAMKeychainQuery.h +++ b/Sources/SAMKeychainQuery.h @@ -52,7 +52,7 @@ typedef NS_ENUM(NSUInteger, SAMKeychainQuerySynchronizationMode) { #ifdef SSKEYCHAIN_ACCESSIBLE_AVAILABLE /** kSecAttrAccessible - Sets the accessibility type for an individual item. If set, this overrides +[SSKeychain accessibilityType]. + Sets the accessibility type for an individual item. If set, this overrides +[SAMKeychain accessibilityType]. */ @property (nonatomic, copy) __attribute__((NSObject)) CFTypeRef accessibilityType; #endif diff --git a/Sources/SAMKeychainQuery.m b/Sources/SAMKeychainQuery.m index b37ad24..b3281ce 100644 --- a/Sources/SAMKeychainQuery.m +++ b/Sources/SAMKeychainQuery.m @@ -45,7 +45,7 @@ - (BOOL)save:(NSError *__autoreleasing *)error { [query setObject:self.comment forKey:(__bridge id)kSecAttrComment]; } #ifdef SSKEYCHAIN_ACCESSIBLE_AVAILABLE - CFTypeRef accessibilityType = self.accessibilityType ?: [SSKeychain accessibilityType]; + CFTypeRef accessibilityType = self.accessibilityType ?: [SAMKeychain accessibilityType]; if (accessibilityType) { [query setObject:(__bridge id)accessibilityType forKey:(__bridge id)kSecAttrAccessible]; @@ -59,7 +59,7 @@ - (BOOL)save:(NSError *__autoreleasing *)error { } [query setObject:self.passwordData forKey:(__bridge id)kSecValueData]; #if SSKEYCHAIN_ACCESSIBLE_AVAILABLE - CFTypeRef accessibilityType = self.accessibilityType ?: [SSKeychain accessibilityType]; + CFTypeRef accessibilityType = self.accessibilityType ?: [SAMKeychain accessibilityType]; if (accessibilityType) { [query setObject:(__bridge id)accessibilityType forKey:(__bridge id)kSecAttrAccessible]; } @@ -117,7 +117,7 @@ - (nullable NSArray *)fetchAll:(NSError *__autoreleasing *)error { [query setObject:(__bridge id)kSecMatchLimitAll forKey:(__bridge id)kSecMatchLimit]; #if SSKEYCHAIN_ACCESSIBLE_AVAILABLE - CFTypeRef accessibilityType = self.accessibilityType ?: [SSKeychain accessibilityType]; + CFTypeRef accessibilityType = self.accessibilityType ?: [SAMKeychain accessibilityType]; if (accessibilityType) { [query setObject:(__bridge id)accessibilityType forKey:(__bridge id)kSecAttrAccessible];