Cyrille QUÉMIN
01/24/2019, 9:10 PMSecKeyCreateRandomKey
and `CFBridgingRetain`:
val privateKeyRef = SecKeyCreateRandomKey(cfKeyGenerationAttributes, error.ptr)
println("Key generated: ${CFBridgingRelease(privateKeyRef)} ")
This works nicely. When I try to retrieve my key at later date I have either crashes or the key is not found.
I suspect this is due to how I define the application data string tag in my dictionary:
CFDictionaryAddValue(cfKeyGenerationAttributes,kSecAttrApplicationTag,CFBridgingRetain(keyAlias.wcstr.ptr))
I tried few combination but I never seem to be able to retrieve the key that is successfully created: it is either crashing or saying that the key is not found.
What is the proper way to pass a string to the dictionary and overall to Kotlin Native for Object C?olonho
01/24/2019, 9:43 PMCyrille QUÉMIN
01/24/2019, 9:46 PMCyrille QUÉMIN
01/24/2019, 9:47 PMolonho
01/25/2019, 5:15 AMCyrille QUÉMIN
01/25/2019, 6:52 AMval cfKeyGenerationAttributes = CFDictionaryCreateMutable(null, 0, null, null)
CFDictionaryAddValue(cfKeyGenerationAttributes, kSecAttrKeyType, kSecAttrKeyTypeRSA)
CFDictionaryAddValue(cfKeyGenerationAttributes, kSecAttrKeySizeInBits, CFBridgingRetain(NSNumber(int = rsaSpec.keyLength)))
CFDictionaryAddValue(cfKeyGenerationAttributes, kSecAttrIsPermanent, CFBridgingRetain(shouldBePersisted))
CFDictionaryAddValue(cfKeyGenerationAttributes, kSecAttrApplicationTag, keyAlias.cstr)
val error = alloc<CFErrorRefVar>()
val privateKeyRef = SecKeyCreateRandomKey(cfKeyGenerationAttributes, error.ptr)
println("Private Key generated: ${CFBridgingRelease(privateKeyRef)} ")
val publicKeySecRef = SecKeyCopyPublicKey(privateKeyRef)
println("Public Key generated: ${CFBridgingRelease(publicKeySecRef)} ")
RSA retrieval:
val cfKeyQueryAttributes = CFDictionaryCreateMutable(null, 0, null, null)
CFDictionaryAddValue(cfKeyQueryAttributes, kSecClass, kSecClassKey)
CFDictionaryAddValue(cfKeyQueryAttributes, kSecAttrKeyType, kSecAttrKeyTypeRSA)
CFDictionaryAddValue(cfKeyQueryAttributes, kSecAttrApplicationTag, keyAlias.cstr)
CFDictionaryAddValue(cfKeyQueryAttributes, kSecReturnRef, CFBridgingRetain(true))
val secKeyRef = alloc<CFTypeRefVar>()
val status = SecItemCopyMatching(cfKeyQueryAttributes, secKeyRef.ptr)
val errMsg = CFBridgingRelease(SecCopyErrorMessageString(status, null))
println("status is: ${errMsg!!}")
This produces a segmentation fault
Everytime, every attempt, the error is when SecItemCopyMatching
is called. Generation displays the messages
I did several different attempts where I change how the alias is used for both generation and retrieval:
- with val cfString: CFStringRef = CFBridgingRetain(keyAlias)!!.reinterpret()
SecItemCopyMatching
produces: No keychain is available. You may need to restart your computer.
- with CFBridgingRetain(keyAlias.cstr)
SecItemCopyMatching
produces One or more parameters passed to a function were not valid
svyatoslav.scherbina
01/25/2019, 8:27 AMCFBridgingRetain(keyAlias)
seems to be correct.
Have you tried writing the same code in Swift?Cyrille QUÉMIN
01/25/2019, 9:40 AMCyrille QUÉMIN
01/25/2019, 9:41 AMsvyatoslav.scherbina
01/25/2019, 9:50 AMCyrille QUÉMIN
01/25/2019, 3:25 PMCyrille QUÉMIN
01/25/2019, 3:25 PM