Skip to content

certificate validation not working properly in .Net Android #95506

@usbatwork

Description

@usbatwork

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

  • in a release build the parameter sslErrors returns SslPolicyErrors.None indicating that there are no errors and hence breaking the validation. The expectation would be to get SslPolicyErrors.RemoteCertificateChainErrors.
  • in a debug build the parameter returns 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

  • create a .Net Android sample (just use the template that comes with VS)
  • add some code to make a web service call as described in the above link
  • connect to a server with a server certificate directly signed by a private CA with the private CA not trusted by the application
  • run the app and check the callback parameter sslErrors
    debug build -> parameter sslErrors has the value RemoteCertificateChainErrors as expected
    release 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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions