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 OpenSSL 0.9.8x does not enable TLS extension support by default, so it
8 will need to be enabled by adding enable-tlsext to config script
12 diff -upr openssl-0.9.8x.orig/ssl/s3_clnt.c openssl-0.9.8x/ssl/s3_clnt.c
13 --- openssl-0.9.8x.orig/ssl/s3_clnt.c 2011-12-26 21:38:28.000000000 +0200
14 +++ openssl-0.9.8x/ssl/s3_clnt.c 2012-07-07 10:46:31.501140621 +0300
15 @@ -757,6 +757,21 @@ int ssl3_get_server_hello(SSL *s)
19 +#ifndef OPENSSL_NO_TLSEXT
20 + /* check if we want to resume the session based on external pre-shared secret */
21 + if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
23 + SSL_CIPHER *pref_cipher=NULL;
24 + s->session->master_key_length=sizeof(s->session->master_key);
25 + if (s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
26 + NULL, &pref_cipher, s->tls_session_secret_cb_arg))
28 + s->session->cipher=pref_cipher ?
29 + pref_cipher : ssl_get_cipher_by_char(s,p+j);
32 +#endif /* OPENSSL_NO_TLSEXT */
34 if (j != 0 && j == s->session->session_id_length
35 && memcmp(p,s->session->session_id,j) == 0)
37 @@ -2725,11 +2740,8 @@ int ssl3_check_finished(SSL *s)
41 - /* If we have no ticket or session ID is non-zero length (a match of
42 - * a non-zero session length would never reach here) it cannot be a
45 - if (!s->session->tlsext_tick || s->session->session_id_length)
46 + /* If we have no ticket it cannot be a resumed session. */
47 + if (!s->session->tlsext_tick)
49 /* this function is called when we really expect a Certificate
50 * message, so permit appropriate message length */
51 diff -upr openssl-0.9.8x.orig/ssl/s3_srvr.c openssl-0.9.8x/ssl/s3_srvr.c
52 --- openssl-0.9.8x.orig/ssl/s3_srvr.c 2012-02-16 17:21:17.000000000 +0200
53 +++ openssl-0.9.8x/ssl/s3_srvr.c 2012-07-07 10:46:31.501140621 +0300
54 @@ -1009,6 +1009,59 @@ int ssl3_get_client_hello(SSL *s)
55 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
59 + /* Check if we want to use external pre-shared secret for this
60 + * handshake for not reused session only. We need to generate
61 + * server_random before calling tls_session_secret_cb in order to allow
62 + * SessionTicket processing to use it in key derivation. */
66 + Time=(unsigned long)time(NULL); /* Time */
67 + pos=s->s3->server_random;
69 + if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0)
71 + al=SSL_AD_INTERNAL_ERROR;
76 + if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
78 + SSL_CIPHER *pref_cipher=NULL;
80 + s->session->master_key_length=sizeof(s->session->master_key);
81 + if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
82 + ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
85 + s->session->ciphers=ciphers;
86 + s->session->verify_result=X509_V_OK;
90 + /* check if some cipher was preferred by call back */
91 + pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
92 + if (pref_cipher == NULL)
94 + al=SSL_AD_HANDSHAKE_FAILURE;
95 + SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
99 + s->session->cipher=pref_cipher;
101 + if (s->cipher_list)
102 + sk_SSL_CIPHER_free(s->cipher_list);
104 + if (s->cipher_list_by_id)
105 + sk_SSL_CIPHER_free(s->cipher_list_by_id);
107 + s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
108 + s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
112 /* Worst case, we will use the NULL compression, but if we have other
113 * options, we will now look for them. We have i-1 compression
114 @@ -1147,16 +1200,22 @@ int ssl3_send_server_hello(SSL *s)
118 - unsigned long l,Time;
120 +#ifdef OPENSSL_NO_TLSEXT
121 + unsigned long Time;
124 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
126 buf=(unsigned char *)s->init_buf->data;
127 +#ifdef OPENSSL_NO_TLSEXT
128 p=s->s3->server_random;
129 + /* Generate server_random if it was not needed previously */
130 Time=(unsigned long)time(NULL); /* Time */
132 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
135 /* Do the message type and length last */
138 diff -upr openssl-0.9.8x.orig/ssl/ssl_err.c openssl-0.9.8x/ssl/ssl_err.c
139 --- openssl-0.9.8x.orig/ssl/ssl_err.c 2012-03-12 16:50:55.000000000 +0200
140 +++ openssl-0.9.8x/ssl/ssl_err.c 2012-07-07 10:46:31.501140621 +0300
141 @@ -264,6 +264,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
142 {ERR_FUNC(SSL_F_TLS1_ENC), "TLS1_ENC"},
143 {ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK), "TLS1_SETUP_KEY_BLOCK"},
144 {ERR_FUNC(SSL_F_WRITE_PENDING), "WRITE_PENDING"},
145 +{ERR_FUNC(SSL_F_SSL_SET_SESSION_TICKET_EXT), "SSL_set_session_ticket_ext"},
149 diff -upr openssl-0.9.8x.orig/ssl/ssl.h openssl-0.9.8x/ssl/ssl.h
150 --- openssl-0.9.8x.orig/ssl/ssl.h 2012-03-12 16:50:55.000000000 +0200
151 +++ openssl-0.9.8x/ssl/ssl.h 2012-07-07 10:46:31.501140621 +0300
152 @@ -344,6 +344,7 @@ extern "C" {
153 * 'struct ssl_st *' function parameters used to prototype callbacks
155 typedef struct ssl_st *ssl_crock_st;
156 +typedef struct tls_session_ticket_ext_st TLS_SESSION_TICKET_EXT;
158 /* used to hold info on the particular ciphers used */
159 typedef struct ssl_cipher_st
160 @@ -362,6 +363,9 @@ typedef struct ssl_cipher_st
162 DECLARE_STACK_OF(SSL_CIPHER)
164 +typedef int (*tls_session_ticket_ext_cb_fn)(SSL *s, const unsigned char *data, int len, void *arg);
165 +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);
167 /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
168 typedef struct ssl_method_st
170 @@ -1050,6 +1054,18 @@ struct ssl_st
172 /* RFC4507 session ticket expected to be received or sent */
173 int tlsext_ticket_expected;
175 + /* TLS Session Ticket extension override */
176 + TLS_SESSION_TICKET_EXT *tlsext_session_ticket;
178 + /* TLS Session Ticket extension callback */
179 + tls_session_ticket_ext_cb_fn tls_session_ticket_ext_cb;
180 + void *tls_session_ticket_ext_cb_arg;
182 + /* TLS pre-shared secret session resumption */
183 + tls_session_secret_cb_fn tls_session_secret_cb;
184 + void *tls_session_secret_cb_arg;
186 SSL_CTX * initial_ctx; /* initial ctx, used to store sessions */
187 #define session_ctx initial_ctx
189 @@ -1663,6 +1679,15 @@ void *SSL_COMP_get_compression_methods(v
190 int SSL_COMP_add_compression_method(int id,void *cm);
193 +/* TLS extensions functions */
194 +int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len);
196 +int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
199 +/* Pre-shared secret session resumption functions */
200 +int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
202 /* BEGIN ERROR CODES */
203 /* The following lines are auto generated by the script mkerr.pl. Any changes
204 * made after this point may be overwritten when the script is next run.
205 @@ -1866,6 +1891,7 @@ void ERR_load_SSL_strings(void);
206 #define SSL_F_TLS1_ENC 210
207 #define SSL_F_TLS1_SETUP_KEY_BLOCK 211
208 #define SSL_F_WRITE_PENDING 212
209 +#define SSL_F_SSL_SET_SESSION_TICKET_EXT 213
212 #define SSL_R_APP_DATA_IN_HANDSHAKE 100
213 diff -upr openssl-0.9.8x.orig/ssl/ssl_sess.c openssl-0.9.8x/ssl/ssl_sess.c
214 --- openssl-0.9.8x.orig/ssl/ssl_sess.c 2010-02-01 18:48:40.000000000 +0200
215 +++ openssl-0.9.8x/ssl/ssl_sess.c 2012-07-07 10:46:31.501140621 +0300
216 @@ -712,6 +712,61 @@ long SSL_CTX_get_timeout(const SSL_CTX *
217 return(s->session_timeout);
220 +#ifndef OPENSSL_NO_TLSEXT
221 +int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
222 + STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
224 + if (s == NULL) return(0);
225 + s->tls_session_secret_cb = tls_session_secret_cb;
226 + s->tls_session_secret_cb_arg = arg;
230 +int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
233 + if (s == NULL) return(0);
234 + s->tls_session_ticket_ext_cb = cb;
235 + s->tls_session_ticket_ext_cb_arg = arg;
239 +int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
241 + if (s->version >= TLS1_VERSION)
243 + if (s->tlsext_session_ticket)
245 + OPENSSL_free(s->tlsext_session_ticket);
246 + s->tlsext_session_ticket = NULL;
249 + s->tlsext_session_ticket = OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
250 + if (!s->tlsext_session_ticket)
252 + SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT, ERR_R_MALLOC_FAILURE);
258 + s->tlsext_session_ticket->length = ext_len;
259 + s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1;
260 + memcpy(s->tlsext_session_ticket->data, ext_data, ext_len);
264 + s->tlsext_session_ticket->length = 0;
265 + s->tlsext_session_ticket->data = NULL;
273 +#endif /* OPENSSL_NO_TLSEXT */
275 typedef struct timeout_param_st
278 diff -upr openssl-0.9.8x.orig/ssl/t1_lib.c openssl-0.9.8x/ssl/t1_lib.c
279 --- openssl-0.9.8x.orig/ssl/t1_lib.c 2012-01-04 16:25:10.000000000 +0200
280 +++ openssl-0.9.8x/ssl/t1_lib.c 2012-07-07 10:47:31.153140501 +0300
281 @@ -106,6 +106,12 @@ int tls1_new(SSL *s)
283 void tls1_free(SSL *s)
285 +#ifndef OPENSSL_NO_TLSEXT
286 + if (s->tlsext_session_ticket)
288 + OPENSSL_free(s->tlsext_session_ticket);
294 @@ -206,8 +212,23 @@ unsigned char *ssl_add_clienthello_tlsex
296 if (!s->new_session && s->session && s->session->tlsext_tick)
297 ticklen = s->session->tlsext_ticklen;
298 + else if (s->session && s->tlsext_session_ticket &&
299 + s->tlsext_session_ticket->data)
301 + ticklen = s->tlsext_session_ticket->length;
302 + s->session->tlsext_tick = OPENSSL_malloc(ticklen);
303 + if (!s->session->tlsext_tick)
305 + memcpy(s->session->tlsext_tick,
306 + s->tlsext_session_ticket->data,
308 + s->session->tlsext_ticklen = ticklen;
312 + if (ticklen == 0 && s->tlsext_session_ticket &&
313 + s->tlsext_session_ticket->data == NULL)
315 /* Check for enough room 2 for extension type, 2 for len
318 @@ -221,6 +242,7 @@ unsigned char *ssl_add_clienthello_tlsex
324 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp &&
325 s->version != DTLS1_VERSION)
326 @@ -486,6 +508,15 @@ int ssl_parse_clienthello_tlsext(SSL *s,
328 renegotiate_seen = 1;
330 + else if (type == TLSEXT_TYPE_session_ticket)
332 + if (s->tls_session_ticket_ext_cb &&
333 + !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
335 + *al = TLS1_AD_INTERNAL_ERROR;
339 else if (type == TLSEXT_TYPE_status_request &&
340 s->version != DTLS1_VERSION && s->ctx->tlsext_status_cb)
342 @@ -663,6 +694,12 @@ int ssl_parse_serverhello_tlsext(SSL *s,
344 else if (type == TLSEXT_TYPE_session_ticket)
346 + if (s->tls_session_ticket_ext_cb &&
347 + !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
349 + *al = TLS1_AD_INTERNAL_ERROR;
352 if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
355 @@ -920,6 +957,15 @@ int tls1_process_ticket(SSL *s, unsigned
356 s->tlsext_ticket_expected = 1;
357 return 0; /* Cache miss */
359 + if (s->tls_session_secret_cb)
361 + /* Indicate cache miss here and instead of
362 + * generating the session from ticket now,
363 + * trigger abbreviated handshake based on
364 + * external mechanism to calculate the master
368 return tls_decrypt_ticket(s, p, size, session_id, len,
371 diff -upr openssl-0.9.8x.orig/ssl/tls1.h openssl-0.9.8x/ssl/tls1.h
372 --- openssl-0.9.8x.orig/ssl/tls1.h 2009-11-08 16:51:54.000000000 +0200
373 +++ openssl-0.9.8x/ssl/tls1.h 2012-07-07 10:46:31.501140621 +0300
374 @@ -401,6 +401,13 @@ SSL_CTX_callback_ctrl(ssl,SSL_CTRL_SET_T
375 #define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74" /*master secret*/
378 +/* TLS extension struct */
379 +struct tls_session_ticket_ext_st
381 + unsigned short length;
388 diff -upr openssl-0.9.8x.orig/util/ssleay.num openssl-0.9.8x/util/ssleay.num
389 --- openssl-0.9.8x.orig/util/ssleay.num 2008-06-05 13:57:21.000000000 +0300
390 +++ openssl-0.9.8x/util/ssleay.num 2012-07-07 10:46:31.505140623 +0300
391 @@ -242,3 +242,5 @@ SSL_set_SSL_CTX
392 SSL_get_servername 291 EXIST::FUNCTION:TLSEXT
393 SSL_get_servername_type 292 EXIST::FUNCTION:TLSEXT
394 SSL_CTX_set_client_cert_engine 293 EXIST::FUNCTION:ENGINE
395 +SSL_set_session_ticket_ext 306 EXIST::FUNCTION:TLSEXT
396 +SSL_set_session_secret_cb 307 EXIST::FUNCTION:TLSEXT