2 * Copyright (C) 2010 Andreas Steffen
3 * Copyright (C) 2010 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_ttls_peer.h"
17 #include "eap_ttls_avp.h"
22 #include <sa/authenticators/eap/eap_method.h>
24 typedef struct private_eap_ttls_peer_t private_eap_ttls_peer_t
;
27 * Private data of an eap_ttls_peer_t object.
29 struct private_eap_ttls_peer_t
{
32 * Public eap_ttls_peer_t interface.
34 eap_ttls_peer_t
public;
39 identification_t
*server
;
44 identification_t
*peer
;
47 * Current EAP-TTLS state
52 * Current phase 2 EAP method
57 * Pending outbound EAP message
70 typedef struct __attribute__((packed
)) {
78 #define MAX_RADIUS_ATTRIBUTE_SIZE 253
80 METHOD(tls_application_t
, process
, status_t
,
81 private_eap_ttls_peer_t
*this, tls_reader_t
*reader
)
83 chunk_t avp_data
= chunk_empty
;
84 chunk_t eap_data
= chunk_empty
;
90 eap_type_t type
, received_type
;
91 u_int32_t vendor
, received_vendor
;
94 bool concatenated
= FALSE
;
98 status
= this->avp
->process(this->avp
, reader
, &avp_data
);
104 DBG1(DBG_IKE
, "need more AVP data");
111 if (eap_data
.len
== 0)
113 if (avp_data
.len
< 4)
115 DBG1(DBG_IKE
, "AVP size to small to contain EAP header");
116 chunk_free(&avp_data
);
119 pkt
= (eap_packet_t
*)avp_data
.ptr
;
120 eap_len
= untoh16(&pkt
->length
);
122 if (eap_len
<= avp_data
.len
)
124 /* standard: EAP packet contained in a single AVP */
128 else if (avp_data
.len
== MAX_RADIUS_ATTRIBUTE_SIZE
)
130 /* non-standard: EAP packet segmented into multiple AVPs */
131 eap_data
= chunk_alloc(eap_len
);
136 DBG1(DBG_IKE
, "non-radius segmentation of EAP packet into AVPs");
137 chunk_free(&avp_data
);
142 if (avp_data
.len
> eap_data
.len
- eap_pos
)
144 DBG1(DBG_IKE
, "AVP size to large to fit into EAP packet");
145 chunk_free(&avp_data
);
146 chunk_free(&eap_data
);
149 memcpy(eap_data
.ptr
+ eap_pos
, avp_data
.ptr
, avp_data
.len
);
150 eap_pos
+= avp_data
.len
;
151 chunk_free(&avp_data
);
153 while (eap_pos
< eap_data
.len
);
155 in
= eap_payload_create_data(eap_data
);
156 chunk_free(&eap_data
);
157 payload
= (payload_t
*)in
;
159 if (payload
->verify(payload
) != SUCCESS
)
164 code
= in
->get_code(in
);
165 received_type
= in
->get_type(in
, &received_vendor
);
166 DBG1(DBG_IKE
, "received tunneled EAP-TTLS AVP%s [EAP/%N/%N]",
167 concatenated ?
"s" : "",
168 eap_code_short_names
, code
,
169 eap_type_short_names
, received_type
);
170 if (code
!= EAP_REQUEST
)
172 DBG1(DBG_IKE
, "%N expected", eap_code_names
, EAP_REQUEST
);
177 if (this->method
== NULL
)
181 DBG1(DBG_IKE
, "server requested vendor specific EAP method %d-%d",
182 received_type
, received_vendor
);
186 DBG1(DBG_IKE
, "server requested %N authentication",
187 eap_type_names
, received_type
);
189 this->method
= charon
->eap
->create_instance(charon
->eap
,
190 received_type
, received_vendor
,
191 EAP_PEER
, this->server
, this->peer
);
194 DBG1(DBG_IKE
, "EAP method not supported");
195 this->out
= eap_payload_create_nak(in
->get_identifier(in
));
201 type
= this->method
->get_type(this->method
, &vendor
);
203 if (type
!= received_type
|| vendor
!= received_vendor
)
205 DBG1(DBG_IKE
, "received invalid EAP request");
210 status
= this->method
->process(this->method
, in
, &this->out
);
216 this->method
->destroy(this->method
);
222 this->method
->destroy(this->method
);
230 DBG1(DBG_IKE
, "vendor specific EAP method %d-%d failed",
235 DBG1(DBG_IKE
, "%N method failed", eap_type_names
, type
);
241 METHOD(tls_application_t
, build
, status_t
,
242 private_eap_ttls_peer_t
*this, tls_writer_t
*writer
)
249 if (this->method
== NULL
&& this->start_phase2
)
251 /* generate an EAP Identity response */
252 this->method
= charon
->eap
->create_instance(charon
->eap
, EAP_IDENTITY
,
253 0, EAP_PEER
, this->server
, this->peer
);
254 if (this->method
== NULL
)
256 DBG1(DBG_IKE
, "EAP_IDENTITY method not available");
259 this->method
->process(this->method
, NULL
, &this->out
);
260 this->method
->destroy(this->method
);
262 this->start_phase2
= FALSE
;
267 code
= this->out
->get_code(this->out
);
268 type
= this->out
->get_type(this->out
, &vendor
);
269 DBG1(DBG_IKE
, "sending tunneled EAP-TTLS AVP [EAP/%N/%N]",
270 eap_code_short_names
, code
, eap_type_short_names
, type
);
272 /* get the raw EAP message data */
273 data
= this->out
->get_data(this->out
);
274 this->avp
->build(this->avp
, writer
, data
);
276 this->out
->destroy(this->out
);
279 return INVALID_STATE
;
282 METHOD(tls_application_t
, destroy
, void,
283 private_eap_ttls_peer_t
*this)
285 this->server
->destroy(this->server
);
286 this->peer
->destroy(this->peer
);
287 DESTROY_IF(this->method
);
288 DESTROY_IF(this->out
);
289 this->avp
->destroy(this->avp
);
296 eap_ttls_peer_t
*eap_ttls_peer_create(identification_t
*server
,
297 identification_t
*peer
)
299 private_eap_ttls_peer_t
*this;
309 .server
= server
->clone(server
),
310 .peer
= peer
->clone(peer
),
311 .start_phase2
= TRUE
,
312 .avp
= eap_ttls_avp_create(),
315 return &this->public;