SslStream client unable to complete handshake with stunnel server
===

I have a fully operational system where openssl based clients interact with an openssl server. Each client have its own certificate that is validated by the server. Certificates have been generated with openssl (X509, pem). They are self-signed.

I now want to write a test client based on SslStream. I used the client example from the SslStream class [documentation][1].

My SslStream client is unable to complete the handshake. stunnel complains about the client not sending its certificate. This is confirmed in Wireshark (Certificates Length: 0 in handshake messages).

My client displays the following exception:

> Inner exception: The message received was unexpected or badly
> formatted

This is how I load my certificate:

    X509Certificate cert = new X509Certificate2(filename, password);
    X509CertificateCollection certColl = new X509CertificateCollection();
    certColl.Add(cert);

I tried retrieving certificate various properties (ex: GetSerialNumberString()). It works. The Verify method returns false. This is the next thing I am going to investigate.

How I setup my SslStream does not seem to matter (same result):

    sslStream.AuthenticateAsClient(serverName);
    
    SslStream sslStream = new SslStream(
      client.GetStream(),
      false,
      new RemoteCertificateValidationCallback(ValidateServerCertificate), 
      new LocalCertificateSelectionCallback(SelectLocalCertificate));
                
Same with authentication:

    sslStream.AuthenticateAsClient(serverName);
    
    sslStream.AuthenticateAsClient(serverName,
      certColl,
      SslProtocols.Tls,
      true);

`SelectLocalCertificate` gets called (twice) and returns my certificate. `ValidateServerCertificate` currently never gets called (to my surprise).

How can I debug that? Even better if you can nail my problem.

  [1]: http://msdn.microsoft.com/en-us/library/d50tfa1c