5

I'm trying to connect to a server via a SslStream / Tcp Client. Everytime I do I get an exception stating: Received an unexpected EOF or 0 bytes from the transport stream at the AuthenticateAsClient line.

I've enabled Trace logging and am getting the following two errors in the logs:

System.Net Information: 0 : [12260] SecureChannel#66702757::.ctor(hostname=xxx.xx.xx.xxx, #clientCertificates=0, encryptionPolicy=RequireEncryption)

System.Net Information: 0 : [12260] SecureChannel#66702757 - Left with 0 client certificates to choose from.

Would anybody be able to give me any advice on how to solve this? Not sure why but it's throwing to the outer catch if that makes any difference.

try
{
    TcpClient client = new TcpClient(_host, _port);
    // _stream = client.GetStream();

    _stream = new SslStream(client.GetStream(), false, ValidateServerCertificate, null);
    _sslReader = new StreamReader(client.GetStream());

    try
    {
        _stream.AuthenticateAsClient(_host);
    }
    catch (AuthenticationException e)
    {
        client.Close();
        return;
    }

    HandleConnect();
}
catch (Exception e)
{
    _logger.Error(e.BuildExceptionInfo());
}

public bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    if (sslPolicyErrors == SslPolicyErrors.None || sslPolicyErrors == SslPolicyErrors.RemoteCertificateNameMismatch)
        return true;

    return false;
}
RagtimeWilly
  • 5,265
  • 3
  • 25
  • 41

2 Answers2

2

My guess is the server is requiring a client certificate.

Boklucius
  • 1,896
  • 17
  • 19
0

This happened to me after updating to Windows build 20H2. It used to work with just:

ssl.AuthenticateAsClient(_host);

but I managed to connect again without using a certificate after I explicitly set the enabledSslProtocols parameter to SslProtocols.Default:

ssl.AuthenticateAsClient(_host, null, SslProtocols.Default, false);
Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62