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