SSL_CTX_SET_CLIENT_CERT_CB(3ossl)
π SSL_CTX_set_client_cert_cb β the OpenSSL bestie that handles your client cert callback π
okay so bestie, we're in the OpenSSL manual pages (that's the 3ossl collection, iykyk) and today's main character is the pair of functions that let you decide which client certificate goes out during a handshake. it's giving choose-your-own-adventure but for TLS. no cap.
β¨ NAME β¨
SSL_CTX_set_client_cert_cb, SSL_CTX_get_client_cert_cb β handle client certificate callback function
like literally, that's the whole vibe. set it, get it, slay.
πΈ 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);
look at that function pointer return type on the getter. she's serving "return a pointer to a function pointer" energy and honestly? iconic.
π¦ DESCRIPTION π¦
SSL_CTX_set_client_cert_cb() sets the client_cert_cb callback. that callback gets called when a server asks for a client certificate and, plot twist, no certificate was set for the SSL object yet. it's the "oops nobody told me who i am" moment and this callback is the rescue.
when client_cert_cb is NULL, no callback function is used. (so like, the vibes are off and nothing happens.)
SSL_CTX_get_client_cert_cb() returns a pointer to the currently set callback function. she's nosy like that, in a cute way.
now, client_cert_cb is the application defined callback. and here's the important part, bestie, so pay attention:
- if it wants to set a certificate, a certificate/private key combo must be set using the
x509andpkeyarguments and"1"must be returned. the certificate will be installed intossl, see the NOTES and BUGS sections (yes, we're going there, buckle up). - if no certificate should be set,
"0"has to be returned and no certificate will be sent. respecting the boundary, we love to see it. - 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. and it is the job of theclient_cert_cbto store information about the state of the last call, if required to continue. so like, take notes babe, she will not remember for you.
π NOTES π
during a handshake (or renegotiation) a server may request a certificate from the client. and a client certificate must only be sent when the server did send the request. don't be out here sending unsolicited certs, that's not the move.
when a certificate was set using the SSL_CTX_use_certificate(3) family of functions, it will be sent to the server. 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. so using the callback function it is possible to implement a proper selection routine, or to allow a user interaction to choose the certificate to be sent. that's the glow-up.
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. thus it will permanently install the certificate and key for this SSL object. it will not be reset by calling SSL_clear(3). if the callback returns no certificate, the OpenSSL library will not send a certificate.
permanent. installed. no take-backs. periodt.
β RETURN VALUES β
SSL_CTX_get_client_cert_cb() returns function pointer of client_cert_cb, or NULL if the callback is not set.
π BUGS π
okay this is the messy part and we're being honest about it:
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:
- adding the intermediate CA certificates into the trusted certificate store for the
SSL_CTXobject (resulting 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 that 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. she said it, not me. π
and second thing:
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.
β οΈ bestie this is not optional.SSL_clear(3)will NOT undo it. you gottaSSL_free(3)and start fresh. no shortcuts, no vibes-based workarounds.
π 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)
the whole friend group, go say hi π«Ά
π 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, bestie π
this was OpenSSL 3.5.9, dated 2026-09-29, filed under SSL_CTX_SET_CLIENT_CERT_CB(3ossl). go forth and set your callback responsibly, mwah πβ¨
π¦ where this page comes from
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms