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