2008-02-28 02:34:43 +01:00
|
|
|
/*
|
|
|
|
* hostapd / EAP-TLS (RFC 2716)
|
2008-05-28 08:57:17 +02:00
|
|
|
* Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
|
2008-02-28 02:34:43 +01:00
|
|
|
*
|
2012-02-11 15:46:35 +01:00
|
|
|
* This software may be distributed under the terms of the BSD license.
|
|
|
|
* See README for more details.
|
2008-02-28 02:34:43 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "eap_i.h"
|
|
|
|
#include "eap_tls_common.h"
|
2009-11-29 22:04:43 +01:00
|
|
|
#include "crypto/tls.h"
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
static void eap_tls_reset(struct eap_sm *sm, void *priv);
|
|
|
|
|
|
|
|
|
|
|
|
struct eap_tls_data {
|
|
|
|
struct eap_ssl_data ssl;
|
|
|
|
enum { START, CONTINUE, SUCCESS, FAILURE } state;
|
2008-11-20 18:39:35 +01:00
|
|
|
int established;
|
2012-08-22 21:34:11 +02:00
|
|
|
u8 eap_type;
|
2008-02-28 02:34:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-05-28 16:19:57 +02:00
|
|
|
static const char * eap_tls_state_txt(int state)
|
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case START:
|
|
|
|
return "START";
|
|
|
|
case CONTINUE:
|
|
|
|
return "CONTINUE";
|
|
|
|
case SUCCESS:
|
|
|
|
return "SUCCESS";
|
|
|
|
case FAILURE:
|
|
|
|
return "FAILURE";
|
|
|
|
default:
|
|
|
|
return "Unknown?!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void eap_tls_state(struct eap_tls_data *data, int state)
|
|
|
|
{
|
|
|
|
wpa_printf(MSG_DEBUG, "EAP-TLS: %s -> %s",
|
|
|
|
eap_tls_state_txt(data->state),
|
|
|
|
eap_tls_state_txt(state));
|
|
|
|
data->state = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
static void * eap_tls_init(struct eap_sm *sm)
|
|
|
|
{
|
|
|
|
struct eap_tls_data *data;
|
|
|
|
|
|
|
|
data = os_zalloc(sizeof(*data));
|
|
|
|
if (data == NULL)
|
|
|
|
return NULL;
|
|
|
|
data->state = START;
|
|
|
|
|
|
|
|
if (eap_server_tls_ssl_init(sm, &data->ssl, 1)) {
|
|
|
|
wpa_printf(MSG_INFO, "EAP-TLS: Failed to initialize SSL.");
|
|
|
|
eap_tls_reset(sm, data);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-22 21:34:11 +02:00
|
|
|
data->eap_type = EAP_TYPE_TLS;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef EAP_SERVER_UNAUTH_TLS
|
|
|
|
static void * eap_unauth_tls_init(struct eap_sm *sm)
|
|
|
|
{
|
|
|
|
struct eap_tls_data *data;
|
|
|
|
|
|
|
|
data = os_zalloc(sizeof(*data));
|
|
|
|
if (data == NULL)
|
|
|
|
return NULL;
|
|
|
|
data->state = START;
|
|
|
|
|
|
|
|
if (eap_server_tls_ssl_init(sm, &data->ssl, 0)) {
|
|
|
|
wpa_printf(MSG_INFO, "EAP-TLS: Failed to initialize SSL.");
|
|
|
|
eap_tls_reset(sm, data);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->eap_type = EAP_UNAUTH_TLS_TYPE;
|
2008-02-28 02:34:43 +01:00
|
|
|
return data;
|
|
|
|
}
|
2012-08-22 21:34:11 +02:00
|
|
|
#endif /* EAP_SERVER_UNAUTH_TLS */
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
|
2013-07-23 20:22:38 +02:00
|
|
|
#ifdef CONFIG_HS20
|
|
|
|
static void * eap_wfa_unauth_tls_init(struct eap_sm *sm)
|
|
|
|
{
|
|
|
|
struct eap_tls_data *data;
|
|
|
|
|
|
|
|
data = os_zalloc(sizeof(*data));
|
|
|
|
if (data == NULL)
|
|
|
|
return NULL;
|
|
|
|
data->state = START;
|
|
|
|
|
|
|
|
if (eap_server_tls_ssl_init(sm, &data->ssl, 0)) {
|
|
|
|
wpa_printf(MSG_INFO, "EAP-TLS: Failed to initialize SSL.");
|
|
|
|
eap_tls_reset(sm, data);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->eap_type = EAP_WFA_UNAUTH_TLS_TYPE;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_HS20 */
|
|
|
|
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
static void eap_tls_reset(struct eap_sm *sm, void *priv)
|
|
|
|
{
|
|
|
|
struct eap_tls_data *data = priv;
|
|
|
|
if (data == NULL)
|
|
|
|
return;
|
|
|
|
eap_server_tls_ssl_deinit(sm, &data->ssl);
|
|
|
|
os_free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct wpabuf * eap_tls_build_start(struct eap_sm *sm,
|
|
|
|
struct eap_tls_data *data, u8 id)
|
|
|
|
{
|
|
|
|
struct wpabuf *req;
|
|
|
|
|
2012-08-22 21:34:11 +02:00
|
|
|
req = eap_tls_msg_alloc(data->eap_type, 1, EAP_CODE_REQUEST, id);
|
2008-02-28 02:34:43 +01:00
|
|
|
if (req == NULL) {
|
|
|
|
wpa_printf(MSG_ERROR, "EAP-TLS: Failed to allocate memory for "
|
|
|
|
"request");
|
2008-05-28 16:19:57 +02:00
|
|
|
eap_tls_state(data, FAILURE);
|
2008-02-28 02:34:43 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
wpabuf_put_u8(req, EAP_TLS_FLAGS_START);
|
|
|
|
|
2008-05-28 16:19:57 +02:00
|
|
|
eap_tls_state(data, CONTINUE);
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-28 08:57:17 +02:00
|
|
|
static struct wpabuf * eap_tls_buildReq(struct eap_sm *sm, void *priv, u8 id)
|
2008-02-28 02:34:43 +01:00
|
|
|
{
|
2008-05-28 08:57:17 +02:00
|
|
|
struct eap_tls_data *data = priv;
|
2008-11-20 18:39:35 +01:00
|
|
|
struct wpabuf *res;
|
2008-02-28 02:34:43 +01:00
|
|
|
|
2008-05-28 08:57:17 +02:00
|
|
|
if (data->ssl.state == FRAG_ACK) {
|
2012-08-22 21:34:11 +02:00
|
|
|
return eap_server_tls_build_ack(id, data->eap_type, 0);
|
2008-05-28 08:57:17 +02:00
|
|
|
}
|
2008-02-28 02:34:43 +01:00
|
|
|
|
2008-05-28 08:57:17 +02:00
|
|
|
if (data->ssl.state == WAIT_FRAG_ACK) {
|
2012-08-22 21:34:11 +02:00
|
|
|
res = eap_server_tls_build_msg(&data->ssl, data->eap_type, 0,
|
2008-11-20 18:39:35 +01:00
|
|
|
id);
|
|
|
|
goto check_established;
|
2008-05-28 08:57:17 +02:00
|
|
|
}
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
switch (data->state) {
|
|
|
|
case START:
|
|
|
|
return eap_tls_build_start(sm, data, id);
|
|
|
|
case CONTINUE:
|
2008-11-20 18:39:35 +01:00
|
|
|
if (tls_connection_established(sm->ssl_ctx, data->ssl.conn))
|
|
|
|
data->established = 1;
|
2008-05-28 08:57:17 +02:00
|
|
|
break;
|
2008-02-28 02:34:43 +01:00
|
|
|
default:
|
|
|
|
wpa_printf(MSG_DEBUG, "EAP-TLS: %s - unexpected state %d",
|
|
|
|
__func__, data->state);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-05-28 08:57:17 +02:00
|
|
|
|
2012-08-22 21:34:11 +02:00
|
|
|
res = eap_server_tls_build_msg(&data->ssl, data->eap_type, 0, id);
|
2008-11-20 18:39:35 +01:00
|
|
|
|
|
|
|
check_established:
|
|
|
|
if (data->established && data->ssl.state != WAIT_FRAG_ACK) {
|
|
|
|
/* TLS handshake has been completed and there are no more
|
|
|
|
* fragments waiting to be sent out. */
|
|
|
|
wpa_printf(MSG_DEBUG, "EAP-TLS: Done");
|
|
|
|
eap_tls_state(data, SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Boolean eap_tls_check(struct eap_sm *sm, void *priv,
|
|
|
|
struct wpabuf *respData)
|
|
|
|
{
|
2012-08-22 21:34:11 +02:00
|
|
|
struct eap_tls_data *data = priv;
|
2008-02-28 02:34:43 +01:00
|
|
|
const u8 *pos;
|
|
|
|
size_t len;
|
|
|
|
|
2012-08-22 21:34:11 +02:00
|
|
|
if (data->eap_type == EAP_UNAUTH_TLS_TYPE)
|
|
|
|
pos = eap_hdr_validate(EAP_VENDOR_UNAUTH_TLS,
|
|
|
|
EAP_VENDOR_TYPE_UNAUTH_TLS, respData,
|
|
|
|
&len);
|
2013-07-23 20:22:38 +02:00
|
|
|
else if (data->eap_type == EAP_WFA_UNAUTH_TLS_TYPE)
|
|
|
|
pos = eap_hdr_validate(EAP_VENDOR_WFA_NEW,
|
|
|
|
EAP_VENDOR_WFA_UNAUTH_TLS, respData,
|
|
|
|
&len);
|
2012-08-22 21:34:11 +02:00
|
|
|
else
|
|
|
|
pos = eap_hdr_validate(EAP_VENDOR_IETF, data->eap_type,
|
|
|
|
respData, &len);
|
2008-02-28 02:34:43 +01:00
|
|
|
if (pos == NULL || len < 1) {
|
|
|
|
wpa_printf(MSG_INFO, "EAP-TLS: Invalid frame");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-28 16:15:17 +02:00
|
|
|
static void eap_tls_process_msg(struct eap_sm *sm, void *priv,
|
|
|
|
const struct wpabuf *respData)
|
2008-02-28 02:34:43 +01:00
|
|
|
{
|
|
|
|
struct eap_tls_data *data = priv;
|
2009-12-23 23:16:58 +01:00
|
|
|
if (data->state == SUCCESS && wpabuf_len(data->ssl.tls_in) == 0) {
|
2008-05-29 19:36:18 +02:00
|
|
|
wpa_printf(MSG_DEBUG, "EAP-TLS: Client acknowledged final TLS "
|
|
|
|
"handshake message");
|
|
|
|
return;
|
|
|
|
}
|
2008-05-28 08:57:17 +02:00
|
|
|
if (eap_server_tls_phase1(sm, &data->ssl) < 0)
|
2008-05-28 16:19:57 +02:00
|
|
|
eap_tls_state(data, FAILURE);
|
2008-05-28 16:15:17 +02:00
|
|
|
}
|
2008-02-28 02:34:43 +01:00
|
|
|
|
2008-05-28 08:57:17 +02:00
|
|
|
|
2008-05-28 16:15:17 +02:00
|
|
|
static void eap_tls_process(struct eap_sm *sm, void *priv,
|
|
|
|
struct wpabuf *respData)
|
|
|
|
{
|
|
|
|
struct eap_tls_data *data = priv;
|
|
|
|
if (eap_server_tls_process(sm, &data->ssl, respData, data,
|
2012-08-22 21:34:11 +02:00
|
|
|
data->eap_type, NULL, eap_tls_process_msg) <
|
2008-05-28 16:15:17 +02:00
|
|
|
0)
|
2008-05-28 16:19:57 +02:00
|
|
|
eap_tls_state(data, FAILURE);
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Boolean eap_tls_isDone(struct eap_sm *sm, void *priv)
|
|
|
|
{
|
|
|
|
struct eap_tls_data *data = priv;
|
|
|
|
return data->state == SUCCESS || data->state == FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static u8 * eap_tls_getKey(struct eap_sm *sm, void *priv, size_t *len)
|
|
|
|
{
|
|
|
|
struct eap_tls_data *data = priv;
|
|
|
|
u8 *eapKeyData;
|
|
|
|
|
|
|
|
if (data->state != SUCCESS)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
eapKeyData = eap_server_tls_derive_key(sm, &data->ssl,
|
|
|
|
"client EAP encryption",
|
|
|
|
EAP_TLS_KEY_LEN);
|
|
|
|
if (eapKeyData) {
|
|
|
|
*len = EAP_TLS_KEY_LEN;
|
|
|
|
wpa_hexdump(MSG_DEBUG, "EAP-TLS: Derived key",
|
|
|
|
eapKeyData, EAP_TLS_KEY_LEN);
|
|
|
|
} else {
|
|
|
|
wpa_printf(MSG_DEBUG, "EAP-TLS: Failed to derive key");
|
|
|
|
}
|
|
|
|
|
|
|
|
return eapKeyData;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static u8 * eap_tls_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
|
|
|
|
{
|
|
|
|
struct eap_tls_data *data = priv;
|
|
|
|
u8 *eapKeyData, *emsk;
|
|
|
|
|
|
|
|
if (data->state != SUCCESS)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
eapKeyData = eap_server_tls_derive_key(sm, &data->ssl,
|
|
|
|
"client EAP encryption",
|
|
|
|
EAP_TLS_KEY_LEN + EAP_EMSK_LEN);
|
|
|
|
if (eapKeyData) {
|
|
|
|
emsk = os_malloc(EAP_EMSK_LEN);
|
|
|
|
if (emsk)
|
|
|
|
os_memcpy(emsk, eapKeyData + EAP_TLS_KEY_LEN,
|
|
|
|
EAP_EMSK_LEN);
|
2014-12-01 00:25:52 +01:00
|
|
|
bin_clear_free(eapKeyData, EAP_TLS_KEY_LEN + EAP_EMSK_LEN);
|
2008-02-28 02:34:43 +01:00
|
|
|
} else
|
|
|
|
emsk = NULL;
|
|
|
|
|
|
|
|
if (emsk) {
|
|
|
|
*len = EAP_EMSK_LEN;
|
|
|
|
wpa_hexdump(MSG_DEBUG, "EAP-TLS: Derived EMSK",
|
|
|
|
emsk, EAP_EMSK_LEN);
|
|
|
|
} else {
|
|
|
|
wpa_printf(MSG_DEBUG, "EAP-TLS: Failed to derive EMSK");
|
|
|
|
}
|
|
|
|
|
|
|
|
return emsk;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Boolean eap_tls_isSuccess(struct eap_sm *sm, void *priv)
|
|
|
|
{
|
|
|
|
struct eap_tls_data *data = priv;
|
|
|
|
return data->state == SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-29 22:46:45 +01:00
|
|
|
static u8 * eap_tls_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
|
|
|
|
{
|
|
|
|
struct eap_tls_data *data = priv;
|
|
|
|
|
|
|
|
if (data->state != SUCCESS)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return eap_server_tls_derive_session_id(sm, &data->ssl, EAP_TYPE_TLS,
|
|
|
|
len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
int eap_server_tls_register(void)
|
|
|
|
{
|
|
|
|
struct eap_method *eap;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
|
|
|
|
EAP_VENDOR_IETF, EAP_TYPE_TLS, "TLS");
|
|
|
|
if (eap == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
eap->init = eap_tls_init;
|
|
|
|
eap->reset = eap_tls_reset;
|
|
|
|
eap->buildReq = eap_tls_buildReq;
|
|
|
|
eap->check = eap_tls_check;
|
|
|
|
eap->process = eap_tls_process;
|
|
|
|
eap->isDone = eap_tls_isDone;
|
|
|
|
eap->getKey = eap_tls_getKey;
|
|
|
|
eap->isSuccess = eap_tls_isSuccess;
|
|
|
|
eap->get_emsk = eap_tls_get_emsk;
|
2014-11-29 22:46:45 +01:00
|
|
|
eap->getSessionId = eap_tls_get_session_id;
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
ret = eap_server_method_register(eap);
|
|
|
|
if (ret)
|
|
|
|
eap_server_method_free(eap);
|
|
|
|
return ret;
|
|
|
|
}
|
2012-08-22 21:34:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef EAP_SERVER_UNAUTH_TLS
|
|
|
|
int eap_server_unauth_tls_register(void)
|
|
|
|
{
|
|
|
|
struct eap_method *eap;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
|
|
|
|
EAP_VENDOR_UNAUTH_TLS,
|
|
|
|
EAP_VENDOR_TYPE_UNAUTH_TLS,
|
|
|
|
"UNAUTH-TLS");
|
|
|
|
if (eap == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
eap->init = eap_unauth_tls_init;
|
|
|
|
eap->reset = eap_tls_reset;
|
|
|
|
eap->buildReq = eap_tls_buildReq;
|
|
|
|
eap->check = eap_tls_check;
|
|
|
|
eap->process = eap_tls_process;
|
|
|
|
eap->isDone = eap_tls_isDone;
|
|
|
|
eap->getKey = eap_tls_getKey;
|
|
|
|
eap->isSuccess = eap_tls_isSuccess;
|
|
|
|
eap->get_emsk = eap_tls_get_emsk;
|
|
|
|
|
|
|
|
ret = eap_server_method_register(eap);
|
|
|
|
if (ret)
|
|
|
|
eap_server_method_free(eap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* EAP_SERVER_UNAUTH_TLS */
|
2013-07-23 20:22:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_HS20
|
|
|
|
int eap_server_wfa_unauth_tls_register(void)
|
|
|
|
{
|
|
|
|
struct eap_method *eap;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
|
|
|
|
EAP_VENDOR_WFA_NEW,
|
|
|
|
EAP_VENDOR_WFA_UNAUTH_TLS,
|
|
|
|
"WFA-UNAUTH-TLS");
|
|
|
|
if (eap == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
eap->init = eap_wfa_unauth_tls_init;
|
|
|
|
eap->reset = eap_tls_reset;
|
|
|
|
eap->buildReq = eap_tls_buildReq;
|
|
|
|
eap->check = eap_tls_check;
|
|
|
|
eap->process = eap_tls_process;
|
|
|
|
eap->isDone = eap_tls_isDone;
|
|
|
|
eap->getKey = eap_tls_getKey;
|
|
|
|
eap->isSuccess = eap_tls_isSuccess;
|
|
|
|
eap->get_emsk = eap_tls_get_emsk;
|
|
|
|
|
|
|
|
ret = eap_server_method_register(eap);
|
|
|
|
if (ret)
|
|
|
|
eap_server_method_free(eap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_HS20 */
|