@@ -296,7 +296,7 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
296296 Log .i(TAG , if (vpnIntent != null ) " got intent" else " no intent" )
297297 val vpnNotConfigured = vpnIntent != null
298298
299- if (! isCertTrusted (config)) {
299+ if (whereIsCertTrusted (config) == null ) {
300300 // The cert isn't trusted, and the VPN may need setup, so there'll be a series of prompts
301301 // here. Explain them beforehand, so users understand what's going on.
302302 withContext(Dispatchers .Main ) {
@@ -568,18 +568,30 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
568568 return VpnService .prepare(this ) == null
569569 }
570570
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? {
572573 val keyStore = KeyStore .getInstance(" AndroidCAStore" )
573574 keyStore.load(null , null )
574575
575576 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+ }
577585 }
578586
579587 private fun ensureCertificateTrusted (proxyConfig : ProxyConfig ) {
580- if (! isCertTrusted (proxyConfig)) {
588+ if (whereIsCertTrusted (proxyConfig) == null ) {
581589 app.trackEvent(" Setup" , " installing-cert" )
582590 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.
583595 val certInstallIntent = KeyChain .createInstallIntent()
584596 certInstallIntent.putExtra(EXTRA_NAME , " HTTP Toolkit CA" )
585597 certInstallIntent.putExtra(EXTRA_CERTIFICATE , proxyConfig.certificate.encoded)
0 commit comments