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"
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
));
158 type
= this->ph2_method
->get_type(this->ph2_method
, &vendor
);
161 status
= this->ph2_method
->process(this->ph2_method
, in
, &this->out
);
167 this->ph2_method
->destroy(this->ph2_method
);
168 this->ph2_method
= NULL
;
169 /* fall through to NEED_MORE */
176 DBG1(DBG_IKE
, "vendor specific EAP method %d-%d failed",
181 DBG1(DBG_IKE
, "%N method failed", eap_type_names
, type
);
187 METHOD(tls_application_t
, build
, status_t
,
188 private_eap_peap_peer_t
*this, bio_writer_t
*writer
)
197 code
= this->out
->get_code(this->out
);
198 type
= this->out
->get_type(this->out
, &vendor
);
199 if (code
== EAP_REQUEST
|| code
== EAP_RESPONSE
)
201 DBG1(DBG_IKE
, "sending tunneled EAP-PEAP AVP [EAP/%N/%N]",
202 eap_code_short_names
, code
, eap_type_short_names
, type
);
206 DBG1(DBG_IKE
, "sending tunneled EAP-PEAP AVP [EAP/%N]",
207 eap_code_short_names
, code
);
210 /* get the raw EAP message data */
211 data
= this->out
->get_data(this->out
);
212 DBG3(DBG_IKE
, "%B", &data
);
213 this->avp
->build(this->avp
, writer
, data
);
215 this->out
->destroy(this->out
);
218 return INVALID_STATE
;
221 METHOD(tls_application_t
, destroy
, void,
222 private_eap_peap_peer_t
*this)
224 this->server
->destroy(this->server
);
225 this->peer
->destroy(this->peer
);
226 DESTROY_IF(this->ph2_method
);
227 DESTROY_IF(this->out
);
228 this->avp
->destroy(this->avp
);
235 eap_peap_peer_t
*eap_peap_peer_create(identification_t
*server
,
236 identification_t
*peer
,
237 eap_method_t
*eap_method
)
239 private_eap_peap_peer_t
*this;
249 .server
= server
->clone(server
),
250 .peer
= peer
->clone(peer
),
251 .ph1_method
= eap_method
,
252 .avp
= eap_peap_avp_create(FALSE
),
255 return &this->public;