udp: Merge udp_send and udp_output
[dragonfly.git] / contrib / hostapd / patches / openssl-0.9.8h-tls-extensions.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 OpenSSL 0.9.8h does not enable TLS extension support by default, so it
8 will need to be enabled by adding enable-tlsext to config script
9 command line.
10
11
12 diff -upr openssl-0.9.8h.orig/ssl/s3_clnt.c openssl-0.9.8h/ssl/s3_clnt.c
13 --- openssl-0.9.8h.orig/ssl/s3_clnt.c   2008-05-28 10:29:27.000000000 +0300
14 +++ openssl-0.9.8h/ssl/s3_clnt.c        2008-05-29 10:44:25.000000000 +0300
15 @@ -752,6 +752,20 @@ int ssl3_get_server_hello(SSL *s)
16                 goto f_err;
17                 }
18  
19 +#ifndef OPENSSL_NO_TLSEXT
20 +       /* check if we want to resume the session based on external pre-shared secret */
21 +       if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
22 +       {
23 +               SSL_CIPHER *pref_cipher=NULL;
24 +               s->session->master_key_length=sizeof(s->session->master_key);
25 +               if (s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
26 +                       NULL, &pref_cipher, s->tls_session_secret_cb_arg))
27 +               {
28 +                       s->session->cipher=pref_cipher ? pref_cipher : ssl_get_cipher_by_char(s,p+j);
29 +               }
30 +       }
31 +#endif /* OPENSSL_NO_TLSEXT */
32 +
33         if (j != 0 && j == s->session->session_id_length
34             && memcmp(p,s->session->session_id,j) == 0)
35             {
36 @@ -2693,11 +2707,8 @@ static int ssl3_check_finished(SSL *s)
37         {
38         int ok;
39         long n;
40 -       /* If we have no ticket or session ID is non-zero length (a match of
41 -        * a non-zero session length would never reach here) it cannot be a
42 -        * resumed session.
43 -        */
44 -       if (!s->session->tlsext_tick || s->session->session_id_length)
45 +       /* If we have no ticket it cannot be a resumed session. */
46 +       if (!s->session->tlsext_tick)
47                 return 1;
48         /* this function is called when we really expect a Certificate
49          * message, so permit appropriate message length */
50 diff -upr openssl-0.9.8h.orig/ssl/s3_srvr.c openssl-0.9.8h/ssl/s3_srvr.c
51 --- openssl-0.9.8h.orig/ssl/s3_srvr.c   2008-04-30 19:11:32.000000000 +0300
52 +++ openssl-0.9.8h/ssl/s3_srvr.c        2008-05-28 18:49:34.000000000 +0300
53 @@ -959,6 +959,59 @@ int ssl3_get_client_hello(SSL *s)
54                         SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
55                         goto err;
56                 }
57 +
58 +       /* Check if we want to use external pre-shared secret for this
59 +        * handshake for not reused session only. We need to generate
60 +        * server_random before calling tls_session_secret_cb in order to allow
61 +        * SessionTicket processing to use it in key derivation. */
62 +       {
63 +               unsigned long Time;
64 +               unsigned char *pos;
65 +               Time=(unsigned long)time(NULL);                 /* Time */
66 +               pos=s->s3->server_random;
67 +               l2n(Time,pos);
68 +               if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0)
69 +               {
70 +                       al=SSL_AD_INTERNAL_ERROR;
71 +                       goto f_err;
72 +               }
73 +       }
74 +
75 +       if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
76 +       {
77 +               SSL_CIPHER *pref_cipher=NULL;
78 +
79 +               s->session->master_key_length=sizeof(s->session->master_key);
80 +               if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length, 
81 +                       ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
82 +               {
83 +                       s->hit=1;
84 +                       s->session->ciphers=ciphers;
85 +                       s->session->verify_result=X509_V_OK;
86 +                       
87 +                       ciphers=NULL;
88 +                       
89 +                       /* check if some cipher was preferred by call back */
90 +                       pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
91 +                       if (pref_cipher == NULL)
92 +                               {
93 +                               al=SSL_AD_HANDSHAKE_FAILURE;
94 +                               SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
95 +                               goto f_err;
96 +                               }
97 +
98 +                       s->session->cipher=pref_cipher;
99 +
100 +                       if (s->cipher_list)
101 +                               sk_SSL_CIPHER_free(s->cipher_list);
102 +
103 +                       if (s->cipher_list_by_id)
104 +                               sk_SSL_CIPHER_free(s->cipher_list_by_id);
105 +
106 +                       s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
107 +                       s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
108 +               }
109 +       }
110  #endif
111         /* Worst case, we will use the NULL compression, but if we have other
112          * options, we will now look for them.  We have i-1 compression
113 @@ -1097,16 +1150,22 @@ int ssl3_send_server_hello(SSL *s)
114         unsigned char *buf;
115         unsigned char *p,*d;
116         int i,sl;
117 -       unsigned long l,Time;
118 +       unsigned long l;
119 +#ifdef OPENSSL_NO_TLSEXT
120 +       unsigned long Time;
121 +#endif
122  
123         if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
124                 {
125                 buf=(unsigned char *)s->init_buf->data;
126 +#ifdef OPENSSL_NO_TLSEXT
127                 p=s->s3->server_random;
128 +               /* Generate server_random if it was not needed previously */
129                 Time=(unsigned long)time(NULL);                 /* Time */
130                 l2n(Time,p);
131                 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
132                         return -1;
133 +#endif
134                 /* Do the message type and length last */
135                 d=p= &(buf[4]);
136  
137 diff -upr openssl-0.9.8h.orig/ssl/ssl.h openssl-0.9.8h/ssl/ssl.h
138 --- openssl-0.9.8h.orig/ssl/ssl.h       2008-04-30 19:11:32.000000000 +0300
139 +++ openssl-0.9.8h/ssl/ssl.h    2008-05-28 18:49:34.000000000 +0300
140 @@ -343,6 +343,7 @@ extern "C" {
141   * 'struct ssl_st *' function parameters used to prototype callbacks
142   * in SSL_CTX. */
143  typedef struct ssl_st *ssl_crock_st;
144 +typedef struct tls_extension_st TLS_EXTENSION;
145  
146  /* used to hold info on the particular ciphers used */
147  typedef struct ssl_cipher_st
148 @@ -364,6 +365,8 @@ DECLARE_STACK_OF(SSL_CIPHER)
149  typedef struct ssl_st SSL;
150  typedef struct ssl_ctx_st SSL_CTX;
151  
152 +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);
153 +
154  /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
155  typedef struct ssl_method_st
156         {
157 @@ -1027,6 +1030,14 @@ struct ssl_st
158  
159         /* RFC4507 session ticket expected to be received or sent */
160         int tlsext_ticket_expected;
161 +
162 +       /* TLS extensions */
163 +       TLS_EXTENSION *tls_extension;
164 +
165 +       /* TLS pre-shared secret session resumption */
166 +       tls_session_secret_cb_fn tls_session_secret_cb;
167 +       void *tls_session_secret_cb_arg;
168 +
169         SSL_CTX * initial_ctx; /* initial ctx, used to store sessions */
170  #define session_ctx initial_ctx
171  #else
172 @@ -1625,6 +1636,12 @@ void *SSL_COMP_get_compression_methods(v
173  int SSL_COMP_add_compression_method(int id,void *cm);
174  #endif
175  
176 +/* TLS extensions functions */
177 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len);
178 +
179 +/* Pre-shared secret session resumption functions */
180 +int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
181 +
182  /* BEGIN ERROR CODES */
183  /* The following lines are auto generated by the script mkerr.pl. Any changes
184   * made after this point may be overwritten when the script is next run.
185 @@ -1815,6 +1832,7 @@ void ERR_load_SSL_strings(void);
186  #define SSL_F_TLS1_ENC                                  210
187  #define SSL_F_TLS1_SETUP_KEY_BLOCK                      211
188  #define SSL_F_WRITE_PENDING                             212
189 +#define SSL_F_SSL_SET_HELLO_EXTENSION                   213
190  
191  /* Reason codes. */
192  #define SSL_R_APP_DATA_IN_HANDSHAKE                     100
193 diff -upr openssl-0.9.8h.orig/ssl/ssl_err.c openssl-0.9.8h/ssl/ssl_err.c
194 --- openssl-0.9.8h.orig/ssl/ssl_err.c   2007-10-12 03:00:30.000000000 +0300
195 +++ openssl-0.9.8h/ssl/ssl_err.c        2008-05-28 18:49:34.000000000 +0300
196 @@ -251,6 +251,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
197  {ERR_FUNC(SSL_F_TLS1_ENC),     "TLS1_ENC"},
198  {ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK), "TLS1_SETUP_KEY_BLOCK"},
199  {ERR_FUNC(SSL_F_WRITE_PENDING),        "WRITE_PENDING"},
200 +{ERR_FUNC(SSL_F_SSL_SET_HELLO_EXTENSION), "SSL_set_hello_extension"},
201  {0,NULL}
202         };
203  
204 diff -upr openssl-0.9.8h.orig/ssl/ssl_sess.c openssl-0.9.8h/ssl/ssl_sess.c
205 --- openssl-0.9.8h.orig/ssl/ssl_sess.c  2007-10-17 20:30:15.000000000 +0300
206 +++ openssl-0.9.8h/ssl/ssl_sess.c       2008-05-28 18:49:34.000000000 +0300
207 @@ -704,6 +704,52 @@ long SSL_CTX_get_timeout(const SSL_CTX *
208         return(s->session_timeout);
209         }
210  
211 +#ifndef OPENSSL_NO_TLSEXT
212 +int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len, 
213 +       STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
214 +{
215 +       if (s == NULL) return(0);
216 +       s->tls_session_secret_cb = tls_session_secret_cb;
217 +       s->tls_session_secret_cb_arg = arg;
218 +       return(1);
219 +}
220 +
221 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len)
222 +{
223 +       if(s->version >= TLS1_VERSION)
224 +       {
225 +               if(s->tls_extension)
226 +               {
227 +                       OPENSSL_free(s->tls_extension);
228 +                       s->tls_extension = NULL;
229 +               }
230 +
231 +               s->tls_extension = OPENSSL_malloc(sizeof(TLS_EXTENSION) + ext_len);
232 +               if(!s->tls_extension)
233 +               {
234 +                       SSLerr(SSL_F_SSL_SET_HELLO_EXTENSION, ERR_R_MALLOC_FAILURE);
235 +                       return 0;
236 +               }
237 +
238 +               s->tls_extension->type = ext_type;
239 +
240 +               if(ext_data)
241 +               {
242 +                       s->tls_extension->length = ext_len;
243 +                       s->tls_extension->data = s->tls_extension + 1;
244 +                       memcpy(s->tls_extension->data, ext_data, ext_len);
245 +               } else {
246 +                       s->tls_extension->length = 0;
247 +                       s->tls_extension->data = NULL;
248 +               }
249 +
250 +               return 1;
251 +       }
252 +
253 +       return 0;
254 +}
255 +#endif /* OPENSSL_NO_TLSEXT */
256 +
257  typedef struct timeout_param_st
258         {
259         SSL_CTX *ctx;
260 diff -upr openssl-0.9.8h.orig/ssl/t1_lib.c openssl-0.9.8h/ssl/t1_lib.c
261 --- openssl-0.9.8h.orig/ssl/t1_lib.c    2008-05-28 10:26:33.000000000 +0300
262 +++ openssl-0.9.8h/ssl/t1_lib.c 2008-05-28 18:49:34.000000000 +0300
263 @@ -106,6 +106,12 @@ int tls1_new(SSL *s)
264  
265  void tls1_free(SSL *s)
266         {
267 +#ifndef OPENSSL_NO_TLSEXT
268 +       if(s->tls_extension)
269 +       {
270 +               OPENSSL_free(s->tls_extension);
271 +       }
272 +#endif
273         ssl3_free(s);
274         }
275  
276 @@ -175,8 +181,24 @@ unsigned char *ssl_add_clienthello_tlsex
277                 int ticklen;
278                 if (s->session && s->session->tlsext_tick)
279                         ticklen = s->session->tlsext_ticklen;
280 +               else if (s->session && s->tls_extension &&
281 +                       s->tls_extension->type == TLSEXT_TYPE_session_ticket &&
282 +                       s->tls_extension->data)
283 +               {
284 +                       ticklen = s->tls_extension->length;
285 +                       s->session->tlsext_tick = OPENSSL_malloc(ticklen);
286 +                       if (!s->session->tlsext_tick)
287 +                               return NULL;
288 +                       memcpy(s->session->tlsext_tick, s->tls_extension->data,
289 +                              ticklen);
290 +                       s->session->tlsext_ticklen = ticklen;
291 +               }
292                 else
293                         ticklen = 0;
294 +               if (ticklen == 0 && s->tls_extension &&
295 +                   s->tls_extension->type == TLSEXT_TYPE_session_ticket &&
296 +                   s->tls_extension->data == NULL)
297 +                       goto skip_ext;
298                 /* Check for enough room 2 for extension type, 2 for len
299                  * rest for ticket
300                  */
301 @@ -190,6 +212,7 @@ unsigned char *ssl_add_clienthello_tlsex
302                         ret += ticklen;
303                         }
304                 }
305 +               skip_ext:
306  
307         if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
308                 {
309 @@ -774,6 +797,8 @@ int tls1_process_ticket(SSL *s, unsigned
310                                 s->tlsext_ticket_expected = 1;
311                                 return 0;       /* Cache miss */
312                                 }
313 +                       if (s->tls_session_secret_cb)
314 +                               return 0;
315                         return tls_decrypt_ticket(s, p, size, session_id, len,
316                                                                         ret);
317                         }
318 diff -upr openssl-0.9.8h.orig/ssl/tls1.h openssl-0.9.8h/ssl/tls1.h
319 --- openssl-0.9.8h.orig/ssl/tls1.h      2008-04-30 19:11:33.000000000 +0300
320 +++ openssl-0.9.8h/ssl/tls1.h   2008-05-28 18:49:34.000000000 +0300
321 @@ -398,6 +398,14 @@ SSL_CTX_callback_ctrl(ssl,SSL_CTRL_SET_T
322  #define TLS_MD_MASTER_SECRET_CONST    "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74"  /*master secret*/
323  #endif
324  
325 +/* TLS extension struct */
326 +struct tls_extension_st
327 +{
328 +       unsigned short type;
329 +       unsigned short length;
330 +       void *data;
331 +};
332 +
333  #ifdef  __cplusplus
334  }
335  #endif
336 diff -upr openssl-0.9.8h.orig/util/ssleay.num openssl-0.9.8h/util/ssleay.num
337 --- openssl-0.9.8h.orig/util/ssleay.num 2007-08-13 01:31:16.000000000 +0300
338 +++ openssl-0.9.8h/util/ssleay.num      2008-05-28 18:49:34.000000000 +0300
339 @@ -241,3 +241,5 @@ SSL_CTX_sess_get_remove_cb              
340  SSL_set_SSL_CTX                         290    EXIST::FUNCTION:
341  SSL_get_servername                      291    EXIST::FUNCTION:TLSEXT
342  SSL_get_servername_type                 292    EXIST::FUNCTION:TLSEXT
343 +SSL_set_hello_extension                        305     EXIST::FUNCTION:TLSEXT
344 +SSL_set_session_secret_cb              306     EXIST::FUNCTION:TLSEXT