LCOV - code coverage report
Current view: top level - radius - radius.c (source / functions) Hit Total Coverage
Test: hostapd hwsim test run 1412854115 Lines: 606 780 77.7 %
Date: 2014-10-09 Functions: 43 44 97.7 %

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

Generated by: LCOV version 1.10