Send EAP-Nak with supported types if requested type is unsupported
[strongswan.git] / src / libcharon / encoding / payloads / eap_payload.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2005-2010 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <stddef.h>
19
20 #include "eap_payload.h"
21
22 #include <daemon.h>
23 #include <eap/eap.h>
24 #include <bio/bio_writer.h>
25
26 typedef struct private_eap_payload_t private_eap_payload_t;
27
28 /**
29 * Private data of an eap_payload_t object.
30 *
31 */
32 struct private_eap_payload_t {
33 /**
34 * Public eap_payload_t interface.
35 */
36 eap_payload_t public;
37
38 /**
39 * Next payload type.
40 */
41 u_int8_t next_payload;
42
43 /**
44 * Critical flag.
45 */
46 bool critical;
47
48 /**
49 * Reserved bits
50 */
51 bool reserved[7];
52
53 /**
54 * Length of this payload.
55 */
56 u_int16_t payload_length;
57
58 /**
59 * EAP message data, if available
60 */
61 chunk_t data;
62 };
63
64 /**
65 * Encoding rules to parse or generate a EAP payload.
66 *
67 * The defined offsets are the positions in a object of type
68 * private_eap_payload_t.
69 *
70 */
71 static encoding_rule_t encodings[] = {
72 /* 1 Byte next payload type, stored in the field next_payload */
73 { U_INT_8, offsetof(private_eap_payload_t, next_payload) },
74 /* the critical bit */
75 { FLAG, offsetof(private_eap_payload_t, critical) },
76 /* 7 Bit reserved bits, nowhere stored */
77 { RESERVED_BIT, offsetof(private_eap_payload_t, reserved[0]) },
78 { RESERVED_BIT, offsetof(private_eap_payload_t, reserved[1]) },
79 { RESERVED_BIT, offsetof(private_eap_payload_t, reserved[2]) },
80 { RESERVED_BIT, offsetof(private_eap_payload_t, reserved[3]) },
81 { RESERVED_BIT, offsetof(private_eap_payload_t, reserved[4]) },
82 { RESERVED_BIT, offsetof(private_eap_payload_t, reserved[5]) },
83 { RESERVED_BIT, offsetof(private_eap_payload_t, reserved[6]) },
84 /* Length of the whole payload*/
85 { PAYLOAD_LENGTH, offsetof(private_eap_payload_t, payload_length) },
86 /* chunt to data, starting at "code" */
87 { CHUNK_DATA, offsetof(private_eap_payload_t, data) },
88 };
89
90 /*
91 1 2 3
92 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
93 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 ! Next Payload !C! RESERVED ! Payload Length !
95 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 ! Code ! Identifier ! Length !
97 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98 ! Type ! Type_Data...
99 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
100 */
101
102 METHOD(payload_t, verify, status_t,
103 private_eap_payload_t *this)
104 {
105 u_int16_t length;
106 u_int8_t code;
107
108 if (this->data.len < 4)
109 {
110 DBG1(DBG_ENC, "EAP payloads EAP message too short (%d)", this->data.len);
111 return FAILED;
112 }
113 length = untoh16(this->data.ptr + 2);
114 if (this->data.len != length)
115 {
116 DBG1(DBG_ENC, "EAP payload length (%d) does not match contained "
117 "message length (%d)", this->data.len, length);
118 return FAILED;
119 }
120 code = this->data.ptr[0];
121 switch (code)
122 {
123 case EAP_REQUEST:
124 case EAP_RESPONSE:
125 {
126 if (this->data.len < 4)
127 {
128 DBG1(DBG_ENC, "EAP Request/Response does not have any data");
129 return FAILED;
130 }
131 break;
132 }
133 case EAP_SUCCESS:
134 case EAP_FAILURE:
135 {
136 if (this->data.len != 4)
137 {
138 DBG1(DBG_ENC, "EAP Success/Failure has data");
139 return FAILED;
140 }
141 break;
142 }
143 default:
144 return FAILED;
145 }
146 return SUCCESS;
147 }
148
149 METHOD(payload_t, get_encoding_rules, int,
150 private_eap_payload_t *this, encoding_rule_t **rules)
151 {
152 *rules = encodings;
153 return countof(encodings);
154 }
155
156 METHOD(payload_t, get_header_length, int,
157 private_eap_payload_t *this)
158 {
159 return 4;
160 }
161
162 METHOD(payload_t, get_payload_type, payload_type_t,
163 private_eap_payload_t *this)
164 {
165 return EXTENSIBLE_AUTHENTICATION;
166 }
167
168 METHOD(payload_t, get_next_type, payload_type_t,
169 private_eap_payload_t *this)
170 {
171 return (this->next_payload);
172 }
173
174 METHOD(payload_t, set_next_type, void,
175 private_eap_payload_t *this, payload_type_t type)
176 {
177 this->next_payload = type;
178 }
179
180 METHOD(payload_t, get_length, size_t,
181 private_eap_payload_t *this)
182 {
183 return this->payload_length;
184 }
185
186 METHOD(eap_payload_t, get_data, chunk_t,
187 private_eap_payload_t *this)
188 {
189 return this->data;
190 }
191
192 METHOD(eap_payload_t, set_data, void,
193 private_eap_payload_t *this, chunk_t data)
194 {
195 free(this->data.ptr);
196 this->data = chunk_clone(data);
197 this->payload_length = this->data.len + 4;
198 }
199
200 METHOD(eap_payload_t, get_code, eap_code_t,
201 private_eap_payload_t *this)
202 {
203 if (this->data.len > 0)
204 {
205 return this->data.ptr[0];
206 }
207 /* should not happen, as it is verified */
208 return 0;
209 }
210
211 METHOD(eap_payload_t, get_identifier, u_int8_t,
212 private_eap_payload_t *this)
213 {
214 if (this->data.len > 1)
215 {
216 return this->data.ptr[1];
217 }
218 /* should not happen, as it is verified */
219 return 0;
220 }
221
222 METHOD(eap_payload_t, get_type, eap_type_t,
223 private_eap_payload_t *this, u_int32_t *vendor)
224 {
225 eap_type_t type;
226
227 *vendor = 0;
228 if (this->data.len > 4)
229 {
230 type = this->data.ptr[4];
231 if (type != EAP_EXPANDED)
232 {
233 return type;
234 }
235 if (this->data.len >= 12)
236 {
237 *vendor = untoh32(this->data.ptr + 4) & 0x00FFFFFF;
238 return untoh32(this->data.ptr + 8);
239 }
240 }
241 return 0;
242 }
243
244 METHOD2(payload_t, eap_payload_t, destroy, void,
245 private_eap_payload_t *this)
246 {
247 chunk_free(&this->data);
248 free(this);
249 }
250
251 /*
252 * Described in header
253 */
254 eap_payload_t *eap_payload_create()
255 {
256 private_eap_payload_t *this;
257
258 INIT(this,
259 .public = {
260 .payload_interface = {
261 .verify = _verify,
262 .get_encoding_rules = _get_encoding_rules,
263 .get_header_length = _get_header_length,
264 .get_length = _get_length,
265 .get_next_type = _get_next_type,
266 .set_next_type = _set_next_type,
267 .get_type = _get_payload_type,
268 .destroy = _destroy,
269 },
270 .get_data = _get_data,
271 .set_data = _set_data,
272 .get_code = _get_code,
273 .get_identifier = _get_identifier,
274 .get_type = _get_type,
275 .destroy = _destroy,
276 },
277 .next_payload = NO_PAYLOAD,
278 .payload_length = get_header_length(this),
279 );
280 return &this->public;
281 }
282
283 /*
284 * Described in header
285 */
286 eap_payload_t *eap_payload_create_data(chunk_t data)
287 {
288 eap_payload_t *this = eap_payload_create();
289
290 this->set_data(this, data);
291 return this;
292 }
293
294 /*
295 * Described in header
296 */
297 eap_payload_t *eap_payload_create_data_own(chunk_t data)
298 {
299 eap_payload_t *this = eap_payload_create();
300
301 this->set_data(this, data);
302 free(data.ptr);
303 return this;
304 }
305
306 /*
307 * Described in header
308 */
309 eap_payload_t *eap_payload_create_code(eap_code_t code, u_int8_t identifier)
310 {
311 chunk_t data;
312
313 data = chunk_from_chars(code, identifier, 0, 0);
314 htoun16(data.ptr + 2, data.len);
315 return eap_payload_create_data(data);
316 }
317
318 /**
319 * Write the given type either expanded or not
320 */
321 static void write_type(bio_writer_t *writer, eap_type_t type, u_int32_t vendor,
322 bool expanded)
323 {
324 if (expanded)
325 {
326 writer->write_uint8(writer, EAP_EXPANDED);
327 writer->write_uint24(writer, vendor);
328 writer->write_uint32(writer, type);
329 }
330 else
331 {
332 writer->write_uint8(writer, type);
333 }
334 }
335
336 /*
337 * Described in header
338 */
339 eap_payload_t *eap_payload_create_nak(u_int8_t identifier, bool expanded)
340 {
341 enumerator_t *enumerator;
342 eap_type_t reg_type;
343 u_int32_t reg_vendor;
344 bio_writer_t *writer;
345 chunk_t length, data;
346 bool added_any = FALSE, found_vendor = FALSE;
347 eap_payload_t *payload;
348
349 writer = bio_writer_create(12);
350 writer->write_uint8(writer, EAP_RESPONSE);
351 writer->write_uint8(writer, identifier);
352 length = writer->skip(writer, 2);
353
354 write_type(writer, EAP_NAK, 0, expanded);
355
356 enumerator = charon->eap->create_enumerator(charon->eap, EAP_PEER);
357 while (enumerator->enumerate(enumerator, &reg_type, &reg_vendor))
358 {
359 if (!reg_vendor || expanded)
360 {
361 write_type(writer, reg_type, reg_vendor, expanded);
362 added_any = TRUE;
363 }
364 else if (reg_vendor)
365 { /* found vendor specifc method, but this is not an expanded Nak */
366 found_vendor = TRUE;
367 }
368 }
369 enumerator->destroy(enumerator);
370
371 if (found_vendor)
372 { /* request an expanded authentication type */
373 write_type(writer, EAP_EXPANDED, 0, expanded);
374 added_any = TRUE;
375 }
376 if (!added_any)
377 { /* no methods added */
378 write_type(writer, 0, 0, expanded);
379 }
380
381 /* set length */
382 data = writer->get_buf(writer);
383 htoun16(length.ptr, data.len);
384
385 payload = eap_payload_create_data(data);
386 writer->destroy(writer);
387 return payload;
388 }
389