• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * RADIUS message processing
3  * Copyright (c) 2002-2009, 2011-2022, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 
11 #include "utils/common.h"
12 #include "utils/wpabuf.h"
13 #include "crypto/md5.h"
14 #include "crypto/crypto.h"
15 #include "radius.h"
16 
17 
18 /**
19  * struct radius_msg - RADIUS message structure for new and parsed messages
20  */
21 struct radius_msg {
22 	/**
23 	 * buf - Allocated buffer for RADIUS message
24 	 */
25 	struct wpabuf *buf;
26 
27 	/**
28 	 * hdr - Pointer to the RADIUS header in buf
29 	 */
30 	struct radius_hdr *hdr;
31 
32 	/**
33 	 * attr_pos - Array of indexes to attributes
34 	 *
35 	 * The values are number of bytes from buf to the beginning of
36 	 * struct radius_attr_hdr.
37 	 */
38 	size_t *attr_pos;
39 
40 	/**
41 	 * attr_size - Total size of the attribute pointer array
42 	 */
43 	size_t attr_size;
44 
45 	/**
46 	 * attr_used - Total number of attributes in the array
47 	 */
48 	size_t attr_used;
49 };
50 
51 
radius_msg_get_hdr(struct radius_msg * msg)52 struct radius_hdr * radius_msg_get_hdr(struct radius_msg *msg)
53 {
54 	return msg->hdr;
55 }
56 
57 
radius_msg_get_buf(struct radius_msg * msg)58 struct wpabuf * radius_msg_get_buf(struct radius_msg *msg)
59 {
60 	return msg->buf;
61 }
62 
63 
64 static struct radius_attr_hdr *
radius_get_attr_hdr(struct radius_msg * msg,int idx)65 radius_get_attr_hdr(struct radius_msg *msg, int idx)
66 {
67 	return (struct radius_attr_hdr *)
68 		(wpabuf_mhead_u8(msg->buf) + msg->attr_pos[idx]);
69 }
70 
71 
radius_msg_set_hdr(struct radius_msg * msg,u8 code,u8 identifier)72 static void radius_msg_set_hdr(struct radius_msg *msg, u8 code, u8 identifier)
73 {
74 	msg->hdr->code = code;
75 	msg->hdr->identifier = identifier;
76 }
77 
78 
radius_msg_initialize(struct radius_msg * msg)79 static int radius_msg_initialize(struct radius_msg *msg)
80 {
81 	msg->attr_pos = os_calloc(RADIUS_DEFAULT_ATTR_COUNT,
82 				  sizeof(*msg->attr_pos));
83 	if (msg->attr_pos == NULL)
84 		return -1;
85 
86 	msg->attr_size = RADIUS_DEFAULT_ATTR_COUNT;
87 	msg->attr_used = 0;
88 
89 	return 0;
90 }
91 
92 
93 /**
94  * radius_msg_new - Create a new RADIUS message
95  * @code: Code for RADIUS header
96  * @identifier: Identifier for RADIUS header
97  * Returns: Context for RADIUS message or %NULL on failure
98  *
99  * The caller is responsible for freeing the returned data with
100  * radius_msg_free().
101  */
radius_msg_new(u8 code,u8 identifier)102 struct radius_msg * radius_msg_new(u8 code, u8 identifier)
103 {
104 	struct radius_msg *msg;
105 
106 	msg = os_zalloc(sizeof(*msg));
107 	if (msg == NULL)
108 		return NULL;
109 
110 	msg->buf = wpabuf_alloc(RADIUS_DEFAULT_MSG_SIZE);
111 	if (msg->buf == NULL || radius_msg_initialize(msg)) {
112 		radius_msg_free(msg);
113 		return NULL;
114 	}
115 	msg->hdr = wpabuf_put(msg->buf, sizeof(struct radius_hdr));
116 
117 	radius_msg_set_hdr(msg, code, identifier);
118 
119 	return msg;
120 }
121 
122 
123 /**
124  * radius_msg_free - Free a RADIUS message
125  * @msg: RADIUS message from radius_msg_new() or radius_msg_parse()
126  */
radius_msg_free(struct radius_msg * msg)127 void radius_msg_free(struct radius_msg *msg)
128 {
129 	if (msg == NULL)
130 		return;
131 
132 	wpabuf_free(msg->buf);
133 	os_free(msg->attr_pos);
134 	os_free(msg);
135 }
136 
137 
radius_code_string(u8 code)138 static const char *radius_code_string(u8 code)
139 {
140 	switch (code) {
141 	case RADIUS_CODE_ACCESS_REQUEST: return "Access-Request";
142 	case RADIUS_CODE_ACCESS_ACCEPT: return "Access-Accept";
143 	case RADIUS_CODE_ACCESS_REJECT: return "Access-Reject";
144 	case RADIUS_CODE_ACCOUNTING_REQUEST: return "Accounting-Request";
145 	case RADIUS_CODE_ACCOUNTING_RESPONSE: return "Accounting-Response";
146 	case RADIUS_CODE_ACCESS_CHALLENGE: return "Access-Challenge";
147 	case RADIUS_CODE_STATUS_SERVER: return "Status-Server";
148 	case RADIUS_CODE_STATUS_CLIENT: return "Status-Client";
149 	case RADIUS_CODE_RESERVED: return "Reserved";
150 	case RADIUS_CODE_DISCONNECT_REQUEST: return "Disconnect-Request";
151 	case RADIUS_CODE_DISCONNECT_ACK: return "Disconnect-ACK";
152 	case RADIUS_CODE_DISCONNECT_NAK: return "Disconnect-NAK";
153 	case RADIUS_CODE_COA_REQUEST: return "CoA-Request";
154 	case RADIUS_CODE_COA_ACK: return "CoA-ACK";
155 	case RADIUS_CODE_COA_NAK: return "CoA-NAK";
156 	default: return "?Unknown?";
157 	}
158 }
159 
160 
161 struct radius_attr_type {
162 	u16 type; /* 0..255 for basic types;
163 		   * (241 << 8) | <ext-type> for extended types */
164 	char *name;
165 	enum {
166 		RADIUS_ATTR_UNDIST, RADIUS_ATTR_TEXT, RADIUS_ATTR_IP,
167 		RADIUS_ATTR_HEXDUMP, RADIUS_ATTR_INT32, RADIUS_ATTR_IPV6
168 	} data_type;
169 };
170 
171 static const struct radius_attr_type radius_attrs[] =
172 {
173 	{ RADIUS_ATTR_USER_NAME, "User-Name", RADIUS_ATTR_TEXT },
174 	{ RADIUS_ATTR_USER_PASSWORD, "User-Password", RADIUS_ATTR_UNDIST },
175 	{ RADIUS_ATTR_NAS_IP_ADDRESS, "NAS-IP-Address", RADIUS_ATTR_IP },
176 	{ RADIUS_ATTR_NAS_PORT, "NAS-Port", RADIUS_ATTR_INT32 },
177 	{ RADIUS_ATTR_SERVICE_TYPE, "Service-Type", RADIUS_ATTR_INT32 },
178 	{ RADIUS_ATTR_FRAMED_IP_ADDRESS, "Framed-IP-Address", RADIUS_ATTR_IP },
179 	{ RADIUS_ATTR_FILTER_ID, "Filter-Id", RADIUS_ATTR_TEXT },
180 	{ RADIUS_ATTR_FRAMED_MTU, "Framed-MTU", RADIUS_ATTR_INT32 },
181 	{ RADIUS_ATTR_REPLY_MESSAGE, "Reply-Message", RADIUS_ATTR_TEXT },
182 	{ RADIUS_ATTR_STATE, "State", RADIUS_ATTR_UNDIST },
183 	{ RADIUS_ATTR_CLASS, "Class", RADIUS_ATTR_UNDIST },
184 	{ RADIUS_ATTR_VENDOR_SPECIFIC, "Vendor-Specific", RADIUS_ATTR_UNDIST },
185 	{ RADIUS_ATTR_SESSION_TIMEOUT, "Session-Timeout", RADIUS_ATTR_INT32 },
186 	{ RADIUS_ATTR_IDLE_TIMEOUT, "Idle-Timeout", RADIUS_ATTR_INT32 },
187 	{ RADIUS_ATTR_TERMINATION_ACTION, "Termination-Action",
188 	  RADIUS_ATTR_INT32 },
189 	{ RADIUS_ATTR_CALLED_STATION_ID, "Called-Station-Id",
190 	  RADIUS_ATTR_TEXT },
191 	{ RADIUS_ATTR_CALLING_STATION_ID, "Calling-Station-Id",
192 	  RADIUS_ATTR_TEXT },
193 	{ RADIUS_ATTR_NAS_IDENTIFIER, "NAS-Identifier", RADIUS_ATTR_TEXT },
194 	{ RADIUS_ATTR_PROXY_STATE, "Proxy-State", RADIUS_ATTR_UNDIST },
195 	{ RADIUS_ATTR_ACCT_STATUS_TYPE, "Acct-Status-Type",
196 	  RADIUS_ATTR_INT32 },
197 	{ RADIUS_ATTR_ACCT_DELAY_TIME, "Acct-Delay-Time", RADIUS_ATTR_INT32 },
198 	{ RADIUS_ATTR_ACCT_INPUT_OCTETS, "Acct-Input-Octets",
199 	  RADIUS_ATTR_INT32 },
200 	{ RADIUS_ATTR_ACCT_OUTPUT_OCTETS, "Acct-Output-Octets",
201 	  RADIUS_ATTR_INT32 },
202 	{ RADIUS_ATTR_ACCT_SESSION_ID, "Acct-Session-Id", RADIUS_ATTR_TEXT },
203 	{ RADIUS_ATTR_ACCT_AUTHENTIC, "Acct-Authentic", RADIUS_ATTR_INT32 },
204 	{ RADIUS_ATTR_ACCT_SESSION_TIME, "Acct-Session-Time",
205 	  RADIUS_ATTR_INT32 },
206 	{ RADIUS_ATTR_ACCT_INPUT_PACKETS, "Acct-Input-Packets",
207 	  RADIUS_ATTR_INT32 },
208 	{ RADIUS_ATTR_ACCT_OUTPUT_PACKETS, "Acct-Output-Packets",
209 	  RADIUS_ATTR_INT32 },
210 	{ RADIUS_ATTR_ACCT_TERMINATE_CAUSE, "Acct-Terminate-Cause",
211 	  RADIUS_ATTR_INT32 },
212 	{ RADIUS_ATTR_ACCT_MULTI_SESSION_ID, "Acct-Multi-Session-Id",
213 	  RADIUS_ATTR_TEXT },
214 	{ RADIUS_ATTR_ACCT_LINK_COUNT, "Acct-Link-Count", RADIUS_ATTR_INT32 },
215 	{ RADIUS_ATTR_ACCT_INPUT_GIGAWORDS, "Acct-Input-Gigawords",
216 	  RADIUS_ATTR_INT32 },
217 	{ RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS, "Acct-Output-Gigawords",
218 	  RADIUS_ATTR_INT32 },
219 	{ RADIUS_ATTR_EVENT_TIMESTAMP, "Event-Timestamp",
220 	  RADIUS_ATTR_INT32 },
221 	{ RADIUS_ATTR_EGRESS_VLANID, "EGRESS-VLANID", RADIUS_ATTR_HEXDUMP },
222 	{ RADIUS_ATTR_NAS_PORT_TYPE, "NAS-Port-Type", RADIUS_ATTR_INT32 },
223 	{ RADIUS_ATTR_TUNNEL_TYPE, "Tunnel-Type", RADIUS_ATTR_HEXDUMP },
224 	{ RADIUS_ATTR_TUNNEL_MEDIUM_TYPE, "Tunnel-Medium-Type",
225 	  RADIUS_ATTR_HEXDUMP },
226 	{ RADIUS_ATTR_TUNNEL_PASSWORD, "Tunnel-Password",
227 	  RADIUS_ATTR_UNDIST },
228 	{ RADIUS_ATTR_CONNECT_INFO, "Connect-Info", RADIUS_ATTR_TEXT },
229 	{ RADIUS_ATTR_EAP_MESSAGE, "EAP-Message", RADIUS_ATTR_UNDIST },
230 	{ RADIUS_ATTR_MESSAGE_AUTHENTICATOR, "Message-Authenticator",
231 	  RADIUS_ATTR_UNDIST },
232 	{ RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID, "Tunnel-Private-Group-Id",
233 	  RADIUS_ATTR_HEXDUMP },
234 	{ RADIUS_ATTR_ACCT_INTERIM_INTERVAL, "Acct-Interim-Interval",
235 	  RADIUS_ATTR_INT32 },
236 	{ RADIUS_ATTR_CHARGEABLE_USER_IDENTITY, "Chargeable-User-Identity",
237 	  RADIUS_ATTR_TEXT },
238 	{ RADIUS_ATTR_NAS_IPV6_ADDRESS, "NAS-IPv6-Address", RADIUS_ATTR_IPV6 },
239 	{ RADIUS_ATTR_ERROR_CAUSE, "Error-Cause", RADIUS_ATTR_INT32 },
240 	{ RADIUS_ATTR_EAP_KEY_NAME, "EAP-Key-Name", RADIUS_ATTR_HEXDUMP },
241 	{ RADIUS_ATTR_OPERATOR_NAME, "Operator-Name", RADIUS_ATTR_TEXT },
242 	{ RADIUS_ATTR_LOCATION_INFO, "Location-Information",
243 	  RADIUS_ATTR_HEXDUMP },
244 	{ RADIUS_ATTR_LOCATION_DATA, "Location-Data", RADIUS_ATTR_HEXDUMP },
245 	{ RADIUS_ATTR_BASIC_LOCATION_POLICY_RULES,
246 	  "Basic-Location-Policy-Rules", RADIUS_ATTR_HEXDUMP },
247 	{ RADIUS_ATTR_EXTENDED_LOCATION_POLICY_RULES,
248 	  "Extended-Location-Policy-Rules", RADIUS_ATTR_HEXDUMP },
249 	{ RADIUS_ATTR_LOCATION_CAPABLE, "Location-Capable", RADIUS_ATTR_INT32 },
250 	{ RADIUS_ATTR_REQUESTED_LOCATION_INFO, "Requested-Location-Info",
251 	  RADIUS_ATTR_INT32 },
252 	{ RADIUS_ATTR_MOBILITY_DOMAIN_ID, "Mobility-Domain-Id",
253 	  RADIUS_ATTR_INT32 },
254 	{ RADIUS_ATTR_WLAN_HESSID, "WLAN-HESSID", RADIUS_ATTR_TEXT },
255 	{ RADIUS_ATTR_WLAN_REASON_CODE, "WLAN-Reason-Code",
256 	  RADIUS_ATTR_INT32 },
257 	{ RADIUS_ATTR_WLAN_PAIRWISE_CIPHER, "WLAN-Pairwise-Cipher",
258 	  RADIUS_ATTR_HEXDUMP },
259 	{ RADIUS_ATTR_WLAN_GROUP_CIPHER, "WLAN-Group-Cipher",
260 	  RADIUS_ATTR_HEXDUMP },
261 	{ RADIUS_ATTR_WLAN_AKM_SUITE, "WLAN-AKM-Suite",
262 	  RADIUS_ATTR_HEXDUMP },
263 	{ RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, "WLAN-Group-Mgmt-Pairwise-Cipher",
264 	  RADIUS_ATTR_HEXDUMP },
265 	{ RADIUS_ATTR_EXT_TYPE_1, "Extended-Type-1", RADIUS_ATTR_UNDIST },
266 	{ RADIUS_ATTR_EXT_TYPE_2, "Extended-Type-2", RADIUS_ATTR_UNDIST },
267 	{ RADIUS_ATTR_EXT_TYPE_3, "Extended-Type-3", RADIUS_ATTR_UNDIST },
268 	{ RADIUS_ATTR_EXT_TYPE_4, "Extended-Type-4", RADIUS_ATTR_UNDIST },
269 	{ RADIUS_ATTR_LONG_EXT_TYPE_1, "Long-Extended-Type-1",
270 	  RADIUS_ATTR_UNDIST },
271 	{ RADIUS_ATTR_LONG_EXT_TYPE_2, "Long-Extended-Type-2",
272 	  RADIUS_ATTR_UNDIST },
273 	{ RADIUS_ATTR_EXT_VENDOR_SPECIFIC_1, "Extended-Vendor-Specific-1",
274 	  RADIUS_ATTR_UNDIST },
275 	{ RADIUS_ATTR_EXT_VENDOR_SPECIFIC_2, "Extended-Vendor-Specific-2",
276 	  RADIUS_ATTR_UNDIST },
277 	{ RADIUS_ATTR_EXT_VENDOR_SPECIFIC_3, "Extended-Vendor-Specific-3",
278 	  RADIUS_ATTR_UNDIST },
279 	{ RADIUS_ATTR_EXT_VENDOR_SPECIFIC_4, "Extended-Vendor-Specific-4",
280 	  RADIUS_ATTR_UNDIST },
281 	{ RADIUS_ATTR_EXT_VENDOR_SPECIFIC_5, "Extended-Vendor-Specific-5",
282 	  RADIUS_ATTR_UNDIST },
283 	{ RADIUS_ATTR_EXT_VENDOR_SPECIFIC_6, "Extended-Vendor-Specific-6",
284 	  RADIUS_ATTR_UNDIST },
285 };
286 #define RADIUS_ATTRS ARRAY_SIZE(radius_attrs)
287 
288 
radius_get_attr_type(u16 type)289 static const struct radius_attr_type * radius_get_attr_type(u16 type)
290 {
291 	size_t i;
292 
293 	for (i = 0; i < RADIUS_ATTRS; i++) {
294 		if (type == radius_attrs[i].type)
295 			return &radius_attrs[i];
296 	}
297 
298 	return NULL;
299 }
300 
301 
radius_is_long_ext_type(u8 type)302 static bool radius_is_long_ext_type(u8 type)
303 {
304 	return type == RADIUS_ATTR_LONG_EXT_TYPE_1 ||
305 		type == RADIUS_ATTR_LONG_EXT_TYPE_2;
306 }
307 
308 
radius_is_ext_type(u8 type)309 static bool radius_is_ext_type(u8 type)
310 {
311 	return type >= RADIUS_ATTR_EXT_TYPE_1 &&
312 		type <= RADIUS_ATTR_LONG_EXT_TYPE_2;
313 }
314 
315 
radius_msg_dump_attr(struct radius_attr_hdr * hdr)316 static void radius_msg_dump_attr(struct radius_attr_hdr *hdr)
317 {
318 	struct radius_attr_hdr_ext *ext = NULL;
319 	const struct radius_attr_type *attr;
320 	int len;
321 	unsigned char *pos;
322 	char buf[1000];
323 
324 	if (hdr->length < sizeof(struct radius_attr_hdr))
325 		return;
326 
327 	if (radius_is_ext_type(hdr->type)) {
328 		if (hdr->length < 4) {
329 			wpa_printf(MSG_INFO,
330 				   "   Invalid attribute %d (too short for extended type)",
331 				hdr->type);
332 			return;
333 		}
334 
335 		ext = (struct radius_attr_hdr_ext *) hdr;
336 	}
337 
338 	if (ext) {
339 		attr = radius_get_attr_type((ext->type << 8) | ext->ext_type);
340 		wpa_printf(MSG_INFO, "   Attribute %d.%d (%s) length=%d",
341 			   ext->type, ext->ext_type,
342 			   attr ? attr->name : "?Unknown?", ext->length);
343 		pos = (unsigned char *) (ext + 1);
344 		len = ext->length - sizeof(struct radius_attr_hdr_ext);
345 	} else {
346 		attr = radius_get_attr_type(hdr->type);
347 		wpa_printf(MSG_INFO, "   Attribute %d (%s) length=%d",
348 			   hdr->type, attr ? attr->name : "?Unknown?",
349 			   hdr->length);
350 		pos = (unsigned char *) (hdr + 1);
351 		len = hdr->length - sizeof(struct radius_attr_hdr);
352 	}
353 
354 	if (!attr)
355 		return;
356 
357 	switch (attr->data_type) {
358 	case RADIUS_ATTR_TEXT:
359 		printf_encode(buf, sizeof(buf), pos, len);
360 		wpa_printf(MSG_INFO, "      Value: '%s'", buf);
361 		break;
362 
363 	case RADIUS_ATTR_IP:
364 		if (len == 4) {
365 			struct in_addr addr;
366 			os_memcpy(&addr, pos, 4);
367 			wpa_printf(MSG_INFO, "      Value: %s",
368 				   inet_ntoa(addr));
369 		} else {
370 			wpa_printf(MSG_INFO, "      Invalid IP address length %d",
371 				   len);
372 		}
373 		break;
374 
375 #ifdef CONFIG_IPV6
376 	case RADIUS_ATTR_IPV6:
377 		if (len == 16) {
378 			const char *atxt;
379 			struct in6_addr *addr = (struct in6_addr *) pos;
380 			atxt = inet_ntop(AF_INET6, addr, buf, sizeof(buf));
381 			wpa_printf(MSG_INFO, "      Value: %s",
382 				   atxt ? atxt : "?");
383 		} else {
384 			wpa_printf(MSG_INFO, "      Invalid IPv6 address length %d",
385 				   len);
386 		}
387 		break;
388 #endif /* CONFIG_IPV6 */
389 
390 	case RADIUS_ATTR_HEXDUMP:
391 	case RADIUS_ATTR_UNDIST:
392 		wpa_snprintf_hex(buf, sizeof(buf), pos, len);
393 		wpa_printf(MSG_INFO, "      Value: %s", buf);
394 		break;
395 
396 	case RADIUS_ATTR_INT32:
397 		if (len == 4)
398 			wpa_printf(MSG_INFO, "      Value: %u",
399 				   WPA_GET_BE32(pos));
400 		else
401 			wpa_printf(MSG_INFO, "      Invalid INT32 length %d",
402 				   len);
403 		break;
404 
405 	default:
406 		break;
407 	}
408 }
409 
410 
radius_msg_dump(struct radius_msg * msg)411 void radius_msg_dump(struct radius_msg *msg)
412 {
413 	size_t i;
414 
415 	wpa_printf(MSG_INFO, "RADIUS message: code=%d (%s) identifier=%d length=%d",
416 		   msg->hdr->code, radius_code_string(msg->hdr->code),
417 		   msg->hdr->identifier, be_to_host16(msg->hdr->length));
418 
419 	for (i = 0; i < msg->attr_used; i++) {
420 		struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
421 		radius_msg_dump_attr(attr);
422 	}
423 }
424 
425 
radius_msg_finish(struct radius_msg * msg,const u8 * secret,size_t secret_len)426 int radius_msg_finish(struct radius_msg *msg, const u8 *secret,
427 		      size_t secret_len)
428 {
429 	if (secret) {
430 		u8 auth[MD5_MAC_LEN];
431 		struct radius_attr_hdr *attr;
432 
433 		os_memset(auth, 0, MD5_MAC_LEN);
434 		attr = radius_msg_add_attr(msg,
435 					   RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
436 					   auth, MD5_MAC_LEN);
437 		if (attr == NULL) {
438 			wpa_printf(MSG_WARNING, "RADIUS: Could not add "
439 				   "Message-Authenticator");
440 			return -1;
441 		}
442 		msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
443 		hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
444 			 wpabuf_len(msg->buf), (u8 *) (attr + 1));
445 	} else
446 		msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
447 
448 	if (wpabuf_len(msg->buf) > 0xffff) {
449 		wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
450 			   (unsigned long) wpabuf_len(msg->buf));
451 		return -1;
452 	}
453 	return 0;
454 }
455 
456 
radius_msg_finish_srv(struct radius_msg * msg,const u8 * secret,size_t secret_len,const u8 * req_authenticator)457 int radius_msg_finish_srv(struct radius_msg *msg, const u8 *secret,
458 			  size_t secret_len, const u8 *req_authenticator)
459 {
460 	u8 auth[MD5_MAC_LEN];
461 	struct radius_attr_hdr *attr;
462 	const u8 *addr[4];
463 	size_t len[4];
464 
465 	os_memset(auth, 0, MD5_MAC_LEN);
466 	attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
467 				   auth, MD5_MAC_LEN);
468 	if (attr == NULL) {
469 		wpa_printf(MSG_ERROR, "WARNING: Could not add Message-Authenticator");
470 		return -1;
471 	}
472 	msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
473 	os_memcpy(msg->hdr->authenticator, req_authenticator,
474 		  sizeof(msg->hdr->authenticator));
475 	hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
476 		 wpabuf_len(msg->buf), (u8 *) (attr + 1));
477 
478 	/* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
479 	addr[0] = (u8 *) msg->hdr;
480 	len[0] = 1 + 1 + 2;
481 	addr[1] = req_authenticator;
482 	len[1] = MD5_MAC_LEN;
483 	addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
484 	len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
485 	addr[3] = secret;
486 	len[3] = secret_len;
487 	md5_vector(4, addr, len, msg->hdr->authenticator);
488 
489 	if (wpabuf_len(msg->buf) > 0xffff) {
490 		wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
491 			   (unsigned long) wpabuf_len(msg->buf));
492 		return -1;
493 	}
494 	return 0;
495 }
496 
497 
radius_msg_finish_das_resp(struct radius_msg * msg,const u8 * secret,size_t secret_len,const struct radius_hdr * req_hdr)498 int radius_msg_finish_das_resp(struct radius_msg *msg, const u8 *secret,
499 			       size_t secret_len,
500 			       const struct radius_hdr *req_hdr)
501 {
502 	const u8 *addr[2];
503 	size_t len[2];
504 	u8 auth[MD5_MAC_LEN];
505 	struct radius_attr_hdr *attr;
506 
507 	os_memset(auth, 0, MD5_MAC_LEN);
508 	attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
509 				   auth, MD5_MAC_LEN);
510 	if (attr == NULL) {
511 		wpa_printf(MSG_WARNING, "Could not add Message-Authenticator");
512 		return -1;
513 	}
514 
515 	msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
516 	os_memcpy(msg->hdr->authenticator, req_hdr->authenticator, 16);
517 	hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
518 		 wpabuf_len(msg->buf), (u8 *) (attr + 1));
519 
520 	/* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
521 	addr[0] = wpabuf_head_u8(msg->buf);
522 	len[0] = wpabuf_len(msg->buf);
523 	addr[1] = secret;
524 	len[1] = secret_len;
525 	if (md5_vector(2, addr, len, msg->hdr->authenticator) < 0)
526 		return -1;
527 
528 	if (wpabuf_len(msg->buf) > 0xffff) {
529 		wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
530 			   (unsigned long) wpabuf_len(msg->buf));
531 		return -1;
532 	}
533 	return 0;
534 }
535 
536 
radius_msg_finish_acct(struct radius_msg * msg,const u8 * secret,size_t secret_len)537 void radius_msg_finish_acct(struct radius_msg *msg, const u8 *secret,
538 			    size_t secret_len)
539 {
540 	const u8 *addr[2];
541 	size_t len[2];
542 
543 	msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
544 	os_memset(msg->hdr->authenticator, 0, MD5_MAC_LEN);
545 	addr[0] = wpabuf_head(msg->buf);
546 	len[0] = wpabuf_len(msg->buf);
547 	addr[1] = secret;
548 	len[1] = secret_len;
549 	md5_vector(2, addr, len, msg->hdr->authenticator);
550 
551 	if (wpabuf_len(msg->buf) > 0xffff) {
552 		wpa_printf(MSG_WARNING, "RADIUS: Too long messages (%lu)",
553 			   (unsigned long) wpabuf_len(msg->buf));
554 	}
555 }
556 
557 
radius_msg_finish_acct_resp(struct radius_msg * msg,const u8 * secret,size_t secret_len,const u8 * req_authenticator)558 void radius_msg_finish_acct_resp(struct radius_msg *msg, const u8 *secret,
559 				 size_t secret_len, const u8 *req_authenticator)
560 {
561 	const u8 *addr[2];
562 	size_t len[2];
563 
564 	msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
565 	os_memcpy(msg->hdr->authenticator, req_authenticator, MD5_MAC_LEN);
566 	addr[0] = wpabuf_head(msg->buf);
567 	len[0] = wpabuf_len(msg->buf);
568 	addr[1] = secret;
569 	len[1] = secret_len;
570 	md5_vector(2, addr, len, msg->hdr->authenticator);
571 
572 	if (wpabuf_len(msg->buf) > 0xffff) {
573 		wpa_printf(MSG_WARNING, "RADIUS: Too long messages (%lu)",
574 			   (unsigned long) wpabuf_len(msg->buf));
575 	}
576 }
577 
578 
radius_msg_verify_acct_req(struct radius_msg * msg,const u8 * secret,size_t secret_len)579 int radius_msg_verify_acct_req(struct radius_msg *msg, const u8 *secret,
580 			       size_t secret_len)
581 {
582 	const u8 *addr[4];
583 	size_t len[4];
584 	u8 zero[MD5_MAC_LEN];
585 	u8 hash[MD5_MAC_LEN];
586 
587 	os_memset(zero, 0, sizeof(zero));
588 	addr[0] = (u8 *) msg->hdr;
589 	len[0] = sizeof(struct radius_hdr) - MD5_MAC_LEN;
590 	addr[1] = zero;
591 	len[1] = MD5_MAC_LEN;
592 	addr[2] = (u8 *) (msg->hdr + 1);
593 	len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
594 	addr[3] = secret;
595 	len[3] = secret_len;
596 	md5_vector(4, addr, len, hash);
597 	return os_memcmp_const(msg->hdr->authenticator, hash, MD5_MAC_LEN) != 0;
598 }
599 
600 
radius_msg_verify_das_req(struct radius_msg * msg,const u8 * secret,size_t secret_len,int require_message_authenticator)601 int radius_msg_verify_das_req(struct radius_msg *msg, const u8 *secret,
602 			      size_t secret_len,
603 			      int require_message_authenticator)
604 {
605 	const u8 *addr[4];
606 	size_t len[4];
607 	u8 zero[MD5_MAC_LEN];
608 	u8 hash[MD5_MAC_LEN];
609 	u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
610 	u8 orig_authenticator[16];
611 
612 	struct radius_attr_hdr *attr = NULL, *tmp;
613 	size_t i;
614 
615 	os_memset(zero, 0, sizeof(zero));
616 	addr[0] = (u8 *) msg->hdr;
617 	len[0] = sizeof(struct radius_hdr) - MD5_MAC_LEN;
618 	addr[1] = zero;
619 	len[1] = MD5_MAC_LEN;
620 	addr[2] = (u8 *) (msg->hdr + 1);
621 	len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
622 	addr[3] = secret;
623 	len[3] = secret_len;
624 	md5_vector(4, addr, len, hash);
625 	if (os_memcmp_const(msg->hdr->authenticator, hash, MD5_MAC_LEN) != 0)
626 		return 1;
627 
628 	for (i = 0; i < msg->attr_used; i++) {
629 		tmp = radius_get_attr_hdr(msg, i);
630 		if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
631 			if (attr != NULL) {
632 				wpa_printf(MSG_WARNING, "Multiple "
633 					   "Message-Authenticator attributes "
634 					   "in RADIUS message");
635 				return 1;
636 			}
637 			attr = tmp;
638 		}
639 	}
640 
641 	if (attr == NULL) {
642 		if (require_message_authenticator) {
643 			wpa_printf(MSG_WARNING,
644 				   "Missing Message-Authenticator attribute in RADIUS message");
645 			return 1;
646 		}
647 		return 0;
648 	}
649 
650 	os_memcpy(orig, attr + 1, MD5_MAC_LEN);
651 	os_memset(attr + 1, 0, MD5_MAC_LEN);
652 	os_memcpy(orig_authenticator, msg->hdr->authenticator,
653 		  sizeof(orig_authenticator));
654 	os_memset(msg->hdr->authenticator, 0,
655 		  sizeof(msg->hdr->authenticator));
656 	hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
657 		 wpabuf_len(msg->buf), auth);
658 	os_memcpy(attr + 1, orig, MD5_MAC_LEN);
659 	os_memcpy(msg->hdr->authenticator, orig_authenticator,
660 		  sizeof(orig_authenticator));
661 
662 	return os_memcmp_const(orig, auth, MD5_MAC_LEN) != 0;
663 }
664 
665 
radius_msg_add_attr_to_array(struct radius_msg * msg,struct radius_attr_hdr * attr)666 static int radius_msg_add_attr_to_array(struct radius_msg *msg,
667 					struct radius_attr_hdr *attr)
668 {
669 	if (msg->attr_used >= msg->attr_size) {
670 		size_t *nattr_pos;
671 		size_t nlen = msg->attr_size * 2;
672 
673 		nattr_pos = os_realloc_array(msg->attr_pos, nlen,
674 					     sizeof(*msg->attr_pos));
675 		if (nattr_pos == NULL)
676 			return -1;
677 
678 		msg->attr_pos = nattr_pos;
679 		msg->attr_size = nlen;
680 	}
681 
682 	msg->attr_pos[msg->attr_used++] =
683 		(unsigned char *) attr - wpabuf_head_u8(msg->buf);
684 
685 	return 0;
686 }
687 
688 
radius_msg_add_attr(struct radius_msg * msg,u16 type,const u8 * data,size_t data_len)689 struct radius_attr_hdr * radius_msg_add_attr(struct radius_msg *msg, u16 type,
690 					     const u8 *data, size_t data_len)
691 {
692 	size_t buf_needed, max_len;
693 	struct radius_attr_hdr *attr = NULL;
694 	struct radius_attr_hdr_ext *ext;
695 	u8 ext_type = 0;
696 
697 	if (TEST_FAIL())
698 		return NULL;
699 
700 	if (type > 255) {
701 		if (!radius_is_ext_type(type >> 8)) {
702 			wpa_printf(MSG_ERROR,
703 				   "%s: Undefined extended type %d.%d",
704 				   __func__, type >> 8, type & 0xff);
705 			return NULL;
706 		}
707 		ext_type = type & 0xff;
708 		type >>= 8;
709 	} else if (radius_is_ext_type(type)) {
710 		wpa_printf(MSG_ERROR, "%s: Unexpected extended type use for %d",
711 			   __func__, type);
712 	}
713 
714 	if (radius_is_long_ext_type(type)) {
715 		size_t hdr_len = sizeof(struct radius_attr_hdr_ext) + 1;
716 		size_t plen = 255 - hdr_len;
717 		size_t num;
718 
719 		max_len = 4096;
720 		num = (data_len + plen - 1) / plen;
721 		if (num == 0)
722 			num = 1;
723 		buf_needed = num * hdr_len + data_len;
724 	} else if (radius_is_ext_type(type)) {
725 		max_len = RADIUS_MAX_EXT_ATTR_LEN;
726 		buf_needed = sizeof(struct radius_attr_hdr_ext) + data_len;
727 	} else {
728 		max_len = RADIUS_MAX_ATTR_LEN;
729 		buf_needed = sizeof(*attr) + data_len;
730 	}
731 	if (data_len > max_len) {
732 		wpa_printf(MSG_ERROR,
733 			   "%s: too long attribute (%zu > %zu bytes)",
734 			   __func__, data_len, max_len);
735 		return NULL;
736 	}
737 
738 	if (wpabuf_tailroom(msg->buf) < buf_needed) {
739 		/* allocate more space for message buffer */
740 		if (wpabuf_resize(&msg->buf, buf_needed) < 0)
741 			return NULL;
742 		msg->hdr = wpabuf_mhead(msg->buf);
743 	}
744 
745 	if (radius_is_long_ext_type(type)) {
746 		size_t plen = 255 - sizeof(struct radius_attr_hdr_ext) - 1;
747 		size_t alen;
748 
749 		do {
750 			alen = data_len > plen ? plen : data_len;
751 			ext = wpabuf_put(msg->buf,
752 					 sizeof(struct radius_attr_hdr_ext));
753 			if (!attr)
754 				attr = (struct radius_attr_hdr *) ext;
755 			ext->type = type;
756 			ext->length = sizeof(*ext) + 1 + alen;
757 			ext->ext_type = ext_type;
758 			wpabuf_put_u8(msg->buf, data_len > alen ? 0x80 : 0);
759 			wpabuf_put_data(msg->buf, data, data_len);
760 			data += alen;
761 			data_len -= alen;
762 			if (radius_msg_add_attr_to_array(
763 				    msg, (struct radius_attr_hdr *) ext))
764 				return NULL;
765 		} while (data_len > 0);
766 	} else if (radius_is_ext_type(type)) {
767 		ext = wpabuf_put(msg->buf, sizeof(struct radius_attr_hdr_ext));
768 		attr = (struct radius_attr_hdr *) ext;
769 		ext->type = type;
770 		ext->length = sizeof(*ext) + data_len;
771 		ext->ext_type = ext_type;
772 		wpabuf_put_data(msg->buf, data, data_len);
773 		if (radius_msg_add_attr_to_array(msg, attr))
774 			return NULL;
775 	} else {
776 		attr = wpabuf_put(msg->buf, sizeof(struct radius_attr_hdr));
777 		attr->type = type;
778 		attr->length = sizeof(*attr) + data_len;
779 		wpabuf_put_data(msg->buf, data, data_len);
780 		if (radius_msg_add_attr_to_array(msg, attr))
781 			return NULL;
782 	}
783 
784 	return attr;
785 }
786 
787 
788 /**
789  * radius_msg_parse - Parse a RADIUS message
790  * @data: RADIUS message to be parsed
791  * @len: Length of data buffer in octets
792  * Returns: Parsed RADIUS message or %NULL on failure
793  *
794  * This parses a RADIUS message and makes a copy of its data. The caller is
795  * responsible for freeing the returned data with radius_msg_free().
796  */
radius_msg_parse(const u8 * data,size_t len)797 struct radius_msg * radius_msg_parse(const u8 *data, size_t len)
798 {
799 	struct radius_msg *msg;
800 	struct radius_hdr *hdr;
801 	struct radius_attr_hdr *attr;
802 	size_t msg_len;
803 	unsigned char *pos, *end;
804 
805 	if (data == NULL || len < sizeof(*hdr))
806 		return NULL;
807 
808 	hdr = (struct radius_hdr *) data;
809 
810 	msg_len = be_to_host16(hdr->length);
811 	if (msg_len < sizeof(*hdr) || msg_len > len) {
812 		wpa_printf(MSG_INFO, "RADIUS: Invalid message length");
813 		return NULL;
814 	}
815 
816 	if (msg_len < len) {
817 		wpa_printf(MSG_DEBUG, "RADIUS: Ignored %lu extra bytes after "
818 			   "RADIUS message", (unsigned long) len - msg_len);
819 	}
820 
821 	msg = os_zalloc(sizeof(*msg));
822 	if (msg == NULL)
823 		return NULL;
824 
825 	msg->buf = wpabuf_alloc_copy(data, msg_len);
826 	if (msg->buf == NULL || radius_msg_initialize(msg)) {
827 		radius_msg_free(msg);
828 		return NULL;
829 	}
830 	msg->hdr = wpabuf_mhead(msg->buf);
831 
832 	/* parse attributes */
833 	pos = wpabuf_mhead_u8(msg->buf) + sizeof(struct radius_hdr);
834 	end = wpabuf_mhead_u8(msg->buf) + wpabuf_len(msg->buf);
835 	while (pos < end) {
836 		if ((size_t) (end - pos) < sizeof(*attr))
837 			goto fail;
838 
839 		attr = (struct radius_attr_hdr *) pos;
840 
841 		if (attr->length > end - pos || attr->length < sizeof(*attr))
842 			goto fail;
843 
844 		/* TODO: check that attr->length is suitable for attr->type */
845 
846 		if (radius_msg_add_attr_to_array(msg, attr))
847 			goto fail;
848 
849 		pos += attr->length;
850 	}
851 
852 	return msg;
853 
854  fail:
855 	radius_msg_free(msg);
856 	return NULL;
857 }
858 
859 
radius_msg_add_eap(struct radius_msg * msg,const u8 * data,size_t data_len)860 int radius_msg_add_eap(struct radius_msg *msg, const u8 *data, size_t data_len)
861 {
862 	const u8 *pos = data;
863 	size_t left = data_len;
864 
865 	while (left > 0) {
866 		int len;
867 		if (left > RADIUS_MAX_ATTR_LEN)
868 			len = RADIUS_MAX_ATTR_LEN;
869 		else
870 			len = left;
871 
872 		if (!radius_msg_add_attr(msg, RADIUS_ATTR_EAP_MESSAGE,
873 					 pos, len))
874 			return 0;
875 
876 		pos += len;
877 		left -= len;
878 	}
879 
880 	return 1;
881 }
882 
883 
radius_msg_get_eap(struct radius_msg * msg)884 struct wpabuf * radius_msg_get_eap(struct radius_msg *msg)
885 {
886 	struct wpabuf *eap;
887 	size_t len, i;
888 	struct radius_attr_hdr *attr;
889 
890 	if (msg == NULL)
891 		return NULL;
892 
893 	len = 0;
894 	for (i = 0; i < msg->attr_used; i++) {
895 		attr = radius_get_attr_hdr(msg, i);
896 		if (attr->type == RADIUS_ATTR_EAP_MESSAGE &&
897 		    attr->length > sizeof(struct radius_attr_hdr))
898 			len += attr->length - sizeof(struct radius_attr_hdr);
899 	}
900 
901 	if (len == 0)
902 		return NULL;
903 
904 	eap = wpabuf_alloc(len);
905 	if (eap == NULL)
906 		return NULL;
907 
908 	for (i = 0; i < msg->attr_used; i++) {
909 		attr = radius_get_attr_hdr(msg, i);
910 		if (attr->type == RADIUS_ATTR_EAP_MESSAGE &&
911 		    attr->length > sizeof(struct radius_attr_hdr)) {
912 			int flen = attr->length - sizeof(*attr);
913 			wpabuf_put_data(eap, attr + 1, flen);
914 		}
915 	}
916 
917 	return eap;
918 }
919 
920 
radius_msg_verify_msg_auth(struct radius_msg * msg,const u8 * secret,size_t secret_len,const u8 * req_auth)921 int radius_msg_verify_msg_auth(struct radius_msg *msg, const u8 *secret,
922 			       size_t secret_len, const u8 *req_auth)
923 {
924 	u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
925 	u8 orig_authenticator[16];
926 	struct radius_attr_hdr *attr = NULL, *tmp;
927 	size_t i;
928 
929 	for (i = 0; i < msg->attr_used; i++) {
930 		tmp = radius_get_attr_hdr(msg, i);
931 		if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
932 			if (attr != NULL) {
933 				wpa_printf(MSG_INFO, "Multiple Message-Authenticator attributes in RADIUS message");
934 				return 1;
935 			}
936 			attr = tmp;
937 		}
938 	}
939 
940 	if (attr == NULL) {
941 		wpa_printf(MSG_INFO, "No Message-Authenticator attribute found");
942 		return 1;
943 	}
944 
945 	os_memcpy(orig, attr + 1, MD5_MAC_LEN);
946 	os_memset(attr + 1, 0, MD5_MAC_LEN);
947 	if (req_auth) {
948 		os_memcpy(orig_authenticator, msg->hdr->authenticator,
949 			  sizeof(orig_authenticator));
950 		os_memcpy(msg->hdr->authenticator, req_auth,
951 			  sizeof(msg->hdr->authenticator));
952 	}
953 	if (hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
954 		     wpabuf_len(msg->buf), auth) < 0)
955 		return 1;
956 	os_memcpy(attr + 1, orig, MD5_MAC_LEN);
957 	if (req_auth) {
958 		os_memcpy(msg->hdr->authenticator, orig_authenticator,
959 			  sizeof(orig_authenticator));
960 	}
961 
962 	if (os_memcmp_const(orig, auth, MD5_MAC_LEN) != 0) {
963 		wpa_printf(MSG_INFO, "Invalid Message-Authenticator!");
964 		return 1;
965 	}
966 
967 	return 0;
968 }
969 
970 
radius_msg_verify(struct radius_msg * msg,const u8 * secret,size_t secret_len,struct radius_msg * sent_msg,int auth)971 int radius_msg_verify(struct radius_msg *msg, const u8 *secret,
972 		      size_t secret_len, struct radius_msg *sent_msg, int auth)
973 {
974 	const u8 *addr[4];
975 	size_t len[4];
976 	u8 hash[MD5_MAC_LEN];
977 
978 	if (sent_msg == NULL) {
979 		wpa_printf(MSG_INFO, "No matching Access-Request message found");
980 		return 1;
981 	}
982 
983 	if (auth &&
984 	    radius_msg_verify_msg_auth(msg, secret, secret_len,
985 				       sent_msg->hdr->authenticator)) {
986 		return 1;
987 	}
988 
989 	/* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
990 	addr[0] = (u8 *) msg->hdr;
991 	len[0] = 1 + 1 + 2;
992 	addr[1] = sent_msg->hdr->authenticator;
993 	len[1] = MD5_MAC_LEN;
994 	addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
995 	len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
996 	addr[3] = secret;
997 	len[3] = secret_len;
998 	if (md5_vector(4, addr, len, hash) < 0 ||
999 	    os_memcmp_const(hash, msg->hdr->authenticator, MD5_MAC_LEN) != 0) {
1000 		wpa_printf(MSG_INFO, "Response Authenticator invalid!");
1001 		return 1;
1002 	}
1003 
1004 	return 0;
1005 }
1006 
1007 
radius_msg_copy_attr(struct radius_msg * dst,struct radius_msg * src,u8 type)1008 int radius_msg_copy_attr(struct radius_msg *dst, struct radius_msg *src,
1009 			 u8 type)
1010 {
1011 	struct radius_attr_hdr *attr;
1012 	size_t i;
1013 	int count = 0;
1014 
1015 	for (i = 0; i < src->attr_used; i++) {
1016 		attr = radius_get_attr_hdr(src, i);
1017 		if (attr->type == type && attr->length >= sizeof(*attr)) {
1018 			if (!radius_msg_add_attr(dst, type, (u8 *) (attr + 1),
1019 						 attr->length - sizeof(*attr)))
1020 				return -1;
1021 			count++;
1022 		}
1023 	}
1024 
1025 	return count;
1026 }
1027 
1028 
1029 /* Create Request Authenticator. The value should be unique over the lifetime
1030  * of the shared secret between authenticator and authentication server.
1031  */
radius_msg_make_authenticator(struct radius_msg * msg)1032 int radius_msg_make_authenticator(struct radius_msg *msg)
1033 {
1034 	return os_get_random((u8 *) &msg->hdr->authenticator,
1035 			     sizeof(msg->hdr->authenticator));
1036 }
1037 
1038 
1039 /* Get Vendor-specific RADIUS Attribute from a parsed RADIUS message.
1040  * Returns the Attribute payload and sets alen to indicate the length of the
1041  * payload if a vendor attribute with subtype is found, otherwise returns NULL.
1042  * The returned payload is allocated with os_malloc() and caller must free it
1043  * by calling os_free().
1044  */
radius_msg_get_vendor_attr(struct radius_msg * msg,u32 vendor,u8 subtype,size_t * alen)1045 static u8 *radius_msg_get_vendor_attr(struct radius_msg *msg, u32 vendor,
1046 				      u8 subtype, size_t *alen)
1047 {
1048 	u8 *data, *pos;
1049 	size_t i, len;
1050 
1051 	if (msg == NULL)
1052 		return NULL;
1053 
1054 	for (i = 0; i < msg->attr_used; i++) {
1055 		struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
1056 		size_t left;
1057 		u32 vendor_id;
1058 		struct radius_attr_vendor *vhdr;
1059 
1060 		if (attr->type != RADIUS_ATTR_VENDOR_SPECIFIC ||
1061 		    attr->length < sizeof(*attr))
1062 			continue;
1063 
1064 		left = attr->length - sizeof(*attr);
1065 		if (left < 4)
1066 			continue;
1067 
1068 		pos = (u8 *) (attr + 1);
1069 
1070 		os_memcpy(&vendor_id, pos, 4);
1071 		pos += 4;
1072 		left -= 4;
1073 
1074 		if (ntohl(vendor_id) != vendor)
1075 			continue;
1076 
1077 		while (left >= sizeof(*vhdr)) {
1078 			vhdr = (struct radius_attr_vendor *) pos;
1079 			if (vhdr->vendor_length > left ||
1080 			    vhdr->vendor_length < sizeof(*vhdr)) {
1081 				break;
1082 			}
1083 			if (vhdr->vendor_type != subtype) {
1084 				pos += vhdr->vendor_length;
1085 				left -= vhdr->vendor_length;
1086 				continue;
1087 			}
1088 
1089 			len = vhdr->vendor_length - sizeof(*vhdr);
1090 			data = os_memdup(pos + sizeof(*vhdr), len);
1091 			if (data == NULL)
1092 				return NULL;
1093 			if (alen)
1094 				*alen = len;
1095 			return data;
1096 		}
1097 	}
1098 
1099 	return NULL;
1100 }
1101 
1102 
decrypt_ms_key(const u8 * key,size_t len,const u8 * req_authenticator,const u8 * secret,size_t secret_len,size_t * reslen)1103 static u8 * decrypt_ms_key(const u8 *key, size_t len,
1104 			   const u8 *req_authenticator,
1105 			   const u8 *secret, size_t secret_len, size_t *reslen)
1106 {
1107 	u8 *plain, *ppos, *res;
1108 	const u8 *pos;
1109 	size_t left, plen;
1110 	u8 hash[MD5_MAC_LEN];
1111 	int i, first = 1;
1112 	const u8 *addr[3];
1113 	size_t elen[3];
1114 
1115 	/* key: 16-bit salt followed by encrypted key info */
1116 
1117 	if (len < 2 + 16) {
1118 		wpa_printf(MSG_DEBUG, "RADIUS: %s: Len is too small: %d",
1119 			   __func__, (int) len);
1120 		return NULL;
1121 	}
1122 
1123 	pos = key + 2;
1124 	left = len - 2;
1125 	if (left % 16) {
1126 		wpa_printf(MSG_INFO, "RADIUS: Invalid ms key len %lu",
1127 			   (unsigned long) left);
1128 		return NULL;
1129 	}
1130 
1131 	plen = left;
1132 	ppos = plain = os_malloc(plen);
1133 	if (plain == NULL)
1134 		return NULL;
1135 	plain[0] = 0;
1136 
1137 	while (left > 0) {
1138 		/* b(1) = MD5(Secret + Request-Authenticator + Salt)
1139 		 * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
1140 
1141 		addr[0] = secret;
1142 		elen[0] = secret_len;
1143 		if (first) {
1144 			addr[1] = req_authenticator;
1145 			elen[1] = MD5_MAC_LEN;
1146 			addr[2] = key;
1147 			elen[2] = 2; /* Salt */
1148 		} else {
1149 			addr[1] = pos - MD5_MAC_LEN;
1150 			elen[1] = MD5_MAC_LEN;
1151 		}
1152 		if (md5_vector(first ? 3 : 2, addr, elen, hash) < 0) {
1153 			os_free(plain);
1154 			return NULL;
1155 		}
1156 		first = 0;
1157 
1158 		for (i = 0; i < MD5_MAC_LEN; i++)
1159 			*ppos++ = *pos++ ^ hash[i];
1160 		left -= MD5_MAC_LEN;
1161 	}
1162 
1163 	if (plain[0] == 0 || plain[0] > plen - 1) {
1164 		wpa_printf(MSG_INFO, "RADIUS: Failed to decrypt MPPE key");
1165 		os_free(plain);
1166 		return NULL;
1167 	}
1168 
1169 	res = os_memdup(plain + 1, plain[0]);
1170 	if (res == NULL) {
1171 		os_free(plain);
1172 		return NULL;
1173 	}
1174 	if (reslen)
1175 		*reslen = plain[0];
1176 	os_free(plain);
1177 	return res;
1178 }
1179 
1180 
encrypt_ms_key(const u8 * key,size_t key_len,u16 salt,const u8 * req_authenticator,const u8 * secret,size_t secret_len,u8 * ebuf,size_t * elen)1181 static void encrypt_ms_key(const u8 *key, size_t key_len, u16 salt,
1182 			   const u8 *req_authenticator,
1183 			   const u8 *secret, size_t secret_len,
1184 			   u8 *ebuf, size_t *elen)
1185 {
1186 	int i, len, first = 1;
1187 	u8 hash[MD5_MAC_LEN], saltbuf[2], *pos;
1188 	const u8 *addr[3];
1189 	size_t _len[3];
1190 
1191 	WPA_PUT_BE16(saltbuf, salt);
1192 
1193 	len = 1 + key_len;
1194 	if (len & 0x0f) {
1195 		len = (len & 0xf0) + 16;
1196 	}
1197 	os_memset(ebuf, 0, len);
1198 	ebuf[0] = key_len;
1199 	os_memcpy(ebuf + 1, key, key_len);
1200 
1201 	*elen = len;
1202 
1203 	pos = ebuf;
1204 	while (len > 0) {
1205 		/* b(1) = MD5(Secret + Request-Authenticator + Salt)
1206 		 * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
1207 		addr[0] = secret;
1208 		_len[0] = secret_len;
1209 		if (first) {
1210 			addr[1] = req_authenticator;
1211 			_len[1] = MD5_MAC_LEN;
1212 			addr[2] = saltbuf;
1213 			_len[2] = sizeof(saltbuf);
1214 		} else {
1215 			addr[1] = pos - MD5_MAC_LEN;
1216 			_len[1] = MD5_MAC_LEN;
1217 		}
1218 		md5_vector(first ? 3 : 2, addr, _len, hash);
1219 		first = 0;
1220 
1221 		for (i = 0; i < MD5_MAC_LEN; i++)
1222 			*pos++ ^= hash[i];
1223 
1224 		len -= MD5_MAC_LEN;
1225 	}
1226 }
1227 
1228 
1229 struct radius_ms_mppe_keys *
radius_msg_get_ms_keys(struct radius_msg * msg,struct radius_msg * sent_msg,const u8 * secret,size_t secret_len)1230 radius_msg_get_ms_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
1231 		       const u8 *secret, size_t secret_len)
1232 {
1233 	u8 *key;
1234 	size_t keylen;
1235 	struct radius_ms_mppe_keys *keys;
1236 
1237 	if (msg == NULL || sent_msg == NULL)
1238 		return NULL;
1239 
1240 	keys = os_zalloc(sizeof(*keys));
1241 	if (keys == NULL)
1242 		return NULL;
1243 
1244 	key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
1245 					 RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY,
1246 					 &keylen);
1247 	if (key) {
1248 		keys->send = decrypt_ms_key(key, keylen,
1249 					    sent_msg->hdr->authenticator,
1250 					    secret, secret_len,
1251 					    &keys->send_len);
1252 		if (!keys->send) {
1253 			wpa_printf(MSG_DEBUG,
1254 				   "RADIUS: Failed to decrypt send key");
1255 		}
1256 		os_free(key);
1257 	}
1258 
1259 	key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
1260 					 RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY,
1261 					 &keylen);
1262 	if (key) {
1263 		keys->recv = decrypt_ms_key(key, keylen,
1264 					    sent_msg->hdr->authenticator,
1265 					    secret, secret_len,
1266 					    &keys->recv_len);
1267 		if (!keys->recv) {
1268 			wpa_printf(MSG_DEBUG,
1269 				   "RADIUS: Failed to decrypt recv key");
1270 		}
1271 		os_free(key);
1272 	}
1273 
1274 	return keys;
1275 }
1276 
1277 
1278 struct radius_ms_mppe_keys *
radius_msg_get_cisco_keys(struct radius_msg * msg,struct radius_msg * sent_msg,const u8 * secret,size_t secret_len)1279 radius_msg_get_cisco_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
1280 			  const u8 *secret, size_t secret_len)
1281 {
1282 	u8 *key;
1283 	size_t keylen;
1284 	struct radius_ms_mppe_keys *keys;
1285 
1286 	if (msg == NULL || sent_msg == NULL)
1287 		return NULL;
1288 
1289 	keys = os_zalloc(sizeof(*keys));
1290 	if (keys == NULL)
1291 		return NULL;
1292 
1293 	key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_CISCO,
1294 					 RADIUS_CISCO_AV_PAIR, &keylen);
1295 	if (key && keylen == 51 &&
1296 	    os_memcmp(key, "leap:session-key=", 17) == 0) {
1297 		keys->recv = decrypt_ms_key(key + 17, keylen - 17,
1298 					    sent_msg->hdr->authenticator,
1299 					    secret, secret_len,
1300 					    &keys->recv_len);
1301 	}
1302 	os_free(key);
1303 
1304 	return keys;
1305 }
1306 
1307 
radius_msg_add_mppe_keys(struct radius_msg * msg,const u8 * req_authenticator,const u8 * secret,size_t secret_len,const u8 * send_key,size_t send_key_len,const u8 * recv_key,size_t recv_key_len)1308 int radius_msg_add_mppe_keys(struct radius_msg *msg,
1309 			     const u8 *req_authenticator,
1310 			     const u8 *secret, size_t secret_len,
1311 			     const u8 *send_key, size_t send_key_len,
1312 			     const u8 *recv_key, size_t recv_key_len)
1313 {
1314 	struct radius_attr_hdr *attr;
1315 	u32 vendor_id = htonl(RADIUS_VENDOR_ID_MICROSOFT);
1316 	u8 *buf;
1317 	struct radius_attr_vendor *vhdr;
1318 	u8 *pos;
1319 	size_t elen;
1320 	int hlen;
1321 	u16 salt;
1322 
1323 	hlen = sizeof(vendor_id) + sizeof(*vhdr) + 2;
1324 
1325 	/* MS-MPPE-Send-Key */
1326 	buf = os_malloc(hlen + send_key_len + 16);
1327 	if (buf == NULL) {
1328 		return 0;
1329 	}
1330 	pos = buf;
1331 	os_memcpy(pos, &vendor_id, sizeof(vendor_id));
1332 	pos += sizeof(vendor_id);
1333 	vhdr = (struct radius_attr_vendor *) pos;
1334 	vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY;
1335 	pos = (u8 *) (vhdr + 1);
1336 	if (os_get_random((u8 *) &salt, sizeof(salt)) < 0) {
1337 		os_free(buf);
1338 		return 0;
1339 	}
1340 	salt |= 0x8000;
1341 	WPA_PUT_BE16(pos, salt);
1342 	pos += 2;
1343 	encrypt_ms_key(send_key, send_key_len, salt, req_authenticator, secret,
1344 		       secret_len, pos, &elen);
1345 	vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
1346 
1347 	attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1348 				   buf, hlen + elen);
1349 	os_free(buf);
1350 	if (attr == NULL) {
1351 		return 0;
1352 	}
1353 
1354 	/* MS-MPPE-Recv-Key */
1355 	buf = os_malloc(hlen + recv_key_len + 16);
1356 	if (buf == NULL) {
1357 		return 0;
1358 	}
1359 	pos = buf;
1360 	os_memcpy(pos, &vendor_id, sizeof(vendor_id));
1361 	pos += sizeof(vendor_id);
1362 	vhdr = (struct radius_attr_vendor *) pos;
1363 	vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY;
1364 	pos = (u8 *) (vhdr + 1);
1365 	salt ^= 1;
1366 	WPA_PUT_BE16(pos, salt);
1367 	pos += 2;
1368 	encrypt_ms_key(recv_key, recv_key_len, salt, req_authenticator, secret,
1369 		       secret_len, pos, &elen);
1370 	vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
1371 
1372 	attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1373 				   buf, hlen + elen);
1374 	os_free(buf);
1375 	if (attr == NULL) {
1376 		return 0;
1377 	}
1378 
1379 	return 1;
1380 }
1381 
1382 
radius_msg_add_wfa(struct radius_msg * msg,u8 subtype,const u8 * data,size_t len)1383 int radius_msg_add_wfa(struct radius_msg *msg, u8 subtype, const u8 *data,
1384 		       size_t len)
1385 {
1386 	struct radius_attr_hdr *attr;
1387 	u8 *buf, *pos;
1388 	size_t alen;
1389 
1390 	alen = 4 + 2 + len;
1391 	buf = os_malloc(alen);
1392 	if (buf == NULL)
1393 		return 0;
1394 	pos = buf;
1395 	WPA_PUT_BE32(pos, RADIUS_VENDOR_ID_WFA);
1396 	pos += 4;
1397 	*pos++ = subtype;
1398 	*pos++ = 2 + len;
1399 	os_memcpy(pos, data, len);
1400 	attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1401 				   buf, alen);
1402 	os_free(buf);
1403 	if (attr == NULL)
1404 		return 0;
1405 
1406 	return 1;
1407 }
1408 
1409 
radius_msg_add_ext_vs(struct radius_msg * msg,u16 type,u32 vendor_id,u8 vendor_type,const u8 * data,size_t len)1410 int radius_msg_add_ext_vs(struct radius_msg *msg, u16 type, u32 vendor_id,
1411 			  u8 vendor_type, const u8 *data, size_t len)
1412 {
1413 	struct radius_attr_hdr *attr;
1414 	u8 *buf, *pos;
1415 	size_t alen;
1416 
1417 	alen = 4 + 1 + len;
1418 	buf = os_malloc(alen);
1419 	if (!buf)
1420 		return 0;
1421 	pos = buf;
1422 	WPA_PUT_BE32(pos, vendor_id);
1423 	pos += 4;
1424 	*pos++ = vendor_type;
1425 	os_memcpy(pos, data, len);
1426 	attr = radius_msg_add_attr(msg, type, buf, alen);
1427 	os_free(buf);
1428 	return attr != NULL;
1429 }
1430 
1431 
radius_user_password_hide(struct radius_msg * msg,const u8 * data,size_t data_len,const u8 * secret,size_t secret_len,u8 * buf,size_t buf_len)1432 int radius_user_password_hide(struct radius_msg *msg,
1433 			      const u8 *data, size_t data_len,
1434 			      const u8 *secret, size_t secret_len,
1435 			      u8 *buf, size_t buf_len)
1436 {
1437 	size_t padlen, i, pos;
1438 	const u8 *addr[2];
1439 	size_t len[2];
1440 	u8 hash[16];
1441 
1442 	if (data_len + 16 > buf_len)
1443 		return -1;
1444 
1445 	os_memcpy(buf, data, data_len);
1446 
1447 	padlen = data_len % 16;
1448 	if (padlen && data_len < buf_len) {
1449 		padlen = 16 - padlen;
1450 		os_memset(buf + data_len, 0, padlen);
1451 		buf_len = data_len + padlen;
1452 	} else {
1453 		buf_len = data_len;
1454 	}
1455 
1456 	addr[0] = secret;
1457 	len[0] = secret_len;
1458 	addr[1] = msg->hdr->authenticator;
1459 	len[1] = 16;
1460 	md5_vector(2, addr, len, hash);
1461 
1462 	for (i = 0; i < 16; i++)
1463 		buf[i] ^= hash[i];
1464 	pos = 16;
1465 
1466 	while (pos < buf_len) {
1467 		addr[0] = secret;
1468 		len[0] = secret_len;
1469 		addr[1] = &buf[pos - 16];
1470 		len[1] = 16;
1471 		md5_vector(2, addr, len, hash);
1472 
1473 		for (i = 0; i < 16; i++)
1474 			buf[pos + i] ^= hash[i];
1475 
1476 		pos += 16;
1477 	}
1478 
1479 	return buf_len;
1480 }
1481 
1482 
1483 /* Add User-Password attribute to a RADIUS message and encrypt it as specified
1484  * in RFC 2865, Chap. 5.2 */
1485 struct radius_attr_hdr *
radius_msg_add_attr_user_password(struct radius_msg * msg,const u8 * data,size_t data_len,const u8 * secret,size_t secret_len)1486 radius_msg_add_attr_user_password(struct radius_msg *msg,
1487 				  const u8 *data, size_t data_len,
1488 				  const u8 *secret, size_t secret_len)
1489 {
1490 	u8 buf[128];
1491 	int res;
1492 
1493 	res = radius_user_password_hide(msg, data, data_len,
1494 					secret, secret_len, buf, sizeof(buf));
1495 	if (res < 0)
1496 		return NULL;
1497 
1498 	return radius_msg_add_attr(msg, RADIUS_ATTR_USER_PASSWORD,
1499 				   buf, res);
1500 }
1501 
1502 
radius_msg_get_attr(struct radius_msg * msg,u8 type,u8 * buf,size_t len)1503 int radius_msg_get_attr(struct radius_msg *msg, u8 type, u8 *buf, size_t len)
1504 {
1505 	struct radius_attr_hdr *attr = NULL, *tmp;
1506 	size_t i, dlen;
1507 
1508 	for (i = 0; i < msg->attr_used; i++) {
1509 		tmp = radius_get_attr_hdr(msg, i);
1510 		if (tmp->type == type) {
1511 			attr = tmp;
1512 			break;
1513 		}
1514 	}
1515 
1516 	if (!attr || attr->length < sizeof(*attr))
1517 		return -1;
1518 
1519 	dlen = attr->length - sizeof(*attr);
1520 	if (buf)
1521 		os_memcpy(buf, (attr + 1), dlen > len ? len : dlen);
1522 	return dlen;
1523 }
1524 
1525 
radius_msg_get_attr_ptr(struct radius_msg * msg,u8 type,u8 ** buf,size_t * len,const u8 * start)1526 int radius_msg_get_attr_ptr(struct radius_msg *msg, u8 type, u8 **buf,
1527 			    size_t *len, const u8 *start)
1528 {
1529 	size_t i;
1530 	struct radius_attr_hdr *attr = NULL, *tmp;
1531 
1532 	for (i = 0; i < msg->attr_used; i++) {
1533 		tmp = radius_get_attr_hdr(msg, i);
1534 		if (tmp->type == type &&
1535 		    (start == NULL || (u8 *) tmp > start)) {
1536 			attr = tmp;
1537 			break;
1538 		}
1539 	}
1540 
1541 	if (!attr || attr->length < sizeof(*attr))
1542 		return -1;
1543 
1544 	*buf = (u8 *) (attr + 1);
1545 	*len = attr->length - sizeof(*attr);
1546 	return 0;
1547 }
1548 
1549 
radius_msg_count_attr(struct radius_msg * msg,u8 type,int min_len)1550 int radius_msg_count_attr(struct radius_msg *msg, u8 type, int min_len)
1551 {
1552 	size_t i;
1553 	int count;
1554 
1555 	for (count = 0, i = 0; i < msg->attr_used; i++) {
1556 		struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
1557 		if (attr->type == type &&
1558 		    attr->length >= sizeof(struct radius_attr_hdr) + min_len)
1559 			count++;
1560 	}
1561 
1562 	return count;
1563 }
1564 
1565 
1566 struct radius_tunnel_attrs {
1567 	int tag_used;
1568 	int type; /* Tunnel-Type */
1569 	int medium_type; /* Tunnel-Medium-Type */
1570 	int vlanid;
1571 };
1572 
1573 
cmp_int(const void * a,const void * b)1574 static int cmp_int(const void *a, const void *b)
1575 {
1576 	int x, y;
1577 
1578 	x = *((int *) a);
1579 	y = *((int *) b);
1580 	return (x - y);
1581 }
1582 
1583 
1584 /**
1585  * radius_msg_get_vlanid - Parse RADIUS attributes for VLAN tunnel information
1586  * The k tagged vlans found are sorted by vlan_id and stored in the first k
1587  * items of tagged.
1588  *
1589  * @msg: RADIUS message
1590  * @untagged: Pointer to store untagged vid
1591  * @numtagged: Size of tagged
1592  * @tagged: Pointer to store tagged list
1593  *
1594  * Returns: 0 if neither tagged nor untagged configuration is found, 1 otherwise
1595  */
radius_msg_get_vlanid(struct radius_msg * msg,int * untagged,int numtagged,int * tagged)1596 int radius_msg_get_vlanid(struct radius_msg *msg, int *untagged, int numtagged,
1597 			  int *tagged)
1598 {
1599 	struct radius_tunnel_attrs tunnel[RADIUS_TUNNEL_TAGS], *tun;
1600 	size_t i;
1601 	struct radius_attr_hdr *attr = NULL;
1602 	const u8 *data;
1603 	char buf[10];
1604 	size_t dlen;
1605 	int j, taggedidx = 0, vlan_id;
1606 
1607 	os_memset(&tunnel, 0, sizeof(tunnel));
1608 	for (j = 0; j < numtagged; j++)
1609 		tagged[j] = 0;
1610 	*untagged = 0;
1611 
1612 	for (i = 0; i < msg->attr_used; i++) {
1613 		attr = radius_get_attr_hdr(msg, i);
1614 		if (attr->length < sizeof(*attr))
1615 			return -1;
1616 		data = (const u8 *) (attr + 1);
1617 		dlen = attr->length - sizeof(*attr);
1618 		if (attr->length < 3)
1619 			continue;
1620 		if (data[0] >= RADIUS_TUNNEL_TAGS)
1621 			tun = &tunnel[0];
1622 		else
1623 			tun = &tunnel[data[0]];
1624 
1625 		switch (attr->type) {
1626 		case RADIUS_ATTR_TUNNEL_TYPE:
1627 			if (attr->length != 6)
1628 				break;
1629 			tun->tag_used++;
1630 			tun->type = WPA_GET_BE24(data + 1);
1631 			break;
1632 		case RADIUS_ATTR_TUNNEL_MEDIUM_TYPE:
1633 			if (attr->length != 6)
1634 				break;
1635 			tun->tag_used++;
1636 			tun->medium_type = WPA_GET_BE24(data + 1);
1637 			break;
1638 		case RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID:
1639 			if (data[0] < RADIUS_TUNNEL_TAGS) {
1640 				data++;
1641 				dlen--;
1642 			}
1643 			if (dlen >= sizeof(buf))
1644 				break;
1645 			os_memcpy(buf, data, dlen);
1646 			buf[dlen] = '\0';
1647 			vlan_id = atoi(buf);
1648 			if (vlan_id <= 0)
1649 				break;
1650 			tun->tag_used++;
1651 			tun->vlanid = vlan_id;
1652 			break;
1653 		case RADIUS_ATTR_EGRESS_VLANID: /* RFC 4675 */
1654 			if (attr->length != 6)
1655 				break;
1656 			vlan_id = WPA_GET_BE24(data + 1);
1657 			if (vlan_id <= 0)
1658 				break;
1659 			if (data[0] == 0x32)
1660 				*untagged = vlan_id;
1661 			else if (data[0] == 0x31 && tagged &&
1662 				 taggedidx < numtagged)
1663 				tagged[taggedidx++] = vlan_id;
1664 			break;
1665 		}
1666 	}
1667 
1668 	/* Use tunnel with the lowest tag for untagged VLAN id */
1669 	for (i = 0; i < RADIUS_TUNNEL_TAGS; i++) {
1670 		tun = &tunnel[i];
1671 		if (tun->tag_used &&
1672 		    tun->type == RADIUS_TUNNEL_TYPE_VLAN &&
1673 		    tun->medium_type == RADIUS_TUNNEL_MEDIUM_TYPE_802 &&
1674 		    tun->vlanid > 0) {
1675 			*untagged = tun->vlanid;
1676 			break;
1677 		}
1678 	}
1679 
1680 	if (taggedidx)
1681 		qsort(tagged, taggedidx, sizeof(int), cmp_int);
1682 
1683 	if (*untagged > 0 || taggedidx)
1684 		return 1;
1685 	return 0;
1686 }
1687 
1688 
1689 /**
1690  * radius_msg_get_tunnel_password - Parse RADIUS attribute Tunnel-Password
1691  * @msg: Received RADIUS message
1692  * @keylen: Length of returned password
1693  * @secret: RADIUS shared secret
1694  * @secret_len: Length of secret
1695  * @sent_msg: Sent RADIUS message
1696  * @n: Number of password attribute to return (starting with 0)
1697  * Returns: Pointer to n-th password (free with os_free) or %NULL
1698  */
radius_msg_get_tunnel_password(struct radius_msg * msg,int * keylen,const u8 * secret,size_t secret_len,struct radius_msg * sent_msg,size_t n)1699 char * radius_msg_get_tunnel_password(struct radius_msg *msg, int *keylen,
1700 				      const u8 *secret, size_t secret_len,
1701 				      struct radius_msg *sent_msg, size_t n)
1702 {
1703 	u8 *buf = NULL;
1704 	size_t buflen;
1705 	const u8 *salt;
1706 	u8 *str;
1707 	const u8 *addr[3];
1708 	size_t len[3];
1709 	u8 hash[16];
1710 	u8 *pos;
1711 	size_t i, j = 0;
1712 	struct radius_attr_hdr *attr;
1713 	const u8 *data;
1714 	size_t dlen;
1715 	const u8 *fdata = NULL; /* points to found item */
1716 	size_t fdlen = -1;
1717 	char *ret = NULL;
1718 
1719 	/* find n-th valid Tunnel-Password attribute */
1720 	for (i = 0; i < msg->attr_used; i++) {
1721 		attr = radius_get_attr_hdr(msg, i);
1722 		if (attr == NULL ||
1723 		    attr->type != RADIUS_ATTR_TUNNEL_PASSWORD) {
1724 			continue;
1725 		}
1726 		if (attr->length <= 5)
1727 			continue;
1728 		data = (const u8 *) (attr + 1);
1729 		dlen = attr->length - sizeof(*attr);
1730 		if (dlen <= 3 || dlen % 16 != 3)
1731 			continue;
1732 		j++;
1733 		if (j <= n)
1734 			continue;
1735 
1736 		fdata = data;
1737 		fdlen = dlen;
1738 		break;
1739 	}
1740 	if (fdata == NULL)
1741 		goto out;
1742 
1743 	/* alloc writable memory for decryption */
1744 	buf = os_memdup(fdata, fdlen);
1745 	if (buf == NULL)
1746 		goto out;
1747 	buflen = fdlen;
1748 
1749 	/* init pointers */
1750 	salt = buf + 1;
1751 	str = buf + 3;
1752 
1753 	/* decrypt blocks */
1754 	pos = buf + buflen - 16; /* last block */
1755 	while (pos >= str + 16) { /* all but the first block */
1756 		addr[0] = secret;
1757 		len[0] = secret_len;
1758 		addr[1] = pos - 16;
1759 		len[1] = 16;
1760 		md5_vector(2, addr, len, hash);
1761 
1762 		for (i = 0; i < 16; i++)
1763 			pos[i] ^= hash[i];
1764 
1765 		pos -= 16;
1766 	}
1767 
1768 	/* decrypt first block */
1769 	if (str != pos)
1770 		goto out;
1771 	addr[0] = secret;
1772 	len[0] = secret_len;
1773 	addr[1] = sent_msg->hdr->authenticator;
1774 	len[1] = 16;
1775 	addr[2] = salt;
1776 	len[2] = 2;
1777 	md5_vector(3, addr, len, hash);
1778 
1779 	for (i = 0; i < 16; i++)
1780 		pos[i] ^= hash[i];
1781 
1782 	/* derive plaintext length from first subfield */
1783 	*keylen = (unsigned char) str[0];
1784 	if ((u8 *) (str + *keylen) >= (u8 *) (buf + buflen)) {
1785 		/* decryption error - invalid key length */
1786 		goto out;
1787 	}
1788 	if (*keylen == 0) {
1789 		/* empty password */
1790 		goto out;
1791 	}
1792 
1793 	/* copy passphrase into new buffer */
1794 	ret = os_malloc(*keylen);
1795 	if (ret)
1796 		os_memcpy(ret, str + 1, *keylen);
1797 
1798 out:
1799 	/* return new buffer */
1800 	os_free(buf);
1801 	return ret;
1802 }
1803 
1804 
radius_free_class(struct radius_class_data * c)1805 void radius_free_class(struct radius_class_data *c)
1806 {
1807 	size_t i;
1808 	if (c == NULL)
1809 		return;
1810 	for (i = 0; i < c->count; i++)
1811 		os_free(c->attr[i].data);
1812 	os_free(c->attr);
1813 	c->attr = NULL;
1814 	c->count = 0;
1815 }
1816 
1817 
radius_copy_class(struct radius_class_data * dst,const struct radius_class_data * src)1818 int radius_copy_class(struct radius_class_data *dst,
1819 		      const struct radius_class_data *src)
1820 {
1821 	size_t i;
1822 
1823 	if (src->attr == NULL)
1824 		return 0;
1825 
1826 	dst->attr = os_calloc(src->count, sizeof(struct radius_attr_data));
1827 	if (dst->attr == NULL)
1828 		return -1;
1829 
1830 	dst->count = 0;
1831 
1832 	for (i = 0; i < src->count; i++) {
1833 		dst->attr[i].data = os_memdup(src->attr[i].data,
1834 					      src->attr[i].len);
1835 		if (dst->attr[i].data == NULL)
1836 			break;
1837 		dst->count++;
1838 		dst->attr[i].len = src->attr[i].len;
1839 	}
1840 
1841 	return 0;
1842 }
1843 
1844 
radius_msg_find_unlisted_attr(struct radius_msg * msg,u8 * attrs)1845 u8 radius_msg_find_unlisted_attr(struct radius_msg *msg, u8 *attrs)
1846 {
1847 	size_t i, j;
1848 	struct radius_attr_hdr *attr;
1849 
1850 	for (i = 0; i < msg->attr_used; i++) {
1851 		attr = radius_get_attr_hdr(msg, i);
1852 
1853 		for (j = 0; attrs[j]; j++) {
1854 			if (attr->type == attrs[j])
1855 				break;
1856 		}
1857 
1858 		if (attrs[j] == 0)
1859 			return attr->type; /* unlisted attr */
1860 	}
1861 
1862 	return 0;
1863 }
1864 
1865 
radius_gen_session_id(u8 * id,size_t len)1866 int radius_gen_session_id(u8 *id, size_t len)
1867 {
1868 	/*
1869 	 * Acct-Session-Id and Acct-Multi-Session-Id should be globally and
1870 	 * temporarily unique. A high quality random number is required
1871 	 * therefore. This could be be improved by switching to a GUID.
1872 	 */
1873 	return os_get_random(id, len);
1874 }
1875