I'm fairly new to crypto on iOS, and I've been running into an error that I haven't been able to find a solution for:
Whenever I try to get a SecKeyRef to a public key in the iOS keychain and use it, I end up with a EXC_BAD_ACCESS error. The SecKeyRef (called "publicKeyReference" in my code below is initially set to NULL, but it should have a value after the SecItemCopyMatching method is called, which can be seen from the memory address in the debugger window.
Here's my code:
SecKeyRef publicKeyReference = NULL;
NSData* publicTag = [publicKeyIdentifier dataUsingEncoding:NSUTF8StringEncoding];
NSMutableDictionary *queryPublicKey = [[NSMutableDictionary alloc] init];
// Set the public key query dictionary.
[queryPublicKey setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass];
[queryPublicKey setObject:publicTag forKey:(__bridge id)kSecAttrApplicationTag];
[queryPublicKey setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
[queryPublicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnPersistentRef];
// Get the key.
sanityCheck = SecItemCopyMatching((__bridge CFDictionaryRef)queryPublicKey, (CFTypeRef *)&publicKeyReference);
// Encrypt using the public.
sanityCheck = SecKeyEncrypt( publicKeyReference,
PADDING,
plainBuffer,
plainBufferSize,
&cipherBuffer[0],
&cipherBufferSize
);
And Here's some screenshots of the error and the debug window:
It seems that something is being assigned to the SecKeyRef, since the value of the address isn't "0x0", but I've been continually getting the EXC_BAD_ACCESS error regardless of what I've tried. Any and all help is greatly appreciated on the issue.