SSL_CTX_SET_CLIENT_HELLO_CB(3ossl)
๐ NAME ๐
So this is the SSL_CTX_SET_CLIENT_HELLO_CB(3ossl) page, bestie โ it lives in the OpenSSL 3ossl manual, and it's literally all about the callback squad for early server-side ClientHello processing โจ. The whole friend group here is:
SSL_CTX_set_client_hello_cb,SSL_client_hello_cb_fn,SSL_client_hello_isv2SSL_client_hello_get0_legacy_version,SSL_client_hello_get0_random,SSL_client_hello_get0_session_idSSL_client_hello_get0_ciphers,SSL_client_hello_get0_compression_methodsSSL_client_hello_get1_extensions_present,SSL_client_hello_get_extension_order,SSL_client_hello_get0_ext
All of them = "callback functions for early server-side ClientHello processing" ๐
๐ธ SYNOPSIS ๐ธ
typedef int (*SSL_client_hello_cb_fn)(SSL *s, int *al, void *arg);
void SSL_CTX_set_client_hello_cb(SSL_CTX *c, SSL_client_hello_cb_fn *f,
void *arg);
int SSL_client_hello_isv2(SSL *s);
unsigned int SSL_client_hello_get0_legacy_version(SSL *s);
size_t SSL_client_hello_get0_random(SSL *s, const unsigned char **out);
size_t SSL_client_hello_get0_session_id(SSL *s, const unsigned char **out);
size_t SSL_client_hello_get0_ciphers(SSL *s, const unsigned char **out);
size_t SSL_client_hello_get0_compression_methods(SSL *s,
const unsigned char **out);
int SSL_client_hello_get1_extensions_present(SSL *s, int **out,
size_t *outlen);
int SSL_client_hello_get_extension_order(SSL *s, uint16_t *exts,
size_t *num_exts);
int SSL_client_hello_get0_ext(SSL *s, unsigned int type, const unsigned char **out,
size_t *outlen);
๐ฆ DESCRIPTION ๐ฆ
โจ setting the callback โจ
SSL_CTX_set_client_hello_cb() sets the callback function, and that callback gets automatically called during the early stages of ClientHello processing on the server. Not later, not "whenever" โ early early. ๐
The argument you supply when setting the callback gets passed back to the callback at runtime, so you can smuggle your little arg in there like a note in a friendship bracelet ๐ซถ.
Now the important part, and I need you to focus: a callback that returns failure (0) will cause the connection to terminate, no cap. And if your callback returns failure, it should indicate what alert value is to be sent in the al parameter โ that's literally its job.
A callback may also return a negative value to suspend the handshake, and then the handshake function will return immediately. SSL_get_error(3) will return SSL_ERROR_WANT_CLIENT_HELLO_CB to indicate the handshake was suspended. ๐ญ
It's the ClientHello callback's job to store information about the state of the last call if you're gonna need it to continue. On the next call into the handshake function, the ClientHello callback will be called again, and if it returns success, normal handshake processing will continue from that point. Like a save point. Iconic behavior, honestly. ๐ฎ
๐ง SSLv2 said what now ๐ง
SSL_client_hello_isv2() tells you whether the ClientHello was carried in an SSLv2 record and is in the SSLv2 format.
The SSLv2 format has substantial differences from the normal SSLv3 format, including:
- it uses three bytes per cipher suite
- it does not allow extensions
Also, the SSLv2 format 'challenge' field is exposed via SSL_client_hello_get0_random(), padded to SSL3_RANDOM_SIZE bytes with zeros if needed. ๐ซง
And for SSLv2 format ClientHellos, SSL_client_hello_get0_compression_methods() returns a dummy list that only includes the null compression method โ because the SSLv2 format does not include a mechanism by which to negotiate compression. It's giving "we don't do that here" ๐.
๐ grabbing the actual fields ๐
SSL_client_hello_get0_random(), SSL_client_hello_get0_session_id(), SSL_client_hello_get0_ciphers(), and SSL_client_hello_get0_compression_methods() provide access to the corresponding ClientHello fields โ returning the field length and optionally setting an out pointer to the octets of that field. Simple, clean, slay. โจ
Similarly, SSL_client_hello_get0_ext() provides access to individual extensions from the ClientHello on a per-extension basis. For the provided wire protocol extension type value, the extension value and length are returned in the output parameters (if present).
๐ extension detective work ๐
SSL_client_hello_get1_extensions_present() can be used prior to SSL_client_hello_get0_ext(), so you can figure out which extensions are present in the ClientHello before you go querying for them. Very "check the group chat before you ask the question" of it. ๐
The out and outlen parameters are both required, and on success the caller must release the storage allocated for *out using OPENSSL_free(). Don't forget, bestie, or that's a leak ๐ฉ.
- The contents of
*outis an array of integers holding the numerical value of the TLS extension types in the order they appear in the ClientHello. *outlencontains the number of elements in the array.- In situations when the ClientHello has no extensions, the function will return success with
*outset toNULLand*outlenset to0.
๐ extension order, but make it no-alloc ๐
SSL_client_hello_get_extension_order() is similar to SSL_client_hello_get1_extensions_present(), but without internal memory allocation. Frugal queen ๐
.
- When called with
extsset toNULL, it returns the number of extensions (e.g., to allocate storage for a subsequent call). - Otherwise,
*extsis populated with theExtensionTypevalues in the order that the corresponding extensions appeared in the ClientHello. *num_extsis an input/output parameter: used as input to supply the size of storage allocated by the caller, and as output to indicate how manyExtensionTypevalues were written.- If the input
*num_extsis smaller than the number of extensions in question, that is treated as an error โ ๏ธ. - A subsequent call with
extsset toNULLcan retrieve the size of storage needed. - A ClientHello that contained no extensions is treated as success, with
*num_extsset to0.
๐ฅ NOTES ๐ฅ
The ClientHello callback provides a vast window of possibilities for application code to affect the TLS handshake. Like, the range? Unmatched. ๐
A primary use of the callback is to allow the server to examine the server name indication extension provided by the client, in order to select an appropriate certificate to present, and make other configuration adjustments relevant to that server name and its configuration. Such configuration changes can include:
- swapping out the associated
SSL_CTXpointer - modifying the server's list of permitted TLS versions
- changing the server's cipher list in response to the client's cipher list
- etc.
It is also recommended that applications utilize a ClientHello callback and not use a servername callback, in order to avoid unexpected behavior that occurs due to the relative order of processing between things like session resumption and the historical servername callback. Trust the process, use the new one ๐.
โ ๏ธ Real talk: the SSL_client_hello_* family of functions may only be called from code executing within a ClientHello callback. Nowhere else. Periodt.
๐ RETURN VALUES ๐
Your application's supplied ClientHello callback returns:
SSL_CLIENT_HELLO_SUCCESSon success โSSL_CLIENT_HELLO_ERRORon failure โSSL_CLIENT_HELLO_RETRYto suspend processing โธ๏ธ
SSL_client_hello_isv2() returns 1 for SSLv2-format ClientHellos and 0 otherwise.
SSL_client_hello_get0_random(), SSL_client_hello_get0_session_id(), SSL_client_hello_get0_ciphers(), and SSL_client_hello_get0_compression_methods() return the length of the corresponding ClientHello fields. And listen โ if zero is returned, the output pointer should not be assumed to be valid. Like, don't touch it, it's not real ๐.
SSL_client_hello_get0_ext() returns 1 if the extension of type 'type' is present, and 0 otherwise.
SSL_client_hello_get1_extensions_present() returns 1 on success and 0 on failure.
SSL_client_hello_get_extension_order() returns 1 on success and 0 on failure.
๐ SEE ALSO ๐
Go read them, bestie, they're the supporting cast ๐ซถ
๐ HISTORY ๐
The SSL ClientHello callback, SSL_client_hello_isv2(), SSL_client_hello_get0_random(), SSL_client_hello_get0_session_id(), SSL_client_hello_get0_ciphers(), SSL_client_hello_get0_compression_methods(), SSL_client_hello_get0_ext(), and SSL_client_hello_get1_extensions_present() were added in OpenSSL 1.1.1.
SSL_client_hello_get_extension_order() was added in OpenSSL 3.2.0. She's the newest member of the friend group ๐ฆ
๐ COPYRIGHT ๐
Copyright 2017-2022 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, diva! This masterpiece is from OpenSSL 3.5.0, dated 2026-09-25 โ go forth and process those ClientHellos early, iykyk ๐ โจ
๐ฆ 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