SecItemAdd on OS X not working, on iOS is

2.8k views Asked by At

I have a problem. I have working Code for iOS to add a private Key to the Keychain by SecItemAdd. It works without any error. On OS X with the same attributes and values, it does not work. Any ideas, whats the problem. Here is the part of the Code:

NSData * keyData = ...
NSString * name = @"TestKey"
NSString * keyID = @"TestKey"
const id keys[]     = {
        (__bridge id)(kSecClass),
        (__bridge id)(kSecAttrKeyClass),
        (__bridge id)(kSecAttrLabel),
        (__bridge id)(kSecAttrApplicationLabel),
        (__bridge id)(kSecAttrIsPermanent),
        (__bridge id)(kSecAttrAccessible),
        (__bridge id)(kSecValueData) };
const id values[]   = {
        (__bridge id)(kSecClassKey),
        (__bridge id)(kSecAttrKeyClassPrivate),
        name,
        keyID,
        (id)kCFBooleanTrue,
        (__bridge id)(kSecAttrAccessibleAfterFirstUnlock),
        keyData };
NSMutableDictionary * attributes    = [[NSMutableDictionary alloc] initWithObjects:values forKeys:keys count:ATTR_COUNT(keys)];

CFTypeRef       result;
NSError * error = nil;

OSStatus osStatus = SecItemAdd((__bridge CFDictionaryRef)attributes, &result);

The error is:

25303 (errKCNoSuchAttr / errSecNoSuchAttr: / The attribute does not exist.).)

2

There are 2 answers

0
NSDestr0yer On

Which OS X version are you trying to support? OS X Keychain Services are different than iOS Keychain Services. For example, kSecClass is only available as of OS X 10.7, and kSecAttrAccessible 10.9.

0
dhruv On

The Error code specifies The attribute does not exist, it is due to the attribute : kSecAttrKeyClass. Try removing this attribute, and use tag names to distinguish the different keys. I was also getting similar issue in my code.