Moving charon to libcharon.
[strongswan.git] / src / libcharon / encoding / payloads / eap_payload.c
1 /*
2 * Copyright (C) 2005-2010 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <stddef.h>
18
19 #include "eap_payload.h"
20
21 #include <daemon.h>
22
23 typedef struct private_eap_payload_t private_eap_payload_t;
24
25 /**
26 * Private data of an eap_payload_t object.
27 *
28 */
29 struct private_eap_payload_t {
30 /**
31 * Public eap_payload_t interface.
32 */
33 eap_payload_t public;
34
35 /**
36 * Next payload type.
37 */
38 u_int8_t next_payload;
39
40 /**
41 * Critical flag.
42 */
43 bool critical;
44
45 /**
46 * Length of this payload.
47 */
48 u_int16_t payload_length;
49
50 /**
51 * EAP message data, if available
52 */
53 chunk_t data;
54 };
55
56 /**
57 * Encoding rules to parse or generate a EAP payload.
58 *
59 * The defined offsets are the positions in a object of type
60 * private_eap_payload_t.
61 *
62 */
63 static encoding_rule_t eap_payload_encodings[] = {
64 /* 1 Byte next payload type, stored in the field next_payload */
65 { U_INT_8, offsetof(private_eap_payload_t, next_payload) },
66 /* the critical bit */
67 { FLAG, offsetof(private_eap_payload_t, critical) },
68 /* 7 Bit reserved bits, nowhere stored */
69 { RESERVED_BIT, 0 },
70 { RESERVED_BIT, 0 },
71 { RESERVED_BIT, 0 },
72 { RESERVED_BIT, 0 },
73 { RESERVED_BIT, 0 },
74 { RESERVED_BIT, 0 },
75 { RESERVED_BIT, 0 },
76 /* Length of the whole payload*/
77 { PAYLOAD_LENGTH, offsetof(private_eap_payload_t, payload_length) },
78 /* chunt to data, starting at "code" */
79 { EAP_DATA, offsetof(private_eap_payload_t, data) },
80 };
81
82 /*
83 1 2 3
84 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
85 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86 ! Next Payload !C! RESERVED ! Payload Length !
87 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
88 ! Code ! Identifier ! Length !
89 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90 ! Type ! Type_Data...
91 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
92 */
93
94 METHOD(payload_t, verify, status_t,
95 private_eap_payload_t *this)
96 {
97 u_int16_t length;
98 u_int8_t code;
99
100 if (this->data.len < 4)
101 {
102 DBG1(DBG_ENC, "EAP payloads EAP message too short (%d)", this->data.len);
103 return FAILED;
104 }
105 length = untoh16(this->data.ptr + 2);
106 if (this->data.len != length)
107 {
108 DBG1(DBG_ENC, "EAP payload length (%d) does not match contained "
109 "message length (%d)", this->data.len, length);
110 return FAILED;
111 }
112 code = this->data.ptr[0];
113 switch (code)
114 {
115 case EAP_REQUEST:
116 case EAP_RESPONSE:
117 {
118 if (this->data.len < 4)
119 {
120 DBG1(DBG_ENC, "EAP Request/Response does not have any data");
121 return FAILED;
122 }
123 break;
124 }
125 case EAP_SUCCESS:
126 case EAP_FAILURE:
127 {
128 if (this->data.len != 4)
129 {
130 DBG1(DBG_ENC, "EAP Success/Failure has data");
131 return FAILED;
132 }
133 break;
134 }
135 default:
136 return FAILED;
137 }
138 return SUCCESS;
139 }
140
141 METHOD(payload_t, get_encoding_rules, void,
142 private_eap_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
143 {
144 *rules = eap_payload_encodings;
145 *rule_count = sizeof(eap_payload_encodings) / sizeof(encoding_rule_t);
146 }
147
148 METHOD(payload_t, get_payload_type, payload_type_t,
149 private_eap_payload_t *this)
150 {
151 return EXTENSIBLE_AUTHENTICATION;
152 }
153
154 METHOD(payload_t, get_next_type, payload_type_t,
155 private_eap_payload_t *this)
156 {
157 return (this->next_payload);
158 }
159
160 METHOD(payload_t, set_next_type, void,
161 private_eap_payload_t *this, payload_type_t type)
162 {
163 this->next_payload = type;
164 }
165
166 METHOD(payload_t, get_length, size_t,
167 private_eap_payload_t *this)
168 {
169 return this->payload_length;
170 }
171
172 METHOD(eap_payload_t, get_data, chunk_t,
173 private_eap_payload_t *this)
174 {
175 return this->data;
176 }
177
178 METHOD(eap_payload_t, set_data, void,
179 private_eap_payload_t *this, chunk_t data)
180 {
181 free(this->data.ptr);
182 this->data = chunk_clone(data);
183 this->payload_length = this->data.len + 4;
184 }
185
186 METHOD(eap_payload_t, get_code, eap_code_t,
187 private_eap_payload_t *this)
188 {
189 if (this->data.len > 0)
190 {
191 return this->data.ptr[0];
192 }
193 /* should not happen, as it is verified */
194 return 0;
195 }
196
197 METHOD(eap_payload_t, get_identifier, u_int8_t,
198 private_eap_payload_t *this)
199 {
200 if (this->data.len > 1)
201 {
202 return this->data.ptr[1];
203 }
204 /* should not happen, as it is verified */
205 return 0;
206 }
207
208 METHOD(eap_payload_t, get_type, eap_type_t,
209 private_eap_payload_t *this, u_int32_t *vendor)
210 {
211 eap_type_t type;
212
213 *vendor = 0;
214 if (this->data.len > 4)
215 {
216 type = this->data.ptr[4];
217 if (type != EAP_EXPANDED)
218 {
219 return type;
220 }
221 if (this->data.len >= 12)
222 {
223 *vendor = untoh32(this->data.ptr + 4) & 0x00FFFFFF;
224 return untoh32(this->data.ptr + 8);
225 }
226 }
227 return 0;
228 }
229
230 METHOD2(payload_t, eap_payload_t, destroy, void,
231 private_eap_payload_t *this)
232 {
233 chunk_free(&this->data);
234 free(this);
235 }
236
237 /*
238 * Described in header
239 */
240 eap_payload_t *eap_payload_create()
241 {
242 private_eap_payload_t *this;
243
244 INIT(this,
245 .public = {
246 .payload_interface = {
247 .verify = _verify,
248 .get_encoding_rules = _get_encoding_rules,
249 .get_length = _get_length,
250 .get_next_type = _get_next_type,
251 .set_next_type = _set_next_type,
252 .get_type = _get_payload_type,
253 .destroy = _destroy,
254 },
255 .get_data = _get_data,
256 .set_data = _set_data,
257 .get_code = _get_code,
258 .get_identifier = _get_identifier,
259 .get_type = _get_type,
260 .destroy = _destroy,
261 },
262 .next_payload = NO_PAYLOAD,
263 .payload_length = EAP_PAYLOAD_HEADER_LENGTH,
264 );
265 return &this->public;
266 }
267
268 /*
269 * Described in header
270 */
271 eap_payload_t *eap_payload_create_data(chunk_t data)
272 {
273 eap_payload_t *this = eap_payload_create();
274
275 this->set_data(this, data);
276 return this;
277 }
278
279 /*
280 * Described in header
281 */
282 eap_payload_t *eap_payload_create_code(eap_code_t code, u_int8_t identifier)
283 {
284 chunk_t data;
285
286 data = chunk_from_chars(code, identifier, 0, 0);
287 htoun16(data.ptr + 2, data.len);
288 return eap_payload_create_data(data);
289 }
290
291 /*
292 * Described in header
293 */
294 eap_payload_t *eap_payload_create_nak(u_int8_t identifier)
295 {
296 chunk_t data;
297
298 data = chunk_from_chars(EAP_RESPONSE, identifier, 0, 0, EAP_NAK);
299 htoun16(data.ptr + 2, data.len);
300 return eap_payload_create_data(data);
301 }
302