@@ -31,12 +31,16 @@ internal object LegacyKeyProvider {
3131 val keyStore = KeyStore .getInstance(ANDROID_KEY_STORE_NAME )
3232 keyStore.load(null )
3333
34- if (keyStore.containsAlias(keyAlias)) {
35- return Result .failure(
36- CredentialStoreError (
37- " Key already exists for the keyAlias: $keyAlias in $ANDROID_KEY_STORE_NAME "
34+ try {
35+ if (keyStore.containsAlias(keyAlias)) {
36+ return Result .failure(
37+ CredentialStoreError (
38+ " Key already exists for the keyAlias: $keyAlias in $ANDROID_KEY_STORE_NAME "
39+ )
3840 )
39- )
41+ }
42+ } catch (exception: Exception ) {
43+ return Result .failure(CredentialStoreError (" Failed to connect to KeyStore" ))
4044 }
4145
4246 val parameterSpec =
@@ -60,9 +64,13 @@ internal object LegacyKeyProvider {
6064 val keyStore = KeyStore .getInstance(ANDROID_KEY_STORE_NAME )
6165 keyStore.load(null )
6266
63- if (! keyStore.containsAlias(keyAlias)) {
64- val message = " Key does not exists for the keyAlias: $keyAlias in $ANDROID_KEY_STORE_NAME "
65- return Result .failure(CredentialStoreError (message))
67+ try {
68+ if (! keyStore.containsAlias(keyAlias)) {
69+ val message = " Key does not exists for the keyAlias: $keyAlias in $ANDROID_KEY_STORE_NAME "
70+ return Result .failure(CredentialStoreError (message))
71+ }
72+ } catch (exception: Exception ) {
73+ return Result .failure(CredentialStoreError (" Failed to connect to KeyStore" ))
6674 }
6775
6876 val key: Key ? = keyStore.getKey(keyAlias, null )
@@ -79,6 +87,10 @@ internal object LegacyKeyProvider {
7987 val keyStore = KeyStore .getInstance(ANDROID_KEY_STORE_NAME )
8088 keyStore.load(null )
8189
82- keyStore.deleteEntry(keyAlias)
90+ try {
91+ keyStore.deleteEntry(keyAlias)
92+ } catch (exception: Exception ) {
93+ // KeyStore unreachable
94+ }
8395 }
8496}
0 commit comments