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-20081111/ssl/s3_clnt.c
10 ===================================================================
11 --- openssl-SNAP-20081111.orig/ssl/s3_clnt.c
12 +++ openssl-SNAP-20081111/ssl/s3_clnt.c
13 @@ -788,6 +788,23 @@ 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,
24 + &s->session->master_key_length,
26 + 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 @@ -2927,11 +2944,8 @@ static 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 Index: openssl-SNAP-20081111/ssl/s3_srvr.c
52 ===================================================================
53 --- openssl-SNAP-20081111.orig/ssl/s3_srvr.c
54 +++ openssl-SNAP-20081111/ssl/s3_srvr.c
55 @@ -1010,6 +1010,59 @@ int ssl3_get_client_hello(SSL *s)
56 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
60 + /* Check if we want to use external pre-shared secret for this
61 + * handshake for not reused session only. We need to generate
62 + * server_random before calling tls_session_secret_cb in order to allow
63 + * SessionTicket processing to use it in key derivation. */
67 + Time=(unsigned long)time(NULL); /* Time */
68 + pos=s->s3->server_random;
70 + if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0)
72 + al=SSL_AD_INTERNAL_ERROR;
77 + if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
79 + SSL_CIPHER *pref_cipher=NULL;
81 + s->session->master_key_length=sizeof(s->session->master_key);
82 + if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
83 + ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
86 + s->session->ciphers=ciphers;
87 + s->session->verify_result=X509_V_OK;
91 + /* check if some cipher was preferred by call back */
92 + pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
93 + if (pref_cipher == NULL)
95 + al=SSL_AD_HANDSHAKE_FAILURE;
96 + SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
100 + s->session->cipher=pref_cipher;
102 + if (s->cipher_list)
103 + sk_SSL_CIPHER_free(s->cipher_list);
105 + if (s->cipher_list_by_id)
106 + sk_SSL_CIPHER_free(s->cipher_list_by_id);
108 + s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
109 + 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 @@ -1134,16 +1187,22 @@ int ssl3_send_server_hello(SSL *s)
119 - unsigned long l,Time;
121 +#ifdef OPENSSL_NO_TLSEXT
122 + unsigned long Time;
125 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
127 buf=(unsigned char *)s->init_buf->data;
128 +#ifdef OPENSSL_NO_TLSEXT
129 p=s->s3->server_random;
130 + /* Generate server_random if it was not needed previously */
131 Time=(unsigned long)time(NULL); /* Time */
133 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
136 /* Do the message type and length last */
139 Index: openssl-SNAP-20081111/ssl/ssl_err.c
140 ===================================================================
141 --- openssl-SNAP-20081111.orig/ssl/ssl_err.c
142 +++ openssl-SNAP-20081111/ssl/ssl_err.c
143 @@ -263,6 +263,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
144 {ERR_FUNC(SSL_F_TLS1_PRF), "tls1_prf"},
145 {ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK), "TLS1_SETUP_KEY_BLOCK"},
146 {ERR_FUNC(SSL_F_WRITE_PENDING), "WRITE_PENDING"},
147 +{ERR_FUNC(SSL_F_SSL_SET_HELLO_EXTENSION), "SSL_set_hello_extension"},
151 Index: openssl-SNAP-20081111/ssl/ssl.h
152 ===================================================================
153 --- openssl-SNAP-20081111.orig/ssl/ssl.h
154 +++ openssl-SNAP-20081111/ssl/ssl.h
155 @@ -355,6 +355,7 @@ extern "C" {
156 * 'struct ssl_st *' function parameters used to prototype callbacks
158 typedef struct ssl_st *ssl_crock_st;
159 +typedef struct tls_extension_st TLS_EXTENSION;
161 /* used to hold info on the particular ciphers used */
162 typedef struct ssl_cipher_st
163 @@ -378,6 +379,8 @@ typedef struct ssl_cipher_st
165 DECLARE_STACK_OF(SSL_CIPHER)
167 +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);
169 /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
170 typedef struct ssl_method_st
172 @@ -1145,6 +1148,13 @@ struct ssl_st
173 void *tlsext_opaque_prf_input;
174 size_t tlsext_opaque_prf_input_len;
176 + /* TLS extensions */
177 + TLS_EXTENSION *tls_extension;
179 + /* TLS pre-shared secret session resumption */
180 + tls_session_secret_cb_fn tls_session_secret_cb;
181 + void *tls_session_secret_cb_arg;
183 SSL_CTX * initial_ctx; /* initial ctx, used to store sessions */
184 #define session_ctx initial_ctx
186 @@ -1746,6 +1756,12 @@ void *SSL_COMP_get_compression_methods(v
187 int SSL_COMP_add_compression_method(int id,void *cm);
190 +/* TLS extensions functions */
191 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len);
193 +/* Pre-shared secret session resumption functions */
194 +int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
196 /* BEGIN ERROR CODES */
197 /* The following lines are auto generated by the script mkerr.pl. Any changes
198 * made after this point may be overwritten when the script is next run.
199 @@ -1948,6 +1964,7 @@ void ERR_load_SSL_strings(void);
200 #define SSL_F_TLS1_PRF 284
201 #define SSL_F_TLS1_SETUP_KEY_BLOCK 211
202 #define SSL_F_WRITE_PENDING 212
203 +#define SSL_F_SSL_SET_HELLO_EXTENSION 213
206 #define SSL_R_APP_DATA_IN_HANDSHAKE 100
207 Index: openssl-SNAP-20081111/ssl/ssl_sess.c
208 ===================================================================
209 --- openssl-SNAP-20081111.orig/ssl/ssl_sess.c
210 +++ openssl-SNAP-20081111/ssl/ssl_sess.c
211 @@ -834,6 +834,54 @@ long SSL_CTX_get_timeout(const SSL_CTX *
212 return(s->session_timeout);
215 +#ifndef OPENSSL_NO_TLSEXT
216 +int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
217 + STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
219 + if (s == NULL) return(0);
220 + s->tls_session_secret_cb = tls_session_secret_cb;
221 + s->tls_session_secret_cb_arg = arg;
225 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len)
227 + if (s->version >= TLS1_VERSION)
229 + if (s->tls_extension)
231 + OPENSSL_free(s->tls_extension);
232 + s->tls_extension = NULL;
235 + s->tls_extension = OPENSSL_malloc(sizeof(TLS_EXTENSION) + ext_len);
236 + if (!s->tls_extension)
238 + SSLerr(SSL_F_SSL_SET_HELLO_EXTENSION, ERR_R_MALLOC_FAILURE);
242 + s->tls_extension->type = ext_type;
246 + s->tls_extension->length = ext_len;
247 + s->tls_extension->data = s->tls_extension + 1;
248 + memcpy(s->tls_extension->data, ext_data, ext_len);
252 + s->tls_extension->length = 0;
253 + s->tls_extension->data = NULL;
261 +#endif /* OPENSSL_NO_TLSEXT */
263 typedef struct timeout_param_st
266 Index: openssl-SNAP-20081111/ssl/t1_lib.c
267 ===================================================================
268 --- openssl-SNAP-20081111.orig/ssl/t1_lib.c
269 +++ openssl-SNAP-20081111/ssl/t1_lib.c
270 @@ -154,6 +154,12 @@ int tls1_new(SSL *s)
272 void tls1_free(SSL *s)
274 +#ifndef OPENSSL_NO_TLSEXT
275 + if (s->tls_extension)
277 + OPENSSL_free(s->tls_extension);
279 +#endif /* OPENSSL_NO_TLSEXT */
283 @@ -357,8 +363,24 @@ unsigned char *ssl_add_clienthello_tlsex
285 if (s->session && s->session->tlsext_tick)
286 ticklen = s->session->tlsext_ticklen;
287 + else if (s->session && s->tls_extension &&
288 + s->tls_extension->type == TLSEXT_TYPE_session_ticket &&
289 + s->tls_extension->data)
291 + ticklen = s->tls_extension->length;
292 + s->session->tlsext_tick = OPENSSL_malloc(ticklen);
293 + if (!s->session->tlsext_tick)
295 + memcpy(s->session->tlsext_tick, s->tls_extension->data,
297 + s->session->tlsext_ticklen = ticklen;
301 + if (ticklen == 0 && s->tls_extension &&
302 + s->tls_extension->type == TLSEXT_TYPE_session_ticket &&
303 + s->tls_extension->data == NULL)
305 /* Check for enough room 2 for extension type, 2 for len
308 @@ -371,6 +393,7 @@ unsigned char *ssl_add_clienthello_tlsex
314 #ifdef TLSEXT_TYPE_opaque_prf_input
315 if (s->s3->client_opaque_prf_input != NULL)
316 @@ -1435,6 +1458,15 @@ int tls1_process_ticket(SSL *s, unsigned
317 s->tlsext_ticket_expected = 1;
318 return 0; /* Cache miss */
320 + if (s->tls_session_secret_cb)
322 + /* Indicate cache miss here and instead of
323 + * generating the session from ticket now,
324 + * trigger abbreviated handshake based on
325 + * external mechanism to calculate the master
329 return tls_decrypt_ticket(s, p, size, session_id, len,
332 Index: openssl-SNAP-20081111/ssl/tls1.h
333 ===================================================================
334 --- openssl-SNAP-20081111.orig/ssl/tls1.h
335 +++ openssl-SNAP-20081111/ssl/tls1.h
336 @@ -512,6 +512,14 @@ SSL_CTX_callback_ctrl(ssl,SSL_CTRL_SET_T
337 #define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74" /*master secret*/
340 +/* TLS extension struct */
341 +struct tls_extension_st
343 + unsigned short type;
344 + unsigned short length;
351 Index: openssl-SNAP-20081111/util/ssleay.num
352 ===================================================================
353 --- openssl-SNAP-20081111.orig/util/ssleay.num
354 +++ openssl-SNAP-20081111/util/ssleay.num
355 @@ -254,3 +254,5 @@ PEM_read_bio_SSL_SESSION
356 SSL_CTX_set_psk_server_callback 303 EXIST::FUNCTION:PSK
357 SSL_get_psk_identity 304 EXIST::FUNCTION:PSK
358 PEM_write_SSL_SESSION 305 EXIST:!WIN16:FUNCTION:
359 +SSL_set_hello_extension 306 EXIST::FUNCTION:TLSEXT
360 +SSL_set_session_secret_cb 307 EXIST::FUNCTION:TLSEXT