Swift 3 Mac OSX - can't cast SecCertificate object in my Keychain to SecKeychainItem?

Question

I am trying to delete an identity from my Keychain in Swift 3.0. My code is currently:

var idCert: SecCertificate? = nil
var idKey: SecKey? = nil
SecIdentityCopyCertificate(id, &idCert)
SecIdentityCopyPrivateKey(id, &idKey)
SecKeychainItemDelete(idCert as! SecKeychainItem)
SecKeychainItemDelete(idKey as! SecKeychainItem)

"id" is a SecIdentity object that I got by using SecItemCopyMatching to get all of my identities, and then used that list to find the identity associated with a specific email address. My code fails when I try to cast idCert to SecKeychainItem. Apple's Documentation says:

A SecCertificate object for a certificate that is stored in a keychain can be safely cast to a SecKeychainItem for manipulation as a keychain item. On the other hand, if the SecCertificate is not stored in a keychain, casting the object to a SecKeychainItem and passing it to Keychain Services functions returns errors.

but upon checking Keychain Access I can plainly see that the certificate is in the Keychain. What is going on? Is it possible for a certificate to appear to be in my Keychain but actually isn't or can't be found for some reason? Is there a way for me to programmatically check if the SecCertificate object is in my Keychain?


Show source
| xcode   | security   | osx   | swift   | keychain   2017-08-08 21:08 0 Answers

Answers to Swift 3 Mac OSX - can't cast SecCertificate object in my Keychain to SecKeychainItem? ( 0 )

Leave a reply to - Swift 3 Mac OSX - can't cast SecCertificate object in my Keychain to SecKeychainItem?

◀ Go back