2c27337894d4250fc6fe7feecd86ef0efd720bc6
[libeap.git] / patches / openssl-0.9.9-session-ticket.patch
1 This patch adds support for TLS SessionTicket extension (RFC 4507) 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-20071101.orig/ssl/s3_clnt.c openssl-SNAP-20071101/ssl/s3_clnt.c
10 --- openssl-SNAP-20071101.orig/ssl/s3_clnt.c    2007-10-26 06:00:28.000000000 -0700
11 +++ openssl-SNAP-20071101/ssl/s3_clnt.c 2007-11-01 19:19:43.000000000 -0700
12 @@ -715,7 +715,7 @@ int ssl3_get_server_hello(SSL *s)
13         STACK_OF(SSL_CIPHER) *sk;
14         SSL_CIPHER *c;
15         unsigned char *p,*d;
16 -       int i,al,ok;
17 +       int i,al,ok,pre_shared;
18         unsigned int j;
19         long n;
20  #ifndef OPENSSL_NO_COMP
21 @@ -782,7 +782,26 @@ int ssl3_get_server_hello(SSL *s)
22                 goto f_err;
23                 }
24  
25 -       if (j != 0 && j == s->session->session_id_length
26 +       /* check if we want to resume the session based on external pre-shared secret */
27 +       pre_shared = 0;
28 +#ifndef OPENSSL_NO_TLSEXT
29 +       if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
30 +       {
31 +               SSL_CIPHER *pref_cipher=NULL;
32 +               s->session->master_key_length=sizeof(s->session->master_key);
33 +               if (s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
34 +                       NULL, &pref_cipher, s->tls_session_secret_cb_arg))
35 +               {
36 +                       s->hit=1;
37 +                       s->session->cipher=pref_cipher ? pref_cipher : ssl_get_cipher_by_char(s,p+j);
38 +                       s->session->session_id_length = j;
39 +                       memcpy(s->session->session_id, p, j);
40 +                       pre_shared = 1;
41 +               }
42 +       }
43 +#endif /* OPENSSL_NO_TLSEXT */
44 +
45 +       if ((pre_shared || j != 0) && j == s->session->session_id_length
46             && memcmp(p,s->session->session_id,j) == 0)
47             {
48             if(s->sid_ctx_length != s->session->sid_ctx_length
49 diff -upr openssl-SNAP-20071101.orig/ssl/s3_srvr.c openssl-SNAP-20071101/ssl/s3_srvr.c
50 --- openssl-SNAP-20071101.orig/ssl/s3_srvr.c    2007-10-26 06:00:29.000000000 -0700
51 +++ openssl-SNAP-20071101/ssl/s3_srvr.c 2007-11-01 19:19:43.000000000 -0700
52 @@ -992,6 +992,59 @@ 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 +               }
108 +       }
109  #endif
110  
111         /* Worst case, we will use the NULL compression, but if we have other
112 @@ -1118,16 +1171,22 @@ int ssl3_send_server_hello(SSL *s)
113         unsigned char *buf;
114         unsigned char *p,*d;
115         int i,sl;
116 -       unsigned long l,Time;
117 +       unsigned long l;
118 +#ifdef OPENSSL_NO_TLSEXT
119 +       unsigned long Time;
120 +#endif
121  
122         if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
123                 {
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 */
129                 l2n(Time,p);
130                 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
131                         return -1;
132 +#endif
133                 /* Do the message type and length last */
134                 d=p= &(buf[4]);
135  
136 diff -upr openssl-SNAP-20071101.orig/ssl/ssl.h openssl-SNAP-20071101/ssl/ssl.h
137 --- openssl-SNAP-20071101.orig/ssl/ssl.h        2007-10-26 17:01:28.000000000 -0700
138 +++ openssl-SNAP-20071101/ssl/ssl.h     2007-11-01 19:20:47.000000000 -0700
139 @@ -353,6 +353,7 @@ extern "C" {
140   * 'struct ssl_st *' function parameters used to prototype callbacks
141   * in SSL_CTX. */
142  typedef struct ssl_st *ssl_crock_st;
143 +typedef struct tls_extension_st TLS_EXTENSION;
144  
145  /* used to hold info on the particular ciphers used */
146  typedef struct ssl_cipher_st
147 @@ -379,6 +380,8 @@ DECLARE_STACK_OF(SSL_CIPHER)
148  typedef struct ssl_st SSL;
149  typedef struct ssl_ctx_st SSL_CTX;
150  
151 +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);
152 +
153  /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
154  typedef struct ssl_method_st
155         {
156 @@ -1121,6 +1124,13 @@ struct ssl_st
157         void *tlsext_opaque_prf_input;
158         size_t tlsext_opaque_prf_input_len;
159  
160 +       /* TLS extensions */
161 +       TLS_EXTENSION *tls_extension;
162 +
163 +       /* TLS pre-shared secret session resumption */
164 +       tls_session_secret_cb_fn tls_session_secret_cb;
165 +       void *tls_session_secret_cb_arg;
166 +
167         SSL_CTX * initial_ctx; /* initial ctx, used to store sessions */
168  #define session_ctx initial_ctx
169  #else
170 @@ -1721,6 +1731,12 @@ void *SSL_COMP_get_compression_methods(v
171  int SSL_COMP_add_compression_method(int id,void *cm);
172  #endif
173  
174 +/* TLS extensions functions */
175 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len);
176 +
177 +/* Pre-shared secret session resumption functions */
178 +int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
179 +
180  /* BEGIN ERROR CODES */
181  /* The following lines are auto generated by the script mkerr.pl. Any changes
182   * made after this point may be overwritten when the script is next run.
183 @@ -1920,6 +1936,7 @@ void ERR_load_SSL_strings(void);
184  #define SSL_F_TLS1_PRF                                  284
185  #define SSL_F_TLS1_SETUP_KEY_BLOCK                      211
186  #define SSL_F_WRITE_PENDING                             212
187 +#define SSL_F_SSL_SET_HELLO_EXTENSION                   213
188  
189  /* Reason codes. */
190  #define SSL_R_APP_DATA_IN_HANDSHAKE                     100
191 diff -upr openssl-SNAP-20071101.orig/ssl/ssl_err.c openssl-SNAP-20071101/ssl/ssl_err.c
192 --- openssl-SNAP-20071101.orig/ssl/ssl_err.c    2007-10-26 17:01:29.000000000 -0700
193 +++ openssl-SNAP-20071101/ssl/ssl_err.c 2007-11-01 19:19:43.000000000 -0700
194 @@ -260,6 +260,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
195  {ERR_FUNC(SSL_F_TLS1_PRF),     "tls1_prf"},
196  {ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK), "TLS1_SETUP_KEY_BLOCK"},
197  {ERR_FUNC(SSL_F_WRITE_PENDING),        "WRITE_PENDING"},
198 +{ERR_FUNC(SSL_F_SSL_SET_HELLO_EXTENSION), "SSL_set_hello_extension"},
199  {0,NULL}
200         };
201  
202 diff -upr openssl-SNAP-20071101.orig/ssl/ssl_sess.c openssl-SNAP-20071101/ssl/ssl_sess.c
203 --- openssl-SNAP-20071101.orig/ssl/ssl_sess.c   2007-10-17 11:00:45.000000000 -0700
204 +++ openssl-SNAP-20071101/ssl/ssl_sess.c        2007-11-01 19:19:43.000000000 -0700
205 @@ -831,6 +831,52 @@ long SSL_CTX_get_timeout(const SSL_CTX *
206         return(s->session_timeout);
207         }
208  
209 +#ifndef OPENSSL_NO_TLSEXT
210 +int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len, 
211 +       STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
212 +{
213 +       if (s == NULL) return(0);
214 +       s->tls_session_secret_cb = tls_session_secret_cb;
215 +       s->tls_session_secret_cb_arg = arg;
216 +       return(1);
217 +}
218 +
219 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len)
220 +{
221 +       if(s->version >= TLS1_VERSION)
222 +       {
223 +               if(s->tls_extension)
224 +               {
225 +                       OPENSSL_free(s->tls_extension);
226 +                       s->tls_extension = NULL;
227 +               }
228 +
229 +               s->tls_extension = OPENSSL_malloc(sizeof(TLS_EXTENSION) + ext_len);
230 +               if(!s->tls_extension)
231 +               {
232 +                       SSLerr(SSL_F_SSL_SET_HELLO_EXTENSION, ERR_R_MALLOC_FAILURE);
233 +                       return 0;
234 +               }
235 +
236 +               s->tls_extension->type = ext_type;
237 +
238 +               if(ext_data)
239 +               {
240 +                       s->tls_extension->length = ext_len;
241 +                       s->tls_extension->data = s->tls_extension + 1;
242 +                       memcpy(s->tls_extension->data, ext_data, ext_len);
243 +               } else {
244 +                       s->tls_extension->length = 0;
245 +                       s->tls_extension->data = NULL;
246 +               }
247 +
248 +               return 1;
249 +       }
250 +
251 +       return 0;
252 +}
253 +#endif /* OPENSSL_NO_TLSEXT */
254 +
255  typedef struct timeout_param_st
256         {
257         SSL_CTX *ctx;
258 diff -upr openssl-SNAP-20071101.orig/ssl/t1_lib.c openssl-SNAP-20071101/ssl/t1_lib.c
259 --- openssl-SNAP-20071101.orig/ssl/t1_lib.c     2007-10-26 06:00:29.000000000 -0700
260 +++ openssl-SNAP-20071101/ssl/t1_lib.c  2007-11-01 19:19:43.000000000 -0700
261 @@ -154,6 +154,12 @@ int tls1_new(SSL *s)
262  
263  void tls1_free(SSL *s)
264         {
265 +#ifndef OPENSSL_NO_TLSEXT
266 +       if(s->tls_extension)
267 +       {
268 +               OPENSSL_free(s->tls_extension);
269 +       }
270 +#endif
271         ssl3_free(s);
272         }
273  
274 @@ -355,8 +361,24 @@ unsigned char *ssl_add_clienthello_tlsex
275                 int ticklen;
276                 if (s->session && s->session->tlsext_tick)
277                         ticklen = s->session->tlsext_ticklen;
278 +               else if (s->session && s->tls_extension &&
279 +                       s->tls_extension->type == TLSEXT_TYPE_session_ticket &&
280 +                       s->tls_extension->data)
281 +               {
282 +                       ticklen = s->tls_extension->length;
283 +                       s->session->tlsext_tick = OPENSSL_malloc(ticklen);
284 +                       if (!s->session->tlsext_tick)
285 +                               return NULL;
286 +                       memcpy(s->session->tlsext_tick, s->tls_extension->data,
287 +                              ticklen);
288 +                       s->session->tlsext_ticklen = ticklen;
289 +               }
290                 else
291                         ticklen = 0;
292 +               if (ticklen == 0 && s->tls_extension &&
293 +                   s->tls_extension->type == TLSEXT_TYPE_session_ticket &&
294 +                   s->tls_extension->data == NULL)
295 +                       goto skip_ext;
296                 /* Check for enough room 2 for extension type, 2 for len
297                  * rest for ticket
298                  */
299 @@ -369,6 +391,7 @@ unsigned char *ssl_add_clienthello_tlsex
300                         ret += ticklen;
301                         }
302                 }
303 +               skip_ext:
304  
305  #ifdef TLSEXT_TYPE_opaque_prf_input
306         if (s->s3->client_opaque_prf_input != NULL)
307 @@ -1422,6 +1445,8 @@ int tls1_process_ticket(SSL *s, unsigned
308                                 s->tlsext_ticket_expected = 1;
309                                 return 0;       /* Cache miss */
310                                 }
311 +                       if (s->tls_session_secret_cb)
312 +                               return 0;
313                         return tls_decrypt_ticket(s, p, size, session_id, len,
314                                                                         ret);
315                         }
316 diff -upr openssl-SNAP-20071101.orig/ssl/tls1.h openssl-SNAP-20071101/ssl/tls1.h
317 --- openssl-SNAP-20071101.orig/ssl/tls1.h       2007-09-26 15:01:39.000000000 -0700
318 +++ openssl-SNAP-20071101/ssl/tls1.h    2007-11-01 19:19:43.000000000 -0700
319 @@ -509,6 +509,14 @@ SSL_CTX_ctrl(ctx,SSL_CTRL_SET_TLSEXT_OPA
320  #define TLS_MD_MASTER_SECRET_CONST    "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74"  /*master secret*/
321  #endif
322  
323 +/* TLS extension struct */
324 +struct tls_extension_st
325 +{
326 +       unsigned short type;
327 +       unsigned short length;
328 +       void *data;
329 +};
330 +
331  #ifdef  __cplusplus
332  }
333  #endif
334 diff -upr openssl-SNAP-20071101.orig/util/ssleay.num openssl-SNAP-20071101/util/ssleay.num
335 --- openssl-SNAP-20071101.orig/util/ssleay.num  2007-08-31 06:03:14.000000000 -0700
336 +++ openssl-SNAP-20071101/util/ssleay.num       2007-11-01 19:19:43.000000000 -0700
337 @@ -253,3 +253,5 @@ PEM_write_bio_SSL_SESSION               
338  PEM_read_SSL_SESSION                    302    EXIST:!WIN16:FUNCTION:
339  PEM_read_bio_SSL_SESSION                303    EXIST::FUNCTION:
340  PEM_write_SSL_SESSION                   304    EXIST:!WIN16:FUNCTION:
341 +SSL_set_hello_extension                        305     EXIST::FUNCTION:TLSEXT
342 +SSL_set_session_secret_cb              306     EXIST::FUNCTION:TLSEXT