SSL_GET_SESSION(3SSL)

πŸ’– SSL_GET_SESSION πŸ’–

okay bestie, we're diving into SSL_GET_SESSION(3SSL) β€” that's the manual page, bestie, part of the OpenSSL crew, and it's literally all about retrieving TLS/SSL session data. yes queen, we're going there ✨


✨ NAME ✨

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


🌸 SYNOPSIS 🌸

like, here's the whole vibe, copy-paste ready:

#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, three functions, one job, and the difference is literally just the reference count. let's break it down fr fr:

  • SSL_get_session() β€” returns a pointer to the SSL_SESSION actually used in ssl. and bestie, hear me: the reference count of the SSL_SESSION is not incremented, so that pointer can become invalid by other operations. it's giving borrowed, not gifted πŸ’€
  • SSL_get0_session() β€” it's the same as SSL_get_session(). that's it. that's the function. iconic minimalism 🫢
  • SSL_get1_session() β€” also the same as SSL_get_session(), BUT the reference count of the SSL_SESSION is incremented by one. so this one's the one that actually holds on πŸ”₯

πŸ“ NOTES πŸ“

alright, pull up a chair, this is the long part but it's important, no cap 🧚

the ssl session contains all 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 is true, but sessions are established after the main handshake has occurred. the server will send the session information to the client at a time of its choosing β€” which may be some while after the initial connection is established (or never, if the server is being like that πŸ™ˆ).

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 lowkey that object cannot be used for resuming the session. to figure out whether an SSL_SESSION object can be used for resumption or not, go read SSL_SESSION_is_resumable(3). it's got the answers you need πŸ’œ

also, and this is the tea: 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.

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. plus it also enables applications to obtain information about all sessions sent by the server. obsessed with that for real ✨


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

and in TLSv1.3 it is recommended that each SSL_SESSION object is only used for resumption once. periodt.

okay so 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). so like... don't hold onto it, okay? πŸ™ˆ

if the data is to be kept, SSL_get1_session() will increment the reference count, so that the session will not be implicitly removed by other operations but stays in memory. to remove the session, SSL_SESSION_free(3) must be explicitly called once to decrement the reference count again. one call, one decrement, that's the deal πŸ’…

and one more thing β€” SSL_SESSION objects keep internal link information about the session cache list when being inserted into one SSL_CTX object's session cache. so 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). no sharing, no cross-pollinating, it's a monogamous situation πŸŽ€


⭐ RETURN VALUES ⭐

the following return values can occur:

  • NULL β€” there is no session available in ssl. it's empty, babes, nothing there 🌈
  • Pointer to an SSL_SESSION β€” the return value points to the data of an SSL session. yay, success, slay πŸ‘‘

πŸ¦‹ SEE ALSO πŸ¦‹

go check out the bestie pages too, iykyk:


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 OpenSSL version 4.0.3, dated 2026-09-29 β€” mwah, go forth and manage your sessions responsibly πŸ’–βœ¨πŸŽ€πŸ«Ά

πŸ“¦ where this page comes from