SSL_CTX_SESS_SET_GET_CB(3)
πΈ SSL_CTX_SESS_SET_GET_CB(3) πΈ
okay so this is the SSL_CTX_SESS_SET_GET_CB(3) page, from the Library Functions Manual, and bestie it is literally all about callback functions for server side external session caching β¨ like that's the whole vibe π
π 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 provide callback functions for server side external session caching π«Ά
β¨ SYNOPSIS β¨
so first of all, we're in libssl libcrypto land, and you gotta pull in the header:
#include <openssl/ssl.h>
then, the setters π:
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 *));
and the getters, which are giving function-returning-a-function-pointer main character energy π:
int
(*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess);
void
(*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX *ctx,
SSL_SESSION *sess);
SSL_SESSION *
(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl,
const unsigned char *data, int len, int *copy);
and then the callback prototypes themselves, spelled out all cute π¦:
int
(*new_session_cb)(SSL *ssl, SSL_SESSION *sess);
void
(*remove_session_cb)(SSL_CTX *ctx, SSL_SESSION *sess);
SSL_SESSION *
(*get_session_cb)(SSL *ssl, unsigned char *data, int len, int *copy);
π¨ quick vibe check: in theset_get_cbversion above,dataisconst unsigned char *, but in the standaloneget_session_cbtypedef it's justunsigned char *. that's how the page has it, so don't come for me, i'm just the messenger π
π DESCRIPTION π
the setters πΈ
SSL_CTX_sess_set_new_cb()β sets the callback function that gets called automatically whenever a new session was negotiated. no cap, every single time π₯SSL_CTX_sess_set_remove_cb()β sets the callback function that gets called automatically whenever a session is removed by the SSL engine. and why would the engine do that? because the session is considered faulty, or because it's become obsolete from exceeding the timeout value. expired = evicted, basically πSSL_CTX_sess_set_get_cb()β sets the callback function that gets called whenever an SSL/TLS client proposes to resume a session but that session cannot be found in the internal session cache (peepSSL_CTX_set_session_cache_mode(3)). also important: this one is SSL/TLS server only π₯οΈ
the getters β
SSL_CTX_sess_get_new_cb(), SSL_CTX_sess_get_remove_cb(), and SSL_CTX_sess_get_get_cb() retrieve the function pointers of the callback functions you provided. and if a callback function hasn't been set? you get the NULL pointer back. that's it. that's the answer π
so why do these exist, fr fr π§
in order to allow external session caching, synchronization with the internal session cache is realized via callback functions. and inside these callback functions, the session can be saved to disk or put into a database using the d2i_SSL_SESSION(3) interface. that's the whole point, that's the assignment, ate π
the three callbacks, one by one π
new_session_cb() π
called whenever a new session has been negotiated and session caching is enabled (again, SSL_CTX_set_session_cache_mode(3)). it gets passed the ssl connection and the ssl session sess. and here's the plot twist: if the callback returns 0, the session will be immediately removed again π so like... return 0 only if you actually mean it.
remove_session_cb() ποΈ
called whenever the SSL engine removes a session from the internal cache. when does that happen, you ask? when the session is removed because it's expired, or when a connection was not shut down cleanly. it also happens for all sessions in the internal session cache when SSL_CTX_free(3) is called β yes, all of them, every last one π
it's passed the ctx and the ssl session sess. and it does not provide any feedback. no return value, no notes, no thoughts, just vibes π«Ά
get_session_cb() π
this one is only called on SSL/TLS servers, with the session id proposed by the client. and listen β it is always called, also when session caching was disabled. iconic behaviour honestly π
it gets passed the ssl connection, plus the session id of length length at the memory location data. then with the parameter copy, the callback can require the SSL engine to increment the reference count of the SSL_SESSION object.
β οΈ and this is the important part, bestie: normally the reference count is not incremented, and therefore the session must not be explicitly freed with SSL_SESSION_free(3). don't do it. put the free() down π₯
π SEE ALSO π
d2i_SSL_SESSION(3), ssl(3), SSL_CTX_flush_sessions(3), SSL_CTX_free(3), SSL_CTX_set_session_cache_mode(3), SSL_SESSION_free(3)
like pls go read these, they're all linked up and they all slay in their own way β¨
π HISTORY π
SSL_CTX_sess_set_new_cb(),SSL_CTX_sess_set_get_cb(),SSL_CTX_sess_get_new_cb(), andSSL_CTX_sess_get_get_cb()β first appeared in SSLeay 0.6.0 βSSL_CTX_sess_set_remove_cb()andSSL_CTX_sess_get_remove_cb()β first appeared in SSLeay 0.8.0 π¦- and these functions have been available since OpenBSD 2.4 π
and that's the page, bestie!! π mwah π
Debian πΈ August 25, 2026 πΈ SSL_CTX_SESS_SET_GET_CB(3) β go forth and cache externally, ily π«Άπ
π¦ where this page comes from
- arch rolling: libressl 4.4.0-1 Β· ISC AND custom:OpenSSL
- Copyright (c) 2001, 2002, 2003, 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