31
31
32
32
#import " SPKeychain.h"
33
33
#import " SPAlertSheets.h"
34
+ #import " SPOSInfo.h"
34
35
35
36
#import < Security/Security.h>
36
37
#import < CoreFoundation/CoreFoundation.h>
@@ -212,33 +213,38 @@ - (void)deletePasswordForName:(NSString *)name account:(NSString *)account
212
213
- (BOOL )passwordExistsForName : (NSString *)name account : (NSString *)account
213
214
{
214
215
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
215
- NSMutableDictionary *query = [NSMutableDictionary dictionary ];
216
-
217
- [query setObject: (id )kSecClassGenericPassword forKey: (id )kSecClass ];
218
- [query setObject: (id )kCFBooleanTrue forKey: (id )kSecReturnAttributes ];
219
- [query setObject: (id )kSecMatchLimitOne forKey: (id )kSecMatchLimit ];
220
-
221
- [query setObject: account forKey: (id )kSecAttrAccount ];
222
- [query setObject: name forKey: (id )kSecAttrService ];
223
-
224
- CFDictionaryRef result = NULL ;
225
-
226
- return SecItemCopyMatching ((CFDictionaryRef )query, (CFTypeRef *)&result) == errSecSuccess;
227
- #else
216
+ // "kSecClassGenericPassword" was introduced with the 10.7 SDK.
217
+ // It won't work on 10.6 either (meaning this code never matches properly there).
218
+ // (That's why there are compile time and runtime checks here)
219
+ if ([SPOSInfo isOSVersionAtLeastMajor: 10 minor: 7 patch: 0 ]) {
220
+ NSMutableDictionary *query = [NSMutableDictionary dictionary ];
221
+
222
+ [query setObject: (id )kSecClassGenericPassword forKey: (id )kSecClass ];
223
+ [query setObject: (id )kCFBooleanTrue forKey: (id )kSecReturnAttributes ];
224
+ [query setObject: (id )kSecMatchLimitOne forKey: (id )kSecMatchLimit ];
225
+
226
+ [query setObject: account forKey: (id )kSecAttrAccount ];
227
+ [query setObject: name forKey: (id )kSecAttrService ];
228
+
229
+ CFDictionaryRef result = NULL ;
230
+
231
+ return SecItemCopyMatching ((CFDictionaryRef )query, (CFTypeRef *)&result) == errSecSuccess;
232
+ }
233
+ #endif
228
234
SecKeychainItemRef item;
229
235
SecKeychainSearchRef search = NULL ;
230
236
NSInteger numberOfItemsFound = 0 ;
231
237
SecKeychainAttributeList list;
232
238
SecKeychainAttribute attributes[2 ];
233
-
239
+
234
240
// Check supplied variables and replaces nils with empty strings
235
241
if (!name) name = @" " ;
236
242
if (!account) account = @" " ;
237
-
243
+
238
244
attributes[0 ].tag = kSecAccountItemAttr ;
239
245
attributes[0 ].data = (void *)[account UTF8String ]; // Account name
240
246
attributes[0 ].length = (UInt32 )strlen ([account UTF8String ]); // Length of account name (bytes)
241
-
247
+
242
248
attributes[1 ].tag = kSecServiceItemAttr ;
243
249
attributes[1 ].data = (void *)[name UTF8String ]; // Service name
244
250
attributes[1 ].length = (UInt32 )strlen ([name UTF8String ]); // Length of service name (bytes)
@@ -257,7 +263,6 @@ - (BOOL)passwordExistsForName:(NSString *)name account:(NSString *)account
257
263
if (search) CFRelease (search);
258
264
259
265
return (numberOfItemsFound > 0 );
260
- #endif
261
266
}
262
267
263
268
/* *
0 commit comments