LCOV - code coverage report
Current view: top level - src/radius - radius.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1475438200 Lines: 689 811 85.0 %
Date: 2016-10-02 Functions: 44 46 95.7 %

          Line data    Source code
       1             : /*
       2             :  * RADIUS message processing
       3             :  * Copyright (c) 2002-2009, 2011-2015, Jouni Malinen <j@w1.fi>
       4             :  *
       5             :  * This software may be distributed under the terms of the BSD license.
       6             :  * See README for more details.
       7             :  */
       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             :  */
      21             : struct 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             : 
      52       72488 : struct radius_hdr * radius_msg_get_hdr(struct radius_msg *msg)
      53             : {
      54       72488 :         return msg->hdr;
      55             : }
      56             : 
      57             : 
      58       14883 : struct wpabuf * radius_msg_get_buf(struct radius_msg *msg)
      59             : {
      60       14883 :         return msg->buf;
      61             : }
      62             : 
      63             : 
      64             : static struct radius_attr_hdr *
      65     1021519 : radius_get_attr_hdr(struct radius_msg *msg, int idx)
      66             : {
      67     1021519 :         return (struct radius_attr_hdr *)
      68     1021519 :                 (wpabuf_mhead_u8(msg->buf) + msg->attr_pos[idx]);
      69             : }
      70             : 
      71             : 
      72       14853 : static void radius_msg_set_hdr(struct radius_msg *msg, u8 code, u8 identifier)
      73             : {
      74       14853 :         msg->hdr->code = code;
      75       14853 :         msg->hdr->identifier = identifier;
      76       14853 : }
      77             : 
      78             : 
      79       29593 : static int radius_msg_initialize(struct radius_msg *msg)
      80             : {
      81       29593 :         msg->attr_pos = os_calloc(RADIUS_DEFAULT_ATTR_COUNT,
      82             :                                   sizeof(*msg->attr_pos));
      83       29593 :         if (msg->attr_pos == NULL)
      84           9 :                 return -1;
      85             : 
      86       29584 :         msg->attr_size = RADIUS_DEFAULT_ATTR_COUNT;
      87       29584 :         msg->attr_used = 0;
      88             : 
      89       29584 :         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             :  */
     102       14863 : struct radius_msg * radius_msg_new(u8 code, u8 identifier)
     103             : {
     104             :         struct radius_msg *msg;
     105             : 
     106       14863 :         msg = os_zalloc(sizeof(*msg));
     107       14863 :         if (msg == NULL)
     108           5 :                 return NULL;
     109             : 
     110       14858 :         msg->buf = wpabuf_alloc(RADIUS_DEFAULT_MSG_SIZE);
     111       14858 :         if (msg->buf == NULL || radius_msg_initialize(msg)) {
     112           5 :                 radius_msg_free(msg);
     113           5 :                 return NULL;
     114             :         }
     115       14853 :         msg->hdr = wpabuf_put(msg->buf, sizeof(struct radius_hdr));
     116             : 
     117       14853 :         radius_msg_set_hdr(msg, code, identifier);
     118             : 
     119       14853 :         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             :  */
     127       35574 : void radius_msg_free(struct radius_msg *msg)
     128             : {
     129       35574 :         if (msg == NULL)
     130       41546 :                 return;
     131             : 
     132       29602 :         wpabuf_free(msg->buf);
     133       29602 :         os_free(msg->attr_pos);
     134       29602 :         os_free(msg);
     135             : }
     136             : 
     137             : 
     138       29588 : static const char *radius_code_string(u8 code)
     139             : {
     140       29588 :         switch (code) {
     141       13715 :         case RADIUS_CODE_ACCESS_REQUEST: return "Access-Request";
     142        2287 :         case RADIUS_CODE_ACCESS_ACCEPT: return "Access-Accept";
     143         535 :         case RADIUS_CODE_ACCESS_REJECT: return "Access-Reject";
     144        1135 :         case RADIUS_CODE_ACCOUNTING_REQUEST: return "Accounting-Request";
     145         997 :         case RADIUS_CODE_ACCOUNTING_RESPONSE: return "Accounting-Response";
     146       10866 :         case RADIUS_CODE_ACCESS_CHALLENGE: return "Access-Challenge";
     147           0 :         case RADIUS_CODE_STATUS_SERVER: return "Status-Server";
     148           0 :         case RADIUS_CODE_STATUS_CLIENT: return "Status-Client";
     149           0 :         case RADIUS_CODE_RESERVED: return "Reserved";
     150          27 :         case RADIUS_CODE_DISCONNECT_REQUEST: return "Disconnect-Request";
     151          10 :         case RADIUS_CODE_DISCONNECT_ACK: return "Disconnect-ACK";
     152          14 :         case RADIUS_CODE_DISCONNECT_NAK: return "Disconnect-NAK";
     153           1 :         case RADIUS_CODE_COA_REQUEST: return "CoA-Request";
     154           0 :         case RADIUS_CODE_COA_ACK: return "CoA-ACK";
     155           1 :         case RADIUS_CODE_COA_NAK: return "CoA-NAK";
     156           0 :         default: return "?Unknown?";
     157             :         }
     158             : }
     159             : 
     160             : 
     161             : struct 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             : 
     170             : static const 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_SERVICE_TYPE, "Service-Type", RADIUS_ATTR_INT32 },
     177             :         { RADIUS_ATTR_FRAMED_IP_ADDRESS, "Framed-IP-Address", RADIUS_ATTR_IP },
     178             :         { RADIUS_ATTR_FRAMED_MTU, "Framed-MTU", RADIUS_ATTR_INT32 },
     179             :         { RADIUS_ATTR_REPLY_MESSAGE, "Reply-Message", RADIUS_ATTR_TEXT },
     180             :         { RADIUS_ATTR_STATE, "State", RADIUS_ATTR_UNDIST },
     181             :         { RADIUS_ATTR_CLASS, "Class", RADIUS_ATTR_UNDIST },
     182             :         { RADIUS_ATTR_VENDOR_SPECIFIC, "Vendor-Specific", RADIUS_ATTR_UNDIST },
     183             :         { RADIUS_ATTR_SESSION_TIMEOUT, "Session-Timeout", RADIUS_ATTR_INT32 },
     184             :         { RADIUS_ATTR_IDLE_TIMEOUT, "Idle-Timeout", RADIUS_ATTR_INT32 },
     185             :         { RADIUS_ATTR_TERMINATION_ACTION, "Termination-Action",
     186             :           RADIUS_ATTR_INT32 },
     187             :         { RADIUS_ATTR_CALLED_STATION_ID, "Called-Station-Id",
     188             :           RADIUS_ATTR_TEXT },
     189             :         { RADIUS_ATTR_CALLING_STATION_ID, "Calling-Station-Id",
     190             :           RADIUS_ATTR_TEXT },
     191             :         { RADIUS_ATTR_NAS_IDENTIFIER, "NAS-Identifier", RADIUS_ATTR_TEXT },
     192             :         { RADIUS_ATTR_PROXY_STATE, "Proxy-State", RADIUS_ATTR_UNDIST },
     193             :         { RADIUS_ATTR_ACCT_STATUS_TYPE, "Acct-Status-Type",
     194             :           RADIUS_ATTR_INT32 },
     195             :         { RADIUS_ATTR_ACCT_DELAY_TIME, "Acct-Delay-Time", RADIUS_ATTR_INT32 },
     196             :         { RADIUS_ATTR_ACCT_INPUT_OCTETS, "Acct-Input-Octets",
     197             :           RADIUS_ATTR_INT32 },
     198             :         { RADIUS_ATTR_ACCT_OUTPUT_OCTETS, "Acct-Output-Octets",
     199             :           RADIUS_ATTR_INT32 },
     200             :         { RADIUS_ATTR_ACCT_SESSION_ID, "Acct-Session-Id", RADIUS_ATTR_TEXT },
     201             :         { RADIUS_ATTR_ACCT_AUTHENTIC, "Acct-Authentic", RADIUS_ATTR_INT32 },
     202             :         { RADIUS_ATTR_ACCT_SESSION_TIME, "Acct-Session-Time",
     203             :           RADIUS_ATTR_INT32 },
     204             :         { RADIUS_ATTR_ACCT_INPUT_PACKETS, "Acct-Input-Packets",
     205             :           RADIUS_ATTR_INT32 },
     206             :         { RADIUS_ATTR_ACCT_OUTPUT_PACKETS, "Acct-Output-Packets",
     207             :           RADIUS_ATTR_INT32 },
     208             :         { RADIUS_ATTR_ACCT_TERMINATE_CAUSE, "Acct-Terminate-Cause",
     209             :           RADIUS_ATTR_INT32 },
     210             :         { RADIUS_ATTR_ACCT_MULTI_SESSION_ID, "Acct-Multi-Session-Id",
     211             :           RADIUS_ATTR_TEXT },
     212             :         { RADIUS_ATTR_ACCT_LINK_COUNT, "Acct-Link-Count", RADIUS_ATTR_INT32 },
     213             :         { RADIUS_ATTR_ACCT_INPUT_GIGAWORDS, "Acct-Input-Gigawords", 
     214             :           RADIUS_ATTR_INT32 },
     215             :         { RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS, "Acct-Output-Gigawords",
     216             :           RADIUS_ATTR_INT32 },
     217             :         { RADIUS_ATTR_EVENT_TIMESTAMP, "Event-Timestamp",
     218             :           RADIUS_ATTR_INT32 },
     219             :         { RADIUS_ATTR_EGRESS_VLANID, "EGRESS-VLANID", RADIUS_ATTR_HEXDUMP },
     220             :         { RADIUS_ATTR_NAS_PORT_TYPE, "NAS-Port-Type", RADIUS_ATTR_INT32 },
     221             :         { RADIUS_ATTR_TUNNEL_TYPE, "Tunnel-Type", RADIUS_ATTR_HEXDUMP },
     222             :         { RADIUS_ATTR_TUNNEL_MEDIUM_TYPE, "Tunnel-Medium-Type",
     223             :           RADIUS_ATTR_HEXDUMP },
     224             :         { RADIUS_ATTR_TUNNEL_PASSWORD, "Tunnel-Password",
     225             :           RADIUS_ATTR_UNDIST },
     226             :         { RADIUS_ATTR_CONNECT_INFO, "Connect-Info", RADIUS_ATTR_TEXT },
     227             :         { RADIUS_ATTR_EAP_MESSAGE, "EAP-Message", RADIUS_ATTR_UNDIST },
     228             :         { RADIUS_ATTR_MESSAGE_AUTHENTICATOR, "Message-Authenticator",
     229             :           RADIUS_ATTR_UNDIST },
     230             :         { RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID, "Tunnel-Private-Group-Id",
     231             :           RADIUS_ATTR_HEXDUMP },
     232             :         { RADIUS_ATTR_ACCT_INTERIM_INTERVAL, "Acct-Interim-Interval",
     233             :           RADIUS_ATTR_INT32 },
     234             :         { RADIUS_ATTR_CHARGEABLE_USER_IDENTITY, "Chargeable-User-Identity",
     235             :           RADIUS_ATTR_TEXT },
     236             :         { RADIUS_ATTR_NAS_IPV6_ADDRESS, "NAS-IPv6-Address", RADIUS_ATTR_IPV6 },
     237             :         { RADIUS_ATTR_ERROR_CAUSE, "Error-Cause", RADIUS_ATTR_INT32 },
     238             :         { RADIUS_ATTR_EAP_KEY_NAME, "EAP-Key-Name", RADIUS_ATTR_HEXDUMP },
     239             :         { RADIUS_ATTR_OPERATOR_NAME, "Operator-Name", RADIUS_ATTR_TEXT },
     240             :         { RADIUS_ATTR_LOCATION_INFO, "Location-Information",
     241             :           RADIUS_ATTR_HEXDUMP },
     242             :         { RADIUS_ATTR_LOCATION_DATA, "Location-Data", RADIUS_ATTR_HEXDUMP },
     243             :         { RADIUS_ATTR_BASIC_LOCATION_POLICY_RULES,
     244             :           "Basic-Location-Policy-Rules", RADIUS_ATTR_HEXDUMP },
     245             :         { RADIUS_ATTR_EXTENDED_LOCATION_POLICY_RULES,
     246             :           "Extended-Location-Policy-Rules", RADIUS_ATTR_HEXDUMP },
     247             :         { RADIUS_ATTR_LOCATION_CAPABLE, "Location-Capable", RADIUS_ATTR_INT32 },
     248             :         { RADIUS_ATTR_REQUESTED_LOCATION_INFO, "Requested-Location-Info",
     249             :           RADIUS_ATTR_INT32 },
     250             :         { RADIUS_ATTR_MOBILITY_DOMAIN_ID, "Mobility-Domain-Id",
     251             :           RADIUS_ATTR_INT32 },
     252             :         { RADIUS_ATTR_WLAN_HESSID, "WLAN-HESSID", RADIUS_ATTR_TEXT },
     253             :         { RADIUS_ATTR_WLAN_PAIRWISE_CIPHER, "WLAN-Pairwise-Cipher",
     254             :           RADIUS_ATTR_HEXDUMP },
     255             :         { RADIUS_ATTR_WLAN_GROUP_CIPHER, "WLAN-Group-Cipher",
     256             :           RADIUS_ATTR_HEXDUMP },
     257             :         { RADIUS_ATTR_WLAN_AKM_SUITE, "WLAN-AKM-Suite",
     258             :           RADIUS_ATTR_HEXDUMP },
     259             :         { RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, "WLAN-Group-Mgmt-Pairwise-Cipher",
     260             :           RADIUS_ATTR_HEXDUMP },
     261             : };
     262             : #define RADIUS_ATTRS ARRAY_SIZE(radius_attrs)
     263             : 
     264             : 
     265      301324 : static const struct radius_attr_type *radius_get_attr_type(u8 type)
     266             : {
     267             :         size_t i;
     268             : 
     269     8371207 :         for (i = 0; i < RADIUS_ATTRS; i++) {
     270     8371207 :                 if (type == radius_attrs[i].type)
     271      301324 :                         return &radius_attrs[i];
     272             :         }
     273             : 
     274           0 :         return NULL;
     275             : }
     276             : 
     277             : 
     278      301324 : static void radius_msg_dump_attr(struct radius_attr_hdr *hdr)
     279             : {
     280             :         const struct radius_attr_type *attr;
     281             :         int len;
     282             :         unsigned char *pos;
     283             :         char buf[1000];
     284             : 
     285      301324 :         attr = radius_get_attr_type(hdr->type);
     286             : 
     287      602648 :         wpa_printf(MSG_INFO, "   Attribute %d (%s) length=%d",
     288      602648 :                    hdr->type, attr ? attr->name : "?Unknown?", hdr->length);
     289             : 
     290      301324 :         if (attr == NULL || hdr->length < sizeof(struct radius_attr_hdr))
     291      301324 :                 return;
     292             : 
     293      301324 :         len = hdr->length - sizeof(struct radius_attr_hdr);
     294      301324 :         pos = (unsigned char *) (hdr + 1);
     295             : 
     296      301324 :         switch (attr->data_type) {
     297             :         case RADIUS_ATTR_TEXT:
     298      101876 :                 printf_encode(buf, sizeof(buf), pos, len);
     299      101876 :                 wpa_printf(MSG_INFO, "      Value: '%s'", buf);
     300      101876 :                 break;
     301             : 
     302             :         case RADIUS_ATTR_IP:
     303          77 :                 if (len == 4) {
     304             :                         struct in_addr addr;
     305          77 :                         os_memcpy(&addr, pos, 4);
     306          77 :                         wpa_printf(MSG_INFO, "      Value: %s",
     307             :                                    inet_ntoa(addr));
     308             :                 } else {
     309           0 :                         wpa_printf(MSG_INFO, "      Invalid IP address length %d",
     310             :                                    len);
     311             :                 }
     312          77 :                 break;
     313             : 
     314             : #ifdef CONFIG_IPV6
     315             :         case RADIUS_ATTR_IPV6:
     316          14 :                 if (len == 16) {
     317             :                         const char *atxt;
     318          14 :                         struct in6_addr *addr = (struct in6_addr *) pos;
     319          14 :                         atxt = inet_ntop(AF_INET6, addr, buf, sizeof(buf));
     320          14 :                         wpa_printf(MSG_INFO, "      Value: %s",
     321             :                                    atxt ? atxt : "?");
     322             :                 } else {
     323           0 :                         wpa_printf(MSG_INFO, "      Invalid IPv6 address length %d",
     324             :                                    len);
     325             :                 }
     326          14 :                 break;
     327             : #endif /* CONFIG_IPV6 */
     328             : 
     329             :         case RADIUS_ATTR_HEXDUMP:
     330             :         case RADIUS_ATTR_UNDIST:
     331      133549 :                 wpa_snprintf_hex(buf, sizeof(buf), pos, len);
     332      133549 :                 wpa_printf(MSG_INFO, "      Value: %s", buf);
     333      133549 :                 break;
     334             : 
     335             :         case RADIUS_ATTR_INT32:
     336       65808 :                 if (len == 4)
     337       65808 :                         wpa_printf(MSG_INFO, "      Value: %u",
     338             :                                    WPA_GET_BE32(pos));
     339             :                 else
     340           0 :                         wpa_printf(MSG_INFO, "      Invalid INT32 length %d",
     341             :                                    len);
     342       65808 :                 break;
     343             : 
     344             :         default:
     345           0 :                 break;
     346             :         }
     347             : }
     348             : 
     349             : 
     350       29588 : void radius_msg_dump(struct radius_msg *msg)
     351             : {
     352             :         size_t i;
     353             : 
     354      118352 :         wpa_printf(MSG_INFO, "RADIUS message: code=%d (%s) identifier=%d length=%d",
     355       59176 :                    msg->hdr->code, radius_code_string(msg->hdr->code),
     356       59176 :                    msg->hdr->identifier, be_to_host16(msg->hdr->length));
     357             : 
     358      330912 :         for (i = 0; i < msg->attr_used; i++) {
     359      301324 :                 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
     360      301324 :                 radius_msg_dump_attr(attr);
     361             :         }
     362       29588 : }
     363             : 
     364             : 
     365        7278 : int radius_msg_finish(struct radius_msg *msg, const u8 *secret,
     366             :                       size_t secret_len)
     367             : {
     368        7278 :         if (secret) {
     369             :                 u8 auth[MD5_MAC_LEN];
     370             :                 struct radius_attr_hdr *attr;
     371             : 
     372        7278 :                 os_memset(auth, 0, MD5_MAC_LEN);
     373        7278 :                 attr = radius_msg_add_attr(msg,
     374             :                                            RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
     375             :                                            auth, MD5_MAC_LEN);
     376        7278 :                 if (attr == NULL) {
     377           1 :                         wpa_printf(MSG_WARNING, "RADIUS: Could not add "
     378             :                                    "Message-Authenticator");
     379           1 :                         return -1;
     380             :                 }
     381        7277 :                 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
     382       14554 :                 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
     383        7277 :                          wpabuf_len(msg->buf), (u8 *) (attr + 1));
     384             :         } else
     385           0 :                 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
     386             : 
     387        7277 :         if (wpabuf_len(msg->buf) > 0xffff) {
     388           0 :                 wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
     389           0 :                            (unsigned long) wpabuf_len(msg->buf));
     390           0 :                 return -1;
     391             :         }
     392        7277 :         return 0;
     393             : }
     394             : 
     395             : 
     396        6421 : int radius_msg_finish_srv(struct radius_msg *msg, const u8 *secret,
     397             :                           size_t secret_len, const u8 *req_authenticator)
     398             : {
     399             :         u8 auth[MD5_MAC_LEN];
     400             :         struct radius_attr_hdr *attr;
     401             :         const u8 *addr[4];
     402             :         size_t len[4];
     403             : 
     404        6421 :         os_memset(auth, 0, MD5_MAC_LEN);
     405        6421 :         attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
     406             :                                    auth, MD5_MAC_LEN);
     407        6421 :         if (attr == NULL) {
     408           0 :                 wpa_printf(MSG_ERROR, "WARNING: Could not add Message-Authenticator");
     409           0 :                 return -1;
     410             :         }
     411        6421 :         msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
     412        6421 :         os_memcpy(msg->hdr->authenticator, req_authenticator,
     413             :                   sizeof(msg->hdr->authenticator));
     414       12842 :         hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
     415        6421 :                  wpabuf_len(msg->buf), (u8 *) (attr + 1));
     416             : 
     417             :         /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
     418        6421 :         addr[0] = (u8 *) msg->hdr;
     419        6421 :         len[0] = 1 + 1 + 2;
     420        6421 :         addr[1] = req_authenticator;
     421        6421 :         len[1] = MD5_MAC_LEN;
     422        6421 :         addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
     423        6421 :         len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
     424        6421 :         addr[3] = secret;
     425        6421 :         len[3] = secret_len;
     426        6421 :         md5_vector(4, addr, len, msg->hdr->authenticator);
     427             : 
     428        6421 :         if (wpabuf_len(msg->buf) > 0xffff) {
     429           0 :                 wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
     430           0 :                            (unsigned long) wpabuf_len(msg->buf));
     431           0 :                 return -1;
     432             :         }
     433        6421 :         return 0;
     434             : }
     435             : 
     436             : 
     437          25 : int radius_msg_finish_das_resp(struct radius_msg *msg, const u8 *secret,
     438             :                                size_t secret_len,
     439             :                                const struct radius_hdr *req_hdr)
     440             : {
     441             :         const u8 *addr[2];
     442             :         size_t len[2];
     443             :         u8 auth[MD5_MAC_LEN];
     444             :         struct radius_attr_hdr *attr;
     445             : 
     446          25 :         os_memset(auth, 0, MD5_MAC_LEN);
     447          25 :         attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
     448             :                                    auth, MD5_MAC_LEN);
     449          25 :         if (attr == NULL) {
     450           0 :                 wpa_printf(MSG_WARNING, "Could not add Message-Authenticator");
     451           0 :                 return -1;
     452             :         }
     453             : 
     454          25 :         msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
     455          25 :         os_memcpy(msg->hdr->authenticator, req_hdr->authenticator, 16);
     456          50 :         hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
     457          25 :                  wpabuf_len(msg->buf), (u8 *) (attr + 1));
     458             : 
     459             :         /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
     460          25 :         addr[0] = wpabuf_head_u8(msg->buf);
     461          25 :         len[0] = wpabuf_len(msg->buf);
     462          25 :         addr[1] = secret;
     463          25 :         len[1] = secret_len;
     464          25 :         if (md5_vector(2, addr, len, msg->hdr->authenticator) < 0)
     465           0 :                 return -1;
     466             : 
     467          25 :         if (wpabuf_len(msg->buf) > 0xffff) {
     468           0 :                 wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
     469           0 :                            (unsigned long) wpabuf_len(msg->buf));
     470           0 :                 return -1;
     471             :         }
     472          25 :         return 0;
     473             : }
     474             : 
     475             : 
     476         604 : void radius_msg_finish_acct(struct radius_msg *msg, const u8 *secret,
     477             :                             size_t secret_len)
     478             : {
     479             :         const u8 *addr[2];
     480             :         size_t len[2];
     481             : 
     482         604 :         msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
     483         604 :         os_memset(msg->hdr->authenticator, 0, MD5_MAC_LEN);
     484         604 :         addr[0] = wpabuf_head(msg->buf);
     485         604 :         len[0] = wpabuf_len(msg->buf);
     486         604 :         addr[1] = secret;
     487         604 :         len[1] = secret_len;
     488         604 :         md5_vector(2, addr, len, msg->hdr->authenticator);
     489             : 
     490         604 :         if (wpabuf_len(msg->buf) > 0xffff) {
     491           0 :                 wpa_printf(MSG_WARNING, "RADIUS: Too long messages (%lu)",
     492           0 :                            (unsigned long) wpabuf_len(msg->buf));
     493             :         }
     494         604 : }
     495             : 
     496             : 
     497         530 : void radius_msg_finish_acct_resp(struct radius_msg *msg, const u8 *secret,
     498             :                                  size_t secret_len, const u8 *req_authenticator)
     499             : {
     500             :         const u8 *addr[2];
     501             :         size_t len[2];
     502             : 
     503         530 :         msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
     504         530 :         os_memcpy(msg->hdr->authenticator, req_authenticator, MD5_MAC_LEN);
     505         530 :         addr[0] = wpabuf_head(msg->buf);
     506         530 :         len[0] = wpabuf_len(msg->buf);
     507         530 :         addr[1] = secret;
     508         530 :         len[1] = secret_len;
     509         530 :         md5_vector(2, addr, len, msg->hdr->authenticator);
     510             : 
     511         530 :         if (wpabuf_len(msg->buf) > 0xffff) {
     512           0 :                 wpa_printf(MSG_WARNING, "RADIUS: Too long messages (%lu)",
     513           0 :                            (unsigned long) wpabuf_len(msg->buf));
     514             :         }
     515         530 : }
     516             : 
     517             : 
     518         532 : int radius_msg_verify_acct_req(struct radius_msg *msg, const u8 *secret,
     519             :                                size_t secret_len)
     520             : {
     521             :         const u8 *addr[4];
     522             :         size_t len[4];
     523             :         u8 zero[MD5_MAC_LEN];
     524             :         u8 hash[MD5_MAC_LEN];
     525             : 
     526         532 :         os_memset(zero, 0, sizeof(zero));
     527         532 :         addr[0] = (u8 *) msg->hdr;
     528         532 :         len[0] = sizeof(struct radius_hdr) - MD5_MAC_LEN;
     529         532 :         addr[1] = zero;
     530         532 :         len[1] = MD5_MAC_LEN;
     531         532 :         addr[2] = (u8 *) (msg->hdr + 1);
     532         532 :         len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
     533         532 :         addr[3] = secret;
     534         532 :         len[3] = secret_len;
     535         532 :         md5_vector(4, addr, len, hash);
     536         532 :         return os_memcmp_const(msg->hdr->authenticator, hash, MD5_MAC_LEN) != 0;
     537             : }
     538             : 
     539             : 
     540          28 : int radius_msg_verify_das_req(struct radius_msg *msg, const u8 *secret,
     541             :                               size_t secret_len,
     542             :                               int require_message_authenticator)
     543             : {
     544             :         const u8 *addr[4];
     545             :         size_t len[4];
     546             :         u8 zero[MD5_MAC_LEN];
     547             :         u8 hash[MD5_MAC_LEN];
     548             :         u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
     549             :         u8 orig_authenticator[16];
     550             : 
     551          28 :         struct radius_attr_hdr *attr = NULL, *tmp;
     552             :         size_t i;
     553             : 
     554          28 :         os_memset(zero, 0, sizeof(zero));
     555          28 :         addr[0] = (u8 *) msg->hdr;
     556          28 :         len[0] = sizeof(struct radius_hdr) - MD5_MAC_LEN;
     557          28 :         addr[1] = zero;
     558          28 :         len[1] = MD5_MAC_LEN;
     559          28 :         addr[2] = (u8 *) (msg->hdr + 1);
     560          28 :         len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
     561          28 :         addr[3] = secret;
     562          28 :         len[3] = secret_len;
     563          28 :         md5_vector(4, addr, len, hash);
     564          28 :         if (os_memcmp_const(msg->hdr->authenticator, hash, MD5_MAC_LEN) != 0)
     565           1 :                 return 1;
     566             : 
     567         103 :         for (i = 0; i < msg->attr_used; i++) {
     568          76 :                 tmp = radius_get_attr_hdr(msg, i);
     569          76 :                 if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
     570           0 :                         if (attr != NULL) {
     571           0 :                                 wpa_printf(MSG_WARNING, "Multiple "
     572             :                                            "Message-Authenticator attributes "
     573             :                                            "in RADIUS message");
     574           0 :                                 return 1;
     575             :                         }
     576           0 :                         attr = tmp;
     577             :                 }
     578             :         }
     579             : 
     580          27 :         if (attr == NULL) {
     581          27 :                 if (require_message_authenticator) {
     582           0 :                         wpa_printf(MSG_WARNING,
     583             :                                    "Missing Message-Authenticator attribute in RADIUS message");
     584           0 :                         return 1;
     585             :                 }
     586          27 :                 return 0;
     587             :         }
     588             : 
     589           0 :         os_memcpy(orig, attr + 1, MD5_MAC_LEN);
     590           0 :         os_memset(attr + 1, 0, MD5_MAC_LEN);
     591           0 :         os_memcpy(orig_authenticator, msg->hdr->authenticator,
     592             :                   sizeof(orig_authenticator));
     593           0 :         os_memset(msg->hdr->authenticator, 0,
     594             :                   sizeof(msg->hdr->authenticator));
     595           0 :         hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
     596           0 :                  wpabuf_len(msg->buf), auth);
     597           0 :         os_memcpy(attr + 1, orig, MD5_MAC_LEN);
     598           0 :         os_memcpy(msg->hdr->authenticator, orig_authenticator,
     599             :                   sizeof(orig_authenticator));
     600             : 
     601           0 :         return os_memcmp_const(orig, auth, MD5_MAC_LEN) != 0;
     602             : }
     603             : 
     604             : 
     605      301228 : static int radius_msg_add_attr_to_array(struct radius_msg *msg,
     606             :                                         struct radius_attr_hdr *attr)
     607             : {
     608      301228 :         if (msg->attr_used >= msg->attr_size) {
     609             :                 size_t *nattr_pos;
     610       10517 :                 int nlen = msg->attr_size * 2;
     611             : 
     612       10517 :                 nattr_pos = os_realloc_array(msg->attr_pos, nlen,
     613             :                                              sizeof(*msg->attr_pos));
     614       10517 :                 if (nattr_pos == NULL)
     615           1 :                         return -1;
     616             : 
     617       10516 :                 msg->attr_pos = nattr_pos;
     618       10516 :                 msg->attr_size = nlen;
     619             :         }
     620             : 
     621      602454 :         msg->attr_pos[msg->attr_used++] =
     622      301227 :                 (unsigned char *) attr - wpabuf_head_u8(msg->buf);
     623             : 
     624      301227 :         return 0;
     625             : }
     626             : 
     627             : 
     628      156987 : struct radius_attr_hdr *radius_msg_add_attr(struct radius_msg *msg, u8 type,
     629             :                                             const u8 *data, size_t data_len)
     630             : {
     631             :         size_t buf_needed;
     632             :         struct radius_attr_hdr *attr;
     633             : 
     634      156987 :         if (data_len > RADIUS_MAX_ATTR_LEN) {
     635           0 :                 wpa_printf(MSG_ERROR, "radius_msg_add_attr: too long attribute (%lu bytes)",
     636             :                        (unsigned long) data_len);
     637           0 :                 return NULL;
     638             :         }
     639             : 
     640      156987 :         buf_needed = sizeof(*attr) + data_len;
     641             : 
     642      156987 :         if (wpabuf_tailroom(msg->buf) < buf_needed) {
     643             :                 /* allocate more space for message buffer */
     644        1507 :                 if (wpabuf_resize(&msg->buf, buf_needed) < 0)
     645           0 :                         return NULL;
     646        1507 :                 msg->hdr = wpabuf_mhead(msg->buf);
     647             :         }
     648             : 
     649      156987 :         attr = wpabuf_put(msg->buf, sizeof(struct radius_attr_hdr));
     650      156987 :         attr->type = type;
     651      156987 :         attr->length = sizeof(*attr) + data_len;
     652      156987 :         wpabuf_put_data(msg->buf, data, data_len);
     653             : 
     654      156987 :         if (radius_msg_add_attr_to_array(msg, attr))
     655           1 :                 return NULL;
     656             : 
     657      156986 :         return attr;
     658             : }
     659             : 
     660             : 
     661             : /**
     662             :  * radius_msg_parse - Parse a RADIUS message
     663             :  * @data: RADIUS message to be parsed
     664             :  * @len: Length of data buffer in octets
     665             :  * Returns: Parsed RADIUS message or %NULL on failure
     666             :  *
     667             :  * This parses a RADIUS message and makes a copy of its data. The caller is
     668             :  * responsible for freeing the returned data with radius_msg_free().
     669             :  */
     670       14751 : struct radius_msg * radius_msg_parse(const u8 *data, size_t len)
     671             : {
     672             :         struct radius_msg *msg;
     673             :         struct radius_hdr *hdr;
     674             :         struct radius_attr_hdr *attr;
     675             :         size_t msg_len;
     676             :         unsigned char *pos, *end;
     677             : 
     678       14751 :         if (data == NULL || len < sizeof(*hdr))
     679           0 :                 return NULL;
     680             : 
     681       14751 :         hdr = (struct radius_hdr *) data;
     682             : 
     683       14751 :         msg_len = be_to_host16(hdr->length);
     684       14751 :         if (msg_len < sizeof(*hdr) || msg_len > len) {
     685           2 :                 wpa_printf(MSG_INFO, "RADIUS: Invalid message length");
     686           2 :                 return NULL;
     687             :         }
     688             : 
     689       14749 :         if (msg_len < len) {
     690           0 :                 wpa_printf(MSG_DEBUG, "RADIUS: Ignored %lu extra bytes after "
     691             :                            "RADIUS message", (unsigned long) len - msg_len);
     692             :         }
     693             : 
     694       14749 :         msg = os_zalloc(sizeof(*msg));
     695       14749 :         if (msg == NULL)
     696           5 :                 return NULL;
     697             : 
     698       14744 :         msg->buf = wpabuf_alloc_copy(data, msg_len);
     699       14744 :         if (msg->buf == NULL || radius_msg_initialize(msg)) {
     700          13 :                 radius_msg_free(msg);
     701          13 :                 return NULL;
     702             :         }
     703       14731 :         msg->hdr = wpabuf_mhead(msg->buf);
     704             : 
     705             :         /* parse attributes */
     706       14731 :         pos = wpabuf_mhead_u8(msg->buf) + sizeof(struct radius_hdr);
     707       14731 :         end = wpabuf_mhead_u8(msg->buf) + wpabuf_len(msg->buf);
     708      173703 :         while (pos < end) {
     709      144241 :                 if ((size_t) (end - pos) < sizeof(*attr))
     710           0 :                         goto fail;
     711             : 
     712      144241 :                 attr = (struct radius_attr_hdr *) pos;
     713             : 
     714      144241 :                 if (attr->length > end - pos || attr->length < sizeof(*attr))
     715             :                         goto fail;
     716             : 
     717             :                 /* TODO: check that attr->length is suitable for attr->type */
     718             : 
     719      144241 :                 if (radius_msg_add_attr_to_array(msg, attr))
     720           0 :                         goto fail;
     721             : 
     722      144241 :                 pos += attr->length;
     723             :         }
     724             : 
     725       14731 :         return msg;
     726             : 
     727             :  fail:
     728           0 :         radius_msg_free(msg);
     729           0 :         return NULL;
     730             : }
     731             : 
     732             : 
     733       13687 : int radius_msg_add_eap(struct radius_msg *msg, const u8 *data, size_t data_len)
     734             : {
     735       13687 :         const u8 *pos = data;
     736       13687 :         size_t left = data_len;
     737             : 
     738       44418 :         while (left > 0) {
     739             :                 int len;
     740       17044 :                 if (left > RADIUS_MAX_ATTR_LEN)
     741        3357 :                         len = RADIUS_MAX_ATTR_LEN;
     742             :                 else
     743       13687 :                         len = left;
     744             : 
     745       17044 :                 if (!radius_msg_add_attr(msg, RADIUS_ATTR_EAP_MESSAGE,
     746             :                                          pos, len))
     747           0 :                         return 0;
     748             : 
     749       17044 :                 pos += len;
     750       17044 :                 left -= len;
     751             :         }
     752             : 
     753       13687 :         return 1;
     754             : }
     755             : 
     756             : 
     757       13690 : struct wpabuf * radius_msg_get_eap(struct radius_msg *msg)
     758             : {
     759             :         struct wpabuf *eap;
     760             :         size_t len, i;
     761             :         struct radius_attr_hdr *attr;
     762             : 
     763       13690 :         if (msg == NULL)
     764           0 :                 return NULL;
     765             : 
     766       13690 :         len = 0;
     767      149942 :         for (i = 0; i < msg->attr_used; i++) {
     768      136252 :                 attr = radius_get_attr_hdr(msg, i);
     769      153480 :                 if (attr->type == RADIUS_ATTR_EAP_MESSAGE &&
     770       17228 :                     attr->length > sizeof(struct radius_attr_hdr))
     771       17228 :                         len += attr->length - sizeof(struct radius_attr_hdr);
     772             :         }
     773             : 
     774       13690 :         if (len == 0)
     775          18 :                 return NULL;
     776             : 
     777       13672 :         eap = wpabuf_alloc(len);
     778       13672 :         if (eap == NULL)
     779           4 :                 return NULL;
     780             : 
     781      149860 :         for (i = 0; i < msg->attr_used; i++) {
     782      136192 :                 attr = radius_get_attr_hdr(msg, i);
     783      153416 :                 if (attr->type == RADIUS_ATTR_EAP_MESSAGE &&
     784       17224 :                     attr->length > sizeof(struct radius_attr_hdr)) {
     785       17224 :                         int flen = attr->length - sizeof(*attr);
     786       17224 :                         wpabuf_put_data(eap, attr + 1, flen);
     787             :                 }
     788             :         }
     789             : 
     790       13668 :         return eap;
     791             : }
     792             : 
     793             : 
     794       13578 : int radius_msg_verify_msg_auth(struct radius_msg *msg, const u8 *secret,
     795             :                                size_t secret_len, const u8 *req_auth)
     796             : {
     797             :         u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
     798             :         u8 orig_authenticator[16];
     799       13578 :         struct radius_attr_hdr *attr = NULL, *tmp;
     800             :         size_t i;
     801             : 
     802      147796 :         for (i = 0; i < msg->attr_used; i++) {
     803      134219 :                 tmp = radius_get_attr_hdr(msg, i);
     804      134219 :                 if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
     805       13578 :                         if (attr != NULL) {
     806           1 :                                 wpa_printf(MSG_INFO, "Multiple Message-Authenticator attributes in RADIUS message");
     807           1 :                                 return 1;
     808             :                         }
     809       13577 :                         attr = tmp;
     810             :                 }
     811             :         }
     812             : 
     813       13577 :         if (attr == NULL) {
     814           1 :                 wpa_printf(MSG_INFO, "No Message-Authenticator attribute found");
     815           1 :                 return 1;
     816             :         }
     817             : 
     818       13576 :         os_memcpy(orig, attr + 1, MD5_MAC_LEN);
     819       13576 :         os_memset(attr + 1, 0, MD5_MAC_LEN);
     820       13576 :         if (req_auth) {
     821        7139 :                 os_memcpy(orig_authenticator, msg->hdr->authenticator,
     822             :                           sizeof(orig_authenticator));
     823        7139 :                 os_memcpy(msg->hdr->authenticator, req_auth,
     824             :                           sizeof(msg->hdr->authenticator));
     825             :         }
     826       13576 :         if (hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
     827       13576 :                      wpabuf_len(msg->buf), auth) < 0)
     828           3 :                 return 1;
     829       13573 :         os_memcpy(attr + 1, orig, MD5_MAC_LEN);
     830       13573 :         if (req_auth) {
     831        7136 :                 os_memcpy(msg->hdr->authenticator, orig_authenticator,
     832             :                           sizeof(orig_authenticator));
     833             :         }
     834             : 
     835       13573 :         if (os_memcmp_const(orig, auth, MD5_MAC_LEN) != 0) {
     836           2 :                 wpa_printf(MSG_INFO, "Invalid Message-Authenticator!");
     837           2 :                 return 1;
     838             :         }
     839             : 
     840       13571 :         return 0;
     841             : }
     842             : 
     843             : 
     844        7616 : int radius_msg_verify(struct radius_msg *msg, const u8 *secret,
     845             :                       size_t secret_len, struct radius_msg *sent_msg, int auth)
     846             : {
     847             :         const u8 *addr[4];
     848             :         size_t len[4];
     849             :         u8 hash[MD5_MAC_LEN];
     850             : 
     851        7616 :         if (sent_msg == NULL) {
     852           0 :                 wpa_printf(MSG_INFO, "No matching Access-Request message found");
     853           0 :                 return 1;
     854             :         }
     855             : 
     856       14757 :         if (auth &&
     857        7141 :             radius_msg_verify_msg_auth(msg, secret, secret_len,
     858        7141 :                                        sent_msg->hdr->authenticator)) {
     859           6 :                 return 1;
     860             :         }
     861             : 
     862             :         /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
     863        7610 :         addr[0] = (u8 *) msg->hdr;
     864        7610 :         len[0] = 1 + 1 + 2;
     865        7610 :         addr[1] = sent_msg->hdr->authenticator;
     866        7610 :         len[1] = MD5_MAC_LEN;
     867        7610 :         addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
     868        7610 :         len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
     869        7610 :         addr[3] = secret;
     870        7610 :         len[3] = secret_len;
     871       15218 :         if (md5_vector(4, addr, len, hash) < 0 ||
     872        7608 :             os_memcmp_const(hash, msg->hdr->authenticator, MD5_MAC_LEN) != 0) {
     873           2 :                 wpa_printf(MSG_INFO, "Response Authenticator invalid!");
     874           2 :                 return 1;
     875             :         }
     876             : 
     877        7608 :         return 0;
     878             : }
     879             : 
     880             : 
     881       11615 : int radius_msg_copy_attr(struct radius_msg *dst, struct radius_msg *src,
     882             :                          u8 type)
     883             : {
     884             :         struct radius_attr_hdr *attr;
     885             :         size_t i;
     886       11615 :         int count = 0;
     887             : 
     888      139128 :         for (i = 0; i < src->attr_used; i++) {
     889      127513 :                 attr = radius_get_attr_hdr(src, i);
     890      127513 :                 if (attr->type == type && attr->length >= sizeof(*attr)) {
     891        4764 :                         if (!radius_msg_add_attr(dst, type, (u8 *) (attr + 1),
     892        4764 :                                                  attr->length - sizeof(*attr)))
     893           0 :                                 return -1;
     894        4764 :                         count++;
     895             :                 }
     896             :         }
     897             : 
     898       11615 :         return count;
     899             : }
     900             : 
     901             : 
     902             : /* Create Request Authenticator. The value should be unique over the lifetime
     903             :  * of the shared secret between authenticator and authentication server.
     904             :  */
     905        7280 : int radius_msg_make_authenticator(struct radius_msg *msg)
     906             : {
     907        7280 :         return os_get_random((u8 *) &msg->hdr->authenticator,
     908             :                              sizeof(msg->hdr->authenticator));
     909             : }
     910             : 
     911             : 
     912             : /* Get Vendor-specific RADIUS Attribute from a parsed RADIUS message.
     913             :  * Returns the Attribute payload and sets alen to indicate the length of the
     914             :  * payload if a vendor attribute with subtype is found, otherwise returns NULL.
     915             :  * The returned payload is allocated with os_malloc() and caller must free it
     916             :  * by calling os_free().
     917             :  */
     918        2238 : static u8 *radius_msg_get_vendor_attr(struct radius_msg *msg, u32 vendor,
     919             :                                       u8 subtype, size_t *alen)
     920             : {
     921             :         u8 *data, *pos;
     922             :         size_t i, len;
     923             : 
     924        2238 :         if (msg == NULL)
     925           0 :                 return NULL;
     926             : 
     927        5592 :         for (i = 0; i < msg->attr_used; i++) {
     928        5582 :                 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
     929             :                 size_t left;
     930             :                 u32 vendor_id;
     931             :                 struct radius_attr_vendor *vhdr;
     932             : 
     933        8924 :                 if (attr->type != RADIUS_ATTR_VENDOR_SPECIFIC ||
     934        3342 :                     attr->length < sizeof(*attr))
     935        4480 :                         continue;
     936             : 
     937        3342 :                 left = attr->length - sizeof(*attr);
     938        3342 :                 if (left < 4)
     939           0 :                         continue;
     940             : 
     941        3342 :                 pos = (u8 *) (attr + 1);
     942             : 
     943        3342 :                 os_memcpy(&vendor_id, pos, 4);
     944        3342 :                 pos += 4;
     945        3342 :                 left -= 4;
     946             : 
     947        3342 :                 if (ntohl(vendor_id) != vendor)
     948           0 :                         continue;
     949             : 
     950        7798 :                 while (left >= sizeof(*vhdr)) {
     951        3342 :                         vhdr = (struct radius_attr_vendor *) pos;
     952        6684 :                         if (vhdr->vendor_length > left ||
     953        3342 :                             vhdr->vendor_length < sizeof(*vhdr)) {
     954             :                                 break;
     955             :                         }
     956        3342 :                         if (vhdr->vendor_type != subtype) {
     957        1114 :                                 pos += vhdr->vendor_length;
     958        1114 :                                 left -= vhdr->vendor_length;
     959        1114 :                                 continue;
     960             :                         }
     961             : 
     962        2228 :                         len = vhdr->vendor_length - sizeof(*vhdr);
     963        2228 :                         data = os_malloc(len);
     964        2228 :                         if (data == NULL)
     965        2230 :                                 return NULL;
     966        2226 :                         os_memcpy(data, pos + sizeof(*vhdr), len);
     967        2226 :                         if (alen)
     968        2226 :                                 *alen = len;
     969        2226 :                         return data;
     970             :                 }
     971             :         }
     972             : 
     973          10 :         return NULL;
     974             : }
     975             : 
     976             : 
     977        2226 : static u8 * decrypt_ms_key(const u8 *key, size_t len,
     978             :                            const u8 *req_authenticator,
     979             :                            const u8 *secret, size_t secret_len, size_t *reslen)
     980             : {
     981             :         u8 *plain, *ppos, *res;
     982             :         const u8 *pos;
     983             :         size_t left, plen;
     984             :         u8 hash[MD5_MAC_LEN];
     985        2226 :         int i, first = 1;
     986             :         const u8 *addr[3];
     987             :         size_t elen[3];
     988             : 
     989             :         /* key: 16-bit salt followed by encrypted key info */
     990             : 
     991        2226 :         if (len < 2 + 16) {
     992           0 :                 wpa_printf(MSG_DEBUG, "RADIUS: %s: Len is too small: %d",
     993             :                            __func__, (int) len);
     994           0 :                 return NULL;
     995             :         }
     996             : 
     997        2226 :         pos = key + 2;
     998        2226 :         left = len - 2;
     999        2226 :         if (left % 16) {
    1000           0 :                 wpa_printf(MSG_INFO, "RADIUS: Invalid ms key len %lu",
    1001             :                            (unsigned long) left);
    1002           0 :                 return NULL;
    1003             :         }
    1004             : 
    1005        2226 :         plen = left;
    1006        2226 :         ppos = plain = os_malloc(plen);
    1007        2226 :         if (plain == NULL)
    1008           1 :                 return NULL;
    1009        2225 :         plain[0] = 0;
    1010             : 
    1011       11110 :         while (left > 0) {
    1012             :                 /* b(1) = MD5(Secret + Request-Authenticator + Salt)
    1013             :                  * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
    1014             : 
    1015        6666 :                 addr[0] = secret;
    1016        6666 :                 elen[0] = secret_len;
    1017        6666 :                 if (first) {
    1018        2225 :                         addr[1] = req_authenticator;
    1019        2225 :                         elen[1] = MD5_MAC_LEN;
    1020        2225 :                         addr[2] = key;
    1021        2225 :                         elen[2] = 2; /* Salt */
    1022             :                 } else {
    1023        4441 :                         addr[1] = pos - MD5_MAC_LEN;
    1024        4441 :                         elen[1] = MD5_MAC_LEN;
    1025             :                 }
    1026        6666 :                 if (md5_vector(first ? 3 : 2, addr, elen, hash) < 0) {
    1027           6 :                         os_free(plain);
    1028           6 :                         return NULL;
    1029             :                 }
    1030        6660 :                 first = 0;
    1031             : 
    1032      113220 :                 for (i = 0; i < MD5_MAC_LEN; i++)
    1033      106560 :                         *ppos++ = *pos++ ^ hash[i];
    1034        6660 :                 left -= MD5_MAC_LEN;
    1035             :         }
    1036             : 
    1037        2219 :         if (plain[0] == 0 || plain[0] > plen - 1) {
    1038           0 :                 wpa_printf(MSG_INFO, "RADIUS: Failed to decrypt MPPE key");
    1039           0 :                 os_free(plain);
    1040           0 :                 return NULL;
    1041             :         }
    1042             : 
    1043        2219 :         res = os_malloc(plain[0]);
    1044        2219 :         if (res == NULL) {
    1045           1 :                 os_free(plain);
    1046           1 :                 return NULL;
    1047             :         }
    1048        2218 :         os_memcpy(res, plain + 1, plain[0]);
    1049        2218 :         if (reslen)
    1050        2218 :                 *reslen = plain[0];
    1051        2218 :         os_free(plain);
    1052        2218 :         return res;
    1053             : }
    1054             : 
    1055             : 
    1056        2264 : static void encrypt_ms_key(const u8 *key, size_t key_len, u16 salt,
    1057             :                            const u8 *req_authenticator,
    1058             :                            const u8 *secret, size_t secret_len,
    1059             :                            u8 *ebuf, size_t *elen)
    1060             : {
    1061        2264 :         int i, len, first = 1;
    1062             :         u8 hash[MD5_MAC_LEN], saltbuf[2], *pos;
    1063             :         const u8 *addr[3];
    1064             :         size_t _len[3];
    1065             : 
    1066        2264 :         WPA_PUT_BE16(saltbuf, salt);
    1067             : 
    1068        2264 :         len = 1 + key_len;
    1069        2264 :         if (len & 0x0f) {
    1070        2264 :                 len = (len & 0xf0) + 16;
    1071             :         }
    1072        2264 :         os_memset(ebuf, 0, len);
    1073        2264 :         ebuf[0] = key_len;
    1074        2264 :         os_memcpy(ebuf + 1, key, key_len);
    1075             : 
    1076        2264 :         *elen = len;
    1077             : 
    1078        2264 :         pos = ebuf;
    1079       11316 :         while (len > 0) {
    1080             :                 /* b(1) = MD5(Secret + Request-Authenticator + Salt)
    1081             :                  * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
    1082        6788 :                 addr[0] = secret;
    1083        6788 :                 _len[0] = secret_len;
    1084        6788 :                 if (first) {
    1085        2264 :                         addr[1] = req_authenticator;
    1086        2264 :                         _len[1] = MD5_MAC_LEN;
    1087        2264 :                         addr[2] = saltbuf;
    1088        2264 :                         _len[2] = sizeof(saltbuf);
    1089             :                 } else {
    1090        4524 :                         addr[1] = pos - MD5_MAC_LEN;
    1091        4524 :                         _len[1] = MD5_MAC_LEN;
    1092             :                 }
    1093        6788 :                 md5_vector(first ? 3 : 2, addr, _len, hash);
    1094        6788 :                 first = 0;
    1095             : 
    1096      115396 :                 for (i = 0; i < MD5_MAC_LEN; i++)
    1097      108608 :                         *pos++ ^= hash[i];
    1098             : 
    1099        6788 :                 len -= MD5_MAC_LEN;
    1100             :         }
    1101        2264 : }
    1102             : 
    1103             : 
    1104             : struct radius_ms_mppe_keys *
    1105        1119 : radius_msg_get_ms_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
    1106             :                        const u8 *secret, size_t secret_len)
    1107             : {
    1108             :         u8 *key;
    1109             :         size_t keylen;
    1110             :         struct radius_ms_mppe_keys *keys;
    1111             : 
    1112        1119 :         if (msg == NULL || sent_msg == NULL)
    1113           0 :                 return NULL;
    1114             : 
    1115        1119 :         keys = os_zalloc(sizeof(*keys));
    1116        1119 :         if (keys == NULL)
    1117           0 :                 return NULL;
    1118             : 
    1119        1119 :         key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
    1120             :                                          RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY,
    1121             :                                          &keylen);
    1122        1119 :         if (key) {
    1123        2228 :                 keys->send = decrypt_ms_key(key, keylen,
    1124        1114 :                                             sent_msg->hdr->authenticator,
    1125             :                                             secret, secret_len,
    1126             :                                             &keys->send_len);
    1127        1114 :                 if (!keys->send) {
    1128           1 :                         wpa_printf(MSG_DEBUG,
    1129             :                                    "RADIUS: Failed to decrypt send key");
    1130             :                 }
    1131        1114 :                 os_free(key);
    1132             :         }
    1133             : 
    1134        1119 :         key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
    1135             :                                          RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY,
    1136             :                                          &keylen);
    1137        1119 :         if (key) {
    1138        2224 :                 keys->recv = decrypt_ms_key(key, keylen,
    1139        1112 :                                             sent_msg->hdr->authenticator,
    1140             :                                             secret, secret_len,
    1141             :                                             &keys->recv_len);
    1142        1112 :                 if (!keys->recv) {
    1143           7 :                         wpa_printf(MSG_DEBUG,
    1144             :                                    "RADIUS: Failed to decrypt recv key");
    1145             :                 }
    1146        1112 :                 os_free(key);
    1147             :         }
    1148             : 
    1149        1119 :         return keys;
    1150             : }
    1151             : 
    1152             : 
    1153             : struct radius_ms_mppe_keys *
    1154           0 : radius_msg_get_cisco_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
    1155             :                           const u8 *secret, size_t secret_len)
    1156             : {
    1157             :         u8 *key;
    1158             :         size_t keylen;
    1159             :         struct radius_ms_mppe_keys *keys;
    1160             : 
    1161           0 :         if (msg == NULL || sent_msg == NULL)
    1162           0 :                 return NULL;
    1163             : 
    1164           0 :         keys = os_zalloc(sizeof(*keys));
    1165           0 :         if (keys == NULL)
    1166           0 :                 return NULL;
    1167             : 
    1168           0 :         key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_CISCO,
    1169             :                                          RADIUS_CISCO_AV_PAIR, &keylen);
    1170           0 :         if (key && keylen == 51 &&
    1171           0 :             os_memcmp(key, "leap:session-key=", 17) == 0) {
    1172           0 :                 keys->recv = decrypt_ms_key(key + 17, keylen - 17,
    1173           0 :                                             sent_msg->hdr->authenticator,
    1174             :                                             secret, secret_len,
    1175             :                                             &keys->recv_len);
    1176             :         }
    1177           0 :         os_free(key);
    1178             : 
    1179           0 :         return keys;
    1180             : }
    1181             : 
    1182             : 
    1183        1133 : int radius_msg_add_mppe_keys(struct radius_msg *msg,
    1184             :                              const u8 *req_authenticator,
    1185             :                              const u8 *secret, size_t secret_len,
    1186             :                              const u8 *send_key, size_t send_key_len,
    1187             :                              const u8 *recv_key, size_t recv_key_len)
    1188             : {
    1189             :         struct radius_attr_hdr *attr;
    1190        1133 :         u32 vendor_id = htonl(RADIUS_VENDOR_ID_MICROSOFT);
    1191             :         u8 *buf;
    1192             :         struct radius_attr_vendor *vhdr;
    1193             :         u8 *pos;
    1194             :         size_t elen;
    1195             :         int hlen;
    1196             :         u16 salt;
    1197             : 
    1198        1133 :         hlen = sizeof(vendor_id) + sizeof(*vhdr) + 2;
    1199             : 
    1200             :         /* MS-MPPE-Send-Key */
    1201        1133 :         buf = os_malloc(hlen + send_key_len + 16);
    1202        1133 :         if (buf == NULL) {
    1203           0 :                 return 0;
    1204             :         }
    1205        1133 :         pos = buf;
    1206        1133 :         os_memcpy(pos, &vendor_id, sizeof(vendor_id));
    1207        1133 :         pos += sizeof(vendor_id);
    1208        1133 :         vhdr = (struct radius_attr_vendor *) pos;
    1209        1133 :         vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY;
    1210        1133 :         pos = (u8 *) (vhdr + 1);
    1211        1133 :         if (os_get_random((u8 *) &salt, sizeof(salt)) < 0) {
    1212           1 :                 os_free(buf);
    1213           1 :                 return 0;
    1214             :         }
    1215        1132 :         salt |= 0x8000;
    1216        1132 :         WPA_PUT_BE16(pos, salt);
    1217        1132 :         pos += 2;
    1218        1132 :         encrypt_ms_key(send_key, send_key_len, salt, req_authenticator, secret,
    1219             :                        secret_len, pos, &elen);
    1220        1132 :         vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
    1221             : 
    1222        1132 :         attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
    1223             :                                    buf, hlen + elen);
    1224        1132 :         os_free(buf);
    1225        1132 :         if (attr == NULL) {
    1226           0 :                 return 0;
    1227             :         }
    1228             : 
    1229             :         /* MS-MPPE-Recv-Key */
    1230        1132 :         buf = os_malloc(hlen + recv_key_len + 16);
    1231        1132 :         if (buf == NULL) {
    1232           0 :                 return 0;
    1233             :         }
    1234        1132 :         pos = buf;
    1235        1132 :         os_memcpy(pos, &vendor_id, sizeof(vendor_id));
    1236        1132 :         pos += sizeof(vendor_id);
    1237        1132 :         vhdr = (struct radius_attr_vendor *) pos;
    1238        1132 :         vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY;
    1239        1132 :         pos = (u8 *) (vhdr + 1);
    1240        1132 :         salt ^= 1;
    1241        1132 :         WPA_PUT_BE16(pos, salt);
    1242        1132 :         pos += 2;
    1243        1132 :         encrypt_ms_key(recv_key, recv_key_len, salt, req_authenticator, secret,
    1244             :                        secret_len, pos, &elen);
    1245        1132 :         vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
    1246             : 
    1247        1132 :         attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
    1248             :                                    buf, hlen + elen);
    1249        1132 :         os_free(buf);
    1250        1132 :         if (attr == NULL) {
    1251           0 :                 return 0;
    1252             :         }
    1253             : 
    1254        1132 :         return 1;
    1255             : }
    1256             : 
    1257             : 
    1258        1413 : int radius_msg_add_wfa(struct radius_msg *msg, u8 subtype, const u8 *data,
    1259             :                        size_t len)
    1260             : {
    1261             :         struct radius_attr_hdr *attr;
    1262             :         u8 *buf, *pos;
    1263             :         size_t alen;
    1264             : 
    1265        1413 :         alen = 4 + 2 + len;
    1266        1413 :         buf = os_malloc(alen);
    1267        1413 :         if (buf == NULL)
    1268           0 :                 return 0;
    1269        1413 :         pos = buf;
    1270        1413 :         WPA_PUT_BE32(pos, RADIUS_VENDOR_ID_WFA);
    1271        1413 :         pos += 4;
    1272        1413 :         *pos++ = subtype;
    1273        1413 :         *pos++ = 2 + len;
    1274        1413 :         os_memcpy(pos, data, len);
    1275        1413 :         attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
    1276             :                                    buf, alen);
    1277        1413 :         os_free(buf);
    1278        1413 :         if (attr == NULL)
    1279           0 :                 return 0;
    1280             : 
    1281        1413 :         return 1;
    1282             : }
    1283             : 
    1284             : 
    1285          14 : int radius_user_password_hide(struct radius_msg *msg,
    1286             :                               const u8 *data, size_t data_len,
    1287             :                               const u8 *secret, size_t secret_len,
    1288             :                               u8 *buf, size_t buf_len)
    1289             : {
    1290             :         size_t padlen, i, pos;
    1291             :         const u8 *addr[2];
    1292             :         size_t len[2];
    1293             :         u8 hash[16];
    1294             : 
    1295          14 :         if (data_len + 16 > buf_len)
    1296           0 :                 return -1;
    1297             : 
    1298          14 :         os_memcpy(buf, data, data_len);
    1299             : 
    1300          14 :         padlen = data_len % 16;
    1301          14 :         if (padlen && data_len < buf_len) {
    1302          14 :                 padlen = 16 - padlen;
    1303          14 :                 os_memset(buf + data_len, 0, padlen);
    1304          14 :                 buf_len = data_len + padlen;
    1305             :         } else {
    1306           0 :                 buf_len = data_len;
    1307             :         }
    1308             : 
    1309          14 :         addr[0] = secret;
    1310          14 :         len[0] = secret_len;
    1311          14 :         addr[1] = msg->hdr->authenticator;
    1312          14 :         len[1] = 16;
    1313          14 :         md5_vector(2, addr, len, hash);
    1314             : 
    1315         238 :         for (i = 0; i < 16; i++)
    1316         224 :                 buf[i] ^= hash[i];
    1317          14 :         pos = 16;
    1318             : 
    1319          28 :         while (pos < buf_len) {
    1320           0 :                 addr[0] = secret;
    1321           0 :                 len[0] = secret_len;
    1322           0 :                 addr[1] = &buf[pos - 16];
    1323           0 :                 len[1] = 16;
    1324           0 :                 md5_vector(2, addr, len, hash);
    1325             : 
    1326           0 :                 for (i = 0; i < 16; i++)
    1327           0 :                         buf[pos + i] ^= hash[i];
    1328             : 
    1329           0 :                 pos += 16;
    1330             :         }
    1331             : 
    1332          14 :         return buf_len;
    1333             : }
    1334             : 
    1335             : 
    1336             : /* Add User-Password attribute to a RADIUS message and encrypt it as specified
    1337             :  * in RFC 2865, Chap. 5.2 */
    1338             : struct radius_attr_hdr *
    1339          10 : radius_msg_add_attr_user_password(struct radius_msg *msg,
    1340             :                                   const u8 *data, size_t data_len,
    1341             :                                   const u8 *secret, size_t secret_len)
    1342             : {
    1343             :         u8 buf[128];
    1344             :         int res;
    1345             : 
    1346          10 :         res = radius_user_password_hide(msg, data, data_len,
    1347             :                                         secret, secret_len, buf, sizeof(buf));
    1348          10 :         if (res < 0)
    1349           0 :                 return NULL;
    1350             : 
    1351          10 :         return radius_msg_add_attr(msg, RADIUS_ATTR_USER_PASSWORD,
    1352             :                                    buf, res);
    1353             : }
    1354             : 
    1355             : 
    1356       22056 : int radius_msg_get_attr(struct radius_msg *msg, u8 type, u8 *buf, size_t len)
    1357             : {
    1358       22056 :         struct radius_attr_hdr *attr = NULL, *tmp;
    1359             :         size_t i, dlen;
    1360             : 
    1361      174569 :         for (i = 0; i < msg->attr_used; i++) {
    1362      157509 :                 tmp = radius_get_attr_hdr(msg, i);
    1363      157509 :                 if (tmp->type == type) {
    1364        4996 :                         attr = tmp;
    1365        4996 :                         break;
    1366             :                 }
    1367             :         }
    1368             : 
    1369       22056 :         if (!attr || attr->length < sizeof(*attr))
    1370       17060 :                 return -1;
    1371             : 
    1372        4996 :         dlen = attr->length - sizeof(*attr);
    1373        4996 :         if (buf)
    1374        4810 :                 os_memcpy(buf, (attr + 1), dlen > len ? len : dlen);
    1375        4996 :         return dlen;
    1376             : }
    1377             : 
    1378             : 
    1379        7473 : int radius_msg_get_attr_ptr(struct radius_msg *msg, u8 type, u8 **buf,
    1380             :                             size_t *len, const u8 *start)
    1381             : {
    1382             :         size_t i;
    1383        7473 :         struct radius_attr_hdr *attr = NULL, *tmp;
    1384             : 
    1385       25098 :         for (i = 0; i < msg->attr_used; i++) {
    1386       21596 :                 tmp = radius_get_attr_hdr(msg, i);
    1387       21596 :                 if (tmp->type == type &&
    1388        4472 :                     (start == NULL || (u8 *) tmp > start)) {
    1389        3971 :                         attr = tmp;
    1390        3971 :                         break;
    1391             :                 }
    1392             :         }
    1393             : 
    1394        7473 :         if (!attr || attr->length < sizeof(*attr))
    1395        3502 :                 return -1;
    1396             : 
    1397        3971 :         *buf = (u8 *) (attr + 1);
    1398        3971 :         *len = attr->length - sizeof(*attr);
    1399        3971 :         return 0;
    1400             : }
    1401             : 
    1402             : 
    1403         240 : int radius_msg_count_attr(struct radius_msg *msg, u8 type, int min_len)
    1404             : {
    1405             :         size_t i;
    1406             :         int count;
    1407             : 
    1408        1208 :         for (count = 0, i = 0; i < msg->attr_used; i++) {
    1409         968 :                 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
    1410         973 :                 if (attr->type == type &&
    1411           5 :                     attr->length >= sizeof(struct radius_attr_hdr) + min_len)
    1412           5 :                         count++;
    1413             :         }
    1414             : 
    1415         240 :         return count;
    1416             : }
    1417             : 
    1418             : 
    1419             : struct radius_tunnel_attrs {
    1420             :         int tag_used;
    1421             :         int type; /* Tunnel-Type */
    1422             :         int medium_type; /* Tunnel-Medium-Type */
    1423             :         int vlanid;
    1424             : };
    1425             : 
    1426             : 
    1427           0 : static int cmp_int(const void *a, const void *b)
    1428             : {
    1429             :         int x, y;
    1430             : 
    1431           0 :         x = *((int *) a);
    1432           0 :         y = *((int *) b);
    1433           0 :         return (x - y);
    1434             : }
    1435             : 
    1436             : 
    1437             : /**
    1438             :  * radius_msg_get_vlanid - Parse RADIUS attributes for VLAN tunnel information
    1439             :  * The k tagged vlans found are sorted by vlan_id and stored in the first k
    1440             :  * items of tagged.
    1441             :  *
    1442             :  * @msg: RADIUS message
    1443             :  * @untagged: Pointer to store untagged vid
    1444             :  * @numtagged: Size of tagged
    1445             :  * @tagged: Pointer to store tagged list
    1446             :  *
    1447             :  * Returns: 0 if neither tagged nor untagged configuration is found, 1 otherwise
    1448             :  */
    1449          40 : int radius_msg_get_vlanid(struct radius_msg *msg, int *untagged, int numtagged,
    1450             :                           int *tagged)
    1451             : {
    1452             :         struct radius_tunnel_attrs tunnel[RADIUS_TUNNEL_TAGS], *tun;
    1453             :         size_t i;
    1454          40 :         struct radius_attr_hdr *attr = NULL;
    1455             :         const u8 *data;
    1456             :         char buf[10];
    1457             :         size_t dlen;
    1458          40 :         int j, taggedidx = 0, vlan_id;
    1459             : 
    1460          40 :         os_memset(&tunnel, 0, sizeof(tunnel));
    1461        1320 :         for (j = 0; j < numtagged; j++)
    1462        1280 :                 tagged[j] = 0;
    1463          40 :         *untagged = 0;
    1464             : 
    1465         239 :         for (i = 0; i < msg->attr_used; i++) {
    1466         199 :                 attr = radius_get_attr_hdr(msg, i);
    1467         199 :                 if (attr->length < sizeof(*attr))
    1468           0 :                         return -1;
    1469         199 :                 data = (const u8 *) (attr + 1);
    1470         199 :                 dlen = attr->length - sizeof(*attr);
    1471         199 :                 if (attr->length < 3)
    1472           0 :                         continue;
    1473         199 :                 if (data[0] >= RADIUS_TUNNEL_TAGS)
    1474          65 :                         tun = &tunnel[0];
    1475             :                 else
    1476         134 :                         tun = &tunnel[data[0]];
    1477             : 
    1478         199 :                 switch (attr->type) {
    1479             :                 case RADIUS_ATTR_TUNNEL_TYPE:
    1480          20 :                         if (attr->length != 6)
    1481           0 :                                 break;
    1482          20 :                         tun->tag_used++;
    1483          20 :                         tun->type = WPA_GET_BE24(data + 1);
    1484          20 :                         break;
    1485             :                 case RADIUS_ATTR_TUNNEL_MEDIUM_TYPE:
    1486          20 :                         if (attr->length != 6)
    1487           0 :                                 break;
    1488          20 :                         tun->tag_used++;
    1489          20 :                         tun->medium_type = WPA_GET_BE24(data + 1);
    1490          20 :                         break;
    1491             :                 case RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID:
    1492          20 :                         if (data[0] < RADIUS_TUNNEL_TAGS) {
    1493           0 :                                 data++;
    1494           0 :                                 dlen--;
    1495             :                         }
    1496          20 :                         if (dlen >= sizeof(buf))
    1497           0 :                                 break;
    1498          20 :                         os_memcpy(buf, data, dlen);
    1499          20 :                         buf[dlen] = '\0';
    1500          20 :                         vlan_id = atoi(buf);
    1501          20 :                         if (vlan_id <= 0)
    1502           0 :                                 break;
    1503          20 :                         tun->tag_used++;
    1504          20 :                         tun->vlanid = vlan_id;
    1505          20 :                         break;
    1506             :                 case RADIUS_ATTR_EGRESS_VLANID: /* RFC 4675 */
    1507           6 :                         if (attr->length != 6)
    1508           0 :                                 break;
    1509           6 :                         vlan_id = WPA_GET_BE24(data + 1);
    1510           6 :                         if (vlan_id <= 0)
    1511           0 :                                 break;
    1512           6 :                         if (data[0] == 0x32)
    1513           1 :                                 *untagged = vlan_id;
    1514           5 :                         else if (data[0] == 0x31 && tagged &&
    1515             :                                  taggedidx < numtagged)
    1516           5 :                                 tagged[taggedidx++] = vlan_id;
    1517           6 :                         break;
    1518             :                 }
    1519             :         }
    1520             : 
    1521             :         /* Use tunnel with the lowest tag for untagged VLAN id */
    1522         680 :         for (i = 0; i < RADIUS_TUNNEL_TAGS; i++) {
    1523         660 :                 tun = &tunnel[i];
    1524         680 :                 if (tun->tag_used &&
    1525          40 :                     tun->type == RADIUS_TUNNEL_TYPE_VLAN &&
    1526          40 :                     tun->medium_type == RADIUS_TUNNEL_MEDIUM_TYPE_802 &&
    1527          20 :                     tun->vlanid > 0) {
    1528          20 :                         *untagged = tun->vlanid;
    1529          20 :                         break;
    1530             :                 }
    1531             :         }
    1532             : 
    1533          40 :         if (taggedidx)
    1534           5 :                 qsort(tagged, taggedidx, sizeof(int), cmp_int);
    1535             : 
    1536          40 :         if (*untagged > 0 || taggedidx)
    1537          25 :                 return 1;
    1538          15 :         return 0;
    1539             : }
    1540             : 
    1541             : 
    1542             : /**
    1543             :  * radius_msg_get_tunnel_password - Parse RADIUS attribute Tunnel-Password
    1544             :  * @msg: Received RADIUS message
    1545             :  * @keylen: Length of returned password
    1546             :  * @secret: RADIUS shared secret
    1547             :  * @secret_len: Length of secret
    1548             :  * @sent_msg: Sent RADIUS message
    1549             :  * @n: Number of password attribute to return (starting with 0)
    1550             :  * Returns: Pointer to n-th password (free with os_free) or %NULL
    1551             :  */
    1552          13 : char * radius_msg_get_tunnel_password(struct radius_msg *msg, int *keylen,
    1553             :                                       const u8 *secret, size_t secret_len,
    1554             :                                       struct radius_msg *sent_msg, size_t n)
    1555             : {
    1556          13 :         u8 *buf = NULL;
    1557             :         size_t buflen;
    1558             :         const u8 *salt;
    1559             :         u8 *str;
    1560             :         const u8 *addr[3];
    1561             :         size_t len[3];
    1562             :         u8 hash[16];
    1563             :         u8 *pos;
    1564          13 :         size_t i, j = 0;
    1565             :         struct radius_attr_hdr *attr;
    1566             :         const u8 *data;
    1567             :         size_t dlen;
    1568          13 :         const u8 *fdata = NULL; /* points to found item */
    1569          13 :         size_t fdlen = -1;
    1570          13 :         char *ret = NULL;
    1571             : 
    1572             :         /* find n-th valid Tunnel-Password attribute */
    1573          29 :         for (i = 0; i < msg->attr_used; i++) {
    1574          19 :                 attr = radius_get_attr_hdr(msg, i);
    1575          38 :                 if (attr == NULL ||
    1576          19 :                     attr->type != RADIUS_ATTR_TUNNEL_PASSWORD) {
    1577          13 :                         continue;
    1578             :                 }
    1579           6 :                 if (attr->length <= 5)
    1580           0 :                         continue;
    1581           6 :                 data = (const u8 *) (attr + 1);
    1582           6 :                 dlen = attr->length - sizeof(*attr);
    1583           6 :                 if (dlen <= 3 || dlen % 16 != 3)
    1584           0 :                         continue;
    1585           6 :                 j++;
    1586           6 :                 if (j <= n)
    1587           3 :                         continue;
    1588             : 
    1589           3 :                 fdata = data;
    1590           3 :                 fdlen = dlen;
    1591           3 :                 break;
    1592             :         }
    1593          13 :         if (fdata == NULL)
    1594          10 :                 goto out;
    1595             : 
    1596             :         /* alloc writable memory for decryption */
    1597           3 :         buf = os_malloc(fdlen);
    1598           3 :         if (buf == NULL)
    1599           0 :                 goto out;
    1600           3 :         os_memcpy(buf, fdata, fdlen);
    1601           3 :         buflen = fdlen;
    1602             : 
    1603             :         /* init pointers */
    1604           3 :         salt = buf + 1;
    1605           3 :         str = buf + 3;
    1606             : 
    1607             :         /* decrypt blocks */
    1608           3 :         pos = buf + buflen - 16; /* last block */
    1609           7 :         while (pos >= str + 16) { /* all but the first block */
    1610           1 :                 addr[0] = secret;
    1611           1 :                 len[0] = secret_len;
    1612           1 :                 addr[1] = pos - 16;
    1613           1 :                 len[1] = 16;
    1614           1 :                 md5_vector(2, addr, len, hash);
    1615             : 
    1616          17 :                 for (i = 0; i < 16; i++)
    1617          16 :                         pos[i] ^= hash[i];
    1618             : 
    1619           1 :                 pos -= 16;
    1620             :         }
    1621             : 
    1622             :         /* decrypt first block */
    1623           3 :         if (str != pos)
    1624           0 :                 goto out;
    1625           3 :         addr[0] = secret;
    1626           3 :         len[0] = secret_len;
    1627           3 :         addr[1] = sent_msg->hdr->authenticator;
    1628           3 :         len[1] = 16;
    1629           3 :         addr[2] = salt;
    1630           3 :         len[2] = 2;
    1631           3 :         md5_vector(3, addr, len, hash);
    1632             : 
    1633          51 :         for (i = 0; i < 16; i++)
    1634          48 :                 pos[i] ^= hash[i];
    1635             : 
    1636             :         /* derive plaintext length from first subfield */
    1637           3 :         *keylen = (unsigned char) str[0];
    1638           3 :         if ((u8 *) (str + *keylen) >= (u8 *) (buf + buflen)) {
    1639             :                 /* decryption error - invalid key length */
    1640           0 :                 goto out;
    1641             :         }
    1642           3 :         if (*keylen == 0) {
    1643             :                 /* empty password */
    1644           0 :                 goto out;
    1645             :         }
    1646             : 
    1647             :         /* copy passphrase into new buffer */
    1648           3 :         ret = os_malloc(*keylen);
    1649           3 :         if (ret)
    1650           3 :                 os_memcpy(ret, str + 1, *keylen);
    1651             : 
    1652             : out:
    1653             :         /* return new buffer */
    1654          13 :         os_free(buf);
    1655          13 :         return ret;
    1656             : }
    1657             : 
    1658             : 
    1659        3906 : void radius_free_class(struct radius_class_data *c)
    1660             : {
    1661             :         size_t i;
    1662        3906 :         if (c == NULL)
    1663        3906 :                 return;
    1664        3918 :         for (i = 0; i < c->count; i++)
    1665          12 :                 os_free(c->attr[i].data);
    1666        3906 :         os_free(c->attr);
    1667        3906 :         c->attr = NULL;
    1668        3906 :         c->count = 0;
    1669             : }
    1670             : 
    1671             : 
    1672        1162 : int radius_copy_class(struct radius_class_data *dst,
    1673             :                       const struct radius_class_data *src)
    1674             : {
    1675             :         size_t i;
    1676             : 
    1677        1162 :         if (src->attr == NULL)
    1678        1155 :                 return 0;
    1679             : 
    1680           7 :         dst->attr = os_calloc(src->count, sizeof(struct radius_attr_data));
    1681           7 :         if (dst->attr == NULL)
    1682           0 :                 return -1;
    1683             : 
    1684           7 :         dst->count = 0;
    1685             : 
    1686          14 :         for (i = 0; i < src->count; i++) {
    1687           7 :                 dst->attr[i].data = os_malloc(src->attr[i].len);
    1688           7 :                 if (dst->attr[i].data == NULL)
    1689           0 :                         break;
    1690           7 :                 dst->count++;
    1691           7 :                 os_memcpy(dst->attr[i].data, src->attr[i].data,
    1692             :                           src->attr[i].len);
    1693           7 :                 dst->attr[i].len = src->attr[i].len;
    1694             :         }
    1695             : 
    1696           7 :         return 0;
    1697             : }
    1698             : 
    1699             : 
    1700          24 : u8 radius_msg_find_unlisted_attr(struct radius_msg *msg, u8 *attrs)
    1701             : {
    1702             :         size_t i, j;
    1703             :         struct radius_attr_hdr *attr;
    1704             : 
    1705          93 :         for (i = 0; i < msg->attr_used; i++) {
    1706          70 :                 attr = radius_get_attr_hdr(msg, i);
    1707             : 
    1708         338 :                 for (j = 0; attrs[j]; j++) {
    1709         337 :                         if (attr->type == attrs[j])
    1710          69 :                                 break;
    1711             :                 }
    1712             : 
    1713          70 :                 if (attrs[j] == 0)
    1714           1 :                         return attr->type; /* unlisted attr */
    1715             :         }
    1716             : 
    1717          23 :         return 0;
    1718             : }
    1719             : 
    1720             : 
    1721        8279 : int radius_gen_session_id(u8 *id, size_t len)
    1722             : {
    1723             :         /*
    1724             :          * Acct-Session-Id and Acct-Multi-Session-Id should be globally and
    1725             :          * temporarily unique. A high quality random number is required
    1726             :          * therefore. This could be be improved by switching to a GUID.
    1727             :          */
    1728        8279 :         return os_get_random(id, len);
    1729             : }

Generated by: LCOV version 1.10