LIBRESSL-SSL_CTX_SET_TLSEXT_SERVERNAME_CALLBACK(3)

πŸ“¦ version 4.4.0, in alpine 3.24, arch rolling

πŸ’… SSL_CTX_SET_TLSEXT_SERVE(3) β€” the SNI glow-up

okay so bestie, welcome to the LibreSSL girlies' corner of the manual (yes, that's the libssl / libcrypto library functions manual, and yes we're doing this). This one's all about server name indication, aka SNI, aka the thing that lets one server hold a million vibes at once. ✨


🌸 NAME 🌸

SSL_CTX_set_tlsext_servername_callback, SSL_CTX_set_tlsext_servername_arg, SSL_get_servername_type, SSL_get_servername, SSL_set_tlsext_host_name β€” handling server name indication (SNI). no cap, that's the whole squad. 🫢


πŸŽ€ SYNOPSIS πŸŽ€

library β€œlibssl” libcrypto #include <openssl/ssl.h>

long
SSL_CTX_set_tlsext_servername_callback(SSL_CTX *ctx,
    int (*cb)(SSL *ssl, int *alert, void *arg));

long
SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg);

const char *
SSL_get_servername(const SSL *ssl, const int type);

int
SSL_get_servername_type(const SSL *ssl);

int
SSL_set_tlsext_host_name(SSL *ssl, const char *name);

✨ DESCRIPTION ✨

πŸ¦‹ the callback situation

SSL_CTX_set_tlsext_servername_callback() sets the application callback cb β€” this is the little function a server uses to do any actions or config it needs based on the servername extension it got in the incoming connection. like, oh, you're asking for that hostname? bet, let me reconfigure real quick. πŸ’…

It's giving the same energy as the ALPN callback: it is executed during Client Hello processing. And the details matter β€” when cb is NULL, SNI is not used. periodt.

🌈 what your callback should return

The servername callback should return one of these, bestie:

  • SSL_TLSEXT_ERR_OK β€” used to indicate the servername the client asked for has been accepted. Typically a server will call SSL_set_SSL_CTX(3) inside the callback to set up a different configuration for the selected servername in this case. main character server energy, honestly. πŸ‘‘
  • SSL_TLSEXT_ERR_ALERT_FATAL β€” the servername requested by the client is not accepted and the handshake gets aborted. the alert value to use should be stored in the location pointed to by the alert parameter to the callback. by default this value is initialised to SSL_AD_UNRECOGNIZED_NAME. it's giving "sorry, we don't know her." πŸ’€
  • SSL_TLSEXT_ERR_ALERT_WARNING β€” if you return this, the servername is not accepted by the server, but the handshake continues and sends a warning alert instead. store the alert value in the location pointed to by the alert parameter exactly like with SSL_TLSEXT_ERR_ALERT_FATAL above. > ⚠️ but heads up β€” TLSv1.3 does not support warning alerts, so if TLSv1.3 has been negotiated then this return value is treated the same way as SSL_TLSEXT_ERR_NOACK. lowkey rude of TLSv1.3 but okay.
  • SSL_TLSEXT_ERR_NOACK β€” indicates the servername is not accepted by the server. no alerts are sent and the server will not acknowledge the requested servername. silent treatment, but make it protocol. πŸ™ˆ

πŸ“ the arg, the getters, and the setter

SSL_CTX_set_tlsext_servername_arg() sets a context-specific argument to be passed into the callback via the arg parameter for ctx. little carry-on luggage for your callback, love that for you. πŸŽ€

SSL_set_tlsext_host_name() sets the server name indication ClientHello extension to contain the value name, or clears it if name is NULL. the type of server name indication extension is set to TLSEXT_NAMETYPE_host_name as defined in RFC 3546. iconic spec behaviour.

πŸ’‘ plot twist: all three of those functions are implemented as macros. not functions. macros. don't come for me, that's just what the page says.

🌟 RETURN VALUES 🌟

  • SSL_CTX_set_tlsext_servername_callback() and SSL_CTX_set_tlsext_servername_arg() β€” always return 1, indicating success. always slaying, never failing. πŸ’–
  • SSL_get_servername() β€” returns a servername extension value of the specified type if provided in the Client Hello, or NULL otherwise.
  • SSL_get_servername_type() β€” returns the servername type, or -1 if no servername is present. currently the only supported type (defined in RFC 3546) is TLSEXT_NAMETYPE_host_name. iykyk.
  • SSL_set_tlsext_host_name() β€” returns 1 on success or 0 in case of an error. simple. clean. no notes. ✨

🧚 SEE ALSO 🧚

ssl(3), SSL_CTX_callback_ctrl(3), SSL_CTX_set_alpn_select_cb(3) β€” go read these next, bestie, they're literally in the same friend group. πŸ¦‹


⭐ HISTORY ⭐

These functions first appeared in OpenSSL 0.9.8f and have been available since OpenBSD 4.5. vintage, but make it functional. πŸ”₯


okay that's the whole page, you ate that read πŸ’… β€” this little love letter is from the Debian build of the LibreSSL manual, dated June, and it's the SSL_CTX_SET_TLSEXT_SERVERNAME_CALLBACK(3) page, so go forth and SNI responsibly. mwah! πŸŽ€βœ¨

πŸ“¦ where this page comes from