blob: 6eba2eb6c3b6fcd7a7a9903aede4c90f6f8b9b2b [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * RADIUS message processing
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003 * Copyright (c) 2002-2009, 2011-2014, 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
170static struct radius_attr_type radius_attrs[] =
171{
172 { RADIUS_ATTR_USER_NAME, "User-Name", RADIUS_ATTR_TEXT },
173 { RADIUS_ATTR_USER_PASSWORD, "User-Password", RADIUS_ATTR_UNDIST },
174 { RADIUS_ATTR_NAS_IP_ADDRESS, "NAS-IP-Address", RADIUS_ATTR_IP },
175 { RADIUS_ATTR_NAS_PORT, "NAS-Port", RADIUS_ATTR_INT32 },
176 { RADIUS_ATTR_FRAMED_MTU, "Framed-MTU", RADIUS_ATTR_INT32 },
177 { RADIUS_ATTR_REPLY_MESSAGE, "Reply-Message", RADIUS_ATTR_TEXT },
178 { RADIUS_ATTR_STATE, "State", RADIUS_ATTR_UNDIST },
179 { RADIUS_ATTR_CLASS, "Class", RADIUS_ATTR_UNDIST },
180 { RADIUS_ATTR_VENDOR_SPECIFIC, "Vendor-Specific", RADIUS_ATTR_UNDIST },
181 { RADIUS_ATTR_SESSION_TIMEOUT, "Session-Timeout", RADIUS_ATTR_INT32 },
182 { RADIUS_ATTR_IDLE_TIMEOUT, "Idle-Timeout", RADIUS_ATTR_INT32 },
183 { RADIUS_ATTR_TERMINATION_ACTION, "Termination-Action",
184 RADIUS_ATTR_INT32 },
185 { RADIUS_ATTR_CALLED_STATION_ID, "Called-Station-Id",
186 RADIUS_ATTR_TEXT },
187 { RADIUS_ATTR_CALLING_STATION_ID, "Calling-Station-Id",
188 RADIUS_ATTR_TEXT },
189 { RADIUS_ATTR_NAS_IDENTIFIER, "NAS-Identifier", RADIUS_ATTR_TEXT },
190 { RADIUS_ATTR_PROXY_STATE, "Proxy-State", RADIUS_ATTR_UNDIST },
191 { RADIUS_ATTR_ACCT_STATUS_TYPE, "Acct-Status-Type",
192 RADIUS_ATTR_INT32 },
193 { RADIUS_ATTR_ACCT_DELAY_TIME, "Acct-Delay-Time", RADIUS_ATTR_INT32 },
194 { RADIUS_ATTR_ACCT_INPUT_OCTETS, "Acct-Input-Octets",
195 RADIUS_ATTR_INT32 },
196 { RADIUS_ATTR_ACCT_OUTPUT_OCTETS, "Acct-Output-Octets",
197 RADIUS_ATTR_INT32 },
198 { RADIUS_ATTR_ACCT_SESSION_ID, "Acct-Session-Id", RADIUS_ATTR_TEXT },
199 { RADIUS_ATTR_ACCT_AUTHENTIC, "Acct-Authentic", RADIUS_ATTR_INT32 },
200 { RADIUS_ATTR_ACCT_SESSION_TIME, "Acct-Session-Time",
201 RADIUS_ATTR_INT32 },
202 { RADIUS_ATTR_ACCT_INPUT_PACKETS, "Acct-Input-Packets",
203 RADIUS_ATTR_INT32 },
204 { RADIUS_ATTR_ACCT_OUTPUT_PACKETS, "Acct-Output-Packets",
205 RADIUS_ATTR_INT32 },
206 { RADIUS_ATTR_ACCT_TERMINATE_CAUSE, "Acct-Terminate-Cause",
207 RADIUS_ATTR_INT32 },
208 { RADIUS_ATTR_ACCT_MULTI_SESSION_ID, "Acct-Multi-Session-Id",
209 RADIUS_ATTR_TEXT },
210 { RADIUS_ATTR_ACCT_LINK_COUNT, "Acct-Link-Count", RADIUS_ATTR_INT32 },
211 { RADIUS_ATTR_ACCT_INPUT_GIGAWORDS, "Acct-Input-Gigawords",
212 RADIUS_ATTR_INT32 },
213 { RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS, "Acct-Output-Gigawords",
214 RADIUS_ATTR_INT32 },
215 { RADIUS_ATTR_EVENT_TIMESTAMP, "Event-Timestamp",
216 RADIUS_ATTR_INT32 },
217 { RADIUS_ATTR_NAS_PORT_TYPE, "NAS-Port-Type", RADIUS_ATTR_INT32 },
218 { RADIUS_ATTR_TUNNEL_TYPE, "Tunnel-Type", RADIUS_ATTR_HEXDUMP },
219 { RADIUS_ATTR_TUNNEL_MEDIUM_TYPE, "Tunnel-Medium-Type",
220 RADIUS_ATTR_HEXDUMP },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800221 { RADIUS_ATTR_TUNNEL_PASSWORD, "Tunnel-Password",
222 RADIUS_ATTR_UNDIST },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700223 { RADIUS_ATTR_CONNECT_INFO, "Connect-Info", RADIUS_ATTR_TEXT },
224 { RADIUS_ATTR_EAP_MESSAGE, "EAP-Message", RADIUS_ATTR_UNDIST },
225 { RADIUS_ATTR_MESSAGE_AUTHENTICATOR, "Message-Authenticator",
226 RADIUS_ATTR_UNDIST },
227 { RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID, "Tunnel-Private-Group-Id",
228 RADIUS_ATTR_HEXDUMP },
229 { RADIUS_ATTR_ACCT_INTERIM_INTERVAL, "Acct-Interim-Interval",
230 RADIUS_ATTR_INT32 },
Dmitry Shmidt04949592012-07-19 12:16:46 -0700231 { RADIUS_ATTR_CHARGEABLE_USER_IDENTITY, "Chargeable-User-Identity",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700232 RADIUS_ATTR_TEXT },
233 { RADIUS_ATTR_NAS_IPV6_ADDRESS, "NAS-IPv6-Address", RADIUS_ATTR_IPV6 },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700234 { RADIUS_ATTR_ERROR_CAUSE, "Error-Cause", RADIUS_ATTR_INT32 },
235 { RADIUS_ATTR_EAP_KEY_NAME, "EAP-Key-Name", RADIUS_ATTR_HEXDUMP },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800236 { RADIUS_ATTR_OPERATOR_NAME, "Operator-Name", RADIUS_ATTR_TEXT },
237 { RADIUS_ATTR_LOCATION_INFO, "Location-Information",
238 RADIUS_ATTR_HEXDUMP },
239 { RADIUS_ATTR_LOCATION_DATA, "Location-Data", RADIUS_ATTR_HEXDUMP },
240 { RADIUS_ATTR_BASIC_LOCATION_POLICY_RULES,
241 "Basic-Location-Policy-Rules", RADIUS_ATTR_HEXDUMP },
242 { RADIUS_ATTR_EXTENDED_LOCATION_POLICY_RULES,
243 "Extended-Location-Policy-Rules", RADIUS_ATTR_HEXDUMP },
244 { RADIUS_ATTR_LOCATION_CAPABLE, "Location-Capable", RADIUS_ATTR_INT32 },
245 { RADIUS_ATTR_REQUESTED_LOCATION_INFO, "Requested-Location-Info",
246 RADIUS_ATTR_INT32 },
Dmitry Shmidt03658832014-08-13 11:03:49 -0700247 { RADIUS_ATTR_MOBILITY_DOMAIN_ID, "Mobility-Domain-Id",
248 RADIUS_ATTR_INT32 },
249 { RADIUS_ATTR_WLAN_HESSID, "WLAN-HESSID", RADIUS_ATTR_TEXT },
250 { RADIUS_ATTR_WLAN_PAIRWISE_CIPHER, "WLAN-Pairwise-Cipher",
251 RADIUS_ATTR_HEXDUMP },
252 { RADIUS_ATTR_WLAN_GROUP_CIPHER, "WLAN-Group-Cipher",
253 RADIUS_ATTR_HEXDUMP },
254 { RADIUS_ATTR_WLAN_AKM_SUITE, "WLAN-AKM-Suite",
255 RADIUS_ATTR_HEXDUMP },
256 { RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, "WLAN-Group-Mgmt-Pairwise-Cipher",
257 RADIUS_ATTR_HEXDUMP },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700258};
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700259#define RADIUS_ATTRS ARRAY_SIZE(radius_attrs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700260
261
262static struct radius_attr_type *radius_get_attr_type(u8 type)
263{
264 size_t i;
265
266 for (i = 0; i < RADIUS_ATTRS; i++) {
267 if (type == radius_attrs[i].type)
268 return &radius_attrs[i];
269 }
270
271 return NULL;
272}
273
274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275static void radius_msg_dump_attr(struct radius_attr_hdr *hdr)
276{
277 struct radius_attr_type *attr;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800278 int len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279 unsigned char *pos;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800280 char buf[1000];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281
282 attr = radius_get_attr_type(hdr->type);
283
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800284 wpa_printf(MSG_INFO, " Attribute %d (%s) length=%d",
285 hdr->type, attr ? attr->name : "?Unknown?", hdr->length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700287 if (attr == NULL || hdr->length < sizeof(struct radius_attr_hdr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288 return;
289
290 len = hdr->length - sizeof(struct radius_attr_hdr);
291 pos = (unsigned char *) (hdr + 1);
292
293 switch (attr->data_type) {
294 case RADIUS_ATTR_TEXT:
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800295 printf_encode(buf, sizeof(buf), pos, len);
296 wpa_printf(MSG_INFO, " Value: '%s'", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700297 break;
298
299 case RADIUS_ATTR_IP:
300 if (len == 4) {
301 struct in_addr addr;
302 os_memcpy(&addr, pos, 4);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800303 wpa_printf(MSG_INFO, " Value: %s",
304 inet_ntoa(addr));
305 } else {
306 wpa_printf(MSG_INFO, " Invalid IP address length %d",
307 len);
308 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309 break;
310
311#ifdef CONFIG_IPV6
312 case RADIUS_ATTR_IPV6:
313 if (len == 16) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314 const char *atxt;
315 struct in6_addr *addr = (struct in6_addr *) pos;
316 atxt = inet_ntop(AF_INET6, addr, buf, sizeof(buf));
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800317 wpa_printf(MSG_INFO, " Value: %s",
318 atxt ? atxt : "?");
319 } else {
320 wpa_printf(MSG_INFO, " Invalid IPv6 address length %d",
321 len);
322 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323 break;
324#endif /* CONFIG_IPV6 */
325
326 case RADIUS_ATTR_HEXDUMP:
327 case RADIUS_ATTR_UNDIST:
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800328 wpa_snprintf_hex(buf, sizeof(buf), pos, len);
329 wpa_printf(MSG_INFO, " Value: %s", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330 break;
331
332 case RADIUS_ATTR_INT32:
333 if (len == 4)
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800334 wpa_printf(MSG_INFO, " Value: %u",
335 WPA_GET_BE32(pos));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336 else
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800337 wpa_printf(MSG_INFO, " Invalid INT32 length %d",
338 len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339 break;
340
341 default:
342 break;
343 }
344}
345
346
347void radius_msg_dump(struct radius_msg *msg)
348{
349 size_t i;
350
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800351 wpa_printf(MSG_INFO, "RADIUS message: code=%d (%s) identifier=%d length=%d",
352 msg->hdr->code, radius_code_string(msg->hdr->code),
353 msg->hdr->identifier, be_to_host16(msg->hdr->length));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354
355 for (i = 0; i < msg->attr_used; i++) {
356 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
357 radius_msg_dump_attr(attr);
358 }
359}
360
361
362int radius_msg_finish(struct radius_msg *msg, const u8 *secret,
363 size_t secret_len)
364{
365 if (secret) {
366 u8 auth[MD5_MAC_LEN];
367 struct radius_attr_hdr *attr;
368
369 os_memset(auth, 0, MD5_MAC_LEN);
370 attr = radius_msg_add_attr(msg,
371 RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
372 auth, MD5_MAC_LEN);
373 if (attr == NULL) {
374 wpa_printf(MSG_WARNING, "RADIUS: Could not add "
375 "Message-Authenticator");
376 return -1;
377 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700378 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700379 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
380 wpabuf_len(msg->buf), (u8 *) (attr + 1));
381 } else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700382 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700383
384 if (wpabuf_len(msg->buf) > 0xffff) {
385 wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
386 (unsigned long) wpabuf_len(msg->buf));
387 return -1;
388 }
389 return 0;
390}
391
392
393int radius_msg_finish_srv(struct radius_msg *msg, const u8 *secret,
394 size_t secret_len, const u8 *req_authenticator)
395{
396 u8 auth[MD5_MAC_LEN];
397 struct radius_attr_hdr *attr;
398 const u8 *addr[4];
399 size_t len[4];
400
401 os_memset(auth, 0, MD5_MAC_LEN);
402 attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
403 auth, MD5_MAC_LEN);
404 if (attr == NULL) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800405 wpa_printf(MSG_ERROR, "WARNING: Could not add Message-Authenticator");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700406 return -1;
407 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700408 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409 os_memcpy(msg->hdr->authenticator, req_authenticator,
410 sizeof(msg->hdr->authenticator));
411 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
412 wpabuf_len(msg->buf), (u8 *) (attr + 1));
413
414 /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
415 addr[0] = (u8 *) msg->hdr;
416 len[0] = 1 + 1 + 2;
417 addr[1] = req_authenticator;
418 len[1] = MD5_MAC_LEN;
419 addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
420 len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
421 addr[3] = secret;
422 len[3] = secret_len;
423 md5_vector(4, addr, len, msg->hdr->authenticator);
424
425 if (wpabuf_len(msg->buf) > 0xffff) {
426 wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
427 (unsigned long) wpabuf_len(msg->buf));
428 return -1;
429 }
430 return 0;
431}
432
433
Dmitry Shmidt04949592012-07-19 12:16:46 -0700434int radius_msg_finish_das_resp(struct radius_msg *msg, const u8 *secret,
435 size_t secret_len,
436 const struct radius_hdr *req_hdr)
437{
438 const u8 *addr[2];
439 size_t len[2];
440 u8 auth[MD5_MAC_LEN];
441 struct radius_attr_hdr *attr;
442
443 os_memset(auth, 0, MD5_MAC_LEN);
444 attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
445 auth, MD5_MAC_LEN);
446 if (attr == NULL) {
447 wpa_printf(MSG_WARNING, "Could not add Message-Authenticator");
448 return -1;
449 }
450
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700451 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700452 os_memcpy(msg->hdr->authenticator, req_hdr->authenticator, 16);
453 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
454 wpabuf_len(msg->buf), (u8 *) (attr + 1));
455
456 /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
457 addr[0] = wpabuf_head_u8(msg->buf);
458 len[0] = wpabuf_len(msg->buf);
459 addr[1] = secret;
460 len[1] = secret_len;
461 if (md5_vector(2, addr, len, msg->hdr->authenticator) < 0)
462 return -1;
463
464 if (wpabuf_len(msg->buf) > 0xffff) {
465 wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
466 (unsigned long) wpabuf_len(msg->buf));
467 return -1;
468 }
469 return 0;
470}
471
472
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700473void radius_msg_finish_acct(struct radius_msg *msg, const u8 *secret,
474 size_t secret_len)
475{
476 const u8 *addr[2];
477 size_t len[2];
478
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700479 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480 os_memset(msg->hdr->authenticator, 0, MD5_MAC_LEN);
481 addr[0] = wpabuf_head(msg->buf);
482 len[0] = wpabuf_len(msg->buf);
483 addr[1] = secret;
484 len[1] = secret_len;
485 md5_vector(2, addr, len, msg->hdr->authenticator);
486
487 if (wpabuf_len(msg->buf) > 0xffff) {
488 wpa_printf(MSG_WARNING, "RADIUS: Too long messages (%lu)",
489 (unsigned long) wpabuf_len(msg->buf));
490 }
491}
492
493
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800494void radius_msg_finish_acct_resp(struct radius_msg *msg, const u8 *secret,
495 size_t secret_len, const u8 *req_authenticator)
496{
497 const u8 *addr[2];
498 size_t len[2];
499
500 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
501 os_memcpy(msg->hdr->authenticator, req_authenticator, MD5_MAC_LEN);
502 addr[0] = wpabuf_head(msg->buf);
503 len[0] = wpabuf_len(msg->buf);
504 addr[1] = secret;
505 len[1] = secret_len;
506 md5_vector(2, addr, len, msg->hdr->authenticator);
507
508 if (wpabuf_len(msg->buf) > 0xffff) {
509 wpa_printf(MSG_WARNING, "RADIUS: Too long messages (%lu)",
510 (unsigned long) wpabuf_len(msg->buf));
511 }
512}
513
514
Dmitry Shmidt04949592012-07-19 12:16:46 -0700515int radius_msg_verify_acct_req(struct radius_msg *msg, const u8 *secret,
516 size_t secret_len)
517{
518 const u8 *addr[4];
519 size_t len[4];
520 u8 zero[MD5_MAC_LEN];
521 u8 hash[MD5_MAC_LEN];
522
523 os_memset(zero, 0, sizeof(zero));
524 addr[0] = (u8 *) msg->hdr;
525 len[0] = sizeof(struct radius_hdr) - MD5_MAC_LEN;
526 addr[1] = zero;
527 len[1] = MD5_MAC_LEN;
528 addr[2] = (u8 *) (msg->hdr + 1);
529 len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
530 addr[3] = secret;
531 len[3] = secret_len;
532 md5_vector(4, addr, len, hash);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700533 return os_memcmp_const(msg->hdr->authenticator, hash, MD5_MAC_LEN) != 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700534}
535
536
537int radius_msg_verify_das_req(struct radius_msg *msg, const u8 *secret,
538 size_t secret_len)
539{
540 const u8 *addr[4];
541 size_t len[4];
542 u8 zero[MD5_MAC_LEN];
543 u8 hash[MD5_MAC_LEN];
544 u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
545 u8 orig_authenticator[16];
546
547 struct radius_attr_hdr *attr = NULL, *tmp;
548 size_t i;
549
550 os_memset(zero, 0, sizeof(zero));
551 addr[0] = (u8 *) msg->hdr;
552 len[0] = sizeof(struct radius_hdr) - MD5_MAC_LEN;
553 addr[1] = zero;
554 len[1] = MD5_MAC_LEN;
555 addr[2] = (u8 *) (msg->hdr + 1);
556 len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
557 addr[3] = secret;
558 len[3] = secret_len;
559 md5_vector(4, addr, len, hash);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700560 if (os_memcmp_const(msg->hdr->authenticator, hash, MD5_MAC_LEN) != 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700561 return 1;
562
563 for (i = 0; i < msg->attr_used; i++) {
564 tmp = radius_get_attr_hdr(msg, i);
565 if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
566 if (attr != NULL) {
567 wpa_printf(MSG_WARNING, "Multiple "
568 "Message-Authenticator attributes "
569 "in RADIUS message");
570 return 1;
571 }
572 attr = tmp;
573 }
574 }
575
576 if (attr == NULL) {
577 /* Message-Authenticator is MAY; not required */
578 return 0;
579 }
580
581 os_memcpy(orig, attr + 1, MD5_MAC_LEN);
582 os_memset(attr + 1, 0, MD5_MAC_LEN);
583 os_memcpy(orig_authenticator, msg->hdr->authenticator,
584 sizeof(orig_authenticator));
585 os_memset(msg->hdr->authenticator, 0,
586 sizeof(msg->hdr->authenticator));
587 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
588 wpabuf_len(msg->buf), auth);
589 os_memcpy(attr + 1, orig, MD5_MAC_LEN);
590 os_memcpy(msg->hdr->authenticator, orig_authenticator,
591 sizeof(orig_authenticator));
592
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700593 return os_memcmp_const(orig, auth, MD5_MAC_LEN) != 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700594}
595
596
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700597static int radius_msg_add_attr_to_array(struct radius_msg *msg,
598 struct radius_attr_hdr *attr)
599{
600 if (msg->attr_used >= msg->attr_size) {
601 size_t *nattr_pos;
602 int nlen = msg->attr_size * 2;
603
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700604 nattr_pos = os_realloc_array(msg->attr_pos, nlen,
605 sizeof(*msg->attr_pos));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606 if (nattr_pos == NULL)
607 return -1;
608
609 msg->attr_pos = nattr_pos;
610 msg->attr_size = nlen;
611 }
612
613 msg->attr_pos[msg->attr_used++] =
614 (unsigned char *) attr - wpabuf_head_u8(msg->buf);
615
616 return 0;
617}
618
619
620struct radius_attr_hdr *radius_msg_add_attr(struct radius_msg *msg, u8 type,
621 const u8 *data, size_t data_len)
622{
623 size_t buf_needed;
624 struct radius_attr_hdr *attr;
625
626 if (data_len > RADIUS_MAX_ATTR_LEN) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800627 wpa_printf(MSG_ERROR, "radius_msg_add_attr: too long attribute (%lu bytes)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700628 (unsigned long) data_len);
629 return NULL;
630 }
631
632 buf_needed = sizeof(*attr) + data_len;
633
634 if (wpabuf_tailroom(msg->buf) < buf_needed) {
635 /* allocate more space for message buffer */
636 if (wpabuf_resize(&msg->buf, buf_needed) < 0)
637 return NULL;
638 msg->hdr = wpabuf_mhead(msg->buf);
639 }
640
641 attr = wpabuf_put(msg->buf, sizeof(struct radius_attr_hdr));
642 attr->type = type;
643 attr->length = sizeof(*attr) + data_len;
644 wpabuf_put_data(msg->buf, data, data_len);
645
646 if (radius_msg_add_attr_to_array(msg, attr))
647 return NULL;
648
649 return attr;
650}
651
652
653/**
654 * radius_msg_parse - Parse a RADIUS message
655 * @data: RADIUS message to be parsed
656 * @len: Length of data buffer in octets
657 * Returns: Parsed RADIUS message or %NULL on failure
658 *
659 * This parses a RADIUS message and makes a copy of its data. The caller is
660 * responsible for freeing the returned data with radius_msg_free().
661 */
662struct radius_msg * radius_msg_parse(const u8 *data, size_t len)
663{
664 struct radius_msg *msg;
665 struct radius_hdr *hdr;
666 struct radius_attr_hdr *attr;
667 size_t msg_len;
668 unsigned char *pos, *end;
669
670 if (data == NULL || len < sizeof(*hdr))
671 return NULL;
672
673 hdr = (struct radius_hdr *) data;
674
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700675 msg_len = be_to_host16(hdr->length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700676 if (msg_len < sizeof(*hdr) || msg_len > len) {
677 wpa_printf(MSG_INFO, "RADIUS: Invalid message length");
678 return NULL;
679 }
680
681 if (msg_len < len) {
682 wpa_printf(MSG_DEBUG, "RADIUS: Ignored %lu extra bytes after "
683 "RADIUS message", (unsigned long) len - msg_len);
684 }
685
686 msg = os_zalloc(sizeof(*msg));
687 if (msg == NULL)
688 return NULL;
689
690 msg->buf = wpabuf_alloc_copy(data, msg_len);
691 if (msg->buf == NULL || radius_msg_initialize(msg)) {
692 radius_msg_free(msg);
693 return NULL;
694 }
695 msg->hdr = wpabuf_mhead(msg->buf);
696
697 /* parse attributes */
698 pos = wpabuf_mhead_u8(msg->buf) + sizeof(struct radius_hdr);
699 end = wpabuf_mhead_u8(msg->buf) + wpabuf_len(msg->buf);
700 while (pos < end) {
701 if ((size_t) (end - pos) < sizeof(*attr))
702 goto fail;
703
704 attr = (struct radius_attr_hdr *) pos;
705
706 if (pos + attr->length > end || attr->length < sizeof(*attr))
707 goto fail;
708
709 /* TODO: check that attr->length is suitable for attr->type */
710
711 if (radius_msg_add_attr_to_array(msg, attr))
712 goto fail;
713
714 pos += attr->length;
715 }
716
717 return msg;
718
719 fail:
720 radius_msg_free(msg);
721 return NULL;
722}
723
724
725int radius_msg_add_eap(struct radius_msg *msg, const u8 *data, size_t data_len)
726{
727 const u8 *pos = data;
728 size_t left = data_len;
729
730 while (left > 0) {
731 int len;
732 if (left > RADIUS_MAX_ATTR_LEN)
733 len = RADIUS_MAX_ATTR_LEN;
734 else
735 len = left;
736
737 if (!radius_msg_add_attr(msg, RADIUS_ATTR_EAP_MESSAGE,
738 pos, len))
739 return 0;
740
741 pos += len;
742 left -= len;
743 }
744
745 return 1;
746}
747
748
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700749struct wpabuf * radius_msg_get_eap(struct radius_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700750{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700751 struct wpabuf *eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700752 size_t len, i;
753 struct radius_attr_hdr *attr;
754
755 if (msg == NULL)
756 return NULL;
757
758 len = 0;
759 for (i = 0; i < msg->attr_used; i++) {
760 attr = radius_get_attr_hdr(msg, i);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700761 if (attr->type == RADIUS_ATTR_EAP_MESSAGE &&
762 attr->length > sizeof(struct radius_attr_hdr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700763 len += attr->length - sizeof(struct radius_attr_hdr);
764 }
765
766 if (len == 0)
767 return NULL;
768
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700769 eap = wpabuf_alloc(len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700770 if (eap == NULL)
771 return NULL;
772
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700773 for (i = 0; i < msg->attr_used; i++) {
774 attr = radius_get_attr_hdr(msg, i);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700775 if (attr->type == RADIUS_ATTR_EAP_MESSAGE &&
776 attr->length > sizeof(struct radius_attr_hdr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700777 int flen = attr->length - sizeof(*attr);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700778 wpabuf_put_data(eap, attr + 1, flen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700779 }
780 }
781
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700782 return eap;
783}
784
785
786int radius_msg_verify_msg_auth(struct radius_msg *msg, const u8 *secret,
787 size_t secret_len, const u8 *req_auth)
788{
789 u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
790 u8 orig_authenticator[16];
791 struct radius_attr_hdr *attr = NULL, *tmp;
792 size_t i;
793
794 for (i = 0; i < msg->attr_used; i++) {
795 tmp = radius_get_attr_hdr(msg, i);
796 if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
797 if (attr != NULL) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800798 wpa_printf(MSG_INFO, "Multiple Message-Authenticator attributes in RADIUS message");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700799 return 1;
800 }
801 attr = tmp;
802 }
803 }
804
805 if (attr == NULL) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800806 wpa_printf(MSG_INFO, "No Message-Authenticator attribute found");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700807 return 1;
808 }
809
810 os_memcpy(orig, attr + 1, MD5_MAC_LEN);
811 os_memset(attr + 1, 0, MD5_MAC_LEN);
812 if (req_auth) {
813 os_memcpy(orig_authenticator, msg->hdr->authenticator,
814 sizeof(orig_authenticator));
815 os_memcpy(msg->hdr->authenticator, req_auth,
816 sizeof(msg->hdr->authenticator));
817 }
818 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
819 wpabuf_len(msg->buf), auth);
820 os_memcpy(attr + 1, orig, MD5_MAC_LEN);
821 if (req_auth) {
822 os_memcpy(msg->hdr->authenticator, orig_authenticator,
823 sizeof(orig_authenticator));
824 }
825
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700826 if (os_memcmp_const(orig, auth, MD5_MAC_LEN) != 0) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800827 wpa_printf(MSG_INFO, "Invalid Message-Authenticator!");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700828 return 1;
829 }
830
831 return 0;
832}
833
834
835int radius_msg_verify(struct radius_msg *msg, const u8 *secret,
836 size_t secret_len, struct radius_msg *sent_msg, int auth)
837{
838 const u8 *addr[4];
839 size_t len[4];
840 u8 hash[MD5_MAC_LEN];
841
842 if (sent_msg == NULL) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800843 wpa_printf(MSG_INFO, "No matching Access-Request message found");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700844 return 1;
845 }
846
847 if (auth &&
848 radius_msg_verify_msg_auth(msg, secret, secret_len,
849 sent_msg->hdr->authenticator)) {
850 return 1;
851 }
852
853 /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
854 addr[0] = (u8 *) msg->hdr;
855 len[0] = 1 + 1 + 2;
856 addr[1] = sent_msg->hdr->authenticator;
857 len[1] = MD5_MAC_LEN;
858 addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
859 len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
860 addr[3] = secret;
861 len[3] = secret_len;
862 md5_vector(4, addr, len, hash);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700863 if (os_memcmp_const(hash, msg->hdr->authenticator, MD5_MAC_LEN) != 0) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800864 wpa_printf(MSG_INFO, "Response Authenticator invalid!");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700865 return 1;
866 }
867
868 return 0;
869}
870
871
872int radius_msg_copy_attr(struct radius_msg *dst, struct radius_msg *src,
873 u8 type)
874{
875 struct radius_attr_hdr *attr;
876 size_t i;
877 int count = 0;
878
879 for (i = 0; i < src->attr_used; i++) {
880 attr = radius_get_attr_hdr(src, i);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700881 if (attr->type == type && attr->length >= sizeof(*attr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 if (!radius_msg_add_attr(dst, type, (u8 *) (attr + 1),
883 attr->length - sizeof(*attr)))
884 return -1;
885 count++;
886 }
887 }
888
889 return count;
890}
891
892
893/* Create Request Authenticator. The value should be unique over the lifetime
894 * of the shared secret between authenticator and authentication server.
895 * Use one-way MD5 hash calculated from current timestamp and some data given
896 * by the caller. */
897void radius_msg_make_authenticator(struct radius_msg *msg,
898 const u8 *data, size_t len)
899{
900 struct os_time tv;
901 long int l;
902 const u8 *addr[3];
903 size_t elen[3];
904
905 os_get_time(&tv);
906 l = os_random();
907 addr[0] = (u8 *) &tv;
908 elen[0] = sizeof(tv);
909 addr[1] = data;
910 elen[1] = len;
911 addr[2] = (u8 *) &l;
912 elen[2] = sizeof(l);
913 md5_vector(3, addr, elen, msg->hdr->authenticator);
914}
915
916
917/* Get Vendor-specific RADIUS Attribute from a parsed RADIUS message.
918 * Returns the Attribute payload and sets alen to indicate the length of the
919 * payload if a vendor attribute with subtype is found, otherwise returns NULL.
920 * The returned payload is allocated with os_malloc() and caller must free it
921 * by calling os_free().
922 */
923static u8 *radius_msg_get_vendor_attr(struct radius_msg *msg, u32 vendor,
924 u8 subtype, size_t *alen)
925{
926 u8 *data, *pos;
927 size_t i, len;
928
929 if (msg == NULL)
930 return NULL;
931
932 for (i = 0; i < msg->attr_used; i++) {
933 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
934 size_t left;
935 u32 vendor_id;
936 struct radius_attr_vendor *vhdr;
937
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700938 if (attr->type != RADIUS_ATTR_VENDOR_SPECIFIC ||
939 attr->length < sizeof(*attr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940 continue;
941
942 left = attr->length - sizeof(*attr);
943 if (left < 4)
944 continue;
945
946 pos = (u8 *) (attr + 1);
947
948 os_memcpy(&vendor_id, pos, 4);
949 pos += 4;
950 left -= 4;
951
952 if (ntohl(vendor_id) != vendor)
953 continue;
954
955 while (left >= sizeof(*vhdr)) {
956 vhdr = (struct radius_attr_vendor *) pos;
957 if (vhdr->vendor_length > left ||
958 vhdr->vendor_length < sizeof(*vhdr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700959 break;
960 }
961 if (vhdr->vendor_type != subtype) {
962 pos += vhdr->vendor_length;
963 left -= vhdr->vendor_length;
964 continue;
965 }
966
967 len = vhdr->vendor_length - sizeof(*vhdr);
968 data = os_malloc(len);
969 if (data == NULL)
970 return NULL;
971 os_memcpy(data, pos + sizeof(*vhdr), len);
972 if (alen)
973 *alen = len;
974 return data;
975 }
976 }
977
978 return NULL;
979}
980
981
982static u8 * decrypt_ms_key(const u8 *key, size_t len,
983 const u8 *req_authenticator,
984 const u8 *secret, size_t secret_len, size_t *reslen)
985{
986 u8 *plain, *ppos, *res;
987 const u8 *pos;
988 size_t left, plen;
989 u8 hash[MD5_MAC_LEN];
990 int i, first = 1;
991 const u8 *addr[3];
992 size_t elen[3];
993
994 /* key: 16-bit salt followed by encrypted key info */
995
996 if (len < 2 + 16)
997 return NULL;
998
999 pos = key + 2;
1000 left = len - 2;
1001 if (left % 16) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001002 wpa_printf(MSG_INFO, "Invalid ms key len %lu",
1003 (unsigned long) left);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001004 return NULL;
1005 }
1006
1007 plen = left;
1008 ppos = plain = os_malloc(plen);
1009 if (plain == NULL)
1010 return NULL;
1011 plain[0] = 0;
1012
1013 while (left > 0) {
1014 /* b(1) = MD5(Secret + Request-Authenticator + Salt)
1015 * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
1016
1017 addr[0] = secret;
1018 elen[0] = secret_len;
1019 if (first) {
1020 addr[1] = req_authenticator;
1021 elen[1] = MD5_MAC_LEN;
1022 addr[2] = key;
1023 elen[2] = 2; /* Salt */
1024 } else {
1025 addr[1] = pos - MD5_MAC_LEN;
1026 elen[1] = MD5_MAC_LEN;
1027 }
1028 md5_vector(first ? 3 : 2, addr, elen, hash);
1029 first = 0;
1030
1031 for (i = 0; i < MD5_MAC_LEN; i++)
1032 *ppos++ = *pos++ ^ hash[i];
1033 left -= MD5_MAC_LEN;
1034 }
1035
1036 if (plain[0] == 0 || plain[0] > plen - 1) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001037 wpa_printf(MSG_INFO, "Failed to decrypt MPPE key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001038 os_free(plain);
1039 return NULL;
1040 }
1041
1042 res = os_malloc(plain[0]);
1043 if (res == NULL) {
1044 os_free(plain);
1045 return NULL;
1046 }
1047 os_memcpy(res, plain + 1, plain[0]);
1048 if (reslen)
1049 *reslen = plain[0];
1050 os_free(plain);
1051 return res;
1052}
1053
1054
1055static void encrypt_ms_key(const u8 *key, size_t key_len, u16 salt,
1056 const u8 *req_authenticator,
1057 const u8 *secret, size_t secret_len,
1058 u8 *ebuf, size_t *elen)
1059{
1060 int i, len, first = 1;
1061 u8 hash[MD5_MAC_LEN], saltbuf[2], *pos;
1062 const u8 *addr[3];
1063 size_t _len[3];
1064
1065 WPA_PUT_BE16(saltbuf, salt);
1066
1067 len = 1 + key_len;
1068 if (len & 0x0f) {
1069 len = (len & 0xf0) + 16;
1070 }
1071 os_memset(ebuf, 0, len);
1072 ebuf[0] = key_len;
1073 os_memcpy(ebuf + 1, key, key_len);
1074
1075 *elen = len;
1076
1077 pos = ebuf;
1078 while (len > 0) {
1079 /* b(1) = MD5(Secret + Request-Authenticator + Salt)
1080 * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
1081 addr[0] = secret;
1082 _len[0] = secret_len;
1083 if (first) {
1084 addr[1] = req_authenticator;
1085 _len[1] = MD5_MAC_LEN;
1086 addr[2] = saltbuf;
1087 _len[2] = sizeof(saltbuf);
1088 } else {
1089 addr[1] = pos - MD5_MAC_LEN;
1090 _len[1] = MD5_MAC_LEN;
1091 }
1092 md5_vector(first ? 3 : 2, addr, _len, hash);
1093 first = 0;
1094
1095 for (i = 0; i < MD5_MAC_LEN; i++)
1096 *pos++ ^= hash[i];
1097
1098 len -= MD5_MAC_LEN;
1099 }
1100}
1101
1102
1103struct radius_ms_mppe_keys *
1104radius_msg_get_ms_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
1105 const u8 *secret, size_t secret_len)
1106{
1107 u8 *key;
1108 size_t keylen;
1109 struct radius_ms_mppe_keys *keys;
1110
1111 if (msg == NULL || sent_msg == NULL)
1112 return NULL;
1113
1114 keys = os_zalloc(sizeof(*keys));
1115 if (keys == NULL)
1116 return NULL;
1117
1118 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
1119 RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY,
1120 &keylen);
1121 if (key) {
1122 keys->send = decrypt_ms_key(key, keylen,
1123 sent_msg->hdr->authenticator,
1124 secret, secret_len,
1125 &keys->send_len);
1126 os_free(key);
1127 }
1128
1129 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
1130 RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY,
1131 &keylen);
1132 if (key) {
1133 keys->recv = decrypt_ms_key(key, keylen,
1134 sent_msg->hdr->authenticator,
1135 secret, secret_len,
1136 &keys->recv_len);
1137 os_free(key);
1138 }
1139
1140 return keys;
1141}
1142
1143
1144struct radius_ms_mppe_keys *
1145radius_msg_get_cisco_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
1146 const u8 *secret, size_t secret_len)
1147{
1148 u8 *key;
1149 size_t keylen;
1150 struct radius_ms_mppe_keys *keys;
1151
1152 if (msg == NULL || sent_msg == NULL)
1153 return NULL;
1154
1155 keys = os_zalloc(sizeof(*keys));
1156 if (keys == NULL)
1157 return NULL;
1158
1159 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_CISCO,
1160 RADIUS_CISCO_AV_PAIR, &keylen);
1161 if (key && keylen == 51 &&
1162 os_memcmp(key, "leap:session-key=", 17) == 0) {
1163 keys->recv = decrypt_ms_key(key + 17, keylen - 17,
1164 sent_msg->hdr->authenticator,
1165 secret, secret_len,
1166 &keys->recv_len);
1167 }
1168 os_free(key);
1169
1170 return keys;
1171}
1172
1173
1174int radius_msg_add_mppe_keys(struct radius_msg *msg,
1175 const u8 *req_authenticator,
1176 const u8 *secret, size_t secret_len,
1177 const u8 *send_key, size_t send_key_len,
1178 const u8 *recv_key, size_t recv_key_len)
1179{
1180 struct radius_attr_hdr *attr;
1181 u32 vendor_id = htonl(RADIUS_VENDOR_ID_MICROSOFT);
1182 u8 *buf;
1183 struct radius_attr_vendor *vhdr;
1184 u8 *pos;
1185 size_t elen;
1186 int hlen;
1187 u16 salt;
1188
1189 hlen = sizeof(vendor_id) + sizeof(*vhdr) + 2;
1190
1191 /* MS-MPPE-Send-Key */
1192 buf = os_malloc(hlen + send_key_len + 16);
1193 if (buf == NULL) {
1194 return 0;
1195 }
1196 pos = buf;
1197 os_memcpy(pos, &vendor_id, sizeof(vendor_id));
1198 pos += sizeof(vendor_id);
1199 vhdr = (struct radius_attr_vendor *) pos;
1200 vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY;
1201 pos = (u8 *) (vhdr + 1);
1202 salt = os_random() | 0x8000;
1203 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 */
1217 buf = os_malloc(hlen + send_key_len + 16);
1218 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
1417 * Returns: VLAN ID for the first tunnel configuration of -1 if none is found
1418 */
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
1480 return -1;
1481}
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}