removed dublicated code in eap_authenticator
[strongswan.git] / src / charon / sa / authenticators / eap_authenticator.c
1 /**
2 * @file eap_authenticator.c
3 *
4 * @brief Implementation of eap_authenticator_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <string.h>
24
25 #include "eap_authenticator.h"
26
27 #include <daemon.h>
28 #include <config/policies/policy.h>
29 #include <sa/authenticators/eap/eap_method.h>
30
31 typedef struct private_eap_authenticator_t private_eap_authenticator_t;
32
33 /**
34 * Private data of an eap_authenticator_t object.
35 */
36 struct private_eap_authenticator_t {
37
38 /**
39 * Public authenticator_t interface.
40 */
41 eap_authenticator_t public;
42
43 /**
44 * Assigned IKE_SA
45 */
46 ike_sa_t *ike_sa;
47
48 /**
49 * Role of this authenticator, PEER or SERVER
50 */
51 eap_role_t role;
52
53 /**
54 * Current EAP method processing
55 */
56 eap_method_t *method;
57
58 /**
59 * MSK used to build and verify auth payload
60 */
61 chunk_t msk;
62 };
63
64 extern chunk_t build_shared_key_signature(chunk_t ike_sa_init, chunk_t nonce,
65 chunk_t secret, identification_t *id,
66 prf_t *prf);
67
68 /**
69 * Implementation of authenticator_t.verify.
70 */
71 static status_t verify(private_eap_authenticator_t *this, chunk_t ike_sa_init,
72 chunk_t my_nonce, auth_payload_t *auth_payload)
73 {
74 chunk_t auth_data, recv_auth_data;
75 identification_t *other_id = this->ike_sa->get_other_id(this->ike_sa);
76 prf_t *prf = this->ike_sa->get_auth_verify(this->ike_sa);
77
78 auth_data = build_shared_key_signature(ike_sa_init, my_nonce, this->msk,
79 other_id, prf);
80
81 recv_auth_data = auth_payload->get_data(auth_payload);
82 if (!chunk_equals(auth_data, recv_auth_data))
83 {
84 DBG1(DBG_IKE, "verification of AUTH payload created from EAP MSK failed");
85 chunk_free(&auth_data);
86 return FAILED;
87 }
88 chunk_free(&auth_data);
89
90 DBG1(DBG_IKE, "authentication of '%D' with %N successful",
91 other_id, auth_method_names, AUTH_EAP);
92 return SUCCESS;
93 }
94
95 /**
96 * Implementation of authenticator_t.build.
97 */
98 static status_t build(private_eap_authenticator_t *this, chunk_t ike_sa_init,
99 chunk_t other_nonce, auth_payload_t **auth_payload)
100 {
101 chunk_t auth_data;
102 identification_t *my_id = this->ike_sa->get_my_id(this->ike_sa);
103 prf_t *prf = this->ike_sa->get_auth_build(this->ike_sa);
104
105 DBG1(DBG_IKE, "authentication of '%D' (myself) with %N",
106 my_id, auth_method_names, AUTH_EAP);
107
108 auth_data = build_shared_key_signature(ike_sa_init, other_nonce,
109 this->msk, my_id, prf);
110
111 *auth_payload = auth_payload_create();
112 (*auth_payload)->set_auth_method(*auth_payload, AUTH_PSK);
113 (*auth_payload)->set_data(*auth_payload, auth_data);
114 chunk_free(&auth_data);
115
116 return SUCCESS;
117 }
118
119 /**
120 * Implementation of eap_authenticator_t.initiate
121 */
122 static status_t initiate(private_eap_authenticator_t *this, eap_type_t type,
123 eap_payload_t **out)
124 {
125 /* if initiate() is called, role is always server */
126 this->role = EAP_SERVER;
127
128 if (type == 0)
129 {
130 DBG1(DBG_IKE,
131 "client requested EAP authentication, but configuration forbids it");
132 *out = eap_payload_create_code(EAP_FAILURE);
133 return FAILED;
134 }
135
136 DBG1(DBG_IKE, "requesting %N authentication", eap_type_names, type);
137 this->method = eap_method_create(type, this->role,
138 this->ike_sa->get_my_id(this->ike_sa),
139 this->ike_sa->get_other_id(this->ike_sa));
140
141 if (this->method == NULL)
142 {
143 DBG1(DBG_IKE, "configured EAP server method %N not supported, sending %N",
144 eap_type_names, type, eap_code_names, EAP_FAILURE);
145 *out = eap_payload_create_code(EAP_FAILURE);
146 return FAILED;
147 }
148 if (this->method->initiate(this->method, out) != NEED_MORE)
149 {
150 DBG1(DBG_IKE, "failed to initiate %N, sending %N",
151 eap_type_names, type, eap_code_names, EAP_FAILURE);
152 *out = eap_payload_create_code(EAP_FAILURE);
153 return FAILED;
154 }
155 return NEED_MORE;
156 }
157
158 /**
159 * Processing method for a peer
160 */
161 static status_t process_peer(private_eap_authenticator_t *this,
162 eap_payload_t *in, eap_payload_t **out)
163 {
164 eap_type_t type = in->get_type(in);
165
166 if (type == EAP_IDENTITY)
167 {
168 eap_method_t *method = eap_method_create(type, EAP_PEER,
169 this->ike_sa->get_other_id(this->ike_sa),
170 this->ike_sa->get_my_id(this->ike_sa));
171
172 if (method == NULL || method->process(method, in, out) != SUCCESS)
173 {
174 DBG1(DBG_IKE, "EAP server requested %N, but unable to process",
175 eap_type_names, type);
176 DESTROY_IF(method);
177 return FAILED;
178 }
179
180 DBG1(DBG_IKE, "EAP server requested %N, sending IKE identity",
181 eap_type_names, type);
182
183 method->destroy(method);
184 return NEED_MORE;
185 }
186
187 /* create an eap_method for the first call */
188 if (this->method == NULL)
189 {
190 DBG1(DBG_IKE, "EAP server requested %N authentication",
191 eap_type_names, type);
192 this->method = eap_method_create(type, EAP_PEER,
193 this->ike_sa->get_other_id(this->ike_sa),
194 this->ike_sa->get_my_id(this->ike_sa));
195 if (this->method == NULL)
196 {
197 DBG1(DBG_IKE, "EAP server requested unsupported "
198 "EAP method %N, sending EAP_NAK", eap_type_names, type);
199 *out = eap_payload_create_nak();
200 return NEED_MORE;
201 }
202 }
203
204 switch (this->method->process(this->method, in, out))
205 {
206 case NEED_MORE:
207 return NEED_MORE;
208 case SUCCESS:
209 DBG1(DBG_IKE, "EAP method %N succeded",
210 eap_type_names, this->method->get_type(this->method));
211 return SUCCESS;
212 case FAILED:
213 default:
214 DBG1(DBG_IKE, "EAP method %N failed",
215 eap_type_names, this->method->get_type(this->method));
216 return FAILED;
217 }
218 }
219
220 /**
221 * Processing method for a server
222 */
223 static status_t process_server(private_eap_authenticator_t *this,
224 eap_payload_t *in, eap_payload_t **out)
225 {
226 switch (this->method->process(this->method, in, out))
227 {
228 case NEED_MORE:
229 return NEED_MORE;
230 case SUCCESS:
231 if (this->method->get_msk(this->method, &this->msk) == SUCCESS)
232 {
233 DBG1(DBG_IKE, "EAP method %N succeded, MSK established",
234 eap_type_names, this->method->get_type(this->method));
235 this->msk = chunk_clone(this->msk);
236 *out = eap_payload_create_code(EAP_SUCCESS);
237 return SUCCESS;
238 }
239 DBG1(DBG_IKE, "EAP method %N succeded, but no MSK established",
240 eap_type_names, this->method->get_type(this->method));
241 *out = eap_payload_create_code(EAP_FAILURE);
242 return FAILED;
243 case FAILED:
244 default:
245 DBG1(DBG_IKE, "EAP method %N failed for peer %D",
246 eap_type_names, this->method->get_type(this->method),
247 this->ike_sa->get_other_id(this->ike_sa));
248 *out = eap_payload_create_code(EAP_FAILURE);
249 return FAILED;
250 }
251 }
252
253 /**
254 * Implementation of eap_authenticator_t.process
255 */
256 static status_t process(private_eap_authenticator_t *this, eap_payload_t *in,
257 eap_payload_t **out)
258 {
259 eap_code_t code = in->get_code(in);
260
261 switch (this->role)
262 {
263 case EAP_SERVER:
264 {
265 switch (code)
266 {
267 case EAP_RESPONSE:
268 {
269 return process_server(this, in, out);
270 }
271 default:
272 {
273 DBG1(DBG_IKE, "received %N, sending %N",
274 eap_code_names, code, eap_code_names, EAP_FAILURE);
275 *out = eap_payload_create_code(EAP_FAILURE);
276 return FAILED;
277 }
278 }
279 }
280 case EAP_PEER:
281 {
282 switch (code)
283 {
284 case EAP_REQUEST:
285 {
286 return process_peer(this, in, out);
287 }
288 case EAP_SUCCESS:
289 {
290 if (this->method->get_msk(this->method, &this->msk) == SUCCESS)
291 {
292 this->msk = chunk_clone(this->msk);
293 return SUCCESS;
294 }
295 DBG1(DBG_IKE, "EAP method %N has no MSK established",
296 eap_type_names, this->method->get_type(this->method));
297 return FAILED;
298 }
299 case EAP_FAILURE:
300 default:
301 {
302 DBG1(DBG_IKE, "received %N, EAP authentication failed",
303 eap_code_names, code);
304 return FAILED;
305 }
306 }
307 }
308 default:
309 {
310 return FAILED;
311 }
312 }
313 }
314
315 /**
316 * Implementation of authenticator_t.is_mutual.
317 */
318 static bool is_mutual(private_eap_authenticator_t *this)
319 {
320 if (this->method)
321 {
322 return this->method->is_mutual(this->method);
323 }
324 return FALSE;
325 }
326
327 /**
328 * Implementation of authenticator_t.destroy.
329 */
330 static void destroy(private_eap_authenticator_t *this)
331 {
332 DESTROY_IF(this->method);
333 chunk_free(&this->msk);
334 free(this);
335 }
336
337 /*
338 * Described in header.
339 */
340 eap_authenticator_t *eap_authenticator_create(ike_sa_t *ike_sa)
341 {
342 private_eap_authenticator_t *this = malloc_thing(private_eap_authenticator_t);
343
344 /* public functions */
345 this->public.authenticator_interface.verify = (status_t(*)(authenticator_t*,chunk_t,chunk_t,auth_payload_t*))verify;
346 this->public.authenticator_interface.build = (status_t(*)(authenticator_t*,chunk_t,chunk_t,auth_payload_t**))build;
347 this->public.authenticator_interface.destroy = (void(*)(authenticator_t*))destroy;
348
349 this->public.is_mutual = (bool(*)(eap_authenticator_t*))is_mutual;
350 this->public.initiate = (status_t(*)(eap_authenticator_t*,eap_type_t,eap_payload_t**))initiate;
351 this->public.process = (status_t(*)(eap_authenticator_t*,eap_payload_t*,eap_payload_t**))process;
352
353 /* private data */
354 this->ike_sa = ike_sa;
355 this->role = EAP_PEER;
356 this->method = NULL;
357 this->msk = chunk_empty;
358
359 return &this->public;
360 }