2 * hostapd / RADIUS message processing
3 * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
23 struct radius_msg *radius_msg_new(u8 code, u8 identifier)
25 struct radius_msg *msg;
27 msg = os_malloc(sizeof(*msg));
31 if (radius_msg_initialize(msg, RADIUS_DEFAULT_MSG_SIZE)) {
36 radius_msg_set_hdr(msg, code, identifier);
42 int radius_msg_initialize(struct radius_msg *msg, size_t init_len)
44 if (msg == NULL || init_len < sizeof(struct radius_hdr))
47 os_memset(msg, 0, sizeof(*msg));
48 msg->buf = wpa_zalloc(init_len);
52 msg->buf_size = init_len;
53 msg->hdr = (struct radius_hdr *) msg->buf;
54 msg->buf_used = sizeof(*msg->hdr);
57 os_malloc(RADIUS_DEFAULT_ATTR_COUNT * sizeof(*msg->attrs));
58 if (msg->attrs == NULL) {
65 msg->attr_size = RADIUS_DEFAULT_ATTR_COUNT;
72 void radius_msg_set_hdr(struct radius_msg *msg, u8 code, u8 identifier)
74 msg->hdr->code = code;
75 msg->hdr->identifier = identifier;
79 void radius_msg_free(struct radius_msg *msg)
81 if (msg->buf != NULL) {
86 msg->buf_size = msg->buf_used = 0;
88 if (msg->attrs != NULL) {
92 msg->attr_size = msg->attr_used = 0;
96 static const char *radius_code_string(u8 code)
99 case RADIUS_CODE_ACCESS_REQUEST: return "Access-Request";
100 case RADIUS_CODE_ACCESS_ACCEPT: return "Access-Accept";
101 case RADIUS_CODE_ACCESS_REJECT: return "Access-Reject";
102 case RADIUS_CODE_ACCOUNTING_REQUEST: return "Accounting-Request";
103 case RADIUS_CODE_ACCOUNTING_RESPONSE: return "Accounting-Response";
104 case RADIUS_CODE_ACCESS_CHALLENGE: return "Access-Challenge";
105 case RADIUS_CODE_STATUS_SERVER: return "Status-Server";
106 case RADIUS_CODE_STATUS_CLIENT: return "Status-Client";
107 case RADIUS_CODE_RESERVED: return "Reserved";
108 default: return "?Unknown?";
113 struct radius_attr_type {
117 RADIUS_ATTR_UNDIST, RADIUS_ATTR_TEXT, RADIUS_ATTR_IP,
118 RADIUS_ATTR_HEXDUMP, RADIUS_ATTR_INT32, RADIUS_ATTR_IPV6
122 static struct radius_attr_type radius_attrs[] =
124 { RADIUS_ATTR_USER_NAME, "User-Name", RADIUS_ATTR_TEXT },
125 { RADIUS_ATTR_USER_PASSWORD, "User-Password", RADIUS_ATTR_UNDIST },
126 { RADIUS_ATTR_NAS_IP_ADDRESS, "NAS-IP-Address", RADIUS_ATTR_IP },
127 { RADIUS_ATTR_NAS_PORT, "NAS-Port", RADIUS_ATTR_INT32 },
128 { RADIUS_ATTR_FRAMED_MTU, "Framed-MTU", RADIUS_ATTR_INT32 },
129 { RADIUS_ATTR_STATE, "State", RADIUS_ATTR_UNDIST },
130 { RADIUS_ATTR_CLASS, "Class", RADIUS_ATTR_UNDIST },
131 { RADIUS_ATTR_VENDOR_SPECIFIC, "Vendor-Specific", RADIUS_ATTR_UNDIST },
132 { RADIUS_ATTR_SESSION_TIMEOUT, "Session-Timeout", RADIUS_ATTR_INT32 },
133 { RADIUS_ATTR_IDLE_TIMEOUT, "Idle-Timeout", RADIUS_ATTR_INT32 },
134 { RADIUS_ATTR_TERMINATION_ACTION, "Termination-Action",
136 { RADIUS_ATTR_CALLED_STATION_ID, "Called-Station-Id",
138 { RADIUS_ATTR_CALLING_STATION_ID, "Calling-Station-Id",
140 { RADIUS_ATTR_NAS_IDENTIFIER, "NAS-Identifier", RADIUS_ATTR_TEXT },
141 { RADIUS_ATTR_PROXY_STATE, "Proxy-State", RADIUS_ATTR_UNDIST },
142 { RADIUS_ATTR_ACCT_STATUS_TYPE, "Acct-Status-Type",
144 { RADIUS_ATTR_ACCT_DELAY_TIME, "Acct-Delay-Time", RADIUS_ATTR_INT32 },
145 { RADIUS_ATTR_ACCT_INPUT_OCTETS, "Acct-Input-Octets",
147 { RADIUS_ATTR_ACCT_OUTPUT_OCTETS, "Acct-Output-Octets",
149 { RADIUS_ATTR_ACCT_SESSION_ID, "Acct-Session-Id", RADIUS_ATTR_TEXT },
150 { RADIUS_ATTR_ACCT_AUTHENTIC, "Acct-Authentic", RADIUS_ATTR_INT32 },
151 { RADIUS_ATTR_ACCT_SESSION_TIME, "Acct-Session-Time",
153 { RADIUS_ATTR_ACCT_INPUT_PACKETS, "Acct-Input-Packets",
155 { RADIUS_ATTR_ACCT_OUTPUT_PACKETS, "Acct-Output-Packets",
157 { RADIUS_ATTR_ACCT_TERMINATE_CAUSE, "Acct-Terminate-Cause",
159 { RADIUS_ATTR_ACCT_MULTI_SESSION_ID, "Acct-Multi-Session-Id",
161 { RADIUS_ATTR_ACCT_LINK_COUNT, "Acct-Link-Count", RADIUS_ATTR_INT32 },
162 { RADIUS_ATTR_ACCT_INPUT_GIGAWORDS, "Acct-Input-Gigawords",
164 { RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS, "Acct-Output-Gigawords",
166 { RADIUS_ATTR_EVENT_TIMESTAMP, "Event-Timestamp",
168 { RADIUS_ATTR_NAS_PORT_TYPE, "NAS-Port-Type", RADIUS_ATTR_INT32 },
169 { RADIUS_ATTR_TUNNEL_TYPE, "Tunnel-Type", RADIUS_ATTR_HEXDUMP },
170 { RADIUS_ATTR_TUNNEL_MEDIUM_TYPE, "Tunnel-Medium-Type",
171 RADIUS_ATTR_HEXDUMP },
172 { RADIUS_ATTR_CONNECT_INFO, "Connect-Info", RADIUS_ATTR_TEXT },
173 { RADIUS_ATTR_EAP_MESSAGE, "EAP-Message", RADIUS_ATTR_UNDIST },
174 { RADIUS_ATTR_MESSAGE_AUTHENTICATOR, "Message-Authenticator",
175 RADIUS_ATTR_UNDIST },
176 { RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID, "Tunnel-Private-Group-Id",
177 RADIUS_ATTR_HEXDUMP },
178 { RADIUS_ATTR_ACCT_INTERIM_INTERVAL, "Acct-Interim-Interval",
180 { RADIUS_ATTR_NAS_IPV6_ADDRESS, "NAS-IPv6-Address", RADIUS_ATTR_IPV6 },
182 #define RADIUS_ATTRS (sizeof(radius_attrs) / sizeof(radius_attrs[0]))
185 static struct radius_attr_type *radius_get_attr_type(u8 type)
189 for (i = 0; i < RADIUS_ATTRS; i++) {
190 if (type == radius_attrs[i].type)
191 return &radius_attrs[i];
198 static void print_char(char c)
200 if (c >= 32 && c < 127)
207 static void radius_msg_dump_attr(struct radius_attr_hdr *hdr)
209 struct radius_attr_type *attr;
213 attr = radius_get_attr_type(hdr->type);
215 printf(" Attribute %d (%s) length=%d\n",
216 hdr->type, attr ? attr->name : "?Unknown?", hdr->length);
221 len = hdr->length - sizeof(struct radius_attr_hdr);
222 pos = (unsigned char *) (hdr + 1);
224 switch (attr->data_type) {
225 case RADIUS_ATTR_TEXT:
227 for (i = 0; i < len; i++)
235 os_memcpy(&addr, pos, 4);
236 printf(" Value: %s\n", inet_ntoa(addr));
238 printf(" Invalid IP address length %d\n", len);
242 case RADIUS_ATTR_IPV6:
246 struct in6_addr *addr = (struct in6_addr *) pos;
247 atxt = inet_ntop(AF_INET6, addr, buf, sizeof(buf));
248 printf(" Value: %s\n", atxt ? atxt : "?");
250 printf(" Invalid IPv6 address length %d\n", len);
252 #endif /* CONFIG_IPV6 */
254 case RADIUS_ATTR_HEXDUMP:
255 case RADIUS_ATTR_UNDIST:
257 for (i = 0; i < len; i++)
258 printf(" %02x", pos[i]);
262 case RADIUS_ATTR_INT32:
264 printf(" Value: %u\n", WPA_GET_BE32(pos));
266 printf(" Invalid INT32 length %d\n", len);
275 void radius_msg_dump(struct radius_msg *msg)
279 printf("RADIUS message: code=%d (%s) identifier=%d length=%d\n",
280 msg->hdr->code, radius_code_string(msg->hdr->code),
281 msg->hdr->identifier, ntohs(msg->hdr->length));
283 for (i = 0; i < msg->attr_used; i++) {
284 radius_msg_dump_attr(msg->attrs[i]);
289 int radius_msg_finish(struct radius_msg *msg, u8 *secret, size_t secret_len)
292 u8 auth[MD5_MAC_LEN];
293 struct radius_attr_hdr *attr;
295 os_memset(auth, 0, MD5_MAC_LEN);
296 attr = radius_msg_add_attr(msg,
297 RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
300 printf("WARNING: Could not add "
301 "Message-Authenticator\n");
304 msg->hdr->length = htons(msg->buf_used);
305 hmac_md5(secret, secret_len, msg->buf, msg->buf_used,
308 msg->hdr->length = htons(msg->buf_used);
310 if (msg->buf_used > 0xffff) {
311 printf("WARNING: too long RADIUS message (%lu)\n",
312 (unsigned long) msg->buf_used);
319 int radius_msg_finish_srv(struct radius_msg *msg, const u8 *secret,
320 size_t secret_len, const u8 *req_authenticator)
322 u8 auth[MD5_MAC_LEN];
323 struct radius_attr_hdr *attr;
327 os_memset(auth, 0, MD5_MAC_LEN);
328 attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
331 printf("WARNING: Could not add Message-Authenticator\n");
334 msg->hdr->length = htons(msg->buf_used);
335 os_memcpy(msg->hdr->authenticator, req_authenticator,
336 sizeof(msg->hdr->authenticator));
337 hmac_md5(secret, secret_len, msg->buf, msg->buf_used,
340 /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
341 addr[0] = (u8 *) msg->hdr;
343 addr[1] = req_authenticator;
344 len[1] = MD5_MAC_LEN;
345 addr[2] = (u8 *) (msg->hdr + 1);
346 len[2] = msg->buf_used - sizeof(*msg->hdr);
349 md5_vector(4, addr, len, msg->hdr->authenticator);
351 if (msg->buf_used > 0xffff) {
352 printf("WARNING: too long RADIUS message (%lu)\n",
353 (unsigned long) msg->buf_used);
360 void radius_msg_finish_acct(struct radius_msg *msg, u8 *secret,
366 msg->hdr->length = htons(msg->buf_used);
367 os_memset(msg->hdr->authenticator, 0, MD5_MAC_LEN);
369 len[0] = msg->buf_used;
372 md5_vector(2, addr, len, msg->hdr->authenticator);
374 if (msg->buf_used > 0xffff) {
375 printf("WARNING: too long RADIUS messages (%lu)\n",
376 (unsigned long) msg->buf_used);
381 static int radius_msg_add_attr_to_array(struct radius_msg *msg,
382 struct radius_attr_hdr *attr)
384 if (msg->attr_used >= msg->attr_size) {
385 struct radius_attr_hdr **nattrs;
386 int nlen = msg->attr_size * 2;
388 nattrs = os_realloc(msg->attrs, nlen * sizeof(*msg->attrs));
393 msg->attr_size = nlen;
396 msg->attrs[msg->attr_used++] = attr;
402 struct radius_attr_hdr *radius_msg_add_attr(struct radius_msg *msg, u8 type,
403 const u8 *data, size_t data_len)
406 struct radius_attr_hdr *attr;
408 if (data_len > RADIUS_MAX_ATTR_LEN) {
409 printf("radius_msg_add_attr: too long attribute (%lu bytes)\n",
410 (unsigned long) data_len);
414 buf_needed = msg->buf_used + sizeof(*attr) + data_len;
416 if (msg->buf_size < buf_needed) {
417 /* allocate more space for message buffer */
419 size_t i, nlen = msg->buf_size;
422 while (nlen < buf_needed)
424 nbuf = os_realloc(msg->buf, nlen);
427 diff = nbuf - msg->buf;
429 msg->hdr = (struct radius_hdr *) msg->buf;
430 /* adjust attr pointers to match with the new buffer */
431 for (i = 0; i < msg->attr_used; i++)
432 msg->attrs[i] = (struct radius_attr_hdr *)
433 (((u8 *) msg->attrs[i]) + diff);
434 os_memset(msg->buf + msg->buf_size, 0, nlen - msg->buf_size);
435 msg->buf_size = nlen;
438 attr = (struct radius_attr_hdr *) (msg->buf + msg->buf_used);
440 attr->length = sizeof(*attr) + data_len;
442 os_memcpy(attr + 1, data, data_len);
444 msg->buf_used += sizeof(*attr) + data_len;
446 if (radius_msg_add_attr_to_array(msg, attr))
453 struct radius_msg *radius_msg_parse(const u8 *data, size_t len)
455 struct radius_msg *msg;
456 struct radius_hdr *hdr;
457 struct radius_attr_hdr *attr;
459 unsigned char *pos, *end;
461 if (data == NULL || len < sizeof(*hdr))
464 hdr = (struct radius_hdr *) data;
466 msg_len = ntohs(hdr->length);
467 if (msg_len < sizeof(*hdr) || msg_len > len) {
468 printf("Invalid RADIUS message length\n");
473 printf("Ignored %lu extra bytes after RADIUS message\n",
474 (unsigned long) len - msg_len);
477 msg = os_malloc(sizeof(*msg));
481 if (radius_msg_initialize(msg, msg_len)) {
486 os_memcpy(msg->buf, data, msg_len);
487 msg->buf_size = msg->buf_used = msg_len;
489 /* parse attributes */
490 pos = (unsigned char *) (msg->hdr + 1);
491 end = msg->buf + msg->buf_used;
493 if ((size_t) (end - pos) < sizeof(*attr))
496 attr = (struct radius_attr_hdr *) pos;
498 if (pos + attr->length > end || attr->length < sizeof(*attr))
501 /* TODO: check that attr->length is suitable for attr->type */
503 if (radius_msg_add_attr_to_array(msg, attr))
512 radius_msg_free(msg);
518 int radius_msg_add_eap(struct radius_msg *msg, const u8 *data, size_t data_len)
520 const u8 *pos = data;
521 size_t left = data_len;
525 if (left > RADIUS_MAX_ATTR_LEN)
526 len = RADIUS_MAX_ATTR_LEN;
530 if (!radius_msg_add_attr(msg, RADIUS_ATTR_EAP_MESSAGE,
542 u8 *radius_msg_get_eap(struct radius_msg *msg, size_t *eap_len)
551 for (i = 0; i < msg->attr_used; i++) {
552 if (msg->attrs[i]->type == RADIUS_ATTR_EAP_MESSAGE)
553 len += msg->attrs[i]->length -
554 sizeof(struct radius_attr_hdr);
560 eap = os_malloc(len);
565 for (i = 0; i < msg->attr_used; i++) {
566 if (msg->attrs[i]->type == RADIUS_ATTR_EAP_MESSAGE) {
567 struct radius_attr_hdr *attr = msg->attrs[i];
568 int flen = attr->length - sizeof(*attr);
569 os_memcpy(pos, attr + 1, flen);
581 int radius_msg_verify_msg_auth(struct radius_msg *msg, const u8 *secret,
582 size_t secret_len, const u8 *req_auth)
584 u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
585 u8 orig_authenticator[16];
586 struct radius_attr_hdr *attr = NULL;
589 for (i = 0; i < msg->attr_used; i++) {
590 if (msg->attrs[i]->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
592 printf("Multiple Message-Authenticator "
593 "attributes in RADIUS message\n");
596 attr = msg->attrs[i];
601 printf("No Message-Authenticator attribute found\n");
605 os_memcpy(orig, attr + 1, MD5_MAC_LEN);
606 os_memset(attr + 1, 0, MD5_MAC_LEN);
608 os_memcpy(orig_authenticator, msg->hdr->authenticator,
609 sizeof(orig_authenticator));
610 os_memcpy(msg->hdr->authenticator, req_auth,
611 sizeof(msg->hdr->authenticator));
613 hmac_md5(secret, secret_len, msg->buf, msg->buf_used, auth);
614 os_memcpy(attr + 1, orig, MD5_MAC_LEN);
616 os_memcpy(msg->hdr->authenticator, orig_authenticator,
617 sizeof(orig_authenticator));
620 if (os_memcmp(orig, auth, MD5_MAC_LEN) != 0) {
621 printf("Invalid Message-Authenticator!\n");
629 int radius_msg_verify(struct radius_msg *msg, const u8 *secret,
630 size_t secret_len, struct radius_msg *sent_msg, int auth)
634 u8 hash[MD5_MAC_LEN];
636 if (sent_msg == NULL) {
637 printf("No matching Access-Request message found\n");
642 radius_msg_verify_msg_auth(msg, secret, secret_len,
643 sent_msg->hdr->authenticator)) {
647 /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
648 addr[0] = (u8 *) msg->hdr;
650 addr[1] = sent_msg->hdr->authenticator;
651 len[1] = MD5_MAC_LEN;
652 addr[2] = (u8 *) (msg->hdr + 1);
653 len[2] = msg->buf_used - sizeof(*msg->hdr);
656 md5_vector(4, addr, len, hash);
657 if (os_memcmp(hash, msg->hdr->authenticator, MD5_MAC_LEN) != 0) {
658 printf("Response Authenticator invalid!\n");
666 int radius_msg_copy_attr(struct radius_msg *dst, struct radius_msg *src,
669 struct radius_attr_hdr *attr;
673 for (i = 0; i < src->attr_used; i++) {
674 attr = src->attrs[i];
675 if (attr->type == type) {
676 if (!radius_msg_add_attr(dst, type, (u8 *) (attr + 1),
677 attr->length - sizeof(*attr)))
687 /* Create Request Authenticator. The value should be unique over the lifetime
688 * of the shared secret between authenticator and authentication server.
689 * Use one-way MD5 hash calculated from current timestamp and some data given
691 void radius_msg_make_authenticator(struct radius_msg *msg,
692 const u8 *data, size_t len)
701 addr[0] = (u8 *) &tv;
702 elen[0] = sizeof(tv);
707 md5_vector(3, addr, elen, msg->hdr->authenticator);
711 /* Get Vendor-specific RADIUS Attribute from a parsed RADIUS message.
712 * Returns the Attribute payload and sets alen to indicate the length of the
713 * payload if a vendor attribute with subtype is found, otherwise returns NULL.
714 * The returned payload is allocated with os_malloc() and caller must free it
715 * by calling os_free().
717 static u8 *radius_msg_get_vendor_attr(struct radius_msg *msg, u32 vendor,
718 u8 subtype, size_t *alen)
726 for (i = 0; i < msg->attr_used; i++) {
727 struct radius_attr_hdr *attr = msg->attrs[i];
730 struct radius_attr_vendor *vhdr;
732 if (attr->type != RADIUS_ATTR_VENDOR_SPECIFIC)
735 left = attr->length - sizeof(*attr);
739 pos = (u8 *) (attr + 1);
741 os_memcpy(&vendor_id, pos, 4);
745 if (ntohl(vendor_id) != vendor)
748 while (left >= sizeof(*vhdr)) {
749 vhdr = (struct radius_attr_vendor *) pos;
750 if (vhdr->vendor_length > left ||
751 vhdr->vendor_length < sizeof(*vhdr)) {
755 if (vhdr->vendor_type != subtype) {
756 pos += vhdr->vendor_length;
757 left -= vhdr->vendor_length;
761 len = vhdr->vendor_length - sizeof(*vhdr);
762 data = os_malloc(len);
765 os_memcpy(data, pos + sizeof(*vhdr), len);
776 static u8 * decrypt_ms_key(const u8 *key, size_t len,
777 const u8 *req_authenticator,
778 const u8 *secret, size_t secret_len, size_t *reslen)
780 u8 *plain, *ppos, *res;
783 u8 hash[MD5_MAC_LEN];
788 /* key: 16-bit salt followed by encrypted key info */
796 printf("Invalid ms key len %lu\n", (unsigned long) left);
801 ppos = plain = os_malloc(plen);
806 /* b(1) = MD5(Secret + Request-Authenticator + Salt)
807 * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
810 elen[0] = secret_len;
812 addr[1] = req_authenticator;
813 elen[1] = MD5_MAC_LEN;
815 elen[2] = 2; /* Salt */
817 addr[1] = pos - MD5_MAC_LEN;
818 elen[1] = MD5_MAC_LEN;
820 md5_vector(first ? 3 : 2, addr, elen, hash);
823 for (i = 0; i < MD5_MAC_LEN; i++)
824 *ppos++ = *pos++ ^ hash[i];
828 if (plain[0] > plen - 1) {
829 printf("Failed to decrypt MPPE key\n");
834 res = os_malloc(plain[0]);
839 os_memcpy(res, plain + 1, plain[0]);
847 static void encrypt_ms_key(const u8 *key, size_t key_len, u16 salt,
848 const u8 *req_authenticator,
849 const u8 *secret, size_t secret_len,
850 u8 *ebuf, size_t *elen)
852 int i, len, first = 1;
853 u8 hash[MD5_MAC_LEN], saltbuf[2], *pos;
857 saltbuf[0] = salt >> 8;
862 len = (len & 0xf0) + 16;
864 os_memset(ebuf, 0, len);
866 os_memcpy(ebuf + 1, key, key_len);
872 /* b(1) = MD5(Secret + Request-Authenticator + Salt)
873 * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
875 _len[0] = secret_len;
877 addr[1] = req_authenticator;
878 _len[1] = MD5_MAC_LEN;
880 _len[2] = sizeof(saltbuf);
882 addr[1] = pos - MD5_MAC_LEN;
883 _len[1] = MD5_MAC_LEN;
885 md5_vector(first ? 3 : 2, addr, _len, hash);
888 for (i = 0; i < MD5_MAC_LEN; i++)
896 struct radius_ms_mppe_keys *
897 radius_msg_get_ms_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
898 u8 *secret, size_t secret_len)
902 struct radius_ms_mppe_keys *keys;
904 if (msg == NULL || sent_msg == NULL)
907 keys = wpa_zalloc(sizeof(*keys));
911 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
912 RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY,
915 keys->send = decrypt_ms_key(key, keylen,
916 sent_msg->hdr->authenticator,
922 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
923 RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY,
926 keys->recv = decrypt_ms_key(key, keylen,
927 sent_msg->hdr->authenticator,
937 struct radius_ms_mppe_keys *
938 radius_msg_get_cisco_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
939 u8 *secret, size_t secret_len)
943 struct radius_ms_mppe_keys *keys;
945 if (msg == NULL || sent_msg == NULL)
948 keys = wpa_zalloc(sizeof(*keys));
952 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_CISCO,
953 RADIUS_CISCO_AV_PAIR, &keylen);
954 if (key && keylen == 51 &&
955 os_memcmp(key, "leap:session-key=", 17) == 0) {
956 keys->recv = decrypt_ms_key(key + 17, keylen - 17,
957 sent_msg->hdr->authenticator,
967 int radius_msg_add_mppe_keys(struct radius_msg *msg,
968 const u8 *req_authenticator,
969 const u8 *secret, size_t secret_len,
970 const u8 *send_key, size_t send_key_len,
971 const u8 *recv_key, size_t recv_key_len)
973 struct radius_attr_hdr *attr;
974 u32 vendor_id = htonl(RADIUS_VENDOR_ID_MICROSOFT);
976 struct radius_attr_vendor *vhdr;
982 hlen = sizeof(vendor_id) + sizeof(*vhdr) + 2;
984 /* MS-MPPE-Send-Key */
985 buf = os_malloc(hlen + send_key_len + 16);
990 os_memcpy(pos, &vendor_id, sizeof(vendor_id));
991 pos += sizeof(vendor_id);
992 vhdr = (struct radius_attr_vendor *) pos;
993 vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY;
994 pos = (u8 *) (vhdr + 1);
995 salt = os_random() | 0x8000;
998 encrypt_ms_key(send_key, send_key_len, salt, req_authenticator, secret,
999 secret_len, pos, &elen);
1000 vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
1002 attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1009 /* MS-MPPE-Recv-Key */
1010 buf = os_malloc(hlen + send_key_len + 16);
1015 os_memcpy(pos, &vendor_id, sizeof(vendor_id));
1016 pos += sizeof(vendor_id);
1017 vhdr = (struct radius_attr_vendor *) pos;
1018 vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY;
1019 pos = (u8 *) (vhdr + 1);
1023 encrypt_ms_key(recv_key, recv_key_len, salt, req_authenticator, secret,
1024 secret_len, pos, &elen);
1025 vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
1027 attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1038 /* Add User-Password attribute to a RADIUS message and encrypt it as specified
1039 * in RFC 2865, Chap. 5.2 */
1040 struct radius_attr_hdr *
1041 radius_msg_add_attr_user_password(struct radius_msg *msg,
1042 u8 *data, size_t data_len,
1043 u8 *secret, size_t secret_len)
1047 size_t buf_len, pos;
1055 os_memcpy(buf, data, data_len);
1058 padlen = data_len % 16;
1060 padlen = 16 - padlen;
1061 os_memset(buf + data_len, 0, padlen);
1066 len[0] = secret_len;
1067 addr[1] = msg->hdr->authenticator;
1069 md5_vector(2, addr, len, hash);
1071 for (i = 0; i < 16; i++)
1075 while (pos < buf_len) {
1077 len[0] = secret_len;
1078 addr[1] = &buf[pos - 16];
1080 md5_vector(2, addr, len, hash);
1082 for (i = 0; i < 16; i++)
1083 buf[pos + i] ^= hash[i];
1088 return radius_msg_add_attr(msg, RADIUS_ATTR_USER_PASSWORD,
1093 int radius_msg_get_attr(struct radius_msg *msg, u8 type, u8 *buf, size_t len)
1095 struct radius_attr_hdr *attr = NULL;
1098 for (i = 0; i < msg->attr_used; i++) {
1099 if (msg->attrs[i]->type == type) {
1100 attr = msg->attrs[i];
1108 dlen = attr->length - sizeof(*attr);
1110 os_memcpy(buf, (attr + 1), dlen > len ? len : dlen);
1115 int radius_msg_get_attr_ptr(struct radius_msg *msg, u8 type, u8 **buf,
1116 size_t *len, const u8 *start)
1119 struct radius_attr_hdr *attr = NULL;
1121 for (i = 0; i < msg->attr_used; i++) {
1122 if (msg->attrs[i]->type == type &&
1123 (start == NULL || (u8 *) msg->attrs[i] > start)) {
1124 attr = msg->attrs[i];
1132 *buf = (u8 *) (attr + 1);
1133 *len = attr->length - sizeof(*attr);
1138 int radius_msg_count_attr(struct radius_msg *msg, u8 type, int min_len)
1143 for (count = 0, i = 0; i < msg->attr_used; i++) {
1144 if (msg->attrs[i]->type == type &&
1145 msg->attrs[i]->length >=
1146 sizeof(struct radius_attr_hdr) + min_len)
1154 struct radius_tunnel_attrs {
1156 int type; /* Tunnel-Type */
1157 int medium_type; /* Tunnel-Medium-Type */
1163 * radius_msg_get_vlanid - Parse RADIUS attributes for VLAN tunnel information
1164 * @msg: RADIUS message
1165 * Returns: VLAN ID for the first tunnel configuration of -1 if none is found
1167 int radius_msg_get_vlanid(struct radius_msg *msg)
1169 struct radius_tunnel_attrs tunnel[RADIUS_TUNNEL_TAGS], *tun;
1171 struct radius_attr_hdr *attr = NULL;
1176 os_memset(&tunnel, 0, sizeof(tunnel));
1178 for (i = 0; i < msg->attr_used; i++) {
1179 attr = msg->attrs[i];
1180 data = (const u8 *) (attr + 1);
1181 dlen = attr->length - sizeof(*attr);
1182 if (attr->length < 3)
1184 if (data[0] >= RADIUS_TUNNEL_TAGS)
1187 tun = &tunnel[data[0]];
1189 switch (attr->type) {
1190 case RADIUS_ATTR_TUNNEL_TYPE:
1191 if (attr->length != 6)
1194 tun->type = (data[1] << 16) | (data[2] << 8) | data[3];
1196 case RADIUS_ATTR_TUNNEL_MEDIUM_TYPE:
1197 if (attr->length != 6)
1201 (data[1] << 16) | (data[2] << 8) | data[3];
1203 case RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID:
1204 if (data[0] < RADIUS_TUNNEL_TAGS) {
1208 if (dlen >= sizeof(buf))
1210 os_memcpy(buf, data, dlen);
1213 tun->vlanid = atoi(buf);
1218 for (i = 0; i < RADIUS_TUNNEL_TAGS; i++) {
1220 if (tun->tag_used &&
1221 tun->type == RADIUS_TUNNEL_TYPE_VLAN &&
1222 tun->medium_type == RADIUS_TUNNEL_MEDIUM_TYPE_802 &&