I have the following code for retrieving and storing RSAkeydata
fileprivate func retrieveAndStoreRSAKeyData(isPublic: Bool) throws -> CFData {
var resultCode = noErr
var result: CFData
let value = {
if isPublic {
let t = self.publicTag
print(t)
}
else
{
let s = self.privateTag
print(s)
}
}
var keyCallbacks = kCFTypeDictionaryKeyCallBacks
var valueCallbacks = kCFTypeDictionaryValueCallBacks
let keys = [Unmanaged.passUnretained(kSecClass).toOpaque(), Unmanaged.passUnretained(kSecAttrApplicationTag).toOpaque(), Unmanaged.passUnretained(kSecAttrKeyType).toOpaque(), Unmanaged.passUnretained(kSecReturnData).toOpaque()]
let values = [Unmanaged.passUnretained(kSecClassKey).toOpaque(), Unmanaged<AnyObject>.passUnretained(value as AnyObject).toOpaque(), Unmanaged.passUnretained(kSecAttrKeyTypeRSA).toOpaque(), Unmanaged.passUnretained(kCFBooleanTrue).toOpaque()]
let queryKey = CFDictionaryCreate(kCFAllocatorDefault,UnsafeMutablePointer.allocate(capacity: keys.count),UnsafeMutablePointer.allocate(capacity: values.count), 4, &keyCallbacks, &valueCallbacks)
// Get the key.
var item: AnyObject?
resultCode = SecItemCopyMatching(queryKey!, &item)
if(resultCode != noErr) {
try generateKeyPair()
/*
Recurcively call the retrieval again after keys have been generated
*/
result = try retrieveAndStoreRSAKeyData(isPublic: isPublic)
}
else {
result = item as! CFData
}
return result
}
The CFDictionaryCreate always fails and code crashes in this line in
let queryKey = CFDictionaryCreate(kCFAllocatorDefault,UnsafeMutablePointer.allocate(capacity: keys.count),UnsafeMutablePointer.allocate(capacity: values.count), 4, &keyCallbacks, &valueCallbacks)
Can someone help me out on this. Thanks in advance
to generate a key pair
and few helper functions