| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) |
| 3 |
.\" |
| 4 |
.\" Standard preamble: |
| 5 |
.\" ======================================================================== |
| 6 |
.de Sp \" Vertical space (when we can't use .PP) |
| 7 |
.if t .sp .5v |
| 8 |
.if n .sp |
| 9 |
.. |
| 10 |
.de Vb \" Begin verbatim text |
| 11 |
.ft CW |
| 12 |
.nf |
| 13 |
.ne \\$1 |
| 14 |
.. |
| 15 |
.de Ve \" End verbatim text |
| 16 |
.ft R |
| 17 |
.fi |
| 18 |
.. |
| 19 |
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. |
| 20 |
.ie n \{\ |
| 21 |
. ds C` "" |
| 22 |
. ds C' "" |
| 23 |
'br\} |
| 24 |
.el\{\ |
| 25 |
. ds C` |
| 26 |
. ds C' |
| 27 |
'br\} |
| 28 |
.\" |
| 29 |
.\" Escape single quotes in literal strings from groff's Unicode transform. |
| 30 |
.ie \n(.g .ds Aq \(aq |
| 31 |
.el .ds Aq ' |
| 32 |
.\" |
| 33 |
.\" If the F register is >0, we'll generate index entries on stderr for |
| 34 |
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 35 |
.\" entries marked with X<> in POD. Of course, you'll have to process the |
| 36 |
.\" output yourself in some meaningful fashion. |
| 37 |
.\" |
| 38 |
.\" Avoid warning from groff about undefined register 'F'. |
| 39 |
.de IX |
| 40 |
.. |
| 41 |
.nr rF 0 |
| 42 |
.if \n(.g .if rF .nr rF 1 |
| 43 |
.if (\n(rF:(\n(.g==0)) \{\ |
| 44 |
. if \nF \{\ |
| 45 |
. de IX |
| 46 |
. tm Index:\\$1\t\\n%\t"\\$2" |
| 47 |
.. |
| 48 |
. if !\nF==2 \{\ |
| 49 |
. nr % 0 |
| 50 |
. nr F 2 |
| 51 |
. \} |
| 52 |
. \} |
| 53 |
.\} |
| 54 |
.rr rF |
| 55 |
.\" ======================================================================== |
| 56 |
.\" |
| 57 |
.IX Title "SSL_CTX_SET_GENERATE_SESSION_ID 3SSL" |
| 58 |
.TH SSL_CTX_SET_GENERATE_SESSION_ID 3SSL 2026-04-07 3.6.2 OpenSSL |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
SSL_CTX_set_generate_session_id, SSL_set_generate_session_id, |
| 65 |
SSL_has_matching_session_id, GEN_SESSION_CB |
| 66 |
\&\- manipulate generation of SSL session IDs (server only) |
| 67 |
.SH SYNOPSIS |
| 68 |
.IX Header "SYNOPSIS" |
| 69 |
.Vb 1 |
| 70 |
\& #include <openssl/ssl.h> |
| 71 |
\& |
| 72 |
\& typedef int (*GEN_SESSION_CB)(SSL *ssl, unsigned char *id, |
| 73 |
\& unsigned int *id_len); |
| 74 |
\& |
| 75 |
\& int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb); |
| 76 |
\& int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB, cb); |
| 77 |
\& int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id, |
| 78 |
\& unsigned int id_len); |
| 79 |
.Ve |
| 80 |
.SH DESCRIPTION |
| 81 |
.IX Header "DESCRIPTION" |
| 82 |
\&\fBSSL_CTX_set_generate_session_id()\fR sets the callback function for generating |
| 83 |
new session ids for SSL/TLS sessions for \fBctx\fR to be \fBcb\fR. |
| 84 |
.PP |
| 85 |
\&\fBSSL_set_generate_session_id()\fR sets the callback function for generating |
| 86 |
new session ids for SSL/TLS sessions for \fBssl\fR to be \fBcb\fR. |
| 87 |
.PP |
| 88 |
\&\fBSSL_has_matching_session_id()\fR checks, whether a session with id \fBid\fR |
| 89 |
(of length \fBid_len\fR) is already contained in the internal session cache |
| 90 |
of the parent context of \fBssl\fR. |
| 91 |
.SH NOTES |
| 92 |
.IX Header "NOTES" |
| 93 |
When a new session is established between client and server, the server |
| 94 |
generates a session id. The session id is an arbitrary sequence of bytes. |
| 95 |
The length of the session id is between 1 and 32 bytes. The session id is not |
| 96 |
security critical but must be unique for the server. Additionally, the session id is |
| 97 |
transmitted in the clear when reusing the session so it must not contain |
| 98 |
sensitive information. |
| 99 |
.PP |
| 100 |
Without a callback being set, an OpenSSL server will generate a unique |
| 101 |
session id from pseudo random numbers of the maximum possible length. |
| 102 |
Using the callback function, the session id can be changed to contain |
| 103 |
additional information like e.g. a host id in order to improve load balancing |
| 104 |
or external caching techniques. |
| 105 |
.PP |
| 106 |
The callback function receives a pointer to the memory location to put |
| 107 |
\&\fBid\fR into and a pointer to the maximum allowed length \fBid_len\fR. The |
| 108 |
buffer at location \fBid\fR is only guaranteed to have the size \fBid_len\fR. |
| 109 |
The callback is only allowed to generate a shorter id and reduce \fBid_len\fR; |
| 110 |
the callback \fBmust never\fR increase \fBid_len\fR or write to the location |
| 111 |
\&\fBid\fR exceeding the given limit. |
| 112 |
.PP |
| 113 |
The location \fBid\fR is filled with 0x00 before the callback is called, so the |
| 114 |
callback may only fill part of the possible length and leave \fBid_len\fR |
| 115 |
untouched while maintaining reproducibility. |
| 116 |
.PP |
| 117 |
Since the sessions must be distinguished, session ids must be unique. |
| 118 |
Without the callback a random number is used, so that the probability |
| 119 |
of generating the same session id is extremely small (2^256 for SSLv3/TLSv1). |
| 120 |
In order to assure the uniqueness of the generated session id, the callback must call |
| 121 |
\&\fBSSL_has_matching_session_id()\fR and generate another id if a conflict occurs. |
| 122 |
If an id conflict is not resolved, the handshake will fail. |
| 123 |
If the application codes e.g. a unique host id, a unique process number, and |
| 124 |
a unique sequence number into the session id, uniqueness could easily be |
| 125 |
achieved without randomness added (it should however be taken care that |
| 126 |
no confidential information is leaked this way). If the application can not |
| 127 |
guarantee uniqueness, it is recommended to use the maximum \fBid_len\fR and |
| 128 |
fill in the bytes not used to code special information with random data |
| 129 |
to avoid collisions. |
| 130 |
.PP |
| 131 |
\&\fBSSL_has_matching_session_id()\fR will only query the internal session cache, |
| 132 |
not the external one. Since the session id is generated before the |
| 133 |
handshake is completed, it is not immediately added to the cache. If |
| 134 |
another thread is using the same internal session cache, a race condition |
| 135 |
can occur in that another thread generates the same session id. |
| 136 |
Collisions can also occur when using an external session cache, since |
| 137 |
the external cache is not tested with \fBSSL_has_matching_session_id()\fR |
| 138 |
and the same race condition applies. |
| 139 |
.PP |
| 140 |
The callback must return 0 if it cannot generate a session id for whatever |
| 141 |
reason and return 1 on success. |
| 142 |
.SH "RETURN VALUES" |
| 143 |
.IX Header "RETURN VALUES" |
| 144 |
\&\fBSSL_CTX_set_generate_session_id()\fR and \fBSSL_set_generate_session_id()\fR |
| 145 |
return 1 on success and 0 for failure. |
| 146 |
.PP |
| 147 |
\&\fBSSL_has_matching_session_id()\fR returns 1 if another session with the |
| 148 |
same id is already in the cache, or 0 otherwise. |
| 149 |
.SH EXAMPLES |
| 150 |
.IX Header "EXAMPLES" |
| 151 |
The callback function listed will generate a session id with the |
| 152 |
server id given, and will fill the rest with pseudo random bytes: |
| 153 |
.PP |
| 154 |
.Vb 1 |
| 155 |
\& const char session_id_prefix = "www\-18"; |
| 156 |
\& |
| 157 |
\& #define MAX_SESSION_ID_ATTEMPTS 10 |
| 158 |
\& static int generate_session_id(SSL *ssl, unsigned char *id, |
| 159 |
\& unsigned int *id_len) |
| 160 |
\& { |
| 161 |
\& unsigned int count = 0; |
| 162 |
\& |
| 163 |
\& do { |
| 164 |
\& RAND_pseudo_bytes(id, *id_len); |
| 165 |
\& /* |
| 166 |
\& * Prefix the session_id with the required prefix. NB: If our |
| 167 |
\& * prefix is too long, clip it \- but there will be worse effects |
| 168 |
\& * anyway, e.g. the server could only possibly create 1 session |
| 169 |
\& * ID (i.e. the prefix!) so all future session negotiations will |
| 170 |
\& * fail due to conflicts. |
| 171 |
\& */ |
| 172 |
\& memcpy(id, session_id_prefix, strlen(session_id_prefix) < *id_len ? |
| 173 |
\& strlen(session_id_prefix) : *id_len); |
| 174 |
\& } while (SSL_has_matching_session_id(ssl, id, *id_len) |
| 175 |
\& && ++count < MAX_SESSION_ID_ATTEMPTS); |
| 176 |
\& if (count >= MAX_SESSION_ID_ATTEMPTS) |
| 177 |
\& return 0; |
| 178 |
\& return 1; |
| 179 |
\& } |
| 180 |
.Ve |
| 181 |
.SH "SEE ALSO" |
| 182 |
.IX Header "SEE ALSO" |
| 183 |
\&\fBssl\fR\|(7), \fBSSL_get_version\fR\|(3) |
| 184 |
.SH COPYRIGHT |
| 185 |
.IX Header "COPYRIGHT" |
| 186 |
Copyright 2001\-2020 The OpenSSL Project Authors. All Rights Reserved. |
| 187 |
.PP |
| 188 |
Licensed under the Apache License 2.0 (the "License"). You may not use |
| 189 |
this file except in compliance with the License. You can obtain a copy |
| 190 |
in the file LICENSE in the source distribution or at |
| 191 |
<https://www.openssl.org/source/license.html>. |