SecItemAdd One or more parameters passed to a function were not valid

I'm trying to add a public key to the KeyChain but I'm getting an errror -50 . This is my code:

    OSStatus error = noErr;
    CFTypeRef persistPeer = NULL;

    NSData *refTag = [[NSData alloc] initWithBytes:(const void *)[keyTag UTF8String] length:[keyTag length]];
    NSMutableDictionary *keyAttr = [[NSMutableDictionary alloc] init];

    [keyAttr setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass];
    [keyAttr setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
    [keyAttr setObject:refTag forKey:(__bridge id)kSecAttrApplicationTag];
    [keyAttr setObject:(__bridge id)kSecAttrKeyClassPublic forKey:(id)kSecAttrKeyClass];

    error = SecItemDelete((CFDictionaryRef) keyAttr);

    [keyAttr setObject:extractedKey forKey:(__bridge id)kSecValueData];
    [keyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnPersistentRef];
    [keyAttr setObject:(__bridge id)kSecAttrAccessible forKey:(__bridge id)kSecAttrAccessibleAfterFirstUnlock];

    error = SecItemAdd((CFDictionaryRef) keyAttr, (CFTypeRef *)&persistPeer);

If I comment out the kSecAttrAccessible, I don't get any errors and it works as expected. According to SecItem.h, the kSecClassKey can have the kSecAttrAccessible attribute. Am I missing something? Is there a required attribute when using kSecAttrAccessible?

Accepted Reply

I saw the issue. I just don't know how to delete my question. For those who encountered the same issue, the value and the id was switched.

This

    [keyAttr setObject:(__bridge id)kSecAttrAccessible forKey:(__bridge id)kSecAttrAccessibleAfterFirstUnlock];

should be

    [keyAttr setObject:(__bridge id)kSecAttrAccessibleAfterFirstUnlock forKey:(__bridge id)kSecAttrAccessible];

Rubber duck debugging.

Replies

I saw the issue. I just don't know how to delete my question. For those who encountered the same issue, the value and the id was switched.

This

    [keyAttr setObject:(__bridge id)kSecAttrAccessible forKey:(__bridge id)kSecAttrAccessibleAfterFirstUnlock];

should be

    [keyAttr setObject:(__bridge id)kSecAttrAccessibleAfterFirstUnlock forKey:(__bridge id)kSecAttrAccessible];

Rubber duck debugging.