1SSL_CTX_set_generate_session_id(3) OpenSSL SSL_CTX_set_generate_session_id(3)
2
3
4
6 SSL_CTX_set_generate_session_id, SSL_set_generate_session_id,
7 SSL_has_matching_session_id - manipulate generation of SSL session IDs
8 (server only)
9
11 #include <openssl/ssl.h>
12
13 typedef int (*GEN_SESSION_CB)(const SSL *ssl, unsigned char *id,
14 unsigned int *id_len);
15
16 int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb);
17 int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB, cb);
18 int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
19 unsigned int id_len);
20
22 SSL_CTX_set_generate_session_id() sets the callback function for
23 generating new session ids for SSL/TLS sessions for ctx to be cb.
24
25 SSL_set_generate_session_id() sets the callback function for generating
26 new session ids for SSL/TLS sessions for ssl to be cb.
27
28 SSL_has_matching_session_id() checks, whether a session with id id (of
29 length id_len) is already contained in the internal session cache of
30 the parent context of ssl.
31
33 When a new session is established between client and server, the server
34 generates a session id. The session id is an arbitrary sequence of
35 bytes. The length of the session id is 16 bytes for SSLv2 sessions and
36 between 1 and 32 bytes for SSLv3/TLSv1. The session id is not security
37 critical but must be unique for the server. Additionally, the session
38 id is transmitted in the clear when reusing the session so it must not
39 contain sensitive information.
40
41 Without a callback being set, an OpenSSL server will generate a unique
42 session id from pseudo random numbers of the maximum possible length.
43 Using the callback function, the session id can be changed to contain
44 additional information like e.g. a host id in order to improve load
45 balancing or external caching techniques.
46
47 The callback function receives a pointer to the memory location to put
48 id into and a pointer to the maximum allowed length id_len. The buffer
49 at location id is only guaranteed to have the size id_len. The
50 callback is only allowed to generate a shorter id and reduce id_len;
51 the callback must never increase id_len or write to the location id
52 exceeding the given limit.
53
54 If a SSLv2 session id is generated and id_len is reduced, it will be
55 restored after the callback has finished and the session id will be
56 padded with 0x00. It is not recommended to change the id_len for SSLv2
57 sessions. The callback can use the SSL_get_version(3) function to
58 check, whether the session is of type SSLv2.
59
60 The location id is filled with 0x00 before the callback is called, so
61 the callback may only fill part of the possible length and leave id_len
62 untouched while maintaining reproducibility.
63
64 Since the sessions must be distinguished, session ids must be unique.
65 Without the callback a random number is used, so that the probability
66 of generating the same session id is extremely small (2^128 possible
67 ids for an SSLv2 session, 2^256 for SSLv3/TLSv1). In order to assure
68 the uniqueness of the generated session id, the callback must call
69 SSL_has_matching_session_id() and generate another id if a conflict
70 occurs. If an id conflict is not resolved, the handshake will fail.
71 If the application codes e.g. a unique host id, a unique process
72 number, and a unique sequence number into the session id, uniqueness
73 could easily be achieved without randomness added (it should however be
74 taken care that no confidential information is leaked this way). If the
75 application can not guarantee uniqueness, it is recommended to use the
76 maximum id_len and fill in the bytes not used to code special
77 information with random data to avoid collisions.
78
79 SSL_has_matching_session_id() will only query the internal session
80 cache, not the external one. Since the session id is generated before
81 the handshake is completed, it is not immediately added to the cache.
82 If another thread is using the same internal session cache, a race
83 condition can occur in that another thread generates the same session
84 id. Collisions can also occur when using an external session cache,
85 since the external cache is not tested with
86 SSL_has_matching_session_id() and the same race condition applies.
87
88 When calling SSL_has_matching_session_id() for an SSLv2 session with
89 reduced id_len, the match operation will be performed using the fixed
90 length required and with a 0x00 padded id.
91
92 The callback must return 0 if it cannot generate a session id for
93 whatever reason and return 1 on success.
94
96 The callback function listed will generate a session id with the server
97 id given, and will fill the rest with pseudo random bytes:
98
99 const char session_id_prefix = "www-18";
100
101 #define MAX_SESSION_ID_ATTEMPTS 10
102 static int generate_session_id(const SSL *ssl, unsigned char *id,
103 unsigned int *id_len)
104 {
105 unsigned int count = 0;
106 const char *version;
107
108 version = SSL_get_version(ssl);
109 if (!strcmp(version, "SSLv2"))
110 /* we must not change id_len */;
111
112 do {
113 RAND_pseudo_bytes(id, *id_len);
114 /* Prefix the session_id with the required prefix. NB: If our
115 * prefix is too long, clip it - but there will be worse effects
116 * anyway, eg. the server could only possibly create 1 session
117 * ID (ie. the prefix!) so all future session negotiations will
118 * fail due to conflicts. */
119 memcpy(id, session_id_prefix,
120 (strlen(session_id_prefix) < *id_len) ?
121 strlen(session_id_prefix) : *id_len);
122 }
123 while(SSL_has_matching_session_id(ssl, id, *id_len) &&
124 (++count < MAX_SESSION_ID_ATTEMPTS));
125 if(count >= MAX_SESSION_ID_ATTEMPTS)
126 return 0;
127 return 1;
128 }
129
131 SSL_CTX_set_generate_session_id() and SSL_set_generate_session_id()
132 always return 1.
133
134 SSL_has_matching_session_id() returns 1 if another session with the
135 same id is already in the cache.
136
138 ssl(3), SSL_get_version(3)
139
141 SSL_CTX_set_generate_session_id(), SSL_set_generate_session_id() and
142 SSL_has_matching_session_id() have been introduced in OpenSSL 0.9.7.
143
144
145
1461.0.2o 2019-09-10SSL_CTX_set_generate_session_id(3)