2 * Copyright (C) 2011 Andreas Steffen
3 * Copyright (C) 2011 HSR Hochschule fuer Technik Rapperswil
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>.
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
16 #include "eap_peap_peer.h"
17 #include "eap_peap_avp.h"
19 #include <utils/debug.h>
22 typedef struct private_eap_peap_peer_t private_eap_peap_peer_t
;
25 * Private data of an eap_peap_peer_t object.
27 struct private_eap_peap_peer_t
{
30 * Public eap_peap_peer_t interface.
32 eap_peap_peer_t
public;
37 identification_t
*server
;
42 identification_t
*peer
;
45 * Outer phase 1 EAP method
47 eap_method_t
*ph1_method
;
50 * Current phase 2 EAP method
52 eap_method_t
*ph2_method
;
55 * Pending outbound EAP message
65 METHOD(tls_application_t
, process
, status_t
,
66 private_eap_peap_peer_t
*this, bio_reader_t
*reader
)
68 chunk_t data
= chunk_empty
;
73 eap_type_t type
, received_type
;
74 u_int32_t vendor
, received_vendor
;
76 status
= this->avp
->process(this->avp
, reader
, &data
,
77 this->ph1_method
->get_identifier(this->ph1_method
));
89 in
= eap_payload_create_data(data
);
90 DBG3(DBG_IKE
, "%B", &data
);
92 payload
= (payload_t
*)in
;
94 if (payload
->verify(payload
) != SUCCESS
)
100 code
= in
->get_code(in
);
101 if (code
== EAP_REQUEST
|| code
== EAP_RESPONSE
)
103 received_type
= in
->get_type(in
, &received_vendor
);
104 DBG1(DBG_IKE
, "received tunneled EAP-PEAP AVP [EAP/%N/%N]",
105 eap_code_short_names
, code
,
106 eap_type_short_names
, received_type
);
107 if (code
!= EAP_REQUEST
)
109 DBG1(DBG_IKE
, "%N expected", eap_code_names
, EAP_REQUEST
);
116 DBG1(DBG_IKE
, "received tunneled EAP-PEAP AVP [EAP/%N]",
117 eap_code_short_names
, code
);
118 this->out
= eap_payload_create_code(code
, in
->get_identifier(in
));
123 /* yet another phase2 authentication? */
124 if (this->ph2_method
)
126 type
= this->ph2_method
->get_type(this->ph2_method
, &vendor
);
128 if (type
!= received_type
|| vendor
!= received_vendor
)
130 this->ph2_method
->destroy(this->ph2_method
);
131 this->ph2_method
= NULL
;
135 if (this->ph2_method
== NULL
)
139 DBG1(DBG_IKE
, "server requested vendor specific EAP method %d-%d "
140 "(id 0x%02X", received_type
, received_vendor
,
141 in
->get_identifier(in
));
145 DBG1(DBG_IKE
, "server requested %N authentication (id 0x%02X)",
146 eap_type_names
, received_type
, in
->get_identifier(in
));
148 this->ph2_method
= charon
->eap
->create_instance(charon
->eap
,
149 received_type
, received_vendor
,
150 EAP_PEER
, this->server
, this->peer
);
151 if (!this->ph2_method
)
153 DBG1(DBG_IKE
, "EAP method not supported");
154 this->out
= eap_payload_create_nak(in
->get_identifier(in
), 0, 0,
155 in
->is_expanded(in
));
159 type
= this->ph2_method
->get_type(this->ph2_method
, &vendor
);
162 status
= this->ph2_method
->process(this->ph2_method
, in
, &this->out
);
168 this->ph2_method
->destroy(this->ph2_method
);
169 this->ph2_method
= NULL
;
170 /* fall through to NEED_MORE */
177 DBG1(DBG_IKE
, "vendor specific EAP method %d-%d failed",
182 DBG1(DBG_IKE
, "%N method failed", eap_type_names
, type
);
188 METHOD(tls_application_t
, build
, status_t
,
189 private_eap_peap_peer_t
*this, bio_writer_t
*writer
)
198 code
= this->out
->get_code(this->out
);
199 type
= this->out
->get_type(this->out
, &vendor
);
200 if (code
== EAP_REQUEST
|| code
== EAP_RESPONSE
)
202 DBG1(DBG_IKE
, "sending tunneled EAP-PEAP AVP [EAP/%N/%N]",
203 eap_code_short_names
, code
, eap_type_short_names
, type
);
207 DBG1(DBG_IKE
, "sending tunneled EAP-PEAP AVP [EAP/%N]",
208 eap_code_short_names
, code
);
211 /* get the raw EAP message data */
212 data
= this->out
->get_data(this->out
);
213 DBG3(DBG_IKE
, "%B", &data
);
214 this->avp
->build(this->avp
, writer
, data
);
216 this->out
->destroy(this->out
);
219 return INVALID_STATE
;
222 METHOD(tls_application_t
, destroy
, void,
223 private_eap_peap_peer_t
*this)
225 this->server
->destroy(this->server
);
226 this->peer
->destroy(this->peer
);
227 DESTROY_IF(this->ph2_method
);
228 DESTROY_IF(this->out
);
229 this->avp
->destroy(this->avp
);
236 eap_peap_peer_t
*eap_peap_peer_create(identification_t
*server
,
237 identification_t
*peer
,
238 eap_method_t
*eap_method
)
240 private_eap_peap_peer_t
*this;
250 .server
= server
->clone(server
),
251 .peer
= peer
->clone(peer
),
252 .ph1_method
= eap_method
,
253 .avp
= eap_peap_avp_create(FALSE
),
256 return &this->public;