SSL_CTX_SESS_SET_GET_CB(3ossl)

πŸ’– SSL_CTX_SESS_SET_GET_CB πŸ’–

okay bestie, gather round, because this is the 3ossl manual page for the server-side external session caching callbacks and it is lowkey iconic. this is where you teach OpenSSL where to stash your sessions when the built-in cache just isn't the vibe. let's get into it 🫢


✨ NAME ✨

SSL_CTX_sess_set_new_cb, SSL_CTX_sess_set_remove_cb, SSL_CTX_sess_set_get_cb, SSL_CTX_sess_get_new_cb, SSL_CTX_sess_get_remove_cb, SSL_CTX_sess_get_get_cb β€” these are the girls that hand you callback functions for server side external session caching. πŸ’…


🌸 SYNOPSIS 🌸

#include <openssl/ssl.h>

void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
                             int (*new_session_cb)(SSL *, SSL_SESSION *));
void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
                                void (*remove_session_cb)(SSL_CTX *ctx,
                                                          SSL_SESSION *));
void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
                             SSL_SESSION (*get_session_cb)(SSL *,
                                                           const unsigned char *,
                                                           int, int *));

int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(struct ssl_st *ssl,
                                             SSL_SESSION *sess);
void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(struct ssl_ctx_st *ctx,
                                                 SSL_SESSION *sess);
SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(struct ssl_st *ssl,
                                                      const unsigned char *data,
                                                      int len, int *copy);

like, yes, the return types are function pointers and yes they are serving cunt in a slightly cursed way. that's just how C do. 😭


πŸ¦‹ DESCRIPTION πŸ¦‹

  • SSL_CTX_sess_set_new_cb() β€” sets the callback function that gets called whenever a new session was negotiated. new session, who dis? ✨
  • SSL_CTX_sess_set_remove_cb() β€” sets the callback function that gets called whenever a session is removed by the SSL engine. for example, this can happen because a session is considered faulty, or has become obsolete because of exceeding the timeout value. rip to that session, she's gone πŸ₯€
  • SSL_CTX_sess_set_get_cb() β€” sets the callback function that gets called whenever a TLS client proposed to resume a session but the session could not be found in the internal session cache (see SSL_CTX_set_session_cache_mode(3)). (TLS server only.) so it's the "hey bestie do you have her? no? okay hold on" callback. 🧚
  • SSL_CTX_sess_get_new_cb(), SSL_CTX_sess_get_remove_cb(), and SSL_CTX_sess_get_get_cb() β€” these retrieve the function pointers that were set by the corresponding set callback functions. and if a callback function has not been set, the NULL pointer is returned. no callback, no problem, just NULL. πŸ’€

πŸ‘‘ NOTES πŸ‘‘

so the whole point: to allow external session caching, synchronization with the internal session cache is realized via callback functions. inside these callback functions, the session can be saved to disk or put into a database using the d2i_SSL_SESSION(3) interface. storage options are endless, she contains multitudes. 🌈

🌟 new_session_cb 🌟

the new_session_cb() is called whenever a new session has been negotiated and session caching is enabled (see SSL_CTX_set_session_cache_mode(3)). it is passed the ssl connection and the nascent ssl session sess. since sessions are reference-counted objects, the reference count on the session is incremented before the callback, on behalf of the application. okay so here's the deal:

  • if the callback returns 0, the session will be immediately removed from the internal cache and the reference count released. bye girl πŸ‘‹
  • if the callback returns 1, the application retains the reference (for an entry in the application-maintained "external session cache"), and is responsible for calling SSL_SESSION_free() when the session reference is no longer in use. that's your child now, you feed her. 🍼
not me reminding you that in TLSv1.3, sessions are established after the main handshake has completed. the server decides when to send the client the session information and this may occur some time after the end of the handshake (or not at all). so applications should expect the new_session_cb() function to be invoked during the handshake (for <= TLSv1.2) or after the handshake (for TLSv1.3). it is also possible in TLSv1.3 for multiple sessions to be established with a single connection. in these case the new_session_cb() function will be invoked multiple times. yes, multiple times. she's a repeater. 😭

in TLSv1.3 it is recommended that each SSL_SESSION object is only used for resumption once. one way of enforcing that is for applications to call SSL_CTX_remove_session(3) after a session has been used. one and done, periodt. πŸ’…

🌸 remove_session_cb 🌸

the remove_session_cb() is called whenever the SSL engine removes a session from the internal cache. this can happen when:

  • the session is removed because it is expired, or
  • when a connection was not shutdown cleanly.

it also happens for all sessions in the internal session cache when SSL_CTX_free(3) is called. the remove_session_cb() is passed the ctx and the ssl session sess. it does not provide any feedback. it's giving silent observer. 🀫

πŸŽ€ get_session_cb πŸŽ€

the get_session_cb() is only called on SSL/TLS servers, and is given the session id proposed by the client. and listen β€” the get_session_cb() is always called, even when session caching was disabled. always. she shows up regardless. πŸ’–

the get_session_cb() is passed the ssl connection and the session id of length length at the memory location data. now the copy situation:

  • by setting the parameter copy to 1, the callback can require the SSL engine to increment the reference count of the SSL_SESSION object;
  • setting copy to 0 causes the reference count to remain unchanged.
and if the get_session_cb() does not write to copy, the reference count is incremented and the session must be explicitly freed with SSL_SESSION_free(3). do not forget this one, bestie, that's a leak and leaks are not the vibe. 😭

⭐ RETURN VALUES ⭐

SSL_CTX_sess_get_new_cb(), SSL_CTX_sess_get_remove_cb() and SSL_CTX_sess_get_get_cb() return different callback function pointers respectively. so like, the one you asked for. each to each. slay. πŸ”₯


πŸ’œ SEE ALSO πŸ’œ

ssl(7), d2i_SSL_SESSION(3), SSL_CTX_set_session_cache_mode(3), SSL_CTX_flush_sessions(3), SSL_SESSION_free(3), SSL_CTX_free(3)

go read those too, they're all part of the same friend group. 🫢


Copyright 2001-2020 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 3.5.9, dated 2026-09-29, filed under SSL_CTX_SESS_SET_GET_CB(3ossl). go forth and cache those sessions externally, you absolute main character πŸ‘‘πŸ’–βœ¨

πŸ“¦ where this page comes from