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 diff -upr openssl-0.9.8i.orig/ssl/s3_clnt.c openssl-0.9.8i/ssl/s3_clnt.c
13 --- openssl-0.9.8i.orig/ssl/s3_clnt.c 2008-06-16 19:56:41.000000000 +0300
14 +++ openssl-0.9.8i/ssl/s3_clnt.c 2008-09-28 16:50:18.000000000 +0300
15 @@ -759,6 +759,20 @@ 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 ? pref_cipher : ssl_get_cipher_by_char(s,p+j);
31 +#endif /* OPENSSL_NO_TLSEXT */
33 if (j != 0 && j == s->session->session_id_length
34 && memcmp(p,s->session->session_id,j) == 0)
36 @@ -2701,11 +2715,8 @@ static int ssl3_check_finished(SSL *s)
40 - /* If we have no ticket or session ID is non-zero length (a match of
41 - * a non-zero session length would never reach here) it cannot be a
44 - if (!s->session->tlsext_tick || s->session->session_id_length)
45 + /* If we have no ticket it cannot be a resumed session. */
46 + if (!s->session->tlsext_tick)
48 /* this function is called when we really expect a Certificate
49 * message, so permit appropriate message length */
50 diff -upr openssl-0.9.8i.orig/ssl/s3_srvr.c openssl-0.9.8i/ssl/s3_srvr.c
51 --- openssl-0.9.8i.orig/ssl/s3_srvr.c 2008-09-14 21:16:09.000000000 +0300
52 +++ openssl-0.9.8i/ssl/s3_srvr.c 2008-09-28 16:50:18.000000000 +0300
53 @@ -959,6 +959,59 @@ int ssl3_get_client_hello(SSL *s)
54 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
58 + /* Check if we want to use external pre-shared secret for this
59 + * handshake for not reused session only. We need to generate
60 + * server_random before calling tls_session_secret_cb in order to allow
61 + * SessionTicket processing to use it in key derivation. */
65 + Time=(unsigned long)time(NULL); /* Time */
66 + pos=s->s3->server_random;
68 + if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0)
70 + al=SSL_AD_INTERNAL_ERROR;
75 + if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
77 + SSL_CIPHER *pref_cipher=NULL;
79 + s->session->master_key_length=sizeof(s->session->master_key);
80 + if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
81 + ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
84 + s->session->ciphers=ciphers;
85 + s->session->verify_result=X509_V_OK;
89 + /* check if some cipher was preferred by call back */
90 + pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
91 + if (pref_cipher == NULL)
93 + al=SSL_AD_HANDSHAKE_FAILURE;
94 + SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
98 + s->session->cipher=pref_cipher;
100 + if (s->cipher_list)
101 + sk_SSL_CIPHER_free(s->cipher_list);
103 + if (s->cipher_list_by_id)
104 + sk_SSL_CIPHER_free(s->cipher_list_by_id);
106 + s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
107 + s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
111 /* Worst case, we will use the NULL compression, but if we have other
112 * options, we will now look for them. We have i-1 compression
113 @@ -1097,16 +1150,22 @@ int ssl3_send_server_hello(SSL *s)
117 - unsigned long l,Time;
119 +#ifdef OPENSSL_NO_TLSEXT
120 + unsigned long Time;
123 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
125 buf=(unsigned char *)s->init_buf->data;
126 +#ifdef OPENSSL_NO_TLSEXT
127 p=s->s3->server_random;
128 + /* Generate server_random if it was not needed previously */
129 Time=(unsigned long)time(NULL); /* Time */
131 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
134 /* Do the message type and length last */
137 diff -upr openssl-0.9.8i.orig/ssl/ssl_err.c openssl-0.9.8i/ssl/ssl_err.c
138 --- openssl-0.9.8i.orig/ssl/ssl_err.c 2008-08-13 22:44:44.000000000 +0300
139 +++ openssl-0.9.8i/ssl/ssl_err.c 2008-09-28 16:50:18.000000000 +0300
140 @@ -253,6 +253,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
141 {ERR_FUNC(SSL_F_TLS1_ENC), "TLS1_ENC"},
142 {ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK), "TLS1_SETUP_KEY_BLOCK"},
143 {ERR_FUNC(SSL_F_WRITE_PENDING), "WRITE_PENDING"},
144 +{ERR_FUNC(SSL_F_SSL_SET_HELLO_EXTENSION), "SSL_set_hello_extension"},
148 diff -upr openssl-0.9.8i.orig/ssl/ssl.h openssl-0.9.8i/ssl/ssl.h
149 --- openssl-0.9.8i.orig/ssl/ssl.h 2008-08-13 22:44:44.000000000 +0300
150 +++ openssl-0.9.8i/ssl/ssl.h 2008-09-28 16:50:18.000000000 +0300
151 @@ -344,6 +344,7 @@ extern "C" {
152 * 'struct ssl_st *' function parameters used to prototype callbacks
154 typedef struct ssl_st *ssl_crock_st;
155 +typedef struct tls_extension_st TLS_EXTENSION;
157 /* used to hold info on the particular ciphers used */
158 typedef struct ssl_cipher_st
159 @@ -362,6 +363,8 @@ typedef struct ssl_cipher_st
161 DECLARE_STACK_OF(SSL_CIPHER)
163 +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);
165 /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
166 typedef struct ssl_method_st
168 @@ -1034,6 +1037,14 @@ struct ssl_st
170 /* RFC4507 session ticket expected to be received or sent */
171 int tlsext_ticket_expected;
173 + /* TLS extensions */
174 + TLS_EXTENSION *tls_extension;
176 + /* TLS pre-shared secret session resumption */
177 + tls_session_secret_cb_fn tls_session_secret_cb;
178 + void *tls_session_secret_cb_arg;
180 SSL_CTX * initial_ctx; /* initial ctx, used to store sessions */
181 #define session_ctx initial_ctx
183 @@ -1632,6 +1643,12 @@ void *SSL_COMP_get_compression_methods(v
184 int SSL_COMP_add_compression_method(int id,void *cm);
187 +/* TLS extensions functions */
188 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len);
190 +/* Pre-shared secret session resumption functions */
191 +int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
193 /* BEGIN ERROR CODES */
194 /* The following lines are auto generated by the script mkerr.pl. Any changes
195 * made after this point may be overwritten when the script is next run.
196 @@ -1824,6 +1841,7 @@ void ERR_load_SSL_strings(void);
197 #define SSL_F_TLS1_ENC 210
198 #define SSL_F_TLS1_SETUP_KEY_BLOCK 211
199 #define SSL_F_WRITE_PENDING 212
200 +#define SSL_F_SSL_SET_HELLO_EXTENSION 213
203 #define SSL_R_APP_DATA_IN_HANDSHAKE 100
204 diff -upr openssl-0.9.8i.orig/ssl/ssl_sess.c openssl-0.9.8i/ssl/ssl_sess.c
205 --- openssl-0.9.8i.orig/ssl/ssl_sess.c 2008-06-04 21:35:27.000000000 +0300
206 +++ openssl-0.9.8i/ssl/ssl_sess.c 2008-09-28 16:50:18.000000000 +0300
207 @@ -707,6 +707,52 @@ long SSL_CTX_get_timeout(const SSL_CTX *
208 return(s->session_timeout);
211 +#ifndef OPENSSL_NO_TLSEXT
212 +int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
213 + STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
215 + if (s == NULL) return(0);
216 + s->tls_session_secret_cb = tls_session_secret_cb;
217 + s->tls_session_secret_cb_arg = arg;
221 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len)
223 + if(s->version >= TLS1_VERSION)
225 + if(s->tls_extension)
227 + OPENSSL_free(s->tls_extension);
228 + s->tls_extension = NULL;
231 + s->tls_extension = OPENSSL_malloc(sizeof(TLS_EXTENSION) + ext_len);
232 + if(!s->tls_extension)
234 + SSLerr(SSL_F_SSL_SET_HELLO_EXTENSION, ERR_R_MALLOC_FAILURE);
238 + s->tls_extension->type = ext_type;
242 + s->tls_extension->length = ext_len;
243 + s->tls_extension->data = s->tls_extension + 1;
244 + memcpy(s->tls_extension->data, ext_data, ext_len);
246 + s->tls_extension->length = 0;
247 + s->tls_extension->data = NULL;
255 +#endif /* OPENSSL_NO_TLSEXT */
257 typedef struct timeout_param_st
260 diff -upr openssl-0.9.8i.orig/ssl/t1_lib.c openssl-0.9.8i/ssl/t1_lib.c
261 --- openssl-0.9.8i.orig/ssl/t1_lib.c 2008-09-04 01:13:04.000000000 +0300
262 +++ openssl-0.9.8i/ssl/t1_lib.c 2008-09-28 16:50:18.000000000 +0300
263 @@ -106,6 +106,12 @@ int tls1_new(SSL *s)
265 void tls1_free(SSL *s)
267 +#ifndef OPENSSL_NO_TLSEXT
268 + if(s->tls_extension)
270 + OPENSSL_free(s->tls_extension);
276 @@ -175,8 +181,24 @@ unsigned char *ssl_add_clienthello_tlsex
278 if (s->session && s->session->tlsext_tick)
279 ticklen = s->session->tlsext_ticklen;
280 + else if (s->session && s->tls_extension &&
281 + s->tls_extension->type == TLSEXT_TYPE_session_ticket &&
282 + s->tls_extension->data)
284 + ticklen = s->tls_extension->length;
285 + s->session->tlsext_tick = OPENSSL_malloc(ticklen);
286 + if (!s->session->tlsext_tick)
288 + memcpy(s->session->tlsext_tick, s->tls_extension->data,
290 + s->session->tlsext_ticklen = ticklen;
294 + if (ticklen == 0 && s->tls_extension &&
295 + s->tls_extension->type == TLSEXT_TYPE_session_ticket &&
296 + s->tls_extension->data == NULL)
298 /* Check for enough room 2 for extension type, 2 for len
301 @@ -190,6 +212,7 @@ unsigned char *ssl_add_clienthello_tlsex
307 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
309 @@ -776,6 +799,8 @@ int tls1_process_ticket(SSL *s, unsigned
310 s->tlsext_ticket_expected = 1;
311 return 0; /* Cache miss */
313 + if (s->tls_session_secret_cb)
315 return tls_decrypt_ticket(s, p, size, session_id, len,
318 diff -upr openssl-0.9.8i.orig/ssl/tls1.h openssl-0.9.8i/ssl/tls1.h
319 --- openssl-0.9.8i.orig/ssl/tls1.h 2008-04-30 19:11:33.000000000 +0300
320 +++ openssl-0.9.8i/ssl/tls1.h 2008-09-28 16:50:18.000000000 +0300
321 @@ -398,6 +398,14 @@ SSL_CTX_callback_ctrl(ssl,SSL_CTRL_SET_T
322 #define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74" /*master secret*/
325 +/* TLS extension struct */
326 +struct tls_extension_st
328 + unsigned short type;
329 + unsigned short length;
336 diff -upr openssl-0.9.8i.orig/util/ssleay.num openssl-0.9.8i/util/ssleay.num
337 --- openssl-0.9.8i.orig/util/ssleay.num 2008-06-05 13:57:21.000000000 +0300
338 +++ openssl-0.9.8i/util/ssleay.num 2008-09-28 16:50:57.000000000 +0300
339 @@ -242,3 +242,5 @@ SSL_set_SSL_CTX
340 SSL_get_servername 291 EXIST::FUNCTION:TLSEXT
341 SSL_get_servername_type 292 EXIST::FUNCTION:TLSEXT
342 SSL_CTX_set_client_cert_engine 293 EXIST::FUNCTION:ENGINE
343 +SSL_set_hello_extension 305 EXIST::FUNCTION:TLSEXT
344 +SSL_set_session_secret_cb 306 EXIST::FUNCTION:TLSEXT