fix(transport): install Android trust manager for IP-literal TLS brokers#67
Merged
Merged
Conversation
8 tasks
The HostnameAwareTrustManager added in 5591751 was only installed when a TLS SNI server name was present. SNI is intentionally null for IP literals (RFC 6066 §3 forbids IP literals in SNI), so configurePlatformTrust returned early and ktor's CIO engine fell back to the platform default trust manager. On Android, when network_security_config.xml contains any <domain-config>, the platform's NetworkSecurityTrustManager throws from the 2-arg checkServerTrusted(chain, authType) regardless of the target host and requires the 3-arg hostname-aware overload (KTOR-2243). So a private broker reached by LAN IP — the common self-signed-cert setup — still failed with "Domain specific configurations require that hostname aware checkServerTrusted(...) is used". Decouple the SNI server name from the trust-evaluation host: pass the real host (IP literal or DNS name) to configurePlatformTrust, whose parameter is now a non-null String — a compile-time guard against re-passing the nullable SNI value. The HostnameAwareTrustManager is now installed for IP literals too, routing through X509TrustManagerExtensions so the platform's domain-aware requirement is satisfied. SNI stays suppressed for IPs via sniServerName(). Reported in meshtastic/Meshtastic-Android#5894. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
4621178 to
3f763be
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Connecting to a private MQTT broker over TLS by IP address fails on Android with a confusing platform error:
Why: ktor's CIO TLS engine only calls the 2-arg
X509TrustManager.checkServerTrusted(chain, authType)(KTOR-2243). On Android, when the consuming app'snetwork_security_config.xmlcontains any<domain-config>block, the platform swaps in aNetworkSecurityTrustManagerthat throws from the 2-arg overload — regardless of the target host — and demands the 3-arg hostname-aware overload.5591751already addressed this withHostnameAwareTrustManager, but only installed it when a TLS SNI server name was present. SNI is intentionallynullfor IP literals (RFC 6066 §3 forbids IPs in SNI), soconfigurePlatformTrustreturned early and ktor fell back to the throwing platform default. A private broker reached by LAN IP — the common self-signed-cert setup — therefore still failed.The defect was surfaced by the recent migration off OkHttp (whose trust manager calls the 3-arg overload) to ktor CIO. Diagnosis credit to @Olli-LUT on the linked issue.
This fix decouples the SNI server name (correctly
nullfor IPs) from the trust-evaluation host (always the real host). The hostname-aware trust manager is now installed for IP-literal brokers too, routing throughX509TrustManagerExtensionsso the platform's domain-aware requirement is satisfied.Changes
All in the
:transport-tcpmodule:configurePlatformTrust(serverName: String?)→configurePlatformTrust(host: String)across theexpectand all threeactuals (android / jvm / native). The non-nullStringis a compile-time guard against re-passing the nullable SNI value.actualnow installsHostnameAwareTrustManagerfor any non-blank host (IP literal or DNS name), not only when SNI is present.TcpTransportcall site passesendpoint.hostfor trust evaluation while still suppressing SNI for IPs.sniServerName()/isIpLiteral()to top-levelinternalfunctions and addedcommonTest'sTcpTransportTlsTestcovering the SNI-vs-trust-host decoupling (IPv4/IPv6 suppress SNI; DNS keeps it).Testing
spotlessCheck+detektpass:transport-tcp:allTestspasses (JVM + macOS + iOS native), incl. newTcpTransportTlsTest(4/4):transport-tcp:compileCommonMainKotlinMetadata+ Android target compile/assemble pass (expect/actual contract validated for all targets)Related Issues
Addresses meshtastic/Meshtastic-Android#5894 (an app-side
mqtt-clientversion bump is required to ship the fix to users).