Re-initialize hostapd/wpa_supplicant git repository based on 0.6.3 release
[libeap.git] / src / radius / radius.c
1 /*
2  * hostapd / RADIUS message processing
3  * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "radius.h"
19 #include "md5.h"
20 #include "crypto.h"
21
22
23 static struct radius_attr_hdr *
24 radius_get_attr_hdr(struct radius_msg *msg, int idx)
25 {
26         return (struct radius_attr_hdr *) (msg->buf + msg->attr_pos[idx]);
27 }
28
29
30 struct radius_msg *radius_msg_new(u8 code, u8 identifier)
31 {
32         struct radius_msg *msg;
33
34         msg = os_malloc(sizeof(*msg));
35         if (msg == NULL)
36                 return NULL;
37
38         if (radius_msg_initialize(msg, RADIUS_DEFAULT_MSG_SIZE)) {
39                 os_free(msg);
40                 return NULL;
41         }
42
43         radius_msg_set_hdr(msg, code, identifier);
44
45         return msg;
46 }
47
48
49 int radius_msg_initialize(struct radius_msg *msg, size_t init_len)
50 {
51         if (msg == NULL || init_len < sizeof(struct radius_hdr))
52                 return -1;
53
54         os_memset(msg, 0, sizeof(*msg));
55         msg->buf = os_zalloc(init_len);
56         if (msg->buf == NULL)
57                 return -1;
58
59         msg->buf_size = init_len;
60         msg->hdr = (struct radius_hdr *) msg->buf;
61         msg->buf_used = sizeof(*msg->hdr);
62
63         msg->attr_pos =
64                 os_zalloc(RADIUS_DEFAULT_ATTR_COUNT * sizeof(*msg->attr_pos));
65         if (msg->attr_pos == NULL) {
66                 os_free(msg->buf);
67                 msg->buf = NULL;
68                 msg->hdr = NULL;
69                 return -1;
70         }
71
72         msg->attr_size = RADIUS_DEFAULT_ATTR_COUNT;
73         msg->attr_used = 0;
74
75         return 0;
76 }
77
78
79 void radius_msg_set_hdr(struct radius_msg *msg, u8 code, u8 identifier)
80 {
81         msg->hdr->code = code;
82         msg->hdr->identifier = identifier;
83 }
84
85
86 void radius_msg_free(struct radius_msg *msg)
87 {
88         os_free(msg->buf);
89         msg->buf = NULL;
90         msg->hdr = NULL;
91         msg->buf_size = msg->buf_used = 0;
92
93         os_free(msg->attr_pos);
94         msg->attr_pos = NULL;
95         msg->attr_size = msg->attr_used = 0;
96 }
97
98
99 static const char *radius_code_string(u8 code)
100 {
101         switch (code) {
102         case RADIUS_CODE_ACCESS_REQUEST: return "Access-Request";
103         case RADIUS_CODE_ACCESS_ACCEPT: return "Access-Accept";
104         case RADIUS_CODE_ACCESS_REJECT: return "Access-Reject";
105         case RADIUS_CODE_ACCOUNTING_REQUEST: return "Accounting-Request";
106         case RADIUS_CODE_ACCOUNTING_RESPONSE: return "Accounting-Response";
107         case RADIUS_CODE_ACCESS_CHALLENGE: return "Access-Challenge";
108         case RADIUS_CODE_STATUS_SERVER: return "Status-Server";
109         case RADIUS_CODE_STATUS_CLIENT: return "Status-Client";
110         case RADIUS_CODE_RESERVED: return "Reserved";
111         default: return "?Unknown?";
112         }
113 }
114
115
116 struct radius_attr_type {
117         u8 type;
118         char *name;
119         enum {
120                 RADIUS_ATTR_UNDIST, RADIUS_ATTR_TEXT, RADIUS_ATTR_IP,
121                 RADIUS_ATTR_HEXDUMP, RADIUS_ATTR_INT32, RADIUS_ATTR_IPV6
122         } data_type;
123 };
124
125 static struct radius_attr_type radius_attrs[] =
126 {
127         { RADIUS_ATTR_USER_NAME, "User-Name", RADIUS_ATTR_TEXT },
128         { RADIUS_ATTR_USER_PASSWORD, "User-Password", RADIUS_ATTR_UNDIST },
129         { RADIUS_ATTR_NAS_IP_ADDRESS, "NAS-IP-Address", RADIUS_ATTR_IP },
130         { RADIUS_ATTR_NAS_PORT, "NAS-Port", RADIUS_ATTR_INT32 },
131         { RADIUS_ATTR_FRAMED_MTU, "Framed-MTU", RADIUS_ATTR_INT32 },
132         { RADIUS_ATTR_REPLY_MESSAGE, "Reply-Message", RADIUS_ATTR_TEXT },
133         { RADIUS_ATTR_STATE, "State", RADIUS_ATTR_UNDIST },
134         { RADIUS_ATTR_CLASS, "Class", RADIUS_ATTR_UNDIST },
135         { RADIUS_ATTR_VENDOR_SPECIFIC, "Vendor-Specific", RADIUS_ATTR_UNDIST },
136         { RADIUS_ATTR_SESSION_TIMEOUT, "Session-Timeout", RADIUS_ATTR_INT32 },
137         { RADIUS_ATTR_IDLE_TIMEOUT, "Idle-Timeout", RADIUS_ATTR_INT32 },
138         { RADIUS_ATTR_TERMINATION_ACTION, "Termination-Action",
139           RADIUS_ATTR_INT32 },
140         { RADIUS_ATTR_CALLED_STATION_ID, "Called-Station-Id",
141           RADIUS_ATTR_TEXT },
142         { RADIUS_ATTR_CALLING_STATION_ID, "Calling-Station-Id",
143           RADIUS_ATTR_TEXT },
144         { RADIUS_ATTR_NAS_IDENTIFIER, "NAS-Identifier", RADIUS_ATTR_TEXT },
145         { RADIUS_ATTR_PROXY_STATE, "Proxy-State", RADIUS_ATTR_UNDIST },
146         { RADIUS_ATTR_ACCT_STATUS_TYPE, "Acct-Status-Type",
147           RADIUS_ATTR_INT32 },
148         { RADIUS_ATTR_ACCT_DELAY_TIME, "Acct-Delay-Time", RADIUS_ATTR_INT32 },
149         { RADIUS_ATTR_ACCT_INPUT_OCTETS, "Acct-Input-Octets",
150           RADIUS_ATTR_INT32 },
151         { RADIUS_ATTR_ACCT_OUTPUT_OCTETS, "Acct-Output-Octets",
152           RADIUS_ATTR_INT32 },
153         { RADIUS_ATTR_ACCT_SESSION_ID, "Acct-Session-Id", RADIUS_ATTR_TEXT },
154         { RADIUS_ATTR_ACCT_AUTHENTIC, "Acct-Authentic", RADIUS_ATTR_INT32 },
155         { RADIUS_ATTR_ACCT_SESSION_TIME, "Acct-Session-Time",
156           RADIUS_ATTR_INT32 },
157         { RADIUS_ATTR_ACCT_INPUT_PACKETS, "Acct-Input-Packets",
158           RADIUS_ATTR_INT32 },
159         { RADIUS_ATTR_ACCT_OUTPUT_PACKETS, "Acct-Output-Packets",
160           RADIUS_ATTR_INT32 },
161         { RADIUS_ATTR_ACCT_TERMINATE_CAUSE, "Acct-Terminate-Cause",
162           RADIUS_ATTR_INT32 },
163         { RADIUS_ATTR_ACCT_MULTI_SESSION_ID, "Acct-Multi-Session-Id",
164           RADIUS_ATTR_TEXT },
165         { RADIUS_ATTR_ACCT_LINK_COUNT, "Acct-Link-Count", RADIUS_ATTR_INT32 },
166         { RADIUS_ATTR_ACCT_INPUT_GIGAWORDS, "Acct-Input-Gigawords", 
167           RADIUS_ATTR_INT32 },
168         { RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS, "Acct-Output-Gigawords",
169           RADIUS_ATTR_INT32 },
170         { RADIUS_ATTR_EVENT_TIMESTAMP, "Event-Timestamp",
171           RADIUS_ATTR_INT32 },
172         { RADIUS_ATTR_NAS_PORT_TYPE, "NAS-Port-Type", RADIUS_ATTR_INT32 },
173         { RADIUS_ATTR_TUNNEL_TYPE, "Tunnel-Type", RADIUS_ATTR_HEXDUMP },
174         { RADIUS_ATTR_TUNNEL_MEDIUM_TYPE, "Tunnel-Medium-Type",
175           RADIUS_ATTR_HEXDUMP },
176         { RADIUS_ATTR_CONNECT_INFO, "Connect-Info", RADIUS_ATTR_TEXT },
177         { RADIUS_ATTR_EAP_MESSAGE, "EAP-Message", RADIUS_ATTR_UNDIST },
178         { RADIUS_ATTR_MESSAGE_AUTHENTICATOR, "Message-Authenticator",
179           RADIUS_ATTR_UNDIST },
180         { RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID, "Tunnel-Private-Group-Id",
181           RADIUS_ATTR_HEXDUMP },
182         { RADIUS_ATTR_ACCT_INTERIM_INTERVAL, "Acct-Interim-Interval",
183           RADIUS_ATTR_INT32 },
184         { RADIUS_ATTR_NAS_IPV6_ADDRESS, "NAS-IPv6-Address", RADIUS_ATTR_IPV6 },
185 };
186 #define RADIUS_ATTRS (sizeof(radius_attrs) / sizeof(radius_attrs[0]))
187
188
189 static struct radius_attr_type *radius_get_attr_type(u8 type)
190 {
191         size_t i;
192
193         for (i = 0; i < RADIUS_ATTRS; i++) {
194                 if (type == radius_attrs[i].type)
195                         return &radius_attrs[i];
196         }
197
198         return NULL;
199 }
200
201
202 static void print_char(char c)
203 {
204         if (c >= 32 && c < 127)
205                 printf("%c", c);
206         else
207                 printf("<%02x>", c);
208 }
209
210
211 static void radius_msg_dump_attr(struct radius_attr_hdr *hdr)
212 {
213         struct radius_attr_type *attr;
214         int i, len;
215         unsigned char *pos;
216
217         attr = radius_get_attr_type(hdr->type);
218
219         printf("   Attribute %d (%s) length=%d\n",
220                hdr->type, attr ? attr->name : "?Unknown?", hdr->length);
221
222         if (attr == NULL)
223                 return;
224
225         len = hdr->length - sizeof(struct radius_attr_hdr);
226         pos = (unsigned char *) (hdr + 1);
227
228         switch (attr->data_type) {
229         case RADIUS_ATTR_TEXT:
230                 printf("      Value: '");
231                 for (i = 0; i < len; i++)
232                         print_char(pos[i]);
233                 printf("'\n");
234                 break;
235
236         case RADIUS_ATTR_IP:
237                 if (len == 4) {
238                         struct in_addr addr;
239                         os_memcpy(&addr, pos, 4);
240                         printf("      Value: %s\n", inet_ntoa(addr));
241                 } else
242                         printf("      Invalid IP address length %d\n", len);
243                 break;
244
245 #ifdef CONFIG_IPV6
246         case RADIUS_ATTR_IPV6:
247                 if (len == 16) {
248                         char buf[128];
249                         const char *atxt;
250                         struct in6_addr *addr = (struct in6_addr *) pos;
251                         atxt = inet_ntop(AF_INET6, addr, buf, sizeof(buf));
252                         printf("      Value: %s\n", atxt ? atxt : "?");
253                 } else
254                         printf("      Invalid IPv6 address length %d\n", len);
255                 break;
256 #endif /* CONFIG_IPV6 */
257
258         case RADIUS_ATTR_HEXDUMP:
259         case RADIUS_ATTR_UNDIST:
260                 printf("      Value:");
261                 for (i = 0; i < len; i++)
262                         printf(" %02x", pos[i]);
263                 printf("\n");
264                 break;
265
266         case RADIUS_ATTR_INT32:
267                 if (len == 4)
268                         printf("      Value: %u\n", WPA_GET_BE32(pos));
269                 else
270                         printf("      Invalid INT32 length %d\n", len);
271                 break;
272
273         default:
274                 break;
275         }
276 }
277
278
279 void radius_msg_dump(struct radius_msg *msg)
280 {
281         size_t i;
282
283         printf("RADIUS message: code=%d (%s) identifier=%d length=%d\n",
284                msg->hdr->code, radius_code_string(msg->hdr->code),
285                msg->hdr->identifier, ntohs(msg->hdr->length));
286
287         for (i = 0; i < msg->attr_used; i++) {
288                 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
289                 radius_msg_dump_attr(attr);
290         }
291 }
292
293
294 int radius_msg_finish(struct radius_msg *msg, u8 *secret, size_t secret_len)
295 {
296         if (secret) {
297                 u8 auth[MD5_MAC_LEN];
298                 struct radius_attr_hdr *attr;
299
300                 os_memset(auth, 0, MD5_MAC_LEN);
301                 attr = radius_msg_add_attr(msg,
302                                            RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
303                                            auth, MD5_MAC_LEN);
304                 if (attr == NULL) {
305                         printf("WARNING: Could not add "
306                                "Message-Authenticator\n");
307                         return -1;
308                 }
309                 msg->hdr->length = htons(msg->buf_used);
310                 hmac_md5(secret, secret_len, msg->buf, msg->buf_used,
311                          (u8 *) (attr + 1));
312         } else
313                 msg->hdr->length = htons(msg->buf_used);
314
315         if (msg->buf_used > 0xffff) {
316                 printf("WARNING: too long RADIUS message (%lu)\n",
317                        (unsigned long) msg->buf_used);
318                 return -1;
319         }
320         return 0;
321 }
322
323
324 int radius_msg_finish_srv(struct radius_msg *msg, const u8 *secret,
325                           size_t secret_len, const u8 *req_authenticator)
326 {
327         u8 auth[MD5_MAC_LEN];
328         struct radius_attr_hdr *attr;
329         const u8 *addr[4];
330         size_t len[4];
331
332         os_memset(auth, 0, MD5_MAC_LEN);
333         attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
334                                    auth, MD5_MAC_LEN);
335         if (attr == NULL) {
336                 printf("WARNING: Could not add Message-Authenticator\n");
337                 return -1;
338         }
339         msg->hdr->length = htons(msg->buf_used);
340         os_memcpy(msg->hdr->authenticator, req_authenticator,
341                   sizeof(msg->hdr->authenticator));
342         hmac_md5(secret, secret_len, msg->buf, msg->buf_used,
343                  (u8 *) (attr + 1));
344
345         /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
346         addr[0] = (u8 *) msg->hdr;
347         len[0] = 1 + 1 + 2;
348         addr[1] = req_authenticator;
349         len[1] = MD5_MAC_LEN;
350         addr[2] = (u8 *) (msg->hdr + 1);
351         len[2] = msg->buf_used - sizeof(*msg->hdr);
352         addr[3] = secret;
353         len[3] = secret_len;
354         md5_vector(4, addr, len, msg->hdr->authenticator);
355
356         if (msg->buf_used > 0xffff) {
357                 printf("WARNING: too long RADIUS message (%lu)\n",
358                        (unsigned long) msg->buf_used);
359                 return -1;
360         }
361         return 0;
362 }
363
364
365 void radius_msg_finish_acct(struct radius_msg *msg, u8 *secret,
366                             size_t secret_len)
367 {
368         const u8 *addr[2];
369         size_t len[2];
370
371         msg->hdr->length = htons(msg->buf_used);
372         os_memset(msg->hdr->authenticator, 0, MD5_MAC_LEN);
373         addr[0] = msg->buf;
374         len[0] = msg->buf_used;
375         addr[1] = secret;
376         len[1] = secret_len;
377         md5_vector(2, addr, len, msg->hdr->authenticator);
378
379         if (msg->buf_used > 0xffff) {
380                 printf("WARNING: too long RADIUS messages (%lu)\n",
381                        (unsigned long) msg->buf_used);
382         }
383 }
384
385
386 static int radius_msg_add_attr_to_array(struct radius_msg *msg,
387                                         struct radius_attr_hdr *attr)
388 {
389         if (msg->attr_used >= msg->attr_size) {
390                 size_t *nattr_pos;
391                 int nlen = msg->attr_size * 2;
392
393                 nattr_pos = os_realloc(msg->attr_pos,
394                                        nlen * sizeof(*msg->attr_pos));
395                 if (nattr_pos == NULL)
396                         return -1;
397
398                 msg->attr_pos = nattr_pos;
399                 msg->attr_size = nlen;
400         }
401
402         msg->attr_pos[msg->attr_used++] = (unsigned char *) attr - msg->buf;
403
404         return 0;
405 }
406
407
408 struct radius_attr_hdr *radius_msg_add_attr(struct radius_msg *msg, u8 type,
409                                             const u8 *data, size_t data_len)
410 {
411         size_t buf_needed;
412         struct radius_attr_hdr *attr;
413
414         if (data_len > RADIUS_MAX_ATTR_LEN) {
415                 printf("radius_msg_add_attr: too long attribute (%lu bytes)\n",
416                        (unsigned long) data_len);
417                 return NULL;
418         }
419
420         buf_needed = msg->buf_used + sizeof(*attr) + data_len;
421
422         if (msg->buf_size < buf_needed) {
423                 /* allocate more space for message buffer */
424                 unsigned char *nbuf;
425                 size_t nlen = msg->buf_size;
426
427                 while (nlen < buf_needed)
428                         nlen *= 2;
429                 nbuf = os_realloc(msg->buf, nlen);
430                 if (nbuf == NULL)
431                         return NULL;
432                 msg->buf = nbuf;
433                 msg->hdr = (struct radius_hdr *) msg->buf;
434                 os_memset(msg->buf + msg->buf_size, 0, nlen - msg->buf_size);
435                 msg->buf_size = nlen;
436         }
437
438         attr = (struct radius_attr_hdr *) (msg->buf + msg->buf_used);
439         attr->type = type;
440         attr->length = sizeof(*attr) + data_len;
441         if (data_len > 0)
442                 os_memcpy(attr + 1, data, data_len);
443
444         msg->buf_used += sizeof(*attr) + data_len;
445
446         if (radius_msg_add_attr_to_array(msg, attr))
447                 return NULL;
448
449         return attr;
450 }
451
452
453 struct radius_msg *radius_msg_parse(const u8 *data, size_t len)
454 {
455         struct radius_msg *msg;
456         struct radius_hdr *hdr;
457         struct radius_attr_hdr *attr;
458         size_t msg_len;
459         unsigned char *pos, *end;
460
461         if (data == NULL || len < sizeof(*hdr))
462                 return NULL;
463
464         hdr = (struct radius_hdr *) data;
465
466         msg_len = ntohs(hdr->length);
467         if (msg_len < sizeof(*hdr) || msg_len > len) {
468                 printf("Invalid RADIUS message length\n");
469                 return NULL;
470         }
471
472         if (msg_len < len) {
473                 printf("Ignored %lu extra bytes after RADIUS message\n",
474                        (unsigned long) len - msg_len);
475         }
476
477         msg = os_malloc(sizeof(*msg));
478         if (msg == NULL)
479                 return NULL;
480
481         if (radius_msg_initialize(msg, msg_len)) {
482                 os_free(msg);
483                 return NULL;
484         }
485
486         os_memcpy(msg->buf, data, msg_len);
487         msg->buf_size = msg->buf_used = msg_len;
488
489         /* parse attributes */
490         pos = (unsigned char *) (msg->hdr + 1);
491         end = msg->buf + msg->buf_used;
492         while (pos < end) {
493                 if ((size_t) (end - pos) < sizeof(*attr))
494                         goto fail;
495
496                 attr = (struct radius_attr_hdr *) pos;
497
498                 if (pos + attr->length > end || attr->length < sizeof(*attr))
499                         goto fail;
500
501                 /* TODO: check that attr->length is suitable for attr->type */
502
503                 if (radius_msg_add_attr_to_array(msg, attr))
504                         goto fail;
505
506                 pos += attr->length;
507         }
508
509         return msg;
510
511  fail:
512         radius_msg_free(msg);
513         os_free(msg);
514         return NULL;
515 }
516
517
518 int radius_msg_add_eap(struct radius_msg *msg, const u8 *data, size_t data_len)
519 {
520         const u8 *pos = data;
521         size_t left = data_len;
522
523         while (left > 0) {
524                 int len;
525                 if (left > RADIUS_MAX_ATTR_LEN)
526                         len = RADIUS_MAX_ATTR_LEN;
527                 else
528                         len = left;
529
530                 if (!radius_msg_add_attr(msg, RADIUS_ATTR_EAP_MESSAGE,
531                                          pos, len))
532                         return 0;
533
534                 pos += len;
535                 left -= len;
536         }
537
538         return 1;
539 }
540
541
542 u8 *radius_msg_get_eap(struct radius_msg *msg, size_t *eap_len)
543 {
544         u8 *eap, *pos;
545         size_t len, i;
546         struct radius_attr_hdr *attr;
547
548         if (msg == NULL)
549                 return NULL;
550
551         len = 0;
552         for (i = 0; i < msg->attr_used; i++) {
553                 attr = radius_get_attr_hdr(msg, i);
554                 if (attr->type == RADIUS_ATTR_EAP_MESSAGE)
555                         len += attr->length - sizeof(struct radius_attr_hdr);
556         }
557
558         if (len == 0)
559                 return NULL;
560
561         eap = os_malloc(len);
562         if (eap == NULL)
563                 return NULL;
564
565         pos = eap;
566         for (i = 0; i < msg->attr_used; i++) {
567                 attr = radius_get_attr_hdr(msg, i);
568                 if (attr->type == RADIUS_ATTR_EAP_MESSAGE) {
569                         int flen = attr->length - sizeof(*attr);
570                         os_memcpy(pos, attr + 1, flen);
571                         pos += flen;
572                 }
573         }
574
575         if (eap_len)
576                 *eap_len = len;
577
578         return eap;
579 }
580
581
582 int radius_msg_verify_msg_auth(struct radius_msg *msg, const u8 *secret,
583                                size_t secret_len, const u8 *req_auth)
584 {
585         u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
586         u8 orig_authenticator[16];
587         struct radius_attr_hdr *attr = NULL, *tmp;
588         size_t i;
589
590         for (i = 0; i < msg->attr_used; i++) {
591                 tmp = radius_get_attr_hdr(msg, i);
592                 if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
593                         if (attr != NULL) {
594                                 printf("Multiple Message-Authenticator "
595                                        "attributes in RADIUS message\n");
596                                 return 1;
597                         }
598                         attr = tmp;
599                 }
600         }
601
602         if (attr == NULL) {
603                 printf("No Message-Authenticator attribute found\n");
604                 return 1;
605         }
606
607         os_memcpy(orig, attr + 1, MD5_MAC_LEN);
608         os_memset(attr + 1, 0, MD5_MAC_LEN);
609         if (req_auth) {
610                 os_memcpy(orig_authenticator, msg->hdr->authenticator,
611                           sizeof(orig_authenticator));
612                 os_memcpy(msg->hdr->authenticator, req_auth,
613                           sizeof(msg->hdr->authenticator));
614         }
615         hmac_md5(secret, secret_len, msg->buf, msg->buf_used, auth);
616         os_memcpy(attr + 1, orig, MD5_MAC_LEN);
617         if (req_auth) {
618                 os_memcpy(msg->hdr->authenticator, orig_authenticator,
619                           sizeof(orig_authenticator));
620         }
621
622         if (os_memcmp(orig, auth, MD5_MAC_LEN) != 0) {
623                 printf("Invalid Message-Authenticator!\n");
624                 return 1;
625         }
626
627         return 0;
628 }
629
630
631 int radius_msg_verify(struct radius_msg *msg, const u8 *secret,
632                       size_t secret_len, struct radius_msg *sent_msg, int auth)
633 {
634         const u8 *addr[4];
635         size_t len[4];
636         u8 hash[MD5_MAC_LEN];
637
638         if (sent_msg == NULL) {
639                 printf("No matching Access-Request message found\n");
640                 return 1;
641         }
642
643         if (auth &&
644             radius_msg_verify_msg_auth(msg, secret, secret_len,
645                                        sent_msg->hdr->authenticator)) {
646                 return 1;
647         }
648
649         /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
650         addr[0] = (u8 *) msg->hdr;
651         len[0] = 1 + 1 + 2;
652         addr[1] = sent_msg->hdr->authenticator;
653         len[1] = MD5_MAC_LEN;
654         addr[2] = (u8 *) (msg->hdr + 1);
655         len[2] = msg->buf_used - sizeof(*msg->hdr);
656         addr[3] = secret;
657         len[3] = secret_len;
658         md5_vector(4, addr, len, hash);
659         if (os_memcmp(hash, msg->hdr->authenticator, MD5_MAC_LEN) != 0) {
660                 printf("Response Authenticator invalid!\n");
661                 return 1;
662         }
663
664         return 0;
665 }
666
667
668 int radius_msg_copy_attr(struct radius_msg *dst, struct radius_msg *src,
669                          u8 type)
670 {
671         struct radius_attr_hdr *attr;
672         size_t i;
673         int count = 0;
674
675         for (i = 0; i < src->attr_used; i++) {
676                 attr = radius_get_attr_hdr(src, i);
677                 if (attr->type == type) {
678                         if (!radius_msg_add_attr(dst, type, (u8 *) (attr + 1),
679                                                  attr->length - sizeof(*attr)))
680                                 return -1;
681                         count++;
682                 }
683         }
684
685         return count;
686 }
687
688
689 /* Create Request Authenticator. The value should be unique over the lifetime
690  * of the shared secret between authenticator and authentication server.
691  * Use one-way MD5 hash calculated from current timestamp and some data given
692  * by the caller. */
693 void radius_msg_make_authenticator(struct radius_msg *msg,
694                                    const u8 *data, size_t len)
695 {
696         struct os_time tv;
697         long int l;
698         const u8 *addr[3];
699         size_t elen[3];
700
701         os_get_time(&tv);
702         l = os_random();
703         addr[0] = (u8 *) &tv;
704         elen[0] = sizeof(tv);
705         addr[1] = data;
706         elen[1] = len;
707         addr[2] = (u8 *) &l;
708         elen[2] = sizeof(l);
709         md5_vector(3, addr, elen, msg->hdr->authenticator);
710 }
711
712
713 /* Get Vendor-specific RADIUS Attribute from a parsed RADIUS message.
714  * Returns the Attribute payload and sets alen to indicate the length of the
715  * payload if a vendor attribute with subtype is found, otherwise returns NULL.
716  * The returned payload is allocated with os_malloc() and caller must free it
717  * by calling os_free().
718  */
719 static u8 *radius_msg_get_vendor_attr(struct radius_msg *msg, u32 vendor,
720                                       u8 subtype, size_t *alen)
721 {
722         u8 *data, *pos;
723         size_t i, len;
724
725         if (msg == NULL)
726                 return NULL;
727
728         for (i = 0; i < msg->attr_used; i++) {
729                 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
730                 size_t left;
731                 u32 vendor_id;
732                 struct radius_attr_vendor *vhdr;
733
734                 if (attr->type != RADIUS_ATTR_VENDOR_SPECIFIC)
735                         continue;
736
737                 left = attr->length - sizeof(*attr);
738                 if (left < 4)
739                         continue;
740
741                 pos = (u8 *) (attr + 1);
742
743                 os_memcpy(&vendor_id, pos, 4);
744                 pos += 4;
745                 left -= 4;
746
747                 if (ntohl(vendor_id) != vendor)
748                         continue;
749
750                 while (left >= sizeof(*vhdr)) {
751                         vhdr = (struct radius_attr_vendor *) pos;
752                         if (vhdr->vendor_length > left ||
753                             vhdr->vendor_length < sizeof(*vhdr)) {
754                                 left = 0;
755                                 break;
756                         }
757                         if (vhdr->vendor_type != subtype) {
758                                 pos += vhdr->vendor_length;
759                                 left -= vhdr->vendor_length;
760                                 continue;
761                         }
762
763                         len = vhdr->vendor_length - sizeof(*vhdr);
764                         data = os_malloc(len);
765                         if (data == NULL)
766                                 return NULL;
767                         os_memcpy(data, pos + sizeof(*vhdr), len);
768                         if (alen)
769                                 *alen = len;
770                         return data;
771                 }
772         }
773
774         return NULL;
775 }
776
777
778 static u8 * decrypt_ms_key(const u8 *key, size_t len,
779                            const u8 *req_authenticator,
780                            const u8 *secret, size_t secret_len, size_t *reslen)
781 {
782         u8 *plain, *ppos, *res;
783         const u8 *pos;
784         size_t left, plen;
785         u8 hash[MD5_MAC_LEN];
786         int i, first = 1;
787         const u8 *addr[3];
788         size_t elen[3];
789
790         /* key: 16-bit salt followed by encrypted key info */
791
792         if (len < 2 + 16)
793                 return NULL;
794
795         pos = key + 2;
796         left = len - 2;
797         if (left % 16) {
798                 printf("Invalid ms key len %lu\n", (unsigned long) left);
799                 return NULL;
800         }
801
802         plen = left;
803         ppos = plain = os_malloc(plen);
804         if (plain == NULL)
805                 return NULL;
806
807         while (left > 0) {
808                 /* b(1) = MD5(Secret + Request-Authenticator + Salt)
809                  * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
810
811                 addr[0] = secret;
812                 elen[0] = secret_len;
813                 if (first) {
814                         addr[1] = req_authenticator;
815                         elen[1] = MD5_MAC_LEN;
816                         addr[2] = key;
817                         elen[2] = 2; /* Salt */
818                 } else {
819                         addr[1] = pos - MD5_MAC_LEN;
820                         elen[1] = MD5_MAC_LEN;
821                 }
822                 md5_vector(first ? 3 : 2, addr, elen, hash);
823                 first = 0;
824
825                 for (i = 0; i < MD5_MAC_LEN; i++)
826                         *ppos++ = *pos++ ^ hash[i];
827                 left -= MD5_MAC_LEN;
828         }
829
830         if (plain[0] > plen - 1) {
831                 printf("Failed to decrypt MPPE key\n");
832                 os_free(plain);
833                 return NULL;
834         }
835
836         res = os_malloc(plain[0]);
837         if (res == NULL) {
838                 os_free(plain);
839                 return NULL;
840         }
841         os_memcpy(res, plain + 1, plain[0]);
842         if (reslen)
843                 *reslen = plain[0];
844         os_free(plain);
845         return res;
846 }
847
848
849 static void encrypt_ms_key(const u8 *key, size_t key_len, u16 salt,
850                            const u8 *req_authenticator,
851                            const u8 *secret, size_t secret_len,
852                            u8 *ebuf, size_t *elen)
853 {
854         int i, len, first = 1;
855         u8 hash[MD5_MAC_LEN], saltbuf[2], *pos;
856         const u8 *addr[3];
857         size_t _len[3];
858
859         WPA_PUT_BE16(saltbuf, salt);
860
861         len = 1 + key_len;
862         if (len & 0x0f) {
863                 len = (len & 0xf0) + 16;
864         }
865         os_memset(ebuf, 0, len);
866         ebuf[0] = key_len;
867         os_memcpy(ebuf + 1, key, key_len);
868
869         *elen = len;
870
871         pos = ebuf;
872         while (len > 0) {
873                 /* b(1) = MD5(Secret + Request-Authenticator + Salt)
874                  * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
875                 addr[0] = secret;
876                 _len[0] = secret_len;
877                 if (first) {
878                         addr[1] = req_authenticator;
879                         _len[1] = MD5_MAC_LEN;
880                         addr[2] = saltbuf;
881                         _len[2] = sizeof(saltbuf);
882                 } else {
883                         addr[1] = pos - MD5_MAC_LEN;
884                         _len[1] = MD5_MAC_LEN;
885                 }
886                 md5_vector(first ? 3 : 2, addr, _len, hash);
887                 first = 0;
888
889                 for (i = 0; i < MD5_MAC_LEN; i++)
890                         *pos++ ^= hash[i];
891
892                 len -= MD5_MAC_LEN;
893         }
894 }
895
896
897 struct radius_ms_mppe_keys *
898 radius_msg_get_ms_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
899                        u8 *secret, size_t secret_len)
900 {
901         u8 *key;
902         size_t keylen;
903         struct radius_ms_mppe_keys *keys;
904
905         if (msg == NULL || sent_msg == NULL)
906                 return NULL;
907
908         keys = os_zalloc(sizeof(*keys));
909         if (keys == NULL)
910                 return NULL;
911
912         key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
913                                          RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY,
914                                          &keylen);
915         if (key) {
916                 keys->send = decrypt_ms_key(key, keylen,
917                                             sent_msg->hdr->authenticator,
918                                             secret, secret_len,
919                                             &keys->send_len);
920                 os_free(key);
921         }
922
923         key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
924                                          RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY,
925                                          &keylen);
926         if (key) {
927                 keys->recv = decrypt_ms_key(key, keylen,
928                                             sent_msg->hdr->authenticator,
929                                             secret, secret_len,
930                                             &keys->recv_len);
931                 os_free(key);
932         }
933
934         return keys;
935 }
936
937
938 struct radius_ms_mppe_keys *
939 radius_msg_get_cisco_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
940                           u8 *secret, size_t secret_len)
941 {
942         u8 *key;
943         size_t keylen;
944         struct radius_ms_mppe_keys *keys;
945
946         if (msg == NULL || sent_msg == NULL)
947                 return NULL;
948
949         keys = os_zalloc(sizeof(*keys));
950         if (keys == NULL)
951                 return NULL;
952
953         key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_CISCO,
954                                          RADIUS_CISCO_AV_PAIR, &keylen);
955         if (key && keylen == 51 &&
956             os_memcmp(key, "leap:session-key=", 17) == 0) {
957                 keys->recv = decrypt_ms_key(key + 17, keylen - 17,
958                                             sent_msg->hdr->authenticator,
959                                             secret, secret_len,
960                                             &keys->recv_len);
961         }
962         os_free(key);
963
964         return keys;
965 }
966
967
968 int radius_msg_add_mppe_keys(struct radius_msg *msg,
969                              const u8 *req_authenticator,
970                              const u8 *secret, size_t secret_len,
971                              const u8 *send_key, size_t send_key_len,
972                              const u8 *recv_key, size_t recv_key_len)
973 {
974         struct radius_attr_hdr *attr;
975         u32 vendor_id = htonl(RADIUS_VENDOR_ID_MICROSOFT);
976         u8 *buf;
977         struct radius_attr_vendor *vhdr;
978         u8 *pos;
979         size_t elen;
980         int hlen;
981         u16 salt;
982
983         hlen = sizeof(vendor_id) + sizeof(*vhdr) + 2;
984
985         /* MS-MPPE-Send-Key */
986         buf = os_malloc(hlen + send_key_len + 16);
987         if (buf == NULL) {
988                 return 0;
989         }
990         pos = buf;
991         os_memcpy(pos, &vendor_id, sizeof(vendor_id));
992         pos += sizeof(vendor_id);
993         vhdr = (struct radius_attr_vendor *) pos;
994         vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY;
995         pos = (u8 *) (vhdr + 1);
996         salt = os_random() | 0x8000;
997         WPA_PUT_BE16(pos, salt);
998         pos += 2;
999         encrypt_ms_key(send_key, send_key_len, salt, req_authenticator, secret,
1000                        secret_len, pos, &elen);
1001         vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
1002
1003         attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1004                                    buf, hlen + elen);
1005         os_free(buf);
1006         if (attr == NULL) {
1007                 return 0;
1008         }
1009
1010         /* MS-MPPE-Recv-Key */
1011         buf = os_malloc(hlen + send_key_len + 16);
1012         if (buf == NULL) {
1013                 return 0;
1014         }
1015         pos = buf;
1016         os_memcpy(pos, &vendor_id, sizeof(vendor_id));
1017         pos += sizeof(vendor_id);
1018         vhdr = (struct radius_attr_vendor *) pos;
1019         vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY;
1020         pos = (u8 *) (vhdr + 1);
1021         salt ^= 1;
1022         WPA_PUT_BE16(pos, salt);
1023         pos += 2;
1024         encrypt_ms_key(recv_key, recv_key_len, salt, req_authenticator, secret,
1025                        secret_len, pos, &elen);
1026         vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
1027
1028         attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1029                                    buf, hlen + elen);
1030         os_free(buf);
1031         if (attr == NULL) {
1032                 return 0;
1033         }
1034
1035         return 1;
1036 }
1037
1038
1039 /* Add User-Password attribute to a RADIUS message and encrypt it as specified
1040  * in RFC 2865, Chap. 5.2 */
1041 struct radius_attr_hdr *
1042 radius_msg_add_attr_user_password(struct radius_msg *msg,
1043                                   u8 *data, size_t data_len,
1044                                   u8 *secret, size_t secret_len)
1045 {
1046         u8 buf[128];
1047         int padlen, i;
1048         size_t buf_len, pos;
1049         const u8 *addr[2];
1050         size_t len[2];
1051         u8 hash[16];
1052
1053         if (data_len > 128)
1054                 return NULL;
1055
1056         os_memcpy(buf, data, data_len);
1057         buf_len = data_len;
1058
1059         padlen = data_len % 16;
1060         if (padlen) {
1061                 padlen = 16 - padlen;
1062                 os_memset(buf + data_len, 0, padlen);
1063                 buf_len += padlen;
1064         }
1065
1066         addr[0] = secret;
1067         len[0] = secret_len;
1068         addr[1] = msg->hdr->authenticator;
1069         len[1] = 16;
1070         md5_vector(2, addr, len, hash);
1071
1072         for (i = 0; i < 16; i++)
1073                 buf[i] ^= hash[i];
1074         pos = 16;
1075
1076         while (pos < buf_len) {
1077                 addr[0] = secret;
1078                 len[0] = secret_len;
1079                 addr[1] = &buf[pos - 16];
1080                 len[1] = 16;
1081                 md5_vector(2, addr, len, hash);
1082
1083                 for (i = 0; i < 16; i++)
1084                         buf[pos + i] ^= hash[i];
1085
1086                 pos += 16;
1087         }
1088
1089         return radius_msg_add_attr(msg, RADIUS_ATTR_USER_PASSWORD,
1090                                    buf, buf_len);
1091 }
1092
1093
1094 int radius_msg_get_attr(struct radius_msg *msg, u8 type, u8 *buf, size_t len)
1095 {
1096         struct radius_attr_hdr *attr = NULL, *tmp;
1097         size_t i, dlen;
1098
1099         for (i = 0; i < msg->attr_used; i++) {
1100                 tmp = radius_get_attr_hdr(msg, i);
1101                 if (tmp->type == type) {
1102                         attr = tmp;
1103                         break;
1104                 }
1105         }
1106
1107         if (!attr)
1108                 return -1;
1109
1110         dlen = attr->length - sizeof(*attr);
1111         if (buf)
1112                 os_memcpy(buf, (attr + 1), dlen > len ? len : dlen);
1113         return dlen;
1114 }
1115
1116
1117 int radius_msg_get_attr_ptr(struct radius_msg *msg, u8 type, u8 **buf,
1118                             size_t *len, const u8 *start)
1119 {
1120         size_t i;
1121         struct radius_attr_hdr *attr = NULL, *tmp;
1122
1123         for (i = 0; i < msg->attr_used; i++) {
1124                 tmp = radius_get_attr_hdr(msg, i);
1125                 if (tmp->type == type &&
1126                     (start == NULL || (u8 *) tmp > start)) {
1127                         attr = tmp;
1128                         break;
1129                 }
1130         }
1131
1132         if (!attr)
1133                 return -1;
1134
1135         *buf = (u8 *) (attr + 1);
1136         *len = attr->length - sizeof(*attr);
1137         return 0;
1138 }
1139
1140
1141 int radius_msg_count_attr(struct radius_msg *msg, u8 type, int min_len)
1142 {
1143         size_t i;
1144         int count;
1145
1146         for (count = 0, i = 0; i < msg->attr_used; i++) {
1147                 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
1148                 if (attr->type == type &&
1149                     attr->length >= sizeof(struct radius_attr_hdr) + min_len)
1150                         count++;
1151         }
1152
1153         return count;
1154 }
1155
1156
1157 struct radius_tunnel_attrs {
1158         int tag_used;
1159         int type; /* Tunnel-Type */
1160         int medium_type; /* Tunnel-Medium-Type */
1161         int vlanid;
1162 };
1163
1164
1165 /**
1166  * radius_msg_get_vlanid - Parse RADIUS attributes for VLAN tunnel information
1167  * @msg: RADIUS message
1168  * Returns: VLAN ID for the first tunnel configuration of -1 if none is found
1169  */
1170 int radius_msg_get_vlanid(struct radius_msg *msg)
1171 {
1172         struct radius_tunnel_attrs tunnel[RADIUS_TUNNEL_TAGS], *tun;
1173         size_t i;
1174         struct radius_attr_hdr *attr = NULL;
1175         const u8 *data;
1176         char buf[10];
1177         size_t dlen;
1178
1179         os_memset(&tunnel, 0, sizeof(tunnel));
1180
1181         for (i = 0; i < msg->attr_used; i++) {
1182                 attr = radius_get_attr_hdr(msg, i);
1183                 data = (const u8 *) (attr + 1);
1184                 dlen = attr->length - sizeof(*attr);
1185                 if (attr->length < 3)
1186                         continue;
1187                 if (data[0] >= RADIUS_TUNNEL_TAGS)
1188                         tun = &tunnel[0];
1189                 else
1190                         tun = &tunnel[data[0]];
1191
1192                 switch (attr->type) {
1193                 case RADIUS_ATTR_TUNNEL_TYPE:
1194                         if (attr->length != 6)
1195                                 break;
1196                         tun->tag_used++;
1197                         tun->type = WPA_GET_BE24(data + 1);
1198                         break;
1199                 case RADIUS_ATTR_TUNNEL_MEDIUM_TYPE:
1200                         if (attr->length != 6)
1201                                 break;
1202                         tun->tag_used++;
1203                         tun->medium_type = WPA_GET_BE24(data + 1);
1204                         break;
1205                 case RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID:
1206                         if (data[0] < RADIUS_TUNNEL_TAGS) {
1207                                 data++;
1208                                 dlen--;
1209                         }
1210                         if (dlen >= sizeof(buf))
1211                                 break;
1212                         os_memcpy(buf, data, dlen);
1213                         buf[dlen] = '\0';
1214                         tun->tag_used++;
1215                         tun->vlanid = atoi(buf);
1216                         break;
1217                 }
1218         }
1219
1220         for (i = 0; i < RADIUS_TUNNEL_TAGS; i++) {
1221                 tun = &tunnel[i];
1222                 if (tun->tag_used &&
1223                     tun->type == RADIUS_TUNNEL_TYPE_VLAN &&
1224                     tun->medium_type == RADIUS_TUNNEL_MEDIUM_TYPE_802 &&
1225                     tun->vlanid > 0)
1226                         return tun->vlanid;
1227         }
1228
1229         return -1;
1230 }