1

I'm trying to load multiple certificates into an SSL_CTX.

Looking at the documentation, I was able to establish SSL connection using these 2 ways:

  1. Create an X509_STORE, add certificates to the store, and then load the cert store into the SSL_CTX using SSL_CTX_set_cert_store.
  2. Call SSL_CTX_use_certificate(ctx, cert) multiple times

Is there a difference between these two? I saw on StackOverflow somewhere that SSL_CTX_use_certificate does not work with self signed certs? (Loading CA certificate from memory) Why? I don't see this on the documentation anywhere. (What does it mean to be self signed?)

*also for #2, does calling SSL_CTX_use_certificate multiple times replace the existing certificate? Would I need to call SSL_CTX_add_extra_chain_cert?

Panda
  • 33
  • 6
  • 1
    "What does it mean to be self signed?" Any certificate is signed. The signature is computed by another certificate (more precisely by the private key attached to some other certificate). A public global certificate is signed by a known CA (with possible intermediate certificates). A local certificate is signed by itself, it acts as its own CA, and that one can be generated by anyone anytime. So it is self signed. – Patrick Mevzek Dec 06 '19 at 16:25

1 Answers1

4

The X509_STORE is used for building the certificate trust chain during certificate validation. Thus, any certificates added by X509_STORE_add_cert are used when validating the peer certificate.

SSL_CTX_use_certificate instead is used to set the local certificate used for authentication against the peer, i.e. this is to set the server certificate at the server and the client certificate at the client. It must be accompanied by a function to set the private key, like SSL_CTX_use_PrivateKey. SSL_CTX_use_certificate can be called multiple times and will either replace the existing certificate or add another one: i.e. one might have both an RSA and a ECDSA certificate at the same time with newer versions of OpenSSL.

SSL_CTX_use_certificate does not work with self signed certs?

OpenSSL does not care if the certificate is self-signed or not when using SSL_CTX_use_certificate. The communication peer which receives the certificate as authentication will hopefully care though and might complain since no local trust anchor is found to validate the certificate.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • Thanks for the response. Question about this part "It must be accompanied by a function to set the private key". If I am only doing server authentication only (no mutual TLS), is the private key still necessary to accompany the local certificate on the client side? I was able to establish SSL connection without one. The setup was like this: [Client] loads CA certificate, VERIFY_PEER mode | [Server] loads private key and public certificate issued by the CA cert that was loaded into the client, VERIFY_NONE mode – Panda Dec 06 '19 at 16:44
  • 1
    @Panda: if you don't do mutual authentication you don't need to set a certificate for authentication in the client at all, i.e. not only no `SSL_CTX_use_PrivateKey` is needed but no `SSL_CTX_use_certificate` either. – Steffen Ullrich Dec 06 '19 at 17:25
  • I don't understand. Even if it's not mutual authentication and server authentication only, how does the client verify that the certificate passed by the server is valid? Doesn't the client need to have some sort of certificate loaded? Or be aware of what the CA cert is to validate the server's cert against it? (https://www.ibm.com/support/knowledgecenter/en/SSFKSJ_7.1.0/com.ibm.mq.doc/sy10670_.htm under server authentication only) – Panda Dec 06 '19 at 18:07
  • 1
    @Panda: The peers certificate is validated against the CA certificates in the `X509_STORE`. As I said `SSL_CTX_use_certificate` is not used to put certificates in that store but `X509_STORE_add_cert` is. – Steffen Ullrich Dec 06 '19 at 19:29
  • Got it. I was confused because I was able to establish an SSL connection somehow with the client loading neither the CA cert nor the peer cert. Any idea why this is happening? – Panda Dec 06 '19 at 19:45
  • 1
    @Panda: I think you actual question about the difference was answered. In your comment your currently ask a new question which should be a) asked as a separate question and b) provide the actual code which is needed to reproduce your problem and understand what you are actually doing. – Steffen Ullrich Dec 06 '19 at 19:52
  • Sorry, will do. Thanks – Panda Dec 06 '19 at 20:24
  • I figured it out if anyone else is running into the same issue.. I was playing around with the verifyCallback function and it was always returning true.. – Panda Dec 06 '19 at 20:48