SSL_GET_SESSION(3)

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

πŸ’… NAME πŸ’…

SSL_get_session, SSL_get0_session, SSL_get1_session β€” bestie, these are the three lil functions that grab you the TLS/SSL session data, and honestly? they're giving "keys to the castle" πŸ”‘

So this page is from the Library Functions Manual (libssl girlies, we love you), and it's all about pulling the SSL_SESSION out of a connection. Let's get into it ✨


🌸 SYNOPSIS 🌸

library β€œlibssl” libcrypto

#include <openssl/ssl.h>

SSL_SESSION *
SSL_get_session(const SSL *ssl);

SSL_SESSION *
SSL_get0_session(const SSL *ssl);

SSL_SESSION *
SSL_get1_session(SSL *ssl);

Three functions, one header, no cap πŸ’–


πŸ¦‹ DESCRIPTION πŸ¦‹

✨ the basics, bestie ✨

  • SSL_get_session() β€” hands you a pointer to the SSL_SESSION that's actually used in ssl. But heads up: the reference count of that SSL_SESSION is not incremented, so like… that pointer can literally become invalid because of other operations. It's giving "borrowed, not yours" πŸ™ˆ
  • SSL_get0_session() β€” same as SSL_get_session(). Literally twins. Same energy πŸ‘―
  • SSL_get1_session() β€” also same as SSL_get_session(), except the reference count of the SSL_SESSION is incremented by one. Now it's actually yours to keep πŸ”₯

And what even is this ssl session, you ask? It contains all the information required to re-establish the connection without a new handshake. That's the whole vibe: no renegotiation, just reconnection. Iconic πŸŽ€

🧚 the pointer lore (read this part fr) 🧚

SSL_get0_session() returns a pointer to the actual session. Since the reference counter is not incremented, that pointer is only valid while the connection is in use. That's the fine print, and I need you to respect it.

So if SSL_clear(3) or SSL_free(3) gets called, the session may be removed completely (if it's considered bad), and the pointer you got will become invalid. Poof. Gone. πŸ’€

And even if the session is valid, it can be removed at any time due to a timeout during SSL_CTX_flush_sessions(3). Like, the session doesn't even have to do anything wrong. Rude, but that's the rules 😭

πŸ‘‘ keeping your data πŸ‘‘

If the data is to be kept, SSL_get1_session() will increment the reference count, so the session will not be implicitly removed by other operations β€” it just stays in memory, chilling. 🫢

But then to actually remove it, SSL_SESSION_free(3) must be explicitly called once, to decrement the reference count again. Ball's in your court, bestie. You gotta clean up after yourself πŸ’…

🚨 the one-SSL_CTX rule (this is important) 🚨

SSL_SESSION objects keep internal link information about the session cache list when being inserted into one SSL_CTX object's session cache.

Which means, and I'm saying this super clearly because it matters:

One SSL_SESSION object, regardless of its reference count, must therefore only be used with one SSL_CTX object (and the SSL objects created from this SSL_CTX object).

One session = one context. No sharing. Don't be messy with it πŸ™…β€β™€οΈ


🌈 RETURN VALUES 🌈

The following return values can occur:

  • NULL β€” There is no session available in ssl. It's giving nothing, because there is nothing 😭
  • Pointer to an SSL_SESSION β€” The return value points to the data of an SSL session. Slay, you got one πŸ’–

πŸ“ SEE ALSO πŸ“

ssl(3), SSL_clear(3), SSL_free(3), SSL_SESSION_free(3), SSL_SESSION_get0_peer(3), SSL_SESSION_get_compress_id(3), SSL_SESSION_get_id(3), SSL_SESSION_get_protocol_version(3), SSL_SESSION_get_time(3), SSL_SESSION_new(3), SSL_SESSION_print(3), SSL_set_session(3)

That's the whole squad, go read them too ✨


⭐ HISTORY ⭐

  • SSL_get_session() first appeared in SSLeay 0.5.2 and has been available since OpenBSD 2.4. Elder icon behavior πŸ‘‘
  • SSL_get0_session() and SSL_get1_session() first appeared in OpenSSL 0.9.5 and have been available since OpenBSD 2.7. The younger siblings finally arrived πŸ¦‹

okay that's the whole page, bestie!! πŸ’œ This was the Debian build, dated June 8, 2025 β€” so like, fresh off the press, and yes it says Library Functions Manual up top which is why we're here gossiping about it πŸŽ€ Now go get your session and hold onto that refcount like it's your last iced coffee πŸ§ŠπŸ’…

πŸ“¦ where this page comes from