Skip to content

Commit 956e2ca

Browse files
committed
Fido: Delete invalidated keys
1 parent b3f5a33 commit 956e2ca

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

play-services-fido/core/src/main/kotlin/org/microg/gms/fido/core/transport/screenlock/ScreenLockCredentialStore.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.database.sqlite.SQLiteDatabase
1010
import android.database.sqlite.SQLiteOpenHelper
1111
import android.os.Build
1212
import android.security.keystore.KeyGenParameterSpec
13+
import android.security.keystore.KeyPermanentlyInvalidatedException
1314
import android.security.keystore.KeyProperties
1415
import android.util.Base64
1516
import android.util.Log
@@ -20,6 +21,7 @@ import java.security.cert.Certificate
2021
import java.security.spec.ECGenParameterSpec
2122
import kotlin.random.Random
2223

24+
@RequiresApi(23)
2325
class ScreenLockCredentialStore(val context: Context) {
2426
private val keyStore by lazy { KeyStore.getInstance("AndroidKeyStore").apply { load(null) } }
2527

@@ -51,10 +53,15 @@ class ScreenLockCredentialStore(val context: Context) {
5153
keyStore.getCertificateChain(getAlias(rpId, keyId))
5254

5355
fun getSignature(rpId: String, keyId: ByteArray): Signature? {
54-
val privateKey = getPrivateKey(rpId, keyId) ?: return null
55-
val signature = Signature.getInstance("SHA256withECDSA")
56-
signature.initSign(privateKey)
57-
return signature
56+
try {
57+
val privateKey = getPrivateKey(rpId, keyId) ?: return null
58+
val signature = Signature.getInstance("SHA256withECDSA")
59+
signature.initSign(privateKey)
60+
return signature
61+
} catch (e: KeyPermanentlyInvalidatedException) {
62+
keyStore.deleteEntry(getAlias(rpId, keyId))
63+
throw e
64+
}
5865
}
5966

6067
fun containsKey(rpId: String, keyId: ByteArray): Boolean = keyStore.containsAlias(getAlias(rpId, keyId))

play-services-fido/core/src/main/kotlin/org/microg/gms/fido/core/ui/AuthenticatorActivity.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class AuthenticatorActivity : AppCompatActivity(), TransportHandlerCallback {
122122
}
123123

124124
@RequiresApi(24)
125-
suspend fun handleRequest(options: RequestOptions) {
125+
suspend fun handleRequest(options: RequestOptions, allowInstant: Boolean = true) {
126126
try {
127127
val facetId = getFacetId(this, options, callerPackage)
128128
options.checkIsValid(this, facetId, callerPackage)
@@ -135,10 +135,10 @@ class AuthenticatorActivity : AppCompatActivity(), TransportHandlerCallback {
135135
Log.d(TAG, "facetId=$facetId, appName=$appName")
136136

137137
// Check if we can directly open screen lock handling
138-
if (!requiresPrivilege) {
138+
if (!requiresPrivilege && allowInstant) {
139139
val instantTransport = transportHandlers.firstOrNull { it.isSupported && it.shouldBeUsedInstantly(options) }
140140
if (instantTransport != null && instantTransport.transport in INSTANT_SUPPORTED_TRANSPORTS) {
141-
startTransportHandling(instantTransport.transport)
141+
startTransportHandling(instantTransport.transport, true)
142142
return
143143
}
144144
}
@@ -250,10 +250,18 @@ class AuthenticatorActivity : AppCompatActivity(), TransportHandlerCallback {
250250
return shouldStartTransportInstantly(SCREEN_LOCK)
251251
}
252252

253-
fun startTransportHandling(transport: Transport): Job = lifecycleScope.launchWhenResumed {
253+
@RequiresApi(24)
254+
fun startTransportHandling(transport: Transport, instant: Boolean = false): Job = lifecycleScope.launchWhenResumed {
254255
val options = options ?: return@launchWhenResumed
255256
try {
256257
finishWithSuccessResponse(getTransportHandler(transport)!!.start(options, callerPackage), transport)
258+
} catch (e: SecurityException) {
259+
Log.w(TAG, e)
260+
if (instant) {
261+
handleRequest(options, false)
262+
} else {
263+
finishWithError(SECURITY_ERR, e.message ?: e.javaClass.simpleName)
264+
}
257265
} catch (e: CancellationException) {
258266
Log.w(TAG, e)
259267
// Ignoring cancellation here

0 commit comments

Comments
 (0)