SSL_CTX_set_tlsext_ticket_key_cb.pod revision 279265
1=pod
2
3=head1 NAME
4
5SSL_CTX_set_tlsext_ticket_key_cb - set a callback for session ticket processing
6
7=head1 SYNOPSIS
8
9 #include <openssl/tls1.h>
10
11 long SSL_CTX_set_tlsext_ticket_key_cb(SSL_CTX sslctx,
12        int (*cb)(SSL *s, unsigned char key_name[16],
13	          unsigned char iv[EVP_MAX_IV_LENGTH],
14		  EVP_CIPHER_CTX *ctx, HMAC_CTX *hctx, int enc));
15
16=head1 DESCRIPTION
17
18SSL_CTX_set_tlsext_ticket_key_cb() sets a callback fuction I<cb> for handling 
19session tickets for the ssl context I<sslctx>. Session tickets, defined in 
20RFC5077 provide an enhanced session resumption capability where the server
21implementation is not required to maintain per session state. It only applies
22to TLS and there is no SSLv3 implementation.
23
24The callback is available when the OpenSSL library was built without 
25I<OPENSSL_NO_TLSEXT> being defined.
26
27The callback function I<cb> will be called for every client instigated TLS
28session when session ticket extension is presented in the TLS hello
29message. It is the responsibility of this function to create or retrieve the
30cryptographic parameters and to maintain their state.
31
32The OpenSSL library uses your callback function to help implement a common TLS 
33ticket construction state according to RFC5077 Section 4 such that per session
34state is unnecessary and a small set of cryptographic variables needs to be 
35maintained by the callback function implementation.
36
37In order to reuse a session, a TLS client must send the a session ticket
38extension to the server. The client can only send exactly one session ticket.
39The server, through the callback function, either agrees to reuse the session
40ticket information or it starts a full TLS handshake to create a new session
41ticket.
42
43Before the callback function is started I<ctx> and I<hctx> have been 
44initialised with EVP_CIPHER_CTX_init and HMAC_CTX_init respectively.
45
46For new sessions tickets, when the client doesn't present a session ticket, or
47an attempted retreival of the ticket failed, or a renew option was indicated,
48the callback function will be called with I<enc> equal to 1. The OpenSSL
49library expects that the function will set an arbitary I<name>, initialize
50I<iv>, and set the cipher context I<ctx> and the hash context I<hctx>.
51
52The I<name> is 16 characters long and is used as a key identifier.
53
54The I<iv> length is the length of the IV of the corresponding cipher. The
55maximum IV length is L<EVP_MAX_IV_LENGTH> bytes defined in B<evp.h>.
56
57The initialization vector I<iv> should be a random value. The cipher context 
58I<ctx> should use the initialisation vector I<iv>. The cipher context can be 
59set using L<EVP_EncryptInit_ex>. The hmac context can be set using L<HMAC_Init_ex>.
60
61When the client presents a session ticket, the callback function with be called 
62with I<enc> set to 0 indicating that the I<cb> function should retreive a set
63of parameters. In this case I<name> and I<iv> have already been parsed out of
64the session ticket. The OpenSSL library expects that the I<name> will be used
65to retrieve a cryptographic parameters and that the cryptographic context
66I<ctx> will be set with the retreived parameters and the initialization vector
67I<iv>. using a function like L<EVP_DecryptInit_ex>. The I<hctx> needs to be set
68using L<HMAC_Init_ex>.
69
70If the I<name> is still valid but a renewal of the ticket is required the
71callback function should return 2. The library will call the callback again
72with an arguement of enc equal to 1 to set the new ticket.
73
74The return value of the I<cb> function is used by OpenSSL to determine what
75further processing will occur. The following return values have meaning:
76
77=over 4
78
79=item Z<>2
80
81This indicates that the I<ctx> and I<hctx> have been set and the session can 
82continue on those parameters. Additionally it indicates that the session
83ticket is in a renewal period and should be replaced. The OpenSSL library will
84call I<cb> again with an enc argument of 1 to set the new ticket (see RFC5077
853.3 paragraph 2).
86
87=item Z<>1
88
89This indicates that the I<ctx> and I<hctx> have been set and the session can 
90continue on those parameters.
91
92=item Z<>0
93
94This indicates that it was not possible to set/retrieve a session ticket and 
95the SSL/TLS session will continue by by negiotationing a set of cryptographic
96parameters or using the alternate SSL/TLS resumption mechanism, session ids.
97
98If called with enc equal to 0 the library will call the I<cb> again to get
99a new set of parameters.
100
101=item less than 0
102
103This indicates an error.
104
105=back
106
107=head1 NOTES
108
109Session resumption shortcuts the TLS so that the client certificate
110negiotation don't occur. It makes up for this by storing client certificate
111an all other negotiated state information encrypted within the ticket. In a
112resumed session the applications will have all this state information available
113exactly as if a full negiotation had occured.
114
115If an attacker can obtain the key used to encrypt a session ticket, they can
116obtain the master secret for any ticket using that key and decrypt any traffic
117using that session: even if the ciphersuite supports forward secrecy. As
118a result applications may wish to use multiple keys and avoid using long term
119keys stored in files.
120
121Applications can use longer keys to maintain a consistent level of security.
122For example if a ciphersuite uses 256 bit ciphers but only a 128 bit ticket key
123the overall security is only 128 bits because breaking the ticket key will
124enable an attacker to obtain the session keys.
125
126=head1 EXAMPLES
127
128Reference Implemention:
129  SSL_CTX_set_tlsext_ticket_key_cb(SSL,ssl_tlsext_ticket_key_cb);
130  ....
131
132  static int ssl_tlsext_ticket_key_cb(SSL *s, unsigned char key_name[16], unsigned char *iv, EVP_CIPHER_CTX *ctx, HMAC_CTX *hctx, int enc)
133  {
134      if (enc) { /* create new session */
135          if (RAND_bytes(iv, EVP_MAX_IV_LENGTH) ) {
136              return -1; /* insufficient random */
137          }
138  
139          key = currentkey(); /* something that you need to implement */
140          if ( !key ) {
141              /* current key doesn't exist or isn't valid */
142              key = createkey(); /* 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              if ( !key ) { /* key couldn't be created */
147                  return 0;
148              }
149          }
150          memcpy(key_name, key->name, 16);
151  
152          EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, key->aes_key, iv);
153          HMAC_Init_ex(&hctx, key->hmac_key, 16, EVP_sha256(), NULL);
154  
155          return 1;
156  
157      } else { /* retrieve session */
158          key = findkey(name);
159  
160          if  (!key || key->expire < now() ) {
161              return 0;
162          }
163  
164          HMAC_Init_ex(&hctx, key->hmac_key, 16, EVP_sha256(), NULL);
165          EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, key->aes_key, iv );
166
167          if (key->expire < ( now() - RENEW_TIME ) ) {
168              /* return 2 - this session will get a new ticket even though the current is still valid */
169              return 2;
170          }
171          return 1;
172  
173      }
174  }
175
176
177
178=head1 RETURN VALUES
179
180returns 0 to indicate the callback function was set.
181
182=head1 SEE ALSO
183
184L<ssl(3)|ssl(3)>, L<SSL_set_session(3)|SSL_set_session(3)>,
185L<SSL_session_reused(3)|SSL_session_reused(3)>,
186L<SSL_CTX_add_session(3)|SSL_CTX_add_session(3)>,
187L<SSL_CTX_sess_number(3)|SSL_CTX_sess_number(3)>,
188L<SSL_CTX_sess_set_get_cb(3)|SSL_CTX_sess_set_get_cb(3)>,
189L<SSL_CTX_set_session_id_context(3)|SSL_CTX_set_session_id_context(3)>,
190
191=head1 HISTORY
192
193This function was introduced in OpenSSL 0.9.8h
194
195=cut
196