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 * Set the Message-Authenticator to the correct
1060 * length and initial value.
1062 if (reply->attribute == PW_MESSAGE_AUTHENTICATOR) {
1063 reply->length = AUTH_VECTOR_LEN;
1064 memset(reply->vp_strvalue, 0, AUTH_VECTOR_LEN);
1065 packet->verified = total_length; /* HACK! */
1069 * Check for overflow.
1071 if (reply->flags.diameter) {
1072 len = reply->length;
1075 librad_log("ERROR: Too many attributes for packet, result is larger than RFC maximum of 4k");
1079 len += 8; /* Code, length */
1080 if (VENDOR(reply->attribute) != 0) len += 4;
1081 if ((len & 0x03) != 0) len += 4 - (len & 0x03);
1083 if (len > 253) len += (len / 253) * 2;
1085 if ((total_length + len) > MAX_PACKET_LEN) {
1086 librad_log("ERROR: Too many attributes for packet, result is larger than RFC maximum of 4k");
1092 len = vp2diameter(packet, original, secret, reply, ptr);
1095 * Print out ONLY the attributes which
1096 * we're sending over the wire, and print
1097 * them out BEFORE they're encrypted.
1101 len = rad_vp2attr(packet, original, secret, reply, ptr);
1104 if (len < 0) return -1;
1107 * Check that the packet is no more than 4k in
1108 * size, AFTER writing the attribute past the 4k
1109 * boundary, but BEFORE deciding to increase the
1110 * size of the packet. Note that the 'data'
1111 * buffer, above, is one attribute longer than
1112 * necessary, in order to permit this overflow.
1114 if ((total_length + len) > MAX_PACKET_LEN) {
1119 total_length += len;
1120 } /* done looping over all attributes */
1123 * Fill in the rest of the fields, and copy the data over
1124 * from the local stack to the newly allocated memory.
1126 * Yes, all this 'memcpy' is slow, but it means
1127 * that we only allocate the minimum amount of
1128 * memory for a request.
1130 packet->data_len = total_length;
1131 packet->data = (uint8_t *) malloc(packet->data_len);
1132 if (!packet->data) {
1133 librad_log("Out of memory");
1137 memcpy(packet->data, data, packet->data_len);
1138 hdr = (radius_packet_t *) packet->data;
1140 total_length = htons(total_length);
1141 memcpy(hdr->length, &total_length, sizeof(total_length));
1148 * Sign a previously encoded packet.
1150 int rad_sign(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1153 radius_packet_t *hdr = (radius_packet_t *)packet->data;
1156 * It wasn't assigned an Id, this is bad!
1158 if (packet->id < 0) {
1159 librad_log("ERROR: RADIUS packets must be assigned an Id.");
1163 if (!packet->data || (packet->data_len < AUTH_HDR_LEN) ||
1164 (packet->verified < 0)) {
1165 librad_log("ERROR: You must call rad_encode() before rad_sign()");
1170 * If there's a Message-Authenticator, update it
1171 * now, BEFORE updating the authentication vector.
1175 if (packet->verified > 0) {
1176 uint8_t calc_auth_vector[AUTH_VECTOR_LEN];
1178 switch (packet->code) {
1179 case PW_ACCOUNTING_REQUEST:
1180 case PW_ACCOUNTING_RESPONSE:
1181 case PW_DISCONNECT_REQUEST:
1182 case PW_DISCONNECT_ACK:
1183 case PW_DISCONNECT_NAK:
1184 case PW_COA_REQUEST:
1187 memset(hdr->vector, 0, AUTH_VECTOR_LEN);
1190 case PW_AUTHENTICATION_ACK:
1191 case PW_AUTHENTICATION_REJECT:
1192 case PW_ACCESS_CHALLENGE:
1194 librad_log("ERROR: Cannot sign response packet without a request packet.");
1197 memcpy(hdr->vector, original->vector,
1201 default: /* others have vector already set to zero */
1207 * Set the authentication vector to zero,
1208 * calculate the signature, and put it
1209 * into the Message-Authenticator
1212 lrad_hmac_md5(packet->data, packet->data_len,
1213 secret, strlen(secret),
1215 memcpy(packet->data + packet->verified + 2,
1216 calc_auth_vector, AUTH_VECTOR_LEN);
1219 * Copy the original request vector back
1220 * to the raw packet.
1222 memcpy(hdr->vector, packet->vector, AUTH_VECTOR_LEN);
1226 * Switch over the packet code, deciding how to
1229 switch (packet->code) {
1231 * Request packets are not signed, bur
1232 * have a random authentication vector.
1234 case PW_AUTHENTICATION_REQUEST:
1235 case PW_STATUS_SERVER:
1239 * Reply packets are signed with the
1240 * authentication vector of the request.
1248 MD5Update(&context, packet->data, packet->data_len);
1249 MD5Update(&context, secret, strlen(secret));
1250 MD5Final(digest, &context);
1252 memcpy(hdr->vector, digest, AUTH_VECTOR_LEN);
1253 memcpy(packet->vector, digest, AUTH_VECTOR_LEN);
1256 }/* switch over packet codes */
1262 * Reply to the request. Also attach
1263 * reply attribute value pairs and any user message provided.
1265 int rad_send(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
1270 char ip_buffer[128];
1273 * Maybe it's a fake packet. Don't send it.
1275 if (!packet || (packet->sockfd < 0)) {
1279 if ((packet->code > 0) && (packet->code < MAX_PACKET_CODE)) {
1280 what = packet_codes[packet->code];
1286 * First time through, allocate room for the packet
1288 if (!packet->data) {
1290 * Encode the packet.
1292 if (rad_encode(packet, original, secret) < 0) {
1297 * Re-sign it, including updating the
1298 * Message-Authenticator.
1300 if (rad_sign(packet, original, secret) < 0) {
1305 * If packet->data points to data, then we print out
1306 * the VP list again only for debugging.
1308 } else if (librad_debug) {
1309 DEBUG("Re-sending %s of id %d to %s port %d\n", what, packet->id,
1310 inet_ntop(packet->dst_ipaddr.af,
1311 &packet->dst_ipaddr.ipaddr,
1312 ip_buffer, sizeof(ip_buffer)),
1315 for (reply = packet->vps; reply; reply = reply->next) {
1316 /* FIXME: ignore attributes > 0xff */
1322 * And send it on it's way.
1324 return rad_sendto(packet->sockfd, packet->data, packet->data_len, 0,
1325 &packet->src_ipaddr, &packet->dst_ipaddr,
1331 * Validates the requesting client NAS. Calculates the
1332 * signature based on the clients private key.
1334 static int calc_acctdigest(RADIUS_PACKET *packet, const char *secret)
1336 uint8_t digest[AUTH_VECTOR_LEN];
1340 * Older clients have the authentication vector set to
1341 * all zeros. Return `1' in that case.
1343 memset(digest, 0, sizeof(digest));
1344 if (memcmp(packet->vector, digest, AUTH_VECTOR_LEN) == 0) {
1345 packet->verified = 1;
1350 * Zero out the auth_vector in the received packet.
1351 * Then append the shared secret to the received packet,
1352 * and calculate the MD5 sum. This must be the same
1353 * as the original MD5 sum (packet->vector).
1355 memset(packet->data + 4, 0, AUTH_VECTOR_LEN);
1358 * MD5(packet + secret);
1361 MD5Update(&context, packet->data, packet->data_len);
1362 MD5Update(&context, secret, strlen(secret));
1363 MD5Final(digest, &context);
1366 * Return 0 if OK, 2 if not OK.
1369 memcmp(digest, packet->vector, AUTH_VECTOR_LEN) ? 2 : 0;
1371 return packet->verified;
1375 * Validates the requesting client NAS. Calculates the
1376 * signature based on the clients private key.
1378 static int calc_replydigest(RADIUS_PACKET *packet, RADIUS_PACKET *original,
1381 uint8_t calc_digest[AUTH_VECTOR_LEN];
1387 if (original == NULL) {
1392 * Copy the original vector in place.
1394 memcpy(packet->data + 4, original->vector, AUTH_VECTOR_LEN);
1397 * MD5(packet + secret);
1400 MD5Update(&context, packet->data, packet->data_len);
1401 MD5Update(&context, secret, strlen(secret));
1402 MD5Final(calc_digest, &context);
1405 * Copy the packet's vector back to the packet.
1407 memcpy(packet->data + 4, packet->vector, AUTH_VECTOR_LEN);
1410 * Return 0 if OK, 2 if not OK.
1413 memcmp(packet->vector, calc_digest, AUTH_VECTOR_LEN) ? 2 : 0;
1414 return packet->verified;
1419 * See if the data pointed to by PTR is a valid RADIUS packet.
1421 * packet is not 'const * const' because we may update data_len,
1422 * if there's more data in the UDP packet than in the RADIUS packet.
1424 int rad_packet_ok(RADIUS_PACKET *packet)
1429 radius_packet_t *hdr;
1430 char host_ipaddr[128];
1435 * Check for packets smaller than the packet header.
1437 * RFC 2865, Section 3., subsection 'length' says:
1439 * "The minimum length is 20 ..."
1441 if (packet->data_len < AUTH_HDR_LEN) {
1442 librad_log("WARNING: Malformed RADIUS packet from host %s: too short (received %d < minimum %d)",
1443 inet_ntop(packet->src_ipaddr.af,
1444 &packet->src_ipaddr.ipaddr,
1445 host_ipaddr, sizeof(host_ipaddr)),
1446 packet->data_len, AUTH_HDR_LEN);
1451 * RFC 2865, Section 3., subsection 'length' says:
1453 * " ... and maximum length is 4096."
1455 if (packet->data_len > MAX_PACKET_LEN) {
1456 librad_log("WARNING: Malformed RADIUS packet from host %s: too long (received %d > maximum %d)",
1457 inet_ntop(packet->src_ipaddr.af,
1458 &packet->src_ipaddr.ipaddr,
1459 host_ipaddr, sizeof(host_ipaddr)),
1460 packet->data_len, MAX_PACKET_LEN);
1465 * Check for packets with mismatched size.
1466 * i.e. We've received 128 bytes, and the packet header
1467 * says it's 256 bytes long.
1469 totallen = (packet->data[2] << 8) | packet->data[3];
1470 hdr = (radius_packet_t *)packet->data;
1473 * Code of 0 is not understood.
1474 * Code of 16 or greate is not understood.
1476 if ((hdr->code == 0) ||
1477 (hdr->code >= MAX_PACKET_CODE)) {
1478 librad_log("WARNING: Bad RADIUS packet from host %s: unknown packet code %d",
1479 inet_ntop(packet->src_ipaddr.af,
1480 &packet->src_ipaddr.ipaddr,
1481 host_ipaddr, sizeof(host_ipaddr)),
1487 * Repeat the length checks. This time, instead of
1488 * looking at the data we received, look at the value
1489 * of the 'length' field inside of the packet.
1491 * Check for packets smaller than the packet header.
1493 * RFC 2865, Section 3., subsection 'length' says:
1495 * "The minimum length is 20 ..."
1497 if (totallen < AUTH_HDR_LEN) {
1498 librad_log("WARNING: Malformed RADIUS packet from host %s: too short (length %d < minimum %d)",
1499 inet_ntop(packet->src_ipaddr.af,
1500 &packet->src_ipaddr.ipaddr,
1501 host_ipaddr, sizeof(host_ipaddr)),
1502 totallen, AUTH_HDR_LEN);
1507 * And again, for the value of the 'length' field.
1509 * RFC 2865, Section 3., subsection 'length' says:
1511 * " ... and maximum length is 4096."
1513 if (totallen > MAX_PACKET_LEN) {
1514 librad_log("WARNING: Malformed RADIUS packet from host %s: too long (length %d > maximum %d)",
1515 inet_ntop(packet->src_ipaddr.af,
1516 &packet->src_ipaddr.ipaddr,
1517 host_ipaddr, sizeof(host_ipaddr)),
1518 totallen, MAX_PACKET_LEN);
1523 * RFC 2865, Section 3., subsection 'length' says:
1525 * "If the packet is shorter than the Length field
1526 * indicates, it MUST be silently discarded."
1528 * i.e. No response to the NAS.
1530 if (packet->data_len < totallen) {
1531 librad_log("WARNING: Malformed RADIUS packet from host %s: received %d octets, packet length says %d",
1532 inet_ntop(packet->src_ipaddr.af,
1533 &packet->src_ipaddr.ipaddr,
1534 host_ipaddr, sizeof(host_ipaddr)),
1535 packet->data_len, totallen);
1540 * RFC 2865, Section 3., subsection 'length' says:
1542 * "Octets outside the range of the Length field MUST be
1543 * treated as padding and ignored on reception."
1545 if (packet->data_len > totallen) {
1547 * We're shortening the packet below, but just
1548 * to be paranoid, zero out the extra data.
1550 memset(packet->data + totallen, 0, packet->data_len - totallen);
1551 packet->data_len = totallen;
1555 * Walk through the packet's attributes, ensuring that
1556 * they add up EXACTLY to the size of the packet.
1558 * If they don't, then the attributes either under-fill
1559 * or over-fill the packet. Any parsing of the packet
1560 * is impossible, and will result in unknown side effects.
1562 * This would ONLY happen with buggy RADIUS implementations,
1563 * or with an intentional attack. Either way, we do NOT want
1564 * to be vulnerable to this problem.
1567 count = totallen - AUTH_HDR_LEN;
1573 * Attribute number zero is NOT defined.
1576 librad_log("WARNING: Malformed RADIUS packet from host %s: Invalid attribute 0",
1577 inet_ntop(packet->src_ipaddr.af,
1578 &packet->src_ipaddr.ipaddr,
1579 host_ipaddr, sizeof(host_ipaddr)));
1584 * Attributes are at LEAST as long as the ID & length
1585 * fields. Anything shorter is an invalid attribute.
1588 librad_log("WARNING: Malformed RADIUS packet from host %s: attribute %d too short",
1589 inet_ntop(packet->src_ipaddr.af,
1590 &packet->src_ipaddr.ipaddr,
1591 host_ipaddr, sizeof(host_ipaddr)),
1597 * Sanity check the attributes for length.
1600 default: /* don't do anything by default */
1603 case PW_EAP_MESSAGE:
1604 seen_eap |= PW_EAP_MESSAGE;
1607 case PW_MESSAGE_AUTHENTICATOR:
1608 if (attr[1] != 2 + AUTH_VECTOR_LEN) {
1609 librad_log("WARNING: Malformed RADIUS packet from host %s: Message-Authenticator has invalid length %d",
1610 inet_ntop(packet->src_ipaddr.af,
1611 &packet->src_ipaddr.ipaddr,
1612 host_ipaddr, sizeof(host_ipaddr)),
1616 seen_eap |= PW_MESSAGE_AUTHENTICATOR;
1621 * FIXME: Look up the base 255 attributes in the
1622 * dictionary, and switch over their type. For
1623 * integer/date/ip, the attribute length SHOULD
1626 count -= attr[1]; /* grab the attribute length */
1628 num_attributes++; /* seen one more attribute */
1632 * If the attributes add up to a packet, it's allowed.
1634 * If not, we complain, and throw the packet away.
1637 librad_log("WARNING: Malformed RADIUS packet from host %s: packet attributes do NOT exactly fill the packet",
1638 inet_ntop(packet->src_ipaddr.af,
1639 &packet->src_ipaddr.ipaddr,
1640 host_ipaddr, sizeof(host_ipaddr)));
1645 * If we're configured to look for a maximum number of
1646 * attributes, and we've seen more than that maximum,
1647 * then throw the packet away, as a possible DoS.
1649 if ((librad_max_attributes > 0) &&
1650 (num_attributes > librad_max_attributes)) {
1651 librad_log("WARNING: Possible DoS attack from host %s: Too many attributes in request (received %d, max %d are allowed).",
1652 inet_ntop(packet->src_ipaddr.af,
1653 &packet->src_ipaddr.ipaddr,
1654 host_ipaddr, sizeof(host_ipaddr)),
1655 num_attributes, librad_max_attributes);
1660 * http://www.freeradius.org/rfc/rfc2869.html#EAP-Message
1662 * A packet with an EAP-Message attribute MUST also have
1663 * a Message-Authenticator attribute.
1665 * A Message-Authenticator all by itself is OK, though.
1668 (seen_eap != PW_MESSAGE_AUTHENTICATOR) &&
1669 (seen_eap != (PW_EAP_MESSAGE | PW_MESSAGE_AUTHENTICATOR))) {
1670 librad_log("WARNING: Insecure packet from host %s: Received EAP-Message with no Message-Authenticator.",
1671 inet_ntop(packet->src_ipaddr.af,
1672 &packet->src_ipaddr.ipaddr,
1673 host_ipaddr, sizeof(host_ipaddr)));
1678 * Fill RADIUS header fields
1680 packet->code = hdr->code;
1681 packet->id = hdr->id;
1682 memcpy(packet->vector, hdr->vector, AUTH_VECTOR_LEN);
1689 * Receive UDP client requests, and fill in
1690 * the basics of a RADIUS_PACKET structure.
1692 RADIUS_PACKET *rad_recv(int fd)
1694 RADIUS_PACKET *packet;
1697 * Allocate the new request data structure
1699 if ((packet = malloc(sizeof(*packet))) == NULL) {
1700 librad_log("out of memory");
1703 memset(packet, 0, sizeof(*packet));
1705 packet->data_len = rad_recvfrom(fd, &packet->data, 0,
1706 &packet->src_ipaddr, &packet->src_port,
1707 &packet->dst_ipaddr, &packet->dst_port);
1710 * Check for socket errors.
1712 if (packet->data_len < 0) {
1713 librad_log("Error receiving packet: %s", strerror(errno));
1714 /* packet->data is NULL */
1720 * If the packet is too big, then rad_recvfrom did NOT
1721 * allocate memory. Instead, it just discarded the
1724 if (packet->data_len > MAX_PACKET_LEN) {
1725 librad_log("Discarding packet: Larger than RFC limitation of 4096 bytes.");
1726 /* packet->data is NULL */
1732 * Read no data. Continue.
1733 * This check is AFTER the MAX_PACKET_LEN check above, because
1734 * if the packet is larger than MAX_PACKET_LEN, we also have
1735 * packet->data == NULL
1737 if ((packet->data_len == 0) || !packet->data) {
1738 librad_log("No data.");
1744 * See if it's a well-formed RADIUS packet.
1746 if (!rad_packet_ok(packet)) {
1752 * Remember which socket we read the packet from.
1754 packet->sockfd = fd;
1757 * FIXME: Do even more filtering by only permitting
1758 * certain IP's. The problem is that we don't know
1759 * how to do this properly for all possible clients...
1763 * Explicitely set the VP list to empty.
1768 char host_ipaddr[128];
1770 if ((packet->code > 0) && (packet->code < MAX_PACKET_CODE)) {
1771 printf("rad_recv: %s packet from host %s port %d",
1772 packet_codes[packet->code],
1773 inet_ntop(packet->src_ipaddr.af,
1774 &packet->src_ipaddr.ipaddr,
1775 host_ipaddr, sizeof(host_ipaddr)),
1778 printf("rad_recv: Packet from host %s port %d code=%d",
1779 inet_ntop(packet->src_ipaddr.af,
1780 &packet->src_ipaddr.ipaddr,
1781 host_ipaddr, sizeof(host_ipaddr)),
1785 printf(", id=%d, length=%d\n", packet->id, packet->data_len);
1793 * Verify the signature of a packet.
1795 int rad_verify(RADIUS_PACKET *packet, RADIUS_PACKET *original,
1802 if (!packet || !packet->data) return -1;
1805 * Before we allocate memory for the attributes, do more
1808 ptr = packet->data + AUTH_HDR_LEN;
1809 length = packet->data_len - AUTH_HDR_LEN;
1810 while (length > 0) {
1811 uint8_t msg_auth_vector[AUTH_VECTOR_LEN];
1812 uint8_t calc_auth_vector[AUTH_VECTOR_LEN];
1817 default: /* don't do anything. */
1821 * Note that more than one Message-Authenticator
1822 * attribute is invalid.
1824 case PW_MESSAGE_AUTHENTICATOR:
1825 memcpy(msg_auth_vector, &ptr[2], sizeof(msg_auth_vector));
1826 memset(&ptr[2], 0, AUTH_VECTOR_LEN);
1828 switch (packet->code) {
1832 case PW_ACCOUNTING_REQUEST:
1833 case PW_ACCOUNTING_RESPONSE:
1834 case PW_DISCONNECT_REQUEST:
1835 case PW_DISCONNECT_ACK:
1836 case PW_DISCONNECT_NAK:
1837 case PW_COA_REQUEST:
1840 memset(packet->data + 4, 0, AUTH_VECTOR_LEN);
1843 case PW_AUTHENTICATION_ACK:
1844 case PW_AUTHENTICATION_REJECT:
1845 case PW_ACCESS_CHALLENGE:
1847 librad_log("ERROR: Cannot validate Message-Authenticator in response packet without a request packet.");
1850 memcpy(packet->data + 4, original->vector, AUTH_VECTOR_LEN);
1854 lrad_hmac_md5(packet->data, packet->data_len,
1855 secret, strlen(secret), calc_auth_vector);
1856 if (memcmp(calc_auth_vector, msg_auth_vector,
1857 sizeof(calc_auth_vector)) != 0) {
1859 librad_log("Received packet from %s with invalid Message-Authenticator! (Shared secret is incorrect.)",
1860 inet_ntop(packet->src_ipaddr.af,
1861 &packet->src_ipaddr.ipaddr,
1862 buffer, sizeof(buffer)));
1863 /* Silently drop packet, according to RFC 3579 */
1865 } /* else the message authenticator was good */
1868 * Reinitialize Authenticators.
1870 memcpy(&ptr[2], msg_auth_vector, AUTH_VECTOR_LEN);
1871 memcpy(packet->data + 4, packet->vector, AUTH_VECTOR_LEN);
1873 } /* switch over the attributes */
1877 } /* loop over the packet, sanity checking the attributes */
1880 * It looks like a RADIUS packet, but we can't validate
1883 if ((packet->code == 0) || packet->code >= MAX_PACKET_CODE) {
1885 librad_log("Received Unknown packet code %d"
1886 "from client %s port %d: Cannot validate signature",
1888 inet_ntop(packet->src_ipaddr.af,
1889 &packet->src_ipaddr.ipaddr,
1890 buffer, sizeof(buffer)),
1896 * Calculate and/or verify digest.
1898 switch(packet->code) {
1902 case PW_AUTHENTICATION_REQUEST:
1903 case PW_STATUS_SERVER:
1904 case PW_DISCONNECT_REQUEST:
1906 * The authentication vector is random
1907 * nonsense, invented by the client.
1911 case PW_ACCOUNTING_REQUEST:
1912 if (calc_acctdigest(packet, secret) > 1) {
1913 librad_log("Received Accounting-Request packet "
1914 "from %s with invalid signature! (Shared secret is incorrect.)",
1915 inet_ntop(packet->src_ipaddr.af,
1916 &packet->src_ipaddr.ipaddr,
1917 buffer, sizeof(buffer)));
1922 /* Verify the reply digest */
1923 case PW_AUTHENTICATION_ACK:
1924 case PW_AUTHENTICATION_REJECT:
1925 case PW_ACCESS_CHALLENGE:
1926 case PW_ACCOUNTING_RESPONSE:
1927 case PW_DISCONNECT_ACK:
1928 case PW_DISCONNECT_NAK:
1931 rcode = calc_replydigest(packet, original, secret);
1933 librad_log("Received %s packet "
1934 "from client %s port %d with invalid signature (err=%d)! (Shared secret is incorrect.)",
1935 packet_codes[packet->code],
1936 inet_ntop(packet->src_ipaddr.af,
1937 &packet->src_ipaddr.ipaddr,
1938 buffer, sizeof(buffer)),
1946 librad_log("Received Unknown packet code %d"
1947 "from client %s port %d: Cannot validate signature",
1949 inet_ntop(packet->src_ipaddr.af,
1950 &packet->src_ipaddr.ipaddr,
1951 buffer, sizeof(buffer)),
1961 * Hack for IETF RADEXT. Don't use in a production environment.
1963 * Note that due to architecture limitations, we can't handle
1964 * AVP's with more than 253 bytes of data, but that should be
1965 * enough for initial inter-operability.
1967 static int diameter2vp(const RADIUS_PACKET *packet,
1968 const RADIUS_PACKET *original, const char *secret,
1969 const uint8_t *data, int total_length,
1972 int lookup, offset, radius_length, attr_length, diameter_length, raw;
1973 uint32_t attr, length, vendor;
1975 VALUE_PAIR *head, **tail, *vp;
1976 uint8_t *header; /* diameter header */
1977 uint8_t diameter[4096];
1979 diameter_length = radius_length = 0;
1985 * Unpack all contiguous Extended-Attributes into a local
1986 * buffer. It's slow, but it's safe.
1988 while (total_length > 0) {
1989 if (data[0] != PW_EXTENDED_ATTRIBUTE) break;
1991 attr_length = data[1];
1992 radius_length += attr_length;
1993 total_length -= attr_length;
1996 memcpy(header, data + 2, attr_length);
1997 header += attr_length;
1998 diameter_length += attr_length;
2000 data += attr_length + 2;
2010 if ((vp = paircreate(PW_EXTENDED_ATTRIBUTE, PW_TYPE_OCTETS)) == NULL) {
2012 return radius_length;
2016 * Don't add it to the tail until we know it's OK.
2020 * Too little data to contain a diameter header.
2022 if (diameter_length < 12) {
2024 vp->type = PW_TYPE_OCTETS;
2025 memcpy(vp->vp_octets, header, diameter_length);
2026 vp->length = diameter_length;
2029 * Ensure there's no encryption or tag stuff,
2030 * we just pass the attribute as-is.
2032 memset(&vp->flags, 0, sizeof(vp->flags));
2035 if (!head) { /* add vp to the list */
2042 return radius_length;
2046 * Sanity check the lengths, next. If the length is too
2047 * short, then the rest of the diameter buffer can't be
2050 length = (header[5] << 16) | (header[6] << 8) | header[7];
2053 * lies about the length (too short)
2054 * or lies about the length (too long)
2056 * with too little diameter data
2057 * or with too little content
2060 (length > diameter_length) ||
2061 (((header[4] & 0x80) != 0) &&
2062 ((diameter_length < 16) || (length < 13)))) {
2064 * The rest of the data is small enough to fit
2065 * into one VP. Copy it over, and return it.
2067 if (diameter_length <= 253) goto done;
2070 * FIXME: make multiple VP's of the rest of the
2071 * data, so that we don't lose anything!
2076 return radius_length;
2079 memcpy(&attr, header, 4);
2081 if (attr > 65535) raw = 1; /* implementation limitations */
2084 * 1..255 are RADIUS, and shouldn't be encapsulated this way.
2085 * 256.. are Diameter.
2087 * We've arbitrarily assigned 32768..65535 from the
2088 * Diameter space to "extended RADIUS" attributes.
2090 if (attr < 32768) raw = 1;
2093 * We don't like any non-vendor flag bits being set.
2095 if ((header[4] & 0x7f) != 0) raw = 1;
2098 if ((header[4] & 0x80) != 0) {
2099 memcpy(&vendor, header + 8 , 4);
2100 vendor = ntohl(vendor);
2101 if (vendor > 32767) raw = 1; /* implementation limitations */
2109 diameter_length -= offset;
2112 * FIXME: This is an implementation limitation. We
2113 * should really allow strings longer than 253 bytes...
2115 * And bailing out completely (i.e. throwing away the rest
2116 * of the data) isn't an intelligent thing to do, either.
2121 return radius_length;
2125 lookup |= (vendor << 16);
2126 lookup |= (1 << 31); /* see dict_addattr */
2128 da = dict_attrbyvalue(lookup);
2133 * Copied from paircreate.
2135 strcpy(vp->name, da->name);
2136 vp->type = da->type;
2137 vp->flags = da->flags;
2138 vp->attribute = da->attr;
2140 vp->type = PW_TYPE_OCTETS;
2144 case PW_TYPE_STRING:
2145 case PW_TYPE_OCTETS:
2146 case PW_TYPE_ABINARY:
2147 memcpy(vp->vp_octets, header, length);
2148 vp->length = length;
2151 case PW_TYPE_INTEGER:
2152 if (length != 4) goto force_octets;
2154 memcpy(&vp->lvalue, header, 4);
2155 vp->lvalue = ntohl(vp->lvalue);
2159 * Try to get named VALUEs
2163 dval = dict_valbyattr(vp->attribute,
2166 strNcpy(vp->vp_strvalue,
2168 sizeof(vp->vp_strvalue));
2174 if (length != 4) goto force_octets;
2176 memcpy(&vp->lvalue, header, 4);
2177 vp->lvalue = ntohl(vp->lvalue);
2182 case PW_TYPE_IPADDR:
2183 if (length != 4) goto force_octets;
2185 memcpy(&vp->lvalue, header, 4);
2190 * IPv6 interface ID is 8 octets long.
2193 if (length != 8) goto force_octets;
2194 memcpy(vp->vp_ifid, header, 8);
2199 * IPv6 addresses are 16 octets long
2201 case PW_TYPE_IPV6ADDR:
2202 if (length != 16) goto force_octets;
2203 memcpy(&vp->vp_ipv6addr, header, 16);
2208 * IPv6 prefixes are 2 to 18 octets long.
2210 * RFC 3162: The first octet is unused.
2211 * The second is the length of the prefix
2212 * the rest are the prefix data.
2214 * The prefix length can have value 0 to 128.
2216 case PW_TYPE_IPV6PREFIX:
2217 if (length < 2 || length > 18) goto force_octets;
2218 if (header[1] > 128) goto force_octets;
2220 memcpy(vp->vp_ipv6prefix, header, length);
2221 vp->length = length;
2225 * FIXME: double-check that
2226 * (vp->vp_octets[1] >> 3) matches vp->length + 2
2228 if (vp->length < 18) {
2229 memset(vp->vp_octets + vp->length, 0,
2236 vp->type = PW_TYPE_OCTETS;
2239 * Ensure there's no encryption or tag stuff,
2240 * we just pass the attribute as-is.
2242 memset(&vp->flags, 0, sizeof(vp->flags));
2254 diameter_length -= length;
2256 if ((length & 0x03) != 0) {
2257 attr_length = 4 - (length & 0x03); /* padding */
2259 if (diameter_length < attr_length) {
2261 return radius_length;
2264 header += attr_length;
2265 diameter_length -= attr_length;
2267 if (diameter_length > 0) goto next_diameter;
2270 return radius_length;
2276 * Parse a RADIUS attribute into a data structure.
2278 VALUE_PAIR *rad_attr2vp(const RADIUS_PACKET *packet, const RADIUS_PACKET *original,
2279 const char *secret, int attribute, int length,
2280 const uint8_t *data)
2285 if ((vp = paircreate(attribute, PW_TYPE_OCTETS)) == NULL) {
2290 * If length is greater than 253, something is SERIOUSLY
2293 if (length > 253) length = 253; /* paranoia (pair-anoia?) */
2295 vp->length = length;
2296 vp->operator = T_OP_EQ;
2302 if (vp->flags.has_tag) {
2303 if (TAG_VALID(data[0]) ||
2304 (vp->flags.encrypt == FLAG_ENCRYPT_TUNNEL_PASSWORD)) {
2306 * Tunnel passwords REQUIRE a tag, even
2307 * if don't have a valid tag.
2309 vp->flags.tag = data[0];
2311 if ((vp->type == PW_TYPE_STRING) ||
2312 (vp->type == PW_TYPE_OCTETS)) offset = 1;
2317 * Copy the data to be decrypted
2319 memcpy(&vp->vp_octets[0], data + offset, length - offset);
2320 vp->length -= offset;
2323 * Decrypt the attribute.
2325 switch (vp->flags.encrypt) {
2329 case FLAG_ENCRYPT_USER_PASSWORD:
2331 rad_pwdecode((char *)vp->vp_strvalue,
2335 rad_pwdecode((char *)vp->vp_strvalue,
2339 if (vp->attribute == PW_USER_PASSWORD) {
2340 vp->length = strlen(vp->vp_strvalue);
2345 * Tunnel-Password's may go ONLY
2346 * in response packets.
2348 case FLAG_ENCRYPT_TUNNEL_PASSWORD:
2349 if (!original) goto raw;
2351 if (rad_tunnel_pwdecode(vp->vp_octets, &vp->length,
2352 secret, original->vector) < 0) {
2358 * Ascend-Send-Secret
2359 * Ascend-Receive-Secret
2361 case FLAG_ENCRYPT_ASCEND_SECRET:
2365 uint8_t my_digest[AUTH_VECTOR_LEN];
2366 make_secret(my_digest,
2369 memcpy(vp->vp_strvalue, my_digest,
2371 vp->vp_strvalue[AUTH_VECTOR_LEN] = '\0';
2372 vp->length = strlen(vp->vp_strvalue);
2378 } /* switch over encryption flags */
2382 case PW_TYPE_STRING:
2383 case PW_TYPE_OCTETS:
2384 case PW_TYPE_ABINARY:
2385 /* nothing more to do */
2389 if (vp->length != 1) goto raw;
2391 vp->lvalue = vp->vp_octets[0];
2396 if (vp->length != 2) goto raw;
2398 vp->lvalue = (vp->vp_octets[0] << 8) | vp->vp_octets[1];
2401 case PW_TYPE_INTEGER:
2402 if (vp->length != 4) goto raw;
2404 memcpy(&vp->lvalue, vp->vp_octets, 4);
2405 vp->lvalue = ntohl(vp->lvalue);
2407 if (vp->flags.has_tag) vp->lvalue &= 0x00ffffff;
2410 * Try to get named VALUEs
2414 dval = dict_valbyattr(vp->attribute,
2417 strNcpy(vp->vp_strvalue,
2419 sizeof(vp->vp_strvalue));
2425 if (vp->length != 4) goto raw;
2427 memcpy(&vp->lvalue, vp->vp_octets, 4);
2428 vp->lvalue = ntohl(vp->lvalue);
2432 case PW_TYPE_IPADDR:
2433 if (vp->length != 4) goto raw;
2435 memcpy(&vp->lvalue, vp->vp_octets, 4);
2439 * IPv6 interface ID is 8 octets long.
2442 if (vp->length != 8) goto raw;
2443 /* vp->vp_ifid == vp->vp_octets */
2447 * IPv6 addresses are 16 octets long
2449 case PW_TYPE_IPV6ADDR:
2450 if (vp->length != 16) goto raw;
2451 /* vp->vp_ipv6addr == vp->vp_octets */
2455 * IPv6 prefixes are 2 to 18 octets long.
2457 * RFC 3162: The first octet is unused.
2458 * The second is the length of the prefix
2459 * the rest are the prefix data.
2461 * The prefix length can have value 0 to 128.
2463 case PW_TYPE_IPV6PREFIX:
2464 if (vp->length < 2 || vp->length > 18) goto raw;
2465 if (vp->vp_octets[1] > 128) goto raw;
2468 * FIXME: double-check that
2469 * (vp->vp_octets[1] >> 3) matches vp->length + 2
2471 if (vp->length < 18) {
2472 memset(vp->vp_octets + vp->length, 0,
2479 vp->type = PW_TYPE_OCTETS;
2480 vp->length = length;
2481 memcpy(vp->vp_octets, data, length);
2485 * Ensure there's no encryption or tag stuff,
2486 * we just pass the attribute as-is.
2488 memset(&vp->flags, 0, sizeof(vp->flags));
2496 * Calculate/check digest, and decode radius attributes.
2498 * -1 on decoding error
2501 int rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original,
2505 uint32_t vendorcode;
2513 radius_packet_t *hdr;
2514 int vsa_tlen, vsa_llen;
2515 DICT_VENDOR *dv = NULL;
2518 * Extract attribute-value pairs
2520 hdr = (radius_packet_t *)packet->data;
2522 packet_length = packet->data_len - AUTH_HDR_LEN;
2525 * There may be VP's already in the packet. Don't
2528 for (tail = &packet->vps; *tail != NULL; tail = &((*tail)->next)) {
2534 vsa_tlen = vsa_llen = 1;
2537 * We have to read at least two bytes.
2539 * rad_recv() above ensures that this is OK.
2541 while (packet_length > 0) {
2546 * Normal attribute, handle it like normal.
2548 if (vendorcode == 0) {
2550 * No room to read attr/length,
2551 * or bad attribute, or attribute is
2552 * too short, or attribute is too long,
2553 * stop processing the packet.
2555 if ((packet_length < 2) ||
2556 (ptr[0] == 0) || (ptr[1] < 2) ||
2557 (ptr[1] > packet_length)) break;
2560 * 192 is "integer" for Ascend. So if we
2561 * get 12 or more bytes, it must be the
2562 * new extended format.
2564 if ((ptr[0] == PW_EXTENDED_ATTRIBUTE) &&
2565 (ptr[1] >= 2 + 12)) {
2567 attrlen = diameter2vp(packet, original, secret,
2568 ptr, packet_length, &pair);
2578 if (attribute != PW_VENDOR_SPECIFIC) goto create_pair;
2581 * No vendor code, or ONLY vendor code.
2583 if (attrlen <= 4) goto create_pair;
2589 * Handle Vendor-Specific
2591 if (vendorlen == 0) {
2597 * attrlen was checked above.
2599 memcpy(&lvalue, ptr, 4);
2600 myvendor = ntohl(lvalue);
2603 * Zero isn't allowed.
2605 if (myvendor == 0) goto create_pair;
2608 * This is an implementation issue.
2609 * We currently pack vendor into the upper
2610 * 16 bits of a 32-bit attribute number,
2611 * so we can't handle vendor numbers larger
2614 if (myvendor > 65535) goto create_pair;
2616 vsa_tlen = vsa_llen = 1;
2617 dv = dict_vendorbyvalue(myvendor);
2619 vsa_tlen = dv->type;
2620 vsa_llen = dv->length;
2624 * Sweep through the list of VSA's,
2625 * seeing if they exactly fill the
2626 * outer Vendor-Specific attribute.
2628 * If not, create a raw Vendor-Specific.
2631 sublen = attrlen - 4;
2634 * See if we can parse it.
2640 * Don't have a type, it's bad.
2642 if (sublen < vsa_tlen) goto create_pair;
2645 * Ensure that the attribute number
2654 myattr = (subptr[0] << 8) | subptr[1];
2658 if ((subptr[0] != 0) ||
2659 (subptr[1] != 0)) goto create_pair;
2661 myattr = (subptr[2] << 8) | subptr[3];
2665 * Our dictionary is broken.
2672 * Not enough room for one more
2675 if (sublen < vsa_tlen + vsa_llen) goto create_pair;
2678 attribute = (myvendor << 16) | myattr;
2679 ptr += 4 + vsa_tlen;
2680 attrlen -= (4 + vsa_tlen);
2681 packet_length -= 4 + vsa_tlen;
2685 if (subptr[vsa_tlen] < (vsa_tlen + vsa_llen))
2688 if (subptr[vsa_tlen] > sublen)
2690 sublen -= subptr[vsa_tlen];
2691 subptr += subptr[vsa_tlen];
2695 if (subptr[vsa_tlen] != 0) goto create_pair;
2696 if (subptr[vsa_tlen + 1] < (vsa_tlen + vsa_llen))
2698 if (subptr[vsa_tlen + 1] > sublen)
2700 sublen -= subptr[vsa_tlen + 1];
2701 subptr += subptr[vsa_tlen + 1];
2705 * Our dictionaries are
2711 } while (sublen > 0);
2713 vendorcode = myvendor;
2714 vendorlen = attrlen - 4;
2721 * attrlen is the length of this attribute.
2722 * total_len is the length of the encompassing
2731 attribute = (ptr[0] << 8) | ptr[1];
2734 default: /* can't hit this. */
2737 attribute |= (vendorcode << 16);
2742 attrlen = ptr[0] - (vsa_tlen + vsa_llen);
2746 attrlen = ptr[1] - (vsa_tlen + vsa_llen);
2749 default: /* can't hit this. */
2753 vendorlen -= vsa_tlen + vsa_llen + attrlen;
2754 if (vendorlen == 0) vendorcode = 0;
2755 packet_length -= (vsa_tlen + vsa_llen);
2758 * Create the attribute, setting the default type
2759 * to 'octets'. If the type in the dictionary
2760 * is different, then the dictionary type will
2761 * over-ride this one.
2764 pair = rad_attr2vp(packet, original, secret,
2765 attribute, attrlen, ptr);
2768 pairfree(&packet->vps);
2769 librad_log("out of memory");
2781 packet_length -= attrlen;
2785 * Merge information from the outside world into our
2788 lrad_rand_seed(packet->data, AUTH_HDR_LEN);
2797 * We assume that the passwd buffer passed is big enough.
2798 * RFC2138 says the password is max 128 chars, so the size
2799 * of the passwd buffer must be at least 129 characters.
2800 * Preferably it's just MAX_STRING_LEN.
2802 * int *pwlen is updated to the new length of the encrypted
2803 * password - a multiple of 16 bytes.
2805 int rad_pwencode(char *passwd, int *pwlen, const char *secret,
2806 const uint8_t *vector)
2808 lrad_MD5_CTX context, old;
2809 uint8_t digest[AUTH_VECTOR_LEN];
2810 int i, n, secretlen;
2814 * RFC maximum is 128 bytes.
2816 * If length is zero, pad it out with zeros.
2818 * If the length isn't aligned to 16 bytes,
2819 * zero out the extra data.
2823 if (len > 128) len = 128;
2826 memset(passwd, 0, AUTH_PASS_LEN);
2827 len = AUTH_PASS_LEN;
2828 } else if ((len % AUTH_PASS_LEN) != 0) {
2829 memset(&passwd[len], 0, AUTH_PASS_LEN - (len % AUTH_PASS_LEN));
2830 len += AUTH_PASS_LEN - (len % AUTH_PASS_LEN);
2835 * Use the secret to setup the decryption digest
2837 secretlen = strlen(secret);
2839 lrad_MD5Init(&context);
2840 lrad_MD5Update(&context, secret, secretlen);
2841 old = context; /* save intermediate work */
2844 * Encrypt it in place. Don't bother checking
2845 * len, as we've ensured above that it's OK.
2847 for (n = 0; n < len; n += AUTH_PASS_LEN) {
2849 lrad_MD5Update(&context, vector, AUTH_PASS_LEN);
2850 lrad_MD5Final(digest, &context);
2853 lrad_MD5Update(&context,
2854 passwd + n - AUTH_PASS_LEN,
2856 lrad_MD5Final(digest, &context);
2859 for (i = 0; i < AUTH_PASS_LEN; i++) {
2860 passwd[i + n] ^= digest[i];
2870 int rad_pwdecode(char *passwd, int pwlen, const char *secret,
2871 const uint8_t *vector)
2873 lrad_MD5_CTX context, old;
2874 uint8_t digest[AUTH_VECTOR_LEN];
2875 int i, n, secretlen;
2878 * The RFC's say that the maximum is 128.
2879 * The buffer we're putting it into above is 254, so
2880 * we don't need to do any length checking.
2882 if (pwlen > 128) pwlen = 128;
2887 if (pwlen == 0) goto done;
2890 * Use the secret to setup the decryption digest
2892 secretlen = strlen(secret);
2894 lrad_MD5Init(&context);
2895 lrad_MD5Update(&context, secret, secretlen);
2896 old = context; /* save intermediate work */
2899 * The inverse of the code above.
2901 for (n = 0; n < pwlen; n += AUTH_PASS_LEN) {
2903 lrad_MD5Update(&context, vector, AUTH_VECTOR_LEN);
2904 lrad_MD5Final(digest, &context);
2907 lrad_MD5Update(&context, passwd, AUTH_PASS_LEN);
2909 lrad_MD5Final(digest, &context);
2912 lrad_MD5Update(&context, passwd + n, AUTH_PASS_LEN);
2915 for (i = 0; i < AUTH_PASS_LEN; i++) {
2916 passwd[i + n] ^= digest[i];
2921 passwd[pwlen] = '\0';
2922 return strlen(passwd);
2927 * Encode Tunnel-Password attributes when sending them out on the wire.
2929 * int *pwlen is updated to the new length of the encrypted
2930 * password - a multiple of 16 bytes.
2932 * This is per RFC-2868 which adds a two char SALT to the initial intermediate
2935 int rad_tunnel_pwencode(char *passwd, int *pwlen, const char *secret,
2936 const uint8_t *vector)
2938 uint8_t buffer[AUTH_VECTOR_LEN + MAX_STRING_LEN + 3];
2939 unsigned char digest[AUTH_VECTOR_LEN];
2941 int i, n, secretlen;
2946 if (len > 127) len = 127;
2949 * Shift the password 3 positions right to place a salt and original
2950 * length, tag will be added automatically on packet send
2952 for (n=len ; n>=0 ; n--) passwd[n+3] = passwd[n];
2956 * save original password length as first password character;
2963 * Generate salt. The RFC's say:
2965 * The high bit of salt[0] must be set, each salt in a
2966 * packet should be unique, and they should be random
2968 * So, we set the high bit, add in a counter, and then
2969 * add in some CSPRNG data. should be OK..
2971 salt[0] = (0x80 | ( ((salt_offset++) & 0x0f) << 3) |
2972 (lrad_rand() & 0x07));
2973 salt[1] = lrad_rand();
2976 * Padd password to multiple of AUTH_PASS_LEN bytes.
2978 n = len % AUTH_PASS_LEN;
2980 n = AUTH_PASS_LEN - n;
2981 for (; n > 0; n--, len++)
2984 /* set new password length */
2988 * Use the secret to setup the decryption digest
2990 secretlen = strlen(secret);
2991 memcpy(buffer, secret, secretlen);
2993 for (n2 = 0; n2 < len; n2+=AUTH_PASS_LEN) {
2995 memcpy(buffer + secretlen, vector, AUTH_VECTOR_LEN);
2996 memcpy(buffer + secretlen + AUTH_VECTOR_LEN, salt, 2);
2997 librad_md5_calc(digest, buffer, secretlen + AUTH_VECTOR_LEN + 2);
2999 memcpy(buffer + secretlen, passwd + n2 - AUTH_PASS_LEN, AUTH_PASS_LEN);
3000 librad_md5_calc(digest, buffer, secretlen + AUTH_PASS_LEN);
3003 for (i = 0; i < AUTH_PASS_LEN; i++) {
3004 passwd[i + n2] ^= digest[i];
3012 * Decode Tunnel-Password encrypted attributes.
3014 * Defined in RFC-2868, this uses a two char SALT along with the
3015 * initial intermediate value, to differentiate it from the
3018 int rad_tunnel_pwdecode(uint8_t *passwd, int *pwlen, const char *secret,
3019 const uint8_t *vector)
3021 lrad_MD5_CTX context, old;
3022 uint8_t digest[AUTH_VECTOR_LEN];
3024 unsigned i, n, len, reallen;
3029 * We need at least a salt.
3032 librad_log("tunnel password is too short");
3037 * There's a salt, but no password. Or, there's a salt
3038 * and a 'data_len' octet. It's wrong, but at least we
3039 * can figure out what it means: the password is empty.
3041 * Note that this means we ignore the 'data_len' field,
3042 * if the attribute length tells us that there's no
3043 * more data. So the 'data_len' field may be wrong,
3052 len -= 2; /* discount the salt */
3055 * Use the secret to setup the decryption digest
3057 secretlen = strlen(secret);
3059 lrad_MD5Init(&context);
3060 lrad_MD5Update(&context, secret, secretlen);
3061 old = context; /* save intermediate work */
3064 * Set up the initial key:
3066 * b(1) = MD5(secret + vector + salt)
3068 lrad_MD5Update(&context, vector, AUTH_VECTOR_LEN);
3069 lrad_MD5Update(&context, passwd, 2);
3072 for (n = 0; n < len; n += AUTH_PASS_LEN) {
3076 lrad_MD5Final(digest, &context);
3081 * A quick check: decrypt the first octet
3082 * of the password, which is the
3083 * 'data_len' field. Ensure it's sane.
3085 reallen = passwd[2] ^ digest[0];
3086 if (reallen >= len) {
3087 librad_log("tunnel password is too long for the attribute");
3091 lrad_MD5Update(&context, passwd + 2, AUTH_PASS_LEN);
3095 lrad_MD5Final(digest, &context);
3098 lrad_MD5Update(&context, passwd + n + 2, AUTH_PASS_LEN);
3101 for (i = base; i < AUTH_PASS_LEN; i++) {
3102 passwd[n + i - 1] = passwd[n + i + 2] ^ digest[i];
3107 * See make_tunnel_password, above.
3109 if (reallen > 239) reallen = 239;
3112 passwd[reallen] = 0;
3118 * Encode a CHAP password
3120 * FIXME: might not work with Ascend because
3121 * we use vp->length, and Ascend gear likes
3122 * to send an extra '\0' in the string!
3124 int rad_chap_encode(RADIUS_PACKET *packet, uint8_t *output, int id,
3125 VALUE_PAIR *password)
3129 uint8_t string[MAX_STRING_LEN * 2 + 1];
3130 VALUE_PAIR *challenge;
3133 * Sanity check the input parameters
3135 if ((packet == NULL) || (password == NULL)) {
3140 * Note that the password VP can be EITHER
3141 * a User-Password attribute (from a check-item list),
3142 * or a CHAP-Password attribute (the client asking
3143 * the library to encode it).
3151 memcpy(ptr, password->vp_strvalue, password->length);
3152 ptr += password->length;
3153 i += password->length;
3156 * Use Chap-Challenge pair if present,
3157 * Request-Authenticator otherwise.
3159 challenge = pairfind(packet->vps, PW_CHAP_CHALLENGE);
3161 memcpy(ptr, challenge->vp_strvalue, challenge->length);
3162 i += challenge->length;
3164 memcpy(ptr, packet->vector, AUTH_VECTOR_LEN);
3165 i += AUTH_VECTOR_LEN;
3169 librad_md5_calc((uint8_t *)output + 1, (uint8_t *)string, i);
3176 * Seed the random number generator.
3178 * May be called any number of times.
3180 void lrad_rand_seed(const void *data, size_t size)
3185 * Ensure that the pool is initialized.
3187 if (!lrad_rand_initialized) {
3190 memset(&lrad_rand_pool, 0, sizeof(lrad_rand_pool));
3192 fd = open("/dev/urandom", O_RDONLY);
3198 while (total < sizeof(lrad_rand_pool.randrsl)) {
3199 this = read(fd, lrad_rand_pool.randrsl,
3200 sizeof(lrad_rand_pool.randrsl) - total);
3201 if ((this < 0) && (errno != EINTR)) break;
3202 if (this > 0) total += this;
3206 lrad_rand_pool.randrsl[0] = fd;
3207 lrad_rand_pool.randrsl[1] = time(NULL);
3208 lrad_rand_pool.randrsl[2] = errno;
3211 lrad_randinit(&lrad_rand_pool, 1);
3212 lrad_rand_pool.randcnt = 0;
3213 lrad_rand_initialized = 1;
3219 * Hash the user data
3222 if (!hash) hash = lrad_rand();
3223 hash = lrad_hash_update(data, size, hash);
3225 lrad_rand_pool.randmem[lrad_rand_pool.randcnt] ^= hash;
3230 * Return a 32-bit random number.
3232 uint32_t lrad_rand(void)
3237 * Ensure that the pool is initialized.
3239 if (!lrad_rand_initialized) {
3240 lrad_rand_seed(NULL, 0);
3243 num = lrad_rand_pool.randrsl[lrad_rand_pool.randcnt++];
3244 if (lrad_rand_pool.randcnt == 256) {
3245 lrad_isaac(&lrad_rand_pool);
3246 lrad_rand_pool.randcnt = 0;
3254 * Allocate a new RADIUS_PACKET
3256 RADIUS_PACKET *rad_alloc(int newvector)
3260 if ((rp = malloc(sizeof(RADIUS_PACKET))) == NULL) {
3261 librad_log("out of memory");
3264 memset(rp, 0, sizeof(*rp));
3270 uint32_t hash, base;
3273 * Don't expose the actual contents of the random
3277 for (i = 0; i < AUTH_VECTOR_LEN; i += sizeof(uint32_t)) {
3278 hash = lrad_rand() ^ base;
3279 memcpy(rp->vector + i, &hash, sizeof(hash));
3282 lrad_rand(); /* stir the pool again */
3288 * Free a RADIUS_PACKET
3290 void rad_free(RADIUS_PACKET **radius_packet_ptr)
3292 RADIUS_PACKET *radius_packet;
3294 if (!radius_packet_ptr) return;
3295 radius_packet = *radius_packet_ptr;
3297 free(radius_packet->data);
3298 pairfree(&radius_packet->vps);
3300 free(radius_packet);
3302 *radius_packet_ptr = NULL;