The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
base.c
Go to the documentation of this file.
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: e92b49699b53f019f48a5ddbfbd30f8797591cfa $
19 *
20 * @file protocols/radius/base.c
21 * @brief Functions to send/receive radius packets.
22 *
23 * @copyright 2000-2003,2006 The FreeRADIUS server project
24 */
25RCSID("$Id: e92b49699b53f019f48a5ddbfbd30f8797591cfa $")
26
27#include <fcntl.h>
28#include <ctype.h>
29
30#include "attrs.h"
31#include "radius.h"
32
33#include <freeradius-devel/io/pair.h>
34#include <freeradius-devel/util/md5.h>
35#include <freeradius-devel/util/net.h>
36#include <freeradius-devel/util/proto.h>
37#include <freeradius-devel/util/udp.h>
38#include <freeradius-devel/protocol/radius/freeradius.internal.h>
39
41static bool instantiated = false;
42
45
48 { .out = &dict_freeradius, .proto = "freeradius" },
49 { .out = &dict_radius, .proto = "radius" },
50
52};
53
65
68 { .out = &attr_packet_type, .name = "Packet-Type", .type = FR_TYPE_UINT32, .dict = &dict_radius },
69 { .out = &attr_packet_authentication_vector, .name = "Packet-Authentication-Vector", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
70 { .out = &attr_packet_token, .name = "Packet-Token", .type = FR_TYPE_UINT32, .dict = &dict_radius },
71 { .out = &attr_packet_reserved, .name = "Packet-Reserved", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
72 { .out = &attr_chap_challenge, .name = "CHAP-Challenge", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
73 { .out = &attr_chargeable_user_identity, .name = "Chargeable-User-Identity", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
74
75 { .out = &attr_eap_message, .name = "EAP-Message", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
76 { .out = &attr_message_authenticator, .name = "Message-Authenticator", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
77 { .out = &attr_state, .name = "State", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
78 { .out = &attr_vendor_specific, .name = "Vendor-Specific", .type = FR_TYPE_VSA, .dict = &dict_radius },
79 { .out = &attr_nas_filter_rule, .name = "NAS-Filter-Rule", .type = FR_TYPE_STRING, .dict = &dict_radius },
80
82};
83
84/*
85 * Some messages get printed out only in debugging mode.
86 */
87#define FR_DEBUG_STRERROR_PRINTF if (fr_debug_lvl) fr_strerror_printf
88#define FR_DEBUG_STRERROR_PRINTF_PUSH if (fr_debug_lvl) fr_strerror_printf_push
89
98
107
118
120 "", //!< 0
121 "Access-Request",
122 "Access-Accept",
123 "Access-Reject",
124 "Accounting-Request",
125 "Accounting-Response",
126 "Accounting-Status",
127 "Password-Request",
128 "Password-Accept",
129 "Password-Reject",
130 "Accounting-Message", //!< 10
131 "Access-Challenge",
132 "Status-Server",
133 "Status-Client",
134 "14",
135 "15",
136 "16",
137 "17",
138 "18",
139 "19",
140 "20", //!< 20
141 "Resource-Free-Request",
142 "Resource-Free-Response",
143 "Resource-Query-Request",
144 "Resource-Query-Response",
145 "Alternate-Resource-Reclaim-Request",
146 "NAS-Reboot-Request",
147 "NAS-Reboot-Response",
148 "28",
149 "Next-Passcode",
150 "New-Pin", //!< 30
151 "Terminate-Session",
152 "Password-Expired",
153 "Event-Request",
154 "Event-Response",
155 "35",
156 "36",
157 "37",
158 "38",
159 "39",
160 "Disconnect-Request", //!< 40
161 "Disconnect-ACK",
162 "Disconnect-NAK",
163 "CoA-Request",
164 "CoA-ACK",
165 "CoA-NAK",
166 "46",
167 "47",
168 "48",
169 "49",
170 "IP-Address-Allocate", //!< 50
171 "IP-Address-Release",
172 "Protocol-Error",
173};
174
175
176/** If we get a reply, the request must come from one of a small
177 * number of packet types.
178 */
194
197
198static int dict_flag_encrypt(fr_dict_attr_t **da_p, char const *value, UNUSED fr_dict_flag_parser_rule_t const *rules)
199{
200 static fr_table_num_sorted_t const encrypted[] = {
201 { L("Ascend-Secret"), RADIUS_FLAG_ENCRYPT_ASCEND_SECRET },
202 { L("Tunnel-Password"), RADIUS_FLAG_ENCRYPT_TUNNEL_PASSWORD },
203 { L("User-Password"), RADIUS_FLAG_ENCRYPT_USER_PASSWORD}
204 };
205 static size_t encrypted_len = NUM_ELEMENTS(encrypted);
206
209
211 if (encrypt == RADIUS_FLAG_ENCRYPT_INVALID) {
212 fr_strerror_printf("Unknown encryption type '%s'", value);
213 return -1;
214 }
215
216 flags->encrypt = encrypt;
217
218 return 0;
219}
220
224
226 { L("abinary"), { .func = dict_flag_abinary } },
227 { L("concat"), { .func = dict_flag_concat } },
228 { L("encrypt"), { .func = dict_flag_encrypt, .needs_value = true } },
229 { L("extended"), { .func = dict_flag_extended } },
230 { L("has_tag"), { .func = dict_flag_has_tag } },
231 { L("long_extended"), { .func = dict_flag_long_extended } }
232};
233
235{
236 int i;
237
238 if ((code == FR_RADIUS_CODE_UNDEFINED) || (code >= FR_RADIUS_CODE_MAX)) return -1;
239
240 for (i = 1; i < FR_RADIUS_CODE_MAX; i++) {
241 allowed[i] |= (allowed_replies[i] == code);
242 }
243
244 return 0;
245}
246
247/** Do Ascend-Send / Recv-Secret calculation.
248 *
249 * The secret is hidden by xoring with a MD5 digest created from
250 * the RADIUS shared secret and the authentication vector.
251 * We put them into MD5 in the reverse order from that used when
252 * encrypting passwords to RADIUS.
253 */
255 char const *secret, size_t secret_len, uint8_t const *vector)
256{
257 fr_md5_ctx_t *md5_ctx;
258 size_t i;
260 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
261
262 FR_DBUFF_EXTEND_LOWAT_OR_RETURN(&work_dbuff, sizeof(digest));
263
264 md5_ctx = fr_md5_ctx_alloc_from_list();
266 fr_md5_update(md5_ctx, (uint8_t const *) secret, secret_len);
267 fr_md5_final(digest, md5_ctx);
269
270 if (inlen > sizeof(digest)) inlen = sizeof(digest);
271 for (i = 0; i < inlen; i++) digest[i] ^= in[i];
272
273 fr_dbuff_in_memcpy(&work_dbuff, digest, sizeof(digest));
274
275 return fr_dbuff_set(dbuff, &work_dbuff);
276}
277
278/** Basic validation of RADIUS packet header
279 *
280 * @note fr_strerror errors are only available if fr_debug_lvl > 0. This is to reduce CPU time
281 * consumed when discarding malformed packet.
282 *
283 * @param[in] sockfd we're reading from.
284 * @param[out] src_ipaddr of the packet.
285 * @param[out] src_port of the packet.
286 * @param[out] code Pointer to where to write the packet code.
287 * @return
288 * - -1 on failure.
289 * - 1 on decode error.
290 * - >= RADIUS_HEADER_LENGTH on success. This is the packet length as specified in the header.
291 */
292ssize_t fr_radius_recv_header(int sockfd, fr_ipaddr_t *src_ipaddr, uint16_t *src_port, unsigned int *code)
293{
294 ssize_t data_len, packet_len;
295 uint8_t header[4];
296
297 data_len = udp_recv_peek(sockfd, header, sizeof(header), UDP_FLAGS_PEEK, src_ipaddr, src_port);
298 if (data_len < 0) {
299 if ((errno == EAGAIN) || (errno == EINTR)) return 0;
300 return -1;
301 }
302
303 /*
304 * Too little data is available, discard the packet.
305 */
306 if (data_len < 4) {
307 char buffer[INET6_ADDRSTRLEN];
308
309 FR_DEBUG_STRERROR_PRINTF("Expected at least 4 bytes of header data, got %zd bytes", data_len);
310invalid:
311 FR_DEBUG_STRERROR_PRINTF_PUSH("Invalid data from %s",
312 inet_ntop(src_ipaddr->af, &src_ipaddr->addr, buffer, sizeof(buffer)));
313 (void) udp_recv_discard(sockfd);
314
315 return 0;
316 }
317
318 /*
319 * See how long the packet says it is.
320 */
321 packet_len = (header[2] * 256) + header[3];
322
323 /*
324 * The length in the packet says it's less than
325 * a RADIUS header length: discard it.
326 */
327 if (packet_len < RADIUS_HEADER_LENGTH) {
328 FR_DEBUG_STRERROR_PRINTF("Expected at least " STRINGIFY(RADIUS_HEADER_LENGTH) " bytes of packet "
329 "data, got %zd bytes", packet_len);
330 goto invalid;
331 }
332
333 /*
334 * Enforce RFC requirements, for sanity.
335 * Anything after 4k will be discarded.
336 */
337 if (packet_len > MAX_PACKET_LEN) {
338 FR_DEBUG_STRERROR_PRINTF("Length field value too large, expected maximum of "
339 STRINGIFY(MAX_PACKET_LEN) " bytes, got %zd bytes", packet_len);
340 goto invalid;
341 }
342
343 *code = header[0];
344
345 /*
346 * The packet says it's this long, but the actual UDP
347 * size could still be smaller.
348 */
349 return packet_len;
350}
351
352/** Sign a previously encoded packet
353 *
354 * Calculates the request/response authenticator for packets which need it, and fills
355 * in the message-authenticator value if the attribute is present in the encoded packet.
356 *
357 * @param[in,out] packet (request or response).
358 * @param[in] vector original packet vector to use
359 * @param[in] secret to sign the packet with.
360 * @param[in] secret_len The length of the secret.
361 * @return
362 * - <0 on error
363 * - 0 on success
364 */
365int fr_radius_sign(uint8_t *packet, uint8_t const *vector,
366 uint8_t const *secret, size_t secret_len)
367{
368 uint8_t *msg, *end;
369 size_t packet_len = fr_nbo_to_uint16(packet + 2);
370
371 /*
372 * No real limit on secret length, this is just
373 * to catch uninitialised fields.
374 */
375 if (!fr_cond_assert(secret_len <= UINT16_MAX)) {
376 fr_strerror_printf("Secret is too long. Expected <= %u, got %zu",
377 (unsigned int) UINT16_MAX, secret_len);
378 return -1;
379 }
380
381 if (packet_len < RADIUS_HEADER_LENGTH) {
382 fr_strerror_const("Packet must be encoded before calling fr_radius_sign()");
383 return -1;
384 }
385
386 /*
387 * Find Message-Authenticator. Its value has to be
388 * calculated before we calculate the Request
389 * Authenticator or the Response Authenticator.
390 */
391 msg = packet + RADIUS_HEADER_LENGTH;
392 end = packet + packet_len;
393
394 while (msg < end) {
395 if ((end - msg) < 2) goto invalid_attribute;
396
397 if (msg[0] != FR_MESSAGE_AUTHENTICATOR) {
398 if (msg[1] < 2) goto invalid_attribute;
399
400 if ((msg + msg[1]) > end) {
401 invalid_attribute:
402 fr_strerror_printf("Invalid attribute at offset %zd", msg - packet);
403 return -1;
404 }
405 msg += msg[1];
406 continue;
407 }
408
409 if (msg[1] < 18) {
410 fr_strerror_const("Message-Authenticator is too small");
411 return -1;
412 }
413
414 switch (packet[0]) {
418 memset(packet + 4, 0, RADIUS_AUTH_VECTOR_LENGTH);
419 break;
420
430 if (!vector) goto need_original;
431 memcpy(packet + 4, vector, RADIUS_AUTH_VECTOR_LENGTH);
432 break;
433
436 /* packet + 4 MUST be the Request Authenticator filled with random data */
437 break;
438
439 default:
440 goto bad_packet;
441 }
442
443 /*
444 * Force Message-Authenticator to be zero,
445 * calculate the HMAC, and put it into the
446 * Message-Authenticator attribute.
447 */
448 memset(msg + 2, 0, RADIUS_AUTH_VECTOR_LENGTH);
449 fr_hmac_md5(msg + 2, packet, packet_len, secret, secret_len);
450 break;
451 }
452
453 /*
454 * Initialize the request authenticator.
455 */
456 switch (packet[0]) {
460 memset(packet + 4, 0, RADIUS_AUTH_VECTOR_LENGTH);
461 break;
462
472 if (!vector) {
473 need_original:
474 fr_strerror_const("Cannot sign response packet without a request packet");
475 return -1;
476 }
477 memcpy(packet + 4, vector, RADIUS_AUTH_VECTOR_LENGTH);
478 break;
479
480 /*
481 * The Request Authenticator is random numbers.
482 * We don't need to sign anything else, so
483 * return.
484 */
487 return 0;
488
489 default:
490 bad_packet:
491 fr_strerror_printf("Cannot sign unknown packet code %u", packet[0]);
492 return -1;
493 }
494
495 /*
496 * Request / Response Authenticator = MD5(packet + secret)
497 */
498 {
499 fr_md5_ctx_t *md5_ctx;
500
501 md5_ctx = fr_md5_ctx_alloc_from_list();
502 fr_md5_update(md5_ctx, packet, packet_len);
503 fr_md5_update(md5_ctx, secret, secret_len);
504 fr_md5_final(packet + 4, md5_ctx);
506 }
507
508 return 0;
509}
510
512 [FR_RADIUS_FAIL_NONE] = "none",
513 [FR_RADIUS_FAIL_MIN_LENGTH_PACKET] = "packet is smaller than the minimum packet length",
514 [FR_RADIUS_FAIL_MAX_LENGTH_PACKET] = "packet is larger than the maximum packet length",
515 [FR_RADIUS_FAIL_MIN_LENGTH_FIELD] = "header 'length' field has a value smaller than the minimum packet length",
516 [FR_RADIUS_FAIL_MIN_LENGTH_MISMATCH] = "header 'length' field has a value larger than the received data",
517 [FR_RADIUS_FAIL_UNKNOWN_PACKET_CODE] = "unknown packet code",
518 [FR_RADIUS_FAIL_UNEXPECTED_REQUEST_CODE] = "unexpected request code",
519 [FR_RADIUS_FAIL_UNEXPECTED_RESPONSE_CODE] = "unexpected response code",
520 [FR_RADIUS_FAIL_TOO_MANY_ATTRIBUTES] = "packet contains too many attributes",
521
522 [FR_RADIUS_FAIL_INVALID_ATTRIBUTE] = "attribute number 0 is invalid",
523
524 [FR_RADIUS_FAIL_HEADER_OVERFLOW] = "attribute header overflows the packet",
525 [FR_RADIUS_FAIL_ATTRIBUTE_TOO_SHORT] = "attribute 'length' field contains invalid value",
526 [FR_RADIUS_FAIL_ATTRIBUTE_OVERFLOW] = "attribute 'length' field overflows the packet",
527 [FR_RADIUS_FAIL_ATTRIBUTE_DECODE] = "unable to decode attributes",
528
529 [FR_RADIUS_FAIL_MA_INVALID_LENGTH] = "Message-Authenticator has invalid length",
530 [FR_RADIUS_FAIL_MA_MISSING] = "Message-Authenticator is required for this packet, but it is missing",
531 [FR_RADIUS_FAIL_MA_INVALID] = "Message-Authenticator fails verification. shared secret is incorrect",
532 [FR_RADIUS_FAIL_MA_TOO_MANY] = "More than one Message-Authenticator in a packet is invalid",
533 [FR_RADIUS_FAIL_PROXY_STATE_MISSING_MA] = "The packet contains Proxy-State, but no Message-Authenticator",
534
535 [FR_RADIUS_FAIL_VERIFY] = "packet fails verification, shared secret is incorrect",
536 [FR_RADIUS_FAIL_NO_MATCHING_REQUEST] = "did not find request which matched response",
537 [FR_RADIUS_FAIL_IO_ERROR] = "IO error",
538 [FR_RADIUS_FAIL_MAX] = "???",
539};
540
541/** See if the data pointed to by PTR is a valid RADIUS packet.
542 *
543 * @param[in] packet to check.
544 * @param[in,out] packet_len_p The size of the packet data, or 0 for "fatal error"
545 * @param[in] max_attributes to allow in the packet.
546 * @param[in] require_message_authenticator whether we require Message-Authenticator.
547 * @param[in] radius_1_1 true if for `radius/1.1` transport.
548 * @param[in] reason if not NULL, will have the failure reason written to where it points.
549 * @return
550 * - True on success.
551 * - False on failure.
552 */
553bool fr_radius_ok(uint8_t const *packet, size_t *packet_len_p,
554 uint32_t max_attributes, bool require_message_authenticator, bool radius_1_1,
556{
557 uint8_t const *attr, *end;
558 size_t totallen;
559 bool seen_ma = false;
560 uint32_t num_attributes;
562 size_t packet_len = *packet_len_p;
563
564 *packet_len_p = 0; /* also signals a fatal error */
565
566 if (radius_1_1) require_message_authenticator = false;
567
568 /*
569 * Check for packets smaller than the packet header.
570 *
571 * RFC 2865, Section 3., subsection 'length' says:
572 *
573 * "The minimum length is 20 ..."
574 */
575 if (packet_len < RADIUS_HEADER_LENGTH) {
577 goto finish;
578 }
579
580 /*
581 * Check for packets with mismatched size.
582 * i.e. We've received 128 bytes, and the packet header
583 * says it's 256 bytes long.
584 */
585 totallen = fr_nbo_to_uint16(packet + 2);
586
587 /*
588 * Repeat the length checks. This time, instead of
589 * looking at the data we received, look at the value
590 * of the 'length' field inside of the packet.
591 *
592 * Check for packets smaller than the packet header.
593 *
594 * RFC 2865, Section 3., subsection 'length' says:
595 *
596 * "The minimum length is 20 ..."
597 */
598 if (totallen < RADIUS_HEADER_LENGTH) {
600 goto finish;
601 }
602
603 /*
604 * And again, for the value of the 'length' field.
605 *
606 * RFC 2865, Section 3., subsection 'length' says:
607 *
608 * " ... and maximum length is 4096."
609 *
610 * HOWEVER. This requirement is for the network layer.
611 * If the code gets here, we assume that a well-formed
612 * packet is an OK packet.
613 *
614 * We allow both the UDP data length, and the RADIUS
615 * "length" field to contain up to 64K of data.
616 */
617
618 /*
619 * RFC 2865, Section 3., subsection 'length' says:
620 *
621 * "If the packet is shorter than the Length field
622 * indicates, it MUST be silently discarded."
623 *
624 * i.e. No response to the NAS.
625 */
626 if (totallen > packet_len) {
628 *packet_len_p = totallen;
629 goto finish;
630 }
631
632 /*
633 * RFC 2865, Section 3., subsection 'length' says:
634 *
635 * "Octets outside the range of the Length field MUST be
636 * treated as padding and ignored on reception."
637 */
638 *packet_len_p = packet_len = totallen;
639
640 /*
641 * Code of 0 is not understood.
642 * Code of 16 or greater is not understood.
643 */
644 if ((packet[0] == 0) ||
645 (packet[0] >= FR_RADIUS_CODE_MAX)) {
647 goto finish;
648 }
649
650 switch (packet[0]) {
651 /*
652 * Message-Authenticator is required in Status-Server
653 * packets, otherwise they can be trivially forged.
654 */
656 require_message_authenticator = !radius_1_1;
657 break;
658
659 /*
660 * Message-Authenticator may or may not be
661 * required for Access-* packets.
662 */
668 break;
669
670 /*
671 * Message-Authenticator is not required for all other packets, but is required if the
672 * caller asks for it.
673 */
676
680
684 break;
685
686 /*
687 * All other packet codes are not handled by the encoder, so we reject them.
688 */
689 default:
691 goto finish;
692 }
693
694 /*
695 * Walk through the packet's attributes, ensuring that
696 * they add up EXACTLY to the size of the packet.
697 *
698 * If they don't, then the attributes either under-fill
699 * or over-fill the packet. Any parsing of the packet
700 * is impossible, and will result in unknown side effects.
701 *
702 * This would ONLY happen with buggy RADIUS implementations,
703 * or with an intentional attack. Either way, we do NOT want
704 * to be vulnerable to this problem.
705 */
706 attr = packet + RADIUS_HEADER_LENGTH;
707 end = packet + packet_len;
708 num_attributes = 0;
709
710 while (attr < end) {
711 /*
712 * We need at least 2 bytes to check the
713 * attribute header.
714 */
715 if ((end - attr) < 2) {
717 goto fatal;
718 }
719
720 /*
721 * Attribute number zero is NOT defined.
722 */
723 if (attr[0] == 0) {
725 goto fatal;
726 }
727
728 /*
729 * Attributes are at LEAST as long as the ID & length
730 * fields. Anything shorter is an invalid attribute.
731 */
732 if (attr[1] < 2) {
734 goto fatal;
735 }
736
737 /*
738 * If there are fewer bytes in the packet than in the
739 * attribute, it's a bad packet.
740 */
741 if ((attr + attr[1]) > end) {
743 goto fatal;
744 }
745
746 /*
747 * Sanity check the attributes for length.
748 */
749 switch (attr[0]) {
750 default: /* don't do anything by default */
751 break;
752
753 /*
754 * If there's an EAP-Message, we require
755 * a Message-Authenticator.
756 */
757 case FR_EAP_MESSAGE:
758 require_message_authenticator = !radius_1_1;
759 break;
760
761 case FR_MESSAGE_AUTHENTICATOR:
762 /*
763 * Message-Authenticator is just a normal attribute in RADIUS/1.1
764 *
765 * @todo - Add an `unlang` policy which checks for this case and complains.
766 */
767 if (radius_1_1) break;
768
769 if (attr[1] != 2 + RADIUS_AUTH_VECTOR_LENGTH) {
771 *packet_len_p = 0;
772 goto finish;
773 }
774
775 /*
776 * Can't have two of them.
777 */
778 if (seen_ma) {
780 *packet_len_p = 0;
781 goto finish;
782 }
783
784 seen_ma = true;
785 break;
786 }
787
788 attr += attr[1];
789 num_attributes++; /* seen one more attribute */
790
791 /*
792 * If we're configured to look for a maximum number of
793 * attributes, and we've seen more than that maximum,
794 * then throw the packet away, as a possible DoS.
795 */
796 if (num_attributes > max_attributes) {
798 /* don't set *packet_len_p=0, we just discard _this_ packet */
799 goto finish;
800 }
801 }
802
803 /*
804 * http://www.freeradius.org/rfc/rfc2869.html#EAP-Message
805 *
806 * A packet with an EAP-Message attribute MUST also have
807 * a Message-Authenticator attribute.
808 *
809 * A Message-Authenticator all by itself is OK, though.
810 *
811 * Similarly, Status-Server packets MUST contain
812 * Message-Authenticator attributes.
813 */
814 if (require_message_authenticator && !seen_ma) {
816fatal:
817 *packet_len_p = 0;
818 goto finish;
819 }
820
821finish:
822
823 if (reason) *reason = failure;
824
825 return (failure == FR_RADIUS_FAIL_NONE);
826}
827
828/** Verify the signature of a request / response packet
829 *
830 * This function does its work by calling fr_radius_sign(), and then comparing the signature in the packet
831 * with the one we calculated. If they differ, there's a problem.
832 *
833 * @note - We rely on the security of the shared secret for UDP and TCP transport. For TLS transport, we
834 * rely on TLS to make the RADIUS packets both secure and private.
835 *
836 * @note - The BlastRADIUS mitigations require that "require_message_authenticator" and "limit_proxy_state"
837 * are used only for Access-Request packets. These mitigations MUST NOT be used for any any other packet
838 * codes.
839 *
840 * The caller should have called fr_radius_packet_ok() to see if the packet is well-formed, at least for the
841 * base RFC attributes.
842 *
843 * @param[in] packet the raw RADIUS packet (request or response)
844 * @param[in] vector the original packet vector
845 * @param[in] secret the shared secret
846 * @param[in] secret_len the length of the secret
847 * @param[in] require_message_authenticator whether we require Message-Authenticator.
848 * @param[in] limit_proxy_state whether we allow Proxy-State without Message-Authenticator.
849 * @return
850 * < <0 on error (negative fr_radius_decode_fail_t)
851 * - 0 on success.
852 */
853int fr_radius_verify(uint8_t *packet, uint8_t const *vector,
854 uint8_t const *secret, size_t secret_len,
855 bool require_message_authenticator, bool limit_proxy_state)
856{
857 bool found_message_authenticator = false;
858 bool found_proxy_state = false;
859 int rcode;
860 int code;
861 uint8_t *attr, *msg, *end;
862 size_t packet_len = fr_nbo_to_uint16(packet + 2);
863 uint8_t request_authenticator[RADIUS_AUTH_VECTOR_LENGTH];
864 uint8_t message_authenticator[RADIUS_AUTH_VECTOR_LENGTH];
865
866 if (packet_len < RADIUS_HEADER_LENGTH) {
867 fr_strerror_printf("invalid packet length %zu", packet_len);
869 }
870
871 code = packet[0];
872 if (!code || (code >= FR_RADIUS_CODE_MAX)) {
873 fr_strerror_printf("Unknown reply code %d", code);
875 }
876
877 /*
878 * RFC 5997 says that all Status-Server packets MUST contain Message-Authenticator.
879 */
880 require_message_authenticator |= (code == FR_RADIUS_CODE_STATUS_SERVER);
881
882 memcpy(request_authenticator, packet + 4, sizeof(request_authenticator));
883
884 /*
885 * Find Message-Authenticator. Its value has to be
886 * calculated before we calculate the Request
887 * Authenticator or the Response Authenticator.
888 */
889 msg = NULL;
890 attr = packet + RADIUS_HEADER_LENGTH;
891 end = packet + packet_len;
892
893 /*
894 * See what we need to do in order to verify the packet.
895 *
896 * Note that fr_radius_packet_ok() also does these checks, but it's worth re-doing them here so
897 * that potential API mis-use is safe. i.e. we do "defense in depth".
898 */
899 while (attr < end) {
900 if ((end - attr) < 2) goto invalid_attribute;
901
902 if (attr[1] < 2) goto invalid_attribute;
903
904 if ((attr + attr[1]) > end) {
905 invalid_attribute:
906 fr_strerror_printf("invalid attribute at offset %zd", attr - packet);
908 }
909
910 /*
911 * If there's no Message-Authenticator, then we need to check Proxy-State, but only if
912 * the caller asked us to limit Proxy-State.
913 */
914 if (attr[0] == FR_PROXY_STATE) {
915 found_proxy_state = limit_proxy_state;
916 goto next;
917 }
918
919 /*
920 * Check the contents of Message-Authenticator
921 */
922 if (attr[0] == FR_MESSAGE_AUTHENTICATOR) {
923 if (found_message_authenticator) {
924 fr_strerror_const("Multiple Message-Authenticators are invalid");
926 }
927
928
929 if (attr[1] != 18) {
930 fr_strerror_const("too small Message-Authenticator");
932 }
933
934 /*
935 * If we have found Message-Authenticator, then we verify that. We also can stop
936 * processing the packet, as we don't care about the contents of Proxy-State.
937 */
938 memcpy(message_authenticator, attr + 2, sizeof(message_authenticator));
939 found_message_authenticator = true;
940 msg = attr;
941 goto next;
942 }
943
944 /*
945 * RFC 3579 Section 3.1 requires Message-Authenticator if the packet contains
946 * EAP-Message.
947 */
948 if (attr[0] == FR_EAP_MESSAGE) require_message_authenticator = true;
949
950 next:
951 attr += attr[1];
952 }
953
954 /*
955 * Enforce limit_proxy_state for Access-Request packets.
956 */
957 if (code == FR_RADIUS_CODE_ACCESS_REQUEST) {
958 if (limit_proxy_state && found_proxy_state && !found_message_authenticator) {
959 fr_strerror_const("Proxy-State is not allowed without Message-Authenticator");
961 }
962 }
963
964 /*
965 * The require_message_authenticator flag can be set for any of these packet types. For other
966 * packet types, we check it if it exists, but we allow packets to not contain it.
967 */
968 if (require_message_authenticator && !found_message_authenticator) {
969 fr_strerror_printf("%s is missing the required Message-Authenticator attribute",
972 }
973
974 /*
975 * Overwrite the contents of Message-Authenticator
976 * with the one we calculate.
977 */
978 rcode = fr_radius_sign(packet, vector, secret, secret_len);
979 if (rcode < 0) {
980 fr_strerror_const_push("Failed calculating correct authenticator");
981 return -FR_RADIUS_FAIL_VERIFY;
982 }
983
984 /*
985 * Check the Message-Authenticator first.
986 *
987 * If it's invalid, restore the original
988 * Message-Authenticator and Request Authenticator
989 * fields.
990 *
991 * If it's valid the original and calculated
992 * message authenticators are the same, so we don't
993 * need to do anything.
994 */
995 if (msg &&
996 (fr_digest_cmp(message_authenticator, msg + 2, sizeof(message_authenticator)) != 0)) {
997 memcpy(msg + 2, message_authenticator, sizeof(message_authenticator));
998 memcpy(packet + 4, request_authenticator, sizeof(request_authenticator));
999
1000 fr_strerror_const("invalid Message-Authenticator (shared secret is incorrect)");
1002 }
1003
1004 /*
1005 * These are random numbers, so there's no point in
1006 * comparing them.
1007 */
1009 return 0;
1010 }
1011
1012 /*
1013 * Check the Request Authenticator.
1014 */
1015 if (fr_digest_cmp(request_authenticator, packet + 4, sizeof(request_authenticator)) != 0) {
1016 memcpy(packet + 4, request_authenticator, sizeof(request_authenticator));
1017 if (vector) {
1018 fr_strerror_const("invalid Response Authenticator (shared secret is incorrect)");
1019 } else {
1020 fr_strerror_const("invalid Request Authenticator (shared secret is incorrect)");
1021 }
1022 return -FR_RADIUS_FAIL_VERIFY;
1023 }
1024
1025 return 0;
1026}
1027
1028void *fr_radius_next_encodable(fr_dcursor_t *cursor, void *current, void *uctx);
1029
1030void *fr_radius_next_encodable(fr_dcursor_t *cursor, void *current, void *uctx)
1031{
1032 fr_pair_t *c = current;
1033 fr_dict_t *dict = talloc_get_type_abort(uctx, fr_dict_t);
1034
1035 while ((c = fr_dcursor_list_next(cursor, c))) {
1036 PAIR_VERIFY(c);
1037 if ((c->da->dict == dict) &&
1038 (!c->da->flags.internal || ((c->da->attr > FR_TAG_BASE) && (c->da->attr < (FR_TAG_BASE + 0x20))))) {
1039 break;
1040 }
1041 }
1042
1043 return c;
1044}
1045
1046
1048{
1049 ssize_t slen;
1050 fr_pair_t const *vp;
1051 fr_dcursor_t cursor;
1052 fr_dbuff_t work_dbuff, length_dbuff;
1053
1054 /*
1055 * The RADIUS header can't do more than 64K of data.
1056 */
1057 work_dbuff = FR_DBUFF_MAX(dbuff, 65535);
1058
1059 /*
1060 * RADIUS/1.1 uses a hard-code ID of 0x00
1061 */
1062 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, packet_ctx->code, packet_ctx->radius_1_1 ? 0x00 : packet_ctx->id);
1063 length_dbuff = FR_DBUFF(&work_dbuff);
1065
1066 switch (packet_ctx->code) {
1069 packet_ctx->request_authenticator = fr_dbuff_current(&work_dbuff);
1070
1071 /*
1072 * Add in the token, and clear out the rest of the buffer.
1073 */
1074 if (packet_ctx->radius_1_1) {
1075 FR_DBUFF_IN_RETURN(&work_dbuff, packet_ctx->token);
1076 FR_DBUFF_MEMSET_RETURN(&work_dbuff, 0, RADIUS_AUTH_VECTOR_LENGTH - sizeof(packet_ctx->token));
1077 break;
1078 }
1079
1080 /*
1081 * Allow over-rides of the authentication vector for testing.
1082 */
1084 if (vp && (vp->vp_length >= RADIUS_AUTH_VECTOR_LENGTH)) {
1085 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, vp->vp_octets, RADIUS_AUTH_VECTOR_LENGTH);
1086 } else {
1087 int i;
1088
1089 for (i = 0; i < 4; i++) {
1090 FR_DBUFF_IN_RETURN(&work_dbuff, (uint32_t) fr_rand());
1091 }
1092 }
1093 break;
1094
1104 if (!packet_ctx->request_authenticator) {
1105 fr_strerror_const("Cannot encode response without request");
1106 return -1;
1107 }
1108
1109 copy_request_authenticator:
1110 /*
1111 * For RADIUS/1.1, we just copy over the entire header from the request, which _should_
1112 * include the token.
1113 */
1114#ifndef NDEBUG
1115 if (packet_ctx->radius_1_1) {
1116 if (fr_nbo_to_uint32(packet_ctx->request_authenticator) != packet_ctx->token) {
1117 fr_assert("RADIUS/1.1 response is not copying the Request Token" == NULL);
1118 }
1119 }
1120#endif
1121
1122 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, packet_ctx->request_authenticator, RADIUS_AUTH_VECTOR_LENGTH);
1123 break;
1124
1127 /*
1128 * Tunnel-Password encoded attributes are allowed
1129 * in CoA-Request packets, by RFC 5176 Section
1130 * 3.6. HOWEVER, the tunnel passwords are
1131 * "encrypted" using the Request Authenticator,
1132 * which is all zeros! That makes them much
1133 * easier to decrypt. The only solution here is
1134 * to say "don't do that!"
1135 */
1137 if (packet_ctx->radius_1_1) goto copy_request_authenticator;
1138
1139 packet_ctx->request_authenticator = fr_dbuff_current(&work_dbuff);
1140
1142 break;
1143
1144 default:
1145 fr_strerror_printf("Cannot encode unknown packet code %d", packet_ctx->code);
1146 return -1;
1147 }
1148
1149 /*
1150 * RADIUS/1.1 can only be done over a secure transport.
1151 */
1152 fr_assert(!packet_ctx->radius_1_1 || packet_ctx->common->secure_transport);
1153
1154 /*
1155 * Always add Message-Authenticator after the packet
1156 * header for insecure transport protocols. RADIUS/1.1 packets are never signed.
1157 */
1158 if (!packet_ctx->common->secure_transport) switch (packet_ctx->code) {
1162#ifdef NAS_VIOLATES_RFC
1163 /*
1164 * Allow ridiculous behavior for vendors who violate the RFCs.
1165 *
1166 * But only if there's no EAP-Message in the packet.
1167 */
1168 if (packet_ctx->allow_vulnerable_clients && !fr_pair_find_by_da(vps, NULL, attr_eap_message)) {
1169 break;
1170 }
1172#endif
1173
1177 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, FR_MESSAGE_AUTHENTICATOR, 0x12,
1178 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1179 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
1180 packet_ctx->seen_message_authenticator = true;
1181 break;
1182
1183 default:
1184 break;
1185 }
1186
1187 /*
1188 * If we're sending Protocol-Error, add in Original-Packet-Code manually. If the user adds it
1189 * later themselves, well, too bad.
1190 */
1191 if (!packet_ctx->radius_1_1 && (packet_ctx->code == FR_RADIUS_CODE_PROTOCOL_ERROR)) {
1192 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, FR_EXTENDED_ATTRIBUTE_1, 0x07, 0x04 /* Original-Packet-Code */,
1193 0x00, 0x00, 0x00, packet_ctx->request_code);
1194 }
1195
1196 /*
1197 * Loop over the reply attributes for the packet.
1198 */
1200 while ((vp = fr_dcursor_current(&cursor))) {
1201 PAIR_VERIFY(vp);
1202
1203 /*
1204 * Encode an individual VP
1205 */
1206 slen = fr_radius_encode_pair(&work_dbuff, &cursor, packet_ctx);
1207 if (slen < 0) return slen;
1208 } /* done looping over all attributes */
1209
1210 /*
1211 * Add Proxy-State to the end of the packet if the caller requested it.
1212 */
1213 if (packet_ctx->add_proxy_state) {
1214 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, FR_PROXY_STATE, (uint8_t) (2 + sizeof(packet_ctx->common->proxy_state)));
1215 FR_DBUFF_IN_RETURN(&work_dbuff, packet_ctx->common->proxy_state);
1216 }
1217
1218 /*
1219 * Fill in the length field we zeroed out earlier.
1220 *
1221 */
1222 fr_dbuff_in(&length_dbuff, (uint16_t) (fr_dbuff_used(&work_dbuff)));
1223
1224 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "%s encoded packet", __FUNCTION__);
1225
1226 return fr_dbuff_set(dbuff, &work_dbuff);
1227}
1228
1230 uint8_t *packet, size_t packet_len,
1231 fr_radius_decode_ctx_t *decode_ctx)
1232{
1233 ssize_t slen;
1234 uint8_t const *attr, *end;
1235 static const uint8_t zeros[RADIUS_AUTH_VECTOR_LENGTH] = {};
1236
1237 decode_ctx->reason = FR_RADIUS_FAIL_NONE;
1238
1239 if (!decode_ctx->request_authenticator) {
1240 switch (packet[0]) {
1244 if (!decode_ctx->radius_1_1) {
1245 decode_ctx->request_authenticator = zeros;
1246 break;
1247 }
1249
1252 decode_ctx->request_authenticator = packet + 4;
1253 break;
1254
1255 default:
1256 fr_strerror_const("No authentication vector passed for packet decode");
1258 return -1;
1259 }
1260 }
1261
1262 /*
1263 * Copy the token over for RADIUS/1.1
1264 */
1265 if (decode_ctx->radius_1_1) decode_ctx->token = fr_nbo_to_uint32(packet + 4);
1266
1267 if (decode_ctx->request_code) {
1268 unsigned int code = packet[0];
1269
1270 if (code >= FR_RADIUS_CODE_MAX) {
1272 return -1;
1273 }
1274 if (decode_ctx->request_code >= FR_RADIUS_CODE_MAX) {
1276 return -1;
1277 }
1278
1279 if (!allowed_replies[code]) {
1281 return -1;
1282 }
1283
1284 /*
1285 * Protocol error can reply to any packet.
1286 *
1287 * Status-Server can get any reply.
1288 *
1289 * Otherwise the reply code must be associated with the request code we sent.
1290 */
1291 if ((allowed_replies[code] != decode_ctx->request_code) &&
1293 (decode_ctx->request_code != FR_RADIUS_CODE_STATUS_SERVER)) {
1295 return -1;
1296 }
1297 }
1298
1299 /*
1300 * We can skip verification for dynamic client checks, and for RADIUS/1.1 where packets are unsigned.
1301 */
1302 if (decode_ctx->verify && !decode_ctx->radius_1_1) {
1303 if (!decode_ctx->request_authenticator) decode_ctx->request_authenticator = zeros;
1304
1305 if (fr_radius_verify(packet, decode_ctx->request_authenticator,
1306 (uint8_t const *) decode_ctx->common->secret, decode_ctx->common->secret_length,
1307 decode_ctx->require_message_authenticator, decode_ctx->limit_proxy_state) < 0) {
1308 decode_ctx->reason = FR_RADIUS_FAIL_VERIFY;
1309 return -1;
1310 }
1311 }
1312
1313 attr = packet + 20;
1314 end = packet + packet_len;
1315
1316 /*
1317 * The caller MUST have called fr_radius_ok() first. If
1318 * he doesn't, all hell breaks loose.
1319 */
1320 while (attr < end) {
1321 /*
1322 * Silently omit Message-Authenticator for RADIUS/1.1
1323 */
1324 if (unlikely(decode_ctx->radius_1_1 && (attr[0] == FR_MESSAGE_AUTHENTICATOR))) {
1325 attr += attr[1];
1326 continue;
1327 }
1328
1329 slen = fr_radius_decode_pair(ctx, out, attr, (end - attr), decode_ctx);
1330 if (slen < 0) {
1332 return slen;
1333 }
1334
1335 /*
1336 * If slen is larger than the room in the packet,
1337 * all kinds of bad things happen.
1338 */
1339 if (!fr_cond_assert(slen <= (end - attr))) {
1340 return -slen;
1341 }
1342
1343 attr += slen;
1344 talloc_free_children(decode_ctx->tmp_ctx);
1345 }
1346
1347 /*
1348 * We've parsed the whole packet, return that.
1349 */
1350 return packet_len;
1351}
1352
1353/** Simple wrapper for callers who just need a shared secret
1354 *
1355 * @note - All callers verify the packet via fr_radius_packet_verify() before calling this function.
1356 */
1358 uint8_t *packet, size_t packet_len,
1359 uint8_t const *vector, char const *secret)
1360{
1361 ssize_t rcode;
1362 fr_radius_ctx_t common_ctx = {};
1364
1365 common_ctx.secret = secret;
1366 common_ctx.secret_length = strlen(secret);
1367
1368 packet_ctx.common = &common_ctx;
1369 packet_ctx.tmp_ctx = talloc(ctx, uint8_t);
1370 packet_ctx.request_authenticator = vector;
1371 packet_ctx.end = packet + packet_len;
1372
1373 rcode = fr_radius_decode(ctx, out, packet, packet_len, &packet_ctx);
1374 talloc_free(packet_ctx.tmp_ctx);
1375
1376 return rcode;
1377}
1378
1380{
1381 if (instance_count > 0) {
1383 return 0;
1384 }
1385
1387
1389 fail:
1391 return -1;
1392 }
1393
1396 goto fail;
1397 }
1398
1399 instantiated = true;
1400 return 0;
1401}
1402
1404{
1405 if (!instantiated) return;
1406
1407 if (--instance_count != 0) return;
1408
1410
1411 instantiated = false;
1412}
1413
1415{
1417
1418 if (da->parent->type == FR_TYPE_STRUCT) {
1419 if (flags->extended) {
1420 fr_strerror_const("Attributes with 'extended' flag cannot be used inside of a 'struct'");
1421 return false;
1422 }
1423
1424 if (flags->long_extended) {
1425 fr_strerror_const("Attributes with 'long_extended' flag cannot be used inside of a 'struct'");
1426 return false;
1427 }
1428
1429
1430 if (flags->concat) {
1431 fr_strerror_const("Attributes with 'concat' flag cannot be used inside of a 'struct'");
1432 return false;
1433 }
1434
1435 if (flags->has_tag) {
1436 fr_strerror_const("Attributes with 'tag' flag cannot be used inside of a 'struct'");
1437 return false;
1438 }
1439
1440 if (flags->abinary) {
1441 fr_strerror_const("Attributes with 'abinary' flag cannot be used inside of a 'struct'");
1442 return false;
1443 }
1444
1445 if (flags->encrypt > 0) {
1446 fr_strerror_const("Attributes with 'encrypt' flag cannot be used inside of a 'struct'");
1447 return false;
1448 }
1449
1450 return true;
1451 }
1452
1453 if (da->flags.length > 253) {
1454 fr_strerror_printf("Attributes cannot be more than 253 octets in length");
1455 return false;
1456 }
1457 /*
1458 * Secret things are secret.
1459 */
1460 if (flags->encrypt != 0) da->flags.secret = true;
1461
1462 if (flags->concat) {
1463 if (!da->parent->flags.is_root) {
1464 fr_strerror_const("Attributes with the 'concat' flag MUST be at the root of the dictionary");
1465 return false;
1466 }
1467
1468 if (da->type != FR_TYPE_OCTETS) {
1469 fr_strerror_const("Attributes with the 'concat' flag MUST be of data type 'octets'");
1470 return false;
1471 }
1472
1473 return true; /* can't use any other flag */
1474 }
1475
1476 /*
1477 * Tagged attributes can only be of two data types. They
1478 * can, however, be VSAs.
1479 */
1480 if (flags->has_tag) {
1481 if ((da->type != FR_TYPE_UINT32) && (da->type != FR_TYPE_STRING)) {
1482 fr_strerror_printf("The 'has_tag' flag can only be used for attributes of type 'integer' "
1483 "or 'string'");
1484 return false;
1485 }
1486
1487 if (!(da->parent->flags.is_root ||
1488 ((da->parent->type == FR_TYPE_VENDOR) &&
1489 (da->parent->parent && da->parent->parent->type == FR_TYPE_VSA)))) {
1490 fr_strerror_const("The 'has_tag' flag can only be used with RFC and VSA attributes");
1491 return false;
1492 }
1493
1494 return true;
1495 }
1496
1497 if (flags->extended) {
1498 if (da->type != FR_TYPE_TLV) {
1499 fr_strerror_const("The 'long' or 'extended' flag can only be used for attributes of type 'tlv'");
1500 return false;
1501 }
1502
1503 if (!da->parent->flags.is_root) {
1504 fr_strerror_const("The 'long' flag can only be used for top-level RFC attributes");
1505 return false;
1506 }
1507
1508 return true;
1509 }
1510
1511 /*
1512 * Stupid hacks for MS-CHAP-MPPE-Keys. The User-Password
1513 * encryption method has no provisions for encoding the
1514 * length of the data. For User-Password, the data is
1515 * (presumably) all printable non-zero data. For
1516 * MS-CHAP-MPPE-Keys, the data is binary crap. So... we
1517 * MUST specify a length in the dictionary.
1518 */
1519 if ((flags->encrypt == RADIUS_FLAG_ENCRYPT_USER_PASSWORD) && (da->type != FR_TYPE_STRING)) {
1520 if (da->type != FR_TYPE_OCTETS) {
1521 fr_strerror_printf("The 'encrypt=User-Password' flag can only be used with "
1522 "attributes of type 'string'");
1523 return false;
1524 }
1525
1526 if (da->flags.length == 0) {
1527 fr_strerror_printf("The 'encrypt=User-Password' flag MUST be used with an explicit length for "
1528 "'octets' data types");
1529 return false;
1530 }
1531 }
1532
1533 switch (da->type) {
1534 case FR_TYPE_STRING:
1535 break;
1536
1537 case FR_TYPE_TLV:
1538 case FR_TYPE_IPV4_ADDR:
1539 case FR_TYPE_UINT32:
1540 case FR_TYPE_OCTETS:
1541 if (flags->encrypt != RADIUS_FLAG_ENCRYPT_ASCEND_SECRET) break;
1543
1544 default:
1545 if (flags->encrypt) {
1546 fr_strerror_printf("The 'encrypt' flag cannot be used with attributes of type '%s'",
1547 fr_type_to_str(da->type));
1548 return false;
1549 }
1550 }
1551
1552 return true;
1553}
1554
1556 .name = "radius",
1557 .default_type_size = 1,
1558 .default_type_length = 1,
1559 .attr = {
1560 .flags = {
1561 .table = radius_flags,
1562 .table_len = NUM_ELEMENTS(radius_flags),
1563 .len = sizeof(fr_radius_attr_flags_t),
1564 },
1565 .valid = attr_valid,
1566 },
1567
1568 .init = fr_radius_global_init,
1569 .free = fr_radius_global_free,
1570
1571 .decode = fr_radius_decode_foreign,
1572 .encode = fr_radius_encode_foreign,
1573};
static int const char char buffer[256]
Definition acutest.h:641
log_entry msg
Definition acutest.h:947
static int packet_ctx
the tests pass only the address of packet_ctx
#define RCSID(id)
Definition build.h:609
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:245
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:422
#define STRINGIFY(x)
Definition build.h:233
#define unlikely(_x)
Definition build.h:492
#define UNUSED
Definition build.h:415
#define NUM_ELEMENTS(_t)
Definition build.h:443
fr_dict_t * dict
Definition common.c:31
#define fr_dbuff_used(_dbuff_or_marker)
Return the number of bytes remaining between the start of the dbuff or marker and the current positio...
Definition dbuff.h:819
#define FR_DBUFF_EXTEND_LOWAT_OR_RETURN(_dbuff_or_marker, _lowat)
Extend if we're below _lowat and return if we can't extend above _lowat.
Definition dbuff.h:725
#define fr_dbuff_current(_dbuff_or_marker)
Return the 'current' position of a dbuff or marker.
Definition dbuff.h:963
#define fr_dbuff_set(_dst, _src)
Set the 'current' position in a dbuff or marker using another dbuff or marker, a char pointer,...
Definition dbuff.h:1056
#define fr_dbuff_start(_dbuff_or_marker)
Return the 'start' position of a dbuff or marker.
Definition dbuff.h:950
#define FR_DBUFF_MEMSET_RETURN(_dbuff_or_marker, _c, _inlen)
Set _inlen bytes of a dbuff or marker to _c returning if there is insufficient space.
Definition dbuff.h:1561
#define FR_DBUFF_IN_MEMCPY_RETURN(_dbuff_or_marker, _in, _inlen)
Copy exactly _inlen bytes into dbuff or marker returning if there's insufficient space.
Definition dbuff.h:1435
#define fr_dbuff_in_memcpy(_dbuff_or_marker, _in, _inlen)
Copy exactly _inlen bytes into a dbuff or marker.
Definition dbuff.h:1403
#define fr_dbuff_in(_dbuff_or_marker, _in)
Copy data from a fixed sized C type into a dbuff or marker.
Definition dbuff.h:1620
#define FR_DBUFF_IN_RETURN(_dbuff_or_marker, _in)
Copy data from a fixed sized C type into a dbuff returning if there is insufficient space.
Definition dbuff.h:1638
#define FR_DBUFF(_dbuff_or_marker)
Create a new dbuff pointing to the same underlying buffer.
Definition dbuff.h:230
#define FR_DBUFF_MAX(_dbuff_or_marker, _max)
Limit the maximum number of bytes available in the dbuff when passing it to another function.
Definition dbuff.h:334
#define FR_DBUFF_IN_BYTES_RETURN(_dbuff_or_marker,...)
Copy a byte sequence into a dbuff or marker returning if there's insufficient space.
Definition dbuff.h:1525
#define fr_dcursor_list_next(_cursor, _ptr)
Definition dcursor.h:810
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition dcursor.h:337
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:177
fr_radius_packet_code_t
RADIUS packet codes.
Definition defs.h:31
@ FR_RADIUS_CODE_ACCESS_CHALLENGE
RFC2865 - Access-Challenge.
Definition defs.h:43
@ FR_RADIUS_CODE_ACCESS_REQUEST
RFC2865 - Access-Request.
Definition defs.h:33
@ FR_RADIUS_CODE_DISCONNECT_REQUEST
RFC3575/RFC5176 - Disconnect-Request.
Definition defs.h:46
@ FR_RADIUS_CODE_MAX
Maximum possible protocol code.
Definition defs.h:53
@ FR_RADIUS_CODE_DISCONNECT_ACK
RFC3575/RFC5176 - Disconnect-Ack (positive)
Definition defs.h:47
@ FR_RADIUS_CODE_STATUS_SERVER
RFC2865/RFC5997 - Status Server (request)
Definition defs.h:44
@ FR_RADIUS_CODE_COA_REQUEST
RFC3575/RFC5176 - CoA-Request.
Definition defs.h:49
@ FR_RADIUS_CODE_ACCESS_ACCEPT
RFC2865 - Access-Accept.
Definition defs.h:34
@ FR_RADIUS_CODE_ACCOUNTING_RESPONSE
RFC2866 - Accounting-Response.
Definition defs.h:37
@ FR_RADIUS_CODE_COA_NAK
RFC3575/RFC5176 - CoA-Nak (not willing to perform)
Definition defs.h:51
@ FR_RADIUS_CODE_UNDEFINED
Packet code has not been set.
Definition defs.h:32
@ FR_RADIUS_CODE_COA_ACK
RFC3575/RFC5176 - CoA-Ack (positive)
Definition defs.h:50
@ FR_RADIUS_CODE_DISCONNECT_NAK
RFC3575/RFC5176 - Disconnect-Nak (not willing to perform)
Definition defs.h:48
@ FR_RADIUS_CODE_PROTOCOL_ERROR
RFC7930 - Protocol-Error (generic NAK)
Definition defs.h:52
@ FR_RADIUS_CODE_ACCOUNTING_REQUEST
RFC2866 - Accounting-Request.
Definition defs.h:36
@ FR_RADIUS_CODE_ACCESS_REJECT
RFC2865 - Access-Reject.
Definition defs.h:35
#define MAX_PACKET_LEN
Definition defs.h:68
static int sockfd
Definition dhcpclient.c:55
@ FR_DICT_ATTR_EXT_PROTOCOL_SPECIFIC
Protocol specific extensions.
Definition dict.h:190
#define fr_dict_autofree(_to_free)
Definition dict.h:942
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
Definition dict.h:316
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:329
int fr_dict_attr_autoload(fr_dict_attr_autoload_t const *to_load)
Process a dict_attr_autoload element to load/verify a dictionary attribute.
Definition dict_util.c:4540
#define fr_dict_autoload(_to_load)
Definition dict.h:939
#define DICT_AUTOLOAD_TERMINATOR
Definition dict.h:335
char const * name
name of this protocol
Definition dict.h:480
#define FR_DICT_ATTR_FLAG_FUNC(_struct, _name)
Define a flag setting function, which sets one bit in a fr_dict_attr_flags_t.
Definition dict.h:459
Specifies an attribute which must be present for the module to function.
Definition dict.h:315
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:328
Protocol specific custom flag definitnion.
Definition dict.h:449
Protocol-specific callbacks in libfreeradius-PROTOCOL.
Definition dict.h:479
static void * fr_dict_attr_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
Definition dict_ext.h:122
Test enumeration values.
Definition dict_test.h:92
talloc_free(hp)
int fr_hmac_md5(uint8_t digest[MD5_DIGEST_LENGTH], uint8_t const *in, size_t inlen, uint8_t const *key, size_t key_len)
Calculate HMAC using internal MD5 implementation.
Definition hmac_md5.c:119
union fr_ipaddr_t::@159 addr
int af
Address family.
Definition inet.h:64
IPv4/6 prefix.
static fr_dict_t const * dict_freeradius
Definition base.c:37
fr_dict_attr_t const * attr_packet_type
Definition base.c:91
fr_dict_attr_t const * attr_eap_message
Definition base.c:94
fr_dict_t const * dict_radius
Definition base.c:76
fr_dict_attr_t const * attr_message_authenticator
Definition base.c:92
static int dict_flag_encrypt(fr_dict_attr_t **da_p, char const *value, UNUSED fr_dict_flag_parser_rule_t const *rules)
Definition base.c:231
static uint32_t instance_count
Definition base.c:44
ssize_t udp_recv_discard(int sockfd)
Discard the next UDP packet.
Definition udp.c:75
ssize_t udp_recv_peek(int sockfd, void *data, size_t data_len, int flags, fr_ipaddr_t *src_ipaddr, uint16_t *src_port)
Peek at the header of a UDP packet.
Definition udp.c:95
void fr_md5_ctx_free_from_list(fr_md5_ctx_t **ctx)
Free function for MD5 digest ctx.
Definition md5.c:515
fr_md5_ctx_t * fr_md5_ctx_alloc_from_list(void)
Allocation function for MD5 digest context.
Definition md5.c:470
#define fr_md5_final(_out, _ctx)
Finalise the ctx, producing the digest.
Definition md5.h:90
void fr_md5_ctx_t
Definition md5.h:25
#define fr_md5_update(_ctx, _in, _inlen)
Ingest plaintext into the digest.
Definition md5.h:83
#define MD5_DIGEST_LENGTH
unsigned short uint16_t
@ FR_TYPE_IPV4_ADDR
32 Bit IPv4 Address.
@ FR_TYPE_TLV
Contains nested attributes.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_STRUCT
like TLV, but without T or L, and fixed-width children
@ FR_TYPE_VENDOR
Attribute that represents a vendor in the attribute tree.
@ FR_TYPE_VSA
Vendor-Specific, for RADIUS attribute 26.
@ FR_TYPE_OCTETS
Raw octets.
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
int fr_digest_cmp(uint8_t const *a, uint8_t const *b, size_t length)
Do a comparison of two authentication digests by comparing the FULL data.
Definition misc.c:516
char const * inet_ntop(int af, void const *src, char *dst, size_t cnt)
Definition missing.c:447
static uint16_t fr_nbo_to_uint16(uint8_t const data[static sizeof(uint16_t)])
Read an unsigned 16bit integer from wire format (big endian)
Definition nbo.h:146
static uint32_t fr_nbo_to_uint32(uint8_t const data[static sizeof(uint32_t)])
Read an unsigned 32bit integer from wire format (big endian)
Definition nbo.h:167
#define RADIUS_HEADER_LENGTH
Definition net.h:80
#define RADIUS_AUTH_VECTOR_LENGTH
Definition net.h:89
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
Definition pair.c:716
static fr_dict_attr_t const * attr_state
Definition base.c:57
static uint8_t const zeros[6]
Definition base.c:105
static bool instantiated
Definition base.c:39
static bool attr_valid(fr_dict_attr_t *da)
Definition base.c:484
fr_dict_protocol_t libfreeradius_radius_dict_protocol
Definition base.c:1555
fr_dict_autoload_t libfreeradius_radius_dict[]
Definition base.c:47
ssize_t fr_radius_ascend_secret(fr_dbuff_t *dbuff, uint8_t const *in, size_t inlen, char const *secret, size_t secret_len, uint8_t const *vector)
Do Ascend-Send / Recv-Secret calculation.
Definition base.c:254
fr_dict_attr_t const * attr_nas_filter_rule
Definition base.c:64
static fr_dict_flag_parser_t const radius_flags[]
Definition base.c:225
fr_dict_attr_t const * attr_packet_authentication_vector
Definition base.c:55
const fr_radius_packet_code_t allowed_replies[FR_RADIUS_CODE_MAX]
If we get a reply, the request must come from one of a small number of packet types.
Definition base.c:179
fr_dict_attr_t const * attr_packet_reserved
Definition base.c:57
ssize_t fr_radius_decode(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t *packet, size_t packet_len, fr_radius_decode_ctx_t *decode_ctx)
Definition base.c:1229
size_t fr_radius_limit_proxy_state_table_len
Definition base.c:106
int fr_radius_sign(uint8_t *packet, uint8_t const *vector, uint8_t const *secret, size_t secret_len)
Sign a previously encoded packet.
Definition base.c:365
int fr_radius_verify(uint8_t *packet, uint8_t const *vector, uint8_t const *secret, size_t secret_len, bool require_message_authenticator, bool limit_proxy_state)
Verify the signature of a request / response packet.
Definition base.c:853
fr_dict_attr_autoload_t libfreeradius_radius_dict_attr[]
Definition base.c:67
char const * fr_radius_decode_fail_reason[FR_RADIUS_FAIL_MAX+1]
Definition base.c:511
size_t fr_radius_require_ma_table_len
Definition base.c:97
bool fr_radius_ok(uint8_t const *packet, size_t *packet_len_p, uint32_t max_attributes, bool require_message_authenticator, bool radius_1_1, fr_radius_decode_fail_t *reason)
See if the data pointed to by PTR is a valid RADIUS packet.
Definition base.c:553
fr_dict_attr_t const * attr_chap_challenge
Definition base.c:58
void * fr_radius_next_encodable(fr_dcursor_t *cursor, void *current, void *uctx)
Definition base.c:1030
fr_dict_attr_t const * attr_vendor_specific
Definition base.c:63
int fr_radius_global_init(void)
Definition base.c:1379
size_t fr_radius_request_name_table_len
Definition base.c:117
int fr_radius_allow_reply(fr_radius_packet_code_t code, bool allowed[static FR_RADIUS_CODE_MAX])
Definition base.c:234
#define FR_DEBUG_STRERROR_PRINTF
Definition base.c:87
fr_dict_attr_t const * attr_chargeable_user_identity
Definition base.c:59
ssize_t fr_radius_decode_simple(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t *packet, size_t packet_len, uint8_t const *vector, char const *secret)
Simple wrapper for callers who just need a shared secret.
Definition base.c:1357
void fr_radius_global_free(void)
Definition base.c:1403
fr_table_num_sorted_t const fr_radius_limit_proxy_state_table[]
Definition base.c:99
fr_table_num_sorted_t const fr_radius_request_name_table[]
Definition base.c:108
fr_table_num_sorted_t const fr_radius_require_ma_table[]
Definition base.c:90
ssize_t fr_radius_encode(fr_dbuff_t *dbuff, fr_pair_list_t *vps, fr_radius_encode_ctx_t *packet_ctx)
Definition base.c:1047
#define FR_DEBUG_STRERROR_PRINTF_PUSH
Definition base.c:88
char const * fr_radius_packet_name[FR_RADIUS_CODE_MAX]
Definition base.c:119
ssize_t fr_radius_recv_header(int sockfd, fr_ipaddr_t *src_ipaddr, uint16_t *src_port, unsigned int *code)
Basic validation of RADIUS packet header.
Definition base.c:292
fr_dict_attr_t const * attr_packet_token
Definition base.c:56
ssize_t fr_radius_decode_foreign(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *data, size_t data_len)
Definition decode.c:2213
ssize_t fr_radius_decode_pair(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *data, size_t data_len, fr_radius_decode_ctx_t *packet_ctx)
Create a "normal" fr_pair_t from the given data.
Definition decode.c:2093
ssize_t fr_radius_encode_pair(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, void *encode_ctx)
Encode a data structure into a RADIUS attribute.
Definition encode.c:1569
ssize_t fr_radius_encode_foreign(fr_dbuff_t *dbuff, fr_pair_list_t const *list)
Definition encode.c:1742
VQP attributes.
#define fr_assert(_expr)
Definition rad_assert.h:37
static char * secret
unsigned int has_tag
Attribute has a tag.
Definition radius.h:198
uint8_t request_code
original code for the request.
Definition radius.h:171
uint8_t const * request_authenticator
Definition radius.h:164
uint32_t token
Token read from a RADIUS/1.1 header.
Definition radius.h:173
unsigned int abinary
Attribute is in "abinary" format.
Definition radius.h:199
fr_radius_attr_flags_encrypt_t
Definition radius.h:186
@ RADIUS_FLAG_ENCRYPT_INVALID
Invalid encryption flag.
Definition radius.h:187
@ RADIUS_FLAG_ENCRYPT_USER_PASSWORD
Encrypt attribute RFC 2865 style.
Definition radius.h:189
@ RADIUS_FLAG_ENCRYPT_ASCEND_SECRET
Encrypt attribute ascend style.
Definition radius.h:191
@ RADIUS_FLAG_ENCRYPT_TUNNEL_PASSWORD
Encrypt attribute RFC 2868 style.
Definition radius.h:190
char const * secret
Definition radius.h:127
unsigned int concat
Attribute is concatenated.
Definition radius.h:197
fr_radius_decode_fail_t
Failure reasons.
Definition radius.h:89
@ FR_RADIUS_FAIL_ATTRIBUTE_DECODE
Definition radius.h:105
@ FR_RADIUS_FAIL_UNEXPECTED_REQUEST_CODE
Definition radius.h:96
@ FR_RADIUS_FAIL_ATTRIBUTE_OVERFLOW
Definition radius.h:104
@ FR_RADIUS_FAIL_VERIFY
Definition radius.h:113
@ FR_RADIUS_FAIL_NONE
Definition radius.h:90
@ FR_RADIUS_FAIL_MA_INVALID_LENGTH
Definition radius.h:107
@ FR_RADIUS_FAIL_MIN_LENGTH_FIELD
Definition radius.h:93
@ FR_RADIUS_FAIL_INVALID_ATTRIBUTE
Definition radius.h:100
@ FR_RADIUS_FAIL_IO_ERROR
Definition radius.h:115
@ FR_RADIUS_FAIL_MAX_LENGTH_PACKET
Definition radius.h:92
@ FR_RADIUS_FAIL_MA_MISSING
Definition radius.h:108
@ FR_RADIUS_FAIL_TOO_MANY_ATTRIBUTES
Definition radius.h:98
@ FR_RADIUS_FAIL_UNEXPECTED_RESPONSE_CODE
Definition radius.h:97
@ FR_RADIUS_FAIL_UNKNOWN_PACKET_CODE
Definition radius.h:95
@ FR_RADIUS_FAIL_MIN_LENGTH_MISMATCH
Definition radius.h:94
@ FR_RADIUS_FAIL_NO_MATCHING_REQUEST
Definition radius.h:114
@ FR_RADIUS_FAIL_HEADER_OVERFLOW
Definition radius.h:102
@ FR_RADIUS_FAIL_MIN_LENGTH_PACKET
Definition radius.h:91
@ FR_RADIUS_FAIL_MAX
Definition radius.h:116
@ FR_RADIUS_FAIL_ATTRIBUTE_TOO_SHORT
Definition radius.h:103
@ FR_RADIUS_FAIL_MA_INVALID
Definition radius.h:109
@ FR_RADIUS_FAIL_PROXY_STATE_MISSING_MA
Definition radius.h:111
@ FR_RADIUS_FAIL_MA_TOO_MANY
Definition radius.h:110
bool limit_proxy_state
Don't allow Proxy-State in requests.
Definition radius.h:179
unsigned int extended
Attribute is an extended attribute.
Definition radius.h:196
fr_radius_attr_flags_encrypt_t encrypt
Attribute is encrypted.
Definition radius.h:200
bool require_message_authenticator
Definition radius.h:178
size_t secret_length
Definition radius.h:128
bool verify
can skip verify for dynamic clients
Definition radius.h:177
fr_radius_ctx_t const * common
Definition radius.h:162
@ FR_RADIUS_REQUIRE_MA_NO
Do not require Message-Authenticator.
Definition radius.h:62
@ FR_RADIUS_REQUIRE_MA_YES
Require Message-Authenticator.
Definition radius.h:63
@ FR_RADIUS_REQUIRE_MA_AUTO
Only require Message-Authenticator if we've previously received a packet from this client with Messag...
Definition radius.h:64
unsigned int long_extended
Attribute is a long extended attribute.
Definition radius.h:195
@ FR_RADIUS_LIMIT_PROXY_STATE_NO
Do not limit Proxy-State.
Definition radius.h:76
@ FR_RADIUS_LIMIT_PROXY_STATE_AUTO
Do not allow Proxy-State unless:
Definition radius.h:81
@ FR_RADIUS_LIMIT_PROXY_STATE_YES
Limit Proxy-State.
Definition radius.h:78
fr_radius_decode_fail_t reason
reason for decode failure
Definition radius.h:169
bool radius_1_1
RADIUS/1.1 header, with a Token and no authenticator.
Definition radius.h:174
static fr_radius_attr_flags_t const * fr_radius_attr_flags(fr_dict_attr_t const *da)
Return RADIUS-specific flags for a given attribute.
Definition radius.h:214
TALLOC_CTX * tmp_ctx
for temporary things cleaned up during decoding
Definition radius.h:166
uint32_t fr_rand(void)
Return a 32-bit random number.
Definition rand.c:128
fr_pair_t * vp
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition pair.h:69
#define fr_table_value_by_str(_table, _name, _def)
Convert a string to a value using a sorted or ordered table.
Definition table.h:685
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
#define UDP_FLAGS_PEEK
Definition udp.h:39
#define fr_pair_dcursor_iter_init(_cursor, _list, _iter, _uctx)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:585
#define PAIR_VERIFY(_x)
Definition pair.h:209
#define FR_PROTO_HEX_DUMP(_data, _data_len, _fmt,...)
Definition proto.h:42
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const_push(_msg)
Definition strerror.h:227
#define fr_strerror_const(_msg)
Definition strerror.h:223
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:454
static fr_sbuff_err_t char size_t fr_sbuff_t * in
Definition value.h:1061
static fr_sbuff_err_t char ** out
Definition value.h:1061