merged EAP framework from branch into trunk
[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 /* create an eap_method for the first call */
167 if (this->method == NULL)
168 {
169 DBG1(DBG_IKE, "EAP server requested %N authentication",
170 eap_type_names, type);
171 this->method = eap_method_create(type, EAP_PEER,
172 this->ike_sa->get_other_id(this->ike_sa),
173 this->ike_sa->get_my_id(this->ike_sa));
174 if (this->method == NULL)
175 {
176 DBG1(DBG_IKE, "EAP server requested unsupported "
177 "EAP method %N, sending EAP_NAK", eap_type_names, type);
178 *out = eap_payload_create_nak();
179 return NEED_MORE;
180 }
181 }
182
183 switch (this->method->process(this->method, in, out))
184 {
185 case NEED_MORE:
186 return NEED_MORE;
187 case SUCCESS:
188 if (this->method->get_msk(this->method, &this->msk) == SUCCESS)
189 {
190 DBG1(DBG_IKE, "EAP method %N succeded, MSK established",
191 eap_type_names, this->method->get_type(this->method));
192 this->msk = chunk_clone(this->msk);
193 return SUCCESS;
194 }
195 DBG1(DBG_IKE, "EAP method %N succeded, but no MSK established",
196 eap_type_names, this->method->get_type(this->method));
197 return FAILED;
198 case FAILED:
199 default:
200 DBG1(DBG_IKE, "EAP method %N failed",
201 eap_type_names, this->method->get_type(this->method));
202 return FAILED;
203 }
204 }
205
206 /**
207 * Processing method for a server
208 */
209 static status_t process_server(private_eap_authenticator_t *this,
210 eap_payload_t *in, eap_payload_t **out)
211 {
212 switch (this->method->process(this->method, in, out))
213 {
214 case NEED_MORE:
215 return NEED_MORE;
216 case SUCCESS:
217 if (this->method->get_msk(this->method, &this->msk) == SUCCESS)
218 {
219 DBG1(DBG_IKE, "EAP method %N succeded, MSK established",
220 eap_type_names, this->method->get_type(this->method));
221 this->msk = chunk_clone(this->msk);
222 *out = eap_payload_create_code(EAP_SUCCESS);
223 return SUCCESS;
224 }
225 DBG1(DBG_IKE, "EAP method %N succeded, but no MSK established",
226 eap_type_names, this->method->get_type(this->method));
227 *out = eap_payload_create_code(EAP_FAILURE);
228 return FAILED;
229 case FAILED:
230 default:
231 DBG1(DBG_IKE, "EAP method %N failed for peer %D",
232 eap_type_names, this->method->get_type(this->method),
233 this->ike_sa->get_other_id(this->ike_sa));
234 *out = eap_payload_create_code(EAP_FAILURE);
235 return FAILED;
236 }
237 }
238
239 /**
240 * Implementation of eap_authenticator_t.process
241 */
242 static status_t process(private_eap_authenticator_t *this, eap_payload_t *in,
243 eap_payload_t **out)
244 {
245 eap_code_t code = in->get_code(in);
246
247 switch (this->role)
248 {
249 case EAP_SERVER:
250 {
251 switch (code)
252 {
253 case EAP_RESPONSE:
254 {
255 return process_server(this, in, out);
256 }
257 default:
258 {
259 DBG1(DBG_IKE, "received %N, sending %N",
260 eap_code_names, code, eap_code_names, EAP_FAILURE);
261 *out = eap_payload_create_code(EAP_FAILURE);
262 return FAILED;
263 }
264 }
265 }
266 case EAP_PEER:
267 {
268 switch (code)
269 {
270 case EAP_REQUEST:
271 {
272 return process_peer(this, in, out);
273 }
274 case EAP_SUCCESS:
275 {
276 if (this->method->get_msk(this->method, &this->msk) == SUCCESS)
277 {
278 DBG1(DBG_IKE, "EAP method %N succeded, MSK established",
279 eap_type_names, this->method->get_type(this->method));
280 this->msk = chunk_clone(this->msk);
281 return SUCCESS;
282 }
283 DBG1(DBG_IKE, "EAP method %N succeded, but no MSK established",
284 eap_type_names, this->method->get_type(this->method));
285 return FAILED;
286 }
287 case EAP_FAILURE:
288 default:
289 {
290 DBG1(DBG_IKE, "received %N, EAP authentication failed",
291 eap_code_names, code);
292 return FAILED;
293 }
294 }
295 }
296 default:
297 {
298 return FAILED;
299 }
300 }
301 }
302
303 /**
304 * Implementation of authenticator_t.is_mutual.
305 */
306 static bool is_mutual(private_eap_authenticator_t *this)
307 {
308 if (this->method)
309 {
310 return this->method->is_mutual(this->method);
311 }
312 return FALSE;
313 }
314
315 /**
316 * Implementation of authenticator_t.destroy.
317 */
318 static void destroy(private_eap_authenticator_t *this)
319 {
320 DESTROY_IF(this->method);
321 chunk_free(&this->msk);
322 free(this);
323 }
324
325 /*
326 * Described in header.
327 */
328 eap_authenticator_t *eap_authenticator_create(ike_sa_t *ike_sa)
329 {
330 private_eap_authenticator_t *this = malloc_thing(private_eap_authenticator_t);
331
332 /* public functions */
333 this->public.authenticator_interface.verify = (status_t(*)(authenticator_t*,chunk_t,chunk_t,auth_payload_t*))verify;
334 this->public.authenticator_interface.build = (status_t(*)(authenticator_t*,chunk_t,chunk_t,auth_payload_t**))build;
335 this->public.authenticator_interface.destroy = (void(*)(authenticator_t*))destroy;
336
337 this->public.is_mutual = (bool(*)(eap_authenticator_t*))is_mutual;
338 this->public.initiate = (status_t(*)(eap_authenticator_t*,eap_type_t,eap_payload_t**))initiate;
339 this->public.process = (status_t(*)(eap_authenticator_t*,eap_payload_t*,eap_payload_t**))process;
340
341 /* private data */
342 this->ike_sa = ike_sa;
343 this->role = EAP_PEER;
344 this->method = NULL;
345 this->msk = chunk_empty;
346
347 return &this->public;
348 }