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).
9 Index: openssl-SNAP-20080928/ssl/s3_clnt.c
10 ===================================================================
11 --- openssl-SNAP-20080928.orig/ssl/s3_clnt.c
12 +++ openssl-SNAP-20080928/ssl/s3_clnt.c
13 @@ -788,6 +788,20 @@ int ssl3_get_server_hello(SSL *s)
17 +#ifndef OPENSSL_NO_TLSEXT
18 + /* check if we want to resume the session based on external pre-shared secret */
19 + if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
21 + SSL_CIPHER *pref_cipher=NULL;
22 + s->session->master_key_length=sizeof(s->session->master_key);
23 + if (s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
24 + NULL, &pref_cipher, s->tls_session_secret_cb_arg))
26 + s->session->cipher=pref_cipher ? pref_cipher : ssl_get_cipher_by_char(s,p+j);
29 +#endif /* OPENSSL_NO_TLSEXT */
31 if (j != 0 && j == s->session->session_id_length
32 && memcmp(p,s->session->session_id,j) == 0)
34 @@ -2927,11 +2941,8 @@ static int ssl3_check_finished(SSL *s)
38 - /* If we have no ticket or session ID is non-zero length (a match of
39 - * a non-zero session length would never reach here) it cannot be a
42 - if (!s->session->tlsext_tick || s->session->session_id_length)
43 + /* If we have no ticket it cannot be a resumed session. */
44 + if (!s->session->tlsext_tick)
46 /* this function is called when we really expect a Certificate
47 * message, so permit appropriate message length */
48 Index: openssl-SNAP-20080928/ssl/s3_srvr.c
49 ===================================================================
50 --- openssl-SNAP-20080928.orig/ssl/s3_srvr.c
51 +++ openssl-SNAP-20080928/ssl/s3_srvr.c
52 @@ -1010,6 +1010,59 @@ int ssl3_get_client_hello(SSL *s)
53 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
57 + /* Check if we want to use external pre-shared secret for this
58 + * handshake for not reused session only. We need to generate
59 + * server_random before calling tls_session_secret_cb in order to allow
60 + * SessionTicket processing to use it in key derivation. */
64 + Time=(unsigned long)time(NULL); /* Time */
65 + pos=s->s3->server_random;
67 + if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0)
69 + al=SSL_AD_INTERNAL_ERROR;
74 + if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
76 + SSL_CIPHER *pref_cipher=NULL;
78 + s->session->master_key_length=sizeof(s->session->master_key);
79 + if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
80 + ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
83 + s->session->ciphers=ciphers;
84 + s->session->verify_result=X509_V_OK;
88 + /* check if some cipher was preferred by call back */
89 + pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
90 + if (pref_cipher == NULL)
92 + al=SSL_AD_HANDSHAKE_FAILURE;
93 + SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
97 + s->session->cipher=pref_cipher;
100 + sk_SSL_CIPHER_free(s->cipher_list);
102 + if (s->cipher_list_by_id)
103 + sk_SSL_CIPHER_free(s->cipher_list_by_id);
105 + s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
106 + 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 @@ -1134,16 +1187,22 @@ int ssl3_send_server_hello(SSL *s)
116 - unsigned long l,Time;
118 +#ifdef OPENSSL_NO_TLSEXT
119 + unsigned long Time;
122 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
124 buf=(unsigned char *)s->init_buf->data;
125 +#ifdef OPENSSL_NO_TLSEXT
126 p=s->s3->server_random;
127 + /* Generate server_random if it was not needed previously */
128 Time=(unsigned long)time(NULL); /* Time */
130 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
133 /* Do the message type and length last */
136 Index: openssl-SNAP-20080928/ssl/ssl_err.c
137 ===================================================================
138 --- openssl-SNAP-20080928.orig/ssl/ssl_err.c
139 +++ openssl-SNAP-20080928/ssl/ssl_err.c
140 @@ -263,6 +263,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
141 {ERR_FUNC(SSL_F_TLS1_PRF), "tls1_prf"},
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 Index: openssl-SNAP-20080928/ssl/ssl.h
149 ===================================================================
150 --- openssl-SNAP-20080928.orig/ssl/ssl.h
151 +++ openssl-SNAP-20080928/ssl/ssl.h
152 @@ -355,6 +355,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_extension_st TLS_EXTENSION;
158 /* used to hold info on the particular ciphers used */
159 typedef struct ssl_cipher_st
160 @@ -378,6 +379,8 @@ typedef struct ssl_cipher_st
162 DECLARE_STACK_OF(SSL_CIPHER)
164 +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);
166 /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
167 typedef struct ssl_method_st
169 @@ -1145,6 +1148,13 @@ struct ssl_st
170 void *tlsext_opaque_prf_input;
171 size_t tlsext_opaque_prf_input_len;
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 @@ -1746,6 +1756,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 @@ -1948,6 +1964,7 @@ void ERR_load_SSL_strings(void);
197 #define SSL_F_TLS1_PRF 284
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 Index: openssl-SNAP-20080928/ssl/ssl_sess.c
205 ===================================================================
206 --- openssl-SNAP-20080928.orig/ssl/ssl_sess.c
207 +++ openssl-SNAP-20080928/ssl/ssl_sess.c
208 @@ -834,6 +834,52 @@ long SSL_CTX_get_timeout(const SSL_CTX *
209 return(s->session_timeout);
212 +#ifndef OPENSSL_NO_TLSEXT
213 +int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
214 + STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
216 + if (s == NULL) return(0);
217 + s->tls_session_secret_cb = tls_session_secret_cb;
218 + s->tls_session_secret_cb_arg = arg;
222 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len)
224 + if(s->version >= TLS1_VERSION)
226 + if(s->tls_extension)
228 + OPENSSL_free(s->tls_extension);
229 + s->tls_extension = NULL;
232 + s->tls_extension = OPENSSL_malloc(sizeof(TLS_EXTENSION) + ext_len);
233 + if(!s->tls_extension)
235 + SSLerr(SSL_F_SSL_SET_HELLO_EXTENSION, ERR_R_MALLOC_FAILURE);
239 + s->tls_extension->type = ext_type;
243 + s->tls_extension->length = ext_len;
244 + s->tls_extension->data = s->tls_extension + 1;
245 + memcpy(s->tls_extension->data, ext_data, ext_len);
247 + s->tls_extension->length = 0;
248 + s->tls_extension->data = NULL;
256 +#endif /* OPENSSL_NO_TLSEXT */
258 typedef struct timeout_param_st
261 Index: openssl-SNAP-20080928/ssl/t1_lib.c
262 ===================================================================
263 --- openssl-SNAP-20080928.orig/ssl/t1_lib.c
264 +++ openssl-SNAP-20080928/ssl/t1_lib.c
265 @@ -154,6 +154,12 @@ int tls1_new(SSL *s)
267 void tls1_free(SSL *s)
269 +#ifndef OPENSSL_NO_TLSEXT
270 + if(s->tls_extension)
272 + OPENSSL_free(s->tls_extension);
278 @@ -357,8 +363,24 @@ unsigned char *ssl_add_clienthello_tlsex
280 if (s->session && s->session->tlsext_tick)
281 ticklen = s->session->tlsext_ticklen;
282 + else if (s->session && s->tls_extension &&
283 + s->tls_extension->type == TLSEXT_TYPE_session_ticket &&
284 + s->tls_extension->data)
286 + ticklen = s->tls_extension->length;
287 + s->session->tlsext_tick = OPENSSL_malloc(ticklen);
288 + if (!s->session->tlsext_tick)
290 + memcpy(s->session->tlsext_tick, s->tls_extension->data,
292 + s->session->tlsext_ticklen = ticklen;
296 + if (ticklen == 0 && s->tls_extension &&
297 + s->tls_extension->type == TLSEXT_TYPE_session_ticket &&
298 + s->tls_extension->data == NULL)
300 /* Check for enough room 2 for extension type, 2 for len
303 @@ -371,6 +393,7 @@ unsigned char *ssl_add_clienthello_tlsex
309 #ifdef TLSEXT_TYPE_opaque_prf_input
310 if (s->s3->client_opaque_prf_input != NULL)
311 @@ -1435,6 +1458,15 @@ int tls1_process_ticket(SSL *s, unsigned
312 s->tlsext_ticket_expected = 1;
313 return 0; /* Cache miss */
315 + if (s->tls_session_secret_cb)
317 + /* Indicate cache miss here and instead of
318 + * generating the session from ticket now,
319 + * trigger abbreviated handshake based on
320 + * external mechanism to calculate the master
324 return tls_decrypt_ticket(s, p, size, session_id, len,
327 Index: openssl-SNAP-20080928/ssl/tls1.h
328 ===================================================================
329 --- openssl-SNAP-20080928.orig/ssl/tls1.h
330 +++ openssl-SNAP-20080928/ssl/tls1.h
331 @@ -512,6 +512,14 @@ SSL_CTX_callback_ctrl(ssl,SSL_CTRL_SET_T
332 #define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74" /*master secret*/
335 +/* TLS extension struct */
336 +struct tls_extension_st
338 + unsigned short type;
339 + unsigned short length;
346 Index: openssl-SNAP-20080928/util/ssleay.num
347 ===================================================================
348 --- openssl-SNAP-20080928.orig/util/ssleay.num
349 +++ openssl-SNAP-20080928/util/ssleay.num
350 @@ -254,3 +254,5 @@ PEM_read_bio_SSL_SESSION
351 SSL_CTX_set_psk_server_callback 303 EXIST::FUNCTION:PSK
352 SSL_get_psk_identity 304 EXIST::FUNCTION:PSK
353 PEM_write_SSL_SESSION 305 EXIST:!WIN16:FUNCTION:
354 +SSL_set_hello_extension 306 EXIST::FUNCTION:TLSEXT
355 +SSL_set_session_secret_cb 307 EXIST::FUNCTION:TLSEXT