2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * @brief Functions to send/receive radius packets.
23 * @copyright 2000-2003,2006 The FreeRADIUS server project
26 #include <freeradius-devel/ident.h>
29 #include <freeradius-devel/libradius.h>
30 #include <freeradius-devel/md5.h>
36 #include <freeradius-devel/udpfromto.h>
44 #define VP_TRACE if (fr_debug_flag) printf
46 static void VP_HEXDUMP(const char *msg, const uint8_t *data, size_t len)
50 printf("--- %s ---\n", msg);
51 for (i = 0; i < len; i++) {
52 if ((i & 0x0f) == 0) printf("%04x: ", (unsigned int) i);
53 printf("%02x ", data[i]);
54 if ((i & 0x0f) == 0x0f) printf("\n");
56 if ((len == 0x0f) || ((len & 0x0f) != 0x0f)) printf("\n");
60 #define VP_TRACE(_x, ...)
61 #define VP_HEXDUMP(_x, _y, _z)
66 * The RFC says 4096 octets max, and most packets are less than 256.
68 #define MAX_PACKET_LEN 4096
71 * The maximum number of attributes which we allow in an incoming
72 * request. If there are more attributes than this, the request
75 * This helps to minimize the potential for a DoS, when an
76 * attacker spoofs Access-Request packets, which don't have a
77 * Message-Authenticator attribute. This means that the packet
78 * is unsigned, and the attacker can use resources on the server,
79 * even if the end request is rejected.
81 int fr_max_attributes = 0;
82 FILE *fr_log_fp = NULL;
84 typedef struct radius_packet_t {
88 uint8_t vector[AUTH_VECTOR_LEN];
92 static fr_randctx fr_rand_pool; /* across multiple calls */
93 static int fr_rand_initialized = 0;
94 static unsigned int salt_offset = 0;
95 static uint8_t nullvector[AUTH_VECTOR_LEN] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; /* for CoA decode */
97 const char *fr_packet_codes[FR_MAX_PACKET_CODE] = {
102 "Accounting-Request",
103 "Accounting-Response",
108 "Accounting-Message",
119 "Resource-Free-Request",
120 "Resource-Free-Response",
121 "Resource-Query-Request",
122 "Resource-Query-Response",
123 "Alternate-Resource-Reclaim-Request",
124 "NAS-Reboot-Request",
125 "NAS-Reboot-Response",
138 "Disconnect-Request",
148 "IP-Address-Allocate",
153 void fr_printf_log(const char *fmt, ...)
158 if ((fr_debug_flag == 0) || !fr_log_fp) {
163 vfprintf(fr_log_fp, fmt, ap);
169 static const char *tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
171 static void print_hex_data(const uint8_t *ptr, int attrlen, int depth)
175 for (i = 0; i < attrlen; i++) {
176 if ((i > 0) && ((i & 0x0f) == 0x00))
177 fprintf(fr_log_fp, "%.*s", depth, tabs);
178 fprintf(fr_log_fp, "%02x ", ptr[i]);
179 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
181 if ((i & 0x0f) != 0) fprintf(fr_log_fp, "\n");
185 void rad_print_hex(RADIUS_PACKET *packet)
189 if (!packet->data || !fr_log_fp) return;
191 fprintf(fr_log_fp, " Code:\t\t%u\n", packet->data[0]);
192 fprintf(fr_log_fp, " Id:\t\t%u\n", packet->data[1]);
193 fprintf(fr_log_fp, " Length:\t%u\n", ((packet->data[2] << 8) |
195 fprintf(fr_log_fp, " Vector:\t");
196 for (i = 4; i < 20; i++) {
197 fprintf(fr_log_fp, "%02x", packet->data[i]);
199 fprintf(fr_log_fp, "\n");
201 if (packet->data_len > 20) {
204 fprintf(fr_log_fp, " Data:");
206 total = packet->data_len - 20;
207 ptr = packet->data + 20;
211 unsigned int vendor = 0;
213 fprintf(fr_log_fp, "\t\t");
214 if (total < 2) { /* too short */
215 fprintf(fr_log_fp, "%02x\n", *ptr);
219 if (ptr[1] > total) { /* too long */
220 for (i = 0; i < total; i++) {
221 fprintf(fr_log_fp, "%02x ", ptr[i]);
226 fprintf(fr_log_fp, "%02x %02x ", ptr[0], ptr[1]);
227 attrlen = ptr[1] - 2;
229 if ((ptr[0] == PW_VENDOR_SPECIFIC) &&
231 vendor = (ptr[3] << 16) | (ptr[4] << 8) | ptr[5];
232 fprintf(fr_log_fp, "%02x%02x%02x%02x (%u) ",
233 ptr[2], ptr[3], ptr[4], ptr[5], vendor);
243 print_hex_data(ptr, attrlen, 3);
253 * @brief Wrapper for sendto which handles sendfromto, IPv6, and all
254 * possible combinations.
256 static int rad_sendto(int sockfd, void *data, size_t data_len, int flags,
257 fr_ipaddr_t *src_ipaddr, int src_port,
258 fr_ipaddr_t *dst_ipaddr, int dst_port)
261 struct sockaddr_storage dst;
262 socklen_t sizeof_dst;
264 #ifdef WITH_UDPFROMTO
265 struct sockaddr_storage src;
266 socklen_t sizeof_src;
268 fr_ipaddr2sockaddr(src_ipaddr, src_port, &src, &sizeof_src);
270 src_port = src_port; /* -Wunused */
273 if (!fr_ipaddr2sockaddr(dst_ipaddr, dst_port, &dst, &sizeof_dst)) {
277 #ifdef WITH_UDPFROMTO
279 * And if they don't specify a source IP address, don't
282 if (((dst_ipaddr->af == AF_INET) || (dst_ipaddr->af == AF_INET6)) &&
283 (src_ipaddr->af != AF_UNSPEC) &&
284 !fr_inaddr_any(src_ipaddr)) {
285 rcode = sendfromto(sockfd, data, data_len, flags,
286 (struct sockaddr *)&src, sizeof_src,
287 (struct sockaddr *)&dst, sizeof_dst);
291 src_ipaddr = src_ipaddr; /* -Wunused */
295 * No udpfromto, fail gracefully.
297 rcode = sendto(sockfd, data, data_len, flags,
298 (struct sockaddr *) &dst, sizeof_dst);
299 #ifdef WITH_UDPFROMTO
303 DEBUG("rad_send() failed: %s\n", strerror(errno));
310 void rad_recv_discard(int sockfd)
313 struct sockaddr_storage src;
314 socklen_t sizeof_src = sizeof(src);
316 (void) recvfrom(sockfd, header, sizeof(header), 0,
317 (struct sockaddr *)&src, &sizeof_src);
321 ssize_t rad_recv_header(int sockfd, fr_ipaddr_t *src_ipaddr, int *src_port,
324 ssize_t data_len, packet_len;
326 struct sockaddr_storage src;
327 socklen_t sizeof_src = sizeof(src);
329 data_len = recvfrom(sockfd, header, sizeof(header), MSG_PEEK,
330 (struct sockaddr *)&src, &sizeof_src);
332 if ((errno == EAGAIN) || (errno == EINTR)) return 0;
337 * Too little data is available, discard the packet.
340 rad_recv_discard(sockfd);
344 } else { /* we got 4 bytes of data. */
346 * See how long the packet says it is.
348 packet_len = (header[2] * 256) + header[3];
351 * The length in the packet says it's less than
352 * a RADIUS header length: discard it.
354 if (packet_len < AUTH_HDR_LEN) {
355 rad_recv_discard(sockfd);
360 * Enforce RFC requirements, for sanity.
361 * Anything after 4k will be discarded.
363 } else if (packet_len > MAX_PACKET_LEN) {
364 rad_recv_discard(sockfd);
371 * Convert AF. If unknown, discard packet.
373 if (!fr_sockaddr2ipaddr(&src, sizeof_src, src_ipaddr, src_port)) {
374 rad_recv_discard(sockfd);
382 * The packet says it's this long, but the actual UDP
383 * size could still be smaller.
390 * @brief wrapper for recvfrom, which handles recvfromto, IPv6, and all
391 * possible combinations.
393 static ssize_t rad_recvfrom(int sockfd, uint8_t **pbuf, int flags,
394 fr_ipaddr_t *src_ipaddr, uint16_t *src_port,
395 fr_ipaddr_t *dst_ipaddr, uint16_t *dst_port)
397 struct sockaddr_storage src;
398 struct sockaddr_storage dst;
399 socklen_t sizeof_src = sizeof(src);
400 socklen_t sizeof_dst = sizeof(dst);
407 memset(&src, 0, sizeof_src);
408 memset(&dst, 0, sizeof_dst);
411 * Get address family, etc. first, so we know if we
412 * need to do udpfromto.
414 * FIXME: udpfromto also does this, but it's not
415 * a critical problem.
417 if (getsockname(sockfd, (struct sockaddr *)&dst,
418 &sizeof_dst) < 0) return -1;
421 * Read the length of the packet, from the packet.
422 * This lets us allocate the buffer to use for
423 * reading the rest of the packet.
425 data_len = recvfrom(sockfd, header, sizeof(header), MSG_PEEK,
426 (struct sockaddr *)&src, &sizeof_src);
428 if ((errno == EAGAIN) || (errno == EINTR)) return 0;
433 * Too little data is available, discard the packet.
436 rad_recv_discard(sockfd);
440 } else { /* we got 4 bytes of data. */
442 * See how long the packet says it is.
444 len = (header[2] * 256) + header[3];
447 * The length in the packet says it's less than
448 * a RADIUS header length: discard it.
450 if (len < AUTH_HDR_LEN) {
451 recvfrom(sockfd, header, sizeof(header), flags,
452 (struct sockaddr *)&src, &sizeof_src);
456 * Enforce RFC requirements, for sanity.
457 * Anything after 4k will be discarded.
459 } else if (len > MAX_PACKET_LEN) {
460 recvfrom(sockfd, header, sizeof(header), flags,
461 (struct sockaddr *)&src, &sizeof_src);
470 * Receive the packet. The OS will discard any data in the
471 * packet after "len" bytes.
473 #ifdef WITH_UDPFROMTO
474 if ((dst.ss_family == AF_INET) || (dst.ss_family == AF_INET6)) {
475 data_len = recvfromto(sockfd, buf, len, flags,
476 (struct sockaddr *)&src, &sizeof_src,
477 (struct sockaddr *)&dst, &sizeof_dst);
481 * No udpfromto, fail gracefully.
483 data_len = recvfrom(sockfd, buf, len, flags,
484 (struct sockaddr *)&src, &sizeof_src);
490 if (!fr_sockaddr2ipaddr(&src, sizeof_src, src_ipaddr, &port)) {
492 return -1; /* Unknown address family, Die Die Die! */
496 fr_sockaddr2ipaddr(&dst, sizeof_dst, dst_ipaddr, &port);
500 * Different address families should never happen.
502 if (src.ss_family != dst.ss_family) {
508 * Tell the caller about the data
516 #define AUTH_PASS_LEN (AUTH_VECTOR_LEN)
518 * @brief Build an encrypted secret value to return in a reply packet
520 * The secret is hidden by xoring with a MD5 digest
521 * created from the shared secret and the authentication
522 * vector. We put them into MD5 in the reverse order from
523 * that used when encrypting passwords to RADIUS.
526 static void make_secret(uint8_t *digest, const uint8_t *vector,
527 const char *secret, const uint8_t *value)
532 fr_MD5Init(&context);
533 fr_MD5Update(&context, vector, AUTH_VECTOR_LEN);
534 fr_MD5Update(&context, (const uint8_t *) secret, strlen(secret));
535 fr_MD5Final(digest, &context);
537 for ( i = 0; i < AUTH_VECTOR_LEN; i++ ) {
538 digest[i] ^= value[i];
542 #define MAX_PASS_LEN (128)
543 static void make_passwd(uint8_t *output, ssize_t *outlen,
544 const uint8_t *input, size_t inlen,
545 const char *secret, const uint8_t *vector)
547 FR_MD5_CTX context, old;
548 uint8_t digest[AUTH_VECTOR_LEN];
549 uint8_t passwd[MAX_PASS_LEN];
554 * If the length is zero, round it up.
558 if (len > MAX_PASS_LEN) len = MAX_PASS_LEN;
560 memcpy(passwd, input, len);
561 if (len < sizeof(passwd)) memset(passwd + len, 0, sizeof(passwd) - len);
567 else if ((len & 0x0f) != 0) {
573 fr_MD5Init(&context);
574 fr_MD5Update(&context, (const uint8_t *) secret, strlen(secret));
580 fr_MD5Update(&context, vector, AUTH_PASS_LEN);
582 for (n = 0; n < len; n += AUTH_PASS_LEN) {
585 fr_MD5Update(&context,
586 passwd + n - AUTH_PASS_LEN,
590 fr_MD5Final(digest, &context);
591 for (i = 0; i < AUTH_PASS_LEN; i++) {
592 passwd[i + n] ^= digest[i];
596 memcpy(output, passwd, len);
599 static void make_tunnel_passwd(uint8_t *output, ssize_t *outlen,
600 const uint8_t *input, size_t inlen, size_t room,
601 const char *secret, const uint8_t *vector)
603 FR_MD5_CTX context, old;
604 uint8_t digest[AUTH_VECTOR_LEN];
605 uint8_t passwd[MAX_STRING_LEN + AUTH_VECTOR_LEN];
612 if (room > 253) room = 253;
615 * Account for 2 bytes of the salt, and round the room
616 * available down to the nearest multiple of 16. Then,
617 * subtract one from that to account for the length byte,
618 * and the resulting number is the upper bound on the data
621 * We could short-cut this calculation just be forcing
622 * inlen to be no more than 239. It would work for all
623 * VSA's, as we don't pack multiple VSA's into one
626 * However, this calculation is more general, if a little
627 * complex. And it will work in the future for all possible
628 * kinds of weird attribute packing.
631 room -= (room & 0x0f);
634 if (inlen > room) inlen = room;
637 * Length of the encrypted data is password length plus
638 * one byte for the length of the password.
641 if ((len & 0x0f) != 0) {
645 *outlen = len + 2; /* account for the salt */
648 * Copy the password over.
650 memcpy(passwd + 3, input, inlen);
651 memset(passwd + 3 + inlen, 0, sizeof(passwd) - 3 - inlen);
654 * Generate salt. The RFC's say:
656 * The high bit of salt[0] must be set, each salt in a
657 * packet should be unique, and they should be random
659 * So, we set the high bit, add in a counter, and then
660 * add in some CSPRNG data. should be OK..
662 passwd[0] = (0x80 | ( ((salt_offset++) & 0x0f) << 3) |
664 passwd[1] = fr_rand();
665 passwd[2] = inlen; /* length of the password string */
667 fr_MD5Init(&context);
668 fr_MD5Update(&context, (const uint8_t *) secret, strlen(secret));
671 fr_MD5Update(&context, vector, AUTH_VECTOR_LEN);
672 fr_MD5Update(&context, &passwd[0], 2);
674 for (n = 0; n < len; n += AUTH_PASS_LEN) {
677 fr_MD5Update(&context,
678 passwd + 2 + n - AUTH_PASS_LEN,
682 fr_MD5Final(digest, &context);
684 for (i = 0; i < AUTH_PASS_LEN; i++) {
685 passwd[i + 2 + n] ^= digest[i];
688 memcpy(output, passwd, len + 2);
691 extern int fr_attr_max_tlv;
692 extern int fr_attr_shift[];
693 extern int fr_attr_mask[];
695 static int do_next_tlv(const VALUE_PAIR *vp, const VALUE_PAIR *next, int nest)
697 unsigned int tlv1, tlv2;
699 if (nest > fr_attr_max_tlv) return 0;
704 * Keep encoding TLVs which have the same scope.
705 * e.g. two attributes of:
706 * ATTR.TLV1.TLV2.TLV3 = data1
707 * ATTR.TLV1.TLV2.TLV4 = data2
708 * both get put into a container of "ATTR.TLV1.TLV2"
712 * Nothing to follow, we're done.
717 * Not from the same vendor, skip it.
719 if (vp->da->vendor != next->da->vendor) return 0;
722 * In a different TLV space, skip it.
725 tlv2 = next->da->attr;
727 tlv1 &= ((1 << fr_attr_shift[nest]) - 1);
728 tlv2 &= ((1 << fr_attr_shift[nest]) - 1);
730 if (tlv1 != tlv2) return 0;
736 static ssize_t vp2data_any(const RADIUS_PACKET *packet,
737 const RADIUS_PACKET *original,
738 const char *secret, int nest,
739 const VALUE_PAIR **pvp,
740 uint8_t *start, size_t room);
742 static ssize_t vp2attr_rfc(const RADIUS_PACKET *packet,
743 const RADIUS_PACKET *original,
744 const char *secret, const VALUE_PAIR **pvp,
745 unsigned int attribute, uint8_t *ptr, size_t room);
748 * @brief This is really a sub-function of vp2data_any(). It encodes
749 * the *data* portion of the TLV, and assumes that the encapsulating
750 * attribute has already been encoded.
752 static ssize_t vp2data_tlvs(const RADIUS_PACKET *packet,
753 const RADIUS_PACKET *original,
754 const char *secret, int nest,
755 const VALUE_PAIR **pvp,
756 uint8_t *start, size_t room)
760 uint8_t *ptr = start;
761 const VALUE_PAIR *vp = *pvp;
762 const VALUE_PAIR *svp = vp;
767 if (nest > fr_attr_max_tlv) {
768 fr_strerror_printf("vp2data_tlvs: attribute nesting overflow");
774 if (room < 2) return ptr - start;
776 ptr[0] = (vp->da->attr >> fr_attr_shift[nest]) & fr_attr_mask[nest];
780 if (room > 255) my_room = 255;
782 len = vp2data_any(packet, original, secret, nest,
783 &vp, ptr + 2, my_room - 2);
784 if (len < 0) return len;
785 if (len == 0) return ptr - start;
786 /* len can NEVER be more than 253 */
791 if ((fr_debug_flag > 3) && fr_log_fp) {
792 fprintf(fr_log_fp, "\t\t%02x %02x ", ptr[0], ptr[1]);
793 print_hex_data(ptr + 2, len, 3);
801 if (!do_next_tlv(svp, vp, nest)) break;
805 if ((fr_debug_flag > 3) && fr_log_fp) {
808 da = dict_attrbyvalue(svp->da->attr & ((1 << fr_attr_shift[nest ]) - 1), svp->da->vendor);
809 if (da) fprintf(fr_log_fp, "\t%s = ...\n", da->name);
817 * @brief Encodes the data portion of an attribute.
818 * @return -1 on error, or the length of the data portion.
820 static ssize_t vp2data_any(const RADIUS_PACKET *packet,
821 const RADIUS_PACKET *original,
822 const char *secret, int nest,
823 const VALUE_PAIR **pvp,
824 uint8_t *start, size_t room)
829 uint8_t *ptr = start;
832 const VALUE_PAIR *vp = *pvp;
835 * See if we need to encode a TLV. The low portion of
836 * the attribute has already been placed into the packer.
837 * If there are still attribute bytes left, then go
838 * encode them as TLVs.
840 * If we cared about the stack, we could unroll the loop.
842 if (vp->da->flags.is_tlv && (nest < fr_attr_max_tlv) &&
843 ((vp->da->attr >> fr_attr_shift[nest + 1]) != 0)) {
844 return vp2data_tlvs(packet, original, secret, nest + 1, pvp,
851 * Set up the default sources for the data.
853 data = vp->vp_octets;
857 * Short-circuit it for long attributes. They can't be
858 * encrypted, tagged, etc.
860 if ((vp->da->type & PW_FLAG_LONG) != 0) goto do_tlv;
862 switch(vp->da->type) {
866 case PW_TYPE_IPV6ADDR:
867 case PW_TYPE_IPV6PREFIX:
868 case PW_TYPE_IPV4PREFIX:
869 case PW_TYPE_ABINARY:
870 /* nothing more to do */
874 len = 1; /* just in case */
875 array[0] = vp->vp_integer & 0xff;
880 len = 2; /* just in case */
881 array[0] = (vp->vp_integer >> 8) & 0xff;
882 array[1] = vp->vp_integer & 0xff;
886 case PW_TYPE_INTEGER:
887 len = 4; /* just in case */
888 lvalue = htonl(vp->vp_integer);
889 memcpy(array, &lvalue, sizeof(lvalue));
893 case PW_TYPE_INTEGER64:
894 len = 8; /* just in case */
895 lvalue64 = htonll(vp->vp_integer64);
896 data = (uint8_t *) &lvalue64;
900 data = (const uint8_t *) &vp->vp_ipaddr;
901 len = 4; /* just in case */
905 * There are no tagged date attributes.
908 lvalue = htonl(vp->vp_date);
909 data = (const uint8_t *) &lvalue;
910 len = 4; /* just in case */
917 len = 4; /* just in case */
918 slvalue = htonl(vp->vp_signed);
919 memcpy(array, &slvalue, sizeof(slvalue));
927 fr_strerror_printf("ERROR: Cannot encode NULL TLV");
932 default: /* unknown type: ignore it */
933 fr_strerror_printf("ERROR: Unknown attribute type %d",
947 * Bound the data to the calling size
949 if (len > (ssize_t) room) len = room;
952 * Encrypt the various password styles
954 * Attributes with encrypted values MUST be less than
957 switch (vp->da->flags.encrypt) {
958 case FLAG_ENCRYPT_USER_PASSWORD:
959 make_passwd(ptr, &len, data, len,
960 secret, packet->vector);
963 case FLAG_ENCRYPT_TUNNEL_PASSWORD:
965 if (vp->da->flags.has_tag) lvalue = 1;
968 * Check if there's enough room. If there isn't,
969 * we discard the attribute.
971 * This is ONLY a problem if we have multiple VSA's
972 * in one Vendor-Specific, though.
974 if (room < (18 + lvalue)) return 0;
976 switch (packet->code) {
977 case PW_AUTHENTICATION_ACK:
978 case PW_AUTHENTICATION_REJECT:
979 case PW_ACCESS_CHALLENGE:
982 fr_strerror_printf("ERROR: No request packet, cannot encrypt %s attribute in the vp.", vp->da->name);
986 if (lvalue) ptr[0] = vp->tag;
987 make_tunnel_passwd(ptr + lvalue, &len, data, len,
989 secret, original->vector);
991 case PW_ACCOUNTING_REQUEST:
992 case PW_DISCONNECT_REQUEST:
995 make_tunnel_passwd(ptr + 1, &len, data, len - 1, room,
996 secret, packet->vector);
1002 * The code above ensures that this attribute
1005 case FLAG_ENCRYPT_ASCEND_SECRET:
1006 if (len != 16) return 0;
1007 make_secret(ptr, packet->vector, secret, data);
1008 len = AUTH_VECTOR_LEN;
1013 if (vp->da->flags.has_tag && TAG_VALID(vp->tag)) {
1014 if (vp->da->type == PW_TYPE_STRING) {
1015 if (len > ((ssize_t) (room - 1))) len = room - 1;
1018 } else if (vp->da->type == PW_TYPE_INTEGER) {
1020 } /* else it can't be any other type */
1022 memcpy(ptr, data, len);
1024 } /* switch over encryption flags */
1027 return len + (ptr - start);
1030 static ssize_t attr_shift(const uint8_t *start, const uint8_t *end,
1031 uint8_t *ptr, int hdr_len, ssize_t len,
1032 int flag_offset, int vsa_offset)
1034 int check_len = len - ptr[1];
1035 int total = len + hdr_len;
1038 * Pass 1: Check if the addition of the headers
1039 * overflows the available room. If so, return
1040 * what we were capable of encoding.
1043 while (check_len > (255 - hdr_len)) {
1045 check_len -= (255 - hdr_len);
1049 * Note that this results in a number of attributes maybe
1050 * being marked as "encoded", but which aren't in the
1051 * packet. Oh well. The solution is to fix the
1052 * "vp2data_any" function to take into account the header
1055 if ((ptr + ptr[1] + total) > end) {
1056 return (ptr + ptr[1]) - start;
1060 * Pass 2: Now that we know there's enough room,
1061 * re-arrange the data to form a set of valid
1062 * RADIUS attributes.
1065 int sublen = 255 - ptr[1];
1067 if (len <= sublen) {
1072 memmove(ptr + 255 + hdr_len, ptr + 255, sublen);
1073 memcpy(ptr + 255, ptr, hdr_len);
1075 if (vsa_offset) ptr[vsa_offset] += sublen;
1076 ptr[flag_offset] |= 0x80;
1080 if (vsa_offset) ptr[vsa_offset] = 3;
1084 if (vsa_offset) ptr[vsa_offset] += len;
1086 return (ptr + ptr[1]) - start;
1091 * @brief Encode an "extended" attribute.
1093 int rad_vp2extended(const RADIUS_PACKET *packet,
1094 const RADIUS_PACKET *original,
1095 const char *secret, const VALUE_PAIR **pvp,
1096 uint8_t *ptr, size_t room)
1100 uint8_t *start = ptr;
1101 const VALUE_PAIR *vp = *pvp;
1103 if (!vp->da->flags.extended) {
1104 fr_strerror_printf("rad_vp2extended called for non-extended attribute");
1109 * The attribute number is encoded into the upper 8 bits
1112 ptr[0] = (vp->da->vendor / FR_MAX_VENDOR) & 0xff;
1114 if (!vp->da->flags.long_extended) {
1115 if (room < 3) return 0;
1118 ptr[2] = vp->da->attr & fr_attr_mask[0];
1121 if (room < 4) return 0;
1124 ptr[2] = vp->da->attr & fr_attr_mask[0];
1129 * Only "flagged" attributes can be longer than one
1132 if (!vp->da->flags.long_extended && (room > 255)) {
1139 if (vp->da->flags.evs) {
1140 uint8_t *evs = ptr + ptr[1];
1142 if (room < (size_t) (ptr[1] + 5)) return 0;
1146 evs[0] = 0; /* always zero */
1147 evs[1] = (vp->da->vendor >> 16) & 0xff;
1148 evs[2] = (vp->da->vendor >> 8) & 0xff;
1149 evs[3] = vp->da->vendor & 0xff;
1150 evs[4] = vp->da->attr & fr_attr_mask[0];
1156 len = vp2data_any(packet, original, secret, 0,
1157 pvp, ptr + ptr[1], room - hdr_len);
1158 if (len <= 0) return len;
1161 * There may be more than 252 octets of data encoded in
1162 * the attribute. If so, move the data up in the packet,
1163 * and copy the existing header over. Set the "M" flag ONLY
1164 * after copying the rest of the data.
1166 if (vp->da->flags.long_extended && (len > (255 - ptr[1]))) {
1167 return attr_shift(start, start + room, ptr, 4, len, 3, 0);
1173 if ((fr_debug_flag > 3) && fr_log_fp) {
1176 fprintf(fr_log_fp, "\t\t%02x %02x ", ptr[0], ptr[1]);
1177 if (!vp->da->flags.long_extended) {
1178 fprintf(fr_log_fp, "%02x ", ptr[2]);
1181 fprintf(fr_log_fp, "%02x %02x ", ptr[2], ptr[3]);
1185 if (vp->da->flags.evs) {
1186 fprintf(fr_log_fp, "%02x%02x%02x%02x (%u) %02x ",
1187 ptr[jump], ptr[jump + 1],
1188 ptr[jump + 2], ptr[jump + 3],
1189 ((ptr[jump + 1] << 16) |
1190 (ptr[jump + 2] << 8) |
1196 print_hex_data(ptr + jump, len, 3);
1200 return (ptr + ptr[1]) - start;
1205 * @brief Encode a WiMAX attribute.
1207 int rad_vp2wimax(const RADIUS_PACKET *packet,
1208 const RADIUS_PACKET *original,
1209 const char *secret, const VALUE_PAIR **pvp,
1210 uint8_t *ptr, size_t room)
1215 uint8_t *start = ptr;
1216 const VALUE_PAIR *vp = *pvp;
1219 * Double-check for WiMAX format.
1221 if (!vp->da->flags.wimax) {
1222 fr_strerror_printf("rad_vp2wimax called for non-WIMAX VSA");
1227 * Not enough room for:
1228 * attr, len, vendor-id, vsa, vsalen, continuation
1230 if (room < 9) return 0;
1233 * Build the Vendor-Specific header
1236 ptr[0] = PW_VENDOR_SPECIFIC;
1238 lvalue = htonl(vp->da->vendor);
1239 memcpy(ptr + 2, &lvalue, 4);
1240 ptr[6] = (vp->da->attr & fr_attr_mask[1]);
1242 ptr[8] = 0; /* continuation byte */
1246 len = vp2data_any(packet, original, secret, 0, pvp, ptr + ptr[1],
1248 if (len <= 0) return len;
1251 * There may be more than 252 octets of data encoded in
1252 * the attribute. If so, move the data up in the packet,
1253 * and copy the existing header over. Set the "C" flag
1254 * ONLY after copying the rest of the data.
1256 if (len > (255 - ptr[1])) {
1257 return attr_shift(start, start + room, ptr, hdr_len, len, 8, 7);
1264 if ((fr_debug_flag > 3) && fr_log_fp) {
1265 fprintf(fr_log_fp, "\t\t%02x %02x %02x%02x%02x%02x (%u) %02x %02x %02x ",
1267 ptr[2], ptr[3], ptr[4], ptr[5],
1268 (ptr[3] << 16) | (ptr[4] << 8) | ptr[5],
1269 ptr[6], ptr[7], ptr[8]);
1270 print_hex_data(ptr + 9, len, 3);
1274 return (ptr + ptr[1]) - start;
1278 * @brief Encode an RFC format TLV.
1280 * This could be a standard attribute,
1281 * or a TLV data type. If it's a standard attribute, then
1282 * vp->da->attr == attribute. Otherwise, attribute may be
1285 static ssize_t vp2attr_rfc(const RADIUS_PACKET *packet,
1286 const RADIUS_PACKET *original,
1287 const char *secret, const VALUE_PAIR **pvp,
1288 unsigned int attribute, uint8_t *ptr, size_t room)
1292 if (room < 2) return 0;
1294 ptr[0] = attribute & 0xff;
1297 if (room > ((unsigned) 255 - ptr[1])) room = 255 - ptr[1];
1299 len = vp2data_any(packet, original, secret, 0, pvp, ptr + ptr[1], room);
1300 if (len <= 0) return len;
1305 if ((fr_debug_flag > 3) && fr_log_fp) {
1306 fprintf(fr_log_fp, "\t\t%02x %02x ", ptr[0], ptr[1]);
1307 print_hex_data(ptr + 2, len, 3);
1316 * @brief Encode a VSA which is a TLV. If it's in the RFC format, call
1317 * vp2attr_rfc. Otherwise, encode it here.
1319 static ssize_t vp2attr_vsa(const RADIUS_PACKET *packet,
1320 const RADIUS_PACKET *original,
1321 const char *secret, const VALUE_PAIR **pvp,
1322 unsigned int attribute, unsigned int vendor,
1323 uint8_t *ptr, size_t room)
1327 const VALUE_PAIR *vp = *pvp;
1330 * Unknown vendor: RFC format.
1331 * Known vendor and RFC format: go do that.
1333 dv = dict_vendorbyvalue(vendor);
1335 (!vp->da->flags.is_tlv && (dv->type == 1) && (dv->length == 1))) {
1336 return vp2attr_rfc(packet, original, secret, pvp,
1337 attribute, ptr, room);
1342 fr_strerror_printf("vp2attr_vsa: Internal sanity check failed,"
1343 " type %u", (unsigned) dv->type);
1347 ptr[0] = 0; /* attr must be 24-bit */
1348 ptr[1] = (attribute >> 16) & 0xff;
1349 ptr[2] = (attribute >> 8) & 0xff;
1350 ptr[3] = attribute & 0xff;
1354 ptr[0] = (attribute >> 8) & 0xff;
1355 ptr[1] = attribute & 0xff;
1359 ptr[0] = attribute & 0xff;
1363 switch (dv->length) {
1365 fr_strerror_printf("vp2attr_vsa: Internal sanity check failed,"
1366 " length %u", (unsigned) dv->length);
1374 ptr[dv->type + 1] = dv->type + 2;
1378 ptr[dv->type] = dv->type + 1;
1383 if (room > ((unsigned) 255 - (dv->type + dv->length))) {
1384 room = 255 - (dv->type + dv->length);
1387 len = vp2data_any(packet, original, secret, 0, pvp,
1388 ptr + dv->type + dv->length, room);
1389 if (len <= 0) return len;
1391 if (dv->length) ptr[dv->type + dv->length - 1] += len;
1394 if ((fr_debug_flag > 3) && fr_log_fp) {
1400 if ((fr_debug_flag > 3) && fr_log_fp)
1401 fprintf(fr_log_fp, "\t\t%02x%02x%02x%02x ",
1402 ptr[0], ptr[1], ptr[2], ptr[3]);
1406 if ((fr_debug_flag > 3) && fr_log_fp)
1407 fprintf(fr_log_fp, "\t\t%02x%02x ",
1412 if ((fr_debug_flag > 3) && fr_log_fp)
1413 fprintf(fr_log_fp, "\t\t%02x ", ptr[0]);
1417 switch (dv->length) {
1422 fprintf(fr_log_fp, " ");
1426 fprintf(fr_log_fp, "%02x ",
1431 fprintf(fr_log_fp, "%02x%02x ",
1432 ptr[dv->type], ptr[dv->type] + 1);
1436 print_hex_data(ptr + dv->type + dv->length, len, 3);
1440 return dv->type + dv->length + len;
1445 * @brief Encode a Vendor-Specific attribute.
1447 int rad_vp2vsa(const RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1448 const char *secret, const VALUE_PAIR **pvp, uint8_t *ptr,
1453 const VALUE_PAIR *vp = *pvp;
1456 * Double-check for WiMAX format.
1458 if (vp->da->flags.wimax) {
1459 return rad_vp2wimax(packet, original, secret, pvp,
1463 if (vp->da->vendor > FR_MAX_VENDOR) {
1464 fr_strerror_printf("rad_vp2vsa: Invalid arguments");
1469 * Not enough room for:
1470 * attr, len, vendor-id
1472 if (room < 6) return 0;
1475 * Build the Vendor-Specific header
1477 ptr[0] = PW_VENDOR_SPECIFIC;
1479 lvalue = htonl(vp->da->vendor);
1480 memcpy(ptr + 2, &lvalue, 4);
1482 if (room > ((unsigned) 255 - ptr[1])) room = 255 - ptr[1];
1484 len = vp2attr_vsa(packet, original, secret, pvp,
1485 vp->da->attr, vp->da->vendor,
1486 ptr + ptr[1], room);
1487 if (len < 0) return len;
1490 if ((fr_debug_flag > 3) && fr_log_fp) {
1491 fprintf(fr_log_fp, "\t\t%02x %02x %02x%02x%02x%02x (%u) ",
1493 ptr[2], ptr[3], ptr[4], ptr[5],
1494 (ptr[3] << 16) | (ptr[4] << 8) | ptr[5]);
1495 print_hex_data(ptr + 6, len, 3);
1506 * @brief Encode an RFC standard attribute 1..255
1508 int rad_vp2rfc(const RADIUS_PACKET *packet,
1509 const RADIUS_PACKET *original,
1510 const char *secret, const VALUE_PAIR **pvp,
1511 uint8_t *ptr, size_t room)
1513 const VALUE_PAIR *vp = *pvp;
1515 if (vp->da->vendor != 0) {
1516 fr_strerror_printf("rad_vp2rfc called with VSA");
1520 if ((vp->da->attr == 0) || (vp->da->attr > 255)) {
1521 fr_strerror_printf("rad_vp2rfc called with non-standard attribute %u", vp->da->attr);
1526 * Only CUI is allowed to have zero length.
1529 if ((vp->length == 0) &&
1530 (vp->da->attr == PW_CHARGEABLE_USER_IDENTITY)) {
1531 ptr[0] = PW_CHARGEABLE_USER_IDENTITY;
1539 * Message-Authenticator is hard-coded.
1541 if (vp->da->attr == PW_MESSAGE_AUTHENTICATOR) {
1542 if (room < 18) return -1;
1545 ptr[0] = PW_MESSAGE_AUTHENTICATOR;
1547 memset(ptr + 2, 0, 16);
1549 if ((fr_debug_flag > 3) && fr_log_fp) {
1550 fprintf(fr_log_fp, "\t\t50 12 ...\n");
1554 *pvp = (*pvp)->next;
1558 return vp2attr_rfc(packet, original, secret, pvp, vp->da->attr,
1562 static ssize_t rad_vp2rfctlv(const RADIUS_PACKET *packet,
1563 const RADIUS_PACKET *original,
1564 const char *secret, const VALUE_PAIR **pvp,
1565 uint8_t *start, size_t room)
1568 const VALUE_PAIR *vp = *pvp;
1570 if (!vp->da->flags.is_tlv) {
1571 fr_strerror_printf("rad_vp2rfctlv: attr is not a TLV");
1575 if ((vp->da->vendor & (FR_MAX_VENDOR - 1)) != 0) {
1576 fr_strerror_printf("rad_vp2rfctlv: attr is not an RFC TLV");
1580 if (room < 5) return 0;
1583 * Encode the first level of TLVs
1585 start[0] = (vp->da->vendor / FR_MAX_VENDOR) & 0xff;
1587 start[2] = vp->da->attr & fr_attr_mask[0];
1590 len = vp2data_any(packet, original, secret, 0, pvp,
1591 start + 4, room - 4);
1592 if (len <= 0) return len;
1595 fprintf(stderr, "SHIT!\n");
1606 * @brief Parse a data structure into a RADIUS attribute.
1608 int rad_vp2attr(const RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1609 const char *secret, const VALUE_PAIR **pvp, uint8_t *start,
1612 const VALUE_PAIR *vp;
1614 if (!pvp || !*pvp || !start || (room <= 2)) return -1;
1619 * RFC format attributes take the fast path.
1621 if (!vp->da->vendor) {
1622 if (vp->da->attr > 255) return 0;
1624 return rad_vp2rfc(packet, original, secret, pvp,
1628 if (vp->da->flags.extended) {
1629 return rad_vp2extended(packet, original, secret, pvp,
1634 * The upper 8 bits of the vendor number are the standard
1635 * space attribute which is a TLV.
1637 if ((vp->da->vendor & (FR_MAX_VENDOR - 1)) == 0) {
1638 return rad_vp2rfctlv(packet, original, secret, pvp,
1642 if (vp->da->flags.wimax) {
1643 return rad_vp2wimax(packet, original, secret, pvp,
1647 return rad_vp2vsa(packet, original, secret, pvp,
1653 * @brief Encode a packet.
1655 int rad_encode(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1658 radius_packet_t *hdr;
1660 uint16_t total_length;
1662 const VALUE_PAIR *reply;
1664 char ip_src_buffer[128];
1665 char ip_dst_buffer[128];
1668 * A 4K packet, aligned on 64-bits.
1670 uint64_t data[MAX_PACKET_LEN / sizeof(uint64_t)];
1672 if ((packet->code > 0) && (packet->code < FR_MAX_PACKET_CODE)) {
1673 what = fr_packet_codes[packet->code];
1678 DEBUG("Sending %s of id %d from %s port %u to %s port %u\n",
1680 inet_ntop(packet->src_ipaddr.af,
1681 &packet->src_ipaddr.ipaddr,
1682 ip_src_buffer, sizeof(ip_src_buffer)),
1684 inet_ntop(packet->dst_ipaddr.af,
1685 &packet->dst_ipaddr.ipaddr,
1686 ip_dst_buffer, sizeof(ip_dst_buffer)),
1690 * Double-check some things based on packet code.
1692 switch (packet->code) {
1693 case PW_AUTHENTICATION_ACK:
1694 case PW_AUTHENTICATION_REJECT:
1695 case PW_ACCESS_CHALLENGE:
1697 fr_strerror_printf("ERROR: Cannot sign response packet without a request packet.");
1703 * These packet vectors start off as all zero.
1705 case PW_ACCOUNTING_REQUEST:
1706 case PW_DISCONNECT_REQUEST:
1707 case PW_COA_REQUEST:
1708 memset(packet->vector, 0, sizeof(packet->vector));
1716 * Use memory on the stack, until we know how
1717 * large the packet will be.
1719 hdr = (radius_packet_t *) data;
1722 * Build standard header
1724 hdr->code = packet->code;
1725 hdr->id = packet->id;
1727 memcpy(hdr->vector, packet->vector, sizeof(hdr->vector));
1729 total_length = AUTH_HDR_LEN;
1732 * Load up the configuration values for the user
1738 * FIXME: Loop twice over the reply list. The first time,
1739 * calculate the total length of data. The second time,
1740 * allocate the memory, and fill in the VP's.
1742 * Hmm... this may be slower than just doing a small
1747 * Loop over the reply attributes for the packet.
1749 reply = packet->vps;
1752 const char *last_name = NULL;
1755 * Ignore non-wire attributes, but allow extended
1758 if ((reply->da->vendor == 0) &&
1759 ((reply->da->attr & 0xFFFF) >= 256) &&
1760 !reply->da->flags.extended && !reply->da->flags.long_extended) {
1763 * Permit the admin to send BADLY formatted
1764 * attributes with a debug build.
1766 if (reply->da->attr == PW_RAW_ATTRIBUTE) {
1767 memcpy(ptr, reply->vp_octets, reply->length);
1768 len = reply->length;
1769 reply = reply->next;
1773 reply = reply->next;
1778 * Set the Message-Authenticator to the correct
1779 * length and initial value.
1781 if (reply->da->attr == PW_MESSAGE_AUTHENTICATOR) {
1783 * Cache the offset to the
1784 * Message-Authenticator
1786 packet->offset = total_length;
1789 last_len = reply->length;
1791 last_name = reply->da->name;
1793 len = rad_vp2attr(packet, original, secret, &reply, ptr,
1794 ((uint8_t *) data) + sizeof(data) - ptr);
1795 if (len < 0) return -1;
1798 * Failed to encode the attribute, likely because
1799 * the packet is full.
1802 if (last_len != 0) {
1803 DEBUG("WARNING: Failed encoding attribute %s\n", last_name);
1805 DEBUG("WARNING: Skipping zero-length attribute %s\n", last_name);
1810 next: /* Used only for Raw-Attribute */
1813 total_length += len;
1814 } /* done looping over all attributes */
1817 * Fill in the rest of the fields, and copy the data over
1818 * from the local stack to the newly allocated memory.
1820 * Yes, all this 'memcpy' is slow, but it means
1821 * that we only allocate the minimum amount of
1822 * memory for a request.
1824 packet->data_len = total_length;
1825 packet->data = (uint8_t *) malloc(packet->data_len);
1826 if (!packet->data) {
1827 fr_strerror_printf("Out of memory");
1831 memcpy(packet->data, hdr, packet->data_len);
1832 hdr = (radius_packet_t *) packet->data;
1834 total_length = htons(total_length);
1835 memcpy(hdr->length, &total_length, sizeof(total_length));
1842 * @brief Sign a previously encoded packet.
1844 int rad_sign(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1847 radius_packet_t *hdr = (radius_packet_t *)packet->data;
1850 * It wasn't assigned an Id, this is bad!
1852 if (packet->id < 0) {
1853 fr_strerror_printf("ERROR: RADIUS packets must be assigned an Id.");
1857 if (!packet->data || (packet->data_len < AUTH_HDR_LEN) ||
1858 (packet->offset < 0)) {
1859 fr_strerror_printf("ERROR: You must call rad_encode() before rad_sign()");
1864 * If there's a Message-Authenticator, update it
1865 * now, BEFORE updating the authentication vector.
1867 if (packet->offset > 0) {
1868 uint8_t calc_auth_vector[AUTH_VECTOR_LEN];
1870 switch (packet->code) {
1871 case PW_ACCOUNTING_RESPONSE:
1872 if (original && original->code == PW_STATUS_SERVER) {
1876 case PW_ACCOUNTING_REQUEST:
1877 case PW_DISCONNECT_REQUEST:
1878 case PW_DISCONNECT_ACK:
1879 case PW_DISCONNECT_NAK:
1880 case PW_COA_REQUEST:
1883 memset(hdr->vector, 0, AUTH_VECTOR_LEN);
1887 case PW_AUTHENTICATION_ACK:
1888 case PW_AUTHENTICATION_REJECT:
1889 case PW_ACCESS_CHALLENGE:
1891 fr_strerror_printf("ERROR: Cannot sign response packet without a request packet.");
1894 memcpy(hdr->vector, original->vector,
1898 default: /* others have vector already set to zero */
1904 * Set the authentication vector to zero,
1905 * calculate the HMAC, and put it
1906 * into the Message-Authenticator
1909 fr_hmac_md5(packet->data, packet->data_len,
1910 (const uint8_t *) secret, strlen(secret),
1912 memcpy(packet->data + packet->offset + 2,
1913 calc_auth_vector, AUTH_VECTOR_LEN);
1916 * Copy the original request vector back
1917 * to the raw packet.
1919 memcpy(hdr->vector, packet->vector, AUTH_VECTOR_LEN);
1923 * Switch over the packet code, deciding how to
1926 switch (packet->code) {
1928 * Request packets are not signed, bur
1929 * have a random authentication vector.
1931 case PW_AUTHENTICATION_REQUEST:
1932 case PW_STATUS_SERVER:
1936 * Reply packets are signed with the
1937 * authentication vector of the request.
1944 fr_MD5Init(&context);
1945 fr_MD5Update(&context, packet->data, packet->data_len);
1946 fr_MD5Update(&context, (const uint8_t *) secret,
1948 fr_MD5Final(digest, &context);
1950 memcpy(hdr->vector, digest, AUTH_VECTOR_LEN);
1951 memcpy(packet->vector, digest, AUTH_VECTOR_LEN);
1954 }/* switch over packet codes */
1960 * @brief Reply to the request. Also attach
1961 * reply attribute value pairs and any user message provided.
1963 int rad_send(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1968 char ip_src_buffer[128];
1969 char ip_dst_buffer[128];
1972 * Maybe it's a fake packet. Don't send it.
1974 if (!packet || (packet->sockfd < 0)) {
1978 if ((packet->code > 0) && (packet->code < FR_MAX_PACKET_CODE)) {
1979 what = fr_packet_codes[packet->code];
1985 * First time through, allocate room for the packet
1987 if (!packet->data) {
1989 * Encode the packet.
1991 if (rad_encode(packet, original, secret) < 0) {
1996 * Re-sign it, including updating the
1997 * Message-Authenticator.
1999 if (rad_sign(packet, original, secret) < 0) {
2004 * If packet->data points to data, then we print out
2005 * the VP list again only for debugging.
2007 } else if (fr_debug_flag) {
2008 DEBUG("Sending %s of id %d from %s port %u to %s port %u\n", what,
2010 inet_ntop(packet->src_ipaddr.af,
2011 &packet->src_ipaddr.ipaddr,
2012 ip_src_buffer, sizeof(ip_src_buffer)),
2014 inet_ntop(packet->dst_ipaddr.af,
2015 &packet->dst_ipaddr.ipaddr,
2016 ip_dst_buffer, sizeof(ip_dst_buffer)),
2019 for (reply = packet->vps; reply; reply = reply->next) {
2020 if ((reply->da->vendor == 0) &&
2021 ((reply->da->attr & 0xFFFF) > 0xff)) continue;
2027 if ((fr_debug_flag > 3) && fr_log_fp) rad_print_hex(packet);
2031 * And send it on it's way.
2033 return rad_sendto(packet->sockfd, packet->data, packet->data_len, 0,
2034 &packet->src_ipaddr, packet->src_port,
2035 &packet->dst_ipaddr, packet->dst_port);
2039 * @brief Do a comparison of two authentication digests by comparing
2042 * Otherwise, the server can be subject to
2043 * timing attacks that allow attackers find a valid message
2046 * http://www.cs.rice.edu/~dwallach/pub/crosby-timing2009.pdf
2048 int rad_digest_cmp(const uint8_t *a, const uint8_t *b, size_t length)
2053 for (i = 0; i < length; i++) {
2054 result |= a[i] ^ b[i];
2057 return result; /* 0 is OK, !0 is !OK, just like memcmp */
2062 * @brief Validates the requesting client NAS. Calculates the
2063 * Request Authenticator based on the clients private key.
2065 static int calc_acctdigest(RADIUS_PACKET *packet, const char *secret)
2067 uint8_t digest[AUTH_VECTOR_LEN];
2071 * Zero out the auth_vector in the received packet.
2072 * Then append the shared secret to the received packet,
2073 * and calculate the MD5 sum. This must be the same
2074 * as the original MD5 sum (packet->vector).
2076 memset(packet->data + 4, 0, AUTH_VECTOR_LEN);
2079 * MD5(packet + secret);
2081 fr_MD5Init(&context);
2082 fr_MD5Update(&context, packet->data, packet->data_len);
2083 fr_MD5Update(&context, (const uint8_t *) secret, strlen(secret));
2084 fr_MD5Final(digest, &context);
2087 * Return 0 if OK, 2 if not OK.
2089 if (rad_digest_cmp(digest, packet->vector, AUTH_VECTOR_LEN) != 0) return 2;
2095 * @brief Validates the requesting client NAS. Calculates the
2096 * Response Authenticator based on the clients private key.
2098 static int calc_replydigest(RADIUS_PACKET *packet, RADIUS_PACKET *original,
2101 uint8_t calc_digest[AUTH_VECTOR_LEN];
2107 if (original == NULL) {
2112 * Copy the original vector in place.
2114 memcpy(packet->data + 4, original->vector, AUTH_VECTOR_LEN);
2117 * MD5(packet + secret);
2119 fr_MD5Init(&context);
2120 fr_MD5Update(&context, packet->data, packet->data_len);
2121 fr_MD5Update(&context, (const uint8_t *) secret, strlen(secret));
2122 fr_MD5Final(calc_digest, &context);
2125 * Copy the packet's vector back to the packet.
2127 memcpy(packet->data + 4, packet->vector, AUTH_VECTOR_LEN);
2130 * Return 0 if OK, 2 if not OK.
2132 if (rad_digest_cmp(packet->vector, calc_digest, AUTH_VECTOR_LEN) != 0) return 2;
2138 * @brief Check if a set of RADIUS formatted TLVs are OK.
2140 int rad_tlv_ok(const uint8_t *data, size_t length,
2141 size_t dv_type, size_t dv_length)
2143 const uint8_t *end = data + length;
2145 if ((dv_length > 2) || (dv_type == 0) || (dv_type > 4)) {
2146 fr_strerror_printf("rad_tlv_ok: Invalid arguments");
2150 while (data < end) {
2153 if ((data + dv_type + dv_length) > end) {
2154 fr_strerror_printf("Attribute header overflow");
2160 if ((data[0] == 0) && (data[1] == 0) &&
2161 (data[2] == 0) && (data[3] == 0)) {
2163 fr_strerror_printf("Invalid attribute 0");
2168 fr_strerror_printf("Invalid attribute > 2^24");
2174 if ((data[0] == 0) && (data[1] == 0)) goto zero;
2178 if (data[0] == 0) goto zero;
2182 fr_strerror_printf("Internal sanity check failed");
2186 switch (dv_length) {
2191 if (data[dv_type + 1] != 0) {
2192 fr_strerror_printf("Attribute is longer than 256 octets");
2197 attrlen = data[dv_type + dv_length - 1];
2202 fr_strerror_printf("Internal sanity check failed");
2206 if (attrlen < (dv_type + dv_length)) {
2207 fr_strerror_printf("Attribute header has invalid length");
2211 if (attrlen > length) {
2212 fr_strerror_printf("Attribute overflows container");
2225 * @brief See if the data pointed to by PTR is a valid RADIUS packet.
2227 * packet is not 'const * const' because we may update data_len,
2228 * if there's more data in the UDP packet than in the RADIUS packet.
2230 int rad_packet_ok(RADIUS_PACKET *packet, int flags)
2235 radius_packet_t *hdr;
2236 char host_ipaddr[128];
2242 * Check for packets smaller than the packet header.
2244 * RFC 2865, Section 3., subsection 'length' says:
2246 * "The minimum length is 20 ..."
2248 if (packet->data_len < AUTH_HDR_LEN) {
2249 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: too short (received %zu < minimum %d)",
2250 inet_ntop(packet->src_ipaddr.af,
2251 &packet->src_ipaddr.ipaddr,
2252 host_ipaddr, sizeof(host_ipaddr)),
2253 packet->data_len, AUTH_HDR_LEN);
2259 * Check for packets with mismatched size.
2260 * i.e. We've received 128 bytes, and the packet header
2261 * says it's 256 bytes long.
2263 totallen = (packet->data[2] << 8) | packet->data[3];
2264 hdr = (radius_packet_t *)packet->data;
2267 * Code of 0 is not understood.
2268 * Code of 16 or greate is not understood.
2270 if ((hdr->code == 0) ||
2271 (hdr->code >= FR_MAX_PACKET_CODE)) {
2272 fr_strerror_printf("WARNING: Bad RADIUS packet from host %s: unknown packet code %d",
2273 inet_ntop(packet->src_ipaddr.af,
2274 &packet->src_ipaddr.ipaddr,
2275 host_ipaddr, sizeof(host_ipaddr)),
2281 * Message-Authenticator is required in Status-Server
2282 * packets, otherwise they can be trivially forged.
2284 if (hdr->code == PW_STATUS_SERVER) require_ma = 1;
2287 * It's also required if the caller asks for it.
2289 if (flags) require_ma = 1;
2292 * Repeat the length checks. This time, instead of
2293 * looking at the data we received, look at the value
2294 * of the 'length' field inside of the packet.
2296 * Check for packets smaller than the packet header.
2298 * RFC 2865, Section 3., subsection 'length' says:
2300 * "The minimum length is 20 ..."
2302 if (totallen < AUTH_HDR_LEN) {
2303 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: too short (length %zu < minimum %d)",
2304 inet_ntop(packet->src_ipaddr.af,
2305 &packet->src_ipaddr.ipaddr,
2306 host_ipaddr, sizeof(host_ipaddr)),
2307 totallen, AUTH_HDR_LEN);
2312 * And again, for the value of the 'length' field.
2314 * RFC 2865, Section 3., subsection 'length' says:
2316 * " ... and maximum length is 4096."
2318 * HOWEVER. This requirement is for the network layer.
2319 * If the code gets here, we assume that a well-formed
2320 * packet is an OK packet.
2322 * We allow both the UDP data length, and the RADIUS
2323 * "length" field to contain up to 64K of data.
2327 * RFC 2865, Section 3., subsection 'length' says:
2329 * "If the packet is shorter than the Length field
2330 * indicates, it MUST be silently discarded."
2332 * i.e. No response to the NAS.
2334 if (packet->data_len < totallen) {
2335 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: received %zu octets, packet length says %zu",
2336 inet_ntop(packet->src_ipaddr.af,
2337 &packet->src_ipaddr.ipaddr,
2338 host_ipaddr, sizeof(host_ipaddr)),
2339 packet->data_len, totallen);
2344 * RFC 2865, Section 3., subsection 'length' says:
2346 * "Octets outside the range of the Length field MUST be
2347 * treated as padding and ignored on reception."
2349 if (packet->data_len > totallen) {
2351 * We're shortening the packet below, but just
2352 * to be paranoid, zero out the extra data.
2354 memset(packet->data + totallen, 0, packet->data_len - totallen);
2355 packet->data_len = totallen;
2359 * Walk through the packet's attributes, ensuring that
2360 * they add up EXACTLY to the size of the packet.
2362 * If they don't, then the attributes either under-fill
2363 * or over-fill the packet. Any parsing of the packet
2364 * is impossible, and will result in unknown side effects.
2366 * This would ONLY happen with buggy RADIUS implementations,
2367 * or with an intentional attack. Either way, we do NOT want
2368 * to be vulnerable to this problem.
2371 count = totallen - AUTH_HDR_LEN;
2376 * We need at least 2 bytes to check the
2380 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: attribute header overflows the packet",
2381 inet_ntop(packet->src_ipaddr.af,
2382 &packet->src_ipaddr.ipaddr,
2383 host_ipaddr, sizeof(host_ipaddr)));
2388 * Attribute number zero is NOT defined.
2391 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: Invalid attribute 0",
2392 inet_ntop(packet->src_ipaddr.af,
2393 &packet->src_ipaddr.ipaddr,
2394 host_ipaddr, sizeof(host_ipaddr)));
2399 * Attributes are at LEAST as long as the ID & length
2400 * fields. Anything shorter is an invalid attribute.
2403 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: attribute %u too short",
2404 inet_ntop(packet->src_ipaddr.af,
2405 &packet->src_ipaddr.ipaddr,
2406 host_ipaddr, sizeof(host_ipaddr)),
2412 * If there are fewer bytes in the packet than in the
2413 * attribute, it's a bad packet.
2415 if (count < attr[1]) {
2416 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: attribute %u data overflows the packet",
2417 inet_ntop(packet->src_ipaddr.af,
2418 &packet->src_ipaddr.ipaddr,
2419 host_ipaddr, sizeof(host_ipaddr)),
2425 * Sanity check the attributes for length.
2428 default: /* don't do anything by default */
2432 * If there's an EAP-Message, we require
2433 * a Message-Authenticator.
2435 case PW_EAP_MESSAGE:
2439 case PW_MESSAGE_AUTHENTICATOR:
2440 if (attr[1] != 2 + AUTH_VECTOR_LEN) {
2441 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: Message-Authenticator has invalid length %d",
2442 inet_ntop(packet->src_ipaddr.af,
2443 &packet->src_ipaddr.ipaddr,
2444 host_ipaddr, sizeof(host_ipaddr)),
2453 * FIXME: Look up the base 255 attributes in the
2454 * dictionary, and switch over their type. For
2455 * integer/date/ip, the attribute length SHOULD
2458 count -= attr[1]; /* grab the attribute length */
2460 num_attributes++; /* seen one more attribute */
2464 * If the attributes add up to a packet, it's allowed.
2466 * If not, we complain, and throw the packet away.
2469 fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: packet attributes do NOT exactly fill the packet",
2470 inet_ntop(packet->src_ipaddr.af,
2471 &packet->src_ipaddr.ipaddr,
2472 host_ipaddr, sizeof(host_ipaddr)));
2477 * If we're configured to look for a maximum number of
2478 * attributes, and we've seen more than that maximum,
2479 * then throw the packet away, as a possible DoS.
2481 if ((fr_max_attributes > 0) &&
2482 (num_attributes > fr_max_attributes)) {
2483 fr_strerror_printf("WARNING: Possible DoS attack from host %s: Too many attributes in request (received %d, max %d are allowed).",
2484 inet_ntop(packet->src_ipaddr.af,
2485 &packet->src_ipaddr.ipaddr,
2486 host_ipaddr, sizeof(host_ipaddr)),
2487 num_attributes, fr_max_attributes);
2492 * http://www.freeradius.org/rfc/rfc2869.html#EAP-Message
2494 * A packet with an EAP-Message attribute MUST also have
2495 * a Message-Authenticator attribute.
2497 * A Message-Authenticator all by itself is OK, though.
2499 * Similarly, Status-Server packets MUST contain
2500 * Message-Authenticator attributes.
2502 if (require_ma && ! seen_ma) {
2503 fr_strerror_printf("WARNING: Insecure packet from host %s: Packet does not contain required Message-Authenticator attribute",
2504 inet_ntop(packet->src_ipaddr.af,
2505 &packet->src_ipaddr.ipaddr,
2506 host_ipaddr, sizeof(host_ipaddr)));
2511 * Fill RADIUS header fields
2513 packet->code = hdr->code;
2514 packet->id = hdr->id;
2515 memcpy(packet->vector, hdr->vector, AUTH_VECTOR_LEN);
2522 * @brief Receive UDP client requests, and fill in
2523 * the basics of a RADIUS_PACKET structure.
2525 RADIUS_PACKET *rad_recv(int fd, int flags)
2529 RADIUS_PACKET *packet;
2532 * Allocate the new request data structure
2534 if ((packet = malloc(sizeof(*packet))) == NULL) {
2535 fr_strerror_printf("out of memory");
2538 memset(packet, 0, sizeof(*packet));
2541 sock_flags = MSG_PEEK;
2545 data_len = rad_recvfrom(fd, &packet->data, sock_flags,
2546 &packet->src_ipaddr, &packet->src_port,
2547 &packet->dst_ipaddr, &packet->dst_port);
2550 * Check for socket errors.
2553 fr_strerror_printf("Error receiving packet: %s", strerror(errno));
2554 /* packet->data is NULL */
2558 packet->data_len = data_len; /* unsigned vs signed */
2561 * If the packet is too big, then rad_recvfrom did NOT
2562 * allocate memory. Instead, it just discarded the
2565 if (packet->data_len > MAX_PACKET_LEN) {
2566 fr_strerror_printf("Discarding packet: Larger than RFC limitation of 4096 bytes.");
2567 /* packet->data is NULL */
2573 * Read no data. Continue.
2574 * This check is AFTER the MAX_PACKET_LEN check above, because
2575 * if the packet is larger than MAX_PACKET_LEN, we also have
2576 * packet->data == NULL
2578 if ((packet->data_len == 0) || !packet->data) {
2579 fr_strerror_printf("Empty packet: Socket is not ready.");
2585 * See if it's a well-formed RADIUS packet.
2587 if (!rad_packet_ok(packet, flags)) {
2593 * Remember which socket we read the packet from.
2595 packet->sockfd = fd;
2598 * FIXME: Do even more filtering by only permitting
2599 * certain IP's. The problem is that we don't know
2600 * how to do this properly for all possible clients...
2604 * Explicitely set the VP list to empty.
2608 if (fr_debug_flag) {
2609 char host_ipaddr[128];
2611 if ((packet->code > 0) && (packet->code < FR_MAX_PACKET_CODE)) {
2612 DEBUG("rad_recv: %s packet from host %s port %d",
2613 fr_packet_codes[packet->code],
2614 inet_ntop(packet->src_ipaddr.af,
2615 &packet->src_ipaddr.ipaddr,
2616 host_ipaddr, sizeof(host_ipaddr)),
2619 DEBUG("rad_recv: Packet from host %s port %d code=%d",
2620 inet_ntop(packet->src_ipaddr.af,
2621 &packet->src_ipaddr.ipaddr,
2622 host_ipaddr, sizeof(host_ipaddr)),
2626 DEBUG(", id=%d, length=%d\n",
2627 packet->id, (int) packet->data_len);
2631 if ((fr_debug_flag > 3) && fr_log_fp) rad_print_hex(packet);
2639 * @brief Verify the Request/Response Authenticator
2640 * (and Message-Authenticator if present) of a packet.
2642 int rad_verify(RADIUS_PACKET *packet, RADIUS_PACKET *original,
2649 if (!packet || !packet->data) return -1;
2652 * Before we allocate memory for the attributes, do more
2655 ptr = packet->data + AUTH_HDR_LEN;
2656 length = packet->data_len - AUTH_HDR_LEN;
2657 while (length > 0) {
2658 uint8_t msg_auth_vector[AUTH_VECTOR_LEN];
2659 uint8_t calc_auth_vector[AUTH_VECTOR_LEN];
2664 default: /* don't do anything. */
2668 * Note that more than one Message-Authenticator
2669 * attribute is invalid.
2671 case PW_MESSAGE_AUTHENTICATOR:
2672 memcpy(msg_auth_vector, &ptr[2], sizeof(msg_auth_vector));
2673 memset(&ptr[2], 0, AUTH_VECTOR_LEN);
2675 switch (packet->code) {
2679 case PW_ACCOUNTING_RESPONSE:
2681 (original->code == PW_STATUS_SERVER)) {
2685 case PW_ACCOUNTING_REQUEST:
2686 case PW_DISCONNECT_REQUEST:
2687 case PW_DISCONNECT_ACK:
2688 case PW_DISCONNECT_NAK:
2689 case PW_COA_REQUEST:
2692 memset(packet->data + 4, 0, AUTH_VECTOR_LEN);
2696 case PW_AUTHENTICATION_ACK:
2697 case PW_AUTHENTICATION_REJECT:
2698 case PW_ACCESS_CHALLENGE:
2700 fr_strerror_printf("ERROR: Cannot validate Message-Authenticator in response packet without a request packet.");
2703 memcpy(packet->data + 4, original->vector, AUTH_VECTOR_LEN);
2707 fr_hmac_md5(packet->data, packet->data_len,
2708 (const uint8_t *) secret, strlen(secret),
2710 if (rad_digest_cmp(calc_auth_vector, msg_auth_vector,
2711 sizeof(calc_auth_vector)) != 0) {
2713 fr_strerror_printf("Received packet from %s with invalid Message-Authenticator! (Shared secret is incorrect.)",
2714 inet_ntop(packet->src_ipaddr.af,
2715 &packet->src_ipaddr.ipaddr,
2716 buffer, sizeof(buffer)));
2717 /* Silently drop packet, according to RFC 3579 */
2719 } /* else the message authenticator was good */
2722 * Reinitialize Authenticators.
2724 memcpy(&ptr[2], msg_auth_vector, AUTH_VECTOR_LEN);
2725 memcpy(packet->data + 4, packet->vector, AUTH_VECTOR_LEN);
2727 } /* switch over the attributes */
2731 } /* loop over the packet, sanity checking the attributes */
2734 * It looks like a RADIUS packet, but we don't know what it is
2735 * so can't validate the authenticators.
2737 if ((packet->code == 0) || (packet->code >= FR_MAX_PACKET_CODE)) {
2739 fr_strerror_printf("Received Unknown packet code %d "
2740 "from client %s port %d: Cannot validate Request/Response Authenticator.",
2742 inet_ntop(packet->src_ipaddr.af,
2743 &packet->src_ipaddr.ipaddr,
2744 buffer, sizeof(buffer)),
2750 * Calculate and/or verify Request or Response Authenticator.
2752 switch(packet->code) {
2756 case PW_AUTHENTICATION_REQUEST:
2757 case PW_STATUS_SERVER:
2759 * The authentication vector is random
2760 * nonsense, invented by the client.
2764 case PW_COA_REQUEST:
2765 case PW_DISCONNECT_REQUEST:
2766 case PW_ACCOUNTING_REQUEST:
2767 if (calc_acctdigest(packet, secret) > 1) {
2768 fr_strerror_printf("Received %s packet "
2769 "from client %s with invalid Request Authenticator! (Shared secret is incorrect.)",
2770 fr_packet_codes[packet->code],
2771 inet_ntop(packet->src_ipaddr.af,
2772 &packet->src_ipaddr.ipaddr,
2773 buffer, sizeof(buffer)));
2778 /* Verify the reply digest */
2779 case PW_AUTHENTICATION_ACK:
2780 case PW_AUTHENTICATION_REJECT:
2781 case PW_ACCESS_CHALLENGE:
2782 case PW_ACCOUNTING_RESPONSE:
2783 case PW_DISCONNECT_ACK:
2784 case PW_DISCONNECT_NAK:
2787 rcode = calc_replydigest(packet, original, secret);
2789 fr_strerror_printf("Received %s packet "
2790 "from home server %s port %d with invalid Response Authenticator! (Shared secret is incorrect.)",
2791 fr_packet_codes[packet->code],
2792 inet_ntop(packet->src_ipaddr.af,
2793 &packet->src_ipaddr.ipaddr,
2794 buffer, sizeof(buffer)),
2801 fr_strerror_printf("Received Unknown packet code %d "
2802 "from client %s port %d: Cannot validate Request/Response Authenticator",
2804 inet_ntop(packet->src_ipaddr.af,
2805 &packet->src_ipaddr.ipaddr,
2806 buffer, sizeof(buffer)),
2815 static ssize_t data2vp(const RADIUS_PACKET *packet,
2816 const RADIUS_PACKET *original,
2818 const DICT_ATTR *da, const uint8_t *start,
2819 const size_t attrlen, const size_t packetlen,
2823 * @brief convert TLVs to one or more VPs
2825 static ssize_t data2vp_tlvs(const RADIUS_PACKET *packet,
2826 const RADIUS_PACKET *original,
2827 const char *secret, const DICT_ATTR *da,
2828 const uint8_t *start, size_t length,
2831 const uint8_t *data = start;
2832 const DICT_ATTR *child;
2833 VALUE_PAIR *head, **tail;
2835 if (length < 3) return -1; /* type, length, value */
2837 VP_HEXDUMP("tlvs", data, length);
2839 if (rad_tlv_ok(data, length, 1, 1) < 0) return -1;
2844 while (data < (start + length)) {
2847 child = dict_attrbyparent(da, data[0], da->vendor);
2849 unsigned int my_attr, my_vendor;
2851 VP_TRACE("Failed to find child %u of TLV %s\n",
2855 * Get child attr/vendor so that
2856 * we can call unknown attr.
2859 my_vendor = da->vendor;
2861 if (!dict_attr_child(da, &my_attr, &my_vendor)) {
2866 child = dict_attrunknown(my_attr, my_vendor, TRUE);
2873 tlv_len = data2vp(packet, original, secret, child,
2874 data + 2, data[1] - 2, data[1] - 2, tail);
2879 tail = &((*tail)->next);
2888 * @brief Convert a top-level VSA to a VP.
2890 * "length" can be LONGER than just this sub-vsa
2892 static ssize_t data2vp_vsa(const RADIUS_PACKET *packet,
2893 const RADIUS_PACKET *original,
2894 const char *secret, DICT_VENDOR *dv,
2895 const uint8_t *data, size_t length,
2898 unsigned int attribute;
2899 ssize_t attrlen, my_len;
2900 const DICT_ATTR *da;
2903 if (length <= (dv->type + dv->length)) {
2904 fr_strerror_printf("data2vp_vsa: Failure to call rad_tlv_ok");
2911 /* data[0] must be zero */
2912 attribute = data[1] << 16;
2913 attribute |= data[2] << 8;
2914 attribute |= data[3];
2918 attribute = data[0] << 8;
2919 attribute |= data[1];
2923 attribute = data[0];
2927 fr_strerror_printf("data2vp_vsa: Internal sanity check failed");
2931 switch (dv->length) {
2933 /* data[dv->type] must be zero, from rad_tlv_ok() */
2934 attrlen = data[dv->type + 1];
2938 attrlen = data[dv->type];
2946 fr_strerror_printf("data2vp_vsa: Internal sanity check failed");
2951 if (attrlen <= (ssize_t) (dv->type + dv->length)) {
2952 fr_strerror_printf("data2vp_vsa: Failure to call rad_tlv_ok");
2958 * See if the VSA is known.
2960 da = dict_attrbyvalue(attribute, dv->vendorpec);
2961 if (!da) da = dict_attrunknown(attribute, dv->vendorpec, TRUE);
2964 my_len = data2vp(packet, original, secret, da,
2965 data + dv->type + dv->length,
2966 attrlen - (dv->type + dv->length),
2967 attrlen - (dv->type + dv->length),
2969 if (my_len < 0) return my_len;
2976 * @brief Convert a Vendor-Specific WIMAX to vps
2978 * Called ONLY for Vendor-Specific
2980 static ssize_t data2vp_wimax(const RADIUS_PACKET *packet,
2981 const RADIUS_PACKET *original,
2982 const char *secret, uint32_t vendor,
2983 const uint8_t *data,
2984 size_t attrlen, size_t packetlen,
2989 uint8_t *head, *tail;
2990 const uint8_t *frag, *end;
2991 const DICT_ATTR *child;
2993 if (attrlen < 8) return -1;
2995 if (((size_t) (data[5] + 4)) != attrlen) return -1;
2997 child = dict_attrbyvalue(data[4], vendor);
2998 if (!child) return -1;
3000 if ((data[6] & 0x80) == 0) {
3001 rcode = data2vp(packet, original, secret, child,
3002 data + 7, data[5] - 3, data[5] - 3,
3004 if (rcode < 0) return -1;
3009 * Calculate the length of all of the fragments. For
3010 * now, they MUST be contiguous in the packet, and they
3011 * MUST be all of the same VSA, WiMAX, and WiMAX-attr.
3013 * The first fragment doesn't have a RADIUS attribute
3014 * header, so it needs to be treated a little special.
3016 fraglen = data[5] - 3;
3017 frag = data + attrlen;
3018 end = data + packetlen;
3020 while (frag < end) {
3021 int last_frag = FALSE;
3024 (frag[0] != PW_VENDOR_SPECIFIC) ||
3025 (frag[1] < 9) || /* too short for wimax */
3026 ((frag + frag[1]) > end) || /* overflow */
3027 (memcmp(frag + 2, data, 4) != 0) || /* not wimax */
3028 (frag[6] != data[4]) || /* not the same wimax attr */
3029 ((frag[7] + 6) != frag[1])) { /* doesn't fill the attr */
3034 last_frag = ((frag[8] & 0x80) == 0);
3036 fraglen += frag[7] - 3;
3040 head = tail = malloc(fraglen);
3041 if (!head) return -1;
3044 * And again, but faster and looser.
3046 * We copy the first fragment, followed by the rest of
3051 memcpy(tail, frag + 4 + 3, frag[4 + 1] - 3);
3052 tail += frag[4 + 1] - 3;
3053 frag += attrlen; /* should be frag[1] - 7 */
3056 * frag now points to RADIUS attributes
3059 memcpy(tail, frag + 2 + 4 + 3, frag[2 + 4 + 1] - 3);
3060 tail += frag[2 + 4 + 1] - 3;
3062 } while (frag < end);
3064 VP_HEXDUMP("wimax fragments", head, fraglen);
3066 rcode = data2vp(packet, original, secret, child,
3067 head, fraglen, fraglen, pvp);
3069 if (rcode < 0) return rcode;
3076 * @brief Convert a top-level VSA to one or more VPs
3078 static ssize_t data2vp_vsas(const RADIUS_PACKET *packet,
3079 const RADIUS_PACKET *original,
3080 const char *secret, const uint8_t *data,
3081 size_t attrlen, size_t packetlen,
3088 VALUE_PAIR *head, **tail;
3090 if (attrlen > packetlen) return -1;
3091 if (attrlen < 5) return -1; /* vid, value */
3092 if (data[0] != 0) return -1; /* we require 24-bit VIDs */
3094 memcpy(&vendor, data, 4);
3095 vendor = ntohl(vendor);
3096 dv = dict_vendorbyvalue(vendor);
3102 if ((vendor == VENDORPEC_WIMAX) && dv->flags) {
3103 rcode = data2vp_wimax(packet, original, secret, vendor,
3104 data, attrlen, packetlen, pvp);
3109 * VSAs should normally be in TLV format.
3111 if (rad_tlv_ok(data + 4, attrlen - 4,
3112 dv->type, dv->length) < 0) return -1;
3115 * There may be more than one VSA in the
3116 * Vendor-Specific. If so, loop over them all.
3125 while (attrlen > 0) {
3128 vsa_len = data2vp_vsa(packet, original, secret, dv,
3129 data, attrlen, tail);
3132 fr_strerror_printf("Internal sanity check %d", __LINE__);
3135 tail = &((*tail)->next);
3138 packetlen -= vsa_len;
3148 * @brief Create any kind of VP from the attribute contents.
3150 * "length" is AT LEAST the length of this attribute, as we
3151 * expect the caller to have verified the data with
3152 * rad_packet_ok(). "length" may be up to the length of the
3155 * @return -1 on error, or "length".
3157 static ssize_t data2vp(const RADIUS_PACKET *packet,
3158 const RADIUS_PACKET *original,
3160 const DICT_ATTR *da, const uint8_t *start,
3161 const size_t attrlen, const size_t packetlen,
3168 const DICT_ATTR *child;
3171 const uint8_t *data = start;
3172 uint8_t buffer[256];
3174 if (!da || (attrlen > 253) || (attrlen > packetlen) ||
3175 (attrlen > 128*1024)) {
3176 fr_strerror_printf("data2vp: invalid arguments");
3180 VP_HEXDUMP("data2vp", start, attrlen);
3182 VP_TRACE("parent %s len %zu ... %zu\n", da->name, attrlen, packetlen);
3187 * Hacks for CUI. The WiMAX spec says that it can be
3188 * zero length, even though this is forbidden by the
3189 * RADIUS specs. So... we make a special case for it.
3192 if (!((da->vendor == 0) &&
3193 (da->attr == PW_CHARGEABLE_USER_IDENTITY))) {
3198 data = (const uint8_t *) "";
3203 * Hacks for tags. If the attribute is capable of
3204 * encoding a tag, and there's room for the tag, and
3205 * there is a tag, or it's encryted with Tunnel-Password,
3206 * then decode the tag.
3208 if (da->flags.has_tag && (datalen > 1) &&
3209 ((data[0] < 0x20) ||
3210 (da->flags.encrypt == FLAG_ENCRYPT_TUNNEL_PASSWORD))) {
3212 if ((da->type == PW_TYPE_STRING) ||
3213 (da->type == PW_TYPE_OCTETS)) {
3214 memcpy(buffer, data + 1, datalen - 1);
3218 memcpy(buffer, data, attrlen);
3227 * Decrypt the attribute.
3229 if (secret && packet && (da->flags.encrypt != FLAG_ENCRYPT_NONE)) {
3230 if (data == start) memcpy(buffer, data, attrlen);
3233 switch (da->flags.encrypt) { /* can't be tagged */
3237 case FLAG_ENCRYPT_USER_PASSWORD:
3239 rad_pwdecode((char *) buffer,
3243 rad_pwdecode((char *) buffer,
3248 datalen = strlen((char *) buffer);
3252 * Tunnel-Password's may go ONLY in response
3253 * packets. They can have a tag, so datalen is
3254 * not the same as attrlen.
3256 case FLAG_ENCRYPT_TUNNEL_PASSWORD:
3257 if (rad_tunnel_pwdecode(buffer, &datalen, secret,
3258 original ? original->vector : nullvector) < 0) {
3264 * Ascend-Send-Secret
3265 * Ascend-Receive-Secret
3267 case FLAG_ENCRYPT_ASCEND_SECRET:
3271 uint8_t my_digest[AUTH_VECTOR_LEN];
3272 make_secret(my_digest,
3275 memcpy(buffer, my_digest,
3277 buffer[AUTH_VECTOR_LEN] = '\0';
3278 datalen = strlen((char *) buffer);
3284 } /* switch over encryption flags */
3288 * Double-check the length after decrypting the
3292 case PW_TYPE_STRING:
3293 case PW_TYPE_OCTETS:
3294 case PW_TYPE_ABINARY:
3297 case PW_TYPE_INTEGER:
3298 case PW_TYPE_IPADDR:
3300 case PW_TYPE_SIGNED:
3301 if (datalen != 4) goto raw;
3304 case PW_TYPE_INTEGER64:
3306 if (datalen != 8) goto raw;
3309 case PW_TYPE_IPV6ADDR:
3310 if (datalen != 16) goto raw;
3313 case PW_TYPE_IPV6PREFIX:
3314 if ((datalen < 2) || (datalen > 18)) goto raw;
3315 if (data[1] > 128) goto raw;
3319 if (datalen != 1) goto raw;
3323 if (datalen != 2) goto raw;
3326 case PW_TYPE_ETHERNET:
3327 if (datalen != 6) goto raw;
3330 case PW_TYPE_COMBO_IP:
3332 child = dict_attrbytype(da->attr, da->vendor,
3334 } else if (datalen == 16) {
3335 child = dict_attrbytype(da->attr, da->vendor,
3340 if (!child) goto raw;
3341 da = child; /* re-write it */
3344 case PW_TYPE_IPV4PREFIX:
3345 if (datalen != 6) goto raw;
3346 if ((data[1] & 0x3f) > 32) goto raw;
3350 * The rest of the data types can cause
3351 * recursion! Ask yourself, "is recursion OK?"
3354 case PW_TYPE_EXTENDED:
3355 if (datalen < 2) goto raw; /* etype, value */
3357 child = dict_attrbyparent(da, data[0], 0);
3358 if (!child) goto raw;
3361 * Recurse to decode the contents, which could be
3362 * a TLV, IPaddr, etc. Note that we decode only
3363 * the current attribute, and we ignore any extra
3366 rcode = data2vp(packet, original, secret, child,
3367 data + 1, attrlen - 1, attrlen - 1, pvp);
3368 if (rcode < 0) goto raw;
3371 case PW_TYPE_LONG_EXTENDED:
3372 if (datalen < 3) goto raw; /* etype, flags, value */
3374 child = dict_attrbyparent(da, data[0], 0);
3375 if (!child) goto raw;
3378 * If there no more fragments, then the contents
3379 * have to be a well-known data type.
3382 if ((data[1] & 0x80) == 0) {
3383 rcode = data2vp(packet, original, secret, child,
3384 data + 2, attrlen - 2, attrlen - 2,
3386 if (rcode < 0) goto raw;
3390 fr_strerror_printf("Internal sanity check %d", __LINE__);
3391 return -1; /* TODO: fixme! */
3394 if (datalen < 6) goto raw; /* vid, vtype, value */
3396 if (data[0] != 0) goto raw; /* we require 24-bit VIDs */
3398 memcpy(&vendor, data, 4);
3399 vendor = ntohl(vendor);
3400 dv = dict_vendorbyvalue(vendor);
3403 child = dict_attrbyparent(da, data[5], vendor);
3404 if (!child) goto raw;
3406 rcode = data2vp(packet, original, secret, child,
3407 data + 5, attrlen - 5, attrlen - 5, pvp);
3408 if (rcode < 0) goto raw;
3413 * We presume that the TLVs all fit into one
3414 * attribute, OR they've already been grouped
3415 * into a contiguous memory buffer.
3417 rcode = data2vp_tlvs(packet, original, secret, da,
3418 data, attrlen, pvp);
3419 if (rcode < 0) goto raw;
3424 * VSAs can be WiMAX, in which case they don't
3425 * fit into one attribute.
3427 rcode = data2vp_vsas(packet, original, secret,
3428 data, attrlen, packetlen, pvp);
3429 if (rcode < 0) goto raw;
3435 * Re-write the attribute to be "raw". It is
3436 * therefore of type "octets", and will be
3439 da = dict_attrunknown(da->attr, da->vendor, TRUE);
3441 fr_strerror_printf("Internal sanity check %d", __LINE__);
3449 * And now that we've verified the basic type
3450 * information, decode the actual data.
3452 vp = pairalloc(NULL, da);
3455 vp->length = datalen;
3459 case PW_TYPE_STRING:
3460 memcpy(vp->vp_strvalue, data, vp->length);
3461 vp->vp_strvalue[vp->length] = '\0';
3464 case PW_TYPE_OCTETS:
3465 case PW_TYPE_ABINARY:
3466 memcpy(vp->vp_octets, data, vp->length);
3470 vp->vp_integer = data[0];
3475 vp->vp_integer = (data[0] << 8) | data[1];
3478 case PW_TYPE_INTEGER:
3479 memcpy(&vp->vp_integer, data, 4);
3480 vp->vp_integer = ntohl(vp->vp_integer);
3483 case PW_TYPE_INTEGER64:
3484 memcpy(&vp->vp_integer64, data, 8);
3485 vp->vp_integer64 = ntohll(vp->vp_integer64);
3489 memcpy(&vp->vp_date, data, 4);
3490 vp->vp_date = ntohl(vp->vp_date);
3494 case PW_TYPE_IPADDR:
3495 memcpy(&vp->vp_ipaddr, data, 4);
3499 memcpy(&vp->vp_ifid, data, 8);
3502 case PW_TYPE_IPV6ADDR:
3503 memcpy(&vp->vp_ipv6addr, data, 16);
3506 case PW_TYPE_IPV6PREFIX:
3508 * FIXME: double-check that
3509 * (vp->vp_octets[1] >> 3) matches vp->length + 2
3511 memcpy(&vp->vp_ipv6prefix, data, vp->length);
3512 if (vp->length < 18) {
3513 memset(((uint8_t *)vp->vp_ipv6prefix) + vp->length, 0,
3518 case PW_TYPE_IPV4PREFIX:
3519 /* FIXME: do the same double-check as for IPv6Prefix */
3520 memcpy(&vp->vp_ipv4prefix, buffer, sizeof(vp->vp_ipv4prefix));
3523 * /32 means "keep all bits". Otherwise, mask
3526 if ((data[1] & 0x3f) > 32) {
3527 uint32_t addr, mask;
3529 memcpy(&addr, vp->vp_octets + 2, sizeof(addr));
3531 mask <<= (32 - (buffer[1] & 0x3f));
3536 memcpy(vp->vp_octets + 2, &addr, sizeof(addr));
3540 case PW_TYPE_SIGNED: /* overloaded with vp_integer */
3541 memcpy(&vp->vp_integer, buffer, 4);
3542 vp->vp_integer = ntohl(vp->vp_integer);
3547 fr_strerror_printf("Internal sanity check %d", __LINE__);
3558 * @brief Create a "normal" VALUE_PAIR from the given data.
3560 ssize_t rad_attr2vp(const RADIUS_PACKET *packet,
3561 const RADIUS_PACKET *original,
3563 const uint8_t *data, size_t length,
3568 const DICT_ATTR *da;
3570 if ((length < 2) || (data[1] < 2) || (data[1] > length)) {
3571 fr_strerror_printf("rad_attr2vp: Insufficient data");
3575 da = dict_attrbyvalue(data[0], 0);
3576 if (!da) da = dict_attrunknown(data[0], 0, TRUE);
3579 * Note that we pass the entire length, not just the
3580 * length of this attribute. The Extended or WiMAX
3581 * attributes may have the "continuation" bit set, and
3582 * will thus be more than one attribute in length.
3584 rcode = data2vp(packet, original, secret, da,
3585 data + 2, data[1] - 2, length - 2, pvp);
3586 if (rcode < 0) return rcode;
3593 * @brief Converts data in network byte order to a VP
3594 * @return -1 on error, or the length of the data read
3596 ssize_t rad_data2vp(unsigned int attribute, unsigned int vendor,
3597 const uint8_t *data, size_t length,
3600 const DICT_ATTR *da;
3602 if (!data || (length == 0) || !pvp) return -1;
3604 da = dict_attrbyvalue(attribute, vendor);
3605 if (!da) da = dict_attrunknown(attribute, vendor, TRUE);
3608 return data2vp(NULL, NULL, NULL, da,
3609 data, length, length, pvp);
3613 * @brief Converts vp_data to network byte order
3614 * @return -1 on error, or the length of the value
3616 ssize_t rad_vp2data(const VALUE_PAIR *vp, uint8_t *out, size_t outlen)
3624 fr_strerror_printf("ERROR: rad_vp2data buffer passed too small");
3629 * Short-circuit it for long attributes.
3631 if ((vp->da->type & PW_FLAG_LONG) != 0) goto do_raw;
3633 switch(vp->da->type) {
3634 case PW_TYPE_STRING:
3635 case PW_TYPE_OCTETS:
3637 case PW_TYPE_IPADDR:
3638 case PW_TYPE_IPV6ADDR:
3639 case PW_TYPE_IPV6PREFIX:
3640 case PW_TYPE_IPV4PREFIX:
3641 case PW_TYPE_ABINARY:
3644 memcpy(out, vp->vp_octets, len);
3647 out[0] = vp->vp_integer & 0xff;
3651 out[0] = (vp->vp_integer >> 8) & 0xff;
3652 out[1] = vp->vp_integer & 0xff;
3655 case PW_TYPE_INTEGER:
3656 lvalue = htonl(vp->vp_integer);
3657 memcpy(out, &lvalue, sizeof(lvalue));
3660 case PW_TYPE_INTEGER64:
3661 lvalue64 = htonll(vp->vp_integer64);
3662 memcpy(out, &lvalue64, sizeof(lvalue64));
3666 lvalue = htonl(vp->vp_date);
3667 memcpy(out, &lvalue, sizeof(lvalue));
3670 case PW_TYPE_SIGNED:
3674 slvalue = htonl(vp->vp_signed);
3675 memcpy(out, &slvalue, sizeof(slvalue));
3678 /* unknown type: ignore it */
3680 fr_strerror_printf("ERROR: Unknown attribute type %d",
3689 * @brief Calculate/check digest, and decode radius attributes.
3690 * @return -1 on decoding error, 0 on success
3692 int rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original,
3698 radius_packet_t *hdr;
3699 VALUE_PAIR *head, **tail, *vp;
3702 * Extract attribute-value pairs
3704 hdr = (radius_packet_t *)packet->data;
3706 packet_length = packet->data_len - AUTH_HDR_LEN;
3713 * Loop over the attributes, decoding them into VPs.
3715 while (packet_length > 0) {
3719 * This may return many VPs
3721 my_len = rad_attr2vp(packet, original, secret,
3722 ptr, packet_length, &vp);
3737 * VSA's may not have been counted properly in
3738 * rad_packet_ok() above, as it is hard to count
3739 * then without using the dictionary. We
3740 * therefore enforce the limits here, too.
3742 if ((fr_max_attributes > 0) &&
3743 (num_attributes > fr_max_attributes)) {
3744 char host_ipaddr[128];
3747 fr_strerror_printf("WARNING: Possible DoS attack from host %s: Too many attributes in request (received %d, max %d are allowed).",
3748 inet_ntop(packet->src_ipaddr.af,
3749 &packet->src_ipaddr.ipaddr,
3750 host_ipaddr, sizeof(host_ipaddr)),
3751 num_attributes, fr_max_attributes);
3756 packet_length -= my_len;
3760 * Merge information from the outside world into our
3763 fr_rand_seed(packet->data, AUTH_HDR_LEN);
3766 * There may be VP's already in the packet. Don't
3767 * destroy them. Instead, add the decoded attributes to
3768 * the tail of the list.
3770 for (tail = &packet->vps; *tail != NULL; tail = &((*tail)->next)) {
3780 * @brief Encode password.
3782 * We assume that the passwd buffer passed is big enough.
3783 * RFC2138 says the password is max 128 chars, so the size
3784 * of the passwd buffer must be at least 129 characters.
3785 * Preferably it's just MAX_STRING_LEN.
3787 * int *pwlen is updated to the new length of the encrypted
3788 * password - a multiple of 16 bytes.
3790 int rad_pwencode(char *passwd, size_t *pwlen, const char *secret,
3791 const uint8_t *vector)
3793 FR_MD5_CTX context, old;
3794 uint8_t digest[AUTH_VECTOR_LEN];
3795 int i, n, secretlen;
3799 * RFC maximum is 128 bytes.
3801 * If length is zero, pad it out with zeros.
3803 * If the length isn't aligned to 16 bytes,
3804 * zero out the extra data.
3808 if (len > 128) len = 128;
3811 memset(passwd, 0, AUTH_PASS_LEN);
3812 len = AUTH_PASS_LEN;
3813 } else if ((len % AUTH_PASS_LEN) != 0) {
3814 memset(&passwd[len], 0, AUTH_PASS_LEN - (len % AUTH_PASS_LEN));
3815 len += AUTH_PASS_LEN - (len % AUTH_PASS_LEN);
3820 * Use the secret to setup the decryption digest
3822 secretlen = strlen(secret);
3824 fr_MD5Init(&context);
3825 fr_MD5Update(&context, (const uint8_t *) secret, secretlen);
3826 old = context; /* save intermediate work */
3829 * Encrypt it in place. Don't bother checking
3830 * len, as we've ensured above that it's OK.
3832 for (n = 0; n < len; n += AUTH_PASS_LEN) {
3834 fr_MD5Update(&context, vector, AUTH_PASS_LEN);
3835 fr_MD5Final(digest, &context);
3838 fr_MD5Update(&context,
3839 (uint8_t *) passwd + n - AUTH_PASS_LEN,
3841 fr_MD5Final(digest, &context);
3844 for (i = 0; i < AUTH_PASS_LEN; i++) {
3845 passwd[i + n] ^= digest[i];
3853 * @brief Decode password.
3855 int rad_pwdecode(char *passwd, size_t pwlen, const char *secret,
3856 const uint8_t *vector)
3858 FR_MD5_CTX context, old;
3859 uint8_t digest[AUTH_VECTOR_LEN];
3861 size_t n, secretlen;
3864 * The RFC's say that the maximum is 128.
3865 * The buffer we're putting it into above is 254, so
3866 * we don't need to do any length checking.
3868 if (pwlen > 128) pwlen = 128;
3873 if (pwlen == 0) goto done;
3876 * Use the secret to setup the decryption digest
3878 secretlen = strlen(secret);
3880 fr_MD5Init(&context);
3881 fr_MD5Update(&context, (const uint8_t *) secret, secretlen);
3882 old = context; /* save intermediate work */
3885 * The inverse of the code above.
3887 for (n = 0; n < pwlen; n += AUTH_PASS_LEN) {
3889 fr_MD5Update(&context, vector, AUTH_VECTOR_LEN);
3890 fr_MD5Final(digest, &context);
3893 if (pwlen > AUTH_PASS_LEN) {
3894 fr_MD5Update(&context, (uint8_t *) passwd,
3898 fr_MD5Final(digest, &context);
3901 if (pwlen > (n + AUTH_PASS_LEN)) {
3902 fr_MD5Update(&context, (uint8_t *) passwd + n,
3907 for (i = 0; i < AUTH_PASS_LEN; i++) {
3908 passwd[i + n] ^= digest[i];
3913 passwd[pwlen] = '\0';
3914 return strlen(passwd);
3919 * @brief Encode Tunnel-Password attributes when sending them out on the wire.
3921 * int *pwlen is updated to the new length of the encrypted
3922 * password - a multiple of 16 bytes.
3924 * This is per RFC-2868 which adds a two char SALT to the initial intermediate
3927 int rad_tunnel_pwencode(char *passwd, size_t *pwlen, const char *secret,
3928 const uint8_t *vector)
3930 uint8_t buffer[AUTH_VECTOR_LEN + MAX_STRING_LEN + 3];
3931 unsigned char digest[AUTH_VECTOR_LEN];
3933 int i, n, secretlen;
3938 if (len > 127) len = 127;
3941 * Shift the password 3 positions right to place a salt and original
3942 * length, tag will be added automatically on packet send
3944 for (n=len ; n>=0 ; n--) passwd[n+3] = passwd[n];
3948 * save original password length as first password character;
3955 * Generate salt. The RFC's say:
3957 * The high bit of salt[0] must be set, each salt in a
3958 * packet should be unique, and they should be random
3960 * So, we set the high bit, add in a counter, and then
3961 * add in some CSPRNG data. should be OK..
3963 salt[0] = (0x80 | ( ((salt_offset++) & 0x0f) << 3) |
3964 (fr_rand() & 0x07));
3965 salt[1] = fr_rand();
3968 * Padd password to multiple of AUTH_PASS_LEN bytes.
3970 n = len % AUTH_PASS_LEN;
3972 n = AUTH_PASS_LEN - n;
3973 for (; n > 0; n--, len++)
3976 /* set new password length */
3980 * Use the secret to setup the decryption digest
3982 secretlen = strlen(secret);
3983 memcpy(buffer, secret, secretlen);
3985 for (n2 = 0; n2 < len; n2+=AUTH_PASS_LEN) {
3987 memcpy(buffer + secretlen, vector, AUTH_VECTOR_LEN);
3988 memcpy(buffer + secretlen + AUTH_VECTOR_LEN, salt, 2);
3989 fr_md5_calc(digest, buffer, secretlen + AUTH_VECTOR_LEN + 2);
3991 memcpy(buffer + secretlen, passwd + n2 - AUTH_PASS_LEN, AUTH_PASS_LEN);
3992 fr_md5_calc(digest, buffer, secretlen + AUTH_PASS_LEN);
3995 for (i = 0; i < AUTH_PASS_LEN; i++) {
3996 passwd[i + n2] ^= digest[i];
4004 * @brief Decode Tunnel-Password encrypted attributes.
4006 * Defined in RFC-2868, this uses a two char SALT along with the
4007 * initial intermediate value, to differentiate it from the
4010 int rad_tunnel_pwdecode(uint8_t *passwd, size_t *pwlen, const char *secret,
4011 const uint8_t *vector)
4013 FR_MD5_CTX context, old;
4014 uint8_t digest[AUTH_VECTOR_LEN];
4016 unsigned i, n, len, reallen;
4021 * We need at least a salt.
4024 fr_strerror_printf("tunnel password is too short");
4029 * There's a salt, but no password. Or, there's a salt
4030 * and a 'data_len' octet. It's wrong, but at least we
4031 * can figure out what it means: the password is empty.
4033 * Note that this means we ignore the 'data_len' field,
4034 * if the attribute length tells us that there's no
4035 * more data. So the 'data_len' field may be wrong,
4044 len -= 2; /* discount the salt */
4047 * Use the secret to setup the decryption digest
4049 secretlen = strlen(secret);
4051 fr_MD5Init(&context);
4052 fr_MD5Update(&context, (const uint8_t *) secret, secretlen);
4053 old = context; /* save intermediate work */
4056 * Set up the initial key:
4058 * b(1) = MD5(secret + vector + salt)
4060 fr_MD5Update(&context, vector, AUTH_VECTOR_LEN);
4061 fr_MD5Update(&context, passwd, 2);
4064 for (n = 0; n < len; n += AUTH_PASS_LEN) {
4068 fr_MD5Final(digest, &context);
4073 * A quick check: decrypt the first octet
4074 * of the password, which is the
4075 * 'data_len' field. Ensure it's sane.
4077 reallen = passwd[2] ^ digest[0];
4078 if (reallen >= len) {
4079 fr_strerror_printf("tunnel password is too long for the attribute");
4083 fr_MD5Update(&context, passwd + 2, AUTH_PASS_LEN);
4087 fr_MD5Final(digest, &context);
4090 fr_MD5Update(&context, passwd + n + 2, AUTH_PASS_LEN);
4093 for (i = base; i < AUTH_PASS_LEN; i++) {
4094 passwd[n + i - 1] = passwd[n + i + 2] ^ digest[i];
4099 * See make_tunnel_password, above.
4101 if (reallen > 239) reallen = 239;
4104 passwd[reallen] = 0;
4110 * @brief Encode a CHAP password
4112 * @bug FIXME: might not work with Ascend because
4113 * we use vp->length, and Ascend gear likes
4114 * to send an extra '\0' in the string!
4116 int rad_chap_encode(RADIUS_PACKET *packet, uint8_t *output, int id,
4117 VALUE_PAIR *password)
4121 uint8_t string[MAX_STRING_LEN * 2 + 1];
4122 VALUE_PAIR *challenge;
4125 * Sanity check the input parameters
4127 if ((packet == NULL) || (password == NULL)) {
4132 * Note that the password VP can be EITHER
4133 * a User-Password attribute (from a check-item list),
4134 * or a CHAP-Password attribute (the client asking
4135 * the library to encode it).
4143 memcpy(ptr, password->vp_strvalue, password->length);
4144 ptr += password->length;
4145 i += password->length;
4148 * Use Chap-Challenge pair if present,
4149 * Request Authenticator otherwise.
4151 challenge = pairfind(packet->vps, PW_CHAP_CHALLENGE, 0, TAG_ANY);
4153 memcpy(ptr, challenge->vp_strvalue, challenge->length);
4154 i += challenge->length;
4156 memcpy(ptr, packet->vector, AUTH_VECTOR_LEN);
4157 i += AUTH_VECTOR_LEN;
4161 fr_md5_calc((uint8_t *)output + 1, (uint8_t *)string, i);
4168 * @brief Seed the random number generator.
4170 * May be called any number of times.
4172 void fr_rand_seed(const void *data, size_t size)
4177 * Ensure that the pool is initialized.
4179 if (!fr_rand_initialized) {
4182 memset(&fr_rand_pool, 0, sizeof(fr_rand_pool));
4184 fd = open("/dev/urandom", O_RDONLY);
4190 while (total < sizeof(fr_rand_pool.randrsl)) {
4191 this = read(fd, fr_rand_pool.randrsl,
4192 sizeof(fr_rand_pool.randrsl) - total);
4193 if ((this < 0) && (errno != EINTR)) break;
4194 if (this > 0) total += this;
4198 fr_rand_pool.randrsl[0] = fd;
4199 fr_rand_pool.randrsl[1] = time(NULL);
4200 fr_rand_pool.randrsl[2] = errno;
4203 fr_randinit(&fr_rand_pool, 1);
4204 fr_rand_pool.randcnt = 0;
4205 fr_rand_initialized = 1;
4211 * Hash the user data
4214 if (!hash) hash = fr_rand();
4215 hash = fr_hash_update(data, size, hash);
4217 fr_rand_pool.randmem[fr_rand_pool.randcnt] ^= hash;
4222 * @brief Return a 32-bit random number.
4224 uint32_t fr_rand(void)
4229 * Ensure that the pool is initialized.
4231 if (!fr_rand_initialized) {
4232 fr_rand_seed(NULL, 0);
4235 num = fr_rand_pool.randrsl[fr_rand_pool.randcnt++];
4236 if (fr_rand_pool.randcnt >= 256) {
4237 fr_rand_pool.randcnt = 0;
4238 fr_isaac(&fr_rand_pool);
4246 * @brief Allocate a new RADIUS_PACKET
4248 RADIUS_PACKET *rad_alloc(int newvector)
4252 if ((rp = malloc(sizeof(RADIUS_PACKET))) == NULL) {
4253 fr_strerror_printf("out of memory");
4256 memset(rp, 0, sizeof(*rp));
4262 uint32_t hash, base;
4265 * Don't expose the actual contents of the random
4269 for (i = 0; i < AUTH_VECTOR_LEN; i += sizeof(uint32_t)) {
4270 hash = fr_rand() ^ base;
4271 memcpy(rp->vector + i, &hash, sizeof(hash));
4274 fr_rand(); /* stir the pool again */
4279 RADIUS_PACKET *rad_alloc_reply(RADIUS_PACKET *packet)
4281 RADIUS_PACKET *reply;
4283 if (!packet) return NULL;
4285 reply = rad_alloc(0);
4286 if (!reply) return NULL;
4289 * Initialize the fields from the request.
4291 reply->sockfd = packet->sockfd;
4292 reply->dst_ipaddr = packet->src_ipaddr;
4293 reply->src_ipaddr = packet->dst_ipaddr;
4294 reply->dst_port = packet->src_port;
4295 reply->src_port = packet->dst_port;
4296 reply->id = packet->id;
4297 reply->code = 0; /* UNKNOWN code */
4298 memcpy(reply->vector, packet->vector,
4299 sizeof(reply->vector));
4302 reply->data_len = 0;
4309 * @brief Free a RADIUS_PACKET
4311 void rad_free(RADIUS_PACKET **radius_packet_ptr)
4313 RADIUS_PACKET *radius_packet;
4315 if (!radius_packet_ptr || !*radius_packet_ptr) return;
4316 radius_packet = *radius_packet_ptr;
4318 free(radius_packet->data);
4320 pairfree(&radius_packet->vps);
4322 free(radius_packet);
4324 *radius_packet_ptr = NULL;