russhwolf / multiplatform-settings

A Kotlin Multiplatform library for saving simple key-value data
Apache License 2.0
1.7k stars 67 forks source link

KeychainSettings: Keychain error -50 when updating or deleting an existing keychain item #79

Closed tKe closed 3 years ago

tKe commented 3 years ago

KeychainSettings fails with Keychain error -50: One or more parameters passed to a function were not valid. when updating or deleting an existing keychain item.

Simplest example to reproduce for me is as follows:

            val settings: Settings = KeychainSettings("test-srvc")
            settings.putString("test-key", "hello world") // will fail here if second execution (update previous value)
            settings.remove("test-key") // will fail here if first execution

I've reproduced this, with kotlin 1.4.21 and multiplatform-settings 0.7.1

NB: It also occurs in KaMPKit when switching AppleSettings for KeychainSettings on the second update of the breed list (when updating the timestamp value)

tKe commented 3 years ago

Also reproducible in the sample app (iPhone Simulator running iOS 13.3): Setting a value in Keychain a second time will result in "INVALID VALUE!" (due to the catch(...) { return false }) and attempting to remove/clear results in the application crashing.

russhwolf commented 3 years ago

The culprit appears to be that the update and remove operations set kSecMatchLimit in a misguided attempt to avoid accidentally touching more than one key. I don't know why, but this gives an error code indicating invalid parameter only after a relaunch. Will publish a fix as version 0.7.2 shortly.

russhwolf commented 3 years ago

There's still an issue when using clear(). Will investigate. Should also verify that keys and size work since the sample app doesn't use them.

russhwolf commented 3 years ago

Ok I think all issues are fixed now in 0.7.3. Pro-tip don't use kSecMatchLimit in any keychain function except SecItemCopyMatching() (this is not documented officially anywhere I could find)