Updated the OpenSSL EAP-FAST patch for the current OpenSSL 0.9.9 snapshot
[wpasupplicant] / 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-20080822/ssl/s3_clnt.c
10 ===================================================================
11 --- openssl-SNAP-20080822.orig/ssl/s3_clnt.c
12 +++ openssl-SNAP-20080822/ssl/s3_clnt.c
13 @@ -788,6 +788,20 @@ 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, &s->session->master_key_length,
24 +                       NULL, &pref_cipher, s->tls_session_secret_cb_arg))
25 +               {
26 +                       s->session->cipher=pref_cipher ? pref_cipher : ssl_get_cipher_by_char(s,p+j);
27 +               }
28 +       }
29 +#endif /* OPENSSL_NO_TLSEXT */
30 +
31         if (j != 0 && j == s->session->session_id_length
32             && memcmp(p,s->session->session_id,j) == 0)
33             {
34 @@ -2927,11 +2941,8 @@ static int ssl3_check_finished(SSL *s)
35         {
36         int ok;
37         long n;
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
40 -        * resumed session.
41 -        */
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)
45                 return 1;
46         /* this function is called when we really expect a Certificate
47          * message, so permit appropriate message length */
48 Index: openssl-SNAP-20080822/ssl/s3_srvr.c
49 ===================================================================
50 --- openssl-SNAP-20080822.orig/ssl/s3_srvr.c
51 +++ openssl-SNAP-20080822/ssl/s3_srvr.c
52 @@ -1004,6 +1004,61 @@ int ssl3_get_client_hello(SSL *s)
53                         SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
54                         goto err;
55                 }
56 +
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. */
61 +       {
62 +               unsigned long Time;
63 +               unsigned char *pos;
64 +               Time=(unsigned long)time(NULL);                 /* Time */
65 +               pos=s->s3->server_random;
66 +               l2n(Time,pos);
67 +               if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0)
68 +               {
69 +                       al=SSL_AD_INTERNAL_ERROR;
70 +                       goto f_err;
71 +               }
72 +       }
73 +
74 +       if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
75 +       {
76 +               SSL_CIPHER *pref_cipher=NULL;
77 +
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))
81 +               {
82 +                       s->hit=1;
83 +                       s->session->ciphers=ciphers;
84 +                       s->session->verify_result=X509_V_OK;
85 +
86 +                       ciphers=NULL;
87 +
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)
91 +                               {
92 +                               al=SSL_AD_HANDSHAKE_FAILURE;
93 +                               SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
94 +                               goto f_err;
95 +                               }
96 +
97 +                       s->session->cipher=pref_cipher;
98 +
99 +                       if (s->cipher_list)
100 +                               sk_SSL_CIPHER_free(s->cipher_list);
101 +
102 +                       if (s->cipher_list_by_id)
103 +                               sk_SSL_CIPHER_free(s->cipher_list_by_id);
104 +
105 +                       s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
106 +                       s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
107 +                       s->s3->tmp.new_cipher = pref_cipher;
108 +                       ssl3_digest_cached_records(s);
109 +               }
110 +       }
111  #endif
112  
113         /* Worst case, we will use the NULL compression, but if we have other
114 @@ -1130,16 +1185,22 @@ int ssl3_send_server_hello(SSL *s)
115         unsigned char *buf;
116         unsigned char *p,*d;
117         int i,sl;
118 -       unsigned long l,Time;
119 +       unsigned long l;
120 +#ifdef OPENSSL_NO_TLSEXT
121 +       unsigned long Time;
122 +#endif
123  
124         if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
125                 {
126                 buf=(unsigned char *)s->init_buf->data;
127 +#ifdef OPENSSL_NO_TLSEXT
128                 p=s->s3->server_random;
129 +               /* Generate server_random if it was not needed previously */
130                 Time=(unsigned long)time(NULL);                 /* Time */
131                 l2n(Time,p);
132                 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
133                         return -1;
134 +#endif
135                 /* Do the message type and length last */
136                 d=p= &(buf[4]);
137  
138 Index: openssl-SNAP-20080822/ssl/ssl_err.c
139 ===================================================================
140 --- openssl-SNAP-20080822.orig/ssl/ssl_err.c
141 +++ openssl-SNAP-20080822/ssl/ssl_err.c
142 @@ -263,6 +263,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
143  {ERR_FUNC(SSL_F_TLS1_PRF),     "tls1_prf"},
144  {ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK), "TLS1_SETUP_KEY_BLOCK"},
145  {ERR_FUNC(SSL_F_WRITE_PENDING),        "WRITE_PENDING"},
146 +{ERR_FUNC(SSL_F_SSL_SET_HELLO_EXTENSION), "SSL_set_hello_extension"},
147  {0,NULL}
148         };
149  
150 Index: openssl-SNAP-20080822/ssl/ssl.h
151 ===================================================================
152 --- openssl-SNAP-20080822.orig/ssl/ssl.h
153 +++ openssl-SNAP-20080822/ssl/ssl.h
154 @@ -354,6 +354,7 @@ extern "C" {
155   * 'struct ssl_st *' function parameters used to prototype callbacks
156   * in SSL_CTX. */
157  typedef struct ssl_st *ssl_crock_st;
158 +typedef struct tls_extension_st TLS_EXTENSION;
159  
160  /* used to hold info on the particular ciphers used */
161  typedef struct ssl_cipher_st
162 @@ -377,6 +378,8 @@ typedef struct ssl_cipher_st
163  
164  DECLARE_STACK_OF(SSL_CIPHER)
165  
166 +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);
167 +
168  /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
169  typedef struct ssl_method_st
170         {
171 @@ -1144,6 +1147,13 @@ struct ssl_st
172         void *tlsext_opaque_prf_input;
173         size_t tlsext_opaque_prf_input_len;
174  
175 +       /* TLS extensions */
176 +       TLS_EXTENSION *tls_extension;
177 +
178 +       /* TLS pre-shared secret session resumption */
179 +       tls_session_secret_cb_fn tls_session_secret_cb;
180 +       void *tls_session_secret_cb_arg;
181 +
182         SSL_CTX * initial_ctx; /* initial ctx, used to store sessions */
183  #define session_ctx initial_ctx
184  #else
185 @@ -1745,6 +1755,12 @@ void *SSL_COMP_get_compression_methods(v
186  int SSL_COMP_add_compression_method(int id,void *cm);
187  #endif
188  
189 +/* TLS extensions functions */
190 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len);
191 +
192 +/* Pre-shared secret session resumption functions */
193 +int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
194 +
195  /* BEGIN ERROR CODES */
196  /* The following lines are auto generated by the script mkerr.pl. Any changes
197   * made after this point may be overwritten when the script is next run.
198 @@ -1947,6 +1963,7 @@ void ERR_load_SSL_strings(void);
199  #define SSL_F_TLS1_PRF                                  284
200  #define SSL_F_TLS1_SETUP_KEY_BLOCK                      211
201  #define SSL_F_WRITE_PENDING                             212
202 +#define SSL_F_SSL_SET_HELLO_EXTENSION                   213
203  
204  /* Reason codes. */
205  #define SSL_R_APP_DATA_IN_HANDSHAKE                     100
206 Index: openssl-SNAP-20080822/ssl/ssl_sess.c
207 ===================================================================
208 --- openssl-SNAP-20080822.orig/ssl/ssl_sess.c
209 +++ openssl-SNAP-20080822/ssl/ssl_sess.c
210 @@ -834,6 +834,52 @@ long SSL_CTX_get_timeout(const SSL_CTX *
211         return(s->session_timeout);
212         }
213  
214 +#ifndef OPENSSL_NO_TLSEXT
215 +int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
216 +       STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
217 +{
218 +       if (s == NULL) return(0);
219 +       s->tls_session_secret_cb = tls_session_secret_cb;
220 +       s->tls_session_secret_cb_arg = arg;
221 +       return(1);
222 +}
223 +
224 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len)
225 +{
226 +       if(s->version >= TLS1_VERSION)
227 +       {
228 +               if(s->tls_extension)
229 +               {
230 +                       OPENSSL_free(s->tls_extension);
231 +                       s->tls_extension = NULL;
232 +               }
233 +
234 +               s->tls_extension = OPENSSL_malloc(sizeof(TLS_EXTENSION) + ext_len);
235 +               if(!s->tls_extension)
236 +               {
237 +                       SSLerr(SSL_F_SSL_SET_HELLO_EXTENSION, ERR_R_MALLOC_FAILURE);
238 +                       return 0;
239 +               }
240 +
241 +               s->tls_extension->type = ext_type;
242 +
243 +               if(ext_data)
244 +               {
245 +                       s->tls_extension->length = ext_len;
246 +                       s->tls_extension->data = s->tls_extension + 1;
247 +                       memcpy(s->tls_extension->data, ext_data, ext_len);
248 +               } else {
249 +                       s->tls_extension->length = 0;
250 +                       s->tls_extension->data = NULL;
251 +               }
252 +
253 +               return 1;
254 +       }
255 +
256 +       return 0;
257 +}
258 +#endif /* OPENSSL_NO_TLSEXT */
259 +
260  typedef struct timeout_param_st
261         {
262         SSL_CTX *ctx;
263 Index: openssl-SNAP-20080822/ssl/t1_lib.c
264 ===================================================================
265 --- openssl-SNAP-20080822.orig/ssl/t1_lib.c
266 +++ openssl-SNAP-20080822/ssl/t1_lib.c
267 @@ -154,6 +154,12 @@ int tls1_new(SSL *s)
268  
269  void tls1_free(SSL *s)
270         {
271 +#ifndef OPENSSL_NO_TLSEXT
272 +       if(s->tls_extension)
273 +       {
274 +               OPENSSL_free(s->tls_extension);
275 +       }
276 +#endif
277         ssl3_free(s);
278         }
279  
280 @@ -357,8 +363,24 @@ unsigned char *ssl_add_clienthello_tlsex
281                 int ticklen;
282                 if (s->session && s->session->tlsext_tick)
283                         ticklen = s->session->tlsext_ticklen;
284 +               else if (s->session && s->tls_extension &&
285 +                       s->tls_extension->type == TLSEXT_TYPE_session_ticket &&
286 +                       s->tls_extension->data)
287 +               {
288 +                       ticklen = s->tls_extension->length;
289 +                       s->session->tlsext_tick = OPENSSL_malloc(ticklen);
290 +                       if (!s->session->tlsext_tick)
291 +                               return NULL;
292 +                       memcpy(s->session->tlsext_tick, s->tls_extension->data,
293 +                              ticklen);
294 +                       s->session->tlsext_ticklen = ticklen;
295 +               }
296                 else
297                         ticklen = 0;
298 +               if (ticklen == 0 && s->tls_extension &&
299 +                   s->tls_extension->type == TLSEXT_TYPE_session_ticket &&
300 +                   s->tls_extension->data == NULL)
301 +                       goto skip_ext;
302                 /* Check for enough room 2 for extension type, 2 for len
303                  * rest for ticket
304                  */
305 @@ -371,6 +393,7 @@ unsigned char *ssl_add_clienthello_tlsex
306                         ret += ticklen;
307                         }
308                 }
309 +               skip_ext:
310  
311  #ifdef TLSEXT_TYPE_opaque_prf_input
312         if (s->s3->client_opaque_prf_input != NULL)
313 @@ -1428,6 +1451,15 @@ int tls1_process_ticket(SSL *s, unsigned
314                                 s->tlsext_ticket_expected = 1;
315                                 return 0;       /* Cache miss */
316                                 }
317 +                       if (s->tls_session_secret_cb)
318 +                               {
319 +                               /* Indicate cache miss here and instead of
320 +                                * generating the session from ticket now,
321 +                                * trigger abbreviated handshake based on
322 +                                * external mechanism to calculate the master
323 +                                * secret later. */
324 +                               return 0;
325 +                               }
326                         return tls_decrypt_ticket(s, p, size, session_id, len,
327                                                                         ret);
328                         }
329 Index: openssl-SNAP-20080822/ssl/tls1.h
330 ===================================================================
331 --- openssl-SNAP-20080822.orig/ssl/tls1.h
332 +++ openssl-SNAP-20080822/ssl/tls1.h
333 @@ -512,6 +512,14 @@ SSL_CTX_callback_ctrl(ssl,SSL_CTRL_SET_T
334  #define TLS_MD_MASTER_SECRET_CONST    "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74"  /*master secret*/
335  #endif
336  
337 +/* TLS extension struct */
338 +struct tls_extension_st
339 +{
340 +       unsigned short type;
341 +       unsigned short length;
342 +       void *data;
343 +};
344 +
345  #ifdef  __cplusplus
346  }
347  #endif
348 Index: openssl-SNAP-20080822/util/ssleay.num
349 ===================================================================
350 --- openssl-SNAP-20080822.orig/util/ssleay.num
351 +++ openssl-SNAP-20080822/util/ssleay.num
352 @@ -254,3 +254,5 @@ PEM_read_bio_SSL_SESSION                
353  SSL_CTX_set_psk_server_callback         303    EXIST::FUNCTION:PSK
354  SSL_get_psk_identity                    304    EXIST::FUNCTION:PSK
355  PEM_write_SSL_SESSION                   305    EXIST:!WIN16:FUNCTION:
356 +SSL_set_hello_extension                        306     EXIST::FUNCTION:TLSEXT
357 +SSL_set_session_secret_cb              307     EXIST::FUNCTION:TLSEXT