UICKeyChainStore.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // UICKeyChainStore.h
  3. // UICKeyChainStore
  4. //
  5. // Created by Kishikawa Katsumi on 11/11/20.
  6. // Copyright (c) 2011 Kishikawa Katsumi. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #if !__has_feature(nullability)
  10. #define NS_ASSUME_NONNULL_BEGIN
  11. #define NS_ASSUME_NONNULL_END
  12. #define nullable
  13. #define nonnull
  14. #define null_unspecified
  15. #define null_resettable
  16. #define __nullable
  17. #define __nonnull
  18. #define __null_unspecified
  19. #endif
  20. #if __has_extension(objc_generics)
  21. #define UIC_KEY_TYPE <NSString *>
  22. #define UIC_CREDENTIAL_TYPE <NSDictionary <NSString *, NSString *>*>
  23. #else
  24. #define UIC_KEY_TYPE
  25. #define UIC_CREDENTIAL_TYPE
  26. #endif
  27. NS_ASSUME_NONNULL_BEGIN
  28. extern NSString * const UICKeyChainStoreErrorDomain;
  29. typedef NS_ENUM(NSInteger, UICKeyChainStoreErrorCode) {
  30. UICKeyChainStoreErrorInvalidArguments = 1,
  31. };
  32. typedef NS_ENUM(NSInteger, UICKeyChainStoreItemClass) {
  33. UICKeyChainStoreItemClassGenericPassword = 1,
  34. UICKeyChainStoreItemClassInternetPassword,
  35. };
  36. typedef NS_ENUM(NSInteger, UICKeyChainStoreProtocolType) {
  37. UICKeyChainStoreProtocolTypeFTP = 1,
  38. UICKeyChainStoreProtocolTypeFTPAccount,
  39. UICKeyChainStoreProtocolTypeHTTP,
  40. UICKeyChainStoreProtocolTypeIRC,
  41. UICKeyChainStoreProtocolTypeNNTP,
  42. UICKeyChainStoreProtocolTypePOP3,
  43. UICKeyChainStoreProtocolTypeSMTP,
  44. UICKeyChainStoreProtocolTypeSOCKS,
  45. UICKeyChainStoreProtocolTypeIMAP,
  46. UICKeyChainStoreProtocolTypeLDAP,
  47. UICKeyChainStoreProtocolTypeAppleTalk,
  48. UICKeyChainStoreProtocolTypeAFP,
  49. UICKeyChainStoreProtocolTypeTelnet,
  50. UICKeyChainStoreProtocolTypeSSH,
  51. UICKeyChainStoreProtocolTypeFTPS,
  52. UICKeyChainStoreProtocolTypeHTTPS,
  53. UICKeyChainStoreProtocolTypeHTTPProxy,
  54. UICKeyChainStoreProtocolTypeHTTPSProxy,
  55. UICKeyChainStoreProtocolTypeFTPProxy,
  56. UICKeyChainStoreProtocolTypeSMB,
  57. UICKeyChainStoreProtocolTypeRTSP,
  58. UICKeyChainStoreProtocolTypeRTSPProxy,
  59. UICKeyChainStoreProtocolTypeDAAP,
  60. UICKeyChainStoreProtocolTypeEPPC,
  61. UICKeyChainStoreProtocolTypeNNTPS,
  62. UICKeyChainStoreProtocolTypeLDAPS,
  63. UICKeyChainStoreProtocolTypeTelnetS,
  64. UICKeyChainStoreProtocolTypeIRCS,
  65. UICKeyChainStoreProtocolTypePOP3S,
  66. };
  67. typedef NS_ENUM(NSInteger, UICKeyChainStoreAuthenticationType) {
  68. UICKeyChainStoreAuthenticationTypeNTLM = 1,
  69. UICKeyChainStoreAuthenticationTypeMSN,
  70. UICKeyChainStoreAuthenticationTypeDPA,
  71. UICKeyChainStoreAuthenticationTypeRPA,
  72. UICKeyChainStoreAuthenticationTypeHTTPBasic,
  73. UICKeyChainStoreAuthenticationTypeHTTPDigest,
  74. UICKeyChainStoreAuthenticationTypeHTMLForm,
  75. UICKeyChainStoreAuthenticationTypeDefault,
  76. };
  77. typedef NS_ENUM(NSInteger, UICKeyChainStoreAccessibility) {
  78. UICKeyChainStoreAccessibilityWhenUnlocked = 1,
  79. UICKeyChainStoreAccessibilityAfterFirstUnlock,
  80. UICKeyChainStoreAccessibilityAlways,
  81. UICKeyChainStoreAccessibilityWhenPasscodeSetThisDeviceOnly
  82. __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0),
  83. UICKeyChainStoreAccessibilityWhenUnlockedThisDeviceOnly,
  84. UICKeyChainStoreAccessibilityAfterFirstUnlockThisDeviceOnly,
  85. UICKeyChainStoreAccessibilityAlwaysThisDeviceOnly,
  86. }
  87. __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_4_0);
  88. typedef NS_ENUM(unsigned long, UICKeyChainStoreAuthenticationPolicy) {
  89. UICKeyChainStoreAuthenticationPolicyUserPresence = 1 << 0,
  90. UICKeyChainStoreAuthenticationPolicyTouchIDAny NS_ENUM_AVAILABLE(10_12_1, 9_0) = 1u << 1,
  91. UICKeyChainStoreAuthenticationPolicyTouchIDCurrentSet NS_ENUM_AVAILABLE(10_12_1, 9_0) = 1u << 3,
  92. UICKeyChainStoreAuthenticationPolicyDevicePasscode NS_ENUM_AVAILABLE(10_11, 9_0) = 1u << 4,
  93. UICKeyChainStoreAuthenticationPolicyControlOr NS_ENUM_AVAILABLE(10_12_1, 9_0) = 1u << 14,
  94. UICKeyChainStoreAuthenticationPolicyControlAnd NS_ENUM_AVAILABLE(10_12_1, 9_0) = 1u << 15,
  95. UICKeyChainStoreAuthenticationPolicyPrivateKeyUsage NS_ENUM_AVAILABLE(10_12_1, 9_0) = 1u << 30,
  96. UICKeyChainStoreAuthenticationPolicyApplicationPassword NS_ENUM_AVAILABLE(10_12_1, 9_0) = 1u << 31,
  97. }__OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0);
  98. @interface UICKeyChainStore : NSObject
  99. @property (nonatomic, readonly) UICKeyChainStoreItemClass itemClass;
  100. @property (nonatomic, readonly, nullable) NSString *service;
  101. @property (nonatomic, readonly, nullable) NSString *accessGroup;
  102. @property (nonatomic, readonly, nullable) NSURL *server;
  103. @property (nonatomic, readonly) UICKeyChainStoreProtocolType protocolType;
  104. @property (nonatomic, readonly) UICKeyChainStoreAuthenticationType authenticationType;
  105. @property (nonatomic) UICKeyChainStoreAccessibility accessibility;
  106. @property (nonatomic, readonly) UICKeyChainStoreAuthenticationPolicy authenticationPolicy
  107. __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0);
  108. @property (nonatomic) BOOL useAuthenticationUI;
  109. @property (nonatomic) BOOL synchronizable;
  110. @property (nonatomic, nullable) NSString *authenticationPrompt
  111. __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);
  112. @property (nonatomic, readonly, nullable) NSArray UIC_KEY_TYPE *allKeys;
  113. @property (nonatomic, readonly, nullable) NSArray *allItems;
  114. + (NSString *)defaultService;
  115. + (void)setDefaultService:(NSString *)defaultService;
  116. + (UICKeyChainStore *)keyChainStore;
  117. + (UICKeyChainStore *)keyChainStoreWithService:(nullable NSString *)service;
  118. + (UICKeyChainStore *)keyChainStoreWithService:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup;
  119. + (UICKeyChainStore *)keyChainStoreWithServer:(NSURL *)server protocolType:(UICKeyChainStoreProtocolType)protocolType;
  120. + (UICKeyChainStore *)keyChainStoreWithServer:(NSURL *)server protocolType:(UICKeyChainStoreProtocolType)protocolType authenticationType:(UICKeyChainStoreAuthenticationType)authenticationType;
  121. - (instancetype)init;
  122. - (instancetype)initWithService:(nullable NSString *)service;
  123. - (instancetype)initWithService:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup;
  124. - (instancetype)initWithServer:(NSURL *)server protocolType:(UICKeyChainStoreProtocolType)protocolType;
  125. - (instancetype)initWithServer:(NSURL *)server protocolType:(UICKeyChainStoreProtocolType)protocolType authenticationType:(UICKeyChainStoreAuthenticationType)authenticationType;
  126. + (nullable NSString *)stringForKey:(NSString *)key;
  127. + (nullable NSString *)stringForKey:(NSString *)key service:(nullable NSString *)service;
  128. + (nullable NSString *)stringForKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup;
  129. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key;
  130. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key service:(nullable NSString *)service;
  131. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup;
  132. + (nullable NSData *)dataForKey:(NSString *)key;
  133. + (nullable NSData *)dataForKey:(NSString *)key service:(nullable NSString *)service;
  134. + (nullable NSData *)dataForKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup;
  135. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key;
  136. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key service:(nullable NSString *)service;
  137. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup;
  138. - (BOOL)contains:(nullable NSString *)key;
  139. - (BOOL)setString:(nullable NSString *)string forKey:(nullable NSString *)key;
  140. - (BOOL)setString:(nullable NSString *)string forKey:(nullable NSString *)key label:(nullable NSString *)label comment:(nullable NSString *)comment;
  141. - (nullable NSString *)stringForKey:(NSString *)key;
  142. - (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key;
  143. - (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key label:(nullable NSString *)label comment:(nullable NSString *)comment;
  144. - (nullable NSData *)dataForKey:(NSString *)key;
  145. + (BOOL)removeItemForKey:(NSString *)key;
  146. + (BOOL)removeItemForKey:(NSString *)key service:(nullable NSString *)service;
  147. + (BOOL)removeItemForKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup;
  148. + (BOOL)removeAllItems;
  149. + (BOOL)removeAllItemsForService:(nullable NSString *)service;
  150. + (BOOL)removeAllItemsForService:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup;
  151. - (BOOL)removeItemForKey:(NSString *)key;
  152. - (BOOL)removeAllItems;
  153. - (nullable NSString *)objectForKeyedSubscript:(NSString<NSCopying> *)key;
  154. - (void)setObject:(nullable NSString *)obj forKeyedSubscript:(NSString<NSCopying> *)key;
  155. + (nullable NSArray UIC_KEY_TYPE *)allKeysWithItemClass:(UICKeyChainStoreItemClass)itemClass;
  156. - (nullable NSArray UIC_KEY_TYPE *)allKeys;
  157. + (nullable NSArray *)allItemsWithItemClass:(UICKeyChainStoreItemClass)itemClass;
  158. - (nullable NSArray *)allItems;
  159. - (void)setAccessibility:(UICKeyChainStoreAccessibility)accessibility authenticationPolicy:(UICKeyChainStoreAuthenticationPolicy)authenticationPolicy
  160. __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0);
  161. #if TARGET_OS_IOS
  162. - (void)sharedPasswordWithCompletion:(nullable void (^)(NSString * __nullable account, NSString * __nullable password, NSError * __nullable error))completion;
  163. - (void)sharedPasswordForAccount:(NSString *)account completion:(nullable void (^)(NSString * __nullable password, NSError * __nullable error))completion;
  164. - (void)setSharedPassword:(nullable NSString *)password forAccount:(NSString *)account completion:(nullable void (^)(NSError * __nullable error))completion;
  165. - (void)removeSharedPasswordForAccount:(NSString *)account completion:(nullable void (^)(NSError * __nullable error))completion;
  166. + (void)requestSharedWebCredentialWithCompletion:(nullable void (^)(NSArray UIC_CREDENTIAL_TYPE *credentials, NSError * __nullable error))completion;
  167. + (void)requestSharedWebCredentialForDomain:(nullable NSString *)domain account:(nullable NSString *)account completion:(nullable void (^)(NSArray UIC_CREDENTIAL_TYPE *credentials, NSError * __nullable error))completion;
  168. + (NSString *)generatePassword;
  169. #endif
  170. @end
  171. @interface UICKeyChainStore (ErrorHandling)
  172. + (nullable NSString *)stringForKey:(NSString *)key error:(NSError * __nullable __autoreleasing * __nullable)error;
  173. + (nullable NSString *)stringForKey:(NSString *)key service:(nullable NSString *)service error:(NSError * __nullable __autoreleasing * __nullable)error;
  174. + (nullable NSString *)stringForKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup error:(NSError * __nullable __autoreleasing * __nullable)error;
  175. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key error:(NSError * __nullable __autoreleasing * __nullable)error;
  176. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key service:(nullable NSString *)service error:(NSError * __nullable __autoreleasing * __nullable)error;
  177. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup error:(NSError * __nullable __autoreleasing * __nullable)error;
  178. + (nullable NSData *)dataForKey:(NSString *)key error:(NSError * __nullable __autoreleasing * __nullable)error;
  179. + (nullable NSData *)dataForKey:(NSString *)key service:(nullable NSString *)service error:(NSError * __nullable __autoreleasing * __nullable)error;
  180. + (nullable NSData *)dataForKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup error:(NSError * __nullable __autoreleasing * __nullable)error;
  181. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key error:(NSError * __nullable __autoreleasing * __nullable)error;
  182. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key service:(nullable NSString *)service error:(NSError * __nullable __autoreleasing * __nullable)error;
  183. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup error:(NSError * __nullable __autoreleasing * __nullable)error;
  184. - (BOOL)setString:(nullable NSString *)string forKey:(NSString * )key error:(NSError * __nullable __autoreleasing * __nullable)error;
  185. - (BOOL)setString:(nullable NSString *)string forKey:(NSString * )key label:(nullable NSString *)label comment:(nullable NSString *)comment error:(NSError * __nullable __autoreleasing * __nullable)error;
  186. - (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key error:(NSError * __nullable __autoreleasing * __nullable)error;
  187. - (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key label:(nullable NSString *)label comment:(nullable NSString *)comment error:(NSError * __nullable __autoreleasing * __nullable)error;
  188. - (nullable NSString *)stringForKey:(NSString *)key error:(NSError * __nullable __autoreleasing * __nullable)error;
  189. - (nullable NSData *)dataForKey:(NSString *)key error:(NSError * __nullable __autoreleasing * __nullable)error;
  190. + (BOOL)removeItemForKey:(NSString *)key error:(NSError * __nullable __autoreleasing * __nullable)error;
  191. + (BOOL)removeItemForKey:(NSString *)key service:(nullable NSString *)service error:(NSError * __nullable __autoreleasing * __nullable)error;
  192. + (BOOL)removeItemForKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup error:(NSError * __nullable __autoreleasing * __nullable)error;
  193. + (BOOL)removeAllItemsWithError:(NSError * __nullable __autoreleasing * __nullable)error;
  194. + (BOOL)removeAllItemsForService:(nullable NSString *)service error:(NSError * __nullable __autoreleasing * __nullable)error;
  195. + (BOOL)removeAllItemsForService:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup error:(NSError * __nullable __autoreleasing * __nullable)error;
  196. - (BOOL)removeItemForKey:(NSString *)key error:(NSError * __nullable __autoreleasing * __nullable)error;
  197. - (BOOL)removeAllItemsWithError:(NSError * __nullable __autoreleasing * __nullable)error;
  198. @end
  199. @interface UICKeyChainStore (ForwardCompatibility)
  200. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key genericAttribute:(nullable id)genericAttribute;
  201. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key genericAttribute:(nullable id)genericAttribute error:(NSError * __nullable __autoreleasing * __nullable)error;
  202. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key service:(nullable NSString *)service genericAttribute:(nullable id)genericAttribute;
  203. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key service:(nullable NSString *)service genericAttribute:(nullable id)genericAttribute error:(NSError * __nullable __autoreleasing * __nullable)error;
  204. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup genericAttribute:(nullable id)genericAttribute;
  205. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup genericAttribute:(nullable id)genericAttribute error:(NSError * __nullable __autoreleasing * __nullable)error;
  206. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key genericAttribute:(nullable id)genericAttribute;
  207. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key genericAttribute:(nullable id)genericAttribute error:(NSError * __nullable __autoreleasing * __nullable)error;
  208. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key service:(nullable NSString *)service genericAttribute:(nullable id)genericAttribute;
  209. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key service:(nullable NSString *)service genericAttribute:(nullable id)genericAttribute error:(NSError * __nullable __autoreleasing * __nullable)error;
  210. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup genericAttribute:(nullable id)genericAttribute;
  211. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup genericAttribute:(nullable id)genericAttribute error:(NSError * __nullable __autoreleasing * __nullable)error;
  212. - (BOOL)setString:(nullable NSString *)string forKey:(NSString *)key genericAttribute:(nullable id)genericAttribute;
  213. - (BOOL)setString:(nullable NSString *)string forKey:(NSString *)key genericAttribute:(nullable id)genericAttribute error:(NSError * __nullable __autoreleasing * __nullable)error;
  214. - (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key genericAttribute:(nullable id)genericAttribute;
  215. - (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key genericAttribute:(nullable id)genericAttribute error:(NSError * __nullable __autoreleasing * __nullable)error;
  216. @end
  217. @interface UICKeyChainStore (Deprecation)
  218. - (void)synchronize __attribute__((deprecated("calling this method is no longer required")));
  219. - (BOOL)synchronizeWithError:(NSError * __nullable __autoreleasing * __nullable)error __attribute__((deprecated("calling this method is no longer required")));
  220. @end
  221. NS_ASSUME_NONNULL_END