-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Use default Trust manager and fallback to user installed CA #7044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TimoPtr
merged 9 commits into
main
from
feature/use_default_keystore_and_fallback_to_user
Jun 18, 2026
+496
−31
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1dd5b1f
Fix server certificate validation while honoring user-installed CAs
bpacholek 8e427d9
Document the user-CA fallback's network_security_config assumption
bpacholek 36d9baf
Adjust tests and remove base64 files
TimoPtr cd70f6c
Simplify documentation
TimoPtr 01a925c
Ignore DiskReadViolation
TimoPtr cfc9109
Apply suggestion
TimoPtr 8b64fce
FIX test
TimoPtr 0afbe62
Update automotive baseline
TimoPtr 40d6b0e
Ignore DiskReadViolation on wear
TimoPtr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...otlin/io/homeassistant/companion/android/common/data/CompositeX509ExtendedTrustManager.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package io.homeassistant.companion.android.common.data | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import android.os.Build | ||
| import androidx.annotation.RequiresApi | ||
| import java.net.Socket | ||
| import java.security.cert.CertificateException | ||
| import java.security.cert.X509Certificate | ||
| import javax.net.ssl.SSLEngine | ||
| import javax.net.ssl.X509ExtendedTrustManager | ||
|
|
||
| /** | ||
| * An [X509ExtendedTrustManager] that trusts a server certificate if either the [primary] or the | ||
| * [fallback] accepts it. The [primary] is always tried first; the [fallback] is only asked when the | ||
| * [primary] rejects the chain with a [CertificateException]. | ||
| * | ||
| * This is deliberately more permissive than the [primary] alone: a certificate the [primary] rejects | ||
| * becomes trusted as soon as the [fallback] accepts it. It can never be stricter than the [primary], | ||
| * so it only ever adds acceptances and never introduces new rejections. | ||
| * | ||
| * Client-trust checks ([checkClientTrusted]) and [getAcceptedIssuers] decide which client | ||
| * certificates to accept, which is unrelated to widening server-certificate trust, so they delegate | ||
| * to the [primary] only. | ||
| * | ||
| * Requires API 24: [X509ExtendedTrustManager] and its hostname-aware overloads don't exist below it. | ||
| */ | ||
| @RequiresApi(Build.VERSION_CODES.N) | ||
| @SuppressLint("CustomX509TrustManager") | ||
| internal class CompositeX509ExtendedTrustManager( | ||
| private val primary: X509ExtendedTrustManager, | ||
| private val fallback: X509ExtendedTrustManager, | ||
| ) : X509ExtendedTrustManager() { | ||
|
|
||
| /** | ||
| * Runs [check] against the [primary], and only if it rejects with a [CertificateException] | ||
| * retries against the [fallback]. If both reject, the [primary]'s exception is rethrown (its | ||
| * verdict wins) with the [fallback]'s attached via [Throwable.addSuppressed]. | ||
| * | ||
| * Only [CertificateException] triggers the fallback; anything else propagates unchanged. | ||
| */ | ||
| private inline fun checkServerOrFallback(check: (X509ExtendedTrustManager) -> Unit) { | ||
| try { | ||
| check(primary) | ||
| } catch (primaryError: CertificateException) { | ||
| try { | ||
| check(fallback) | ||
| } catch (fallbackError: CertificateException) { | ||
| primaryError.addSuppressed(fallbackError) | ||
| throw primaryError | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) { | ||
| checkServerOrFallback { it.checkServerTrusted(chain, authType) } | ||
| } | ||
|
|
||
| override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?, socket: Socket?) { | ||
| checkServerOrFallback { it.checkServerTrusted(chain, authType, socket) } | ||
| } | ||
|
|
||
| override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?, engine: SSLEngine?) { | ||
| checkServerOrFallback { it.checkServerTrusted(chain, authType, engine) } | ||
| } | ||
|
|
||
| // Client trust uses the primary only (see class doc). | ||
| override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?) { | ||
| primary.checkClientTrusted(chain, authType) | ||
| } | ||
|
|
||
| override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?, socket: Socket?) { | ||
| primary.checkClientTrusted(chain, authType, socket) | ||
| } | ||
|
|
||
| override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?, engine: SSLEngine?) { | ||
| primary.checkClientTrusted(chain, authType, engine) | ||
| } | ||
|
|
||
| /** Returns the [primary]'s accepted issuers only; the [fallback] has no role in client authentication. */ | ||
| override fun getAcceptedIssuers(): Array<X509Certificate> { | ||
| return primary.acceptedIssuers | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
...n/io/homeassistant/companion/android/common/data/CompositeX509ExtendedTrustManagerTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| package io.homeassistant.companion.android.common.data | ||
|
|
||
| import io.mockk.every | ||
| import io.mockk.mockk | ||
| import io.mockk.verify | ||
| import java.net.Socket | ||
| import java.security.cert.CertificateException | ||
| import java.security.cert.X509Certificate | ||
| import javax.net.ssl.SSLEngine | ||
| import javax.net.ssl.X509ExtendedTrustManager | ||
| import org.junit.jupiter.api.Assertions.assertSame | ||
| import org.junit.jupiter.api.Assertions.assertThrows | ||
| import org.junit.jupiter.api.Assertions.assertTrue | ||
| import org.junit.jupiter.api.BeforeEach | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.params.ParameterizedTest | ||
| import org.junit.jupiter.params.provider.MethodSource | ||
|
|
||
| class CompositeX509ExtendedTrustManagerTest { | ||
|
|
||
| private lateinit var primary: X509ExtendedTrustManager | ||
| private lateinit var fallback: X509ExtendedTrustManager | ||
| private lateinit var composite: CompositeX509ExtendedTrustManager | ||
|
|
||
| @BeforeEach | ||
| fun setUp() { | ||
| primary = mockk(relaxed = true) | ||
| fallback = mockk(relaxed = true) | ||
| composite = CompositeX509ExtendedTrustManager(primary = primary, fallback = fallback) | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "{0}") | ||
| @MethodSource("serverChecks") | ||
| fun `Given primary accepts when checking server certificate then fallback is not consulted`(check: ServerCheck) { | ||
| check.run(composite) | ||
|
|
||
| verify(exactly = 1) { check.run(primary) } | ||
| verify(exactly = 0) { check.run(fallback) } | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "{0}") | ||
| @MethodSource("serverChecks") | ||
| fun `Given primary rejects and fallback accepts when checking server certificate then it is trusted`( | ||
| check: ServerCheck, | ||
| ) { | ||
| every { check.run(primary) } throws CertificateException("primary rejected") | ||
|
|
||
| // Must not throw: the fallback (relaxed mock) accepts the certificate. | ||
| check.run(composite) | ||
|
|
||
| verify(exactly = 1) { check.run(fallback) } | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "{0}") | ||
| @MethodSource("serverChecks") | ||
| fun `Given both reject when checking server certificate then primary error is thrown with fallback suppressed`( | ||
| check: ServerCheck, | ||
| ) { | ||
| val primaryError = CertificateException("primary rejected") | ||
| val fallbackError = CertificateException("fallback rejected") | ||
| every { check.run(primary) } throws primaryError | ||
| every { check.run(fallback) } throws fallbackError | ||
|
|
||
| val thrown = assertThrows(CertificateException::class.java) { check.run(composite) } | ||
|
|
||
| assertSame(primaryError, thrown) | ||
| assertTrue(thrown.suppressed.contains(fallbackError)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Given primary throws a non-certificate error when checking server then it propagates without fallback`() { | ||
| every { primary.checkServerTrusted(CHAIN, AUTH_TYPE) } throws IllegalStateException("boom") | ||
|
|
||
| assertThrows(IllegalStateException::class.java) { composite.checkServerTrusted(CHAIN, AUTH_TYPE) } | ||
|
|
||
| verify(exactly = 0) { fallback.checkServerTrusted(CHAIN, AUTH_TYPE) } | ||
| } | ||
|
|
||
| @Test | ||
| fun `When checking client certificate then it delegates to the primary only`() { | ||
| composite.checkClientTrusted(CHAIN, AUTH_TYPE) | ||
| composite.checkClientTrusted(CHAIN, AUTH_TYPE, SOCKET) | ||
| composite.checkClientTrusted(CHAIN, AUTH_TYPE, ENGINE) | ||
|
|
||
| verify { | ||
| primary.checkClientTrusted(CHAIN, AUTH_TYPE) | ||
| primary.checkClientTrusted(CHAIN, AUTH_TYPE, SOCKET) | ||
| primary.checkClientTrusted(CHAIN, AUTH_TYPE, ENGINE) | ||
| } | ||
| verify(exactly = 0) { | ||
| fallback.checkClientTrusted(any(), any()) | ||
| fallback.checkClientTrusted(any(), any(), any<Socket>()) | ||
| fallback.checkClientTrusted(any(), any(), any<SSLEngine>()) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `When getting accepted issuers then only the primary issuers are returned`() { | ||
| val issuers = arrayOf<X509Certificate>(mockk()) | ||
| every { primary.acceptedIssuers } returns issuers | ||
|
|
||
| assertSame(issuers, composite.acceptedIssuers) | ||
|
|
||
| verify(exactly = 0) { fallback.acceptedIssuers } | ||
| } | ||
|
|
||
| /** | ||
| * One of the three `checkServerTrusted` overloads, used to run each test against all of them. | ||
| * [run] calls the overload on any [X509ExtendedTrustManager]: the composite under test, or a mock | ||
| * inside a MockK `every`/`verify` block. | ||
| */ | ||
| data class ServerCheck(private val label: String, val run: (X509ExtendedTrustManager) -> Unit) { | ||
| override fun toString(): String = label | ||
| } | ||
|
|
||
| companion object { | ||
| private const val AUTH_TYPE = "RSA" | ||
| private val CHAIN = arrayOf<X509Certificate>(mockk(relaxed = true)) | ||
| private val SOCKET = mockk<Socket>(relaxed = true) | ||
| private val ENGINE = mockk<SSLEngine>(relaxed = true) | ||
|
|
||
| @JvmStatic | ||
| fun serverChecks() = listOf( | ||
| ServerCheck("checkServerTrusted(chain, authType)") { | ||
| it.checkServerTrusted(CHAIN, AUTH_TYPE) | ||
| }, | ||
| ServerCheck("checkServerTrusted(chain, authType, socket)") { | ||
| it.checkServerTrusted(CHAIN, AUTH_TYPE, SOCKET) | ||
| }, | ||
| ServerCheck("checkServerTrusted(chain, authType, engine)") { | ||
| it.checkServerTrusted(CHAIN, AUTH_TYPE, ENGINE) | ||
| }, | ||
| ) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.