SslStream and PFX certificate failed

342 views Asked by At

I can connect with server successfully using python client like this:

...
sslSock = ssl.wrap_socket(sock, key_file, cert_file, ssl_version=ssl.PROTOCOL_TLSv1)
...

But I cannot connect with server using C# :

...
byte[] pfxData = File.ReadAllBytes("bob_pfx.pfx");
TcpClient client = new TcpClient(machineName, port);
Stream stream = client.GetStream();

SslStream sslstream = new SslStream(client.GetStream());

X509Certificate2 certificate = new X509Certificate2(pfxData,"", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);

X509Certificate2Collection certificateCollection = new X509Certificate2Collection();
certificateCollection.Add(certificate);

try
{
    sslstream.AuthenticateAsClient(machineName, certificateCollection, SslProtocols.Tls, true);
}
catch (SystemException ex)
{
    Console.WriteLine(ex.Message);
}
...

The file "bob_pfx.pfx" was built by this command:

openssl pkcs12 -export -out test.pfx -inkey rui.key -in rui.crt

And the fault message is:

The authentication or decryption has failed.

Is there any problem in my codes ? Thanks very much!

0

There are 0 answers