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, GEN_SESSION_CB - manipulate generation of
8 SSL session IDs (server only)
9
11 #include <openssl/ssl.h>
12
13 typedef int (*GEN_SESSION_CB)(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 between 1 and 32 bytes. The
36 session id is not security critical but must be unique for the server.
37 Additionally, the session id is transmitted in the clear when reusing
38 the session so it must not contain sensitive information.
39
40 Without a callback being set, an OpenSSL server will generate a unique
41 session id from pseudo random numbers of the maximum possible length.
42 Using the callback function, the session id can be changed to contain
43 additional information like e.g. a host id in order to improve load
44 balancing or external caching techniques.
45
46 The callback function receives a pointer to the memory location to put
47 id into and a pointer to the maximum allowed length id_len. The buffer
48 at location id is only guaranteed to have the size id_len. The
49 callback is only allowed to generate a shorter id and reduce id_len;
50 the callback must never increase id_len or write to the location id
51 exceeding the given limit.
52
53 The location id is filled with 0x00 before the callback is called, so
54 the callback may only fill part of the possible length and leave id_len
55 untouched while maintaining reproducibility.
56
57 Since the sessions must be distinguished, session ids must be unique.
58 Without the callback a random number is used, so that the probability
59 of generating the same session id is extremely small (2^256 for
60 SSLv3/TLSv1). In order to assure the uniqueness of the generated
61 session id, the callback must call SSL_has_matching_session_id() and
62 generate another id if a conflict occurs. If an id conflict is not
63 resolved, the handshake will fail. If the application codes e.g. a
64 unique host id, a unique process number, and a unique sequence number
65 into the session id, uniqueness could easily be achieved without
66 randomness added (it should however be taken care that no confidential
67 information is leaked this way). If the application can not guarantee
68 uniqueness, it is recommended to use the maximum id_len and fill in the
69 bytes not used to code special information with random data to avoid
70 collisions.
71
72 SSL_has_matching_session_id() will only query the internal session
73 cache, not the external one. Since the session id is generated before
74 the handshake is completed, it is not immediately added to the cache.
75 If another thread is using the same internal session cache, a race
76 condition can occur in that another thread generates the same session
77 id. Collisions can also occur when using an external session cache,
78 since the external cache is not tested with
79 SSL_has_matching_session_id() and the same race condition applies.
80
81 The callback must return 0 if it cannot generate a session id for
82 whatever reason and return 1 on success.
83
85 SSL_CTX_set_generate_session_id() and SSL_set_generate_session_id()
86 always return 1.
87
88 SSL_has_matching_session_id() returns 1 if another session with the
89 same id is already in the cache.
90
92 The callback function listed will generate a session id with the server
93 id given, and will fill the rest with pseudo random bytes:
94
95 const char session_id_prefix = "www-18";
96
97 #define MAX_SESSION_ID_ATTEMPTS 10
98 static int generate_session_id(SSL *ssl, unsigned char *id,
99 unsigned int *id_len)
100 {
101 unsigned int count = 0;
102
103 do {
104 RAND_pseudo_bytes(id, *id_len);
105 /*
106 * Prefix the session_id with the required prefix. NB: If our
107 * prefix is too long, clip it - but there will be worse effects
108 * anyway, e.g. the server could only possibly create 1 session
109 * ID (i.e. the prefix!) so all future session negotiations will
110 * fail due to conflicts.
111 */
112 memcpy(id, session_id_prefix, strlen(session_id_prefix) < *id_len ?
113 strlen(session_id_prefix) : *id_len);
114 } while (SSL_has_matching_session_id(ssl, id, *id_len)
115 && ++count < MAX_SESSION_ID_ATTEMPTS);
116 if (count >= MAX_SESSION_ID_ATTEMPTS)
117 return 0;
118 return 1;
119 }
120
122 ssl(7), SSL_get_version(3)
123
125 Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
126
127 Licensed under the OpenSSL license (the "License"). You may not use
128 this file except in compliance with the License. You can obtain a copy
129 in the file LICENSE in the source distribution or at
130 <https://www.openssl.org/source/license.html>.
131
132
133
1341.1.1q 2023-02-06SSL_CTX_SET_GENERATE_SESSION_ID(3)