SSL_GET_SESSION(3)
π 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 theSSL_SESSIONthat's actually used inssl. But heads up: the reference count of thatSSL_SESSIONis not incremented, so likeβ¦ that pointer can literally become invalid because of other operations. It's giving "borrowed, not yours" πSSL_get0_session()β same asSSL_get_session(). Literally twins. Same energy π―SSL_get1_session()β also same asSSL_get_session(), except the reference count of theSSL_SESSIONis 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 ifSSL_clear(3)orSSL_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:
OneSSL_SESSIONobject, regardless of its reference count, must therefore only be used with oneSSL_CTXobject (and theSSLobjects created from thisSSL_CTXobject).
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 inssl. 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()andSSL_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
- alpine 3.24: libressl-doc 4.3.1-r0 Β· custom
- arch rolling: libressl 4.4.0-1 Β· ISC AND custom:OpenSSL
- Copyright (c) 2000, 2001, 2005, 2013, 2016 The OpenSSL Project
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms