support of multiple certificates with same peer id
[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/peer_cfg.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 /**
65 * reuse shared key signature function from PSK authenticator
66 */
67 extern chunk_t build_shared_key_signature(chunk_t ike_sa_init, chunk_t nonce,
68 chunk_t secret, identification_t *id,
69 chunk_t skp, prf_t *prf);
70 /**
71 * Implementation of authenticator_t.verify.
72 */
73 static status_t verify(private_eap_authenticator_t *this, chunk_t ike_sa_init,
74 chunk_t my_nonce, auth_payload_t *auth_payload)
75 {
76 chunk_t auth_data, recv_auth_data, secret;
77 identification_t *other_id = this->ike_sa->get_other_id(this->ike_sa);
78
79 if (this->msk.len)
80 { /* use MSK if EAP method established one... */
81 secret = this->msk;
82 }
83 else
84 { /* ... or use SKp if not */
85 secret = this->ike_sa->get_skp_verify(this->ike_sa);
86 }
87 auth_data = build_shared_key_signature(ike_sa_init, my_nonce, secret,
88 other_id, this->ike_sa->get_skp_verify(this->ike_sa),
89 this->ike_sa->get_prf(this->ike_sa));
90
91 recv_auth_data = auth_payload->get_data(auth_payload);
92 if (!chunk_equals(auth_data, recv_auth_data))
93 {
94 DBG1(DBG_IKE, "verification of AUTH payload created from EAP MSK failed");
95 chunk_free(&auth_data);
96 return FAILED;
97 }
98 chunk_free(&auth_data);
99
100 DBG1(DBG_IKE, "authentication of '%D' with %N successful",
101 other_id, auth_method_names, AUTH_EAP);
102 return SUCCESS;
103 }
104
105 /**
106 * Implementation of authenticator_t.build.
107 */
108 static status_t build(private_eap_authenticator_t *this, chunk_t ike_sa_init,
109 chunk_t other_nonce, auth_payload_t **auth_payload)
110 {
111 chunk_t auth_data, secret;
112 identification_t *my_id = this->ike_sa->get_my_id(this->ike_sa);
113
114 DBG1(DBG_IKE, "authentication of '%D' (myself) with %N",
115 my_id, auth_method_names, AUTH_EAP);
116
117 if (this->msk.len)
118 { /* use MSK if EAP method established one... */
119 secret = this->msk;
120 }
121 else
122 { /* ... or use SKp if not */
123 secret = this->ike_sa->get_skp_build(this->ike_sa);
124 }
125 auth_data = build_shared_key_signature(ike_sa_init, other_nonce, secret,
126 my_id, this->ike_sa->get_skp_build(this->ike_sa),
127 this->ike_sa->get_prf(this->ike_sa));
128
129 *auth_payload = auth_payload_create();
130 (*auth_payload)->set_auth_method(*auth_payload, AUTH_PSK);
131 (*auth_payload)->set_data(*auth_payload, auth_data);
132 chunk_free(&auth_data);
133
134 return SUCCESS;
135 }
136
137 /**
138 * Implementation of eap_authenticator_t.initiate
139 */
140 static status_t initiate(private_eap_authenticator_t *this, eap_type_t type,
141 eap_payload_t **out)
142 {
143 /* if initiate() is called, role is always server */
144 this->role = EAP_SERVER;
145
146 if (type == 0)
147 {
148 DBG1(DBG_IKE,
149 "client requested EAP authentication, but configuration forbids it");
150 *out = eap_payload_create_code(EAP_FAILURE);
151 return FAILED;
152 }
153
154 DBG1(DBG_IKE, "requesting %N authentication", eap_type_names, type);
155 this->method = eap_method_create(type, this->role,
156 this->ike_sa->get_my_id(this->ike_sa),
157 this->ike_sa->get_other_id(this->ike_sa));
158
159 if (this->method == NULL)
160 {
161 DBG1(DBG_IKE, "configured EAP server method %N not supported, sending %N",
162 eap_type_names, type, eap_code_names, EAP_FAILURE);
163 *out = eap_payload_create_code(EAP_FAILURE);
164 return FAILED;
165 }
166 if (this->method->initiate(this->method, out) != NEED_MORE)
167 {
168 DBG1(DBG_IKE, "failed to initiate %N, sending %N",
169 eap_type_names, type, eap_code_names, EAP_FAILURE);
170 *out = eap_payload_create_code(EAP_FAILURE);
171 return FAILED;
172 }
173 return NEED_MORE;
174 }
175
176 /**
177 * Processing method for a peer
178 */
179 static status_t process_peer(private_eap_authenticator_t *this,
180 eap_payload_t *in, eap_payload_t **out)
181 {
182 eap_type_t type = in->get_type(in);
183
184 if (type == EAP_IDENTITY)
185 {
186 eap_method_t *method = eap_method_create(type, EAP_PEER,
187 this->ike_sa->get_other_id(this->ike_sa),
188 this->ike_sa->get_my_id(this->ike_sa));
189
190 if (method == NULL || method->process(method, in, out) != SUCCESS)
191 {
192 DBG1(DBG_IKE, "EAP server requested %N, but unable to process",
193 eap_type_names, type);
194 DESTROY_IF(method);
195 return FAILED;
196 }
197
198 DBG1(DBG_IKE, "EAP server requested %N, sending IKE identity",
199 eap_type_names, type);
200
201 method->destroy(method);
202 return NEED_MORE;
203 }
204
205 /* create an eap_method for the first call */
206 if (this->method == NULL)
207 {
208 DBG1(DBG_IKE, "EAP server requested %N authentication",
209 eap_type_names, type);
210 this->method = eap_method_create(type, EAP_PEER,
211 this->ike_sa->get_other_id(this->ike_sa),
212 this->ike_sa->get_my_id(this->ike_sa));
213 if (this->method == NULL)
214 {
215 DBG1(DBG_IKE, "EAP server requested unsupported "
216 "EAP method %N, sending EAP_NAK", eap_type_names, type);
217 *out = eap_payload_create_nak();
218 return NEED_MORE;
219 }
220 }
221
222 switch (this->method->process(this->method, in, out))
223 {
224 case NEED_MORE:
225 return NEED_MORE;
226 case SUCCESS:
227 DBG1(DBG_IKE, "EAP method %N succeded",
228 eap_type_names, this->method->get_type(this->method));
229 return SUCCESS;
230 case FAILED:
231 default:
232 DBG1(DBG_IKE, "EAP method %N failed",
233 eap_type_names, this->method->get_type(this->method));
234 return FAILED;
235 }
236 }
237
238 /**
239 * Processing method for a server
240 */
241 static status_t process_server(private_eap_authenticator_t *this,
242 eap_payload_t *in, eap_payload_t **out)
243 {
244 switch (this->method->process(this->method, in, out))
245 {
246 case NEED_MORE:
247 return NEED_MORE;
248 case SUCCESS:
249 if (this->method->get_msk(this->method, &this->msk) == SUCCESS)
250 {
251 DBG1(DBG_IKE, "EAP method %N succeded, MSK established",
252 eap_type_names, this->method->get_type(this->method));
253 this->msk = chunk_clone(this->msk);
254 }
255 else
256 {
257 DBG1(DBG_IKE, "EAP method %N succeded, no MSK established",
258 eap_type_names, this->method->get_type(this->method));
259 }
260 *out = eap_payload_create_code(EAP_SUCCESS);
261 return SUCCESS;
262 case FAILED:
263 default:
264 DBG1(DBG_IKE, "EAP method %N failed for peer %D",
265 eap_type_names, this->method->get_type(this->method),
266 this->ike_sa->get_other_id(this->ike_sa));
267 *out = eap_payload_create_code(EAP_FAILURE);
268 return FAILED;
269 }
270 }
271
272 /**
273 * Implementation of eap_authenticator_t.process
274 */
275 static status_t process(private_eap_authenticator_t *this, eap_payload_t *in,
276 eap_payload_t **out)
277 {
278 eap_code_t code = in->get_code(in);
279
280 switch (this->role)
281 {
282 case EAP_SERVER:
283 {
284 switch (code)
285 {
286 case EAP_RESPONSE:
287 {
288 return process_server(this, in, out);
289 }
290 default:
291 {
292 DBG1(DBG_IKE, "received %N, sending %N",
293 eap_code_names, code, eap_code_names, EAP_FAILURE);
294 *out = eap_payload_create_code(EAP_FAILURE);
295 return FAILED;
296 }
297 }
298 }
299 case EAP_PEER:
300 {
301 switch (code)
302 {
303 case EAP_REQUEST:
304 {
305 return process_peer(this, in, out);
306 }
307 case EAP_SUCCESS:
308 {
309 if (this->method->get_msk(this->method, &this->msk) == SUCCESS)
310 {
311 this->msk = chunk_clone(this->msk);
312 }
313 return SUCCESS;
314 }
315 case EAP_FAILURE:
316 default:
317 {
318 DBG1(DBG_IKE, "received %N, EAP authentication failed",
319 eap_code_names, code);
320 return FAILED;
321 }
322 }
323 }
324 default:
325 {
326 return FAILED;
327 }
328 }
329 }
330
331 /**
332 * Implementation of authenticator_t.is_mutual.
333 */
334 static bool is_mutual(private_eap_authenticator_t *this)
335 {
336 if (this->method)
337 {
338 return this->method->is_mutual(this->method);
339 }
340 return FALSE;
341 }
342
343 /**
344 * Implementation of authenticator_t.destroy.
345 */
346 static void destroy(private_eap_authenticator_t *this)
347 {
348 DESTROY_IF(this->method);
349 chunk_free(&this->msk);
350 free(this);
351 }
352
353 /*
354 * Described in header.
355 */
356 eap_authenticator_t *eap_authenticator_create(ike_sa_t *ike_sa)
357 {
358 private_eap_authenticator_t *this = malloc_thing(private_eap_authenticator_t);
359
360 /* public functions */
361 this->public.authenticator_interface.verify = (status_t(*)(authenticator_t*,chunk_t,chunk_t,auth_payload_t*))verify;
362 this->public.authenticator_interface.build = (status_t(*)(authenticator_t*,chunk_t,chunk_t,auth_payload_t**))build;
363 this->public.authenticator_interface.destroy = (void(*)(authenticator_t*))destroy;
364
365 this->public.is_mutual = (bool(*)(eap_authenticator_t*))is_mutual;
366 this->public.initiate = (status_t(*)(eap_authenticator_t*,eap_type_t,eap_payload_t**))initiate;
367 this->public.process = (status_t(*)(eap_authenticator_t*,eap_payload_t*,eap_payload_t**))process;
368
369 /* private data */
370 this->ike_sa = ike_sa;
371 this->role = EAP_PEER;
372 this->method = NULL;
373 this->msk = chunk_empty;
374
375 return &this->public;
376 }