Merge branch 'vendor/BMAKE'
[dragonfly.git] / contrib / hostapd / patches / openssl-0.9.8d-tls-extensions.patch
1 This patch is adding support for TLS hello extensions and externally
2 generated pre-shared key material to OpenSSL 0.9.8d. This is
3 based on the patch from Alexey Kobozev <akobozev@cisco.com>
4 (sent to openssl-dev mailing list on Tue, 07 Jun 2005 15:40:58 +0300).
5
6
7
8 diff -uprN openssl-0.9.8d.orig/include/openssl/ssl.h openssl-0.9.8d/include/openssl/ssl.h
9 --- openssl-0.9.8d.orig/include/openssl/ssl.h   2006-06-14 06:52:49.000000000 -0700
10 +++ openssl-0.9.8d/include/openssl/ssl.h        2006-12-10 08:20:02.000000000 -0800
11 @@ -345,6 +345,7 @@ extern "C" {
12   * 'struct ssl_st *' function parameters used to prototype callbacks
13   * in SSL_CTX. */
14  typedef struct ssl_st *ssl_crock_st;
15 +typedef struct tls_extension_st TLS_EXTENSION;
16  
17  /* used to hold info on the particular ciphers used */
18  typedef struct ssl_cipher_st
19 @@ -366,6 +367,8 @@ DECLARE_STACK_OF(SSL_CIPHER)
20  typedef struct ssl_st SSL;
21  typedef struct ssl_ctx_st SSL_CTX;
22  
23 +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);
24 +
25  /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
26  typedef struct ssl_method_st
27         {
28 @@ -973,6 +976,15 @@ struct ssl_st
29         int first_packet;
30         int client_version;     /* what was passed, used for
31                                  * SSLv3/TLS rollback check */
32 +
33 +       /* TLS externsions */
34 +       TLS_EXTENSION *tls_extension;
35 +       int (*tls_extension_cb)(SSL *s, TLS_EXTENSION *tls_ext, void *arg);
36 +       void *tls_extension_cb_arg;
37 +
38 +       /* TLS pre-shared secret session resumption */
39 +       tls_session_secret_cb_fn tls_session_secret_cb;
40 +       void *tls_session_secret_cb_arg;
41         };
42  
43  #ifdef __cplusplus
44 @@ -1538,6 +1550,13 @@ void *SSL_COMP_get_compression_methods(v
45  int SSL_COMP_add_compression_method(int id,void *cm);
46  #endif
47  
48 +/* TLS extensions functions */
49 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len);
50 +int SSL_set_hello_extension_cb(SSL *s, int (*cb)(SSL *, TLS_EXTENSION *, void *), void *arg);
51 +
52 +/* Pre-shared secret session resumption functions */
53 +int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
54 +
55  /* BEGIN ERROR CODES */
56  /* The following lines are auto generated by the script mkerr.pl. Any changes
57   * made after this point may be overwritten when the script is next run.
58 @@ -1719,6 +1738,7 @@ void ERR_load_SSL_strings(void);
59  #define SSL_F_TLS1_ENC                                  210
60  #define SSL_F_TLS1_SETUP_KEY_BLOCK                      211
61  #define SSL_F_WRITE_PENDING                             212
62 +#define SSL_F_SSL_SET_HELLO_EXTENSION   213
63  
64  /* Reason codes. */
65  #define SSL_R_APP_DATA_IN_HANDSHAKE                     100
66 diff -uprN openssl-0.9.8d.orig/include/openssl/tls1.h openssl-0.9.8d/include/openssl/tls1.h
67 --- openssl-0.9.8d.orig/include/openssl/tls1.h  2006-06-14 10:52:01.000000000 -0700
68 +++ openssl-0.9.8d/include/openssl/tls1.h       2006-12-10 08:20:02.000000000 -0800
69 @@ -296,6 +296,14 @@ extern "C" {
70  #define TLS_MD_MASTER_SECRET_CONST    "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74"  /*master secret*/
71  #endif
72  
73 +/* TLS extension struct */
74 +struct tls_extension_st
75 +{
76 +       unsigned short type;
77 +       unsigned short length;
78 +       void *data;
79 +};
80 +
81  #ifdef  __cplusplus
82  }
83  #endif
84 diff -uprN openssl-0.9.8d.orig/ssl/Makefile openssl-0.9.8d/ssl/Makefile
85 --- openssl-0.9.8d.orig/ssl/Makefile    2006-02-03 17:49:35.000000000 -0800
86 +++ openssl-0.9.8d/ssl/Makefile 2006-12-10 08:20:02.000000000 -0800
87 @@ -24,7 +24,7 @@ LIBSRC=       \
88         s2_meth.c   s2_srvr.c s2_clnt.c  s2_lib.c  s2_enc.c s2_pkt.c \
89         s3_meth.c   s3_srvr.c s3_clnt.c  s3_lib.c  s3_enc.c s3_pkt.c s3_both.c \
90         s23_meth.c s23_srvr.c s23_clnt.c s23_lib.c          s23_pkt.c \
91 -       t1_meth.c   t1_srvr.c t1_clnt.c  t1_lib.c  t1_enc.c \
92 +       t1_meth.c   t1_srvr.c t1_clnt.c  t1_lib.c  t1_enc.c                    t1_ext.c \
93         d1_meth.c   d1_srvr.c d1_clnt.c  d1_lib.c  d1_pkt.c \
94         d1_both.c d1_enc.c \
95         ssl_lib.c ssl_err2.c ssl_cert.c ssl_sess.c \
96 @@ -35,7 +35,7 @@ LIBOBJ= \
97         s2_meth.o  s2_srvr.o  s2_clnt.o  s2_lib.o  s2_enc.o s2_pkt.o \
98         s3_meth.o  s3_srvr.o  s3_clnt.o  s3_lib.o  s3_enc.o s3_pkt.o s3_both.o \
99         s23_meth.o s23_srvr.o s23_clnt.o s23_lib.o          s23_pkt.o \
100 -       t1_meth.o   t1_srvr.o t1_clnt.o  t1_lib.o  t1_enc.o \
101 +       t1_meth.o   t1_srvr.o t1_clnt.o  t1_lib.o  t1_enc.o                    t1_ext.o \
102         d1_meth.o   d1_srvr.o d1_clnt.o  d1_lib.o  d1_pkt.o \
103         d1_both.o d1_enc.o \
104         ssl_lib.o ssl_err2.o ssl_cert.o ssl_sess.o \
105 @@ -968,3 +968,4 @@ t1_srvr.o: ../include/openssl/ssl23.h ..
106  t1_srvr.o: ../include/openssl/stack.h ../include/openssl/symhacks.h
107  t1_srvr.o: ../include/openssl/tls1.h ../include/openssl/x509.h
108  t1_srvr.o: ../include/openssl/x509_vfy.h ssl_locl.h t1_srvr.c
109 +t1_ext.o: t1_ext.c ssl_locl.h
110 diff -uprN openssl-0.9.8d.orig/ssl/s3_clnt.c openssl-0.9.8d/ssl/s3_clnt.c
111 --- openssl-0.9.8d.orig/ssl/s3_clnt.c   2005-12-12 23:41:46.000000000 -0800
112 +++ openssl-0.9.8d/ssl/s3_clnt.c        2006-12-10 08:20:02.000000000 -0800
113 @@ -601,6 +601,20 @@ int ssl3_client_hello(SSL *s)
114  #endif
115                 *(p++)=0; /* Add the NULL method */
116                 
117 +               /* send client hello extensions if any */
118 +               if (s->version >= TLS1_VERSION && s->tls_extension)
119 +               {
120 +                       // set the total extensions length
121 +                       s2n(s->tls_extension->length + 4, p);
122 +
123 +                       // put the extensions with type and length
124 +                       s2n(s->tls_extension->type, p);
125 +                       s2n(s->tls_extension->length, p);
126 +                       
127 +                       memcpy(p, s->tls_extension->data, s->tls_extension->length);
128 +                       p+=s->tls_extension->length;
129 +               }
130 +
131                 l=(p-d);
132                 d=buf;
133                 *(d++)=SSL3_MT_CLIENT_HELLO;
134 @@ -623,7 +637,7 @@ int ssl3_get_server_hello(SSL *s)
135         STACK_OF(SSL_CIPHER) *sk;
136         SSL_CIPHER *c;
137         unsigned char *p,*d;
138 -       int i,al,ok;
139 +       int i,al,ok,pre_shared;
140         unsigned int j;
141         long n;
142  #ifndef OPENSSL_NO_COMP
143 @@ -690,7 +704,24 @@ int ssl3_get_server_hello(SSL *s)
144                 goto f_err;
145                 }
146  
147 -       if (j != 0 && j == s->session->session_id_length
148 +       /* check if we want to resume the session based on external pre-shared secret */
149 +       pre_shared = 0;
150 +       if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
151 +       {
152 +               SSL_CIPHER *pref_cipher=NULL;
153 +               s->session->master_key_length=sizeof(s->session->master_key);
154 +               if (s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
155 +                       NULL, &pref_cipher, s->tls_session_secret_cb_arg))
156 +               {
157 +                       s->hit=1;
158 +                       s->session->cipher=pref_cipher ? pref_cipher : ssl_get_cipher_by_char(s,p+j);
159 +                       s->session->session_id_length = j;
160 +                       memcpy(s->session->session_id, p, j);
161 +                       pre_shared = 1;
162 +               }
163 +       }
164 +
165 +       if ((pre_shared || j != 0) && j == s->session->session_id_length
166             && memcmp(p,s->session->session_id,j) == 0)
167             {
168             if(s->sid_ctx_length != s->session->sid_ctx_length
169 diff -uprN openssl-0.9.8d.orig/ssl/s3_srvr.c openssl-0.9.8d/ssl/s3_srvr.c
170 --- openssl-0.9.8d.orig/ssl/s3_srvr.c   2006-09-28 04:29:03.000000000 -0700
171 +++ openssl-0.9.8d/ssl/s3_srvr.c        2006-12-10 08:20:02.000000000 -0800
172 @@ -943,6 +943,75 @@ int ssl3_get_client_hello(SSL *s)
173                 }
174  #endif
175  
176 +       /* Check for TLS client hello extension here */
177 +       if (p < (d+n) && s->version >= TLS1_VERSION)
178 +       {
179 +               if (s->tls_extension_cb)
180 +               {
181 +                       TLS_EXTENSION tls_ext;
182 +                       unsigned short ext_total_len;
183 +                       
184 +                       n2s(p, ext_total_len);
185 +                       n2s(p, tls_ext.type);
186 +                       n2s(p, tls_ext.length);
187 +
188 +                       // sanity check in TLS extension len
189 +                       if (tls_ext.length > (d+n) - p)
190 +                       {
191 +                               // just cut the lenth to packet border
192 +                               tls_ext.length = (d+n) - p;
193 +                       }
194 +
195 +                       tls_ext.data = p;
196 +
197 +                       // returns an alert code or 0
198 +                       al = s->tls_extension_cb(s, &tls_ext, s->tls_extension_cb_arg);
199 +                       if (al != 0)
200 +                       {
201 +                               SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_PEER_ERROR);
202 +                               goto f_err;
203 +                       }
204 +               }
205 +       }
206 +
207 +       /* Check if we want to use external pre-shared secret for this handshake */
208 +       /* for not reused session only */
209 +       if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
210 +       {
211 +               SSL_CIPHER *pref_cipher=NULL;
212 +
213 +               s->session->master_key_length=sizeof(s->session->master_key);
214 +               if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length, 
215 +                       ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
216 +               {
217 +                       s->hit=1;
218 +                       s->session->ciphers=ciphers;
219 +                       s->session->verify_result=X509_V_OK;
220 +                       
221 +                       ciphers=NULL;
222 +                       
223 +                       /* check if some cipher was preferred by call back */
224 +                       pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
225 +                       if (pref_cipher == NULL)
226 +                               {
227 +                               al=SSL_AD_HANDSHAKE_FAILURE;
228 +                               SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
229 +                               goto f_err;
230 +                               }
231 +
232 +                       s->session->cipher=pref_cipher;
233 +
234 +                       if (s->cipher_list)
235 +                               sk_SSL_CIPHER_free(s->cipher_list);
236 +
237 +                       if (s->cipher_list_by_id)
238 +                               sk_SSL_CIPHER_free(s->cipher_list_by_id);
239 +
240 +                       s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
241 +                       s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
242 +               }
243 +       }
244 +
245         /* Given s->session->ciphers and SSL_get_ciphers, we must
246          * pick a cipher */
247  
248 diff -uprN openssl-0.9.8d.orig/ssl/ssl.h openssl-0.9.8d/ssl/ssl.h
249 --- openssl-0.9.8d.orig/ssl/ssl.h       2006-06-14 06:52:49.000000000 -0700
250 +++ openssl-0.9.8d/ssl/ssl.h    2006-12-10 08:20:02.000000000 -0800
251 @@ -345,6 +345,7 @@ extern "C" {
252   * 'struct ssl_st *' function parameters used to prototype callbacks
253   * in SSL_CTX. */
254  typedef struct ssl_st *ssl_crock_st;
255 +typedef struct tls_extension_st TLS_EXTENSION;
256  
257  /* used to hold info on the particular ciphers used */
258  typedef struct ssl_cipher_st
259 @@ -366,6 +367,8 @@ DECLARE_STACK_OF(SSL_CIPHER)
260  typedef struct ssl_st SSL;
261  typedef struct ssl_ctx_st SSL_CTX;
262  
263 +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);
264 +
265  /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
266  typedef struct ssl_method_st
267         {
268 @@ -973,6 +976,15 @@ struct ssl_st
269         int first_packet;
270         int client_version;     /* what was passed, used for
271                                  * SSLv3/TLS rollback check */
272 +
273 +       /* TLS externsions */
274 +       TLS_EXTENSION *tls_extension;
275 +       int (*tls_extension_cb)(SSL *s, TLS_EXTENSION *tls_ext, void *arg);
276 +       void *tls_extension_cb_arg;
277 +
278 +       /* TLS pre-shared secret session resumption */
279 +       tls_session_secret_cb_fn tls_session_secret_cb;
280 +       void *tls_session_secret_cb_arg;
281         };
282  
283  #ifdef __cplusplus
284 @@ -1538,6 +1550,13 @@ void *SSL_COMP_get_compression_methods(v
285  int SSL_COMP_add_compression_method(int id,void *cm);
286  #endif
287  
288 +/* TLS extensions functions */
289 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len);
290 +int SSL_set_hello_extension_cb(SSL *s, int (*cb)(SSL *, TLS_EXTENSION *, void *), void *arg);
291 +
292 +/* Pre-shared secret session resumption functions */
293 +int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
294 +
295  /* BEGIN ERROR CODES */
296  /* The following lines are auto generated by the script mkerr.pl. Any changes
297   * made after this point may be overwritten when the script is next run.
298 @@ -1719,6 +1738,7 @@ void ERR_load_SSL_strings(void);
299  #define SSL_F_TLS1_ENC                                  210
300  #define SSL_F_TLS1_SETUP_KEY_BLOCK                      211
301  #define SSL_F_WRITE_PENDING                             212
302 +#define SSL_F_SSL_SET_HELLO_EXTENSION   213
303  
304  /* Reason codes. */
305  #define SSL_R_APP_DATA_IN_HANDSHAKE                     100
306 diff -uprN openssl-0.9.8d.orig/ssl/ssl_err.c openssl-0.9.8d/ssl/ssl_err.c
307 --- openssl-0.9.8d.orig/ssl/ssl_err.c   2006-01-08 13:52:46.000000000 -0800
308 +++ openssl-0.9.8d/ssl/ssl_err.c        2006-12-10 08:20:02.000000000 -0800
309 @@ -242,6 +242,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
310  {ERR_FUNC(SSL_F_TLS1_ENC),     "TLS1_ENC"},
311  {ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK), "TLS1_SETUP_KEY_BLOCK"},
312  {ERR_FUNC(SSL_F_WRITE_PENDING),        "WRITE_PENDING"},
313 +{ERR_FUNC(SSL_F_SSL_SET_HELLO_EXTENSION), "SSL_set_hello_extension"},
314  {0,NULL}
315         };
316  
317 diff -uprN openssl-0.9.8d.orig/ssl/ssl_sess.c openssl-0.9.8d/ssl/ssl_sess.c
318 --- openssl-0.9.8d.orig/ssl/ssl_sess.c  2005-12-30 15:51:57.000000000 -0800
319 +++ openssl-0.9.8d/ssl/ssl_sess.c       2006-12-10 08:20:02.000000000 -0800
320 @@ -656,6 +656,15 @@ long SSL_CTX_get_timeout(const SSL_CTX *
321         return(s->session_timeout);
322         }
323  
324 +int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len, 
325 +       STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
326 +{
327 +       if (s == NULL) return(0);
328 +       s->tls_session_secret_cb = tls_session_secret_cb;
329 +       s->tls_session_secret_cb_arg = arg;
330 +       return(1);
331 +}
332 +
333  typedef struct timeout_param_st
334         {
335         SSL_CTX *ctx;
336 diff -uprN openssl-0.9.8d.orig/ssl/t1_ext.c openssl-0.9.8d/ssl/t1_ext.c
337 --- openssl-0.9.8d.orig/ssl/t1_ext.c    1969-12-31 16:00:00.000000000 -0800
338 +++ openssl-0.9.8d/ssl/t1_ext.c 2006-12-10 08:20:02.000000000 -0800
339 @@ -0,0 +1,48 @@
340 +
341 +#include <stdio.h>
342 +#include "ssl_locl.h"
343 +
344 +
345 +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len)
346 +{
347 +       if(s->version >= TLS1_VERSION)
348 +       {
349 +               if(s->tls_extension)
350 +               {
351 +                       OPENSSL_free(s->tls_extension);
352 +                       s->tls_extension = NULL;
353 +               }
354 +
355 +               if(ext_data)
356 +               {
357 +                       s->tls_extension = OPENSSL_malloc(sizeof(TLS_EXTENSION) + ext_len);
358 +                       if(!s->tls_extension)
359 +                       {
360 +                               SSLerr(SSL_F_SSL_SET_HELLO_EXTENSION, ERR_R_MALLOC_FAILURE);
361 +                               return 0;
362 +                       }
363 +
364 +                       s->tls_extension->type = ext_type;
365 +                       s->tls_extension->length = ext_len;
366 +                       s->tls_extension->data = s->tls_extension + 1;
367 +                       memcpy(s->tls_extension->data, ext_data, ext_len);
368 +               }
369 +
370 +               return 1;
371 +       }
372 +
373 +       return 0;
374 +}
375 +
376 +int SSL_set_hello_extension_cb(SSL *s, int (*cb)(SSL *, TLS_EXTENSION *, void *), void *arg)
377 +{
378 +       if(s->version >= TLS1_VERSION)
379 +       {
380 +               s->tls_extension_cb = cb;
381 +               s->tls_extension_cb_arg = arg;
382 +
383 +               return 1;
384 +       }
385 +
386 +       return 0;
387 +}
388 diff -uprN openssl-0.9.8d.orig/ssl/t1_lib.c openssl-0.9.8d/ssl/t1_lib.c
389 --- openssl-0.9.8d.orig/ssl/t1_lib.c    2005-08-05 16:52:07.000000000 -0700
390 +++ openssl-0.9.8d/ssl/t1_lib.c 2006-12-10 08:20:02.000000000 -0800
391 @@ -97,6 +97,10 @@ int tls1_new(SSL *s)
392  
393  void tls1_free(SSL *s)
394         {
395 +       if(s->tls_extension)
396 +       {
397 +               OPENSSL_free(s->tls_extension);
398 +       }
399         ssl3_free(s);
400         }
401  
402 diff -uprN openssl-0.9.8d.orig/ssl/tls1.h openssl-0.9.8d/ssl/tls1.h
403 --- openssl-0.9.8d.orig/ssl/tls1.h      2006-06-14 10:52:01.000000000 -0700
404 +++ openssl-0.9.8d/ssl/tls1.h   2006-12-10 08:20:02.000000000 -0800
405 @@ -296,6 +296,14 @@ extern "C" {
406  #define TLS_MD_MASTER_SECRET_CONST    "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74"  /*master secret*/
407  #endif
408  
409 +/* TLS extension struct */
410 +struct tls_extension_st
411 +{
412 +       unsigned short type;
413 +       unsigned short length;
414 +       void *data;
415 +};
416 +
417  #ifdef  __cplusplus
418  }
419  #endif
420 diff -uprN openssl-0.9.8d.orig/util/ssleay.num openssl-0.9.8d/util/ssleay.num
421 --- openssl-0.9.8d.orig/util/ssleay.num 2005-05-08 17:22:02.000000000 -0700
422 +++ openssl-0.9.8d/util/ssleay.num      2006-12-10 08:20:02.000000000 -0800
423 @@ -226,3 +226,6 @@ DTLSv1_server_method                    
424  SSL_COMP_get_compression_methods        276    EXIST:!VMS:FUNCTION:COMP
425  SSL_COMP_get_compress_methods           276    EXIST:VMS:FUNCTION:COMP
426  SSL_SESSION_get_id                      277    EXIST::FUNCTION:
427 +SSL_set_hello_extension                        278     EXIST::FUNCTION:
428 +SSL_set_hello_extension_cb             279     EXIST::FUNCTION:
429 +SSL_set_session_secret_cb              280     EXIST::FUNCTION: