Disconnect hostapd from building in base
[dragonfly.git] / contrib / hostapd / patches / openssl-0.9.8i-tls-extensions.patch
CommitLineData
a875087d
JL
1This patch adds support for TLS SessionTicket extension (RFC 5077) for
2the parts used by EAP-FAST (RFC 4851).
3
4This 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
7OpenSSL 0.9.8i does not enable TLS extension support by default, so it
8will need to be enabled by adding enable-tlsext to config script
9command line.
10
11
12Index: openssl-0.9.8i/ssl/s3_clnt.c
13===================================================================
14--- openssl-0.9.8i.orig/ssl/s3_clnt.c 2008-06-16 19:56:41.000000000 +0300
15+++ openssl-0.9.8i/ssl/s3_clnt.c 2008-11-23 20:39:40.000000000 +0200
16@@ -759,6 +759,21 @@
17 goto f_err;
18 }
19
20+#ifndef OPENSSL_NO_TLSEXT
21+ /* check if we want to resume the session based on external pre-shared secret */
22+ if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
23+ {
24+ SSL_CIPHER *pref_cipher=NULL;
25+ s->session->master_key_length=sizeof(s->session->master_key);
26+ if (s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
27+ NULL, &pref_cipher, s->tls_session_secret_cb_arg))
28+ {
29+ s->session->cipher=pref_cipher ?
30+ pref_cipher : ssl_get_cipher_by_char(s,p+j);
31+ }
32+ }
33+#endif /* OPENSSL_NO_TLSEXT */
34+
35 if (j != 0 && j == s->session->session_id_length
36 && memcmp(p,s->session->session_id,j) == 0)
37 {
38@@ -2701,11 +2716,8 @@
39 {
40 int ok;
41 long n;
42- /* If we have no ticket or session ID is non-zero length (a match of
43- * a non-zero session length would never reach here) it cannot be a
44- * resumed session.
45- */
46- if (!s->session->tlsext_tick || s->session->session_id_length)
47+ /* If we have no ticket it cannot be a resumed session. */
48+ if (!s->session->tlsext_tick)
49 return 1;
50 /* this function is called when we really expect a Certificate
51 * message, so permit appropriate message length */
52Index: openssl-0.9.8i/ssl/s3_srvr.c
53===================================================================
54--- openssl-0.9.8i.orig/ssl/s3_srvr.c 2008-09-14 21:16:09.000000000 +0300
55+++ openssl-0.9.8i/ssl/s3_srvr.c 2008-11-23 20:37:40.000000000 +0200
56@@ -959,6 +959,59 @@
57 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
58 goto err;
59 }
60+
61+ /* Check if we want to use external pre-shared secret for this
62+ * handshake for not reused session only. We need to generate
63+ * server_random before calling tls_session_secret_cb in order to allow
64+ * SessionTicket processing to use it in key derivation. */
65+ {
66+ unsigned long Time;
67+ unsigned char *pos;
68+ Time=(unsigned long)time(NULL); /* Time */
69+ pos=s->s3->server_random;
70+ l2n(Time,pos);
71+ if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0)
72+ {
73+ al=SSL_AD_INTERNAL_ERROR;
74+ goto f_err;
75+ }
76+ }
77+
78+ if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
79+ {
80+ SSL_CIPHER *pref_cipher=NULL;
81+
82+ s->session->master_key_length=sizeof(s->session->master_key);
83+ if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
84+ ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
85+ {
86+ s->hit=1;
87+ s->session->ciphers=ciphers;
88+ s->session->verify_result=X509_V_OK;
89+
90+ ciphers=NULL;
91+
92+ /* check if some cipher was preferred by call back */
93+ pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
94+ if (pref_cipher == NULL)
95+ {
96+ al=SSL_AD_HANDSHAKE_FAILURE;
97+ SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
98+ goto f_err;
99+ }
100+
101+ s->session->cipher=pref_cipher;
102+
103+ if (s->cipher_list)
104+ sk_SSL_CIPHER_free(s->cipher_list);
105+
106+ if (s->cipher_list_by_id)
107+ sk_SSL_CIPHER_free(s->cipher_list_by_id);
108+
109+ s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
110+ s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
111+ }
112+ }
113 #endif
114 /* Worst case, we will use the NULL compression, but if we have other
115 * options, we will now look for them. We have i-1 compression
116@@ -1097,16 +1150,22 @@
117 unsigned char *buf;
118 unsigned char *p,*d;
119 int i,sl;
120- unsigned long l,Time;
121+ unsigned long l;
122+#ifdef OPENSSL_NO_TLSEXT
123+ unsigned long Time;
124+#endif
125
126 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
127 {
128 buf=(unsigned char *)s->init_buf->data;
129+#ifdef OPENSSL_NO_TLSEXT
130 p=s->s3->server_random;
131+ /* Generate server_random if it was not needed previously */
132 Time=(unsigned long)time(NULL); /* Time */
133 l2n(Time,p);
134 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
135 return -1;
136+#endif
137 /* Do the message type and length last */
138 d=p= &(buf[4]);
139
140Index: openssl-0.9.8i/ssl/ssl_err.c
141===================================================================
142--- openssl-0.9.8i.orig/ssl/ssl_err.c 2008-08-13 22:44:44.000000000 +0300
143+++ openssl-0.9.8i/ssl/ssl_err.c 2008-11-23 20:33:43.000000000 +0200
144@@ -253,6 +253,7 @@
145 {ERR_FUNC(SSL_F_TLS1_ENC), "TLS1_ENC"},
146 {ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK), "TLS1_SETUP_KEY_BLOCK"},
147 {ERR_FUNC(SSL_F_WRITE_PENDING), "WRITE_PENDING"},
148+{ERR_FUNC(SSL_F_SSL_SET_SESSION_TICKET_EXT), "SSL_set_session_ticket_ext"},
149 {0,NULL}
150 };
151
152Index: openssl-0.9.8i/ssl/ssl.h
153===================================================================
154--- openssl-0.9.8i.orig/ssl/ssl.h 2008-08-13 22:44:44.000000000 +0300
155+++ openssl-0.9.8i/ssl/ssl.h 2008-11-23 20:35:41.000000000 +0200
156@@ -344,6 +344,7 @@
157 * 'struct ssl_st *' function parameters used to prototype callbacks
158 * in SSL_CTX. */
159 typedef struct ssl_st *ssl_crock_st;
160+typedef struct tls_session_ticket_ext_st TLS_SESSION_TICKET_EXT;
161
162 /* used to hold info on the particular ciphers used */
163 typedef struct ssl_cipher_st
164@@ -362,6 +363,9 @@
165
166 DECLARE_STACK_OF(SSL_CIPHER)
167
168+typedef int (*tls_session_ticket_ext_cb_fn)(SSL *s, const unsigned char *data, int len, void *arg);
169+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);
170+
171 /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
172 typedef struct ssl_method_st
173 {
174@@ -1034,6 +1038,18 @@
175
176 /* RFC4507 session ticket expected to be received or sent */
177 int tlsext_ticket_expected;
178+
179+ /* TLS Session Ticket extension override */
180+ TLS_SESSION_TICKET_EXT *tlsext_session_ticket;
181+
182+ /* TLS Session Ticket extension callback */
183+ tls_session_ticket_ext_cb_fn tls_session_ticket_ext_cb;
184+ void *tls_session_ticket_ext_cb_arg;
185+
186+ /* TLS pre-shared secret session resumption */
187+ tls_session_secret_cb_fn tls_session_secret_cb;
188+ void *tls_session_secret_cb_arg;
189+
190 SSL_CTX * initial_ctx; /* initial ctx, used to store sessions */
191 #define session_ctx initial_ctx
192 #else
193@@ -1632,6 +1648,15 @@
194 int SSL_COMP_add_compression_method(int id,void *cm);
195 #endif
196
197+/* TLS extensions functions */
198+int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len);
199+
200+int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
201+ void *arg);
202+
203+/* Pre-shared secret session resumption functions */
204+int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
205+
206 /* BEGIN ERROR CODES */
207 /* The following lines are auto generated by the script mkerr.pl. Any changes
208 * made after this point may be overwritten when the script is next run.
209@@ -1824,6 +1849,7 @@
210 #define SSL_F_TLS1_ENC 210
211 #define SSL_F_TLS1_SETUP_KEY_BLOCK 211
212 #define SSL_F_WRITE_PENDING 212
213+#define SSL_F_SSL_SET_SESSION_TICKET_EXT 213
214
215 /* Reason codes. */
216 #define SSL_R_APP_DATA_IN_HANDSHAKE 100
217Index: openssl-0.9.8i/ssl/ssl_sess.c
218===================================================================
219--- openssl-0.9.8i.orig/ssl/ssl_sess.c 2008-06-04 21:35:27.000000000 +0300
220+++ openssl-0.9.8i/ssl/ssl_sess.c 2008-11-23 20:32:24.000000000 +0200
221@@ -707,6 +707,61 @@
222 return(s->session_timeout);
223 }
224
225+#ifndef OPENSSL_NO_TLSEXT
226+int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
227+ STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
228+ {
229+ if (s == NULL) return(0);
230+ s->tls_session_secret_cb = tls_session_secret_cb;
231+ s->tls_session_secret_cb_arg = arg;
232+ return(1);
233+ }
234+
235+int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
236+ void *arg)
237+ {
238+ if (s == NULL) return(0);
239+ s->tls_session_ticket_ext_cb = cb;
240+ s->tls_session_ticket_ext_cb_arg = arg;
241+ return(1);
242+ }
243+
244+int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
245+ {
246+ if (s->version >= TLS1_VERSION)
247+ {
248+ if (s->tlsext_session_ticket)
249+ {
250+ OPENSSL_free(s->tlsext_session_ticket);
251+ s->tlsext_session_ticket = NULL;
252+ }
253+
254+ s->tlsext_session_ticket = OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
255+ if (!s->tlsext_session_ticket)
256+ {
257+ SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT, ERR_R_MALLOC_FAILURE);
258+ return 0;
259+ }
260+
261+ if (ext_data)
262+ {
263+ s->tlsext_session_ticket->length = ext_len;
264+ s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1;
265+ memcpy(s->tlsext_session_ticket->data, ext_data, ext_len);
266+ }
267+ else
268+ {
269+ s->tlsext_session_ticket->length = 0;
270+ s->tlsext_session_ticket->data = NULL;
271+ }
272+
273+ return 1;
274+ }
275+
276+ return 0;
277+ }
278+#endif /* OPENSSL_NO_TLSEXT */
279+
280 typedef struct timeout_param_st
281 {
282 SSL_CTX *ctx;
283Index: openssl-0.9.8i/ssl/t1_lib.c
284===================================================================
285--- openssl-0.9.8i.orig/ssl/t1_lib.c 2008-09-04 01:13:04.000000000 +0300
286+++ openssl-0.9.8i/ssl/t1_lib.c 2008-11-23 20:31:20.000000000 +0200
287@@ -106,6 +106,12 @@
288
289 void tls1_free(SSL *s)
290 {
291+#ifndef OPENSSL_NO_TLSEXT
292+ if (s->tlsext_session_ticket)
293+ {
294+ OPENSSL_free(s->tlsext_session_ticket);
295+ }
296+#endif
297 ssl3_free(s);
298 }
299
300@@ -175,8 +181,23 @@
301 int ticklen;
302 if (s->session && s->session->tlsext_tick)
303 ticklen = s->session->tlsext_ticklen;
304+ else if (s->session && s->tlsext_session_ticket &&
305+ s->tlsext_session_ticket->data)
306+ {
307+ ticklen = s->tlsext_session_ticket->length;
308+ s->session->tlsext_tick = OPENSSL_malloc(ticklen);
309+ if (!s->session->tlsext_tick)
310+ return NULL;
311+ memcpy(s->session->tlsext_tick,
312+ s->tlsext_session_ticket->data,
313+ ticklen);
314+ s->session->tlsext_ticklen = ticklen;
315+ }
316 else
317 ticklen = 0;
318+ if (ticklen == 0 && s->tlsext_session_ticket &&
319+ s->tlsext_session_ticket->data == NULL)
320+ goto skip_ext;
321 /* Check for enough room 2 for extension type, 2 for len
322 * rest for ticket
323 */
324@@ -190,6 +211,7 @@
325 ret += ticklen;
326 }
327 }
328+ skip_ext:
329
330 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
331 {
332@@ -407,6 +429,15 @@
333 }
334
335 }
336+ else if (type == TLSEXT_TYPE_session_ticket)
337+ {
338+ if (s->tls_session_ticket_ext_cb &&
339+ !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
340+ {
341+ *al = TLS1_AD_INTERNAL_ERROR;
342+ return 0;
343+ }
344+ }
345 else if (type == TLSEXT_TYPE_status_request
346 && s->ctx->tlsext_status_cb)
347 {
348@@ -553,6 +584,12 @@
349 }
350 else if (type == TLSEXT_TYPE_session_ticket)
351 {
352+ if (s->tls_session_ticket_ext_cb &&
353+ !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
354+ {
355+ *al = TLS1_AD_INTERNAL_ERROR;
356+ return 0;
357+ }
358 if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
359 || (size > 0))
360 {
361@@ -776,6 +813,15 @@
362 s->tlsext_ticket_expected = 1;
363 return 0; /* Cache miss */
364 }
365+ if (s->tls_session_secret_cb)
366+ {
367+ /* Indicate cache miss here and instead of
368+ * generating the session from ticket now,
369+ * trigger abbreviated handshake based on
370+ * external mechanism to calculate the master
371+ * secret later. */
372+ return 0;
373+ }
374 return tls_decrypt_ticket(s, p, size, session_id, len,
375 ret);
376 }
377Index: openssl-0.9.8i/ssl/tls1.h
378===================================================================
379--- openssl-0.9.8i.orig/ssl/tls1.h 2008-04-30 19:11:33.000000000 +0300
380+++ openssl-0.9.8i/ssl/tls1.h 2008-11-23 20:22:38.000000000 +0200
381@@ -398,6 +398,13 @@
382 #define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74" /*master secret*/
383 #endif
384
385+/* TLS extension struct */
386+struct tls_session_ticket_ext_st
387+ {
388+ unsigned short length;
389+ void *data;
390+ };
391+
392 #ifdef __cplusplus
393 }
394 #endif
395Index: openssl-0.9.8i/util/ssleay.num
396===================================================================
397--- openssl-0.9.8i.orig/util/ssleay.num 2008-06-05 13:57:21.000000000 +0300
398+++ openssl-0.9.8i/util/ssleay.num 2008-11-23 20:22:05.000000000 +0200
399@@ -242,3 +242,5 @@
400 SSL_get_servername 291 EXIST::FUNCTION:TLSEXT
401 SSL_get_servername_type 292 EXIST::FUNCTION:TLSEXT
402 SSL_CTX_set_client_cert_engine 293 EXIST::FUNCTION:ENGINE
403+SSL_set_session_ticket_ext 306 EXIST::FUNCTION:TLSEXT
404+SSL_set_session_secret_cb 307 EXIST::FUNCTION:TLSEXT