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