I am adding a public key to the keyChain using SecItemAdd and then later retreive it using the SecItemCopyMatching
Now up to the point where I add the key with a PersistentRef the PersistRef is not nil, when I modify my query to get the SecKeyRef using SecItemCopyMatching, the result is nil. It is working fine on iOS 8. Find below the code and let me know if I need to add something specifically for iOS9 or am I missing something here.
NSData *data = [[NSData alloc] initWithBase64EncodedString:key options:NSDataBase64DecodingIgnoreUnknownCharacters];
if(!data){
return nil;
}
NSString *tag = @"publicTag";
NSData *d_tag = [NSData dataWithBytes:[tag UTF8String] length:[tag length]];
// delete any old key
NSMutableDictionary *publicKey = [[NSMutableDictionary alloc] init];
[publicKey setObject:(__bridge id) kSecClassKey forKey:(__bridge id)kSecClass];
[publicKey setObject:(__bridge id) kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
[publicKey setObject:d_tag forKey:(__bridge id)kSecAttrApplicationTag];
SecItemDelete((__bridge CFDictionaryRef)publicKey);
[publicKey setObject:data forKey:(__bridge id)kSecValueData];
[publicKey setObject:(__bridge id) kSecAttrKeyClassPublic forKey:(__bridge id)
kSecAttrKeyClass];
[publicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)
kSecReturnPersistentRef];
CFTypeRef persistKey = nil;
OSStatus status = ((__bridge CFDictionaryRef)publicKey, &persistKey); // status is 0 and persistKey is also not nil
if (persistKey != nil){
CFRelease(persistKey);
}
if ((status != noErr) && (status != errSecDuplicateItem)) {
return nil;
}
[publicKey removeObjectForKey:(__bridge id)kSecValueData];
[publicKey removeObjectForKey:(__bridge id)kSecReturnPersistentRef];
[publicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnRef];
[publicKey setObject:(__bridge id) kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
SecKeyRef keyRef = nil;
status = SecItemCopyMatching((__bridge CFDictionaryRef)publicKey, (CFTypeRef *)&keyRef); // status is 0 but keyRef is nil here
if(status != noErr){
return nil;
}
return keyRef;
it returns the keyRef Object in iOS 8 but returns nil iOS 9.