Storing Identity in keychain

I'm trying to add .pfx certificate to keychain which comes with password protection. So i decided to get identity from certificate and store it. I'm using system keychain to store identity.And program will run as root.

if let password = CFStringCreateCopy(kCFAllocatorDefault, "something" as CFString) {
        let p12 = SecPKCS12Import(data as CFData, [kSecImportExportPassphrase as String: password] as CFDictionary, &cfarray)
        print(cfarray)
        if let identityArray = cfarray as? NSArray {
            if let identityDict = identityArray.object(at: 0) as? NSDictionary {
                let identityKey = String(kSecImportItemIdentity)
                let identity = identityDict[identityKey] as! SecIdentity
                let query: [String: Any] = [kSecClass as String: kSecClassIdentity,
                                            kSecValueRef as String: identity,
                                            kSecAttrLabel as String: "iddata",
                                            kSecUseKeychain as String: keychain,
                                            kSecImportExportPassphrase as String: password]
                let status = SecItemAdd(query as CFDictionary, nil)
                print(status)
            }
    }
    }

When executing I'm getting -50 as output for print(status).

I also tried with this query dictionary for SecItemAdd

let query: [String: Any] = [kSecClass as String: kSecClassIdentity,kSecValueRef as String: identity,kSecAttrLabel as String: "iddata",kSecUseKeychain as String: keychain,kSecImportExportPassphrase as String: password]

What is wrong with this? Please help

SecPKCS12Import is one remaining area where the iOS and macOS Security framework differ substantially. On iOS, SecPKCS12Import creates an in-memory identity reference. On macOS, it imports the identity into the keychain and then create an identity that references that. So, presuming this is macOS, based on your other thread, there’s no need to add this identity to the keychain.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Storing Identity in keychain
 
 
Q