title: SSL handshake failing with "sslv3 alert handshake failure:SSL alert number 40"
body: <p>I have a .pfx file, which can perfectly connect to the remote server when used on a windows client. I want to now connect to server using a linux client.</p>
  
  <p>Problem 1) I used following openssl commands to extract public cert and private key from the pfx file,</p>
  
  <pre><code>openssl pkcs12 -in Name.pfx -nocerts -out priv.pem -nodes
  openssl pkcs12 -in Name.pfx -nokeys -out pub.pem
  </code></pre>
  
  <p>But when I ran following two commands to verify md5 of both files, I found both of them different.</p>
  
  <pre><code>openssl rsa -noout -text -in priv.pem | openssl md5
  openssl x509 -noout -text -in pub.pem | openssl md5
  </code></pre>
  
  <p>Problem 2) I instead used following command to extract single pem file from pfx that has both cert and key.</p>
  
  <pre><code>openssl pkcs12 -in Name.pfx -out bundle.pem
  </code></pre>
  
  <p>Using this pem file I tried connecting to the remote server, with following command :</p>
  
  <pre><code>openssl s_client -servername 1.2.3.4 -connect 1.2.3.4:1234 -CAfile bundle.pem -state -tls1_2
  </code></pre>
  
  <p>This gives following output on terminal</p>
  
  <pre><code>CONNECTED(00000003)
  SSL_connect:before/connect initialization
  SSL_connect:SSLv3 write client hello A
  SSL_connect:SSLv3 read server hello A
  depth=0 O = "My Name", CN = Name - Local
  verify return:1
  SSL_connect:SSLv3 read server certificate A
  SSL_connect:SSLv3 read server key exchange A
  SSL_connect:SSLv3 read server certificate request A
  SSL_connect:SSLv3 read server done A
  SSL_connect:SSLv3 write client certificate A
  SSL_connect:SSLv3 write client key exchange A
  SSL_connect:SSLv3 write change cipher spec A
  SSL_connect:SSLv3 write finished A
  SSL_connect:SSLv3 flush data
  SSL3 alert read:fatal:handshake failure
  SSL_connect:failed in SSLv3 read finished A
  140250807310240:error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure:s3_pkt.c:1275:SSL alert number 40
  140250807310240:error:1409E0E5:SSL routines:SSL3_WRITE_BYTES:ssl handshake failure:s3_pkt.c:598:
  ---
  Certificate chain
   0 s:/O=My Name /CN=Name - Local
     i:/O=My Name /CN=Name - Local
  ---
  Server certificate
  -----BEGIN CERTIFICATE-----
  &lt;random string of certificate&gt;
  -----END CERTIFICATE-----
  subject=/O=My Name /CN=Name - Local
  issuer=/O=My Name /CN=Name - Local
  ---
  No client certificate CA names sent
  Server Temp Key: ECDH, secp521r1, 521 bits
  ---
  SSL handshake has read 1332 bytes and written 206 bytes
  ---
  New, TLSv1/SSLv3, Cipher is ECDHE-RSA-AES256-GCM-SHA384
  Server public key is 2048 bit
  Secure Renegotiation IS supported
  Compression: NONE
  Expansion: NONE
  SSL-Session:
      Protocol  : TLSv1.2
      Cipher    : ECDHE-RSA-AES256-GCM-SHA384
      Session-ID: &lt;some string&gt;
      Session-ID-ctx: 
      Master-Key: &lt;some string&gt;
      Key-Arg   : None
      Krb5 Principal: None
      PSK identity: None
      PSK identity hint: None
      Start Time: 1495217834
      Timeout   : 7200 (sec)
      Verify return code: 0 (ok)
  ---
  </code></pre>
  
  <p>I cant figure out why is handshake failing. Stuck for 3 days, where exactly does problem lie.</p>