-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYKeychainQuery.m
363 lines (304 loc) · 10.5 KB
/
SSYKeychainQuery.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#import "SSYKeychainQuery.h"
#import "SSYKeychain.h" // for error domain and error codes
@implementation SSYKeychainQuery
@synthesize account = _account ;
@synthesize service = _service ;
@synthesize label = _label ;
@synthesize server = _server ;
@synthesize passwordData = _passwordData ;
#if __IPHONE_3_0 && TARGET_OS_IPHONE
@synthesize accessGroup = _accessGroup ;
#endif
#ifdef ICLOUD_SYNCHRONIZATION_AVAILABLE
@synthesize synchronizationMode = _synchronizationMode ;
#endif
#pragma mark - Public
- (id)init {
self = [super init] ;
if (self) {
// Default class value for all queries
[self setItemClass:(__bridge id)kSecClassGenericPassword] ;
}
return self ;
}
#if !__has_feature(objc_arc)
- (void)dealloc {
[_account release] ;
[_service release] ;
[_label release] ;
[_server release] ;
[_itemClass release] ;
#if __IPHONE_3_0 && TARGET_OS_IPHONE
[_accessGroup release] ;
#endif
[_passwordData release] ;
[super dealloc] ;
}
#endif
- (NSError*)errorForServiceServerAccount {
NSInteger errorCode = 0 ;
if (![self account]) {
errorCode = 197010 ;
}
else if ([[self itemClass] isEqualToString:(NSString*)kSecClassInternetPassword]) {
if (![self server]) {
errorCode = 197011 ;
}
}
else if (![self service]) {
errorCode = 197012 ;
}
return [[self class] errorWithCode:errorCode] ;
}
- (BOOL)save:(NSError *__autoreleasing *)error_p {
BOOL ok = YES ;
NSError* error = [self errorForServiceServerAccount] ;
if (error) {
ok = NO ;
}
if (ok) {
[self deleteItem:nil] ;
NSMutableDictionary *query = [self query] ;
[query setObject:[self passwordData] forKey:(__bridge id)kSecValueData] ;
if ([self label]) {
[query setObject:[self label] forKey:(__bridge id)kSecAttrLabel] ;
}
if ([self server]) {
[query setObject:[self server] forKey:(__bridge id)kSecAttrServer] ;
}
#if __IPHONE_4_0 && TARGET_OS_IPHONE
CFTypeRef accessibilityType = [SSYKeychain accessibilityType] ;
if (accessibilityType) {
[query setObject:(__bridge id)accessibilityType forKey:(__bridge id)kSecAttrAccessible] ;
}
#endif
OSStatus status = SecItemAdd((
__bridge CFDictionaryRef)query,
NULL) ;
if (status != errSecSuccess && error != NULL) {
error = [[self class] errorWithCode:status] ;
}
ok = (status == errSecSuccess) ;
}
if (error_p && error) {
*error_p = error ;
}
return ok ;
}
- (BOOL)deleteItem:(NSError *__autoreleasing *)error_p {
BOOL ok = YES ;
NSError* error = [self errorForServiceServerAccount] ;
if (error) {
ok = NO ;
}
if (ok) {
NSMutableDictionary *query = [self query] ;
#if TARGET_OS_IPHONE
status = SecItemDelete((__bridge CFDictionaryRef)query) ;
if (status != errSecSuccess) {
error = [[self class] errorWithCode:status] ;
}
#else
CFTypeRef result = NULL ;
[query setObject:@YES forKey:(__bridge id)kSecReturnRef] ;
OSStatus status = SecItemCopyMatching((
__bridge CFDictionaryRef)query,
&result) ;
if (status == errSecSuccess) {
status = SecKeychainItemDelete((SecKeychainItemRef)result) ;
CFRelease(result) ;
if (status != errSecSuccess) {
error = [[self class] errorWithCode:status] ;
}
}
else if (status != errSecItemNotFound) {
error = [[self class] errorWithCode:status] ;
}
#endif
ok = ((status == errSecSuccess) || (status == errSecItemNotFound)) ;
}
if (error_p && error) {
*error_p = error ;
}
return ok ;
}
- (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] ;
CFTypeRef result = NULL ;
OSStatus status = SecItemCopyMatching(
(__bridge CFDictionaryRef)query,
&result) ;
if (status != errSecSuccess && error != NULL) {
*error = [[self class] errorWithCode:status] ;
}
#if !__has_feature(objc_arc)
return [(NSArray*)result autorelease] ;
#else
return (__bridge_transfer NSArray*)result ;
#endif
}
- (BOOL)fetch:(NSError *__autoreleasing *)error_p {
BOOL ok = YES ;
NSError* error = [self errorForServiceServerAccount] ;
if (error) {
ok = NO ;
}
CFTypeRef result = NULL ;
if (ok) {
NSMutableDictionary *query = [self query] ;
[query setObject:@YES
forKey:(__bridge id)kSecReturnData] ;
[query setObject:(__bridge id)kSecMatchLimitOne
forKey:(__bridge id)kSecMatchLimit] ;
OSStatus status = SecItemCopyMatching(
(__bridge CFDictionaryRef)query,
&result) ;
ok = (status == errSecSuccess) ;
if (!ok) {
error = [[self class] errorWithCode:status] ;
}
}
#if !__has_feature(objc_arc)
[self setPasswordData:result] ;
[(NSData*)result autorelease] ;
#else
[self setPasswordData:(__bridge_transfer NSData*)result] ;
#endif
if (error_p && error) {
*error_p = error ;
}
return ok ;
}
#pragma mark - Accessors
- (void)setPassword:(NSString *)password {
[self setPasswordData:[password dataUsingEncoding:NSUTF8StringEncoding]] ;
}
- (NSString *)password {
if ([[self passwordData] length] > 0) {
NSString* word = [[NSString alloc] initWithData:[self passwordData] encoding:NSUTF8StringEncoding] ;
#if !__has_feature(objc_arc)
[word autorelease] ;
#endif
return word ;
}
return nil ;
}
#pragma mark - Synchronization Status
#ifdef ICLOUD_SYNCHRONIZATION_AVAILABLE
+ (BOOL)isSynchronizationAvailable {
#if TARGET_OS_IPHONE
// Apple suggested way to check for 7.0 at runtime
// https://developer.apple.com/library/ios/documentation/userexperience/conceptual/transitionguide/SupportingEarlieriOS.html
return floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1 ;
#else
return floor(NSFoundationVersionNumber) > NSFoundationVersionNumber10_8_4 ;
#endif
}
#endif
#pragma mark - Private
- (NSMutableDictionary *)query {
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:3] ;
[dictionary setObject:[self itemClass]
forKey:(__bridge id)kSecClass] ;
if ([self service]) {
[dictionary setObject:[self service] forKey:(__bridge id)kSecAttrService] ;
}
if ([self account]) {
[dictionary setObject:[self account] forKey:(__bridge id)kSecAttrAccount] ;
}
if ([self server]) {
[dictionary setObject:[self server] forKey:(__bridge id)kSecAttrServer] ;
}
#if __IPHONE_3_0 && TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR
if ([self accessGroup]) {
[dictionary setObject:[self accessGroup] forKey:(__bridge id)kSecAttrAccessGroup] ;
}
#endif
#ifdef ICLOUD_SYNCHRONIZATION_AVAILABLE
if ([[self class] isSynchronizationAvailable]) {
id value ;
switch ([self synchronizationMode]) {
case SSYKeychainQuerySynchronizationModeNo: {
value = @NO ;
break ;
}
case SSYKeychainQuerySynchronizationModeYes: {
value = @YES ;
break ;
}
case SSYKeychainQuerySynchronizationModeAny: {
value = (__bridge id)(kSecAttrSynchronizableAny) ;
break ;
}
}
[dictionary setObject:value forKey:(__bridge id)(kSecAttrSynchronizable)] ;
}
#endif
return dictionary ;
}
+ (NSError *)errorWithCode:(NSInteger)code {
NSString *message = nil ;
switch (code) {
case errSecSuccess: return nil ;
case SSYKeychainErrorBadArguments: message = NSLocalizedStringFromTable(@"SSYKeychainErrorBadArguments", @"SSYKeychain", nil) ; break ;
#if TARGET_OS_IPHONE
case errSecUnimplemented: {
message = NSLocalizedStringFromTable(@"errSecUnimplemented", @"SSYKeychain", nil) ;
break ;
}
case errSecParam: {
message = NSLocalizedStringFromTable(@"errSecParam", @"SSYKeychain", nil) ;
break ;
}
case errSecAllocate: {
message = NSLocalizedStringFromTable(@"errSecAllocate", @"SSYKeychain", nil) ;
break ;
}
case errSecNotAvailable: {
message = NSLocalizedStringFromTable(@"errSecNotAvailable", @"SSYKeychain", nil) ;
break ;
}
case errSecDuplicateItem: {
message = NSLocalizedStringFromTable(@"errSecDuplicateItem", @"SSYKeychain", nil) ;
break ;
}
case errSecItemNotFound: {
message = NSLocalizedStringFromTable(@"errSecItemNotFound", @"SSYKeychain", nil) ;
break ;
}
case errSecInteractionNotAllowed: {
message = NSLocalizedStringFromTable(@"errSecInteractionNotAllowed", @"SSYKeychain", nil) ;
break ;
}
case errSecDecode: {
message = NSLocalizedStringFromTable(@"errSecDecode", @"SSYKeychain", nil) ;
break ;
}
case errSecAuthFailed: {
message = NSLocalizedStringFromTable(@"errSecAuthFailed", @"SSYKeychain", nil) ;
break ;
}
default: {
message = NSLocalizedStringFromTable(@"errSecDefault", @"SSYKeychain", nil) ;
}
#else
default:
#if !__has_feature(objc_arc)
message = (NSString*)SecCopyErrorMessageString((OSStatus)code, NULL) ;
[message autorelease] ;
#else
message = (__bridge_transfer NSString *)SecCopyErrorMessageString((int)code, NULL) ;
#endif
#endif
}
NSDictionary *userInfo = nil ;
if (message) {
userInfo = @{ NSLocalizedDescriptionKey : message };
}
return [NSError errorWithDomain:kSSYKeychainErrorDomain code:code userInfo:userInfo] ;
}
@end