2 * Copyright (C) 2011 Andreas Steffen
4 * Copyright (C) secunet Security Networks AG
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #include "eap_peap_server.h"
18 #include "eap_peap_avp.h"
20 #include <utils/debug.h>
23 typedef struct private_eap_peap_server_t private_eap_peap_server_t
;
26 * Private data of an eap_peap_server_t object.
28 struct private_eap_peap_server_t
{
31 * Public eap_peap_server_t interface.
33 eap_peap_server_t
public;
38 identification_t
*server
;
43 identification_t
*peer
;
46 * Current EAP-PEAP phase2 state
51 * Current EAP-PEAP phase2 TNC state
53 bool start_phase2_tnc
;
56 * Starts phase 2 with EAP Identity request
61 * Final EAP-PEAP phase2 result
63 eap_code_t phase2_result
;
66 * Outer phase 1 EAP method
68 eap_method_t
*ph1_method
;
71 * Current phase 2 EAP method
73 eap_method_t
*ph2_method
;
76 * Pending outbound EAP message
87 * Start EAP client authentication protocol
89 static status_t
start_phase2_auth(private_eap_peap_server_t
*this)
94 eap_type_str
= lib
->settings
->get_str(lib
->settings
,
95 "%s.plugins.eap-peap.phase2_method", "mschapv2",
97 type
= eap_type_from_string(eap_type_str
);
100 DBG1(DBG_IKE
, "unrecognized phase2 method \"%s\"", eap_type_str
);
103 DBG1(DBG_IKE
, "phase2 method %N selected", eap_type_names
, type
);
104 this->ph2_method
= charon
->eap
->create_instance(charon
->eap
, type
, 0,
105 EAP_SERVER
, this->server
, this->peer
);
106 if (this->ph2_method
== NULL
)
108 DBG1(DBG_IKE
, "%N method not available", eap_type_names
, type
);
112 /* synchronize EAP message identifiers of inner protocol with outer */
113 this->ph2_method
->set_identifier(this->ph2_method
,
114 this->ph1_method
->get_identifier(this->ph1_method
) + 1);
116 if (this->ph2_method
->initiate(this->ph2_method
, &this->out
) == NEED_MORE
)
122 DBG1(DBG_IKE
, "%N method failed", eap_type_names
, type
);
128 * If configured, start EAP-TNC protocol
130 static status_t
start_phase2_tnc(private_eap_peap_server_t
*this)
132 if (this->start_phase2_tnc
&& lib
->settings
->get_bool(lib
->settings
,
133 "%s.plugins.eap-peap.phase2_tnc", FALSE
, lib
->ns
))
135 DBG1(DBG_IKE
, "phase2 method %N selected", eap_type_names
, EAP_TNC
);
136 this->ph2_method
= charon
->eap
->create_instance(charon
->eap
, EAP_TNC
,
137 0, EAP_SERVER
, this->server
, this->peer
);
138 if (this->ph2_method
== NULL
)
140 DBG1(DBG_IKE
, "%N method not available", eap_type_names
, EAP_TNC
);
143 this->start_phase2_tnc
= FALSE
;
145 /* synchronize EAP message identifiers of inner protocol with outer */
146 this->ph2_method
->set_identifier(this->ph2_method
,
147 this->ph1_method
->get_identifier(this->ph1_method
) + 1);
149 if (this->ph2_method
->initiate(this->ph2_method
, &this->out
) == NEED_MORE
)
155 DBG1(DBG_IKE
, "%N method failed", eap_type_names
, EAP_TNC
);
162 METHOD(tls_application_t
, process
, status_t
,
163 private_eap_peap_server_t
*this, bio_reader_t
*reader
)
165 chunk_t data
= chunk_empty
;
170 eap_type_t type
= EAP_NAK
, received_type
;
171 uint32_t vendor
, received_vendor
;
173 status
= this->avp
->process(this->avp
, reader
, &data
,
174 this->ph1_method
->get_identifier(this->ph1_method
));
186 in
= eap_payload_create_data(data
);
187 DBG3(DBG_IKE
, "%B", &data
);
189 payload
= (payload_t
*)in
;
191 if (payload
->verify(payload
) != SUCCESS
)
197 code
= in
->get_code(in
);
198 if (code
== EAP_REQUEST
|| code
== EAP_RESPONSE
)
200 received_type
= in
->get_type(in
, &received_vendor
);
201 DBG1(DBG_IKE
, "received tunneled EAP-PEAP AVP [EAP/%N/%N]",
202 eap_code_short_names
, code
,
203 eap_type_short_names
, received_type
);
204 if (code
!= EAP_RESPONSE
)
206 DBG1(DBG_IKE
, "%N expected", eap_code_names
, EAP_RESPONSE
);
213 DBG1(DBG_IKE
, "received tunneled EAP-PEAP AVP [EAP/%N]",
214 eap_code_short_names
, code
);
216 /* if EAP_SUCCESS check if to continue phase2 with EAP-TNC */
217 return (this->phase2_result
== EAP_SUCCESS
&& code
== EAP_SUCCESS
) ?
218 start_phase2_tnc(this) : FAILED
;
221 if (this->ph2_method
)
223 type
= this->ph2_method
->get_type(this->ph2_method
, &vendor
);
225 if (type
!= received_type
|| vendor
!= received_vendor
)
227 if (received_vendor
== 0 && received_type
== EAP_NAK
)
229 DBG1(DBG_IKE
, "peer does not support %N", eap_type_names
, type
);
233 DBG1(DBG_IKE
, "received invalid EAP response");
240 if (!received_vendor
&& received_type
== EAP_IDENTITY
)
244 if (this->ph2_method
== NULL
)
246 /* Received an EAP Identity response without a matching request */
247 this->ph2_method
= charon
->eap
->create_instance(charon
->eap
,
248 EAP_IDENTITY
, 0, EAP_SERVER
,
249 this->server
, this->peer
);
250 if (this->ph2_method
== NULL
)
252 DBG1(DBG_IKE
, "%N method not available",
253 eap_type_names
, EAP_IDENTITY
);
259 if (this->ph2_method
->process(this->ph2_method
, in
, &this->out
) != SUCCESS
)
262 DBG1(DBG_IKE
, "%N method failed", eap_type_names
, EAP_IDENTITY
);
267 if (this->ph2_method
->get_msk(this->ph2_method
, &eap_id
) == SUCCESS
)
269 this->peer
->destroy(this->peer
);
270 this->peer
= identification_create_from_data(eap_id
);
271 DBG1(DBG_IKE
, "received EAP identity '%Y'", this->peer
);
275 this->ph2_method
->destroy(this->ph2_method
);
276 this->ph2_method
= NULL
;
278 /* Start Phase 2 of EAP-PEAP authentication */
279 if (lib
->settings
->get_bool(lib
->settings
,
280 "%s.plugins.eap-peap.request_peer_auth", FALSE
, lib
->ns
))
282 return start_phase2_tnc(this);
286 return start_phase2_auth(this);
290 if (this->ph2_method
== 0)
292 DBG1(DBG_IKE
, "no %N phase2 method installed", eap_type_names
, EAP_PEAP
);
297 status
= this->ph2_method
->process(this->ph2_method
, in
, &this->out
);
303 DBG1(DBG_IKE
, "%N phase2 authentication of '%Y' with %N successful",
304 eap_type_names
, EAP_PEAP
, this->peer
,
305 eap_type_names
, type
);
306 this->ph2_method
->destroy(this->ph2_method
);
307 this->ph2_method
= NULL
;
309 /* EAP-PEAP requires the sending of an inner EAP_SUCCESS message */
310 this->phase2_result
= EAP_SUCCESS
;
311 this->out
= eap_payload_create_code(this->phase2_result
, 1 +
312 this->ph1_method
->get_identifier(this->ph1_method
));
320 DBG1(DBG_IKE
, "vendor specific EAP method %d-%d failed",
325 DBG1(DBG_IKE
, "%N method failed", eap_type_names
, type
);
327 /* EAP-PEAP requires the sending of an inner EAP_FAILURE message */
328 this->phase2_result
= EAP_FAILURE
;
329 this->out
= eap_payload_create_code(this->phase2_result
, 1 +
330 this->ph1_method
->get_identifier(this->ph1_method
));
336 METHOD(tls_application_t
, build
, status_t
,
337 private_eap_peap_server_t
*this, bio_writer_t
*writer
)
344 if (this->ph2_method
== NULL
&& this->start_phase2
&& this->start_phase2_id
)
347 * Start Phase 2 with an EAP Identity request either piggybacked right
348 * onto the TLS Finished payload or delayed after the reception of an
349 * empty EAP Acknowledge message.
351 this->ph2_method
= charon
->eap
->create_instance(charon
->eap
, EAP_IDENTITY
,
352 0, EAP_SERVER
, this->server
, this->peer
);
353 if (this->ph2_method
== NULL
)
355 DBG1(DBG_IKE
, "%N method not available",
356 eap_type_names
, EAP_IDENTITY
);
360 /* synchronize EAP message identifiers of inner protocol with outer */
361 this->ph2_method
->set_identifier(this->ph2_method
,
362 this->ph1_method
->get_identifier(this->ph1_method
));
364 this->ph2_method
->initiate(this->ph2_method
, &this->out
);
365 this->start_phase2
= FALSE
;
368 this->start_phase2_id
= TRUE
;
372 code
= this->out
->get_code(this->out
);
373 type
= this->out
->get_type(this->out
, &vendor
);
374 if (code
== EAP_REQUEST
|| code
== EAP_RESPONSE
)
376 DBG1(DBG_IKE
, "sending tunneled EAP-PEAP AVP [EAP/%N/%N]",
377 eap_code_short_names
, code
, eap_type_short_names
, type
);
381 DBG1(DBG_IKE
, "sending tunneled EAP-PEAP AVP [EAP/%N]",
382 eap_code_short_names
, code
);
385 /* get the raw EAP message data */
386 data
= this->out
->get_data(this->out
);
387 DBG3(DBG_IKE
, "%B", &data
);
388 this->avp
->build(this->avp
, writer
, data
);
390 this->out
->destroy(this->out
);
393 return INVALID_STATE
;
396 METHOD(tls_application_t
, destroy
, void,
397 private_eap_peap_server_t
*this)
399 this->server
->destroy(this->server
);
400 this->peer
->destroy(this->peer
);
401 DESTROY_IF(this->ph2_method
);
402 DESTROY_IF(this->out
);
403 this->avp
->destroy(this->avp
);
410 eap_peap_server_t
*eap_peap_server_create(identification_t
*server
,
411 identification_t
*peer
,
412 eap_method_t
*eap_method
)
414 private_eap_peap_server_t
*this;
424 .server
= server
->clone(server
),
425 .peer
= peer
->clone(peer
),
426 .ph1_method
= eap_method
,
427 .start_phase2
= TRUE
,
428 .start_phase2_tnc
= TRUE
,
429 .start_phase2_id
= lib
->settings
->get_bool(lib
->settings
,
430 "%s.plugins.eap-peap.phase2_piggyback",
432 .phase2_result
= EAP_FAILURE
,
433 .avp
= eap_peap_avp_create(TRUE
),
436 return &this->public;