Skip to content

Commit 72efcef

Browse files
committed
Identify system cert trust when the cert is also user-trusted
Previously we used the first found certificate alias to find the store, and it seems the order of that wasn't guaranteed. Now we check all aliases individually.
1 parent 8354940 commit 72efcef

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

app/src/main/java/tech/httptoolkit/android/ProxySetup.kt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.util.Base64
88
import android.util.Log
99
import androidx.core.content.ContextCompat.getSystemService
1010
import com.beust.klaxon.Klaxon
11+
import io.sentry.Sentry
1112
import kotlinx.coroutines.*
1213
import okhttp3.OkHttpClient
1314
import okhttp3.Request
@@ -161,13 +162,24 @@ fun whereIsCertTrusted(proxyConfig: ProxyConfig): String? {
161162
val keyStore = KeyStore.getInstance("AndroidCAStore")
162163
keyStore.load(null, null)
163164

164-
val certificateAlias = keyStore.getCertificateAlias(proxyConfig.certificate)
165-
Log.i(TAG, "Cert alias $certificateAlias")
165+
val proxyCertData = proxyConfig.certificate.encoded
166+
167+
val aliases = keyStore.aliases()
168+
169+
val proxyCertAliases = aliases.toList().filter { alias ->
170+
val storedCertData = keyStore.getCertificate(alias).encoded
171+
return@filter storedCertData != null && storedCertData.contentEquals(proxyCertData)
172+
}
173+
174+
Log.i(TAG, "Proxy cert aliases: $proxyCertAliases")
166175

167176
return when {
168-
certificateAlias == null -> null
169-
certificateAlias.startsWith("system:") -> "system"
170-
certificateAlias.startsWith("user:") -> "user"
171-
else -> "unknown-store"
177+
proxyCertAliases.isEmpty() -> null
178+
proxyCertAliases.any { alias -> alias.startsWith("system:") } -> "system"
179+
proxyCertAliases.any { alias -> alias.startsWith("user:") } -> "user"
180+
else -> {
181+
Sentry.capture("Cert has no recognizable aliases")
182+
return "unknown-store"
183+
}
172184
}
173185
}

0 commit comments

Comments
 (0)