Fixed fallback to full handshake when server rejects PAC-Opaque
[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 diff -upr openssl-SNAP-20080415.orig/ssl/s3_clnt.c openssl-SNAP-20080415/ssl/s3_clnt.c
10 --- openssl-SNAP-20080415.orig/ssl/s3_clnt.c    2008-01-06 00:00:33.000000000 +0200
11 +++ openssl-SNAP-20080415/ssl/s3_clnt.c 2008-04-15 16:58:39.000000000 +0300
12 @@ -785,6 +785,20 @@ int ssl3_get_server_hello(SSL *s)
13                 goto f_err;
14                 }
15  
16 +#ifndef OPENSSL_NO_TLSEXT
17 +       /* check if we want to resume the session based on external pre-shared secret */
18 +       if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
19 +       {
20 +               SSL_CIPHER *pref_cipher=NULL;
21 +               s->session->master_key_length=sizeof(s->session->master_key);
22 +               if (s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
23 +                       NULL, &pref_cipher, s->tls_session_secret_cb_arg))
24 +               {
25 +                       s->session->cipher=pref_cipher ? pref_cipher : ssl_get_cipher_by_char(s,p+j);
26 +               }
27 +       }
28 +#endif /* OPENSSL_NO_TLSEXT */
29 +
30         if (j != 0 && j == s->session->session_id_length
31             && memcmp(p,s->session->session_id,j) == 0)
32             {
33 diff -upr openssl-SNAP-20080415.orig/ssl/s3_srvr.c openssl-SNAP-20080415/ssl/s3_srvr.c
34 --- openssl-SNAP-20080415.orig/ssl/s3_srvr.c    2007-10-26 16:00:29.000000000 +0300
35 +++ openssl-SNAP-20080415/ssl/s3_srvr.c 2008-04-15 16:32:08.000000000 +0300
36 @@ -992,6 +992,59 @@ int ssl3_get_client_hello(SSL *s)
37                         SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
38                         goto err;
39                 }
40 +
41 +       /* Check if we want to use external pre-shared secret for this
42 +        * handshake for not reused session only. We need to generate
43 +        * server_random before calling tls_session_secret_cb in order to allow
44 +        * SessionTicket processing to use it in key derivation. */
45 +       {
46 +               unsigned long Time;
47 +               unsigned char *pos;
48 +               Time=(unsigned long)time(NULL);                 /* Time */
49 +               pos=s->s3->server_random;
50 +               l2n(Time,pos);
51 +               if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0)
52 +               {
53 +                       al=SSL_AD_INTERNAL_ERROR;
54 +                       goto f_err;
55 +               }
56 +       }
57 +
58 +       if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
59 +       {
60 +               SSL_CIPHER *pref_cipher=NULL;
61 +
62 +               s->session->master_key_length=sizeof(s->session->master_key);
63 +               if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length, 
64 +                       ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
65 +               {
66 +                       s->hit=1;
67 +                       s->session->ciphers=ciphers;
68 +                       s->session->verify_result=X509_V_OK;
69 +                       
70 +                       ciphers=NULL;
71 +                       
72 +                       /* check if some cipher was preferred by call back */
73 +                       pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
74 +                       if (pref_cipher == NULL)
75 +                               {
76 +                               al=SSL_AD_HANDSHAKE_FAILURE;
77 +                               SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
78 +                               goto f_err;
79 +                               }
80 +
81 +                       s->session->cipher=pref_cipher;
82 +
83 +                       if (s->cipher_list)
84 +                               sk_SSL_CIPHER_free(s->cipher_list);
85 +
86 +                       if (s->cipher_list_by_id)
87 +                               sk_SSL_CIPHER_free(s->cipher_list_by_id);
88 +
89 +                       s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
90 +                       s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
91 +               }
92 +       }
93  #endif
94  
95         /* Worst case, we will use the NULL compression, but if we have other
96 @@ -1118,16 +1171,22 @@ int ssl3_send_server_hello(SSL *s)
97         unsigned char *buf;
98         unsigned char *p,*d;
99         int i,sl;
100 -       unsigned long l,Time;
101 +       unsigned long l;
102 +#ifdef OPENSSL_NO_TLSEXT
103 +       unsigned long Time;
104 +#endif
105  
106         if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
107                 {
108                 buf=(unsigned char *)s->init_buf->data;
109 +#ifdef OPENSSL_NO_TLSEXT
110                 p=s->s3->server_random;
111 +               /* Generate server_random if it was not needed previously */
112                 Time=(unsigned long)time(NULL);                 /* Time */
113                 l2n(Time,p);
114                 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
115                         return -1;
116 +#endif
117                 /* Do the message type and length last */
118                 d=p= &(buf[4]);
119  
120 diff -upr openssl-SNAP-20080415.orig/ssl/ssl.h openssl-SNAP-20080415/ssl/ssl.h
121 --- openssl-SNAP-20080415.orig/ssl/ssl.h        2007-10-27 03:01:28.000000000 +0300
122 +++ openssl-SNAP-20080415/ssl/ssl.h     2008-04-15 16:32:08.000000000 +0300
123 @@ -353,6 +353,7 @@ extern "C" {
124   * 'struct ssl_st *' function parameters used to prototype callbacks
125   * in SSL_CTX. */
126  typedef struct ssl_st *ssl_crock_st;
127 +typedef struct tls_extension_st TLS_EXTENSION;
128  
129  /* used to hold info on the particular ciphers used */
130  typedef struct ssl_cipher_st
131 @@ -379,6 +380,8 @@ DECLARE_STACK_OF(SSL_CIPHER)
132  typedef struct ssl_st SSL;
133  typedef struct ssl_ctx_st SSL_CTX;
134  
135 +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);
136 +
137  /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
138  typedef struct ssl_method_st
139         {
140 @@ -1121,6 +1124,13 @@ struct ssl_st
141         void *tlsext_opaque_prf_input;
142         size_t tlsext_opaque_prf_input_len;
143  
144 +       /* TLS extensions */
145 +       TLS_EXTENSION *tls_extension;
146 +
147 +       /* TLS pre-shared secret session resumption */
148 +       tls_session_secret_cb_fn tls_session_secret_cb;
149 +       void *tls_session_secret_cb_arg;
150 +
151         SSL_CTX * initial_ctx; /* initial ctx, used to store sessions */
152  #define session_ctx initial_ctx
153  #else
154 @@ -1721,6 +1731,12 @@ void *SSL_COMP_get_compression_methods(v
155  int SSL_COMP_add_compression_method(int id,void *cm);
156  #endif
157  
158 +/* TLS extensions functions */
159 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len);
160 +
161 +/* Pre-shared secret session resumption functions */
162 +int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
163 +
164  /* BEGIN ERROR CODES */
165  /* The following lines are auto generated by the script mkerr.pl. Any changes
166   * made after this point may be overwritten when the script is next run.
167 @@ -1920,6 +1936,7 @@ void ERR_load_SSL_strings(void);
168  #define SSL_F_TLS1_PRF                                  284
169  #define SSL_F_TLS1_SETUP_KEY_BLOCK                      211
170  #define SSL_F_WRITE_PENDING                             212
171 +#define SSL_F_SSL_SET_HELLO_EXTENSION                   213
172  
173  /* Reason codes. */
174  #define SSL_R_APP_DATA_IN_HANDSHAKE                     100
175 diff -upr openssl-SNAP-20080415.orig/ssl/ssl_err.c openssl-SNAP-20080415/ssl/ssl_err.c
176 --- openssl-SNAP-20080415.orig/ssl/ssl_err.c    2007-10-27 03:01:29.000000000 +0300
177 +++ openssl-SNAP-20080415/ssl/ssl_err.c 2008-04-15 16:32:08.000000000 +0300
178 @@ -260,6 +260,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
179  {ERR_FUNC(SSL_F_TLS1_PRF),     "tls1_prf"},
180  {ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK), "TLS1_SETUP_KEY_BLOCK"},
181  {ERR_FUNC(SSL_F_WRITE_PENDING),        "WRITE_PENDING"},
182 +{ERR_FUNC(SSL_F_SSL_SET_HELLO_EXTENSION), "SSL_set_hello_extension"},
183  {0,NULL}
184         };
185  
186 diff -upr openssl-SNAP-20080415.orig/ssl/ssl_sess.c openssl-SNAP-20080415/ssl/ssl_sess.c
187 --- openssl-SNAP-20080415.orig/ssl/ssl_sess.c   2007-10-17 21:00:45.000000000 +0300
188 +++ openssl-SNAP-20080415/ssl/ssl_sess.c        2008-04-15 16:32:08.000000000 +0300
189 @@ -831,6 +831,52 @@ long SSL_CTX_get_timeout(const SSL_CTX *
190         return(s->session_timeout);
191         }
192  
193 +#ifndef OPENSSL_NO_TLSEXT
194 +int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len, 
195 +       STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
196 +{
197 +       if (s == NULL) return(0);
198 +       s->tls_session_secret_cb = tls_session_secret_cb;
199 +       s->tls_session_secret_cb_arg = arg;
200 +       return(1);
201 +}
202 +
203 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len)
204 +{
205 +       if(s->version >= TLS1_VERSION)
206 +       {
207 +               if(s->tls_extension)
208 +               {
209 +                       OPENSSL_free(s->tls_extension);
210 +                       s->tls_extension = NULL;
211 +               }
212 +
213 +               s->tls_extension = OPENSSL_malloc(sizeof(TLS_EXTENSION) + ext_len);
214 +               if(!s->tls_extension)
215 +               {
216 +                       SSLerr(SSL_F_SSL_SET_HELLO_EXTENSION, ERR_R_MALLOC_FAILURE);
217 +                       return 0;
218 +               }
219 +
220 +               s->tls_extension->type = ext_type;
221 +
222 +               if(ext_data)
223 +               {
224 +                       s->tls_extension->length = ext_len;
225 +                       s->tls_extension->data = s->tls_extension + 1;
226 +                       memcpy(s->tls_extension->data, ext_data, ext_len);
227 +               } else {
228 +                       s->tls_extension->length = 0;
229 +                       s->tls_extension->data = NULL;
230 +               }
231 +
232 +               return 1;
233 +       }
234 +
235 +       return 0;
236 +}
237 +#endif /* OPENSSL_NO_TLSEXT */
238 +
239  typedef struct timeout_param_st
240         {
241         SSL_CTX *ctx;
242 diff -upr openssl-SNAP-20080415.orig/ssl/t1_lib.c openssl-SNAP-20080415/ssl/t1_lib.c
243 --- openssl-SNAP-20080415.orig/ssl/t1_lib.c     2008-03-17 00:00:10.000000000 +0200
244 +++ openssl-SNAP-20080415/ssl/t1_lib.c  2008-04-15 16:32:08.000000000 +0300
245 @@ -154,6 +154,12 @@ int tls1_new(SSL *s)
246  
247  void tls1_free(SSL *s)
248         {
249 +#ifndef OPENSSL_NO_TLSEXT
250 +       if(s->tls_extension)
251 +       {
252 +               OPENSSL_free(s->tls_extension);
253 +       }
254 +#endif
255         ssl3_free(s);
256         }
257  
258 @@ -357,8 +363,24 @@ unsigned char *ssl_add_clienthello_tlsex
259                 int ticklen;
260                 if (s->session && s->session->tlsext_tick)
261                         ticklen = s->session->tlsext_ticklen;
262 +               else if (s->session && s->tls_extension &&
263 +                       s->tls_extension->type == TLSEXT_TYPE_session_ticket &&
264 +                       s->tls_extension->data)
265 +               {
266 +                       ticklen = s->tls_extension->length;
267 +                       s->session->tlsext_tick = OPENSSL_malloc(ticklen);
268 +                       if (!s->session->tlsext_tick)
269 +                               return NULL;
270 +                       memcpy(s->session->tlsext_tick, s->tls_extension->data,
271 +                              ticklen);
272 +                       s->session->tlsext_ticklen = ticklen;
273 +               }
274                 else
275                         ticklen = 0;
276 +               if (ticklen == 0 && s->tls_extension &&
277 +                   s->tls_extension->type == TLSEXT_TYPE_session_ticket &&
278 +                   s->tls_extension->data == NULL)
279 +                       goto skip_ext;
280                 /* Check for enough room 2 for extension type, 2 for len
281                  * rest for ticket
282                  */
283 @@ -371,6 +393,7 @@ unsigned char *ssl_add_clienthello_tlsex
284                         ret += ticklen;
285                         }
286                 }
287 +               skip_ext:
288  
289  #ifdef TLSEXT_TYPE_opaque_prf_input
290         if (s->s3->client_opaque_prf_input != NULL)
291 @@ -1425,6 +1448,8 @@ int tls1_process_ticket(SSL *s, unsigned
292                                 s->tlsext_ticket_expected = 1;
293                                 return 0;       /* Cache miss */
294                                 }
295 +                       if (s->tls_session_secret_cb)
296 +                               return 0;
297                         return tls_decrypt_ticket(s, p, size, session_id, len,
298                                                                         ret);
299                         }
300 diff -upr openssl-SNAP-20080415.orig/ssl/tls1.h openssl-SNAP-20080415/ssl/tls1.h
301 --- openssl-SNAP-20080415.orig/ssl/tls1.h       2007-09-27 01:01:39.000000000 +0300
302 +++ openssl-SNAP-20080415/ssl/tls1.h    2008-04-15 16:32:08.000000000 +0300
303 @@ -509,6 +509,14 @@ SSL_CTX_ctrl(ctx,SSL_CTRL_SET_TLSEXT_OPA
304  #define TLS_MD_MASTER_SECRET_CONST    "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74"  /*master secret*/
305  #endif
306  
307 +/* TLS extension struct */
308 +struct tls_extension_st
309 +{
310 +       unsigned short type;
311 +       unsigned short length;
312 +       void *data;
313 +};
314 +
315  #ifdef  __cplusplus
316  }
317  #endif
318 diff -upr openssl-SNAP-20080415.orig/util/ssleay.num openssl-SNAP-20080415/util/ssleay.num
319 --- openssl-SNAP-20080415.orig/util/ssleay.num  2007-08-31 16:03:14.000000000 +0300
320 +++ openssl-SNAP-20080415/util/ssleay.num       2008-04-15 16:32:08.000000000 +0300
321 @@ -253,3 +253,5 @@ PEM_write_bio_SSL_SESSION               
322  PEM_read_SSL_SESSION                    302    EXIST:!WIN16:FUNCTION:
323  PEM_read_bio_SSL_SESSION                303    EXIST::FUNCTION:
324  PEM_write_SSL_SESSION                   304    EXIST:!WIN16:FUNCTION:
325 +SSL_set_hello_extension                        305     EXIST::FUNCTION:TLSEXT
326 +SSL_set_session_secret_cb              306     EXIST::FUNCTION:TLSEXT