2a089d4d26a04e103d5a45cdc3bbbccb9d0fcf5c
[dragonfly.git] / contrib / hostapd-0.5.8 / eap_tls_common.c
1 /*
2  * hostapd / EAP-TLS/PEAP/TTLS common functions
3  * Copyright (c) 2004-2006, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "hostapd.h"
18 #include "common.h"
19 #include "eap_i.h"
20 #include "eap_tls_common.h"
21 #include "sha1.h"
22 #include "tls.h"
23
24
25 int eap_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
26                      int verify_peer)
27 {
28         data->eap = sm;
29         data->phase2 = sm->init_phase2;
30
31         data->conn = tls_connection_init(sm->ssl_ctx);
32         if (data->conn == NULL) {
33                 wpa_printf(MSG_INFO, "SSL: Failed to initialize new TLS "
34                            "connection");
35                 return -1;
36         }
37
38         if (tls_connection_set_verify(sm->ssl_ctx, data->conn, verify_peer)) {
39                 wpa_printf(MSG_INFO, "SSL: Failed to configure verification "
40                            "of TLS peer certificate");
41                 tls_connection_deinit(sm->ssl_ctx, data->conn);
42                 data->conn = NULL;
43                 return -1;
44         }
45
46         /* TODO: make this configurable */
47         data->tls_out_limit = 1398;
48         if (data->phase2) {
49                 /* Limit the fragment size in the inner TLS authentication
50                  * since the outer authentication with EAP-PEAP does not yet
51                  * support fragmentation */
52                 if (data->tls_out_limit > 100)
53                         data->tls_out_limit -= 100;
54         }
55         return 0;
56 }
57
58
59 void eap_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data)
60 {
61         tls_connection_deinit(sm->ssl_ctx, data->conn);
62         free(data->tls_in);
63         free(data->tls_out);
64 }
65
66
67 u8 * eap_tls_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
68                         char *label, size_t len)
69 {
70         struct tls_keys keys;
71         u8 *rnd = NULL, *out;
72
73         out = malloc(len);
74         if (out == NULL)
75                 return NULL;
76
77         if (tls_connection_prf(sm->ssl_ctx, data->conn, label, 0, out, len) ==
78             0)
79                 return out;
80
81         if (tls_connection_get_keys(sm->ssl_ctx, data->conn, &keys))
82                 goto fail;
83
84         if (keys.client_random == NULL || keys.server_random == NULL ||
85             keys.master_key == NULL)
86                 goto fail;
87
88         rnd = malloc(keys.client_random_len + keys.server_random_len);
89         if (rnd == NULL)
90                 goto fail;
91         memcpy(rnd, keys.client_random, keys.client_random_len);
92         memcpy(rnd + keys.client_random_len, keys.server_random,
93                keys.server_random_len);
94
95         if (tls_prf(keys.master_key, keys.master_key_len,
96                     label, rnd, keys.client_random_len +
97                     keys.server_random_len, out, len))
98                 goto fail;
99
100         free(rnd);
101         return out;
102
103 fail:
104         free(out);
105         free(rnd);
106         return NULL;
107 }
108
109
110 int eap_tls_data_reassemble(struct eap_sm *sm, struct eap_ssl_data *data,
111                             u8 **in_data, size_t *in_len)
112 {
113         u8 *buf;
114
115         if (data->tls_in_left > *in_len || data->tls_in) {
116                 if (data->tls_in_len + *in_len > 65536) {
117                         /* Limit length to avoid rogue peers from causing large
118                          * memory allocations. */
119                         free(data->tls_in);
120                         data->tls_in = NULL;
121                         data->tls_in_len = 0;
122                         wpa_printf(MSG_INFO, "SSL: Too long TLS fragment (size"
123                                    " over 64 kB)");
124                         return -1;
125                 }
126                 buf = realloc(data->tls_in, data->tls_in_len + *in_len);
127                 if (buf == NULL) {
128                         free(data->tls_in);
129                         data->tls_in = NULL;
130                         data->tls_in_len = 0;
131                         wpa_printf(MSG_INFO, "SSL: Could not allocate memory "
132                                    "for TLS data");
133                         return -1;
134                 }
135                 memcpy(buf + data->tls_in_len, *in_data, *in_len);
136                 data->tls_in = buf;
137                 data->tls_in_len += *in_len;
138                 if (*in_len > data->tls_in_left) {
139                         wpa_printf(MSG_INFO, "SSL: more data than TLS message "
140                                    "length indicated");
141                         data->tls_in_left = 0;
142                         return -1;
143                 }
144                 data->tls_in_left -= *in_len;
145                 if (data->tls_in_left > 0) {
146                         wpa_printf(MSG_DEBUG, "SSL: Need %lu bytes more input "
147                                    "data", (unsigned long) data->tls_in_left);
148                         return 1;
149                 }
150
151                 *in_data = data->tls_in;
152                 *in_len = data->tls_in_len;
153         } else
154                 data->tls_in_left = 0;
155
156         return 0;
157 }
158
159
160 int eap_tls_process_helper(struct eap_sm *sm, struct eap_ssl_data *data,
161                            u8 *in_data, size_t in_len)
162 {
163         WPA_ASSERT(data->tls_out_len == 0 || in_len == 0);
164
165         if (data->tls_out_len == 0) {
166                 /* No more data to send out - expect to receive more data from
167                  * the peer. */
168                 int res = eap_tls_data_reassemble(sm, data, &in_data, &in_len);
169                 if (res < 0 || res == 1) {
170                         wpa_printf(MSG_DEBUG, "SSL: data reassembly failed");
171                         return res;
172                 }
173                 /* Full TLS message reassembled - continue handshake processing
174                  */
175                 if (data->tls_out) {
176                         /* This should not happen.. */
177                         wpa_printf(MSG_INFO, "SSL: eap_tls_process_helper - "
178                                    "pending tls_out data even though "
179                                    "tls_out_len = 0");
180                         free(data->tls_out);
181                         WPA_ASSERT(data->tls_out == NULL);
182                 }
183                 data->tls_out = tls_connection_server_handshake(
184                         sm->ssl_ctx, data->conn, in_data, in_len,
185                         &data->tls_out_len);
186
187                 /* Clear reassembled input data (if the buffer was needed). */
188                 data->tls_in_left = data->tls_in_total = data->tls_in_len = 0;
189                 free(data->tls_in);
190                 data->tls_in = NULL;
191         }
192
193         if (data->tls_out == NULL) {
194                 wpa_printf(MSG_DEBUG, "SSL: failed to generate output data");
195                 data->tls_out_len = 0;
196                 return -1;
197         }
198         if (data->tls_out_len == 0) {
199                 /* TLS negotiation should now be complete since all other cases
200                  * needing more that should have been catched above based on
201                  * the TLS Message Length field. */
202                 wpa_printf(MSG_DEBUG, "SSL: No data to be sent out");
203                 free(data->tls_out);
204                 data->tls_out = NULL;
205
206                 if (tls_connection_get_read_alerts(sm->ssl_ctx, data->conn)) {
207                         wpa_printf(MSG_DEBUG, "SSL: Remote end sent a fatal "
208                                    "alert - abort handshake");
209                         return -1;
210                 }
211
212                 return 1;
213         }
214
215         wpa_printf(MSG_DEBUG, "SSL: %lu bytes left to be sent out (of total "
216                    "%lu bytes)",
217                    (unsigned long) data->tls_out_len - data->tls_out_pos,
218                    (unsigned long) data->tls_out_len);
219
220         return 0;
221 }
222
223
224 int eap_tls_buildReq_helper(struct eap_sm *sm, struct eap_ssl_data *data,
225                             int eap_type, int peap_version, u8 id,
226                             u8 **out_data, size_t *out_len)
227 {
228         size_t len;
229         u8 *pos, *flags;
230         struct eap_hdr *req;
231
232         *out_len = 0;
233
234         req = malloc(sizeof(struct eap_hdr) + 2 + 4 + data->tls_out_limit);
235         if (req == NULL) {
236                 *out_data = NULL;
237                 return -1;
238         }
239         req->code = EAP_CODE_REQUEST;
240         req->identifier = id;
241         pos = (u8 *) (req + 1);
242         *pos++ = eap_type;
243         flags = pos++;
244         *flags = peap_version;
245         if (data->tls_out_pos == 0 &&
246             data->tls_out_len > data->tls_out_limit) {
247                 *flags |= EAP_TLS_FLAGS_LENGTH_INCLUDED;
248                 *pos++ = (data->tls_out_len >> 24) & 0xff;
249                 *pos++ = (data->tls_out_len >> 16) & 0xff;
250                 *pos++ = (data->tls_out_len >> 8) & 0xff;
251                 *pos++ = data->tls_out_len & 0xff;
252         }
253
254         len = data->tls_out_len - data->tls_out_pos;
255         if (len > data->tls_out_limit) {
256                 *flags |= EAP_TLS_FLAGS_MORE_FRAGMENTS;
257                 len = data->tls_out_limit;
258                 wpa_printf(MSG_DEBUG, "SSL: sending %lu bytes, more fragments "
259                            "will follow", (unsigned long) len);
260         }
261         memcpy(pos, &data->tls_out[data->tls_out_pos], len);
262         data->tls_out_pos += len;
263         *out_len = (pos - (u8 *) req) + len;
264         req->length = htons(*out_len);
265         *out_data = (u8 *) req;
266
267         if (!(*flags & EAP_TLS_FLAGS_MORE_FRAGMENTS)) {
268                 data->tls_out_len = 0;
269                 data->tls_out_pos = 0;
270                 free(data->tls_out);
271                 data->tls_out = NULL;
272         }
273
274         return 0;
275 }
276
277
278 u8 * eap_tls_build_ack(size_t *reqDataLen, u8 id, int eap_type,
279                        int peap_version)
280 {
281         struct eap_hdr *req;
282         u8 *pos;
283
284         *reqDataLen = sizeof(struct eap_hdr) + 2;
285         req = malloc(*reqDataLen);
286         if (req == NULL)
287                 return NULL;
288         wpa_printf(MSG_DEBUG, "SSL: Building ACK");
289         req->code = EAP_CODE_REQUEST;
290         req->identifier = id;
291         req->length = htons(*reqDataLen);
292         pos = (u8 *) (req + 1);
293         *pos++ = eap_type; /* Type */
294         *pos = peap_version; /* Flags */
295         return (u8 *) req;
296 }