Home | History | Annotate | Download | only in radius
      1 /*
      2  * RADIUS message processing
      3  * Copyright (c) 2002-2009, 2011-2015, Jouni Malinen <j (at) 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 
     52 struct radius_hdr * radius_msg_get_hdr(struct radius_msg *msg)
     53 {
     54 	return msg->hdr;
     55 }
     56 
     57 
     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 *
     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 
     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 
     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  */
    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  */
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 (TEST_FAIL())
    635 		return NULL;
    636 
    637 	if (data_len > RADIUS_MAX_ATTR_LEN) {
    638 		wpa_printf(MSG_ERROR, "radius_msg_add_attr: too long attribute (%lu bytes)",
    639 		       (unsigned long) data_len);
    640 		return NULL;
    641 	}
    642 
    643 	buf_needed = sizeof(*attr) + data_len;
    644 
    645 	if (wpabuf_tailroom(msg->buf) < buf_needed) {
    646 		/* allocate more space for message buffer */
    647 		if (wpabuf_resize(&msg->buf, buf_needed) < 0)
    648 			return NULL;
    649 		msg->hdr = wpabuf_mhead(msg->buf);
    650 	}
    651 
    652 	attr = wpabuf_put(msg->buf, sizeof(struct radius_attr_hdr));
    653 	attr->type = type;
    654 	attr->length = sizeof(*attr) + data_len;
    655 	wpabuf_put_data(msg->buf, data, data_len);
    656 
    657 	if (radius_msg_add_attr_to_array(msg, attr))
    658 		return NULL;
    659 
    660 	return attr;
    661 }
    662 
    663 
    664 /**
    665  * radius_msg_parse - Parse a RADIUS message
    666  * @data: RADIUS message to be parsed
    667  * @len: Length of data buffer in octets
    668  * Returns: Parsed RADIUS message or %NULL on failure
    669  *
    670  * This parses a RADIUS message and makes a copy of its data. The caller is
    671  * responsible for freeing the returned data with radius_msg_free().
    672  */
    673 struct radius_msg * radius_msg_parse(const u8 *data, size_t len)
    674 {
    675 	struct radius_msg *msg;
    676 	struct radius_hdr *hdr;
    677 	struct radius_attr_hdr *attr;
    678 	size_t msg_len;
    679 	unsigned char *pos, *end;
    680 
    681 	if (data == NULL || len < sizeof(*hdr))
    682 		return NULL;
    683 
    684 	hdr = (struct radius_hdr *) data;
    685 
    686 	msg_len = be_to_host16(hdr->length);
    687 	if (msg_len < sizeof(*hdr) || msg_len > len) {
    688 		wpa_printf(MSG_INFO, "RADIUS: Invalid message length");
    689 		return NULL;
    690 	}
    691 
    692 	if (msg_len < len) {
    693 		wpa_printf(MSG_DEBUG, "RADIUS: Ignored %lu extra bytes after "
    694 			   "RADIUS message", (unsigned long) len - msg_len);
    695 	}
    696 
    697 	msg = os_zalloc(sizeof(*msg));
    698 	if (msg == NULL)
    699 		return NULL;
    700 
    701 	msg->buf = wpabuf_alloc_copy(data, msg_len);
    702 	if (msg->buf == NULL || radius_msg_initialize(msg)) {
    703 		radius_msg_free(msg);
    704 		return NULL;
    705 	}
    706 	msg->hdr = wpabuf_mhead(msg->buf);
    707 
    708 	/* parse attributes */
    709 	pos = wpabuf_mhead_u8(msg->buf) + sizeof(struct radius_hdr);
    710 	end = wpabuf_mhead_u8(msg->buf) + wpabuf_len(msg->buf);
    711 	while (pos < end) {
    712 		if ((size_t) (end - pos) < sizeof(*attr))
    713 			goto fail;
    714 
    715 		attr = (struct radius_attr_hdr *) pos;
    716 
    717 		if (attr->length > end - pos || attr->length < sizeof(*attr))
    718 			goto fail;
    719 
    720 		/* TODO: check that attr->length is suitable for attr->type */
    721 
    722 		if (radius_msg_add_attr_to_array(msg, attr))
    723 			goto fail;
    724 
    725 		pos += attr->length;
    726 	}
    727 
    728 	return msg;
    729 
    730  fail:
    731 	radius_msg_free(msg);
    732 	return NULL;
    733 }
    734 
    735 
    736 int radius_msg_add_eap(struct radius_msg *msg, const u8 *data, size_t data_len)
    737 {
    738 	const u8 *pos = data;
    739 	size_t left = data_len;
    740 
    741 	while (left > 0) {
    742 		int len;
    743 		if (left > RADIUS_MAX_ATTR_LEN)
    744 			len = RADIUS_MAX_ATTR_LEN;
    745 		else
    746 			len = left;
    747 
    748 		if (!radius_msg_add_attr(msg, RADIUS_ATTR_EAP_MESSAGE,
    749 					 pos, len))
    750 			return 0;
    751 
    752 		pos += len;
    753 		left -= len;
    754 	}
    755 
    756 	return 1;
    757 }
    758 
    759 
    760 struct wpabuf * radius_msg_get_eap(struct radius_msg *msg)
    761 {
    762 	struct wpabuf *eap;
    763 	size_t len, i;
    764 	struct radius_attr_hdr *attr;
    765 
    766 	if (msg == NULL)
    767 		return NULL;
    768 
    769 	len = 0;
    770 	for (i = 0; i < msg->attr_used; i++) {
    771 		attr = radius_get_attr_hdr(msg, i);
    772 		if (attr->type == RADIUS_ATTR_EAP_MESSAGE &&
    773 		    attr->length > sizeof(struct radius_attr_hdr))
    774 			len += attr->length - sizeof(struct radius_attr_hdr);
    775 	}
    776 
    777 	if (len == 0)
    778 		return NULL;
    779 
    780 	eap = wpabuf_alloc(len);
    781 	if (eap == NULL)
    782 		return NULL;
    783 
    784 	for (i = 0; i < msg->attr_used; i++) {
    785 		attr = radius_get_attr_hdr(msg, i);
    786 		if (attr->type == RADIUS_ATTR_EAP_MESSAGE &&
    787 		    attr->length > sizeof(struct radius_attr_hdr)) {
    788 			int flen = attr->length - sizeof(*attr);
    789 			wpabuf_put_data(eap, attr + 1, flen);
    790 		}
    791 	}
    792 
    793 	return eap;
    794 }
    795 
    796 
    797 int radius_msg_verify_msg_auth(struct radius_msg *msg, const u8 *secret,
    798 			       size_t secret_len, const u8 *req_auth)
    799 {
    800 	u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
    801 	u8 orig_authenticator[16];
    802 	struct radius_attr_hdr *attr = NULL, *tmp;
    803 	size_t i;
    804 
    805 	for (i = 0; i < msg->attr_used; i++) {
    806 		tmp = radius_get_attr_hdr(msg, i);
    807 		if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
    808 			if (attr != NULL) {
    809 				wpa_printf(MSG_INFO, "Multiple Message-Authenticator attributes in RADIUS message");
    810 				return 1;
    811 			}
    812 			attr = tmp;
    813 		}
    814 	}
    815 
    816 	if (attr == NULL) {
    817 		wpa_printf(MSG_INFO, "No Message-Authenticator attribute found");
    818 		return 1;
    819 	}
    820 
    821 	os_memcpy(orig, attr + 1, MD5_MAC_LEN);
    822 	os_memset(attr + 1, 0, MD5_MAC_LEN);
    823 	if (req_auth) {
    824 		os_memcpy(orig_authenticator, msg->hdr->authenticator,
    825 			  sizeof(orig_authenticator));
    826 		os_memcpy(msg->hdr->authenticator, req_auth,
    827 			  sizeof(msg->hdr->authenticator));
    828 	}
    829 	if (hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
    830 		     wpabuf_len(msg->buf), auth) < 0)
    831 		return 1;
    832 	os_memcpy(attr + 1, orig, MD5_MAC_LEN);
    833 	if (req_auth) {
    834 		os_memcpy(msg->hdr->authenticator, orig_authenticator,
    835 			  sizeof(orig_authenticator));
    836 	}
    837 
    838 	if (os_memcmp_const(orig, auth, MD5_MAC_LEN) != 0) {
    839 		wpa_printf(MSG_INFO, "Invalid Message-Authenticator!");
    840 		return 1;
    841 	}
    842 
    843 	return 0;
    844 }
    845 
    846 
    847 int radius_msg_verify(struct radius_msg *msg, const u8 *secret,
    848 		      size_t secret_len, struct radius_msg *sent_msg, int auth)
    849 {
    850 	const u8 *addr[4];
    851 	size_t len[4];
    852 	u8 hash[MD5_MAC_LEN];
    853 
    854 	if (sent_msg == NULL) {
    855 		wpa_printf(MSG_INFO, "No matching Access-Request message found");
    856 		return 1;
    857 	}
    858 
    859 	if (auth &&
    860 	    radius_msg_verify_msg_auth(msg, secret, secret_len,
    861 				       sent_msg->hdr->authenticator)) {
    862 		return 1;
    863 	}
    864 
    865 	/* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
    866 	addr[0] = (u8 *) msg->hdr;
    867 	len[0] = 1 + 1 + 2;
    868 	addr[1] = sent_msg->hdr->authenticator;
    869 	len[1] = MD5_MAC_LEN;
    870 	addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
    871 	len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
    872 	addr[3] = secret;
    873 	len[3] = secret_len;
    874 	if (md5_vector(4, addr, len, hash) < 0 ||
    875 	    os_memcmp_const(hash, msg->hdr->authenticator, MD5_MAC_LEN) != 0) {
    876 		wpa_printf(MSG_INFO, "Response Authenticator invalid!");
    877 		return 1;
    878 	}
    879 
    880 	return 0;
    881 }
    882 
    883 
    884 int radius_msg_copy_attr(struct radius_msg *dst, struct radius_msg *src,
    885 			 u8 type)
    886 {
    887 	struct radius_attr_hdr *attr;
    888 	size_t i;
    889 	int count = 0;
    890 
    891 	for (i = 0; i < src->attr_used; i++) {
    892 		attr = radius_get_attr_hdr(src, i);
    893 		if (attr->type == type && attr->length >= sizeof(*attr)) {
    894 			if (!radius_msg_add_attr(dst, type, (u8 *) (attr + 1),
    895 						 attr->length - sizeof(*attr)))
    896 				return -1;
    897 			count++;
    898 		}
    899 	}
    900 
    901 	return count;
    902 }
    903 
    904 
    905 /* Create Request Authenticator. The value should be unique over the lifetime
    906  * of the shared secret between authenticator and authentication server.
    907  */
    908 int radius_msg_make_authenticator(struct radius_msg *msg)
    909 {
    910 	return os_get_random((u8 *) &msg->hdr->authenticator,
    911 			     sizeof(msg->hdr->authenticator));
    912 }
    913 
    914 
    915 /* Get Vendor-specific RADIUS Attribute from a parsed RADIUS message.
    916  * Returns the Attribute payload and sets alen to indicate the length of the
    917  * payload if a vendor attribute with subtype is found, otherwise returns NULL.
    918  * The returned payload is allocated with os_malloc() and caller must free it
    919  * by calling os_free().
    920  */
    921 static u8 *radius_msg_get_vendor_attr(struct radius_msg *msg, u32 vendor,
    922 				      u8 subtype, size_t *alen)
    923 {
    924 	u8 *data, *pos;
    925 	size_t i, len;
    926 
    927 	if (msg == NULL)
    928 		return NULL;
    929 
    930 	for (i = 0; i < msg->attr_used; i++) {
    931 		struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
    932 		size_t left;
    933 		u32 vendor_id;
    934 		struct radius_attr_vendor *vhdr;
    935 
    936 		if (attr->type != RADIUS_ATTR_VENDOR_SPECIFIC ||
    937 		    attr->length < sizeof(*attr))
    938 			continue;
    939 
    940 		left = attr->length - sizeof(*attr);
    941 		if (left < 4)
    942 			continue;
    943 
    944 		pos = (u8 *) (attr + 1);
    945 
    946 		os_memcpy(&vendor_id, pos, 4);
    947 		pos += 4;
    948 		left -= 4;
    949 
    950 		if (ntohl(vendor_id) != vendor)
    951 			continue;
    952 
    953 		while (left >= sizeof(*vhdr)) {
    954 			vhdr = (struct radius_attr_vendor *) pos;
    955 			if (vhdr->vendor_length > left ||
    956 			    vhdr->vendor_length < sizeof(*vhdr)) {
    957 				break;
    958 			}
    959 			if (vhdr->vendor_type != subtype) {
    960 				pos += vhdr->vendor_length;
    961 				left -= vhdr->vendor_length;
    962 				continue;
    963 			}
    964 
    965 			len = vhdr->vendor_length - sizeof(*vhdr);
    966 			data = os_memdup(pos + sizeof(*vhdr), len);
    967 			if (data == NULL)
    968 				return NULL;
    969 			if (alen)
    970 				*alen = len;
    971 			return data;
    972 		}
    973 	}
    974 
    975 	return NULL;
    976 }
    977 
    978 
    979 static u8 * decrypt_ms_key(const u8 *key, size_t len,
    980 			   const u8 *req_authenticator,
    981 			   const u8 *secret, size_t secret_len, size_t *reslen)
    982 {
    983 	u8 *plain, *ppos, *res;
    984 	const u8 *pos;
    985 	size_t left, plen;
    986 	u8 hash[MD5_MAC_LEN];
    987 	int i, first = 1;
    988 	const u8 *addr[3];
    989 	size_t elen[3];
    990 
    991 	/* key: 16-bit salt followed by encrypted key info */
    992 
    993 	if (len < 2 + 16) {
    994 		wpa_printf(MSG_DEBUG, "RADIUS: %s: Len is too small: %d",
    995 			   __func__, (int) len);
    996 		return NULL;
    997 	}
    998 
    999 	pos = key + 2;
   1000 	left = len - 2;
   1001 	if (left % 16) {
   1002 		wpa_printf(MSG_INFO, "RADIUS: Invalid ms key len %lu",
   1003 			   (unsigned long) left);
   1004 		return NULL;
   1005 	}
   1006 
   1007 	plen = left;
   1008 	ppos = plain = os_malloc(plen);
   1009 	if (plain == NULL)
   1010 		return NULL;
   1011 	plain[0] = 0;
   1012 
   1013 	while (left > 0) {
   1014 		/* b(1) = MD5(Secret + Request-Authenticator + Salt)
   1015 		 * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
   1016 
   1017 		addr[0] = secret;
   1018 		elen[0] = secret_len;
   1019 		if (first) {
   1020 			addr[1] = req_authenticator;
   1021 			elen[1] = MD5_MAC_LEN;
   1022 			addr[2] = key;
   1023 			elen[2] = 2; /* Salt */
   1024 		} else {
   1025 			addr[1] = pos - MD5_MAC_LEN;
   1026 			elen[1] = MD5_MAC_LEN;
   1027 		}
   1028 		if (md5_vector(first ? 3 : 2, addr, elen, hash) < 0) {
   1029 			os_free(plain);
   1030 			return NULL;
   1031 		}
   1032 		first = 0;
   1033 
   1034 		for (i = 0; i < MD5_MAC_LEN; i++)
   1035 			*ppos++ = *pos++ ^ hash[i];
   1036 		left -= MD5_MAC_LEN;
   1037 	}
   1038 
   1039 	if (plain[0] == 0 || plain[0] > plen - 1) {
   1040 		wpa_printf(MSG_INFO, "RADIUS: Failed to decrypt MPPE key");
   1041 		os_free(plain);
   1042 		return NULL;
   1043 	}
   1044 
   1045 	res = os_memdup(plain + 1, plain[0]);
   1046 	if (res == NULL) {
   1047 		os_free(plain);
   1048 		return NULL;
   1049 	}
   1050 	if (reslen)
   1051 		*reslen = plain[0];
   1052 	os_free(plain);
   1053 	return res;
   1054 }
   1055 
   1056 
   1057 static void encrypt_ms_key(const u8 *key, size_t key_len, u16 salt,
   1058 			   const u8 *req_authenticator,
   1059 			   const u8 *secret, size_t secret_len,
   1060 			   u8 *ebuf, size_t *elen)
   1061 {
   1062 	int i, len, first = 1;
   1063 	u8 hash[MD5_MAC_LEN], saltbuf[2], *pos;
   1064 	const u8 *addr[3];
   1065 	size_t _len[3];
   1066 
   1067 	WPA_PUT_BE16(saltbuf, salt);
   1068 
   1069 	len = 1 + key_len;
   1070 	if (len & 0x0f) {
   1071 		len = (len & 0xf0) + 16;
   1072 	}
   1073 	os_memset(ebuf, 0, len);
   1074 	ebuf[0] = key_len;
   1075 	os_memcpy(ebuf + 1, key, key_len);
   1076 
   1077 	*elen = len;
   1078 
   1079 	pos = ebuf;
   1080 	while (len > 0) {
   1081 		/* b(1) = MD5(Secret + Request-Authenticator + Salt)
   1082 		 * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
   1083 		addr[0] = secret;
   1084 		_len[0] = secret_len;
   1085 		if (first) {
   1086 			addr[1] = req_authenticator;
   1087 			_len[1] = MD5_MAC_LEN;
   1088 			addr[2] = saltbuf;
   1089 			_len[2] = sizeof(saltbuf);
   1090 		} else {
   1091 			addr[1] = pos - MD5_MAC_LEN;
   1092 			_len[1] = MD5_MAC_LEN;
   1093 		}
   1094 		md5_vector(first ? 3 : 2, addr, _len, hash);
   1095 		first = 0;
   1096 
   1097 		for (i = 0; i < MD5_MAC_LEN; i++)
   1098 			*pos++ ^= hash[i];
   1099 
   1100 		len -= MD5_MAC_LEN;
   1101 	}
   1102 }
   1103 
   1104 
   1105 struct radius_ms_mppe_keys *
   1106 radius_msg_get_ms_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
   1107 		       const u8 *secret, size_t secret_len)
   1108 {
   1109 	u8 *key;
   1110 	size_t keylen;
   1111 	struct radius_ms_mppe_keys *keys;
   1112 
   1113 	if (msg == NULL || sent_msg == NULL)
   1114 		return NULL;
   1115 
   1116 	keys = os_zalloc(sizeof(*keys));
   1117 	if (keys == NULL)
   1118 		return NULL;
   1119 
   1120 	key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
   1121 					 RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY,
   1122 					 &keylen);
   1123 	if (key) {
   1124 		keys->send = decrypt_ms_key(key, keylen,
   1125 					    sent_msg->hdr->authenticator,
   1126 					    secret, secret_len,
   1127 					    &keys->send_len);
   1128 		if (!keys->send) {
   1129 			wpa_printf(MSG_DEBUG,
   1130 				   "RADIUS: Failed to decrypt send key");
   1131 		}
   1132 		os_free(key);
   1133 	}
   1134 
   1135 	key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
   1136 					 RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY,
   1137 					 &keylen);
   1138 	if (key) {
   1139 		keys->recv = decrypt_ms_key(key, keylen,
   1140 					    sent_msg->hdr->authenticator,
   1141 					    secret, secret_len,
   1142 					    &keys->recv_len);
   1143 		if (!keys->recv) {
   1144 			wpa_printf(MSG_DEBUG,
   1145 				   "RADIUS: Failed to decrypt recv key");
   1146 		}
   1147 		os_free(key);
   1148 	}
   1149 
   1150 	return keys;
   1151 }
   1152 
   1153 
   1154 struct radius_ms_mppe_keys *
   1155 radius_msg_get_cisco_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
   1156 			  const u8 *secret, size_t secret_len)
   1157 {
   1158 	u8 *key;
   1159 	size_t keylen;
   1160 	struct radius_ms_mppe_keys *keys;
   1161 
   1162 	if (msg == NULL || sent_msg == NULL)
   1163 		return NULL;
   1164 
   1165 	keys = os_zalloc(sizeof(*keys));
   1166 	if (keys == NULL)
   1167 		return NULL;
   1168 
   1169 	key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_CISCO,
   1170 					 RADIUS_CISCO_AV_PAIR, &keylen);
   1171 	if (key && keylen == 51 &&
   1172 	    os_memcmp(key, "leap:session-key=", 17) == 0) {
   1173 		keys->recv = decrypt_ms_key(key + 17, keylen - 17,
   1174 					    sent_msg->hdr->authenticator,
   1175 					    secret, secret_len,
   1176 					    &keys->recv_len);
   1177 	}
   1178 	os_free(key);
   1179 
   1180 	return keys;
   1181 }
   1182 
   1183 
   1184 int radius_msg_add_mppe_keys(struct radius_msg *msg,
   1185 			     const u8 *req_authenticator,
   1186 			     const u8 *secret, size_t secret_len,
   1187 			     const u8 *send_key, size_t send_key_len,
   1188 			     const u8 *recv_key, size_t recv_key_len)
   1189 {
   1190 	struct radius_attr_hdr *attr;
   1191 	u32 vendor_id = htonl(RADIUS_VENDOR_ID_MICROSOFT);
   1192 	u8 *buf;
   1193 	struct radius_attr_vendor *vhdr;
   1194 	u8 *pos;
   1195 	size_t elen;
   1196 	int hlen;
   1197 	u16 salt;
   1198 
   1199 	hlen = sizeof(vendor_id) + sizeof(*vhdr) + 2;
   1200 
   1201 	/* MS-MPPE-Send-Key */
   1202 	buf = os_malloc(hlen + send_key_len + 16);
   1203 	if (buf == NULL) {
   1204 		return 0;
   1205 	}
   1206 	pos = buf;
   1207 	os_memcpy(pos, &vendor_id, sizeof(vendor_id));
   1208 	pos += sizeof(vendor_id);
   1209 	vhdr = (struct radius_attr_vendor *) pos;
   1210 	vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY;
   1211 	pos = (u8 *) (vhdr + 1);
   1212 	if (os_get_random((u8 *) &salt, sizeof(salt)) < 0) {
   1213 		os_free(buf);
   1214 		return 0;
   1215 	}
   1216 	salt |= 0x8000;
   1217 	WPA_PUT_BE16(pos, salt);
   1218 	pos += 2;
   1219 	encrypt_ms_key(send_key, send_key_len, salt, req_authenticator, secret,
   1220 		       secret_len, pos, &elen);
   1221 	vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
   1222 
   1223 	attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
   1224 				   buf, hlen + elen);
   1225 	os_free(buf);
   1226 	if (attr == NULL) {
   1227 		return 0;
   1228 	}
   1229 
   1230 	/* MS-MPPE-Recv-Key */
   1231 	buf = os_malloc(hlen + recv_key_len + 16);
   1232 	if (buf == NULL) {
   1233 		return 0;
   1234 	}
   1235 	pos = buf;
   1236 	os_memcpy(pos, &vendor_id, sizeof(vendor_id));
   1237 	pos += sizeof(vendor_id);
   1238 	vhdr = (struct radius_attr_vendor *) pos;
   1239 	vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY;
   1240 	pos = (u8 *) (vhdr + 1);
   1241 	salt ^= 1;
   1242 	WPA_PUT_BE16(pos, salt);
   1243 	pos += 2;
   1244 	encrypt_ms_key(recv_key, recv_key_len, salt, req_authenticator, secret,
   1245 		       secret_len, pos, &elen);
   1246 	vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
   1247 
   1248 	attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
   1249 				   buf, hlen + elen);
   1250 	os_free(buf);
   1251 	if (attr == NULL) {
   1252 		return 0;
   1253 	}
   1254 
   1255 	return 1;
   1256 }
   1257 
   1258 
   1259 int radius_msg_add_wfa(struct radius_msg *msg, u8 subtype, const u8 *data,
   1260 		       size_t len)
   1261 {
   1262 	struct radius_attr_hdr *attr;
   1263 	u8 *buf, *pos;
   1264 	size_t alen;
   1265 
   1266 	alen = 4 + 2 + len;
   1267 	buf = os_malloc(alen);
   1268 	if (buf == NULL)
   1269 		return 0;
   1270 	pos = buf;
   1271 	WPA_PUT_BE32(pos, RADIUS_VENDOR_ID_WFA);
   1272 	pos += 4;
   1273 	*pos++ = subtype;
   1274 	*pos++ = 2 + len;
   1275 	os_memcpy(pos, data, len);
   1276 	attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
   1277 				   buf, alen);
   1278 	os_free(buf);
   1279 	if (attr == NULL)
   1280 		return 0;
   1281 
   1282 	return 1;
   1283 }
   1284 
   1285 
   1286 int radius_user_password_hide(struct radius_msg *msg,
   1287 			      const u8 *data, size_t data_len,
   1288 			      const u8 *secret, size_t secret_len,
   1289 			      u8 *buf, size_t buf_len)
   1290 {
   1291 	size_t padlen, i, pos;
   1292 	const u8 *addr[2];
   1293 	size_t len[2];
   1294 	u8 hash[16];
   1295 
   1296 	if (data_len + 16 > buf_len)
   1297 		return -1;
   1298 
   1299 	os_memcpy(buf, data, data_len);
   1300 
   1301 	padlen = data_len % 16;
   1302 	if (padlen && data_len < buf_len) {
   1303 		padlen = 16 - padlen;
   1304 		os_memset(buf + data_len, 0, padlen);
   1305 		buf_len = data_len + padlen;
   1306 	} else {
   1307 		buf_len = data_len;
   1308 	}
   1309 
   1310 	addr[0] = secret;
   1311 	len[0] = secret_len;
   1312 	addr[1] = msg->hdr->authenticator;
   1313 	len[1] = 16;
   1314 	md5_vector(2, addr, len, hash);
   1315 
   1316 	for (i = 0; i < 16; i++)
   1317 		buf[i] ^= hash[i];
   1318 	pos = 16;
   1319 
   1320 	while (pos < buf_len) {
   1321 		addr[0] = secret;
   1322 		len[0] = secret_len;
   1323 		addr[1] = &buf[pos - 16];
   1324 		len[1] = 16;
   1325 		md5_vector(2, addr, len, hash);
   1326 
   1327 		for (i = 0; i < 16; i++)
   1328 			buf[pos + i] ^= hash[i];
   1329 
   1330 		pos += 16;
   1331 	}
   1332 
   1333 	return buf_len;
   1334 }
   1335 
   1336 
   1337 /* Add User-Password attribute to a RADIUS message and encrypt it as specified
   1338  * in RFC 2865, Chap. 5.2 */
   1339 struct radius_attr_hdr *
   1340 radius_msg_add_attr_user_password(struct radius_msg *msg,
   1341 				  const u8 *data, size_t data_len,
   1342 				  const u8 *secret, size_t secret_len)
   1343 {
   1344 	u8 buf[128];
   1345 	int res;
   1346 
   1347 	res = radius_user_password_hide(msg, data, data_len,
   1348 					secret, secret_len, buf, sizeof(buf));
   1349 	if (res < 0)
   1350 		return NULL;
   1351 
   1352 	return radius_msg_add_attr(msg, RADIUS_ATTR_USER_PASSWORD,
   1353 				   buf, res);
   1354 }
   1355 
   1356 
   1357 int radius_msg_get_attr(struct radius_msg *msg, u8 type, u8 *buf, size_t len)
   1358 {
   1359 	struct radius_attr_hdr *attr = NULL, *tmp;
   1360 	size_t i, dlen;
   1361 
   1362 	for (i = 0; i < msg->attr_used; i++) {
   1363 		tmp = radius_get_attr_hdr(msg, i);
   1364 		if (tmp->type == type) {
   1365 			attr = tmp;
   1366 			break;
   1367 		}
   1368 	}
   1369 
   1370 	if (!attr || attr->length < sizeof(*attr))
   1371 		return -1;
   1372 
   1373 	dlen = attr->length - sizeof(*attr);
   1374 	if (buf)
   1375 		os_memcpy(buf, (attr + 1), dlen > len ? len : dlen);
   1376 	return dlen;
   1377 }
   1378 
   1379 
   1380 int radius_msg_get_attr_ptr(struct radius_msg *msg, u8 type, u8 **buf,
   1381 			    size_t *len, const u8 *start)
   1382 {
   1383 	size_t i;
   1384 	struct radius_attr_hdr *attr = NULL, *tmp;
   1385 
   1386 	for (i = 0; i < msg->attr_used; i++) {
   1387 		tmp = radius_get_attr_hdr(msg, i);
   1388 		if (tmp->type == type &&
   1389 		    (start == NULL || (u8 *) tmp > start)) {
   1390 			attr = tmp;
   1391 			break;
   1392 		}
   1393 	}
   1394 
   1395 	if (!attr || attr->length < sizeof(*attr))
   1396 		return -1;
   1397 
   1398 	*buf = (u8 *) (attr + 1);
   1399 	*len = attr->length - sizeof(*attr);
   1400 	return 0;
   1401 }
   1402 
   1403 
   1404 int radius_msg_count_attr(struct radius_msg *msg, u8 type, int min_len)
   1405 {
   1406 	size_t i;
   1407 	int count;
   1408 
   1409 	for (count = 0, i = 0; i < msg->attr_used; i++) {
   1410 		struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
   1411 		if (attr->type == type &&
   1412 		    attr->length >= sizeof(struct radius_attr_hdr) + min_len)
   1413 			count++;
   1414 	}
   1415 
   1416 	return count;
   1417 }
   1418 
   1419 
   1420 struct radius_tunnel_attrs {
   1421 	int tag_used;
   1422 	int type; /* Tunnel-Type */
   1423 	int medium_type; /* Tunnel-Medium-Type */
   1424 	int vlanid;
   1425 };
   1426 
   1427 
   1428 static int cmp_int(const void *a, const void *b)
   1429 {
   1430 	int x, y;
   1431 
   1432 	x = *((int *) a);
   1433 	y = *((int *) b);
   1434 	return (x - y);
   1435 }
   1436 
   1437 
   1438 /**
   1439  * radius_msg_get_vlanid - Parse RADIUS attributes for VLAN tunnel information
   1440  * The k tagged vlans found are sorted by vlan_id and stored in the first k
   1441  * items of tagged.
   1442  *
   1443  * @msg: RADIUS message
   1444  * @untagged: Pointer to store untagged vid
   1445  * @numtagged: Size of tagged
   1446  * @tagged: Pointer to store tagged list
   1447  *
   1448  * Returns: 0 if neither tagged nor untagged configuration is found, 1 otherwise
   1449  */
   1450 int radius_msg_get_vlanid(struct radius_msg *msg, int *untagged, int numtagged,
   1451 			  int *tagged)
   1452 {
   1453 	struct radius_tunnel_attrs tunnel[RADIUS_TUNNEL_TAGS], *tun;
   1454 	size_t i;
   1455 	struct radius_attr_hdr *attr = NULL;
   1456 	const u8 *data;
   1457 	char buf[10];
   1458 	size_t dlen;
   1459 	int j, taggedidx = 0, vlan_id;
   1460 
   1461 	os_memset(&tunnel, 0, sizeof(tunnel));
   1462 	for (j = 0; j < numtagged; j++)
   1463 		tagged[j] = 0;
   1464 	*untagged = 0;
   1465 
   1466 	for (i = 0; i < msg->attr_used; i++) {
   1467 		attr = radius_get_attr_hdr(msg, i);
   1468 		if (attr->length < sizeof(*attr))
   1469 			return -1;
   1470 		data = (const u8 *) (attr + 1);
   1471 		dlen = attr->length - sizeof(*attr);
   1472 		if (attr->length < 3)
   1473 			continue;
   1474 		if (data[0] >= RADIUS_TUNNEL_TAGS)
   1475 			tun = &tunnel[0];
   1476 		else
   1477 			tun = &tunnel[data[0]];
   1478 
   1479 		switch (attr->type) {
   1480 		case RADIUS_ATTR_TUNNEL_TYPE:
   1481 			if (attr->length != 6)
   1482 				break;
   1483 			tun->tag_used++;
   1484 			tun->type = WPA_GET_BE24(data + 1);
   1485 			break;
   1486 		case RADIUS_ATTR_TUNNEL_MEDIUM_TYPE:
   1487 			if (attr->length != 6)
   1488 				break;
   1489 			tun->tag_used++;
   1490 			tun->medium_type = WPA_GET_BE24(data + 1);
   1491 			break;
   1492 		case RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID:
   1493 			if (data[0] < RADIUS_TUNNEL_TAGS) {
   1494 				data++;
   1495 				dlen--;
   1496 			}
   1497 			if (dlen >= sizeof(buf))
   1498 				break;
   1499 			os_memcpy(buf, data, dlen);
   1500 			buf[dlen] = '\0';
   1501 			vlan_id = atoi(buf);
   1502 			if (vlan_id <= 0)
   1503 				break;
   1504 			tun->tag_used++;
   1505 			tun->vlanid = vlan_id;
   1506 			break;
   1507 		case RADIUS_ATTR_EGRESS_VLANID: /* RFC 4675 */
   1508 			if (attr->length != 6)
   1509 				break;
   1510 			vlan_id = WPA_GET_BE24(data + 1);
   1511 			if (vlan_id <= 0)
   1512 				break;
   1513 			if (data[0] == 0x32)
   1514 				*untagged = vlan_id;
   1515 			else if (data[0] == 0x31 && tagged &&
   1516 				 taggedidx < numtagged)
   1517 				tagged[taggedidx++] = vlan_id;
   1518 			break;
   1519 		}
   1520 	}
   1521 
   1522 	/* Use tunnel with the lowest tag for untagged VLAN id */
   1523 	for (i = 0; i < RADIUS_TUNNEL_TAGS; i++) {
   1524 		tun = &tunnel[i];
   1525 		if (tun->tag_used &&
   1526 		    tun->type == RADIUS_TUNNEL_TYPE_VLAN &&
   1527 		    tun->medium_type == RADIUS_TUNNEL_MEDIUM_TYPE_802 &&
   1528 		    tun->vlanid > 0) {
   1529 			*untagged = tun->vlanid;
   1530 			break;
   1531 		}
   1532 	}
   1533 
   1534 	if (taggedidx)
   1535 		qsort(tagged, taggedidx, sizeof(int), cmp_int);
   1536 
   1537 	if (*untagged > 0 || taggedidx)
   1538 		return 1;
   1539 	return 0;
   1540 }
   1541 
   1542 
   1543 /**
   1544  * radius_msg_get_tunnel_password - Parse RADIUS attribute Tunnel-Password
   1545  * @msg: Received RADIUS message
   1546  * @keylen: Length of returned password
   1547  * @secret: RADIUS shared secret
   1548  * @secret_len: Length of secret
   1549  * @sent_msg: Sent RADIUS message
   1550  * @n: Number of password attribute to return (starting with 0)
   1551  * Returns: Pointer to n-th password (free with os_free) or %NULL
   1552  */
   1553 char * radius_msg_get_tunnel_password(struct radius_msg *msg, int *keylen,
   1554 				      const u8 *secret, size_t secret_len,
   1555 				      struct radius_msg *sent_msg, size_t n)
   1556 {
   1557 	u8 *buf = NULL;
   1558 	size_t buflen;
   1559 	const u8 *salt;
   1560 	u8 *str;
   1561 	const u8 *addr[3];
   1562 	size_t len[3];
   1563 	u8 hash[16];
   1564 	u8 *pos;
   1565 	size_t i, j = 0;
   1566 	struct radius_attr_hdr *attr;
   1567 	const u8 *data;
   1568 	size_t dlen;
   1569 	const u8 *fdata = NULL; /* points to found item */
   1570 	size_t fdlen = -1;
   1571 	char *ret = NULL;
   1572 
   1573 	/* find n-th valid Tunnel-Password attribute */
   1574 	for (i = 0; i < msg->attr_used; i++) {
   1575 		attr = radius_get_attr_hdr(msg, i);
   1576 		if (attr == NULL ||
   1577 		    attr->type != RADIUS_ATTR_TUNNEL_PASSWORD) {
   1578 			continue;
   1579 		}
   1580 		if (attr->length <= 5)
   1581 			continue;
   1582 		data = (const u8 *) (attr + 1);
   1583 		dlen = attr->length - sizeof(*attr);
   1584 		if (dlen <= 3 || dlen % 16 != 3)
   1585 			continue;
   1586 		j++;
   1587 		if (j <= n)
   1588 			continue;
   1589 
   1590 		fdata = data;
   1591 		fdlen = dlen;
   1592 		break;
   1593 	}
   1594 	if (fdata == NULL)
   1595 		goto out;
   1596 
   1597 	/* alloc writable memory for decryption */
   1598 	buf = os_memdup(fdata, fdlen);
   1599 	if (buf == NULL)
   1600 		goto out;
   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 
   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 
   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_memdup(src->attr[i].data,
   1688 					      src->attr[i].len);
   1689 		if (dst->attr[i].data == NULL)
   1690 			break;
   1691 		dst->count++;
   1692 		dst->attr[i].len = src->attr[i].len;
   1693 	}
   1694 
   1695 	return 0;
   1696 }
   1697 
   1698 
   1699 u8 radius_msg_find_unlisted_attr(struct radius_msg *msg, u8 *attrs)
   1700 {
   1701 	size_t i, j;
   1702 	struct radius_attr_hdr *attr;
   1703 
   1704 	for (i = 0; i < msg->attr_used; i++) {
   1705 		attr = radius_get_attr_hdr(msg, i);
   1706 
   1707 		for (j = 0; attrs[j]; j++) {
   1708 			if (attr->type == attrs[j])
   1709 				break;
   1710 		}
   1711 
   1712 		if (attrs[j] == 0)
   1713 			return attr->type; /* unlisted attr */
   1714 	}
   1715 
   1716 	return 0;
   1717 }
   1718 
   1719 
   1720 int radius_gen_session_id(u8 *id, size_t len)
   1721 {
   1722 	/*
   1723 	 * Acct-Session-Id and Acct-Multi-Session-Id should be globally and
   1724 	 * temporarily unique. A high quality random number is required
   1725 	 * therefore. This could be be improved by switching to a GUID.
   1726 	 */
   1727 	return os_get_random(id, len);
   1728 }
   1729