2 * WPA Supplicant / EAP-TTLS (draft-ietf-pppext-eap-ttls-03.txt)
3 * Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
21 #include "eap_tls_common.h"
22 #include "wpa_supplicant.h"
23 #include "config_ssid.h"
30 static void eap_ttls_deinit(struct eap_sm *sm, void *priv);
33 struct eap_ttls_data {
34 struct eap_ssl_data ssl;
36 const struct eap_method *phase2_method;
43 EAP_TTLS_PHASE2_MSCHAPV2,
44 EAP_TTLS_PHASE2_MSCHAP,
50 size_t num_phase2_eap_types;
53 int auth_response_valid;
55 int resuming; /* starting a resumed session */
56 int reauth; /* reauthentication */
59 u8 *pending_phase2_req;
60 size_t pending_phase2_req_len;
64 static void * eap_ttls_init(struct eap_sm *sm)
66 struct eap_ttls_data *data;
67 struct wpa_ssid *config = eap_get_config(sm);
70 data = malloc(sizeof(*data));
73 memset(data, 0, sizeof(*data));
75 data->phase2_type = EAP_TTLS_PHASE2_EAP;
76 if (config && config->phase2) {
77 if (strstr(config->phase2, "autheap=")) {
79 data->phase2_type = EAP_TTLS_PHASE2_EAP;
80 } else if (strstr(config->phase2, "auth=MSCHAPV2")) {
81 selected = "MSCHAPV2";
82 data->phase2_type = EAP_TTLS_PHASE2_MSCHAPV2;
83 } else if (strstr(config->phase2, "auth=MSCHAP")) {
85 data->phase2_type = EAP_TTLS_PHASE2_MSCHAP;
86 } else if (strstr(config->phase2, "auth=PAP")) {
88 data->phase2_type = EAP_TTLS_PHASE2_PAP;
89 } else if (strstr(config->phase2, "auth=CHAP")) {
91 data->phase2_type = EAP_TTLS_PHASE2_CHAP;
94 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase2 type: %s", selected);
96 if (data->phase2_type == EAP_TTLS_PHASE2_EAP) {
97 if (config && config->phase2) {
98 char *start, *pos, *buf;
99 u8 method, *methods = NULL, *_methods;
100 size_t num_methods = 0;
101 start = buf = strdup(config->phase2);
103 eap_ttls_deinit(sm, data);
106 while (start && *start != '\0') {
107 pos = strstr(start, "autheap=");
110 if (start != pos && *(pos - 1) != ' ') {
116 pos = strchr(start, ' ');
119 method = eap_get_phase2_type(start);
120 if (method == EAP_TYPE_NONE) {
121 wpa_printf(MSG_ERROR, "EAP-TTLS: "
122 "Unsupported Phase2 EAP "
123 "method '%s'", start);
126 _methods = realloc(methods,
128 if (_methods == NULL) {
130 eap_ttls_deinit(sm, data);
134 methods[num_methods - 1] = method;
140 data->phase2_eap_types = methods;
141 data->num_phase2_eap_types = num_methods;
143 if (data->phase2_eap_types == NULL) {
144 data->phase2_eap_types = eap_get_phase2_types(
145 config, &data->num_phase2_eap_types);
147 if (data->phase2_eap_types == NULL) {
148 wpa_printf(MSG_ERROR, "EAP-TTLS: No Phase2 EAP method "
150 eap_ttls_deinit(sm, data);
153 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase2 EAP types",
154 data->phase2_eap_types,
155 data->num_phase2_eap_types);
156 data->phase2_eap_type = EAP_TYPE_NONE;
160 if (eap_tls_ssl_init(sm, &data->ssl, config)) {
161 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to initialize SSL.");
162 eap_ttls_deinit(sm, data);
170 static void eap_ttls_deinit(struct eap_sm *sm, void *priv)
172 struct eap_ttls_data *data = priv;
175 if (data->phase2_priv && data->phase2_method)
176 data->phase2_method->deinit(sm, data->phase2_priv);
177 free(data->phase2_eap_types);
178 eap_tls_ssl_deinit(sm, &data->ssl);
179 free(data->key_data);
180 free(data->pending_phase2_req);
185 static int eap_ttls_encrypt(struct eap_sm *sm, struct eap_ttls_data *data,
186 int id, u8 *plain, size_t plain_len,
187 u8 **out_data, size_t *out_len)
191 struct eap_hdr *resp;
193 /* TODO: add support for fragmentation, if needed. This will need to
194 * add TLS Message Length field, if the frame is fragmented. */
195 resp = malloc(sizeof(struct eap_hdr) + 2 + data->ssl.tls_out_limit);
199 resp->code = EAP_CODE_RESPONSE;
200 resp->identifier = id;
202 pos = (u8 *) (resp + 1);
203 *pos++ = EAP_TYPE_TTLS;
206 res = tls_connection_encrypt(sm->ssl_ctx, data->ssl.conn,
208 pos, data->ssl.tls_out_limit);
210 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt Phase 2 "
216 *out_len = sizeof(struct eap_hdr) + 2 + res;
217 resp->length = host_to_be16(*out_len);
218 *out_data = (u8 *) resp;
223 static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
224 int mandatory, size_t len)
226 struct ttls_avp_vendor *avp;
230 avp = (struct ttls_avp_vendor *) avphdr;
231 flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
233 flags |= AVP_FLAGS_VENDOR;
234 hdrlen = sizeof(*avp);
235 avp->vendor_id = host_to_be32(vendor_id);
237 hdrlen = sizeof(struct ttls_avp);
240 avp->avp_code = host_to_be32(avp_code);
241 avp->avp_length = host_to_be32((flags << 24) | (hdrlen + len));
243 return avphdr + hdrlen;
247 static u8 * eap_ttls_avp_add(u8 *start, u8 *avphdr, u32 avp_code,
248 u32 vendor_id, int mandatory,
249 u8 *data, size_t len)
252 pos = eap_ttls_avp_hdr(avphdr, avp_code, vendor_id, mandatory, len);
253 memcpy(pos, data, len);
260 static int eap_ttls_avp_encapsulate(u8 **resp, size_t *resp_len, u32 avp_code,
265 avp = malloc(sizeof(struct ttls_avp) + *resp_len + 4);
273 pos = eap_ttls_avp_hdr(avp, avp_code, 0, mandatory, *resp_len);
274 memcpy(pos, *resp, *resp_len);
279 *resp_len = pos - avp;
284 static int eap_ttls_phase2_nak(struct eap_sm *sm,
285 struct eap_ttls_data *data,
287 u8 **resp, size_t *resp_len)
289 struct eap_hdr *resp_hdr;
290 u8 *pos = (u8 *) (hdr + 1);
292 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 Request: Nak type=%d", *pos);
293 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Allowed Phase2 EAP types",
294 data->phase2_eap_types, data->num_phase2_eap_types);
295 *resp_len = sizeof(struct eap_hdr) + 1 + data->num_phase2_eap_types;
296 *resp = malloc(*resp_len);
300 resp_hdr = (struct eap_hdr *) (*resp);
301 resp_hdr->code = EAP_CODE_RESPONSE;
302 resp_hdr->identifier = hdr->identifier;
303 resp_hdr->length = host_to_be16(*resp_len);
304 pos = (u8 *) (resp_hdr + 1);
305 *pos++ = EAP_TYPE_NAK;
306 memcpy(pos, data->phase2_eap_types, data->num_phase2_eap_types);
312 static int eap_ttls_phase2_request_eap(struct eap_sm *sm,
313 struct eap_ttls_data *data,
314 struct eap_method_ret *ret,
317 u8 **resp, size_t *resp_len)
319 size_t len = be_to_host16(hdr->length);
321 struct eap_method_ret iret;
322 struct wpa_ssid *config = eap_get_config(sm);
324 if (len <= sizeof(struct eap_hdr)) {
325 wpa_printf(MSG_INFO, "EAP-TTLS: too short "
326 "Phase 2 request (len=%lu)", (unsigned long) len);
329 pos = (u8 *) (hdr + 1);
330 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP Request: type=%d", *pos);
332 case EAP_TYPE_IDENTITY:
333 *resp = eap_sm_buildIdentity(sm, req->identifier, resp_len, 1);
336 if (data->phase2_eap_type == EAP_TYPE_NONE) {
338 for (i = 0; i < data->num_phase2_eap_types; i++) {
339 if (data->phase2_eap_types[i] != *pos)
342 data->phase2_eap_type = *pos;
343 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
344 "Phase 2 EAP method %d",
345 data->phase2_eap_type);
349 if (*pos != data->phase2_eap_type || *pos == EAP_TYPE_NONE) {
350 if (eap_ttls_phase2_nak(sm, data, hdr, resp, resp_len))
355 if (data->phase2_priv == NULL) {
356 data->phase2_method = eap_sm_get_eap_methods(*pos);
357 if (data->phase2_method) {
360 data->phase2_method->init(sm);
364 if (data->phase2_priv == NULL || data->phase2_method == NULL) {
365 wpa_printf(MSG_INFO, "EAP-TTLS: failed to initialize "
366 "Phase 2 EAP method %d", *pos);
369 memset(&iret, 0, sizeof(iret));
370 *resp = data->phase2_method->process(sm, data->phase2_priv,
371 &iret, (u8 *) hdr, len,
373 if ((iret.methodState == METHOD_DONE ||
374 iret.methodState == METHOD_MAY_CONT) &&
375 (iret.decision == DECISION_UNCOND_SUCC ||
376 iret.decision == DECISION_COND_SUCC ||
377 iret.decision == DECISION_FAIL)) {
378 ret->methodState = iret.methodState;
379 ret->decision = iret.decision;
385 (config->pending_req_identity || config->pending_req_password ||
386 config->pending_req_otp)) {
393 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP encapsulate EAP Response",
395 return eap_ttls_avp_encapsulate(resp, resp_len,
396 RADIUS_ATTR_EAP_MESSAGE, 1);
400 static int eap_ttls_phase2_request_mschapv2(struct eap_sm *sm,
401 struct eap_ttls_data *data,
402 struct eap_method_ret *ret,
405 u8 **resp, size_t *resp_len)
407 struct wpa_ssid *config = eap_get_config(sm);
408 u8 *buf, *pos, *challenge, *username, *peer_challenge;
412 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAPV2 Request");
414 /* MSCHAPv2 does not include optional domain name in the
415 * challenge-response calculation, so remove domain prefix
417 username = config->identity;
418 username_len = config->identity_len;
420 for (i = 0; i < username_len; i++) {
421 if (username[i] == '\\') {
422 username_len -= i + 1;
428 pos = buf = malloc(config->identity_len + 1000);
430 wpa_printf(MSG_ERROR,
431 "EAP-TTLS/MSCHAPV2: Failed to allocate memory");
436 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
437 config->identity, config->identity_len);
439 /* MS-CHAP-Challenge */
440 challenge = eap_tls_derive_key(sm, &data->ssl, "ttls challenge",
441 EAP_TTLS_MSCHAPV2_CHALLENGE_LEN * 2 +
443 if (challenge == NULL) {
445 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
446 "implicit challenge");
449 peer_challenge = challenge + 1 + EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
451 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
452 RADIUS_VENDOR_ID_MICROSOFT, 1,
453 challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
455 /* MS-CHAP2-Response */
456 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_RESPONSE,
457 RADIUS_VENDOR_ID_MICROSOFT, 1,
458 EAP_TTLS_MSCHAPV2_RESPONSE_LEN);
459 data->ident = challenge[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN];
460 *pos++ = data->ident;
461 *pos++ = 0; /* Flags */
462 memcpy(pos, peer_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
463 pos += EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
464 memset(pos, 0, 8); /* Reserved, must be zero */
466 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAPV2: implicit auth_challenge",
467 challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
468 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAPV2: peer_challenge",
469 peer_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
470 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MSCHAPV2 username",
471 username, username_len);
472 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAPV2 password",
473 config->password, config->password_len);
474 generate_nt_response(challenge, peer_challenge,
475 username, username_len,
476 config->password, config->password_len,
478 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAPV2 response", pos, 24);
479 generate_authenticator_response(config->password, config->password_len,
480 peer_challenge, challenge,
481 username, username_len,
482 pos, data->auth_response);
483 data->auth_response_valid = 1;
490 *resp_len = pos - buf;
492 if (sm->workaround) {
493 /* At least FreeRADIUS seems to be terminating
494 * EAP-TTLS/MSHCAPV2 without the expected MS-CHAP-v2 Success
496 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: EAP workaround - "
497 "allow success without tunneled response");
498 ret->methodState = METHOD_MAY_CONT;
499 ret->decision = DECISION_COND_SUCC;
506 static int eap_ttls_phase2_request_mschap(struct eap_sm *sm,
507 struct eap_ttls_data *data,
508 struct eap_method_ret *ret,
511 u8 **resp, size_t *resp_len)
513 struct wpa_ssid *config = eap_get_config(sm);
514 u8 *buf, *pos, *challenge;
516 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAP Request");
518 pos = buf = malloc(config->identity_len + 1000);
520 wpa_printf(MSG_ERROR,
521 "EAP-TTLS/MSCHAP: Failed to allocate memory");
526 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
527 config->identity, config->identity_len);
529 /* MS-CHAP-Challenge */
530 challenge = eap_tls_derive_key(sm, &data->ssl, "ttls challenge",
532 if (challenge == NULL) {
534 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAP: Failed to derive "
535 "implicit challenge");
539 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
540 RADIUS_VENDOR_ID_MICROSOFT, 1,
541 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
543 /* MS-CHAP-Response */
544 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_RESPONSE,
545 RADIUS_VENDOR_ID_MICROSOFT, 1,
546 EAP_TTLS_MSCHAP_RESPONSE_LEN);
547 data->ident = challenge[EAP_TTLS_MSCHAP_CHALLENGE_LEN];
548 *pos++ = data->ident;
549 *pos++ = 1; /* Flags: Use NT style passwords */
550 memset(pos, 0, 24); /* LM-Response */
552 nt_challenge_response(challenge,
553 config->password, config->password_len,
554 pos); /* NT-Response */
555 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password",
556 config->password, config->password_len);
557 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP implicit challenge",
558 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
559 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP response", pos, 24);
565 *resp_len = pos - buf;
567 /* EAP-TTLS/MSCHAP does not provide tunneled success notification, so
568 * assume that Phase2 succeeds. */
569 ret->methodState = METHOD_DONE;
570 ret->decision = DECISION_COND_SUCC;
576 static int eap_ttls_phase2_request_pap(struct eap_sm *sm,
577 struct eap_ttls_data *data,
578 struct eap_method_ret *ret,
581 u8 **resp, size_t *resp_len)
583 struct wpa_ssid *config = eap_get_config(sm);
587 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 PAP Request");
589 pos = buf = malloc(config->identity_len + config->password_len + 100);
591 wpa_printf(MSG_ERROR,
592 "EAP-TTLS/PAP: Failed to allocate memory");
597 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
598 config->identity, config->identity_len);
600 /* User-Password; in RADIUS, this is encrypted, but EAP-TTLS encrypts
601 * the data, so no separate encryption is used in the AVP itself.
602 * However, the password is padded to obfuscate its length. */
603 pad = (16 - (config->password_len & 15)) & 15;
604 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_USER_PASSWORD, 0, 1,
605 config->password_len + pad);
606 memcpy(pos, config->password, config->password_len);
607 pos += config->password_len;
613 *resp_len = pos - buf;
615 /* EAP-TTLS/PAP does not provide tunneled success notification, so
616 * assume that Phase2 succeeds. */
617 ret->methodState = METHOD_DONE;
618 ret->decision = DECISION_COND_SUCC;
624 static int eap_ttls_phase2_request_chap(struct eap_sm *sm,
625 struct eap_ttls_data *data,
626 struct eap_method_ret *ret,
629 u8 **resp, size_t *resp_len)
631 struct wpa_ssid *config = eap_get_config(sm);
632 u8 *buf, *pos, *challenge;
635 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
637 pos = buf = malloc(config->identity_len + 1000);
639 wpa_printf(MSG_ERROR,
640 "EAP-TTLS/CHAP: Failed to allocate memory");
645 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
646 config->identity, config->identity_len);
649 challenge = eap_tls_derive_key(sm, &data->ssl, "ttls challenge",
651 if (challenge == NULL) {
653 wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
654 "implicit challenge");
658 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
659 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
662 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
663 1 + EAP_TTLS_CHAP_PASSWORD_LEN);
664 data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
665 *pos++ = data->ident;
667 /* MD5(Ident + Password + Challenge) */
669 MD5Update(&context, &data->ident, 1);
670 MD5Update(&context, config->password, config->password_len);
671 MD5Update(&context, challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
672 MD5Final(pos, &context);
674 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
675 config->identity, config->identity_len);
676 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
677 config->password, config->password_len);
678 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
679 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
680 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
681 pos, EAP_TTLS_CHAP_PASSWORD_LEN);
682 pos += EAP_TTLS_CHAP_PASSWORD_LEN;
687 *resp_len = pos - buf;
689 /* EAP-TTLS/CHAP does not provide tunneled success notification, so
690 * assume that Phase2 succeeds. */
691 ret->methodState = METHOD_DONE;
692 ret->decision = DECISION_COND_SUCC;
698 static int eap_ttls_phase2_request(struct eap_sm *sm,
699 struct eap_ttls_data *data,
700 struct eap_method_ret *ret,
703 u8 **resp, size_t *resp_len)
705 struct wpa_ssid *config = eap_get_config(sm);
708 if (data->phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
709 data->phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
710 data->phase2_type == EAP_TTLS_PHASE2_PAP ||
711 data->phase2_type == EAP_TTLS_PHASE2_CHAP) {
712 if (config == NULL || config->identity == NULL) {
714 "EAP-TTLS: Identity not configured");
715 eap_sm_request_identity(sm, config);
716 if (config->password == NULL)
717 eap_sm_request_password(sm, config);
721 if (config->password == NULL) {
723 "EAP-TTLS: Password not configured");
724 eap_sm_request_password(sm, config);
729 switch (data->phase2_type) {
730 case EAP_TTLS_PHASE2_EAP:
731 res = eap_ttls_phase2_request_eap(sm, data, ret, req, hdr,
734 case EAP_TTLS_PHASE2_MSCHAPV2:
735 res = eap_ttls_phase2_request_mschapv2(sm, data, ret, req, hdr,
738 case EAP_TTLS_PHASE2_MSCHAP:
739 res = eap_ttls_phase2_request_mschap(sm, data, ret, req, hdr,
742 case EAP_TTLS_PHASE2_PAP:
743 res = eap_ttls_phase2_request_pap(sm, data, ret, req, hdr,
746 case EAP_TTLS_PHASE2_CHAP:
747 res = eap_ttls_phase2_request_chap(sm, data, ret, req, hdr,
751 wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown");
757 ret->methodState = METHOD_DONE;
758 ret->decision = DECISION_FAIL;
765 static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
766 struct eap_method_ret *ret, struct eap_hdr *req,
767 u8 *in_data, size_t in_len,
768 u8 **out_data, size_t *out_len)
770 u8 *in_decrypted = NULL, *pos;
771 int buf_len, len_decrypted = 0, len, left, retval = 0, res;
772 struct eap_hdr *hdr = NULL;
773 u8 *resp = NULL, *mschapv2 = NULL, *eapdata = NULL;
774 size_t resp_len, eap_len = 0;
775 struct ttls_avp *avp;
776 u8 recv_response[20];
777 int mschapv2_error = 0;
778 struct wpa_ssid *config = eap_get_config(sm);
780 wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
781 " Phase 2", (unsigned long) in_len);
783 if (data->pending_phase2_req) {
784 wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
785 "skip decryption and use old data");
786 /* Clear TLS reassembly state. */
787 free(data->ssl.tls_in);
788 data->ssl.tls_in = NULL;
789 data->ssl.tls_in_len = 0;
790 data->ssl.tls_in_left = 0;
791 data->ssl.tls_in_total = 0;
793 in_decrypted = data->pending_phase2_req;
794 data->pending_phase2_req = NULL;
795 len_decrypted = data->pending_phase2_req_len;
796 if (data->pending_phase2_req_len == 0) {
799 goto fake_req_identity;
804 if (in_len == 0 && data->phase2_start) {
805 data->phase2_start = 0;
806 /* EAP-TTLS does not use Phase2 on fast re-auth; this must be
807 * done only if TLS part was indeed resuming a previous
808 * session. Most Authentication Servers terminate EAP-TTLS
809 * before reaching this point, but some do not. Make
810 * wpa_supplicant stop phase 2 here, if needed. */
812 tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
813 wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
815 *out_data = eap_tls_build_ack(&data->ssl, out_len,
818 ret->methodState = METHOD_DONE;
819 ret->decision = DECISION_UNCOND_SUCC;
820 data->phase2_success = 1;
824 wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
825 "Phase 2 - use fake EAP-Request Identity");
826 buf_len = sizeof(*hdr) + 1;
827 in_decrypted = malloc(buf_len);
828 if (in_decrypted == NULL) {
829 wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate "
830 "memory for fake EAP-Identity Request");
834 hdr = (struct eap_hdr *) in_decrypted;
835 hdr->code = EAP_CODE_REQUEST;
837 hdr->length = host_to_be16(sizeof(*hdr) + 1);
838 in_decrypted[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
842 res = eap_tls_data_reassemble(sm, &data->ssl, &in_data, &in_len);
843 if (res < 0 || res == 1) {
849 if (data->ssl.tls_in_total > buf_len)
850 buf_len = data->ssl.tls_in_total;
851 in_decrypted = malloc(buf_len);
852 if (in_decrypted == NULL) {
853 free(data->ssl.tls_in);
854 data->ssl.tls_in = NULL;
855 data->ssl.tls_in_len = 0;
856 wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate memory "
862 len_decrypted = tls_connection_decrypt(sm->ssl_ctx, data->ssl.conn,
864 in_decrypted, buf_len);
865 free(data->ssl.tls_in);
866 data->ssl.tls_in = NULL;
867 data->ssl.tls_in_len = 0;
868 if (len_decrypted < 0) {
869 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to decrypt Phase 2 "
876 data->phase2_start = 0;
878 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs",
879 in_decrypted, len_decrypted);
880 if (len_decrypted < sizeof(struct ttls_avp)) {
881 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 AVP frame"
882 " len=%d expected %lu or more - dropped",
884 (unsigned long) sizeof(struct ttls_avp));
891 left = len_decrypted;
895 u32 avp_code, avp_length, vendor_id = 0;
898 avp = (struct ttls_avp *) pos;
899 avp_code = be_to_host32(avp->avp_code);
900 avp_length = be_to_host32(avp->avp_length);
901 avp_flags = (avp_length >> 24) & 0xff;
902 avp_length &= 0xffffff;
903 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
904 "length=%d", (int) avp_code, avp_flags,
906 if (avp_length > left) {
907 wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
908 "(len=%d, left=%d) - dropped",
909 (int) avp_length, left);
913 dpos = (u8 *) (avp + 1);
914 dlen = avp_length - sizeof(*avp);
915 if (avp_flags & AVP_FLAGS_VENDOR) {
917 wpa_printf(MSG_WARNING, "EAP-TTLS: vendor AVP "
922 vendor_id = be_to_host32(* (u32 *) dpos);
923 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
929 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
931 if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
932 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
933 if (eapdata == NULL) {
934 eapdata = malloc(dlen);
935 if (eapdata == NULL) {
937 wpa_printf(MSG_WARNING, "EAP-TTLS: "
938 "failed to allocate memory "
939 "for Phase 2 EAP data");
942 memcpy(eapdata, dpos, dlen);
945 u8 *neweap = realloc(eapdata, eap_len + dlen);
946 if (neweap == NULL) {
948 wpa_printf(MSG_WARNING, "EAP-TTLS: "
949 "failed to allocate memory "
950 "for Phase 2 EAP data");
953 memcpy(neweap + eap_len, dpos, dlen);
957 } else if (vendor_id == 0 &&
958 avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
959 /* This is an optional message that can be displayed to
961 wpa_hexdump_ascii(MSG_DEBUG,
962 "EAP-TTLS: AVP - Reply-Message",
964 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
965 avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
966 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: "
967 "MS-CHAP2-Success", dpos, dlen);
969 wpa_printf(MSG_WARNING, "EAP-TTLS: Unexpected "
970 "MS-CHAP2-Success length "
971 "(len=%lu, expected 43)",
972 (unsigned long) dlen);
977 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
978 avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
979 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: "
980 "MS-CHAP-Error", dpos, dlen);
982 } else if (avp_flags & AVP_FLAGS_MANDATORY) {
983 wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported "
984 "mandatory AVP code %d vendor_id %d - "
985 "dropped", (int) avp_code, (int) vendor_id);
989 wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported "
990 "AVP code %d vendor_id %d",
991 (int) avp_code, (int) vendor_id);
994 pad = (4 - (avp_length & 3)) & 3;
995 pos += avp_length + pad;
996 left -= avp_length + pad;
999 switch (data->phase2_type) {
1000 case EAP_TTLS_PHASE2_EAP:
1001 if (eapdata == NULL) {
1002 wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in "
1003 "the packet - dropped");
1008 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
1010 hdr = (struct eap_hdr *) eapdata;
1012 if (eap_len < sizeof(*hdr)) {
1013 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 "
1014 "EAP frame (len=%lu, expected %lu or more) "
1015 "- dropped", (unsigned long) eap_len,
1016 (unsigned long) sizeof(*hdr));
1020 len = be_to_host16(hdr->length);
1021 if (len > eap_len) {
1022 wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in "
1023 "Phase 2 EAP frame (EAP hdr len=%d, EAP "
1024 "data len in AVP=%lu)", len,
1025 (unsigned long) eap_len);
1029 wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
1030 "identifier=%d length=%d",
1031 hdr->code, hdr->identifier, len);
1033 switch (hdr->code) {
1034 case EAP_CODE_REQUEST:
1035 if (eap_ttls_phase2_request(sm, data, ret, req, hdr,
1036 &resp, &resp_len)) {
1037 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 "
1038 "Request processing failed");
1044 wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
1045 "Phase 2 EAP header", hdr->code);
1050 case EAP_TTLS_PHASE2_MSCHAPV2:
1051 if (mschapv2_error) {
1052 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Received "
1053 "MS-CHAP-Error - failed");
1054 ret->methodState = METHOD_DONE;
1055 ret->decision = DECISION_FAIL;
1056 *out_data = eap_tls_build_ack(&data->ssl, out_len,
1062 if (mschapv2 == NULL) {
1063 wpa_printf(MSG_WARNING, "EAP-TTLS: no MS-CHAP2-Success"
1064 " AVP received for Phase2 MSCHAPV2");
1068 if (mschapv2[0] != data->ident) {
1069 wpa_printf(MSG_WARNING, "EAP-TTLS: Ident mismatch "
1070 "for Phase 2 MSCHAPV2 (received Ident "
1071 "0x%02x, expected 0x%02x)",
1072 mschapv2[0], data->ident);
1076 if (!data->auth_response_valid ||
1077 mschapv2[1] != 'S' || mschapv2[2] != '=' ||
1078 hexstr2bin((char *) (mschapv2 + 3), recv_response, 20) ||
1079 memcmp(data->auth_response, recv_response, 20) != 0) {
1080 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid "
1081 "authenticator response in Phase 2 "
1082 "MSCHAPV2 success request");
1087 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 MSCHAPV2 "
1088 "authentication succeeded");
1089 ret->methodState = METHOD_DONE;
1090 ret->decision = DECISION_UNCOND_SUCC;
1091 data->phase2_success = 1;
1093 /* Reply with empty data; authentication server will reply
1094 * with EAP-Success after this. */
1097 case EAP_TTLS_PHASE2_MSCHAP:
1098 case EAP_TTLS_PHASE2_PAP:
1099 case EAP_TTLS_PHASE2_CHAP:
1100 /* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
1101 * requests to the supplicant */
1102 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
1109 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Encrypting Phase 2 data",
1112 if (eap_ttls_encrypt(sm, data, req->identifier,
1113 resp, resp_len, out_data, out_len)) {
1114 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt "
1118 } else if (config->pending_req_identity ||
1119 config->pending_req_password ||
1120 config->pending_req_otp) {
1121 free(data->pending_phase2_req);
1122 data->pending_phase2_req = malloc(len_decrypted);
1123 if (data->pending_phase2_req) {
1124 memcpy(data->pending_phase2_req, in_decrypted,
1126 data->pending_phase2_req_len = len_decrypted;
1135 ret->methodState = METHOD_DONE;
1136 ret->decision = DECISION_FAIL;
1143 static u8 * eap_ttls_process(struct eap_sm *sm, void *priv,
1144 struct eap_method_ret *ret,
1145 u8 *reqData, size_t reqDataLen,
1146 size_t *respDataLen)
1148 struct eap_hdr *req;
1150 unsigned int tls_msg_len;
1151 u8 flags, *pos, *resp, id;
1152 struct eap_ttls_data *data = priv;
1154 if (tls_get_errors(sm->ssl_ctx)) {
1155 wpa_printf(MSG_INFO, "EAP-TTLS: TLS errors detected");
1160 req = (struct eap_hdr *) reqData;
1161 pos = (u8 *) (req + 1);
1162 if (reqDataLen < sizeof(*req) + 2 || *pos != EAP_TYPE_TTLS ||
1163 (left = be_to_host16(req->length)) > reqDataLen) {
1164 wpa_printf(MSG_INFO, "EAP-TTLS: Invalid frame");
1168 left -= sizeof(struct eap_hdr);
1169 id = req->identifier;
1173 wpa_printf(MSG_DEBUG, "EAP-TTLS: Received packet(len=%lu) - "
1174 "Flags 0x%02x", (unsigned long) reqDataLen, flags);
1175 if (flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) {
1177 wpa_printf(MSG_INFO, "EAP-TTLS: Short frame with TLS "
1182 tls_msg_len = (pos[0] << 24) | (pos[1] << 16) | (pos[2] << 8) |
1184 wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS Message Length: %d",
1186 if (data->ssl.tls_in_left == 0) {
1187 data->ssl.tls_in_total = tls_msg_len;
1188 data->ssl.tls_in_left = tls_msg_len;
1189 free(data->ssl.tls_in);
1190 data->ssl.tls_in = NULL;
1191 data->ssl.tls_in_len = 0;
1197 ret->ignore = FALSE;
1198 ret->methodState = METHOD_CONT;
1199 ret->decision = DECISION_FAIL;
1200 ret->allowNotifications = TRUE;
1202 if (flags & EAP_TLS_FLAGS_START) {
1203 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start");
1204 /* draft-ietf-pppext-eap-ttls-03.txt, Ch. 8.1:
1205 * EAP-TTLS Start packet may, in a future specification, be
1206 * allowed to contain data. Client based on this draft version
1207 * must ignore such data but must not reject the Start packet.
1213 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1215 res = eap_ttls_decrypt(sm, data, ret, req, pos, left,
1216 &resp, respDataLen);
1218 res = eap_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS, 0,
1220 &resp, respDataLen);
1222 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1223 wpa_printf(MSG_DEBUG,
1224 "EAP-TTLS: TLS done, proceed to Phase 2");
1225 if (data->resuming) {
1226 wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth -"
1227 " may skip Phase 2");
1228 ret->decision = DECISION_COND_SUCC;
1229 ret->methodState = METHOD_MAY_CONT;
1231 data->phase2_start = 1;
1232 free(data->key_data);
1234 eap_tls_derive_key(sm, &data->ssl,
1235 "ttls keying material",
1237 if (data->key_data) {
1238 wpa_hexdump_key(MSG_DEBUG,
1239 "EAP-TTLS: Derived key",
1243 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to "
1247 if (*respDataLen == 0) {
1248 if (eap_ttls_decrypt(sm, data, ret, req, NULL,
1249 0, &resp, respDataLen)) {
1250 wpa_printf(MSG_WARNING, "EAP-TTLS: "
1251 "failed to process early "
1252 "start for Phase 2");
1260 if (ret->methodState == METHOD_DONE) {
1261 ret->allowNotifications = FALSE;
1262 if (ret->decision == DECISION_UNCOND_SUCC ||
1263 ret->decision == DECISION_COND_SUCC) {
1264 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1265 "completed successfully");
1266 data->phase2_success = 1;
1268 } else if (sm->workaround && ret->methodState == METHOD_MAY_CONT &&
1269 (ret->decision == DECISION_UNCOND_SUCC ||
1270 ret->decision == DECISION_COND_SUCC)) {
1271 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1272 "completed successfully (EAP workaround)");
1273 data->phase2_success = 1;
1277 return eap_tls_build_ack(&data->ssl, respDataLen, id,
1284 static Boolean eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
1286 struct eap_ttls_data *data = priv;
1287 return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1288 data->phase2_success;
1292 static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
1294 struct eap_ttls_data *data = priv;
1295 free(data->pending_phase2_req);
1296 data->pending_phase2_req = NULL;
1300 static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
1302 struct eap_ttls_data *data = priv;
1303 free(data->key_data);
1304 data->key_data = NULL;
1305 if (eap_tls_reauth_init(sm, &data->ssl)) {
1309 data->phase2_start = 0;
1310 data->phase2_success = 0;
1317 static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
1318 size_t buflen, int verbose)
1320 struct eap_ttls_data *data = priv;
1323 len = eap_tls_status(sm, &data->ssl, buf, buflen, verbose);
1324 switch (data->phase2_type) {
1325 case EAP_TTLS_PHASE2_EAP:
1326 len += snprintf(buf + len, buflen - len,
1327 "EAP-TTLS Phase2 method=EAP-%s\n",
1328 data->phase2_method ? data->phase2_method->name
1331 case EAP_TTLS_PHASE2_MSCHAPV2:
1332 len += snprintf(buf + len, buflen - len,
1333 "EAP-TTLS Phase2 method=MSCHAPV2\n");
1335 case EAP_TTLS_PHASE2_MSCHAP:
1336 len += snprintf(buf + len, buflen - len,
1337 "EAP-TTLS Phase2 method=MSCHAP\n");
1339 case EAP_TTLS_PHASE2_PAP:
1340 len += snprintf(buf + len, buflen - len,
1341 "EAP-TTLS Phase2 method=PAP\n");
1343 case EAP_TTLS_PHASE2_CHAP:
1344 len += snprintf(buf + len, buflen - len,
1345 "EAP-TTLS Phase2 method=CHAP\n");
1353 static Boolean eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
1355 struct eap_ttls_data *data = priv;
1356 return data->key_data != NULL && data->phase2_success;
1360 static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
1362 struct eap_ttls_data *data = priv;
1365 if (data->key_data == NULL || !data->phase2_success)
1368 key = malloc(EAP_TLS_KEY_LEN);
1372 *len = EAP_TLS_KEY_LEN;
1373 memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
1379 const struct eap_method eap_method_ttls =
1381 .method = EAP_TYPE_TTLS,
1383 .init = eap_ttls_init,
1384 .deinit = eap_ttls_deinit,
1385 .process = eap_ttls_process,
1386 .isKeyAvailable = eap_ttls_isKeyAvailable,
1387 .getKey = eap_ttls_getKey,
1388 .get_status = eap_ttls_get_status,
1389 .has_reauth_data = eap_ttls_has_reauth_data,
1390 .deinit_for_reauth = eap_ttls_deinit_for_reauth,
1391 .init_for_reauth = eap_ttls_init_for_reauth,