Import hostapd 0.5.8
[dragonfly.git] / contrib / hostapd-0.5.8 / eap_identity.c
1 /*
2  * hostapd / EAP-Identity
3  * Copyright (c) 2004-2006, Jouni Malinen <j@w1.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "hostapd.h"
18 #include "common.h"
19 #include "eap_i.h"
20
21
22 struct eap_identity_data {
23         enum { CONTINUE, SUCCESS, FAILURE } state;
24         int pick_up;
25 };
26
27
28 static void * eap_identity_init(struct eap_sm *sm)
29 {
30         struct eap_identity_data *data;
31
32         data = wpa_zalloc(sizeof(*data));
33         if (data == NULL)
34                 return NULL;
35         data->state = CONTINUE;
36
37         return data;
38 }
39
40
41 static void * eap_identity_initPickUp(struct eap_sm *sm)
42 {
43         struct eap_identity_data *data;
44         data = eap_identity_init(sm);
45         if (data) {
46                 data->pick_up = 1;
47         }
48         return data;
49 }
50
51
52 static void eap_identity_reset(struct eap_sm *sm, void *priv)
53 {
54         struct eap_identity_data *data = priv;
55         free(data);
56 }
57
58
59 static u8 * eap_identity_buildReq(struct eap_sm *sm, void *priv, int id,
60                                   size_t *reqDataLen)
61 {
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;
67
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;
74         }
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;
82         }
83
84         if (req_data)
85                 memcpy(pos, req_data, req_data_len);
86
87         return (u8 *) req;
88 }
89
90
91 static Boolean eap_identity_check(struct eap_sm *sm, void *priv,
92                              u8 *respData, size_t respDataLen)
93 {
94         const u8 *pos;
95         size_t len;
96
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;
102         }
103
104         return FALSE;
105 }
106
107
108 static void eap_identity_process(struct eap_sm *sm, void *priv,
109                             u8 *respData, size_t respDataLen)
110 {
111         struct eap_identity_data *data = priv;
112         const u8 *pos;
113         size_t len;
114
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;
121                 }
122                 data->pick_up = 0;
123         }
124
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 */
129
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;
139         }
140 }
141
142
143 static Boolean eap_identity_isDone(struct eap_sm *sm, void *priv)
144 {
145         struct eap_identity_data *data = priv;
146         return data->state != CONTINUE;
147 }
148
149
150 static Boolean eap_identity_isSuccess(struct eap_sm *sm, void *priv)
151 {
152         struct eap_identity_data *data = priv;
153         return data->state == SUCCESS;
154 }
155
156
157 int eap_server_identity_register(void)
158 {
159         struct eap_method *eap;
160         int ret;
161
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;
167
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;
176
177         ret = eap_server_method_register(eap);
178         if (ret)
179                 eap_server_method_free(eap);
180         return ret;
181 }