Moving charon to libcharon.
[strongswan.git] / src / libcharon / plugins / eap_radius / radius_message.c
1 /*
2 * Copyright (C) 2009 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 "radius_message.h"
17
18 #include <daemon.h>
19 #include <crypto/hashers/hasher.h>
20
21 typedef struct private_radius_message_t private_radius_message_t;
22 typedef struct rmsg_t rmsg_t;
23 typedef struct rattr_t rattr_t;
24
25 /**
26 * RADIUS message header
27 */
28 struct rmsg_t {
29 /** message code, radius_message_code_t */
30 u_int8_t code;
31 /** message identifier */
32 u_int8_t identifier;
33 /** length of Code, Identifier, Length, Authenticator and Attributes */
34 u_int16_t length;
35 /** message authenticator, MD5 hash */
36 u_int8_t authenticator[HASH_SIZE_MD5];
37 /** variable list of packed attributes */
38 u_int8_t attributes[];
39 } __attribute__((packed));
40
41 /**
42 * RADIUS message attribute.
43 */
44 struct rattr_t {
45 /** attribute type, radius_attribute_type_t */
46 u_int8_t type;
47 /** length of the attriubte, including the Type, Length and Value fields */
48 u_int8_t length;
49 /** variable length attribute value */
50 u_int8_t value[];
51 } __attribute__((packed));
52
53 /**
54 * Private data of an radius_message_t object.
55 */
56 struct private_radius_message_t {
57
58 /**
59 * Public radius_message_t interface.
60 */
61 radius_message_t public;
62
63 /**
64 * message data, allocated
65 */
66 rmsg_t *msg;
67 };
68
69 ENUM_BEGIN(radius_message_code_names, RMC_ACCESS_REQUEST, RMC_ACCOUNTING_RESPONSE,
70 "Access-Request",
71 "Access-Accept",
72 "Access-Reject",
73 "Accounting-Request",
74 "Accounting-Response");
75 ENUM_NEXT(radius_message_code_names, RMC_ACCESS_CHALLENGE, RMC_ACCESS_CHALLENGE, RMC_ACCOUNTING_RESPONSE,
76 "Access-Challenge");
77 ENUM_END(radius_message_code_names, RMC_ACCESS_CHALLENGE);
78
79 ENUM(radius_attribute_type_names, RAT_USER_NAME, RAT_MIP6_HOME_LINK_PREFIX,
80 "User-Name",
81 "User-Password",
82 "CHAP-Password",
83 "NAS-IP-Address",
84 "NAS-Port",
85 "Service-Type",
86 "Framed-Protocol",
87 "Framed-IP-Address",
88 "Framed-IP-Netmask",
89 "Framed-Routing",
90 "Filter-Id",
91 "Framed-MTU",
92 "Framed-Compression",
93 "Login-IP-Host",
94 "Login-Service",
95 "Login-TCP-Port",
96 "Unassigned",
97 "Reply-Message",
98 "Callback-Number",
99 "Callback-Id",
100 "Unassigned",
101 "Framed-Route",
102 "Framed-IPX-Network",
103 "State",
104 "Class",
105 "Vendor-Specific",
106 "Session-Timeout",
107 "Idle-Timeout",
108 "Termination-Action",
109 "Called-Station-Id",
110 "Calling-Station-Id",
111 "NAS-Identifier",
112 "Proxy-State",
113 "Login-LAT-Service",
114 "Login-LAT-Node",
115 "Login-LAT-Group",
116 "Framed-AppleTalk-Link",
117 "Framed-AppleTalk-Network",
118 "Framed-AppleTalk-Zone",
119 "Acct-Status-Type",
120 "Acct-Delay-Time",
121 "Acct-Input-Octets",
122 "Acct-Output-Octets",
123 "Acct-Session-Id",
124 "Acct-Authentic",
125 "Acct-Session-Time",
126 "Acct-Input-Packets",
127 "Acct-Output-Packets",
128 "Acct-Terminate-Cause",
129 "Acct-Multi-Session-Id",
130 "Acct-Link-Count",
131 "Acct-Input-Gigawords",
132 "Acct-Output-Gigawords",
133 "Unassigned",
134 "Event-Timestamp",
135 "Egress-VLANID",
136 "Ingress-Filters",
137 "Egress-VLAN-Name",
138 "User-Priority-Table",
139 "CHAP-Challenge",
140 "NAS-Port-Type",
141 "Port-Limit",
142 "Login-LAT-Port",
143 "Tunnel-Type",
144 "Tunnel-Medium-Type",
145 "Tunnel-Client-Endpoint",
146 "Tunnel-Server-Endpoint",
147 "Acct-Tunnel-Connection",
148 "Tunnel-Password",
149 "ARAP-Password",
150 "ARAP-Features",
151 "ARAP-Zone-Access",
152 "ARAP-Security",
153 "ARAP-Security-Data",
154 "Password-Retry",
155 "Prompt",
156 "Connect-Info",
157 "Configuration-Token",
158 "EAP-Message",
159 "Message-Authenticator",
160 "Tunnel-Private-Group-ID",
161 "Tunnel-Assignment-ID",
162 "Tunnel-Preference",
163 "ARAP-Challenge-Response",
164 "Acct-Interim-Interval",
165 "Acct-Tunnel-Packets-Lost",
166 "NAS-Port-Id",
167 "Framed-Pool",
168 "CUI",
169 "Tunnel-Client-Auth-ID",
170 "Tunnel-Server-Auth-ID",
171 "NAS-Filter-Rule",
172 "Unassigned",
173 "Originating-Line-Info",
174 "NAS-IPv6-Address",
175 "Framed-Interface-Id",
176 "Framed-IPv6-Prefix",
177 "Login-IPv6-Host",
178 "Framed-IPv6-Route",
179 "Framed-IPv6-Pool",
180 "Error-Cause",
181 "EAP-Key-Name",
182 "Digest-Response",
183 "Digest-Realm",
184 "Digest-Nonce",
185 "Digest-Response-Auth",
186 "Digest-Nextnonce",
187 "Digest-Method",
188 "Digest-URI",
189 "Digest-Qop",
190 "Digest-Algorithm",
191 "Digest-Entity-Body-Hash",
192 "Digest-CNonce",
193 "Digest-Nonce-Count",
194 "Digest-Username",
195 "Digest-Opaque",
196 "Digest-Auth-Param",
197 "Digest-AKA-Auts",
198 "Digest-Domain",
199 "Digest-Stale",
200 "Digest-HA1",
201 "SIP-AOR",
202 "Delegated-IPv6-Prefix",
203 "MIP6-Feature-Vector",
204 "MIP6-Home-Link-Prefix");
205
206 /**
207 * Attribute enumerator implementation
208 */
209 typedef struct {
210 /** implements enumerator interface */
211 enumerator_t public;
212 /** currently pointing attribute */
213 rattr_t *next;
214 /** bytes left */
215 int left;
216 } attribute_enumerator_t;
217
218
219 /**
220 * Implementation of attribute_enumerator_t.enumerate
221 */
222 static bool attribute_enumerate(attribute_enumerator_t *this,
223 int *type, chunk_t *data)
224
225 {
226 if (this->left == 0)
227 {
228 return FALSE;
229 }
230 if (this->left < sizeof(rattr_t) ||
231 this->left < this->next->length)
232 {
233 DBG1(DBG_IKE, "RADIUS message truncated");
234 return FALSE;
235 }
236 *type = this->next->type;
237 data->ptr = this->next->value;
238 data->len = this->next->length - sizeof(rattr_t);
239 this->left -= this->next->length;
240 this->next = ((void*)this->next) + this->next->length;
241 return TRUE;
242 }
243
244 /**
245 * Implementation of radius_message_t.create_enumerator
246 */
247 static enumerator_t* create_enumerator(private_radius_message_t *this)
248 {
249 attribute_enumerator_t *e;
250
251 if (ntohs(this->msg->length) < sizeof(rmsg_t) + sizeof(rattr_t))
252 {
253 return enumerator_create_empty();
254 }
255
256 e = malloc_thing(attribute_enumerator_t);
257 e->public.enumerate = (void*)attribute_enumerate;
258 e->public.destroy = (void*)free;
259 e->next = (rattr_t*)this->msg->attributes;
260 e->left = ntohs(this->msg->length) - sizeof(rmsg_t);
261 return &e->public;
262 }
263
264 /**
265 * Implementation of radius_message_t.add
266 */
267 static void add(private_radius_message_t *this, radius_attribute_type_t type,
268 chunk_t data)
269 {
270 rattr_t *attribute;
271
272 data.len = min(data.len, 253);
273 this->msg = realloc(this->msg,
274 ntohs(this->msg->length) + sizeof(rattr_t) + data.len);
275 attribute = ((void*)this->msg) + ntohs(this->msg->length);
276 attribute->type = type;
277 attribute->length = data.len + sizeof(rattr_t);
278 memcpy(attribute->value, data.ptr, data.len);
279 this->msg->length = htons(ntohs(this->msg->length) + attribute->length);
280 }
281
282 /**
283 * Implementation of radius_message_t.sign
284 */
285 static void sign(private_radius_message_t *this, rng_t *rng, signer_t *signer)
286 {
287 char buf[HASH_SIZE_MD5];
288
289 /* build Request-Authenticator */
290 rng->get_bytes(rng, HASH_SIZE_MD5, this->msg->authenticator);
291
292 /* build Message-Authenticator attribute, using 16 null bytes */
293 memset(buf, 0, sizeof(buf));
294 add(this, RAT_MESSAGE_AUTHENTICATOR, chunk_create(buf, sizeof(buf)));
295 signer->get_signature(signer,
296 chunk_create((u_char*)this->msg, ntohs(this->msg->length)),
297 ((u_char*)this->msg) + ntohs(this->msg->length) - HASH_SIZE_MD5);
298 }
299
300 /**
301 * Implementation of radius_message_t.verify
302 */
303 static bool verify(private_radius_message_t *this, u_int8_t *req_auth,
304 chunk_t secret, hasher_t *hasher, signer_t *signer)
305 {
306 char buf[HASH_SIZE_MD5], res_auth[HASH_SIZE_MD5];
307 enumerator_t *enumerator;
308 int type;
309 chunk_t data, msg;
310 bool has_eap = FALSE, has_auth = FALSE;
311
312 /* replace Response by Request Authenticator for verification */
313 memcpy(res_auth, this->msg->authenticator, HASH_SIZE_MD5);
314 memcpy(this->msg->authenticator, req_auth, HASH_SIZE_MD5);
315 msg = chunk_create((u_char*)this->msg, ntohs(this->msg->length));
316
317 /* verify Response-Authenticator */
318 hasher->get_hash(hasher, msg, NULL);
319 hasher->get_hash(hasher, secret, buf);
320 if (!memeq(buf, res_auth, HASH_SIZE_MD5))
321 {
322 DBG1(DBG_CFG, "RADIUS Response-Authenticator verification failed");
323 return FALSE;
324 }
325
326 /* verify Message-Authenticator attribute */
327 enumerator = create_enumerator(this);
328 while (enumerator->enumerate(enumerator, &type, &data))
329 {
330 if (type == RAT_MESSAGE_AUTHENTICATOR)
331 {
332 if (data.len != HASH_SIZE_MD5)
333 {
334 DBG1(DBG_CFG, "RADIUS Message-Authenticator invalid length");
335 enumerator->destroy(enumerator);
336 return FALSE;
337 }
338 memcpy(buf, data.ptr, data.len);
339 memset(data.ptr, 0, data.len);
340 if (signer->verify_signature(signer, msg,
341 chunk_create(buf, sizeof(buf))))
342 {
343 /* restore Message-Authenticator */
344 memcpy(data.ptr, buf, data.len);
345 has_auth = TRUE;
346 break;
347 }
348 else
349 {
350 DBG1(DBG_CFG, "RADIUS Message-Authenticator verification failed");
351 enumerator->destroy(enumerator);
352 return FALSE;
353 }
354 }
355 else if (type == RAT_EAP_MESSAGE)
356 {
357 has_eap = TRUE;
358 }
359 }
360 enumerator->destroy(enumerator);
361 /* restore Response-Authenticator */
362 memcpy(this->msg->authenticator, res_auth, HASH_SIZE_MD5);
363
364 if (has_eap && !has_auth)
365 { /* Message-Authenticator is required if we have an EAP-Message */
366 DBG1(DBG_CFG, "RADIUS Message-Authenticator attribute missing");
367 return FALSE;
368 }
369 return TRUE;
370 }
371
372 /**
373 * Implementation of radius_message_t.get_code
374 */
375 static radius_message_code_t get_code(private_radius_message_t *this)
376 {
377 return this->msg->code;
378 }
379
380 /**
381 * Implementation of radius_message_t.get_identifier
382 */
383 static u_int8_t get_identifier(private_radius_message_t *this)
384 {
385 return this->msg->identifier;
386 }
387
388 /**
389 * Implementation of radius_message_t.set_identifier
390 */
391 static void set_identifier(private_radius_message_t *this, u_int8_t identifier)
392 {
393 this->msg->identifier = identifier;
394 }
395
396 /**
397 * Implementation of radius_message_t.get_authenticator
398 */
399 static u_int8_t* get_authenticator(private_radius_message_t *this)
400 {
401 return this->msg->authenticator;
402 }
403
404
405 /**
406 * Implementation of radius_message_t.get_encoding
407 */
408 static chunk_t get_encoding(private_radius_message_t *this)
409 {
410 return chunk_create((u_char*)this->msg, ntohs(this->msg->length));
411 }
412
413 /**
414 * Implementation of radius_message_t.destroy.
415 */
416 static void destroy(private_radius_message_t *this)
417 {
418 free(this->msg);
419 free(this);
420 }
421
422 /**
423 * Generic constructor
424 */
425 static private_radius_message_t *radius_message_create()
426 {
427 private_radius_message_t *this = malloc_thing(private_radius_message_t);
428
429 this->public.create_enumerator = (enumerator_t*(*)(radius_message_t*))create_enumerator;
430 this->public.add = (void(*)(radius_message_t*, radius_attribute_type_t,chunk_t))add;
431 this->public.get_code = (radius_message_code_t(*)(radius_message_t*))get_code;
432 this->public.get_identifier = (u_int8_t(*)(radius_message_t*))get_identifier;
433 this->public.set_identifier = (void(*)(radius_message_t*, u_int8_t identifier))set_identifier;
434 this->public.get_authenticator = (u_int8_t*(*)(radius_message_t*))get_authenticator;
435 this->public.get_encoding = (chunk_t(*)(radius_message_t*))get_encoding;
436 this->public.sign = (void(*)(radius_message_t*, rng_t *rng, signer_t *signer))sign;
437 this->public.verify = (bool(*)(radius_message_t*, u_int8_t *req_auth, chunk_t secret, hasher_t *hasher, signer_t *signer))verify;
438 this->public.destroy = (void(*)(radius_message_t*))destroy;
439
440 return this;
441 }
442
443 /**
444 * See header
445 */
446 radius_message_t *radius_message_create_request()
447 {
448 private_radius_message_t *this = radius_message_create();
449
450 this->msg = malloc_thing(rmsg_t);
451 this->msg->code = RMC_ACCESS_REQUEST;
452 this->msg->identifier = 0;
453 this->msg->length = htons(sizeof(rmsg_t));
454
455 return &this->public;
456 }
457
458 /**
459 * See header
460 */
461 radius_message_t *radius_message_parse_response(chunk_t data)
462 {
463 private_radius_message_t *this = radius_message_create();
464
465 this->msg = malloc(data.len);
466 memcpy(this->msg, data.ptr, data.len);
467 if (data.len < sizeof(rmsg_t) ||
468 ntohs(this->msg->length) != data.len)
469 {
470 DBG1(DBG_IKE, "RADIUS message has invalid length");
471 destroy(this);
472 return NULL;
473 }
474 return &this->public;
475 }
476