37

I'm trying to connect to a service that requires a certificate for authorization. The process is that I send the service a CSR file. The service signs the CSR and sends me a certificate that I use for connection.

  1. I generated the CSR by the following command line:

    openssl req -new -nodes -newkey rsa:2048 -keyout cert.key -out cert.csr
    
  2. I took the content of the cert.csr and sent to them. They generate the client certificate and I got a PEM file back.

  3. I now try to connect using their certificate file in SSLCERT for curl() and providing the private key from cert.key as CURLOPT_SSLKEY - (which I got at step 1).

  4. Fails with: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure

What am I doing wrong in this process?

It works when I try with a received a test certificate including a private key from the service (self signed certificate). But when I use a certificate they generated from my CSR and then use my private key as key, it errors with handshake failure.

So I know it does not have something to do with that openssl / curl doesn't support v3/TLS etc. that others when researching for a solution found out their problem was.

Here is what I run:

  curl -i -v --request POST https://service.com/ --cert clientcert.pem --key private_key.pem --cert-type pem --tlsv1.1 --insecure
* Connected to service.com (1xx.xxx.xxx.xx) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Request CERT (13):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS handshake, CERT verify (15):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS alert, Server hello (2):
* error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure
* Closing connection 0

Running following versions: curl 7.35.0 (x86_64-pc-linux-gnu) libcurl/7.35.0 OpenSSL/1.0.1f zlib/1.2.8 libidn/1.28 librtmp/2.3

10
  • 4
    No private key is ever sent anywhere, let alone via SSL. That's why they're called private. Difficult to understand what exactly you're asking here.
    – user207421
    Apr 2, 2016 at 7:52
  • @EJP Okay, but I need to send a private key because the only thing I receive from them is a certificate (generated out of a CSR i made)? Am i missing something?
    – Karem
    Apr 2, 2016 at 7:55
  • @EJP I must send a private key, otherwise the error "unable to set private key file:"
    – Karem
    Apr 2, 2016 at 7:59
  • 3
    You need to use your private key in the SSL configuration of your endpoint. You don't send it anywhere, any time, ever. Neither does SSL. It just uses it to sign the certificate message sent to the peer.
    – user207421
    Apr 2, 2016 at 8:00
  • 1
    @EJP I meant "set" and not send actually, thanks for pointing out. When I configure CURL to use the test certificate + test private key I got from them, it works. When i configure CURL to use the certificate generated out from my CSR + my private key it doesn't work. So am I generating the signed CSR in a wrong way, since i suppose my error does not match?
    – Karem
    Apr 2, 2016 at 8:12

4 Answers 4

11

Not a definite answer but too much to fit in comments:

I hypothesize they gave you a cert that either has a wrong issuer (although their server could use a more specific alert code for that) or a wrong subject. We know the cert matches your privatekey -- because both curl and openssl client paired them without complaining about a mismatch; but we don't actually know it matches their desired CA(s) -- because your curl uses openssl and openssl SSL client does NOT enforce that a configured client cert matches certreq.CAs.

Do openssl x509 <clientcert.pem -noout -subject -issuer and the same on the cert from the test P12 that works. Do openssl s_client (or check the one you did) and look under Acceptable client certificate CA names; the name there or one of them should match (exactly!) the issuer(s) of your certs. If not, that's most likely your problem and you need to check with them you submitted your CSR to the correct place and in the correct way. Perhaps they have different regimes in different regions, or business lines, or test vs prod, or active vs pending, etc.

If the issuer of your cert does match desiredCAs, compare its subject to the working (test-P12) one: are they in similar format? are there any components in the working one not present in yours? If they allow it, try generating and submitting a new CSR with a subject name exactly the same as the test-P12 one, or as close as you can get, and see if that produces a cert that works better. (You don't have to generate a new key to do this, but if you choose to, keep track of which certs match which keys so you don't get them mixed up.) If that doesn't help look at the certificate extensions with openssl x509 <cert -noout -text for any difference(s) that might reasonably be related to subject authorization, like KeyUsage, ExtendedKeyUsage, maybe Policy, maybe Constraints, maybe even something nonstandard.

If all else fails, ask the server operator(s) what their logs say about the problem, or if you have access look at the logs yourself.

3
  • 1
    Thanks for your answer. I just checked. The issuer and subject is the same format for both the p12 (test-certificate that works) and the "live" client-certificate I am trying with. There is differences in both issuer and subject but this is only the data and not the format/structure. What's left from your answer is to try openssl s_client and see Acceptable client certificate. Will try if I can connect and check that right now.
    – Karem
    Apr 2, 2016 at 22:47
  • 1
    That was totally it! Different CA names (test vs prod)!
    – Karem
    Apr 2, 2016 at 23:55
  • @Karem, How did you solved the issue, did you mean you were using ssl certificates which had different domain e.g. sub.site.com vs bus.site.com?
    – Nono
    Nov 9, 2020 at 14:33
8

What SSL private key should be sent along with the client certificate?

None of them :)

One of the appealing things about client certificates is it does not do dumb things, like transmit a secret (like a password), in the plain text to a server (HTTP basic_auth). The password is still used to unlock the key for the client certificate, its just not used directly to during exchange or tp authenticate the client.

Instead, the client chooses a temporary, random key for that session. The client then signs the temporary, random key with his cert and sends it to the server (some hand waiving). If a bad guy intercepts anything, its random so it can't be used in the future. It can't even be used for a second run of the protocol with the server because the server will select a new, random value, too.


Fails with: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure

Use TLS 1.0 and above; and use Server Name Indication.

You have not provided any code, so its not clear to me how to tell you what to do. Instead, here's the OpenSSL command line to test it:

openssl s_client -connect www.example.com:443 -tls1 -servername www.example.com \
    -cert mycert.pem -key mykey.pem -CAfile <certificate-authority-for-service>.pem

You can also use -CAfile to avoid the “verify error:num=20”. See, for example, “verify error:num=20” when connecting to gateway.sandbox.push.apple.com.

7
  • 1
    Thanks for the explanation. I'm still yet confused. For testing, I received a .p12 that I extracted to get both certificate + private key file. This works perfect (!) when configured in curl (using openssl) at CURLOPT_SSLCERT + CURLOPT_SSLKEY. Now for the real client certificate that are generated out (on their end) from a CSR i generated on my server, I receive no private key from them and then I thought ok maybe I should use the private key I got when generating the CSR and put it as CURLOPT_SSLKEY. Didn't work (which lead me to this question).
    – Karem
    Apr 2, 2016 at 19:10
  • And now you and others say that private key should not be set at all. If I don't have a private key set (with CURLOPT_SSLKEY) i receive the error "unable to set private key file: " (it cant find a private key in the client certificate). So many frustrating hours and I just can't figure this out.
    – Karem
    Apr 2, 2016 at 19:11
  • I am using TLS 1.2 (and as said everything works with the test client certificate + test private key). I feel like I am missing something/I understood something very wrong.
    – Karem
    Apr 2, 2016 at 19:16
  • @Karem - You have the private key from when you created the CSR. Its not in the client certificate as that only has the public key. According to the OpenSSL command you showed: -keyout cert.key. Check the filesystem for cert.key.
    – jww
    Apr 2, 2016 at 20:01
  • Exactly! And that's the one I am using. The cert.key! I took the content of this and put into private_key.pem to run in the curl command (see the updated question) in --key parameter. But I still receive the error handshake failure.
    – Karem
    Apr 2, 2016 at 20:12
3

The solution for me on a CentOS 8 system was checking the System Cryptography Policy by verifying the /etc/crypto-policies/config reads the default value of DEFAULT rather than any other value.

Once changing this value to DEFAULT, run the following command:

/usr/bin/update-crypto-policies --set DEFAULT

Rerun the curl command and it should work.

0

In my case the cause of the error was that I only imported the certificate (e.g., cert1.pem) and the private key (e.g., privkey1.pem) into the keystore and to solve the issue, I had to import the full chain of certificates as well.
I got the following files from "Let's Encrypt" certification authority:

privkey1.pem
fullchain1.pem
chain1.pem
cert1.pem

So I did the following:

  1. Join the content of three certificate files together into a file named all.pem:
cat cert1.pem chain1.pem fullchain1.pem > all.pem
  1. Create my keystore containing the full certificate chain:
openssl pkcs12 -export -in all.pem -inkey privkey1.pem -out my_keystore.p12 -name mycertalias -CAfile chain1.pem -caname root
3
  • Do you mean the keystore of the server-machine? or on the client-machine? May 27, 2022 at 11:31
  • The keystore of the server. Continuing with the example command which I provided in my answer, my_keystore.p12 is the keystore file used by the server in order to provide HTTPS functionality to the clients. In my case it is a Java servlet container (Jetty). Jun 9, 2022 at 20:57
  • By the way, I am aware that this question is about using a client certificate in order to access a server but the error which I was getting is the same, so this may help someone even if the authentication "direction" is the opposite. Jun 9, 2022 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.