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, const 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,
315 const struct eap_hdr *req,
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,
403 const struct eap_hdr *req,
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,
509 const struct eap_hdr *req,
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,
579 const struct eap_hdr *req,
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,
627 const struct eap_hdr *req,
629 u8 **resp, size_t *resp_len)
631 struct wpa_ssid *config = eap_get_config(sm);
632 u8 *buf, *pos, *challenge;
636 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
638 pos = buf = malloc(config->identity_len + 1000);
640 wpa_printf(MSG_ERROR,
641 "EAP-TTLS/CHAP: Failed to allocate memory");
646 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
647 config->identity, config->identity_len);
650 challenge = eap_tls_derive_key(sm, &data->ssl, "ttls challenge",
652 if (challenge == NULL) {
654 wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
655 "implicit challenge");
659 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
660 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
663 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
664 1 + EAP_TTLS_CHAP_PASSWORD_LEN);
665 data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
666 *pos++ = data->ident;
668 /* MD5(Ident + Password + Challenge) */
669 addr[0] = &data->ident;
671 addr[1] = config->password;
672 len[1] = config->password_len;
674 len[2] = EAP_TTLS_CHAP_CHALLENGE_LEN;
675 md5_vector(3, addr, len, pos);
677 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
678 config->identity, config->identity_len);
679 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
680 config->password, config->password_len);
681 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
682 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
683 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
684 pos, EAP_TTLS_CHAP_PASSWORD_LEN);
685 pos += EAP_TTLS_CHAP_PASSWORD_LEN;
690 *resp_len = pos - buf;
692 /* EAP-TTLS/CHAP does not provide tunneled success notification, so
693 * assume that Phase2 succeeds. */
694 ret->methodState = METHOD_DONE;
695 ret->decision = DECISION_COND_SUCC;
701 static int eap_ttls_phase2_request(struct eap_sm *sm,
702 struct eap_ttls_data *data,
703 struct eap_method_ret *ret,
704 const struct eap_hdr *req,
706 u8 **resp, size_t *resp_len)
708 struct wpa_ssid *config = eap_get_config(sm);
711 if (data->phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
712 data->phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
713 data->phase2_type == EAP_TTLS_PHASE2_PAP ||
714 data->phase2_type == EAP_TTLS_PHASE2_CHAP) {
715 if (config == NULL || config->identity == NULL) {
717 "EAP-TTLS: Identity not configured");
718 eap_sm_request_identity(sm, config);
719 if (config->password == NULL)
720 eap_sm_request_password(sm, config);
724 if (config->password == NULL) {
726 "EAP-TTLS: Password not configured");
727 eap_sm_request_password(sm, config);
732 switch (data->phase2_type) {
733 case EAP_TTLS_PHASE2_EAP:
734 res = eap_ttls_phase2_request_eap(sm, data, ret, req, hdr,
737 case EAP_TTLS_PHASE2_MSCHAPV2:
738 res = eap_ttls_phase2_request_mschapv2(sm, data, ret, req, hdr,
741 case EAP_TTLS_PHASE2_MSCHAP:
742 res = eap_ttls_phase2_request_mschap(sm, data, ret, req, hdr,
745 case EAP_TTLS_PHASE2_PAP:
746 res = eap_ttls_phase2_request_pap(sm, data, ret, req, hdr,
749 case EAP_TTLS_PHASE2_CHAP:
750 res = eap_ttls_phase2_request_chap(sm, data, ret, req, hdr,
754 wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown");
760 ret->methodState = METHOD_DONE;
761 ret->decision = DECISION_FAIL;
768 static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
769 struct eap_method_ret *ret,
770 const struct eap_hdr *req,
771 const u8 *in_data, size_t in_len,
772 u8 **out_data, size_t *out_len)
774 u8 *in_decrypted = NULL, *pos;
775 int buf_len, len_decrypted = 0, len, left, retval = 0;
776 struct eap_hdr *hdr = NULL;
777 u8 *resp = NULL, *mschapv2 = NULL, *eapdata = NULL;
778 size_t resp_len, eap_len = 0;
779 struct ttls_avp *avp;
780 u8 recv_response[20];
781 int mschapv2_error = 0;
782 struct wpa_ssid *config = eap_get_config(sm);
787 wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
788 " Phase 2", (unsigned long) in_len);
790 if (data->pending_phase2_req) {
791 wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
792 "skip decryption and use old data");
793 /* Clear TLS reassembly state. */
794 free(data->ssl.tls_in);
795 data->ssl.tls_in = NULL;
796 data->ssl.tls_in_len = 0;
797 data->ssl.tls_in_left = 0;
798 data->ssl.tls_in_total = 0;
800 in_decrypted = data->pending_phase2_req;
801 data->pending_phase2_req = NULL;
802 len_decrypted = data->pending_phase2_req_len;
803 if (data->pending_phase2_req_len == 0) {
806 goto fake_req_identity;
811 if (in_len == 0 && data->phase2_start) {
812 data->phase2_start = 0;
813 /* EAP-TTLS does not use Phase2 on fast re-auth; this must be
814 * done only if TLS part was indeed resuming a previous
815 * session. Most Authentication Servers terminate EAP-TTLS
816 * before reaching this point, but some do not. Make
817 * wpa_supplicant stop phase 2 here, if needed. */
819 tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
820 wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
822 *out_data = eap_tls_build_ack(&data->ssl, out_len,
825 ret->methodState = METHOD_DONE;
826 ret->decision = DECISION_UNCOND_SUCC;
827 data->phase2_success = 1;
831 wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
832 "Phase 2 - use fake EAP-Request Identity");
833 buf_len = sizeof(*hdr) + 1;
834 in_decrypted = malloc(buf_len);
835 if (in_decrypted == NULL) {
836 wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate "
837 "memory for fake EAP-Identity Request");
841 hdr = (struct eap_hdr *) in_decrypted;
842 hdr->code = EAP_CODE_REQUEST;
844 hdr->length = host_to_be16(sizeof(*hdr) + 1);
845 in_decrypted[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
849 msg = eap_tls_data_reassemble(sm, &data->ssl, in_data, in_len,
850 &msg_len, &need_more_input);
852 return need_more_input ? 1 : -1;
855 if (data->ssl.tls_in_total > buf_len)
856 buf_len = data->ssl.tls_in_total;
857 in_decrypted = malloc(buf_len);
858 if (in_decrypted == NULL) {
859 free(data->ssl.tls_in);
860 data->ssl.tls_in = NULL;
861 data->ssl.tls_in_len = 0;
862 wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate memory "
868 len_decrypted = tls_connection_decrypt(sm->ssl_ctx, data->ssl.conn,
870 in_decrypted, buf_len);
871 free(data->ssl.tls_in);
872 data->ssl.tls_in = NULL;
873 data->ssl.tls_in_len = 0;
874 if (len_decrypted < 0) {
875 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to decrypt Phase 2 "
882 data->phase2_start = 0;
884 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs",
885 in_decrypted, len_decrypted);
886 if (len_decrypted < sizeof(struct ttls_avp)) {
887 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 AVP frame"
888 " len=%d expected %lu or more - dropped",
890 (unsigned long) sizeof(struct ttls_avp));
897 left = len_decrypted;
901 u32 avp_code, avp_length, vendor_id = 0;
904 avp = (struct ttls_avp *) pos;
905 avp_code = be_to_host32(avp->avp_code);
906 avp_length = be_to_host32(avp->avp_length);
907 avp_flags = (avp_length >> 24) & 0xff;
908 avp_length &= 0xffffff;
909 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
910 "length=%d", (int) avp_code, avp_flags,
912 if (avp_length > left) {
913 wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
914 "(len=%d, left=%d) - dropped",
915 (int) avp_length, left);
919 dpos = (u8 *) (avp + 1);
920 dlen = avp_length - sizeof(*avp);
921 if (avp_flags & AVP_FLAGS_VENDOR) {
923 wpa_printf(MSG_WARNING, "EAP-TTLS: vendor AVP "
928 vendor_id = be_to_host32(* (u32 *) dpos);
929 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
935 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
937 if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
938 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
939 if (eapdata == NULL) {
940 eapdata = malloc(dlen);
941 if (eapdata == NULL) {
943 wpa_printf(MSG_WARNING, "EAP-TTLS: "
944 "failed to allocate memory "
945 "for Phase 2 EAP data");
948 memcpy(eapdata, dpos, dlen);
951 u8 *neweap = realloc(eapdata, eap_len + dlen);
952 if (neweap == NULL) {
954 wpa_printf(MSG_WARNING, "EAP-TTLS: "
955 "failed to allocate memory "
956 "for Phase 2 EAP data");
959 memcpy(neweap + eap_len, dpos, dlen);
963 } else if (vendor_id == 0 &&
964 avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
965 /* This is an optional message that can be displayed to
967 wpa_hexdump_ascii(MSG_DEBUG,
968 "EAP-TTLS: AVP - Reply-Message",
970 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
971 avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
972 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: "
973 "MS-CHAP2-Success", dpos, dlen);
975 wpa_printf(MSG_WARNING, "EAP-TTLS: Unexpected "
976 "MS-CHAP2-Success length "
977 "(len=%lu, expected 43)",
978 (unsigned long) dlen);
983 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
984 avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
985 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: "
986 "MS-CHAP-Error", dpos, dlen);
988 } else if (avp_flags & AVP_FLAGS_MANDATORY) {
989 wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported "
990 "mandatory AVP code %d vendor_id %d - "
991 "dropped", (int) avp_code, (int) vendor_id);
995 wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported "
996 "AVP code %d vendor_id %d",
997 (int) avp_code, (int) vendor_id);
1000 pad = (4 - (avp_length & 3)) & 3;
1001 pos += avp_length + pad;
1002 left -= avp_length + pad;
1005 switch (data->phase2_type) {
1006 case EAP_TTLS_PHASE2_EAP:
1007 if (eapdata == NULL) {
1008 wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in "
1009 "the packet - dropped");
1014 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
1016 hdr = (struct eap_hdr *) eapdata;
1018 if (eap_len < sizeof(*hdr)) {
1019 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 "
1020 "EAP frame (len=%lu, expected %lu or more) "
1021 "- dropped", (unsigned long) eap_len,
1022 (unsigned long) sizeof(*hdr));
1026 len = be_to_host16(hdr->length);
1027 if (len > eap_len) {
1028 wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in "
1029 "Phase 2 EAP frame (EAP hdr len=%d, EAP "
1030 "data len in AVP=%lu)", len,
1031 (unsigned long) eap_len);
1035 wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
1036 "identifier=%d length=%d",
1037 hdr->code, hdr->identifier, len);
1039 switch (hdr->code) {
1040 case EAP_CODE_REQUEST:
1041 if (eap_ttls_phase2_request(sm, data, ret, req, hdr,
1042 &resp, &resp_len)) {
1043 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 "
1044 "Request processing failed");
1050 wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
1051 "Phase 2 EAP header", hdr->code);
1056 case EAP_TTLS_PHASE2_MSCHAPV2:
1057 if (mschapv2_error) {
1058 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Received "
1059 "MS-CHAP-Error - failed");
1060 ret->methodState = METHOD_DONE;
1061 ret->decision = DECISION_FAIL;
1062 *out_data = eap_tls_build_ack(&data->ssl, out_len,
1068 if (mschapv2 == NULL) {
1069 wpa_printf(MSG_WARNING, "EAP-TTLS: no MS-CHAP2-Success"
1070 " AVP received for Phase2 MSCHAPV2");
1074 if (mschapv2[0] != data->ident) {
1075 wpa_printf(MSG_WARNING, "EAP-TTLS: Ident mismatch "
1076 "for Phase 2 MSCHAPV2 (received Ident "
1077 "0x%02x, expected 0x%02x)",
1078 mschapv2[0], data->ident);
1082 if (!data->auth_response_valid ||
1083 mschapv2[1] != 'S' || mschapv2[2] != '=' ||
1084 hexstr2bin((char *) (mschapv2 + 3), recv_response, 20) ||
1085 memcmp(data->auth_response, recv_response, 20) != 0) {
1086 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid "
1087 "authenticator response in Phase 2 "
1088 "MSCHAPV2 success request");
1093 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 MSCHAPV2 "
1094 "authentication succeeded");
1095 ret->methodState = METHOD_DONE;
1096 ret->decision = DECISION_UNCOND_SUCC;
1097 data->phase2_success = 1;
1099 /* Reply with empty data; authentication server will reply
1100 * with EAP-Success after this. */
1103 case EAP_TTLS_PHASE2_MSCHAP:
1104 case EAP_TTLS_PHASE2_PAP:
1105 case EAP_TTLS_PHASE2_CHAP:
1106 /* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
1107 * requests to the supplicant */
1108 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
1115 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Encrypting Phase 2 data",
1118 if (eap_ttls_encrypt(sm, data, req->identifier,
1119 resp, resp_len, out_data, out_len)) {
1120 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt "
1124 } else if (config->pending_req_identity ||
1125 config->pending_req_password ||
1126 config->pending_req_otp ||
1127 config->pending_req_new_password) {
1128 free(data->pending_phase2_req);
1129 data->pending_phase2_req = malloc(len_decrypted);
1130 if (data->pending_phase2_req) {
1131 memcpy(data->pending_phase2_req, in_decrypted,
1133 data->pending_phase2_req_len = len_decrypted;
1142 ret->methodState = METHOD_DONE;
1143 ret->decision = DECISION_FAIL;
1150 static u8 * eap_ttls_process(struct eap_sm *sm, void *priv,
1151 struct eap_method_ret *ret,
1152 const u8 *reqData, size_t reqDataLen,
1153 size_t *respDataLen)
1155 const struct eap_hdr *req;
1158 u8 flags, *resp, id;
1160 struct eap_ttls_data *data = priv;
1162 pos = eap_tls_process_init(sm, &data->ssl, EAP_TYPE_TTLS, ret,
1163 reqData, reqDataLen, &left, &flags);
1166 req = (const struct eap_hdr *) reqData;
1167 id = req->identifier;
1169 if (flags & EAP_TLS_FLAGS_START) {
1170 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start");
1171 /* draft-ietf-pppext-eap-ttls-03.txt, Ch. 8.1:
1172 * EAP-TTLS Start packet may, in a future specification, be
1173 * allowed to contain data. Client based on this draft version
1174 * must ignore such data but must not reject the Start packet.
1180 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1182 res = eap_ttls_decrypt(sm, data, ret, req, pos, left,
1183 &resp, respDataLen);
1185 res = eap_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS, 0,
1187 &resp, respDataLen);
1189 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1190 wpa_printf(MSG_DEBUG,
1191 "EAP-TTLS: TLS done, proceed to Phase 2");
1192 if (data->resuming) {
1193 wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth -"
1194 " may skip Phase 2");
1195 ret->decision = DECISION_COND_SUCC;
1196 ret->methodState = METHOD_MAY_CONT;
1198 data->phase2_start = 1;
1199 free(data->key_data);
1201 eap_tls_derive_key(sm, &data->ssl,
1202 "ttls keying material",
1204 if (data->key_data) {
1205 wpa_hexdump_key(MSG_DEBUG,
1206 "EAP-TTLS: Derived key",
1210 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to "
1214 if (*respDataLen == 0) {
1215 if (eap_ttls_decrypt(sm, data, ret, req, NULL,
1216 0, &resp, respDataLen)) {
1217 wpa_printf(MSG_WARNING, "EAP-TTLS: "
1218 "failed to process early "
1219 "start for Phase 2");
1227 if (ret->methodState == METHOD_DONE) {
1228 ret->allowNotifications = FALSE;
1229 if (ret->decision == DECISION_UNCOND_SUCC ||
1230 ret->decision == DECISION_COND_SUCC) {
1231 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1232 "completed successfully");
1233 data->phase2_success = 1;
1235 } else if (sm->workaround && ret->methodState == METHOD_MAY_CONT &&
1236 (ret->decision == DECISION_UNCOND_SUCC ||
1237 ret->decision == DECISION_COND_SUCC)) {
1238 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1239 "completed successfully (EAP workaround)");
1240 data->phase2_success = 1;
1244 return eap_tls_build_ack(&data->ssl, respDataLen, id,
1251 static Boolean eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
1253 struct eap_ttls_data *data = priv;
1254 return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1255 data->phase2_success;
1259 static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
1261 struct eap_ttls_data *data = priv;
1262 free(data->pending_phase2_req);
1263 data->pending_phase2_req = NULL;
1267 static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
1269 struct eap_ttls_data *data = priv;
1270 free(data->key_data);
1271 data->key_data = NULL;
1272 if (eap_tls_reauth_init(sm, &data->ssl)) {
1276 data->phase2_start = 0;
1277 data->phase2_success = 0;
1284 static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
1285 size_t buflen, int verbose)
1287 struct eap_ttls_data *data = priv;
1290 len = eap_tls_status(sm, &data->ssl, buf, buflen, verbose);
1291 switch (data->phase2_type) {
1292 case EAP_TTLS_PHASE2_EAP:
1293 len += snprintf(buf + len, buflen - len,
1294 "EAP-TTLS Phase2 method=EAP-%s\n",
1295 data->phase2_method ? data->phase2_method->name
1298 case EAP_TTLS_PHASE2_MSCHAPV2:
1299 len += snprintf(buf + len, buflen - len,
1300 "EAP-TTLS Phase2 method=MSCHAPV2\n");
1302 case EAP_TTLS_PHASE2_MSCHAP:
1303 len += snprintf(buf + len, buflen - len,
1304 "EAP-TTLS Phase2 method=MSCHAP\n");
1306 case EAP_TTLS_PHASE2_PAP:
1307 len += snprintf(buf + len, buflen - len,
1308 "EAP-TTLS Phase2 method=PAP\n");
1310 case EAP_TTLS_PHASE2_CHAP:
1311 len += snprintf(buf + len, buflen - len,
1312 "EAP-TTLS Phase2 method=CHAP\n");
1320 static Boolean eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
1322 struct eap_ttls_data *data = priv;
1323 return data->key_data != NULL && data->phase2_success;
1327 static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
1329 struct eap_ttls_data *data = priv;
1332 if (data->key_data == NULL || !data->phase2_success)
1335 key = malloc(EAP_TLS_KEY_LEN);
1339 *len = EAP_TLS_KEY_LEN;
1340 memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
1346 const struct eap_method eap_method_ttls =
1348 .method = EAP_TYPE_TTLS,
1350 .init = eap_ttls_init,
1351 .deinit = eap_ttls_deinit,
1352 .process = eap_ttls_process,
1353 .isKeyAvailable = eap_ttls_isKeyAvailable,
1354 .getKey = eap_ttls_getKey,
1355 .get_status = eap_ttls_get_status,
1356 .has_reauth_data = eap_ttls_has_reauth_data,
1357 .deinit_for_reauth = eap_ttls_deinit_for_reauth,
1358 .init_for_reauth = eap_ttls_init_for_reauth,