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
?