Skip to content

Commit f8d49ac

Browse files
committed
chore: optimise code
Signed-off-by: qwq233 <[email protected]>
1 parent 816adef commit f8d49ac

File tree

4 files changed

+19
-25
lines changed

4 files changed

+19
-25
lines changed

service/src/main/java/io/github/a13e300/tricky_store/Config.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ object Config {
155155
val generalSettings: General = General(),
156156
@TomlComments("Remember to override the corresponding system properties when modifying the following values") val deviceProps: DeviceProps = DeviceProps(),
157157
val globalConfig: AppConfig = AppConfig(),
158-
@TomlComments("Disable specific module function.", "Do not modify if you know nothing about it.") val additionalAppConfig: Map<String, AppConfig> = mapOf(
158+
@TomlComments("Disable specific module function for specific app.", "Do not modify if you know nothing about it.") val additionalAppConfig: Map<String, AppConfig> = mapOf(
159159
"com.example.app" to AppConfig(generateKey = true, createOperation = true, importKey = true)
160160
)
161161
) {
@@ -165,6 +165,7 @@ object Config {
165165
@TomlComments("SDK Version (i.e.: 35 for Android 15)") val osVersion: Int = Build.VERSION.SDK_INT,
166166
@TomlComments("Auto reset the security patch props on startup") val autoResetProps: Boolean = true,
167167
)
168+
168169
@Serializable
169170
data class DeviceProps(
170171
val brand: String = Build.BRAND,
@@ -187,6 +188,16 @@ object Config {
187188
)
188189
}
189190

191+
fun isGenerateKeyEnabled(callingUid: Int) = devConfig.additionalAppConfig[callingUid.getPackageNameByUid()]?.generateKey == true && devConfig.globalConfig.generateKey
192+
193+
fun isCreateOperationEnabled(callingUid: Int) = devConfig.additionalAppConfig[callingUid.getPackageNameByUid()]?.createOperation == true && devConfig.globalConfig.createOperation
194+
195+
fun isImportKeyEnabled(callingUid: Int) = devConfig.additionalAppConfig[callingUid.getPackageNameByUid()]?.importKey == true && devConfig.globalConfig.importKey
196+
197+
private fun Int.getPackageNameByUid() = runCatching {
198+
getPm()?.getPackagesForUid(this)?.first()
199+
}.getOrNull()
200+
190201
fun parseDevConfig(f: File?) = runCatching {
191202
f ?: return@runCatching
192203
// stop watching writing to prevent recursive calls

service/src/main/java/io/github/a13e300/tricky_store/KeystoreInterceptor.kt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import android.system.keystore2.IKeystoreService
99
import android.system.keystore2.KeyDescriptor
1010
import android.system.keystore2.KeyEntryResponse
1111
import io.github.a13e300.tricky_store.Cache.Key
12-
import io.github.a13e300.tricky_store.Config.devConfig
1312
import io.github.a13e300.tricky_store.binder.BinderInterceptor
1413
import io.github.a13e300.tricky_store.keystore.CertHack
1514
import io.github.a13e300.tricky_store.keystore.Utils
@@ -49,9 +48,7 @@ object KeystoreInterceptor : BinderInterceptor() {
4948
if (Config.needGenerate(callingUid))
5049
runCatching {
5150
data.enforceInterface(IKeystoreService.DESCRIPTOR)
52-
if (!devConfig.globalConfig.generateKey
53-
|| devConfig.additionalAppConfig[callingUid.getPackageNameByUid()]?.generateKey == false
54-
) {
51+
if (!Config.isGenerateKeyEnabled(callingUid)) {
5552
Logger.d("generateKey feature disabled for $callingUid")
5653
return Skip
5754
}
@@ -62,6 +59,7 @@ object KeystoreInterceptor : BinderInterceptor() {
6259
Cache.getKeyResponse(callingUid, descriptor.alias)
6360
val p = Parcel.obtain()
6461
if (response == null) {
62+
Logger.i("pass null key for uid=$callingUid alias=${descriptor.alias}")
6563
p.writeTypedObject(null, 0)
6664
} else {
6765
Logger.i("generate key for uid=$callingUid alias=${descriptor.alias}")
@@ -79,9 +77,7 @@ object KeystoreInterceptor : BinderInterceptor() {
7977
Logger.d("KeystoreInceptor onPreTransact updateSubcomponent uid=$callingUid pid=$callingPid")
8078
runCatching {
8179
data.enforceInterface(IKeystoreService.DESCRIPTOR)
82-
if (!devConfig.globalConfig.importKey
83-
|| devConfig.additionalAppConfig[callingUid.getPackageNameByUid()]?.importKey == false
84-
) {
80+
if (!Config.isImportKeyEnabled(callingUid)) {
8581
Logger.d("importKey feature disabled for $callingUid")
8682
return Skip
8783
}
@@ -155,9 +151,7 @@ object KeystoreInterceptor : BinderInterceptor() {
155151
Logger.d("KeystoreInterceptor intercept getKeyEntry uid=$callingUid pid=$callingPid")
156152
try {
157153
data.enforceInterface("android.system.keystore2.IKeystoreService")
158-
if (!devConfig.globalConfig.generateKey
159-
|| devConfig.additionalAppConfig[callingUid.getPackageNameByUid()]?.generateKey == false
160-
) {
154+
if (!Config.isGenerateKeyEnabled(callingUid)) {
161155
Logger.d("getKeyEntry feature disabled for $callingUid")
162156
return Skip
163157
}

service/src/main/java/io/github/a13e300/tricky_store/SecurityLevelInterceptor.kt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import android.system.keystore2.IKeystoreSecurityLevel
1313
import android.system.keystore2.KeyDescriptor
1414
import android.system.keystore2.KeyEntryResponse
1515
import android.system.keystore2.KeyMetadata
16-
import io.github.a13e300.tricky_store.Config.devConfig
1716
import io.github.a13e300.tricky_store.binder.BinderInterceptor
1817
import io.github.a13e300.tricky_store.keystore.CertHack
1918
import io.github.a13e300.tricky_store.keystore.Utils
@@ -48,9 +47,7 @@ class SecurityLevelInterceptor(
4847
generateKeyTransaction -> runCatching {
4948
data.enforceInterface(IKeystoreSecurityLevel.DESCRIPTOR)
5049
Logger.i("intercept key gen uid=$callingUid pid=$callingPid")
51-
if (!devConfig.globalConfig.generateKey
52-
|| devConfig.additionalAppConfig[callingUid.getPackageNameByUid()]?.generateKey == false
53-
) {
50+
if (!Config.isGenerateKeyEnabled(callingUid)) {
5451
Logger.d("generateKey feature disabled for $callingUid")
5552
return Skip
5653
}
@@ -77,9 +74,7 @@ class SecurityLevelInterceptor(
7774

7875
importKeyTransaction -> runCatching {
7976
data.enforceInterface(IKeystoreSecurityLevel.DESCRIPTOR)
80-
if (!devConfig.globalConfig.importKey
81-
|| devConfig.additionalAppConfig[callingUid.getPackageNameByUid()]?.importKey == false
82-
) {
77+
if (!Config.isImportKeyEnabled(callingUid)) {
8378
Logger.d("importKey feature disabled for $callingUid")
8479
return Skip
8580
}
@@ -130,9 +125,7 @@ class SecurityLevelInterceptor(
130125
createOperationTransaction -> runCatching {
131126
data.enforceInterface(IKeystoreSecurityLevel.DESCRIPTOR)
132127
Logger.d("createOperationTransaction uid=$callingUid pid=$callingPid")
133-
if (!devConfig.globalConfig.createOperation
134-
|| devConfig.additionalAppConfig[callingUid.getPackageNameByUid()]?.createOperation == false
135-
) {
128+
if (!Config.isCreateOperationEnabled(callingUid)) {
136129
Logger.d("createOperation feature disabled for $callingUid")
137130
return Skip
138131
}

service/src/main/java/io/github/a13e300/tricky_store/util.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,5 @@ val telephonyInfos by lazy {
121121

122122
fun String.toDER() = DEROctetString(this.toByteArray())
123123

124-
fun Int.getPackageNameByUid() = runCatching {
125-
getPm()?.getPackagesForUid(this)?.first()
126-
}.getOrNull()
127-
128124
fun DEROctetString.toTaggedObj(tag: Int, explicit: Boolean = true) = DERTaggedObject(explicit, tag, this)
129125
fun String.trimLine() = trim().split("\n").joinToString("\n") { it.trim() }

0 commit comments

Comments
 (0)