Android does not trust the proxy certificate
Apps built for Android 7+ ignore user-installed CAs. How to allow them in a debug build with network_security_config, and what it will not fix.
What it means
The certificate is installed, Chrome on the phone works, and the app fails with SSLHandshakeException: Trust anchor for certification path not found. A trust anchor is the root certificate path validation starts
from (RFC 5280 § 6.1.1).
An app with targetSdkVersion 24 (Android 7.0) or higher trusts only system
CAs and ignores user-installed ones.
How to fix it
The app has to allow user certificates itself, in a debug build only.
app/src/main/res/xml/network_security_config.xml:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<debug-overrides>
<trust-anchors>
<certificates src="user" />
<certificates src="system" />
</trust-anchors>
</debug-overrides>
</network-security-config>
Manifest:
<application
android:networkSecurityConfig="@xml/network_security_config"
... >
<debug-overrides> applies only to builds with android:debuggable="true",
which Gradle sets on debug builds only. A plain <base-config> with
src="user" works in release too; do not use it.
One host instead of all:
<domain-config>
<domain includeSubdomains="true">api.example.com</domain>
<trust-anchors>
<certificates src="user" />
</trust-anchors>
</domain-config>
<debug-overrides> accepts only <trust-anchors>, so a <domain-config> is
live in release builds. Put this copy of the file in app/src/debug/res/xml/:
Gradle uses it for the debug variant only.
- The cutoff is the app's
targetSdkVersion, not the phone's Android version. - The config is compiled into the APK: rebuild and reinstall after editing it.
What this will not fix
Certificate pinning. A <pin-set> in the same config is lifted in debug
builds with overridePins="true" on the user <certificates> in
<debug-overrides>. OkHttp's CertificatePinner or a custom TrustManager
reject a certificate the system already trusts; turn them off in the debug
variant's code. Details: SSL pinning.
Flutter. Dart has its own TLS stack and ignores both
network_security_config and user certificates. Use a SecurityContext with
your CA, or badCertificateCallback in a debug build.
Somebody else's app. Install the certificate as a system CA (root plus a Magisk or KernelSU module such as
cert-fixer or AlwaysTrustUserCerts), or repatch the APK with apk-mitm.
Android 11, 14 and later
Android 11: CA certificates install only from Settings. An app calling
KeyChain.createInstallIntent(), or opening the file from a browser or file
manager, gets «Can't install CA certificates… must be installed in Settings».
Install by hand: Settings → Security → Encryption & credentials → Install a
certificate → CA certificate. Vendor paths differ; search Settings for «CA
certificate». The certificate lands in the user store, so the config above is
still needed.
Android 14: the system store moved from /system/etc/security/cacerts to
the read-only com.android.conscrypt APEX, /apex/com.android.conscrypt/cacerts.
Tricks that drop a file into /system fail; use a module that handles the APEX
path. User certificates did not move.
See it on your own traffic
Until the config takes effect, Solpuga shows only connections that open and die at once; after, the app's requests in full. Setting the proxy on the phone is a separate step. Full setup: Mobile Devices.
Related
- iOS: profile installed, certificate not trustedOn iOS a root certificate takes two steps. The second one is under About.
- How to see the traffic from an iPhone or an Android phonePhone and computer on one Wi-Fi, a proxy in the network settings, a root certificate with full trust. Four steps, and the five things that break them.