blob: a6304e1ccb4421af0e160ed29809ce4b24313b72 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * RADIUS message processing
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003 * Copyright (c) 2002-2009, 2011-2015, 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 {
162 u8 type;
163 char *name;
164 enum {
165 RADIUS_ATTR_UNDIST, RADIUS_ATTR_TEXT, RADIUS_ATTR_IP,
166 RADIUS_ATTR_HEXDUMP, RADIUS_ATTR_INT32, RADIUS_ATTR_IPV6
167 } data_type;
168};
169
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700170static const struct radius_attr_type radius_attrs[] =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700171{
172 { RADIUS_ATTR_USER_NAME, "User-Name", RADIUS_ATTR_TEXT },
173 { RADIUS_ATTR_USER_PASSWORD, "User-Password", RADIUS_ATTR_UNDIST },
174 { RADIUS_ATTR_NAS_IP_ADDRESS, "NAS-IP-Address", RADIUS_ATTR_IP },
175 { RADIUS_ATTR_NAS_PORT, "NAS-Port", RADIUS_ATTR_INT32 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800176 { RADIUS_ATTR_FRAMED_IP_ADDRESS, "Framed-IP-Address", RADIUS_ATTR_IP },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700177 { RADIUS_ATTR_FRAMED_MTU, "Framed-MTU", RADIUS_ATTR_INT32 },
178 { RADIUS_ATTR_REPLY_MESSAGE, "Reply-Message", RADIUS_ATTR_TEXT },
179 { RADIUS_ATTR_STATE, "State", RADIUS_ATTR_UNDIST },
180 { RADIUS_ATTR_CLASS, "Class", RADIUS_ATTR_UNDIST },
181 { RADIUS_ATTR_VENDOR_SPECIFIC, "Vendor-Specific", RADIUS_ATTR_UNDIST },
182 { RADIUS_ATTR_SESSION_TIMEOUT, "Session-Timeout", RADIUS_ATTR_INT32 },
183 { RADIUS_ATTR_IDLE_TIMEOUT, "Idle-Timeout", RADIUS_ATTR_INT32 },
184 { RADIUS_ATTR_TERMINATION_ACTION, "Termination-Action",
185 RADIUS_ATTR_INT32 },
186 { RADIUS_ATTR_CALLED_STATION_ID, "Called-Station-Id",
187 RADIUS_ATTR_TEXT },
188 { RADIUS_ATTR_CALLING_STATION_ID, "Calling-Station-Id",
189 RADIUS_ATTR_TEXT },
190 { RADIUS_ATTR_NAS_IDENTIFIER, "NAS-Identifier", RADIUS_ATTR_TEXT },
191 { RADIUS_ATTR_PROXY_STATE, "Proxy-State", RADIUS_ATTR_UNDIST },
192 { RADIUS_ATTR_ACCT_STATUS_TYPE, "Acct-Status-Type",
193 RADIUS_ATTR_INT32 },
194 { RADIUS_ATTR_ACCT_DELAY_TIME, "Acct-Delay-Time", RADIUS_ATTR_INT32 },
195 { RADIUS_ATTR_ACCT_INPUT_OCTETS, "Acct-Input-Octets",
196 RADIUS_ATTR_INT32 },
197 { RADIUS_ATTR_ACCT_OUTPUT_OCTETS, "Acct-Output-Octets",
198 RADIUS_ATTR_INT32 },
199 { RADIUS_ATTR_ACCT_SESSION_ID, "Acct-Session-Id", RADIUS_ATTR_TEXT },
200 { RADIUS_ATTR_ACCT_AUTHENTIC, "Acct-Authentic", RADIUS_ATTR_INT32 },
201 { RADIUS_ATTR_ACCT_SESSION_TIME, "Acct-Session-Time",
202 RADIUS_ATTR_INT32 },
203 { RADIUS_ATTR_ACCT_INPUT_PACKETS, "Acct-Input-Packets",
204 RADIUS_ATTR_INT32 },
205 { RADIUS_ATTR_ACCT_OUTPUT_PACKETS, "Acct-Output-Packets",
206 RADIUS_ATTR_INT32 },
207 { RADIUS_ATTR_ACCT_TERMINATE_CAUSE, "Acct-Terminate-Cause",
208 RADIUS_ATTR_INT32 },
209 { RADIUS_ATTR_ACCT_MULTI_SESSION_ID, "Acct-Multi-Session-Id",
210 RADIUS_ATTR_TEXT },
211 { RADIUS_ATTR_ACCT_LINK_COUNT, "Acct-Link-Count", RADIUS_ATTR_INT32 },
212 { RADIUS_ATTR_ACCT_INPUT_GIGAWORDS, "Acct-Input-Gigawords",
213 RADIUS_ATTR_INT32 },
214 { RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS, "Acct-Output-Gigawords",
215 RADIUS_ATTR_INT32 },
216 { RADIUS_ATTR_EVENT_TIMESTAMP, "Event-Timestamp",
217 RADIUS_ATTR_INT32 },
218 { RADIUS_ATTR_NAS_PORT_TYPE, "NAS-Port-Type", RADIUS_ATTR_INT32 },
219 { RADIUS_ATTR_TUNNEL_TYPE, "Tunnel-Type", RADIUS_ATTR_HEXDUMP },
220 { RADIUS_ATTR_TUNNEL_MEDIUM_TYPE, "Tunnel-Medium-Type",
221 RADIUS_ATTR_HEXDUMP },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800222 { RADIUS_ATTR_TUNNEL_PASSWORD, "Tunnel-Password",
223 RADIUS_ATTR_UNDIST },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700224 { RADIUS_ATTR_CONNECT_INFO, "Connect-Info", RADIUS_ATTR_TEXT },
225 { RADIUS_ATTR_EAP_MESSAGE, "EAP-Message", RADIUS_ATTR_UNDIST },
226 { RADIUS_ATTR_MESSAGE_AUTHENTICATOR, "Message-Authenticator",
227 RADIUS_ATTR_UNDIST },
228 { RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID, "Tunnel-Private-Group-Id",
229 RADIUS_ATTR_HEXDUMP },
230 { RADIUS_ATTR_ACCT_INTERIM_INTERVAL, "Acct-Interim-Interval",
231 RADIUS_ATTR_INT32 },
Dmitry Shmidt04949592012-07-19 12:16:46 -0700232 { RADIUS_ATTR_CHARGEABLE_USER_IDENTITY, "Chargeable-User-Identity",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700233 RADIUS_ATTR_TEXT },
234 { RADIUS_ATTR_NAS_IPV6_ADDRESS, "NAS-IPv6-Address", RADIUS_ATTR_IPV6 },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700235 { RADIUS_ATTR_ERROR_CAUSE, "Error-Cause", RADIUS_ATTR_INT32 },
236 { RADIUS_ATTR_EAP_KEY_NAME, "EAP-Key-Name", RADIUS_ATTR_HEXDUMP },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800237 { RADIUS_ATTR_OPERATOR_NAME, "Operator-Name", RADIUS_ATTR_TEXT },
238 { RADIUS_ATTR_LOCATION_INFO, "Location-Information",
239 RADIUS_ATTR_HEXDUMP },
240 { RADIUS_ATTR_LOCATION_DATA, "Location-Data", RADIUS_ATTR_HEXDUMP },
241 { RADIUS_ATTR_BASIC_LOCATION_POLICY_RULES,
242 "Basic-Location-Policy-Rules", RADIUS_ATTR_HEXDUMP },
243 { RADIUS_ATTR_EXTENDED_LOCATION_POLICY_RULES,
244 "Extended-Location-Policy-Rules", RADIUS_ATTR_HEXDUMP },
245 { RADIUS_ATTR_LOCATION_CAPABLE, "Location-Capable", RADIUS_ATTR_INT32 },
246 { RADIUS_ATTR_REQUESTED_LOCATION_INFO, "Requested-Location-Info",
247 RADIUS_ATTR_INT32 },
Dmitry Shmidt03658832014-08-13 11:03:49 -0700248 { RADIUS_ATTR_MOBILITY_DOMAIN_ID, "Mobility-Domain-Id",
249 RADIUS_ATTR_INT32 },
250 { RADIUS_ATTR_WLAN_HESSID, "WLAN-HESSID", RADIUS_ATTR_TEXT },
251 { RADIUS_ATTR_WLAN_PAIRWISE_CIPHER, "WLAN-Pairwise-Cipher",
252 RADIUS_ATTR_HEXDUMP },
253 { RADIUS_ATTR_WLAN_GROUP_CIPHER, "WLAN-Group-Cipher",
254 RADIUS_ATTR_HEXDUMP },
255 { RADIUS_ATTR_WLAN_AKM_SUITE, "WLAN-AKM-Suite",
256 RADIUS_ATTR_HEXDUMP },
257 { RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, "WLAN-Group-Mgmt-Pairwise-Cipher",
258 RADIUS_ATTR_HEXDUMP },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700259};
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700260#define RADIUS_ATTRS ARRAY_SIZE(radius_attrs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261
262
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700263static const struct radius_attr_type *radius_get_attr_type(u8 type)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700264{
265 size_t i;
266
267 for (i = 0; i < RADIUS_ATTRS; i++) {
268 if (type == radius_attrs[i].type)
269 return &radius_attrs[i];
270 }
271
272 return NULL;
273}
274
275
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276static void radius_msg_dump_attr(struct radius_attr_hdr *hdr)
277{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700278 const struct radius_attr_type *attr;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800279 int len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700280 unsigned char *pos;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800281 char buf[1000];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700282
283 attr = radius_get_attr_type(hdr->type);
284
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800285 wpa_printf(MSG_INFO, " Attribute %d (%s) length=%d",
286 hdr->type, attr ? attr->name : "?Unknown?", hdr->length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700288 if (attr == NULL || hdr->length < sizeof(struct radius_attr_hdr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289 return;
290
291 len = hdr->length - sizeof(struct radius_attr_hdr);
292 pos = (unsigned char *) (hdr + 1);
293
294 switch (attr->data_type) {
295 case RADIUS_ATTR_TEXT:
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800296 printf_encode(buf, sizeof(buf), pos, len);
297 wpa_printf(MSG_INFO, " Value: '%s'", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700298 break;
299
300 case RADIUS_ATTR_IP:
301 if (len == 4) {
302 struct in_addr addr;
303 os_memcpy(&addr, pos, 4);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800304 wpa_printf(MSG_INFO, " Value: %s",
305 inet_ntoa(addr));
306 } else {
307 wpa_printf(MSG_INFO, " Invalid IP address length %d",
308 len);
309 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310 break;
311
312#ifdef CONFIG_IPV6
313 case RADIUS_ATTR_IPV6:
314 if (len == 16) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700315 const char *atxt;
316 struct in6_addr *addr = (struct in6_addr *) pos;
317 atxt = inet_ntop(AF_INET6, addr, buf, sizeof(buf));
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800318 wpa_printf(MSG_INFO, " Value: %s",
319 atxt ? atxt : "?");
320 } else {
321 wpa_printf(MSG_INFO, " Invalid IPv6 address length %d",
322 len);
323 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324 break;
325#endif /* CONFIG_IPV6 */
326
327 case RADIUS_ATTR_HEXDUMP:
328 case RADIUS_ATTR_UNDIST:
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800329 wpa_snprintf_hex(buf, sizeof(buf), pos, len);
330 wpa_printf(MSG_INFO, " Value: %s", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700331 break;
332
333 case RADIUS_ATTR_INT32:
334 if (len == 4)
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800335 wpa_printf(MSG_INFO, " Value: %u",
336 WPA_GET_BE32(pos));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337 else
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800338 wpa_printf(MSG_INFO, " Invalid INT32 length %d",
339 len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340 break;
341
342 default:
343 break;
344 }
345}
346
347
348void radius_msg_dump(struct radius_msg *msg)
349{
350 size_t i;
351
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800352 wpa_printf(MSG_INFO, "RADIUS message: code=%d (%s) identifier=%d length=%d",
353 msg->hdr->code, radius_code_string(msg->hdr->code),
354 msg->hdr->identifier, be_to_host16(msg->hdr->length));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700355
356 for (i = 0; i < msg->attr_used; i++) {
357 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
358 radius_msg_dump_attr(attr);
359 }
360}
361
362
363int radius_msg_finish(struct radius_msg *msg, const u8 *secret,
364 size_t secret_len)
365{
366 if (secret) {
367 u8 auth[MD5_MAC_LEN];
368 struct radius_attr_hdr *attr;
369
370 os_memset(auth, 0, MD5_MAC_LEN);
371 attr = radius_msg_add_attr(msg,
372 RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
373 auth, MD5_MAC_LEN);
374 if (attr == NULL) {
375 wpa_printf(MSG_WARNING, "RADIUS: Could not add "
376 "Message-Authenticator");
377 return -1;
378 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700379 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
381 wpabuf_len(msg->buf), (u8 *) (attr + 1));
382 } else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700383 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384
385 if (wpabuf_len(msg->buf) > 0xffff) {
386 wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
387 (unsigned long) wpabuf_len(msg->buf));
388 return -1;
389 }
390 return 0;
391}
392
393
394int radius_msg_finish_srv(struct radius_msg *msg, const u8 *secret,
395 size_t secret_len, const u8 *req_authenticator)
396{
397 u8 auth[MD5_MAC_LEN];
398 struct radius_attr_hdr *attr;
399 const u8 *addr[4];
400 size_t len[4];
401
402 os_memset(auth, 0, MD5_MAC_LEN);
403 attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
404 auth, MD5_MAC_LEN);
405 if (attr == NULL) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800406 wpa_printf(MSG_ERROR, "WARNING: Could not add Message-Authenticator");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700407 return -1;
408 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700409 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700410 os_memcpy(msg->hdr->authenticator, req_authenticator,
411 sizeof(msg->hdr->authenticator));
412 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
413 wpabuf_len(msg->buf), (u8 *) (attr + 1));
414
415 /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
416 addr[0] = (u8 *) msg->hdr;
417 len[0] = 1 + 1 + 2;
418 addr[1] = req_authenticator;
419 len[1] = MD5_MAC_LEN;
420 addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
421 len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
422 addr[3] = secret;
423 len[3] = secret_len;
424 md5_vector(4, addr, len, msg->hdr->authenticator);
425
426 if (wpabuf_len(msg->buf) > 0xffff) {
427 wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
428 (unsigned long) wpabuf_len(msg->buf));
429 return -1;
430 }
431 return 0;
432}
433
434
Dmitry Shmidt04949592012-07-19 12:16:46 -0700435int radius_msg_finish_das_resp(struct radius_msg *msg, const u8 *secret,
436 size_t secret_len,
437 const struct radius_hdr *req_hdr)
438{
439 const u8 *addr[2];
440 size_t len[2];
441 u8 auth[MD5_MAC_LEN];
442 struct radius_attr_hdr *attr;
443
444 os_memset(auth, 0, MD5_MAC_LEN);
445 attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
446 auth, MD5_MAC_LEN);
447 if (attr == NULL) {
448 wpa_printf(MSG_WARNING, "Could not add Message-Authenticator");
449 return -1;
450 }
451
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700452 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700453 os_memcpy(msg->hdr->authenticator, req_hdr->authenticator, 16);
454 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
455 wpabuf_len(msg->buf), (u8 *) (attr + 1));
456
457 /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
458 addr[0] = wpabuf_head_u8(msg->buf);
459 len[0] = wpabuf_len(msg->buf);
460 addr[1] = secret;
461 len[1] = secret_len;
462 if (md5_vector(2, addr, len, msg->hdr->authenticator) < 0)
463 return -1;
464
465 if (wpabuf_len(msg->buf) > 0xffff) {
466 wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
467 (unsigned long) wpabuf_len(msg->buf));
468 return -1;
469 }
470 return 0;
471}
472
473
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700474void radius_msg_finish_acct(struct radius_msg *msg, const u8 *secret,
475 size_t secret_len)
476{
477 const u8 *addr[2];
478 size_t len[2];
479
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700480 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700481 os_memset(msg->hdr->authenticator, 0, MD5_MAC_LEN);
482 addr[0] = wpabuf_head(msg->buf);
483 len[0] = wpabuf_len(msg->buf);
484 addr[1] = secret;
485 len[1] = secret_len;
486 md5_vector(2, addr, len, msg->hdr->authenticator);
487
488 if (wpabuf_len(msg->buf) > 0xffff) {
489 wpa_printf(MSG_WARNING, "RADIUS: Too long messages (%lu)",
490 (unsigned long) wpabuf_len(msg->buf));
491 }
492}
493
494
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800495void radius_msg_finish_acct_resp(struct radius_msg *msg, const u8 *secret,
496 size_t secret_len, const u8 *req_authenticator)
497{
498 const u8 *addr[2];
499 size_t len[2];
500
501 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
502 os_memcpy(msg->hdr->authenticator, req_authenticator, MD5_MAC_LEN);
503 addr[0] = wpabuf_head(msg->buf);
504 len[0] = wpabuf_len(msg->buf);
505 addr[1] = secret;
506 len[1] = secret_len;
507 md5_vector(2, addr, len, msg->hdr->authenticator);
508
509 if (wpabuf_len(msg->buf) > 0xffff) {
510 wpa_printf(MSG_WARNING, "RADIUS: Too long messages (%lu)",
511 (unsigned long) wpabuf_len(msg->buf));
512 }
513}
514
515
Dmitry Shmidt04949592012-07-19 12:16:46 -0700516int radius_msg_verify_acct_req(struct radius_msg *msg, const u8 *secret,
517 size_t secret_len)
518{
519 const u8 *addr[4];
520 size_t len[4];
521 u8 zero[MD5_MAC_LEN];
522 u8 hash[MD5_MAC_LEN];
523
524 os_memset(zero, 0, sizeof(zero));
525 addr[0] = (u8 *) msg->hdr;
526 len[0] = sizeof(struct radius_hdr) - MD5_MAC_LEN;
527 addr[1] = zero;
528 len[1] = MD5_MAC_LEN;
529 addr[2] = (u8 *) (msg->hdr + 1);
530 len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
531 addr[3] = secret;
532 len[3] = secret_len;
533 md5_vector(4, addr, len, hash);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700534 return os_memcmp_const(msg->hdr->authenticator, hash, MD5_MAC_LEN) != 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700535}
536
537
538int radius_msg_verify_das_req(struct radius_msg *msg, const u8 *secret,
539 size_t secret_len)
540{
541 const u8 *addr[4];
542 size_t len[4];
543 u8 zero[MD5_MAC_LEN];
544 u8 hash[MD5_MAC_LEN];
545 u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
546 u8 orig_authenticator[16];
547
548 struct radius_attr_hdr *attr = NULL, *tmp;
549 size_t i;
550
551 os_memset(zero, 0, sizeof(zero));
552 addr[0] = (u8 *) msg->hdr;
553 len[0] = sizeof(struct radius_hdr) - MD5_MAC_LEN;
554 addr[1] = zero;
555 len[1] = MD5_MAC_LEN;
556 addr[2] = (u8 *) (msg->hdr + 1);
557 len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
558 addr[3] = secret;
559 len[3] = secret_len;
560 md5_vector(4, addr, len, hash);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700561 if (os_memcmp_const(msg->hdr->authenticator, hash, MD5_MAC_LEN) != 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700562 return 1;
563
564 for (i = 0; i < msg->attr_used; i++) {
565 tmp = radius_get_attr_hdr(msg, i);
566 if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
567 if (attr != NULL) {
568 wpa_printf(MSG_WARNING, "Multiple "
569 "Message-Authenticator attributes "
570 "in RADIUS message");
571 return 1;
572 }
573 attr = tmp;
574 }
575 }
576
577 if (attr == NULL) {
578 /* Message-Authenticator is MAY; not required */
579 return 0;
580 }
581
582 os_memcpy(orig, attr + 1, MD5_MAC_LEN);
583 os_memset(attr + 1, 0, MD5_MAC_LEN);
584 os_memcpy(orig_authenticator, msg->hdr->authenticator,
585 sizeof(orig_authenticator));
586 os_memset(msg->hdr->authenticator, 0,
587 sizeof(msg->hdr->authenticator));
588 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
589 wpabuf_len(msg->buf), auth);
590 os_memcpy(attr + 1, orig, MD5_MAC_LEN);
591 os_memcpy(msg->hdr->authenticator, orig_authenticator,
592 sizeof(orig_authenticator));
593
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700594 return os_memcmp_const(orig, auth, MD5_MAC_LEN) != 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700595}
596
597
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700598static int radius_msg_add_attr_to_array(struct radius_msg *msg,
599 struct radius_attr_hdr *attr)
600{
601 if (msg->attr_used >= msg->attr_size) {
602 size_t *nattr_pos;
603 int nlen = msg->attr_size * 2;
604
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700605 nattr_pos = os_realloc_array(msg->attr_pos, nlen,
606 sizeof(*msg->attr_pos));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700607 if (nattr_pos == NULL)
608 return -1;
609
610 msg->attr_pos = nattr_pos;
611 msg->attr_size = nlen;
612 }
613
614 msg->attr_pos[msg->attr_used++] =
615 (unsigned char *) attr - wpabuf_head_u8(msg->buf);
616
617 return 0;
618}
619
620
621struct radius_attr_hdr *radius_msg_add_attr(struct radius_msg *msg, u8 type,
622 const u8 *data, size_t data_len)
623{
624 size_t buf_needed;
625 struct radius_attr_hdr *attr;
626
627 if (data_len > RADIUS_MAX_ATTR_LEN) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800628 wpa_printf(MSG_ERROR, "radius_msg_add_attr: too long attribute (%lu bytes)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700629 (unsigned long) data_len);
630 return NULL;
631 }
632
633 buf_needed = sizeof(*attr) + data_len;
634
635 if (wpabuf_tailroom(msg->buf) < buf_needed) {
636 /* allocate more space for message buffer */
637 if (wpabuf_resize(&msg->buf, buf_needed) < 0)
638 return NULL;
639 msg->hdr = wpabuf_mhead(msg->buf);
640 }
641
642 attr = wpabuf_put(msg->buf, sizeof(struct radius_attr_hdr));
643 attr->type = type;
644 attr->length = sizeof(*attr) + data_len;
645 wpabuf_put_data(msg->buf, data, data_len);
646
647 if (radius_msg_add_attr_to_array(msg, attr))
648 return NULL;
649
650 return attr;
651}
652
653
654/**
655 * radius_msg_parse - Parse a RADIUS message
656 * @data: RADIUS message to be parsed
657 * @len: Length of data buffer in octets
658 * Returns: Parsed RADIUS message or %NULL on failure
659 *
660 * This parses a RADIUS message and makes a copy of its data. The caller is
661 * responsible for freeing the returned data with radius_msg_free().
662 */
663struct radius_msg * radius_msg_parse(const u8 *data, size_t len)
664{
665 struct radius_msg *msg;
666 struct radius_hdr *hdr;
667 struct radius_attr_hdr *attr;
668 size_t msg_len;
669 unsigned char *pos, *end;
670
671 if (data == NULL || len < sizeof(*hdr))
672 return NULL;
673
674 hdr = (struct radius_hdr *) data;
675
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700676 msg_len = be_to_host16(hdr->length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700677 if (msg_len < sizeof(*hdr) || msg_len > len) {
678 wpa_printf(MSG_INFO, "RADIUS: Invalid message length");
679 return NULL;
680 }
681
682 if (msg_len < len) {
683 wpa_printf(MSG_DEBUG, "RADIUS: Ignored %lu extra bytes after "
684 "RADIUS message", (unsigned long) len - msg_len);
685 }
686
687 msg = os_zalloc(sizeof(*msg));
688 if (msg == NULL)
689 return NULL;
690
691 msg->buf = wpabuf_alloc_copy(data, msg_len);
692 if (msg->buf == NULL || radius_msg_initialize(msg)) {
693 radius_msg_free(msg);
694 return NULL;
695 }
696 msg->hdr = wpabuf_mhead(msg->buf);
697
698 /* parse attributes */
699 pos = wpabuf_mhead_u8(msg->buf) + sizeof(struct radius_hdr);
700 end = wpabuf_mhead_u8(msg->buf) + wpabuf_len(msg->buf);
701 while (pos < end) {
702 if ((size_t) (end - pos) < sizeof(*attr))
703 goto fail;
704
705 attr = (struct radius_attr_hdr *) pos;
706
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800707 if (attr->length > end - pos || attr->length < sizeof(*attr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700708 goto fail;
709
710 /* TODO: check that attr->length is suitable for attr->type */
711
712 if (radius_msg_add_attr_to_array(msg, attr))
713 goto fail;
714
715 pos += attr->length;
716 }
717
718 return msg;
719
720 fail:
721 radius_msg_free(msg);
722 return NULL;
723}
724
725
726int radius_msg_add_eap(struct radius_msg *msg, const u8 *data, size_t data_len)
727{
728 const u8 *pos = data;
729 size_t left = data_len;
730
731 while (left > 0) {
732 int len;
733 if (left > RADIUS_MAX_ATTR_LEN)
734 len = RADIUS_MAX_ATTR_LEN;
735 else
736 len = left;
737
738 if (!radius_msg_add_attr(msg, RADIUS_ATTR_EAP_MESSAGE,
739 pos, len))
740 return 0;
741
742 pos += len;
743 left -= len;
744 }
745
746 return 1;
747}
748
749
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700750struct wpabuf * radius_msg_get_eap(struct radius_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700751{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700752 struct wpabuf *eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700753 size_t len, i;
754 struct radius_attr_hdr *attr;
755
756 if (msg == NULL)
757 return NULL;
758
759 len = 0;
760 for (i = 0; i < msg->attr_used; i++) {
761 attr = radius_get_attr_hdr(msg, i);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700762 if (attr->type == RADIUS_ATTR_EAP_MESSAGE &&
763 attr->length > sizeof(struct radius_attr_hdr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700764 len += attr->length - sizeof(struct radius_attr_hdr);
765 }
766
767 if (len == 0)
768 return NULL;
769
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700770 eap = wpabuf_alloc(len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771 if (eap == NULL)
772 return NULL;
773
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700774 for (i = 0; i < msg->attr_used; i++) {
775 attr = radius_get_attr_hdr(msg, i);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700776 if (attr->type == RADIUS_ATTR_EAP_MESSAGE &&
777 attr->length > sizeof(struct radius_attr_hdr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700778 int flen = attr->length - sizeof(*attr);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700779 wpabuf_put_data(eap, attr + 1, flen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780 }
781 }
782
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700783 return eap;
784}
785
786
787int radius_msg_verify_msg_auth(struct radius_msg *msg, const u8 *secret,
788 size_t secret_len, const u8 *req_auth)
789{
790 u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
791 u8 orig_authenticator[16];
792 struct radius_attr_hdr *attr = NULL, *tmp;
793 size_t i;
794
795 for (i = 0; i < msg->attr_used; i++) {
796 tmp = radius_get_attr_hdr(msg, i);
797 if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
798 if (attr != NULL) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800799 wpa_printf(MSG_INFO, "Multiple Message-Authenticator attributes in RADIUS message");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800 return 1;
801 }
802 attr = tmp;
803 }
804 }
805
806 if (attr == NULL) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800807 wpa_printf(MSG_INFO, "No Message-Authenticator attribute found");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808 return 1;
809 }
810
811 os_memcpy(orig, attr + 1, MD5_MAC_LEN);
812 os_memset(attr + 1, 0, MD5_MAC_LEN);
813 if (req_auth) {
814 os_memcpy(orig_authenticator, msg->hdr->authenticator,
815 sizeof(orig_authenticator));
816 os_memcpy(msg->hdr->authenticator, req_auth,
817 sizeof(msg->hdr->authenticator));
818 }
819 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
820 wpabuf_len(msg->buf), auth);
821 os_memcpy(attr + 1, orig, MD5_MAC_LEN);
822 if (req_auth) {
823 os_memcpy(msg->hdr->authenticator, orig_authenticator,
824 sizeof(orig_authenticator));
825 }
826
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700827 if (os_memcmp_const(orig, auth, MD5_MAC_LEN) != 0) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800828 wpa_printf(MSG_INFO, "Invalid Message-Authenticator!");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700829 return 1;
830 }
831
832 return 0;
833}
834
835
836int radius_msg_verify(struct radius_msg *msg, const u8 *secret,
837 size_t secret_len, struct radius_msg *sent_msg, int auth)
838{
839 const u8 *addr[4];
840 size_t len[4];
841 u8 hash[MD5_MAC_LEN];
842
843 if (sent_msg == NULL) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800844 wpa_printf(MSG_INFO, "No matching Access-Request message found");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700845 return 1;
846 }
847
848 if (auth &&
849 radius_msg_verify_msg_auth(msg, secret, secret_len,
850 sent_msg->hdr->authenticator)) {
851 return 1;
852 }
853
854 /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
855 addr[0] = (u8 *) msg->hdr;
856 len[0] = 1 + 1 + 2;
857 addr[1] = sent_msg->hdr->authenticator;
858 len[1] = MD5_MAC_LEN;
859 addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
860 len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
861 addr[3] = secret;
862 len[3] = secret_len;
863 md5_vector(4, addr, len, hash);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700864 if (os_memcmp_const(hash, msg->hdr->authenticator, MD5_MAC_LEN) != 0) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800865 wpa_printf(MSG_INFO, "Response Authenticator invalid!");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866 return 1;
867 }
868
869 return 0;
870}
871
872
873int radius_msg_copy_attr(struct radius_msg *dst, struct radius_msg *src,
874 u8 type)
875{
876 struct radius_attr_hdr *attr;
877 size_t i;
878 int count = 0;
879
880 for (i = 0; i < src->attr_used; i++) {
881 attr = radius_get_attr_hdr(src, i);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700882 if (attr->type == type && attr->length >= sizeof(*attr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883 if (!radius_msg_add_attr(dst, type, (u8 *) (attr + 1),
884 attr->length - sizeof(*attr)))
885 return -1;
886 count++;
887 }
888 }
889
890 return count;
891}
892
893
894/* Create Request Authenticator. The value should be unique over the lifetime
895 * of the shared secret between authenticator and authentication server.
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800896 */
897int radius_msg_make_authenticator(struct radius_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700898{
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800899 return os_get_random((u8 *) &msg->hdr->authenticator,
900 sizeof(msg->hdr->authenticator));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700901}
902
903
904/* Get Vendor-specific RADIUS Attribute from a parsed RADIUS message.
905 * Returns the Attribute payload and sets alen to indicate the length of the
906 * payload if a vendor attribute with subtype is found, otherwise returns NULL.
907 * The returned payload is allocated with os_malloc() and caller must free it
908 * by calling os_free().
909 */
910static u8 *radius_msg_get_vendor_attr(struct radius_msg *msg, u32 vendor,
911 u8 subtype, size_t *alen)
912{
913 u8 *data, *pos;
914 size_t i, len;
915
916 if (msg == NULL)
917 return NULL;
918
919 for (i = 0; i < msg->attr_used; i++) {
920 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
921 size_t left;
922 u32 vendor_id;
923 struct radius_attr_vendor *vhdr;
924
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700925 if (attr->type != RADIUS_ATTR_VENDOR_SPECIFIC ||
926 attr->length < sizeof(*attr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700927 continue;
928
929 left = attr->length - sizeof(*attr);
930 if (left < 4)
931 continue;
932
933 pos = (u8 *) (attr + 1);
934
935 os_memcpy(&vendor_id, pos, 4);
936 pos += 4;
937 left -= 4;
938
939 if (ntohl(vendor_id) != vendor)
940 continue;
941
942 while (left >= sizeof(*vhdr)) {
943 vhdr = (struct radius_attr_vendor *) pos;
944 if (vhdr->vendor_length > left ||
945 vhdr->vendor_length < sizeof(*vhdr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700946 break;
947 }
948 if (vhdr->vendor_type != subtype) {
949 pos += vhdr->vendor_length;
950 left -= vhdr->vendor_length;
951 continue;
952 }
953
954 len = vhdr->vendor_length - sizeof(*vhdr);
955 data = os_malloc(len);
956 if (data == NULL)
957 return NULL;
958 os_memcpy(data, pos + sizeof(*vhdr), len);
959 if (alen)
960 *alen = len;
961 return data;
962 }
963 }
964
965 return NULL;
966}
967
968
969static u8 * decrypt_ms_key(const u8 *key, size_t len,
970 const u8 *req_authenticator,
971 const u8 *secret, size_t secret_len, size_t *reslen)
972{
973 u8 *plain, *ppos, *res;
974 const u8 *pos;
975 size_t left, plen;
976 u8 hash[MD5_MAC_LEN];
977 int i, first = 1;
978 const u8 *addr[3];
979 size_t elen[3];
980
981 /* key: 16-bit salt followed by encrypted key info */
982
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800983 if (len < 2 + 16) {
984 wpa_printf(MSG_DEBUG, "RADIUS: %s: Len is too small: %d",
985 __func__, (int) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986 return NULL;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800987 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700988
989 pos = key + 2;
990 left = len - 2;
991 if (left % 16) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800992 wpa_printf(MSG_INFO, "RADIUS: Invalid ms key len %lu",
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800993 (unsigned long) left);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994 return NULL;
995 }
996
997 plen = left;
998 ppos = plain = os_malloc(plen);
999 if (plain == NULL)
1000 return NULL;
1001 plain[0] = 0;
1002
1003 while (left > 0) {
1004 /* b(1) = MD5(Secret + Request-Authenticator + Salt)
1005 * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
1006
1007 addr[0] = secret;
1008 elen[0] = secret_len;
1009 if (first) {
1010 addr[1] = req_authenticator;
1011 elen[1] = MD5_MAC_LEN;
1012 addr[2] = key;
1013 elen[2] = 2; /* Salt */
1014 } else {
1015 addr[1] = pos - MD5_MAC_LEN;
1016 elen[1] = MD5_MAC_LEN;
1017 }
1018 md5_vector(first ? 3 : 2, addr, elen, hash);
1019 first = 0;
1020
1021 for (i = 0; i < MD5_MAC_LEN; i++)
1022 *ppos++ = *pos++ ^ hash[i];
1023 left -= MD5_MAC_LEN;
1024 }
1025
1026 if (plain[0] == 0 || plain[0] > plen - 1) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001027 wpa_printf(MSG_INFO, "RADIUS: Failed to decrypt MPPE key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001028 os_free(plain);
1029 return NULL;
1030 }
1031
1032 res = os_malloc(plain[0]);
1033 if (res == NULL) {
1034 os_free(plain);
1035 return NULL;
1036 }
1037 os_memcpy(res, plain + 1, plain[0]);
1038 if (reslen)
1039 *reslen = plain[0];
1040 os_free(plain);
1041 return res;
1042}
1043
1044
1045static void encrypt_ms_key(const u8 *key, size_t key_len, u16 salt,
1046 const u8 *req_authenticator,
1047 const u8 *secret, size_t secret_len,
1048 u8 *ebuf, size_t *elen)
1049{
1050 int i, len, first = 1;
1051 u8 hash[MD5_MAC_LEN], saltbuf[2], *pos;
1052 const u8 *addr[3];
1053 size_t _len[3];
1054
1055 WPA_PUT_BE16(saltbuf, salt);
1056
1057 len = 1 + key_len;
1058 if (len & 0x0f) {
1059 len = (len & 0xf0) + 16;
1060 }
1061 os_memset(ebuf, 0, len);
1062 ebuf[0] = key_len;
1063 os_memcpy(ebuf + 1, key, key_len);
1064
1065 *elen = len;
1066
1067 pos = ebuf;
1068 while (len > 0) {
1069 /* b(1) = MD5(Secret + Request-Authenticator + Salt)
1070 * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
1071 addr[0] = secret;
1072 _len[0] = secret_len;
1073 if (first) {
1074 addr[1] = req_authenticator;
1075 _len[1] = MD5_MAC_LEN;
1076 addr[2] = saltbuf;
1077 _len[2] = sizeof(saltbuf);
1078 } else {
1079 addr[1] = pos - MD5_MAC_LEN;
1080 _len[1] = MD5_MAC_LEN;
1081 }
1082 md5_vector(first ? 3 : 2, addr, _len, hash);
1083 first = 0;
1084
1085 for (i = 0; i < MD5_MAC_LEN; i++)
1086 *pos++ ^= hash[i];
1087
1088 len -= MD5_MAC_LEN;
1089 }
1090}
1091
1092
1093struct radius_ms_mppe_keys *
1094radius_msg_get_ms_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
1095 const u8 *secret, size_t secret_len)
1096{
1097 u8 *key;
1098 size_t keylen;
1099 struct radius_ms_mppe_keys *keys;
1100
1101 if (msg == NULL || sent_msg == NULL)
1102 return NULL;
1103
1104 keys = os_zalloc(sizeof(*keys));
1105 if (keys == NULL)
1106 return NULL;
1107
1108 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
1109 RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY,
1110 &keylen);
1111 if (key) {
1112 keys->send = decrypt_ms_key(key, keylen,
1113 sent_msg->hdr->authenticator,
1114 secret, secret_len,
1115 &keys->send_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001116 if (!keys->send) {
1117 wpa_printf(MSG_DEBUG,
1118 "RADIUS: Failed to decrypt send key");
1119 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001120 os_free(key);
1121 }
1122
1123 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
1124 RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY,
1125 &keylen);
1126 if (key) {
1127 keys->recv = decrypt_ms_key(key, keylen,
1128 sent_msg->hdr->authenticator,
1129 secret, secret_len,
1130 &keys->recv_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001131 if (!keys->recv) {
1132 wpa_printf(MSG_DEBUG,
1133 "RADIUS: Failed to decrypt recv key");
1134 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001135 os_free(key);
1136 }
1137
1138 return keys;
1139}
1140
1141
1142struct radius_ms_mppe_keys *
1143radius_msg_get_cisco_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
1144 const u8 *secret, size_t secret_len)
1145{
1146 u8 *key;
1147 size_t keylen;
1148 struct radius_ms_mppe_keys *keys;
1149
1150 if (msg == NULL || sent_msg == NULL)
1151 return NULL;
1152
1153 keys = os_zalloc(sizeof(*keys));
1154 if (keys == NULL)
1155 return NULL;
1156
1157 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_CISCO,
1158 RADIUS_CISCO_AV_PAIR, &keylen);
1159 if (key && keylen == 51 &&
1160 os_memcmp(key, "leap:session-key=", 17) == 0) {
1161 keys->recv = decrypt_ms_key(key + 17, keylen - 17,
1162 sent_msg->hdr->authenticator,
1163 secret, secret_len,
1164 &keys->recv_len);
1165 }
1166 os_free(key);
1167
1168 return keys;
1169}
1170
1171
1172int radius_msg_add_mppe_keys(struct radius_msg *msg,
1173 const u8 *req_authenticator,
1174 const u8 *secret, size_t secret_len,
1175 const u8 *send_key, size_t send_key_len,
1176 const u8 *recv_key, size_t recv_key_len)
1177{
1178 struct radius_attr_hdr *attr;
1179 u32 vendor_id = htonl(RADIUS_VENDOR_ID_MICROSOFT);
1180 u8 *buf;
1181 struct radius_attr_vendor *vhdr;
1182 u8 *pos;
1183 size_t elen;
1184 int hlen;
1185 u16 salt;
1186
1187 hlen = sizeof(vendor_id) + sizeof(*vhdr) + 2;
1188
1189 /* MS-MPPE-Send-Key */
1190 buf = os_malloc(hlen + send_key_len + 16);
1191 if (buf == NULL) {
1192 return 0;
1193 }
1194 pos = buf;
1195 os_memcpy(pos, &vendor_id, sizeof(vendor_id));
1196 pos += sizeof(vendor_id);
1197 vhdr = (struct radius_attr_vendor *) pos;
1198 vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY;
1199 pos = (u8 *) (vhdr + 1);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001200 if (os_get_random((u8 *) &salt, sizeof(salt)) < 0)
1201 return 0;
1202 salt |= 0x8000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001203 WPA_PUT_BE16(pos, salt);
1204 pos += 2;
1205 encrypt_ms_key(send_key, send_key_len, salt, req_authenticator, secret,
1206 secret_len, pos, &elen);
1207 vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
1208
1209 attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1210 buf, hlen + elen);
1211 os_free(buf);
1212 if (attr == NULL) {
1213 return 0;
1214 }
1215
1216 /* MS-MPPE-Recv-Key */
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -07001217 buf = os_malloc(hlen + recv_key_len + 16);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218 if (buf == NULL) {
1219 return 0;
1220 }
1221 pos = buf;
1222 os_memcpy(pos, &vendor_id, sizeof(vendor_id));
1223 pos += sizeof(vendor_id);
1224 vhdr = (struct radius_attr_vendor *) pos;
1225 vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY;
1226 pos = (u8 *) (vhdr + 1);
1227 salt ^= 1;
1228 WPA_PUT_BE16(pos, salt);
1229 pos += 2;
1230 encrypt_ms_key(recv_key, recv_key_len, salt, req_authenticator, secret,
1231 secret_len, pos, &elen);
1232 vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
1233
1234 attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1235 buf, hlen + elen);
1236 os_free(buf);
1237 if (attr == NULL) {
1238 return 0;
1239 }
1240
1241 return 1;
1242}
1243
1244
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001245int radius_msg_add_wfa(struct radius_msg *msg, u8 subtype, const u8 *data,
1246 size_t len)
1247{
1248 struct radius_attr_hdr *attr;
1249 u8 *buf, *pos;
1250 size_t alen;
1251
1252 alen = 4 + 2 + len;
1253 buf = os_malloc(alen);
1254 if (buf == NULL)
1255 return 0;
1256 pos = buf;
1257 WPA_PUT_BE32(pos, RADIUS_VENDOR_ID_WFA);
1258 pos += 4;
1259 *pos++ = subtype;
1260 *pos++ = 2 + len;
1261 os_memcpy(pos, data, len);
1262 attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1263 buf, alen);
1264 os_free(buf);
1265 if (attr == NULL)
1266 return 0;
1267
1268 return 1;
1269}
1270
1271
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001272int radius_user_password_hide(struct radius_msg *msg,
1273 const u8 *data, size_t data_len,
1274 const u8 *secret, size_t secret_len,
1275 u8 *buf, size_t buf_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001276{
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001277 size_t padlen, i, pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001278 const u8 *addr[2];
1279 size_t len[2];
1280 u8 hash[16];
1281
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001282 if (data_len + 16 > buf_len)
1283 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001284
1285 os_memcpy(buf, data, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001286
1287 padlen = data_len % 16;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001288 if (padlen && data_len < buf_len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001289 padlen = 16 - padlen;
1290 os_memset(buf + data_len, 0, padlen);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001291 buf_len = data_len + padlen;
1292 } else {
1293 buf_len = data_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001294 }
1295
1296 addr[0] = secret;
1297 len[0] = secret_len;
1298 addr[1] = msg->hdr->authenticator;
1299 len[1] = 16;
1300 md5_vector(2, addr, len, hash);
1301
1302 for (i = 0; i < 16; i++)
1303 buf[i] ^= hash[i];
1304 pos = 16;
1305
1306 while (pos < buf_len) {
1307 addr[0] = secret;
1308 len[0] = secret_len;
1309 addr[1] = &buf[pos - 16];
1310 len[1] = 16;
1311 md5_vector(2, addr, len, hash);
1312
1313 for (i = 0; i < 16; i++)
1314 buf[pos + i] ^= hash[i];
1315
1316 pos += 16;
1317 }
1318
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001319 return buf_len;
1320}
1321
1322
1323/* Add User-Password attribute to a RADIUS message and encrypt it as specified
1324 * in RFC 2865, Chap. 5.2 */
1325struct radius_attr_hdr *
1326radius_msg_add_attr_user_password(struct radius_msg *msg,
1327 const u8 *data, size_t data_len,
1328 const u8 *secret, size_t secret_len)
1329{
1330 u8 buf[128];
1331 int res;
1332
1333 res = radius_user_password_hide(msg, data, data_len,
1334 secret, secret_len, buf, sizeof(buf));
1335 if (res < 0)
1336 return NULL;
1337
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001338 return radius_msg_add_attr(msg, RADIUS_ATTR_USER_PASSWORD,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001339 buf, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001340}
1341
1342
1343int radius_msg_get_attr(struct radius_msg *msg, u8 type, u8 *buf, size_t len)
1344{
1345 struct radius_attr_hdr *attr = NULL, *tmp;
1346 size_t i, dlen;
1347
1348 for (i = 0; i < msg->attr_used; i++) {
1349 tmp = radius_get_attr_hdr(msg, i);
1350 if (tmp->type == type) {
1351 attr = tmp;
1352 break;
1353 }
1354 }
1355
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001356 if (!attr || attr->length < sizeof(*attr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001357 return -1;
1358
1359 dlen = attr->length - sizeof(*attr);
1360 if (buf)
1361 os_memcpy(buf, (attr + 1), dlen > len ? len : dlen);
1362 return dlen;
1363}
1364
1365
1366int radius_msg_get_attr_ptr(struct radius_msg *msg, u8 type, u8 **buf,
1367 size_t *len, const u8 *start)
1368{
1369 size_t i;
1370 struct radius_attr_hdr *attr = NULL, *tmp;
1371
1372 for (i = 0; i < msg->attr_used; i++) {
1373 tmp = radius_get_attr_hdr(msg, i);
1374 if (tmp->type == type &&
1375 (start == NULL || (u8 *) tmp > start)) {
1376 attr = tmp;
1377 break;
1378 }
1379 }
1380
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001381 if (!attr || attr->length < sizeof(*attr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001382 return -1;
1383
1384 *buf = (u8 *) (attr + 1);
1385 *len = attr->length - sizeof(*attr);
1386 return 0;
1387}
1388
1389
1390int radius_msg_count_attr(struct radius_msg *msg, u8 type, int min_len)
1391{
1392 size_t i;
1393 int count;
1394
1395 for (count = 0, i = 0; i < msg->attr_used; i++) {
1396 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
1397 if (attr->type == type &&
1398 attr->length >= sizeof(struct radius_attr_hdr) + min_len)
1399 count++;
1400 }
1401
1402 return count;
1403}
1404
1405
1406struct radius_tunnel_attrs {
1407 int tag_used;
1408 int type; /* Tunnel-Type */
1409 int medium_type; /* Tunnel-Medium-Type */
1410 int vlanid;
1411};
1412
1413
1414/**
1415 * radius_msg_get_vlanid - Parse RADIUS attributes for VLAN tunnel information
1416 * @msg: RADIUS message
Dmitry Shmidt83474442015-04-15 13:47:09 -07001417 * Returns: VLAN ID for the first tunnel configuration or 0 if none is found
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001418 */
1419int radius_msg_get_vlanid(struct radius_msg *msg)
1420{
1421 struct radius_tunnel_attrs tunnel[RADIUS_TUNNEL_TAGS], *tun;
1422 size_t i;
1423 struct radius_attr_hdr *attr = NULL;
1424 const u8 *data;
1425 char buf[10];
1426 size_t dlen;
1427
1428 os_memset(&tunnel, 0, sizeof(tunnel));
1429
1430 for (i = 0; i < msg->attr_used; i++) {
1431 attr = radius_get_attr_hdr(msg, i);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001432 if (attr->length < sizeof(*attr))
1433 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001434 data = (const u8 *) (attr + 1);
1435 dlen = attr->length - sizeof(*attr);
1436 if (attr->length < 3)
1437 continue;
1438 if (data[0] >= RADIUS_TUNNEL_TAGS)
1439 tun = &tunnel[0];
1440 else
1441 tun = &tunnel[data[0]];
1442
1443 switch (attr->type) {
1444 case RADIUS_ATTR_TUNNEL_TYPE:
1445 if (attr->length != 6)
1446 break;
1447 tun->tag_used++;
1448 tun->type = WPA_GET_BE24(data + 1);
1449 break;
1450 case RADIUS_ATTR_TUNNEL_MEDIUM_TYPE:
1451 if (attr->length != 6)
1452 break;
1453 tun->tag_used++;
1454 tun->medium_type = WPA_GET_BE24(data + 1);
1455 break;
1456 case RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID:
1457 if (data[0] < RADIUS_TUNNEL_TAGS) {
1458 data++;
1459 dlen--;
1460 }
1461 if (dlen >= sizeof(buf))
1462 break;
1463 os_memcpy(buf, data, dlen);
1464 buf[dlen] = '\0';
1465 tun->tag_used++;
1466 tun->vlanid = atoi(buf);
1467 break;
1468 }
1469 }
1470
1471 for (i = 0; i < RADIUS_TUNNEL_TAGS; i++) {
1472 tun = &tunnel[i];
1473 if (tun->tag_used &&
1474 tun->type == RADIUS_TUNNEL_TYPE_VLAN &&
1475 tun->medium_type == RADIUS_TUNNEL_MEDIUM_TYPE_802 &&
1476 tun->vlanid > 0)
1477 return tun->vlanid;
1478 }
1479
Dmitry Shmidt83474442015-04-15 13:47:09 -07001480 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001481}
1482
1483
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001484/**
1485 * radius_msg_get_tunnel_password - Parse RADIUS attribute Tunnel-Password
1486 * @msg: Received RADIUS message
1487 * @keylen: Length of returned password
1488 * @secret: RADIUS shared secret
1489 * @secret_len: Length of secret
1490 * @sent_msg: Sent RADIUS message
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001491 * @n: Number of password attribute to return (starting with 0)
1492 * Returns: Pointer to n-th password (free with os_free) or %NULL
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001493 */
1494char * radius_msg_get_tunnel_password(struct radius_msg *msg, int *keylen,
1495 const u8 *secret, size_t secret_len,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001496 struct radius_msg *sent_msg, size_t n)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001497{
1498 u8 *buf = NULL;
1499 size_t buflen;
1500 const u8 *salt;
1501 u8 *str;
1502 const u8 *addr[3];
1503 size_t len[3];
1504 u8 hash[16];
1505 u8 *pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001506 size_t i, j = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001507 struct radius_attr_hdr *attr;
1508 const u8 *data;
1509 size_t dlen;
1510 const u8 *fdata = NULL; /* points to found item */
1511 size_t fdlen = -1;
1512 char *ret = NULL;
1513
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001514 /* find n-th valid Tunnel-Password attribute */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001515 for (i = 0; i < msg->attr_used; i++) {
1516 attr = radius_get_attr_hdr(msg, i);
1517 if (attr == NULL ||
1518 attr->type != RADIUS_ATTR_TUNNEL_PASSWORD) {
1519 continue;
1520 }
1521 if (attr->length <= 5)
1522 continue;
1523 data = (const u8 *) (attr + 1);
1524 dlen = attr->length - sizeof(*attr);
1525 if (dlen <= 3 || dlen % 16 != 3)
1526 continue;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001527 j++;
1528 if (j <= n)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001529 continue;
1530
1531 fdata = data;
1532 fdlen = dlen;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001533 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001534 }
1535 if (fdata == NULL)
1536 goto out;
1537
1538 /* alloc writable memory for decryption */
1539 buf = os_malloc(fdlen);
1540 if (buf == NULL)
1541 goto out;
1542 os_memcpy(buf, fdata, fdlen);
1543 buflen = fdlen;
1544
1545 /* init pointers */
1546 salt = buf + 1;
1547 str = buf + 3;
1548
1549 /* decrypt blocks */
1550 pos = buf + buflen - 16; /* last block */
1551 while (pos >= str + 16) { /* all but the first block */
1552 addr[0] = secret;
1553 len[0] = secret_len;
1554 addr[1] = pos - 16;
1555 len[1] = 16;
1556 md5_vector(2, addr, len, hash);
1557
1558 for (i = 0; i < 16; i++)
1559 pos[i] ^= hash[i];
1560
1561 pos -= 16;
1562 }
1563
1564 /* decrypt first block */
1565 if (str != pos)
1566 goto out;
1567 addr[0] = secret;
1568 len[0] = secret_len;
1569 addr[1] = sent_msg->hdr->authenticator;
1570 len[1] = 16;
1571 addr[2] = salt;
1572 len[2] = 2;
1573 md5_vector(3, addr, len, hash);
1574
1575 for (i = 0; i < 16; i++)
1576 pos[i] ^= hash[i];
1577
1578 /* derive plaintext length from first subfield */
1579 *keylen = (unsigned char) str[0];
1580 if ((u8 *) (str + *keylen) >= (u8 *) (buf + buflen)) {
1581 /* decryption error - invalid key length */
1582 goto out;
1583 }
1584 if (*keylen == 0) {
1585 /* empty password */
1586 goto out;
1587 }
1588
1589 /* copy passphrase into new buffer */
1590 ret = os_malloc(*keylen);
1591 if (ret)
1592 os_memcpy(ret, str + 1, *keylen);
1593
1594out:
1595 /* return new buffer */
1596 os_free(buf);
1597 return ret;
1598}
1599
1600
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001601void radius_free_class(struct radius_class_data *c)
1602{
1603 size_t i;
1604 if (c == NULL)
1605 return;
1606 for (i = 0; i < c->count; i++)
1607 os_free(c->attr[i].data);
1608 os_free(c->attr);
1609 c->attr = NULL;
1610 c->count = 0;
1611}
1612
1613
1614int radius_copy_class(struct radius_class_data *dst,
1615 const struct radius_class_data *src)
1616{
1617 size_t i;
1618
1619 if (src->attr == NULL)
1620 return 0;
1621
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001622 dst->attr = os_calloc(src->count, sizeof(struct radius_attr_data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001623 if (dst->attr == NULL)
1624 return -1;
1625
1626 dst->count = 0;
1627
1628 for (i = 0; i < src->count; i++) {
1629 dst->attr[i].data = os_malloc(src->attr[i].len);
1630 if (dst->attr[i].data == NULL)
1631 break;
1632 dst->count++;
1633 os_memcpy(dst->attr[i].data, src->attr[i].data,
1634 src->attr[i].len);
1635 dst->attr[i].len = src->attr[i].len;
1636 }
1637
1638 return 0;
1639}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001640
1641
1642u8 radius_msg_find_unlisted_attr(struct radius_msg *msg, u8 *attrs)
1643{
1644 size_t i, j;
1645 struct radius_attr_hdr *attr;
1646
1647 for (i = 0; i < msg->attr_used; i++) {
1648 attr = radius_get_attr_hdr(msg, i);
1649
1650 for (j = 0; attrs[j]; j++) {
1651 if (attr->type == attrs[j])
1652 break;
1653 }
1654
1655 if (attrs[j] == 0)
1656 return attr->type; /* unlisted attr */
1657 }
1658
1659 return 0;
1660}
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001661
1662
1663int radius_gen_session_id(u8 *id, size_t len)
1664{
1665 /*
1666 * Acct-Session-Id and Acct-Multi-Session-Id should be globally and
1667 * temporarily unique. A high quality random number is required
1668 * therefore. This could be be improved by switching to a GUID.
1669 */
1670 return os_get_random(id, len);
1671}