Removed strayed code fragment
[strongswan.git] / src / charon / plugins / eap_identity / eap_identity.c
1 /*
2 * Copyright (C) 2007-2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #include "eap_identity.h"
17
18 #include <daemon.h>
19 #include <library.h>
20
21 typedef struct private_eap_identity_t private_eap_identity_t;
22
23 /**
24 * Private data of an eap_identity_t object.
25 */
26 struct private_eap_identity_t {
27
28 /**
29 * Public authenticator_t interface.
30 */
31 eap_identity_t public;
32
33 /**
34 * ID of the peer
35 */
36 identification_t *peer;
37
38 /**
39 * received identity chunk
40 */
41 chunk_t identity;
42 };
43
44 typedef struct eap_identity_header_t eap_identity_header_t;
45
46 /**
47 * packed EAP Identity header struct
48 */
49 struct eap_identity_header_t {
50 /** EAP code (REQUEST/RESPONSE) */
51 u_int8_t code;
52 /** unique message identifier */
53 u_int8_t identifier;
54 /** length of whole message */
55 u_int16_t length;
56 /** EAP type */
57 u_int8_t type;
58 /** identity data */
59 u_int8_t data[];
60 } __attribute__((__packed__));
61
62 /**
63 * Implementation of eap_method_t.process for the peer
64 */
65 static status_t process_peer(private_eap_identity_t *this,
66 eap_payload_t *in, eap_payload_t **out)
67 {
68 chunk_t id;
69 eap_identity_header_t *hdr;
70 size_t len;
71
72 id = this->peer->get_encoding(this->peer);
73 len = sizeof(eap_identity_header_t) + id.len;
74
75 hdr = alloca(len);
76 hdr->code = EAP_RESPONSE;
77 hdr->identifier = in->get_identifier(in);
78 hdr->length = htons(len);
79 hdr->type = EAP_IDENTITY;
80 memcpy(hdr->data, id.ptr, id.len);
81
82 *out = eap_payload_create_data(chunk_create((u_char*)hdr, len));
83 return SUCCESS;
84 }
85
86 /**
87 * Implementation of eap_method_t.initiate for the peer
88 */
89 static status_t initiate_peer(private_eap_identity_t *this, eap_payload_t **out)
90 {
91 /* peer never initiates */
92 return FAILED;
93 }
94
95 /**
96 * Implementation of eap_method_t.process for the server
97 */
98 static status_t process_server(private_eap_identity_t *this,
99 eap_payload_t *in, eap_payload_t **out)
100 {
101 chunk_t data;
102
103 data = chunk_skip(in->get_data(in), 5);
104 if (data.len)
105 {
106 this->identity = chunk_clone(data);
107 }
108 return SUCCESS;
109 }
110
111 /**
112 * Implementation of eap_method_t.initiate for the server
113 */
114 static status_t initiate_server(private_eap_identity_t *this, eap_payload_t **out)
115 {
116 eap_identity_header_t hdr;
117
118 hdr.code = EAP_REQUEST;
119 hdr.identifier = 0;
120 hdr.length = htons(sizeof(eap_identity_header_t));
121 hdr.type = EAP_IDENTITY;
122
123 *out = eap_payload_create_data(chunk_create((u_char*)&hdr,
124 sizeof(eap_identity_header_t)));
125 return NEED_MORE;
126 }
127
128 /**
129 * Implementation of eap_method_t.get_type.
130 */
131 static eap_type_t get_type(private_eap_identity_t *this, u_int32_t *vendor)
132 {
133 *vendor = 0;
134 return EAP_IDENTITY;
135 }
136
137 /**
138 * Implementation of eap_method_t.get_msk.
139 */
140 static status_t get_msk(private_eap_identity_t *this, chunk_t *msk)
141 {
142 if (this->identity.ptr)
143 {
144 *msk = this->identity;
145 return SUCCESS;
146 }
147 return FAILED;
148 }
149
150 /**
151 * Implementation of eap_method_t.is_mutual.
152 */
153 static bool is_mutual(private_eap_identity_t *this)
154 {
155 return FALSE;
156 }
157
158 /**
159 * Implementation of eap_method_t.destroy.
160 */
161 static void destroy(private_eap_identity_t *this)
162 {
163 this->peer->destroy(this->peer);
164 free(this->identity.ptr);
165 free(this);
166 }
167
168 /**
169 * Generic constructor
170 */
171 static private_eap_identity_t *eap_identity_create(identification_t *server,
172 identification_t *peer)
173 {
174 private_eap_identity_t *this = malloc_thing(private_eap_identity_t);
175
176 this->public.eap_method_interface.initiate = NULL;
177 this->public.eap_method_interface.process = NULL;
178 this->public.eap_method_interface.get_type = (eap_type_t(*)(eap_method_t*,u_int32_t*))get_type;
179 this->public.eap_method_interface.is_mutual = (bool(*)(eap_method_t*))is_mutual;
180 this->public.eap_method_interface.get_msk = (status_t(*)(eap_method_t*,chunk_t*))get_msk;
181 this->public.eap_method_interface.destroy = (void(*)(eap_method_t*))destroy;
182
183 this->peer = peer->clone(peer);
184 this->identity = chunk_empty;
185
186 return this;
187 }
188
189 /*
190 * Described in header.
191 */
192 eap_identity_t *eap_identity_create_peer(identification_t *server,
193 identification_t *peer)
194 {
195 private_eap_identity_t *this = eap_identity_create(server, peer);
196
197 /* public functions */
198 this->public.eap_method_interface.initiate = (status_t(*)(eap_method_t*,eap_payload_t**))initiate_peer;
199 this->public.eap_method_interface.process = (status_t(*)(eap_method_t*,eap_payload_t*,eap_payload_t**))process_peer;
200
201 return &this->public;
202 }
203
204 /*
205 * Described in header.
206 */
207 eap_identity_t *eap_identity_create_server(identification_t *server,
208 identification_t *peer)
209 {
210 private_eap_identity_t *this = eap_identity_create(server, peer);
211
212 /* public functions */
213 this->public.eap_method_interface.initiate = (status_t(*)(eap_method_t*,eap_payload_t**))initiate_server;
214 this->public.eap_method_interface.process = (status_t(*)(eap_method_t*,eap_payload_t*,eap_payload_t**))process_server;
215
216 return &this->public;
217 }
218