SSL_CTX_SET_CLIENT_CERT_CB(3ossl)
π 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
x509andpkeyarguments and return"1". The certificate will be installed intosslβ 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 returnSSL_ERROR_WANT_X509_LOOKUPto indicate that the handshake was suspended. The next call to the handshake function will again lead to the call ofclient_cert_cb. So it's literally the job of theclient_cert_cbto 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_cbcannot 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:- adding the intermediate CA certificates into the trusted certificate store for the
SSL_CTXobject β which results in having to add CA certificates that otherwise maybe would not be trusted, or - adding the chain certificates using the
SSL_CTX_add_extra_chain_cert(3)function, which is only available for theSSL_CTXobject 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.
- adding the intermediate CA certificates into the trusted certificate store for the
- Once the
SSLobject has been used in conjunction with the callback function, the certificate will be set for theSSLobject and will not be cleared even whenSSL_clear(3)is being called. It is therefore mandatory to destroy theSSLobject usingSSL_free(3)and create a new one to return to the previous state.
π¨ fr fr on that last one: if you're relying onSSL_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 π
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
- opensuse-leap 16.0: openssl-3-doc 3.5.0-160000.11.1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms