RADIUS: Remove unused write
[mech_eap.git] / src / radius / radius.c
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 struct radius_hdr * radius_msg_get_hdr(struct radius_msg *msg)
53 {
54         return msg->hdr;
55 }
56
57
58 struct wpabuf * radius_msg_get_buf(struct radius_msg *msg)
59 {
60         return msg->buf;
61 }
62
63
64 static struct radius_attr_hdr *
65 radius_get_attr_hdr(struct radius_msg *msg, int idx)
66 {
67         return (struct radius_attr_hdr *)
68                 (wpabuf_mhead_u8(msg->buf) + msg->attr_pos[idx]);
69 }
70
71
72 static void radius_msg_set_hdr(struct radius_msg *msg, u8 code, u8 identifier)
73 {
74         msg->hdr->code = code;
75         msg->hdr->identifier = identifier;
76 }
77
78
79 static int radius_msg_initialize(struct radius_msg *msg)
80 {
81         msg->attr_pos = os_calloc(RADIUS_DEFAULT_ATTR_COUNT,
82                                   sizeof(*msg->attr_pos));
83         if (msg->attr_pos == NULL)
84                 return -1;
85
86         msg->attr_size = RADIUS_DEFAULT_ATTR_COUNT;
87         msg->attr_used = 0;
88
89         return 0;
90 }
91
92
93 /**
94  * radius_msg_new - Create a new RADIUS message
95  * @code: Code for RADIUS header
96  * @identifier: Identifier for RADIUS header
97  * Returns: Context for RADIUS message or %NULL on failure
98  *
99  * The caller is responsible for freeing the returned data with
100  * radius_msg_free().
101  */
102 struct radius_msg * radius_msg_new(u8 code, u8 identifier)
103 {
104         struct radius_msg *msg;
105
106         msg = os_zalloc(sizeof(*msg));
107         if (msg == NULL)
108                 return NULL;
109
110         msg->buf = wpabuf_alloc(RADIUS_DEFAULT_MSG_SIZE);
111         if (msg->buf == NULL || radius_msg_initialize(msg)) {
112                 radius_msg_free(msg);
113                 return NULL;
114         }
115         msg->hdr = wpabuf_put(msg->buf, sizeof(struct radius_hdr));
116
117         radius_msg_set_hdr(msg, code, identifier);
118
119         return msg;
120 }
121
122
123 /**
124  * radius_msg_free - Free a RADIUS message
125  * @msg: RADIUS message from radius_msg_new() or radius_msg_parse()
126  */
127 void radius_msg_free(struct radius_msg *msg)
128 {
129         if (msg == NULL)
130                 return;
131
132         wpabuf_free(msg->buf);
133         os_free(msg->attr_pos);
134         os_free(msg);
135 }
136
137
138 static const char *radius_code_string(u8 code)
139 {
140         switch (code) {
141         case RADIUS_CODE_ACCESS_REQUEST: return "Access-Request";
142         case RADIUS_CODE_ACCESS_ACCEPT: return "Access-Accept";
143         case RADIUS_CODE_ACCESS_REJECT: return "Access-Reject";
144         case RADIUS_CODE_ACCOUNTING_REQUEST: return "Accounting-Request";
145         case RADIUS_CODE_ACCOUNTING_RESPONSE: return "Accounting-Response";
146         case RADIUS_CODE_ACCESS_CHALLENGE: return "Access-Challenge";
147         case RADIUS_CODE_STATUS_SERVER: return "Status-Server";
148         case RADIUS_CODE_STATUS_CLIENT: return "Status-Client";
149         case RADIUS_CODE_RESERVED: return "Reserved";
150         case RADIUS_CODE_DISCONNECT_REQUEST: return "Disconnect-Request";
151         case RADIUS_CODE_DISCONNECT_ACK: return "Disconnect-ACK";
152         case RADIUS_CODE_DISCONNECT_NAK: return "Disconnect-NAK";
153         case RADIUS_CODE_COA_REQUEST: return "CoA-Request";
154         case RADIUS_CODE_COA_ACK: return "CoA-ACK";
155         case RADIUS_CODE_COA_NAK: return "CoA-NAK";
156         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 static struct radius_attr_type *radius_get_attr_type(u8 type)
252 {
253         size_t i;
254
255         for (i = 0; i < RADIUS_ATTRS; i++) {
256                 if (type == radius_attrs[i].type)
257                         return &radius_attrs[i];
258         }
259
260         return NULL;
261 }
262
263
264 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         attr = radius_get_attr_type(hdr->type);
272
273         wpa_printf(MSG_INFO, "   Attribute %d (%s) length=%d",
274                    hdr->type, attr ? attr->name : "?Unknown?", hdr->length);
275
276         if (attr == NULL || hdr->length < sizeof(struct radius_attr_hdr))
277                 return;
278
279         len = hdr->length - sizeof(struct radius_attr_hdr);
280         pos = (unsigned char *) (hdr + 1);
281
282         switch (attr->data_type) {
283         case RADIUS_ATTR_TEXT:
284                 printf_encode(buf, sizeof(buf), pos, len);
285                 wpa_printf(MSG_INFO, "      Value: '%s'", buf);
286                 break;
287
288         case RADIUS_ATTR_IP:
289                 if (len == 4) {
290                         struct in_addr addr;
291                         os_memcpy(&addr, pos, 4);
292                         wpa_printf(MSG_INFO, "      Value: %s",
293                                    inet_ntoa(addr));
294                 } else {
295                         wpa_printf(MSG_INFO, "      Invalid IP address length %d",
296                                    len);
297                 }
298                 break;
299
300 #ifdef CONFIG_IPV6
301         case RADIUS_ATTR_IPV6:
302                 if (len == 16) {
303                         const char *atxt;
304                         struct in6_addr *addr = (struct in6_addr *) pos;
305                         atxt = inet_ntop(AF_INET6, addr, buf, sizeof(buf));
306                         wpa_printf(MSG_INFO, "      Value: %s",
307                                    atxt ? atxt : "?");
308                 } else {
309                         wpa_printf(MSG_INFO, "      Invalid IPv6 address length %d",
310                                    len);
311                 }
312                 break;
313 #endif /* CONFIG_IPV6 */
314
315         case RADIUS_ATTR_HEXDUMP:
316         case RADIUS_ATTR_UNDIST:
317                 wpa_snprintf_hex(buf, sizeof(buf), pos, len);
318                 wpa_printf(MSG_INFO, "      Value: %s", buf);
319                 break;
320
321         case RADIUS_ATTR_INT32:
322                 if (len == 4)
323                         wpa_printf(MSG_INFO, "      Value: %u",
324                                    WPA_GET_BE32(pos));
325                 else
326                         wpa_printf(MSG_INFO, "      Invalid INT32 length %d",
327                                    len);
328                 break;
329
330         default:
331                 break;
332         }
333 }
334
335
336 void radius_msg_dump(struct radius_msg *msg)
337 {
338         size_t i;
339
340         wpa_printf(MSG_INFO, "RADIUS message: code=%d (%s) identifier=%d length=%d",
341                    msg->hdr->code, radius_code_string(msg->hdr->code),
342                    msg->hdr->identifier, be_to_host16(msg->hdr->length));
343
344         for (i = 0; i < msg->attr_used; i++) {
345                 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
346                 radius_msg_dump_attr(attr);
347         }
348 }
349
350
351 int radius_msg_finish(struct radius_msg *msg, const u8 *secret,
352                       size_t secret_len)
353 {
354         if (secret) {
355                 u8 auth[MD5_MAC_LEN];
356                 struct radius_attr_hdr *attr;
357
358                 os_memset(auth, 0, MD5_MAC_LEN);
359                 attr = radius_msg_add_attr(msg,
360                                            RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
361                                            auth, MD5_MAC_LEN);
362                 if (attr == NULL) {
363                         wpa_printf(MSG_WARNING, "RADIUS: Could not add "
364                                    "Message-Authenticator");
365                         return -1;
366                 }
367                 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
368                 hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
369                          wpabuf_len(msg->buf), (u8 *) (attr + 1));
370         } else
371                 msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
372
373         if (wpabuf_len(msg->buf) > 0xffff) {
374                 wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
375                            (unsigned long) wpabuf_len(msg->buf));
376                 return -1;
377         }
378         return 0;
379 }
380
381
382 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         os_memset(auth, 0, MD5_MAC_LEN);
391         attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
392                                    auth, MD5_MAC_LEN);
393         if (attr == NULL) {
394                 wpa_printf(MSG_ERROR, "WARNING: Could not add Message-Authenticator");
395                 return -1;
396         }
397         msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
398         os_memcpy(msg->hdr->authenticator, req_authenticator,
399                   sizeof(msg->hdr->authenticator));
400         hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
401                  wpabuf_len(msg->buf), (u8 *) (attr + 1));
402
403         /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
404         addr[0] = (u8 *) msg->hdr;
405         len[0] = 1 + 1 + 2;
406         addr[1] = req_authenticator;
407         len[1] = MD5_MAC_LEN;
408         addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
409         len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
410         addr[3] = secret;
411         len[3] = secret_len;
412         md5_vector(4, addr, len, msg->hdr->authenticator);
413
414         if (wpabuf_len(msg->buf) > 0xffff) {
415                 wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
416                            (unsigned long) wpabuf_len(msg->buf));
417                 return -1;
418         }
419         return 0;
420 }
421
422
423 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         os_memset(auth, 0, MD5_MAC_LEN);
433         attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
434                                    auth, MD5_MAC_LEN);
435         if (attr == NULL) {
436                 wpa_printf(MSG_WARNING, "Could not add Message-Authenticator");
437                 return -1;
438         }
439
440         msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
441         os_memcpy(msg->hdr->authenticator, req_hdr->authenticator, 16);
442         hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
443                  wpabuf_len(msg->buf), (u8 *) (attr + 1));
444
445         /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
446         addr[0] = wpabuf_head_u8(msg->buf);
447         len[0] = wpabuf_len(msg->buf);
448         addr[1] = secret;
449         len[1] = secret_len;
450         if (md5_vector(2, addr, len, msg->hdr->authenticator) < 0)
451                 return -1;
452
453         if (wpabuf_len(msg->buf) > 0xffff) {
454                 wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
455                            (unsigned long) wpabuf_len(msg->buf));
456                 return -1;
457         }
458         return 0;
459 }
460
461
462 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         msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
469         os_memset(msg->hdr->authenticator, 0, MD5_MAC_LEN);
470         addr[0] = wpabuf_head(msg->buf);
471         len[0] = wpabuf_len(msg->buf);
472         addr[1] = secret;
473         len[1] = secret_len;
474         md5_vector(2, addr, len, msg->hdr->authenticator);
475
476         if (wpabuf_len(msg->buf) > 0xffff) {
477                 wpa_printf(MSG_WARNING, "RADIUS: Too long messages (%lu)",
478                            (unsigned long) wpabuf_len(msg->buf));
479         }
480 }
481
482
483 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         msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
490         os_memcpy(msg->hdr->authenticator, req_authenticator, MD5_MAC_LEN);
491         addr[0] = wpabuf_head(msg->buf);
492         len[0] = wpabuf_len(msg->buf);
493         addr[1] = secret;
494         len[1] = secret_len;
495         md5_vector(2, addr, len, msg->hdr->authenticator);
496
497         if (wpabuf_len(msg->buf) > 0xffff) {
498                 wpa_printf(MSG_WARNING, "RADIUS: Too long messages (%lu)",
499                            (unsigned long) wpabuf_len(msg->buf));
500         }
501 }
502
503
504 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         os_memset(zero, 0, sizeof(zero));
513         addr[0] = (u8 *) msg->hdr;
514         len[0] = sizeof(struct radius_hdr) - MD5_MAC_LEN;
515         addr[1] = zero;
516         len[1] = MD5_MAC_LEN;
517         addr[2] = (u8 *) (msg->hdr + 1);
518         len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
519         addr[3] = secret;
520         len[3] = secret_len;
521         md5_vector(4, addr, len, hash);
522         return os_memcmp_const(msg->hdr->authenticator, hash, MD5_MAC_LEN) != 0;
523 }
524
525
526 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         struct radius_attr_hdr *attr = NULL, *tmp;
537         size_t i;
538
539         os_memset(zero, 0, sizeof(zero));
540         addr[0] = (u8 *) msg->hdr;
541         len[0] = sizeof(struct radius_hdr) - MD5_MAC_LEN;
542         addr[1] = zero;
543         len[1] = MD5_MAC_LEN;
544         addr[2] = (u8 *) (msg->hdr + 1);
545         len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
546         addr[3] = secret;
547         len[3] = secret_len;
548         md5_vector(4, addr, len, hash);
549         if (os_memcmp_const(msg->hdr->authenticator, hash, MD5_MAC_LEN) != 0)
550                 return 1;
551
552         for (i = 0; i < msg->attr_used; i++) {
553                 tmp = radius_get_attr_hdr(msg, i);
554                 if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
555                         if (attr != NULL) {
556                                 wpa_printf(MSG_WARNING, "Multiple "
557                                            "Message-Authenticator attributes "
558                                            "in RADIUS message");
559                                 return 1;
560                         }
561                         attr = tmp;
562                 }
563         }
564
565         if (attr == NULL) {
566                 /* Message-Authenticator is MAY; not required */
567                 return 0;
568         }
569
570         os_memcpy(orig, attr + 1, MD5_MAC_LEN);
571         os_memset(attr + 1, 0, MD5_MAC_LEN);
572         os_memcpy(orig_authenticator, msg->hdr->authenticator,
573                   sizeof(orig_authenticator));
574         os_memset(msg->hdr->authenticator, 0,
575                   sizeof(msg->hdr->authenticator));
576         hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
577                  wpabuf_len(msg->buf), auth);
578         os_memcpy(attr + 1, orig, MD5_MAC_LEN);
579         os_memcpy(msg->hdr->authenticator, orig_authenticator,
580                   sizeof(orig_authenticator));
581
582         return os_memcmp_const(orig, auth, MD5_MAC_LEN) != 0;
583 }
584
585
586 static int radius_msg_add_attr_to_array(struct radius_msg *msg,
587                                         struct radius_attr_hdr *attr)
588 {
589         if (msg->attr_used >= msg->attr_size) {
590                 size_t *nattr_pos;
591                 int nlen = msg->attr_size * 2;
592
593                 nattr_pos = os_realloc_array(msg->attr_pos, nlen,
594                                              sizeof(*msg->attr_pos));
595                 if (nattr_pos == NULL)
596                         return -1;
597
598                 msg->attr_pos = nattr_pos;
599                 msg->attr_size = nlen;
600         }
601
602         msg->attr_pos[msg->attr_used++] =
603                 (unsigned char *) attr - wpabuf_head_u8(msg->buf);
604
605         return 0;
606 }
607
608
609 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         if (data_len > RADIUS_MAX_ATTR_LEN) {
616                 wpa_printf(MSG_ERROR, "radius_msg_add_attr: too long attribute (%lu bytes)",
617                        (unsigned long) data_len);
618                 return NULL;
619         }
620
621         buf_needed = sizeof(*attr) + data_len;
622
623         if (wpabuf_tailroom(msg->buf) < buf_needed) {
624                 /* allocate more space for message buffer */
625                 if (wpabuf_resize(&msg->buf, buf_needed) < 0)
626                         return NULL;
627                 msg->hdr = wpabuf_mhead(msg->buf);
628         }
629
630         attr = wpabuf_put(msg->buf, sizeof(struct radius_attr_hdr));
631         attr->type = type;
632         attr->length = sizeof(*attr) + data_len;
633         wpabuf_put_data(msg->buf, data, data_len);
634
635         if (radius_msg_add_attr_to_array(msg, attr))
636                 return NULL;
637
638         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 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         if (data == NULL || len < sizeof(*hdr))
660                 return NULL;
661
662         hdr = (struct radius_hdr *) data;
663
664         msg_len = be_to_host16(hdr->length);
665         if (msg_len < sizeof(*hdr) || msg_len > len) {
666                 wpa_printf(MSG_INFO, "RADIUS: Invalid message length");
667                 return NULL;
668         }
669
670         if (msg_len < len) {
671                 wpa_printf(MSG_DEBUG, "RADIUS: Ignored %lu extra bytes after "
672                            "RADIUS message", (unsigned long) len - msg_len);
673         }
674
675         msg = os_zalloc(sizeof(*msg));
676         if (msg == NULL)
677                 return NULL;
678
679         msg->buf = wpabuf_alloc_copy(data, msg_len);
680         if (msg->buf == NULL || radius_msg_initialize(msg)) {
681                 radius_msg_free(msg);
682                 return NULL;
683         }
684         msg->hdr = wpabuf_mhead(msg->buf);
685
686         /* parse attributes */
687         pos = wpabuf_mhead_u8(msg->buf) + sizeof(struct radius_hdr);
688         end = wpabuf_mhead_u8(msg->buf) + wpabuf_len(msg->buf);
689         while (pos < end) {
690                 if ((size_t) (end - pos) < sizeof(*attr))
691                         goto fail;
692
693                 attr = (struct radius_attr_hdr *) pos;
694
695                 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                 if (radius_msg_add_attr_to_array(msg, attr))
701                         goto fail;
702
703                 pos += attr->length;
704         }
705
706         return msg;
707
708  fail:
709         radius_msg_free(msg);
710         return NULL;
711 }
712
713
714 int radius_msg_add_eap(struct radius_msg *msg, const u8 *data, size_t data_len)
715 {
716         const u8 *pos = data;
717         size_t left = data_len;
718
719         while (left > 0) {
720                 int len;
721                 if (left > RADIUS_MAX_ATTR_LEN)
722                         len = RADIUS_MAX_ATTR_LEN;
723                 else
724                         len = left;
725
726                 if (!radius_msg_add_attr(msg, RADIUS_ATTR_EAP_MESSAGE,
727                                          pos, len))
728                         return 0;
729
730                 pos += len;
731                 left -= len;
732         }
733
734         return 1;
735 }
736
737
738 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         if (msg == NULL)
745                 return NULL;
746
747         len = 0;
748         for (i = 0; i < msg->attr_used; i++) {
749                 attr = radius_get_attr_hdr(msg, i);
750                 if (attr->type == RADIUS_ATTR_EAP_MESSAGE &&
751                     attr->length > sizeof(struct radius_attr_hdr))
752                         len += attr->length - sizeof(struct radius_attr_hdr);
753         }
754
755         if (len == 0)
756                 return NULL;
757
758         eap = wpabuf_alloc(len);
759         if (eap == NULL)
760                 return NULL;
761
762         for (i = 0; i < msg->attr_used; i++) {
763                 attr = radius_get_attr_hdr(msg, i);
764                 if (attr->type == RADIUS_ATTR_EAP_MESSAGE &&
765                     attr->length > sizeof(struct radius_attr_hdr)) {
766                         int flen = attr->length - sizeof(*attr);
767                         wpabuf_put_data(eap, attr + 1, flen);
768                 }
769         }
770
771         return eap;
772 }
773
774
775 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         struct radius_attr_hdr *attr = NULL, *tmp;
781         size_t i;
782
783         for (i = 0; i < msg->attr_used; i++) {
784                 tmp = radius_get_attr_hdr(msg, i);
785                 if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
786                         if (attr != NULL) {
787                                 wpa_printf(MSG_INFO, "Multiple Message-Authenticator attributes in RADIUS message");
788                                 return 1;
789                         }
790                         attr = tmp;
791                 }
792         }
793
794         if (attr == NULL) {
795                 wpa_printf(MSG_INFO, "No Message-Authenticator attribute found");
796                 return 1;
797         }
798
799         os_memcpy(orig, attr + 1, MD5_MAC_LEN);
800         os_memset(attr + 1, 0, MD5_MAC_LEN);
801         if (req_auth) {
802                 os_memcpy(orig_authenticator, msg->hdr->authenticator,
803                           sizeof(orig_authenticator));
804                 os_memcpy(msg->hdr->authenticator, req_auth,
805                           sizeof(msg->hdr->authenticator));
806         }
807         hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
808                  wpabuf_len(msg->buf), auth);
809         os_memcpy(attr + 1, orig, MD5_MAC_LEN);
810         if (req_auth) {
811                 os_memcpy(msg->hdr->authenticator, orig_authenticator,
812                           sizeof(orig_authenticator));
813         }
814
815         if (os_memcmp_const(orig, auth, MD5_MAC_LEN) != 0) {
816                 wpa_printf(MSG_INFO, "Invalid Message-Authenticator!");
817                 return 1;
818         }
819
820         return 0;
821 }
822
823
824 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         if (sent_msg == NULL) {
832                 wpa_printf(MSG_INFO, "No matching Access-Request message found");
833                 return 1;
834         }
835
836         if (auth &&
837             radius_msg_verify_msg_auth(msg, secret, secret_len,
838                                        sent_msg->hdr->authenticator)) {
839                 return 1;
840         }
841
842         /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
843         addr[0] = (u8 *) msg->hdr;
844         len[0] = 1 + 1 + 2;
845         addr[1] = sent_msg->hdr->authenticator;
846         len[1] = MD5_MAC_LEN;
847         addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
848         len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
849         addr[3] = secret;
850         len[3] = secret_len;
851         md5_vector(4, addr, len, hash);
852         if (os_memcmp_const(hash, msg->hdr->authenticator, MD5_MAC_LEN) != 0) {
853                 wpa_printf(MSG_INFO, "Response Authenticator invalid!");
854                 return 1;
855         }
856
857         return 0;
858 }
859
860
861 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         int count = 0;
867
868         for (i = 0; i < src->attr_used; i++) {
869                 attr = radius_get_attr_hdr(src, i);
870                 if (attr->type == type && attr->length >= sizeof(*attr)) {
871                         if (!radius_msg_add_attr(dst, type, (u8 *) (attr + 1),
872                                                  attr->length - sizeof(*attr)))
873                                 return -1;
874                         count++;
875                 }
876         }
877
878         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 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         os_get_time(&tv);
895         l = os_random();
896         addr[0] = (u8 *) &tv;
897         elen[0] = sizeof(tv);
898         addr[1] = data;
899         elen[1] = len;
900         addr[2] = (u8 *) &l;
901         elen[2] = sizeof(l);
902         md5_vector(3, addr, elen, msg->hdr->authenticator);
903 }
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 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         if (msg == NULL)
919                 return NULL;
920
921         for (i = 0; i < msg->attr_used; i++) {
922                 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                 if (attr->type != RADIUS_ATTR_VENDOR_SPECIFIC ||
928                     attr->length < sizeof(*attr))
929                         continue;
930
931                 left = attr->length - sizeof(*attr);
932                 if (left < 4)
933                         continue;
934
935                 pos = (u8 *) (attr + 1);
936
937                 os_memcpy(&vendor_id, pos, 4);
938                 pos += 4;
939                 left -= 4;
940
941                 if (ntohl(vendor_id) != vendor)
942                         continue;
943
944                 while (left >= sizeof(*vhdr)) {
945                         vhdr = (struct radius_attr_vendor *) pos;
946                         if (vhdr->vendor_length > left ||
947                             vhdr->vendor_length < sizeof(*vhdr)) {
948                                 break;
949                         }
950                         if (vhdr->vendor_type != subtype) {
951                                 pos += vhdr->vendor_length;
952                                 left -= vhdr->vendor_length;
953                                 continue;
954                         }
955
956                         len = vhdr->vendor_length - sizeof(*vhdr);
957                         data = os_malloc(len);
958                         if (data == NULL)
959                                 return NULL;
960                         os_memcpy(data, pos + sizeof(*vhdr), len);
961                         if (alen)
962                                 *alen = len;
963                         return data;
964                 }
965         }
966
967         return NULL;
968 }
969
970
971 static u8 * decrypt_ms_key(const u8 *key, size_t len,
972                            const u8 *req_authenticator,
973                            const u8 *secret, size_t secret_len, size_t *reslen)
974 {
975         u8 *plain, *ppos, *res;
976         const u8 *pos;
977         size_t left, plen;
978         u8 hash[MD5_MAC_LEN];
979         int i, first = 1;
980         const u8 *addr[3];
981         size_t elen[3];
982
983         /* key: 16-bit salt followed by encrypted key info */
984
985         if (len < 2 + 16)
986                 return NULL;
987
988         pos = key + 2;
989         left = len - 2;
990         if (left % 16) {
991                 wpa_printf(MSG_INFO, "Invalid ms key len %lu",
992                            (unsigned long) left);
993                 return NULL;
994         }
995
996         plen = left;
997         ppos = plain = os_malloc(plen);
998         if (plain == NULL)
999                 return NULL;
1000         plain[0] = 0;
1001
1002         while (left > 0) {
1003                 /* b(1) = MD5(Secret + Request-Authenticator + Salt)
1004                  * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
1005
1006                 addr[0] = secret;
1007                 elen[0] = secret_len;
1008                 if (first) {
1009                         addr[1] = req_authenticator;
1010                         elen[1] = MD5_MAC_LEN;
1011                         addr[2] = key;
1012                         elen[2] = 2; /* Salt */
1013                 } else {
1014                         addr[1] = pos - MD5_MAC_LEN;
1015                         elen[1] = MD5_MAC_LEN;
1016                 }
1017                 md5_vector(first ? 3 : 2, addr, elen, hash);
1018                 first = 0;
1019
1020                 for (i = 0; i < MD5_MAC_LEN; i++)
1021                         *ppos++ = *pos++ ^ hash[i];
1022                 left -= MD5_MAC_LEN;
1023         }
1024
1025         if (plain[0] == 0 || plain[0] > plen - 1) {
1026                 wpa_printf(MSG_INFO, "Failed to decrypt MPPE key");
1027                 os_free(plain);
1028                 return NULL;
1029         }
1030
1031         res = os_malloc(plain[0]);
1032         if (res == NULL) {
1033                 os_free(plain);
1034                 return NULL;
1035         }
1036         os_memcpy(res, plain + 1, plain[0]);
1037         if (reslen)
1038                 *reslen = plain[0];
1039         os_free(plain);
1040         return res;
1041 }
1042
1043
1044 static void encrypt_ms_key(const u8 *key, size_t key_len, u16 salt,
1045                            const u8 *req_authenticator,
1046                            const u8 *secret, size_t secret_len,
1047                            u8 *ebuf, size_t *elen)
1048 {
1049         int i, len, first = 1;
1050         u8 hash[MD5_MAC_LEN], saltbuf[2], *pos;
1051         const u8 *addr[3];
1052         size_t _len[3];
1053
1054         WPA_PUT_BE16(saltbuf, salt);
1055
1056         len = 1 + key_len;
1057         if (len & 0x0f) {
1058                 len = (len & 0xf0) + 16;
1059         }
1060         os_memset(ebuf, 0, len);
1061         ebuf[0] = key_len;
1062         os_memcpy(ebuf + 1, key, key_len);
1063
1064         *elen = len;
1065
1066         pos = ebuf;
1067         while (len > 0) {
1068                 /* b(1) = MD5(Secret + Request-Authenticator + Salt)
1069                  * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
1070                 addr[0] = secret;
1071                 _len[0] = secret_len;
1072                 if (first) {
1073                         addr[1] = req_authenticator;
1074                         _len[1] = MD5_MAC_LEN;
1075                         addr[2] = saltbuf;
1076                         _len[2] = sizeof(saltbuf);
1077                 } else {
1078                         addr[1] = pos - MD5_MAC_LEN;
1079                         _len[1] = MD5_MAC_LEN;
1080                 }
1081                 md5_vector(first ? 3 : 2, addr, _len, hash);
1082                 first = 0;
1083
1084                 for (i = 0; i < MD5_MAC_LEN; i++)
1085                         *pos++ ^= hash[i];
1086
1087                 len -= MD5_MAC_LEN;
1088         }
1089 }
1090
1091
1092 struct radius_ms_mppe_keys *
1093 radius_msg_get_ms_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
1094                        const u8 *secret, size_t secret_len)
1095 {
1096         u8 *key;
1097         size_t keylen;
1098         struct radius_ms_mppe_keys *keys;
1099
1100         if (msg == NULL || sent_msg == NULL)
1101                 return NULL;
1102
1103         keys = os_zalloc(sizeof(*keys));
1104         if (keys == NULL)
1105                 return NULL;
1106
1107         key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
1108                                          RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY,
1109                                          &keylen);
1110         if (key) {
1111                 keys->send = decrypt_ms_key(key, keylen,
1112                                             sent_msg->hdr->authenticator,
1113                                             secret, secret_len,
1114                                             &keys->send_len);
1115                 os_free(key);
1116         }
1117
1118         key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
1119                                          RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY,
1120                                          &keylen);
1121         if (key) {
1122                 keys->recv = decrypt_ms_key(key, keylen,
1123                                             sent_msg->hdr->authenticator,
1124                                             secret, secret_len,
1125                                             &keys->recv_len);
1126                 os_free(key);
1127         }
1128
1129         return keys;
1130 }
1131
1132
1133 struct radius_ms_mppe_keys *
1134 radius_msg_get_cisco_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
1135                           const u8 *secret, size_t secret_len)
1136 {
1137         u8 *key;
1138         size_t keylen;
1139         struct radius_ms_mppe_keys *keys;
1140
1141         if (msg == NULL || sent_msg == NULL)
1142                 return NULL;
1143
1144         keys = os_zalloc(sizeof(*keys));
1145         if (keys == NULL)
1146                 return NULL;
1147
1148         key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_CISCO,
1149                                          RADIUS_CISCO_AV_PAIR, &keylen);
1150         if (key && keylen == 51 &&
1151             os_memcmp(key, "leap:session-key=", 17) == 0) {
1152                 keys->recv = decrypt_ms_key(key + 17, keylen - 17,
1153                                             sent_msg->hdr->authenticator,
1154                                             secret, secret_len,
1155                                             &keys->recv_len);
1156         }
1157         os_free(key);
1158
1159         return keys;
1160 }
1161
1162
1163 int radius_msg_add_mppe_keys(struct radius_msg *msg,
1164                              const u8 *req_authenticator,
1165                              const u8 *secret, size_t secret_len,
1166                              const u8 *send_key, size_t send_key_len,
1167                              const u8 *recv_key, size_t recv_key_len)
1168 {
1169         struct radius_attr_hdr *attr;
1170         u32 vendor_id = htonl(RADIUS_VENDOR_ID_MICROSOFT);
1171         u8 *buf;
1172         struct radius_attr_vendor *vhdr;
1173         u8 *pos;
1174         size_t elen;
1175         int hlen;
1176         u16 salt;
1177
1178         hlen = sizeof(vendor_id) + sizeof(*vhdr) + 2;
1179
1180         /* MS-MPPE-Send-Key */
1181         buf = os_malloc(hlen + send_key_len + 16);
1182         if (buf == NULL) {
1183                 return 0;
1184         }
1185         pos = buf;
1186         os_memcpy(pos, &vendor_id, sizeof(vendor_id));
1187         pos += sizeof(vendor_id);
1188         vhdr = (struct radius_attr_vendor *) pos;
1189         vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY;
1190         pos = (u8 *) (vhdr + 1);
1191         salt = os_random() | 0x8000;
1192         WPA_PUT_BE16(pos, salt);
1193         pos += 2;
1194         encrypt_ms_key(send_key, send_key_len, salt, req_authenticator, secret,
1195                        secret_len, pos, &elen);
1196         vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
1197
1198         attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1199                                    buf, hlen + elen);
1200         os_free(buf);
1201         if (attr == NULL) {
1202                 return 0;
1203         }
1204
1205         /* MS-MPPE-Recv-Key */
1206         buf = os_malloc(hlen + send_key_len + 16);
1207         if (buf == NULL) {
1208                 return 0;
1209         }
1210         pos = buf;
1211         os_memcpy(pos, &vendor_id, sizeof(vendor_id));
1212         pos += sizeof(vendor_id);
1213         vhdr = (struct radius_attr_vendor *) pos;
1214         vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY;
1215         pos = (u8 *) (vhdr + 1);
1216         salt ^= 1;
1217         WPA_PUT_BE16(pos, salt);
1218         pos += 2;
1219         encrypt_ms_key(recv_key, recv_key_len, salt, req_authenticator, secret,
1220                        secret_len, pos, &elen);
1221         vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
1222
1223         attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1224                                    buf, hlen + elen);
1225         os_free(buf);
1226         if (attr == NULL) {
1227                 return 0;
1228         }
1229
1230         return 1;
1231 }
1232
1233
1234 int radius_msg_add_wfa(struct radius_msg *msg, u8 subtype, const u8 *data,
1235                        size_t len)
1236 {
1237         struct radius_attr_hdr *attr;
1238         u8 *buf, *pos;
1239         size_t alen;
1240
1241         alen = 4 + 2 + len;
1242         buf = os_malloc(alen);
1243         if (buf == NULL)
1244                 return 0;
1245         pos = buf;
1246         WPA_PUT_BE32(pos, RADIUS_VENDOR_ID_WFA);
1247         pos += 4;
1248         *pos++ = subtype;
1249         *pos++ = 2 + len;
1250         os_memcpy(pos, data, len);
1251         attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1252                                    buf, alen);
1253         os_free(buf);
1254         if (attr == NULL)
1255                 return 0;
1256
1257         return 1;
1258 }
1259
1260
1261 int radius_user_password_hide(struct radius_msg *msg,
1262                               const u8 *data, size_t data_len,
1263                               const u8 *secret, size_t secret_len,
1264                               u8 *buf, size_t buf_len)
1265 {
1266         size_t padlen, i, pos;
1267         const u8 *addr[2];
1268         size_t len[2];
1269         u8 hash[16];
1270
1271         if (data_len + 16 > buf_len)
1272                 return -1;
1273
1274         os_memcpy(buf, data, data_len);
1275
1276         padlen = data_len % 16;
1277         if (padlen && data_len < buf_len) {
1278                 padlen = 16 - padlen;
1279                 os_memset(buf + data_len, 0, padlen);
1280                 buf_len = data_len + padlen;
1281         } else {
1282                 buf_len = data_len;
1283         }
1284
1285         addr[0] = secret;
1286         len[0] = secret_len;
1287         addr[1] = msg->hdr->authenticator;
1288         len[1] = 16;
1289         md5_vector(2, addr, len, hash);
1290
1291         for (i = 0; i < 16; i++)
1292                 buf[i] ^= hash[i];
1293         pos = 16;
1294
1295         while (pos < buf_len) {
1296                 addr[0] = secret;
1297                 len[0] = secret_len;
1298                 addr[1] = &buf[pos - 16];
1299                 len[1] = 16;
1300                 md5_vector(2, addr, len, hash);
1301
1302                 for (i = 0; i < 16; i++)
1303                         buf[pos + i] ^= hash[i];
1304
1305                 pos += 16;
1306         }
1307
1308         return buf_len;
1309 }
1310
1311
1312 /* Add User-Password attribute to a RADIUS message and encrypt it as specified
1313  * in RFC 2865, Chap. 5.2 */
1314 struct radius_attr_hdr *
1315 radius_msg_add_attr_user_password(struct radius_msg *msg,
1316                                   const u8 *data, size_t data_len,
1317                                   const u8 *secret, size_t secret_len)
1318 {
1319         u8 buf[128];
1320         int res;
1321
1322         res = radius_user_password_hide(msg, data, data_len,
1323                                         secret, secret_len, buf, sizeof(buf));
1324         if (res < 0)
1325                 return NULL;
1326
1327         return radius_msg_add_attr(msg, RADIUS_ATTR_USER_PASSWORD,
1328                                    buf, res);
1329 }
1330
1331
1332 int radius_msg_get_attr(struct radius_msg *msg, u8 type, u8 *buf, size_t len)
1333 {
1334         struct radius_attr_hdr *attr = NULL, *tmp;
1335         size_t i, dlen;
1336
1337         for (i = 0; i < msg->attr_used; i++) {
1338                 tmp = radius_get_attr_hdr(msg, i);
1339                 if (tmp->type == type) {
1340                         attr = tmp;
1341                         break;
1342                 }
1343         }
1344
1345         if (!attr || attr->length < sizeof(*attr))
1346                 return -1;
1347
1348         dlen = attr->length - sizeof(*attr);
1349         if (buf)
1350                 os_memcpy(buf, (attr + 1), dlen > len ? len : dlen);
1351         return dlen;
1352 }
1353
1354
1355 int radius_msg_get_attr_ptr(struct radius_msg *msg, u8 type, u8 **buf,
1356                             size_t *len, const u8 *start)
1357 {
1358         size_t i;
1359         struct radius_attr_hdr *attr = NULL, *tmp;
1360
1361         for (i = 0; i < msg->attr_used; i++) {
1362                 tmp = radius_get_attr_hdr(msg, i);
1363                 if (tmp->type == type &&
1364                     (start == NULL || (u8 *) tmp > start)) {
1365                         attr = tmp;
1366                         break;
1367                 }
1368         }
1369
1370         if (!attr || attr->length < sizeof(*attr))
1371                 return -1;
1372
1373         *buf = (u8 *) (attr + 1);
1374         *len = attr->length - sizeof(*attr);
1375         return 0;
1376 }
1377
1378
1379 int radius_msg_count_attr(struct radius_msg *msg, u8 type, int min_len)
1380 {
1381         size_t i;
1382         int count;
1383
1384         for (count = 0, i = 0; i < msg->attr_used; i++) {
1385                 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
1386                 if (attr->type == type &&
1387                     attr->length >= sizeof(struct radius_attr_hdr) + min_len)
1388                         count++;
1389         }
1390
1391         return count;
1392 }
1393
1394
1395 struct radius_tunnel_attrs {
1396         int tag_used;
1397         int type; /* Tunnel-Type */
1398         int medium_type; /* Tunnel-Medium-Type */
1399         int vlanid;
1400 };
1401
1402
1403 /**
1404  * radius_msg_get_vlanid - Parse RADIUS attributes for VLAN tunnel information
1405  * @msg: RADIUS message
1406  * Returns: VLAN ID for the first tunnel configuration of -1 if none is found
1407  */
1408 int radius_msg_get_vlanid(struct radius_msg *msg)
1409 {
1410         struct radius_tunnel_attrs tunnel[RADIUS_TUNNEL_TAGS], *tun;
1411         size_t i;
1412         struct radius_attr_hdr *attr = NULL;
1413         const u8 *data;
1414         char buf[10];
1415         size_t dlen;
1416
1417         os_memset(&tunnel, 0, sizeof(tunnel));
1418
1419         for (i = 0; i < msg->attr_used; i++) {
1420                 attr = radius_get_attr_hdr(msg, i);
1421                 if (attr->length < sizeof(*attr))
1422                         return -1;
1423                 data = (const u8 *) (attr + 1);
1424                 dlen = attr->length - sizeof(*attr);
1425                 if (attr->length < 3)
1426                         continue;
1427                 if (data[0] >= RADIUS_TUNNEL_TAGS)
1428                         tun = &tunnel[0];
1429                 else
1430                         tun = &tunnel[data[0]];
1431
1432                 switch (attr->type) {
1433                 case RADIUS_ATTR_TUNNEL_TYPE:
1434                         if (attr->length != 6)
1435                                 break;
1436                         tun->tag_used++;
1437                         tun->type = WPA_GET_BE24(data + 1);
1438                         break;
1439                 case RADIUS_ATTR_TUNNEL_MEDIUM_TYPE:
1440                         if (attr->length != 6)
1441                                 break;
1442                         tun->tag_used++;
1443                         tun->medium_type = WPA_GET_BE24(data + 1);
1444                         break;
1445                 case RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID:
1446                         if (data[0] < RADIUS_TUNNEL_TAGS) {
1447                                 data++;
1448                                 dlen--;
1449                         }
1450                         if (dlen >= sizeof(buf))
1451                                 break;
1452                         os_memcpy(buf, data, dlen);
1453                         buf[dlen] = '\0';
1454                         tun->tag_used++;
1455                         tun->vlanid = atoi(buf);
1456                         break;
1457                 }
1458         }
1459
1460         for (i = 0; i < RADIUS_TUNNEL_TAGS; i++) {
1461                 tun = &tunnel[i];
1462                 if (tun->tag_used &&
1463                     tun->type == RADIUS_TUNNEL_TYPE_VLAN &&
1464                     tun->medium_type == RADIUS_TUNNEL_MEDIUM_TYPE_802 &&
1465                     tun->vlanid > 0)
1466                         return tun->vlanid;
1467         }
1468
1469         return -1;
1470 }
1471
1472
1473 /**
1474  * radius_msg_get_tunnel_password - Parse RADIUS attribute Tunnel-Password
1475  * @msg: Received RADIUS message
1476  * @keylen: Length of returned password
1477  * @secret: RADIUS shared secret
1478  * @secret_len: Length of secret
1479  * @sent_msg: Sent RADIUS message
1480  * @n: Number of password attribute to return (starting with 0)
1481  * Returns: Pointer to n-th password (free with os_free) or %NULL
1482  */
1483 char * radius_msg_get_tunnel_password(struct radius_msg *msg, int *keylen,
1484                                       const u8 *secret, size_t secret_len,
1485                                       struct radius_msg *sent_msg, size_t n)
1486 {
1487         u8 *buf = NULL;
1488         size_t buflen;
1489         const u8 *salt;
1490         u8 *str;
1491         const u8 *addr[3];
1492         size_t len[3];
1493         u8 hash[16];
1494         u8 *pos;
1495         size_t i, j = 0;
1496         struct radius_attr_hdr *attr;
1497         const u8 *data;
1498         size_t dlen;
1499         const u8 *fdata = NULL; /* points to found item */
1500         size_t fdlen = -1;
1501         char *ret = NULL;
1502
1503         /* find n-th valid Tunnel-Password attribute */
1504         for (i = 0; i < msg->attr_used; i++) {
1505                 attr = radius_get_attr_hdr(msg, i);
1506                 if (attr == NULL ||
1507                     attr->type != RADIUS_ATTR_TUNNEL_PASSWORD) {
1508                         continue;
1509                 }
1510                 if (attr->length <= 5)
1511                         continue;
1512                 data = (const u8 *) (attr + 1);
1513                 dlen = attr->length - sizeof(*attr);
1514                 if (dlen <= 3 || dlen % 16 != 3)
1515                         continue;
1516                 j++;
1517                 if (j <= n)
1518                         continue;
1519
1520                 fdata = data;
1521                 fdlen = dlen;
1522                 break;
1523         }
1524         if (fdata == NULL)
1525                 goto out;
1526
1527         /* alloc writable memory for decryption */
1528         buf = os_malloc(fdlen);
1529         if (buf == NULL)
1530                 goto out;
1531         os_memcpy(buf, fdata, fdlen);
1532         buflen = fdlen;
1533
1534         /* init pointers */
1535         salt = buf + 1;
1536         str = buf + 3;
1537
1538         /* decrypt blocks */
1539         pos = buf + buflen - 16; /* last block */
1540         while (pos >= str + 16) { /* all but the first block */
1541                 addr[0] = secret;
1542                 len[0] = secret_len;
1543                 addr[1] = pos - 16;
1544                 len[1] = 16;
1545                 md5_vector(2, addr, len, hash);
1546
1547                 for (i = 0; i < 16; i++)
1548                         pos[i] ^= hash[i];
1549
1550                 pos -= 16;
1551         }
1552
1553         /* decrypt first block */
1554         if (str != pos)
1555                 goto out;
1556         addr[0] = secret;
1557         len[0] = secret_len;
1558         addr[1] = sent_msg->hdr->authenticator;
1559         len[1] = 16;
1560         addr[2] = salt;
1561         len[2] = 2;
1562         md5_vector(3, addr, len, hash);
1563
1564         for (i = 0; i < 16; i++)
1565                 pos[i] ^= hash[i];
1566
1567         /* derive plaintext length from first subfield */
1568         *keylen = (unsigned char) str[0];
1569         if ((u8 *) (str + *keylen) >= (u8 *) (buf + buflen)) {
1570                 /* decryption error - invalid key length */
1571                 goto out;
1572         }
1573         if (*keylen == 0) {
1574                 /* empty password */
1575                 goto out;
1576         }
1577
1578         /* copy passphrase into new buffer */
1579         ret = os_malloc(*keylen);
1580         if (ret)
1581                 os_memcpy(ret, str + 1, *keylen);
1582
1583 out:
1584         /* return new buffer */
1585         os_free(buf);
1586         return ret;
1587 }
1588
1589
1590 void radius_free_class(struct radius_class_data *c)
1591 {
1592         size_t i;
1593         if (c == NULL)
1594                 return;
1595         for (i = 0; i < c->count; i++)
1596                 os_free(c->attr[i].data);
1597         os_free(c->attr);
1598         c->attr = NULL;
1599         c->count = 0;
1600 }
1601
1602
1603 int radius_copy_class(struct radius_class_data *dst,
1604                       const struct radius_class_data *src)
1605 {
1606         size_t i;
1607
1608         if (src->attr == NULL)
1609                 return 0;
1610
1611         dst->attr = os_calloc(src->count, sizeof(struct radius_attr_data));
1612         if (dst->attr == NULL)
1613                 return -1;
1614
1615         dst->count = 0;
1616
1617         for (i = 0; i < src->count; i++) {
1618                 dst->attr[i].data = os_malloc(src->attr[i].len);
1619                 if (dst->attr[i].data == NULL)
1620                         break;
1621                 dst->count++;
1622                 os_memcpy(dst->attr[i].data, src->attr[i].data,
1623                           src->attr[i].len);
1624                 dst->attr[i].len = src->attr[i].len;
1625         }
1626
1627         return 0;
1628 }
1629
1630
1631 u8 radius_msg_find_unlisted_attr(struct radius_msg *msg, u8 *attrs)
1632 {
1633         size_t i, j;
1634         struct radius_attr_hdr *attr;
1635
1636         for (i = 0; i < msg->attr_used; i++) {
1637                 attr = radius_get_attr_hdr(msg, i);
1638
1639                 for (j = 0; attrs[j]; j++) {
1640                         if (attr->type == attrs[j])
1641                                 break;
1642                 }
1643
1644                 if (attrs[j] == 0)
1645                         return attr->type; /* unlisted attr */
1646         }
1647
1648         return 0;
1649 }