📄 eap_ttls.c
字号:
/* * hostapd / EAP-TTLS (RFC 5281) * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * Alternatively, this software may be distributed under the terms of BSD * license. * * See README and COPYING for more details. */#include "includes.h"#include "common.h"#include "eap_server/eap_i.h"#include "eap_server/eap_tls_common.h"#include "ms_funcs.h"#include "sha1.h"#include "eap_common/chap.h"#include "tls.h"#include "eap_common/eap_ttls.h"/* Maximum supported TTLS version * 0 = RFC 5281 * 1 = draft-funk-eap-ttls-v1-00.txt */#ifndef EAP_TTLS_VERSION#define EAP_TTLS_VERSION 0 /* TTLSv1 implementation is not yet complete */#endif /* EAP_TTLS_VERSION */#define MSCHAPV2_KEY_LEN 16static void eap_ttls_reset(struct eap_sm *sm, void *priv);struct eap_ttls_data { struct eap_ssl_data ssl; enum { START, PHASE1, PHASE2_START, PHASE2_METHOD, PHASE2_MSCHAPV2_RESP, PHASE_FINISHED, SUCCESS, FAILURE } state; int ttls_version; int force_version; const struct eap_method *phase2_method; void *phase2_priv; int mschapv2_resp_ok; u8 mschapv2_auth_response[20]; u8 mschapv2_ident; int tls_ia_configured; struct wpabuf *pending_phase2_eap_resp; int tnc_started;};static const char * eap_ttls_state_txt(int state){ switch (state) { case START: return "START"; case PHASE1: return "PHASE1"; case PHASE2_START: return "PHASE2_START"; case PHASE2_METHOD: return "PHASE2_METHOD"; case PHASE2_MSCHAPV2_RESP: return "PHASE2_MSCHAPV2_RESP"; case PHASE_FINISHED: return "PHASE_FINISHED"; case SUCCESS: return "SUCCESS"; case FAILURE: return "FAILURE"; default: return "Unknown?!"; }}static void eap_ttls_state(struct eap_ttls_data *data, int state){ wpa_printf(MSG_DEBUG, "EAP-TTLS: %s -> %s", eap_ttls_state_txt(data->state), eap_ttls_state_txt(state)); data->state = state;}static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id, int mandatory, size_t len){ struct ttls_avp_vendor *avp; u8 flags; size_t hdrlen; avp = (struct ttls_avp_vendor *) avphdr; flags = mandatory ? AVP_FLAGS_MANDATORY : 0; if (vendor_id) { flags |= AVP_FLAGS_VENDOR; hdrlen = sizeof(*avp); avp->vendor_id = host_to_be32(vendor_id); } else { hdrlen = sizeof(struct ttls_avp); } avp->avp_code = host_to_be32(avp_code); avp->avp_length = host_to_be32((flags << 24) | (hdrlen + len)); return avphdr + hdrlen;}static struct wpabuf * eap_ttls_avp_encapsulate(struct wpabuf *resp, u32 avp_code, int mandatory){ struct wpabuf *avp; u8 *pos; avp = wpabuf_alloc(sizeof(struct ttls_avp) + wpabuf_len(resp) + 4); if (avp == NULL) { wpabuf_free(resp); return NULL; } pos = eap_ttls_avp_hdr(wpabuf_mhead(avp), avp_code, 0, mandatory, wpabuf_len(resp)); os_memcpy(pos, wpabuf_head(resp), wpabuf_len(resp)); pos += wpabuf_len(resp); AVP_PAD((const u8 *) wpabuf_head(avp), pos); wpabuf_free(resp); wpabuf_put(avp, pos - (u8 *) wpabuf_head(avp)); return avp;}struct eap_ttls_avp { /* Note: eap is allocated memory; caller is responsible for freeing * it. All the other pointers are pointing to the packet data, i.e., * they must not be freed separately. */ u8 *eap; size_t eap_len; u8 *user_name; size_t user_name_len; u8 *user_password; size_t user_password_len; u8 *chap_challenge; size_t chap_challenge_len; u8 *chap_password; size_t chap_password_len; u8 *mschap_challenge; size_t mschap_challenge_len; u8 *mschap_response; size_t mschap_response_len; u8 *mschap2_response; size_t mschap2_response_len;};static int eap_ttls_avp_parse(u8 *buf, size_t len, struct eap_ttls_avp *parse){ struct ttls_avp *avp; u8 *pos; int left; pos = buf; left = len; os_memset(parse, 0, sizeof(*parse)); while (left > 0) { u32 avp_code, avp_length, vendor_id = 0; u8 avp_flags, *dpos; size_t pad, dlen; avp = (struct ttls_avp *) pos; avp_code = be_to_host32(avp->avp_code); avp_length = be_to_host32(avp->avp_length); avp_flags = (avp_length >> 24) & 0xff; avp_length &= 0xffffff; wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x " "length=%d", (int) avp_code, avp_flags, (int) avp_length); if ((int) avp_length > left) { wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow " "(len=%d, left=%d) - dropped", (int) avp_length, left); goto fail; } if (avp_length < sizeof(*avp)) { wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid AVP length " "%d", avp_length); goto fail; } dpos = (u8 *) (avp + 1); dlen = avp_length - sizeof(*avp); if (avp_flags & AVP_FLAGS_VENDOR) { if (dlen < 4) { wpa_printf(MSG_WARNING, "EAP-TTLS: vendor AVP " "underflow"); goto fail; } vendor_id = be_to_host32(* (be32 *) dpos); wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d", (int) vendor_id); dpos += 4; dlen -= 4; } wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen); if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) { wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message"); if (parse->eap == NULL) { parse->eap = os_malloc(dlen); if (parse->eap == NULL) { wpa_printf(MSG_WARNING, "EAP-TTLS: " "failed to allocate memory " "for Phase 2 EAP data"); goto fail; } os_memcpy(parse->eap, dpos, dlen); parse->eap_len = dlen; } else { u8 *neweap = os_realloc(parse->eap, parse->eap_len + dlen); if (neweap == NULL) { wpa_printf(MSG_WARNING, "EAP-TTLS: " "failed to allocate memory " "for Phase 2 EAP data"); goto fail; } os_memcpy(neweap + parse->eap_len, dpos, dlen); parse->eap = neweap; parse->eap_len += dlen; } } else if (vendor_id == 0 && avp_code == RADIUS_ATTR_USER_NAME) { wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: User-Name", dpos, dlen); parse->user_name = dpos; parse->user_name_len = dlen; } else if (vendor_id == 0 && avp_code == RADIUS_ATTR_USER_PASSWORD) { u8 *password = dpos; size_t password_len = dlen; while (password_len > 0 && password[password_len - 1] == '\0') { password_len--; } wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: " "User-Password (PAP)", password, password_len); parse->user_password = password; parse->user_password_len = password_len; } else if (vendor_id == 0 && avp_code == RADIUS_ATTR_CHAP_CHALLENGE) { wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP-Challenge (CHAP)", dpos, dlen); parse->chap_challenge = dpos; parse->chap_challenge_len = dlen; } else if (vendor_id == 0 && avp_code == RADIUS_ATTR_CHAP_PASSWORD) { wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP-Password (CHAP)", dpos, dlen); parse->chap_password = dpos; parse->chap_password_len = dlen; } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT && avp_code == RADIUS_ATTR_MS_CHAP_CHALLENGE) { wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MS-CHAP-Challenge", dpos, dlen); parse->mschap_challenge = dpos; parse->mschap_challenge_len = dlen; } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT && avp_code == RADIUS_ATTR_MS_CHAP_RESPONSE) { wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MS-CHAP-Response (MSCHAP)", dpos, dlen); parse->mschap_response = dpos; parse->mschap_response_len = dlen; } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT && avp_code == RADIUS_ATTR_MS_CHAP2_RESPONSE) { wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MS-CHAP2-Response (MSCHAPV2)", dpos, dlen); parse->mschap2_response = dpos; parse->mschap2_response_len = dlen; } else if (avp_flags & AVP_FLAGS_MANDATORY) { wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported " "mandatory AVP code %d vendor_id %d - " "dropped", (int) avp_code, (int) vendor_id); goto fail; } else { wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported " "AVP code %d vendor_id %d", (int) avp_code, (int) vendor_id); } pad = (4 - (avp_length & 3)) & 3; pos += avp_length + pad; left -= avp_length + pad; } return 0;fail: os_free(parse->eap); parse->eap = NULL; return -1;}static u8 * eap_ttls_implicit_challenge(struct eap_sm *sm, struct eap_ttls_data *data, size_t len){ struct tls_keys keys; u8 *challenge, *rnd; if (data->ttls_version == 0) { return eap_server_tls_derive_key(sm, &data->ssl, "ttls challenge", len); } os_memset(&keys, 0, sizeof(keys)); if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) || keys.client_random == NULL || keys.server_random == NULL || keys.inner_secret == NULL) { wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, " "client random, or server random to derive " "implicit challenge"); return NULL; } rnd = os_malloc(keys.client_random_len + keys.server_random_len); challenge = os_malloc(len); if (rnd == NULL || challenge == NULL) { wpa_printf(MSG_INFO, "EAP-TTLS: No memory for implicit " "challenge derivation"); os_free(rnd); os_free(challenge); return NULL; } os_memcpy(rnd, keys.server_random, keys.server_random_len); os_memcpy(rnd + keys.server_random_len, keys.client_random, keys.client_random_len); if (tls_prf(keys.inner_secret, keys.inner_secret_len, "inner application challenge", rnd, keys.client_random_len + keys.server_random_len, challenge, len)) { wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive implicit " "challenge"); os_free(rnd); os_free(challenge); return NULL; } os_free(rnd); wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived implicit challenge", challenge, len); return challenge;}static void * eap_ttls_init(struct eap_sm *sm){ struct eap_ttls_data *data; data = os_zalloc(sizeof(*data)); if (data == NULL) return NULL; data->ttls_version = EAP_TTLS_VERSION; data->force_version = -1; if (sm->user && sm->user->force_version >= 0) { data->force_version = sm->user->force_version; wpa_printf(MSG_DEBUG, "EAP-TTLS: forcing version %d", data->force_version); data->ttls_version = data->force_version; } data->state = START; if (!(tls_capabilities(sm->ssl_ctx) & TLS_CAPABILITY_IA) && data->ttls_version > 0) { if (data->force_version > 0) { wpa_printf(MSG_INFO, "EAP-TTLS: Forced TTLSv%d and " "TLS library does not support TLS/IA.", data->force_version); eap_ttls_reset(sm, data); return NULL; } data->ttls_version = 0; } if (eap_server_tls_ssl_init(sm, &data->ssl, 0)) { wpa_printf(MSG_INFO, "EAP-TTLS: Failed to initialize SSL."); eap_ttls_reset(sm, data); return NULL; } return data;}static void eap_ttls_reset(struct eap_sm *sm, void *priv){ struct eap_ttls_data *data = priv; if (data == NULL) return; if (data->phase2_priv && data->phase2_method) data->phase2_method->reset(sm, data->phase2_priv); eap_server_tls_ssl_deinit(sm, &data->ssl); wpabuf_free(data->pending_phase2_eap_resp); os_free(data);}static struct wpabuf * eap_ttls_build_start(struct eap_sm *sm, struct eap_ttls_data *data, u8 id){ struct wpabuf *req; req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TTLS, 1, EAP_CODE_REQUEST, id); if (req == NULL) { wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to allocate memory for" " request"); eap_ttls_state(data, FAILURE); return NULL; } wpabuf_put_u8(req, EAP_TLS_FLAGS_START | data->ttls_version); eap_ttls_state(data, PHASE1); return req;}static struct wpabuf * eap_ttls_build_phase2_eap_req( struct eap_sm *sm, struct eap_ttls_data *data, u8 id){ struct wpabuf *buf, *encr_req; u8 *req; size_t req_len; buf = data->phase2_method->buildReq(sm, data->phase2_priv, id); if (buf == NULL) return NULL; wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS/EAP: Encapsulate Phase 2 data", buf); buf = eap_ttls_avp_encapsulate(buf, RADIUS_ATTR_EAP_MESSAGE, 1); if (buf == NULL) { wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: Failed to encapsulate " "packet"); return NULL; } req = wpabuf_mhead(buf); req_len = wpabuf_len(buf); wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS/EAP: Encrypt encapsulated Phase " "2 data", req, req_len); encr_req = eap_server_tls_encrypt(sm, &data->ssl, req, req_len); wpabuf_free(buf); return encr_req;}static struct wpabuf * eap_ttls_build_phase2_mschapv2( struct eap_sm *sm, struct eap_ttls_data *data){ struct wpabuf *encr_req; u8 *req, *pos, *end; int ret; size_t req_len; pos = req = os_malloc(100); if (req == NULL) return NULL; end = req + 100;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -