SecKeychainAddIToolsPassword.c [plain text]
#include <Security/SecKeychain.h>
#include <Security/SecKeychainItem.h>
#include <Security/SecAccess.h>
#include <Security/SecAccessPriv.h>
#include <Security/SecTrustedApplication.h>
#include <Security/SecACL.h>
#include <CoreFoundation/CoreFoundation.h>
OSStatus SecKeychainAddIToolsPassword(SecKeychainRef keychain, UInt32 accountNameLength, const char *accountName,
UInt32 passwordLength, const void *passwordData, SecKeychainItemRef *itemRef)
{
OSStatus err;
SecKeychainItemRef item = nil;
const char *serviceUTF8 = "iTools";
CFStringRef itemLabel = nil;
const int allowAny = 0;
itemLabel = CFStringCreateWithBytes(kCFAllocatorDefault, accountName, accountNameLength, kCFStringEncodingUTF8, FALSE);
SecAccessRef access = NULL;
err = SecAccessCreateWithTrustedApplications(CFSTR("/System/Library/Frameworks/Security.framework/Resources/iToolsTrustedApps.plist"), itemLabel, allowAny, &access);
SecKeychainAttribute attrs[] =
{
{ kSecLabelItemAttr, accountNameLength, (char *)accountName }, { kSecAccountItemAttr, accountNameLength, (char *)accountName },
{ kSecServiceItemAttr, strlen(serviceUTF8), (char *)serviceUTF8 }
};
SecKeychainAttributeList attributes = { sizeof(attrs) / sizeof(attrs[0]), attrs };
err = SecKeychainItemCreateFromContent(kSecGenericPasswordItemClass,
&attributes,
passwordLength,
(const char *)passwordData,
keychain,
access,
&item);
if (access)
CFRelease(access);
if (item)
CFRelease(item);
return noErr;
}