blob: f2d8e96fe70b123343db219a805b0050ea0c631c [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 Shmidt8d520ff2011-05-09 14:06:53 -0700236};
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700237#define RADIUS_ATTRS ARRAY_SIZE(radius_attrs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700238
239
240static struct radius_attr_type *radius_get_attr_type(u8 type)
241{
242 size_t i;
243
244 for (i = 0; i < RADIUS_ATTRS; i++) {
245 if (type == radius_attrs[i].type)
246 return &radius_attrs[i];
247 }
248
249 return NULL;
250}
251
252
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700253static void radius_msg_dump_attr(struct radius_attr_hdr *hdr)
254{
255 struct radius_attr_type *attr;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800256 int len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700257 unsigned char *pos;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800258 char buf[1000];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700259
260 attr = radius_get_attr_type(hdr->type);
261
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800262 wpa_printf(MSG_INFO, " Attribute %d (%s) length=%d",
263 hdr->type, attr ? attr->name : "?Unknown?", hdr->length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700264
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700265 if (attr == NULL || hdr->length < sizeof(struct radius_attr_hdr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700266 return;
267
268 len = hdr->length - sizeof(struct radius_attr_hdr);
269 pos = (unsigned char *) (hdr + 1);
270
271 switch (attr->data_type) {
272 case RADIUS_ATTR_TEXT:
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800273 printf_encode(buf, sizeof(buf), pos, len);
274 wpa_printf(MSG_INFO, " Value: '%s'", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275 break;
276
277 case RADIUS_ATTR_IP:
278 if (len == 4) {
279 struct in_addr addr;
280 os_memcpy(&addr, pos, 4);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800281 wpa_printf(MSG_INFO, " Value: %s",
282 inet_ntoa(addr));
283 } else {
284 wpa_printf(MSG_INFO, " Invalid IP address length %d",
285 len);
286 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287 break;
288
289#ifdef CONFIG_IPV6
290 case RADIUS_ATTR_IPV6:
291 if (len == 16) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700292 const char *atxt;
293 struct in6_addr *addr = (struct in6_addr *) pos;
294 atxt = inet_ntop(AF_INET6, addr, buf, sizeof(buf));
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800295 wpa_printf(MSG_INFO, " Value: %s",
296 atxt ? atxt : "?");
297 } else {
298 wpa_printf(MSG_INFO, " Invalid IPv6 address length %d",
299 len);
300 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700301 break;
302#endif /* CONFIG_IPV6 */
303
304 case RADIUS_ATTR_HEXDUMP:
305 case RADIUS_ATTR_UNDIST:
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800306 wpa_snprintf_hex(buf, sizeof(buf), pos, len);
307 wpa_printf(MSG_INFO, " Value: %s", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308 break;
309
310 case RADIUS_ATTR_INT32:
311 if (len == 4)
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800312 wpa_printf(MSG_INFO, " Value: %u",
313 WPA_GET_BE32(pos));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314 else
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800315 wpa_printf(MSG_INFO, " Invalid INT32 length %d",
316 len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317 break;
318
319 default:
320 break;
321 }
322}
323
324
325void radius_msg_dump(struct radius_msg *msg)
326{
327 size_t i;
328
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800329 wpa_printf(MSG_INFO, "RADIUS message: code=%d (%s) identifier=%d length=%d",
330 msg->hdr->code, radius_code_string(msg->hdr->code),
331 msg->hdr->identifier, be_to_host16(msg->hdr->length));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332
333 for (i = 0; i < msg->attr_used; i++) {
334 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
335 radius_msg_dump_attr(attr);
336 }
337}
338
339
340int radius_msg_finish(struct radius_msg *msg, const u8 *secret,
341 size_t secret_len)
342{
343 if (secret) {
344 u8 auth[MD5_MAC_LEN];
345 struct radius_attr_hdr *attr;
346
347 os_memset(auth, 0, MD5_MAC_LEN);
348 attr = radius_msg_add_attr(msg,
349 RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
350 auth, MD5_MAC_LEN);
351 if (attr == NULL) {
352 wpa_printf(MSG_WARNING, "RADIUS: Could not add "
353 "Message-Authenticator");
354 return -1;
355 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700356 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
358 wpabuf_len(msg->buf), (u8 *) (attr + 1));
359 } else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700360 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700361
362 if (wpabuf_len(msg->buf) > 0xffff) {
363 wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
364 (unsigned long) wpabuf_len(msg->buf));
365 return -1;
366 }
367 return 0;
368}
369
370
371int radius_msg_finish_srv(struct radius_msg *msg, const u8 *secret,
372 size_t secret_len, const u8 *req_authenticator)
373{
374 u8 auth[MD5_MAC_LEN];
375 struct radius_attr_hdr *attr;
376 const u8 *addr[4];
377 size_t len[4];
378
379 os_memset(auth, 0, MD5_MAC_LEN);
380 attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
381 auth, MD5_MAC_LEN);
382 if (attr == NULL) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800383 wpa_printf(MSG_ERROR, "WARNING: Could not add Message-Authenticator");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384 return -1;
385 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700386 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700387 os_memcpy(msg->hdr->authenticator, req_authenticator,
388 sizeof(msg->hdr->authenticator));
389 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
390 wpabuf_len(msg->buf), (u8 *) (attr + 1));
391
392 /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
393 addr[0] = (u8 *) msg->hdr;
394 len[0] = 1 + 1 + 2;
395 addr[1] = req_authenticator;
396 len[1] = MD5_MAC_LEN;
397 addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
398 len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
399 addr[3] = secret;
400 len[3] = secret_len;
401 md5_vector(4, addr, len, msg->hdr->authenticator);
402
403 if (wpabuf_len(msg->buf) > 0xffff) {
404 wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
405 (unsigned long) wpabuf_len(msg->buf));
406 return -1;
407 }
408 return 0;
409}
410
411
Dmitry Shmidt04949592012-07-19 12:16:46 -0700412int radius_msg_finish_das_resp(struct radius_msg *msg, const u8 *secret,
413 size_t secret_len,
414 const struct radius_hdr *req_hdr)
415{
416 const u8 *addr[2];
417 size_t len[2];
418 u8 auth[MD5_MAC_LEN];
419 struct radius_attr_hdr *attr;
420
421 os_memset(auth, 0, MD5_MAC_LEN);
422 attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
423 auth, MD5_MAC_LEN);
424 if (attr == NULL) {
425 wpa_printf(MSG_WARNING, "Could not add Message-Authenticator");
426 return -1;
427 }
428
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700429 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700430 os_memcpy(msg->hdr->authenticator, req_hdr->authenticator, 16);
431 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
432 wpabuf_len(msg->buf), (u8 *) (attr + 1));
433
434 /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
435 addr[0] = wpabuf_head_u8(msg->buf);
436 len[0] = wpabuf_len(msg->buf);
437 addr[1] = secret;
438 len[1] = secret_len;
439 if (md5_vector(2, addr, len, msg->hdr->authenticator) < 0)
440 return -1;
441
442 if (wpabuf_len(msg->buf) > 0xffff) {
443 wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
444 (unsigned long) wpabuf_len(msg->buf));
445 return -1;
446 }
447 return 0;
448}
449
450
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700451void radius_msg_finish_acct(struct radius_msg *msg, const u8 *secret,
452 size_t secret_len)
453{
454 const u8 *addr[2];
455 size_t len[2];
456
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700457 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458 os_memset(msg->hdr->authenticator, 0, MD5_MAC_LEN);
459 addr[0] = wpabuf_head(msg->buf);
460 len[0] = wpabuf_len(msg->buf);
461 addr[1] = secret;
462 len[1] = secret_len;
463 md5_vector(2, addr, len, msg->hdr->authenticator);
464
465 if (wpabuf_len(msg->buf) > 0xffff) {
466 wpa_printf(MSG_WARNING, "RADIUS: Too long messages (%lu)",
467 (unsigned long) wpabuf_len(msg->buf));
468 }
469}
470
471
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800472void radius_msg_finish_acct_resp(struct radius_msg *msg, const u8 *secret,
473 size_t secret_len, const u8 *req_authenticator)
474{
475 const u8 *addr[2];
476 size_t len[2];
477
478 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
479 os_memcpy(msg->hdr->authenticator, req_authenticator, MD5_MAC_LEN);
480 addr[0] = wpabuf_head(msg->buf);
481 len[0] = wpabuf_len(msg->buf);
482 addr[1] = secret;
483 len[1] = secret_len;
484 md5_vector(2, addr, len, msg->hdr->authenticator);
485
486 if (wpabuf_len(msg->buf) > 0xffff) {
487 wpa_printf(MSG_WARNING, "RADIUS: Too long messages (%lu)",
488 (unsigned long) wpabuf_len(msg->buf));
489 }
490}
491
492
Dmitry Shmidt04949592012-07-19 12:16:46 -0700493int radius_msg_verify_acct_req(struct radius_msg *msg, const u8 *secret,
494 size_t secret_len)
495{
496 const u8 *addr[4];
497 size_t len[4];
498 u8 zero[MD5_MAC_LEN];
499 u8 hash[MD5_MAC_LEN];
500
501 os_memset(zero, 0, sizeof(zero));
502 addr[0] = (u8 *) msg->hdr;
503 len[0] = sizeof(struct radius_hdr) - MD5_MAC_LEN;
504 addr[1] = zero;
505 len[1] = MD5_MAC_LEN;
506 addr[2] = (u8 *) (msg->hdr + 1);
507 len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
508 addr[3] = secret;
509 len[3] = secret_len;
510 md5_vector(4, addr, len, hash);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700511 return os_memcmp_const(msg->hdr->authenticator, hash, MD5_MAC_LEN) != 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700512}
513
514
515int radius_msg_verify_das_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 u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
523 u8 orig_authenticator[16];
524
525 struct radius_attr_hdr *attr = NULL, *tmp;
526 size_t i;
527
528 os_memset(zero, 0, sizeof(zero));
529 addr[0] = (u8 *) msg->hdr;
530 len[0] = sizeof(struct radius_hdr) - MD5_MAC_LEN;
531 addr[1] = zero;
532 len[1] = MD5_MAC_LEN;
533 addr[2] = (u8 *) (msg->hdr + 1);
534 len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
535 addr[3] = secret;
536 len[3] = secret_len;
537 md5_vector(4, addr, len, hash);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700538 if (os_memcmp_const(msg->hdr->authenticator, hash, MD5_MAC_LEN) != 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700539 return 1;
540
541 for (i = 0; i < msg->attr_used; i++) {
542 tmp = radius_get_attr_hdr(msg, i);
543 if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
544 if (attr != NULL) {
545 wpa_printf(MSG_WARNING, "Multiple "
546 "Message-Authenticator attributes "
547 "in RADIUS message");
548 return 1;
549 }
550 attr = tmp;
551 }
552 }
553
554 if (attr == NULL) {
555 /* Message-Authenticator is MAY; not required */
556 return 0;
557 }
558
559 os_memcpy(orig, attr + 1, MD5_MAC_LEN);
560 os_memset(attr + 1, 0, MD5_MAC_LEN);
561 os_memcpy(orig_authenticator, msg->hdr->authenticator,
562 sizeof(orig_authenticator));
563 os_memset(msg->hdr->authenticator, 0,
564 sizeof(msg->hdr->authenticator));
565 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
566 wpabuf_len(msg->buf), auth);
567 os_memcpy(attr + 1, orig, MD5_MAC_LEN);
568 os_memcpy(msg->hdr->authenticator, orig_authenticator,
569 sizeof(orig_authenticator));
570
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700571 return os_memcmp_const(orig, auth, MD5_MAC_LEN) != 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700572}
573
574
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700575static int radius_msg_add_attr_to_array(struct radius_msg *msg,
576 struct radius_attr_hdr *attr)
577{
578 if (msg->attr_used >= msg->attr_size) {
579 size_t *nattr_pos;
580 int nlen = msg->attr_size * 2;
581
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700582 nattr_pos = os_realloc_array(msg->attr_pos, nlen,
583 sizeof(*msg->attr_pos));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700584 if (nattr_pos == NULL)
585 return -1;
586
587 msg->attr_pos = nattr_pos;
588 msg->attr_size = nlen;
589 }
590
591 msg->attr_pos[msg->attr_used++] =
592 (unsigned char *) attr - wpabuf_head_u8(msg->buf);
593
594 return 0;
595}
596
597
598struct radius_attr_hdr *radius_msg_add_attr(struct radius_msg *msg, u8 type,
599 const u8 *data, size_t data_len)
600{
601 size_t buf_needed;
602 struct radius_attr_hdr *attr;
603
604 if (data_len > RADIUS_MAX_ATTR_LEN) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800605 wpa_printf(MSG_ERROR, "radius_msg_add_attr: too long attribute (%lu bytes)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606 (unsigned long) data_len);
607 return NULL;
608 }
609
610 buf_needed = sizeof(*attr) + data_len;
611
612 if (wpabuf_tailroom(msg->buf) < buf_needed) {
613 /* allocate more space for message buffer */
614 if (wpabuf_resize(&msg->buf, buf_needed) < 0)
615 return NULL;
616 msg->hdr = wpabuf_mhead(msg->buf);
617 }
618
619 attr = wpabuf_put(msg->buf, sizeof(struct radius_attr_hdr));
620 attr->type = type;
621 attr->length = sizeof(*attr) + data_len;
622 wpabuf_put_data(msg->buf, data, data_len);
623
624 if (radius_msg_add_attr_to_array(msg, attr))
625 return NULL;
626
627 return attr;
628}
629
630
631/**
632 * radius_msg_parse - Parse a RADIUS message
633 * @data: RADIUS message to be parsed
634 * @len: Length of data buffer in octets
635 * Returns: Parsed RADIUS message or %NULL on failure
636 *
637 * This parses a RADIUS message and makes a copy of its data. The caller is
638 * responsible for freeing the returned data with radius_msg_free().
639 */
640struct radius_msg * radius_msg_parse(const u8 *data, size_t len)
641{
642 struct radius_msg *msg;
643 struct radius_hdr *hdr;
644 struct radius_attr_hdr *attr;
645 size_t msg_len;
646 unsigned char *pos, *end;
647
648 if (data == NULL || len < sizeof(*hdr))
649 return NULL;
650
651 hdr = (struct radius_hdr *) data;
652
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700653 msg_len = be_to_host16(hdr->length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700654 if (msg_len < sizeof(*hdr) || msg_len > len) {
655 wpa_printf(MSG_INFO, "RADIUS: Invalid message length");
656 return NULL;
657 }
658
659 if (msg_len < len) {
660 wpa_printf(MSG_DEBUG, "RADIUS: Ignored %lu extra bytes after "
661 "RADIUS message", (unsigned long) len - msg_len);
662 }
663
664 msg = os_zalloc(sizeof(*msg));
665 if (msg == NULL)
666 return NULL;
667
668 msg->buf = wpabuf_alloc_copy(data, msg_len);
669 if (msg->buf == NULL || radius_msg_initialize(msg)) {
670 radius_msg_free(msg);
671 return NULL;
672 }
673 msg->hdr = wpabuf_mhead(msg->buf);
674
675 /* parse attributes */
676 pos = wpabuf_mhead_u8(msg->buf) + sizeof(struct radius_hdr);
677 end = wpabuf_mhead_u8(msg->buf) + wpabuf_len(msg->buf);
678 while (pos < end) {
679 if ((size_t) (end - pos) < sizeof(*attr))
680 goto fail;
681
682 attr = (struct radius_attr_hdr *) pos;
683
684 if (pos + attr->length > end || attr->length < sizeof(*attr))
685 goto fail;
686
687 /* TODO: check that attr->length is suitable for attr->type */
688
689 if (radius_msg_add_attr_to_array(msg, attr))
690 goto fail;
691
692 pos += attr->length;
693 }
694
695 return msg;
696
697 fail:
698 radius_msg_free(msg);
699 return NULL;
700}
701
702
703int radius_msg_add_eap(struct radius_msg *msg, const u8 *data, size_t data_len)
704{
705 const u8 *pos = data;
706 size_t left = data_len;
707
708 while (left > 0) {
709 int len;
710 if (left > RADIUS_MAX_ATTR_LEN)
711 len = RADIUS_MAX_ATTR_LEN;
712 else
713 len = left;
714
715 if (!radius_msg_add_attr(msg, RADIUS_ATTR_EAP_MESSAGE,
716 pos, len))
717 return 0;
718
719 pos += len;
720 left -= len;
721 }
722
723 return 1;
724}
725
726
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700727struct wpabuf * radius_msg_get_eap(struct radius_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700728{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700729 struct wpabuf *eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700730 size_t len, i;
731 struct radius_attr_hdr *attr;
732
733 if (msg == NULL)
734 return NULL;
735
736 len = 0;
737 for (i = 0; i < msg->attr_used; i++) {
738 attr = radius_get_attr_hdr(msg, i);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700739 if (attr->type == RADIUS_ATTR_EAP_MESSAGE &&
740 attr->length > sizeof(struct radius_attr_hdr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700741 len += attr->length - sizeof(struct radius_attr_hdr);
742 }
743
744 if (len == 0)
745 return NULL;
746
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700747 eap = wpabuf_alloc(len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748 if (eap == NULL)
749 return NULL;
750
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700751 for (i = 0; i < msg->attr_used; i++) {
752 attr = radius_get_attr_hdr(msg, i);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700753 if (attr->type == RADIUS_ATTR_EAP_MESSAGE &&
754 attr->length > sizeof(struct radius_attr_hdr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700755 int flen = attr->length - sizeof(*attr);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700756 wpabuf_put_data(eap, attr + 1, flen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700757 }
758 }
759
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700760 return eap;
761}
762
763
764int radius_msg_verify_msg_auth(struct radius_msg *msg, const u8 *secret,
765 size_t secret_len, const u8 *req_auth)
766{
767 u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
768 u8 orig_authenticator[16];
769 struct radius_attr_hdr *attr = NULL, *tmp;
770 size_t i;
771
772 for (i = 0; i < msg->attr_used; i++) {
773 tmp = radius_get_attr_hdr(msg, i);
774 if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
775 if (attr != NULL) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800776 wpa_printf(MSG_INFO, "Multiple Message-Authenticator attributes in RADIUS message");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700777 return 1;
778 }
779 attr = tmp;
780 }
781 }
782
783 if (attr == NULL) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800784 wpa_printf(MSG_INFO, "No Message-Authenticator attribute found");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700785 return 1;
786 }
787
788 os_memcpy(orig, attr + 1, MD5_MAC_LEN);
789 os_memset(attr + 1, 0, MD5_MAC_LEN);
790 if (req_auth) {
791 os_memcpy(orig_authenticator, msg->hdr->authenticator,
792 sizeof(orig_authenticator));
793 os_memcpy(msg->hdr->authenticator, req_auth,
794 sizeof(msg->hdr->authenticator));
795 }
796 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
797 wpabuf_len(msg->buf), auth);
798 os_memcpy(attr + 1, orig, MD5_MAC_LEN);
799 if (req_auth) {
800 os_memcpy(msg->hdr->authenticator, orig_authenticator,
801 sizeof(orig_authenticator));
802 }
803
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700804 if (os_memcmp_const(orig, auth, MD5_MAC_LEN) != 0) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800805 wpa_printf(MSG_INFO, "Invalid Message-Authenticator!");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700806 return 1;
807 }
808
809 return 0;
810}
811
812
813int radius_msg_verify(struct radius_msg *msg, const u8 *secret,
814 size_t secret_len, struct radius_msg *sent_msg, int auth)
815{
816 const u8 *addr[4];
817 size_t len[4];
818 u8 hash[MD5_MAC_LEN];
819
820 if (sent_msg == NULL) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800821 wpa_printf(MSG_INFO, "No matching Access-Request message found");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822 return 1;
823 }
824
825 if (auth &&
826 radius_msg_verify_msg_auth(msg, secret, secret_len,
827 sent_msg->hdr->authenticator)) {
828 return 1;
829 }
830
831 /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
832 addr[0] = (u8 *) msg->hdr;
833 len[0] = 1 + 1 + 2;
834 addr[1] = sent_msg->hdr->authenticator;
835 len[1] = MD5_MAC_LEN;
836 addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
837 len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
838 addr[3] = secret;
839 len[3] = secret_len;
840 md5_vector(4, addr, len, hash);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700841 if (os_memcmp_const(hash, msg->hdr->authenticator, MD5_MAC_LEN) != 0) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800842 wpa_printf(MSG_INFO, "Response Authenticator invalid!");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700843 return 1;
844 }
845
846 return 0;
847}
848
849
850int radius_msg_copy_attr(struct radius_msg *dst, struct radius_msg *src,
851 u8 type)
852{
853 struct radius_attr_hdr *attr;
854 size_t i;
855 int count = 0;
856
857 for (i = 0; i < src->attr_used; i++) {
858 attr = radius_get_attr_hdr(src, i);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700859 if (attr->type == type && attr->length >= sizeof(*attr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700860 if (!radius_msg_add_attr(dst, type, (u8 *) (attr + 1),
861 attr->length - sizeof(*attr)))
862 return -1;
863 count++;
864 }
865 }
866
867 return count;
868}
869
870
871/* Create Request Authenticator. The value should be unique over the lifetime
872 * of the shared secret between authenticator and authentication server.
873 * Use one-way MD5 hash calculated from current timestamp and some data given
874 * by the caller. */
875void radius_msg_make_authenticator(struct radius_msg *msg,
876 const u8 *data, size_t len)
877{
878 struct os_time tv;
879 long int l;
880 const u8 *addr[3];
881 size_t elen[3];
882
883 os_get_time(&tv);
884 l = os_random();
885 addr[0] = (u8 *) &tv;
886 elen[0] = sizeof(tv);
887 addr[1] = data;
888 elen[1] = len;
889 addr[2] = (u8 *) &l;
890 elen[2] = sizeof(l);
891 md5_vector(3, addr, elen, msg->hdr->authenticator);
892}
893
894
895/* Get Vendor-specific RADIUS Attribute from a parsed RADIUS message.
896 * Returns the Attribute payload and sets alen to indicate the length of the
897 * payload if a vendor attribute with subtype is found, otherwise returns NULL.
898 * The returned payload is allocated with os_malloc() and caller must free it
899 * by calling os_free().
900 */
901static u8 *radius_msg_get_vendor_attr(struct radius_msg *msg, u32 vendor,
902 u8 subtype, size_t *alen)
903{
904 u8 *data, *pos;
905 size_t i, len;
906
907 if (msg == NULL)
908 return NULL;
909
910 for (i = 0; i < msg->attr_used; i++) {
911 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
912 size_t left;
913 u32 vendor_id;
914 struct radius_attr_vendor *vhdr;
915
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700916 if (attr->type != RADIUS_ATTR_VENDOR_SPECIFIC ||
917 attr->length < sizeof(*attr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918 continue;
919
920 left = attr->length - sizeof(*attr);
921 if (left < 4)
922 continue;
923
924 pos = (u8 *) (attr + 1);
925
926 os_memcpy(&vendor_id, pos, 4);
927 pos += 4;
928 left -= 4;
929
930 if (ntohl(vendor_id) != vendor)
931 continue;
932
933 while (left >= sizeof(*vhdr)) {
934 vhdr = (struct radius_attr_vendor *) pos;
935 if (vhdr->vendor_length > left ||
936 vhdr->vendor_length < sizeof(*vhdr)) {
937 left = 0;
938 break;
939 }
940 if (vhdr->vendor_type != subtype) {
941 pos += vhdr->vendor_length;
942 left -= vhdr->vendor_length;
943 continue;
944 }
945
946 len = vhdr->vendor_length - sizeof(*vhdr);
947 data = os_malloc(len);
948 if (data == NULL)
949 return NULL;
950 os_memcpy(data, pos + sizeof(*vhdr), len);
951 if (alen)
952 *alen = len;
953 return data;
954 }
955 }
956
957 return NULL;
958}
959
960
961static u8 * decrypt_ms_key(const u8 *key, size_t len,
962 const u8 *req_authenticator,
963 const u8 *secret, size_t secret_len, size_t *reslen)
964{
965 u8 *plain, *ppos, *res;
966 const u8 *pos;
967 size_t left, plen;
968 u8 hash[MD5_MAC_LEN];
969 int i, first = 1;
970 const u8 *addr[3];
971 size_t elen[3];
972
973 /* key: 16-bit salt followed by encrypted key info */
974
975 if (len < 2 + 16)
976 return NULL;
977
978 pos = key + 2;
979 left = len - 2;
980 if (left % 16) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800981 wpa_printf(MSG_INFO, "Invalid ms key len %lu",
982 (unsigned long) left);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983 return NULL;
984 }
985
986 plen = left;
987 ppos = plain = os_malloc(plen);
988 if (plain == NULL)
989 return NULL;
990 plain[0] = 0;
991
992 while (left > 0) {
993 /* b(1) = MD5(Secret + Request-Authenticator + Salt)
994 * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
995
996 addr[0] = secret;
997 elen[0] = secret_len;
998 if (first) {
999 addr[1] = req_authenticator;
1000 elen[1] = MD5_MAC_LEN;
1001 addr[2] = key;
1002 elen[2] = 2; /* Salt */
1003 } else {
1004 addr[1] = pos - MD5_MAC_LEN;
1005 elen[1] = MD5_MAC_LEN;
1006 }
1007 md5_vector(first ? 3 : 2, addr, elen, hash);
1008 first = 0;
1009
1010 for (i = 0; i < MD5_MAC_LEN; i++)
1011 *ppos++ = *pos++ ^ hash[i];
1012 left -= MD5_MAC_LEN;
1013 }
1014
1015 if (plain[0] == 0 || plain[0] > plen - 1) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001016 wpa_printf(MSG_INFO, "Failed to decrypt MPPE key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001017 os_free(plain);
1018 return NULL;
1019 }
1020
1021 res = os_malloc(plain[0]);
1022 if (res == NULL) {
1023 os_free(plain);
1024 return NULL;
1025 }
1026 os_memcpy(res, plain + 1, plain[0]);
1027 if (reslen)
1028 *reslen = plain[0];
1029 os_free(plain);
1030 return res;
1031}
1032
1033
1034static void encrypt_ms_key(const u8 *key, size_t key_len, u16 salt,
1035 const u8 *req_authenticator,
1036 const u8 *secret, size_t secret_len,
1037 u8 *ebuf, size_t *elen)
1038{
1039 int i, len, first = 1;
1040 u8 hash[MD5_MAC_LEN], saltbuf[2], *pos;
1041 const u8 *addr[3];
1042 size_t _len[3];
1043
1044 WPA_PUT_BE16(saltbuf, salt);
1045
1046 len = 1 + key_len;
1047 if (len & 0x0f) {
1048 len = (len & 0xf0) + 16;
1049 }
1050 os_memset(ebuf, 0, len);
1051 ebuf[0] = key_len;
1052 os_memcpy(ebuf + 1, key, key_len);
1053
1054 *elen = len;
1055
1056 pos = ebuf;
1057 while (len > 0) {
1058 /* b(1) = MD5(Secret + Request-Authenticator + Salt)
1059 * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
1060 addr[0] = secret;
1061 _len[0] = secret_len;
1062 if (first) {
1063 addr[1] = req_authenticator;
1064 _len[1] = MD5_MAC_LEN;
1065 addr[2] = saltbuf;
1066 _len[2] = sizeof(saltbuf);
1067 } else {
1068 addr[1] = pos - MD5_MAC_LEN;
1069 _len[1] = MD5_MAC_LEN;
1070 }
1071 md5_vector(first ? 3 : 2, addr, _len, hash);
1072 first = 0;
1073
1074 for (i = 0; i < MD5_MAC_LEN; i++)
1075 *pos++ ^= hash[i];
1076
1077 len -= MD5_MAC_LEN;
1078 }
1079}
1080
1081
1082struct radius_ms_mppe_keys *
1083radius_msg_get_ms_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
1084 const u8 *secret, size_t secret_len)
1085{
1086 u8 *key;
1087 size_t keylen;
1088 struct radius_ms_mppe_keys *keys;
1089
1090 if (msg == NULL || sent_msg == NULL)
1091 return NULL;
1092
1093 keys = os_zalloc(sizeof(*keys));
1094 if (keys == NULL)
1095 return NULL;
1096
1097 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
1098 RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY,
1099 &keylen);
1100 if (key) {
1101 keys->send = decrypt_ms_key(key, keylen,
1102 sent_msg->hdr->authenticator,
1103 secret, secret_len,
1104 &keys->send_len);
1105 os_free(key);
1106 }
1107
1108 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
1109 RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY,
1110 &keylen);
1111 if (key) {
1112 keys->recv = decrypt_ms_key(key, keylen,
1113 sent_msg->hdr->authenticator,
1114 secret, secret_len,
1115 &keys->recv_len);
1116 os_free(key);
1117 }
1118
1119 return keys;
1120}
1121
1122
1123struct radius_ms_mppe_keys *
1124radius_msg_get_cisco_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
1125 const u8 *secret, size_t secret_len)
1126{
1127 u8 *key;
1128 size_t keylen;
1129 struct radius_ms_mppe_keys *keys;
1130
1131 if (msg == NULL || sent_msg == NULL)
1132 return NULL;
1133
1134 keys = os_zalloc(sizeof(*keys));
1135 if (keys == NULL)
1136 return NULL;
1137
1138 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_CISCO,
1139 RADIUS_CISCO_AV_PAIR, &keylen);
1140 if (key && keylen == 51 &&
1141 os_memcmp(key, "leap:session-key=", 17) == 0) {
1142 keys->recv = decrypt_ms_key(key + 17, keylen - 17,
1143 sent_msg->hdr->authenticator,
1144 secret, secret_len,
1145 &keys->recv_len);
1146 }
1147 os_free(key);
1148
1149 return keys;
1150}
1151
1152
1153int radius_msg_add_mppe_keys(struct radius_msg *msg,
1154 const u8 *req_authenticator,
1155 const u8 *secret, size_t secret_len,
1156 const u8 *send_key, size_t send_key_len,
1157 const u8 *recv_key, size_t recv_key_len)
1158{
1159 struct radius_attr_hdr *attr;
1160 u32 vendor_id = htonl(RADIUS_VENDOR_ID_MICROSOFT);
1161 u8 *buf;
1162 struct radius_attr_vendor *vhdr;
1163 u8 *pos;
1164 size_t elen;
1165 int hlen;
1166 u16 salt;
1167
1168 hlen = sizeof(vendor_id) + sizeof(*vhdr) + 2;
1169
1170 /* MS-MPPE-Send-Key */
1171 buf = os_malloc(hlen + send_key_len + 16);
1172 if (buf == NULL) {
1173 return 0;
1174 }
1175 pos = buf;
1176 os_memcpy(pos, &vendor_id, sizeof(vendor_id));
1177 pos += sizeof(vendor_id);
1178 vhdr = (struct radius_attr_vendor *) pos;
1179 vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY;
1180 pos = (u8 *) (vhdr + 1);
1181 salt = os_random() | 0x8000;
1182 WPA_PUT_BE16(pos, salt);
1183 pos += 2;
1184 encrypt_ms_key(send_key, send_key_len, salt, req_authenticator, secret,
1185 secret_len, pos, &elen);
1186 vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
1187
1188 attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1189 buf, hlen + elen);
1190 os_free(buf);
1191 if (attr == NULL) {
1192 return 0;
1193 }
1194
1195 /* MS-MPPE-Recv-Key */
1196 buf = os_malloc(hlen + send_key_len + 16);
1197 if (buf == NULL) {
1198 return 0;
1199 }
1200 pos = buf;
1201 os_memcpy(pos, &vendor_id, sizeof(vendor_id));
1202 pos += sizeof(vendor_id);
1203 vhdr = (struct radius_attr_vendor *) pos;
1204 vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY;
1205 pos = (u8 *) (vhdr + 1);
1206 salt ^= 1;
1207 WPA_PUT_BE16(pos, salt);
1208 pos += 2;
1209 encrypt_ms_key(recv_key, recv_key_len, salt, req_authenticator, secret,
1210 secret_len, pos, &elen);
1211 vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
1212
1213 attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1214 buf, hlen + elen);
1215 os_free(buf);
1216 if (attr == NULL) {
1217 return 0;
1218 }
1219
1220 return 1;
1221}
1222
1223
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001224int radius_msg_add_wfa(struct radius_msg *msg, u8 subtype, const u8 *data,
1225 size_t len)
1226{
1227 struct radius_attr_hdr *attr;
1228 u8 *buf, *pos;
1229 size_t alen;
1230
1231 alen = 4 + 2 + len;
1232 buf = os_malloc(alen);
1233 if (buf == NULL)
1234 return 0;
1235 pos = buf;
1236 WPA_PUT_BE32(pos, RADIUS_VENDOR_ID_WFA);
1237 pos += 4;
1238 *pos++ = subtype;
1239 *pos++ = 2 + len;
1240 os_memcpy(pos, data, len);
1241 attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1242 buf, alen);
1243 os_free(buf);
1244 if (attr == NULL)
1245 return 0;
1246
1247 return 1;
1248}
1249
1250
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001251int radius_user_password_hide(struct radius_msg *msg,
1252 const u8 *data, size_t data_len,
1253 const u8 *secret, size_t secret_len,
1254 u8 *buf, size_t buf_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255{
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001256 size_t padlen, i, pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001257 const u8 *addr[2];
1258 size_t len[2];
1259 u8 hash[16];
1260
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001261 if (data_len + 16 > buf_len)
1262 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001263
1264 os_memcpy(buf, data, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265
1266 padlen = data_len % 16;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001267 if (padlen && data_len < buf_len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001268 padlen = 16 - padlen;
1269 os_memset(buf + data_len, 0, padlen);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001270 buf_len = data_len + padlen;
1271 } else {
1272 buf_len = data_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001273 }
1274
1275 addr[0] = secret;
1276 len[0] = secret_len;
1277 addr[1] = msg->hdr->authenticator;
1278 len[1] = 16;
1279 md5_vector(2, addr, len, hash);
1280
1281 for (i = 0; i < 16; i++)
1282 buf[i] ^= hash[i];
1283 pos = 16;
1284
1285 while (pos < buf_len) {
1286 addr[0] = secret;
1287 len[0] = secret_len;
1288 addr[1] = &buf[pos - 16];
1289 len[1] = 16;
1290 md5_vector(2, addr, len, hash);
1291
1292 for (i = 0; i < 16; i++)
1293 buf[pos + i] ^= hash[i];
1294
1295 pos += 16;
1296 }
1297
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001298 return buf_len;
1299}
1300
1301
1302/* Add User-Password attribute to a RADIUS message and encrypt it as specified
1303 * in RFC 2865, Chap. 5.2 */
1304struct radius_attr_hdr *
1305radius_msg_add_attr_user_password(struct radius_msg *msg,
1306 const u8 *data, size_t data_len,
1307 const u8 *secret, size_t secret_len)
1308{
1309 u8 buf[128];
1310 int res;
1311
1312 res = radius_user_password_hide(msg, data, data_len,
1313 secret, secret_len, buf, sizeof(buf));
1314 if (res < 0)
1315 return NULL;
1316
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001317 return radius_msg_add_attr(msg, RADIUS_ATTR_USER_PASSWORD,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001318 buf, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001319}
1320
1321
1322int radius_msg_get_attr(struct radius_msg *msg, u8 type, u8 *buf, size_t len)
1323{
1324 struct radius_attr_hdr *attr = NULL, *tmp;
1325 size_t i, dlen;
1326
1327 for (i = 0; i < msg->attr_used; i++) {
1328 tmp = radius_get_attr_hdr(msg, i);
1329 if (tmp->type == type) {
1330 attr = tmp;
1331 break;
1332 }
1333 }
1334
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001335 if (!attr || attr->length < sizeof(*attr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001336 return -1;
1337
1338 dlen = attr->length - sizeof(*attr);
1339 if (buf)
1340 os_memcpy(buf, (attr + 1), dlen > len ? len : dlen);
1341 return dlen;
1342}
1343
1344
1345int radius_msg_get_attr_ptr(struct radius_msg *msg, u8 type, u8 **buf,
1346 size_t *len, const u8 *start)
1347{
1348 size_t i;
1349 struct radius_attr_hdr *attr = NULL, *tmp;
1350
1351 for (i = 0; i < msg->attr_used; i++) {
1352 tmp = radius_get_attr_hdr(msg, i);
1353 if (tmp->type == type &&
1354 (start == NULL || (u8 *) tmp > start)) {
1355 attr = tmp;
1356 break;
1357 }
1358 }
1359
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001360 if (!attr || attr->length < sizeof(*attr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001361 return -1;
1362
1363 *buf = (u8 *) (attr + 1);
1364 *len = attr->length - sizeof(*attr);
1365 return 0;
1366}
1367
1368
1369int radius_msg_count_attr(struct radius_msg *msg, u8 type, int min_len)
1370{
1371 size_t i;
1372 int count;
1373
1374 for (count = 0, i = 0; i < msg->attr_used; i++) {
1375 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
1376 if (attr->type == type &&
1377 attr->length >= sizeof(struct radius_attr_hdr) + min_len)
1378 count++;
1379 }
1380
1381 return count;
1382}
1383
1384
1385struct radius_tunnel_attrs {
1386 int tag_used;
1387 int type; /* Tunnel-Type */
1388 int medium_type; /* Tunnel-Medium-Type */
1389 int vlanid;
1390};
1391
1392
1393/**
1394 * radius_msg_get_vlanid - Parse RADIUS attributes for VLAN tunnel information
1395 * @msg: RADIUS message
1396 * Returns: VLAN ID for the first tunnel configuration of -1 if none is found
1397 */
1398int radius_msg_get_vlanid(struct radius_msg *msg)
1399{
1400 struct radius_tunnel_attrs tunnel[RADIUS_TUNNEL_TAGS], *tun;
1401 size_t i;
1402 struct radius_attr_hdr *attr = NULL;
1403 const u8 *data;
1404 char buf[10];
1405 size_t dlen;
1406
1407 os_memset(&tunnel, 0, sizeof(tunnel));
1408
1409 for (i = 0; i < msg->attr_used; i++) {
1410 attr = radius_get_attr_hdr(msg, i);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001411 if (attr->length < sizeof(*attr))
1412 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001413 data = (const u8 *) (attr + 1);
1414 dlen = attr->length - sizeof(*attr);
1415 if (attr->length < 3)
1416 continue;
1417 if (data[0] >= RADIUS_TUNNEL_TAGS)
1418 tun = &tunnel[0];
1419 else
1420 tun = &tunnel[data[0]];
1421
1422 switch (attr->type) {
1423 case RADIUS_ATTR_TUNNEL_TYPE:
1424 if (attr->length != 6)
1425 break;
1426 tun->tag_used++;
1427 tun->type = WPA_GET_BE24(data + 1);
1428 break;
1429 case RADIUS_ATTR_TUNNEL_MEDIUM_TYPE:
1430 if (attr->length != 6)
1431 break;
1432 tun->tag_used++;
1433 tun->medium_type = WPA_GET_BE24(data + 1);
1434 break;
1435 case RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID:
1436 if (data[0] < RADIUS_TUNNEL_TAGS) {
1437 data++;
1438 dlen--;
1439 }
1440 if (dlen >= sizeof(buf))
1441 break;
1442 os_memcpy(buf, data, dlen);
1443 buf[dlen] = '\0';
1444 tun->tag_used++;
1445 tun->vlanid = atoi(buf);
1446 break;
1447 }
1448 }
1449
1450 for (i = 0; i < RADIUS_TUNNEL_TAGS; i++) {
1451 tun = &tunnel[i];
1452 if (tun->tag_used &&
1453 tun->type == RADIUS_TUNNEL_TYPE_VLAN &&
1454 tun->medium_type == RADIUS_TUNNEL_MEDIUM_TYPE_802 &&
1455 tun->vlanid > 0)
1456 return tun->vlanid;
1457 }
1458
1459 return -1;
1460}
1461
1462
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001463/**
1464 * radius_msg_get_tunnel_password - Parse RADIUS attribute Tunnel-Password
1465 * @msg: Received RADIUS message
1466 * @keylen: Length of returned password
1467 * @secret: RADIUS shared secret
1468 * @secret_len: Length of secret
1469 * @sent_msg: Sent RADIUS message
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001470 * @n: Number of password attribute to return (starting with 0)
1471 * Returns: Pointer to n-th password (free with os_free) or %NULL
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001472 */
1473char * radius_msg_get_tunnel_password(struct radius_msg *msg, int *keylen,
1474 const u8 *secret, size_t secret_len,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001475 struct radius_msg *sent_msg, size_t n)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001476{
1477 u8 *buf = NULL;
1478 size_t buflen;
1479 const u8 *salt;
1480 u8 *str;
1481 const u8 *addr[3];
1482 size_t len[3];
1483 u8 hash[16];
1484 u8 *pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001485 size_t i, j = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001486 struct radius_attr_hdr *attr;
1487 const u8 *data;
1488 size_t dlen;
1489 const u8 *fdata = NULL; /* points to found item */
1490 size_t fdlen = -1;
1491 char *ret = NULL;
1492
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001493 /* find n-th valid Tunnel-Password attribute */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001494 for (i = 0; i < msg->attr_used; i++) {
1495 attr = radius_get_attr_hdr(msg, i);
1496 if (attr == NULL ||
1497 attr->type != RADIUS_ATTR_TUNNEL_PASSWORD) {
1498 continue;
1499 }
1500 if (attr->length <= 5)
1501 continue;
1502 data = (const u8 *) (attr + 1);
1503 dlen = attr->length - sizeof(*attr);
1504 if (dlen <= 3 || dlen % 16 != 3)
1505 continue;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001506 j++;
1507 if (j <= n)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001508 continue;
1509
1510 fdata = data;
1511 fdlen = dlen;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001512 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001513 }
1514 if (fdata == NULL)
1515 goto out;
1516
1517 /* alloc writable memory for decryption */
1518 buf = os_malloc(fdlen);
1519 if (buf == NULL)
1520 goto out;
1521 os_memcpy(buf, fdata, fdlen);
1522 buflen = fdlen;
1523
1524 /* init pointers */
1525 salt = buf + 1;
1526 str = buf + 3;
1527
1528 /* decrypt blocks */
1529 pos = buf + buflen - 16; /* last block */
1530 while (pos >= str + 16) { /* all but the first block */
1531 addr[0] = secret;
1532 len[0] = secret_len;
1533 addr[1] = pos - 16;
1534 len[1] = 16;
1535 md5_vector(2, addr, len, hash);
1536
1537 for (i = 0; i < 16; i++)
1538 pos[i] ^= hash[i];
1539
1540 pos -= 16;
1541 }
1542
1543 /* decrypt first block */
1544 if (str != pos)
1545 goto out;
1546 addr[0] = secret;
1547 len[0] = secret_len;
1548 addr[1] = sent_msg->hdr->authenticator;
1549 len[1] = 16;
1550 addr[2] = salt;
1551 len[2] = 2;
1552 md5_vector(3, addr, len, hash);
1553
1554 for (i = 0; i < 16; i++)
1555 pos[i] ^= hash[i];
1556
1557 /* derive plaintext length from first subfield */
1558 *keylen = (unsigned char) str[0];
1559 if ((u8 *) (str + *keylen) >= (u8 *) (buf + buflen)) {
1560 /* decryption error - invalid key length */
1561 goto out;
1562 }
1563 if (*keylen == 0) {
1564 /* empty password */
1565 goto out;
1566 }
1567
1568 /* copy passphrase into new buffer */
1569 ret = os_malloc(*keylen);
1570 if (ret)
1571 os_memcpy(ret, str + 1, *keylen);
1572
1573out:
1574 /* return new buffer */
1575 os_free(buf);
1576 return ret;
1577}
1578
1579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580void radius_free_class(struct radius_class_data *c)
1581{
1582 size_t i;
1583 if (c == NULL)
1584 return;
1585 for (i = 0; i < c->count; i++)
1586 os_free(c->attr[i].data);
1587 os_free(c->attr);
1588 c->attr = NULL;
1589 c->count = 0;
1590}
1591
1592
1593int radius_copy_class(struct radius_class_data *dst,
1594 const struct radius_class_data *src)
1595{
1596 size_t i;
1597
1598 if (src->attr == NULL)
1599 return 0;
1600
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001601 dst->attr = os_calloc(src->count, sizeof(struct radius_attr_data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001602 if (dst->attr == NULL)
1603 return -1;
1604
1605 dst->count = 0;
1606
1607 for (i = 0; i < src->count; i++) {
1608 dst->attr[i].data = os_malloc(src->attr[i].len);
1609 if (dst->attr[i].data == NULL)
1610 break;
1611 dst->count++;
1612 os_memcpy(dst->attr[i].data, src->attr[i].data,
1613 src->attr[i].len);
1614 dst->attr[i].len = src->attr[i].len;
1615 }
1616
1617 return 0;
1618}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001619
1620
1621u8 radius_msg_find_unlisted_attr(struct radius_msg *msg, u8 *attrs)
1622{
1623 size_t i, j;
1624 struct radius_attr_hdr *attr;
1625
1626 for (i = 0; i < msg->attr_used; i++) {
1627 attr = radius_get_attr_hdr(msg, i);
1628
1629 for (j = 0; attrs[j]; j++) {
1630 if (attr->type == attrs[j])
1631 break;
1632 }
1633
1634 if (attrs[j] == 0)
1635 return attr->type; /* unlisted attr */
1636 }
1637
1638 return 0;
1639}