@@ -296,7 +296,7 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
296
296
Log .i(TAG , if (vpnIntent != null ) " got intent" else " no intent" )
297
297
val vpnNotConfigured = vpnIntent != null
298
298
299
- if (! isCertTrusted (config)) {
299
+ if (whereIsCertTrusted (config) == null ) {
300
300
// The cert isn't trusted, and the VPN may need setup, so there'll be a series of prompts
301
301
// here. Explain them beforehand, so users understand what's going on.
302
302
withContext(Dispatchers .Main ) {
@@ -568,18 +568,30 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
568
568
return VpnService .prepare(this ) == null
569
569
}
570
570
571
- private fun isCertTrusted (proxyConfig : ProxyConfig ): Boolean {
571
+ // Returns the name of the cert store (if the cert is trusted) or null (if not)
572
+ private fun whereIsCertTrusted (proxyConfig : ProxyConfig ): String? {
572
573
val keyStore = KeyStore .getInstance(" AndroidCAStore" )
573
574
keyStore.load(null , null )
574
575
575
576
val certificateAlias = keyStore.getCertificateAlias(proxyConfig.certificate)
576
- return certificateAlias != null
577
+ Log .i(TAG , " Cert alias $certificateAlias " )
578
+
579
+ return when {
580
+ certificateAlias == null -> null
581
+ certificateAlias.startsWith(" system:" ) -> " system"
582
+ certificateAlias.startsWith(" user:" ) -> " user"
583
+ else -> " unknown-store"
584
+ }
577
585
}
578
586
579
587
private fun ensureCertificateTrusted (proxyConfig : ProxyConfig ) {
580
- if (! isCertTrusted (proxyConfig)) {
588
+ if (whereIsCertTrusted (proxyConfig) == null ) {
581
589
app.trackEvent(" Setup" , " installing-cert" )
582
590
Log .i(TAG , " Certificate not trusted, prompting to install" )
591
+
592
+ // Install the required cert into the user CA store. Notably, if the cert is already
593
+ // installed as a system cert but disabled, this will get triggered, and will enable
594
+ // the cert, rather than adding a user cert.
583
595
val certInstallIntent = KeyChain .createInstallIntent()
584
596
certInstallIntent.putExtra(EXTRA_NAME , " HTTP Toolkit CA" )
585
597
certInstallIntent.putExtra(EXTRA_CERTIFICATE , proxyConfig.certificate.encoded)
0 commit comments