Added member fields for reserved bits and bytes in all payloads
[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 * Reserved bits
47 */
48 bool reserved[7];
49
50 /**
51 * Length of this payload.
52 */
53 u_int16_t payload_length;
54
55 /**
56 * EAP message data, if available
57 */
58 chunk_t data;
59 };
60
61 /**
62 * Encoding rules to parse or generate a EAP payload.
63 *
64 * The defined offsets are the positions in a object of type
65 * private_eap_payload_t.
66 *
67 */
68 static encoding_rule_t eap_payload_encodings[] = {
69 /* 1 Byte next payload type, stored in the field next_payload */
70 { U_INT_8, offsetof(private_eap_payload_t, next_payload) },
71 /* the critical bit */
72 { FLAG, offsetof(private_eap_payload_t, critical) },
73 /* 7 Bit reserved bits, nowhere stored */
74 { RESERVED_BIT, offsetof(private_eap_payload_t, reserved[0]) },
75 { RESERVED_BIT, offsetof(private_eap_payload_t, reserved[1]) },
76 { RESERVED_BIT, offsetof(private_eap_payload_t, reserved[2]) },
77 { RESERVED_BIT, offsetof(private_eap_payload_t, reserved[3]) },
78 { RESERVED_BIT, offsetof(private_eap_payload_t, reserved[4]) },
79 { RESERVED_BIT, offsetof(private_eap_payload_t, reserved[5]) },
80 { RESERVED_BIT, offsetof(private_eap_payload_t, reserved[6]) },
81 /* Length of the whole payload*/
82 { PAYLOAD_LENGTH, offsetof(private_eap_payload_t, payload_length) },
83 /* chunt to data, starting at "code" */
84 { EAP_DATA, offsetof(private_eap_payload_t, data) },
85 };
86
87 /*
88 1 2 3
89 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
90 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91 ! Next Payload !C! RESERVED ! Payload Length !
92 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93 ! Code ! Identifier ! Length !
94 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95 ! Type ! Type_Data...
96 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
97 */
98
99 METHOD(payload_t, verify, status_t,
100 private_eap_payload_t *this)
101 {
102 u_int16_t length;
103 u_int8_t code;
104
105 if (this->data.len < 4)
106 {
107 DBG1(DBG_ENC, "EAP payloads EAP message too short (%d)", this->data.len);
108 return FAILED;
109 }
110 length = untoh16(this->data.ptr + 2);
111 if (this->data.len != length)
112 {
113 DBG1(DBG_ENC, "EAP payload length (%d) does not match contained "
114 "message length (%d)", this->data.len, length);
115 return FAILED;
116 }
117 code = this->data.ptr[0];
118 switch (code)
119 {
120 case EAP_REQUEST:
121 case EAP_RESPONSE:
122 {
123 if (this->data.len < 4)
124 {
125 DBG1(DBG_ENC, "EAP Request/Response does not have any data");
126 return FAILED;
127 }
128 break;
129 }
130 case EAP_SUCCESS:
131 case EAP_FAILURE:
132 {
133 if (this->data.len != 4)
134 {
135 DBG1(DBG_ENC, "EAP Success/Failure has data");
136 return FAILED;
137 }
138 break;
139 }
140 default:
141 return FAILED;
142 }
143 return SUCCESS;
144 }
145
146 METHOD(payload_t, get_encoding_rules, void,
147 private_eap_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
148 {
149 *rules = eap_payload_encodings;
150 *rule_count = sizeof(eap_payload_encodings) / sizeof(encoding_rule_t);
151 }
152
153 METHOD(payload_t, get_payload_type, payload_type_t,
154 private_eap_payload_t *this)
155 {
156 return EXTENSIBLE_AUTHENTICATION;
157 }
158
159 METHOD(payload_t, get_next_type, payload_type_t,
160 private_eap_payload_t *this)
161 {
162 return (this->next_payload);
163 }
164
165 METHOD(payload_t, set_next_type, void,
166 private_eap_payload_t *this, payload_type_t type)
167 {
168 this->next_payload = type;
169 }
170
171 METHOD(payload_t, get_length, size_t,
172 private_eap_payload_t *this)
173 {
174 return this->payload_length;
175 }
176
177 METHOD(eap_payload_t, get_data, chunk_t,
178 private_eap_payload_t *this)
179 {
180 return this->data;
181 }
182
183 METHOD(eap_payload_t, set_data, void,
184 private_eap_payload_t *this, chunk_t data)
185 {
186 free(this->data.ptr);
187 this->data = chunk_clone(data);
188 this->payload_length = this->data.len + 4;
189 }
190
191 METHOD(eap_payload_t, get_code, eap_code_t,
192 private_eap_payload_t *this)
193 {
194 if (this->data.len > 0)
195 {
196 return this->data.ptr[0];
197 }
198 /* should not happen, as it is verified */
199 return 0;
200 }
201
202 METHOD(eap_payload_t, get_identifier, u_int8_t,
203 private_eap_payload_t *this)
204 {
205 if (this->data.len > 1)
206 {
207 return this->data.ptr[1];
208 }
209 /* should not happen, as it is verified */
210 return 0;
211 }
212
213 METHOD(eap_payload_t, get_type, eap_type_t,
214 private_eap_payload_t *this, u_int32_t *vendor)
215 {
216 eap_type_t type;
217
218 *vendor = 0;
219 if (this->data.len > 4)
220 {
221 type = this->data.ptr[4];
222 if (type != EAP_EXPANDED)
223 {
224 return type;
225 }
226 if (this->data.len >= 12)
227 {
228 *vendor = untoh32(this->data.ptr + 4) & 0x00FFFFFF;
229 return untoh32(this->data.ptr + 8);
230 }
231 }
232 return 0;
233 }
234
235 METHOD2(payload_t, eap_payload_t, destroy, void,
236 private_eap_payload_t *this)
237 {
238 chunk_free(&this->data);
239 free(this);
240 }
241
242 /*
243 * Described in header
244 */
245 eap_payload_t *eap_payload_create()
246 {
247 private_eap_payload_t *this;
248
249 INIT(this,
250 .public = {
251 .payload_interface = {
252 .verify = _verify,
253 .get_encoding_rules = _get_encoding_rules,
254 .get_length = _get_length,
255 .get_next_type = _get_next_type,
256 .set_next_type = _set_next_type,
257 .get_type = _get_payload_type,
258 .destroy = _destroy,
259 },
260 .get_data = _get_data,
261 .set_data = _set_data,
262 .get_code = _get_code,
263 .get_identifier = _get_identifier,
264 .get_type = _get_type,
265 .destroy = _destroy,
266 },
267 .next_payload = NO_PAYLOAD,
268 .payload_length = EAP_PAYLOAD_HEADER_LENGTH,
269 );
270 return &this->public;
271 }
272
273 /*
274 * Described in header
275 */
276 eap_payload_t *eap_payload_create_data(chunk_t data)
277 {
278 eap_payload_t *this = eap_payload_create();
279
280 this->set_data(this, data);
281 return this;
282 }
283
284 /*
285 * Described in header
286 */
287 eap_payload_t *eap_payload_create_code(eap_code_t code, u_int8_t identifier)
288 {
289 chunk_t data;
290
291 data = chunk_from_chars(code, identifier, 0, 0);
292 htoun16(data.ptr + 2, data.len);
293 return eap_payload_create_data(data);
294 }
295
296 /*
297 * Described in header
298 */
299 eap_payload_t *eap_payload_create_nak(u_int8_t identifier)
300 {
301 chunk_t data;
302
303 data = chunk_from_chars(EAP_RESPONSE, identifier, 0, 0, EAP_NAK);
304 htoun16(data.ptr + 2, data.len);
305 return eap_payload_create_data(data);
306 }
307