Skip to content

WebSocket custom SSL validation only works when using client certificates #5729

@esardaya

Description

@esardaya

Describe the bug
It's possible to disable (or provide custom) SSL validation by setting SslCertificateAuthentication like in this example:

serviceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication()
{
    CertificateValidationMode = X509CertificateValidationMode.None,
    RevocationMode = X509RevocationMode.NoCheck,
};

However, this only works when using client certificate authentication.
Looking at:

if (_channelFactory is HttpsChannelFactory<IDuplexSessionChannel> httpsChannelFactory && httpsChannelFactory.RequireClientCertificate)

It seems like the WebSocket RemoteCertificateValidationCallback is only ever set if RequireClientCertificate is true, which will only be the case when using client certificates.

It should be possible to disable or customize server SSL certificate validation regardless of the auth type being used.

To Reproduce

  1. Create a binding where WebSockets are always enabled, without setting any client certificate auth.
var binding = new NetHttpBinding(BasicHttpSecurityMode.Transport)
{
    MaxReceivedMessageSize = int.MaxValue,  
    WebSocketSettings = { TransportUsage = WebSocketTransportUsage.Always },
};
  1. Disable SSL validation with:
serviceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.Custom;
serviceCertificate.Authentication.CustomCertificateValidator = new DisableCertificateValidation();
serviceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication()
{
    CertificateValidationMode = X509CertificateValidationMode.None,
    RevocationMode = X509RevocationMode.NoCheck
};
  1. Try to call a service that uses a self-signed SSL certificate, or one that doesn't match the domain.

Expected behavior
The call should succeed.

Actual behavior
Call fails with an error similar to this one:

Exception info dump:
System.ServiceModel.CommunicationException: Unable to connect to the remote server
 ---> System.Net.WebSockets.WebSocketException (0x80004005): Unable to connect to the remote server
 ---> System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
 ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
Add any other context about the problem here.

Metadata

Metadata

Labels

feature requestAdding new functionality requiring adding an API to the public contract.triaged

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions