blob: a64228067e0ed605caf18414aacf416fff5af18f [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * RADIUS message processing
Sunil Ravia04bd252022-05-02 22:54:18 -07003 * Copyright (c) 2002-2009, 2011-2022, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/wpabuf.h"
13#include "crypto/md5.h"
14#include "crypto/crypto.h"
15#include "radius.h"
16
17
18/**
19 * struct radius_msg - RADIUS message structure for new and parsed messages
20 */
21struct radius_msg {
22 /**
23 * buf - Allocated buffer for RADIUS message
24 */
25 struct wpabuf *buf;
26
27 /**
28 * hdr - Pointer to the RADIUS header in buf
29 */
30 struct radius_hdr *hdr;
31
32 /**
33 * attr_pos - Array of indexes to attributes
34 *
35 * The values are number of bytes from buf to the beginning of
36 * struct radius_attr_hdr.
37 */
38 size_t *attr_pos;
39
40 /**
41 * attr_size - Total size of the attribute pointer array
42 */
43 size_t attr_size;
44
45 /**
46 * attr_used - Total number of attributes in the array
47 */
48 size_t attr_used;
49};
50
51
52struct radius_hdr * radius_msg_get_hdr(struct radius_msg *msg)
53{
54 return msg->hdr;
55}
56
57
58struct wpabuf * radius_msg_get_buf(struct radius_msg *msg)
59{
60 return msg->buf;
61}
62
63
64static struct radius_attr_hdr *
65radius_get_attr_hdr(struct radius_msg *msg, int idx)
66{
67 return (struct radius_attr_hdr *)
68 (wpabuf_mhead_u8(msg->buf) + msg->attr_pos[idx]);
69}
70
71
72static void radius_msg_set_hdr(struct radius_msg *msg, u8 code, u8 identifier)
73{
74 msg->hdr->code = code;
75 msg->hdr->identifier = identifier;
76}
77
78
79static int radius_msg_initialize(struct radius_msg *msg)
80{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070081 msg->attr_pos = os_calloc(RADIUS_DEFAULT_ATTR_COUNT,
82 sizeof(*msg->attr_pos));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070083 if (msg->attr_pos == NULL)
84 return -1;
85
86 msg->attr_size = RADIUS_DEFAULT_ATTR_COUNT;
87 msg->attr_used = 0;
88
89 return 0;
90}
91
92
93/**
94 * radius_msg_new - Create a new RADIUS message
95 * @code: Code for RADIUS header
96 * @identifier: Identifier for RADIUS header
97 * Returns: Context for RADIUS message or %NULL on failure
98 *
99 * The caller is responsible for freeing the returned data with
100 * radius_msg_free().
101 */
102struct radius_msg * radius_msg_new(u8 code, u8 identifier)
103{
104 struct radius_msg *msg;
105
106 msg = os_zalloc(sizeof(*msg));
107 if (msg == NULL)
108 return NULL;
109
110 msg->buf = wpabuf_alloc(RADIUS_DEFAULT_MSG_SIZE);
111 if (msg->buf == NULL || radius_msg_initialize(msg)) {
112 radius_msg_free(msg);
113 return NULL;
114 }
115 msg->hdr = wpabuf_put(msg->buf, sizeof(struct radius_hdr));
116
117 radius_msg_set_hdr(msg, code, identifier);
118
119 return msg;
120}
121
122
123/**
124 * radius_msg_free - Free a RADIUS message
125 * @msg: RADIUS message from radius_msg_new() or radius_msg_parse()
126 */
127void radius_msg_free(struct radius_msg *msg)
128{
129 if (msg == NULL)
130 return;
131
132 wpabuf_free(msg->buf);
133 os_free(msg->attr_pos);
134 os_free(msg);
135}
136
137
138static const char *radius_code_string(u8 code)
139{
140 switch (code) {
141 case RADIUS_CODE_ACCESS_REQUEST: return "Access-Request";
142 case RADIUS_CODE_ACCESS_ACCEPT: return "Access-Accept";
143 case RADIUS_CODE_ACCESS_REJECT: return "Access-Reject";
144 case RADIUS_CODE_ACCOUNTING_REQUEST: return "Accounting-Request";
145 case RADIUS_CODE_ACCOUNTING_RESPONSE: return "Accounting-Response";
146 case RADIUS_CODE_ACCESS_CHALLENGE: return "Access-Challenge";
147 case RADIUS_CODE_STATUS_SERVER: return "Status-Server";
148 case RADIUS_CODE_STATUS_CLIENT: return "Status-Client";
149 case RADIUS_CODE_RESERVED: return "Reserved";
Dmitry Shmidt04949592012-07-19 12:16:46 -0700150 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 Shmidt8d520ff2011-05-09 14:06:53 -0700156 default: return "?Unknown?";
157 }
158}
159
160
161struct radius_attr_type {
Sunil Ravia04bd252022-05-02 22:54:18 -0700162 u16 type; /* 0..255 for basic types;
163 * (241 << 8) | <ext-type> for extended types */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700164 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 Shmidt1d755d02015-04-28 10:34:29 -0700171static const struct radius_attr_type radius_attrs[] =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700172{
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 Shmidt57c2d392016-02-23 13:40:19 -0800177 { RADIUS_ATTR_SERVICE_TYPE, "Service-Type", RADIUS_ATTR_INT32 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800178 { RADIUS_ATTR_FRAMED_IP_ADDRESS, "Framed-IP-Address", RADIUS_ATTR_IP },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700179 { RADIUS_ATTR_FRAMED_MTU, "Framed-MTU", RADIUS_ATTR_INT32 },
180 { RADIUS_ATTR_REPLY_MESSAGE, "Reply-Message", RADIUS_ATTR_TEXT },
181 { RADIUS_ATTR_STATE, "State", RADIUS_ATTR_UNDIST },
182 { RADIUS_ATTR_CLASS, "Class", RADIUS_ATTR_UNDIST },
183 { RADIUS_ATTR_VENDOR_SPECIFIC, "Vendor-Specific", RADIUS_ATTR_UNDIST },
184 { RADIUS_ATTR_SESSION_TIMEOUT, "Session-Timeout", RADIUS_ATTR_INT32 },
185 { RADIUS_ATTR_IDLE_TIMEOUT, "Idle-Timeout", RADIUS_ATTR_INT32 },
186 { RADIUS_ATTR_TERMINATION_ACTION, "Termination-Action",
187 RADIUS_ATTR_INT32 },
188 { RADIUS_ATTR_CALLED_STATION_ID, "Called-Station-Id",
189 RADIUS_ATTR_TEXT },
190 { RADIUS_ATTR_CALLING_STATION_ID, "Calling-Station-Id",
191 RADIUS_ATTR_TEXT },
192 { RADIUS_ATTR_NAS_IDENTIFIER, "NAS-Identifier", RADIUS_ATTR_TEXT },
193 { RADIUS_ATTR_PROXY_STATE, "Proxy-State", RADIUS_ATTR_UNDIST },
194 { RADIUS_ATTR_ACCT_STATUS_TYPE, "Acct-Status-Type",
195 RADIUS_ATTR_INT32 },
196 { RADIUS_ATTR_ACCT_DELAY_TIME, "Acct-Delay-Time", RADIUS_ATTR_INT32 },
197 { RADIUS_ATTR_ACCT_INPUT_OCTETS, "Acct-Input-Octets",
198 RADIUS_ATTR_INT32 },
199 { RADIUS_ATTR_ACCT_OUTPUT_OCTETS, "Acct-Output-Octets",
200 RADIUS_ATTR_INT32 },
201 { RADIUS_ATTR_ACCT_SESSION_ID, "Acct-Session-Id", RADIUS_ATTR_TEXT },
202 { RADIUS_ATTR_ACCT_AUTHENTIC, "Acct-Authentic", RADIUS_ATTR_INT32 },
203 { RADIUS_ATTR_ACCT_SESSION_TIME, "Acct-Session-Time",
204 RADIUS_ATTR_INT32 },
205 { RADIUS_ATTR_ACCT_INPUT_PACKETS, "Acct-Input-Packets",
206 RADIUS_ATTR_INT32 },
207 { RADIUS_ATTR_ACCT_OUTPUT_PACKETS, "Acct-Output-Packets",
208 RADIUS_ATTR_INT32 },
209 { RADIUS_ATTR_ACCT_TERMINATE_CAUSE, "Acct-Terminate-Cause",
210 RADIUS_ATTR_INT32 },
211 { RADIUS_ATTR_ACCT_MULTI_SESSION_ID, "Acct-Multi-Session-Id",
212 RADIUS_ATTR_TEXT },
213 { RADIUS_ATTR_ACCT_LINK_COUNT, "Acct-Link-Count", RADIUS_ATTR_INT32 },
Dmitry Shmidt29333592017-01-09 12:27:11 -0800214 { RADIUS_ATTR_ACCT_INPUT_GIGAWORDS, "Acct-Input-Gigawords",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 RADIUS_ATTR_INT32 },
216 { RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS, "Acct-Output-Gigawords",
217 RADIUS_ATTR_INT32 },
218 { RADIUS_ATTR_EVENT_TIMESTAMP, "Event-Timestamp",
219 RADIUS_ATTR_INT32 },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800220 { RADIUS_ATTR_EGRESS_VLANID, "EGRESS-VLANID", RADIUS_ATTR_HEXDUMP },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700221 { RADIUS_ATTR_NAS_PORT_TYPE, "NAS-Port-Type", RADIUS_ATTR_INT32 },
222 { RADIUS_ATTR_TUNNEL_TYPE, "Tunnel-Type", RADIUS_ATTR_HEXDUMP },
223 { RADIUS_ATTR_TUNNEL_MEDIUM_TYPE, "Tunnel-Medium-Type",
224 RADIUS_ATTR_HEXDUMP },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800225 { RADIUS_ATTR_TUNNEL_PASSWORD, "Tunnel-Password",
226 RADIUS_ATTR_UNDIST },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227 { RADIUS_ATTR_CONNECT_INFO, "Connect-Info", RADIUS_ATTR_TEXT },
228 { RADIUS_ATTR_EAP_MESSAGE, "EAP-Message", RADIUS_ATTR_UNDIST },
229 { RADIUS_ATTR_MESSAGE_AUTHENTICATOR, "Message-Authenticator",
230 RADIUS_ATTR_UNDIST },
231 { RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID, "Tunnel-Private-Group-Id",
232 RADIUS_ATTR_HEXDUMP },
233 { RADIUS_ATTR_ACCT_INTERIM_INTERVAL, "Acct-Interim-Interval",
234 RADIUS_ATTR_INT32 },
Dmitry Shmidt04949592012-07-19 12:16:46 -0700235 { RADIUS_ATTR_CHARGEABLE_USER_IDENTITY, "Chargeable-User-Identity",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700236 RADIUS_ATTR_TEXT },
237 { RADIUS_ATTR_NAS_IPV6_ADDRESS, "NAS-IPv6-Address", RADIUS_ATTR_IPV6 },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700238 { RADIUS_ATTR_ERROR_CAUSE, "Error-Cause", RADIUS_ATTR_INT32 },
239 { RADIUS_ATTR_EAP_KEY_NAME, "EAP-Key-Name", RADIUS_ATTR_HEXDUMP },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800240 { RADIUS_ATTR_OPERATOR_NAME, "Operator-Name", RADIUS_ATTR_TEXT },
241 { RADIUS_ATTR_LOCATION_INFO, "Location-Information",
242 RADIUS_ATTR_HEXDUMP },
243 { RADIUS_ATTR_LOCATION_DATA, "Location-Data", RADIUS_ATTR_HEXDUMP },
244 { RADIUS_ATTR_BASIC_LOCATION_POLICY_RULES,
245 "Basic-Location-Policy-Rules", RADIUS_ATTR_HEXDUMP },
246 { RADIUS_ATTR_EXTENDED_LOCATION_POLICY_RULES,
247 "Extended-Location-Policy-Rules", RADIUS_ATTR_HEXDUMP },
248 { RADIUS_ATTR_LOCATION_CAPABLE, "Location-Capable", RADIUS_ATTR_INT32 },
249 { RADIUS_ATTR_REQUESTED_LOCATION_INFO, "Requested-Location-Info",
250 RADIUS_ATTR_INT32 },
Dmitry Shmidt03658832014-08-13 11:03:49 -0700251 { RADIUS_ATTR_MOBILITY_DOMAIN_ID, "Mobility-Domain-Id",
252 RADIUS_ATTR_INT32 },
253 { RADIUS_ATTR_WLAN_HESSID, "WLAN-HESSID", RADIUS_ATTR_TEXT },
Roshan Pius3a1667e2018-07-03 15:17:14 -0700254 { RADIUS_ATTR_WLAN_REASON_CODE, "WLAN-Reason-Code",
255 RADIUS_ATTR_INT32 },
Dmitry Shmidt03658832014-08-13 11:03:49 -0700256 { RADIUS_ATTR_WLAN_PAIRWISE_CIPHER, "WLAN-Pairwise-Cipher",
257 RADIUS_ATTR_HEXDUMP },
258 { RADIUS_ATTR_WLAN_GROUP_CIPHER, "WLAN-Group-Cipher",
259 RADIUS_ATTR_HEXDUMP },
260 { RADIUS_ATTR_WLAN_AKM_SUITE, "WLAN-AKM-Suite",
261 RADIUS_ATTR_HEXDUMP },
262 { RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, "WLAN-Group-Mgmt-Pairwise-Cipher",
263 RADIUS_ATTR_HEXDUMP },
Sunil Ravia04bd252022-05-02 22:54:18 -0700264 { RADIUS_ATTR_EXT_TYPE_1, "Extended-Type-1", RADIUS_ATTR_UNDIST },
265 { RADIUS_ATTR_EXT_TYPE_2, "Extended-Type-2", RADIUS_ATTR_UNDIST },
266 { RADIUS_ATTR_EXT_TYPE_3, "Extended-Type-3", RADIUS_ATTR_UNDIST },
267 { RADIUS_ATTR_EXT_TYPE_4, "Extended-Type-4", RADIUS_ATTR_UNDIST },
268 { RADIUS_ATTR_LONG_EXT_TYPE_1, "Long-Extended-Type-1",
269 RADIUS_ATTR_UNDIST },
270 { RADIUS_ATTR_LONG_EXT_TYPE_2, "Long-Extended-Type-2",
271 RADIUS_ATTR_UNDIST },
272 { RADIUS_ATTR_EXT_VENDOR_SPECIFIC_1, "Extended-Vendor-Specific-1",
273 RADIUS_ATTR_UNDIST },
274 { RADIUS_ATTR_EXT_VENDOR_SPECIFIC_2, "Extended-Vendor-Specific-2",
275 RADIUS_ATTR_UNDIST },
276 { RADIUS_ATTR_EXT_VENDOR_SPECIFIC_3, "Extended-Vendor-Specific-3",
277 RADIUS_ATTR_UNDIST },
278 { RADIUS_ATTR_EXT_VENDOR_SPECIFIC_4, "Extended-Vendor-Specific-4",
279 RADIUS_ATTR_UNDIST },
280 { RADIUS_ATTR_EXT_VENDOR_SPECIFIC_5, "Extended-Vendor-Specific-5",
281 RADIUS_ATTR_UNDIST },
282 { RADIUS_ATTR_EXT_VENDOR_SPECIFIC_6, "Extended-Vendor-Specific-6",
283 RADIUS_ATTR_UNDIST },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284};
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700285#define RADIUS_ATTRS ARRAY_SIZE(radius_attrs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286
287
Sunil Ravia04bd252022-05-02 22:54:18 -0700288static const struct radius_attr_type * radius_get_attr_type(u16 type)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289{
290 size_t i;
291
292 for (i = 0; i < RADIUS_ATTRS; i++) {
293 if (type == radius_attrs[i].type)
294 return &radius_attrs[i];
295 }
296
297 return NULL;
298}
299
300
Sunil Ravia04bd252022-05-02 22:54:18 -0700301static bool radius_is_long_ext_type(u8 type)
302{
303 return type == RADIUS_ATTR_LONG_EXT_TYPE_1 ||
304 type == RADIUS_ATTR_LONG_EXT_TYPE_2;
305}
306
307
308static bool radius_is_ext_type(u8 type)
309{
310 return type >= RADIUS_ATTR_EXT_TYPE_1 &&
311 type <= RADIUS_ATTR_LONG_EXT_TYPE_2;
312}
313
314
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700315static void radius_msg_dump_attr(struct radius_attr_hdr *hdr)
316{
Sunil Ravia04bd252022-05-02 22:54:18 -0700317 struct radius_attr_hdr_ext *ext = NULL;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700318 const struct radius_attr_type *attr;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800319 int len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320 unsigned char *pos;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800321 char buf[1000];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322
Sunil Ravia04bd252022-05-02 22:54:18 -0700323 if (hdr->length < sizeof(struct radius_attr_hdr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324 return;
325
Sunil Ravia04bd252022-05-02 22:54:18 -0700326 if (radius_is_ext_type(hdr->type)) {
327 if (hdr->length < 4) {
328 wpa_printf(MSG_INFO,
329 " Invalid attribute %d (too short for extended type)",
330 hdr->type);
331 return;
332 }
333
334 ext = (struct radius_attr_hdr_ext *) hdr;
335 }
336
337 if (ext) {
338 attr = radius_get_attr_type((ext->type << 8) | ext->ext_type);
339 wpa_printf(MSG_INFO, " Attribute %d.%d (%s) length=%d",
340 ext->type, ext->ext_type,
341 attr ? attr->name : "?Unknown?", ext->length);
342 pos = (unsigned char *) (ext + 1);
343 len = ext->length - sizeof(struct radius_attr_hdr_ext);
344 } else {
345 attr = radius_get_attr_type(hdr->type);
346 wpa_printf(MSG_INFO, " Attribute %d (%s) length=%d",
347 hdr->type, attr ? attr->name : "?Unknown?",
348 hdr->length);
349 pos = (unsigned char *) (hdr + 1);
350 len = hdr->length - sizeof(struct radius_attr_hdr);
351 }
352
353 if (!attr)
354 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700355
356 switch (attr->data_type) {
357 case RADIUS_ATTR_TEXT:
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800358 printf_encode(buf, sizeof(buf), pos, len);
359 wpa_printf(MSG_INFO, " Value: '%s'", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700360 break;
361
362 case RADIUS_ATTR_IP:
363 if (len == 4) {
364 struct in_addr addr;
365 os_memcpy(&addr, pos, 4);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800366 wpa_printf(MSG_INFO, " Value: %s",
367 inet_ntoa(addr));
368 } else {
369 wpa_printf(MSG_INFO, " Invalid IP address length %d",
370 len);
371 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372 break;
373
374#ifdef CONFIG_IPV6
375 case RADIUS_ATTR_IPV6:
376 if (len == 16) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700377 const char *atxt;
378 struct in6_addr *addr = (struct in6_addr *) pos;
379 atxt = inet_ntop(AF_INET6, addr, buf, sizeof(buf));
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800380 wpa_printf(MSG_INFO, " Value: %s",
381 atxt ? atxt : "?");
382 } else {
383 wpa_printf(MSG_INFO, " Invalid IPv6 address length %d",
384 len);
385 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700386 break;
387#endif /* CONFIG_IPV6 */
388
389 case RADIUS_ATTR_HEXDUMP:
390 case RADIUS_ATTR_UNDIST:
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800391 wpa_snprintf_hex(buf, sizeof(buf), pos, len);
392 wpa_printf(MSG_INFO, " Value: %s", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700393 break;
394
395 case RADIUS_ATTR_INT32:
396 if (len == 4)
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800397 wpa_printf(MSG_INFO, " Value: %u",
398 WPA_GET_BE32(pos));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399 else
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800400 wpa_printf(MSG_INFO, " Invalid INT32 length %d",
401 len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700402 break;
403
404 default:
405 break;
406 }
407}
408
409
410void radius_msg_dump(struct radius_msg *msg)
411{
412 size_t i;
413
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800414 wpa_printf(MSG_INFO, "RADIUS message: code=%d (%s) identifier=%d length=%d",
415 msg->hdr->code, radius_code_string(msg->hdr->code),
416 msg->hdr->identifier, be_to_host16(msg->hdr->length));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700417
418 for (i = 0; i < msg->attr_used; i++) {
419 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
420 radius_msg_dump_attr(attr);
421 }
422}
423
424
425int radius_msg_finish(struct radius_msg *msg, const u8 *secret,
426 size_t secret_len)
427{
428 if (secret) {
429 u8 auth[MD5_MAC_LEN];
430 struct radius_attr_hdr *attr;
431
432 os_memset(auth, 0, MD5_MAC_LEN);
433 attr = radius_msg_add_attr(msg,
434 RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
435 auth, MD5_MAC_LEN);
436 if (attr == NULL) {
437 wpa_printf(MSG_WARNING, "RADIUS: Could not add "
438 "Message-Authenticator");
439 return -1;
440 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700441 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700442 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
443 wpabuf_len(msg->buf), (u8 *) (attr + 1));
444 } else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700445 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700446
447 if (wpabuf_len(msg->buf) > 0xffff) {
448 wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
449 (unsigned long) wpabuf_len(msg->buf));
450 return -1;
451 }
452 return 0;
453}
454
455
456int radius_msg_finish_srv(struct radius_msg *msg, const u8 *secret,
457 size_t secret_len, const u8 *req_authenticator)
458{
459 u8 auth[MD5_MAC_LEN];
460 struct radius_attr_hdr *attr;
461 const u8 *addr[4];
462 size_t len[4];
463
464 os_memset(auth, 0, MD5_MAC_LEN);
465 attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
466 auth, MD5_MAC_LEN);
467 if (attr == NULL) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800468 wpa_printf(MSG_ERROR, "WARNING: Could not add Message-Authenticator");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700469 return -1;
470 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700471 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700472 os_memcpy(msg->hdr->authenticator, req_authenticator,
473 sizeof(msg->hdr->authenticator));
474 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
475 wpabuf_len(msg->buf), (u8 *) (attr + 1));
476
477 /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
478 addr[0] = (u8 *) msg->hdr;
479 len[0] = 1 + 1 + 2;
480 addr[1] = req_authenticator;
481 len[1] = MD5_MAC_LEN;
482 addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
483 len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
484 addr[3] = secret;
485 len[3] = secret_len;
486 md5_vector(4, addr, len, msg->hdr->authenticator);
487
488 if (wpabuf_len(msg->buf) > 0xffff) {
489 wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
490 (unsigned long) wpabuf_len(msg->buf));
491 return -1;
492 }
493 return 0;
494}
495
496
Dmitry Shmidt04949592012-07-19 12:16:46 -0700497int radius_msg_finish_das_resp(struct radius_msg *msg, const u8 *secret,
498 size_t secret_len,
499 const struct radius_hdr *req_hdr)
500{
501 const u8 *addr[2];
502 size_t len[2];
503 u8 auth[MD5_MAC_LEN];
504 struct radius_attr_hdr *attr;
505
506 os_memset(auth, 0, MD5_MAC_LEN);
507 attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
508 auth, MD5_MAC_LEN);
509 if (attr == NULL) {
510 wpa_printf(MSG_WARNING, "Could not add Message-Authenticator");
511 return -1;
512 }
513
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700514 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700515 os_memcpy(msg->hdr->authenticator, req_hdr->authenticator, 16);
516 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
517 wpabuf_len(msg->buf), (u8 *) (attr + 1));
518
519 /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
520 addr[0] = wpabuf_head_u8(msg->buf);
521 len[0] = wpabuf_len(msg->buf);
522 addr[1] = secret;
523 len[1] = secret_len;
524 if (md5_vector(2, addr, len, msg->hdr->authenticator) < 0)
525 return -1;
526
527 if (wpabuf_len(msg->buf) > 0xffff) {
528 wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
529 (unsigned long) wpabuf_len(msg->buf));
530 return -1;
531 }
532 return 0;
533}
534
535
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700536void radius_msg_finish_acct(struct radius_msg *msg, const u8 *secret,
537 size_t secret_len)
538{
539 const u8 *addr[2];
540 size_t len[2];
541
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700542 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700543 os_memset(msg->hdr->authenticator, 0, MD5_MAC_LEN);
544 addr[0] = wpabuf_head(msg->buf);
545 len[0] = wpabuf_len(msg->buf);
546 addr[1] = secret;
547 len[1] = secret_len;
548 md5_vector(2, addr, len, msg->hdr->authenticator);
549
550 if (wpabuf_len(msg->buf) > 0xffff) {
551 wpa_printf(MSG_WARNING, "RADIUS: Too long messages (%lu)",
552 (unsigned long) wpabuf_len(msg->buf));
553 }
554}
555
556
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800557void radius_msg_finish_acct_resp(struct radius_msg *msg, const u8 *secret,
558 size_t secret_len, const u8 *req_authenticator)
559{
560 const u8 *addr[2];
561 size_t len[2];
562
563 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
564 os_memcpy(msg->hdr->authenticator, req_authenticator, MD5_MAC_LEN);
565 addr[0] = wpabuf_head(msg->buf);
566 len[0] = wpabuf_len(msg->buf);
567 addr[1] = secret;
568 len[1] = secret_len;
569 md5_vector(2, addr, len, msg->hdr->authenticator);
570
571 if (wpabuf_len(msg->buf) > 0xffff) {
572 wpa_printf(MSG_WARNING, "RADIUS: Too long messages (%lu)",
573 (unsigned long) wpabuf_len(msg->buf));
574 }
575}
576
577
Dmitry Shmidt04949592012-07-19 12:16:46 -0700578int radius_msg_verify_acct_req(struct radius_msg *msg, const u8 *secret,
579 size_t secret_len)
580{
581 const u8 *addr[4];
582 size_t len[4];
583 u8 zero[MD5_MAC_LEN];
584 u8 hash[MD5_MAC_LEN];
585
586 os_memset(zero, 0, sizeof(zero));
587 addr[0] = (u8 *) msg->hdr;
588 len[0] = sizeof(struct radius_hdr) - MD5_MAC_LEN;
589 addr[1] = zero;
590 len[1] = MD5_MAC_LEN;
591 addr[2] = (u8 *) (msg->hdr + 1);
592 len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
593 addr[3] = secret;
594 len[3] = secret_len;
595 md5_vector(4, addr, len, hash);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700596 return os_memcmp_const(msg->hdr->authenticator, hash, MD5_MAC_LEN) != 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700597}
598
599
600int radius_msg_verify_das_req(struct radius_msg *msg, const u8 *secret,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700601 size_t secret_len,
602 int require_message_authenticator)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700603{
604 const u8 *addr[4];
605 size_t len[4];
606 u8 zero[MD5_MAC_LEN];
607 u8 hash[MD5_MAC_LEN];
608 u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
609 u8 orig_authenticator[16];
610
611 struct radius_attr_hdr *attr = NULL, *tmp;
612 size_t i;
613
614 os_memset(zero, 0, sizeof(zero));
615 addr[0] = (u8 *) msg->hdr;
616 len[0] = sizeof(struct radius_hdr) - MD5_MAC_LEN;
617 addr[1] = zero;
618 len[1] = MD5_MAC_LEN;
619 addr[2] = (u8 *) (msg->hdr + 1);
620 len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
621 addr[3] = secret;
622 len[3] = secret_len;
623 md5_vector(4, addr, len, hash);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700624 if (os_memcmp_const(msg->hdr->authenticator, hash, MD5_MAC_LEN) != 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700625 return 1;
626
627 for (i = 0; i < msg->attr_used; i++) {
628 tmp = radius_get_attr_hdr(msg, i);
629 if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
630 if (attr != NULL) {
631 wpa_printf(MSG_WARNING, "Multiple "
632 "Message-Authenticator attributes "
633 "in RADIUS message");
634 return 1;
635 }
636 attr = tmp;
637 }
638 }
639
640 if (attr == NULL) {
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700641 if (require_message_authenticator) {
642 wpa_printf(MSG_WARNING,
643 "Missing Message-Authenticator attribute in RADIUS message");
644 return 1;
645 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700646 return 0;
647 }
648
649 os_memcpy(orig, attr + 1, MD5_MAC_LEN);
650 os_memset(attr + 1, 0, MD5_MAC_LEN);
651 os_memcpy(orig_authenticator, msg->hdr->authenticator,
652 sizeof(orig_authenticator));
653 os_memset(msg->hdr->authenticator, 0,
654 sizeof(msg->hdr->authenticator));
655 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
656 wpabuf_len(msg->buf), auth);
657 os_memcpy(attr + 1, orig, MD5_MAC_LEN);
658 os_memcpy(msg->hdr->authenticator, orig_authenticator,
659 sizeof(orig_authenticator));
660
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700661 return os_memcmp_const(orig, auth, MD5_MAC_LEN) != 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700662}
663
664
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700665static int radius_msg_add_attr_to_array(struct radius_msg *msg,
666 struct radius_attr_hdr *attr)
667{
668 if (msg->attr_used >= msg->attr_size) {
669 size_t *nattr_pos;
Hai Shalomfdcde762020-04-02 11:19:20 -0700670 size_t nlen = msg->attr_size * 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700671
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700672 nattr_pos = os_realloc_array(msg->attr_pos, nlen,
673 sizeof(*msg->attr_pos));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700674 if (nattr_pos == NULL)
675 return -1;
676
677 msg->attr_pos = nattr_pos;
678 msg->attr_size = nlen;
679 }
680
681 msg->attr_pos[msg->attr_used++] =
682 (unsigned char *) attr - wpabuf_head_u8(msg->buf);
683
684 return 0;
685}
686
687
Sunil Ravia04bd252022-05-02 22:54:18 -0700688struct radius_attr_hdr * radius_msg_add_attr(struct radius_msg *msg, u16 type,
689 const u8 *data, size_t data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690{
Sunil Ravia04bd252022-05-02 22:54:18 -0700691 size_t buf_needed, max_len;
692 struct radius_attr_hdr *attr = NULL;
693 struct radius_attr_hdr_ext *ext;
694 u8 ext_type = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700695
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700696 if (TEST_FAIL())
697 return NULL;
698
Sunil Ravia04bd252022-05-02 22:54:18 -0700699 if (type > 255) {
700 if (!radius_is_ext_type(type >> 8)) {
701 wpa_printf(MSG_ERROR,
702 "%s: Undefined extended type %d.%d",
703 __func__, type >> 8, type & 0xff);
704 return NULL;
705 }
706 ext_type = type & 0xff;
707 type >>= 8;
708 } else if (radius_is_ext_type(type)) {
709 wpa_printf(MSG_ERROR, "%s: Unexpected extended type use for %d",
710 __func__, type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711 }
712
Sunil Ravia04bd252022-05-02 22:54:18 -0700713 if (radius_is_long_ext_type(type)) {
714 size_t hdr_len = sizeof(struct radius_attr_hdr_ext) + 1;
715 size_t plen = 255 - hdr_len;
716 size_t num;
717
718 max_len = 4096;
719 num = (data_len + plen - 1) / plen;
720 if (num == 0)
721 num = 1;
722 buf_needed = num * hdr_len + data_len;
723 } else if (radius_is_ext_type(type)) {
724 max_len = RADIUS_MAX_EXT_ATTR_LEN;
725 buf_needed = sizeof(struct radius_attr_hdr_ext) + data_len;
726 } else {
727 max_len = RADIUS_MAX_ATTR_LEN;
728 buf_needed = sizeof(*attr) + data_len;
729 }
730 if (data_len > max_len) {
731 wpa_printf(MSG_ERROR,
732 "%s: too long attribute (%zu > %zu bytes)",
733 __func__, data_len, max_len);
734 return NULL;
735 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700736
737 if (wpabuf_tailroom(msg->buf) < buf_needed) {
738 /* allocate more space for message buffer */
739 if (wpabuf_resize(&msg->buf, buf_needed) < 0)
740 return NULL;
741 msg->hdr = wpabuf_mhead(msg->buf);
742 }
743
Sunil Ravia04bd252022-05-02 22:54:18 -0700744 if (radius_is_long_ext_type(type)) {
745 size_t plen = 255 - sizeof(struct radius_attr_hdr_ext) - 1;
746 size_t alen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700747
Sunil Ravia04bd252022-05-02 22:54:18 -0700748 do {
749 alen = data_len > plen ? plen : data_len;
750 ext = wpabuf_put(msg->buf,
751 sizeof(struct radius_attr_hdr_ext));
752 if (!attr)
753 attr = (struct radius_attr_hdr *) ext;
754 ext->type = type;
755 ext->length = sizeof(*ext) + 1 + alen;
756 ext->ext_type = ext_type;
757 wpabuf_put_u8(msg->buf, data_len > alen ? 0x80 : 0);
758 wpabuf_put_data(msg->buf, data, data_len);
759 data += alen;
760 data_len -= alen;
761 if (radius_msg_add_attr_to_array(
762 msg, (struct radius_attr_hdr *) ext))
763 return NULL;
764 } while (data_len > 0);
765 } else if (radius_is_ext_type(type)) {
766 ext = wpabuf_put(msg->buf, sizeof(struct radius_attr_hdr_ext));
767 attr = (struct radius_attr_hdr *) ext;
768 ext->type = type;
769 ext->length = sizeof(*ext) + data_len;
770 ext->ext_type = ext_type;
771 wpabuf_put_data(msg->buf, data, data_len);
772 if (radius_msg_add_attr_to_array(msg, attr))
773 return NULL;
774 } else {
775 attr = wpabuf_put(msg->buf, sizeof(struct radius_attr_hdr));
776 attr->type = type;
777 attr->length = sizeof(*attr) + data_len;
778 wpabuf_put_data(msg->buf, data, data_len);
779 if (radius_msg_add_attr_to_array(msg, attr))
780 return NULL;
781 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700782
783 return attr;
784}
785
786
787/**
788 * radius_msg_parse - Parse a RADIUS message
789 * @data: RADIUS message to be parsed
790 * @len: Length of data buffer in octets
791 * Returns: Parsed RADIUS message or %NULL on failure
792 *
793 * This parses a RADIUS message and makes a copy of its data. The caller is
794 * responsible for freeing the returned data with radius_msg_free().
795 */
796struct radius_msg * radius_msg_parse(const u8 *data, size_t len)
797{
798 struct radius_msg *msg;
799 struct radius_hdr *hdr;
800 struct radius_attr_hdr *attr;
801 size_t msg_len;
802 unsigned char *pos, *end;
803
804 if (data == NULL || len < sizeof(*hdr))
805 return NULL;
806
807 hdr = (struct radius_hdr *) data;
808
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700809 msg_len = be_to_host16(hdr->length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700810 if (msg_len < sizeof(*hdr) || msg_len > len) {
811 wpa_printf(MSG_INFO, "RADIUS: Invalid message length");
812 return NULL;
813 }
814
815 if (msg_len < len) {
816 wpa_printf(MSG_DEBUG, "RADIUS: Ignored %lu extra bytes after "
817 "RADIUS message", (unsigned long) len - msg_len);
818 }
819
820 msg = os_zalloc(sizeof(*msg));
821 if (msg == NULL)
822 return NULL;
823
824 msg->buf = wpabuf_alloc_copy(data, msg_len);
825 if (msg->buf == NULL || radius_msg_initialize(msg)) {
826 radius_msg_free(msg);
827 return NULL;
828 }
829 msg->hdr = wpabuf_mhead(msg->buf);
830
831 /* parse attributes */
832 pos = wpabuf_mhead_u8(msg->buf) + sizeof(struct radius_hdr);
833 end = wpabuf_mhead_u8(msg->buf) + wpabuf_len(msg->buf);
834 while (pos < end) {
835 if ((size_t) (end - pos) < sizeof(*attr))
836 goto fail;
837
838 attr = (struct radius_attr_hdr *) pos;
839
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800840 if (attr->length > end - pos || attr->length < sizeof(*attr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700841 goto fail;
842
843 /* TODO: check that attr->length is suitable for attr->type */
844
845 if (radius_msg_add_attr_to_array(msg, attr))
846 goto fail;
847
848 pos += attr->length;
849 }
850
851 return msg;
852
853 fail:
854 radius_msg_free(msg);
855 return NULL;
856}
857
858
859int radius_msg_add_eap(struct radius_msg *msg, const u8 *data, size_t data_len)
860{
861 const u8 *pos = data;
862 size_t left = data_len;
863
864 while (left > 0) {
865 int len;
866 if (left > RADIUS_MAX_ATTR_LEN)
867 len = RADIUS_MAX_ATTR_LEN;
868 else
869 len = left;
870
871 if (!radius_msg_add_attr(msg, RADIUS_ATTR_EAP_MESSAGE,
872 pos, len))
873 return 0;
874
875 pos += len;
876 left -= len;
877 }
878
879 return 1;
880}
881
882
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700883struct wpabuf * radius_msg_get_eap(struct radius_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700884{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700885 struct wpabuf *eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 size_t len, i;
887 struct radius_attr_hdr *attr;
888
889 if (msg == NULL)
890 return NULL;
891
892 len = 0;
893 for (i = 0; i < msg->attr_used; i++) {
894 attr = radius_get_attr_hdr(msg, i);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700895 if (attr->type == RADIUS_ATTR_EAP_MESSAGE &&
896 attr->length > sizeof(struct radius_attr_hdr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897 len += attr->length - sizeof(struct radius_attr_hdr);
898 }
899
900 if (len == 0)
901 return NULL;
902
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700903 eap = wpabuf_alloc(len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 if (eap == NULL)
905 return NULL;
906
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700907 for (i = 0; i < msg->attr_used; i++) {
908 attr = radius_get_attr_hdr(msg, i);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700909 if (attr->type == RADIUS_ATTR_EAP_MESSAGE &&
910 attr->length > sizeof(struct radius_attr_hdr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911 int flen = attr->length - sizeof(*attr);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700912 wpabuf_put_data(eap, attr + 1, flen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700913 }
914 }
915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700916 return eap;
917}
918
919
920int radius_msg_verify_msg_auth(struct radius_msg *msg, const u8 *secret,
921 size_t secret_len, const u8 *req_auth)
922{
923 u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
924 u8 orig_authenticator[16];
925 struct radius_attr_hdr *attr = NULL, *tmp;
926 size_t i;
927
928 for (i = 0; i < msg->attr_used; i++) {
929 tmp = radius_get_attr_hdr(msg, i);
930 if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
931 if (attr != NULL) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800932 wpa_printf(MSG_INFO, "Multiple Message-Authenticator attributes in RADIUS message");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700933 return 1;
934 }
935 attr = tmp;
936 }
937 }
938
939 if (attr == NULL) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800940 wpa_printf(MSG_INFO, "No Message-Authenticator attribute found");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700941 return 1;
942 }
943
944 os_memcpy(orig, attr + 1, MD5_MAC_LEN);
945 os_memset(attr + 1, 0, MD5_MAC_LEN);
946 if (req_auth) {
947 os_memcpy(orig_authenticator, msg->hdr->authenticator,
948 sizeof(orig_authenticator));
949 os_memcpy(msg->hdr->authenticator, req_auth,
950 sizeof(msg->hdr->authenticator));
951 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700952 if (hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
953 wpabuf_len(msg->buf), auth) < 0)
954 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700955 os_memcpy(attr + 1, orig, MD5_MAC_LEN);
956 if (req_auth) {
957 os_memcpy(msg->hdr->authenticator, orig_authenticator,
958 sizeof(orig_authenticator));
959 }
960
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700961 if (os_memcmp_const(orig, auth, MD5_MAC_LEN) != 0) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800962 wpa_printf(MSG_INFO, "Invalid Message-Authenticator!");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963 return 1;
964 }
965
966 return 0;
967}
968
969
970int radius_msg_verify(struct radius_msg *msg, const u8 *secret,
971 size_t secret_len, struct radius_msg *sent_msg, int auth)
972{
973 const u8 *addr[4];
974 size_t len[4];
975 u8 hash[MD5_MAC_LEN];
976
977 if (sent_msg == NULL) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800978 wpa_printf(MSG_INFO, "No matching Access-Request message found");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700979 return 1;
980 }
981
982 if (auth &&
983 radius_msg_verify_msg_auth(msg, secret, secret_len,
984 sent_msg->hdr->authenticator)) {
985 return 1;
986 }
987
988 /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
989 addr[0] = (u8 *) msg->hdr;
990 len[0] = 1 + 1 + 2;
991 addr[1] = sent_msg->hdr->authenticator;
992 len[1] = MD5_MAC_LEN;
993 addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
994 len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
995 addr[3] = secret;
996 len[3] = secret_len;
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700997 if (md5_vector(4, addr, len, hash) < 0 ||
998 os_memcmp_const(hash, msg->hdr->authenticator, MD5_MAC_LEN) != 0) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800999 wpa_printf(MSG_INFO, "Response Authenticator invalid!");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000 return 1;
1001 }
1002
1003 return 0;
1004}
1005
1006
1007int radius_msg_copy_attr(struct radius_msg *dst, struct radius_msg *src,
1008 u8 type)
1009{
1010 struct radius_attr_hdr *attr;
1011 size_t i;
1012 int count = 0;
1013
1014 for (i = 0; i < src->attr_used; i++) {
1015 attr = radius_get_attr_hdr(src, i);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001016 if (attr->type == type && attr->length >= sizeof(*attr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001017 if (!radius_msg_add_attr(dst, type, (u8 *) (attr + 1),
1018 attr->length - sizeof(*attr)))
1019 return -1;
1020 count++;
1021 }
1022 }
1023
1024 return count;
1025}
1026
1027
1028/* Create Request Authenticator. The value should be unique over the lifetime
1029 * of the shared secret between authenticator and authentication server.
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001030 */
1031int radius_msg_make_authenticator(struct radius_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032{
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001033 return os_get_random((u8 *) &msg->hdr->authenticator,
1034 sizeof(msg->hdr->authenticator));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001035}
1036
1037
1038/* Get Vendor-specific RADIUS Attribute from a parsed RADIUS message.
1039 * Returns the Attribute payload and sets alen to indicate the length of the
1040 * payload if a vendor attribute with subtype is found, otherwise returns NULL.
1041 * The returned payload is allocated with os_malloc() and caller must free it
1042 * by calling os_free().
1043 */
1044static u8 *radius_msg_get_vendor_attr(struct radius_msg *msg, u32 vendor,
1045 u8 subtype, size_t *alen)
1046{
1047 u8 *data, *pos;
1048 size_t i, len;
1049
1050 if (msg == NULL)
1051 return NULL;
1052
1053 for (i = 0; i < msg->attr_used; i++) {
1054 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
1055 size_t left;
1056 u32 vendor_id;
1057 struct radius_attr_vendor *vhdr;
1058
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001059 if (attr->type != RADIUS_ATTR_VENDOR_SPECIFIC ||
1060 attr->length < sizeof(*attr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001061 continue;
1062
1063 left = attr->length - sizeof(*attr);
1064 if (left < 4)
1065 continue;
1066
1067 pos = (u8 *) (attr + 1);
1068
1069 os_memcpy(&vendor_id, pos, 4);
1070 pos += 4;
1071 left -= 4;
1072
1073 if (ntohl(vendor_id) != vendor)
1074 continue;
1075
1076 while (left >= sizeof(*vhdr)) {
1077 vhdr = (struct radius_attr_vendor *) pos;
1078 if (vhdr->vendor_length > left ||
1079 vhdr->vendor_length < sizeof(*vhdr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080 break;
1081 }
1082 if (vhdr->vendor_type != subtype) {
1083 pos += vhdr->vendor_length;
1084 left -= vhdr->vendor_length;
1085 continue;
1086 }
1087
1088 len = vhdr->vendor_length - sizeof(*vhdr);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001089 data = os_memdup(pos + sizeof(*vhdr), len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090 if (data == NULL)
1091 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 if (alen)
1093 *alen = len;
1094 return data;
1095 }
1096 }
1097
1098 return NULL;
1099}
1100
1101
1102static u8 * decrypt_ms_key(const u8 *key, size_t len,
1103 const u8 *req_authenticator,
1104 const u8 *secret, size_t secret_len, size_t *reslen)
1105{
1106 u8 *plain, *ppos, *res;
1107 const u8 *pos;
1108 size_t left, plen;
1109 u8 hash[MD5_MAC_LEN];
1110 int i, first = 1;
1111 const u8 *addr[3];
1112 size_t elen[3];
1113
1114 /* key: 16-bit salt followed by encrypted key info */
1115
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001116 if (len < 2 + 16) {
1117 wpa_printf(MSG_DEBUG, "RADIUS: %s: Len is too small: %d",
1118 __func__, (int) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001119 return NULL;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001120 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001121
1122 pos = key + 2;
1123 left = len - 2;
1124 if (left % 16) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001125 wpa_printf(MSG_INFO, "RADIUS: Invalid ms key len %lu",
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001126 (unsigned long) left);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127 return NULL;
1128 }
1129
1130 plen = left;
1131 ppos = plain = os_malloc(plen);
1132 if (plain == NULL)
1133 return NULL;
1134 plain[0] = 0;
1135
1136 while (left > 0) {
1137 /* b(1) = MD5(Secret + Request-Authenticator + Salt)
1138 * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
1139
1140 addr[0] = secret;
1141 elen[0] = secret_len;
1142 if (first) {
1143 addr[1] = req_authenticator;
1144 elen[1] = MD5_MAC_LEN;
1145 addr[2] = key;
1146 elen[2] = 2; /* Salt */
1147 } else {
1148 addr[1] = pos - MD5_MAC_LEN;
1149 elen[1] = MD5_MAC_LEN;
1150 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001151 if (md5_vector(first ? 3 : 2, addr, elen, hash) < 0) {
1152 os_free(plain);
1153 return NULL;
1154 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155 first = 0;
1156
1157 for (i = 0; i < MD5_MAC_LEN; i++)
1158 *ppos++ = *pos++ ^ hash[i];
1159 left -= MD5_MAC_LEN;
1160 }
1161
1162 if (plain[0] == 0 || plain[0] > plen - 1) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001163 wpa_printf(MSG_INFO, "RADIUS: Failed to decrypt MPPE key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164 os_free(plain);
1165 return NULL;
1166 }
1167
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001168 res = os_memdup(plain + 1, plain[0]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 if (res == NULL) {
1170 os_free(plain);
1171 return NULL;
1172 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173 if (reslen)
1174 *reslen = plain[0];
1175 os_free(plain);
1176 return res;
1177}
1178
1179
1180static void encrypt_ms_key(const u8 *key, size_t key_len, u16 salt,
1181 const u8 *req_authenticator,
1182 const u8 *secret, size_t secret_len,
1183 u8 *ebuf, size_t *elen)
1184{
1185 int i, len, first = 1;
1186 u8 hash[MD5_MAC_LEN], saltbuf[2], *pos;
1187 const u8 *addr[3];
1188 size_t _len[3];
1189
1190 WPA_PUT_BE16(saltbuf, salt);
1191
1192 len = 1 + key_len;
1193 if (len & 0x0f) {
1194 len = (len & 0xf0) + 16;
1195 }
1196 os_memset(ebuf, 0, len);
1197 ebuf[0] = key_len;
1198 os_memcpy(ebuf + 1, key, key_len);
1199
1200 *elen = len;
1201
1202 pos = ebuf;
1203 while (len > 0) {
1204 /* b(1) = MD5(Secret + Request-Authenticator + Salt)
1205 * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
1206 addr[0] = secret;
1207 _len[0] = secret_len;
1208 if (first) {
1209 addr[1] = req_authenticator;
1210 _len[1] = MD5_MAC_LEN;
1211 addr[2] = saltbuf;
1212 _len[2] = sizeof(saltbuf);
1213 } else {
1214 addr[1] = pos - MD5_MAC_LEN;
1215 _len[1] = MD5_MAC_LEN;
1216 }
1217 md5_vector(first ? 3 : 2, addr, _len, hash);
1218 first = 0;
1219
1220 for (i = 0; i < MD5_MAC_LEN; i++)
1221 *pos++ ^= hash[i];
1222
1223 len -= MD5_MAC_LEN;
1224 }
1225}
1226
1227
1228struct radius_ms_mppe_keys *
1229radius_msg_get_ms_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
1230 const u8 *secret, size_t secret_len)
1231{
1232 u8 *key;
1233 size_t keylen;
1234 struct radius_ms_mppe_keys *keys;
1235
1236 if (msg == NULL || sent_msg == NULL)
1237 return NULL;
1238
1239 keys = os_zalloc(sizeof(*keys));
1240 if (keys == NULL)
1241 return NULL;
1242
1243 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
1244 RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY,
1245 &keylen);
1246 if (key) {
1247 keys->send = decrypt_ms_key(key, keylen,
1248 sent_msg->hdr->authenticator,
1249 secret, secret_len,
1250 &keys->send_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001251 if (!keys->send) {
1252 wpa_printf(MSG_DEBUG,
1253 "RADIUS: Failed to decrypt send key");
1254 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255 os_free(key);
1256 }
1257
1258 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
1259 RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY,
1260 &keylen);
1261 if (key) {
1262 keys->recv = decrypt_ms_key(key, keylen,
1263 sent_msg->hdr->authenticator,
1264 secret, secret_len,
1265 &keys->recv_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001266 if (!keys->recv) {
1267 wpa_printf(MSG_DEBUG,
1268 "RADIUS: Failed to decrypt recv key");
1269 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270 os_free(key);
1271 }
1272
1273 return keys;
1274}
1275
1276
1277struct radius_ms_mppe_keys *
1278radius_msg_get_cisco_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
1279 const u8 *secret, size_t secret_len)
1280{
1281 u8 *key;
1282 size_t keylen;
1283 struct radius_ms_mppe_keys *keys;
1284
1285 if (msg == NULL || sent_msg == NULL)
1286 return NULL;
1287
1288 keys = os_zalloc(sizeof(*keys));
1289 if (keys == NULL)
1290 return NULL;
1291
1292 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_CISCO,
1293 RADIUS_CISCO_AV_PAIR, &keylen);
1294 if (key && keylen == 51 &&
1295 os_memcmp(key, "leap:session-key=", 17) == 0) {
1296 keys->recv = decrypt_ms_key(key + 17, keylen - 17,
1297 sent_msg->hdr->authenticator,
1298 secret, secret_len,
1299 &keys->recv_len);
1300 }
1301 os_free(key);
1302
1303 return keys;
1304}
1305
1306
1307int radius_msg_add_mppe_keys(struct radius_msg *msg,
1308 const u8 *req_authenticator,
1309 const u8 *secret, size_t secret_len,
1310 const u8 *send_key, size_t send_key_len,
1311 const u8 *recv_key, size_t recv_key_len)
1312{
1313 struct radius_attr_hdr *attr;
1314 u32 vendor_id = htonl(RADIUS_VENDOR_ID_MICROSOFT);
1315 u8 *buf;
1316 struct radius_attr_vendor *vhdr;
1317 u8 *pos;
1318 size_t elen;
1319 int hlen;
1320 u16 salt;
1321
1322 hlen = sizeof(vendor_id) + sizeof(*vhdr) + 2;
1323
1324 /* MS-MPPE-Send-Key */
1325 buf = os_malloc(hlen + send_key_len + 16);
1326 if (buf == NULL) {
1327 return 0;
1328 }
1329 pos = buf;
1330 os_memcpy(pos, &vendor_id, sizeof(vendor_id));
1331 pos += sizeof(vendor_id);
1332 vhdr = (struct radius_attr_vendor *) pos;
1333 vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY;
1334 pos = (u8 *) (vhdr + 1);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001335 if (os_get_random((u8 *) &salt, sizeof(salt)) < 0) {
1336 os_free(buf);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001337 return 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001338 }
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001339 salt |= 0x8000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001340 WPA_PUT_BE16(pos, salt);
1341 pos += 2;
1342 encrypt_ms_key(send_key, send_key_len, salt, req_authenticator, secret,
1343 secret_len, pos, &elen);
1344 vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
1345
1346 attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1347 buf, hlen + elen);
1348 os_free(buf);
1349 if (attr == NULL) {
1350 return 0;
1351 }
1352
1353 /* MS-MPPE-Recv-Key */
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -07001354 buf = os_malloc(hlen + recv_key_len + 16);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 if (buf == NULL) {
1356 return 0;
1357 }
1358 pos = buf;
1359 os_memcpy(pos, &vendor_id, sizeof(vendor_id));
1360 pos += sizeof(vendor_id);
1361 vhdr = (struct radius_attr_vendor *) pos;
1362 vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY;
1363 pos = (u8 *) (vhdr + 1);
1364 salt ^= 1;
1365 WPA_PUT_BE16(pos, salt);
1366 pos += 2;
1367 encrypt_ms_key(recv_key, recv_key_len, salt, req_authenticator, secret,
1368 secret_len, pos, &elen);
1369 vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
1370
1371 attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1372 buf, hlen + elen);
1373 os_free(buf);
1374 if (attr == NULL) {
1375 return 0;
1376 }
1377
1378 return 1;
1379}
1380
1381
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001382int radius_msg_add_wfa(struct radius_msg *msg, u8 subtype, const u8 *data,
1383 size_t len)
1384{
1385 struct radius_attr_hdr *attr;
1386 u8 *buf, *pos;
1387 size_t alen;
1388
1389 alen = 4 + 2 + len;
1390 buf = os_malloc(alen);
1391 if (buf == NULL)
1392 return 0;
1393 pos = buf;
1394 WPA_PUT_BE32(pos, RADIUS_VENDOR_ID_WFA);
1395 pos += 4;
1396 *pos++ = subtype;
1397 *pos++ = 2 + len;
1398 os_memcpy(pos, data, len);
1399 attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1400 buf, alen);
1401 os_free(buf);
1402 if (attr == NULL)
1403 return 0;
1404
1405 return 1;
1406}
1407
1408
Sunil Ravia04bd252022-05-02 22:54:18 -07001409int radius_msg_add_ext_vs(struct radius_msg *msg, u16 type, u32 vendor_id,
1410 u8 vendor_type, const u8 *data, size_t len)
1411{
1412 struct radius_attr_hdr *attr;
1413 u8 *buf, *pos;
1414 size_t alen;
1415
1416 alen = 4 + 1 + len;
1417 buf = os_malloc(alen);
1418 if (!buf)
1419 return 0;
1420 pos = buf;
1421 WPA_PUT_BE32(pos, vendor_id);
1422 pos += 4;
1423 *pos++ = vendor_type;
1424 os_memcpy(pos, data, len);
1425 attr = radius_msg_add_attr(msg, type, buf, alen);
1426 os_free(buf);
1427 return attr != NULL;
1428}
1429
1430
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001431int radius_user_password_hide(struct radius_msg *msg,
1432 const u8 *data, size_t data_len,
1433 const u8 *secret, size_t secret_len,
1434 u8 *buf, size_t buf_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001435{
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001436 size_t padlen, i, pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001437 const u8 *addr[2];
1438 size_t len[2];
1439 u8 hash[16];
1440
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001441 if (data_len + 16 > buf_len)
1442 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001443
1444 os_memcpy(buf, data, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001445
1446 padlen = data_len % 16;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001447 if (padlen && data_len < buf_len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001448 padlen = 16 - padlen;
1449 os_memset(buf + data_len, 0, padlen);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001450 buf_len = data_len + padlen;
1451 } else {
1452 buf_len = data_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001453 }
1454
1455 addr[0] = secret;
1456 len[0] = secret_len;
1457 addr[1] = msg->hdr->authenticator;
1458 len[1] = 16;
1459 md5_vector(2, addr, len, hash);
1460
1461 for (i = 0; i < 16; i++)
1462 buf[i] ^= hash[i];
1463 pos = 16;
1464
1465 while (pos < buf_len) {
1466 addr[0] = secret;
1467 len[0] = secret_len;
1468 addr[1] = &buf[pos - 16];
1469 len[1] = 16;
1470 md5_vector(2, addr, len, hash);
1471
1472 for (i = 0; i < 16; i++)
1473 buf[pos + i] ^= hash[i];
1474
1475 pos += 16;
1476 }
1477
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001478 return buf_len;
1479}
1480
1481
1482/* Add User-Password attribute to a RADIUS message and encrypt it as specified
1483 * in RFC 2865, Chap. 5.2 */
1484struct radius_attr_hdr *
1485radius_msg_add_attr_user_password(struct radius_msg *msg,
1486 const u8 *data, size_t data_len,
1487 const u8 *secret, size_t secret_len)
1488{
1489 u8 buf[128];
1490 int res;
1491
1492 res = radius_user_password_hide(msg, data, data_len,
1493 secret, secret_len, buf, sizeof(buf));
1494 if (res < 0)
1495 return NULL;
1496
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001497 return radius_msg_add_attr(msg, RADIUS_ATTR_USER_PASSWORD,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001498 buf, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001499}
1500
1501
1502int radius_msg_get_attr(struct radius_msg *msg, u8 type, u8 *buf, size_t len)
1503{
1504 struct radius_attr_hdr *attr = NULL, *tmp;
1505 size_t i, dlen;
1506
1507 for (i = 0; i < msg->attr_used; i++) {
1508 tmp = radius_get_attr_hdr(msg, i);
1509 if (tmp->type == type) {
1510 attr = tmp;
1511 break;
1512 }
1513 }
1514
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001515 if (!attr || attr->length < sizeof(*attr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001516 return -1;
1517
1518 dlen = attr->length - sizeof(*attr);
1519 if (buf)
1520 os_memcpy(buf, (attr + 1), dlen > len ? len : dlen);
1521 return dlen;
1522}
1523
1524
1525int radius_msg_get_attr_ptr(struct radius_msg *msg, u8 type, u8 **buf,
1526 size_t *len, const u8 *start)
1527{
1528 size_t i;
1529 struct radius_attr_hdr *attr = NULL, *tmp;
1530
1531 for (i = 0; i < msg->attr_used; i++) {
1532 tmp = radius_get_attr_hdr(msg, i);
1533 if (tmp->type == type &&
1534 (start == NULL || (u8 *) tmp > start)) {
1535 attr = tmp;
1536 break;
1537 }
1538 }
1539
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001540 if (!attr || attr->length < sizeof(*attr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001541 return -1;
1542
1543 *buf = (u8 *) (attr + 1);
1544 *len = attr->length - sizeof(*attr);
1545 return 0;
1546}
1547
1548
1549int radius_msg_count_attr(struct radius_msg *msg, u8 type, int min_len)
1550{
1551 size_t i;
1552 int count;
1553
1554 for (count = 0, i = 0; i < msg->attr_used; i++) {
1555 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
1556 if (attr->type == type &&
1557 attr->length >= sizeof(struct radius_attr_hdr) + min_len)
1558 count++;
1559 }
1560
1561 return count;
1562}
1563
1564
1565struct radius_tunnel_attrs {
1566 int tag_used;
1567 int type; /* Tunnel-Type */
1568 int medium_type; /* Tunnel-Medium-Type */
1569 int vlanid;
1570};
1571
1572
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001573static int cmp_int(const void *a, const void *b)
1574{
1575 int x, y;
1576
1577 x = *((int *) a);
1578 y = *((int *) b);
1579 return (x - y);
1580}
1581
1582
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001583/**
1584 * radius_msg_get_vlanid - Parse RADIUS attributes for VLAN tunnel information
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001585 * The k tagged vlans found are sorted by vlan_id and stored in the first k
1586 * items of tagged.
1587 *
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001588 * @msg: RADIUS message
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001589 * @untagged: Pointer to store untagged vid
1590 * @numtagged: Size of tagged
1591 * @tagged: Pointer to store tagged list
1592 *
1593 * Returns: 0 if neither tagged nor untagged configuration is found, 1 otherwise
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001594 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001595int radius_msg_get_vlanid(struct radius_msg *msg, int *untagged, int numtagged,
1596 int *tagged)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001597{
1598 struct radius_tunnel_attrs tunnel[RADIUS_TUNNEL_TAGS], *tun;
1599 size_t i;
1600 struct radius_attr_hdr *attr = NULL;
1601 const u8 *data;
1602 char buf[10];
1603 size_t dlen;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001604 int j, taggedidx = 0, vlan_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001605
1606 os_memset(&tunnel, 0, sizeof(tunnel));
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001607 for (j = 0; j < numtagged; j++)
1608 tagged[j] = 0;
1609 *untagged = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001610
1611 for (i = 0; i < msg->attr_used; i++) {
1612 attr = radius_get_attr_hdr(msg, i);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001613 if (attr->length < sizeof(*attr))
1614 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001615 data = (const u8 *) (attr + 1);
1616 dlen = attr->length - sizeof(*attr);
1617 if (attr->length < 3)
1618 continue;
1619 if (data[0] >= RADIUS_TUNNEL_TAGS)
1620 tun = &tunnel[0];
1621 else
1622 tun = &tunnel[data[0]];
1623
1624 switch (attr->type) {
1625 case RADIUS_ATTR_TUNNEL_TYPE:
1626 if (attr->length != 6)
1627 break;
1628 tun->tag_used++;
1629 tun->type = WPA_GET_BE24(data + 1);
1630 break;
1631 case RADIUS_ATTR_TUNNEL_MEDIUM_TYPE:
1632 if (attr->length != 6)
1633 break;
1634 tun->tag_used++;
1635 tun->medium_type = WPA_GET_BE24(data + 1);
1636 break;
1637 case RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID:
1638 if (data[0] < RADIUS_TUNNEL_TAGS) {
1639 data++;
1640 dlen--;
1641 }
1642 if (dlen >= sizeof(buf))
1643 break;
1644 os_memcpy(buf, data, dlen);
1645 buf[dlen] = '\0';
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001646 vlan_id = atoi(buf);
1647 if (vlan_id <= 0)
1648 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001649 tun->tag_used++;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001650 tun->vlanid = vlan_id;
1651 break;
1652 case RADIUS_ATTR_EGRESS_VLANID: /* RFC 4675 */
1653 if (attr->length != 6)
1654 break;
1655 vlan_id = WPA_GET_BE24(data + 1);
1656 if (vlan_id <= 0)
1657 break;
1658 if (data[0] == 0x32)
1659 *untagged = vlan_id;
1660 else if (data[0] == 0x31 && tagged &&
1661 taggedidx < numtagged)
1662 tagged[taggedidx++] = vlan_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001663 break;
1664 }
1665 }
1666
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001667 /* Use tunnel with the lowest tag for untagged VLAN id */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001668 for (i = 0; i < RADIUS_TUNNEL_TAGS; i++) {
1669 tun = &tunnel[i];
1670 if (tun->tag_used &&
1671 tun->type == RADIUS_TUNNEL_TYPE_VLAN &&
1672 tun->medium_type == RADIUS_TUNNEL_MEDIUM_TYPE_802 &&
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001673 tun->vlanid > 0) {
1674 *untagged = tun->vlanid;
1675 break;
1676 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677 }
1678
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001679 if (taggedidx)
1680 qsort(tagged, taggedidx, sizeof(int), cmp_int);
1681
1682 if (*untagged > 0 || taggedidx)
1683 return 1;
Dmitry Shmidt83474442015-04-15 13:47:09 -07001684 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685}
1686
1687
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001688/**
1689 * radius_msg_get_tunnel_password - Parse RADIUS attribute Tunnel-Password
1690 * @msg: Received RADIUS message
1691 * @keylen: Length of returned password
1692 * @secret: RADIUS shared secret
1693 * @secret_len: Length of secret
1694 * @sent_msg: Sent RADIUS message
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001695 * @n: Number of password attribute to return (starting with 0)
1696 * Returns: Pointer to n-th password (free with os_free) or %NULL
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001697 */
1698char * radius_msg_get_tunnel_password(struct radius_msg *msg, int *keylen,
1699 const u8 *secret, size_t secret_len,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001700 struct radius_msg *sent_msg, size_t n)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001701{
1702 u8 *buf = NULL;
1703 size_t buflen;
1704 const u8 *salt;
1705 u8 *str;
1706 const u8 *addr[3];
1707 size_t len[3];
1708 u8 hash[16];
1709 u8 *pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001710 size_t i, j = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001711 struct radius_attr_hdr *attr;
1712 const u8 *data;
1713 size_t dlen;
1714 const u8 *fdata = NULL; /* points to found item */
1715 size_t fdlen = -1;
1716 char *ret = NULL;
1717
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001718 /* find n-th valid Tunnel-Password attribute */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001719 for (i = 0; i < msg->attr_used; i++) {
1720 attr = radius_get_attr_hdr(msg, i);
1721 if (attr == NULL ||
1722 attr->type != RADIUS_ATTR_TUNNEL_PASSWORD) {
1723 continue;
1724 }
1725 if (attr->length <= 5)
1726 continue;
1727 data = (const u8 *) (attr + 1);
1728 dlen = attr->length - sizeof(*attr);
1729 if (dlen <= 3 || dlen % 16 != 3)
1730 continue;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001731 j++;
1732 if (j <= n)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001733 continue;
1734
1735 fdata = data;
1736 fdlen = dlen;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001737 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001738 }
1739 if (fdata == NULL)
1740 goto out;
1741
1742 /* alloc writable memory for decryption */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001743 buf = os_memdup(fdata, fdlen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001744 if (buf == NULL)
1745 goto out;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001746 buflen = fdlen;
1747
1748 /* init pointers */
1749 salt = buf + 1;
1750 str = buf + 3;
1751
1752 /* decrypt blocks */
1753 pos = buf + buflen - 16; /* last block */
1754 while (pos >= str + 16) { /* all but the first block */
1755 addr[0] = secret;
1756 len[0] = secret_len;
1757 addr[1] = pos - 16;
1758 len[1] = 16;
1759 md5_vector(2, addr, len, hash);
1760
1761 for (i = 0; i < 16; i++)
1762 pos[i] ^= hash[i];
1763
1764 pos -= 16;
1765 }
1766
1767 /* decrypt first block */
1768 if (str != pos)
1769 goto out;
1770 addr[0] = secret;
1771 len[0] = secret_len;
1772 addr[1] = sent_msg->hdr->authenticator;
1773 len[1] = 16;
1774 addr[2] = salt;
1775 len[2] = 2;
1776 md5_vector(3, addr, len, hash);
1777
1778 for (i = 0; i < 16; i++)
1779 pos[i] ^= hash[i];
1780
1781 /* derive plaintext length from first subfield */
1782 *keylen = (unsigned char) str[0];
1783 if ((u8 *) (str + *keylen) >= (u8 *) (buf + buflen)) {
1784 /* decryption error - invalid key length */
1785 goto out;
1786 }
1787 if (*keylen == 0) {
1788 /* empty password */
1789 goto out;
1790 }
1791
1792 /* copy passphrase into new buffer */
1793 ret = os_malloc(*keylen);
1794 if (ret)
1795 os_memcpy(ret, str + 1, *keylen);
1796
1797out:
1798 /* return new buffer */
1799 os_free(buf);
1800 return ret;
1801}
1802
1803
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804void radius_free_class(struct radius_class_data *c)
1805{
1806 size_t i;
1807 if (c == NULL)
1808 return;
1809 for (i = 0; i < c->count; i++)
1810 os_free(c->attr[i].data);
1811 os_free(c->attr);
1812 c->attr = NULL;
1813 c->count = 0;
1814}
1815
1816
1817int radius_copy_class(struct radius_class_data *dst,
1818 const struct radius_class_data *src)
1819{
1820 size_t i;
1821
1822 if (src->attr == NULL)
1823 return 0;
1824
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001825 dst->attr = os_calloc(src->count, sizeof(struct radius_attr_data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001826 if (dst->attr == NULL)
1827 return -1;
1828
1829 dst->count = 0;
1830
1831 for (i = 0; i < src->count; i++) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001832 dst->attr[i].data = os_memdup(src->attr[i].data,
1833 src->attr[i].len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001834 if (dst->attr[i].data == NULL)
1835 break;
1836 dst->count++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001837 dst->attr[i].len = src->attr[i].len;
1838 }
1839
1840 return 0;
1841}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001842
1843
1844u8 radius_msg_find_unlisted_attr(struct radius_msg *msg, u8 *attrs)
1845{
1846 size_t i, j;
1847 struct radius_attr_hdr *attr;
1848
1849 for (i = 0; i < msg->attr_used; i++) {
1850 attr = radius_get_attr_hdr(msg, i);
1851
1852 for (j = 0; attrs[j]; j++) {
1853 if (attr->type == attrs[j])
1854 break;
1855 }
1856
1857 if (attrs[j] == 0)
1858 return attr->type; /* unlisted attr */
1859 }
1860
1861 return 0;
1862}
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001863
1864
1865int radius_gen_session_id(u8 *id, size_t len)
1866{
1867 /*
1868 * Acct-Session-Id and Acct-Multi-Session-Id should be globally and
1869 * temporarily unique. A high quality random number is required
1870 * therefore. This could be be improved by switching to a GUID.
1871 */
1872 return os_get_random(id, len);
1873}