1 This patch adds support for TLS SessionTicket extension (RFC 5077) for
2 the parts used by EAP-FAST (RFC 4851).
4 This is based on the patch from Alexey Kobozev <akobozev@cisco.com>
5 (sent to openssl-dev mailing list on Tue, 07 Jun 2005 15:40:58 +0300).
7 NOTE: This patch (without SSL_set_hello_extension() wrapper) was
8 merged into the upstream OpenSSL 0.9.9 tree and as such, an external
9 patch for EAP-FAST support is not needed anymore.
13 Index: openssl-SNAP-20081111/ssl/s3_clnt.c
14 ===================================================================
15 --- openssl-SNAP-20081111.orig/ssl/s3_clnt.c
16 +++ openssl-SNAP-20081111/ssl/s3_clnt.c
17 @@ -788,6 +788,23 @@ int ssl3_get_server_hello(SSL *s)
21 +#ifndef OPENSSL_NO_TLSEXT
22 + /* check if we want to resume the session based on external pre-shared secret */
23 + if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
25 + SSL_CIPHER *pref_cipher=NULL;
26 + s->session->master_key_length=sizeof(s->session->master_key);
27 + if (s->tls_session_secret_cb(s, s->session->master_key,
28 + &s->session->master_key_length,
30 + s->tls_session_secret_cb_arg))
32 + s->session->cipher = pref_cipher ?
33 + pref_cipher : ssl_get_cipher_by_char(s, p+j);
36 +#endif /* OPENSSL_NO_TLSEXT */
38 if (j != 0 && j == s->session->session_id_length
39 && memcmp(p,s->session->session_id,j) == 0)
41 @@ -2927,11 +2944,8 @@ static int ssl3_check_finished(SSL *s)
45 - /* If we have no ticket or session ID is non-zero length (a match of
46 - * a non-zero session length would never reach here) it cannot be a
49 - if (!s->session->tlsext_tick || s->session->session_id_length)
50 + /* If we have no ticket it cannot be a resumed session. */
51 + if (!s->session->tlsext_tick)
53 /* this function is called when we really expect a Certificate
54 * message, so permit appropriate message length */
55 Index: openssl-SNAP-20081111/ssl/s3_srvr.c
56 ===================================================================
57 --- openssl-SNAP-20081111.orig/ssl/s3_srvr.c
58 +++ openssl-SNAP-20081111/ssl/s3_srvr.c
59 @@ -1010,6 +1010,59 @@ int ssl3_get_client_hello(SSL *s)
60 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
64 + /* Check if we want to use external pre-shared secret for this
65 + * handshake for not reused session only. We need to generate
66 + * server_random before calling tls_session_secret_cb in order to allow
67 + * SessionTicket processing to use it in key derivation. */
71 + Time=(unsigned long)time(NULL); /* Time */
72 + pos=s->s3->server_random;
74 + if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0)
76 + al=SSL_AD_INTERNAL_ERROR;
81 + if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
83 + SSL_CIPHER *pref_cipher=NULL;
85 + s->session->master_key_length=sizeof(s->session->master_key);
86 + if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
87 + ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
90 + s->session->ciphers=ciphers;
91 + s->session->verify_result=X509_V_OK;
95 + /* check if some cipher was preferred by call back */
96 + pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
97 + if (pref_cipher == NULL)
99 + al=SSL_AD_HANDSHAKE_FAILURE;
100 + SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
104 + s->session->cipher=pref_cipher;
106 + if (s->cipher_list)
107 + sk_SSL_CIPHER_free(s->cipher_list);
109 + if (s->cipher_list_by_id)
110 + sk_SSL_CIPHER_free(s->cipher_list_by_id);
112 + s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
113 + s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
118 /* Worst case, we will use the NULL compression, but if we have other
119 @@ -1134,16 +1187,22 @@ int ssl3_send_server_hello(SSL *s)
123 - unsigned long l,Time;
125 +#ifdef OPENSSL_NO_TLSEXT
126 + unsigned long Time;
129 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
131 buf=(unsigned char *)s->init_buf->data;
132 +#ifdef OPENSSL_NO_TLSEXT
133 p=s->s3->server_random;
134 + /* Generate server_random if it was not needed previously */
135 Time=(unsigned long)time(NULL); /* Time */
137 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
140 /* Do the message type and length last */
143 Index: openssl-SNAP-20081111/ssl/ssl_err.c
144 ===================================================================
145 --- openssl-SNAP-20081111.orig/ssl/ssl_err.c
146 +++ openssl-SNAP-20081111/ssl/ssl_err.c
147 @@ -263,6 +263,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
148 {ERR_FUNC(SSL_F_TLS1_PRF), "tls1_prf"},
149 {ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK), "TLS1_SETUP_KEY_BLOCK"},
150 {ERR_FUNC(SSL_F_WRITE_PENDING), "WRITE_PENDING"},
151 +{ERR_FUNC(SSL_F_SSL_SET_SESSION_TICKET_EXT), "SSL_set_session_ticket_ext"},
155 Index: openssl-SNAP-20081111/ssl/ssl.h
156 ===================================================================
157 --- openssl-SNAP-20081111.orig/ssl/ssl.h
158 +++ openssl-SNAP-20081111/ssl/ssl.h
159 @@ -355,6 +355,7 @@ extern "C" {
160 * 'struct ssl_st *' function parameters used to prototype callbacks
162 typedef struct ssl_st *ssl_crock_st;
163 +typedef struct tls_session_ticket_ext_st TLS_SESSION_TICKET_EXT;
165 /* used to hold info on the particular ciphers used */
166 typedef struct ssl_cipher_st
167 @@ -378,6 +379,8 @@ typedef struct ssl_cipher_st
169 DECLARE_STACK_OF(SSL_CIPHER)
171 +typedef int (*tls_session_secret_cb_fn)(SSL *s, void *secret, int *secret_len, STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg);
173 /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
174 typedef struct ssl_method_st
176 @@ -1145,6 +1148,13 @@ struct ssl_st
177 void *tlsext_opaque_prf_input;
178 size_t tlsext_opaque_prf_input_len;
180 + /* TLS Session Ticket extension override */
181 + TLS_SESSION_TICKET_EXT *tlsext_session_ticket;
183 + /* TLS pre-shared secret session resumption */
184 + tls_session_secret_cb_fn tls_session_secret_cb;
185 + void *tls_session_secret_cb_arg;
187 SSL_CTX * initial_ctx; /* initial ctx, used to store sessions */
188 #define session_ctx initial_ctx
190 @@ -1746,6 +1756,16 @@ void *SSL_COMP_get_compression_methods(v
191 int SSL_COMP_add_compression_method(int id,void *cm);
194 +/* NOTE: This function will be removed; it is only here for backwards
195 + * compatibility for the API during testing. */
196 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len);
198 +/* TLS extensions functions */
199 +int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len);
201 +/* Pre-shared secret session resumption functions */
202 +int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
204 /* BEGIN ERROR CODES */
205 /* The following lines are auto generated by the script mkerr.pl. Any changes
206 * made after this point may be overwritten when the script is next run.
207 @@ -1948,6 +1968,7 @@ void ERR_load_SSL_strings(void);
208 #define SSL_F_TLS1_PRF 284
209 #define SSL_F_TLS1_SETUP_KEY_BLOCK 211
210 #define SSL_F_WRITE_PENDING 212
211 +#define SSL_F_SSL_SET_SESSION_TICKET_EXT 213
214 #define SSL_R_APP_DATA_IN_HANDSHAKE 100
215 Index: openssl-SNAP-20081111/ssl/ssl_sess.c
216 ===================================================================
217 --- openssl-SNAP-20081111.orig/ssl/ssl_sess.c
218 +++ openssl-SNAP-20081111/ssl/ssl_sess.c
219 @@ -834,6 +834,62 @@ long SSL_CTX_get_timeout(const SSL_CTX *
220 return(s->session_timeout);
223 +#ifndef OPENSSL_NO_TLSEXT
224 +int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
225 + STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
227 + if (s == NULL) return(0);
228 + s->tls_session_secret_cb = tls_session_secret_cb;
229 + s->tls_session_secret_cb_arg = arg;
233 +int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
235 + if (s->version >= TLS1_VERSION)
237 + if (s->tlsext_session_ticket)
239 + OPENSSL_free(s->tlsext_session_ticket);
240 + s->tlsext_session_ticket = NULL;
243 + s->tlsext_session_ticket = OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
244 + if (!s->tlsext_session_ticket)
246 + SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT, ERR_R_MALLOC_FAILURE);
252 + s->tlsext_session_ticket->length = ext_len;
253 + s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1;
254 + memcpy(s->tlsext_session_ticket->data, ext_data, ext_len);
258 + s->tlsext_session_ticket->length = 0;
259 + s->tlsext_session_ticket->data = NULL;
268 +/* NOTE: This function will be removed; it is only here for backwards
269 + * compatibility for the API during testing. */
270 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len)
272 + if (ext_type != TLSEXT_TYPE_session_ticket)
275 + return SSL_set_session_ticket_ext(s, ext_data, ext_len);
277 +#endif /* OPENSSL_NO_TLSEXT */
279 typedef struct timeout_param_st
282 Index: openssl-SNAP-20081111/ssl/t1_lib.c
283 ===================================================================
284 --- openssl-SNAP-20081111.orig/ssl/t1_lib.c
285 +++ openssl-SNAP-20081111/ssl/t1_lib.c
286 @@ -154,6 +154,12 @@ int tls1_new(SSL *s)
288 void tls1_free(SSL *s)
290 +#ifndef OPENSSL_NO_TLSEXT
291 + if (s->tlsext_session_ticket)
293 + OPENSSL_free(s->tlsext_session_ticket);
295 +#endif /* OPENSSL_NO_TLSEXT */
299 @@ -357,8 +363,23 @@ unsigned char *ssl_add_clienthello_tlsex
301 if (s->session && s->session->tlsext_tick)
302 ticklen = s->session->tlsext_ticklen;
303 + else if (s->session && s->tlsext_session_ticket &&
304 + s->tlsext_session_ticket->data)
306 + ticklen = s->tlsext_session_ticket->length;
307 + s->session->tlsext_tick = OPENSSL_malloc(ticklen);
308 + if (!s->session->tlsext_tick)
310 + memcpy(s->session->tlsext_tick,
311 + s->tlsext_session_ticket->data,
313 + s->session->tlsext_ticklen = ticklen;
317 + if (ticklen == 0 && s->tlsext_session_ticket &&
318 + s->tlsext_session_ticket->data == NULL)
320 /* Check for enough room 2 for extension type, 2 for len
323 @@ -371,6 +392,7 @@ unsigned char *ssl_add_clienthello_tlsex
329 #ifdef TLSEXT_TYPE_opaque_prf_input
330 if (s->s3->client_opaque_prf_input != NULL)
331 @@ -1435,6 +1457,15 @@ int tls1_process_ticket(SSL *s, unsigned
332 s->tlsext_ticket_expected = 1;
333 return 0; /* Cache miss */
335 + if (s->tls_session_secret_cb)
337 + /* Indicate cache miss here and instead of
338 + * generating the session from ticket now,
339 + * trigger abbreviated handshake based on
340 + * external mechanism to calculate the master
344 return tls_decrypt_ticket(s, p, size, session_id, len,
347 Index: openssl-SNAP-20081111/ssl/tls1.h
348 ===================================================================
349 --- openssl-SNAP-20081111.orig/ssl/tls1.h
350 +++ openssl-SNAP-20081111/ssl/tls1.h
351 @@ -512,6 +512,13 @@ SSL_CTX_callback_ctrl(ssl,SSL_CTRL_SET_T
352 #define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74" /*master secret*/
355 +/* TLS Session Ticket extension struct */
356 +struct tls_session_ticket_ext_st
358 + unsigned short length;
365 Index: openssl-SNAP-20081111/util/ssleay.num
366 ===================================================================
367 --- openssl-SNAP-20081111.orig/util/ssleay.num
368 +++ openssl-SNAP-20081111/util/ssleay.num
369 @@ -254,3 +254,5 @@ PEM_read_bio_SSL_SESSION
370 SSL_CTX_set_psk_server_callback 303 EXIST::FUNCTION:PSK
371 SSL_get_psk_identity 304 EXIST::FUNCTION:PSK
372 PEM_write_SSL_SESSION 305 EXIST:!WIN16:FUNCTION:
373 +SSL_set_session_ticket_ext 306 EXIST::FUNCTION:TLSEXT
374 +SSL_set_session_secret_cb 307 EXIST::FUNCTION:TLSEXT