SSL_CTX_SET_CLIENT_CERT_CB(3ossl)
π SSL_CTX_SET_CLIENT_CERT_CB π
okay so this is an OpenSSL 3ossl situation and it's literally all about the client certificate callback function β SSL_CTX_set_client_cert_cb and SSL_CTX_get_client_cert_cb, bestie. it's giving "who sends the cert, and when" energy π
β¨ 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 return type for the getter. she's a function-pointer-returning-function and she knows it π
πΈ DESCRIPTION πΈ
So 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 no certificate was yet set for the SSL object. peak "oh you need my ID? hold on" vibes π
When client_cert_cb is NULL, no callback function is used. simple as that, no drama π
SSL_CTX_get_client_cert_cb() returns a pointer to the currently set callback function. literally just handing you back what's already there π«Ά
client_cert_cb is the application defined callback. here's the rules, no cap:
- if it wants to set a certificate, a certificate/private key combination must be set using the
x509andpkeyarguments, AND"1"must be returned. the certificate will be installed intosslβ see the NOTES and BUGS sections, they're important fr fr. - if no certificate should be set,
"0"has to be returned and no certificate will be sent. - 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, you keep the receipts, bestie π
π¦ NOTES π¦
During a handshake (or renegotiation) a server may request a certificate from the client. a client certificate must only be sent when the server did send the request. we don't volunteer info unasked, periodt β¨
When a certificate was set using the SSL_CTX_use_certificate(3) family of functions, it will be sent to the server. BUT β the TLS standard requires that only a certificate is sent if it matches the list of acceptable CAs sent by the server. and this constraint is violated by the default behavior of the OpenSSL library π¬ (yes, the default is lowkey out of pocket, we said what we said). 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. main character behavior: you pick π
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. so 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. ngl "permanently" is doing a lot of heavy lifting in that sentence π
β RETURN VALUES β
SSL_CTX_get_client_cert_cb() returns function pointer of client_cert_cb or NULL if the callback is not set. that's it, that's the whole return value section π
π BUGS π
okay this is the part where we sit down. πͺ
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). and 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 - by 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. she said it. π
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. no shortcuts, no workarounds, you will be freeing that object bestie π₯
warning bestie: that last one is not a suggestion.SSL_clear(3)will not undo this. onlySSL_free(3)+ a fresh object gets you back. π¨
π 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)
π 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 whole page, bestie!! π this was OpenSSL 3.5.6, dated 2026-04-07, manual page SSL_CTX_SET_CLIENT_CERT_CB(3ossl) β go forth and pick your certs wisely π§π
π¦ where this page comes from
- freebsd 15.1: base 15.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms