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