SSL_CTX_SET_CLIENT_CERT_CB(3ossl)

πŸ“¦
version 15.1 Β· 3 versions
, in freebsd 15.1

πŸ’… 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 x509 and pkey arguments, AND "1" must be returned. the certificate will be installed into ssl β€” 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 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. and it is the job of the client_cert_cb to 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_CTX object (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 the SSL_CTX object 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. only SSL_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 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