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.8h 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.8h.orig/ssl/s3_clnt.c openssl-0.9.8h/ssl/s3_clnt.c
13 --- openssl-0.9.8h.orig/ssl/s3_clnt.c 2008-05-28 10:29:27.000000000 +0300
14 +++ openssl-0.9.8h/ssl/s3_clnt.c 2008-05-29 10:44:25.000000000 +0300
15 @@ -752,6 +752,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 @@ -2693,11 +2707,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.8h.orig/ssl/s3_srvr.c openssl-0.9.8h/ssl/s3_srvr.c
51 --- openssl-0.9.8h.orig/ssl/s3_srvr.c 2008-04-30 19:11:32.000000000 +0300
52 +++ openssl-0.9.8h/ssl/s3_srvr.c 2008-05-28 18:49:34.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.8h.orig/ssl/ssl.h openssl-0.9.8h/ssl/ssl.h
138 --- openssl-0.9.8h.orig/ssl/ssl.h 2008-04-30 19:11:32.000000000 +0300
139 +++ openssl-0.9.8h/ssl/ssl.h 2008-05-28 18:49:34.000000000 +0300
140 @@ -343,6 +343,7 @@ extern "C" {
141 * 'struct ssl_st *' function parameters used to prototype callbacks
143 typedef struct ssl_st *ssl_crock_st;
144 +typedef struct tls_extension_st TLS_EXTENSION;
146 /* used to hold info on the particular ciphers used */
147 typedef struct ssl_cipher_st
148 @@ -364,6 +365,8 @@ DECLARE_STACK_OF(SSL_CIPHER)
149 typedef struct ssl_st SSL;
150 typedef struct ssl_ctx_st SSL_CTX;
152 +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);
154 /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
155 typedef struct ssl_method_st
157 @@ -1027,6 +1030,14 @@ struct ssl_st
159 /* RFC4507 session ticket expected to be received or sent */
160 int tlsext_ticket_expected;
162 + /* TLS extensions */
163 + TLS_EXTENSION *tls_extension;
165 + /* TLS pre-shared secret session resumption */
166 + tls_session_secret_cb_fn tls_session_secret_cb;
167 + void *tls_session_secret_cb_arg;
169 SSL_CTX * initial_ctx; /* initial ctx, used to store sessions */
170 #define session_ctx initial_ctx
172 @@ -1625,6 +1636,12 @@ void *SSL_COMP_get_compression_methods(v
173 int SSL_COMP_add_compression_method(int id,void *cm);
176 +/* TLS extensions functions */
177 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len);
179 +/* Pre-shared secret session resumption functions */
180 +int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
182 /* BEGIN ERROR CODES */
183 /* The following lines are auto generated by the script mkerr.pl. Any changes
184 * made after this point may be overwritten when the script is next run.
185 @@ -1815,6 +1832,7 @@ void ERR_load_SSL_strings(void);
186 #define SSL_F_TLS1_ENC 210
187 #define SSL_F_TLS1_SETUP_KEY_BLOCK 211
188 #define SSL_F_WRITE_PENDING 212
189 +#define SSL_F_SSL_SET_HELLO_EXTENSION 213
192 #define SSL_R_APP_DATA_IN_HANDSHAKE 100
193 diff -upr openssl-0.9.8h.orig/ssl/ssl_err.c openssl-0.9.8h/ssl/ssl_err.c
194 --- openssl-0.9.8h.orig/ssl/ssl_err.c 2007-10-12 03:00:30.000000000 +0300
195 +++ openssl-0.9.8h/ssl/ssl_err.c 2008-05-28 18:49:34.000000000 +0300
196 @@ -251,6 +251,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
197 {ERR_FUNC(SSL_F_TLS1_ENC), "TLS1_ENC"},
198 {ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK), "TLS1_SETUP_KEY_BLOCK"},
199 {ERR_FUNC(SSL_F_WRITE_PENDING), "WRITE_PENDING"},
200 +{ERR_FUNC(SSL_F_SSL_SET_HELLO_EXTENSION), "SSL_set_hello_extension"},
204 diff -upr openssl-0.9.8h.orig/ssl/ssl_sess.c openssl-0.9.8h/ssl/ssl_sess.c
205 --- openssl-0.9.8h.orig/ssl/ssl_sess.c 2007-10-17 20:30:15.000000000 +0300
206 +++ openssl-0.9.8h/ssl/ssl_sess.c 2008-05-28 18:49:34.000000000 +0300
207 @@ -704,6 +704,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.8h.orig/ssl/t1_lib.c openssl-0.9.8h/ssl/t1_lib.c
261 --- openssl-0.9.8h.orig/ssl/t1_lib.c 2008-05-28 10:26:33.000000000 +0300
262 +++ openssl-0.9.8h/ssl/t1_lib.c 2008-05-28 18:49:34.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 @@ -774,6 +797,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.8h.orig/ssl/tls1.h openssl-0.9.8h/ssl/tls1.h
319 --- openssl-0.9.8h.orig/ssl/tls1.h 2008-04-30 19:11:33.000000000 +0300
320 +++ openssl-0.9.8h/ssl/tls1.h 2008-05-28 18:49:34.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.8h.orig/util/ssleay.num openssl-0.9.8h/util/ssleay.num
337 --- openssl-0.9.8h.orig/util/ssleay.num 2007-08-13 01:31:16.000000000 +0300
338 +++ openssl-0.9.8h/util/ssleay.num 2008-05-28 18:49:34.000000000 +0300
339 @@ -241,3 +241,5 @@ SSL_CTX_sess_get_remove_cb
340 SSL_set_SSL_CTX 290 EXIST::FUNCTION:
341 SSL_get_servername 291 EXIST::FUNCTION:TLSEXT
342 SSL_get_servername_type 292 EXIST::FUNCTION:TLSEXT
343 +SSL_set_hello_extension 305 EXIST::FUNCTION:TLSEXT
344 +SSL_set_session_secret_cb 306 EXIST::FUNCTION:TLSEXT