Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SslStream TcpClient - Received an unexpected EOF or 0 bytes from the transport stream

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;
}
like image 776
RagtimeWilly Avatar asked Nov 04 '22 00:11

RagtimeWilly


1 Answers

My guess is the server is requiring a client certificate.

like image 91
Boklucius Avatar answered Nov 09 '22 16:11

Boklucius