1e28a4053SRui Paulo /*
2e28a4053SRui Paulo * hostapd / EAP-TTLS (RFC 5281)
3f05cddf9SRui Paulo * Copyright (c) 2004-2011, Jouni Malinen <j@w1.fi>
4e28a4053SRui Paulo *
5f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo * See README for more details.
7e28a4053SRui Paulo */
8e28a4053SRui Paulo
9e28a4053SRui Paulo #include "includes.h"
10e28a4053SRui Paulo
11e28a4053SRui Paulo #include "common.h"
12e28a4053SRui Paulo #include "crypto/ms_funcs.h"
13e28a4053SRui Paulo #include "crypto/sha1.h"
14e28a4053SRui Paulo #include "crypto/tls.h"
15e28a4053SRui Paulo #include "eap_server/eap_i.h"
16e28a4053SRui Paulo #include "eap_server/eap_tls_common.h"
17e28a4053SRui Paulo #include "eap_common/chap.h"
18e28a4053SRui Paulo #include "eap_common/eap_ttls.h"
19e28a4053SRui Paulo
20e28a4053SRui Paulo
21f05cddf9SRui Paulo #define EAP_TTLS_VERSION 0
22e28a4053SRui Paulo
23e28a4053SRui Paulo
24e28a4053SRui Paulo static void eap_ttls_reset(struct eap_sm *sm, void *priv);
25e28a4053SRui Paulo
26e28a4053SRui Paulo
27e28a4053SRui Paulo struct eap_ttls_data {
28e28a4053SRui Paulo struct eap_ssl_data ssl;
29e28a4053SRui Paulo enum {
30e28a4053SRui Paulo START, PHASE1, PHASE2_START, PHASE2_METHOD,
31f05cddf9SRui Paulo PHASE2_MSCHAPV2_RESP, SUCCESS, FAILURE
32e28a4053SRui Paulo } state;
33e28a4053SRui Paulo
34e28a4053SRui Paulo int ttls_version;
35e28a4053SRui Paulo const struct eap_method *phase2_method;
36e28a4053SRui Paulo void *phase2_priv;
37e28a4053SRui Paulo int mschapv2_resp_ok;
38e28a4053SRui Paulo u8 mschapv2_auth_response[20];
39e28a4053SRui Paulo u8 mschapv2_ident;
40e28a4053SRui Paulo struct wpabuf *pending_phase2_eap_resp;
41e28a4053SRui Paulo int tnc_started;
42e28a4053SRui Paulo };
43e28a4053SRui Paulo
44e28a4053SRui Paulo
eap_ttls_state_txt(int state)45e28a4053SRui Paulo static const char * eap_ttls_state_txt(int state)
46e28a4053SRui Paulo {
47e28a4053SRui Paulo switch (state) {
48e28a4053SRui Paulo case START:
49e28a4053SRui Paulo return "START";
50e28a4053SRui Paulo case PHASE1:
51e28a4053SRui Paulo return "PHASE1";
52e28a4053SRui Paulo case PHASE2_START:
53e28a4053SRui Paulo return "PHASE2_START";
54e28a4053SRui Paulo case PHASE2_METHOD:
55e28a4053SRui Paulo return "PHASE2_METHOD";
56e28a4053SRui Paulo case PHASE2_MSCHAPV2_RESP:
57e28a4053SRui Paulo return "PHASE2_MSCHAPV2_RESP";
58e28a4053SRui Paulo case SUCCESS:
59e28a4053SRui Paulo return "SUCCESS";
60e28a4053SRui Paulo case FAILURE:
61e28a4053SRui Paulo return "FAILURE";
62e28a4053SRui Paulo default:
63e28a4053SRui Paulo return "Unknown?!";
64e28a4053SRui Paulo }
65e28a4053SRui Paulo }
66e28a4053SRui Paulo
67e28a4053SRui Paulo
eap_ttls_state(struct eap_ttls_data * data,int state)68e28a4053SRui Paulo static void eap_ttls_state(struct eap_ttls_data *data, int state)
69e28a4053SRui Paulo {
70e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS: %s -> %s",
71e28a4053SRui Paulo eap_ttls_state_txt(data->state),
72e28a4053SRui Paulo eap_ttls_state_txt(state));
73e28a4053SRui Paulo data->state = state;
74325151a3SRui Paulo if (state == FAILURE)
75325151a3SRui Paulo tls_connection_remove_session(data->ssl.conn);
76325151a3SRui Paulo }
77325151a3SRui Paulo
78325151a3SRui Paulo
eap_ttls_valid_session(struct eap_sm * sm,struct eap_ttls_data * data)79325151a3SRui Paulo static void eap_ttls_valid_session(struct eap_sm *sm,
80325151a3SRui Paulo struct eap_ttls_data *data)
81325151a3SRui Paulo {
82325151a3SRui Paulo struct wpabuf *buf;
83325151a3SRui Paulo
84c1d255d3SCy Schubert if (!sm->cfg->tls_session_lifetime)
85325151a3SRui Paulo return;
86325151a3SRui Paulo
87325151a3SRui Paulo buf = wpabuf_alloc(1 + 1 + sm->identity_len);
88325151a3SRui Paulo if (!buf)
89325151a3SRui Paulo return;
90325151a3SRui Paulo wpabuf_put_u8(buf, EAP_TYPE_TTLS);
91325151a3SRui Paulo if (sm->identity) {
92325151a3SRui Paulo u8 id_len;
93325151a3SRui Paulo
94325151a3SRui Paulo if (sm->identity_len <= 255)
95325151a3SRui Paulo id_len = sm->identity_len;
96325151a3SRui Paulo else
97325151a3SRui Paulo id_len = 255;
98325151a3SRui Paulo wpabuf_put_u8(buf, id_len);
99325151a3SRui Paulo wpabuf_put_data(buf, sm->identity, id_len);
100325151a3SRui Paulo } else {
101325151a3SRui Paulo wpabuf_put_u8(buf, 0);
102325151a3SRui Paulo }
103325151a3SRui Paulo tls_connection_set_success_data(data->ssl.conn, buf);
104e28a4053SRui Paulo }
105e28a4053SRui Paulo
106e28a4053SRui Paulo
eap_ttls_avp_hdr(u8 * avphdr,u32 avp_code,u32 vendor_id,int mandatory,size_t len)107e28a4053SRui Paulo static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
108e28a4053SRui Paulo int mandatory, size_t len)
109e28a4053SRui Paulo {
110e28a4053SRui Paulo struct ttls_avp_vendor *avp;
111e28a4053SRui Paulo u8 flags;
112e28a4053SRui Paulo size_t hdrlen;
113e28a4053SRui Paulo
114e28a4053SRui Paulo avp = (struct ttls_avp_vendor *) avphdr;
115e28a4053SRui Paulo flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
116e28a4053SRui Paulo if (vendor_id) {
117e28a4053SRui Paulo flags |= AVP_FLAGS_VENDOR;
118e28a4053SRui Paulo hdrlen = sizeof(*avp);
119e28a4053SRui Paulo avp->vendor_id = host_to_be32(vendor_id);
120e28a4053SRui Paulo } else {
121e28a4053SRui Paulo hdrlen = sizeof(struct ttls_avp);
122e28a4053SRui Paulo }
123e28a4053SRui Paulo
124e28a4053SRui Paulo avp->avp_code = host_to_be32(avp_code);
125f05cddf9SRui Paulo avp->avp_length = host_to_be32(((u32) flags << 24) |
126f05cddf9SRui Paulo ((u32) (hdrlen + len)));
127e28a4053SRui Paulo
128e28a4053SRui Paulo return avphdr + hdrlen;
129e28a4053SRui Paulo }
130e28a4053SRui Paulo
131e28a4053SRui Paulo
eap_ttls_avp_encapsulate(struct wpabuf * resp,u32 avp_code,int mandatory)132e28a4053SRui Paulo static struct wpabuf * eap_ttls_avp_encapsulate(struct wpabuf *resp,
133e28a4053SRui Paulo u32 avp_code, int mandatory)
134e28a4053SRui Paulo {
135e28a4053SRui Paulo struct wpabuf *avp;
136e28a4053SRui Paulo u8 *pos;
137e28a4053SRui Paulo
138e28a4053SRui Paulo avp = wpabuf_alloc(sizeof(struct ttls_avp) + wpabuf_len(resp) + 4);
139e28a4053SRui Paulo if (avp == NULL) {
140e28a4053SRui Paulo wpabuf_free(resp);
141e28a4053SRui Paulo return NULL;
142e28a4053SRui Paulo }
143e28a4053SRui Paulo
144e28a4053SRui Paulo pos = eap_ttls_avp_hdr(wpabuf_mhead(avp), avp_code, 0, mandatory,
145e28a4053SRui Paulo wpabuf_len(resp));
146e28a4053SRui Paulo os_memcpy(pos, wpabuf_head(resp), wpabuf_len(resp));
147e28a4053SRui Paulo pos += wpabuf_len(resp);
148e28a4053SRui Paulo AVP_PAD((const u8 *) wpabuf_head(avp), pos);
149e28a4053SRui Paulo wpabuf_free(resp);
150e28a4053SRui Paulo wpabuf_put(avp, pos - (u8 *) wpabuf_head(avp));
151e28a4053SRui Paulo return avp;
152e28a4053SRui Paulo }
153e28a4053SRui Paulo
154e28a4053SRui Paulo
155e28a4053SRui Paulo struct eap_ttls_avp {
156e28a4053SRui Paulo /* Note: eap is allocated memory; caller is responsible for freeing
157e28a4053SRui Paulo * it. All the other pointers are pointing to the packet data, i.e.,
158e28a4053SRui Paulo * they must not be freed separately. */
159e28a4053SRui Paulo u8 *eap;
160e28a4053SRui Paulo size_t eap_len;
161e28a4053SRui Paulo u8 *user_name;
162e28a4053SRui Paulo size_t user_name_len;
163e28a4053SRui Paulo u8 *user_password;
164e28a4053SRui Paulo size_t user_password_len;
165e28a4053SRui Paulo u8 *chap_challenge;
166e28a4053SRui Paulo size_t chap_challenge_len;
167e28a4053SRui Paulo u8 *chap_password;
168e28a4053SRui Paulo size_t chap_password_len;
169e28a4053SRui Paulo u8 *mschap_challenge;
170e28a4053SRui Paulo size_t mschap_challenge_len;
171e28a4053SRui Paulo u8 *mschap_response;
172e28a4053SRui Paulo size_t mschap_response_len;
173e28a4053SRui Paulo u8 *mschap2_response;
174e28a4053SRui Paulo size_t mschap2_response_len;
175e28a4053SRui Paulo };
176e28a4053SRui Paulo
177e28a4053SRui Paulo
eap_ttls_avp_parse(struct wpabuf * buf,struct eap_ttls_avp * parse)178e28a4053SRui Paulo static int eap_ttls_avp_parse(struct wpabuf *buf, struct eap_ttls_avp *parse)
179e28a4053SRui Paulo {
180e28a4053SRui Paulo struct ttls_avp *avp;
181e28a4053SRui Paulo u8 *pos;
182e28a4053SRui Paulo int left;
183e28a4053SRui Paulo
184e28a4053SRui Paulo pos = wpabuf_mhead(buf);
185e28a4053SRui Paulo left = wpabuf_len(buf);
186e28a4053SRui Paulo os_memset(parse, 0, sizeof(*parse));
187e28a4053SRui Paulo
188e28a4053SRui Paulo while (left > 0) {
189e28a4053SRui Paulo u32 avp_code, avp_length, vendor_id = 0;
190e28a4053SRui Paulo u8 avp_flags, *dpos;
191e28a4053SRui Paulo size_t pad, dlen;
192e28a4053SRui Paulo avp = (struct ttls_avp *) pos;
193e28a4053SRui Paulo avp_code = be_to_host32(avp->avp_code);
194e28a4053SRui Paulo avp_length = be_to_host32(avp->avp_length);
195e28a4053SRui Paulo avp_flags = (avp_length >> 24) & 0xff;
196e28a4053SRui Paulo avp_length &= 0xffffff;
197e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
198e28a4053SRui Paulo "length=%d", (int) avp_code, avp_flags,
199e28a4053SRui Paulo (int) avp_length);
200e28a4053SRui Paulo if ((int) avp_length > left) {
201e28a4053SRui Paulo wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
202e28a4053SRui Paulo "(len=%d, left=%d) - dropped",
203e28a4053SRui Paulo (int) avp_length, left);
204e28a4053SRui Paulo goto fail;
205e28a4053SRui Paulo }
206e28a4053SRui Paulo if (avp_length < sizeof(*avp)) {
207e28a4053SRui Paulo wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid AVP length "
208e28a4053SRui Paulo "%d", avp_length);
209e28a4053SRui Paulo goto fail;
210e28a4053SRui Paulo }
211e28a4053SRui Paulo dpos = (u8 *) (avp + 1);
212e28a4053SRui Paulo dlen = avp_length - sizeof(*avp);
213e28a4053SRui Paulo if (avp_flags & AVP_FLAGS_VENDOR) {
214e28a4053SRui Paulo if (dlen < 4) {
215e28a4053SRui Paulo wpa_printf(MSG_WARNING, "EAP-TTLS: vendor AVP "
216e28a4053SRui Paulo "underflow");
217e28a4053SRui Paulo goto fail;
218e28a4053SRui Paulo }
219e28a4053SRui Paulo vendor_id = be_to_host32(* (be32 *) dpos);
220e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
221e28a4053SRui Paulo (int) vendor_id);
222e28a4053SRui Paulo dpos += 4;
223e28a4053SRui Paulo dlen -= 4;
224e28a4053SRui Paulo }
225e28a4053SRui Paulo
226e28a4053SRui Paulo wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
227e28a4053SRui Paulo
228e28a4053SRui Paulo if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
229e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
230e28a4053SRui Paulo if (parse->eap == NULL) {
23185732ac8SCy Schubert parse->eap = os_memdup(dpos, dlen);
232e28a4053SRui Paulo if (parse->eap == NULL) {
233e28a4053SRui Paulo wpa_printf(MSG_WARNING, "EAP-TTLS: "
234e28a4053SRui Paulo "failed to allocate memory "
235e28a4053SRui Paulo "for Phase 2 EAP data");
236e28a4053SRui Paulo goto fail;
237e28a4053SRui Paulo }
238e28a4053SRui Paulo parse->eap_len = dlen;
239e28a4053SRui Paulo } else {
240e28a4053SRui Paulo u8 *neweap = os_realloc(parse->eap,
241e28a4053SRui Paulo parse->eap_len + dlen);
242e28a4053SRui Paulo if (neweap == NULL) {
243e28a4053SRui Paulo wpa_printf(MSG_WARNING, "EAP-TTLS: "
244e28a4053SRui Paulo "failed to allocate memory "
245e28a4053SRui Paulo "for Phase 2 EAP data");
246e28a4053SRui Paulo goto fail;
247e28a4053SRui Paulo }
248e28a4053SRui Paulo os_memcpy(neweap + parse->eap_len, dpos, dlen);
249e28a4053SRui Paulo parse->eap = neweap;
250e28a4053SRui Paulo parse->eap_len += dlen;
251e28a4053SRui Paulo }
252e28a4053SRui Paulo } else if (vendor_id == 0 &&
253e28a4053SRui Paulo avp_code == RADIUS_ATTR_USER_NAME) {
254e28a4053SRui Paulo wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: User-Name",
255e28a4053SRui Paulo dpos, dlen);
256e28a4053SRui Paulo parse->user_name = dpos;
257e28a4053SRui Paulo parse->user_name_len = dlen;
258e28a4053SRui Paulo } else if (vendor_id == 0 &&
259e28a4053SRui Paulo avp_code == RADIUS_ATTR_USER_PASSWORD) {
260e28a4053SRui Paulo u8 *password = dpos;
261e28a4053SRui Paulo size_t password_len = dlen;
262e28a4053SRui Paulo while (password_len > 0 &&
263e28a4053SRui Paulo password[password_len - 1] == '\0') {
264e28a4053SRui Paulo password_len--;
265e28a4053SRui Paulo }
266e28a4053SRui Paulo wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: "
267e28a4053SRui Paulo "User-Password (PAP)",
268e28a4053SRui Paulo password, password_len);
269e28a4053SRui Paulo parse->user_password = password;
270e28a4053SRui Paulo parse->user_password_len = password_len;
271e28a4053SRui Paulo } else if (vendor_id == 0 &&
272e28a4053SRui Paulo avp_code == RADIUS_ATTR_CHAP_CHALLENGE) {
273e28a4053SRui Paulo wpa_hexdump(MSG_DEBUG,
274e28a4053SRui Paulo "EAP-TTLS: CHAP-Challenge (CHAP)",
275e28a4053SRui Paulo dpos, dlen);
276e28a4053SRui Paulo parse->chap_challenge = dpos;
277e28a4053SRui Paulo parse->chap_challenge_len = dlen;
278e28a4053SRui Paulo } else if (vendor_id == 0 &&
279e28a4053SRui Paulo avp_code == RADIUS_ATTR_CHAP_PASSWORD) {
280e28a4053SRui Paulo wpa_hexdump(MSG_DEBUG,
281e28a4053SRui Paulo "EAP-TTLS: CHAP-Password (CHAP)",
282e28a4053SRui Paulo dpos, dlen);
283e28a4053SRui Paulo parse->chap_password = dpos;
284e28a4053SRui Paulo parse->chap_password_len = dlen;
285e28a4053SRui Paulo } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
286e28a4053SRui Paulo avp_code == RADIUS_ATTR_MS_CHAP_CHALLENGE) {
287e28a4053SRui Paulo wpa_hexdump(MSG_DEBUG,
288e28a4053SRui Paulo "EAP-TTLS: MS-CHAP-Challenge",
289e28a4053SRui Paulo dpos, dlen);
290e28a4053SRui Paulo parse->mschap_challenge = dpos;
291e28a4053SRui Paulo parse->mschap_challenge_len = dlen;
292e28a4053SRui Paulo } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
293e28a4053SRui Paulo avp_code == RADIUS_ATTR_MS_CHAP_RESPONSE) {
294e28a4053SRui Paulo wpa_hexdump(MSG_DEBUG,
295e28a4053SRui Paulo "EAP-TTLS: MS-CHAP-Response (MSCHAP)",
296e28a4053SRui Paulo dpos, dlen);
297e28a4053SRui Paulo parse->mschap_response = dpos;
298e28a4053SRui Paulo parse->mschap_response_len = dlen;
299e28a4053SRui Paulo } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
300e28a4053SRui Paulo avp_code == RADIUS_ATTR_MS_CHAP2_RESPONSE) {
301e28a4053SRui Paulo wpa_hexdump(MSG_DEBUG,
302e28a4053SRui Paulo "EAP-TTLS: MS-CHAP2-Response (MSCHAPV2)",
303e28a4053SRui Paulo dpos, dlen);
304e28a4053SRui Paulo parse->mschap2_response = dpos;
305e28a4053SRui Paulo parse->mschap2_response_len = dlen;
306e28a4053SRui Paulo } else if (avp_flags & AVP_FLAGS_MANDATORY) {
307e28a4053SRui Paulo wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported "
308e28a4053SRui Paulo "mandatory AVP code %d vendor_id %d - "
309e28a4053SRui Paulo "dropped", (int) avp_code, (int) vendor_id);
310e28a4053SRui Paulo goto fail;
311e28a4053SRui Paulo } else {
312e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported "
313e28a4053SRui Paulo "AVP code %d vendor_id %d",
314e28a4053SRui Paulo (int) avp_code, (int) vendor_id);
315e28a4053SRui Paulo }
316e28a4053SRui Paulo
317e28a4053SRui Paulo pad = (4 - (avp_length & 3)) & 3;
318e28a4053SRui Paulo pos += avp_length + pad;
319e28a4053SRui Paulo left -= avp_length + pad;
320e28a4053SRui Paulo }
321e28a4053SRui Paulo
322e28a4053SRui Paulo return 0;
323e28a4053SRui Paulo
324e28a4053SRui Paulo fail:
325e28a4053SRui Paulo os_free(parse->eap);
326e28a4053SRui Paulo parse->eap = NULL;
327e28a4053SRui Paulo return -1;
328e28a4053SRui Paulo }
329e28a4053SRui Paulo
330e28a4053SRui Paulo
eap_ttls_implicit_challenge(struct eap_sm * sm,struct eap_ttls_data * data,size_t len)331e28a4053SRui Paulo static u8 * eap_ttls_implicit_challenge(struct eap_sm *sm,
332e28a4053SRui Paulo struct eap_ttls_data *data, size_t len)
333e28a4053SRui Paulo {
334f05cddf9SRui Paulo return eap_server_tls_derive_key(sm, &data->ssl, "ttls challenge",
3354bc52338SCy Schubert NULL, 0, len);
336e28a4053SRui Paulo }
337e28a4053SRui Paulo
338e28a4053SRui Paulo
eap_ttls_init(struct eap_sm * sm)339e28a4053SRui Paulo static void * eap_ttls_init(struct eap_sm *sm)
340e28a4053SRui Paulo {
341e28a4053SRui Paulo struct eap_ttls_data *data;
342e28a4053SRui Paulo
343e28a4053SRui Paulo data = os_zalloc(sizeof(*data));
344e28a4053SRui Paulo if (data == NULL)
345e28a4053SRui Paulo return NULL;
346e28a4053SRui Paulo data->ttls_version = EAP_TTLS_VERSION;
347e28a4053SRui Paulo data->state = START;
348e28a4053SRui Paulo
349325151a3SRui Paulo if (eap_server_tls_ssl_init(sm, &data->ssl, 0, EAP_TYPE_TTLS)) {
350e28a4053SRui Paulo wpa_printf(MSG_INFO, "EAP-TTLS: Failed to initialize SSL.");
351e28a4053SRui Paulo eap_ttls_reset(sm, data);
352e28a4053SRui Paulo return NULL;
353e28a4053SRui Paulo }
354e28a4053SRui Paulo
355e28a4053SRui Paulo return data;
356e28a4053SRui Paulo }
357e28a4053SRui Paulo
358e28a4053SRui Paulo
eap_ttls_reset(struct eap_sm * sm,void * priv)359e28a4053SRui Paulo static void eap_ttls_reset(struct eap_sm *sm, void *priv)
360e28a4053SRui Paulo {
361e28a4053SRui Paulo struct eap_ttls_data *data = priv;
362e28a4053SRui Paulo if (data == NULL)
363e28a4053SRui Paulo return;
364e28a4053SRui Paulo if (data->phase2_priv && data->phase2_method)
365e28a4053SRui Paulo data->phase2_method->reset(sm, data->phase2_priv);
366e28a4053SRui Paulo eap_server_tls_ssl_deinit(sm, &data->ssl);
367e28a4053SRui Paulo wpabuf_free(data->pending_phase2_eap_resp);
3685b9c547cSRui Paulo bin_clear_free(data, sizeof(*data));
369e28a4053SRui Paulo }
370e28a4053SRui Paulo
371e28a4053SRui Paulo
eap_ttls_build_start(struct eap_sm * sm,struct eap_ttls_data * data,u8 id)372e28a4053SRui Paulo static struct wpabuf * eap_ttls_build_start(struct eap_sm *sm,
373e28a4053SRui Paulo struct eap_ttls_data *data, u8 id)
374e28a4053SRui Paulo {
375e28a4053SRui Paulo struct wpabuf *req;
376e28a4053SRui Paulo
377e28a4053SRui Paulo req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TTLS, 1,
378e28a4053SRui Paulo EAP_CODE_REQUEST, id);
379e28a4053SRui Paulo if (req == NULL) {
380e28a4053SRui Paulo wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to allocate memory for"
381e28a4053SRui Paulo " request");
382e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
383e28a4053SRui Paulo return NULL;
384e28a4053SRui Paulo }
385e28a4053SRui Paulo
386e28a4053SRui Paulo wpabuf_put_u8(req, EAP_TLS_FLAGS_START | data->ttls_version);
387e28a4053SRui Paulo
388e28a4053SRui Paulo eap_ttls_state(data, PHASE1);
389e28a4053SRui Paulo
390e28a4053SRui Paulo return req;
391e28a4053SRui Paulo }
392e28a4053SRui Paulo
393e28a4053SRui Paulo
eap_ttls_build_phase2_eap_req(struct eap_sm * sm,struct eap_ttls_data * data,u8 id)394e28a4053SRui Paulo static struct wpabuf * eap_ttls_build_phase2_eap_req(
395e28a4053SRui Paulo struct eap_sm *sm, struct eap_ttls_data *data, u8 id)
396e28a4053SRui Paulo {
397e28a4053SRui Paulo struct wpabuf *buf, *encr_req;
398e28a4053SRui Paulo
399e28a4053SRui Paulo
400e28a4053SRui Paulo buf = data->phase2_method->buildReq(sm, data->phase2_priv, id);
401e28a4053SRui Paulo if (buf == NULL)
402e28a4053SRui Paulo return NULL;
403e28a4053SRui Paulo
404e28a4053SRui Paulo wpa_hexdump_buf_key(MSG_DEBUG,
405e28a4053SRui Paulo "EAP-TTLS/EAP: Encapsulate Phase 2 data", buf);
406e28a4053SRui Paulo
407e28a4053SRui Paulo buf = eap_ttls_avp_encapsulate(buf, RADIUS_ATTR_EAP_MESSAGE, 1);
408e28a4053SRui Paulo if (buf == NULL) {
409e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: Failed to encapsulate "
410e28a4053SRui Paulo "packet");
411e28a4053SRui Paulo return NULL;
412e28a4053SRui Paulo }
413e28a4053SRui Paulo
414e28a4053SRui Paulo wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS/EAP: Encrypt encapsulated "
415e28a4053SRui Paulo "Phase 2 data", buf);
416e28a4053SRui Paulo
417e28a4053SRui Paulo encr_req = eap_server_tls_encrypt(sm, &data->ssl, buf);
418e28a4053SRui Paulo wpabuf_free(buf);
419e28a4053SRui Paulo
420e28a4053SRui Paulo return encr_req;
421e28a4053SRui Paulo }
422e28a4053SRui Paulo
423e28a4053SRui Paulo
eap_ttls_build_phase2_mschapv2(struct eap_sm * sm,struct eap_ttls_data * data)424e28a4053SRui Paulo static struct wpabuf * eap_ttls_build_phase2_mschapv2(
425e28a4053SRui Paulo struct eap_sm *sm, struct eap_ttls_data *data)
426e28a4053SRui Paulo {
427e28a4053SRui Paulo struct wpabuf *encr_req, msgbuf;
428e28a4053SRui Paulo u8 *req, *pos, *end;
429e28a4053SRui Paulo int ret;
430e28a4053SRui Paulo
431e28a4053SRui Paulo pos = req = os_malloc(100);
432e28a4053SRui Paulo if (req == NULL)
433e28a4053SRui Paulo return NULL;
434e28a4053SRui Paulo end = req + 100;
435e28a4053SRui Paulo
436e28a4053SRui Paulo if (data->mschapv2_resp_ok) {
437e28a4053SRui Paulo pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_SUCCESS,
438e28a4053SRui Paulo RADIUS_VENDOR_ID_MICROSOFT, 1, 43);
439e28a4053SRui Paulo *pos++ = data->mschapv2_ident;
440e28a4053SRui Paulo ret = os_snprintf((char *) pos, end - pos, "S=");
4415b9c547cSRui Paulo if (!os_snprintf_error(end - pos, ret))
442e28a4053SRui Paulo pos += ret;
443e28a4053SRui Paulo pos += wpa_snprintf_hex_uppercase(
444e28a4053SRui Paulo (char *) pos, end - pos, data->mschapv2_auth_response,
445e28a4053SRui Paulo sizeof(data->mschapv2_auth_response));
446e28a4053SRui Paulo } else {
447e28a4053SRui Paulo pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_ERROR,
448*a90b9d01SCy Schubert RADIUS_VENDOR_ID_MICROSOFT, 1, 7);
449*a90b9d01SCy Schubert *pos++ = data->mschapv2_ident;
450e28a4053SRui Paulo os_memcpy(pos, "Failed", 6);
451e28a4053SRui Paulo pos += 6;
452e28a4053SRui Paulo AVP_PAD(req, pos);
453e28a4053SRui Paulo }
454e28a4053SRui Paulo
455e28a4053SRui Paulo wpabuf_set(&msgbuf, req, pos - req);
456e28a4053SRui Paulo wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Encrypting Phase 2 "
457e28a4053SRui Paulo "data", &msgbuf);
458e28a4053SRui Paulo
459e28a4053SRui Paulo encr_req = eap_server_tls_encrypt(sm, &data->ssl, &msgbuf);
460e28a4053SRui Paulo os_free(req);
461e28a4053SRui Paulo
462e28a4053SRui Paulo return encr_req;
463e28a4053SRui Paulo }
464e28a4053SRui Paulo
465e28a4053SRui Paulo
eap_ttls_buildReq(struct eap_sm * sm,void * priv,u8 id)466e28a4053SRui Paulo static struct wpabuf * eap_ttls_buildReq(struct eap_sm *sm, void *priv, u8 id)
467e28a4053SRui Paulo {
468e28a4053SRui Paulo struct eap_ttls_data *data = priv;
469e28a4053SRui Paulo
470e28a4053SRui Paulo if (data->ssl.state == FRAG_ACK) {
471e28a4053SRui Paulo return eap_server_tls_build_ack(id, EAP_TYPE_TTLS,
472e28a4053SRui Paulo data->ttls_version);
473e28a4053SRui Paulo }
474e28a4053SRui Paulo
475e28a4053SRui Paulo if (data->ssl.state == WAIT_FRAG_ACK) {
476e28a4053SRui Paulo return eap_server_tls_build_msg(&data->ssl, EAP_TYPE_TTLS,
477e28a4053SRui Paulo data->ttls_version, id);
478e28a4053SRui Paulo }
479e28a4053SRui Paulo
480e28a4053SRui Paulo switch (data->state) {
481e28a4053SRui Paulo case START:
482e28a4053SRui Paulo return eap_ttls_build_start(sm, data, id);
483e28a4053SRui Paulo case PHASE1:
484c1d255d3SCy Schubert if (tls_connection_established(sm->cfg->ssl_ctx,
485c1d255d3SCy Schubert data->ssl.conn)) {
486e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase1 done, "
487e28a4053SRui Paulo "starting Phase2");
488e28a4053SRui Paulo eap_ttls_state(data, PHASE2_START);
489e28a4053SRui Paulo }
490e28a4053SRui Paulo break;
491e28a4053SRui Paulo case PHASE2_METHOD:
492e28a4053SRui Paulo wpabuf_free(data->ssl.tls_out);
493e28a4053SRui Paulo data->ssl.tls_out_pos = 0;
494e28a4053SRui Paulo data->ssl.tls_out = eap_ttls_build_phase2_eap_req(sm, data,
495e28a4053SRui Paulo id);
496e28a4053SRui Paulo break;
497e28a4053SRui Paulo case PHASE2_MSCHAPV2_RESP:
498e28a4053SRui Paulo wpabuf_free(data->ssl.tls_out);
499e28a4053SRui Paulo data->ssl.tls_out_pos = 0;
500e28a4053SRui Paulo data->ssl.tls_out = eap_ttls_build_phase2_mschapv2(sm, data);
501e28a4053SRui Paulo break;
502e28a4053SRui Paulo default:
503e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS: %s - unexpected state %d",
504e28a4053SRui Paulo __func__, data->state);
505e28a4053SRui Paulo return NULL;
506e28a4053SRui Paulo }
507e28a4053SRui Paulo
508e28a4053SRui Paulo return eap_server_tls_build_msg(&data->ssl, EAP_TYPE_TTLS,
509e28a4053SRui Paulo data->ttls_version, id);
510e28a4053SRui Paulo }
511e28a4053SRui Paulo
512e28a4053SRui Paulo
eap_ttls_check(struct eap_sm * sm,void * priv,struct wpabuf * respData)513c1d255d3SCy Schubert static bool eap_ttls_check(struct eap_sm *sm, void *priv,
514e28a4053SRui Paulo struct wpabuf *respData)
515e28a4053SRui Paulo {
516e28a4053SRui Paulo const u8 *pos;
517e28a4053SRui Paulo size_t len;
518e28a4053SRui Paulo
519e28a4053SRui Paulo pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TTLS, respData, &len);
520e28a4053SRui Paulo if (pos == NULL || len < 1) {
521e28a4053SRui Paulo wpa_printf(MSG_INFO, "EAP-TTLS: Invalid frame");
522c1d255d3SCy Schubert return true;
523e28a4053SRui Paulo }
524e28a4053SRui Paulo
525c1d255d3SCy Schubert return false;
526e28a4053SRui Paulo }
527e28a4053SRui Paulo
528e28a4053SRui Paulo
eap_ttls_process_phase2_pap(struct eap_sm * sm,struct eap_ttls_data * data,const u8 * user_password,size_t user_password_len)529e28a4053SRui Paulo static void eap_ttls_process_phase2_pap(struct eap_sm *sm,
530e28a4053SRui Paulo struct eap_ttls_data *data,
531e28a4053SRui Paulo const u8 *user_password,
532e28a4053SRui Paulo size_t user_password_len)
533e28a4053SRui Paulo {
534e28a4053SRui Paulo if (!sm->user || !sm->user->password || sm->user->password_hash ||
535e28a4053SRui Paulo !(sm->user->ttls_auth & EAP_TTLS_AUTH_PAP)) {
536e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP: No plaintext user "
537e28a4053SRui Paulo "password configured");
538e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
539e28a4053SRui Paulo return;
540e28a4053SRui Paulo }
541e28a4053SRui Paulo
542e28a4053SRui Paulo if (sm->user->password_len != user_password_len ||
5435b9c547cSRui Paulo os_memcmp_const(sm->user->password, user_password,
5445b9c547cSRui Paulo user_password_len) != 0) {
545e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP: Invalid user password");
546e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
547e28a4053SRui Paulo return;
548e28a4053SRui Paulo }
549e28a4053SRui Paulo
550e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP: Correct user password");
551f05cddf9SRui Paulo eap_ttls_state(data, SUCCESS);
552325151a3SRui Paulo eap_ttls_valid_session(sm, data);
553e28a4053SRui Paulo }
554e28a4053SRui Paulo
555e28a4053SRui Paulo
eap_ttls_process_phase2_chap(struct eap_sm * sm,struct eap_ttls_data * data,const u8 * challenge,size_t challenge_len,const u8 * password,size_t password_len)556e28a4053SRui Paulo static void eap_ttls_process_phase2_chap(struct eap_sm *sm,
557e28a4053SRui Paulo struct eap_ttls_data *data,
558e28a4053SRui Paulo const u8 *challenge,
559e28a4053SRui Paulo size_t challenge_len,
560e28a4053SRui Paulo const u8 *password,
561e28a4053SRui Paulo size_t password_len)
562e28a4053SRui Paulo {
563e28a4053SRui Paulo u8 *chal, hash[CHAP_MD5_LEN];
564e28a4053SRui Paulo
565e28a4053SRui Paulo if (challenge == NULL || password == NULL ||
566e28a4053SRui Paulo challenge_len != EAP_TTLS_CHAP_CHALLENGE_LEN ||
567e28a4053SRui Paulo password_len != 1 + EAP_TTLS_CHAP_PASSWORD_LEN) {
568e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: Invalid CHAP attributes "
569e28a4053SRui Paulo "(challenge len %lu password len %lu)",
570e28a4053SRui Paulo (unsigned long) challenge_len,
571e28a4053SRui Paulo (unsigned long) password_len);
572e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
573e28a4053SRui Paulo return;
574e28a4053SRui Paulo }
575e28a4053SRui Paulo
576e28a4053SRui Paulo if (!sm->user || !sm->user->password || sm->user->password_hash ||
577e28a4053SRui Paulo !(sm->user->ttls_auth & EAP_TTLS_AUTH_CHAP)) {
578e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: No plaintext user "
579e28a4053SRui Paulo "password configured");
580e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
581e28a4053SRui Paulo return;
582e28a4053SRui Paulo }
583e28a4053SRui Paulo
584e28a4053SRui Paulo chal = eap_ttls_implicit_challenge(sm, data,
585e28a4053SRui Paulo EAP_TTLS_CHAP_CHALLENGE_LEN + 1);
586e28a4053SRui Paulo if (chal == NULL) {
587e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: Failed to generate "
588e28a4053SRui Paulo "challenge from TLS data");
589e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
590e28a4053SRui Paulo return;
591e28a4053SRui Paulo }
592e28a4053SRui Paulo
5935b9c547cSRui Paulo if (os_memcmp_const(challenge, chal, EAP_TTLS_CHAP_CHALLENGE_LEN)
5945b9c547cSRui Paulo != 0 ||
595e28a4053SRui Paulo password[0] != chal[EAP_TTLS_CHAP_CHALLENGE_LEN]) {
596e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: Challenge mismatch");
597e28a4053SRui Paulo os_free(chal);
598e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
599e28a4053SRui Paulo return;
600e28a4053SRui Paulo }
601e28a4053SRui Paulo os_free(chal);
602e28a4053SRui Paulo
603e28a4053SRui Paulo /* MD5(Ident + Password + Challenge) */
604e28a4053SRui Paulo chap_md5(password[0], sm->user->password, sm->user->password_len,
605e28a4053SRui Paulo challenge, challenge_len, hash);
606e28a4053SRui Paulo
6075b9c547cSRui Paulo if (os_memcmp_const(hash, password + 1, EAP_TTLS_CHAP_PASSWORD_LEN) ==
6085b9c547cSRui Paulo 0) {
609e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: Correct user password");
610f05cddf9SRui Paulo eap_ttls_state(data, SUCCESS);
611325151a3SRui Paulo eap_ttls_valid_session(sm, data);
612e28a4053SRui Paulo } else {
613e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: Invalid user password");
614e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
615e28a4053SRui Paulo }
616e28a4053SRui Paulo }
617e28a4053SRui Paulo
618e28a4053SRui Paulo
eap_ttls_process_phase2_mschap(struct eap_sm * sm,struct eap_ttls_data * data,u8 * challenge,size_t challenge_len,u8 * response,size_t response_len)619e28a4053SRui Paulo static void eap_ttls_process_phase2_mschap(struct eap_sm *sm,
620e28a4053SRui Paulo struct eap_ttls_data *data,
621e28a4053SRui Paulo u8 *challenge, size_t challenge_len,
622e28a4053SRui Paulo u8 *response, size_t response_len)
623e28a4053SRui Paulo {
624e28a4053SRui Paulo u8 *chal, nt_response[24];
625e28a4053SRui Paulo
626e28a4053SRui Paulo if (challenge == NULL || response == NULL ||
627e28a4053SRui Paulo challenge_len != EAP_TTLS_MSCHAP_CHALLENGE_LEN ||
628e28a4053SRui Paulo response_len != EAP_TTLS_MSCHAP_RESPONSE_LEN) {
629e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: Invalid MS-CHAP "
630e28a4053SRui Paulo "attributes (challenge len %lu response len %lu)",
631e28a4053SRui Paulo (unsigned long) challenge_len,
632e28a4053SRui Paulo (unsigned long) response_len);
633e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
634e28a4053SRui Paulo return;
635e28a4053SRui Paulo }
636e28a4053SRui Paulo
637e28a4053SRui Paulo if (!sm->user || !sm->user->password ||
638e28a4053SRui Paulo !(sm->user->ttls_auth & EAP_TTLS_AUTH_MSCHAP)) {
639e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: No user password "
640e28a4053SRui Paulo "configured");
641e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
642e28a4053SRui Paulo return;
643e28a4053SRui Paulo }
644e28a4053SRui Paulo
645e28a4053SRui Paulo chal = eap_ttls_implicit_challenge(sm, data,
646e28a4053SRui Paulo EAP_TTLS_MSCHAP_CHALLENGE_LEN + 1);
647e28a4053SRui Paulo if (chal == NULL) {
648e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: Failed to generate "
649e28a4053SRui Paulo "challenge from TLS data");
650e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
651e28a4053SRui Paulo return;
652e28a4053SRui Paulo }
653e28a4053SRui Paulo
654325151a3SRui Paulo #ifdef CONFIG_TESTING_OPTIONS
655325151a3SRui Paulo eap_server_mschap_rx_callback(sm, "TTLS-MSCHAP",
656325151a3SRui Paulo sm->identity, sm->identity_len,
657325151a3SRui Paulo challenge, response + 2 + 24);
658325151a3SRui Paulo #endif /* CONFIG_TESTING_OPTIONS */
659325151a3SRui Paulo
6605b9c547cSRui Paulo if (os_memcmp_const(challenge, chal, EAP_TTLS_MSCHAP_CHALLENGE_LEN)
6615b9c547cSRui Paulo != 0 ||
662e28a4053SRui Paulo response[0] != chal[EAP_TTLS_MSCHAP_CHALLENGE_LEN]) {
663e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: Challenge mismatch");
664e28a4053SRui Paulo os_free(chal);
665e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
666e28a4053SRui Paulo return;
667e28a4053SRui Paulo }
668e28a4053SRui Paulo os_free(chal);
669e28a4053SRui Paulo
67085732ac8SCy Schubert if ((sm->user->password_hash &&
67185732ac8SCy Schubert challenge_response(challenge, sm->user->password, nt_response)) ||
67285732ac8SCy Schubert (!sm->user->password_hash &&
673e28a4053SRui Paulo nt_challenge_response(challenge, sm->user->password,
67485732ac8SCy Schubert sm->user->password_len, nt_response))) {
67585732ac8SCy Schubert eap_ttls_state(data, FAILURE);
67685732ac8SCy Schubert return;
67785732ac8SCy Schubert }
678e28a4053SRui Paulo
6795b9c547cSRui Paulo if (os_memcmp_const(nt_response, response + 2 + 24, 24) == 0) {
680e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: Correct response");
681f05cddf9SRui Paulo eap_ttls_state(data, SUCCESS);
682325151a3SRui Paulo eap_ttls_valid_session(sm, data);
683e28a4053SRui Paulo } else {
684e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: Invalid NT-Response");
685e28a4053SRui Paulo wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAP: Received",
686e28a4053SRui Paulo response + 2 + 24, 24);
687e28a4053SRui Paulo wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAP: Expected",
688e28a4053SRui Paulo nt_response, 24);
689e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
690e28a4053SRui Paulo }
691e28a4053SRui Paulo }
692e28a4053SRui Paulo
693e28a4053SRui Paulo
eap_ttls_process_phase2_mschapv2(struct eap_sm * sm,struct eap_ttls_data * data,u8 * challenge,size_t challenge_len,u8 * response,size_t response_len)694e28a4053SRui Paulo static void eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
695e28a4053SRui Paulo struct eap_ttls_data *data,
696e28a4053SRui Paulo u8 *challenge,
697e28a4053SRui Paulo size_t challenge_len,
698e28a4053SRui Paulo u8 *response, size_t response_len)
699e28a4053SRui Paulo {
700e28a4053SRui Paulo u8 *chal, *username, nt_response[24], *rx_resp, *peer_challenge,
701e28a4053SRui Paulo *auth_challenge;
702e28a4053SRui Paulo size_t username_len, i;
703e28a4053SRui Paulo
704e28a4053SRui Paulo if (challenge == NULL || response == NULL ||
705e28a4053SRui Paulo challenge_len != EAP_TTLS_MSCHAPV2_CHALLENGE_LEN ||
706e28a4053SRui Paulo response_len != EAP_TTLS_MSCHAPV2_RESPONSE_LEN) {
707e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Invalid MS-CHAP2 "
708e28a4053SRui Paulo "attributes (challenge len %lu response len %lu)",
709e28a4053SRui Paulo (unsigned long) challenge_len,
710e28a4053SRui Paulo (unsigned long) response_len);
711e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
712e28a4053SRui Paulo return;
713e28a4053SRui Paulo }
714e28a4053SRui Paulo
715e28a4053SRui Paulo if (!sm->user || !sm->user->password ||
716e28a4053SRui Paulo !(sm->user->ttls_auth & EAP_TTLS_AUTH_MSCHAPV2)) {
717e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: No user password "
718e28a4053SRui Paulo "configured");
719e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
720e28a4053SRui Paulo return;
721e28a4053SRui Paulo }
722e28a4053SRui Paulo
723f05cddf9SRui Paulo if (sm->identity == NULL) {
724f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: No user identity "
725f05cddf9SRui Paulo "known");
726f05cddf9SRui Paulo eap_ttls_state(data, FAILURE);
727f05cddf9SRui Paulo return;
728f05cddf9SRui Paulo }
729f05cddf9SRui Paulo
730e28a4053SRui Paulo /* MSCHAPv2 does not include optional domain name in the
731e28a4053SRui Paulo * challenge-response calculation, so remove domain prefix
732e28a4053SRui Paulo * (if present). */
733e28a4053SRui Paulo username = sm->identity;
734e28a4053SRui Paulo username_len = sm->identity_len;
735e28a4053SRui Paulo for (i = 0; i < username_len; i++) {
736e28a4053SRui Paulo if (username[i] == '\\') {
737e28a4053SRui Paulo username_len -= i + 1;
738e28a4053SRui Paulo username += i + 1;
739e28a4053SRui Paulo break;
740e28a4053SRui Paulo }
741e28a4053SRui Paulo }
742e28a4053SRui Paulo
743e28a4053SRui Paulo chal = eap_ttls_implicit_challenge(
744e28a4053SRui Paulo sm, data, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 1);
745e28a4053SRui Paulo if (chal == NULL) {
746e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Failed to generate "
747e28a4053SRui Paulo "challenge from TLS data");
748e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
749e28a4053SRui Paulo return;
750e28a4053SRui Paulo }
751e28a4053SRui Paulo
7525b9c547cSRui Paulo if (os_memcmp_const(challenge, chal, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN)
7535b9c547cSRui Paulo != 0 ||
754e28a4053SRui Paulo response[0] != chal[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN]) {
755e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Challenge mismatch");
756e28a4053SRui Paulo os_free(chal);
757e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
758e28a4053SRui Paulo return;
759e28a4053SRui Paulo }
760e28a4053SRui Paulo os_free(chal);
761e28a4053SRui Paulo
762e28a4053SRui Paulo auth_challenge = challenge;
763e28a4053SRui Paulo peer_challenge = response + 2;
764e28a4053SRui Paulo
765e28a4053SRui Paulo wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-TTLS/MSCHAPV2: User",
766e28a4053SRui Paulo username, username_len);
767e28a4053SRui Paulo wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAPV2: auth_challenge",
768e28a4053SRui Paulo auth_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
769e28a4053SRui Paulo wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAPV2: peer_challenge",
770e28a4053SRui Paulo peer_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
771e28a4053SRui Paulo
772e28a4053SRui Paulo if (sm->user->password_hash) {
773e28a4053SRui Paulo generate_nt_response_pwhash(auth_challenge, peer_challenge,
774e28a4053SRui Paulo username, username_len,
775e28a4053SRui Paulo sm->user->password,
776e28a4053SRui Paulo nt_response);
777e28a4053SRui Paulo } else {
778e28a4053SRui Paulo generate_nt_response(auth_challenge, peer_challenge,
779e28a4053SRui Paulo username, username_len,
780e28a4053SRui Paulo sm->user->password,
781e28a4053SRui Paulo sm->user->password_len,
782e28a4053SRui Paulo nt_response);
783e28a4053SRui Paulo }
784e28a4053SRui Paulo
785e28a4053SRui Paulo rx_resp = response + 2 + EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 8;
786325151a3SRui Paulo #ifdef CONFIG_TESTING_OPTIONS
787325151a3SRui Paulo {
788325151a3SRui Paulo u8 challenge2[8];
789325151a3SRui Paulo
790325151a3SRui Paulo if (challenge_hash(peer_challenge, auth_challenge,
791325151a3SRui Paulo username, username_len, challenge2) == 0) {
792325151a3SRui Paulo eap_server_mschap_rx_callback(sm, "TTLS-MSCHAPV2",
793325151a3SRui Paulo username, username_len,
794325151a3SRui Paulo challenge2, rx_resp);
795325151a3SRui Paulo }
796325151a3SRui Paulo }
797325151a3SRui Paulo #endif /* CONFIG_TESTING_OPTIONS */
7985b9c547cSRui Paulo if (os_memcmp_const(nt_response, rx_resp, 24) == 0) {
799e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Correct "
800e28a4053SRui Paulo "NT-Response");
801e28a4053SRui Paulo data->mschapv2_resp_ok = 1;
802e28a4053SRui Paulo
803e28a4053SRui Paulo if (sm->user->password_hash) {
804e28a4053SRui Paulo generate_authenticator_response_pwhash(
805e28a4053SRui Paulo sm->user->password,
806e28a4053SRui Paulo peer_challenge, auth_challenge,
807e28a4053SRui Paulo username, username_len, nt_response,
808e28a4053SRui Paulo data->mschapv2_auth_response);
809e28a4053SRui Paulo } else {
810e28a4053SRui Paulo generate_authenticator_response(
811e28a4053SRui Paulo sm->user->password, sm->user->password_len,
812e28a4053SRui Paulo peer_challenge, auth_challenge,
813e28a4053SRui Paulo username, username_len, nt_response,
814e28a4053SRui Paulo data->mschapv2_auth_response);
815e28a4053SRui Paulo }
816e28a4053SRui Paulo } else {
817e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Invalid "
818e28a4053SRui Paulo "NT-Response");
819e28a4053SRui Paulo wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAPV2: Received",
820e28a4053SRui Paulo rx_resp, 24);
821e28a4053SRui Paulo wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAPV2: Expected",
822e28a4053SRui Paulo nt_response, 24);
823e28a4053SRui Paulo data->mschapv2_resp_ok = 0;
824e28a4053SRui Paulo }
825e28a4053SRui Paulo eap_ttls_state(data, PHASE2_MSCHAPV2_RESP);
826e28a4053SRui Paulo data->mschapv2_ident = response[0];
827e28a4053SRui Paulo }
828e28a4053SRui Paulo
829e28a4053SRui Paulo
eap_ttls_phase2_eap_init(struct eap_sm * sm,struct eap_ttls_data * data,int vendor,enum eap_type eap_type)830e28a4053SRui Paulo static int eap_ttls_phase2_eap_init(struct eap_sm *sm,
831e28a4053SRui Paulo struct eap_ttls_data *data,
832c1d255d3SCy Schubert int vendor, enum eap_type eap_type)
833e28a4053SRui Paulo {
834e28a4053SRui Paulo if (data->phase2_priv && data->phase2_method) {
835e28a4053SRui Paulo data->phase2_method->reset(sm, data->phase2_priv);
836e28a4053SRui Paulo data->phase2_method = NULL;
837e28a4053SRui Paulo data->phase2_priv = NULL;
838e28a4053SRui Paulo }
839c1d255d3SCy Schubert data->phase2_method = eap_server_get_eap_method(vendor, eap_type);
840e28a4053SRui Paulo if (!data->phase2_method)
841e28a4053SRui Paulo return -1;
842e28a4053SRui Paulo
843e28a4053SRui Paulo sm->init_phase2 = 1;
844e28a4053SRui Paulo data->phase2_priv = data->phase2_method->init(sm);
845e28a4053SRui Paulo sm->init_phase2 = 0;
846e28a4053SRui Paulo return data->phase2_priv == NULL ? -1 : 0;
847e28a4053SRui Paulo }
848e28a4053SRui Paulo
849e28a4053SRui Paulo
eap_ttls_process_phase2_eap_response(struct eap_sm * sm,struct eap_ttls_data * data,u8 * in_data,size_t in_len)850e28a4053SRui Paulo static void eap_ttls_process_phase2_eap_response(struct eap_sm *sm,
851e28a4053SRui Paulo struct eap_ttls_data *data,
852e28a4053SRui Paulo u8 *in_data, size_t in_len)
853e28a4053SRui Paulo {
854c1d255d3SCy Schubert int next_vendor = EAP_VENDOR_IETF;
855c1d255d3SCy Schubert enum eap_type next_type = EAP_TYPE_NONE;
856e28a4053SRui Paulo struct eap_hdr *hdr;
857e28a4053SRui Paulo u8 *pos;
858e28a4053SRui Paulo size_t left;
859e28a4053SRui Paulo struct wpabuf buf;
860e28a4053SRui Paulo const struct eap_method *m = data->phase2_method;
861e28a4053SRui Paulo void *priv = data->phase2_priv;
862e28a4053SRui Paulo
863e28a4053SRui Paulo if (priv == NULL) {
864e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: %s - Phase2 not "
865e28a4053SRui Paulo "initialized?!", __func__);
866e28a4053SRui Paulo return;
867e28a4053SRui Paulo }
868e28a4053SRui Paulo
869e28a4053SRui Paulo hdr = (struct eap_hdr *) in_data;
870e28a4053SRui Paulo pos = (u8 *) (hdr + 1);
871e28a4053SRui Paulo
872e28a4053SRui Paulo if (in_len > sizeof(*hdr) && *pos == EAP_TYPE_NAK) {
873e28a4053SRui Paulo left = in_len - sizeof(*hdr);
874e28a4053SRui Paulo wpa_hexdump(MSG_DEBUG, "EAP-TTLS/EAP: Phase2 type Nak'ed; "
875e28a4053SRui Paulo "allowed types", pos + 1, left - 1);
876e28a4053SRui Paulo eap_sm_process_nak(sm, pos + 1, left - 1);
877e28a4053SRui Paulo if (sm->user && sm->user_eap_method_index < EAP_MAX_METHODS &&
878e28a4053SRui Paulo sm->user->methods[sm->user_eap_method_index].method !=
879e28a4053SRui Paulo EAP_TYPE_NONE) {
880c1d255d3SCy Schubert next_vendor = sm->user->methods[
881c1d255d3SCy Schubert sm->user_eap_method_index].vendor;
882e28a4053SRui Paulo next_type = sm->user->methods[
883e28a4053SRui Paulo sm->user_eap_method_index++].method;
884c1d255d3SCy Schubert wpa_printf(MSG_DEBUG, "EAP-TTLS: try EAP type %u:%u",
885c1d255d3SCy Schubert next_vendor, next_type);
886c1d255d3SCy Schubert if (eap_ttls_phase2_eap_init(sm, data, next_vendor,
887c1d255d3SCy Schubert next_type)) {
888c1d255d3SCy Schubert wpa_printf(MSG_DEBUG,
889c1d255d3SCy Schubert "EAP-TTLS: Failed to initialize EAP type %u:%u",
890c1d255d3SCy Schubert next_vendor, next_type);
891e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
892e28a4053SRui Paulo return;
893e28a4053SRui Paulo }
894e28a4053SRui Paulo } else {
895e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
896e28a4053SRui Paulo }
897e28a4053SRui Paulo return;
898e28a4053SRui Paulo }
899e28a4053SRui Paulo
900e28a4053SRui Paulo wpabuf_set(&buf, in_data, in_len);
901e28a4053SRui Paulo
902e28a4053SRui Paulo if (m->check(sm, priv, &buf)) {
903e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: Phase2 check() asked to "
904e28a4053SRui Paulo "ignore the packet");
905e28a4053SRui Paulo return;
906e28a4053SRui Paulo }
907e28a4053SRui Paulo
908e28a4053SRui Paulo m->process(sm, priv, &buf);
909e28a4053SRui Paulo
910e28a4053SRui Paulo if (sm->method_pending == METHOD_PENDING_WAIT) {
911e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: Phase2 method is in "
912e28a4053SRui Paulo "pending wait state - save decrypted response");
913e28a4053SRui Paulo wpabuf_free(data->pending_phase2_eap_resp);
914e28a4053SRui Paulo data->pending_phase2_eap_resp = wpabuf_dup(&buf);
915e28a4053SRui Paulo }
916e28a4053SRui Paulo
917e28a4053SRui Paulo if (!m->isDone(sm, priv))
918e28a4053SRui Paulo return;
919e28a4053SRui Paulo
920e28a4053SRui Paulo if (!m->isSuccess(sm, priv)) {
921e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: Phase2 method failed");
922e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
923e28a4053SRui Paulo return;
924e28a4053SRui Paulo }
925e28a4053SRui Paulo
926e28a4053SRui Paulo switch (data->state) {
927e28a4053SRui Paulo case PHASE2_START:
928e28a4053SRui Paulo if (eap_user_get(sm, sm->identity, sm->identity_len, 1) != 0) {
929e28a4053SRui Paulo wpa_hexdump_ascii(MSG_DEBUG, "EAP_TTLS: Phase2 "
930e28a4053SRui Paulo "Identity not found in the user "
931e28a4053SRui Paulo "database",
932e28a4053SRui Paulo sm->identity, sm->identity_len);
933e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
934e28a4053SRui Paulo break;
935e28a4053SRui Paulo }
936e28a4053SRui Paulo
937e28a4053SRui Paulo eap_ttls_state(data, PHASE2_METHOD);
938c1d255d3SCy Schubert next_vendor = sm->user->methods[0].vendor;
939e28a4053SRui Paulo next_type = sm->user->methods[0].method;
940e28a4053SRui Paulo sm->user_eap_method_index = 1;
941c1d255d3SCy Schubert wpa_printf(MSG_DEBUG, "EAP-TTLS: try EAP type %u:%u",
942c1d255d3SCy Schubert next_vendor, next_type);
943c1d255d3SCy Schubert if (eap_ttls_phase2_eap_init(sm, data, next_vendor,
944c1d255d3SCy Schubert next_type)) {
945c1d255d3SCy Schubert wpa_printf(MSG_DEBUG,
946c1d255d3SCy Schubert "EAP-TTLS: Failed to initialize EAP type %u:%u",
947c1d255d3SCy Schubert next_vendor, next_type);
948e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
949e28a4053SRui Paulo }
950e28a4053SRui Paulo break;
951e28a4053SRui Paulo case PHASE2_METHOD:
952e28a4053SRui Paulo eap_ttls_state(data, SUCCESS);
953325151a3SRui Paulo eap_ttls_valid_session(sm, data);
954e28a4053SRui Paulo break;
955e28a4053SRui Paulo case FAILURE:
956e28a4053SRui Paulo break;
957e28a4053SRui Paulo default:
958e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS: %s - unexpected state %d",
959e28a4053SRui Paulo __func__, data->state);
960e28a4053SRui Paulo break;
961e28a4053SRui Paulo }
962e28a4053SRui Paulo }
963e28a4053SRui Paulo
964e28a4053SRui Paulo
eap_ttls_process_phase2_eap(struct eap_sm * sm,struct eap_ttls_data * data,const u8 * eap,size_t eap_len)965e28a4053SRui Paulo static void eap_ttls_process_phase2_eap(struct eap_sm *sm,
966e28a4053SRui Paulo struct eap_ttls_data *data,
967e28a4053SRui Paulo const u8 *eap, size_t eap_len)
968e28a4053SRui Paulo {
969e28a4053SRui Paulo struct eap_hdr *hdr;
970e28a4053SRui Paulo size_t len;
971e28a4053SRui Paulo
972e28a4053SRui Paulo if (data->state == PHASE2_START) {
973e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: initializing Phase 2");
974c1d255d3SCy Schubert if (eap_ttls_phase2_eap_init(sm, data, EAP_VENDOR_IETF,
975c1d255d3SCy Schubert EAP_TYPE_IDENTITY) < 0) {
976e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: failed to "
977e28a4053SRui Paulo "initialize EAP-Identity");
978e28a4053SRui Paulo return;
979e28a4053SRui Paulo }
980e28a4053SRui Paulo }
981e28a4053SRui Paulo
982e28a4053SRui Paulo if (eap_len < sizeof(*hdr)) {
983e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: too short Phase 2 EAP "
984e28a4053SRui Paulo "packet (len=%lu)", (unsigned long) eap_len);
985e28a4053SRui Paulo return;
986e28a4053SRui Paulo }
987e28a4053SRui Paulo
988e28a4053SRui Paulo hdr = (struct eap_hdr *) eap;
989e28a4053SRui Paulo len = be_to_host16(hdr->length);
990e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: received Phase 2 EAP: code=%d "
991e28a4053SRui Paulo "identifier=%d length=%lu", hdr->code, hdr->identifier,
992e28a4053SRui Paulo (unsigned long) len);
993e28a4053SRui Paulo if (len > eap_len) {
994e28a4053SRui Paulo wpa_printf(MSG_INFO, "EAP-TTLS/EAP: Length mismatch in Phase 2"
995e28a4053SRui Paulo " EAP frame (hdr len=%lu, data len in AVP=%lu)",
996e28a4053SRui Paulo (unsigned long) len, (unsigned long) eap_len);
997e28a4053SRui Paulo return;
998e28a4053SRui Paulo }
999e28a4053SRui Paulo
1000e28a4053SRui Paulo switch (hdr->code) {
1001e28a4053SRui Paulo case EAP_CODE_RESPONSE:
1002e28a4053SRui Paulo eap_ttls_process_phase2_eap_response(sm, data, (u8 *) hdr,
1003e28a4053SRui Paulo len);
1004e28a4053SRui Paulo break;
1005e28a4053SRui Paulo default:
1006e28a4053SRui Paulo wpa_printf(MSG_INFO, "EAP-TTLS/EAP: Unexpected code=%d in "
1007e28a4053SRui Paulo "Phase 2 EAP header", hdr->code);
1008e28a4053SRui Paulo break;
1009e28a4053SRui Paulo }
1010e28a4053SRui Paulo }
1011e28a4053SRui Paulo
1012e28a4053SRui Paulo
eap_ttls_process_phase2(struct eap_sm * sm,struct eap_ttls_data * data,struct wpabuf * in_buf)1013e28a4053SRui Paulo static void eap_ttls_process_phase2(struct eap_sm *sm,
1014e28a4053SRui Paulo struct eap_ttls_data *data,
1015e28a4053SRui Paulo struct wpabuf *in_buf)
1016e28a4053SRui Paulo {
1017e28a4053SRui Paulo struct wpabuf *in_decrypted;
1018e28a4053SRui Paulo struct eap_ttls_avp parse;
1019e28a4053SRui Paulo
1020e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
1021e28a4053SRui Paulo " Phase 2", (unsigned long) wpabuf_len(in_buf));
1022e28a4053SRui Paulo
1023e28a4053SRui Paulo if (data->pending_phase2_eap_resp) {
1024e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 EAP response "
1025e28a4053SRui Paulo "- skip decryption and use old data");
1026e28a4053SRui Paulo eap_ttls_process_phase2_eap(
1027e28a4053SRui Paulo sm, data, wpabuf_head(data->pending_phase2_eap_resp),
1028e28a4053SRui Paulo wpabuf_len(data->pending_phase2_eap_resp));
1029e28a4053SRui Paulo wpabuf_free(data->pending_phase2_eap_resp);
1030e28a4053SRui Paulo data->pending_phase2_eap_resp = NULL;
1031e28a4053SRui Paulo return;
1032e28a4053SRui Paulo }
1033e28a4053SRui Paulo
1034c1d255d3SCy Schubert in_decrypted = tls_connection_decrypt(sm->cfg->ssl_ctx, data->ssl.conn,
1035e28a4053SRui Paulo in_buf);
1036e28a4053SRui Paulo if (in_decrypted == NULL) {
1037e28a4053SRui Paulo wpa_printf(MSG_INFO, "EAP-TTLS: Failed to decrypt Phase 2 "
1038e28a4053SRui Paulo "data");
1039e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
1040e28a4053SRui Paulo return;
1041e28a4053SRui Paulo }
1042e28a4053SRui Paulo
1043e28a4053SRui Paulo wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 EAP",
1044e28a4053SRui Paulo in_decrypted);
1045e28a4053SRui Paulo
1046e28a4053SRui Paulo if (eap_ttls_avp_parse(in_decrypted, &parse) < 0) {
1047e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to parse AVPs");
1048e28a4053SRui Paulo wpabuf_free(in_decrypted);
1049e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
1050e28a4053SRui Paulo return;
1051e28a4053SRui Paulo }
1052e28a4053SRui Paulo
1053e28a4053SRui Paulo if (parse.user_name) {
10545b9c547cSRui Paulo char *nbuf;
10555b9c547cSRui Paulo nbuf = os_malloc(parse.user_name_len * 4 + 1);
10565b9c547cSRui Paulo if (nbuf) {
10575b9c547cSRui Paulo printf_encode(nbuf, parse.user_name_len * 4 + 1,
10585b9c547cSRui Paulo parse.user_name,
10595b9c547cSRui Paulo parse.user_name_len);
10605b9c547cSRui Paulo eap_log_msg(sm, "TTLS-User-Name '%s'", nbuf);
10615b9c547cSRui Paulo os_free(nbuf);
10625b9c547cSRui Paulo }
10635b9c547cSRui Paulo
1064e28a4053SRui Paulo os_free(sm->identity);
106585732ac8SCy Schubert sm->identity = os_memdup(parse.user_name, parse.user_name_len);
1066f05cddf9SRui Paulo if (sm->identity == NULL) {
1067f05cddf9SRui Paulo eap_ttls_state(data, FAILURE);
1068f05cddf9SRui Paulo goto done;
1069e28a4053SRui Paulo }
1070f05cddf9SRui Paulo sm->identity_len = parse.user_name_len;
1071e28a4053SRui Paulo if (eap_user_get(sm, parse.user_name, parse.user_name_len, 1)
1072e28a4053SRui Paulo != 0) {
1073e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase2 Identity not "
1074e28a4053SRui Paulo "found in the user database");
1075e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
1076e28a4053SRui Paulo goto done;
1077e28a4053SRui Paulo }
1078e28a4053SRui Paulo }
1079e28a4053SRui Paulo
1080e28a4053SRui Paulo #ifdef EAP_SERVER_TNC
1081e28a4053SRui Paulo if (data->tnc_started && parse.eap == NULL) {
1082e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS: TNC started but no EAP "
1083e28a4053SRui Paulo "response from peer");
1084e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
1085e28a4053SRui Paulo goto done;
1086e28a4053SRui Paulo }
1087e28a4053SRui Paulo #endif /* EAP_SERVER_TNC */
1088e28a4053SRui Paulo
1089e28a4053SRui Paulo if (parse.eap) {
1090e28a4053SRui Paulo eap_ttls_process_phase2_eap(sm, data, parse.eap,
1091e28a4053SRui Paulo parse.eap_len);
1092e28a4053SRui Paulo } else if (parse.user_password) {
1093e28a4053SRui Paulo eap_ttls_process_phase2_pap(sm, data, parse.user_password,
1094e28a4053SRui Paulo parse.user_password_len);
1095e28a4053SRui Paulo } else if (parse.chap_password) {
1096e28a4053SRui Paulo eap_ttls_process_phase2_chap(sm, data,
1097e28a4053SRui Paulo parse.chap_challenge,
1098e28a4053SRui Paulo parse.chap_challenge_len,
1099e28a4053SRui Paulo parse.chap_password,
1100e28a4053SRui Paulo parse.chap_password_len);
1101e28a4053SRui Paulo } else if (parse.mschap_response) {
1102e28a4053SRui Paulo eap_ttls_process_phase2_mschap(sm, data,
1103e28a4053SRui Paulo parse.mschap_challenge,
1104e28a4053SRui Paulo parse.mschap_challenge_len,
1105e28a4053SRui Paulo parse.mschap_response,
1106e28a4053SRui Paulo parse.mschap_response_len);
1107e28a4053SRui Paulo } else if (parse.mschap2_response) {
1108e28a4053SRui Paulo eap_ttls_process_phase2_mschapv2(sm, data,
1109e28a4053SRui Paulo parse.mschap_challenge,
1110e28a4053SRui Paulo parse.mschap_challenge_len,
1111e28a4053SRui Paulo parse.mschap2_response,
1112e28a4053SRui Paulo parse.mschap2_response_len);
1113e28a4053SRui Paulo }
1114e28a4053SRui Paulo
1115e28a4053SRui Paulo done:
1116e28a4053SRui Paulo wpabuf_free(in_decrypted);
1117e28a4053SRui Paulo os_free(parse.eap);
1118e28a4053SRui Paulo }
1119e28a4053SRui Paulo
1120e28a4053SRui Paulo
eap_ttls_start_tnc(struct eap_sm * sm,struct eap_ttls_data * data)1121e28a4053SRui Paulo static void eap_ttls_start_tnc(struct eap_sm *sm, struct eap_ttls_data *data)
1122e28a4053SRui Paulo {
1123e28a4053SRui Paulo #ifdef EAP_SERVER_TNC
1124c1d255d3SCy Schubert if (!sm->cfg->tnc || data->state != SUCCESS || data->tnc_started)
1125e28a4053SRui Paulo return;
1126e28a4053SRui Paulo
1127e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS: Initialize TNC");
1128c1d255d3SCy Schubert if (eap_ttls_phase2_eap_init(sm, data, EAP_VENDOR_IETF, EAP_TYPE_TNC)) {
1129e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to initialize TNC");
1130e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
1131e28a4053SRui Paulo return;
1132e28a4053SRui Paulo }
1133e28a4053SRui Paulo
1134e28a4053SRui Paulo data->tnc_started = 1;
1135e28a4053SRui Paulo eap_ttls_state(data, PHASE2_METHOD);
1136e28a4053SRui Paulo #endif /* EAP_SERVER_TNC */
1137e28a4053SRui Paulo }
1138e28a4053SRui Paulo
1139e28a4053SRui Paulo
eap_ttls_process_version(struct eap_sm * sm,void * priv,int peer_version)1140e28a4053SRui Paulo static int eap_ttls_process_version(struct eap_sm *sm, void *priv,
1141e28a4053SRui Paulo int peer_version)
1142e28a4053SRui Paulo {
1143e28a4053SRui Paulo struct eap_ttls_data *data = priv;
1144e28a4053SRui Paulo if (peer_version < data->ttls_version) {
1145e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS: peer ver=%d, own ver=%d; "
1146e28a4053SRui Paulo "use version %d",
1147e28a4053SRui Paulo peer_version, data->ttls_version, peer_version);
1148e28a4053SRui Paulo data->ttls_version = peer_version;
1149e28a4053SRui Paulo }
1150e28a4053SRui Paulo
1151e28a4053SRui Paulo return 0;
1152e28a4053SRui Paulo }
1153e28a4053SRui Paulo
1154e28a4053SRui Paulo
eap_ttls_process_msg(struct eap_sm * sm,void * priv,const struct wpabuf * respData)1155e28a4053SRui Paulo static void eap_ttls_process_msg(struct eap_sm *sm, void *priv,
1156e28a4053SRui Paulo const struct wpabuf *respData)
1157e28a4053SRui Paulo {
1158e28a4053SRui Paulo struct eap_ttls_data *data = priv;
1159e28a4053SRui Paulo
1160e28a4053SRui Paulo switch (data->state) {
1161e28a4053SRui Paulo case PHASE1:
1162e28a4053SRui Paulo if (eap_server_tls_phase1(sm, &data->ssl) < 0)
1163e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
1164e28a4053SRui Paulo break;
1165e28a4053SRui Paulo case PHASE2_START:
1166e28a4053SRui Paulo case PHASE2_METHOD:
1167e28a4053SRui Paulo eap_ttls_process_phase2(sm, data, data->ssl.tls_in);
1168e28a4053SRui Paulo eap_ttls_start_tnc(sm, data);
1169e28a4053SRui Paulo break;
1170e28a4053SRui Paulo case PHASE2_MSCHAPV2_RESP:
1171e28a4053SRui Paulo if (data->mschapv2_resp_ok && wpabuf_len(data->ssl.tls_in) ==
1172e28a4053SRui Paulo 0) {
1173e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Peer "
1174e28a4053SRui Paulo "acknowledged response");
1175f05cddf9SRui Paulo eap_ttls_state(data, SUCCESS);
1176325151a3SRui Paulo eap_ttls_valid_session(sm, data);
1177e28a4053SRui Paulo } else if (!data->mschapv2_resp_ok) {
1178e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Peer "
1179e28a4053SRui Paulo "acknowledged error");
1180e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
1181e28a4053SRui Paulo } else {
1182e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Unexpected "
1183e28a4053SRui Paulo "frame from peer (payload len %lu, "
1184e28a4053SRui Paulo "expected empty frame)",
1185e28a4053SRui Paulo (unsigned long)
1186e28a4053SRui Paulo wpabuf_len(data->ssl.tls_in));
1187e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
1188e28a4053SRui Paulo }
1189e28a4053SRui Paulo eap_ttls_start_tnc(sm, data);
1190e28a4053SRui Paulo break;
1191e28a4053SRui Paulo default:
1192e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS: Unexpected state %d in %s",
1193e28a4053SRui Paulo data->state, __func__);
1194e28a4053SRui Paulo break;
1195e28a4053SRui Paulo }
1196e28a4053SRui Paulo }
1197e28a4053SRui Paulo
1198e28a4053SRui Paulo
eap_ttls_process(struct eap_sm * sm,void * priv,struct wpabuf * respData)1199e28a4053SRui Paulo static void eap_ttls_process(struct eap_sm *sm, void *priv,
1200e28a4053SRui Paulo struct wpabuf *respData)
1201e28a4053SRui Paulo {
1202e28a4053SRui Paulo struct eap_ttls_data *data = priv;
1203325151a3SRui Paulo const struct wpabuf *buf;
1204325151a3SRui Paulo const u8 *pos;
1205325151a3SRui Paulo u8 id_len;
1206325151a3SRui Paulo
1207e28a4053SRui Paulo if (eap_server_tls_process(sm, &data->ssl, respData, data,
1208e28a4053SRui Paulo EAP_TYPE_TTLS, eap_ttls_process_version,
1209325151a3SRui Paulo eap_ttls_process_msg) < 0) {
1210e28a4053SRui Paulo eap_ttls_state(data, FAILURE);
1211325151a3SRui Paulo return;
1212325151a3SRui Paulo }
1213325151a3SRui Paulo
1214c1d255d3SCy Schubert if (!tls_connection_established(sm->cfg->ssl_ctx, data->ssl.conn) ||
1215c1d255d3SCy Schubert !tls_connection_resumed(sm->cfg->ssl_ctx, data->ssl.conn))
1216325151a3SRui Paulo return;
1217325151a3SRui Paulo
1218325151a3SRui Paulo buf = tls_connection_get_success_data(data->ssl.conn);
1219325151a3SRui Paulo if (!buf || wpabuf_len(buf) < 1) {
1220325151a3SRui Paulo wpa_printf(MSG_DEBUG,
1221325151a3SRui Paulo "EAP-TTLS: No success data in resumed session - reject attempt");
1222325151a3SRui Paulo eap_ttls_state(data, FAILURE);
1223325151a3SRui Paulo return;
1224325151a3SRui Paulo }
1225325151a3SRui Paulo
1226325151a3SRui Paulo pos = wpabuf_head(buf);
1227325151a3SRui Paulo if (*pos != EAP_TYPE_TTLS) {
1228325151a3SRui Paulo wpa_printf(MSG_DEBUG,
1229325151a3SRui Paulo "EAP-TTLS: Resumed session for another EAP type (%u) - reject attempt",
1230325151a3SRui Paulo *pos);
1231325151a3SRui Paulo eap_ttls_state(data, FAILURE);
1232325151a3SRui Paulo return;
1233325151a3SRui Paulo }
1234325151a3SRui Paulo
1235325151a3SRui Paulo pos++;
1236325151a3SRui Paulo id_len = *pos++;
1237325151a3SRui Paulo wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: Identity from cached session",
1238325151a3SRui Paulo pos, id_len);
1239325151a3SRui Paulo os_free(sm->identity);
1240325151a3SRui Paulo sm->identity = os_malloc(id_len ? id_len : 1);
1241325151a3SRui Paulo if (!sm->identity) {
1242325151a3SRui Paulo sm->identity_len = 0;
1243325151a3SRui Paulo eap_ttls_state(data, FAILURE);
1244325151a3SRui Paulo return;
1245325151a3SRui Paulo }
1246325151a3SRui Paulo
1247325151a3SRui Paulo os_memcpy(sm->identity, pos, id_len);
1248325151a3SRui Paulo sm->identity_len = id_len;
1249325151a3SRui Paulo
1250325151a3SRui Paulo if (eap_user_get(sm, sm->identity, sm->identity_len, 1) != 0) {
1251325151a3SRui Paulo wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: Phase2 Identity not found in the user database",
1252325151a3SRui Paulo sm->identity, sm->identity_len);
1253325151a3SRui Paulo eap_ttls_state(data, FAILURE);
1254325151a3SRui Paulo return;
1255325151a3SRui Paulo }
1256325151a3SRui Paulo
1257325151a3SRui Paulo wpa_printf(MSG_DEBUG,
1258325151a3SRui Paulo "EAP-TTLS: Resuming previous session - skip Phase2");
1259325151a3SRui Paulo eap_ttls_state(data, SUCCESS);
1260325151a3SRui Paulo tls_connection_set_success_data_resumed(data->ssl.conn);
1261e28a4053SRui Paulo }
1262e28a4053SRui Paulo
1263e28a4053SRui Paulo
eap_ttls_isDone(struct eap_sm * sm,void * priv)1264c1d255d3SCy Schubert static bool eap_ttls_isDone(struct eap_sm *sm, void *priv)
1265e28a4053SRui Paulo {
1266e28a4053SRui Paulo struct eap_ttls_data *data = priv;
1267e28a4053SRui Paulo return data->state == SUCCESS || data->state == FAILURE;
1268e28a4053SRui Paulo }
1269e28a4053SRui Paulo
1270e28a4053SRui Paulo
eap_ttls_getKey(struct eap_sm * sm,void * priv,size_t * len)1271e28a4053SRui Paulo static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
1272e28a4053SRui Paulo {
1273e28a4053SRui Paulo struct eap_ttls_data *data = priv;
1274e28a4053SRui Paulo u8 *eapKeyData;
1275c1d255d3SCy Schubert const char *label;
1276c1d255d3SCy Schubert const u8 eap_tls13_context[1] = { EAP_TYPE_TTLS };
1277c1d255d3SCy Schubert const u8 *context = NULL;
1278c1d255d3SCy Schubert size_t context_len = 0;
1279e28a4053SRui Paulo
1280e28a4053SRui Paulo if (data->state != SUCCESS)
1281e28a4053SRui Paulo return NULL;
1282e28a4053SRui Paulo
1283c1d255d3SCy Schubert if (data->ssl.tls_v13) {
1284c1d255d3SCy Schubert label = "EXPORTER_EAP_TLS_Key_Material";
1285c1d255d3SCy Schubert context = eap_tls13_context;
1286c1d255d3SCy Schubert context_len = sizeof(eap_tls13_context);
1287c1d255d3SCy Schubert } else {
1288c1d255d3SCy Schubert label = "ttls keying material";
1289c1d255d3SCy Schubert }
1290c1d255d3SCy Schubert
1291e28a4053SRui Paulo eapKeyData = eap_server_tls_derive_key(sm, &data->ssl,
1292c1d255d3SCy Schubert label, context, context_len,
1293c1d255d3SCy Schubert EAP_TLS_KEY_LEN + EAP_EMSK_LEN);
1294e28a4053SRui Paulo if (eapKeyData) {
1295e28a4053SRui Paulo *len = EAP_TLS_KEY_LEN;
1296e28a4053SRui Paulo wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
1297e28a4053SRui Paulo eapKeyData, EAP_TLS_KEY_LEN);
1298e28a4053SRui Paulo } else {
1299e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive key");
1300e28a4053SRui Paulo }
1301e28a4053SRui Paulo
1302e28a4053SRui Paulo return eapKeyData;
1303e28a4053SRui Paulo }
1304e28a4053SRui Paulo
1305e28a4053SRui Paulo
eap_ttls_isSuccess(struct eap_sm * sm,void * priv)1306c1d255d3SCy Schubert static bool eap_ttls_isSuccess(struct eap_sm *sm, void *priv)
1307e28a4053SRui Paulo {
1308e28a4053SRui Paulo struct eap_ttls_data *data = priv;
1309e28a4053SRui Paulo return data->state == SUCCESS;
1310e28a4053SRui Paulo }
1311e28a4053SRui Paulo
1312e28a4053SRui Paulo
eap_ttls_get_session_id(struct eap_sm * sm,void * priv,size_t * len)13135b9c547cSRui Paulo static u8 * eap_ttls_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
13145b9c547cSRui Paulo {
13155b9c547cSRui Paulo struct eap_ttls_data *data = priv;
13165b9c547cSRui Paulo
13175b9c547cSRui Paulo if (data->state != SUCCESS)
13185b9c547cSRui Paulo return NULL;
13195b9c547cSRui Paulo
13205b9c547cSRui Paulo return eap_server_tls_derive_session_id(sm, &data->ssl, EAP_TYPE_TTLS,
13215b9c547cSRui Paulo len);
13225b9c547cSRui Paulo }
13235b9c547cSRui Paulo
13245b9c547cSRui Paulo
eap_ttls_get_emsk(struct eap_sm * sm,void * priv,size_t * len)13255b9c547cSRui Paulo static u8 * eap_ttls_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
13265b9c547cSRui Paulo {
13275b9c547cSRui Paulo struct eap_ttls_data *data = priv;
13285b9c547cSRui Paulo u8 *eapKeyData, *emsk;
1329c1d255d3SCy Schubert const char *label;
1330c1d255d3SCy Schubert const u8 eap_tls13_context[1] = { EAP_TYPE_TTLS };
1331c1d255d3SCy Schubert const u8 *context = NULL;
1332c1d255d3SCy Schubert size_t context_len = 0;
13335b9c547cSRui Paulo
13345b9c547cSRui Paulo if (data->state != SUCCESS)
13355b9c547cSRui Paulo return NULL;
13365b9c547cSRui Paulo
1337c1d255d3SCy Schubert if (data->ssl.tls_v13) {
1338c1d255d3SCy Schubert label = "EXPORTER_EAP_TLS_Key_Material";
1339c1d255d3SCy Schubert context = eap_tls13_context;
1340c1d255d3SCy Schubert context_len = sizeof(eap_tls13_context);
1341c1d255d3SCy Schubert } else {
1342c1d255d3SCy Schubert label = "ttls keying material";
1343c1d255d3SCy Schubert }
1344c1d255d3SCy Schubert
13455b9c547cSRui Paulo eapKeyData = eap_server_tls_derive_key(sm, &data->ssl,
1346c1d255d3SCy Schubert label, context, context_len,
13475b9c547cSRui Paulo EAP_TLS_KEY_LEN + EAP_EMSK_LEN);
13485b9c547cSRui Paulo if (eapKeyData) {
13495b9c547cSRui Paulo emsk = os_malloc(EAP_EMSK_LEN);
13505b9c547cSRui Paulo if (emsk)
13515b9c547cSRui Paulo os_memcpy(emsk, eapKeyData + EAP_TLS_KEY_LEN,
13525b9c547cSRui Paulo EAP_EMSK_LEN);
13535b9c547cSRui Paulo bin_clear_free(eapKeyData, EAP_TLS_KEY_LEN + EAP_EMSK_LEN);
13545b9c547cSRui Paulo } else
13555b9c547cSRui Paulo emsk = NULL;
13565b9c547cSRui Paulo
13575b9c547cSRui Paulo if (emsk) {
13585b9c547cSRui Paulo *len = EAP_EMSK_LEN;
13595b9c547cSRui Paulo wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Derived EMSK",
13605b9c547cSRui Paulo emsk, EAP_EMSK_LEN);
13615b9c547cSRui Paulo } else {
13625b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive EMSK");
13635b9c547cSRui Paulo }
13645b9c547cSRui Paulo
13655b9c547cSRui Paulo return emsk;
13665b9c547cSRui Paulo }
13675b9c547cSRui Paulo
13685b9c547cSRui Paulo
eap_server_ttls_register(void)1369e28a4053SRui Paulo int eap_server_ttls_register(void)
1370e28a4053SRui Paulo {
1371e28a4053SRui Paulo struct eap_method *eap;
1372e28a4053SRui Paulo
1373e28a4053SRui Paulo eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
1374e28a4053SRui Paulo EAP_VENDOR_IETF, EAP_TYPE_TTLS, "TTLS");
1375e28a4053SRui Paulo if (eap == NULL)
1376e28a4053SRui Paulo return -1;
1377e28a4053SRui Paulo
1378e28a4053SRui Paulo eap->init = eap_ttls_init;
1379e28a4053SRui Paulo eap->reset = eap_ttls_reset;
1380e28a4053SRui Paulo eap->buildReq = eap_ttls_buildReq;
1381e28a4053SRui Paulo eap->check = eap_ttls_check;
1382e28a4053SRui Paulo eap->process = eap_ttls_process;
1383e28a4053SRui Paulo eap->isDone = eap_ttls_isDone;
1384e28a4053SRui Paulo eap->getKey = eap_ttls_getKey;
1385e28a4053SRui Paulo eap->isSuccess = eap_ttls_isSuccess;
13865b9c547cSRui Paulo eap->getSessionId = eap_ttls_get_session_id;
13875b9c547cSRui Paulo eap->get_emsk = eap_ttls_get_emsk;
1388e28a4053SRui Paulo
1389780fb4a2SCy Schubert return eap_server_method_register(eap);
1390e28a4053SRui Paulo }
1391