WebSocket server in .NET framework 4.5.2 with TLS : AuthenticateAsServer doesn’t accept certificate

I’m trying to setup a webSocket server in .NET 4.5.2 using TLS encryption and the System.Net libraries. Here is the server code :

static void Main(string[] args)
        {
            TcpListener httpsListener;
            X509Certificate2 serverCertificate = null;

            serverCertificate = new X509Certificate2("D:\\\\myCert.pfx", "password");
            httpsListener = new TcpListener(IPAddress.Any, 5000);
            httpsListener.Start();

            TcpClient tcpClient = httpsListener.AcceptTcpClient();
            SslStream sslStream = new SslStream(tcpClient.GetStream(), false);

            sslStream.AuthenticateAsServer(serverCertificate, clientCertificateRequired: false, enabledSslProtocols: SslProtocols.Tls12, checkCertificateRevocation: true);
            
            //treat request

        }

The error happens in the AuthenticateAsServer method :
System.ComponentModel.Win32Exception: ‘The credentials supplied to the package were not recognized’

The certificate is generatezd using the following openssl commands :

openssl genrsa -des3 -out ServKey.key 16384
openssl req -new -key ServKey.key -out ServCSR.csr
openssl req -new -newkey rsa:16384 -nodes -out CA_CSR.csr -keyout CA_CSR_key.key -sha256
openssl x509 -signkey CA_CSR_key.key -days 365 -req -in CA_CSR.csr -out CA.crt -sha256
openssl x509 -req -days 365 -in ServCSR.csr -CA CA.crt -CAkey CA_CSR_key.key -out myCert.crt -set_serial 01 -sha256
openssl pkcs12 -export -out myCert.pfx -inkey ServKey.key -in myCert.crt -certfile CA.crt

I’m unsure what are these credentials that the error is talking about. The apssword is correct.

I will be grateful for any help/insight.

Regards

  • 2

    That usually means “the certificate doesn’t have an associated private key”. Is serverCertificate.HasPrivateKey reporting true?

    – 

  • 1

    Probably a permissions issue on the private key for the certificate.

    – 

Leave a Comment