LIBRESSL-SSL_CTX_SET_SESSION_ID_CONTEXT(3)

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

πŸ’– LIBRESSL-SSL_CTX_SET_SESSION_ID_CONTEXT(3) πŸ’–

okay so bestie, this one's from the Library Functions Manual for libssl β€” specifically SSL_CTX_set_session_id_context and SSL_set_session_id_context, and it's lowkey about telling your server "hey, these sessions belong to me" so nobody else's sessions wander into your house uninvited 🏠✨

No cap, it's a server-side-only situation, so if you're out here acting as a client, this page is not your main character moment πŸ™ˆ


✨ SYNOPSIS ✨

bestie. the vibes. it's libssl and libcrypto energy, and you're pulling in:

#include <openssl/ssl.h>

then you've got these two icons πŸ’…

int
SSL_CTX_set_session_id_context(SSL_CTX *ctx,
    const unsigned char *sid_ctx, unsigned int sid_ctx_len);

int
SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx,
    unsigned int sid_ctx_len);

🌸 DESCRIPTION 🌸

So SSL_CTX_set_session_id_context() sets the context sid_ctx of length sid_ctx_len β€” and that's the context within which a session can be reused for the ctx object. Slay πŸŽ€

And SSL_set_session_id_context() does the exact same little dance, except it sets the context sid_ctx of length sid_ctx_len within which a session can be reused for the ssl object. Periodt πŸ’œ

Sessions are generated within a certain context, okay?

And here's why that matters, because this is the part where things could go feral 😭 β€” when you're exporting/importing sessions with i2d_SSL_SESSION(3) and d2i_SSL_SESSION(3), it would be possible to re-import a session that was generated from another context (e.g., another application), and that might lead to malfunctions. Not cute. Not iconic. πŸ’€

So therefore, every application must set its own session id context sid_ctx, which is what's used to distinguish the contexts, and which gets stored in exported sessions. It's literally the wristband at the club that proves you belong, iykyk πŸ”₯

The sid_ctx can be any kind of binary data with a given length β€” so you can absolutely use, for instance, the name of the application, the hostname, the service name... go off, be creative 🌈

The session id context becomes part of the session. And it's set by the SSL/TLS server. So SSL_CTX_set_session_id_context() and SSL_set_session_id_context() are therefore only useful on the server side. Client-side besties, this page isn't for you and that's okay 🫢

Oh and also β€” OpenSSL clients will check the session id context returned by the server when reusing a session. They're watching πŸ‘€

And one little limit, bestie: the maximum length of the sid_ctx is limited to SSL_MAX_SSL_SESSION_ID_LENGTH. That's the ceiling, no negotiating πŸ’…


⚠️ WARNINGS ⚠️

okay this is the serious part, fr fr, listen up 🚨

If the session id context is not set on an SSL/TLS server and client certificates are used, stored sessions will not be reused β€” instead a fatal error will be flagged and the handshake will fail. So like. don't skip this step. It will not be a slay 😭

And if a server returns a different session id context to an OpenSSL client when reusing a session, an error will be flagged and the handshake will fail. OpenSSL servers will always return the correct session id context though, since an OpenSSL server checks the session id context itself before reusing a session, as described above. So the OpenSSL-server girlies are safe πŸ’–


πŸ¦‹ RETURN VALUES πŸ¦‹

SSL_CTX_set_session_id_context() and SSL_set_session_id_context() return the following values:

  • 0 β€” The length sid_ctx_len of the session id context sid_ctx exceeded the maximum allowed length of SSL_MAX_SSL_SESSION_ID_LENGTH. The error is logged to the error stack. πŸ’€
  • 1 β€” The operation succeeded. we love to see it ✨

πŸ‘‘ SEE ALSO πŸ‘‘

go read those too, bestie, cross-referencing is self-care πŸŽ€


🧚 HISTORY 🧚

  • SSL_set_session_id_context() first appeared in OpenSSL 0.9.2b.
  • SSL_CTX_set_session_id_context() first appeared in OpenSSL 0.9.3.
  • Both functions have been available since OpenBSD 2.6. vintage icon behavior πŸ’…β­

so yeah, that's the whole page 🌸 set your session id context, keep your sessions in their own lane, and don't let a fatal handshake error ruin your day πŸ˜­πŸ’–

Debian / June 8, 2025 / SSL_CTX_SET_SESSION_ID_CONTEXT(3) β€” mwah, kisses, go forth and configure responsibly, bestie πŸ«ΆπŸ’œπŸŽ€

πŸ“¦ where this page comes from