implemented server side support for EAP-TTLS
[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 <debug.h>
20 #include <daemon.h>
21
22 #include <sa/authenticators/eap/eap_method.h>
23
24 typedef struct private_eap_ttls_server_t private_eap_ttls_server_t;
25
26 /**
27 * Private data of an eap_ttls_server_t object.
28 */
29 struct private_eap_ttls_server_t {
30
31 /**
32 * Public eap_ttls_server_t interface.
33 */
34 eap_ttls_server_t public;
35
36 /**
37 * Server identity
38 */
39 identification_t *server;
40
41 /**
42 * Peer identity
43 */
44 identification_t *peer;
45
46 /**
47 * Current EAP-TTLS state
48 */
49 bool start_phase2;
50
51 /**
52 * Current phase 2 EAP method
53 */
54 eap_method_t *method;
55
56 /**
57 * Pending outbound EAP message
58 */
59 eap_payload_t *out;
60
61 /**
62 * AVP handler
63 */
64 eap_ttls_avp_t *avp;
65 };
66
67 METHOD(tls_application_t, process, status_t,
68 private_eap_ttls_server_t *this, tls_reader_t *reader)
69 {
70 chunk_t data;
71 status_t status;
72 payload_t *payload;
73 eap_payload_t *in;
74 eap_code_t code;
75 eap_type_t type, received_type;
76 u_int32_t vendor, received_vendor;
77
78 status = this->avp->process(this->avp, reader, &data);
79 if (status == FAILED)
80 {
81 return FAILED;
82 }
83 in = eap_payload_create_data(data);
84 payload = (payload_t*)in;
85
86 if (payload->verify(payload) != SUCCESS)
87 {
88 in->destroy(in);
89 return FAILED;
90 }
91 code = in->get_code(in);
92 received_type = in->get_type(in, &received_vendor);
93 DBG1(DBG_IKE, "received tunneled EAP-TTLS AVP [EAP/%N/%N]",
94 eap_code_short_names, code,
95 eap_type_short_names, received_type);
96 if (code != EAP_RESPONSE)
97 {
98 DBG1(DBG_IKE, "%N expected", eap_code_names, EAP_RESPONSE);
99 in->destroy(in);
100 return FAILED;
101 }
102
103 if (this->method)
104 {
105 type = this->method->get_type(this->method, &vendor);
106
107 if (type != received_type || vendor != received_vendor)
108 {
109 if (received_vendor == 0 && received_type == EAP_NAK)
110 {
111 DBG1(DBG_IKE, "peer does not support %N", eap_type_names, type);
112 }
113 else
114 {
115 DBG1(DBG_IKE, "received invalid EAP response");
116 }
117 in->destroy(in);
118 return FAILED;
119 }
120 }
121
122 if (!received_vendor && received_type == EAP_IDENTITY)
123 {
124 chunk_t eap_id;
125 char * eap_type_str;
126
127 if (this->method == NULL)
128 {
129 /* Received an EAP Identity response without a matching request */
130 this->method = charon->eap->create_instance(charon->eap,
131 EAP_IDENTITY, 0, EAP_SERVER,
132 this->server, this->peer);
133 if (this->method == NULL)
134 {
135 DBG1(DBG_IKE, "%N method not available",
136 eap_type_names, EAP_IDENTITY);
137 return FAILED;
138 }
139 }
140
141 if (this->method->process(this->method, in, &this->out) != SUCCESS)
142 {
143
144 DBG1(DBG_IKE, "%N method failed", eap_type_names, EAP_IDENTITY);
145 return FAILED;
146 }
147
148 if (this->method->get_msk(this->method, &eap_id) == SUCCESS)
149 {
150 this->peer->destroy(this->peer);
151 this->peer = identification_create_from_data(eap_id);
152 DBG1(DBG_IKE, "received EAP identity '%Y'", this->peer);
153 }
154
155 in->destroy(in);
156 this->method->destroy(this->method);
157 this->method = NULL;
158
159 /* Start Phase 2 of EAP-TTLS authentication */
160 eap_type_str = lib->settings->get_str(lib->settings,
161 "charon.plugins.eap-ttls.phase2_method", "md5");
162 type = eap_type_from_string(eap_type_str);
163 if (type == 0)
164 {
165 DBG1(DBG_IKE, "unrecognized phase2 method \"%s\"", eap_type_str);
166 return FAILED;
167 }
168 DBG1(DBG_IKE, "phase2 method %N selected", eap_type_names, type);
169
170 this->method = charon->eap->create_instance(charon->eap, type, 0,
171 EAP_SERVER, this->server, this->peer);
172 if (this->method == NULL)
173 {
174 DBG1(DBG_IKE, "%N method not available", eap_type_names, type);
175 return FAILED;
176 }
177 if (this->method->initiate(this->method, &this->out) == NEED_MORE)
178 {
179 return NEED_MORE;
180 }
181 else
182 {
183 DBG1(DBG_IKE, "%N method failed", eap_type_names, type);
184 return FAILED;
185 }
186 }
187
188 if (this->method == 0)
189 {
190 DBG1(DBG_IKE, "no %N phase2 method installed", eap_type_names, EAP_TTLS);
191 in->destroy(in);
192 return FAILED;
193 }
194
195 status = this->method->process(this->method, in, &this->out);
196 in->destroy(in);
197
198 switch (status)
199 {
200 case SUCCESS:
201 DBG1(DBG_IKE, "%N phase2 authentication of '%Y' with %N successful",
202 eap_type_names, EAP_TTLS, this->peer,
203 eap_type_names, type);
204 break;
205 case NEED_MORE:
206 break;
207 case FAILED:
208 default:
209 DBG1(DBG_IKE, "%N method failed", eap_type_names, type);
210 }
211 return status;
212 }
213
214 METHOD(tls_application_t, build, status_t,
215 private_eap_ttls_server_t *this, tls_writer_t *writer)
216 {
217 chunk_t data;
218 eap_code_t code;
219 eap_type_t type;
220 u_int32_t vendor;
221
222 if (this->out)
223 {
224 code = this->out->get_code(this->out);
225 type = this->out->get_type(this->out, &vendor);
226 DBG1(DBG_IKE, "sending tunneled EAP-TTLS AVP [EAP/%N/%N]",
227 eap_code_short_names, code, eap_type_short_names, type);
228
229 /* get the raw EAP message data */
230 data = this->out->get_data(this->out);
231 this->avp->build(this->avp, writer, data);
232
233 this->out->destroy(this->out);
234 this->out = NULL;
235 }
236 return INVALID_STATE;
237 }
238
239 METHOD(tls_application_t, destroy, void,
240 private_eap_ttls_server_t *this)
241 {
242 this->server->destroy(this->server);
243 this->peer->destroy(this->peer);
244 DESTROY_IF(this->method);
245 DESTROY_IF(this->out);
246 this->avp->destroy(this->avp);
247 free(this);
248 }
249
250 /**
251 * See header
252 */
253 eap_ttls_server_t *eap_ttls_server_create(identification_t *server,
254 identification_t *peer)
255 {
256 private_eap_ttls_server_t *this;
257
258 INIT(this,
259 .public.application = {
260 .process = _process,
261 .build = _build,
262 .destroy = _destroy,
263 },
264 .server = server->clone(server),
265 .peer = peer->clone(peer),
266 .start_phase2 = TRUE,
267 .method = NULL,
268 .out = NULL,
269 .avp = eap_ttls_avp_create(),
270 );
271
272 return &this->public;
273 }