ab4fe7b216bf00ed28fd57e790a43d709229ad2f
[libeap.git] / patches / openssl-0.9.9-session-ticket.patch
1 This patch adds support for TLS SessionTicket extension (RFC 5077) for
2 the parts used by EAP-FAST (RFC 4851).
3
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).
6
7
8
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)
14                 goto f_err;
15                 }
16  
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)
20 +               {
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,
25 +                                            NULL, &pref_cipher,
26 +                                            s->tls_session_secret_cb_arg))
27 +                       {
28 +                       s->session->cipher = pref_cipher ?
29 +                               pref_cipher : ssl_get_cipher_by_char(s, p+j);
30 +                       }
31 +               }
32 +#endif /* OPENSSL_NO_TLSEXT */
33 +
34         if (j != 0 && j == s->session->session_id_length
35             && memcmp(p,s->session->session_id,j) == 0)
36             {
37 @@ -2927,11 +2944,8 @@ static int ssl3_check_finished(SSL *s)
38         {
39         int ok;
40         long n;
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
43 -        * resumed session.
44 -        */
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)
48                 return 1;
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);
57                         goto err;
58                 }
59 +
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. */
64 +       {
65 +               unsigned long Time;
66 +               unsigned char *pos;
67 +               Time=(unsigned long)time(NULL);                 /* Time */
68 +               pos=s->s3->server_random;
69 +               l2n(Time,pos);
70 +               if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0)
71 +                       {
72 +                       al=SSL_AD_INTERNAL_ERROR;
73 +                       goto f_err;
74 +                       }
75 +       }
76 +
77 +       if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
78 +               {
79 +               SSL_CIPHER *pref_cipher=NULL;
80 +
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))
84 +                       {
85 +                       s->hit=1;
86 +                       s->session->ciphers=ciphers;
87 +                       s->session->verify_result=X509_V_OK;
88 +
89 +                       ciphers=NULL;
90 +
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)
94 +                               {
95 +                               al=SSL_AD_HANDSHAKE_FAILURE;
96 +                               SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
97 +                               goto f_err;
98 +                               }
99 +
100 +                       s->session->cipher=pref_cipher;
101 +
102 +                       if (s->cipher_list)
103 +                               sk_SSL_CIPHER_free(s->cipher_list);
104 +
105 +                       if (s->cipher_list_by_id)
106 +                               sk_SSL_CIPHER_free(s->cipher_list_by_id);
107 +
108 +                       s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
109 +                       s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
110 +                       }
111 +               }
112  #endif
113  
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)
116         unsigned char *buf;
117         unsigned char *p,*d;
118         int i,sl;
119 -       unsigned long l,Time;
120 +       unsigned long l;
121 +#ifdef OPENSSL_NO_TLSEXT
122 +       unsigned long Time;
123 +#endif
124  
125         if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
126                 {
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 */
132                 l2n(Time,p);
133                 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
134                         return -1;
135 +#endif
136                 /* Do the message type and length last */
137                 d=p= &(buf[4]);
138  
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"},
148  {0,NULL}
149         };
150  
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
157   * in SSL_CTX. */
158  typedef struct ssl_st *ssl_crock_st;
159 +typedef struct tls_extension_st TLS_EXTENSION;
160  
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
164  
165  DECLARE_STACK_OF(SSL_CIPHER)
166  
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);
168 +
169  /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
170  typedef struct ssl_method_st
171         {
172 @@ -1145,6 +1148,13 @@ struct ssl_st
173         void *tlsext_opaque_prf_input;
174         size_t tlsext_opaque_prf_input_len;
175  
176 +       /* TLS extensions */
177 +       TLS_EXTENSION *tls_extension;
178 +
179 +       /* TLS pre-shared secret session resumption */
180 +       tls_session_secret_cb_fn tls_session_secret_cb;
181 +       void *tls_session_secret_cb_arg;
182 +
183         SSL_CTX * initial_ctx; /* initial ctx, used to store sessions */
184  #define session_ctx initial_ctx
185  #else
186 @@ -1746,6 +1756,12 @@ void *SSL_COMP_get_compression_methods(v
187  int SSL_COMP_add_compression_method(int id,void *cm);
188  #endif
189  
190 +/* TLS extensions functions */
191 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len);
192 +
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);
195 +
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
204  
205  /* Reason codes. */
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);
213         }
214  
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)
218 +       {
219 +       if (s == NULL) return(0);
220 +       s->tls_session_secret_cb = tls_session_secret_cb;
221 +       s->tls_session_secret_cb_arg = arg;
222 +       return(1);
223 +       }
224 +
225 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len)
226 +       {
227 +       if (s->version >= TLS1_VERSION)
228 +               {
229 +               if (s->tls_extension)
230 +                       {
231 +                       OPENSSL_free(s->tls_extension);
232 +                       s->tls_extension = NULL;
233 +                       }
234 +
235 +               s->tls_extension = OPENSSL_malloc(sizeof(TLS_EXTENSION) + ext_len);
236 +               if (!s->tls_extension)
237 +                       {
238 +                       SSLerr(SSL_F_SSL_SET_HELLO_EXTENSION, ERR_R_MALLOC_FAILURE);
239 +                       return 0;
240 +                       }
241 +
242 +               s->tls_extension->type = ext_type;
243 +
244 +               if (ext_data)
245 +                       {
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);
249 +                       }
250 +               else
251 +                       {
252 +                       s->tls_extension->length = 0;
253 +                       s->tls_extension->data = NULL;
254 +                       }
255 +
256 +               return 1;
257 +               }
258 +
259 +       return 0;
260 +       }
261 +#endif /* OPENSSL_NO_TLSEXT */
262 +
263  typedef struct timeout_param_st
264         {
265         SSL_CTX *ctx;
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)
271  
272  void tls1_free(SSL *s)
273         {
274 +#ifndef OPENSSL_NO_TLSEXT
275 +       if (s->tls_extension)
276 +               {
277 +               OPENSSL_free(s->tls_extension);
278 +               }
279 +#endif /* OPENSSL_NO_TLSEXT */
280         ssl3_free(s);
281         }
282  
283 @@ -357,8 +363,24 @@ unsigned char *ssl_add_clienthello_tlsex
284                 int ticklen;
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)
290 +                       {
291 +                       ticklen = s->tls_extension->length;
292 +                       s->session->tlsext_tick = OPENSSL_malloc(ticklen);
293 +                       if (!s->session->tlsext_tick)
294 +                               return NULL;
295 +                       memcpy(s->session->tlsext_tick, s->tls_extension->data,
296 +                              ticklen);
297 +                       s->session->tlsext_ticklen = ticklen;
298 +                       }
299                 else
300                         ticklen = 0;
301 +               if (ticklen == 0 && s->tls_extension &&
302 +                   s->tls_extension->type == TLSEXT_TYPE_session_ticket &&
303 +                   s->tls_extension->data == NULL)
304 +                       goto skip_ext;
305                 /* Check for enough room 2 for extension type, 2 for len
306                  * rest for ticket
307                  */
308 @@ -371,6 +393,7 @@ unsigned char *ssl_add_clienthello_tlsex
309                         ret += ticklen;
310                         }
311                 }
312 +               skip_ext:
313  
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 */
319                                 }
320 +                       if (s->tls_session_secret_cb)
321 +                               {
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
326 +                                * secret later. */
327 +                               return 0;
328 +                               }
329                         return tls_decrypt_ticket(s, p, size, session_id, len,
330                                                                         ret);
331                         }
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*/
338  #endif
339  
340 +/* TLS extension struct */
341 +struct tls_extension_st
342 +       {
343 +       unsigned short type;
344 +       unsigned short length;
345 +       void *data;
346 +       };
347 +
348  #ifdef  __cplusplus
349  }
350  #endif
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