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