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.8i does not enable TLS extension support by default, so it
8 will need to be enabled by adding enable-tlsext to config script
12 Index: openssl-0.9.8i/ssl/s3_clnt.c
13 ===================================================================
14 --- openssl-0.9.8i.orig/ssl/s3_clnt.c 2008-06-16 19:56:41.000000000 +0300
15 +++ openssl-0.9.8i/ssl/s3_clnt.c 2008-11-23 20:39:40.000000000 +0200
20 +#ifndef OPENSSL_NO_TLSEXT
21 + /* check if we want to resume the session based on external pre-shared secret */
22 + if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
24 + SSL_CIPHER *pref_cipher=NULL;
25 + s->session->master_key_length=sizeof(s->session->master_key);
26 + if (s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
27 + NULL, &pref_cipher, s->tls_session_secret_cb_arg))
29 + s->session->cipher=pref_cipher ?
30 + pref_cipher : ssl_get_cipher_by_char(s,p+j);
33 +#endif /* OPENSSL_NO_TLSEXT */
35 if (j != 0 && j == s->session->session_id_length
36 && memcmp(p,s->session->session_id,j) == 0)
38 @@ -2701,11 +2716,8 @@
42 - /* If we have no ticket or session ID is non-zero length (a match of
43 - * a non-zero session length would never reach here) it cannot be a
46 - if (!s->session->tlsext_tick || s->session->session_id_length)
47 + /* If we have no ticket it cannot be a resumed session. */
48 + if (!s->session->tlsext_tick)
50 /* this function is called when we really expect a Certificate
51 * message, so permit appropriate message length */
52 Index: openssl-0.9.8i/ssl/s3_srvr.c
53 ===================================================================
54 --- openssl-0.9.8i.orig/ssl/s3_srvr.c 2008-09-14 21:16:09.000000000 +0300
55 +++ openssl-0.9.8i/ssl/s3_srvr.c 2008-11-23 20:37:40.000000000 +0200
57 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
61 + /* Check if we want to use external pre-shared secret for this
62 + * handshake for not reused session only. We need to generate
63 + * server_random before calling tls_session_secret_cb in order to allow
64 + * SessionTicket processing to use it in key derivation. */
68 + Time=(unsigned long)time(NULL); /* Time */
69 + pos=s->s3->server_random;
71 + if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0)
73 + al=SSL_AD_INTERNAL_ERROR;
78 + if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
80 + SSL_CIPHER *pref_cipher=NULL;
82 + s->session->master_key_length=sizeof(s->session->master_key);
83 + if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
84 + ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
87 + s->session->ciphers=ciphers;
88 + s->session->verify_result=X509_V_OK;
92 + /* check if some cipher was preferred by call back */
93 + pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
94 + if (pref_cipher == NULL)
96 + al=SSL_AD_HANDSHAKE_FAILURE;
97 + SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
101 + s->session->cipher=pref_cipher;
103 + if (s->cipher_list)
104 + sk_SSL_CIPHER_free(s->cipher_list);
106 + if (s->cipher_list_by_id)
107 + sk_SSL_CIPHER_free(s->cipher_list_by_id);
109 + s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
110 + s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
114 /* Worst case, we will use the NULL compression, but if we have other
115 * options, we will now look for them. We have i-1 compression
116 @@ -1097,16 +1150,22 @@
120 - unsigned long l,Time;
122 +#ifdef OPENSSL_NO_TLSEXT
123 + unsigned long Time;
126 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
128 buf=(unsigned char *)s->init_buf->data;
129 +#ifdef OPENSSL_NO_TLSEXT
130 p=s->s3->server_random;
131 + /* Generate server_random if it was not needed previously */
132 Time=(unsigned long)time(NULL); /* Time */
134 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
137 /* Do the message type and length last */
140 Index: openssl-0.9.8i/ssl/ssl_err.c
141 ===================================================================
142 --- openssl-0.9.8i.orig/ssl/ssl_err.c 2008-08-13 22:44:44.000000000 +0300
143 +++ openssl-0.9.8i/ssl/ssl_err.c 2008-11-23 20:33:43.000000000 +0200
145 {ERR_FUNC(SSL_F_TLS1_ENC), "TLS1_ENC"},
146 {ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK), "TLS1_SETUP_KEY_BLOCK"},
147 {ERR_FUNC(SSL_F_WRITE_PENDING), "WRITE_PENDING"},
148 +{ERR_FUNC(SSL_F_SSL_SET_SESSION_TICKET_EXT), "SSL_set_session_ticket_ext"},
152 Index: openssl-0.9.8i/ssl/ssl.h
153 ===================================================================
154 --- openssl-0.9.8i.orig/ssl/ssl.h 2008-08-13 22:44:44.000000000 +0300
155 +++ openssl-0.9.8i/ssl/ssl.h 2008-11-23 20:35:41.000000000 +0200
157 * 'struct ssl_st *' function parameters used to prototype callbacks
159 typedef struct ssl_st *ssl_crock_st;
160 +typedef struct tls_session_ticket_ext_st TLS_SESSION_TICKET_EXT;
162 /* used to hold info on the particular ciphers used */
163 typedef struct ssl_cipher_st
166 DECLARE_STACK_OF(SSL_CIPHER)
168 +typedef int (*tls_session_ticket_ext_cb_fn)(SSL *s, const unsigned char *data, int len, void *arg);
169 +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);
171 /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
172 typedef struct ssl_method_st
174 @@ -1034,6 +1038,18 @@
176 /* RFC4507 session ticket expected to be received or sent */
177 int tlsext_ticket_expected;
179 + /* TLS Session Ticket extension override */
180 + TLS_SESSION_TICKET_EXT *tlsext_session_ticket;
182 + /* TLS Session Ticket extension callback */
183 + tls_session_ticket_ext_cb_fn tls_session_ticket_ext_cb;
184 + void *tls_session_ticket_ext_cb_arg;
186 + /* TLS pre-shared secret session resumption */
187 + tls_session_secret_cb_fn tls_session_secret_cb;
188 + void *tls_session_secret_cb_arg;
190 SSL_CTX * initial_ctx; /* initial ctx, used to store sessions */
191 #define session_ctx initial_ctx
193 @@ -1632,6 +1648,15 @@
194 int SSL_COMP_add_compression_method(int id,void *cm);
197 +/* TLS extensions functions */
198 +int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len);
200 +int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
203 +/* Pre-shared secret session resumption functions */
204 +int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
206 /* BEGIN ERROR CODES */
207 /* The following lines are auto generated by the script mkerr.pl. Any changes
208 * made after this point may be overwritten when the script is next run.
209 @@ -1824,6 +1849,7 @@
210 #define SSL_F_TLS1_ENC 210
211 #define SSL_F_TLS1_SETUP_KEY_BLOCK 211
212 #define SSL_F_WRITE_PENDING 212
213 +#define SSL_F_SSL_SET_SESSION_TICKET_EXT 213
216 #define SSL_R_APP_DATA_IN_HANDSHAKE 100
217 Index: openssl-0.9.8i/ssl/ssl_sess.c
218 ===================================================================
219 --- openssl-0.9.8i.orig/ssl/ssl_sess.c 2008-06-04 21:35:27.000000000 +0300
220 +++ openssl-0.9.8i/ssl/ssl_sess.c 2008-11-23 20:32:24.000000000 +0200
222 return(s->session_timeout);
225 +#ifndef OPENSSL_NO_TLSEXT
226 +int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
227 + STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
229 + if (s == NULL) return(0);
230 + s->tls_session_secret_cb = tls_session_secret_cb;
231 + s->tls_session_secret_cb_arg = arg;
235 +int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
238 + if (s == NULL) return(0);
239 + s->tls_session_ticket_ext_cb = cb;
240 + s->tls_session_ticket_ext_cb_arg = arg;
244 +int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
246 + if (s->version >= TLS1_VERSION)
248 + if (s->tlsext_session_ticket)
250 + OPENSSL_free(s->tlsext_session_ticket);
251 + s->tlsext_session_ticket = NULL;
254 + s->tlsext_session_ticket = OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
255 + if (!s->tlsext_session_ticket)
257 + SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT, ERR_R_MALLOC_FAILURE);
263 + s->tlsext_session_ticket->length = ext_len;
264 + s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1;
265 + memcpy(s->tlsext_session_ticket->data, ext_data, ext_len);
269 + s->tlsext_session_ticket->length = 0;
270 + s->tlsext_session_ticket->data = NULL;
278 +#endif /* OPENSSL_NO_TLSEXT */
280 typedef struct timeout_param_st
283 Index: openssl-0.9.8i/ssl/t1_lib.c
284 ===================================================================
285 --- openssl-0.9.8i.orig/ssl/t1_lib.c 2008-09-04 01:13:04.000000000 +0300
286 +++ openssl-0.9.8i/ssl/t1_lib.c 2008-11-23 20:31:20.000000000 +0200
289 void tls1_free(SSL *s)
291 +#ifndef OPENSSL_NO_TLSEXT
292 + if (s->tlsext_session_ticket)
294 + OPENSSL_free(s->tlsext_session_ticket);
302 if (s->session && s->session->tlsext_tick)
303 ticklen = s->session->tlsext_ticklen;
304 + else if (s->session && s->tlsext_session_ticket &&
305 + s->tlsext_session_ticket->data)
307 + ticklen = s->tlsext_session_ticket->length;
308 + s->session->tlsext_tick = OPENSSL_malloc(ticklen);
309 + if (!s->session->tlsext_tick)
311 + memcpy(s->session->tlsext_tick,
312 + s->tlsext_session_ticket->data,
314 + s->session->tlsext_ticklen = ticklen;
318 + if (ticklen == 0 && s->tlsext_session_ticket &&
319 + s->tlsext_session_ticket->data == NULL)
321 /* Check for enough room 2 for extension type, 2 for len
330 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
336 + else if (type == TLSEXT_TYPE_session_ticket)
338 + if (s->tls_session_ticket_ext_cb &&
339 + !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
341 + *al = TLS1_AD_INTERNAL_ERROR;
345 else if (type == TLSEXT_TYPE_status_request
346 && s->ctx->tlsext_status_cb)
350 else if (type == TLSEXT_TYPE_session_ticket)
352 + if (s->tls_session_ticket_ext_cb &&
353 + !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
355 + *al = TLS1_AD_INTERNAL_ERROR;
358 if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
362 s->tlsext_ticket_expected = 1;
363 return 0; /* Cache miss */
365 + if (s->tls_session_secret_cb)
367 + /* Indicate cache miss here and instead of
368 + * generating the session from ticket now,
369 + * trigger abbreviated handshake based on
370 + * external mechanism to calculate the master
374 return tls_decrypt_ticket(s, p, size, session_id, len,
377 Index: openssl-0.9.8i/ssl/tls1.h
378 ===================================================================
379 --- openssl-0.9.8i.orig/ssl/tls1.h 2008-04-30 19:11:33.000000000 +0300
380 +++ openssl-0.9.8i/ssl/tls1.h 2008-11-23 20:22:38.000000000 +0200
382 #define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74" /*master secret*/
385 +/* TLS extension struct */
386 +struct tls_session_ticket_ext_st
388 + unsigned short length;
395 Index: openssl-0.9.8i/util/ssleay.num
396 ===================================================================
397 --- openssl-0.9.8i.orig/util/ssleay.num 2008-06-05 13:57:21.000000000 +0300
398 +++ openssl-0.9.8i/util/ssleay.num 2008-11-23 20:22:05.000000000 +0200
400 SSL_get_servername 291 EXIST::FUNCTION:TLSEXT
401 SSL_get_servername_type 292 EXIST::FUNCTION:TLSEXT
402 SSL_CTX_set_client_cert_engine 293 EXIST::FUNCTION:ENGINE
403 +SSL_set_session_ticket_ext 306 EXIST::FUNCTION:TLSEXT
404 +SSL_set_session_secret_cb 307 EXIST::FUNCTION:TLSEXT