2 * Copyright (C) 2007-2008 Martin Willi
3 * 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
18 #include "eap_identity.h"
23 typedef struct private_eap_identity_t private_eap_identity_t
;
26 * Private data of an eap_identity_t object.
28 struct private_eap_identity_t
{
31 * Public authenticator_t interface.
33 eap_identity_t
public;
38 identification_t
*peer
;
41 * received identity chunk
46 typedef struct eap_identity_header_t eap_identity_header_t
;
49 * packed EAP Identity header struct
51 struct eap_identity_header_t
{
52 /** EAP code (REQUEST/RESPONSE) */
54 /** unique message identifier */
56 /** length of whole message */
62 } __attribute__((__packed__
));
65 * Implementation of eap_method_t.process for the peer
67 static status_t
process_peer(private_eap_identity_t
*this,
68 eap_payload_t
*in
, eap_payload_t
**out
)
71 eap_identity_header_t
*hdr
;
74 id
= this->peer
->get_encoding(this->peer
);
75 len
= sizeof(eap_identity_header_t
) + id
.len
;
78 hdr
->code
= EAP_RESPONSE
;
79 hdr
->identifier
= in
->get_identifier(in
);
80 hdr
->length
= htons(len
);
81 hdr
->type
= EAP_IDENTITY
;
82 memcpy(hdr
->data
, id
.ptr
, id
.len
);
84 *out
= eap_payload_create_data(chunk_create((u_char
*)hdr
, len
));
89 * Implementation of eap_method_t.initiate for the peer
91 static status_t
initiate_peer(private_eap_identity_t
*this, eap_payload_t
**out
)
93 /* peer never initiates */
98 * Implementation of eap_method_t.process for the server
100 static status_t
process_server(private_eap_identity_t
*this,
101 eap_payload_t
*in
, eap_payload_t
**out
)
105 data
= chunk_skip(in
->get_data(in
), 5);
108 this->identity
= chunk_clone(data
);
114 * Implementation of eap_method_t.initiate for the server
116 static status_t
initiate_server(private_eap_identity_t
*this, eap_payload_t
**out
)
118 eap_identity_header_t hdr
;
120 hdr
.code
= EAP_REQUEST
;
122 hdr
.length
= htons(sizeof(eap_identity_header_t
));
123 hdr
.type
= EAP_IDENTITY
;
125 *out
= eap_payload_create_data(chunk_create((u_char
*)&hdr
,
126 sizeof(eap_identity_header_t
)));
131 * Implementation of eap_method_t.get_type.
133 static eap_type_t
get_type(private_eap_identity_t
*this, u_int32_t
*vendor
)
140 * Implementation of eap_method_t.get_msk.
142 static status_t
get_msk(private_eap_identity_t
*this, chunk_t
*msk
)
144 if (this->identity
.ptr
)
146 *msk
= this->identity
;
153 * Implementation of eap_method_t.is_mutual.
155 static bool is_mutual(private_eap_identity_t
*this)
161 * Implementation of eap_method_t.destroy.
163 static void destroy(private_eap_identity_t
*this)
165 this->peer
->destroy(this->peer
);
166 free(this->identity
.ptr
);
171 * Generic constructor
173 static private_eap_identity_t
*eap_identity_create(identification_t
*server
,
174 identification_t
*peer
)
176 private_eap_identity_t
*this = malloc_thing(private_eap_identity_t
);
178 this->public.eap_method_interface
.initiate
= NULL
;
179 this->public.eap_method_interface
.process
= NULL
;
180 this->public.eap_method_interface
.get_type
= (eap_type_t(*)(eap_method_t
*,u_int32_t
*))get_type
;
181 this->public.eap_method_interface
.is_mutual
= (bool(*)(eap_method_t
*))is_mutual
;
182 this->public.eap_method_interface
.get_msk
= (status_t(*)(eap_method_t
*,chunk_t
*))get_msk
;
183 this->public.eap_method_interface
.destroy
= (void(*)(eap_method_t
*))destroy
;
185 this->peer
= peer
->clone(peer
);
186 this->identity
= chunk_empty
;
192 * Described in header.
194 eap_identity_t
*eap_identity_create_peer(identification_t
*server
,
195 identification_t
*peer
)
197 private_eap_identity_t
*this = eap_identity_create(server
, peer
);
199 /* public functions */
200 this->public.eap_method_interface
.initiate
= (status_t(*)(eap_method_t
*,eap_payload_t
**))initiate_peer
;
201 this->public.eap_method_interface
.process
= (status_t(*)(eap_method_t
*,eap_payload_t
*,eap_payload_t
**))process_peer
;
203 return &this->public;
207 * Described in header.
209 eap_identity_t
*eap_identity_create_server(identification_t
*server
,
210 identification_t
*peer
)
212 private_eap_identity_t
*this = eap_identity_create(server
, peer
);
214 /* public functions */
215 this->public.eap_method_interface
.initiate
= (status_t(*)(eap_method_t
*,eap_payload_t
**))initiate_server
;
216 this->public.eap_method_interface
.process
= (status_t(*)(eap_method_t
*,eap_payload_t
*,eap_payload_t
**))process_server
;
218 return &this->public;