-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
certificate validation not working properly in .Net Android #95506
certificate validation not working properly in .Net Android #95506
Comments
Tagging subscribers to this area: @dotnet/area-system-security, @bartonjs, @vcsjones Issue DetailsDescriptionI'm trying to validate a certificate using
as described here: In a .NET Android project it works as expected in debug mode only - but not in release mode: When connecting to a server with a self-signed certificate
The settings for a debug and release build are the same to my knowledge. In a .NET iOS project both debug and release builds work as expected. Reproduction Steps
Expected behaviorFor both debug and release build SslPolicyErrors.RemoteCertificateChainErrors is returned when connecting to a server with a self-signed certificate. Actual behaviordebug build -> parameter sslErrors has the value 'RemoteCertificateChainErrors' as expected Regression?It worked perfectly fine in Xamarin.Android. The issue started to occur in .NET6 Android and persists in .NET8 Android. Known WorkaroundsNone found yet. A potential workaround could be to (re-)build the chain in the callback and validate the new chain. But this didn't work out likely due to the following issue: Configuration.NET6 Android and .NET 8 Android (.Net 7 hasn't been tested) Other informationIf I don't register the callback at all I get a trust exception as expected for release and debug builds. I have tested only with a privately signed certificate as described in the repro steps. I haven't tested with a self-signed cert but would expect that the same happens here, too.
|
/cc @simonrozsival |
@usbatwork I was able to replicate the issue. As a workaround, consider setting The problem seems to be in |
@simonrozsival Thanks a lot for looking into this for us! |
@simonrozsival Not entirely sure but I might have found a related issue: We updated to .NET8 and applied the workaround on Android as suggested. Now we get an exception on iOS: |
@usbatwork hmm, that's interesting. I think the iOS issue is unrelated, the internal implementation is very different. I will try to reproduce the iOS issue and narrow down what could be going on there. |
@usbatwork Just to make sure: did you configure |
@simonrozsival Thanks a lot for the hint! I checked this. |
@simonrozsival We checked about Hence enforcing If you are aware of any side effects of setting |
@usbatwork from what you describe, it seems that the managed handler is a better fit for your use case. The only side effect that I can think of is a slight increase in the .ipa size. Let me know if you run into any unexpected problems. |
Follow-up to #8594 to fix dotnet/runtime#95506 on Android API 21-23
Follow-up to #8594 to fix dotnet/runtime#95506 on Android API 21-23
Fixes: dotnet/runtime#95506 In Release configuration the `X509ExtendedTrustManagerInvoker` class is trimmed and so the `trustManager is IX509TrustManager tm` pattern matching doesn't work. This PR addresses the problem in two ways: * an internal X509 trust manager is now required - it can't silently work with a null internal trust manager anymore * `[DynamicDependency]` attribute to prevent trimming of the invoker classes for the `IX509TrustManager` interface and for the `X509ExtendedTrustManager` abstract class
Follow-up to #8594 to fix dotnet/runtime#95506 on Android API 21-23
Description
I'm trying to validate a certificate using
clientHandler.ServerCertificateCustomValidationCallback
as described here:
https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclienthandler.servercertificatecustomvalidationcallback?view=net-8.0&viewFallbackFrom=net-android-34.0
In a .NET Android project it works as expected in debug mode only - but not in release mode: When connecting to a server with a self-signed certificate
SslPolicyErrors.None
indicating that there are no errors and hence breaking the validation. The expectation would be to getSslPolicyErrors.RemoteCertificateChainErrors
.SslPolicyErrors.RemoteCertificateChainErrors
as expected.The settings for a debug and release build are the same to my knowledge.
In a .NET iOS project both debug and release builds work as expected.
Reproduction Steps
sslErrors
debug build -> parameter sslErrors has the value
RemoteCertificateChainErrors
as expectedrelease build -> parameter sslErrors has the value
None
Expected behavior
For both debug and release build SslPolicyErrors.RemoteCertificateChainErrors is returned when connecting to a server with a self-signed certificate.
Actual behavior
debug build -> parameter sslErrors has the value 'RemoteCertificateChainErrors' as expected
release build -> parameter sslErrors has the value 'None'
Regression?
It worked perfectly fine in Xamarin.Android. The issue started to occur in .NET6 Android and persists in .NET8 Android.
Known Workarounds
None found yet. A potential workaround could be to (re-)build the chain in the callback and validate the new chain. But this didn't work out likely due to the following issue:
#84202
Configuration
.NET6 Android and .NET 8 Android (.Net 7 hasn't been tested)
Android 14 simulator and also on real Android 14 device
Visual Studio Enterprise 2022 (64-bit) - Version 17.7.4 building on Windows 11
Other information
If I don't register the callback at all I get a trust exception as expected for release and debug builds.
I have tested only with a privately signed certificate as described in the repro steps. I haven't tested with a self-signed cert but would expect that the same happens here, too.
The text was updated successfully, but these errors were encountered: