aboutsummaryrefslogtreecommitdiffstats
path: root/KeyChain.m
diff options
context:
space:
mode:
Diffstat (limited to 'KeyChain.m')
-rw-r--r--KeyChain.m86
1 files changed, 65 insertions, 21 deletions
diff --git a/KeyChain.m b/KeyChain.m
index 7e71c489..bb3f6315 100644
--- a/KeyChain.m
+++ b/KeyChain.m
@@ -23,42 +23,86 @@
// Or mail to <lorenz@textor.ch>
#import "KeyChain.h"
-
+#include <CoreFoundation/CoreFoundation.h>
+#include <Security/Security.h>
@implementation KeyChain
- (void)addPassword:(NSString *)password forName:(NSString *)name account:(NSString *)account
{
- int code;
- code = kcaddgenericpassword([name cString], [account cString],
- [password cStringLength], [password cString], NULL);
-
- if ( code != 0 )
- NSLog(@"Error while trying to add password for name: %@ account: %@", name, account);
+ OSStatus status;
+ status = SecKeychainAddGenericPassword(
+ NULL, // default keychain
+ [name cStringLength], // length of service name
+ [name cString], // service name
+ [account cStringLength], // length of account name
+ [account cString], // account name
+ [password cStringLength], // length of password
+ [password cString], // pointer to password data
+ NULL // the item reference
+ );
+
+ if ( status != noErr )
+ NSLog(@"Error (%i) while trying to add password for name: %@ account: %@", status, name, account);
}
- (NSString *)getPasswordForName:(NSString *)name account:(NSString *)account
{
- int code;
- UInt32 length;
- void *p = (void *)malloc(128 * sizeof(char));
- NSString *password = @"";
-
- code = kcfindgenericpassword([name cString], [account cString], 128, p, &length, nil);
+ OSStatus status;
+
+ void *passwordData = nil;
+ UInt32 passwordLength = nil;
+ SecKeychainItemRef itemRef = nil;
+ NSString *password = @"";
+
+ status = SecKeychainFindGenericPassword (
+ NULL, // default keychain
+ [name cStringLength], // length of service name
+ [name cString], // service name
+ [account cStringLength], // length of account name
+ [account cString], // account name
+ &passwordLength, // length of password
+ &passwordData, // pointer to password data
+ &itemRef // the item reference
+ );
+
+ if ( status == noErr ) {
+ password = [NSString stringWithCString:passwordData length:passwordLength];
+
+ //Free the data allocated by SecKeychainFindGenericPassword:
+ status = SecKeychainItemFreeContent (
+ NULL, //No attribute data to release
+ passwordData //Release data
+ );
+ }
- if (!code)
- password = [NSString stringWithCString:(const char*)p length:length];
- free(p);
-
- return password;
+ return password;
}
- (void)deletePasswordForName:(NSString *)name account:(NSString *)account
{
- KCItemRef itemref = nil ;
+ OSStatus status;
+ SecKeychainItemRef itemRef = nil;
+
+ status = SecKeychainFindGenericPassword (
+ NULL, // default keychain
+ [name cStringLength], // length of service name
+ [name cString], // service name
+ [account cStringLength], // length of account name
+ [account cString], // account name
+ nil, // length of password
+ nil, // pointer to password data
+ &itemRef // the item reference
+ );
- kcfindgenericpassword([name cString],[account cString],nil,nil,nil,&itemref);
- KCDeleteItem(itemref);
+// if ( status != noErr )
+ NSLog(@"Error (%i) while trying to find password for name: %@ account: %@", status, name, account);
+
+ status = SecKeychainItemDelete(itemRef);
+// if ( status != noErr )
+ NSLog(@"Error (%i) while trying to delete password for name: %@ account: %@", status, name, account);
+
+ CFRelease(itemRef);
}
@end