• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * hostapd / EAP-Identity
3  * Copyright (c) 2004-2006, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 
11 #include "common.h"
12 #include "eap_i.h"
13 
14 
15 struct eap_identity_data {
16 	enum { CONTINUE, SUCCESS, FAILURE } state;
17 	int pick_up;
18 };
19 
20 
eap_identity_init(struct eap_sm * sm)21 static void * eap_identity_init(struct eap_sm *sm)
22 {
23 	struct eap_identity_data *data;
24 
25 	data = os_zalloc(sizeof(*data));
26 	if (data == NULL)
27 		return NULL;
28 	data->state = CONTINUE;
29 
30 	return data;
31 }
32 
33 
eap_identity_initPickUp(struct eap_sm * sm)34 static void * eap_identity_initPickUp(struct eap_sm *sm)
35 {
36 	struct eap_identity_data *data;
37 	data = eap_identity_init(sm);
38 	if (data) {
39 		data->pick_up = 1;
40 	}
41 	return data;
42 }
43 
44 
eap_identity_reset(struct eap_sm * sm,void * priv)45 static void eap_identity_reset(struct eap_sm *sm, void *priv)
46 {
47 	struct eap_identity_data *data = priv;
48 	os_free(data);
49 }
50 
51 
eap_identity_buildReq(struct eap_sm * sm,void * priv,u8 id)52 static struct wpabuf * eap_identity_buildReq(struct eap_sm *sm, void *priv,
53 					     u8 id)
54 {
55 	struct eap_identity_data *data = priv;
56 	struct wpabuf *req;
57 	const char *req_data;
58 	size_t req_data_len;
59 
60 #ifndef EXT_CODE_CROP
61 	if (sm->eapol_cb->get_eap_req_id_text) {
62 		req_data = sm->eapol_cb->get_eap_req_id_text(sm->eapol_ctx,
63 							     &req_data_len);
64 	} else {
65 		req_data = NULL;
66 		req_data_len = 0;
67 	}
68 #else
69 	(void)sm;
70 	req_data = NULL;
71 	req_data_len = 0;
72 #endif /* EXT_CODE_CROP */
73 	req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, req_data_len,
74 			    EAP_CODE_REQUEST, id);
75 	if (req == NULL) {
76 		wpa_error_log0(MSG_ERROR, "EAP-Identity: Failed to allocate "
77 			   "memory for request");
78 		data->state = FAILURE;
79 		return NULL;
80 	}
81 
82 	wpabuf_put_data(req, req_data, req_data_len);
83 
84 	return req;
85 }
86 
87 
eap_identity_check(struct eap_sm * sm,void * priv,struct wpabuf * respData)88 static bool eap_identity_check(struct eap_sm *sm, void *priv,
89 			       struct wpabuf *respData)
90 {
91 	const u8 *pos;
92 	size_t len;
93 
94 	pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY,
95 			       respData, &len);
96 	if (pos == NULL) {
97 		wpa_warning_log0(MSG_INFO, "EAP-Identity: Invalid frame");
98 		return true;
99 	}
100 
101 	return false;
102 }
103 
104 
eap_identity_process(struct eap_sm * sm,void * priv,struct wpabuf * respData)105 static void eap_identity_process(struct eap_sm *sm, void *priv,
106 				 struct wpabuf *respData)
107 {
108 	struct eap_identity_data *data = priv;
109 	const u8 *pos;
110 	size_t len;
111 	char *buf;
112 
113 	if (data->pick_up) {
114 		if (eap_identity_check(sm, data, respData)) {
115 			wpa_warning_log0(MSG_DEBUG, "EAP-Identity: failed to pick "
116 				   "up already started negotiation");
117 			data->state = FAILURE;
118 			return;
119 		}
120 		data->pick_up = 0;
121 	}
122 
123 	pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY,
124 			       respData, &len);
125 	if (pos == NULL)
126 		return; /* Should not happen - frame already validated */
127 
128 	wpa_hexdump_ascii(MSG_DEBUG, "EAP-Identity: Peer identity", pos, len);
129 	buf = os_malloc(len * 4 + 1);
130 	if (buf) {
131 		printf_encode(buf, len * 4 + 1, pos, len);
132 		eap_log_msg(sm, "EAP-Response/Identity '%s'", buf);
133 		os_free(buf);
134 	}
135 	if (sm->identity)
136 		sm->update_user = true;
137 	os_free(sm->identity);
138 	sm->identity = os_malloc(len ? len : 1);
139 	if (sm->identity == NULL) {
140 		data->state = FAILURE;
141 	} else {
142 		os_memcpy(sm->identity, pos, len);
143 		sm->identity_len = len;
144 		data->state = SUCCESS;
145 	}
146 }
147 
148 
eap_identity_isDone(struct eap_sm * sm,void * priv)149 static bool eap_identity_isDone(struct eap_sm *sm, void *priv)
150 {
151 	struct eap_identity_data *data = priv;
152 	return data->state != CONTINUE;
153 }
154 
155 
eap_identity_isSuccess(struct eap_sm * sm,void * priv)156 static bool eap_identity_isSuccess(struct eap_sm *sm, void *priv)
157 {
158 	struct eap_identity_data *data = priv;
159 	return data->state == SUCCESS;
160 }
161 
162 
eap_server_identity_register(void)163 int eap_server_identity_register(void)
164 {
165 	struct eap_method *eap;
166 
167 	eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
168 				      EAP_VENDOR_IETF, EAP_TYPE_IDENTITY,
169 				      "Identity");
170 	if (eap == NULL) {
171 		return -1;
172 	}
173 
174 	eap->init = eap_identity_init;
175 	eap->initPickUp = eap_identity_initPickUp;
176 	eap->reset = eap_identity_reset;
177 	eap->buildReq = eap_identity_buildReq;
178 	eap->check = eap_identity_check;
179 	eap->process = eap_identity_process;
180 	eap->isDone = eap_identity_isDone;
181 	eap->isSuccess = eap_identity_isSuccess;
182 	return eap_server_method_register(eap);
183 }
184