Fix an issue where the random number generator's random event injector
[dragonfly.git] / contrib / hostapd-0.5.8 / eap_identity.c
blobab6f26b1fea53400eae68abaeb08b78e3564e978
1 /*
2 * hostapd / EAP-Identity
3 * Copyright (c) 2004-2006, Jouni Malinen <j@w1.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
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "hostapd.h"
18 #include "common.h"
19 #include "eap_i.h"
22 struct eap_identity_data {
23 enum { CONTINUE, SUCCESS, FAILURE } state;
24 int pick_up;
28 static void * eap_identity_init(struct eap_sm *sm)
30 struct eap_identity_data *data;
32 data = wpa_zalloc(sizeof(*data));
33 if (data == NULL)
34 return NULL;
35 data->state = CONTINUE;
37 return data;
41 static void * eap_identity_initPickUp(struct eap_sm *sm)
43 struct eap_identity_data *data;
44 data = eap_identity_init(sm);
45 if (data) {
46 data->pick_up = 1;
48 return data;
52 static void eap_identity_reset(struct eap_sm *sm, void *priv)
54 struct eap_identity_data *data = priv;
55 free(data);
59 static u8 * eap_identity_buildReq(struct eap_sm *sm, void *priv, int id,
60 size_t *reqDataLen)
62 struct eap_identity_data *data = priv;
63 struct eap_hdr *req;
64 u8 *pos;
65 const char *req_data;
66 size_t req_data_len;
68 if (sm->eapol_cb->get_eap_req_id_text) {
69 req_data = sm->eapol_cb->get_eap_req_id_text(sm->eapol_ctx,
70 &req_data_len);
71 } else {
72 req_data = NULL;
73 req_data_len = 0;
75 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, reqDataLen,
76 req_data_len, EAP_CODE_REQUEST, id, &pos);
77 if (req == NULL) {
78 wpa_printf(MSG_ERROR, "EAP-Identity: Failed to allocate "
79 "memory for request");
80 data->state = FAILURE;
81 return NULL;
84 if (req_data)
85 memcpy(pos, req_data, req_data_len);
87 return (u8 *) req;
91 static Boolean eap_identity_check(struct eap_sm *sm, void *priv,
92 u8 *respData, size_t respDataLen)
94 const u8 *pos;
95 size_t len;
97 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY,
98 respData, respDataLen, &len);
99 if (pos == NULL) {
100 wpa_printf(MSG_INFO, "EAP-Identity: Invalid frame");
101 return TRUE;
104 return FALSE;
108 static void eap_identity_process(struct eap_sm *sm, void *priv,
109 u8 *respData, size_t respDataLen)
111 struct eap_identity_data *data = priv;
112 const u8 *pos;
113 size_t len;
115 if (data->pick_up) {
116 if (eap_identity_check(sm, data, respData, respDataLen)) {
117 wpa_printf(MSG_DEBUG, "EAP-Identity: failed to pick "
118 "up already started negotiation");
119 data->state = FAILURE;
120 return;
122 data->pick_up = 0;
125 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY,
126 respData, respDataLen, &len);
127 if (pos == NULL)
128 return; /* Should not happen - frame already validated */
130 wpa_hexdump_ascii(MSG_DEBUG, "EAP-Identity: Peer identity", pos, len);
131 free(sm->identity);
132 sm->identity = malloc(len);
133 if (sm->identity == NULL) {
134 data->state = FAILURE;
135 } else {
136 memcpy(sm->identity, pos, len);
137 sm->identity_len = len;
138 data->state = SUCCESS;
143 static Boolean eap_identity_isDone(struct eap_sm *sm, void *priv)
145 struct eap_identity_data *data = priv;
146 return data->state != CONTINUE;
150 static Boolean eap_identity_isSuccess(struct eap_sm *sm, void *priv)
152 struct eap_identity_data *data = priv;
153 return data->state == SUCCESS;
157 int eap_server_identity_register(void)
159 struct eap_method *eap;
160 int ret;
162 eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
163 EAP_VENDOR_IETF, EAP_TYPE_IDENTITY,
164 "Identity");
165 if (eap == NULL)
166 return -1;
168 eap->init = eap_identity_init;
169 eap->initPickUp = eap_identity_initPickUp;
170 eap->reset = eap_identity_reset;
171 eap->buildReq = eap_identity_buildReq;
172 eap->check = eap_identity_check;
173 eap->process = eap_identity_process;
174 eap->isDone = eap_identity_isDone;
175 eap->isSuccess = eap_identity_isSuccess;
177 ret = eap_server_method_register(eap);
178 if (ret)
179 eap_server_method_free(eap);
180 return ret;