SSL_CTX_SET_CLIENT_CERT_CB(3ossl)

πŸ“¦ version 3.5.0, in opensuse-leap 16.0

πŸ’… SSL_CTX_SET_CLIENT_CERT_CB πŸ’…

ok so bestie, this is a page from the 3ossl manual β€” OpenSSL, if the "ossl" didn't give it away πŸŽ€ β€” and it's all about SSL_CTX_set_client_cert_cb and SSL_CTX_get_client_cert_cb, the two functions that handle the client certificate callback function. Yes, that's a mouthful. We're getting through it together. 🫢


✨ NAME ✨

SSL_CTX_set_client_cert_cb, SSL_CTX_get_client_cert_cb β€” the functions that handle the client certificate callback function πŸ’–


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <openssl/ssl.h>

void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
                                int (*client_cert_cb)(SSL *ssl, X509 **x509,
                                                      EVP_PKEY **pkey));
int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL *ssl, X509 **x509,
                                                EVP_PKEY **pkey);

like, look at that second one. it returns a function pointer. main character energy honestly πŸ‘‘


🌸 DESCRIPTION 🌸

SSL_CTX_set_client_cert_cb() sets the client_cert_cb callback, and that callback gets called when a server asks for a client certificate and there was no certificate already set for the SSL object. periodt.

When client_cert_cb is NULL, no callback function is used. simple as that. πŸ™ˆ

SSL_CTX_get_client_cert_cb() returns a pointer to whichever callback function is currently set. cute little getter, we love to see it πŸ’…

Now the good part β€” client_cert_cb is the application defined callback, and here's the vibe check on what it can do:

  • If it wants to set a certificate, it has to set a certificate/private key combo using the x509 and pkey arguments and return "1". The certificate will be installed into ssl β€” peep the NOTES and BUGS sections for the tea on that. β˜•
  • If no certificate should be set, it has to return "0" and no certificate will be sent. no notes.
  • A negative return value will suspend the handshake and the handshake function will return immediately. SSL_get_error(3) will return SSL_ERROR_WANT_X509_LOOKUP to indicate that the handshake was suspended. The next call to the handshake function will again lead to the call of client_cert_cb. So it's literally the job of the client_cert_cb to store information about the state of the last call if it needs to continue. keep your receipts, bestie 🧾

πŸŽ€ NOTES πŸŽ€

During a handshake (or renegotiation β€” throwback era, ik), a server may request a certificate from the client. And a client certificate must only be sent when the server did send the request. rules are rules.

When a certificate was set using the SSL_CTX_use_certificate(3) family of functions, it will be sent to the server. BUT β€” and this is the part nobody talks about β€” the TLS standard requires that only a certificate is sent if it matches the list of acceptable CAs sent by the server. This constraint is violated by the default behavior of the OpenSSL library. 😭 yeah. the default just does not care. Using the callback function it's possible to implement a proper selection routine, or to allow a user interaction to choose the certificate to be sent. so basically the callback is your glow-up path ✨

If a callback function is defined and no certificate was yet defined for the SSL object, the callback function will be called. If the callback function returns a certificate, the OpenSSL library will try to load the private key and certificate data into the SSL object using the SSL_use_certificate() and SSL_use_private_key() functions. Which means it will permanently install the certificate and key for this SSL object. It will not be reset by calling SSL_clear(3). 🚨 permanent means permanent. If the callback returns no certificate, the OpenSSL library will not send a certificate.


⭐ RETURN VALUES ⭐

SSL_CTX_get_client_cert_cb() returns the function pointer of client_cert_cb, or NULL if the callback is not set. that's it, that's the whole section πŸ’œ


πŸ’€ BUGS πŸ’€

ok so this is where it gets real, and I need you to stay with me:

  • The client_cert_cb cannot return a complete certificate chain β€” it can only return one client certificate. If the chain only has a length of 2, the root CA certificate may be omitted according to the TLS standard, and thus a standard conforming answer can be sent to the server. For a longer chain, the client must send the complete chain (with the option to leave out the root CA certificate). This can only be accomplished by either:
    1. adding the intermediate CA certificates into the trusted certificate store for the SSL_CTX object β€” which results in having to add CA certificates that otherwise maybe would not be trusted, or
    2. adding the chain certificates using the SSL_CTX_add_extra_chain_cert(3) function, which is only available for the SSL_CTX object as a whole and therefore probably can only apply for one client certificate β€” making the concept of the callback function (to allow the choice from several certificates) questionable. πŸ™ƒ yeah. questionable. that's the word they used.
  • Once the SSL object has been used in conjunction with the callback function, the certificate will be set for the SSL object and will not be cleared even when SSL_clear(3) is being called. It is therefore mandatory to destroy the SSL object using SSL_free(3) and create a new one to return to the previous state.
🚨 fr fr on that last one: if you're relying on SSL_clear(3) to reset the cert, you're gonna have a bad time. SSL_free(3) and start fresh. no shortcuts, bestie.

🌈 SEE ALSO 🌈

ssl(7), SSL_CTX_use_certificate(3), SSL_CTX_add_extra_chain_cert(3), SSL_get_client_CA_list(3), SSL_clear(3), SSL_free(3)

like a little friend group 🫢 go read them, they're all slay.


Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.


πŸŽ€ and that's the page!! this was version 3.5.0, dated 2026-09-25, from the SSL_CTX_SET_CLIENT_CERT_CB(3ossl) manual. mwah, go forth and pick your certs wisely πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from