1SSL_CTX_SET_TLSEXT_TICKET_KEY_CB(3) OpenSSLSSL_CTX_SET_TLSEXT_TICKET_KEY_CB(3)
2
3
4

NAME

6       SSL_CTX_set_tlsext_ticket_key_cb - set a callback for session ticket
7       processing
8

SYNOPSIS

10        #include <openssl/tls1.h>
11
12        long SSL_CTX_set_tlsext_ticket_key_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, HMAC_CTX *hctx, int enc));
16

DESCRIPTION

18       SSL_CTX_set_tlsext_ticket_key_cb() sets a callback function cb for
19       handling session tickets for the ssl context sslctx. Session tickets,
20       defined in RFC5077 provide an enhanced session resumption capability
21       where the server implementation is not required to maintain per session
22       state. It only applies to TLS and there is no SSLv3 implementation.
23
24       The callback function cb will be called for every client instigated TLS
25       session when session ticket extension is presented in the TLS hello
26       message. It is the responsibility of this function to create or
27       retrieve the cryptographic parameters and to maintain their state.
28
29       The OpenSSL library uses your callback function to help implement a
30       common TLS ticket construction state according to RFC5077 Section 4
31       such that per session state is unnecessary and a small set of
32       cryptographic variables needs to be maintained by the callback function
33       implementation.
34
35       In order to reuse a session, a TLS client must send the a session
36       ticket extension to the server. The client can only send exactly one
37       session ticket.  The server, through the callback function, either
38       agrees to reuse the session ticket information or it starts a full TLS
39       handshake to create a new session ticket.
40
41       Before the callback function is started ctx and hctx have been
42       initialised with EVP_CIPHER_CTX_reset(3) and HMAC_CTX_reset(3)
43       respectively.
44
45       For new sessions tickets, when the client doesn't present a session
46       ticket, or an attempted retrieval of the ticket failed, or a renew
47       option was indicated, the callback function will be called with enc
48       equal to 1. The OpenSSL library expects that the function will set an
49       arbitrary name, initialize iv, and set the cipher context ctx and the
50       hash context hctx.
51
52       The name is 16 characters long and is used as a key identifier.
53
54       The iv length is the length of the IV of the corresponding cipher. The
55       maximum IV length is EVP_MAX_IV_LENGTH bytes defined in evp.h.
56
57       The initialization vector iv should be a random value. The cipher
58       context ctx should use the initialisation vector iv. The cipher context
59       can be set using EVP_EncryptInit_ex(3). The hmac context can be set
60       using HMAC_Init_ex(3).
61
62       When the client presents a session ticket, the callback function with
63       be called with enc set to 0 indicating that the cb function should
64       retrieve a set of parameters. In this case name and iv have already
65       been parsed out of the session ticket. The OpenSSL library expects that
66       the name will be used to retrieve a cryptographic parameters and that
67       the cryptographic context ctx will be set with the retrieved parameters
68       and the initialization vector iv. using a function like
69       EVP_DecryptInit_ex(3). The hctx needs to be set using HMAC_Init_ex(3).
70
71       If the name is still valid but a renewal of the ticket is required the
72       callback function should return 2. The library will call the callback
73       again with an argument of enc equal to 1 to set the new ticket.
74
75       The return value of the cb function is used by OpenSSL to determine
76       what further processing will occur. The following return values have
77       meaning:
78
79       2   This indicates that the ctx and hctx have been set and the session
80           can continue on those parameters. Additionally it indicates that
81           the session ticket is in a renewal period and should be replaced.
82           The OpenSSL library will call cb again with an enc argument of 1 to
83           set the new ticket (see RFC5077 3.3 paragraph 2).
84
85       1   This indicates that the ctx and hctx have been set and the session
86           can continue on those parameters.
87
88       0   This indicates that it was not possible to set/retrieve a session
89           ticket and the SSL/TLS session will continue by negotiating a set
90           of cryptographic parameters or using the alternate SSL/TLS
91           resumption mechanism, session ids.
92
93           If called with enc equal to 0 the library will call the cb again to
94           get a new set of parameters.
95
96       less than 0
97           This indicates an error.
98

NOTES

100       Session resumption shortcuts the TLS so that the client certificate
101       negotiation don't occur. It makes up for this by storing client
102       certificate an all other negotiated state information encrypted within
103       the ticket. In a resumed session the applications will have all this
104       state information available exactly as if a full negotiation had
105       occurred.
106
107       If an attacker can obtain the key used to encrypt a session ticket,
108       they can obtain the master secret for any ticket using that key and
109       decrypt any traffic using that session: even if the cipher suite
110       supports forward secrecy. As a result applications may wish to use
111       multiple keys and avoid using long term keys stored in files.
112
113       Applications can use longer keys to maintain a consistent level of
114       security.  For example if a cipher suite uses 256 bit ciphers but only
115       a 128 bit ticket key the overall security is only 128 bits because
116       breaking the ticket key will enable an attacker to obtain the session
117       keys.
118

RETURN VALUES

120       returns 0 to indicate the callback function was set.
121

EXAMPLES

123       Reference Implementation:
124
125        SSL_CTX_set_tlsext_ticket_key_cb(SSL, ssl_tlsext_ticket_key_cb);
126        ...
127
128        static int ssl_tlsext_ticket_key_cb(SSL *s, unsigned char key_name[16],
129                                            unsigned char *iv, EVP_CIPHER_CTX *ctx,
130                                            HMAC_CTX *hctx, int enc)
131        {
132            your_type_t *key; /* something that you need to implement */
133
134            if (enc) { /* create new session */
135                if (RAND_bytes(iv, EVP_MAX_IV_LENGTH) <= 0)
136                    return -1; /* insufficient random */
137
138                key = currentkey(); /* something that you need to implement */
139                if (key == NULL) {
140                    /* current key doesn't exist or isn't valid */
141                    key = createkey(); /*
142                                        * Something that you need to implement.
143                                        * createkey needs to initialise a name,
144                                        * an aes_key, a hmac_key and optionally
145                                        * an expire time.
146                                        */
147                    if (key == NULL) /* key couldn't be created */
148                        return 0;
149                }
150                memcpy(key_name, key->name, 16);
151
152                EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, key->aes_key, iv);
153                HMAC_Init_ex(&hctx, key->hmac_key, 32, EVP_sha256(), NULL);
154
155                return 1;
156
157            } else { /* retrieve session */
158                time_t t = time(NULL);
159                key = findkey(key_name); /* something that you need to implement */
160
161                if (key == NULL || key->expire < t)
162                    return 0;
163
164                HMAC_Init_ex(&hctx, key->hmac_key, 32, EVP_sha256(), NULL);
165                EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, key->aes_key, iv);
166
167                if (key->expire < t - RENEW_TIME) { /* RENEW_TIME: implement */
168                    /*
169                     * return 2 - This session will get a new ticket even though the
170                     * current one is still valid.
171                     */
172                    return 2;
173                }
174                return 1;
175            }
176        }
177

SEE ALSO

179       ssl(7), SSL_set_session(3), SSL_session_reused(3),
180       SSL_CTX_add_session(3), SSL_CTX_sess_number(3),
181       SSL_CTX_sess_set_get_cb(3), SSL_CTX_set_session_id_context(3),
182
184       Copyright 2014-2020 The OpenSSL Project Authors. All Rights Reserved.
185
186       Licensed under the OpenSSL license (the "License").  You may not use
187       this file except in compliance with the License.  You can obtain a copy
188       in the file LICENSE in the source distribution or at
189       <https://www.openssl.org/source/license.html>.
190
191
192
1931.1.1l                            2021-09-15SSL_CTX_SET_TLSEXT_TICKET_KEY_CB(3)
Impressum