Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / build-logic / shared-test / java / com / vayunmathur / testsupport / FakeAndroidKeyStore.kt
FakeAndroidKeyStore.kt125 lines · 5.1 KiBRaw
| 1 | package com.vayunmathur.testsupport |
| 2 | |
| 3 | import android.security.keystore.KeyGenParameterSpec |
| 4 | import java.io.InputStream |
| 5 | import java.io.OutputStream |
| 6 | import java.security.Key |
| 7 | import java.security.KeyStoreSpi |
| 8 | import java.security.Provider |
| 9 | import java.security.SecureRandom |
| 10 | import java.security.Security |
| 11 | import java.security.cert.Certificate |
| 12 | import java.util.Collections |
| 13 | import java.util.Date |
| 14 | import java.util.Enumeration |
| 15 | import javax.crypto.KeyGenerator |
| 16 | import javax.crypto.KeyGeneratorSpi |
| 17 | import javax.crypto.SecretKey |
| 18 | import javax.crypto.spec.SecretKeySpec |
| 19 | |
| 20 | /** |
| 21 | * Shared in-memory AndroidKeyStore provider for Robolectric (which has none). |
| 22 | * |
| 23 | * This is the repo's one shared copy, in `build-logic/shared-test/java`, which |
| 24 | * both convention plugins (`common-conventions-app` and |
| 25 | * `common-conventions-library`) add to every module's unit-test source set — |
| 26 | * the same mechanism that shares [DeterministicAwait]. It is a superset of |
| 27 | * the per-module copies it replaces: |
| 28 | * - alias helpers ([put]/[get]/[remove]/[contains]/[aliases]/[clear]) used by |
| 29 | * tests that inspect or reset the store directly; |
| 30 | * - [generator] for tests that obtain a generator through JCA; |
| 31 | * - [failGeneration] so a test can simulate key generation failing with no |
| 32 | * way to authenticate the user (the biometric unlock path); |
| 33 | * - byte-array key entries are stored (as an AES [SecretKeySpec]), matching |
| 34 | * the copies whose `engineSetKeyEntry(ByteArray)` stored the key. |
| 35 | * |
| 36 | * Keys never leave the test process. Install once per test class with |
| 37 | * [install]; installing twice is a no-op once any AndroidKeyStore provider |
| 38 | * (real or fake) is present. |
| 39 | */ |
| 40 | object FakeAndroidKeyStore { |
| 41 | private val keys = Collections.synchronizedMap(HashMap<String, Key>()) |
| 42 | |
| 43 | /** When true, the next key generation fails as it does with no way to authenticate the user. */ |
| 44 | var failGeneration: Boolean = false |
| 45 | |
| 46 | @Synchronized |
| 47 | fun install() { |
| 48 | if (Security.getProvider("AndroidKeyStore") == null) { |
| 49 | Security.addProvider(FakeProvider()) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | fun put(alias: String, key: Key) { |
| 54 | keys[alias] = key |
| 55 | } |
| 56 | |
| 57 | fun get(alias: String): SecretKey? = keys[alias] as? SecretKey |
| 58 | |
| 59 | fun remove(alias: String) { |
| 60 | keys.remove(alias) |
| 61 | } |
| 62 | |
| 63 | fun contains(alias: String): Boolean = keys.containsKey(alias) |
| 64 | |
| 65 | fun aliases(): List<String> = synchronized(keys) { keys.keys.toList() } |
| 66 | |
| 67 | fun clear() = synchronized(keys) { keys.clear() } |
| 68 | |
| 69 | /** Convenience for tests that obtain a generator through JCA. */ |
| 70 | fun generator(): KeyGenerator = KeyGenerator.getInstance("AES", "AndroidKeyStore") |
| 71 | |
| 72 | class FakeProvider : Provider("AndroidKeyStore", 1.0, "in-memory test keystore") { |
| 73 | init { |
| 74 | put("KeyStore.AndroidKeyStore", FakeKeyStoreSpi::class.java.name) |
| 75 | put("KeyGenerator.AES", FakeKeyGeneratorSpi::class.java.name) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | class FakeKeyStoreSpi : KeyStoreSpi() { |
| 80 | override fun engineLoad(stream: InputStream?, password: CharArray?) = Unit |
| 81 | override fun engineStore(stream: OutputStream?, password: CharArray?) = Unit |
| 82 | override fun engineGetKey(alias: String, password: CharArray?): Key? = keys[alias] |
| 83 | override fun engineContainsAlias(alias: String): Boolean = keys.containsKey(alias) |
| 84 | override fun engineDeleteEntry(alias: String) { |
| 85 | keys.remove(alias) |
| 86 | } |
| 87 | override fun engineAliases(): Enumeration<String> = Collections.enumeration(aliases()) |
| 88 | override fun engineSize(): Int = keys.size |
| 89 | override fun engineIsKeyEntry(alias: String): Boolean = keys.containsKey(alias) |
| 90 | override fun engineIsCertificateEntry(alias: String): Boolean = false |
| 91 | override fun engineGetCertificate(alias: String): Certificate? = null |
| 92 | override fun engineGetCertificateChain(alias: String): Array<Certificate>? = null |
| 93 | override fun engineGetCertificateAlias(cert: Certificate): String? = null |
| 94 | override fun engineGetCreationDate(alias: String): Date = Date() |
| 95 | override fun engineSetKeyEntry( |
| 96 | alias: String, |
| 97 | key: Key, |
| 98 | password: CharArray?, |
| 99 | chain: Array<out Certificate>?, |
| 100 | ) { |
| 101 | keys[alias] = key |
| 102 | } |
| 103 | override fun engineSetKeyEntry(alias: String, key: ByteArray, chain: Array<out Certificate>?) { |
| 104 | keys[alias] = SecretKeySpec(key, "AES") |
| 105 | } |
| 106 | override fun engineSetCertificateEntry(alias: String, cert: Certificate) = Unit |
| 107 | } |
| 108 | |
| 109 | class FakeKeyGeneratorSpi : KeyGeneratorSpi() { |
| 110 | private var alias: String = "test-key" |
| 111 | |
| 112 | override fun engineInit(random: SecureRandom) = Unit |
| 113 | override fun engineInit(keysize: Int, random: SecureRandom) = Unit |
| 114 | override fun engineInit(params: java.security.spec.AlgorithmParameterSpec?, random: SecureRandom) { |
| 115 | if (failGeneration) throw IllegalStateException("no authentication method available") |
| 116 | if (params is KeyGenParameterSpec) alias = params.keystoreAlias |
| 117 | } |
| 118 | |
| 119 | override fun engineGenerateKey(): SecretKey { |
| 120 | val bytes = ByteArray(32) |
| 121 | SecureRandom().nextBytes(bytes) |
| 122 | return SecretKeySpec(bytes, "AES").also { put(alias, it) } |
| 123 | } |
| 124 | } |
| 125 | } |