Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keychain SecItemUpdate returning errSecParam

Tags:

swift

keychain

I can add items to the keychain, I can even read them out.

However, I'm struggling to use SecItemUpdate to update a value, and seem to get errSecParam returned every time.

To do so I'm creating the following query and attributes (very likely to be an issue here)

let query: [String: Any] = [kSecAttrAccount as String : "MyString",
    kSecValueData as String: "test2".data(using: .utf16)!,
    kSecMatchLimit as String  : kSecMatchLimitOne
]

let attributes: [String: Any] = [kSecAttrAccount as String: "aa",
                                         kSecValueData as String: data]

which is then used to update

SecItemUpdate(query as CFDictionary, attributes as CFDictionary)

like image 427
WishIHadThreeGuns Avatar asked Sep 20 '26 14:09

WishIHadThreeGuns


1 Answers

It should be specified class in query and not data (for update), so, for example, to add/update password in keychain it should be as following

// adding item
let addQuery: [CFString: Any] = [
    kSecClass: kSecClassGenericPassword,
    kSecAttrService: service as CFString,        // eg. "www.mysite.com"
    kSecAttrAccount: name as CFString            // eg. "user"
    kSecValueData: password.data(using: .utf8)   // eg. "password"
] as CFDictionary

if errSecSuccess != SecItemAdd(addQuery, nil) { 
    // report error here
}

// updating item (same query is for SecItemDelete)

let updateQuery: [CFString: Any] = [
    kSecClass: kSecClassGenericPassword,
    kSecAttrService: service as CFString,        // eg. "www.mysite.com"
    kSecAttrAccount: name as CFString            // eg. "user"
] as CFDictionary

let newAttributes = [
    kSecAttrAccount: newName as CFString            // eg. "user1"
    kSecValueData: newPassword.data(using: .utf8)   // eg. "password1"
] as CFDictionary

if errSecSuccess != SecItemUpdate(updateQuery, newAttributes) {
   // report error here
}
like image 83
Asperi Avatar answered Sep 22 '26 17:09

Asperi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!