blob: 266b29f7a0298cd7fa6a4b15c8f4c41042942737 [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.
896 * Use one-way MD5 hash calculated from current timestamp and some data given
897 * by the caller. */
898void radius_msg_make_authenticator(struct radius_msg *msg,
899 const u8 *data, size_t len)
900{
901 struct os_time tv;
902 long int l;
903 const u8 *addr[3];
904 size_t elen[3];
905
906 os_get_time(&tv);
907 l = os_random();
908 addr[0] = (u8 *) &tv;
909 elen[0] = sizeof(tv);
910 addr[1] = data;
911 elen[1] = len;
912 addr[2] = (u8 *) &l;
913 elen[2] = sizeof(l);
914 md5_vector(3, addr, elen, msg->hdr->authenticator);
915}
916
917
918/* Get Vendor-specific RADIUS Attribute from a parsed RADIUS message.
919 * Returns the Attribute payload and sets alen to indicate the length of the
920 * payload if a vendor attribute with subtype is found, otherwise returns NULL.
921 * The returned payload is allocated with os_malloc() and caller must free it
922 * by calling os_free().
923 */
924static u8 *radius_msg_get_vendor_attr(struct radius_msg *msg, u32 vendor,
925 u8 subtype, size_t *alen)
926{
927 u8 *data, *pos;
928 size_t i, len;
929
930 if (msg == NULL)
931 return NULL;
932
933 for (i = 0; i < msg->attr_used; i++) {
934 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
935 size_t left;
936 u32 vendor_id;
937 struct radius_attr_vendor *vhdr;
938
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700939 if (attr->type != RADIUS_ATTR_VENDOR_SPECIFIC ||
940 attr->length < sizeof(*attr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700941 continue;
942
943 left = attr->length - sizeof(*attr);
944 if (left < 4)
945 continue;
946
947 pos = (u8 *) (attr + 1);
948
949 os_memcpy(&vendor_id, pos, 4);
950 pos += 4;
951 left -= 4;
952
953 if (ntohl(vendor_id) != vendor)
954 continue;
955
956 while (left >= sizeof(*vhdr)) {
957 vhdr = (struct radius_attr_vendor *) pos;
958 if (vhdr->vendor_length > left ||
959 vhdr->vendor_length < sizeof(*vhdr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960 break;
961 }
962 if (vhdr->vendor_type != subtype) {
963 pos += vhdr->vendor_length;
964 left -= vhdr->vendor_length;
965 continue;
966 }
967
968 len = vhdr->vendor_length - sizeof(*vhdr);
969 data = os_malloc(len);
970 if (data == NULL)
971 return NULL;
972 os_memcpy(data, pos + sizeof(*vhdr), len);
973 if (alen)
974 *alen = len;
975 return data;
976 }
977 }
978
979 return NULL;
980}
981
982
983static u8 * decrypt_ms_key(const u8 *key, size_t len,
984 const u8 *req_authenticator,
985 const u8 *secret, size_t secret_len, size_t *reslen)
986{
987 u8 *plain, *ppos, *res;
988 const u8 *pos;
989 size_t left, plen;
990 u8 hash[MD5_MAC_LEN];
991 int i, first = 1;
992 const u8 *addr[3];
993 size_t elen[3];
994
995 /* key: 16-bit salt followed by encrypted key info */
996
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800997 if (len < 2 + 16) {
998 wpa_printf(MSG_DEBUG, "RADIUS: %s: Len is too small: %d",
999 __func__, (int) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000 return NULL;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001001 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001002
1003 pos = key + 2;
1004 left = len - 2;
1005 if (left % 16) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001006 wpa_printf(MSG_INFO, "RADIUS: Invalid ms key len %lu",
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001007 (unsigned long) left);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008 return NULL;
1009 }
1010
1011 plen = left;
1012 ppos = plain = os_malloc(plen);
1013 if (plain == NULL)
1014 return NULL;
1015 plain[0] = 0;
1016
1017 while (left > 0) {
1018 /* b(1) = MD5(Secret + Request-Authenticator + Salt)
1019 * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
1020
1021 addr[0] = secret;
1022 elen[0] = secret_len;
1023 if (first) {
1024 addr[1] = req_authenticator;
1025 elen[1] = MD5_MAC_LEN;
1026 addr[2] = key;
1027 elen[2] = 2; /* Salt */
1028 } else {
1029 addr[1] = pos - MD5_MAC_LEN;
1030 elen[1] = MD5_MAC_LEN;
1031 }
1032 md5_vector(first ? 3 : 2, addr, elen, hash);
1033 first = 0;
1034
1035 for (i = 0; i < MD5_MAC_LEN; i++)
1036 *ppos++ = *pos++ ^ hash[i];
1037 left -= MD5_MAC_LEN;
1038 }
1039
1040 if (plain[0] == 0 || plain[0] > plen - 1) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001041 wpa_printf(MSG_INFO, "RADIUS: Failed to decrypt MPPE key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 os_free(plain);
1043 return NULL;
1044 }
1045
1046 res = os_malloc(plain[0]);
1047 if (res == NULL) {
1048 os_free(plain);
1049 return NULL;
1050 }
1051 os_memcpy(res, plain + 1, plain[0]);
1052 if (reslen)
1053 *reslen = plain[0];
1054 os_free(plain);
1055 return res;
1056}
1057
1058
1059static void encrypt_ms_key(const u8 *key, size_t key_len, u16 salt,
1060 const u8 *req_authenticator,
1061 const u8 *secret, size_t secret_len,
1062 u8 *ebuf, size_t *elen)
1063{
1064 int i, len, first = 1;
1065 u8 hash[MD5_MAC_LEN], saltbuf[2], *pos;
1066 const u8 *addr[3];
1067 size_t _len[3];
1068
1069 WPA_PUT_BE16(saltbuf, salt);
1070
1071 len = 1 + key_len;
1072 if (len & 0x0f) {
1073 len = (len & 0xf0) + 16;
1074 }
1075 os_memset(ebuf, 0, len);
1076 ebuf[0] = key_len;
1077 os_memcpy(ebuf + 1, key, key_len);
1078
1079 *elen = len;
1080
1081 pos = ebuf;
1082 while (len > 0) {
1083 /* b(1) = MD5(Secret + Request-Authenticator + Salt)
1084 * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
1085 addr[0] = secret;
1086 _len[0] = secret_len;
1087 if (first) {
1088 addr[1] = req_authenticator;
1089 _len[1] = MD5_MAC_LEN;
1090 addr[2] = saltbuf;
1091 _len[2] = sizeof(saltbuf);
1092 } else {
1093 addr[1] = pos - MD5_MAC_LEN;
1094 _len[1] = MD5_MAC_LEN;
1095 }
1096 md5_vector(first ? 3 : 2, addr, _len, hash);
1097 first = 0;
1098
1099 for (i = 0; i < MD5_MAC_LEN; i++)
1100 *pos++ ^= hash[i];
1101
1102 len -= MD5_MAC_LEN;
1103 }
1104}
1105
1106
1107struct radius_ms_mppe_keys *
1108radius_msg_get_ms_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
1109 const u8 *secret, size_t secret_len)
1110{
1111 u8 *key;
1112 size_t keylen;
1113 struct radius_ms_mppe_keys *keys;
1114
1115 if (msg == NULL || sent_msg == NULL)
1116 return NULL;
1117
1118 keys = os_zalloc(sizeof(*keys));
1119 if (keys == NULL)
1120 return NULL;
1121
1122 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
1123 RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY,
1124 &keylen);
1125 if (key) {
1126 keys->send = decrypt_ms_key(key, keylen,
1127 sent_msg->hdr->authenticator,
1128 secret, secret_len,
1129 &keys->send_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001130 if (!keys->send) {
1131 wpa_printf(MSG_DEBUG,
1132 "RADIUS: Failed to decrypt send key");
1133 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134 os_free(key);
1135 }
1136
1137 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
1138 RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY,
1139 &keylen);
1140 if (key) {
1141 keys->recv = decrypt_ms_key(key, keylen,
1142 sent_msg->hdr->authenticator,
1143 secret, secret_len,
1144 &keys->recv_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001145 if (!keys->recv) {
1146 wpa_printf(MSG_DEBUG,
1147 "RADIUS: Failed to decrypt recv key");
1148 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149 os_free(key);
1150 }
1151
1152 return keys;
1153}
1154
1155
1156struct radius_ms_mppe_keys *
1157radius_msg_get_cisco_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
1158 const u8 *secret, size_t secret_len)
1159{
1160 u8 *key;
1161 size_t keylen;
1162 struct radius_ms_mppe_keys *keys;
1163
1164 if (msg == NULL || sent_msg == NULL)
1165 return NULL;
1166
1167 keys = os_zalloc(sizeof(*keys));
1168 if (keys == NULL)
1169 return NULL;
1170
1171 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_CISCO,
1172 RADIUS_CISCO_AV_PAIR, &keylen);
1173 if (key && keylen == 51 &&
1174 os_memcmp(key, "leap:session-key=", 17) == 0) {
1175 keys->recv = decrypt_ms_key(key + 17, keylen - 17,
1176 sent_msg->hdr->authenticator,
1177 secret, secret_len,
1178 &keys->recv_len);
1179 }
1180 os_free(key);
1181
1182 return keys;
1183}
1184
1185
1186int radius_msg_add_mppe_keys(struct radius_msg *msg,
1187 const u8 *req_authenticator,
1188 const u8 *secret, size_t secret_len,
1189 const u8 *send_key, size_t send_key_len,
1190 const u8 *recv_key, size_t recv_key_len)
1191{
1192 struct radius_attr_hdr *attr;
1193 u32 vendor_id = htonl(RADIUS_VENDOR_ID_MICROSOFT);
1194 u8 *buf;
1195 struct radius_attr_vendor *vhdr;
1196 u8 *pos;
1197 size_t elen;
1198 int hlen;
1199 u16 salt;
1200
1201 hlen = sizeof(vendor_id) + sizeof(*vhdr) + 2;
1202
1203 /* MS-MPPE-Send-Key */
1204 buf = os_malloc(hlen + send_key_len + 16);
1205 if (buf == NULL) {
1206 return 0;
1207 }
1208 pos = buf;
1209 os_memcpy(pos, &vendor_id, sizeof(vendor_id));
1210 pos += sizeof(vendor_id);
1211 vhdr = (struct radius_attr_vendor *) pos;
1212 vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY;
1213 pos = (u8 *) (vhdr + 1);
1214 salt = os_random() | 0x8000;
1215 WPA_PUT_BE16(pos, salt);
1216 pos += 2;
1217 encrypt_ms_key(send_key, send_key_len, salt, req_authenticator, secret,
1218 secret_len, pos, &elen);
1219 vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
1220
1221 attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1222 buf, hlen + elen);
1223 os_free(buf);
1224 if (attr == NULL) {
1225 return 0;
1226 }
1227
1228 /* MS-MPPE-Recv-Key */
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -07001229 buf = os_malloc(hlen + recv_key_len + 16);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001230 if (buf == NULL) {
1231 return 0;
1232 }
1233 pos = buf;
1234 os_memcpy(pos, &vendor_id, sizeof(vendor_id));
1235 pos += sizeof(vendor_id);
1236 vhdr = (struct radius_attr_vendor *) pos;
1237 vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY;
1238 pos = (u8 *) (vhdr + 1);
1239 salt ^= 1;
1240 WPA_PUT_BE16(pos, salt);
1241 pos += 2;
1242 encrypt_ms_key(recv_key, recv_key_len, salt, req_authenticator, secret,
1243 secret_len, pos, &elen);
1244 vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
1245
1246 attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1247 buf, hlen + elen);
1248 os_free(buf);
1249 if (attr == NULL) {
1250 return 0;
1251 }
1252
1253 return 1;
1254}
1255
1256
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001257int radius_msg_add_wfa(struct radius_msg *msg, u8 subtype, const u8 *data,
1258 size_t len)
1259{
1260 struct radius_attr_hdr *attr;
1261 u8 *buf, *pos;
1262 size_t alen;
1263
1264 alen = 4 + 2 + len;
1265 buf = os_malloc(alen);
1266 if (buf == NULL)
1267 return 0;
1268 pos = buf;
1269 WPA_PUT_BE32(pos, RADIUS_VENDOR_ID_WFA);
1270 pos += 4;
1271 *pos++ = subtype;
1272 *pos++ = 2 + len;
1273 os_memcpy(pos, data, len);
1274 attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1275 buf, alen);
1276 os_free(buf);
1277 if (attr == NULL)
1278 return 0;
1279
1280 return 1;
1281}
1282
1283
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001284int radius_user_password_hide(struct radius_msg *msg,
1285 const u8 *data, size_t data_len,
1286 const u8 *secret, size_t secret_len,
1287 u8 *buf, size_t buf_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001288{
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001289 size_t padlen, i, pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001290 const u8 *addr[2];
1291 size_t len[2];
1292 u8 hash[16];
1293
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001294 if (data_len + 16 > buf_len)
1295 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001296
1297 os_memcpy(buf, data, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001298
1299 padlen = data_len % 16;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001300 if (padlen && data_len < buf_len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001301 padlen = 16 - padlen;
1302 os_memset(buf + data_len, 0, padlen);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001303 buf_len = data_len + padlen;
1304 } else {
1305 buf_len = data_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306 }
1307
1308 addr[0] = secret;
1309 len[0] = secret_len;
1310 addr[1] = msg->hdr->authenticator;
1311 len[1] = 16;
1312 md5_vector(2, addr, len, hash);
1313
1314 for (i = 0; i < 16; i++)
1315 buf[i] ^= hash[i];
1316 pos = 16;
1317
1318 while (pos < buf_len) {
1319 addr[0] = secret;
1320 len[0] = secret_len;
1321 addr[1] = &buf[pos - 16];
1322 len[1] = 16;
1323 md5_vector(2, addr, len, hash);
1324
1325 for (i = 0; i < 16; i++)
1326 buf[pos + i] ^= hash[i];
1327
1328 pos += 16;
1329 }
1330
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001331 return buf_len;
1332}
1333
1334
1335/* Add User-Password attribute to a RADIUS message and encrypt it as specified
1336 * in RFC 2865, Chap. 5.2 */
1337struct radius_attr_hdr *
1338radius_msg_add_attr_user_password(struct radius_msg *msg,
1339 const u8 *data, size_t data_len,
1340 const u8 *secret, size_t secret_len)
1341{
1342 u8 buf[128];
1343 int res;
1344
1345 res = radius_user_password_hide(msg, data, data_len,
1346 secret, secret_len, buf, sizeof(buf));
1347 if (res < 0)
1348 return NULL;
1349
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350 return radius_msg_add_attr(msg, RADIUS_ATTR_USER_PASSWORD,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001351 buf, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001352}
1353
1354
1355int radius_msg_get_attr(struct radius_msg *msg, u8 type, u8 *buf, size_t len)
1356{
1357 struct radius_attr_hdr *attr = NULL, *tmp;
1358 size_t i, dlen;
1359
1360 for (i = 0; i < msg->attr_used; i++) {
1361 tmp = radius_get_attr_hdr(msg, i);
1362 if (tmp->type == type) {
1363 attr = tmp;
1364 break;
1365 }
1366 }
1367
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001368 if (!attr || attr->length < sizeof(*attr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001369 return -1;
1370
1371 dlen = attr->length - sizeof(*attr);
1372 if (buf)
1373 os_memcpy(buf, (attr + 1), dlen > len ? len : dlen);
1374 return dlen;
1375}
1376
1377
1378int radius_msg_get_attr_ptr(struct radius_msg *msg, u8 type, u8 **buf,
1379 size_t *len, const u8 *start)
1380{
1381 size_t i;
1382 struct radius_attr_hdr *attr = NULL, *tmp;
1383
1384 for (i = 0; i < msg->attr_used; i++) {
1385 tmp = radius_get_attr_hdr(msg, i);
1386 if (tmp->type == type &&
1387 (start == NULL || (u8 *) tmp > start)) {
1388 attr = tmp;
1389 break;
1390 }
1391 }
1392
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001393 if (!attr || attr->length < sizeof(*attr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 return -1;
1395
1396 *buf = (u8 *) (attr + 1);
1397 *len = attr->length - sizeof(*attr);
1398 return 0;
1399}
1400
1401
1402int radius_msg_count_attr(struct radius_msg *msg, u8 type, int min_len)
1403{
1404 size_t i;
1405 int count;
1406
1407 for (count = 0, i = 0; i < msg->attr_used; i++) {
1408 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
1409 if (attr->type == type &&
1410 attr->length >= sizeof(struct radius_attr_hdr) + min_len)
1411 count++;
1412 }
1413
1414 return count;
1415}
1416
1417
1418struct radius_tunnel_attrs {
1419 int tag_used;
1420 int type; /* Tunnel-Type */
1421 int medium_type; /* Tunnel-Medium-Type */
1422 int vlanid;
1423};
1424
1425
1426/**
1427 * radius_msg_get_vlanid - Parse RADIUS attributes for VLAN tunnel information
1428 * @msg: RADIUS message
Dmitry Shmidt83474442015-04-15 13:47:09 -07001429 * Returns: VLAN ID for the first tunnel configuration or 0 if none is found
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430 */
1431int radius_msg_get_vlanid(struct radius_msg *msg)
1432{
1433 struct radius_tunnel_attrs tunnel[RADIUS_TUNNEL_TAGS], *tun;
1434 size_t i;
1435 struct radius_attr_hdr *attr = NULL;
1436 const u8 *data;
1437 char buf[10];
1438 size_t dlen;
1439
1440 os_memset(&tunnel, 0, sizeof(tunnel));
1441
1442 for (i = 0; i < msg->attr_used; i++) {
1443 attr = radius_get_attr_hdr(msg, i);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001444 if (attr->length < sizeof(*attr))
1445 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001446 data = (const u8 *) (attr + 1);
1447 dlen = attr->length - sizeof(*attr);
1448 if (attr->length < 3)
1449 continue;
1450 if (data[0] >= RADIUS_TUNNEL_TAGS)
1451 tun = &tunnel[0];
1452 else
1453 tun = &tunnel[data[0]];
1454
1455 switch (attr->type) {
1456 case RADIUS_ATTR_TUNNEL_TYPE:
1457 if (attr->length != 6)
1458 break;
1459 tun->tag_used++;
1460 tun->type = WPA_GET_BE24(data + 1);
1461 break;
1462 case RADIUS_ATTR_TUNNEL_MEDIUM_TYPE:
1463 if (attr->length != 6)
1464 break;
1465 tun->tag_used++;
1466 tun->medium_type = WPA_GET_BE24(data + 1);
1467 break;
1468 case RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID:
1469 if (data[0] < RADIUS_TUNNEL_TAGS) {
1470 data++;
1471 dlen--;
1472 }
1473 if (dlen >= sizeof(buf))
1474 break;
1475 os_memcpy(buf, data, dlen);
1476 buf[dlen] = '\0';
1477 tun->tag_used++;
1478 tun->vlanid = atoi(buf);
1479 break;
1480 }
1481 }
1482
1483 for (i = 0; i < RADIUS_TUNNEL_TAGS; i++) {
1484 tun = &tunnel[i];
1485 if (tun->tag_used &&
1486 tun->type == RADIUS_TUNNEL_TYPE_VLAN &&
1487 tun->medium_type == RADIUS_TUNNEL_MEDIUM_TYPE_802 &&
1488 tun->vlanid > 0)
1489 return tun->vlanid;
1490 }
1491
Dmitry Shmidt83474442015-04-15 13:47:09 -07001492 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001493}
1494
1495
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001496/**
1497 * radius_msg_get_tunnel_password - Parse RADIUS attribute Tunnel-Password
1498 * @msg: Received RADIUS message
1499 * @keylen: Length of returned password
1500 * @secret: RADIUS shared secret
1501 * @secret_len: Length of secret
1502 * @sent_msg: Sent RADIUS message
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001503 * @n: Number of password attribute to return (starting with 0)
1504 * Returns: Pointer to n-th password (free with os_free) or %NULL
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001505 */
1506char * radius_msg_get_tunnel_password(struct radius_msg *msg, int *keylen,
1507 const u8 *secret, size_t secret_len,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001508 struct radius_msg *sent_msg, size_t n)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001509{
1510 u8 *buf = NULL;
1511 size_t buflen;
1512 const u8 *salt;
1513 u8 *str;
1514 const u8 *addr[3];
1515 size_t len[3];
1516 u8 hash[16];
1517 u8 *pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001518 size_t i, j = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001519 struct radius_attr_hdr *attr;
1520 const u8 *data;
1521 size_t dlen;
1522 const u8 *fdata = NULL; /* points to found item */
1523 size_t fdlen = -1;
1524 char *ret = NULL;
1525
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001526 /* find n-th valid Tunnel-Password attribute */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001527 for (i = 0; i < msg->attr_used; i++) {
1528 attr = radius_get_attr_hdr(msg, i);
1529 if (attr == NULL ||
1530 attr->type != RADIUS_ATTR_TUNNEL_PASSWORD) {
1531 continue;
1532 }
1533 if (attr->length <= 5)
1534 continue;
1535 data = (const u8 *) (attr + 1);
1536 dlen = attr->length - sizeof(*attr);
1537 if (dlen <= 3 || dlen % 16 != 3)
1538 continue;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001539 j++;
1540 if (j <= n)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001541 continue;
1542
1543 fdata = data;
1544 fdlen = dlen;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001545 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001546 }
1547 if (fdata == NULL)
1548 goto out;
1549
1550 /* alloc writable memory for decryption */
1551 buf = os_malloc(fdlen);
1552 if (buf == NULL)
1553 goto out;
1554 os_memcpy(buf, fdata, fdlen);
1555 buflen = fdlen;
1556
1557 /* init pointers */
1558 salt = buf + 1;
1559 str = buf + 3;
1560
1561 /* decrypt blocks */
1562 pos = buf + buflen - 16; /* last block */
1563 while (pos >= str + 16) { /* all but the first block */
1564 addr[0] = secret;
1565 len[0] = secret_len;
1566 addr[1] = pos - 16;
1567 len[1] = 16;
1568 md5_vector(2, addr, len, hash);
1569
1570 for (i = 0; i < 16; i++)
1571 pos[i] ^= hash[i];
1572
1573 pos -= 16;
1574 }
1575
1576 /* decrypt first block */
1577 if (str != pos)
1578 goto out;
1579 addr[0] = secret;
1580 len[0] = secret_len;
1581 addr[1] = sent_msg->hdr->authenticator;
1582 len[1] = 16;
1583 addr[2] = salt;
1584 len[2] = 2;
1585 md5_vector(3, addr, len, hash);
1586
1587 for (i = 0; i < 16; i++)
1588 pos[i] ^= hash[i];
1589
1590 /* derive plaintext length from first subfield */
1591 *keylen = (unsigned char) str[0];
1592 if ((u8 *) (str + *keylen) >= (u8 *) (buf + buflen)) {
1593 /* decryption error - invalid key length */
1594 goto out;
1595 }
1596 if (*keylen == 0) {
1597 /* empty password */
1598 goto out;
1599 }
1600
1601 /* copy passphrase into new buffer */
1602 ret = os_malloc(*keylen);
1603 if (ret)
1604 os_memcpy(ret, str + 1, *keylen);
1605
1606out:
1607 /* return new buffer */
1608 os_free(buf);
1609 return ret;
1610}
1611
1612
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001613void radius_free_class(struct radius_class_data *c)
1614{
1615 size_t i;
1616 if (c == NULL)
1617 return;
1618 for (i = 0; i < c->count; i++)
1619 os_free(c->attr[i].data);
1620 os_free(c->attr);
1621 c->attr = NULL;
1622 c->count = 0;
1623}
1624
1625
1626int radius_copy_class(struct radius_class_data *dst,
1627 const struct radius_class_data *src)
1628{
1629 size_t i;
1630
1631 if (src->attr == NULL)
1632 return 0;
1633
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001634 dst->attr = os_calloc(src->count, sizeof(struct radius_attr_data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001635 if (dst->attr == NULL)
1636 return -1;
1637
1638 dst->count = 0;
1639
1640 for (i = 0; i < src->count; i++) {
1641 dst->attr[i].data = os_malloc(src->attr[i].len);
1642 if (dst->attr[i].data == NULL)
1643 break;
1644 dst->count++;
1645 os_memcpy(dst->attr[i].data, src->attr[i].data,
1646 src->attr[i].len);
1647 dst->attr[i].len = src->attr[i].len;
1648 }
1649
1650 return 0;
1651}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001652
1653
1654u8 radius_msg_find_unlisted_attr(struct radius_msg *msg, u8 *attrs)
1655{
1656 size_t i, j;
1657 struct radius_attr_hdr *attr;
1658
1659 for (i = 0; i < msg->attr_used; i++) {
1660 attr = radius_get_attr_hdr(msg, i);
1661
1662 for (j = 0; attrs[j]; j++) {
1663 if (attr->type == attrs[j])
1664 break;
1665 }
1666
1667 if (attrs[j] == 0)
1668 return attr->type; /* unlisted attr */
1669 }
1670
1671 return 0;
1672}