I am trying to connect with Java netty based server, which auto generates certificates for itself (and server guys told me, that is accepting any certificate from client side for now).
My task was to migrate TcpSocket connection into Tls encrypted connection.
First of all, I converted TcpSocket into NetworkStream:
using (var client = new NetworkStream(connection.TcpSocket))
{
if (client.CanRead)
{
client.BeginRead(recvState.DataBuffer, 0, recvState.DataBuffer.Length, ReceiveCallback,
recvState);
}
}
and that is working perfectly. So then, I decided to build SslAuthentication - like here:
using (var client = new NetworkStream(connection.TcpSocket))
using (var sslStream = new SslStream(client, false, App_CertificateValidation))
{
var clientCertificate = new X509Certificate2("client.pfx");
var clientCertificateCollection = new X509Certificate2Collection(new[] { clientCertificate });
sslStream.AuthenticateAsClient("MyServer", clientCertificateCollection, SslProtocols.Tls, false);
if (sslStream.CanRead)
{
sslStream.BeginRead(recvState.DataBuffer, 0, recvState.DataBuffer.Length, ReceiveCallback,
recvState);
}
}
Where client.pfx
is random certificate with no password, as a file in project and also imported into Current User Certificates > Personal > Certificates
in certmgr.msc
.
The problem is AuthenticateAsClient throws an
System.IO.IOException: Authentication failed because the remote party has closed the transport stream exception.
Also, if the hostname in AuthenticateAsCtlient method means anything, if server accepts every certificate? Should I put there something significant?
I still can contact with the server guys, so I can ask them about everything - do we need any additional information?
Got it working.
The server who is hosting the socket server MUST have installed on its Certification Storage the certificate WITH the Private Key. If you install it without it (just the certificate or just the public key) you will get those errors of authentication failure.
I hope it help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With