Skip to content

Commit

Permalink
Fix broken keychain access with SSH on 10.6 (fixes #2268)
Browse files Browse the repository at this point in the history
(From the department of commits to be rolled back soon)
  • Loading branch information
dmoagx committed Oct 5, 2015
1 parent d4641ec commit 31de04a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
39 changes: 22 additions & 17 deletions Source/SPKeychain.m
Expand Up @@ -31,6 +31,7 @@

#import "SPKeychain.h"
#import "SPAlertSheets.h"
#import "SPOSInfo.h"

#import <Security/Security.h>
#import <CoreFoundation/CoreFoundation.h>
Expand Down Expand Up @@ -212,33 +213,38 @@ - (void)deletePasswordForName:(NSString *)name account:(NSString *)account
- (BOOL)passwordExistsForName:(NSString *)name account:(NSString *)account
{
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
NSMutableDictionary *query = [NSMutableDictionary dictionary];

[query setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
[query setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnAttributes];
[query setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];

[query setObject:account forKey:(id)kSecAttrAccount];
[query setObject:name forKey:(id)kSecAttrService];

CFDictionaryRef result = NULL;

return SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&result) == errSecSuccess;
#else
// "kSecClassGenericPassword" was introduced with the 10.7 SDK.
// It won't work on 10.6 either (meaning this code never matches properly there).
// (That's why there are compile time and runtime checks here)
if([SPOSInfo isOSVersionAtLeastMajor:10 minor:7 patch:0]) {
NSMutableDictionary *query = [NSMutableDictionary dictionary];

[query setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
[query setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnAttributes];
[query setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];

[query setObject:account forKey:(id)kSecAttrAccount];
[query setObject:name forKey:(id)kSecAttrService];

CFDictionaryRef result = NULL;

return SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&result) == errSecSuccess;
}
#endif
SecKeychainItemRef item;
SecKeychainSearchRef search = NULL;
NSInteger numberOfItemsFound = 0;
SecKeychainAttributeList list;
SecKeychainAttribute attributes[2];

// Check supplied variables and replaces nils with empty strings
if (!name) name = @"";
if (!account) account = @"";

attributes[0].tag = kSecAccountItemAttr;
attributes[0].data = (void *)[account UTF8String]; // Account name
attributes[0].length = (UInt32)strlen([account UTF8String]); // Length of account name (bytes)

attributes[1].tag = kSecServiceItemAttr;
attributes[1].data = (void *)[name UTF8String]; // Service name
attributes[1].length = (UInt32)strlen([name UTF8String]); // Length of service name (bytes)
Expand All @@ -257,7 +263,6 @@ - (BOOL)passwordExistsForName:(NSString *)name account:(NSString *)account
if (search) CFRelease(search);

return (numberOfItemsFound > 0);
#endif
}

/**
Expand Down
5 changes: 5 additions & 0 deletions Source/SPOSInfo.m
Expand Up @@ -30,6 +30,11 @@

#import "SPOSInfo.h"

// Needed because this class is also compiled with SequelProTunnelAssistant which can't access SPConstants.h
#ifndef __MAC_10_10
#define __MAC_10_10 101000
#endif

#if __MAC_OS_X_VERSION_MAX_ALLOWED < __MAC_10_10
// This code is available since 10.8 but public only since 10.10
typedef struct {
Expand Down
2 changes: 2 additions & 0 deletions sequel-pro.xcodeproj/project.pbxproj
Expand Up @@ -191,6 +191,7 @@
506CE9311A311C6C0039F736 /* SPTableContentFilterController.m in Sources */ = {isa = PBXBuildFile; fileRef = 506CE9301A311C6C0039F736 /* SPTableContentFilterController.m */; };
507FF1121BBCC57600104523 /* SPFunctions.m in Sources */ = {isa = PBXBuildFile; fileRef = 507FF1111BBCC57600104523 /* SPFunctions.m */; };
507FF1621BBF0D5000104523 /* SPTableCopyTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 112730551180788A000737FD /* SPTableCopyTest.m */; };
507FF2421BC33BBC00104523 /* SPOSInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 50EAB5B71A8FBB08008F627A /* SPOSInfo.m */; };
50A9F8B119EAD4B90053E571 /* SPGotoDatabaseController.m in Sources */ = {isa = PBXBuildFile; fileRef = 50A9F8B019EAD4B90053E571 /* SPGotoDatabaseController.m */; };
50D3C3491A75B8A800B5429C /* GotoDatabaseDialog.xib in Resources */ = {isa = PBXBuildFile; fileRef = 50D3C34B1A75B8A800B5429C /* GotoDatabaseDialog.xib */; };
50D3C3521A77135F00B5429C /* SPParserUtils.c in Sources */ = {isa = PBXBuildFile; fileRef = 50D3C3501A77135F00B5429C /* SPParserUtils.c */; };
Expand Down Expand Up @@ -3140,6 +3141,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
507FF2421BC33BBC00104523 /* SPOSInfo.m in Sources */,
586F457B0FDB269E00B428D7 /* RegexKitLite.m in Sources */,
58CDB3410FCE141900F8ACA3 /* SequelProTunnelAssistant.m in Sources */,
58CDB3420FCE142500F8ACA3 /* SPKeychain.m in Sources */,
Expand Down

0 comments on commit 31de04a

Please sign in to comment.