freebsd-nq/contrib/hostapd/eap_identity.c

182 lines
3.9 KiB
C
Raw Normal View History

2005-06-05 22:35:03 +00:00
/*
* hostapd / EAP-Identity
2007-07-09 16:15:06 +00:00
* Copyright (c) 2004-2006, Jouni Malinen <j@w1.fi>
2005-06-05 22:35:03 +00:00
*
* 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.
*/
2007-07-09 16:15:06 +00:00
#include "includes.h"
2005-06-05 22:35:03 +00:00
#include "hostapd.h"
#include "common.h"
#include "eap_i.h"
struct eap_identity_data {
enum { CONTINUE, SUCCESS, FAILURE } state;
int pick_up;
};
static void * eap_identity_init(struct eap_sm *sm)
{
struct eap_identity_data *data;
2007-07-09 16:15:06 +00:00
data = wpa_zalloc(sizeof(*data));
2005-06-05 22:35:03 +00:00
if (data == NULL)
2007-07-09 16:15:06 +00:00
return NULL;
2005-06-05 22:35:03 +00:00
data->state = CONTINUE;
return data;
}
static void * eap_identity_initPickUp(struct eap_sm *sm)
{
struct eap_identity_data *data;
data = eap_identity_init(sm);
if (data) {
data->pick_up = 1;
}
return data;
}
static void eap_identity_reset(struct eap_sm *sm, void *priv)
{
struct eap_identity_data *data = priv;
free(data);
}
static u8 * eap_identity_buildReq(struct eap_sm *sm, void *priv, int id,
2007-07-09 16:15:06 +00:00
size_t *reqDataLen)
2005-06-05 22:35:03 +00:00
{
struct eap_identity_data *data = priv;
struct eap_hdr *req;
u8 *pos;
2006-03-07 05:47:04 +00:00
const char *req_data;
size_t req_data_len;
if (sm->eapol_cb->get_eap_req_id_text) {
req_data = sm->eapol_cb->get_eap_req_id_text(sm->eapol_ctx,
&req_data_len);
} else {
req_data = NULL;
req_data_len = 0;
}
2007-07-09 16:15:06 +00:00
req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, reqDataLen,
req_data_len, EAP_CODE_REQUEST, id, &pos);
2005-06-05 22:35:03 +00:00
if (req == NULL) {
wpa_printf(MSG_ERROR, "EAP-Identity: Failed to allocate "
"memory for request");
data->state = FAILURE;
return NULL;
}
2006-03-07 05:47:04 +00:00
if (req_data)
memcpy(pos, req_data, req_data_len);
2005-06-05 22:35:03 +00:00
return (u8 *) req;
}
static Boolean eap_identity_check(struct eap_sm *sm, void *priv,
u8 *respData, size_t respDataLen)
{
2007-07-09 16:15:06 +00:00
const u8 *pos;
2005-06-05 22:35:03 +00:00
size_t len;
2007-07-09 16:15:06 +00:00
pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY,
respData, respDataLen, &len);
if (pos == NULL) {
2005-06-05 22:35:03 +00:00
wpa_printf(MSG_INFO, "EAP-Identity: Invalid frame");
return TRUE;
}
return FALSE;
}
static void eap_identity_process(struct eap_sm *sm, void *priv,
u8 *respData, size_t respDataLen)
{
struct eap_identity_data *data = priv;
2007-07-09 16:15:06 +00:00
const u8 *pos;
size_t len;
2005-06-05 22:35:03 +00:00
if (data->pick_up) {
if (eap_identity_check(sm, data, respData, respDataLen)) {
wpa_printf(MSG_DEBUG, "EAP-Identity: failed to pick "
"up already started negotiation");
data->state = FAILURE;
return;
}
data->pick_up = 0;
}
2007-07-09 16:15:06 +00:00
pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY,
respData, respDataLen, &len);
if (pos == NULL)
return; /* Should not happen - frame already validated */
2005-06-05 22:35:03 +00:00
wpa_hexdump_ascii(MSG_DEBUG, "EAP-Identity: Peer identity", pos, len);
free(sm->identity);
sm->identity = malloc(len);
if (sm->identity == NULL) {
data->state = FAILURE;
} else {
memcpy(sm->identity, pos, len);
sm->identity_len = len;
data->state = SUCCESS;
}
}
static Boolean eap_identity_isDone(struct eap_sm *sm, void *priv)
{
struct eap_identity_data *data = priv;
return data->state != CONTINUE;
}
static Boolean eap_identity_isSuccess(struct eap_sm *sm, void *priv)
{
struct eap_identity_data *data = priv;
return data->state == SUCCESS;
}
2007-07-09 16:15:06 +00:00
int eap_server_identity_register(void)
2005-06-05 22:35:03 +00:00
{
2007-07-09 16:15:06 +00:00
struct eap_method *eap;
int ret;
eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
EAP_VENDOR_IETF, EAP_TYPE_IDENTITY,
"Identity");
if (eap == NULL)
return -1;
eap->init = eap_identity_init;
eap->initPickUp = eap_identity_initPickUp;
eap->reset = eap_identity_reset;
eap->buildReq = eap_identity_buildReq;
eap->check = eap_identity_check;
eap->process = eap_identity_process;
eap->isDone = eap_identity_isDone;
eap->isSuccess = eap_identity_isSuccess;
ret = eap_server_method_register(eap);
if (ret)
eap_server_method_free(eap);
return ret;
}