1SSL_CTX_SET_TLSEXT_TICKET_KEY_CB(3ossOlp)enSSSSLL_CTX_SET_TLSEXT_TICKET_KEY_CB(3ossl)
2
3
4
6 SSL_CTX_set_tlsext_ticket_key_evp_cb, SSL_CTX_set_tlsext_ticket_key_cb
7 - set a callback for session ticket processing
8
10 #include <openssl/tls1.h>
11
12 int SSL_CTX_set_tlsext_ticket_key_evp_cb(SSL_CTX sslctx,
13 int (*cb)(SSL *s, unsigned char key_name[16],
14 unsigned char iv[EVP_MAX_IV_LENGTH],
15 EVP_CIPHER_CTX *ctx, EVP_MAC_CTX *hctx, int enc));
16
17 The following function has been deprecated since OpenSSL 3.0, and can
18 be hidden entirely by defining OPENSSL_API_COMPAT with a suitable
19 version value, see openssl_user_macros(7):
20
21 int SSL_CTX_set_tlsext_ticket_key_cb(SSL_CTX sslctx,
22 int (*cb)(SSL *s, unsigned char key_name[16],
23 unsigned char iv[EVP_MAX_IV_LENGTH],
24 EVP_CIPHER_CTX *ctx, HMAC_CTX *hctx, int enc));
25
27 SSL_CTX_set_tlsext_ticket_key_evp_cb() sets a callback function cb for
28 handling session tickets for the ssl context sslctx. Session tickets,
29 defined in RFC5077 provide an enhanced session resumption capability
30 where the server implementation is not required to maintain per session
31 state. It only applies to TLS and there is no SSLv3 implementation.
32
33 The callback function cb will be called for every client instigated TLS
34 session when session ticket extension is presented in the TLS hello
35 message. It is the responsibility of this function to create or
36 retrieve the cryptographic parameters and to maintain their state.
37
38 The OpenSSL library uses your callback function to help implement a
39 common TLS ticket construction state according to RFC5077 Section 4
40 such that per session state is unnecessary and a small set of
41 cryptographic variables needs to be maintained by the callback function
42 implementation.
43
44 In order to reuse a session, a TLS client must send the a session
45 ticket extension to the server. The client can only send exactly one
46 session ticket. The server, through the callback function, either
47 agrees to reuse the session ticket information or it starts a full TLS
48 handshake to create a new session ticket.
49
50 Before the callback function is started ctx and hctx have been
51 initialised with EVP_CIPHER_CTX_reset(3) and EVP_MAC_CTX_new(3)
52 respectively.
53
54 For new sessions tickets, when the client doesn't present a session
55 ticket, or an attempted retrieval of the ticket failed, or a renew
56 option was indicated, the callback function will be called with enc
57 equal to 1. The OpenSSL library expects that the function will set an
58 arbitrary name, initialize iv, and set the cipher context ctx and the
59 hash context hctx.
60
61 The name is 16 characters long and is used as a key identifier.
62
63 The iv length is the length of the IV of the corresponding cipher. The
64 maximum IV length is EVP_MAX_IV_LENGTH bytes defined in
65 <openssl/evp.h>.
66
67 The initialization vector iv should be a random value. The cipher
68 context ctx should use the initialisation vector iv. The cipher context
69 can be set using EVP_EncryptInit_ex(3). The hmac context and digest can
70 be set using EVP_MAC_CTX_set_params(3) with the OSSL_MAC_PARAM_KEY and
71 OSSL_MAC_PARAM_DIGEST parameters respectively.
72
73 When the client presents a session ticket, the callback function with
74 be called with enc set to 0 indicating that the cb function should
75 retrieve a set of parameters. In this case name and iv have already
76 been parsed out of the session ticket. The OpenSSL library expects that
77 the name will be used to retrieve a cryptographic parameters and that
78 the cryptographic context ctx will be set with the retrieved parameters
79 and the initialization vector iv. using a function like
80 EVP_DecryptInit_ex(3). The key material and digest for hctx need to be
81 set using EVP_MAC_CTX_set_params(3) with the OSSL_MAC_PARAM_KEY and
82 OSSL_MAC_PARAM_DIGEST parameters respectively.
83
84 If the name is still valid but a renewal of the ticket is required the
85 callback function should return 2. The library will call the callback
86 again with an argument of enc equal to 1 to set the new ticket.
87
88 The return value of the cb function is used by OpenSSL to determine
89 what further processing will occur. The following return values have
90 meaning:
91
92 2 This indicates that the ctx and hctx have been set and the session
93 can continue on those parameters. Additionally it indicates that
94 the session ticket is in a renewal period and should be replaced.
95 The OpenSSL library will call cb again with an enc argument of 1 to
96 set the new ticket (see RFC5077 3.3 paragraph 2).
97
98 1 This indicates that the ctx and hctx have been set and the session
99 can continue on those parameters.
100
101 0 This indicates that it was not possible to set/retrieve a session
102 ticket and the SSL/TLS session will continue by negotiating a set
103 of cryptographic parameters or using the alternate SSL/TLS
104 resumption mechanism, session ids.
105
106 If called with enc equal to 0 the library will call the cb again to
107 get a new set of parameters.
108
109 less than 0
110 This indicates an error.
111
112 The SSL_CTX_set_tlsext_ticket_key_cb() function is identical to
113 SSL_CTX_set_tlsext_ticket_key_evp_cb() except that it takes a
114 deprecated HMAC_CTX pointer instead of an EVP_MAC_CTX one. Before this
115 callback function is started hctx will have been initialised with
116 EVP_MAC_CTX_new(3) and the digest set with EVP_MAC_CTX_set_params(3).
117 The hctx key material can be set using HMAC_Init_ex(3).
118
120 Session resumption shortcuts the TLS so that the client certificate
121 negotiation don't occur. It makes up for this by storing client
122 certificate an all other negotiated state information encrypted within
123 the ticket. In a resumed session the applications will have all this
124 state information available exactly as if a full negotiation had
125 occurred.
126
127 If an attacker can obtain the key used to encrypt a session ticket,
128 they can obtain the master secret for any ticket using that key and
129 decrypt any traffic using that session: even if the cipher suite
130 supports forward secrecy. As a result applications may wish to use
131 multiple keys and avoid using long term keys stored in files.
132
133 Applications can use longer keys to maintain a consistent level of
134 security. For example if a cipher suite uses 256 bit ciphers but only
135 a 128 bit ticket key the overall security is only 128 bits because
136 breaking the ticket key will enable an attacker to obtain the session
137 keys.
138
140 Returns 1 to indicate the callback function was set and 0 otherwise.
141
143 Reference Implementation:
144
145 SSL_CTX_set_tlsext_ticket_key_evp_cb(SSL, ssl_tlsext_ticket_key_cb);
146 ...
147
148 static int ssl_tlsext_ticket_key_cb(SSL *s, unsigned char key_name[16],
149 unsigned char *iv, EVP_CIPHER_CTX *ctx,
150 EVP_MAC_CTX *hctx, int enc)
151 {
152 OSSL_PARAM params[3];
153 your_type_t *key; /* something that you need to implement */
154
155 if (enc) { /* create new session */
156 if (RAND_bytes(iv, EVP_MAX_IV_LENGTH) <= 0)
157 return -1; /* insufficient random */
158
159 key = currentkey(); /* something that you need to implement */
160 if (key == NULL) {
161 /* current key doesn't exist or isn't valid */
162 key = createkey(); /*
163 * Something that you need to implement.
164 * createkey needs to initialise a name,
165 * an aes_key, a hmac_key and optionally
166 * an expire time.
167 */
168 if (key == NULL) /* key couldn't be created */
169 return 0;
170 }
171 memcpy(key_name, key->name, 16);
172
173 if (EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, key->aes_key,
174 iv) == 0)
175 return -1; /* error in cipher initialisation */
176
177 params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
178 key->hmac_key, 32);
179 params[1] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
180 "sha256", 0);
181 params[2] = OSSL_PARAM_construct_end();
182 if (EVP_MAC_CTX_set_params(hctx, params) == 0)
183 return -1; /* error in mac initialisation */
184
185 return 1;
186
187 } else { /* retrieve session */
188 time_t t = time(NULL);
189 key = findkey(key_name); /* something that you need to implement */
190
191 if (key == NULL || key->expire < t)
192 return 0;
193
194 params[0] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
195 key->hmac_key, 32);
196 params[1] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
197 "sha256", 0);
198 params[2] = OSSL_PARAM_construct_end();
199 if (EVP_MAC_CTX_set_params(hctx, params) == 0)
200 return -1; /* error in mac initialisation */
201
202 if (EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, key->aes_key,
203 iv) == 0)
204 return -1; /* error in cipher initialisation */
205
206 if (key->expire < t - RENEW_TIME) { /* RENEW_TIME: implement */
207 /*
208 * return 2 - This session will get a new ticket even though the
209 * current one is still valid.
210 */
211 return 2;
212 }
213 return 1;
214 }
215 }
216
218 ssl(7), SSL_set_session(3), SSL_session_reused(3),
219 SSL_CTX_add_session(3), SSL_CTX_sess_number(3),
220 SSL_CTX_sess_set_get_cb(3), SSL_CTX_set_session_id_context(3),
221
223 The SSL_CTX_set_tlsext_ticket_key_cb() function was deprecated in
224 OpenSSL 3.0.
225
226 The SSL_CTX_set_tlsext_ticket_key_evp_cb() function was introduced in
227 OpenSSL 3.0.
228
230 Copyright 2014-2021 The OpenSSL Project Authors. All Rights Reserved.
231
232 Licensed under the Apache License 2.0 (the "License"). You may not use
233 this file except in compliance with the License. You can obtain a copy
234 in the file LICENSE in the source distribution or at
235 <https://www.openssl.org/source/license.html>.
236
237
238
2393.1.1 2023-0S8S-L3_1CTX_SET_TLSEXT_TICKET_KEY_CB(3ossl)