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