SSL_GET_SESSION(3ossl)

✨ SSL_GET_SESSION β€” the 3ossl manual, but make it cute ✨

okay bestie, welcome to SSL_GET_SESSION(3ossl), the page where we learn how to retrieve TLS/SSL session data πŸ’–


🌸 NAME 🌸

SSL_get_session, SSL_get0_session, SSL_get1_session β€” retrieve TLS/SSL session data πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

#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);

πŸ’… DESCRIPTION πŸ’…

okay so. SSL_get_session() returns a pointer to the SSL_SESSION that was actually used in ssl. and here's the tea: the reference count of that SSL_SESSION is NOT incremented, which means that pointer can lowkey become invalid the moment other operations happen 😭 like, it's giving "borrowed, not owned."

SSL_get0_session() is literally the same as SSL_get_session(). same function, different name, no notes πŸ’œ

SSL_get1_session() is also the same as SSL_get_session(), but β€” and this is the important part β€” the reference count of the SSL_SESSION gets incremented by one. so now you own a piece of it, bestie 🫢


πŸ‘‘ NOTES πŸ‘‘

okay this is the part where you actually need to pay attention, fr fr.

the ssl session contains all the information required to re-establish the connection without a full handshake β€” for SSL versions up to and including TLSv1.2 🌈

in TLSv1.3 the same thing is true, but sessions are established after the main handshake has already occurred. the server will send the session information to the client at a time of its choosing, which may be a while after the initial connection is established β€” or, like, never πŸ™ˆ. so if you call these functions on the client side in TLSv1.3 before the session has been established, you'll still get an SSL_SESSION object back, but that object cannot be used for resuming the session πŸ’€. if you wanna know whether an SSL_SESSION object can be used for resumption or not, go read SSL_SESSION_is_resumable(3) β€” it's the vibe check you're looking for ✨

additionally, in TLSv1.3, a server can send multiple messages that establish a session for a single connection πŸ‘€. in that case, on the client side, the above functions will only return information on the last session that was received. on the server side they will only return information on the last session that was sent β€” or, if no session tickets were sent, then the session for the current connection. slay, but also kind of limiting πŸ’…

the preferred way for applications to obtain a resumable SSL_SESSION object is to use a new session callback, as described in SSL_CTX_sess_set_new_cb(3) πŸŽ€. the new session callback is only invoked when a session is actually established, so this avoids the whole problem described above where an application obtains an SSL_SESSION object that cannot be used for resumption in TLSv1.3. bonus: it also lets applications obtain information about all sessions sent by the server πŸ‘‘

a session will be automatically removed from the session cache and marked as non-resumable if the connection is not closed down cleanly 😭 β€” like, for example, if a fatal error occurs on the connection, or if SSL_shutdown(3) is not called prior to SSL_free(3). so be nice to your connections, bestie.

in TLSv1.3 it is recommended that each SSL_SESSION object is only used for resumption once. one and done, main character energy ⭐

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. if SSL_clear(3) or SSL_free(3) is called, the session may be removed completely (if it's considered bad πŸ‘Ž), and the pointer you obtained will become invalid. and even if the session is valid, it can be removed at any time due to timeout during SSL_CTX_flush_sessions(3) 🫠

if you want to keep the data, SSL_get1_session() will increment the reference count, so that the session will not be implicitly removed by other operations but stays in memory 🧚. in order to remove the session, SSL_SESSION_free(3) must be explicitly called once to decrement the reference count again. periodt.

and now for a warning that is genuinely important, no drama:

SSL_SESSION objects keep internal link information about the session cache list when being inserted into one SSL_CTX object's session cache. 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). do not share, bestie. it's not that kind of relationship πŸ’€

πŸ”₯ RETURN VALUES πŸ”₯

the following return values can occur:

  • NULL β€” there is no session available in ssl 😭
  • Pointer to an SSL_SESSION β€” the return value points to the data of an SSL session ✨

🌈 SEE ALSO 🌈

ssl(7), SSL_free(3), SSL_clear(3), SSL_SESSION_free(3)


Copyright 2000-2018 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, bestie 🫢 this was 3.5.9, dated 2026-09-29, and it's the SSL_GET_SESSION(3ossl) manual β€” now go increment your reference counts responsibly πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from