implemented server side support for EAP-TTLS
[strongswan.git] / src / libcharon / plugins / eap_ttls / eap_ttls_peer.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_peer.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_peer_t private_eap_ttls_peer_t;
25
26 /**
27 * Private data of an eap_ttls_peer_t object.
28 */
29 struct private_eap_ttls_peer_t {
30
31 /**
32 * Public eap_ttls_peer_t interface.
33 */
34 eap_ttls_peer_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_peer_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_REQUEST)
97 {
98 DBG1(DBG_IKE, "%N expected", eap_code_names, EAP_REQUEST);
99 in->destroy(in);
100 return FAILED;
101 }
102
103 if (this->method == NULL)
104 {
105 if (received_vendor)
106 {
107 DBG1(DBG_IKE, "server requested vendor specific EAP method %d-%d",
108 received_type, received_vendor);
109 }
110 else
111 {
112 DBG1(DBG_IKE, "server requested %N authentication",
113 eap_type_names, received_type);
114 }
115 this->method = charon->eap->create_instance(charon->eap,
116 received_type, received_vendor,
117 EAP_PEER, this->server, this->peer);
118 if (!this->method)
119 {
120 DBG1(DBG_IKE, "EAP method not supported");
121 this->out = eap_payload_create_nak(in->get_identifier(in));
122 in->destroy(in);
123 return NEED_MORE;
124 }
125 }
126
127 type = this->method->get_type(this->method, &vendor);
128
129 if (type != received_type || vendor != received_vendor)
130 {
131 DBG1(DBG_IKE, "received invalid EAP request");
132 in->destroy(in);
133 return FAILED;
134 }
135
136 if (this->method->process(this->method, in, &this->out) == NEED_MORE)
137 {
138 in->destroy(in);
139 return NEED_MORE;
140 }
141
142 if (vendor)
143 {
144 DBG1(DBG_IKE, "vendor specific EAP method %d-%d failed", type, vendor);
145 }
146 else
147 {
148 DBG1(DBG_IKE, "%N method failed", eap_type_names, type);
149 }
150 in->destroy(in);
151 return FAILED;
152 }
153
154 METHOD(tls_application_t, build, status_t,
155 private_eap_ttls_peer_t *this, tls_writer_t *writer)
156 {
157 chunk_t data;
158 eap_code_t code;
159 eap_type_t type;
160 u_int32_t vendor;
161
162 if (this->method == NULL && this->start_phase2)
163 {
164 /* generate an EAP Identity response */
165 this->method = charon->eap->create_instance(charon->eap, EAP_IDENTITY,
166 0, EAP_PEER, this->server, this->peer);
167 if (this->method == NULL)
168 {
169 DBG1(DBG_IKE, "EAP_IDENTITY method not available");
170 return FAILED;
171 }
172 this->method->process(this->method, NULL, &this->out);
173 this->method->destroy(this->method);
174 this->method = NULL;
175 this->start_phase2 = FALSE;
176 }
177
178 if (this->out)
179 {
180 code = this->out->get_code(this->out);
181 type = this->out->get_type(this->out, &vendor);
182 DBG1(DBG_IKE, "sending tunneled EAP-TTLS AVP [EAP/%N/%N]",
183 eap_code_short_names, code, eap_type_short_names, type);
184
185 /* get the raw EAP message data */
186 data = this->out->get_data(this->out);
187 this->avp->build(this->avp, writer, data);
188
189 this->out->destroy(this->out);
190 this->out = NULL;
191 }
192 return INVALID_STATE;
193 }
194
195 METHOD(tls_application_t, destroy, void,
196 private_eap_ttls_peer_t *this)
197 {
198 this->server->destroy(this->server);
199 this->peer->destroy(this->peer);
200 DESTROY_IF(this->method);
201 DESTROY_IF(this->out);
202 this->avp->destroy(this->avp);
203 free(this);
204 }
205
206 /**
207 * See header
208 */
209 eap_ttls_peer_t *eap_ttls_peer_create(identification_t *server,
210 identification_t *peer)
211 {
212 private_eap_ttls_peer_t *this;
213
214 INIT(this,
215 .public.application = {
216 .process = _process,
217 .build = _build,
218 .destroy = _destroy,
219 },
220 .server = server->clone(server),
221 .peer = peer->clone(peer),
222 .start_phase2 = TRUE,
223 .method = NULL,
224 .out = NULL,
225 .avp = eap_ttls_avp_create(),
226 );
227
228 return &this->public;
229 }