/*Copyright (c) 2008 Extendmac, LLC. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #import "EMKeychainProxy.h" #import "NGAUtilities.h" @implementation EMKeychainProxy #pragma mark - #pragma mark Shared Singleton SINGLETON_IMPLEMENTATION(EMKeychainProxy, sharedProxy) #pragma mark - #pragma mark Accessors - (BOOL)logsErrors { return _logErrors; } - (void)setLogsErrors:(BOOL)flag { _logErrors = flag; } #pragma mark - #pragma mark Getting Keychain Items - (EMGenericKeychainItem *)genericKeychainItemForService:(NSString *)serviceNameString withUsername:(NSString *)usernameString { if (!usernameString || [usernameString length] == 0) return nil; const char *serviceName = [serviceNameString UTF8String]; const char *username = [usernameString UTF8String]; UInt32 passwordLength = 0; char *password = nil; SecKeychainItemRef item = nil; OSStatus returnStatus = 0; @synchronized(self) { returnStatus = SecKeychainFindGenericPassword(NULL, (UInt32)strlen(serviceName), serviceName, (UInt32)strlen(username), username, &passwordLength, (void **)&password, &item); } if (returnStatus != noErr || !item) { if (_logErrors) { NSLog(@"Error (%@) - %s", NSStringFromSelector(_cmd), GetMacOSStatusErrorString(returnStatus)); } return nil; } NSString *passwordString = [[[NSString alloc] initWithBytes:password length:passwordLength encoding:NSUTF8StringEncoding] autorelease]; SecKeychainItemFreeContent(NULL, password); return [EMGenericKeychainItem genericKeychainItem:item forServiceName:serviceNameString username:usernameString password:passwordString]; } - (EMInternetKeychainItem *)internetKeychainItemForServer:(NSString *)serverString withUsername:(NSString *)usernameString path:(NSString *)pathString port:(NSInteger)port protocol:(SecProtocolType)protocol { if (!usernameString || [usernameString length] == 0 || !serverString || [serverString length] == 0) return nil; const char *server = [serverString UTF8String]; const char *username = [usernameString UTF8String]; const char *path = [pathString UTF8String]; if (!pathString || [pathString length] == 0) { path = ""; } UInt32 passwordLength = 0; char *password = nil; SecKeychainItemRef item = nil; //0 is kSecAuthenticationTypeAny OSStatus returnStatus = 0; @synchronized(self) { returnStatus = SecKeychainFindInternetPassword(NULL, (UInt32)strlen(server), server, 0, NULL, (UInt32)strlen(username), username, (UInt32)strlen(path), path, port, protocol, 0, &passwordLength, (void **)&password, &item); } if (returnStatus != noErr && protocol == kSecProtocolTypeFTP) { //Some clients (like Transmit) still save passwords with kSecProtocolTypeFTPAccount, which was deprecated. Let's check for that. protocol = kSecProtocolTypeFTPAccount; @synchronized(self) { returnStatus = SecKeychainFindInternetPassword(NULL, (UInt32)strlen(server), server, 0, NULL, (UInt32)strlen(username), username, (UInt32)strlen(path), path, port, protocol, 0, &passwordLength, (void **)&password, &item); } } if (returnStatus != noErr || !item) { if (_logErrors) { NSLog(@"Error (%@) - %s", NSStringFromSelector(_cmd), GetMacOSStatusErrorString(returnStatus)); } return nil; } NSString *passwordString = [[[NSString alloc] initWithBytes:password length:passwordLength encoding:NSUTF8StringEncoding] autorelease]; SecKeychainItemFreeContent(NULL, password); return [EMInternetKeychainItem internetKeychainItem:item forServer:serverString username:usernameString password:passwordString path:pathString port:port protocol:protocol]; } #pragma mark - #pragma mark Saving Passwords - (EMGenericKeychainItem *)addGenericKeychainItemForService:(NSString *)serviceNameString withUsername:(NSString *)usernameString password:(NSString *)passwordString { if (!usernameString || [usernameString length] == 0 || !serviceNameString || [serviceNameString length] == 0) return nil; const char *serviceName = [serviceNameString UTF8String]; const char *username = [usernameString UTF8String]; const char *password = [passwordString UTF8String]; SecKeychainItemRef item = nil; OSStatus returnStatus = 0; @synchronized(self) { returnStatus = SecKeychainAddGenericPassword(NULL, (UInt32)strlen(serviceName), serviceName, (UInt32)strlen(username), username, (UInt32)strlen(password), (void *)password, &item); } if (returnStatus != noErr || !item) { NSLog(@"Error (%@) - %s", NSStringFromSelector(_cmd), GetMacOSStatusErrorString(returnStatus)); return nil; } return [EMGenericKeychainItem genericKeychainItem:item forServiceName:serviceNameString username:usernameString password:passwordString]; } - (EMInternetKeychainItem *)addInternetKeychainItemForServer:(NSString *)serverString withUsername:(NSString *)usernameString password:(NSString *)passwordString path:(NSString *)pathString port:(NSInteger)port protocol:(SecProtocolType)protocol { if (!usernameString || [usernameString length] == 0 || !serverString || [serverString length] == 0 || !passwordString || [passwordString length] == 0) return nil; const char *server = [serverString UTF8String]; const char *username = [usernameString UTF8String]; const char *password = [passwordString UTF8String]; const char *path = [pathString UTF8String]; if (!pathString || [pathString length] == 0) { path = ""; } SecKeychainItemRef item = nil; OSStatus returnStatus = 0; @synchronized(self) { returnStatus = SecKeychainAddInternetPassword(NULL, (UInt32)strlen(server), server, 0, NULL, (UInt32)strlen(username), username, (UInt32)strlen(path), path, port, protocol, kSecAuthenticationTypeDefault, (UInt32)strlen(password), (void *)password, &item); } if (returnStatus != noErr || !item) { NSLog(@"Error (%@) - %s", NSStringFromSelector(_cmd), GetMacOSStatusErrorString(returnStatus)); return nil; } return [EMInternetKeychainItem internetKeychainItem:item forServer:serverString username:usernameString password:passwordString path:pathString port:port protocol:protocol]; } #pragma mark - #pragma mark Misc - (void)lockKeychain { @synchronized(self) { SecKeychainLock(NULL); } } - (void)unlockKeychain { @synchronized(self) { SecKeychainUnlock(NULL, 0, NULL, NO); } } @end