for the time being assume a single request/response exchange for a given EAP method
[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 = chunk_empty;
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 switch (status)
80 {
81 case SUCCESS:
82 break;
83 case NEED_MORE:
84 DBG1(DBG_IKE, "need more AVP data");
85 return NEED_MORE;
86 case FAILED:
87 default:
88 return FAILED;
89 }
90 in = eap_payload_create_data(data);
91 chunk_free(&data);
92 payload = (payload_t*)in;
93
94 if (payload->verify(payload) != SUCCESS)
95 {
96 in->destroy(in);
97 return FAILED;
98 }
99 code = in->get_code(in);
100 received_type = in->get_type(in, &received_vendor);
101 DBG1(DBG_IKE, "received tunneled EAP-TTLS AVP [EAP/%N/%N]",
102 eap_code_short_names, code,
103 eap_type_short_names, received_type);
104 if (code != EAP_REQUEST)
105 {
106 DBG1(DBG_IKE, "%N expected", eap_code_names, EAP_REQUEST);
107 in->destroy(in);
108 return FAILED;
109 }
110
111 if (this->method == NULL)
112 {
113 if (received_vendor)
114 {
115 DBG1(DBG_IKE, "server requested vendor specific EAP method %d-%d",
116 received_type, received_vendor);
117 }
118 else
119 {
120 DBG1(DBG_IKE, "server requested %N authentication",
121 eap_type_names, received_type);
122 }
123 this->method = charon->eap->create_instance(charon->eap,
124 received_type, received_vendor,
125 EAP_PEER, this->server, this->peer);
126 if (!this->method)
127 {
128 DBG1(DBG_IKE, "EAP method not supported");
129 this->out = eap_payload_create_nak(in->get_identifier(in));
130 in->destroy(in);
131 return NEED_MORE;
132 }
133 }
134
135 type = this->method->get_type(this->method, &vendor);
136
137 if (type != received_type || vendor != received_vendor)
138 {
139 DBG1(DBG_IKE, "received invalid EAP request");
140 in->destroy(in);
141 return FAILED;
142 }
143
144 status = this->method->process(this->method, in, &this->out);
145 in->destroy(in);
146
147 switch (status)
148 {
149 case SUCCESS:
150 /* fall through to NEED_MORE since response must be sent */
151 case NEED_MORE:
152 /* TODO support multiple EAP request/response exchanges */
153 this->method->destroy(this->method);
154 this->method = NULL;
155 return NEED_MORE;
156 case FAILED:
157 default:
158 if (vendor)
159 {
160 DBG1(DBG_IKE, "vendor specific EAP method %d-%d failed",
161 type, vendor);
162 }
163 else
164 {
165 DBG1(DBG_IKE, "%N method failed", eap_type_names, type);
166 }
167 return FAILED;
168 }
169 }
170
171 METHOD(tls_application_t, build, status_t,
172 private_eap_ttls_peer_t *this, tls_writer_t *writer)
173 {
174 chunk_t data;
175 eap_code_t code;
176 eap_type_t type;
177 u_int32_t vendor;
178
179 if (this->method == NULL && this->start_phase2)
180 {
181 /* generate an EAP Identity response */
182 this->method = charon->eap->create_instance(charon->eap, EAP_IDENTITY,
183 0, EAP_PEER, this->server, this->peer);
184 if (this->method == NULL)
185 {
186 DBG1(DBG_IKE, "EAP_IDENTITY method not available");
187 return FAILED;
188 }
189 this->method->process(this->method, NULL, &this->out);
190 this->method->destroy(this->method);
191 this->method = NULL;
192 this->start_phase2 = FALSE;
193 }
194
195 if (this->out)
196 {
197 code = this->out->get_code(this->out);
198 type = this->out->get_type(this->out, &vendor);
199 DBG1(DBG_IKE, "sending tunneled EAP-TTLS AVP [EAP/%N/%N]",
200 eap_code_short_names, code, eap_type_short_names, type);
201
202 /* get the raw EAP message data */
203 data = this->out->get_data(this->out);
204 this->avp->build(this->avp, writer, data);
205
206 this->out->destroy(this->out);
207 this->out = NULL;
208 }
209 return INVALID_STATE;
210 }
211
212 METHOD(tls_application_t, destroy, void,
213 private_eap_ttls_peer_t *this)
214 {
215 this->server->destroy(this->server);
216 this->peer->destroy(this->peer);
217 DESTROY_IF(this->method);
218 DESTROY_IF(this->out);
219 this->avp->destroy(this->avp);
220 free(this);
221 }
222
223 /**
224 * See header
225 */
226 eap_ttls_peer_t *eap_ttls_peer_create(identification_t *server,
227 identification_t *peer)
228 {
229 private_eap_ttls_peer_t *this;
230
231 INIT(this,
232 .public = {
233 .application = {
234 .process = _process,
235 .build = _build,
236 .destroy = _destroy,
237 },
238 },
239 .server = server->clone(server),
240 .peer = peer->clone(peer),
241 .start_phase2 = TRUE,
242 .avp = eap_ttls_avp_create(),
243 );
244
245 return &this->public;
246 }