Skip to content

[Android] Avoid eager trust-anchor enumeration in certificate-chain builds - #135928

Merged
simonrozsival merged 3 commits into
mainfrom
simonrozsival-android-tls-chain-performance
Oct 21, 2026
Merged

simonrozsival merged 3 commits into
mainfrom
simonrozsival-android-tls-chain-performance

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Summary

Reduce Android certificate-chain setup overhead by avoiding full AndroidCAStore enumeration when a supplied certificate identifies a usable system trust anchor.

Constructing PKIXBuilderParameters directly from AndroidCAStore enumerates its certificate entries and materializes every trust anchor. On the investigated Samsung A16, this setup took approximately 123 ms, substantially more than the subsequent path-building operation.

This change:

  • Resolves the target and ExtraStore certificates against the live AndroidCAStore using exact certificate lookup, then constructs PKIX parameters from the matching system anchors.
  • For Android TLS server validation, forwards the platform trust manager’s selected anchor as a temporary hint. The hint must independently match a certificate in the current system store; it does not grant trust by itself.
  • Retains independent PKIX validation and managed policy checks. If the targeted attempt fails building, validation, revocation, or policy checks, it retries with freshly initialized full-store parameters. If no usable anchor is supplied, it uses the full store immediately.
  • Keeps CustomRootTrust isolated from system trust and removes the temporary TLS hint before invoking the application’s certificate-validation callback.

No cross-build trust-store, root-certificate, or mutable PKIX-state cache is introduced. No public API changes are included.

Performance

An earlier prototype measured offline system-trust X509Chain.Build at a median of 169.819 ms before the change and 8.166 ms afterward, approximately a 95% reduction.

These measurements used a Samsung SM-A165F running Android 16/API 36, arm64 CoreCLR, Release/full trimming, and three fresh processes with eight samples each. Revocation checking and certificate downloads were disabled for this offline comparison.

The physical-device figures describe the earlier prototype, not a rerun of the final three-commit series. They demonstrate the cost of eager trust-anchor setup; they are not evidence of a historical .NET regression.

Validation

The final local series was built for Android arm64 CoreCLR Release, including the native, Java, and managed changes. Both affected test projects were rebuilt.

On an API 35 arm64 emulator:

  • ChainTests: 44 passed, 1 unchanged skip.
  • AndroidPlatformTrustTests: 19 passed.

Coverage includes supplied/missing anchors, fallback from an unrelated trusted anchor, policy and expiry failures, concurrent builders, custom-trust isolation, hostname/policy rejection, and callback-certificate ownership.

The final series has not been rerun on the physical device. The new tests do not directly exercise revocation-triggered fallback.

Related to dotnet/android#12934.

Note

This description was drafted with GitHub Copilot.

/cc @wfurt @vcsjones

simonrozsival and others added 3 commits October 14, 2026 01:07
Resolve candidate anchors against a fresh AndroidCAStore, retain independent PKIX validation and full-store fallback, and keep custom trust isolated. Add concurrent system-chain, fallback, policy, and expiry coverage in the existing test project.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Use the platform-validated root only as a temporary hint, independently recheck live system membership and PKIX policy, and retain callback certificate ownership. Cover system-hint lifetimes and custom trust, hostname, policy, and callback behavior in the existing Android TLS test project.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The managed ExtraStore handle array already contains the target. Skip candidates referring to that same Java certificate during anchor discovery without changing the certificate store or independent validation.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to 'arch-android': @vitek-karas, @simonrozsival, @steveisok, @akoeplinger
See info in area-owners.md if you want to be subscribed.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/ncl, @bartonjs, @vcsjones
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Revocation-triggered fallback needs regression coverage verifying fresh checker/error state and the final validation result.

1 open finding
What changed in this PR

Reduces Android certificate-chain setup overhead by trying verified system-anchor hints before enumerating the full trust store.

Changes:

  • Adds targeted anchor lookup with fresh full-store fallback.
  • Passes temporary TLS anchor hints without granting trust or exposing them to application callbacks.
  • Adds Android chain, policy, and certificate-ownership tests.

Validation here was limited to source inspection; no builds or tests were run.

File Description
src/​native/​libs/​System.Security.Cryptography.Native.Android/​pal_x509chain.h Declares targeted-trust and fallback interfaces.
src/​native/​libs/​System.Security.Cryptography.Native.Android/​pal_x509chain.c Implements anchor lookup and fresh fallback state.
src/​native/​libs/​System.Security.Cryptography.Native.Android/​pal_trust_manager.h Extends callback signatures with anchor hints.
src/​native/​libs/​System.Security.Cryptography.Native.Android/​pal_trust_manager.c Verifies hints and manages callback references.
src/​native/​libs/​System.Security.Cryptography.Native.Android/​pal_jni.h Declares new Java method bindings.
src/​native/​libs/​System.Security.Cryptography.Native.Android/​pal_jni.c Registers bindings and updated callback signature.
src/​native/​libs/​System.Security.Cryptography.Native.Android/​net/​dot/​android/​crypto/​DotnetProxyTrustManager.java Forwards the platform-selected server anchor.
src/​libraries/​System.Security.Cryptography/​tests/​X509Certificates/​ChainTests.cs Tests supplied anchors, fallback, policies, and concurrency.
src/​libraries/​System.Security.Cryptography/​src/​System/​Security/​Cryptography/​X509Certificates/​ChainPal.Android.cs Retries targeted failures using full-store parameters.
src/​libraries/​System.Net.Security/​tests/​AndroidPlatformTrustTests/​System.Net.Security.AndroidPlatformTrust.Tests.csproj Includes HTTP test configuration.
src/​libraries/​System.Net.Security/​tests/​AndroidPlatformTrustTests/​AndroidPlatformTrustTests.cs Tests certificate ownership and custom-trust policies.
src/​libraries/​System.Net.Security/​src/​System/​Net/​Security/​SslStream.Android.cs Scopes anchor hints to synchronous validation.
src/​libraries/​System.Net.Security/​src/​System/​Net/​CertificateValidationPal.Android.cs Temporarily supplies hints during chain building.
src/​libraries/​Common/​src/​Interop/​Android/​System.Security.Cryptography.Native.Android/​Interop.X509Chain.cs Marshals trust selection and build outcomes.
src/​libraries/​Common/​src/​Interop/​Android/​System.Security.Cryptography.Native.Android/​Interop.Ssl.cs Updates the managed callback signature.

🧠 Review effort: Balanced

@simonrozsival

Copy link
Copy Markdown
Member Author

/azp run runtime-android

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@simonrozsival
simonrozsival merged commit 49be0fe into main Oct 21, 2026
123 of 128 checks passed
@simonrozsival
simonrozsival deleted the simonrozsival-android-tls-chain-performance branch October 21, 2026 07:44
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants