UICKeyChainStore.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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(NSInteger, UICKeyChainStoreAuthenticationPolicy) {
  89. UICKeyChainStoreAuthenticationPolicyUserPresence = kSecAccessControlUserPresence,
  90. };
  91. @interface UICKeyChainStore : NSObject
  92. @property (nonatomic, readonly) UICKeyChainStoreItemClass itemClass;
  93. @property (nonatomic, readonly, nullable) NSString *service;
  94. @property (nonatomic, readonly, nullable) NSString *accessGroup;
  95. @property (nonatomic, readonly, nullable) NSURL *server;
  96. @property (nonatomic, readonly) UICKeyChainStoreProtocolType protocolType;
  97. @property (nonatomic, readonly) UICKeyChainStoreAuthenticationType authenticationType;
  98. @property (nonatomic) UICKeyChainStoreAccessibility accessibility;
  99. @property (nonatomic, readonly) UICKeyChainStoreAuthenticationPolicy authenticationPolicy
  100. __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0);
  101. @property (nonatomic) BOOL synchronizable;
  102. @property (nonatomic, nullable) NSString *authenticationPrompt
  103. __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);
  104. @property (nonatomic, readonly, nullable) NSArray UIC_KEY_TYPE *allKeys;
  105. @property (nonatomic, readonly, nullable) NSArray *allItems;
  106. + (NSString *)defaultService;
  107. + (void)setDefaultService:(NSString *)defaultService;
  108. + (UICKeyChainStore *)keyChainStore;
  109. + (UICKeyChainStore *)keyChainStoreWithService:(nullable NSString *)service;
  110. + (UICKeyChainStore *)keyChainStoreWithService:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup;
  111. + (UICKeyChainStore *)keyChainStoreWithServer:(NSURL *)server protocolType:(UICKeyChainStoreProtocolType)protocolType;
  112. + (UICKeyChainStore *)keyChainStoreWithServer:(NSURL *)server protocolType:(UICKeyChainStoreProtocolType)protocolType authenticationType:(UICKeyChainStoreAuthenticationType)authenticationType;
  113. - (instancetype)init;
  114. - (instancetype)initWithService:(nullable NSString *)service;
  115. - (instancetype)initWithService:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup;
  116. - (instancetype)initWithServer:(NSURL *)server protocolType:(UICKeyChainStoreProtocolType)protocolType;
  117. - (instancetype)initWithServer:(NSURL *)server protocolType:(UICKeyChainStoreProtocolType)protocolType authenticationType:(UICKeyChainStoreAuthenticationType)authenticationType;
  118. + (nullable NSString *)stringForKey:(NSString *)key;
  119. + (nullable NSString *)stringForKey:(NSString *)key service:(nullable NSString *)service;
  120. + (nullable NSString *)stringForKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup;
  121. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key;
  122. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key service:(nullable NSString *)service;
  123. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup;
  124. + (nullable NSData *)dataForKey:(NSString *)key;
  125. + (nullable NSData *)dataForKey:(NSString *)key service:(nullable NSString *)service;
  126. + (nullable NSData *)dataForKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup;
  127. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key;
  128. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key service:(nullable NSString *)service;
  129. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup;
  130. - (BOOL)contains:(nullable NSString *)key;
  131. - (BOOL)setString:(nullable NSString *)string forKey:(nullable NSString *)key;
  132. - (BOOL)setString:(nullable NSString *)string forKey:(nullable NSString *)key label:(nullable NSString *)label comment:(nullable NSString *)comment;
  133. - (nullable NSString *)stringForKey:(NSString *)key;
  134. - (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key;
  135. - (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key label:(nullable NSString *)label comment:(nullable NSString *)comment;
  136. - (nullable NSData *)dataForKey:(NSString *)key;
  137. + (BOOL)removeItemForKey:(NSString *)key;
  138. + (BOOL)removeItemForKey:(NSString *)key service:(nullable NSString *)service;
  139. + (BOOL)removeItemForKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup;
  140. + (BOOL)removeAllItems;
  141. + (BOOL)removeAllItemsForService:(nullable NSString *)service;
  142. + (BOOL)removeAllItemsForService:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup;
  143. - (BOOL)removeItemForKey:(NSString *)key;
  144. - (BOOL)removeAllItems;
  145. - (nullable NSString *)objectForKeyedSubscript:(NSString<NSCopying> *)key;
  146. - (void)setObject:(nullable NSString *)obj forKeyedSubscript:(NSString<NSCopying> *)key;
  147. + (nullable NSArray UIC_KEY_TYPE *)allKeysWithItemClass:(UICKeyChainStoreItemClass)itemClass;
  148. - (nullable NSArray UIC_KEY_TYPE *)allKeys;
  149. + (nullable NSArray *)allItemsWithItemClass:(UICKeyChainStoreItemClass)itemClass;
  150. - (nullable NSArray *)allItems;
  151. - (void)setAccessibility:(UICKeyChainStoreAccessibility)accessibility authenticationPolicy:(UICKeyChainStoreAuthenticationPolicy)authenticationPolicy
  152. __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0);
  153. #if TARGET_OS_IOS
  154. - (void)sharedPasswordWithCompletion:(nullable void (^)(NSString * __nullable account, NSString * __nullable password, NSError * __nullable error))completion;
  155. - (void)sharedPasswordForAccount:(NSString *)account completion:(nullable void (^)(NSString * __nullable password, NSError * __nullable error))completion;
  156. - (void)setSharedPassword:(nullable NSString *)password forAccount:(NSString *)account completion:(nullable void (^)(NSError * __nullable error))completion;
  157. - (void)removeSharedPasswordForAccount:(NSString *)account completion:(nullable void (^)(NSError * __nullable error))completion;
  158. + (void)requestSharedWebCredentialWithCompletion:(nullable void (^)(NSArray UIC_CREDENTIAL_TYPE *credentials, NSError * __nullable error))completion;
  159. + (void)requestSharedWebCredentialForDomain:(nullable NSString *)domain account:(nullable NSString *)account completion:(nullable void (^)(NSArray UIC_CREDENTIAL_TYPE *credentials, NSError * __nullable error))completion;
  160. + (NSString *)generatePassword;
  161. #endif
  162. @end
  163. @interface UICKeyChainStore (ErrorHandling)
  164. + (nullable NSString *)stringForKey:(NSString *)key error:(NSError * __nullable __autoreleasing * __nullable)error;
  165. + (nullable NSString *)stringForKey:(NSString *)key service:(nullable NSString *)service error:(NSError * __nullable __autoreleasing * __nullable)error;
  166. + (nullable NSString *)stringForKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup error:(NSError * __nullable __autoreleasing * __nullable)error;
  167. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key error:(NSError * __nullable __autoreleasing * __nullable)error;
  168. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key service:(nullable NSString *)service error:(NSError * __nullable __autoreleasing * __nullable)error;
  169. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup error:(NSError * __nullable __autoreleasing * __nullable)error;
  170. + (nullable NSData *)dataForKey:(NSString *)key error:(NSError * __nullable __autoreleasing * __nullable)error;
  171. + (nullable NSData *)dataForKey:(NSString *)key service:(nullable NSString *)service error:(NSError * __nullable __autoreleasing * __nullable)error;
  172. + (nullable NSData *)dataForKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup error:(NSError * __nullable __autoreleasing * __nullable)error;
  173. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key error:(NSError * __nullable __autoreleasing * __nullable)error;
  174. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key service:(nullable NSString *)service error:(NSError * __nullable __autoreleasing * __nullable)error;
  175. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup error:(NSError * __nullable __autoreleasing * __nullable)error;
  176. - (BOOL)setString:(nullable NSString *)string forKey:(NSString * )key error:(NSError * __nullable __autoreleasing * __nullable)error;
  177. - (BOOL)setString:(nullable NSString *)string forKey:(NSString * )key label:(nullable NSString *)label comment:(nullable NSString *)comment error:(NSError * __nullable __autoreleasing * __nullable)error;
  178. - (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key error:(NSError * __nullable __autoreleasing * __nullable)error;
  179. - (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key label:(nullable NSString *)label comment:(nullable NSString *)comment error:(NSError * __nullable __autoreleasing * __nullable)error;
  180. - (nullable NSString *)stringForKey:(NSString *)key error:(NSError * __nullable __autoreleasing * __nullable)error;
  181. - (nullable NSData *)dataForKey:(NSString *)key error:(NSError * __nullable __autoreleasing * __nullable)error;
  182. + (BOOL)removeItemForKey:(NSString *)key error:(NSError * __nullable __autoreleasing * __nullable)error;
  183. + (BOOL)removeItemForKey:(NSString *)key service:(nullable NSString *)service error:(NSError * __nullable __autoreleasing * __nullable)error;
  184. + (BOOL)removeItemForKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup error:(NSError * __nullable __autoreleasing * __nullable)error;
  185. + (BOOL)removeAllItemsWithError:(NSError * __nullable __autoreleasing * __nullable)error;
  186. + (BOOL)removeAllItemsForService:(nullable NSString *)service error:(NSError * __nullable __autoreleasing * __nullable)error;
  187. + (BOOL)removeAllItemsForService:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup error:(NSError * __nullable __autoreleasing * __nullable)error;
  188. - (BOOL)removeItemForKey:(NSString *)key error:(NSError * __nullable __autoreleasing * __nullable)error;
  189. - (BOOL)removeAllItemsWithError:(NSError * __nullable __autoreleasing * __nullable)error;
  190. @end
  191. @interface UICKeyChainStore (ForwardCompatibility)
  192. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key genericAttribute:(nullable id)genericAttribute;
  193. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key genericAttribute:(nullable id)genericAttribute error:(NSError * __nullable __autoreleasing * __nullable)error;
  194. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key service:(nullable NSString *)service genericAttribute:(nullable id)genericAttribute;
  195. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key service:(nullable NSString *)service genericAttribute:(nullable id)genericAttribute error:(NSError * __nullable __autoreleasing * __nullable)error;
  196. + (BOOL)setString:(nullable NSString *)value forKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup genericAttribute:(nullable id)genericAttribute;
  197. + (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;
  198. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key genericAttribute:(nullable id)genericAttribute;
  199. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key genericAttribute:(nullable id)genericAttribute error:(NSError * __nullable __autoreleasing * __nullable)error;
  200. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key service:(nullable NSString *)service genericAttribute:(nullable id)genericAttribute;
  201. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key service:(nullable NSString *)service genericAttribute:(nullable id)genericAttribute error:(NSError * __nullable __autoreleasing * __nullable)error;
  202. + (BOOL)setData:(nullable NSData *)data forKey:(NSString *)key service:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup genericAttribute:(nullable id)genericAttribute;
  203. + (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;
  204. - (BOOL)setString:(nullable NSString *)string forKey:(NSString *)key genericAttribute:(nullable id)genericAttribute;
  205. - (BOOL)setString:(nullable NSString *)string forKey:(NSString *)key 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. @end
  209. @interface UICKeyChainStore (Deprecation)
  210. - (void)synchronize __attribute__((deprecated("calling this method is no longer required")));
  211. - (BOOL)synchronizeWithError:(NSError * __nullable __autoreleasing * __nullable)error __attribute__((deprecated("calling this method is no longer required")));
  212. @end
  213. NS_ASSUME_NONNULL_END