88c2b88c6df5566240ca4a91f0b78b081ab3755d
[strongswan.git] / src / libcharon / plugins / eap_ttls / eap_ttls_server.c
1 /*
2 * Copyright (C) 2010 Andreas Steffen
3 * Copyright (C) 2010 HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "eap_ttls_server.h"
17 #include "eap_ttls_avp.h"
18
19 #include <utils/debug.h>
20 #include <daemon.h>
21
22 #include <sa/eap/eap_method.h>
23 #include <sa/eap/eap_inner_method.h>
24
25 typedef struct private_eap_ttls_server_t private_eap_ttls_server_t;
26
27 /**
28 * Private data of an eap_ttls_server_t object.
29 */
30 struct private_eap_ttls_server_t {
31
32 /**
33 * Public eap_ttls_server_t interface.
34 */
35 eap_ttls_server_t public;
36
37 /**
38 * Server identity
39 */
40 identification_t *server;
41
42 /**
43 * Peer identity
44 */
45 identification_t *peer;
46
47 /**
48 * Current EAP-TTLS phase2 state
49 */
50 bool start_phase2;
51
52 /**
53 * Current EAP-TTLS phase2 TNC state
54 */
55 bool start_phase2_tnc;
56
57 /**
58 * Current phase 2 EAP method
59 */
60 eap_method_t *method;
61
62 /**
63 * Pending outbound EAP message
64 */
65 eap_payload_t *out;
66
67 /**
68 * AVP handler
69 */
70 eap_ttls_avp_t *avp;
71 };
72
73 /**
74 * Start EAP client authentication protocol
75 */
76 static status_t start_phase2_auth(private_eap_ttls_server_t *this)
77 {
78 char *eap_type_str;
79 eap_type_t type;
80
81 eap_type_str = lib->settings->get_str(lib->settings,
82 "%s.plugins.eap-ttls.phase2_method", "md5",
83 lib->ns);
84 type = eap_type_from_string(eap_type_str);
85 if (type == 0)
86 {
87 DBG1(DBG_IKE, "unrecognized phase2 method \"%s\"", eap_type_str);
88 return FAILED;
89 }
90 DBG1(DBG_IKE, "phase2 method %N selected", eap_type_names, type);
91 this->method = charon->eap->create_instance(charon->eap, type, 0,
92 EAP_SERVER, this->server, this->peer);
93 if (this->method == NULL)
94 {
95 DBG1(DBG_IKE, "%N method not available", eap_type_names, type);
96 return FAILED;
97 }
98 if (this->method->initiate(this->method, &this->out) == NEED_MORE)
99 {
100 return NEED_MORE;
101 }
102 else
103 {
104 DBG1(DBG_IKE, "%N method failed", eap_type_names, type);
105 return FAILED;
106 }
107 }
108
109 /**
110 * If configured, start EAP-TNC protocol
111 */
112 static status_t start_phase2_tnc(private_eap_ttls_server_t *this,
113 eap_type_t auth_type)
114 {
115 eap_inner_method_t *inner_method;
116
117 if (this->start_phase2_tnc && lib->settings->get_bool(lib->settings,
118 "%s.plugins.eap-ttls.phase2_tnc", FALSE, lib->ns))
119 {
120 DBG1(DBG_IKE, "phase2 method %N selected", eap_type_names, EAP_TNC);
121 this->method = charon->eap->create_instance(charon->eap, EAP_TNC,
122 0, EAP_SERVER, this->server, this->peer);
123 if (this->method == NULL)
124 {
125 DBG1(DBG_IKE, "%N method not available", eap_type_names, EAP_TNC);
126 return FAILED;
127 }
128 inner_method = (eap_inner_method_t *)this->method;
129 inner_method->set_auth_type(inner_method, auth_type);
130
131 this->start_phase2_tnc = FALSE;
132 if (this->method->initiate(this->method, &this->out) == NEED_MORE)
133 {
134 return NEED_MORE;
135 }
136 else
137 {
138 DBG1(DBG_IKE, "%N method failed", eap_type_names, EAP_TNC);
139 return FAILED;
140 }
141 }
142 return SUCCESS;
143 }
144
145 METHOD(tls_application_t, process, status_t,
146 private_eap_ttls_server_t *this, bio_reader_t *reader)
147 {
148 chunk_t data = chunk_empty;
149 status_t status;
150 payload_t *payload;
151 eap_payload_t *in;
152 eap_code_t code;
153 eap_type_t type = EAP_NAK, received_type;
154 u_int32_t vendor, received_vendor;
155
156 status = this->avp->process(this->avp, reader, &data);
157 switch (status)
158 {
159 case SUCCESS:
160 break;
161 case NEED_MORE:
162 return NEED_MORE;
163 case FAILED:
164 default:
165 return FAILED;
166 }
167 in = eap_payload_create_data(data);
168 chunk_free(&data);
169 payload = (payload_t*)in;
170
171 if (payload->verify(payload) != SUCCESS)
172 {
173 in->destroy(in);
174 return FAILED;
175 }
176 code = in->get_code(in);
177 received_type = in->get_type(in, &received_vendor);
178 DBG1(DBG_IKE, "received tunneled EAP-TTLS AVP [EAP/%N/%N]",
179 eap_code_short_names, code,
180 eap_type_short_names, received_type);
181 if (code != EAP_RESPONSE)
182 {
183 DBG1(DBG_IKE, "%N expected", eap_code_names, EAP_RESPONSE);
184 in->destroy(in);
185 return FAILED;
186 }
187
188 if (this->method)
189 {
190 type = this->method->get_type(this->method, &vendor);
191
192 if (type != received_type || vendor != received_vendor)
193 {
194 if (received_vendor == 0 && received_type == EAP_NAK)
195 {
196 DBG1(DBG_IKE, "peer does not support %N", eap_type_names, type);
197 }
198 else
199 {
200 DBG1(DBG_IKE, "received invalid EAP response");
201 }
202 in->destroy(in);
203 return FAILED;
204 }
205 }
206
207 if (!received_vendor && received_type == EAP_IDENTITY)
208 {
209 chunk_t eap_id;
210
211 if (this->method == NULL)
212 {
213 /* Received an EAP Identity response without a matching request */
214 this->method = charon->eap->create_instance(charon->eap,
215 EAP_IDENTITY, 0, EAP_SERVER,
216 this->server, this->peer);
217 if (this->method == NULL)
218 {
219 DBG1(DBG_IKE, "%N method not available",
220 eap_type_names, EAP_IDENTITY);
221 return FAILED;
222 }
223 }
224
225 if (this->method->process(this->method, in, &this->out) != SUCCESS)
226 {
227
228 DBG1(DBG_IKE, "%N method failed", eap_type_names, EAP_IDENTITY);
229 return FAILED;
230 }
231
232 if (this->method->get_msk(this->method, &eap_id) == SUCCESS)
233 {
234 this->peer->destroy(this->peer);
235 this->peer = identification_create_from_data(eap_id);
236 DBG1(DBG_IKE, "received EAP identity '%Y'", this->peer);
237 }
238
239 in->destroy(in);
240 this->method->destroy(this->method);
241 this->method = NULL;
242
243 /* Start Phase 2 of EAP-TTLS authentication */
244 if (lib->settings->get_bool(lib->settings,
245 "%s.plugins.eap-ttls.request_peer_auth", FALSE, lib->ns))
246 {
247 return start_phase2_tnc(this, EAP_TLS);
248 }
249 else
250 {
251 return start_phase2_auth(this);
252 }
253 }
254
255 if (this->method == 0)
256 {
257 DBG1(DBG_IKE, "no %N phase2 method installed", eap_type_names, EAP_TTLS);
258 in->destroy(in);
259 return FAILED;
260 }
261
262 status = this->method->process(this->method, in, &this->out);
263 in->destroy(in);
264
265 switch (status)
266 {
267 case SUCCESS:
268 DBG1(DBG_IKE, "%N phase2 authentication of '%Y' with %N successful",
269 eap_type_names, EAP_TTLS, this->peer,
270 eap_type_names, type);
271 this->method->destroy(this->method);
272 this->method = NULL;
273
274 /* continue phase2 with EAP-TNC? */
275 return start_phase2_tnc(this, type);
276 case NEED_MORE:
277 break;
278 case FAILED:
279 default:
280 if (vendor)
281 {
282 DBG1(DBG_IKE, "vendor specific EAP method %d-%d failed",
283 type, vendor);
284 }
285 else
286 {
287 DBG1(DBG_IKE, "%N method failed", eap_type_names, type);
288 }
289 return FAILED;
290 }
291 return status;
292 }
293
294 METHOD(tls_application_t, build, status_t,
295 private_eap_ttls_server_t *this, bio_writer_t *writer)
296 {
297 chunk_t data;
298 eap_code_t code;
299 eap_type_t type;
300 u_int32_t vendor;
301
302 if (this->method == NULL && this->start_phase2 &&
303 lib->settings->get_bool(lib->settings,
304 "%s.plugins.eap-ttls.phase2_piggyback", FALSE, lib->ns))
305 {
306 /* generate an EAP Identity request which will be piggybacked right
307 * onto the TLS Finished message thus initiating EAP-TTLS phase2
308 */
309 this->method = charon->eap->create_instance(charon->eap, EAP_IDENTITY,
310 0, EAP_SERVER, this->server, this->peer);
311 if (this->method == NULL)
312 {
313 DBG1(DBG_IKE, "%N method not available",
314 eap_type_names, EAP_IDENTITY);
315 return FAILED;
316 }
317 this->method->initiate(this->method, &this->out);
318 this->start_phase2 = FALSE;
319 }
320
321 if (this->out)
322 {
323 code = this->out->get_code(this->out);
324 type = this->out->get_type(this->out, &vendor);
325 DBG1(DBG_IKE, "sending tunneled EAP-TTLS AVP [EAP/%N/%N]",
326 eap_code_short_names, code, eap_type_short_names, type);
327
328 /* get the raw EAP message data */
329 data = this->out->get_data(this->out);
330 this->avp->build(this->avp, writer, data);
331
332 this->out->destroy(this->out);
333 this->out = NULL;
334 }
335 return INVALID_STATE;
336 }
337
338 METHOD(tls_application_t, destroy, void,
339 private_eap_ttls_server_t *this)
340 {
341 this->server->destroy(this->server);
342 this->peer->destroy(this->peer);
343 DESTROY_IF(this->method);
344 DESTROY_IF(this->out);
345 this->avp->destroy(this->avp);
346 free(this);
347 }
348
349 /**
350 * See header
351 */
352 eap_ttls_server_t *eap_ttls_server_create(identification_t *server,
353 identification_t *peer)
354 {
355 private_eap_ttls_server_t *this;
356
357 INIT(this,
358 .public = {
359 .application = {
360 .process = _process,
361 .build = _build,
362 .destroy = _destroy,
363 },
364 },
365 .server = server->clone(server),
366 .peer = peer->clone(peer),
367 .start_phase2 = TRUE,
368 .start_phase2_tnc = TRUE,
369 .avp = eap_ttls_avp_create(),
370 );
371
372 return &this->public;
373 }