The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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: d16952add9833cdfb8e7c23a4e6d654b499ee0ec $
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: d16952add9833cdfb8e7c23a4e6d654b499ee0ec $")
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/table.h>
38#include <freeradius-devel/util/udp.h>
39#include <freeradius-devel/protocol/radius/freeradius.internal.h>
40
42static bool instantiated = false;
43
46
49 { .out = &dict_freeradius, .proto = "freeradius" },
50 { .out = &dict_radius, .proto = "radius" },
51
53};
54
64
67 { .out = &attr_packet_type, .name = "Packet-Type", .type = FR_TYPE_UINT32, .dict = &dict_radius },
68 { .out = &attr_packet_authentication_vector, .name = "Packet-Authentication-Vector", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
69 { .out = &attr_chap_challenge, .name = "CHAP-Challenge", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
70 { .out = &attr_chargeable_user_identity, .name = "Chargeable-User-Identity", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
71
72 { .out = &attr_eap_message, .name = "EAP-Message", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
73 { .out = &attr_message_authenticator, .name = "Message-Authenticator", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
74 { .out = &attr_state, .name = "State", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
75 { .out = &attr_vendor_specific, .name = "Vendor-Specific", .type = FR_TYPE_VSA, .dict = &dict_radius },
76 { .out = &attr_nas_filter_rule, .name = "NAS-Filter-Rule", .type = FR_TYPE_STRING, .dict = &dict_radius },
77
79};
80
81/*
82 * Some messages get printed out only in debugging mode.
83 */
84#define FR_DEBUG_STRERROR_PRINTF if (fr_debug_lvl) fr_strerror_printf
85#define FR_DEBUG_STRERROR_PRINTF_PUSH if (fr_debug_lvl) fr_strerror_printf_push
86
95
104
115
117 "", //!< 0
118 "Access-Request",
119 "Access-Accept",
120 "Access-Reject",
121 "Accounting-Request",
122 "Accounting-Response",
123 "Accounting-Status",
124 "Password-Request",
125 "Password-Accept",
126 "Password-Reject",
127 "Accounting-Message", //!< 10
128 "Access-Challenge",
129 "Status-Server",
130 "Status-Client",
131 "14",
132 "15",
133 "16",
134 "17",
135 "18",
136 "19",
137 "20", //!< 20
138 "Resource-Free-Request",
139 "Resource-Free-Response",
140 "Resource-Query-Request",
141 "Resource-Query-Response",
142 "Alternate-Resource-Reclaim-Request",
143 "NAS-Reboot-Request",
144 "NAS-Reboot-Response",
145 "28",
146 "Next-Passcode",
147 "New-Pin", //!< 30
148 "Terminate-Session",
149 "Password-Expired",
150 "Event-Request",
151 "Event-Response",
152 "35",
153 "36",
154 "37",
155 "38",
156 "39",
157 "Disconnect-Request", //!< 40
158 "Disconnect-ACK",
159 "Disconnect-NAK",
160 "CoA-Request",
161 "CoA-ACK",
162 "CoA-NAK",
163 "46",
164 "47",
165 "48",
166 "49",
167 "IP-Address-Allocate", //!< 50
168 "IP-Address-Release",
169 "Protocol-Error",
170};
171
172
173/** If we get a reply, the request must come from one of a small
174 * number of packet types.
175 */
191
194
195static int dict_flag_encrypt(fr_dict_attr_t **da_p, char const *value, UNUSED fr_dict_flag_parser_rule_t const *rules)
196{
197 static fr_table_num_sorted_t const encrypted[] = {
198 { L("Ascend-Secret"), RADIUS_FLAG_ENCRYPT_ASCEND_SECRET },
199 { L("Tunnel-Password"), RADIUS_FLAG_ENCRYPT_TUNNEL_PASSWORD },
200 { L("User-Password"), RADIUS_FLAG_ENCRYPT_USER_PASSWORD}
201 };
202 static size_t encrypted_len = NUM_ELEMENTS(encrypted);
203
206
208 if (encrypt == RADIUS_FLAG_ENCRYPT_INVALID) {
209 fr_strerror_printf("Unknown encryption type '%s'", value);
210 return -1;
211 }
212
213 flags->encrypt = encrypt;
214
215 return 0;
216}
217
221
223 { L("abinary"), { .func = dict_flag_abinary } },
224 { L("concat"), { .func = dict_flag_concat } },
225 { L("encrypt"), { .func = dict_flag_encrypt, .needs_value = true } },
226 { L("extended"), { .func = dict_flag_extended } },
227 { L("has_tag"), { .func = dict_flag_has_tag } },
228 { L("long_extended"), { .func = dict_flag_long_extended } }
229};
230
231int fr_radius_allow_reply(int code, bool allowed[static FR_RADIUS_CODE_MAX])
232{
233 int i;
234
235 if ((code <= 0) || (code >= FR_RADIUS_CODE_MAX)) return -1;
236
237 for (i = 1; i < FR_RADIUS_CODE_MAX; i++) {
238 allowed[i] |= (allowed_replies[i] == (fr_radius_packet_code_t) code);
239 }
240
241 return 0;
242}
243
244/** Do Ascend-Send / Recv-Secret calculation.
245 *
246 * The secret is hidden by xoring with a MD5 digest created from
247 * the RADIUS shared secret and the authentication vector.
248 * We put them into MD5 in the reverse order from that used when
249 * encrypting passwords to RADIUS.
250 */
252 char const *secret, size_t secret_len, uint8_t const *vector)
253{
254 fr_md5_ctx_t *md5_ctx;
255 size_t i;
257 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
258
259 FR_DBUFF_EXTEND_LOWAT_OR_RETURN(&work_dbuff, sizeof(digest));
260
261 md5_ctx = fr_md5_ctx_alloc_from_list();
263 fr_md5_update(md5_ctx, (uint8_t const *) secret, secret_len);
264 fr_md5_final(digest, md5_ctx);
266
267 if (inlen > sizeof(digest)) inlen = sizeof(digest);
268 for (i = 0; i < inlen; i++) digest[i] ^= in[i];
269
270 fr_dbuff_in_memcpy(&work_dbuff, digest, sizeof(digest));
271
272 return fr_dbuff_set(dbuff, &work_dbuff);
273}
274
275/** Basic validation of RADIUS packet header
276 *
277 * @note fr_strerror errors are only available if fr_debug_lvl > 0. This is to reduce CPU time
278 * consumed when discarding malformed packet.
279 *
280 * @param[in] sockfd we're reading from.
281 * @param[out] src_ipaddr of the packet.
282 * @param[out] src_port of the packet.
283 * @param[out] code Pointer to where to write the packet code.
284 * @return
285 * - -1 on failure.
286 * - 1 on decode error.
287 * - >= RADIUS_HEADER_LENGTH on success. This is the packet length as specified in the header.
288 */
289ssize_t fr_radius_recv_header(int sockfd, fr_ipaddr_t *src_ipaddr, uint16_t *src_port, unsigned int *code)
290{
291 ssize_t data_len, packet_len;
292 uint8_t header[4];
293
294 data_len = udp_recv_peek(sockfd, header, sizeof(header), UDP_FLAGS_PEEK, src_ipaddr, src_port);
295 if (data_len < 0) {
296 if ((errno == EAGAIN) || (errno == EINTR)) return 0;
297 return -1;
298 }
299
300 /*
301 * Too little data is available, discard the packet.
302 */
303 if (data_len < 4) {
304 char buffer[INET6_ADDRSTRLEN];
305
306 FR_DEBUG_STRERROR_PRINTF("Expected at least 4 bytes of header data, got %zd bytes", data_len);
307invalid:
308 FR_DEBUG_STRERROR_PRINTF_PUSH("Invalid data from %s",
309 inet_ntop(src_ipaddr->af, &src_ipaddr->addr, buffer, sizeof(buffer)));
310 (void) udp_recv_discard(sockfd);
311
312 return 0;
313 }
314
315 /*
316 * See how long the packet says it is.
317 */
318 packet_len = (header[2] * 256) + header[3];
319
320 /*
321 * The length in the packet says it's less than
322 * a RADIUS header length: discard it.
323 */
324 if (packet_len < RADIUS_HEADER_LENGTH) {
325 FR_DEBUG_STRERROR_PRINTF("Expected at least " STRINGIFY(RADIUS_HEADER_LENGTH) " bytes of packet "
326 "data, got %zd bytes", packet_len);
327 goto invalid;
328 }
329
330 /*
331 * Enforce RFC requirements, for sanity.
332 * Anything after 4k will be discarded.
333 */
334 if (packet_len > MAX_PACKET_LEN) {
335 FR_DEBUG_STRERROR_PRINTF("Length field value too large, expected maximum of "
336 STRINGIFY(MAX_PACKET_LEN) " bytes, got %zd bytes", packet_len);
337 goto invalid;
338 }
339
340 *code = header[0];
341
342 /*
343 * The packet says it's this long, but the actual UDP
344 * size could still be smaller.
345 */
346 return packet_len;
347}
348
349/** Sign a previously encoded packet
350 *
351 * Calculates the request/response authenticator for packets which need it, and fills
352 * in the message-authenticator value if the attribute is present in the encoded packet.
353 *
354 * @param[in,out] packet (request or response).
355 * @param[in] vector original packet vector to use
356 * @param[in] secret to sign the packet with.
357 * @param[in] secret_len The length of the secret.
358 * @return
359 * - <0 on error
360 * - 0 on success
361 */
362int fr_radius_sign(uint8_t *packet, uint8_t const *vector,
363 uint8_t const *secret, size_t secret_len)
364{
365 uint8_t *msg, *end;
366 size_t packet_len = fr_nbo_to_uint16(packet + 2);
367
368 /*
369 * No real limit on secret length, this is just
370 * to catch uninitialised fields.
371 */
372 if (!fr_cond_assert(secret_len <= UINT16_MAX)) {
373 fr_strerror_printf("Secret is too long. Expected <= %u, got %zu",
374 (unsigned int) UINT16_MAX, secret_len);
375 return -1;
376 }
377
378 if (packet_len < RADIUS_HEADER_LENGTH) {
379 fr_strerror_const("Packet must be encoded before calling fr_radius_sign()");
380 return -1;
381 }
382
383 /*
384 * Find Message-Authenticator. Its value has to be
385 * calculated before we calculate the Request
386 * Authenticator or the Response Authenticator.
387 */
388 msg = packet + RADIUS_HEADER_LENGTH;
389 end = packet + packet_len;
390
391 while (msg < end) {
392 if ((end - msg) < 2) goto invalid_attribute;
393
394 if (msg[0] != FR_MESSAGE_AUTHENTICATOR) {
395 if (msg[1] < 2) goto invalid_attribute;
396
397 if ((msg + msg[1]) > end) {
398 invalid_attribute:
399 fr_strerror_printf("Invalid attribute at offset %zd", msg - packet);
400 return -1;
401 }
402 msg += msg[1];
403 continue;
404 }
405
406 if (msg[1] < 18) {
407 fr_strerror_const("Message-Authenticator is too small");
408 return -1;
409 }
410
411 switch (packet[0]) {
415 memset(packet + 4, 0, RADIUS_AUTH_VECTOR_LENGTH);
416 break;
417
427 if (!vector) goto need_original;
428 memcpy(packet + 4, vector, RADIUS_AUTH_VECTOR_LENGTH);
429 break;
430
433 /* packet + 4 MUST be the Request Authenticator filled with random data */
434 break;
435
436 default:
437 goto bad_packet;
438 }
439
440 /*
441 * Force Message-Authenticator to be zero,
442 * calculate the HMAC, and put it into the
443 * Message-Authenticator attribute.
444 */
445 memset(msg + 2, 0, RADIUS_AUTH_VECTOR_LENGTH);
446 fr_hmac_md5(msg + 2, packet, packet_len, secret, secret_len);
447 break;
448 }
449
450 /*
451 * Initialize the request authenticator.
452 */
453 switch (packet[0]) {
457 memset(packet + 4, 0, RADIUS_AUTH_VECTOR_LENGTH);
458 break;
459
469 if (!vector) {
470 need_original:
471 fr_strerror_const("Cannot sign response packet without a request packet");
472 return -1;
473 }
474 memcpy(packet + 4, vector, RADIUS_AUTH_VECTOR_LENGTH);
475 break;
476
477 /*
478 * The Request Authenticator is random numbers.
479 * We don't need to sign anything else, so
480 * return.
481 */
484 return 0;
485
486 default:
487 bad_packet:
488 fr_strerror_printf("Cannot sign unknown packet code %u", packet[0]);
489 return -1;
490 }
491
492 /*
493 * Request / Response Authenticator = MD5(packet + secret)
494 */
495 {
496 fr_md5_ctx_t *md5_ctx;
497
498 md5_ctx = fr_md5_ctx_alloc_from_list();
499 fr_md5_update(md5_ctx, packet, packet_len);
500 fr_md5_update(md5_ctx, secret, secret_len);
501 fr_md5_final(packet + 4, md5_ctx);
503 }
504
505 return 0;
506}
507
509 [FR_RADIUS_FAIL_NONE] = "none",
510 [FR_RADIUS_FAIL_MIN_LENGTH_PACKET] = "packet is smaller than the minimum packet length",
511 [FR_RADIUS_FAIL_MAX_LENGTH_PACKET] = "packet is larger than the maximum packet length",
512 [FR_RADIUS_FAIL_MIN_LENGTH_FIELD] = "header 'length' field has a value smaller than the minimum packet length",
513 [FR_RADIUS_FAIL_MIN_LENGTH_MISMATCH] = "header 'length' field has a value larger than the received data",
514 [FR_RADIUS_FAIL_UNKNOWN_PACKET_CODE] = "unknown packet code",
515 [FR_RADIUS_FAIL_UNEXPECTED_REQUEST_CODE] = "unexpected request code",
516 [FR_RADIUS_FAIL_UNEXPECTED_RESPONSE_CODE] = "unexpected response code",
517 [FR_RADIUS_FAIL_TOO_MANY_ATTRIBUTES] = "packet contains too many attributes",
518
519 [FR_RADIUS_FAIL_INVALID_ATTRIBUTE] = "attribute number 0 is invalid",
520
521 [FR_RADIUS_FAIL_HEADER_OVERFLOW] = "attribute header overflows the packet",
522 [FR_RADIUS_FAIL_ATTRIBUTE_TOO_SHORT] = "attribute 'length' field contains invalid value",
523 [FR_RADIUS_FAIL_ATTRIBUTE_OVERFLOW] = "attribute 'length' field overflows the packet",
524 [FR_RADIUS_FAIL_ATTRIBUTE_DECODE] = "unable to decode attributes",
525
526 [FR_RADIUS_FAIL_MA_INVALID_LENGTH] = "Message-Authenticate has invalid length",
527 [FR_RADIUS_FAIL_MA_MISSING] = "Message-Authenticator is required for this packet, but it is missing",
528 [FR_RADIUS_FAIL_MA_INVALID] = "Message-Authenticator fails verification. shared secret is incorrect",
529 [FR_RADIUS_FAIL_PROXY_STATE_MISSING] = "Proxy-State is required for this request, but it is missing",
530
531 [FR_RADIUS_FAIL_VERIFY] = "packet fails verification, shared secret is incorrect",
532 [FR_RADIUS_FAIL_NO_MATCHING_REQUEST] = "did not find request which matched response",
533 [FR_RADIUS_FAIL_IO_ERROR] = "IO error",
534 [FR_RADIUS_FAIL_MAX] = "???",
535};
536
537/** See if the data pointed to by PTR is a valid RADIUS packet.
538 *
539 * @param[in] packet to check.
540 * @param[in,out] packet_len_p The size of the packet data.
541 * @param[in] max_attributes to allow in the packet.
542 * @param[in] require_message_authenticator whether we require Message-Authenticator.
543 * @param[in] reason if not NULL, will have the failure reason written to where it points.
544 * @return
545 * - True on success.
546 * - False on failure.
547 */
548bool fr_radius_ok(uint8_t const *packet, size_t *packet_len_p,
549 uint32_t max_attributes, bool require_message_authenticator, fr_radius_decode_fail_t *reason)
550{
551 uint8_t const *attr, *end;
552 size_t totallen;
553 bool seen_ma = false;
554 uint32_t num_attributes;
556 size_t packet_len = *packet_len_p;
557
558 /*
559 * Check for packets smaller than the packet header.
560 *
561 * RFC 2865, Section 3., subsection 'length' says:
562 *
563 * "The minimum length is 20 ..."
564 */
565 if (packet_len < RADIUS_HEADER_LENGTH) {
567 goto finish;
568 }
569
570
571 /*
572 * Check for packets with mismatched size.
573 * i.e. We've received 128 bytes, and the packet header
574 * says it's 256 bytes long.
575 */
576 totallen = fr_nbo_to_uint16(packet + 2);
577
578 /*
579 * Code of 0 is not understood.
580 * Code of 16 or greater is not understood.
581 */
582 if ((packet[0] == 0) ||
583 (packet[0] >= FR_RADIUS_CODE_MAX)) {
585 goto finish;
586 }
587
588 switch (packet[0]) {
589 /*
590 * Message-Authenticator is required in Status-Server
591 * packets, otherwise they can be trivially forged.
592 */
594 require_message_authenticator = true;
595 break;
596
597 /*
598 * Message-Authenticator may or may not be
599 * required for Access-* packets.
600 */
606 break;
607
608 /*
609 * Message-Authenticator is not required for all other packets, but is required if the
610 * caller asks for it.
611 */
612 default:
613 break;
614 }
615
616 /*
617 * Repeat the length checks. This time, instead of
618 * looking at the data we received, look at the value
619 * of the 'length' field inside of the packet.
620 *
621 * Check for packets smaller than the packet header.
622 *
623 * RFC 2865, Section 3., subsection 'length' says:
624 *
625 * "The minimum length is 20 ..."
626 */
627 if (totallen < RADIUS_HEADER_LENGTH) {
629 goto finish;
630 }
631
632 /*
633 * And again, for the value of the 'length' field.
634 *
635 * RFC 2865, Section 3., subsection 'length' says:
636 *
637 * " ... and maximum length is 4096."
638 *
639 * HOWEVER. This requirement is for the network layer.
640 * If the code gets here, we assume that a well-formed
641 * packet is an OK packet.
642 *
643 * We allow both the UDP data length, and the RADIUS
644 * "length" field to contain up to 64K of data.
645 */
646
647 /*
648 * RFC 2865, Section 3., subsection 'length' says:
649 *
650 * "If the packet is shorter than the Length field
651 * indicates, it MUST be silently discarded."
652 *
653 * i.e. No response to the NAS.
654 */
655 if (totallen > packet_len) {
657 goto finish;
658 }
659
660 /*
661 * RFC 2865, Section 3., subsection 'length' says:
662 *
663 * "Octets outside the range of the Length field MUST be
664 * treated as padding and ignored on reception."
665 */
666 if (totallen < packet_len) {
667 *packet_len_p = packet_len = totallen;
668 }
669
670 /*
671 * Walk through the packet's attributes, ensuring that
672 * they add up EXACTLY to the size of the packet.
673 *
674 * If they don't, then the attributes either under-fill
675 * or over-fill the packet. Any parsing of the packet
676 * is impossible, and will result in unknown side effects.
677 *
678 * This would ONLY happen with buggy RADIUS implementations,
679 * or with an intentional attack. Either way, we do NOT want
680 * to be vulnerable to this problem.
681 */
682 attr = packet + RADIUS_HEADER_LENGTH;
683 end = packet + packet_len;
684 num_attributes = 0;
685
686 while (attr < end) {
687 /*
688 * We need at least 2 bytes to check the
689 * attribute header.
690 */
691 if ((end - attr) < 2) {
693 goto finish;
694 }
695
696 /*
697 * Attribute number zero is NOT defined.
698 */
699 if (attr[0] == 0) {
701 goto finish;
702 }
703
704 /*
705 * Attributes are at LEAST as long as the ID & length
706 * fields. Anything shorter is an invalid attribute.
707 */
708 if (attr[1] < 2) {
710 goto finish;
711 }
712
713 /*
714 * If there are fewer bytes in the packet than in the
715 * attribute, it's a bad packet.
716 */
717 if ((attr + attr[1]) > end) {
719 goto finish;
720 }
721
722 /*
723 * Sanity check the attributes for length.
724 */
725 switch (attr[0]) {
726 default: /* don't do anything by default */
727 break;
728
729 /*
730 * If there's an EAP-Message, we require
731 * a Message-Authenticator.
732 */
733 case FR_EAP_MESSAGE:
734 require_message_authenticator = true;
735 break;
736
737 case FR_MESSAGE_AUTHENTICATOR:
738 if (attr[1] != 2 + RADIUS_AUTH_VECTOR_LENGTH) {
740 goto finish;
741 }
742 seen_ma = true;
743 break;
744 }
745
746 attr += attr[1];
747 num_attributes++; /* seen one more attribute */
748 }
749
750 /*
751 * If we're configured to look for a maximum number of
752 * attributes, and we've seen more than that maximum,
753 * then throw the packet away, as a possible DoS.
754 */
755 if (num_attributes > max_attributes) {
757 goto finish;
758 }
759
760 /*
761 * http://www.freeradius.org/rfc/rfc2869.html#EAP-Message
762 *
763 * A packet with an EAP-Message attribute MUST also have
764 * a Message-Authenticator attribute.
765 *
766 * A Message-Authenticator all by itself is OK, though.
767 *
768 * Similarly, Status-Server packets MUST contain
769 * Message-Authenticator attributes.
770 */
771 if (require_message_authenticator && !seen_ma) {
773 goto finish;
774 }
775
776finish:
777
778 if (reason) *reason = failure;
779
780 return (failure == FR_RADIUS_FAIL_NONE);
781}
782
783
784/** Verify a request / response packet
785 *
786 * This function does its work by calling fr_radius_sign(), and then
787 * comparing the signature in the packet with the one we calculated.
788 * If they differ, there's a problem.
789 *
790 * @param[in] packet the raw RADIUS packet (request or response)
791 * @param[in] vector the original packet vector
792 * @param[in] secret the shared secret
793 * @param[in] secret_len the length of the secret
794 * @param[in] require_message_authenticator whether we require Message-Authenticator.
795 * @param[in] limit_proxy_state whether we allow Proxy-State without Message-Authenticator.
796 * @return
797 * < <0 on error (negative fr_radius_decode_fail_t)
798 * - 0 on success.
799 */
800int fr_radius_verify(uint8_t *packet, uint8_t const *vector,
801 uint8_t const *secret, size_t secret_len,
802 bool require_message_authenticator, bool limit_proxy_state)
803{
804 bool found_message_authenticator = false;
805 bool found_proxy_state = false;
806 int rcode;
807 int code;
808 uint8_t *msg, *end;
809 size_t packet_len = fr_nbo_to_uint16(packet + 2);
810 uint8_t request_authenticator[RADIUS_AUTH_VECTOR_LENGTH];
811 uint8_t message_authenticator[RADIUS_AUTH_VECTOR_LENGTH];
812
813 if (packet_len < RADIUS_HEADER_LENGTH) {
814 fr_strerror_printf("invalid packet length %zu", packet_len);
816 }
817
818 code = packet[0];
819 if (!code || (code >= FR_RADIUS_CODE_MAX)) {
820 fr_strerror_printf("Unknown reply code %d", code);
822 }
823
824 memcpy(request_authenticator, packet + 4, sizeof(request_authenticator));
825
826 /*
827 * Find Message-Authenticator. Its value has to be
828 * calculated before we calculate the Request
829 * Authenticator or the Response Authenticator.
830 */
831 msg = packet + RADIUS_HEADER_LENGTH;
832 end = packet + packet_len;
833
834 while (msg < end) {
835 if ((end - msg) < 2) goto invalid_attribute;
836
837 if (msg[0] != FR_MESSAGE_AUTHENTICATOR) {
838 if (msg[1] < 2) goto invalid_attribute;
839
840 /*
841 * If we're not allowing Proxy-State without
842 * Message-authenticator, we need to record
843 * the fact we found Proxy-State.
844 */
845 if (limit_proxy_state && (msg[0] == FR_PROXY_STATE)) found_proxy_state = true;
846
847 if ((msg + msg[1]) > end) {
848 invalid_attribute:
849 fr_strerror_printf("invalid attribute at offset %zd", msg - packet);
851 }
852 msg += msg[1];
853 continue;
854 }
855
856 if (msg[1] < 18) {
857 fr_strerror_const("too small Message-Authenticator");
859 }
860
861 /*
862 * Found it, save a copy.
863 */
864 memcpy(message_authenticator, msg + 2, sizeof(message_authenticator));
865 found_message_authenticator = true;
866 break;
867 }
868
869 if (packet[0] == FR_RADIUS_CODE_ACCESS_REQUEST) {
870 if (limit_proxy_state && found_proxy_state && !found_message_authenticator) {
871 fr_strerror_const("Proxy-State is not allowed without Message-Authenticator");
873 }
874
875 if (require_message_authenticator && !found_message_authenticator) {
876 fr_strerror_const("Access-Request is missing the required Message-Authenticator attribute");
878 }
879 }
880
881 /*
882 * Overwrite the contents of Message-Authenticator
883 * with the one we calculate.
884 */
885 rcode = fr_radius_sign(packet, vector, secret, secret_len);
886 if (rcode < 0) {
887 fr_strerror_const_push("Failed calculating correct authenticator");
888 return -FR_RADIUS_FAIL_VERIFY;
889 }
890
891 /*
892 * Check the Message-Authenticator first.
893 *
894 * If it's invalid, restore the original
895 * Message-Authenticator and Request Authenticator
896 * fields.
897 *
898 * If it's valid the original and calculated
899 * message authenticators are the same, so we don't
900 * need to do anything.
901 */
902 if ((msg < end) &&
903 (fr_digest_cmp(message_authenticator, msg + 2, sizeof(message_authenticator)) != 0)) {
904 memcpy(msg + 2, message_authenticator, sizeof(message_authenticator));
905 memcpy(packet + 4, request_authenticator, sizeof(request_authenticator));
906
907 fr_strerror_const("invalid Message-Authenticator (shared secret is incorrect)");
909 }
910
911 /*
912 * These are random numbers, so there's no point in
913 * comparing them.
914 */
915 if ((packet[0] == FR_RADIUS_CODE_ACCESS_REQUEST) || (packet[0] == FR_RADIUS_CODE_STATUS_SERVER)) {
916 return 0;
917 }
918
919 /*
920 * Check the Request Authenticator.
921 */
922 if (fr_digest_cmp(request_authenticator, packet + 4, sizeof(request_authenticator)) != 0) {
923 memcpy(packet + 4, request_authenticator, sizeof(request_authenticator));
924 if (vector) {
925 fr_strerror_const("invalid Response Authenticator (shared secret is incorrect)");
926 } else {
927 fr_strerror_const("invalid Request Authenticator (shared secret is incorrect)");
928 }
929 return -FR_RADIUS_FAIL_VERIFY;
930 }
931
932 return 0;
933}
934
935void *fr_radius_next_encodable(fr_dcursor_t *cursor, void *current, void *uctx);
936
937void *fr_radius_next_encodable(fr_dcursor_t *cursor, void *current, void *uctx)
938{
939 fr_pair_t *c = current;
940 fr_dict_t *dict = talloc_get_type_abort(uctx, fr_dict_t);
941
942 while ((c = fr_dlist_next(cursor->dlist, c))) {
943 PAIR_VERIFY(c);
944 if ((c->da->dict == dict) &&
945 (!c->da->flags.internal || ((c->da->attr > FR_TAG_BASE) && (c->da->attr < (FR_TAG_BASE + 0x20))))) {
946 break;
947 }
948 }
949
950 return c;
951}
952
953
955{
956 ssize_t slen;
957 fr_pair_t const *vp;
958 fr_dcursor_t cursor;
959 fr_dbuff_t work_dbuff, length_dbuff;
960
961 /*
962 * The RADIUS header can't do more than 64K of data.
963 */
964 work_dbuff = FR_DBUFF_MAX(dbuff, 65535);
965
966 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, packet_ctx->code, packet_ctx->id);
967 length_dbuff = FR_DBUFF(&work_dbuff);
969
970 switch (packet_ctx->code) {
973 packet_ctx->request_authenticator = fr_dbuff_current(&work_dbuff);
974
975 /*
976 * Allow over-rides of the authentication vector for testing.
977 */
979 if (vp && (vp->vp_length >= RADIUS_AUTH_VECTOR_LENGTH)) {
981 } else {
982 int i;
983
984 for (i = 0; i < 4; i++) {
985 FR_DBUFF_IN_RETURN(&work_dbuff, (uint32_t) fr_rand());
986 }
987 }
988 break;
989
999 if (!packet_ctx->request_authenticator) {
1000 fr_strerror_const("Cannot encode response without request");
1001 return -1;
1002 }
1004 break;
1005
1008 /*
1009 * Tunnel-Password encoded attributes are allowed
1010 * in CoA-Request packets, by RFC 5176 Section
1011 * 3.6. HOWEVER, the tunnel passwords are
1012 * "encrypted" using the Request Authenticator,
1013 * which is all zeros! That makes them much
1014 * easier to decrypt. The only solution here is
1015 * to say "don't do that!"
1016 */
1018 packet_ctx->request_authenticator = fr_dbuff_current(&work_dbuff);
1019
1021 break;
1022
1023 default:
1024 fr_strerror_printf("Cannot encode unknown packet code %d", packet_ctx->code);
1025 return -1;
1026 }
1027
1028 /*
1029 * Always add Message-Authenticator after the packet
1030 * header for insecure transport protocols.
1031 */
1032 if (!packet_ctx->common->secure_transport) switch (packet_ctx->code) {
1036#ifdef NAS_VIOLATES_RFC
1037 /*
1038 * Allow ridiculous behavior for vendors who violate the RFCs.
1039 *
1040 * But only if there's no EAP-Message in the packet.
1041 */
1042 if (packet_ctx->allow_vulnerable_clients && !fr_pair_find_by_da(vps, NULL, attr_eap_message)) {
1043 break;
1044 }
1046#endif
1047
1051 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, FR_MESSAGE_AUTHENTICATOR, 0x12,
1052 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1053 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
1054 packet_ctx->seen_message_authenticator = true;
1055 break;
1056
1057 default:
1058 break;
1059 }
1060
1061 /*
1062 * If we're sending Protocol-Error, add in
1063 * Original-Packet-Code manually. If the user adds it
1064 * later themselves, well, too bad.
1065 */
1066 if (packet_ctx->code == FR_RADIUS_CODE_PROTOCOL_ERROR) {
1067 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, FR_EXTENDED_ATTRIBUTE_1, 0x07, 0x04 /* Original-Packet-Code */,
1068 0x00, 0x00, 0x00, packet_ctx->request_code);
1069 }
1070
1071 /*
1072 * Loop over the reply attributes for the packet.
1073 */
1075 while ((vp = fr_dcursor_current(&cursor))) {
1076 PAIR_VERIFY(vp);
1077
1078 /*
1079 * Encode an individual VP
1080 */
1081 slen = fr_radius_encode_pair(&work_dbuff, &cursor, packet_ctx);
1082 if (slen < 0) return slen;
1083 } /* done looping over all attributes */
1084
1085 /*
1086 * Add Proxy-State to the end of the packet if the caller requested it.
1087 */
1088 if (packet_ctx->add_proxy_state) {
1089 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, FR_PROXY_STATE, (uint8_t) (2 + sizeof(packet_ctx->common->proxy_state)));
1090 FR_DBUFF_IN_RETURN(&work_dbuff, packet_ctx->common->proxy_state);
1091 }
1092
1093 /*
1094 * Fill in the length field we zeroed out earlier.
1095 *
1096 */
1097 fr_dbuff_in(&length_dbuff, (uint16_t) (fr_dbuff_used(&work_dbuff)));
1098
1099 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "%s encoded packet", __FUNCTION__);
1100
1101 return fr_dbuff_set(dbuff, &work_dbuff);
1102}
1103
1105 uint8_t *packet, size_t packet_len,
1106 fr_radius_decode_ctx_t *decode_ctx)
1107{
1108 ssize_t slen;
1109 uint8_t const *attr, *end;
1110 static const uint8_t zeros[RADIUS_AUTH_VECTOR_LENGTH] = {};
1111
1112 decode_ctx->reason = FR_RADIUS_FAIL_NONE;
1113
1114 if (!decode_ctx->request_authenticator) {
1115 switch (packet[0]) {
1118 decode_ctx->request_authenticator = packet + 4;
1119 break;
1120
1124 decode_ctx->request_authenticator = zeros;
1125 break;
1126
1127 default:
1128 fr_strerror_const("No authentication vector passed for packet decode");
1130 return -1;
1131 }
1132 }
1133
1134 if (decode_ctx->request_code) {
1135 unsigned int code = packet[0];
1136
1137 if (code >= FR_RADIUS_CODE_MAX) {
1139 return -1;
1140 }
1141 if (decode_ctx->request_code >= FR_RADIUS_CODE_MAX) {
1143 return -1;
1144 }
1145
1146 if (!allowed_replies[code]) {
1148 return -1;
1149 }
1150
1151 /*
1152 * Protocol error can reply to any packet.
1153 *
1154 * Status-Server can get any reply.
1155 *
1156 * Otherwise the reply code must be associated with the request code we sent.
1157 */
1158 if ((allowed_replies[code] != decode_ctx->request_code) &&
1160 (decode_ctx->request_code != FR_RADIUS_CODE_STATUS_SERVER)) {
1162 return -1;
1163 }
1164 }
1165
1166 /*
1167 * We can skip verification for dynamic client checks, and where packets are unsigned as with
1168 * RADIUS/1.1.
1169 */
1170 if (decode_ctx->verify) {
1171 if (!decode_ctx->request_authenticator) decode_ctx->request_authenticator = zeros;
1172
1173 if (fr_radius_verify(packet, decode_ctx->request_authenticator,
1174 (uint8_t const *) decode_ctx->common->secret, decode_ctx->common->secret_length,
1175 decode_ctx->require_message_authenticator, decode_ctx->limit_proxy_state) < 0) {
1176 decode_ctx->reason = FR_RADIUS_FAIL_VERIFY;
1177 return -1;
1178 }
1179 }
1180
1181 attr = packet + 20;
1182 end = packet + packet_len;
1183
1184 /*
1185 * The caller MUST have called fr_radius_ok() first. If
1186 * he doesn't, all hell breaks loose.
1187 */
1188 while (attr < end) {
1189 slen = fr_radius_decode_pair(ctx, out, attr, (end - attr), decode_ctx);
1190 if (slen < 0) {
1192 return slen;
1193 }
1194
1195 /*
1196 * If slen is larger than the room in the packet,
1197 * all kinds of bad things happen.
1198 */
1199 if (!fr_cond_assert(slen <= (end - attr))) {
1200 return -slen;
1201 }
1202
1203 attr += slen;
1204 talloc_free_children(decode_ctx->tmp_ctx);
1205 }
1206
1207 /*
1208 * We've parsed the whole packet, return that.
1209 */
1210 return packet_len;
1211}
1212
1213/** Simple wrapper for callers who just need a shared secret
1214 *
1215 */
1217 uint8_t *packet, size_t packet_len,
1218 uint8_t const *vector, char const *secret)
1219{
1220 ssize_t rcode;
1221 fr_radius_ctx_t common_ctx = {};
1222 fr_radius_decode_ctx_t packet_ctx = {};
1223
1224 common_ctx.secret = secret;
1225 common_ctx.secret_length = strlen(secret);
1226
1227 packet_ctx.common = &common_ctx;
1228 packet_ctx.tmp_ctx = talloc(ctx, uint8_t);
1229 packet_ctx.request_authenticator = vector;
1230 packet_ctx.end = packet + packet_len;
1231
1232 rcode = fr_radius_decode(ctx, out, packet, packet_len, &packet_ctx);
1233 talloc_free(packet_ctx.tmp_ctx);
1234
1235 return rcode;
1236}
1237
1239{
1240 if (instance_count > 0) {
1242 return 0;
1243 }
1244
1246
1248 fail:
1250 return -1;
1251 }
1252
1255 goto fail;
1256 }
1257
1258 instantiated = true;
1259 return 0;
1260}
1261
1263{
1264 if (!instantiated) return;
1265
1266 if (--instance_count != 0) return;
1267
1269}
1270
1272{
1274
1275 if (da->parent->type == FR_TYPE_STRUCT) {
1276 if (flags->extended) {
1277 fr_strerror_const("Attributes with 'extended' flag cannot be used inside of a 'struct'");
1278 return false;
1279 }
1280
1281 if (flags->long_extended) {
1282 fr_strerror_const("Attributes with 'long_extended' flag cannot be used inside of a 'struct'");
1283 return false;
1284 }
1285
1286
1287 if (flags->concat) {
1288 fr_strerror_const("Attributes with 'concat' flag cannot be used inside of a 'struct'");
1289 return false;
1290 }
1291
1292 if (flags->has_tag) {
1293 fr_strerror_const("Attributes with 'tag' flag cannot be used inside of a 'struct'");
1294 return false;
1295 }
1296
1297 if (flags->abinary) {
1298 fr_strerror_const("Attributes with 'abinary' flag cannot be used inside of a 'struct'");
1299 return false;
1300 }
1301
1302 if (flags->encrypt > 0) {
1303 fr_strerror_const("Attributes with 'encrypt' flag cannot be used inside of a 'struct'");
1304 return false;
1305 }
1306
1307 return true;
1308 }
1309
1310 if (da->flags.length > 253) {
1311 fr_strerror_printf("Attributes cannot be more than 253 octets in length");
1312 return false;
1313 }
1314 /*
1315 * Secret things are secret.
1316 */
1317 if (flags->encrypt != 0) da->flags.secret = true;
1318
1319 if (flags->concat) {
1320 if (!da->parent->flags.is_root) {
1321 fr_strerror_const("Attributes with the 'concat' flag MUST be at the root of the dictionary");
1322 return false;
1323 }
1324
1325 if (da->type != FR_TYPE_OCTETS) {
1326 fr_strerror_const("Attributes with the 'concat' flag MUST be of data type 'octets'");
1327 return false;
1328 }
1329
1330 return true; /* can't use any other flag */
1331 }
1332
1333 /*
1334 * Tagged attributes can only be of two data types. They
1335 * can, however, be VSAs.
1336 */
1337 if (flags->has_tag) {
1338 if ((da->type != FR_TYPE_UINT32) && (da->type != FR_TYPE_STRING)) {
1339 fr_strerror_printf("The 'has_tag' flag can only be used for attributes of type 'integer' "
1340 "or 'string'");
1341 return false;
1342 }
1343
1344 if (!(da->parent->flags.is_root ||
1345 ((da->parent->type == FR_TYPE_VENDOR) &&
1346 (da->parent->parent && da->parent->parent->type == FR_TYPE_VSA)))) {
1347 fr_strerror_const("The 'has_tag' flag can only be used with RFC and VSA attributes");
1348 return false;
1349 }
1350
1351 return true;
1352 }
1353
1354 if (flags->extended) {
1355 if (da->type != FR_TYPE_TLV) {
1356 fr_strerror_const("The 'long' or 'extended' flag can only be used for attributes of type 'tlv'");
1357 return false;
1358 }
1359
1360 if (!da->parent->flags.is_root) {
1361 fr_strerror_const("The 'long' flag can only be used for top-level RFC attributes");
1362 return false;
1363 }
1364
1365 return true;
1366 }
1367
1368 /*
1369 * Stupid hacks for MS-CHAP-MPPE-Keys. The User-Password
1370 * encryption method has no provisions for encoding the
1371 * length of the data. For User-Password, the data is
1372 * (presumably) all printable non-zero data. For
1373 * MS-CHAP-MPPE-Keys, the data is binary crap. So... we
1374 * MUST specify a length in the dictionary.
1375 */
1376 if ((flags->encrypt == RADIUS_FLAG_ENCRYPT_USER_PASSWORD) && (da->type != FR_TYPE_STRING)) {
1377 if (da->type != FR_TYPE_OCTETS) {
1378 fr_strerror_printf("The 'encrypt=User-Password' flag can only be used with "
1379 "attributes of type 'string'");
1380 return false;
1381 }
1382
1383 if (da->flags.length == 0) {
1384 fr_strerror_printf("The 'encrypt=User-Password' flag MUST be used with an explicit length for "
1385 "'octets' data types");
1386 return false;
1387 }
1388 }
1389
1390 switch (da->type) {
1391 case FR_TYPE_STRING:
1392 break;
1393
1394 case FR_TYPE_TLV:
1395 case FR_TYPE_IPV4_ADDR:
1396 case FR_TYPE_UINT32:
1397 case FR_TYPE_OCTETS:
1398 if (flags->encrypt != RADIUS_FLAG_ENCRYPT_ASCEND_SECRET) break;
1400
1401 default:
1402 if (flags->encrypt) {
1403 fr_strerror_printf("The 'encrypt' flag cannot be used with attributes of type '%s'",
1404 fr_type_to_str(da->type));
1405 return false;
1406 }
1407 }
1408
1409 return true;
1410}
1411
1414 .name = "radius",
1415 .default_type_size = 1,
1416 .default_type_length = 1,
1417 .attr = {
1418 .flags = {
1419 .table = radius_flags,
1420 .table_len = NUM_ELEMENTS(radius_flags),
1421 .len = sizeof(fr_radius_attr_flags_t),
1422 },
1423 .valid = attr_valid,
1424 },
1425
1426 .init = fr_radius_global_init,
1427 .free = fr_radius_global_free,
1428
1429 .decode = fr_radius_decode_foreign,
1430 .encode = fr_radius_encode_foreign,
1431};
static int const char char buffer[256]
Definition acutest.h:578
log_entry msg
Definition acutest.h:796
#define RCSID(id)
Definition build.h:487
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:209
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:324
#define STRINGIFY(x)
Definition build.h:197
#define UNUSED
Definition build.h:317
#define NUM_ELEMENTS(_t)
Definition build.h:339
#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:777
#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:683
#define fr_dbuff_current(_dbuff_or_marker)
Return the 'current' position of a dbuff or marker.
Definition dbuff.h:921
#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:1014
#define fr_dbuff_start(_dbuff_or_marker)
Return the 'start' position of a dbuff or marker.
Definition dbuff.h:908
#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:1519
#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:1393
#define fr_dbuff_in_memcpy(_dbuff_or_marker, _in, _inlen)
Copy exactly _inlen bytes into a dbuff or marker.
Definition dbuff.h:1361
#define fr_dbuff_in(_dbuff_or_marker, _in)
Copy data from a fixed sized C type into a dbuff or marker.
Definition dbuff.h:1578
#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:1596
#define FR_DBUFF(_dbuff_or_marker)
Create a new dbuff pointing to the same underlying buffer.
Definition dbuff.h:232
#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:311
#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:1483
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition dcursor.h:339
fr_dlist_head_t * dlist
Head of the doubly linked list being iterated over.
Definition dcursor.h:94
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:131
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:56
#define fr_dict_autofree(_to_free)
Definition dict.h:917
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
Definition dict.h:294
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:307
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:4402
@ FR_DICT_ATTR_EXT_PROTOCOL_SPECIFIC
Protocol specific extensions.
Definition dict.h:192
#define fr_dict_autoload(_to_load)
Definition dict.h:914
#define DICT_AUTOLOAD_TERMINATOR
Definition dict.h:313
static fr_slen_t in
Definition dict.h:884
char const * name
name of this protocol
Definition dict.h:458
#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:437
Specifies an attribute which must be present for the module to function.
Definition dict.h:293
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:306
Protocol specific custom flag definitnion.
Definition dict.h:427
Protocol-specific callbacks in libfreeradius-PROTOCOL.
Definition dict.h:457
static void * fr_dict_attr_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
Definition dict_ext.h:121
Test enumeration values.
Definition dict_test.h:92
static void * fr_dlist_next(fr_dlist_head_t const *list_head, void const *ptr)
Get the next item in a list.
Definition dlist.h:537
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
int af
Address family.
Definition inet.h:64
union fr_ipaddr_t::@137 addr
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:93
fr_dict_attr_t const * attr_state
Definition base.c:103
fr_dict_attr_t const * attr_eap_message
Definition base.c:96
fr_dict_t const * dict_radius
Definition base.c:78
fr_dict_attr_t const * attr_message_authenticator
Definition base.c:94
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:227
static uint32_t instance_count
Definition base.c:44
int 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:509
fr_md5_ctx_t * fr_md5_ctx_alloc_from_list(void)
Allocation function for MD5 digest context.
Definition md5.c:464
#define fr_md5_final(_out, _ctx)
Finalise the ctx, producing the digest.
Definition md5.h:93
void fr_md5_ctx_t
Definition md5.h:28
#define fr_md5_update(_ctx, _in, _inlen)
Ingest plaintext into the digest.
Definition md5.h:86
#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:505
char const * inet_ntop(int af, void const *src, char *dst, size_t cnt)
Definition missing.c:443
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
#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:704
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:721
fr_dict_protocol_t libfreeradius_radius_dict_protocol
Definition base.c:1413
fr_dict_autoload_t libfreeradius_radius_dict[]
Definition base.c:48
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:251
fr_dict_attr_t const * attr_nas_filter_rule
Definition base.c:63
static fr_dict_flag_parser_t const radius_flags[]
Definition base.c:222
fr_dict_attr_t const * attr_packet_authentication_vector
Definition base.c:56
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:176
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:1104
size_t fr_radius_limit_proxy_state_table_len
Definition base.c:103
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:362
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 a request / response packet.
Definition base.c:800
fr_dict_attr_autoload_t libfreeradius_radius_dict_attr[]
Definition base.c:66
char const * fr_radius_decode_fail_reason[FR_RADIUS_FAIL_MAX+1]
Definition base.c:508
size_t fr_radius_require_ma_table_len
Definition base.c:94
fr_dict_attr_t const * attr_chap_challenge
Definition base.c:57
void * fr_radius_next_encodable(fr_dcursor_t *cursor, void *current, void *uctx)
Definition base.c:937
fr_dict_attr_t const * attr_vendor_specific
Definition base.c:62
int fr_radius_global_init(void)
Definition base.c:1238
size_t fr_radius_request_name_table_len
Definition base.c:114
#define FR_DEBUG_STRERROR_PRINTF
Definition base.c:84
fr_dict_attr_t const * attr_chargeable_user_identity
Definition base.c:58
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:1216
void fr_radius_global_free(void)
Definition base.c:1262
fr_table_num_sorted_t const fr_radius_limit_proxy_state_table[]
Definition base.c:96
fr_table_num_sorted_t const fr_radius_request_name_table[]
Definition base.c:105
fr_table_num_sorted_t const fr_radius_require_ma_table[]
Definition base.c:87
bool fr_radius_ok(uint8_t const *packet, size_t *packet_len_p, uint32_t max_attributes, bool require_message_authenticator, fr_radius_decode_fail_t *reason)
See if the data pointed to by PTR is a valid RADIUS packet.
Definition base.c:548
ssize_t fr_radius_encode(fr_dbuff_t *dbuff, fr_pair_list_t *vps, fr_radius_encode_ctx_t *packet_ctx)
Definition base.c:954
#define FR_DEBUG_STRERROR_PRINTF_PUSH
Definition base.c:85
char const * fr_radius_packet_name[FR_RADIUS_CODE_MAX]
Definition base.c:116
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:289
int fr_radius_allow_reply(int code, bool allowed[static FR_RADIUS_CODE_MAX])
Definition base.c:231
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:2087
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:1972
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:1537
ssize_t fr_radius_encode_foreign(fr_dbuff_t *dbuff, fr_pair_list_t const *list)
Definition encode.c:1702
VQP attributes.
static rc_request_t * current
static char * secret
unsigned int has_tag
Attribute has a tag.
Definition radius.h:193
bool secure_transport
for TLS
Definition radius.h:130
@ FR_RADIUS_REQUIRE_MA_NO
Do not require Message-Authenticator.
Definition radius.h:63
@ FR_RADIUS_REQUIRE_MA_YES
Require Message-Authenticator.
Definition radius.h:64
@ FR_RADIUS_REQUIRE_MA_AUTO
Only require Message-Authenticator if we've previously received a packet from this client with Messag...
Definition radius.h:65
fr_radius_ctx_t const * common
Definition radius.h:136
uint8_t request_code
original code for the request.
Definition radius.h:169
uint8_t const * request_authenticator
Definition radius.h:162
unsigned int abinary
Attribute is in "abinary" format.
Definition radius.h:194
fr_radius_decode_fail_t
Failure reasons.
Definition radius.h:90
@ FR_RADIUS_FAIL_ATTRIBUTE_DECODE
Definition radius.h:106
@ FR_RADIUS_FAIL_UNEXPECTED_REQUEST_CODE
Definition radius.h:97
@ FR_RADIUS_FAIL_ATTRIBUTE_OVERFLOW
Definition radius.h:105
@ FR_RADIUS_FAIL_VERIFY
Definition radius.h:113
@ FR_RADIUS_FAIL_NONE
Definition radius.h:91
@ FR_RADIUS_FAIL_MA_INVALID_LENGTH
Definition radius.h:108
@ FR_RADIUS_FAIL_MIN_LENGTH_FIELD
Definition radius.h:94
@ FR_RADIUS_FAIL_INVALID_ATTRIBUTE
Definition radius.h:101
@ FR_RADIUS_FAIL_IO_ERROR
Definition radius.h:115
@ FR_RADIUS_FAIL_MAX_LENGTH_PACKET
Definition radius.h:93
@ FR_RADIUS_FAIL_MA_MISSING
Definition radius.h:109
@ FR_RADIUS_FAIL_TOO_MANY_ATTRIBUTES
Definition radius.h:99
@ FR_RADIUS_FAIL_UNEXPECTED_RESPONSE_CODE
Definition radius.h:98
@ FR_RADIUS_FAIL_UNKNOWN_PACKET_CODE
Definition radius.h:96
@ FR_RADIUS_FAIL_MIN_LENGTH_MISMATCH
Definition radius.h:95
@ FR_RADIUS_FAIL_NO_MATCHING_REQUEST
Definition radius.h:114
@ FR_RADIUS_FAIL_HEADER_OVERFLOW
Definition radius.h:103
@ FR_RADIUS_FAIL_PROXY_STATE_MISSING
Definition radius.h:111
@ FR_RADIUS_FAIL_MIN_LENGTH_PACKET
Definition radius.h:92
@ FR_RADIUS_FAIL_MAX
Definition radius.h:116
@ FR_RADIUS_FAIL_ATTRIBUTE_TOO_SHORT
Definition radius.h:104
@ FR_RADIUS_FAIL_MA_INVALID
Definition radius.h:110
char const * secret
Definition radius.h:127
unsigned int concat
Attribute is concatenated.
Definition radius.h:192
uint8_t const * end
end of the packet
Definition radius.h:165
bool limit_proxy_state
Don't allow Proxy-State in requests.
Definition radius.h:174
uint64_t proxy_state
Definition radius.h:132
uint8_t const * request_authenticator
Definition radius.h:138
unsigned int extended
Attribute is an extended attribute.
Definition radius.h:191
fr_radius_attr_flags_encrypt_t encrypt
Attribute is encrypted.
Definition radius.h:195
bool require_message_authenticator
Definition radius.h:173
size_t secret_length
Definition radius.h:128
bool verify
can skip verify for dynamic clients
Definition radius.h:172
fr_radius_ctx_t const * common
Definition radius.h:160
@ FR_RADIUS_LIMIT_PROXY_STATE_NO
Do not limit Proxy-State.
Definition radius.h:77
@ FR_RADIUS_LIMIT_PROXY_STATE_AUTO
Do not allow Proxy-State unless:
Definition radius.h:82
@ FR_RADIUS_LIMIT_PROXY_STATE_YES
Limit Proxy-State.
Definition radius.h:79
unsigned int long_extended
Attribute is a long extended attribute.
Definition radius.h:190
fr_radius_decode_fail_t reason
reason for decode failure
Definition radius.h:167
fr_radius_attr_flags_encrypt_t
Definition radius.h:181
@ RADIUS_FLAG_ENCRYPT_INVALID
Invalid encryption flag.
Definition radius.h:182
@ RADIUS_FLAG_ENCRYPT_USER_PASSWORD
Encrypt attribute RFC 2865 style.
Definition radius.h:184
@ RADIUS_FLAG_ENCRYPT_ASCEND_SECRET
Encrypt attribute ascend style.
Definition radius.h:186
@ RADIUS_FLAG_ENCRYPT_TUNNEL_PASSWORD
Encrypt attribute RFC 2868 style.
Definition radius.h:185
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:201
bool add_proxy_state
do we add a Proxy-State?
Definition radius.h:151
bool seen_message_authenticator
Definition radius.h:152
TALLOC_CTX * tmp_ctx
for temporary things cleaned up during decoding
Definition radius.h:164
uint32_t fr_rand(void)
Return a 32-bit random number.
Definition rand.c:105
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:653
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:204
#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:455
static size_t char fr_sbuff_t size_t inlen
Definition value.h:1024
static size_t char ** out
Definition value.h:1024