2 * radius.c Functions to send/receive radius packets.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2000-2003,2006 The FreeRADIUS server project
23 #include <freeradius-devel/ident.h>
26 #include <freeradius-devel/autoconf.h>
27 #include <freeradius-devel/md5.h>
39 #include <freeradius-devel/udpfromto.h>
51 #include <freeradius-devel/missing.h>
52 #include <freeradius-devel/libradius.h>
55 * The RFC says 4096 octets max, and most packets are less than 256.
57 #define MAX_PACKET_LEN 4096
60 * The maximum number of attributes which we allow in an incoming
61 * request. If there are more attributes than this, the request
64 * This helps to minimize the potential for a DoS, when an
65 * attacker spoofs Access-Request packets, which don't have a
66 * Message-Authenticator attribute. This means that the packet
67 * is unsigned, and the attacker can use resources on the server,
68 * even if the end request is rejected.
70 int librad_max_attributes = 0;
72 typedef struct radius_packet_t {
76 uint8_t vector[AUTH_VECTOR_LEN];
80 static lrad_randctx lrad_rand_pool; /* across multiple calls */
81 static int lrad_rand_initialized = 0;
82 static unsigned int salt_offset = 0;
85 #define MAX_PACKET_CODE (52)
86 static const char *packet_codes[] = {
92 "Accounting-Response",
108 "Resource-Free-Request",
109 "Resource-Free-Response",
110 "Resource-Query-Request",
111 "Resource-Query-Response",
112 "Alternate-Resource-Reclaim-Request",
113 "NAS-Reboot-Request",
114 "NAS-Reboot-Response",
127 "Disconnect-Request",
137 "IP-Address-Allocate",
143 * Wrapper for sendto which handles sendfromto, IPv6, and all
144 * possible combinations.
146 static int rad_sendto(int sockfd, void *data, size_t data_len, int flags,
147 lrad_ipaddr_t *src_ipaddr, lrad_ipaddr_t *dst_ipaddr,
150 struct sockaddr_storage dst;
151 socklen_t sizeof_dst = sizeof(dst);
153 #ifdef WITH_UDPFROMTO
154 struct sockaddr_storage src;
155 socklen_t sizeof_src = sizeof(src);
157 memset(&src, 0, sizeof(src));
159 memset(&dst, 0, sizeof(dst));
164 if (dst_ipaddr->af == AF_INET) {
165 struct sockaddr_in *s4;
167 s4 = (struct sockaddr_in *)&dst;
168 sizeof_dst = sizeof(struct sockaddr_in);
170 s4->sin_family = AF_INET;
171 s4->sin_addr = dst_ipaddr->ipaddr.ip4addr;
172 s4->sin_port = htons(dst_port);
174 #ifdef WITH_UDPFROMTO
175 s4 = (struct sockaddr_in *)&src;
176 sizeof_src = sizeof(struct sockaddr_in);
178 s4->sin_family = AF_INET;
179 s4->sin_addr = src_ipaddr->ipaddr.ip4addr;
183 * IPv6 MAY be supported.
185 #ifdef HAVE_STRUCT_SOCKADDR_IN6
186 } else if (dst_ipaddr->af == AF_INET6) {
187 struct sockaddr_in6 *s6;
189 s6 = (struct sockaddr_in6 *)&dst;
190 sizeof_dst = sizeof(struct sockaddr_in6);
192 s6->sin6_family = AF_INET6;
193 s6->sin6_addr = dst_ipaddr->ipaddr.ip6addr;
194 s6->sin6_port = htons(dst_port);
196 #ifdef WITH_UDPFROMTO
197 return -1; /* UDPFROMTO && IPv6 are not supported */
199 s6 = (struct sockaddr_in6 *)&src;
200 sizeof_src = sizeof(struct sockaddr_in6);
202 s6->sin6_family = AF_INET6;
203 s6->sin6_addr = src_ipaddr->ipaddr.ip6addr;
205 #endif /* WITH_UDPFROMTO */
206 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
207 } else return -1; /* Unknown address family, Die Die Die! */
209 #ifdef WITH_UDPFROMTO
211 * Only IPv4 is supported for udpfromto.
213 * And if they don't specify a source IP address, don't
216 if ((dst_ipaddr->af == AF_INET) ||
217 (src_ipaddr->af != AF_UNSPEC)) {
218 return sendfromto(sockfd, data, data_len, flags,
219 (struct sockaddr *)&src, sizeof_src,
220 (struct sockaddr *)&dst, sizeof_dst);
223 src_ipaddr = src_ipaddr; /* -Wunused */
227 * No udpfromto, OR an IPv6 socket, fail gracefully.
229 return sendto(sockfd, data, data_len, flags,
230 (struct sockaddr *)&dst, sizeof_dst);
235 * Wrapper for recvfrom, which handles recvfromto, IPv6, and all
236 * possible combinations.
238 static ssize_t rad_recvfrom(int sockfd, uint8_t **pbuf, int flags,
239 lrad_ipaddr_t *src_ipaddr, uint16_t *src_port,
240 lrad_ipaddr_t *dst_ipaddr, uint16_t *dst_port)
242 struct sockaddr_storage src;
243 struct sockaddr_storage dst;
244 socklen_t sizeof_src = sizeof(src);
245 socklen_t sizeof_dst = sizeof(dst);
251 memset(&src, 0, sizeof_src);
252 memset(&dst, 0, sizeof_dst);
255 * Get address family, etc. first, so we know if we
256 * need to do udpfromto.
258 * FIXME: udpfromto also does this, but it's not
259 * a critical problem.
261 if (getsockname(sockfd, (struct sockaddr *)&dst,
262 &sizeof_dst) < 0) return -1;
265 * Read the length of the packet, from the packet.
266 * This lets us allocate the buffer to use for
267 * reading the rest of the packet.
269 data_len = recvfrom(sockfd, header, sizeof(header), MSG_PEEK,
270 (struct sockaddr *)&src, &sizeof_src);
271 if (data_len < 0) return -1;
274 * Too little data is available, discard the packet.
277 recvfrom(sockfd, header, sizeof(header), flags,
278 (struct sockaddr *)&src, &sizeof_src);
281 } else { /* we got 4 bytes of data. */
283 * See how long the packet says it is.
285 len = (header[2] * 256) + header[3];
288 * The length in the packet says it's less than
289 * a RADIUS header length: discard it.
291 if (len < AUTH_HDR_LEN) {
292 recvfrom(sockfd, header, sizeof(header), flags,
293 (struct sockaddr *)&src, &sizeof_src);
297 * Enforce RFC requirements, for sanity.
298 * Anything after 4k will be discarded.
300 } else if (len > MAX_PACKET_LEN) {
301 recvfrom(sockfd, header, sizeof(header), flags,
302 (struct sockaddr *)&src, &sizeof_src);
311 * Receive the packet. The OS will discard any data in the
312 * packet after "len" bytes.
314 #ifdef WITH_UDPFROMTO
315 if (dst.ss_family == AF_INET) {
316 data_len = recvfromto(sockfd, buf, len, flags,
317 (struct sockaddr *)&src, &sizeof_src,
318 (struct sockaddr *)&dst, &sizeof_dst);
322 * No udpfromto, OR an IPv6 socket. Fail gracefully.
324 data_len = recvfrom(sockfd, buf, len, flags,
325 (struct sockaddr *)&src, &sizeof_src);
332 * Check address families, and update src/dst ports, etc.
334 if (src.ss_family == AF_INET) {
335 struct sockaddr_in *s4;
337 s4 = (struct sockaddr_in *)&src;
338 src_ipaddr->af = AF_INET;
339 src_ipaddr->ipaddr.ip4addr = s4->sin_addr;
340 *src_port = ntohs(s4->sin_port);
342 s4 = (struct sockaddr_in *)&dst;
343 dst_ipaddr->af = AF_INET;
344 dst_ipaddr->ipaddr.ip4addr = s4->sin_addr;
345 *dst_port = ntohs(s4->sin_port);
347 #ifdef HAVE_STRUCT_SOCKADDR_IN6
348 } else if (src.ss_family == AF_INET6) {
349 struct sockaddr_in6 *s6;
351 s6 = (struct sockaddr_in6 *)&src;
352 src_ipaddr->af = AF_INET6;
353 src_ipaddr->ipaddr.ip6addr = s6->sin6_addr;
354 *src_port = ntohs(s6->sin6_port);
356 s6 = (struct sockaddr_in6 *)&dst;
357 dst_ipaddr->af = AF_INET6;
358 dst_ipaddr->ipaddr.ip6addr = s6->sin6_addr;
359 *dst_port = ntohs(s6->sin6_port);
363 return -1; /* Unknown address family, Die Die Die! */
367 * Different address families should never happen.
369 if (src.ss_family != dst.ss_family) {
375 * Tell the caller about the data
383 #define AUTH_PASS_LEN (AUTH_VECTOR_LEN)
384 /*************************************************************************
386 * Function: make_secret
388 * Purpose: Build an encrypted secret value to return in a reply
389 * packet. The secret is hidden by xoring with a MD5 digest
390 * created from the shared secret and the authentication
391 * vector. We put them into MD5 in the reverse order from
392 * that used when encrypting passwords to RADIUS.
394 *************************************************************************/
395 static void make_secret(uint8_t *digest, const uint8_t *vector,
396 const char *secret, const uint8_t *value)
398 lrad_MD5_CTX context;
401 lrad_MD5Init(&context);
402 lrad_MD5Update(&context, vector, AUTH_VECTOR_LEN);
403 lrad_MD5Update(&context, secret, strlen(secret));
404 lrad_MD5Final(digest, &context);
406 for ( i = 0; i < AUTH_VECTOR_LEN; i++ ) {
407 digest[i] ^= value[i];
411 #define MAX_PASS_LEN (128)
412 static void make_passwd(uint8_t *output, int *outlen,
413 const uint8_t *input, int inlen,
414 const char *secret, const uint8_t *vector)
416 lrad_MD5_CTX context, old;
417 uint8_t digest[AUTH_VECTOR_LEN];
418 uint8_t passwd[MAX_PASS_LEN];
423 * If the length is zero, round it up.
429 else if (len > MAX_PASS_LEN) len = MAX_PASS_LEN;
431 else if ((len & 0x0f) != 0) {
437 memcpy(passwd, input, len);
438 memset(passwd + len, 0, sizeof(passwd) - len);
440 lrad_MD5Init(&context);
441 lrad_MD5Update(&context, secret, strlen(secret));
447 lrad_MD5Update(&context, vector, AUTH_PASS_LEN);
449 for (n = 0; n < len; n += AUTH_PASS_LEN) {
452 lrad_MD5Update(&context,
453 passwd + n - AUTH_PASS_LEN,
457 lrad_MD5Final(digest, &context);
458 for (i = 0; i < AUTH_PASS_LEN; i++) {
459 passwd[i + n] ^= digest[i];
463 memcpy(output, passwd, len);
466 static void make_tunnel_passwd(uint8_t *output, int *outlen,
467 const uint8_t *input, int inlen, int room,
468 const char *secret, const uint8_t *vector)
470 lrad_MD5_CTX context, old;
471 uint8_t digest[AUTH_VECTOR_LEN];
472 uint8_t passwd[MAX_STRING_LEN + AUTH_VECTOR_LEN];
479 if (room > 253) room = 253;
482 * Account for 2 bytes of the salt, and round the room
483 * available down to the nearest multiple of 16. Then,
484 * subtract one from that to account for the length byte,
485 * and the resulting number is the upper bound on the data
488 * We could short-cut this calculation just be forcing
489 * inlen to be no more than 239. It would work for all
490 * VSA's, as we don't pack multiple VSA's into one
493 * However, this calculation is more general, if a little
494 * complex. And it will work in the future for all possible
495 * kinds of weird attribute packing.
498 room -= (room & 0x0f);
501 if (inlen > room) inlen = room;
504 * Length of the encrypted data is password length plus
505 * one byte for the length of the password.
508 if ((len & 0x0f) != 0) {
512 *outlen = len + 2; /* account for the salt */
515 * Copy the password over.
517 memcpy(passwd + 3, input, inlen);
518 memset(passwd + 3 + inlen, 0, sizeof(passwd) - 3 - inlen);
521 * Generate salt. The RFC's say:
523 * The high bit of salt[0] must be set, each salt in a
524 * packet should be unique, and they should be random
526 * So, we set the high bit, add in a counter, and then
527 * add in some CSPRNG data. should be OK..
529 passwd[0] = (0x80 | ( ((salt_offset++) & 0x0f) << 3) |
530 (lrad_rand() & 0x07));
531 passwd[1] = lrad_rand();
532 passwd[2] = inlen; /* length of the password string */
534 lrad_MD5Init(&context);
535 lrad_MD5Update(&context, secret, strlen(secret));
538 lrad_MD5Update(&context, vector, AUTH_VECTOR_LEN);
539 lrad_MD5Update(&context, &passwd[0], 2);
541 for (n = 0; n < len; n += AUTH_PASS_LEN) {
544 lrad_MD5Update(&context,
545 passwd + 2 + n - AUTH_PASS_LEN,
549 lrad_MD5Final(digest, &context);
550 for (i = 0; i < AUTH_PASS_LEN; i++) {
551 passwd[i + 2 + n] ^= digest[i];
554 memcpy(output, passwd, len + 2);
558 * Hack for IETF RADEXT. Don't use in a production environment!
560 * FIXME: Pack multiple diameter attributes into one
561 * Extended-Attribute!
563 static int vp2diameter(const RADIUS_PACKET *packet,
564 const RADIUS_PACKET *original, const char *secret,
565 const VALUE_PAIR *vp, uint8_t *ptr)
568 uint32_t length = vp->length;
573 *(ptr++) = PW_EXTENDED_ATTRIBUTE;
574 length_ptr = ptr++; /* fill this in later. */
577 attr = vp->attribute;
578 attr &= ~(1 << 31); /* implemented limitations, see dict_addattr */
579 vendor = VENDOR(attr);
580 attr = attr & 0xffff;
583 memcpy(ptr, &attr, sizeof(attr));
586 length += 8; /* includes 8 bytes of attr & length */
587 align = 0; /* no padding */
590 length += 4; /* include 4 bytes of vendor */
593 length = ntohl(length);
594 memcpy(ptr, &length, sizeof(length));
598 vendor = ntohl(vendor);
599 memcpy(ptr, &vendor, sizeof(vendor));
603 length = ntohl(length);
604 memcpy(ptr, &length, sizeof(length));
611 case PW_TYPE_INTEGER:
613 attr = ntohl(vp->lvalue); /* stored in host order */
614 memcpy(ptr, &attr, sizeof(attr));
619 attr = vp->lvalue; /* stored in network order */
620 memcpy(ptr, &attr, sizeof(attr));
625 case PW_TYPE_IPV6ADDR:
626 case PW_TYPE_IPV6PREFIX:
627 case PW_TYPE_ABINARY:
628 memcpy(ptr, vp->vp_strvalue, vp->length);
630 if ((length & 0x03) != 0) align = 4 - (length & 0x03);
634 * Length MAY be larger than can fit into the
635 * encapsulating attribute!
644 if ((length & 0x03) != 0) align = 4 - (length & 0x03);
646 if (total + length > 255) {
648 offset = 255 - total;
650 memcpy(ptr, vp->vp_octets, offset);
652 *(ptr++) = PW_EXTENDED_ATTRIBUTE;
660 memcpy(ptr, vp->vp_octets + offset, length);
666 * Skip to the end of the data.
670 *length_ptr += length;
673 * Align the data to a multiple of 4 bytes.
678 *length_ptr += align;
679 for (i = 0; i < align; i++) {
690 * Parse a data structure into a RADIUS attribute.
692 int rad_vp2attr(const RADIUS_PACKET *packet, const RADIUS_PACKET *original,
693 const char *secret, const VALUE_PAIR *vp, uint8_t *ptr)
696 int offset, len, total_length;
698 uint8_t *length_ptr, *vsa_length_ptr;
699 const uint8_t *data = NULL;
702 vendorcode = total_length = 0;
703 length_ptr = vsa_length_ptr = NULL;
706 * For interoperability, always put vendor attributes
707 * into their own VSA.
709 if ((vendorcode = VENDOR(vp->attribute)) == 0) {
710 *(ptr++) = vp->attribute & 0xFF;
718 DICT_VENDOR *dv = dict_vendorbyvalue(vendorcode);
721 * This must be an RFC-format attribute. If it
722 * wasn't, then the "decode" function would have
723 * made a Vendor-Specific attribute (i.e. type
724 * 26), and we would have "vendorcode == 0" here.
728 vsa_llen = dv->length;
732 * Build a VSA header.
734 *ptr++ = PW_VENDOR_SPECIFIC;
735 vsa_length_ptr = ptr;
737 lvalue = htonl(vendorcode);
738 memcpy(ptr, &lvalue, 4);
744 ptr[0] = (vp->attribute & 0xFF);
748 ptr[0] = ((vp->attribute >> 8) & 0xFF);
749 ptr[1] = (vp->attribute & 0xFF);
755 ptr[2] = ((vp->attribute >> 8) & 0xFF);
756 ptr[3] = (vp->attribute & 0xFF);
760 return 0; /* silently discard it */
766 length_ptr = vsa_length_ptr;
767 vsa_length_ptr = NULL;
776 length_ptr = ptr + 1;
780 return 0; /* silently discard it */
784 total_length += vsa_tlen + vsa_llen;
785 if (vsa_length_ptr) *vsa_length_ptr += vsa_tlen + vsa_llen;
786 *length_ptr += vsa_tlen + vsa_llen;
790 if (vp->flags.has_tag) {
791 if (TAG_VALID(vp->flags.tag)) {
792 ptr[0] = vp->flags.tag & 0xff;
795 } else if (vp->flags.encrypt == FLAG_ENCRYPT_TUNNEL_PASSWORD) {
797 * Tunnel passwords REQUIRE a tag, even
798 * if don't have a valid tag.
802 } /* else don't write a tag */
803 } /* else the attribute doesn't have a tag */
806 * Set up the default sources for the data.
808 data = vp->vp_octets;
815 case PW_TYPE_IPV6ADDR:
816 case PW_TYPE_IPV6PREFIX:
817 case PW_TYPE_ABINARY:
818 /* nothing more to do */
822 len = 1; /* just in case */
823 array[0] = vp->lvalue & 0xff;
830 len = 2; /* just in case */
831 array[0] = (vp->lvalue >> 8) & 0xff;
832 array[1] = vp->lvalue & 0xff;
837 case PW_TYPE_INTEGER:
838 len = 4; /* just in case */
839 lvalue = htonl(vp->lvalue);
840 memcpy(array, &lvalue, sizeof(lvalue));
843 * Perhaps discard the first octet.
845 data = &array[offset];
850 data = (const uint8_t *) &vp->lvalue;
851 len = 4; /* just in case */
855 * There are no tagged date attributes.
858 lvalue = htonl(vp->lvalue);
859 data = (const uint8_t *) &lvalue;
860 len = 4; /* just in case */
863 default: /* unknown type: ignore it */
864 librad_log("ERROR: Unknown attribute type %d", vp->type);
869 * Bound the data to 255 bytes.
871 if (len + offset + total_length > 255) {
872 len = 255 - offset - total_length;
876 * Encrypt the various password styles
878 * Attributes with encrypted values MUST be less than
881 switch (vp->flags.encrypt) {
882 case FLAG_ENCRYPT_USER_PASSWORD:
883 make_passwd(ptr + offset, &len,
885 secret, packet->vector);
888 case FLAG_ENCRYPT_TUNNEL_PASSWORD:
890 librad_log("ERROR: No request packet, cannot encrypt %s attribute in the vp.", vp->name);
895 * Check if 255 - offset - total_length is less
896 * than 18. If so, we can't fit the data into
897 * the available space, and we discard the
900 * This is ONLY a problem if we have multiple VSA's
901 * in one Vendor-Specific, though.
903 if ((255 - offset - total_length) < 18) return 0;
905 make_tunnel_passwd(ptr + offset, &len,
906 data, len, 255 - offset - total_length,
907 secret, original->vector);
911 * The code above ensures that this attribute
914 case FLAG_ENCRYPT_ASCEND_SECRET:
915 make_secret(ptr + offset, packet->vector,
917 len = AUTH_VECTOR_LEN;
923 * Just copy the data over
925 memcpy(ptr + offset, data, len);
927 } /* switch over encryption flags */
930 * Account for the tag (if any).
935 * RFC 2865 section 5 says that zero-length attributes
938 if (len == 0) return 0;
941 * Update the various lengths.
944 if (vsa_length_ptr) *vsa_length_ptr += len;
948 return total_length; /* of attribute */
955 int rad_encode(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
958 radius_packet_t *hdr;
960 uint16_t total_length;
967 * For simplicity in the following logic, we allow
968 * the attributes to "overflow" the 4k maximum
969 * RADIUS packet size, by one attribute.
971 * It's uint32_t, for alignment purposes.
973 uint32_t data[(MAX_PACKET_LEN + 256) / 4];
975 if ((packet->code > 0) && (packet->code < MAX_PACKET_CODE)) {
976 what = packet_codes[packet->code];
981 DEBUG("Sending %s of id %d to %s port %d\n",
983 inet_ntop(packet->dst_ipaddr.af,
984 &packet->dst_ipaddr.ipaddr,
985 ip_buffer, sizeof(ip_buffer)),
989 * Double-check some things based on packet code.
991 switch (packet->code) {
992 case PW_AUTHENTICATION_ACK:
993 case PW_AUTHENTICATION_REJECT:
994 case PW_ACCESS_CHALLENGE:
996 librad_log("ERROR: Cannot sign response packet without a request packet.");
1002 * These packet vectors start off as all zero.
1004 case PW_ACCOUNTING_REQUEST:
1005 case PW_DISCONNECT_REQUEST:
1006 case PW_COA_REQUEST:
1007 memset(packet->vector, 0, sizeof(packet->vector));
1015 * Use memory on the stack, until we know how
1016 * large the packet will be.
1018 hdr = (radius_packet_t *) data;
1021 * Build standard header
1023 hdr->code = packet->code;
1024 hdr->id = packet->id;
1026 memcpy(hdr->vector, packet->vector, sizeof(hdr->vector));
1028 total_length = AUTH_HDR_LEN;
1029 packet->verified = 0;
1032 * Load up the configuration values for the user
1037 * FIXME: Loop twice over the reply list. The first time,
1038 * calculate the total length of data. The second time,
1039 * allocate the memory, and fill in the VP's.
1041 * Hmm... this may be slower than just doing a small
1046 * Loop over the reply attributes for the packet.
1048 for (reply = packet->vps; reply; reply = reply->next) {
1050 * Ignore non-wire attributes
1052 if ((VENDOR(reply->attribute) == 0) &&
1053 ((reply->attribute & 0xFFFF) > 0xff) &&
1054 !reply->flags.diameter) {
1059 * Check that the packet is no more than 4k in
1060 * size, AFTER over-flowing the 4k boundary.
1061 * Note that the 'data' buffer, above, is one
1062 * attribute longer than necessary, in order to
1063 * permit this overflow.
1065 if (total_length > MAX_PACKET_LEN) {
1066 librad_log("ERROR: Too many attributes for packet, result is larger than RFC maximum of 4k");
1071 * Set the Message-Authenticator to the correct
1072 * length and initial value.
1074 if (reply->attribute == PW_MESSAGE_AUTHENTICATOR) {
1075 reply->length = AUTH_VECTOR_LEN;
1076 memset(reply->vp_strvalue, 0, AUTH_VECTOR_LEN);
1077 packet->verified = total_length; /* HACK! */
1081 * Check for overflow.
1083 if (reply->flags.diameter) {
1084 len = reply->length;
1087 librad_log("ERROR: Too many attributes for packet, result is larger than RFC maximum of 4k");
1091 len += 8; /* Code, length */
1092 if (VENDOR(reply->attribute) != 0) len += 4;
1093 if ((len & 0x03) != 0) len += 4 - (len & 0x03);
1095 if (len > 253) len += (len / 253) * 2;
1097 if ((total_length + len) > MAX_PACKET_LEN) {
1098 librad_log("ERROR: Too many attributes for packet, result is larger than RFC maximum of 4k");
1104 len = vp2diameter(packet, original, secret, reply, ptr);
1107 * Print out ONLY the attributes which
1108 * we're sending over the wire, and print
1109 * them out BEFORE they're encrypted.
1113 len = rad_vp2attr(packet, original, secret, reply, ptr);
1116 if (len < 0) return -1;
1118 total_length += len;
1119 } /* done looping over all attributes */
1122 * Fill in the rest of the fields, and copy the data over
1123 * from the local stack to the newly allocated memory.
1125 * Yes, all this 'memcpy' is slow, but it means
1126 * that we only allocate the minimum amount of
1127 * memory for a request.
1129 packet->data_len = total_length;
1130 packet->data = (uint8_t *) malloc(packet->data_len);
1131 if (!packet->data) {
1132 librad_log("Out of memory");
1136 memcpy(packet->data, data, packet->data_len);
1137 hdr = (radius_packet_t *) packet->data;
1139 total_length = htons(total_length);
1140 memcpy(hdr->length, &total_length, sizeof(total_length));
1147 * Sign a previously encoded packet.
1149 int rad_sign(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1152 radius_packet_t *hdr = (radius_packet_t *)packet->data;
1155 * It wasn't assigned an Id, this is bad!
1157 if (packet->id < 0) {
1158 librad_log("ERROR: RADIUS packets must be assigned an Id.");
1162 if (!packet->data || (packet->data_len < AUTH_HDR_LEN) ||
1163 (packet->verified < 0)) {
1164 librad_log("ERROR: You must call rad_encode() before rad_sign()");
1169 * If there's a Message-Authenticator, update it
1170 * now, BEFORE updating the authentication vector.
1174 if (packet->verified > 0) {
1175 uint8_t calc_auth_vector[AUTH_VECTOR_LEN];
1177 switch (packet->code) {
1178 case PW_ACCOUNTING_REQUEST:
1179 case PW_ACCOUNTING_RESPONSE:
1180 case PW_DISCONNECT_REQUEST:
1181 case PW_DISCONNECT_ACK:
1182 case PW_DISCONNECT_NAK:
1183 case PW_COA_REQUEST:
1186 memset(hdr->vector, 0, AUTH_VECTOR_LEN);
1189 case PW_AUTHENTICATION_ACK:
1190 case PW_AUTHENTICATION_REJECT:
1191 case PW_ACCESS_CHALLENGE:
1193 librad_log("ERROR: Cannot sign response packet without a request packet.");
1196 memcpy(hdr->vector, original->vector,
1200 default: /* others have vector already set to zero */
1206 * Set the authentication vector to zero,
1207 * calculate the signature, and put it
1208 * into the Message-Authenticator
1211 lrad_hmac_md5(packet->data, packet->data_len,
1212 secret, strlen(secret),
1214 memcpy(packet->data + packet->verified + 2,
1215 calc_auth_vector, AUTH_VECTOR_LEN);
1218 * Copy the original request vector back
1219 * to the raw packet.
1221 memcpy(hdr->vector, packet->vector, AUTH_VECTOR_LEN);
1225 * Switch over the packet code, deciding how to
1228 switch (packet->code) {
1230 * Request packets are not signed, bur
1231 * have a random authentication vector.
1233 case PW_AUTHENTICATION_REQUEST:
1234 case PW_STATUS_SERVER:
1238 * Reply packets are signed with the
1239 * authentication vector of the request.
1247 MD5Update(&context, packet->data, packet->data_len);
1248 MD5Update(&context, secret, strlen(secret));
1249 MD5Final(digest, &context);
1251 memcpy(hdr->vector, digest, AUTH_VECTOR_LEN);
1252 memcpy(packet->vector, digest, AUTH_VECTOR_LEN);
1255 }/* switch over packet codes */
1261 * Reply to the request. Also attach
1262 * reply attribute value pairs and any user message provided.
1264 int rad_send(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1269 char ip_buffer[128];
1272 * Maybe it's a fake packet. Don't send it.
1274 if (!packet || (packet->sockfd < 0)) {
1278 if ((packet->code > 0) && (packet->code < MAX_PACKET_CODE)) {
1279 what = packet_codes[packet->code];
1285 * First time through, allocate room for the packet
1287 if (!packet->data) {
1289 * Encode the packet.
1291 if (rad_encode(packet, original, secret) < 0) {
1296 * Re-sign it, including updating the
1297 * Message-Authenticator.
1299 if (rad_sign(packet, original, secret) < 0) {
1304 * If packet->data points to data, then we print out
1305 * the VP list again only for debugging.
1307 } else if (librad_debug) {
1308 DEBUG("Re-sending %s of id %d to %s port %d\n", what, packet->id,
1309 inet_ntop(packet->dst_ipaddr.af,
1310 &packet->dst_ipaddr.ipaddr,
1311 ip_buffer, sizeof(ip_buffer)),
1314 for (reply = packet->vps; reply; reply = reply->next) {
1315 /* FIXME: ignore attributes > 0xff */
1321 * And send it on it's way.
1323 return rad_sendto(packet->sockfd, packet->data, packet->data_len, 0,
1324 &packet->src_ipaddr, &packet->dst_ipaddr,
1330 * Validates the requesting client NAS. Calculates the
1331 * signature based on the clients private key.
1333 static int calc_acctdigest(RADIUS_PACKET *packet, const char *secret)
1335 uint8_t digest[AUTH_VECTOR_LEN];
1339 * Older clients have the authentication vector set to
1340 * all zeros. Return `1' in that case.
1342 memset(digest, 0, sizeof(digest));
1343 if (memcmp(packet->vector, digest, AUTH_VECTOR_LEN) == 0) {
1344 packet->verified = 1;
1349 * Zero out the auth_vector in the received packet.
1350 * Then append the shared secret to the received packet,
1351 * and calculate the MD5 sum. This must be the same
1352 * as the original MD5 sum (packet->vector).
1354 memset(packet->data + 4, 0, AUTH_VECTOR_LEN);
1357 * MD5(packet + secret);
1360 MD5Update(&context, packet->data, packet->data_len);
1361 MD5Update(&context, secret, strlen(secret));
1362 MD5Final(digest, &context);
1365 * Return 0 if OK, 2 if not OK.
1368 memcmp(digest, packet->vector, AUTH_VECTOR_LEN) ? 2 : 0;
1370 return packet->verified;
1374 * Validates the requesting client NAS. Calculates the
1375 * signature based on the clients private key.
1377 static int calc_replydigest(RADIUS_PACKET *packet, RADIUS_PACKET *original,
1380 uint8_t calc_digest[AUTH_VECTOR_LEN];
1386 if (original == NULL) {
1391 * Copy the original vector in place.
1393 memcpy(packet->data + 4, original->vector, AUTH_VECTOR_LEN);
1396 * MD5(packet + secret);
1399 MD5Update(&context, packet->data, packet->data_len);
1400 MD5Update(&context, secret, strlen(secret));
1401 MD5Final(calc_digest, &context);
1404 * Copy the packet's vector back to the packet.
1406 memcpy(packet->data + 4, packet->vector, AUTH_VECTOR_LEN);
1409 * Return 0 if OK, 2 if not OK.
1412 memcmp(packet->vector, calc_digest, AUTH_VECTOR_LEN) ? 2 : 0;
1413 return packet->verified;
1418 * See if the data pointed to by PTR is a valid RADIUS packet.
1420 * packet is not 'const * const' because we may update data_len,
1421 * if there's more data in the UDP packet than in the RADIUS packet.
1423 int rad_packet_ok(RADIUS_PACKET *packet)
1428 radius_packet_t *hdr;
1429 char host_ipaddr[128];
1434 * Check for packets smaller than the packet header.
1436 * RFC 2865, Section 3., subsection 'length' says:
1438 * "The minimum length is 20 ..."
1440 if (packet->data_len < AUTH_HDR_LEN) {
1441 librad_log("WARNING: Malformed RADIUS packet from host %s: too short (received %d < minimum %d)",
1442 inet_ntop(packet->src_ipaddr.af,
1443 &packet->src_ipaddr.ipaddr,
1444 host_ipaddr, sizeof(host_ipaddr)),
1445 packet->data_len, AUTH_HDR_LEN);
1450 * RFC 2865, Section 3., subsection 'length' says:
1452 * " ... and maximum length is 4096."
1454 if (packet->data_len > MAX_PACKET_LEN) {
1455 librad_log("WARNING: Malformed RADIUS packet from host %s: too long (received %d > maximum %d)",
1456 inet_ntop(packet->src_ipaddr.af,
1457 &packet->src_ipaddr.ipaddr,
1458 host_ipaddr, sizeof(host_ipaddr)),
1459 packet->data_len, MAX_PACKET_LEN);
1464 * Check for packets with mismatched size.
1465 * i.e. We've received 128 bytes, and the packet header
1466 * says it's 256 bytes long.
1468 totallen = (packet->data[2] << 8) | packet->data[3];
1469 hdr = (radius_packet_t *)packet->data;
1472 * Code of 0 is not understood.
1473 * Code of 16 or greate is not understood.
1475 if ((hdr->code == 0) ||
1476 (hdr->code >= MAX_PACKET_CODE)) {
1477 librad_log("WARNING: Bad RADIUS packet from host %s: unknown packet code %d",
1478 inet_ntop(packet->src_ipaddr.af,
1479 &packet->src_ipaddr.ipaddr,
1480 host_ipaddr, sizeof(host_ipaddr)),
1486 * Repeat the length checks. This time, instead of
1487 * looking at the data we received, look at the value
1488 * of the 'length' field inside of the packet.
1490 * Check for packets smaller than the packet header.
1492 * RFC 2865, Section 3., subsection 'length' says:
1494 * "The minimum length is 20 ..."
1496 if (totallen < AUTH_HDR_LEN) {
1497 librad_log("WARNING: Malformed RADIUS packet from host %s: too short (length %d < minimum %d)",
1498 inet_ntop(packet->src_ipaddr.af,
1499 &packet->src_ipaddr.ipaddr,
1500 host_ipaddr, sizeof(host_ipaddr)),
1501 totallen, AUTH_HDR_LEN);
1506 * And again, for the value of the 'length' field.
1508 * RFC 2865, Section 3., subsection 'length' says:
1510 * " ... and maximum length is 4096."
1512 if (totallen > MAX_PACKET_LEN) {
1513 librad_log("WARNING: Malformed RADIUS packet from host %s: too long (length %d > maximum %d)",
1514 inet_ntop(packet->src_ipaddr.af,
1515 &packet->src_ipaddr.ipaddr,
1516 host_ipaddr, sizeof(host_ipaddr)),
1517 totallen, MAX_PACKET_LEN);
1522 * RFC 2865, Section 3., subsection 'length' says:
1524 * "If the packet is shorter than the Length field
1525 * indicates, it MUST be silently discarded."
1527 * i.e. No response to the NAS.
1529 if (packet->data_len < totallen) {
1530 librad_log("WARNING: Malformed RADIUS packet from host %s: received %d octets, packet length says %d",
1531 inet_ntop(packet->src_ipaddr.af,
1532 &packet->src_ipaddr.ipaddr,
1533 host_ipaddr, sizeof(host_ipaddr)),
1534 packet->data_len, totallen);
1539 * RFC 2865, Section 3., subsection 'length' says:
1541 * "Octets outside the range of the Length field MUST be
1542 * treated as padding and ignored on reception."
1544 if (packet->data_len > totallen) {
1546 * We're shortening the packet below, but just
1547 * to be paranoid, zero out the extra data.
1549 memset(packet->data + totallen, 0, packet->data_len - totallen);
1550 packet->data_len = totallen;
1554 * Walk through the packet's attributes, ensuring that
1555 * they add up EXACTLY to the size of the packet.
1557 * If they don't, then the attributes either under-fill
1558 * or over-fill the packet. Any parsing of the packet
1559 * is impossible, and will result in unknown side effects.
1561 * This would ONLY happen with buggy RADIUS implementations,
1562 * or with an intentional attack. Either way, we do NOT want
1563 * to be vulnerable to this problem.
1566 count = totallen - AUTH_HDR_LEN;
1572 * Attribute number zero is NOT defined.
1575 librad_log("WARNING: Malformed RADIUS packet from host %s: Invalid attribute 0",
1576 inet_ntop(packet->src_ipaddr.af,
1577 &packet->src_ipaddr.ipaddr,
1578 host_ipaddr, sizeof(host_ipaddr)));
1583 * Attributes are at LEAST as long as the ID & length
1584 * fields. Anything shorter is an invalid attribute.
1587 librad_log("WARNING: Malformed RADIUS packet from host %s: attribute %d too short",
1588 inet_ntop(packet->src_ipaddr.af,
1589 &packet->src_ipaddr.ipaddr,
1590 host_ipaddr, sizeof(host_ipaddr)),
1596 * Sanity check the attributes for length.
1599 default: /* don't do anything by default */
1602 case PW_EAP_MESSAGE:
1603 seen_eap |= PW_EAP_MESSAGE;
1606 case PW_MESSAGE_AUTHENTICATOR:
1607 if (attr[1] != 2 + AUTH_VECTOR_LEN) {
1608 librad_log("WARNING: Malformed RADIUS packet from host %s: Message-Authenticator has invalid length %d",
1609 inet_ntop(packet->src_ipaddr.af,
1610 &packet->src_ipaddr.ipaddr,
1611 host_ipaddr, sizeof(host_ipaddr)),
1615 seen_eap |= PW_MESSAGE_AUTHENTICATOR;
1620 * FIXME: Look up the base 255 attributes in the
1621 * dictionary, and switch over their type. For
1622 * integer/date/ip, the attribute length SHOULD
1625 count -= attr[1]; /* grab the attribute length */
1627 num_attributes++; /* seen one more attribute */
1631 * If the attributes add up to a packet, it's allowed.
1633 * If not, we complain, and throw the packet away.
1636 librad_log("WARNING: Malformed RADIUS packet from host %s: packet attributes do NOT exactly fill the packet",
1637 inet_ntop(packet->src_ipaddr.af,
1638 &packet->src_ipaddr.ipaddr,
1639 host_ipaddr, sizeof(host_ipaddr)));
1644 * If we're configured to look for a maximum number of
1645 * attributes, and we've seen more than that maximum,
1646 * then throw the packet away, as a possible DoS.
1648 if ((librad_max_attributes > 0) &&
1649 (num_attributes > librad_max_attributes)) {
1650 librad_log("WARNING: Possible DoS attack from host %s: Too many attributes in request (received %d, max %d are allowed).",
1651 inet_ntop(packet->src_ipaddr.af,
1652 &packet->src_ipaddr.ipaddr,
1653 host_ipaddr, sizeof(host_ipaddr)),
1654 num_attributes, librad_max_attributes);
1659 * http://www.freeradius.org/rfc/rfc2869.html#EAP-Message
1661 * A packet with an EAP-Message attribute MUST also have
1662 * a Message-Authenticator attribute.
1664 * A Message-Authenticator all by itself is OK, though.
1667 (seen_eap != PW_MESSAGE_AUTHENTICATOR) &&
1668 (seen_eap != (PW_EAP_MESSAGE | PW_MESSAGE_AUTHENTICATOR))) {
1669 librad_log("WARNING: Insecure packet from host %s: Received EAP-Message with no Message-Authenticator.",
1670 inet_ntop(packet->src_ipaddr.af,
1671 &packet->src_ipaddr.ipaddr,
1672 host_ipaddr, sizeof(host_ipaddr)));
1677 * Fill RADIUS header fields
1679 packet->code = hdr->code;
1680 packet->id = hdr->id;
1681 memcpy(packet->vector, hdr->vector, AUTH_VECTOR_LEN);
1688 * Receive UDP client requests, and fill in
1689 * the basics of a RADIUS_PACKET structure.
1691 RADIUS_PACKET *rad_recv(int fd)
1693 RADIUS_PACKET *packet;
1696 * Allocate the new request data structure
1698 if ((packet = malloc(sizeof(*packet))) == NULL) {
1699 librad_log("out of memory");
1702 memset(packet, 0, sizeof(*packet));
1704 packet->data_len = rad_recvfrom(fd, &packet->data, 0,
1705 &packet->src_ipaddr, &packet->src_port,
1706 &packet->dst_ipaddr, &packet->dst_port);
1709 * Check for socket errors.
1711 if (packet->data_len < 0) {
1712 librad_log("Error receiving packet: %s", strerror(errno));
1713 /* packet->data is NULL */
1719 * If the packet is too big, then rad_recvfrom did NOT
1720 * allocate memory. Instead, it just discarded the
1723 if (packet->data_len > MAX_PACKET_LEN) {
1724 librad_log("Discarding packet: Larger than RFC limitation of 4096 bytes.");
1725 /* packet->data is NULL */
1731 * Read no data. Continue.
1732 * This check is AFTER the MAX_PACKET_LEN check above, because
1733 * if the packet is larger than MAX_PACKET_LEN, we also have
1734 * packet->data == NULL
1736 if ((packet->data_len == 0) || !packet->data) {
1737 librad_log("No data.");
1743 * See if it's a well-formed RADIUS packet.
1745 if (!rad_packet_ok(packet)) {
1751 * Remember which socket we read the packet from.
1753 packet->sockfd = fd;
1756 * FIXME: Do even more filtering by only permitting
1757 * certain IP's. The problem is that we don't know
1758 * how to do this properly for all possible clients...
1762 * Explicitely set the VP list to empty.
1767 char host_ipaddr[128];
1769 if ((packet->code > 0) && (packet->code < MAX_PACKET_CODE)) {
1770 printf("rad_recv: %s packet from host %s port %d",
1771 packet_codes[packet->code],
1772 inet_ntop(packet->src_ipaddr.af,
1773 &packet->src_ipaddr.ipaddr,
1774 host_ipaddr, sizeof(host_ipaddr)),
1777 printf("rad_recv: Packet from host %s port %d code=%d",
1778 inet_ntop(packet->src_ipaddr.af,
1779 &packet->src_ipaddr.ipaddr,
1780 host_ipaddr, sizeof(host_ipaddr)),
1784 printf(", id=%d, length=%d\n", packet->id, packet->data_len);
1792 * Verify the signature of a packet.
1794 int rad_verify(RADIUS_PACKET *packet, RADIUS_PACKET *original,
1801 if (!packet || !packet->data) return -1;
1804 * Before we allocate memory for the attributes, do more
1807 ptr = packet->data + AUTH_HDR_LEN;
1808 length = packet->data_len - AUTH_HDR_LEN;
1809 while (length > 0) {
1810 uint8_t msg_auth_vector[AUTH_VECTOR_LEN];
1811 uint8_t calc_auth_vector[AUTH_VECTOR_LEN];
1816 default: /* don't do anything. */
1820 * Note that more than one Message-Authenticator
1821 * attribute is invalid.
1823 case PW_MESSAGE_AUTHENTICATOR:
1824 memcpy(msg_auth_vector, &ptr[2], sizeof(msg_auth_vector));
1825 memset(&ptr[2], 0, AUTH_VECTOR_LEN);
1827 switch (packet->code) {
1831 case PW_ACCOUNTING_REQUEST:
1832 case PW_ACCOUNTING_RESPONSE:
1833 case PW_DISCONNECT_REQUEST:
1834 case PW_DISCONNECT_ACK:
1835 case PW_DISCONNECT_NAK:
1836 case PW_COA_REQUEST:
1839 memset(packet->data + 4, 0, AUTH_VECTOR_LEN);
1842 case PW_AUTHENTICATION_ACK:
1843 case PW_AUTHENTICATION_REJECT:
1844 case PW_ACCESS_CHALLENGE:
1846 librad_log("ERROR: Cannot validate Message-Authenticator in response packet without a request packet.");
1849 memcpy(packet->data + 4, original->vector, AUTH_VECTOR_LEN);
1853 lrad_hmac_md5(packet->data, packet->data_len,
1854 secret, strlen(secret), calc_auth_vector);
1855 if (memcmp(calc_auth_vector, msg_auth_vector,
1856 sizeof(calc_auth_vector)) != 0) {
1858 librad_log("Received packet from %s with invalid Message-Authenticator! (Shared secret is incorrect.)",
1859 inet_ntop(packet->src_ipaddr.af,
1860 &packet->src_ipaddr.ipaddr,
1861 buffer, sizeof(buffer)));
1862 /* Silently drop packet, according to RFC 3579 */
1864 } /* else the message authenticator was good */
1867 * Reinitialize Authenticators.
1869 memcpy(&ptr[2], msg_auth_vector, AUTH_VECTOR_LEN);
1870 memcpy(packet->data + 4, packet->vector, AUTH_VECTOR_LEN);
1872 } /* switch over the attributes */
1876 } /* loop over the packet, sanity checking the attributes */
1879 * It looks like a RADIUS packet, but we can't validate
1882 if ((packet->code == 0) || packet->code >= MAX_PACKET_CODE) {
1884 librad_log("Received Unknown packet code %d"
1885 "from client %s port %d: Cannot validate signature",
1887 inet_ntop(packet->src_ipaddr.af,
1888 &packet->src_ipaddr.ipaddr,
1889 buffer, sizeof(buffer)),
1895 * Calculate and/or verify digest.
1897 switch(packet->code) {
1901 case PW_AUTHENTICATION_REQUEST:
1902 case PW_STATUS_SERVER:
1903 case PW_DISCONNECT_REQUEST:
1905 * The authentication vector is random
1906 * nonsense, invented by the client.
1910 case PW_ACCOUNTING_REQUEST:
1911 if (calc_acctdigest(packet, secret) > 1) {
1912 librad_log("Received Accounting-Request packet "
1913 "from %s with invalid signature! (Shared secret is incorrect.)",
1914 inet_ntop(packet->src_ipaddr.af,
1915 &packet->src_ipaddr.ipaddr,
1916 buffer, sizeof(buffer)));
1921 /* Verify the reply digest */
1922 case PW_AUTHENTICATION_ACK:
1923 case PW_AUTHENTICATION_REJECT:
1924 case PW_ACCESS_CHALLENGE:
1925 case PW_ACCOUNTING_RESPONSE:
1926 case PW_DISCONNECT_ACK:
1927 case PW_DISCONNECT_NAK:
1930 rcode = calc_replydigest(packet, original, secret);
1932 librad_log("Received %s packet "
1933 "from client %s port %d with invalid signature (err=%d)! (Shared secret is incorrect.)",
1934 packet_codes[packet->code],
1935 inet_ntop(packet->src_ipaddr.af,
1936 &packet->src_ipaddr.ipaddr,
1937 buffer, sizeof(buffer)),
1945 librad_log("Received Unknown packet code %d"
1946 "from client %s port %d: Cannot validate signature",
1948 inet_ntop(packet->src_ipaddr.af,
1949 &packet->src_ipaddr.ipaddr,
1950 buffer, sizeof(buffer)),
1960 * Hack for IETF RADEXT. Don't use in a production environment.
1962 * Note that due to architecture limitations, we can't handle
1963 * AVP's with more than 253 bytes of data, but that should be
1964 * enough for initial inter-operability.
1966 static int diameter2vp(const RADIUS_PACKET *packet,
1967 const RADIUS_PACKET *original, const char *secret,
1968 const uint8_t *data, int total_length,
1971 int lookup, offset, radius_length, attr_length, diameter_length, raw;
1972 uint32_t attr, length, vendor;
1974 VALUE_PAIR *head, **tail, *vp;
1975 uint8_t *header; /* diameter header */
1976 uint8_t diameter[4096];
1978 diameter_length = radius_length = 0;
1984 * Unpack all contiguous Extended-Attributes into a local
1985 * buffer. It's slow, but it's safe.
1987 while (total_length > 0) {
1988 if (data[0] != PW_EXTENDED_ATTRIBUTE) break;
1990 attr_length = data[1];
1991 radius_length += attr_length;
1992 total_length -= attr_length;
1995 memcpy(header, data + 2, attr_length);
1996 header += attr_length;
1997 diameter_length += attr_length;
1999 data += attr_length + 2;
2009 if ((vp = paircreate(PW_EXTENDED_ATTRIBUTE, PW_TYPE_OCTETS)) == NULL) {
2011 return radius_length;
2015 * Don't add it to the tail until we know it's OK.
2019 * Too little data to contain a diameter header.
2021 if (diameter_length < 12) {
2023 vp->type = PW_TYPE_OCTETS;
2024 memcpy(vp->vp_octets, header, diameter_length);
2025 vp->length = diameter_length;
2028 * Ensure there's no encryption or tag stuff,
2029 * we just pass the attribute as-is.
2031 memset(&vp->flags, 0, sizeof(vp->flags));
2034 if (!head) { /* add vp to the list */
2041 return radius_length;
2045 * Sanity check the lengths, next. If the length is too
2046 * short, then the rest of the diameter buffer can't be
2049 length = (header[5] << 16) | (header[6] << 8) | header[7];
2052 * lies about the length (too short)
2053 * or lies about the length (too long)
2055 * with too little diameter data
2056 * or with too little content
2059 (length > diameter_length) ||
2060 (((header[4] & 0x80) != 0) &&
2061 ((diameter_length < 16) || (length < 13)))) {
2063 * The rest of the data is small enough to fit
2064 * into one VP. Copy it over, and return it.
2066 if (diameter_length <= 253) goto done;
2069 * FIXME: make multiple VP's of the rest of the
2070 * data, so that we don't lose anything!
2075 return radius_length;
2078 memcpy(&attr, header, 4);
2080 if (attr > 65535) raw = 1; /* implementation limitations */
2083 * 1..255 are RADIUS, and shouldn't be encapsulated this way.
2084 * 256.. are Diameter.
2086 * We've arbitrarily assigned 32768..65535 from the
2087 * Diameter space to "extended RADIUS" attributes.
2089 if (attr < 32768) raw = 1;
2092 * We don't like any non-vendor flag bits being set.
2094 if ((header[4] & 0x7f) != 0) raw = 1;
2097 if ((header[4] & 0x80) != 0) {
2098 memcpy(&vendor, header + 8 , 4);
2099 vendor = ntohl(vendor);
2100 if (vendor > 32767) raw = 1; /* implementation limitations */
2108 diameter_length -= offset;
2111 * FIXME: This is an implementation limitation. We
2112 * should really allow strings longer than 253 bytes...
2114 * And bailing out completely (i.e. throwing away the rest
2115 * of the data) isn't an intelligent thing to do, either.
2120 return radius_length;
2124 lookup |= (vendor << 16);
2125 lookup |= (1 << 31); /* see dict_addattr */
2127 da = dict_attrbyvalue(lookup);
2132 * Copied from paircreate.
2134 strcpy(vp->name, da->name);
2135 vp->type = da->type;
2136 vp->flags = da->flags;
2137 vp->attribute = da->attr;
2139 vp->type = PW_TYPE_OCTETS;
2143 case PW_TYPE_STRING:
2144 case PW_TYPE_OCTETS:
2145 case PW_TYPE_ABINARY:
2146 memcpy(vp->vp_octets, header, length);
2147 vp->length = length;
2150 case PW_TYPE_INTEGER:
2151 if (length != 4) goto force_octets;
2153 memcpy(&vp->lvalue, header, 4);
2154 vp->lvalue = ntohl(vp->lvalue);
2158 * Try to get named VALUEs
2162 dval = dict_valbyattr(vp->attribute,
2165 strNcpy(vp->vp_strvalue,
2167 sizeof(vp->vp_strvalue));
2173 if (length != 4) goto force_octets;
2175 memcpy(&vp->lvalue, header, 4);
2176 vp->lvalue = ntohl(vp->lvalue);
2181 case PW_TYPE_IPADDR:
2182 if (length != 4) goto force_octets;
2184 memcpy(&vp->lvalue, header, 4);
2189 * IPv6 interface ID is 8 octets long.
2192 if (length != 8) goto force_octets;
2193 memcpy(vp->vp_ifid, header, 8);
2198 * IPv6 addresses are 16 octets long
2200 case PW_TYPE_IPV6ADDR:
2201 if (length != 16) goto force_octets;
2202 memcpy(&vp->vp_ipv6addr, header, 16);
2207 * IPv6 prefixes are 2 to 18 octets long.
2209 * RFC 3162: The first octet is unused.
2210 * The second is the length of the prefix
2211 * the rest are the prefix data.
2213 * The prefix length can have value 0 to 128.
2215 case PW_TYPE_IPV6PREFIX:
2216 if (length < 2 || length > 18) goto force_octets;
2217 if (header[1] > 128) goto force_octets;
2219 memcpy(vp->vp_ipv6prefix, header, length);
2220 vp->length = length;
2224 * FIXME: double-check that
2225 * (vp->vp_octets[1] >> 3) matches vp->length + 2
2227 if (vp->length < 18) {
2228 memset(vp->vp_octets + vp->length, 0,
2235 vp->type = PW_TYPE_OCTETS;
2238 * Ensure there's no encryption or tag stuff,
2239 * we just pass the attribute as-is.
2241 memset(&vp->flags, 0, sizeof(vp->flags));
2253 diameter_length -= length;
2255 if ((length & 0x03) != 0) {
2256 attr_length = 4 - (length & 0x03); /* padding */
2258 if (diameter_length < attr_length) {
2260 return radius_length;
2263 header += attr_length;
2264 diameter_length -= attr_length;
2266 if (diameter_length > 0) goto next_diameter;
2269 return radius_length;
2275 * Parse a RADIUS attribute into a data structure.
2277 VALUE_PAIR *rad_attr2vp(const RADIUS_PACKET *packet, const RADIUS_PACKET *original,
2278 const char *secret, int attribute, int length,
2279 const uint8_t *data)
2284 if ((vp = paircreate(attribute, PW_TYPE_OCTETS)) == NULL) {
2289 * If length is greater than 253, something is SERIOUSLY
2292 if (length > 253) length = 253; /* paranoia (pair-anoia?) */
2294 vp->length = length;
2295 vp->operator = T_OP_EQ;
2301 if (vp->flags.has_tag) {
2302 if (TAG_VALID(data[0]) ||
2303 (vp->flags.encrypt == FLAG_ENCRYPT_TUNNEL_PASSWORD)) {
2305 * Tunnel passwords REQUIRE a tag, even
2306 * if don't have a valid tag.
2308 vp->flags.tag = data[0];
2310 if ((vp->type == PW_TYPE_STRING) ||
2311 (vp->type == PW_TYPE_OCTETS)) offset = 1;
2316 * Copy the data to be decrypted
2318 memcpy(&vp->vp_octets[0], data + offset, length - offset);
2319 vp->length -= offset;
2322 * Decrypt the attribute.
2324 switch (vp->flags.encrypt) {
2328 case FLAG_ENCRYPT_USER_PASSWORD:
2330 rad_pwdecode((char *)vp->vp_strvalue,
2334 rad_pwdecode((char *)vp->vp_strvalue,
2338 if (vp->attribute == PW_USER_PASSWORD) {
2339 vp->length = strlen(vp->vp_strvalue);
2344 * Tunnel-Password's may go ONLY
2345 * in response packets.
2347 case FLAG_ENCRYPT_TUNNEL_PASSWORD:
2348 if (!original) goto raw;
2350 if (rad_tunnel_pwdecode(vp->vp_octets, &vp->length,
2351 secret, original->vector) < 0) {
2357 * Ascend-Send-Secret
2358 * Ascend-Receive-Secret
2360 case FLAG_ENCRYPT_ASCEND_SECRET:
2364 uint8_t my_digest[AUTH_VECTOR_LEN];
2365 make_secret(my_digest,
2368 memcpy(vp->vp_strvalue, my_digest,
2370 vp->vp_strvalue[AUTH_VECTOR_LEN] = '\0';
2371 vp->length = strlen(vp->vp_strvalue);
2377 } /* switch over encryption flags */
2381 case PW_TYPE_STRING:
2382 case PW_TYPE_OCTETS:
2383 case PW_TYPE_ABINARY:
2384 /* nothing more to do */
2388 if (vp->length != 1) goto raw;
2390 vp->lvalue = vp->vp_octets[0];
2395 if (vp->length != 2) goto raw;
2397 vp->lvalue = (vp->vp_octets[0] << 8) | vp->vp_octets[1];
2400 case PW_TYPE_INTEGER:
2401 if (vp->length != 4) goto raw;
2403 memcpy(&vp->lvalue, vp->vp_octets, 4);
2404 vp->lvalue = ntohl(vp->lvalue);
2406 if (vp->flags.has_tag) vp->lvalue &= 0x00ffffff;
2409 * Try to get named VALUEs
2413 dval = dict_valbyattr(vp->attribute,
2416 strNcpy(vp->vp_strvalue,
2418 sizeof(vp->vp_strvalue));
2424 if (vp->length != 4) goto raw;
2426 memcpy(&vp->lvalue, vp->vp_octets, 4);
2427 vp->lvalue = ntohl(vp->lvalue);
2431 case PW_TYPE_IPADDR:
2432 if (vp->length != 4) goto raw;
2434 memcpy(&vp->lvalue, vp->vp_octets, 4);
2438 * IPv6 interface ID is 8 octets long.
2441 if (vp->length != 8) goto raw;
2442 /* vp->vp_ifid == vp->vp_octets */
2446 * IPv6 addresses are 16 octets long
2448 case PW_TYPE_IPV6ADDR:
2449 if (vp->length != 16) goto raw;
2450 /* vp->vp_ipv6addr == vp->vp_octets */
2454 * IPv6 prefixes are 2 to 18 octets long.
2456 * RFC 3162: The first octet is unused.
2457 * The second is the length of the prefix
2458 * the rest are the prefix data.
2460 * The prefix length can have value 0 to 128.
2462 case PW_TYPE_IPV6PREFIX:
2463 if (vp->length < 2 || vp->length > 18) goto raw;
2464 if (vp->vp_octets[1] > 128) goto raw;
2467 * FIXME: double-check that
2468 * (vp->vp_octets[1] >> 3) matches vp->length + 2
2470 if (vp->length < 18) {
2471 memset(vp->vp_octets + vp->length, 0,
2478 vp->type = PW_TYPE_OCTETS;
2479 vp->length = length;
2480 memcpy(vp->vp_octets, data, length);
2484 * Ensure there's no encryption or tag stuff,
2485 * we just pass the attribute as-is.
2487 memset(&vp->flags, 0, sizeof(vp->flags));
2495 * Calculate/check digest, and decode radius attributes.
2497 * -1 on decoding error
2500 int rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original,
2504 uint32_t vendorcode;
2512 radius_packet_t *hdr;
2513 int vsa_tlen, vsa_llen;
2514 DICT_VENDOR *dv = NULL;
2517 * Extract attribute-value pairs
2519 hdr = (radius_packet_t *)packet->data;
2521 packet_length = packet->data_len - AUTH_HDR_LEN;
2524 * There may be VP's already in the packet. Don't
2527 for (tail = &packet->vps; *tail != NULL; tail = &((*tail)->next)) {
2533 vsa_tlen = vsa_llen = 1;
2536 * We have to read at least two bytes.
2538 * rad_recv() above ensures that this is OK.
2540 while (packet_length > 0) {
2545 * Normal attribute, handle it like normal.
2547 if (vendorcode == 0) {
2549 * No room to read attr/length,
2550 * or bad attribute, or attribute is
2551 * too short, or attribute is too long,
2552 * stop processing the packet.
2554 if ((packet_length < 2) ||
2555 (ptr[0] == 0) || (ptr[1] < 2) ||
2556 (ptr[1] > packet_length)) break;
2559 * 192 is "integer" for Ascend. So if we
2560 * get 12 or more bytes, it must be the
2561 * new extended format.
2563 if ((ptr[0] == PW_EXTENDED_ATTRIBUTE) &&
2564 (ptr[1] >= 2 + 12)) {
2566 attrlen = diameter2vp(packet, original, secret,
2567 ptr, packet_length, &pair);
2577 if (attribute != PW_VENDOR_SPECIFIC) goto create_pair;
2580 * No vendor code, or ONLY vendor code.
2582 if (attrlen <= 4) goto create_pair;
2588 * Handle Vendor-Specific
2590 if (vendorlen == 0) {
2596 * attrlen was checked above.
2598 memcpy(&lvalue, ptr, 4);
2599 myvendor = ntohl(lvalue);
2602 * Zero isn't allowed.
2604 if (myvendor == 0) goto create_pair;
2607 * This is an implementation issue.
2608 * We currently pack vendor into the upper
2609 * 16 bits of a 32-bit attribute number,
2610 * so we can't handle vendor numbers larger
2613 if (myvendor > 65535) goto create_pair;
2615 vsa_tlen = vsa_llen = 1;
2616 dv = dict_vendorbyvalue(myvendor);
2618 vsa_tlen = dv->type;
2619 vsa_llen = dv->length;
2623 * Sweep through the list of VSA's,
2624 * seeing if they exactly fill the
2625 * outer Vendor-Specific attribute.
2627 * If not, create a raw Vendor-Specific.
2630 sublen = attrlen - 4;
2633 * See if we can parse it.
2639 * Don't have a type, it's bad.
2641 if (sublen < vsa_tlen) goto create_pair;
2644 * Ensure that the attribute number
2653 myattr = (subptr[0] << 8) | subptr[1];
2657 if ((subptr[0] != 0) ||
2658 (subptr[1] != 0)) goto create_pair;
2660 myattr = (subptr[2] << 8) | subptr[3];
2664 * Our dictionary is broken.
2671 * Not enough room for one more
2674 if (sublen < vsa_tlen + vsa_llen) goto create_pair;
2677 attribute = (myvendor << 16) | myattr;
2678 ptr += 4 + vsa_tlen;
2679 attrlen -= (4 + vsa_tlen);
2680 packet_length -= 4 + vsa_tlen;
2684 if (subptr[vsa_tlen] < (vsa_tlen + vsa_llen))
2687 if (subptr[vsa_tlen] > sublen)
2689 sublen -= subptr[vsa_tlen];
2690 subptr += subptr[vsa_tlen];
2694 if (subptr[vsa_tlen] != 0) goto create_pair;
2695 if (subptr[vsa_tlen + 1] < (vsa_tlen + vsa_llen))
2697 if (subptr[vsa_tlen + 1] > sublen)
2699 sublen -= subptr[vsa_tlen + 1];
2700 subptr += subptr[vsa_tlen + 1];
2704 * Our dictionaries are
2710 } while (sublen > 0);
2712 vendorcode = myvendor;
2713 vendorlen = attrlen - 4;
2720 * attrlen is the length of this attribute.
2721 * total_len is the length of the encompassing
2730 attribute = (ptr[0] << 8) | ptr[1];
2733 default: /* can't hit this. */
2736 attribute |= (vendorcode << 16);
2741 attrlen = ptr[0] - (vsa_tlen + vsa_llen);
2745 attrlen = ptr[1] - (vsa_tlen + vsa_llen);
2748 default: /* can't hit this. */
2752 vendorlen -= vsa_tlen + vsa_llen + attrlen;
2753 if (vendorlen == 0) vendorcode = 0;
2754 packet_length -= (vsa_tlen + vsa_llen);
2757 * Create the attribute, setting the default type
2758 * to 'octets'. If the type in the dictionary
2759 * is different, then the dictionary type will
2760 * over-ride this one.
2763 pair = rad_attr2vp(packet, original, secret,
2764 attribute, attrlen, ptr);
2767 pairfree(&packet->vps);
2768 librad_log("out of memory");
2780 packet_length -= attrlen;
2784 * Merge information from the outside world into our
2787 lrad_rand_seed(packet->data, AUTH_HDR_LEN);
2796 * We assume that the passwd buffer passed is big enough.
2797 * RFC2138 says the password is max 128 chars, so the size
2798 * of the passwd buffer must be at least 129 characters.
2799 * Preferably it's just MAX_STRING_LEN.
2801 * int *pwlen is updated to the new length of the encrypted
2802 * password - a multiple of 16 bytes.
2804 int rad_pwencode(char *passwd, int *pwlen, const char *secret,
2805 const uint8_t *vector)
2807 lrad_MD5_CTX context, old;
2808 uint8_t digest[AUTH_VECTOR_LEN];
2809 int i, n, secretlen;
2813 * RFC maximum is 128 bytes.
2815 * If length is zero, pad it out with zeros.
2817 * If the length isn't aligned to 16 bytes,
2818 * zero out the extra data.
2822 if (len > 128) len = 128;
2825 memset(passwd, 0, AUTH_PASS_LEN);
2826 len = AUTH_PASS_LEN;
2827 } else if ((len % AUTH_PASS_LEN) != 0) {
2828 memset(&passwd[len], 0, AUTH_PASS_LEN - (len % AUTH_PASS_LEN));
2829 len += AUTH_PASS_LEN - (len % AUTH_PASS_LEN);
2834 * Use the secret to setup the decryption digest
2836 secretlen = strlen(secret);
2838 lrad_MD5Init(&context);
2839 lrad_MD5Update(&context, secret, secretlen);
2840 old = context; /* save intermediate work */
2843 * Encrypt it in place. Don't bother checking
2844 * len, as we've ensured above that it's OK.
2846 for (n = 0; n < len; n += AUTH_PASS_LEN) {
2848 lrad_MD5Update(&context, vector, AUTH_PASS_LEN);
2849 lrad_MD5Final(digest, &context);
2852 lrad_MD5Update(&context,
2853 passwd + n - AUTH_PASS_LEN,
2855 lrad_MD5Final(digest, &context);
2858 for (i = 0; i < AUTH_PASS_LEN; i++) {
2859 passwd[i + n] ^= digest[i];
2869 int rad_pwdecode(char *passwd, int pwlen, const char *secret,
2870 const uint8_t *vector)
2872 lrad_MD5_CTX context, old;
2873 uint8_t digest[AUTH_VECTOR_LEN];
2874 int i, n, secretlen;
2877 * The RFC's say that the maximum is 128.
2878 * The buffer we're putting it into above is 254, so
2879 * we don't need to do any length checking.
2881 if (pwlen > 128) pwlen = 128;
2886 if (pwlen == 0) goto done;
2889 * Use the secret to setup the decryption digest
2891 secretlen = strlen(secret);
2893 lrad_MD5Init(&context);
2894 lrad_MD5Update(&context, secret, secretlen);
2895 old = context; /* save intermediate work */
2898 * The inverse of the code above.
2900 for (n = 0; n < pwlen; n += AUTH_PASS_LEN) {
2902 lrad_MD5Update(&context, vector, AUTH_VECTOR_LEN);
2903 lrad_MD5Final(digest, &context);
2906 lrad_MD5Update(&context, passwd, AUTH_PASS_LEN);
2908 lrad_MD5Final(digest, &context);
2911 lrad_MD5Update(&context, passwd + n, AUTH_PASS_LEN);
2914 for (i = 0; i < AUTH_PASS_LEN; i++) {
2915 passwd[i + n] ^= digest[i];
2920 passwd[pwlen] = '\0';
2921 return strlen(passwd);
2926 * Encode Tunnel-Password attributes when sending them out on the wire.
2928 * int *pwlen is updated to the new length of the encrypted
2929 * password - a multiple of 16 bytes.
2931 * This is per RFC-2868 which adds a two char SALT to the initial intermediate
2934 int rad_tunnel_pwencode(char *passwd, int *pwlen, const char *secret,
2935 const uint8_t *vector)
2937 uint8_t buffer[AUTH_VECTOR_LEN + MAX_STRING_LEN + 3];
2938 unsigned char digest[AUTH_VECTOR_LEN];
2940 int i, n, secretlen;
2945 if (len > 127) len = 127;
2948 * Shift the password 3 positions right to place a salt and original
2949 * length, tag will be added automatically on packet send
2951 for (n=len ; n>=0 ; n--) passwd[n+3] = passwd[n];
2955 * save original password length as first password character;
2962 * Generate salt. The RFC's say:
2964 * The high bit of salt[0] must be set, each salt in a
2965 * packet should be unique, and they should be random
2967 * So, we set the high bit, add in a counter, and then
2968 * add in some CSPRNG data. should be OK..
2970 salt[0] = (0x80 | ( ((salt_offset++) & 0x0f) << 3) |
2971 (lrad_rand() & 0x07));
2972 salt[1] = lrad_rand();
2975 * Padd password to multiple of AUTH_PASS_LEN bytes.
2977 n = len % AUTH_PASS_LEN;
2979 n = AUTH_PASS_LEN - n;
2980 for (; n > 0; n--, len++)
2983 /* set new password length */
2987 * Use the secret to setup the decryption digest
2989 secretlen = strlen(secret);
2990 memcpy(buffer, secret, secretlen);
2992 for (n2 = 0; n2 < len; n2+=AUTH_PASS_LEN) {
2994 memcpy(buffer + secretlen, vector, AUTH_VECTOR_LEN);
2995 memcpy(buffer + secretlen + AUTH_VECTOR_LEN, salt, 2);
2996 librad_md5_calc(digest, buffer, secretlen + AUTH_VECTOR_LEN + 2);
2998 memcpy(buffer + secretlen, passwd + n2 - AUTH_PASS_LEN, AUTH_PASS_LEN);
2999 librad_md5_calc(digest, buffer, secretlen + AUTH_PASS_LEN);
3002 for (i = 0; i < AUTH_PASS_LEN; i++) {
3003 passwd[i + n2] ^= digest[i];
3011 * Decode Tunnel-Password encrypted attributes.
3013 * Defined in RFC-2868, this uses a two char SALT along with the
3014 * initial intermediate value, to differentiate it from the
3017 int rad_tunnel_pwdecode(uint8_t *passwd, int *pwlen, const char *secret,
3018 const uint8_t *vector)
3020 lrad_MD5_CTX context, old;
3021 uint8_t digest[AUTH_VECTOR_LEN];
3023 unsigned i, n, len, reallen;
3028 * We need at least a salt.
3031 librad_log("tunnel password is too short");
3036 * There's a salt, but no password. Or, there's a salt
3037 * and a 'data_len' octet. It's wrong, but at least we
3038 * can figure out what it means: the password is empty.
3040 * Note that this means we ignore the 'data_len' field,
3041 * if the attribute length tells us that there's no
3042 * more data. So the 'data_len' field may be wrong,
3051 len -= 2; /* discount the salt */
3054 * Use the secret to setup the decryption digest
3056 secretlen = strlen(secret);
3058 lrad_MD5Init(&context);
3059 lrad_MD5Update(&context, secret, secretlen);
3060 old = context; /* save intermediate work */
3063 * Set up the initial key:
3065 * b(1) = MD5(secret + vector + salt)
3067 lrad_MD5Update(&context, vector, AUTH_VECTOR_LEN);
3068 lrad_MD5Update(&context, passwd, 2);
3071 for (n = 0; n < len; n += AUTH_PASS_LEN) {
3075 lrad_MD5Final(digest, &context);
3080 * A quick check: decrypt the first octet
3081 * of the password, which is the
3082 * 'data_len' field. Ensure it's sane.
3084 reallen = passwd[2] ^ digest[0];
3085 if (reallen >= len) {
3086 librad_log("tunnel password is too long for the attribute");
3090 lrad_MD5Update(&context, passwd + 2, AUTH_PASS_LEN);
3094 lrad_MD5Final(digest, &context);
3097 lrad_MD5Update(&context, passwd + n + 2, AUTH_PASS_LEN);
3100 for (i = base; i < AUTH_PASS_LEN; i++) {
3101 passwd[n + i - 1] = passwd[n + i + 2] ^ digest[i];
3106 * See make_tunnel_password, above.
3108 if (reallen > 239) reallen = 239;
3111 passwd[reallen] = 0;
3117 * Encode a CHAP password
3119 * FIXME: might not work with Ascend because
3120 * we use vp->length, and Ascend gear likes
3121 * to send an extra '\0' in the string!
3123 int rad_chap_encode(RADIUS_PACKET *packet, uint8_t *output, int id,
3124 VALUE_PAIR *password)
3128 uint8_t string[MAX_STRING_LEN * 2 + 1];
3129 VALUE_PAIR *challenge;
3132 * Sanity check the input parameters
3134 if ((packet == NULL) || (password == NULL)) {
3139 * Note that the password VP can be EITHER
3140 * a User-Password attribute (from a check-item list),
3141 * or a CHAP-Password attribute (the client asking
3142 * the library to encode it).
3150 memcpy(ptr, password->vp_strvalue, password->length);
3151 ptr += password->length;
3152 i += password->length;
3155 * Use Chap-Challenge pair if present,
3156 * Request-Authenticator otherwise.
3158 challenge = pairfind(packet->vps, PW_CHAP_CHALLENGE);
3160 memcpy(ptr, challenge->vp_strvalue, challenge->length);
3161 i += challenge->length;
3163 memcpy(ptr, packet->vector, AUTH_VECTOR_LEN);
3164 i += AUTH_VECTOR_LEN;
3168 librad_md5_calc((uint8_t *)output + 1, (uint8_t *)string, i);
3175 * Seed the random number generator.
3177 * May be called any number of times.
3179 void lrad_rand_seed(const void *data, size_t size)
3184 * Ensure that the pool is initialized.
3186 if (!lrad_rand_initialized) {
3189 memset(&lrad_rand_pool, 0, sizeof(lrad_rand_pool));
3191 fd = open("/dev/urandom", O_RDONLY);
3197 while (total < sizeof(lrad_rand_pool.randrsl)) {
3198 this = read(fd, lrad_rand_pool.randrsl,
3199 sizeof(lrad_rand_pool.randrsl) - total);
3200 if ((this < 0) && (errno != EINTR)) break;
3201 if (this > 0) total += this;
3205 lrad_rand_pool.randrsl[0] = fd;
3206 lrad_rand_pool.randrsl[1] = time(NULL);
3207 lrad_rand_pool.randrsl[2] = errno;
3210 lrad_randinit(&lrad_rand_pool, 1);
3211 lrad_rand_pool.randcnt = 0;
3212 lrad_rand_initialized = 1;
3218 * Hash the user data
3221 if (!hash) hash = lrad_rand();
3222 hash = lrad_hash_update(data, size, hash);
3224 lrad_rand_pool.randmem[lrad_rand_pool.randcnt] ^= hash;
3229 * Return a 32-bit random number.
3231 uint32_t lrad_rand(void)
3236 * Ensure that the pool is initialized.
3238 if (!lrad_rand_initialized) {
3239 lrad_rand_seed(NULL, 0);
3242 num = lrad_rand_pool.randrsl[lrad_rand_pool.randcnt++];
3243 if (lrad_rand_pool.randcnt == 256) {
3244 lrad_isaac(&lrad_rand_pool);
3245 lrad_rand_pool.randcnt = 0;
3253 * Allocate a new RADIUS_PACKET
3255 RADIUS_PACKET *rad_alloc(int newvector)
3259 if ((rp = malloc(sizeof(RADIUS_PACKET))) == NULL) {
3260 librad_log("out of memory");
3263 memset(rp, 0, sizeof(*rp));
3269 uint32_t hash, base;
3272 * Don't expose the actual contents of the random
3276 for (i = 0; i < AUTH_VECTOR_LEN; i += sizeof(uint32_t)) {
3277 hash = lrad_rand() ^ base;
3278 memcpy(rp->vector + i, &hash, sizeof(hash));
3281 lrad_rand(); /* stir the pool again */
3287 * Free a RADIUS_PACKET
3289 void rad_free(RADIUS_PACKET **radius_packet_ptr)
3291 RADIUS_PACKET *radius_packet;
3293 if (!radius_packet_ptr) return;
3294 radius_packet = *radius_packet_ptr;
3296 free(radius_packet->data);
3297 pairfree(&radius_packet->vps);
3299 free(radius_packet);
3301 *radius_packet_ptr = NULL;